git: 8782ceb94273 - stable/13 - Reduce ifdef soup by adding pre-3.0 compat support

From: Enji Cooper <ngie_at_FreeBSD.org>
Date: Wed, 24 Sep 2025 22:17:09 UTC
The branch stable/13 has been updated by ngie:

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

commit 8782ceb94273915fd726cd1a62d7cc080837d397
Author:     Enji Cooper <ngie@FreeBSD.org>
AuthorDate: 2023-05-27 21:07:45 +0000
Commit:     Enji Cooper <ngie@FreeBSD.org>
CommitDate: 2025-09-24 05:36:59 +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
    
    (cherry picked from commit dcf5d5603b3af831002caa7b2f64aec8bda14071)
---
 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 523547086f6d..c183a6980edd 100644
--- a/usr.bin/factor/factor.c
+++ b/usr.bin/factor/factor.c
@@ -79,7 +79,15 @@ __RCSID("$NetBSD: factor.c,v 1.19 2009/08/12 05:54:31 dholland Exp $");
 
 #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 */
 
@@ -206,11 +214,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);
@@ -283,11 +287,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);
@@ -296,13 +296,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;