Course contentsShow
← Security
Lesson 213 of 3,0525. Cryptographic Hashing and MACsFree lesson

Length Extension Attacks

Understanding how Merkle-Damgård construction enables length extension attacks and why SHA-3 is immune.

Length Extension Attacks

What you'll learn: How certain hash functions can be exploited to append data without knowing the original secret, and why SHA-3 is designed to prevent this.

The Vulnerability in Merkle-Damgård Construction

Many classic hash functions (MD5, SHA-1, SHA-2 family) use the Merkle-Damgård construction. This design processes messages in sequential blocks, with each block's output feeding into the next block's computation as the internal state.

Here's the problem: if you know H(secret || message) and the length of secret, you can compute H(secret || message || padding || new_data) without ever knowing the secret. You're literally extending the message and creating a valid hash for the extended version.

Why This Works

The Merkle-Damgård construction's internal state at the end of hashing becomes the hash output itself. An attacker can:

  1. Take the known hash output as the starting state
  2. Continue the hash computation with additional blocks
  3. Produce a valid hash for the extended message

Analogy: Imagine a relay race where each runner hands off a baton (the internal state). If you grab the baton at the finish line, you can continue running and add more laps—even though you never ran the first part of the race.

Why SHA-3 Is Immune

SHA-3 uses the Keccak sponge construction instead of Merkle-Damgård. In a sponge function, the final hash is "squeezed" from a much larger internal state—you only see part of it. Without knowing the full internal state, you cannot continue the computation. The output is essentially "detached" from the ability to resume processing.

Security Impact

Length extension attacks break constructions like H(secret || message) used for message authentication. This is why proper MAC algorithms (HMAC, CMAC) exist—they're specifically designed to prevent this attack.

Key Takeaway: Merkle-Damgård hash functions (SHA-2 and earlier) allow attackers to extend messages and forge valid hashes without knowing secrets, while SHA-3's sponge construction prevents this by design.