Commit d3b4e88f2d for openssl.org

commit d3b4e88f2de4ea9cc101bef9a835fe1e9f01c494
Author: Richard Levitte <levitte@openssl.foundation>
Date:   Wed May 27 09:45:17 2026 +0200

    Refactor BN_mod() and BN_nnmod() arguments to match documentation

    The documentation has this signature for that function:

        int BN_mod(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
        int BN_nnmod(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);

    The implementation, however, had this signature:

        #define BN_mod(rem, m, d, ctx) BN_div(NULL, (rem), (m), (d), (ctx))
        int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx);

    That pattern alone trips up anyone who associates 'm' with modulus, and
    and finds themselves using BN_nnmod() incorrectly.

    This change modifies the argument names to match documentation.

    Reviewed-by: Matt Caswell <matt@openssl.foundation>
    Reviewed-by: Tomas Mraz <tomas@openssl.foundation>
    Reviewed-by: Igor Ustinov <igus@openssl.foundation>
    Reviewed-by: Eugene Syromiatnikov <esyr@openssl.org>
    MergeDate: Fri May 29 07:25:31 2026
    (Merged from https://github.com/openssl/openssl/pull/31304)

diff --git a/crypto/bn/bn_mod.c b/crypto/bn/bn_mod.c
index ae08729545..703072bbf2 100644
--- a/crypto/bn/bn_mod.c
+++ b/crypto/bn/bn_mod.c
@@ -11,24 +11,24 @@
 #include "internal/nelem.h"
 #include "bn_local.h"

-int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)
+int BN_nnmod(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx)
 {
     /*
-     * like BN_mod, but returns non-negative remainder (i.e., 0 <= r < |d|
+     * like BN_mod, but returns non-negative remainder (i.e., 0 <= r < |m|
      * always holds)
      */

-    if (r == d) {
+    if (r == m) {
         ERR_raise(ERR_LIB_BN, ERR_R_PASSED_INVALID_ARGUMENT);
         return 0;
     }

-    if (!(BN_mod(r, m, d, ctx)))
+    if (!(BN_mod(r, a, m, ctx)))
         return 0;
     if (!r->neg)
         return 1;
-    /* now   -|d| < r < 0,  so we have to set  r := r + |d| */
-    return (d->neg ? BN_sub : BN_add)(r, r, d);
+    /* now   -|m| < r < 0,  so we have to set  r := r + |m| */
+    return (m->neg ? BN_sub : BN_add)(r, r, m);
 }

 int BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
diff --git a/include/openssl/bn.h b/include/openssl/bn.h
index 430d20dcb9..5d7e5ce83f 100644
--- a/include/openssl/bn.h
+++ b/include/openssl/bn.h
@@ -273,8 +273,8 @@ int BN_is_negative(const BIGNUM *b);

 int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d,
     BN_CTX *ctx);
-#define BN_mod(rem, m, d, ctx) BN_div(NULL, (rem), (m), (d), (ctx))
-int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx);
+#define BN_mod(rem, a, m, ctx) BN_div(NULL, (rem), (a), (m), (ctx))
+int BN_nnmod(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
 int BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
     BN_CTX *ctx);
 int BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,