HAMZA KAREEM / CONCEPTS hamzawi.dev

Lesson 1322 min read

Envelope Encryption (KEK vs DEK)

Your KMS Key Never Touches Your Data

Cloud infrastructurecloud infrastructure / security & encryption

The common belief

"My S3 bucket / RDS volume / secret is encrypted with my KMS key. So rotating that key re-encrypts my data, and deleting it shreds the data."

Half right, and the wrong half is the expensive one.

Actually: KMS encrypts a key, not your bytes

AWS KMS (and GCP KMS, Azure Key Vault) refuses payloads over 4 KB. Your 4 GB video was never a candidate. What really happens:

1️⃣ App calls GenerateDataKey → KMS returns the SAME random 256-bit key twice: Plaintext (usable now) + CiphertextBlob (the same key, wrapped by your KMS key).

2️⃣ App encrypts the 4 GB locally with AES-256-GCM using the plaintext key — fast, local, no network.

3️⃣ App zeroes the plaintext key from memory and stores the CiphertextBlob next to the object.

4️⃣ To read: send the blob to KMS:Decrypt, get the data key back, decrypt locally.

That data key is the DEK (data encryption key). Your KMS key is the KEK (key encryption key). The KEK's only job is wrapping DEKs.

Concrete

aws kms generate-data-key --key-id alias/app --key-spec AES_256

→ { "Plaintext": "base64…", "CiphertextBlob": "base64…", "KeyId": "arn:…" }

Two fields, one key. Never log Plaintext. Persist only CiphertextBlob.

Now the nuance practitioners get wrong

"Key rotation re-encrypts my data."

No. Automatic KMS rotation creates new KEK backing material and uses it for future wraps. Old DEK blobs stay wrapped by the old material, which KMS keeps forever so old objects still decrypt. Your 10 TB is untouched. True re-encryption = re-wrapping every DEK yourself (ReEncrypt), or rewriting objects.

"Encryption context is just metadata."

It's AAD — authenticated additional data, cryptographically bound to the ciphertext. Encrypt with {"tenant":"42"} and Decrypt with {"tenant":"43"} fails. It's how you stop tenant A's blob being decrypted in tenant B's request path. Free defense-in-depth; most teams skip it.

"One KMS call per object is fine."

At 1 object = 1 Decrypt, 5k req/s blows past the region's shared cryptographic ops quota, and KMS becomes a hard dependency in your hot path — its latency and availability are now yours. Fix: a data key cache (the AWS Encryption SDK ships one) with limits on age, bytes encrypted, and messages per key. Reuse one DEK for 1000 objects → 1000x fewer KMS calls. The trade: a leaked cached DEK exposes all 1000.

✅ "Deleting the KMS key shreds the data" — this one IS true, and it's the superpower. Schedule key deletion and every DEK wrapped by it becomes undecryptable garbage. Crypto-shredding: GDPR erasure of 10 TB in one API call, no scan, no rewrite. Per-tenant KEKs make this per-tenant deletion.

⚠️ Which is also the foot-gun: one KEK for 50 tenants means you can never shred just one. Design the KEK boundary = your deletion boundary.

Your exercise

Take any service you own that stores user blobs. Answer three questions:

  • How many KMS Decrypt calls does one page load make? (Count them. Most people guess low.)
  • If a single tenant demands full erasure tomorrow, can you do it without rewriting storage?
  • Is your encryption context empty?

If the KEK boundary doesn't match your deletion boundary, sketch the migration now — it's much cheaper before the data exists.