Section 5.20

Key derivation function (KDF)

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.

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 KINK_{\mathrm{IN}} 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 ii is K(i)=PRF(KIN,[i]2‖Label‖𝟶𝚡𝟶𝟶‖Context‖[L]2),K(i) = \mathrm{PRF}\bigl(K_{\mathrm{IN}},\ [i]_2 \mathbin{\Vert} \mathrm{Label} \mathbin{\Vert} \texttt{0x00} \mathbin{\Vert} \mathrm{Context} \mathbin{\Vert} [L]_2\bigr), where [i]2[i]_2 is the block number ii written as a bit string of fixed length, Label\mathrm{Label} identifies the purpose of the derived key, the zero byte 0x00 separates the label from Context\mathrm{Context}, which holds information such as the identities of the parties, [L]2[L]_2 encodes the requested output length LL in bits, and ‖\Vert joins strings end to end. The blocks are joined and cut to LL 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 PRK=HMAC(salt,IKM)\mathrm{PRK} = \mathrm{HMAC}(\mathrm{salt}, \mathrm{IKM}), 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 ZZ 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 CC chained HMAC calls keyed with the password, where CC is the iteration count; the salt is part of the first message. Each guess by an attacker therefore costs CC 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
info authentication: 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 10610^{6} possible values, which is log2106≈19.9\log_2 10^{6} \approx 19.9 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 10610^{6} PINs. Each run costs 2 HMAC calls, one for Extract and one for the single 32-byte Expand block, so the whole search costs 2×106=2,000,0002 \times 10^{6} = 2{,}000{,}000 HMAC calls. PBKDF2 with HMAC over SHA-256 and C=100,000C = 100{,}000 iterations costs 10510^{5} HMAC calls per 32-byte key, so the same search costs 105×106=101110^{5} \times 10^{6} = 10^{11} HMAC calls, which is 1011/(2×106)=50,00010^{11} / (2 \times 10^{6}) = 50{,}000 times more. Both searches have only 10610^{6} candidates. PBKDF2 makes each candidate more expensive; it adds no candidates.

Stated precisely

The input contract of each kind, as the standards state it:

One limit binds every deterministic KDF FF. Fix the non-secret inputs, and let the secret XX take, as far as the attacker knows, at most NN values. Then F(X)F(X) takes at most NN values, because each value of XX 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, −log2-\log_2 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 10610^{6} 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.

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 NN possible values into a key with more than NN 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].