LCOV - code coverage report
Current view: top level - tests - pubkey.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 351 501 70.1 %
Date: 2015-11-05 17:08:00 Functions: 19 23 82.6 %

          Line data    Source code
       1             : /* pubkey.c - Public key encryption/decryption tests
       2             :  *      Copyright (C) 2001, 2002, 2003, 2005 Free Software Foundation, Inc.
       3             :  *
       4             :  * This file is part of Libgcrypt.
       5             :  *
       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.
      10             :  *
      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.
      15             :  *
      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/>.
      18             :  */
      19             : 
      20             : #ifdef HAVE_CONFIG_H
      21             : #include <config.h>
      22             : #endif
      23             : #include <stdarg.h>
      24             : #include <stdio.h>
      25             : #include <stdlib.h>
      26             : #include <string.h>
      27             : 
      28             : 
      29             : #include "../src/gcrypt-int.h"
      30             : 
      31             : #define my_isascii(c) (!((c) & 0x80))
      32             : #define digitp(p)   (*(p) >= '0' && *(p) <= '9')
      33             : #define hexdigitp(a) (digitp (a)                     \
      34             :                       || (*(a) >= 'A' && *(a) <= 'F')  \
      35             :                       || (*(a) >= 'a' && *(a) <= 'f'))
      36             : #define xtoi_1(p)   (*(p) <= '9'? (*(p)- '0'): \
      37             :                      *(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10))
      38             : #define xtoi_2(p)   ((xtoi_1(p) * 16) + xtoi_1((p)+1))
      39             : #define DIM(v)               (sizeof(v)/sizeof((v)[0]))
      40             : #define DIMof(type,member)   DIM(((type *)0)->member)
      41             : 
      42             : 
      43             : /* Sample RSA keys, taken from basic.c.  */
      44             : 
      45             : static const char sample_private_key_1[] =
      46             : "(private-key\n"
      47             : " (openpgp-rsa\n"
      48             : "  (n #00e0ce96f90b6c9e02f3922beada93fe50a875eac6bcc18bb9a9cf2e84965caa"
      49             :       "2d1ff95a7f542465c6c0c19d276e4526ce048868a7a914fd343cc3a87dd74291"
      50             :       "ffc565506d5bbb25cbac6a0e2dd1f8bcaab0d4a29c2f37c950f363484bf269f7"
      51             :       "891440464baf79827e03a36e70b814938eebdc63e964247be75dc58b014b7ea251#)\n"
      52             : "  (e #010001#)\n"
      53             : "  (d #046129F2489D71579BE0A75FE029BD6CDB574EBF57EA8A5B0FDA942CAB943B11"
      54             :       "7D7BB95E5D28875E0F9FC5FCC06A72F6D502464DABDED78EF6B716177B83D5BD"
      55             :       "C543DC5D3FED932E59F5897E92E6F58A0F33424106A3B6FA2CBF877510E4AC21"
      56             :       "C3EE47851E97D12996222AC3566D4CCB0B83D164074ABF7DE655FC2446DA1781#)\n"
      57             : "  (p #00e861b700e17e8afe6837e7512e35b6ca11d0ae47d8b85161c67baf64377213"
      58             :       "fe52d772f2035b3ca830af41d8a4120e1c1c70d12cc22f00d28d31dd48a8d424f1#)\n"
      59             : "  (q #00f7a7ca5367c661f8e62df34f0d05c10c88e5492348dd7bddc942c9a8f369f9"
      60             :       "35a07785d2db805215ed786e4285df1658eed3ce84f469b81b50d358407b4ad361#)\n"
      61             : "  (u #304559a9ead56d2309d203811a641bb1a09626bc8eb36fffa23c968ec5bd891e"
      62             :       "ebbafc73ae666e01ba7c8990bae06cc2bbe10b75e69fcacb353a6473079d8e9b#)\n"
      63             : " )\n"
      64             : ")\n";
      65             : 
      66             : /* The same key as above but without p, q and u to test the non CRT case. */
      67             : static const char sample_private_key_1_1[] =
      68             : "(private-key\n"
      69             : " (openpgp-rsa\n"
      70             : "  (n #00e0ce96f90b6c9e02f3922beada93fe50a875eac6bcc18bb9a9cf2e84965caa"
      71             :       "2d1ff95a7f542465c6c0c19d276e4526ce048868a7a914fd343cc3a87dd74291"
      72             :       "ffc565506d5bbb25cbac6a0e2dd1f8bcaab0d4a29c2f37c950f363484bf269f7"
      73             :       "891440464baf79827e03a36e70b814938eebdc63e964247be75dc58b014b7ea251#)\n"
      74             : "  (e #010001#)\n"
      75             : "  (d #046129F2489D71579BE0A75FE029BD6CDB574EBF57EA8A5B0FDA942CAB943B11"
      76             :       "7D7BB95E5D28875E0F9FC5FCC06A72F6D502464DABDED78EF6B716177B83D5BD"
      77             :       "C543DC5D3FED932E59F5897E92E6F58A0F33424106A3B6FA2CBF877510E4AC21"
      78             :       "C3EE47851E97D12996222AC3566D4CCB0B83D164074ABF7DE655FC2446DA1781#)\n"
      79             : " )\n"
      80             : ")\n";
      81             : 
      82             : /* The same key as above but just without q to test the non CRT case.  This
      83             :    should fail. */
      84             : static const char sample_private_key_1_2[] =
      85             : "(private-key\n"
      86             : " (openpgp-rsa\n"
      87             : "  (n #00e0ce96f90b6c9e02f3922beada93fe50a875eac6bcc18bb9a9cf2e84965caa"
      88             :       "2d1ff95a7f542465c6c0c19d276e4526ce048868a7a914fd343cc3a87dd74291"
      89             :       "ffc565506d5bbb25cbac6a0e2dd1f8bcaab0d4a29c2f37c950f363484bf269f7"
      90             :       "891440464baf79827e03a36e70b814938eebdc63e964247be75dc58b014b7ea251#)\n"
      91             : "  (e #010001#)\n"
      92             : "  (d #046129F2489D71579BE0A75FE029BD6CDB574EBF57EA8A5B0FDA942CAB943B11"
      93             :       "7D7BB95E5D28875E0F9FC5FCC06A72F6D502464DABDED78EF6B716177B83D5BD"
      94             :       "C543DC5D3FED932E59F5897E92E6F58A0F33424106A3B6FA2CBF877510E4AC21"
      95             :       "C3EE47851E97D12996222AC3566D4CCB0B83D164074ABF7DE655FC2446DA1781#)\n"
      96             : "  (p #00e861b700e17e8afe6837e7512e35b6ca11d0ae47d8b85161c67baf64377213"
      97             :       "fe52d772f2035b3ca830af41d8a4120e1c1c70d12cc22f00d28d31dd48a8d424f1#)\n"
      98             : "  (u #304559a9ead56d2309d203811a641bb1a09626bc8eb36fffa23c968ec5bd891e"
      99             :       "ebbafc73ae666e01ba7c8990bae06cc2bbe10b75e69fcacb353a6473079d8e9b#)\n"
     100             : " )\n"
     101             : ")\n";
     102             : 
     103             : static const char sample_public_key_1[] =
     104             : "(public-key\n"
     105             : " (rsa\n"
     106             : "  (n #00e0ce96f90b6c9e02f3922beada93fe50a875eac6bcc18bb9a9cf2e84965caa"
     107             :       "2d1ff95a7f542465c6c0c19d276e4526ce048868a7a914fd343cc3a87dd74291"
     108             :       "ffc565506d5bbb25cbac6a0e2dd1f8bcaab0d4a29c2f37c950f363484bf269f7"
     109             :       "891440464baf79827e03a36e70b814938eebdc63e964247be75dc58b014b7ea251#)\n"
     110             : "  (e #010001#)\n"
     111             : " )\n"
     112             : ")\n";
     113             : 
     114             : 
     115             : static int verbose;
     116             : static int error_count;
     117             : 
     118             : static void
     119           0 : die (const char *format, ...)
     120             : {
     121             :   va_list arg_ptr ;
     122             : 
     123           0 :   va_start( arg_ptr, format ) ;
     124           0 :   vfprintf (stderr, format, arg_ptr );
     125           0 :   va_end(arg_ptr);
     126           0 :   if (*format && format[strlen(format)-1] != '\n')
     127           0 :     putc ('\n', stderr);
     128           0 :   exit (1);
     129             : }
     130             : 
     131             : static void
     132           0 : fail (const char *format, ...)
     133             : {
     134             :   va_list arg_ptr;
     135             : 
     136           0 :   va_start (arg_ptr, format);
     137           0 :   vfprintf (stderr, format, arg_ptr);
     138           0 :   va_end (arg_ptr);
     139           0 :   error_count++;
     140           0 : }
     141             : 
     142             : static void
     143           0 : info (const char *format, ...)
     144             : {
     145             :   va_list arg_ptr;
     146             : 
     147           0 :   va_start (arg_ptr, format);
     148           0 :   vfprintf (stderr, format, arg_ptr);
     149           0 :   va_end (arg_ptr);
     150           0 : }
     151             : 
     152             : static void
     153           0 : show_sexp (const char *prefix, gcry_sexp_t a)
     154             : {
     155             :   char *buf;
     156             :   size_t size;
     157             : 
     158           0 :   if (prefix)
     159           0 :     fputs (prefix, stderr);
     160           0 :   size = gcry_sexp_sprint (a, GCRYSEXP_FMT_ADVANCED, NULL, 0);
     161           0 :   buf = gcry_xmalloc (size);
     162             : 
     163           0 :   gcry_sexp_sprint (a, GCRYSEXP_FMT_ADVANCED, buf, size);
     164           0 :   fprintf (stderr, "%.*s", (int)size, buf);
     165           0 :   gcry_free (buf);
     166           0 : }
     167             : 
     168             : 
     169             : /* Convert STRING consisting of hex characters into its binary
     170             :    representation and return it as an allocated buffer. The valid
     171             :    length of the buffer is returned at R_LENGTH.  The string is
     172             :    delimited by end of string.  The function returns NULL on
     173             :    error.  */
     174             : static void *
     175           2 : data_from_hex (const char *string, size_t *r_length)
     176             : {
     177             :   const char *s;
     178             :   unsigned char *buffer;
     179             :   size_t length;
     180             : 
     181           2 :   buffer = gcry_xmalloc (strlen(string)/2+1);
     182           2 :   length = 0;
     183          65 :   for (s=string; *s; s +=2 )
     184             :     {
     185          63 :       if (!hexdigitp (s) || !hexdigitp (s+1))
     186           0 :         die ("error parsing hex string `%s'\n", string);
     187          63 :       ((unsigned char*)buffer)[length++] = xtoi_2 (s);
     188             :     }
     189           2 :   *r_length = length;
     190           2 :   return buffer;
     191             : }
     192             : 
     193             : 
     194             : static void
     195           2 : extract_cmp_data (gcry_sexp_t sexp, const char *name, const char *expected)
     196             : {
     197             :   gcry_sexp_t l1;
     198             :   const void *a;
     199             :   size_t alen;
     200             :   void *b;
     201             :   size_t blen;
     202             : 
     203           2 :   l1 = gcry_sexp_find_token (sexp, name, 0);
     204           2 :   a = gcry_sexp_nth_data (l1, 1, &alen);
     205           2 :   b = data_from_hex (expected, &blen);
     206           2 :   if (!a)
     207           0 :     fail ("parameter \"%s\" missing in key\n", name);
     208           2 :   else if ( alen != blen || memcmp (a, b, alen) )
     209             :     {
     210           0 :       fail ("parameter \"%s\" does not match expected value\n", name);
     211           0 :       if (verbose)
     212             :         {
     213           0 :           info ("expected: %s\n", expected);
     214           0 :           show_sexp ("sexp: ", sexp);
     215             :         }
     216             :     }
     217           2 :   gcry_free (b);
     218           2 :   gcry_sexp_release (l1);
     219           2 : }
     220             : 
     221             : 
     222             : static void
     223          28 : check_keys_crypt (gcry_sexp_t pkey, gcry_sexp_t skey,
     224             :                   gcry_sexp_t plain0, gpg_err_code_t decrypt_fail_code)
     225             : {
     226             :   gcry_sexp_t plain1, cipher, l;
     227             :   gcry_mpi_t x0, x1;
     228             :   int rc;
     229             :   int have_flags;
     230             : 
     231             :   /* Extract data from plaintext.  */
     232          28 :   l = gcry_sexp_find_token (plain0, "value", 0);
     233          28 :   x0 = gcry_sexp_nth_mpi (l, 1, GCRYMPI_FMT_USG);
     234          28 :   gcry_sexp_release (l);
     235             : 
     236             :   /* Encrypt data.  */
     237          28 :   rc = gcry_pk_encrypt (&cipher, plain0, pkey);
     238          28 :   if (rc)
     239           0 :     die ("encryption failed: %s\n", gcry_strerror (rc));
     240             : 
     241          28 :   l = gcry_sexp_find_token (cipher, "flags", 0);
     242          28 :   have_flags = !!l;
     243          28 :   gcry_sexp_release (l);
     244             : 
     245             :   /* Decrypt data.  */
     246          28 :   rc = gcry_pk_decrypt (&plain1, cipher, skey);
     247          28 :   gcry_sexp_release (cipher);
     248          28 :   if (rc)
     249             :     {
     250           0 :       if (decrypt_fail_code && gpg_err_code (rc) == decrypt_fail_code)
     251             :         {
     252           0 :           gcry_mpi_release (x0);
     253          28 :           return; /* This is the expected failure code.  */
     254             :         }
     255           0 :       die ("decryption failed: %s\n", gcry_strerror (rc));
     256             :     }
     257             : 
     258             :   /* Extract decrypted data.  Note that for compatibility reasons, the
     259             :      output of gcry_pk_decrypt depends on whether a flags lists (even
     260             :      if empty) occurs in its input data.  Because we passed the output
     261             :      of encrypt directly to decrypt, such a flag value won't be there
     262             :      as of today.  We check it anyway. */
     263          28 :   l = gcry_sexp_find_token (plain1, "value", 0);
     264          28 :   if (l)
     265             :     {
     266           0 :       if (!have_flags)
     267           0 :         die ("compatibility mode of pk_decrypt broken\n");
     268           0 :       gcry_sexp_release (plain1);
     269           0 :       x1 = gcry_sexp_nth_mpi (l, 1, GCRYMPI_FMT_USG);
     270           0 :       gcry_sexp_release (l);
     271             :     }
     272             :   else
     273             :     {
     274          28 :       if (have_flags)
     275           0 :         die ("compatibility mode of pk_decrypt broken\n");
     276          28 :       x1 = gcry_sexp_nth_mpi (plain1, 0, GCRYMPI_FMT_USG);
     277          28 :       gcry_sexp_release (plain1);
     278             :     }
     279             : 
     280             :   /* Compare.  */
     281          28 :   if (gcry_mpi_cmp (x0, x1))
     282           0 :     die ("data corrupted\n");
     283          28 :   gcry_mpi_release (x0);
     284          28 :   gcry_mpi_release (x1);
     285             : }
     286             : 
     287             : static void
     288          14 : check_keys (gcry_sexp_t pkey, gcry_sexp_t skey, unsigned int nbits_data,
     289             :             gpg_err_code_t decrypt_fail_code)
     290             : {
     291             :   gcry_sexp_t plain;
     292             :   gcry_mpi_t x;
     293             :   int rc;
     294             : 
     295             :   /* Create plain text.  */
     296          14 :   x = gcry_mpi_new (nbits_data);
     297          14 :   gcry_mpi_randomize (x, nbits_data, GCRY_WEAK_RANDOM);
     298             : 
     299          14 :   rc = gcry_sexp_build (&plain, NULL, "(data (flags raw) (value %m))", x);
     300          14 :   if (rc)
     301           0 :     die ("converting data for encryption failed: %s\n",
     302             :          gcry_strerror (rc));
     303             : 
     304          14 :   check_keys_crypt (pkey, skey, plain, decrypt_fail_code);
     305          14 :   gcry_sexp_release (plain);
     306          14 :   gcry_mpi_release (x);
     307             : 
     308             :   /* Create plain text.  */
     309          14 :   x = gcry_mpi_new (nbits_data);
     310          14 :   gcry_mpi_randomize (x, nbits_data, GCRY_WEAK_RANDOM);
     311             : 
     312          14 :   rc = gcry_sexp_build (&plain, NULL,
     313             :                         "(data (flags raw no-blinding) (value %m))", x);
     314          14 :   gcry_mpi_release (x);
     315          14 :   if (rc)
     316           0 :     die ("converting data for encryption failed: %s\n",
     317             :          gcry_strerror (rc));
     318             : 
     319          14 :   check_keys_crypt (pkey, skey, plain, decrypt_fail_code);
     320          14 :   gcry_sexp_release (plain);
     321          14 : }
     322             : 
     323             : static void
     324           6 : get_keys_sample (gcry_sexp_t *pkey, gcry_sexp_t *skey, int secret_variant)
     325             : {
     326             :   gcry_sexp_t pub_key, sec_key;
     327             :   int rc;
     328             :   static const char *secret;
     329             : 
     330             : 
     331           6 :   switch (secret_variant)
     332             :     {
     333           2 :     case 0: secret = sample_private_key_1; break;
     334           2 :     case 1: secret = sample_private_key_1_1; break;
     335           2 :     case 2: secret = sample_private_key_1_2; break;
     336           0 :     default: die ("BUG\n");
     337             :     }
     338             : 
     339           6 :   rc = gcry_sexp_sscan (&pub_key, NULL, sample_public_key_1,
     340             :                         strlen (sample_public_key_1));
     341           6 :   if (!rc)
     342           6 :     rc = gcry_sexp_sscan (&sec_key, NULL, secret, strlen (secret));
     343           6 :   if (rc)
     344           0 :     die ("converting sample keys failed: %s\n", gcry_strerror (rc));
     345             : 
     346           6 :   *pkey = pub_key;
     347           6 :   *skey = sec_key;
     348           6 : }
     349             : 
     350             : static void
     351           2 : get_keys_new (gcry_sexp_t *pkey, gcry_sexp_t *skey)
     352             : {
     353             :   gcry_sexp_t key_spec, key, pub_key, sec_key;
     354             :   int rc;
     355             : 
     356           2 :   rc = gcry_sexp_new (&key_spec,
     357             :                       "(genkey (rsa (nbits 4:1024)))", 0, 1);
     358           2 :   if (rc)
     359           0 :     die ("error creating S-expression: %s\n", gcry_strerror (rc));
     360           2 :   rc = gcry_pk_genkey (&key, key_spec);
     361           2 :   gcry_sexp_release (key_spec);
     362           2 :   if (rc)
     363           0 :     die ("error generating RSA key: %s\n", gcry_strerror (rc));
     364             : 
     365           2 :   if (verbose > 1)
     366           0 :     show_sexp ("generated RSA key:\n", key);
     367             : 
     368           2 :   pub_key = gcry_sexp_find_token (key, "public-key", 0);
     369           2 :   if (! pub_key)
     370           0 :     die ("public part missing in key\n");
     371             : 
     372           2 :   sec_key = gcry_sexp_find_token (key, "private-key", 0);
     373           2 :   if (! sec_key)
     374           0 :     die ("private part missing in key\n");
     375             : 
     376           2 :   gcry_sexp_release (key);
     377           2 :   *pkey = pub_key;
     378           2 :   *skey = sec_key;
     379           2 : }
     380             : 
     381             : 
     382             : static void
     383           2 : get_keys_x931_new (gcry_sexp_t *pkey, gcry_sexp_t *skey)
     384             : {
     385             :   gcry_sexp_t key_spec, key, pub_key, sec_key;
     386             :   int rc;
     387             : 
     388           2 :   rc = gcry_sexp_new (&key_spec,
     389             :                       "(genkey (rsa (nbits 4:1024)(use-x931)))", 0, 1);
     390           2 :   if (rc)
     391           0 :     die ("error creating S-expression: %s\n", gcry_strerror (rc));
     392           2 :   rc = gcry_pk_genkey (&key, key_spec);
     393           2 :   gcry_sexp_release (key_spec);
     394           2 :   if (rc)
     395           0 :     die ("error generating RSA key: %s\n", gcry_strerror (rc));
     396             : 
     397           2 :   if (verbose > 1)
     398           0 :     show_sexp ("generated RSA (X9.31) key:\n", key);
     399             : 
     400           2 :   pub_key = gcry_sexp_find_token (key, "public-key", 0);
     401           2 :   if (!pub_key)
     402           0 :     die ("public part missing in key\n");
     403             : 
     404           2 :   sec_key = gcry_sexp_find_token (key, "private-key", 0);
     405           2 :   if (!sec_key)
     406           0 :     die ("private part missing in key\n");
     407             : 
     408           2 :   gcry_sexp_release (key);
     409           2 :   *pkey = pub_key;
     410           2 :   *skey = sec_key;
     411           2 : }
     412             : 
     413             : 
     414             : static void
     415           4 : get_elg_key_new (gcry_sexp_t *pkey, gcry_sexp_t *skey, int fixed_x)
     416             : {
     417             :   gcry_sexp_t key_spec, key, pub_key, sec_key;
     418             :   int rc;
     419             : 
     420           4 :   rc = gcry_sexp_new
     421             :     (&key_spec,
     422             :      (fixed_x
     423             :       ? "(genkey (elg (nbits 4:1024)(xvalue my.not-so-secret.key)))"
     424             :       : "(genkey (elg (nbits 3:512)))"),
     425             :      0, 1);
     426             : 
     427           4 :   if (rc)
     428           0 :     die ("error creating S-expression: %s\n", gcry_strerror (rc));
     429           4 :   rc = gcry_pk_genkey (&key, key_spec);
     430           4 :   gcry_sexp_release (key_spec);
     431           4 :   if (rc)
     432           0 :     die ("error generating Elgamal key: %s\n", gcry_strerror (rc));
     433             : 
     434           4 :   if (verbose > 1)
     435           0 :     show_sexp ("generated ELG key:\n", key);
     436             : 
     437           4 :   pub_key = gcry_sexp_find_token (key, "public-key", 0);
     438           4 :   if (!pub_key)
     439           0 :     die ("public part missing in key\n");
     440             : 
     441           4 :   sec_key = gcry_sexp_find_token (key, "private-key", 0);
     442           4 :   if (!sec_key)
     443           0 :     die ("private part missing in key\n");
     444             : 
     445           4 :   gcry_sexp_release (key);
     446           4 :   *pkey = pub_key;
     447           4 :   *skey = sec_key;
     448           4 : }
     449             : 
     450             : 
     451             : static void
     452           4 : get_dsa_key_new (gcry_sexp_t *pkey, gcry_sexp_t *skey, int transient_key)
     453             : {
     454             :   gcry_sexp_t key_spec, key, pub_key, sec_key;
     455             :   int rc;
     456             : 
     457           4 :   rc = gcry_sexp_new (&key_spec,
     458             :                       transient_key
     459             :                       ? "(genkey (dsa (nbits 4:1024)(transient-key)))"
     460             :                       : "(genkey (dsa (nbits 4:1024)))",
     461             :                       0, 1);
     462           4 :   if (rc)
     463           0 :     die ("error creating S-expression: %s\n", gcry_strerror (rc));
     464           4 :   rc = gcry_pk_genkey (&key, key_spec);
     465           4 :   gcry_sexp_release (key_spec);
     466           4 :   if (rc)
     467           0 :     die ("error generating DSA key: %s\n", gcry_strerror (rc));
     468             : 
     469           4 :   if (verbose > 1)
     470           0 :     show_sexp ("generated DSA key:\n", key);
     471             : 
     472           4 :   pub_key = gcry_sexp_find_token (key, "public-key", 0);
     473           4 :   if (!pub_key)
     474           0 :     die ("public part missing in key\n");
     475             : 
     476           4 :   sec_key = gcry_sexp_find_token (key, "private-key", 0);
     477           4 :   if (!sec_key)
     478           0 :     die ("private part missing in key\n");
     479             : 
     480           4 :   gcry_sexp_release (key);
     481           4 :   *pkey = pub_key;
     482           4 :   *skey = sec_key;
     483           4 : }
     484             : 
     485             : 
     486             : static void
     487           2 : get_dsa_key_fips186_new (gcry_sexp_t *pkey, gcry_sexp_t *skey)
     488             : {
     489             :   gcry_sexp_t key_spec, key, pub_key, sec_key;
     490             :   int rc;
     491             : 
     492           2 :   rc = gcry_sexp_new
     493             :     (&key_spec, "(genkey (dsa (nbits 4:1024)(use-fips186)))",  0, 1);
     494           2 :   if (rc)
     495           0 :     die ("error creating S-expression: %s\n", gcry_strerror (rc));
     496           2 :   rc = gcry_pk_genkey (&key, key_spec);
     497           2 :   gcry_sexp_release (key_spec);
     498           2 :   if (rc)
     499           0 :     die ("error generating DSA key: %s\n", gcry_strerror (rc));
     500             : 
     501           2 :   if (verbose > 1)
     502           0 :     show_sexp ("generated DSA key (fips 186):\n", key);
     503             : 
     504           2 :   pub_key = gcry_sexp_find_token (key, "public-key", 0);
     505           2 :   if (!pub_key)
     506           0 :     die ("public part missing in key\n");
     507             : 
     508           2 :   sec_key = gcry_sexp_find_token (key, "private-key", 0);
     509           2 :   if (!sec_key)
     510           0 :     die ("private part missing in key\n");
     511             : 
     512           2 :   gcry_sexp_release (key);
     513           2 :   *pkey = pub_key;
     514           2 :   *skey = sec_key;
     515           2 : }
     516             : 
     517             : 
     518             : static void
     519           2 : get_dsa_key_with_domain_new (gcry_sexp_t *pkey, gcry_sexp_t *skey)
     520             : {
     521             :   gcry_sexp_t key_spec, key, pub_key, sec_key;
     522             :   int rc;
     523             : 
     524           2 :   rc = gcry_sexp_new
     525             :     (&key_spec,
     526             :      "(genkey (dsa (transient-key)(domain"
     527             :      "(p #d3aed1876054db831d0c1348fbb1ada72507e5fbf9a62cbd47a63aeb7859d6921"
     528             :      "4adeb9146a6ec3f43520f0fd8e3125dd8bbc5d87405d1ac5f82073cd762a3f8d7"
     529             :      "74322657c9da88a7d2f0e1a9ceb84a39cb40876179e6a76e400498de4bb9379b0"
     530             :      "5f5feb7b91eb8fea97ee17a955a0a8a37587a272c4719d6feb6b54ba4ab69#)"
     531             :      "(q #9c916d121de9a03f71fb21bc2e1c0d116f065a4f#)"
     532             :      "(g #8157c5f68ca40b3ded11c353327ab9b8af3e186dd2e8dade98761a0996dda99ab"
     533             :      "0250d3409063ad99efae48b10c6ab2bba3ea9a67b12b911a372a2bba260176fad"
     534             :      "b4b93247d9712aad13aa70216c55da9858f7a298deb670a403eb1e7c91b847f1e"
     535             :      "ccfbd14bd806fd42cf45dbb69cd6d6b43add2a78f7d16928eaa04458dea44#)"
     536             :      ")))", 0, 1);
     537           2 :   if (rc)
     538           0 :     die ("error creating S-expression: %s\n", gcry_strerror (rc));
     539           2 :   rc = gcry_pk_genkey (&key, key_spec);
     540           2 :   gcry_sexp_release (key_spec);
     541           2 :   if (rc)
     542           0 :     die ("error generating DSA key: %s\n", gcry_strerror (rc));
     543             : 
     544           2 :   if (verbose > 1)
     545           0 :     show_sexp ("generated DSA key:\n", key);
     546             : 
     547           2 :   pub_key = gcry_sexp_find_token (key, "public-key", 0);
     548           2 :   if (!pub_key)
     549           0 :     die ("public part missing in key\n");
     550             : 
     551           2 :   sec_key = gcry_sexp_find_token (key, "private-key", 0);
     552           2 :   if (!sec_key)
     553           0 :     die ("private part missing in key\n");
     554             : 
     555           2 :   gcry_sexp_release (key);
     556           2 :   *pkey = pub_key;
     557           2 :   *skey = sec_key;
     558           2 : }
     559             : 
     560             : static void
     561           2 : get_dsa_key_fips186_with_domain_new (gcry_sexp_t *pkey, gcry_sexp_t *skey)
     562             : {
     563             :   gcry_sexp_t key_spec, key, pub_key, sec_key;
     564             :   int rc;
     565             : 
     566           2 :   rc = gcry_sexp_new
     567             :     (&key_spec,
     568             :      "(genkey (dsa (transient-key)(use-fips186)(domain"
     569             :      "(p #d3aed1876054db831d0c1348fbb1ada72507e5fbf9a62cbd47a63aeb7859d6921"
     570             :      "4adeb9146a6ec3f43520f0fd8e3125dd8bbc5d87405d1ac5f82073cd762a3f8d7"
     571             :      "74322657c9da88a7d2f0e1a9ceb84a39cb40876179e6a76e400498de4bb9379b0"
     572             :      "5f5feb7b91eb8fea97ee17a955a0a8a37587a272c4719d6feb6b54ba4ab69#)"
     573             :      "(q #9c916d121de9a03f71fb21bc2e1c0d116f065a4f#)"
     574             :      "(g #8157c5f68ca40b3ded11c353327ab9b8af3e186dd2e8dade98761a0996dda99ab"
     575             :      "0250d3409063ad99efae48b10c6ab2bba3ea9a67b12b911a372a2bba260176fad"
     576             :      "b4b93247d9712aad13aa70216c55da9858f7a298deb670a403eb1e7c91b847f1e"
     577             :      "ccfbd14bd806fd42cf45dbb69cd6d6b43add2a78f7d16928eaa04458dea44#)"
     578             :      ")))", 0, 1);
     579           2 :   if (rc)
     580           0 :     die ("error creating S-expression: %s\n", gcry_strerror (rc));
     581           2 :   rc = gcry_pk_genkey (&key, key_spec);
     582           2 :   gcry_sexp_release (key_spec);
     583           2 :   if (rc)
     584           0 :     die ("error generating DSA key: %s\n", gcry_strerror (rc));
     585             : 
     586           2 :   if (verbose > 1)
     587           0 :     show_sexp ("generated DSA key:\n", key);
     588             : 
     589           2 :   pub_key = gcry_sexp_find_token (key, "public-key", 0);
     590           2 :   if (!pub_key)
     591           0 :     die ("public part missing in key\n");
     592             : 
     593           2 :   sec_key = gcry_sexp_find_token (key, "private-key", 0);
     594           2 :   if (!sec_key)
     595           0 :     die ("private part missing in key\n");
     596             : 
     597           2 :   gcry_sexp_release (key);
     598           2 :   *pkey = pub_key;
     599           2 :   *skey = sec_key;
     600           2 : }
     601             : 
     602             : 
     603             : static void
     604           2 : get_dsa_key_fips186_with_seed_new (gcry_sexp_t *pkey, gcry_sexp_t *skey)
     605             : {
     606             :   gcry_sexp_t key_spec, key, pub_key, sec_key;
     607             :   int rc;
     608             : 
     609           2 :   rc = gcry_sexp_new
     610             :     (&key_spec,
     611             :      "(genkey"
     612             :      "  (dsa"
     613             :      "    (nbits 4:1024)"
     614             :      "    (use-fips186)"
     615             :      "    (transient-key)"
     616             :      "    (derive-parms"
     617             :      "      (seed #0cb1990c1fd3626055d7a0096f8fa99807399871#))))",
     618             :      0, 1);
     619           2 :   if (rc)
     620           0 :     die ("error creating S-expression: %s\n", gcry_strerror (rc));
     621           2 :   rc = gcry_pk_genkey (&key, key_spec);
     622           2 :   gcry_sexp_release (key_spec);
     623           2 :   if (rc)
     624           0 :     die ("error generating DSA key: %s\n", gcry_strerror (rc));
     625             : 
     626           2 :   if (verbose > 1)
     627           0 :     show_sexp ("generated DSA key (fips 186 with seed):\n", key);
     628             : 
     629           2 :   pub_key = gcry_sexp_find_token (key, "public-key", 0);
     630           2 :   if (!pub_key)
     631           0 :     die ("public part missing in key\n");
     632             : 
     633           2 :   sec_key = gcry_sexp_find_token (key, "private-key", 0);
     634           2 :   if (!sec_key)
     635           0 :     die ("private part missing in key\n");
     636             : 
     637           2 :   gcry_sexp_release (key);
     638           2 :   *pkey = pub_key;
     639           2 :   *skey = sec_key;
     640           2 : }
     641             : 
     642             : 
     643             : static void
     644           2 : check_run (void)
     645             : {
     646             :   gpg_error_t err;
     647             :   gcry_sexp_t pkey, skey;
     648             :   int variant;
     649             : 
     650           8 :   for (variant=0; variant < 3; variant++)
     651             :     {
     652           6 :       if (verbose)
     653           0 :         fprintf (stderr, "Checking sample key (%d).\n", variant);
     654           6 :       get_keys_sample (&pkey, &skey, variant);
     655             :       /* Check gcry_pk_testkey which requires all elements.  */
     656           6 :       err = gcry_pk_testkey (skey);
     657           6 :       if ((variant == 0 && err)
     658           6 :           || (variant > 0 && gpg_err_code (err) != GPG_ERR_NO_OBJ))
     659           0 :           die ("gcry_pk_testkey failed: %s\n", gpg_strerror (err));
     660             :       /* Run the usual check but expect an error from variant 2.  */
     661           6 :       check_keys (pkey, skey, 800, variant == 2? GPG_ERR_NO_OBJ : 0);
     662           6 :       gcry_sexp_release (pkey);
     663           6 :       gcry_sexp_release (skey);
     664             :     }
     665             : 
     666           2 :   if (verbose)
     667           0 :     fprintf (stderr, "Checking generated RSA key.\n");
     668           2 :   get_keys_new (&pkey, &skey);
     669           2 :   check_keys (pkey, skey, 800, 0);
     670           2 :   gcry_sexp_release (pkey);
     671           2 :   gcry_sexp_release (skey);
     672             : 
     673           2 :   if (verbose)
     674           0 :     fprintf (stderr, "Checking generated RSA key (X9.31).\n");
     675           2 :   get_keys_x931_new (&pkey, &skey);
     676           2 :   check_keys (pkey, skey, 800, 0);
     677           2 :   gcry_sexp_release (pkey);
     678           2 :   gcry_sexp_release (skey);
     679             : 
     680           2 :   if (verbose)
     681           0 :     fprintf (stderr, "Checking generated Elgamal key.\n");
     682           2 :   get_elg_key_new (&pkey, &skey, 0);
     683           2 :   check_keys (pkey, skey, 400, 0);
     684           2 :   gcry_sexp_release (pkey);
     685           2 :   gcry_sexp_release (skey);
     686             : 
     687           2 :   if (verbose)
     688           0 :     fprintf (stderr, "Checking passphrase generated Elgamal key.\n");
     689           2 :   get_elg_key_new (&pkey, &skey, 1);
     690           2 :   check_keys (pkey, skey, 800, 0);
     691           2 :   gcry_sexp_release (pkey);
     692           2 :   gcry_sexp_release (skey);
     693             : 
     694           2 :   if (verbose)
     695           0 :     fprintf (stderr, "Generating DSA key.\n");
     696           2 :   get_dsa_key_new (&pkey, &skey, 0);
     697             :   /* Fixme:  Add a check function for DSA keys.  */
     698           2 :   gcry_sexp_release (pkey);
     699           2 :   gcry_sexp_release (skey);
     700             : 
     701           2 :   if (!gcry_fips_mode_active ())
     702             :     {
     703           2 :       if (verbose)
     704           0 :         fprintf (stderr, "Generating transient DSA key.\n");
     705           2 :       get_dsa_key_new (&pkey, &skey, 1);
     706             :       /* Fixme:  Add a check function for DSA keys.  */
     707           2 :       gcry_sexp_release (pkey);
     708           2 :       gcry_sexp_release (skey);
     709             :     }
     710             : 
     711           2 :   if (verbose)
     712           0 :     fprintf (stderr, "Generating DSA key (FIPS 186).\n");
     713           2 :   get_dsa_key_fips186_new (&pkey, &skey);
     714             :   /* Fixme:  Add a check function for DSA keys.  */
     715           2 :   gcry_sexp_release (pkey);
     716           2 :   gcry_sexp_release (skey);
     717             : 
     718           2 :   if (verbose)
     719           0 :     fprintf (stderr, "Generating DSA key with given domain.\n");
     720           2 :   get_dsa_key_with_domain_new (&pkey, &skey);
     721             :   /* Fixme:  Add a check function for DSA keys.  */
     722           2 :   gcry_sexp_release (pkey);
     723           2 :   gcry_sexp_release (skey);
     724             : 
     725           2 :   if (verbose)
     726           0 :     fprintf (stderr, "Generating DSA key with given domain (FIPS 186).\n");
     727           2 :   get_dsa_key_fips186_with_domain_new (&pkey, &skey);
     728             :   /* Fixme:  Add a check function for DSA keys.  */
     729           2 :   gcry_sexp_release (pkey);
     730           2 :   gcry_sexp_release (skey);
     731             : 
     732           2 :   if (verbose)
     733           0 :     fprintf (stderr, "Generating DSA key with given seed (FIPS 186).\n");
     734           2 :   get_dsa_key_fips186_with_seed_new (&pkey, &skey);
     735             :   /* Fixme:  Add a check function for DSA keys.  */
     736           2 :   gcry_sexp_release (pkey);
     737           2 :   gcry_sexp_release (skey);
     738           2 : }
     739             : 
     740             : 
     741             : 
     742             : static gcry_mpi_t
     743           4 : key_param_from_sexp (gcry_sexp_t sexp, const char *topname, const char *name)
     744             : {
     745             :   gcry_sexp_t l1, l2;
     746             :   gcry_mpi_t result;
     747             : 
     748           4 :   l1 = gcry_sexp_find_token (sexp, topname, 0);
     749           4 :   if (!l1)
     750           0 :     return NULL;
     751             : 
     752           4 :   l2 = gcry_sexp_find_token (l1, name, 0);
     753           4 :   if (!l2)
     754             :     {
     755           0 :       gcry_sexp_release (l1);
     756           0 :       return NULL;
     757             :     }
     758             : 
     759           4 :   result = gcry_sexp_nth_mpi (l2, 1, GCRYMPI_FMT_USG);
     760           4 :   gcry_sexp_release (l2);
     761           4 :   gcry_sexp_release (l1);
     762           4 :   return result;
     763             : }
     764             : 
     765             : 
     766             : static void
     767           4 : check_x931_derived_key (int what)
     768             : {
     769             :   static struct {
     770             :     const char *param;
     771             :     const char *expected_d;
     772             :   } testtable[] = {
     773             :     { /* First example from X9.31 (D.1.1).  */
     774             :       "(genkey\n"
     775             :       "  (rsa\n"
     776             :       "    (nbits 4:1024)\n"
     777             :       "    (rsa-use-e 1:3)\n"
     778             :       "    (derive-parms\n"
     779             :       "      (Xp1 #1A1916DDB29B4EB7EB6732E128#)\n"
     780             :       "      (Xp2 #192E8AAC41C576C822D93EA433#)\n"
     781             :       "      (Xp  #D8CD81F035EC57EFE822955149D3BFF70C53520D\n"
     782             :       "            769D6D76646C7A792E16EBD89FE6FC5B605A6493\n"
     783             :       "            39DFC925A86A4C6D150B71B9EEA02D68885F5009\n"
     784             :       "            B98BD984#)\n"
     785             :       "      (Xq1 #1A5CF72EE770DE50CB09ACCEA9#)\n"
     786             :       "      (Xq2 #134E4CAA16D2350A21D775C404#)\n"
     787             :       "      (Xq  #CC1092495D867E64065DEE3E7955F2EBC7D47A2D\n"
     788             :       "            7C9953388F97DDDC3E1CA19C35CA659EDC2FC325\n"
     789             :       "            6D29C2627479C086A699A49C4C9CEE7EF7BD1B34\n"
     790             :       "            321DE34A#))))\n",
     791             :       "1CCDA20BCFFB8D517EE9666866621B11822C7950D55F4BB5BEE37989A7D173"
     792             :       "12E326718BE0D79546EAAE87A56623B919B1715FFBD7F16028FC4007741961"
     793             :       "C88C5D7B4DAAAC8D36A98C9EFBB26C8A4A0E6BC15B358E528A1AC9D0F042BE"
     794             :       "B93BCA16B541B33F80C933A3B769285C462ED5677BFE89DF07BED5C127FD13"
     795             :       "241D3C4B"
     796             :     },
     797             : 
     798             :     { /* Second example from X9.31 (D.2.1).  */
     799             :       "(genkey\n"
     800             :       "  (rsa\n"
     801             :       "    (nbits 4:1536)\n"
     802             :       "    (rsa-use-e 1:3)\n"
     803             :       "    (derive-parms\n"
     804             :       "      (Xp1 #18272558B61316348297EACA74#)\n"
     805             :       "      (Xp2 #1E970E8C6C97CEF91F05B0FA80#)\n"
     806             :       "      (Xp  #F7E943C7EF2169E930DCF23FE389EF7507EE8265\n"
     807             :       "            0D42F4A0D3A3CEFABE367999BB30EE680B2FE064\n"
     808             :       "            60F707F46005F8AA7CBFCDDC4814BBE7F0F8BC09\n"
     809             :       "            318C8E51A48D134296E40D0BBDD282DCCBDDEE1D\n"
     810             :       "            EC86F0B1C96EAFF5CDA70F9AEB6EE31E#)\n"
     811             :       "      (Xq1 #11FDDA6E8128DC1629F75192BA#)\n"
     812             :       "      (Xq2 #18AB178ECA907D72472F65E480#)\n"
     813             :       "      (Xq  #C47560011412D6E13E3E7D007B5C05DBF5FF0D0F\n"
     814             :       "            CFF1FA2070D16C7ABA93EDFB35D8700567E5913D\n"
     815             :       "            B734E3FBD15862EBC59FA0425DFA131E549136E8\n"
     816             :       "            E52397A8ABE4705EC4877D4F82C4AAC651B33DA6\n"
     817             :       "            EA14B9D5F2A263DC65626E4D6CEAC767#))))\n",
     818             :       "1FB56069985F18C4519694FB71055721A01F14422DC901C35B03A64D4A5BD1"
     819             :       "259D573305F5B056AC931B82EDB084E39A0FD1D1A86CC5B147A264F7EF4EB2"
     820             :       "0ED1E7FAAE5CAE4C30D5328B7F74C3CAA72C88B70DED8EDE207B8629DA2383"
     821             :       "B78C3CE1CA3F9F218D78C938B35763AF2A8714664CC57F5CECE2413841F5E9"
     822             :       "EDEC43B728E25A41BF3E1EF8D9EEE163286C9F8BF0F219D3B322C3E4B0389C"
     823             :       "2E8BB28DC04C47DA2BF38823731266D2CF6CC3FC181738157624EF051874D0"
     824             :       "BBCCB9F65C83"
     825             :       /* Note that this example in X9.31 gives this value for D:
     826             : 
     827             :         "7ED581A6617C6311465A53EDC4155C86807C5108B724070D6C0E9935296F44"
     828             :         "96755CCC17D6C15AB24C6E0BB6C2138E683F4746A1B316C51E8993DFBD3AC8"
     829             :         "3B479FEAB972B930C354CA2DFDD30F2A9CB222DC37B63B7881EE18A7688E0E"
     830             :         "DE30F38728FE7C8635E324E2CD5D8EBCAA1C51993315FD73B38904E107D7A7"
     831             :         "B7B10EDCA3896906FCF87BE367BB858CA1B27E2FC3C8674ECC8B0F92C0E270"
     832             :         "BA2ECA3701311F68AFCE208DCC499B4B3DB30FF0605CE055D893BC1461D342"
     833             :         "EF32E7D9720B"
     834             : 
     835             :         This is a bug in X9.31, obviously introduced by using
     836             : 
     837             :            d = e^{-1} mod (p-1)(q-1)
     838             : 
     839             :          instead of using the universal exponent as required by 4.1.3:
     840             : 
     841             :            d = e^{-1} mod lcm(p-1,q-1)
     842             : 
     843             :          The examples in X9.31 seem to be pretty buggy, see
     844             :          cipher/primegen.c for another bug.  Not only that I had to
     845             :          spend 100 USD for the 66 pages of the document, it also took
     846             :          me several hours to figure out that the bugs are in the
     847             :          document and not in my code.
     848             :        */
     849             :     },
     850             : 
     851             :     { /* First example from NIST RSAVS (B.1.1).  */
     852             :       "(genkey\n"
     853             :       "  (rsa\n"
     854             :       "    (nbits 4:1024)\n"
     855             :       "    (rsa-use-e 1:3)\n"
     856             :       "    (derive-parms\n"
     857             :       "      (Xp1 #1ed3d6368e101dab9124c92ac8#)\n"
     858             :       "      (Xp2 #16e5457b8844967ce83cab8c11#)\n"
     859             :       "      (Xp  #b79f2c2493b4b76f329903d7555b7f5f06aaa5ea\n"
     860             :       "            ab262da1dcda8194720672a4e02229a0c71f60ae\n"
     861             :       "            c4f0d2ed8d49ef583ca7d5eeea907c10801c302a\n"
     862             :       "            cab44595#)\n"
     863             :       "      (Xq1 #1a5d9e3fa34fb479bedea412f6#)\n"
     864             :       "      (Xq2 #1f9cca85f185341516d92e82fd#)\n"
     865             :       "      (Xq  #c8387fd38fa33ddcea6a9de1b2d55410663502db\n"
     866             :       "            c225655a9310cceac9f4cf1bce653ec916d45788\n"
     867             :       "            f8113c46bc0fa42bf5e8d0c41120c1612e2ea8bb\n"
     868             :       "            2f389eda#))))\n",
     869             :       "17ef7ad4fd96011b62d76dfb2261b4b3270ca8e07bc501be954f8719ef586b"
     870             :       "f237e8f693dd16c23e7adecc40279dc6877c62ab541df5849883a5254fccfd"
     871             :       "4072a657b7f4663953930346febd6bbd82f9a499038402cbf97fd5f068083a"
     872             :       "c81ad0335c4aab0da19cfebe060a1bac7482738efafea078e21df785e56ea0"
     873             :       "dc7e8feb"
     874             :     },
     875             : 
     876             :     { /* Second example from NIST RSAVS (B.1.1).  */
     877             :       "(genkey\n"
     878             :       "  (rsa\n"
     879             :       "    (nbits 4:1536)\n"
     880             :       "    (rsa-use-e 1:3)\n"
     881             :       "    (derive-parms\n"
     882             :       "      (Xp1 #1e64c1af460dff8842c22b64d0#)\n"
     883             :       "      (Xp2 #1e948edcedba84039c81f2ac0c#)\n"
     884             :       "      (Xp  #c8c67df894c882045ede26a9008ab09ea0672077\n"
     885             :       "            d7bc71d412511cd93981ddde8f91b967da404056\n"
     886             :       "            c39f105f7f239abdaff92923859920f6299e82b9\n"
     887             :       "            5bd5b8c959948f4a034d81613d6235a3953b49ce\n"
     888             :       "            26974eb7bb1f14843841281b363b9cdb#)\n"
     889             :       "      (Xq1 #1f3df0f017ddd05611a97b6adb#)\n"
     890             :       "      (Xq2 #143edd7b22d828913abf24ca4d#)\n"
     891             :       "      (Xq  #f15147d0e7c04a1e3f37adde802cdc610999bf7a\n"
     892             :       "            b0088434aaeda0c0ab3910b14d2ce56cb66bffd9\n"
     893             :       "            7552195fae8b061077e03920814d8b9cfb5a3958\n"
     894             :       "            b3a82c2a7fc97e55db543948d3396289245336ec\n"
     895             :       "            9e3cb308cc655aebd766340da8921383#))))\n",
     896             :       "1f8b19f3f5f2ac9fc599f110cad403dcd9bdf5f7f00fb2790e78e820398184"
     897             :       "1f3fb3dd230fb223d898f45719d9b2d3525587ff2b8bcc7425e40550a5b536"
     898             :       "1c8e9c1d26e83fbd9c33c64029c0e878b829d55def12912b73d94fd758c461"
     899             :       "0f473e230c41b5e4c86e27c5a5029d82c811c88525d0269b95bd2ff272994a"
     900             :       "dbd80f2c2ecf69065feb8abd8b445b9c6d306b1585d7d3d7576d49842bc7e2"
     901             :       "8b4a2f88f4a47e71c3edd35fdf83f547ea5c2b532975c551ed5268f748b2c4"
     902             :       "2ccf8a84835b"
     903             :     }
     904             :   };
     905             :   gpg_error_t err;
     906             :   gcry_sexp_t key_spec, key, pub_key, sec_key;
     907             :   gcry_mpi_t d_expected, d_have;
     908             : 
     909           4 :   if (what < 0 && what >= sizeof testtable)
     910           0 :     die ("invalid WHAT value\n");
     911             : 
     912           4 :   err = gcry_sexp_new (&key_spec, testtable[what].param, 0, 1);
     913           4 :   if (err)
     914           0 :     die ("error creating S-expression [%d]: %s\n", what, gpg_strerror (err));
     915             : 
     916           4 :   err = gcry_pk_genkey (&key, key_spec);
     917           4 :   gcry_sexp_release (key_spec);
     918           4 :   if (err)
     919           0 :     die ("error generating RSA key [%d]: %s\n", what, gpg_strerror (err));
     920             : 
     921           4 :   pub_key = gcry_sexp_find_token (key, "public-key", 0);
     922           4 :   if (!pub_key)
     923           0 :     die ("public part missing in key [%d]\n", what);
     924             : 
     925           4 :   sec_key = gcry_sexp_find_token (key, "private-key", 0);
     926           4 :   if (!sec_key)
     927           0 :     die ("private part missing in key [%d]\n", what);
     928             : 
     929           4 :   err = gcry_mpi_scan
     930           4 :     (&d_expected, GCRYMPI_FMT_HEX, testtable[what].expected_d, 0, NULL);
     931           4 :   if (err)
     932           0 :     die ("error converting string [%d]\n", what);
     933             : 
     934           4 :   if (verbose > 1)
     935           0 :     show_sexp ("generated key:\n", key);
     936             : 
     937           4 :   d_have = key_param_from_sexp (sec_key, "rsa", "d");
     938           4 :   if (!d_have)
     939           0 :     die ("parameter d not found in RSA secret key [%d]\n", what);
     940           4 :   if (gcry_mpi_cmp (d_expected, d_have))
     941             :     {
     942           0 :       show_sexp (NULL, sec_key);
     943           0 :       die ("parameter d does match expected value [%d]\n", what);
     944             :     }
     945           4 :   gcry_mpi_release (d_expected);
     946           4 :   gcry_mpi_release (d_have);
     947             : 
     948           4 :   gcry_sexp_release (key);
     949           4 :   gcry_sexp_release (pub_key);
     950           4 :   gcry_sexp_release (sec_key);
     951           4 : }
     952             : 
     953             : 
     954             : 
     955             : static void
     956           1 : check_ecc_sample_key (void)
     957             : {
     958             :   static const char ecc_private_key[] =
     959             :     "(private-key\n"
     960             :     " (ecdsa\n"
     961             :     "  (curve \"NIST P-256\")\n"
     962             :     "  (q #04D4F6A6738D9B8D3A7075C1E4EE95015FC0C9B7E4272D2BEB6644D3609FC781"
     963             :     "B71F9A8072F58CB66AE2F89BB12451873ABF7D91F9E1FBF96BF2F70E73AAC9A283#)\n"
     964             :     "  (d #5A1EF0035118F19F3110FB81813D3547BCE1E5BCE77D1F744715E1D5BBE70378#)"
     965             :     "))";
     966             :   static const char ecc_private_key_wo_q[] =
     967             :     "(private-key\n"
     968             :     " (ecdsa\n"
     969             :     "  (curve \"NIST P-256\")\n"
     970             :     "  (d #5A1EF0035118F19F3110FB81813D3547BCE1E5BCE77D1F744715E1D5BBE70378#)"
     971             :     "))";
     972             :   static const char ecc_public_key[] =
     973             :     "(public-key\n"
     974             :     " (ecdsa\n"
     975             :     "  (curve \"NIST P-256\")\n"
     976             :     "  (q #04D4F6A6738D9B8D3A7075C1E4EE95015FC0C9B7E4272D2BEB6644D3609FC781"
     977             :     "B71F9A8072F58CB66AE2F89BB12451873ABF7D91F9E1FBF96BF2F70E73AAC9A283#)"
     978             :     "))";
     979             :   static const char hash_string[] =
     980             :     "(data (flags raw)\n"
     981             :     " (value #00112233445566778899AABBCCDDEEFF"
     982             :     /* */    "000102030405060708090A0B0C0D0E0F#))";
     983             :   static const char hash2_string[] =
     984             :     "(data (flags raw)\n"
     985             :     " (hash sha1 #00112233445566778899AABBCCDDEEFF"
     986             :     /* */    "000102030405060708090A0B0C0D0E0F"
     987             :     /* */    "000102030405060708090A0B0C0D0E0F"
     988             :     /* */    "00112233445566778899AABBCCDDEEFF#))";
     989             :   /* hash2, but longer than curve length, so it will be truncated */
     990             :   static const char hash3_string[] =
     991             :     "(data (flags raw)\n"
     992             :     " (hash sha1 #00112233445566778899AABBCCDDEEFF"
     993             :     /* */    "000102030405060708090A0B0C0D0E0F"
     994             :     /* */    "000102030405060708090A0B0C0D0E0F"
     995             :     /* */    "00112233445566778899AABBCCDDEEFF"
     996             :     /* */    "000102030405060708090A0B0C0D0E0F#))";
     997             : 
     998             :   gpg_error_t err;
     999             :   gcry_sexp_t key, hash, hash2, hash3, sig, sig2;
    1000             : 
    1001           1 :   if (verbose)
    1002           0 :     fprintf (stderr, "Checking sample ECC key.\n");
    1003             : 
    1004           1 :   if ((err = gcry_sexp_new (&hash, hash_string, 0, 1)))
    1005           0 :     die ("line %d: %s", __LINE__, gpg_strerror (err));
    1006             : 
    1007           1 :   if ((err = gcry_sexp_new (&hash2, hash2_string, 0, 1)))
    1008           0 :     die ("line %d: %s", __LINE__, gpg_strerror (err));
    1009             : 
    1010           1 :   if ((err = gcry_sexp_new (&hash3, hash3_string, 0, 1)))
    1011           0 :     die ("line %d: %s", __LINE__, gpg_strerror (err));
    1012             : 
    1013           1 :   if ((err = gcry_sexp_new (&key, ecc_private_key, 0, 1)))
    1014           0 :     die ("line %d: %s", __LINE__, gpg_strerror (err));
    1015             : 
    1016           1 :   if ((err = gcry_pk_sign (&sig, hash, key)))
    1017           0 :     die ("gcry_pk_sign failed: %s", gpg_strerror (err));
    1018             : 
    1019           1 :   gcry_sexp_release (key);
    1020           1 :   if ((err = gcry_sexp_new (&key, ecc_public_key, 0, 1)))
    1021           0 :     die ("line %d: %s", __LINE__, gpg_strerror (err));
    1022             : 
    1023           1 :   if ((err = gcry_pk_verify (sig, hash, key)))
    1024           0 :     die ("gcry_pk_verify failed: %s", gpg_strerror (err));
    1025             : 
    1026             :   /* Verify hash truncation */
    1027           1 :   gcry_sexp_release (key);
    1028           1 :   if ((err = gcry_sexp_new (&key, ecc_private_key, 0, 1)))
    1029           0 :     die ("line %d: %s", __LINE__, gpg_strerror (err));
    1030             : 
    1031           1 :   if ((err = gcry_pk_sign (&sig2, hash2, key)))
    1032           0 :     die ("gcry_pk_sign failed: %s", gpg_strerror (err));
    1033             : 
    1034           1 :   gcry_sexp_release (sig);
    1035           1 :   if ((err = gcry_pk_sign (&sig, hash3, key)))
    1036           0 :     die ("gcry_pk_sign failed: %s", gpg_strerror (err));
    1037             : 
    1038           1 :   gcry_sexp_release (key);
    1039           1 :   if ((err = gcry_sexp_new (&key, ecc_public_key, 0, 1)))
    1040           0 :     die ("line %d: %s", __LINE__, gpg_strerror (err));
    1041             : 
    1042           1 :   if ((err = gcry_pk_verify (sig, hash2, key)))
    1043           0 :     die ("gcry_pk_verify failed: %s", gpg_strerror (err));
    1044             : 
    1045           1 :   if ((err = gcry_pk_verify (sig2, hash3, key)))
    1046           0 :     die ("gcry_pk_verify failed: %s", gpg_strerror (err));
    1047             : 
    1048             :   /* Now try signing without the Q parameter.  */
    1049             : 
    1050           1 :   gcry_sexp_release (key);
    1051           1 :   if ((err = gcry_sexp_new (&key, ecc_private_key_wo_q, 0, 1)))
    1052           0 :     die ("line %d: %s", __LINE__, gpg_strerror (err));
    1053             : 
    1054           1 :   gcry_sexp_release (sig);
    1055           1 :   if ((err = gcry_pk_sign (&sig, hash, key)))
    1056           0 :     die ("gcry_pk_sign without Q failed: %s", gpg_strerror (err));
    1057             : 
    1058           1 :   gcry_sexp_release (key);
    1059           1 :   if ((err = gcry_sexp_new (&key, ecc_public_key, 0, 1)))
    1060           0 :     die ("line %d: %s", __LINE__, gpg_strerror (err));
    1061             : 
    1062           1 :   if ((err = gcry_pk_verify (sig, hash, key)))
    1063           0 :     die ("gcry_pk_verify signed without Q failed: %s", gpg_strerror (err));
    1064             : 
    1065           1 :   gcry_sexp_release (sig);
    1066           1 :   gcry_sexp_release (sig2);
    1067           1 :   gcry_sexp_release (key);
    1068           1 :   gcry_sexp_release (hash);
    1069           1 :   gcry_sexp_release (hash2);
    1070           1 :   gcry_sexp_release (hash3);
    1071           1 : }
    1072             : 
    1073             : 
    1074             : static void
    1075           1 : check_ed25519ecdsa_sample_key (void)
    1076             : {
    1077             :   static const char ecc_private_key[] =
    1078             :     "(private-key\n"
    1079             :     " (ecc\n"
    1080             :     "  (curve \"Ed25519\")\n"
    1081             :     "  (q #044C056555BE4084BB3D8D8895FDF7C2893DFE0256251923053010977D12658321"
    1082             :     "        156D1ADDC07987713A418783658B476358D48D582DB53233D9DED3C1C2577B04#)"
    1083             :     "  (d #09A0C38E0F1699073541447C19DA12E3A07A7BFDB0C186E4AC5BCE6F23D55252#)"
    1084             :     "))";
    1085             :   static const char ecc_private_key_wo_q[] =
    1086             :     "(private-key\n"
    1087             :     " (ecc\n"
    1088             :     "  (curve \"Ed25519\")\n"
    1089             :     "  (d #09A0C38E0F1699073541447C19DA12E3A07A7BFDB0C186E4AC5BCE6F23D55252#)"
    1090             :     "))";
    1091             :   static const char ecc_public_key[] =
    1092             :     "(public-key\n"
    1093             :     " (ecc\n"
    1094             :     "  (curve \"Ed25519\")\n"
    1095             :     "  (q #044C056555BE4084BB3D8D8895FDF7C2893DFE0256251923053010977D12658321"
    1096             :     "        156D1ADDC07987713A418783658B476358D48D582DB53233D9DED3C1C2577B04#)"
    1097             :     "))";
    1098             :   static const char ecc_public_key_comp[] =
    1099             :     "(public-key\n"
    1100             :     " (ecc\n"
    1101             :     "  (curve \"Ed25519\")\n"
    1102             :     "  (q #047b57c2c1d3ded93332b52d588dd45863478b658387413a718779c0dd1a6d95#)"
    1103             :     "))";
    1104             :   static const char hash_string[] =
    1105             :     "(data (flags rfc6979)\n"
    1106             :     " (hash sha256 #00112233445566778899AABBCCDDEEFF"
    1107             :     /* */          "000102030405060708090A0B0C0D0E0F#))";
    1108             : 
    1109             :   gpg_error_t err;
    1110             :   gcry_sexp_t key, hash, sig;
    1111             : 
    1112           1 :   if (verbose)
    1113           0 :     fprintf (stderr, "Checking sample Ed25519/ECDSA key.\n");
    1114             : 
    1115             :   /* Sign.  */
    1116           1 :   if ((err = gcry_sexp_new (&hash, hash_string, 0, 1)))
    1117           0 :     die ("line %d: %s", __LINE__, gpg_strerror (err));
    1118           1 :   if ((err = gcry_sexp_new (&key, ecc_private_key, 0, 1)))
    1119           0 :     die ("line %d: %s", __LINE__, gpg_strerror (err));
    1120           1 :   if ((err = gcry_pk_sign (&sig, hash, key)))
    1121           0 :     die ("gcry_pk_sign failed: %s", gpg_strerror (err));
    1122             : 
    1123             :   /* Verify.  */
    1124           1 :   gcry_sexp_release (key);
    1125           1 :   if ((err = gcry_sexp_new (&key, ecc_public_key, 0, 1)))
    1126           0 :     die ("line %d: %s", __LINE__, gpg_strerror (err));
    1127           1 :   if ((err = gcry_pk_verify (sig, hash, key)))
    1128           0 :     die ("gcry_pk_verify failed: %s", gpg_strerror (err));
    1129             : 
    1130             :   /* Verify again using a compressed public key.  */
    1131           1 :   gcry_sexp_release (key);
    1132           1 :   if ((err = gcry_sexp_new (&key, ecc_public_key_comp, 0, 1)))
    1133           0 :     die ("line %d: %s", __LINE__, gpg_strerror (err));
    1134           1 :   if ((err = gcry_pk_verify (sig, hash, key)))
    1135           0 :     die ("gcry_pk_verify failed (comp): %s", gpg_strerror (err));
    1136             : 
    1137             :   /* Sign without a Q parameter.  */
    1138           1 :   gcry_sexp_release (key);
    1139           1 :   if ((err = gcry_sexp_new (&key, ecc_private_key_wo_q, 0, 1)))
    1140           0 :     die ("line %d: %s", __LINE__, gpg_strerror (err));
    1141           1 :   gcry_sexp_release (sig);
    1142           1 :   if ((err = gcry_pk_sign (&sig, hash, key)))
    1143           0 :     die ("gcry_pk_sign w/o Q failed: %s", gpg_strerror (err));
    1144             : 
    1145             :   /* Verify.  */
    1146           1 :   gcry_sexp_release (key);
    1147           1 :   if ((err = gcry_sexp_new (&key, ecc_public_key, 0, 1)))
    1148           0 :     die ("line %d: %s", __LINE__, gpg_strerror (err));
    1149           1 :   if ((err = gcry_pk_verify (sig, hash, key)))
    1150           0 :     die ("gcry_pk_verify signed w/o Q failed: %s", gpg_strerror (err));
    1151             : 
    1152             :   /* Verify again using a compressed public key.  */
    1153           1 :   gcry_sexp_release (key);
    1154           1 :   if ((err = gcry_sexp_new (&key, ecc_public_key_comp, 0, 1)))
    1155           0 :     die ("line %d: %s", __LINE__, gpg_strerror (err));
    1156           1 :   if ((err = gcry_pk_verify (sig, hash, key)))
    1157           0 :     die ("gcry_pk_verify signed w/o Q failed (comp): %s", gpg_strerror (err));
    1158             : 
    1159           1 :   extract_cmp_data (sig, "r", ("a63123a783ef29b8276e08987daca4"
    1160             :                                "655d0179e22199bf63691fd88eb64e15"));
    1161           1 :   extract_cmp_data (sig, "s", ("0d9b45c696ab90b96b08812b485df185"
    1162             :                                "623ddaf5d02fa65ca5056cb6bd0f16f1"));
    1163             : 
    1164           1 :   gcry_sexp_release (sig);
    1165           1 :   gcry_sexp_release (key);
    1166           1 :   gcry_sexp_release (hash);
    1167           1 : }
    1168             : 
    1169             : 
    1170             : int
    1171           1 : main (int argc, char **argv)
    1172             : {
    1173           1 :   int debug = 0;
    1174             :   int i;
    1175             : 
    1176           1 :   if (argc > 1 && !strcmp (argv[1], "--verbose"))
    1177           0 :     verbose = 1;
    1178           1 :   else if (argc > 1 && !strcmp (argv[1], "--debug"))
    1179             :     {
    1180           0 :       verbose = 2;
    1181           0 :       debug = 1;
    1182             :     }
    1183             : 
    1184           1 :   gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
    1185           1 :   if (!gcry_check_version (GCRYPT_VERSION))
    1186           0 :     die ("version mismatch\n");
    1187           1 :   gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
    1188           1 :   if (debug)
    1189           0 :     gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u , 0);
    1190             :   /* No valuable keys are create, so we can speed up our RNG. */
    1191           1 :   gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
    1192             : 
    1193           3 :   for (i=0; i < 2; i++)
    1194           2 :     check_run ();
    1195             : 
    1196           5 :   for (i=0; i < 4; i++)
    1197           4 :     check_x931_derived_key (i);
    1198             : 
    1199           1 :   check_ecc_sample_key ();
    1200           1 :   check_ed25519ecdsa_sample_key ();
    1201             : 
    1202           1 :   return !!error_count;
    1203             : }

Generated by: LCOV version 1.11