Skip to main content
Cloud basicsLesson 7 of 17

How Cloud KMS Encrypts Your Data (Envelope Encryption)

An app must store customer files encrypted, and the key must not sit in its code or config. Follow how a cloud KMS (AWS KMS here; Google Cloud KMS and Azure Key Vault work alike) protects the key, and how envelope encryption encrypts the data without the key ever leaving the KMS.

Data key—
Access—
Audit—
Key stateEnabled
Cloud KMSAppAccount AStorageDisk · object storeKMS APIalias/app-dataHSMKey material insideKey policyOn the key itselfIAM policyOn the callerAudit logCloudTrailAccount BOther teamWrite file
RequestResponse / reportWatchAppears in a later step

What you will learn

  • Why a KMS key stays inside the HSM
  • Envelope encryption with GenerateDataKey and Decrypt
  • Key policy and IAM policy must both allow a call
  • Rotation keeps old key material for decrypt
  • Scheduled deletion and crypto-shredding

When a KMS call fails​

Error (AWS KMS)Likely causeFix
AccessDeniedExceptionKey policy does not allow the caller or its account, or IAM policy lacks the actionRead the key policy first, then the caller's IAM policy. Cross-account needs both sides
DisabledExceptionThe key is disabledEnable it, or point the app at the right key
KMSInvalidStateExceptionThe key is pending deletion (or pending import)cancel-key-deletion if it was a mistake, then enable the key
InvalidCiphertextExceptionWrong or damaged ciphertext, or the encryption context does not match the one used to encryptPass exactly the same encryption context on Decrypt
ThrottlingExceptionThe account's shared request quota per second in that Region is exceededRetry with backoff, reuse data keys, use bucket keys, or ask for a quota increase
NotFoundExceptionWrong key ID, alias or RegionUse the full key ARN, especially across accounts

The same ideas exist elsewhere: Google Cloud KMS calls rotation "key versions", and Azure Key Vault uses soft-delete with a retention period instead of a deletion window.

Try it yourself​

aws kms create-key --description "app data" # Returns KeyId
aws kms create-alias --alias-name alias/app-data --target-key-id <key-id>
aws kms generate-data-key --key-id alias/app-data --key-spec AES_256 \
--query CiphertextBlob --output text | base64 --decode > datakey.enc # Keep only the encrypted copy
aws kms decrypt --ciphertext-blob fileb://datakey.enc \
--query KeyId --output text # KMS finds the key from the ciphertext
aws kms get-key-policy --key-id alias/app-data --policy-name default --output text # Who may use the key
aws kms enable-key-rotation --key-id <key-id> # 365 days unless --rotation-period-in-days
aws kms get-key-rotation-status --key-id <key-id>
aws cloudtrail lookup-events --lookup-attributes AttributeKey=EventName,AttributeValue=Decrypt --max-results 5 # Who used it
aws kms schedule-key-deletion --key-id <key-id> --pending-window-in-days 7 # Only on a test key
aws kms cancel-key-deletion --key-id <key-id> # Undo during the waiting period

Check yourself

  1. Why does an app use GenerateDataKey instead of sending all its data to the KMS Encrypt call?
  2. Automatic rotation is turned on. What happens to a file encrypted last year?
  3. A role has kms:Decrypt in its IAM policy, but the key policy neither lists the role nor lets the account use IAM. What happens?

Further reading: AWS KMS concepts · Data keys and envelope encryption (AWS) · Rotating KMS keys (AWS) · Envelope encryption (Google Cloud KMS) · Azure Key Vault overview · Cryptography a Developer Needs · Keeping Secrets Safe · Permission Checks on One API Request (IAM)