Speed

A Record Opens With Its Nonce

A protected record opens with the key, a nonce made of the IV XORed with the record sequence number, and its header as additional data; record 4 of the recording opens to an EncryptedExtensions message.

The question

Every protected record is encrypted by AES-GCM under a key, a nonce and the record header as additional data. Where does the nonce come from, when nothing in the record states it?

This page opens the fourth record of the recorded loopback connection (a connection between two test programs, made on 2026-11-20 and kept as test material): the server's first protected record. Its key and IV come from the method of the previous lesson. The nonce is built from the IV and the record's sequence number, and the additional data is the 5-byte record header (RFC 9846, 5.2 and 5.3).

Work it by hand

The sequence number is 64 bits, written as 8 zero bytes and then the number in 4 bytes here, for 12 bytes in all, the length of the IV. The nonce is the IV XORed with that: for sequence number 0 the nonce is the IV itself. For the server's handshake IV, 22861d52bad707381730aad7, sequence number 5 would change only the last byte: d7 XOR 05 is d2.

The plaintext of a protected record ends with one byte that gives the real content type, and the rest is the message. The first record sent under a particular traffic key uses sequence number 0 (RFC 9846, 5.3), and the number counts up by one per record; it starts again at 0 under each new key. The server's handshake records use 0 to 3 and its application records start again at 0.

Run it

The program takes the key, the IV, the sequence number, the 5-byte header and the sealed body (ciphertext followed by the 16-byte tag) of record 4 of the recording. Lines 1 to 6 build the nonce byte by byte; line 7 opens the record with ssl.open; line 8 hex-encodes the plaintext. The message is everything but its last byte, which is returned as inner_type; line 12 returns the message.

The 12-byte nonce, which equals the IV 22 86 1d 52, the first 16 bytes of the decrypted message starting 08 00 00 1e, and the inner type 22.

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

let seq_hex = str_repeat("00", 8) + u32_hex(seq);
let nonce = "";
for (i in range(0, 12)) {
  let byte = xor_byte(hex_u8(iv, i), hex_u8(seq_hex, i));
  nonce = nonce + hex_byte(byte);
}
let plain = ssl.open("aes-128-gcm", key, nonce, ssl.unhex(header), body);
let all = str_hex_encode(plain);
let last = hex_len(all) - 1;
lmcode_return_put("nonce", nonce);
lmcode_return_put("inner_type", hex_u8(all, last));
let answer = hex_slice(all, 0, last);
{
  "answer": "0800001e001c000a0018001611ec11eb11ee001d0017001e00180019002901000101",
  "nonce": "22861d52bad707381730aad7",
  "inner_type": 22
}

The result begins 0800001e: the handshake message type 8 (EncryptedExtensions) and a length of 30, and the type byte at the end, returned as inner_type, is 22 (0x16), a handshake. You can change the inputs only together with a consistent record: another sequence number, another header or a body of which one byte changed stops the run with "Ssl.open: authentication failed (wrong key, IV, AAD or tampered data)" (shown by the page; the sequence number 1 gives it).

Your turn

A reader wants the nonce and the additional data of a record, without opening it. The program below is meant to return a list of two hex texts for an IV, a sequence number (below 2 to the 32) and a record header: the 12-byte nonce, then the additional data. Decide whether it does, and find the candidates that do. Check runs each candidate on the data shown and on six others. The programs here never open a record; the data are test material.

A 12-byte IV, the sequence number 1 and the 5-byte header of a record.

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

let seq_hex = str_repeat("00", 8) + u32_hex(seq);
let nonce = "";
for (i in range(0, 12)) {
  let byte = xor_byte(hex_u8(iv, i), hex_u8(seq_hex, i));
  nonce = nonce + hex_byte(byte);
}
let answer = str_list();
answer.add(nonce);
answer.add(hex_slice(header, 1, 4));
{
  "answer": [
    "107586be4c017629ddcf99ba",
    "03030013"
  ]
}

Return a list of two hex texts: the 12-byte AEAD nonce for the IV, the sequence number seq (below 2 to the power 32) and the header, then the additional data for that record.

Read the result carefully

The nonce is never sent: both sides count the records. A wrong count makes the open fail, and a program that opens records cannot be run on a wrong nonce without ending the run, which is why the exercise does not open anything. The page opens one record of one recording with keys that protect nothing; it does not show a real connection, key updates, or what a peer does after a failure.

The sequence number here is limited to 32 bits because the helper writes 4 bytes; a real 64-bit number is not tested.

Recap

  • The nonce is the 64-bit record sequence number, padded on the left with zeros to the IV length, XORed with the IV.
  • The additional data of the record comes from its header.
  • The plaintext ends with the real content type byte.
  • The sequence number restarts at 0 with every new traffic key.