lsh.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // lsh.h - written and placed in the public domain by Jeffrey Walton
  2. // Based on the specification and source code provided by
  3. // Korea Internet & Security Agency (KISA) website. Also
  4. // see https://seed.kisa.or.kr/kisa/algorithm/EgovLSHInfo.do
  5. // and https://seed.kisa.or.kr/kisa/Board/22/detailView.do.
  6. // We are hitting some sort of GCC bug in the LSH AVX2 code path.
  7. // Clang is OK on the AVX2 code path. We believe it is GCC Issue
  8. // 82735, https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82735. It
  9. // makes using zeroupper a little tricky.
  10. /// \file lsh.h
  11. /// \brief Classes for the LSH hash functions
  12. /// \since Crypto++ 8.6
  13. /// \sa <A HREF="https://seed.kisa.or.kr/kisa/algorithm/EgovLSHInfo.do">LSH</A>
  14. /// on the Korea Internet & Security Agency (KISA) website.
  15. #ifndef CRYPTOPP_LSH_H
  16. #define CRYPTOPP_LSH_H
  17. #include "cryptlib.h"
  18. #include "secblock.h"
  19. // Enable SSE2 and AVX2 for 64-bit machines.
  20. // 32-bit machines slow down with SSE2.
  21. #if (CRYPTOPP_BOOL_X32) || (CRYPTOPP_BOOL_X64)
  22. # define CRYPTOPP_ENABLE_64BIT_SSE 1
  23. #endif
  24. NAMESPACE_BEGIN(CryptoPP)
  25. /// \brief LSH-224 and LSH-256 hash base class
  26. /// \details LSH256_Base is the base class for both LSH-224 and LSH-256
  27. /// \since Crypto++ 8.6
  28. class LSH256_Base : public HashTransformation
  29. {
  30. public:
  31. /// \brief Block size, in bytes
  32. /// \details LSH_256 uses LSH256_MSG_BLK_BYTE_LEN for block size, which is 128
  33. CRYPTOPP_CONSTANT(BLOCKSIZE = 128);
  34. virtual ~LSH256_Base() {}
  35. unsigned int BlockSize() const { return BLOCKSIZE; }
  36. unsigned int DigestSize() const { return m_digestSize; }
  37. unsigned int OptimalDataAlignment() const { return GetAlignmentOf<word32>(); }
  38. void Restart();
  39. void Update(const byte *input, size_t size);
  40. void TruncatedFinal(byte *hash, size_t size);
  41. std::string AlgorithmProvider() const;
  42. protected:
  43. LSH256_Base(unsigned int algType, unsigned int digestSize)
  44. : m_digestSize(digestSize) { m_state[80] = algType; }
  45. protected:
  46. // Working state is:
  47. // * cv_l = 8 32-bit words
  48. // * cv_r = 8 32-bit words
  49. // * submsg_e_l = 8 32-bit words
  50. // * submsg_e_r = 8 32-bit words
  51. // * submsg_o_l = 8 32-bit words
  52. // * submsg_o_r = 8 32-bit words
  53. // * last_block = 32 32-bit words (128 bytes)
  54. // * algType
  55. // * remainingBitLength
  56. FixedSizeSecBlock<word32, 80+2> m_state;
  57. // word32 m_algType, m_remainingBitLength;
  58. word32 m_digestSize;
  59. };
  60. /// \brief LSH-224 hash function
  61. /// \sa <A HREF="https://seed.kisa.or.kr/kisa/algorithm/EgovLSHInfo.do">LSH</A>
  62. /// on the Korea Internet & Security Agency (KISA) website.
  63. /// \since Crypto++ 8.6
  64. class LSH224 : public LSH256_Base
  65. {
  66. public:
  67. /// \brief Digest size, in bytes
  68. /// \details LSH_256 uses LSH_GET_HASHBYTE(algType) for digest size, which is 28
  69. CRYPTOPP_CONSTANT(DIGESTSIZE = 28);
  70. /// \brief Block size, in bytes
  71. /// \details LSH_256 uses LSH256_MSG_BLK_BYTE_LEN for block size, which is 128
  72. CRYPTOPP_CONSTANT(BLOCKSIZE = LSH256_Base::BLOCKSIZE);
  73. /// \brief The algorithm's name
  74. /// \return the standard algorithm name
  75. /// \details The standard algorithm name can be a name like <tt>AES</tt> or <tt>AES/GCM</tt>.
  76. /// Some algorithms do not have standard names yet. For example, there is no standard
  77. /// algorithm name for Shoup's ECIES.
  78. /// \note StaticAlgorithmName is not universally implemented yet.
  79. static std::string StaticAlgorithmName() { return "LSH-224"; }
  80. /// \brief Construct a LSH-224
  81. /// \details LSH_TYPE_224 is the magic value 0x000001C defined in lsh.cpp.
  82. LSH224() : LSH256_Base(0x000001C, DIGESTSIZE) { Restart(); }
  83. std::string AlgorithmName() const { return StaticAlgorithmName(); }
  84. };
  85. /// \brief LSH-256 hash function
  86. /// \sa <A HREF="https://seed.kisa.or.kr/kisa/algorithm/EgovLSHInfo.do">LSH</A>
  87. /// on the Korea Internet & Security Agency (KISA) website.
  88. /// \since Crypto++ 8.6
  89. class LSH256 : public LSH256_Base
  90. {
  91. public:
  92. /// \brief Digest size, in bytes
  93. /// \details LSH_256 uses LSH_GET_HASHBYTE(algType) for digest size, which is 32
  94. CRYPTOPP_CONSTANT(DIGESTSIZE = 32);
  95. /// \brief Block size, in bytes
  96. /// \details LSH_256 uses LSH256_MSG_BLK_BYTE_LEN for block size, which is 128
  97. CRYPTOPP_CONSTANT(BLOCKSIZE = LSH256_Base::BLOCKSIZE);
  98. /// \brief The algorithm's name
  99. /// \return the standard algorithm name
  100. /// \details The standard algorithm name can be a name like <tt>AES</tt> or <tt>AES/GCM</tt>.
  101. /// Some algorithms do not have standard names yet. For example, there is no standard
  102. /// algorithm name for Shoup's ECIES.
  103. /// \note StaticAlgorithmName is not universally implemented yet.
  104. static std::string StaticAlgorithmName() { return "LSH-256"; }
  105. /// \brief Construct a LSH-256
  106. /// \details LSH_TYPE_256 is the magic value 0x0000020 defined in lsh.cpp.
  107. LSH256() : LSH256_Base(0x0000020, DIGESTSIZE) { Restart(); }
  108. std::string AlgorithmName() const { return StaticAlgorithmName(); }
  109. };
  110. /// \brief LSH-384 and LSH-512 hash base class
  111. /// \details LSH512_Base is the base class for both LSH-384 and LSH-512
  112. /// \since Crypto++ 8.6
  113. class LSH512_Base : public HashTransformation
  114. {
  115. public:
  116. /// \brief Block size, in bytes
  117. /// \details LSH_512 uses LSH512_MSG_BLK_BYTE_LEN for block size, which is 256
  118. CRYPTOPP_CONSTANT(BLOCKSIZE = 256);
  119. virtual ~LSH512_Base() {}
  120. unsigned int BlockSize() const { return BLOCKSIZE; }
  121. unsigned int DigestSize() const { return m_digestSize; }
  122. unsigned int OptimalDataAlignment() const { return GetAlignmentOf<word64>(); }
  123. void Restart();
  124. void Update(const byte *input, size_t size);
  125. void TruncatedFinal(byte *hash, size_t size);
  126. std::string AlgorithmProvider() const;
  127. protected:
  128. LSH512_Base(unsigned int algType, unsigned int digestSize)
  129. : m_digestSize(digestSize) { m_state[80] = algType; }
  130. protected:
  131. // Working state is:
  132. // * cv_l = 8 64-bit words
  133. // * cv_r = 8 64-bit words
  134. // * submsg_e_l = 8 64-bit words
  135. // * submsg_e_r = 8 64-bit words
  136. // * submsg_o_l = 8 64-bit words
  137. // * submsg_o_r = 8 64-bit words
  138. // * last_block = 32 64-bit words (256 bytes)
  139. // * algType
  140. // * remainingBitLength
  141. FixedSizeSecBlock<word64, 80+2> m_state;
  142. // word32 m_algType, m_remainingBitLength;
  143. word32 m_digestSize;
  144. };
  145. /// \brief LSH-384 hash function
  146. /// \sa <A HREF="https://seed.kisa.or.kr/kisa/algorithm/EgovLSHInfo.do">LSH</A>
  147. /// on the Korea Internet & Security Agency (KISA) website.
  148. /// \since Crypto++ 8.6
  149. class LSH384 : public LSH512_Base
  150. {
  151. public:
  152. /// \brief Digest size, in bytes
  153. /// \details LSH_512 uses LSH_GET_HASHBYTE(algType) for digest size, which is 48
  154. CRYPTOPP_CONSTANT(DIGESTSIZE = 48);
  155. /// \brief Block size, in bytes
  156. /// \details LSH_512 uses LSH512_MSG_BLK_BYTE_LEN for block size, which is 256
  157. CRYPTOPP_CONSTANT(BLOCKSIZE = LSH512_Base::BLOCKSIZE);
  158. /// \brief The algorithm's name
  159. /// \return the standard algorithm name
  160. /// \details The standard algorithm name can be a name like <tt>AES</tt> or <tt>AES/GCM</tt>.
  161. /// Some algorithms do not have standard names yet. For example, there is no standard
  162. /// algorithm name for Shoup's ECIES.
  163. /// \note StaticAlgorithmName is not universally implemented yet.
  164. static std::string StaticAlgorithmName() { return "LSH-384"; }
  165. /// \brief Construct a LSH-384
  166. /// \details LSH_TYPE_384 is the magic value 0x0010030 defined in lsh.cpp.
  167. LSH384() : LSH512_Base(0x0010030, DIGESTSIZE) { Restart(); }
  168. std::string AlgorithmName() const { return StaticAlgorithmName(); }
  169. };
  170. /// \brief LSH-512 hash function
  171. /// \sa <A HREF="https://seed.kisa.or.kr/kisa/algorithm/EgovLSHInfo.do">LSH</A>
  172. /// on the Korea Internet & Security Agency (KISA) website.
  173. /// \since Crypto++ 8.6
  174. class LSH512 : public LSH512_Base
  175. {
  176. public:
  177. /// \brief Digest size, in bytes
  178. /// \details LSH_512 uses LSH_GET_HASHBYTE(algType) for digest size, which is 64
  179. CRYPTOPP_CONSTANT(DIGESTSIZE = 64);
  180. /// \brief Block size, in bytes
  181. /// \details LSH_512 uses LSH512_MSG_BLK_BYTE_LEN for block size, which is 256
  182. CRYPTOPP_CONSTANT(BLOCKSIZE = LSH512_Base::BLOCKSIZE);
  183. /// \brief The algorithm's name
  184. /// \return the standard algorithm name
  185. /// \details The standard algorithm name can be a name like <tt>AES</tt> or <tt>AES/GCM</tt>.
  186. /// Some algorithms do not have standard names yet. For example, there is no standard
  187. /// algorithm name for Shoup's ECIES.
  188. /// \note StaticAlgorithmName is not universally implemented yet.
  189. static std::string StaticAlgorithmName() { return "LSH-512"; }
  190. /// \brief Construct a LSH-512
  191. /// \details LSH_TYPE_512 is the magic value 0x0010040 defined in lsh.cpp.
  192. LSH512() : LSH512_Base(0x0010040, DIGESTSIZE) { Restart(); }
  193. std::string AlgorithmName() const { return StaticAlgorithmName(); }
  194. };
  195. /// \brief LSH-512-256 hash function
  196. /// \sa <A HREF="https://seed.kisa.or.kr/kisa/algorithm/EgovLSHInfo.do">LSH</A>
  197. /// on the Korea Internet & Security Agency (KISA) website.
  198. /// \since Crypto++ 8.6
  199. class LSH512_256 : public LSH512_Base
  200. {
  201. public:
  202. /// \brief Digest size, in bytes
  203. /// \details LSH_512 uses LSH_GET_HASHBYTE(algType) for digest size, which is 32
  204. CRYPTOPP_CONSTANT(DIGESTSIZE = 32);
  205. /// \brief Block size, in bytes
  206. /// \details LSH_512 uses LSH512_MSG_BLK_BYTE_LEN for block size, which is 256
  207. CRYPTOPP_CONSTANT(BLOCKSIZE = LSH512_Base::BLOCKSIZE);
  208. /// \brief The algorithm's name
  209. /// \return the standard algorithm name
  210. /// \details The standard algorithm name can be a name like <tt>AES</tt> or <tt>AES/GCM</tt>.
  211. /// Some algorithms do not have standard names yet. For example, there is no standard
  212. /// algorithm name for Shoup's ECIES.
  213. /// \note StaticAlgorithmName is not universally implemented yet.
  214. static std::string StaticAlgorithmName() { return "LSH-512-256"; }
  215. /// \brief Construct a LSH-512-256
  216. /// \details LSH_TYPE_512_256 is the magic value 0x0010020 defined in lsh.cpp.
  217. LSH512_256() : LSH512_Base(0x0010020, DIGESTSIZE) { Restart(); }
  218. std::string AlgorithmName() const { return StaticAlgorithmName(); }
  219. };
  220. NAMESPACE_END
  221. #endif // CRYPTOPP_LSH_H