prime.c revision 256281
143902Sbrian/* ====================================================================
243902Sbrian * Copyright (c) 2004 The OpenSSL Project.  All rights reserved.
343948Sbrian *
443902Sbrian * Redistribution and use in source and binary forms, with or without
543948Sbrian * modification, are permitted provided that the following conditions
643902Sbrian * are met:
745070Sbrian *
843902Sbrian * 1. Redistributions of source code must retain the above copyright
943902Sbrian *    notice, this list of conditions and the following disclaimer.
1043902Sbrian *
1143902Sbrian * 2. Redistributions in binary form must reproduce the above copyright
1243902Sbrian *    notice, this list of conditions and the following disclaimer in
1343902Sbrian *    the documentation and/or other materials provided with the
1443902Sbrian *    distribution.
1543902Sbrian *
1643902Sbrian * 3. All advertising materials mentioning features or use of this
1743902Sbrian *    software must display the following acknowledgment:
1843902Sbrian *    "This product includes software developed by the OpenSSL Project
1943902Sbrian *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
2043902Sbrian *
2143948Sbrian * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
2243902Sbrian *    endorse or promote products derived from this software without
2343902Sbrian *    prior written permission. For written permission, please contact
2443948Sbrian *    openssl-core@openssl.org.
2543948Sbrian *
2643902Sbrian * 5. Products derived from this software may not be called "OpenSSL"
2743902Sbrian *    nor may "OpenSSL" appear in their names without prior written
2843902Sbrian *    permission of the OpenSSL Project.
2943902Sbrian *
3043902Sbrian * 6. Redistributions of any form whatsoever must retain the following
3143948Sbrian *    acknowledgment:
3243948Sbrian *    "This product includes software developed by the OpenSSL Project
3343902Sbrian *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
3443902Sbrian *
3543902Sbrian * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
3643902Sbrian * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3743948Sbrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
3843948Sbrian * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
3943902Sbrian * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4043902Sbrian * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4143902Sbrian * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
4243902Sbrian * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4343948Sbrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
4443948Sbrian * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
4543948Sbrian * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
4643902Sbrian * OF THE POSSIBILITY OF SUCH DAMAGE.
4743902Sbrian *
4843948Sbrian */
4943948Sbrian
5043902Sbrian#include <string.h>
5143902Sbrian
5243902Sbrian#include "apps.h"
5343948Sbrian#include <openssl/bn.h>
5443902Sbrian
5543902Sbrian
5643948Sbrian#undef PROG
5743948Sbrian#define PROG prime_main
5843902Sbrian
5943902Sbrianint MAIN(int, char **);
6043902Sbrian
6143902Sbrianint MAIN(int argc, char **argv)
6243902Sbrian    {
6343902Sbrian    int hex=0;
6443948Sbrian    int checks=20;
6543902Sbrian    int generate=0;
6643948Sbrian    int bits=0;
6743948Sbrian    int safe=0;
6843902Sbrian    BIGNUM *bn=NULL;
6943948Sbrian    BIO *bio_out;
7043948Sbrian
7143948Sbrian    apps_startup();
7243948Sbrian
7343948Sbrian    if (bio_err == NULL)
7443948Sbrian	if ((bio_err=BIO_new(BIO_s_file())) != NULL)
7543902Sbrian	    BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
7643902Sbrian
7743902Sbrian    --argc;
7843902Sbrian    ++argv;
7943902Sbrian    while (argc >= 1 && **argv == '-')
8043902Sbrian	{
8143902Sbrian	if(!strcmp(*argv,"-hex"))
8243902Sbrian	    hex=1;
8343948Sbrian	else if(!strcmp(*argv,"-generate"))
8443948Sbrian	    generate=1;
8543902Sbrian	else if(!strcmp(*argv,"-bits"))
8643902Sbrian	    if(--argc < 1)
8743948Sbrian		goto bad;
8843948Sbrian	    else
8943902Sbrian		bits=atoi(*++argv);
9043902Sbrian	else if(!strcmp(*argv,"-safe"))
9143902Sbrian	    safe=1;
9243902Sbrian	else if(!strcmp(*argv,"-checks"))
9343902Sbrian	    if(--argc < 1)
9443902Sbrian		goto bad;
9543902Sbrian	    else
9643902Sbrian		checks=atoi(*++argv);
9743902Sbrian	else
9843902Sbrian	    {
9943902Sbrian	    BIO_printf(bio_err,"Unknown option '%s'\n",*argv);
10043902Sbrian	    goto bad;
10143902Sbrian	    }
10243902Sbrian	--argc;
10343902Sbrian	++argv;
10443902Sbrian	}
10543902Sbrian
10643902Sbrian    if (argv[0] == NULL && !generate)
10743902Sbrian	{
10843902Sbrian	BIO_printf(bio_err,"No prime specified\n");
10943902Sbrian	goto bad;
11043902Sbrian	}
11143902Sbrian
11243902Sbrian    if ((bio_out=BIO_new(BIO_s_file())) != NULL)
11343902Sbrian	{
11443902Sbrian	BIO_set_fp(bio_out,stdout,BIO_NOCLOSE);
11543902Sbrian#ifdef OPENSSL_SYS_VMS
11643902Sbrian	    {
11743902Sbrian	    BIO *tmpbio = BIO_new(BIO_f_linebuffer());
11843902Sbrian	    bio_out = BIO_push(tmpbio, bio_out);
11943902Sbrian	    }
12043902Sbrian#endif
12143902Sbrian	}
12243902Sbrian
12343902Sbrian    if(generate)
12443902Sbrian	{
12543902Sbrian	char *s;
12643902Sbrian
12743902Sbrian	if(!bits)
12843902Sbrian	    {
12943902Sbrian	    BIO_printf(bio_err,"Specifiy the number of bits.\n");
13043902Sbrian	    return 1;
13143902Sbrian	    }
13243902Sbrian	bn=BN_new();
13343902Sbrian	BN_generate_prime_ex(bn,bits,safe,NULL,NULL,NULL);
13443902Sbrian	s=hex ? BN_bn2hex(bn) : BN_bn2dec(bn);
13543902Sbrian	BIO_printf(bio_out,"%s\n",s);
13643902Sbrian	OPENSSL_free(s);
13743902Sbrian	}
13843902Sbrian    else
13943902Sbrian	{
14043902Sbrian	if(hex)
14143902Sbrian	    BN_hex2bn(&bn,argv[0]);
14243902Sbrian	else
14343902Sbrian	    BN_dec2bn(&bn,argv[0]);
14443902Sbrian
14543902Sbrian	BN_print(bio_out,bn);
14643902Sbrian	BIO_printf(bio_out," is %sprime\n",
14743902Sbrian		   BN_is_prime_ex(bn,checks,NULL,NULL) ? "" : "not ");
14843902Sbrian	}
14943902Sbrian
15043902Sbrian    BN_free(bn);
15143902Sbrian    BIO_free_all(bio_out);
15243902Sbrian
15343902Sbrian    return 0;
15443902Sbrian
15543902Sbrian    bad:
15643902Sbrian    BIO_printf(bio_err,"options are\n");
15743902Sbrian    BIO_printf(bio_err,"%-14s hex\n","-hex");
15843902Sbrian    BIO_printf(bio_err,"%-14s number of checks\n","-checks <n>");
15943902Sbrian    return 1;
16043902Sbrian    }
16143902Sbrian