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.
pull/655/head
Jeffrey Walton 2018-05-05 22:56:15 -04:00
parent 3deb24b7de
commit 3159969808
No known key found for this signature in database
GPG Key ID: B36AB348921B1838
6 changed files with 35 additions and 21 deletions

View File

@ -356,6 +356,9 @@ void BLAKE2_Base<W, T_64bit>::Restart(const BLAKE2_ParameterBlock<T_64bit>& bloc
template <class W, bool T_64bit> template <class W, bool T_64bit>
void BLAKE2_Base<W, T_64bit>::Update(const byte *input, size_t length) void BLAKE2_Base<W, T_64bit>::Update(const byte *input, size_t length)
{ {
CRYPTOPP_ASSERT(input != NULLPTR);
if (length == 0) { return; }
State& state = *m_state.data(); State& state = *m_state.data();
if (state.length + length > BLOCKSIZE) if (state.length + length > BLOCKSIZE)
{ {
@ -390,6 +393,7 @@ void BLAKE2_Base<W, T_64bit>::Update(const byte *input, size_t length)
template <class W, bool T_64bit> template <class W, bool T_64bit>
void BLAKE2_Base<W, T_64bit>::TruncatedFinal(byte *hash, size_t size) void BLAKE2_Base<W, T_64bit>::TruncatedFinal(byte *hash, size_t size)
{ {
CRYPTOPP_ASSERT(hash != NULLPTR);
this->ThrowIfInvalidTruncatedSize(size); this->ThrowIfInvalidTruncatedSize(size);
// Set last block unconditionally // Set last block unconditionally

View File

@ -10,14 +10,16 @@
NAMESPACE_BEGIN(CryptoPP) NAMESPACE_BEGIN(CryptoPP)
template <class T, class BASE> void IteratedHashBase<T, BASE>::Update(const byte *input, size_t len) template <class T, class BASE> void IteratedHashBase<T, BASE>::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; 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++; // carry from low to high
m_countHi += (HashWordType)SafeRightShift<8*sizeof(HashWordType)>(len); m_countHi += (HashWordType)SafeRightShift<8*sizeof(HashWordType)>(length);
if (m_countHi < oldCountHi || SafeRightShift<2*8*sizeof(HashWordType)>(len) != 0) if (m_countHi < oldCountHi || SafeRightShift<2*8*sizeof(HashWordType)>(length) != 0)
throw HashInputTooLong(this->AlgorithmName()); throw HashInputTooLong(this->AlgorithmName());
const unsigned int blockSize = this->BlockSize(); const unsigned int blockSize = this->BlockSize();
@ -29,36 +31,36 @@ template <class T, class BASE> void IteratedHashBase<T, BASE>::Update(const byte
if (num != 0) // process left over data if (num != 0) // process left over data
{ {
if (num+len >= blockSize) if (num+length >= blockSize)
{ {
if (data && input) {memcpy(data+num, input, blockSize-num);} if (data && input) {memcpy(data+num, input, blockSize-num);}
HashBlock(dataBuf); HashBlock(dataBuf);
input += (blockSize-num); input += (blockSize-num);
len -= (blockSize-num); length -= (blockSize-num);
num = 0; num = 0;
// drop through and do the rest // drop through and do the rest
} }
else else
{ {
if (data && input && len) {memcpy(data+num, input, len);} if (data && input && length) {memcpy(data+num, input, length);}
return; return;
} }
} }
// now process the input data in blocks of blockSize bytes and save the leftovers to m_data // 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) if (input == data)
{ {
CRYPTOPP_ASSERT(len == blockSize); CRYPTOPP_ASSERT(length == blockSize);
HashBlock(dataBuf); HashBlock(dataBuf);
return; return;
} }
else if (IsAligned<T>(input)) else if (IsAligned<T>(input))
{ {
size_t leftOver = HashMultipleBlocks((T *)(void*)input, len); size_t leftOver = HashMultipleBlocks((T *)(void*)input, length);
input += (len - leftOver); input += (length - leftOver);
len = leftOver; length = leftOver;
} }
else else
do do
@ -66,12 +68,12 @@ template <class T, class BASE> void IteratedHashBase<T, BASE>::Update(const byte
if (data && input) memcpy(data, input, blockSize); if (data && input) memcpy(data, input, blockSize);
HashBlock(dataBuf); HashBlock(dataBuf);
input+=blockSize; input+=blockSize;
len-=blockSize; length-=blockSize;
} while (len >= blockSize); } while (length >= blockSize);
} }
if (data && input && len && data != input) if (data && input && data != input)
memcpy(data, input, len); memcpy(data, input, length);
} }
template <class T, class BASE> byte * IteratedHashBase<T, BASE>::CreateUpdateSpace(size_t &size) template <class T, class BASE> byte * IteratedHashBase<T, BASE>::CreateUpdateSpace(size_t &size)
@ -129,6 +131,7 @@ template <class T, class BASE> void IteratedHashBase<T, BASE>::Restart()
template <class T, class BASE> void IteratedHashBase<T, BASE>::TruncatedFinal(byte *digest, size_t size) template <class T, class BASE> void IteratedHashBase<T, BASE>::TruncatedFinal(byte *digest, size_t size)
{ {
CRYPTOPP_ASSERT(digest != NULLPTR);
this->ThrowIfInvalidTruncatedSize(size); this->ThrowIfInvalidTruncatedSize(size);
T* dataBuf = this->DataBuf(); T* dataBuf = this->DataBuf();

View File

@ -251,8 +251,8 @@ static void KeccakF1600(word64 *state)
void Keccak::Update(const byte *input, size_t length) void Keccak::Update(const byte *input, size_t length)
{ {
CRYPTOPP_ASSERT((input && length) || !(input || length)); CRYPTOPP_ASSERT(!(input == NULLPTR && length != 0));
if (!length) { return; } if (length == 0) { return; }
size_t spaceLeft; size_t spaceLeft;
while (length >= (spaceLeft = r() - m_counter)) while (length >= (spaceLeft = r() - m_counter))
@ -278,6 +278,7 @@ void Keccak::Restart()
void Keccak::TruncatedFinal(byte *hash, size_t size) void Keccak::TruncatedFinal(byte *hash, size_t size)
{ {
CRYPTOPP_ASSERT(hash != NULLPTR);
ThrowIfInvalidTruncatedSize(size); ThrowIfInvalidTruncatedSize(size);
m_state.BytePtr()[m_counter] ^= 1; m_state.BytePtr()[m_counter] ^= 1;

View File

@ -251,8 +251,8 @@ static void KeccakF1600(word64 *state)
void SHA3::Update(const byte *input, size_t length) void SHA3::Update(const byte *input, size_t length)
{ {
CRYPTOPP_ASSERT((input && length) || !(input || length)); CRYPTOPP_ASSERT(!(input == NULLPTR && length != 0));
if (!length) { return; } if (length == 0) { return; }
size_t spaceLeft; size_t spaceLeft;
while (length >= (spaceLeft = r() - m_counter)) while (length >= (spaceLeft = r() - m_counter))
@ -278,6 +278,7 @@ void SHA3::Restart()
void SHA3::TruncatedFinal(byte *hash, size_t size) void SHA3::TruncatedFinal(byte *hash, size_t size)
{ {
CRYPTOPP_ASSERT(hash != NULLPTR);
ThrowIfInvalidTruncatedSize(size); ThrowIfInvalidTruncatedSize(size);
m_state.BytePtr()[m_counter] ^= 0x06; m_state.BytePtr()[m_counter] ^= 0x06;

View File

@ -25,6 +25,7 @@ void Tiger::InitState(HashWordType *state)
void Tiger::TruncatedFinal(byte *hash, size_t size) void Tiger::TruncatedFinal(byte *hash, size_t size)
{ {
CRYPTOPP_ASSERT(hash != NULLPTR);
ThrowIfInvalidTruncatedSize(size); ThrowIfInvalidTruncatedSize(size);
PadLastBlock(56, 0x01); PadLastBlock(56, 0x01);

View File

@ -96,6 +96,7 @@ void Whirlpool::InitState(HashWordType *state)
void Whirlpool::TruncatedFinal(byte *hash, size_t size) void Whirlpool::TruncatedFinal(byte *hash, size_t size)
{ {
CRYPTOPP_ASSERT(hash != NULLPTR);
ThrowIfInvalidTruncatedSize(size); ThrowIfInvalidTruncatedSize(size);
PadLastBlock(32); PadLastBlock(32);
@ -407,6 +408,9 @@ const word64 Whirlpool_C[4*256+R] = {
// Whirlpool basic transformation. Transforms state based on block. // Whirlpool basic transformation. Transforms state based on block.
void Whirlpool::Transform(word64 *digest, const word64 *block) void Whirlpool::Transform(word64 *digest, const word64 *block)
{ {
CRYPTOPP_ASSERT(digest != NULLPTR);
CRYPTOPP_ASSERT(block != NULLPTR);
#if CRYPTOPP_SSE2_ASM_AVAILABLE #if CRYPTOPP_SSE2_ASM_AVAILABLE
if (HasSSE2()) if (HasSSE2())
{ {