]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - crypto/openssl/crypto/bn/bn_prime.c
MFV r353558: 10572 10579 Fix race in dnode_check_slots_free()
[FreeBSD/FreeBSD.git] / crypto / openssl / crypto / bn / bn_prime.c
1 /*
2  * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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_lcl.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 witness(BIGNUM *w, const BIGNUM *a, const BIGNUM *a1,
23                    const BIGNUM *a1_odd, int k, BN_CTX *ctx,
24                    BN_MONT_CTX *mont);
25 static int probable_prime(BIGNUM *rnd, int bits, prime_t *mods);
26 static int probable_prime_dh_safe(BIGNUM *rnd, int bits,
27                                   const BIGNUM *add, const BIGNUM *rem,
28                                   BN_CTX *ctx);
29
30 int BN_GENCB_call(BN_GENCB *cb, int a, int b)
31 {
32     /* No callback means continue */
33     if (!cb)
34         return 1;
35     switch (cb->ver) {
36     case 1:
37         /* Deprecated-style callbacks */
38         if (!cb->cb.cb_1)
39             return 1;
40         cb->cb.cb_1(a, b, cb->arg);
41         return 1;
42     case 2:
43         /* New-style callbacks */
44         return cb->cb.cb_2(a, b, cb);
45     default:
46         break;
47     }
48     /* Unrecognised callback type */
49     return 0;
50 }
51
52 int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe,
53                          const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb)
54 {
55     BIGNUM *t;
56     int found = 0;
57     int i, j, c1 = 0;
58     BN_CTX *ctx = NULL;
59     prime_t *mods = NULL;
60     int checks = BN_prime_checks_for_size(bits);
61
62     if (bits < 2) {
63         /* There are no prime numbers this small. */
64         BNerr(BN_F_BN_GENERATE_PRIME_EX, BN_R_BITS_TOO_SMALL);
65         return 0;
66     } else if (add == NULL && safe && bits < 6 && bits != 3) {
67         /*
68          * The smallest safe prime (7) is three bits.
69          * But the following two safe primes with less than 6 bits (11, 23)
70          * are unreachable for BN_rand with BN_RAND_TOP_TWO.
71          */
72         BNerr(BN_F_BN_GENERATE_PRIME_EX, BN_R_BITS_TOO_SMALL);
73         return 0;
74     }
75
76     mods = OPENSSL_zalloc(sizeof(*mods) * NUMPRIMES);
77     if (mods == NULL)
78         goto err;
79
80     ctx = BN_CTX_new();
81     if (ctx == NULL)
82         goto err;
83     BN_CTX_start(ctx);
84     t = BN_CTX_get(ctx);
85     if (t == NULL)
86         goto err;
87  loop:
88     /* make a random number and set the top and bottom bits */
89     if (add == NULL) {
90         if (!probable_prime(ret, bits, mods))
91             goto err;
92     } else {
93         if (safe) {
94             if (!probable_prime_dh_safe(ret, bits, add, rem, ctx))
95                 goto err;
96         } else {
97             if (!bn_probable_prime_dh(ret, bits, add, rem, ctx))
98                 goto err;
99         }
100     }
101
102     if (!BN_GENCB_call(cb, 0, c1++))
103         /* aborted */
104         goto err;
105
106     if (!safe) {
107         i = BN_is_prime_fasttest_ex(ret, checks, ctx, 0, cb);
108         if (i == -1)
109             goto err;
110         if (i == 0)
111             goto loop;
112     } else {
113         /*
114          * for "safe prime" generation, check that (p-1)/2 is prime. Since a
115          * prime is odd, We just need to divide by 2
116          */
117         if (!BN_rshift1(t, ret))
118             goto err;
119
120         for (i = 0; i < checks; i++) {
121             j = BN_is_prime_fasttest_ex(ret, 1, ctx, 0, cb);
122             if (j == -1)
123                 goto err;
124             if (j == 0)
125                 goto loop;
126
127             j = BN_is_prime_fasttest_ex(t, 1, ctx, 0, cb);
128             if (j == -1)
129                 goto err;
130             if (j == 0)
131                 goto loop;
132
133             if (!BN_GENCB_call(cb, 2, c1 - 1))
134                 goto err;
135             /* We have a safe prime test pass */
136         }
137     }
138     /* we have a prime :-) */
139     found = 1;
140  err:
141     OPENSSL_free(mods);
142     BN_CTX_end(ctx);
143     BN_CTX_free(ctx);
144     bn_check_top(ret);
145     return found;
146 }
147
148 int BN_is_prime_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,
149                    BN_GENCB *cb)
150 {
151     return BN_is_prime_fasttest_ex(a, checks, ctx_passed, 0, cb);
152 }
153
154 int BN_is_prime_fasttest_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,
155                             int do_trial_division, BN_GENCB *cb)
156 {
157     int i, j, ret = -1;
158     int k;
159     BN_CTX *ctx = NULL;
160     BIGNUM *A1, *A1_odd, *A3, *check; /* taken from ctx */
161     BN_MONT_CTX *mont = NULL;
162
163     /* Take care of the really small primes 2 & 3 */
164     if (BN_is_word(a, 2) || BN_is_word(a, 3))
165         return 1;
166
167     /* Check odd and bigger than 1 */
168     if (!BN_is_odd(a) || BN_cmp(a, BN_value_one()) <= 0)
169         return 0;
170
171     if (checks == BN_prime_checks)
172         checks = BN_prime_checks_for_size(BN_num_bits(a));
173
174     /* first look for small factors */
175     if (do_trial_division) {
176         for (i = 1; i < NUMPRIMES; i++) {
177             BN_ULONG mod = BN_mod_word(a, primes[i]);
178             if (mod == (BN_ULONG)-1)
179                 goto err;
180             if (mod == 0)
181                 return BN_is_word(a, primes[i]);
182         }
183         if (!BN_GENCB_call(cb, 1, -1))
184             goto err;
185     }
186
187     if (ctx_passed != NULL)
188         ctx = ctx_passed;
189     else if ((ctx = BN_CTX_new()) == NULL)
190         goto err;
191     BN_CTX_start(ctx);
192
193     A1 = BN_CTX_get(ctx);
194     A3 = BN_CTX_get(ctx);
195     A1_odd = BN_CTX_get(ctx);
196     check = BN_CTX_get(ctx);
197     if (check == NULL)
198         goto err;
199
200     /* compute A1 := a - 1 */
201     if (!BN_copy(A1, a) || !BN_sub_word(A1, 1))
202         goto err;
203     /* compute A3 := a - 3 */
204     if (!BN_copy(A3, a) || !BN_sub_word(A3, 3))
205         goto err;
206
207     /* write  A1  as  A1_odd * 2^k */
208     k = 1;
209     while (!BN_is_bit_set(A1, k))
210         k++;
211     if (!BN_rshift(A1_odd, A1, k))
212         goto err;
213
214     /* Montgomery setup for computations mod a */
215     mont = BN_MONT_CTX_new();
216     if (mont == NULL)
217         goto err;
218     if (!BN_MONT_CTX_set(mont, a, ctx))
219         goto err;
220
221     for (i = 0; i < checks; i++) {
222         /* 1 < check < a-1 */
223         if (!BN_priv_rand_range(check, A3) || !BN_add_word(check, 2))
224             goto err;
225
226         j = witness(check, a, A1, A1_odd, k, ctx, mont);
227         if (j == -1)
228             goto err;
229         if (j) {
230             ret = 0;
231             goto err;
232         }
233         if (!BN_GENCB_call(cb, 1, i))
234             goto err;
235     }
236     ret = 1;
237  err:
238     if (ctx != NULL) {
239         BN_CTX_end(ctx);
240         if (ctx_passed == NULL)
241             BN_CTX_free(ctx);
242     }
243     BN_MONT_CTX_free(mont);
244
245     return ret;
246 }
247
248 static int witness(BIGNUM *w, const BIGNUM *a, const BIGNUM *a1,
249                    const BIGNUM *a1_odd, int k, BN_CTX *ctx,
250                    BN_MONT_CTX *mont)
251 {
252     if (!BN_mod_exp_mont(w, w, a1_odd, a, ctx, mont)) /* w := w^a1_odd mod a */
253         return -1;
254     if (BN_is_one(w))
255         return 0;               /* probably prime */
256     if (BN_cmp(w, a1) == 0)
257         return 0;               /* w == -1 (mod a), 'a' is probably prime */
258     while (--k) {
259         if (!BN_mod_mul(w, w, w, a, ctx)) /* w := w^2 mod a */
260             return -1;
261         if (BN_is_one(w))
262             return 1;           /* 'a' is composite, otherwise a previous 'w'
263                                  * would have been == -1 (mod 'a') */
264         if (BN_cmp(w, a1) == 0)
265             return 0;           /* w == -1 (mod a), 'a' is probably prime */
266     }
267     /*
268      * If we get here, 'w' is the (a-1)/2-th power of the original 'w', and
269      * it is neither -1 nor +1 -- so 'a' cannot be prime
270      */
271     bn_check_top(w);
272     return 1;
273 }
274
275 static int probable_prime(BIGNUM *rnd, int bits, prime_t *mods)
276 {
277     int i;
278     BN_ULONG delta;
279     BN_ULONG maxdelta = BN_MASK2 - primes[NUMPRIMES - 1];
280     char is_single_word = bits <= BN_BITS2;
281
282  again:
283     /* TODO: Not all primes are private */
284     if (!BN_priv_rand(rnd, bits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ODD))
285         return 0;
286     /* we now have a random number 'rnd' to test. */
287     for (i = 1; i < NUMPRIMES; i++) {
288         BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);
289         if (mod == (BN_ULONG)-1)
290             return 0;
291         mods[i] = (prime_t) mod;
292     }
293     /*
294      * If bits is so small that it fits into a single word then we
295      * additionally don't want to exceed that many bits.
296      */
297     if (is_single_word) {
298         BN_ULONG size_limit;
299
300         if (bits == BN_BITS2) {
301             /*
302              * Shifting by this much has undefined behaviour so we do it a
303              * different way
304              */
305             size_limit = ~((BN_ULONG)0) - BN_get_word(rnd);
306         } else {
307             size_limit = (((BN_ULONG)1) << bits) - BN_get_word(rnd) - 1;
308         }
309         if (size_limit < maxdelta)
310             maxdelta = size_limit;
311     }
312     delta = 0;
313  loop:
314     if (is_single_word) {
315         BN_ULONG rnd_word = BN_get_word(rnd);
316
317         /*-
318          * In the case that the candidate prime is a single word then
319          * we check that:
320          *   1) It's greater than primes[i] because we shouldn't reject
321          *      3 as being a prime number because it's a multiple of
322          *      three.
323          *   2) That it's not a multiple of a known prime. We don't
324          *      check that rnd-1 is also coprime to all the known
325          *      primes because there aren't many small primes where
326          *      that's true.
327          */
328         for (i = 1; i < NUMPRIMES && primes[i] < rnd_word; i++) {
329             if ((mods[i] + delta) % primes[i] == 0) {
330                 delta += 2;
331                 if (delta > maxdelta)
332                     goto again;
333                 goto loop;
334             }
335         }
336     } else {
337         for (i = 1; i < NUMPRIMES; i++) {
338             /*
339              * check that rnd is not a prime and also that gcd(rnd-1,primes)
340              * == 1 (except for 2)
341              */
342             if (((mods[i] + delta) % primes[i]) <= 1) {
343                 delta += 2;
344                 if (delta > maxdelta)
345                     goto again;
346                 goto loop;
347             }
348         }
349     }
350     if (!BN_add_word(rnd, delta))
351         return 0;
352     if (BN_num_bits(rnd) != bits)
353         goto again;
354     bn_check_top(rnd);
355     return 1;
356 }
357
358 int bn_probable_prime_dh(BIGNUM *rnd, int bits,
359                          const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx)
360 {
361     int i, ret = 0;
362     BIGNUM *t1;
363
364     BN_CTX_start(ctx);
365     if ((t1 = BN_CTX_get(ctx)) == NULL)
366         goto err;
367
368     if (!BN_rand(rnd, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))
369         goto err;
370
371     /* we need ((rnd-rem) % add) == 0 */
372
373     if (!BN_mod(t1, rnd, add, ctx))
374         goto err;
375     if (!BN_sub(rnd, rnd, t1))
376         goto err;
377     if (rem == NULL) {
378         if (!BN_add_word(rnd, 1))
379             goto err;
380     } else {
381         if (!BN_add(rnd, rnd, rem))
382             goto err;
383     }
384
385     /* we now have a random number 'rand' to test. */
386
387  loop:
388     for (i = 1; i < NUMPRIMES; i++) {
389         /* check that rnd is a prime */
390         BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);
391         if (mod == (BN_ULONG)-1)
392             goto err;
393         if (mod <= 1) {
394             if (!BN_add(rnd, rnd, add))
395                 goto err;
396             goto loop;
397         }
398     }
399     ret = 1;
400
401  err:
402     BN_CTX_end(ctx);
403     bn_check_top(rnd);
404     return ret;
405 }
406
407 static int probable_prime_dh_safe(BIGNUM *p, int bits, const BIGNUM *padd,
408                                   const BIGNUM *rem, BN_CTX *ctx)
409 {
410     int i, ret = 0;
411     BIGNUM *t1, *qadd, *q;
412
413     bits--;
414     BN_CTX_start(ctx);
415     t1 = BN_CTX_get(ctx);
416     q = BN_CTX_get(ctx);
417     qadd = BN_CTX_get(ctx);
418     if (qadd == NULL)
419         goto err;
420
421     if (!BN_rshift1(qadd, padd))
422         goto err;
423
424     if (!BN_rand(q, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))
425         goto err;
426
427     /* we need ((rnd-rem) % add) == 0 */
428     if (!BN_mod(t1, q, qadd, ctx))
429         goto err;
430     if (!BN_sub(q, q, t1))
431         goto err;
432     if (rem == NULL) {
433         if (!BN_add_word(q, 1))
434             goto err;
435     } else {
436         if (!BN_rshift1(t1, rem))
437             goto err;
438         if (!BN_add(q, q, t1))
439             goto err;
440     }
441
442     /* we now have a random number 'rand' to test. */
443     if (!BN_lshift1(p, q))
444         goto err;
445     if (!BN_add_word(p, 1))
446         goto err;
447
448  loop:
449     for (i = 1; i < NUMPRIMES; i++) {
450         /* check that p and q are prime */
451         /*
452          * check that for p and q gcd(p-1,primes) == 1 (except for 2)
453          */
454         BN_ULONG pmod = BN_mod_word(p, (BN_ULONG)primes[i]);
455         BN_ULONG qmod = BN_mod_word(q, (BN_ULONG)primes[i]);
456         if (pmod == (BN_ULONG)-1 || qmod == (BN_ULONG)-1)
457             goto err;
458         if (pmod == 0 || qmod == 0) {
459             if (!BN_add(p, p, padd))
460                 goto err;
461             if (!BN_add(q, q, qadd))
462                 goto err;
463             goto loop;
464         }
465     }
466     ret = 1;
467
468  err:
469     BN_CTX_end(ctx);
470     bn_check_top(p);
471     return ret;
472 }