Speed

Length Extension on a Toy

A hash that processes its message block after block ends in a state that is the digest itself, so anyone who knows a digest and the message length can continue the hash; forge the tag of a 16-bit toy without the key, and see which construction avoids it.

The question

A service wants to protect a message by sending a tag that depends on a secret key. The simplest idea is a tag computed as the hash of the key followed by the message. If the hash works block after block, as SHA-256 does, can someone who does not know the key add something to the message and compute the matching tag?

This lesson shows that they can, on a toy hash of 16 bits that this page defines and runs. The toy is a toy: it is no MAC, and nothing on this page is an attack on a deployed system.

Work it by hand

The toy keeps a state s, a number below 65521, starting at 1. For each input byte b it sets s = (s times 257 plus b plus 1) mod 65521. Its padding is the byte 80, then as many zero bytes as it takes to reach a multiple of 8 bytes, then one byte holding the message length mod 256. The digest is the state after the padded message.

Take the test material key k and the message pay=10: seven bytes. The first three steps are k, 107: s = 365; p, 112: s = 28397; a, 97: s = 25296. The padding of 7 bytes is 80 07. After the whole padded message the state is the tag.

Nothing is kept but the state, so the tag is the state. Someone who knows the tag and that the hashed text had 7 bytes knows the padding, 80 07, and can continue from the tag: the glue 80 07, then the added bytes, then the padding of the longer length.

Run it

The program computes the tag with the key, and the tag of the longer message, body then the glue then more, also with the key. Then it continues the hash from the tag, without the key. Lines 1 to 7 and 8 to 13 define the toy and its padding. Line 15 is the tag and line 18 is the tag of the longer message. Line 19 is the forgery: it starts from tag, takes the bytes of more and then the padding of the length of the longer message.

The toy key k, the message pay=10 and the addition &pay=99, the tag 64510 of the message, and the forged tag 57546 and the real tag 57546 of the longer message, which are equal: true.

Public test material, generated for this lesson. Never use it for anything real.

fn toy(state: int, data: string) -> int {
  let s = state;
  for (i in range(0, hex_len(data))) {
    s = (s * 257 + hex_u8(data, i) + 1) % 65521;
  }
  return s;
}
fn pad(n: int) -> string {
  let p = "80";
  let zeros = (7 - n % 8 + 8) % 8;
  for (i in range(0, zeros)) { p = p + "00"; }
  return p + u8_hex(n % 256);
}
let m = str_hex_encode(secret + body);
let tag = toy(1, m + pad(hex_len(m)));
let glue = pad(hex_len(m));
let longer = m + glue + str_hex_encode(more);
let real = toy(1, longer + pad(hex_len(longer)));
let forged = toy(toy(tag, str_hex_encode(more)), pad(hex_len(longer)));
lmcode_return_put("tag", tag);
lmcode_return_put("forged", forged);
lmcode_return_put("real", real);
let answer = forged == real;
{
  "answer": true,
  "tag": 64510,
  "forged": 57546,
  "real": 57546
}

The key is test material, a toy key of the lesson. Line 23 returns whether the forged tag equals the real one. You can change secret, body and more: printable ASCII and newlines, any of them empty, up to 2048 bytes. The answer stays true, because the construction does not depend on the text.

Your turn

The program below is meant to return the tag of the longer message, message, the glue padding and ext, from the tag of the message with a key, the length keylen of the key and the texts, without the key. The toy functions are in the program: the toy is the one of Work it by hand. Read how it builds the forged tag and judge each result. Check runs it on the data shown and on four others.

The tag of a message with a key, the key length 9, the message item=pen and the addition &qty=9, and the forged tag that the shown program returns.

Public test material, generated for this lesson. Never use it for anything real.

fn toy(state: int, data: string) -> int {
  let s = state;
  for (i in range(0, hex_len(data))) {
    s = (s * 257 + hex_u8(data, i) + 1) % 65521;
  }
  return s;
}
fn pad(n: int) -> string {
  let p = "80";
  let zeros = (7 - n % 8 + 8) % 8;
  for (i in range(0, zeros)) { p = p + "00"; }
  return p + u8_hex(n % 256);
}
let n = str_len(message);
let glue = pad(n);
let total = n + hex_len(glue) + str_len(ext);
let answer = toy(tag, str_hex_encode(ext) + pad(total));
{
  "answer": 53457
}

A toy hash has a state s below 65521, starting at 1; each byte b sets s to (s times 257 plus b plus 1) mod 65521. Its padding is the byte 80, zero bytes up to a multiple of 8 bytes, and one byte of the length mod 256. The digest is the state after the padded message. tag is the toy digest of a secret key of keylen bytes followed by message. Return, as an int, the toy digest of the key, message, the padding of that text and ext, which is what a forger can compute from tag and keylen without the key. Check runs your program with other keys that the program does not see.

Read the result carefully

The forgery uses no key, only the tag and the length of the key. The same structure holds for SHA-256, which also ends in its state, but lmssl has no call that continues a digest, so that statement is stated here, not run: the page shows only the toy. The page does not attack any real system.

HMAC is a construction that avoids this; it is the subject of HMAC uses two pads. HMAC hashes the message under one key pad and then hashes that digest under another key pad, so continuing a tag does not give the HMAC of a longer message. The book linked above shows the two pads. The toy has a 16-bit state and is no MAC: a tag with only 65521 values could be guessed in any case.

Recap

  • The tag of a hash of key and message is the hash's last state, so it can be continued.
  • The padding depends only on the length of what was hashed, not on its content.
  • The forged tag equals the tag that the key holder would compute for the longer message.
  • HMAC is the construction that avoids it; a plain hash of key and message is not a MAC.