| lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | #include <string.h> | 
|  | 2 | #include "sha256.h" | 
|  | 3 |  | 
|  | 4 | static const struct | 
|  | 5 | { | 
|  | 6 | const char *input; | 
|  | 7 | const char result[32]; | 
|  | 8 | } tests[] = | 
|  | 9 | { | 
|  | 10 | /* Test vectors from FIPS 180-2: appendix B.1.  */ | 
|  | 11 | { "abc", | 
|  | 12 | "\xba\x78\x16\xbf\x8f\x01\xcf\xea\x41\x41\x40\xde\x5d\xae\x22\x23" | 
|  | 13 | "\xb0\x03\x61\xa3\x96\x17\x7a\x9c\xb4\x10\xff\x61\xf2\x00\x15\xad" }, | 
|  | 14 | /* Test vectors from FIPS 180-2: appendix B.2.  */ | 
|  | 15 | { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", | 
|  | 16 | "\x24\x8d\x6a\x61\xd2\x06\x38\xb8\xe5\xc0\x26\x93\x0c\x3e\x60\x39" | 
|  | 17 | "\xa3\x3c\xe4\x59\x64\xff\x21\x67\xf6\xec\xed\xd4\x19\xdb\x06\xc1" }, | 
|  | 18 | /* Test vectors from the NESSIE project.  */ | 
|  | 19 | { "", | 
|  | 20 | "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9\x24" | 
|  | 21 | "\x27\xae\x41\xe4\x64\x9b\x93\x4c\xa4\x95\x99\x1b\x78\x52\xb8\x55" }, | 
|  | 22 | { "a", | 
|  | 23 | "\xca\x97\x81\x12\xca\x1b\xbd\xca\xfa\xc2\x31\xb3\x9a\x23\xdc\x4d" | 
|  | 24 | "\xa7\x86\xef\xf8\x14\x7c\x4e\x72\xb9\x80\x77\x85\xaf\xee\x48\xbb" }, | 
|  | 25 | { "message digest", | 
|  | 26 | "\xf7\x84\x6f\x55\xcf\x23\xe1\x4e\xeb\xea\xb5\xb4\xe1\x55\x0c\xad" | 
|  | 27 | "\x5b\x50\x9e\x33\x48\xfb\xc4\xef\xa3\xa1\x41\x3d\x39\x3c\xb6\x50" }, | 
|  | 28 | { "abcdefghijklmnopqrstuvwxyz", | 
|  | 29 | "\x71\xc4\x80\xdf\x93\xd6\xae\x2f\x1e\xfa\xd1\x44\x7c\x66\xc9\x52" | 
|  | 30 | "\x5e\x31\x62\x18\xcf\x51\xfc\x8d\x9e\xd8\x32\xf2\xda\xf1\x8b\x73" }, | 
|  | 31 | { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", | 
|  | 32 | "\x24\x8d\x6a\x61\xd2\x06\x38\xb8\xe5\xc0\x26\x93\x0c\x3e\x60\x39" | 
|  | 33 | "\xa3\x3c\xe4\x59\x64\xff\x21\x67\xf6\xec\xed\xd4\x19\xdb\x06\xc1" }, | 
|  | 34 | { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", | 
|  | 35 | "\xdb\x4b\xfc\xbd\x4d\xa0\xcd\x85\xa6\x0c\x3c\x37\xd3\xfb\xd8\x80" | 
|  | 36 | "\x5c\x77\xf1\x5f\xc6\xb1\xfd\xfe\x61\x4e\xe0\xa7\xc8\xfd\xb4\xc0" }, | 
|  | 37 | { "123456789012345678901234567890123456789012345678901234567890" | 
|  | 38 | "12345678901234567890", | 
|  | 39 | "\xf3\x71\xbc\x4a\x31\x1f\x2b\x00\x9e\xef\x95\x2d\xd8\x3c\xa8\x0e" | 
|  | 40 | "\x2b\x60\x02\x6c\x8e\x93\x55\x92\xd0\xf9\xc3\x08\x45\x3c\x81\x3e" } | 
|  | 41 | }; | 
|  | 42 |  | 
|  | 43 |  | 
|  | 44 | int | 
|  | 45 | main (void) | 
|  | 46 | { | 
|  | 47 | struct sha256_ctx ctx; | 
|  | 48 | char sum[32]; | 
|  | 49 | int result = 0; | 
|  | 50 | int cnt; | 
|  | 51 |  | 
|  | 52 | for (cnt = 0; cnt < (int) (sizeof (tests) / sizeof (tests[0])); ++cnt) | 
|  | 53 | { | 
|  | 54 | __sha256_init_ctx (&ctx); | 
|  | 55 | __sha256_process_bytes (tests[cnt].input, strlen (tests[cnt].input), | 
|  | 56 | &ctx); | 
|  | 57 | __sha256_finish_ctx (&ctx, sum); | 
|  | 58 | if (memcmp (tests[cnt].result, sum, 32) != 0) | 
|  | 59 | { | 
|  | 60 | printf ("test %d run %d failed\n", cnt, 1); | 
|  | 61 | result = 1; | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 | __sha256_init_ctx (&ctx); | 
|  | 65 | for (int i = 0; tests[cnt].input[i] != '\0'; ++i) | 
|  | 66 | __sha256_process_bytes (&tests[cnt].input[i], 1, &ctx); | 
|  | 67 | __sha256_finish_ctx (&ctx, sum); | 
|  | 68 | if (memcmp (tests[cnt].result, sum, 32) != 0) | 
|  | 69 | { | 
|  | 70 | printf ("test %d run %d failed\n", cnt, 2); | 
|  | 71 | result = 1; | 
|  | 72 | } | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | /* Test vector from FIPS 180-2: appendix B.3.  */ | 
|  | 76 | char buf[1000]; | 
|  | 77 | memset (buf, 'a', sizeof (buf)); | 
|  | 78 | __sha256_init_ctx (&ctx); | 
|  | 79 | for (int i = 0; i < 1000; ++i) | 
|  | 80 | __sha256_process_bytes (buf, sizeof (buf), &ctx); | 
|  | 81 | __sha256_finish_ctx (&ctx, sum); | 
|  | 82 | static const char expected[32] = | 
|  | 83 | "\xcd\xc7\x6e\x5c\x99\x14\xfb\x92\x81\xa1\xc7\xe2\x84\xd7\x3e\x67" | 
|  | 84 | "\xf1\x80\x9a\x48\xa4\x97\x20\x0e\x04\x6d\x39\xcc\xc7\x11\x2c\xd0"; | 
|  | 85 | if (memcmp (expected, sum, 32) != 0) | 
|  | 86 | { | 
|  | 87 | printf ("test %d failed\n", cnt++); | 
|  | 88 | result = 1; | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | __sha256_init_ctx (&ctx); | 
|  | 92 | for (int i = 0; i < 100000; ++i) | 
|  | 93 | __sha256_process_bytes (buf, 10, &ctx); | 
|  | 94 | __sha256_finish_ctx (&ctx, sum); | 
|  | 95 | if (memcmp (expected, sum, 32) != 0) | 
|  | 96 | { | 
|  | 97 | printf ("test %d failed\n", cnt++); | 
|  | 98 | result = 1; | 
|  | 99 | } | 
|  | 100 |  | 
|  | 101 | return result; | 
|  | 102 | } |