Crypto++  6.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,
6 /// ARM SHA since Crypto++ 6.0, Intel SHA since Crypto++ 6.0
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)
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 Crypto++ 1.0, Intel SHA extensions since Crypto++ 6.0
25 class CRYPTOPP_DLL SHA1 : public IteratedHashWithStaticTransform<word32, BigEndian, 64, 20, SHA1>
26 {
27 public:
28  /// \brief Initialize state array
29  /// \param state the state of the hash
30  /// \details InitState sets a state array to SHA1 initial values
31  /// \details Hashes which derive from IteratedHashWithStaticTransform provide static
32  /// member functions InitState and Transform. External classes, like SEAL and MDC,
33  /// can initialize state with a user provided key and operate the hash on the data
34  /// with the user supplied state.
35  /// \note On Intel platforms the state array must be 16-byte aligned for SSE2.
36  static void CRYPTOPP_API InitState(HashWordType *state);
37  /// \brief Operate the hash
38  /// \param digest the state of the hash
39  /// \param data the data to be digested
40  /// \details Transform operates the hash on <tt>data</tt>. When the call is invoked
41  /// <tt>digest</tt> holds initial state. Upon return <tt>digest</tt> holds the hash
42  /// or updated state.
43  /// \details Hashes which derive from IteratedHashWithStaticTransform provide static
44  /// member functions InitState and Transform. External classes, like SEAL and MDC,
45  /// can initialize state with a user provided key and operate the hash on the data
46  /// with the user supplied state.
47  /// \note On Intel platforms the state array and data must be 16-byte aligned for SSE2.
48  static void CRYPTOPP_API Transform(HashWordType *digest, const HashWordType *data);
49  /// \brief The algorithm name
50  /// \returns C-style string "SHA-1"
51  CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "SHA-1";}
52 
53 protected:
54  size_t HashMultipleBlocks(const HashWordType *input, size_t length);
55 };
56 
57 /// \brief SHA-256 message digest
58 /// \sa <a href="http://www.weidai.com/scan-mirror/md.html#SHA-256">SHA-256</a>
59 /// \since Crypto++ 4.0, Intel SHA extensions since Crypto++ 6.0
60 class CRYPTOPP_DLL SHA256 : public IteratedHashWithStaticTransform<word32, BigEndian, 64, 32, SHA256, 32, true>
61 {
62 public:
63  /// \brief Initialize state array
64  /// \param state the state of the hash
65  /// \details InitState sets a state array to SHA256 initial values
66  /// \details Hashes which derive from IteratedHashWithStaticTransform provide static
67  /// member functions InitState and Transform. External classes, like SEAL and MDC,
68  /// can initialize state with a user provided key and operate the hash on the data
69  /// with the user supplied state.
70  /// \note On Intel platforms the state array must be 16-byte aligned for SSE2.
71  static void CRYPTOPP_API InitState(HashWordType *state);
72  /// \brief Operate the hash
73  /// \param digest the state of the hash
74  /// \param data the data to be digested
75  /// \details Transform operates the hash on <tt>data</tt>. When the call is invoked
76  /// <tt>digest</tt> holds initial state. Upon return <tt>digest</tt> holds the hash
77  /// or updated state.
78  /// \details Hashes which derive from IteratedHashWithStaticTransform provide static
79  /// member functions InitState and Transform. External classes, like SEAL and MDC,
80  /// can initialize state with a user provided key and operate the hash on the data
81  /// with the user supplied state.
82  /// \note On Intel platforms the state array and data must be 16-byte aligned for SSE2.
83  static void CRYPTOPP_API Transform(HashWordType *digest, const HashWordType *data);
84  /// \brief The algorithm name
85  /// \returns C-style string "SHA-256"
86  CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "SHA-256";}
87 
88 protected:
89  size_t HashMultipleBlocks(const HashWordType *input, size_t length);
90 };
91 
92 /// \brief SHA-224 message digest
93 /// \sa <a href="http://www.weidai.com/scan-mirror/md.html#SHA-224">SHA-224</a>
94 /// \since Crypto++ 4.0, Intel SHA extensions since Crypto++ 6.0
95 class CRYPTOPP_DLL SHA224 : public IteratedHashWithStaticTransform<word32, BigEndian, 64, 32, SHA224, 28, true>
96 {
97 public:
98  /// \brief Initialize state array
99  /// \param state the state of the hash
100  /// \details InitState sets a state array to SHA224 initial values
101  /// \details Hashes which derive from IteratedHashWithStaticTransform provide static
102  /// member functions InitState and Transform. External classes, like SEAL and MDC,
103  /// can initialize state with a user provided key and operate the hash on the data
104  /// with the user supplied state.
105  /// \note On Intel platforms the state array must be 16-byte aligned for SSE2.
106  static void CRYPTOPP_API InitState(HashWordType *state);
107  /// \brief Operate the hash
108  /// \param digest the state of the hash
109  /// \param data the data to be digested
110  /// \details Transform operates the hash on <tt>data</tt>. When the call is invoked
111  /// <tt>digest</tt> holds initial state. Upon return <tt>digest</tt> holds the hash
112  /// or updated state.
113  /// \details Hashes which derive from IteratedHashWithStaticTransform provide static
114  /// member functions InitState and Transform. External classes, like SEAL and MDC,
115  /// can initialize state with a user provided key and operate the hash on the data
116  /// with the user supplied state.
117  /// \note On Intel platforms the state array and data must be 16-byte aligned for SSE2.
118  static void CRYPTOPP_API Transform(HashWordType *digest, const HashWordType *data) {SHA256::Transform(digest, data);}
119  /// \brief The algorithm name
120  /// \returns C-style string "SHA-224"
121  CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "SHA-224";}
122 
123 protected:
124  size_t HashMultipleBlocks(const HashWordType *input, size_t length);
125 };
126 
127 /// \brief SHA-512 message digest
128 /// \sa <a href="http://www.weidai.com/scan-mirror/md.html#SHA-512">SHA-512</a>
129 /// \since Crypto++ 4.0
130 class CRYPTOPP_DLL SHA512 : public IteratedHashWithStaticTransform<word64, BigEndian, 128, 64, SHA512, 64, SHA_X86_ALIGN16>
131 {
132 public:
133  /// \brief Initialize state array
134  /// \param state the state of the hash
135  /// \details InitState sets a state array to SHA512 initial values
136  /// \details Hashes which derive from IteratedHashWithStaticTransform provide static
137  /// member functions InitState and Transform. External classes, like SEAL and MDC,
138  /// can initialize state with a user provided key and operate the hash on the data
139  /// with the user supplied state.
140  /// \note On Intel platforms the state array must be 16-byte aligned for SSE2.
141  static void CRYPTOPP_API InitState(HashWordType *state);
142  /// \brief Operate the hash
143  /// \param digest the state of the hash
144  /// \param data the data to be digested
145  /// \details Transform operates the hash on <tt>data</tt>. When the call is invoked
146  /// <tt>digest</tt> holds initial state. Upon return <tt>digest</tt> holds the hash
147  /// or updated state.
148  /// \details Hashes which derive from IteratedHashWithStaticTransform provide static
149  /// member functions InitState and Transform. External classes, like SEAL and MDC,
150  /// can initialize state with a user provided key and operate the hash on the data
151  /// with the user supplied state.
152  /// \note On Intel platforms the state array and data must be 16-byte aligned for SSE2.
153  static void CRYPTOPP_API Transform(HashWordType *digest, const HashWordType *data);
154  /// \brief The algorithm name
155  /// \returns C-style string "SHA-512"
156  CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "SHA-512";}
157 };
158 
159 /// \brief SHA-384 message digest
160 /// \sa <a href="http://www.weidai.com/scan-mirror/md.html#SHA-384">SHA-384</a>
161 /// \since Crypto++ 4.0
162 class CRYPTOPP_DLL SHA384 : public IteratedHashWithStaticTransform<word64, BigEndian, 128, 64, SHA384, 48, SHA_X86_ALIGN16>
163 {
164 public:
165  /// \brief Initialize state array
166  /// \param state the state of the hash
167  /// \details InitState sets a state array to SHA384 initial values
168  /// \details Hashes which derive from IteratedHashWithStaticTransform provide static
169  /// member functions InitState and Transform. External classes, like SEAL and MDC,
170  /// can initialize state with a user provided key and operate the hash on the data
171  /// with the user supplied state.
172  /// \note On Intel platforms the state array must be 16-byte aligned for SSE2.
173  static void CRYPTOPP_API InitState(HashWordType *state);
174  /// \brief Operate the hash
175  /// \param digest the state of the hash
176  /// \param data the data to be digested
177  /// \details Transform operates the hash on <tt>data</tt>. When the call is invoked
178  /// <tt>digest</tt> holds initial state. Upon return <tt>digest</tt> holds the hash
179  /// or updated state.
180  /// \details Hashes which derive from IteratedHashWithStaticTransform provide static
181  /// member functions InitState and Transform. External classes, like SEAL and MDC,
182  /// can initialize state with a user provided key and operate the hash on the data
183  /// with the user supplied state.
184  /// \note On Intel platforms the state array and data must be 16-byte aligned for SSE2.
185  static void CRYPTOPP_API Transform(HashWordType *digest, const HashWordType *data) {SHA512::Transform(digest, data);}
186  /// \brief The algorithm name
187  /// \returns C-style string "SHA-384"
188  CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "SHA-384";}
189 };
190 
191 NAMESPACE_END
192 
193 #endif
SHA-384 message digest.
Definition: sha.h:162
SHA-256 message digest.
Definition: sha.h:60
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:133
Library configuration file.
SHA-512 message digest.
Definition: sha.h:130
SHA-1 message digest.
Definition: sha.h:25
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:95