Section 5.18

Nonce

Definition

A nonce is a value that a cryptographic algorithm §14.1 or protocol may use only once per scope, usually once per key. A nonce-based encryption mode such as CCM §5.17 takes a fresh nonce with each encryption, so one key never encrypts two messages in the same way. A challenge–response protocol, which tests a party by asking for an answer computed from a value the verifier sends, uses a fresh nonce so an old answer is refused. A nonce need not be secret. The word is English: “for the nonce” means for the present occasion. It is also often read as “number used once”.

The idea

A device encrypts its messages with AES-CCM (the Advanced Encryption Standard in counter with cipher block chaining message authentication code mode) and uses a message counter as the nonce. The counter is kept in RAM. The device sends message 0 with nonce 0 and message 1 with nonce 1, then loses power. After the restart the counter is 0 again, and the next message is encrypted with the same key and nonce 0. Each step looked correct to the code that ran it. The fault is that the key survived the restart while the counter’s history did not.

AES-CCM, like every mode built on counter mode §15.5, turns the key and the nonce into a keystream, a sequence of secret pseudorandom bytes, and combines it with the message by bitwise XOR (exclusive or). The same key and nonce always give the same keystream. Two messages encrypted with one keystream leak the XOR of the two messages, and an attacker who knows one of them reads the other.

A useful picture is a book of single-use pads that the key selects, with the nonce as the page number. The page number may be written on the envelope, because it is useless without the book. What must never happen is the use of one page twice. The picture covers encryption only. A challenge nonce has a further job, explained below, for which the page number must also be unpredictable.

How it works

1. Fix the scope. The scope is everything that encrypts under the same key: every device that holds it, both directions of a connection, every run after a restart, and every restored copy of stored state. RFC 5116, which defines a common interface for authenticated encryption §5.16, states that devices sharing a key must coordinate so that their nonces differ.

2. Choose how nonces are made. The NIST glossary lists the usual sources: a random value generated anew for each use, a timestamp, a sequence number, or a combination [321].

3. Make the counter survive restarts. Keep the counter in non-volatile memory, which keeps its content without power, such as flash memory §6.6. Writing it after every message is slow and wears the memory, so store a value ahead of the one in use: before using any nonce equal to or above the stored value ss, write s+Bs + B, where BB is the size of a reserved block, and wait until the storage confirms the write. After a restart, continue from the stored value. RFC 5116 calls this checkpointing. NIST Special Publication (SP) 800-38D, which defines the GCM (Galois/Counter Mode) authenticated encryption mode, requires of GCM implementations validated against the FIPS 140 security requirements [338] that a loss of power never cause a nonce to repeat, and suggests storing a value one or more steps ahead of the one in use [319].

4. Stop before the counter runs out. Wrapping a full counter back to 0 repeats nonces, so the sender must stop or switch to a new key first, which is key rotation §5.29.

5. Defend against rollback. A rollback returns stored state to an earlier version: a restored backup, a restored virtual machine snapshot (a case RFC 5297 names), or an attacker writing an old copy back into flash. The stored counter goes back, and old nonces return. The remedies are a counter that cannot go back, such as a monotonic counter §8.28 in hardware, or a new key whenever the stored history cannot be trusted, for example a session key agreed after every restart from new random values supplied by both parties. If neither is possible, stop encrypting until a fresh key is established; SP 800-38D sets this rule for a nonce generator that cannot recover from a loss of power.

Challenge nonces. In a challenge–response protocol the verifier draws an unpredictable nonce, sends it, records it as pending, accepts at most one answer computed from it, and then deletes it. If the next challenge could be guessed, an attacker could obtain the answer in advance. A counter is a correct nonce for AES-CCM but a poor challenge; the NIST glossary notes that a nonce is not necessarily unpredictable.

Worked example

Reuse. Take the key and nonce of packet vector 1 of RFC 3610, which the AES-CCM entry works through. Their first four keystream bytes are 50 85 9D 91. Encrypt the message OPEN, whose ASCII bytes are 4F 50 45 4E, and then, after a restart that reset the counter, the message SHUT, whose bytes are 53 48 55 54, with the same key and nonce:

OPEN: 4F 50 45 4E XOR 50 85 9D 91 = 1F D5 D8 DF
SHUT: 53 48 55 54 XOR 50 85 9D 91 = 03 CD C8 C5

The attacker combines the ciphertexts: 1F D5 D8 DF XOR 03 CD C8 C5 = 1C 18 10 1A. The keystream has canceled out, and the result equals OPEN XOR SHUT. An attacker who knows that the first message was OPEN XORs it in and reads SHUT without the key.

Reserving ahead. A device keeps a stored value ss in flash memory and the next nonce uu in RAM, and reserves blocks of B=100B = 100 values. Every nonce below the stored value may already have been used. At start-up it reads s=200s = 200, writes s=300s = 300, waits for the write to be confirmed, and sets u=200u = 200. It sends 58 messages with the nonces 200 to 257 and then loses power. At the next start it reads s=300s = 300, writes s=400s = 400, and continues at u=300u = 300. The 42 values from 258 to 299 are never used, which costs capacity but not safety: 58+42=10058 + 42 = 100 values were reserved, and every new nonce is at least 300. The opposite order, using 200 before 300 is safely stored, opens a window: a power loss before the write completes leaves s=200s = 200 in flash, and the next start uses 200 again.

