AEAD Symmetric Encryption & Envelope Keys
Why AES-GCM and ChaCha20-Poly1305 fail catastrophically on nonce reuse, how AAD stops record swapping, and how envelope encryption scales with KMS.
In this lesson (3 bite-sized sections)
Confidentiality without integrity is broken encryption
A common production bug is treating encryption as a privacy shield while ignoring tampering. Unauthenticated modes like AES-CBC or raw AES-CTR scramble bytes, but an active attacker can flip bits in transit and trigger predictable changes in plaintext or exploit padding oracles to decrypt traffic byte by byte. Modern systems therefore use AEAD (Authenticated Encryption with Associated Data), which binds confidentiality and integrity into one indivisible operation. Every encrypt call produces both ciphertext and a 128-bit authentication tag; decrypt refuses to release a single byte of plaintext unless that tag verifies.

Three AEAD constructions dominate engineering practice: AES-256-GCM (hardware-accelerated via AES-NI and ARMv8 crypto extensions, reaching multiple GB/s per core), ChaCha20-Poly1305 (constant-time in pure software and fast on mobile CPUs lacking AES hardware), and AES-256-GCM-SIV (nonce-misuse resistant, leaking only whether two plaintexts encrypted under the same nonce are identical rather than collapsing completely).
| Algorithm | Nonce size | Hardware speed | Nonce reuse failure | Best fit |
|---|---|---|---|---|
| AES-256-GCM | 96-bit (12 B) | Multi-GB/s with AES-NI | Catastrophic: leaks XOR of plaintexts and GHASH auth key | Servers, TLS 1.3, cloud KMS envelope encryption |
| ChaCha20-Poly1305 | 96-bit (12 B) | Fast in software, no AES-NI needed | Catastrophic: leaks XOR of plaintexts and Poly1305 key | Mobile clients, WireGuard, low-power edge nodes |
| XChaCha20-Poly1305 | 192-bit (24 B) | Fast in software | Random nonces safe past 2^64 messages per key | Stateless distributed writers without central counters |
| AES-256-GCM-SIV | 96-bit (12 B) | Two-pass, ~1.5-2x slower than GCM | Degrades gracefully: leaks only plaintext equality | High-risk stateless services where nonce bugs are likely |
Why nonce reuse destroys AES-GCM
A nonce (number used once) is public, not secret, yet reusing a (key, nonce) pair in AES-GCM or ChaCha20-Poly1305 is fatal. Both ciphers generate a pseudorandom keystream from (key, nonce) and XOR it with the plaintext. If two messages share a keystream, XORing their ciphertexts cancels the keystream completely and exposes P1 XOR P2. Worse, in AES-GCM, nonce reuse reveals the secret GHASH authentication key H via polynomial root finding, letting an attacker forge valid authentication tags for arbitrary messages forever under that key.
Even with a CSPRNG, random 96-bit nonces collide far sooner than intuition suggests because of the birthday bound: under a single AES-GCM key, collision probability reaches the NIST SP 800-38D safety threshold (2^-32) after roughly 2^32 invocations (about 4.3 billion messages). High-volume fleets therefore rotate DEKs well before hitting that ceiling, partition deterministic counters per worker, or switch to XChaCha20-Poly1305 (192-bit nonces) or AES-GCM-SIV for stateless writers.
Binding context with AAD and scaling with envelope encryption
Authenticated encryption alone proves a ciphertext came from your key, not which row or tenant it belongs to. An attacker with database write access can copy the encrypted salary blob from Tenant A into Tenant B's row (a confused-deputy swap), and the database decrypts it cleanly. Pass immutable context as AAD (Associated Authenticated Data), such as tenant_id:table:row_id:column_name. AAD is stored or reconstructed in plaintext and authenticated inside the tag without being encrypted, so moving a ciphertext to a different record causes decryption to fail immediately.
To avoid calling a hardware KMS on every request (and hitting KMS API quotas or network latency), production systems use envelope encryption. Your application requests or generates a unique 256-bit DEK (Data Encryption Key) per tenant, object, or batch, encrypts the payload locally with AES-256-GCM, and wraps only the 32-byte DEK with a KMS-resident KEK (Key Encryption Key). The wrapped DEK lives right beside the ciphertext.
- Request DEKKMS GenerateDataKey returns plaintext DEK + KMS-wrapped DEK
- Encrypt locallyAES-256-GCM(DEK, fresh nonce, plaintext, AAD=tenant:row)
- Zero plaintext DEKDrop raw DEK from memory after sealing the batch
- Persist envelopeStore ciphertext, 16-byte tag, nonce, wrapped DEK, key version
Match each component of an envelope-encrypted database record to its cryptographic job.
Sort 4 more items to check
Recap: always choose an AEAD over unauthenticated modes, guard nonces like a loaded weapon (or choose XChaCha20 / AES-GCM-SIV), bind tenant and record identifiers in AAD to block ciphertext swapping, and use DEK/KEK envelope encryption so KMS never sits on your bulk data path.
Sources: NIST SP 800-38D, AES-GCM · RFC 8439, ChaCha20-Poly1305 · RFC 8452, AES-GCM-SIV
Apply
Your turn
The task this lesson builds to.
Specify the field-level encryption layer for a multi-tenant HR SaaS storing encrypted salaries and tax IDs across 200 stateless API servers backed by Postgres and AWS KMS.
Think about
- How do you prevent 96-bit nonce collisions across 200 stateless API writers?
- What exact fields belong in AAD to stop an attacker swapping blobs between tenants or rows?
- How does DEK/KEK envelope encryption keep KMS off the hot per-row write path?
Draft your design answer here Save your draft, compare against the model answer, and track progress.
Practice
Make it stick
A second problem on the same idea, so it survives past today.
Audit an existing payments vault where a 3-node service encrypted 8 billion card tokens under a single AES-256-GCM key using random 96-bit nonces and no AAD. Lay out the exact incident risk, immediate containment, and zero-downtime migration to a safe envelope design.
Think about
- Why has 8 billion encryptions under one AES-GCM key already crossed the NIST safety boundary?
- Can you safely re-encrypt in place without a dual-read envelope version header?
- How do you migrate existing rows to carry AAD (
merchant_id|token_id) without downtime?
Draft your design answer here Save your draft, compare against the model answer, and track progress.