10Sstevel@tonic-gate /* crypto/dh/dh_gen.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate * All rights reserved.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * This package is an SSL implementation written
60Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate * the following conditions are aheared to. The following conditions
110Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation
130Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate *
160Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate * the code are not to be removed.
180Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate * as the author of the parts of the library used.
200Sstevel@tonic-gate * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate *
230Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate * are met:
260Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate * must display the following acknowledgement:
330Sstevel@tonic-gate * "This product includes cryptographic software written by
340Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate * being used are not cryptographic related :-).
370Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate *
410Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate * SUCH DAMAGE.
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be
550Sstevel@tonic-gate * copied and put under another distribution licence
560Sstevel@tonic-gate * [including the GNU Public Licence.]
570Sstevel@tonic-gate */
580Sstevel@tonic-gate
59*2139Sjp161948 /* NB: These functions have been upgraded - the previous prototypes are in
60*2139Sjp161948 * dh_depr.c as wrappers to these ones.
61*2139Sjp161948 * - Geoff
62*2139Sjp161948 */
63*2139Sjp161948
640Sstevel@tonic-gate #include <stdio.h>
650Sstevel@tonic-gate #include "cryptlib.h"
660Sstevel@tonic-gate #include <openssl/bn.h>
670Sstevel@tonic-gate #include <openssl/dh.h>
680Sstevel@tonic-gate
69*2139Sjp161948 static int dh_builtin_genparams(DH *ret, int prime_len, int generator, BN_GENCB *cb);
70*2139Sjp161948
DH_generate_parameters_ex(DH * ret,int prime_len,int generator,BN_GENCB * cb)71*2139Sjp161948 int DH_generate_parameters_ex(DH *ret, int prime_len, int generator, BN_GENCB *cb)
72*2139Sjp161948 {
73*2139Sjp161948 if(ret->meth->generate_params)
74*2139Sjp161948 return ret->meth->generate_params(ret, prime_len, generator, cb);
75*2139Sjp161948 return dh_builtin_genparams(ret, prime_len, generator, cb);
76*2139Sjp161948 }
77*2139Sjp161948
780Sstevel@tonic-gate /* We generate DH parameters as follows
790Sstevel@tonic-gate * find a prime q which is prime_len/2 bits long.
800Sstevel@tonic-gate * p=(2*q)+1 or (p-1)/2 = q
810Sstevel@tonic-gate * For this case, g is a generator if
820Sstevel@tonic-gate * g^((p-1)/q) mod p != 1 for values of q which are the factors of p-1.
830Sstevel@tonic-gate * Since the factors of p-1 are q and 2, we just need to check
840Sstevel@tonic-gate * g^2 mod p != 1 and g^q mod p != 1.
850Sstevel@tonic-gate *
860Sstevel@tonic-gate * Having said all that,
870Sstevel@tonic-gate * there is another special case method for the generators 2, 3 and 5.
880Sstevel@tonic-gate * for 2, p mod 24 == 11
890Sstevel@tonic-gate * for 3, p mod 12 == 5 <<<<< does not work for safe primes.
900Sstevel@tonic-gate * for 5, p mod 10 == 3 or 7
910Sstevel@tonic-gate *
920Sstevel@tonic-gate * Thanks to Phil Karn <karn@qualcomm.com> for the pointers about the
930Sstevel@tonic-gate * special generators and for answering some of my questions.
940Sstevel@tonic-gate *
950Sstevel@tonic-gate * I've implemented the second simple method :-).
960Sstevel@tonic-gate * Since DH should be using a safe prime (both p and q are prime),
970Sstevel@tonic-gate * this generator function can take a very very long time to run.
980Sstevel@tonic-gate */
990Sstevel@tonic-gate /* Actually there is no reason to insist that 'generator' be a generator.
1000Sstevel@tonic-gate * It's just as OK (and in some sense better) to use a generator of the
1010Sstevel@tonic-gate * order-q subgroup.
1020Sstevel@tonic-gate */
dh_builtin_genparams(DH * ret,int prime_len,int generator,BN_GENCB * cb)103*2139Sjp161948 static int dh_builtin_genparams(DH *ret, int prime_len, int generator, BN_GENCB *cb)
1040Sstevel@tonic-gate {
105*2139Sjp161948 BIGNUM *t1,*t2;
1060Sstevel@tonic-gate int g,ok= -1;
1070Sstevel@tonic-gate BN_CTX *ctx=NULL;
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate ctx=BN_CTX_new();
1100Sstevel@tonic-gate if (ctx == NULL) goto err;
1110Sstevel@tonic-gate BN_CTX_start(ctx);
1120Sstevel@tonic-gate t1 = BN_CTX_get(ctx);
1130Sstevel@tonic-gate t2 = BN_CTX_get(ctx);
1140Sstevel@tonic-gate if (t1 == NULL || t2 == NULL) goto err;
115*2139Sjp161948
116*2139Sjp161948 /* Make sure 'ret' has the necessary elements */
117*2139Sjp161948 if(!ret->p && ((ret->p = BN_new()) == NULL)) goto err;
118*2139Sjp161948 if(!ret->g && ((ret->g = BN_new()) == NULL)) goto err;
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate if (generator <= 1)
1210Sstevel@tonic-gate {
122*2139Sjp161948 DHerr(DH_F_DH_BUILTIN_GENPARAMS, DH_R_BAD_GENERATOR);
1230Sstevel@tonic-gate goto err;
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate if (generator == DH_GENERATOR_2)
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate if (!BN_set_word(t1,24)) goto err;
1280Sstevel@tonic-gate if (!BN_set_word(t2,11)) goto err;
1290Sstevel@tonic-gate g=2;
1300Sstevel@tonic-gate }
1310Sstevel@tonic-gate #if 0 /* does not work for safe primes */
1320Sstevel@tonic-gate else if (generator == DH_GENERATOR_3)
1330Sstevel@tonic-gate {
1340Sstevel@tonic-gate if (!BN_set_word(t1,12)) goto err;
1350Sstevel@tonic-gate if (!BN_set_word(t2,5)) goto err;
1360Sstevel@tonic-gate g=3;
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate #endif
1390Sstevel@tonic-gate else if (generator == DH_GENERATOR_5)
1400Sstevel@tonic-gate {
1410Sstevel@tonic-gate if (!BN_set_word(t1,10)) goto err;
1420Sstevel@tonic-gate if (!BN_set_word(t2,3)) goto err;
1430Sstevel@tonic-gate /* BN_set_word(t3,7); just have to miss
1440Sstevel@tonic-gate * out on these ones :-( */
1450Sstevel@tonic-gate g=5;
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate else
1480Sstevel@tonic-gate {
1490Sstevel@tonic-gate /* in the general case, don't worry if 'generator' is a
1500Sstevel@tonic-gate * generator or not: since we are using safe primes,
1510Sstevel@tonic-gate * it will generate either an order-q or an order-2q group,
1520Sstevel@tonic-gate * which both is OK */
1530Sstevel@tonic-gate if (!BN_set_word(t1,2)) goto err;
1540Sstevel@tonic-gate if (!BN_set_word(t2,1)) goto err;
1550Sstevel@tonic-gate g=generator;
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate
158*2139Sjp161948 if(!BN_generate_prime_ex(ret->p,prime_len,1,t1,t2,cb)) goto err;
159*2139Sjp161948 if(!BN_GENCB_call(cb, 3, 0)) goto err;
1600Sstevel@tonic-gate if (!BN_set_word(ret->g,g)) goto err;
1610Sstevel@tonic-gate ok=1;
1620Sstevel@tonic-gate err:
1630Sstevel@tonic-gate if (ok == -1)
1640Sstevel@tonic-gate {
165*2139Sjp161948 DHerr(DH_F_DH_BUILTIN_GENPARAMS,ERR_R_BN_LIB);
1660Sstevel@tonic-gate ok=0;
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate if (ctx != NULL)
1700Sstevel@tonic-gate {
1710Sstevel@tonic-gate BN_CTX_end(ctx);
1720Sstevel@tonic-gate BN_CTX_free(ctx);
1730Sstevel@tonic-gate }
174*2139Sjp161948 return ok;
1750Sstevel@tonic-gate }
176