Chapter 06 · Hash functions
Fingerprints of data
A hash function squeezes any input, a word or a whole disk, into a short fingerprint that changes completely if a single bit changes. Fingerprints protect downloads, passwords, signatures, Git and Bitcoin. Breaking them means finding two inputs with the same fingerprint, and the birthday paradox says that is far easier than it sounds.
In this chapter
A cryptographic hash function maps an input of any length to an output of fixed length , typically 256 bits, called the digest. There is no key. It must be fast to compute and, in a precise sense, impossible to steer: the digest of SHA-256 for “abc” is ba7816bf…, and for “abd” something with no visible relation to it. Hashes are the most used cryptographic primitive of all: they identify Git commits, the blocks of Bitcoin's chain, the files of software updates; they are inside every signature, every password database and every TLS handshake.
Three kinds of resistance
- Preimage resistance (one-wayness): given a digest , it is infeasible to find any with .
- Second-preimage resistance: given , it is infeasible to find with .
- Collision resistance: it is infeasible to find any pair with .
Collisions must exist, since infinitely many inputs share outputs; the requirement is that nobody can find one. For an ideal -bit hash, finding a preimage takes about tries. Finding a collision takes far fewer.
The avalanche
A good hash behaves like a random function: every output bit depends on every input bit, and changing the input flips each output bit with probability one half, independently of the others.
The birthday paradox
In a room of 23 people, the probability that two share a birthday is more than one half. The paradox, which is not a paradox, is that we compare pairs, and 23 people make 253 pairs. The same arithmetic governs collisions.
If values are drawn independently and uniformly from a set of elements, the probability that two of them coincide is
It reaches one half at , and the expected number of draws until the first collision is . For an -bit hash, : collisions appear after about hashes.
Proof of the approximation
The -th draw avoids the earlier ones with probability , so no collision happens with probability . Using , this is at most , and for the approximation is tight. Setting it to gives , that is .
So a 128-bit hash offers only 64 bits of collision resistance, within reach of a large computation; that is why modern hashes have at least 256 bits. The same bound limits 64-bit block ciphers (Sweet32) and random nonces.
Merkle–Damgård
How do you hash an input of any length with a function of fixed size? Ralph Merkle (in his 1979 thesis) and Ivan Damgård (1989) independently found the construction behind MD5, SHA-1 and SHA-2. Take a compression function that maps a chaining value and a message block to a new chaining value. Pad the message (with its length at the end), cut it into blocks , and iterate:
If the compression function is collision resistant, so is the iterated hash (with length padding).
Proof
Suppose with . If their lengths differ, the last blocks differ (they encode the lengths), and is already a collision of . Otherwise walk back from the end: at the first step (from the end) where but of both is equal, we have a collision of . Such a step exists because the inputs differ somewhere.
The construction has a quirk: is the internal state after processing , so anyone who knows and the length of , but not itself, can compute for any . This length-extension attack breaks the naive authentication tag , as Flickr's API learned in 2009, and is the reason for HMAC.
The fall of MD5 and SHA-1
Ron Rivest designed MD5 in 1992; the NSA designed SHA-1 (160 bits) in 1995. In August 2004 the Chinese cryptographer Xiaoyun Wang and her team presented collisions for MD5 and several related hashes, computed in hours; a year later they showed that SHA-1 was much weaker than its 80-bit birthday bound. The consequences took years to play out:
- 2008: Marc Stevens, Alexander Sotirov and colleagues used an MD5 collision to create a rogue certificate authority trusted by every browser.
- 2012: the Flame malware, attributed to state actors, forged a Microsoft code-signing certificate with a new MD5 collision attack and spread through Windows Update.
- 2017: Google and CWI Amsterdam announced SHAttered, two different PDF files with the same SHA-1, after SHA-1 computations (about 6,500 CPU-years and 110 GPU-years).
- 2020: Gaëtan Leurent and Thomas Peyrin's “SHA-1 is a Shambles” produced a chosen-prefix collision for about $45,000 of GPU time, enough to impersonate PGP keys.
Git, which identified everything by SHA-1, added collision detection in 2017 and supports SHA-256 repositories since 2020. The desktop app keeps MD5 and SHA-1 for checking old checksums, marked as broken.
SHA-2, SHA-3 and BLAKE
SHA-2 (2001: SHA-224, SHA-256, SHA-384, SHA-512) is also Merkle–Damgård, with a much stronger compression function, and remains unbroken. But after Wang's attacks NIST wanted a backup built on different principles, and in 2007 opened another public competition. Sixty-four candidates were submitted; in 2012 NIST chose Keccak, by Guido Bertoni, Joan Daemen (co-designer of AES), Michaël Peeters and Gilles Van Assche, published as SHA-3 in FIPS 202 (2015).
Keccak is a sponge. Its state of 1,600 bits is split into a rate of bits and a capacity of bits. In the absorbing phase each -bit block of the message is XORed into the rate part and the whole state is permuted by a fixed permutation ; in the squeezing phase the output is read bits at a time from the rate part, permuting in between. The capacity is never touched directly by the input or the output.
If is a random permutation, the sponge cannot be distinguished from a random oracle with fewer than about calls. SHA3-256 uses , so its generic security is against everything except the birthday bound of its 256-bit output, .
Because the capacity is hidden, a sponge has no length extension. Keccak's structure also gives extendable-output functions (SHAKE128, SHAKE256), used inside the post-quantum standards. BLAKE2 (2012), derived from the SHA-3 finalist BLAKE by Jean-Philippe Aumasson and colleagues, is faster than MD5 in software and as secure as SHA-3; BLAKE3 (2020) organises the computation as a Merkle tree, so it hashes in parallel on every core.
HMAC: a hash with a key
A hash proves nothing about who made a message: anyone can recompute it. A message authentication code mixes in a secret key, so only those who know the key can produce or check the tag. Mihir Bellare, Ran Canetti and Hugo Krawczyk designed HMAC in 1996 to be safe with Merkle–Damgård hashes:
with two fixed constants and . The outer hash hides the inner state, which defeats length extension, and the construction is provably a pseudorandom function if the compression function is. HMAC authenticates TLS records, JSON Web Tokens (HS256), AWS API requests and one-time passwords (TOTP). Verification must compare tags in constant time, or the time taken reveals how many bytes matched; the desktop app does.
Further reading
- Ralph Merkle, Secrecy, Authentication and Public Key Systems, PhD thesis, Stanford (1979); Ivan Damgård, “A Design Principle for Hash Functions”, CRYPTO 1989.
- Xiaoyun Wang and Hongbo Yu, “How to Break MD5 and Other Hash Functions”, EUROCRYPT 2005.
- Marc Stevens et al., “The first collision for full SHA-1”, CRYPTO 2017 (shattered.io).
- Guido Bertoni, Joan Daemen, Michaël Peeters, Gilles Van Assche, “On the Indifferentiability of the Sponge Construction”, EUROCRYPT 2008.
- Mihir Bellare, Ran Canetti, Hugo Krawczyk, “Keying Hash Functions for Message Authentication”, CRYPTO 1996.