Skip to content

Commit

Permalink
Update hsm-service.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielHougaard committed Nov 8, 2024
1 parent c80ce07 commit ae4f5f8
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions backend/src/ee/services/hsm/hsm-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ export const hsmServiceFactory = ({ hsmModule: { isInitialized, pkcs11 } }: THsm
const encryptedLength = pkcs11.C_Encrypt(sessionHandle, data, tempBuffer);

// Create a copy of the encrypted data using the actual length
const encryptedData = Buffer.from(tempBuffer.slice(0, encryptedLength.length || 16));
const encryptedData = Buffer.from(tempBuffer.subarray(0, encryptedLength.length || 16));

// Initialize HMAC
const hmacMechanism = {
Expand Down Expand Up @@ -275,7 +275,7 @@ export const hsmServiceFactory = ({ hsmModule: { isInitialized, pkcs11 } }: THsm
// Split encrypted data and HMAC
const hmac = encryptedDataWithHmac.subarray(-HMAC_SIZE); // Last 32 bytes are HMAC

const encryptedData = encryptedDataWithHmac.slice(0, -HMAC_SIZE); // Everything except last 32 bytes
const encryptedData = encryptedDataWithHmac.subarray(0, -HMAC_SIZE); // Everything except last 32 bytes

// Find the keys
const aesKey = $findKey(sessionHandle, HsmKeyType.AES);
Expand All @@ -300,6 +300,7 @@ export const hsmServiceFactory = ({ hsmModule: { isInitialized, pkcs11 } }: THsm
try {
pkcs11.C_VerifyFinal(sessionHandle, hmac);
} catch (error) {
logger.error(error, "HSM: HMAC verification failed");
throw new Error("Decryption failed"); // Generic error for failed verification
}

Expand Down

0 comments on commit ae4f5f8

Please sign in to comment.