- Main Page
- Namespaces
- Classes
- Files

- File List
- 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 //! _
    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         enum {BLOCKSIZE = T_BlockSize};
    00066         CRYPTOPP_COMPILE_ASSERT((BLOCKSIZE & (BLOCKSIZE - 1)) == 0);            // blockSize is a power of 2
    00067
    00068         ByteOrder GetByteOrder() const {return T_Endianness::ToEnum();}
    00069
    00070         inline static void CorrectEndianess(HashWordType *out, const HashWordType *in, size_t byteCount)
    00071         {
    00072                 ConditionalByteReverse(T_Endianness::ToEnum(), out, in, byteCount);
    00073         }
    00074
    00075 protected:
    00076         IteratedHash() {this->SetBlockSize(T_BlockSize);}
    00077 };
    00078
    00079 //! _
    00080 template <class T_HashWordType, class T_Endianness, unsigned int T_BlockSize, unsigned int T_StateSize, class T_Transform, unsigned int T_DigestSize = T_StateSize>
    00081 class CRYPTOPP_NO_VTABLE IteratedHashWithStaticTransform
    00082         : public ClonableImpl<T_Transform, AlgorithmImpl<IteratedHash<T_HashWordType, T_Endianness, T_BlockSize>, T_Transform> >
    00083 {
    00084 public:
    00085         enum {DIGESTSIZE = T_DigestSize};
    00086         unsigned int DigestSize() const {return DIGESTSIZE;};
    00087
    00088 protected:
    00089         IteratedHashWithStaticTransform()
    00090         {
    00091                 this->SetStateSize(T_StateSize);
    00092                 Init();
    00093         }
    00094         void HashEndianCorrectedBlock(const T_HashWordType *data) {T_Transform::Transform(this->m_digest, data);}
    00095         void Init() {T_Transform::InitState(this->m_digest);}
    00096 };
    00097
    00098 NAMESPACE_END
    00099
    00100 #ifdef CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES
    00101 #include "iterhash.cpp"
    00102 #endif
    00103
    00104 NAMESPACE_BEGIN(CryptoPP)
    00105
    00106 #ifdef WORD64_AVAILABLE
    00107 CRYPTOPP_DLL_TEMPLATE_CLASS IteratedHashBase<word64, HashTransformation>;
    00108 CRYPTOPP_STATIC_TEMPLATE_CLASS IteratedHashBase<word64, MessageAuthenticationCode>;
    00109 #endif
    00110
    00111 CRYPTOPP_DLL_TEMPLATE_CLASS IteratedHashBase<word32, HashTransformation>;
    00112 CRYPTOPP_STATIC_TEMPLATE_CLASS IteratedHashBase<word32, MessageAuthenticationCode>;
    00113
    00114 NAMESPACE_END
    00115
    00116 #endif