From 414c5c543860947bc43dd1cc523ea62704fdcc99 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Fri, 20 Jul 2018 20:12:54 -0400 Subject: [PATCH] Fix Tiger crash on Sparc (GH #690) Man, Sparc does not mess around with unaligned buffers. Without -xmemalign=4i the hardware wants 8-byte aligned word64's so it can use the high performance 64-bit move or add. Since we do not use -xmemalign we get the default behavior of either -xmemalgin=8i or -xmemalgin=8s. It shoul dnot matter to us since we removed unaligned data access at GH #682. --- iterhash.cpp | 26 +++++++++++++++++++------- misc.h | 4 ++++ 2 files changed, 23 insertions(+), 7 deletions(-) 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