diff options
| author | jsing <> | 2023-02-13 04:25:37 +0000 |
|---|---|---|
| committer | jsing <> | 2023-02-13 04:25:37 +0000 |
| commit | 53aa84bf6a4cfaffa003a28a5651a7f30bd74400 (patch) | |
| tree | 3f4664b9d6259b538a970687b222189d9d59afb5 /src/lib/libcrypto/bn/bn_print.c | |
| parent | 44502592f345cf60ebd8062da41e157768f12e94 (diff) | |
| download | openbsd-53aa84bf6a4cfaffa003a28a5651a7f30bd74400.tar.gz openbsd-53aa84bf6a4cfaffa003a28a5651a7f30bd74400.tar.bz2 openbsd-53aa84bf6a4cfaffa003a28a5651a7f30bd74400.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_print.c')
| -rw-r--r-- | src/lib/libcrypto/bn/bn_print.c | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/src/lib/libcrypto/bn/bn_print.c b/src/lib/libcrypto/bn/bn_print.c index 7251e4d831..7e0683b679 100644 --- a/src/lib/libcrypto/bn/bn_print.c +++ b/src/lib/libcrypto/bn/bn_print.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: bn_print.c,v 1.37 2022/11/26 16:08:51 tb Exp $ */ | 1 | /* $OpenBSD: bn_print.c,v 1.38 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 | * |
| @@ -251,7 +251,8 @@ BN_hex2bn(BIGNUM **bn, const char *a) | |||
| 251 | } | 251 | } |
| 252 | ret->top = h; | 252 | ret->top = h; |
| 253 | bn_correct_top(ret); | 253 | bn_correct_top(ret); |
| 254 | ret->neg = neg; | 254 | |
| 255 | BN_set_negative(ret, neg); | ||
| 255 | 256 | ||
| 256 | *bn = ret; | 257 | *bn = ret; |
| 257 | return (num); | 258 | return (num); |
| @@ -317,9 +318,11 @@ BN_dec2bn(BIGNUM **bn, const char *a) | |||
| 317 | j = 0; | 318 | j = 0; |
| 318 | } | 319 | } |
| 319 | } | 320 | } |
| 320 | ret->neg = neg; | ||
| 321 | 321 | ||
| 322 | bn_correct_top(ret); | 322 | bn_correct_top(ret); |
| 323 | |||
| 324 | BN_set_negative(ret, neg); | ||
| 325 | |||
| 323 | *bn = ret; | 326 | *bn = ret; |
| 324 | return (num); | 327 | return (num); |
| 325 | 328 | ||
| @@ -344,7 +347,7 @@ BN_asc2bn(BIGNUM **bn, const char *a) | |||
| 344 | return 0; | 347 | return 0; |
| 345 | } | 348 | } |
| 346 | if (*a == '-') | 349 | if (*a == '-') |
| 347 | (*bn)->neg = 1; | 350 | BN_set_negative(*bn, 1); |
| 348 | return 1; | 351 | return 1; |
| 349 | } | 352 | } |
| 350 | 353 | ||
