Full name: Password hashing and memory-hard key derivation function.
Definition
Password hashing is storing, in place of each password, the output of a deliberately slow function that cannot feasibly be run backwards, computed from the password and a salt §5.19, a random value kept with the record. If the records are stolen, every guess costs the attacker real work, separately for each account. A memory-hard key derivation function §5.20 (KDF), such as scrypt or Argon2, is a password hashing function that also occupies much memory during each evaluation, which makes massively parallel guessing hardware expensive.
The idea
An attacker steals a server’s table of user records. If each record holds the SHA-256 §5.2 (Secure Hash Algorithm with a 256-bit output) hash of the password, the attacker cannot run the hash backwards, but does not need to: the attacker hashes candidate passwords and compares the results with the table. This is offline guessing. No server sees the attempts, so no lockout or rate limiting §9.38 applies.
Passwords chosen by people are few and predictable. Lists of leaked passwords, dictionary words with small changes, and short strings of digits cover a large share of real passwords within a few billion candidates. A 128-bit random key could never be found this way; a password can, because its entropy §5.26, the attacker’s uncertainty about it, is small. SHA-256 is designed to be fast, and a GPU (graphics processing unit), a chip with thousands of simple cores, computes billions of SHA-256 hashes per second. A fast hash makes each guess almost free.
Password hashing cannot make a password less guessable, but it can make each guess expensive. Three measures work together. A salt that differs for every account forces the attacker to attack each account separately and makes tables computed in advance useless. A work factor, a cost setting chosen by the defender, makes one evaluation slow; this deliberate slowing is also called key stretching. Memory hardness makes one evaluation also occupy, for example, 64 MiB for its whole duration, so a thousand guesses in progress at once need a thousand times that memory. The defender pays the cost once per login; the attacker pays it once per guess. The same functions also turn passwords into encryption keys, which is why they are also called key derivation functions.
How it works
Storing a password.
Generate a fresh salt, for example 16 random bytes from a cryptographically secure pseudorandom number generator §5.27.
Choose the cost parameters. For Argon2 they are the memory size in KiB, the number of passes over that memory, and the number of lanes , independent strands of the computation that may run at the same time.
Run the password hashing function on the password, the salt and the parameters. Call the result , the verifier.
Store the name of the function, the parameters, the salt and . None of them is secret.
Checking a login. Read the stored record, run the same function on the offered password with the stored salt and parameters, and compare the result with using a constant-time comparison §4.33. If the defender later raises the cost, a record can be recomputed with the new cost at the user’s next successful login, when the password itself is available.
How Argon2 is memory-hard. Argon2 fills an array §14.5 of about blocks of 1 KiB each. Each new block is computed by a mixing function from the previous block and one earlier block whose position is chosen pseudorandomly. Further passes overwrite the blocks in the same way, and the output is derived from the final blocks. Since any block may be needed again later, an evaluation that keeps less memory must recompute discarded blocks whenever they are needed, and each recomputation needs further blocks in turn. That trade of memory for time is what the design makes unprofitable. Argon2d chooses positions from the data computed so far, which resists such trades best but makes the memory access pattern depend on the password, a possible side channel §1.29. Argon2i chooses positions independently of the password. Argon2id, the variant RFC 9106 recommends, works as Argon2i for the first half of the first pass and as Argon2d for the rest.
Other password hashing functions. PBKDF2 (Password-Based Key Derivation Function 2), defined in RFC 8018 [381], repeats HMAC §5.8 (hash-based message authentication code) a chosen number of times; it costs time but almost no memory, so a GPU runs many copies efficiently. bcrypt (1999 [380]) uses a fixed 4 KiB of memory and reads at most 72 bytes of the password. scrypt (2009 [379], later RFC 7914 [378]) introduced the idea of a memory-hard function. Argon2 won the Password Hashing Competition in 2015 and is specified in RFC 9106.
Worked example
The speeds in this example are invented round numbers chosen for easy arithmetic, not measurements. The attacker holds 1,000,000 stolen records and a list of candidate passwords, and computes SHA-256 hashes per second.
Unsalted SHA-256. One pass over the list costs hashes, which takes seconds. Each result is looked up among all 1,000,000 stored values at once, so in 0.01 seconds the attacker tests every candidate against every account.
Salted SHA-256. Each account has its own salt, so every candidate must be hashed again for every account: hashes, which take seconds, about 2.8 hours. The salt multiplied the total work by the number of accounts, but one chosen account still falls in 0.01 seconds.
Salted and slow. Suppose one evaluation of the password hashing function costs as much as SHA-256 hashes. One account now costs seconds, about 17 minutes, and all accounts cost seconds, about 31.7 years, since a year has seconds and . A server core that computes SHA-256 hashes per second spends seconds on one login.
Memory. With 64 MiB per evaluation, an attacker who wants 1,000 evaluations in progress at once needs MiB, which is GiB of fast memory, besides the computing cores. A GPU with 24 GiB of memory holds at most evaluations at once, however many cores it has.
What cost cannot do. If an account’s password is among the attacker’s first 1,000 candidates, the slow function still gives it up after seconds. Cost multiplies the work of every guess; it does not move a weak password further down the list.
Stated precisely
Let be the password hashing function, with password , salt and cost parameters . The defender stores , and , and accepts an offered password exactly when . Let be the computing cost of one evaluation.
Guessing. For one account, let be the probabilities of the possible passwords, sorted from most to least likely. An attacker with a computing budget evaluates candidates, where is rounded down to a whole number, and succeeds with probability at most . The defender controls only : raising it tenfold divides by ten, and it changes none of the .
Salt. If every account has a different salt, no single evaluation of serves two accounts, so attacking accounts costs times as much as attacking one, and a table computed before the theft is useless. The salt need not be secret. RFC 9106 states that a 128-bit salt is sufficient for all applications and that 64 bits may be used when space is short.
Memory hardness. The design goal of a memory-hard function is that every way of evaluating it with less memory takes so much more computation that the product of memory and time per evaluation stays close to the product for the intended memory. The attacker’s cost per guess is then roughly proportional to that product, which a GPU or an ASIC (application-specific integrated circuit, a chip built for one task) lowers far less than it lowers the cost of plain computation. RFC 9106 notes that Argon2i needs more passes over its memory to resist such trade-off attacks.
Recommended settings. RFC 9106 recommends Argon2id with pass, lanes and 2 GiB of memory as a uniformly safe first choice, and , and 64 MiB when much less memory is available, both with a 128-bit salt and a 256-bit output.
Limits and common mistakes
Cost does not add entropy. A password among the attacker’s first guesses is found whatever the cost.
Online guessing, in which the attacker tries passwords against the live login service, is a separate threat that needs rate limiting or lockout. A slow password hash also makes each login attempt expensive for the server, which an attacker can exploit for denial of service §1.28 by sending many login requests.
The cost must fit the defender’s hardware. A device with little memory cannot spend 64 MiB on one evaluation, and every reduction in memory or time makes the attacker’s work cheaper by the same factor.
A fast key derivation function such as HKDF §5.21 (HMAC-based Extract-and-Expand Key Derivation Function) is the wrong tool for passwords. HKDF assumes that its input is already hard to guess and is fast by design.
Passwords must not be stored encrypted in place of hashed. Whoever obtains the encryption key recovers every password at once, while a hash must be attacked one guess at a time.
A pepper §5.49, a secret mixed into every password hash and kept outside the database, protects the records only as long as the pepper stays secret.
bcrypt reads only the first 72 bytes of a password, so two longer passwords that share those bytes are accepted as equal. Hardware gets faster over time, so the parameters are stored with each record and raised when needed.
Questions answered
Why is one fast SHA-256 operation a poor password-storage strategy?
One fast SHA-256 (Secure Hash Algorithm with a 256-bit output) operation is a poor way to store passwords because an attacker who steals the stored values can test guesses offline at the same low cost per guess, and passwords chosen by people are predictable enough to be found by guessing. SHA-256 is built to be fast: a single graphics processor computes billions of hashes per second, so a list of likely passwords is tested in a fraction of a second. The hash cannot be run backwards, but it does not need to be: hashing each candidate and comparing finds every password on the list. Without a salt, a random value stored with each record, one pass over the list tests every account at once, and tables computed in advance apply directly.
A password hashing function such as Argon2id, scrypt, bcrypt or PBKDF2 (Password-Based Key Derivation Function 2) addresses both problems. A different salt for each account forces a separate attack on each account, and a deliberately high cost in time, and for memory-hard functions also in memory, makes every guess expensive, so the same budget tests far fewer candidates. Neither measure makes a weak password strong: a password near the top of the attacker’s list is still found quickly.
Related entries: Salt (§5.19), Key derivation function (KDF) (§5.20), Entropy and min-entropy (§5.26), Algorithmic complexity attack (§15.35), Password pepper: a separate secret, compromise cases, and replacement (§5.49), HKDF (§5.21), Rate limiting (§9.38). Further reading: [138, 378, 379, 380, 381].