Speed

Padding Fills the Block

SHA-256 appends a one bit, zero bits and the message length so that the padded message is a whole number of 64-byte blocks; build the padding of a short message byte by byte and count the blocks.

The question

SHA-256 reads its input in blocks of 512 bits, which are 64 bytes. A message rarely has a length that is a multiple of 64, so before the hash starts the message is padded. How does the padding look, and how many blocks does a message of a given length need?

The count of blocks is the amount of work: each block costs one run of the same compression step. The padding is also a first hint of why a hash can be continued, which a later lesson uses on a toy.

Work it by hand

FIPS 180-4 section 5.1.1 gives the rule and an example. Take the three letters abc, which are 24 bits. The padded message is:

  • the message itself: 61 62 63;
  • a one bit followed by seven zero bits, the byte 80;
  • as many zero bytes as it takes to leave exactly 8 bytes at the end of the block: for abc that is 52 zero bytes, and together with the zeros of the 80 byte they are the 423 zero bits that FIPS 180-4 names for this message;
  • the message length in bits as a 64-bit number, here 24, which is 18 in hex: 00 00 00 00 00 00 00 18.

3 + 1 + 52 + 8 is 64 bytes, one block. The padding always takes at least 9 bytes: the byte 80 and the 8 length bytes.

Run it

The program builds the padded message of the text. Line 1 counts its bytes. The loop of lines 3 to 5 looks for the number of zero bytes that makes the length of the message, the byte 80 and the zeros end 8 bytes before a multiple of 64. Line 6 writes the 64-bit length as two words with the helper u32_hex, shown under the program, and line 7 joins the message, 80, the zeros and the length as hex. Line 10 returns the padded message as hex.

The lines between return the number of zero bytes and the number of 64-byte blocks, which is the length of the hex text divided by 128.

The message abc, 52 zero bytes, and the padded message of 64 bytes shown in hex: 61 62 63, then 80, then zeros, ending with 00 00 00 00 00 00 00 18, in 1 block.
let n = str_len(text);
let zeros = 0;
for (z in range(0, 64)) {
  if ((n + 1 + z) % 64 == 56) { zeros = z; }
}
let tail = u32_hex(0) + u32_hex(n * 8);
let padded = ssl.hex(text) + "80" + str_repeat("00", zeros) + tail;
lmcode_return_put("zero_bytes", zeros);
lmcode_return_put("blocks", str_len(padded) / 128);
let answer = padded;
{
  "answer": "61626380000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000018",
  "zero_bytes": 52,
  "blocks": 1
}

No call of lmssl hashes a prepared block, so the page shows the padding and does not check it against a digest. You can change the text: printable ASCII and newlines up to 2048 bytes. The picture shows the first 64 bytes of the padded message; a longer message has more blocks than the picture shows.

Your turn

The program below should return how many 64-byte blocks SHA-256 processes for a message of n bytes, padding included. Read how it counts, run it with several lengths, and judge each count by building the padding by hand as above. Check runs it on the length shown and on seven others.

A message of 60 bytes and the number of 64-byte blocks that the shown program returns.
let blocks = n / 64;
if (n % 64 > 0) { blocks = blocks + 1; }
let answer = blocks;
{
  "answer": 1
}

Return, as an int, the number of 64-byte blocks that SHA-256 processes for a message of n bytes, with the padding of FIPS 180-4 section 5.1.1 (the byte 80, zero bytes and the 8-byte length) included. n is from 0 up to 2000. Check runs your program with other lengths, including 0 and lengths near a multiple of 64.

Read the result carefully

The page builds the padding of FIPS 180-4 section 5.1.1 for SHA-256 and counts blocks. It does not run the 64 rounds on each block, and it does not show how SHA-512 pads: that function uses 128-byte blocks and a longer length field, defined in section 5.1.2. The length is in bits, and the page's messages are far shorter than the field can hold.

The count of blocks is a count of work for one hash and nothing more: it says nothing about the time a real program needs. The messages in the exercise are numbers of bytes, not texts.

Recap

  • SHA-256 pads the message with the byte 80, zero bytes and the 64-bit length in bits, so that its length is a multiple of 64 bytes.
  • For abc the padded message is one block: 61 62 63, 80, 52 zero bytes and 00 00 00 00 00 00 00 18.
  • The length of the message in bits is the last 8 bytes of the padded message.
  • A longer message needs more blocks, and each block is one run of the compression step.