Fabcoin Core  0.16.2
P2P Digital Currency
iterhash.h
Go to the documentation of this file.
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 
10 
14 {
15 public:
16  explicit HashInputTooLong(const std::string &alg)
17  : InvalidDataFormat("IteratedHashBase: input data exceeds maximum allowed by hash function " + alg) {}
18 };
19 
26 template <class T, class BASE>
28 {
29 public:
30  typedef T HashWordType;
31 
33  IteratedHashBase() : m_countLo(0), m_countHi(0) {}
34 
40  unsigned int OptimalBlockSize() const {return this->BlockSize();}
41 
45  unsigned int OptimalDataAlignment() const {return GetAlignmentOf<T>();}
46 
50  void Update(const byte *input, size_t length);
51 
60  byte * CreateUpdateSpace(size_t &size);
61 
64  void Restart();
65 
71  void TruncatedFinal(byte *digest, size_t digestSize);
72 
73 protected:
74  inline T GetBitCountHi() const {return (m_countLo >> (8*sizeof(T)-3)) + (m_countHi << 3);}
75  inline T GetBitCountLo() const {return m_countLo << 3;}
76 
77  void PadLastBlock(unsigned int lastBlockSize, byte padFirst=0x80);
78  virtual void Init() =0;
79 
80  virtual ByteOrder GetByteOrder() const =0;
81  virtual void HashEndianCorrectedBlock(const HashWordType *data) =0;
82  virtual size_t HashMultipleBlocks(const T *input, size_t length);
83  void HashBlock(const HashWordType *input) {HashMultipleBlocks(input, this->BlockSize());}
84 
85  virtual T* DataBuf() =0;
86  virtual T* StateBuf() =0;
87 
88 private:
89  T m_countLo, m_countHi;
90 };
91 
100 template <class T_HashWordType, class T_Endianness, unsigned int T_BlockSize, class T_Base = HashTransformation>
101 class CRYPTOPP_NO_VTABLE IteratedHash : public IteratedHashBase<T_HashWordType, T_Base>
102 {
103 public:
104  typedef T_Endianness ByteOrderClass;
105  typedef T_HashWordType HashWordType;
106 
107  CRYPTOPP_CONSTANT(BLOCKSIZE = T_BlockSize)
108  // BCB2006 workaround: can't use BLOCKSIZE here
109  CRYPTOPP_COMPILE_ASSERT((T_BlockSize & (T_BlockSize - 1)) == 0); // blockSize is a power of 2
110 
111  virtual ~IteratedHash() {}
112 
116  unsigned int BlockSize() const {return T_BlockSize;}
117 
122  ByteOrder GetByteOrder() const {return T_Endianness::ToEnum();}
123 
129  inline void CorrectEndianess(HashWordType *out, const HashWordType *in, size_t byteCount)
130  {
131  ConditionalByteReverse(T_Endianness::ToEnum(), out, in, byteCount);
132  }
133 
134 protected:
135  T_HashWordType* DataBuf() {return this->m_data;}
136  FixedSizeSecBlock<T_HashWordType, T_BlockSize/sizeof(T_HashWordType)> m_data;
137 };
138 
149 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>
151  : public ClonableImpl<T_Transform, AlgorithmImpl<IteratedHash<T_HashWordType, T_Endianness, T_BlockSize>, T_Transform> >
152 {
153 public:
154  CRYPTOPP_CONSTANT(DIGESTSIZE = T_DigestSize ? T_DigestSize : T_StateSize)
155 
157 
161  unsigned int DigestSize() const {return DIGESTSIZE;};
162 
163 protected:
165  void HashEndianCorrectedBlock(const T_HashWordType *data) {T_Transform::Transform(this->m_state, data);}
166  void Init() {T_Transform::InitState(this->m_state);}
167 
168  T_HashWordType* StateBuf() {return this->m_state;}
169  FixedSizeAlignedSecBlock<T_HashWordType, T_BlockSize/sizeof(T_HashWordType), T_StateAligned> m_state;
170 };
171 
172 #ifndef __GNUC__
175 
178 #endif
179 
181 
182 #endif
T_HashWordType * StateBuf()
Definition: iterhash.h:168
ByteOrder GetByteOrder() const
Provides the byte order of the hash.
Definition: iterhash.h:122
uint8_t byte
Definition: Common.h:57
IteratedHashBase()
Construct an IteratedHashBase.
Definition: iterhash.h:33
Classes providing basic library services.
Utility functions for the Crypto++ library.
ByteOrder
Provides the byte ordering.
Definition: cryptlib.h:124
void HashBlock(const HashWordType *input)
Definition: iterhash.h:83
Base class for identifying alogorithm.
Definition: simple.h:26
#define T(i, x)
Iterated hash with a static transformation function.
Definition: iterhash.h:150
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
#define CRYPTOPP_DLL_TEMPLATE_CLASS
Definition: config.h:720
Abstract base classes that provide a uniform interface to this library.
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: iterhash.h:161
Exception thrown when trying to hash more data than is allowed by a hash function.
Definition: iterhash.h:13
Classes and functions for secure memory allocations.
#define CRYPTOPP_COMPILE_ASSERT(assertion)
Definition: misc.h:139
T GetBitCountLo() const
Definition: iterhash.h:75
T ConditionalByteReverse(ByteOrder order, T value)
Reverses bytes in a value depending upon endianness.
Definition: misc.h:1807
T GetBitCountHi() const
Definition: iterhash.h:74
HashInputTooLong(const std::string &alg)
Definition: iterhash.h:16
void HashEndianCorrectedBlock(const T_HashWordType *data)
Definition: iterhash.h:165
T_HashWordType HashWordType
Definition: iterhash.h:105
#define CRYPTOPP_STATIC_TEMPLATE_CLASS
Definition: config.h:734
Fixed size stack-based SecBlock with 16-byte alignment.
Definition: secblock.h:766
void CorrectEndianess(HashWordType *out, const HashWordType *in, size_t byteCount)
Adjusts the byte ordering of the hash.
Definition: iterhash.h:129
unsigned int OptimalBlockSize() const
Provides the input block size most efficient for this cipher.
Definition: iterhash.h:40
#define CRYPTOPP_CONSTANT(x)
Definition: config.h:540
Fixed size stack-based SecBlock.
Definition: secblock.h:753
unsigned int OptimalDataAlignment() const
Provides input and output data alignment for optimal performance.
Definition: iterhash.h:45
#define CRYPTOPP_NO_VTABLE
Definition: config.h:369
uint8_t const size_t const size
Definition: sha3.h:20
unsigned int BlockSize() const
Provides the block size of the hash.
Definition: iterhash.h:116
Iterated hash base class.
Definition: iterhash.h:27
Iterated hash base class.
Definition: iterhash.h:101
#define NAMESPACE_END
Definition: config.h:201
#define CRYPTOPP_DLL
Definition: config.h:704
Input data was received that did not conform to expected format.
Definition: cryptlib.h:191
uint8_t const * data
Definition: sha3.h:19
T_Endianness ByteOrderClass
Definition: iterhash.h:104
T_HashWordType * DataBuf()
Definition: iterhash.h:135