Production Cryptographic Failure Modes
Diagnose five recurring production crypto failures: AES-GCM nonce reuse, CBC padding oracles, JWT key confusion, cross-domain key reuse, and downgrade bugs.
In this lesson (4 bite-sized sections)
Strong primitives still fail when wired poorly
Real-world cryptographic breaches almost never break AES-256 or SHA-256 math. They exploit composition failures: choosing the right primitive and wiring it into a distributed system in a way that violates its security contract. Five failure patterns account for the overwhelming majority of production cryptographic incidents.
Symmetric traps: nonce reuse and padding oracles
1. AES-GCM nonce reuse. AES-GCM is a counter-mode stream cipher paired with a polynomial authenticator (GHASH). It demands that a (key, nonce) pair is never reused. In stateless fleets, teams routinely trigger collisions by resetting a counter to zero on container restart, deriving deterministic nonces from a mutable row_id, or picking random 96-bit nonces past the birthday bound (~2^32 encryptions per key).
Fix nonce reuse by rotating keys via per-message KDF subkeys, switching to XChaCha20-Poly1305 (192-bit random nonces safe to ~2^64 messages), or using AES-GCM-SIV (RFC 8452) for nonce-misuse resistance.
2. ECB mode and unauthenticated CBC padding oracles. Never use ECB (identical plaintext blocks produce identical ciphertext blocks). Never deploy AES-CBC without an Encrypt-then-MAC layer. Whenever a server decrypts unauthenticated CBC ciphertext and leaks whether PKCS#7 padding was valid, through distinct HTTP status codes (400 vs 422), error strings, or response timing, an attacker can decrypt arbitrary ciphertext one byte at a time in roughly 128 queries per byte without knowing the key. Replace raw CBC with an AEAD cipher (AES-GCM or ChaCha20-Poly1305).
Asymmetric and token traps: JWT confusion and key reuse
3. JWT alg: none and RS256-to-HS256 key confusion. A JWT header travels inside attacker-controlled input before verification runs. If verification middleware blindly reads header.alg and forwards a public RSA PEM key into a generic verify(token, key) function, an attacker can set alg: HS256 and sign a forged admin token using the server's public RSA key as the HMAC secret. Fix this by pinning the expected algorithm server-side (ignoring header.alg for key-type dispatch), binding each kid to one explicit algorithm, and rejecting alg: none unconditionally.
4. Cross-domain key reuse. Never use the same keypair or symmetric secret for two cryptographic roles, such as signing webhooks and wrapping DEKs with one RSA key, or deriving both encryption and MAC keys from the same raw secret. A decryption oracle or protocol flaw in one domain immediately compromises the other. Enforce strict key separation: distinct key IDs per purpose, or domain-separated subkeys via HKDF (info = "vault-wrap-v1" vs info = "webhook-sign-v1").
Unauthenticated metadata and protocol downgrades
5. Context swapping and unauthenticated headers. Encrypting a payload with AES-GCM authenticates only the ciphertext, not the surrounding JSON envelope or database row columns. If tenant_id, user_id, key_id, and alg sit outside AEAD Associated Data (AAD), an attacker with storage write access can copy one user's ciphertext into another user's row or tamper with envelope fields.
| Failure mode | Root mechanism | Exploit impact | Production fix |
|---|---|---|---|
| AES-GCM nonce reuse | Counter reset, deterministic ID nonce, or 96-bit birthday collision | Plaintext XOR leak plus GHASH key recovery and forgery | Per-key KDF rotation, XChaCha20-Poly1305 (192-bit), or AES-GCM-SIV |
| Unauthenticated CBC oracle | CBC without MAC plus distinct padding vs parse errors or timing | Full byte-by-byte plaintext recovery without the key | Replace with AEAD (AES-GCM / ChaCha20-Poly1305) |
| JWT algorithm confusion | Dispatching key verification from untrusted header.alg | Attacker signs forged tokens with public RSA PEM via HS256 | Pin allowed algorithm server-side; bind kid to one algorithm |
| Cross-domain key reuse | One RSA or symmetric key used for both signing and encryption | Oracle or breach in one flow compromises the other domain | One key per purpose, or HKDF with explicit domain labels |
| Unauthenticated metadata | Envelope headers or row IDs omitted from AEAD AAD | Cross-user ciphertext swap or silent downgrade to legacy cipher | Bind version, alg, key_id, tenant_id, and record_id into AAD |
Separate controls that eliminate the underlying cryptographic failure from patches that leave the system exploitable.
Sort 5 more items to check
Sources: NIST SP 800-38D, AES-GCM · RFC 8725, JWT Best Current Practices · RFC 9180, HPKE
Apply
Your turn
The task this lesson builds to.
Audit the QuickSign specification below, name both cryptographic failure modes it introduces, trace how each breaks in production, and specify the exact fixes.
QuickSign webhook and token spec (read only)
Webhook encryption. QuickSign delivers 15k encrypted webhook payloads per second from a stateless fleet of 40 containers sharing one yearly AES-256-GCM key. To avoid extra RNG syscalls, each container picks one random 32-bit prefix at boot and appends a 64-bit counter initialized to 0.
Admin callback auth. QuickSign's CallbackAuth middleware verifies incoming JWTs for both internal HS256 jobs and external RS256 webhooks by reading header.alg from the token and passing the configured key string (either the HMAC secret or the partner's RSA public PEM) into jwt.verify(token, key).
Think about
- What happens to QuickSign's 64-bit per-process counter across 40 stateless containers rotating and restarting every day?
- How can an external caller forge an admin callback token against QuickSign's shared JWT middleware?
- Why does simply shortening key rotation to monthly fail to eliminate the counter-restart collision?
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.
Critique the supplied VaultPay architecture memo below: uncover every cryptographic failure mode hidden in the design, trace the concrete exploit each one enables, and specify a targeted remediation and key-rotation plan.
VaultPay auth and secrets architecture memo (read only)
Document vault encryption. VaultPay stores encrypted tax filings in S3 under a long-lived per-tenant AES-256-GCM key (k-14). To save a database column when users revise filings, the uploader computes a deterministic 96-bit nonce from SHA-256(tenant_id || doc_id)[0..12] and overwrites the S3 object. Each object is saved as JSON: {"alg": "aes-256-gcm", "key_id": "k-14", "tenant_id": "t-99", "ct": "..."}. To stay compatible with old exports, the reader checks the JSON alg field and falls back to aes-256-cbc whenever specified; only ct is passed to the cipher.
Legacy CSV export endpoint. Bulk exports use AES-256-CBC with PKCS#7 padding and no separate MAC. When a caller downloads an export, the API returns HTTP 400 Invalid padding if decryption padding fails and HTTP 422 Malformed CSV if padding succeeds on corrupted text.
TokenGateway and master RSA key. TokenGateway validates both internal HS256 worker tokens and external RS256 customer tokens in one middleware by inspecting header.alg and passing the RSA public PEM from auth-jwks (or the worker secret) into verify(token, key). Finally, one 2048-bit keypair (vaultpay-master-rsa) both signs outgoing payment webhooks and directly wraps tenant DEKs using RSA-PKCS1v1.5.
Think about
- Look closely at how VaultPay constructs AES-256-GCM nonces when a user edits an existing document.
- Compare the HTTP status codes on the legacy CSV export endpoint and ask what they reveal to a caller.
- Trace what happens when TokenGateway reads header.alg, what vaultpay-master-rsa signs versus wraps, and what is authenticated inside the storage envelope.
Draft your design answer here Save your draft, compare against the model answer, and track progress.