Skip to reference

std.x.tls.record

wolf-std d58f9e1 · toolchain pin verified against wolf 0.2.41 (0261597). Source signatures and documentation; execution support is stated in each module.

All modules · Plain text · Stability tier: x

Module

std.x.tls.record — the TLS 1.3 record layer and its key-schedule primitives (RFC 8446 §5 + §7.1/§7.3). Stability tier: x — the FIRST rung of the stdc-tls campaign (sprint sc20), the protocol the whole D53 crypto ladder was built for. This module is the RECORD LAYER and the KEY SCHEDULE PRIMITIVES, nothing above them: no ClientHello, no certificate, no state machine — the handshake (sc21) produces the secrets, this rung derives keys from them and frames records under them. It is testable in isolation against RFC 8448's published traces, exactly the sc16-before-everything discipline.

The nursery banner (D31). std.x.tls.record is not std.tls: its path, names and behaviour may change or vanish in one sprint; no other std module may depend on it. Its reason and graduation trigger live in std/x/README.md. It depends on TWO sibling residents — std.x.crypto.sha2 (HKDF-SHA-256, the key schedule) and std.x.crypto.chacha20 (AEAD_CHACHA20_POLY1305, the record AEAD) — plus no facade at all. Those two resident-on-resident edges are ones the graduation move to std.tls/std.crypto must resolve, named in the register beside jose's own sha2/curve25519 edges (the sc19 precedent — a resident MAY depend on a sibling resident, the edge is recorded, not forbidden).

Built from the RFC DOCUMENTS, not ported. RFC 8446 (August 2018) §7.1 (HKDF-Expand-Label, Derive-Secret), §7.3 (traffic key calculation), §5.2 (TLSCiphertext framing), §5.3 (the per-record nonce) and §5.4 (record padding). RFC 8448 (January 2019) §3 is the vector spine: the trace publishes every intermediate secret, traffic key, IV and the info (HkdfLabel) bytes themselves — asserted byte-for-byte in tests/x/tls/record/.

## The four byte-exact rules this module lives or dies on

1. HkdfLabel is byte-exact (§7.1). The label prefix is the six bytes "tls13 " — INCLUDING the trailing space (74 6c 73 31 33 20), NOT "TLS 1.3, ". The structure is uint16 length, then a one-byte-prefixed opaque label = "tls13 " + Label, then a one-byte-prefixed opaque context, all big-endian. One wrong length byte and every downstream key is wrong; hkdf_label's output is asserted against RFC 8448's published info dumps.

2. The nonce is IV XOR seq, right-aligned (§5.3) — NOT IV || seq and NOT seq || IV. The 64-bit sequence number is encoded big-endian and left-padded with zeros to the IV length (12), then XORed with the static write IV. record_nonce builds exactly that field; a wrong nonce fails every AEAD in RFC 8448's trace.

3. The AEAD additional-data is the record HEADER (§5.2), not empty: opaque_type(23) || legacy_record_version(0x0303) || length, the five bytes on the wire ahead of the ciphertext. additional_data builds it and seal/open pass it; sealing without it, or with the wrong length, fails the trace.

