PKCS#11 and creating AES Keys

1,676 views
Skip to first unread message

Kugel Ball

unread,
Feb 13, 2015, 2:55:05 AM2/13/15
to golan...@googlegroups.com
Hi all,

I am playing around with https://github.com/miekg/pkcs11 and when trying to create an AES Key... I get the following error: CKR_ATTRIBUTE_VALUE_INVALID

aesKeyTemplate := []*pkcs11.Attribute{
pkcs11.NewAttribute(pkcs11.CKA_CLASS, pkcs11.CKO_SECRET_KEY),
pkcs11.NewAttribute(pkcs11.CKA_KEY_TYPE, pkcs11.CKK_AES),
pkcs11.NewAttribute(pkcs11.CKA_ENCRYPT, true),
pkcs11.NewAttribute(pkcs11.CKA_DECRYPT, true),
pkcs11.NewAttribute(pkcs11.CKA_SIGN, true),
pkcs11.NewAttribute(pkcs11.CKA_VERIFY, true),
pkcs11.NewAttribute(pkcs11.CKA_TOKEN, true),
pkcs11.NewAttribute(pkcs11.CKA_PRIVATE, false),
pkcs11.NewAttribute(pkcs11.CKA_VALUE, 16), /* KeyLength */
pkcs11.NewAttribute(pkcs11.CKA_LABEL, "My First AES Key"), /* Name of Key */
}
 
aesKey, err := p.CreateObject(session, aesKeyTemplate)
if err != nil {
panic(fmt.Sprintf("GenerateKey() failed %s\n", err))
}

I am using the same flow as http://www-01.ibm.com/support/knowledgecenter/linuxonibm/com.ibm.linux.z.lxce/lxce_linklib_object_samples.html

I am not really sure, what I have done wrong.  Any help would be very welcome,

-keith



Miek Gieben

unread,
Feb 13, 2015, 6:28:51 AM2/13/15
to golan...@googlegroups.com
[ Quoting <ke...@keithball.net> in "[go-nuts] PKCS#11 and creating AES ..." ]
As discussed on twitter. Your underlaying HSM probably does not know what to do
with AES.

/Miek

--
Miek Gieben

Kugel Ball

unread,
Feb 13, 2015, 8:33:58 AM2/13/15
to golan...@googlegroups.com
Thanks for the tip.

I upgraded to SoftHSM2b2.  This allowed me to create the AES key.

However, I was unable to call encrypt.  


err = p.EncryptInit(session, []*pkcs11.Mechanism{pkcs11.NewMechanism(pkcs11.CKM_AES_CBC, nil)}, aesKey)
if err != nil {
panic(fmt.Sprintf("EncryptInit() failed %s\n", err))
}

It appears that the mechanism requires extra attributes, 

See the AES example here:

You make a comment in the code when creating the mechanism.

// TODO(miek): Not seen anything as elaborate as Attributes, so for know do nothing.

I managed to get a working example of RSA encryption using a public and private key pair, based off of the IBM documentation.

Thanks for your help and for publishing the pkcs11 code.  Very useful!

Miek Gieben

unread,
Feb 13, 2015, 10:53:30 AM2/13/15
to golan...@googlegroups.com
[ Quoting <ke...@keithball.net> in "Re: [go-nuts] PKCS#11 and creating ..." ]
>Thanks for the tip.
>
>I upgraded to SoftHSM2b2. This allowed me to create the AES key.
>
>However, I was unable to call encrypt.
>
>
>err = p.EncryptInit(session,
>[]*pkcs11.Mechanism{pkcs11.NewMechanism(pkcs11.CKM_AES_CBC, nil)}, aesKey)
>if err != nil {
>panic(fmt.Sprintf("EncryptInit() failed %s\n", err))
>}
>
>It appears that the mechanism requires extra attributes,
>
>See the AES example here:
>http://www-01.ibm.com/support/knowledgecenter/linuxonibm/com.ibm.linux.z.lxce/lxce_linklib_crypto_samples.html
>
>You make a comment in the code when creating the mechanism.
>
>// TODO(miek): Not seen anything as elaborate as Attributes, so for know do
>nothing.
>
>I managed to get a working example of RSA encryption using a public and
>private key pair, based off of the IBM documentation.
>
>Thanks for your help and for publishing the pkcs11 code. Very useful!

Thanks. Could you file a bug for the above?

etot...@gmail.com

unread,
May 11, 2015, 11:27:45 AM5/11/15
to golan...@googlegroups.com
I'm not sure if it's related, but I had a very similar issue when trying this with the python PKCS11 library:  PyKCS11.  The solution to my problem was that CKA_ID was not a specified attribute, and appeared to be required when CKA_TOKEN=True.  This is presumably because making it a TOKEN object requires it to be stored persistently on the card, and thus it needs an ID.  Session keys do not require a CKA_ID.  Hope that helps:

Reply all
Reply to author
Forward
0 new messages