Learn › Blockchain & Access Control › Module 1 · Foundations and hashing

Merkle–Damgård, MD5, SHA-1, CBC-MAC and authenticated encryption

How iterated hashes pad and compress, the MD5 and SHA-1 round structures, the collision history from 2004 to SHAttered, CBC-MAC, EtM/E&M/MtE, and why CBC encryption with CBC-MAC under one key fails.

Iterated (Merkle–Damgård) hashing

compress: {0,1}m+t → {0,1}m   (m-bit chaining state + t-bit block → new m-bit state)

z0 = IV; zi = compress(zi−1 ‖ yi); digest = g(zr)

  1. Pad: append a 1 bit, zeros, and the original length so the total is a multiple of t.
  2. Initialise with the public IV.
  3. Iterate compress over each block.
  4. Finalise with an optional output transform g.

Worked example

m = 128, t = 512, message 1,200 bits. Append 1 + 271 zeros + 64-bit length → 1,536 bits = 3 blocks. z1 = c(IV ‖ y1), z2 = c(z1 ‖ y2), z3 = c(z2 ‖ y3) = digest.

Hash standards

AlgorithmYearDigestStatus
MD51991128Broken: practical collisions since 2004; checksums only
SHA-01993160Withdrawn; collision attack 261 (1998)
SHA-11995160Broken: 269 attack (2005), real collision SHAttered (2017)
SHA-22001224–512Secure; only reduced-round attacks
SHA-3 (Keccak)2015224–512Secure; sponge construction, a structural backup to SHA-2

MD5

  • Pad to 448 mod 512, then append the 64-bit length.
  • Buffer A, B, C, D = 01234567, 89ABCDEF, FEDCBA98, 76543210 (as written byte-wise).
  • Each 512-bit block: 4 rounds × 16 steps = 64 steps using F = (B∧C)∨(¬B∧D), G = (B∧D)∨(C∧¬D), H = B⊕C⊕D, I = C⊕(B∨¬D), a message word X[k] and a sine-derived constant T[i]; then add the result to the chaining value mod 232.

Collision timeline: 1996 compression-function collisions; 2004 full collisions; 2007 chosen-prefix collisions (two chosen prefixes p1, p2, suffixes m1, m2 with MD5(p1‖m1) = MD5(p2‖m2)); 2008 rogue CA certificate; 2012 Flame malware forged a Microsoft code-signing certificate.

SHA-1

  • Padding: append 1, d = (447 − |x|) mod 512 zeros, 64-bit length.
  • Buffer H0…H4 = 67452301, EFCDAB89, 98BADCFE, 10325476, C3D2E1F0.
  • Message schedule: Wt = ROTL1(Wt−3 ⊕ Wt−8 ⊕ Wt−14 ⊕ Wt−16) for t = 16…79 (the rotation is what SHA-0 lacked).
  • 80 steps in 4 rounds: f = Ch (0–19), Parity (20–39), Maj (40–59), Parity (60–79).
  • Step: temp = ROTL5(A) + ft(B,C,D) + E + Wt + Kt; E = D; D = C; C = ROTL30(B); B = A; A = temp. Kt come from √2, √3, √5, √10.
  • Feed-forward: add A…E to H0…H4; output 160 bits.

MACs and CBC-MAC

A MAC gives integrity and origin authentication but not non-repudiation: Bob holds the same key, so a judge cannot tell whether Alice or Bob made the tag. Requirement: even after seeing many valid (mi, tag) pairs, a tag for any new message must be infeasible to forge.

CBC-MAC (IV = 0): c0 = EK(p0), ci = EK(pi ⊕ ci−1); tag = last block only.

Used in interbank messaging and card authorisation with HSM-held keys.

Authenticated encryption

ModeConstructionSeen in
Encrypt-then-MACC = EK1(P); tag = MACK2(C)Strongest generic choice: verify before decrypting
Encrypt-and-MACC = EK1(P); tag = MACK2(P)SSH
MAC-then-EncryptC = EK1(P ‖ MACK2(P))Older TLS

Why CBC encryption + CBC-MAC with the same key fails

Proof sketch

Alice sends c0…c3 (CBC, IV = 0) and mac = CBC-MACK(p) = c3. Mallory replaces c0, c1, c2 with garbage c′ and keeps c3 and mac. Bob decrypts p′i = D(c′i) ⊕ c′i−1. Recomputing the MAC: E(p′0) = c′0, E(p′1 ⊕ c′0) = c′1, E(p′2 ⊕ c′1) = c′2, E(p′3 ⊕ c′2) = E(D(c3)) = c3 = mac. The check passes although the plaintext is corrupted.

Fix: CCM — CTR-mode encryption (ci = pi ⊕ EK(ctr + i)) with CBC-MAC over the plaintext. CTR does not chain ciphertext into the MAC computation, so any change is caught. WPA2’s CCMP uses AES-CCM.