xref: /openbsd-src/lib/libcrypto/bn/bn_prime.c (revision fc405d53b73a2d73393cb97f684863d17b583e38)
1 /* $OpenBSD: bn_prime.c,v 1.32 2023/05/10 12:21:55 tb Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 /* ====================================================================
59  * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
60  *
61  * Redistribution and use in source and binary forms, with or without
62  * modification, are permitted provided that the following conditions
63  * are met:
64  *
65  * 1. Redistributions of source code must retain the above copyright
66  *    notice, this list of conditions and the following disclaimer.
67  *
68  * 2. Redistributions in binary form must reproduce the above copyright
69  *    notice, this list of conditions and the following disclaimer in
70  *    the documentation and/or other materials provided with the
71  *    distribution.
72  *
73  * 3. All advertising materials mentioning features or use of this
74  *    software must display the following acknowledgment:
75  *    "This product includes software developed by the OpenSSL Project
76  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77  *
78  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79  *    endorse or promote products derived from this software without
80  *    prior written permission. For written permission, please contact
81  *    openssl-core@openssl.org.
82  *
83  * 5. Products derived from this software may not be called "OpenSSL"
84  *    nor may "OpenSSL" appear in their names without prior written
85  *    permission of the OpenSSL Project.
86  *
87  * 6. Redistributions of any form whatsoever must retain the following
88  *    acknowledgment:
89  *    "This product includes software developed by the OpenSSL Project
90  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91  *
92  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103  * OF THE POSSIBILITY OF SUCH DAMAGE.
104  * ====================================================================
105  *
106  * This product includes cryptographic software written by Eric Young
107  * (eay@cryptsoft.com).  This product includes software written by Tim
108  * Hudson (tjh@cryptsoft.com).
109  *
110  */
111 
112 #include <stdio.h>
113 #include <time.h>
114 
115 #include <openssl/err.h>
116 
117 #include "bn_local.h"
118 
119 /* The quick sieve algorithm approach to weeding out primes is
120  * Philip Zimmermann's, as implemented in PGP.  I have had a read of
121  * his comments and implemented my own version.
122  */
123 #include "bn_prime.h"
124 
125 static int probable_prime(BIGNUM *rnd, int bits);
126 static int probable_prime_dh(BIGNUM *rnd, int bits,
127     const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx);
128 static int probable_prime_dh_safe(BIGNUM *rnd, int bits,
129     const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx);
130 
131 int
132 BN_GENCB_call(BN_GENCB *cb, int a, int b)
133 {
134 	/* No callback means continue */
135 	if (!cb)
136 		return 1;
137 	switch (cb->ver) {
138 	case 1:
139 		/* Deprecated-style callbacks */
140 		if (!cb->cb.cb_1)
141 			return 1;
142 		cb->cb.cb_1(a, b, cb->arg);
143 		return 1;
144 	case 2:
145 		/* New-style callbacks */
146 		return cb->cb.cb_2(a, b, cb);
147 	default:
148 		break;
149 	}
150 	/* Unrecognised callback type */
151 	return 0;
152 }
153 
154 int
155 BN_generate_prime_ex(BIGNUM *ret, int bits, int safe, const BIGNUM *add,
156     const BIGNUM *rem, BN_GENCB *cb)
157 {
158 	BN_CTX *ctx;
159 	BIGNUM *p;
160 	int is_prime;
161 	int loops = 0;
162 	int found = 0;
163 
164 	if (bits < 2 || (bits == 2 && safe)) {
165 		/*
166 		 * There are no prime numbers smaller than 2, and the smallest
167 		 * safe prime (7) spans three bits.
168 		 */
169 		BNerror(BN_R_BITS_TOO_SMALL);
170 		return 0;
171 	}
172 
173 	if ((ctx = BN_CTX_new()) == NULL)
174 		goto err;
175 	BN_CTX_start(ctx);
176 	if ((p = BN_CTX_get(ctx)) == NULL)
177 		goto err;
178 
179  loop:
180 	/* Make a random number and set the top and bottom bits. */
181 	if (add == NULL) {
182 		if (!probable_prime(ret, bits))
183 			goto err;
184 	} else {
185 		if (safe) {
186 			if (!probable_prime_dh_safe(ret, bits, add, rem, ctx))
187 				goto err;
188 		} else {
189 			if (!probable_prime_dh(ret, bits, add, rem, ctx))
190 				goto err;
191 		}
192 	}
193 
194 	if (!BN_GENCB_call(cb, 0, loops++))
195 		goto err;
196 
197 	if (!safe) {
198 		if (!bn_is_prime_bpsw(&is_prime, ret, ctx, 1))
199 			goto err;
200 		if (!is_prime)
201 			goto loop;
202 	} else {
203 		if (!bn_is_prime_bpsw(&is_prime, ret, ctx, 1))
204 			goto err;
205 		if (!is_prime)
206 			goto loop;
207 
208 		/*
209 		 * For safe prime generation, check that p = (ret-1)/2 is prime.
210 		 * Since this prime has >= 3 bits, it is odd, and we can simply
211 		 * divide by 2.
212 		 */
213 		if (!BN_rshift1(p, ret))
214 			goto err;
215 
216 		if (!bn_is_prime_bpsw(&is_prime, p, ctx, 1))
217 			goto err;
218 		if (!is_prime)
219 			goto loop;
220 
221 		if (!BN_GENCB_call(cb, 2, loops - 1))
222 			goto err;
223 	}
224 
225 	found = 1;
226 
227  err:
228 	BN_CTX_end(ctx);
229 	BN_CTX_free(ctx);
230 
231 	return found;
232 }
233 
234 int
235 BN_is_prime_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed, BN_GENCB *cb)
236 {
237 	return BN_is_prime_fasttest_ex(a, checks, ctx_passed, 0, cb);
238 }
239 
240 int
241 BN_is_prime_fasttest_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,
242     int do_trial_division, BN_GENCB *cb)
243 {
244 	int is_prime;
245 
246 	if (checks < 0)
247 		return -1;
248 
249 	if (checks == BN_prime_checks)
250 		checks = BN_prime_checks_for_size(BN_num_bits(a));
251 
252 	/* XXX - tickle BN_GENCB in bn_is_prime_bpsw(). */
253 	if (!bn_is_prime_bpsw(&is_prime, a, ctx_passed, checks))
254 		return -1;
255 
256 	return is_prime;
257 }
258 
259 static int
260 probable_prime(BIGNUM *rnd, int bits)
261 {
262 	int i;
263 	BN_ULONG mods[NUMPRIMES];
264 	BN_ULONG delta, maxdelta;
265 
266 again:
267 	if (!BN_rand(rnd, bits, 1, 1))
268 		return (0);
269 	/* we now have a random number 'rand' to test. */
270 	for (i = 1; i < NUMPRIMES; i++) {
271 		BN_ULONG mod = BN_mod_word(rnd, primes[i]);
272 		if (mod == (BN_ULONG)-1)
273 			return (0);
274 		mods[i] = mod;
275 	}
276 	maxdelta = BN_MASK2 - primes[NUMPRIMES - 1];
277 	delta = 0;
278 loop:
279 	for (i = 1; i < NUMPRIMES; i++) {
280 		/* check that rnd is not a prime and also
281 		 * that gcd(rnd-1,primes) == 1 (except for 2) */
282 		if (((mods[i] + delta) % primes[i]) <= 1) {
283 			delta += 2;
284 			if (delta > maxdelta)
285 				goto again;
286 			goto loop;
287 		}
288 	}
289 	if (!BN_add_word(rnd, delta))
290 		return (0);
291 	return (1);
292 }
293 
294 static int
295 probable_prime_dh(BIGNUM *rnd, int bits, const BIGNUM *add, const BIGNUM *rem,
296     BN_CTX *ctx)
297 {
298 	int i, ret = 0;
299 	BIGNUM *t1;
300 
301 	BN_CTX_start(ctx);
302 	if ((t1 = BN_CTX_get(ctx)) == NULL)
303 		goto err;
304 
305 	if (!BN_rand(rnd, bits, 0, 1))
306 		goto err;
307 
308 	/* we need ((rnd-rem) % add) == 0 */
309 
310 	if (!BN_mod_ct(t1, rnd, add, ctx))
311 		goto err;
312 	if (!BN_sub(rnd, rnd, t1))
313 		goto err;
314 	if (rem == NULL) {
315 		if (!BN_add_word(rnd, 1))
316 			goto err;
317 	} else {
318 		if (!BN_add(rnd, rnd, rem))
319 			goto err;
320 	}
321 
322 	/* we now have a random number 'rand' to test. */
323 
324 loop:
325 	for (i = 1; i < NUMPRIMES; i++) {
326 		/* check that rnd is a prime */
327 		BN_LONG mod = BN_mod_word(rnd, primes[i]);
328 		if (mod == (BN_ULONG)-1)
329 			goto err;
330 		if (mod <= 1) {
331 			if (!BN_add(rnd, rnd, add))
332 				goto err;
333 			goto loop;
334 		}
335 	}
336 	ret = 1;
337 
338 err:
339 	BN_CTX_end(ctx);
340 	return (ret);
341 }
342 
343 static int
344 probable_prime_dh_safe(BIGNUM *p, int bits, const BIGNUM *padd,
345     const BIGNUM *rem, BN_CTX *ctx)
346 {
347 	int i, ret = 0;
348 	BIGNUM *t1, *qadd, *q;
349 
350 	bits--;
351 	BN_CTX_start(ctx);
352 	if ((t1 = BN_CTX_get(ctx)) == NULL)
353 		goto err;
354 	if ((q = BN_CTX_get(ctx)) == NULL)
355 		goto err;
356 	if ((qadd = BN_CTX_get(ctx)) == NULL)
357 		goto err;
358 
359 	if (!BN_rshift1(qadd, padd))
360 		goto err;
361 
362 	if (!BN_rand(q, bits, 0, 1))
363 		goto err;
364 
365 	/* we need ((rnd-rem) % add) == 0 */
366 	if (!BN_mod_ct(t1, q,qadd, ctx))
367 		goto err;
368 	if (!BN_sub(q, q, t1))
369 		goto err;
370 	if (rem == NULL) {
371 		if (!BN_add_word(q, 1))
372 			goto err;
373 	} else {
374 		if (!BN_rshift1(t1, rem))
375 			goto err;
376 		if (!BN_add(q, q, t1))
377 			goto err;
378 	}
379 
380 	/* we now have a random number 'rand' to test. */
381 	if (!BN_lshift1(p, q))
382 		goto err;
383 	if (!BN_add_word(p, 1))
384 		goto err;
385 
386 loop:
387 	for (i = 1; i < NUMPRIMES; i++) {
388 		/* check that p and q are prime */
389 		/* check that for p and q
390 		 * gcd(p-1,primes) == 1 (except for 2) */
391 		BN_ULONG pmod = BN_mod_word(p, primes[i]);
392 		BN_ULONG qmod = BN_mod_word(q, primes[i]);
393 		if (pmod == (BN_ULONG)-1 || qmod == (BN_ULONG)-1)
394 			goto err;
395 		if (pmod == 0 || qmod == 0) {
396 			if (!BN_add(p, p, padd))
397 				goto err;
398 			if (!BN_add(q, q, qadd))
399 				goto err;
400 			goto loop;
401 		}
402 	}
403 	ret = 1;
404 
405 err:
406 	BN_CTX_end(ctx);
407 	return (ret);
408 }
409