summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortb <>2023-03-26 18:49:48 +0000
committertb <>2023-03-26 18:49:48 +0000
commite7c1fe4923c04c3cbcc74b60d5dd714eb4a9e3c0 (patch)
tree91a9d01480166f0c7000d76fa494e2650e4582ea /src
parente38cf77b1ca206b636cffd867656a62dd8cf3d98 (diff)
downloadopenbsd-e7c1fe4923c04c3cbcc74b60d5dd714eb4a9e3c0.tar.gz
openbsd-e7c1fe4923c04c3cbcc74b60d5dd714eb4a9e3c0.tar.bz2
openbsd-e7c1fe4923c04c3cbcc74b60d5dd714eb4a9e3c0.zip
Correctly reduce negative inpot to BN_mod_exp2_mont()
Negative bases could result in a negative modulus being returned. This is not strictly speaking incorrect but slightly surprising. This is all a consequence of the shortcut of defining BN_mod() as a macro using BN_div(). Fixes ossfuzz #55997 ok jsing
Diffstat (limited to 'src')
-rw-r--r--src/lib/libcrypto/bn/bn_exp.c6
1 files changed, 3 insertions, 3 deletions
diff --git a/src/lib/libcrypto/bn/bn_exp.c b/src/lib/libcrypto/bn/bn_exp.c
index 9abf574b57..ba9b2700f1 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.38 2023/03/15 04:30:20 jsing Exp $ */ 1/* $OpenBSD: bn_exp.c,v 1.39 2023/03/26 18:49:48 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 *
@@ -1206,7 +1206,7 @@ BN_mod_exp2_mont(BIGNUM *rr, const BIGNUM *a1, const BIGNUM *p1,
1206 * Build table for a1: val1[i] := a1^(2*i + 1) mod m for i = 0 .. 2^(window1-1) 1206 * Build table for a1: val1[i] := a1^(2*i + 1) mod m for i = 0 .. 2^(window1-1)
1207 */ 1207 */
1208 if (a1->neg || BN_ucmp(a1, m) >= 0) { 1208 if (a1->neg || BN_ucmp(a1, m) >= 0) {
1209 if (!BN_mod_ct(val1[0], a1, m, ctx)) 1209 if (!BN_nnmod(val1[0], a1, m, ctx))
1210 goto err; 1210 goto err;
1211 a_mod_m = val1[0]; 1211 a_mod_m = val1[0];
1212 } else 1212 } else
@@ -1237,7 +1237,7 @@ BN_mod_exp2_mont(BIGNUM *rr, const BIGNUM *a1, const BIGNUM *p1,
1237 * Build table for a2: val2[i] := a2^(2*i + 1) mod m for i = 0 .. 2^(window2-1) 1237 * Build table for a2: val2[i] := a2^(2*i + 1) mod m for i = 0 .. 2^(window2-1)
1238 */ 1238 */
1239 if (a2->neg || BN_ucmp(a2, m) >= 0) { 1239 if (a2->neg || BN_ucmp(a2, m) >= 0) {
1240 if (!BN_mod_ct(val2[0], a2, m, ctx)) 1240 if (!BN_nnmod(val2[0], a2, m, ctx))
1241 goto err; 1241 goto err;
1242 a_mod_m = val2[0]; 1242 a_mod_m = val2[0];
1243 } else 1243 } else