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