Skip to content
CryptoVault

Chapter VII

Protocols in the wild

How the primitives combine in the systems you use every day: the padlock, your password, your phone, your messages and the blockchain.

  1. VII.1TLS 1.3
  2. VII.2Storing passwords
  3. VII.3One-time codes (TOTP)
  4. VII.4Passkeys
  5. VII.5End-to-end messaging
  6. VII.6Signed tokens (JWT)
  7. VII.7Blockchains

TLS 1.3

One round trip: an ephemeral key exchange for secrecy, a signed certificate for identity, HKDF for keys, AEAD for data.

encrypted from here onbrowserserver99381de5…ClientHelloX25519 key share (V.3)c9828876…ServerHelloX25519 key share (V.3)8bd4054f…8bd4054f…same secret, never sentHKDFHKDF3fce5160…3fce5160…server’s handshake key (III.6)Certificatechain up to a trusted root (VI.5)CertificateVerifysignature over the transcript (VI.1)Finished, each wayHMAC of the transcript (III.5)application dataAES-GCM or ChaCha20-Poly1305 (IV.6)
Values from the RFC 8448 trace, recomputed: everything after the two hellos is encrypted, the certificate included.

Browsers now add an ML-KEM share (chapter IX) to X25519’s, against a future quantum computer: 1,216 bytes instead of 32.

Drop the signature and you get a private channel to Mallory in the middle.

Storing passwords

Store a slow, salted, memory-hard hash such as Argon2id. Never the password, never a fast hash.

A breached server loses its password table. A salt makes equal passwords look different; a slow hash makes each guess expensive.

SHA-256(password)PBKDF2 × 600,000salthashalice8d969eef6ecad3c2…alicea00087…f22fee71…bob8d969eef6ecad3c2…bobbb5349…d35c3bbc…same hash, cracked oncedifferent hashesSHA-25622 billion / s2 minutesPBKDF214,800 / s6 yearssecondminutehourdayyear
Alice and Bob both chose 123456; the bars time a search of all 36⁸ passwords of 8 lowercase letters and digits on one RTX 4090.

Prefer Argon2id (at least 19 MiB, 2 passes), which also fills memory GPUs lack; PBKDF2 where FIPS applies.

Try it: what one guess costs
PitfallEncrypting passwords instead of hashing them: whoever steals the key reads them all.

One-time codes (TOTP)

Phone and server share a secret, compute an HMAC of the current 30-second window, and keep six digits.

A QR code gives your phone the secret K that the server keeps. Both then compute the code from K and the clock.

phoneserverK = GEZDGNBV…K = GEZDGNBV…QR code, oncet = 59 st = 59 s⌊59 / 30⌋ = 1HMAC-SHA-1(K, counter)4 bytes75a48a19d4cbe100644e8ac1397eea747a2d33ablast hex digit b = offset 11top bit cleared, in decimal1094287082last six digits287 082287 082typed in, equal
Phone and server run the same computation, each with its own copy of the secret and its own clock: no network involved.
PitfallA typed code can be typed into a fake page, which relays it to the real site within its 30 seconds.

Passkeys

You log in by signing a fresh challenge bound to the site’s real domain: a phishing site gets nothing usable, and a breach leaks only public keys.

The private key stays on your device; the site keeps the public key and, at each sign-in, sends a fresh challenge to sign.

bank.examplekeeps your public keychallengerelayedMMalloryyou on the real sitechallengeAAECAwQFBg…origin, from the browserhttps://bank.exampledevice signsto the bankchallengeoriginsignaturesigned inyou on a look-alikechallengeAAECAwQFBg…origin, from the browserhttps://bank-example.comdevice signsrelayed to the bankchallengeoriginsignaturerefused
The browser, not the page, writes the origin into what the device signs, so a signature made on a look-alike is useless.
Nothing to phish, and nothing worth stealing from the server: public keys sign nothing.

End-to-end messaging

A new key for every message, old ones deleted, fresh Diffie-Hellman secrets mixed in: a stolen key opens no past message, and soon no future one.

Signal and WhatsApp start from Diffie-Hellman exchanges with keys the recipient published in advance (chapter V). Message keys then come off a one-way HMAC chain.

chain keymessage keyck₁ 00010203…HMAC 01mk₁ 9b4c8120…sealed message 1HMAC 02ck₂ 4304c22c…HMAC 01mk₂ f7703c39…sealed message 2HMAC 02ck₃ 26fc86b9…no way backEEvecopies the phonereads message 3next DH stepnew chainroot key + new DH secretlocked out
HMAC-SHA-256, as Signal’s specification recommends; dashed keys are already deleted.
A stolen phone opens no past message (forward secrecy), and the next exchange locks the thief out (post-compromise security).

Signed tokens (JWT)

header.payload.signature in Base64url: anyone can read it, nobody should trust it before checking the signature.

Many APIs hand out a signed token instead of keeping a session. Base64url is no encryption: only the signature makes the claims trustworthy.

sent, in Base64urldecodedheadereyJ0eXAiOiJKV1Qi…typ: "JWT", alg: "HS256".payloadeyJpc3MiOiJqb2Ui…iss: "joe"exp: 1300819380example.com/is_root: true.signaturedBjftJeZ4CVP-mB9…HMAC-SHA-256 of header.payloadunder the server’s secret keyMMalloryeyJpc3MiOiJtYWxs…iss: "mallory", old signaturesignature does not matcheyJhbGciOiJub25l…alg: "none", no signaturealgorithm "none" refused
Anyone can decode the first two parts; only the holder of the key can make the third.
PitfallThe header names the algorithm, and Mallory writes the header. The server fixes the algorithm; it never reads it from the token.

Blockchains

Signatures authorize payments, hash links freeze history, Merkle trees summarize blocks, proof of work makes rewriting costly.

ECDSA or Schnorr signatures on the curve secp256k1 authorize spending. Each header commits to its transactions through a Merkle root, and to the previous block through its hash.

block 0, 3 January 2009000000000019d668…prevnone, 00000000…nonce2083236893Merkle root4a5e1e4b…block 100,000000000000003ba27…prev000000000002d01c…nonce274148111Merkle rootf3e94742…⋯99,999blocksf3e947…ccdafb…8e3089…8c14f0…fff252…6359f0…e9a668…hashed in pairs,double SHA-256block 100,000’s transactions
A header counts only if its double SHA-256, printed byte-reversed, falls below a target: about 2³² tries for block 0.
Try it: mine a block
Rewriting a block means redoing its work and that of every block since.

In one breath

  1. TLS 1.3. One round trip: an ephemeral key exchange for secrecy, a signed certificate for identity, HKDF for keys, AEAD for data.
  2. Storing passwords. Store a slow, salted, memory-hard hash such as Argon2id. Never the password, never a fast hash.
  3. One-time codes (TOTP). Phone and server share a secret, compute an HMAC of the current 30-second window, and keep six digits.
  4. Passkeys. You log in by signing a fresh challenge bound to the site’s real domain: a phishing site gets nothing usable, and a breach leaks only public keys.
  5. End-to-end messaging. A new key for every message, old ones deleted, fresh Diffie-Hellman secrets mixed in: a stolen key opens no past message, and soon no future one.
  6. Signed tokens (JWT). header.payload.signature in Base64url: anyone can read it, nobody should trust it before checking the signature.
  7. Blockchains. Signatures authorize payments, hash links freeze history, Merkle trees summarize blocks, proof of work makes rewriting costly.