Section 1.26

Replay attack

Definition

A replay attack is an attack in which an attacker records a genuine message and sends it again later, unchanged, so that the receiver acts on it again or in a situation its sender never intended. The copy passes every authenticity check: a message authentication code §5.7 (MAC) or a digital signature §5.14 shows who produced the bytes and that they are unchanged, not when they were produced or whether they were already used. The defense is a freshness check: a test of the message against state that the receiver keeps.

The idea

A car lock opens when it receives a radio command to unlock that carries a valid MAC. On Monday an attacker standing nearby records the packet that the owner’s remote key sends. On Tuesday the attacker transmits the same bytes. The lock checks the MAC, the MAC is correct because the owner’s remote key computed it, and the door opens. The attacker forged nothing, decrypted nothing and never learned the secret key. Encrypting the packet would not help, because the attacker never needs to read the packet, only to repeat it.

Authenticity is a property of the bytes alone. Whether the bytes may be acted on now also depends on the receiver’s situation: what it has already done and what it is waiting for. A bank is a fair picture: the signature on a check shows who wrote it, but only the bank’s record of the check numbers already paid stops the same check being paid twice. The picture stops being accurate in one respect: a photocopied check can be told from the original, while a copied message is the same bytes, so the receiver’s memory, or a value the receiver chose itself, is the only defense.

How it works

A freshness check needs two parts: a freshness value inside the bytes that the MAC or signature covers, so that the attacker cannot change it, and state at the receiver against which that value is judged. Three designs are common.

  1. Counter, also called a sequence number. The sender puts a number cc in each message and increases it for every new message. The receiver keeps the counter of the last message it accepted, here called last\mathrm{last}, in storage that survives a restart. It accepts a message only if cc passes its acceptance rule, and then sets last\mathrm{last} to cc.

  2. Challenge and response. The receiver sends a fresh random nonce §5.18, a value used for one exchange only. It accepts only a reply that carries, inside the bytes its MAC covers, the nonce of the challenge the receiver is still waiting for, and it then forgets that nonce. An old reply carries an old nonce and fails. This costs an extra message but needs no stored counter.

  3. Timestamp. The sender includes its sending time, and the receiver accepts the message only if its own clock is within a fixed window, such as 30 seconds, of that time. This needs clocks that agree and that the attacker cannot set back; within the window a copy is still accepted unless the receiver remembers the messages it has seen.

The receiver must also fix the order of two steps: recording acceptance, and carrying out the action the message requests. If it acts first and loses power before it stores the new last\mathrm{last}, it restarts with the old value, accepts the replay, and the action happens twice. If it stores last\mathrm{last} first and loses power before acting, the action never happens, and the sender must send a new message with a new counter. For an action that must not happen twice, such as a payment, record first. The other way out is idempotency §8.31: an action designed so that doing it twice has the same result as doing it once.

Worked example

A receiver has accepted messages up to counter 18. Each case below starts from last=18\mathrm{last} = 18. The counter field is 32 bits wide, so its largest value is 232−1=4,294,967,2952^{32} - 1 = 4{,}294{,}967{,}295.

Now let the receiver keep last\mathrm{last} only in RAM. After a restart last\mathrm{last} comes back as 0, and the recorded message with c=18c = 18 passes 18>018 > 0. Every check ran correctly; the state that the checks rely on was lost.

Stated precisely

Let mm be the bytes that a message’s MAC or signature covers, let cc be the counter inside mm, and let last\mathrm{last} be the counter of the last message the receiver accepted under the same key, and from the same sender when several senders share the key. The receiver accepts mm only if the tag or signature verifies and c>lastc > \mathrm{last}, or c=last+1c = \mathrm{last} + 1 under the exactly-next rule. It stores cc as the new last\mathrm{last} before any effect of mm can be observed. Then each message is accepted at most once, and no message is accepted after a message with a larger counter, provided five assumptions hold.

  1. Only key holders can produce a valid tag or signature.

  2. The counter lies inside mm.

  3. Each honest sender never reuses a counter value under one key; several senders that share one key each need their own counter, and the receiver then keeps last\mathrm{last} per sender rather than per key, identifying the sender from an identity inside mm, so both conclusions hold for each sender separately.

  4. last\mathrm{last} never decreases: it survives power loss and restarts, and no one in the threat model §1.2 can restore an older stored copy.

  5. The counter never passes its largest value and starts again from 0, which is called counter wraparound §6.21; the key is changed before that happens.

The guarantee concerns one receiver. A copy sent to a different receiver, or into a different session, is judged against different state. It fails there only if the receiver’s identity or the session’s identifier is inside mm, or if each receiver has its own key.

Choosing the counter rule

Three acceptance rules look alike and behave differently.

Limits and common mistakes

A random nonce chosen by the sender stops replay only if the receiver remembers every nonce it has accepted under the key. A nonce that the receiver chose itself, in a challenge, needs no such list.

Restoring stored state reopens replay. A device whose storage is restored from a backup, or rolled back by an attacker to an older copy, has an older last\mathrm{last}. A monotonic counter §8.28, a hardware counter that can only increase, closes this.

Fresh does not mean permitted. A fresh message from a key holder can still request something that the receiver must refuse.

A reflection attack sends a party’s own message back to it. If both directions use one key, the message verifies. A separate key for each direction, or a direction field inside the authenticated bytes, stops it.

A sender that resends a message because an acknowledgment was lost creates an innocent duplicate. The receiver must recognize it by its counter and answer with the stored result instead of acting again.

Questions answered

Which state makes an old authenticated command unacceptable?

State that the receiver keeps, not anything in the command, makes an old authenticated command unacceptable: a record of what the receiver has already accepted, or of what it is waiting for. Three forms are common.

The counter, nonce or time must lie inside the bytes that the command’s message authentication code or signature covers, so that an attacker cannot change it. The receiver’s state must survive restarts, must be updated before the command’s effect becomes visible, and must not be restorable to an earlier value; otherwise the old command becomes acceptable again.

Related entries: Nonce (§5.18), Failure atomicity (§8.5), Downgrade attack (§1.27), Replay resistance requires state or a fresh challenge (§17.10), Monotonic counter (§8.28), Idempotency (§8.31), Byzantine failure (§1.31).