security/nss/cmd/lowhashtest/lowhashtest.c

Wed, 31 Dec 2014 07:16:47 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:16:47 +0100
branch
TOR_BUG_9701
changeset 3
141e0f1194b1
permissions
-rw-r--r--

Revert simplistic fix pending revisit of Mozilla integration attempt.

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <assert.h>
     5 #include "nspr.h"
     7 /* nss headers */
     8 #include "prtypes.h"
     9 #include "plgetopt.h"
    10 #include "hasht.h"
    11 #include "nsslowhash.h"
    12 #include "secport.h"
    13 #include "hasht.h"
    14 #include "basicutil.h"
    16 static char *progName = NULL;
    18 static int test_long_message(NSSLOWInitContext *initCtx,
    19 	HASH_HashType algoType, unsigned int hashLen,
    20 	const PRUint8 expected[], PRUint8 results[])
    21 {
    22     unsigned int len, i, rv = 0;
    23     NSSLOWHASHContext *ctx;
    25     /* The message is meant to be 'a' repeated 1,000,000 times.
    26      * This is too much to allocate on the stack so we will use a 1,000 char
    27      * buffer and call update 1,000 times.
    28      */
    29     unsigned char buf[1000];
    30     (void) PORT_Memset(buf, 'a', sizeof(buf));
    32     ctx = NSSLOWHASH_NewContext(initCtx, algoType);
    33     if (ctx == NULL) {
    34     	SECU_PrintError(progName, "Couldn't get hash context\n");
    35 	return 1;
    36     }
    38     NSSLOWHASH_Begin(ctx);
    39     for (i = 0; i < 1000; ++i) {
    40 	NSSLOWHASH_Update(ctx, buf, 1000);
    41     }
    43     NSSLOWHASH_End(ctx, results, &len, hashLen);
    44     PR_ASSERT(len == hashLen);
    45     PR_ASSERT(PORT_Memcmp(expected, results, hashLen) == 0);
    46     if (PORT_Memcmp(expected, results, len) != 0) {
    47   	SECU_PrintError(progName, "Hash mismatch\n");
    48 	SECU_PrintBuf(stdout, "Expected: ", expected, hashLen);
    49 	SECU_PrintBuf(stdout, "Actual:   ", results, len);
    50 	rv = 1;
    51     }
    53     NSSLOWHASH_Destroy(ctx);
    54     NSSLOW_Shutdown(initCtx);
    56     return rv;
    57 }
    59 static int test_long_message_sha1(NSSLOWInitContext *initCtx) {
    60     PRUint8 results[SHA1_LENGTH];
    61     /* Test vector from FIPS 180-2: appendix B.3.  */
    63     /* 34aa973c d4c4daa4 f61eeb2b dbad2731 6534016f. */
    64     static const PRUint8 expected[SHA256_LENGTH] =
    65     { 0x34,0xaa,0x97,0x3c, 0xd4,0xc4,0xda,0xa4, 0xf6,0x1e,0xeb,0x2b,
    66       0xdb,0xad,0x27,0x31, 0x65,0x34,0x01,0x6f };
    67     unsigned char buf[1000];
    68     (void) PORT_Memset(buf, 'a', sizeof(buf));
    69     return test_long_message(initCtx, HASH_AlgSHA1,
    70 		SHA1_LENGTH, &expected[0], results);
    71 }
    73 static int test_long_message_sha256(NSSLOWInitContext *initCtx) {
    74     PRUint8 results[SHA256_LENGTH];
    75     /* cdc76e5c 9914fb92 81a1c7e2 84d73e67 f1809a48 a497200e 046d39cc c7112cd0. */
    76     static const PRUint8 expected[SHA256_LENGTH] =
    77     { 0xcd,0xc7,0x6e,0x5c, 0x99,0x14,0xfb,0x92, 0x81,0xa1,0xc7,0xe2, 0x84,0xd7,0x3e,0x67,
    78       0xf1,0x80,0x9a,0x48, 0xa4,0x97,0x20,0x0e, 0x04,0x6d,0x39,0xcc, 0xc7,0x11,0x2c,0xd0 };
    79     unsigned char buf[1000];
    80     (void) PORT_Memset(buf, 'a', sizeof(buf));
    81     return test_long_message(initCtx, HASH_AlgSHA256,
    82 		SHA256_LENGTH, &expected[0], results);
    83 }
    85 static int test_long_message_sha384(NSSLOWInitContext *initCtx) {
    86     PRUint8 results[SHA384_LENGTH];
    87     /* Test vector from FIPS 180-2: appendix B.3.  */
    88     /*
    89 	9d0e1809716474cb 
    90 	086e834e310a4a1c 
    91 	ed149e9c00f24852 
    92 	7972cec5704c2a5b
    93 	07b8b3dc38ecc4eb 
    94 	ae97ddd87f3d8985.
    95     */
    96     static const PRUint8 expected[SHA384_LENGTH] =
    97     { 0x9d,0x0e,0x18,0x09,0x71,0x64,0x74,0xcb, 
    98       0x08,0x6e,0x83,0x4e,0x31,0x0a,0x4a,0x1c, 
    99       0xed,0x14,0x9e,0x9c,0x00,0xf2,0x48,0x52,
   100       0x79,0x72,0xce,0xc5,0x70,0x4c,0x2a,0x5b,
   101       0x07,0xb8,0xb3,0xdc,0x38,0xec,0xc4,0xeb, 
   102       0xae,0x97,0xdd,0xd8,0x7f,0x3d,0x89,0x85 };
   103     unsigned char buf[1000];
   104     (void) PORT_Memset(buf, 'a', sizeof(buf));
   106     return test_long_message(initCtx, HASH_AlgSHA384,
   107 		SHA384_LENGTH, &expected[0], results);
   108 }
   110 static int test_long_message_sha512(NSSLOWInitContext *initCtx) {
   111     PRUint8 results[SHA512_LENGTH];
   112     /* Test vector from FIPS 180-2: appendix B.3.  */
   113     static const PRUint8 expected[SHA512_LENGTH] =
   114     { 0xe7,0x18,0x48,0x3d,0x0c,0xe7,0x69,0x64,0x4e,0x2e,0x42,0xc7,0xbc,0x15,0xb4,0x63,
   115       0x8e,0x1f,0x98,0xb1,0x3b,0x20,0x44,0x28,0x56,0x32,0xa8,0x03,0xaf,0xa9,0x73,0xeb,
   116       0xde,0x0f,0xf2,0x44,0x87,0x7e,0xa6,0x0a,0x4c,0xb0,0x43,0x2c,0xe5,0x77,0xc3,0x1b,
   117       0xeb,0x00,0x9c,0x5c,0x2c,0x49,0xaa,0x2e,0x4e,0xad,0xb2,0x17,0xad,0x8c,0xc0,0x9b};
   118     unsigned char buf[1000];
   119     (void) PORT_Memset(buf, 'a', sizeof(buf));
   121     return test_long_message(initCtx, HASH_AlgSHA512,
   122 		SHA512_LENGTH, &expected[0], results);
   123 }
   126 static int testMessageDigest(NSSLOWInitContext *initCtx,
   127 	HASH_HashType algoType, unsigned int hashLen,
   128 	const unsigned char *message,
   129 	const PRUint8 expected[], PRUint8 results[])
   130 {
   131     NSSLOWHASHContext *ctx;
   132     unsigned int len;
   133     int rv = 0;
   135     ctx = NSSLOWHASH_NewContext(initCtx, algoType);
   136     if (ctx == NULL) {
   137     	SECU_PrintError(progName, "Couldn't get hash context\n");
   138 	return 1;
   139     }
   141     NSSLOWHASH_Begin(ctx);
   142     NSSLOWHASH_Update(ctx, message, PORT_Strlen((const char *)message));
   143     NSSLOWHASH_End(ctx, results, &len, hashLen);
   144     PR_ASSERT(len == hashLen);
   145     PR_ASSERT(PORT_Memcmp(expected, results, len) == 0);
   147     if (PORT_Memcmp(expected, results, len) != 0) {
   148   	SECU_PrintError(progName, "Hash mismatch\n");
   149 	SECU_PrintBuf(stdout, "Expected: ", expected, hashLen);
   150 	SECU_PrintBuf(stdout, "Actual:   ", results, len);
   151 	rv = 1;
   152     }
   154     NSSLOWHASH_Destroy(ctx);
   155     NSSLOW_Shutdown(initCtx);
   157     return rv;
   158 }
   161 static int testMD5(NSSLOWInitContext *initCtx)
   162 {
   163 	/* test vectors that glibc, our API main client, uses */
   165     static const struct {
   166 	const unsigned char *input;
   167 	const PRUint8 result[MD5_LENGTH];
   168    } md5tests[] = {
   169 	{ (unsigned char *) "",
   170 	  {0xd4,0x1d,0x8c,0xd9,0x8f,0x00,0xb2,0x04,0xe9,0x80,0x09,0x98,0xec,0xf8,0x42,0x7e} },
   171 	{ (unsigned char *) "a",
   172 	  {0x0c,0xc1,0x75,0xb9,0xc0,0xf1,0xb6,0xa8,0x31,0xc3,0x99,0xe2,0x69,0x77,0x26,0x61} },
   173 	{ (unsigned char *) "abc",
   174 	  {0x90,0x01,0x50,0x98,0x3c,0xd2,0x4f,0xb0,0xd6,0x96,0x3f,0x7d,0x28,0xe1,0x7f,0x72} },
   175 	{ (unsigned char *) "message digest",
   176 	  {0xf9,0x6b,0x69,0x7d,0x7c,0xb7,0x93,0x8d,0x52,0x5a,0x2f,0x31,0xaa,0xf1,0x61,0xd0} },
   177 	{ (unsigned char *) "abcdefghijklmnopqrstuvwxyz",
   178 	  {0xc3,0xfc,0xd3,0xd7,0x61,0x92,0xe4,0x00,0x7d,0xfb,0x49,0x6c,0xca,0x67,0xe1,0x3b} },
   179 	{ (unsigned char *) "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
   180 	  {0xd1,0x74,0xab,0x98,0xd2,0x77,0xd9,0xf5,0xa5,0x61,0x1c,0x2c,0x9f,0x41,0x9d,0x9f} },
   181 	{ (unsigned char *) "123456789012345678901234567890123456789012345678901234567890"
   182 	  "12345678901234567890",
   183 	  {0x57,0xed,0xf4,0xa2,0x2b,0xe3,0xc9,0x55,0xac,0x49,0xda,0x2e,0x21,0x07,0xb6,0x7a} }
   184     };
   185     PRUint8 results[MD5_LENGTH];
   186     int rv = 0, cnt, numTests;
   188     numTests = sizeof(md5tests)/sizeof(md5tests[0]);
   189     for (cnt = 0; cnt < numTests; cnt++) {
   190 	rv += testMessageDigest(initCtx, HASH_AlgMD5, MD5_LENGTH,
   191         		(const unsigned char *) md5tests[cnt].input,
   192         		md5tests[cnt].result, &results[0]);
   193     }
   194     return rv;
   195 }
   197 /*
   198  * Tests with test vectors from FIPS 180-2 Appendixes B.1, B.2, B.3, C, and D
   199  *
   200  */
   202 static int testSHA1(NSSLOWInitContext *initCtx)
   203 {
   204     static const struct {
   205 	const unsigned char *input;
   206 	const PRUint8 result[SHA1_LENGTH];
   207     } sha1tests[] = {
   208 	/* one block messsage */
   209 	{ (const unsigned char *)
   210 	  "abc",
   211 	   /* a9993e36 4706816a ba3e2571 7850c26c 9cd0d89d. */
   213 	  { 0xa9,0x99,0x3e,0x36, 0x47,0x06,0x81,0x6a, /* a9993e36 4706816a */
   214 	    0xba,0x3e,0x25,0x71, /* ba3e2571 */
   215 	    0x78,0x50,0xc2,0x6c, 0x9c,0xd0,0xd8,0x9d} /* 7850c26c 9cd0d89d */
   216 	},
   217 	{ (const unsigned char *)
   218 	  "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
   219 	  /* 84983e44 1c3bd26e baae4aa1 f95129e5 e54670f1. */
   220 	  {0x84,0x98,0x3e,0x44, 0x1c,0x3b,0xd2,0x6e, 0xba,0xae,0x4a,0xa1,
   221 	   0xf9,0x51,0x29,0xe5, 0xe5,0x46,0x70,0xf1}
   222 	}
   223     };
   225     PRUint8 results[SHA1_LENGTH];
   226     int rv = 0, cnt, numTests;
   228     numTests = sizeof(sha1tests)/sizeof(sha1tests[0]);
   229     for (cnt = 0; cnt < numTests; cnt++) {
   230 	rv += testMessageDigest(initCtx, HASH_AlgSHA1, SHA1_LENGTH,
   231         		(const unsigned char *) sha1tests[cnt].input,
   232         		sha1tests[cnt].result, &results[0]);
   233     }
   235     rv += test_long_message_sha1(initCtx);
   236     return rv;
   237 }
   239 static int testSHA224(NSSLOWInitContext *initCtx)
   240 {
   241     static const struct {
   242 	const unsigned char *input;
   243 	const PRUint8 result[SHA224_LENGTH];
   244     } sha224tests[] = {
   245 	/* one block messsage */
   246 	{ (const unsigned char *) "abc",
   247 	  {0x23,0x09,0x7D,0x22,0x34,0x05,0xD8,0x22,0x86,0x42,0xA4,0x77,0xBD,0xA2,0x55,0xB3,
   248 	   0x2A,0xAD,0xBC,0xE4,0xBD,0xA0,0xB3,0xF7,0xE3,0x6C,0x9D,0xA7}
   249 	},
   250 	 /* two block message */
   251 	{ (const unsigned char *) "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
   252 	  {0x75,0x38,0x8B,0x16,0x51,0x27,0x76,0xCC,0x5D,0xBA,0x5D,0xA1,0xFD,0x89,0x01,0x50,
   253 	  0xB0,0xC6,0x45,0x5C,0xB4,0xF5,0x8B,0x19,0x52,0x52,0x25,0x25}
   254 	}
   255     };
   257     PRUint8 results[SHA224_LENGTH];
   258     int rv = 0, cnt, numTests;
   260     numTests = sizeof(sha224tests)/sizeof(sha224tests[0]);
   261     for (cnt = 0; cnt < numTests; cnt++) {
   262 	rv += testMessageDigest(initCtx, HASH_AlgSHA224, SHA224_LENGTH,
   263         		(const unsigned char *) sha224tests[cnt].input,
   264         		sha224tests[cnt].result, &results[0]);
   265     }
   267     return rv;
   268 }
   270 static int testSHA256(NSSLOWInitContext *initCtx)
   271 {
   272     static const struct {
   273 	const unsigned char *input;
   274 	const PRUint8 result[SHA256_LENGTH];
   275     } sha256tests[] = {
   276 	/* Test vectors from FIPS 180-2: appendix B.1.  */
   277 	{ (unsigned char *) "abc",
   278 	  {0xba,0x78,0x16,0xbf,0x8f,0x01,0xcf,0xea,0x41,0x41,0x40,0xde,0x5d,0xae,0x22,0x23,
   279 	   0xb0,0x03,0x61,0xa3,0x96,0x17,0x7a,0x9c,0xb4,0x10,0xff,0x61,0xf2,0x00,0x15,0xad} 
   280 	},
   281 	/* Test vectors from FIPS 180-2: appendix B.2.  */
   282 	{ (unsigned char *) "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
   283 	  {0x24,0x8d,0x6a,0x61,0xd2,0x06,0x38,0xb8,0xe5,0xc0,0x26,0x93,0x0c,0x3e,0x60,0x39,
   284 	   0xa3,0x3c,0xe4,0x59,0x64,0xff,0x21,0x67,0xf6,0xec,0xed,0xd4,0x19,0xdb,0x06,0xc1} 
   285 	}
   286     };
   288     PRUint8 results[SHA256_LENGTH];
   289     int rv = 0, cnt, numTests;
   291     numTests = sizeof(sha256tests)/sizeof(sha256tests[0]);
   292     for (cnt = 0; cnt < numTests; cnt++) {
   293 	rv += testMessageDigest(initCtx, HASH_AlgSHA256, SHA256_LENGTH,
   294         		(const unsigned char *) sha256tests[cnt].input,
   295         		sha256tests[cnt].result, &results[0]);
   296     }
   298     rv += test_long_message_sha256(initCtx);
   299     return rv;
   300 }
   302 static int testSHA384(NSSLOWInitContext *initCtx)
   303 {
   304     static const struct {
   305 	const unsigned char *input;
   306 	const PRUint8 result[SHA384_LENGTH];
   307     } sha384tests[] = {
   308 	/* Test vector from FIPS 180-2: appendix D, single-block message.  */
   309 	{ (unsigned char *) "abc",
   310 	  {0xcb,0x00,0x75,0x3f,0x45,0xa3,0x5e,0x8b,
   311 	   0xb5,0xa0,0x3d,0x69,0x9a,0xc6,0x50,0x07,
   312 	   0x27,0x2c,0x32,0xab,0x0e,0xde,0xd1,0x63,
   313 	   0x1a,0x8b,0x60,0x5a,0x43,0xff,0x5b,0xed,
   314 	   0x80,0x86,0x07,0x2b,0xa1,0xe7,0xcc,0x23,
   315 	   0x58,0xba,0xec,0xa1,0x34,0xc8,0x25,0xa7} },
   317 	/* Test vectors from FIPS 180-2: appendix D, multi-block message.  */
   318 	{ (unsigned char *)
   319 	  "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
   320 	  "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
   321         /* 
   322 	09330c33f71147e8 
   323         3d192fc782cd1b47 
   324         53111b173b3b05d2 
   325         2fa08086e3b0f712
   326 	fcc7c71a557e2db9 
   327         66c3e9fa91746039.
   328         */
   329 	{0x09,0x33,0x0c,0x33,0xf7,0x11,0x47,0xe8,
   330          0x3d,0x19,0x2f,0xc7,0x82,0xcd,0x1b,0x47,
   331          0x53,0x11,0x1b,0x17,0x3b,0x3b,0x05,0xd2,
   332 	 0x2f,0xa0,0x80,0x86,0xe3,0xb0,0xf7,0x12,
   333 	 0xfc,0xc7,0xc7,0x1a,0x55,0x7e,0x2d,0xb9,
   334          0x66,0xc3,0xe9,0xfa,0x91,0x74,0x60,0x39} }
   335    };
   337     PRUint8 results[SHA384_LENGTH];
   338     int rv = 0, cnt, numTests;
   340     numTests = sizeof(sha384tests)/sizeof(sha384tests[0]);
   341     for (cnt = 0; cnt < numTests; cnt++) {
   342         rv += testMessageDigest(initCtx, HASH_AlgSHA384, SHA384_LENGTH,
   343         			(const unsigned char *) sha384tests[cnt].input,
   344         			sha384tests[cnt].result, &results[0]);
   345     }
   346     rv += test_long_message_sha384(initCtx);
   348     return rv;
   349 }
   352 int testSHA512(NSSLOWInitContext *initCtx)
   353 {
   354     static const struct {
   355 	const unsigned char *input;
   356 	const PRUint8 result[SHA512_LENGTH];
   357     } sha512tests[] = {
   358 	/* Test vectors from FIPS 180-2: appendix C.1.  */
   359 	{ (unsigned char *) "abc",
   360 	  { 0xdd,0xaf,0x35,0xa1,0x93,0x61,0x7a,0xba,0xcc,0x41,0x73,0x49,0xae,0x20,0x41,0x31,
   361 	    0x12,0xe6,0xfa,0x4e,0x89,0xa9,0x7e,0xa2,0x0a,0x9e,0xee,0xe6,0x4b,0x55,0xd3,0x9a,
   362 	    0x21,0x92,0x99,0x2a,0x27,0x4f,0xc1,0xa8,0x36,0xba,0x3c,0x23,0xa3,0xfe,0xeb,0xbd,
   363 	    0x45,0x4d,0x44,0x23,0x64,0x3c,0xe8,0x0e,0x2a,0x9a,0xc9,0x4f,0xa5,0x4c,0xa4,0x9f} 
   364 	},
   365 	/* Test vectors from FIPS 180-2: appendix C.2.  */
   366 	{ (unsigned char *) "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
   367 	  "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
   368 	  {0x8e,0x95,0x9b,0x75,0xda,0xe3,0x13,0xda,0x8c,0xf4,0xf7,0x28,0x14,0xfc,0x14,0x3f,
   369 	   0x8f,0x77,0x79,0xc6,0xeb,0x9f,0x7f,0xa1,0x72,0x99,0xae,0xad,0xb6,0x88,0x90,0x18,
   370 	   0x50,0x1d,0x28,0x9e,0x49,0x00,0xf7,0xe4,0x33,0x1b,0x99,0xde,0xc4,0xb5,0x43,0x3a,
   371 	   0xc7,0xd3,0x29,0xee,0xb6,0xdd,0x26,0x54,0x5e,0x96,0xe5,0x5b,0x87,0x4b,0xe9,0x09} 
   372 	}
   373     };
   375     PRUint8 results[SHA512_LENGTH];
   376     int rv = 0, cnt, numTests;
   378     numTests = sizeof(sha512tests)/sizeof(sha512tests[0]);
   379     for (cnt = 0; cnt < numTests; cnt++) {
   380 	rv = testMessageDigest(initCtx, HASH_AlgSHA512, SHA512_LENGTH,
   381         			(const unsigned char *) sha512tests[cnt].input,
   382         			sha512tests[cnt].result, &results[0]);
   383     }
   384     rv += test_long_message_sha512(initCtx);
   385     return rv;
   386 }
   388 static void
   389 Usage(char *progName)
   390 {
   391     fprintf(stderr, "Usage: %s [algorithm]\n",
   392 	    progName);
   393     fprintf(stderr, "algorithm must be one of %s\n",
   394 	    "{ MD5 | SHA1 | SHA224 | SHA256 | SHA384 | SHA512 }");
   395     fprintf(stderr, "default is to test all\n");
   396     exit(-1);
   397 }
   399 int main(int argc, char **argv)
   400 {
   401     NSSLOWInitContext *initCtx;
   402     int rv = 0; /* counts the number of failures */
   404     progName = strrchr(argv[0], '/');
   405     progName = progName ? progName+1 : argv[0];
   407     initCtx = NSSLOW_Init();
   408     if (initCtx == NULL) {
   409 	SECU_PrintError(progName, "Couldn't initialize for hashing\n");
   410 	return 1;
   411     }
   413     if (argc || !argv[1] || strlen(argv[1]) == 0) {
   414 	rv += testMD5(initCtx);
   415 	rv += testSHA1(initCtx);
   416 	rv += testSHA224(initCtx);
   417 	rv += testSHA256(initCtx);
   418 	rv += testSHA384(initCtx);
   419 	rv += testSHA512(initCtx);
   420     } else if (strcmp(argv[1], "MD5") == 0) {
   421 	rv += testMD5(initCtx);
   422     } else if (strcmp(argv[1], "SHA1") == 0) {
   423 	rv += testSHA1(initCtx);
   424     } else if (strcmp(argv[1], "SHA224") == 0) {
   425 	rv += testSHA224(initCtx);
   426     } else if (strcmp(argv[1], "SHA226") == 0) {
   427 	rv += testSHA256(initCtx);
   428     } else if (strcmp(argv[1], "SHA384") == 0) {
   429 	rv += testSHA384(initCtx);
   430     } else if (strcmp(argv[1], "SHA512") == 0) {
   431 	rv += testSHA512(initCtx);
   432     } else {
   433     	SECU_PrintError(progName, "Unsupported hash type %s\n", argv[0]);
   434     	Usage(progName);
   435     }
   437     NSSLOW_Shutdown(initCtx);
   439     return (rv == 0) ? 0 : 1;
   440 }

mercurial