sha3.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // sha3.h - originally written and placed in the public domain by Wei Dai
  2. /// \file sha3.h
  3. /// \brief Classes for SHA3 message digests
  4. /// \details The Crypto++ implementation conforms to the FIPS 202 version of SHA3 using F1600 with XOF d=0x06.
  5. /// Previous behavior (XOF d=0x01) is available in Keccak classes.
  6. /// \sa <a href="http://en.wikipedia.org/wiki/SHA-3">SHA-3</a>,
  7. /// <A HREF="http://csrc.nist.gov/groups/ST/hash/sha-3/fips202_standard_2015.html">SHA-3 STANDARD (FIPS 202)</A>.
  8. /// \since Crypto++ 5.6.2
  9. #ifndef CRYPTOPP_SHA3_H
  10. #define CRYPTOPP_SHA3_H
  11. #include "cryptlib.h"
  12. #include "secblock.h"
  13. #include "misc.h"
  14. NAMESPACE_BEGIN(CryptoPP)
  15. /// \brief SHA3 message digest base class
  16. /// \details The Crypto++ implementation conforms to FIPS 202 version of SHA3 using F1600 with XOF d=0x06.
  17. /// Previous behavior (XOF d=0x01) is available in Keccak classes.
  18. /// \details SHA3 is the base class for SHA3_224, SHA3_256, SHA3_384 and SHA3_512.
  19. /// Library users should instantiate a derived class, and only use SHA3
  20. /// as a base class reference or pointer.
  21. /// \sa Keccak, SHA3_224, SHA3_256, SHA3_384 and SHA3_512.
  22. /// \since Crypto++ 5.6.2
  23. class SHA3 : public HashTransformation
  24. {
  25. protected:
  26. /// \brief Construct a SHA3
  27. /// \param digestSize the digest size, in bytes
  28. /// \details SHA3 is the base class for SHA3_224, SHA3_256, SHA3_384 and SHA3_512.
  29. /// Library users should instantiate a derived class, and only use SHA3
  30. /// as a base class reference or pointer.
  31. /// \details This constructor was moved to protected at Crypto++ 8.1
  32. /// because users were attempting to create Keccak objects with it.
  33. /// \since Crypto++ 5.6.2
  34. SHA3(unsigned int digestSize) : m_digestSize(digestSize) {Restart();}
  35. public:
  36. unsigned int DigestSize() const {return m_digestSize;}
  37. unsigned int OptimalDataAlignment() const {return GetAlignmentOf<word64>();}
  38. void Update(const byte *input, size_t length);
  39. void Restart();
  40. void TruncatedFinal(byte *hash, size_t size);
  41. protected:
  42. inline unsigned int r() const {return BlockSize();}
  43. FixedSizeSecBlock<word64, 25> m_state;
  44. unsigned int m_digestSize, m_counter;
  45. };
  46. /// \brief SHA3 message digest template
  47. /// \tparam T_DigestSize the size of the digest, in bytes
  48. /// \since Crypto++ 5.6.2
  49. template<unsigned int T_DigestSize>
  50. class SHA3_Final : public SHA3
  51. {
  52. public:
  53. CRYPTOPP_CONSTANT(DIGESTSIZE = T_DigestSize);
  54. CRYPTOPP_CONSTANT(BLOCKSIZE = 200 - 2 * DIGESTSIZE);
  55. static std::string StaticAlgorithmName()
  56. { return "SHA3-" + IntToString(DIGESTSIZE * 8); }
  57. /// \brief Construct a SHA3-X message digest
  58. SHA3_Final() : SHA3(DIGESTSIZE) {}
  59. /// \brief Provides the block size of the compression function
  60. /// \return block size of the compression function, in bytes
  61. /// \details BlockSize() will return 0 if the hash is not block based
  62. /// or does not have an equivalent block size. For example, Keccak
  63. /// and SHA-3 do not have a block size, but they do have an equivalent
  64. /// block size called rate expressed as <tt>r</tt>.
  65. unsigned int BlockSize() const { return BLOCKSIZE; }
  66. std::string AlgorithmName() const { return StaticAlgorithmName(); }
  67. private:
  68. #if !defined(__BORLANDC__)
  69. // ensure there was no underflow in the math
  70. CRYPTOPP_COMPILE_ASSERT(BLOCKSIZE < 200);
  71. #endif
  72. };
  73. /// \brief SHA3-224 message digest
  74. /// \since Crypto++ 5.6.2
  75. class SHA3_224 : public SHA3_Final<28> {};
  76. /// \brief SHA3-256 message digest
  77. /// \since Crypto++ 5.6.2
  78. class SHA3_256 : public SHA3_Final<32> {};
  79. /// \brief SHA3-384 message digest
  80. /// \since Crypto++ 5.6.2
  81. class SHA3_384 : public SHA3_Final<48> {};
  82. /// \brief SHA3-512 message digest
  83. /// \since Crypto++ 5.6.2
  84. class SHA3_512 : public SHA3_Final<64> {};
  85. NAMESPACE_END
  86. #endif