Skip to content
CryptoVault

Chapter VIII

When crypto fails

The math rarely breaks. Implementations, randomness, old options and people do.

  1. VIII.1Side channels
  2. VIII.2Bad randomness
  3. VIII.3Padding oracles
  4. VIII.4Downgrade attacks
  5. VIII.5Implementation bugs
  6. VIII.6Key management
  7. VIII.7The short list of rules

Side channels

Time, power and cache behavior can leak a secret that the math protects. Compare secrets in constant time.

Comparing a secret with === stops at the first wrong byte: a guess that matches more takes longer to reject.

secret tag7e3a9ftime to reject “7e0000” … “7ef000”early exit, ===3 is slower: keep it0123456789abcdefconstant timeall equal: nothing to keep0123456789abcdef
6 positions × 16 candidates: 96 guesses, instead of 16,777,216 for brute force.
Try it: follow the clock

Tag recovered so far ______

The server’s response time for each candidate at position 1. Click the one to keep.

Simulated timing: each matching character costs one more trip around the comparison loop. Real attacks repeat each guess many times to average out network noise.

Use a constant-time comparison, such as crypto.timingSafeEqual.

Bad randomness

A predictable or repeated random number turns the strongest algorithm into a guessing game.

A 2006 Debian patch left OpenSSL’s generator a single variable, the process ID, which Linux then capped at 32,767.

the serverthe attackerprocess ID 9743every ID, 1 to 32,767seedthe same generatorseedthe same generator385a3c730f17ccb1…match at try 9,743=what it looks like: 128 bitswhat it holds: 32,767 keys, 15 bits
The key looks random, but only 32,767 keys could ever come out.
PitfallMath.random() and time seeds are not cryptographic: use the operating system’s CSPRNG (chapter II).

Padding oracles

If a server reveals whether the padding was valid, an attacker can decrypt CBC ciphertext byte by byte.

CBC decryption checks the padding (chapter IV). If “bad padding” gets a different answer, even just a faster one, the server is an oracle Mallory can question.

C1′, forged by Mallory…000000gC2, the target…6e24f970decryptI…81aa135binside the serverP′…81aa1301padding?g = 00…59: nog = 5a: yesso I ends in 5a ⊕ 01 = 5band P ends in 5b ⊕ 32 (real C1) = 69, “i”
Then she aims for 02 02, 03 03 03…: at most 256 guesses per byte, and never the key.
Try it: peel a message apart
················
················
················

Questions asked to the server 0. Bytes recovered 0 of 48. Key used: none.

Bytes come from the right of each block, the way the attack works. The last block ends in PKCS#7 padding, shown in hex.

Use an AEAD. Failing that, encrypt-then-MAC, and reject a bad MAC before touching the padding.

Downgrade attacks

If an attacker can push both sides to an old, weak option, the strong options don’t matter.

Mallory, in the middle, rewrites the offer so only weak options survive: FREAK and Logjam forced 512-bit export keys, POODLE forced SSL 3.0.

Alice offersthe server receivesTLS 1.3×TLS 1.2TLS 1.2X25519×DHE 2048-bit×DHE_EXPORT 512-bitMMallorypicks TLS 1.2, DHE_EXPORTa TLS 1.3 server pushed to TLS 1.2 ends its signed random with…444f574e47524401DOWNGRDAlice offered TLS 1.3: she sees the mark and aborts
Without the edit, the server would have picked TLS 1.3 and X25519. Its signature covers the mark, so Mallory cannot erase it.
TLS 1.3 dropped the weak options, signs the whole handshake, and marks a forced fallback.

Implementation bugs

Heartbleed broke no cipher: a missing bounds check let anyone read server memory, private keys included.

Heartbleed broke no cipher: OpenSSL’s heartbeat echoed back as many bytes as the request claimed, up to 64 KB, unchecked.

MMallorylength64payloadbird“send it back”server memorybird..GET/inboxHTTP/1.1..Cookie:session=8f3a91c2d07e..POST/login..user=alice64 bytessent back
4 bytes in, 64 out. The fix, OpenSSL 1.0.1g: drop any request whose claimed length exceeds the payload.
PitfallCheck every length an attacker sends. Apple’s “goto fail” (2014) was one duplicated line that skipped a signature check.

Key management

Most breaches steal keys rather than break algorithms. Keep keys out of code, limit who can use them, rotate them.

Attackers rarely break AES; they find the key. Keep it in a hardware security module (HSM) or a cloud KMS, and wrap data keys under a key-encryption key (KEK) held there.

where the key sitsgit repositorycontainer imagelog fileKMS or HSMUber, 2016inside the KMS or HSMKEK 00010203…wrapsdata key 001122…encryptsthe data, gigabytesstored as1fa68b0a8112b447…24 bytes, next to the data
Rotating the KEK re-wraps these 24 bytes; the data itself is never touched.
One key, one purpose, least privilege. Plan rotation and revocation before you need them.

The short list of rules

Use a vetted library’s high-level API, AEAD, modern curves and Argon2id. Never invent your own primitive or protocol.

NeedUseAvoid
EncryptionAES-256-GCM, ChaCha20-Poly1305ECB, DES, RC4, unauthenticated CBC
Integrity / MACHMAC-SHA-256, the AEAD tagMD5, SHA-1, a bare hash as a MAC
SignaturesEd25519, ECDSA P-256textbook RSA, a reused ECDSA nonce
Key exchangeX25519 (hybrid with ML-KEM)export-grade groups, unauthenticated DH
PasswordsArgon2id, scrypt, bcryptplain SHA-256, unsalted or fast hashes
Randomnessthe operating system’s CSPRNGMath.random(), time or PID seeds
Don’t roll your own: use a vetted library’s high-level API, such as libsodium or Google’s Tink.

In one breath

  1. Side channels. Time, power and cache behavior can leak a secret that the math protects. Compare secrets in constant time.
  2. Bad randomness. A predictable or repeated random number turns the strongest algorithm into a guessing game.
  3. Padding oracles. If a server reveals whether the padding was valid, an attacker can decrypt CBC ciphertext byte by byte.
  4. Downgrade attacks. If an attacker can push both sides to an old, weak option, the strong options don’t matter.
  5. Implementation bugs. Heartbleed broke no cipher: a missing bounds check let anyone read server memory, private keys included.
  6. Key management. Most breaches steal keys rather than break algorithms. Keep keys out of code, limit who can use them, rotate them.
  7. The short list of rules. Use a vetted library’s high-level API, AEAD, modern curves and Argon2id. Never invent your own primitive or protocol.