Upstream sync.
[shishi.git] / crypto / tests / keygen.c
bloba65a0c39ec01453f6d6dcf8698a1c5516a0ca48e
1 /* keygen.c - key generation regression tests
2 * Copyright (C) 2003 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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdarg.h>
25 #include "../src/gcrypt.h"
29 static int verbose;
30 static int error_count;
32 static void
33 fail ( const char *format, ... )
35 va_list arg_ptr ;
37 va_start( arg_ptr, format ) ;
38 vfprintf (stderr, format, arg_ptr );
39 va_end(arg_ptr);
40 error_count++;
43 static void
44 die ( const char *format, ... )
46 va_list arg_ptr ;
48 va_start( arg_ptr, format ) ;
49 vfprintf (stderr, format, arg_ptr );
50 va_end(arg_ptr);
51 exit (1);
55 static void
56 print_mpi (const char *text, GcryMPI a)
58 char *buf;
59 void *bufaddr = &buf;
60 int rc;
62 rc = gcry_mpi_aprint (GCRYMPI_FMT_HEX, bufaddr, NULL, a);
63 if (rc)
64 fprintf (stderr, "%s=[error printing number: %s]\n",
65 text, gcry_strerror (rc));
66 else
68 fprintf (stderr, "%s=0x%s\n", text, buf);
69 gcry_free (buf);
75 static void
76 check_generated_rsa_key (GcrySexp key, unsigned long expected_e)
78 GcrySexp skey, pkey, list;
80 pkey = gcry_sexp_find_token (key, "public-key", 0);
81 if (!pkey)
82 fail ("public part missing in return value\n");
83 else
85 GcryMPI e = NULL;
87 list = gcry_sexp_find_token (pkey, "e", 0);
88 if (!list || !(e=gcry_sexp_nth_mpi (list, 1, 0)) )
89 fail ("public exponent not found\n");
90 else if (!expected_e)
92 if (verbose)
93 print_mpi ("e", e);
95 else if ( gcry_mpi_cmp_ui (e, expected_e))
97 print_mpi ("e", e);
98 fail ("public exponent is not %lu\n", expected_e);
100 gcry_sexp_release (list);
101 gcry_mpi_release (e);
102 gcry_sexp_release (pkey);
105 skey = gcry_sexp_find_token (key, "private-key", 0);
106 if (!skey)
107 fail ("private part missing in return value\n");
108 else
110 int rc = gcry_pk_testkey (skey);
111 if (rc)
112 fail ("gcry_pk_testkey failed: %s\n", gcry_strerror (rc));
113 gcry_sexp_release (skey);
118 static void
119 check_rsa_keys (void)
121 GcrySexp keyparm, key;
122 int rc;
124 if (verbose)
125 fprintf (stderr, "creating 1024 bit RSA key using old interface\n");
126 rc = gcry_sexp_new (&keyparm,
127 "(genkey\n"
128 " (rsa\n"
129 " (nbits 4:1024)\n"
130 " ))", 0, 1);
131 if (rc)
132 die ("error creating S-expression: %s\n", gcry_strerror (rc));
133 rc = gcry_pk_genkey (&key, keyparm);
134 gcry_sexp_release (keyparm);
135 if (rc)
136 die ("error generating RSA key: %s\n", gcry_strerror (rc));
138 check_generated_rsa_key (key, 65537);
139 gcry_sexp_release (key);
141 if (verbose)
142 fprintf (stderr, "creating 512 bit RSA key with e=257\n");
143 rc = gcry_sexp_new (&keyparm,
144 "(genkey\n"
145 " (rsa\n"
146 " (nbits 3:512)\n"
147 " (rsa-use-e 3:257)\n"
148 " ))", 0, 1);
149 if (rc)
150 die ("error creating S-expression: %s\n", gcry_strerror (rc));
151 rc = gcry_pk_genkey (&key, keyparm);
152 gcry_sexp_release (keyparm);
153 if (rc)
154 die ("error generating RSA key: %s\n", gcry_strerror (rc));
156 check_generated_rsa_key (key, 257);
157 gcry_sexp_release (key);
159 if (verbose)
160 fprintf (stderr, "creating 512 bit RSA key with default e\n");
161 rc = gcry_sexp_new (&keyparm,
162 "(genkey\n"
163 " (rsa\n"
164 " (nbits 3:512)\n"
165 " (rsa-use-e 1:0)\n"
166 " ))", 0, 1);
167 if (rc)
168 die ("error creating S-expression: %s\n", gcry_strerror (rc));
169 rc = gcry_pk_genkey (&key, keyparm);
170 gcry_sexp_release (keyparm);
171 if (rc)
172 die ("error generating RSA key: %s\n", gcry_strerror (rc));
174 check_generated_rsa_key (key, 0); /* We don't expect a constant exponent. */
175 gcry_sexp_release (key);
183 main (int argc, char **argv)
185 int debug = 0;
187 if (argc > 1 && !strcmp (argv[1], "--verbose"))
188 verbose = 1;
189 else if (argc > 1 && !strcmp (argv[1], "--debug"))
190 verbose = debug = 1;
192 if (!gcry_check_version (GCRYPT_VERSION))
193 die ("version mismatch\n");
194 gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
195 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
196 if (debug)
197 gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u , 0);
198 /* No valuable keys are create, so we can speed up our RNG. */
199 gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
200 check_rsa_keys ();
202 return error_count? 1:0;