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