prime.c revision 280304
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 9280304Sjkim * 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#undef PROG 56142425Snectar#define PROG prime_main 57142425Snectar 58160814Ssimonint MAIN(int, char **); 59160814Ssimon 60142425Snectarint MAIN(int argc, char **argv) 61280304Sjkim{ 62280304Sjkim int hex = 0; 63280304Sjkim int checks = 20; 64280304Sjkim int generate = 0; 65280304Sjkim int bits = 0; 66280304Sjkim int safe = 0; 67280304Sjkim BIGNUM *bn = NULL; 68160814Ssimon BIO *bio_out; 69142425Snectar 70142425Snectar apps_startup(); 71142425Snectar 72142425Snectar if (bio_err == NULL) 73280304Sjkim if ((bio_err = BIO_new(BIO_s_file())) != NULL) 74280304Sjkim BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT); 75142425Snectar 76142425Snectar --argc; 77142425Snectar ++argv; 78280304Sjkim while (argc >= 1 && **argv == '-') { 79280304Sjkim if (!strcmp(*argv, "-hex")) 80280304Sjkim hex = 1; 81280304Sjkim else if (!strcmp(*argv, "-generate")) 82280304Sjkim generate = 1; 83280304Sjkim else if (!strcmp(*argv, "-bits")) 84280304Sjkim if (--argc < 1) 85280304Sjkim goto bad; 86280304Sjkim else 87280304Sjkim bits = atoi(*++argv); 88280304Sjkim else if (!strcmp(*argv, "-safe")) 89280304Sjkim safe = 1; 90280304Sjkim else if (!strcmp(*argv, "-checks")) 91280304Sjkim if (--argc < 1) 92280304Sjkim goto bad; 93280304Sjkim else 94280304Sjkim checks = atoi(*++argv); 95280304Sjkim else { 96280304Sjkim BIO_printf(bio_err, "Unknown option '%s'\n", *argv); 97280304Sjkim goto bad; 98280304Sjkim } 99280304Sjkim --argc; 100280304Sjkim ++argv; 101280304Sjkim } 102142425Snectar 103280304Sjkim if (argv[0] == NULL && !generate) { 104280304Sjkim BIO_printf(bio_err, "No prime specified\n"); 105280304Sjkim goto bad; 106280304Sjkim } 107160814Ssimon 108280304Sjkim if ((bio_out = BIO_new(BIO_s_file())) != NULL) { 109280304Sjkim BIO_set_fp(bio_out, stdout, BIO_NOCLOSE); 110160814Ssimon#ifdef OPENSSL_SYS_VMS 111280304Sjkim { 112280304Sjkim BIO *tmpbio = BIO_new(BIO_f_linebuffer()); 113280304Sjkim bio_out = BIO_push(tmpbio, bio_out); 114280304Sjkim } 115160814Ssimon#endif 116280304Sjkim } 117160814Ssimon 118280304Sjkim if (generate) { 119280304Sjkim char *s; 120238405Sjkim 121280304Sjkim if (!bits) { 122280304Sjkim BIO_printf(bio_err, "Specifiy the number of bits.\n"); 123280304Sjkim return 1; 124280304Sjkim } 125280304Sjkim bn = BN_new(); 126280304Sjkim BN_generate_prime_ex(bn, bits, safe, NULL, NULL, NULL); 127280304Sjkim s = hex ? BN_bn2hex(bn) : BN_bn2dec(bn); 128280304Sjkim BIO_printf(bio_out, "%s\n", s); 129280304Sjkim OPENSSL_free(s); 130280304Sjkim } else { 131280304Sjkim if (hex) 132280304Sjkim BN_hex2bn(&bn, argv[0]); 133280304Sjkim else 134280304Sjkim BN_dec2bn(&bn, argv[0]); 135142425Snectar 136280304Sjkim BN_print(bio_out, bn); 137280304Sjkim BIO_printf(bio_out, " is %sprime\n", 138280304Sjkim BN_is_prime_ex(bn, checks, NULL, NULL) ? "" : "not "); 139280304Sjkim } 140142425Snectar 141160814Ssimon BN_free(bn); 142160814Ssimon BIO_free_all(bio_out); 143160814Ssimon 144142425Snectar return 0; 145160814Ssimon 146280304Sjkim bad: 147280304Sjkim BIO_printf(bio_err, "options are\n"); 148280304Sjkim BIO_printf(bio_err, "%-14s hex\n", "-hex"); 149280304Sjkim BIO_printf(bio_err, "%-14s number of checks\n", "-checks <n>"); 150160814Ssimon return 1; 151280304Sjkim} 152