4. Padding removal scans from the END for the content-type (§5.4): the deprotected inner plaintext is content || content_type || zero-padding, and the real content-type is the LAST non-zero octet. An all-zero inner has no content-type and is a malformed record (§5.4's unexpected_message), raised as the unexpected row.

The AEAD is ChaCha20-Poly1305, the MTI (RFC 8446 §9.1). RFC 8448's own records are AES-128-GCM (a rung with its own D-question, out of this sprint), so the key-schedule/nonce/AAD framing is asserted against RFC 8448 byte-for-byte and the record AEAD seal/open byte-match uses ChaCha20-Poly1305 fixtures derived from RFC 8448's real traffic secrets by an independent reference (the sc17 discipline; F-0094 records the AES-GCM/ChaCha reconciliation).

**Byte sequences are List[byte] (sc35)**, the sha2/chacha20 convention: the 0..255 element contract (§11's interim, F-0035) is the TYPE's now, so this module's require_byte — its recorded ct exception, a comparison pair on every ingested element — is deleted rather than left as decoration.

Error rows. open raises {tag, short, unexpected}: a forged, modified or truncated box is the DATA's failure (tag, sc17's verify-before-expose — zero plaintext leaves on this path); a record too short to hold a header, a tag and one content-type octet is short; a deprotected inner with no non-zero octet is unexpected (§5.4). Everything else is total over its documented domain; length misuse is a caller-contract violation that traps assert.

The sc21-consumable seam (FROZEN). The handshake sprint gets: (a) the key schedule — extract, expand_label, derive_secret, traffic_key, traffic_iv, finished_key — to turn its computed secrets into AEAD keys; and (b) the framing — seal/open over (key, iv, seq, ...) — to protect and deprotect records. sc21 produces the secrets (transcript hashes over real handshake messages); this module never computes a transcript, it takes the context bytes as an argument. The seam is: GIVEN keys -> sealed/ opened records. It is frozen at sc20; std/x/README.md records it.

Constant-time obligations (D53's retrofit clause). c28's compiler-enforced #[consttime] does not exist yet; until it does, every pub fn carries a ct: line. The honest summary: the key schedule handles SECRETS (traffic secrets, keys, IVs) and branches/ indexes only on PUBLIC quantities (label bytes, lengths, the public sequence number) — the secret flows through sha2's HKDF and chacha20's AEAD, whose own ct: lines carry the wrapping-not-checked posture. record_nonce does wrapping-not-checked arithmetic on the write IV (a secret) with the public sequence number and is branch-free. open's padding scan reads the DEPROTECTED length (§5.4) — a value-dependent loop bound c28 must model; it is marked below. The 0..255 ingestion guards were the other recorded value-dependent branch, shared with sha2/chacha20, and F-0035's real byte type RETIRED them at sc35 — exactly as this note said it would.

Types and traits

No public declarations in this section.

Functions and methods

hkdf_label

fn · Source line 164

pub fn hkdf_label(length: int, label: str, context: List[byte]) -> List[byte]

The HkdfLabel structure encoded to bytes (§7.1): uint16 length big-endian, then the one-byte-length-prefixed "tls13 " + label, then the one-byte-length-prefixed context. This is the exact info fed to HKDF-Expand — RFC 8448 §3 publishes it as the info dumps, asserted byte-for-byte. ct: public structure only (label and context are public; the length prefixes are public) — no secret touched here.

expand_label

fn · Source line 196

pub fn expand_label( secret: List[byte], label: str, context: List[byte], length: int, ) -> List[byte]

HKDF-Expand-Label (§7.1): expand secret under the structured HkdfLabel to length bytes. The digest is SHA-256 (the TLS_*_SHA256 suites); std.x.crypto.sha2.hkdf256_expand is the HKDF-Expand. ct: secret is the PRK, carried through sha2's HKDF (its ct: line holds the wrapping-not-checked posture); the label/context/length that select HkdfLabel are public.

derive_secret

fn · Source line 214

pub fn derive_secret(secret: List[byte], label: str, transcript: List[byte]) -> List[byte]

Derive-Secret (§7.1): HKDF-Expand-Label with the transcript hash as context and the digest length (32) as output. transcript is Transcript-Hash(Messages) — this module never computes a transcript (that is sc21's job over real handshake messages); the caller supplies the 32-byte hash, or sha2.sum256([]) for the empty transcript the "derived"/"finished" steps use. ct: as expand_label — secret is carried through HKDF; the transcript hash is public.

extract

fn · Source line 225

pub fn extract(salt: List[byte], ikm: List[byte]) -> List[byte]

HKDF-Extract (RFC 5869 §2.2) — the key schedule's extract step, a thin name over sc16's HKDF so a test can build RFC 8448's whole chain through this module alone. salt is the previous stage's "derived" secret (or Hash.length zeros for the first extract); ikm is the (EC)DHE shared secret (or zeros where there is no input keying material). ct: salt/ikm are secret; carried through sha2's HMAC.

traffic_key

fn · Source line 247

pub fn traffic_key(secret: List[byte], key_len: int) -> List[byte]

The [sender]_write_key (§7.3): expand the traffic secret under the "key" label to the AEAD's key length (32 for ChaCha20-Poly1305, 16 for AES-128-GCM — the caller states which, since it knows the suite). The context is empty. ct: secret is the traffic secret; carried through HKDF.

traffic_iv

fn · Source line 254

pub fn traffic_iv(secret: List[byte]) -> List[byte]

The [sender]_write_iv (§7.3): expand the traffic secret under the "iv" label to the 12-byte IV length. The context is empty. ct: secret is the traffic secret; carried through HKDF.

finished_key

fn · Source line 263

pub fn finished_key(secret: List[byte]) -> List[byte]

The Finished MAC key (§4.4.4): HKDF-Expand-Label(Secret, "finished", "", Hash.length). Not used by the record layer itself, but part of the same §7.1 vocabulary and published by RFC 8448 §3, so the seam carries it for sc21's Finished computation. ct: secret is the traffic secret; carried through HKDF.

record_nonce

fn · Source line 307

pub fn record_nonce(iv: List[byte], seq: int) -> List[byte]

The per-record nonce (§5.3): iv XOR pad_left(seq, iv_length). iv is the static [sender]_write_iv (12 bytes). seq is the record sequence number (0 for the first record under a key, incrementing). ct: branch-free over the public sequence number; the wrapping XOR touches the secret write IV (wrapping-not-checked, marked for c28).

content_type_application_data

fn · Source line 337

pub fn content_type_application_data() -> int

ContentType application_data (23) — the OUTER opaque_type of every TLSCiphertext (§5.2; the real content type hides inside the encryption). Zero-argument functions stand in for consts (F-0025).

content_type_handshake

fn · Source line 343

pub fn content_type_handshake() -> int

ContentType handshake (22) — a common INNER content type (the EncryptedExtensions/Certificate/Finished flight).

record_header

fn · Source line 357

pub fn record_header(length: int) -> List[byte]

The five-byte TLSCiphertext header for an encrypted record of length bytes: 17 03 03 <length big-endian uint16> (§5.2). The legacy_record_version is 0x0303 ("TLS 1.2") on every 1.3 record. ct: public lengths only.

additional_data

fn · Source line 373

pub fn additional_data(cipher_len: int) -> List[byte]

The AEAD additional-data for a record whose ciphertext (including the 16-byte tag) is cipher_len bytes: the record header (§5.2). This is exactly record_header(cipher_len), named for the role. ct: public lengths only.

inner_plaintext

fn · Source line 387

pub fn inner_plaintext(content: List[byte], content_type: int, pad: int) -> List[byte]

The TLSInnerPlaintext (§5.2): content || ContentType content_type || zeros[pad]. The real content type is appended after the content, and pad zero octets follow (§5.4's length hiding). pad may be 0. ct: public structure; content is plaintext the caller owns.

seal

fn · Source line 416

pub fn seal( key: List[byte], iv: List[byte], seq: int, content_type: int, content: List[byte], pad: int, ) -> List[byte]

Seal a record (§5.2): build the TLSInnerPlaintext (content || content_type || pad zeros), AEAD-seal it under the per-record nonce with the record header as additional-data, and frame it as a TLSCiphertext (header || ciphertext || tag). The returned bytes are the complete record on the wire. key is the AEAD key, iv the static write IV, seq the record sequence number. ct: the AEAD is chacha20's (its ct: line carries); this function adds only public framing and the nonce (see record_nonce).

open

fn · Source line 463

pub fn open(key: List[byte], iv: List[byte], seq: int, record: List[byte]) -> List[byte] ! { tag, short, unexpected, }

Open a record (§5.2/§5.4): verify the tag BEFORE exposing any plaintext (sc17's discipline — a tag failure returns with ZERO plaintext), then strip the padding by scanning from the end for the content-type octet, and return the deprotected inner plaintext (content || content_type, padding removed). Use content_of and content_type_of on the result. record is the complete wire record (header included); key/iv/seq as for seal.

Rows: short — the record cannot hold a header, a tag and one content-type octet; tag — the AEAD rejected the box (forged, modified or truncated), no plaintext exposed; unexpected — the deprotected inner is all zeros, with no content-type octet (§5.4's unexpected_message). ct: the tag verification is chacha20's (marked as c28's first target there). The padding scan below reads the DEPROTECTED length — a value-dependent loop bound c28 must model (§5.4 notes it as a constant-time hazard).

content_type_of

fn · Source line 520

pub fn content_type_of(inner: List[byte]) -> int

The inner content type recovered by open: the last octet of the deprotected inner plaintext (§5.4). ct: reads a public position of already-authenticated plaintext.

content_of

fn · Source line 529

pub fn content_of(inner: List[byte]) -> List[byte]

The inner content recovered by open: the deprotected inner plaintext with its trailing content-type octet removed (§5.4). ct: reads public positions of already-authenticated plaintext.

to_hex

fn · Source line 551

pub fn to_hex(b: List[byte]) -> str

Lowercase hex of a byte list. ct: NOT constant-time (a rendering convenience for tests and vectors, never a secret-handling path).

Constants

No public declarations in this section.

Errors and row tags

Tags named by public signatures: short, tag, unexpected. Their conditions are documented on the functions that return them.

No public declarations in this section.