]>
Commit | Line | Data |
---|---|---|
72200048 MW |
1 | /** |
2 | * @file hmac_test.h | |
3 | * | |
ed37dee6 | 4 | * @brief Tests for the hmac_t class. |
72200048 MW |
5 | * |
6 | */ | |
7 | ||
8 | /* | |
9 | * Copyright (C) 2005 Jan Hutter, Martin Willi | |
10 | * Hochschule fuer Technik Rapperswil | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or modify it | |
13 | * under the terms of the GNU General Public License as published by the | |
14 | * Free Software Foundation; either version 2 of the License, or (at your | |
15 | * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. | |
16 | * | |
17 | * This program is distributed in the hope that it will be useful, but | |
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
19 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
20 | * for more details. | |
21 | */ | |
22 | ||
23 | #include <string.h> | |
24 | ||
25 | #include "hmac_test.h" | |
26 | ||
68621281 | 27 | #include <crypto/hmac.h> |
72200048 MW |
28 | |
29 | ||
30 | /* | |
31 | * described in Header-File | |
32 | */ | |
51d56047 | 33 | void test_hmac_sha1(protected_tester_t *tester) |
72200048 MW |
34 | { |
35 | /* | |
36 | * Test cases from RFC2202 | |
37 | * | |
38 | * test_case = 1 | |
39 | * key = 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b | |
40 | * key_len = 20 | |
41 | * data = "Hi There" | |
42 | * data_len = 8 | |
43 | * digest = 0xb617318655057264e28bc0b6fb378c8ef146be00 | |
44 | * | |
45 | * test_case = 2 | |
46 | * key = "Jefe" | |
47 | * key_len = 4 | |
48 | * data = "what do ya want for nothing?" | |
49 | * data_len = 28 | |
50 | * digest = 0xeffcdf6ae5eb2fa2d27416d5f184df9c259a7c79 | |
51 | * | |
52 | * test_case = 3 | |
53 | * key = 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | |
54 | * key_len = 20 | |
55 | * data = 0xdd repeated 50 times | |
56 | * data_len = 50 | |
57 | * digest = 0x125d7342b9ac11cd91a39af48aa17b4f63f175d3 | |
58 | * | |
59 | * test_case = 4 | |
60 | * key = 0x0102030405060708090a0b0c0d0e0f10111213141516171819 | |
61 | * key_len = 25 | |
62 | * data = 0xcd repeated 50 times | |
63 | * data_len = 50 | |
64 | * digest = 0x4c9007f4026250c6bc8414f9bf50c86c2d7235da | |
65 | * | |
66 | * test_case = 5 | |
67 | * key = 0x0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c | |
68 | * key_len = 20 | |
69 | * data = "Test With Truncation" | |
70 | * data_len = 20 | |
71 | * digest = 0x4c1a03424b55e07fe7f27be1d58bb9324a9a5a04 | |
72 | * digest-96 = 0x4c1a03424b55e07fe7f27be1 | |
73 | * | |
74 | * test_case = 6 | |
75 | * key = 0xaa repeated 80 times | |
76 | * key_len = 80 | |
77 | * data = "Test Using Larger Than Block-Size Key - Hash Key First" | |
78 | * data_len = 54 | |
79 | * digest = 0xaa4ae5e15272d00e95705637ce8a3b55ed402112 | |
80 | * | |
81 | * test_case = 7 | |
82 | * key = 0xaa repeated 80 times | |
83 | * key_len = 80 | |
84 | * data = "Test Using Larger Than Block-Size Key and Larger | |
85 | * Than One Block-Size Data" | |
86 | * data_len = 73 | |
87 | * digest = 0xe8e99d0f45237d786d6bbaa7965c7808bbff1a91 | |
88 | * | |
a70d953d | 89 | * currently performing test 1, 2, 4 and 7 |
72200048 MW |
90 | */ |
91 | ||
a70d953d MW |
92 | chunk_t keys[4]; |
93 | chunk_t data[4]; | |
94 | chunk_t digest[4]; | |
95 | chunk_t reference[4]; | |
72200048 MW |
96 | int i; |
97 | ||
98 | /* | |
99 | * values for test 1 | |
100 | */ | |
101 | u_int8_t key1[] = { | |
102 | 0x0b,0x0b,0x0b,0x0b, | |
103 | 0x0b,0x0b,0x0b,0x0b, | |
104 | 0x0b,0x0b,0x0b,0x0b, | |
105 | 0x0b,0x0b,0x0b,0x0b, | |
106 | 0x0b,0x0b,0x0b,0x0b | |
107 | }; | |
108 | keys[0].ptr = key1; | |
109 | keys[0].len = sizeof(key1); | |
110 | data[0].ptr = "Hi There"; | |
111 | data[0].len = 8; | |
112 | u_int8_t reference1[] = { | |
113 | 0xb6,0x17,0x31,0x86, | |
114 | 0x55,0x05,0x72,0x64, | |
115 | 0xe2,0x8b,0xc0,0xb6, | |
116 | 0xfb,0x37,0x8c,0x8e, | |
117 | 0xf1,0x46,0xbe,0x00 | |
118 | }; | |
119 | reference[0].ptr = reference1; | |
120 | reference[0].len = sizeof(reference1); | |
121 | ||
122 | /* | |
123 | * values for test 2 | |
124 | */ | |
125 | u_int8_t reference2[] = { | |
126 | 0xef,0xfc,0xdf,0x6a, | |
127 | 0xe5,0xeb,0x2f,0xa2, | |
128 | 0xd2,0x74,0x16,0xd5, | |
129 | 0xf1,0x84,0xdf,0x9c, | |
130 | 0x25,0x9a,0x7c,0x79 | |
131 | }; | |
132 | keys[1].ptr = "Jefe"; | |
133 | keys[1].len = 4; | |
134 | data[1].ptr = "what do ya want for nothing?"; | |
135 | data[1].len = 28; | |
136 | reference[1].ptr = reference2; | |
137 | reference[1].len = sizeof(reference2); | |
138 | ||
139 | /* | |
140 | * values for test 7 | |
141 | */ | |
142 | u_int8_t key7[] = { | |
143 | 0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa, | |
144 | 0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa, | |
145 | 0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa, | |
146 | 0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa, | |
147 | 0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa, | |
148 | 0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa, | |
149 | 0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa, | |
150 | 0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa, | |
151 | 0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa, | |
152 | 0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa, | |
153 | }; | |
154 | u_int8_t reference7[] = { | |
155 | 0xe8,0xe9,0x9d,0x0f, | |
156 | 0x45,0x23,0x7d,0x78, | |
157 | 0x6d,0x6b,0xba,0xa7, | |
158 | 0x96,0x5c,0x78,0x08, | |
159 | 0xbb,0xff,0x1a,0x91 | |
160 | }; | |
161 | keys[2].ptr = key7; | |
162 | keys[2].len = sizeof(key7); | |
163 | data[2].ptr = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"; | |
164 | data[2].len = 73; | |
165 | reference[2].ptr = reference7; | |
166 | reference[2].len = sizeof(reference7); | |
167 | ||
168 | ||
169 | for (i=0; i<3; i++) | |
170 | { | |
a217b51d MW |
171 | hmac_t *hmac = hmac_create(HASH_SHA1); |
172 | hmac->set_key(hmac, keys[i]); | |
72200048 MW |
173 | hmac->allocate_mac(hmac, data[i], &digest[i]); |
174 | hmac->destroy(hmac); | |
175 | ||
176 | tester->assert_true(tester, digest[i].len == 20, "chunk len"); | |
177 | tester->assert_false(tester, memcmp(digest[i].ptr, reference[i].ptr, 20), "hmac value"); | |
5113680f | 178 | free(digest[i].ptr); |
72200048 | 179 | } |
a70d953d MW |
180 | |
181 | /* | |
182 | * test 4 is donne in append mode | |
183 | */ | |
184 | u_int8_t val = 0xcd; | |
185 | ||
186 | u_int8_t key4[] = { | |
187 | 0x01,0x02,0x03,0x04, | |
188 | 0x05,0x06,0x07,0x08, | |
189 | 0x09,0x0a,0x0b,0x0c, | |
190 | 0x0d,0x0e,0x0f,0x10, | |
191 | 0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19 | |
192 | }; | |
193 | keys[3].ptr = key4; | |
194 | keys[3].len = sizeof(key4); | |
195 | u_int8_t reference4[] = { | |
196 | 0x4c,0x90,0x07,0xf4, | |
197 | 0x02,0x62,0x50,0xc6, | |
198 | 0xbc,0x84,0x14,0xf9, | |
199 | 0xbf,0x50,0xc8,0x6c, | |
200 | 0x2d,0x72,0x35,0xda | |
201 | }; | |
202 | reference[3].ptr = reference4; | |
203 | reference[3].len = sizeof(reference4); | |
204 | ||
205 | hmac_t *hmac = hmac_create(HASH_SHA1); | |
206 | hmac->set_key(hmac, keys[3]); | |
207 | data[3].ptr = &val; | |
208 | data[3].len = 1; | |
209 | for (i=0; i<49; i++) | |
210 | { | |
211 | hmac->get_mac(hmac, data[3], NULL); | |
212 | } | |
213 | hmac->allocate_mac(hmac, data[3], &digest[3]); | |
214 | hmac->destroy(hmac); | |
215 | ||
216 | tester->assert_true(tester, digest[3].len == 20, "chunk len append mode"); | |
217 | tester->assert_false(tester, memcmp(digest[3].ptr, reference[3].ptr, 20), "hmac value append mode"); | |
5113680f | 218 | free(digest[3].ptr); |
72200048 | 219 | } |
2018a40a MW |
220 | |
221 | /* | |
222 | * described in Header-File | |
223 | */ | |
51d56047 | 224 | void test_hmac_md5(protected_tester_t *tester) |
2018a40a MW |
225 | { |
226 | /* | |
227 | * Test cases from RFC2202 | |
228 | * | |
229 | * test_case = 1 | |
230 | * key = 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b | |
231 | * key_len = 16 | |
232 | * data = "Hi There" | |
233 | * data_len = 8 | |
234 | * digest = 0x9294727a3638bb1c13f48ef8158bfc9d | |
235 | * | |
236 | * test_case = 2 | |
237 | * key = "Jefe" | |
238 | * key_len = 4 | |
239 | * data = "what do ya want for nothing?" | |
240 | * data_len = 28 | |
241 | * digest = 0x750c783e6ab0b503eaa86e310a5db738 | |
242 | * | |
243 | * test_case = 3 | |
244 | * key = 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | |
245 | * key_len 16 | |
246 | * data = 0xdd repeated 50 times | |
247 | * data_len = 50 | |
248 | * digest = 0x56be34521d144c88dbb8c733f0e8b3f6 | |
249 | * | |
250 | * test_case = 4 | |
251 | * key = 0x0102030405060708090a0b0c0d0e0f10111213141516171819 | |
252 | * key_len 25 | |
253 | * data = 0xcd repeated 50 times | |
254 | * data_len = 50 | |
255 | * digest = 0x697eaf0aca3a3aea3a75164746ffaa79 | |
256 | * | |
257 | * test_case = 5 | |
258 | * key = 0x0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c | |
259 | * key_len = 16 | |
260 | * data = "Test With Truncation" | |
261 | * data_len = 20 | |
262 | * digest = 0x56461ef2342edc00f9bab995690efd4c | |
263 | * digest-96 0x56461ef2342edc00f9bab995 | |
264 | * | |
265 | * test_case = 6 | |
266 | * key = 0xaa repeated 80 times | |
267 | * key_len = 80 | |
268 | * data = "Test Using Larger Than Block-Size Key - Hash Key First" | |
269 | * data_len = 54 | |
270 | * digest = 0x6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd | |
271 | * | |
272 | * test_case = 7 | |
273 | * key = 0xaa repeated 80 times | |
274 | * key_len = 80 | |
275 | * data = "Test Using Larger Than Block-Size Key and Larger | |
276 | * Than One Block-Size Data" | |
277 | * data_len = 73 | |
278 | * digest = 0x6f630fad67cda0ee1fb1f562db3aa53e | |
279 | * | |
280 | * | |
281 | * | |
282 | * currently performing test 1, 2, 4 and 7 | |
283 | * | |
284 | */ | |
285 | chunk_t keys[4]; | |
286 | chunk_t data[4]; | |
287 | chunk_t digest[4]; | |
288 | chunk_t reference[4]; | |
289 | int i; | |
290 | ||
291 | /* | |
292 | * values for test 1 | |
293 | */ | |
294 | u_int8_t key1[] = { | |
295 | 0x0b,0x0b,0x0b,0x0b, | |
296 | 0x0b,0x0b,0x0b,0x0b, | |
297 | 0x0b,0x0b,0x0b,0x0b, | |
298 | 0x0b,0x0b,0x0b,0x0b, | |
299 | }; | |
300 | keys[0].ptr = key1; | |
301 | keys[0].len = sizeof(key1); | |
302 | data[0].ptr = "Hi There"; | |
303 | data[0].len = 8; | |
304 | u_int8_t reference1[] = { | |
305 | 0x92,0x94,0x72,0x7a, | |
306 | 0x36,0x38,0xbb,0x1c, | |
307 | 0x13,0xf4,0x8e,0xf8, | |
308 | 0x15,0x8b,0xfc,0x9d | |
309 | }; | |
310 | reference[0].ptr = reference1; | |
311 | reference[0].len = sizeof(reference1); | |
312 | ||
313 | /* | |
314 | * values for test 2 | |
315 | */ | |
316 | u_int8_t reference2[] = { | |
317 | 0x75,0x0c,0x78,0x3e, | |
318 | 0x6a,0xb0,0xb5,0x03, | |
319 | 0xea,0xa8,0x6e,0x31, | |
320 | 0x0a,0x5d,0xb7,0x38 | |
321 | }; | |
322 | keys[1].ptr = "Jefe"; | |
323 | keys[1].len = 4; | |
324 | data[1].ptr = "what do ya want for nothing?"; | |
325 | data[1].len = 28; | |
326 | reference[1].ptr = reference2; | |
327 | reference[1].len = sizeof(reference2); | |
328 | ||
329 | /* | |
330 | * values for test 7 | |
331 | */ | |
332 | u_int8_t key7[] = { | |
333 | 0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa, | |
334 | 0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa, | |
335 | 0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa, | |
336 | 0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa, | |
337 | 0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa, | |
338 | 0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa, | |
339 | 0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa, | |
340 | 0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa, | |
341 | 0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa, | |
342 | 0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa, | |
343 | }; | |
344 | u_int8_t reference7[] = { | |
345 | 0x6f,0x63,0x0f,0xad, | |
346 | 0x67,0xcd,0xa0,0xee, | |
347 | 0x1f,0xb1,0xf5,0x62, | |
348 | 0xdb,0x3a,0xa5,0x3e | |
349 | }; | |
350 | keys[2].ptr = key7; | |
351 | keys[2].len = sizeof(key7); | |
352 | data[2].ptr = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"; | |
353 | data[2].len = 73; | |
354 | reference[2].ptr = reference7; | |
355 | reference[2].len = sizeof(reference7); | |
356 | ||
357 | ||
358 | for (i=0; i<3; i++) | |
359 | { | |
360 | hmac_t *hmac = hmac_create(HASH_MD5); | |
361 | hmac->set_key(hmac, keys[i]); | |
362 | hmac->allocate_mac(hmac, data[i], &digest[i]); | |
363 | hmac->destroy(hmac); | |
364 | tester->assert_true(tester, digest[i].len == 16, "chunk len"); | |
365 | tester->assert_false(tester, memcmp(digest[i].ptr, reference[i].ptr, 16), "hmac value"); | |
5113680f | 366 | free(digest[i].ptr); |
2018a40a MW |
367 | } |
368 | ||
369 | /* | |
370 | * test 4 is donne in append mode | |
371 | */ | |
372 | u_int8_t val = 0xcd; | |
373 | ||
374 | u_int8_t key4[] = { | |
375 | 0x01,0x02,0x03,0x04, | |
376 | 0x05,0x06,0x07,0x08, | |
377 | 0x09,0x0a,0x0b,0x0c, | |
378 | 0x0d,0x0e,0x0f,0x10, | |
379 | 0x11,0x12,0x13,0x14, | |
380 | 0x15,0x16,0x17,0x18, | |
381 | 0x19 | |
382 | }; | |
383 | keys[3].ptr = key4; | |
384 | keys[3].len = sizeof(key4); | |
385 | u_int8_t reference4[] = { | |
386 | 0x69,0x7e,0xaf,0x0a, | |
387 | 0xca,0x3a,0x3a,0xea, | |
388 | 0x3a,0x75,0x16,0x47, | |
389 | 0x46,0xff,0xaa,0x79 | |
390 | }; | |
391 | reference[3].ptr = reference4; | |
392 | reference[3].len = sizeof(reference4); | |
393 | ||
394 | hmac_t *hmac = hmac_create(HASH_MD5); | |
395 | hmac->set_key(hmac, keys[3]); | |
396 | data[3].ptr = &val; | |
397 | data[3].len = 1; | |
398 | for (i=0; i<49; i++) | |
399 | { | |
400 | hmac->get_mac(hmac, data[3], NULL); | |
401 | } | |
402 | hmac->allocate_mac(hmac, data[3], &digest[3]); | |
403 | hmac->destroy(hmac); | |
404 | ||
405 | tester->assert_true(tester, digest[3].len == 16, "chunk len append mode"); | |
406 | tester->assert_false(tester, memcmp(digest[3].ptr, reference[3].ptr, 16), "hmac value append mode"); | |
5113680f | 407 | free(digest[3].ptr); |
2018a40a | 408 | } |