Section 5.49

Password pepper: a separate secret, compromise cases, and replacement

Definition

A password pepper is a secret key that a password-checking server mixes into every stored password record and keeps apart from the database of records. A thief who copies only the database then cannot test password guesses offline, because every test needs the pepper. A pepper differs from a salt §5.19, the random value stored openly in each record: the salt is public and different for every record, while the pepper is secret, usually one key for many records, and protects only while it stays out of the thief’s hands.

The idea

A web service stores, for each user, a salt and a password hash: the output of a deliberately slow function of the password and the salt. An attacker who steals a copy of the database can test guesses against each record on its own computers, which is called an offline attack. With a hash that takes 100 ms, a list of 10,000 common passwords takes 1,000 seconds per account on one processor core, and weak passwords fall. Stolen databases and leaked backups are common, and often the thief gets nothing else from the server.

The pepper adds a step that needs a secret the database does not contain. After the slow hash, the server computes HMAC §5.8 (hash-based message authentication code), a keyed function, with the pepper as key, and stores that result. Without the pepper, a stored value is random-looking bytes against which no guess can be checked. Think of a codebook locked in a separate safe: a burglar who steals the files cannot read them without breaking into the safe as well. The picture stops where the safe is opened. A thief who takes the pepper too is back to facing only the salt and the slow hash; the pepper never makes a password stronger.

NIST (National Institute of Standards and Technology) SP 800-63B (Special Publication 800-63B), revision 4, section 3.1.1.2, recommends exactly this extra step: a keyed hashing or encryption operation with a secret key known only to the verifier, generated by an approved random bit generator and stored separately from the hashed passwords, preferably inside a hardware-protected area. NIST does not use the word pepper.

How it works

The construction uses these values.

Setting a password:

  1. Compute h=PasswordHash(p,s,c)h = \mathrm{PasswordHash}(p, s, c).

  2. Compute v=HMAC(k,h)v = \mathrm{HMAC}(k, h).

  3. Store ss, cc, kid\mathrm{kid} and vv. Store neither pp nor hh.

