TExcelTAD
Home › Applied Cryptography: Ciphers, Hashes, Signatures, PKI & TLS › Nonce Reuse: How One Repeated Number Breaks GCM and CTR

Nonce Reuse: How One Repeated Number Breaks GCM and CTR

AES-GCM has one rule for its users: never encrypt twice with the same key and nonce. It sounds easy, and it is broken in practice again and again: a hard-coded nonce copied from a tutorial, a counter that resets when a container restarts, two servers that start their counters at the same value, a random generator that wasn't random. This lesson shows exactly what a repeated nonce gives away, on two messages the example encrypts itself, and then how to find and prevent the bug.

Why a repeat is fatal

GCM encrypts like CTR mode: the keystream is AES(K, nonce ‖ counter) for counter values 2, 3, 4, … (counter 1 is used for the tag). The keystream depends only on the key and the nonce, not on the message. Two messages under the same (key, nonce) are therefore XORed with the same keystream, which is the two-time pad of The One-Time Pad:

text
c1 = p1 XOR keystream
c2 = p2 XOR keystream          =>   c1 XOR c2 = p1 XOR p2

The break, on a toy

The example below contains the bug on purpose: a constant nonce. It encrypts two internal messages, then plays the attacker, who sees only the two ciphertexts and knows (or guesses) what the second one says, for example because meeting notices follow a template.

python · try it yourself
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
def xor(a, b): return bytes(x ^ y for x, y in zip(a, b))

key = bytes(range(16))                 # toy key for this lesson
nonce = b"\x00" * 11 + b"\x07"         # the bug: a constant nonce
gcm = AESGCM(key)
p1 = b"salary: alice 5200, bob 4800"
p2 = b"meeting: room 4, 10:30 today"
c1 = gcm.encrypt(nonce, p1, None)[:-16]   # drop the tags to look at the ciphertext bodies
c2 = gcm.encrypt(nonce, p2, None)[:-16]

x = xor(c1, c2)
print("c1 ^ c2 == p1 ^ p2:", x == xor(p1, p2))
# Known plaintext: the attacker knows (or guesses) p2, a routine meeting notice.
print("recovered p1:", xor(x, p2).decode())
# Partial guess: only the first 8 bytes "meeting:" are guessed
print("from a crib :", xor(x[:8], b"meeting:").decode())

# The fix: a fresh nonce per message (os.urandom(12), or a counter that never repeats under this key)
n1, n2 = (1).to_bytes(12, "big"), (2).to_bytes(12, "big")
d1 = gcm.encrypt(n1, p1, None)[:-16]; d2 = gcm.encrypt(n2, p2, None)[:-16]
print("fresh nonces: c1 ^ c2 == p1 ^ p2:", xor(d1, d2) == xor(p1, p2))
c1 ^ c2 == p1 ^ p2: True
recovered p1: salary: alice 5200, bob 4800
from a crib : salary: 
fresh nonces: c1 ^ c2 == p1 ^ p2: False

Knowing the meeting notice gives the salary message in full. Guessing just the word meeting: already reveals salary: , and an attacker extends such fragments word by word ("crib dragging"). With fresh nonces the keystreams differ and XOR-ing ciphertexts gives nothing useful.

The second loss: authenticity

For GCM the damage doesn't stop at confidentiality. The tag is computed with GHASH, a polynomial in a secret value H = AES(K, 0^128) evaluated at the ciphertext blocks, and masked with AES(K, nonce ‖ 1). Two messages with the same nonce share the mask, so XOR-ing their tags removes it and leaves an equation in H alone. Solving it (Antoine Joux called this the "forbidden attack" in 2006) yields a few candidates for H, and with H the attacker can compute valid tags for any ciphertext under that nonce: forged messages that the receiver accepts. A 2016 Internet-wide scan by Böck, Zauner, Devlin, Somorovsky and Jovanovic found TLS servers that repeated GCM nonces and were open to exactly this forgery.

This lesson shows the confidentiality loss as a runnable toy and leaves the forgery as a concept: the mechanism above is enough to know that a GCM nonce repeat must be treated as a key compromise.

