HKDF — key derivation
One secret, many keys
The shared secret of an ECDH exchange is 32 bytes, but an application needs several keys: a separate one for encryption in each direction and another for a MAC. HKDF (RFC 5869) makes them in two steps built from HMAC. Extract computes PRK = HMAC(salt, IKM): the salt is the key and the input keying material (IKM) the message. Why, if the secret is secret anyway? A raw DH result is not a uniformly random byte string. Z on the P-256 curve is the x coordinate of a point: a number below p, and only about every other such number is the x coordinate of some point. That is why RFC 5869 (§3.3) says the extract step should not be skipped for a Diffie–Hellman value: it concentrates the entropy of IKM into a 32-byte PRK that can be used as a random HMAC key. Expand stretches PRK with a chain of HMACs, T(i) = HMAC(PRK, T(i−1) ‖ info ‖ i), until it has L bytes — at most 255 blocks. The info parameter labels the purpose: from one PRK, different labels give independent keys, and the same inputs always give the same key, so both sides of an exchange compute it on their own. The salt need not be secret; the RFC recommends a random one as long as the hash output (§3.1), and without one Extract uses 32 zero bytes.
HKDF-SHA-256 step by step
client → server encryption key
A label can be at most 1024 B.
server → client encryption key
A label can be at most 1024 B.
MAC key
A label can be at most 1024 B.
L is a whole number from 1 to 8,160: the block counter is one byte, so at most 255 blocks of 32 B (RFC 5869 §2.3). WebCrypto refuses more too.
Generating two P-256 key pairs and agreeing on an ECDH secret…
A fast KDF and a weak password
HKDF is fast by design: one key is a few HMACs. With an ECDH secret that is an advantage; with a password it is a disaster. Extract concentrates entropy but cannot add any (RFC 5869, §4): a key from the password “sunshine” is exactly as hard to guess as the password itself. The app below encrypts a note with the key HKDF(password, salt, info). The salt is public, stored next to the ciphertext, so an attacker with a leak computes what the app computes, word by word, and the AES-GCM tag tells her which word hit. A salt only rules out tables computed in advance, not guessing — just as in the password hashing station. RFC 5869 points out that a password-based KDF slows down dictionary attacks with a salt and a deliberately costly computation, and HKDF has no such slowdown. Switch the app to Argon2id with the parameters of that station: the guessing rate is measured on your device, and the time for the whole dictionary and the whole password space is a labelled extrapolation from that measurement.
A note encrypted with a key from a password
HKDF-SHA-256 from WebCrypto · info = “trapdoorlab notes v1” · salt 16 B · nothing slows it down
Type any password, but not your real one. Everything is computed in your browser, nothing goes to the server.
The password must be 1 to 1024 B.
The app derives an AES-256-GCM key from the password and encrypts the note “Safe PIN: 4071. The spare key is with the neighbour.” with it. Next to it, it stores the salt and the nonce in the clear.
The app is encrypting the note…
Where HKDF makes keys
- TLS 1.3The key schedule is a chain of HKDF-Extract calls into which the (EC)DHE secret enters as IKM. Separate client and server traffic secrets come from HKDF-Expand with labels such as “c hs traffic” and “s hs traffic”, prefixed with “tls13 ”, with the hash of the handshake transcript as context, and the key and IV from them through further Expand calls labelled “key” and “iv” (RFC 9846, §7.1 and §7.3).Source (TLS 1.3)
- Web PushPush message encryption combines a P-256 ECDH secret with the browser’s 16-byte authentication secret: HKDF-SHA-256 with that secret as the salt and both public keys in info (“WebPush: info”). RFC 8291 writes these steps out as single HMAC-SHA-256 calls — just like the toy above.Source (Web Push)
- Signal — PQXDHThe session key SK is 32 bytes of HKDF whose IKM concatenates the outputs of three or four DH exchanges and the secret of a post-quantum KEM, prefixed with 32 0xFF bytes. The salt is all zeros, as long as the hash output, and info joins the protocol name, curve, hash and KEM.Source (Signal — PQXDH)
- Signal — Double RatchetAt every DH ratchet step, the new root key and chain key come from HKDF: the current root key is the salt, the DH output the input keying material, and info must differ from other uses of HKDF in the application.Source (Signal — Double Ratchet)
- WireGuardThe WireGuard handshake uses HKDF over HMAC-BLAKE2s instead of HMAC-SHA-256. Every Curve25519 DH result enters Extract, and the two transport keys — for sending and for receiving — are T(1) and T(2) of a single Expand with an empty info.Source (WireGuard)
- HPKEThe hybrid encryption of RFC 9180 wraps Extract and Expand in LabeledExtract and LabeledExpand: it puts “HPKE-v1”, the ciphersuite identifier and a label (e.g. “eae_prk”, “shared_secret”) in front of IKM and info, so KDF calls in different places of the protocol never yield the same key.Source (HPKE)
HKDF from the platform, a separate info for every purpose
Derive keys from high-entropy secrets — an ECDH or KEM output, a master key, a PRF output — with the platform’s HKDF:
in the browser WebCrypto’s deriveKey or deriveBits, in Node crypto.hkdf or hkdfSync, in .NET the HKDF
class (DeriveKey, Extract, Expand). Give every purpose its own info: direction, use, algorithm, protocol
version — like the labels in TLS 1.3. If you have a salt — random or from the protocol’s authenticated nonces —
pass it in (RFC 5869, §3.1). Make a WebCrypto key a
non-extractable CryptoKey straight away (the WebCrypto station). This is how this site
derives, from the passkey PRF output, the key that wraps the key encrypting your progress (the
Your account station).
const ikm = await crypto.subtle.importKey('raw', shared, 'HKDF', false, ['deriveKey']);
const derive = (info) => crypto.subtle.deriveKey(
{ name: 'HKDF', hash: 'SHA-256', salt, info: new TextEncoder().encode(info) },
ikm,
{ name: 'AES-GCM', length: 256 },
false,
['encrypt', 'decrypt'],
);
// a separate key for each direction
const clientToServer = await derive('myapp v1 client->server');
const serverToClient = await derive('myapp v1 server->client');// .NET: the same function, info as bytes
byte[] clientToServer = HKDF.DeriveKey(
HashAlgorithmName.SHA256, shared, 32, salt, "myapp v1 client->server"u8.ToArray());Do not derive a key from a password with HKDF: a password has too little entropy, and HKDF does not slow down guessing — passwords need Argon2id (Password hashing). Do not use one key for two purposes, such as encryption and a MAC, or both directions — derive two keys with different info. Do not cut keys straight out of a raw DH secret, and do not skip Extract for it (RFC 5869, §3.3). Do not let an attacker choose the salt (§3.4). Do not write your own Expand or a “KDF” of the form SHA-256(secret ‖ label): a wrong order of T, info and the counter still gives “random” bytes, just not the ones the other side computes, and a bare hash with the secret in front is open to length extension (the MAC station).
// ✕ HKDF on a password: fast, so a dictionary takes milliseconds
const base = await crypto.subtle.importKey(
'raw', new TextEncoder().encode(password), 'HKDF', false, ['deriveKey']);
// ✕ keys cut from the raw DH secret: no Extract, no info
const encKey = shared.slice(0, 16);
const macKey = shared.slice(16, 32);