OpenSSL SHA1 not validating NIST SHAKE test vector?

坚强是说给别人听的谎言 提交于 2020-01-17 07:14:44

问题


I have written a C code which calls on OpenSSL SHA1 to generate a digest but my generated digest is not matching.

#define MSG_SIZE 190
#include <stdio.h>
#include <string.h>
#include <openssl/sha.h>

void str2hex(char *str, unsigned char *hex, int len) {
 int tt, ss;
 char temp[5];
 for (tt = 0, ss = 0; tt < len, ss < 2 * len; tt++, ss += 2) {
   temp[0] = '0';
   temp[1] = 'x';
   temp[2] = str[ss];
   temp[3] = str[ss + 1];
   temp[4] = 0;

  hex[tt] = strtol(temp, NULL, 0);
 }
}

int main() {
 unsigned char digest[MSG_SIZE],stringt[MSG_SIZE];
 int i;
 char *string = "df1300";

 str2hex(string, stringt, MSG_SIZE);

 SHA_CTX ctx;
 SHA1_Init(&ctx);
 SHA1_Update(&ctx, stringt, strlen(string)/2);
 SHA1_Final(digest, &ctx);

 printf("SHA1 digest\n");

 for (i = 0; i < 16; i++)
   printf("%02x", digest[i]);

 printf("\n");
 return 0;
}

The output i am receiving is

SHA1 digest
0450c758f08f726ef77880a15118a237

But the output should be (from NIST test vectors for SHAKE):

daf289eea10de7fef76e085ad70a3bc6

If anyone could have a look at my code and let me know where I am doing wrong it will be helpful.


回答1:


Aside from you showing on the first 16 of 20 bytes, you've computed the right answer.

$ echo df1300 | xxd -r -p | openssl dgst -sha1
(stdin)= 0450c758f08f726ef77880a15118a2379e45f032

$ echo df1300 | xxd -r -p | sha1sum
0450c758f08f726ef77880a15118a2379e45f032  -

The CAVP test vectors at http://csrc.nist.gov/groups/STM/cavp/secure-hashing.html#shavs (FIPS 180-4) don't list df1300 as a test case, so it's unclear what you're sourcing and why it's wrong.

Since you're doing 16 bytes, maybe you meant MD5? Let's just try them all:

$ openssl dgst -?
...
-md4            to use the md4 message digest algorithm
-md5            to use the md5 message digest algorithm
-ripemd160      to use the ripemd160 message digest algorithm
-sha            to use the sha message digest algorithm
-sha1           to use the sha1 message digest algorithm
-sha224         to use the sha224 message digest algorithm
-sha256         to use the sha256 message digest algorithm
-sha384         to use the sha384 message digest algorithm
-sha512         to use the sha512 message digest algorithm
-whirlpool      to use the whirlpool message digest algorithm

$ echo df1300 | xxd -r -p | openssl dgst -md4
(stdin)= 76dfc0e2cf5e5206ed12237d98018416
$ echo df1300 | xxd -r -p | openssl dgst -md5
(stdin)= 7fca8bd4d9be0b3f0e1783ad3aed3413
$ echo df1300 | xxd -r -p | openssl dgst -ripemd160
(stdin)= 30797300cf85c2c5f9135053a757b578cb5856f4
$ echo df1300 | xxd -r -p | openssl dgst -sha
(stdin)= 01f78f8a5c08ccb197d01c005f203e326bf435a2
$ echo df1300 | xxd -r -p | openssl dgst -sha1
(stdin)= 0450c758f08f726ef77880a15118a2379e45f032
$ echo df1300 | xxd -r -p | openssl dgst -sha224
(stdin)= 1b680a76915e27d88c269d63817e04dbca207dd38cca7e9d927a3c40
$ echo df1300 | xxd -r -p | openssl dgst -sha256
(stdin)= f0ac1a347ed5d335113b20a763533bea7d7fff610f5f143d811297aa95dd5f0a
$ echo df1300 | xxd -r -p | openssl dgst -sha384
(stdin)= 6b18f2ef884a7126d88389b4410e46524b0f501af3f847eb4c4fe0e97ffc24735e30a247799768cac4c38d0d85235502
$ echo df1300 | xxd -r -p | openssl dgst -sha512
(stdin)= 4752e6b0bc2d2e80dec50546ea0eb8aad04b944193c3f1eda7b82c2c0bab042ccad2f20f609b8f1dd6abbc3b480d0478a2cf636a3a917706ed785b0d56375a3c
$ echo df1300 | xxd -r -p | openssl dgst -whirlpool
(stdin)= f19a5cd03adb2856eec83f88e8d39d34ac591d61e37fbb3831a82a12177d19a761b08df38a4f8d591a694cfd0ff81ef46651d943b4fabec192f64cb4befd00ac

But none of these look like your expected value, either in a big-endian or little-endian representation. Maybe you're looking at an HMAC?

To cross-check OpenSSL's accuracy, let's take an example from the CAVP11 dataset that's also three bytes:

Len = 24
Msg = df4bd2
MD = bf36ed5d74727dfd5d7854ec6b1d49468d8ee8aa

$ echo df4bd2 | xxd -r -p | openssl dgst -sha1
(stdin)= bf36ed5d74727dfd5d7854ec6b1d49468d8ee8aa

Yep, looks good.



来源:https://stackoverflow.com/questions/43656920/openssl-sha1-not-validating-nist-shake-test-vector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!