mirror of
https://github.com/veracrypt/VeraCrypt
synced 2024-11-10 13:13:34 +01:00
Use properly aligned memory in code using Streebog hash implementation that uses SSE.
This commit is contained in:
parent
3e029b0dde
commit
57ce7aab7b
@ -173,7 +173,7 @@ namespace VeraCrypt
|
|||||||
Running = true;
|
Running = true;
|
||||||
EnrichedByUser = false;
|
EnrichedByUser = false;
|
||||||
|
|
||||||
Pool.Allocate (PoolSize);
|
Pool.Allocate (PoolSize, 16);
|
||||||
Test();
|
Test();
|
||||||
|
|
||||||
if (!PoolHash)
|
if (!PoolHash)
|
||||||
|
@ -102,7 +102,7 @@ void gost_set_key(const byte *key, gost_kds *ks, int useDynamicSbox)
|
|||||||
#endif
|
#endif
|
||||||
//Generate pseudorandom data based on the key
|
//Generate pseudorandom data based on the key
|
||||||
STREEBOG_init(&sctx);
|
STREEBOG_init(&sctx);
|
||||||
STREEBOG_add(&sctx, key, 32);
|
STREEBOG_add(&sctx, ks->key, 32);
|
||||||
STREEBOG_finalize(&sctx, sbox_seed);
|
STREEBOG_finalize(&sctx, sbox_seed);
|
||||||
|
|
||||||
#if defined (DEVICE_DRIVER) && !defined (_WIN64)
|
#if defined (DEVICE_DRIVER) && !defined (_WIN64)
|
||||||
|
@ -13,6 +13,7 @@
|
|||||||
#define GOST_CIPHER_H
|
#define GOST_CIPHER_H
|
||||||
|
|
||||||
#include "Common/Tcdefs.h"
|
#include "Common/Tcdefs.h"
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
@ -47,7 +48,7 @@ typedef unsigned int gst_udword;
|
|||||||
|
|
||||||
typedef struct gost_kds
|
typedef struct gost_kds
|
||||||
{
|
{
|
||||||
byte key[32];
|
CRYPTOPP_ALIGN_DATA(16) byte key[32];
|
||||||
gst_udword sbox_cvt[256 * 4];
|
gst_udword sbox_cvt[256 * 4];
|
||||||
byte sbox[8][16];
|
byte sbox[8][16];
|
||||||
} gost_kds;
|
} gost_kds;
|
||||||
|
@ -15,13 +15,13 @@
|
|||||||
|
|
||||||
namespace VeraCrypt
|
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 ()
|
Buffer::~Buffer ()
|
||||||
@ -30,37 +30,42 @@ namespace VeraCrypt
|
|||||||
Free ();
|
Free ();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Buffer::Allocate (size_t size)
|
void Buffer::Allocate (size_t size, size_t alignment)
|
||||||
{
|
{
|
||||||
if (size < 1)
|
if (size < 1)
|
||||||
throw ParameterIncorrect (SRC_POS);
|
throw ParameterIncorrect (SRC_POS);
|
||||||
|
|
||||||
if (DataPtr != nullptr)
|
if (DataPtr != nullptr)
|
||||||
{
|
{
|
||||||
if (DataSize == size)
|
if ((DataSize == size) && (DataAlignment == alignment))
|
||||||
return;
|
return;
|
||||||
Free();
|
Free();
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
DataPtr = static_cast<byte *> (Memory::Allocate (size));
|
DataPtr = static_cast<byte *> ((alignment > 0)? Memory::AllocateAligned (size, alignment) : Memory::Allocate (size));
|
||||||
DataSize = size;
|
DataSize = size;
|
||||||
|
DataAlignment = alignment;
|
||||||
}
|
}
|
||||||
catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
DataPtr = nullptr;
|
DataPtr = nullptr;
|
||||||
DataSize = 0;
|
DataSize = 0;
|
||||||
|
DataAlignment = 0;
|
||||||
throw;
|
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())
|
if (bufferPtr.Size())
|
||||||
Allocate (bufferPtr.Size());
|
Allocate (bufferPtr.Size(), alignment);
|
||||||
}
|
}
|
||||||
else if (bufferPtr.Size() > DataSize)
|
else if (bufferPtr.Size() > DataSize)
|
||||||
throw ParameterTooLarge (SRC_POS);
|
throw ParameterTooLarge (SRC_POS);
|
||||||
@ -80,9 +85,13 @@ namespace VeraCrypt
|
|||||||
if (DataPtr == nullptr)
|
if (DataPtr == nullptr)
|
||||||
throw NotInitialized (SRC_POS);
|
throw NotInitialized (SRC_POS);
|
||||||
|
|
||||||
Memory::Free (DataPtr);
|
if (DataAlignment > 0)
|
||||||
|
Memory::FreeAligned (DataPtr);
|
||||||
|
else
|
||||||
|
Memory::Free (DataPtr);
|
||||||
DataPtr = nullptr;
|
DataPtr = nullptr;
|
||||||
DataSize = 0;
|
DataSize = 0;
|
||||||
|
DataAlignment = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
BufferPtr Buffer::GetRange (size_t offset, size_t size) const
|
BufferPtr Buffer::GetRange (size_t offset, size_t size) const
|
||||||
@ -99,9 +108,9 @@ namespace VeraCrypt
|
|||||||
Memory::Zero (DataPtr, DataSize);
|
Memory::Zero (DataPtr, DataSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
SecureBuffer::SecureBuffer (size_t size)
|
SecureBuffer::SecureBuffer (size_t size, size_t alignment)
|
||||||
{
|
{
|
||||||
Allocate (size);
|
Allocate (size, alignment);
|
||||||
}
|
}
|
||||||
|
|
||||||
SecureBuffer::~SecureBuffer ()
|
SecureBuffer::~SecureBuffer ()
|
||||||
@ -110,9 +119,9 @@ namespace VeraCrypt
|
|||||||
Free ();
|
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 ()
|
void SecureBuffer::Free ()
|
||||||
|
@ -71,17 +71,18 @@ namespace VeraCrypt
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Buffer ();
|
Buffer ();
|
||||||
Buffer (size_t size);
|
Buffer (size_t size, size_t alignment = 0);
|
||||||
Buffer (const ConstBufferPtr &bufferPtr) { CopyFrom (bufferPtr); }
|
Buffer (const ConstBufferPtr &bufferPtr, size_t alignment = 0) { CopyFrom (bufferPtr, alignment); }
|
||||||
virtual ~Buffer ();
|
virtual ~Buffer ();
|
||||||
|
|
||||||
virtual void Allocate (size_t size);
|
virtual void Allocate (size_t size, size_t alignment = 0);
|
||||||
virtual void CopyFrom (const ConstBufferPtr &bufferPtr);
|
virtual void CopyFrom (const ConstBufferPtr &bufferPtr, size_t alignment = 0);
|
||||||
virtual byte *Ptr () const { return DataPtr; }
|
virtual byte *Ptr () const { return DataPtr; }
|
||||||
virtual void Erase ();
|
virtual void Erase ();
|
||||||
virtual void Free ();
|
virtual void Free ();
|
||||||
virtual BufferPtr GetRange (size_t offset, size_t size) const;
|
virtual BufferPtr GetRange (size_t offset, size_t size) const;
|
||||||
virtual size_t Size () const { return DataSize; }
|
virtual size_t Size () const { return DataSize; }
|
||||||
|
virtual size_t Alignment () const { return DataAlignment; }
|
||||||
virtual bool IsAllocated () const { return DataSize != 0; }
|
virtual bool IsAllocated () const { return DataSize != 0; }
|
||||||
virtual void Zero ();
|
virtual void Zero ();
|
||||||
|
|
||||||
@ -92,6 +93,7 @@ namespace VeraCrypt
|
|||||||
protected:
|
protected:
|
||||||
byte *DataPtr;
|
byte *DataPtr;
|
||||||
size_t DataSize;
|
size_t DataSize;
|
||||||
|
size_t DataAlignment;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Buffer (const Buffer &);
|
Buffer (const Buffer &);
|
||||||
@ -102,11 +104,11 @@ namespace VeraCrypt
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
SecureBuffer () { }
|
SecureBuffer () { }
|
||||||
SecureBuffer (size_t size);
|
SecureBuffer (size_t size, size_t alignment = 0);
|
||||||
SecureBuffer (const ConstBufferPtr &bufferPtr) { CopyFrom (bufferPtr); }
|
SecureBuffer (const ConstBufferPtr &bufferPtr) { CopyFrom (bufferPtr); }
|
||||||
virtual ~SecureBuffer ();
|
virtual ~SecureBuffer ();
|
||||||
|
|
||||||
virtual void Allocate (size_t size);
|
virtual void Allocate (size_t size, size_t alignment = 0);
|
||||||
virtual void Free ();
|
virtual void Free ();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -13,6 +13,7 @@
|
|||||||
#include "Common/Tcdefs.h"
|
#include "Common/Tcdefs.h"
|
||||||
#include "Memory.h"
|
#include "Memory.h"
|
||||||
#include "Exception.h"
|
#include "Exception.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
namespace VeraCrypt
|
namespace VeraCrypt
|
||||||
{
|
{
|
||||||
@ -27,6 +28,23 @@ namespace VeraCrypt
|
|||||||
|
|
||||||
return bufPtr;
|
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)
|
int Memory::Compare (const void *memory1, size_t size1, const void *memory2, size_t size2)
|
||||||
{
|
{
|
||||||
@ -59,4 +77,14 @@ namespace VeraCrypt
|
|||||||
assert (memory != nullptr);
|
assert (memory != nullptr);
|
||||||
free (memory);
|
free (memory);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Memory::FreeAligned (void *memory)
|
||||||
|
{
|
||||||
|
assert (memory != nullptr);
|
||||||
|
#ifdef TC_WINDOWS
|
||||||
|
_aligned_free (memory);
|
||||||
|
#else
|
||||||
|
free (memory);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -73,10 +73,12 @@ namespace VeraCrypt
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static void *Allocate (size_t size);
|
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 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 Copy (void *memoryDestination, const void *memorySource, size_t size);
|
||||||
static void Erase (void *memory, size_t size);
|
static void Erase (void *memory, size_t size);
|
||||||
static void Free (void *memory);
|
static void Free (void *memory);
|
||||||
|
static void FreeAligned (void *memory);
|
||||||
static void Zero (void *memory, size_t size);
|
static void Zero (void *memory, size_t size);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user