Repository navigation
Design TLS Handshake
Status: implemented (1.7) · Builds on: TLS · Date: 2026-10-24
A TLS handshake on mbedtls cost about 7.4 ms of server CPU on the test VM (an 8-vCPU VMware guest on a Xeon
E5-2690 v2), against 3.0 ms for openssl s_server with the same RSA-2048 certificate. A 10 KB TLS echo costs
about 0.1 ms. Two steps make up nearly all of it:
| Step (one core, test VM) | mbedtls | OpenSSL |
|---|---|---|
| RSA-2048 PSS signature (CertificateVerify) | 5.1 ms | 1.3 ms |
| X25519 key pair and shared secret | 2.1 ms | 0.14 ms |
mbedtls does both on its generic bignum code; no build option it has changed that measurably (the exponentiation window size was tried). HttpArena's 8gbit profile opens 512 TLS connections and scores CPU per request and mean latency: the handshakes were about 7 µs of CPU per request there, and while a worker signed, the requests of every other connection on it waited.
hs13 serves the handshake almost every client offers, and only that:
- TLS 1.3, X25519 key share,
TLS_AES_128_GCM_SHA256, an RSA certificate (1024 to 4096-bit, a multiple of 128 bits, primes of equal size) signed for withrsa_pss_rsae_sha256; - ALPN absent or including
http/1.1(orh2on a listener that speaks HTTP/2, which then wins); middlebox compatibility mode (a session ID, answered with a change_cipher_spec) or not; - a ClientHello in one record, and the client's Finished in one record (a split Finished is refused with decrypt_error; no client known splits it). No HelloRetryRequest, no session tickets (as before: viltrum issues none), no client certificates, no early data.
Everything else goes to mbedtls unchanged: TLS 1.2, a hello whose only shares are P-256 or P-384, AES-256 or ChaCha20 as the only suites, an ECDSA certificate, a hello split across records, early data or a cookie. The ClientHello is read into the BIO read-ahead without being consumed, so mbedtls finds it where it would have.
| File | What |
|---|---|
engine/x25519.h |
X25519 (RFC 7748): five 51-bit limbs, products in 128-bit sums (after curve25519-donna), ref10's inversion chain |
engine/rsa.h |
RSASSA-PSS-SHA256 signing: CRT, Montgomery multiplication by product scanning with an x86-64 mulq step, dedicated squaring |
engine/hs13.h |
ClientHello parsing, ServerHello, the key schedule, sealing the server flight, the client Finished check |
engine/hs13_linux.c.v |
The state machine on a connection: hello, flight out, client Finished in, hand-off to the record layer |
A connection's handshake starts in hs13. When the ClientHello is one it serves, it builds the whole server
flight in one go: ServerHello, an optional change_cipher_spec, then EncryptedExtensions, Certificate (built
once per certificate load), CertificateVerify and Finished under the server handshake key, in records of at
most 16 KB. It then waits for the client's Finished under the client handshake key, checks it in constant
time, and moves the session to the TLS 1.3 record layer (tls13_linux.c.v) with the application traffic
secrets. mbedtls is never called for such a connection.
SHA-256, HMAC and HKDF stay on mbedtls: they cost microseconds here. Parsing the key file and the cold precomputation (Montgomery constants, the first blinding pair) use mbedtls' bignum too.
- Constant time in every secret: the X25519 ladder swaps with masks; Montgomery products end in a masked subtraction; the reduction carries into the next column rather than rippling (no data-dependent loop); exponentiation uses a fixed 4-bit window over every bit of the exponent's limbs and reads the whole table for each lookup.
- Blinding: the message is multiplied by r^e before the exponentiation and the result by r^-1 after; the pair is squared after each signature. Each worker keeps its own pair, so no lock.
- Faults: the signature is checked with the public exponent before it leaves (Boneh, DeMillo and Lipton's CRT fault attack).
- Shared secret: an all-zero X25519 result (a small-order point) fails the handshake (RFC 8446 7.4.2).
- Secrets are wiped when the handshake ends or the connection closes.
- RFC 7748's X25519 vectors, the iterated test, and the Diffie-Hellman example.
- RFC 8448's simple 1-RTT trace: our parser on its ClientHello, our ServerHello byte for byte from its random and key, the handshake secrets, the master secret, the traffic keys, both Finished values and the application secrets.
- Signatures for 2048, 3072 and 4096-bit keys verified by mbedtls' PSS verification (during development); in CI, 64 signatures with the test key.
- Which hellos are served, deferred or refused; every truncation of a good hello; 500k mutated hellos in CI. 3M mutations under ASAN and UBSAN, the full flight on a sample, during development.
- The existing TLS tests run through hs13 in the gcc build (the tcc build has no 128-bit integers and stays on
mbedtls). By hand: OpenSSL
s_client(default, ALPN, no middlebox mode, and the fallback cases), curl, Python'sssl, a 26 KB certificate chain.
On the test VM:
| mbedtls | hs13 | |
|---|---|---|
Server CPU per handshake (openssl s_time, 1 worker) |
7.35 ms | 3.73 ms |
| RSA-2048 PSS signature | 3.9 to 6.2 ms | 2.1 to 3.0 ms |
| X25519, one scalar multiplication | about 1 ms | 62 µs |
128 TLS connections dialed at once, 10 KB echo paced at 2000 req/s for 5 s, latency from each request's scheduled send, 3 alternated rounds (both with handshakes queued one per turn of the loop):
| CPU per request | p99 | p99.9 | |
|---|---|---|---|
| mbedtls | 249 to 334 µs | 7.6 to 33 ms | 16.8 to 67.5 ms |
| hs13 | 162 to 178 µs | 1.6 to 3.0 ms | 4.8 to 7.2 ms |
OpenSSL still signs about twice as fast (1.3 ms). Its x86-64 code uses mulx/adx and AVX2 where the CPU has
them; ours is one generic path.
- ECDSA certificates and P-256/P-384 key exchange on our own code (mbedtls serves them).
- HelloRetryRequest, PSK resumption, 0-RTT, client certificates.
- TLS 1.2 on our own code.