PeculiarVentures / graphene

A simple layer for interacting with PKCS #11 / PKCS11 / CryptoKI for Node in TypeScript. (Keywords: Javascript, PKCS#11, Crypto, Smart Card, HSM)
MIT License
169 stars 34 forks source link

AES GCM mode with SoftHSM2 fails to decrypt more than one block #160

Open tstiemerling opened 7 months ago

tstiemerling commented 7 months ago

Hi, the SoftHSM2 implementation of GCM decrypt does not return any data until you call C_DecryptFinal (not sure if other P11 implementations are the same), so the final() method in decipher.ts is failing since it only allocates one block for the plaintext, but SoftHSM2 is expecting a buffer equal to the plaintext length.

public final(): Buffer { const dec = Buffer.alloc(this.blockSize);

const res = this.lib.C_DecryptFinal(this.session.handle, dec);

return res;

}

Discussed in https://github.com/PeculiarVentures/graphene/discussions/159

Originally posted by **tstiemerling** April 3, 2024 Are there any examples of using AES GCM mode? I am trying to use graphene with SoftHSM2 (windows build) and I getting CKR_BUFFER_TOO_SMALL returned on decrypt (unit test). The offending SoftHSM code seems to be here: c:\build\src\softhsm-2.5.0-x64\src\lib\softhsm.cpp(3256): Output buffer too short ulEncryptedDataLen: 0x10 output buffer size: 0x20 blockSize: 0x10 remainingSize: 0x100 maxSize: 0x110