Skip to content

Commit cf29490

Browse files
committed
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>
1 parent 14352f5 commit cf29490

3 files changed

Lines changed: 7 additions & 2 deletions

File tree

src/headers/tomcrypt_cipher.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ struct saferp_key {
3535

3636
#ifdef LTC_RIJNDAEL
3737
struct rijndael_key {
38-
unsigned char K[(60 + 60 + 4) * sizeof(ulong32)];
38+
unsigned char K[LTC_ALIGNED_BUF_SIZE(ulong32, 60 + 60, 16)];
3939
ulong32 *eK;
4040
ulong32 *dK;
4141
int Nr;

src/headers/tomcrypt_macros.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,11 @@ static inline ulong64 ROR64(ulong64 word, int i)
453453
#define LTC_UNUSED_PARAM(x) (void)(x)
454454
#endif
455455

456+
/* Calculates the number of bytes required to hold a buffer that will later on be
457+
* aligned by using LTC_ALIGN_BUF().
458+
*/
459+
#define LTC_ALIGNED_BUF_SIZE(type, n, align) (((n) + ((align)/sizeof(type))) * sizeof(type))
460+
456461
/* there is no snprintf before Visual C++ 2015 */
457462
#if defined(_MSC_VER) && _MSC_VER < 1900
458463
#define snprintf _snprintf

src/headers/tomcrypt_private.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ LTC_STATIC_ASSERT(correct_ltc_uintptr_size, sizeof(ltc_uintptr) == sizeof(void*)
3333
/* Aligns a `unsigned char` buffer `buf` to `n` bytes and returns that aligned address.
3434
* Make sure that the buffer that is passed is huge enough.
3535
*/
36-
#define LTC_ALIGN_BUF(buf, n) ((void*)((ltc_uintptr)&((unsigned char*)(buf))[n - 1] & (~(CONSTPTR(n) - CONSTPTR(1)))))
36+
#define LTC_ALIGN_BUF(buf, align) ((void*)((ltc_uintptr)&((unsigned char*)(buf))[(align) - 1] & (~(CONSTPTR(align) - CONSTPTR(1)))))
3737

3838
#define LTC_OID_MAX_STRLEN 256
3939

0 commit comments

Comments
 (0)