From 62add4d0ce3500d31fbb5c861fa67f5f3766803c Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Thu, 5 Oct 2023 10:26:05 +0200 Subject: [PATCH] Add `LTC_ALIGN_BUF()` Signed-off-by: Steffen Jaeckel --- demos/gcm-file/gcm_filehandle.c | 4 +--- src/encauth/gcm/gcm_memory.c | 4 +--- src/headers/tomcrypt_cfg.h | 2 +- src/headers/tomcrypt_private.h | 21 +++++++++++++++++++++ 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/demos/gcm-file/gcm_filehandle.c b/demos/gcm-file/gcm_filehandle.c index 11c55a62..6065f966 100644 --- a/demos/gcm-file/gcm_filehandle.c +++ b/demos/gcm-file/gcm_filehandle.c @@ -98,9 +98,7 @@ int gcm_filehandle( int cipher, * but again it's only for SSE2 anyways, so who cares? */ #ifdef LTC_GCM_TABLES_SSE2 - if ((unsigned long)gcm & 15) { - gcm = (gcm_state *)((unsigned long)gcm + (16 - ((unsigned long)gcm & 15))); - } + gcm = LTC_ALIGN_BUF(gcm, 16); #endif if ((err = gcm_init(gcm, cipher, key, keylen)) != CRYPT_OK) { diff --git a/src/encauth/gcm/gcm_memory.c b/src/encauth/gcm/gcm_memory.c index 4da5d542..6de0b309 100644 --- a/src/encauth/gcm/gcm_memory.c +++ b/src/encauth/gcm/gcm_memory.c @@ -70,9 +70,7 @@ int gcm_memory( int cipher, * but again it's only for SSE2 anyways, so who cares? */ #ifdef LTC_GCM_TABLES_SSE2 - if ((unsigned long)gcm & 15) { - gcm = (gcm_state *)((unsigned long)gcm + (16 - ((unsigned long)gcm & 15))); - } + gcm = LTC_ALIGN_BUF(gcm, 16); #endif if ((err = gcm_init(gcm, cipher, key, keylen)) != CRYPT_OK) { diff --git a/src/headers/tomcrypt_cfg.h b/src/headers/tomcrypt_cfg.h index 35af3008..3d90d03c 100644 --- a/src/headers/tomcrypt_cfg.h +++ b/src/headers/tomcrypt_cfg.h @@ -209,7 +209,7 @@ LTC_EXPORT int LTC_CALL XSTRCMP(const char *s1, const char *s2); typedef unsigned __int64 ulong64; typedef __int64 long64; #else - #define CONST64(n) n ## ULL + #define CONST64(n) n ## uLL typedef unsigned long long ulong64; typedef long long long64; #endif diff --git a/src/headers/tomcrypt_private.h b/src/headers/tomcrypt_private.h index aa251e2a..041bdd63 100644 --- a/src/headers/tomcrypt_private.h +++ b/src/headers/tomcrypt_private.h @@ -6,9 +6,30 @@ /* * Internal Macros */ +/* Static assertion */ +#define LTC_STATIC_ASSERT(msg, cond) typedef char ltc_static_assert_##msg[(cond) ? 1 : -1]; #define LTC_PAD_MASK (0xF000U) +#if defined(ENDIAN_64BITWORD) + #define CONSTPTR(n) CONST64(n) +#else + #define CONSTPTR(n) n ## uL +#endif + +LTC_STATIC_ASSERT(correct_CONSTPTR_size, sizeof(CONSTPTR(1)) == sizeof(void*)) + +/* Poor-man's `uintptr_t` since we can't use stdint.h + * c.f. https://github.com/DCIT/perl-CryptX/issues/95#issuecomment-1745280962 */ +typedef size_t ltc_uintptr; + +LTC_STATIC_ASSERT(correct_ltc_uintptr_size, sizeof(ltc_uintptr) == sizeof(void*)) + +/* Aligns a `unsigned char` buffer `buf` to `n` bytes and returns that aligned address. + * Make sure that the buffer that is passed is huge enough. + */ +#define LTC_ALIGN_BUF(buf, n) ((void*)((ltc_uintptr)&((unsigned char*)(buf))[n - 1] & (~(CONSTPTR(n) - CONSTPTR(1))))) + /* `NULL` as defined by the standard is not guaranteed to be of a pointer * type. In order to make sure that in vararg API's a pointer type is used, * define our own version and use that one internally.