How to rotate HSM keys
This guide shows how to put an HSM-resident key in a keyset, schedule its rotation, rotate it by hand, use it by keyset address and repair a failed rotation. The exact rules behind each step are in the HSM keysets reference.
Prerequisites
- A KMS server configured with an HSM; see HSM keys & operations and the page of your HSM model. The examples use the
utimacomodel on slot 0; replace thehsm::utimaco::0::prefix with yours (see Multi-HSM support). ckmsconfigured for a user listed inhsm_admin. The examples assume an HSM admin; for other users see HSM key authorization model.- For scheduled rotation only: an HSM that stores
CKA_START_DATE/CKA_END_DATE(see HSM model support), and the scheduler enabled as described in Enable the scheduler.
Enroll an HSM key in a keyset
-
Create the key on the HSM, with its base UID as the key identifier:
ckms sym keys create --algorithm aes --number-of-bits 256 hsm::utimaco::0::my-aes-key -
Set the keyset name. For an HSM key it must be the key's base UID:
ckms sym keys set-rotation-policy \ --key-id hsm::utimaco::0::my-aes-key \ --rotation-name hsm::utimaco::0::my-aes-keyRotation policy set successfully. Unique identifier: hsm::utimaco::0::my-aes-keyThe key's
CKA_LABELbecomeshsm::utimaco::0::my-aes-key::0::my-aes-key@latest.
For an RSA or EC key pair, run the same command with ckms rsa keys or ckms ec keys on the private key UID.
Explicit enrollment is optional: a key that never joined a keyset is enrolled under its base UID when you arm auto-rotation on it (see below) or at its first manual re-key.
Arm or disarm auto-rotation
Arm the schedule by setting an interval of at least one day (86400 seconds) on the key, or on the private key of a pair. This example rotates every 30 days:
ckms sym keys set-rotation-policy --key-id hsm::utimaco::0::my-aes-key --interval 2592000
If the key is not in a keyset yet, setting the interval also enrolls it under its base UID. The scheduler rotates the key once today (UTC) reaches its CKA_END_DATE, then re-arms the new key with the same interval. Renewal warnings are logged 30, 7 and 1 days before.
Disarm it with an interval of 0; the key stays in its keyset:
ckms sym keys set-rotation-policy --key-id hsm::utimaco::0::my-aes-key --interval 0
Do not pass --offset (RotateOffset is refused) and do not set RotateAutomatic: --interval alone arms and disarms the schedule. Do not set the interval on the public key of a pair.
Rotate an HSM key manually
Rotate an AES key by naming its base UID; the server always rotates the keyset head:
ckms sym keys re-key --key-id hsm::utimaco::0::my-aes-key
The symmetric key was successfully refreshed.
Unique identifier: hsm::utimaco::0::my-aes-key@1
Each further re-key of the base UID returns @2, @3, and so on.
Rotate an RSA or EC key pair by naming the private key:
ckms rsa keys re-key --key-id hsm::utimaco::0::my-rsa-key
The key pair was successfully rotated.
Unique identifier: hsm::utimaco::0::my-rsa-key@1
The new public key is hsm::utimaco::0::my-rsa-key@1_pk.
Do not re-key an explicit old generation: once @1 exists, --key-id hsm::utimaco::0::my-aes-key@0 (or @first) fails with Invalid_Field and a message containing is not the latest in its keyset. Use the base UID or @latest instead.
Encrypt, decrypt, sign and verify by keyset address
Encrypt with the base UID: the server uses the keyset head.
ckms sym encrypt --key-id hsm::utimaco::0::my-aes-key --output-file data.enc data.bin
Decrypt with the base UID: the server tries each generation, newest first, so data encrypted before a rotation still decrypts.
ckms sym decrypt --key-id hsm::utimaco::0::my-aes-key --output-file data.out data.enc
Decrypting with a mode that cannot detect a wrong key (for example --data-encryption-algorithm aes-cbc) is refused for the base UID. Name the generation that encrypted the data instead:
ckms sym decrypt --key-id hsm::utimaco::0::my-aes-key@1 --data-encryption-algorithm aes-cbc --output-file data.out data.enc
Sign with the private key's base UID and verify with the public key's base UID; verification tries every generation until one accepts the signature:
ckms rsa sign --key-id hsm::utimaco::0::my-rsa-key --output-file data.sig data.bin
ckms rsa sign-verify --key-id hsm::utimaco::0::my-rsa-key_pk data.bin data.sig
Encrypt, Sign and MAC with an old generation (@0, @first, or @N that is no longer the head) fail with Wrong_Key_Lifecycle_State. The full matrix is in Addressing.
AES-GCM Encrypt by keyset address runs on the HSM and fails on an HSM that cannot generate the GCM IV (for example SoftHSM2, Kryoptic). See AES-GCM IVs and HSM FIPS mode.
Recover from an interrupted rotation
A rotation writes several labels one after the other: the old head, the new key and, for a key pair, the two public keys. If it fails after generating the new key but before every label is written, the keyset is left half-rotated, and every further rotation fails with Object_Already_Exists:
HSM key '<new uid>' already exists — generation <N> was already created on this HSM slot. …
Depending on where the rotation stopped, the new generation may already be in use: once the new private or AES key is labelled @latest it is the keyset head, so clients may have encrypted or signed with it, and database keys may have been re-wrapped under it, even though the public key labels were never updated. Do not destroy any generation before you know it is unused.
- Note the
<new uid>from the error message (for a scheduled rotation, the server log line starts with[auto-rotate] Failed to rotate key). - Stop writes to the keyset: disarm auto-rotation (
--interval 0, see Arm or disarm auto-rotation) and pause the applications that encrypt or sign with it. - Inspect the state of every generation and its public key: read
CKA_LABELwith the vendor tools orpkcs11-tool --list-objects, and check whether data, signatures or database keys wrapped by the new generation exist. - Prefer repairing the labels over deleting keys: an HSM operator writes the missing labels with the vendor tools, in the format given in
CKA_LABELformat, so that the old head is retired and the new generation (and its public key) is labelled as the head. - Destroy a generation only when step 3 showed that nothing depends on it, then rotate again with
ckms sym keys re-key --key-id hsm::utimaco::0::my-aes-key(orckms rsa keys re-keyfor a pair). - Re-arm auto-rotation.