summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/lib/libcrypto/bn/bn_exp.c8
-rw-r--r--src/lib/libcrypto/bn/bn_local.h4
-rw-r--r--src/lib/libcrypto/bn/bn_recp.c30
3 files changed, 19 insertions, 23 deletions
diff --git a/src/lib/libcrypto/bn/bn_exp.c b/src/lib/libcrypto/bn/bn_exp.c
index 8ff518e938..129c12495c 100644
--- a/src/lib/libcrypto/bn/bn_exp.c
+++ b/src/lib/libcrypto/bn/bn_exp.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_exp.c,v 1.54 2025/01/21 15:44:22 tb Exp $ */ 1/* $OpenBSD: bn_exp.c,v 1.55 2025/01/22 10:08:10 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 *
@@ -1023,7 +1023,7 @@ BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,
1023 1023
1024 window = BN_window_bits_for_exponent_size(bits); 1024 window = BN_window_bits_for_exponent_size(bits);
1025 if (window > 1) { 1025 if (window > 1) {
1026 if (!BN_mod_mul_reciprocal(aa, val[0], val[0], recp, ctx)) 1026 if (!BN_mod_sqr_reciprocal(aa, val[0], recp, ctx))
1027 goto err; 1027 goto err;
1028 j = 1 << (window - 1); 1028 j = 1 << (window - 1);
1029 for (i = 1; i < j; i++) { 1029 for (i = 1; i < j; i++) {
@@ -1047,7 +1047,7 @@ BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,
1047 for (;;) { 1047 for (;;) {
1048 if (BN_is_bit_set(q, wstart) == 0) { 1048 if (BN_is_bit_set(q, wstart) == 0) {
1049 if (!start) 1049 if (!start)
1050 if (!BN_mod_mul_reciprocal(r, r, r, recp, ctx)) 1050 if (!BN_mod_sqr_reciprocal(r, r, recp, ctx))
1051 goto err; 1051 goto err;
1052 if (wstart == 0) 1052 if (wstart == 0)
1053 break; 1053 break;
@@ -1076,7 +1076,7 @@ BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,
1076 /* add the 'bytes above' */ 1076 /* add the 'bytes above' */
1077 if (!start) 1077 if (!start)
1078 for (i = 0; i < j; i++) { 1078 for (i = 0; i < j; i++) {
1079 if (!BN_mod_mul_reciprocal(r, r, r, recp, ctx)) 1079 if (!BN_mod_sqr_reciprocal(r, r, recp, ctx))
1080 goto err; 1080 goto err;
1081 } 1081 }
1082 1082
diff --git a/src/lib/libcrypto/bn/bn_local.h b/src/lib/libcrypto/bn/bn_local.h
index 2042e0b193..d9389995f7 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.46 2025/01/21 15:44:22 tb Exp $ */ 1/* $OpenBSD: bn_local.h,v 1.47 2025/01/22 10:08:10 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 *
@@ -277,6 +277,8 @@ int BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, BN_RECP_CTX *recp,
277 BN_CTX *ctx); 277 BN_CTX *ctx);
278int BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y, 278int BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y,
279 BN_RECP_CTX *recp, BN_CTX *ctx); 279 BN_RECP_CTX *recp, BN_CTX *ctx);
280int BN_mod_sqr_reciprocal(BIGNUM *r, const BIGNUM *x, BN_RECP_CTX *recp,
281 BN_CTX *ctx);
280int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 282int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
281 const BIGNUM *m, BN_CTX *ctx); 283 const BIGNUM *m, BN_CTX *ctx);
282 284
diff --git a/src/lib/libcrypto/bn/bn_recp.c b/src/lib/libcrypto/bn/bn_recp.c
index 8dd6b8af65..bf38380710 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.27 2025/01/22 09:39:56 tb Exp $ */ 1/* $OpenBSD: bn_recp.c,v 1.28 2025/01/22 10:08:10 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 *
@@ -230,24 +230,18 @@ int
230BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y, 230BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y,
231 BN_RECP_CTX *recp, BN_CTX *ctx) 231 BN_RECP_CTX *recp, BN_CTX *ctx)
232{ 232{
233 int ret = 0; 233 if (!BN_mul(r, x, y, ctx))
234 BIGNUM *a; 234 return 0;
235 235
236 BN_CTX_start(ctx); 236 return BN_div_recp(NULL, r, r, recp, ctx);
237 if ((a = BN_CTX_get(ctx)) == NULL) 237}
238 goto err;
239
240 if (x == y) {
241 if (!BN_sqr(a, x, ctx))
242 goto err;
243 } else {
244 if (!BN_mul(a, x, y, ctx))
245 goto err;
246 }
247 238
248 ret = BN_div_recp(NULL, r, a, recp, ctx); 239/* Compute r = x^2 % m. */
240int
241BN_mod_sqr_reciprocal(BIGNUM *r, const BIGNUM *x, BN_RECP_CTX *recp, BN_CTX *ctx)
242{
243 if (!BN_sqr(r, x, ctx))
244 return 0;
249 245
250err: 246 return BN_div_recp(NULL, r, r, recp, ctx);
251 BN_CTX_end(ctx);
252 return ret;
253} 247}