# What is the length field in SHA-512 padding?

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?

• Yes, it's the length of the message in bits (without padding). – CodesInChaos Feb 15 '15 at 23:27
• Ok, so it would be an unsigned 128-bit integer with the value of 1919? – Jesse Feb 15 '15 at 23:29
• 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. – Jesse Feb 18 '15 at 13:55

## 2 Answers

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.)

• 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? – Jesse Feb 15 '15 at 23:42
• @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. – Nova Feb 16 '15 at 0:13
• @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. – poncho Feb 16 '15 at 0:16
• I fully forgot to answer the question about the "80 step process" thing, thank you @poncho! – Nova Feb 16 '15 at 0:51
• 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. – fgrieu Oct 7 '17 at 22:12

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

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