Definition
A hash compression function is a function that takes two inputs of fixed size, a chaining value of bits and a message block of bits, and returns a new chaining value of 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 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 : encrypt the chaining value under the key , then combine the chaining value into the result by XOR (exclusive or). The final XOR, called the feed-forward, is essential. Without it, could be run backwards: for any chosen output and any two blocks and , decrypting under each of the two keys gives chaining values and with , a collision found without effort. The compression function of SHA-256 has this shape, with addition modulo 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 . The Merkle–Damgård construction §15.8 is the standard arrangement:
Pad the message to a whole number of -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.
Start from a fixed initial chaining value , the initial value (IV), given in the standard.
Iterate: for each block in order, compute .
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 padded blocks takes calls of .
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 , bit rotations §4.50 and bitwise logic functions. Finally each working variable is added modulo 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 , the remainder of after division by 16. Starting from , the blocks 3 and 5 give and . The blocks 4 and 4 give and . 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 has input bits, so possible inputs, and possible outputs; on average inputs share each output. The SHA-256 compression function has input bits and 256 output bits, so on average 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 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 bytes. The next multiple of 64 is 256, so zero bytes fill the gap, and blocks result. SHA-256 calls four times, up to , and is the hash. A 2,000,000-byte message needs at least bytes; since is too small, it takes 31,251 blocks, bytes with zero bytes. That is 31,251 calls of with the same 32-byte chaining value buffer.
Stated precisely
Let be the set of bit strings of length . A compression function is a function with . Given an initial value and a padding rule that turns a message into blocks , the iterated hash is where is the output rule; for SHA-256, returns its input unchanged.
Collision resistance §5.5 of . No feasible algorithm outputs two different pairs with . The chaining values and 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 is collision-resistant, then is collision-resistant. The theorem is a reduction: from any collision of , a collision of can be read off the two computations.
Counting. Because , has collisions. A generic search finds one after about evaluations, so 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 is sufficient for collision resistance of , 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 , 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 -bit chaining value never resists collision search better than about 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 -bit chaining value and a -bit message block and returns a new -bit chaining value; for SHA-256 (Secure Hash Algorithm with a 256-bit output), and . 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 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].