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 compression function , which takes a 256-bit chaining value and a 512-bit (64-byte) message block and returns a new 256-bit chaining value;
the initial value (IV), a fixed 256-bit constant written in the standard, FIPS 180-4;
the padding rule;
the output rule: SHA-256 outputs the last chaining value unchanged.
The steps:
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.Split the padded message into 512-bit blocks .
Start with the chaining value .
Iterate: for , compute .
Output .
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 bits long. The padding appends the byte 0x80, then zero bytes, then 8 length bytes, so that , which gives . 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 once, , and is the hash:
ba7816bf8f01cfea414140de5dae2223
b00361a396177a9cb410ff61f20015ad
Two blocks. A 60-byte message is bits long. The smallest possible padding, 1 byte 0x80 and 8 length bytes, already gives bytes, more than 64, so the padding runs into a second block: zero bytes, and bytes, which is 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: .
Continuing from a hash. Let be a 16-byte secret key followed by a 20-byte message, 36 bytes in all, which is bits (0x120). Its padding is 0x80, zero bytes and the 8 length bytes 00 00 00 00 00 00 01 20, so , meaning followed by , is exactly one block, and the hash of is . Now append any 10 bytes . The message is bytes, which is bits (0x250). Its first block is , and its second block is , the byte 0x80, zero bytes and the length 592. So its hash is . Every input of that last call is known to someone who knows and the length of but not itself.
Stated precisely
Let be the set of bit strings of length , and let be the compression function; SHA-256 has and . Let be a fixed -bit value. For a message of bits, let where joins bit strings, is zero bits with the smallest whole number that makes a multiple of , and is written as a 64-bit binary number. Split into blocks of bits. Then
Collision resistance §5.5 carries over. Suppose and . If , the last blocks differ in their length fields, so the two last calls of have different inputs and the same output: a collision of . If , both messages have 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 has found a collision of , and if no feasible computation finds a collision of , none finds one of .
Extension identity. For every message , write . For every bit string , equals the result of the same iteration started from in place of and run over the blocks of , with chosen as above. Only and , which determines , are needed.
Limits and common mistakes
The proof shows that collision resistance carries over from to . 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 , with a secret key , 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 . 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 messages that all have the same hash costs only about times the work of finding one collision, because successive block collisions, one after another along the chain, can be combined in ways.
The theorem needs collision resistance of for every chaining value, including values that an attacker chooses. Collisions of 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 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 , the hash of a secret key followed by a message , 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 and knows or guesses the length of can compute the padding that the hash appended, start the hash computation with as its state, and process extra data with a final length field that counts all of . The result is the correct hash of that longer input, so the message 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].