Reduce ifdef soup by adding pre-3.0 compat support

This change creates a static inline function, BN_check_prime, for
pre-3.0 use which is implemented with the previous (1.1) compatible call
under the covers, `BN_is_prime_ex`.

The `nchecks` parameter value is maintained, even though it has no
noticable behavior change, given that the documentation clearly states
that at least 64 or 128 rounds are executed on the backend, depending on
how many bits there are in the given number being factored out.

MFC after:	1 week
Differential Revision:	 https://reviews.freebsd.org/D40305
This commit is contained in:
Enji Cooper 2023-05-27 14:07:45 -07:00
parent 11ce203e05
commit dcf5d5603b

View file

@ -82,7 +82,15 @@ __FBSDID("$FreeBSD$");
#include <openssl/bn.h>
#define PRIME_CHECKS 5
#if OPENSSL_VERSION_NUMBER < 0x30000000L
static inline int
BN_check_prime(BN *p, BN_CTX *ctx, BN_GENCB *cb)
{
const int nchecks = 5;
return BN_is_prime_ex(val, nchecks, ctx, cb);
}
#endif
static void pollard_pminus1(BIGNUM *); /* print factors for big numbers */
@ -209,11 +217,7 @@ pr_fact(BIGNUM *val)
if (!BN_sqr(bnfact, bnfact, ctx))
errx(1, "error in BN_sqr()");
if (BN_cmp(bnfact, val) > 0 ||
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
BN_check_prime(val, NULL, NULL) == 1)
#else
BN_is_prime_ex(val, PRIME_CHECKS, NULL, NULL) == 1)
#endif
pr_print(val);
else
pollard_pminus1(val);
@ -286,11 +290,7 @@ newbase:
errx(1, "error in BN_gcd()");
if (!BN_is_one(x)) {
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
if (BN_check_prime(x, NULL, NULL) == 1)
#else
if (BN_is_prime_ex(x, PRIME_CHECKS, NULL, NULL) == 1)
#endif
pr_print(x);
else
pollard_pminus1(x);
@ -299,13 +299,7 @@ newbase:
BN_div(num, NULL, val, x, ctx);
if (BN_is_one(num))
return;
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
if (BN_check_prime(num, NULL, NULL) == 1)
#else
if (BN_is_prime_ex(num, PRIME_CHECKS, NULL, NULL)
== 1)
#endif
{
if (BN_check_prime(num, NULL, NULL) == 1) {
pr_print(num);
fflush(stdout);
return;