Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / nettle / testsuite / umac-test.c
blob1e9a5580f4a4f85a5ba00c17a4ff36f423002101
1 #include "testutils.h"
2 #include "umac.h"
4 /* FIXME: Missing tests:
6 Getting to unlikely cases in the poly64 and poly128 operations.
7 */
9 static void
10 update (void *ctx, nettle_hash_update_func *f,
11 const struct tstring *msg,
12 unsigned length)
14 for (; length > msg->length; length -= msg->length)
15 f(ctx, msg->length, msg->data);
16 f(ctx, length, msg->data);
19 static void
20 check_digest (const char *name, void *ctx, nettle_hash_digest_func *f,
21 const struct tstring *msg, unsigned length,
22 unsigned tag_length, const uint8_t *ref)
24 uint8_t tag[16];
25 f(ctx, tag_length, tag);
26 if (memcmp (tag, ref, tag_length) != 0)
28 printf ("%s failed\n", name);
29 printf ("msg: "); print_hex (msg->length, msg->data);
30 printf ("length: %u\n", length);
31 printf ("tag: "); print_hex (tag_length, tag);
32 printf ("ref: "); print_hex (tag_length, ref);
33 abort ();
38 static void
39 test_umac (const struct tstring *key,
40 const struct tstring *nonce,
41 const struct tstring *msg,
42 unsigned length,
43 const struct tstring *ref32,
44 const struct tstring *ref64,
45 const struct tstring *ref128)
47 struct umac32_ctx ctx32;
48 struct umac64_ctx ctx64;
49 struct umac96_ctx ctx96;
50 struct umac128_ctx ctx128;
52 ASSERT (key->length == UMAC_KEY_SIZE);
53 ASSERT (ref32->length == 4);
54 ASSERT (ref64->length == 8);
55 ASSERT (ref128->length == 16);
57 umac32_set_key (&ctx32, key->data);
58 umac32_set_nonce (&ctx32, nonce->length, nonce->data);
60 update(&ctx32, (nettle_hash_update_func *) umac32_update, msg, length);
62 check_digest ("umac32", &ctx32, (nettle_hash_digest_func *) umac32_digest,
63 msg, length, 4, ref32->data);
65 umac64_set_key (&ctx64, key->data);
66 umac64_set_nonce (&ctx64, nonce->length, nonce->data);
68 update(&ctx64, (nettle_hash_update_func *) umac64_update, msg, length);
70 check_digest ("umac64", &ctx64, (nettle_hash_digest_func *) umac64_digest,
71 msg, length, 8, ref64->data);
73 umac96_set_key (&ctx96, key->data);
74 umac96_set_nonce (&ctx96, nonce->length, nonce->data);
76 update(&ctx96, (nettle_hash_update_func *) umac96_update, msg, length);
78 check_digest ("umac96", &ctx96, (nettle_hash_digest_func *) umac96_digest,
79 msg, length, 12, ref128->data);
81 umac128_set_key (&ctx128, key->data);
82 umac128_set_nonce (&ctx128, nonce->length, nonce->data);
84 update(&ctx128, (nettle_hash_update_func *) umac128_update, msg, length);
86 check_digest ("umac128", &ctx128, (nettle_hash_digest_func *) umac128_digest,
87 msg, length, 16, ref128->data);
90 static void
91 test_align(const struct tstring *key,
92 const struct tstring *nonce,
93 const struct tstring *msg,
94 unsigned length,
95 const struct tstring *ref32,
96 const struct tstring *ref64,
97 const struct tstring *ref128)
99 uint8_t *buffer = xalloc(length + 16);
100 unsigned offset;
101 for (offset = 0; offset < 16; offset++)
103 struct umac32_ctx ctx32;
104 struct umac64_ctx ctx64;
105 struct umac96_ctx ctx96;
106 struct umac128_ctx ctx128;
108 uint8_t *input;
109 unsigned i;
111 memset(buffer, 17, length + 16);
112 input = buffer + offset;
114 for (i = 0; i + msg->length < length; i += msg->length)
115 memcpy (input + i, msg->data, msg->length);
116 memcpy (input + i, msg->data, length - i);
118 umac32_set_key (&ctx32, key->data);
119 umac32_set_nonce (&ctx32, nonce->length, nonce->data);
121 umac32_update(&ctx32, length, input);
123 check_digest ("umac32 (alignment)",
124 &ctx32, (nettle_hash_digest_func *) umac32_digest,
125 msg, length, 4, ref32->data);
127 umac64_set_key (&ctx64, key->data);
128 umac64_set_nonce (&ctx64, nonce->length, nonce->data);
130 umac64_update(&ctx64, length, input);
132 check_digest ("umac64 (alignment)",
133 &ctx64, (nettle_hash_digest_func *) umac64_digest,
134 msg, length, 8, ref64->data);
136 umac96_set_key (&ctx96, key->data);
137 umac96_set_nonce (&ctx96, nonce->length, nonce->data);
139 umac96_update(&ctx96, length, input);
141 check_digest ("umac96 (alignment)",
142 &ctx96, (nettle_hash_digest_func *) umac96_digest,
143 msg, length, 12, ref128->data);
145 umac128_set_key (&ctx128, key->data);
146 umac128_set_nonce (&ctx128, nonce->length, nonce->data);
148 umac128_update(&ctx128, length, input);
150 check_digest ("umac128 (alignment)",
151 &ctx128, (nettle_hash_digest_func *) umac128_digest,
152 msg, length, 16, ref128->data);
154 free (buffer);
157 static void
158 test_incr (const struct tstring *key,
159 const struct tstring *nonce,
160 unsigned count,
161 const struct tstring *msg,
162 const struct tstring *ref32,
163 const struct tstring *ref64,
164 const struct tstring *ref128)
166 struct umac32_ctx ctx32;
167 struct umac64_ctx ctx64;
168 struct umac96_ctx ctx96;
169 struct umac128_ctx ctx128;
171 unsigned i;
173 ASSERT (key->length == UMAC_KEY_SIZE);
174 ASSERT (ref32->length == 4 * count);
175 ASSERT (ref64->length == 8 * count);
176 ASSERT (ref128->length == 16 * count);
177 umac32_set_key (&ctx32, key->data);
178 umac64_set_key (&ctx64, key->data);
179 umac96_set_key (&ctx96, key->data);
180 umac128_set_key (&ctx128, key->data);
181 if (nonce)
183 umac32_set_nonce (&ctx32, nonce->length, nonce->data);
184 umac64_set_nonce (&ctx64, nonce->length, nonce->data);
185 umac96_set_nonce (&ctx96, nonce->length, nonce->data);
186 umac128_set_nonce (&ctx128, nonce->length, nonce->data);
188 for (i = 0; i < count; i++)
190 umac32_update (&ctx32, msg->length, msg->data);
191 check_digest ("umac32 incr",
192 &ctx32, (nettle_hash_digest_func *) umac32_digest,
193 msg, i, 4, ref32->data + 4*i);
195 umac64_update (&ctx64, msg->length, msg->data);
196 check_digest ("umac64 incr",
197 &ctx64, (nettle_hash_digest_func *) umac64_digest,
198 msg, i, 8, ref64->data + 8*i);
200 umac96_update (&ctx96, msg->length, msg->data);
201 check_digest ("umac96 incr",
202 &ctx96, (nettle_hash_digest_func *) umac96_digest,
203 msg, i, 12, ref128->data + 16*i);
205 umac128_update (&ctx128, msg->length, msg->data);
206 check_digest ("umac128 incr",
207 &ctx128, (nettle_hash_digest_func *) umac128_digest,
208 msg, i, 16, ref128->data + 16*i);
213 void
214 test_main(void)
216 /* From RFC 4418 (except that it lacks the last 32 bits of 128-bit
217 tags) */
218 test_umac (SDATA("abcdefghijklmnop"), SDATA("bcdefghi"),
219 SDATA(""), 0,
220 SHEX("113145FB"),
221 SHEX("6E155FAD26900BE1"),
222 SHEX("32fedb100c79ad58f07ff7643cc60465"));
223 test_umac (SDATA("abcdefghijklmnop"), SDATA("bcdefghi"),
224 SDATA("a"), 3,
225 SHEX("3B91D102"),
226 SHEX("44B5CB542F220104"),
227 SHEX("185e4fe905cba7bd85e4c2dc3d117d8d"));
228 test_umac (SDATA("abcdefghijklmnop"), SDATA("bcdefghi"),
229 SDATA("a"), 1<<10,
230 SHEX("599B350B"),
231 SHEX("26BF2F5D60118BD9"),
232 SHEX("7a54abe04af82d60fb298c3cbd195bcb"));
234 test_umac (SDATA("abcdefghijklmnop"), SDATA("bcdefghi"),
235 SDATA("aaaaaaaa"), 1<<15,
236 SHEX("58DCF532"),
237 SHEX("27F8EF643B0D118D"),
238 SHEX("7b136bd911e4b734286ef2be501f2c3c"));
239 test_umac (SDATA("abcdefghijklmnop"), SDATA("bcdefghi"),
240 SDATA("aaaaaaaa"), 1<<20,
241 SHEX("DB6364D1"),
242 SHEX("A4477E87E9F55853"),
243 SHEX("f8acfa3ac31cfeea047f7b115b03bef5"));
244 /* Needs POLY128 */
245 /* For the 'a' * 2^25 testcase, see errata
246 http://fastcrypto.org/umac/rfc4418.errata.txt */
247 test_umac (SDATA("abcdefghijklmnop"), SDATA ("bcdefghi"),
248 SDATA ("aaaaaaaa"), 1<<25,
249 SHEX("85EE5CAE"),
250 SHEX("FACA46F856E9B45F"),
251 SHEX("a621c2457c0012e64f3fdae9e7e1870c"));
252 test_umac (SDATA("abcdefghijklmnop"), SDATA ("bcdefghi"),
253 SDATA ("abc"), 3,
254 SHEX("ABF3A3A0"),
255 SHEX("D4D7B9F6BD4FBFCF"),
256 SHEX("883c3d4b97a61976ffcf232308cba5a5"));
257 test_umac (SDATA("abcdefghijklmnop"), SDATA ("bcdefghi"),
258 SDATA ("abc"), 1500,
259 SHEX("ABEB3C8B"),
260 SHEX("D4CF26DDEFD5C01A"),
261 SHEX("8824a260c53c66a36c9260a62cb83aa1"));
263 test_incr (SDATA("abcdefghijklmnop"), NULL, 6,
264 SDATA("zero"),
265 SHEX("a0e94011 8c6fea51 6d897143 db1b28c5 a75e23b7 44ea26be"),
266 SHEX("a0e940111c9c2cd5 6d8971434be8ee41 c9c9aef87e2be502"
267 "a0a112b593656107 a75e23b7d419e03a 950526f26a8cc07a"),
268 SHEX("a0e940111c9c2cd5fa59090e3ac2061f"
269 "cbbf18b799fd0f4afb9216e52a89f247"
270 "c9c9aef87e2be50237716af8e24f8959"
271 "d6e96ef461f54d1c85aa66cbd76ca336"
272 "a75e23b7d419e03a02d55ebf1ba62824"
273 "2e63031d182a59b84f148d9a91de70a3"));
275 test_incr (SDATA("abcdefghijklmnop"), SDATA("a"), 5,
276 SDATA("nonce-a"),
277 SHEX("81b4ac24 b7e8aad0 f70246fe 0595f0bf a8e9fe85"),
278 SHEX("b7e8aad0da6e7f99 138814c6a03bdadf fb77dd1cd4c7074f"
279 "0595f0bf8585c7e2 817c0b7757cb60f7"),
280 SHEX("d7604bffb5e368da5fe564da0068d2cc"
281 "138814c6a03bdadff7f1666e1bd881aa"
282 "86a016d9e67957c8ab5ebb78a673e4e9"
283 "0595f0bf8585c7e28dfab00598d4e612"
284 "3266ec16a9d85b4f0dc74ec8272238a9"));
286 test_incr (SDATA("abcdefghijklmnop"), SHEX("beafcafe"), 5,
287 SDATA("nonce-beaf-cafe"),
288 SHEX("f19d9dc1 4604a56a 4ba9420e da86ff71 77facd79"),
289 SHEX("9e878413aa079032 9cfd7af0bb107748 4ba9420e55b6ba13"
290 "77facd797b686e24 9000c0de4f5f7236"),
291 SHEX("9e878413aa0790329604f3b6ae980e58"
292 "f2b2dd5dab08bb3bc5e9a83e1b4ab2e7"
293 "4ba9420e55b6ba137d03443f6ee01734"
294 "2721ca2e1bcda53a54ae65e0da139c0d"
295 "9000c0de4f5f7236b81ae1a52e78a821"));
297 /* Tests exercising various sizes of nonce and data: All nonce
298 lengths from 1 to 16 bytes. Data sizes chosen for testing for
299 various off-by-one errors,
301 0, 1, 2, 3, 4,
302 1020, 1021, 1022, 1023, 1024, 1025, 1026, 1027,
303 2046, 2047, 2048, 2049, 2050
304 16777212, 16777213, 16777214, 16777215, 16777216, 16777217,
305 16778239, 16778240, 16778241, 16778242, 16778243, 16778244
307 test_umac (SDATA("abcdefghijklmnop"), SDATA("b"),
308 SDATA("defdefdefdefdef"), 0,
309 SHEX("3a58486b"),
310 SHEX("9e38f67da91a08d9"),
311 SHEX("9e38f67da91a08d9c980f4db4089c877"));
312 test_umac (SDATA("abcdefghijklmnop"), SDATA("bc"),
313 SDATA("defdefdefdefdef"), 1,
314 SHEX("d86b1512"),
315 SHEX("fb0e207971b8e66a"),
316 SHEX("ef406c2ec70d0222f59e860eabb79ed0"));
317 test_umac (SDATA("abcdefghijklmnop"), SDATA("bcd"),
318 SDATA("defdefdefdefdef"), 2,
319 SHEX("1ae6e02d"),
320 SHEX("1ae6e02d73aa9ab2"),
321 SHEX("1ae6e02d73aa9ab2a27fb89e014dc07b"));
322 test_umac (SDATA("abcdefghijklmnop"), SDATA("bcde"),
323 SDATA("defdefdefdefdef"), 3,
324 SHEX("e8c1eb59"),
325 SHEX("c81cf22342e84302"),
326 SHEX("82626d0d575e01038e5e2cc6408216f5"));
327 test_umac (SDATA("abcdefghijklmnop"), SDATA("bcdef"),
328 SDATA("defdefdefdefdef"), 4,
329 SHEX("8950f0d3"),
330 SHEX("aba003e7bd673cc3"),
331 SHEX("aba003e7bd673cc368ba8513cecf2e7c"));
333 test_umac (SDATA("abcdefghijklmnop"), SDATA("bcdefg"),
334 SDATA("defdefdefdefdef"), 1020,
335 SHEX("7412167c"),
336 SHEX("f98828a161bb4ae3"),
337 SHEX("d8b4811f747d588d7a913360960de7cf"));
338 test_umac (SDATA("abcdefghijklmnop"), SDATA("bcdefgh"),
339 SDATA("defdefdefdefdef"), 1021,
340 SHEX("2d54936b"),
341 SHEX("2d54936be5bff72d"),
342 SHEX("2d54936be5bff72d2e1052361163b474"));
343 test_umac (SDATA("abcdefghijklmnop"), SDATA("bcdefghi"),
344 SDATA("defdefdefdefdef"), 1022,
345 SHEX("53ca8dd2"),
346 SHEX("2cee9784556387b3"),
347 SHEX("700513397f8a210a98938d3e7ac3bd88"));
348 test_umac (SDATA("abcdefghijklmnop"), SDATA("bcdefghij"),
349 SDATA("defdefdefdefdef"), 1023,
350 SHEX("26cc58df"),
351 SHEX("24ac4284ca371f42"),
352 SHEX("24ac4284ca371f4280f60bd274633d67"));
353 test_umac (SDATA("abcdefghijklmnop"), SDATA("bcdefghijk"),
354 SDATA("defdefdefdefdef"), 1024,
355 SHEX("3cada45a"),
356 SHEX("64c6a0fd14615a76"),
357 SHEX("abc223116cedd2db5af365e641a97539"));
358 test_umac (SDATA("abcdefghijklmnop"), SDATA("bcdefghijkl"),
359 SDATA("defdefdefdefdef"), 1025,
360 SHEX("93251e18"),
361 SHEX("93251e18e56bbdc4"),
362 SHEX("93251e18e56bbdc457de556f95c59931"));
363 test_umac (SDATA("abcdefghijklmnop"), SDATA("bcdefghijklm"),
364 SDATA("defdefdefdefdef"), 1026,
365 SHEX("24a4c3ab"),
366 SHEX("5d98bd8dfaf16352"),
367 SHEX("c1298672e52386753383a15ed58c0e42"));
368 test_umac (SDATA("abcdefghijklmnop"), SDATA("bcdefghijklmn"),
369 SDATA("defdefdefdefdef"), 1027,
370 SHEX("e7e98945"),
371 SHEX("5b0557c9fdcf661b"),
372 SHEX("5b0557c9fdcf661b1758efc603516ebe"));
374 /* Test varying the alignment of the buffer eventually passed to
375 _umac_nh and _umac_nh_n. */
376 test_align (SDATA("abcdefghijklmnop"), SDATA("bcdefghijk"),
377 SDATA("defdefdefdefdef"), 1024,
378 SHEX("3cada45a"),
379 SHEX("64c6a0fd14615a76"),
380 SHEX("abc223116cedd2db5af365e641a97539"));
382 test_umac (SDATA("abcdefghijklmnop"), SDATA("bcdefghijklmno"),
383 SDATA("defdefdefdefdef"), 2046,
384 SHEX("e12ddc9f"),
385 SHEX("65e85d47447c2277"),
386 SHEX("16bb5183017826ed47c9995c1e5834f3"));
387 test_umac (SDATA("abcdefghijklmnop"), SDATA("bcdefghijklmnop"),
388 SDATA("defdefdefdefdef"), 2047,
389 SHEX("34d723a6"),
390 SHEX("34d723a6cb1676d3"),
391 SHEX("34d723a6cb1676d3547a5064dc5b0a37"));
392 test_umac (SDATA("abcdefghijklmnop"), SDATA("bcdefghijklmnopq"),
393 SDATA("defdefdefdefdef"), 2048,
394 SHEX("21fd8802"),
395 SHEX("3968d5d0af147884"),
396 SHEX("84565620def1e3a614d274e87626f215"));
397 test_umac (SDATA("abcdefghijklmnop"), SDATA("b"),
398 SDATA("defdefdefdefdef"), 2049,
399 SHEX("097e5abd"),
400 SHEX("ad1ee4ab606061c5"),
401 SHEX("ad1ee4ab606061c55e0d2ecfee59940a"));
402 test_umac (SDATA("abcdefghijklmnop"), SDATA("bc"),
403 SDATA("defdefdefdefdef"), 2050,
404 SHEX("a03a7fe9"),
405 SHEX("835f4a8242100055"),
406 SHEX("971106d5f4a5e41dce40a91704cfe1f3"));
408 test_umac (SDATA("abcdefghijklmnop"), SDATA("bcd"),
409 SDATA("defdefdefdefdef"), 16777212,
410 SHEX("7ef41cf3"),
411 SHEX("7ef41cf351960aaf"),
412 SHEX("7ef41cf351960aaf729bb19fcee7d8c4"));
413 test_umac (SDATA("abcdefghijklmnop"), SDATA("bcde"),
414 SDATA("defdefdefdefdef"), 16777213,
415 SHEX("8bf81932"),
416 SHEX("ab250048807ff640"),
417 SHEX("e15b9f6695c9b441de035e9b10b8ac32"));
418 test_umac (SDATA("abcdefghijklmnop"), SDATA("bcdef"),
419 SDATA("defdefdefdefdef"), 16777214,
420 SHEX("ddb2f0ab"),
421 SHEX("ff42039fcfe1248e"),
422 SHEX("ff42039fcfe1248e36c19efed14d7140"));
423 test_umac (SDATA("abcdefghijklmnop"), SDATA("bcdefg"),
424 SDATA("defdefdefdefdef"), 16777215,
425 SHEX("e67ad507"),
426 SHEX("6be0ebda623d76df"),
427 SHEX("4adc426477fb64b1ce5afd76d505f048"));
428 test_umac (SDATA("abcdefghijklmnop"), SDATA("bcdefgh"),
429 SDATA("defdefdefdefdef"), 16777216,
430 SHEX("42d8562a"),
431 SHEX("42d8562a224a9e9a"),
432 SHEX("42d8562a224a9e9a75c2f85d39462d07"));
433 test_umac (SDATA("abcdefghijklmnop"), SDATA("bcdefghi"),
434 SDATA("defdefdefdefdef"), 16777217,
435 SHEX("486b138d"),
436 SHEX("374f09dbb0b84b88"),
437 SHEX("6ba48d669a51ed3195ebc2aa562ee71b"));
439 test_umac (SDATA("abcdefghijklmnop"), SDATA("bcdefghij"),
440 SDATA("defdefdefdefdef"), 16778239,
441 SHEX("850cb2c5"),
442 SHEX("876ca89ed045777b"),
443 SHEX("876ca89ed045777bf7efa7934e1758c2"));
444 test_umac (SDATA("abcdefghijklmnop"), SDATA("bcdefghijk"),
445 SDATA("defdefdefdefdef"), 16778240,
446 SHEX("b9fc4f81"),
447 SHEX("e1974b26fb35f2c6"),
448 SHEX("2e93c8ca83b97a6b1a21082e2a4c540d"));
449 test_umac (SDATA("abcdefghijklmnop"), SDATA("bcdefghijkl"),
450 SDATA("defdefdefdefdef"), 16778241,
451 SHEX("ffced8f2"),
452 SHEX("ffced8f2494d85bf"),
453 SHEX("ffced8f2494d85bf0cb39408ddfe0295"));
454 test_umac (SDATA("abcdefghijklmnop"), SDATA("bcdefghijklm"),
455 SDATA("defdefdefdefdef"), 16778242,
456 SHEX("1c99c5fb"),
457 SHEX("65a5bbdda3b85368"),
458 SHEX("f9148022bc6ab64f019e9db83704c17b"));
459 test_umac (SDATA("abcdefghijklmnop"), SDATA("bcdefghijklmn"),
460 SDATA("defdefdefdefdef"), 16778243,
461 SHEX("ec304be9"),
462 SHEX("50dc9565fbfc4884"),
463 SHEX(" 50dc9565fbfc48844a4be34403804605"));
464 test_umac (SDATA("abcdefghijklmnop"), SDATA("bcdefghijklmno"),
465 SDATA("defdefdefdefdef"), 16778244,
466 SHEX("8034e26f"),
467 SHEX("04f163b7c2d5d849"),
468 SHEX("77a26f7387d1dcd39378a3220652cff7"));