Speed

A Salt Separates the Users

PBKDF2 mixes a salt into the key it derives from a password, so the same password gives unrelated keys under different salts; derive two keys from one test password and compare them byte by byte.

The question

The last lesson showed that a bare digest gives equal digests for equal passwords. Can a key derived from a password be made different for every user who chose it, without asking the users to choose different passwords?

RFC 8018 section 4.1 says what a salt is for. It lists two benefits: it is difficult for an opponent to precompute all the keys, or even the most likely keys, for a dictionary of passwords, and it is unlikely that the same key will be selected twice. The salt is an input of the derivation, next to the password.

Work it by hand

PBKDF2, defined in RFC 8018 section 5.2, takes a password, a salt, an iteration count and a length in bytes. Its pseudorandom function here is HMAC with SHA-256, the subject of HMAC uses two pads; this lesson only calls it.

The passwords and salts on this page are test material. With the password password, the salt NaCl, 1000 iterations and 32 bytes, the key starts 67dbcc78 548a7532 8f689012 5bec2c59 and ends 2a760e2a ec, as printed in the run below. Add one character to the salt, NaCl2, and the key shares nothing visible with it. The salt is not secret: it is stored next to the result, and its job is to make the work depend on the user, not to hide.

Run it

The program derives two keys from the same test password, one under each salt, with 1000 iterations and 32 bytes, a toy count. Line 1 is the first key and line 2 the second. The loop that starts at line 4 compares them byte by byte, two hex digits at a time, and line 3 returns the number of bytes that differ.

The test password password with the salts NaCl and NaCl2; the 32-byte key under NaCl starts 67dbcc78, the key under NaCl2 starts b7affb3b, and the number of bytes that differ is 32.

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

let k1 = ssl.pbkdf2(word, salt_a, 1000, "SHA256", 32);
let k2 = ssl.pbkdf2(word, salt_b, 1000, "SHA256", 32);
let answer = 0;
for (i in range(0, 32)) {
  if (str_substr(k1, 2 * i, 2) != str_substr(k2, 2 * i, 2)) {
    answer = answer + 1;
  }
}
lmcode_return_put("first", k1);
lmcode_return_put("second", k2);
{
  "answer": 32,
  "first": "67dbcc78548a75328f6890125bec2c59876e8de2e4c92c99b201872a760e2aec",
  "second": "b7affb3b2e9e0e745035912280321de9882a6602ffedba47697ecd925b3712ee"
}

You can change the password and both salts: printable ASCII and newlines, up to 2048 bytes. An empty password and an empty salt are both accepted. Equal salts give equal keys and the count 0.

Your turn

The program below is meant to return the PBKDF2-HMAC-SHA-256 key of a password and a salt, with 1000 iterations and 32 bytes, as 64 lowercase hex digits. Read what it returns and judge each result. Check runs it on the data shown and on four others, including an empty password and an empty salt.

The password letmein, the salt pepper1, and the 64-digit hex text that the shown program returns, which starts 95b1695a.

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

let iterations = 1000;
let size = 32;
let answer = ssl.digest("SHA256", salt + phrase);
{
  "answer": "95b1695a6869f47b98d07d996a211b5b232063aa024122ce27a546c163759020"
}

phrase is a test password and salt a salt, both text. Return, as lowercase hex text, the 32-byte key of PBKDF2 with HMAC-SHA-256 for that password and salt with 1000 iterations. Check runs your program with other passwords and salts, including an empty password and an empty salt.

Read the result carefully

The page derives keys of a toy setting: 1000 iterations, test passwords and salts of a few letters. It does not say how long a salt should be or how many iterations anyone should use; RFC 8018 section 4.1 is the text the benefits come from. A salt does not make a weak password strong: someone who knows the salt can still hash guesses with it, one salt at a time.

Recap

  • The same password under two salts gives two unrelated keys.
  • A salt is stored with the result: it is not secret.
  • Precomputing one table of guesses for all users no longer works.
  • The key depends on the password, the salt and the iteration count together.