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, BN_CTX *ctx);
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_ex2(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_EX2, BN_R_BITS_TOO_SMALL);
101 } else if (add == NULL && safe && bits < 6 && bits != 3) {
103 * The smallest safe prime (7) is three bits.
104 * But the following two safe primes with less than 6 bits (11, 23)
105 * are unreachable for BN_rand with BN_RAND_TOP_TWO.
107 BNerr(BN_F_BN_GENERATE_PRIME_EX2, BN_R_BITS_TOO_SMALL);
111 mods = OPENSSL_zalloc(sizeof(*mods) * NUMPRIMES);
120 /* make a random number and set the top and bottom bits */
122 if (!probable_prime(ret, bits, mods, ctx))
126 if (!probable_prime_dh_safe(ret, bits, add, rem, ctx))
129 if (!bn_probable_prime_dh(ret, bits, add, rem, ctx))
134 if (!BN_GENCB_call(cb, 0, c1++))
139 i = BN_is_prime_fasttest_ex(ret, checks, ctx, 0, cb);
146 * for "safe prime" generation, check that (p-1)/2 is prime. Since a
147 * prime is odd, We just need to divide by 2
149 if (!BN_rshift1(t, ret))
152 for (i = 0; i < checks; i++) {
153 j = BN_is_prime_fasttest_ex(ret, 1, ctx, 0, cb);
159 j = BN_is_prime_fasttest_ex(t, 1, ctx, 0, cb);
165 if (!BN_GENCB_call(cb, 2, c1 - 1))
167 /* We have a safe prime test pass */
170 /* we have a prime :-) */
180 int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe,
181 const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb)
183 BN_CTX *ctx = BN_CTX_new();
189 retval = BN_generate_prime_ex2(ret, bits, safe, add, rem, cb, ctx);
196 int BN_is_prime_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,
199 return BN_is_prime_fasttest_ex(a, checks, ctx_passed, 0, cb);
202 /* See FIPS 186-4 C.3.1 Miller Rabin Probabilistic Primality Test. */
203 int BN_is_prime_fasttest_ex(const BIGNUM *w, int checks, BN_CTX *ctx,
204 int do_trial_division, BN_GENCB *cb)
206 int i, status, ret = -1;
208 BN_CTX *ctxlocal = NULL;
215 /* w must be bigger than 1 */
216 if (BN_cmp(w, BN_value_one()) <= 0)
221 /* Take care of the really small prime 3 */
222 if (BN_is_word(w, 3))
225 /* 2 is the only even prime */
226 return BN_is_word(w, 2);
229 /* first look for small factors */
230 if (do_trial_division) {
231 for (i = 1; i < NUMPRIMES; i++) {
232 BN_ULONG mod = BN_mod_word(w, primes[i]);
233 if (mod == (BN_ULONG)-1)
236 return BN_is_word(w, primes[i]);
238 if (!BN_GENCB_call(cb, 1, -1))
242 if (ctx == NULL && (ctxlocal = ctx = BN_CTX_new()) == NULL)
246 ret = bn_miller_rabin_is_prime(w, checks, ctx, cb, 0, &status);
249 ret = (status == BN_PRIMETEST_PROBABLY_PRIME);
252 BN_CTX_free(ctxlocal);
258 * Refer to FIPS 186-4 C.3.2 Enhanced Miller-Rabin Probabilistic Primality Test.
259 * OR C.3.1 Miller-Rabin Probabilistic Primality Test (if enhanced is zero).
260 * The Step numbers listed in the code refer to the enhanced case.
262 * if enhanced is set, then status returns one of the following:
263 * BN_PRIMETEST_PROBABLY_PRIME
264 * BN_PRIMETEST_COMPOSITE_WITH_FACTOR
265 * BN_PRIMETEST_COMPOSITE_NOT_POWER_OF_PRIME
266 * if enhanced is zero, then status returns either
267 * BN_PRIMETEST_PROBABLY_PRIME or
268 * BN_PRIMETEST_COMPOSITE
270 * returns 0 if there was an error, otherwise it returns 1.
272 int bn_miller_rabin_is_prime(const BIGNUM *w, int iterations, BN_CTX *ctx,
273 BN_GENCB *cb, int enhanced, int *status)
275 int i, j, a, ret = 0;
276 BIGNUM *g, *w1, *w3, *x, *m, *z, *b;
277 BN_MONT_CTX *mont = NULL;
285 w1 = BN_CTX_get(ctx);
286 w3 = BN_CTX_get(ctx);
295 && BN_sub_word(w1, 1)
298 && BN_sub_word(w3, 3)))
301 /* check w is larger than 3, otherwise the random b will be too small */
302 if (BN_is_zero(w3) || BN_is_negative(w3))
305 /* (Step 1) Calculate largest integer 'a' such that 2^a divides w-1 */
307 while (!BN_is_bit_set(w1, a))
309 /* (Step 2) m = (w-1) / 2^a */
310 if (!BN_rshift(m, w1, a))
313 /* Montgomery setup for computations mod a */
314 mont = BN_MONT_CTX_new();
315 if (mont == NULL || !BN_MONT_CTX_set(mont, w, ctx))
318 if (iterations == BN_prime_checks)
319 iterations = BN_prime_checks_for_size(BN_num_bits(w));
322 for (i = 0; i < iterations; ++i) {
323 /* (Step 4.1) obtain a Random string of bits b where 1 < b < w-1 */
324 if (!BN_priv_rand_range_ex(b, w3, ctx)
325 || !BN_add_word(b, 2)) /* 1 < b < w-1 */
330 if (!BN_gcd(g, b, w, ctx))
334 *status = BN_PRIMETEST_COMPOSITE_WITH_FACTOR;
339 /* (Step 4.5) z = b^m mod w */
340 if (!BN_mod_exp_mont(z, b, m, w, ctx, mont))
342 /* (Step 4.6) if (z = 1 or z = w-1) */
343 if (BN_is_one(z) || BN_cmp(z, w1) == 0)
345 /* (Step 4.7) for j = 1 to a-1 */
346 for (j = 1; j < a ; ++j) {
347 /* (Step 4.7.1 - 4.7.2) x = z. z = x^2 mod w */
348 if (!BN_copy(x, z) || !BN_mod_mul(z, x, x, w, ctx))
351 if (BN_cmp(z, w1) == 0)
357 /* At this point z = b^((w-1)/2) mod w */
358 /* (Steps 4.8 - 4.9) x = z, z = x^2 mod w */
359 if (!BN_copy(x, z) || !BN_mod_mul(z, x, x, w, ctx))
364 /* (Step 4.11) x = b^(w-1) mod w */
369 /* (Step 4.1.2) g = GCD(x-1, w) */
370 if (!BN_sub_word(x, 1) || !BN_gcd(g, x, w, ctx))
372 /* (Steps 4.1.3 - 4.1.4) */
374 *status = BN_PRIMETEST_COMPOSITE_NOT_POWER_OF_PRIME;
376 *status = BN_PRIMETEST_COMPOSITE_WITH_FACTOR;
378 *status = BN_PRIMETEST_COMPOSITE;
384 if (!BN_GENCB_call(cb, 1, i))
388 *status = BN_PRIMETEST_PROBABLY_PRIME;
399 BN_MONT_CTX_free(mont);
403 static int probable_prime(BIGNUM *rnd, int bits, prime_t *mods, BN_CTX *ctx)
407 BN_ULONG maxdelta = BN_MASK2 - primes[NUMPRIMES - 1];
408 char is_single_word = bits <= BN_BITS2;
411 /* TODO: Not all primes are private */
412 if (!BN_priv_rand_ex(rnd, bits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ODD, ctx))
414 /* we now have a random number 'rnd' to test. */
415 for (i = 1; i < NUMPRIMES; i++) {
416 BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);
417 if (mod == (BN_ULONG)-1)
419 mods[i] = (prime_t) mod;
422 * If bits is so small that it fits into a single word then we
423 * additionally don't want to exceed that many bits.
425 if (is_single_word) {
428 if (bits == BN_BITS2) {
430 * Shifting by this much has undefined behaviour so we do it a
433 size_limit = ~((BN_ULONG)0) - BN_get_word(rnd);
435 size_limit = (((BN_ULONG)1) << bits) - BN_get_word(rnd) - 1;
437 if (size_limit < maxdelta)
438 maxdelta = size_limit;
442 if (is_single_word) {
443 BN_ULONG rnd_word = BN_get_word(rnd);
446 * In the case that the candidate prime is a single word then
448 * 1) It's greater than primes[i] because we shouldn't reject
449 * 3 as being a prime number because it's a multiple of
451 * 2) That it's not a multiple of a known prime. We don't
452 * check that rnd-1 is also coprime to all the known
453 * primes because there aren't many small primes where
456 for (i = 1; i < NUMPRIMES && primes[i] < rnd_word; i++) {
457 if ((mods[i] + delta) % primes[i] == 0) {
459 if (delta > maxdelta)
465 for (i = 1; i < NUMPRIMES; i++) {
467 * check that rnd is not a prime and also that gcd(rnd-1,primes)
468 * == 1 (except for 2)
470 if (((mods[i] + delta) % primes[i]) <= 1) {
472 if (delta > maxdelta)
478 if (!BN_add_word(rnd, delta))
480 if (BN_num_bits(rnd) != bits)
486 int bn_probable_prime_dh(BIGNUM *rnd, int bits,
487 const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx)
493 if ((t1 = BN_CTX_get(ctx)) == NULL)
496 if (!BN_rand_ex(rnd, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD, ctx))
499 /* we need ((rnd-rem) % add) == 0 */
501 if (!BN_mod(t1, rnd, add, ctx))
503 if (!BN_sub(rnd, rnd, t1))
506 if (!BN_add_word(rnd, 1))
509 if (!BN_add(rnd, rnd, rem))
513 /* we now have a random number 'rand' to test. */
516 for (i = 1; i < NUMPRIMES; i++) {
517 /* check that rnd is a prime */
518 BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);
519 if (mod == (BN_ULONG)-1)
522 if (!BN_add(rnd, rnd, add))
535 static int probable_prime_dh_safe(BIGNUM *p, int bits, const BIGNUM *padd,
536 const BIGNUM *rem, BN_CTX *ctx)
539 BIGNUM *t1, *qadd, *q;
543 t1 = BN_CTX_get(ctx);
545 qadd = BN_CTX_get(ctx);
549 if (!BN_rshift1(qadd, padd))
552 if (!BN_rand_ex(q, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD, ctx))
555 /* we need ((rnd-rem) % add) == 0 */
556 if (!BN_mod(t1, q, qadd, ctx))
558 if (!BN_sub(q, q, t1))
561 if (!BN_add_word(q, 1))
564 if (!BN_rshift1(t1, rem))
566 if (!BN_add(q, q, t1))
570 /* we now have a random number 'rand' to test. */
571 if (!BN_lshift1(p, q))
573 if (!BN_add_word(p, 1))
577 for (i = 1; i < NUMPRIMES; i++) {
578 /* check that p and q are prime */
580 * check that for p and q gcd(p-1,primes) == 1 (except for 2)
582 BN_ULONG pmod = BN_mod_word(p, (BN_ULONG)primes[i]);
583 BN_ULONG qmod = BN_mod_word(q, (BN_ULONG)primes[i]);
584 if (pmod == (BN_ULONG)-1 || qmod == (BN_ULONG)-1)
586 if (pmod == 0 || qmod == 0) {
587 if (!BN_add(p, p, padd))
589 if (!BN_add(q, q, qadd))