optimizations
parent
ce5e051e42
commit
de8b060ea1
67
iterhash.cpp
67
iterhash.cpp
|
|
@ -19,15 +19,17 @@ template <class T, class BASE> void IteratedHashBase<T, BASE>::Update(const byte
|
||||||
if (m_countHi < oldCountHi || SafeRightShift<2*8*sizeof(HashWordType)>(len) != 0)
|
if (m_countHi < oldCountHi || SafeRightShift<2*8*sizeof(HashWordType)>(len) != 0)
|
||||||
throw HashInputTooLong(this->AlgorithmName());
|
throw HashInputTooLong(this->AlgorithmName());
|
||||||
|
|
||||||
unsigned int blockSize = BlockSize();
|
unsigned int blockSize = this->BlockSize();
|
||||||
unsigned int num = ModPowerOf2(oldCountLo, blockSize);
|
unsigned int num = ModPowerOf2(oldCountLo, blockSize);
|
||||||
|
T* dataBuf = this->DataBuf();
|
||||||
|
byte* data = (byte *)dataBuf;
|
||||||
|
|
||||||
if (num != 0) // process left over data
|
if (num != 0) // process left over data
|
||||||
{
|
{
|
||||||
if ((num+len) >= blockSize)
|
if ((num+len) >= blockSize)
|
||||||
{
|
{
|
||||||
memcpy((byte *)m_data.begin()+num, input, blockSize-num);
|
memcpy(data+num, input, blockSize-num);
|
||||||
HashBlock(m_data);
|
HashBlock(dataBuf);
|
||||||
input += (blockSize-num);
|
input += (blockSize-num);
|
||||||
len-=(blockSize - num);
|
len-=(blockSize - num);
|
||||||
num=0;
|
num=0;
|
||||||
|
|
@ -35,7 +37,7 @@ template <class T, class BASE> void IteratedHashBase<T, BASE>::Update(const byte
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
memcpy((byte *)m_data.begin()+num, input, len);
|
memcpy(data+num, input, len);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -43,10 +45,10 @@ template <class T, class BASE> void IteratedHashBase<T, BASE>::Update(const byte
|
||||||
// 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 (len >= blockSize)
|
||||||
{
|
{
|
||||||
if (input == (byte *)m_data.begin())
|
if (input == data)
|
||||||
{
|
{
|
||||||
assert(len == blockSize);
|
assert(len == blockSize);
|
||||||
HashBlock(m_data);
|
HashBlock(dataBuf);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else if (IsAligned<T>(input))
|
else if (IsAligned<T>(input))
|
||||||
|
|
@ -58,36 +60,37 @@ template <class T, class BASE> void IteratedHashBase<T, BASE>::Update(const byte
|
||||||
else
|
else
|
||||||
do
|
do
|
||||||
{ // copy input first if it's not aligned correctly
|
{ // copy input first if it's not aligned correctly
|
||||||
memcpy(m_data, input, blockSize);
|
memcpy(data, input, blockSize);
|
||||||
HashBlock(m_data);
|
HashBlock(dataBuf);
|
||||||
input+=blockSize;
|
input+=blockSize;
|
||||||
len-=blockSize;
|
len-=blockSize;
|
||||||
} while (len >= blockSize);
|
} while (len >= blockSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(m_data, input, len);
|
memcpy(data, input, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
unsigned int blockSize = BlockSize();
|
unsigned int blockSize = this->BlockSize();
|
||||||
unsigned int num = ModPowerOf2(m_countLo, blockSize);
|
unsigned int num = ModPowerOf2(m_countLo, blockSize);
|
||||||
size = blockSize - num;
|
size = blockSize - num;
|
||||||
return (byte *)m_data.begin() + num;
|
return (byte *)DataBuf() + num;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T, class BASE> size_t IteratedHashBase<T, BASE>::HashMultipleBlocks(const T *input, size_t length)
|
template <class T, class BASE> size_t IteratedHashBase<T, BASE>::HashMultipleBlocks(const T *input, size_t length)
|
||||||
{
|
{
|
||||||
unsigned int blockSize = BlockSize();
|
unsigned int blockSize = this->BlockSize();
|
||||||
bool noReverse = NativeByteOrderIs(GetByteOrder());
|
bool noReverse = NativeByteOrderIs(this->GetByteOrder());
|
||||||
|
T* dataBuf = this->DataBuf();
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
if (noReverse)
|
if (noReverse)
|
||||||
HashEndianCorrectedBlock(input);
|
this->HashEndianCorrectedBlock(input);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ByteReverse(this->m_data.begin(), input, this->BlockSize());
|
ByteReverse(dataBuf, input, this->BlockSize());
|
||||||
HashEndianCorrectedBlock(this->m_data);
|
this->HashEndianCorrectedBlock(dataBuf);
|
||||||
}
|
}
|
||||||
|
|
||||||
input += blockSize/sizeof(T);
|
input += blockSize/sizeof(T);
|
||||||
|
|
@ -99,16 +102,18 @@ template <class T, class BASE> size_t IteratedHashBase<T, BASE>::HashMultipleBlo
|
||||||
|
|
||||||
template <class T, class BASE> void IteratedHashBase<T, BASE>::PadLastBlock(unsigned int lastBlockSize, byte padFirst)
|
template <class T, class BASE> void IteratedHashBase<T, BASE>::PadLastBlock(unsigned int lastBlockSize, byte padFirst)
|
||||||
{
|
{
|
||||||
unsigned int blockSize = BlockSize();
|
unsigned int blockSize = this->BlockSize();
|
||||||
unsigned int num = ModPowerOf2(m_countLo, blockSize);
|
unsigned int num = ModPowerOf2(m_countLo, blockSize);
|
||||||
((byte *)m_data.begin())[num++]=padFirst;
|
T* dataBuf = this->DataBuf();
|
||||||
|
byte* data = (byte *)dataBuf;
|
||||||
|
data[num++] = padFirst;
|
||||||
if (num <= lastBlockSize)
|
if (num <= lastBlockSize)
|
||||||
memset((byte *)m_data.begin()+num, 0, lastBlockSize-num);
|
memset(data+num, 0, lastBlockSize-num);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
memset((byte *)m_data.begin()+num, 0, blockSize-num);
|
memset(data+num, 0, blockSize-num);
|
||||||
HashBlock(m_data);
|
HashBlock(dataBuf);
|
||||||
memset(m_data, 0, lastBlockSize);
|
memset(data, 0, lastBlockSize);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -122,16 +127,20 @@ template <class T, class BASE> void IteratedHashBase<T, BASE>::TruncatedFinal(by
|
||||||
{
|
{
|
||||||
this->ThrowIfInvalidTruncatedSize(size);
|
this->ThrowIfInvalidTruncatedSize(size);
|
||||||
|
|
||||||
PadLastBlock(this->BlockSize() - 2*sizeof(HashWordType));
|
T* dataBuf = this->DataBuf();
|
||||||
|
T* stateBuf = this->StateBuf();
|
||||||
|
unsigned int blockSize = this->BlockSize();
|
||||||
ByteOrder order = this->GetByteOrder();
|
ByteOrder order = this->GetByteOrder();
|
||||||
ConditionalByteReverse<HashWordType>(order, this->m_data, this->m_data, this->BlockSize() - 2*sizeof(HashWordType));
|
|
||||||
|
|
||||||
this->m_data[this->m_data.size()-2] = order ? this->GetBitCountHi() : this->GetBitCountLo();
|
PadLastBlock(blockSize - 2*sizeof(HashWordType));
|
||||||
this->m_data[this->m_data.size()-1] = order ? this->GetBitCountLo() : this->GetBitCountHi();
|
ConditionalByteReverse<HashWordType>(order, dataBuf, dataBuf, blockSize - 2*sizeof(HashWordType));
|
||||||
|
|
||||||
HashEndianCorrectedBlock(this->m_data);
|
dataBuf[blockSize/sizeof(T)-2] = order ? this->GetBitCountHi() : this->GetBitCountLo();
|
||||||
ConditionalByteReverse<HashWordType>(order, this->m_digest, this->m_digest, this->DigestSize());
|
dataBuf[blockSize/sizeof(T)-1] = order ? this->GetBitCountLo() : this->GetBitCountHi();
|
||||||
memcpy(digest, this->m_digest, size);
|
|
||||||
|
HashEndianCorrectedBlock(dataBuf);
|
||||||
|
ConditionalByteReverse<HashWordType>(order, stateBuf, stateBuf, this->DigestSize());
|
||||||
|
memcpy(digest, stateBuf, size);
|
||||||
|
|
||||||
this->Restart(); // reinit for next use
|
this->Restart(); // reinit for next use
|
||||||
}
|
}
|
||||||
|
|
|
||||||
35
iterhash.h
35
iterhash.h
|
|
@ -24,20 +24,16 @@ public:
|
||||||
typedef T HashWordType;
|
typedef T HashWordType;
|
||||||
|
|
||||||
IteratedHashBase() : m_countLo(0), m_countHi(0) {}
|
IteratedHashBase() : m_countLo(0), m_countHi(0) {}
|
||||||
unsigned int BlockSize() const {return (unsigned int)m_data.size() * sizeof(T);}
|
unsigned int OptimalBlockSize() const {return this->BlockSize();}
|
||||||
unsigned int OptimalBlockSize() const {return BlockSize();}
|
unsigned int OptimalDataAlignment() const {return GetAlignmentOf<T>();}
|
||||||
unsigned int OptimalDataAlignment() const {return sizeof(T);}
|
|
||||||
void Update(const byte *input, size_t length);
|
void Update(const byte *input, size_t length);
|
||||||
byte * CreateUpdateSpace(size_t &size);
|
byte * CreateUpdateSpace(size_t &size);
|
||||||
void Restart();
|
void Restart();
|
||||||
void TruncatedFinal(byte *digest, size_t size);
|
void TruncatedFinal(byte *digest, size_t size);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void SetBlockSize(unsigned int blockSize) {m_data.resize(blockSize / sizeof(HashWordType));}
|
inline T GetBitCountHi() const {return (m_countLo >> (8*sizeof(T)-3)) + (m_countHi << 3);}
|
||||||
void SetStateSize(unsigned int stateSize) {m_digest.resize(stateSize / sizeof(HashWordType));}
|
inline T GetBitCountLo() const {return m_countLo << 3;}
|
||||||
|
|
||||||
T GetBitCountHi() const {return (m_countLo >> (8*sizeof(T)-3)) + (m_countHi << 3);}
|
|
||||||
T GetBitCountLo() const {return m_countLo << 3;}
|
|
||||||
|
|
||||||
void PadLastBlock(unsigned int lastBlockSize, byte padFirst=0x80);
|
void PadLastBlock(unsigned int lastBlockSize, byte padFirst=0x80);
|
||||||
virtual void Init() =0;
|
virtual void Init() =0;
|
||||||
|
|
@ -45,10 +41,10 @@ protected:
|
||||||
virtual ByteOrder GetByteOrder() const =0;
|
virtual ByteOrder GetByteOrder() const =0;
|
||||||
virtual void HashEndianCorrectedBlock(const HashWordType *data) =0;
|
virtual void HashEndianCorrectedBlock(const HashWordType *data) =0;
|
||||||
virtual size_t HashMultipleBlocks(const T *input, size_t length);
|
virtual size_t HashMultipleBlocks(const T *input, size_t length);
|
||||||
void HashBlock(const HashWordType *input) {HashMultipleBlocks(input, BlockSize());}
|
void HashBlock(const HashWordType *input) {HashMultipleBlocks(input, this->BlockSize());}
|
||||||
|
|
||||||
SecBlock<T> m_data; // Data buffer
|
virtual T* DataBuf() =0;
|
||||||
SecBlock<T> m_digest; // Message digest
|
virtual T* StateBuf() =0;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
T m_countLo, m_countHi;
|
T m_countLo, m_countHi;
|
||||||
|
|
@ -65,6 +61,7 @@ public:
|
||||||
CRYPTOPP_CONSTANT(BLOCKSIZE = T_BlockSize)
|
CRYPTOPP_CONSTANT(BLOCKSIZE = T_BlockSize)
|
||||||
// BCB2006 workaround: can't use BLOCKSIZE here
|
// BCB2006 workaround: can't use BLOCKSIZE here
|
||||||
CRYPTOPP_COMPILE_ASSERT((T_BlockSize & (T_BlockSize - 1)) == 0); // blockSize is a power of 2
|
CRYPTOPP_COMPILE_ASSERT((T_BlockSize & (T_BlockSize - 1)) == 0); // blockSize is a power of 2
|
||||||
|
unsigned int BlockSize() const {return T_BlockSize;}
|
||||||
|
|
||||||
ByteOrder GetByteOrder() const {return T_Endianness::ToEnum();}
|
ByteOrder GetByteOrder() const {return T_Endianness::ToEnum();}
|
||||||
|
|
||||||
|
|
@ -74,7 +71,8 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
IteratedHash() {this->SetBlockSize(T_BlockSize);}
|
T_HashWordType* DataBuf() {return this->m_data;}
|
||||||
|
FixedSizeSecBlock<T_HashWordType, T_BlockSize/sizeof(T_HashWordType)> m_data;
|
||||||
};
|
};
|
||||||
|
|
||||||
//! _
|
//! _
|
||||||
|
|
@ -87,13 +85,12 @@ public:
|
||||||
unsigned int DigestSize() const {return DIGESTSIZE;};
|
unsigned int DigestSize() const {return DIGESTSIZE;};
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
IteratedHashWithStaticTransform()
|
IteratedHashWithStaticTransform() {this->Init();}
|
||||||
{
|
void HashEndianCorrectedBlock(const T_HashWordType *data) {T_Transform::Transform(this->m_state, data);}
|
||||||
this->SetStateSize(T_StateSize);
|
void Init() {T_Transform::InitState(this->m_state);}
|
||||||
Init();
|
|
||||||
}
|
T_HashWordType* StateBuf() {return this->m_state;}
|
||||||
void HashEndianCorrectedBlock(const T_HashWordType *data) {T_Transform::Transform(this->m_digest, data);}
|
FixedSizeSecBlock<T_HashWordType, T_BlockSize/sizeof(T_HashWordType)> m_state;
|
||||||
void Init() {T_Transform::InitState(this->m_digest);}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
NAMESPACE_END
|
NAMESPACE_END
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue