Full name: HMAC-based Extract-and-Expand Key Derivation Function.
Definition
HKDF is a key derivation function §5.20: an algorithm §14.1 that turns one secret value into one or more cryptographic keys of the lengths an application requests. HKDF is built entirely from HMAC §5.8 (hash-based message authentication code), a function that computes a fixed-length value from a key and a message; the H in HKDF stands for HMAC. HKDF has two steps. Extract turns the secret input into one fixed-length key. Expand turns that key into the requested number of output bytes and ties them to a stated purpose. RFC 5869 defines HKDF.
The idea
A secret value rarely arrives in the form that a cipher (an encryption algorithm) needs: a key of a fixed length that is uniformly random, meaning that every value of that length is equally likely. Take a Diffie–Hellman key agreement §19.1.4, in which two devices each combine their own private number with the other’s public number and arrive at the same shared secret. That shared secret is a large number with mathematical structure. Its bits are not uniformly random, and its length is fixed by the mathematics, not by the cipher. The two devices also usually need several keys from that one secret: one to encrypt messages, one to protect them against changes, often a separate pair for each direction.
A home-made fix is to run the secret through a cryptographic hash function §5.1, which turns input of any length into a short fixed-length value, and to cut the result into pieces. That mixes two different jobs, repairing an imperfect secret and making several keys from one, and it gives no way to record which piece is for which purpose. HKDF separates the jobs. Extract turns imperfect secret input into one good key. Expand turns one good key into many keys that look unrelated to each other, each labeled with its purpose. Both steps are plain HMAC calls, so a device that already has HMAC needs no other cryptographic building block.
How it works
RFC 5869 gives each value a short name, and other documents reuse these names:
: the output length, in bytes, of the hash function inside HMAC. For SHA-256 §5.2 (Secure Hash Algorithm with a 256-bit output) it is .
IKM, input keying material: the secret value that HKDF starts from, for example a Diffie–Hellman shared secret.
salt §5.19: an optional value that need not be secret, should be random and may be reused. A random salt strengthens Extract. If no salt is supplied, HKDF uses zero bytes.
PRK, pseudorandom key: the output of Extract. Pseudorandom means that someone who does not know the IKM cannot tell the key apart from uniformly random bytes.
info: an optional label that need not be secret and states what the output is for, such as the text
encrypt, client to server. If no label is supplied, info is empty.: the number of output bytes requested.
OKM, output keying material: the bytes that HKDF returns.
Step 1, Extract. Compute one HMAC with the salt as the HMAC key and the IKM as the HMAC message: The PRK is bytes long, whatever the length of the IKM. The salt sits in the key position although it is not secret. RFC 5869 stresses this order: the IKM is the HMAC message, not the HMAC key.
Step 2, Expand. Produce the output in blocks of bytes, as many blocks as it takes to reach bytes. Each block is one HMAC call that uses the PRK as its key. The message for a block is the previous block, then the info label, then one byte that holds the number of the block, counting from 1. The first block has no previous block, so its message is the info label followed by the byte 0x01, the value 1 in hexadecimal notation. Join the blocks end to end and keep the first bytes. Those bytes are the OKM.
To derive several keys from one secret, run Extract once, then run Expand once for each key, each time with a different info label. Outputs made with different labels look unrelated. Someone who learns one derived key learns nothing useful about another, as long as the PRK stays secret and HMAC behaves as a pseudorandom function §15.1, meaning a keyed function whose outputs look random to anyone who does not hold the key. HKDF uses no randomness of its own: the same salt, IKM, info label and always give the same OKM, so two devices derive the same keys when they agree on all four.
Worked example
This is test case 1 of RFC 5869. The hash function is SHA-256, so .
IKM: 22 bytes, each with the value
0x0b.salt: the 13 bytes
0x00,0x01, and so on up to0x0c.info: the 10 bytes
0xf0,0xf1, and so on up to0xf9.bytes.
Extract computes HMAC with the salt as key and the IKM as message. The result is the 32-byte PRK, written here in hexadecimal:
077709362c2e32df0ddc3f0dc47bba6390b6c73bb50f9c3122ec844ad7c2b3e5
Expand must produce 42 bytes. One block supplies 32 bytes, which leaves bytes, so two blocks are needed. The first block is the HMAC, keyed with the PRK, of the 10 info bytes followed by the byte 0x01, a message of bytes. The second block is the HMAC, again keyed with the PRK, of , then the info bytes, then the byte 0x02, a message of bytes:
=
3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf
=34007208d5b887185865b4b0a85a993b89b9b65683d60f0106d28fff039d0b6f
The OKM is all 32 bytes of followed by the first 10 bytes of , which is bytes:
3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf
34007208d5b887185865
The remaining bytes of are thrown away. These 42 bytes equal the OKM printed in RFC 5869, which confirms the computation.
Stated precisely
Let be the output length in bytes of the hash function inside HMAC, and let be the requested output length in bytes. Let be the number of blocks, where means rounded up to a whole number. Write for HMAC with key and message , write for the single byte with value , and write for the byte string followed by the byte string . Then
The block number is stored in one byte, so is at most 255 and is at most . With SHA-256 that is bytes for one Expand call.
The security claim rests on three assumptions.
An unpredictable IKM. Given everything else the attacker knows, the attacker’s best single guess at the IKM is right with probability at most . The IKM is then said to have bits of min-entropy §5.26; the larger is, the stronger the claim.
HMAC as a randomness extractor. Keyed with a salt that is random, independent of the IKM and not chosen by the attacker, HMAC turns such an IKM into a PRK that no feasible computation can tell apart from a uniformly random key, even when the salt is known.
HMAC as a pseudorandom function. Keyed with a secret, uniformly random key, HMAC gives outputs that no feasible computation can tell apart from random values, even for messages that the attacker chooses.
If all three hold, each OKM cannot be told apart from random bytes, even by an attacker who knows the salt, chooses the info labels and has seen the OKMs for other labels. The third assumption cannot replace the second: a pseudorandom function promises nothing once its key is known, and the salt is not secret. Without a salt, Extract is one fixed function, and the claim covers only IKM sources that are independent of the hash function. RFC 5869 cites the paper that states this claim as a theorem: Krawczyk, “Cryptographic Extraction and Key Derivation: The HKDF Scheme” (2010).
Limits and common mistakes
HKDF does not create unpredictability. If the IKM can take only possible values, an attacker runs HKDF on all of them and tests each result against data that the real keys protected, however long the output is. Nor is any output harder to guess than the -byte PRK.
HKDF is fast: it costs a few HMAC calls, so an attacker can test guesses cheaply. It must therefore not be used to turn a password into a key. A password needs a deliberately slow function, such as PBKDF2 (Password-Based Key Derivation Function 2), scrypt or Argon2, that makes every guess expensive.
The salt and the info label add no secrecy. The job of the info label is domain separation §5.22: it keeps keys for different purposes apart. Two purposes that produce the same label bytes get the same key, so labels must be encoded without ambiguity. RFC 5869 adds one requirement: the label must be independent of the IKM.
With the same PRK and the same info label, a shorter output is the beginning of a longer one. A 16-byte key and a 32-byte key requested under one label share their first 16 bytes. Give every key its own label.
RFC 5869 advises against using the PRK directly as the output key, even when is no larger than , because that skips the info label.
Everything derived from one IKM stands or falls together. Whoever learns the IKM or the PRK can recompute every output.
Questions answered
When may the extract step be omitted, and what assumptions justify that?
The Extract step of HKDF may be omitted only when the input is already a uniformly random or pseudorandom secret key at least as long as the hash output ( bytes, which is 32 bytes for SHA-256). That input is then used directly as the pseudorandom key (PRK) of the Expand step, which produces the output keys. Examples are a 32-byte key produced by a cryptographic random number generator, or a key that an earlier key derivation step already produced. Two assumptions justify the omission. The first is that an attacker cannot tell the input key apart from uniformly random bytes, meaning bytes in which every value is equally likely. The security argument for Expand needs nothing else from its key, and Extract exists to produce such a key from input that does not have that property. The second is that HMAC behaves as a pseudorandom function (with a secret key, its outputs look random), which Expand needs with or without Extract.
Extract must not be omitted for the shared secret of a Diffie–Hellman key agreement, or for any other input that is secret but not uniformly random. Section 3.3 of RFC 5869 names the Diffie–Hellman value as the case where Extract should not be skipped. Keeping Extract costs one HMAC call and is the general case that the security argument covers, so keep it when in doubt.
Related entries: Key derivation function (KDF) (§5.20), HMAC (§5.8), Salt (§5.19), Domain separation (§5.22), Entropy and min-entropy (§5.26), Password hashing and memory-hard KDF (§15.10). Further reading: [9, 46, 221].