1 /*  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  * Scenarios tested include:
37  * - NIST SHA256 test vectors
38  */
39 
40 #include <tinycrypt/sha256.h>
41 #include <tinycrypt/constants.h>
42 #include <zephyr/test_utils.h>
43 
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <zephyr/types.h>
48 #include <zephyr/ztest.h>
49 
50 /*
51  * NIST SHA256 test vector 1.
52  */
ZTEST(tinycrypt,test_sha256_1)53 ZTEST(tinycrypt, test_sha256_1)
54 {
55 
56 	uint32_t result = TC_PASS;
57 
58 	TC_PRINT("SHA256 test #1:\n");
59 	const uint8_t expected[32] = {
60 		0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41,
61 		    0x40, 0xde,
62 		0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17,
63 		    0x7a, 0x9c,
64 		0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
65 	};
66 	const char *m = "abc";
67 	uint8_t digest[32];
68 	struct tc_sha256_state_struct s;
69 
70 	(void)tc_sha256_init(&s);
71 	tc_sha256_update(&s, (const uint8_t *)m, strlen(m));
72 	(void)tc_sha256_final(digest, &s);
73 	result = check_result(1, expected, sizeof(expected),
74 			      digest, sizeof(digest), 1);
75 
76 	/**TESTPOINT: Check result*/
77 	zassert_false(result, "SHA256 test #1 failed.");
78 }
79 
80 /*
81  * NIST SHA256 test vector 2.
82  */
ZTEST(tinycrypt,test_sha256_2)83 ZTEST(tinycrypt, test_sha256_2)
84 {
85 	uint32_t result = TC_PASS;
86 
87 	TC_PRINT("SHA256 test #2:\n");
88 	const uint8_t expected[32] = {
89 		0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, 0xe5, 0xc0,
90 		    0x26, 0x93,
91 		0x0c, 0x3e, 0x60, 0x39, 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff,
92 		    0x21, 0x67,
93 		0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1
94 	};
95 	const char *m =
96 	    "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
97 	uint8_t digest[32];
98 	struct tc_sha256_state_struct s;
99 
100 	(void)tc_sha256_init(&s);
101 	tc_sha256_update(&s, (const uint8_t *)m, strlen(m));
102 	(void)tc_sha256_final(digest, &s);
103 
104 	result = check_result(2, expected, sizeof(expected),
105 			      digest, sizeof(digest), 1);
106 
107 	/**TESTPOINT: Check result*/
108 	zassert_false(result, "SHA256 test #2 failed.");
109 }
110 
ZTEST(tinycrypt,test_sha256_3)111 ZTEST(tinycrypt, test_sha256_3)
112 {
113 	uint32_t result = TC_PASS;
114 
115 	TC_PRINT("SHA256 test #3:\n");
116 	const uint8_t expected[32] = {
117 		0x68, 0x32, 0x57, 0x20, 0xaa, 0xbd, 0x7c, 0x82, 0xf3, 0x0f,
118 		    0x55, 0x4b,
119 		0x31, 0x3d, 0x05, 0x70, 0xc9, 0x5a, 0xcc, 0xbb, 0x7d, 0xc4,
120 		    0xb5, 0xaa,
121 		0xe1, 0x12, 0x04, 0xc0, 0x8f, 0xfe, 0x73, 0x2b
122 	};
123 	const uint8_t m[1] = { 0xbd };
124 	uint8_t digest[32];
125 	struct tc_sha256_state_struct s;
126 
127 	(void)tc_sha256_init(&s);
128 	tc_sha256_update(&s, m, sizeof(m));
129 	(void)tc_sha256_final(digest, &s);
130 
131 	result = check_result(3, expected, sizeof(expected),
132 			      digest, sizeof(digest), 1);
133 
134 	/**TESTPOINT: Check result*/
135 	zassert_false(result, "SHA256 test #3 failed.");
136 
137 }
138 
ZTEST(tinycrypt,test_sha256_4)139 ZTEST(tinycrypt, test_sha256_4)
140 {
141 	uint32_t result = TC_PASS;
142 
143 	TC_PRINT("SHA256 test #4:\n");
144 	const uint8_t expected[32] = {
145 		0x7a, 0xbc, 0x22, 0xc0, 0xae, 0x5a, 0xf2, 0x6c, 0xe9, 0x3d,
146 		    0xbb, 0x94,
147 		0x43, 0x3a, 0x0e, 0x0b, 0x2e, 0x11, 0x9d, 0x01, 0x4f, 0x8e,
148 		    0x7f, 0x65,
149 		0xbd, 0x56, 0xc6, 0x1c, 0xcc, 0xcd, 0x95, 0x04
150 	};
151 	const uint8_t m[4] = { 0xc9, 0x8c, 0x8e, 0x55 };
152 	uint8_t digest[32];
153 	struct tc_sha256_state_struct s;
154 
155 	(void)tc_sha256_init(&s);
156 	tc_sha256_update(&s, m, sizeof(m));
157 	(void)tc_sha256_final(digest, &s);
158 
159 	result = check_result(4, expected, sizeof(expected),
160 			      digest, sizeof(digest), 1);
161 
162 	/**TESTPOINT: Check result*/
163 	zassert_false(result, "SHA256 test #4 failed.");
164 
165 }
166 
ZTEST(tinycrypt,test_sha256_5)167 ZTEST(tinycrypt, test_sha256_5)
168 {
169 	uint32_t result = TC_PASS;
170 
171 	TC_PRINT("SHA256 test #5:\n");
172 	const uint8_t expected[32] = {
173 		0x02, 0x77, 0x94, 0x66, 0xcd, 0xec, 0x16, 0x38, 0x11, 0xd0,
174 		    0x78, 0x81,
175 		0x5c, 0x63, 0x3f, 0x21, 0x90, 0x14, 0x13, 0x08, 0x14, 0x49,
176 		    0x00, 0x2f,
177 		0x24, 0xaa, 0x3e, 0x80, 0xf0, 0xb8, 0x8e, 0xf7
178 	};
179 	uint8_t m[55];
180 	uint8_t digest[32];
181 	struct tc_sha256_state_struct s;
182 
183 	(void)memset(m, 0x00, sizeof(m));
184 
185 	(void)tc_sha256_init(&s);
186 	tc_sha256_update(&s, m, sizeof(m));
187 	(void)tc_sha256_final(digest, &s);
188 
189 	result = check_result(5, expected, sizeof(expected),
190 			      digest, sizeof(digest), 1);
191 
192 	/**TESTPOINT: Check result*/
193 	zassert_false(result, "SHA256 test #5 failed.");
194 
195 }
196 
ZTEST(tinycrypt,test_sha256_6)197 ZTEST(tinycrypt, test_sha256_6)
198 {
199 	uint32_t result = TC_PASS;
200 
201 	TC_PRINT("SHA256 test #6:\n");
202 	const uint8_t expected[32] = {
203 		0xd4, 0x81, 0x7a, 0xa5, 0x49, 0x76, 0x28, 0xe7, 0xc7, 0x7e,
204 		    0x6b, 0x60,
205 		0x61, 0x07, 0x04, 0x2b, 0xbb, 0xa3, 0x13, 0x08, 0x88, 0xc5,
206 		    0xf4, 0x7a,
207 		0x37, 0x5e, 0x61, 0x79, 0xbe, 0x78, 0x9f, 0xbb
208 	};
209 	uint8_t m[56];
210 	uint8_t digest[32];
211 	struct tc_sha256_state_struct s;
212 
213 	(void)memset(m, 0x00, sizeof(m));
214 
215 	(void)tc_sha256_init(&s);
216 	tc_sha256_update(&s, m, sizeof(m));
217 	(void)tc_sha256_final(digest, &s);
218 
219 	result = check_result(6, expected, sizeof(expected),
220 			      digest, sizeof(digest), 1);
221 
222 	/**TESTPOINT: Check result*/
223 	zassert_false(result, "SHA256 test #6 failed.");
224 
225 }
226 
ZTEST(tinycrypt,test_sha256_7)227 ZTEST(tinycrypt, test_sha256_7)
228 {
229 	uint32_t result = TC_PASS;
230 
231 	TC_PRINT("SHA256 test #7:\n");
232 	const uint8_t expected[32] = {
233 		0x65, 0xa1, 0x6c, 0xb7, 0x86, 0x13, 0x35, 0xd5, 0xac, 0xe3,
234 		    0xc6, 0x07,
235 		0x18, 0xb5, 0x05, 0x2e, 0x44, 0x66, 0x07, 0x26, 0xda, 0x4c,
236 		    0xd1, 0x3b,
237 		0xb7, 0x45, 0x38, 0x1b, 0x23, 0x5a, 0x17, 0x85
238 	};
239 	uint8_t m[57];
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(7, expected, sizeof(expected),
250 			      digest, sizeof(digest), 1);
251 
252 	/**TESTPOINT: Check result*/
253 	zassert_false(result, "SHA256 test #7 failed.");
254 
255 }
256 
ZTEST(tinycrypt,test_sha256_8)257 ZTEST(tinycrypt, test_sha256_8)
258 {
259 	uint32_t result = TC_PASS;
260 
261 	TC_PRINT("SHA256 test #8:\n");
262 	const uint8_t expected[32] = {
263 		0xf5, 0xa5, 0xfd, 0x42, 0xd1, 0x6a, 0x20, 0x30, 0x27, 0x98,
264 		    0xef, 0x6e,
265 		0xd3, 0x09, 0x97, 0x9b, 0x43, 0x00, 0x3d, 0x23, 0x20, 0xd9,
266 		    0xf0, 0xe8,
267 		0xea, 0x98, 0x31, 0xa9, 0x27, 0x59, 0xfb, 0x4b
268 	};
269 	uint8_t m[64];
270 	uint8_t digest[32];
271 	struct tc_sha256_state_struct s;
272 
273 	(void)memset(m, 0x00, sizeof(m));
274 
275 	(void)tc_sha256_init(&s);
276 	tc_sha256_update(&s, m, sizeof(m));
277 	(void)tc_sha256_final(digest, &s);
278 
279 	result = check_result(8, expected, sizeof(expected),
280 			      digest, sizeof(digest), 1);
281 
282 	/**TESTPOINT: Check result*/
283 	zassert_false(result, "SHA256 test #8 failed.");
284 
285 }
286 
ZTEST(tinycrypt,test_sha256_9)287 ZTEST(tinycrypt, test_sha256_9)
288 {
289 	uint32_t result = TC_PASS;
290 
291 	TC_PRINT("SHA256 test #9:\n");
292 	const uint8_t expected[32] = {
293 		0x54, 0x1b, 0x3e, 0x9d, 0xaa, 0x09, 0xb2, 0x0b, 0xf8, 0x5f,
294 		    0xa2, 0x73,
295 		0xe5, 0xcb, 0xd3, 0xe8, 0x01, 0x85, 0xaa, 0x4e, 0xc2, 0x98,
296 		    0xe7, 0x65,
297 		0xdb, 0x87, 0x74, 0x2b, 0x70, 0x13, 0x8a, 0x53
298 	};
299 	uint8_t m[1000];
300 	uint8_t digest[32];
301 	struct tc_sha256_state_struct s;
302 
303 	(void)memset(m, 0x00, sizeof(m));
304 
305 	(void)tc_sha256_init(&s);
306 	tc_sha256_update(&s, m, sizeof(m));
307 	(void)tc_sha256_final(digest, &s);
308 
309 	result = check_result(9, expected, sizeof(expected),
310 			      digest, sizeof(digest), 1);
311 
312 	/**TESTPOINT: Check result*/
313 	zassert_false(result, "SHA256 test #9 failed.");
314 
315 }
316 
ZTEST(tinycrypt,test_sha256_10)317 ZTEST(tinycrypt, test_sha256_10)
318 {
319 	uint32_t result = TC_PASS;
320 
321 	TC_PRINT("SHA256 test #10:\n");
322 	const uint8_t expected[32] = {
323 		0xc2, 0xe6, 0x86, 0x82, 0x34, 0x89, 0xce, 0xd2, 0x01, 0x7f,
324 		    0x60, 0x59,
325 		0xb8, 0xb2, 0x39, 0x31, 0x8b, 0x63, 0x64, 0xf6, 0xdc, 0xd8,
326 		    0x35, 0xd0,
327 		0xa5, 0x19, 0x10, 0x5a, 0x1e, 0xad, 0xd6, 0xe4
328 	};
329 	uint8_t m[1000];
330 	uint8_t digest[32];
331 	struct tc_sha256_state_struct s;
332 
333 	(void)memset(m, 0x41, sizeof(m));
334 
335 	(void)tc_sha256_init(&s);
336 	tc_sha256_update(&s, m, sizeof(m));
337 	(void)tc_sha256_final(digest, &s);
338 
339 	result = check_result(10, expected, sizeof(expected),
340 			      digest, sizeof(digest), 1);
341 
342 	/**TESTPOINT: Check result*/
343 	zassert_false(result, "SHA256 test #10 failed.");
344 
345 }
346 
ZTEST(tinycrypt,test_sha256_11)347 ZTEST(tinycrypt, test_sha256_11)
348 {
349 	uint32_t result = TC_PASS;
350 
351 	TC_PRINT("SHA256 test #11:\n");
352 	const uint8_t expected[32] = {
353 		0xf4, 0xd6, 0x2d, 0xde, 0xc0, 0xf3, 0xdd, 0x90, 0xea, 0x13,
354 		    0x80, 0xfa,
355 		0x16, 0xa5, 0xff, 0x8d, 0xc4, 0xc5, 0x4b, 0x21, 0x74, 0x06,
356 		    0x50, 0xf2,
357 		0x4a, 0xfc, 0x41, 0x20, 0x90, 0x35, 0x52, 0xb0
358 	};
359 	uint8_t m[1005];
360 	uint8_t digest[32];
361 	struct tc_sha256_state_struct s;
362 
363 	(void)memset(m, 0x55, sizeof(m));
364 
365 	(void)tc_sha256_init(&s);
366 	tc_sha256_update(&s, m, sizeof(m));
367 	(void)tc_sha256_final(digest, &s);
368 
369 	result = check_result(11, expected, sizeof(expected),
370 			      digest, sizeof(digest), 1);
371 
372 	/**TESTPOINT: Check result*/
373 	zassert_false(result, "SHA256 test #11 failed.");
374 
375 }
376 
ZTEST(tinycrypt,test_sha256_12)377 ZTEST(tinycrypt, test_sha256_12)
378 {
379 	uint32_t result = TC_PASS;
380 
381 	TC_PRINT("SHA256 test #12:\n");
382 	const uint8_t expected[32] = {
383 		0xd2, 0x97, 0x51, 0xf2, 0x64, 0x9b, 0x32, 0xff, 0x57, 0x2b,
384 		    0x5e, 0x0a,
385 		0x9f, 0x54, 0x1e, 0xa6, 0x60, 0xa5, 0x0f, 0x94, 0xff, 0x0b,
386 		    0xee, 0xdf,
387 		0xb0, 0xb6, 0x92, 0xb9, 0x24, 0xcc, 0x80, 0x25
388 	};
389 	uint8_t m[1000];
390 	uint8_t digest[32];
391 	struct tc_sha256_state_struct s;
392 	uint32_t i;
393 
394 	(void)memset(m, 0x00, sizeof(m));
395 
396 	(void)tc_sha256_init(&s);
397 	for (i = 0U; i < 1000; ++i) {
398 		tc_sha256_update(&s, m, sizeof(m));
399 	}
400 	(void)tc_sha256_final(digest, &s);
401 
402 	result = check_result(12, expected, sizeof(expected),
403 			      digest, sizeof(digest), 1);
404 
405 	/**TESTPOINT: Check result*/
406 	zassert_false(result, "SHA256 test #12 failed.");
407 }
408 #if EXTREME_SLOW
ZTEST(tinycrypt,test_sha256_13)409 ZTEST(tinycrypt, test_sha256_13)
410 {
411 	uint32_t result = TC_PASS;
412 
413 	TC_PRINT("SHA256 test #13:\n");
414 	const uint8_t expected[32] = {
415 		0x15, 0xa1, 0x86, 0x8c, 0x12, 0xcc, 0x53, 0x95, 0x1e, 0x18,
416 		    0x23, 0x44,
417 		0x27, 0x74, 0x47, 0xcd, 0x09, 0x79, 0x53, 0x6b, 0xad, 0xcc,
418 		    0x51, 0x2a,
419 		0xd2, 0x4c, 0x67, 0xe9, 0xb2, 0xd4, 0xf3, 0xdd
420 	};
421 	uint8_t m[32768];
422 	uint8_t digest[32];
423 	struct tc_sha256_state_struct s;
424 	uint32_t i;
425 
426 	(void)memset(m, 0x5a, sizeof(m));
427 
428 	(void)tc_sha256_init(&s);
429 	for (i = 0U; i < 16384; ++i) {
430 		tc_sha256_update(&s, m, sizeof(m));
431 	}
432 	(void)tc_sha256_final(digest, &s);
433 
434 	result = check_result(13, expected, sizeof(expected),
435 			      digest, sizeof(digest), 1);
436 
437 	/**TESTPOINT: Check result*/
438 	zassert_false(result, "SHA256 test #13 failed.");
439 
440 }
441 
ZTEST(tinycrypt,test_sha256_14)442 ZTEST(tinycrypt, test_sha256_14)
443 {
444 	uint32_t result = TC_PASS;
445 
446 	TC_PRINT("SHA256 test #14:\n");
447 	const uint8_t expected[32] = {
448 		0x46, 0x1c, 0x19, 0xa9, 0x3b, 0xd4, 0x34, 0x4f, 0x92, 0x15,
449 		    0xf5, 0xec,
450 		0x64, 0x35, 0x70, 0x90, 0x34, 0x2b, 0xc6, 0x6b, 0x15, 0xa1,
451 		    0x48, 0x31,
452 		0x7d, 0x27, 0x6e, 0x31, 0xcb, 0xc2, 0x0b, 0x53
453 	};
454 	uint8_t m[32768];
455 	uint8_t digest[32];
456 	struct tc_sha256_state_struct s;
457 	uint32_t i;
458 
459 	(void)memset(m, 0x00, sizeof(m));
460 
461 	(void)tc_sha256_init(&s);
462 	for (i = 0U; i < 33280; ++i) {
463 		tc_sha256_update(&s, m, sizeof(m));
464 	}
465 	(void)tc_sha256_final(digest, &s);
466 
467 	result = check_result(14, expected, sizeof(expected),
468 			      digest, sizeof(digest), 1);
469 
470 	/**TESTPOINT: Check result*/
471 	zassert_false(result, "SHA256 test #14 failed.");
472 
473 }
474 #endif
475 
ZTEST(tinycrypt,test_sha256_13_and_14)476 ZTEST(tinycrypt, test_sha256_13_and_14)
477 {
478 #if EXTREME_SLOW
479 	result = test_sha256_13();
480 	result = test_sha256_14();
481 #endif
482 }
483