Speed

Iterations Cost the Guesser

PBKDF2 repeats its HMAC c times for every block of key it produces, so one guess costs c times the number of blocks; count the HMAC computations for a dictionary of guesses with toy numbers.

The question

A guesser who wants to test a password against a PBKDF2 key has to run the whole derivation for each guess. How many HMAC computations is one derivation, and what changes that number?

The page counts: it does not time anything. The counts it uses, 100 iterations and a few dozen bytes, are toys chosen so that the arithmetic is easy to check.

Work it by hand

RFC 8018 section 5.2 defines PBKDF2 for a password P, a salt S, an iteration count c and a key length dkLen in bytes. Step 2 sets l = CEIL(dkLen / hLen), where hLen is the length in bytes of the HMAC output, 32 for SHA-256. Every block T_i is F(P, S, c, i) = U_1 xor U_2 xor ... xor U_c, and each U is one HMAC computation: U_1 = PRF(P, S || INT(i)), U_2 = PRF(P, U_1), and so on.

So a key costs c times l HMAC computations. For dkLen 48 and SHA-256, l = CEIL(48 / 32) = 2. With c = 100, that is 200 computations for one guess, and 200 guesses cost 40000.

Run it

The program takes a number of guesses, an iteration count and a key length, and counts the HMAC computations of PBKDF2 with HMAC-SHA-256, where hLen is 32. Line 1 rounds the length up to whole blocks, line 2 multiplies by the iteration count, and line 3 returns the total for all guesses.

200 guesses, 100 iterations and a key of 48 bytes: 2 blocks, 200 HMAC computations for one guess and 40000 for all guesses.
let blocks = (size + 31) / 32;
let per = rounds * blocks;
let answer = guesses * per;
lmcode_return_put("blocks", blocks);
lmcode_return_put("per", per);
{
  "answer": 40000,
  "blocks": 2,
  "per": 200
}

You can change the three numbers: whole numbers of 0 or more. A key length of 0 needs 0 blocks and costs 0. A total of 2^53 or more stops the run with lmcode return: invalid JSON value at /answer: unsafe_integer; when the count for one guess alone reaches 2^53, the message starts Line 5: and names /per.

Your turn

The program below is meant to return the number of HMAC computations of one PBKDF2 call, from the iteration count iters, the key length dklen in bytes and the name digest of the hash, which is SHA1 with 20 bytes, SHA256 with 32 or SHA512 with 64. Read how it counts and judge each result. Check runs it on the data shown and on five others.

The hash SHA1, 500 iterations and a key of 50 bytes, and the count that the shown program returns, 1000.
let hlen = 32;
if (digest == "SHA1") { hlen = 20; }
if (digest == "SHA512") { hlen = 64; }
let blocks = dklen / hlen;
let answer = iters * blocks;
{
  "answer": 1000
}

digest is SHA1, SHA256 or SHA512 and PBKDF2 uses HMAC with it; the HMAC output has 20, 32 or 64 bytes. iters is the iteration count c and dklen the key length in bytes, 1 or more. PBKDF2 (RFC 8018 section 5.2) computes the key in blocks of the HMAC output length and the last block may be cut short. Return, as an int, how many HMAC computations one PBKDF2 call makes. Check runs your program with other settings.

Read the result carefully

RFC 8018 section 4.2, dated January 2017, quotes NIST SP 800-132 on the iteration count: "The iteration count shall be selected as large as possible, as long as the time required to generate the key using the entered password is acceptable for the users. [...] A minimum iteration count of 1,000 is recommended." The page quotes it and gives no advice of its own.

The counts here are toys for arithmetic. The page does not time a derivation, does not say how many guesses a real attacker can make and does not say which count to choose.

Recap

  • One PBKDF2 block costs c HMAC computations.
  • The number of blocks is the key length divided by the hash length, rounded up.
  • The hash length hLen, 20, 32 or 64 bytes here, is the size of one block.
  • The cost of one guess is c times the number of blocks.