summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortb <>2025-01-06 12:35:27 +0000
committertb <>2025-01-06 12:35:27 +0000
commit75d0c2055bd9a0ead97a9eaa83b7887c8a89f10e (patch)
tree4d972561c740c5722770d1761d303404400ad414
parent040f047180525bbee39de311cf3ae17a898c86dc (diff)
downloadopenbsd-75d0c2055bd9a0ead97a9eaa83b7887c8a89f10e.tar.gz
openbsd-75d0c2055bd9a0ead97a9eaa83b7887c8a89f10e.tar.bz2
openbsd-75d0c2055bd9a0ead97a9eaa83b7887c8a89f10e.zip
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.
-rw-r--r--src/lib/libcrypto/bn/bn_local.h6
-rw-r--r--src/lib/libcrypto/bn/bn_recp.c75
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 @@
1/* $OpenBSD: bn_local.h,v 1.43 2024/04/16 13:07:14 jsing Exp $ */ 1/* $OpenBSD: bn_local.h,v 1.44 2025/01/06 12:35:27 tb Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -280,8 +280,6 @@ int bn_rand_interval(BIGNUM *rnd, BN_ULONG lower_word, const BIGNUM *upper_exc);
280 280
281void BN_init(BIGNUM *); 281void BN_init(BIGNUM *);
282 282
283int BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx);
284
285void BN_RECP_CTX_init(BN_RECP_CTX *recp); 283void BN_RECP_CTX_init(BN_RECP_CTX *recp);
286BN_RECP_CTX *BN_RECP_CTX_new(void); 284BN_RECP_CTX *BN_RECP_CTX_new(void);
287void BN_RECP_CTX_free(BN_RECP_CTX *recp); 285void 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,
290 BN_RECP_CTX *recp, BN_CTX *ctx); 288 BN_RECP_CTX *recp, BN_CTX *ctx);
291int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 289int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
292 const BIGNUM *m, BN_CTX *ctx); 290 const BIGNUM *m, BN_CTX *ctx);
293int BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m,
294 BN_RECP_CTX *recp, BN_CTX *ctx);
295 291
296/* Explicitly const time / non-const time versions for internal use */ 292/* Explicitly const time / non-const time versions for internal use */
297int BN_mod_exp_ct(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 293int 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 @@
1/* $OpenBSD: bn_recp.c,v 1.19 2023/03/27 10:25:02 tb Exp $ */ 1/* $OpenBSD: bn_recp.c,v 1.20 2025/01/06 12:35:27 tb Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -107,37 +107,35 @@ BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *d, BN_CTX *ctx)
107 return (1); 107 return (1);
108} 108}
109 109
110int 110/* len is the expected size of the result
111BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y, 111 * We actually calculate with an extra word of precision, so
112 BN_RECP_CTX *recp, BN_CTX *ctx) 112 * we can do faster division if the remainder is not required.
113 */
114/* r := 2^len / m */
115static int
116BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx)
113{ 117{
114 int ret = 0; 118 int ret = -1;
115 BIGNUM *a; 119 BIGNUM *t;
116 const BIGNUM *ca;
117 120
118 BN_CTX_start(ctx); 121 BN_CTX_start(ctx);
119 if ((a = BN_CTX_get(ctx)) == NULL) 122 if ((t = BN_CTX_get(ctx)) == NULL)
120 goto err; 123 goto err;
121 if (y != NULL) {
122 if (x == y) {
123 if (!BN_sqr(a, x, ctx))
124 goto err;
125 } else {
126 if (!BN_mul(a, x, y, ctx))
127 goto err;
128 }
129 ca = a;
130 } else
131 ca = x; /* Just do the mod */
132 124
133 ret = BN_div_recp(NULL, r, ca, recp, ctx); 125 if (!BN_set_bit(t, len))
126 goto err;
127
128 if (!BN_div_ct(r, NULL, t,m, ctx))
129 goto err;
130
131 ret = len;
134 132
135err: 133err:
136 BN_CTX_end(ctx); 134 BN_CTX_end(ctx);
137 return (ret); 135 return (ret);
138} 136}
139 137
140int 138static int
141BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, BN_RECP_CTX *recp, 139BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, BN_RECP_CTX *recp,
142 BN_CTX *ctx) 140 BN_CTX *ctx)
143{ 141{
@@ -231,28 +229,31 @@ err:
231 return (ret); 229 return (ret);
232} 230}
233 231
234/* len is the expected size of the result 232
235 * We actually calculate with an extra word of precision, so
236 * we can do faster division if the remainder is not required.
237 */
238/* r := 2^len / m */
239int 233int
240BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx) 234BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y,
235 BN_RECP_CTX *recp, BN_CTX *ctx)
241{ 236{
242 int ret = -1; 237 int ret = 0;
243 BIGNUM *t; 238 BIGNUM *a;
239 const BIGNUM *ca;
244 240
245 BN_CTX_start(ctx); 241 BN_CTX_start(ctx);
246 if ((t = BN_CTX_get(ctx)) == NULL) 242 if ((a = BN_CTX_get(ctx)) == NULL)
247 goto err;
248
249 if (!BN_set_bit(t, len))
250 goto err;
251
252 if (!BN_div_ct(r, NULL, t,m, ctx))
253 goto err; 243 goto err;
244 if (y != NULL) {
245 if (x == y) {
246 if (!BN_sqr(a, x, ctx))
247 goto err;
248 } else {
249 if (!BN_mul(a, x, y, ctx))
250 goto err;
251 }
252 ca = a;
253 } else
254 ca = x; /* Just do the mod */
254 255
255 ret = len; 256 ret = BN_div_recp(NULL, r, ca, recp, ctx);
256 257
257err: 258err:
258 BN_CTX_end(ctx); 259 BN_CTX_end(ctx);