5
$\begingroup$

If I am using SHA-512 on a message and need to determine the padding field and length field, how do I determine the length field? I think I understand the padding, but not the length field.

For instance, if I have a 1919-bit message the padding field would be one 1 and zero 0's. But what would the length field be? 1919?

$\endgroup$
5
  • 1
    $\begingroup$ Yes, it's the length of the message in bits (without padding). $\endgroup$ Feb 15, 2015 at 23:27
  • $\begingroup$ Ok, so it would be an unsigned 128-bit integer with the value of 1919? $\endgroup$
    – Jesse
    Feb 15, 2015 at 23:29
  • 1
    $\begingroup$ 1919 bits: padding field: (-1919 - 128) mod 1024 = 1 1 ones followed by 0 zeroes. length field: unsigned 128-bit integer with value of 1919 /// Special Case: 1920 bits padding field: (-1920 - 128) mod 1024 = 0. Padding = 1024 bits. Padding should be added always so a whole block is added. $\endgroup$
    – Jesse
    Feb 18, 2015 at 13:55
  • $\begingroup$ So what if the message length was 1920 bits? Since we add the 128-bits to end, would it be no padding or do we need to add another block? $\endgroup$
    – Tim Hap
    Feb 10 at 15:31
  • $\begingroup$ For a 1920-bit message, we need to add another block, because the padding consists of one bit at 1, now giving 1921 bits, then as many bit(s) at 0 as it takes to reach 128 less then a multiple of 1024 bits, which is 1023 bits at 0 now giving 2944 bits, then the 128-bit representation for the integer 1920, now giving 3072 bits fed to 3 executions of the round function. $\endgroup$
    – fgrieu
    Feb 10 at 18:07

2 Answers 2

1
$\begingroup$

Yes, your padding and the idea about the length field are correct. Now you just have to append the size of the message as 2 64-bit big-endian integers (128 bit) or one 128-bit big-endian integer. After that your enhanced message should be divisible by 1024.

Source: Pseudocode of SHA-256 of the English Wikipedia. (Notice the additional information about SHA-512.)

$\endgroup$
5
  • $\begingroup$ Could you elaborate on the append the size of the message as a 64-bit big-endian integer? Would this be part of the 80 step process? $\endgroup$
    – Jesse
    Feb 15, 2015 at 23:42
  • $\begingroup$ @InkSlob: To be honest, I have no idea about the whole endian things. Never did understand that fully, because there are many different things to consider which are not explained. Well, okay, only one, but that's enough to get me to not explain that. If someone could answer that I would insert it into my answer. $\endgroup$
    – Nova
    Feb 16, 2015 at 0:13
  • 4
    $\begingroup$ @InkSlob: Actually, the length field is encoded as a 128-bit big endian integer. For a 1919 bit message, the length field would be 0x0000000000000000000000000000077F (that is, the first 14 bytes are 0x00, and the last two are 0x07 and 0x7F); that is the value 1919. It goes in the last 16 bytes of the final block. $\endgroup$
    – poncho
    Feb 16, 2015 at 0:16
  • $\begingroup$ I fully forgot to answer the question about the "80 step process" thing, thank you @poncho! $\endgroup$
    – Nova
    Feb 16, 2015 at 0:51
  • 1
    $\begingroup$ What's this "append the size of the message as 64-bit big-endian integer (128 bit)" supposed to mean? I would understand append the size of the message as two 64-bit big-endian integers (128 bit) or append the size of the message as a 128-bit big-endian integer. $\endgroup$
    – fgrieu
    Oct 7, 2017 at 22:12
6
$\begingroup$

In $\text{SHA-512}$ the size of the blocks is 1024 bit. The last block must contain:

  • the rest of data in message (mod 1024).
  • some filling (padding)
  • the last 128 bits as length

SHA-512

If the message is 1919 bit length:

  1. Calculate the size of the data in the last block: $1919 \mod 1024 = 895$
  2. Add the size of length field(128 bit) to the last block size(895 bit), $128 + 895 = 1023$
  3. See that we have to add 1 bit as padding to the last block to became 1024 bit

The answer will be:

  • size of padding field = 1 bit
  • data of padding field = 1
  • data of length field = 1919 as an unsigned 128-bit big endian integer 0x0000000000000000000000000000077F
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.