1/* prime.c - part of the Libgcrypt test suite. 2 Copyright (C) 2001, 2002, 2003, 2005 Free Software Foundation, Inc. 3 4 This program is free software; you can redistribute it and/or 5 modify it under the terms of the GNU General Public License as 6 published by the Free Software Foundation; either version 2 of the 7 License, or (at your option) any later version. 8 9 This program is distributed in the hope that it will be useful, but 10 WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 General Public License for more details. 13 14 You should have received a copy of the GNU General Public License 15 along with this program; if not, write to the Free Software 16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 17 USA. */ 18 19#ifdef HAVE_CONFIG_H 20#include <config.h> 21#endif 22#include <assert.h> 23#include <stdio.h> 24#include <string.h> 25#include <stdlib.h> 26 27#include "../src/gcrypt.h" 28 29static int verbose; 30 31static void 32die (const char *format, ...) 33{ 34 va_list arg_ptr; 35 36 va_start (arg_ptr, format); 37 vfprintf (stderr, format, arg_ptr); 38 va_end (arg_ptr); 39 exit (1); 40} 41 42static void 43check_primes (void) 44{ 45 gcry_error_t err = GPG_ERR_NO_ERROR; 46 gcry_mpi_t *factors = NULL; 47 gcry_mpi_t prime = NULL; 48 gcry_mpi_t g; 49 unsigned int i = 0; 50 struct prime_spec 51 { 52 unsigned int prime_bits; 53 unsigned int factor_bits; 54 unsigned int flags; 55 } prime_specs[] = 56 { 57 { 1024, 100, GCRY_PRIME_FLAG_SPECIAL_FACTOR }, 58 { 128, 0, 0 }, 59 { 0 }, 60 }; 61 62 for (i = 0; prime_specs[i].prime_bits; i++) 63 { 64 err = gcry_prime_generate (&prime, 65 prime_specs[i].prime_bits, 66 prime_specs[i].factor_bits, 67 &factors, 68 NULL, NULL, 69 GCRY_WEAK_RANDOM, 70 prime_specs[i].flags); 71 assert (! err); 72 if (verbose) 73 { 74 fprintf (stderr, "test %d: p = ", i); 75 gcry_mpi_dump (prime); 76 putc ('\n', stderr); 77 } 78 79 err = gcry_prime_check (prime, 0); 80 assert (! err); 81 82 err = gcry_prime_group_generator (&g, prime, factors, NULL); 83 assert (!err); 84 gcry_prime_release_factors (factors); factors = NULL; 85 86 if (verbose) 87 { 88 fprintf (stderr, " %d: g = ", i); 89 gcry_mpi_dump (g); 90 putc ('\n', stderr); 91 } 92 gcry_mpi_release (g); 93 94 95 gcry_mpi_add_ui (prime, prime, 1); 96 err = gcry_prime_check (prime, 0); 97 assert (err); 98 } 99} 100 101int 102main (int argc, char **argv) 103{ 104 int debug = 0; 105 106 if ((argc > 1) && (! strcmp (argv[1], "--verbose"))) 107 verbose = 1; 108 else if ((argc > 1) && (! strcmp (argv[1], "--debug"))) 109 verbose = debug = 1; 110 111 gcry_control (GCRYCTL_DISABLE_SECMEM, 0); 112 if (! gcry_check_version (GCRYPT_VERSION)) 113 die ("version mismatch\n"); 114 115 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0); 116 if (debug) 117 gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0); 118 119 check_primes (); 120 121 return 0; 122} 123