Cryptography / Key derivation functions (PBKDF2)

Key derivation functions (PBKDF2)

How PBKDF2 turns a short, guessable password into a 256-bit key: a random salt, hundreds of thousands of hash rounds, and why Argon2 adds memory to the bill.

Intermediate7 steps~7 min
Password — short, human, guessable
Salt — random, per user, not secret
Key — the derived result: 32 bytes
Guesser — tries likely passwords, fast
// THE SHORT VERSION

In 30 seconds

A key derivation function turns a password into a cryptographic key. Passwords are short and guessable, so the function is built to be slow on purpose.

PBKDF2 mixes the password with a random salt, so equal passwords give different keys, then repeats a keyed hash hundreds of thousands of times. This key stretching costs a real user a fraction of a second and an attacker that much for every guess.

Newer functions such as Argon2 also demand a lot of memory, which blunts the specialised hardware attackers use.

Key derivation functions (PBKDF2), step by step

STEPS01 / 07

Attack times assume 10 billion hashes a second and count each round as one hash; real speeds vary with hardware. The keys shown are real PBKDF2-HMAC-SHA256 outputs for this password and salt.

STEP 01 / 07 A password is a poor key
THE MATH
password: 36^7 ≈ 2^36 choices key: 2^256 choices key = KDF(password)
  1. Step 1: A password is a poor key. A cipher needs a key of 256 unpredictable bits. A person remembers something like tulip42: seven characters, at most about 78 billion combinations even if chosen at random. A key derivation function (KDF) turns the password into a key of the right size, and its real job is to make every guess at the password expensive.
  2. Step 2: One hash is too fast. Hashing the password once does give 256 bits, but SHA-256 was built to be fast. A single graphics card can test on the order of ten billion guesses a second, so every seven-character password of lowercase letters and digits falls in about eight seconds. The output looks random; the input is still easy to guess.
  3. Step 3: Add a salt. A salt is a random value chosen once per user and mixed into the derivation. Two people with the same password now get unrelated keys, and a table of results computed in advance is useless because it would be needed for every possible salt. The salt is not secret; it is stored beside the result.
  4. Step 4: Stretch it: 600,000 rounds. PBKDF2 runs a keyed hash, HMAC, in a chain: the first round mixes the password with the salt, and each later round feeds on the result before it. All the rounds are combined with XOR into the key. Deliberately repeating the work is called key stretching; 600,000 rounds is a common setting with SHA-256.
  5. Step 5: The count sets the price. The slowdown is the same for everyone, but you derive the key once and an attacker must do it for every guess. In this example a login costs a fraction of a second while trying every password grows from 8 seconds to about 54 days. The count has to rise as hardware gets faster, and it cannot rescue a truly weak password.
  6. Step 6: What the server stores. To check a login, the server keeps the salt, the round count and the derived result. When Ana types her password, it repeats the derivation with the stored salt and count and compares. For encryption, such as a password manager or a disk, the result is used directly as the key and is not stored at all.
  7. Step 7: Argon2 adds a memory cost. PBKDF2 needs almost no memory, so a graphics card or custom chip can run thousands of guesses side by side. Argon2, winner of the 2015 Password Hashing Competition, makes each guess fill a large block of memory, limiting how many fit on the hardware. It is the usual pick for new systems; PBKDF2 stays where standards require it.
VALUES SO FAR
passwordtulip42secret
1 hash····attack
salt····public
rounds····public
key····secret
login····cost
PBKDF2····attack
Argon2id····memory

Spotted a mistake on this page?