Skip to content
Crypto2026-10-04Shuai Li6 min read

HKDF: How TLS 1.3 Grows One Shared Secret Into a Whole Key Tree

The problem: one secret, one tree of keys

When a TLS handshake completes, both sides hold a shared secret (the X25519 output, or the two secrets concatenated in the ML-KEM hybrid group). But a TLS session needs far more than one key:

  • Client and server handshake-stage write keys and IVs (protecting the Finished messages)
  • Client and server application-data write keys and IVs
  • Per-stage Finished verification keys
  • The exporter interface (key material for upper layers)
  • Resumption PSKs and ticket keys

And these keys must be mutually independent — leaking an application-data key must not endanger handshake integrity, and old session keys must not help attack new ones. Growing one secret into a tree, safely, is HKDF's job.

Two steps: Extract converges, Expand branches

HKDF (RFC 5869) is built on HMAC, in two phases:

Extract: PRK = HMAC(salt, IKM) — mix the input keying material (whose distribution may be uneven) with a salt, converging it into a pseudorandom key of uniform cryptographic quality. The salt provides domain separation: different purposes, different salts, different PRKs even from identical IKM. When salt is empty, the RFC substitutes HashLen zero octets — "leave blank" is not "skip Extract".

Expand: OKM = HKDF-Expand(PRK, info, L) — derive output keying material of any length (up to 255 × HashLen), labeled by info. The info field says exactly what this key "is, for whom, for what". Internally Expand is a counter-chained HMAC: T(1) || T(2) || …, each block consuming the previous block, info, and a counter byte.

Why not just hash(secret || label)? Three hard reasons: length-extension-style structural weaknesses (depending on the hash's construction), no safe way to produce long outputs, and the decisive one — no built-in domain separation. The info mechanism guarantees "client write key" and "server write key" can never collide into the same key.

The on-site HMAC tool now has an HKDF mode: enter IKM / salt / info / length and you get both PRK (Extract) and OKM (Expand). Input encoding supports UTF-8 or Hex — pick Hex when checking RFC 5869 appendix vectors.

Salt vs info: two different knobs

FieldRoleTypical practice
saltDomain separation at Extract; may be publicSession nonce, protocol constant, context ID; empty → zero fill
infoPurpose label at Expand; may be public"c ap traffic", "client-write-key", a version string

Salt answers "which derivation context is this"; info answers "which branch of the tree". Both may travel in the clear — they are not secrets; the secret lives in the IKM (or the upstream DH/KEM output). Swapping the two can still be secure, but readability and cross-implementation checklists suffer; follow the protocol's field semantics.

The TLS 1.3 three-layer schedule

TLS 1.3 uses HKDF as a scheduling tree:

Early Secret  ←  PSK (resumption path)
   ↓ Derive-Secret
Handshake Secret  ←  ECDHE/ML-KEM shared secret injected
   ↓ "c hs traffic" / "s hs traffic"   → handshake-stage keys
Master Secret
   ↓ "c ap traffic" / "s ap traffic"   → application-data keys
   ↓ "exp master"                      → exporter
   ↓ "res master"                      → resumption PSK

Each layer is one HKDF-Extract (injecting a new secret) plus multiple labeled Expands. The key property: each handshake stage only unlocks its own layer's keys — if handshake keys are compromised, application-layer keys still stand independently; meanwhile the Finished messages, keyed by handshake keys, cover the entire transcript so a man in the middle cannot tamper with negotiation.

Boundaries with HMAC and PBKDF2

Three names get shoved into the same phrase "key derivation", with different jobs:

HMACHKDFPBKDF2 / bcrypt / argon2
NatureMAC: prove message + key holderFast KDF: branch keys from high-entropy secretsSlow KDF: stretch low-entropy passwords
InputMessage + shared keyAlready high-entropy IKM (DH/KEM output)Human passwords
Design goalIntegrity / authenticationFast (session setup must not stall)Slow (punish brute force)
Brute-force resistanceNot its job (key must be long)Not its jobIts core job

Rule of thumb:

  • Prove "who sent it / was it altered" → HMAC
  • You already hold a high-entropy secret and need many purpose keys → HKDF
  • You only have a user password → PBKDF2 / argon2 (the on-site AES tool takes the slow path when deriving from a passphrase)

Deriving from a password with HKDF, or deriving TLS session keys with argon2, are both category errors — the first has no defense at all, the second adds hundreds of milliseconds for nothing.

Three misuses that show up in code review

1. Leaving info empty. The info label is the domain-separation mechanism — an empty info means every derived key comes from the same derivation context, and two "different" keys can collide semantically. Always label: "client-write-key", "v1 session", anything unique to the purpose.

2. Using PRK directly as a key. The output of Extract is an intermediate, not a finished key. Skipping Expand means losing length control and label separation. The pattern is always Extract → Expand(purpose) → key.

3. Omitting the salt. HKDF permits an empty salt, but a fixed, context-specific salt (even a constant string) hardens the derivation against cross-protocol interactions — when the same IKM ever flows through two different protocols, salts keep their outputs apart.

A cleaner case study: WireGuard's HKDF chain

TLS 1.3 is the flagship user, but WireGuard is the more teachable one — its entire key schedule is a short, readable chain of HMAC and HKDF calls: the static ECDH outputs feed a handshake-derived chain, every rekey derives the next traffic key from the previous one, and every key is labeled by its role. Reading the WireGuard paper's key-derivation figure is a fast way to make HKDF "click" — it is TLS 1.3's design philosophy with less ceremony.

Implementer's note

HKDF is a first-class citizen in browser WebCrypto (crypto.subtle.deriveBits with the HKDF algorithm). The on-site tool contrast is direct: HMAC mode is one sign call; HKDF mode uses the same primitives for Extract (HMAC(salt, IKM)) then Expand (deriveBits). RFC 5869 Appendix A vectors (including empty salt) are covered in local tests. The other side of the contrast: the AES tool must take the slow PBKDF2 path when deriving keys from passphrases (browser-measured at only tens of thousands of iterations per second — a feature, not a performance bug). The two derivation regimes coexist in one tool site, turning the fast-KDF / slow-KDF table into something touchable.