Warning, file /include/cryptopp/iterhash.h was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001
0002
0003
0004
0005
0006 #ifndef CRYPTOPP_ITERHASH_H
0007 #define CRYPTOPP_ITERHASH_H
0008
0009 #include "cryptlib.h"
0010 #include "secblock.h"
0011 #include "misc.h"
0012 #include "simple.h"
0013
0014 #if CRYPTOPP_MSC_VERSION
0015 # pragma warning(push)
0016 # pragma warning(disable: 4231 4275)
0017 # if (CRYPTOPP_MSC_VERSION >= 1400)
0018 # pragma warning(disable: 6011 6386 28193)
0019 # endif
0020 #endif
0021
0022 NAMESPACE_BEGIN(CryptoPP)
0023
0024
0025 class CRYPTOPP_DLL HashInputTooLong : public InvalidDataFormat
0026 {
0027 public:
0028 explicit HashInputTooLong(const std::string &alg)
0029 : InvalidDataFormat("IteratedHashBase: input data exceeds maximum allowed by hash function " + alg) {}
0030 };
0031
0032
0033
0034
0035
0036
0037 template <class T, class BASE>
0038 class CRYPTOPP_NO_VTABLE IteratedHashBase : public BASE
0039 {
0040 public:
0041 typedef T HashWordType;
0042
0043 virtual ~IteratedHashBase() {}
0044
0045
0046 IteratedHashBase() : m_countLo(0), m_countHi(0) {}
0047
0048
0049
0050
0051
0052
0053 unsigned int OptimalBlockSize() const {return this->BlockSize();}
0054
0055
0056
0057
0058 unsigned int OptimalDataAlignment() const {return GetAlignmentOf<T>();}
0059
0060
0061
0062
0063 void Update(const byte *input, size_t length);
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073 byte * CreateUpdateSpace(size_t &size);
0074
0075
0076
0077 void Restart();
0078
0079
0080
0081
0082
0083
0084 void TruncatedFinal(byte *digest, size_t digestSize);
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094 virtual std::string AlgorithmProvider() const { return "C++"; }
0095
0096 protected:
0097 inline T GetBitCountHi() const
0098 {return (m_countLo >> (8*sizeof(T)-3)) + (m_countHi << 3);}
0099 inline T GetBitCountLo() const
0100 {return m_countLo << 3;}
0101
0102 void PadLastBlock(unsigned int lastBlockSize, byte padFirst=0x80);
0103 virtual void Init() =0;
0104
0105 virtual ByteOrder GetByteOrder() const =0;
0106 virtual void HashEndianCorrectedBlock(const HashWordType *data) =0;
0107 virtual size_t HashMultipleBlocks(const T *input, size_t length);
0108 void HashBlock(const HashWordType *input)
0109 {HashMultipleBlocks(input, this->BlockSize());}
0110
0111 virtual T* DataBuf() =0;
0112 virtual T* StateBuf() =0;
0113
0114 private:
0115 T m_countLo, m_countHi;
0116 };
0117
0118
0119
0120
0121
0122
0123
0124
0125 template <class T_HashWordType, class T_Endianness, unsigned int T_BlockSize, class T_Base = HashTransformation>
0126 class CRYPTOPP_NO_VTABLE IteratedHash : public IteratedHashBase<T_HashWordType, T_Base>
0127 {
0128 public:
0129 typedef T_Endianness ByteOrderClass;
0130 typedef T_HashWordType HashWordType;
0131
0132 CRYPTOPP_CONSTANT(BLOCKSIZE = T_BlockSize);
0133
0134 CRYPTOPP_COMPILE_ASSERT((T_BlockSize & (T_BlockSize - 1)) == 0);
0135
0136 virtual ~IteratedHash() {}
0137
0138
0139
0140
0141 unsigned int BlockSize() const {return T_BlockSize;}
0142
0143
0144
0145
0146
0147 ByteOrder GetByteOrder() const {return T_Endianness::ToEnum();}
0148
0149
0150
0151
0152
0153
0154 inline void CorrectEndianess(HashWordType *out, const HashWordType *in, size_t byteCount)
0155 {
0156 CRYPTOPP_ASSERT(in != NULLPTR);
0157 CRYPTOPP_ASSERT(out != NULLPTR);
0158 CRYPTOPP_ASSERT(IsAligned<T_HashWordType>(in));
0159 CRYPTOPP_ASSERT(IsAligned<T_HashWordType>(out));
0160
0161 ConditionalByteReverse(T_Endianness::ToEnum(), out, in, byteCount);
0162 }
0163
0164 protected:
0165 enum { Blocks = T_BlockSize/sizeof(T_HashWordType) };
0166 T_HashWordType* DataBuf() {return this->m_data;}
0167 FixedSizeSecBlock<T_HashWordType, Blocks> m_data;
0168 };
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179 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>
0180 class CRYPTOPP_NO_VTABLE IteratedHashWithStaticTransform
0181 : public ClonableImpl<T_Transform, AlgorithmImpl<IteratedHash<T_HashWordType, T_Endianness, T_BlockSize>, T_Transform> >
0182 {
0183 public:
0184 CRYPTOPP_CONSTANT(DIGESTSIZE = T_DigestSize ? T_DigestSize : T_StateSize);
0185
0186 virtual ~IteratedHashWithStaticTransform() {}
0187
0188
0189
0190
0191 unsigned int DigestSize() const {return DIGESTSIZE;}
0192
0193 protected:
0194
0195 IteratedHashWithStaticTransform() {IteratedHashWithStaticTransform::Init();}
0196 void HashEndianCorrectedBlock(const T_HashWordType *data) {T_Transform::Transform(this->m_state, data);}
0197 void Init() {T_Transform::InitState(this->m_state);}
0198
0199 enum { Blocks = T_BlockSize/sizeof(T_HashWordType) };
0200 T_HashWordType* StateBuf() {return this->m_state;}
0201 FixedSizeAlignedSecBlock<T_HashWordType, Blocks, T_StateAligned> m_state;
0202 };
0203
0204 #if !defined(__GNUC__) && !defined(__clang__)
0205 CRYPTOPP_DLL_TEMPLATE_CLASS IteratedHashBase<word64, HashTransformation>;
0206 CRYPTOPP_STATIC_TEMPLATE_CLASS IteratedHashBase<word64, MessageAuthenticationCode>;
0207
0208 CRYPTOPP_DLL_TEMPLATE_CLASS IteratedHashBase<word32, HashTransformation>;
0209 CRYPTOPP_STATIC_TEMPLATE_CLASS IteratedHashBase<word32, MessageAuthenticationCode>;
0210 #endif
0211
0212 NAMESPACE_END
0213
0214 #if CRYPTOPP_MSC_VERSION
0215 # pragma warning(pop)
0216 #endif
0217
0218 #endif