AES GCM mode examples #159
tstiemerling
started this conversation in
General
Replies: 1 comment 2 replies
-
@tstiemerling Have you seen these tests https://github.com/PeculiarVentures/graphene/blob/master/test/aes.ts#L140-L164? Here is simple example const graphene = require("graphene-pk11");
const crypto = require("crypto");
const Module = graphene.Module;
const lib = "/usr/local/lib/softhsm/libsofthsm2.so"; // Path to SoftHSM lib
const slotIndex = 0; // Slot index
const pin = "12345"; // User PIN
const mod = Module.load(lib, "SoftHSM");
mod.initialize();
const slot = mod.getSlots(slotIndex, true);
if (!slot)
throw new Error("Slot is not found");
const session = slot.open(graphene.SessionFlag.RW_SESSION | graphene.SessionFlag.SERIAL_SESSION);
session.login(pin);
// Generate AES key
const aesKey = session.generateKey(graphene.KeyGenMechanism.AES, {
keyType: graphene.KeyType.AES,
valueLen: 256 / 8,
encrypt: true,
decrypt: true,
token: false,
});
// Encrypt data
const iv = crypto.randomBytes(12);
const alg = {
name: "AES_GCM",
params: new graphene.AesGcmParams(iv),
};
const cipher = session.createCipher(alg, aesKey);
const data = Buffer.from("Hello World!");
const enc = Buffer.concat([cipher.update(data), cipher.final()]);
// Decrypt data
const decipher = session.createDecipher(alg, aesKey);
const dec = Buffer.concat([decipher.update(enc), decipher.final()]);
console.log("Decrypted data:", dec.toString());
session.logout();
session.close();
mod.finalize(); |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
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
Beta Was this translation helpful? Give feedback.
All reactions