From 0dccf98a21beb245f6cd9ed76fb7368529df09c7 Mon Sep 17 00:00:00 2001 From: Tim Ruffing Date: Tue, 21 Jul 2020 14:05:56 +0200 Subject: [PATCH] Use preprocessor macros instead of autoconf to detect endianness This does not fix any particular issue but it's preferable to not rely on autoconf. This avoids endianness mess for users on BE hosts if they use their build without autoconf. The macros are carefully written to err on the side of the caution, e.g., we #error if the user manually configures a different endianness than what we detect. --- configure.ac | 2 -- src/hash_impl.h | 5 +++-- src/util.h | 14 ++++++++++++++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/configure.ac b/configure.ac index 6021b760b517c..2fd340a6a515b 100644 --- a/configure.ac +++ b/configure.ac @@ -493,8 +493,6 @@ if test x"$enable_module_recovery" = x"yes"; then AC_DEFINE(ENABLE_MODULE_RECOVERY, 1, [Define this symbol to enable the ECDSA pubkey recovery module]) fi -AC_C_BIGENDIAN() - if test x"$use_external_asm" = x"yes"; then AC_DEFINE(USE_EXTERNAL_ASM, 1, [Define this symbol if an external (non-inline) assembly implementation is used]) fi diff --git a/src/hash_impl.h b/src/hash_impl.h index 782f97216c284..1985a07836daf 100644 --- a/src/hash_impl.h +++ b/src/hash_impl.h @@ -8,6 +8,7 @@ #define SECP256K1_HASH_IMPL_H #include "hash.h" +#include "util.h" #include #include @@ -27,9 +28,9 @@ (h) = t1 + t2; \ } while(0) -#ifdef WORDS_BIGENDIAN +#if defined(SECP256K1_BIG_ENDIAN) #define BE32(x) (x) -#else +#elif defined(SECP256K1_LITTLE_ENDIAN) #define BE32(p) ((((p) & 0xFF) << 24) | (((p) & 0xFF00) << 8) | (((p) & 0xFF0000) >> 8) | (((p) & 0xFF000000) >> 24)) #endif diff --git a/src/util.h b/src/util.h index 8289e23e0c5db..befb9dccdb414 100644 --- a/src/util.h +++ b/src/util.h @@ -179,6 +179,20 @@ static SECP256K1_INLINE void *manual_alloc(void** prealloc_ptr, size_t alloc_siz SECP256K1_GNUC_EXT typedef unsigned __int128 uint128_t; #endif +#if defined(__BYTE_ORDER__) +# if defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ && !defined(SECP256K1_LITTLE_ENDIAN) +# define SECP256K1_LITTLE_ENDIAN +# elif defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ && !defined(SECP256K1_BIG_ENDIAN) +# define SECP256K1_BIG_ENDIAN +# endif +#endif +#if defined(_MSC_VER) && defined(_WIN32) && !defined(SECP256K1_LITTLE_ENDIAN) +# define SECP256K1_LITTLE_ENDIAN +#endif +#if defined(SECP256K1_LITTLE_ENDIAN) == defined(SECP256K1_BIG_ENDIAN) +# error Please make sure that either SECP256K1_LITTLE_ENDIAN or SECP256K1_BIG_ENDIAN is set, see src/util.h. +#endif + /* Zero memory if flag == 1. Flag must be 0 or 1. Constant time. */ static SECP256K1_INLINE void memczero(void *s, size_t len, int flag) { unsigned char *p = (unsigned char *)s;