Speed

Application Data After the Handshake

Once the handshake is done, client_send protects a text in one record and server_recv returns the plaintext. The record is 22 bytes longer than the text: a 5-byte header, one byte for the real content type and 16 for the authentication tag. Send texts between two test endpoints in this tab's memory.

The question

When done() is true the two ends hold their traffic keys, and the program can send application data. pair.client_send(text) protects a text with the client's key and returns the bytes of the record; pair.server_recv() returns what the server reads. Both ends are test endpoints in this tab's memory, and nothing leaves it.

How long is the record for a text of a given length, and what part of it can the page show without printing a byte that is different on every run?

Work it by hand

RFC 9846 section 5.2 describes a protected record. Its payload is the content, then one byte for the real content type, then optional padding, encrypted with an AEAD algorithm. The length field counts the content, the padding, one byte for the inner type and "any expansion added by the AEAD algorithm", which the section says varies with the algorithm. The 5-byte header comes in front: outer type 23, version 0303, length.

For a text of 5 bytes the run below hands over 27 bytes: 5 of header, 5 of text, 1 for the inner type and 16 of expansion, the authentication tag of the suite that this build chose. The length field of the header says 22, the 5 plus 1 plus 16 behind the header.

Run it

The program steps the pair to the end of the handshake in the loop of lines 7 to 9. If it is done, line 11 makes the record from message, and line 12 reads the plaintext back. The program returns what the server read as its answer, the length of the record, its first five bytes (the header, the only part of a record that the page shows) and the difference between the record and the text.

The 5-byte header of the record that carries the text: type 23, version 0303 and length 22 for a text of 5 bytes.
Watch the steps (recorded video)

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()) { record = pair.client_send(message); }
if (pair.done()) { got = pair.server_recv(); }
lmcode_return_put("record_bytes", str_len(record));
lmcode_return_put("header", str_substr(str_hex_encode(record), 0, 10));
lmcode_return_put("overhead", str_len(record) - str_len(message));
let answer = got;
{
  "answer": "hello",
  "record_bytes": 27,
  "header": "1703030016",
  "overhead": 22
}

The record is 22 bytes longer than the text with the suite of this build. In this build an empty text makes no record at all: 0 bytes, and the server reads nothing. You can change message: printable ASCII and line breaks, up to 2048 bytes, with a record 22 bytes longer for every non-empty text. The four PEM texts are test material: leave them as they are.

Your turn

The program below is meant to return the total number of bytes that the client hands to the server for two texts, first and second. Either text can be empty, and in this build an empty text makes no record. Decide whether it does, and find the candidates that do. Check runs each candidate on the data shown and on four others.

The first text GET /, the second text pong, and the number that the shown program returns.

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;
if (pair.done()) {
  let one = pair.client_send(first + second);
  answer = str_len(one);
}
lmcode_return_put("texts", str_len(first) + str_len(second));
{
  "answer": 31,
  "texts": 9
}

The program builds a TLS client and a TLS server from the test chain for the name www.example.test, joins them in memory and steps them until the handshake is done. Then the client writes first and then second with client_send, one call each, and each call returns the bytes of the record that it made (nothing, for an empty text). Return, as a whole number, the total number of bytes that the client handed to the server for the two texts. Check runs your program with other texts.

Read the result carefully

The length of a record tells an observer the length of the text, because no padding is used here, even though the body of the record is encrypted. The page shows only the header: the body is different on every run. The test chain is valid from 2026-01-01 to 2036-01-01 and the client checks the dates against the reader's clock; outside those years the handshake fails, nothing is sent, and the demo returns an empty text and the programs of Your turn return 0.

The 16 bytes of the tag are what this build's suite adds; another suite can add another amount, and the RFC says only that the expansion varies. For the tag itself see Authenticated Encryption. The certificates are test material.

For fixed bytes you can inspect, see the recorded handshake.

Recap

  • After done() a text becomes one record, and server_recv() returns the plaintext.
  • In this build the record is the text plus 22 bytes.
  • In this build an empty text makes no record (RFC 9846 section 5.1 allows empty fragments but does not require them).
  • The header shows type 23 and the length; the body is never shown.