Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.7 KiB

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