Crypto++  5.6.3
Free C++ class library of cryptographic schemes
iterhash.h
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 
9 NAMESPACE_BEGIN(CryptoPP)
10 
11 //! exception thrown when trying to hash more data than is allowed by a hash function
12 class CRYPTOPP_DLL HashInputTooLong : public InvalidDataFormat
13 {
14 public:
15  explicit HashInputTooLong(const std::string &alg)
16  : InvalidDataFormat("IteratedHashBase: input data exceeds maximum allowed by hash function " + alg) {}
17 };
18 
19 //! _
20 template <class T, class BASE>
21 class CRYPTOPP_NO_VTABLE IteratedHashBase : public BASE
22 {
23 public:
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 
34 protected:
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 
49 private:
50  T m_countLo, m_countHi;
51 };
52 
53 //! _
54 template <class T_HashWordType, class T_Endianness, unsigned int T_BlockSize, class T_Base = HashTransformation>
55 class CRYPTOPP_NO_VTABLE IteratedHash : public IteratedHashBase<T_HashWordType, T_Base>
56 {
57 public:
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 
73 protected:
74  T_HashWordType* DataBuf() {return this->m_data;}
75  FixedSizeSecBlock<T_HashWordType, T_BlockSize/sizeof(T_HashWordType)> m_data;
76 };
77 
78 //! _
79 template <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>
80 class CRYPTOPP_NO_VTABLE IteratedHashWithStaticTransform
81  : public ClonableImpl<T_Transform, AlgorithmImpl<IteratedHash<T_HashWordType, T_Endianness, T_BlockSize>, T_Transform> >
82 {
83 public:
84  CRYPTOPP_CONSTANT(DIGESTSIZE = T_DigestSize ? T_DigestSize : T_StateSize)
85  unsigned int DigestSize() const {return DIGESTSIZE;};
86 
87 protected:
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 
104 NAMESPACE_END
105 
106 #endif
const char * DigestSize()
int, in bytes
Definition: argnames.h:78
Classes providing basic library services.
Utility functions for the Crypto++ library.
ByteOrder
Provides the byte ordering.
Definition: cryptlib.h:123
Base class for identifying alogorithm.
Definition: simple.h:26
Abstract base classes that provide a uniform interface to this library.
exception thrown when trying to hash more data than is allowed by a hash function ...
Definition: iterhash.h:12
Classes and functions for secure memory allocations.
#define CRYPTOPP_COMPILE_ASSERT(expr)
Compile time assertion.
Definition: misc.h:102
T ConditionalByteReverse(ByteOrder order, T value)
Reverses bytes in a value depending upon endianess.
Definition: misc.h:1705
Fixed size stack-based SecBlock with 16-byte alignment.
Definition: secblock.h:765
Fixed size stack-based SecBlock.
Definition: secblock.h:752
const char * BlockSize()
int, in bytes
Definition: argnames.h:26
Crypto++ library namespace.
Input data was received that did not conform to expected format.
Definition: cryptlib.h:189