liveBook · Manning

Length-extension attack

As explained in Real-World Cryptography

A length-extension attack is an attack that exploits iterative hash constructions to compute a hash for an extended message when given a hash of an original message and its length.

A length-extension attack exploits the iterative Merkle–Damgård design used by SHA-2. Given a digest of an original message and its length, an attacker can continue hashing to produce a valid digest for the original message, padding, and appended data. section 2.5.1 section 3.6

The attack works because:

  • Merkle–Damgård hashing processes padded input block by block, carrying the previous compression result into the next step. section 2.5.1
  • The published digest exposes the hash's effective intermediate state after the original input. section 3.6
  • An attacker can treat the original padding as part of the input and continue hashing appended data without knowing a secret prefix. section 3.6

Tampering with a signed cookie

Suppose a server checks SHA-256(secret || "user=bob"). An attacker can extend the known digest with the appropriate padding and "&admin=true", producing a digest that validates the extended message even though the secret remains unknown. section 3.6

Figure 3.13 An attacker successfully uses a length-extension attack to tamper with their cookie and computes the correct hash using the previous hash.

Trace the attacker’s successful cookie modification and reuse of the previous hash. section 3.6

Do not hash secrets with SHA-2 as a homemade MAC. Padding details can block some protocols, but small protocol changes can restore exploitability; use a construction resistant to this weakness. section 3.6 section 2.7

How Real-World Cryptography covers it

How deep it goes
Works through digest continuation, padding, and appended data, but stops short of implementation details.
Examples
Uses a user=bob cookie extended with &admin=true and illustrates successful digest reuse.
What it assumes
Assumes basic hash-function and SHA-2 familiarity while explaining Merkle–Damgård processing through examples.
What you can do after
Evaluate whether secret-prefixed SHA-2 authentication is vulnerable and avoid using it as a homemade MAC.
How it connects
Connects SHA-2, SHA-256, Merkle–Damgård construction, hash functions, and message authentication codes.

Also explained in

Compare how 2 books explain length-extension attack →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Real-World Cryptography. The section links point to the parts of the book it draws on.