From 0478be3626065a4fb0e5ddccefd3759d9b273ce9 Mon Sep 17 00:00:00 2001 From: Bernard Ladenthin Date: Mon, 21 Apr 2025 11:26:34 +0200 Subject: [PATCH] Add alignment constants for derived key and KEY_INFO buffers to ensure SIMD compatibility. (#1526) --- src/Common/Volumes.c | 8 ++++---- src/Common/Volumes.h | 6 ++++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Common/Volumes.c b/src/Common/Volumes.c index e2323d34..a5a5e037 100644 --- a/src/Common/Volumes.c +++ b/src/Common/Volumes.c @@ -173,11 +173,11 @@ int ReadVolumeHeader (BOOL bBoot, unsigned char *encryptedHeader, Password *pass { unsigned char header[TC_VOLUME_HEADER_EFFECTIVE_SIZE]; unsigned char* keyInfoBuffer = NULL; - int keyInfoBufferSize = sizeof (KEY_INFO) + 16; + int keyInfoBufferSize = sizeof (KEY_INFO) + TC_KEY_INFO_BUFFER_ALIGNMENT; size_t keyInfoBufferOffset; PKEY_INFO keyInfo; PCRYPTO_INFO cryptoInfo; - CRYPTOPP_ALIGN_DATA(16) unsigned char dk[MASTER_KEYDATA_SIZE]; + CRYPTOPP_ALIGN_DATA(TC_DERIVED_KEY_BUFFER_ALIGNMENT) unsigned char dk[MASTER_KEYDATA_SIZE]; int enqPkcs5Prf, pkcs5_prf; uint16 headerVersion; int status = ERR_PARAMETER_INCORRECT; @@ -199,7 +199,7 @@ int ReadVolumeHeader (BOOL bBoot, unsigned char *encryptedHeader, Password *pass keyInfoBuffer = TCalloc(keyInfoBufferSize); if (!keyInfoBuffer) return ERR_OUTOFMEMORY; - keyInfoBufferOffset = 16 - (((uint64) keyInfoBuffer) % 16); + keyInfoBufferOffset = TC_KEY_INFO_BUFFER_ALIGNMENT - (((uint64) keyInfoBuffer) % TC_KEY_INFO_BUFFER_ALIGNMENT); keyInfo = (PKEY_INFO) (keyInfoBuffer + keyInfoBufferOffset); #if !defined(DEVICE_DRIVER) && !defined(_UEFI) @@ -884,7 +884,7 @@ int CreateVolumeHeaderInMemory (HWND hwndDlg, BOOL bBoot, unsigned char *header, #endif // !defined(_UEFI) { unsigned char *p = header; - static CRYPTOPP_ALIGN_DATA(16) KEY_INFO keyInfo; + static CRYPTOPP_ALIGN_DATA(TC_KEY_INFO_BUFFER_ALIGNMENT) KEY_INFO keyInfo; int nUserKeyLen = password? password->Length : 0; PCRYPTO_INFO cryptoInfo = crypto_open (); diff --git a/src/Common/Volumes.h b/src/Common/Volumes.h index 7530072f..51e62181 100644 --- a/src/Common/Volumes.h +++ b/src/Common/Volumes.h @@ -35,6 +35,12 @@ extern "C" { // specifies the minimum program version required to decrypt the system partition/drive #define TC_SYSENC_KEYSCOPE_MIN_REQ_PROG_VERSION 0x010b +// Required 16-byte alignment for derived key buffers to ensure optimal performance and compatibility with SIMD instructions. +#define TC_DERIVED_KEY_BUFFER_ALIGNMENT 16 + +// Required 16-byte alignment for KEY_INFO buffer to ensure optimal performance and compatibility with SIMD instructions. +#define TC_KEY_INFO_BUFFER_ALIGNMENT 16 + // Current volume format version (created by TrueCrypt 6.0+) #define TC_VOLUME_FORMAT_VERSION 2