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 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 BlockSize() const {return m_data.size() * sizeof(T);}
00028 unsigned int OptimalBlockSize() const {return BlockSize();}
00029 unsigned int OptimalDataAlignment() const {return sizeof(T);}
00030 void Update(const byte *input, unsigned int length);
00031 byte * CreateUpdateSpace(unsigned int &size);
00032 void Restart();
00033 void TruncatedFinal(byte *digest, unsigned int size);
00034
00035 protected:
00036 void SetBlockSize(unsigned int blockSize) {m_data.resize(blockSize / sizeof(HashWordType));}
00037 void SetStateSize(unsigned int stateSize) {m_digest.resize(stateSize / sizeof(HashWordType));}
00038
00039 T GetBitCountHi() const {return (m_countLo >> (8*sizeof(T)-3)) + (m_countHi << 3);}
00040 T GetBitCountLo() const {return m_countLo << 3;}
00041
00042 void PadLastBlock(unsigned int lastBlockSize, byte padFirst=0x80);
00043 virtual void Init() =0;
00044
00045 virtual ByteOrder GetByteOrder() const =0;
00046 virtual void HashEndianCorrectedBlock(const HashWordType *data) =0;
00047 virtual unsigned int HashMultipleBlocks(const T *input, unsigned int length);
00048 void HashBlock(const HashWordType *input) {HashMultipleBlocks(input, BlockSize());}
00049
00050 SecBlock<T> m_data;
00051 SecBlock<T> m_digest;
00052
00053 private:
00054 T m_countLo, m_countHi;
00055 };
00056
00057 #ifdef WORD64_AVAILABLE
00058 CRYPTOPP_DLL_TEMPLATE_CLASS IteratedHashBase<word64, HashTransformation>;
00059 CRYPTOPP_STATIC_TEMPLATE_CLASS IteratedHashBase<word64, MessageAuthenticationCode>;
00060 #endif
00061
00062 CRYPTOPP_DLL_TEMPLATE_CLASS IteratedHashBase<word32, HashTransformation>;
00063 CRYPTOPP_STATIC_TEMPLATE_CLASS IteratedHashBase<word32, MessageAuthenticationCode>;
00064
00065
00066 template <class T_HashWordType, class T_Endianness, unsigned int T_BlockSize, class T_Base = HashTransformation>
00067 class CRYPTOPP_NO_VTABLE IteratedHash : public IteratedHashBase<T_HashWordType, T_Base>
00068 {
00069 public:
00070 typedef T_Endianness ByteOrderClass;
00071 typedef T_HashWordType HashWordType;
00072
00073 enum {BLOCKSIZE = T_BlockSize};
00074 CRYPTOPP_COMPILE_ASSERT((BLOCKSIZE & (BLOCKSIZE - 1)) == 0);
00075
00076 ByteOrder GetByteOrder() const {return T_Endianness::ToEnum();}
00077
00078 inline static void CorrectEndianess(HashWordType *out, const HashWordType *in, unsigned int byteCount)
00079 {
00080 ConditionalByteReverse(T_Endianness::ToEnum(), out, in, byteCount);
00081 }
00082
00083 protected:
00084 IteratedHash() {this->SetBlockSize(T_BlockSize);}
00085 };
00086
00087
00088 template <class T_HashWordType, class T_Endianness, unsigned int T_BlockSize, unsigned int T_StateSize, class T_Transform, unsigned int T_DigestSize = T_StateSize>
00089 class CRYPTOPP_NO_VTABLE IteratedHashWithStaticTransform
00090 : public ClonableImpl<T_Transform, AlgorithmImpl<IteratedHash<T_HashWordType, T_Endianness, T_BlockSize>, T_Transform> >
00091 {
00092 public:
00093 enum {DIGESTSIZE = T_DigestSize};
00094 unsigned int DigestSize() const {return DIGESTSIZE;};
00095
00096 protected:
00097 IteratedHashWithStaticTransform()
00098 {
00099 this->SetStateSize(T_StateSize);
00100 Init();
00101 }
00102 void HashEndianCorrectedBlock(const T_HashWordType *data) {T_Transform::Transform(this->m_digest, data);}
00103 void Init() {T_Transform::InitState(this->m_digest);}
00104 };
00105
00106 NAMESPACE_END
00107
00108 #endif