C_WrapKey to encrypt (wrap) a key and C_UnwrapKey to import a previously wrapped key.
Functions
C_WrapKey
Encrypts (wraps) a key object using a wrapping key.Handle of the open session.
Wrapping mechanism, such as
CKM_AES_KEY_WRAP or CKM_RSA_PKCS_OAEP. Some mechanisms require parameters (for example, CKM_AES_CBC requires a 16-byte IV).Handle of the key to use for wrapping. Must have
CKA_WRAP = CK_TRUE.Handle of the key to be wrapped. Must have
CKA_EXTRACTABLE = CK_TRUE.Buffer to receive the wrapped key bytes. Pass
NULL_PTR on the first call to get the required size.On input: size of
pWrappedKey. On output: number of bytes written or required.C_UnwrapKey
Decrypts (unwraps) a wrapped key and creates a new key object on the token.Handle of the key to use for unwrapping. Must have
CKA_UNWRAP = CK_TRUE.The wrapped key bytes to import.
Length of
pWrappedKey in bytes.Attributes for the newly created key object (type, usage flags, token storage, etc.).
Number of attributes in
pTemplate.Receives the handle of the newly created key object.
Supported wrapping mechanisms
| Mechanism | Wrapping key type | Parameters | Notes |
|---|---|---|---|
CKM_AES_KEY_WRAP | CKK_AES | None | RFC 3394 AES Key Wrap; requires HAVE_AES_KEY_WRAP build |
CKM_AES_KEY_WRAP_PAD | CKK_AES | None | RFC 5649 AES Key Wrap with padding; requires HAVE_AES_KEY_WRAP_PAD build |
CKM_AES_CBC | CKK_AES | 16-byte IV | |
CKM_AES_CBC_PAD | CKK_AES | 16-byte IV | PKCS#7 padding; added in SoftHSM v2.7.0 |
CKM_RSA_PKCS | CKK_RSA (public key) | None | PKCS#1 v1.5 encryption |
CKM_RSA_PKCS_OAEP | CKK_RSA (public key) | CK_RSA_PKCS_OAEP_PARAMS | OAEP encryption |
CKM_RSA_AES_KEY_WRAP | CKK_RSA (public key) | CK_RSA_AES_KEY_WRAP_PARAMS | RSA-OAEP wraps an ephemeral AES key, which then wraps the target key; added in SoftHSM v2.7.0 |
Required key attributes
On the wrapping key
| Attribute | Value required |
|---|---|
CKA_WRAP | CK_TRUE |
CKM_RSA_PKCS, CKM_RSA_PKCS_OAEP, CKM_RSA_AES_KEY_WRAP) require a CKO_PUBLIC_KEY of type CKK_RSA. AES wrapping mechanisms require a CKO_SECRET_KEY of type CKK_AES.
On the key being wrapped
| Attribute | Value required |
|---|---|
CKA_EXTRACTABLE | CK_TRUE |
Error codes
| Return value | Meaning |
|---|---|
CKR_OK | Success |
CKR_ARGUMENTS_BAD | Mechanism parameters are missing or malformed |
CKR_WRAPPING_KEY_HANDLE_INVALID | The wrapping key handle is invalid |
CKR_WRAPPING_KEY_TYPE_INCONSISTENT | The wrapping key type does not match the mechanism |
CKR_KEY_NOT_WRAPPABLE | The key to be wrapped does not have CKA_EXTRACTABLE = CK_TRUE |
CKR_KEY_UNEXTRACTABLE | The key cannot be exported |
CKR_KEY_FUNCTION_NOT_PERMITTED | The wrapping key does not have CKA_WRAP = CK_TRUE |
CKR_MECHANISM_INVALID | The mechanism is not supported |
CKR_BUFFER_TOO_SMALL | Output buffer is too small |
CKR_USER_NOT_LOGGED_IN | Private object access without login |