]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/bn/bn_prime.c
Call RSA generation callback at the correct time.
[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
8e704858 22static int probable_prime(BIGNUM *rnd, int bits, prime_t *mods);
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
GT
86
87int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe,
0f113f3e
MC
88 const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb)
89{
90 BIGNUM *t;
91 int found = 0;
92 int i, j, c1 = 0;
8e704858
RS
93 BN_CTX *ctx = NULL;
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. */
99 BNerr(BN_F_BN_GENERATE_PRIME_EX, BN_R_BITS_TOO_SMALL);
100 return 0;
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);
104 return 0;
105 }
106
d71eb667
MC
107 mods = OPENSSL_zalloc(sizeof(*mods) * NUMPRIMES);
108 if (mods == NULL)
109 goto err;
110
0f113f3e
MC
111 ctx = BN_CTX_new();
112 if (ctx == NULL)
113 goto err;
114 BN_CTX_start(ctx);
115 t = BN_CTX_get(ctx);
e8e55976 116 if (t == NULL)
0f113f3e
MC
117 goto err;
118 loop:
119 /* make a random number and set the top and bottom bits */
120 if (add == NULL) {
8e704858 121 if (!probable_prime(ret, bits, mods))
0f113f3e
MC
122 goto err;
123 } else {
124 if (safe) {
125 if (!probable_prime_dh_safe(ret, bits, add, rem, ctx))
126 goto err;
127 } else {
128 if (!bn_probable_prime_dh(ret, bits, add, rem, ctx))
129 goto err;
130 }
131 }
d70a5627 132
0f113f3e
MC
133 if (!BN_GENCB_call(cb, 0, c1++))
134 /* aborted */
135 goto err;
136
137 if (!safe) {
138 i = BN_is_prime_fasttest_ex(ret, checks, ctx, 0, cb);
139 if (i == -1)
140 goto err;
141 if (i == 0)
142 goto loop;
143 } else {
144 /*
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
147 */
148 if (!BN_rshift1(t, ret))
149 goto err;
150
151 for (i = 0; i < checks; i++) {
152 j = BN_is_prime_fasttest_ex(ret, 1, ctx, 0, cb);
153 if (j == -1)
154 goto err;
155 if (j == 0)
156 goto loop;
157
158 j = BN_is_prime_fasttest_ex(t, 1, ctx, 0, cb);
159 if (j == -1)
160 goto err;
161 if (j == 0)
162 goto loop;
163
164 if (!BN_GENCB_call(cb, 2, c1 - 1))
165 goto err;
166 /* We have a safe prime test pass */
167 }
168 }
169 /* we have a prime :-) */
170 found = 1;
171 err:
8e704858 172 OPENSSL_free(mods);
ce1415ed 173 BN_CTX_end(ctx);
23a1d5e9 174 BN_CTX_free(ctx);
0f113f3e
MC
175 bn_check_top(ret);
176 return found;
177}
178
179int BN_is_prime_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,
180 BN_GENCB *cb)
181{
182 return BN_is_prime_fasttest_ex(a, checks, ctx_passed, 0, cb);
183}
e74231ed 184
8240d5fa
SL
185/* See FIPS 186-4 C.3.1 Miller Rabin Probabilistic Primality Test. */
186int BN_is_prime_fasttest_ex(const BIGNUM *w, int checks, BN_CTX *ctx_passed,
0f113f3e
MC
187 int do_trial_division, BN_GENCB *cb)
188{
8240d5fa 189 int i, status, ret = -1;
0f113f3e 190 BN_CTX *ctx = NULL;
7d79d13a 191
8240d5fa
SL
192 /* w must be bigger than 1 */
193 if (BN_cmp(w, BN_value_one()) <= 0)
0f113f3e
MC
194 return 0;
195
8240d5fa
SL
196 /* w must be odd */
197 if (BN_is_odd(w)) {
198 /* Take care of the really small prime 3 */
199 if (BN_is_word(w, 3))
200 return 1;
201 } else {
202 /* 2 is the only even prime */
203 return BN_is_word(w, 2);
204 }
0f113f3e
MC
205
206 /* first look for small factors */
0f113f3e 207 if (do_trial_division) {
d70a5627 208 for (i = 1; i < NUMPRIMES; i++) {
8240d5fa 209 BN_ULONG mod = BN_mod_word(w, primes[i]);
d70a5627 210 if (mod == (BN_ULONG)-1)
8240d5fa 211 return -1;
d70a5627 212 if (mod == 0)
8240d5fa 213 return BN_is_word(w, primes[i]);
d70a5627 214 }
0f113f3e 215 if (!BN_GENCB_call(cb, 1, -1))
8240d5fa 216 return -1;
0f113f3e 217 }
0f113f3e
MC
218 if (ctx_passed != NULL)
219 ctx = ctx_passed;
220 else if ((ctx = BN_CTX_new()) == NULL)
221 goto err;
0f113f3e 222
8240d5fa
SL
223 ret = bn_miller_rabin_is_prime(w, checks, ctx, cb, 0, &status);
224 if (!ret)
0f113f3e 225 goto err;
8240d5fa
SL
226 ret = (status == BN_PRIMETEST_PROBABLY_PRIME);
227err:
228 if (ctx_passed == NULL)
229 BN_CTX_free(ctx);
230 return ret;
231}
232
233/*
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.
237 *
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
245 *
246 * returns 0 if there was an error, otherwise it returns 1.
247 */
248int bn_miller_rabin_is_prime(const BIGNUM *w, int iterations, BN_CTX *ctx,
249 BN_GENCB *cb, int enhanced, int *status)
250{
251 int i, j, a, ret = 0;
252 BIGNUM *g, *w1, *w3, *x, *m, *z, *b;
253 BN_MONT_CTX *mont = NULL;
0f113f3e 254
8240d5fa
SL
255 /* w must be odd */
256 if (!BN_is_odd(w))
257 return 0;
258
259 BN_CTX_start(ctx);
260 g = BN_CTX_get(ctx);
261 w1 = BN_CTX_get(ctx);
262 w3 = BN_CTX_get(ctx);
263 x = BN_CTX_get(ctx);
264 m = BN_CTX_get(ctx);
265 z = BN_CTX_get(ctx);
266 b = BN_CTX_get(ctx);
267
268 if (!(b != NULL
269 /* w1 := w - 1 */
270 && BN_copy(w1, w)
271 && BN_sub_word(w1, 1)
272 /* w3 := w - 3 */
273 && BN_copy(w3, w)
274 && BN_sub_word(w3, 3)))
0f113f3e 275 goto err;
8240d5fa
SL
276
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))
0f113f3e 279 goto err;
0f113f3e 280
8240d5fa
SL
281 /* (Step 1) Calculate largest integer 'a' such that 2^a divides w-1 */
282 a = 1;
283 while (!BN_is_bit_set(w1, a))
284 a++;
285 /* (Step 2) m = (w-1) / 2^a */
286 if (!BN_rshift(m, w1, a))
0f113f3e
MC
287 goto err;
288
8b24f942 289 /* Montgomery setup for computations mod a */
0f113f3e 290 mont = BN_MONT_CTX_new();
8240d5fa 291 if (mont == NULL || !BN_MONT_CTX_set(mont, w, ctx))
0f113f3e
MC
292 goto err;
293
8240d5fa
SL
294 if (iterations == BN_prime_checks)
295 iterations = BN_prime_checks_for_size(BN_num_bits(w));
0f113f3e 296
8240d5fa
SL
297 /* (Step 4) */
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 */
0f113f3e 301 goto err;
8240d5fa
SL
302
303 if (enhanced) {
304 /* (Step 4.3) */
305 if (!BN_gcd(g, b, w, ctx))
306 goto err;
307 /* (Step 4.4) */
308 if (!BN_is_one(g)) {
309 *status = BN_PRIMETEST_COMPOSITE_WITH_FACTOR;
310 ret = 1;
311 goto err;
312 }
313 }
314 /* (Step 4.5) z = b^m mod w */
315 if (!BN_mod_exp_mont(z, b, m, w, ctx, mont))
0f113f3e 316 goto err;
8240d5fa
SL
317 /* (Step 4.6) if (z = 1 or z = w-1) */
318 if (BN_is_one(z) || BN_cmp(z, w1) == 0)
319 goto outer_loop;
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))
324 goto err;
325 /* (Step 4.7.3) */
326 if (BN_cmp(z, w1) == 0)
327 goto outer_loop;
328 /* (Step 4.7.4) */
329 if (BN_is_one(z))
330 goto composite;
0f113f3e 331 }
8240d5fa
SL
332 /* At this point z = b^((w-1)/2) mod w */
333 /* (Steps 4.8 - 4.9) x = z, z = x^2 mod w */
334 if (!BN_copy(x, z) || !BN_mod_mul(z, x, x, w, ctx))
335 goto err;
336 /* (Step 4.10) */
337 if (BN_is_one(z))
338 goto composite;
339 /* (Step 4.11) x = b^(w-1) mod w */
340 if (!BN_copy(x, z))
341 goto err;
342composite:
343 if (enhanced) {
344 /* (Step 4.1.2) g = GCD(x-1, w) */
345 if (!BN_sub_word(x, 1) || !BN_gcd(g, x, w, ctx))
346 goto err;
347 /* (Steps 4.1.3 - 4.1.4) */
348 if (BN_is_one(g))
349 *status = BN_PRIMETEST_COMPOSITE_NOT_POWER_OF_PRIME;
350 else
351 *status = BN_PRIMETEST_COMPOSITE_WITH_FACTOR;
352 } else {
353 *status = BN_PRIMETEST_COMPOSITE;
354 }
355 ret = 1;
356 goto err;
357outer_loop: ;
358 /* (Step 4.1.5) */
3e3dcf9a
KR
359 if (!BN_GENCB_call(cb, 1, i))
360 goto err;
0f113f3e 361 }
8240d5fa
SL
362 /* (Step 5) */
363 *status = BN_PRIMETEST_PROBABLY_PRIME;
0f113f3e 364 ret = 1;
8240d5fa
SL
365err:
366 BN_clear(g);
367 BN_clear(w1);
368 BN_clear(w3);
369 BN_clear(x);
370 BN_clear(m);
371 BN_clear(z);
372 BN_clear(b);
373 BN_CTX_end(ctx);
23a1d5e9 374 BN_MONT_CTX_free(mont);
26a7d938 375 return ret;
0f113f3e 376}
a87030a1 377
8e704858 378static int probable_prime(BIGNUM *rnd, int bits, prime_t *mods)
0f113f3e
MC
379{
380 int i;
0f113f3e
MC
381 BN_ULONG delta;
382 BN_ULONG maxdelta = BN_MASK2 - primes[NUMPRIMES - 1];
383 char is_single_word = bits <= BN_BITS2;
384
385 again:
4cffafe9 386 /* TODO: Not all primes are private */
ddc6a5c8 387 if (!BN_priv_rand(rnd, bits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ODD))
26a7d938 388 return 0;
0f113f3e 389 /* we now have a random number 'rnd' to test. */
d70a5627
DB
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)
393 return 0;
394 mods[i] = (prime_t) mod;
395 }
0f113f3e
MC
396 /*
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.
399 */
400 if (is_single_word) {
e4676e90 401 BN_ULONG size_limit;
02e112a8 402
e4676e90
MC
403 if (bits == BN_BITS2) {
404 /*
405 * Shifting by this much has undefined behaviour so we do it a
406 * different way
407 */
408 size_limit = ~((BN_ULONG)0) - BN_get_word(rnd);
409 } else {
410 size_limit = (((BN_ULONG)1) << bits) - BN_get_word(rnd) - 1;
411 }
0f113f3e
MC
412 if (size_limit < maxdelta)
413 maxdelta = size_limit;
414 }
415 delta = 0;
416 loop:
417 if (is_single_word) {
418 BN_ULONG rnd_word = BN_get_word(rnd);
419
50e735f9
MC
420 /*-
421 * In the case that the candidate prime is a single word then
422 * we check that:
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
425 * three.
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
429 * that's true.
430 */
0f113f3e
MC
431 for (i = 1; i < NUMPRIMES && primes[i] < rnd_word; i++) {
432 if ((mods[i] + delta) % primes[i] == 0) {
433 delta += 2;
434 if (delta > maxdelta)
435 goto again;
436 goto loop;
437 }
438 }
439 } else {
440 for (i = 1; i < NUMPRIMES; i++) {
441 /*
442 * check that rnd is not a prime and also that gcd(rnd-1,primes)
443 * == 1 (except for 2)
444 */
445 if (((mods[i] + delta) % primes[i]) <= 1) {
446 delta += 2;
447 if (delta > maxdelta)
448 goto again;
449 goto loop;
450 }
451 }
452 }
453 if (!BN_add_word(rnd, delta))
26a7d938 454 return 0;
0f113f3e
MC
455 if (BN_num_bits(rnd) != bits)
456 goto again;
457 bn_check_top(rnd);
208fb891 458 return 1;
0f113f3e 459}
d02b48c6 460
982c42cb 461int bn_probable_prime_dh(BIGNUM *rnd, int bits,
0f113f3e
MC
462 const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx)
463{
464 int i, ret = 0;
465 BIGNUM *t1;
466
467 BN_CTX_start(ctx);
468 if ((t1 = BN_CTX_get(ctx)) == NULL)
469 goto err;
470
4cffafe9 471 if (!BN_rand(rnd, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))
0f113f3e
MC
472 goto err;
473
474 /* we need ((rnd-rem) % add) == 0 */
475
476 if (!BN_mod(t1, rnd, add, ctx))
477 goto err;
478 if (!BN_sub(rnd, rnd, t1))
479 goto err;
480 if (rem == NULL) {
481 if (!BN_add_word(rnd, 1))
482 goto err;
483 } else {
484 if (!BN_add(rnd, rnd, rem))
485 goto err;
486 }
487
488 /* we now have a random number 'rand' to test. */
489
490 loop:
491 for (i = 1; i < NUMPRIMES; i++) {
492 /* check that rnd is a prime */
d70a5627
DB
493 BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);
494 if (mod == (BN_ULONG)-1)
495 goto err;
496 if (mod <= 1) {
0f113f3e
MC
497 if (!BN_add(rnd, rnd, add))
498 goto err;
499 goto loop;
500 }
501 }
502 ret = 1;
503
504 err:
505 BN_CTX_end(ctx);
506 bn_check_top(rnd);
26a7d938 507 return ret;
0f113f3e 508}
b0513819 509
020fc820 510static int probable_prime_dh_safe(BIGNUM *p, int bits, const BIGNUM *padd,
0f113f3e
MC
511 const BIGNUM *rem, BN_CTX *ctx)
512{
513 int i, ret = 0;
514 BIGNUM *t1, *qadd, *q;
515
516 bits--;
517 BN_CTX_start(ctx);
518 t1 = BN_CTX_get(ctx);
519 q = BN_CTX_get(ctx);
520 qadd = BN_CTX_get(ctx);
521 if (qadd == NULL)
522 goto err;
523
524 if (!BN_rshift1(qadd, padd))
525 goto err;
526
4cffafe9 527 if (!BN_rand(q, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))
0f113f3e
MC
528 goto err;
529
530 /* we need ((rnd-rem) % add) == 0 */
531 if (!BN_mod(t1, q, qadd, ctx))
532 goto err;
533 if (!BN_sub(q, q, t1))
534 goto err;
535 if (rem == NULL) {
536 if (!BN_add_word(q, 1))
537 goto err;
538 } else {
539 if (!BN_rshift1(t1, rem))
540 goto err;
541 if (!BN_add(q, q, t1))
542 goto err;
543 }
544
545 /* we now have a random number 'rand' to test. */
546 if (!BN_lshift1(p, q))
547 goto err;
548 if (!BN_add_word(p, 1))
549 goto err;
550
551 loop:
552 for (i = 1; i < NUMPRIMES; i++) {
553 /* check that p and q are prime */
554 /*
555 * check that for p and q gcd(p-1,primes) == 1 (except for 2)
556 */
d70a5627
DB
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)
560 goto err;
561 if (pmod == 0 || qmod == 0) {
0f113f3e
MC
562 if (!BN_add(p, p, padd))
563 goto err;
564 if (!BN_add(q, q, qadd))
565 goto err;
566 goto loop;
567 }
568 }
569 ret = 1;
570
571 err:
572 BN_CTX_end(ctx);
573 bn_check_top(p);
26a7d938 574 return ret;
0f113f3e 575}