Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | 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 //! 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 #ifndef SKIP_EXPLICIT_INSTANTIATION
00058 #ifdef WORD64_AVAILABLE
00059 CRYPTOPP_DLL_TEMPLATE_CLASS IteratedHashBase<word64, HashTransformation>;
00060 CRYPTOPP_STATIC_TEMPLATE_CLASS IteratedHashBase<word64, MessageAuthenticationCode>;
00061 #endif
00062 
00063 CRYPTOPP_DLL_TEMPLATE_CLASS IteratedHashBase<word32, HashTransformation>;
00064 CRYPTOPP_STATIC_TEMPLATE_CLASS IteratedHashBase<word32, MessageAuthenticationCode>;
00065 #endif // SKIP_EXPLICIT_INSTANTIATION
00066 
00067 //! _
00068 template <class T_HashWordType, class T_Endianness, unsigned int T_BlockSize, class T_Base = HashTransformation>
00069 class CRYPTOPP_NO_VTABLE IteratedHash : public IteratedHashBase<T_HashWordType, T_Base>
00070 {
00071 public:
00072         typedef T_Endianness ByteOrderClass;
00073         typedef T_HashWordType HashWordType;
00074 
00075         enum {BLOCKSIZE = T_BlockSize};
00076         CRYPTOPP_COMPILE_ASSERT((BLOCKSIZE & (BLOCKSIZE - 1)) == 0);            // blockSize is a power of 2
00077 
00078         ByteOrder GetByteOrder() const {return T_Endianness::ToEnum();}
00079 
00080         inline static void CorrectEndianess(HashWordType *out, const HashWordType *in, size_t byteCount)
00081         {
00082                 ConditionalByteReverse(T_Endianness::ToEnum(), out, in, byteCount);
00083         }
00084 
00085 protected:
00086         IteratedHash() {this->SetBlockSize(T_BlockSize);}
00087 };
00088 
00089 //! _
00090 template <class T_HashWordType, class T_Endianness, unsigned int T_BlockSize, unsigned int T_StateSize, class T_Transform, unsigned int T_DigestSize = T_StateSize>
00091 class CRYPTOPP_NO_VTABLE IteratedHashWithStaticTransform
00092         : public ClonableImpl<T_Transform, AlgorithmImpl<IteratedHash<T_HashWordType, T_Endianness, T_BlockSize>, T_Transform> >
00093 {
00094 public:
00095         enum {DIGESTSIZE = T_DigestSize};
00096         unsigned int DigestSize() const {return DIGESTSIZE;};
00097 
00098 protected:
00099         IteratedHashWithStaticTransform()
00100         {
00101                 this->SetStateSize(T_StateSize);
00102                 Init();
00103         }
00104         void HashEndianCorrectedBlock(const T_HashWordType *data) {T_Transform::Transform(this->m_digest, data);}
00105         void Init() {T_Transform::InitState(this->m_digest);}
00106 };
00107 
00108 NAMESPACE_END
00109 
00110 #endif

Generated on Tue Aug 16 08:38:42 2005 for Crypto++ by  doxygen 1.3.9.1