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
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 cause | Fix |
|---|---|---|
| AccessDeniedException | Key policy does not allow the caller or its account, or IAM policy lacks the action | Read the key policy first, then the caller's IAM policy. Cross-account needs both sides |
| DisabledException | The key is disabled | Enable it, or point the app at the right key |
| KMSInvalidStateException | The key is pending deletion (or pending import) | cancel-key-deletion if it was a mistake, then enable the key |
| InvalidCiphertextException | Wrong or damaged ciphertext, or the encryption context does not match the one used to encrypt | Pass exactly the same encryption context on Decrypt |
| ThrottlingException | The account's shared request quota per second in that Region is exceeded | Retry with backoff, reuse data keys, use bucket keys, or ask for a quota increase |
| NotFoundException | Wrong key ID, alias or Region | Use 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
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)