Functions
C_EncryptInit
Initializes an encryption operation in the session. You must call this beforeC_Encrypt, C_EncryptUpdate, or C_EncryptFinal.
Handle of the open session.
Pointer to a
CK_MECHANISM structure identifying the encryption algorithm and its parameters (for example, an IV for CBC mode or CK_GCM_PARAMS for GCM).Handle of the key object to use. The key must have
CKA_ENCRYPT set to CK_TRUE.SymEncryptInit for symmetric mechanisms or AsymEncryptInit for RSA.
C_Encrypt
Encrypts data in a single-part operation. CallC_EncryptInit first.
Handle of the open session.
Pointer to the plaintext data to encrypt.
Length of
pData in bytes.Pointer to the buffer that receives the ciphertext. Pass
NULL_PTR on the first call to retrieve the required buffer size.On input: size of
pEncryptedData. On output: number of bytes written (or required if pEncryptedData is NULL_PTR).CKR_DATA_LEN_RANGE is returned. With padding (CKM_AES_CBC_PAD, CKM_DES3_CBC_PAD) the output may be up to one block larger than the input. For GCM mode, the output is ulDataLen + tagBytes.
C_EncryptUpdate
Continues a multi-part encryption operation.C_EncryptFinal.
C_EncryptFinal
Finishes a multi-part encryption operation and returns any remaining ciphertext.C_EncryptFinal returns (success or failure) the session’s encrypt operation is cleared. A new C_EncryptInit is required before the next encrypt.
C_DecryptInit / C_Decrypt / C_DecryptUpdate / C_DecryptFinal
The decrypt functions mirror the encrypt functions exactly:| Encrypt function | Decrypt counterpart |
|---|---|
C_EncryptInit(hSession, pMechanism, hKey) | C_DecryptInit(hSession, pMechanism, hKey) |
C_Encrypt(hSession, pData, ulDataLen, pEncryptedData, pulEncryptedDataLen) | C_Decrypt(hSession, pEncryptedData, ulEncryptedDataLen, pData, pulDataLen) |
C_EncryptUpdate(...) | C_DecryptUpdate(...) |
C_EncryptFinal(...) | C_DecryptFinal(...) |
CKA_DECRYPT set to CK_TRUE. The same two-pass pattern applies to C_Decrypt — pass NULL_PTR for pData to get the required output buffer length.
For GCM decryption, authentication tag verification happens inside the cipher. If the tag does not match, the operation returns CKR_ENCRYPTED_DATA_INVALID.
Supported mechanisms
Symmetric mechanisms
| Mechanism | Key type | Parameters | Notes |
|---|---|---|---|
CKM_AES_ECB | CKK_AES | None | No padding; input must be a block multiple |
CKM_AES_CBC | CKK_AES | 16-byte IV (CK_BYTE[16]) | No padding |
CKM_AES_CBC_PAD | CKK_AES | 16-byte IV | PKCS#7 padding |
CKM_AES_CTR | CKK_AES | CK_AES_CTR_PARAMS | Stream cipher, no padding |
CKM_AES_GCM | CKK_AES | CK_GCM_PARAMS | Authenticated encryption |
CKM_DES3_ECB | CKK_DES2 or CKK_DES3 | None | |
CKM_DES3_CBC | CKK_DES2 or CKK_DES3 | 8-byte IV | |
CKM_DES3_CBC_PAD | CKK_DES2 or CKK_DES3 | 8-byte IV | PKCS#7 padding |
CKM_DES_ECB | CKK_DES | None | Not available in FIPS mode |
CKM_DES_CBC | CKK_DES | 8-byte IV | Not available in FIPS mode |
CKM_DES_CBC_PAD | CKK_DES | 8-byte IV | Not available in FIPS mode |
Asymmetric mechanisms
| Mechanism | Key type | Parameters |
|---|---|---|
CKM_RSA_PKCS | CKK_RSA | None |
CKM_RSA_PKCS_OAEP | CKK_RSA | CK_RSA_PKCS_OAEP_PARAMS |
CKM_RSA_X_509 | CKK_RSA | None |
Mechanism parameters
CK_RSA_PKCS_OAEP_PARAMS
Required forCKM_RSA_PKCS_OAEP.
CK_GCM_PARAMS
Required forCKM_AES_GCM.
CK_AES_CTR_PARAMS
Required forCKM_AES_CTR.
Error codes
| Return value | Meaning |
|---|---|
CKR_OK | Success |
CKR_ARGUMENTS_BAD | A required argument is NULL_PTR or the mechanism parameters are malformed |
CKR_KEY_FUNCTION_NOT_PERMITTED | The key does not have CKA_ENCRYPT / CKA_DECRYPT |
CKR_KEY_TYPE_INCONSISTENT | The key type does not match the mechanism |
CKR_DATA_LEN_RANGE | Input length is not a valid multiple for unpadded block cipher |
CKR_BUFFER_TOO_SMALL | Output buffer too small; required size is written to *pulEncryptedDataLen |
CKR_ENCRYPTED_DATA_INVALID | GCM authentication tag verification failed |
CKR_ENCRYPTED_DATA_LEN_RANGE | Ciphertext length is invalid for the mechanism |
CKR_OPERATION_ACTIVE | Another operation is already in progress on this session |
CKR_OPERATION_NOT_INITIALIZED | C_EncryptInit has not been called |
CKR_MECHANISM_INVALID | The mechanism is not supported |
CKR_MECHANISM_PARAM_INVALID | Mechanism parameters are out of range |
Prior to SoftHSM v2.7.0, some decryption failures that should have returned
CKR_ENCRYPTED_DATA_INVALID or CKR_ENCRYPTED_DATA_LEN_RANGE instead returned CKR_GENERAL_ERROR. This was corrected in v2.7.0.