Section 5.11

Length-extension attack

Definition

A length-extension attack forges a tag, the value that shows a message came from a key holder, when the tag is computed as H(K‖M)H(K \mathbin{\Vert} M): a public hash function HH applied to a secret key KK followed by the message MM. If HH outputs the whole working value that it updates while reading its input, as SHA-256 §5.2 (Secure Hash Algorithm with a 256-bit output) does, an attacker who has seen one message and its tag can compute a valid tag for a longer message that starts with MM, without knowing KK.

The idea

A developer needs to authenticate commands sent to a device and already has SHA-256. The tempting rule is: tag equals SHA-256 of the key followed by the command. The reasoning sounds right. The attacker does not know the key, so it cannot hash the key followed by a different command. That reasoning treats SHA-256 as a black box that returns an unrelated random-looking value for every input. SHA-256 is built differently.

SHA-256 reads its input in 64-byte blocks and keeps a 32-byte running state. Each block is mixed into the state, and when the input ends, the state itself is published as the digest. A picture: a clerk keeps a running total while reading a list of prices, and announces the total after the last item. Anyone who hears that total can add more prices and announce a correct new total without ever seeing the first list. The picture is right that the published value is a starting point for more work. It is wrong in two ways: the state is not a sum, so it cannot be taken apart to recover the earlier input, and the attacker must also account for the padding that SHA-256 adds at the end of every input.

How it works

How SHA-256 processes input. FIPS 180-4, the standard that defines SHA-256, specifies these steps.

  1. Padding. Append one 1 bit, then zero bits, then the input length in bits as a 64-bit number written most significant byte first (big-endian §4.21). The number of zero bits is the smallest that makes the total length a multiple of 512 bits, which is 64 bytes. For an input made of whole bytes, the padding is the byte 0x80 (binary 10000000), then zero bytes, then the 8 length bytes.

  2. Start. Set the state to a fixed initial value of eight 32-bit words given in the standard.

  3. Blocks. For each 64-byte block of the padded input, the hash compression function §15.9 mixes the block into the state.

  4. Output. After the last block, output the state, all 256 bits, as the digest.

This block-by-block design is the Merkle–Damgård construction §15.8.

The attack. Let |x||x| be the length of a byte string xx in bytes. The attacker knows a message MM, its tag T=H(K‖M)T = H(K \mathbin{\Vert} M) and the length |K||K| of the key, but not the bytes of KK.

  1. Compute the padding PP that SHA-256 appended to K‖MK \mathbin{\Vert} M. PP depends only on the length |K|+|M||K| + |M|, which the attacker knows.

  2. Read TT as the state of SHA-256 after it has processed K‖M‖PK \mathbin{\Vert} M \mathbin{\Vert} P, a whole number of blocks.

  3. Choose extra bytes XX. Continue SHA-256 from that state over XX, and finish with the padding for the total length |K|+|M|+|P|+|X||K| + |M| + |P| + |X|. Call the result T′T'.

  4. Send the message M′=M‖P‖XM' = M \mathbin{\Vert} P \mathbin{\Vert} X with the tag T′T'.