Checking a password at login:

  1. Read the user’s record and recompute h′h' from the submitted password with the stored ss and cc.

  2. Have the pepper’s holder compute HMAC(k,h′)\mathrm{HMAC}(k, h') with the key that kid\mathrm{kid} names.

  3. Compare the result with vv using a constant-time comparison §4.33, one whose running time does not depend on where the two values first differ.

The pepper lives outside the database and its backups: in a hardware security module (HSM), a dedicated device that keeps keys inside and performs operations with them without releasing them; in a key management service; or at least in a configuration secret that is never stored with the database. With an HSM, the server sends h′h' and receives the HMAC; the key never leaves the device. The slow hash runs first and the cheap HMAC last, so the HSM does little work per login, and the slow hash keeps its full effect if the pepper ever leaks.

Two variants exist. Encrypting hh with the pepper key instead of computing an HMAC gives a reversible wrapper, which matters for replacing the key (see below). Mixing the pepper into the password before the slow hash also keeps the database useless without the key, but some password hashes accept limited input; bcrypt, for example, uses only the first 72 bytes, so a long pepper can crowd out the password.

Worked example

Assume that PasswordHash\mathrm{PasswordHash} takes 100 ms on one processor core, that the attacker’s list holds 10,000 candidate passwords, and that the pepper is 32 random bytes.

Database only. To test the candidate maple for Alice, the attacker computes h=PasswordHash(𝚖𝚊𝚙𝚕𝚎,sAlice,c)h = \mathrm{PasswordHash}(\texttt{maple}, s_{\mathrm{Alice}}, c) in 100 ms, then needs HMAC(k,h)\mathrm{HMAC}(k, h) and does not have kk. Guessing kk means searching 22562^{256} keys, which is infeasible. No guess can be tested offline; the attacker can only try passwords at the login page, where rate limiting §9.38 applies.

Database and pepper. Each candidate costs one slow hash: 10,000×0.1=1,00010{,}000 \times 0.1 = 1{,}000 seconds, about 16.7 minutes, per account on one core; the HMAC adds a negligible amount. For 1,000,000 accounts that is 10910^9 seconds, about 31.7 years on one core or about 11.6 days on 1,000 cores. The salt forces this work to be repeated for every account, and the slow hash sets the price of each guess.

A weak pepper. Suppose a developer chose the pepper from about 2202^{20} possibilities, such as a word followed by digits. The attacker registers an account with the known password maple, steals the database, computes hh for that account once (100 ms), and then tries each candidate pepper with one HMAC. At an assumed 1 microsecond per HMAC, 220=1,048,5762^{20} = 1{,}048{,}576 tries take about 1.05 seconds. The pepper is recovered and the attacker is in the previous case. This is why the pepper must be a random key, and why NIST requires an approved random bit generator.

Stated precisely

Let h=PasswordHash(p,s,c)h = \mathrm{PasswordHash}(p, s, c) and v=HMAC(k,h)v = \mathrm{HMAC}(k, h). Assume:

  1. kk is uniformly random, at least 128 bits long, and independent of everything in the database (NIST sets a floor of 112 bits of security strength);

  2. HMAC is a pseudorandom function §15.1: with a secret, uniformly random key, its outputs cannot be told apart from random values by any feasible computation;

  3. the attacker holds the whole database, including records for passwords it chose, but neither kk nor any way to have HMAC computed with kk.

Then the stored vv values look like independent random strings to the attacker, and the database gives no way to check a password guess. The attacker’s options are to guess kk, each guess right with probability 2−1282^{-128} or less, or to guess passwords online through the login service.

If assumption 3 fails because kk leaks, the protection falls exactly to that of the salt and the slow hash: one evaluation of PasswordHash\mathrm{PasswordHash} per guess per account. If it fails because the attacker can have HMAC computed with kk, for example by controlling the server that talks to the HSM, each such request tests one guess, so the attack runs at the rate the key holder allows and stops when the attacker loses that access.

Compromise cases

Each case below states what the attacker holds and what the pepper still does.

Replacing the pepper

Key rotation §5.29 for a pepper depends on what the record stores. With v=HMAC(k7,h)v = \mathrm{HMAC}(k_7, h), where k7k_7 is the pepper with key identifier 7, computing the value under a new key k8k_8 needs hh, and hh needs the password. HMAC cannot be reversed. The usual procedure is:

  1. Generate k8k_8 and keep k7k_7 available.

  2. At each successful login to a record with identifier 7, the server has just computed hh; it stores HMAC(k8,h)\mathrm{HMAC}(k_8, h) with identifier 8.

  3. After a stated deadline, force a password reset for the records still at identifier 7, then destroy k7k_7.

Dormant accounts stay under k7k_7 until the deadline.

Wrapping gives immediate protection without the password: replace every vv by v′=HMAC(k8,v)v' = \mathrm{HMAC}(k_8, v), and verify by computing HMAC(k8,HMAC(k7,h′))\mathrm{HMAC}(k_8, \mathrm{HMAC}(k_7, h')). After k7k_7 leaks, this restores protection at once for every copy of the database taken after the wrapping: a thief who has such a copy and k7k_7 still needs k8k_8. It retires nothing: k7k_7 is needed for every verification from then on.

Encrypting instead, v=Enc(k7,h)v = \mathrm{Enc}(k_7, h) with authenticated encryption §5.16, allows all records to be decrypted with k7k_7 and re-encrypted with k8k_8 in one pass, after which k7k_7 can be destroyed. A holder of the key recovers hh, not the password, which leaves it in the same position as a holder of an HMAC pepper: offline guessing at the cost of the slow hash.

Whatever the scheme, the record’s key identifier selects only among approved key versions, and destroying a key that some record still needs locks those users out. No scheme changes a record copied before the record itself was replaced, for example in an old backup: whoever holds that copy and k7k_7 can still test guesses against it at the cost of the slow hash. After k7k_7 leaks, old copies must therefore be destroyed or brought under k8k_8 as well, and a copy already held by someone who knows k7k_7 is protected only by the salt and the slow hash.

