VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/src/Platform/Buffer.cpp
diff options
context:
space:
mode:
authorMounir IDRASSI <mounir.idrassi@idrix.fr>2016-09-28 00:14:05 +0200
committerMounir IDRASSI <mounir.idrassi@idrix.fr>2016-10-17 18:40:19 +0200
commit57ce7aab7be0ca2c3e990eaf7d9cf3691efd1ea0 (patch)
tree3cfd2715c14b191f2817f1bac60f8236a12cb870 /src/Platform/Buffer.cpp
parent3e029b0dde5326a4f1221c86a2050aafceaafa3e (diff)
downloadVeraCrypt-57ce7aab7be0ca2c3e990eaf7d9cf3691efd1ea0.tar.gz
VeraCrypt-57ce7aab7be0ca2c3e990eaf7d9cf3691efd1ea0.zip
Use properly aligned memory in code using Streebog hash implementation that uses SSE.
Diffstat (limited to 'src/Platform/Buffer.cpp')
-rw-r--r--src/Platform/Buffer.cpp37
1 files changed, 23 insertions, 14 deletions
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 ()