Nonce Reuse & IV Misuse
Applied Crypto & Common Pitfalls · Networking & Cryptography track
5 min read
In this module
The Number You Must Never Repeat
A nonce (number used once) is a value that must stay unique across every encryption operation under a given key. IVs (initialisation vectors) play the same role. Reusing either one is one of the most common and most ruinous mistakes in applied cryptography.
The irony? Nonce reuse is conceptually trivial to avoid — just pick a different number every time. In practice, systems fail at this with alarming regularity, and the consequences are catastrophic in ways the developers usually don't see coming.
Why Nonces Exist
Without a nonce, encrypting the same plaintext with the same key always produces the same ciphertext. The ECB problem rises from the grave — patterns in the plaintext bleed through to the ciphertext. An attacker who spots two identical ciphertexts knows the underlying plaintexts were identical, even if they can't read either.
The nonce makes every encryption produce different output, even for identical plaintexts. It doesn't need to be secret (it usually rides along with the ciphertext), but it has to be unique for every message encrypted under the same key.
CTR Mode Nonce Reuse
In CTR mode, the keystream is generated by encrypting nonce || counter values:
Keystream = AES(key, nonce || 0) || AES(key, nonce || 1) || ...Ciphertext = Plaintext XOR KeystreamIf the nonce is reused, the same keystream is generated:
C1 = P1 XOR KeystreamC2 = P2 XOR Keystream (same keystream!)
C1 XOR C2 = P1 XOR P2 (keystream cancels out)Nonce reuse with CTR mode or any stream cipher causes the same keystream to be used twice. XORing the two ciphertexts eliminates the keystream, exposing the XOR of the plaintexts. With any known plaintext (common headers, structured data), both messages can be fully recovered. This is a complete break of confidentiality from a single repeated value.
The Plaintext XOR Problem
Once the attacker has P1 XOR P2, both plaintexts are usually recoverable:
- Known plaintext: If part of one message is predictable (HTTP headers, JSON braces, protocol framing), those known bytes light up the corresponding bytes of the other message. HTTP responses, for instance, almost always open with the same handful of headers.
- Crib dragging: Guess common words and phrases, XOR them against the combined stream at every offset, scan for plausible plaintext fragments. A guess at one position that yields readable English at the matching position in the other stream is probably correct, and the rest unravels from there. The same technique cracked Soviet traffic in the VENONA project, where one-time pad pages had been reused, and it still works on modern systems that repeat a keystream.
Even with no known plaintext, statistical analysis of English (or any structured data) can pry both messages apart from their XOR. Natural language carries plenty of redundancy to feed the recovery algorithm.
Known plaintext is the easiest of these to see. Below, a server encrypts two HTTP responses under one key and one nonce (with a toy keystream generator in place of AES-CTR). The first is the public home page, which the attacker can simply fetch. Predict how much of the second response, cookie included, they get back.
All of it, as far as the known response reaches. C1 XOR C2 XOR P1 is P2, byte for byte, and no key was involved.
Real-World Nonce Reuse Disasters
WEP (WiFi encryption): WEP used a 24-bit IV with RC4. Sixteen million possible IVs, thousands of packets per second — IVs repeated within minutes. Stacked on top of related-key weaknesses in RC4, this opened the door to complete key recovery. WEP's collapse is the textbook lesson on why nonce space must comfortably outpace expected message volume.
GCM nonce reuse: AES-GCM is catastrophically broken by nonce reuse. Plaintext recovery (as with CTR) is the start; the authentication key (used for GHASH) leaks too. The attacker walks away with the ability to forge authenticated messages without knowing the encryption key. One reused nonce breaks both confidentiality and authenticity in a single stroke.
Microsoft PPTP VPN: Used the same RC4 keystream in both directions. Client and server shared the key, so XORing client and server traffic together exposed both streams. A design error, not an implementation bug. The protocol itself was structurally broken.
IV Requirements for CBC
CBC mode requires a random, unpredictable IV for each message. The IV doesn't strictly need to be unique (though uniqueness is healthy) — it must be unpredictable to the attacker. Predictable IVs enable chosen-plaintext attacks like BEAST.
This differs subtly from CTR mode, where the nonce must be unique but doesn't need to be unpredictable. The distinction matters: a simple counter is fine for CTR (unique but predictable) and disastrous for CBC (where predictability is the attack vector). Getting these IV requirements wrong for your specific cipher mode is a perennial source of CVEs.
The requirements for the modes you're likely to meet, and what breaks when they're ignored:
Generating Nonces Safely
Several strategies exist:
Random nonces: Generate a random nonce for each message. With a 96-bit nonce (as in AES-GCM), the birthday paradox puts a 50% chance of a collision at around 2^48 messages, which is far too late to be the limit. NIST SP 800-38D caps random-nonce AES-GCM at 2^32 messages per key, keeping the collision risk below about one in four billion. Plenty of applications never get near that; high-volume systems need a key rotation strategy well before they do.
Counter-based nonces: Use a simple counter that ticks up per message. Uniqueness is guaranteed as long as the counter never wraps or resets. The catch is state — you have to remember the last nonce used, and that state has to survive crashes, restarts, and failovers without rolling back.
Nonce-misuse-resistant schemes: AES-GCM-SIV and other SIV (Synthetic IV) constructions derive the effective IV from the nonce and the message content together, using a keyed function. Nonce reuse under SIV degrades security to deterministic encryption (you can tell that two identical messages were sent) but doesn't catastrophically break confidentiality. A useful safety net when guaranteed nonce uniqueness is hard to engineer.
The Lesson
Nonce management looks trivial. Just pick a unique number. In practice, systems botch uniqueness through counter resets after crashes, RNG collisions in high-volume systems, clock rollbacks, distributed systems unable to coordinate state, and plain old bugs in nonce generation code. Failures are common enough that nonce-misuse-resistant constructions should be the default whenever the option exists. They catch you when — not if — the inevitable happens.