From 4cb60beefbd11802c165aeec3ea8cb6813eb8691 Mon Sep 17 00:00:00 2001 From: Joel Sing Date: Sat, 17 Dec 2022 15:56:26 +0000 Subject: Provide BN_zero()/BN_one() as functions and make BN_zero() always succeed. BN_zero() is currently implemented using BN_set_word(), which means it can fail, however almost nothing ever checks the return value. A long time ago OpenSSL changed BN_zero() to always succeed and return void, however kept BN_zero as a macro that calls a new BN_zero_ex() function, so that it can be switched back to the "can fail" version. Take a simpler approach - change BN_zero()/BN_one() to functions and make BN_zero() always succeed. This will be exposed in the next bump, at which point we can hopefully also remove the BN_zero_ex() function. ok tb@ --- lib/libcrypto/bn/bn.h | 7 ++++++- lib/libcrypto/bn/bn_isqrt.c | 5 ++--- lib/libcrypto/bn/bn_lib.c | 17 ++++++++++++++--- 3 files changed, 22 insertions(+), 7 deletions(-) (limited to 'lib') diff --git a/lib/libcrypto/bn/bn.h b/lib/libcrypto/bn/bn.h index bef0a878e23..ba6c25ba0a0 100644 --- a/lib/libcrypto/bn/bn.h +++ b/lib/libcrypto/bn/bn.h @@ -1,4 +1,4 @@ -/* $OpenBSD: bn.h,v 1.56 2022/11/30 01:47:19 jsing Exp $ */ +/* $OpenBSD: bn.h,v 1.57 2022/12/17 15:56:25 jsing Exp $ */ /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -329,6 +329,10 @@ int BN_is_one(const BIGNUM *a); int BN_is_word(const BIGNUM *a, const BN_ULONG w); int BN_is_odd(const BIGNUM *a); +#if defined(LIBRESSL_INTERNAL) || defined(LIBRESSL_NEXT_API) +void BN_zero(BIGNUM *a); +int BN_one(BIGNUM *a); +#else #define BN_one(a) BN_set_word((a), 1) void BN_zero_ex(BIGNUM *a); @@ -338,6 +342,7 @@ void BN_zero_ex(BIGNUM *a); #else #define BN_zero(a) (BN_set_word((a),0)) #endif +#endif const BIGNUM *BN_value_one(void); char * BN_options(void); diff --git a/lib/libcrypto/bn/bn_isqrt.c b/lib/libcrypto/bn/bn_isqrt.c index 81f90b10be7..ec77e1b078e 100644 --- a/lib/libcrypto/bn/bn_isqrt.c +++ b/lib/libcrypto/bn/bn_isqrt.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bn_isqrt.c,v 1.5 2022/12/01 21:59:54 tb Exp $ */ +/* $OpenBSD: bn_isqrt.c,v 1.6 2022/12/17 15:56:25 jsing Exp $ */ /* * Copyright (c) 2022 Theo Buehler * @@ -74,8 +74,7 @@ bn_isqrt(BIGNUM *out_sqrt, int *out_perfect, const BIGNUM *n, BN_CTX *in_ctx) if (BN_is_zero(n)) { perfect = 1; - if (!BN_zero(a)) - goto err; + BN_zero(a); goto done; } diff --git a/lib/libcrypto/bn/bn_lib.c b/lib/libcrypto/bn/bn_lib.c index 851c337ef0a..c47f2fa0241 100644 --- a/lib/libcrypto/bn/bn_lib.c +++ b/lib/libcrypto/bn/bn_lib.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bn_lib.c,v 1.66 2022/11/30 03:08:39 jsing Exp $ */ +/* $OpenBSD: bn_lib.c,v 1.67 2022/12/17 15:56:25 jsing Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -998,11 +998,22 @@ BN_swap_ct(BN_ULONG condition, BIGNUM *a, BIGNUM *b, size_t nwords) } void -BN_zero_ex(BIGNUM *a) +BN_zero(BIGNUM *a) { a->neg = 0; a->top = 0; - /* XXX: a->flags &= ~BN_FIXED_TOP */ +} + +void +BN_zero_ex(BIGNUM *a) +{ + BN_zero(a); +} + +int +BN_one(BIGNUM *a) +{ + return BN_set_word(a, 1); } int -- cgit v1.2.3