2 * WARNING: do not edit!
3 * Generated by crypto/bn/bn_prime.pl
4 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
6 * Licensed under the OpenSSL license (the "License"). You may not use
7 * this file except in compliance with the License. You can obtain a copy
8 * in the file LICENSE in the source distribution or at
9 * https://www.openssl.org/source/license.html
14 #include "internal/cryptlib.h"
18 * The quick sieve algorithm approach to weeding out primes is Philip
19 * Zimmermann's, as implemented in PGP. I have had a read of his comments
20 * and implemented my own version.
24 static int witness(BIGNUM
*w
, const BIGNUM
*a
, const BIGNUM
*a1
,
25 const BIGNUM
*a1_odd
, int k
, BN_CTX
*ctx
,
27 static int probable_prime(BIGNUM
*rnd
, int bits
, prime_t
*mods
);
28 static int probable_prime_dh_safe(BIGNUM
*rnd
, int bits
,
29 const BIGNUM
*add
, const BIGNUM
*rem
,
32 int BN_GENCB_call(BN_GENCB
*cb
, int a
, int b
)
34 /* No callback means continue */
39 /* Deprecated-style callbacks */
42 cb
->cb
.cb_1(a
, b
, cb
->arg
);
45 /* New-style callbacks */
46 return cb
->cb
.cb_2(a
, b
, cb
);
50 /* Unrecognised callback type */
54 int BN_generate_prime_ex(BIGNUM
*ret
, int bits
, int safe
,
55 const BIGNUM
*add
, const BIGNUM
*rem
, BN_GENCB
*cb
)
62 int checks
= BN_prime_checks_for_size(bits
);
65 /* There are no prime numbers this small. */
66 BNerr(BN_F_BN_GENERATE_PRIME_EX
, BN_R_BITS_TOO_SMALL
);
68 } else if (bits
== 2 && safe
) {
69 /* The smallest safe prime (7) is three bits. */
70 BNerr(BN_F_BN_GENERATE_PRIME_EX
, BN_R_BITS_TOO_SMALL
);
74 mods
= OPENSSL_zalloc(sizeof(*mods
) * NUMPRIMES
);
86 /* make a random number and set the top and bottom bits */
88 if (!probable_prime(ret
, bits
, mods
))
92 if (!probable_prime_dh_safe(ret
, bits
, add
, rem
, ctx
))
95 if (!bn_probable_prime_dh(ret
, bits
, add
, rem
, ctx
))
100 if (!BN_GENCB_call(cb
, 0, c1
++))
105 i
= BN_is_prime_fasttest_ex(ret
, checks
, ctx
, 0, cb
);
112 * for "safe prime" generation, check that (p-1)/2 is prime. Since a
113 * prime is odd, We just need to divide by 2
115 if (!BN_rshift1(t
, ret
))
118 for (i
= 0; i
< checks
; i
++) {
119 j
= BN_is_prime_fasttest_ex(ret
, 1, ctx
, 0, cb
);
125 j
= BN_is_prime_fasttest_ex(t
, 1, ctx
, 0, cb
);
131 if (!BN_GENCB_call(cb
, 2, c1
- 1))
133 /* We have a safe prime test pass */
136 /* we have a prime :-) */
147 int BN_is_prime_ex(const BIGNUM
*a
, int checks
, BN_CTX
*ctx_passed
,
150 return BN_is_prime_fasttest_ex(a
, checks
, ctx_passed
, 0, cb
);
153 int BN_is_prime_fasttest_ex(const BIGNUM
*a
, int checks
, BN_CTX
*ctx_passed
,
154 int do_trial_division
, BN_GENCB
*cb
)
159 BIGNUM
*A1
, *A1_odd
, *check
; /* taken from ctx */
160 BN_MONT_CTX
*mont
= NULL
;
162 if (BN_cmp(a
, BN_value_one()) <= 0)
165 if (checks
== BN_prime_checks
)
166 checks
= BN_prime_checks_for_size(BN_num_bits(a
));
168 /* first look for small factors */
170 /* a is even => a is prime if and only if a == 2 */
171 return BN_is_word(a
, 2);
172 if (do_trial_division
) {
173 for (i
= 1; i
< NUMPRIMES
; i
++) {
174 BN_ULONG mod
= BN_mod_word(a
, primes
[i
]);
175 if (mod
== (BN_ULONG
)-1)
178 return BN_is_word(a
, primes
[i
]);
180 if (!BN_GENCB_call(cb
, 1, -1))
184 if (ctx_passed
!= NULL
)
186 else if ((ctx
= BN_CTX_new()) == NULL
)
190 A1
= BN_CTX_get(ctx
);
191 A1_odd
= BN_CTX_get(ctx
);
192 check
= BN_CTX_get(ctx
);
196 /* compute A1 := a - 1 */
199 if (!BN_sub_word(A1
, 1))
201 if (BN_is_zero(A1
)) {
206 /* write A1 as A1_odd * 2^k */
208 while (!BN_is_bit_set(A1
, k
))
210 if (!BN_rshift(A1_odd
, A1
, k
))
213 /* Montgomery setup for computations mod a */
214 mont
= BN_MONT_CTX_new();
217 if (!BN_MONT_CTX_set(mont
, a
, ctx
))
220 for (i
= 0; i
< checks
; i
++) {
221 if (!BN_pseudo_rand_range(check
, A1
))
223 if (!BN_add_word(check
, 1))
225 /* now 1 <= check < a */
227 j
= witness(check
, a
, A1
, A1_odd
, k
, ctx
, mont
);
234 if (!BN_GENCB_call(cb
, 1, i
))
241 if (ctx_passed
== NULL
)
244 BN_MONT_CTX_free(mont
);
249 static int witness(BIGNUM
*w
, const BIGNUM
*a
, const BIGNUM
*a1
,
250 const BIGNUM
*a1_odd
, int k
, BN_CTX
*ctx
,
253 if (!BN_mod_exp_mont(w
, w
, a1_odd
, a
, ctx
, mont
)) /* w := w^a1_odd mod a */
256 return 0; /* probably prime */
257 if (BN_cmp(w
, a1
) == 0)
258 return 0; /* w == -1 (mod a), 'a' is probably prime */
260 if (!BN_mod_mul(w
, w
, w
, a
, ctx
)) /* w := w^2 mod a */
263 return 1; /* 'a' is composite, otherwise a previous 'w'
264 * would have been == -1 (mod 'a') */
265 if (BN_cmp(w
, a1
) == 0)
266 return 0; /* w == -1 (mod a), 'a' is probably prime */
269 * If we get here, 'w' is the (a-1)/2-th power of the original 'w', and
270 * it is neither -1 nor +1 -- so 'a' cannot be prime
276 static int probable_prime(BIGNUM
*rnd
, int bits
, prime_t
*mods
)
280 BN_ULONG maxdelta
= BN_MASK2
- primes
[NUMPRIMES
- 1];
281 char is_single_word
= bits
<= BN_BITS2
;
284 if (!BN_rand(rnd
, bits
, BN_RAND_TOP_TWO
, BN_RAND_BOTTOM_ODD
))
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)
291 mods
[i
] = (prime_t
) mod
;
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.
297 if (is_single_word
) {
300 if (bits
== BN_BITS2
) {
302 * Shifting by this much has undefined behaviour so we do it a
305 size_limit
= ~((BN_ULONG
)0) - BN_get_word(rnd
);
307 size_limit
= (((BN_ULONG
)1) << bits
) - BN_get_word(rnd
) - 1;
309 if (size_limit
< maxdelta
)
310 maxdelta
= size_limit
;
314 if (is_single_word
) {
315 BN_ULONG rnd_word
= BN_get_word(rnd
);
318 * In the case that the candidate prime is a single word then
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
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
328 for (i
= 1; i
< NUMPRIMES
&& primes
[i
] < rnd_word
; i
++) {
329 if ((mods
[i
] + delta
) % primes
[i
] == 0) {
331 if (delta
> maxdelta
)
337 for (i
= 1; i
< NUMPRIMES
; i
++) {
339 * check that rnd is not a prime and also that gcd(rnd-1,primes)
340 * == 1 (except for 2)
342 if (((mods
[i
] + delta
) % primes
[i
]) <= 1) {
344 if (delta
> maxdelta
)
350 if (!BN_add_word(rnd
, delta
))
352 if (BN_num_bits(rnd
) != bits
)
358 int bn_probable_prime_dh(BIGNUM
*rnd
, int bits
,
359 const BIGNUM
*add
, const BIGNUM
*rem
, BN_CTX
*ctx
)
365 if ((t1
= BN_CTX_get(ctx
)) == NULL
)
368 if (!BN_rand(rnd
, bits
, BN_RAND_TOP_ONE
, BN_RAND_BOTTOM_ODD
))
371 /* we need ((rnd-rem) % add) == 0 */
373 if (!BN_mod(t1
, rnd
, add
, ctx
))
375 if (!BN_sub(rnd
, rnd
, t1
))
378 if (!BN_add_word(rnd
, 1))
381 if (!BN_add(rnd
, rnd
, rem
))
385 /* we now have a random number 'rand' to test. */
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)
394 if (!BN_add(rnd
, rnd
, add
))
407 static int probable_prime_dh_safe(BIGNUM
*p
, int bits
, const BIGNUM
*padd
,
408 const BIGNUM
*rem
, BN_CTX
*ctx
)
411 BIGNUM
*t1
, *qadd
, *q
;
415 t1
= BN_CTX_get(ctx
);
417 qadd
= BN_CTX_get(ctx
);
421 if (!BN_rshift1(qadd
, padd
))
424 if (!BN_rand(q
, bits
, BN_RAND_TOP_ONE
, BN_RAND_BOTTOM_ODD
))
427 /* we need ((rnd-rem) % add) == 0 */
428 if (!BN_mod(t1
, q
, qadd
, ctx
))
430 if (!BN_sub(q
, q
, t1
))
433 if (!BN_add_word(q
, 1))
436 if (!BN_rshift1(t1
, rem
))
438 if (!BN_add(q
, q
, t1
))
442 /* we now have a random number 'rand' to test. */
443 if (!BN_lshift1(p
, q
))
445 if (!BN_add_word(p
, 1))
449 for (i
= 1; i
< NUMPRIMES
; i
++) {
450 /* check that p and q are prime */
452 * check that for p and q gcd(p-1,primes) == 1 (except for 2)
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)
458 if (pmod
== 0 || qmod
== 0) {
459 if (!BN_add(p
, p
, padd
))
461 if (!BN_add(q
, q
, qadd
))