Common Cryptographic Attacks
How cryptography fails in practice — brute force and birthday attacks, nonce and key reuse, padding and other oracles, timing and other side channels, downgrade attacks, man-in-the-middle, bad randomness, implementation bugs like Heartbleed, key management failures, and a checklist for engineers.
Modern algorithms like AES, SHA-256 and Ed25519 have never been broken in practice. Yet cryptographic failures are common — because attackers target how crypto is used: parameters, randomness, implementations, protocols and keys. This lesson collects the patterns that recur in real breaches.
Attacks on the maths (mostly solved)
| Attack | Idea | Defence |
|---|---|---|
| Brute force | Try every key | Keys ≥ 128 bits (2¹²⁸ is infeasible); never derive keys from short passwords without a slow KDF |
| Birthday attack | Collisions appear after ~2ⁿ⁄² attempts | ≥ 256-bit hashes; limit data per key for 64-bit block ciphers (Sweet32) |
| Cryptanalysis of weak primitives | Exploit structural weaknesses | Don't use MD5, SHA-1, DES, RC4, ECB, 1024-bit RSA/DH |
| Quantum attacks (future) | Shor breaks RSA/ECC; Grover halves symmetric strength | Post-quantum hybrids; 256-bit symmetric keys for long-term secrets |
Misuse of good primitives
Nonce and key reuse
The most common way to break perfectly good algorithms:
- Stream ciphers / CTR / GCM with a repeated (key, nonce): ; with GCM, the attacker also recovers the authentication key and can forge messages. Real cases: WPA2's KRACK attack forced nonce reuse; many IoT devices use fixed IVs.
- ECDSA nonce reuse or bias reveals the private key (PS3, Bitcoin wallets — Digital signatures).
- Two-time pad: reusing a one-time pad (Venona).
Defences: library-managed nonces, counters, XChaCha20 or GCM-SIV, deterministic signatures (Ed25519).
Unauthenticated encryption
Encryption without integrity lets attackers modify ciphertexts: flipping bits in CTR mode, or abusing CBC's malleability. Always use an AEAD and verify before acting on decrypted data (Authenticated encryption).
ECB mode and static IVs
Patterns leak (the ECB penguin); predictable CBC IVs enabled the BEAST attack on TLS 1.0 (Block cipher modes).
Wrong primitive for the job
- Fast hashes for passwords → offline cracking (Password storage).
H(key ∥ message)as a MAC → length extension.- Encryption used where authentication was needed ("encrypted cookies" that can be tampered with).
- Base64 or XOR with a fixed key treated as "encryption".
Oracle attacks
An oracle is any behaviour that answers a yes/no question about secret data — and repeated questions can reveal everything.
| Oracle | Leak | Famous cases |
|---|---|---|
| Padding oracle | "Was the CBC padding valid?" (error, status code or timing) | ASP.NET (2010), POODLE, Lucky Thirteen |
| Bleichenbacher's RSA oracle | "Was the PKCS#1 v1.5 padding valid?" | 1998, again as ROBOT (2017) in major TLS stacks |
| Compression oracle | Compressed size of secret + attacker data reveals matching bytes | CRIME, BREACH (HTTP compression of pages containing secrets and reflected input) |
| Error/format oracles | Different errors for "bad MAC" vs "bad format" | Many custom protocols |
Defences: AEADs, constant-time code paths, a single generic error, no compression of secrets alongside attacker-controlled data.
Side-channel attacks
The algorithm is sound, but its physical execution leaks secrets.
| Channel | Example | Defence |
|---|---|---|
| Timing | Early-exit comparisons reveal how many MAC bytes matched; RSA timing (Kocher, 1996); key-dependent branches | Constant-time code: no secret-dependent branches, memory indices or early exits |
| Cache | Table-based AES leaks key bits through cache access patterns to co-located processes | Hardware AES-NI, bitsliced implementations |
| Speculative execution | Spectre/Meltdown read memory across security boundaries | OS/browser mitigations, site isolation (Browser rendering) |
| Power / electromagnetic | Measuring a smart card's power draw reveals keys (DPA) | Masking, hardware countermeasures |
| Fault injection | Glitching voltage/clock to cause errors (RSA-CRT Bellcore attack, Rowhammer) | Verify results, error detection |
| Acoustic, thermal | Exotic, but demonstrated | Physical security |
# ✗ leaks timing
def check(tag, expected): return tag == expected
# ✓ constant time
import hmac
def check(tag, expected): return hmac.compare_digest(tag, expected)Protocol-level attacks
Man in the middle
Unauthenticated key exchange, ignored certificate errors, or disabled verification (verify=False, InsecureSkipVerify: true, trust-all TrustManager) let attackers intercept everything. Never disable certificate validation in production code — not even "temporarily" (PKI and certificates).
Downgrade attacks
Attackers tamper with negotiation to force weaker options both sides still support:
- FREAK and Logjam (2015): forced "export-grade" 512-bit RSA/DH, breakable in hours.
- POODLE: forced fallback to SSL 3.0.
- SSL stripping: rewriting
https://links tohttp://on first visit.
Defences: disable legacy versions and ciphers entirely; TLS 1.3 signs the full handshake transcript and has downgrade sentinels; HSTS (with preload) prevents SSL stripping.
Replay attacks
A captured valid message is resent later ("transfer ₹500" twice). Defences: nonces, timestamps, sequence numbers, and single-use tokens covered by the MAC or signature. TLS 1.3's 0-RTT early data is replayable — only use it for idempotent requests.
Signature and token validation bugs
- JWT
alg: noneaccepted, or RS256/HS256 algorithm confusion (the public key used as an HMAC secret). - Verifying one part of a message but using another (XML signature wrapping in SAML).
- Not checking expiry, audience or issuer (Authentication).
Randomness failures
- Debian OpenSSL bug (2006–2008): a code cleanup removed almost all entropy; only 32,767 possible keys per type existed, so SSH and TLS keys were trivially guessable.
- Embedded devices generating keys at first boot with no entropy → shared RSA primes across thousands of devices, recoverable with GCD.
- Using
Math.random(),rand()or timestamps for tokens, IDs or keys.
Always use the OS CSPRNG (secrets, crypto.getRandomValues, SecureRandom, /dev/urandom, getrandom()).
Implementation bugs
- Heartbleed (2014): a missing bounds check in OpenSSL's heartbeat extension let anyone read 64 KB chunks of server memory — including private keys and session cookies. Affected roughly 17% of HTTPS servers.
- Apple "goto fail" (2014): a duplicated
goto fail;line skipped signature verification in TLS. - Integer overflows, buffer overreads and parser bugs in certificate/ASN.1 handling.
Defences: memory-safe languages, well-maintained libraries, fuzzing, formal verification (HACL*, fiat-crypto in browsers), and prompt patching.
Key management failures
In practice, the most common way encrypted data is exposed:
- Keys or secrets committed to Git, baked into container images or mobile apps, or pasted in tickets.
- Keys stored next to the data they protect.
- No rotation or revocation plan; one key for everything, forever.
- Over-broad access to KMS keys; no audit logs.
Defences: secrets managers and KMS/HSM, envelope encryption, least privilege, rotation, secret scanning in CI, and separation of duties.
An engineer's checklist
- Use high-level, vetted libraries and standard protocols (TLS 1.3, libsodium/Tink, Web Crypto); never invent crypto.
- AEAD for encryption (AES-GCM or ChaCha20-Poly1305) with unique nonces; Ed25519 or ECDSA with deterministic nonces for signatures.
- Argon2id/bcrypt for passwords; HMAC-SHA256 for MACs; SHA-256+ for hashing; CSPRNG for all randomness.
- Compare secrets in constant time; return one generic error; never compress secrets with attacker input.
- Always validate certificates; disable legacy protocol versions and ciphers; enable HSTS.
- Prevent replay with nonces/timestamps; validate every token field (alg, exp, aud, iss).
- Protect, rotate and audit keys; keep secrets out of code; patch crypto libraries promptly.