Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/Common/Tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <string.h>
#include "Pkcs5.h"
#include "cpu.h"
#include "Volumes.h"

typedef struct {
CRYPTOPP_ALIGN_DATA(16) unsigned __int8 key1[32];
Expand Down Expand Up @@ -1280,6 +1281,28 @@ BOOL TestSectorBufEncryption (PCRYPTO_INFO ci)
return (nTestsPerformed == 150);
}

#ifndef TC_WINDOWS_DRIVER
// Must stay smaller than the alignment under test: the ABI aligns larger objects by itself.
#define TC_ALIGNMENT_PROBE_SIZE 4

/* Verifies that CRYPTOPP_ALIGN_DATA actually delivers the alignment it requests. It expands
to nothing for compilers that are neither MSVC nor GCC/Clang (see Crypto/config.h), and the
SIMD backends issue aligned loads against buffers declared with it. */
static BOOL TestBufferAlignment (void)
{
static CRYPTOPP_ALIGN_DATA(TC_DERIVED_KEY_BUFFER_ALIGNMENT) unsigned char derivedKeyProbe[TC_ALIGNMENT_PROBE_SIZE];
static CRYPTOPP_ALIGN_DATA(TC_KEY_INFO_BUFFER_ALIGNMENT) unsigned char keyInfoProbe[TC_ALIGNMENT_PROBE_SIZE];

if (!TC_IS_ALIGNED (derivedKeyProbe, TC_DERIVED_KEY_BUFFER_ALIGNMENT))
return FALSE;

if (!TC_IS_ALIGNED (keyInfoProbe, TC_KEY_INFO_BUFFER_ALIGNMENT))
return FALSE;

return TRUE;
}
#endif

static BOOL DoAutoTestAlgorithms (void)
{
PCRYPTO_INFO ci;
Expand Down Expand Up @@ -1489,6 +1512,13 @@ static BOOL DoAutoTestAlgorithms (void)
bFailed = TRUE;

crypto_close (ci);

#ifndef TC_WINDOWS_DRIVER
/* Not run in the driver: a failing self-test there reaches TC_BUG_CHECK and would bugcheck the machine. */
if (!TestBufferAlignment ())
bFailed = TRUE;
#endif

return !bFailed;
}

Expand Down
3 changes: 3 additions & 0 deletions src/Common/Volumes.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ extern "C" {
// Required 16-byte alignment for KEY_INFO buffer to ensure optimal performance and compatibility with SIMD instructions.
#define TC_KEY_INFO_BUFFER_ALIGNMENT 16

// TRUE if the address meets the given alignment.
#define TC_IS_ALIGNED(address, alignment) ((((uint64) (address)) % (alignment)) == 0)

// Current volume format version (created by TrueCrypt 6.0+)
#define TC_VOLUME_FORMAT_VERSION 2

Expand Down
20 changes: 20 additions & 0 deletions src/Volume/EncryptionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,30 @@ namespace VeraCrypt

void EncryptionTest::TestAll ()
{
TestAlignment();
TestAll (false);
TestAll (true);
}

// Must stay smaller than the alignment under test: the ABI aligns larger objects by itself.
#define TC_ALIGNMENT_PROBE_SIZE 4

// Verifies that CRYPTOPP_ALIGN_DATA actually delivers the alignment it requests.
// It expands to nothing for compilers that are neither MSVC nor GCC/Clang (see
// Crypto/config.h), and the SIMD backends issue aligned loads against buffers
// declared with it, so a silent expansion to nothing would fault at run time.
void EncryptionTest::TestAlignment ()
{
static CRYPTOPP_ALIGN_DATA(TC_DERIVED_KEY_BUFFER_ALIGNMENT) uint8 derivedKeyProbe[TC_ALIGNMENT_PROBE_SIZE];
static CRYPTOPP_ALIGN_DATA(TC_KEY_INFO_BUFFER_ALIGNMENT) uint8 keyInfoProbe[TC_ALIGNMENT_PROBE_SIZE];

if (!TC_IS_ALIGNED (derivedKeyProbe, TC_DERIVED_KEY_BUFFER_ALIGNMENT))
throw TestFailed (SRC_POS);

if (!TC_IS_ALIGNED (keyInfoProbe, TC_KEY_INFO_BUFFER_ALIGNMENT))
throw TestFailed (SRC_POS);
}

void EncryptionTest::TestAll (bool enableCpuEncryptionSupport)
{
bool hwSupportEnabled = Cipher::IsHwSupportEnabled();
Expand Down
1 change: 1 addition & 0 deletions src/Volume/EncryptionTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace VeraCrypt
static void TestAll (bool enableCpuEncryptionSupport);

protected:
static void TestAlignment ();
static void TestCiphers ();
static void TestLegacyModes ();
static void TestPkcs5 ();
Expand Down
Loading