]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/bn/bn_prime.c
Reorganize local header files
[thirdparty/openssl.git] / crypto / bn / bn_prime.c
1 /*
2 * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10 #include <stdio.h>
11 #include <time.h>
12 #include "internal/cryptlib.h"
13 #include "bn_local.h"
14
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.
19 */
20 #include "bn_prime.h"
21
22 static int probable_prime(BIGNUM *rnd, int bits, int safe, prime_t *mods,
23 BN_CTX *ctx);
24 static int probable_prime_dh(BIGNUM *rnd, int bits, int safe, prime_t *mods,
25 const BIGNUM *add, const BIGNUM *rem,
26 BN_CTX *ctx);
27
28 #define square(x) ((BN_ULONG)(x) * (BN_ULONG)(x))
29
30 #if BN_BITS2 == 64
31 # define BN_DEF(lo, hi) (BN_ULONG)hi<<32|lo
32 #else
33 # define BN_DEF(lo, hi) lo, hi
34 #endif
35
36 /*
37 * See SP800 89 5.3.3 (Step f)
38 * The product of the set of primes ranging from 3 to 751
39 * Generated using process in test/bn_internal_test.c test_bn_small_factors().
40 * This includes 751 (which is not currently included in SP 800-89).
41 */
42 static const BN_ULONG small_prime_factors[] = {
43 BN_DEF(0x3ef4e3e1, 0xc4309333), BN_DEF(0xcd2d655f, 0x71161eb6),
44 BN_DEF(0x0bf94862, 0x95e2238c), BN_DEF(0x24f7912b, 0x3eb233d3),
45 BN_DEF(0xbf26c483, 0x6b55514b), BN_DEF(0x5a144871, 0x0a84d817),
46 BN_DEF(0x9b82210a, 0x77d12fee), BN_DEF(0x97f050b3, 0xdb5b93c2),
47 BN_DEF(0x4d6c026b, 0x4acad6b9), BN_DEF(0x54aec893, 0xeb7751f3),
48 BN_DEF(0x36bc85c4, 0xdba53368), BN_DEF(0x7f5ec78e, 0xd85a1b28),
49 BN_DEF(0x6b322244, 0x2eb072d8), BN_DEF(0x5e2b3aea, 0xbba51112),
50 BN_DEF(0x0e2486bf, 0x36ed1a6c), BN_DEF(0xec0c5727, 0x5f270460),
51 (BN_ULONG)0x000017b1
52 };
53
54 #define BN_SMALL_PRIME_FACTORS_TOP OSSL_NELEM(small_prime_factors)
55 static const BIGNUM _bignum_small_prime_factors = {
56 (BN_ULONG *)small_prime_factors,
57 BN_SMALL_PRIME_FACTORS_TOP,
58 BN_SMALL_PRIME_FACTORS_TOP,
59 0,
60 BN_FLG_STATIC_DATA
61 };
62
63 const BIGNUM *bn_get0_small_factors(void)
64 {
65 return &_bignum_small_prime_factors;
66 }
67
68 int BN_GENCB_call(BN_GENCB *cb, int a, int b)
69 {
70 /* No callback means continue */
71 if (!cb)
72 return 1;
73 switch (cb->ver) {
74 case 1:
75 /* Deprecated-style callbacks */
76 if (!cb->cb.cb_1)
77 return 1;
78 cb->cb.cb_1(a, b, cb->arg);
79 return 1;
80 case 2:
81 /* New-style callbacks */
82 return cb->cb.cb_2(a, b, cb);
83 default:
84 break;
85 }
86 /* Unrecognised callback type */
87 return 0;
88 }
89
90 int BN_generate_prime_ex2(BIGNUM *ret, int bits, int safe,
91 const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb,
92 BN_CTX *ctx)
93 {
94 BIGNUM *t;
95 int found = 0;
96 int i, j, c1 = 0;
97 prime_t *mods = NULL;
98 int checks = BN_prime_checks_for_size(bits);
99
100 if (bits < 2) {
101 /* There are no prime numbers this small. */
102 BNerr(BN_F_BN_GENERATE_PRIME_EX2, BN_R_BITS_TOO_SMALL);
103 return 0;
104 } else if (add == NULL && safe && bits < 6 && bits != 3) {
105 /*
106 * The smallest safe prime (7) is three bits.
107 * But the following two safe primes with less than 6 bits (11, 23)
108 * are unreachable for BN_rand with BN_RAND_TOP_TWO.
109 */
110 BNerr(BN_F_BN_GENERATE_PRIME_EX2, BN_R_BITS_TOO_SMALL);
111 return 0;
112 }
113
114 mods = OPENSSL_zalloc(sizeof(*mods) * NUMPRIMES);
115 if (mods == NULL)
116 goto err;
117
118 BN_CTX_start(ctx);
119 t = BN_CTX_get(ctx);
120 if (t == NULL)
121 goto err;
122 loop:
123 /* make a random number and set the top and bottom bits */
124 if (add == NULL) {
125 if (!probable_prime(ret, bits, safe, mods, ctx))
126 goto err;
127 } else {
128 if (!probable_prime_dh(ret, bits, safe, mods, add, rem, ctx))
129 goto err;
130 }
131
132 if (!BN_GENCB_call(cb, 0, c1++))
133 /* aborted */
134 goto err;
135
136 if (!safe) {
137 i = BN_is_prime_fasttest_ex(ret, checks, ctx, 0, cb);
138 if (i == -1)
139 goto err;
140 if (i == 0)
141 goto loop;
142 } else {
143 /*
144 * for "safe prime" generation, check that (p-1)/2 is prime. Since a
145 * prime is odd, We just need to divide by 2
146 */
147 if (!BN_rshift1(t, ret))
148 goto err;
149
150 for (i = 0; i < checks; i++) {
151 j = BN_is_prime_fasttest_ex(ret, 1, ctx, 0, cb);
152 if (j == -1)
153 goto err;
154 if (j == 0)
155 goto loop;
156
157 j = BN_is_prime_fasttest_ex(t, 1, ctx, 0, cb);
158 if (j == -1)
159 goto err;
160 if (j == 0)
161 goto loop;
162
163 if (!BN_GENCB_call(cb, 2, c1 - 1))
164 goto err;
165 /* We have a safe prime test pass */
166 }
167 }
168 /* we have a prime :-) */
169 found = 1;
170 err:
171 OPENSSL_free(mods);
172 BN_CTX_end(ctx);
173 bn_check_top(ret);
174 return found;
175 }
176
177 #ifndef FIPS_MODE
178 int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe,
179 const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb)
180 {
181 BN_CTX *ctx = BN_CTX_new();
182 int retval;
183
184 if (ctx == NULL)
185 return 0;
186
187 retval = BN_generate_prime_ex2(ret, bits, safe, add, rem, cb, ctx);
188
189 BN_CTX_free(ctx);
190 return retval;
191 }
192 #endif
193
194 int BN_is_prime_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,
195 BN_GENCB *cb)
196 {
197 return BN_is_prime_fasttest_ex(a, checks, ctx_passed, 0, cb);
198 }
199
200 /* See FIPS 186-4 C.3.1 Miller Rabin Probabilistic Primality Test. */
201 int BN_is_prime_fasttest_ex(const BIGNUM *w, int checks, BN_CTX *ctx,
202 int do_trial_division, BN_GENCB *cb)
203 {
204 int i, status, ret = -1;
205 #ifndef FIPS_MODE
206 BN_CTX *ctxlocal = NULL;
207 #else
208
209 if (ctx == NULL)
210 return -1;
211 #endif
212
213 /* w must be bigger than 1 */
214 if (BN_cmp(w, BN_value_one()) <= 0)
215 return 0;
216
217 /* w must be odd */
218 if (BN_is_odd(w)) {
219 /* Take care of the really small prime 3 */
220 if (BN_is_word(w, 3))
221 return 1;
222 } else {
223 /* 2 is the only even prime */
224 return BN_is_word(w, 2);
225 }
226
227 /* first look for small factors */
228 if (do_trial_division) {
229 for (i = 1; i < NUMPRIMES; i++) {
230 BN_ULONG mod = BN_mod_word(w, primes[i]);
231 if (mod == (BN_ULONG)-1)
232 return -1;
233 if (mod == 0)
234 return BN_is_word(w, primes[i]);
235 }
236 if (!BN_GENCB_call(cb, 1, -1))
237 return -1;
238 }
239 #ifndef FIPS_MODE
240 if (ctx == NULL && (ctxlocal = ctx = BN_CTX_new()) == NULL)
241 goto err;
242 #endif
243
244 ret = bn_miller_rabin_is_prime(w, checks, ctx, cb, 0, &status);
245 if (!ret)
246 goto err;
247 ret = (status == BN_PRIMETEST_PROBABLY_PRIME);
248 err:
249 #ifndef FIPS_MODE
250 BN_CTX_free(ctxlocal);
251 #endif
252 return ret;
253 }
254
255 /*
256 * Refer to FIPS 186-4 C.3.2 Enhanced Miller-Rabin Probabilistic Primality Test.
257 * OR C.3.1 Miller-Rabin Probabilistic Primality Test (if enhanced is zero).
258 * The Step numbers listed in the code refer to the enhanced case.
259 *
260 * if enhanced is set, then status returns one of the following:
261 * BN_PRIMETEST_PROBABLY_PRIME
262 * BN_PRIMETEST_COMPOSITE_WITH_FACTOR
263 * BN_PRIMETEST_COMPOSITE_NOT_POWER_OF_PRIME
264 * if enhanced is zero, then status returns either
265 * BN_PRIMETEST_PROBABLY_PRIME or
266 * BN_PRIMETEST_COMPOSITE
267 *
268 * returns 0 if there was an error, otherwise it returns 1.
269 */
270 int bn_miller_rabin_is_prime(const BIGNUM *w, int iterations, BN_CTX *ctx,
271 BN_GENCB *cb, int enhanced, int *status)
272 {
273 int i, j, a, ret = 0;
274 BIGNUM *g, *w1, *w3, *x, *m, *z, *b;
275 BN_MONT_CTX *mont = NULL;
276
277 /* w must be odd */
278 if (!BN_is_odd(w))
279 return 0;
280
281 BN_CTX_start(ctx);
282 g = BN_CTX_get(ctx);
283 w1 = BN_CTX_get(ctx);
284 w3 = BN_CTX_get(ctx);
285 x = BN_CTX_get(ctx);
286 m = BN_CTX_get(ctx);
287 z = BN_CTX_get(ctx);
288 b = BN_CTX_get(ctx);
289
290 if (!(b != NULL
291 /* w1 := w - 1 */
292 && BN_copy(w1, w)
293 && BN_sub_word(w1, 1)
294 /* w3 := w - 3 */
295 && BN_copy(w3, w)
296 && BN_sub_word(w3, 3)))
297 goto err;
298
299 /* check w is larger than 3, otherwise the random b will be too small */
300 if (BN_is_zero(w3) || BN_is_negative(w3))
301 goto err;
302
303 /* (Step 1) Calculate largest integer 'a' such that 2^a divides w-1 */
304 a = 1;
305 while (!BN_is_bit_set(w1, a))
306 a++;
307 /* (Step 2) m = (w-1) / 2^a */
308 if (!BN_rshift(m, w1, a))
309 goto err;
310
311 /* Montgomery setup for computations mod a */
312 mont = BN_MONT_CTX_new();
313 if (mont == NULL || !BN_MONT_CTX_set(mont, w, ctx))
314 goto err;
315
316 if (iterations == BN_prime_checks)
317 iterations = BN_prime_checks_for_size(BN_num_bits(w));
318
319 /* (Step 4) */
320 for (i = 0; i < iterations; ++i) {
321 /* (Step 4.1) obtain a Random string of bits b where 1 < b < w-1 */
322 if (!BN_priv_rand_range_ex(b, w3, ctx)
323 || !BN_add_word(b, 2)) /* 1 < b < w-1 */
324 goto err;
325
326 if (enhanced) {
327 /* (Step 4.3) */
328 if (!BN_gcd(g, b, w, ctx))
329 goto err;
330 /* (Step 4.4) */
331 if (!BN_is_one(g)) {
332 *status = BN_PRIMETEST_COMPOSITE_WITH_FACTOR;
333 ret = 1;
334 goto err;
335 }
336 }
337 /* (Step 4.5) z = b^m mod w */
338 if (!BN_mod_exp_mont(z, b, m, w, ctx, mont))
339 goto err;
340 /* (Step 4.6) if (z = 1 or z = w-1) */
341 if (BN_is_one(z) || BN_cmp(z, w1) == 0)
342 goto outer_loop;
343 /* (Step 4.7) for j = 1 to a-1 */
344 for (j = 1; j < a ; ++j) {
345 /* (Step 4.7.1 - 4.7.2) x = z. z = x^2 mod w */
346 if (!BN_copy(x, z) || !BN_mod_mul(z, x, x, w, ctx))
347 goto err;
348 /* (Step 4.7.3) */
349 if (BN_cmp(z, w1) == 0)
350 goto outer_loop;
351 /* (Step 4.7.4) */
352 if (BN_is_one(z))
353 goto composite;
354 }
355 /* At this point z = b^((w-1)/2) mod w */
356 /* (Steps 4.8 - 4.9) x = z, z = x^2 mod w */
357 if (!BN_copy(x, z) || !BN_mod_mul(z, x, x, w, ctx))
358 goto err;
359 /* (Step 4.10) */
360 if (BN_is_one(z))
361 goto composite;
362 /* (Step 4.11) x = b^(w-1) mod w */
363 if (!BN_copy(x, z))
364 goto err;
365 composite:
366 if (enhanced) {
367 /* (Step 4.1.2) g = GCD(x-1, w) */
368 if (!BN_sub_word(x, 1) || !BN_gcd(g, x, w, ctx))
369 goto err;
370 /* (Steps 4.1.3 - 4.1.4) */
371 if (BN_is_one(g))
372 *status = BN_PRIMETEST_COMPOSITE_NOT_POWER_OF_PRIME;
373 else
374 *status = BN_PRIMETEST_COMPOSITE_WITH_FACTOR;
375 } else {
376 *status = BN_PRIMETEST_COMPOSITE;
377 }
378 ret = 1;
379 goto err;
380 outer_loop: ;
381 /* (Step 4.1.5) */
382 if (!BN_GENCB_call(cb, 1, i))
383 goto err;
384 }
385 /* (Step 5) */
386 *status = BN_PRIMETEST_PROBABLY_PRIME;
387 ret = 1;
388 err:
389 BN_clear(g);
390 BN_clear(w1);
391 BN_clear(w3);
392 BN_clear(x);
393 BN_clear(m);
394 BN_clear(z);
395 BN_clear(b);
396 BN_CTX_end(ctx);
397 BN_MONT_CTX_free(mont);
398 return ret;
399 }
400
401 static int probable_prime(BIGNUM *rnd, int bits, int safe, prime_t *mods,
402 BN_CTX *ctx)
403 {
404 int i;
405 BN_ULONG delta;
406 BN_ULONG maxdelta = BN_MASK2 - primes[NUMPRIMES - 1];
407
408 again:
409 /* TODO: Not all primes are private */
410 if (!BN_priv_rand_ex(rnd, bits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ODD, ctx))
411 return 0;
412 if (safe && !BN_set_bit(rnd, 1))
413 return 0;
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)
418 return 0;
419 mods[i] = (prime_t) mod;
420 }
421 delta = 0;
422 loop:
423 for (i = 1; i < NUMPRIMES; i++) {
424 /*
425 * check that rnd is a prime and also that
426 * gcd(rnd-1,primes) == 1 (except for 2)
427 * do the second check only if we are interested in safe primes
428 * in the case that the candidate prime is a single word then
429 * we check only the primes up to sqrt(rnd)
430 */
431 if (bits <= 31 && delta <= 0x7fffffff
432 && square(primes[i]) > BN_get_word(rnd) + delta)
433 break;
434 if (safe ? (mods[i] + delta) % primes[i] <= 1
435 : (mods[i] + delta) % primes[i] == 0) {
436 delta += safe ? 4 : 2;
437 if (delta > maxdelta)
438 goto again;
439 goto loop;
440 }
441 }
442 if (!BN_add_word(rnd, delta))
443 return 0;
444 if (BN_num_bits(rnd) != bits)
445 goto again;
446 bn_check_top(rnd);
447 return 1;
448 }
449
450 static int probable_prime_dh(BIGNUM *rnd, int bits, int safe, prime_t *mods,
451 const BIGNUM *add, const BIGNUM *rem,
452 BN_CTX *ctx)
453 {
454 int i, ret = 0;
455 BIGNUM *t1;
456 BN_ULONG delta;
457 BN_ULONG maxdelta = BN_MASK2 - primes[NUMPRIMES - 1];
458
459 BN_CTX_start(ctx);
460 if ((t1 = BN_CTX_get(ctx)) == NULL)
461 goto err;
462
463 if (maxdelta > BN_MASK2 - BN_get_word(add))
464 maxdelta = BN_MASK2 - BN_get_word(add);
465
466 again:
467 if (!BN_rand_ex(rnd, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD, ctx))
468 goto err;
469
470 /* we need ((rnd-rem) % add) == 0 */
471
472 if (!BN_mod(t1, rnd, add, ctx))
473 goto err;
474 if (!BN_sub(rnd, rnd, t1))
475 goto err;
476 if (rem == NULL) {
477 if (!BN_add_word(rnd, safe ? 3u : 1u))
478 goto err;
479 } else {
480 if (!BN_add(rnd, rnd, rem))
481 goto err;
482 }
483
484 if (BN_num_bits(rnd) < bits
485 || BN_get_word(rnd) < (safe ? 5u : 3u)) {
486 if (!BN_add(rnd, rnd, add))
487 goto err;
488 }
489
490 /* we now have a random number 'rnd' to test. */
491 for (i = 1; i < NUMPRIMES; i++) {
492 BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);
493 if (mod == (BN_ULONG)-1)
494 goto err;
495 mods[i] = (prime_t) mod;
496 }
497 delta = 0;
498 loop:
499 for (i = 1; i < NUMPRIMES; i++) {
500 /* check that rnd is a prime */
501 if (bits <= 31 && delta <= 0x7fffffff
502 && square(primes[i]) > BN_get_word(rnd) + delta)
503 break;
504 /* rnd mod p == 1 implies q = (rnd-1)/2 is divisible by p */
505 if (safe ? (mods[i] + delta) % primes[i] <= 1
506 : (mods[i] + delta) % primes[i] == 0) {
507 delta += BN_get_word(add);
508 if (delta > maxdelta)
509 goto again;
510 goto loop;
511 }
512 }
513 if (!BN_add_word(rnd, delta))
514 goto err;
515 ret = 1;
516
517 err:
518 BN_CTX_end(ctx);
519 bn_check_top(rnd);
520 return ret;
521 }