1 | /*
|
---|
2 | * Copyright 1995-2022 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 | static int bn_is_prime_int(const BIGNUM *w, int checks, BN_CTX *ctx,
|
---|
28 | int do_trial_division, BN_GENCB *cb);
|
---|
29 |
|
---|
30 | #define square(x) ((BN_ULONG)(x) * (BN_ULONG)(x))
|
---|
31 |
|
---|
32 | #if BN_BITS2 == 64
|
---|
33 | # define BN_DEF(lo, hi) (BN_ULONG)hi<<32|lo
|
---|
34 | #else
|
---|
35 | # define BN_DEF(lo, hi) lo, hi
|
---|
36 | #endif
|
---|
37 |
|
---|
38 | /*
|
---|
39 | * See SP800 89 5.3.3 (Step f)
|
---|
40 | * The product of the set of primes ranging from 3 to 751
|
---|
41 | * Generated using process in test/bn_internal_test.c test_bn_small_factors().
|
---|
42 | * This includes 751 (which is not currently included in SP 800-89).
|
---|
43 | */
|
---|
44 | static const BN_ULONG small_prime_factors[] = {
|
---|
45 | BN_DEF(0x3ef4e3e1, 0xc4309333), BN_DEF(0xcd2d655f, 0x71161eb6),
|
---|
46 | BN_DEF(0x0bf94862, 0x95e2238c), BN_DEF(0x24f7912b, 0x3eb233d3),
|
---|
47 | BN_DEF(0xbf26c483, 0x6b55514b), BN_DEF(0x5a144871, 0x0a84d817),
|
---|
48 | BN_DEF(0x9b82210a, 0x77d12fee), BN_DEF(0x97f050b3, 0xdb5b93c2),
|
---|
49 | BN_DEF(0x4d6c026b, 0x4acad6b9), BN_DEF(0x54aec893, 0xeb7751f3),
|
---|
50 | BN_DEF(0x36bc85c4, 0xdba53368), BN_DEF(0x7f5ec78e, 0xd85a1b28),
|
---|
51 | BN_DEF(0x6b322244, 0x2eb072d8), BN_DEF(0x5e2b3aea, 0xbba51112),
|
---|
52 | BN_DEF(0x0e2486bf, 0x36ed1a6c), BN_DEF(0xec0c5727, 0x5f270460),
|
---|
53 | (BN_ULONG)0x000017b1
|
---|
54 | };
|
---|
55 |
|
---|
56 | #define BN_SMALL_PRIME_FACTORS_TOP OSSL_NELEM(small_prime_factors)
|
---|
57 | static const BIGNUM _bignum_small_prime_factors = {
|
---|
58 | (BN_ULONG *)small_prime_factors,
|
---|
59 | BN_SMALL_PRIME_FACTORS_TOP,
|
---|
60 | BN_SMALL_PRIME_FACTORS_TOP,
|
---|
61 | 0,
|
---|
62 | BN_FLG_STATIC_DATA
|
---|
63 | };
|
---|
64 |
|
---|
65 | const BIGNUM *ossl_bn_get0_small_factors(void)
|
---|
66 | {
|
---|
67 | return &_bignum_small_prime_factors;
|
---|
68 | }
|
---|
69 |
|
---|
70 | /*
|
---|
71 | * Calculate the number of trial divisions that gives the best speed in
|
---|
72 | * combination with Miller-Rabin prime test, based on the sized of the prime.
|
---|
73 | */
|
---|
74 | static int calc_trial_divisions(int bits)
|
---|
75 | {
|
---|
76 | if (bits <= 512)
|
---|
77 | return 64;
|
---|
78 | else if (bits <= 1024)
|
---|
79 | return 128;
|
---|
80 | else if (bits <= 2048)
|
---|
81 | return 384;
|
---|
82 | else if (bits <= 4096)
|
---|
83 | return 1024;
|
---|
84 | return NUMPRIMES;
|
---|
85 | }
|
---|
86 |
|
---|
87 | /*
|
---|
88 | * Use a minimum of 64 rounds of Miller-Rabin, which should give a false
|
---|
89 | * positive rate of 2^-128. If the size of the prime is larger than 2048
|
---|
90 | * the user probably wants a higher security level than 128, so switch
|
---|
91 | * to 128 rounds giving a false positive rate of 2^-256.
|
---|
92 | * Returns the number of rounds.
|
---|
93 | */
|
---|
94 | static int bn_mr_min_checks(int bits)
|
---|
95 | {
|
---|
96 | if (bits > 2048)
|
---|
97 | return 128;
|
---|
98 | return 64;
|
---|
99 | }
|
---|
100 |
|
---|
101 | int BN_GENCB_call(BN_GENCB *cb, int a, int b)
|
---|
102 | {
|
---|
103 | /* No callback means continue */
|
---|
104 | if (!cb)
|
---|
105 | return 1;
|
---|
106 | switch (cb->ver) {
|
---|
107 | case 1:
|
---|
108 | /* Deprecated-style callbacks */
|
---|
109 | if (!cb->cb.cb_1)
|
---|
110 | return 1;
|
---|
111 | cb->cb.cb_1(a, b, cb->arg);
|
---|
112 | return 1;
|
---|
113 | case 2:
|
---|
114 | /* New-style callbacks */
|
---|
115 | return cb->cb.cb_2(a, b, cb);
|
---|
116 | default:
|
---|
117 | break;
|
---|
118 | }
|
---|
119 | /* Unrecognised callback type */
|
---|
120 | return 0;
|
---|
121 | }
|
---|
122 |
|
---|
123 | int BN_generate_prime_ex2(BIGNUM *ret, int bits, int safe,
|
---|
124 | const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb,
|
---|
125 | BN_CTX *ctx)
|
---|
126 | {
|
---|
127 | BIGNUM *t;
|
---|
128 | int found = 0;
|
---|
129 | int i, j, c1 = 0;
|
---|
130 | prime_t *mods = NULL;
|
---|
131 | int checks = bn_mr_min_checks(bits);
|
---|
132 |
|
---|
133 | if (bits < 2) {
|
---|
134 | /* There are no prime numbers this small. */
|
---|
135 | ERR_raise(ERR_LIB_BN, BN_R_BITS_TOO_SMALL);
|
---|
136 | return 0;
|
---|
137 | } else if (add == NULL && safe && bits < 6 && bits != 3) {
|
---|
138 | /*
|
---|
139 | * The smallest safe prime (7) is three bits.
|
---|
140 | * But the following two safe primes with less than 6 bits (11, 23)
|
---|
141 | * are unreachable for BN_rand with BN_RAND_TOP_TWO.
|
---|
142 | */
|
---|
143 | ERR_raise(ERR_LIB_BN, BN_R_BITS_TOO_SMALL);
|
---|
144 | return 0;
|
---|
145 | }
|
---|
146 |
|
---|
147 | mods = OPENSSL_zalloc(sizeof(*mods) * NUMPRIMES);
|
---|
148 | if (mods == NULL) {
|
---|
149 | ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
|
---|
150 | return 0;
|
---|
151 | }
|
---|
152 |
|
---|
153 | BN_CTX_start(ctx);
|
---|
154 | t = BN_CTX_get(ctx);
|
---|
155 | if (t == NULL)
|
---|
156 | goto err;
|
---|
157 | loop:
|
---|
158 | /* make a random number and set the top and bottom bits */
|
---|
159 | if (add == NULL) {
|
---|
160 | if (!probable_prime(ret, bits, safe, mods, ctx))
|
---|
161 | goto err;
|
---|
162 | } else {
|
---|
163 | if (!probable_prime_dh(ret, bits, safe, mods, add, rem, ctx))
|
---|
164 | goto err;
|
---|
165 | }
|
---|
166 |
|
---|
167 | if (!BN_GENCB_call(cb, 0, c1++))
|
---|
168 | /* aborted */
|
---|
169 | goto err;
|
---|
170 |
|
---|
171 | if (!safe) {
|
---|
172 | i = bn_is_prime_int(ret, checks, ctx, 0, cb);
|
---|
173 | if (i == -1)
|
---|
174 | goto err;
|
---|
175 | if (i == 0)
|
---|
176 | goto loop;
|
---|
177 | } else {
|
---|
178 | /*
|
---|
179 | * for "safe prime" generation, check that (p-1)/2 is prime. Since a
|
---|
180 | * prime is odd, We just need to divide by 2
|
---|
181 | */
|
---|
182 | if (!BN_rshift1(t, ret))
|
---|
183 | goto err;
|
---|
184 |
|
---|
185 | for (i = 0; i < checks; i++) {
|
---|
186 | j = bn_is_prime_int(ret, 1, ctx, 0, cb);
|
---|
187 | if (j == -1)
|
---|
188 | goto err;
|
---|
189 | if (j == 0)
|
---|
190 | goto loop;
|
---|
191 |
|
---|
192 | j = bn_is_prime_int(t, 1, ctx, 0, cb);
|
---|
193 | if (j == -1)
|
---|
194 | goto err;
|
---|
195 | if (j == 0)
|
---|
196 | goto loop;
|
---|
197 |
|
---|
198 | if (!BN_GENCB_call(cb, 2, c1 - 1))
|
---|
199 | goto err;
|
---|
200 | /* We have a safe prime test pass */
|
---|
201 | }
|
---|
202 | }
|
---|
203 | /* we have a prime :-) */
|
---|
204 | found = 1;
|
---|
205 | err:
|
---|
206 | OPENSSL_free(mods);
|
---|
207 | BN_CTX_end(ctx);
|
---|
208 | bn_check_top(ret);
|
---|
209 | return found;
|
---|
210 | }
|
---|
211 |
|
---|
212 | #ifndef FIPS_MODULE
|
---|
213 | int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe,
|
---|
214 | const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb)
|
---|
215 | {
|
---|
216 | BN_CTX *ctx = BN_CTX_new();
|
---|
217 | int retval;
|
---|
218 |
|
---|
219 | if (ctx == NULL)
|
---|
220 | return 0;
|
---|
221 |
|
---|
222 | retval = BN_generate_prime_ex2(ret, bits, safe, add, rem, cb, ctx);
|
---|
223 |
|
---|
224 | BN_CTX_free(ctx);
|
---|
225 | return retval;
|
---|
226 | }
|
---|
227 | #endif
|
---|
228 |
|
---|
229 | #ifndef OPENSSL_NO_DEPRECATED_3_0
|
---|
230 | int BN_is_prime_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,
|
---|
231 | BN_GENCB *cb)
|
---|
232 | {
|
---|
233 | return ossl_bn_check_prime(a, checks, ctx_passed, 0, cb);
|
---|
234 | }
|
---|
235 |
|
---|
236 | int BN_is_prime_fasttest_ex(const BIGNUM *w, int checks, BN_CTX *ctx,
|
---|
237 | int do_trial_division, BN_GENCB *cb)
|
---|
238 | {
|
---|
239 | return ossl_bn_check_prime(w, checks, ctx, do_trial_division, cb);
|
---|
240 | }
|
---|
241 | #endif
|
---|
242 |
|
---|
243 | /* Wrapper around bn_is_prime_int that sets the minimum number of checks */
|
---|
244 | int ossl_bn_check_prime(const BIGNUM *w, int checks, BN_CTX *ctx,
|
---|
245 | int do_trial_division, BN_GENCB *cb)
|
---|
246 | {
|
---|
247 | int min_checks = bn_mr_min_checks(BN_num_bits(w));
|
---|
248 |
|
---|
249 | if (checks < min_checks)
|
---|
250 | checks = min_checks;
|
---|
251 |
|
---|
252 | return bn_is_prime_int(w, checks, ctx, do_trial_division, cb);
|
---|
253 | }
|
---|
254 |
|
---|
255 | /*
|
---|
256 | * Use this only for key generation.
|
---|
257 | * It always uses trial division. The number of checks
|
---|
258 | * (MR rounds) passed in is used without being clamped to a minimum value.
|
---|
259 | */
|
---|
260 | int ossl_bn_check_generated_prime(const BIGNUM *w, int checks, BN_CTX *ctx,
|
---|
261 | BN_GENCB *cb)
|
---|
262 | {
|
---|
263 | return bn_is_prime_int(w, checks, ctx, 1, cb);
|
---|
264 | }
|
---|
265 |
|
---|
266 | int BN_check_prime(const BIGNUM *p, BN_CTX *ctx, BN_GENCB *cb)
|
---|
267 | {
|
---|
268 | return ossl_bn_check_prime(p, 0, ctx, 1, cb);
|
---|
269 | }
|
---|
270 |
|
---|
271 | /*
|
---|
272 | * Tests that |w| is probably prime
|
---|
273 | * See FIPS 186-4 C.3.1 Miller Rabin Probabilistic Primality Test.
|
---|
274 | *
|
---|
275 | * Returns 0 when composite, 1 when probable prime, -1 on error.
|
---|
276 | */
|
---|
277 | static int bn_is_prime_int(const BIGNUM *w, int checks, BN_CTX *ctx,
|
---|
278 | int do_trial_division, BN_GENCB *cb)
|
---|
279 | {
|
---|
280 | int i, status, ret = -1;
|
---|
281 | #ifndef FIPS_MODULE
|
---|
282 | BN_CTX *ctxlocal = NULL;
|
---|
283 | #else
|
---|
284 |
|
---|
285 | if (ctx == NULL)
|
---|
286 | return -1;
|
---|
287 | #endif
|
---|
288 |
|
---|
289 | /* w must be bigger than 1 */
|
---|
290 | if (BN_cmp(w, BN_value_one()) <= 0)
|
---|
291 | return 0;
|
---|
292 |
|
---|
293 | /* w must be odd */
|
---|
294 | if (BN_is_odd(w)) {
|
---|
295 | /* Take care of the really small prime 3 */
|
---|
296 | if (BN_is_word(w, 3))
|
---|
297 | return 1;
|
---|
298 | } else {
|
---|
299 | /* 2 is the only even prime */
|
---|
300 | return BN_is_word(w, 2);
|
---|
301 | }
|
---|
302 |
|
---|
303 | /* first look for small factors */
|
---|
304 | if (do_trial_division) {
|
---|
305 | int trial_divisions = calc_trial_divisions(BN_num_bits(w));
|
---|
306 |
|
---|
307 | for (i = 1; i < trial_divisions; i++) {
|
---|
308 | BN_ULONG mod = BN_mod_word(w, primes[i]);
|
---|
309 | if (mod == (BN_ULONG)-1)
|
---|
310 | return -1;
|
---|
311 | if (mod == 0)
|
---|
312 | return BN_is_word(w, primes[i]);
|
---|
313 | }
|
---|
314 | if (!BN_GENCB_call(cb, 1, -1))
|
---|
315 | return -1;
|
---|
316 | }
|
---|
317 | #ifndef FIPS_MODULE
|
---|
318 | if (ctx == NULL && (ctxlocal = ctx = BN_CTX_new()) == NULL)
|
---|
319 | goto err;
|
---|
320 | #endif
|
---|
321 |
|
---|
322 | if (!ossl_bn_miller_rabin_is_prime(w, checks, ctx, cb, 0, &status)) {
|
---|
323 | ret = -1;
|
---|
324 | goto err;
|
---|
325 | }
|
---|
326 | ret = (status == BN_PRIMETEST_PROBABLY_PRIME);
|
---|
327 | err:
|
---|
328 | #ifndef FIPS_MODULE
|
---|
329 | BN_CTX_free(ctxlocal);
|
---|
330 | #endif
|
---|
331 | return ret;
|
---|
332 | }
|
---|
333 |
|
---|
334 | /*
|
---|
335 | * Refer to FIPS 186-4 C.3.2 Enhanced Miller-Rabin Probabilistic Primality Test.
|
---|
336 | * OR C.3.1 Miller-Rabin Probabilistic Primality Test (if enhanced is zero).
|
---|
337 | * The Step numbers listed in the code refer to the enhanced case.
|
---|
338 | *
|
---|
339 | * if enhanced is set, then status returns one of the following:
|
---|
340 | * BN_PRIMETEST_PROBABLY_PRIME
|
---|
341 | * BN_PRIMETEST_COMPOSITE_WITH_FACTOR
|
---|
342 | * BN_PRIMETEST_COMPOSITE_NOT_POWER_OF_PRIME
|
---|
343 | * if enhanced is zero, then status returns either
|
---|
344 | * BN_PRIMETEST_PROBABLY_PRIME or
|
---|
345 | * BN_PRIMETEST_COMPOSITE
|
---|
346 | *
|
---|
347 | * returns 0 if there was an error, otherwise it returns 1.
|
---|
348 | */
|
---|
349 | int ossl_bn_miller_rabin_is_prime(const BIGNUM *w, int iterations, BN_CTX *ctx,
|
---|
350 | BN_GENCB *cb, int enhanced, int *status)
|
---|
351 | {
|
---|
352 | int i, j, a, ret = 0;
|
---|
353 | BIGNUM *g, *w1, *w3, *x, *m, *z, *b;
|
---|
354 | BN_MONT_CTX *mont = NULL;
|
---|
355 |
|
---|
356 | /* w must be odd */
|
---|
357 | if (!BN_is_odd(w))
|
---|
358 | return 0;
|
---|
359 |
|
---|
360 | BN_CTX_start(ctx);
|
---|
361 | g = BN_CTX_get(ctx);
|
---|
362 | w1 = BN_CTX_get(ctx);
|
---|
363 | w3 = BN_CTX_get(ctx);
|
---|
364 | x = BN_CTX_get(ctx);
|
---|
365 | m = BN_CTX_get(ctx);
|
---|
366 | z = BN_CTX_get(ctx);
|
---|
367 | b = BN_CTX_get(ctx);
|
---|
368 |
|
---|
369 | if (!(b != NULL
|
---|
370 | /* w1 := w - 1 */
|
---|
371 | && BN_copy(w1, w)
|
---|
372 | && BN_sub_word(w1, 1)
|
---|
373 | /* w3 := w - 3 */
|
---|
374 | && BN_copy(w3, w)
|
---|
375 | && BN_sub_word(w3, 3)))
|
---|
376 | goto err;
|
---|
377 |
|
---|
378 | /* check w is larger than 3, otherwise the random b will be too small */
|
---|
379 | if (BN_is_zero(w3) || BN_is_negative(w3))
|
---|
380 | goto err;
|
---|
381 |
|
---|
382 | /* (Step 1) Calculate largest integer 'a' such that 2^a divides w-1 */
|
---|
383 | a = 1;
|
---|
384 | while (!BN_is_bit_set(w1, a))
|
---|
385 | a++;
|
---|
386 | /* (Step 2) m = (w-1) / 2^a */
|
---|
387 | if (!BN_rshift(m, w1, a))
|
---|
388 | goto err;
|
---|
389 |
|
---|
390 | /* Montgomery setup for computations mod a */
|
---|
391 | mont = BN_MONT_CTX_new();
|
---|
392 | if (mont == NULL || !BN_MONT_CTX_set(mont, w, ctx))
|
---|
393 | goto err;
|
---|
394 |
|
---|
395 | if (iterations == 0)
|
---|
396 | iterations = bn_mr_min_checks(BN_num_bits(w));
|
---|
397 |
|
---|
398 | /* (Step 4) */
|
---|
399 | for (i = 0; i < iterations; ++i) {
|
---|
400 | /* (Step 4.1) obtain a Random string of bits b where 1 < b < w-1 */
|
---|
401 | if (!BN_priv_rand_range_ex(b, w3, 0, ctx)
|
---|
402 | || !BN_add_word(b, 2)) /* 1 < b < w-1 */
|
---|
403 | goto err;
|
---|
404 |
|
---|
405 | if (enhanced) {
|
---|
406 | /* (Step 4.3) */
|
---|
407 | if (!BN_gcd(g, b, w, ctx))
|
---|
408 | goto err;
|
---|
409 | /* (Step 4.4) */
|
---|
410 | if (!BN_is_one(g)) {
|
---|
411 | *status = BN_PRIMETEST_COMPOSITE_WITH_FACTOR;
|
---|
412 | ret = 1;
|
---|
413 | goto err;
|
---|
414 | }
|
---|
415 | }
|
---|
416 | /* (Step 4.5) z = b^m mod w */
|
---|
417 | if (!BN_mod_exp_mont(z, b, m, w, ctx, mont))
|
---|
418 | goto err;
|
---|
419 | /* (Step 4.6) if (z = 1 or z = w-1) */
|
---|
420 | if (BN_is_one(z) || BN_cmp(z, w1) == 0)
|
---|
421 | goto outer_loop;
|
---|
422 | /* (Step 4.7) for j = 1 to a-1 */
|
---|
423 | for (j = 1; j < a ; ++j) {
|
---|
424 | /* (Step 4.7.1 - 4.7.2) x = z. z = x^2 mod w */
|
---|
425 | if (!BN_copy(x, z) || !BN_mod_mul(z, x, x, w, ctx))
|
---|
426 | goto err;
|
---|
427 | /* (Step 4.7.3) */
|
---|
428 | if (BN_cmp(z, w1) == 0)
|
---|
429 | goto outer_loop;
|
---|
430 | /* (Step 4.7.4) */
|
---|
431 | if (BN_is_one(z))
|
---|
432 | goto composite;
|
---|
433 | }
|
---|
434 | /* At this point z = b^((w-1)/2) mod w */
|
---|
435 | /* (Steps 4.8 - 4.9) x = z, z = x^2 mod w */
|
---|
436 | if (!BN_copy(x, z) || !BN_mod_mul(z, x, x, w, ctx))
|
---|
437 | goto err;
|
---|
438 | /* (Step 4.10) */
|
---|
439 | if (BN_is_one(z))
|
---|
440 | goto composite;
|
---|
441 | /* (Step 4.11) x = b^(w-1) mod w */
|
---|
442 | if (!BN_copy(x, z))
|
---|
443 | goto err;
|
---|
444 | composite:
|
---|
445 | if (enhanced) {
|
---|
446 | /* (Step 4.1.2) g = GCD(x-1, w) */
|
---|
447 | if (!BN_sub_word(x, 1) || !BN_gcd(g, x, w, ctx))
|
---|
448 | goto err;
|
---|
449 | /* (Steps 4.1.3 - 4.1.4) */
|
---|
450 | if (BN_is_one(g))
|
---|
451 | *status = BN_PRIMETEST_COMPOSITE_NOT_POWER_OF_PRIME;
|
---|
452 | else
|
---|
453 | *status = BN_PRIMETEST_COMPOSITE_WITH_FACTOR;
|
---|
454 | } else {
|
---|
455 | *status = BN_PRIMETEST_COMPOSITE;
|
---|
456 | }
|
---|
457 | ret = 1;
|
---|
458 | goto err;
|
---|
459 | outer_loop: ;
|
---|
460 | /* (Step 4.1.5) */
|
---|
461 | if (!BN_GENCB_call(cb, 1, i))
|
---|
462 | goto err;
|
---|
463 | }
|
---|
464 | /* (Step 5) */
|
---|
465 | *status = BN_PRIMETEST_PROBABLY_PRIME;
|
---|
466 | ret = 1;
|
---|
467 | err:
|
---|
468 | BN_clear(g);
|
---|
469 | BN_clear(w1);
|
---|
470 | BN_clear(w3);
|
---|
471 | BN_clear(x);
|
---|
472 | BN_clear(m);
|
---|
473 | BN_clear(z);
|
---|
474 | BN_clear(b);
|
---|
475 | BN_CTX_end(ctx);
|
---|
476 | BN_MONT_CTX_free(mont);
|
---|
477 | return ret;
|
---|
478 | }
|
---|
479 |
|
---|
480 | /*
|
---|
481 | * Generate a random number of |bits| bits that is probably prime by sieving.
|
---|
482 | * If |safe| != 0, it generates a safe prime.
|
---|
483 | * |mods| is a preallocated array that gets reused when called again.
|
---|
484 | *
|
---|
485 | * The probably prime is saved in |rnd|.
|
---|
486 | *
|
---|
487 | * Returns 1 on success and 0 on error.
|
---|
488 | */
|
---|
489 | static int probable_prime(BIGNUM *rnd, int bits, int safe, prime_t *mods,
|
---|
490 | BN_CTX *ctx)
|
---|
491 | {
|
---|
492 | int i;
|
---|
493 | BN_ULONG delta;
|
---|
494 | int trial_divisions = calc_trial_divisions(bits);
|
---|
495 | BN_ULONG maxdelta = BN_MASK2 - primes[trial_divisions - 1];
|
---|
496 |
|
---|
497 | again:
|
---|
498 | if (!BN_priv_rand_ex(rnd, bits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ODD, 0,
|
---|
499 | ctx))
|
---|
500 | return 0;
|
---|
501 | if (safe && !BN_set_bit(rnd, 1))
|
---|
502 | return 0;
|
---|
503 | /* we now have a random number 'rnd' to test. */
|
---|
504 | for (i = 1; i < trial_divisions; i++) {
|
---|
505 | BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);
|
---|
506 | if (mod == (BN_ULONG)-1)
|
---|
507 | return 0;
|
---|
508 | mods[i] = (prime_t) mod;
|
---|
509 | }
|
---|
510 | delta = 0;
|
---|
511 | loop:
|
---|
512 | for (i = 1; i < trial_divisions; i++) {
|
---|
513 | /*
|
---|
514 | * check that rnd is a prime and also that
|
---|
515 | * gcd(rnd-1,primes) == 1 (except for 2)
|
---|
516 | * do the second check only if we are interested in safe primes
|
---|
517 | * in the case that the candidate prime is a single word then
|
---|
518 | * we check only the primes up to sqrt(rnd)
|
---|
519 | */
|
---|
520 | if (bits <= 31 && delta <= 0x7fffffff
|
---|
521 | && square(primes[i]) > BN_get_word(rnd) + delta)
|
---|
522 | break;
|
---|
523 | if (safe ? (mods[i] + delta) % primes[i] <= 1
|
---|
524 | : (mods[i] + delta) % primes[i] == 0) {
|
---|
525 | delta += safe ? 4 : 2;
|
---|
526 | if (delta > maxdelta)
|
---|
527 | goto again;
|
---|
528 | goto loop;
|
---|
529 | }
|
---|
530 | }
|
---|
531 | if (!BN_add_word(rnd, delta))
|
---|
532 | return 0;
|
---|
533 | if (BN_num_bits(rnd) != bits)
|
---|
534 | goto again;
|
---|
535 | bn_check_top(rnd);
|
---|
536 | return 1;
|
---|
537 | }
|
---|
538 |
|
---|
539 | /*
|
---|
540 | * Generate a random number |rnd| of |bits| bits that is probably prime
|
---|
541 | * and satisfies |rnd| % |add| == |rem| by sieving.
|
---|
542 | * If |safe| != 0, it generates a safe prime.
|
---|
543 | * |mods| is a preallocated array that gets reused when called again.
|
---|
544 | *
|
---|
545 | * Returns 1 on success and 0 on error.
|
---|
546 | */
|
---|
547 | static int probable_prime_dh(BIGNUM *rnd, int bits, int safe, prime_t *mods,
|
---|
548 | const BIGNUM *add, const BIGNUM *rem,
|
---|
549 | BN_CTX *ctx)
|
---|
550 | {
|
---|
551 | int i, ret = 0;
|
---|
552 | BIGNUM *t1;
|
---|
553 | BN_ULONG delta;
|
---|
554 | int trial_divisions = calc_trial_divisions(bits);
|
---|
555 | BN_ULONG maxdelta = BN_MASK2 - primes[trial_divisions - 1];
|
---|
556 |
|
---|
557 | BN_CTX_start(ctx);
|
---|
558 | if ((t1 = BN_CTX_get(ctx)) == NULL)
|
---|
559 | goto err;
|
---|
560 |
|
---|
561 | if (maxdelta > BN_MASK2 - BN_get_word(add))
|
---|
562 | maxdelta = BN_MASK2 - BN_get_word(add);
|
---|
563 |
|
---|
564 | again:
|
---|
565 | if (!BN_rand_ex(rnd, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD, 0, ctx))
|
---|
566 | goto err;
|
---|
567 |
|
---|
568 | /* we need ((rnd-rem) % add) == 0 */
|
---|
569 |
|
---|
570 | if (!BN_mod(t1, rnd, add, ctx))
|
---|
571 | goto err;
|
---|
572 | if (!BN_sub(rnd, rnd, t1))
|
---|
573 | goto err;
|
---|
574 | if (rem == NULL) {
|
---|
575 | if (!BN_add_word(rnd, safe ? 3u : 1u))
|
---|
576 | goto err;
|
---|
577 | } else {
|
---|
578 | if (!BN_add(rnd, rnd, rem))
|
---|
579 | goto err;
|
---|
580 | }
|
---|
581 |
|
---|
582 | if (BN_num_bits(rnd) < bits
|
---|
583 | || BN_get_word(rnd) < (safe ? 5u : 3u)) {
|
---|
584 | if (!BN_add(rnd, rnd, add))
|
---|
585 | goto err;
|
---|
586 | }
|
---|
587 |
|
---|
588 | /* we now have a random number 'rnd' to test. */
|
---|
589 | for (i = 1; i < trial_divisions; i++) {
|
---|
590 | BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);
|
---|
591 | if (mod == (BN_ULONG)-1)
|
---|
592 | goto err;
|
---|
593 | mods[i] = (prime_t) mod;
|
---|
594 | }
|
---|
595 | delta = 0;
|
---|
596 | loop:
|
---|
597 | for (i = 1; i < trial_divisions; i++) {
|
---|
598 | /* check that rnd is a prime */
|
---|
599 | if (bits <= 31 && delta <= 0x7fffffff
|
---|
600 | && square(primes[i]) > BN_get_word(rnd) + delta)
|
---|
601 | break;
|
---|
602 | /* rnd mod p == 1 implies q = (rnd-1)/2 is divisible by p */
|
---|
603 | if (safe ? (mods[i] + delta) % primes[i] <= 1
|
---|
604 | : (mods[i] + delta) % primes[i] == 0) {
|
---|
605 | delta += BN_get_word(add);
|
---|
606 | if (delta > maxdelta)
|
---|
607 | goto again;
|
---|
608 | goto loop;
|
---|
609 | }
|
---|
610 | }
|
---|
611 | if (!BN_add_word(rnd, delta))
|
---|
612 | goto err;
|
---|
613 | ret = 1;
|
---|
614 |
|
---|
615 | err:
|
---|
616 | BN_CTX_end(ctx);
|
---|
617 | bn_check_top(rnd);
|
---|
618 | return ret;
|
---|
619 | }
|
---|