Limits and common mistakes

A pepper does not strengthen a password. A weak password stays weak online, and against anyone who obtains the pepper.

A pepper does not replace the salt or the slow hash. If the pepper leaks, they are the only protection left.

The pepper must be a random key, never a word chosen by a person, and must never be stored where the database is: not in the same backups, logs, configuration files or source code repository.

A key that cannot be exported from an HSM can still be used through it. An attacker in control of the server can use the HSM’s interface, so authentication §1.6, rate limits and monitoring on that interface matter.

An attacker who can write to the database can copy its own record into another user’s row and then log in there with its own password. Including the account identifier in the HMAC input, encoded unambiguously, for example with a length prefix §4.30, makes a copied record fail to verify.

Losing the pepper locks out every user. Keep a backup in separately protected storage, for example a second HSM or shares held by several custodians through threshold secret sharing §5.44, and test that the backup can be restored.

Questions answered

How does a pepper differ from a salt?

A salt is a public random value, different for each password record and stored inside it; a pepper is a secret key, usually shared by many records, stored apart from the database. The salt makes each record’s computation different, so one guess cannot be tested against all accounts at once and tables cannot be computed in advance. It protects nothing in a single stolen record, because the thief has it. The pepper makes stolen records useless for offline guessing for as long as the thief does not also have the pepper.

Their failure cases differ too. A leaked salt is harmless; a leaked pepper removes its protection. A lost salt makes one record unverifiable; a lost pepper makes every record keyed with it unverifiable. Neither makes a weak password strong, and neither replaces a slow password hash.

Does moving a key to an HSM automatically eliminate password guessing?

No: moving the key into a hardware security module (HSM) stops the key from being copied, not from being used to test guesses. Anyone who can send requests to the HSM can still have it compute the keyed step for password guesses. An attacker who controls the application server can do exactly that, one guess per request, for as long as the control lasts. The HSM changes the attack from offline guessing at the attacker’s own speed to guessing at the speed and for the duration that the HSM allows. Authentication of callers, rate limits and monitoring on the HSM interface decide how much guessing that is, and online guessing through the login page remains possible in any case.

Can the final-HMAC-only design rotate its key for every dormant account immediately?

No: a design that stores only the final HMAC (hash-based message authentication code), the keyed hash computed with the pepper, cannot move a dormant account to a new key until its user logs in again. In that design each record stores only v=HMAC(k,h)v = \mathrm{HMAC}(k, h), where hh is the slow password hash and kk the pepper, and neither the password nor hh is kept. Computing the value under a new key needs hh, and HMAC cannot be reversed to get it, so a record can move to the new key only when its user next logs in and supplies the password. Accounts that stay dormant are either forced to reset after a deadline or kept under the old key. Wrapping every record under the new key, HMAC(knew,v)\mathrm{HMAC}(k_{\mathrm{new}}, v), works at once: a thief who copies the database after the wrapping cannot test guesses with a leaked old key alone, although a copy taken before the wrapping, such as an old backup, still allows that. The old key must then be kept for every future check, so wrapping does not retire it. A design that encrypts hh instead of computing an HMAC can re-key every record at once.

What if the pepper is lost rather than stolen?

If the pepper is lost, no record keyed with it can be verified any more, so even users with the correct password cannot log in, and all of them must reset their passwords through another channel such as a recovery e-mail. Loss harms availability, not secrecy. The defense is a backup of the key in separately protected storage, such as a second hardware security module or shares held by several custodians, with a tested restore procedure. Every backup is also a copy that could be stolen, so it needs the same protection as the original.

Related entries: Salt (§5.19), Password hashing and memory-hard KDF (§15.10), HMAC (§5.8), Entropy and min-entropy (§5.26), Key revocation (§5.32), Authentication coverage (§5.41). Further reading: [199].