1#ifndef CRYPTOPP_ITERHASH_H
2#define CRYPTOPP_ITERHASH_H
3
4#include "cryptlib.h"
5#include "secblock.h"
6#include "misc.h"
7#include "simple.h"
8
9NAMESPACE_BEGIN(CryptoPP)
10
11//! exception thrown when trying to hash more data than is allowed by a hash function
12class CRYPTOPP_DLL HashInputTooLong : public InvalidDataFormat
13{
14public:
15	explicit HashInputTooLong(const std::string &alg)
16		: InvalidDataFormat("IteratedHashBase: input data exceeds maximum allowed by hash function " + alg) {}
17};
18
19//! _
20template <class T, class BASE>
21class CRYPTOPP_NO_VTABLE IteratedHashBase : public BASE
22{
23public:
24	typedef T HashWordType;
25
26	IteratedHashBase() : m_countLo(0), m_countHi(0) {}
27	unsigned int OptimalBlockSize() const {return this->BlockSize();}
28	unsigned int OptimalDataAlignment() const {return GetAlignmentOf<T>();}
29	void Update(const byte *input, size_t length);
30	byte * CreateUpdateSpace(size_t &size);
31	void Restart();
32	void TruncatedFinal(byte *digest, size_t size);
33
34protected:
35	inline T GetBitCountHi() const {return (m_countLo >> (8*sizeof(T)-3)) + (m_countHi << 3);}
36	inline T GetBitCountLo() const {return m_countLo << 3;}
37
38	void PadLastBlock(unsigned int lastBlockSize, byte padFirst=0x80);
39	virtual void Init() =0;
40
41	virtual ByteOrder GetByteOrder() const =0;
42	virtual void HashEndianCorrectedBlock(const HashWordType *data) =0;
43	virtual size_t HashMultipleBlocks(const T *input, size_t length);
44	void HashBlock(const HashWordType *input) {HashMultipleBlocks(input, this->BlockSize());}
45
46	virtual T* DataBuf() =0;
47	virtual T* StateBuf() =0;
48
49private:
50	T m_countLo, m_countHi;
51};
52
53//! _
54template <class T_HashWordType, class T_Endianness, unsigned int T_BlockSize, class T_Base = HashTransformation>
55class CRYPTOPP_NO_VTABLE IteratedHash : public IteratedHashBase<T_HashWordType, T_Base>
56{
57public:
58	typedef T_Endianness ByteOrderClass;
59	typedef T_HashWordType HashWordType;
60
61	CRYPTOPP_CONSTANT(BLOCKSIZE = T_BlockSize)
62	// BCB2006 workaround: can't use BLOCKSIZE here
63	CRYPTOPP_COMPILE_ASSERT((T_BlockSize & (T_BlockSize - 1)) == 0);	// blockSize is a power of 2
64	unsigned int BlockSize() const {return T_BlockSize;}
65
66	ByteOrder GetByteOrder() const {return T_Endianness::ToEnum();}
67
68	inline static void CorrectEndianess(HashWordType *out, const HashWordType *in, size_t byteCount)
69	{
70		ConditionalByteReverse(T_Endianness::ToEnum(), out, in, byteCount);
71	}
72
73protected:
74	T_HashWordType* DataBuf() {return this->m_data;}
75	FixedSizeSecBlock<T_HashWordType, T_BlockSize/sizeof(T_HashWordType)> m_data;
76};
77
78//! _
79template <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>
80class CRYPTOPP_NO_VTABLE IteratedHashWithStaticTransform
81	: public ClonableImpl<T_Transform, AlgorithmImpl<IteratedHash<T_HashWordType, T_Endianness, T_BlockSize>, T_Transform> >
82{
83public:
84	CRYPTOPP_CONSTANT(DIGESTSIZE = T_DigestSize ? T_DigestSize : T_StateSize)
85	unsigned int DigestSize() const {return DIGESTSIZE;};
86
87protected:
88	IteratedHashWithStaticTransform() {this->Init();}
89	void HashEndianCorrectedBlock(const T_HashWordType *data) {T_Transform::Transform(this->m_state, data);}
90	void Init() {T_Transform::InitState(this->m_state);}
91
92	T_HashWordType* StateBuf() {return this->m_state;}
93	FixedSizeAlignedSecBlock<T_HashWordType, T_BlockSize/sizeof(T_HashWordType), T_StateAligned> m_state;
94};
95
96#ifndef __GNUC__
97	CRYPTOPP_DLL_TEMPLATE_CLASS IteratedHashBase<word64, HashTransformation>;
98	CRYPTOPP_STATIC_TEMPLATE_CLASS IteratedHashBase<word64, MessageAuthenticationCode>;
99
100	CRYPTOPP_DLL_TEMPLATE_CLASS IteratedHashBase<word32, HashTransformation>;
101	CRYPTOPP_STATIC_TEMPLATE_CLASS IteratedHashBase<word32, MessageAuthenticationCode>;
102#endif
103
104NAMESPACE_END
105
106#endif
107