prime.c revision 238405
1142425Snectar/* ==================================================================== 2142425Snectar * Copyright (c) 2004 The OpenSSL Project. All rights reserved. 3142425Snectar * 4142425Snectar * Redistribution and use in source and binary forms, with or without 5142425Snectar * modification, are permitted provided that the following conditions 6142425Snectar * are met: 7142425Snectar * 8142425Snectar * 1. Redistributions of source code must retain the above copyright 9142425Snectar * notice, this list of conditions and the following disclaimer. 10142425Snectar * 11142425Snectar * 2. Redistributions in binary form must reproduce the above copyright 12142425Snectar * notice, this list of conditions and the following disclaimer in 13142425Snectar * the documentation and/or other materials provided with the 14142425Snectar * distribution. 15142425Snectar * 16142425Snectar * 3. All advertising materials mentioning features or use of this 17142425Snectar * software must display the following acknowledgment: 18142425Snectar * "This product includes software developed by the OpenSSL Project 19142425Snectar * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 20142425Snectar * 21142425Snectar * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 22142425Snectar * endorse or promote products derived from this software without 23142425Snectar * prior written permission. For written permission, please contact 24142425Snectar * openssl-core@openssl.org. 25142425Snectar * 26142425Snectar * 5. Products derived from this software may not be called "OpenSSL" 27142425Snectar * nor may "OpenSSL" appear in their names without prior written 28142425Snectar * permission of the OpenSSL Project. 29142425Snectar * 30142425Snectar * 6. Redistributions of any form whatsoever must retain the following 31142425Snectar * acknowledgment: 32142425Snectar * "This product includes software developed by the OpenSSL Project 33142425Snectar * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 34142425Snectar * 35142425Snectar * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 36142425Snectar * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37142425Snectar * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38142425Snectar * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 39142425Snectar * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40142425Snectar * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41142425Snectar * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42142425Snectar * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43142425Snectar * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44142425Snectar * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45142425Snectar * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46142425Snectar * OF THE POSSIBILITY OF SUCH DAMAGE. 47142425Snectar * 48142425Snectar */ 49142425Snectar 50142425Snectar#include <string.h> 51142425Snectar 52142425Snectar#include "apps.h" 53142425Snectar#include <openssl/bn.h> 54142425Snectar 55142425Snectar 56142425Snectar#undef PROG 57142425Snectar#define PROG prime_main 58142425Snectar 59160814Ssimonint MAIN(int, char **); 60160814Ssimon 61142425Snectarint MAIN(int argc, char **argv) 62142425Snectar { 63142425Snectar int hex=0; 64142425Snectar int checks=20; 65238405Sjkim int generate=0; 66238405Sjkim int bits=0; 67238405Sjkim int safe=0; 68142425Snectar BIGNUM *bn=NULL; 69160814Ssimon BIO *bio_out; 70142425Snectar 71142425Snectar apps_startup(); 72142425Snectar 73142425Snectar if (bio_err == NULL) 74142425Snectar if ((bio_err=BIO_new(BIO_s_file())) != NULL) 75142425Snectar BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT); 76142425Snectar 77142425Snectar --argc; 78142425Snectar ++argv; 79142425Snectar while (argc >= 1 && **argv == '-') 80142425Snectar { 81142425Snectar if(!strcmp(*argv,"-hex")) 82142425Snectar hex=1; 83238405Sjkim else if(!strcmp(*argv,"-generate")) 84238405Sjkim generate=1; 85238405Sjkim else if(!strcmp(*argv,"-bits")) 86238405Sjkim if(--argc < 1) 87238405Sjkim goto bad; 88238405Sjkim else 89238405Sjkim bits=atoi(*++argv); 90238405Sjkim else if(!strcmp(*argv,"-safe")) 91238405Sjkim safe=1; 92142425Snectar else if(!strcmp(*argv,"-checks")) 93142425Snectar if(--argc < 1) 94142425Snectar goto bad; 95142425Snectar else 96142425Snectar checks=atoi(*++argv); 97142425Snectar else 98142425Snectar { 99142425Snectar BIO_printf(bio_err,"Unknown option '%s'\n",*argv); 100160814Ssimon goto bad; 101142425Snectar } 102142425Snectar --argc; 103142425Snectar ++argv; 104142425Snectar } 105142425Snectar 106238405Sjkim if (argv[0] == NULL && !generate) 107160814Ssimon { 108160814Ssimon BIO_printf(bio_err,"No prime specified\n"); 109160814Ssimon goto bad; 110160814Ssimon } 111160814Ssimon 112238405Sjkim if ((bio_out=BIO_new(BIO_s_file())) != NULL) 113160814Ssimon { 114160814Ssimon BIO_set_fp(bio_out,stdout,BIO_NOCLOSE); 115160814Ssimon#ifdef OPENSSL_SYS_VMS 116160814Ssimon { 117160814Ssimon BIO *tmpbio = BIO_new(BIO_f_linebuffer()); 118160814Ssimon bio_out = BIO_push(tmpbio, bio_out); 119160814Ssimon } 120160814Ssimon#endif 121160814Ssimon } 122160814Ssimon 123238405Sjkim if(generate) 124238405Sjkim { 125238405Sjkim char *s; 126238405Sjkim 127238405Sjkim if(!bits) 128238405Sjkim { 129238405Sjkim BIO_printf(bio_err,"Specifiy the number of bits.\n"); 130238405Sjkim return 1; 131238405Sjkim } 132238405Sjkim bn=BN_new(); 133238405Sjkim BN_generate_prime_ex(bn,bits,safe,NULL,NULL,NULL); 134238405Sjkim s=hex ? BN_bn2hex(bn) : BN_bn2dec(bn); 135238405Sjkim BIO_printf(bio_out,"%s\n",s); 136238405Sjkim OPENSSL_free(s); 137238405Sjkim } 138142425Snectar else 139238405Sjkim { 140238405Sjkim if(hex) 141238405Sjkim BN_hex2bn(&bn,argv[0]); 142238405Sjkim else 143238405Sjkim BN_dec2bn(&bn,argv[0]); 144142425Snectar 145238405Sjkim BN_print(bio_out,bn); 146238405Sjkim BIO_printf(bio_out," is %sprime\n", 147238405Sjkim BN_is_prime_ex(bn,checks,NULL,NULL) ? "" : "not "); 148238405Sjkim } 149142425Snectar 150160814Ssimon BN_free(bn); 151160814Ssimon BIO_free_all(bio_out); 152160814Ssimon 153142425Snectar return 0; 154160814Ssimon 155160814Ssimon bad: 156160814Ssimon BIO_printf(bio_err,"options are\n"); 157160814Ssimon BIO_printf(bio_err,"%-14s hex\n","-hex"); 158160814Ssimon BIO_printf(bio_err,"%-14s number of checks\n","-checks <n>"); 159160814Ssimon return 1; 160142425Snectar } 161