keccak.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // keccak.h - originally written and placed in the public domain by Wei Dai
  2. /// \file keccak.h
  3. /// \brief Classes for Keccak message digests
  4. /// \details The Crypto++ Keccak implementation uses F1600 with XOF d=0x01.
  5. /// FIPS 202 conformance (XOF d=0x06) is available in SHA3 classes.
  6. /// \details Keccak will likely change in the future to accommodate extensibility of the
  7. /// round function and the XOF functions.
  8. /// \sa <a href="http://en.wikipedia.org/wiki/Keccak">Keccak</a>
  9. /// \since Crypto++ 5.6.4
  10. #ifndef CRYPTOPP_KECCAK_H
  11. #define CRYPTOPP_KECCAK_H
  12. #include "cryptlib.h"
  13. #include "secblock.h"
  14. NAMESPACE_BEGIN(CryptoPP)
  15. /// \brief Keccak message digest base class
  16. /// \details The Crypto++ Keccak implementation uses F1600 with XOF d=0x01.
  17. /// FIPS 202 conformance (XOF d=0x06) is available in SHA3 classes.
  18. /// \details Keccak is the base class for Keccak_224, Keccak_256, Keccak_384 and Keccak_512.
  19. /// Library users should instantiate a derived class, and only use Keccak
  20. /// as a base class reference or pointer.
  21. /// \details Keccak will likely change in the future to accommodate extensibility of the
  22. /// round function and the XOF functions.
  23. /// \details Perform the following to specify a different digest size. The class will use F1600,
  24. /// XOF d=0x01, and a new value for <tt>r()</tt> (which will be <tt>200-2*24 = 152</tt>).
  25. /// <pre> Keccack_192 : public Keccack
  26. /// {
  27. /// public:
  28. /// CRYPTOPP_CONSTANT(DIGESTSIZE = 24);
  29. /// Keccack_192() : Keccack(DIGESTSIZE) {}
  30. /// };
  31. /// </pre>
  32. ///
  33. /// \sa SHA3, Keccak_224, Keccak_256, Keccak_384 and Keccak_512.
  34. /// \since Crypto++ 5.6.4
  35. class Keccak : public HashTransformation
  36. {
  37. protected:
  38. /// \brief Construct a Keccak
  39. /// \param digestSize the digest size, in bytes
  40. /// \details Keccak is the base class for Keccak_224, Keccak_256, Keccak_384 and Keccak_512.
  41. /// Library users should instantiate a derived class, and only use Keccak
  42. /// as a base class reference or pointer.
  43. /// \details This constructor was moved to protected at Crypto++ 8.1
  44. /// because users were attempting to create Keccak objects with it.
  45. /// \since Crypto++ 5.6.4
  46. Keccak(unsigned int digestSize) : m_digestSize(digestSize) {Restart();}
  47. public:
  48. unsigned int DigestSize() const {return m_digestSize;}
  49. unsigned int OptimalDataAlignment() const {return GetAlignmentOf<word64>();}
  50. void Update(const byte *input, size_t length);
  51. void Restart();
  52. void TruncatedFinal(byte *hash, size_t size);
  53. protected:
  54. inline unsigned int r() const {return BlockSize();}
  55. FixedSizeSecBlock<word64, 25> m_state;
  56. unsigned int m_digestSize, m_counter;
  57. };
  58. /// \brief Keccak message digest template
  59. /// \tparam T_DigestSize the size of the digest, in bytes
  60. /// \since Crypto++ 6.0
  61. template<unsigned int T_DigestSize>
  62. class Keccak_Final : public Keccak
  63. {
  64. public:
  65. CRYPTOPP_CONSTANT(DIGESTSIZE = T_DigestSize);
  66. CRYPTOPP_CONSTANT(BLOCKSIZE = 200 - 2 * DIGESTSIZE);
  67. static std::string StaticAlgorithmName()
  68. { return "Keccak-" + IntToString(DIGESTSIZE * 8); }
  69. /// \brief Construct a Keccak-X message digest
  70. Keccak_Final() : Keccak(DIGESTSIZE) {}
  71. /// \brief Provides the block size of the compression function
  72. /// \return block size of the compression function, in bytes
  73. /// \details BlockSize() will return 0 if the hash is not block based
  74. /// or does not have an equivalent block size. For example, Keccak
  75. /// and SHA-3 do not have a block size, but they do have an equivalent
  76. /// block size called rate expressed as <tt>r</tt>.
  77. unsigned int BlockSize() const { return BLOCKSIZE; }
  78. std::string AlgorithmName() const { return StaticAlgorithmName(); }
  79. private:
  80. #if !defined(__BORLANDC__)
  81. // ensure there was no underflow in the math
  82. CRYPTOPP_COMPILE_ASSERT(BLOCKSIZE < 200);
  83. #endif
  84. };
  85. /// \brief Keccak-224 message digest
  86. /// \since Crypto++ 5.6.4
  87. DOCUMENTED_TYPEDEF(Keccak_Final<28>, Keccak_224);
  88. /// \brief Keccak-256 message digest
  89. /// \since Crypto++ 5.6.4
  90. DOCUMENTED_TYPEDEF(Keccak_Final<32>, Keccak_256);
  91. /// \brief Keccak-384 message digest
  92. /// \since Crypto++ 5.6.4
  93. DOCUMENTED_TYPEDEF(Keccak_Final<48>, Keccak_384);
  94. /// \brief Keccak-512 message digest
  95. /// \since Crypto++ 5.6.4
  96. DOCUMENTED_TYPEDEF(Keccak_Final<64>, Keccak_512);
  97. NAMESPACE_END
  98. #endif