git: dcf5d5603b3a - main - Reduce ifdef soup by adding pre-3.0 compat support

From: Enji Cooper <ngie_at_FreeBSD.org>
Date: Sun, 28 May 2023 00:05:51 UTC
The branch main has been updated by ngie:

URL: https://cgit.FreeBSD.org/src/commit/?id=dcf5d5603b3af831002caa7b2f64aec8bda14071

commit dcf5d5603b3af831002caa7b2f64aec8bda14071
Author:     Enji Cooper <ngie@FreeBSD.org>
AuthorDate: 2023-05-27 21:07:45 +0000
Commit:     Enji Cooper <ngie@FreeBSD.org>
CommitDate: 2023-05-28 00:05:39 +0000

    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
---
 usr.bin/factor/factor.c | 26 ++++++++++----------------
 1 file changed, 10 insertions(+), 16 deletions(-)

diff --git a/usr.bin/factor/factor.c b/usr.bin/factor/factor.c
index a3b0a65b5eca..7fc31353e5fa 100644
--- a/usr.bin/factor/factor.c
+++ b/usr.bin/factor/factor.c
@@ -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;