Speed

HKDF Splits One Secret

HKDF extracts a pseudorandom key from a secret and expands it into keys named by an info label; run the command on the test case of RFC 5869, then compute the first key of the expansion yourself from the two HMAC steps.

The question

A protocol has one good shared secret and needs several keys from it: one to encrypt, one to authenticate, one for each direction. Using the secret itself for all of them would tie the keys together. Can one secret be split into keys that are independent of each other?

HKDF, defined in RFC 5869, does it with two steps of HMAC. It is not a way to turn a password into a key: that is a different problem, and the page comes back to it below.

Work it by hand

RFC 5869 section 2.2 defines the extract step, PRK = HMAC-Hash(salt, IKM): the salt is the HMAC key and the input keying material IKM is the message. Section 2.3 defines the expand step: T(1) = HMAC-Hash(PRK, T(0) | info | 0x01), where T(0) is empty, T(2) = HMAC-Hash(PRK, T(1) | info | 0x02), and so on; the output is the first L octets of T(1) | T(2) | ...

Section 3.2 explains the info input: it binds the derived key to a context, so that the same IKM gives different keys for different contexts. In RFC 5869 appendix A.1 the first test case has IKM of 22 bytes 0b, a salt of 13 bytes, a 10-byte info and L = 42. Its PRK is 07770936 2c2e32df 0ddc3f0d c47bba63 90b6c73b b50f9c31 22ec844a d7c2b3e5.

Run it

The command below runs HKDF in one call with the inputs of that test case, with extract and expand together, and writes 42 bytes to out.bin. The first 32 bytes are T(1) and the last 10 are the start of T(2). The inputs are literals of the command, so this page fixes them; they are test material.

The 42 bytes that HKDF wrote to out.bin, starting 3cb25f25 faacd57a 90434f64 d0362f2a and ending 34007208 d5b88718 5865, and their count, 42.

Public test material, generated for this lesson. Never use it for anything real.

# HKDF with the inputs of the test case of RFC 5869 appendix A.1.
# The 42 bytes go to out.bin.
openssl kdf -keylen 42 -kdfopt digest:SHA256 -kdfopt hexkey:0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b -kdfopt hexsalt:000102030405060708090a0b0c -kdfopt hexinfo:f0f1f2f3f4f5f6f7f8f9 -kdfopt mode:EXTRACT_AND_EXPAND -binary -out out.bin HKDF
{
  "answer": "3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865",
  "file": "out.bin",
  "bytes": 42
}

You can change -keylen from 1 to 8160, which is 255 times 32, the largest L of RFC 5869 section 2.3 for SHA-256, and the hex values. A -keylen of 8161 stops with EVP_KDF_derive failed and a -keylen of 0 with Invalid derived key length.

Your turn

The program below is meant to return T(1), the first 32 bytes of the output keying material, as lowercase hex, for HKDF with HMAC-SHA-256. The hex texts ikm, salt and info are the inputs of RFC 5869 sections 2.2 and 2.3; the first step uses the salt as key, the second uses the PRK. Read how it works and judge each result. Check runs it on the data shown and on four others.

The input keying material 0b0b0b0b0b0b0b0b0b0b, the salt 000102, the info 6b657931, and the 64-digit hex text that the shown program returns, which starts 4cf7532b.

Public test material, generated for this lesson. Never use it for anything real.

let counter = "01";
let msg = ssl.unhex(info + counter);
let answer = ssl.hmac("SHA256", ssl.unhex(ikm), msg);
{
  "answer": "4cf7532b305d640b2b64c7ddd4963bb529c6ca7c9d12afb038bf854bb2f3cf60"
}

ikm, salt and info are hex texts (any of them may be empty). Return, as lowercase hex text, the first 32 bytes of the HKDF output of RFC 5869 with HMAC-SHA-256: PRK = HMAC(salt, ikm) with the salt as key, and T(1) = HMAC(PRK, info followed by the byte 01) with the PRK as key. Check runs your program with other inputs, including an empty salt and an empty info.

Read the result carefully

Different info labels give different keys from one secret. RFC 5869 section 4 says that HKDF's extract step "can concentrate existing entropy but cannot amplify entropy", and that deriving keys from a source of low entropy such as a user's password is an application that cannot use HKDF as it is. HKDF has no iteration count, so a password put through it is not slowed down by the function. The page does not run that.

What a derived key is then used for is the subject of two other books: AES Modes of Operation encrypts with a key and an IV, and Encrypt and Authenticate adds the tag that a mode alone does not have.

Recap

  • HKDF extracts a pseudorandom key, then expands it with an info label into keys.
  • Different info labels give independent-looking keys from one secret.
  • The expand step turns the PRK and an info label into one key per label.
  • HKDF is not a password function: it has no cost parameter.