ext-cryptopp/iterhash.cpp

115 lines
2.9 KiB
C++
Raw Normal View History

2002-10-04 17:31:41 +00:00
// iterhash.cpp - written and placed in the public domain by Wei Dai
#include "pch.h"
#include "iterhash.h"
#include "misc.h"
NAMESPACE_BEGIN(CryptoPP)
2004-07-23 09:57:11 +00:00
HashInputTooLong::HashInputTooLong(const std::string &alg)
: InvalidDataFormat("IteratedHashBase: input data exceeds maximum allowed by hash function " + alg)
{
}
2002-10-04 17:31:41 +00:00
template <class T, class BASE> void IteratedHashBase<T, BASE>::Update(const byte *input, unsigned int len)
{
2004-07-23 09:57:11 +00:00
HashWordType oldCountLo = m_countLo, oldCountHi = m_countHi;
if ((m_countLo = oldCountLo + len) < oldCountLo)
2003-03-20 01:24:12 +00:00
m_countHi++; // carry from low to high
2002-10-04 17:31:41 +00:00
m_countHi += SafeRightShift<8*sizeof(HashWordType)>(len);
2004-07-23 09:57:11 +00:00
if (m_countHi < oldCountHi)
throw HashInputTooLong(AlgorithmName());
2002-10-04 17:31:41 +00:00
unsigned int blockSize = BlockSize();
2004-07-23 09:57:11 +00:00
unsigned int num = ModPowerOf2(oldCountLo, blockSize);
2002-10-04 17:31:41 +00:00
2003-03-20 01:24:12 +00:00
if (num != 0) // process left over data
2002-10-04 17:31:41 +00:00
{
if ((num+len) >= blockSize)
{
memcpy((byte *)m_data.begin()+num, input, blockSize-num);
HashBlock(m_data);
input += (blockSize-num);
len-=(blockSize - num);
num=0;
// drop through and do the rest
}
else
{
memcpy((byte *)m_data.begin()+num, input, len);
return;
}
}
2003-03-20 01:24:12 +00:00
// now process the input data in blocks of blockSize bytes and save the leftovers to m_data
2002-10-04 17:31:41 +00:00
if (len >= blockSize)
{
if (input == (byte *)m_data.begin())
{
assert(len == blockSize);
HashBlock(m_data);
return;
}
else if (IsAligned<T>(input))
{
unsigned int leftOver = HashMultipleBlocks((T *)input, len);
input += (len - leftOver);
len = leftOver;
}
else
do
{ // copy input first if it's not aligned correctly
memcpy(m_data, input, blockSize);
HashBlock(m_data);
input+=blockSize;
len-=blockSize;
} while (len >= blockSize);
}
memcpy(m_data, input, len);
}
template <class T, class BASE> byte * IteratedHashBase<T, BASE>::CreateUpdateSpace(unsigned int &size)
{
unsigned int blockSize = BlockSize();
unsigned int num = ModPowerOf2(m_countLo, blockSize);
size = blockSize - num;
return (byte *)m_data.begin() + num;
}
template <class T, class BASE> unsigned int IteratedHashBase<T, BASE>::HashMultipleBlocks(const T *input, unsigned int length)
{
unsigned int blockSize = BlockSize();
do
{
HashBlock(input);
input += blockSize/sizeof(T);
length -= blockSize;
}
while (length >= blockSize);
return length;
}
template <class T, class BASE> void IteratedHashBase<T, BASE>::PadLastBlock(unsigned int lastBlockSize, byte padFirst)
{
unsigned int blockSize = BlockSize();
unsigned int num = ModPowerOf2(m_countLo, blockSize);
((byte *)m_data.begin())[num++]=padFirst;
if (num <= lastBlockSize)
memset((byte *)m_data.begin()+num, 0, lastBlockSize-num);
else
{
memset((byte *)m_data.begin()+num, 0, blockSize-num);
HashBlock(m_data);
memset(m_data, 0, lastBlockSize);
}
}
template <class T, class BASE> void IteratedHashBase<T, BASE>::Restart()
{
m_countLo = m_countHi = 0;
Init();
}
NAMESPACE_END