dhparam.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
0dc2366f7b9f9f36e10909b1e95edbf2a261c2acVenugopal Iyer/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * All rights reserved.
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * This package is an SSL implementation written
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * by Eric Young (eay@cryptsoft.com).
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * The implementation was written so as to conform with Netscapes SSL.
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * This library is free for commercial and non-commercial use as long as
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * the following conditions are aheared to. The following conditions
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * apply to all code found in this distribution, be it the RC4, RSA,
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * lhash, DES, etc., code; not just the SSL code. The SSL documentation
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * included with this distribution is covered by the same copyright terms
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * except that the holder is Tim Hudson (tjh@cryptsoft.com).
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * Copyright remains Eric Young's, and as such any Copyright notices in
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * the code are not to be removed.
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * If this package is used in a product, Eric Young should be given attribution
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * as the author of the parts of the library used.
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * This can be in the form of a textual message at program startup or
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * in documentation (online or textual) provided with the package.
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * Redistribution and use in source and binary forms, with or without
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * modification, are permitted provided that the following conditions
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * 1. Redistributions of source code must retain the copyright
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * notice, this list of conditions and the following disclaimer.
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * 2. Redistributions in binary form must reproduce the above copyright
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * notice, this list of conditions and the following disclaimer in the
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * documentation and/or other materials provided with the distribution.
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * 3. All advertising materials mentioning features or use of this software
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * must display the following acknowledgement:
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * "This product includes cryptographic software written by
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * Eric Young (eay@cryptsoft.com)"
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * The word 'cryptographic' can be left out if the rouines from the library
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * being used are not cryptographic related :-).
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * 4. If you include any Windows specific code (or a derivative thereof) from
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * the apps directory (application code) you must include an acknowledgement:
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * SUCH DAMAGE.
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * The licence and distribution terms for any publically available version or
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * derivative of this code cannot be changed. i.e. this code cannot simply be
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * copied and put under another distribution licence
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * [including the GNU Public Licence.]
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang/* ====================================================================
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * Redistribution and use in source and binary forms, with or without
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * modification, are permitted provided that the following conditions
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * 1. Redistributions of source code must retain the above copyright
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * notice, this list of conditions and the following disclaimer.
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * 2. Redistributions in binary form must reproduce the above copyright
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * notice, this list of conditions and the following disclaimer in
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * the documentation and/or other materials provided with the
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * distribution.
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * 3. All advertising materials mentioning features or use of this
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * software must display the following acknowledgment:
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * "This product includes software developed by the OpenSSL Project
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * endorse or promote products derived from this software without
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * prior written permission. For written permission, please contact
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * openssl-core@openssl.org.
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * 5. Products derived from this software may not be called "OpenSSL"
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * nor may "OpenSSL" appear in their names without prior written
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * permission of the OpenSSL Project.
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * 6. Redistributions of any form whatsoever must retain the following
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * acknowledgment:
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * "This product includes software developed by the OpenSSL Project
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * OF THE POSSIBILITY OF SUCH DAMAGE.
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * ====================================================================
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * This product includes cryptographic software written by Eric Young
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * (eay@cryptsoft.com). This product includes software written by Tim
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * Hudson (tjh@cryptsoft.com).
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang/* -inform arg - input format - default PEM (DER or PEM)
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * -outform arg - output format - default PEM
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * -in arg - input file - default stdin
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * -out arg - output file - default stdout
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * -dsaparam - read or generate DSA parameters, convert to DH
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang * -check - check the parameters are ok
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fangstatic void MS_CALLBACK dh_cb(int p, int n, void *arg);
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fangint MAIN(int, char **);
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang int informat,outformat,check=0,noout=0,C=0,ret=1;
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang int num = 0, g = 0;
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang else if (((sscanf(*argv,"%d",&num) == 0) || (num <= 0)))
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang BIO_printf(bio_err,"%s [options] [numbits]\n",prog);
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang BIO_printf(bio_err," -inform arg input format - one of DER PEM\n");
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang BIO_printf(bio_err," -outform arg output format - one of DER PEM\n");
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang BIO_printf(bio_err," -dsaparam read or generate DSA parameters, convert to DH\n");
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang BIO_printf(bio_err," -check check the DH parameters\n");
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang BIO_printf(bio_err," -text print a text form of the DH parameters\n");
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang BIO_printf(bio_err," -2 generate parameters using 2 as the generator value\n");
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang BIO_printf(bio_err," -5 generate parameters using 5 as the generator value\n");
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang BIO_printf(bio_err," numbits number of bits in to generate (default 512)\n");
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang BIO_printf(bio_err," -engine e use engine e, possibly a hardware device.\n");
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang BIO_printf(bio_err," - load the file (or the files in the directory) into\n");
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang BIO_printf(bio_err," the random number generator\n");
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang BIO_printf(bio_err, "generator may not be chosen for DSA parameters\n");
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang /* DH parameters */
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL)
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n");
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang BIO_printf(bio_err,"Generating DSA parameters, %d bit long prime\n",num);
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang dsa = DSA_generate_parameters(num, NULL, 0, NULL, NULL, dh_cb, bio_err);
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang BIO_printf(bio_err,"Generating DH parameters, %d bit long safe prime, generator %d\n",num,g);
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang BIO_printf(bio_err,"This is going to take a long time\n");
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang if (informat != FORMAT_ASN1 && informat != FORMAT_PEM)
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang BIO_printf(bio_err,"bad input format specified\n");
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang else /* informat == FORMAT_PEM */
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang BIO_printf(bio_err,"unable to load DSA parameters\n");
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang else /* informat == FORMAT_PEM */
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang BIO_printf(bio_err,"unable to load DH parameters\n");
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang /* dh != NULL */
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang printf("unable to check the generator value\n");
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang unsigned char *data;
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang "#include <openssl/dh.h>\n"
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang "#endif\n");
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang printf("\tstatic unsigned char dh%d_p[]={",bits);
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang for (i=0; i<l; i++)
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang printf("\tstatic unsigned char dh%d_g[]={",bits);
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang for (i=0; i<l; i++)
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang printf("\tif ((dh=DH_new()) == NULL) return(NULL);\n");
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang printf("\tdh->p=BN_bin2bn(dh%d_p,sizeof(dh%d_p),NULL);\n",
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang printf("\tdh->g=BN_bin2bn(dh%d_g,sizeof(dh%d_g),NULL);\n",
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang printf("\tif ((dh->p == NULL) || (dh->g == NULL))\n");
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang BIO_printf(bio_err,"bad output format specified for outfile\n");
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang BIO_printf(bio_err,"unable to write DH parameters\n");
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang/* dh_cb is identical to dsa_cb in apps/dsaparam.c */
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fangstatic void MS_CALLBACK dh_cb(int p, int n, void *arg)
22a84b8d79248a611e4ba663a268d3c4bed054acQuaker Fang if (p == 0) c='.';