Section 15.9

Hash compression function

Definition

A hash compression function is a function that takes two inputs of fixed size, a chaining value of nn bits and a message block of bb bits, and returns a new chaining value of nn bits. Its output is shorter than its input; that is the only sense in which it compresses, and nothing can be decompressed. Called once per block, it is the building block of iterated hash functions such as SHA-256 §5.2 (Secure Hash Algorithm with a 256-bit output), whose compression function maps 256+512256 + 512 bits to 256 bits.

The idea

A hash function must handle input of any length, but a circuit or a routine handles a fixed amount of data at a time, and a cryptographic design is easiest to study when its input size is fixed. A compression function solves the fixed-size problem: it absorbs one block into a running value, the chaining value, and is then called again for the next block. The chaining value is the only memory of everything read so far.

A running checksum §4.32 that adds up the bytes of a file also handles any length with a total of fixed size, and it shows what goes wrong without careful design: swapping two bytes, or raising one byte and lowering another by the same amount, leaves the total unchanged, so different files collide trivially. A cryptographic compression function mixes the chaining value and the block so thoroughly that no feasible computation finds two inputs with the same output, although such inputs certainly exist.

A common way to build one starts from a block cipher §15.3, an algorithm §14.1 that encrypts one fixed-size block under a key, and uses the message block as the key. The Davies-Meyer construction computes f(h,m)=Em(h)⊕hf(h, m) = E_m(h) \oplus h: encrypt the chaining value hh under the key mm, then combine the chaining value into the result by XOR (exclusive or). The final XOR, called the feed-forward, is essential. Without it, f(h,m)=Em(h)f(h, m) = E_m(h) could be run backwards: for any chosen output yy and any two blocks mm and m′m', decrypting yy under each of the two keys gives chaining values hh and h′h' with f(h,m)=f(h′,m′)=yf(h, m) = f(h', m') = y, a collision found without effort. The compression function of SHA-256 has this shape, with addition modulo 2322^{32} of each 32-bit word in place of the XOR.

How it works

Supporting input of any length takes four rules around the compression function ff. The Merkle–Damgård construction §15.8 is the standard arrangement:

  1. Pad the message to a whole number of bb-bit blocks so that no two messages give the same padded string. SHA-256 appends a 1 bit, zero bits and the message length as a 64-bit number.

  2. Start from a fixed initial chaining value h0h_0, the initial value (IV), given in the standard.

  3. Iterate: for each block mim_i in order, compute hi=f(hi−1,mi)h_i = f(h_{i-1}, m_i).

  4. Output the last chaining value, or a fixed part of it.

The memory needed is constant: one chaining value and one block buffer, whatever the length of the message. The time grows in proportion to the length: a message of ℓ\ell padded blocks takes ℓ\ell calls of ff.

Inside the SHA-256 compression function (FIPS 180-4): the 512-bit block is read as 16 words of 32 bits and expanded into a message schedule of 64 words. Eight 32-bit working variables start as the eight words of the chaining value. Each of 64 rounds updates the working variables using one schedule word, one fixed round constant, additions modulo 2322^{32}, bit rotations §4.50 and bitwise logic functions. Finally each working variable is added modulo 2322^{32} to the matching word of the old chaining value, the feed-forward, and the eight sums form the new chaining value.

Worked example

A toy that fails. Let the chaining value and the block be 4-bit numbers, 0 to 15, and let f(h,m)=(h+m)mod16f(h, m) = (h + m) \bmod 16, the remainder of h+mh + m after division by 16. Starting from h0=0h_0 = 0, the blocks 3 and 5 give h1=3h_1 = 3 and h2=(3+5)mod16=8h_2 = (3 + 5) \bmod 16 = 8. The blocks 4 and 4 give h1=4h_1 = 4 and h2=(4+4)mod16=8h_2 = (4 + 4) \bmod 16 = 8. Two different inputs reach the same chaining value: a collision, found by inspection. A final block holding the length does not help, because both inputs have the same length, get the same final block and already have equal chaining values before it.

Collisions always exist. The toy ff has 4+4=84 + 4 = 8 input bits, so 28=2562^{8} = 256 possible inputs, and 24=162^{4} = 16 possible outputs; on average 256/16=16256 / 16 = 16 inputs share each output. The SHA-256 compression function has 256+512=768256 + 512 = 768 input bits and 256 output bits, so on average 2768/2256=25122^{768} / 2^{256} = 2^{512} inputs share each output. Collisions exist in vast numbers. Security means only that nobody can find one: a generic search, which relies on the birthday bound §5.6, needs about 2256/2=21282^{256/2} = 2^{128} evaluations, and no faster method is publicly known for the full SHA-256 compression function.

