2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #include "internal/cryptlib.h"
16 * The quick sieve algorithm approach to weeding out primes is Philip
17 * Zimmermann's, as implemented in PGP. I have had a read of his comments
18 * and implemented my own version.
22 static int witness(BIGNUM *w, const BIGNUM *a, const BIGNUM *a1,
23 const BIGNUM *a1_odd, int k, BN_CTX *ctx,
25 static int probable_prime(BIGNUM *rnd, int bits, int safe, prime_t *mods);
26 static int probable_prime_dh(BIGNUM *rnd, int bits, int safe, prime_t *mods,
27 const BIGNUM *add, const BIGNUM *rem,
30 #define square(x) ((BN_ULONG)(x) * (BN_ULONG)(x))
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 (add == NULL && safe && bits < 6 && bits != 3) {
70 * The smallest safe prime (7) is three bits.
71 * But the following two safe primes with less than 6 bits (11, 23)
72 * are unreachable for BN_rand with BN_RAND_TOP_TWO.
74 BNerr(BN_F_BN_GENERATE_PRIME_EX, BN_R_BITS_TOO_SMALL);
78 mods = OPENSSL_zalloc(sizeof(*mods) * NUMPRIMES);
90 /* make a random number and set the top and bottom bits */
92 if (!probable_prime(ret, bits, safe, mods))
95 if (!probable_prime_dh(ret, bits, safe, mods, add, rem, ctx))
99 if (!BN_GENCB_call(cb, 0, c1++))
104 i = BN_is_prime_fasttest_ex(ret, checks, ctx, 0, cb);
111 * for "safe prime" generation, check that (p-1)/2 is prime. Since a
112 * prime is odd, We just need to divide by 2
114 if (!BN_rshift1(t, ret))
117 for (i = 0; i < checks; i++) {
118 j = BN_is_prime_fasttest_ex(ret, 1, ctx, 0, cb);
124 j = BN_is_prime_fasttest_ex(t, 1, ctx, 0, cb);
130 if (!BN_GENCB_call(cb, 2, c1 - 1))
132 /* We have a safe prime test pass */
135 /* we have a prime :-) */
145 int BN_is_prime_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,
148 return BN_is_prime_fasttest_ex(a, checks, ctx_passed, 0, cb);
151 int BN_is_prime_fasttest_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,
152 int do_trial_division, BN_GENCB *cb)
157 BIGNUM *A1, *A1_odd, *A3, *check; /* taken from ctx */
158 BN_MONT_CTX *mont = NULL;
160 /* Take care of the really small primes 2 & 3 */
161 if (BN_is_word(a, 2) || BN_is_word(a, 3))
164 /* Check odd and bigger than 1 */
165 if (!BN_is_odd(a) || BN_cmp(a, BN_value_one()) <= 0)
168 if (checks == BN_prime_checks)
169 checks = BN_prime_checks_for_size(BN_num_bits(a));
171 /* first look for small factors */
172 if (do_trial_division) {
173 for (i = 1; i < NUMPRIMES; i++) {
174 BN_ULONG mod = BN_mod_word(a, primes[i]);
175 if (mod == (BN_ULONG)-1)
178 return BN_is_word(a, primes[i]);
180 if (!BN_GENCB_call(cb, 1, -1))
184 if (ctx_passed != NULL)
186 else if ((ctx = BN_CTX_new()) == NULL)
190 A1 = BN_CTX_get(ctx);
191 A3 = BN_CTX_get(ctx);
192 A1_odd = BN_CTX_get(ctx);
193 check = BN_CTX_get(ctx);
197 /* compute A1 := a - 1 */
198 if (!BN_copy(A1, a) || !BN_sub_word(A1, 1))
200 /* compute A3 := a - 3 */
201 if (!BN_copy(A3, a) || !BN_sub_word(A3, 3))
204 /* write A1 as A1_odd * 2^k */
206 while (!BN_is_bit_set(A1, k))
208 if (!BN_rshift(A1_odd, A1, k))
211 /* Montgomery setup for computations mod a */
212 mont = BN_MONT_CTX_new();
215 if (!BN_MONT_CTX_set(mont, a, ctx))
218 for (i = 0; i < checks; i++) {
219 /* 1 < check < a-1 */
220 if (!BN_priv_rand_range(check, A3) || !BN_add_word(check, 2))
223 j = witness(check, a, A1, A1_odd, k, ctx, mont);
230 if (!BN_GENCB_call(cb, 1, i))
237 if (ctx_passed == NULL)
240 BN_MONT_CTX_free(mont);
245 static int witness(BIGNUM *w, const BIGNUM *a, const BIGNUM *a1,
246 const BIGNUM *a1_odd, int k, BN_CTX *ctx,
249 if (!BN_mod_exp_mont(w, w, a1_odd, a, ctx, mont)) /* w := w^a1_odd mod a */
252 return 0; /* probably prime */
253 if (BN_cmp(w, a1) == 0)
254 return 0; /* w == -1 (mod a), 'a' is probably prime */
256 if (!BN_mod_mul(w, w, w, a, ctx)) /* w := w^2 mod a */
259 return 1; /* 'a' is composite, otherwise a previous 'w'
260 * would have been == -1 (mod 'a') */
261 if (BN_cmp(w, a1) == 0)
262 return 0; /* w == -1 (mod a), 'a' is probably prime */
265 * If we get here, 'w' is the (a-1)/2-th power of the original 'w', and
266 * it is neither -1 nor +1 -- so 'a' cannot be prime
272 static int probable_prime(BIGNUM *rnd, int bits, int safe, prime_t *mods)
276 BN_ULONG maxdelta = BN_MASK2 - primes[NUMPRIMES - 1];
279 /* TODO: Not all primes are private */
280 if (!BN_priv_rand(rnd, bits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ODD))
282 if (safe && !BN_set_bit(rnd, 1))
284 /* we now have a random number 'rnd' to test. */
285 for (i = 1; i < NUMPRIMES; i++) {
286 BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);
287 if (mod == (BN_ULONG)-1)
289 mods[i] = (prime_t) mod;
293 for (i = 1; i < NUMPRIMES; i++) {
295 * check that rnd is a prime and also that
296 * gcd(rnd-1,primes) == 1 (except for 2)
297 * do the second check only if we are interested in safe primes
298 * in the case that the candidate prime is a single word then
299 * we check only the primes up to sqrt(rnd)
301 if (bits <= 31 && delta <= 0x7fffffff
302 && square(primes[i]) > BN_get_word(rnd) + delta)
304 if (safe ? (mods[i] + delta) % primes[i] <= 1
305 : (mods[i] + delta) % primes[i] == 0) {
306 delta += safe ? 4 : 2;
307 if (delta > maxdelta)
312 if (!BN_add_word(rnd, delta))
314 if (BN_num_bits(rnd) != bits)
320 static int probable_prime_dh(BIGNUM *rnd, int bits, int safe, prime_t *mods,
321 const BIGNUM *add, const BIGNUM *rem,
327 BN_ULONG maxdelta = BN_MASK2 - primes[NUMPRIMES - 1];
330 if ((t1 = BN_CTX_get(ctx)) == NULL)
333 if (maxdelta > BN_MASK2 - BN_get_word(add))
334 maxdelta = BN_MASK2 - BN_get_word(add);
337 if (!BN_rand(rnd, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))
340 /* we need ((rnd-rem) % add) == 0 */
342 if (!BN_mod(t1, rnd, add, ctx))
344 if (!BN_sub(rnd, rnd, t1))
347 if (!BN_add_word(rnd, safe ? 3u : 1u))
350 if (!BN_add(rnd, rnd, rem))
354 if (BN_num_bits(rnd) < bits
355 || BN_get_word(rnd) < (safe ? 5u : 3u)) {
356 if (!BN_add(rnd, rnd, add))
360 /* we now have a random number 'rnd' to test. */
361 for (i = 1; i < NUMPRIMES; i++) {
362 BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);
363 if (mod == (BN_ULONG)-1)
365 mods[i] = (prime_t) mod;
369 for (i = 1; i < NUMPRIMES; i++) {
370 /* check that rnd is a prime */
371 if (bits <= 31 && delta <= 0x7fffffff
372 && square(primes[i]) > BN_get_word(rnd) + delta)
374 /* rnd mod p == 1 implies q = (rnd-1)/2 is divisible by p */
375 if (safe ? (mods[i] + delta) % primes[i] <= 1
376 : (mods[i] + delta) % primes[i] == 0) {
377 delta += BN_get_word(add);
378 if (delta > maxdelta)
383 if (!BN_add_word(rnd, delta))