summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bn/bn_recp.c
diff options
context:
space:
mode:
authorjsing <>2023-02-13 04:25:37 +0000
committerjsing <>2023-02-13 04:25:37 +0000
commit59402a8926d023550549cfdb576bcecdb23bb2bc (patch)
tree3f4664b9d6259b538a970687b222189d9d59afb5 /src/lib/libcrypto/bn/bn_recp.c
parentb0f0ff7e648539633669f7fb4530b8d5fc901052 (diff)
downloadopenbsd-59402a8926d023550549cfdb576bcecdb23bb2bc.tar.gz
openbsd-59402a8926d023550549cfdb576bcecdb23bb2bc.tar.bz2
openbsd-59402a8926d023550549cfdb576bcecdb23bb2bc.zip
Avoid negative zero.
Whenever setting negative to one (or when it could potentially be one), always use BN_set_negative() since it checks for a zero valued bignum and will not permit negative to be set in this case. Since BN_is_zero() currently relies on top == 0, call BN_set_negative() after top has been set (or bn_correct_top() has been called). This fixes a long standing issue where -0 and +0 have been permitted, however multiple code paths (such as BN_cmp()) fail to treat these as equivalent. Prompted by Guido Vranken who is adding negative zero fuzzing to oss-fuzz. ok tb@
Diffstat (limited to 'src/lib/libcrypto/bn/bn_recp.c')
-rw-r--r--src/lib/libcrypto/bn/bn_recp.c7
1 files changed, 4 insertions, 3 deletions
diff --git a/src/lib/libcrypto/bn/bn_recp.c b/src/lib/libcrypto/bn/bn_recp.c
index 150b588b48..117f8933bb 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.17 2022/11/26 16:08:51 tb Exp $ */ 1/* $OpenBSD: bn_recp.c,v 1.18 2023/02/13 04:25:37 jsing 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 *
@@ -221,8 +221,9 @@ BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, BN_RECP_CTX *recp,
221 } 221 }
222#endif 222#endif
223 223
224 r->neg = BN_is_zero(r) ? 0 : m->neg; 224 BN_set_negative(r, m->neg);
225 d->neg = m->neg^recp->N.neg; 225 BN_set_negative(d, m->neg ^ recp->N.neg);
226
226 ret = 1; 227 ret = 1;
227 228
228err: 229err: