From 3159969808669a35a3a185bd6bef8955f0cf1822 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Sat, 5 May 2018 22:56:15 -0400 Subject: [PATCH] Back-off on Hash asserts (GH #652) The asserts were a little aggressive and caused very noisy Debug runs. The library itself was one of the biggest offenders. --- blake2.cpp | 4 ++++ iterhash.cpp | 37 ++++++++++++++++++++----------------- keccak.cpp | 5 +++-- sha3.cpp | 5 +++-- tiger.cpp | 1 + whrlpool.cpp | 4 ++++ 6 files changed, 35 insertions(+), 21 deletions(-) diff --git a/blake2.cpp b/blake2.cpp index 0500e7a0..e858f8c7 100644 --- a/blake2.cpp +++ b/blake2.cpp @@ -356,6 +356,9 @@ void BLAKE2_Base::Restart(const BLAKE2_ParameterBlock& bloc template void BLAKE2_Base::Update(const byte *input, size_t length) { + CRYPTOPP_ASSERT(input != NULLPTR); + if (length == 0) { return; } + State& state = *m_state.data(); if (state.length + length > BLOCKSIZE) { @@ -390,6 +393,7 @@ void BLAKE2_Base::Update(const byte *input, size_t length) template void BLAKE2_Base::TruncatedFinal(byte *hash, size_t size) { + CRYPTOPP_ASSERT(hash != NULLPTR); this->ThrowIfInvalidTruncatedSize(size); // Set last block unconditionally diff --git a/iterhash.cpp b/iterhash.cpp index 33a697a5..91e00af8 100644 --- a/iterhash.cpp +++ b/iterhash.cpp @@ -10,14 +10,16 @@ NAMESPACE_BEGIN(CryptoPP) -template void IteratedHashBase::Update(const byte *input, size_t len) +template void IteratedHashBase::Update(const byte *input, size_t length) { - CRYPTOPP_ASSERT((input && len) || !(input || len)); + CRYPTOPP_ASSERT(!(input == NULLPTR && length != 0)); + if (length == 0) { return; } + HashWordType oldCountLo = m_countLo, oldCountHi = m_countHi; - if ((m_countLo = oldCountLo + HashWordType(len)) < oldCountLo) + if ((m_countLo = oldCountLo + HashWordType(length)) < oldCountLo) m_countHi++; // carry from low to high - m_countHi += (HashWordType)SafeRightShift<8*sizeof(HashWordType)>(len); - if (m_countHi < oldCountHi || SafeRightShift<2*8*sizeof(HashWordType)>(len) != 0) + m_countHi += (HashWordType)SafeRightShift<8*sizeof(HashWordType)>(length); + if (m_countHi < oldCountHi || SafeRightShift<2*8*sizeof(HashWordType)>(length) != 0) throw HashInputTooLong(this->AlgorithmName()); const unsigned int blockSize = this->BlockSize(); @@ -29,36 +31,36 @@ template void IteratedHashBase::Update(const byte if (num != 0) // process left over data { - if (num+len >= blockSize) + if (num+length >= blockSize) { if (data && input) {memcpy(data+num, input, blockSize-num);} HashBlock(dataBuf); input += (blockSize-num); - len -= (blockSize-num); + length -= (blockSize-num); num = 0; // drop through and do the rest } else { - if (data && input && len) {memcpy(data+num, input, len);} + if (data && input && length) {memcpy(data+num, input, length);} return; } } // now process the input data in blocks of blockSize bytes and save the leftovers to m_data - if (len >= blockSize) + if (length >= blockSize) { if (input == data) { - CRYPTOPP_ASSERT(len == blockSize); + CRYPTOPP_ASSERT(length == blockSize); HashBlock(dataBuf); return; } else if (IsAligned(input)) { - size_t leftOver = HashMultipleBlocks((T *)(void*)input, len); - input += (len - leftOver); - len = leftOver; + size_t leftOver = HashMultipleBlocks((T *)(void*)input, length); + input += (length - leftOver); + length = leftOver; } else do @@ -66,12 +68,12 @@ template void IteratedHashBase::Update(const byte if (data && input) memcpy(data, input, blockSize); HashBlock(dataBuf); input+=blockSize; - len-=blockSize; - } while (len >= blockSize); + length-=blockSize; + } while (length >= blockSize); } - if (data && input && len && data != input) - memcpy(data, input, len); + if (data && input && data != input) + memcpy(data, input, length); } template byte * IteratedHashBase::CreateUpdateSpace(size_t &size) @@ -129,6 +131,7 @@ template void IteratedHashBase::Restart() template void IteratedHashBase::TruncatedFinal(byte *digest, size_t size) { + CRYPTOPP_ASSERT(digest != NULLPTR); this->ThrowIfInvalidTruncatedSize(size); T* dataBuf = this->DataBuf(); diff --git a/keccak.cpp b/keccak.cpp index cc1771cf..3450dd6a 100644 --- a/keccak.cpp +++ b/keccak.cpp @@ -251,8 +251,8 @@ static void KeccakF1600(word64 *state) void Keccak::Update(const byte *input, size_t length) { - CRYPTOPP_ASSERT((input && length) || !(input || length)); - if (!length) { return; } + CRYPTOPP_ASSERT(!(input == NULLPTR && length != 0)); + if (length == 0) { return; } size_t spaceLeft; while (length >= (spaceLeft = r() - m_counter)) @@ -278,6 +278,7 @@ void Keccak::Restart() void Keccak::TruncatedFinal(byte *hash, size_t size) { + CRYPTOPP_ASSERT(hash != NULLPTR); ThrowIfInvalidTruncatedSize(size); m_state.BytePtr()[m_counter] ^= 1; diff --git a/sha3.cpp b/sha3.cpp index ec3b04e3..51e01117 100644 --- a/sha3.cpp +++ b/sha3.cpp @@ -251,8 +251,8 @@ static void KeccakF1600(word64 *state) void SHA3::Update(const byte *input, size_t length) { - CRYPTOPP_ASSERT((input && length) || !(input || length)); - if (!length) { return; } + CRYPTOPP_ASSERT(!(input == NULLPTR && length != 0)); + if (length == 0) { return; } size_t spaceLeft; while (length >= (spaceLeft = r() - m_counter)) @@ -278,6 +278,7 @@ void SHA3::Restart() void SHA3::TruncatedFinal(byte *hash, size_t size) { + CRYPTOPP_ASSERT(hash != NULLPTR); ThrowIfInvalidTruncatedSize(size); m_state.BytePtr()[m_counter] ^= 0x06; diff --git a/tiger.cpp b/tiger.cpp index 3365b981..a4a1f291 100644 --- a/tiger.cpp +++ b/tiger.cpp @@ -25,6 +25,7 @@ void Tiger::InitState(HashWordType *state) void Tiger::TruncatedFinal(byte *hash, size_t size) { + CRYPTOPP_ASSERT(hash != NULLPTR); ThrowIfInvalidTruncatedSize(size); PadLastBlock(56, 0x01); diff --git a/whrlpool.cpp b/whrlpool.cpp index e0e401cc..3986e225 100644 --- a/whrlpool.cpp +++ b/whrlpool.cpp @@ -96,6 +96,7 @@ void Whirlpool::InitState(HashWordType *state) void Whirlpool::TruncatedFinal(byte *hash, size_t size) { + CRYPTOPP_ASSERT(hash != NULLPTR); ThrowIfInvalidTruncatedSize(size); PadLastBlock(32); @@ -407,6 +408,9 @@ const word64 Whirlpool_C[4*256+R] = { // Whirlpool basic transformation. Transforms state based on block. void Whirlpool::Transform(word64 *digest, const word64 *block) { + CRYPTOPP_ASSERT(digest != NULLPTR); + CRYPTOPP_ASSERT(block != NULLPTR); + #if CRYPTOPP_SSE2_ASM_AVAILABLE if (HasSSE2()) {