1 /* basic.c - basic regression tests
2 * Copyright (C) 2001, 2002, 2003, 2005, 2008 Free Software Foundation, Inc.
4 * This file is part of Libgcrypt.
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
28 #include "../src/gcrypt.h"
30 typedef struct test_spec_pubkey_key
36 test_spec_pubkey_key_t;
38 typedef struct test_spec_pubkey
42 test_spec_pubkey_key_t key;
46 #define FLAG_CRYPT (1 << 0)
47 #define FLAG_SIGN (1 << 1)
48 #define FLAG_GRIP (1 << 2)
51 static int error_count;
52 static int in_fips_mode;
55 fail (const char *format, ...)
59 va_start (arg_ptr, format);
60 vfprintf (stderr, format, arg_ptr);
66 die (const char *format, ...)
70 va_start (arg_ptr, format);
71 vfprintf (stderr, format, arg_ptr);
76 #define MAX_DATA_LEN 100
79 progress_handler (void *cb_data, const char *what, int printchar,
80 int current, int total)
87 if (printchar == '\n')
88 fputs ( "<LF>", stdout);
95 check_cbc_mac_cipher (void)
100 char key[MAX_DATA_LEN];
101 unsigned char plaintext[MAX_DATA_LEN];
103 char mac[MAX_DATA_LEN];
109 "This is a sample plaintext for CBC MAC of sixtyfour bytes.......",
110 0, "\x23\x8f\x6d\xc7\x53\x6a\x62\x97\x11\xc4\xa5\x16\x43\xea\xb0\xb6" },
112 "abcdefghABCDEFGH01234567",
113 "This is a sample plaintext for CBC MAC of sixtyfour bytes.......",
114 0, "\x5c\x11\xf0\x01\x47\xbd\x3d\x3a" },
117 "This is a sample plaintext for CBC MAC of sixtyfour bytes.......",
118 0, "\xfa\x4b\xdf\x9d\xfa\xab\x01\x70" }
121 unsigned char out[MAX_DATA_LEN];
122 int i, blklen, keylen;
123 gcry_error_t err = 0;
126 fprintf (stderr, "Starting CBC MAC checks.\n");
128 for (i = 0; i < sizeof (tv) / sizeof (tv[0]); i++)
130 if (gcry_cipher_test_algo (tv[i].algo) && in_fips_mode)
133 fprintf (stderr, " algorithm %d not available in fips mode\n",
138 err = gcry_cipher_open (&hd,
140 GCRY_CIPHER_MODE_CBC, GCRY_CIPHER_CBC_MAC);
143 fail ("cbc-mac algo %d, grcy_open_cipher failed: %s\n",
144 tv[i].algo, gpg_strerror (err));
148 blklen = gcry_cipher_get_algo_blklen(tv[i].algo);
151 fail ("cbc-mac algo %d, gcry_cipher_get_algo_blklen failed\n",
153 gcry_cipher_close (hd);
157 keylen = gcry_cipher_get_algo_keylen (tv[i].algo);
160 fail ("cbc-mac algo %d, gcry_cipher_get_algo_keylen failed\n",
165 err = gcry_cipher_setkey (hd, tv[i].key, keylen);
168 fail ("cbc-mac algo %d, gcry_cipher_setkey failed: %s\n",
169 tv[i].algo, gpg_strerror (err));
170 gcry_cipher_close (hd);
174 err = gcry_cipher_setiv (hd, NULL, 0);
177 fail ("cbc-mac algo %d, gcry_cipher_setiv failed: %s\n",
178 tv[i].algo, gpg_strerror (err));
179 gcry_cipher_close (hd);
184 fprintf (stderr, " checking CBC MAC for %s [%i]\n",
185 gcry_cipher_algo_name (tv[i].algo),
187 err = gcry_cipher_encrypt (hd,
192 strlen ((char*)tv[i].plaintext));
195 fail ("cbc-mac algo %d, gcry_cipher_encrypt failed: %s\n",
196 tv[i].algo, gpg_strerror (err));
197 gcry_cipher_close (hd);
204 for (j = 0; j < gcry_cipher_get_algo_blklen (tv[i].algo); j++)
205 printf ("\\x%02x", out[j] & 0xFF);
210 if (memcmp (tv[i].mac, out, blklen))
211 fail ("cbc-mac algo %d, encrypt mismatch entry %d\n", tv[i].algo, i);
213 gcry_cipher_close (hd);
216 fprintf (stderr, "Completed CBC MAC checks.\n");
220 check_aes128_cbc_cts_cipher (void)
222 char key[128 / 8] = "chicken teriyaki";
223 unsigned char plaintext[] =
224 "I would like the General Gau's Chicken, please, and wonton soup.";
227 unsigned char out[MAX_DATA_LEN];
231 { "\xc6\x35\x35\x68\xf2\xbf\x8c\xb4\xd8\xa5\x80\x36\x2d\xa7\xff\x7f"
234 { "\xfc\x00\x78\x3e\x0e\xfd\xb2\xc1\xd4\x45\xd4\xc8\xef\xf7\xed\x22"
235 "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5",
237 { "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
238 "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84",
240 { "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
241 "\xb3\xff\xfd\x94\x0c\x16\xa1\x8c\x1b\x55\x49\xd2\xf8\x38\x02\x9e"
242 "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5",
244 { "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
245 "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8"
246 "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8",
248 { "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
249 "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
250 "\x48\x07\xef\xe8\x36\xee\x89\xa5\x26\x73\x0d\xbc\x2f\x7b\xc8\x40"
251 "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8",
255 unsigned char out[MAX_DATA_LEN];
257 gcry_error_t err = 0;
260 fprintf (stderr, "Starting AES128 CBC CTS checks.\n");
261 err = gcry_cipher_open (&hd,
263 GCRY_CIPHER_MODE_CBC, GCRY_CIPHER_CBC_CTS);
266 fail ("aes-cbc-cts, grcy_open_cipher failed: %s\n", gpg_strerror (err));
270 err = gcry_cipher_setkey (hd, key, 128 / 8);
273 fail ("aes-cbc-cts, gcry_cipher_setkey failed: %s\n",
275 gcry_cipher_close (hd);
279 for (i = 0; i < sizeof (tv) / sizeof (tv[0]); i++)
281 err = gcry_cipher_setiv (hd, NULL, 0);
284 fail ("aes-cbc-cts, gcry_cipher_setiv failed: %s\n",
286 gcry_cipher_close (hd);
291 fprintf (stderr, " checking encryption for length %i\n", tv[i].inlen);
292 err = gcry_cipher_encrypt (hd, out, MAX_DATA_LEN,
293 plaintext, tv[i].inlen);
296 fail ("aes-cbc-cts, gcry_cipher_encrypt failed: %s\n",
298 gcry_cipher_close (hd);
302 if (memcmp (tv[i].out, out, tv[i].inlen))
303 fail ("aes-cbc-cts, encrypt mismatch entry %d\n", i);
305 err = gcry_cipher_setiv (hd, NULL, 0);
308 fail ("aes-cbc-cts, gcry_cipher_setiv failed: %s\n",
310 gcry_cipher_close (hd);
314 fprintf (stderr, " checking decryption for length %i\n", tv[i].inlen);
315 err = gcry_cipher_decrypt (hd, out, tv[i].inlen, NULL, 0);
318 fail ("aes-cbc-cts, gcry_cipher_decrypt failed: %s\n",
320 gcry_cipher_close (hd);
324 if (memcmp (plaintext, out, tv[i].inlen))
325 fail ("aes-cbc-cts, decrypt mismatch entry %d\n", i);
328 gcry_cipher_close (hd);
330 fprintf (stderr, "Completed AES128 CBC CTS checks.\n");
334 check_ctr_cipher (void)
339 char key[MAX_DATA_LEN];
340 char ctr[MAX_DATA_LEN];
343 unsigned char plaintext[MAX_DATA_LEN];
345 char out[MAX_DATA_LEN];
350 /* http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf */
352 "\x2b\x7e\x15\x16\x28\xae\xd2\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c",
353 "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff",
354 { { "\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\xe9\x3d\x7e\x11\x73\x93\x17\x2a",
356 "\x87\x4d\x61\x91\xb6\x20\xe3\x26\x1b\xef\x68\x64\x99\x0d\xb6\xce" },
357 { "\xae\x2d\x8a\x57\x1e\x03\xac\x9c\x9e\xb7\x6f\xac\x45\xaf\x8e\x51",
359 "\x98\x06\xf6\x6b\x79\x70\xfd\xff\x86\x17\x18\x7b\xb9\xff\xfd\xff" },
360 { "\x30\xc8\x1c\x46\xa3\x5c\xe4\x11\xe5\xfb\xc1\x19\x1a\x0a\x52\xef",
362 "\x5a\xe4\xdf\x3e\xdb\xd5\xd3\x5e\x5b\x4f\x09\x02\x0d\xb0\x3e\xab" },
363 { "\xf6\x9f\x24\x45\xdf\x4f\x9b\x17\xad\x2b\x41\x7b\xe6\x6c\x37\x10",
365 "\x1e\x03\x1d\xda\x2f\xbe\x03\xd1\x79\x21\x70\xa0\xf3\x00\x9c\xee" },
368 { GCRY_CIPHER_AES192,
369 "\x8e\x73\xb0\xf7\xda\x0e\x64\x52\xc8\x10\xf3\x2b"
370 "\x80\x90\x79\xe5\x62\xf8\xea\xd2\x52\x2c\x6b\x7b",
371 "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff",
372 { { "\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\xe9\x3d\x7e\x11\x73\x93\x17\x2a",
374 "\x1a\xbc\x93\x24\x17\x52\x1c\xa2\x4f\x2b\x04\x59\xfe\x7e\x6e\x0b" },
375 { "\xae\x2d\x8a\x57\x1e\x03\xac\x9c\x9e\xb7\x6f\xac\x45\xaf\x8e\x51",
377 "\x09\x03\x39\xec\x0a\xa6\xfa\xef\xd5\xcc\xc2\xc6\xf4\xce\x8e\x94" },
378 { "\x30\xc8\x1c\x46\xa3\x5c\xe4\x11\xe5\xfb\xc1\x19\x1a\x0a\x52\xef",
380 "\x1e\x36\xb2\x6b\xd1\xeb\xc6\x70\xd1\xbd\x1d\x66\x56\x20\xab\xf7" },
381 { "\xf6\x9f\x24\x45\xdf\x4f\x9b\x17\xad\x2b\x41\x7b\xe6\x6c\x37\x10",
383 "\x4f\x78\xa7\xf6\xd2\x98\x09\x58\x5a\x97\xda\xec\x58\xc6\xb0\x50" },
386 { GCRY_CIPHER_AES256,
387 "\x60\x3d\xeb\x10\x15\xca\x71\xbe\x2b\x73\xae\xf0\x85\x7d\x77\x81"
388 "\x1f\x35\x2c\x07\x3b\x61\x08\xd7\x2d\x98\x10\xa3\x09\x14\xdf\xf4",
389 "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff",
390 { { "\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\xe9\x3d\x7e\x11\x73\x93\x17\x2a",
392 "\x60\x1e\xc3\x13\x77\x57\x89\xa5\xb7\xa7\xf5\x04\xbb\xf3\xd2\x28" },
393 { "\xae\x2d\x8a\x57\x1e\x03\xac\x9c\x9e\xb7\x6f\xac\x45\xaf\x8e\x51",
395 "\xf4\x43\xe3\xca\x4d\x62\xb5\x9a\xca\x84\xe9\x90\xca\xca\xf5\xc5" },
396 { "\x30\xc8\x1c\x46\xa3\x5c\xe4\x11\xe5\xfb\xc1\x19\x1a\x0a\x52\xef",
398 "\x2b\x09\x30\xda\xa2\x3d\xe9\x4c\xe8\x70\x17\xba\x2d\x84\x98\x8d" },
399 { "\xf6\x9f\x24\x45\xdf\x4f\x9b\x17\xad\x2b\x41\x7b\xe6\x6c\x37\x10",
401 "\xdf\xc9\xc5\x8d\xb6\x7a\xad\xa6\x13\xc2\xdd\x08\x45\x79\x41\xa6" }
405 gcry_cipher_hd_t hde, hdd;
406 unsigned char out[MAX_DATA_LEN];
407 int i, j, keylen, blklen;
408 gcry_error_t err = 0;
411 fprintf (stderr, "Starting CTR cipher checks.\n");
412 for (i = 0; i < sizeof (tv) / sizeof (tv[0]); i++)
414 err = gcry_cipher_open (&hde, tv[i].algo, GCRY_CIPHER_MODE_CTR, 0);
416 err = gcry_cipher_open (&hdd, tv[i].algo, GCRY_CIPHER_MODE_CTR, 0);
419 fail ("aes-ctr, grcy_open_cipher failed: %s\n", gpg_strerror (err));
423 keylen = gcry_cipher_get_algo_keylen(tv[i].algo);
426 fail ("aes-ctr, gcry_cipher_get_algo_keylen failed\n");
430 err = gcry_cipher_setkey (hde, tv[i].key, keylen);
432 err = gcry_cipher_setkey (hdd, tv[i].key, keylen);
435 fail ("aes-ctr, gcry_cipher_setkey failed: %s\n",
437 gcry_cipher_close (hde);
438 gcry_cipher_close (hdd);
442 blklen = gcry_cipher_get_algo_blklen(tv[i].algo);
445 fail ("aes-ctr, gcry_cipher_get_algo_blklen failed\n");
449 err = gcry_cipher_setctr (hde, tv[i].ctr, blklen);
451 err = gcry_cipher_setctr (hdd, tv[i].ctr, blklen);
454 fail ("aes-ctr, gcry_cipher_setctr failed: %s\n",
456 gcry_cipher_close (hde);
457 gcry_cipher_close (hdd);
462 fprintf (stderr, " checking CTR mode for for %s [%i]\n",
463 gcry_cipher_algo_name (tv[i].algo),
465 for (j = 0; tv[i].data[j].inlen; j++)
467 err = gcry_cipher_encrypt (hde, out, MAX_DATA_LEN,
468 tv[i].data[j].plaintext,
469 tv[i].data[j].inlen == -1 ?
470 strlen ((char*)tv[i].data[j].plaintext) :
471 tv[i].data[j].inlen);
474 fail ("aes-ctr, gcry_cipher_encrypt (%d, %d) failed: %s\n",
475 i, j, gpg_strerror (err));
476 gcry_cipher_close (hde);
477 gcry_cipher_close (hdd);
481 if (memcmp (tv[i].data[j].out, out, tv[i].data[j].inlen))
482 fail ("aes-ctr, encrypt mismatch entry %d:%d\n", i, j);
484 err = gcry_cipher_decrypt (hdd, out, tv[i].data[j].inlen, NULL, 0);
487 fail ("aes-ctr, gcry_cipher_decrypt (%d, %d) failed: %s\n",
488 i, j, gpg_strerror (err));
489 gcry_cipher_close (hde);
490 gcry_cipher_close (hdd);
494 if (memcmp (tv[i].data[j].plaintext, out, tv[i].data[j].inlen))
495 fail ("aes-ctr, decrypt mismatch entry %d:%d\n", i, j);
498 gcry_cipher_close (hde);
499 gcry_cipher_close (hdd);
502 fprintf (stderr, "Completed CTR cipher checks.\n");
506 check_cfb_cipher (void)
511 char key[MAX_DATA_LEN];
512 char iv[MAX_DATA_LEN];
515 unsigned char plaintext[MAX_DATA_LEN];
517 char out[MAX_DATA_LEN];
522 /* http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf */
524 "\x2b\x7e\x15\x16\x28\xae\xd2\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c",
525 "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
526 { { "\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\xe9\x3d\x7e\x11\x73\x93\x17\x2a",
528 "\x3b\x3f\xd9\x2e\xb7\x2d\xad\x20\x33\x34\x49\xf8\xe8\x3c\xfb\x4a" },
529 { "\xae\x2d\x8a\x57\x1e\x03\xac\x9c\x9e\xb7\x6f\xac\x45\xaf\x8e\x51",
531 "\xc8\xa6\x45\x37\xa0\xb3\xa9\x3f\xcd\xe3\xcd\xad\x9f\x1c\xe5\x8b"},
532 { "\x30\xc8\x1c\x46\xa3\x5c\xe4\x11\xe5\xfb\xc1\x19\x1a\x0a\x52\xef",
534 "\x26\x75\x1f\x67\xa3\xcb\xb1\x40\xb1\x80\x8c\xf1\x87\xa4\xf4\xdf" },
535 { "\xf6\x9f\x24\x45\xdf\x4f\x9b\x17\xad\x2b\x41\x7b\xe6\x6c\x37\x10",
537 "\xc0\x4b\x05\x35\x7c\x5d\x1c\x0e\xea\xc4\xc6\x6f\x9f\xf7\xf2\xe6" },
540 { GCRY_CIPHER_AES192,
541 "\x8e\x73\xb0\xf7\xda\x0e\x64\x52\xc8\x10\xf3\x2b"
542 "\x80\x90\x79\xe5\x62\xf8\xea\xd2\x52\x2c\x6b\x7b",
543 "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
544 { { "\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\xe9\x3d\x7e\x11\x73\x93\x17\x2a",
546 "\xcd\xc8\x0d\x6f\xdd\xf1\x8c\xab\x34\xc2\x59\x09\xc9\x9a\x41\x74" },
547 { "\xae\x2d\x8a\x57\x1e\x03\xac\x9c\x9e\xb7\x6f\xac\x45\xaf\x8e\x51",
549 "\x67\xce\x7f\x7f\x81\x17\x36\x21\x96\x1a\x2b\x70\x17\x1d\x3d\x7a" },
550 { "\x30\xc8\x1c\x46\xa3\x5c\xe4\x11\xe5\xfb\xc1\x19\x1a\x0a\x52\xef",
552 "\x2e\x1e\x8a\x1d\xd5\x9b\x88\xb1\xc8\xe6\x0f\xed\x1e\xfa\xc4\xc9" },
553 { "\xf6\x9f\x24\x45\xdf\x4f\x9b\x17\xad\x2b\x41\x7b\xe6\x6c\x37\x10",
555 "\xc0\x5f\x9f\x9c\xa9\x83\x4f\xa0\x42\xae\x8f\xba\x58\x4b\x09\xff" },
558 { GCRY_CIPHER_AES256,
559 "\x60\x3d\xeb\x10\x15\xca\x71\xbe\x2b\x73\xae\xf0\x85\x7d\x77\x81"
560 "\x1f\x35\x2c\x07\x3b\x61\x08\xd7\x2d\x98\x10\xa3\x09\x14\xdf\xf4",
561 "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
562 { { "\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\xe9\x3d\x7e\x11\x73\x93\x17\x2a",
564 "\xdc\x7e\x84\xbf\xda\x79\x16\x4b\x7e\xcd\x84\x86\x98\x5d\x38\x60" },
565 { "\xae\x2d\x8a\x57\x1e\x03\xac\x9c\x9e\xb7\x6f\xac\x45\xaf\x8e\x51",
567 "\x39\xff\xed\x14\x3b\x28\xb1\xc8\x32\x11\x3c\x63\x31\xe5\x40\x7b" },
568 { "\x30\xc8\x1c\x46\xa3\x5c\xe4\x11\xe5\xfb\xc1\x19\x1a\x0a\x52\xef",
570 "\xdf\x10\x13\x24\x15\xe5\x4b\x92\xa1\x3e\xd0\xa8\x26\x7a\xe2\xf9" },
571 { "\xf6\x9f\x24\x45\xdf\x4f\x9b\x17\xad\x2b\x41\x7b\xe6\x6c\x37\x10",
573 "\x75\xa3\x85\x74\x1a\xb9\xce\xf8\x20\x31\x62\x3d\x55\xb1\xe4\x71" }
577 gcry_cipher_hd_t hde, hdd;
578 unsigned char out[MAX_DATA_LEN];
579 int i, j, keylen, blklen;
580 gcry_error_t err = 0;
582 for (i = 0; i < sizeof (tv) / sizeof (tv[0]); i++)
584 err = gcry_cipher_open (&hde, tv[i].algo, GCRY_CIPHER_MODE_CFB, 0);
586 err = gcry_cipher_open (&hdd, tv[i].algo, GCRY_CIPHER_MODE_CFB, 0);
589 fail ("aes-cfb, grcy_open_cipher failed: %s\n", gpg_strerror (err));
593 keylen = gcry_cipher_get_algo_keylen(tv[i].algo);
596 fail ("aes-cfb, gcry_cipher_get_algo_keylen failed\n");
600 err = gcry_cipher_setkey (hde, tv[i].key, keylen);
602 err = gcry_cipher_setkey (hdd, tv[i].key, keylen);
605 fail ("aes-cfb, gcry_cipher_setkey failed: %s\n",
607 gcry_cipher_close (hde);
608 gcry_cipher_close (hdd);
612 blklen = gcry_cipher_get_algo_blklen(tv[i].algo);
615 fail ("aes-cfb, gcry_cipher_get_algo_blklen failed\n");
619 err = gcry_cipher_setiv (hde, tv[i].iv, blklen);
621 err = gcry_cipher_setiv (hdd, tv[i].iv, blklen);
624 fail ("aes-cfb, gcry_cipher_setiv failed: %s\n",
626 gcry_cipher_close (hde);
627 gcry_cipher_close (hdd);
631 for (j = 0; tv[i].data[j].inlen; j++)
633 err = gcry_cipher_encrypt (hde, out, MAX_DATA_LEN,
634 tv[i].data[j].plaintext,
635 tv[i].data[j].inlen);
638 fail ("aes-cfb, gcry_cipher_encrypt (%d, %d) failed: %s\n",
639 i, j, gpg_strerror (err));
640 gcry_cipher_close (hde);
641 gcry_cipher_close (hdd);
645 if (memcmp (tv[i].data[j].out, out, tv[i].data[j].inlen)) {
646 fail ("aes-cfb, encrypt mismatch entry %d:%d\n", i, j);
648 err = gcry_cipher_decrypt (hdd, out, tv[i].data[j].inlen, NULL, 0);
651 fail ("aes-cfb, gcry_cipher_decrypt (%d, %d) failed: %s\n",
652 i, j, gpg_strerror (err));
653 gcry_cipher_close (hde);
654 gcry_cipher_close (hdd);
658 if (memcmp (tv[i].data[j].plaintext, out, tv[i].data[j].inlen))
659 fail ("aes-cfb, decrypt mismatch entry %d:%d\n", i, j);
662 gcry_cipher_close (hde);
663 gcry_cipher_close (hdd);
668 check_ofb_cipher (void)
673 char key[MAX_DATA_LEN];
674 char iv[MAX_DATA_LEN];
677 unsigned char plaintext[MAX_DATA_LEN];
679 char out[MAX_DATA_LEN];
684 /* http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf */
686 "\x2b\x7e\x15\x16\x28\xae\xd2\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c",
687 "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
688 { { "\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\xe9\x3d\x7e\x11\x73\x93\x17\x2a",
690 "\x3b\x3f\xd9\x2e\xb7\x2d\xad\x20\x33\x34\x49\xf8\xe8\x3c\xfb\x4a" },
691 { "\xae\x2d\x8a\x57\x1e\x03\xac\x9c\x9e\xb7\x6f\xac\x45\xaf\x8e\x51",
693 "\x77\x89\x50\x8d\x16\x91\x8f\x03\xf5\x3c\x52\xda\xc5\x4e\xd8\x25"},
694 { "\x30\xc8\x1c\x46\xa3\x5c\xe4\x11\xe5\xfb\xc1\x19\x1a\x0a\x52\xef",
696 "\x97\x40\x05\x1e\x9c\x5f\xec\xf6\x43\x44\xf7\xa8\x22\x60\xed\xcc" },
697 { "\xf6\x9f\x24\x45\xdf\x4f\x9b\x17\xad\x2b\x41\x7b\xe6\x6c\x37\x10",
699 "\x30\x4c\x65\x28\xf6\x59\xc7\x78\x66\xa5\x10\xd9\xc1\xd6\xae\x5e" },
702 { GCRY_CIPHER_AES192,
703 "\x8e\x73\xb0\xf7\xda\x0e\x64\x52\xc8\x10\xf3\x2b"
704 "\x80\x90\x79\xe5\x62\xf8\xea\xd2\x52\x2c\x6b\x7b",
705 "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
706 { { "\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\xe9\x3d\x7e\x11\x73\x93\x17\x2a",
708 "\xcd\xc8\x0d\x6f\xdd\xf1\x8c\xab\x34\xc2\x59\x09\xc9\x9a\x41\x74" },
709 { "\xae\x2d\x8a\x57\x1e\x03\xac\x9c\x9e\xb7\x6f\xac\x45\xaf\x8e\x51",
711 "\xfc\xc2\x8b\x8d\x4c\x63\x83\x7c\x09\xe8\x17\x00\xc1\x10\x04\x01" },
712 { "\x30\xc8\x1c\x46\xa3\x5c\xe4\x11\xe5\xfb\xc1\x19\x1a\x0a\x52\xef",
714 "\x8d\x9a\x9a\xea\xc0\xf6\x59\x6f\x55\x9c\x6d\x4d\xaf\x59\xa5\xf2" },
715 { "\xf6\x9f\x24\x45\xdf\x4f\x9b\x17\xad\x2b\x41\x7b\xe6\x6c\x37\x10",
717 "\x6d\x9f\x20\x08\x57\xca\x6c\x3e\x9c\xac\x52\x4b\xd9\xac\xc9\x2a" },
720 { GCRY_CIPHER_AES256,
721 "\x60\x3d\xeb\x10\x15\xca\x71\xbe\x2b\x73\xae\xf0\x85\x7d\x77\x81"
722 "\x1f\x35\x2c\x07\x3b\x61\x08\xd7\x2d\x98\x10\xa3\x09\x14\xdf\xf4",
723 "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
724 { { "\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\xe9\x3d\x7e\x11\x73\x93\x17\x2a",
726 "\xdc\x7e\x84\xbf\xda\x79\x16\x4b\x7e\xcd\x84\x86\x98\x5d\x38\x60" },
727 { "\xae\x2d\x8a\x57\x1e\x03\xac\x9c\x9e\xb7\x6f\xac\x45\xaf\x8e\x51",
729 "\x4f\xeb\xdc\x67\x40\xd2\x0b\x3a\xc8\x8f\x6a\xd8\x2a\x4f\xb0\x8d" },
730 { "\x30\xc8\x1c\x46\xa3\x5c\xe4\x11\xe5\xfb\xc1\x19\x1a\x0a\x52\xef",
732 "\x71\xab\x47\xa0\x86\xe8\x6e\xed\xf3\x9d\x1c\x5b\xba\x97\xc4\x08" },
733 { "\xf6\x9f\x24\x45\xdf\x4f\x9b\x17\xad\x2b\x41\x7b\xe6\x6c\x37\x10",
735 "\x01\x26\x14\x1d\x67\xf3\x7b\xe8\x53\x8f\x5a\x8b\xe7\x40\xe4\x84" }
739 gcry_cipher_hd_t hde, hdd;
740 unsigned char out[MAX_DATA_LEN];
741 int i, j, keylen, blklen;
742 gcry_error_t err = 0;
744 for (i = 0; i < sizeof (tv) / sizeof (tv[0]); i++)
746 err = gcry_cipher_open (&hde, tv[i].algo, GCRY_CIPHER_MODE_OFB, 0);
748 err = gcry_cipher_open (&hdd, tv[i].algo, GCRY_CIPHER_MODE_OFB, 0);
751 fail ("aes-ofb, grcy_open_cipher failed: %s\n", gpg_strerror (err));
755 keylen = gcry_cipher_get_algo_keylen(tv[i].algo);
758 fail ("aes-ofb, gcry_cipher_get_algo_keylen failed\n");
762 err = gcry_cipher_setkey (hde, tv[i].key, keylen);
764 err = gcry_cipher_setkey (hdd, tv[i].key, keylen);
767 fail ("aes-ofb, gcry_cipher_setkey failed: %s\n",
769 gcry_cipher_close (hde);
770 gcry_cipher_close (hdd);
774 blklen = gcry_cipher_get_algo_blklen(tv[i].algo);
777 fail ("aes-ofb, gcry_cipher_get_algo_blklen failed\n");
781 err = gcry_cipher_setiv (hde, tv[i].iv, blklen);
783 err = gcry_cipher_setiv (hdd, tv[i].iv, blklen);
786 fail ("aes-ofb, gcry_cipher_setiv failed: %s\n",
788 gcry_cipher_close (hde);
789 gcry_cipher_close (hdd);
793 for (j = 0; tv[i].data[j].inlen; j++)
795 err = gcry_cipher_encrypt (hde, out, MAX_DATA_LEN,
796 tv[i].data[j].plaintext,
797 tv[i].data[j].inlen);
800 fail ("aes-ofb, gcry_cipher_encrypt (%d, %d) failed: %s\n",
801 i, j, gpg_strerror (err));
802 gcry_cipher_close (hde);
803 gcry_cipher_close (hdd);
807 if (memcmp (tv[i].data[j].out, out, tv[i].data[j].inlen))
808 fail ("aes-ofb, encrypt mismatch entry %d:%d\n", i, j);
810 err = gcry_cipher_decrypt (hdd, out, tv[i].data[j].inlen, NULL, 0);
813 fail ("aes-ofb, gcry_cipher_decrypt (%d, %d) failed: %s\n",
814 i, j, gpg_strerror (err));
815 gcry_cipher_close (hde);
816 gcry_cipher_close (hdd);
820 if (memcmp (tv[i].data[j].plaintext, out, tv[i].data[j].inlen))
821 fail ("aes-ofb, decrypt mismatch entry %d:%d\n", i, j);
824 err = gcry_cipher_reset(hde);
826 err = gcry_cipher_reset(hdd);
829 fail ("aes-ofb, gcry_cipher_reset (%d, %d) failed: %s\n",
830 i, j, gpg_strerror (err));
831 gcry_cipher_close (hde);
832 gcry_cipher_close (hdd);
836 /* gcry_cipher_reset clears the IV */
837 err = gcry_cipher_setiv (hde, tv[i].iv, blklen);
839 err = gcry_cipher_setiv (hdd, tv[i].iv, blklen);
842 fail ("aes-ofb, gcry_cipher_setiv failed: %s\n",
844 gcry_cipher_close (hde);
845 gcry_cipher_close (hdd);
849 /* this time we encrypt and decrypt one byte at a time */
850 for (j = 0; tv[i].data[j].inlen; j++)
853 for (byteNum = 0; byteNum < tv[i].data[j].inlen; ++byteNum)
855 err = gcry_cipher_encrypt (hde, out+byteNum, 1,
856 (tv[i].data[j].plaintext) + byteNum,
860 fail ("aes-ofb, gcry_cipher_encrypt (%d, %d) failed: %s\n",
861 i, j, gpg_strerror (err));
862 gcry_cipher_close (hde);
863 gcry_cipher_close (hdd);
868 if (memcmp (tv[i].data[j].out, out, tv[i].data[j].inlen))
869 fail ("aes-ofb, encrypt mismatch entry %d:%d\n", i, j);
871 for (byteNum = 0; byteNum < tv[i].data[j].inlen; ++byteNum)
873 err = gcry_cipher_decrypt (hdd, out+byteNum, 1, NULL, 0);
876 fail ("aes-ofb, gcry_cipher_decrypt (%d, %d) failed: %s\n",
877 i, j, gpg_strerror (err));
878 gcry_cipher_close (hde);
879 gcry_cipher_close (hdd);
884 if (memcmp (tv[i].data[j].plaintext, out, tv[i].data[j].inlen))
885 fail ("aes-ofb, decrypt mismatch entry %d:%d\n", i, j);
888 gcry_cipher_close (hde);
889 gcry_cipher_close (hdd);
894 check_one_cipher (int algo, int mode, int flags)
898 unsigned char plain[16], in[16], out[16];
900 gcry_error_t err = 0;
902 memcpy (key, "0123456789abcdef.,;/[]{}-=ABCDEF", 32);
903 memcpy (plain, "foobar42FOOBAR17", 16);
905 keylen = gcry_cipher_get_algo_keylen (algo);
908 fail ("algo %d, mode %d, gcry_cipher_get_algo_keylen failed\n",
913 if (keylen < 40 / 8 || keylen > 32)
915 fail ("algo %d, mode %d, keylength problem (%d)\n", algo, mode, keylen);
919 err = gcry_cipher_open (&hd, algo, mode, flags);
922 fail ("algo %d, mode %d, grcy_open_cipher failed: %s\n",
923 algo, mode, gpg_strerror (err));
927 err = gcry_cipher_setkey (hd, key, keylen);
930 fail ("algo %d, mode %d, gcry_cipher_setkey failed: %s\n",
931 algo, mode, gpg_strerror (err));
932 gcry_cipher_close (hd);
936 err = gcry_cipher_encrypt (hd, out, 16, plain, 16);
939 fail ("algo %d, mode %d, gcry_cipher_encrypt failed: %s\n",
940 algo, mode, gpg_strerror (err));
941 gcry_cipher_close (hd);
945 gcry_cipher_reset (hd);
947 err = gcry_cipher_decrypt (hd, in, 16, out, 16);
950 fail ("algo %d, mode %d, gcry_cipher_decrypt failed: %s\n",
951 algo, mode, gpg_strerror (err));
952 gcry_cipher_close (hd);
956 if (memcmp (plain, in, 16))
957 fail ("algo %d, mode %d, encrypt-decrypt mismatch\n", algo, mode);
959 /* Again, using in-place encryption. */
960 gcry_cipher_reset (hd);
962 memcpy (out, plain, 16);
963 err = gcry_cipher_encrypt (hd, out, 16, NULL, 0);
966 fail ("algo %d, mode %d, in-place, gcry_cipher_encrypt failed: %s\n",
967 algo, mode, gpg_strerror (err));
968 gcry_cipher_close (hd);
972 gcry_cipher_reset (hd);
974 err = gcry_cipher_decrypt (hd, out, 16, NULL, 0);
977 fail ("algo %d, mode %d, in-place, gcry_cipher_decrypt failed: %s\n",
978 algo, mode, gpg_strerror (err));
979 gcry_cipher_close (hd);
983 if (memcmp (plain, out, 16))
984 fail ("algo %d, mode %d, in-place, encrypt-decrypt mismatch\n",algo, mode);
987 gcry_cipher_close (hd);
995 static int algos[] = {
997 GCRY_CIPHER_BLOWFISH,
1012 GCRY_CIPHER_TWOFISH,
1013 GCRY_CIPHER_TWOFISH128,
1016 GCRY_CIPHER_SERPENT128,
1017 GCRY_CIPHER_SERPENT192,
1018 GCRY_CIPHER_SERPENT256,
1021 GCRY_CIPHER_RFC2268_40,
1027 GCRY_CIPHER_CAMELLIA128,
1028 GCRY_CIPHER_CAMELLIA192,
1029 GCRY_CIPHER_CAMELLIA256,
1033 static int algos2[] = {
1035 GCRY_CIPHER_ARCFOUR,
1042 fprintf (stderr, "Starting Cipher checks.\n");
1043 for (i = 0; algos[i]; i++)
1045 if (gcry_cipher_test_algo (algos[i]) && in_fips_mode)
1048 fprintf (stderr, " algorithm %d not available in fips mode\n",
1053 fprintf (stderr, " checking %s [%i]\n",
1054 gcry_cipher_algo_name (algos[i]),
1055 gcry_cipher_map_name (gcry_cipher_algo_name (algos[i])));
1057 check_one_cipher (algos[i], GCRY_CIPHER_MODE_ECB, 0);
1058 check_one_cipher (algos[i], GCRY_CIPHER_MODE_CFB, 0);
1059 check_one_cipher (algos[i], GCRY_CIPHER_MODE_OFB, 0);
1060 check_one_cipher (algos[i], GCRY_CIPHER_MODE_CBC, 0);
1061 check_one_cipher (algos[i], GCRY_CIPHER_MODE_CBC, GCRY_CIPHER_CBC_CTS);
1062 check_one_cipher (algos[i], GCRY_CIPHER_MODE_CTR, 0);
1065 for (i = 0; algos2[i]; i++)
1067 if (gcry_cipher_test_algo (algos[i]) && in_fips_mode)
1070 fprintf (stderr, " algorithm %d not available in fips mode\n",
1075 fprintf (stderr, " checking `%s'\n",
1076 gcry_cipher_algo_name (algos2[i]));
1078 check_one_cipher (algos2[i], GCRY_CIPHER_MODE_STREAM, 0);
1080 /* we have now run all cipher's selftests */
1083 fprintf (stderr, "Completed Cipher checks.\n");
1085 /* TODO: add some extra encryption to test the higher level functions */
1091 check_one_md (int algo, const char *data, int len, const char *expect)
1093 gcry_md_hd_t hd, hd2;
1097 gcry_error_t err = 0;
1099 err = gcry_md_open (&hd, algo, 0);
1102 fail ("algo %d, grcy_md_open failed: %s\n", algo, gpg_strerror (err));
1106 mdlen = gcry_md_get_algo_dlen (algo);
1107 if (mdlen < 1 || mdlen > 500)
1109 fail ("algo %d, grcy_md_get_algo_dlen failed: %d\n", algo, mdlen);
1113 if (*data == '!' && !data[1])
1114 { /* hash one million times a "a" */
1117 /* Write in odd size chunks so that we test the buffering. */
1118 memset (aaa, 'a', 1000);
1119 for (i = 0; i < 1000; i++)
1120 gcry_md_write (hd, aaa, 1000);
1123 gcry_md_write (hd, data, len);
1125 err = gcry_md_copy (&hd2, hd);
1128 fail ("algo %d, gcry_md_copy failed: %s\n", algo, gpg_strerror (err));
1133 p = gcry_md_read (hd2, algo);
1135 if (memcmp (p, expect, mdlen))
1137 printf ("computed: ");
1138 for (i = 0; i < mdlen; i++)
1139 printf ("%02x ", p[i] & 0xFF);
1140 printf ("\nexpected: ");
1141 for (i = 0; i < mdlen; i++)
1142 printf ("%02x ", expect[i] & 0xFF);
1145 fail ("algo %d, digest mismatch\n", algo);
1148 gcry_md_close (hd2);
1153 check_digests (void)
1163 "\x31\xD6\xCF\xE0\xD1\x6A\xE9\x31\xB7\x3C\x59\xD7\xE0\xC0\x89\xC0" },
1165 "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb\x24" },
1166 { GCRY_MD_MD4, "message digest",
1167 "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01\x4b" },
1169 "\xD4\x1D\x8C\xD9\x8F\x00\xB2\x04\xE9\x80\x09\x98\xEC\xF8\x42\x7E" },
1171 "\x0C\xC1\x75\xB9\xC0\xF1\xB6\xA8\x31\xC3\x99\xE2\x69\x77\x26\x61" },
1172 { GCRY_MD_MD5, "abc",
1173 "\x90\x01\x50\x98\x3C\xD2\x4F\xB0\xD6\x96\x3F\x7D\x28\xE1\x7F\x72" },
1174 { GCRY_MD_MD5, "message digest",
1175 "\xF9\x6B\x69\x7D\x7C\xB7\x93\x8D\x52\x5A\x2F\x31\xAA\xF1\x61\xD0" },
1176 { GCRY_MD_SHA1, "abc",
1177 "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
1178 "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D" },
1180 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
1181 "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE"
1182 "\x4A\xA1\xF9\x51\x29\xE5\xE5\x46\x70\xF1" },
1183 { GCRY_MD_SHA1, "!" /* kludge for "a"*1000000 */ ,
1184 "\x34\xAA\x97\x3C\xD4\xC4\xDA\xA4\xF6\x1E"
1185 "\xEB\x2B\xDB\xAD\x27\x31\x65\x34\x01\x6F" },
1187 { GCRY_MD_SHA224, "abc",
1188 "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2\x55\xb3"
1189 "\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7" },
1191 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
1192 "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01\x50"
1193 "\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25" },
1194 { GCRY_MD_SHA224, "!",
1195 "\x20\x79\x46\x55\x98\x0c\x91\xd8\xbb\xb4\xc1\xea\x97\x61\x8a\x4b"
1196 "\xf0\x3f\x42\x58\x19\x48\xb2\xee\x4e\xe7\xad\x67" },
1197 { GCRY_MD_SHA256, "abc",
1198 "\xba\x78\x16\xbf\x8f\x01\xcf\xea\x41\x41\x40\xde\x5d\xae\x22\x23"
1199 "\xb0\x03\x61\xa3\x96\x17\x7a\x9c\xb4\x10\xff\x61\xf2\x00\x15\xad" },
1201 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
1202 "\x24\x8d\x6a\x61\xd2\x06\x38\xb8\xe5\xc0\x26\x93\x0c\x3e\x60\x39"
1203 "\xa3\x3c\xe4\x59\x64\xff\x21\x67\xf6\xec\xed\xd4\x19\xdb\x06\xc1" },
1204 { GCRY_MD_SHA256, "!",
1205 "\xcd\xc7\x6e\x5c\x99\x14\xfb\x92\x81\xa1\xc7\xe2\x84\xd7\x3e\x67"
1206 "\xf1\x80\x9a\x48\xa4\x97\x20\x0e\x04\x6d\x39\xcc\xc7\x11\x2c\xd0" },
1207 { GCRY_MD_SHA384, "abc",
1208 "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50\x07"
1209 "\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff\x5b\xed"
1210 "\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34\xc8\x25\xa7" },
1211 { GCRY_MD_SHA512, "abc",
1212 "\xDD\xAF\x35\xA1\x93\x61\x7A\xBA\xCC\x41\x73\x49\xAE\x20\x41\x31"
1213 "\x12\xE6\xFA\x4E\x89\xA9\x7E\xA2\x0A\x9E\xEE\xE6\x4B\x55\xD3\x9A"
1214 "\x21\x92\x99\x2A\x27\x4F\xC1\xA8\x36\xBA\x3C\x23\xA3\xFE\xEB\xBD"
1215 "\x45\x4D\x44\x23\x64\x3C\xE8\x0E\x2A\x9A\xC9\x4F\xA5\x4C\xA4\x9F" },
1216 { GCRY_MD_RMD160, "",
1217 "\x9c\x11\x85\xa5\xc5\xe9\xfc\x54\x61\x28"
1218 "\x08\x97\x7e\xe8\xf5\x48\xb2\x25\x8d\x31" },
1219 { GCRY_MD_RMD160, "a",
1220 "\x0b\xdc\x9d\x2d\x25\x6b\x3e\xe9\xda\xae"
1221 "\x34\x7b\xe6\xf4\xdc\x83\x5a\x46\x7f\xfe" },
1222 { GCRY_MD_RMD160, "abc",
1223 "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04"
1224 "\x4a\x8e\x98\xc6\xb0\x87\xf1\x5a\x0b\xfc" },
1225 { GCRY_MD_RMD160, "message digest",
1226 "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8"
1227 "\x81\xb1\x23\xa8\x5f\xfa\x21\x59\x5f\x36" },
1228 { GCRY_MD_CRC32, "", "\x00\x00\x00\x00" },
1229 { GCRY_MD_CRC32, "foo", "\x8c\x73\x65\x21" },
1230 { GCRY_MD_CRC32_RFC1510, "", "\x00\x00\x00\x00" },
1231 { GCRY_MD_CRC32_RFC1510, "foo", "\x73\x32\xbc\x33" },
1232 { GCRY_MD_CRC32_RFC1510, "test0123456789", "\xb8\x3e\x88\xd6" },
1233 { GCRY_MD_CRC32_RFC1510, "MASSACHVSETTS INSTITVTE OF TECHNOLOGY",
1234 "\xe3\x41\x80\xf7" },
1236 { GCRY_MD_CRC32_RFC1510, "\x80\x00", "\x3b\x83\x98\x4b" },
1237 { GCRY_MD_CRC32_RFC1510, "\x00\x08", "\x0e\xdb\x88\x32" },
1238 { GCRY_MD_CRC32_RFC1510, "\x00\x80", "\xed\xb8\x83\x20" },
1240 { GCRY_MD_CRC32_RFC1510, "\x80", "\xed\xb8\x83\x20" },
1242 { GCRY_MD_CRC32_RFC1510, "\x80\x00\x00\x00", "\xed\x59\xb6\x3b" },
1243 { GCRY_MD_CRC32_RFC1510, "\x00\x00\x00\x01", "\x77\x07\x30\x96" },
1245 { GCRY_MD_CRC24_RFC2440, "", "\xb7\x04\xce" },
1246 { GCRY_MD_CRC24_RFC2440, "foo", "\x4f\xc2\x55" },
1247 { GCRY_MD_TIGER, "",
1248 "\x24\xF0\x13\x0C\x63\xAC\x93\x32\x16\x16\x6E\x76"
1249 "\xB1\xBB\x92\x5F\xF3\x73\xDE\x2D\x49\x58\x4E\x7A" },
1250 { GCRY_MD_TIGER, "abc",
1251 "\xF2\x58\xC1\xE8\x84\x14\xAB\x2A\x52\x7A\xB5\x41"
1252 "\xFF\xC5\xB8\xBF\x93\x5F\x7B\x95\x1C\x13\x29\x51" },
1253 { GCRY_MD_TIGER, "Tiger",
1254 "\x9F\x00\xF5\x99\x07\x23\x00\xDD\x27\x6A\xBB\x38"
1255 "\xC8\xEB\x6D\xEC\x37\x79\x0C\x11\x6F\x9D\x2B\xDF" },
1256 { GCRY_MD_TIGER, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefg"
1257 "hijklmnopqrstuvwxyz0123456789+-",
1258 "\x87\xFB\x2A\x90\x83\x85\x1C\xF7\x47\x0D\x2C\xF8"
1259 "\x10\xE6\xDF\x9E\xB5\x86\x44\x50\x34\xA5\xA3\x86" },
1260 { GCRY_MD_TIGER, "ABCDEFGHIJKLMNOPQRSTUVWXYZ=abcdef"
1261 "ghijklmnopqrstuvwxyz+0123456789",
1262 "\x46\x7D\xB8\x08\x63\xEB\xCE\x48\x8D\xF1\xCD\x12"
1263 "\x61\x65\x5D\xE9\x57\x89\x65\x65\x97\x5F\x91\x97" },
1264 { GCRY_MD_TIGER, "Tiger - A Fast New Hash Function, "
1265 "by Ross Anderson and Eli Biham",
1266 "\x0C\x41\x0A\x04\x29\x68\x86\x8A\x16\x71\xDA\x5A"
1267 "\x3F\xD2\x9A\x72\x5E\xC1\xE4\x57\xD3\xCD\xB3\x03" },
1268 { GCRY_MD_TIGER, "Tiger - A Fast New Hash Function, "
1269 "by Ross Anderson and Eli Biham, proceedings of Fa"
1270 "st Software Encryption 3, Cambridge.",
1271 "\xEB\xF5\x91\xD5\xAF\xA6\x55\xCE\x7F\x22\x89\x4F"
1272 "\xF8\x7F\x54\xAC\x89\xC8\x11\xB6\xB0\xDA\x31\x93" },
1273 { GCRY_MD_TIGER, "Tiger - A Fast New Hash Function, "
1274 "by Ross Anderson and Eli Biham, proceedings of Fa"
1275 "st Software Encryption 3, Cambridge, 1996.",
1276 "\x3D\x9A\xEB\x03\xD1\xBD\x1A\x63\x57\xB2\x77\x4D"
1277 "\xFD\x6D\x5B\x24\xDD\x68\x15\x1D\x50\x39\x74\xFC" },
1278 { GCRY_MD_TIGER, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefgh"
1279 "ijklmnopqrstuvwxyz0123456789+-ABCDEFGHIJKLMNOPQRS"
1280 "TUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-",
1281 "\x00\xB8\x3E\xB4\xE5\x34\x40\xC5\x76\xAC\x6A\xAE"
1282 "\xE0\xA7\x48\x58\x25\xFD\x15\xE7\x0A\x59\xFF\xE4" },
1283 { GCRY_MD_WHIRLPOOL, "",
1284 "\x19\xFA\x61\xD7\x55\x22\xA4\x66\x9B\x44\xE3\x9C\x1D\x2E\x17\x26"
1285 "\xC5\x30\x23\x21\x30\xD4\x07\xF8\x9A\xFE\xE0\x96\x49\x97\xF7\xA7"
1286 "\x3E\x83\xBE\x69\x8B\x28\x8F\xEB\xCF\x88\xE3\xE0\x3C\x4F\x07\x57"
1287 "\xEA\x89\x64\xE5\x9B\x63\xD9\x37\x08\xB1\x38\xCC\x42\xA6\x6E\xB3" },
1288 { GCRY_MD_WHIRLPOOL, "a",
1289 "\x8A\xCA\x26\x02\x79\x2A\xEC\x6F\x11\xA6\x72\x06\x53\x1F\xB7\xD7"
1290 "\xF0\xDF\xF5\x94\x13\x14\x5E\x69\x73\xC4\x50\x01\xD0\x08\x7B\x42"
1291 "\xD1\x1B\xC6\x45\x41\x3A\xEF\xF6\x3A\x42\x39\x1A\x39\x14\x5A\x59"
1292 "\x1A\x92\x20\x0D\x56\x01\x95\xE5\x3B\x47\x85\x84\xFD\xAE\x23\x1A" },
1293 { GCRY_MD_WHIRLPOOL, "a",
1294 "\x8A\xCA\x26\x02\x79\x2A\xEC\x6F\x11\xA6\x72\x06\x53\x1F\xB7\xD7"
1295 "\xF0\xDF\xF5\x94\x13\x14\x5E\x69\x73\xC4\x50\x01\xD0\x08\x7B\x42"
1296 "\xD1\x1B\xC6\x45\x41\x3A\xEF\xF6\x3A\x42\x39\x1A\x39\x14\x5A\x59"
1297 "\x1A\x92\x20\x0D\x56\x01\x95\xE5\x3B\x47\x85\x84\xFD\xAE\x23\x1A" },
1298 { GCRY_MD_WHIRLPOOL,
1299 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
1300 "\xDC\x37\xE0\x08\xCF\x9E\xE6\x9B\xF1\x1F\x00\xED\x9A\xBA\x26\x90"
1301 "\x1D\xD7\xC2\x8C\xDE\xC0\x66\xCC\x6A\xF4\x2E\x40\xF8\x2F\x3A\x1E"
1302 "\x08\xEB\xA2\x66\x29\x12\x9D\x8F\xB7\xCB\x57\x21\x1B\x92\x81\xA6"
1303 "\x55\x17\xCC\x87\x9D\x7B\x96\x21\x42\xC6\x5F\x5A\x7A\xF0\x14\x67" },
1304 { GCRY_MD_WHIRLPOOL,
1306 "\x0C\x99\x00\x5B\xEB\x57\xEF\xF5\x0A\x7C\xF0\x05\x56\x0D\xDF\x5D"
1307 "\x29\x05\x7F\xD8\x6B\x20\xBF\xD6\x2D\xEC\xA0\xF1\xCC\xEA\x4A\xF5"
1308 "\x1F\xC1\x54\x90\xED\xDC\x47\xAF\x32\xBB\x2B\x66\xC3\x4F\xF9\xAD"
1309 "\x8C\x60\x08\xAD\x67\x7F\x77\x12\x69\x53\xB2\x26\xE4\xED\x8B\x01" },
1315 fprintf (stderr, "Starting hash checks.\n");
1317 for (i = 0; algos[i].md; i++)
1319 if (gcry_md_test_algo (algos[i].md) && in_fips_mode)
1322 fprintf (stderr, " algorithm %d not available in fips mode\n",
1327 fprintf (stderr, " checking %s [%i] for length %zi\n",
1328 gcry_md_algo_name (algos[i].md),
1330 !strcmp (algos[i].data, "!")?
1331 1000000 : strlen(algos[i].data));
1333 check_one_md (algos[i].md, algos[i].data, strlen (algos[i].data),
1338 fprintf (stderr, "Completed hash checks.\n");
1342 check_one_hmac (int algo, const char *data, int datalen,
1343 const char *key, int keylen, const char *expect)
1345 gcry_md_hd_t hd, hd2;
1349 gcry_error_t err = 0;
1351 err = gcry_md_open (&hd, algo, GCRY_MD_FLAG_HMAC);
1354 fail ("algo %d, grcy_md_open failed: %s\n", algo, gpg_strerror (err));
1358 mdlen = gcry_md_get_algo_dlen (algo);
1359 if (mdlen < 1 || mdlen > 500)
1361 fail ("algo %d, grcy_md_get_algo_dlen failed: %d\n", algo, mdlen);
1365 gcry_md_setkey( hd, key, keylen );
1367 gcry_md_write (hd, data, datalen);
1369 err = gcry_md_copy (&hd2, hd);
1372 fail ("algo %d, gcry_md_copy failed: %s\n", algo, gpg_strerror (err));
1377 p = gcry_md_read (hd2, algo);
1379 fail("algo %d, hmac gcry_md_read failed\n", algo);
1381 if (memcmp (p, expect, mdlen))
1383 printf ("computed: ");
1384 for (i = 0; i < mdlen; i++)
1385 printf ("%02x ", p[i] & 0xFF);
1386 printf ("\nexpected: ");
1387 for (i = 0; i < mdlen; i++)
1388 printf ("%02x ", expect[i] & 0xFF);
1391 fail ("algo %d, digest mismatch\n", algo);
1394 gcry_md_close (hd2);
1408 { GCRY_MD_MD5, "what do ya want for nothing?", "Jefe",
1409 "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7\x38" },
1412 "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
1413 "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc\x9d" },
1415 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
1416 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
1417 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
1418 "\xdd\xdd\xdd\xdd\xdd",
1419 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA",
1420 "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3\xf6" },
1422 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
1423 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
1424 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
1425 "\xcd\xcd\xcd\xcd\xcd",
1426 "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
1427 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19",
1428 "\x69\x7e\xaf\x0a\xca\x3a\x3a\xea\x3a\x75\x16\x47\x46\xff\xaa\x79" },
1429 { GCRY_MD_MD5, "Test With Truncation",
1430 "\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c",
1431 "\x56\x46\x1e\xf2\x34\x2e\xdc\x00\xf9\xba\xb9\x95\x69\x0e\xfd\x4c" },
1432 { GCRY_MD_MD5, "Test Using Larger Than Block-Size Key - Hash Key First",
1433 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1434 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1435 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1436 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1437 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1438 "\xaa\xaa\xaa\xaa\xaa",
1439 "\x6b\x1a\xb7\xfe\x4b\xd7\xbf\x8f\x0b\x62\xe6\xce\x61\xb9\xd0\xcd" },
1441 "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data",
1442 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1443 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1444 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1445 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1446 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1447 "\xaa\xaa\xaa\xaa\xaa",
1448 "\x6f\x63\x0f\xad\x67\xcd\xa0\xee\x1f\xb1\xf5\x62\xdb\x3a\xa5\x3e", },
1449 { GCRY_MD_SHA256, "what do ya want for nothing?", "Jefe",
1450 "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75\xc7\x5a"
1451 "\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec\x38\x43" },
1454 "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
1456 "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1\x2b\x88"
1457 "\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32\xcf\xf7" },
1459 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
1460 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
1461 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
1462 "\xdd\xdd\xdd\xdd\xdd",
1463 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
1465 "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81\xa7"
1466 "\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5\x65\xfe" },
1468 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
1469 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
1470 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
1471 "\xcd\xcd\xcd\xcd\xcd",
1472 "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
1473 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19",
1474 "\x82\x55\x8a\x38\x9a\x44\x3c\x0e\xa4\xcc\x81\x98\x99\xf2\x08"
1475 "\x3a\x85\xf0\xfa\xa3\xe5\x78\xf8\x07\x7a\x2e\x3f\xf4\x67\x29\x66\x5b" },
1477 "Test Using Larger Than Block-Size Key - Hash Key First",
1478 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1479 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1480 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1481 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1482 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1483 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1484 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1485 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1487 "\x60\xe4\x31\x59\x1e\xe0\xb6\x7f\x0d\x8a\x26\xaa\xcb\xf5\xb7\x7f"
1488 "\x8e\x0b\xc6\x21\x37\x28\xc5\x14\x05\x46\x04\x0f\x0e\xe3\x7f\x54" },
1490 "This is a test using a larger than block-size key and a larger than block-size data. The key needs to be hashed before being used by the HMAC algorithm.",
1491 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1492 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1493 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1494 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1495 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1496 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1497 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1498 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1500 "\x9b\x09\xff\xa7\x1b\x94\x2f\xcb\x27\x63\x5f\xbc\xd5\xb0\xe9\x44"
1501 "\xbf\xdc\x63\x64\x4f\x07\x13\x93\x8a\x7f\x51\x53\x5c\x3a\x35\xe2" },
1502 { GCRY_MD_SHA224, "what do ya want for nothing?", "Jefe",
1503 "\xa3\x0e\x01\x09\x8b\xc6\xdb\xbf\x45\x69\x0f\x3a\x7e\x9e\x6d\x0f"
1504 "\x8b\xbe\xa2\xa3\x9e\x61\x48\x00\x8f\xd0\x5e\x44" },
1507 "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
1509 "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3\x3f\x47"
1510 "\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22" },
1512 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
1513 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
1514 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
1515 "\xdd\xdd\xdd\xdd\xdd",
1516 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
1518 "\x7f\xb3\xcb\x35\x88\xc6\xc1\xf6\xff\xa9\x69\x4d\x7d\x6a\xd2\x64"
1519 "\x93\x65\xb0\xc1\xf6\x5d\x69\xd1\xec\x83\x33\xea" },
1521 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
1522 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
1523 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
1524 "\xcd\xcd\xcd\xcd\xcd",
1525 "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
1526 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19",
1527 "\x6c\x11\x50\x68\x74\x01\x3c\xac\x6a\x2a\xbc\x1b\xb3\x82\x62"
1528 "\x7c\xec\x6a\x90\xd8\x6e\xfc\x01\x2d\xe7\xaf\xec\x5a" },
1530 "Test Using Larger Than Block-Size Key - Hash Key First",
1531 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1532 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1533 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1534 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1535 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1536 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1537 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1538 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1540 "\x95\xe9\xa0\xdb\x96\x20\x95\xad\xae\xbe\x9b\x2d\x6f\x0d\xbc\xe2"
1541 "\xd4\x99\xf1\x12\xf2\xd2\xb7\x27\x3f\xa6\x87\x0e" },
1543 "This is a test using a larger than block-size key and a larger than block-size data. The key needs to be hashed before being used by the HMAC algorithm.",
1544 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1545 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1546 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1547 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1548 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1549 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1550 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1551 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1553 "\x3a\x85\x41\x66\xac\x5d\x9f\x02\x3f\x54\xd5\x17\xd0\xb3\x9d\xbd"
1554 "\x94\x67\x70\xdb\x9c\x2b\x95\xc9\xf6\xf5\x65\xd1" },
1555 { GCRY_MD_SHA384, "what do ya want for nothing?", "Jefe",
1556 "\xaf\x45\xd2\xe3\x76\x48\x40\x31\x61\x7f\x78\xd2\xb5\x8a\x6b\x1b"
1557 "\x9c\x7e\xf4\x64\xf5\xa0\x1b\x47\xe4\x2e\xc3\x73\x63\x22\x44\x5e"
1558 "\x8e\x22\x40\xca\x5e\x69\xe2\xc7\x8b\x32\x39\xec\xfa\xb2\x16\x49" },
1561 "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
1563 "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90\x7f\x15"
1564 "\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb\xc5\x9c\xfa\xea"
1565 "\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2\xfa\x9c\xb6" },
1567 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
1568 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
1569 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
1570 "\xdd\xdd\xdd\xdd\xdd",
1571 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
1573 "\x88\x06\x26\x08\xd3\xe6\xad\x8a\x0a\xa2\xac\xe0\x14\xc8\xa8\x6f"
1574 "\x0a\xa6\x35\xd9\x47\xac\x9f\xeb\xe8\x3e\xf4\xe5\x59\x66\x14\x4b"
1575 "\x2a\x5a\xb3\x9d\xc1\x38\x14\xb9\x4e\x3a\xb6\xe1\x01\xa3\x4f\x27" },
1577 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
1578 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
1579 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
1580 "\xcd\xcd\xcd\xcd\xcd",
1581 "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
1582 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19",
1583 "\x3e\x8a\x69\xb7\x78\x3c\x25\x85\x19\x33\xab\x62\x90\xaf\x6c\xa7"
1584 "\x7a\x99\x81\x48\x08\x50\x00\x9c\xc5\x57\x7c\x6e\x1f\x57\x3b\x4e"
1585 "\x68\x01\xdd\x23\xc4\xa7\xd6\x79\xcc\xf8\xa3\x86\xc6\x74\xcf\xfb" },
1587 "Test Using Larger Than Block-Size Key - Hash Key First",
1588 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1589 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1590 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1591 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1592 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1593 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1594 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1595 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1597 "\x4e\xce\x08\x44\x85\x81\x3e\x90\x88\xd2\xc6\x3a\x04\x1b\xc5\xb4"
1598 "\x4f\x9e\xf1\x01\x2a\x2b\x58\x8f\x3c\xd1\x1f\x05\x03\x3a\xc4\xc6"
1599 "\x0c\x2e\xf6\xab\x40\x30\xfe\x82\x96\x24\x8d\xf1\x63\xf4\x49\x52" },
1601 "This is a test using a larger than block-size key and a larger than block-size data. The key needs to be hashed before being used by the HMAC algorithm.",
1602 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1603 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1604 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1605 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1606 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1607 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1608 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1609 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1611 "\x66\x17\x17\x8e\x94\x1f\x02\x0d\x35\x1e\x2f\x25\x4e\x8f\xd3\x2c"
1612 "\x60\x24\x20\xfe\xb0\xb8\xfb\x9a\xdc\xce\xbb\x82\x46\x1e\x99\xc5"
1613 "\xa6\x78\xcc\x31\xe7\x99\x17\x6d\x38\x60\xe6\x11\x0c\x46\x52\x3e" },
1614 { GCRY_MD_SHA512, "what do ya want for nothing?", "Jefe",
1615 "\x16\x4b\x7a\x7b\xfc\xf8\x19\xe2\xe3\x95\xfb\xe7\x3b\x56\xe0\xa3"
1616 "\x87\xbd\x64\x22\x2e\x83\x1f\xd6\x10\x27\x0c\xd7\xea\x25\x05\x54"
1617 "\x97\x58\xbf\x75\xc0\x5a\x99\x4a\x6d\x03\x4f\x65\xf8\xf0\xe6\xfd"
1618 "\xca\xea\xb1\xa3\x4d\x4a\x6b\x4b\x63\x6e\x07\x0a\x38\xbc\xe7\x37" },
1621 "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
1623 "\x87\xaa\x7c\xde\xa5\xef\x61\x9d\x4f\xf0\xb4\x24\x1a\x1d\x6c\xb0"
1624 "\x23\x79\xf4\xe2\xce\x4e\xc2\x78\x7a\xd0\xb3\x05\x45\xe1\x7c\xde"
1625 "\xda\xa8\x33\xb7\xd6\xb8\xa7\x02\x03\x8b\x27\x4e\xae\xa3\xf4\xe4"
1626 "\xbe\x9d\x91\x4e\xeb\x61\xf1\x70\x2e\x69\x6c\x20\x3a\x12\x68\x54" },
1628 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
1629 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
1630 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
1631 "\xdd\xdd\xdd\xdd\xdd",
1632 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
1634 "\xfa\x73\xb0\x08\x9d\x56\xa2\x84\xef\xb0\xf0\x75\x6c\x89\x0b\xe9"
1635 "\xb1\xb5\xdb\xdd\x8e\xe8\x1a\x36\x55\xf8\x3e\x33\xb2\x27\x9d\x39"
1636 "\xbf\x3e\x84\x82\x79\xa7\x22\xc8\x06\xb4\x85\xa4\x7e\x67\xc8\x07"
1637 "\xb9\x46\xa3\x37\xbe\xe8\x94\x26\x74\x27\x88\x59\xe1\x32\x92\xfb" },
1639 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
1640 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
1641 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
1642 "\xcd\xcd\xcd\xcd\xcd",
1643 "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
1644 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19",
1645 "\xb0\xba\x46\x56\x37\x45\x8c\x69\x90\xe5\xa8\xc5\xf6\x1d\x4a\xf7"
1646 "\xe5\x76\xd9\x7f\xf9\x4b\x87\x2d\xe7\x6f\x80\x50\x36\x1e\xe3\xdb"
1647 "\xa9\x1c\xa5\xc1\x1a\xa2\x5e\xb4\xd6\x79\x27\x5c\xc5\x78\x80\x63"
1648 "\xa5\xf1\x97\x41\x12\x0c\x4f\x2d\xe2\xad\xeb\xeb\x10\xa2\x98\xdd" },
1650 "Test Using Larger Than Block-Size Key - Hash Key First",
1651 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1652 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1653 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1654 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1655 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1656 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1657 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1658 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1660 "\x80\xb2\x42\x63\xc7\xc1\xa3\xeb\xb7\x14\x93\xc1\xdd\x7b\xe8\xb4"
1661 "\x9b\x46\xd1\xf4\x1b\x4a\xee\xc1\x12\x1b\x01\x37\x83\xf8\xf3\x52"
1662 "\x6b\x56\xd0\x37\xe0\x5f\x25\x98\xbd\x0f\xd2\x21\x5d\x6a\x1e\x52"
1663 "\x95\xe6\x4f\x73\xf6\x3f\x0a\xec\x8b\x91\x5a\x98\x5d\x78\x65\x98" },
1665 "This is a test using a larger than block-size key and a larger than block-size data. The key needs to be hashed before being used by the HMAC algorithm.",
1666 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1667 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1668 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1669 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1670 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1671 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1672 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1673 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
1675 "\xe3\x7b\x6a\x77\x5d\xc8\x7d\xba\xa4\xdf\xa9\xf9\x6e\x5e\x3f\xfd"
1676 "\xde\xbd\x71\xf8\x86\x72\x89\x86\x5d\xf5\xa3\x2d\x20\xcd\xc9\x44"
1677 "\xb6\x02\x2c\xac\x3c\x49\x82\xb1\x0d\x5e\xeb\x55\xc3\xe4\xde\x15"
1678 "\x13\x46\x76\xfb\x6d\xe0\x44\x60\x65\xc9\x74\x40\xfa\x8c\x6a\x58" },
1684 fprintf (stderr, "Starting hashed MAC checks.\n");
1686 for (i = 0; algos[i].md; i++)
1688 if (gcry_md_test_algo (algos[i].md) && in_fips_mode)
1691 fprintf (stderr, " algorithm %d not available in fips mode\n",
1696 fprintf (stderr, " checking %s [%i] for length %zi\n",
1697 gcry_md_algo_name (algos[i].md),
1699 strlen(algos[i].data));
1701 check_one_hmac (algos[i].md, algos[i].data, strlen (algos[i].data),
1702 algos[i].key, strlen(algos[i].key),
1707 fprintf (stderr, "Completed hashed MAC checks.\n");
1710 /* Check that the signature SIG matches the hash HASH. PKEY is the
1711 public key used for the verification. BADHASH is a hasvalue which
1712 should; result in a bad signature status. */
1714 verify_one_signature (gcry_sexp_t pkey, gcry_sexp_t hash,
1715 gcry_sexp_t badhash, gcry_sexp_t sig)
1719 rc = gcry_pk_verify (sig, hash, pkey);
1721 fail ("gcry_pk_verify failed: %s\n", gpg_strerror (rc));
1722 rc = gcry_pk_verify (sig, badhash, pkey);
1723 if (gcry_err_code (rc) != GPG_ERR_BAD_SIGNATURE)
1724 fail ("gcry_pk_verify failed to detect a bad signature: %s\n",
1729 /* Test the public key sign function using the private ket SKEY. PKEY
1730 is used for verification. */
1732 check_pubkey_sign (int n, gcry_sexp_t skey, gcry_sexp_t pkey)
1735 gcry_sexp_t sig, badhash, hash;
1737 static const char baddata[] =
1738 "(data\n (flags pkcs1)\n"
1739 " (hash sha1 #11223344556677889900AABBCCDDEEFF10203041#))\n";
1746 { "(data\n (flags pkcs1)\n"
1747 " (hash sha1 #11223344556677889900AABBCCDDEEFF10203040#))\n",
1749 /* This test is to see whether hash algorithms not hard wired in
1750 pubkey.c are detected: */
1751 { "(data\n (flags pkcs1)\n"
1752 " (hash oid.1.3.14.3.2.29 "
1753 " #11223344556677889900AABBCCDDEEFF10203040#))\n",
1755 { "(data\n (flags )\n"
1756 " (hash sha1 #11223344556677889900AABBCCDDEEFF10203040#))\n",
1758 { "(data\n (flags pkcs1)\n"
1759 " (hash foo #11223344556677889900AABBCCDDEEFF10203040#))\n",
1760 GPG_ERR_DIGEST_ALGO },
1761 { "(data\n (flags )\n" " (value #11223344556677889900AA#))\n",
1763 { "(data\n (flags )\n" " (value #0090223344556677889900AA#))\n",
1765 { "(data\n (flags raw)\n" " (value #11223344556677889900AA#))\n",
1767 { "(data\n (flags pkcs1)\n"
1768 " (value #11223344556677889900AA#))\n",
1770 { "(data\n (flags raw foo)\n"
1771 " (value #11223344556677889900AA#))\n",
1778 rc = gcry_sexp_sscan (&badhash, NULL, baddata, strlen (baddata));
1780 die ("converting data failed: %s\n", gpg_strerror (rc));
1782 for (dataidx = 0; datas[dataidx].data; dataidx++)
1785 fprintf (stderr, " signature test %d\n", dataidx);
1787 rc = gcry_sexp_sscan (&hash, NULL, datas[dataidx].data,
1788 strlen (datas[dataidx].data));
1790 die ("converting data failed: %s\n", gpg_strerror (rc));
1792 rc = gcry_pk_sign (&sig, hash, skey);
1793 if (gcry_err_code (rc) != datas[dataidx].expected_rc)
1794 fail ("gcry_pk_sign failed: %s\n", gpg_strerror (rc));
1797 verify_one_signature (pkey, hash, badhash, sig);
1799 gcry_sexp_release (sig);
1801 gcry_sexp_release (hash);
1805 gcry_sexp_release (badhash);
1809 check_pubkey_grip (int n, const unsigned char *grip,
1810 gcry_sexp_t skey, gcry_sexp_t pkey)
1812 unsigned char sgrip[20], pgrip[20];
1814 if (!gcry_pk_get_keygrip (skey, sgrip))
1815 die ("get keygrip for private RSA key failed\n");
1816 if (!gcry_pk_get_keygrip (pkey, pgrip))
1817 die ("[%i] get keygrip for public RSA key failed\n", n);
1818 if (memcmp (sgrip, pgrip, 20))
1819 fail ("[%i] keygrips don't match\n", n);
1820 if (memcmp (sgrip, grip, 20))
1821 fail ("wrong keygrip for RSA key\n");
1825 do_check_one_pubkey (int n, gcry_sexp_t skey, gcry_sexp_t pkey,
1826 const unsigned char *grip, int flags)
1828 if (flags & FLAG_SIGN)
1829 check_pubkey_sign (n, skey, pkey);
1830 if (grip && (flags & FLAG_GRIP))
1831 check_pubkey_grip (n, grip, skey, pkey);
1835 check_one_pubkey (int n, test_spec_pubkey_t spec)
1837 gcry_error_t err = GPG_ERR_NO_ERROR;
1838 gcry_sexp_t skey, pkey;
1840 err = gcry_sexp_sscan (&skey, NULL, spec.key.secret,
1841 strlen (spec.key.secret));
1843 err = gcry_sexp_sscan (&pkey, NULL, spec.key.public,
1844 strlen (spec.key.public));
1846 die ("converting sample key failed: %s\n", gpg_strerror (err));
1848 do_check_one_pubkey (n, skey, pkey,
1849 (const unsigned char*)spec.key.grip, spec.flags);
1851 gcry_sexp_release (skey);
1852 gcry_sexp_release (pkey);
1856 get_keys_new (gcry_sexp_t *pkey, gcry_sexp_t *skey)
1858 gcry_sexp_t key_spec, key, pub_key, sec_key;
1861 fprintf (stderr, " generating RSA key:");
1862 rc = gcry_sexp_new (&key_spec,
1863 in_fips_mode ? "(genkey (rsa (nbits 4:1024)))"
1864 : "(genkey (rsa (nbits 4:1024)(transient-key)))",
1867 die ("error creating S-expression: %s\n", gpg_strerror (rc));
1868 rc = gcry_pk_genkey (&key, key_spec);
1869 gcry_sexp_release (key_spec);
1871 die ("error generating RSA key: %s\n", gpg_strerror (rc));
1873 pub_key = gcry_sexp_find_token (key, "public-key", 0);
1875 die ("public part missing in key\n");
1877 sec_key = gcry_sexp_find_token (key, "private-key", 0);
1879 die ("private part missing in key\n");
1881 gcry_sexp_release (key);
1887 check_one_pubkey_new (int n)
1889 gcry_sexp_t skey, pkey;
1891 get_keys_new (&pkey, &skey);
1892 do_check_one_pubkey (n, skey, pkey, NULL, FLAG_SIGN | FLAG_CRYPT);
1895 /* Run all tests for the public key functions. */
1899 test_spec_pubkey_t pubkeys[] =
1902 GCRY_PK_RSA, FLAG_CRYPT | FLAG_SIGN,
1906 " (n #00e0ce96f90b6c9e02f3922beada93fe50a875eac6bcc18bb9a9cf2e84965caa"
1907 " 2d1ff95a7f542465c6c0c19d276e4526ce048868a7a914fd343cc3a87dd74291"
1908 " ffc565506d5bbb25cbac6a0e2dd1f8bcaab0d4a29c2f37c950f363484bf269f7"
1909 " 891440464baf79827e03a36e70b814938eebdc63e964247be75dc58b014b7ea251#)\n"
1911 " (d #046129F2489D71579BE0A75FE029BD6CDB574EBF57EA8A5B0FDA942CAB943B11"
1912 " 7D7BB95E5D28875E0F9FC5FCC06A72F6D502464DABDED78EF6B716177B83D5BD"
1913 " C543DC5D3FED932E59F5897E92E6F58A0F33424106A3B6FA2CBF877510E4AC21"
1914 " C3EE47851E97D12996222AC3566D4CCB0B83D164074ABF7DE655FC2446DA1781#)\n"
1915 " (p #00e861b700e17e8afe6837e7512e35b6ca11d0ae47d8b85161c67baf64377213"
1916 " fe52d772f2035b3ca830af41d8a4120e1c1c70d12cc22f00d28d31dd48a8d424f1#)\n"
1917 " (q #00f7a7ca5367c661f8e62df34f0d05c10c88e5492348dd7bddc942c9a8f369f9"
1918 " 35a07785d2db805215ed786e4285df1658eed3ce84f469b81b50d358407b4ad361#)\n"
1919 " (u #304559a9ead56d2309d203811a641bb1a09626bc8eb36fffa23c968ec5bd891e"
1920 " ebbafc73ae666e01ba7c8990bae06cc2bbe10b75e69fcacb353a6473079d8e9b#)))\n",
1924 " (n #00e0ce96f90b6c9e02f3922beada93fe50a875eac6bcc18bb9a9cf2e84965caa"
1925 " 2d1ff95a7f542465c6c0c19d276e4526ce048868a7a914fd343cc3a87dd74291"
1926 " ffc565506d5bbb25cbac6a0e2dd1f8bcaab0d4a29c2f37c950f363484bf269f7"
1927 " 891440464baf79827e03a36e70b814938eebdc63e964247be75dc58b014b7ea251#)\n"
1928 " (e #010001#)))\n",
1930 "\x32\x10\x0c\x27\x17\x3e\xf6\xe9\xc4\xe9"
1931 "\xa2\x5d\x3d\x69\xf8\x6d\x37\xa4\xf9\x39"}
1934 GCRY_PK_DSA, FLAG_SIGN,
1938 " (p #00AD7C0025BA1A15F775F3F2D673718391D00456978D347B33D7B49E7F32EDAB"
1939 " 96273899DD8B2BB46CD6ECA263FAF04A28903503D59062A8865D2AE8ADFB5191"
1940 " CF36FFB562D0E2F5809801A1F675DAE59698A9E01EFE8D7DCFCA084F4C6F5A44"
1941 " 44D499A06FFAEA5E8EF5E01F2FD20A7B7EF3F6968AFBA1FB8D91F1559D52D8777B#)\n"
1942 " (q #00EB7B5751D25EBBB7BD59D920315FD840E19AEBF9#)\n"
1943 " (g #1574363387FDFD1DDF38F4FBE135BB20C7EE4772FB94C337AF86EA8E49666503"
1944 " AE04B6BE81A2F8DD095311E0217ACA698A11E6C5D33CCDAE71498ED35D13991E"
1945 " B02F09AB40BD8F4C5ED8C75DA779D0AE104BC34C960B002377068AB4B5A1F984"
1946 " 3FBA91F537F1B7CAC4D8DD6D89B0D863AF7025D549F9C765D2FC07EE208F8D15#)\n"
1947 " (y #64B11EF8871BE4AB572AA810D5D3CA11A6CDBC637A8014602C72960DB135BF46"
1948 " A1816A724C34F87330FC9E187C5D66897A04535CC2AC9164A7150ABFA8179827"
1949 " 6E45831AB811EEE848EBB24D9F5F2883B6E5DDC4C659DEF944DCFD80BF4D0A20"
1950 " 42CAA7DC289F0C5A9D155F02D3D551DB741A81695B74D4C8F477F9C7838EB0FB#)\n"
1951 " (x #11D54E4ADBD3034160F2CED4B7CD292A4EBF3EC0#)))\n",
1955 " (p #00AD7C0025BA1A15F775F3F2D673718391D00456978D347B33D7B49E7F32EDAB"
1956 " 96273899DD8B2BB46CD6ECA263FAF04A28903503D59062A8865D2AE8ADFB5191"
1957 " CF36FFB562D0E2F5809801A1F675DAE59698A9E01EFE8D7DCFCA084F4C6F5A44"
1958 " 44D499A06FFAEA5E8EF5E01F2FD20A7B7EF3F6968AFBA1FB8D91F1559D52D8777B#)\n"
1959 " (q #00EB7B5751D25EBBB7BD59D920315FD840E19AEBF9#)\n"
1960 " (g #1574363387FDFD1DDF38F4FBE135BB20C7EE4772FB94C337AF86EA8E49666503"
1961 " AE04B6BE81A2F8DD095311E0217ACA698A11E6C5D33CCDAE71498ED35D13991E"
1962 " B02F09AB40BD8F4C5ED8C75DA779D0AE104BC34C960B002377068AB4B5A1F984"
1963 " 3FBA91F537F1B7CAC4D8DD6D89B0D863AF7025D549F9C765D2FC07EE208F8D15#)\n"
1964 " (y #64B11EF8871BE4AB572AA810D5D3CA11A6CDBC637A8014602C72960DB135BF46"
1965 " A1816A724C34F87330FC9E187C5D66897A04535CC2AC9164A7150ABFA8179827"
1966 " 6E45831AB811EEE848EBB24D9F5F2883B6E5DDC4C659DEF944DCFD80BF4D0A20"
1967 " 42CAA7DC289F0C5A9D155F02D3D551DB741A81695B74D4C8F477F9C7838EB0FB#)))\n",
1969 "\xc6\x39\x83\x1a\x43\xe5\x05\x5d\xc6\xd8"
1970 "\x4a\xa6\xf9\xeb\x23\xbf\xa9\x12\x2d\x5b" }
1973 GCRY_PK_ELG, FLAG_SIGN | FLAG_CRYPT,
1977 " (p #00B93B93386375F06C2D38560F3B9C6D6D7B7506B20C1773F73F8DE56E6CD65D"
1978 " F48DFAAA1E93F57A2789B168362A0F787320499F0B2461D3A4268757A7B27517"
1979 " B7D203654A0CD484DEC6AF60C85FEB84AAC382EAF2047061FE5DAB81A20A0797"
1980 " 6E87359889BAE3B3600ED718BE61D4FC993CC8098A703DD0DC942E965E8F18D2A7#)\n"
1982 " (y #72DAB3E83C9F7DD9A931FDECDC6522C0D36A6F0A0FEC955C5AC3C09175BBFF2B"
1983 " E588DB593DC2E420201BEB3AC17536918417C497AC0F8657855380C1FCF11C5B"
1984 " D20DB4BEE9BDF916648DE6D6E419FA446C513AAB81C30CB7B34D6007637BE675"
1985 " 56CE6473E9F9EE9B9FADD275D001563336F2186F424DEC6199A0F758F6A00FF4#)\n"
1986 " (x #03C28900087B38DABF4A0AB98ACEA39BB674D6557096C01D72E31C16BDD32214#)))\n",
1990 " (p #00B93B93386375F06C2D38560F3B9C6D6D7B7506B20C1773F73F8DE56E6CD65D"
1991 " F48DFAAA1E93F57A2789B168362A0F787320499F0B2461D3A4268757A7B27517"
1992 " B7D203654A0CD484DEC6AF60C85FEB84AAC382EAF2047061FE5DAB81A20A0797"
1993 " 6E87359889BAE3B3600ED718BE61D4FC993CC8098A703DD0DC942E965E8F18D2A7#)\n"
1995 " (y #72DAB3E83C9F7DD9A931FDECDC6522C0D36A6F0A0FEC955C5AC3C09175BBFF2B"
1996 " E588DB593DC2E420201BEB3AC17536918417C497AC0F8657855380C1FCF11C5B"
1997 " D20DB4BEE9BDF916648DE6D6E419FA446C513AAB81C30CB7B34D6007637BE675"
1998 " 56CE6473E9F9EE9B9FADD275D001563336F2186F424DEC6199A0F758F6A00FF4#)))\n",
2000 "\xa7\x99\x61\xeb\x88\x83\xd2\xf4\x05\xc8"
2001 "\x4f\xba\x06\xf8\x78\x09\xbc\x1e\x20\xe5" }
2006 fprintf (stderr, "Starting public key checks.\n");
2007 for (i = 0; i < sizeof (pubkeys) / sizeof (*pubkeys); i++)
2010 if (gcry_pk_test_algo (pubkeys[i].id) && in_fips_mode)
2013 fprintf (stderr, " algorithm %d not available in fips mode\n",
2017 check_one_pubkey (i, pubkeys[i]);
2020 fprintf (stderr, "Completed public key checks.\n");
2023 fprintf (stderr, "Starting additional public key checks.\n");
2024 for (i = 0; i < sizeof (pubkeys) / sizeof (*pubkeys); i++)
2027 if (gcry_pk_test_algo (pubkeys[i].id) && in_fips_mode)
2030 fprintf (stderr, " algorithm %d not available in fips mode\n",
2034 check_one_pubkey_new (i);
2037 fprintf (stderr, "Completed additional public key checks.\n");
2042 main (int argc, char **argv)
2052 while (argc && last_argc != argc )
2055 if (!strcmp (*argv, "--"))
2060 else if (!strcmp (*argv, "--verbose"))
2065 else if (!strcmp (*argv, "--debug"))
2067 verbose = debug = 1;
2070 else if (!strcmp (*argv, "--fips"))
2077 gcry_control (GCRYCTL_SET_VERBOSITY, (int)verbose);
2080 gcry_control (GCRYCTL_FORCE_FIPS_MODE, 0);
2082 if (!gcry_check_version (GCRYPT_VERSION))
2083 die ("version mismatch\n");
2086 gcry_set_progress_handler (progress_handler, NULL);
2088 gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
2089 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
2091 gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
2092 /* No valuable keys are create, so we can speed up our RNG. */
2093 gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
2095 if ( gcry_control (GCRYCTL_FIPS_MODE_P, 0) )
2099 check_aes128_cbc_cts_cipher ();
2100 check_cbc_mac_cipher ();
2101 check_ctr_cipher ();
2102 check_cfb_cipher ();
2103 check_ofb_cipher ();
2108 /* If we are in fips mode do some more tests. */
2113 /* First trigger a self-test. */
2114 gcry_control (GCRYCTL_FORCE_FIPS_MODE, 0);
2115 if (!gcry_control (GCRYCTL_OPERATIONAL_P, 0))
2116 fail ("not in operational state after self-test\n");
2118 /* Get us into the error state. */
2119 err = gcry_md_open (&md, GCRY_MD_SHA1, 0);
2121 fail ("failed to open SHA-1 hash context: %s\n", gpg_strerror (err));
2124 err = gcry_md_enable (md, GCRY_MD_SHA256);
2126 fail ("failed to add SHA-256 hash context: %s\n",
2127 gpg_strerror (err));
2130 /* gcry_md_get_algo is only defined for a context with
2131 just one digest algorithm. With our setup it should
2132 put the oibrary intoerror state. */
2133 gcry_md_get_algo (md);
2135 if (gcry_control (GCRYCTL_OPERATIONAL_P, 0))
2136 fail ("expected error state but still in operational state\n");
2139 /* Now run a self-test and to get back into
2140 operational state. */
2141 gcry_control (GCRYCTL_FORCE_FIPS_MODE, 0);
2142 if (!gcry_control (GCRYCTL_OPERATIONAL_P, 0))
2143 fail ("did not reach operational after error "
2153 fprintf (stderr, "\nAll tests completed. Errors: %i\n", error_count);
2155 return error_count ? 1 : 0;