Speed

Key Length and Blocks

PBKDF2 builds a long key from blocks, each computed with its own block number; derive 32 and 64 bytes from one test password, see that the short key is the start of the long one, and take the second block.

The question

Ask PBKDF2 for 32 bytes and then for 64 bytes from the same password, salt and iteration count. Is the 32-byte key the start of the 64-byte key? And if it is, what are the other 32 bytes: a continuation of the first block, or something computed separately?

RFC 8018 section 5.2 answers it: the key is built from blocks of the HMAC output length, and the derived key is the first dkLen bytes of them.

Work it by hand

In RFC 8018 section 5.2 the blocks are T_1 = F(P, S, c, 1), T_2 = F(P, S, c, 2) and so on, and the derived key is the first dkLen octets of T_1 || T_2 || ... Inside F the first HMAC input is S || INT(i), the salt followed by the block number i as four bytes. Block 2 therefore starts from a different message than block 1, and the two blocks are not related by a longer run.

RFC 7914 section 11 lists PBKDF2-HMAC-SHA-256 with the test password passwd, the salt salt and 1 iteration. Its 64 bytes begin 55ac046e 56e3089f ec1691c2 2544b605 f9418521 6dde0465 e68b9d57 c20dacbc, and continue 49ca9ccc f179b645 991664b3 9d77ef31 7c71b845 b1e30bd5 09112041 d3a19783. The first line is the whole 32-byte key.

Run it

The program derives 32 and 64 bytes with the test password passwd, the salt salt and 1 iteration, a toy count, and compares the 32-byte key with the first 32 bytes of the 64-byte key. Line 1 derives 32 bytes, line 2 derives 64, and line 3 compares the first 64 hex digits of the long key with the short key.

The test password passwd, the salt salt and 1 iteration: the key of 32 bytes starts 55ac046e and ends dacbc, the key of 64 bytes starts with the same 32 bytes and continues 49ca9ccc, and the short key is a prefix: true.

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

let short = ssl.pbkdf2(word, nacl, rounds, "SHA256", 32);
let long = ssl.pbkdf2(word, nacl, rounds, "SHA256", 64);
let answer = str_substr(long, 0, 64) == short;
lmcode_return_put("short", short);
lmcode_return_put("long", long);
{
  "answer": true,
  "short": "55ac046e56e3089fec1691c22544b605f94185216dde0465e68b9d57c20dacbc",
  "long": "55ac046e56e3089fec1691c22544b605f94185216dde0465e68b9d57c20dacbc49ca9cccf179b645991664b39d77ef317c71b845b1e30bd509112041d3a19783"
}

You can change the password, the salt and the count: printable ASCII and newlines for the texts, a whole number of 1 or more for the count; 0 stops the run with Line 1: Ssl.pbkdf2: iterations must be >= 1 and length in 1..1024, and a count below 0 gives the same message. An empty password or salt is accepted. The answer stays true for every input, because the two calls share their first block.

Your turn

The program below is meant to return bytes 32 to 63 of the 64-byte PBKDF2-HMAC-SHA-256 key, the second block, as 64 lowercase hex digits, for the password phrase, the salt salt and the iteration count iters. Read how it takes the bytes and judge each result. Check runs it on the data shown and on four others.

The test password maple, the salt leaf, 3 iterations, and the 32-digit hex text that the shown program returns, which starts 728fcb8c.

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

let size = 64;
let wide = ssl.pbkdf2(phrase, salt, iters, "SHA256", size);
let answer = str_substr(wide, 32, 32);
{
  "answer": "728fcb8c225a0898a51d78897202f8b8"
}

phrase is a test password, salt a salt and iters an iteration count of 1 or more. The PBKDF2 key with HMAC-SHA-256 of 64 bytes is the 32-byte block T_1 followed by the 32-byte block T_2. Return, as lowercase hex text of 64 digits, the bytes 32 to 63 of that 64-byte key, counting the bytes from 0. Check runs your program with other passwords, salts and counts, including an empty password.

Read the result carefully

The second block is computed on its own, so knowing the first 32 bytes of a key tells nothing about the next 32 in any way this page shows. The page does not say how many bytes a key should have. It also does not show that a longer key costs a guesser more: that count was the last lesson, and this page measures none of it. The values are test material.

Recap

  • A PBKDF2 key is built from blocks of the HMAC output length.
  • The derived key is the first dkLen bytes of the blocks in order.
  • Asking for more bytes gives the same start and new blocks after it.
  • The second block uses the block number 2, so it is not the first block run longer.