xref: /onnv-gate/usr/src/common/openssl/crypto/bn/bn_prime.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/bn/bn_prime.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 /* ====================================================================
590Sstevel@tonic-gate  * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
600Sstevel@tonic-gate  *
610Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
620Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
630Sstevel@tonic-gate  * are met:
640Sstevel@tonic-gate  *
650Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
660Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
670Sstevel@tonic-gate  *
680Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
690Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
700Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
710Sstevel@tonic-gate  *    distribution.
720Sstevel@tonic-gate  *
730Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
740Sstevel@tonic-gate  *    software must display the following acknowledgment:
750Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
760Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
770Sstevel@tonic-gate  *
780Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
790Sstevel@tonic-gate  *    endorse or promote products derived from this software without
800Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
810Sstevel@tonic-gate  *    openssl-core@openssl.org.
820Sstevel@tonic-gate  *
830Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
840Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
850Sstevel@tonic-gate  *    permission of the OpenSSL Project.
860Sstevel@tonic-gate  *
870Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
880Sstevel@tonic-gate  *    acknowledgment:
890Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
900Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
910Sstevel@tonic-gate  *
920Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
930Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
940Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
950Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
960Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
970Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
980Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
990Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1000Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1010Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1020Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1030Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
1040Sstevel@tonic-gate  * ====================================================================
1050Sstevel@tonic-gate  *
1060Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
1070Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
1080Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
1090Sstevel@tonic-gate  *
1100Sstevel@tonic-gate  */
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate #include <stdio.h>
1130Sstevel@tonic-gate #include <time.h>
1140Sstevel@tonic-gate #include "cryptlib.h"
1150Sstevel@tonic-gate #include "bn_lcl.h"
1160Sstevel@tonic-gate #include <openssl/rand.h>
1170Sstevel@tonic-gate 
118*2139Sjp161948 /* NB: these functions have been "upgraded", the deprecated versions (which are
119*2139Sjp161948  * compatibility wrappers using these functions) are in bn_depr.c.
120*2139Sjp161948  * - Geoff
121*2139Sjp161948  */
122*2139Sjp161948 
1230Sstevel@tonic-gate /* The quick sieve algorithm approach to weeding out primes is
1240Sstevel@tonic-gate  * Philip Zimmermann's, as implemented in PGP.  I have had a read of
1250Sstevel@tonic-gate  * his comments and implemented my own version.
1260Sstevel@tonic-gate  */
1270Sstevel@tonic-gate #include "bn_prime.h"
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate static int witness(BIGNUM *w, const BIGNUM *a, const BIGNUM *a1,
1300Sstevel@tonic-gate 	const BIGNUM *a1_odd, int k, BN_CTX *ctx, BN_MONT_CTX *mont);
1310Sstevel@tonic-gate static int probable_prime(BIGNUM *rnd, int bits);
1320Sstevel@tonic-gate static int probable_prime_dh(BIGNUM *rnd, int bits,
1330Sstevel@tonic-gate 	const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx);
1340Sstevel@tonic-gate static int probable_prime_dh_safe(BIGNUM *rnd, int bits,
1350Sstevel@tonic-gate 	const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx);
1360Sstevel@tonic-gate 
BN_GENCB_call(BN_GENCB * cb,int a,int b)137*2139Sjp161948 int BN_GENCB_call(BN_GENCB *cb, int a, int b)
1380Sstevel@tonic-gate 	{
139*2139Sjp161948 	/* No callback means continue */
140*2139Sjp161948 	if(!cb) return 1;
141*2139Sjp161948 	switch(cb->ver)
142*2139Sjp161948 		{
143*2139Sjp161948 	case 1:
144*2139Sjp161948 		/* Deprecated-style callbacks */
145*2139Sjp161948 		if(!cb->cb.cb_1)
146*2139Sjp161948 			return 1;
147*2139Sjp161948 		cb->cb.cb_1(a, b, cb->arg);
148*2139Sjp161948 		return 1;
149*2139Sjp161948 	case 2:
150*2139Sjp161948 		/* New-style callbacks */
151*2139Sjp161948 		return cb->cb.cb_2(a, b, cb);
152*2139Sjp161948 	default:
153*2139Sjp161948 		break;
154*2139Sjp161948 		}
155*2139Sjp161948 	/* Unrecognised callback type */
156*2139Sjp161948 	return 0;
157*2139Sjp161948 	}
158*2139Sjp161948 
BN_generate_prime_ex(BIGNUM * ret,int bits,int safe,const BIGNUM * add,const BIGNUM * rem,BN_GENCB * cb)159*2139Sjp161948 int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe,
160*2139Sjp161948 	const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb)
161*2139Sjp161948 	{
162*2139Sjp161948 	BIGNUM *t;
1630Sstevel@tonic-gate 	int found=0;
1640Sstevel@tonic-gate 	int i,j,c1=0;
1650Sstevel@tonic-gate 	BN_CTX *ctx;
1660Sstevel@tonic-gate 	int checks = BN_prime_checks_for_size(bits);
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	ctx=BN_CTX_new();
1690Sstevel@tonic-gate 	if (ctx == NULL) goto err;
170*2139Sjp161948 	BN_CTX_start(ctx);
171*2139Sjp161948 	t = BN_CTX_get(ctx);
172*2139Sjp161948 	if(!t) goto err;
1730Sstevel@tonic-gate loop:
1740Sstevel@tonic-gate 	/* make a random number and set the top and bottom bits */
1750Sstevel@tonic-gate 	if (add == NULL)
1760Sstevel@tonic-gate 		{
177*2139Sjp161948 		if (!probable_prime(ret,bits)) goto err;
1780Sstevel@tonic-gate 		}
1790Sstevel@tonic-gate 	else
1800Sstevel@tonic-gate 		{
1810Sstevel@tonic-gate 		if (safe)
1820Sstevel@tonic-gate 			{
183*2139Sjp161948 			if (!probable_prime_dh_safe(ret,bits,add,rem,ctx))
1840Sstevel@tonic-gate 				 goto err;
1850Sstevel@tonic-gate 			}
1860Sstevel@tonic-gate 		else
1870Sstevel@tonic-gate 			{
188*2139Sjp161948 			if (!probable_prime_dh(ret,bits,add,rem,ctx))
1890Sstevel@tonic-gate 				goto err;
1900Sstevel@tonic-gate 			}
1910Sstevel@tonic-gate 		}
192*2139Sjp161948 	/* if (BN_mod_word(ret,(BN_ULONG)3) == 1) goto loop; */
193*2139Sjp161948 	if(!BN_GENCB_call(cb, 0, c1++))
194*2139Sjp161948 		/* aborted */
195*2139Sjp161948 		goto err;
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	if (!safe)
1980Sstevel@tonic-gate 		{
199*2139Sjp161948 		i=BN_is_prime_fasttest_ex(ret,checks,ctx,0,cb);
2000Sstevel@tonic-gate 		if (i == -1) goto err;
2010Sstevel@tonic-gate 		if (i == 0) goto loop;
2020Sstevel@tonic-gate 		}
2030Sstevel@tonic-gate 	else
2040Sstevel@tonic-gate 		{
2050Sstevel@tonic-gate 		/* for "safe prime" generation,
2060Sstevel@tonic-gate 		 * check that (p-1)/2 is prime.
2070Sstevel@tonic-gate 		 * Since a prime is odd, We just
2080Sstevel@tonic-gate 		 * need to divide by 2 */
209*2139Sjp161948 		if (!BN_rshift1(t,ret)) goto err;
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 		for (i=0; i<checks; i++)
2120Sstevel@tonic-gate 			{
213*2139Sjp161948 			j=BN_is_prime_fasttest_ex(ret,1,ctx,0,cb);
2140Sstevel@tonic-gate 			if (j == -1) goto err;
2150Sstevel@tonic-gate 			if (j == 0) goto loop;
2160Sstevel@tonic-gate 
217*2139Sjp161948 			j=BN_is_prime_fasttest_ex(t,1,ctx,0,cb);
2180Sstevel@tonic-gate 			if (j == -1) goto err;
2190Sstevel@tonic-gate 			if (j == 0) goto loop;
2200Sstevel@tonic-gate 
221*2139Sjp161948 			if(!BN_GENCB_call(cb, 2, c1-1))
222*2139Sjp161948 				goto err;
2230Sstevel@tonic-gate 			/* We have a safe prime test pass */
2240Sstevel@tonic-gate 			}
2250Sstevel@tonic-gate 		}
2260Sstevel@tonic-gate 	/* we have a prime :-) */
2270Sstevel@tonic-gate 	found = 1;
2280Sstevel@tonic-gate err:
229*2139Sjp161948 	if (ctx != NULL)
230*2139Sjp161948 		{
231*2139Sjp161948 		BN_CTX_end(ctx);
232*2139Sjp161948 		BN_CTX_free(ctx);
233*2139Sjp161948 		}
234*2139Sjp161948 	bn_check_top(ret);
235*2139Sjp161948 	return found;
2360Sstevel@tonic-gate 	}
2370Sstevel@tonic-gate 
BN_is_prime_ex(const BIGNUM * a,int checks,BN_CTX * ctx_passed,BN_GENCB * cb)238*2139Sjp161948 int BN_is_prime_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed, BN_GENCB *cb)
2390Sstevel@tonic-gate 	{
240*2139Sjp161948 	return BN_is_prime_fasttest_ex(a, checks, ctx_passed, 0, cb);
2410Sstevel@tonic-gate 	}
2420Sstevel@tonic-gate 
BN_is_prime_fasttest_ex(const BIGNUM * a,int checks,BN_CTX * ctx_passed,int do_trial_division,BN_GENCB * cb)243*2139Sjp161948 int BN_is_prime_fasttest_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,
244*2139Sjp161948 		int do_trial_division, BN_GENCB *cb)
2450Sstevel@tonic-gate 	{
2460Sstevel@tonic-gate 	int i, j, ret = -1;
2470Sstevel@tonic-gate 	int k;
2480Sstevel@tonic-gate 	BN_CTX *ctx = NULL;
2490Sstevel@tonic-gate 	BIGNUM *A1, *A1_odd, *check; /* taken from ctx */
2500Sstevel@tonic-gate 	BN_MONT_CTX *mont = NULL;
2510Sstevel@tonic-gate 	const BIGNUM *A = NULL;
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	if (BN_cmp(a, BN_value_one()) <= 0)
2540Sstevel@tonic-gate 		return 0;
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	if (checks == BN_prime_checks)
2570Sstevel@tonic-gate 		checks = BN_prime_checks_for_size(BN_num_bits(a));
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	/* first look for small factors */
2600Sstevel@tonic-gate 	if (!BN_is_odd(a))
2610Sstevel@tonic-gate 		return 0;
2620Sstevel@tonic-gate 	if (do_trial_division)
2630Sstevel@tonic-gate 		{
2640Sstevel@tonic-gate 		for (i = 1; i < NUMPRIMES; i++)
2650Sstevel@tonic-gate 			if (BN_mod_word(a, primes[i]) == 0)
2660Sstevel@tonic-gate 				return 0;
267*2139Sjp161948 		if(!BN_GENCB_call(cb, 1, -1))
268*2139Sjp161948 			goto err;
2690Sstevel@tonic-gate 		}
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	if (ctx_passed != NULL)
2720Sstevel@tonic-gate 		ctx = ctx_passed;
2730Sstevel@tonic-gate 	else
2740Sstevel@tonic-gate 		if ((ctx=BN_CTX_new()) == NULL)
2750Sstevel@tonic-gate 			goto err;
2760Sstevel@tonic-gate 	BN_CTX_start(ctx);
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	/* A := abs(a) */
2790Sstevel@tonic-gate 	if (a->neg)
2800Sstevel@tonic-gate 		{
2810Sstevel@tonic-gate 		BIGNUM *t;
2820Sstevel@tonic-gate 		if ((t = BN_CTX_get(ctx)) == NULL) goto err;
2830Sstevel@tonic-gate 		BN_copy(t, a);
2840Sstevel@tonic-gate 		t->neg = 0;
2850Sstevel@tonic-gate 		A = t;
2860Sstevel@tonic-gate 		}
2870Sstevel@tonic-gate 	else
2880Sstevel@tonic-gate 		A = a;
2890Sstevel@tonic-gate 	A1 = BN_CTX_get(ctx);
2900Sstevel@tonic-gate 	A1_odd = BN_CTX_get(ctx);
2910Sstevel@tonic-gate 	check = BN_CTX_get(ctx);
2920Sstevel@tonic-gate 	if (check == NULL) goto err;
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	/* compute A1 := A - 1 */
2950Sstevel@tonic-gate 	if (!BN_copy(A1, A))
2960Sstevel@tonic-gate 		goto err;
2970Sstevel@tonic-gate 	if (!BN_sub_word(A1, 1))
2980Sstevel@tonic-gate 		goto err;
2990Sstevel@tonic-gate 	if (BN_is_zero(A1))
3000Sstevel@tonic-gate 		{
3010Sstevel@tonic-gate 		ret = 0;
3020Sstevel@tonic-gate 		goto err;
3030Sstevel@tonic-gate 		}
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 	/* write  A1  as  A1_odd * 2^k */
3060Sstevel@tonic-gate 	k = 1;
3070Sstevel@tonic-gate 	while (!BN_is_bit_set(A1, k))
3080Sstevel@tonic-gate 		k++;
3090Sstevel@tonic-gate 	if (!BN_rshift(A1_odd, A1, k))
3100Sstevel@tonic-gate 		goto err;
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	/* Montgomery setup for computations mod A */
3130Sstevel@tonic-gate 	mont = BN_MONT_CTX_new();
3140Sstevel@tonic-gate 	if (mont == NULL)
3150Sstevel@tonic-gate 		goto err;
3160Sstevel@tonic-gate 	if (!BN_MONT_CTX_set(mont, A, ctx))
3170Sstevel@tonic-gate 		goto err;
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 	for (i = 0; i < checks; i++)
3200Sstevel@tonic-gate 		{
3210Sstevel@tonic-gate 		if (!BN_pseudo_rand_range(check, A1))
3220Sstevel@tonic-gate 			goto err;
3230Sstevel@tonic-gate 		if (!BN_add_word(check, 1))
3240Sstevel@tonic-gate 			goto err;
3250Sstevel@tonic-gate 		/* now 1 <= check < A */
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 		j = witness(check, A, A1, A1_odd, k, ctx, mont);
3280Sstevel@tonic-gate 		if (j == -1) goto err;
3290Sstevel@tonic-gate 		if (j)
3300Sstevel@tonic-gate 			{
3310Sstevel@tonic-gate 			ret=0;
3320Sstevel@tonic-gate 			goto err;
3330Sstevel@tonic-gate 			}
334*2139Sjp161948 		if(!BN_GENCB_call(cb, 1, i))
335*2139Sjp161948 			goto err;
3360Sstevel@tonic-gate 		}
3370Sstevel@tonic-gate 	ret=1;
3380Sstevel@tonic-gate err:
3390Sstevel@tonic-gate 	if (ctx != NULL)
3400Sstevel@tonic-gate 		{
3410Sstevel@tonic-gate 		BN_CTX_end(ctx);
3420Sstevel@tonic-gate 		if (ctx_passed == NULL)
3430Sstevel@tonic-gate 			BN_CTX_free(ctx);
3440Sstevel@tonic-gate 		}
3450Sstevel@tonic-gate 	if (mont != NULL)
3460Sstevel@tonic-gate 		BN_MONT_CTX_free(mont);
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	return(ret);
3490Sstevel@tonic-gate 	}
3500Sstevel@tonic-gate 
witness(BIGNUM * w,const BIGNUM * a,const BIGNUM * a1,const BIGNUM * a1_odd,int k,BN_CTX * ctx,BN_MONT_CTX * mont)3510Sstevel@tonic-gate static int witness(BIGNUM *w, const BIGNUM *a, const BIGNUM *a1,
3520Sstevel@tonic-gate 	const BIGNUM *a1_odd, int k, BN_CTX *ctx, BN_MONT_CTX *mont)
3530Sstevel@tonic-gate 	{
3540Sstevel@tonic-gate 	if (!BN_mod_exp_mont(w, w, a1_odd, a, ctx, mont)) /* w := w^a1_odd mod a */
3550Sstevel@tonic-gate 		return -1;
3560Sstevel@tonic-gate 	if (BN_is_one(w))
3570Sstevel@tonic-gate 		return 0; /* probably prime */
3580Sstevel@tonic-gate 	if (BN_cmp(w, a1) == 0)
3590Sstevel@tonic-gate 		return 0; /* w == -1 (mod a),  'a' is probably prime */
3600Sstevel@tonic-gate 	while (--k)
3610Sstevel@tonic-gate 		{
3620Sstevel@tonic-gate 		if (!BN_mod_mul(w, w, w, a, ctx)) /* w := w^2 mod a */
3630Sstevel@tonic-gate 			return -1;
3640Sstevel@tonic-gate 		if (BN_is_one(w))
3650Sstevel@tonic-gate 			return 1; /* 'a' is composite, otherwise a previous 'w' would
3660Sstevel@tonic-gate 			           * have been == -1 (mod 'a') */
3670Sstevel@tonic-gate 		if (BN_cmp(w, a1) == 0)
3680Sstevel@tonic-gate 			return 0; /* w == -1 (mod a), 'a' is probably prime */
3690Sstevel@tonic-gate 		}
3700Sstevel@tonic-gate 	/* If we get here, 'w' is the (a-1)/2-th power of the original 'w',
3710Sstevel@tonic-gate 	 * and it is neither -1 nor +1 -- so 'a' cannot be prime */
372*2139Sjp161948 	bn_check_top(w);
3730Sstevel@tonic-gate 	return 1;
3740Sstevel@tonic-gate 	}
3750Sstevel@tonic-gate 
probable_prime(BIGNUM * rnd,int bits)3760Sstevel@tonic-gate static int probable_prime(BIGNUM *rnd, int bits)
3770Sstevel@tonic-gate 	{
3780Sstevel@tonic-gate 	int i;
3790Sstevel@tonic-gate 	BN_ULONG mods[NUMPRIMES];
3800Sstevel@tonic-gate 	BN_ULONG delta,d;
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate again:
3830Sstevel@tonic-gate 	if (!BN_rand(rnd,bits,1,1)) return(0);
3840Sstevel@tonic-gate 	/* we now have a random number 'rand' to test. */
3850Sstevel@tonic-gate 	for (i=1; i<NUMPRIMES; i++)
3860Sstevel@tonic-gate 		mods[i]=BN_mod_word(rnd,(BN_ULONG)primes[i]);
3870Sstevel@tonic-gate 	delta=0;
3880Sstevel@tonic-gate 	loop: for (i=1; i<NUMPRIMES; i++)
3890Sstevel@tonic-gate 		{
3900Sstevel@tonic-gate 		/* check that rnd is not a prime and also
3910Sstevel@tonic-gate 		 * that gcd(rnd-1,primes) == 1 (except for 2) */
3920Sstevel@tonic-gate 		if (((mods[i]+delta)%primes[i]) <= 1)
3930Sstevel@tonic-gate 			{
3940Sstevel@tonic-gate 			d=delta;
3950Sstevel@tonic-gate 			delta+=2;
3960Sstevel@tonic-gate 			/* perhaps need to check for overflow of
3970Sstevel@tonic-gate 			 * delta (but delta can be up to 2^32)
3980Sstevel@tonic-gate 			 * 21-May-98 eay - added overflow check */
3990Sstevel@tonic-gate 			if (delta < d) goto again;
4000Sstevel@tonic-gate 			goto loop;
4010Sstevel@tonic-gate 			}
4020Sstevel@tonic-gate 		}
4030Sstevel@tonic-gate 	if (!BN_add_word(rnd,delta)) return(0);
404*2139Sjp161948 	bn_check_top(rnd);
4050Sstevel@tonic-gate 	return(1);
4060Sstevel@tonic-gate 	}
4070Sstevel@tonic-gate 
probable_prime_dh(BIGNUM * rnd,int bits,const BIGNUM * add,const BIGNUM * rem,BN_CTX * ctx)4080Sstevel@tonic-gate static int probable_prime_dh(BIGNUM *rnd, int bits,
4090Sstevel@tonic-gate 	const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx)
4100Sstevel@tonic-gate 	{
4110Sstevel@tonic-gate 	int i,ret=0;
4120Sstevel@tonic-gate 	BIGNUM *t1;
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	BN_CTX_start(ctx);
4150Sstevel@tonic-gate 	if ((t1 = BN_CTX_get(ctx)) == NULL) goto err;
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	if (!BN_rand(rnd,bits,0,1)) goto err;
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	/* we need ((rnd-rem) % add) == 0 */
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	if (!BN_mod(t1,rnd,add,ctx)) goto err;
4220Sstevel@tonic-gate 	if (!BN_sub(rnd,rnd,t1)) goto err;
4230Sstevel@tonic-gate 	if (rem == NULL)
4240Sstevel@tonic-gate 		{ if (!BN_add_word(rnd,1)) goto err; }
4250Sstevel@tonic-gate 	else
4260Sstevel@tonic-gate 		{ if (!BN_add(rnd,rnd,rem)) goto err; }
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	/* we now have a random number 'rand' to test. */
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	loop: for (i=1; i<NUMPRIMES; i++)
4310Sstevel@tonic-gate 		{
4320Sstevel@tonic-gate 		/* check that rnd is a prime */
4330Sstevel@tonic-gate 		if (BN_mod_word(rnd,(BN_ULONG)primes[i]) <= 1)
4340Sstevel@tonic-gate 			{
4350Sstevel@tonic-gate 			if (!BN_add(rnd,rnd,add)) goto err;
4360Sstevel@tonic-gate 			goto loop;
4370Sstevel@tonic-gate 			}
4380Sstevel@tonic-gate 		}
4390Sstevel@tonic-gate 	ret=1;
4400Sstevel@tonic-gate err:
4410Sstevel@tonic-gate 	BN_CTX_end(ctx);
442*2139Sjp161948 	bn_check_top(rnd);
4430Sstevel@tonic-gate 	return(ret);
4440Sstevel@tonic-gate 	}
4450Sstevel@tonic-gate 
probable_prime_dh_safe(BIGNUM * p,int bits,const BIGNUM * padd,const BIGNUM * rem,BN_CTX * ctx)4460Sstevel@tonic-gate static int probable_prime_dh_safe(BIGNUM *p, int bits, const BIGNUM *padd,
4470Sstevel@tonic-gate 	const BIGNUM *rem, BN_CTX *ctx)
4480Sstevel@tonic-gate 	{
4490Sstevel@tonic-gate 	int i,ret=0;
4500Sstevel@tonic-gate 	BIGNUM *t1,*qadd,*q;
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	bits--;
4530Sstevel@tonic-gate 	BN_CTX_start(ctx);
4540Sstevel@tonic-gate 	t1 = BN_CTX_get(ctx);
4550Sstevel@tonic-gate 	q = BN_CTX_get(ctx);
4560Sstevel@tonic-gate 	qadd = BN_CTX_get(ctx);
4570Sstevel@tonic-gate 	if (qadd == NULL) goto err;
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	if (!BN_rshift1(qadd,padd)) goto err;
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 	if (!BN_rand(q,bits,0,1)) goto err;
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	/* we need ((rnd-rem) % add) == 0 */
4640Sstevel@tonic-gate 	if (!BN_mod(t1,q,qadd,ctx)) goto err;
4650Sstevel@tonic-gate 	if (!BN_sub(q,q,t1)) goto err;
4660Sstevel@tonic-gate 	if (rem == NULL)
4670Sstevel@tonic-gate 		{ if (!BN_add_word(q,1)) goto err; }
4680Sstevel@tonic-gate 	else
4690Sstevel@tonic-gate 		{
4700Sstevel@tonic-gate 		if (!BN_rshift1(t1,rem)) goto err;
4710Sstevel@tonic-gate 		if (!BN_add(q,q,t1)) goto err;
4720Sstevel@tonic-gate 		}
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	/* we now have a random number 'rand' to test. */
4750Sstevel@tonic-gate 	if (!BN_lshift1(p,q)) goto err;
4760Sstevel@tonic-gate 	if (!BN_add_word(p,1)) goto err;
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	loop: for (i=1; i<NUMPRIMES; i++)
4790Sstevel@tonic-gate 		{
4800Sstevel@tonic-gate 		/* check that p and q are prime */
4810Sstevel@tonic-gate 		/* check that for p and q
4820Sstevel@tonic-gate 		 * gcd(p-1,primes) == 1 (except for 2) */
4830Sstevel@tonic-gate 		if (	(BN_mod_word(p,(BN_ULONG)primes[i]) == 0) ||
4840Sstevel@tonic-gate 			(BN_mod_word(q,(BN_ULONG)primes[i]) == 0))
4850Sstevel@tonic-gate 			{
4860Sstevel@tonic-gate 			if (!BN_add(p,p,padd)) goto err;
4870Sstevel@tonic-gate 			if (!BN_add(q,q,qadd)) goto err;
4880Sstevel@tonic-gate 			goto loop;
4890Sstevel@tonic-gate 			}
4900Sstevel@tonic-gate 		}
4910Sstevel@tonic-gate 	ret=1;
4920Sstevel@tonic-gate err:
4930Sstevel@tonic-gate 	BN_CTX_end(ctx);
494*2139Sjp161948 	bn_check_top(p);
4950Sstevel@tonic-gate 	return(ret);
4960Sstevel@tonic-gate 	}
497