1 #include "crypt-port.h"
2 #include "alg-sha256.h"
3
4 #include <stdio.h>
5
6 #if INCLUDE_sha256crypt || INCLUDE_scrypt || INCLUDE_yescrypt || \
7 INCLUDE_gost_yescrypt
8
9 static const struct
10 {
11 const char *input;
12 const char result[32];
13 } tests[] =
14 {
15 /* Test vectors from FIPS 180-2: appendix B.1. */
16 {
17 "abc",
18 "\xba\x78\x16\xbf\x8f\x01\xcf\xea\x41\x41\x40\xde\x5d\xae\x22\x23"
19 "\xb0\x03\x61\xa3\x96\x17\x7a\x9c\xb4\x10\xff\x61\xf2\x00\x15\xad"
20 },
21 /* Test vectors from FIPS 180-2: appendix B.2. */
22 {
23 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
24 "\x24\x8d\x6a\x61\xd2\x06\x38\xb8\xe5\xc0\x26\x93\x0c\x3e\x60\x39"
25 "\xa3\x3c\xe4\x59\x64\xff\x21\x67\xf6\xec\xed\xd4\x19\xdb\x06\xc1"
26 },
27 /* Test vectors from the NESSIE project. */
28 {
29 "",
30 "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9\x24"
31 "\x27\xae\x41\xe4\x64\x9b\x93\x4c\xa4\x95\x99\x1b\x78\x52\xb8\x55"
32 },
33 {
34 "a",
35 "\xca\x97\x81\x12\xca\x1b\xbd\xca\xfa\xc2\x31\xb3\x9a\x23\xdc\x4d"
36 "\xa7\x86\xef\xf8\x14\x7c\x4e\x72\xb9\x80\x77\x85\xaf\xee\x48\xbb"
37 },
38 {
39 "message digest",
40 "\xf7\x84\x6f\x55\xcf\x23\xe1\x4e\xeb\xea\xb5\xb4\xe1\x55\x0c\xad"
41 "\x5b\x50\x9e\x33\x48\xfb\xc4\xef\xa3\xa1\x41\x3d\x39\x3c\xb6\x50"
42 },
43 {
44 "abcdefghijklmnopqrstuvwxyz",
45 "\x71\xc4\x80\xdf\x93\xd6\xae\x2f\x1e\xfa\xd1\x44\x7c\x66\xc9\x52"
46 "\x5e\x31\x62\x18\xcf\x51\xfc\x8d\x9e\xd8\x32\xf2\xda\xf1\x8b\x73"
47 },
48 {
49 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
50 "\xdb\x4b\xfc\xbd\x4d\xa0\xcd\x85\xa6\x0c\x3c\x37\xd3\xfb\xd8\x80"
51 "\x5c\x77\xf1\x5f\xc6\xb1\xfd\xfe\x61\x4e\xe0\xa7\xc8\xfd\xb4\xc0"
52 },
53 {
54 "123456789012345678901234567890123456789012345678901234567890"
55 "12345678901234567890",
56 "\xf3\x71\xbc\x4a\x31\x1f\x2b\x00\x9e\xef\x95\x2d\xd8\x3c\xa8\x0e"
57 "\x2b\x60\x02\x6c\x8e\x93\x55\x92\xd0\xf9\xc3\x08\x45\x3c\x81\x3e"
58 }
59 };
60
61
62 static void
63 report_failure(int n, const char *tag,
64 const char expected[32], uint8_t actual[32])
65 {
66 int i;
67 printf ("FAIL: test %d (%s):\n exp:", n, tag);
68 for (i = 0; i < 32; i++)
69 {
70 if (i % 4 == 0)
71 putchar (' ');
72 printf ("%02x", (unsigned int)(unsigned char)expected[i]);
73 }
74 printf ("\n got:");
75 for (i = 0; i < 32; i++)
76 {
77 if (i % 4 == 0)
78 putchar (' ');
79 printf ("%02x", (unsigned int)(unsigned char)actual[i]);
80 }
81 putchar ('\n');
82 putchar ('\n');
83 }
84
85 int
86 main (void)
87 {
88 SHA256_CTX ctx;
89 uint8_t sum[32];
90 int result = 0;
91 int cnt;
92 int i;
93
94 for (cnt = 0; cnt < (int) ARRAY_SIZE (tests); ++cnt)
95 {
96 SHA256_Buf (tests[cnt].input, strlen (tests[cnt].input), sum);
97 if (memcmp (tests[cnt].result, sum, 32) != 0)
98 {
99 report_failure (cnt, "all at once", tests[cnt].result, sum);
100 result = 1;
101 }
102
103 SHA256_Init (&ctx);
104 for (i = 0; tests[cnt].input[i] != '\0'; ++i)
105 SHA256_Update (&ctx, &tests[cnt].input[i], 1);
106 SHA256_Final (sum, &ctx);
107 if (memcmp (tests[cnt].result, sum, 32) != 0)
108 {
109 report_failure (cnt, "byte by byte", tests[cnt].result, sum);
110 result = 1;
111 }
112 }
113
114 /* Test vector from FIPS 180-2: appendix B.3. */
115 char buf[1000];
116 memset (buf, 'a', sizeof (buf));
117 SHA256_Init (&ctx);
118 for (i = 0; i < 1000; ++i)
119 SHA256_Update (&ctx, buf, sizeof (buf));
120 SHA256_Final (sum, &ctx);
121 static const char expected[32] =
122 "\xcd\xc7\x6e\x5c\x99\x14\xfb\x92\x81\xa1\xc7\xe2\x84\xd7\x3e\x67"
123 "\xf1\x80\x9a\x48\xa4\x97\x20\x0e\x04\x6d\x39\xcc\xc7\x11\x2c\xd0";
124 if (memcmp (expected, sum, 32) != 0)
125 {
126 report_failure (cnt, "block by block", expected, sum);
127 result = 1;
128 }
129
130 return result;
131 }
132
133 #else
134
135 int
136 main (void)
137 {
138 return 77; /* UNSUPPORTED */
139 }
140
141 #endif