Chapter VIII
When crypto fails
The math rarely breaks. Implementations, randomness, old options and people do.
- VIII.1Side channels
- VIII.2Bad randomness
- VIII.3Padding oracles
- VIII.4Downgrade attacks
- VIII.5Implementation bugs
- VIII.6Key management
- 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.
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.
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.
Math.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.
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.
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.
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.
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.
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.
| Need | Use | Avoid |
|---|---|---|
| Encryption | AES-256-GCM, ChaCha20-Poly1305 | ECB, DES, RC4, unauthenticated CBC |
| Integrity / MAC | HMAC-SHA-256, the AEAD tag | MD5, SHA-1, a bare hash as a MAC |
| Signatures | Ed25519, ECDSA P-256 | textbook RSA, a reused ECDSA nonce |
| Key exchange | X25519 (hybrid with ML-KEM) | export-grade groups, unauthenticated DH |
| Passwords | Argon2id, scrypt, bcrypt | plain SHA-256, unsalted or fast hashes |
| Randomness | the operating system’s CSPRNG | Math.random(), time or PID seeds |
In one breath
- Side channels. Time, power and cache behavior can leak a secret that the math protects. Compare secrets in constant time.
- Bad randomness. A predictable or repeated random number turns the strongest algorithm into a guessing game.
- Padding oracles. If a server reveals whether the padding was valid, an attacker can decrypt CBC ciphertext byte by byte.
- Downgrade attacks. If an attacker can push both sides to an old, weak option, the strong options don’t matter.
- Implementation bugs. Heartbleed broke no cipher: a missing bounds check let anyone read server memory, private keys included.
- Key management. Most breaches steal keys rather than break algorithms. Keep keys out of code, limit who can use them, rotate 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.