Debian Codegraph @

cipher/mac-hmac.c

raw
1 /* mac-hmac.c - HMAC glue for MAC API
2 * Copyright (C) 2013 Jussi Kivilinna <[email protected]>
3 * Copyright (C) 2008 Free Software Foundation, Inc.
4 *
5 * This file is part of Libgcrypt.
6 *
7 * Libgcrypt is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * Libgcrypt is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26
27 #include "g10lib.h"
28 #include "./mac-internal.h"
29 #include "bufhelp.h"
30 #include "cipher.h"
31
32
33 static int
34 map_mac_algo_to_md (int mac_algo)
35 {
36 switch (mac_algo)
37 {
38 default:
39 return GCRY_MD_NONE;
40 case GCRY_MAC_HMAC_MD2:
41 return GCRY_MD_MD2;
42 case GCRY_MAC_HMAC_MD4:
43 return GCRY_MD_MD4;
44 case GCRY_MAC_HMAC_MD5:
45 return GCRY_MD_MD5;
46 case GCRY_MAC_HMAC_SHA1:
47 return GCRY_MD_SHA1;
48 case GCRY_MAC_HMAC_SHA224:
49 return GCRY_MD_SHA224;
50 case GCRY_MAC_HMAC_SHA256:
51 return GCRY_MD_SHA256;
52 case GCRY_MAC_HMAC_SHA384:
53 return GCRY_MD_SHA384;
54 case GCRY_MAC_HMAC_SHA512:
55 return GCRY_MD_SHA512;
56 case GCRY_MAC_HMAC_SHA512_256:
57 return GCRY_MD_SHA512_256;
58 case GCRY_MAC_HMAC_SHA512_224:
59 return GCRY_MD_SHA512_224;
60 case GCRY_MAC_HMAC_SHA3_224:
61 return GCRY_MD_SHA3_224;
62 case GCRY_MAC_HMAC_SHA3_256:
63 return GCRY_MD_SHA3_256;
64 case GCRY_MAC_HMAC_SHA3_384:
65 return GCRY_MD_SHA3_384;
66 case GCRY_MAC_HMAC_SHA3_512:
67 return GCRY_MD_SHA3_512;
68 case GCRY_MAC_HMAC_RMD160:
69 return GCRY_MD_RMD160;
70 case GCRY_MAC_HMAC_TIGER1:
71 return GCRY_MD_TIGER1;
72 case GCRY_MAC_HMAC_WHIRLPOOL:
73 return GCRY_MD_WHIRLPOOL;
74 case GCRY_MAC_HMAC_GOSTR3411_94:
75 return GCRY_MD_GOSTR3411_94;
76 case GCRY_MAC_HMAC_GOSTR3411_CP:
77 return GCRY_MD_GOSTR3411_CP;
78 case GCRY_MAC_HMAC_STRIBOG256:
79 return GCRY_MD_STRIBOG256;
80 case GCRY_MAC_HMAC_STRIBOG512:
81 return GCRY_MD_STRIBOG512;
82 case GCRY_MAC_HMAC_BLAKE2B_512:
83 return GCRY_MD_BLAKE2B_512;
84 case GCRY_MAC_HMAC_BLAKE2B_384:
85 return GCRY_MD_BLAKE2B_384;
86 case GCRY_MAC_HMAC_BLAKE2B_256:
87 return GCRY_MD_BLAKE2B_256;
88 case GCRY_MAC_HMAC_BLAKE2B_160:
89 return GCRY_MD_BLAKE2B_160;
90 case GCRY_MAC_HMAC_BLAKE2S_256:
91 return GCRY_MD_BLAKE2S_256;
92 case GCRY_MAC_HMAC_BLAKE2S_224:
93 return GCRY_MD_BLAKE2S_224;
94 case GCRY_MAC_HMAC_BLAKE2S_160:
95 return GCRY_MD_BLAKE2S_160;
96 case GCRY_MAC_HMAC_BLAKE2S_128:
97 return GCRY_MD_BLAKE2S_128;
98 case GCRY_MAC_HMAC_SM3:
99 return GCRY_MD_SM3;
100 }
101 }
102
103
104 static gcry_err_code_t
105 hmac_open (gcry_mac_hd_t h)
106 {
107 gcry_err_code_t err;
108 gcry_md_hd_t hd;
109 int secure = (h->magic == CTX_MAC_MAGIC_SECURE);
110 unsigned int flags;
111 int md_algo;
112
113 md_algo = map_mac_algo_to_md (h->spec->algo);
114
115 flags = GCRY_MD_FLAG_HMAC;
116 flags |= (secure ? GCRY_MD_FLAG_SECURE : 0);
117
118 err = _gcry_md_open (&hd, md_algo, flags);
119 if (err)
120 return err;
121
122 h->u.hmac.md_algo = md_algo;
123 h->u.hmac.md_ctx = hd;
124 return 0;
125 }
126
127
128 static void
129 hmac_close (gcry_mac_hd_t h)
130 {
131 _gcry_md_close (h->u.hmac.md_ctx);
132 h->u.hmac.md_ctx = NULL;
133 }
134
135
136 static gcry_err_code_t
137 hmac_setkey (gcry_mac_hd_t h, const unsigned char *key, size_t keylen)
138 {
139 return _gcry_md_setkey (h->u.hmac.md_ctx, key, keylen);
140 }
141
142
143 static gcry_err_code_t
144 hmac_reset (gcry_mac_hd_t h)
145 {
146 _gcry_md_reset (h->u.hmac.md_ctx);
147 return 0;
148 }
149
150
151 static gcry_err_code_t
152 hmac_write (gcry_mac_hd_t h, const unsigned char *buf, size_t buflen)
153 {
154 _gcry_md_write (h->u.hmac.md_ctx, buf, buflen);
155 return 0;
156 }
157
158
159 static gcry_err_code_t
160 hmac_read (gcry_mac_hd_t h, unsigned char *outbuf, size_t * outlen)
161 {
162 unsigned int dlen;
163 const unsigned char *digest;
164
165 dlen = _gcry_md_get_algo_dlen (h->u.hmac.md_algo);
166 digest = _gcry_md_read (h->u.hmac.md_ctx, h->u.hmac.md_algo);
167
168 if (*outlen <= dlen)
169 buf_cpy (outbuf, digest, *outlen);
170 else
171 {
172 buf_cpy (outbuf, digest, dlen);
173 *outlen = dlen;
174 }
175
176 return 0;
177 }
178
179
180 static gcry_err_code_t
181 hmac_verify (gcry_mac_hd_t h, const unsigned char *buf, size_t buflen)
182 {
183 unsigned int dlen;
184 const unsigned char *digest;
185
186 dlen = _gcry_md_get_algo_dlen (h->u.hmac.md_algo);
187 digest = _gcry_md_read (h->u.hmac.md_ctx, h->u.hmac.md_algo);
188
189 if (buflen > dlen)
190 return GPG_ERR_INV_LENGTH;
191
192 return buf_eq_const (buf, digest, buflen) ? 0 : GPG_ERR_CHECKSUM;
193 }
194
195
196 static unsigned int
197 hmac_get_maclen (int algo)
198 {
199 return _gcry_md_get_algo_dlen (map_mac_algo_to_md (algo));
200 }
201
202
203 static unsigned int
204 hmac_get_keylen (int algo)
205 {
206 /* Return blocksize for default key length. */
207 switch (algo)
208 {
209 case GCRY_MD_SHA3_224:
210 return 1152 / 8;
211 case GCRY_MD_SHA3_256:
212 return 1088 / 8;
213 case GCRY_MD_SHA3_384:
214 return 832 / 8;
215 case GCRY_MD_SHA3_512:
216 return 576 / 8;
217 case GCRY_MAC_HMAC_SHA384:
218 case GCRY_MAC_HMAC_SHA512:
219 return 128;
220 case GCRY_MAC_HMAC_GOSTR3411_94:
221 return 32;
222 default:
223 return 64;
224 }
225 }
226
227
228 /* Check one HMAC with digest ALGO using the regualr HAMC
229 * API. (DATA,DATALEN) is the data to be MACed, (KEY,KEYLEN) the key
230 * and (EXPECT,EXPECTLEN) the expected result. If TRUNC is set, the
231 * EXPECTLEN may be less than the digest length. Returns NULL on
232 * success or a string describing the failure. */
233 static const char *
234 check_one (int algo,
235 const void *data, size_t datalen,
236 const void *key, size_t keylen,
237 const void *expect, size_t expectlen, int trunc)
238 {
239 gcry_md_hd_t hd;
240 const unsigned char *digest;
241
242 /* printf ("HMAC algo %d\n", algo); */
243
244 /* Skip test with shoter key in FIPS mode. */
245 if (fips_mode () && keylen < 14)
246 return NULL;
247
248 if (trunc)
249 {
250 if (_gcry_md_get_algo_dlen (algo) < expectlen)
251 return "invalid tests data";
252 }
253 else
254 {
255 if (_gcry_md_get_algo_dlen (algo) != expectlen)
256 return "invalid tests data";
257 }
258 if (_gcry_md_open (&hd, algo, GCRY_MD_FLAG_HMAC))
259 return "gcry_md_open failed";
260 if (_gcry_md_setkey (hd, key, keylen))
261 {
262 _gcry_md_close (hd);
263 return "gcry_md_setkey failed";
264 }
265 _gcry_md_write (hd, data, datalen);
266 digest = _gcry_md_read (hd, algo);
267 if (!digest)
268 {
269 _gcry_md_close (hd);
270 return "gcry_md_read failed";
271 }
272 if (memcmp (digest, expect, expectlen))
273 {
274 /* int i; */
275
276 /* fputs (" {", stdout); */
277 /* for (i=0; i < expectlen-1; i++) */
278 /* { */
279 /* if (i && !(i % 8)) */
280 /* fputs ("\n ", stdout); */
281 /* printf (" 0x%02x,", digest[i]); */
282 /* } */
283 /* printf (" 0x%02x } },\n", digest[i]); */
284
285 _gcry_md_close (hd);
286 return "does not match";
287 }
288 _gcry_md_close (hd);
289 return NULL;
290 }
291
292
293 static gpg_err_code_t
294 selftests_sha1 (int extended, selftest_report_func_t report)
295 {
296 const char *what;
297 const char *errtxt;
298 unsigned char key[128];
299 int i, j;
300
301 what = "FIPS-198a, A.1";
302 for (i=0; i < 64; i++)
303 key[i] = i;
304 errtxt = check_one (GCRY_MD_SHA1,
305 "Sample #1", 9,
306 key, 64,
307 "\x4f\x4c\xa3\xd5\xd6\x8b\xa7\xcc\x0a\x12"
308 "\x08\xc9\xc6\x1e\x9c\x5d\xa0\x40\x3c\x0a", 20, 0);
309 if (errtxt)
310 goto failed;
311
312 if (extended)
313 {
314 what = "FIPS-198a, A.2";
315 for (i=0, j=0x30; i < 20; i++)
316 key[i] = j++;
317 errtxt = check_one (GCRY_MD_SHA1,
318 "Sample #2", 9,
319 key, 20,
320 "\x09\x22\xd3\x40\x5f\xaa\x3d\x19\x4f\x82"
321 "\xa4\x58\x30\x73\x7d\x5c\xc6\xc7\x5d\x24", 20, 0);
322 if (errtxt)
323 goto failed;
324
325 what = "FIPS-198a, A.3";
326 for (i=0, j=0x50; i < 100; i++)
327 key[i] = j++;
328 errtxt = check_one (GCRY_MD_SHA1,
329 "Sample #3", 9,
330 key, 100,
331 "\xbc\xf4\x1e\xab\x8b\xb2\xd8\x02\xf3\xd0"
332 "\x5c\xaf\x7c\xb0\x92\xec\xf8\xd1\xa3\xaa", 20, 0);
333 if (errtxt)
334 goto failed;
335
336 what = "FIPS-198a, A.4";
337 for (i=0, j=0x70; i < 49; i++)
338 key[i] = j++;
339 errtxt = check_one (GCRY_MD_SHA1,
340 "Sample #4", 9,
341 key, 49,
342 "\x9e\xa8\x86\xef\xe2\x68\xdb\xec\xce\x42"
343 "\x0c\x75\x24\xdf\x32\xe0\x75\x1a\x2a\x26", 20, 0);
344 if (errtxt)
345 goto failed;
346 }
347
348 return 0; /* Succeeded. */
349
350 failed:
351 if (report)
352 report ("hmac", GCRY_MD_SHA1, what, errtxt);
353 return GPG_ERR_SELFTEST_FAILED;
354 }
355
356
357
358 static gpg_err_code_t
359 selftests_sha224 (int extended, selftest_report_func_t report)
360 {
361 static struct
362 {
363 const char * const desc;
364 const char * const data;
365 const char * const key;
366 const char expect[28];
367 } tv[] =
368 {
369 { "data-28 key-4",
370 "what do ya want for nothing?",
371 "Jefe",
372 { 0xa3, 0x0e, 0x01, 0x09, 0x8b, 0xc6, 0xdb, 0xbf,
373 0x45, 0x69, 0x0f, 0x3a, 0x7e, 0x9e, 0x6d, 0x0f,
374 0x8b, 0xbe, 0xa2, 0xa3, 0x9e, 0x61, 0x48, 0x00,
375 0x8f, 0xd0, 0x5e, 0x44 } },
376
377 { "data-9 key-20",
378 "Hi There",
379 "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
380 "\x0b\x0b\x0b\x0b",
381 { 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19,
382 0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f,
383 0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f,
384 0x53, 0x68, 0x4b, 0x22 } },
385
386 { "data-50 key-20",
387 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
388 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
389 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
390 "\xdd\xdd",
391 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
392 "\xaa\xaa\xaa\xaa",
393 { 0x7f, 0xb3, 0xcb, 0x35, 0x88, 0xc6, 0xc1, 0xf6,
394 0xff, 0xa9, 0x69, 0x4d, 0x7d, 0x6a, 0xd2, 0x64,
395 0x93, 0x65, 0xb0, 0xc1, 0xf6, 0x5d, 0x69, 0xd1,
396 0xec, 0x83, 0x33, 0xea } },
397
398 { "data-50 key-26",
399 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
400 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
401 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
402 "\xcd\xcd",
403 "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10"
404 "\x11\x12\x13\x14\x15\x16\x17\x18\x19",
405 { 0x6c, 0x11, 0x50, 0x68, 0x74, 0x01, 0x3c, 0xac,
406 0x6a, 0x2a, 0xbc, 0x1b, 0xb3, 0x82, 0x62, 0x7c,
407 0xec, 0x6a, 0x90, 0xd8, 0x6e, 0xfc, 0x01, 0x2d,
408 0xe7, 0xaf, 0xec, 0x5a } },
409
410 { "data-54 key-131",
411 "Test Using Larger Than Block-Size Key - Hash Key First",
412 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
413 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
414 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
415 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
416 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
417 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
418 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
419 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
420 "\xaa\xaa\xaa",
421 { 0x95, 0xe9, 0xa0, 0xdb, 0x96, 0x20, 0x95, 0xad,
422 0xae, 0xbe, 0x9b, 0x2d, 0x6f, 0x0d, 0xbc, 0xe2,
423 0xd4, 0x99, 0xf1, 0x12, 0xf2, 0xd2, 0xb7, 0x27,
424 0x3f, 0xa6, 0x87, 0x0e } },
425
426 { "data-152 key-131",
427 "This is a test using a larger than block-size key and a larger "
428 "than block-size data. The key needs to be hashed before being "
429 "used by the HMAC algorithm.",
430 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
431 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
432 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
433 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
434 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
435 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
436 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
437 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
438 "\xaa\xaa\xaa",
439 { 0x3a, 0x85, 0x41, 0x66, 0xac, 0x5d, 0x9f, 0x02,
440 0x3f, 0x54, 0xd5, 0x17, 0xd0, 0xb3, 0x9d, 0xbd,
441 0x94, 0x67, 0x70, 0xdb, 0x9c, 0x2b, 0x95, 0xc9,
442 0xf6, 0xf5, 0x65, 0xd1 } },
443
444 { NULL }
445 };
446 const char *what;
447 const char *errtxt;
448 int tvidx;
449
450 for (tvidx=0; tv[tvidx].desc; tvidx++)
451 {
452 what = tv[tvidx].desc;
453 errtxt = check_one (GCRY_MD_SHA224,
454 tv[tvidx].data, strlen (tv[tvidx].data),
455 tv[tvidx].key, strlen (tv[tvidx].key),
456 tv[tvidx].expect, DIM (tv[tvidx].expect), 0);
457 if (errtxt)
458 goto failed;
459 if (!extended)
460 break;
461 }
462
463 return 0; /* Succeeded. */
464
465 failed:
466 if (report)
467 report ("hmac", GCRY_MD_SHA224, what, errtxt);
468 return GPG_ERR_SELFTEST_FAILED;
469 }
470
471
472 static gpg_err_code_t
473 selftests_sha256 (int extended, selftest_report_func_t report)
474 {
475 static struct
476 {
477 const char * const desc;
478 const char * const data;
479 const char * const key;
480 const char expect[32];
481 } tv[] =
482 {
483 { "data-28 key-4",
484 "what do ya want for nothing?",
485 "Jefe",
486 { 0x5b, 0xdc, 0xc1, 0x46, 0xbf, 0x60, 0x75, 0x4e,
487 0x6a, 0x04, 0x24, 0x26, 0x08, 0x95, 0x75, 0xc7,
488 0x5a, 0x00, 0x3f, 0x08, 0x9d, 0x27, 0x39, 0x83,
489 0x9d, 0xec, 0x58, 0xb9, 0x64, 0xec, 0x38, 0x43 } },
490
491 { "data-9 key-20",
492 "Hi There",
493 "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
494 "\x0b\x0b\x0b\x0b",
495 { 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
496 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
497 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
498 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7 } },
499
500 { "data-50 key-20",
501 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
502 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
503 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
504 "\xdd\xdd",
505 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
506 "\xaa\xaa\xaa\xaa",
507 { 0x77, 0x3e, 0xa9, 0x1e, 0x36, 0x80, 0x0e, 0x46,
508 0x85, 0x4d, 0xb8, 0xeb, 0xd0, 0x91, 0x81, 0xa7,
509 0x29, 0x59, 0x09, 0x8b, 0x3e, 0xf8, 0xc1, 0x22,
510 0xd9, 0x63, 0x55, 0x14, 0xce, 0xd5, 0x65, 0xfe } },
511
512 { "data-50 key-26",
513 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
514 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
515 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
516 "\xcd\xcd",
517 "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10"
518 "\x11\x12\x13\x14\x15\x16\x17\x18\x19",
519 { 0x82, 0x55, 0x8a, 0x38, 0x9a, 0x44, 0x3c, 0x0e,
520 0xa4, 0xcc, 0x81, 0x98, 0x99, 0xf2, 0x08, 0x3a,
521 0x85, 0xf0, 0xfa, 0xa3, 0xe5, 0x78, 0xf8, 0x07,
522 0x7a, 0x2e, 0x3f, 0xf4, 0x67, 0x29, 0x66, 0x5b } },
523
524 { "data-54 key-131",
525 "Test Using Larger Than Block-Size Key - Hash Key First",
526 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
527 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
528 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
529 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
530 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
531 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
532 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
533 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
534 "\xaa\xaa\xaa",
535 { 0x60, 0xe4, 0x31, 0x59, 0x1e, 0xe0, 0xb6, 0x7f,
536 0x0d, 0x8a, 0x26, 0xaa, 0xcb, 0xf5, 0xb7, 0x7f,
537 0x8e, 0x0b, 0xc6, 0x21, 0x37, 0x28, 0xc5, 0x14,
538 0x05, 0x46, 0x04, 0x0f, 0x0e, 0xe3, 0x7f, 0x54 } },
539
540 { "data-152 key-131",
541 "This is a test using a larger than block-size key and a larger "
542 "than block-size data. The key needs to be hashed before being "
543 "used by the HMAC algorithm.",
544 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
545 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
546 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
547 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
548 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
549 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
550 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
551 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
552 "\xaa\xaa\xaa",
553 { 0x9b, 0x09, 0xff, 0xa7, 0x1b, 0x94, 0x2f, 0xcb,
554 0x27, 0x63, 0x5f, 0xbc, 0xd5, 0xb0, 0xe9, 0x44,
555 0xbf, 0xdc, 0x63, 0x64, 0x4f, 0x07, 0x13, 0x93,
556 0x8a, 0x7f, 0x51, 0x53, 0x5c, 0x3a, 0x35, 0xe2 } },
557
558 { NULL }
559 };
560 const char *what;
561 const char *errtxt;
562 int tvidx;
563
564 for (tvidx=0; tv[tvidx].desc; tvidx++)
565 {
566 what = tv[tvidx].desc;
567 errtxt = check_one (GCRY_MD_SHA256,
568 tv[tvidx].data, strlen (tv[tvidx].data),
569 tv[tvidx].key, strlen (tv[tvidx].key),
570 tv[tvidx].expect, DIM (tv[tvidx].expect), 0);
571 if (errtxt)
572 goto failed;
573 if (!extended)
574 break;
575 }
576
577 return 0; /* Succeeded. */
578
579 failed:
580 if (report)
581 report ("hmac", GCRY_MD_SHA256, what, errtxt);
582 return GPG_ERR_SELFTEST_FAILED;
583 }
584
585
586 static gpg_err_code_t
587 selftests_sha384 (int extended, selftest_report_func_t report)
588 {
589 static struct
590 {
591 const char * const desc;
592 const char * const data;
593 const char * const key;
594 const char expect[48];
595 } tv[] =
596 {
597 { "data-28 key-4",
598 "what do ya want for nothing?",
599 "Jefe",
600 { 0xaf, 0x45, 0xd2, 0xe3, 0x76, 0x48, 0x40, 0x31,
601 0x61, 0x7f, 0x78, 0xd2, 0xb5, 0x8a, 0x6b, 0x1b,
602 0x9c, 0x7e, 0xf4, 0x64, 0xf5, 0xa0, 0x1b, 0x47,
603 0xe4, 0x2e, 0xc3, 0x73, 0x63, 0x22, 0x44, 0x5e,
604 0x8e, 0x22, 0x40, 0xca, 0x5e, 0x69, 0xe2, 0xc7,
605 0x8b, 0x32, 0x39, 0xec, 0xfa, 0xb2, 0x16, 0x49 } },
606
607 { "data-9 key-20",
608 "Hi There",
609 "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
610 "\x0b\x0b\x0b\x0b",
611 { 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62,
612 0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f,
613 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6,
614 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c,
615 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f,
616 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6 } },
617
618 { "data-50 key-20",
619 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
620 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
621 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
622 "\xdd\xdd",
623 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
624 "\xaa\xaa\xaa\xaa",
625 { 0x88, 0x06, 0x26, 0x08, 0xd3, 0xe6, 0xad, 0x8a,
626 0x0a, 0xa2, 0xac, 0xe0, 0x14, 0xc8, 0xa8, 0x6f,
627 0x0a, 0xa6, 0x35, 0xd9, 0x47, 0xac, 0x9f, 0xeb,
628 0xe8, 0x3e, 0xf4, 0xe5, 0x59, 0x66, 0x14, 0x4b,
629 0x2a, 0x5a, 0xb3, 0x9d, 0xc1, 0x38, 0x14, 0xb9,
630 0x4e, 0x3a, 0xb6, 0xe1, 0x01, 0xa3, 0x4f, 0x27 } },
631
632 { "data-50 key-26",
633 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
634 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
635 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
636 "\xcd\xcd",
637 "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10"
638 "\x11\x12\x13\x14\x15\x16\x17\x18\x19",
639 { 0x3e, 0x8a, 0x69, 0xb7, 0x78, 0x3c, 0x25, 0x85,
640 0x19, 0x33, 0xab, 0x62, 0x90, 0xaf, 0x6c, 0xa7,
641 0x7a, 0x99, 0x81, 0x48, 0x08, 0x50, 0x00, 0x9c,
642 0xc5, 0x57, 0x7c, 0x6e, 0x1f, 0x57, 0x3b, 0x4e,
643 0x68, 0x01, 0xdd, 0x23, 0xc4, 0xa7, 0xd6, 0x79,
644 0xcc, 0xf8, 0xa3, 0x86, 0xc6, 0x74, 0xcf, 0xfb } },
645
646 { "data-54 key-131",
647 "Test Using Larger Than Block-Size Key - Hash Key First",
648 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
649 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
650 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
651 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
652 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
653 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
654 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
655 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
656 "\xaa\xaa\xaa",
657 { 0x4e, 0xce, 0x08, 0x44, 0x85, 0x81, 0x3e, 0x90,
658 0x88, 0xd2, 0xc6, 0x3a, 0x04, 0x1b, 0xc5, 0xb4,
659 0x4f, 0x9e, 0xf1, 0x01, 0x2a, 0x2b, 0x58, 0x8f,
660 0x3c, 0xd1, 0x1f, 0x05, 0x03, 0x3a, 0xc4, 0xc6,
661 0x0c, 0x2e, 0xf6, 0xab, 0x40, 0x30, 0xfe, 0x82,
662 0x96, 0x24, 0x8d, 0xf1, 0x63, 0xf4, 0x49, 0x52 } },
663
664 { "data-152 key-131",
665 "This is a test using a larger than block-size key and a larger "
666 "than block-size data. The key needs to be hashed before being "
667 "used by the HMAC algorithm.",
668 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
669 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
670 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
671 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
672 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
673 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
674 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
675 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
676 "\xaa\xaa\xaa",
677 { 0x66, 0x17, 0x17, 0x8e, 0x94, 0x1f, 0x02, 0x0d,
678 0x35, 0x1e, 0x2f, 0x25, 0x4e, 0x8f, 0xd3, 0x2c,
679 0x60, 0x24, 0x20, 0xfe, 0xb0, 0xb8, 0xfb, 0x9a,
680 0xdc, 0xce, 0xbb, 0x82, 0x46, 0x1e, 0x99, 0xc5,
681 0xa6, 0x78, 0xcc, 0x31, 0xe7, 0x99, 0x17, 0x6d,
682 0x38, 0x60, 0xe6, 0x11, 0x0c, 0x46, 0x52, 0x3e } },
683
684 { NULL }
685 };
686 const char *what;
687 const char *errtxt;
688 int tvidx;
689
690 for (tvidx=0; tv[tvidx].desc; tvidx++)
691 {
692 what = tv[tvidx].desc;
693 errtxt = check_one (GCRY_MD_SHA384,
694 tv[tvidx].data, strlen (tv[tvidx].data),
695 tv[tvidx].key, strlen (tv[tvidx].key),
696 tv[tvidx].expect, DIM (tv[tvidx].expect), 0);
697 if (errtxt)
698 goto failed;
699 if (!extended)
700 break;
701 }
702
703 return 0; /* Succeeded. */
704
705 failed:
706 if (report)
707 report ("hmac", GCRY_MD_SHA384, what, errtxt);
708 return GPG_ERR_SELFTEST_FAILED;
709 }
710
711
712 static gpg_err_code_t
713 selftests_sha512 (int extended, selftest_report_func_t report)
714 {
715 static struct
716 {
717 const char * const desc;
718 const char * const data;
719 const char * const key;
720 const char expect[64];
721 } tv[] =
722 {
723 { "data-28 key-4",
724 "what do ya want for nothing?",
725 "Jefe",
726 { 0x16, 0x4b, 0x7a, 0x7b, 0xfc, 0xf8, 0x19, 0xe2,
727 0xe3, 0x95, 0xfb, 0xe7, 0x3b, 0x56, 0xe0, 0xa3,
728 0x87, 0xbd, 0x64, 0x22, 0x2e, 0x83, 0x1f, 0xd6,
729 0x10, 0x27, 0x0c, 0xd7, 0xea, 0x25, 0x05, 0x54,
730 0x97, 0x58, 0xbf, 0x75, 0xc0, 0x5a, 0x99, 0x4a,
731 0x6d, 0x03, 0x4f, 0x65, 0xf8, 0xf0, 0xe6, 0xfd,
732 0xca, 0xea, 0xb1, 0xa3, 0x4d, 0x4a, 0x6b, 0x4b,
733 0x63, 0x6e, 0x07, 0x0a, 0x38, 0xbc, 0xe7, 0x37 } },
734
735 { "data-9 key-20",
736 "Hi There",
737 "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
738 "\x0b\x0b\x0b\x0b",
739 { 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d,
740 0x4f, 0xf0, 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0,
741 0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78,
742 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, 0x7c, 0xde,
743 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
744 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4,
745 0xbe, 0x9d, 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70,
746 0x2e, 0x69, 0x6c, 0x20, 0x3a, 0x12, 0x68, 0x54 } },
747
748 { "data-50 key-20",
749 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
750 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
751 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
752 "\xdd\xdd",
753 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
754 "\xaa\xaa\xaa\xaa",
755 { 0xfa, 0x73, 0xb0, 0x08, 0x9d, 0x56, 0xa2, 0x84,
756 0xef, 0xb0, 0xf0, 0x75, 0x6c, 0x89, 0x0b, 0xe9,
757 0xb1, 0xb5, 0xdb, 0xdd, 0x8e, 0xe8, 0x1a, 0x36,
758 0x55, 0xf8, 0x3e, 0x33, 0xb2, 0x27, 0x9d, 0x39,
759 0xbf, 0x3e, 0x84, 0x82, 0x79, 0xa7, 0x22, 0xc8,
760 0x06, 0xb4, 0x85, 0xa4, 0x7e, 0x67, 0xc8, 0x07,
761 0xb9, 0x46, 0xa3, 0x37, 0xbe, 0xe8, 0x94, 0x26,
762 0x74, 0x27, 0x88, 0x59, 0xe1, 0x32, 0x92, 0xfb } },
763
764 { "data-50 key-26",
765 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
766 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
767 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
768 "\xcd\xcd",
769 "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10"
770 "\x11\x12\x13\x14\x15\x16\x17\x18\x19",
771 { 0xb0, 0xba, 0x46, 0x56, 0x37, 0x45, 0x8c, 0x69,
772 0x90, 0xe5, 0xa8, 0xc5, 0xf6, 0x1d, 0x4a, 0xf7,
773 0xe5, 0x76, 0xd9, 0x7f, 0xf9, 0x4b, 0x87, 0x2d,
774 0xe7, 0x6f, 0x80, 0x50, 0x36, 0x1e, 0xe3, 0xdb,
775 0xa9, 0x1c, 0xa5, 0xc1, 0x1a, 0xa2, 0x5e, 0xb4,
776 0xd6, 0x79, 0x27, 0x5c, 0xc5, 0x78, 0x80, 0x63,
777 0xa5, 0xf1, 0x97, 0x41, 0x12, 0x0c, 0x4f, 0x2d,
778 0xe2, 0xad, 0xeb, 0xeb, 0x10, 0xa2, 0x98, 0xdd } },
779
780 { "data-54 key-131",
781 "Test Using Larger Than Block-Size Key - Hash Key First",
782 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
783 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
784 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
785 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
786 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
787 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
788 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
789 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
790 "\xaa\xaa\xaa",
791 { 0x80, 0xb2, 0x42, 0x63, 0xc7, 0xc1, 0xa3, 0xeb,
792 0xb7, 0x14, 0x93, 0xc1, 0xdd, 0x7b, 0xe8, 0xb4,
793 0x9b, 0x46, 0xd1, 0xf4, 0x1b, 0x4a, 0xee, 0xc1,
794 0x12, 0x1b, 0x01, 0x37, 0x83, 0xf8, 0xf3, 0x52,
795 0x6b, 0x56, 0xd0, 0x37, 0xe0, 0x5f, 0x25, 0x98,
796 0xbd, 0x0f, 0xd2, 0x21, 0x5d, 0x6a, 0x1e, 0x52,
797 0x95, 0xe6, 0x4f, 0x73, 0xf6, 0x3f, 0x0a, 0xec,
798 0x8b, 0x91, 0x5a, 0x98, 0x5d, 0x78, 0x65, 0x98 } },
799
800 { "data-152 key-131",
801 "This is a test using a larger than block-size key and a larger "
802 "than block-size data. The key needs to be hashed before being "
803 "used by the HMAC algorithm.",
804 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
805 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
806 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
807 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
808 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
809 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
810 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
811 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
812 "\xaa\xaa\xaa",
813 { 0xe3, 0x7b, 0x6a, 0x77, 0x5d, 0xc8, 0x7d, 0xba,
814 0xa4, 0xdf, 0xa9, 0xf9, 0x6e, 0x5e, 0x3f, 0xfd,
815 0xde, 0xbd, 0x71, 0xf8, 0x86, 0x72, 0x89, 0x86,
816 0x5d, 0xf5, 0xa3, 0x2d, 0x20, 0xcd, 0xc9, 0x44,
817 0xb6, 0x02, 0x2c, 0xac, 0x3c, 0x49, 0x82, 0xb1,
818 0x0d, 0x5e, 0xeb, 0x55, 0xc3, 0xe4, 0xde, 0x15,
819 0x13, 0x46, 0x76, 0xfb, 0x6d, 0xe0, 0x44, 0x60,
820 0x65, 0xc9, 0x74, 0x40, 0xfa, 0x8c, 0x6a, 0x58 } },
821
822 { NULL }
823 };
824 const char *what;
825 const char *errtxt;
826 int tvidx;
827
828 for (tvidx=0; tv[tvidx].desc; tvidx++)
829 {
830 what = tv[tvidx].desc;
831 errtxt = check_one (GCRY_MD_SHA512,
832 tv[tvidx].data, strlen (tv[tvidx].data),
833 tv[tvidx].key, strlen (tv[tvidx].key),
834 tv[tvidx].expect, DIM (tv[tvidx].expect), 0);
835 if (errtxt)
836 goto failed;
837 if (!extended)
838 break;
839 }
840
841 return 0; /* Succeeded. */
842
843 failed:
844 if (report)
845 report ("hmac", GCRY_MD_SHA512, what, errtxt);
846 return GPG_ERR_SELFTEST_FAILED;
847 }
848
849
850
851 /* Test for the SHA3 algorithms. Vectors taken on 2017-07-18 from
852 * http://www.wolfgang-ehrhardt.de/hmac-sha3-testvectors.html */
853 static gpg_err_code_t
854 selftests_sha3 (int hashalgo, int extended, selftest_report_func_t report)
855 {
856 static struct
857 {
858 const char * const desc;
859 const char * const data;
860 const char * const key;
861 const char expect_224[28];
862 const char expect_256[32];
863 const char expect_384[48];
864 const char expect_512[64];
865 unsigned char trunc;
866 } tv[] =
867 {
868 { "data-9 key-20", /* Test 1 */
869 "Hi There",
870 "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
871 "\x0b\x0b\x0b\x0b",
872
873 { 0x3b, 0x16, 0x54, 0x6b, 0xbc, 0x7b, 0xe2, 0x70,
874 0x6a, 0x03, 0x1d, 0xca, 0xfd, 0x56, 0x37, 0x3d,
875 0x98, 0x84, 0x36, 0x76, 0x41, 0xd8, 0xc5, 0x9a,
876 0xf3, 0xc8, 0x60, 0xf7 },
877 { 0xba, 0x85, 0x19, 0x23, 0x10, 0xdf, 0xfa, 0x96,
878 0xe2, 0xa3, 0xa4, 0x0e, 0x69, 0x77, 0x43, 0x51,
879 0x14, 0x0b, 0xb7, 0x18, 0x5e, 0x12, 0x02, 0xcd,
880 0xcc, 0x91, 0x75, 0x89, 0xf9, 0x5e, 0x16, 0xbb },
881 { 0x68, 0xd2, 0xdc, 0xf7, 0xfd, 0x4d, 0xdd, 0x0a,
882 0x22, 0x40, 0xc8, 0xa4, 0x37, 0x30, 0x5f, 0x61,
883 0xfb, 0x73, 0x34, 0xcf, 0xb5, 0xd0, 0x22, 0x6e,
884 0x1b, 0xc2, 0x7d, 0xc1, 0x0a, 0x2e, 0x72, 0x3a,
885 0x20, 0xd3, 0x70, 0xb4, 0x77, 0x43, 0x13, 0x0e,
886 0x26, 0xac, 0x7e, 0x3d, 0x53, 0x28, 0x86, 0xbd },
887 { 0xeb, 0x3f, 0xbd, 0x4b, 0x2e, 0xaa, 0xb8, 0xf5,
888 0xc5, 0x04, 0xbd, 0x3a, 0x41, 0x46, 0x5a, 0xac,
889 0xec, 0x15, 0x77, 0x0a, 0x7c, 0xab, 0xac, 0x53,
890 0x1e, 0x48, 0x2f, 0x86, 0x0b, 0x5e, 0xc7, 0xba,
891 0x47, 0xcc, 0xb2, 0xc6, 0xf2, 0xaf, 0xce, 0x8f,
892 0x88, 0xd2, 0x2b, 0x6d, 0xc6, 0x13, 0x80, 0xf2,
893 0x3a, 0x66, 0x8f, 0xd3, 0x88, 0x8b, 0xb8, 0x05,
894 0x37, 0xc0, 0xa0, 0xb8, 0x64, 0x07, 0x68, 0x9e }
895 },
896
897 { "data-28 key-4", /* Test 2 */
898 /* Test with a key shorter than the length of the HMAC output. */
899 "what do ya want for nothing?",
900 "Jefe",
901
902 { 0x7f, 0xdb, 0x8d, 0xd8, 0x8b, 0xd2, 0xf6, 0x0d,
903 0x1b, 0x79, 0x86, 0x34, 0xad, 0x38, 0x68, 0x11,
904 0xc2, 0xcf, 0xc8, 0x5b, 0xfa, 0xf5, 0xd5, 0x2b,
905 0xba, 0xce, 0x5e, 0x66 },
906 { 0xc7, 0xd4, 0x07, 0x2e, 0x78, 0x88, 0x77, 0xae,
907 0x35, 0x96, 0xbb, 0xb0, 0xda, 0x73, 0xb8, 0x87,
908 0xc9, 0x17, 0x1f, 0x93, 0x09, 0x5b, 0x29, 0x4a,
909 0xe8, 0x57, 0xfb, 0xe2, 0x64, 0x5e, 0x1b, 0xa5 },
910 { 0xf1, 0x10, 0x1f, 0x8c, 0xbf, 0x97, 0x66, 0xfd,
911 0x67, 0x64, 0xd2, 0xed, 0x61, 0x90, 0x3f, 0x21,
912 0xca, 0x9b, 0x18, 0xf5, 0x7c, 0xf3, 0xe1, 0xa2,
913 0x3c, 0xa1, 0x35, 0x08, 0xa9, 0x32, 0x43, 0xce,
914 0x48, 0xc0, 0x45, 0xdc, 0x00, 0x7f, 0x26, 0xa2,
915 0x1b, 0x3f, 0x5e, 0x0e, 0x9d, 0xf4, 0xc2, 0x0a },
916 { 0x5a, 0x4b, 0xfe, 0xab, 0x61, 0x66, 0x42, 0x7c,
917 0x7a, 0x36, 0x47, 0xb7, 0x47, 0x29, 0x2b, 0x83,
918 0x84, 0x53, 0x7c, 0xdb, 0x89, 0xaf, 0xb3, 0xbf,
919 0x56, 0x65, 0xe4, 0xc5, 0xe7, 0x09, 0x35, 0x0b,
920 0x28, 0x7b, 0xae, 0xc9, 0x21, 0xfd, 0x7c, 0xa0,
921 0xee, 0x7a, 0x0c, 0x31, 0xd0, 0x22, 0xa9, 0x5e,
922 0x1f, 0xc9, 0x2b, 0xa9, 0xd7, 0x7d, 0xf8, 0x83,
923 0x96, 0x02, 0x75, 0xbe, 0xb4, 0xe6, 0x20, 0x24 }
924 },
925
926 { "data-50 key-20", /* Test 3 */
927 /* Test with a combined length of key and data that is larger
928 * than 64 bytes (= block-size of SHA-224 and SHA-256). */
929 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
930 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
931 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
932 "\xdd\xdd",
933 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
934 "\xaa\xaa\xaa\xaa",
935
936 { 0x67, 0x6c, 0xfc, 0x7d, 0x16, 0x15, 0x36, 0x38,
937 0x78, 0x03, 0x90, 0x69, 0x2b, 0xe1, 0x42, 0xd2,
938 0xdf, 0x7c, 0xe9, 0x24, 0xb9, 0x09, 0xc0, 0xc0,
939 0x8d, 0xbf, 0xdc, 0x1a },
940 { 0x84, 0xec, 0x79, 0x12, 0x4a, 0x27, 0x10, 0x78,
941 0x65, 0xce, 0xdd, 0x8b, 0xd8, 0x2d, 0xa9, 0x96,
942 0x5e, 0x5e, 0xd8, 0xc3, 0x7b, 0x0a, 0xc9, 0x80,
943 0x05, 0xa7, 0xf3, 0x9e, 0xd5, 0x8a, 0x42, 0x07 },
944 { 0x27, 0x5c, 0xd0, 0xe6, 0x61, 0xbb, 0x8b, 0x15,
945 0x1c, 0x64, 0xd2, 0x88, 0xf1, 0xf7, 0x82, 0xfb,
946 0x91, 0xa8, 0xab, 0xd5, 0x68, 0x58, 0xd7, 0x2b,
947 0xab, 0xb2, 0xd4, 0x76, 0xf0, 0x45, 0x83, 0x73,
948 0xb4, 0x1b, 0x6a, 0xb5, 0xbf, 0x17, 0x4b, 0xec,
949 0x42, 0x2e, 0x53, 0xfc, 0x31, 0x35, 0xac, 0x6e },
950 { 0x30, 0x9e, 0x99, 0xf9, 0xec, 0x07, 0x5e, 0xc6,
951 0xc6, 0xd4, 0x75, 0xed, 0xa1, 0x18, 0x06, 0x87,
952 0xfc, 0xf1, 0x53, 0x11, 0x95, 0x80, 0x2a, 0x99,
953 0xb5, 0x67, 0x74, 0x49, 0xa8, 0x62, 0x51, 0x82,
954 0x85, 0x1c, 0xb3, 0x32, 0xaf, 0xb6, 0xa8, 0x9c,
955 0x41, 0x13, 0x25, 0xfb, 0xcb, 0xcd, 0x42, 0xaf,
956 0xcb, 0x7b, 0x6e, 0x5a, 0xab, 0x7e, 0xa4, 0x2c,
957 0x66, 0x0f, 0x97, 0xfd, 0x85, 0x84, 0xbf, 0x03 }
958 },
959
960 { "data-50 key-25", /* Test 4 */
961 /* Test with a combined length of key and data that is larger
962 * than 64 bytes (= block-size of SHA-224 and SHA-256). */
963 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
964 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
965 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
966 "\xcd\xcd",
967 "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10"
968 "\x11\x12\x13\x14\x15\x16\x17\x18\x19",
969
970 { 0xa9, 0xd7, 0x68, 0x5a, 0x19, 0xc4, 0xe0, 0xdb,
971 0xd9, 0xdf, 0x25, 0x56, 0xcc, 0x8a, 0x7d, 0x2a,
972 0x77, 0x33, 0xb6, 0x76, 0x25, 0xce, 0x59, 0x4c,
973 0x78, 0x27, 0x0e, 0xeb },
974 { 0x57, 0x36, 0x6a, 0x45, 0xe2, 0x30, 0x53, 0x21,
975 0xa4, 0xbc, 0x5a, 0xa5, 0xfe, 0x2e, 0xf8, 0xa9,
976 0x21, 0xf6, 0xaf, 0x82, 0x73, 0xd7, 0xfe, 0x7b,
977 0xe6, 0xcf, 0xed, 0xb3, 0xf0, 0xae, 0xa6, 0xd7 },
978 { 0x3a, 0x5d, 0x7a, 0x87, 0x97, 0x02, 0xc0, 0x86,
979 0xbc, 0x96, 0xd1, 0xdd, 0x8a, 0xa1, 0x5d, 0x9c,
980 0x46, 0x44, 0x6b, 0x95, 0x52, 0x13, 0x11, 0xc6,
981 0x06, 0xfd, 0xc4, 0xe3, 0x08, 0xf4, 0xb9, 0x84,
982 0xda, 0x2d, 0x0f, 0x94, 0x49, 0xb3, 0xba, 0x84,
983 0x25, 0xec, 0x7f, 0xb8, 0xc3, 0x1b, 0xc1, 0x36 },
984 { 0xb2, 0x7e, 0xab, 0x1d, 0x6e, 0x8d, 0x87, 0x46,
985 0x1c, 0x29, 0xf7, 0xf5, 0x73, 0x9d, 0xd5, 0x8e,
986 0x98, 0xaa, 0x35, 0xf8, 0xe8, 0x23, 0xad, 0x38,
987 0xc5, 0x49, 0x2a, 0x20, 0x88, 0xfa, 0x02, 0x81,
988 0x99, 0x3b, 0xbf, 0xff, 0x9a, 0x0e, 0x9c, 0x6b,
989 0xf1, 0x21, 0xae, 0x9e, 0xc9, 0xbb, 0x09, 0xd8,
990 0x4a, 0x5e, 0xba, 0xc8, 0x17, 0x18, 0x2e, 0xa9,
991 0x74, 0x67, 0x3f, 0xb1, 0x33, 0xca, 0x0d, 0x1d }
992 },
993
994 { "data-20 key-20 trunc", /* Test 5 */
995 /* Test with a truncation of output to 128 bits. */
996 "Test With Truncation",
997 "\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c"
998 "\x0c\x0c\x0c\x0c",
999
1000 { 0x49, 0xfd, 0xd3, 0xab, 0xd0, 0x05, 0xeb, 0xb8,
1001 0xae, 0x63, 0xfe, 0xa9, 0x46, 0xd1, 0x88, 0x3c },
1002 { 0x6e, 0x02, 0xc6, 0x45, 0x37, 0xfb, 0x11, 0x80,
1003 0x57, 0xab, 0xb7, 0xfb, 0x66, 0xa2, 0x3b, 0x3c },
1004 { 0x47, 0xc5, 0x1a, 0xce, 0x1f, 0xfa, 0xcf, 0xfd,
1005 0x74, 0x94, 0x72, 0x46, 0x82, 0x61, 0x57, 0x83 },
1006 { 0x0f, 0xa7, 0x47, 0x59, 0x48, 0xf4, 0x3f, 0x48,
1007 0xca, 0x05, 0x16, 0x67, 0x1e, 0x18, 0x97, 0x8c },
1008 16
1009 },
1010
1011 { "data-54 key-131", /* Test 6 */
1012 /* Test with a key larger than 128 bytes (= block-size of
1013 * SHA-384 and SHA-512). */
1014 "Test Using Larger Than Block-Size Key - Hash Key First",
1015 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1016 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1017 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1018 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1019 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1020 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1021 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1022 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1023 "\xaa\xaa\xaa",
1024
1025 { 0xb4, 0xa1, 0xf0, 0x4c, 0x00, 0x28, 0x7a, 0x9b,
1026 0x7f, 0x60, 0x75, 0xb3, 0x13, 0xd2, 0x79, 0xb8,
1027 0x33, 0xbc, 0x8f, 0x75, 0x12, 0x43, 0x52, 0xd0,
1028 0x5f, 0xb9, 0x99, 0x5f },
1029 { 0xed, 0x73, 0xa3, 0x74, 0xb9, 0x6c, 0x00, 0x52,
1030 0x35, 0xf9, 0x48, 0x03, 0x2f, 0x09, 0x67, 0x4a,
1031 0x58, 0xc0, 0xce, 0x55, 0x5c, 0xfc, 0x1f, 0x22,
1032 0x3b, 0x02, 0x35, 0x65, 0x60, 0x31, 0x2c, 0x3b },
1033 { 0x0f, 0xc1, 0x95, 0x13, 0xbf, 0x6b, 0xd8, 0x78,
1034 0x03, 0x70, 0x16, 0x70, 0x6a, 0x0e, 0x57, 0xbc,
1035 0x52, 0x81, 0x39, 0x83, 0x6b, 0x9a, 0x42, 0xc3,
1036 0xd4, 0x19, 0xe4, 0x98, 0xe0, 0xe1, 0xfb, 0x96,
1037 0x16, 0xfd, 0x66, 0x91, 0x38, 0xd3, 0x3a, 0x11,
1038 0x05, 0xe0, 0x7c, 0x72, 0xb6, 0x95, 0x3b, 0xcc },
1039 { 0x00, 0xf7, 0x51, 0xa9, 0xe5, 0x06, 0x95, 0xb0,
1040 0x90, 0xed, 0x69, 0x11, 0xa4, 0xb6, 0x55, 0x24,
1041 0x95, 0x1c, 0xdc, 0x15, 0xa7, 0x3a, 0x5d, 0x58,
1042 0xbb, 0x55, 0x21, 0x5e, 0xa2, 0xcd, 0x83, 0x9a,
1043 0xc7, 0x9d, 0x2b, 0x44, 0xa3, 0x9b, 0xaf, 0xab,
1044 0x27, 0xe8, 0x3f, 0xde, 0x9e, 0x11, 0xf6, 0x34,
1045 0x0b, 0x11, 0xd9, 0x91, 0xb1, 0xb9, 0x1b, 0xf2,
1046 0xee, 0xe7, 0xfc, 0x87, 0x24, 0x26, 0xc3, 0xa4 }
1047 },
1048
1049 { "data-54 key-147", /* Test 6a */
1050 /* Test with a key larger than 144 bytes (= block-size of
1051 * SHA3-224). */
1052 "Test Using Larger Than Block-Size Key - Hash Key First",
1053 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1054 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1055 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1056 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1057 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1058 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1059 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1060 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1061 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1062 "\xaa\xaa\xaa",
1063
1064 { 0xb9, 0x6d, 0x73, 0x0c, 0x14, 0x8c, 0x2d, 0xaa,
1065 0xd8, 0x64, 0x9d, 0x83, 0xde, 0xfa, 0xa3, 0x71,
1066 0x97, 0x38, 0xd3, 0x47, 0x75, 0x39, 0x7b, 0x75,
1067 0x71, 0xc3, 0x85, 0x15 },
1068 { 0xa6, 0x07, 0x2f, 0x86, 0xde, 0x52, 0xb3, 0x8b,
1069 0xb3, 0x49, 0xfe, 0x84, 0xcd, 0x6d, 0x97, 0xfb,
1070 0x6a, 0x37, 0xc4, 0xc0, 0xf6, 0x2a, 0xae, 0x93,
1071 0x98, 0x11, 0x93, 0xa7, 0x22, 0x9d, 0x34, 0x67 },
1072 { 0x71, 0x3d, 0xff, 0x03, 0x02, 0xc8, 0x50, 0x86,
1073 0xec, 0x5a, 0xd0, 0x76, 0x8d, 0xd6, 0x5a, 0x13,
1074 0xdd, 0xd7, 0x90, 0x68, 0xd8, 0xd4, 0xc6, 0x21,
1075 0x2b, 0x71, 0x2e, 0x41, 0x64, 0x94, 0x49, 0x11,
1076 0x14, 0x80, 0x23, 0x00, 0x44, 0x18, 0x5a, 0x99,
1077 0x10, 0x3e, 0xd8, 0x20, 0x04, 0xdd, 0xbf, 0xcc },
1078 { 0xb1, 0x48, 0x35, 0xc8, 0x19, 0xa2, 0x90, 0xef,
1079 0xb0, 0x10, 0xac, 0xe6, 0xd8, 0x56, 0x8d, 0xc6,
1080 0xb8, 0x4d, 0xe6, 0x0b, 0xc4, 0x9b, 0x00, 0x4c,
1081 0x3b, 0x13, 0xed, 0xa7, 0x63, 0x58, 0x94, 0x51,
1082 0xe5, 0xdd, 0x74, 0x29, 0x28, 0x84, 0xd1, 0xbd,
1083 0xce, 0x64, 0xe6, 0xb9, 0x19, 0xdd, 0x61, 0xdc,
1084 0x9c, 0x56, 0xa2, 0x82, 0xa8, 0x1c, 0x0b, 0xd1,
1085 0x4f, 0x1f, 0x36, 0x5b, 0x49, 0xb8, 0x3a, 0x5b }
1086 },
1087
1088 { "data-152 key-131", /* Test 7 */
1089 /* Test with a key and data that is larger than 128 bytes (=
1090 * block-size of SHA-384 and SHA-512). */
1091 "This is a test using a larger than block-size key and a larger "
1092 "than block-size data. The key needs to be hashed before being "
1093 "used by the HMAC algorithm.",
1094 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1095 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1096 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1097 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1098 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1099 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1100 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1101 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1102 "\xaa\xaa\xaa",
1103
1104 { 0x05, 0xd8, 0xcd, 0x6d, 0x00, 0xfa, 0xea, 0x8d,
1105 0x1e, 0xb6, 0x8a, 0xde, 0x28, 0x73, 0x0b, 0xbd,
1106 0x3c, 0xba, 0xb6, 0x92, 0x9f, 0x0a, 0x08, 0x6b,
1107 0x29, 0xcd, 0x62, 0xa0 },
1108 { 0x65, 0xc5, 0xb0, 0x6d, 0x4c, 0x3d, 0xe3, 0x2a,
1109 0x7a, 0xef, 0x87, 0x63, 0x26, 0x1e, 0x49, 0xad,
1110 0xb6, 0xe2, 0x29, 0x3e, 0xc8, 0xe7, 0xc6, 0x1e,
1111 0x8d, 0xe6, 0x17, 0x01, 0xfc, 0x63, 0xe1, 0x23 },
1112 { 0x02, 0x6f, 0xdf, 0x6b, 0x50, 0x74, 0x1e, 0x37,
1113 0x38, 0x99, 0xc9, 0xf7, 0xd5, 0x40, 0x6d, 0x4e,
1114 0xb0, 0x9f, 0xc6, 0x66, 0x56, 0x36, 0xfc, 0x1a,
1115 0x53, 0x00, 0x29, 0xdd, 0xf5, 0xcf, 0x3c, 0xa5,
1116 0xa9, 0x00, 0xed, 0xce, 0x01, 0xf5, 0xf6, 0x1e,
1117 0x2f, 0x40, 0x8c, 0xdf, 0x2f, 0xd3, 0xe7, 0xe8 },
1118 { 0x38, 0xa4, 0x56, 0xa0, 0x04, 0xbd, 0x10, 0xd3,
1119 0x2c, 0x9a, 0xb8, 0x33, 0x66, 0x84, 0x11, 0x28,
1120 0x62, 0xc3, 0xdb, 0x61, 0xad, 0xcc, 0xa3, 0x18,
1121 0x29, 0x35, 0x5e, 0xaf, 0x46, 0xfd, 0x5c, 0x73,
1122 0xd0, 0x6a, 0x1f, 0x0d, 0x13, 0xfe, 0xc9, 0xa6,
1123 0x52, 0xfb, 0x38, 0x11, 0xb5, 0x77, 0xb1, 0xb1,
1124 0xd1, 0xb9, 0x78, 0x9f, 0x97, 0xae, 0x5b, 0x83,
1125 0xc6, 0xf4, 0x4d, 0xfc, 0xf1, 0xd6, 0x7e, 0xba }
1126 },
1127
1128 { "data-152 key-147", /* Test 7a */
1129 /* Test with a key larger than 144 bytes (= block-size of
1130 * SHA3-224). */
1131 "This is a test using a larger than block-size key and a larger "
1132 "than block-size data. The key needs to be hashed before being "
1133 "used by the HMAC algorithm.",
1134 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1135 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1136 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1137 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1138 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1139 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1140 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1141 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1142 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1143 "\xaa\xaa\xaa",
1144
1145 { 0xc7, 0x9c, 0x9b, 0x09, 0x34, 0x24, 0xe5, 0x88,
1146 0xa9, 0x87, 0x8b, 0xbc, 0xb0, 0x89, 0xe0, 0x18,
1147 0x27, 0x00, 0x96, 0xe9, 0xb4, 0xb1, 0xa9, 0xe8,
1148 0x22, 0x0c, 0x86, 0x6a },
1149 { 0xe6, 0xa3, 0x6d, 0x9b, 0x91, 0x5f, 0x86, 0xa0,
1150 0x93, 0xca, 0xc7, 0xd1, 0x10, 0xe9, 0xe0, 0x4c,
1151 0xf1, 0xd6, 0x10, 0x0d, 0x30, 0x47, 0x55, 0x09,
1152 0xc2, 0x47, 0x5f, 0x57, 0x1b, 0x75, 0x8b, 0x5a },
1153 { 0xca, 0xd1, 0x8a, 0x8f, 0xf6, 0xc4, 0xcc, 0x3a,
1154 0xd4, 0x87, 0xb9, 0x5f, 0x97, 0x69, 0xe9, 0xb6,
1155 0x1c, 0x06, 0x2a, 0xef, 0xd6, 0x95, 0x25, 0x69,
1156 0xe6, 0xe6, 0x42, 0x18, 0x97, 0x05, 0x4c, 0xfc,
1157 0x70, 0xb5, 0xfd, 0xc6, 0x60, 0x5c, 0x18, 0x45,
1158 0x71, 0x12, 0xfc, 0x6a, 0xaa, 0xd4, 0x55, 0x85 },
1159 { 0xdc, 0x03, 0x0e, 0xe7, 0x88, 0x70, 0x34, 0xf3,
1160 0x2c, 0xf4, 0x02, 0xdf, 0x34, 0x62, 0x2f, 0x31,
1161 0x1f, 0x3e, 0x6c, 0xf0, 0x48, 0x60, 0xc6, 0xbb,
1162 0xd7, 0xfa, 0x48, 0x86, 0x74, 0x78, 0x2b, 0x46,
1163 0x59, 0xfd, 0xbd, 0xf3, 0xfd, 0x87, 0x78, 0x52,
1164 0x88, 0x5c, 0xfe, 0x6e, 0x22, 0x18, 0x5f, 0xe7,
1165 0xb2, 0xee, 0x95, 0x20, 0x43, 0x62, 0x9b, 0xc9,
1166 0xd5, 0xf3, 0x29, 0x8a, 0x41, 0xd0, 0x2c, 0x66 }
1167 }/*,*/
1168
1169 /* Our API does not allow specifying a bit count and thus we
1170 * can't use the following test. */
1171 /* { "data-5bit key-4", /\* Test 8 *\/ */
1172 /* /\* Test with data bit size no multiple of 8, the data bits are */
1173 /* * '11001' from the NIST example using SHA-3 order (= 5 bits */
1174 /* * from LSB hex byte 13 or 5 bits from MSB hex byte c8). *\/ */
1175 /* "\xc8", */
1176 /* "Jefe", */
1177
1178 /* { 0x5f, 0x8c, 0x0e, 0xa7, 0xfa, 0xfe, 0xcd, 0x0c, */
1179 /* 0x34, 0x63, 0xaa, 0xd0, 0x97, 0x42, 0xce, 0xce, */
1180 /* 0xb1, 0x42, 0xfe, 0x0a, 0xb6, 0xf4, 0x53, 0x94, */
1181 /* 0x38, 0xc5, 0x9d, 0xe8 }, */
1182 /* { 0xec, 0x82, 0x22, 0x77, 0x3f, 0xac, 0x68, 0xb3, */
1183 /* 0xd3, 0xdc, 0xb1, 0x82, 0xae, 0xc8, 0xb0, 0x50, */
1184 /* 0x7a, 0xce, 0x44, 0x48, 0xd2, 0x0a, 0x11, 0x47, */
1185 /* 0xe6, 0x82, 0x11, 0x8d, 0xa4, 0xe3, 0xf4, 0x4c }, */
1186 /* { 0x21, 0xfb, 0xd3, 0xbf, 0x3e, 0xbb, 0xa3, 0xcf, */
1187 /* 0xc9, 0xef, 0x64, 0xc0, 0x59, 0x1c, 0x92, 0xc5, */
1188 /* 0xac, 0xb2, 0x65, 0xe9, 0x2d, 0x87, 0x61, 0xd1, */
1189 /* 0xf9, 0x1a, 0x52, 0xa1, 0x03, 0xa6, 0xc7, 0x96, */
1190 /* 0x94, 0xcf, 0xd6, 0x7a, 0x9a, 0x2a, 0xc1, 0x32, */
1191 /* 0x4f, 0x02, 0xfe, 0xa6, 0x3b, 0x81, 0xef, 0xfc }, */
1192 /* { 0x27, 0xf9, 0x38, 0x8c, 0x15, 0x67, 0xef, 0x4e, */
1193 /* 0xf2, 0x00, 0x60, 0x2a, 0x6c, 0xf8, 0x71, 0xd6, */
1194 /* 0x8a, 0x6f, 0xb0, 0x48, 0xd4, 0x73, 0x7a, 0xc4, */
1195 /* 0x41, 0x8a, 0x2f, 0x02, 0x12, 0x89, 0xd1, 0x3d, */
1196 /* 0x1f, 0xd1, 0x12, 0x0f, 0xec, 0xb9, 0xcf, 0x96, */
1197 /* 0x4c, 0x5b, 0x11, 0x7a, 0xb5, 0xb1, 0x1c, 0x61, */
1198 /* 0x4b, 0x2d, 0xa3, 0x9d, 0xad, 0xd5, 0x1f, 0x2f, */
1199 /* 0x5e, 0x22, 0xaa, 0xcc, 0xec, 0x7d, 0x57, 0x6e } */
1200 /* } */
1201
1202 };
1203 const char *what;
1204 const char *errtxt;
1205 int tvidx;
1206 const char *expect;
1207 int nexpect;
1208
1209 for (tvidx=0; tvidx < DIM(tv); tvidx++)
1210 {
1211 what = tv[tvidx].desc;
1212 if (hashalgo == GCRY_MD_SHA3_224)
1213 {
1214 expect = tv[tvidx].expect_224;
1215 nexpect = DIM (tv[tvidx].expect_224);
1216 }
1217 else if (hashalgo == GCRY_MD_SHA3_256)
1218 {
1219 expect = tv[tvidx].expect_256;
1220 nexpect = DIM (tv[tvidx].expect_256);
1221 }
1222 else if (hashalgo == GCRY_MD_SHA3_384)
1223 {
1224 expect = tv[tvidx].expect_384;
1225 nexpect = DIM (tv[tvidx].expect_384);
1226 }
1227 else if (hashalgo == GCRY_MD_SHA3_512)
1228 {
1229 expect = tv[tvidx].expect_512;
1230 nexpect = DIM (tv[tvidx].expect_512);
1231 }
1232 else
1233 BUG();
1234
1235 if (tv[tvidx].trunc && tv[tvidx].trunc < nexpect)
1236 nexpect = tv[tvidx].trunc;
1237
1238 errtxt = check_one (hashalgo,
1239 tv[tvidx].data, strlen (tv[tvidx].data),
1240 tv[tvidx].key, strlen (tv[tvidx].key),
1241 expect, nexpect, !!tv[tvidx].trunc);
1242 if (errtxt)
1243 goto failed;
1244 if (!extended)
1245 break;
1246 }
1247
1248 return 0; /* Succeeded. */
1249
1250 failed:
1251 if (report)
1252 report ("hmac", hashalgo, what, errtxt);
1253 return GPG_ERR_SELFTEST_FAILED;
1254 }
1255
1256
1257 static gpg_err_code_t
1258 hmac_selftest (int algo, int extended, selftest_report_func_t report)
1259 {
1260 gpg_err_code_t ec;
1261
1262 switch (algo)
1263 {
1264 case GCRY_MAC_HMAC_SHA1:
1265 ec = selftests_sha1 (extended, report);
1266 break;
1267 case GCRY_MAC_HMAC_SHA224:
1268 ec = selftests_sha224 (extended, report);
1269 break;
1270 case GCRY_MAC_HMAC_SHA256:
1271 ec = selftests_sha256 (extended, report);
1272 break;
1273 case GCRY_MAC_HMAC_SHA384:
1274 ec = selftests_sha384 (extended, report);
1275 break;
1276 case GCRY_MAC_HMAC_SHA512:
1277 ec = selftests_sha512 (extended, report);
1278 break;
1279
1280 case GCRY_MAC_HMAC_SHA3_224:
1281 case GCRY_MAC_HMAC_SHA3_256:
1282 case GCRY_MAC_HMAC_SHA3_384:
1283 case GCRY_MAC_HMAC_SHA3_512:
1284 {
1285 int md_algo = map_mac_algo_to_md (algo);
1286 ec = selftests_sha3 (md_algo, extended, report);
1287 }
1288 break;
1289
1290 default:
1291 ec = GPG_ERR_MAC_ALGO;
1292 break;
1293 }
1294
1295 return ec;
1296 }
1297
1298
1299 static const gcry_mac_spec_ops_t hmac_ops = {
1300 hmac_open,
1301 hmac_close,
1302 hmac_setkey,
1303 NULL,
1304 hmac_reset,
1305 hmac_write,
1306 hmac_read,
1307 hmac_verify,
1308 hmac_get_maclen,
1309 hmac_get_keylen,
1310 NULL,
1311 hmac_selftest
1312 };
1313
1314
1315 #if USE_SHA1
1316 const gcry_mac_spec_t _gcry_mac_type_spec_hmac_sha1 = {
1317 GCRY_MAC_HMAC_SHA1, {0, 1}, "HMAC_SHA1",
1318 &hmac_ops
1319 };
1320 #endif
1321 #if USE_SHA256
1322 const gcry_mac_spec_t _gcry_mac_type_spec_hmac_sha256 = {
1323 GCRY_MAC_HMAC_SHA256, {0, 1}, "HMAC_SHA256",
1324 &hmac_ops
1325 };
1326
1327 const gcry_mac_spec_t _gcry_mac_type_spec_hmac_sha224 = {
1328 GCRY_MAC_HMAC_SHA224, {0, 1}, "HMAC_SHA224",
1329 &hmac_ops
1330 };
1331 #endif
1332 #if USE_SHA512
1333 const gcry_mac_spec_t _gcry_mac_type_spec_hmac_sha512 = {
1334 GCRY_MAC_HMAC_SHA512, {0, 1}, "HMAC_SHA512",
1335 &hmac_ops
1336 };
1337
1338 const gcry_mac_spec_t _gcry_mac_type_spec_hmac_sha384 = {
1339 GCRY_MAC_HMAC_SHA384, {0, 1}, "HMAC_SHA384",
1340 &hmac_ops
1341 };
1342
1343 const gcry_mac_spec_t _gcry_mac_type_spec_hmac_sha512_256 = {
1344 GCRY_MAC_HMAC_SHA512_256, {0, 1}, "HMAC_SHA512_256",
1345 &hmac_ops
1346 };
1347
1348 const gcry_mac_spec_t _gcry_mac_type_spec_hmac_sha512_224 = {
1349 GCRY_MAC_HMAC_SHA512_224, {0, 1}, "HMAC_SHA512_224",
1350 &hmac_ops
1351 };
1352
1353 #endif
1354 #if USE_SHA3
1355 const gcry_mac_spec_t _gcry_mac_type_spec_hmac_sha3_224 = {
1356 GCRY_MAC_HMAC_SHA3_224, {0, 1}, "HMAC_SHA3_224",
1357 &hmac_ops
1358 };
1359
1360 const gcry_mac_spec_t _gcry_mac_type_spec_hmac_sha3_256 = {
1361 GCRY_MAC_HMAC_SHA3_256, {0, 1}, "HMAC_SHA3_256",
1362 &hmac_ops
1363 };
1364
1365 const gcry_mac_spec_t _gcry_mac_type_spec_hmac_sha3_384 = {
1366 GCRY_MAC_HMAC_SHA3_384, {0, 1}, "HMAC_SHA3_384",
1367 &hmac_ops
1368 };
1369
1370 const gcry_mac_spec_t _gcry_mac_type_spec_hmac_sha3_512 = {
1371 GCRY_MAC_HMAC_SHA3_512, {0, 1}, "HMAC_SHA3_512",
1372 &hmac_ops
1373 };
1374 #endif
1375 #if USE_GOST_R_3411_94
1376 const gcry_mac_spec_t _gcry_mac_type_spec_hmac_gost3411_94 = {
1377 GCRY_MAC_HMAC_GOSTR3411_94, {0, 0}, "HMAC_GOSTR3411_94",
1378 &hmac_ops
1379 };
1380 const gcry_mac_spec_t _gcry_mac_type_spec_hmac_gost3411_cp = {
1381 GCRY_MAC_HMAC_GOSTR3411_CP, {0, 0}, "HMAC_GOSTR3411_CP",
1382 &hmac_ops
1383 };
1384 #endif
1385 #if USE_GOST_R_3411_12
1386 const gcry_mac_spec_t _gcry_mac_type_spec_hmac_stribog256 = {
1387 GCRY_MAC_HMAC_STRIBOG256, {0, 0}, "HMAC_STRIBOG256",
1388 &hmac_ops
1389 };
1390
1391 const gcry_mac_spec_t _gcry_mac_type_spec_hmac_stribog512 = {
1392 GCRY_MAC_HMAC_STRIBOG512, {0, 0}, "HMAC_STRIBOG512",
1393 &hmac_ops
1394 };
1395 #endif
1396 #if USE_WHIRLPOOL
1397 const gcry_mac_spec_t _gcry_mac_type_spec_hmac_whirlpool = {
1398 GCRY_MAC_HMAC_WHIRLPOOL, {0, 0}, "HMAC_WHIRLPOOL",
1399 &hmac_ops
1400 };
1401 #endif
1402 #if USE_RMD160
1403 const gcry_mac_spec_t _gcry_mac_type_spec_hmac_rmd160 = {
1404 GCRY_MAC_HMAC_RMD160, {0, 0}, "HMAC_RIPEMD160",
1405 &hmac_ops
1406 };
1407 #endif
1408 #if USE_TIGER
1409 const gcry_mac_spec_t _gcry_mac_type_spec_hmac_tiger1 = {
1410 GCRY_MAC_HMAC_TIGER1, {0, 0}, "HMAC_TIGER",
1411 &hmac_ops
1412 };
1413 #endif
1414 #if USE_MD5
1415 const gcry_mac_spec_t _gcry_mac_type_spec_hmac_md5 = {
1416 GCRY_MAC_HMAC_MD5, {0, 0}, "HMAC_MD5",
1417 &hmac_ops
1418 };
1419 #endif
1420 #if USE_MD4
1421 const gcry_mac_spec_t _gcry_mac_type_spec_hmac_md4 = {
1422 GCRY_MAC_HMAC_MD4, {0, 0}, "HMAC_MD4",
1423 &hmac_ops
1424 };
1425 #endif
1426 #if USE_MD2
1427 const gcry_mac_spec_t _gcry_mac_type_spec_hmac_md2 = {
1428 GCRY_MAC_HMAC_MD2, {0, 0}, "HMAC_MD2",
1429 &hmac_ops
1430 };
1431 #endif
1432 #if USE_BLAKE2
1433 const gcry_mac_spec_t _gcry_mac_type_spec_hmac_blake2b_512 = {
1434 GCRY_MAC_HMAC_BLAKE2B_512, {0, 0}, "HMAC_BLAKE2B_512",
1435 &hmac_ops
1436 };
1437 const gcry_mac_spec_t _gcry_mac_type_spec_hmac_blake2b_384 = {
1438 GCRY_MAC_HMAC_BLAKE2B_384, {0, 0}, "HMAC_BLAKE2B_384",
1439 &hmac_ops
1440 };
1441 const gcry_mac_spec_t _gcry_mac_type_spec_hmac_blake2b_256 = {
1442 GCRY_MAC_HMAC_BLAKE2B_256, {0, 0}, "HMAC_BLAKE2B_256",
1443 &hmac_ops
1444 };
1445 const gcry_mac_spec_t _gcry_mac_type_spec_hmac_blake2b_160 = {
1446 GCRY_MAC_HMAC_BLAKE2B_160, {0, 0}, "HMAC_BLAKE2B_160",
1447 &hmac_ops
1448 };
1449 const gcry_mac_spec_t _gcry_mac_type_spec_hmac_blake2s_256 = {
1450 GCRY_MAC_HMAC_BLAKE2S_256, {0, 0}, "HMAC_BLAKE2S_256",
1451 &hmac_ops
1452 };
1453 const gcry_mac_spec_t _gcry_mac_type_spec_hmac_blake2s_224 = {
1454 GCRY_MAC_HMAC_BLAKE2S_224, {0, 0}, "HMAC_BLAKE2S_224",
1455 &hmac_ops
1456 };
1457 const gcry_mac_spec_t _gcry_mac_type_spec_hmac_blake2s_160 = {
1458 GCRY_MAC_HMAC_BLAKE2S_160, {0, 0}, "HMAC_BLAKE2S_160",
1459 &hmac_ops
1460 };
1461 const gcry_mac_spec_t _gcry_mac_type_spec_hmac_blake2s_128 = {
1462 GCRY_MAC_HMAC_BLAKE2S_128, {0, 0}, "HMAC_BLAKE2S_128",
1463 &hmac_ops
1464 };
1465 #endif
1466 #if USE_SM3
1467 const gcry_mac_spec_t _gcry_mac_type_spec_hmac_sm3 = {
1468 GCRY_MAC_HMAC_SM3, {0, 0}, "HMAC_SM3",
1469 &hmac_ops
1470 };
1471 #endif