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
25 #include "../src/gcrypt.h"
30 static int error_count
;
33 fail ( const char *format
, ... )
37 va_start( arg_ptr
, format
) ;
38 vfprintf (stderr
, format
, arg_ptr
);
44 die ( const char *format
, ... )
48 va_start( arg_ptr
, format
) ;
49 vfprintf (stderr
, format
, arg_ptr
);
56 print_mpi (const char *text
, GcryMPI a
)
62 rc
= gcry_mpi_aprint (GCRYMPI_FMT_HEX
, bufaddr
, NULL
, a
);
64 fprintf (stderr
, "%s=[error printing number: %s]\n",
65 text
, gcry_strerror (rc
));
68 fprintf (stderr
, "%s=0x%s\n", text
, buf
);
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);
82 fail ("public part missing in return value\n");
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");
95 else if ( gcry_mpi_cmp_ui (e
, expected_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);
107 fail ("private part missing in return value\n");
110 int rc
= gcry_pk_testkey (skey
);
112 fail ("gcry_pk_testkey failed: %s\n", gcry_strerror (rc
));
113 gcry_sexp_release (skey
);
119 check_rsa_keys (void)
121 GcrySexp keyparm
, key
;
125 fprintf (stderr
, "creating 1024 bit RSA key using old interface\n");
126 rc
= gcry_sexp_new (&keyparm
,
132 die ("error creating S-expression: %s\n", gcry_strerror (rc
));
133 rc
= gcry_pk_genkey (&key
, keyparm
);
134 gcry_sexp_release (keyparm
);
136 die ("error generating RSA key: %s\n", gcry_strerror (rc
));
138 check_generated_rsa_key (key
, 65537);
139 gcry_sexp_release (key
);
142 fprintf (stderr
, "creating 512 bit RSA key with e=257\n");
143 rc
= gcry_sexp_new (&keyparm
,
147 " (rsa-use-e 3:257)\n"
150 die ("error creating S-expression: %s\n", gcry_strerror (rc
));
151 rc
= gcry_pk_genkey (&key
, keyparm
);
152 gcry_sexp_release (keyparm
);
154 die ("error generating RSA key: %s\n", gcry_strerror (rc
));
156 check_generated_rsa_key (key
, 257);
157 gcry_sexp_release (key
);
160 fprintf (stderr
, "creating 512 bit RSA key with default e\n");
161 rc
= gcry_sexp_new (&keyparm
,
168 die ("error creating S-expression: %s\n", gcry_strerror (rc
));
169 rc
= gcry_pk_genkey (&key
, keyparm
);
170 gcry_sexp_release (keyparm
);
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
)
187 if (argc
> 1 && !strcmp (argv
[1], "--verbose"))
189 else if (argc
> 1 && !strcmp (argv
[1], "--debug"))
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);
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);
202 return error_count
? 1:0;