sha.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // sha.h - originally written and placed in the public domain by Wei Dai
  2. /// \file sha.h
  3. /// \brief Classes for SHA-1 and SHA-2 family of message digests
  4. /// \since SHA1 since Crypto++ 1.0, SHA2 since Crypto++ 4.0, ARMv8 SHA since
  5. /// Crypto++ 6.0, Intel SHA since Crypto++ 6.0, Power8 SHA since Crypto++ 6.1
  6. #ifndef CRYPTOPP_SHA_H
  7. #define CRYPTOPP_SHA_H
  8. #include "config.h"
  9. #include "iterhash.h"
  10. // Clang 3.3 integrated assembler crash on Linux. Clang 3.4 due to compiler
  11. // error with .intel_syntax, http://llvm.org/bugs/show_bug.cgi?id=24232
  12. #if CRYPTOPP_BOOL_X32 || defined(CRYPTOPP_DISABLE_MIXED_ASM)
  13. # define CRYPTOPP_DISABLE_SHA_ASM 1
  14. #endif
  15. NAMESPACE_BEGIN(CryptoPP)
  16. /// \brief SHA-1 message digest
  17. /// \sa <a href="http://www.weidai.com/scan-mirror/md.html#SHA-1">SHA-1</a>
  18. /// \since SHA1 since Crypto++ 1.0, SHA2 since Crypto++ 4.0, ARMv8 SHA since
  19. /// Crypto++ 6.0, Intel SHA since Crypto++ 6.0
  20. class CRYPTOPP_DLL SHA1 : public IteratedHashWithStaticTransform<word32, BigEndian, 64, 20, SHA1>
  21. {
  22. public:
  23. /// \brief Initialize state array
  24. /// \param state the state of the hash
  25. /// \details InitState sets a state array to SHA1 initial values
  26. /// \details Hashes which derive from IteratedHashWithStaticTransform provide static
  27. /// member functions InitState and Transform. External classes, like SEAL and MDC,
  28. /// can initialize state with a user provided key and operate the hash on the data
  29. /// with the user supplied state.
  30. /// \note On Intel platforms the state array must be 16-byte aligned for SSE2.
  31. static void CRYPTOPP_API InitState(HashWordType *state);
  32. /// \brief Operate the hash
  33. /// \param digest the state of the hash
  34. /// \param data the data to be digested
  35. /// \details Transform operates the hash on <tt>data</tt>. When the call is invoked
  36. /// <tt>digest</tt> holds initial state. Upon return <tt>digest</tt> holds the hash
  37. /// or updated state.
  38. /// \details Hashes which derive from IteratedHashWithStaticTransform provide static
  39. /// member functions InitState and Transform. External classes, like SEAL and MDC,
  40. /// can initialize state with a user provided key and operate the hash on the data
  41. /// with the user supplied state.
  42. /// \note On Intel platforms the state array and data must be 16-byte aligned for SSE2.
  43. static void CRYPTOPP_API Transform(HashWordType *digest, const HashWordType *data);
  44. /// \brief The algorithm name
  45. /// \return C-style string "SHA-1"
  46. CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "SHA-1";}
  47. // Algorithm class
  48. std::string AlgorithmProvider() const;
  49. protected:
  50. size_t HashMultipleBlocks(const HashWordType *input, size_t length);
  51. };
  52. /// \brief SHA-256 message digest
  53. /// \sa <a href="http://www.weidai.com/scan-mirror/md.html#SHA-256">SHA-256</a>
  54. /// \since SHA2 since Crypto++ 4.0, ARMv8 SHA since Crypto++ 6.0,
  55. /// Intel SHA since Crypto++ 6.0, Power8 SHA since Crypto++ 6.1
  56. class CRYPTOPP_DLL SHA256 : public IteratedHashWithStaticTransform<word32, BigEndian, 64, 32, SHA256, 32, true>
  57. {
  58. public:
  59. /// \brief Initialize state array
  60. /// \param state the state of the hash
  61. /// \details InitState sets a state array to SHA256 initial values
  62. /// \details Hashes which derive from IteratedHashWithStaticTransform provide static
  63. /// member functions InitState and Transform. External classes, like SEAL and MDC,
  64. /// can initialize state with a user provided key and operate the hash on the data
  65. /// with the user supplied state.
  66. /// \note On Intel platforms the state array must be 16-byte aligned for SSE2.
  67. static void CRYPTOPP_API InitState(HashWordType *state);
  68. /// \brief Operate the hash
  69. /// \param digest the state of the hash
  70. /// \param data the data to be digested
  71. /// \details Transform operates the hash on <tt>data</tt>. When the call is invoked
  72. /// <tt>digest</tt> holds initial state. Upon return <tt>digest</tt> holds the hash
  73. /// or updated state.
  74. /// \details Hashes which derive from IteratedHashWithStaticTransform provide static
  75. /// member functions InitState and Transform. External classes, like SEAL and MDC,
  76. /// can initialize state with a user provided key and operate the hash on the data
  77. /// with the user supplied state.
  78. /// \note On Intel platforms the state array and data must be 16-byte aligned for SSE2.
  79. static void CRYPTOPP_API Transform(HashWordType *digest, const HashWordType *data);
  80. /// \brief The algorithm name
  81. /// \return C-style string "SHA-256"
  82. CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "SHA-256";}
  83. // Algorithm class
  84. std::string AlgorithmProvider() const;
  85. protected:
  86. size_t HashMultipleBlocks(const HashWordType *input, size_t length);
  87. };
  88. /// \brief SHA-224 message digest
  89. /// \sa <a href="http://www.weidai.com/scan-mirror/md.html#SHA-224">SHA-224</a>
  90. /// \since SHA2 since Crypto++ 4.0, ARMv8 SHA since Crypto++ 6.0,
  91. /// Intel SHA since Crypto++ 6.0, Power8 SHA since Crypto++ 6.1
  92. class CRYPTOPP_DLL SHA224 : public IteratedHashWithStaticTransform<word32, BigEndian, 64, 32, SHA224, 28, true>
  93. {
  94. public:
  95. /// \brief Initialize state array
  96. /// \param state the state of the hash
  97. /// \details InitState sets a state array to SHA224 initial values
  98. /// \details Hashes which derive from IteratedHashWithStaticTransform provide static
  99. /// member functions InitState and Transform. External classes, like SEAL and MDC,
  100. /// can initialize state with a user provided key and operate the hash on the data
  101. /// with the user supplied state.
  102. /// \note On Intel platforms the state array must be 16-byte aligned for SSE2.
  103. static void CRYPTOPP_API InitState(HashWordType *state);
  104. /// \brief Operate the hash
  105. /// \param digest the state of the hash
  106. /// \param data the data to be digested
  107. /// \details Transform operates the hash on <tt>data</tt>. When the call is invoked
  108. /// <tt>digest</tt> holds initial state. Upon return <tt>digest</tt> holds the hash
  109. /// or updated state.
  110. /// \details Hashes which derive from IteratedHashWithStaticTransform provide static
  111. /// member functions InitState and Transform. External classes, like SEAL and MDC,
  112. /// can initialize state with a user provided key and operate the hash on the data
  113. /// with the user supplied state.
  114. /// \note On Intel platforms the state array and data must be 16-byte aligned for SSE2.
  115. static void CRYPTOPP_API Transform(HashWordType *digest, const HashWordType *data) {SHA256::Transform(digest, data);}
  116. /// \brief The algorithm name
  117. /// \return C-style string "SHA-224"
  118. CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "SHA-224";}
  119. // Algorithm class
  120. std::string AlgorithmProvider() const;
  121. protected:
  122. size_t HashMultipleBlocks(const HashWordType *input, size_t length);
  123. };
  124. /// \brief SHA-512 message digest
  125. /// \sa <a href="http://www.weidai.com/scan-mirror/md.html#SHA-512">SHA-512</a>
  126. /// \since SHA2 since Crypto++ 4.0, Power8 SHA since Crypto++ 6.1
  127. class CRYPTOPP_DLL SHA512 : public IteratedHashWithStaticTransform<word64, BigEndian, 128, 64, SHA512, 64, true>
  128. {
  129. public:
  130. /// \brief Initialize state array
  131. /// \param state the state of the hash
  132. /// \details InitState sets a state array to SHA512 initial values
  133. /// \details Hashes which derive from IteratedHashWithStaticTransform provide static
  134. /// member functions InitState and Transform. External classes, like SEAL and MDC,
  135. /// can initialize state with a user provided key and operate the hash on the data
  136. /// with the user supplied state.
  137. /// \note On Intel platforms the state array must be 16-byte aligned for SSE2.
  138. static void CRYPTOPP_API InitState(HashWordType *state);
  139. /// \brief Operate the hash
  140. /// \param digest the state of the hash
  141. /// \param data the data to be digested
  142. /// \details Transform operates the hash on <tt>data</tt>. When the call is invoked
  143. /// <tt>digest</tt> holds initial state. Upon return <tt>digest</tt> holds the hash
  144. /// or updated state.
  145. /// \details Hashes which derive from IteratedHashWithStaticTransform provide static
  146. /// member functions InitState and Transform. External classes, like SEAL and MDC,
  147. /// can initialize state with a user provided key and operate the hash on the data
  148. /// with the user supplied state.
  149. /// \note On Intel platforms the state array and data must be 16-byte aligned for SSE2.
  150. static void CRYPTOPP_API Transform(HashWordType *digest, const HashWordType *data);
  151. /// \brief The algorithm name
  152. /// \return C-style string "SHA-512"
  153. CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "SHA-512";}
  154. // Algorithm class
  155. std::string AlgorithmProvider() const;
  156. };
  157. /// \brief SHA-384 message digest
  158. /// \sa <a href="http://www.weidai.com/scan-mirror/md.html#SHA-384">SHA-384</a>
  159. /// \since SHA2 since Crypto++ 4.0, Power8 SHA since Crypto++ 6.1
  160. class CRYPTOPP_DLL SHA384 : public IteratedHashWithStaticTransform<word64, BigEndian, 128, 64, SHA384, 48, true>
  161. {
  162. public:
  163. /// \brief Initialize state array
  164. /// \param state the state of the hash
  165. /// \details InitState sets a state array to SHA384 initial values
  166. /// \details Hashes which derive from IteratedHashWithStaticTransform provide static
  167. /// member functions InitState and Transform. External classes, like SEAL and MDC,
  168. /// can initialize state with a user provided key and operate the hash on the data
  169. /// with the user supplied state.
  170. /// \note On Intel platforms the state array must be 16-byte aligned for SSE2.
  171. static void CRYPTOPP_API InitState(HashWordType *state);
  172. /// \brief Operate the hash
  173. /// \param digest the state of the hash
  174. /// \param data the data to be digested
  175. /// \details Transform operates the hash on <tt>data</tt>. When the call is invoked
  176. /// <tt>digest</tt> holds initial state. Upon return <tt>digest</tt> holds the hash
  177. /// or updated state.
  178. /// \details Hashes which derive from IteratedHashWithStaticTransform provide static
  179. /// member functions InitState and Transform. External classes, like SEAL and MDC,
  180. /// can initialize state with a user provided key and operate the hash on the data
  181. /// with the user supplied state.
  182. /// \note On Intel platforms the state array and data must be 16-byte aligned for SSE2.
  183. static void CRYPTOPP_API Transform(HashWordType *digest, const HashWordType *data) {SHA512::Transform(digest, data);}
  184. /// \brief The algorithm name
  185. /// \return C-style string "SHA-384"
  186. CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "SHA-384";}
  187. // Algorithm class
  188. std::string AlgorithmProvider() const;
  189. };
  190. NAMESPACE_END
  191. #endif