Real ways nonces repeat

  • Constants: nonce = b"\x00" * 12, or a fixed value pasted from documentation.
  • Counters without durable state: a counter kept in memory starts at 0 after every restart or deployment.
  • Shared keys, independent counters: several processes, pods or devices with one key, each counting from 0.
  • Too many random nonces: random 96-bit nonces collide with probability about n^2 / 2^97 after n messages; that is why NIST limits a key to 2^32 random-nonce encryptions.
  • Broken randomness: a VM snapshot restored twice gives the same random state twice; an embedded device with no entropy at boot draws the same "random" nonce on every boot.
  • Derived nonces: a nonce computed from the message or from a timestamp with one-second resolution repeats for equal messages or for two messages in the same second.

How it is detected

  • Code review and static analysis: literal byte strings or zeros passed as nonce/IV; nonces built from time.time() or a hash of the plaintext; counters without persistence. Search terms: nonce =, iv =, AESGCM(, GCM(, .encrypt( with a constant first argument.
  • Data checks: nonces are stored or sent with the ciphertext, so duplicates per key can be counted directly in the database, the message queue or a packet capture of your own system.
  • Key-usage limits: dashboards for messages encrypted per key; alerts long before 2^32 for random nonces.
  • Protocol testing: tools such as the 2016 scanner repeat TLS handshakes against your own servers and compare explicit nonces across records.

How it is fixed and recovered

  • Fix the generator: os.urandom(12) per message with per-key limits and key rotation; or a counter that is persisted before use and includes a sender id in the nonce for multi-writer setups; or a 192-bit-nonce AEAD (XChaCha20-Poly1305) where random nonces are always safe.
  • Use a misuse-resistant mode where uniqueness can't be guaranteed: AES-GCM-SIV (RFC 8452) only reveals that two equal messages were equal.
  • Recover: if reuse happened, assume all messages under that key are exposed and forgeable. Rotate the key, re-encrypt stored data under the new key with correct nonces, and review what an attacker could have forged.

Pitfalls

  • Confusing "nonce" with "random": the requirement is uniqueness. A counter is a perfectly good nonce if it never repeats under the key.
  • Using the same nonce with two different keys and calling it reuse: that is fine. The pair (key, nonce) must be unique.
  • Forgetting that AESGCM.decrypt also needs the nonce: store or send it with every ciphertext.

Practice

1. Kubernetes runs 12 replicas of a service that encrypts events with one AES-GCM key and a nonce made from an in-memory counter starting at 1. What goes wrong, and what are two fixes?

All 12 replicas produce nonces 1, 2, 3, …, so the same nonce is used up to 12 times, and every restart repeats them again. Fixes: random 96-bit nonces from os.urandom (within the per-key limit), or a per-replica key derived with HKDF from a master key and the pod's unique id, each with its own persistent counter.

2. You find two ciphertexts with the same nonce in your own logs. The first 12 bytes of one plaintext are a fixed JSON prefix {"type":"pay. What can you read, and what must you do?

XOR the two ciphertexts and then XOR with the known prefix to read the first 12 bytes of the other message. Treat the key as compromised for confidentiality and authenticity: rotate it, re-encrypt, and find the nonce bug.

3. With random 96-bit nonces, roughly what is the chance of any repeat after 2^32 messages?

About (2^32)^2 / 2^97 = 2^-33, roughly one in 8.6 billion. That is the margin behind NIST's limit; at 2^48 messages n^2 / 2^97 is already 1/2, which means a repeat with probability about 40 % (1 - e^(-1/2)), and beyond that it quickly becomes certain.

Test yourself

Two AES-GCM messages share a key and a nonce. What does XOR-ing their ciphertexts give?

  1. The key
  2. The XOR of the two plaintexts
  3. Random noise
  4. The GHASH key directly

Besides confidentiality, what else does a GCM nonce repeat break?

  1. Nothing else
  2. Authenticity: the GHASH key can be recovered and tags forged
  3. Only the AAD
  4. The key schedule

Which nonce scheme is safe with one key on many servers?

  1. Each server counts from 0
  2. A nonce derived from the current second
  3. Random 96-bit nonces within the per-key message limit
  4. The constant 0

What property must a nonce have?

  1. Secret
  2. Unique per key
  3. Long as the message
  4. Derived from the plaintext