The receiver computes H(K‖M′)H(K \mathbin{\Vert} M'). Its input is K‖M‖P‖XK \mathbin{\Vert} M \mathbin{\Vert} P \mathbin{\Vert} X, so SHA-256 reaches exactly the state TT after the first blocks and then processes the same final bytes that the attacker processed. The result is T′T', and the forged message is accepted.

What the attacker cannot choose. The padding bytes PP sit between MM and XX in the forged message, where they act as glue padding that joins MM to XX. The forgery does harm only if the receiver acts on a message with those bytes in the middle, for example a parser that splits a message into name=value fields and tolerates odd bytes inside a value. If the attacker does not know |K||K|, it tries each likely length. Every guess gives a different PP and costs one forged message sent to the receiver.

Worked example

All values are computed with SHA-256, and the key is invented for this example.

Original tag. The sender computes TT, the SHA-256 digest of K‖MK \mathbin{\Vert} M, over 16+8=2416 + 8 = 24 bytes. The attacker sees MM and

TT = d7cc54f1bb13ff0845ecfd6930d4bc68a171be6102460e2fe182a2faec943483

Glue padding. The input K‖MK \mathbin{\Vert} M has 24 bytes, which is 24×8=19224 \times 8 = 192 bits, 0xc0 in hexadecimal. The padding must bring it to 64 bytes: one byte 0x80, then zz zero bytes, then 8 length bytes, with 24+1+z+8=6424 + 1 + z + 8 = 64, so z=31z = 31. The padding PP is 1+31+8=401 + 31 + 8 = 40 bytes long and ends with the length bytes 00 00 00 00 00 00 00 c0.

Forged message. M′M' is user=bob, then the 40 bytes of PP, then &role=admin: 8+40+11=598 + 40 + 11 = 59 bytes, of which 40 are padding.

Forged tag. The attacker loads the 32 bytes of TT as the state of SHA-256 after 64 processed bytes, and hashes XX. The receiver’s full input will be 16+59=7516 + 59 = 75 bytes, which is 75×8=60075 \times 8 = 600 bits, so the final padding is 0x80, 44 zero bytes and the length 600 (0x258) in 8 bytes. With the 11 bytes of XX that fills exactly one block: 11+1+44+8=6411 + 1 + 44 + 8 = 64 bytes. The result is

T′T' = f81c54aacf4a5e11bebe66740a28464ed8a9c107fe89d55662c9194e8f5dc690

Check. The receiver, which knows KK, computes the SHA-256 digest of K‖M′K \mathbin{\Vert} M' over 75 bytes and gets exactly T′T'. It accepts M′M', a message the key holder never sent. A parser that splits fields at & sees a user field whose value ends in 40 odd bytes, followed by a new field role=admin. The same continuation started from the HMAC §5.8 (hash-based message authentication code) tag of MM under the same key does not give the HMAC tag of M′M'.

Stated precisely

Let HH be SHA-256 and |x||x| the length of a byte string xx in bytes. For an input of ℓ\ell bytes, FIPS 180-4 defines the padding pad(ℓ)=𝟶𝚡𝟾𝟶‖0z‖⟨8ℓ⟩64,z=(55−ℓ)mod64,\mathrm{pad}(\ell) = \mathtt{0x80} \mathbin{\Vert} 0^{z} \mathbin{\Vert} \langle 8\ell \rangle_{64}, \qquad z = (55 - \ell) \bmod 64, where 0z0^{z} is zz zero bytes and ⟨8ℓ⟩64\langle 8\ell \rangle_{64} is the bit length 8ℓ8\ell written as a 64-bit big-endian number. The total ℓ+|pad(ℓ)|\ell + |\mathrm{pad}(\ell)| is then a multiple of 64.

Write Cont(S,n,X)\mathrm{Cont}(S, n, X) for the digest that SHA-256 outputs when it starts from the state SS instead of its fixed initial value, counts nn bytes as already processed (nn a multiple of 64), processes XX, and finishes with pad(n+|X|)\mathrm{pad}(n + |X|). Let ℓ=|K|+|M|\ell = |K| + |M| and T=H(K‖M)T = H(K \mathbin{\Vert} M). Then for every byte string XX H(K‖M‖pad(ℓ)‖X)=Cont(T,ℓ+|pad(ℓ)|,X).H\bigl(K \mathbin{\Vert} M \mathbin{\Vert} \mathrm{pad}(\ell) \mathbin{\Vert} X\bigr) = \mathrm{Cont}\bigl(T,\ \ell + |\mathrm{pad}(\ell)|,\ X\bigr). The right side uses TT, ℓ\ell and XX, but not KK. The equation holds because FIPS 180-4 defines the SHA-256 digest as the final internal state itself.

Conditions. The attack needs all four of the following.

  1. The tag is H(K‖M)H(K \mathbin{\Vert} M) for a hash whose output is its whole final state. NIST SP 800-107 (Special Publication 800-107) notes that this holds for SHA-1 (Secure Hash Algorithm §14.1 1, with a 160-bit output), SHA-256 and SHA-512 (Secure Hash Algorithm with a 512-bit output) [310].

  2. The attacker has one message with its valid tag.

  3. The attacker knows |K||K|, or can guess it.

  4. The receiver acts on a message that contains the padding bytes in the middle.

The forged message is always M‖pad(ℓ)‖XM \mathbin{\Vert} \mathrm{pad}(\ell) \mathbin{\Vert} X, never M‖XM \mathbin{\Vert} X.

Why HMAC is immune. With SHA-256, an HMAC tag is H((K0⊕opad)‖I)H((K_0 \oplus \mathrm{opad}) \mathbin{\Vert} I). Here K0K_0 is the key brought to 64 bytes, opad\mathrm{opad} is a fixed public 64-byte constant, ⊕\oplus is bitwise XOR (exclusive or: each output bit is 1 exactly when the two input bits differ), and II is the 32-byte inner digest, itself a keyed hash of the message. Extending the tag gives the hash of (K0⊕opad)‖I‖pad(96)‖X(K_0 \oplus \mathrm{opad}) \mathbin{\Vert} I \mathbin{\Vert} \mathrm{pad}(96) \mathbin{\Vert} X, an outer input longer than 96 bytes. A receiver always hashes an outer input of exactly 64+32=9664 + 32 = 96 bytes, so the extended value is of no use to the attacker. The inner digest II, which an extension of the inner hash would need, is never published. Bellare, Canetti and Krawczyk, who designed HMAC, give this reason: the outer hash prevents the exposure of the inner result [314].

Limits and common mistakes

The attack forges tags; it does not reveal the key. It also needs no weakness in SHA-256 as a hash: preimage resistance §5.3 (finding an input for a given digest is infeasible) and collision resistance §5.5 (finding two inputs with the same digest is infeasible) are untouched. The flaw is the rule H(K‖M)H(K \mathbin{\Vert} M).

Not every hash function allows the simple attack. SHA-384 (Secure Hash Algorithm with a 384-bit output) and SHA-512/256 (a variant of SHA-512 with its own starting value and its output cut to 256 bits), both in FIPS 180-4, output only part of their 512-bit final state: SHA-384 hides 128 bits of it and SHA-512/256 hides 256 bits. The four SHA-3 (Secure Hash Algorithm-3) hash functions of FIPS 202 [315] never output a part of their state called the capacity, which is twice the digest length. Without the hidden bits the attacker cannot continue. SHA-224 (Secure Hash Algorithm with a 224-bit output) hides only 32 of its 256 state bits: an attacker can try all 2322^{32} values of the missing bits, each try costing one forged message sent to the receiver.

A hash that resists length extension does not make H(K‖M)H(K \mathbin{\Vert} M) a proven message authentication code §5.7. Avoiding one attack is not a security proof. HMAC has a proof with named assumptions, given in the HMAC entry.

Moving the key to the end, H(M‖K)H(M \mathbin{\Vert} K), stops length extension but opens another attack, described by Bellare, Canetti and Krawczyk in 1996. Two messages of equal length that drive the hash to the same internal state have the same tag under every key, and such pairs can be searched for without the key, on the attacker’s own computer.

Do not rely on the parser to reject the glue padding. Formats and parsers change. The tag check, not the message format, must be what rejects a forged message.

Questions answered

Why is an ordinary SHA-256 digest not a substitute for HMAC?

An ordinary SHA-256 (Secure Hash Algorithm with a 256-bit output) digest is not a substitute for HMAC because it involves no secret, so anyone can compute it. An attacker who changes a message simply computes the digest of the changed message and sends that. A digest detects accidental damage. It detects deliberate change only when the digest itself reaches the receiver over a path the attacker cannot alter. HMAC (hash-based message authentication code) mixes a secret key into the computation, so only key holders can produce a valid tag, the short value sent with a message to show that a key holder produced it.

Adding a key by hand does not repair the digest. If the tag is the SHA-256 digest of the key followed by the message, an attacker who sees one message and its tag can continue the hash, because SHA-256 outputs its entire final internal state. The attacker obtains a valid tag for the message, followed by SHA-256’s padding, followed by bytes of the attacker’s choice, without learning the key. This is the length-extension attack. Putting the key after the message avoids it but lets an offline search for hash collisions produce forgeries. HMAC hashes twice with two different key-derived blocks, never publishes the inner digest, and has a security proof with stated assumptions. That is why HMAC, not a home-made combination of a key and SHA-256, is the construction to use.

Related entries: HMAC (§5.8), Authentication coverage (§5.41), SHA-256 (§5.2), Collision resistance (§5.5), Merkle–Damgård construction (§15.8), Hash compression function (§15.9). Further reading: [7, 8, 46, 310, 314, 315].