2 * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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 probable_prime(BIGNUM *rnd, int bits, prime_t *mods);
23 static int probable_prime_dh_safe(BIGNUM *rnd, int bits,
24 const BIGNUM *add, const BIGNUM *rem,
28 # define BN_DEF(lo, hi) (BN_ULONG)hi<<32|lo
30 # define BN_DEF(lo, hi) lo, hi
34 * See SP800 89 5.3.3 (Step f)
35 * The product of the set of primes ranging from 3 to 751
36 * Generated using process in test/bn_internal_test.c test_bn_small_factors().
37 * This includes 751 (which is not currently included in SP 800-89).
39 static const BN_ULONG small_prime_factors[] = {
40 BN_DEF(0x3ef4e3e1, 0xc4309333), BN_DEF(0xcd2d655f, 0x71161eb6),
41 BN_DEF(0x0bf94862, 0x95e2238c), BN_DEF(0x24f7912b, 0x3eb233d3),
42 BN_DEF(0xbf26c483, 0x6b55514b), BN_DEF(0x5a144871, 0x0a84d817),
43 BN_DEF(0x9b82210a, 0x77d12fee), BN_DEF(0x97f050b3, 0xdb5b93c2),
44 BN_DEF(0x4d6c026b, 0x4acad6b9), BN_DEF(0x54aec893, 0xeb7751f3),
45 BN_DEF(0x36bc85c4, 0xdba53368), BN_DEF(0x7f5ec78e, 0xd85a1b28),
46 BN_DEF(0x6b322244, 0x2eb072d8), BN_DEF(0x5e2b3aea, 0xbba51112),
47 BN_DEF(0x0e2486bf, 0x36ed1a6c), BN_DEF(0xec0c5727, 0x5f270460),
51 #define BN_SMALL_PRIME_FACTORS_TOP OSSL_NELEM(small_prime_factors)
52 static const BIGNUM _bignum_small_prime_factors = {
53 (BN_ULONG *)small_prime_factors,
54 BN_SMALL_PRIME_FACTORS_TOP,
55 BN_SMALL_PRIME_FACTORS_TOP,
60 const BIGNUM *bn_get0_small_factors(void)
62 return &_bignum_small_prime_factors;
65 int BN_GENCB_call(BN_GENCB *cb, int a, int b)
67 /* No callback means continue */
72 /* Deprecated-style callbacks */
75 cb->cb.cb_1(a, b, cb->arg);
78 /* New-style callbacks */
79 return cb->cb.cb_2(a, b, cb);
83 /* Unrecognised callback type */
87 int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe,
88 const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb)
95 int checks = BN_prime_checks_for_size(bits);
98 /* There are no prime numbers this small. */
99 BNerr(BN_F_BN_GENERATE_PRIME_EX, BN_R_BITS_TOO_SMALL);
101 } else if (bits == 2 && safe) {
102 /* The smallest safe prime (7) is three bits. */
103 BNerr(BN_F_BN_GENERATE_PRIME_EX, BN_R_BITS_TOO_SMALL);
107 mods = OPENSSL_zalloc(sizeof(*mods) * NUMPRIMES);
119 /* make a random number and set the top and bottom bits */
121 if (!probable_prime(ret, bits, mods))
125 if (!probable_prime_dh_safe(ret, bits, add, rem, ctx))
128 if (!bn_probable_prime_dh(ret, bits, add, rem, ctx))
133 if (!BN_GENCB_call(cb, 0, c1++))
138 i = BN_is_prime_fasttest_ex(ret, checks, ctx, 0, cb);
145 * for "safe prime" generation, check that (p-1)/2 is prime. Since a
146 * prime is odd, We just need to divide by 2
148 if (!BN_rshift1(t, ret))
151 for (i = 0; i < checks; i++) {
152 j = BN_is_prime_fasttest_ex(ret, 1, ctx, 0, cb);
158 j = BN_is_prime_fasttest_ex(t, 1, ctx, 0, cb);
164 if (!BN_GENCB_call(cb, 2, c1 - 1))
166 /* We have a safe prime test pass */
169 /* we have a prime :-) */
180 int BN_is_prime_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,
183 return BN_is_prime_fasttest_ex(a, checks, ctx_passed, 0, cb);
186 /* See FIPS 186-4 C.3.1 Miller Rabin Probabilistic Primality Test. */
187 int BN_is_prime_fasttest_ex(const BIGNUM *w, int checks, BN_CTX *ctx_passed,
188 int do_trial_division, BN_GENCB *cb)
190 int i, status, ret = -1;
193 /* w must be bigger than 1 */
194 if (BN_cmp(w, BN_value_one()) <= 0)
199 /* Take care of the really small prime 3 */
200 if (BN_is_word(w, 3))
203 /* 2 is the only even prime */
204 return BN_is_word(w, 2);
207 /* first look for small factors */
208 if (do_trial_division) {
209 for (i = 1; i < NUMPRIMES; i++) {
210 BN_ULONG mod = BN_mod_word(w, primes[i]);
211 if (mod == (BN_ULONG)-1)
214 return BN_is_word(w, primes[i]);
216 if (!BN_GENCB_call(cb, 1, -1))
219 if (ctx_passed != NULL)
221 else if ((ctx = BN_CTX_new()) == NULL)
224 ret = bn_miller_rabin_is_prime(w, checks, ctx, cb, 0, &status);
227 ret = (status == BN_PRIMETEST_PROBABLY_PRIME);
229 if (ctx_passed == NULL)
235 * Refer to FIPS 186-4 C.3.2 Enhanced Miller-Rabin Probabilistic Primality Test.
236 * OR C.3.1 Miller-Rabin Probabilistic Primality Test (if enhanced is zero).
237 * The Step numbers listed in the code refer to the enhanced case.
239 * if enhanced is set, then status returns one of the following:
240 * BN_PRIMETEST_PROBABLY_PRIME
241 * BN_PRIMETEST_COMPOSITE_WITH_FACTOR
242 * BN_PRIMETEST_COMPOSITE_NOT_POWER_OF_PRIME
243 * if enhanced is zero, then status returns either
244 * BN_PRIMETEST_PROBABLY_PRIME or
245 * BN_PRIMETEST_COMPOSITE
247 * returns 0 if there was an error, otherwise it returns 1.
249 int bn_miller_rabin_is_prime(const BIGNUM *w, int iterations, BN_CTX *ctx,
250 BN_GENCB *cb, int enhanced, int *status)
252 int i, j, a, ret = 0;
253 BIGNUM *g, *w1, *w3, *x, *m, *z, *b;
254 BN_MONT_CTX *mont = NULL;
262 w1 = BN_CTX_get(ctx);
263 w3 = BN_CTX_get(ctx);
272 && BN_sub_word(w1, 1)
275 && BN_sub_word(w3, 3)))
278 /* check w is larger than 3, otherwise the random b will be too small */
279 if (BN_is_zero(w3) || BN_is_negative(w3))
282 /* (Step 1) Calculate largest integer 'a' such that 2^a divides w-1 */
284 while (!BN_is_bit_set(w1, a))
286 /* (Step 2) m = (w-1) / 2^a */
287 if (!BN_rshift(m, w1, a))
290 /* Montgomery setup for computations mod a */
291 mont = BN_MONT_CTX_new();
292 if (mont == NULL || !BN_MONT_CTX_set(mont, w, ctx))
295 if (iterations == BN_prime_checks)
296 iterations = BN_prime_checks_for_size(BN_num_bits(w));
299 for (i = 0; i < iterations; ++i) {
300 /* (Step 4.1) obtain a Random string of bits b where 1 < b < w-1 */
301 if (!BN_priv_rand_range(b, w3) || !BN_add_word(b, 2)) /* 1 < b < w-1 */
306 if (!BN_gcd(g, b, w, ctx))
310 *status = BN_PRIMETEST_COMPOSITE_WITH_FACTOR;
315 /* (Step 4.5) z = b^m mod w */
316 if (!BN_mod_exp_mont(z, b, m, w, ctx, mont))
318 /* (Step 4.6) if (z = 1 or z = w-1) */
319 if (BN_is_one(z) || BN_cmp(z, w1) == 0)
321 /* (Step 4.7) for j = 1 to a-1 */
322 for (j = 1; j < a ; ++j) {
323 /* (Step 4.7.1 - 4.7.2) x = z. z = x^2 mod w */
324 if (!BN_copy(x, z) || !BN_mod_mul(z, x, x, w, ctx))
327 if (BN_cmp(z, w1) == 0)
333 if (!BN_GENCB_call(cb, 1, i))
335 /* At this point z = b^((w-1)/2) mod w */
336 /* (Steps 4.8 - 4.9) x = z, z = x^2 mod w */
337 if (!BN_copy(x, z) || !BN_mod_mul(z, x, x, w, ctx))
342 /* (Step 4.11) x = b^(w-1) mod w */
347 /* (Step 4.1.2) g = GCD(x-1, w) */
348 if (!BN_sub_word(x, 1) || !BN_gcd(g, x, w, ctx))
350 /* (Steps 4.1.3 - 4.1.4) */
352 *status = BN_PRIMETEST_COMPOSITE_NOT_POWER_OF_PRIME;
354 *status = BN_PRIMETEST_COMPOSITE_WITH_FACTOR;
356 *status = BN_PRIMETEST_COMPOSITE;
364 *status = BN_PRIMETEST_PROBABLY_PRIME;
375 BN_MONT_CTX_free(mont);
379 static int probable_prime(BIGNUM *rnd, int bits, prime_t *mods)
383 BN_ULONG maxdelta = BN_MASK2 - primes[NUMPRIMES - 1];
384 char is_single_word = bits <= BN_BITS2;
387 /* TODO: Not all primes are private */
388 if (!BN_priv_rand(rnd, bits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ODD))
390 /* we now have a random number 'rnd' to test. */
391 for (i = 1; i < NUMPRIMES; i++) {
392 BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);
393 if (mod == (BN_ULONG)-1)
395 mods[i] = (prime_t) mod;
398 * If bits is so small that it fits into a single word then we
399 * additionally don't want to exceed that many bits.
401 if (is_single_word) {
404 if (bits == BN_BITS2) {
406 * Shifting by this much has undefined behaviour so we do it a
409 size_limit = ~((BN_ULONG)0) - BN_get_word(rnd);
411 size_limit = (((BN_ULONG)1) << bits) - BN_get_word(rnd) - 1;
413 if (size_limit < maxdelta)
414 maxdelta = size_limit;
418 if (is_single_word) {
419 BN_ULONG rnd_word = BN_get_word(rnd);
422 * In the case that the candidate prime is a single word then
424 * 1) It's greater than primes[i] because we shouldn't reject
425 * 3 as being a prime number because it's a multiple of
427 * 2) That it's not a multiple of a known prime. We don't
428 * check that rnd-1 is also coprime to all the known
429 * primes because there aren't many small primes where
432 for (i = 1; i < NUMPRIMES && primes[i] < rnd_word; i++) {
433 if ((mods[i] + delta) % primes[i] == 0) {
435 if (delta > maxdelta)
441 for (i = 1; i < NUMPRIMES; i++) {
443 * check that rnd is not a prime and also that gcd(rnd-1,primes)
444 * == 1 (except for 2)
446 if (((mods[i] + delta) % primes[i]) <= 1) {
448 if (delta > maxdelta)
454 if (!BN_add_word(rnd, delta))
456 if (BN_num_bits(rnd) != bits)
462 int bn_probable_prime_dh(BIGNUM *rnd, int bits,
463 const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx)
469 if ((t1 = BN_CTX_get(ctx)) == NULL)
472 if (!BN_rand(rnd, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))
475 /* we need ((rnd-rem) % add) == 0 */
477 if (!BN_mod(t1, rnd, add, ctx))
479 if (!BN_sub(rnd, rnd, t1))
482 if (!BN_add_word(rnd, 1))
485 if (!BN_add(rnd, rnd, rem))
489 /* we now have a random number 'rand' to test. */
492 for (i = 1; i < NUMPRIMES; i++) {
493 /* check that rnd is a prime */
494 BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);
495 if (mod == (BN_ULONG)-1)
498 if (!BN_add(rnd, rnd, add))
511 static int probable_prime_dh_safe(BIGNUM *p, int bits, const BIGNUM *padd,
512 const BIGNUM *rem, BN_CTX *ctx)
515 BIGNUM *t1, *qadd, *q;
519 t1 = BN_CTX_get(ctx);
521 qadd = BN_CTX_get(ctx);
525 if (!BN_rshift1(qadd, padd))
528 if (!BN_rand(q, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))
531 /* we need ((rnd-rem) % add) == 0 */
532 if (!BN_mod(t1, q, qadd, ctx))
534 if (!BN_sub(q, q, t1))
537 if (!BN_add_word(q, 1))
540 if (!BN_rshift1(t1, rem))
542 if (!BN_add(q, q, t1))
546 /* we now have a random number 'rand' to test. */
547 if (!BN_lshift1(p, q))
549 if (!BN_add_word(p, 1))
553 for (i = 1; i < NUMPRIMES; i++) {
554 /* check that p and q are prime */
556 * check that for p and q gcd(p-1,primes) == 1 (except for 2)
558 BN_ULONG pmod = BN_mod_word(p, (BN_ULONG)primes[i]);
559 BN_ULONG qmod = BN_mod_word(q, (BN_ULONG)primes[i]);
560 if (pmod == (BN_ULONG)-1 || qmod == (BN_ULONG)-1)
562 if (pmod == 0 || qmod == 0) {
563 if (!BN_add(p, p, padd))
565 if (!BN_add(q, q, qadd))