Security Scol plugin
iterhash.h
Go to the documentation of this file.
1// iterhash.h - originally written and placed in the public domain by Wei Dai
2
5
6#ifndef CRYPTOPP_ITERHASH_H
7#define CRYPTOPP_ITERHASH_H
8
9#include "cryptlib.h"
10#include "secblock.h"
11#include "misc.h"
12#include "simple.h"
13
14#if CRYPTOPP_MSC_VERSION
15# pragma warning(push)
16# pragma warning(disable: 4231 4275)
17# if (CRYPTOPP_MSC_VERSION >= 1400)
18# pragma warning(disable: 6011 6386 28193)
19# endif
20#endif
21
22NAMESPACE_BEGIN(CryptoPP)
23
24
25class CRYPTOPP_DLL HashInputTooLong : public InvalidDataFormat
26{
27public:
28 explicit HashInputTooLong(const std::string &alg)
29 : InvalidDataFormat("IteratedHashBase: input data exceeds maximum allowed by hash function " + alg) {}
30};
31
37template <class T, class BASE>
38class CRYPTOPP_NO_VTABLE IteratedHashBase : public BASE
39{
40public:
41 typedef T HashWordType;
42
43 virtual ~IteratedHashBase() {}
44
46 IteratedHashBase() : m_countLo(0), m_countHi(0) {}
47
53 unsigned int OptimalBlockSize() const {return this->BlockSize();}
54
58 unsigned int OptimalDataAlignment() const {return GetAlignmentOf<T>();}
59
63 void Update(const byte *input, size_t length);
64
73 byte * CreateUpdateSpace(size_t &size);
74
77 void Restart();
78
84 void TruncatedFinal(byte *digest, size_t digestSize);
85
94 virtual std::string AlgorithmProvider() const { return "C++"; }
95
96protected:
97 inline T GetBitCountHi() const
98 {return (m_countLo >> (8*sizeof(T)-3)) + (m_countHi << 3);}
99 inline T GetBitCountLo() const
100 {return m_countLo << 3;}
101
102 void PadLastBlock(unsigned int lastBlockSize, byte padFirst=0x80);
103 virtual void Init() =0;
104
105 virtual ByteOrder GetByteOrder() const =0;
106 virtual void HashEndianCorrectedBlock(const HashWordType *data) =0;
107 virtual size_t HashMultipleBlocks(const T *input, size_t length);
108 void HashBlock(const HashWordType *input)
109 {HashMultipleBlocks(input, this->BlockSize());}
110
111 virtual T* DataBuf() =0;
112 virtual T* StateBuf() =0;
113
114private:
115 T m_countLo, m_countHi;
116};
117
125template <class T_HashWordType, class T_Endianness, unsigned int T_BlockSize, class T_Base = HashTransformation>
126class CRYPTOPP_NO_VTABLE IteratedHash : public IteratedHashBase<T_HashWordType, T_Base>
127{
128public:
129 typedef T_Endianness ByteOrderClass;
130 typedef T_HashWordType HashWordType;
131
132 CRYPTOPP_CONSTANT(BLOCKSIZE = T_BlockSize);
133 // BCB2006 workaround: can't use BLOCKSIZE here
134 CRYPTOPP_COMPILE_ASSERT((T_BlockSize & (T_BlockSize - 1)) == 0); // blockSize is a power of 2
135
136 virtual ~IteratedHash() {}
137
141 unsigned int BlockSize() const {return T_BlockSize;}
142
147 ByteOrder GetByteOrder() const {return T_Endianness::ToEnum();}
148
154 inline void CorrectEndianess(HashWordType *out, const HashWordType *in, size_t byteCount)
155 {
156 CRYPTOPP_ASSERT(in != NULLPTR);
157 CRYPTOPP_ASSERT(out != NULLPTR);
158 CRYPTOPP_ASSERT(IsAligned<T_HashWordType>(in));
159 CRYPTOPP_ASSERT(IsAligned<T_HashWordType>(out));
160
161 ConditionalByteReverse(T_Endianness::ToEnum(), out, in, byteCount);
162 }
163
164protected:
165 enum { Blocks = T_BlockSize/sizeof(T_HashWordType) };
166 T_HashWordType* DataBuf() {return this->m_data;}
168};
169
179template <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>
180class CRYPTOPP_NO_VTABLE IteratedHashWithStaticTransform
181 : public ClonableImpl<T_Transform, AlgorithmImpl<IteratedHash<T_HashWordType, T_Endianness, T_BlockSize>, T_Transform> >
182{
183public:
184 CRYPTOPP_CONSTANT(DIGESTSIZE = T_DigestSize ? T_DigestSize : T_StateSize);
185
187
191 unsigned int DigestSize() const {return DIGESTSIZE;}
192
193protected:
194 // https://github.com/weidai11/cryptopp/issues/147#issuecomment-766231864
195 IteratedHashWithStaticTransform() {IteratedHashWithStaticTransform::Init();}
196 void HashEndianCorrectedBlock(const T_HashWordType *data) {T_Transform::Transform(this->m_state, data);}
197 void Init() {T_Transform::InitState(this->m_state);}
198
199 enum { Blocks = T_BlockSize/sizeof(T_HashWordType) };
200 T_HashWordType* StateBuf() {return this->m_state;}
202};
203
204#if !defined(__GNUC__) && !defined(__clang__)
205 CRYPTOPP_DLL_TEMPLATE_CLASS IteratedHashBase<word64, HashTransformation>;
206 CRYPTOPP_STATIC_TEMPLATE_CLASS IteratedHashBase<word64, MessageAuthenticationCode>;
207
208 CRYPTOPP_DLL_TEMPLATE_CLASS IteratedHashBase<word32, HashTransformation>;
209 CRYPTOPP_STATIC_TEMPLATE_CLASS IteratedHashBase<word32, MessageAuthenticationCode>;
210#endif
211
212NAMESPACE_END
213
214#if CRYPTOPP_MSC_VERSION
215# pragma warning(pop)
216#endif
217
218#endif
Base class for identifying algorithm.
Definition simple.h:26
Fixed size stack-based SecBlock with 16-byte alignment.
Definition secblock.h:1259
Fixed size stack-based SecBlock.
Definition secblock.h:1246
Exception thrown when trying to hash more data than is allowed by a hash function.
Definition iterhash.h:26
Input data was received that did not conform to expected format.
Definition cryptlib.h:213
Iterated hash base class.
Definition iterhash.h:39
unsigned int OptimalDataAlignment() const
Provides input and output data alignment for optimal performance.
Definition iterhash.h:58
virtual std::string AlgorithmProvider() const
Retrieve the provider of this algorithm.
Definition iterhash.h:94
IteratedHashBase()
Construct an IteratedHashBase.
Definition iterhash.h:46
unsigned int OptimalBlockSize() const
Provides the input block size most efficient for this cipher.
Definition iterhash.h:53
Iterated hash base class.
Definition iterhash.h:127
ByteOrder GetByteOrder() const
Provides the byte order of the hash.
Definition iterhash.h:147
void CorrectEndianess(HashWordType *out, const HashWordType *in, size_t byteCount)
Adjusts the byte ordering of the hash.
Definition iterhash.h:154
unsigned int BlockSize() const
Provides the block size of the hash.
Definition iterhash.h:141
Iterated hash with a static transformation function.
Definition iterhash.h:182
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition iterhash.h:191
Abstract base classes that provide a uniform interface to this library.
ByteOrder
Provides the byte ordering.
Definition cryptlib.h:143
Utility functions for the Crypto++ library.
T ConditionalByteReverse(ByteOrder order, T value)
Reverses bytes in a value depending upon endianness.
Definition misc.h:2208
Classes and functions for secure memory allocations.
Classes providing basic library services.