shake.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // shake.h - written and placed in the public domain by Jeffrey Walton
  2. /// \file shake.h
  3. /// \brief Classes for SHAKE message digests
  4. /// \details The library provides byte oriented SHAKE128 and SHAKE256 using F1600.
  5. /// FIPS 202 allows nearly unlimited output sizes, but Crypto++ limits the output
  6. /// size to <tt>UINT_MAX</tt> due underlying data types.
  7. /// \sa Keccak, SHA3, SHAKE128, SHAKE256,
  8. /// <a href="https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf">FIPS 202,
  9. /// SHA-3 Standard: Permutation-Based Hash and Extendable-Output Functions</a>
  10. /// \since Crypto++ 8.1
  11. #ifndef CRYPTOPP_SHAKE_H
  12. #define CRYPTOPP_SHAKE_H
  13. #include "cryptlib.h"
  14. #include "secblock.h"
  15. NAMESPACE_BEGIN(CryptoPP)
  16. /// \brief SHAKE message digest base class
  17. /// \details SHAKE is the base class for SHAKE128 and SHAKE258.
  18. /// Library users should instantiate a derived class, and only use SHAKE
  19. /// as a base class reference or pointer.
  20. /// \sa Keccak, SHA3, SHAKE128, SHAKE256,
  21. /// <a href="https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf">FIPS 202,
  22. /// SHA-3 Standard: Permutation-Based Hash and Extendable-Output Functions</a>
  23. /// \since Crypto++ 8.1
  24. class SHAKE : public HashTransformation
  25. {
  26. protected:
  27. /// \brief Construct a SHAKE
  28. /// \param digestSize the digest size, in bytes
  29. /// \details SHAKE is the base class for SHAKE128 and SHAKE256.
  30. /// Library users should instantiate a derived class, and only use SHAKE
  31. /// as a base class reference or pointer.
  32. /// \details This constructor was moved to protected at Crypto++ 8.1
  33. /// because users were attempting to create Keccak objects with it.
  34. /// \since Crypto++ 8.1
  35. SHAKE(unsigned int digestSize) : m_digestSize(digestSize) {Restart();}
  36. public:
  37. unsigned int DigestSize() const {return m_digestSize;}
  38. unsigned int OptimalDataAlignment() const {return GetAlignmentOf<word64>();}
  39. void Update(const byte *input, size_t length);
  40. void Restart();
  41. void TruncatedFinal(byte *hash, size_t size);
  42. protected:
  43. inline unsigned int r() const {return BlockSize();}
  44. // SHAKE-128 and SHAKE-256 effectively allow unlimited
  45. // output length. However, we use an unsigned int so
  46. // we are limited in practice to UINT_MAX.
  47. void ThrowIfInvalidTruncatedSize(size_t size) const;
  48. FixedSizeSecBlock<word64, 25> m_state;
  49. unsigned int m_digestSize, m_counter;
  50. };
  51. /// \brief SHAKE message digest template
  52. /// \tparam T_Strength the strength of the digest
  53. /// \since Crypto++ 8.1
  54. template<unsigned int T_Strength>
  55. class SHAKE_Final : public SHAKE
  56. {
  57. public:
  58. CRYPTOPP_CONSTANT(DIGESTSIZE = (T_Strength == 128 ? 32 : 64));
  59. CRYPTOPP_CONSTANT(BLOCKSIZE = (T_Strength == 128 ? 1344/8 : 1088/8));
  60. static std::string StaticAlgorithmName()
  61. { return "SHAKE-" + IntToString(T_Strength); }
  62. /// \brief Construct a SHAKE-X message digest
  63. /// \details SHAKE128 and SHAKE256 don't need the output size in advance
  64. /// because the output size does not affect the digest. TruncatedFinal
  65. /// produces the correct digest for any output size. However, cSHAKE
  66. /// requires the output size in advance because the algorithm uses
  67. /// output size as a parameter to the hash function.
  68. SHAKE_Final(unsigned int outputSize=DIGESTSIZE) : SHAKE(outputSize) {}
  69. /// \brief Provides the block size of the compression function
  70. /// \return block size of the compression function, in bytes
  71. /// \details BlockSize() will return 0 if the hash is not block based
  72. /// or does not have an equivalent block size. For example, Keccak
  73. /// and SHA-3 do not have a block size, but they do have an equivalent
  74. /// to block size called rate expressed as <tt>r</tt>.
  75. unsigned int BlockSize() const { return BLOCKSIZE; }
  76. std::string AlgorithmName() const { return StaticAlgorithmName(); }
  77. private:
  78. #if !defined(__BORLANDC__)
  79. // ensure there was no underflow in the math
  80. CRYPTOPP_COMPILE_ASSERT(BLOCKSIZE < 200);
  81. #endif
  82. };
  83. /// \brief SHAKE128 message digest
  84. /// \details The library provides byte oriented SHAKE128 using F1600.
  85. /// FIPS 202 allows nearly unlimited output sizes, but Crypto++ limits
  86. /// the output size to <tt>UINT_MAX</tt> due underlying data types.
  87. /// \sa Keccak, SHA3, SHAKE256,
  88. /// <a href="https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf">FIPS 202,
  89. /// SHA-3 Standard: Permutation-Based Hash and Extendable-Output Functions</a>
  90. /// \since Crypto++ 8.1
  91. class SHAKE128 : public SHAKE_Final<128>
  92. {
  93. public:
  94. /// \brief Construct a SHAKE128 message digest
  95. /// \details SHAKE128 and SHAKE256 don't need the output size in advance
  96. /// because the output size does not affect the digest. TruncatedFinal
  97. /// produces the correct digest for any output size. However, cSHAKE
  98. /// requires the output size in advance because the algorithm uses
  99. /// output size as a parameter to the hash function.
  100. /// \since Crypto++ 8.1
  101. SHAKE128() {}
  102. /// \brief Construct a SHAKE128 message digest
  103. /// \details SHAKE128 and SHAKE256 don't need the output size in advance
  104. /// because the output size does not affect the digest. TruncatedFinal
  105. /// produces the correct digest for any output size. However, cSHAKE
  106. /// requires the output size in advance because the algorithm uses
  107. /// output size as a parameter to the hash function.
  108. /// \since Crypto++ 8.1
  109. SHAKE128(unsigned int outputSize) : SHAKE_Final<128>(outputSize) {}
  110. };
  111. /// \brief SHAKE256 message digest
  112. /// \details The library provides byte oriented SHAKE256 using F1600.
  113. /// FIPS 202 allows nearly unlimited output sizes, but Crypto++ limits
  114. /// the output size to <tt>UINT_MAX</tt> due underlying data types.
  115. /// \sa Keccak, SHA3, SHAKE128,
  116. /// <a href="https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf">FIPS 202,
  117. /// SHA-3 Standard: Permutation-Based Hash and Extendable-Output Functions</a>
  118. /// \since Crypto++ 8.1
  119. class SHAKE256 : public SHAKE_Final<256>
  120. {
  121. public:
  122. /// \brief Construct a SHAKE256 message digest
  123. /// \details SHAKE128 and SHAKE256 don't need the output size in advance
  124. /// because the output size does not affect the digest. TruncatedFinal
  125. /// produces the correct digest for any output size. However, cSHAKE
  126. /// requires the output size in advance because the algorithm uses
  127. /// output size as a parameter to the hash function.
  128. /// \since Crypto++ 8.1
  129. SHAKE256() {}
  130. /// \brief Construct a SHAKE256 message digest
  131. /// \details SHAKE128 and SHAKE256 don't need the output size in advance
  132. /// because the output size does not affect the digest. TruncatedFinal
  133. /// produces the correct digest for any output size. However, cSHAKE
  134. /// requires the output size in advance because the algorithm uses
  135. /// output size as a parameter to the hash function.
  136. /// \since Crypto++ 8.1
  137. SHAKE256(unsigned int outputSize) : SHAKE_Final<256>(outputSize) {}
  138. };
  139. NAMESPACE_END
  140. #endif