summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bn/bn_prime.c
diff options
context:
space:
mode:
authortb <>2023-05-10 12:21:55 +0000
committertb <>2023-05-10 12:21:55 +0000
commit536ed901af3f95bbdf5d437d3f0ad7d96659ee96 (patch)
tree19201b080903229104292ae2352a17e8ddb4af50 /src/lib/libcrypto/bn/bn_prime.c
parent2571fa44f98869d5490b1fc30fb1ed5868093cdb (diff)
downloadopenbsd-536ed901af3f95bbdf5d437d3f0ad7d96659ee96.tar.gz
openbsd-536ed901af3f95bbdf5d437d3f0ad7d96659ee96.tar.bz2
openbsd-536ed901af3f95bbdf5d437d3f0ad7d96659ee96.zip
Add Miller-Rabin test for random bases to BPSW
The behavior of the BPSW primality test for numbers > 2^64 is not very well understood. While there is no known composite that passes the test, there are heuristics that indicate that there are likely infinitely many. Therefore it seems appropriate to harden the test. Having a settable number of MR rounds before doing a version of BPSW is also the approach taken by Go's primality check in math/big. This adds a new implementation of the old MR test that runs before running the strong Lucas test. I like to imagine that it's slightly cleaner code. We're effectively at about twice the cost of what we had a year ago. In addition, it adds some non-determinism in case there actually are false positives for the BPSW test. The implementation is straightforward. It could easily be tweaked to use the additional gcds in the "enhanced" MR test of FIPS 186-5, but as long as we are only going to throw away the additional info, that's not worth much. This is a first step towards incorporating some of the considerations in "A performant misuse-resistant API for Primality Testing" by Massimo and Paterson. Further work will happen in tree. In particular, there are plans to crank the number of Miller-Rabin tests considerably so as to have a guaranteed baseline. The manual will be updated shortly. positive feedback beck ok jsing
Diffstat (limited to 'src/lib/libcrypto/bn/bn_prime.c')
-rw-r--r--src/lib/libcrypto/bn/bn_prime.c16
1 files changed, 11 insertions, 5 deletions
diff --git a/src/lib/libcrypto/bn/bn_prime.c b/src/lib/libcrypto/bn/bn_prime.c
index c2fd0fc2e9..b8f0eb69ce 100644
--- a/src/lib/libcrypto/bn/bn_prime.c
+++ b/src/lib/libcrypto/bn/bn_prime.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_prime.c,v 1.31 2023/04/25 19:57:59 tb Exp $ */ 1/* $OpenBSD: bn_prime.c,v 1.32 2023/05/10 12:21:55 tb Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -195,12 +195,12 @@ BN_generate_prime_ex(BIGNUM *ret, int bits, int safe, const BIGNUM *add,
195 goto err; 195 goto err;
196 196
197 if (!safe) { 197 if (!safe) {
198 if (!bn_is_prime_bpsw(&is_prime, ret, ctx)) 198 if (!bn_is_prime_bpsw(&is_prime, ret, ctx, 1))
199 goto err; 199 goto err;
200 if (!is_prime) 200 if (!is_prime)
201 goto loop; 201 goto loop;
202 } else { 202 } else {
203 if (!bn_is_prime_bpsw(&is_prime, ret, ctx)) 203 if (!bn_is_prime_bpsw(&is_prime, ret, ctx, 1))
204 goto err; 204 goto err;
205 if (!is_prime) 205 if (!is_prime)
206 goto loop; 206 goto loop;
@@ -213,7 +213,7 @@ BN_generate_prime_ex(BIGNUM *ret, int bits, int safe, const BIGNUM *add,
213 if (!BN_rshift1(p, ret)) 213 if (!BN_rshift1(p, ret))
214 goto err; 214 goto err;
215 215
216 if (!bn_is_prime_bpsw(&is_prime, p, ctx)) 216 if (!bn_is_prime_bpsw(&is_prime, p, ctx, 1))
217 goto err; 217 goto err;
218 if (!is_prime) 218 if (!is_prime)
219 goto loop; 219 goto loop;
@@ -243,8 +243,14 @@ BN_is_prime_fasttest_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,
243{ 243{
244 int is_prime; 244 int is_prime;
245 245
246 if (checks < 0)
247 return -1;
248
249 if (checks == BN_prime_checks)
250 checks = BN_prime_checks_for_size(BN_num_bits(a));
251
246 /* XXX - tickle BN_GENCB in bn_is_prime_bpsw(). */ 252 /* XXX - tickle BN_GENCB in bn_is_prime_bpsw(). */
247 if (!bn_is_prime_bpsw(&is_prime, a, ctx_passed)) 253 if (!bn_is_prime_bpsw(&is_prime, a, ctx_passed, checks))
248 return -1; 254 return -1;
249 255
250 return is_prime; 256 return is_prime;