From: Richard Levitte Date: Wed, 27 May 2026 07:45:17 +0000 (+0200) Subject: Refactor BN_mod() and BN_nnmod() arguments to match documentation X-Git-Url: http://git.ipfire.org/gitweb/index.cgi?a=commitdiff_plain;h=d3b4e88f2de4ea9cc101bef9a835fe1e9f01c494;p=thirdparty%2Fopenssl.git 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 Reviewed-by: Tomas Mraz Reviewed-by: Igor Ustinov Reviewed-by: Eugene Syromiatnikov 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 ae087295459..703072bbf22 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 430d20dcb92..5d7e5ce83fe 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,