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 :-) */
179 int BN_is_prime_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,
182 return BN_is_prime_fasttest_ex(a, checks, ctx_passed, 0, cb);
185 /* See FIPS 186-4 C.3.1 Miller Rabin Probabilistic Primality Test. */
186 int BN_is_prime_fasttest_ex(const BIGNUM *w, int checks, BN_CTX *ctx_passed,
187 int do_trial_division, BN_GENCB *cb)
189 int i, status, ret = -1;
192 /* w must be bigger than 1 */
193 if (BN_cmp(w, BN_value_one()) <= 0)
198 /* Take care of the really small prime 3 */
199 if (BN_is_word(w, 3))
202 /* 2 is the only even prime */
203 return BN_is_word(w, 2);
206 /* first look for small factors */
207 if (do_trial_division) {
208 for (i = 1; i < NUMPRIMES; i++) {
209 BN_ULONG mod = BN_mod_word(w, primes[i]);
210 if (mod == (BN_ULONG)-1)
213 return BN_is_word(w, primes[i]);
215 if (!BN_GENCB_call(cb, 1, -1))
218 if (ctx_passed != NULL)
220 else if ((ctx = BN_CTX_new()) == NULL)
223 ret = bn_miller_rabin_is_prime(w, checks, ctx, cb, 0, &status);
226 ret = (status == BN_PRIMETEST_PROBABLY_PRIME);
228 if (ctx_passed == NULL)
234 * Refer to FIPS 186-4 C.3.2 Enhanced Miller-Rabin Probabilistic Primality Test.
235 * OR C.3.1 Miller-Rabin Probabilistic Primality Test (if enhanced is zero).
236 * The Step numbers listed in the code refer to the enhanced case.
238 * if enhanced is set, then status returns one of the following:
239 * BN_PRIMETEST_PROBABLY_PRIME
240 * BN_PRIMETEST_COMPOSITE_WITH_FACTOR
241 * BN_PRIMETEST_COMPOSITE_NOT_POWER_OF_PRIME
242 * if enhanced is zero, then status returns either
243 * BN_PRIMETEST_PROBABLY_PRIME or
244 * BN_PRIMETEST_COMPOSITE
246 * returns 0 if there was an error, otherwise it returns 1.
248 int bn_miller_rabin_is_prime(const BIGNUM *w, int iterations, BN_CTX *ctx,
249 BN_GENCB *cb, int enhanced, int *status)
251 int i, j, a, ret = 0;
252 BIGNUM *g, *w1, *w3, *x, *m, *z, *b;
253 BN_MONT_CTX *mont = NULL;
261 w1 = BN_CTX_get(ctx);
262 w3 = BN_CTX_get(ctx);
271 && BN_sub_word(w1, 1)
274 && BN_sub_word(w3, 3)))
277 /* check w is larger than 3, otherwise the random b will be too small */
278 if (BN_is_zero(w3) || BN_is_negative(w3))
281 /* (Step 1) Calculate largest integer 'a' such that 2^a divides w-1 */
283 while (!BN_is_bit_set(w1, a))
285 /* (Step 2) m = (w-1) / 2^a */
286 if (!BN_rshift(m, w1, a))
289 /* Montgomery setup for computations mod a */
290 mont = BN_MONT_CTX_new();
291 if (mont == NULL || !BN_MONT_CTX_set(mont, w, ctx))
294 if (iterations == BN_prime_checks)
295 iterations = BN_prime_checks_for_size(BN_num_bits(w));
298 for (i = 0; i < iterations; ++i) {
299 /* (Step 4.1) obtain a Random string of bits b where 1 < b < w-1 */
300 if (!BN_priv_rand_range(b, w3) || !BN_add_word(b, 2)) /* 1 < b < w-1 */
305 if (!BN_gcd(g, b, w, ctx))
309 *status = BN_PRIMETEST_COMPOSITE_WITH_FACTOR;
314 /* (Step 4.5) z = b^m mod w */
315 if (!BN_mod_exp_mont(z, b, m, w, ctx, mont))
317 /* (Step 4.6) if (z = 1 or z = w-1) */
318 if (BN_is_one(z) || BN_cmp(z, w1) == 0)
320 /* (Step 4.7) for j = 1 to a-1 */
321 for (j = 1; j < a ; ++j) {
322 /* (Step 4.7.1 - 4.7.2) x = z. z = x^2 mod w */
323 if (!BN_copy(x, z) || !BN_mod_mul(z, x, x, w, ctx))
326 if (BN_cmp(z, w1) == 0)
332 if (!BN_GENCB_call(cb, 1, i))
334 /* At this point z = b^((w-1)/2) mod w */
335 /* (Steps 4.8 - 4.9) x = z, z = x^2 mod w */
336 if (!BN_copy(x, z) || !BN_mod_mul(z, x, x, w, ctx))
341 /* (Step 4.11) x = b^(w-1) mod w */
346 /* (Step 4.1.2) g = GCD(x-1, w) */
347 if (!BN_sub_word(x, 1) || !BN_gcd(g, x, w, ctx))
349 /* (Steps 4.1.3 - 4.1.4) */
351 *status = BN_PRIMETEST_COMPOSITE_NOT_POWER_OF_PRIME;
353 *status = BN_PRIMETEST_COMPOSITE_WITH_FACTOR;
355 *status = BN_PRIMETEST_COMPOSITE;
363 *status = BN_PRIMETEST_PROBABLY_PRIME;
374 BN_MONT_CTX_free(mont);
378 static int probable_prime(BIGNUM *rnd, int bits, prime_t *mods)
382 BN_ULONG maxdelta = BN_MASK2 - primes[NUMPRIMES - 1];
383 char is_single_word = bits <= BN_BITS2;
386 /* TODO: Not all primes are private */
387 if (!BN_priv_rand(rnd, bits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ODD))
389 /* we now have a random number 'rnd' to test. */
390 for (i = 1; i < NUMPRIMES; i++) {
391 BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);
392 if (mod == (BN_ULONG)-1)
394 mods[i] = (prime_t) mod;
397 * If bits is so small that it fits into a single word then we
398 * additionally don't want to exceed that many bits.
400 if (is_single_word) {
403 if (bits == BN_BITS2) {
405 * Shifting by this much has undefined behaviour so we do it a
408 size_limit = ~((BN_ULONG)0) - BN_get_word(rnd);
410 size_limit = (((BN_ULONG)1) << bits) - BN_get_word(rnd) - 1;
412 if (size_limit < maxdelta)
413 maxdelta = size_limit;
417 if (is_single_word) {
418 BN_ULONG rnd_word = BN_get_word(rnd);
421 * In the case that the candidate prime is a single word then
423 * 1) It's greater than primes[i] because we shouldn't reject
424 * 3 as being a prime number because it's a multiple of
426 * 2) That it's not a multiple of a known prime. We don't
427 * check that rnd-1 is also coprime to all the known
428 * primes because there aren't many small primes where
431 for (i = 1; i < NUMPRIMES && primes[i] < rnd_word; i++) {
432 if ((mods[i] + delta) % primes[i] == 0) {
434 if (delta > maxdelta)
440 for (i = 1; i < NUMPRIMES; i++) {
442 * check that rnd is not a prime and also that gcd(rnd-1,primes)
443 * == 1 (except for 2)
445 if (((mods[i] + delta) % primes[i]) <= 1) {
447 if (delta > maxdelta)
453 if (!BN_add_word(rnd, delta))
455 if (BN_num_bits(rnd) != bits)
461 int bn_probable_prime_dh(BIGNUM *rnd, int bits,
462 const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx)
468 if ((t1 = BN_CTX_get(ctx)) == NULL)
471 if (!BN_rand(rnd, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))
474 /* we need ((rnd-rem) % add) == 0 */
476 if (!BN_mod(t1, rnd, add, ctx))
478 if (!BN_sub(rnd, rnd, t1))
481 if (!BN_add_word(rnd, 1))
484 if (!BN_add(rnd, rnd, rem))
488 /* we now have a random number 'rand' to test. */
491 for (i = 1; i < NUMPRIMES; i++) {
492 /* check that rnd is a prime */
493 BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);
494 if (mod == (BN_ULONG)-1)
497 if (!BN_add(rnd, rnd, add))
510 static int probable_prime_dh_safe(BIGNUM *p, int bits, const BIGNUM *padd,
511 const BIGNUM *rem, BN_CTX *ctx)
514 BIGNUM *t1, *qadd, *q;
518 t1 = BN_CTX_get(ctx);
520 qadd = BN_CTX_get(ctx);
524 if (!BN_rshift1(qadd, padd))
527 if (!BN_rand(q, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))
530 /* we need ((rnd-rem) % add) == 0 */
531 if (!BN_mod(t1, q, qadd, ctx))
533 if (!BN_sub(q, q, t1))
536 if (!BN_add_word(q, 1))
539 if (!BN_rshift1(t1, rem))
541 if (!BN_add(q, q, t1))
545 /* we now have a random number 'rand' to test. */
546 if (!BN_lshift1(p, q))
548 if (!BN_add_word(p, 1))
552 for (i = 1; i < NUMPRIMES; i++) {
553 /* check that p and q are prime */
555 * check that for p and q gcd(p-1,primes) == 1 (except for 2)
557 BN_ULONG pmod = BN_mod_word(p, (BN_ULONG)primes[i]);
558 BN_ULONG qmod = BN_mod_word(q, (BN_ULONG)primes[i]);
559 if (pmod == (BN_ULONG)-1 || qmod == (BN_ULONG)-1)
561 if (pmod == 0 || qmod == 0) {
562 if (!BN_add(p, p, padd))
564 if (!BN_add(q, q, qadd))