diff options
-rw-r--r-- | tests/ChangeLog | 6 | ||||
-rw-r--r-- | tests/Makefile.am | 2 | ||||
-rw-r--r-- | tests/prime.c | 99 |
3 files changed, 106 insertions, 1 deletions
diff --git a/tests/ChangeLog b/tests/ChangeLog index 6a2d37ad..3eb947a9 100644 --- a/tests/ChangeLog +++ b/tests/ChangeLog @@ -2,6 +2,12 @@ * pubkey.c (check_keys_crypt): Fix for compatibility mode. +2003-09-02 Moritz Schulte <mo@g10code.com> + + * Makefile.am (TESTS): Added: prime. + + * prime.c: New file. + 2003-08-27 Moritz Schulte <mo@g10code.com> * basic.c (check_ciphers): Added: Serpent. diff --git a/tests/Makefile.am b/tests/Makefile.am index 057dd698..c643f9ee 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -18,7 +18,7 @@ ## Process this file with automake to produce Makefile.in -TESTS = register ac basic tsexp keygen pubkey +TESTS = prime register ac basic tsexp keygen pubkey INCLUDES = -I$(top_srcdir)/src LDADD = ../src/libgcrypt.la diff --git a/tests/prime.c b/tests/prime.c new file mode 100644 index 00000000..aa101491 --- /dev/null +++ b/tests/prime.c @@ -0,0 +1,99 @@ +/* prime.c - part of the Libgcrypt test suite. + Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. */ + +#include <assert.h> +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +#include "../src/gcrypt.h" + +static int verbose, debug; + +static void +die (const char *format, ...) +{ + va_list arg_ptr; + + va_start (arg_ptr, format); + vfprintf (stderr, format, arg_ptr); + va_end (arg_ptr); + exit (1); +} + +static void +check_primes (void) +{ + gcry_error_t err = GPG_ERR_NO_ERROR; + gcry_mpi_t *factors = NULL; + gcry_mpi_t prime = NULL; + unsigned int i = 0; + struct prime_spec + { + unsigned int prime_bits; + unsigned int factor_bits; + unsigned int flags; + } prime_specs[] = + { + { 1024, 100, GCRY_PRIME_FLAG_SPECIAL_FACTOR }, + { 0 }, + }; + + for (i = 0; prime_specs[i].prime_bits; i++) + { + err = gcry_prime_generate (&prime, + prime_specs[i].prime_bits, + prime_specs[i].factor_bits, + &factors, + NULL, NULL, + GCRY_WEAK_RANDOM, + prime_specs[i].flags); + assert (! err); + printf ("%i: ", i); + gcry_mpi_dump (prime); + putchar ('\n'); + err = gcry_prime_check (prime, 0); + assert (! err); + gcry_mpi_add_ui (prime, prime, 1); + err = gcry_prime_check (prime, 0); + assert (err); + } +} + +int +main (int argc, char **argv) +{ + int debug = 0; + + if ((argc > 1) && (! strcmp (argv[1], "--verbose"))) + verbose = 1; + else if ((argc > 1) && (! strcmp (argv[1], "--debug"))) + verbose = debug = 1; + + if (! gcry_check_version (GCRYPT_VERSION)) + die ("version mismatch\n"); + + gcry_control (GCRYCTL_DISABLE_SECMEM, 0); + gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0); + if (debug) + gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0); + + check_primes (); + + return 0; +} |