Crypto++
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 //! exception thrown when trying to hash more data than is allowed by a hash function
00012 class CRYPTOPP_DLL HashInputTooLong : public InvalidDataFormat
00013 {
00014 public:
00015     explicit HashInputTooLong(const std::string &alg)
00016         : InvalidDataFormat("IteratedHashBase: input data exceeds maximum allowed by hash function " + alg) {}
00017 };
00018 
00019 //! _
00020 template <class T, class BASE>
00021 class CRYPTOPP_NO_VTABLE IteratedHashBase : public BASE
00022 {
00023 public:
00024     typedef T HashWordType;
00025 
00026     IteratedHashBase() : m_countLo(0), m_countHi(0) {}
00027     unsigned int OptimalBlockSize() const {return this->BlockSize();}
00028     unsigned int OptimalDataAlignment() const {return GetAlignmentOf<T>();}
00029     void Update(const byte *input, size_t length);
00030     byte * CreateUpdateSpace(size_t &size);
00031     void Restart();
00032     void TruncatedFinal(byte *digest, size_t size);
00033 
00034 protected:
00035     inline T GetBitCountHi() const {return (m_countLo >> (8*sizeof(T)-3)) + (m_countHi << 3);}
00036     inline T GetBitCountLo() const {return m_countLo << 3;}
00037 
00038     void PadLastBlock(unsigned int lastBlockSize, byte padFirst=0x80);
00039     virtual void Init() =0;
00040 
00041     virtual ByteOrder GetByteOrder() const =0;
00042     virtual void HashEndianCorrectedBlock(const HashWordType *data) =0;
00043     virtual size_t HashMultipleBlocks(const T *input, size_t length);
00044     void HashBlock(const HashWordType *input) {HashMultipleBlocks(input, this->BlockSize());}
00045 
00046     virtual T* DataBuf() =0;
00047     virtual T* StateBuf() =0;
00048 
00049 private:
00050     T m_countLo, m_countHi;
00051 };
00052 
00053 //! _
00054 template <class T_HashWordType, class T_Endianness, unsigned int T_BlockSize, class T_Base = HashTransformation>
00055 class CRYPTOPP_NO_VTABLE IteratedHash : public IteratedHashBase<T_HashWordType, T_Base>
00056 {
00057 public:
00058     typedef T_Endianness ByteOrderClass;
00059     typedef T_HashWordType HashWordType;
00060 
00061     CRYPTOPP_CONSTANT(BLOCKSIZE = T_BlockSize)
00062     // BCB2006 workaround: can't use BLOCKSIZE here
00063     CRYPTOPP_COMPILE_ASSERT((T_BlockSize & (T_BlockSize - 1)) == 0);    // blockSize is a power of 2
00064     unsigned int BlockSize() const {return T_BlockSize;}
00065 
00066     ByteOrder GetByteOrder() const {return T_Endianness::ToEnum();}
00067 
00068     inline static void CorrectEndianess(HashWordType *out, const HashWordType *in, size_t byteCount)
00069     {
00070         ConditionalByteReverse(T_Endianness::ToEnum(), out, in, byteCount);
00071     }
00072 
00073 protected:
00074     T_HashWordType* DataBuf() {return this->m_data;}
00075     FixedSizeSecBlock<T_HashWordType, T_BlockSize/sizeof(T_HashWordType)> m_data;
00076 };
00077 
00078 //! _
00079 template <class T_HashWordType, class T_Endianness, unsigned int T_BlockSize, unsigned int T_StateSize, class T_Transform, unsigned int T_DigestSize = 0, bool T_StateAligned = false>
00080 class CRYPTOPP_NO_VTABLE IteratedHashWithStaticTransform
00081     : public ClonableImpl<T_Transform, AlgorithmImpl<IteratedHash<T_HashWordType, T_Endianness, T_BlockSize>, T_Transform> >
00082 {
00083 public:
00084     CRYPTOPP_CONSTANT(DIGESTSIZE = T_DigestSize ? T_DigestSize : T_StateSize)
00085     unsigned int DigestSize() const {return DIGESTSIZE;};
00086 
00087 protected:
00088     IteratedHashWithStaticTransform() {this->Init();}
00089     void HashEndianCorrectedBlock(const T_HashWordType *data) {T_Transform::Transform(this->m_state, data);}
00090     void Init() {T_Transform::InitState(this->m_state);}
00091 
00092     T_HashWordType* StateBuf() {return this->m_state;}
00093     FixedSizeAlignedSecBlock<T_HashWordType, T_BlockSize/sizeof(T_HashWordType), T_StateAligned> m_state;
00094 };
00095 
00096 #ifndef __GNUC__
00097     CRYPTOPP_DLL_TEMPLATE_CLASS IteratedHashBase<word64, HashTransformation>;
00098     CRYPTOPP_STATIC_TEMPLATE_CLASS IteratedHashBase<word64, MessageAuthenticationCode>;
00099 
00100     CRYPTOPP_DLL_TEMPLATE_CLASS IteratedHashBase<word32, HashTransformation>;
00101     CRYPTOPP_STATIC_TEMPLATE_CLASS IteratedHashBase<word32, MessageAuthenticationCode>;
00102 #endif
00103 
00104 NAMESPACE_END
00105 
00106 #endif