Definition
A key derivation function (KDF) is an algorithm §14.1 that computes one or more cryptographic keys from a secret input and non-secret context information. The secret input may be a key, a shared secret that two parties computed together, or a password. The context, such as a label naming each key’s purpose, makes keys for different purposes differ. Equal inputs always give equal keys, so two parties holding the same secret derive the same keys without sending them. KDFs differ in which secret inputs they accept.
The idea
A phone and a sensor finish a Diffie–Hellman key agreement §19.1.4, in which each combines its own private number with the other’s public number, and both now hold the same 32-byte shared secret. They need four keys: one to encrypt and one to authenticate in each direction. The shared secret is not fit to use as a key directly. Its bits are not uniformly random, its length is set by the mathematics rather than by the cipher, and one value used for four jobs would let a weakness in one job spill into the others.
A KDF is the standard repair: secret in, labeled keys out. A useful picture is a factory with a strict rule about its raw material. KDFs come in three kinds of factory.
Expansion takes a secret that is already a good key, meaning that nobody without it can tell it apart from uniformly random bytes, and turns it into as many keys as needed, one per label.
Extraction followed by expansion first condenses a secret that is hard to guess but not uniformly random, such as the Diffie–Hellman value, into one good key, and then expands that key.
Password-based derivation accepts a secret that is easy to guess, a password, and makes each guess expensive.
Feeding a factory the wrong raw material produces keys that look fine and are not. The picture stops at the output: a KDF gives the same keys every time it gets the same inputs.
How it works
Most of the standard KDFs below are built from a pseudorandom function §15.1 (PRF), a keyed function whose outputs cannot be told apart from random values by anyone without the key. The usual choice is HMAC §5.8 (hash-based message authentication code), a keyed hash function.
Expansion. NIST Special Publication (SP) 800-108r1 defines KDFs that take a key-derivation key and fixed input data and call the PRF as many times as the output length needs [306]. In its counter mode §15.5, output block is where is the block number written as a bit string of fixed length, identifies the purpose of the derived key, the zero byte 0x00 separates the label from , which holds information such as the identities of the parties, encodes the requested output length in bits, and joins strings end to end. The blocks are joined and cut to bits. The Expand step of HKDF §5.21 (HMAC-based Extract-and-Expand Key Derivation Function) does the same job with a different way of chaining the blocks.
Extraction. When the secret is not a uniformly random key, a first step condenses it into one. The Extract step of HKDF computes , where IKM, input keying material, is the secret, the salt §5.19 is a non-secret and preferably random value, and the result PRK, the pseudorandom key, is the key that Expand then uses. NIST SP 800-56C Rev. 2 calls this two-step method randomness extraction followed by key expansion [323], and uses the SP 800-108r1 functions for the expansion. SP 800-56C also defines one-step KDFs, which apply a hash function or a keyed function such as HMAC to a counter, the shared secret of a key agreement, and fixed information, with no separate extraction step.
Password-based derivation. PBKDF2 (Password-Based Key Derivation Function 2), which SP 800-132 approves, computes each output block as the XOR (exclusive or) of chained HMAC calls keyed with the password, where is the iteration count; the salt is part of the first message. Each guess by an attacker therefore costs HMAC calls per output block. Argon2, defined in RFC 9106, is a memory-hard function §15.10: each evaluation must also fill a set amount of memory.
Context. Whatever the kind, the non-secret inputs state what each key is for: a label, the identities of the parties, the algorithm the key will serve, and often the output length. RFC 5869, which defines HKDF, names protocol numbers, algorithm identifiers and user identities as typical contents of its info input. Each purpose gets its own context bytes, encoded so that two different contexts never give the same bytes. This is domain separation §5.22.
Worked example
One secret, two keys. Start from the 32-byte PRK of RFC 5869 test case 1, which begins 077709362c2e and is shown in full in the HKDF entry. Run the Expand step of HKDF with SHA-256 §5.2 (Secure Hash Algorithm with a 256-bit output) twice, asking for 16 bytes each time, with the text encryption and then the text authentication as the info label:
info
encryption:f6f274c198f909c7d84cd755655012f6
infoauthentication:8f619d13699ff9998c1154efce4eec60
The two keys come from one secret and show no relation to each other. Repeating a call gives the same key again, which is how two parties derive matching keys. Asking for 32 bytes with the info encryption gives a key whose first 16 bytes are the 16-byte key above, which is why every key, whatever its length, needs its own label.
A KDF does not create unpredictability. Suppose the only secret is a 6-digit PIN (personal identification number), so it has possible values, which is bits. HKDF turns the PIN into a 32-byte key, but an attacker who can test a candidate key, for example on one captured encrypted message, runs HKDF on all PINs. Each run costs 2 HMAC calls, one for Extract and one for the single 32-byte Expand block, so the whole search costs HMAC calls. PBKDF2 with HMAC over SHA-256 and iterations costs HMAC calls per 32-byte key, so the same search costs HMAC calls, which is times more. Both searches have only candidates. PBKDF2 makes each candidate more expensive; it adds no candidates.
Stated precisely
The input contract of each kind, as the standards state it:
Expansion (SP 800-108r1; HKDF Expand used alone): the key-derivation key must be a cryptographic key. SP 800-108r1 assumes that no feasible computation can tell it apart from a string chosen uniformly at random among all strings of its length, and allows any disjoint parts of the output to be used as separate keys.
Extraction (HKDF Extract; the two-step method of SP 800-56C): the secret may be non-uniform and partly known to the attacker, provided that it keeps enough min-entropy §5.26, a measure of how hard it is to guess in one try, given everything the attacker knows. RFC 5869 states that the extract step can concentrate existing entropy but cannot amplify it.
Password-based (PBKDF2, Argon2): the secret may be easy to guess. The function promises only that each evaluation costs a set amount of computation and, for Argon2, of memory.
One limit binds every deterministic KDF . Fix the non-secret inputs, and let the secret take, as far as the attacker knows, at most values. Then takes at most values, because each value of gives exactly one output. More generally, the most likely output is at least as likely as the most likely input, so the min-entropy of the output, of the probability of its most likely value, is at most the min-entropy of the input. The output length plays no part: a 32-byte key derived from a 6-digit PIN has at most possible values.
Limits and common mistakes
Fast KDFs such as HKDF and the SP 800-108r1 functions must not turn a password into a key. RFC 5869 states that slowing down guesses is not part of HKDF and advises applications that start from a password to consider a password-based KDF instead.
Skipping extraction for a secret that is not uniformly random is a misuse. RFC 5869 says that the extract step should not be skipped when the input is a Diffie–Hellman value, because that value is not a uniformly random or pseudorandom string.
A hash of the secret and the context fields joined together is not automatically a sound KDF. Joining fields without lengths or separators is ambiguous: the label ab followed by the context c and the label a followed by the context bc give the same bytes, and therefore the same key. Standard KDFs fix the encoding of their inputs.
A KDF does not repair a compromised secret. Whoever learns the input secret recomputes every derived key. The derived keys are separate only against an attacker who lacks that secret, and that separation rests on the PRF assumption.
Equal inputs give equal keys. Two purposes that use the same label get the same key, and with HKDF a shorter output is the beginning of a longer one under the same label. Key separation §5.23 therefore depends on distinct labels.
The context protects only what it contains. If the identity of the receiving device is not in the context, two devices that share a root secret derive the same key.
Questions answered
Does the KDF require uniformly random source material or support extraction?
It depends on the kind of KDF (key derivation function), and the answer must be read from that function’s specification: an expansion-only KDF requires a key that is uniformly random or cannot be told apart from one, while an extract-then-expand KDF supports extraction from a secret that is hard to guess but not uniformly random.
Expansion only, such as the functions of NIST Special Publication (SP) 800-108r1 or the Expand step of HKDF used alone: the input must already be a cryptographic key, for example the output of a cryptographically secure random generator or of an earlier derivation. SP 800-108r1 assumes that the input cannot be told apart from a uniformly random string of its length.
Extraction supported, such as full HKDF (HMAC-based Extract-and-Expand Key Derivation Function) or the two-step method of NIST SP 800-56C: the input may be a secret that is not uniformly random, such as a Diffie–Hellman shared secret, provided it has enough min-entropy, meaning it is hard to guess in one try given what the attacker knows. Extraction condenses that unpredictability into a short key; it cannot add any.
Password-based, such as PBKDF2 or Argon2: the input may be a guessable password; the function adds cost to each guess, not unpredictability.
Two consequences follow. Feeding a Diffie–Hellman value or another structured secret into an expansion-only interface is a misuse, even though it produces output. And no KDF turns a secret with possible values into a key with more than possible values, whatever the length of the key.
Related entries: HKDF (§5.21), Domain separation (§5.22), Entropy and min-entropy (§5.26), Salt (§5.19), Key separation (§5.23), Pseudorandom function (PRF) (§15.1), Password hashing and memory-hard KDF (§15.10), HKDF extraction versus expansion (§19.1.7), Diffie–Hellman key agreement (§19.1.4). Further reading: [9, 138, 306, 323].