Section 5.3

Preimage resistance

Definition

Preimage resistance is the property of a hash function that, given only a digest, finding any input that produces that digest would take more computation than anyone can perform. A hash function is a public algorithm §14.1 that turns an input of any length into a short fixed-length value called the digest, and a preimage of a digest is any input that produces it. The property is also called one-wayness. It concerns a search among all possible inputs, so it gives no protection to an input that can be guessed.

The idea

A service wants to recognize a secret without keeping the secret itself. It stores the digest of the secret. When the secret is presented later, the service hashes it and compares the result with the stored digest. If the stored digests are stolen, the theft should not hand over the secrets. That requires a function whose digests cannot be turned back into inputs.

The picture is a one-way street. Computing a digest from an input is fast, and nothing faster than trial and error is known for the way back. Trial and error is hopeless when the digest is long, because each trial input produces one particular 256-bit digest with probability 2−2562^{-256}.

The picture has a gap. If the attacker knows that the original input was one of a few thousand likely values, the attacker hashes those values in the forward direction and compares. The street is still one-way. The attacker never needed to drive it backwards.

How it works

The property is defined by a contest between a challenger and an attacker. Write HH for the hash function.

  1. The challenger picks an input xx at random from a set far larger than the set of all digests, computes the digest y=H(x)y = H(x), and gives the attacker only yy.

  2. The attacker computes for as long as it can afford and then names an input x′x'.

  3. The attacker wins if H(x′)=yH(x') = y. The input x′x' need not be equal to xx. Any preimage of yy counts.

HH is preimage resistant if no attacker with a feasible amount of computation wins, except with a probability so small that it can be ignored.

The attack that works against every hash function is exhaustive search.

  1. Choose a candidate input that has not been tried.

  2. Compute its digest.

  3. Stop if the digest equals yy. Otherwise go back to step 1.

To predict the cost, model the function as an ideal hash function: one in which the digest of each new input is an independent, uniformly random value of nn bits. Then each new candidate succeeds with probability 1/2n1/2^n, and the search needs about 2n2^n hash computations on average. Preimage resistance of a real function means that no known method does usefully better than this search.

A dictionary attack is the same loop with a different supply of candidates. The attacker does not take candidates from all possible inputs. The attacker takes them from a dictionary, a list of the values that the input is likely to be, most likely first. The cost is then set by the length of the list, not by the length of the digest.

The hashing can be done in advance. A table of digests for a whole dictionary is computed once and then answers every stolen digest by a lookup.

Worked example

Exhaustive search, small scale. Suppose an ideal hash function had digests of only 4 bits, so that 24=162^4 = 16 digests exist. One candidate hits a given digest with probability 1/16=0.06251/16 = 0.0625. Two different candidates both miss with probability (15/16)2=225/256(15/16)^2 = 225/256, so at least one hits with probability 1−225/256=31/256≈0.1211 - 225/256 = 31/256 \approx 0.121. The chance first passes one half at 11 candidates, because 1−(15/16)11≈0.5081 - (15/16)^{11} \approx 0.508. The average number of candidates needed is 16. Each additional digest bit doubles these numbers. At 256 bits the average is 2256≈1.16×10772^{256} \approx 1.16 \times 10^{77} hash computations, which no computer can perform.

Dictionary search, real scale. A door code has 4 decimal digits, and a system stores the digest of the code, computed with SHA-256 §5.2 (Secure Hash Algorithm with a 256-bit output) over the code as ASCII text. The stored digest begins c745b79a. Only 104=10,00010^4 = 10{,}000 codes exist. An attacker hashes 0000, 0001, 0002 and so on. The digest of 4831 equals the stored digest, so the search ends after 4,832 hash computations. If every code is equally likely, the average is (10,000+1)/2=5,000.5(10{,}000 + 1)/2 = 5{,}000.5 computations and the worst case is 10,000. The digest length of 256 bits played no part. Measured in bits, the code holds log210,000≈13.3\log_2 10{,}000 \approx 13.3 bits of uncertainty, so the attack costs about 213.32^{13.3} computations, not 22562^{256}.

Stated precisely

Let HH be a hash function with digests of nn bits, and let N=2nN = 2^n be the number of possible digests. NIST Special Publication 800-107 Revision 1 (2012) states preimage resistance as: given a randomly chosen digest, it is computationally infeasible to find an input that hashes to it [310]. For an ideal hash function, an attacker that hashes qq different candidates finds a preimage of a given digest with probability 1−(1−1N)q≤qN.1 - \left(1 - \frac{1}{N}\right)^{q} \;\le\; \frac{q}{N}. With N=16N = 16 and q=2q = 2 this is 31/25631/256, as in the example, against the simpler limit 2/16=0.1252/16 = 0.125. NIST gives the expected preimage strength of a hash function with an nn-bit digest as nn bits, where a strength of ss bits means about 2s2^s operations. For SHA-256 that is 256 bits.

When the input is not uniformly random, the cost of guessing the input also bounds the attacker’s work. Suppose the input is one of DD known candidates with probabilities p1≥p2≥…≥pDp_1 \ge p_2 \ge \dots \ge p_D. An attacker who hashes the qq most likely candidates finds the input with probability p1+p2+…+pqp_1 + p_2 + \dots + p_q. If all candidates are equally likely, this is q/Dq/D, and the expected number of hash computations is (D+1)/2(D + 1)/2. The cost of recovering the input is therefore about the smaller of 2n2^n and the cost of guessing the input. Hashing does not raise the cost of guessing. The chance of the single best guess, p1p_1, is what the min-entropy §5.26 of the input measures: it is −log2p1-\log_2 p_1 bits.

Limits and common mistakes

A preimage is any matching input, not necessarily the original. With a long digest and a short candidate list the two coincide in practice: the chance that a wrong code among 10,000 matches a 256-bit digest by accident is about 10,000/225610{,}000 / 2^{256}.

Preimage resistance does not make a guessable input secret. Digests of passwords, short codes, telephone numbers, serial numbers and other values from a small or predictable set can be reversed by guessing.

Guesses can be tested only by someone who can compute the function. If a secret key enters the computation, as in HMAC §5.8 (hash-based message authentication code), an attacker without the key cannot test guesses. A password pepper §5.49, a secret value mixed into every password hash and kept apart from the stored digests, uses this effect.

Many targets make each success cheaper. If the attacker holds TT different digests and is content with a preimage of any one of them, each candidate has TT chances, and the expected work falls from 2n2^n to about 2n/T2^n / T.

The hash properties are separate. A collision is a pair of different inputs with the same digest, and a practical way to find collisions for a function does not by itself give preimages. RFC 6151, published in 2011 [311], reports that collisions for MD5 (Message Digest 5), an older hash function with a 128-bit digest, can be found in about a minute on a notebook computer, while the best preimage attack that the same document cites still costs about 2123.42^{123.4} operations.

The property says that a whole input is hard to find. It does not promise that a digest hides every partial fact about the input.

Questions answered

How does a low-entropy input dictionary §10.16 change the practical search cost?

A dictionary of low-entropy inputs replaces the cost set by the digest length with the far smaller cost set by the number of likely inputs. A hash function turns any input into a short fixed-length value called the digest. Entropy here means how unpredictable the input is. A low-entropy input is one of only a few likely values, and the dictionary is the attacker’s list of those values. If the hash function is preimage resistant, meaning that nothing better than trial and error is known for finding an input that produces a given digest, a blind search for a digest of nn bits costs about 2n2^n hash computations. An attacker who holds a dictionary of DD candidates hashes the candidates and compares each result with the digest. That costs at most DD computations, and (D+1)/2(D + 1)/2 on average if all candidates are equally likely. For a 4-digit code hashed with SHA-256 (Secure Hash Algorithm with a 256-bit output), the cost is at most 10,000 computations in place of about 22562^{256}.

If the candidates are not equally likely, the attacker tries the most likely first, and the chance of success after qq tries is the sum of the qq largest candidate probabilities. The digest length does not appear in that sum, so a longer digest or a stronger hash function does not help.

Three measures change the cost. A salt §5.19, a random value that is different for each stored digest, hashed together with the input and stored beside the digest, prevents one precomputed table from serving many digests, so each digest costs up to DD computations of its own. A deliberately slow password-hashing function §15.10 multiplies the cost of each guess. A secret key mixed into the hash prevents anyone without the key from testing guesses. Only a less predictable input removes the attack.

Related entries: Entropy and min-entropy (§5.26), Salt (§5.19), Second-preimage resistance (§5.4), Cryptographic hash function (§5.1), Collision resistance (§5.5), Password pepper: a separate secret, compromise cases, and replacement (§5.49), Password hashing and memory-hard KDF (§15.10). Further reading: [7, 310, 311].