security/nss/cmd/lowhashtest/lowhashtest.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/cmd/lowhashtest/lowhashtest.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,441 @@
     1.4 +#include <stdio.h>
     1.5 +#include <string.h>
     1.6 +#include <assert.h>
     1.7 +
     1.8 +#include "nspr.h"
     1.9 +
    1.10 +/* nss headers */
    1.11 +#include "prtypes.h"
    1.12 +#include "plgetopt.h"
    1.13 +#include "hasht.h"
    1.14 +#include "nsslowhash.h"
    1.15 +#include "secport.h"
    1.16 +#include "hasht.h"
    1.17 +#include "basicutil.h"
    1.18 +
    1.19 +static char *progName = NULL;
    1.20 +
    1.21 +static int test_long_message(NSSLOWInitContext *initCtx,
    1.22 +	HASH_HashType algoType, unsigned int hashLen,
    1.23 +	const PRUint8 expected[], PRUint8 results[])
    1.24 +{
    1.25 +    unsigned int len, i, rv = 0;
    1.26 +    NSSLOWHASHContext *ctx;
    1.27 +
    1.28 +    /* The message is meant to be 'a' repeated 1,000,000 times.
    1.29 +     * This is too much to allocate on the stack so we will use a 1,000 char
    1.30 +     * buffer and call update 1,000 times.
    1.31 +     */
    1.32 +    unsigned char buf[1000];
    1.33 +    (void) PORT_Memset(buf, 'a', sizeof(buf));
    1.34 +
    1.35 +    ctx = NSSLOWHASH_NewContext(initCtx, algoType);
    1.36 +    if (ctx == NULL) {
    1.37 +    	SECU_PrintError(progName, "Couldn't get hash context\n");
    1.38 +	return 1;
    1.39 +    }
    1.40 +
    1.41 +    NSSLOWHASH_Begin(ctx);
    1.42 +    for (i = 0; i < 1000; ++i) {
    1.43 +	NSSLOWHASH_Update(ctx, buf, 1000);
    1.44 +    }
    1.45 +
    1.46 +    NSSLOWHASH_End(ctx, results, &len, hashLen);
    1.47 +    PR_ASSERT(len == hashLen);
    1.48 +    PR_ASSERT(PORT_Memcmp(expected, results, hashLen) == 0);
    1.49 +    if (PORT_Memcmp(expected, results, len) != 0) {
    1.50 +  	SECU_PrintError(progName, "Hash mismatch\n");
    1.51 +	SECU_PrintBuf(stdout, "Expected: ", expected, hashLen);
    1.52 +	SECU_PrintBuf(stdout, "Actual:   ", results, len);
    1.53 +	rv = 1;
    1.54 +    }
    1.55 +
    1.56 +    NSSLOWHASH_Destroy(ctx);
    1.57 +    NSSLOW_Shutdown(initCtx);
    1.58 +
    1.59 +    return rv;
    1.60 +}
    1.61 +
    1.62 +static int test_long_message_sha1(NSSLOWInitContext *initCtx) {
    1.63 +    PRUint8 results[SHA1_LENGTH];
    1.64 +    /* Test vector from FIPS 180-2: appendix B.3.  */
    1.65 +
    1.66 +    /* 34aa973c d4c4daa4 f61eeb2b dbad2731 6534016f. */
    1.67 +    static const PRUint8 expected[SHA256_LENGTH] =
    1.68 +    { 0x34,0xaa,0x97,0x3c, 0xd4,0xc4,0xda,0xa4, 0xf6,0x1e,0xeb,0x2b,
    1.69 +      0xdb,0xad,0x27,0x31, 0x65,0x34,0x01,0x6f };
    1.70 +    unsigned char buf[1000];
    1.71 +    (void) PORT_Memset(buf, 'a', sizeof(buf));
    1.72 +    return test_long_message(initCtx, HASH_AlgSHA1,
    1.73 +		SHA1_LENGTH, &expected[0], results);
    1.74 +}
    1.75 +
    1.76 +static int test_long_message_sha256(NSSLOWInitContext *initCtx) {
    1.77 +    PRUint8 results[SHA256_LENGTH];
    1.78 +    /* cdc76e5c 9914fb92 81a1c7e2 84d73e67 f1809a48 a497200e 046d39cc c7112cd0. */
    1.79 +    static const PRUint8 expected[SHA256_LENGTH] =
    1.80 +    { 0xcd,0xc7,0x6e,0x5c, 0x99,0x14,0xfb,0x92, 0x81,0xa1,0xc7,0xe2, 0x84,0xd7,0x3e,0x67,
    1.81 +      0xf1,0x80,0x9a,0x48, 0xa4,0x97,0x20,0x0e, 0x04,0x6d,0x39,0xcc, 0xc7,0x11,0x2c,0xd0 };
    1.82 +    unsigned char buf[1000];
    1.83 +    (void) PORT_Memset(buf, 'a', sizeof(buf));
    1.84 +    return test_long_message(initCtx, HASH_AlgSHA256,
    1.85 +		SHA256_LENGTH, &expected[0], results);
    1.86 +}
    1.87 +
    1.88 +static int test_long_message_sha384(NSSLOWInitContext *initCtx) {
    1.89 +    PRUint8 results[SHA384_LENGTH];
    1.90 +    /* Test vector from FIPS 180-2: appendix B.3.  */
    1.91 +    /*
    1.92 +	9d0e1809716474cb 
    1.93 +	086e834e310a4a1c 
    1.94 +	ed149e9c00f24852 
    1.95 +	7972cec5704c2a5b
    1.96 +	07b8b3dc38ecc4eb 
    1.97 +	ae97ddd87f3d8985.
    1.98 +    */
    1.99 +    static const PRUint8 expected[SHA384_LENGTH] =
   1.100 +    { 0x9d,0x0e,0x18,0x09,0x71,0x64,0x74,0xcb, 
   1.101 +      0x08,0x6e,0x83,0x4e,0x31,0x0a,0x4a,0x1c, 
   1.102 +      0xed,0x14,0x9e,0x9c,0x00,0xf2,0x48,0x52,
   1.103 +      0x79,0x72,0xce,0xc5,0x70,0x4c,0x2a,0x5b,
   1.104 +      0x07,0xb8,0xb3,0xdc,0x38,0xec,0xc4,0xeb, 
   1.105 +      0xae,0x97,0xdd,0xd8,0x7f,0x3d,0x89,0x85 };
   1.106 +    unsigned char buf[1000];
   1.107 +    (void) PORT_Memset(buf, 'a', sizeof(buf));
   1.108 +
   1.109 +    return test_long_message(initCtx, HASH_AlgSHA384,
   1.110 +		SHA384_LENGTH, &expected[0], results);
   1.111 +}
   1.112 +
   1.113 +static int test_long_message_sha512(NSSLOWInitContext *initCtx) {
   1.114 +    PRUint8 results[SHA512_LENGTH];
   1.115 +    /* Test vector from FIPS 180-2: appendix B.3.  */
   1.116 +    static const PRUint8 expected[SHA512_LENGTH] =
   1.117 +    { 0xe7,0x18,0x48,0x3d,0x0c,0xe7,0x69,0x64,0x4e,0x2e,0x42,0xc7,0xbc,0x15,0xb4,0x63,
   1.118 +      0x8e,0x1f,0x98,0xb1,0x3b,0x20,0x44,0x28,0x56,0x32,0xa8,0x03,0xaf,0xa9,0x73,0xeb,
   1.119 +      0xde,0x0f,0xf2,0x44,0x87,0x7e,0xa6,0x0a,0x4c,0xb0,0x43,0x2c,0xe5,0x77,0xc3,0x1b,
   1.120 +      0xeb,0x00,0x9c,0x5c,0x2c,0x49,0xaa,0x2e,0x4e,0xad,0xb2,0x17,0xad,0x8c,0xc0,0x9b};
   1.121 +    unsigned char buf[1000];
   1.122 +    (void) PORT_Memset(buf, 'a', sizeof(buf));
   1.123 +
   1.124 +    return test_long_message(initCtx, HASH_AlgSHA512,
   1.125 +		SHA512_LENGTH, &expected[0], results);
   1.126 +}
   1.127 +
   1.128 +
   1.129 +static int testMessageDigest(NSSLOWInitContext *initCtx,
   1.130 +	HASH_HashType algoType, unsigned int hashLen,
   1.131 +	const unsigned char *message,
   1.132 +	const PRUint8 expected[], PRUint8 results[])
   1.133 +{
   1.134 +    NSSLOWHASHContext *ctx;
   1.135 +    unsigned int len;
   1.136 +    int rv = 0;
   1.137 +
   1.138 +    ctx = NSSLOWHASH_NewContext(initCtx, algoType);
   1.139 +    if (ctx == NULL) {
   1.140 +    	SECU_PrintError(progName, "Couldn't get hash context\n");
   1.141 +	return 1;
   1.142 +    }
   1.143 +
   1.144 +    NSSLOWHASH_Begin(ctx);
   1.145 +    NSSLOWHASH_Update(ctx, message, PORT_Strlen((const char *)message));
   1.146 +    NSSLOWHASH_End(ctx, results, &len, hashLen);
   1.147 +    PR_ASSERT(len == hashLen);
   1.148 +    PR_ASSERT(PORT_Memcmp(expected, results, len) == 0);
   1.149 +
   1.150 +    if (PORT_Memcmp(expected, results, len) != 0) {
   1.151 +  	SECU_PrintError(progName, "Hash mismatch\n");
   1.152 +	SECU_PrintBuf(stdout, "Expected: ", expected, hashLen);
   1.153 +	SECU_PrintBuf(stdout, "Actual:   ", results, len);
   1.154 +	rv = 1;
   1.155 +    }
   1.156 +
   1.157 +    NSSLOWHASH_Destroy(ctx);
   1.158 +    NSSLOW_Shutdown(initCtx);
   1.159 +
   1.160 +    return rv;
   1.161 +}
   1.162 +
   1.163 +
   1.164 +static int testMD5(NSSLOWInitContext *initCtx)
   1.165 +{
   1.166 +	/* test vectors that glibc, our API main client, uses */
   1.167 +
   1.168 +    static const struct {
   1.169 +	const unsigned char *input;
   1.170 +	const PRUint8 result[MD5_LENGTH];
   1.171 +   } md5tests[] = {
   1.172 +	{ (unsigned char *) "",
   1.173 +	  {0xd4,0x1d,0x8c,0xd9,0x8f,0x00,0xb2,0x04,0xe9,0x80,0x09,0x98,0xec,0xf8,0x42,0x7e} },
   1.174 +	{ (unsigned char *) "a",
   1.175 +	  {0x0c,0xc1,0x75,0xb9,0xc0,0xf1,0xb6,0xa8,0x31,0xc3,0x99,0xe2,0x69,0x77,0x26,0x61} },
   1.176 +	{ (unsigned char *) "abc",
   1.177 +	  {0x90,0x01,0x50,0x98,0x3c,0xd2,0x4f,0xb0,0xd6,0x96,0x3f,0x7d,0x28,0xe1,0x7f,0x72} },
   1.178 +	{ (unsigned char *) "message digest",
   1.179 +	  {0xf9,0x6b,0x69,0x7d,0x7c,0xb7,0x93,0x8d,0x52,0x5a,0x2f,0x31,0xaa,0xf1,0x61,0xd0} },
   1.180 +	{ (unsigned char *) "abcdefghijklmnopqrstuvwxyz",
   1.181 +	  {0xc3,0xfc,0xd3,0xd7,0x61,0x92,0xe4,0x00,0x7d,0xfb,0x49,0x6c,0xca,0x67,0xe1,0x3b} },
   1.182 +	{ (unsigned char *) "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
   1.183 +	  {0xd1,0x74,0xab,0x98,0xd2,0x77,0xd9,0xf5,0xa5,0x61,0x1c,0x2c,0x9f,0x41,0x9d,0x9f} },
   1.184 +	{ (unsigned char *) "123456789012345678901234567890123456789012345678901234567890"
   1.185 +	  "12345678901234567890",
   1.186 +	  {0x57,0xed,0xf4,0xa2,0x2b,0xe3,0xc9,0x55,0xac,0x49,0xda,0x2e,0x21,0x07,0xb6,0x7a} }
   1.187 +    };
   1.188 +    PRUint8 results[MD5_LENGTH];
   1.189 +    int rv = 0, cnt, numTests;
   1.190 +
   1.191 +    numTests = sizeof(md5tests)/sizeof(md5tests[0]);
   1.192 +    for (cnt = 0; cnt < numTests; cnt++) {
   1.193 +	rv += testMessageDigest(initCtx, HASH_AlgMD5, MD5_LENGTH,
   1.194 +        		(const unsigned char *) md5tests[cnt].input,
   1.195 +        		md5tests[cnt].result, &results[0]);
   1.196 +    }
   1.197 +    return rv;
   1.198 +}
   1.199 +
   1.200 +/*
   1.201 + * Tests with test vectors from FIPS 180-2 Appendixes B.1, B.2, B.3, C, and D
   1.202 + *
   1.203 + */
   1.204 +
   1.205 +static int testSHA1(NSSLOWInitContext *initCtx)
   1.206 +{
   1.207 +    static const struct {
   1.208 +	const unsigned char *input;
   1.209 +	const PRUint8 result[SHA1_LENGTH];
   1.210 +    } sha1tests[] = {
   1.211 +	/* one block messsage */
   1.212 +	{ (const unsigned char *)
   1.213 +	  "abc",
   1.214 +	   /* a9993e36 4706816a ba3e2571 7850c26c 9cd0d89d. */
   1.215 +
   1.216 +	  { 0xa9,0x99,0x3e,0x36, 0x47,0x06,0x81,0x6a, /* a9993e36 4706816a */
   1.217 +	    0xba,0x3e,0x25,0x71, /* ba3e2571 */
   1.218 +	    0x78,0x50,0xc2,0x6c, 0x9c,0xd0,0xd8,0x9d} /* 7850c26c 9cd0d89d */
   1.219 +	},
   1.220 +	{ (const unsigned char *)
   1.221 +	  "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
   1.222 +	  /* 84983e44 1c3bd26e baae4aa1 f95129e5 e54670f1. */
   1.223 +	  {0x84,0x98,0x3e,0x44, 0x1c,0x3b,0xd2,0x6e, 0xba,0xae,0x4a,0xa1,
   1.224 +	   0xf9,0x51,0x29,0xe5, 0xe5,0x46,0x70,0xf1}
   1.225 +	}
   1.226 +    };
   1.227 +
   1.228 +    PRUint8 results[SHA1_LENGTH];
   1.229 +    int rv = 0, cnt, numTests;
   1.230 +
   1.231 +    numTests = sizeof(sha1tests)/sizeof(sha1tests[0]);
   1.232 +    for (cnt = 0; cnt < numTests; cnt++) {
   1.233 +	rv += testMessageDigest(initCtx, HASH_AlgSHA1, SHA1_LENGTH,
   1.234 +        		(const unsigned char *) sha1tests[cnt].input,
   1.235 +        		sha1tests[cnt].result, &results[0]);
   1.236 +    }
   1.237 +
   1.238 +    rv += test_long_message_sha1(initCtx);
   1.239 +    return rv;
   1.240 +}
   1.241 +
   1.242 +static int testSHA224(NSSLOWInitContext *initCtx)
   1.243 +{
   1.244 +    static const struct {
   1.245 +	const unsigned char *input;
   1.246 +	const PRUint8 result[SHA224_LENGTH];
   1.247 +    } sha224tests[] = {
   1.248 +	/* one block messsage */
   1.249 +	{ (const unsigned char *) "abc",
   1.250 +	  {0x23,0x09,0x7D,0x22,0x34,0x05,0xD8,0x22,0x86,0x42,0xA4,0x77,0xBD,0xA2,0x55,0xB3,
   1.251 +	   0x2A,0xAD,0xBC,0xE4,0xBD,0xA0,0xB3,0xF7,0xE3,0x6C,0x9D,0xA7}
   1.252 +	},
   1.253 +	 /* two block message */
   1.254 +	{ (const unsigned char *) "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
   1.255 +	  {0x75,0x38,0x8B,0x16,0x51,0x27,0x76,0xCC,0x5D,0xBA,0x5D,0xA1,0xFD,0x89,0x01,0x50,
   1.256 +	  0xB0,0xC6,0x45,0x5C,0xB4,0xF5,0x8B,0x19,0x52,0x52,0x25,0x25}
   1.257 +	}
   1.258 +    };
   1.259 +
   1.260 +    PRUint8 results[SHA224_LENGTH];
   1.261 +    int rv = 0, cnt, numTests;
   1.262 +
   1.263 +    numTests = sizeof(sha224tests)/sizeof(sha224tests[0]);
   1.264 +    for (cnt = 0; cnt < numTests; cnt++) {
   1.265 +	rv += testMessageDigest(initCtx, HASH_AlgSHA224, SHA224_LENGTH,
   1.266 +        		(const unsigned char *) sha224tests[cnt].input,
   1.267 +        		sha224tests[cnt].result, &results[0]);
   1.268 +    }
   1.269 +
   1.270 +    return rv;
   1.271 +}
   1.272 +
   1.273 +static int testSHA256(NSSLOWInitContext *initCtx)
   1.274 +{
   1.275 +    static const struct {
   1.276 +	const unsigned char *input;
   1.277 +	const PRUint8 result[SHA256_LENGTH];
   1.278 +    } sha256tests[] = {
   1.279 +	/* Test vectors from FIPS 180-2: appendix B.1.  */
   1.280 +	{ (unsigned char *) "abc",
   1.281 +	  {0xba,0x78,0x16,0xbf,0x8f,0x01,0xcf,0xea,0x41,0x41,0x40,0xde,0x5d,0xae,0x22,0x23,
   1.282 +	   0xb0,0x03,0x61,0xa3,0x96,0x17,0x7a,0x9c,0xb4,0x10,0xff,0x61,0xf2,0x00,0x15,0xad} 
   1.283 +	},
   1.284 +	/* Test vectors from FIPS 180-2: appendix B.2.  */
   1.285 +	{ (unsigned char *) "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
   1.286 +	  {0x24,0x8d,0x6a,0x61,0xd2,0x06,0x38,0xb8,0xe5,0xc0,0x26,0x93,0x0c,0x3e,0x60,0x39,
   1.287 +	   0xa3,0x3c,0xe4,0x59,0x64,0xff,0x21,0x67,0xf6,0xec,0xed,0xd4,0x19,0xdb,0x06,0xc1} 
   1.288 +	}
   1.289 +    };
   1.290 +
   1.291 +    PRUint8 results[SHA256_LENGTH];
   1.292 +    int rv = 0, cnt, numTests;
   1.293 +
   1.294 +    numTests = sizeof(sha256tests)/sizeof(sha256tests[0]);
   1.295 +    for (cnt = 0; cnt < numTests; cnt++) {
   1.296 +	rv += testMessageDigest(initCtx, HASH_AlgSHA256, SHA256_LENGTH,
   1.297 +        		(const unsigned char *) sha256tests[cnt].input,
   1.298 +        		sha256tests[cnt].result, &results[0]);
   1.299 +    }
   1.300 +
   1.301 +    rv += test_long_message_sha256(initCtx);
   1.302 +    return rv;
   1.303 +}
   1.304 +
   1.305 +static int testSHA384(NSSLOWInitContext *initCtx)
   1.306 +{
   1.307 +    static const struct {
   1.308 +	const unsigned char *input;
   1.309 +	const PRUint8 result[SHA384_LENGTH];
   1.310 +    } sha384tests[] = {
   1.311 +	/* Test vector from FIPS 180-2: appendix D, single-block message.  */
   1.312 +	{ (unsigned char *) "abc",
   1.313 +	  {0xcb,0x00,0x75,0x3f,0x45,0xa3,0x5e,0x8b,
   1.314 +	   0xb5,0xa0,0x3d,0x69,0x9a,0xc6,0x50,0x07,
   1.315 +	   0x27,0x2c,0x32,0xab,0x0e,0xde,0xd1,0x63,
   1.316 +	   0x1a,0x8b,0x60,0x5a,0x43,0xff,0x5b,0xed,
   1.317 +	   0x80,0x86,0x07,0x2b,0xa1,0xe7,0xcc,0x23,
   1.318 +	   0x58,0xba,0xec,0xa1,0x34,0xc8,0x25,0xa7} },
   1.319 +
   1.320 +	/* Test vectors from FIPS 180-2: appendix D, multi-block message.  */
   1.321 +	{ (unsigned char *)
   1.322 +	  "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
   1.323 +	  "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
   1.324 +        /* 
   1.325 +	09330c33f71147e8 
   1.326 +        3d192fc782cd1b47 
   1.327 +        53111b173b3b05d2 
   1.328 +        2fa08086e3b0f712
   1.329 +	fcc7c71a557e2db9 
   1.330 +        66c3e9fa91746039.
   1.331 +        */
   1.332 +	{0x09,0x33,0x0c,0x33,0xf7,0x11,0x47,0xe8,
   1.333 +         0x3d,0x19,0x2f,0xc7,0x82,0xcd,0x1b,0x47,
   1.334 +         0x53,0x11,0x1b,0x17,0x3b,0x3b,0x05,0xd2,
   1.335 +	 0x2f,0xa0,0x80,0x86,0xe3,0xb0,0xf7,0x12,
   1.336 +	 0xfc,0xc7,0xc7,0x1a,0x55,0x7e,0x2d,0xb9,
   1.337 +         0x66,0xc3,0xe9,0xfa,0x91,0x74,0x60,0x39} }
   1.338 +   };
   1.339 +
   1.340 +    PRUint8 results[SHA384_LENGTH];
   1.341 +    int rv = 0, cnt, numTests;
   1.342 +
   1.343 +    numTests = sizeof(sha384tests)/sizeof(sha384tests[0]);
   1.344 +    for (cnt = 0; cnt < numTests; cnt++) {
   1.345 +        rv += testMessageDigest(initCtx, HASH_AlgSHA384, SHA384_LENGTH,
   1.346 +        			(const unsigned char *) sha384tests[cnt].input,
   1.347 +        			sha384tests[cnt].result, &results[0]);
   1.348 +    }
   1.349 +    rv += test_long_message_sha384(initCtx);
   1.350 +
   1.351 +    return rv;
   1.352 +}
   1.353 +
   1.354 +
   1.355 +int testSHA512(NSSLOWInitContext *initCtx)
   1.356 +{
   1.357 +    static const struct {
   1.358 +	const unsigned char *input;
   1.359 +	const PRUint8 result[SHA512_LENGTH];
   1.360 +    } sha512tests[] = {
   1.361 +	/* Test vectors from FIPS 180-2: appendix C.1.  */
   1.362 +	{ (unsigned char *) "abc",
   1.363 +	  { 0xdd,0xaf,0x35,0xa1,0x93,0x61,0x7a,0xba,0xcc,0x41,0x73,0x49,0xae,0x20,0x41,0x31,
   1.364 +	    0x12,0xe6,0xfa,0x4e,0x89,0xa9,0x7e,0xa2,0x0a,0x9e,0xee,0xe6,0x4b,0x55,0xd3,0x9a,
   1.365 +	    0x21,0x92,0x99,0x2a,0x27,0x4f,0xc1,0xa8,0x36,0xba,0x3c,0x23,0xa3,0xfe,0xeb,0xbd,
   1.366 +	    0x45,0x4d,0x44,0x23,0x64,0x3c,0xe8,0x0e,0x2a,0x9a,0xc9,0x4f,0xa5,0x4c,0xa4,0x9f} 
   1.367 +	},
   1.368 +	/* Test vectors from FIPS 180-2: appendix C.2.  */
   1.369 +	{ (unsigned char *) "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
   1.370 +	  "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
   1.371 +	  {0x8e,0x95,0x9b,0x75,0xda,0xe3,0x13,0xda,0x8c,0xf4,0xf7,0x28,0x14,0xfc,0x14,0x3f,
   1.372 +	   0x8f,0x77,0x79,0xc6,0xeb,0x9f,0x7f,0xa1,0x72,0x99,0xae,0xad,0xb6,0x88,0x90,0x18,
   1.373 +	   0x50,0x1d,0x28,0x9e,0x49,0x00,0xf7,0xe4,0x33,0x1b,0x99,0xde,0xc4,0xb5,0x43,0x3a,
   1.374 +	   0xc7,0xd3,0x29,0xee,0xb6,0xdd,0x26,0x54,0x5e,0x96,0xe5,0x5b,0x87,0x4b,0xe9,0x09} 
   1.375 +	}
   1.376 +    };
   1.377 +
   1.378 +    PRUint8 results[SHA512_LENGTH];
   1.379 +    int rv = 0, cnt, numTests;
   1.380 +
   1.381 +    numTests = sizeof(sha512tests)/sizeof(sha512tests[0]);
   1.382 +    for (cnt = 0; cnt < numTests; cnt++) {
   1.383 +	rv = testMessageDigest(initCtx, HASH_AlgSHA512, SHA512_LENGTH,
   1.384 +        			(const unsigned char *) sha512tests[cnt].input,
   1.385 +        			sha512tests[cnt].result, &results[0]);
   1.386 +    }
   1.387 +    rv += test_long_message_sha512(initCtx);
   1.388 +    return rv;
   1.389 +}
   1.390 +
   1.391 +static void
   1.392 +Usage(char *progName)
   1.393 +{
   1.394 +    fprintf(stderr, "Usage: %s [algorithm]\n",
   1.395 +	    progName);
   1.396 +    fprintf(stderr, "algorithm must be one of %s\n",
   1.397 +	    "{ MD5 | SHA1 | SHA224 | SHA256 | SHA384 | SHA512 }");
   1.398 +    fprintf(stderr, "default is to test all\n");
   1.399 +    exit(-1);
   1.400 +}
   1.401 +
   1.402 +int main(int argc, char **argv)
   1.403 +{
   1.404 +    NSSLOWInitContext *initCtx;
   1.405 +    int rv = 0; /* counts the number of failures */
   1.406 +
   1.407 +    progName = strrchr(argv[0], '/');
   1.408 +    progName = progName ? progName+1 : argv[0];
   1.409 +
   1.410 +    initCtx = NSSLOW_Init();
   1.411 +    if (initCtx == NULL) {
   1.412 +	SECU_PrintError(progName, "Couldn't initialize for hashing\n");
   1.413 +	return 1;
   1.414 +    }
   1.415 +
   1.416 +    if (argc || !argv[1] || strlen(argv[1]) == 0) {
   1.417 +	rv += testMD5(initCtx);
   1.418 +	rv += testSHA1(initCtx);
   1.419 +	rv += testSHA224(initCtx);
   1.420 +	rv += testSHA256(initCtx);
   1.421 +	rv += testSHA384(initCtx);
   1.422 +	rv += testSHA512(initCtx);
   1.423 +    } else if (strcmp(argv[1], "MD5") == 0) {
   1.424 +	rv += testMD5(initCtx);
   1.425 +    } else if (strcmp(argv[1], "SHA1") == 0) {
   1.426 +	rv += testSHA1(initCtx);
   1.427 +    } else if (strcmp(argv[1], "SHA224") == 0) {
   1.428 +	rv += testSHA224(initCtx);
   1.429 +    } else if (strcmp(argv[1], "SHA226") == 0) {
   1.430 +	rv += testSHA256(initCtx);
   1.431 +    } else if (strcmp(argv[1], "SHA384") == 0) {
   1.432 +	rv += testSHA384(initCtx);
   1.433 +    } else if (strcmp(argv[1], "SHA512") == 0) {
   1.434 +	rv += testSHA512(initCtx);
   1.435 +    } else {
   1.436 +    	SECU_PrintError(progName, "Unsupported hash type %s\n", argv[0]);
   1.437 +    	Usage(progName);
   1.438 +    }
   1.439 +
   1.440 +    NSSLOW_Shutdown(initCtx);
   1.441 +
   1.442 +    return (rv == 0) ? 0 : 1;
   1.443 +}
   1.444 +

mercurial