2 * Copyright 1995-2017 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, prime_t *mods);
26 static int probable_prime_dh_safe(BIGNUM *rnd, int bits,
27 const BIGNUM *add, const BIGNUM *rem,
30 int BN_GENCB_call(BN_GENCB *cb, int a, int b)
32 /* No callback means continue */
37 /* Deprecated-style callbacks */
40 cb->cb.cb_1(a, b, cb->arg);
43 /* New-style callbacks */
44 return cb->cb.cb_2(a, b, cb);
48 /* Unrecognised callback type */
52 int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe,
53 const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb)
60 int checks = BN_prime_checks_for_size(bits);
63 /* There are no prime numbers this small. */
64 BNerr(BN_F_BN_GENERATE_PRIME_EX, BN_R_BITS_TOO_SMALL);
66 } else if (bits == 2 && safe) {
67 /* The smallest safe prime (7) is three bits. */
68 BNerr(BN_F_BN_GENERATE_PRIME_EX, BN_R_BITS_TOO_SMALL);
72 mods = OPENSSL_zalloc(sizeof(*mods) * NUMPRIMES);
84 /* make a random number and set the top and bottom bits */
86 if (!probable_prime(ret, bits, mods))
90 if (!probable_prime_dh_safe(ret, bits, add, rem, ctx))
93 if (!bn_probable_prime_dh(ret, bits, add, rem, ctx))
98 if (!BN_GENCB_call(cb, 0, c1++))
103 i = BN_is_prime_fasttest_ex(ret, checks, ctx, 0, cb);
110 * for "safe prime" generation, check that (p-1)/2 is prime. Since a
111 * prime is odd, We just need to divide by 2
113 if (!BN_rshift1(t, ret))
116 for (i = 0; i < checks; i++) {
117 j = BN_is_prime_fasttest_ex(ret, 1, ctx, 0, cb);
123 j = BN_is_prime_fasttest_ex(t, 1, ctx, 0, cb);
129 if (!BN_GENCB_call(cb, 2, c1 - 1))
131 /* We have a safe prime test pass */
134 /* 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, *check; /* taken from ctx */
158 BN_MONT_CTX *mont = NULL;
160 if (BN_cmp(a, BN_value_one()) <= 0)
163 if (checks == BN_prime_checks)
164 checks = BN_prime_checks_for_size(BN_num_bits(a));
166 /* first look for small factors */
168 /* a is even => a is prime if and only if a == 2 */
169 return BN_is_word(a, 2);
170 if (do_trial_division) {
171 for (i = 1; i < NUMPRIMES; i++) {
172 BN_ULONG mod = BN_mod_word(a, primes[i]);
173 if (mod == (BN_ULONG)-1)
176 return BN_is_word(a, primes[i]);
178 if (!BN_GENCB_call(cb, 1, -1))
182 if (ctx_passed != NULL)
184 else if ((ctx = BN_CTX_new()) == NULL)
188 A1 = BN_CTX_get(ctx);
189 A1_odd = BN_CTX_get(ctx);
190 check = BN_CTX_get(ctx);
194 /* compute A1 := a - 1 */
197 if (!BN_sub_word(A1, 1))
199 if (BN_is_zero(A1)) {
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 if (!BN_priv_rand_range(check, A1))
221 if (!BN_add_word(check, 1))
223 /* now 1 <= check < a */
225 j = witness(check, a, A1, A1_odd, k, ctx, mont);
232 if (!BN_GENCB_call(cb, 1, i))
239 if (ctx_passed == NULL)
242 BN_MONT_CTX_free(mont);
247 static int witness(BIGNUM *w, const BIGNUM *a, const BIGNUM *a1,
248 const BIGNUM *a1_odd, int k, BN_CTX *ctx,
251 if (!BN_mod_exp_mont(w, w, a1_odd, a, ctx, mont)) /* w := w^a1_odd mod a */
254 return 0; /* probably prime */
255 if (BN_cmp(w, a1) == 0)
256 return 0; /* w == -1 (mod a), 'a' is probably prime */
258 if (!BN_mod_mul(w, w, w, a, ctx)) /* w := w^2 mod a */
261 return 1; /* 'a' is composite, otherwise a previous 'w'
262 * would have been == -1 (mod 'a') */
263 if (BN_cmp(w, a1) == 0)
264 return 0; /* w == -1 (mod a), 'a' is probably prime */
267 * If we get here, 'w' is the (a-1)/2-th power of the original 'w', and
268 * it is neither -1 nor +1 -- so 'a' cannot be prime
274 static int probable_prime(BIGNUM *rnd, int bits, prime_t *mods)
278 BN_ULONG maxdelta = BN_MASK2 - primes[NUMPRIMES - 1];
279 char is_single_word = bits <= BN_BITS2;
282 /* TODO: Not all primes are private */
283 if (!BN_priv_rand(rnd, bits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ODD))
285 /* we now have a random number 'rnd' to test. */
286 for (i = 1; i < NUMPRIMES; i++) {
287 BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);
288 if (mod == (BN_ULONG)-1)
290 mods[i] = (prime_t) mod;
293 * If bits is so small that it fits into a single word then we
294 * additionally don't want to exceed that many bits.
296 if (is_single_word) {
299 if (bits == BN_BITS2) {
301 * Shifting by this much has undefined behaviour so we do it a
304 size_limit = ~((BN_ULONG)0) - BN_get_word(rnd);
306 size_limit = (((BN_ULONG)1) << bits) - BN_get_word(rnd) - 1;
308 if (size_limit < maxdelta)
309 maxdelta = size_limit;
313 if (is_single_word) {
314 BN_ULONG rnd_word = BN_get_word(rnd);
317 * In the case that the candidate prime is a single word then
319 * 1) It's greater than primes[i] because we shouldn't reject
320 * 3 as being a prime number because it's a multiple of
322 * 2) That it's not a multiple of a known prime. We don't
323 * check that rnd-1 is also coprime to all the known
324 * primes because there aren't many small primes where
327 for (i = 1; i < NUMPRIMES && primes[i] < rnd_word; i++) {
328 if ((mods[i] + delta) % primes[i] == 0) {
330 if (delta > maxdelta)
336 for (i = 1; i < NUMPRIMES; i++) {
338 * check that rnd is not a prime and also that gcd(rnd-1,primes)
339 * == 1 (except for 2)
341 if (((mods[i] + delta) % primes[i]) <= 1) {
343 if (delta > maxdelta)
349 if (!BN_add_word(rnd, delta))
351 if (BN_num_bits(rnd) != bits)
357 int bn_probable_prime_dh(BIGNUM *rnd, int bits,
358 const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx)
364 if ((t1 = BN_CTX_get(ctx)) == NULL)
367 if (!BN_rand(rnd, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))
370 /* we need ((rnd-rem) % add) == 0 */
372 if (!BN_mod(t1, rnd, add, ctx))
374 if (!BN_sub(rnd, rnd, t1))
377 if (!BN_add_word(rnd, 1))
380 if (!BN_add(rnd, rnd, rem))
384 /* we now have a random number 'rand' to test. */
387 for (i = 1; i < NUMPRIMES; i++) {
388 /* check that rnd is a prime */
389 BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);
390 if (mod == (BN_ULONG)-1)
393 if (!BN_add(rnd, rnd, add))
406 static int probable_prime_dh_safe(BIGNUM *p, int bits, const BIGNUM *padd,
407 const BIGNUM *rem, BN_CTX *ctx)
410 BIGNUM *t1, *qadd, *q;
414 t1 = BN_CTX_get(ctx);
416 qadd = BN_CTX_get(ctx);
420 if (!BN_rshift1(qadd, padd))
423 if (!BN_rand(q, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))
426 /* we need ((rnd-rem) % add) == 0 */
427 if (!BN_mod(t1, q, qadd, ctx))
429 if (!BN_sub(q, q, t1))
432 if (!BN_add_word(q, 1))
435 if (!BN_rshift1(t1, rem))
437 if (!BN_add(q, q, t1))
441 /* we now have a random number 'rand' to test. */
442 if (!BN_lshift1(p, q))
444 if (!BN_add_word(p, 1))
448 for (i = 1; i < NUMPRIMES; i++) {
449 /* check that p and q are prime */
451 * check that for p and q gcd(p-1,primes) == 1 (except for 2)
453 BN_ULONG pmod = BN_mod_word(p, (BN_ULONG)primes[i]);
454 BN_ULONG qmod = BN_mod_word(q, (BN_ULONG)primes[i]);
455 if (pmod == (BN_ULONG)-1 || qmod == (BN_ULONG)-1)
457 if (pmod == 0 || qmod == 0) {
458 if (!BN_add(p, p, padd))
460 if (!BN_add(q, q, qadd))