Nonce From a Counter
A nonce must be distinct for every message under one key; a counter gives distinctness. The TLS 1.3 record nonce pads the 64-bit sequence number with zeros on the left and XORs it with a fixed IV, so every sequence number gives another nonce.
The question
An authenticated cipher needs a nonce for every message, and RFC 5116 section 3.1 says that for one key every nonce must be distinct. What is a simple way to make sure of that? A counter: count the messages, and let the count decide the nonce.
RFC 5116 section 3.2 recommends a nonce with a fixed part and a counter part. TLS 1.3 uses a variant in its record layer, described in RFC 9846 section 5.3: the sequence number of the record, padded on the left with zeros to the length of the IV, is XORed with the write IV. This lesson computes such nonces and seals with them. It does not teach TLS; the records themselves are in tls-records-carry-a-length.
Work it by hand
Take the IV 000102030405060708090a0b, 12 bytes, and the sequence number 7. As a 64-bit number in network byte order it is 00 00 00 00 00 00 00 07; padded on the left with zeros to 12 bytes it is eleven zero bytes and 07. XOR byte by byte: 0b with 07 is 0c, and the other bytes meet a zero. The nonce is 000102030405060708090a0c.
The next sequence number, 8, gives 0b with 08, which is 03. The last byte goes from 0c to 03, and every sequence number gives another nonce because XOR with a fixed value is a one-to-one change of the padded number.
Run it
The function of lines 1 to 8 builds the nonce for an IV and a sequence number: it pads the number with u32_hex (line 2) and XORs the 12 bytes one by one (lines 4 to 6).
The loop of lines 10 to 15 makes the nonces for seq, seq + 1 and seq + 2, seals the text ping under each with aes-128-gcm and the associated data hdr (line 12), and writes a row with the start of the tag (lines 13 and 14). Line 16 returns the table and line 17 returns its first row.

Public test material, generated for this lesson. Never use it for anything real.
fn nonce_for(iv: string, n: int) -> string {
let pad = "0000000000000000" + u32_hex(n);
let r = "";
for (i in range(0, 12)) {
r = r + hex_byte(xor_byte(byte_at(iv, i), byte_at(pad, i)));
}
return r;
}
let rows = str_list();
for (k in range(0, 3)) {
let nonce = nonce_for(iv, seq + k);
let sealed = ssl.seal("aes-128-gcm", key, nonce, "hdr", "ping");
let tag = str_substr(sealed, str_len(sealed) - 32, 12);
rows.add(str(seq + k) + "|" + nonce + "|" + tag);
}
lmcode_return_put("rows", rows);
let answer = rows.at(0);
{
"answer": "7|000102030405060708090a0c|b21d021b5eba",
"rows": [
"7|000102030405060708090a0c|b21d021b5eba",
"8|000102030405060708090a03|20d75ae4030a",
"9|000102030405060708090a02|9bfec75d8d8b"
]
}You can change iv, seq and key. The key must be 16 bytes, otherwise line 12 stops with "Ssl.seal: key must be 16 bytes". The helper keeps 32 bits of the number: with seq 4294967296 the nonce is that of 0, and measured with -1 it is that of 0 as well, so the exercise stays below 2^32. A shorter iv is read as if it were padded with zero bytes.
Your turn
The program below is meant to return the 12-byte nonce, as 24 hex digits, that RFC 9846 section 5.3 forms from write_iv (12 bytes, hex) and the record sequence number record, a number from 0 to 4294967295: the number as 64 bits in network byte order, padded on the left with zeros to 12 bytes, XORed with write_iv. Decide whether it does, and find the candidates that do. Check runs each candidate on the data shown and on six others.

Public test material, generated for this lesson. Never use it for anything real.
let low = record % 256;
let last = xor_byte(hex_u8(write_iv, 11), low);
let answer = hex_slice(write_iv, 0, 11) + hex_byte(last);
{
"answer": "5f0e3a77c2d19b04e8a63149"
}write_iv is a 12-byte IV (hex) and record a record sequence number from 0 to 4294967295. Return the per-record nonce of RFC 9846 section 5.3 as 24 hex digits: the sequence number as a 64-bit number in network byte order, padded on the left with zeros to 12 bytes, XORed with write_iv. Check runs each candidate on the data shown and on six others.
Read the result carefully
The nonce is distinct only while the sequence number does not repeat under the same key and IV; RFC 9846 section 5.3 says the 64-bit number should not wrap and that an implementation that would need to wrap it MUST rekey or terminate the connection. The program keeps 32 bits of it, which is enough for this page and not for a real sequence number.
The tool is more permissive than the recommendation. RFC 5116 section 3.2 says implementations SHOULD support 12-octet nonces and section 5.1 gives 12 octets for AES-GCM, and SP 800-38D section 5.2.1 recommends 96-bit IVs for GCM; yet ssl.seal with aes-256-gcm accepted a 2-byte nonce in a measured run, while chacha20-poly1305 refused it with "bad IV length 2". The keys here are test material.
Recap
- A counter makes nonces distinct: each message gets the next value (RFC 5116, sections 3.1 and 3.2).
- TLS 1.3 pads the 64-bit sequence number with zeros on the left to the IV length and XORs it with the write IV (RFC 9846, section 5.3).
- From sequence number 7 to 8 the last byte of the nonce goes from 0c to 03.
- Distinctness lasts only while the counter does not repeat or wrap under one key.