ext-cryptopp/iterhash.cpp

167 lines
4.4 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
#ifndef __GNUC__
#define CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES
#endif
2002-10-04 17:31:41 +00:00
#include "iterhash.h"
#include "misc.h"
NAMESPACE_BEGIN(CryptoPP)
2005-07-12 04:23:32 +00:00
template <class T, class BASE> void IteratedHashBase<T, BASE>::Update(const byte *input, size_t len)
2002-10-04 17:31:41 +00:00
{
2004-07-23 09:57:11 +00:00
HashWordType oldCountLo = m_countLo, oldCountHi = m_countHi;
2005-07-12 04:23:32 +00:00
if ((m_countLo = oldCountLo + HashWordType(len)) < oldCountLo)
2003-03-20 01:24:12 +00:00
m_countHi++; // carry from low to high
2005-07-12 04:23:32 +00:00
m_countHi += (HashWordType)SafeRightShift<8*sizeof(HashWordType)>(len);
if (m_countHi < oldCountHi || SafeRightShift<2*8*sizeof(HashWordType)>(len) != 0)
2004-10-17 22:39:30 +00:00
throw HashInputTooLong(this->AlgorithmName());
2002-10-04 17:31:41 +00:00
2007-04-16 00:33:09 +00:00
unsigned int blockSize = this->BlockSize();
2004-07-23 09:57:11 +00:00
unsigned int num = ModPowerOf2(oldCountLo, blockSize);
2007-04-16 00:33:09 +00:00
T* dataBuf = this->DataBuf();
byte* data = (byte *)dataBuf;
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)
2002-10-04 17:31:41 +00:00
{
2015-07-17 11:13:23 +00:00
if(data && input)
memcpy(data+num, input, blockSize-num);
2007-04-16 00:33:09 +00:00
HashBlock(dataBuf);
2002-10-04 17:31:41 +00:00
input += (blockSize-num);
len -= (blockSize-num);
num = 0;
2002-10-04 17:31:41 +00:00
// drop through and do the rest
}
else
{
2015-07-17 11:13:23 +00:00
if(data && input)
memcpy(data+num, input, len);
2002-10-04 17:31:41 +00:00
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)
{
2007-04-16 00:33:09 +00:00
if (input == data)
2002-10-04 17:31:41 +00:00
{
assert(len == blockSize);
2007-04-16 00:33:09 +00:00
HashBlock(dataBuf);
2002-10-04 17:31:41 +00:00
return;
}
else if (IsAligned<T>(input))
{
2005-07-12 04:23:32 +00:00
size_t leftOver = HashMultipleBlocks((T *)input, len);
2002-10-04 17:31:41 +00:00
input += (len - leftOver);
len = leftOver;
}
else
do
{ // copy input first if it's not aligned correctly
2015-07-17 11:13:23 +00:00
if(data && input)
memcpy(data, input, blockSize);
2007-04-16 00:33:09 +00:00
HashBlock(dataBuf);
2002-10-04 17:31:41 +00:00
input+=blockSize;
len-=blockSize;
} while (len >= blockSize);
}
if (len && data != input)
memcpy(data, input, len);
2002-10-04 17:31:41 +00:00
}
2005-07-12 04:23:32 +00:00
template <class T, class BASE> byte * IteratedHashBase<T, BASE>::CreateUpdateSpace(size_t &size)
2002-10-04 17:31:41 +00:00
{
2007-04-16 00:33:09 +00:00
unsigned int blockSize = this->BlockSize();
2002-10-04 17:31:41 +00:00
unsigned int num = ModPowerOf2(m_countLo, blockSize);
size = blockSize - num;
2007-04-16 00:33:09 +00:00
return (byte *)DataBuf() + num;
2002-10-04 17:31:41 +00:00
}
2005-07-12 04:23:32 +00:00
template <class T, class BASE> size_t IteratedHashBase<T, BASE>::HashMultipleBlocks(const T *input, size_t length)
2002-10-04 17:31:41 +00:00
{
2007-04-16 00:33:09 +00:00
unsigned int blockSize = this->BlockSize();
bool noReverse = NativeByteOrderIs(this->GetByteOrder());
T* dataBuf = this->DataBuf();
2002-10-04 17:31:41 +00:00
do
{
if (noReverse)
2007-04-16 00:33:09 +00:00
this->HashEndianCorrectedBlock(input);
else
{
2007-04-16 00:33:09 +00:00
ByteReverse(dataBuf, input, this->BlockSize());
this->HashEndianCorrectedBlock(dataBuf);
}
2002-10-04 17:31:41 +00:00
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)
{
2007-04-16 00:33:09 +00:00
unsigned int blockSize = this->BlockSize();
2002-10-04 17:31:41 +00:00
unsigned int num = ModPowerOf2(m_countLo, blockSize);
2007-04-16 00:33:09 +00:00
T* dataBuf = this->DataBuf();
byte* data = (byte *)dataBuf;
data[num++] = padFirst;
2002-10-04 17:31:41 +00:00
if (num <= lastBlockSize)
2007-04-16 00:33:09 +00:00
memset(data+num, 0, lastBlockSize-num);
2002-10-04 17:31:41 +00:00
else
{
2007-04-16 00:33:09 +00:00
memset(data+num, 0, blockSize-num);
HashBlock(dataBuf);
memset(data, 0, lastBlockSize);
2002-10-04 17:31:41 +00:00
}
}
template <class T, class BASE> void IteratedHashBase<T, BASE>::Restart()
{
m_countLo = m_countHi = 0;
Init();
}
2005-07-12 04:23:32 +00:00
template <class T, class BASE> void IteratedHashBase<T, BASE>::TruncatedFinal(byte *digest, size_t size)
{
this->ThrowIfInvalidTruncatedSize(size);
2007-04-16 00:33:09 +00:00
T* dataBuf = this->DataBuf();
T* stateBuf = this->StateBuf();
unsigned int blockSize = this->BlockSize();
ByteOrder order = this->GetByteOrder();
2007-04-16 00:33:09 +00:00
PadLastBlock(blockSize - 2*sizeof(HashWordType));
dataBuf[blockSize/sizeof(T)-2+order] = ConditionalByteReverse(order, this->GetBitCountLo());
dataBuf[blockSize/sizeof(T)-1-order] = ConditionalByteReverse(order, this->GetBitCountHi());
HashBlock(dataBuf);
2007-04-16 00:33:09 +00:00
if (IsAligned<HashWordType>(digest) && size%sizeof(HashWordType)==0)
ConditionalByteReverse<HashWordType>(order, (HashWordType *)digest, stateBuf, size);
else
{
ConditionalByteReverse<HashWordType>(order, stateBuf, stateBuf, this->DigestSize());
memcpy(digest, stateBuf, size);
}
this->Restart(); // reinit for next use
}
#ifdef __GNUC__
template class IteratedHashBase<word64, HashTransformation>;
template class IteratedHashBase<word64, MessageAuthenticationCode>;
template class IteratedHashBase<word32, HashTransformation>;
template class IteratedHashBase<word32, MessageAuthenticationCode>;
#endif
2002-10-04 17:31:41 +00:00
NAMESPACE_END