Module
std.x.tls.handshake — the TLS 1.3 handshake (RFC 8446 §4 + §4.4 + §7.1). Stability tier: x — the SECOND rung of the stdc-tls campaign (sprint sc21), built on sc20's record layer (the FROZEN "given keys -> records" seam) and the D53 crypto ladder: sc18's X25519 for the key exchange, sc16's SHA-256/HMAC/HKDF for the transcript hash and the key schedule. This module owns the MESSAGE FLOW, the TRANSCRIPT HASH, the full KEY SCHEDULE (early -> handshake -> master), FINISHED, and the handshake STATE MACHINE; sc20 owns the record framing beneath it. It is testable in isolation against RFC 8448's published trace, the sc16-before-everything discipline.
The nursery banner (D31). std.x.tls.handshake 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 THREE sibling residents — std.x.tls.record (the key-schedule primitives and the seam it produces keys for), std.x.crypto.sha2 (SHA-256 + HMAC for the transcript and Finished) and std.x.crypto.curve25519 (X25519 for the (EC)DHE shared secret) — resident-on-resident edges the graduation move to std.tls/std.crypto must resolve, named in the register beside record's and jose's own edges.
Built from the RFC DOCUMENTS, not ported. RFC 8446 (August 2018) §4 (the handshake messages + extensions), §4.1 (the client/server flows), §4.4.1 (the transcript hash), §4.4.3 (the CertificateVerify signature content), §4.4.4 (Finished), §7.1 (the key schedule). RFC 8448 (January 2019) §3 (Simple 1-RTT Handshake) is the vector spine: every handshake message, every intermediate transcript hash, every secret, both Finished MACs — asserted byte-for-byte in tests/x/tls/handshake/.
## The three byte-exact rules this module lives or dies on
1. **The transcript hash is over the exact WIRE bytes of each handshake message, INCLUDING the 4-byte handshake header (msg_type || uint24 length), concatenated in order** (§4.4.1) — NOT the 5-byte record header (that is sc20's layer, never hashed). A single off-by-one in what is hashed and every secret downstream is wrong; RFC 8448 catches it at the first Derive-Secret. This module's transcript_add/transcript_hash accumulate exactly the message bytes the builders emit.
2. key_share carries raw X25519 point bytes (§4.2.8, sc18) — the 32-byte public key on the wire, and the (EC)DHE shared secret is curve25519.x25519(private, peer_public), feeding the handshake extract as IKM. A wrong endianness or a non-canonical point fails the trace; the all-zero shared secret is rejected (sc18's {zero} row, RFC 8446 §7.4.2).
3. Finished is HMAC-SHA256(finished_key, Transcript-Hash) (§4.4.4) where finished_key = HKDF-Expand-Label(BaseKey, "finished", "", Hash.length) (sc20's record.finished_key) and the transcript is the running hash THROUGH the message before Finished (the server's over CH..CertificateVerify, the client's over CH..server Finished). The verify compares constant-time-shaped (ct_eq); a mismatch is the caller's alert, no key material exposed.
Certificate is OPAQUE here (§4.4.2). This sprint hashes and carries the Certificate message; it does NOT parse the ASN.1/X.509 inside (that is sc22). certificate frames the message structure (certificate_request_context + a certificate_list of opaque CertificateEntry cert_data) around a DER blob it never inspects.
CertificateVerify: signature content BUILT, signature NOT verified here. certificate_verify frames the message (SignatureScheme + opaque signature) and certificate_verify_content builds the §4.4.3 signed octets (64 0x20 bytes + the context string + 0x00 + Transcript-Hash) so sc22 can verify. The actual signature check is DEFERRED to sc22: RFC 8448's trace signs with rsa_pss_rsae_sha256 (0x0804), and no RSA-PSS (nor the P-256 ECDSA the client-auth trace uses) exists in the D53 crypto ladder yet — only Ed25519/X25519. So NOTHING about peer identity is trusted at this rung; deriving the right keys and verifying Finished against RFC 8448 is the deliverable.
Do not rebuild the record framing. sc20 owns seal/open and the key derivation (record.extract/derive_secret/traffic_key/ traffic_iv/finished_key); this module produces the transcript hashes and shared secret those functions take as inputs, drives the flow, and hands sc20 the traffic keys. Byte sequences are List[byte] (sc35 — the 0..255 element contract is the TYPE's now, F-0035 discharged), the sha2/chacha20/record convention.
The ws05-consumable seam (FROZEN). A completed handshake yields a HandshakeResult: the negotiated cipher_suite, the four traffic secrets (client/server handshake+application), the exporter and resumption master secrets — the negotiated params + secrets wolf-wws ws05 hands to sc20's record layer (record.traffic_key/traffic_iv turn each secret into an AEAD key/iv) to serve and consume TLS. sc21 owns transcript hashes, the key schedule and the flow; ws05 owns the socket conversation; sc20 owns the record AEAD. The seam is frozen at sc21; std/x/README.md records it. (OpenSSL s_client/s_server interop is ws05's differential, not built here — RFC 8448 is this oracle.)
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 message serialization, the transcript hash and the state machine branch and index only on PUBLIC quantities (message bytes, lengths, message types) — no secret is present. The key schedule and Finished handle SECRETS (the traffic secrets, the finished_key, the shared secret) and carry them through sc20's record.*, sc16's HMAC/HKDF and sc18's X25519, whose own ct: lines hold the wrapping-not-checked posture; this module adds no secret-dependent branch of its own. ct_eq is the Finished compare — accumulate-then-single-check, no early exit, a c28 target (the sc17 open-tag discipline). The 0..255 ingestion guards are the recorded value-dependent branch shared with the ladder (F-0035's real byte type retires them).
Functions and methods
fn · Source line 184
pub fn concat(a: List[byte], b: List[byte]) -> List[byte]
a followed by b — the byte-sequence concatenation the callers and the transcript build on. ct: public structure only.
fn · Source line 204
pub fn hs_client_hello() -> int
HandshakeType client_hello (1). Zero-argument functions stand in for consts (F-0025).
fn · Source line 207
pub fn hs_server_hello() -> int
HandshakeType server_hello (2).
fn · Source line 210
pub fn hs_encrypted_extensions() -> int
HandshakeType encrypted_extensions (8).
fn · Source line 213
pub fn hs_certificate() -> int
HandshakeType certificate (11).
fn · Source line 216
pub fn hs_certificate_verify() -> int
HandshakeType certificate_verify (15).
fn · Source line 219
pub fn hs_finished() -> int
HandshakeType finished (20).
fn · Source line 225
pub fn handshake(msg_type: int, body: List[byte]) -> List[byte]
Frame a handshake message: msg_type byte, a big-endian uint24 length of body, then body (§4). This is the exact wire message the transcript hashes (header included). ct: public structure only.
fn · Source line 244
pub fn ext(extension_type: int, data: List[byte]) -> List[byte]
One Extension: extension_type (uint16) then a uint16-length-prefixed extension_data. ct: public structure only.
fn · Source line 254
pub fn ext_server_name(host: str) -> List[byte]
server_name (0), a single host_name (§3 of RFC 6066): a ServerNameList of one ServerName { NameType host_name(0), HostName opaque<1..2^16-1> }.
fn · Source line 263
pub fn ext_server_name_ack() -> List[byte]
server_name (0) as the server's empty acknowledgement in EncryptedExtensions (§4.2.10): the extension_data is empty.
fn · Source line 269
pub fn ext_renegotiation_info_empty() -> List[byte]
renegotiation_info (0xff01) with an empty renegotiated_connection (RFC 5746): a single zero length byte. TLS 1.3 carries it inertly.
fn · Source line 275
pub fn ext_supported_groups(groups: List[int]) -> List[byte]
supported_groups (0x000a, §4.2.7): a NamedGroupList, each group a big-endian uint16, uint16-length-prefixed.
fn · Source line 284
pub fn ext_session_ticket_empty() -> List[byte]
session_ticket (0x0023, RFC 5077) with an empty ticket.
fn · Source line 293
pub fn ext_key_share_client_x25519(pubkey: List[byte]) -> List[byte]
key_share (0x0033, §4.2.8) for a ClientHello: a KeyShareClientHello = a uint16-length-prefixed vector of one KeyShareEntry { NamedGroup x25519(0x001d), key_exchange opaque<1..2^16-1> = the 32 point bytes }. ct: the point bytes are public.
fn · Source line 303
pub fn ext_key_share_server_x25519(pubkey: List[byte]) -> List[byte]
key_share (0x0033, §4.2.8) for a ServerHello: a KeyShareServerHello = a single KeyShareEntry (NO client_shares vector wrapper). ct: the point bytes are public.
fn · Source line 313
pub fn ext_supported_versions_client() -> List[byte]
supported_versions (0x002b, §4.2.1) for a ClientHello: a uint8-length-prefixed list of ProtocolVersions, here the single TLS 1.3 (0x0304).
fn · Source line 319
pub fn ext_supported_versions_server() -> List[byte]
supported_versions (0x002b, §4.2.1) for a ServerHello: the single selected_version (0x0304), NOT length-prefixed.
fn · Source line 325
pub fn ext_signature_algorithms(schemes: List[int]) -> List[byte]
signature_algorithms (0x000d, §4.2.3): a SignatureSchemeList, each scheme a big-endian uint16, uint16-length-prefixed.
fn · Source line 335
pub fn ext_psk_key_exchange_modes(modes: List[byte]) -> List[byte]
psk_key_exchange_modes (0x002d, §4.2.9): a uint8-length-prefixed list of PskKeyExchangeMode bytes (0x01 = psk_dhe_ke).
fn · Source line 340
pub fn ext_record_size_limit(limit: int) -> List[byte]
record_size_limit (0x001c, RFC 8449): a single big-endian uint16.
fn · Source line 357
pub fn client_hello( random: List[byte], session_id: List[byte], cipher_suites: List[byte], extensions: List[byte], ) -> List[byte]
ClientHello (§4.1.2): legacy_version 0x0303, a 32-byte random, an opaque legacy_session_id<0..32>, a CipherSuite vector<2..2^16-2> (raw suite bytes, uint16-length-prefixed here), the single null legacy_compression_method, and the extensions vector<8..2^16-1>. cipher_suites and extensions are the concatenated inner bytes. ct: every field is public.
fn · Source line 379
pub fn server_hello( random: List[byte], session_id: List[byte], cipher_suite: int, extensions: List[byte], ) -> List[byte]
ServerHello (§4.1.3): legacy_version 0x0303, a 32-byte random, the echoed opaque legacy_session_id_echo<0..32>, the selected cipher_suite (uint16), the null legacy_compression_method, and the extensions vector<6..2^16-1>. ct: every field is public (the server random is not a secret).
fn · Source line 399
pub fn encrypted_extensions(extensions: List[byte]) -> List[byte]
EncryptedExtensions (§4.3.1): the extensions vector<0..2^16-1>. extensions is the concatenated inner bytes. ct: public structure only.
fn · Source line 409
pub fn certificate(context: List[byte], cert_der: List[byte]) -> List[byte]
Certificate (§4.4.2) — OPAQUE cert bytes here (no ASN.1 parse): a certificate_request_context<0..255> then a certificate_list<0..2^24-1> of one CertificateEntry { cert_data opaque<1..2^24-1> = the DER blob, extensions Extension<0..2^16-1> = empty }. cert_der is carried verbatim and never inspected (X.509 is sc22). ct: public structure; the DER is public.
fn · Source line 424
pub fn certificate_verify(algorithm: int, signature: List[byte]) -> List[byte]
CertificateVerify (§4.4.3): the SignatureScheme algorithm (uint16) and the opaque signature<0..2^16-1>. The signature is CARRIED, not verified here (see the module header — RSA-PSS/ECDSA verification is sc22); certificate_verify_content builds the octets it is over. ct: public structure; the signature is public wire material.
fn · Source line 438
pub fn certificate_verify_content(context: str, transcript_hash: List[byte]) -> List[byte]
The octet string a CertificateVerify signature is computed over (§4.4.3): 64 0x20 (space) octets, then the ASCII context string ("TLS 1.3, server CertificateVerify" or "...client..."), then a single 0x00 separator, then the Transcript-Hash up to but not including the CertificateVerify. Built here for sc22 to verify against a real public key; this rung does NOT verify the signature. ct: public content (transcript hash is public).
fn · Source line 456
pub fn finished_message(verify_data: List[byte]) -> List[byte]
Finished (§4.4.4): the verify_data (an HMAC-SHA256 output, 32 bytes) framed as a handshake message. Use finished_verify_data to compute the verify_data. ct: the verify_data is derived from a secret via HMAC; the framing is public.
fn · Source line 471
pub fn transcript_new() -> List[byte]
A fresh, empty transcript. ct: no secret.
fn · Source line 478
pub fn transcript_add(mut acc: List[byte], msg: List[byte])
Append one handshake message's exact wire bytes (header included) to the running transcript, in order (§4.4.1). ct: public message bytes only.
fn · Source line 486
pub fn transcript_hash(acc: List[byte]) -> List[byte]
The Transcript-Hash: SHA-256 of the accumulated handshake messages (§4.4.1). Feeds Derive-Secret (sc20's record.derive_secret) and Finished. ct: hashes public message bytes; the digest is public context.
fn · Source line 540
pub fn ecdhe_shared(private: List[byte], peer_public: List[byte]) -> List[byte] ! {zero}
The (EC)DHE shared secret for the handshake extract (§7.4.2): the X25519 of your private scalar against the peer's peer_public point (sc18). Both parties reach the same value; the all-zero result is rejected (RFC 8446 §7.4.2 / sc18's {zero} row). ct: private is a secret scalar carried through curve25519's ladder (its ct: line holds the branch-free posture); the reject-on-zero is on the public output.
fn · Source line 549
pub fn early_secret() -> List[byte]
The early secret (§7.1): HKDF-Extract(0, 0) for the no-PSK handshake — salt and IKM both 32 zero bytes. ct: carried through record/sha2 HKDF.
fn · Source line 557
pub fn handshake_secret(early: List[byte], dh: List[byte]) -> List[byte]
The handshake secret (§7.1): HKDF-Extract with salt Derive-Secret(early, "derived", "") and IKM the (EC)DHE shared secret. empty_hash is SHA-256 of the empty string, the "derived" context. ct: early/dh are secret, carried through record/sha2.
fn · Source line 565
pub fn master_secret(hs: List[byte]) -> List[byte]
The master secret (§7.1): HKDF-Extract with salt Derive-Secret(handshake, "derived", "") and IKM 32 zero bytes. ct: hs is secret, carried through record/sha2.
fn · Source line 573
pub fn client_handshake_traffic(hs: List[byte], th_ch_sh: List[byte]) -> List[byte]
client_handshake_traffic_secret (§7.1): Derive-Secret(handshake, "c hs traffic", Transcript-Hash(ClientHello..ServerHello)). ct: hs is secret; the transcript hash is public.
fn · Source line 580
pub fn server_handshake_traffic(hs: List[byte], th_ch_sh: List[byte]) -> List[byte]
server_handshake_traffic_secret (§7.1): Derive-Secret(handshake, "s hs traffic", Transcript-Hash(ClientHello..ServerHello)). ct: hs is secret; the transcript hash is public.
fn · Source line 587
pub fn client_application_traffic(master: List[byte], th_sfin: List[byte]) -> List[byte]
client_application_traffic_secret_0 (§7.1): Derive-Secret(master, "c ap traffic", Transcript-Hash(ClientHello..server Finished)). ct: master is secret; the transcript hash is public.
fn · Source line 594
pub fn server_application_traffic(master: List[byte], th_sfin: List[byte]) -> List[byte]
server_application_traffic_secret_0 (§7.1): Derive-Secret(master, "s ap traffic", Transcript-Hash(ClientHello..server Finished)). ct: master is secret; the transcript hash is public.
fn · Source line 601
pub fn exporter_master(master: List[byte], th_sfin: List[byte]) -> List[byte]
exporter_master_secret (§7.1): Derive-Secret(master, "exp master", Transcript-Hash(ClientHello..server Finished)). ct: master is secret; the transcript hash is public.
fn · Source line 608
pub fn resumption_master(master: List[byte], th_cfin: List[byte]) -> List[byte]
resumption_master_secret (§7.1): Derive-Secret(master, "res master", Transcript-Hash(ClientHello..client Finished)). ct: master is secret; the transcript hash is public.
fn · Source line 623
pub fn finished_verify_data(base_secret: List[byte], transcript_hash: List[byte]) -> List[byte]
The Finished verify_data (§4.4.4): HMAC-SHA256(finished_key, Transcript-Hash), where finished_key = HKDF-Expand-Label(base_secret, "finished", "", 32) (sc20's record.finished_key). base_secret is the sender's handshake traffic secret (the server's for the server Finished, the client's for the client Finished). transcript_hash is the running hash THROUGH the message before this Finished. ct: base_secret is secret, carried through record/sha2 HMAC.
fn · Source line 631
pub fn finished_for(base_secret: List[byte], transcript_hash: List[byte]) -> List[byte]
The complete Finished handshake message for a sender: frame the verify_data computed from base_secret over transcript_hash. ct: as finished_verify_data.
fn · Source line 643
pub fn ct_eq(a: List[byte], b: List[byte]) -> bool
Constant-time-shaped byte equality (§4.4.4's compare, sc17's tag discipline): fold every position's difference into an accumulator and test ONCE at the end — no early exit on the first mismatch, no secret-dependent branch. Unequal lengths answer false without reading (a public, structural fact). ct: THE compare — accumulate-then-single-check, a c28 verification target. Loop bound and indices are public (the 32-byte digest length); the per-byte XOR touches secret-derived MAC bytes without branching.
fn · Source line 665
pub fn verify_finished( base_secret: List[byte], transcript_hash: List[byte], received: List[byte], ) -> bool
Verify a peer's Finished (§4.4.4): recompute the expected verify_data from base_secret (the peer's handshake traffic secret) over transcript_hash and compare it constant-time-shaped to received. A false answer is the caller's decrypt_error/handshake-abort alert; no key material is exposed on the miss (the recompute leaks nothing the peer did not already send). ct: recompute is finished_verify_data; compare is ct_eq.
fn · Source line 681
pub fn accept_finished( base_secret: List[byte], transcript_hash: List[byte], received: List[byte], ) -> int ! {decrypt_error}
Accept a peer's Finished or raise the handshake-abort alert (§4.4.4 + §6.2): return 1 when the constant-time compare holds, otherwise the decrypt_error row — a wrong Finished aborts the handshake with the named alert and NO key material exposed (the recompute reveals nothing the peer did not already send; nothing derived-onward is returned on the miss). The state machine and the record layer continue their smuggling/confusion discipline through this named reject. ct: as verify_finished.
fn · Source line 704
pub fn role_client() -> int
The client role (0) — sends ClientHello, then receives the server's flight (ServerHello, EncryptedExtensions, Certificate, CertificateVerify, Finished), then sends its own Finished.
fn · Source line 708
pub fn role_server() -> int
The server role (1) — receives ClientHello, sends its flight, then receives the client's Finished.
fn · Source line 714
pub fn hs_expected(role: int, stage: int) -> int
The handshake message type the given role legally receives at stage, or -1 when the flow expects no further message (the flight is complete). §4.1's ordered flows. ct: public role/stage only.
fn · Source line 735
pub fn hs_step(role: int, stage: int, msg_type: int) -> int ! {unexpected_message}
Drive one received message through the state machine (§4.1): if msg_type is the message the flow expects next for role at stage, return the next stage; otherwise raise unexpected_message (§6.2) — the reordered/dropped/illegal-message reject, by name. stage starts at 0 (the client after sending ClientHello / the server before receiving it). ct: branches on public message type and stage only.
fn · Source line 749
pub fn hs_client_flight_complete(stage: int) -> bool
Whether the client has received the full server flight (its own Finished is next to SEND): stage 5 after ServerHello..server Finished. ct: public.
fn · Source line 756
pub fn hs_server_complete(stage: int) -> bool
Whether the server has received the client's Finished — the handshake is complete: stage 2 after ClientHello and client Finished. ct: public.
fn · Source line 791
pub fn handshake_result( cipher_suite: int, client_handshake_secret: List[byte], server_handshake_secret: List[byte], client_application_secret: List[byte], server_application_secret: List[byte], exporter_master_secret: List[byte], resumption_master_secret: List[byte], ) -> HandshakeResult
Build the frozen ws05 result (the constructor — a module-qualified struct literal handshake.HandshakeResult { } does not parse across the module boundary, E0201, so the seam is a function exactly as sha2.new256 is). Called once a handshake completes; cipher_suite is the negotiated suite, the six secrets are this module's schedule outputs. ws05 hands the result to sc20's record layer. ct: a value container; the secrets are carried, no branch on them.
fn · Source line 819
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).