3

I am trying to integrate safenet HSM with our application. I am writing the program in C. I am referring to the PKCS11 v2.20 cryptoki standard document from RSA labs. I need to generate an AES 256 bit key. While defining the template for key generation I am not sure what value needs to be passed for CKA_VALUE. While generating a DES3 key, I didn't provide this attribute and I was able to generate a key.

I searched for sample programs for CKA_LABEL but failed to find any solid examples in C. I found a couple of Java programs where they have used CKA_VALUE_LEN instead of CKA_VALUE. I am not sure if that will work.

This is the snippet given in the document. Most websites give only this snippet as an example. Nothing is specified for the array value.

CK_OBJECT_CLASS class = CKO_SECRET_KEY;
CK_KEY_TYPE keyType = CKK_AES;
CK_UTF8CHAR label[] = “An AES secret key object”;
CK_BYTE value[] = {...};
CK_BBOOL true = CK_TRUE;
CK_ATTRIBUTE template[] = {
{CKA_CLASS, &class, sizeof(class)},
{CKA_KEY_TYPE, &keyType, sizeof(keyType)},
{CKA_TOKEN, &true, sizeof(true)},
{CKA_LABEL, label, sizeof(label)-1},
{CKA_ENCRYPT, &true, sizeof(true)},
{CKA_VALUE, value, sizeof(value)}
};
2
  • I take it that you get an error if you don't pass a CKA_VALUE? What's the error? I can't think what AES-256 would need that DES3 doesn't - the keys here are just random bytes aren't they? That said, which param here specifies 256 bits?
    – Rup
    Dec 20, 2018 at 12:10
  • which param here specifies 256 bits >> That's the doubt even I had and tried specifying CKA_VALUE_LEN. And the error was gone. What @always_a_rookie_to_learn pointed out makes sense to me now!
    – Chen
    Dec 21, 2018 at 1:55

1 Answer 1

5

The CKA_VALUE is the actual value of the key. When you tell the HSM to generate a secret key, it will generate a key for you on the hardware based on the attributes you pass in secret key template, and set the value generated in the CKA_VALUE. This attribute however cannot be read/extracted nor it can be set when generating the key, because the HSM won't allow you to inject a key (directly) from the software nor allow you to extract it from the HSM (directly).

The CKA_VALUE_LEN is the length of the key you can tell the HSM to generate. The AES key can be of length 128, 192 or 256 bits. Depending on the key size you want you would set the CKA_VALUE_LEN as 16, 24 or 32 (key size as bytes).

2
  • Thanks @always_a_rookie_to_learn . I tried that and was able to generate a key.
    – Chen
    Dec 21, 2018 at 1:57
  • @Chen Glad it helped. If the solution solved your problem you can mark the answer as accepted, which will help others if they are facing the same issue. Dec 21, 2018 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.