]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
Refactor BN_mod() and BN_nnmod() arguments to match documentation
authorRichard Levitte <levitte@openssl.foundation>
Wed, 27 May 2026 07:45:17 +0000 (09:45 +0200)
committerNorbert Pocs <norbertp@openssl.org>
Fri, 29 May 2026 07:25:27 +0000 (09:25 +0200)
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)

crypto/bn/bn_mod.c
include/openssl/bn.h

index ae0872954594becf43ec4d657ed6ebf313be1ade..703072bbf22d222ccbc1d03b8971f5f1996710f2 100644 (file)
 #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,
index 430d20dcb923188922810b490ffb1ca98d6be9b8..5d7e5ce83fe6262b5e1bdbe54b56d2dd160392ed 100644 (file)
@@ -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,