Section 5.19

Salt

Definition

A salt is a value, normally not secret, that is given to a password hash or a key derivation function §5.20 together with the secret input, so that the same secret gives different results under different salts. A password hash is a deliberately slow function whose output a server stores in place of the password; a key derivation function turns a secret into cryptographic keys. In password storage every record gets its own salt. In key derivation the salt strengthens the step that turns an imperfect secret into a key.

The idea

A service stores, for each user, a hash of the password rather than the password: a fixed-length value computed by a one-way function, which lets the server check a login without keeping the password. The file of hashes leaks. Two users chose the password maple, so without a salt their stored values are identical, and the attacker sees at once that they share a password. Worse, the attacker can hash a list of ten million common passwords once, even before the leak, and look up every stored value in that one table.

A salt breaks this sharing. The service picks a different random salt for each record and hashes the salt together with the password. The two maple records now hold different values, and a table computed for one salt is useless for every other record. The attacker must repeat the work for each record and cannot start before seeing the salts. A picture: each record writes its password in a private dialect, and the salt names the dialect. The picture fails in one respect: the dialect is not secret. The attacker reads the salt from the stolen record and can try guesses against that one record at full speed, so a weak password stays weak.

Key derivation uses a salt differently. In HKDF §5.21 (HMAC-based Extract-and-Expand Key Derivation Function), the extraction step computes HMAC §5.8 (hash-based message authentication code), a keyed hash function, with the salt as the key and the secret as the message. A random salt that the attacker did not choose lets this step turn many kinds of imperfect secret into a good key, and one salt may serve many derivations.

How it works

Salting a stored password. NIST Special Publication (SP) 800-63B-4, NIST’s guideline for authentication §1.6, requires that stored passwords be salted and hashed with a password hashing §15.10 scheme, which takes a password, a salt and a cost factor.

  1. When a password is set, generate a new salt, for example 16 bytes from a CSPRNG §5.27 (cryptographically secure pseudorandom number generator).

  2. Compute the password hash from the password, the salt and the cost settings, with a slow function such as PBKDF2 (Password-Based Key Derivation Function 2) or Argon2.

  3. Store the salt, the hash, and the name of the algorithm §14.1 with its cost settings in the user’s record.

  4. At login, read the salt from the record, recompute the hash from the password typed in, and compare.

The standards set these requirements on password salts.

Salting a key derivation. The extraction step of HKDF computes the pseudorandom key (PRK) as PRK=HMAC(salt,IKM)\mathrm{PRK} = \mathrm{HMAC}(\mathrm{salt}, \mathrm{IKM}), where IKM, input keying material, is the secret. RFC 5869, which defines HKDF, describes its salt as follows.

SP 800-56C Rev. 2, NIST’s recommendation for key derivation during key establishment, likewise does not require a random salt and allows a fixed default value such as all zero bytes [323].

Worked example

Work for the attacker. A leaked file holds n=1,000,000n = 1{,}000{,}000 password records, and the attacker has a list of g=10,000,000g = 10{,}000{,}000 likely passwords.

The salt multiplies the cost of attacking everyone. It leaves the cost of attacking one user unchanged; only a slow password hash raises the cost of each computation.

How long a random salt must be. Draw each salt at random and count the expected number of pairs of records that receive the same salt. There are n(n−1)/2=499,999,500,000n(n-1)/2 = 499{,}999{,}500{,}000 pairs among the million records, and each pair matches with probability 2−b2^{-b} for salts of bb bits:

So if salts of 32 bits, the minimum length that SP 800-63B-4 allows, are drawn at random, about 116 pairs among a million users share a salt, and each such pair can be attacked with one computation per guess. SP 800-63B-4 asks that salts be chosen to minimize such repeats; with random 128-bit salts, the length SP 800-132 requires for the random part, no repeat is expected at all.

Stated precisely

Let H(p,s)H(p, s) be a password hash of the password pp with the salt ss. Let the stolen file hold the records (si,hi)(s_i, h_i) for i=1,…,ni = 1, \ldots, n, where hi=H(pi,si)h_i = H(p_i, s_i), let the attacker hold a list GG of gg candidate passwords, and let dd be the number of distinct values among s1,…,sns_1, \ldots, s_n. Testing every candidate against every record costs g×dg \times d evaluations of HH. With no salt, or one salt shared by all records, d=1d = 1; with a distinct salt per record, d=nd = n. The probability of recovering the password of one record does not depend on the salt: it is the probability that pip_i is in GG.

Computing a table before the leak is impossible only if the salt cannot be predicted before it is chosen. For a salt known in advance, such as a user name, a table for that salt can be computed in advance.

For nn salts of bb bits drawn independently and uniformly, the expected number of pairs with equal salts is n(n−1)/2⋅2−bn(n-1)/2 \cdot 2^{-b}, and the probability that at least one pair is equal is at most that number. This is the birthday bound §5.6.

For HKDF, the role of the salt is part of the HKDF security claim: if HMAC acts as a randomness extractor, a function that turns an input that is hard to guess but not uniformly random into output that cannot be told apart from uniformly random bytes, then with a salt that is random, independent of the IKM and not chosen by the attacker, the extraction step turns any IKM with enough min-entropy §5.26, a measure of how hard the IKM is to guess in one try, into a key that cannot be told apart from random. The HKDF entry states the claim and its assumptions in full.

Limits and common mistakes

A salt adds no secret. Whoever has the record has the salt, so a password that is easy to guess stays easy to guess for that record. Only the cost of each guess, which a slow password hash sets, and the strength of the password itself protect a single user. HKDF and SHA-256 alone are fast and therefore the wrong tools for passwords.

A salt is not a pepper §5.49. A password pepper is a secret value, kept apart from the database, that is mixed into every password hash; it protects only while it stays secret.

Unique is not the same as unpredictable. A user name or a record number is unique within one service, so it stops one table from covering every record. But it is known before the leak, so a table for one targeted account can be prepared in advance, and the same user name on two services gives the same salt.

A salt is not a nonce §5.18. HKDF allows one salt to be reused for many derivations; a password salt should not repeat between records; a nonce, the value an encryption mode takes with each message, must never repeat under one key. The name salt does not say which rule applies; the specification of the function that receives the value does.

In HKDF, keeping keys for different purposes apart, called domain separation §5.22, is the job of the info input, not of the salt. RFC 5869 describes info as the input that binds the derived keys to application- and context-specific information.

The salt must be stored with the hash. Without it the stored hash can no longer be checked.

Questions answered

When must salt be random, unique, or merely domain-specific?

A salt must be unique for each stored password, and random generation is the standard way to make it unique; a salt for a key derivation step should be random when the application can obtain one, but it may be reused or fixed; a salt that is merely domain-specific, the same for every input in one application, is acceptable only where the salt’s job is not to keep users apart. A salt is a usually public value fed into a password hash or a key derivation function together with the secret. In detail:

The rule comes from the specification of the function that receives the salt, not from the word salt.

Related entries: Key derivation function (KDF) (§5.20), HKDF (§5.21), Entropy and min-entropy (§5.26), Nonce (§5.18), Password pepper: a separate secret, compromise cases, and replacement (§5.49), Password hashing and memory-hard KDF (§15.10), Birthday bound (§5.6), Domain separation (§5.22). Further reading: [9, 138, 199, 322, 323].