1 /*  test_hmac.c - TinyCrypt implementation of some HMAC 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 HMAC routines:
36  * Scenarios tested include:
37  * - HMAC tests (RFC 4231 test vectors)
38  */
39 
40 #include <tinycrypt/hmac.h>
41 #include <tinycrypt/constants.h>
42 #include <zephyr/test_utils.h>
43 #include <zephyr/ztest.h>
44 
do_hmac_test(TCHmacState_t h,uint32_t testnum,const uint8_t * data,size_t datalen,const uint8_t * expected,size_t expectedlen)45 static uint32_t do_hmac_test(TCHmacState_t h, uint32_t testnum, const uint8_t *data,
46 		      size_t datalen, const uint8_t *expected,
47 		      size_t expectedlen)
48 {
49 	uint8_t digest[32];
50 	uint32_t result = TC_PASS;
51 
52 	(void)tc_hmac_init(h);
53 	(void)tc_hmac_update(h, data, datalen);
54 	(void)tc_hmac_final(digest, TC_SHA256_DIGEST_SIZE, h);
55 	result = check_result(testnum, expected, expectedlen,
56 			      digest, sizeof(digest), 1);
57 	return result;
58 }
59 
60 /*
61  * NIST test vectors for encryption.
62  */
ZTEST(tinycrypt,test_hmac_1)63 ZTEST(tinycrypt, test_hmac_1)
64 {
65 	uint32_t result = TC_PASS;
66 
67 
68 	TC_PRINT("HMAC %s:\n", __func__);
69 
70 	const uint8_t key[20] = {
71 		0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
72 		    0x0b, 0x0b,
73 		0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b
74 	};
75 	const uint8_t data[8] = {
76 		0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65
77 	};
78 	const uint8_t expected[32] = {
79 		0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53, 0x5c, 0xa8,
80 		    0xaf, 0xce,
81 		0xaf, 0x0b, 0xf1, 0x2b, 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83,
82 		    0x3d, 0xa7,
83 		0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
84 	};
85 	struct tc_hmac_state_struct h;
86 
87 	(void)memset(&h, 0x00, sizeof(h));
88 	(void)tc_hmac_set_key(&h, key, sizeof(key));
89 	result = do_hmac_test(&h, 1, data, sizeof(data),
90 			      expected, sizeof(expected));
91 
92 	/**TESTPOINT: Check result*/
93 	zassert_false(result, "HMAC test #7 failed");
94 }
95 
ZTEST(tinycrypt,test_hmac_2)96 ZTEST(tinycrypt, test_hmac_2)
97 {
98 	uint32_t result = TC_PASS;
99 
100 	TC_PRINT("HMAC %s:\n", __func__);
101 	const uint8_t key[4] = {
102 		0x4a, 0x65, 0x66, 0x65
103 	};
104 	const uint8_t data[28] = {
105 		0x77, 0x68, 0x61, 0x74, 0x20, 0x64, 0x6f, 0x20, 0x79, 0x61,
106 		    0x20, 0x77,
107 		0x61, 0x6e, 0x74, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x6e, 0x6f,
108 		    0x74, 0x68,
109 		0x69, 0x6e, 0x67, 0x3f
110 	};
111 	const uint8_t expected[32] = {
112 		0x5b, 0xdc, 0xc1, 0x46, 0xbf, 0x60, 0x75, 0x4e, 0x6a, 0x04,
113 		    0x24, 0x26,
114 		0x08, 0x95, 0x75, 0xc7, 0x5a, 0x00, 0x3f, 0x08, 0x9d, 0x27,
115 		    0x39, 0x83,
116 		0x9d, 0xec, 0x58, 0xb9, 0x64, 0xec, 0x38, 0x43
117 	};
118 	struct tc_hmac_state_struct h;
119 
120 	(void)memset(&h, 0x00, sizeof(h));
121 	(void)tc_hmac_set_key(&h, key, sizeof(key));
122 
123 	result = do_hmac_test(&h, 2, data, sizeof(data),
124 			      expected, sizeof(expected));
125 
126 	/**TESTPOINT: Check result*/
127 	zassert_false(result, "HMAC test #7 failed");
128 }
129 
ZTEST(tinycrypt,test_hmac_3)130 ZTEST(tinycrypt, test_hmac_3)
131 {
132 	uint32_t result = TC_PASS;
133 
134 	TC_PRINT("HMAC %s:\n", __func__);
135 	const uint8_t key[20] = {
136 		0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
137 		    0xaa, 0xaa,
138 		0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
139 	};
140 	const uint8_t data[50] = {
141 		0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
142 		    0xdd, 0xdd,
143 		0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
144 		    0xdd, 0xdd,
145 		0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
146 		    0xdd, 0xdd,
147 		0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
148 		    0xdd, 0xdd,
149 		0xdd, 0xdd
150 	};
151 	const uint8_t expected[32] = {
152 		0x77, 0x3e, 0xa9, 0x1e, 0x36, 0x80, 0x0e, 0x46, 0x85, 0x4d,
153 		    0xb8, 0xeb,
154 		0xd0, 0x91, 0x81, 0xa7, 0x29, 0x59, 0x09, 0x8b, 0x3e, 0xf8,
155 		    0xc1, 0x22,
156 		0xd9, 0x63, 0x55, 0x14, 0xce, 0xd5, 0x65, 0xfe
157 	};
158 	struct tc_hmac_state_struct h;
159 
160 	(void)memset(&h, 0x00, sizeof(h));
161 	(void)tc_hmac_set_key(&h, key, sizeof(key));
162 
163 	result = do_hmac_test(&h, 3, data, sizeof(data),
164 			      expected, sizeof(expected));
165 
166 	/**TESTPOINT: Check result*/
167 	zassert_false(result, "HMAC test #3 failed");
168 }
169 
ZTEST(tinycrypt,test_hmac_4)170 ZTEST(tinycrypt, test_hmac_4)
171 {
172 	uint32_t result = TC_PASS;
173 
174 	TC_PRINT("HMAC %s:\n", __func__);
175 	const uint8_t key[25] = {
176 		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
177 		    0x0b, 0x0c,
178 		0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
179 		    0x17, 0x18,
180 		0x19
181 	};
182 	const uint8_t data[50] = {
183 		0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
184 		    0xcd, 0xcd,
185 		0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
186 		    0xcd, 0xcd,
187 		0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
188 		    0xcd, 0xcd,
189 		0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
190 		    0xcd, 0xcd,
191 		0xcd, 0xcd
192 	};
193 	const uint8_t expected[32] = {
194 		0x82, 0x55, 0x8a, 0x38, 0x9a, 0x44, 0x3c, 0x0e, 0xa4, 0xcc,
195 		    0x81, 0x98,
196 		0x99, 0xf2, 0x08, 0x3a, 0x85, 0xf0, 0xfa, 0xa3, 0xe5, 0x78,
197 		    0xf8, 0x07,
198 		0x7a, 0x2e, 0x3f, 0xf4, 0x67, 0x29, 0x66, 0x5b
199 	};
200 	struct tc_hmac_state_struct h;
201 
202 	(void)memset(&h, 0x00, sizeof(h));
203 	(void)tc_hmac_set_key(&h, key, sizeof(key));
204 
205 	result = do_hmac_test(&h, 4, data, sizeof(data),
206 			      expected, sizeof(expected));
207 
208 	/**TESTPOINT: Check result*/
209 	zassert_false(result, "HMAC test #4 failed");
210 }
211 
ZTEST(tinycrypt,test_hmac_5)212 ZTEST(tinycrypt, test_hmac_5)
213 {
214 	uint32_t result = TC_PASS;
215 
216 	TC_PRINT("HMAC %s:\n", __func__);
217 	const uint8_t key[20] = {
218 		0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
219 		    0x0c, 0x0c,
220 		0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c
221 	};
222 	const uint8_t data[20] = {
223 		0x54, 0x65, 0x73, 0x74, 0x20, 0x57, 0x69, 0x74, 0x68, 0x20,
224 		    0x54, 0x72,
225 		0x75, 0x6e, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e
226 	};
227 	const uint8_t expected[32] = {
228 		0xa3, 0xb6, 0x16, 0x74, 0x73, 0x10, 0x0e, 0xe0, 0x6e, 0x0c,
229 		    0x79, 0x6c,
230 		0x29, 0x55, 0x55, 0x2b, 0xfa, 0x6f, 0x7c, 0x0a, 0x6a, 0x8a,
231 		    0xef, 0x8b,
232 		0x93, 0xf8, 0x60, 0xaa, 0xb0, 0xcd, 0x20, 0xc5
233 	};
234 	struct tc_hmac_state_struct h;
235 
236 	(void)memset(&h, 0x00, sizeof(h));
237 	(void)tc_hmac_set_key(&h, key, sizeof(key));
238 
239 	result = do_hmac_test(&h, 5, data, sizeof(data),
240 			      expected, sizeof(expected));
241 
242 	/**TESTPOINT: Check result*/
243 	zassert_false(result, "HMAC test #5 failed");
244 }
245 
ZTEST(tinycrypt,test_hmac_6)246 ZTEST(tinycrypt, test_hmac_6)
247 {
248 	uint32_t result = TC_PASS;
249 
250 	TC_PRINT("HMAC %s:\n", __func__);
251 	const uint8_t key[131] = {
252 		0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
253 		    0xaa, 0xaa,
254 		0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
255 		    0xaa, 0xaa,
256 		0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
257 		    0xaa, 0xaa,
258 		0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
259 		    0xaa, 0xaa,
260 		0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
261 		    0xaa, 0xaa,
262 		0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
263 		    0xaa, 0xaa,
264 		0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
265 		    0xaa, 0xaa,
266 		0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
267 		    0xaa, 0xaa,
268 		0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
269 		    0xaa, 0xaa,
270 		0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
271 		    0xaa, 0xaa,
272 		0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
273 	};
274 	const uint8_t data[54] = {
275 		0x54, 0x65, 0x73, 0x74, 0x20, 0x55, 0x73, 0x69, 0x6e, 0x67,
276 		    0x20, 0x4c,
277 		0x61, 0x72, 0x67, 0x65, 0x72, 0x20, 0x54, 0x68, 0x61, 0x6e,
278 		    0x20, 0x42,
279 		0x6c, 0x6f, 0x63, 0x6b, 0x2d, 0x53, 0x69, 0x7a, 0x65, 0x20,
280 		    0x4b, 0x65,
281 		0x79, 0x20, 0x2d, 0x20, 0x48, 0x61, 0x73, 0x68, 0x20, 0x4b,
282 		    0x65, 0x79,
283 		0x20, 0x46, 0x69, 0x72, 0x73, 0x74
284 	};
285 	const uint8_t expected[32] = {
286 		0x60, 0xe4, 0x31, 0x59, 0x1e, 0xe0, 0xb6, 0x7f, 0x0d, 0x8a,
287 		    0x26, 0xaa,
288 		0xcb, 0xf5, 0xb7, 0x7f, 0x8e, 0x0b, 0xc6, 0x21, 0x37, 0x28,
289 		    0xc5, 0x14,
290 		0x05, 0x46, 0x04, 0x0f, 0x0e, 0xe3, 0x7f, 0x54
291 	};
292 	struct tc_hmac_state_struct h;
293 
294 	(void)memset(&h, 0x00, sizeof(h));
295 	(void)tc_hmac_set_key(&h, key, sizeof(key));
296 
297 	result = do_hmac_test(&h, 6, data, sizeof(data),
298 			      expected, sizeof(expected));
299 
300 	/**TESTPOINT: Check result*/
301 	zassert_false(result, "HMAC test #6 failed");
302 }
303 
ZTEST(tinycrypt,test_hmac_7)304 ZTEST(tinycrypt, test_hmac_7)
305 {
306 	uint32_t result = TC_PASS;
307 
308 	TC_PRINT("HMAC %s:\n", __func__);
309 	const uint8_t key[131] = {
310 		0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
311 		    0xaa, 0xaa,
312 		0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
313 		    0xaa, 0xaa,
314 		0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
315 		    0xaa, 0xaa,
316 		0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
317 		    0xaa, 0xaa,
318 		0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
319 		    0xaa, 0xaa,
320 		0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
321 		    0xaa, 0xaa,
322 		0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
323 		    0xaa, 0xaa,
324 		0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
325 		    0xaa, 0xaa,
326 		0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
327 		    0xaa, 0xaa,
328 		0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
329 		    0xaa, 0xaa,
330 		0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
331 	};
332 	const uint8_t data[152] = {
333 		0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20,
334 		    0x74, 0x65,
335 		0x73, 0x74, 0x20, 0x75, 0x73, 0x69, 0x6e, 0x67, 0x20, 0x61,
336 		    0x20, 0x6c,
337 		0x61, 0x72, 0x67, 0x65, 0x72, 0x20, 0x74, 0x68, 0x61, 0x6e,
338 		    0x20, 0x62,
339 		0x6c, 0x6f, 0x63, 0x6b, 0x2d, 0x73, 0x69, 0x7a, 0x65, 0x20,
340 		    0x6b, 0x65,
341 		0x79, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x61, 0x20, 0x6c, 0x61,
342 		    0x72, 0x67,
343 		0x65, 0x72, 0x20, 0x74, 0x68, 0x61, 0x6e, 0x20, 0x62, 0x6c,
344 		    0x6f, 0x63,
345 		0x6b, 0x2d, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x64, 0x61, 0x74,
346 		    0x61, 0x2e,
347 		0x20, 0x54, 0x68, 0x65, 0x20, 0x6b, 0x65, 0x79, 0x20, 0x6e,
348 		    0x65, 0x65,
349 		0x64, 0x73, 0x20, 0x74, 0x6f, 0x20, 0x62, 0x65, 0x20, 0x68,
350 		    0x61, 0x73,
351 		0x68, 0x65, 0x64, 0x20, 0x62, 0x65, 0x66, 0x6f, 0x72, 0x65,
352 		    0x20, 0x62,
353 		0x65, 0x69, 0x6e, 0x67, 0x20, 0x75, 0x73, 0x65, 0x64, 0x20,
354 		    0x62, 0x79,
355 		0x20, 0x74, 0x68, 0x65, 0x20, 0x48, 0x4d, 0x41, 0x43, 0x20,
356 		    0x61, 0x6c,
357 		0x67, 0x6f, 0x72, 0x69, 0x74, 0x68, 0x6d, 0x2e
358 	};
359 	const uint8_t expected[32] = {
360 		0x9b, 0x09, 0xff, 0xa7, 0x1b, 0x94, 0x2f, 0xcb, 0x27, 0x63,
361 		    0x5f, 0xbc,
362 		0xd5, 0xb0, 0xe9, 0x44, 0xbf, 0xdc, 0x63, 0x64, 0x4f, 0x07,
363 		    0x13, 0x93,
364 		0x8a, 0x7f, 0x51, 0x53, 0x5c, 0x3a, 0x35, 0xe2
365 	};
366 	struct tc_hmac_state_struct h;
367 
368 	(void)memset(&h, 0x00, sizeof(h));
369 	(void)tc_hmac_set_key(&h, key, sizeof(key));
370 
371 	result = do_hmac_test(&h, 7, data, sizeof(data),
372 			      expected, sizeof(expected));
373 
374 	/**TESTPOINT: Check result*/
375 	zassert_false(result, "HMAC test #7 failed");
376 }
377