← Back to section · Foundations of Cryptography
The Merkle–Damgård Construction and Length Extension: A Structural Flaw in the Pipeline
MD5, SHA-1 and SHA-256 share one iterative skeleton: chop the message into blocks and compress them one by one. The design is simple and elegant, but it carries a structural weakness — given H(m), anyone can compute H(m‖padding‖suffix) without knowing m.
From MD5 to SHA-1 to SHA-256, the mainstream hash functions of the past three decades share a single skeleton: the Merkle–Damgård construction. Proposed independently by Ralph Merkle and Ivan Damgård around 1989, its idea is almost disarmingly simple — if all we know how to build is a function that compresses fixed-length input, then chop a message of arbitrary length into fixed-length blocks and feed them in one at a time. This skeleton carried an entire generation of hash functions, and it also brought a weakness that has nothing to do with any particular algorithm and everything to do with the structure itself: the length extension attack.
The construction: a relay race of states
Merkle–Damgård needs only two parts. One is a compression function , which takes a fixed-length internal state (the chaining variable) and one fixed-length message block, and produces a new internal state. The other is an initialization vector, a hard-coded constant that serves as the state for the first block. To hash a message, you first pad it so its total length is a multiple of the block size — the padding also encodes the bit length of the original message, a step called MD strengthening — and then iterate from the IV: . The internal state after the last block is the hash value itself, output as-is, with no post-processing.
The design carries an elegant security theorem: if the compression function is collision resistant, then so is the whole construction. The designer only has to concentrate on one small fixed-input function, and security for messages of arbitrary length follows automatically. That is why it ruled for thirty years — the object of analysis is small, the proof is clean, implementations are simple, and hardware pipelines love it.
The flaw: the hash value is the internal state
The problem sits precisely in that “final state, output verbatim” step. In Merkle–Damgård, the hash value is not some summary or projection of the internal state — it is the entire internal state. Anyone who obtains therefore holds the machine’s complete state after processing ; the padding rules are public, and the message length is usually known or guessable. The attacker does not need to start from the beginning. Starting from the intermediate state , they append the legitimate padding for and then any suffix of their choosing, iterate block by block, and arrive at exactly — a valid hash of a message they have never seen and could not otherwise obtain. The whole procedure requires neither the contents of nor any break of the compression function.
This is not a theoretical parlour game. What it breaks is a once-ubiquitous usage pattern: using as a message authentication code. The server believes only the key holder can produce a correct tag, but an attacker who intercepts one (message, tag) pair can forge a valid tag for “original message + padding + malicious suffix”, and the server’s verification recomputes exactly that extended string. API signatures and signed download links at multiple real-world services fell to precisely this pattern. Note well what was not harmed: SHA-256’s collision resistance is untouched. The casualty is the protocol design that mistook an iterated hash for a keyed primitive.
A real incident: not just a textbook curiosity
The most famous demonstration of length extension in the wild came in 2009, when security researchers Thai Duong and Juliano Rizzo demonstrated a forgery against Flickr’s API signatures. Flickr’s signing scheme at the time was exactly of the form — the secret prepended, the whole thing hashed with MD5. Using length extension, the researchers computed a valid signature for an extended parameter string without knowing the secret, and thereby called interfaces that should not have been open to them. What makes the case a classic is that it had nothing to do with MD5’s known weaknesses: MD5 was already considered “insecure” by then, but the attack is unrelated to its collision failures — swap in the then-impeccable SHA-256 and the same signing scheme falls the same way. It cleanly proves this article’s central claim: the disease is in the construction, not the algorithm.
In the years since, signature schemes of the same shape have reappeared in APIs, presigned object-storage links and anti-tamper tokens for web forms — and been opened by the same technique, repeatedly. The defensive consensus has hardened accordingly: any home-grown “stir the key into the hash” scheme must first pass the length-extension test; those that cannot are rejected outright in favour of a standard keyed primitive such as HMAC.
Three lines of repair
The first is to change the usage: HMAC wraps the internal state in two nested layers with two distinct padding constants, so the output an attacker sees is no longer any state that can be iterated further — length extension has nothing to grab onto (a companion article in this section treats HMAC in detail).
The second is to truncate the output: SHA-384 and SHA-512/256 are variants of the same construction that output only part of the internal state. Missing the truncated portion, the attacker cannot resume the iteration — knowing most of the result is not the same as holding the machine’s state.
The third is to change the construction: SHA-3’s sponge divides the internal state into a “rate” and a “capacity”; only the rate is ever output, and the capacity never sees daylight. What the attacker observes is forever a slice of the state, and extension fails structurally. This is why SHA-3 is best understood not as merely “another hash function” but as a direct answer to this class of structural lesson.
The lesson for designers
The most memorable thing about length extension is that it is independent of algorithmic strength. A future hash function could have flawless collision resistance, yet if it keeps the “state equals output” iterative skeleton, length extension still goes through. A substantial share of cryptographic failures do not live inside the hard mathematics; they live at the seams of constructions. When evaluating a hashing scheme, alongside “is it collision resistant?”, it is worth asking one more question: how much of its internal state does its output expose?
Tags: Merkle-DamgårdLength ExtensionSHA-256