From 6c6e991121d8be037bc8023536ae59c7e8176e09 Mon Sep 17 00:00:00 2001 From: tb Date: Sat, 4 Dec 2021 15:59:52 +0000 Subject: [PATCH] Provide function implementations for various BN_* macros BN_abs_is_word, BN_is_{zero,one,word,odd}, BN_one, BN_zero_ex are now implemented as functions for internal use. They will be exposed publicly to replace the macros reaching into BIGNUM in the next bump. ok inoguchi jsing --- lib/libcrypto/bn/bn.h | 18 +++++++++++++++--- lib/libcrypto/bn/bn_lib.c | 40 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/lib/libcrypto/bn/bn.h b/lib/libcrypto/bn/bn.h index 20212bf171c..e9837cbbd61 100644 --- a/lib/libcrypto/bn/bn.h +++ b/lib/libcrypto/bn/bn.h @@ -1,4 +1,4 @@ -/* $OpenBSD: bn.h,v 1.46 2021/12/04 15:53:01 tb Exp $ */ +/* $OpenBSD: bn.h,v 1.47 2021/12/04 15:59:52 tb Exp $ */ /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -409,10 +409,21 @@ void *BN_GENCB_get_arg(BN_GENCB *cb); (b) >= 308 ? 8 : \ (b) >= 55 ? 27 : \ /* b >= 6 */ 34) - + #define BN_num_bytes(a) ((BN_num_bits(a)+7)/8) -/* Note that BN_abs_is_word didn't work reliably for w == 0 until 0.9.8 */ +#if defined(LIBRESSL_OPAQUE_BN) || defined(LIBRESSL_CRYPTO_INTERNAL) +int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w); +int BN_is_zero(const BIGNUM *a); +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); + +#define BN_one(a) BN_set_word((a), 1) + +void BN_zero_ex(BIGNUM *a); + +#else #define BN_abs_is_word(a,w) ((((a)->top == 1) && ((a)->d[0] == (BN_ULONG)(w))) || \ (((w) == 0) && ((a)->top == 0))) #define BN_is_zero(a) ((a)->top == 0) @@ -427,6 +438,7 @@ void *BN_GENCB_get_arg(BN_GENCB *cb); _tmp_bn->top = 0; \ _tmp_bn->neg = 0; \ } while(0) +#endif /* LIBRESSL_OPAQUE_BN */ #ifdef OPENSSL_NO_DEPRECATED #define BN_zero(a) BN_zero_ex(a) diff --git a/lib/libcrypto/bn/bn_lib.c b/lib/libcrypto/bn/bn_lib.c index e4085c9d671..77ee3b1fdcd 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.50 2021/12/04 15:53:01 tb Exp $ */ +/* $OpenBSD: bn_lib.c,v 1.51 2021/12/04 15:59:52 tb Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -1061,6 +1061,44 @@ BN_swap_ct(BN_ULONG condition, BIGNUM *a, BIGNUM *b, size_t nwords) return 1; } +void +BN_zero_ex(BIGNUM *a) +{ + a->neg = 0; + a->top = 0; + /* XXX: a->flags &= ~BN_FIXED_TOP */ +} + +int +BN_abs_is_word(const BIGNUM *a, const BN_ULONG w) +{ + return (a->top == 1 && a->d[0] == w) || (w == 0 && a->top == 0); +} + +int +BN_is_zero(const BIGNUM *a) +{ + return a->top == 0; +} + +int +BN_is_one(const BIGNUM *a) +{ + return BN_abs_is_word(a, 1) && !a->neg; +} + +int +BN_is_word(const BIGNUM *a, const BN_ULONG w) +{ + return BN_abs_is_word(a, w) && (w == 0 || !a->neg); +} + +int +BN_is_odd(const BIGNUM *a) +{ + return a->top > 0 && (a->d[0] & 1); +} + BN_GENCB * BN_GENCB_new(void) { -- 2.20.1