From 733ab0781327c061f0bbf22162a158784abf3334 Mon Sep 17 00:00:00 2001 From: Ulrich Weigand Date: Thu, 10 Nov 2022 14:23:38 +0100 Subject: [PATCH] Support big-endian platforms --- crypto/fipsmodule/aes/aes_nohw.c | 12 +++++++++ crypto/internal.h | 42 ++++++++++++++++++++++++-------- src/lib.rs | 8 ++++-- 3 files changed, 50 insertions(+), 12 deletions(-) diff --git a/crypto/fipsmodule/aes/aes_nohw.c b/crypto/fipsmodule/aes/aes_nohw.c index 1b35f635a6..731178516d 100644 --- a/crypto/fipsmodule/aes/aes_nohw.c +++ b/crypto/fipsmodule/aes/aes_nohw.c @@ -273,6 +273,9 @@ static inline aes_word_t aes_nohw_delta_swap(aes_word_t a, aes_word_t mask, // http://programming.sirrida.de/calcperm.php on smaller inputs. #if defined(OPENSSL_64_BIT) static inline uint64_t aes_nohw_compact_word(uint64_t a) { +#if defined(RING_BIG_ENDIAN) + a = CRYPTO_bswap8(a); +#endif // Numbering the 64/2 = 16 4-bit chunks, least to most significant, we swap // quartets of those chunks: // 0 1 2 3 | 4 5 6 7 | 8 9 10 11 | 12 13 14 15 => @@ -294,10 +297,16 @@ static inline uint64_t aes_nohw_uncompact_word(uint64_t a) { a = aes_nohw_delta_swap(a, UINT64_C(0x00000000ffff0000), 16); a = aes_nohw_delta_swap(a, UINT64_C(0x0000ff000000ff00), 8); a = aes_nohw_delta_swap(a, UINT64_C(0x00f000f000f000f0), 4); +#if defined(RING_BIG_ENDIAN) + a = CRYPTO_bswap8(a); +#endif return a; } #else // !OPENSSL_64_BIT static inline uint32_t aes_nohw_compact_word(uint32_t a) { +#if defined(RING_BIG_ENDIAN) + a = CRYPTO_bswap4(a); +#endif // Numbering the 32/2 = 16 pairs of bits, least to most significant, we swap: // 0 1 2 3 | 4 5 6 7 | 8 9 10 11 | 12 13 14 15 => // 0 4 2 6 | 1 5 3 7 | 8 12 10 14 | 9 13 11 15 @@ -316,6 +325,9 @@ static inline uint32_t aes_nohw_uncompact_word(uint32_t a) { // Reverse the steps of |aes_nohw_uncompact_word|. a = aes_nohw_delta_swap(a, 0x0000f0f0, 12); a = aes_nohw_delta_swap(a, 0x00cc00cc, 6); +#if defined(RING_BIG_ENDIAN) + a = CRYPTO_bswap4(a); +#endif return a; } diff --git a/crypto/internal.h b/crypto/internal.h index 0de2a3e86b..5f92e34e3f 100644 --- a/crypto/internal.h +++ b/crypto/internal.h @@ -401,55 +401,77 @@ static inline void *OPENSSL_memset(void *dst, int c, size_t n) { // endianness. They use |memcpy|, and so avoid alignment or strict aliasing // requirements on the input and output pointers. +#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) +#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ +#define RING_BIG_ENDIAN +#endif +#endif + static inline uint32_t CRYPTO_load_u32_le(const void *in) { uint32_t v; OPENSSL_memcpy(&v, in, sizeof(v)); +#if defined(RING_BIG_ENDIAN) + return CRYPTO_bswap4(v); +#else return v; +#endif } static inline void CRYPTO_store_u32_le(void *out, uint32_t v) { +#if defined(RING_BIG_ENDIAN) + v = CRYPTO_bswap4(v); +#endif OPENSSL_memcpy(out, &v, sizeof(v)); } static inline uint32_t CRYPTO_load_u32_be(const void *in) { uint32_t v; OPENSSL_memcpy(&v, in, sizeof(v)); +#if !defined(RING_BIG_ENDIAN) return CRYPTO_bswap4(v); +#else + return v; +#endif } static inline void CRYPTO_store_u32_be(void *out, uint32_t v) { +#if !defined(RING_BIG_ENDIAN) v = CRYPTO_bswap4(v); +#endif OPENSSL_memcpy(out, &v, sizeof(v)); } static inline uint64_t CRYPTO_load_u64_le(const void *in) { uint64_t v; OPENSSL_memcpy(&v, in, sizeof(v)); +#if defined(RING_BIG_ENDIAN) + return CRYPTO_bswap8(v); +#else return v; +#endif } static inline void CRYPTO_store_u64_le(void *out, uint64_t v) { +#if defined(RING_BIG_ENDIAN) + v = CRYPTO_bswap8(v); +#endif OPENSSL_memcpy(out, &v, sizeof(v)); } static inline uint64_t CRYPTO_load_u64_be(const void *ptr) { uint64_t ret; OPENSSL_memcpy(&ret, ptr, sizeof(ret)); +#if !defined(RING_BIG_ENDIAN) return CRYPTO_bswap8(ret); +#else + return ret; +#endif } static inline void CRYPTO_store_u64_be(void *out, uint64_t v) { +#if !defined(RING_BIG_ENDIAN) v = CRYPTO_bswap8(v); - OPENSSL_memcpy(out, &v, sizeof(v)); -} - -static inline crypto_word_t CRYPTO_load_word_le(const void *in) { - crypto_word_t v; - OPENSSL_memcpy(&v, in, sizeof(v)); - return v; -} - -static inline void CRYPTO_store_word_le(void *out, crypto_word_t v) { +#endif OPENSSL_memcpy(out, &v, sizeof(v)); } diff --git a/src/lib.rs b/src/lib.rs index 644708c828..99e1ff3ee6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -115,5 +115,9 @@ mod sealed { pub trait Sealed {} } -// TODO: https://github.com/briansmith/ring/issues/1555. -const _LITTLE_ENDIAN_ONLY: () = assert!(cfg!(target_endian = "little")); +// XXX: 64-bit big endian is tested; 32-bit is not. +// TODO: Add 32-bit big endian test coverage to CI. +const _ENDIAN_TESTING: () = assert!(cfg!(any( + target_endian = "little", + target_pointer_width = "64" +)));