From f3f05f0cf8c30a990193e332e26fdd8e54b3f59b Mon Sep 17 00:00:00 2001 From: tb <> Date: Fri, 5 Dec 2025 14:12:32 +0000 Subject: libcrypto: make most of the BN_* macro pollution internal This is a first sweep of reducing the number of terribly named macros in bn.h More can go away. Those we need internally go to bn_local.h. Annoyingly bn_internal.h uses some of them, so it includes bn_local.h now. This can be cleaned up later. ok jsing kenjiro --- src/lib/libcrypto/bn/bn.h | 27 +-------------------------- src/lib/libcrypto/bn/bn_convert.c | 10 +++++++++- src/lib/libcrypto/bn/bn_internal.h | 3 ++- src/lib/libcrypto/bn/bn_local.h | 16 +++++++++++++++- src/lib/libcrypto/ec/ec_internal.h | 4 ++-- 5 files changed, 29 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/lib/libcrypto/bn/bn.h b/src/lib/libcrypto/bn/bn.h index e64fcf355f..1ee1f1f1f8 100644 --- a/src/lib/libcrypto/bn/bn.h +++ b/src/lib/libcrypto/bn/bn.h @@ -1,4 +1,4 @@ -/* $OpenBSD: bn.h,v 1.83 2025/11/07 13:41:40 jsing Exp $ */ +/* $OpenBSD: bn.h,v 1.84 2025/12/05 14:12:32 tb Exp $ */ /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -143,43 +143,18 @@ extern "C" { #if defined(_LP64) || defined(_WIN64) #undef BN_LLONG #define BN_ULONG uint64_t -#define BN_LONG int64_t -#define BN_BITS 128 #define BN_BYTES 8 #define BN_BITS2 64 -#define BN_BITS4 32 -#define BN_MASK2 UINT64_C(0xffffffffffffffff) -#define BN_MASK2l UINT64_C(0xffffffff) -#define BN_MASK2h UINT64_C(0xffffffff00000000) -#define BN_MASK2h1 UINT64_C(0xffffffff80000000) -#define BN_TBIT UINT64_C(0x8000000000000000) -#define BN_DEC_CONV UINT64_C(10000000000000000000) #define BN_DEC_FMT1 "%" PRIu64 -#define BN_DEC_FMT2 "%019" PRIu64 -#define BN_DEC_NUM 19 #define BN_HEX_FMT1 "%" PRIX64 -#define BN_HEX_FMT2 "%016" PRIX64 #else #define BN_ULLONG uint64_t #define BN_LLONG #define BN_ULONG uint32_t -#define BN_LONG int32_t -#define BN_BITS 64 #define BN_BYTES 4 #define BN_BITS2 32 -#define BN_BITS4 16 -#define BN_MASK UINT64_C(0xffffffffffffffff) -#define BN_MASK2 UINT32_C(0xffffffff) -#define BN_MASK2l UINT32_C(0xffff) -#define BN_MASK2h1 UINT32_C(0xffff8000) -#define BN_MASK2h UINT32_C(0xffff0000) -#define BN_TBIT UINT32_C(0x80000000) -#define BN_DEC_CONV UINT32_C(1000000000) #define BN_DEC_FMT1 "%" PRIu32 -#define BN_DEC_FMT2 "%09" PRIu32 -#define BN_DEC_NUM 9 #define BN_HEX_FMT1 "%" PRIX32 -#define BN_HEX_FMT2 "%08" PRIX32 #endif #define BN_FLG_MALLOCED 0x01 diff --git a/src/lib/libcrypto/bn/bn_convert.c b/src/lib/libcrypto/bn/bn_convert.c index ca5c7d7865..ab5bc519c8 100644 --- a/src/lib/libcrypto/bn/bn_convert.c +++ b/src/lib/libcrypto/bn/bn_convert.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bn_convert.c,v 1.24 2025/05/10 05:54:38 tb Exp $ */ +/* $OpenBSD: bn_convert.c,v 1.25 2025/12/05 14:12:32 tb Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -71,6 +71,14 @@ #include "crypto_internal.h" #include "err_local.h" +#if BN_BYTES == 8 +#define BN_DEC_CONV UINT64_C(10000000000000000000) +#define BN_DEC_NUM 19 +#else +#define BN_DEC_CONV UINT32_C(1000000000) +#define BN_DEC_NUM 9 +#endif + static int bn_dec2bn_cbs(BIGNUM **bnp, CBS *cbs); static int bn_hex2bn_cbs(BIGNUM **bnp, CBS *cbs); diff --git a/src/lib/libcrypto/bn/bn_internal.h b/src/lib/libcrypto/bn/bn_internal.h index 8b5145e225..efe8202aa0 100644 --- a/src/lib/libcrypto/bn/bn_internal.h +++ b/src/lib/libcrypto/bn/bn_internal.h @@ -1,4 +1,4 @@ -/* $OpenBSD: bn_internal.h,v 1.20 2025/08/02 16:20:00 jsing Exp $ */ +/* $OpenBSD: bn_internal.h,v 1.21 2025/12/05 14:12:32 tb Exp $ */ /* * Copyright (c) 2023 Joel Sing * @@ -18,6 +18,7 @@ #include #include "bn_arch.h" +#include "bn_local.h" #ifndef HEADER_BN_INTERNAL_H #define HEADER_BN_INTERNAL_H diff --git a/src/lib/libcrypto/bn/bn_local.h b/src/lib/libcrypto/bn/bn_local.h index a7d61f1b03..106a2cdf2d 100644 --- a/src/lib/libcrypto/bn/bn_local.h +++ b/src/lib/libcrypto/bn/bn_local.h @@ -1,4 +1,4 @@ -/* $OpenBSD: bn_local.h,v 1.60 2025/09/07 05:21:29 jsing Exp $ */ +/* $OpenBSD: bn_local.h,v 1.61 2025/12/05 14:12:32 tb Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -116,6 +116,20 @@ #include +#if BN_BYTES == 8 +#define BN_MASK2 UINT64_C(0xffffffffffffffff) +#define BN_MASK2l UINT64_C(0xffffffff) +#define BN_MASK2h UINT64_C(0xffffffff00000000) +#define BN_BITS 128 +#define BN_BITS4 32 +#else +#define BN_MASK2 UINT32_C(0xffffffff) +#define BN_MASK2l UINT32_C(0xffff) +#define BN_MASK2h UINT32_C(0xffff0000) +#define BN_BITS 64 +#define BN_BITS4 16 +#endif + __BEGIN_HIDDEN_DECLS struct bignum_st { diff --git a/src/lib/libcrypto/ec/ec_internal.h b/src/lib/libcrypto/ec/ec_internal.h index 327d9ea94d..de0affa206 100644 --- a/src/lib/libcrypto/ec/ec_internal.h +++ b/src/lib/libcrypto/ec/ec_internal.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ec_internal.h,v 1.2 2025/08/02 15:44:09 jsing Exp $ */ +/* $OpenBSD: ec_internal.h,v 1.3 2025/12/05 14:12:32 tb Exp $ */ /* * Copyright (c) 2024 Joel Sing * @@ -24,7 +24,7 @@ #define EC_FIELD_ELEMENT_MAX_BYTES \ (EC_FIELD_ELEMENT_MAX_BITS + 7) / 8 #define EC_FIELD_ELEMENT_MAX_WORDS \ - ((EC_FIELD_ELEMENT_MAX_BYTES + BN_BYTES - 1) / BN_BYTES) + ((EC_FIELD_ELEMENT_MAX_BYTES + sizeof(BN_ULONG) - 1) / sizeof(BN_ULONG)) typedef struct { BN_ULONG w[EC_FIELD_ELEMENT_MAX_WORDS]; -- cgit v1.2.3-55-g6feb