Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AES_GCM with Luna: TypeError: Argument 1 has wrong type. Property 'type' should be a Number #107

Open
cveto opened this issue Apr 8, 2024 · 0 comments

Comments

@cveto
Copy link

cveto commented Apr 8, 2024

I am trying to use pkcs11js with Thales Luna HSM. Works fine with AES_CBC, but getting it to work with AES_GCM is giving me some trouble.

I think the issue could be with the IV parameter, but I am not sure.
With Luna, IV can not be provided to the HSM, but the HSM generates it. in C, I can get it working by just setting pointer of IV in CK_GCM_PARAMS to NULL
params.pIv = NULL;

The error I get which is triggered just after C_EcryptInit is:

TypeError: Argument 1 has wrong type. Property 'type' should be a Number
    at PKCS11.C_EncryptInit (/home/e143810/nodejs/node_modules/pkcs11js/index.js:77:22)
    at Object.<anonymous> (/home/e143810/nodejs/f_encrypt_AES128GCM.js:85:12)

The code I am using is quite basic:

 /**
    * Your app code here
    */
    var template = [
        { type: pkcs11js.CKA_CLASS, value: pkcs11js.CKO_SECRET_KEY },
        { type: pkcs11js.CKA_TOKEN, value: false },
        { type: pkcs11js.CKA_LABEL, value: "My AES Key2" },
        { type: pkcs11js.CKA_VALUE_LEN, value: 256 / 8 },
        { type: pkcs11js.CKA_ENCRYPT, value: true },
        { type: pkcs11js.CKA_DECRYPT, value: true },
    ];
    var secretKey = pkcs11.C_GenerateKey(session, { mechanism: pkcs11js.CKM_AES_KEY_GEN }, template);
    console.log("key handle:",secretKey.toJSON());

    // Generate AES GCM parameters
    // Generate random IV and setup IV params
    //var iv = pkcs11.C_GenerateRandom(session, new Buffer.alloc(12));
    var iv = new Buffer.alloc(32);
    // Generate Additional Authentication Data(AAD) bytes
    var aad = new Buffer.from("AAAD");
    // Generate iv bits size
    var ivBits = 12*8;//96
    // Generate tag bits size
    var tagBits = 128;
    var type = 3;//MechParams.AesGCM;
    console.log("MechParams.AesGCM=",3);//MechParams.AesGCM);
    var gcm_params = {
      iv,
      ivBits: iv.length * 8,
      aad: aad,
      aadBits: aad.length * 8,
      tagBits,
    };

    pkcs11.C_EncryptInit(
        session,
        {
            mechanism: pkcs11js.CKM_AES_GCM,
            parameter: gcm_params
        },
        secretKey
    );


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant