liveBook · Manning

Length-extension attack

As explained in Hacking Cryptography

A length-extension attack computes a valid hash for an extended message from a hash of the original message and its length when the hash construction exposes a reusable internal state.

A length-extension attack lets an attacker continue a Merkle–Damgård hash from a known digest. Given H(A), the original message length, and chosen appended data B, the attacker computes a valid digest for the padded original input followed by B without learning A. The digest exposes the hash’s final internal state, so hashing can be resumed rather than reversed. section 7.3.2 section 6.3.1

This breaks secret-prefix MACs of the form H(key || message). A captured valid tag becomes a starting state for extending a signed request; padding must match the length of the hidden key plus original compacted message. The resulting request can pass verification despite the attacker not knowing the API secret. section 7.2 section 7.3.2

Forging a bank transaction

A bank signs transaction parameters with SHA-256 over a client secret and request data. An attacker intercepts a valid amount=10 request, calculates Merkle–Damgård padding from the assumed original input length, resumes SHA-256 from the observed MAC, and appends parameters for amount=424242. The forged MAC authenticates the altered transaction. section 7.3.1 section 7.3.2

Avoid this failure by choosing a MAC construction rather than inventing one:

  • Use HMAC, which hashes the secret in multiple passes and resists length-extension and collision attacks. section 10.1.5
  • Prefer sponge-based hashes where applicable; they resist length extension even beneath a secret-prefix MAC. section 10.1.5 section 6.3
Figure 7.9 Because the internal state of the hash function is available to the attacker in the digest, the hash calculation can be “resumed.”
figure

The diagram shows why a digest can serve as the state from which processing of appended data resumes. section 7.3.2

How Hacking Cryptography covers it

How deep it goes
Works through padding generation, SHA-256 state replacement, and a forged HTTP request using OpenSSL.
Examples
Uses a SHA-256-signed bank transaction, extending amount=10 into a forged amount=424242 request.
What it assumes
Builds from hash functions, Merkle–Damgård construction, and MACs while explaining resumable digest state.
What you can do after
Diagnose/debug with it by recognizing forgeable secret-prefix MAC authentication and selecting HMAC or sponge-based hashes.
How it connects
Connects Merkle–Damgård construction, secret-prefix MACs, HMAC, collision attacks, SHA-2, and SHA-3.

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 Hacking Cryptography. The section links point to the parts of the book it draws on.