Jump to content
  • Advertisement
Sign in to follow this  
TheShau

SHA1 padding

This topic is 4514 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to write a SHA1 implementation using masm32. This isnt by itself that big of a chalange but I've come accross one issue. In the padding move, When the data is completed to a 512 bit boundary I'm not quite clear on the algorithm. I've been using the RFC 3174 document and it states that the data should be padded with an on bit, then a string of N off bits and then the length of the data in bits as a 64-bit unsigned integer. Where N is calculated to fill in the missing space. What I dont get is what The algorithm should do if there isnt enough room untill the next 512 bit boundary. For example if the data is 62 bytes long, Then I dont have room to put in the on bit and the length. Obviously I should either add another block, or truncate the last one, but which is it? I dont think putting a "This data cannot be hashed." message would work. :) Thanks in advanced, TheShau

Share this post


Link to post
Share on other sites
Advertisement
You should indeed let the padding spill over into the next 512-bit block.

In the C reference implementation of SHA1 in section 7 of RFC 3174, the comments in the SHA1PadMessage() function on page 17 state:

/*  Check to see if the current message block is too small to hold
* the initial padding bits and length. If so, we will pad the
* block, process it, and then continue padding into a second
* block.
*/


Hope this helps.

Share this post


Link to post
Share on other sites
Do this
  1. append a "1" bit
  2. append 0-511 "0" bits until message length = 448 (mod 512)
  3. append the length of message as a 64-bit big-endian integer
There will be 65 to 576 bits of padding (1 "1" bit, 0-511 "0" bits, 64-bit length).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!