Definition
Collision resistance is the property of a hash function that finding two different inputs 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 two different inputs with equal digests are called a collision. Collisions always exist, because there are more possible inputs than digests. The property says that none can be found in practice. The attacker may choose both inputs freely.
The idea
An attacker writes two versions of a contract, one fair and one fraudulent, and adjusts both until they have the same digest. The attacker gives the fair version to a person who approves it with a digital signature §5.14, a value that only the holder of a private key can produce and anyone can check. Most signature schemes compute the signature over the digest of a document, not over the document itself, so the signature is equally valid for the fraudulent version. Nothing was forged and nothing was changed after approval. The attack took place before the signing, when the attacker chose both documents.
Finding such a pair is far easier than matching one given digest, and the reason is counting. A collection of inputs contains pairs, and any pair may be the colliding one. The number of chances grows with the square of the number of inputs. This is the birthday effect §5.6, named after the fact that 23 people are enough for two of them to share a birthday with probability above one half.
How it works
The property is defined by a contest. Write for the hash function.
The attacker is given and nothing else. No input is fixed in advance.
The attacker computes for as long as it can afford and then names two inputs and .
The attacker wins if and .
is collision 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 called the birthday attack.
Choose an input that has not been used and compute its digest.
Look the digest up among the digests computed so far. Keeping them in a hash table §14.7, a structure built for fast lookup, makes this step cheap.
If the digest is already there, output the earlier input and the new one. Otherwise store the digest with its input and go back to step 1.
The first input has nothing to match. The second can match one earlier digest, the third can match two, and input number can match . Model the function as an ideal hash function, one whose digests behave like independent, uniformly random values of bits. A collision then becomes likely when the number of inputs reaches about , the square root of the number of possible digests. Variants of the search need almost no memory, so the size of the table is not what protects a hash function.
The two colliding inputs can be made meaningful. Take a fair document that has many places where a harmless change is possible, such as an extra space or an equivalent word. Thirty-two such places, each with two alternatives, give versions of the document. Do the same with the fraudulent document. With a 64-bit digest, versions of each document form pairs of one fair and one fraudulent version, which is enough to expect one pair with equal digests.
Worked example
Counting pairs. Four inputs , , , form six pairs: , , , , , . The formula gives the same number, . Against one fixed digest these four inputs would have four chances. Against each other they have six. The gap widens quickly: 100 inputs have 100 chances against a fixed digest and pairs among themselves.
A real search. Keep only the first 16 bits of the digest computed by SHA-256 §5.2 (Secure Hash Algorithm with a 256-bit output), which are its first four hexadecimal digits. This toy function has possible digests. Hash the decimal numbers 0, 1, 2 and so on, written as ASCII text, and record each toy digest. The first repeat appears at the input 251: its toy digest c75d was already produced by 157. The full digests begin c75d3f1f for 251 and c75de23d for 157. The collision took 252 hash computations. The theory predicts about on average. Matching the toy digest of one fixed input, the text abc, with the same list of numbers takes 76,793 computations, about 300 times as many.
Full size. For SHA-256 the square root of is , about hash computations. That number, not , is the cost of the generic collision search.
Stated precisely
Let be a hash function with digests of bits and let . A collision is a pair of inputs with Every such function that accepts more than different inputs has collisions: among any different inputs, two must share a digest. Collision resistance is therefore a statement about what anyone knows how to compute. A collision exists, so a program that simply prints it exists too; the claim is that nobody knows such a program or a feasible way to obtain one. Textbooks make this mathematically exact by defining the property for a family of functions selected by a public random parameter.
For an ideal hash function, the probability that different inputs contain at least one collision is where means the product of the factors for to . This probability is at most . For large it is about 0.39 at and passes one half near . The expected number of inputs up to the first collision is about . The generic cost of a collision is therefore about hash computations, and the collision strength of a hash function with an -bit digest is at most bits, where a strength of bits means about operations. NIST Special Publication 800-107 Revision 1 (2012) [310] lists 128 bits for SHA-256 and states that a digest shortened to bits has a collision strength of bits.
Collision resistance implies second-preimage resistance §5.4, the property that nobody can find, for a given input, a different input with the same digest. If someone could find such an input for a given input , the pair would be a collision.
Limits and common mistakes
Counted in bits, collision resistance is the weakest of the three standard hash properties: at most bits, against up to bits for second-preimage resistance and for preimage resistance §5.3, the property that nobody can find an input for a given digest. A digest must therefore be twice as long as the collision strength required.
The birthday figure is an upper limit on the strength, not a promise. A weakness inside a function can make collisions far cheaper. RFC 6151 reports that collisions for MD5 (Message Digest 5), a function with a 128-bit digest, can be found in about a minute on a notebook computer [311], against a generic cost of about hash computations. The first collision for SHA-1 (Secure Hash Algorithm 1), which has a 160-bit digest, was published in 2017 [312] after work equal to about SHA-1 computations, against a generic cost of about . That computation also shows that operations, the generic cost of a collision for a 128-bit digest, are within reach.
A collision is not a preimage and not a second preimage. The search yields two inputs that match each other. It does not yield an input that matches a digest that already exists.
A collision attack on a hash function does not by itself break HMAC §5.8 (hash-based message authentication code), a construction that uses a hash function and a secret key to protect a message against changes. Because the key enters the computation, the attacker cannot compute the digests alone. RFC 6151 states that the attacks on HMAC with MD5 do not seem to indicate a practical vulnerability when HMAC with MD5 is used as a message authentication code §5.7.
The property is needed whenever someone other than the party relying on the digest can influence what is hashed before the digest is fixed. When an honest party alone chose the input, second-preimage resistance is the property at work.
The word collision is also used for hash tables, where it means two keys that fall into the same slot of a small table. That event is expected and the table handles it. A cryptographic collision is two inputs with the same full digest.
Questions answered
Why does the birthday effect reduce generic collision search to about 2^(n/2) work?
Because a collision may occur between any two of the inputs that the attacker hashes, the number of chances is the number of pairs, which grows with the square of the number of inputs. A hash function turns any input into a short fixed-length value called the digest. A collision is a pair of different inputs with the same digest, and is the digest length in bits. Hashing inputs creates pairs. For an ideal hash function, one whose digests behave like independent random values, each pair collides with probability , so the expected number of colliding pairs is . This is about one half when and about 1 when . The work is therefore about hash computations, the square root of the possible digests.
Compare a search against one fixed digest. There each new input has a single chance of , the chances grow only in proportion to , and about inputs are needed. The birthday effect is this difference between counting inputs and counting pairs.
Three words in the question carry conditions. Generic means that the search uses nothing about the inside of the hash function. It therefore applies to every hash function, and a function with an internal weakness can be broken faster. About hides a small constant: the probability of a collision is roughly 0.39 after inputs, and the average search ends after about inputs. Work counts hash computations; the search can be arranged to need very little memory. For SHA-256 (Secure Hash Algorithm with a 256-bit output), and the figure is .
Related entries: Birthday bound (§5.6), Second-preimage resistance (§5.4), SHA-256 (§5.2), Cryptographic hash function (§5.1), Preimage resistance (§5.3), Digital signature (§5.14), Hash collision versus cryptographic collision (§14.8). Further reading: [66, 310, 311, 312].