From 7a3ccabbaf41a330cdbdad4cc02fb5b931c44951 Mon Sep 17 00:00:00 2001 From: chromatic Date: Thu, 28 Nov 2024 22:56:05 -0800 Subject: [PATCH] Fix {be,le}2{enc,dec} includes Use autoconf probes to see if they're available, and fall back to compat versions in src/compat/endian.h otherwise. --- configure.ac | 2 +- src/compat/endian.h | 40 ++++++++++++++++++++++++++++++++++++++++ src/crypto/scrypt.cpp | 19 +------------------ src/crypto/scrypt.h | 18 +----------------- 4 files changed, 43 insertions(+), 36 deletions(-) diff --git a/configure.ac b/configure.ac index 0200117f67e..73cdfe0e797 100644 --- a/configure.ac +++ b/configure.ac @@ -602,7 +602,7 @@ AC_CHECK_DECLS([strnlen]) # Check for daemon(3), unrelated to --with-daemon (although used by it) AC_CHECK_DECLS([daemon]) -AC_CHECK_DECLS([le16toh, le32toh, le64toh, htole16, htole32, htole64, be16toh, be32toh, be64toh, htobe16, htobe32, htobe64],,, +AC_CHECK_DECLS([le16toh, le32toh, le64toh, htole16, htole32, htole64, be16toh, be32toh, be64toh, htobe16, htobe32, htobe64, be32dec, be32enc, le32dec, le32enc],,, [#if HAVE_ENDIAN_H #include #elif HAVE_SYS_ENDIAN_H diff --git a/src/compat/endian.h b/src/compat/endian.h index 79d6b2fdbb2..996c3a4436b 100644 --- a/src/compat/endian.h +++ b/src/compat/endian.h @@ -191,6 +191,46 @@ inline uint64_t le64toh(uint64_t little_endian_64bits) } #endif // HAVE_DECL_LE64TOH +#if HAVE_DECL_BE32DEC == 0 +static inline uint32_t be32dec(const void *pp) +{ + const uint8_t *p = (uint8_t const *)pp; + return ((uint32_t)(p[3]) + ((uint32_t)(p[2]) << 8) + + ((uint32_t)(p[1]) << 16) + ((uint32_t)(p[0]) << 24)); +} +#endif // HAVE_DECL_BE32DEC + +#if HAVE_DECL_BE32ENC == 0 +static inline void be32enc(void *pp, uint32_t x) +{ + uint8_t *p = (uint8_t *)pp; + p[3] = x & 0xff; + p[2] = (x >> 8) & 0xff; + p[1] = (x >> 16) & 0xff; + p[0] = (x >> 24) & 0xff; +} +#endif // HAVE_DECL_BE32ENC + +#if HAVE_DECL_LE32DEC == 0 +static inline uint32_t le32dec(const void *pp) +{ + const uint8_t *p = (uint8_t const *)pp; + return ((uint32_t)(p[0]) + ((uint32_t)(p[1]) << 8) + + ((uint32_t)(p[2]) << 16) + ((uint32_t)(p[3]) << 24)); +} +#endif // HAVE_DECL_LE32DEC + +#if HAVE_DECL_LE32ENC == 0 +static inline void le32enc(void *pp, uint32_t x) +{ + uint8_t *p = (uint8_t *)pp; + p[0] = x & 0xff; + p[1] = (x >> 8) & 0xff; + p[2] = (x >> 16) & 0xff; + p[3] = (x >> 24) & 0xff; +} +#endif // HAVE_DECL_LE32ENC + #endif // WORDS_BIGENDIAN #endif // BITCOIN_COMPAT_ENDIAN_H diff --git a/src/crypto/scrypt.cpp b/src/crypto/scrypt.cpp index 60781611208..0969f2afd1e 100644 --- a/src/crypto/scrypt.cpp +++ b/src/crypto/scrypt.cpp @@ -32,6 +32,7 @@ #include #include #include +#include "compat/endian.h" #if defined(USE_SSE2) && !defined(USE_SSE2_ALWAYS) #ifdef _MSC_VER @@ -43,24 +44,6 @@ #endif #endif -#ifndef __FreeBSD__ -static inline uint32_t be32dec(const void *pp) -{ - const uint8_t *p = (uint8_t const *)pp; - return ((uint32_t)(p[3]) + ((uint32_t)(p[2]) << 8) + - ((uint32_t)(p[1]) << 16) + ((uint32_t)(p[0]) << 24)); -} - -static inline void be32enc(void *pp, uint32_t x) -{ - uint8_t *p = (uint8_t *)pp; - p[3] = x & 0xff; - p[2] = (x >> 8) & 0xff; - p[1] = (x >> 16) & 0xff; - p[0] = (x >> 24) & 0xff; -} - -#endif /** * PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen): * Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and diff --git a/src/crypto/scrypt.h b/src/crypto/scrypt.h index 8552efe17be..d21ebfa3327 100644 --- a/src/crypto/scrypt.h +++ b/src/crypto/scrypt.h @@ -7,6 +7,7 @@ #define BITCOIN_CRYPTO_SCRYPT_H #include #include +#include "compat/endian.h" #if defined(HAVE_CONFIG_H) #include "bitcoin-config.h" // for USE_SSE2 @@ -36,21 +37,4 @@ void PBKDF2_SHA256(const uint8_t *passwd, size_t passwdlen, const uint8_t *salt, size_t saltlen, uint64_t c, uint8_t *buf, size_t dkLen); -#ifndef __FreeBSD__ -static inline uint32_t le32dec(const void *pp) -{ - const uint8_t *p = (uint8_t const *)pp; - return ((uint32_t)(p[0]) + ((uint32_t)(p[1]) << 8) + - ((uint32_t)(p[2]) << 16) + ((uint32_t)(p[3]) << 24)); -} - -static inline void le32enc(void *pp, uint32_t x) -{ - uint8_t *p = (uint8_t *)pp; - p[0] = x & 0xff; - p[1] = (x >> 8) & 0xff; - p[2] = (x >> 16) & 0xff; - p[3] = (x >> 24) & 0xff; -} -#endif #endif // BITCOIN_CRYPTO_SCRYPT_H