2 * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
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
12 #include "internal/cryptlib.h"
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.
22 static int witness(BIGNUM
*w
, const BIGNUM
*a
, const BIGNUM
*a1
,
23 const BIGNUM
*a1_odd
, int k
, BN_CTX
*ctx
,
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
,
30 #define square(x) ((BN_ULONG)(x) * (BN_ULONG)(x))
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 (add
== NULL
&& safe
&& bits
< 6 && bits
!= 3) {
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.
74 BNerr(BN_F_BN_GENERATE_PRIME_EX
, BN_R_BITS_TOO_SMALL
);
78 mods
= OPENSSL_zalloc(sizeof(*mods
) * NUMPRIMES
);
90 /* make a random number and set the top and bottom bits */
92 if (!probable_prime(ret
, bits
, safe
, mods
))
96 if (!probable_prime_dh_safe(ret
, bits
, add
, rem
, ctx
))
99 if (!bn_probable_prime_dh(ret
, bits
, add
, rem
, ctx
))
104 if (!BN_GENCB_call(cb
, 0, c1
++))
109 i
= BN_is_prime_fasttest_ex(ret
, checks
, ctx
, 0, cb
);
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
119 if (!BN_rshift1(t
, ret
))
122 for (i
= 0; i
< checks
; i
++) {
123 j
= BN_is_prime_fasttest_ex(ret
, 1, ctx
, 0, cb
);
129 j
= BN_is_prime_fasttest_ex(t
, 1, ctx
, 0, cb
);
135 if (!BN_GENCB_call(cb
, 2, c1
- 1))
137 /* We have a safe prime test pass */
140 /* we have a prime :-) */
150 int BN_is_prime_ex(const BIGNUM
*a
, int checks
, BN_CTX
*ctx_passed
,
153 return BN_is_prime_fasttest_ex(a
, checks
, ctx_passed
, 0, cb
);
156 int BN_is_prime_fasttest_ex(const BIGNUM
*a
, int checks
, BN_CTX
*ctx_passed
,
157 int do_trial_division
, BN_GENCB
*cb
)
162 BIGNUM
*A1
, *A1_odd
, *A3
, *check
; /* taken from ctx */
163 BN_MONT_CTX
*mont
= NULL
;
165 /* Take care of the really small primes 2 & 3 */
166 if (BN_is_word(a
, 2) || BN_is_word(a
, 3))
169 /* Check odd and bigger than 1 */
170 if (!BN_is_odd(a
) || BN_cmp(a
, BN_value_one()) <= 0)
173 if (checks
== BN_prime_checks
)
174 checks
= BN_prime_checks_for_size(BN_num_bits(a
));
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)
183 return BN_is_word(a
, primes
[i
]);
185 if (!BN_GENCB_call(cb
, 1, -1))
189 if (ctx_passed
!= NULL
)
191 else if ((ctx
= BN_CTX_new()) == NULL
)
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
);
202 /* compute A1 := a - 1 */
203 if (!BN_copy(A1
, a
) || !BN_sub_word(A1
, 1))
205 /* compute A3 := a - 3 */
206 if (!BN_copy(A3
, a
) || !BN_sub_word(A3
, 3))
209 /* write A1 as A1_odd * 2^k */
211 while (!BN_is_bit_set(A1
, k
))
213 if (!BN_rshift(A1_odd
, A1
, k
))
216 /* Montgomery setup for computations mod a */
217 mont
= BN_MONT_CTX_new();
220 if (!BN_MONT_CTX_set(mont
, a
, ctx
))
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))
228 j
= witness(check
, a
, A1
, A1_odd
, k
, ctx
, mont
);
235 if (!BN_GENCB_call(cb
, 1, i
))
242 if (ctx_passed
== NULL
)
245 BN_MONT_CTX_free(mont
);
250 static int witness(BIGNUM
*w
, const BIGNUM
*a
, const BIGNUM
*a1
,
251 const BIGNUM
*a1_odd
, int k
, BN_CTX
*ctx
,
254 if (!BN_mod_exp_mont(w
, w
, a1_odd
, a
, ctx
, mont
)) /* w := w^a1_odd mod a */
257 return 0; /* probably prime */
258 if (BN_cmp(w
, a1
) == 0)
259 return 0; /* w == -1 (mod a), 'a' is probably prime */
261 if (!BN_mod_mul(w
, w
, w
, a
, ctx
)) /* w := w^2 mod a */
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 */
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
277 static int probable_prime(BIGNUM
*rnd
, int bits
, int safe
, prime_t
*mods
)
281 BN_ULONG maxdelta
= BN_MASK2
- primes
[NUMPRIMES
- 1];
284 /* TODO: Not all primes are private */
285 if (!BN_priv_rand(rnd
, bits
, BN_RAND_TOP_TWO
, BN_RAND_BOTTOM_ODD
))
287 if (safe
&& !BN_set_bit(rnd
, 1))
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)
294 mods
[i
] = (prime_t
) mod
;
298 for (i
= 1; i
< NUMPRIMES
; i
++) {
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)
306 if (bits
<= 31 && delta
<= 0x7fffffff
307 && square(primes
[i
]) > BN_get_word(rnd
) + delta
)
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
)
317 if (!BN_add_word(rnd
, delta
))
319 if (BN_num_bits(rnd
) != bits
)
325 int bn_probable_prime_dh(BIGNUM
*rnd
, int bits
,
326 const BIGNUM
*add
, const BIGNUM
*rem
, BN_CTX
*ctx
)
332 if ((t1
= BN_CTX_get(ctx
)) == NULL
)
335 if (!BN_rand(rnd
, bits
, BN_RAND_TOP_ONE
, BN_RAND_BOTTOM_ODD
))
338 /* we need ((rnd-rem) % add) == 0 */
340 if (!BN_mod(t1
, rnd
, add
, ctx
))
342 if (!BN_sub(rnd
, rnd
, t1
))
345 if (!BN_add_word(rnd
, 1))
348 if (!BN_add(rnd
, rnd
, rem
))
352 /* we now have a random number 'rand' to test. */
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)
361 if (!BN_add(rnd
, rnd
, add
))
374 static int probable_prime_dh_safe(BIGNUM
*p
, int bits
, const BIGNUM
*padd
,
375 const BIGNUM
*rem
, BN_CTX
*ctx
)
378 BIGNUM
*t1
, *qadd
, *q
;
382 t1
= BN_CTX_get(ctx
);
384 qadd
= BN_CTX_get(ctx
);
388 if (!BN_rshift1(qadd
, padd
))
391 if (!BN_rand(q
, bits
, BN_RAND_TOP_ONE
, BN_RAND_BOTTOM_ODD
))
394 /* we need ((rnd-rem) % add) == 0 */
395 if (!BN_mod(t1
, q
, qadd
, ctx
))
397 if (!BN_sub(q
, q
, t1
))
400 if (!BN_add_word(q
, 1))
403 if (!BN_rshift1(t1
, rem
))
405 if (!BN_add(q
, q
, t1
))
409 /* we now have a random number 'rand' to test. */
410 if (!BN_lshift1(p
, q
))
412 if (!BN_add_word(p
, 1))
416 for (i
= 1; i
< NUMPRIMES
; i
++) {
417 /* check that p and q are prime */
419 * check that for p and q gcd(p-1,primes) == 1 (except for 2)
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)
425 if (pmod
== 0 || qmod
== 0) {
426 if (!BN_add(p
, p
, padd
))
428 if (!BN_add(q
, q
, qadd
))