C_GenerateKey for symmetric keys and C_GenerateKeyPair for asymmetric key pairs. Both take a mechanism and a template of CK_ATTRIBUTE values that control the generated key’s properties.
Functions
C_GenerateKey
Generates a symmetric key.Handle of the open session.
Key generation mechanism, such as
CKM_AES_KEY_GEN. No mechanism parameters are required for standard symmetric key generation.Array of
CK_ATTRIBUTE structures specifying the new key’s attributes. Must include at minimum CKA_VALUE_LEN for AES keys.Number of attributes in
pTemplate.Receives the handle of the newly created key object.
C_GenerateKeyPair
Generates an asymmetric key pair (public key + private key).Handle of the open session.
Key pair generation mechanism, such as
CKM_RSA_PKCS_KEY_PAIR_GEN or CKM_EC_KEY_PAIR_GEN. No mechanism parameters are used.Attributes for the public key object. For RSA, include
CKA_MODULUS_BITS and optionally CKA_PUBLIC_EXPONENT. For EC, include CKA_EC_PARAMS.Number of public key attributes.
Attributes for the private key object. Typically includes
CKA_SENSITIVE, CKA_EXTRACTABLE, CKA_SIGN, etc.Number of private key attributes.
Receives the handle of the generated public key.
Receives the handle of the generated private key.
Supported mechanisms
Symmetric key generation
| Mechanism | Key type | Required template attributes |
|---|---|---|
CKM_AES_KEY_GEN | CKK_AES | CKA_VALUE_LEN (16, 24, or 32) |
CKM_DES_KEY_GEN | CKK_DES | None (key length is fixed at 8 bytes) |
CKM_DES2_KEY_GEN | CKK_DES2 | None (key length is fixed at 16 bytes) |
CKM_DES3_KEY_GEN | CKK_DES3 | None (key length is fixed at 24 bytes) |
CKM_GENERIC_SECRET_KEY_GEN | CKK_GENERIC_SECRET | CKA_VALUE_LEN |
Asymmetric key pair generation
| Mechanism | Key type | Required template attributes |
|---|---|---|
CKM_RSA_PKCS_KEY_PAIR_GEN | CKK_RSA | CKA_MODULUS_BITS in the public template |
CKM_DSA_KEY_PAIR_GEN | CKK_DSA | DSA domain parameters (CKA_PRIME, CKA_SUBPRIME, CKA_BASE) in the public template |
CKM_DH_PKCS_KEY_PAIR_GEN | CKK_DH | DH parameters in the public template |
CKM_EC_KEY_PAIR_GEN | CKK_EC | CKA_EC_PARAMS (DER-encoded OID) in the public template |
CKM_EC_EDWARDS_KEY_PAIR_GEN | CKK_EC_EDWARDS | CKA_EC_PARAMS in the public template; requires WITH_EDDSA build |
CKM_GOSTR3410_KEY_PAIR_GEN | CKK_GOSTR3410 | GOST parameters; requires WITH_GOST build |
CKM_ML_DSA_KEY_PAIR_GEN | CKK_ML_DSA | ML-DSA parameter set in the public template; requires WITH_ML_DSA build |
Key attributes
Common attributes for all keys
| Attribute | Type | Default | Description |
|---|---|---|---|
CKA_TOKEN | CK_BBOOL | CK_FALSE | If CK_TRUE, the key is stored persistently on the token |
CKA_PRIVATE | CK_BBOOL | Public key: CK_FALSE; Private key: CK_TRUE | If CK_TRUE, the user must be logged in to access the key |
CKA_LABEL | CK_UTF8CHAR[] | Empty | Human-readable label |
CKA_ID | CK_BYTE[] | Empty | Application-defined key identifier |
CKA_SENSITIVE | CK_BBOOL | CK_FALSE | If CK_TRUE, CKA_VALUE cannot be read via C_GetAttributeValue |
CKA_EXTRACTABLE | CK_BBOOL | CK_TRUE | If CK_FALSE, the key cannot be wrapped out of the token |
CKA_ENCRYPT | CK_BBOOL | CK_FALSE | Permits use with C_EncryptInit |
CKA_DECRYPT | CK_BBOOL | CK_FALSE | Permits use with C_DecryptInit |
CKA_SIGN | CK_BBOOL | CK_FALSE | Permits use with C_SignInit |
CKA_VERIFY | CK_BBOOL | CK_FALSE | Permits use with C_VerifyInit |
CKA_WRAP | CK_BBOOL | CK_FALSE | Permits use with C_WrapKey |
CKA_UNWRAP | CK_BBOOL | CK_FALSE | Permits use with C_UnwrapKey |
CKA_DERIVE | CK_BBOOL | CK_FALSE | Permits use with C_DeriveKey |
AES key attributes
| Attribute | Description |
|---|---|
CKA_VALUE_LEN | Key length in bytes: 16 (AES-128), 24 (AES-192), or 32 (AES-256) |
RSA key pair attributes
| Attribute | Applies to | Description |
|---|---|---|
CKA_MODULUS_BITS | Public template | RSA modulus size in bits (e.g., 2048, 3072, 4096) |
CKA_PUBLIC_EXPONENT | Public template | Public exponent as a big-endian byte string; defaults to 65537 if omitted |
EC key pair attributes
| Attribute | Applies to | Description |
|---|---|---|
CKA_EC_PARAMS | Public template | DER-encoded OID of the named curve (e.g., P-256, P-384, P-521) |
The DER encoding for P-256 is
\x06\x08\x2a\x86\x48\xce\x3d\x03\x01\x07. Libraries such as libp11 or OpenSSL’s OBJ_txt2oid can produce these OID encodings.Error codes
| Return value | Meaning |
|---|---|
CKR_OK | Success |
CKR_ARGUMENTS_BAD | A required argument is NULL_PTR |
CKR_MECHANISM_INVALID | The mechanism is not supported |
CKR_TEMPLATE_INCOMPLETE | A required attribute is missing from the template |
CKR_TEMPLATE_INCONSISTENT | Template attributes conflict with the mechanism |
CKR_ATTRIBUTE_VALUE_INVALID | An attribute value is out of range |
CKR_SESSION_READ_ONLY | Token key requested in a read-only session |
CKR_USER_NOT_LOGGED_IN | Private key requested without logging in |