Highlights

Contents

Part III
03

Password storage

We learn how to store passwords so that a stolen database does not hand over the accounts. Databases leak through backups, SQL injection and insiders, and many users reuse passwords elsewhere. We will see why fast hashes fail, what salts and peppers add, and how bcrypt, scrypt and Argon2 make each guess expensive.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1Session authentication2Logout, devices andrevocation3Password storage4Login attacks anddefences5Password reset6JSON Web Tokens7Access and refresh tokens8JWT attacks and mistakes9OAuth 2.010OpenID Connect and thefull login

#Section 8Why SHA-256 isn’t a password hash

Assume Wren’s users table leaks. That is the right assumption to design for, since breaches of exactly this kind happen to large companies every year, through backups, injection bugs and insiders. LinkedIn lost 6.5 million unsalted SHA-1 password hashes in 2012, and most were cracked within days. So the question is what the attacker can do with the table.

Suppose Wren stored sha256(password) for each user. The attacker cannot reverse SHA-256, but they do not need to. They guess. Take every password of eight lowercase letters and digits, which is 368=2,821,109,907,45636^8 = 2{,}821{,}109{,}907{,}456 candidates. Hash each one and compare. A single modern GPU computes on the order of 101010^{10} SHA-256 hashes per second (an illustrative figure), so it covers the whole space in about 282 seconds. Every user with such a password is exposed in under five minutes, and real attackers do better by trying likely passwords first, from leaked lists and common patterns.

CRACKING EVERY 8-CHARACTER LOWERCASE-AND-DIGIT PASSWORD (36^8 = 2.82 x 10^12)CRACKING EVERY 8-CHARACTER LOWERCASE-AND-DIGIT PASSWORD (36^8 = 2.82 x 10^12)one GPUSHA-256282 secondsbcrypt8.9 years10¹⁰/s vs 10⁴/sillustrative single-GPU rates; the ratio, not the exact numbers, is the point
Figure 3.11The same search at fast-hash and slow-hash speeds. The rates are illustrative, and the ratio is the point.

General-purpose hashes are designed to be fast, which is exactly wrong for this job. A password hash, also called a password-based key derivation function, is deliberately slow and has a tunable cost. At an illustrative 10410^4 bcrypt hashes per second, the same search takes 2.82×1082.82 \times 10^{8} seconds, about 8.9 years. The user pays 250 ms once at login and never notices. The attacker pays it on every one of the trillions of guesses.

Unsalted fast hashes have a second flaw. An attacker can hash common passwords once in advance and store the results in a lookup table, or a compressed version called a rainbow table. A leaked table is then matched instantly, without any new hashing. Identical passwords also produce identical hashes, so cracking one cracks every user who shares it, and the most common hash in the table tells the attacker which password to crack first.

WITHOUT A SALT, ONE PRECOMPUTED TABLE CRACKS EVERY USER AT ONCEWITHOUT A SALT, ONE PRECOMPUTED TABLE CRACKS EVERY USER AT ONCE5e8848…passwordef92b7…12345665e84b…qwertylookup book, computed onceuser 42: 5e8848…user 43: 5e8848…user 44: ef92b7…same password, same hash: one crack, many users
Figure 3.12One precomputed table cracks every unsalted hash of a common password at once.

#Section 9Salt and pepper

A salt is a random value generated for each user, typically 16 bytes, and stored next to the hash in plain sight. The stored hash becomes H(salt ∥ password)H(\text{salt} \,\|\, \text{password}). Two users who both chose hunter2 now have unrelated hashes, because their salts differ. A precomputed table is useless, since the attacker would need a separate table for every possible salt. Salts are not secret, and they do not need to be. Their job is uniqueness. They force the attacker to attack each user separately, which multiplies the work by the number of users, a factor of 1,000,000 for Wren.

A RANDOM PER-USER SALT MAKES THE SAME PASSWORD HASH DIFFERENTLYA RANDOM PER-USER SALT MAKES THE SAME PASSWORD HASH DIFFERENTLYuser 42"hunter2"+ 9f1c…hasha7d0…user 43"hunter2"+ 0b3e…hash41c9…salts sit in the clear beside the hash; their job is uniqueness, not secrecy
Figure 3.13Same password, different salts, unrelated hashes. Precomputation stops working.

A pepper is a secret key applied to every password and kept outside the database, in a key management service or a hardware security module. A common construction is HMAC(pepper,argon2(password,salt))\text{HMAC}(\text{pepper}, \text{argon2}(\text{password}, \text{salt})). An attacker who steals only a database dump, through SQL injection or a stolen backup, cannot test a single guess without the pepper, so the dump is useless on its own. It does nothing against an attacker who has fully compromised the application server, since the server must use the pepper to check logins.

