From a881da8482cf1ded82dda8177f3be4c5a6f48ebc Mon Sep 17 00:00:00 2001 From: jsing <> Date: Fri, 3 Feb 2023 05:10:57 +0000 Subject: Clean up BN_mod_mul() and simplify BN_mod_sqr(). Use the same naming/code pattern in BN_mod_mul() as is used in BN_mul(). Note that the 'rr' allocation is unnecessary, since both BN_mul() and BN_sqr() handle the case where r == a || r == b. However, it avoids a potential copy on the exit from BN_mul()/BN_sqr(), so leave it in place for now. Turn BN_mod_sqr() into a wrapper that calls BN_mod_mul(), since it already calls BN_sqr() in the a == b. The supposed gain of calling BN_mod_ct() instead of BN_nnmod() does not really exist. ok tb@ --- src/lib/libcrypto/bn/bn_mod.c | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) (limited to 'src/lib') diff --git a/src/lib/libcrypto/bn/bn_mod.c b/src/lib/libcrypto/bn/bn_mod.c index 4a62715974..762ffb5580 100644 --- a/src/lib/libcrypto/bn/bn_mod.c +++ b/src/lib/libcrypto/bn/bn_mod.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bn_mod.c,v 1.17 2023/02/03 05:06:20 jsing Exp $ */ +/* $OpenBSD: bn_mod.c,v 1.18 2023/02/03 05:10:57 jsing Exp $ */ /* Includes code written by Lenka Fibikova * for the OpenSSL project. */ /* ==================================================================== @@ -189,41 +189,43 @@ BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m) return BN_usub(r, m, r); } -/* slow but works */ int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, BN_CTX *ctx) { - BIGNUM *t; + BIGNUM *rr; int ret = 0; - BN_CTX_start(ctx); - if ((t = BN_CTX_get(ctx)) == NULL) + + rr = r; + if (rr == a || rr == b) + rr = BN_CTX_get(ctx); + if (rr == NULL) goto err; + if (a == b) { - if (!BN_sqr(t, a, ctx)) + if (!BN_sqr(rr, a, ctx)) goto err; } else { - if (!BN_mul(t, a,b, ctx)) + if (!BN_mul(rr, a, b, ctx)) goto err; } - if (!BN_nnmod(r, t,m, ctx)) + if (!BN_nnmod(r, rr, m, ctx)) goto err; + ret = 1; -err: + err: BN_CTX_end(ctx); - return (ret); + + return ret; } int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx) { - if (!BN_sqr(r, a, ctx)) - return 0; - /* r->neg == 0, thus we don't need BN_nnmod */ - return BN_mod_ct(r, r, m, ctx); + return BN_mod_mul(r, a, a, m, ctx); } int -- cgit v1.2.3-55-g6feb