Merkle-Damgard & Length Extension
The construction behind MD5, SHA-1 and SHA-2 — chained compression functions with length padding — why H(secret ‖ message) is a broken MAC, and the collision timeline that retired two hash generations.
MD5, SHA-1 and the whole SHA-2 family are one design: the Merkle–Damgård construction, which turns a fixed-size compression function into a hash for arbitrary-length input by chaining. Understanding the chain buys you three things at once — why these hashes exist, why H(secret ‖ message) is a forgeable MAC (the bug HMAC was invented to fix), and why the industry spent two decades migrating off MD5 and SHA-1.
The construction #
Pad the message, split it into fixed-size blocks , and fold:
flowchart LR IV --> f1["f"] --> f2["f"] --> f3["f"] --> H["H(M)"] M1["M1"] --> f1 M2["M2"] --> f2 M3["M3 ‖ padding ‖ len"] --> f3
Padding (Merkle–Damgård strengthening): append a single 1 bit, then zeros, so the last block has 64 bits left; write the original message length in bits there. For a 512-bit-block hash that means padding to . Always applied, even when the length already fits — two different-length messages must never pad to identical block sequences. Embedding the length is what makes the security proof work: a collision in implies a collision in , so designers only have to build a collision-resistant compression function.
The concrete instances differ only in the state and the mangling inside :
| Hash | State (chaining variables) | Block | Output | internals |
|---|---|---|---|---|
| MD5 (1992) | 4 × 32-bit (A,B,C,D) | 512 b | 128 b | 4 rounds × 16 ops; nonlinear F/G/H/I; per-op constants $\lfloor 2^{32} |
| SHA-1 (1995) | 5 × 32-bit (A..E) | 512 b | 160 b | expand 16 words → 80 via XOR + 1-bit rotate; 80 steps |
| SHA-256 | 8 × 32-bit | 512 b | 256 b | 64 rounds; message schedule with σ functions |
| SHA-512 | 8 × 64-bit | 1024 b | 512 b | 80 rounds; faster than SHA-256 on 64-bit CPUs |
The lineage is literal: MD4 → MD5 ("improved MD4") → SHA-0 → SHA-1. The only change from SHA-0 to SHA-1 is that 1-bit rotate in the message expansion — NSA fixed a weakness without saying which; SHA-0 collisions were later found at cost, vindicating the tweak.
Length extension: the design's built-in trap #
The final chaining value is the output — Merkle–Damgård keeps no secret finalization. So given and (not itself!), anyone can set the internal state to and keep hashing:
This breaks the tempting MAC construction : an attacker who sees one valid pair can forge a valid tag for , without ever learning the secret. They only need to guess the secret's length (a small brute-force).
Defenses, in order of correctness:
- HMAC (RFC 2104): — the outer application hides the inner chaining state. This is the answer for MACs; it remains secure even with MD5/SHA-1 inside (collision resistance is not what HMAC needs).
- Truncation: SHA-384 and SHA-512/256 output fewer bits than their internal state, so the attacker can't reconstruct the state. Immune by accident of design.
- A different construction: SHA-3 is a sponge — output is squeezed from a larger hidden state, no extension possible. BLAKE2/BLAKE3 finalize with a flag. For keyed hashing, BLAKE2 takes a native key parameter; SHA-3 offers KMAC.
The collision timeline #
Collision resistance is capped by the birthday bound at ; real cryptanalysis got far below it:
- 2004 Wang et al. break MD5 collision resistance at ~$2^{39}$ work (against a $2^{64}$ birthday bound); within two years the attack is down to seconds on a single laptop.
- 2008 Chosen-prefix MD5 collision forges a working rogue CA certificate against RapidSSL (Sotirov, Stevens et al., 200 PS3s).
- 2012 Flame malware carries a state-sponsored, previously unknown chosen-prefix MD5 collision to forge a Microsoft code-signing cert and impersonate Windows Update.
- 2017 SHAttered: first public SHA-1 collision (Google + CWI), ~$2^{63}$ work, two colliding PDFs; browsers drop SHA-1 certs.
- 2020 SHA-1 is a Shambles: chosen-prefix SHA-1 collision at ~$2^{63.4}$ (down from $2^{67.1}$), demonstrated by forging a PGP key. The authors spent ~$75k on rented GPU time and estimated ~$45k at then-current prices — the point being that chosen-prefix SHA-1 collisions had moved into ordinary-budget range. Chosen-prefix is the practically dangerous kind: both messages can be meaningful.
- 2022 NIST announces SHA-1 retirement across all uses by end of 2030; signatures were disallowed back in 2013.
Operational status today:
| Hash | Collisions | Preimages | Verdict |
|---|---|---|---|
| MD5 | trivial (seconds) | theoretical only | forbidden anywhere collision resistance or signatures matter; still functionally fine as a non-adversarial checksum, but use BLAKE3/xxHash instead |
| SHA-1 | practical, chosen-prefix | no practical | forbidden for signatures/certs; git still runs on hardened SHA-1 (sha1dc detects known collision patterns); git's SHA-256 object format exists but adoption is still partial |
| SHA-2 | none known | none | current default, with HMAC or truncated variants where extension matters |
| SHA-3 / BLAKE3 | none | none | current; different construction (sponge / tree) |
Source claims audit #
Seeded from a university lab manual (MD5, SHA-1, N-hash labs; MD5/N-hash material follows Applied Cryptography). Status of its load-bearing claims:
| Claim from source | Status | Notes |
|---|---|---|
| MD5/SHA-1 padding rule, chaining structure, round counts, IVs | current | Mechanically accurate per RFC 1321 / RFC 3174 |
| "SHA-1 is recommended as primary for US government agencies" | outdated | True in ~2002; deprecated 2011, signatures disallowed 2013, full retirement by 2030 |
| "MD5 is an improved, more complex MD4" | outdated as security claim | Historically true; both are catastrophically broken for collisions |
| SHA-1 max input bits "= 2 exabytes" | current | Correct ( bytes ≈ 2.3 EB) |
| N-hash presented as a serious construction | outdated | Broken by differential cryptanalysis (Biham–Shamir, 1991); dead since the 90s; kept only as history |
| Source omits length extension entirely | — | Added; it is the single most exploitable property of this construction family |
| Source omits all collision history | — | Added timeline; this is what actually retired MD5/SHA-1 |
Sources
- docsRFC 1321 — The MD5 Message-Digest Algorithm
- docsRFC 3174 — US Secure Hash Algorithm 1 (SHA1)
- articleSHAttered — the first SHA-1 collision (Stevens et al., 2017)
- paperSHA-1 is a Shambles — chosen-prefix collisions (Leurent & Peyrin, 2020)
- articleFlickr API signature forgery via length extension (Duong & Rizzo, 2009)