The pepper's costs are operational. Rotating it requires re-hashing each password at the user's next login, since nobody knows the passwords to re-hash them in bulk, so the system must support two peppers during the change and record which one each hash used. Losing it locks every user out permanently. Many teams skip the pepper and rely on a strong password hash plus tight database access control, and that is a reasonable choice, but if you add one, keep it in a KMS, never in the same database or the same config file as the hashes.

A PEPPER IS A SECRET KEPT OUTSIDE THE DATABASEA PEPPER IS A SECRET KEPT OUTSIDE THE DATABASEusers tablesalt + hashKMS / HSMpepper keyHMAC(pepper, argon2(pw, salt))a stolen DB dump alonecannot be cracked offline
Figure 3.14The pepper lives where a SQL injection cannot reach. A dump alone cannot be cracked.

#Section 10bcrypt, scrypt and Argon2

Three algorithms dominate. bcrypt (Provos and Mazières, 1999) makes each guess cost CPU time. Its cost parameter sets the number of rounds of its expensive key setup to 2cost2^{\text{cost}}, so cost 12 means 4,096 rounds. It is battle-tested and available in every language. It has one sharp edge. It silently ignores input beyond 72 bytes, so two long passphrases that share their first 72 bytes hash identically, and systems that pre-hash to work around this must do it carefully.

scrypt (Percival, 2009) adds a large memory requirement. Attackers crack passwords on GPUs and custom chips that run thousands of small cores in parallel, each with little memory. A hash that needs many megabytes of fast memory per guess limits how many guesses run at once, which cuts the attacker’s advantage far more than CPU cost alone. Argon2 won the Password Hashing Competition in 2015. Its Argon2id variant has separate settings for memory, time (passes over memory) and parallelism (lanes), and it resists both GPU attacks and side-channel attacks. OWASP’s current minimum recommendation is Argon2id with 19 MiB of memory, 2 passes and 1 lane, and it is Wren’s choice.

THREE PASSWORD HASHES: WHAT EACH ONE MAKES EXPENSIVETHREE PASSWORD HASHES: WHAT EACH ONE MAKES EXPENSIVECPUbcrypt (1999)CPU timecost 12 = 2¹² rounds72-byte input limitCPUscrypt (2009)CPU + memoryN, r, p parametershurts GPU parallelismCPUArgon2id (2015)CPU + memory + lanesm=19 MiB, t=2, p=1OWASP minimum
Figure 3.15Each algorithm raises a different cost. Argon2id is the current default recommendation.

The work factor is the setting that makes hashing slow. For bcrypt, each step up in cost doubles the time, for the server and for the attacker alike. With illustrative timings, cost 10 takes 62.5 ms, cost 11 takes 125 ms, cost 12 takes 250 ms and cost 13 takes 500 ms. At Wren’s peak of 20 logins per second and 250 ms per hash, hashing alone needs 20 × 0.25 = 5 CPU cores. During a promotion that triples logins, it needs 15. The right setting is the most expensive one your login traffic can afford. Too low helps attackers, too high lets a burst of login attempts exhaust your CPU, which an attacker can do on purpose.

EACH +1 ON THE BCRYPT COST DOUBLES THE WORK, FOR YOU AND FOR THE ATTACKEREACH +1 ON THE BCRYPT COST DOUBLES THE WORK, FOR YOU AND FOR THE ATTACKERcost 1062.5 mscost 11125 mscost 12250 mscost 13500 msat 20 logins/s, cost 12 needs 5 cores just for hashing
Figure 3.16The cost doubles with each step. The right setting is a capacity decision, not a constant.

The stored string records everything needed to verify it: algorithm, version, parameters, salt and hash, as in $argon2id$v=19$m=19456,t=2,p=1$<salt>$<hash>. When Wren raises its parameters, it cannot re-hash stored values, because it never knew the passwords. Instead, after each successful login, it compares the stored parameters with the current target and, if they are weaker, re-hashes the password it just verified. Active users are upgraded within weeks. Accounts that never log in keep old hashes, and some teams force a reset on accounts still using a weak scheme after a deadline.

THE STORED STRING CARRIES ITS OWN ALGORITHM AND PARAMETERSTHE STORED STRING CARRIES ITS OWN ALGORITHM AND PARAMETERS$argon2idalgorithm$v=19version$m=19456,t=2,p=1cost$<salt>salt$<hash>hashraise m or t later: re-hash each user at their next successful login
Figure 3.17The hash string describes itself, which makes parameter upgrades possible one login at a time.