From edf95dbcbdd09632b6ec70858d95000ed9c78885 Mon Sep 17 00:00:00 2001 From: Daniel Pouzzner Date: Thu, 13 Apr 2023 16:56:38 -0500 Subject: [PATCH] add WOLF_C89 clauses to the W64LIT() definitions in wolfssl/wolfcrypt/types.h, and wrap several long long numeric literals with W64LIT() in wolfcrypt/src/{aes.c,blake2b.c,siphash.c}; add WOLF_C89 handling to SP_ULONG_BITS and SP_ULLONG_BITS setup in wolfssl/wolfcrypt/sp_int.h. --- wolfcrypt/src/aes.c | 8 ++++---- wolfcrypt/src/blake2b.c | 12 ++++++------ wolfcrypt/src/siphash.c | 10 +++++----- wolfssl/wolfcrypt/sp_int.h | 16 ++++++++++++++-- wolfssl/wolfcrypt/types.h | 20 ++++++++++++++++---- 5 files changed, 45 insertions(+), 21 deletions(-) diff --git a/wolfcrypt/src/aes.c b/wolfcrypt/src/aes.c index d7d577607..40751e457 100644 --- a/wolfcrypt/src/aes.c +++ b/wolfcrypt/src/aes.c @@ -5398,10 +5398,10 @@ static WC_INLINE void GMULT(byte *x, byte m[32][AES_BLOCK_SIZE]) a = (z8[1] >> 56) & 0xf; /* Rotate z by 4-bits */ - n3 = z8[1] & 0xf0f0f0f0f0f0f0f0ULL; - n2 = z8[1] & 0x0f0f0f0f0f0f0f0fULL; - n1 = z8[0] & 0xf0f0f0f0f0f0f0f0ULL; - n0 = z8[0] & 0x0f0f0f0f0f0f0f0fULL; + n3 = z8[1] & W64LIT(0xf0f0f0f0f0f0f0f0U); + n2 = z8[1] & W64LIT(0x0f0f0f0f0f0f0f0fU); + n1 = z8[0] & W64LIT(0xf0f0f0f0f0f0f0f0U); + n0 = z8[0] & W64LIT(0x0f0f0f0f0f0f0f0fU); z8[1] = (n3 >> 4) | (n2 << 12) | (n0 >> 52); z8[0] = (n1 >> 4) | (n0 << 12); diff --git a/wolfcrypt/src/blake2b.c b/wolfcrypt/src/blake2b.c index 269e7c404..ffd85123b 100644 --- a/wolfcrypt/src/blake2b.c +++ b/wolfcrypt/src/blake2b.c @@ -48,10 +48,10 @@ static const word64 blake2b_IV[8] = { - 0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL, - 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL, - 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL, - 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL + W64LIT(0x6a09e667f3bcc908U), W64LIT(0xbb67ae8584caa73bU), + W64LIT(0x3c6ef372fe94f82bU), W64LIT(0xa54ff53a5f1d36f1U), + W64LIT(0x510e527fade682d1U), W64LIT(0x9b05688c2b3e6c1fU), + W64LIT(0x1f83d9abfb41bd6bU), W64LIT(0x5be0cd19137e2179U) }; static const byte blake2b_sigma[12][16] = @@ -73,7 +73,7 @@ static const byte blake2b_sigma[12][16] = static WC_INLINE int blake2b_set_lastnode( blake2b_state *S ) { - S->f[1] = ~0ULL; + S->f[1] = ~W64LIT(0U); return 0; } @@ -82,7 +82,7 @@ static WC_INLINE int blake2b_set_lastblock( blake2b_state *S ) { if( S->last_node ) blake2b_set_lastnode( S ); - S->f[0] = ~0ULL; + S->f[0] = ~W64LIT(0U); return 0; } diff --git a/wolfcrypt/src/siphash.c b/wolfcrypt/src/siphash.c index 4494b436c..a2b031da8 100644 --- a/wolfcrypt/src/siphash.c +++ b/wolfcrypt/src/siphash.c @@ -165,15 +165,15 @@ int wc_InitSipHash(SipHash* sipHash, const unsigned char* key, word64 k1 = GET_U64(key + 8); /* Initialize state with key. */ - sipHash->v[0] = 0x736f6d6570736575ULL; + sipHash->v[0] = W64LIT(0x736f6d6570736575U); if (outSz == SIPHASH_MAC_SIZE_8) { - sipHash->v[1] = 0x646f72616e646f6dULL; + sipHash->v[1] = W64LIT(0x646f72616e646f6dU); } else { - sipHash->v[1] = 0x646f72616e646f83ULL; + sipHash->v[1] = W64LIT(0x646f72616e646f83U); } - sipHash->v[2] = 0x6c7967656e657261ULL; - sipHash->v[3] = 0x7465646279746573ULL; + sipHash->v[2] = W64LIT(0x6c7967656e657261U); + sipHash->v[3] = W64LIT(0x7465646279746573U); sipHash->v[0] ^= k0; sipHash->v[1] ^= k1; diff --git a/wolfssl/wolfcrypt/sp_int.h b/wolfssl/wolfcrypt/sp_int.h index 90f2e3f7f..7dd679e61 100644 --- a/wolfssl/wolfcrypt/sp_int.h +++ b/wolfssl/wolfcrypt/sp_int.h @@ -96,7 +96,12 @@ extern "C" { #error "Size of unsigned int not detected" #endif -#if !defined(NO_64BIT) && ULONG_MAX == 18446744073709551615ULL && \ +#if defined(WOLF_C89) && !defined(NO_64BIT) && ULONG_MAX == 18446744073709551615UL + #define SP_ULONG_BITS 64 + + typedef unsigned long sp_uint64; + typedef long sp_int64; +#elif !defined(WOLF_C89) && !defined(NO_64BIT) && ULONG_MAX == 18446744073709551615ULL && \ 4294967295UL != 18446744073709551615ULL /* verify pre-processor supports * 64-bit ULL types */ #define SP_ULONG_BITS 64 @@ -122,7 +127,14 @@ extern "C" { #endif #ifdef ULLONG_MAX - #if ULLONG_MAX == 18446744073709551615ULL + #if defined(WOLF_C89) && ULLONG_MAX == 18446744073709551615UL + #define SP_ULLONG_BITS 64 + + #if SP_ULLONG_BITS > SP_ULONG_BITS + typedef unsigned long long sp_uint64; + typedef long long sp_int64; + #endif + #elif !defined(WOLF_C89) && ULLONG_MAX == 18446744073709551615ULL #define SP_ULLONG_BITS 64 #if SP_ULLONG_BITS > SP_ULONG_BITS diff --git a/wolfssl/wolfcrypt/types.h b/wolfssl/wolfcrypt/types.h index 8e18d8c63..28daab712 100644 --- a/wolfssl/wolfcrypt/types.h +++ b/wolfssl/wolfcrypt/types.h @@ -174,17 +174,29 @@ decouple library dependencies with standard string, memory and so on. typedef unsigned long long word64; #elif defined(SIZEOF_LONG) && SIZEOF_LONG == 8 #define WORD64_AVAILABLE - #define W64LIT(x) x##LL + #ifdef WOLF_C89 + #define W64LIT(x) x##L + #else + #define W64LIT(x) x##LL + #endif typedef long sword64; typedef unsigned long word64; #elif defined(SIZEOF_LONG_LONG) && SIZEOF_LONG_LONG == 8 #define WORD64_AVAILABLE - #define W64LIT(x) x##LL + #ifdef WOLF_C89 + #define W64LIT(x) x##L + #else + #define W64LIT(x) x##LL + #endif typedef long long sword64; typedef unsigned long long word64; #elif defined(__SIZEOF_LONG_LONG__) && __SIZEOF_LONG_LONG__ == 8 #define WORD64_AVAILABLE - #define W64LIT(x) x##LL + #ifdef WOLF_C89 + #define W64LIT(x) x##L + #else + #define W64LIT(x) x##LL + #endif typedef long long sword64; typedef unsigned long long word64; #endif @@ -971,7 +983,7 @@ typedef struct w64wrapper { DYNAMIC_TYPE_SNIFFER_PB_BUFFER = 1003, DYNAMIC_TYPE_SNIFFER_TICKET_ID = 1004, DYNAMIC_TYPE_SNIFFER_NAMED_KEY = 1005, - DYNAMIC_TYPE_SNIFFER_KEY = 1006, + DYNAMIC_TYPE_SNIFFER_KEY = 1006 }; /* max error buffer string size */