Definition
Extraction and expansion are the two steps of HKDF §5.21, a key derivation function §5.20: an algorithm §14.1 that turns one secret value into cryptographic keys. Extraction turns a secret input that is hard to guess but not uniformly random (not every value equally likely), such as the shared secret of a Diffie–Hellman key agreement §19.1.4, into one short key that looks uniformly random. Expansion turns that key into as many bytes as requested, tied to a stated purpose. Extraction concentrates unpredictability the input already has, expansion lengthens the output, and neither adds unpredictability.
Where it fits
The HKDF entry gives the full algorithm. Extraction computes one HMAC §5.8 (hash-based message authentication code) over the secret input, which RFC 5869 calls the input keying material (IKM), keyed with a public, preferably random salt §5.19. With the hash function SHA-256 §5.2 (Secure Hash Algorithm with a 256-bit output), the result is a 32-byte pseudorandom key (PRK). Expansion computes further HMACs keyed with the PRK over a public purpose label and a block counter until it has enough bytes.
Entropy §5.26 here means unpredictability to the attacker, measured as min-entropy: a secret has bits of min-entropy when the attacker’s best single guess at it is right with probability at most . Both steps are deterministic and their other inputs are public, so the attacker can run either step on any candidate secret. If the secret’s most likely value has probability , the step’s output for occurs with probability at least , so the attacker’s best guess at the output is right at least that often. No deterministic step raises min-entropy. However long the output of expansion is, it is computed from the 32-byte PRK, so it has at most 256 bits of min-entropy. What the steps add is pseudorandomness: output that no feasible computation can tell apart from uniformly random bytes, as long as the IKM is too unpredictable to guess, the salt is random and the PRK stays secret.
Worked numbers. Let the IKM be one of 4 equally likely values, so its min-entropy is 2 bits: a guess is right with probability . HKDF with SHA-256 turns each value into a key of 32 bytes, which is bits. With the public salt and label fixed, an attacker can list all candidate keys in four HKDF evaluations. An independently verifiable tag can help identify which candidate was used. The output is 256 bits long and has at most 2 bits of min-entropy. It has exactly 2 if the four outputs are distinct. If two inputs produce the same key, that key is more likely and the min-entropy is lower. If the IKM is a public device identifier, its min-entropy is 0 bits, and one computation gives the attacker the key.
Limits and common mistakes
Judge a derived key by the min-entropy of its secret input, never by the length or look of the output. The same bound holds for a plain hash of the secret or one HMAC over it.
HKDF is fast, so it does not slow an attacker who tries candidate passwords. Passwords need password hashing §15.10: a deliberately slow function, such as the memory-hard functions scrypt and Argon2.
The salt and the purpose label are public and add no secrecy. The label keeps keys for different purposes apart (domain separation §5.22); it makes none of them stronger.
Questions answered
Can extraction create entropy?
No. Extraction, the first step of HKDF (the HMAC-based Extract-and-Expand Key Derivation Function), is a deterministic computation on the secret input and a public salt, so its output is never harder to guess than its input: an attacker who guesses the input and runs extraction on the guess is right about the output at least as often. Entropy, here the attacker’s uncertainty about the secret, can stay the same or fall, never rise.
Extraction concentrates the unpredictability that the input already has. From a secret that is hard to guess but not uniformly random, such as the shared secret of a Diffie–Hellman key agreement, it makes a short key that no feasible computation can tell apart from uniformly random bytes, provided the input is too unpredictable to guess and the salt is random. An input with 4 equally likely values gives at most 4 possible keys, each 256 bits long.
Related entries: Entropy and min-entropy (§5.26), Password hashing and memory-hard KDF (§15.10), Key derivation function (KDF) (§5.20). Further reading: [9].