AWS KMS: A Cloud Key Management Service

security10 min한국어

#kms#aws-kms#hsm#envelope-encryption#security

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.

File key Server Holds signing.pem Copy the disk Attacker Gets private key KMS Server Holds alias only AWS KMS (HSM) Private key Sign(hash) signature
A file key is copied along with the disk; a KMS key never leaves the HSM.

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.

build-server Authenticates with IAM role KMS API Checks permissions HSM key material CloudTrail Event per request Sign(digest) signature Operation Log Never leaves in plaintext
The application never receives the key, only the result of the operation.

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.

Symmetric key: the same key encrypts and decrypts Plaintext Ciphertext Plaintext Encrypt with key K Decrypt with K Asymmetric key: a private and public key pair Message signature Verified Sign with private key Verify with public key
A symmetric key is used on both sides; an asymmetric key splits the roles between a private and a public 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.

Release file Created by build-server 32-byte hash Sent as DIGEST KMS Sign ECDSA_SHA_256 SHA-256 Public key GetPublicKey, once User Verifies without KMS signature DER-encoded (r, s)
The signature is created inside the KMS HSM; verification needs only the public key.
  1. build-server computes the SHA-256 hash (32 bytes) of the release file
  2. It calls Sign
    • KeyId=alias/signing, SigningAlgorithm=ECDSA_SHA_256
    • MessageType=DIGEST: the hash is already computed, so KMS skips the hashing step
  3. KMS signs with the private key inside the HSM and returns a DER-encoded signature
  4. build-server distributes the signature with the release file
  5. Users verify the signature with the public key they already have, without KMS
RAW Original message Up to 4,096 bytes KMS Hash, then sign signature Sent as is DIGEST Release file No size limit KMS Sign only signature SHA-256
With RAW, KMS computes the hash; with DIGEST, the hash is computed beforehand.

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.

30 len 02 len r (32-33 bytes) 02 len s (32-33 bytes) SEQUENCE INTEGER r INTEGER s A value whose top bit is 1 gets a leading 0x00 to stay positive, making it 33 bytes Max length 72 bytes = header 2 + (2 + 33) × 2
A signature is the two integers r and s wrapped in a DER SEQUENCE.

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 keyKMS key
How long an attacker can signIndefinitely, even outside the serverOnly while using the server’s credentials
Blocking new signaturesUsers must discard the keyImmediately with DisableKey
Tracing the leakNo recordSign 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.

KMS key alias/backup build-server Encrypts backups 1. GenerateDataKey plaintext data key 32 bytes encrypted data key CiphertextBlob 2. Returns both Erased after use backup.tar AES-256-GCM Storage Data + wrapped key
Large data is encrypted locally with a data key; KMS only wraps the data key.
  1. build-server calls GenerateDataKey
    • KeyId=alias/backup, KeySpec=AES_256
    • Encryption context {"backup": "2026-11-20"}
  2. KMS returns a 32-byte data key in two forms
    • Plaintext: the plaintext data key
    • CiphertextBlob: the data key encrypted under the KMS key
  3. build-server encrypts backup.tar with AES-256-GCM using the plaintext data key and erases the plaintext data key from memory
  4. It stores the ciphertext together with the CiphertextBlob
  5. To restore, it calls Decrypt on the CiphertextBlob with 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.

HSM memory domain key 256-bit AES-GCM HBK KMS key material derived key New per encryption Wraps Derive Plaintext keys used only in HSM memory durable storage Stores EKT (wrapped HBK) Ciphertext Identifies the KMS key EKT (wrapped HBK)
Plaintext keys exist only in HSM memory; outside the HSM there are only wrapped keys (EKTs) and ciphertext.
KeyWhat it isLocation and lifetime
Domain keyA 256-bit AES-GCM key that wraps HBKsOnly in HSM memory, rotated daily (may be relaxed to at most weekly)
HBKThe key material of a KMS keyPlaintext only in HSM memory; stored as an EKT (exported key token) wrapped by a domain key
Derived keyA 256-bit AES-GCM key derived from the HBK for each encryptionUsed 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.

alias/signing Can be reassigned KMS key 1234abcd-… Metadata Key usage, spec, state key material No plaintext outside HSM key policy One per key ARN Account, Region
The name an application uses and the actual key material are separate layers.

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.

KindKey specAlgorithmsPublic key
SymmetricSYMMETRIC_DEFAULTAES-256-GCM encryption and decryptionNone
RSARSA_2048, RSA_3072, RSA_4096Encryption RSAES_OAEP_SHA_1, RSAES_OAEP_SHA_256; signing RSASSA_PSS_SHA_*, RSASSA_PKCS1_V1_5_SHA_*GetPublicKey
Elliptic curveECC_NIST_P256, P384, P521, ECC_SECG_P256K1ECDSA_SHA_256, ECDSA_SHA_384, ECDSA_SHA_512GetPublicKey
EdDSAECC_NIST_EDWARDS25519ED25519_SHA_512, ED25519_PH_SHA_512GetPublicKey
ML-DSAML_DSA_44, ML_DSA_65, ML_DSA_87ML_DSA_SHAKE_256GetPublicKey
HMACHMAC_224 to HMAC_512HMAC_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 encryptionOn decryptionResult
{"backup": "2026-11-20"}{"backup": "2026-11-20"}Success
{"backup": "2026-11-20"}NoneFailure
{"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.

build-server role kms:Sign request key policy One per key, required IAM policy If delegated grant Principal, operation
The key policy comes first. IAM policies alone cannot grant use of a KMS key.

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.

FieldExample valueWhat it tells
eventNameSignThe operation
userIdentity.arn…:assumed-role/build-server/i-0abcThe requesting principal
requestParameters.keyIdalias/signingThe key used
requestParameters.messageTypeDIGESTThe signing mode
eventTime, sourceIPAddressTime, sourceWhen 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

Symmetric key alias/backup: automatic rotation Key ID, ARN unchanged key material v1 Decrypt only key material v2 Decrypt only key material v3 Encrypt, decrypt Rotate Rotate Asymmetric key alias/signing: replaced by a new key alias/signing Key A (old) Key B (new) Move alias Distribute new public key Deletion Enabled PendingDeletion Deleted Schedule Cancellable 7-30 day wait
A symmetric key accumulates key material within the same key; an asymmetric key is replaced by moving the alias.

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