diff options
author | Mounir IDRASSI <mounir.idrassi@idrix.fr> | 2016-09-28 00:14:05 +0200 |
---|---|---|
committer | Mounir IDRASSI <mounir.idrassi@idrix.fr> | 2016-10-17 18:40:19 +0200 |
commit | 57ce7aab7be0ca2c3e990eaf7d9cf3691efd1ea0 (patch) | |
tree | 3cfd2715c14b191f2817f1bac60f8236a12cb870 /src | |
parent | 3e029b0dde5326a4f1221c86a2050aafceaafa3e (diff) | |
download | VeraCrypt-57ce7aab7be0ca2c3e990eaf7d9cf3691efd1ea0.tar.gz VeraCrypt-57ce7aab7be0ca2c3e990eaf7d9cf3691efd1ea0.zip |
Use properly aligned memory in code using Streebog hash implementation that uses SSE.
Diffstat (limited to 'src')
-rw-r--r-- | src/Core/RandomNumberGenerator.cpp | 2 | ||||
-rw-r--r-- | src/Crypto/GostCipher.c | 2 | ||||
-rw-r--r-- | src/Crypto/GostCipher.h | 3 | ||||
-rw-r--r-- | src/Platform/Buffer.cpp | 37 | ||||
-rw-r--r-- | src/Platform/Buffer.h | 14 | ||||
-rw-r--r-- | src/Platform/Memory.cpp | 28 | ||||
-rw-r--r-- | src/Platform/Memory.h | 2 |
7 files changed, 65 insertions, 23 deletions
diff --git a/src/Core/RandomNumberGenerator.cpp b/src/Core/RandomNumberGenerator.cpp index 9551f585..ea05fe6f 100644 --- a/src/Core/RandomNumberGenerator.cpp +++ b/src/Core/RandomNumberGenerator.cpp @@ -173,7 +173,7 @@ namespace VeraCrypt Running = true; EnrichedByUser = false; - Pool.Allocate (PoolSize); + Pool.Allocate (PoolSize, 16); Test(); if (!PoolHash) diff --git a/src/Crypto/GostCipher.c b/src/Crypto/GostCipher.c index 4d7e5070..0fd3941a 100644 --- a/src/Crypto/GostCipher.c +++ b/src/Crypto/GostCipher.c @@ -102,7 +102,7 @@ void gost_set_key(const byte *key, gost_kds *ks, int useDynamicSbox) #endif //Generate pseudorandom data based on the key STREEBOG_init(&sctx); - STREEBOG_add(&sctx, key, 32); + STREEBOG_add(&sctx, ks->key, 32); STREEBOG_finalize(&sctx, sbox_seed); #if defined (DEVICE_DRIVER) && !defined (_WIN64) diff --git a/src/Crypto/GostCipher.h b/src/Crypto/GostCipher.h index 36afb777..6031c438 100644 --- a/src/Crypto/GostCipher.h +++ b/src/Crypto/GostCipher.h @@ -13,6 +13,7 @@ #define GOST_CIPHER_H #include "Common/Tcdefs.h" +#include "config.h" #ifdef __cplusplus extern "C" { @@ -47,7 +48,7 @@ typedef unsigned int gst_udword; typedef struct gost_kds { - byte key[32]; + CRYPTOPP_ALIGN_DATA(16) byte key[32]; gst_udword sbox_cvt[256 * 4]; byte sbox[8][16]; } gost_kds; diff --git a/src/Platform/Buffer.cpp b/src/Platform/Buffer.cpp index 4948a875..a53fd3da 100644 --- a/src/Platform/Buffer.cpp +++ b/src/Platform/Buffer.cpp @@ -15,13 +15,13 @@ namespace VeraCrypt { - Buffer::Buffer () : DataPtr (nullptr), DataSize (0) + Buffer::Buffer () : DataPtr (nullptr), DataSize (0), DataAlignment (0) { } - Buffer::Buffer (size_t size) : DataPtr (nullptr), DataSize (0) + Buffer::Buffer (size_t size, size_t alignment) : DataPtr (nullptr), DataSize (0), DataAlignment (0) { - Allocate (size); + Allocate (size, alignment); } Buffer::~Buffer () @@ -30,37 +30,42 @@ namespace VeraCrypt Free (); } - void Buffer::Allocate (size_t size) + void Buffer::Allocate (size_t size, size_t alignment) { if (size < 1) throw ParameterIncorrect (SRC_POS); if (DataPtr != nullptr) { - if (DataSize == size) + if ((DataSize == size) && (DataAlignment == alignment)) return; Free(); } try { - DataPtr = static_cast<byte *> (Memory::Allocate (size)); + DataPtr = static_cast<byte *> ((alignment > 0)? Memory::AllocateAligned (size, alignment) : Memory::Allocate (size)); DataSize = size; + DataAlignment = alignment; } catch (...) { DataPtr = nullptr; DataSize = 0; + DataAlignment = 0; throw; } } - void Buffer::CopyFrom (const ConstBufferPtr &bufferPtr) + void Buffer::CopyFrom (const ConstBufferPtr &bufferPtr, size_t alignment) { - if (!IsAllocated ()) + if (!IsAllocated () || ((bufferPtr.Size()) && (DataAlignment != alignment))) { + if (IsAllocated ()) + Free (); + if (bufferPtr.Size()) - Allocate (bufferPtr.Size()); + Allocate (bufferPtr.Size(), alignment); } else if (bufferPtr.Size() > DataSize) throw ParameterTooLarge (SRC_POS); @@ -80,9 +85,13 @@ namespace VeraCrypt if (DataPtr == nullptr) throw NotInitialized (SRC_POS); - Memory::Free (DataPtr); + if (DataAlignment > 0) + Memory::FreeAligned (DataPtr); + else + Memory::Free (DataPtr); DataPtr = nullptr; DataSize = 0; + DataAlignment = 0; } BufferPtr Buffer::GetRange (size_t offset, size_t size) const @@ -99,9 +108,9 @@ namespace VeraCrypt Memory::Zero (DataPtr, DataSize); } - SecureBuffer::SecureBuffer (size_t size) + SecureBuffer::SecureBuffer (size_t size, size_t alignment) { - Allocate (size); + Allocate (size, alignment); } SecureBuffer::~SecureBuffer () @@ -110,9 +119,9 @@ namespace VeraCrypt Free (); } - void SecureBuffer::Allocate (size_t size) + void SecureBuffer::Allocate (size_t size, size_t alignment) { - Buffer::Allocate (size); + Buffer::Allocate (size, alignment); } void SecureBuffer::Free () diff --git a/src/Platform/Buffer.h b/src/Platform/Buffer.h index b47907b3..797cd7ff 100644 --- a/src/Platform/Buffer.h +++ b/src/Platform/Buffer.h @@ -71,17 +71,18 @@ namespace VeraCrypt { public: Buffer (); - Buffer (size_t size); - Buffer (const ConstBufferPtr &bufferPtr) { CopyFrom (bufferPtr); } + Buffer (size_t size, size_t alignment = 0); + Buffer (const ConstBufferPtr &bufferPtr, size_t alignment = 0) { CopyFrom (bufferPtr, alignment); } virtual ~Buffer (); - virtual void Allocate (size_t size); - virtual void CopyFrom (const ConstBufferPtr &bufferPtr); + virtual void Allocate (size_t size, size_t alignment = 0); + virtual void CopyFrom (const ConstBufferPtr &bufferPtr, size_t alignment = 0); virtual byte *Ptr () const { return DataPtr; } virtual void Erase (); virtual void Free (); virtual BufferPtr GetRange (size_t offset, size_t size) const; virtual size_t Size () const { return DataSize; } + virtual size_t Alignment () const { return DataAlignment; } virtual bool IsAllocated () const { return DataSize != 0; } virtual void Zero (); @@ -92,6 +93,7 @@ namespace VeraCrypt protected: byte *DataPtr; size_t DataSize; + size_t DataAlignment; private: Buffer (const Buffer &); @@ -102,11 +104,11 @@ namespace VeraCrypt { public: SecureBuffer () { } - SecureBuffer (size_t size); + SecureBuffer (size_t size, size_t alignment = 0); SecureBuffer (const ConstBufferPtr &bufferPtr) { CopyFrom (bufferPtr); } virtual ~SecureBuffer (); - virtual void Allocate (size_t size); + virtual void Allocate (size_t size, size_t alignment = 0); virtual void Free (); private: diff --git a/src/Platform/Memory.cpp b/src/Platform/Memory.cpp index c8c04766..1184d466 100644 --- a/src/Platform/Memory.cpp +++ b/src/Platform/Memory.cpp @@ -13,6 +13,7 @@ #include "Common/Tcdefs.h" #include "Memory.h" #include "Exception.h" +#include <stdlib.h> namespace VeraCrypt { @@ -27,6 +28,23 @@ namespace VeraCrypt return bufPtr; } + + void *Memory::AllocateAligned (std::size_t size, std::size_t alignment) + { + if (size < 1) + throw ParameterIncorrect (SRC_POS); +#ifdef TC_WINDOWS + void *bufPtr = _aligned_malloc (size, alignment); +#else + void *bufPtr = NULL; + if (0 != posix_memalign (&bufPtr, alignment, size)) + bufPtr = NULL; +#endif + if (!bufPtr) + throw bad_alloc(); + + return bufPtr; + } int Memory::Compare (const void *memory1, size_t size1, const void *memory2, size_t size2) { @@ -59,4 +77,14 @@ namespace VeraCrypt assert (memory != nullptr); free (memory); } + + void Memory::FreeAligned (void *memory) + { + assert (memory != nullptr); +#ifdef TC_WINDOWS + _aligned_free (memory); +#else + free (memory); +#endif + } } diff --git a/src/Platform/Memory.h b/src/Platform/Memory.h index 69ce2211..d303c23e 100644 --- a/src/Platform/Memory.h +++ b/src/Platform/Memory.h @@ -73,10 +73,12 @@ namespace VeraCrypt { public: static void *Allocate (size_t size); + static void *AllocateAligned (size_t size, size_t alignment); static int Compare (const void *memory1, size_t size1, const void *memory2, size_t size2); static void Copy (void *memoryDestination, const void *memorySource, size_t size); static void Erase (void *memory, size_t size); static void Free (void *memory); + static void FreeAligned (void *memory); static void Zero (void *memory, size_t size); }; |