Speed

A Flipped Bit in a Record

After the handshake every record carries a tag that covers its header and its content. One changed bit in the body makes the receiver refuse the whole record and read nothing, and the pair cannot be used after it.

The question

After the handshake the two ends send each other protected records. What happens if one bit of such a record changes on its way, and how much of the message does the receiver get?

Both ends are test endpoints in this tab's memory, made with ssl.tls_pair. The handshake runs first; then the client sends a short text and the server reads it. You ask for one bit of the record to be flipped with pair.tamper and read what the server says. Nothing attacks anything. The certificates and the key are public test material.

Work it by hand

The client sends again, 5 bytes. A TLS 1.3 record has a header of 5 bytes. Its body holds the content, one byte for the inner content type, and a tag of 16 bytes for these suites (RFC 9846 section 5.2 describes the record): 5 + 1 + 16 = 22, which is hex 16, so the header reads 17 03 03 00 16 and the whole record has 5 + 22 = 27 bytes.

The standard says that the additional data that the tag covers is the record header (section 5.2), and the tag covers the encrypted body too. So a changed bit in the body makes the check fail, and the server should refuse the record and hand over no part of the 5 bytes.

Run it

Lines 1 to 5 build the pair and lines 6 to 9 run the handshake. If it finished, line 12 asks for bit 0 of byte offset of the next transfer to be flipped, and line 13 sends message and lets the server read. Line 15 returns the number of bytes of the record and the number of bytes the server read. Lines 16 to 18 return the first 5 bytes of the record, error() and whether the pair is still done.

Four rows: the message again, the record header 1703030016, the two numbers 27 bytes sent and 0 bytes read, and done after false.

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

let server = ssl.server_ctx(ssl.parse_cert(leaf_pem), ssl.parse_key(leaf_key));
server.chain(ssl.parse_cert(int_pem)); server.groups("X25519");
let client = ssl.client_ctx(); client.add_ca(ssl.parse_cert(root_pem));
client.groups("X25519");
let pair = ssl.tls_pair(client, server, "www.example.test");
let turns = 0;
while (!pair.done() && pair.error() == "" && turns < 10) {
  let flight = pair.step(); turns = turns + 1;
}
let record = ""; let got = "";
if (pair.done()) {
  pair.tamper(str_to_int(offset));
  record = pair.client_send(message); got = pair.server_recv();
}
let answer = int_list(); answer.add(str_len(record)); answer.add(str_len(got));
lmcode_return_put("header", str_substr(str_hex_encode(record), 0, 10));
lmcode_return_put("error", pair.error());
lmcode_return_put("done_after", str(pair.done()));
{
  "answer": [
    27,
    0
  ],
  "header": "1703030016",
  "error": "server read: error:0A000119:SSL routines::decryption failed or bad record mac; error:0A000139:SSL routines::record layer failure",
  "done_after": "false"
}

With again and offset 8 the answer is [27, 0]: 27 bytes went out and the server read none. The header is 1703030016, unchanged. What this OpenSSL build says: server read: error:0A000119:SSL routines::decryption failed or bad record mac; error:0A000139:SSL routines::record layer failure, and the pair is no longer done. These come from this run.

You can change message and offset. A longer message gives a longer record and a header with another length. An empty message sends no record: the answer is [0, 0]. offset is read as text, and text that is not a whole number is read as 0.

Offsets 5 to the last byte of the record give the result above. Offset 0 gives server read: error:0A0001BB:SSL routines::bad record type; ..., offsets 1 and 2 give ... wrong version number; ..., and offsets 3 and 4 change the length.

When the length grows (always at offset 3, at offset 4 when the length is even, as with again) there is no error, the server reads nothing and the pair stays done, because the server waits for more bytes; when it shrinks (offset 4 with a message of even length, such as abcd) the record is cut short and the tag check fails.

An offset of 27 or more stops line 13 with "Line 13: TlsPair.client_send: tamper offset 27 is beyond the 27 bytes sent", and a negative one stops line 12 with "Line 12: TlsPair.tamper: negative tamper offset".

Your turn

The client sends first and the server reads it. Then, only if error() is still empty, the client sends second and the server reads again. damaged says which send has bit 0 of byte 8 of its record flipped: 0 none, 1 the first, 2 the second. The program below should return the total number of bytes the server read. Decide whether it does, and find the candidates that do. Check runs each candidate on the data shown and on five others.

The first text abc, the second text defg and the damaged send 1 of the case shown.

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

let server = ssl.server_ctx(ssl.parse_cert(leaf_pem), ssl.parse_key(leaf_key));
server.chain(ssl.parse_cert(int_pem)); server.groups("X25519");
let client = ssl.client_ctx(); client.add_ca(ssl.parse_cert(root_pem));
client.groups("X25519");
let pair = ssl.tls_pair(client, server, "www.example.test");
let turns = 0;
while (!pair.done() && pair.error() == "" && turns < 10) {
  let flight = pair.step(); turns = turns + 1;
}
let answer = 0; let sent = 0;
if (pair.done()) {
  let one = pair.client_send(first);
  if (damaged == 1) { pair.tamper(8); }
  answer = str_len(pair.server_recv());
  if (pair.error() == "") {
    let two = pair.client_send(second);
    if (damaged == 2) { pair.tamper(8); }
    answer = answer + str_len(pair.server_recv());
  }
}
lmcode_return_put("turns", turns);
{
  "answer": 3,
  "turns": 4
}

A client asks for www.example.test and trusts root_pem; the server holds leaf_pem, int_pem and leaf_key; both ends use the group X25519. Both ends are test endpoints in this tab's memory. Run the handshake, taking turns while it is not done and has no error, at most ten times. If it is done, the client sends first and the server reads; then, only if error() is empty, the client sends second and the server reads again. Bit 0 of byte 8 of the record is flipped on the send that damaged names (1 the first, 2 the second, 0 neither). Return the total number of bytes that the server read. Check runs each candidate on the data shown and on five others.

Read the result carefully

TLS does not repair a damaged record. One refused record ends the session here, and this pair cannot be used afterwards: a send after an error stops the program. A real connection would be closed, which is stated here and not run. The tag that did the work is that of an authenticated cipher, explained in authenticated-encryption.

The error text is what this OpenSSL build says, and another build words it differently. The certificates are valid from 2026-01-01 to 2036-01-01, and the results hold only between those years. They are test material, and nothing here shows how to change a record on purpose.

For fixed handshake bytes, see the recorded handshake.

Recap

  • A protected record is a 5-byte header, the encrypted content, one byte of inner type and a 16-byte tag (RFC 9846 section 5.2).
  • The tag covers the header too; a flipped bit in the body makes the receiver refuse the whole record, while a flipped bit in the header is met first by the parser (a wrong type or version is refused, a longer length is waited for, a shorter one cuts the record and fails the tag check).
  • The receiver reads no part of the text of a refused record.
  • After the error the session is over for good.