Section 5.21

HKDF

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:

Step 1, Extract. Compute one HMAC with the salt as the HMAC key and the IKM as the HMAC message: PRK=HMAC(key=salt,message=IKM).\mathrm{PRK} = \mathrm{HMAC}(\text{key} = \mathrm{salt},\ \text{message} = \mathrm{IKM}). The PRK is HashLen\mathrm{HashLen} 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 HashLen\mathrm{HashLen} bytes, as many blocks as it takes to reach LL 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 LL 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 LL 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 HashLen=32\mathrm{HashLen} = 32.

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 42−32=1042 - 32 = 10 bytes, so two blocks are needed. The first block T1T_1 is the HMAC, keyed with the PRK, of the 10 info bytes followed by the byte 0x01, a message of 10+1=1110 + 1 = 11 bytes. The second block T2T_2 is the HMAC, again keyed with the PRK, of T1T_1, then the info bytes, then the byte 0x02, a message of 32+10+1=4332 + 10 + 1 = 43 bytes:

T1T_1 = 3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf
T2T_2 = 34007208d5b887185865b4b0a85a993b89b9b65683d60f0106d28fff039d0b6f

The OKM is all 32 bytes of T1T_1 followed by the first 10 bytes of T2T_2, which is 32+10=4232 + 10 = 42 bytes:

3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf
34007208d5b887185865

The remaining 32−10=2232 - 10 = 22 bytes of T2T_2 are thrown away. These 42 bytes equal the OKM printed in RFC 5869, which confirms the computation.

Stated precisely

Let HashLen\mathrm{HashLen} be the output length in bytes of the hash function inside HMAC, and let LL be the requested output length in bytes. Let N=⌈L/HashLen⌉N = \lceil L / \mathrm{HashLen} \rceil be the number of blocks, where ⌈x⌉\lceil x \rceil means xx rounded up to a whole number. Write HMAC(k,m)\mathrm{HMAC}(k, m) for HMAC with key kk and message mm, write byte(i)\mathrm{byte}(i) for the single byte with value ii, and write a‖ba \mathbin{\Vert} b for the byte string aa followed by the byte string bb. Then PRK=HMAC(salt,IKM),T0=the empty string,Ti=HMAC(PRK,Ti−1‖info‖byte(i))for 1≤i≤N,OKM=the first L bytes of T1‖T2‖…‖TN.\begin{aligned} \mathrm{PRK} &= \mathrm{HMAC}(\mathrm{salt},\ \mathrm{IKM}), \\ T_0 &= \text{the empty string}, \\ T_i &= \mathrm{HMAC}(\mathrm{PRK},\ T_{i-1} \mathbin{\Vert} \mathrm{info} \mathbin{\Vert} \mathrm{byte}(i)) \quad \text{for } 1 \le i \le N, \\ \mathrm{OKM} &= \text{the first } L \text{ bytes of } T_1 \mathbin{\Vert} T_2 \mathbin{\Vert} \dots \mathbin{\Vert} T_N. \end{aligned}

The block number is stored in one byte, so NN is at most 255 and LL is at most 255×HashLen255 \times \mathrm{HashLen}. With SHA-256 that is 255×32=8160255 \times 32 = 8160 bytes for one Expand call.

The security claim rests on three assumptions.

  1. An unpredictable IKM. Given everything else the attacker knows, the attacker’s best single guess at the IKM is right with probability at most 2−m2^{-m}. The IKM is then said to have mm bits of min-entropy §5.26; the larger mm is, the stronger the claim.

  2. 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.

  3. 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 2202^{20} possible values, an attacker runs HKDF on all 2202^{20} 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 HashLen\mathrm{HashLen}-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 LL is no larger than HashLen\mathrm{HashLen}, 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 (HashLen\mathrm{HashLen} 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].