blob: 5da973ff4e15951cd45bb8077cfd51e9057e9c36 [file] [log] [blame]
David Brownfecda2d2017-09-07 10:20:34 -06001/* test_sha256.c - TinyCrypt implementation of some SHA-256 tests */
2
3/*
4 * Copyright (C) 2015 by Intel Corporation, All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * - Neither the name of Intel Corporation nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 DESCRIPTION
35 This module tests the following SHA256 routines:
36
37 Scenarios tested include:
38 - NIST SHA256 test vectors
39*/
40
41#include <tinycrypt/sha256.h>
42#include <tinycrypt/constants.h>
43#include <test_utils.h>
44
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48#include <stdint.h>
49
50/*
51 * NIST SHA256 test vector 1.
52 */
53uint32_t test_1(void)
54{
55 uint32_t result = TC_PASS;
56
57 TC_PRINT("SHA256 test #1:\n");
58 const uint8_t expected[32] = {
59 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde,
60 0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
61 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
62 };
63 const char *m = "abc";
64 uint8_t digest[32];
65 struct tc_sha256_state_struct s;
66
67 (void)tc_sha256_init(&s);
68 tc_sha256_update(&s, (const uint8_t *) m, strlen(m));
69 (void)tc_sha256_final(digest, &s);
70 result = check_result(1, expected, sizeof(expected),
71 digest, sizeof(digest));
72 TC_END_RESULT(result);
73 return result;
74}
75
76/*
77 * NIST SHA256 test vector 2.
78 */
79uint32_t test_2(void)
80{
81 uint32_t result = TC_PASS;
82
83 TC_PRINT("SHA256 test #2:\n");
84 const uint8_t expected[32] = {
85 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, 0xe5, 0xc0, 0x26, 0x93,
86 0x0c, 0x3e, 0x60, 0x39, 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
87 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1
88 };
89 const char *m = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
90 uint8_t digest[32];
91 struct tc_sha256_state_struct s;
92
93 (void)tc_sha256_init(&s);
94 tc_sha256_update(&s, (const uint8_t *) m, strlen(m));
95 (void) tc_sha256_final(digest, &s);
96
97 result = check_result(2, expected, sizeof(expected),
98 digest, sizeof(digest));
99 TC_END_RESULT(result);
100 return result;
101}
102
103uint32_t test_3(void)
104{
105 uint32_t result = TC_PASS;
106
107 TC_PRINT("SHA256 test #3:\n");
108 const uint8_t expected[32] = {
109 0x68, 0x32, 0x57, 0x20, 0xaa, 0xbd, 0x7c, 0x82, 0xf3, 0x0f, 0x55, 0x4b,
110 0x31, 0x3d, 0x05, 0x70, 0xc9, 0x5a, 0xcc, 0xbb, 0x7d, 0xc4, 0xb5, 0xaa,
111 0xe1, 0x12, 0x04, 0xc0, 0x8f, 0xfe, 0x73, 0x2b
112 };
113 const uint8_t m[1] = { 0xbd };
114 uint8_t digest[32];
115 struct tc_sha256_state_struct s;
116
117 (void)tc_sha256_init(&s);
118 tc_sha256_update(&s, m, sizeof(m));
119 (void)tc_sha256_final(digest, &s);
120
121 result = check_result(3, expected, sizeof(expected),
122 digest, sizeof(digest));
123 TC_END_RESULT(result);
124 return result;
125}
126
127uint32_t test_4(void)
128{
129 uint32_t result = TC_PASS;
130
131 TC_PRINT("SHA256 test #4:\n");
132 const uint8_t expected[32] = {
133 0x7a, 0xbc, 0x22, 0xc0, 0xae, 0x5a, 0xf2, 0x6c, 0xe9, 0x3d, 0xbb, 0x94,
134 0x43, 0x3a, 0x0e, 0x0b, 0x2e, 0x11, 0x9d, 0x01, 0x4f, 0x8e, 0x7f, 0x65,
135 0xbd, 0x56, 0xc6, 0x1c, 0xcc, 0xcd, 0x95, 0x04
136 };
137 const uint8_t m[4] = { 0xc9, 0x8c, 0x8e, 0x55 };
138 uint8_t digest[32];
139 struct tc_sha256_state_struct s;
140
141 (void)tc_sha256_init(&s);
142 tc_sha256_update(&s, m, sizeof(m));
143 (void)tc_sha256_final(digest, &s);
144
145 result = check_result(4, expected, sizeof(expected),
146 digest, sizeof(digest));
147 TC_END_RESULT(result);
148 return result;
149}
150
151uint32_t test_5(void)
152{
153 uint32_t result = TC_PASS;
154
155 TC_PRINT("SHA256 test #5:\n");
156 const uint8_t expected[32] = {
157 0x02, 0x77, 0x94, 0x66, 0xcd, 0xec, 0x16, 0x38, 0x11, 0xd0, 0x78, 0x81,
158 0x5c, 0x63, 0x3f, 0x21, 0x90, 0x14, 0x13, 0x08, 0x14, 0x49, 0x00, 0x2f,
159 0x24, 0xaa, 0x3e, 0x80, 0xf0, 0xb8, 0x8e, 0xf7
160 };
161 uint8_t m[55];
162 uint8_t digest[32];
163 struct tc_sha256_state_struct s;
164
165 (void)memset(m, 0x00, sizeof(m));
166
167 (void)tc_sha256_init(&s);
168 tc_sha256_update(&s, m, sizeof(m));
169 (void)tc_sha256_final(digest, &s);
170
171 result = check_result(5, expected, sizeof(expected),
172 digest, sizeof(digest));
173 TC_END_RESULT(result);
174 return result;
175}
176
177uint32_t test_6(void)
178{
179 uint32_t result = TC_PASS;
180
181 TC_PRINT("SHA256 test #6:\n");
182 const uint8_t expected[32] = {
183 0xd4, 0x81, 0x7a, 0xa5, 0x49, 0x76, 0x28, 0xe7, 0xc7, 0x7e, 0x6b, 0x60,
184 0x61, 0x07, 0x04, 0x2b, 0xbb, 0xa3, 0x13, 0x08, 0x88, 0xc5, 0xf4, 0x7a,
185 0x37, 0x5e, 0x61, 0x79, 0xbe, 0x78, 0x9f, 0xbb
186 };
187 uint8_t m[56];
188 uint8_t digest[32];
189 struct tc_sha256_state_struct s;
190
191 (void)memset(m, 0x00, sizeof(m));
192
193 (void)tc_sha256_init(&s);
194 tc_sha256_update(&s, m, sizeof(m));
195 (void)tc_sha256_final(digest, &s);
196
197 result = check_result(6, expected, sizeof(expected),
198 digest, sizeof(digest));
199 TC_END_RESULT(result);
200 return result;
201}
202
203uint32_t test_7(void)
204{
205 uint32_t result = TC_PASS;
206
207 TC_PRINT("SHA256 test #7:\n");
208 const uint8_t expected[32] = {
209 0x65, 0xa1, 0x6c, 0xb7, 0x86, 0x13, 0x35, 0xd5, 0xac, 0xe3, 0xc6, 0x07,
210 0x18, 0xb5, 0x05, 0x2e, 0x44, 0x66, 0x07, 0x26, 0xda, 0x4c, 0xd1, 0x3b,
211 0xb7, 0x45, 0x38, 0x1b, 0x23, 0x5a, 0x17, 0x85
212 };
213 uint8_t m[57];
214 uint8_t digest[32];
215 struct tc_sha256_state_struct s;
216
217 (void)memset(m, 0x00, sizeof(m));
218
219 (void)tc_sha256_init(&s);
220 tc_sha256_update(&s, m, sizeof(m));
221 (void)tc_sha256_final(digest, &s);
222
223 result = check_result(7, expected, sizeof(expected),
224 digest, sizeof(digest));
225 TC_END_RESULT(result);
226 return result;
227}
228
229uint32_t test_8(void)
230{
231 uint32_t result = TC_PASS;
232
233 TC_PRINT("SHA256 test #8:\n");
234 const uint8_t expected[32] = {
235 0xf5, 0xa5, 0xfd, 0x42, 0xd1, 0x6a, 0x20, 0x30, 0x27, 0x98, 0xef, 0x6e,
236 0xd3, 0x09, 0x97, 0x9b, 0x43, 0x00, 0x3d, 0x23, 0x20, 0xd9, 0xf0, 0xe8,
237 0xea, 0x98, 0x31, 0xa9, 0x27, 0x59, 0xfb, 0x4b
238 };
239 uint8_t m[64];
240 uint8_t digest[32];
241 struct tc_sha256_state_struct s;
242
243 (void)memset(m, 0x00, sizeof(m));
244
245 (void)tc_sha256_init(&s);
246 tc_sha256_update(&s, m, sizeof(m));
247 (void)tc_sha256_final(digest, &s);
248
249 result = check_result(8, expected, sizeof(expected),
250 digest, sizeof(digest));
251 TC_END_RESULT(result);
252 return result;
253}
254
255uint32_t test_9(void)
256{
257 uint32_t result = TC_PASS;
258
259 TC_PRINT("SHA256 test #9:\n");
260 const uint8_t expected[32] = {
261 0x54, 0x1b, 0x3e, 0x9d, 0xaa, 0x09, 0xb2, 0x0b, 0xf8, 0x5f, 0xa2, 0x73,
262 0xe5, 0xcb, 0xd3, 0xe8, 0x01, 0x85, 0xaa, 0x4e, 0xc2, 0x98, 0xe7, 0x65,
263 0xdb, 0x87, 0x74, 0x2b, 0x70, 0x13, 0x8a, 0x53
264 };
265 uint8_t m[1000];
266 uint8_t digest[32];
267 struct tc_sha256_state_struct s;
268
269 (void)memset(m, 0x00, sizeof(m));
270
271 (void)tc_sha256_init(&s);
272 tc_sha256_update(&s, m, sizeof(m));
273 (void)tc_sha256_final(digest, &s);
274
275 result = check_result(9, expected, sizeof(expected),
276 digest, sizeof(digest));
277 TC_END_RESULT(result);
278 return result;
279}
280
281uint32_t test_10(void)
282{
283 uint32_t result = TC_PASS;
284
285 TC_PRINT("SHA256 test #10:\n");
286 const uint8_t expected[32] = {
287 0xc2, 0xe6, 0x86, 0x82, 0x34, 0x89, 0xce, 0xd2, 0x01, 0x7f, 0x60, 0x59,
288 0xb8, 0xb2, 0x39, 0x31, 0x8b, 0x63, 0x64, 0xf6, 0xdc, 0xd8, 0x35, 0xd0,
289 0xa5, 0x19, 0x10, 0x5a, 0x1e, 0xad, 0xd6, 0xe4
290 };
291 uint8_t m[1000];
292 uint8_t digest[32];
293 struct tc_sha256_state_struct s;
294
295 (void)memset(m, 0x41, sizeof(m));
296
297 (void)tc_sha256_init(&s);
298 tc_sha256_update(&s, m, sizeof(m));
299 (void)tc_sha256_final(digest, &s);
300
301 result = check_result(10, expected, sizeof(expected),
302 digest, sizeof(digest));
303 TC_END_RESULT(result);
304 return result;
305}
306
307uint32_t test_11(void)
308{
309 uint32_t result = TC_PASS;
310
311 TC_PRINT("SHA256 test #11:\n");
312 const uint8_t expected[32] = {
313 0xf4, 0xd6, 0x2d, 0xde, 0xc0, 0xf3, 0xdd, 0x90, 0xea, 0x13, 0x80, 0xfa,
314 0x16, 0xa5, 0xff, 0x8d, 0xc4, 0xc5, 0x4b, 0x21, 0x74, 0x06, 0x50, 0xf2,
315 0x4a, 0xfc, 0x41, 0x20, 0x90, 0x35, 0x52, 0xb0
316 };
317 uint8_t m[1005];
318 uint8_t digest[32];
319 struct tc_sha256_state_struct s;
320
321 (void)memset(m, 0x55, sizeof(m));
322
323 (void)tc_sha256_init(&s);
324 tc_sha256_update(&s, m, sizeof(m));
325 (void)tc_sha256_final(digest, &s);
326
327 result = check_result(11, expected, sizeof(expected),
328 digest, sizeof(digest));
329 TC_END_RESULT(result);
330 return result;
331}
332
333uint32_t test_12(void)
334{
335 uint32_t result = TC_PASS;
336
337 TC_PRINT("SHA256 test #12:\n");
338 const uint8_t expected[32] = {
339 0xd2, 0x97, 0x51, 0xf2, 0x64, 0x9b, 0x32, 0xff, 0x57, 0x2b, 0x5e, 0x0a,
340 0x9f, 0x54, 0x1e, 0xa6, 0x60, 0xa5, 0x0f, 0x94, 0xff, 0x0b, 0xee, 0xdf,
341 0xb0, 0xb6, 0x92, 0xb9, 0x24, 0xcc, 0x80, 0x25
342 };
343 uint8_t m[1000];
344 uint8_t digest[32];
345 struct tc_sha256_state_struct s;
346 uint32_t i;
347
348 (void)memset(m, 0x00, sizeof(m));
349
350 (void)tc_sha256_init(&s);
351 for (i = 0; i < 1000; ++i) {
352 tc_sha256_update(&s, m, sizeof(m));
353 }
354 (void)tc_sha256_final(digest, &s);
355
356 result = check_result(12, expected, sizeof(expected),
357 digest, sizeof(digest));
358 TC_END_RESULT(result);
359 return result;
360}
361
362uint32_t test_13(void)
363{
364 uint32_t result = TC_PASS;
365
366 TC_PRINT("SHA256 test #13:\n");
367 const uint8_t expected[32] = {
368 0x15, 0xa1, 0x86, 0x8c, 0x12, 0xcc, 0x53, 0x95, 0x1e, 0x18, 0x23, 0x44,
369 0x27, 0x74, 0x47, 0xcd, 0x09, 0x79, 0x53, 0x6b, 0xad, 0xcc, 0x51, 0x2a,
370 0xd2, 0x4c, 0x67, 0xe9, 0xb2, 0xd4, 0xf3, 0xdd
371 };
372 uint8_t m[32768];
373 uint8_t digest[32];
374 struct tc_sha256_state_struct s;
375 uint32_t i;
376
377 (void)memset(m, 0x5a, sizeof(m));
378
379 (void)tc_sha256_init(&s);
380 for (i = 0; i < 16384; ++i) {
381 tc_sha256_update(&s, m, sizeof(m));
382 }
383 (void)tc_sha256_final(digest, &s);
384
385 result = check_result(13, expected, sizeof(expected),
386 digest, sizeof(digest));
387 TC_END_RESULT(result);
388 return result;
389}
390
391uint32_t test_14(void)
392{
393 uint32_t result = TC_PASS;
394
395 TC_PRINT("SHA256 test #14:\n");
396 const uint8_t expected[32] = {
397 0x46, 0x1c, 0x19, 0xa9, 0x3b, 0xd4, 0x34, 0x4f, 0x92, 0x15, 0xf5, 0xec,
398 0x64, 0x35, 0x70, 0x90, 0x34, 0x2b, 0xc6, 0x6b, 0x15, 0xa1, 0x48, 0x31,
399 0x7d, 0x27, 0x6e, 0x31, 0xcb, 0xc2, 0x0b, 0x53
400 };
401 uint8_t m[32768];
402 uint8_t digest[32];
403 struct tc_sha256_state_struct s;
404 uint32_t i;
405
406 (void)memset(m, 0x00, sizeof(m));
407
408 (void) tc_sha256_init(&s);
409 for (i = 0; i < 33280; ++i) {
410 tc_sha256_update(&s, m, sizeof(m));
411 }
412 (void) tc_sha256_final(digest, &s);
413
414 result = check_result(14, expected, sizeof(expected),
415 digest, sizeof(digest));
416 TC_END_RESULT(result);
417 return result;
418}
419
420/*
421 * Main task to test AES
422 */
423
424int main(void)
425{
426 uint32_t result = TC_PASS;
427
428 TC_START("Performing SHA256 tests (NIST tests vectors):");
429
430 result = test_1();
431 if (result == TC_FAIL) { /* terminate test */
432 TC_ERROR("SHA256 test #1 failed.\n");
433 goto exitTest;
434 }
435 result = test_2();
436 if (result == TC_FAIL) { /* terminate test */
437 TC_ERROR("SHA256 test #2 failed.\n");
438 goto exitTest;
439 }
440 result = test_3();
441 if (result == TC_FAIL) { /* terminate test */
442 TC_ERROR("SHA256 test #3 failed.\n");
443 goto exitTest;
444 }
445 result = test_4();
446 if (result == TC_FAIL) { /* terminate test */
447 TC_ERROR("SHA256 test #4 failed.\n");
448 goto exitTest;
449 }
450 result = test_5();
451 if (result == TC_FAIL) { /* terminate test */
452 TC_ERROR("SHA256 test #5 failed.\n");
453 goto exitTest;
454 }
455 result = test_6();
456 if (result == TC_FAIL) { /* terminate test */
457 TC_ERROR("SHA256 test #6 failed.\n");
458 goto exitTest;
459 }
460 result = test_7();
461 if (result == TC_FAIL) { /* terminate test */
462 TC_ERROR("SHA256 test #7 failed.\n");
463 goto exitTest;
464 }
465 result = test_8();
466 if (result == TC_FAIL) { /* terminate test */
467 TC_ERROR("SHA256 test #8 failed.\n");
468 goto exitTest;
469 }
470 result = test_9();
471 if (result == TC_FAIL) { /* terminate test */
472 TC_ERROR("SHA256 test #9 failed.\n");
473 goto exitTest;
474 }
475 result = test_10();
476 if (result == TC_FAIL) { /* terminate test */
477 TC_ERROR("SHA256 test #10 failed.\n");
478 goto exitTest;
479 }
480 result = test_11();
481 if (result == TC_FAIL) { /* terminate test */
482 TC_ERROR("SHA256 test #11 failed.\n");
483 goto exitTest;
484 }
485 result = test_12();
486 if (result == TC_FAIL) { /* terminate test */
487 TC_ERROR("SHA256 test #12 failed.\n");
488 goto exitTest;
489 }
490 result = test_13();
491 if (result == TC_FAIL) { /* terminate test */
492 TC_ERROR("SHA256 test #13 failed.\n");
493 goto exitTest;
494 }
495 result = test_14();
496 if (result == TC_FAIL) { /* terminate test */
497 TC_ERROR("SHA256 test #14 failed.\n");
498 goto exitTest;
499 }
500
501 TC_PRINT("All SHA256 tests succeeded!\n");
502
503exitTest:
504 TC_END_RESULT(result);
505 TC_END_REPORT(result);
506}