prime.c revision 142425
1142425Snectar/* ====================================================================
2160814Ssimon * 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.
25238405Sjkim *
26142425Snectar * 5. Products derived from this software may not be called "OpenSSL"
27142425Snectar *    nor may "OpenSSL" appear in their names without prior written
28238405Sjkim *    permission of the OpenSSL Project.
29142425Snectar *
30238405Sjkim * 6. Redistributions of any form whatsoever must retain the following
31238405Sjkim *    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
38238405Sjkim * 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
41238405Sjkim * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42142425Snectar * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43238405Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44238405Sjkim * 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
50238405Sjkim#include <string.h>
51142425Snectar
52142425Snectar#include "apps.h"
53142425Snectar#include <openssl/bn.h>
54142425Snectar
55142425Snectar
56142425Snectar#undef PROG
57142425Snectar#define PROG prime_main
58142425Snectar
59142425Snectarint MAIN(int argc, char **argv)
60142425Snectar    {
61142425Snectar    int hex=0;
62142425Snectar    int checks=20;
63142425Snectar    BIGNUM *bn=NULL;
64142425Snectar    BIO *bio_out=NULL;
65142425Snectar
66142425Snectar    apps_startup();
67142425Snectar
68238405Sjkim    if (bio_err == NULL)
69142425Snectar	if ((bio_err=BIO_new(BIO_s_file())) != NULL)
70142425Snectar	    BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
71142425Snectar
72142425Snectar    if (bio_out == NULL)
73142425Snectar	if ((bio_out=BIO_new(BIO_s_file())) != NULL)
74142425Snectar	    {
75142425Snectar	    BIO_set_fp(bio_out,stdout,BIO_NOCLOSE);
76142425Snectar#ifdef OPENSSL_SYS_VMS
77142425Snectar	    {
78142425Snectar	    BIO *tmpbio = BIO_new(BIO_f_linebuffer());
79142425Snectar	    bio_out = BIO_push(tmpbio, bio_out);
80142425Snectar	    }
81160814Ssimon#endif
82160814Ssimon	    }
83142425Snectar
84142425Snectar    --argc;
85142425Snectar    ++argv;
86142425Snectar    while (argc >= 1 && **argv == '-')
87142425Snectar	{
88142425Snectar	if(!strcmp(*argv,"-hex"))
89142425Snectar	    hex=1;
90142425Snectar	else if(!strcmp(*argv,"-checks"))
91142425Snectar	    if(--argc < 1)
92142425Snectar		goto bad;
93142425Snectar	    else
94142425Snectar		checks=atoi(*++argv);
95142425Snectar	else
96284285Sjkim	    {
97284285Sjkim	    BIO_printf(bio_err,"Unknown option '%s'\n",*argv);
98142425Snectar	bad:
99160814Ssimon	    BIO_printf(bio_err,"options are\n");
100142425Snectar	    BIO_printf(bio_err,"%-14s hex\n","-hex");
101142425Snectar	    BIO_printf(bio_err,"%-14s number of checks\n","-checks <n>");
102142425Snectar	    exit(1);
103142425Snectar	    }
104142425Snectar	--argc;
105142425Snectar	++argv;
106142425Snectar	}
107142425Snectar
108142425Snectar    if(hex)
109142425Snectar	BN_hex2bn(&bn,argv[0]);
110142425Snectar    else
111142425Snectar	BN_dec2bn(&bn,argv[0]);
112142425Snectar
113160814Ssimon    BN_print(bio_out,bn);
114160814Ssimon    BIO_printf(bio_out," is %sprime\n",
115160814Ssimon	       BN_is_prime(bn,checks,NULL,NULL,NULL) ? "" : "not ");
116160814Ssimon
117142425Snectar    return 0;
118142425Snectar    }
119142425Snectar