prime.c revision 225736
121259Swollman/* ====================================================================
221259Swollman * Copyright (c) 2004 The OpenSSL Project.  All rights reserved.
321259Swollman *
421259Swollman * Redistribution and use in source and binary forms, with or without
521259Swollman * modification, are permitted provided that the following conditions
621259Swollman * are met:
721259Swollman *
821259Swollman * 1. Redistributions of source code must retain the above copyright
921259Swollman *    notice, this list of conditions and the following disclaimer.
1021259Swollman *
1121259Swollman * 2. Redistributions in binary form must reproduce the above copyright
1221259Swollman *    notice, this list of conditions and the following disclaimer in
1321259Swollman *    the documentation and/or other materials provided with the
1421259Swollman *    distribution.
1521259Swollman *
1621259Swollman * 3. All advertising materials mentioning features or use of this
1721259Swollman *    software must display the following acknowledgment:
1821259Swollman *    "This product includes software developed by the OpenSSL Project
1921259Swollman *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
2021259Swollman *
2121259Swollman * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
2221259Swollman *    endorse or promote products derived from this software without
2321259Swollman *    prior written permission. For written permission, please contact
2421259Swollman *    openssl-core@openssl.org.
2521259Swollman *
2621259Swollman * 5. Products derived from this software may not be called "OpenSSL"
2721259Swollman *    nor may "OpenSSL" appear in their names without prior written
2821259Swollman *    permission of the OpenSSL Project.
2921259Swollman *
3021259Swollman * 6. Redistributions of any form whatsoever must retain the following
3121259Swollman *    acknowledgment:
3221259Swollman *    "This product includes software developed by the OpenSSL Project
3321259Swollman *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
3450477Speter *
3521259Swollman * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
3621259Swollman * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3721259Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
3821259Swollman * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
3921259Swollman * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4021259Swollman * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4121259Swollman * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
4221259Swollman * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4321259Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
4421259Swollman * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
4521259Swollman * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
4621259Swollman * OF THE POSSIBILITY OF SUCH DAMAGE.
4721259Swollman *
4821259Swollman */
4921259Swollman
5021259Swollman#include <string.h>
5121259Swollman
5221259Swollman#include "apps.h"
5321259Swollman#include <openssl/bn.h>
5421259Swollman
5521259Swollman
5621259Swollman#undef PROG
5721259Swollman#define PROG prime_main
5821259Swollman
5921259Swollmanint MAIN(int, char **);
6021259Swollman
6121259Swollmanint MAIN(int argc, char **argv)
6221259Swollman    {
6321259Swollman    int hex=0;
6421259Swollman    int checks=20;
6521259Swollman    BIGNUM *bn=NULL;
6621259Swollman    BIO *bio_out;
6721259Swollman
6821259Swollman    apps_startup();
6921259Swollman
7021259Swollman    if (bio_err == NULL)
7121259Swollman	if ((bio_err=BIO_new(BIO_s_file())) != NULL)
7221259Swollman	    BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
7321259Swollman
7421259Swollman    --argc;
7521259Swollman    ++argv;
7621259Swollman    while (argc >= 1 && **argv == '-')
7721259Swollman	{
7860938Sjake	if(!strcmp(*argv,"-hex"))
7960938Sjake	    hex=1;
8060938Sjake	else if(!strcmp(*argv,"-checks"))
8160938Sjake	    if(--argc < 1)
8221259Swollman		goto bad;
8321259Swollman	    else
8421259Swollman		checks=atoi(*++argv);
8521259Swollman	else
8621259Swollman	    {
8721259Swollman	    BIO_printf(bio_err,"Unknown option '%s'\n",*argv);
8821259Swollman	    goto bad;
8921259Swollman	    }
9021259Swollman	--argc;
9121259Swollman	++argv;
9221259Swollman	}
9321259Swollman
9421259Swollman    if (argv[0] == NULL)
9521259Swollman	{
9621259Swollman	BIO_printf(bio_err,"No prime specified\n");
9721259Swollman	goto bad;
9821259Swollman	}
9921259Swollman
10021259Swollman   if ((bio_out=BIO_new(BIO_s_file())) != NULL)
10121259Swollman	{
10260938Sjake	BIO_set_fp(bio_out,stdout,BIO_NOCLOSE);
10321259Swollman#ifdef OPENSSL_SYS_VMS
10421259Swollman	    {
10521259Swollman	    BIO *tmpbio = BIO_new(BIO_f_linebuffer());
10621259Swollman	    bio_out = BIO_push(tmpbio, bio_out);
10721259Swollman	    }
10821259Swollman#endif
10921259Swollman	}
11021259Swollman
11121259Swollman    if(hex)
11221259Swollman	BN_hex2bn(&bn,argv[0]);
11321259Swollman    else
11421404Swollman	BN_dec2bn(&bn,argv[0]);
11521404Swollman
11621259Swollman    BN_print(bio_out,bn);
11721259Swollman    BIO_printf(bio_out," is %sprime\n",
11821259Swollman	       BN_is_prime_ex(bn,checks,NULL,NULL) ? "" : "not ");
11921259Swollman
12021259Swollman    BN_free(bn);
12121259Swollman    BIO_free_all(bio_out);
12221259Swollman
12321259Swollman    return 0;
12421259Swollman
12536735Sdfr    bad:
12621259Swollman    BIO_printf(bio_err,"options are\n");
12721259Swollman    BIO_printf(bio_err,"%-14s hex\n","-hex");
12821259Swollman    BIO_printf(bio_err,"%-14s number of checks\n","-checks <n>");
12921259Swollman    return 1;
13021259Swollman    }
13121259Swollman