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