Search code examples
opensslcryptographysha1

Getting Message Digest from SHA1 message


I am looking at this page on how to validate SHA1 implementation on a platform: http://csrc.nist.gov/groups/STM/cavp/secure-hashing.html#sha-1

So I open up the file and view the test values: https://pastebin.com/SL4WaAtM it is thousands of lines long but this is the start(BIT oriented implementations).

I focus on the first values:

Len = 2
Msg = 40
MD = ec6b39952e1a3ec3ab3507185cf756181c84bbe2

My understanding is that with a Message that openssl would get the MD, however I am not getting the same MD as that above?

 echo -n "40" | xxd -r -p | openssl dgst -sha1 
 (stdin)= 9a78211436f6d425ec38f5c4e02270801f3524f8

Any ideas on what I am doing wrong?.Other values don't match either.


Solution

  • You're interpreting the data incorrectly. Len is the number of bits in the message. By interacting with openssl on the command line, you are (to the best of my knowledge) restricted to working with whole bytes.

    So for example Len = 2 and M = 40 means you should be hashing the first two bits of 0x40 (=01) and not the whole byte (01000000). On the command line, I don't think it's possible to feed openssl with bit streams that aren't a multiple of 8 bits in length, so you'll have to interface directly with the ssl library code if you want to do this.

    However, you can test the bit patterns that are a multiple of 8 bits in length:

    0 bits:
    echo -ne "" | openssl sha1
    (stdin)= da39a3ee5e6b4b0d3255bfef95601890afd80709
    
    8 bits:
    echo -ne "\x5d" | openssl sha1
    (stdin)= 4ff447b8ef42ca51fa6fb287bed8d40f49be58f1
    
    16 bits:
    echo -ne "\x53\xa1" | openssl sha1
    (stdin)= c9066463926e470db1ba15cbd06e614dbf0bc9a7
    
    etc...