Key Derivation Functions
Cryptographic Protocols · Networking & Cryptography track
6 min read
In this module
From Shared Secrets to Usable Keys
When two parties finish a Diffie-Hellman exchange, they share a secret. That raw shared secret isn't fit to use directly as an encryption key. It may have mathematical structure, biased bits, or simply be the wrong size. A Key Derivation Function (KDF) turns raw key material into one or more cryptographically strong keys.
Why Not Use the Raw Secret?
The shared secret from Diffie-Hellman is a point on an elliptic curve or a number from modular exponentiation. It has structure. Some bits may be predictable, and the distribution isn't uniform. Feeding it straight into AES could weaken the encryption.
A KDF applies a hash-like transformation that:
- Produces output computationally indistinguishable from random
- Can spit out multiple independent keys from one input
- Strips any structure or bias from the input material
HKDF: The Standard KDF
HKDF (HMAC-based Key Derivation Function) is the most widely used KDF for protocol key derivation. It has two stages:
Extract: Condense the input key material into a fixed-size pseudorandom key.
prk = HMAC-Hash(salt, input_key_material)Expand: Produce as many output bytes as needed from the PRK.
output = HMAC-Hash(prk, info || 0x01) || HMAC-Hash(prk, output_block_1 || info || 0x02) || ...The info parameter lets you derive different keys for different purposes from the same input.
HKDF extracts entropy from raw key material (like a DH shared secret) and expands it into multiple independent keys. The extract step removes bias; the expand step produces as many bytes as needed. TLS 1.3 uses HKDF throughout its key schedule.
Deriving Multiple Keys
A single TLS 1.3 connection needs separate keys for each direction and each phase of the conversation. Because TLS 1.3 only uses AEAD ciphers, there are no separate MAC keys: each direction needs just an encryption key and an IV. HKDF mints them all from secrets that trace back to the one Diffie-Hellman result, varying only the label (RFC 8446, section 7):
client_hs_secret = Derive-Secret(handshake_secret, "c hs traffic", transcript)server_hs_secret = Derive-Secret(handshake_secret, "s hs traffic", transcript)
client_write_key = HKDF-Expand-Label(client_hs_secret, "key", "", 16)client_write_iv = HKDF-Expand-Label(client_hs_secret, "iv", "", 12)server_write_key = HKDF-Expand-Label(server_hs_secret, "key", "", 16)server_write_iv = HKDF-Expand-Label(server_hs_secret, "iv", "", 12)Derive-Secret is HKDF-Expand-Label with a hash of the handshake messages so far as its context, so the secrets are tied to this exact conversation. The key length of 16 bytes is for AES-128-GCM. The application-data keys come out the same way, one stage further down the schedule.
Each derived key is cryptographically independent. Knowing one reveals nothing about the others.
Password-Based KDFs
When the input is a password (low entropy, short, predictable), standard KDFs aren't enough. An attacker who knows the salt and parameters could rip through billions of passwords per second. Password-based KDFs are deliberately slow to make brute-force attacks impractical.
PBKDF2 (Password-Based Key Derivation Function 2): Applies HMAC repeatedly (thousands or millions of iterations) to make every password guess expensive.
key = PBKDF2(password, salt, iterations=600000, output_length=32)Every guess costs 600,000 HMAC computations. A single high-end GPU can compute billions of SHA-256 HMACs per second, so even that attacker is cut down to something in the order of ten thousand guesses a second (the exact figure depends on the hardware). That's slow enough to protect a strong password and fast enough to find a weak one, which is why the memory-hard functions below exist.
bcrypt: Built specifically for password hashing. Uses a cost factor that doubles the work with each increment, has a built-in salt, and produces a standard format string.
scrypt: Memory-hard by design. Needs serious RAM in addition to CPU time, which makes it nasty for GPU and ASIC attackers, since those chips have limited memory per core.
Argon2: Winner of the 2015 Password Hashing Competition. Memory-hard, time-hard, and supports parallelism tuning. Argon2id (the hybrid variant) is the recommended choice for new systems.
The Salt
A salt is a random value mixed into the KDF input. Every user (or every key derivation) gets a unique salt. Without salting:
- All users with the same password end up with the same derived key
- Attackers can precompute tables (rainbow tables) mapping passwords to keys
- One cracked password reveals every account that picked the same one
With a unique salt per user, each password hashes to a different value even when two users pick the same one. The salt sits alongside the hash. It isn't secret.
Key Stretching vs. Key Derivation
The terms are related but distinct:
Key derivation (HKDF): Takes high-entropy input (like a DH secret) and produces properly formatted keys. Fast, because the input is already strong.
Key stretching (PBKDF2, Argon2): Takes low-entropy input (like a password) and produces a key while deliberately throttling the computation. Slow by design, because the input is weak and has to resist brute force.
KDFs in the TLS Key Schedule
TLS 1.3's key schedule is built entirely on HKDF. Starting from the Diffie-Hellman shared secret, HKDF derives:
- Handshake keys — encrypt the handshake messages after the key exchange
- Application keys — encrypt the actual application data
- Resumption keys — enable 0-RTT resumption for future connections
- Exporter keys — allow deriving additional keys for other protocols
Each derivation step combines the previous secret with new context, creating a cascading schedule where compromising one set of keys reveals nothing about the others. The layering is a masterclass in how primitive cryptographic building blocks compose into protocol-level security that actually survives contact with attackers.
Follow the schedule through a real handshake. The panel lights up each secret as the client and the server become able to compute it.
TLS 1.3 full handshake
A first connection: ECDHE key exchange and a certificate, in one round trip. (RFC 8446 section 2)
- Handshake traffic keys from here
- Application traffic keys from here
Nothing sent yet. Step through to watch what each side learns and when encryption starts.
- Early Secret
- Early Secret
- Early SecretClient knows it.Server knows it.HKDF-Extract(0, 0): no PSK, so a public constant
- Handshake SecretClient doesn't know it.Server doesn't know it.HKDF-Extract(Derive-Secret(Early Secret, "derived", ""), (EC)DHE)
- client_handshake_traffic_secretClient doesn't know it.Server doesn't know it.Derive-Secret(Handshake Secret, "c hs traffic", ClientHello...ServerHello)
- server_handshake_traffic_secretClient doesn't know it.Server doesn't know it.Derive-Secret(Handshake Secret, "s hs traffic", ClientHello...ServerHello)
- Master SecretClient doesn't know it.Server doesn't know it.HKDF-Extract(Derive-Secret(Handshake Secret, "derived", ""), 0)
- client_application_traffic_secret_0Client doesn't know it.Server doesn't know it.Derive-Secret(Master Secret, "c ap traffic", ClientHello...server Finished)
- server_application_traffic_secret_0Client doesn't know it.Server doesn't know it.Derive-Secret(Master Secret, "s ap traffic", ClientHello...server Finished)
- exporter_master_secretClient doesn't know it.Server doesn't know it.Derive-Secret(Master Secret, "exp master", ClientHello...server Finished)
- resumption_master_secretClient doesn't know it.Server doesn't know it.Derive-Secret(Master Secret, "res master", ClientHello...client Finished)