Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

# iterhash.h

    00001 #ifndef CRYPTOPP_ITERHASH_H
    00002 #define CRYPTOPP_ITERHASH_H
    00003
    00004 #include "cryptlib.h"
    00005 #include "secblock.h"
    00006 #include "misc.h"
    00007 #include "simple.h"
    00008
    00009 NAMESPACE_BEGIN(CryptoPP)
    00010
    00011 //! _
    00012 template <class T, class BASE>
    00013 class CRYPTOPP_NO_VTABLE IteratedHashBase : public BASE
    00014 {
    00015 public:
    00016         typedef T HashWordType;
    00017
    00018         IteratedHashBase() : m_countHi(0), m_countLo(0) {}
    00019         unsigned int BlockSize() const {return m_data.size() * sizeof(T);}
    00020         unsigned int OptimalBlockSize() const {return BlockSize();}
    00021         unsigned int OptimalDataAlignment() const {return sizeof(T);}
    00022         void Update(const byte *input, unsigned int length);
    00023         byte * CreateUpdateSpace(unsigned int &size);
    00024         void Restart();
    00025
    00026 protected:
    00027         void SetBlockSize(unsigned int blockSize) {m_data.resize(blockSize / sizeof(HashWordType));}
    00028         void SetStateSize(unsigned int stateSize) {m_digest.resize(stateSize / sizeof(HashWordType));}
    00029
    00030         T GetBitCountHi() const {return (m_countLo >> (8*sizeof(T)-3)) + (m_countHi << 3);}
    00031         T GetBitCountLo() const {return m_countLo << 3;}
    00032
    00033         virtual unsigned int HashMultipleBlocks(const T *input, unsigned int length);
    00034         void PadLastBlock(unsigned int lastBlockSize, byte padFirst=0x80);
    00035         virtual void Init() =0;
    00036         virtual void HashBlock(const T *input) =0;
    00037
    00038         SecBlock<T> m_data;                     // Data buffer
    00039         SecBlock<T> m_digest;           // Message digest
    00040
    00041 private:
    00042         T m_countLo, m_countHi;
    00043 };
    00044
    00045 #ifdef WORD64_AVAILABLE
    00046 CRYPTOPP_STATIC_TEMPLATE_CLASS IteratedHashBase<word64, HashTransformation>;
    00047 CRYPTOPP_STATIC_TEMPLATE_CLASS IteratedHashBase<word64, MessageAuthenticationCode>;
    00048 #endif
    00049
    00050 CRYPTOPP_DLL_TEMPLATE_CLASS IteratedHashBase<word32, HashTransformation>;
    00051 CRYPTOPP_STATIC_TEMPLATE_CLASS IteratedHashBase<word32, MessageAuthenticationCode>;
    00052
    00053 //! _
    00054 template <class T, class B, class BASE>
    00055 class CRYPTOPP_NO_VTABLE IteratedHashBase2 : public IteratedHashBase<T, BASE>
    00056 {
    00057 public:
    00058         typedef B ByteOrderClass;
    00059         typedef typename IteratedHashBase<T, BASE>::HashWordType HashWordType;
    00060
    00061         inline static void CorrectEndianess(HashWordType *out, const HashWordType *in, unsigned int byteCount)
    00062         {
    00063                 ConditionalByteReverse(B::ToEnum(), out, in, byteCount);
    00064         }
    00065
    00066         void TruncatedFinal(byte *digest, unsigned int size);
    00067
    00068 protected:
    00069         void HashBlock(const HashWordType *input);
    00070         virtual void HashEndianCorrectedBlock(const HashWordType *data) =0;
    00071 };
    00072
    00073 //! _
    00074 template <class T_HashWordType, class T_Endianness, unsigned int T_BlockSize, class T_Base = HashTransformation>
    00075 class CRYPTOPP_NO_VTABLE IteratedHash : public IteratedHashBase2<T_HashWordType, T_Endianness, T_Base>
    00076 {
    00077 public:
    00078         enum {BLOCKSIZE = T_BlockSize};
    00079         CRYPTOPP_COMPILE_ASSERT((BLOCKSIZE & (BLOCKSIZE - 1)) == 0);            // blockSize is a power of 2
    00080
    00081 protected:
    00082         IteratedHash() {this->SetBlockSize(T_BlockSize);}
    00083 };
    00084
    00085 //! _
    00086 template <class T_HashWordType, class T_Endianness, unsigned int T_BlockSize, unsigned int T_StateSize, class T_Transform, unsigned int T_DigestSize = T_StateSize>
    00087 class CRYPTOPP_NO_VTABLE IteratedHashWithStaticTransform
    00088         : public ClonableImpl<T_Transform, AlgorithmImpl<IteratedHash<T_HashWordType, T_Endianness, T_BlockSize>, T_Transform> >
    00089 {
    00090 public:
    00091         enum {DIGESTSIZE = T_DigestSize};
    00092         unsigned int DigestSize() const {return DIGESTSIZE;};
    00093
    00094 protected:
    00095         IteratedHashWithStaticTransform()
    00096         {
    00097                 this->SetStateSize(T_StateSize);
    00098                 Init();
    00099         }
    00100         void HashEndianCorrectedBlock(const T_HashWordType *data) {T_Transform::Transform(this->m_digest, data);}
    00101         void Init() {T_Transform::InitState(this->m_digest);}
    00102 };
    00103
    00104 // *************************************************************
    00105
    00106 template <class T, class B, class BASE> void IteratedHashBase2<T, B, BASE>::TruncatedFinal(byte *digest, unsigned int size)
    00107 {
    00108         this->ThrowIfInvalidTruncatedSize(size);
    00109
    00110         PadLastBlock(this->BlockSize() - 2*sizeof(HashWordType));
    00111         CorrectEndianess(this->m_data, this->m_data, this->BlockSize() - 2*sizeof(HashWordType));
    00112
    00113         this->m_data[this->m_data.size()-2] = B::ToEnum() ? this->GetBitCountHi() : this->GetBitCountLo();
    00114         this->m_data[this->m_data.size()-1] = B::ToEnum() ? this->GetBitCountLo() : this->GetBitCountHi();
    00115
    00116         HashEndianCorrectedBlock(this->m_data);
    00117         CorrectEndianess(this->m_digest, this->m_digest, this->DigestSize());
    00118         memcpy(digest, this->m_digest, size);
    00119
    00120         this->Restart();                // reinit for next use
    00121 }
    00122
    00123 template <class T, class B, class BASE> void IteratedHashBase2<T, B, BASE>::HashBlock(const HashWordType *input)
    00124 {
    00125         if (NativeByteOrderIs(B::ToEnum()))
    00126                 HashEndianCorrectedBlock(input);
    00127         else
    00128         {
    00129                 ByteReverse(this->m_data.begin(), input, this->BlockSize());
    00130                 HashEndianCorrectedBlock(this->m_data);
    00131         }
    00132 }
    00133
    00134 NAMESPACE_END
    00135
    00136 #endif