Any length. A 200-byte message with the 1-byte 0x80 and the 8-byte length needs 200+1+8=209200 + 1 + 8 = 209 bytes. The next multiple of 64 is 256, so 256−209=47256 - 209 = 47 zero bytes fill the gap, and 256/64=4256 / 64 = 4 blocks result. SHA-256 calls ff four times, h1=f(h0,m1)h_1 = f(h_0, m_1) up to h4=f(h3,m4)h_4 = f(h_3, m_4), and h4h_4 is the hash. A 2,000,000-byte message needs at least 2,000,000+9=2,000,0092{,}000{,}000 + 9 = 2{,}000{,}009 bytes; since 31,250×64=2,000,00031{,}250 \times 64 = 2{,}000{,}000 is too small, it takes 31,251 blocks, 31,251×64=2,000,06431{,}251 \times 64 = 2{,}000{,}064 bytes with 2,000,064−2,000,009=552{,}000{,}064 - 2{,}000{,}009 = 55 zero bytes. That is 31,251 calls of ff with the same 32-byte chaining value buffer.

Stated precisely

Let {0,1}j\{0,1\}^{j} be the set of bit strings of length jj. A compression function is a function f:{0,1}n×{0,1}b→{0,1}nf: \{0,1\}^{n} \times \{0,1\}^{b} \to \{0,1\}^{n} with b>0b > 0. Given an initial value IV\mathrm{IV} and a padding rule that turns a message MM into blocks m1,…,mℓm_1, \dots, m_\ell, the iterated hash is h0=IV,hi=f(hi−1,mi) for 1≤i≤ℓ,H(M)=g(hℓ),h_0 = \mathrm{IV}, \qquad h_i = f(h_{i-1}, m_i) \text{ for } 1 \le i \le \ell, \qquad H(M) = g(h_\ell), where gg is the output rule; for SHA-256, gg returns its input unchanged.

Collision resistance §5.5 of ff. No feasible algorithm outputs two different pairs (h,m)≠(h′,m′)(h, m) \ne (h', m') with f(h,m)=f(h′,m′)f(h, m) = f(h', m'). The chaining values hh and h′h' may be chosen freely, so a collision found with chosen chaining values, called a pseudo-collision, already violates this property.

Merkle–Damgård theorem. If the padding ends with the message length and ff is collision-resistant, then HH is collision-resistant. The theorem is a reduction: from any collision of HH, a collision of ff can be read off the two computations.

Counting. Because 2n+b>2n2^{n+b} > 2^{n}, ff has collisions. A generic search finds one after about 2n/22^{n/2} evaluations, so nn must be at least twice the desired security level §7.33 in bits.

Limits and common mistakes

Compression here is not data compression. The input cannot be recovered from the output, and the output has the same size for every input.

Collision resistance of ff is sufficient for collision resistance of HH, not necessary. The compression function of MD5 (Message Digest 5) had pseudo-collisions, published by den Boer and Bosselaers in 1993 [377], more than ten years before collisions of MD5 itself were published in 2004 [376].

The chaining value is neither secret nor a key. Anyone can compute the chaining values of a public message, and after the last block the chaining value is the hash output itself. That is why a Merkle–Damgård hash allows a length-extension attack §5.11 against a secret key placed in front of the message.

Other properties of HH, such as preimage resistance §5.3 (the difficulty of finding an input for a given output), need their own arguments; the Merkle–Damgård theorem covers collisions only.

Not every hash function is built from a compression function in this way. SHA-3 is a sponge construction: it applies a fixed permutation to a 1600-bit state and absorbs each block into part of the state, keeping the rest hidden.

Iteration cannot add strength. A hash built from an nn-bit chaining value never resists collision search better than about 2n/22^{n/2} evaluations.

Questions answered

How does a fixed-size compression function support arbitrary-length input?

A fixed-size compression function supports input of any length by being called once per block, with a fixed-size chaining value that carries the result of all earlier blocks into the next call. The compression function takes an nn-bit chaining value and a bb-bit message block and returns a new nn-bit chaining value; for SHA-256 (Secure Hash Algorithm with a 256-bit output), n=256n = 256 and b=512b = 512. The hash pads the message to a whole number of blocks, ending with the message length; starts from a fixed initial chaining value; for each block in turn replaces the chaining value with the compression function of the chaining value and the block; and outputs the last chaining value. Memory stays constant and time grows with the number of blocks: a 200-byte message, for example, pads to 256 bytes, four blocks, and takes four calls.

The padding and the fixed start are parts of the hash, not extras. The length at the end of the padding is what lets a collision of the whole hash be traced back to a collision of the compression function (the Merkle–Damgård theorem). The only limit on the length is the size of the length field: SHA-256 accepts messages shorter than 2642^{64} bits.

Related entries: Merkle–Damgård construction (§15.8), Cryptographic hash function (§5.1), Collision resistance (§5.5), Birthday bound (§5.6), SHA-256 (§5.2). Further reading: [7, 376, 377].