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