Speed

Honesty Boundary

An authenticated cipher has no memory: the same sealed value opens every time it is presented, so recognising a replayed message is the receiver's job, done with a sequence number it keeps. This page also lists what it does not show.

The question

An authenticated cipher refuses a changed message. Does it also refuse a message it has already accepted, sent again by someone who recorded it? Opening a sealed value recomputes a tag from the key, the nonce, the associated data and the ciphertext, and compares it with the received tag. Nothing in that computation records that the value was opened before.

So a copy of a genuine sealed value is genuine. This lesson opens one real AES-128-GCM sealed value several times, and then simulates the rule a receiver can add: accept a message only when its sequence number is new. The key is test material.

Work it by hand

Seal the message pay 10 to bob under a key, a nonce and the associated data seq 1. Open it twice with the same inputs. Each opening recomputes the same tag from the same four inputs and finds it equal to the received tag, so each opening returns the same message. The cipher has no place to remember the first opening.

A receiver can keep that memory itself. RFC 9846 section 5.3 describes it for TLS 1.3: a 64-bit sequence number is kept separately for reading and for writing records, and it is incremented by one after each record, so a copy of an old record arrives when the receiver expects a later number. The exercise below carries the number in each message to show the rule; it is a model, not what TLS puts on the wire.

Run it

The program seals the message once with aes-128-gcm and the associated data seq 1 (line 1) and then calls ssl.open on the same sealed value times times, at most 64 (lines 2 and 4 to 7), counting the openings that return the message (line 6). Line 8 returns the sealed value and line 9 the count.

The 29-byte sealed value, the number of openings asked, 3, and the number of successful opens, 3.

Public test material, generated for this lesson. Never use it for anything real.

let sealed = ssl.seal("aes-128-gcm", key, nonce, "seq 1", message);
let n = math_min(times, 64);
let ok = 0;
for (i in range(0, n)) {
  let back = ssl.open("aes-128-gcm", key, nonce, "seq 1", sealed);
  if (back == message) { ok = ok + 1; }
}
lmcode_return_put("sealed", sealed);
let answer = ok;
{
  "answer": 3,
  "sealed": "15b84e644a4f2f83e1a3d2b58851bcfa9b0aee6f7f4032037d0ee1cc0d"
}

You can change the message, the key, the nonce and times. A times of 0 or below makes no opening and returns 0, and one above 64 is cut to 64. The key must be 16 bytes, otherwise line 1 stops with "Ssl.seal: key must be 16 bytes". An empty nonce stops line 1 with a message that starts "Ssl.seal: bad IV length"; the tool accepts other lengths, but RFC 5116 section 5.1 specifies 12 bytes for AES-GCM, so keep 12.

Your turn

This is a simulation of a receiver, not an AEAD call. arrivals is a text of sequence numbers separated by single spaces, in the order the messages arrive. The program below is meant to return how many messages the receiver accepts when it accepts a message only if its number is greater than every number it has accepted so far. Decide whether it does, and find the candidates that do. Check runs each candidate on the data shown and on eight others.

The arrivals 5 3 4 8 7 9 and the number of accepted messages that the shown program returns.
let parts = str_split(arrivals, " ");
let last = 0 - 1;
let answer = 0;
for (i in range(0, parts.len())) {
  let n = str_to_int(parts.at(i));
  if (n > last) { answer = answer + 1; }
  last = n;
}
{
  "answer": 4
}

arrivals is a text of sequence numbers, non-negative whole numbers separated by single spaces, in arrival order. A receiver accepts a message only if its number is greater than every number it has accepted so far (the first message is accepted). Return how many messages it accepts. Check runs each candidate on the data shown and on eight others.

Read the result carefully

The exercise is a model of one rule, written in a few lines; real protocols use other rules, for example windows of numbers, which this page does not show. The program reads each part of the text as a number: a part that is not a number counts as 0, and the empty text counts as one message numbered 0 (measured).

The page does not show key management: a key in a program is test material, and holding a key in a program is not a way to look after one. It does not show what happens when a nonce is used twice under one key, which is named and shown in a-reused-nonce-leaks-the-xor, and it does not show rekeying, tag shortening or side channels.

Recap

  • Opening a sealed value checks the key, nonce, associated data and ciphertext; it keeps no record of earlier openings.
  • A copy of a genuine sealed value opens as often as it is presented.
  • Replay protection is the receiver's job: a sequence number kept by the receiver (RFC 9846, section 5.3).
  • The exercise simulates one rule; it is not an AEAD call and not a complete defence.