]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/bn/bn_prime.c
Make sure all BIGNUM operations work within the FIPS provider
[thirdparty/openssl.git] / crypto / bn / bn_prime.c
CommitLineData
4f22f405 1/*
8240d5fa 2 * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
4f22f405 3 *
367ace68 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
4f22f405
RS
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
bfe30e4d 8 */
d02b48c6
RE
9
10#include <stdio.h>
11#include <time.h>
b39fc560 12#include "internal/cryptlib.h"
d02b48c6 13#include "bn_lcl.h"
d02b48c6 14
0f113f3e
MC
15/*
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.
d02b48c6
RE
19 */
20#include "bn_prime.h"
21
2934be91 22static int probable_prime(BIGNUM *rnd, int bits, prime_t *mods, BN_CTX *ctx);
76aa0ddc 23static int probable_prime_dh_safe(BIGNUM *rnd, int bits,
0f113f3e
MC
24 const BIGNUM *add, const BIGNUM *rem,
25 BN_CTX *ctx);
eb952088 26
8240d5fa
SL
27#if BN_BITS2 == 64
28# define BN_DEF(lo, hi) (BN_ULONG)hi<<32|lo
29#else
30# define BN_DEF(lo, hi) lo, hi
31#endif
32
33/*
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).
38 */
39static 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),
48 (BN_ULONG)0x000017b1
49};
50
51#define BN_SMALL_PRIME_FACTORS_TOP OSSL_NELEM(small_prime_factors)
52static const BIGNUM _bignum_small_prime_factors = {
53 (BN_ULONG *)small_prime_factors,
54 BN_SMALL_PRIME_FACTORS_TOP,
55 BN_SMALL_PRIME_FACTORS_TOP,
56 0,
57 BN_FLG_STATIC_DATA
58};
59
60const BIGNUM *bn_get0_small_factors(void)
61{
62 return &_bignum_small_prime_factors;
63}
64
e9224c71 65int BN_GENCB_call(BN_GENCB *cb, int a, int b)
0f113f3e
MC
66{
67 /* No callback means continue */
68 if (!cb)
69 return 1;
70 switch (cb->ver) {
71 case 1:
72 /* Deprecated-style callbacks */
73 if (!cb->cb.cb_1)
74 return 1;
75 cb->cb.cb_1(a, b, cb->arg);
76 return 1;
77 case 2:
78 /* New-style callbacks */
79 return cb->cb.cb_2(a, b, cb);
80 default:
81 break;
82 }
83 /* Unrecognised callback type */
84 return 0;
85}
e9224c71 86
2934be91
MC
87int BN_generate_prime_ex2(BIGNUM *ret, int bits, int safe,
88 const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb,
89 BN_CTX *ctx)
0f113f3e
MC
90{
91 BIGNUM *t;
92 int found = 0;
93 int i, j, c1 = 0;
8e704858 94 prime_t *mods = NULL;
0f113f3e
MC
95 int checks = BN_prime_checks_for_size(bits);
96
97 if (bits < 2) {
98 /* There are no prime numbers this small. */
2934be91 99 BNerr(BN_F_BN_GENERATE_PRIME_EX2, BN_R_BITS_TOO_SMALL);
0f113f3e 100 return 0;
291f616c
BE
101 } else if (add == NULL && safe && bits < 6 && bits != 3) {
102 /*
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.
106 */
2934be91 107 BNerr(BN_F_BN_GENERATE_PRIME_EX2, BN_R_BITS_TOO_SMALL);
0f113f3e
MC
108 return 0;
109 }
110
d71eb667
MC
111 mods = OPENSSL_zalloc(sizeof(*mods) * NUMPRIMES);
112 if (mods == NULL)
113 goto err;
114
0f113f3e
MC
115 BN_CTX_start(ctx);
116 t = BN_CTX_get(ctx);
e8e55976 117 if (t == NULL)
0f113f3e
MC
118 goto err;
119 loop:
120 /* make a random number and set the top and bottom bits */
121 if (add == NULL) {
2934be91 122 if (!probable_prime(ret, bits, mods, ctx))
0f113f3e
MC
123 goto err;
124 } else {
125 if (safe) {
126 if (!probable_prime_dh_safe(ret, bits, add, rem, ctx))
127 goto err;
128 } else {
129 if (!bn_probable_prime_dh(ret, bits, add, rem, ctx))
130 goto err;
131 }
132 }
d70a5627 133
0f113f3e
MC
134 if (!BN_GENCB_call(cb, 0, c1++))
135 /* aborted */
136 goto err;
137
138 if (!safe) {
139 i = BN_is_prime_fasttest_ex(ret, checks, ctx, 0, cb);
140 if (i == -1)
141 goto err;
142 if (i == 0)
143 goto loop;
144 } else {
145 /*
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
148 */
149 if (!BN_rshift1(t, ret))
150 goto err;
151
152 for (i = 0; i < checks; i++) {
153 j = BN_is_prime_fasttest_ex(ret, 1, ctx, 0, cb);
154 if (j == -1)
155 goto err;
156 if (j == 0)
157 goto loop;
158
159 j = BN_is_prime_fasttest_ex(t, 1, ctx, 0, cb);
160 if (j == -1)
161 goto err;
162 if (j == 0)
163 goto loop;
164
165 if (!BN_GENCB_call(cb, 2, c1 - 1))
166 goto err;
167 /* We have a safe prime test pass */
168 }
169 }
170 /* we have a prime :-) */
171 found = 1;
172 err:
8e704858 173 OPENSSL_free(mods);
ce1415ed 174 BN_CTX_end(ctx);
0f113f3e
MC
175 bn_check_top(ret);
176 return found;
177}
178
2934be91
MC
179#ifndef FIPS_MODE
180int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe,
181 const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb)
182{
183 BN_CTX *ctx = BN_CTX_new();
184 int retval;
185
186 if (ctx == NULL)
187 return 0;
188
189 retval = BN_generate_prime_ex2(ret, bits, safe, add, rem, cb, ctx);
190
191 BN_CTX_free(ctx);
192 return retval;
193}
194#endif
195
0f113f3e
MC
196int BN_is_prime_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,
197 BN_GENCB *cb)
198{
199 return BN_is_prime_fasttest_ex(a, checks, ctx_passed, 0, cb);
200}
e74231ed 201
8240d5fa 202/* See FIPS 186-4 C.3.1 Miller Rabin Probabilistic Primality Test. */
2934be91 203int BN_is_prime_fasttest_ex(const BIGNUM *w, int checks, BN_CTX *ctx,
0f113f3e
MC
204 int do_trial_division, BN_GENCB *cb)
205{
8240d5fa 206 int i, status, ret = -1;
2934be91
MC
207#ifndef FIPS_MODE
208 BN_CTX *ctxlocal = NULL;
209#else
210
211 if (ctx == NULL)
212 return -1;
213#endif
7d79d13a 214
8240d5fa
SL
215 /* w must be bigger than 1 */
216 if (BN_cmp(w, BN_value_one()) <= 0)
0f113f3e
MC
217 return 0;
218
8240d5fa
SL
219 /* w must be odd */
220 if (BN_is_odd(w)) {
221 /* Take care of the really small prime 3 */
222 if (BN_is_word(w, 3))
223 return 1;
224 } else {
225 /* 2 is the only even prime */
226 return BN_is_word(w, 2);
227 }
0f113f3e
MC
228
229 /* first look for small factors */
0f113f3e 230 if (do_trial_division) {
d70a5627 231 for (i = 1; i < NUMPRIMES; i++) {
8240d5fa 232 BN_ULONG mod = BN_mod_word(w, primes[i]);
d70a5627 233 if (mod == (BN_ULONG)-1)
8240d5fa 234 return -1;
d70a5627 235 if (mod == 0)
8240d5fa 236 return BN_is_word(w, primes[i]);
d70a5627 237 }
0f113f3e 238 if (!BN_GENCB_call(cb, 1, -1))
8240d5fa 239 return -1;
0f113f3e 240 }
2934be91
MC
241#ifndef FIPS_MODE
242 if (ctx == NULL && (ctxlocal = ctx = BN_CTX_new()) == NULL)
0f113f3e 243 goto err;
2934be91 244#endif
0f113f3e 245
8240d5fa
SL
246 ret = bn_miller_rabin_is_prime(w, checks, ctx, cb, 0, &status);
247 if (!ret)
0f113f3e 248 goto err;
8240d5fa
SL
249 ret = (status == BN_PRIMETEST_PROBABLY_PRIME);
250err:
2934be91
MC
251#ifndef FIPS_MODE
252 BN_CTX_free(ctxlocal);
253#endif
8240d5fa
SL
254 return ret;
255}
256
257/*
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.
261 *
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
269 *
270 * returns 0 if there was an error, otherwise it returns 1.
271 */
272int bn_miller_rabin_is_prime(const BIGNUM *w, int iterations, BN_CTX *ctx,
273 BN_GENCB *cb, int enhanced, int *status)
274{
275 int i, j, a, ret = 0;
276 BIGNUM *g, *w1, *w3, *x, *m, *z, *b;
277 BN_MONT_CTX *mont = NULL;
0f113f3e 278
8240d5fa
SL
279 /* w must be odd */
280 if (!BN_is_odd(w))
281 return 0;
282
283 BN_CTX_start(ctx);
284 g = BN_CTX_get(ctx);
285 w1 = BN_CTX_get(ctx);
286 w3 = BN_CTX_get(ctx);
287 x = BN_CTX_get(ctx);
288 m = BN_CTX_get(ctx);
289 z = BN_CTX_get(ctx);
290 b = BN_CTX_get(ctx);
291
292 if (!(b != NULL
293 /* w1 := w - 1 */
294 && BN_copy(w1, w)
295 && BN_sub_word(w1, 1)
296 /* w3 := w - 3 */
297 && BN_copy(w3, w)
298 && BN_sub_word(w3, 3)))
0f113f3e 299 goto err;
8240d5fa
SL
300
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))
0f113f3e 303 goto err;
0f113f3e 304
8240d5fa
SL
305 /* (Step 1) Calculate largest integer 'a' such that 2^a divides w-1 */
306 a = 1;
307 while (!BN_is_bit_set(w1, a))
308 a++;
309 /* (Step 2) m = (w-1) / 2^a */
310 if (!BN_rshift(m, w1, a))
0f113f3e
MC
311 goto err;
312
8b24f942 313 /* Montgomery setup for computations mod a */
0f113f3e 314 mont = BN_MONT_CTX_new();
8240d5fa 315 if (mont == NULL || !BN_MONT_CTX_set(mont, w, ctx))
0f113f3e
MC
316 goto err;
317
8240d5fa
SL
318 if (iterations == BN_prime_checks)
319 iterations = BN_prime_checks_for_size(BN_num_bits(w));
0f113f3e 320
8240d5fa
SL
321 /* (Step 4) */
322 for (i = 0; i < iterations; ++i) {
323 /* (Step 4.1) obtain a Random string of bits b where 1 < b < w-1 */
2934be91
MC
324 if (!BN_priv_rand_range_ex(b, w3, ctx)
325 || !BN_add_word(b, 2)) /* 1 < b < w-1 */
0f113f3e 326 goto err;
8240d5fa
SL
327
328 if (enhanced) {
329 /* (Step 4.3) */
330 if (!BN_gcd(g, b, w, ctx))
331 goto err;
332 /* (Step 4.4) */
333 if (!BN_is_one(g)) {
334 *status = BN_PRIMETEST_COMPOSITE_WITH_FACTOR;
335 ret = 1;
336 goto err;
337 }
338 }
339 /* (Step 4.5) z = b^m mod w */
340 if (!BN_mod_exp_mont(z, b, m, w, ctx, mont))
0f113f3e 341 goto err;
8240d5fa
SL
342 /* (Step 4.6) if (z = 1 or z = w-1) */
343 if (BN_is_one(z) || BN_cmp(z, w1) == 0)
344 goto outer_loop;
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))
349 goto err;
350 /* (Step 4.7.3) */
351 if (BN_cmp(z, w1) == 0)
352 goto outer_loop;
353 /* (Step 4.7.4) */
354 if (BN_is_one(z))
355 goto composite;
0f113f3e 356 }
8240d5fa
SL
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))
360 goto err;
361 /* (Step 4.10) */
362 if (BN_is_one(z))
363 goto composite;
364 /* (Step 4.11) x = b^(w-1) mod w */
365 if (!BN_copy(x, z))
366 goto err;
367composite:
368 if (enhanced) {
369 /* (Step 4.1.2) g = GCD(x-1, w) */
370 if (!BN_sub_word(x, 1) || !BN_gcd(g, x, w, ctx))
371 goto err;
372 /* (Steps 4.1.3 - 4.1.4) */
373 if (BN_is_one(g))
374 *status = BN_PRIMETEST_COMPOSITE_NOT_POWER_OF_PRIME;
375 else
376 *status = BN_PRIMETEST_COMPOSITE_WITH_FACTOR;
377 } else {
378 *status = BN_PRIMETEST_COMPOSITE;
379 }
380 ret = 1;
381 goto err;
382outer_loop: ;
383 /* (Step 4.1.5) */
3e3dcf9a
KR
384 if (!BN_GENCB_call(cb, 1, i))
385 goto err;
0f113f3e 386 }
8240d5fa
SL
387 /* (Step 5) */
388 *status = BN_PRIMETEST_PROBABLY_PRIME;
0f113f3e 389 ret = 1;
8240d5fa
SL
390err:
391 BN_clear(g);
392 BN_clear(w1);
393 BN_clear(w3);
394 BN_clear(x);
395 BN_clear(m);
396 BN_clear(z);
397 BN_clear(b);
398 BN_CTX_end(ctx);
23a1d5e9 399 BN_MONT_CTX_free(mont);
26a7d938 400 return ret;
0f113f3e 401}
a87030a1 402
2934be91 403static int probable_prime(BIGNUM *rnd, int bits, prime_t *mods, BN_CTX *ctx)
0f113f3e
MC
404{
405 int i;
0f113f3e
MC
406 BN_ULONG delta;
407 BN_ULONG maxdelta = BN_MASK2 - primes[NUMPRIMES - 1];
408 char is_single_word = bits <= BN_BITS2;
409
410 again:
4cffafe9 411 /* TODO: Not all primes are private */
2934be91 412 if (!BN_priv_rand_ex(rnd, bits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ODD, ctx))
26a7d938 413 return 0;
0f113f3e 414 /* we now have a random number 'rnd' to test. */
d70a5627
DB
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)
418 return 0;
419 mods[i] = (prime_t) mod;
420 }
0f113f3e
MC
421 /*
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.
424 */
425 if (is_single_word) {
e4676e90 426 BN_ULONG size_limit;
02e112a8 427
e4676e90
MC
428 if (bits == BN_BITS2) {
429 /*
430 * Shifting by this much has undefined behaviour so we do it a
431 * different way
432 */
433 size_limit = ~((BN_ULONG)0) - BN_get_word(rnd);
434 } else {
435 size_limit = (((BN_ULONG)1) << bits) - BN_get_word(rnd) - 1;
436 }
0f113f3e
MC
437 if (size_limit < maxdelta)
438 maxdelta = size_limit;
439 }
440 delta = 0;
441 loop:
442 if (is_single_word) {
443 BN_ULONG rnd_word = BN_get_word(rnd);
444
50e735f9
MC
445 /*-
446 * In the case that the candidate prime is a single word then
447 * we check that:
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
450 * three.
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
454 * that's true.
455 */
0f113f3e
MC
456 for (i = 1; i < NUMPRIMES && primes[i] < rnd_word; i++) {
457 if ((mods[i] + delta) % primes[i] == 0) {
458 delta += 2;
459 if (delta > maxdelta)
460 goto again;
461 goto loop;
462 }
463 }
464 } else {
465 for (i = 1; i < NUMPRIMES; i++) {
466 /*
467 * check that rnd is not a prime and also that gcd(rnd-1,primes)
468 * == 1 (except for 2)
469 */
470 if (((mods[i] + delta) % primes[i]) <= 1) {
471 delta += 2;
472 if (delta > maxdelta)
473 goto again;
474 goto loop;
475 }
476 }
477 }
478 if (!BN_add_word(rnd, delta))
26a7d938 479 return 0;
0f113f3e
MC
480 if (BN_num_bits(rnd) != bits)
481 goto again;
482 bn_check_top(rnd);
208fb891 483 return 1;
0f113f3e 484}
d02b48c6 485
982c42cb 486int bn_probable_prime_dh(BIGNUM *rnd, int bits,
0f113f3e
MC
487 const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx)
488{
489 int i, ret = 0;
490 BIGNUM *t1;
491
492 BN_CTX_start(ctx);
493 if ((t1 = BN_CTX_get(ctx)) == NULL)
494 goto err;
495
2934be91 496 if (!BN_rand_ex(rnd, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD, ctx))
0f113f3e
MC
497 goto err;
498
499 /* we need ((rnd-rem) % add) == 0 */
500
501 if (!BN_mod(t1, rnd, add, ctx))
502 goto err;
503 if (!BN_sub(rnd, rnd, t1))
504 goto err;
505 if (rem == NULL) {
506 if (!BN_add_word(rnd, 1))
507 goto err;
508 } else {
509 if (!BN_add(rnd, rnd, rem))
510 goto err;
511 }
512
513 /* we now have a random number 'rand' to test. */
514
515 loop:
516 for (i = 1; i < NUMPRIMES; i++) {
517 /* check that rnd is a prime */
d70a5627
DB
518 BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);
519 if (mod == (BN_ULONG)-1)
520 goto err;
521 if (mod <= 1) {
0f113f3e
MC
522 if (!BN_add(rnd, rnd, add))
523 goto err;
524 goto loop;
525 }
526 }
527 ret = 1;
528
529 err:
530 BN_CTX_end(ctx);
531 bn_check_top(rnd);
26a7d938 532 return ret;
0f113f3e 533}
b0513819 534
020fc820 535static int probable_prime_dh_safe(BIGNUM *p, int bits, const BIGNUM *padd,
0f113f3e
MC
536 const BIGNUM *rem, BN_CTX *ctx)
537{
538 int i, ret = 0;
539 BIGNUM *t1, *qadd, *q;
540
541 bits--;
542 BN_CTX_start(ctx);
543 t1 = BN_CTX_get(ctx);
544 q = BN_CTX_get(ctx);
545 qadd = BN_CTX_get(ctx);
546 if (qadd == NULL)
547 goto err;
548
549 if (!BN_rshift1(qadd, padd))
550 goto err;
551
2934be91 552 if (!BN_rand_ex(q, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD, ctx))
0f113f3e
MC
553 goto err;
554
555 /* we need ((rnd-rem) % add) == 0 */
556 if (!BN_mod(t1, q, qadd, ctx))
557 goto err;
558 if (!BN_sub(q, q, t1))
559 goto err;
560 if (rem == NULL) {
561 if (!BN_add_word(q, 1))
562 goto err;
563 } else {
564 if (!BN_rshift1(t1, rem))
565 goto err;
566 if (!BN_add(q, q, t1))
567 goto err;
568 }
569
570 /* we now have a random number 'rand' to test. */
571 if (!BN_lshift1(p, q))
572 goto err;
573 if (!BN_add_word(p, 1))
574 goto err;
575
576 loop:
577 for (i = 1; i < NUMPRIMES; i++) {
578 /* check that p and q are prime */
579 /*
580 * check that for p and q gcd(p-1,primes) == 1 (except for 2)
581 */
d70a5627
DB
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)
585 goto err;
586 if (pmod == 0 || qmod == 0) {
0f113f3e
MC
587 if (!BN_add(p, p, padd))
588 goto err;
589 if (!BN_add(q, q, qadd))
590 goto err;
591 goto loop;
592 }
593 }
594 ret = 1;
595
596 err:
597 BN_CTX_end(ctx);
598 bn_check_top(p);
26a7d938 599 return ret;
0f113f3e 600}