prime.c revision 160814
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; 65142425Snectar BIGNUM *bn=NULL; 66160814Ssimon BIO *bio_out; 67142425Snectar 68142425Snectar apps_startup(); 69142425Snectar 70142425Snectar if (bio_err == NULL) 71142425Snectar if ((bio_err=BIO_new(BIO_s_file())) != NULL) 72142425Snectar BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT); 73142425Snectar 74142425Snectar --argc; 75142425Snectar ++argv; 76142425Snectar while (argc >= 1 && **argv == '-') 77142425Snectar { 78142425Snectar if(!strcmp(*argv,"-hex")) 79142425Snectar hex=1; 80142425Snectar else if(!strcmp(*argv,"-checks")) 81142425Snectar if(--argc < 1) 82142425Snectar goto bad; 83142425Snectar else 84142425Snectar checks=atoi(*++argv); 85142425Snectar else 86142425Snectar { 87142425Snectar BIO_printf(bio_err,"Unknown option '%s'\n",*argv); 88160814Ssimon goto bad; 89142425Snectar } 90142425Snectar --argc; 91142425Snectar ++argv; 92142425Snectar } 93142425Snectar 94160814Ssimon if (argv[0] == NULL) 95160814Ssimon { 96160814Ssimon BIO_printf(bio_err,"No prime specified\n"); 97160814Ssimon goto bad; 98160814Ssimon } 99160814Ssimon 100160814Ssimon if ((bio_out=BIO_new(BIO_s_file())) != NULL) 101160814Ssimon { 102160814Ssimon BIO_set_fp(bio_out,stdout,BIO_NOCLOSE); 103160814Ssimon#ifdef OPENSSL_SYS_VMS 104160814Ssimon { 105160814Ssimon BIO *tmpbio = BIO_new(BIO_f_linebuffer()); 106160814Ssimon bio_out = BIO_push(tmpbio, bio_out); 107160814Ssimon } 108160814Ssimon#endif 109160814Ssimon } 110160814Ssimon 111142425Snectar if(hex) 112142425Snectar BN_hex2bn(&bn,argv[0]); 113142425Snectar else 114142425Snectar BN_dec2bn(&bn,argv[0]); 115142425Snectar 116142425Snectar BN_print(bio_out,bn); 117142425Snectar BIO_printf(bio_out," is %sprime\n", 118160814Ssimon BN_is_prime_ex(bn,checks,NULL,NULL) ? "" : "not "); 119142425Snectar 120160814Ssimon BN_free(bn); 121160814Ssimon BIO_free_all(bio_out); 122160814Ssimon 123142425Snectar return 0; 124160814Ssimon 125160814Ssimon bad: 126160814Ssimon BIO_printf(bio_err,"options are\n"); 127160814Ssimon BIO_printf(bio_err,"%-14s hex\n","-hex"); 128160814Ssimon BIO_printf(bio_err,"%-14s number of checks\n","-checks <n>"); 129160814Ssimon return 1; 130142425Snectar } 131