Running out. With the 4-byte counter field that RFC 5116 asks implementations to support, one device has 232=4,294,967,2962^{32} = 4{,}294{,}967{,}296 nonces per key. At 100 messages per second they last 4,294,967,296/100≈42,949,6734{,}294{,}967{,}296 / 100 \approx 42{,}949{,}673 seconds, about 497 days, and the device must switch keys before then. The values skipped at restarts barely matter: 10,000 restarts that each skip 99 values lose 990,000990{,}000 values, about 0.02% of the counter space.

Stated precisely

Let KK be a key and let E1,E2,…E_1, E_2, \ldots be all the encryptions ever performed under KK, by any device and in any run. Write N(Ei)N(E_i) for the nonce of EiE_i. The uniqueness requirement is N(Ei)≠N(Ej)for all i≠j.N(E_i) \ne N(E_j) \quad \text{for all } i \ne j. RFC 5116 states it as: each nonce provided to distinct invocations of the authenticated encryption operation must be distinct, for any particular value of the key. The NIST glossary gives the general definition: a time-varying value that has at most a negligible chance of repeating.

The reserve-ahead scheme keeps one invariant: every nonce used so far is smaller than the stored value ss. The invariant, and with it uniqueness across restarts, holds if four assumptions hold.

  1. Durable writes. Once storage confirms a write, the value survives a loss of power.

  2. No rollback. Storage never returns a value older than the last confirmed one.

  3. One user per counter. No other encrypter draws nonces from the same counter under the same key and the same fixed field.

  4. No wrap. The sender stops before the counter field overflows.

Then every nonce used after a restart is at least the stored ss, and every nonce used before it is below ss, so the two sets do not overlap.

For random nonces of bb bits, drawn independently and uniformly, the probability that any two of qq nonces are equal is at most q(q−1)/2b+1q(q-1)/2^{b+1}: there are q(q−1)/2q(q-1)/2 pairs, and each pair is equal with probability 2−b2^{-b}. For b=96b = 96 and q=232q = 2^{32} messages the bound is about 2−332^{-33}, roughly 1.2×10−101.2 \times 10^{-10}. SP 800-38D requires that the probability of a repeated nonce under one GCM key be no greater than 2−322^{-32}, and limits a GCM key whose nonces come from a random generator to 2322^{32} encryptions.

Limits and common mistakes

A nonce is neither a secret nor a key. It usually travels in the clear next to the ciphertext.

Unique is not unpredictable. A counter meets the needs of AES-CCM but makes a poor challenge, because the next value is known.

A unique nonce does not stop a replay attack §1.26, in which an attacker records a valid message and sends it again. The copy carries its original nonce and decrypts correctly, so the receiver must remember which nonces or counter values it has accepted.

The damage from a repeated nonce depends on the algorithm. For AES-CCM, RFC 5116 states that the XOR of the two messages is revealed; for GCM, that a repeat also undermines all the authenticity protection provided by the key. SIV §19.1.11 (synthetic initialization vector) mode, RFC 5297, is designed to tolerate repeats: a repeated nonce keeps authenticity and reveals only whether the same message and associated data were encrypted twice.

A new key resets the scope only if it really is new. A key derived again from the same secret and inputs after a restart is the same key, so its counter must continue. Deriving a key per restart from a stored restart counter only moves the rollback problem to that counter.

A random nonce is only as good as its generator. A generator that starts from the same seed after every restart repeats nonces exactly as a reset counter does. Short random nonces also run out fast: for 32-bit random nonces the bound q(q−1)/233q(q-1)/2^{33} reaches about one half at q=216=65,536q = 2^{16} = 65{,}536 messages. That half is an upper bound. For independent, uniform 32-bit draws the probability of at least one collision at 65,536 draws is about 1−exp(−0.5)=0.3931-\exp(-0.5)=0.393, or 39%.

Questions answered

How is uniqueness preserved across reboot and counter rollback?

Uniqueness across a reboot is preserved by keeping the nonce counter in non-volatile memory and storing a value ahead of the nonces before they are used, so that after a restart the sender continues above every nonce it may already have used. A nonce is a value that may be used only once per key; reusing one with an encryption mode built on counter mode lets an attacker who knows one of the two messages read the other. The procedure:

  1. Before using nonces from the stored value ss upward, durably write s+Bs + B for a block size BB, and wait for the storage to confirm. After a restart, continue from the stored value. Values reserved but not used are skipped, never reused.

  2. Never write the counter back to a smaller value, and never restart it at zero while the same key is in use.

  3. Stop, or switch to a new key, before the counter field overflows.

  4. If several devices share the key, give each a distinct fixed field in its nonces, so that their counters cannot produce equal nonces.

Counter rollback, the return of stored state to an older copy through a restored backup, a restored virtual machine snapshot or an attacker rewriting storage, defeats any scheme that trusts the stored value. Uniqueness then needs one of three things: a counter that cannot go back, such as a monotonic counter in hardware; a new key whenever the stored history cannot be trusted, for example a session key agreed after each restart from fresh random values supplied by both parties, so that old nonce values belong to an old key; or, failing both, refusing to encrypt until a fresh key is established, the rule NIST Special Publication 800-38D sets for a nonce generator that cannot recover from a loss of power.

Related entries: Authenticated encryption (AEAD) (§5.16), Birthday bound (§5.6), Replay attack (§1.26), CSPRNG (§5.27), AES-CCM (§5.17), Monotonic counter (§8.28), Synthetic initialization vector (SIV) and misuse resistance (§19.1.11), Nonce collision probability versus replay freshness (§19.1.13), Key rotation (§5.29). Further reading: [67, 74, 153, 319, 321, 338].