Section 15.8

Merkle–Damgård construction

Definition

The Merkle–Damgård construction is a method for building a hash function that accepts messages of any length from a hash compression function §15.9, a function that maps a fixed-size chaining value and a fixed-size message block to a new chaining value. The message is padded with its own length and split into blocks, which are fed to the compression function one at a time, starting from a fixed initial value. The last chaining value is the hash. Ralph Merkle and Ivan Damgård described the construction independently in 1989 [373, 374].

The idea

A hash function must accept inputs of any length and return a short value of fixed size. One mixing function that handles every length directly is hard to design and hard to analyze. A function with one fixed input size is far easier: for SHA-256 §5.2 (Secure Hash Algorithm with a 256-bit output), cryptographers study a single function with 768 bits in and 256 bits out.

The Merkle–Damgård construction reduces the general problem to the fixed-size one. Picture reading a long document while keeping a summary on one card of fixed size: after each page, a new card is written from the old card and the page, and the old card is thrown away. The card after the last page is the hash. The picture stops being accurate in one respect: a chaining value is a scrambled value that reveals nothing readable about the pages.

The padding ends with the length of the message. Merkle and Damgård needed the length there for their proof: two messages of different lengths then have different last blocks, so a collision of the hash, two different messages with the same hash, always shows up as a collision of the compression function. Adding the length is called Merkle–Damgård strengthening.

The construction has one structural weakness: the hash value is the complete internal state at the end, so anyone who knows the hash of a message and its length can continue the computation with more blocks. The length in the padding does not prevent this, which is why hashing a secret key followed by a message does not give a secure MAC (message authentication code §5.7); the Length-extension attack §5.11 entry works through that attack with real values.

How it works

The parts, with the sizes SHA-256 uses:

The steps:

  1. Pad. Append one 1 bit, which for a message of whole bytes is the byte 0x80. Append zero bits until the length is 64 bits short of a multiple of 512 bits. Append the original message length in bits as a 64-bit big-endian §4.21 number, most significant byte first.

  2. Split the padded message into 512-bit blocks m1,m2,…,mℓm_1, m_2, \dots, m_\ell.

  3. Start with the chaining value h0=IVh_0 = \mathrm{IV}.

  4. Iterate: for i=1,2,…,ℓi = 1, 2, \dots, \ell, compute hi=f(hi−1,mi)h_i = f(h_{i-1}, m_i).

  5. Output hℓh_\ell.

Each step reads only the current chaining value and one block, so a hash can be computed over a stream of data with 32 bytes of chaining value, one 64-byte block buffer and a length counter, whatever the length of the message. The same fact is the weakness: the chaining value after any whole-block prefix of the padded message is all that is needed to go on.

Worked example

One block. The 3-byte ASCII message abc is 3×8=243 \times 8 = 24 bits long. The padding appends the byte 0x80, then zz zero bytes, then 8 length bytes, so that 3+1+z+8=643 + 1 + z + 8 = 64, which gives z=52z = 52. The 8 length bytes hold 24, which is 0x18: 00 00 00 00 00 00 00 18. The padded message is one block, so SHA-256 calls ff once, h1=f(IV,m1)h_1 = f(\mathrm{IV}, m_1), and h1h_1 is the hash:

ba7816bf8f01cfea414140de5dae2223
b00361a396177a9cb410ff61f20015ad

Two blocks. A 60-byte message is 60×8=48060 \times 8 = 480 bits long. The smallest possible padding, 1 byte 0x80 and 8 length bytes, already gives 60+1+8=6960 + 1 + 8 = 69 bytes, more than 64, so the padding runs into a second block: 128−60−1−8=59128 - 60 - 1 - 8 = 59 zero bytes, and 60+1+59+8=12860 + 1 + 59 + 8 = 128 bytes, which is 128/64=2128 / 64 = 2 blocks. The length field holds 480 (0x1e0), the length of the original message, not of the padded one. The longest message that fits in one block has 55 bytes: 55+1+0+8=6455 + 1 + 0 + 8 = 64.

Continuing from a hash. Let SS be a 16-byte secret key followed by a 20-byte message, 36 bytes in all, which is 36×8=28836 \times 8 = 288 bits (0x120). Its padding GG is 0x80, 64−36−1−8=1964 - 36 - 1 - 8 = 19 zero bytes and the 8 length bytes 00 00 00 00 00 00 01 20, so S‖GS \mathbin{\Vert} G, meaning SS followed by GG, is exactly one block, and the hash of SS is H(S)=f(IV,S‖G)H(S) = f(\mathrm{IV}, S \mathbin{\Vert} G). Now append any 10 bytes XX. The message S‖G‖XS \mathbin{\Vert} G \mathbin{\Vert} X is 64+10=7464 + 10 = 74 bytes, which is 74×8=59274 \times 8 = 592 bits (0x250). Its first block is S‖GS \mathbin{\Vert} G, and its second block is XX, the byte 0x80, 64−10−1−8=4564 - 10 - 1 - 8 = 45 zero bytes and the length 592. So its hash is f(f(IV,S‖G),second block)=f(H(S),second block)f(f(\mathrm{IV}, S \mathbin{\Vert} G), \text{second block}) = f(H(S), \text{second block}). Every input of that last call is known to someone who knows H(S)H(S) and the length of SS but not SS itself.

Stated precisely

