xref: /onnv-gate/usr/src/common/openssl/crypto/bn/bn_prime.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /* crypto/bn/bn_prime.c */
2*0Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3*0Sstevel@tonic-gate  * All rights reserved.
4*0Sstevel@tonic-gate  *
5*0Sstevel@tonic-gate  * This package is an SSL implementation written
6*0Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
7*0Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
10*0Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
11*0Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
12*0Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13*0Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
14*0Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15*0Sstevel@tonic-gate  *
16*0Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
17*0Sstevel@tonic-gate  * the code are not to be removed.
18*0Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
19*0Sstevel@tonic-gate  * as the author of the parts of the library used.
20*0Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
21*0Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
22*0Sstevel@tonic-gate  *
23*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
24*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
25*0Sstevel@tonic-gate  * are met:
26*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
27*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
28*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
29*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
30*0Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
31*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
32*0Sstevel@tonic-gate  *    must display the following acknowledgement:
33*0Sstevel@tonic-gate  *    "This product includes cryptographic software written by
34*0Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
35*0Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
36*0Sstevel@tonic-gate  *    being used are not cryptographic related :-).
37*0Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
38*0Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
39*0Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40*0Sstevel@tonic-gate  *
41*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42*0Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44*0Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45*0Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46*0Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47*0Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49*0Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50*0Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51*0Sstevel@tonic-gate  * SUCH DAMAGE.
52*0Sstevel@tonic-gate  *
53*0Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
54*0Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55*0Sstevel@tonic-gate  * copied and put under another distribution licence
56*0Sstevel@tonic-gate  * [including the GNU Public Licence.]
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate /* ====================================================================
59*0Sstevel@tonic-gate  * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
60*0Sstevel@tonic-gate  *
61*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
62*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
63*0Sstevel@tonic-gate  * are met:
64*0Sstevel@tonic-gate  *
65*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
66*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
67*0Sstevel@tonic-gate  *
68*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
69*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
70*0Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
71*0Sstevel@tonic-gate  *    distribution.
72*0Sstevel@tonic-gate  *
73*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
74*0Sstevel@tonic-gate  *    software must display the following acknowledgment:
75*0Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
76*0Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77*0Sstevel@tonic-gate  *
78*0Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79*0Sstevel@tonic-gate  *    endorse or promote products derived from this software without
80*0Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
81*0Sstevel@tonic-gate  *    openssl-core@openssl.org.
82*0Sstevel@tonic-gate  *
83*0Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
84*0Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
85*0Sstevel@tonic-gate  *    permission of the OpenSSL Project.
86*0Sstevel@tonic-gate  *
87*0Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
88*0Sstevel@tonic-gate  *    acknowledgment:
89*0Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
90*0Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91*0Sstevel@tonic-gate  *
92*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93*0Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95*0Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96*0Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97*0Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98*0Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99*0Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101*0Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102*0Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103*0Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
104*0Sstevel@tonic-gate  * ====================================================================
105*0Sstevel@tonic-gate  *
106*0Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
107*0Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
108*0Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
109*0Sstevel@tonic-gate  *
110*0Sstevel@tonic-gate  */
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate #include <stdio.h>
113*0Sstevel@tonic-gate #include <time.h>
114*0Sstevel@tonic-gate #include "cryptlib.h"
115*0Sstevel@tonic-gate #include "bn_lcl.h"
116*0Sstevel@tonic-gate #include <openssl/rand.h>
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate /* The quick sieve algorithm approach to weeding out primes is
119*0Sstevel@tonic-gate  * Philip Zimmermann's, as implemented in PGP.  I have had a read of
120*0Sstevel@tonic-gate  * his comments and implemented my own version.
121*0Sstevel@tonic-gate  */
122*0Sstevel@tonic-gate #include "bn_prime.h"
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate static int witness(BIGNUM *w, const BIGNUM *a, const BIGNUM *a1,
125*0Sstevel@tonic-gate 	const BIGNUM *a1_odd, int k, BN_CTX *ctx, BN_MONT_CTX *mont);
126*0Sstevel@tonic-gate static int probable_prime(BIGNUM *rnd, int bits);
127*0Sstevel@tonic-gate static int probable_prime_dh(BIGNUM *rnd, int bits,
128*0Sstevel@tonic-gate 	const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx);
129*0Sstevel@tonic-gate static int probable_prime_dh_safe(BIGNUM *rnd, int bits,
130*0Sstevel@tonic-gate 	const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx);
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate BIGNUM *BN_generate_prime(BIGNUM *ret, int bits, int safe,
133*0Sstevel@tonic-gate 	const BIGNUM *add, const BIGNUM *rem,
134*0Sstevel@tonic-gate 	void (*callback)(int,int,void *), void *cb_arg)
135*0Sstevel@tonic-gate 	{
136*0Sstevel@tonic-gate 	BIGNUM *rnd=NULL;
137*0Sstevel@tonic-gate 	BIGNUM t;
138*0Sstevel@tonic-gate 	int found=0;
139*0Sstevel@tonic-gate 	int i,j,c1=0;
140*0Sstevel@tonic-gate 	BN_CTX *ctx;
141*0Sstevel@tonic-gate 	int checks = BN_prime_checks_for_size(bits);
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 	BN_init(&t);
144*0Sstevel@tonic-gate 	ctx=BN_CTX_new();
145*0Sstevel@tonic-gate 	if (ctx == NULL) goto err;
146*0Sstevel@tonic-gate 	if (ret == NULL)
147*0Sstevel@tonic-gate 		{
148*0Sstevel@tonic-gate 		if ((rnd=BN_new()) == NULL) goto err;
149*0Sstevel@tonic-gate 		}
150*0Sstevel@tonic-gate 	else
151*0Sstevel@tonic-gate 		rnd=ret;
152*0Sstevel@tonic-gate loop:
153*0Sstevel@tonic-gate 	/* make a random number and set the top and bottom bits */
154*0Sstevel@tonic-gate 	if (add == NULL)
155*0Sstevel@tonic-gate 		{
156*0Sstevel@tonic-gate 		if (!probable_prime(rnd,bits)) goto err;
157*0Sstevel@tonic-gate 		}
158*0Sstevel@tonic-gate 	else
159*0Sstevel@tonic-gate 		{
160*0Sstevel@tonic-gate 		if (safe)
161*0Sstevel@tonic-gate 			{
162*0Sstevel@tonic-gate 			if (!probable_prime_dh_safe(rnd,bits,add,rem,ctx))
163*0Sstevel@tonic-gate 				 goto err;
164*0Sstevel@tonic-gate 			}
165*0Sstevel@tonic-gate 		else
166*0Sstevel@tonic-gate 			{
167*0Sstevel@tonic-gate 			if (!probable_prime_dh(rnd,bits,add,rem,ctx))
168*0Sstevel@tonic-gate 				goto err;
169*0Sstevel@tonic-gate 			}
170*0Sstevel@tonic-gate 		}
171*0Sstevel@tonic-gate 	/* if (BN_mod_word(rnd,(BN_ULONG)3) == 1) goto loop; */
172*0Sstevel@tonic-gate 	if (callback != NULL) callback(0,c1++,cb_arg);
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 	if (!safe)
175*0Sstevel@tonic-gate 		{
176*0Sstevel@tonic-gate 		i=BN_is_prime_fasttest(rnd,checks,callback,ctx,cb_arg,0);
177*0Sstevel@tonic-gate 		if (i == -1) goto err;
178*0Sstevel@tonic-gate 		if (i == 0) goto loop;
179*0Sstevel@tonic-gate 		}
180*0Sstevel@tonic-gate 	else
181*0Sstevel@tonic-gate 		{
182*0Sstevel@tonic-gate 		/* for "safe prime" generation,
183*0Sstevel@tonic-gate 		 * check that (p-1)/2 is prime.
184*0Sstevel@tonic-gate 		 * Since a prime is odd, We just
185*0Sstevel@tonic-gate 		 * need to divide by 2 */
186*0Sstevel@tonic-gate 		if (!BN_rshift1(&t,rnd)) goto err;
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 		for (i=0; i<checks; i++)
189*0Sstevel@tonic-gate 			{
190*0Sstevel@tonic-gate 			j=BN_is_prime_fasttest(rnd,1,callback,ctx,cb_arg,0);
191*0Sstevel@tonic-gate 			if (j == -1) goto err;
192*0Sstevel@tonic-gate 			if (j == 0) goto loop;
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate 			j=BN_is_prime_fasttest(&t,1,callback,ctx,cb_arg,0);
195*0Sstevel@tonic-gate 			if (j == -1) goto err;
196*0Sstevel@tonic-gate 			if (j == 0) goto loop;
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate 			if (callback != NULL) callback(2,c1-1,cb_arg);
199*0Sstevel@tonic-gate 			/* We have a safe prime test pass */
200*0Sstevel@tonic-gate 			}
201*0Sstevel@tonic-gate 		}
202*0Sstevel@tonic-gate 	/* we have a prime :-) */
203*0Sstevel@tonic-gate 	found = 1;
204*0Sstevel@tonic-gate err:
205*0Sstevel@tonic-gate 	if (!found && (ret == NULL) && (rnd != NULL)) BN_free(rnd);
206*0Sstevel@tonic-gate 	BN_free(&t);
207*0Sstevel@tonic-gate 	if (ctx != NULL) BN_CTX_free(ctx);
208*0Sstevel@tonic-gate 	return(found ? rnd : NULL);
209*0Sstevel@tonic-gate 	}
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate int BN_is_prime(const BIGNUM *a, int checks, void (*callback)(int,int,void *),
212*0Sstevel@tonic-gate 	BN_CTX *ctx_passed, void *cb_arg)
213*0Sstevel@tonic-gate 	{
214*0Sstevel@tonic-gate 	return BN_is_prime_fasttest(a, checks, callback, ctx_passed, cb_arg, 0);
215*0Sstevel@tonic-gate 	}
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate int BN_is_prime_fasttest(const BIGNUM *a, int checks,
218*0Sstevel@tonic-gate 		void (*callback)(int,int,void *),
219*0Sstevel@tonic-gate 		BN_CTX *ctx_passed, void *cb_arg,
220*0Sstevel@tonic-gate 		int do_trial_division)
221*0Sstevel@tonic-gate 	{
222*0Sstevel@tonic-gate 	int i, j, ret = -1;
223*0Sstevel@tonic-gate 	int k;
224*0Sstevel@tonic-gate 	BN_CTX *ctx = NULL;
225*0Sstevel@tonic-gate 	BIGNUM *A1, *A1_odd, *check; /* taken from ctx */
226*0Sstevel@tonic-gate 	BN_MONT_CTX *mont = NULL;
227*0Sstevel@tonic-gate 	const BIGNUM *A = NULL;
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate 	if (BN_cmp(a, BN_value_one()) <= 0)
230*0Sstevel@tonic-gate 		return 0;
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 	if (checks == BN_prime_checks)
233*0Sstevel@tonic-gate 		checks = BN_prime_checks_for_size(BN_num_bits(a));
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 	/* first look for small factors */
236*0Sstevel@tonic-gate 	if (!BN_is_odd(a))
237*0Sstevel@tonic-gate 		return 0;
238*0Sstevel@tonic-gate 	if (do_trial_division)
239*0Sstevel@tonic-gate 		{
240*0Sstevel@tonic-gate 		for (i = 1; i < NUMPRIMES; i++)
241*0Sstevel@tonic-gate 			if (BN_mod_word(a, primes[i]) == 0)
242*0Sstevel@tonic-gate 				return 0;
243*0Sstevel@tonic-gate 		if (callback != NULL) callback(1, -1, cb_arg);
244*0Sstevel@tonic-gate 		}
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate 	if (ctx_passed != NULL)
247*0Sstevel@tonic-gate 		ctx = ctx_passed;
248*0Sstevel@tonic-gate 	else
249*0Sstevel@tonic-gate 		if ((ctx=BN_CTX_new()) == NULL)
250*0Sstevel@tonic-gate 			goto err;
251*0Sstevel@tonic-gate 	BN_CTX_start(ctx);
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate 	/* A := abs(a) */
254*0Sstevel@tonic-gate 	if (a->neg)
255*0Sstevel@tonic-gate 		{
256*0Sstevel@tonic-gate 		BIGNUM *t;
257*0Sstevel@tonic-gate 		if ((t = BN_CTX_get(ctx)) == NULL) goto err;
258*0Sstevel@tonic-gate 		BN_copy(t, a);
259*0Sstevel@tonic-gate 		t->neg = 0;
260*0Sstevel@tonic-gate 		A = t;
261*0Sstevel@tonic-gate 		}
262*0Sstevel@tonic-gate 	else
263*0Sstevel@tonic-gate 		A = a;
264*0Sstevel@tonic-gate 	A1 = BN_CTX_get(ctx);
265*0Sstevel@tonic-gate 	A1_odd = BN_CTX_get(ctx);
266*0Sstevel@tonic-gate 	check = BN_CTX_get(ctx);
267*0Sstevel@tonic-gate 	if (check == NULL) goto err;
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate 	/* compute A1 := A - 1 */
270*0Sstevel@tonic-gate 	if (!BN_copy(A1, A))
271*0Sstevel@tonic-gate 		goto err;
272*0Sstevel@tonic-gate 	if (!BN_sub_word(A1, 1))
273*0Sstevel@tonic-gate 		goto err;
274*0Sstevel@tonic-gate 	if (BN_is_zero(A1))
275*0Sstevel@tonic-gate 		{
276*0Sstevel@tonic-gate 		ret = 0;
277*0Sstevel@tonic-gate 		goto err;
278*0Sstevel@tonic-gate 		}
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate 	/* write  A1  as  A1_odd * 2^k */
281*0Sstevel@tonic-gate 	k = 1;
282*0Sstevel@tonic-gate 	while (!BN_is_bit_set(A1, k))
283*0Sstevel@tonic-gate 		k++;
284*0Sstevel@tonic-gate 	if (!BN_rshift(A1_odd, A1, k))
285*0Sstevel@tonic-gate 		goto err;
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate 	/* Montgomery setup for computations mod A */
288*0Sstevel@tonic-gate 	mont = BN_MONT_CTX_new();
289*0Sstevel@tonic-gate 	if (mont == NULL)
290*0Sstevel@tonic-gate 		goto err;
291*0Sstevel@tonic-gate 	if (!BN_MONT_CTX_set(mont, A, ctx))
292*0Sstevel@tonic-gate 		goto err;
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate 	for (i = 0; i < checks; i++)
295*0Sstevel@tonic-gate 		{
296*0Sstevel@tonic-gate 		if (!BN_pseudo_rand_range(check, A1))
297*0Sstevel@tonic-gate 			goto err;
298*0Sstevel@tonic-gate 		if (!BN_add_word(check, 1))
299*0Sstevel@tonic-gate 			goto err;
300*0Sstevel@tonic-gate 		/* now 1 <= check < A */
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate 		j = witness(check, A, A1, A1_odd, k, ctx, mont);
303*0Sstevel@tonic-gate 		if (j == -1) goto err;
304*0Sstevel@tonic-gate 		if (j)
305*0Sstevel@tonic-gate 			{
306*0Sstevel@tonic-gate 			ret=0;
307*0Sstevel@tonic-gate 			goto err;
308*0Sstevel@tonic-gate 			}
309*0Sstevel@tonic-gate 		if (callback != NULL) callback(1,i,cb_arg);
310*0Sstevel@tonic-gate 		}
311*0Sstevel@tonic-gate 	ret=1;
312*0Sstevel@tonic-gate err:
313*0Sstevel@tonic-gate 	if (ctx != NULL)
314*0Sstevel@tonic-gate 		{
315*0Sstevel@tonic-gate 		BN_CTX_end(ctx);
316*0Sstevel@tonic-gate 		if (ctx_passed == NULL)
317*0Sstevel@tonic-gate 			BN_CTX_free(ctx);
318*0Sstevel@tonic-gate 		}
319*0Sstevel@tonic-gate 	if (mont != NULL)
320*0Sstevel@tonic-gate 		BN_MONT_CTX_free(mont);
321*0Sstevel@tonic-gate 
322*0Sstevel@tonic-gate 	return(ret);
323*0Sstevel@tonic-gate 	}
324*0Sstevel@tonic-gate 
325*0Sstevel@tonic-gate static int witness(BIGNUM *w, const BIGNUM *a, const BIGNUM *a1,
326*0Sstevel@tonic-gate 	const BIGNUM *a1_odd, int k, BN_CTX *ctx, BN_MONT_CTX *mont)
327*0Sstevel@tonic-gate 	{
328*0Sstevel@tonic-gate 	if (!BN_mod_exp_mont(w, w, a1_odd, a, ctx, mont)) /* w := w^a1_odd mod a */
329*0Sstevel@tonic-gate 		return -1;
330*0Sstevel@tonic-gate 	if (BN_is_one(w))
331*0Sstevel@tonic-gate 		return 0; /* probably prime */
332*0Sstevel@tonic-gate 	if (BN_cmp(w, a1) == 0)
333*0Sstevel@tonic-gate 		return 0; /* w == -1 (mod a),  'a' is probably prime */
334*0Sstevel@tonic-gate 	while (--k)
335*0Sstevel@tonic-gate 		{
336*0Sstevel@tonic-gate 		if (!BN_mod_mul(w, w, w, a, ctx)) /* w := w^2 mod a */
337*0Sstevel@tonic-gate 			return -1;
338*0Sstevel@tonic-gate 		if (BN_is_one(w))
339*0Sstevel@tonic-gate 			return 1; /* 'a' is composite, otherwise a previous 'w' would
340*0Sstevel@tonic-gate 			           * have been == -1 (mod 'a') */
341*0Sstevel@tonic-gate 		if (BN_cmp(w, a1) == 0)
342*0Sstevel@tonic-gate 			return 0; /* w == -1 (mod a), 'a' is probably prime */
343*0Sstevel@tonic-gate 		}
344*0Sstevel@tonic-gate 	/* If we get here, 'w' is the (a-1)/2-th power of the original 'w',
345*0Sstevel@tonic-gate 	 * and it is neither -1 nor +1 -- so 'a' cannot be prime */
346*0Sstevel@tonic-gate 	return 1;
347*0Sstevel@tonic-gate 	}
348*0Sstevel@tonic-gate 
349*0Sstevel@tonic-gate static int probable_prime(BIGNUM *rnd, int bits)
350*0Sstevel@tonic-gate 	{
351*0Sstevel@tonic-gate 	int i;
352*0Sstevel@tonic-gate 	BN_ULONG mods[NUMPRIMES];
353*0Sstevel@tonic-gate 	BN_ULONG delta,d;
354*0Sstevel@tonic-gate 
355*0Sstevel@tonic-gate again:
356*0Sstevel@tonic-gate 	if (!BN_rand(rnd,bits,1,1)) return(0);
357*0Sstevel@tonic-gate 	/* we now have a random number 'rand' to test. */
358*0Sstevel@tonic-gate 	for (i=1; i<NUMPRIMES; i++)
359*0Sstevel@tonic-gate 		mods[i]=BN_mod_word(rnd,(BN_ULONG)primes[i]);
360*0Sstevel@tonic-gate 	delta=0;
361*0Sstevel@tonic-gate 	loop: for (i=1; i<NUMPRIMES; i++)
362*0Sstevel@tonic-gate 		{
363*0Sstevel@tonic-gate 		/* check that rnd is not a prime and also
364*0Sstevel@tonic-gate 		 * that gcd(rnd-1,primes) == 1 (except for 2) */
365*0Sstevel@tonic-gate 		if (((mods[i]+delta)%primes[i]) <= 1)
366*0Sstevel@tonic-gate 			{
367*0Sstevel@tonic-gate 			d=delta;
368*0Sstevel@tonic-gate 			delta+=2;
369*0Sstevel@tonic-gate 			/* perhaps need to check for overflow of
370*0Sstevel@tonic-gate 			 * delta (but delta can be up to 2^32)
371*0Sstevel@tonic-gate 			 * 21-May-98 eay - added overflow check */
372*0Sstevel@tonic-gate 			if (delta < d) goto again;
373*0Sstevel@tonic-gate 			goto loop;
374*0Sstevel@tonic-gate 			}
375*0Sstevel@tonic-gate 		}
376*0Sstevel@tonic-gate 	if (!BN_add_word(rnd,delta)) return(0);
377*0Sstevel@tonic-gate 	return(1);
378*0Sstevel@tonic-gate 	}
379*0Sstevel@tonic-gate 
380*0Sstevel@tonic-gate static int probable_prime_dh(BIGNUM *rnd, int bits,
381*0Sstevel@tonic-gate 	const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx)
382*0Sstevel@tonic-gate 	{
383*0Sstevel@tonic-gate 	int i,ret=0;
384*0Sstevel@tonic-gate 	BIGNUM *t1;
385*0Sstevel@tonic-gate 
386*0Sstevel@tonic-gate 	BN_CTX_start(ctx);
387*0Sstevel@tonic-gate 	if ((t1 = BN_CTX_get(ctx)) == NULL) goto err;
388*0Sstevel@tonic-gate 
389*0Sstevel@tonic-gate 	if (!BN_rand(rnd,bits,0,1)) goto err;
390*0Sstevel@tonic-gate 
391*0Sstevel@tonic-gate 	/* we need ((rnd-rem) % add) == 0 */
392*0Sstevel@tonic-gate 
393*0Sstevel@tonic-gate 	if (!BN_mod(t1,rnd,add,ctx)) goto err;
394*0Sstevel@tonic-gate 	if (!BN_sub(rnd,rnd,t1)) goto err;
395*0Sstevel@tonic-gate 	if (rem == NULL)
396*0Sstevel@tonic-gate 		{ if (!BN_add_word(rnd,1)) goto err; }
397*0Sstevel@tonic-gate 	else
398*0Sstevel@tonic-gate 		{ if (!BN_add(rnd,rnd,rem)) goto err; }
399*0Sstevel@tonic-gate 
400*0Sstevel@tonic-gate 	/* we now have a random number 'rand' to test. */
401*0Sstevel@tonic-gate 
402*0Sstevel@tonic-gate 	loop: for (i=1; i<NUMPRIMES; i++)
403*0Sstevel@tonic-gate 		{
404*0Sstevel@tonic-gate 		/* check that rnd is a prime */
405*0Sstevel@tonic-gate 		if (BN_mod_word(rnd,(BN_ULONG)primes[i]) <= 1)
406*0Sstevel@tonic-gate 			{
407*0Sstevel@tonic-gate 			if (!BN_add(rnd,rnd,add)) goto err;
408*0Sstevel@tonic-gate 			goto loop;
409*0Sstevel@tonic-gate 			}
410*0Sstevel@tonic-gate 		}
411*0Sstevel@tonic-gate 	ret=1;
412*0Sstevel@tonic-gate err:
413*0Sstevel@tonic-gate 	BN_CTX_end(ctx);
414*0Sstevel@tonic-gate 	return(ret);
415*0Sstevel@tonic-gate 	}
416*0Sstevel@tonic-gate 
417*0Sstevel@tonic-gate static int probable_prime_dh_safe(BIGNUM *p, int bits, const BIGNUM *padd,
418*0Sstevel@tonic-gate 	const BIGNUM *rem, BN_CTX *ctx)
419*0Sstevel@tonic-gate 	{
420*0Sstevel@tonic-gate 	int i,ret=0;
421*0Sstevel@tonic-gate 	BIGNUM *t1,*qadd,*q;
422*0Sstevel@tonic-gate 
423*0Sstevel@tonic-gate 	bits--;
424*0Sstevel@tonic-gate 	BN_CTX_start(ctx);
425*0Sstevel@tonic-gate 	t1 = BN_CTX_get(ctx);
426*0Sstevel@tonic-gate 	q = BN_CTX_get(ctx);
427*0Sstevel@tonic-gate 	qadd = BN_CTX_get(ctx);
428*0Sstevel@tonic-gate 	if (qadd == NULL) goto err;
429*0Sstevel@tonic-gate 
430*0Sstevel@tonic-gate 	if (!BN_rshift1(qadd,padd)) goto err;
431*0Sstevel@tonic-gate 
432*0Sstevel@tonic-gate 	if (!BN_rand(q,bits,0,1)) goto err;
433*0Sstevel@tonic-gate 
434*0Sstevel@tonic-gate 	/* we need ((rnd-rem) % add) == 0 */
435*0Sstevel@tonic-gate 	if (!BN_mod(t1,q,qadd,ctx)) goto err;
436*0Sstevel@tonic-gate 	if (!BN_sub(q,q,t1)) goto err;
437*0Sstevel@tonic-gate 	if (rem == NULL)
438*0Sstevel@tonic-gate 		{ if (!BN_add_word(q,1)) goto err; }
439*0Sstevel@tonic-gate 	else
440*0Sstevel@tonic-gate 		{
441*0Sstevel@tonic-gate 		if (!BN_rshift1(t1,rem)) goto err;
442*0Sstevel@tonic-gate 		if (!BN_add(q,q,t1)) goto err;
443*0Sstevel@tonic-gate 		}
444*0Sstevel@tonic-gate 
445*0Sstevel@tonic-gate 	/* we now have a random number 'rand' to test. */
446*0Sstevel@tonic-gate 	if (!BN_lshift1(p,q)) goto err;
447*0Sstevel@tonic-gate 	if (!BN_add_word(p,1)) goto err;
448*0Sstevel@tonic-gate 
449*0Sstevel@tonic-gate 	loop: for (i=1; i<NUMPRIMES; i++)
450*0Sstevel@tonic-gate 		{
451*0Sstevel@tonic-gate 		/* check that p and q are prime */
452*0Sstevel@tonic-gate 		/* check that for p and q
453*0Sstevel@tonic-gate 		 * gcd(p-1,primes) == 1 (except for 2) */
454*0Sstevel@tonic-gate 		if (	(BN_mod_word(p,(BN_ULONG)primes[i]) == 0) ||
455*0Sstevel@tonic-gate 			(BN_mod_word(q,(BN_ULONG)primes[i]) == 0))
456*0Sstevel@tonic-gate 			{
457*0Sstevel@tonic-gate 			if (!BN_add(p,p,padd)) goto err;
458*0Sstevel@tonic-gate 			if (!BN_add(q,q,qadd)) goto err;
459*0Sstevel@tonic-gate 			goto loop;
460*0Sstevel@tonic-gate 			}
461*0Sstevel@tonic-gate 		}
462*0Sstevel@tonic-gate 	ret=1;
463*0Sstevel@tonic-gate err:
464*0Sstevel@tonic-gate 	BN_CTX_end(ctx);
465*0Sstevel@tonic-gate 	return(ret);
466*0Sstevel@tonic-gate 	}
467