diff --git a/iterhash.cpp b/iterhash.cpp index e9974ced..da34dcb2 100644 --- a/iterhash.cpp +++ b/iterhash.cpp @@ -33,7 +33,7 @@ template void IteratedHashBase::Update(const byte if (num+length >= blockSize) { if (input) - {memcpy(data+num, input, blockSize-num);} + {std::memcpy(data+num, input, blockSize-num);} HashBlock(dataBuf); input += (blockSize-num); @@ -44,7 +44,7 @@ template void IteratedHashBase::Update(const byte else { if (input && length) - {memcpy(data+num, input, length);} + {std::memcpy(data+num, input, length);} return; } } @@ -69,7 +69,7 @@ template void IteratedHashBase::Update(const byte do { // copy input first if it's not aligned correctly if (input) - { memcpy(data, input, blockSize); } + { std::memcpy(data, input, blockSize); } HashBlock(dataBuf); input+=blockSize; @@ -79,7 +79,7 @@ template void IteratedHashBase::Update(const byte } if (input && data != input) - memcpy(data, input, length); + std::memcpy(data, input, length); } template byte * IteratedHashBase::CreateUpdateSpace(size_t &size) @@ -92,19 +92,22 @@ template byte * IteratedHashBase::CreateUpdateSpa template size_t IteratedHashBase::HashMultipleBlocks(const T *input, size_t length) { - unsigned int blockSize = this->BlockSize(); + const unsigned int blockSize = this->BlockSize(); bool noReverse = NativeByteOrderIs(this->GetByteOrder()); T* dataBuf = this->DataBuf(); // IteratedHashBase Update calls this with an aligned input, // but HashBlock may call it with an unaligned buffer. + // Alignment checks due to Issues 690/ do { if (noReverse) { if (IsAligned(input)) + { this->HashEndianCorrectedBlock(input); + } else { std::memcpy(dataBuf, input, this->BlockSize()); @@ -113,8 +116,17 @@ template size_t IteratedHashBase::HashMultipleBlo } else { - ByteReverse(dataBuf, input, this->BlockSize()); - this->HashEndianCorrectedBlock(dataBuf); + if (IsAligned(input)) + { + ByteReverse(dataBuf, input, this->BlockSize()); + this->HashEndianCorrectedBlock(dataBuf); + } + else + { + std::memcpy(dataBuf, input, this->BlockSize()); + ByteReverse(dataBuf, dataBuf, this->BlockSize()); + this->HashEndianCorrectedBlock(dataBuf); + } } input += blockSize/sizeof(T); diff --git a/misc.h b/misc.h index 2d92a2fb..038738ee 100644 --- a/misc.h +++ b/misc.h @@ -2068,7 +2068,11 @@ inline T ConditionalByteReverse(ByteOrder order, T value) template void ByteReverse(T *out, const T *in, size_t byteCount) { + // Alignment check due to Issues 690 CRYPTOPP_ASSERT(byteCount % sizeof(T) == 0); + CRYPTOPP_ASSERT(IsAligned(in)); + CRYPTOPP_ASSERT(IsAligned(out)); + size_t count = byteCount/sizeof(T); for (size_t i=0; i