Let {0,1}j\{0,1\}^{j} be the set of bit strings of length jj, and let f:{0,1}n×{0,1}b→{0,1}nf: \{0,1\}^{n} \times \{0,1\}^{b} \to \{0,1\}^{n} be the compression function; SHA-256 has n=256n = 256 and b=512b = 512. Let IV\mathrm{IV} be a fixed nn-bit value. For a message MM of |M|<264|M| < 2^{64} bits, let pad(M)=M‖1‖0k‖⟨|M|⟩64,\mathrm{pad}(M) = M \mathbin{\Vert} 1 \mathbin{\Vert} 0^{k} \mathbin{\Vert} \langle |M| \rangle_{64}, where ‖\Vert joins bit strings, 0k0^{k} is kk zero bits with kk the smallest whole number ≥0\ge 0 that makes |M|+1+k+64|M| + 1 + k + 64 a multiple of bb, and ⟨|M|⟩64\langle |M| \rangle_{64} is |M||M| written as a 64-bit binary number. Split pad(M)\mathrm{pad}(M) into blocks m1,…,mℓm_1, \dots, m_\ell of bb bits. Then h0=IV,hi=f(hi−1,mi) for 1≤i≤ℓ,H(M)=hℓ.h_0 = \mathrm{IV}, \qquad h_i = f(h_{i-1}, m_i) \text{ for } 1 \le i \le \ell, \qquad H(M) = h_\ell.

Collision resistance §5.5 carries over. Suppose M≠M′M \ne M' and H(M)=H(M′)H(M) = H(M'). If |M|≠|M′||M| \ne |M'|, the last blocks differ in their length fields, so the two last calls of ff have different inputs and the same output: a collision of ff. If |M|=|M′||M| = |M'|, both messages have ℓ\ell blocks; walk backwards from the last call until reaching a call whose inputs differ. That call’s outputs are equal, because every later call had equal inputs. Such a call exists because the messages differ. So whoever finds a collision of HH has found a collision of ff, and if no feasible computation finds a collision of ff, none finds one of HH.

Extension identity. For every message MM, write pad(M)=M‖G\mathrm{pad}(M) = M \mathbin{\Vert} G. For every bit string XX, H(M‖G‖X)H(M \mathbin{\Vert} G \mathbin{\Vert} X) equals the result of the same iteration started from H(M)H(M) in place of IV\mathrm{IV} and run over the blocks of X‖1‖0k′‖⟨|M|+|G|+|X|⟩64X \mathbin{\Vert} 1 \mathbin{\Vert} 0^{k'} \mathbin{\Vert} \langle |M| + |G| + |X| \rangle_{64}, with k′k' chosen as above. Only H(M)H(M) and |M||M|, which determines GG, are needed.

Limits and common mistakes

The proof shows that collision resistance carries over from ff to HH. Other properties do not carry over automatically. The extension identity is a property that a randomly chosen function would not have, so a Merkle–Damgård hash must not be treated as a random function of its whole input.

A secret-prefix tag H(K‖M)H(K \mathbin{\Vert} M), with a secret key KK, is insecure for every Merkle–Damgård hash that outputs its whole chaining value. HMAC §5.8 (hash-based message authentication code) is the standard repair: its outer hash call takes the inner hash as a short message under a second key, so the inner chaining value never appears in the output.

Hashes that output only part of their final state force an extender to guess the hidden part. SHA-384 and SHA-224, members of the same family as SHA-256, are examples: SHA-384 hides 128 of its 512 state bits, far too many to guess, while SHA-224 hides only 32 of its 256, so one guess of the hidden bits is right with probability 2−322^{-32}. SHA-3 is not a Merkle–Damgård hash; it uses a sponge construction that keeps part of its state hidden by design.

Even with an ideal compression function, every Merkle–Damgård hash has generic weaknesses that a random function would not have. For example, Joux showed in 2004 [375] that finding 2k2^{k} messages that all have the same hash costs only about kk times the work of finding one collision, because kk successive block collisions, one after another along the chain, can be combined in 2k2^{k} ways.

The theorem needs collision resistance of ff for every chaining value, including values that an attacker chooses. Collisions of ff with chosen chaining values, called pseudo-collisions, void the proof, though not necessarily the hash.

The Merkle–Damgård construction is a chain and is unrelated to a Merkle tree §5.43, which hashes data pairwise up a tree so that one item can be proved to belong to the data. The length field also limits SHA-256 to messages shorter than 2642^{64} bits.

Questions answered

Why does padding with a length not make a secret-prefix hash a secure MAC?

Padding with the length does not make a secret-prefix hash H(K‖M)H(K \mathbin{\Vert} M), the hash of a secret key KK followed by a message MM, a secure MAC (message authentication code), because the padding is public and computable and the hash value is the complete internal state after the padded input. In a Merkle–Damgård hash such as SHA-256 (Secure Hash Algorithm with a 256-bit output), the input is padded with a 1 bit, zero bits and its length, and processed block by block by a compression function; the output is the last chaining value.

An attacker who sees the tag t=H(K‖M)t = H(K \mathbin{\Vert} M) and knows or guesses the length of KK can compute the padding GG that the hash appended, start the hash computation with tt as its state, and process extra data XX with a final length field that counts all of K‖M‖G‖XK \mathbin{\Vert} M \mathbin{\Vert} G \mathbin{\Vert} X. The result is the correct hash of that longer input, so the message M‖G‖XM \mathbin{\Vert} G \mathbin{\Vert} X has a valid tag that the key holder never computed. The length field does not stop this: in the forged message the old padding is ordinary message data, and the new length field is simply the new total. The padding exists to make the encoding of every message unambiguous, and the length in it lets collision resistance carry over from the compression function; neither hides the state. A secure MAC needs a construction that keeps the state secret, such as HMAC (hash-based message authentication code).

Related entries: Hash compression function (§15.9), Length-extension attack (§5.11), HMAC (§5.8), Merkle trees and authenticated data structures (§5.43), Collision resistance (§5.5), SHA-256 (§5.2). Further reading: [7, 8, 373, 374, 375].