Section 14.8

Hash collision versus cryptographic collision

Definition

A hash collision in a hash table §14.7 means that the table’s hash function, reduced to a bucket number, gives two different lookup keys the same bucket. A cryptographic collision means that two different inputs give the same output of a cryptographic hash function §5.1, such as SHA-256 §5.2 (Secure Hash Algorithm with a 256-bit output), designed to make such pairs infeasible to find. The difference: a table expects collisions and resolves them by comparing keys, costing only time; a cryptographic collision breaks a security guarantee.

Where it fits

Main entry: Collision resistance (§5.5).

Table collisions cannot be avoided: a table with mm buckets and more than mm possible keys must give two keys the same bucket, by the pigeonhole principle. As the Hash table entry explains, the table resolves them by comparing full keys, so they never change a lookup’s answer. A cryptographic hash function is judged by collision resistance instead: no feasible computation finds two inputs with equal outputs. Its output is long for that reason; by the birthday bound §5.6, a generic collision search on a bb-bit output needs about 2b/22^{b/2} evaluations, 21282^{128} for SHA-256.

A table hash function is chosen for speed. A common one is the Fowler–Noll–Vo hash, FNV, named after its designers Glenn Fowler, Landon Curt Noll and Kiem-Phong Vo. Its 32-bit variant FNV-1a starts from 2166136261 (0x811c9dc5) and, for each input byte, combines the byte into the low 8 bits with a bitwise XOR (exclusive or: each result bit is 1 where the two input bits differ), then multiplies by 16777619 (0x01000193), keeping the low 32 bits. For the input a, the byte 0x61, the XOR gives 0x811c9da4; multiplying gives 0x811d68e40c292c, whose low 32 bits, 0xe40c292c, are the result. The variant FNV-1 multiplies before the XOR.

FNV-1a uses no secret and has a short output, so anyone can search it for collisions: the words costarring and liquid share the 32-bit FNV-1a value 0x5e4daa9d. For any function whose 32-bit outputs behave like random values, about 77,000 random inputs give an even chance that two share an output, and about 2322^{32}, or 4.3 billion, attempts on average find an input with a chosen output. Both are small computations for one desktop computer.

What a collision costs depends on what the table stores. If it stores and compares full keys, colliding keys keep separate entries and only add comparisons; many deliberate collisions slow the table, an algorithmic complexity attack §15.35. If it stores only the 32-bit value as an entry’s identifier, two colliding inputs share one entry, and each reads or overwrites the other’s state. An attacker who may choose an input, such as a user name, can compute offline one whose FNV-1a value equals a victim’s.

Limits and common mistakes

A cryptographic hash function without a secret does not stop deliberate bucket collisions: the bucket index keeps only about log2m\log_2 m bits, so for a 1024-bucket table about one input in 1024 lands in any chosen bucket. A secret random value mixed into the hash stops this, as in SipHash, a fast table hash function that takes a secret key.

No hash function authenticates data by itself. That needs a secret, as in a message authentication code §5.7 (MAC) such as HMAC §5.8 (hash-based message authentication code), or a digital signature §5.14. A public hash such as SHA-256 detects changes only if the expected output reaches the checker by a path the attacker cannot alter.

Questions answered

Why can a good hash table use a function unsuitable for authentication §1.6?

A hash table can use a fast function without security properties because it never relies on the hash to tell keys apart. It uses the hash only to choose where to look, and it compares the stored keys themselves before reporting a match, so a collision, two keys with the same bucket, costs extra comparisons and never a wrong answer. The table needs only speed and an even spread of the keys it receives, which a simple function such as FNV-1a, the Fowler–Noll–Vo hash, provides.

Authentication, confirming that data comes from the claimed sender unchanged, needs the opposite: no one without a secret key may be able to produce data that passes the check. FNV-1a has no secret key and a 32-bit output, so anyone can compute it for altered data or find two inputs with the same output; authentication needs a message authentication code (MAC) such as HMAC, or a digital signature. One caution remains: when an attacker chooses the keys, a table whose hash uses no secret can be flooded with colliding keys, so tables that take untrusted input use a hash with a secret random value, such as SipHash.

Related entries: Hash table (§14.7), Collision resistance (§5.5), Message authentication code (MAC) (§5.7), Integrity (§1.9), Birthday bound (§5.6), Algorithmic complexity attack (§15.35), Public key fingerprints and truncated identifiers (§19.3.1).