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 : a public hash function applied to a secret key followed by the message . If 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 , without knowing .
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.
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(binary10000000), then zero bytes, then the 8 length bytes.Start. Set the state to a fixed initial value of eight 32-bit words given in the standard.
Blocks. For each 64-byte block of the padded input, the hash compression function §15.9 mixes the block into the state.
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 be the length of a byte string in bytes. The attacker knows a message , its tag and the length of the key, but not the bytes of .
Compute the padding that SHA-256 appended to . depends only on the length , which the attacker knows.
Read as the state of SHA-256 after it has processed , a whole number of blocks.
Choose extra bytes . Continue SHA-256 from that state over , and finish with the padding for the total length . Call the result .
Send the message with the tag .
The receiver computes . Its input is , so SHA-256 reaches exactly the state after the first blocks and then processes the same final bytes that the attacker processed. The result is , and the forged message is accepted.
What the attacker cannot choose. The padding bytes sit between and in the forged message, where they act as glue padding that joins to . 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 , it tries each likely length. Every guess gives a different 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.
Secret key : the 16 ASCII bytes
my-secret-key-16, unknown to the attacker.Message : the 8 ASCII bytes
user=bob.Extension , chosen by the attacker: the 11 ASCII bytes
&role=admin.
Original tag. The sender computes , the SHA-256 digest of , over bytes. The attacker sees and
=
d7cc54f1bb13ff0845ecfd6930d4bc68a171be6102460e2fe182a2faec943483
Glue padding. The input has 24 bytes, which is bits, 0xc0 in hexadecimal. The padding must bring it to 64 bytes: one byte 0x80, then zero bytes, then 8 length bytes, with , so . The padding is bytes long and ends with the length bytes 00 00 00 00 00 00 00 c0.
Forged message. is user=bob, then the 40 bytes of , then &role=admin: bytes, of which 40 are padding.
Forged tag. The attacker loads the 32 bytes of as the state of SHA-256 after 64 processed bytes, and hashes . The receiver’s full input will be bytes, which is bits, so the final padding is 0x80, 44 zero bytes and the length 600 (0x258) in 8 bytes. With the 11 bytes of that fills exactly one block: bytes. The result is
=
f81c54aacf4a5e11bebe66740a28464ed8a9c107fe89d55662c9194e8f5dc690
Check. The receiver, which knows , computes the SHA-256 digest of over 75 bytes and gets exactly . It accepts , 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 under the same key does not give the HMAC tag of .
Stated precisely
Let be SHA-256 and the length of a byte string in bytes. For an input of bytes, FIPS 180-4 defines the padding where is zero bytes and is the bit length written as a 64-bit big-endian number. The total is then a multiple of 64.
Write for the digest that SHA-256 outputs when it starts from the state instead of its fixed initial value, counts bytes as already processed ( a multiple of 64), processes , and finishes with . Let and . Then for every byte string The right side uses , and , but not . 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.
The tag is 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].
The attacker has one message with its valid tag.
The attacker knows , or can guess it.
The receiver acts on a message that contains the padding bytes in the middle.
The forged message is always , never .
Why HMAC is immune. With SHA-256, an HMAC tag is . Here is the key brought to 64 bytes, is a fixed public 64-byte constant, is bitwise XOR (exclusive or: each output bit is 1 exactly when the two input bits differ), and is the 32-byte inner digest, itself a keyed hash of the message. Extending the tag gives the hash of , an outer input longer than 96 bytes. A receiver always hashes an outer input of exactly bytes, so the extended value is of no use to the attacker. The inner digest , 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 .
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 values of the missing bits, each try costing one forged message sent to the receiver.
A hash that resists length extension does not make 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, , 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].