From 75d0c2055bd9a0ead97a9eaa83b7887c8a89f10e Mon Sep 17 00:00:00 2001 From: tb <> Date: Mon, 6 Jan 2025 12:35:27 +0000 Subject: Shuffle functions into a more sensible order BN_reciprocal() is only called by BN_div_recp() which in turn is only called by BN_mod_mul_reciprocal(). So use this order and make the first two static. --- src/lib/libcrypto/bn/bn_local.h | 6 +--- src/lib/libcrypto/bn/bn_recp.c | 75 +++++++++++++++++++++-------------------- 2 files changed, 39 insertions(+), 42 deletions(-) diff --git a/src/lib/libcrypto/bn/bn_local.h b/src/lib/libcrypto/bn/bn_local.h index 58b5d54903..bfdff56b0e 100644 --- a/src/lib/libcrypto/bn/bn_local.h +++ b/src/lib/libcrypto/bn/bn_local.h @@ -1,4 +1,4 @@ -/* $OpenBSD: bn_local.h,v 1.43 2024/04/16 13:07:14 jsing Exp $ */ +/* $OpenBSD: bn_local.h,v 1.44 2025/01/06 12:35:27 tb Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -280,8 +280,6 @@ int bn_rand_interval(BIGNUM *rnd, BN_ULONG lower_word, const BIGNUM *upper_exc); void BN_init(BIGNUM *); -int BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx); - void BN_RECP_CTX_init(BN_RECP_CTX *recp); BN_RECP_CTX *BN_RECP_CTX_new(void); void BN_RECP_CTX_free(BN_RECP_CTX *recp); @@ -290,8 +288,6 @@ int BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y, BN_RECP_CTX *recp, BN_CTX *ctx); int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx); -int BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, - BN_RECP_CTX *recp, BN_CTX *ctx); /* Explicitly const time / non-const time versions for internal use */ int BN_mod_exp_ct(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, diff --git a/src/lib/libcrypto/bn/bn_recp.c b/src/lib/libcrypto/bn/bn_recp.c index 35390e30d4..c567a5b017 100644 --- a/src/lib/libcrypto/bn/bn_recp.c +++ b/src/lib/libcrypto/bn/bn_recp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bn_recp.c,v 1.19 2023/03/27 10:25:02 tb Exp $ */ +/* $OpenBSD: bn_recp.c,v 1.20 2025/01/06 12:35:27 tb Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -107,37 +107,35 @@ BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *d, BN_CTX *ctx) return (1); } -int -BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y, - BN_RECP_CTX *recp, BN_CTX *ctx) +/* len is the expected size of the result + * We actually calculate with an extra word of precision, so + * we can do faster division if the remainder is not required. + */ +/* r := 2^len / m */ +static int +BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx) { - int ret = 0; - BIGNUM *a; - const BIGNUM *ca; + int ret = -1; + BIGNUM *t; BN_CTX_start(ctx); - if ((a = BN_CTX_get(ctx)) == NULL) + if ((t = BN_CTX_get(ctx)) == NULL) goto err; - if (y != NULL) { - if (x == y) { - if (!BN_sqr(a, x, ctx)) - goto err; - } else { - if (!BN_mul(a, x, y, ctx)) - goto err; - } - ca = a; - } else - ca = x; /* Just do the mod */ - ret = BN_div_recp(NULL, r, ca, recp, ctx); + if (!BN_set_bit(t, len)) + goto err; + + if (!BN_div_ct(r, NULL, t,m, ctx)) + goto err; + + ret = len; err: BN_CTX_end(ctx); return (ret); } -int +static int BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, BN_RECP_CTX *recp, BN_CTX *ctx) { @@ -231,28 +229,31 @@ err: return (ret); } -/* len is the expected size of the result - * We actually calculate with an extra word of precision, so - * we can do faster division if the remainder is not required. - */ -/* r := 2^len / m */ + int -BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx) +BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y, + BN_RECP_CTX *recp, BN_CTX *ctx) { - int ret = -1; - BIGNUM *t; + int ret = 0; + BIGNUM *a; + const BIGNUM *ca; BN_CTX_start(ctx); - if ((t = BN_CTX_get(ctx)) == NULL) - goto err; - - if (!BN_set_bit(t, len)) - goto err; - - if (!BN_div_ct(r, NULL, t,m, ctx)) + if ((a = BN_CTX_get(ctx)) == NULL) goto err; + if (y != NULL) { + if (x == y) { + if (!BN_sqr(a, x, ctx)) + goto err; + } else { + if (!BN_mul(a, x, y, ctx)) + goto err; + } + ca = a; + } else + ca = x; /* Just do the mod */ - ret = len; + ret = BN_div_recp(NULL, r, ca, recp, ctx); err: BN_CTX_end(ctx); -- cgit v1.2.3-55-g6feb