AWS KMS: A Cloud Key Management Service
#kms#aws-kms#hsm#envelope-encryption#security
Contents
KMS (Key Management Service) keeps cryptographic keys inside an HSM (hardware security module) and performs key operations such as signing and encryption on the caller’s behalf through an API.
For example, if a release signing key is kept in AWS KMS instead of a PEM file, the server holds only the alias alias/signing, and signing becomes an API call that sends a hash and receives a signature. Copying the entire server disk does not reveal the private key.
AWS KMS, Google Cloud KMS, and Azure Key Vault belong to the same category; the explanations and examples here are based on AWS KMS.
1. What Is KMS?
If a signing or encryption key is kept as a PEM file on the server disk, anyone who copies the disk can produce the same signatures and decrypt the data outside the server.
Nothing records who used the key or when.
An application that uses KMS never receives the key.
It refers to the key by an alias or key ID, asks the KMS API to perform an operation, and receives only the result.
AWS KMS keys are created and used inside FIPS 140-3 validated HSMs, and every request leaves the principal, key, and operation in CloudTrail.
KMS keys fall into two broad kinds: symmetric and asymmetric.
A symmetric key uses one key for both encryption and decryption, so only a holder of the key can recover the data.
An asymmetric key is a mathematically related pair of a private key and a public key.
A signature made with the private key is verified with the public key, and since the private key cannot be computed from the public key, the public key can be given to anyone.
RSA keys can also be used to encrypt with the public key and decrypt with the private key.
How keys are kept inside the HSM is covered in 3.1 Key Hierarchy, permissions in 3.4 Key Policy, and logging in 3.5 CloudTrail.
2. Usage Examples
The example server build-server signs release files and backs up its build history database.
It uses an ECDSA P-256 key, alias/signing, for signing and a symmetric key, alias/backup, for backup encryption.
2.1 Signing a Release File
KMS Sign accepts messages of up to 4,096 bytes.
Release files are larger, so build-server computes the SHA-256 hash itself and sends that.
build-servercomputes the SHA-256 hash (32 bytes) of the release file- It calls
SignKeyId=alias/signing,SigningAlgorithm=ECDSA_SHA_256MessageType=DIGEST: the hash is already computed, so KMS skips the hashing step
- KMS signs with the private key inside the HSM and returns a DER-encoded signature
build-serverdistributes the signature with the release file- Users verify the signature with the public key they already have, without KMS
With DIGEST, the hash length must match the algorithm (32 bytes for ECDSA_SHA_256).
The AWS documentation warns that sending an unhashed message as DIGEST can compromise the security of the signature.
An ECDSA signature is a pair of integers r and s, and KMS returns it DER-encoded as specified by ANSI X9.62 and RFC 3279.
The format is the same as the signature Go’s ecdsa.SignASN1 produces with a file key, so verification code needs no changes.
The public key is fetched once with GetPublicKey and distributed to users.
It is returned as a DER-encoded SubjectPublicKeyInfo (SPKI) as defined in RFC 5280.
KMS also has a Verify API, but every call adds request cost and latency, so verifying locally with the public key is the better choice.
Assuming the signing key leaks, the difference between a file key and a KMS key becomes clear.
| File key | KMS key | |
|---|---|---|
| How long an attacker can sign | Indefinitely, even outside the server | Only while using the server’s credentials |
| Blocking new signatures | Users must discard the key | Immediately with DisableKey |
| Tracing the leak | No record | Sign events in CloudTrail |
Signatures already distributed remain valid for anyone holding the public key, so revoking past signatures has to be handled outside KMS.
2.2 Encrypting a Backup
KMS Encrypt accepts at most 4,096 bytes of plaintext, so a database backup of hundreds of MB cannot be encrypted with it directly.
The method used here is envelope encryption.
The data is encrypted locally with a data key, and KMS wraps only the data key with the KMS key.
build-servercallsGenerateDataKeyKeyId=alias/backup,KeySpec=AES_256- Encryption context
{"backup": "2026-11-20"}
- KMS returns a 32-byte data key in two forms
Plaintext: the plaintext data keyCiphertextBlob: the data key encrypted under the KMS key
build-serverencryptsbackup.tarwith AES-256-GCM using the plaintext data key and erases the plaintext data key from memory- It stores the ciphertext together with the
CiphertextBlob - To restore, it calls
Decrypton theCiphertextBlobwith the same encryption context to get the data key, and decrypts the backup with it
However large the data, only the 32-byte data key travels to and from KMS.
Even if the backup file and the encrypted data key are stolen together, the data key cannot be recovered without kms:Decrypt permission on alias/backup.
Local AES-GCM encryption is safe only if the same IV (initialization vector) is never used twice with the same data key.
NIST SP 800-38D recommends a 96-bit IV, and requesting a new data key for each backup also reduces the risk of IV reuse.
What the encryption context does is covered in 3.3 Encryption Context.
3. Core Structure
3.1 Key Hierarchy
The actual key value of a KMS key is called the HBK (HSM backing key).
For a symmetric key it is a 256-bit symmetric key; for an asymmetric key it is an RSA or elliptic curve private key.
The HBK is generated in an HSM and is designed never to leave the HSM in plaintext.
| Key | What it is | Location and lifetime |
|---|---|---|
| Domain key | A 256-bit AES-GCM key that wraps HBKs | Only in HSM memory, rotated daily (may be relaxed to at most weekly) |
| HBK | The key material of a KMS key | Plaintext only in HSM memory; stored as an EKT (exported key token) wrapped by a domain key |
| Derived key | A 256-bit AES-GCM key derived from the HBK for each encryption | Used once in HSM memory and derived again on decryption |
Only EKTs, the wrapped HBKs, remain in durable storage outside the HSM.
When a request arrives, the HSM unwraps the EKT with the domain key into memory, and no plaintext key remains after the operation.
Symmetric ciphertext contains information identifying the KMS key that encrypted it, so Decrypt needs no separate key ID.
KMS does not store the ciphertext it returns or the results it decrypts.
3.2 KMS Key
Each KMS key has a unique key ID and an ARN that includes the account and Region.
An alias is a name that points to a key ID and can be reassigned to another key, so changing keys does not require changing application settings.
The metadata holds the key usage (encryption, signing, MAC), key spec, and state (Enabled, Disabled, PendingDeletion).
The key spec and key usage are chosen when the KMS key is created and cannot be changed later.
| Kind | Key spec | Algorithms | Public key |
|---|---|---|---|
| Symmetric | SYMMETRIC_DEFAULT | AES-256-GCM encryption and decryption | None |
| RSA | RSA_2048, RSA_3072, RSA_4096 | Encryption RSAES_OAEP_SHA_1, RSAES_OAEP_SHA_256; signing RSASSA_PSS_SHA_*, RSASSA_PKCS1_V1_5_SHA_* | GetPublicKey |
| Elliptic curve | ECC_NIST_P256, P384, P521, ECC_SECG_P256K1 | ECDSA_SHA_256, ECDSA_SHA_384, ECDSA_SHA_512 | GetPublicKey |
| EdDSA | ECC_NIST_EDWARDS25519 | ED25519_SHA_512, ED25519_PH_SHA_512 | GetPublicKey |
| ML-DSA | ML_DSA_44, ML_DSA_65, ML_DSA_87 | ML_DSA_SHAKE_256 | GetPublicKey |
| HMAC | HMAC_224 to HMAC_512 | HMAC_SHA_224 to HMAC_SHA_512 (RFC 2104) | None |
For RSA signing, AWS recommends PSS over PKCS #1 v1.5.
ML-DSA is a lattice-based signature algorithm designed to resist quantum computers.
3.3 Encryption Context
SYMMETRIC_DEFAULT for symmetric keys is AES-256-GCM.
GCM produces an authentication tag along with the ciphertext, making it an AEAD (authenticated encryption) scheme in which decryption fails if even one bit of the ciphertext changes.
An encryption context is a set of key-value pairs attached to a request, and KMS uses it as the AAD (additional authenticated data) in GCM.
The encryption context is neither encrypted nor secret.
Instead, it is cryptographically bound to the ciphertext, and decryption fails with InvalidCiphertextException unless exactly the same context, including case, is supplied.
| On encryption | On decryption | Result |
|---|---|---|
{"backup": "2026-11-20"} | {"backup": "2026-11-20"} | Success |
{"backup": "2026-11-20"} | None | Failure |
{"backup": "2026-11-20"} | {"backup": "2026-11-21"} | Failure |
In the backups of 2.2, swapping in the CiphertextBlob of a backup from another date does not unwrap the data key, because the context does not match.
The context appears in plaintext in CloudTrail, which helps trace which data was decrypted.
Encryption context can be used only with symmetric KMS keys.
3.4 Key Policy
A KMS request passes only if the key policy allows it.
IAM policies are evaluated only when the key policy delegates permissions to the account.
The default key policy gives the account root full access, so permissions can be managed with the account’s IAM policies.
A grant is a delegated permission that allows a specific principal a specific operation, and it can be created or revoked without editing policies.
The build-server role gets only kms:Sign and kms:GetPublicKey on alias/signing, and kms:GenerateDataKey and kms:Decrypt on alias/backup.
Key creation (kms:CreateKey) and scheduling deletion (kms:ScheduleKeyDeletion) are given only to the operator role.
3.5 CloudTrail
KMS API calls are recorded as events in CloudTrail.
One signing request from build-server records the following fields.
| Field | Example value | What it tells |
|---|---|---|
eventName | Sign | The operation |
userIdentity.arn | …:assumed-role/build-server/i-0abc | The requesting principal |
requestParameters.keyId | alias/signing | The key used |
requestParameters.messageType | DIGEST | The signing mode |
eventTime, sourceIPAddress | Time, source | When and from where |
For symmetric encryption requests, the encryption context is recorded in plaintext as well.
Detection rules can flag Sign requests from an unusual principal or at an unusual time.
3.6 Key Rotation and Deletion
Automatic rotation is available only for symmetric encryption keys whose key material KMS generated.
The period is set between 90 and 2,560 days, and defaults to 365 days.
Each rotation adds new key material, and the existing key material keeps decrypting ciphertext created before the rotation.
KMS picks the key material that matches the ciphertext, so the key ID and ARN stay the same and applications do not notice the rotation.
Asymmetric and HMAC keys support neither automatic nor on-demand rotation.
To replace the signing key in 2.1, create a new KMS key, move alias/signing to it, and distribute the new public key to users.
ScheduleKeyDeletion sets a waiting period of 7 to 30 days (30 by default).
During the waiting period the key is in PendingDeletion and cannot be used, and the deletion can be cancelled.
Once the waiting period ends, data encrypted under the deleted key can never be decrypted again.
To stop use without deleting, disable the key with DisableKey and re-enable it when needed.
References
- AWS KMS cryptographic details: https://docs.aws.amazon.com/kms/latest/cryptographic-details/intro.html
- AWS KMS key hierarchy: https://docs.aws.amazon.com/kms/latest/cryptographic-details/key-hierarchy.html
- Choosing a key spec: https://docs.aws.amazon.com/kms/latest/developerguide/symm-asymm-choose-key-spec.html
- Encryption context: https://docs.aws.amazon.com/kms/latest/developerguide/encrypt_context.html
- GenerateDataKey API: https://docs.aws.amazon.com/kms/latest/APIReference/API_GenerateDataKey.html
- Sign API: https://docs.aws.amazon.com/kms/latest/APIReference/API_Sign.html
- GetPublicKey API: https://docs.aws.amazon.com/kms/latest/APIReference/API_GetPublicKey.html
- Key policies: https://docs.aws.amazon.com/kms/latest/developerguide/key-policies.html
- Key rotation: https://docs.aws.amazon.com/kms/latest/developerguide/rotate-keys.html
- Deleting keys: https://docs.aws.amazon.com/kms/latest/APIReference/API_ScheduleKeyDeletion.html
- Logging KMS requests with CloudTrail: https://docs.aws.amazon.com/kms/latest/developerguide/logging-using-cloudtrail.html
- NIST SP 800-38D (GCM): https://csrc.nist.gov/pubs/sp/800/38/d/final
- RFC 3279 (ECDSA signature encoding): https://www.rfc-editor.org/rfc/rfc3279
- RFC 5280 (SubjectPublicKeyInfo): https://www.rfc-editor.org/rfc/rfc5280