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 BlockSize() const {return (unsigned int)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, size_t length);
00031         byte * CreateUpdateSpace(size_t &size);
00032         void Restart();
00033         void TruncatedFinal(byte *digest, size_t 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 size_t HashMultipleBlocks(const T *input, size_t length);
00048         void HashBlock(const HashWordType *input) {HashMultipleBlocks(input, BlockSize());}
00049 
00050         SecBlock<T> m_data;                     // Data buffer
00051         SecBlock<T> m_digest;           // Message digest
00052 
00053 private:
00054         T m_countLo, m_countHi;
00055 };
00056 
00057 //! _
00058 template <class T_HashWordType, class T_Endianness, unsigned int T_BlockSize, class T_Base = HashTransformation>
00059 class CRYPTOPP_NO_VTABLE IteratedHash : public IteratedHashBase<T_HashWordType, T_Base>
00060 {
00061 public:
00062         typedef T_Endianness ByteOrderClass;
00063         typedef T_HashWordType HashWordType;
00064 
00065         CRYPTOPP_CONSTANT(BLOCKSIZE = T_BlockSize)
00066         // BCB2006 workaround: can't use BLOCKSIZE here
00067         CRYPTOPP_COMPILE_ASSERT((T_BlockSize & (T_BlockSize - 1)) == 0);        // blockSize is a power of 2
00068 
00069         ByteOrder GetByteOrder() const {return T_Endianness::ToEnum();}
00070 
00071         inline static void CorrectEndianess(HashWordType *out, const HashWordType *in, size_t byteCount)
00072         {
00073                 ConditionalByteReverse(T_Endianness::ToEnum(), out, in, byteCount);
00074         }
00075 
00076 protected:
00077         IteratedHash() {this->SetBlockSize(T_BlockSize);}
00078 };
00079 
00080 //! _
00081 template <class T_HashWordType, class T_Endianness, unsigned int T_BlockSize, unsigned int T_StateSize, class T_Transform, unsigned int T_DigestSize = 0>
00082 class CRYPTOPP_NO_VTABLE IteratedHashWithStaticTransform
00083         : public ClonableImpl<T_Transform, AlgorithmImpl<IteratedHash<T_HashWordType, T_Endianness, T_BlockSize>, T_Transform> >
00084 {
00085 public:
00086         CRYPTOPP_CONSTANT(DIGESTSIZE = T_DigestSize ? T_DigestSize : T_StateSize)
00087         unsigned int DigestSize() const {return DIGESTSIZE;};
00088 
00089 protected:
00090         IteratedHashWithStaticTransform()
00091         {
00092                 this->SetStateSize(T_StateSize);
00093                 Init();
00094         }
00095         void HashEndianCorrectedBlock(const T_HashWordType *data) {T_Transform::Transform(this->m_digest, data);}
00096         void Init() {T_Transform::InitState(this->m_digest);}
00097 };
00098 
00099 NAMESPACE_END
00100 
00101 #ifdef CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES
00102 #include "iterhash.cpp"
00103 #endif
00104 
00105 NAMESPACE_BEGIN(CryptoPP)
00106 
00107 #ifdef WORD64_AVAILABLE
00108 CRYPTOPP_DLL_TEMPLATE_CLASS IteratedHashBase<word64, HashTransformation>;
00109 CRYPTOPP_STATIC_TEMPLATE_CLASS IteratedHashBase<word64, MessageAuthenticationCode>;
00110 #endif
00111 
00112 CRYPTOPP_DLL_TEMPLATE_CLASS IteratedHashBase<word32, HashTransformation>;
00113 CRYPTOPP_STATIC_TEMPLATE_CLASS IteratedHashBase<word32, MessageAuthenticationCode>;
00114 
00115 NAMESPACE_END
00116 
00117 #endif

Generated on Sat Dec 23 02:07:08 2006 for Crypto++ by  doxygen 1.5.1-p1