Section 5.4

Second-preimage resistance

Definition

Second-preimage resistance is the property of a hash function that, given one input, finding a different input with the same 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 second preimage of an input is any other input with the same digest. In collision resistance §5.5, the nearest related property, the attacker may choose both inputs. Here the first input is fixed in advance.

The idea

A publisher releases a program and announces its digest in a place the attacker cannot change, for example built into software that users already trust. Users check each copy they download against that digest. An attacker who wants users to accept a modified program must produce a different file with exactly the announced digest. The attacker did not write the original and cannot change it. This task is finding a second preimage, and second-preimage resistance means that it cannot be done.

A room full of people shows why a fixed target is hard to hit. Ask whether someone else in the room shares your birthday. With 22 other people the chance is 1−(364/365)22≈0.0591 - (364/365)^{22} \approx 0.059, about 6%, and it takes 253 other people to reach one half. Now ask whether any two people in a room of 23 share a birthday. That chance is already about 0.507. The second question is easy because any of the 23×22/2=25323 \times 22 / 2 = 253 pairs may match. The first is hard because only the pairs that include you count. Finding a second preimage is the first question: one side of the match is fixed.

How it works

The property is defined by a contest. Write HH for the hash function.

  1. An input xx is chosen without any influence from the attacker and is given to the attacker. The attacker can therefore compute the digest H(x)H(x).

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

  3. The attacker wins if x′≠xx' \neq x and H(x′)=H(x)H(x') = H(x).

HH is second-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 tries candidate inputs one at a time and compares the digest of each with H(x)H(x). Model the function as an ideal hash function, one whose digests behave like independent, uniformly random values of nn bits. Each candidate then matches with probability 1/2n1/2^n, and the search takes about 2n2^n hash computations on average. Matches among the attacker’s own candidates do not help: two candidates with equal digests are a collision, but neither has the digest of xx.

Knowing xx, and not just its digest, gives the attacker one known advantage, and only for very long inputs. A function such as SHA-256 §5.2 (Secure Hash Algorithm with a 256-bit output) processes its input block by block and passes through one intermediate state for each block. An attacker can aim at any of the intermediate states of a long xx, and not only at the final digest, which gives more targets. The part headed Stated precisely gives the size of this effect.

Worked example

A function without the property. Define a toy digest as the sum of all bytes of the input, keeping only the lowest 8 bits. The ASCII message pay 19 has the bytes 70 61 79 20 31 39 in hexadecimal. Their sum is 0x1d4, so the toy digest is 0xd4. The message pay 91 contains the same bytes in a different order and has the same digest. A second preimage of pay 19 was found without any search, and it states a different amount.

A cut-down cryptographic function. Keep only the first 16 bits of the SHA-256 digest, which are its first four hexadecimal digits. This toy function has 216=65,5362^{16} = 65{,}536 possible digests. The toy digest of the ASCII text abc is ba78. To find a second preimage, hash the decimal numbers 0, 1, 2 and so on, written as ASCII text, until one has the toy digest ba78. The search ends at 76792, after 76,793 hash computations. The expected number is 65,536, so this run took slightly longer than average. The full SHA-256 digest of 76792 begins ba781f7b and that of abc begins ba7816bf: the first 16 bits agree and the rest do not.

Scale. Each additional digest bit doubles the expected work. For all 256 bits it is 2256≈1.16×10772^{256} \approx 1.16 \times 10^{77} hash computations. For comparison, the same list of inputs contains two entries with equal toy digests, 157 and 251, after only 252 hash computations. Any equal pair is far easier to find than a match with one fixed input.

Stated precisely

Let HH be a hash function with digests of nn bits and let N=2nN = 2^n. A second preimage of an input xx is an input x′x' such that x′≠xandH(x′)=H(x).x' \neq x \quad \text{and} \quad H(x') = H(x). Both conditions are needed. An unchanged copy of xx has the same digest and is not a second preimage. For an ideal hash function, an attacker that hashes qq different candidates finds a second preimage with probability 1−(1−1/N)q1 - (1 - 1/N)^q, which is at most q/Nq/N.

NIST Special Publication 800-107 Revision 1 (2012) [310] gives the expected second-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, whose digest is its complete internal state, the strength falls to about 256−M256 - M bits when the given message is 2M2^M blocks long. SHA-256 has 512-bit blocks. A message of 2332^{33} bits, which is 1 GiB, has 233/29=2242^{33} / 2^{9} = 2^{24} blocks, so the strength is 256−24=232256 - 24 = 232 bits. The longest permitted message, just under 2642^{64} bits, has about 2552^{55} blocks, which gives 201 bits, the low end of the range of 201 to 256 bits that NIST lists.

A function that is collision resistant is also second-preimage resistant, because an attacker who finds a second preimage x′x' of xx has found the collision (x,x′)(x, x'). The reverse does not follow. With a 100-bit digest, for example, the generic second-preimage search costs about 21002^{100} hash computations and the generic collision search only about 2502^{50}.

Limits and common mistakes

The property protects a check only when the reference digest can be trusted. An attacker who can replace the announced digest needs no second preimage.

The property covers a first input that the attacker did not choose. If the attacker can influence the first input before its digest is fixed, for example by writing the document that someone else will approve, the attacker prepares two inputs with the same digest in advance. The property that then matters is collision resistance, whose strength is only half the digest length.

A collision attack on a function is not a second-preimage attack. RFC 6151, published in 2011, 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 [311]. Yet in its section on preimage and second-preimage resistance the same document cites only a preimage attack, at a cost of about 2123.42^{123.4} operations, which is far out of reach. RFC 6151 still rules MD5 out wherever collision resistance is required.

Several targets help the attacker. With rr given inputs and the aim of matching any one of them, the chance of each candidate is rr times as high.

The property is about bytes. It does not tell the receiver that the matching object is the newest one or that it is safe to use.

Questions answered

How does this differ from finding any collision?

In a second-preimage attack one of the two inputs is fixed in advance, outside the attacker’s control, and the attacker must find a different input with the same digest. In a collision attack the attacker chooses both inputs freely and needs only that their two digests are equal to each other. The digest is the short fixed-length output of a hash function.

This freedom changes the cost. Against a fixed input, each candidate has one chance in 2n2^n of matching a digest of nn bits, so the generic search costs about 2n2^n hash computations. In a collision search every pair of candidates is a chance, and qq candidates form q(q−1)/2q(q-1)/2 pairs, so the generic search costs only about 2n/22^{n/2} computations. For SHA-256 (Secure Hash Algorithm with a 256-bit output) the two figures are about 22562^{256} and 21282^{128}.

Which property a system needs depends on who chose the first input and when. If an honest party created the object and fixed its digest before the attacker could act, replacing the object requires a second preimage. If the attacker can supply or shape the object before its digest is fixed, the attacker can prepare two objects with the same digest, have the harmless one approved, and later present the other. That is a collision attack. Collision resistance is the stronger requirement: a function that has it also has second-preimage resistance, but a function can have second-preimage resistance without it.

Related entries: Preimage resistance (§5.3), Collision resistance (§5.5), Integrity (§1.9), Cryptographic hash function (§5.1), Birthday bound (§5.6). Further reading: [7, 310, 311].