2 * WARNING: do not edit!
3 * Generated by crypto/bn/bn_prime.pl
4 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
6 * Licensed under the OpenSSL license (the "License"). You may not use
7 * this file except in compliance with the License. You can obtain a copy
8 * in the file LICENSE in the source distribution or at
9 * https://www.openssl.org/source/license.html
14 #include "internal/cryptlib.h"
18 * The quick sieve algorithm approach to weeding out primes is Philip
19 * Zimmermann's, as implemented in PGP. I have had a read of his comments
20 * and implemented my own version.
24 static int witness(BIGNUM *w, const BIGNUM *a, const BIGNUM *a1,
25 const BIGNUM *a1_odd, int k, BN_CTX *ctx,
27 static int probable_prime(BIGNUM *rnd, int bits, prime_t *mods);
28 static int probable_prime_dh_safe(BIGNUM *rnd, int bits,
29 const BIGNUM *add, const BIGNUM *rem,
32 int BN_GENCB_call(BN_GENCB *cb, int a, int b)
34 /* No callback means continue */
39 /* Deprecated-style callbacks */
42 cb->cb.cb_1(a, b, cb->arg);
45 /* New-style callbacks */
46 return cb->cb.cb_2(a, b, cb);
50 /* Unrecognised callback type */
54 int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe,
55 const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb)
62 int checks = BN_prime_checks_for_size(bits);
65 /* There are no prime numbers this small. */
66 BNerr(BN_F_BN_GENERATE_PRIME_EX, BN_R_BITS_TOO_SMALL);
68 } else if (bits == 2 && safe) {
69 /* The smallest safe prime (7) is three bits. */
70 BNerr(BN_F_BN_GENERATE_PRIME_EX, BN_R_BITS_TOO_SMALL);
74 mods = OPENSSL_zalloc(sizeof(*mods) * NUMPRIMES);
86 /* make a random number and set the top and bottom bits */
88 if (!probable_prime(ret, bits, mods))
92 if (!probable_prime_dh_safe(ret, bits, add, rem, ctx))
95 if (!bn_probable_prime_dh(ret, bits, add, rem, ctx))
100 if (!BN_GENCB_call(cb, 0, c1++))
105 i = BN_is_prime_fasttest_ex(ret, checks, ctx, 0, cb);
112 * for "safe prime" generation, check that (p-1)/2 is prime. Since a
113 * prime is odd, We just need to divide by 2
115 if (!BN_rshift1(t, ret))
118 for (i = 0; i < checks; i++) {
119 j = BN_is_prime_fasttest_ex(ret, 1, ctx, 0, cb);
125 j = BN_is_prime_fasttest_ex(t, 1, ctx, 0, cb);
131 if (!BN_GENCB_call(cb, 2, c1 - 1))
133 /* We have a safe prime test pass */
136 /* we have a prime :-) */
147 int BN_is_prime_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,
150 return BN_is_prime_fasttest_ex(a, checks, ctx_passed, 0, cb);
153 int BN_is_prime_fasttest_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,
154 int do_trial_division, BN_GENCB *cb)
159 BIGNUM *A1, *A1_odd, *check; /* taken from ctx */
160 BN_MONT_CTX *mont = NULL;
161 const BIGNUM *A = NULL;
163 if (BN_cmp(a, BN_value_one()) <= 0)
166 if (checks == BN_prime_checks)
167 checks = BN_prime_checks_for_size(BN_num_bits(a));
169 /* first look for small factors */
171 /* a is even => a is prime if and only if a == 2 */
172 return BN_is_word(a, 2);
173 if (do_trial_division) {
174 for (i = 1; i < NUMPRIMES; i++) {
175 BN_ULONG mod = BN_mod_word(a, primes[i]);
176 if (mod == (BN_ULONG)-1)
179 return BN_is_word(a, primes[i]);
181 if (!BN_GENCB_call(cb, 1, -1))
185 if (ctx_passed != NULL)
187 else if ((ctx = BN_CTX_new()) == NULL)
194 if ((t = BN_CTX_get(ctx)) == NULL)
196 if (BN_copy(t, a) == NULL)
202 A1 = BN_CTX_get(ctx);
203 A1_odd = BN_CTX_get(ctx);
204 check = BN_CTX_get(ctx);
208 /* compute A1 := A - 1 */
211 if (!BN_sub_word(A1, 1))
213 if (BN_is_zero(A1)) {
218 /* write A1 as A1_odd * 2^k */
220 while (!BN_is_bit_set(A1, k))
222 if (!BN_rshift(A1_odd, A1, k))
225 /* Montgomery setup for computations mod A */
226 mont = BN_MONT_CTX_new();
229 if (!BN_MONT_CTX_set(mont, A, ctx))
232 for (i = 0; i < checks; i++) {
233 if (!BN_pseudo_rand_range(check, A1))
235 if (!BN_add_word(check, 1))
237 /* now 1 <= check < A */
239 j = witness(check, A, A1, A1_odd, k, ctx, mont);
246 if (!BN_GENCB_call(cb, 1, i))
253 if (ctx_passed == NULL)
256 BN_MONT_CTX_free(mont);
261 static int witness(BIGNUM *w, const BIGNUM *a, const BIGNUM *a1,
262 const BIGNUM *a1_odd, int k, BN_CTX *ctx,
265 if (!BN_mod_exp_mont(w, w, a1_odd, a, ctx, mont)) /* w := w^a1_odd mod a */
268 return 0; /* probably prime */
269 if (BN_cmp(w, a1) == 0)
270 return 0; /* w == -1 (mod a), 'a' is probably prime */
272 if (!BN_mod_mul(w, w, w, a, ctx)) /* w := w^2 mod a */
275 return 1; /* 'a' is composite, otherwise a previous 'w'
276 * would have been == -1 (mod 'a') */
277 if (BN_cmp(w, a1) == 0)
278 return 0; /* w == -1 (mod a), 'a' is probably prime */
281 * If we get here, 'w' is the (a-1)/2-th power of the original 'w', and
282 * it is neither -1 nor +1 -- so 'a' cannot be prime
288 static int probable_prime(BIGNUM *rnd, int bits, prime_t *mods)
292 BN_ULONG maxdelta = BN_MASK2 - primes[NUMPRIMES - 1];
293 char is_single_word = bits <= BN_BITS2;
296 if (!BN_rand(rnd, bits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ODD))
298 /* we now have a random number 'rnd' to test. */
299 for (i = 1; i < NUMPRIMES; i++) {
300 BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);
301 if (mod == (BN_ULONG)-1)
303 mods[i] = (prime_t) mod;
306 * If bits is so small that it fits into a single word then we
307 * additionally don't want to exceed that many bits.
309 if (is_single_word) {
312 if (bits == BN_BITS2) {
314 * Shifting by this much has undefined behaviour so we do it a
317 size_limit = ~((BN_ULONG)0) - BN_get_word(rnd);
319 size_limit = (((BN_ULONG)1) << bits) - BN_get_word(rnd) - 1;
321 if (size_limit < maxdelta)
322 maxdelta = size_limit;
326 if (is_single_word) {
327 BN_ULONG rnd_word = BN_get_word(rnd);
330 * In the case that the candidate prime is a single word then
332 * 1) It's greater than primes[i] because we shouldn't reject
333 * 3 as being a prime number because it's a multiple of
335 * 2) That it's not a multiple of a known prime. We don't
336 * check that rnd-1 is also coprime to all the known
337 * primes because there aren't many small primes where
340 for (i = 1; i < NUMPRIMES && primes[i] < rnd_word; i++) {
341 if ((mods[i] + delta) % primes[i] == 0) {
343 if (delta > maxdelta)
349 for (i = 1; i < NUMPRIMES; i++) {
351 * check that rnd is not a prime and also that gcd(rnd-1,primes)
352 * == 1 (except for 2)
354 if (((mods[i] + delta) % primes[i]) <= 1) {
356 if (delta > maxdelta)
362 if (!BN_add_word(rnd, delta))
364 if (BN_num_bits(rnd) != bits)
370 int bn_probable_prime_dh(BIGNUM *rnd, int bits,
371 const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx)
377 if ((t1 = BN_CTX_get(ctx)) == NULL)
380 if (!BN_rand(rnd, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))
383 /* we need ((rnd-rem) % add) == 0 */
385 if (!BN_mod(t1, rnd, add, ctx))
387 if (!BN_sub(rnd, rnd, t1))
390 if (!BN_add_word(rnd, 1))
393 if (!BN_add(rnd, rnd, rem))
397 /* we now have a random number 'rand' to test. */
400 for (i = 1; i < NUMPRIMES; i++) {
401 /* check that rnd is a prime */
402 BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);
403 if (mod == (BN_ULONG)-1)
406 if (!BN_add(rnd, rnd, add))
419 static int probable_prime_dh_safe(BIGNUM *p, int bits, const BIGNUM *padd,
420 const BIGNUM *rem, BN_CTX *ctx)
423 BIGNUM *t1, *qadd, *q;
427 t1 = BN_CTX_get(ctx);
429 qadd = BN_CTX_get(ctx);
433 if (!BN_rshift1(qadd, padd))
436 if (!BN_rand(q, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))
439 /* we need ((rnd-rem) % add) == 0 */
440 if (!BN_mod(t1, q, qadd, ctx))
442 if (!BN_sub(q, q, t1))
445 if (!BN_add_word(q, 1))
448 if (!BN_rshift1(t1, rem))
450 if (!BN_add(q, q, t1))
454 /* we now have a random number 'rand' to test. */
455 if (!BN_lshift1(p, q))
457 if (!BN_add_word(p, 1))
461 for (i = 1; i < NUMPRIMES; i++) {
462 /* check that p and q are prime */
464 * check that for p and q gcd(p-1,primes) == 1 (except for 2)
466 BN_ULONG pmod = BN_mod_word(p, (BN_ULONG)primes[i]);
467 BN_ULONG qmod = BN_mod_word(q, (BN_ULONG)primes[i]);
468 if (pmod == (BN_ULONG)-1 || qmod == (BN_ULONG)-1)
470 if (pmod == 0 || qmod == 0) {
471 if (!BN_add(p, p, padd))
473 if (!BN_add(q, q, qadd))