Introduce LTC_ALIGNED_BUF_SIZE(). [skip ci]

Since there's absolutely no way to ensure that a struct is somehow
aligned -- all those compile-time mechanism are best-effort and no
guarantee -- we align the necessary buffers at run-time.

AES-NI already introduced that, let's improve a bit upon its usage.

Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
This commit is contained in:
Steffen Jaeckel 2026-04-14 08:45:14 +02:00
parent 14352f5036
commit cf29490bfc
3 changed files with 7 additions and 2 deletions

View file

@ -35,7 +35,7 @@ struct saferp_key {
#ifdef LTC_RIJNDAEL
struct rijndael_key {
unsigned char K[(60 + 60 + 4) * sizeof(ulong32)];
unsigned char K[LTC_ALIGNED_BUF_SIZE(ulong32, 60 + 60, 16)];
ulong32 *eK;
ulong32 *dK;
int Nr;

View file

@ -453,6 +453,11 @@ static inline ulong64 ROR64(ulong64 word, int i)
#define LTC_UNUSED_PARAM(x) (void)(x)
#endif
/* Calculates the number of bytes required to hold a buffer that will later on be
* aligned by using LTC_ALIGN_BUF().
*/
#define LTC_ALIGNED_BUF_SIZE(type, n, align) (((n) + ((align)/sizeof(type))) * sizeof(type))
/* there is no snprintf before Visual C++ 2015 */
#if defined(_MSC_VER) && _MSC_VER < 1900
#define snprintf _snprintf

View file

@ -33,7 +33,7 @@ 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)))))
#define LTC_ALIGN_BUF(buf, align) ((void*)((ltc_uintptr)&((unsigned char*)(buf))[(align) - 1] & (~(CONSTPTR(align) - CONSTPTR(1)))))
#define LTC_OID_MAX_STRLEN 256