diff options
author | weidai <weidai@57ff6487-cd31-0410-9ec3-f628ee90f5f0> | 2007-04-16 00:33:09 +0000 |
---|---|---|
committer | weidai <weidai@57ff6487-cd31-0410-9ec3-f628ee90f5f0> | 2007-04-16 00:33:09 +0000 |
commit | 6320616a7ffda3e44bc6a82a60abc8e9929ea5bd (patch) | |
tree | ef363033c979770ff3d187dc3b40bb4a5c81322d | |
parent | 28d0351a8f22f7e686ce5e833469e8c1429316ef (diff) | |
download | cryptopp-6320616a7ffda3e44bc6a82a60abc8e9929ea5bd.tar.gz |
optimizations
git-svn-id: svn://svn.code.sf.net/p/cryptopp/code/trunk/c5@310 57ff6487-cd31-0410-9ec3-f628ee90f5f0
-rw-r--r-- | iterhash.cpp | 67 | ||||
-rw-r--r-- | iterhash.h | 35 |
2 files changed, 54 insertions, 48 deletions
diff --git a/iterhash.cpp b/iterhash.cpp index eee0bfe..73bbf98 100644 --- a/iterhash.cpp +++ b/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) throw HashInputTooLong(this->AlgorithmName()); - unsigned int blockSize = BlockSize(); + unsigned int blockSize = this->BlockSize(); unsigned int num = ModPowerOf2(oldCountLo, blockSize); + T* dataBuf = this->DataBuf(); + byte* data = (byte *)dataBuf; if (num != 0) // process left over data { if ((num+len) >= blockSize) { - memcpy((byte *)m_data.begin()+num, input, blockSize-num); - HashBlock(m_data); + memcpy(data+num, input, blockSize-num); + HashBlock(dataBuf); input += (blockSize-num); len-=(blockSize - num); num=0; @@ -35,7 +37,7 @@ template <class T, class BASE> void IteratedHashBase<T, BASE>::Update(const byte } else { - memcpy((byte *)m_data.begin()+num, input, len); + memcpy(data+num, input, len); 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 if (len >= blockSize) { - if (input == (byte *)m_data.begin()) + if (input == data) { assert(len == blockSize); - HashBlock(m_data); + HashBlock(dataBuf); return; } else if (IsAligned<T>(input)) @@ -58,36 +60,37 @@ template <class T, class BASE> void IteratedHashBase<T, BASE>::Update(const byte else do { // copy input first if it's not aligned correctly - memcpy(m_data, input, blockSize); - HashBlock(m_data); + memcpy(data, input, blockSize); + HashBlock(dataBuf); input+=blockSize; 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) { - unsigned int blockSize = BlockSize(); + unsigned int blockSize = this->BlockSize(); unsigned int num = ModPowerOf2(m_countLo, blockSize); 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) { - unsigned int blockSize = BlockSize(); - bool noReverse = NativeByteOrderIs(GetByteOrder()); + unsigned int blockSize = this->BlockSize(); + bool noReverse = NativeByteOrderIs(this->GetByteOrder()); + T* dataBuf = this->DataBuf(); do { if (noReverse) - HashEndianCorrectedBlock(input); + this->HashEndianCorrectedBlock(input); else { - ByteReverse(this->m_data.begin(), input, this->BlockSize()); - HashEndianCorrectedBlock(this->m_data); + ByteReverse(dataBuf, input, this->BlockSize()); + this->HashEndianCorrectedBlock(dataBuf); } 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) { - unsigned int blockSize = BlockSize(); + unsigned int blockSize = this->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) - memset((byte *)m_data.begin()+num, 0, lastBlockSize-num); + memset(data+num, 0, lastBlockSize-num); else { - memset((byte *)m_data.begin()+num, 0, blockSize-num); - HashBlock(m_data); - memset(m_data, 0, lastBlockSize); + memset(data+num, 0, blockSize-num); + HashBlock(dataBuf); + memset(data, 0, lastBlockSize); } } @@ -122,16 +127,20 @@ template <class T, class BASE> void IteratedHashBase<T, BASE>::TruncatedFinal(by { 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(); - 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(); - this->m_data[this->m_data.size()-1] = order ? this->GetBitCountLo() : this->GetBitCountHi(); + PadLastBlock(blockSize - 2*sizeof(HashWordType)); + ConditionalByteReverse<HashWordType>(order, dataBuf, dataBuf, blockSize - 2*sizeof(HashWordType)); - HashEndianCorrectedBlock(this->m_data); - ConditionalByteReverse<HashWordType>(order, this->m_digest, this->m_digest, this->DigestSize()); - memcpy(digest, this->m_digest, size); + dataBuf[blockSize/sizeof(T)-2] = order ? this->GetBitCountHi() : this->GetBitCountLo(); + dataBuf[blockSize/sizeof(T)-1] = order ? this->GetBitCountLo() : this->GetBitCountHi(); + + HashEndianCorrectedBlock(dataBuf); + ConditionalByteReverse<HashWordType>(order, stateBuf, stateBuf, this->DigestSize()); + memcpy(digest, stateBuf, size); this->Restart(); // reinit for next use } @@ -24,20 +24,16 @@ public: typedef T HashWordType; IteratedHashBase() : m_countLo(0), m_countHi(0) {} - unsigned int BlockSize() const {return (unsigned int)m_data.size() * sizeof(T);} - unsigned int OptimalBlockSize() const {return BlockSize();} - unsigned int OptimalDataAlignment() const {return sizeof(T);} + unsigned int OptimalBlockSize() const {return this->BlockSize();} + unsigned int OptimalDataAlignment() const {return GetAlignmentOf<T>();} void Update(const byte *input, size_t length); byte * CreateUpdateSpace(size_t &size); void Restart(); void TruncatedFinal(byte *digest, size_t size); protected: - void SetBlockSize(unsigned int blockSize) {m_data.resize(blockSize / sizeof(HashWordType));} - void SetStateSize(unsigned int stateSize) {m_digest.resize(stateSize / sizeof(HashWordType));} - - T GetBitCountHi() const {return (m_countLo >> (8*sizeof(T)-3)) + (m_countHi << 3);} - T GetBitCountLo() const {return m_countLo << 3;} + inline T GetBitCountHi() const {return (m_countLo >> (8*sizeof(T)-3)) + (m_countHi << 3);} + inline T GetBitCountLo() const {return m_countLo << 3;} void PadLastBlock(unsigned int lastBlockSize, byte padFirst=0x80); virtual void Init() =0; @@ -45,10 +41,10 @@ protected: virtual ByteOrder GetByteOrder() const =0; virtual void HashEndianCorrectedBlock(const HashWordType *data) =0; 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 - SecBlock<T> m_digest; // Message digest + virtual T* DataBuf() =0; + virtual T* StateBuf() =0; private: T m_countLo, m_countHi; @@ -65,6 +61,7 @@ public: CRYPTOPP_CONSTANT(BLOCKSIZE = T_BlockSize) // BCB2006 workaround: can't use BLOCKSIZE here 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();} @@ -74,7 +71,8 @@ public: } 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;}; protected: - IteratedHashWithStaticTransform() - { - this->SetStateSize(T_StateSize); - Init(); - } - void HashEndianCorrectedBlock(const T_HashWordType *data) {T_Transform::Transform(this->m_digest, data);} - void Init() {T_Transform::InitState(this->m_digest);} + IteratedHashWithStaticTransform() {this->Init();} + void HashEndianCorrectedBlock(const T_HashWordType *data) {T_Transform::Transform(this->m_state, data);} + void Init() {T_Transform::InitState(this->m_state);} + + T_HashWordType* StateBuf() {return this->m_state;} + FixedSizeSecBlock<T_HashWordType, T_BlockSize/sizeof(T_HashWordType)> m_state; }; NAMESPACE_END |