Crypto++ 8.2 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 // Clang 3.3 integrated assembler crash on Linux. Clang 3.4 due to compiler
15 // error with .intel_syntax, http://llvm.org/bugs/show_bug.cgi?id=24232
16 #if CRYPTOPP_BOOL_X32 || defined(CRYPTOPP_DISABLE_MIXED_ASM)
17 # define CRYPTOPP_DISABLE_SHA_ASM 1
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 /// \return C-style string "SHA-1"
52 CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "SHA-1";}
53 // Algorithm class
54 std::string AlgorithmProvider() const;
55
56 protected:
57 size_t HashMultipleBlocks(const HashWordType *input, size_t length);
58 };
59
60 /// \brief SHA-256 message digest
61 /// \sa <a href="http://www.weidai.com/scan-mirror/md.html#SHA-256">SHA-256</a>
62 /// \since SHA2 since Crypto++ 4.0, ARMv8 SHA since Crypto++ 6.0,
63 /// Intel SHA since Crypto++ 6.0, Power8 SHA since Crypto++ 6.1
64 class CRYPTOPP_DLL SHA256 : public IteratedHashWithStaticTransform<word32, BigEndian, 64, 32, SHA256, 32, true>
65 {
66 public:
67 /// \brief Initialize state array
68 /// \param state the state of the hash
69 /// \details InitState sets a state array to SHA256 initial values
70 /// \details Hashes which derive from IteratedHashWithStaticTransform provide static
71 /// member functions InitState and Transform. External classes, like SEAL and MDC,
72 /// can initialize state with a user provided key and operate the hash on the data
73 /// with the user supplied state.
74 /// \note On Intel platforms the state array must be 16-byte aligned for SSE2.
75 static void CRYPTOPP_API InitState(HashWordType *state);
76 /// \brief Operate the hash
77 /// \param digest the state of the hash
78 /// \param data the data to be digested
79 /// \details Transform operates the hash on <tt>data</tt>. When the call is invoked
80 /// <tt>digest</tt> holds initial state. Upon return <tt>digest</tt> holds the hash
81 /// or updated state.
82 /// \details Hashes which derive from IteratedHashWithStaticTransform provide static
83 /// member functions InitState and Transform. External classes, like SEAL and MDC,
84 /// can initialize state with a user provided key and operate the hash on the data
85 /// with the user supplied state.
86 /// \note On Intel platforms the state array and data must be 16-byte aligned for SSE2.
87 static void CRYPTOPP_API Transform(HashWordType *digest, const HashWordType *data);
88 /// \brief The algorithm name
89 /// \return C-style string "SHA-256"
90 CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "SHA-256";}
91
92 // Algorithm class
93 std::string AlgorithmProvider() const;
94
95 protected:
96 size_t HashMultipleBlocks(const HashWordType *input, size_t length);
97 };
98
99 /// \brief SHA-224 message digest
100 /// \sa <a href="http://www.weidai.com/scan-mirror/md.html#SHA-224">SHA-224</a>
101 /// \since SHA2 since Crypto++ 4.0, ARMv8 SHA since Crypto++ 6.0,
102 /// Intel SHA since Crypto++ 6.0, Power8 SHA since Crypto++ 6.1
103 class CRYPTOPP_DLL SHA224 : public IteratedHashWithStaticTransform<word32, BigEndian, 64, 32, SHA224, 28, true>
104 {
105 public:
106 /// \brief Initialize state array
107 /// \param state the state of the hash
108 /// \details InitState sets a state array to SHA224 initial values
109 /// \details Hashes which derive from IteratedHashWithStaticTransform provide static
110 /// member functions InitState and Transform. External classes, like SEAL and MDC,
111 /// can initialize state with a user provided key and operate the hash on the data
112 /// with the user supplied state.
113 /// \note On Intel platforms the state array must be 16-byte aligned for SSE2.
114 static void CRYPTOPP_API InitState(HashWordType *state);
115 /// \brief Operate the hash
116 /// \param digest the state of the hash
117 /// \param data the data to be digested
118 /// \details Transform operates the hash on <tt>data</tt>. When the call is invoked
119 /// <tt>digest</tt> holds initial state. Upon return <tt>digest</tt> holds the hash
120 /// or updated state.
121 /// \details Hashes which derive from IteratedHashWithStaticTransform provide static
122 /// member functions InitState and Transform. External classes, like SEAL and MDC,
123 /// can initialize state with a user provided key and operate the hash on the data
124 /// with the user supplied state.
125 /// \note On Intel platforms the state array and data must be 16-byte aligned for SSE2.
126 static void CRYPTOPP_API Transform(HashWordType *digest, const HashWordType *data) {SHA256::Transform(digest, data);}
127 /// \brief The algorithm name
128 /// \return C-style string "SHA-224"
129 CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "SHA-224";}
130
131 // Algorithm class
132 std::string AlgorithmProvider() const;
133
134 protected:
135 size_t HashMultipleBlocks(const HashWordType *input, size_t length);
136 };
137
138 /// \brief SHA-512 message digest
139 /// \sa <a href="http://www.weidai.com/scan-mirror/md.html#SHA-512">SHA-512</a>
140 /// \since SHA2 since Crypto++ 4.0, Power8 SHA since Crypto++ 6.1
141 class CRYPTOPP_DLL SHA512 : public IteratedHashWithStaticTransform<word64, BigEndian, 128, 64, SHA512, 64, true>
142 {
143 public:
144 /// \brief Initialize state array
145 /// \param state the state of the hash
146 /// \details InitState sets a state array to SHA512 initial values
147 /// \details Hashes which derive from IteratedHashWithStaticTransform provide static
148 /// member functions InitState and Transform. External classes, like SEAL and MDC,
149 /// can initialize state with a user provided key and operate the hash on the data
150 /// with the user supplied state.
151 /// \note On Intel platforms the state array must be 16-byte aligned for SSE2.
152 static void CRYPTOPP_API InitState(HashWordType *state);
153 /// \brief Operate the hash
154 /// \param digest the state of the hash
155 /// \param data the data to be digested
156 /// \details Transform operates the hash on <tt>data</tt>. When the call is invoked
157 /// <tt>digest</tt> holds initial state. Upon return <tt>digest</tt> holds the hash
158 /// or updated state.
159 /// \details Hashes which derive from IteratedHashWithStaticTransform provide static
160 /// member functions InitState and Transform. External classes, like SEAL and MDC,
161 /// can initialize state with a user provided key and operate the hash on the data
162 /// with the user supplied state.
163 /// \note On Intel platforms the state array and data must be 16-byte aligned for SSE2.
164 static void CRYPTOPP_API Transform(HashWordType *digest, const HashWordType *data);
165 /// \brief The algorithm name
166 /// \return C-style string "SHA-512"
167 CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "SHA-512";}
168
169 // Algorithm class
170 std::string AlgorithmProvider() const;
171 };
172
173 /// \brief SHA-384 message digest
174 /// \sa <a href="http://www.weidai.com/scan-mirror/md.html#SHA-384">SHA-384</a>
175 /// \since SHA2 since Crypto++ 4.0, Power8 SHA since Crypto++ 6.1
176 class CRYPTOPP_DLL SHA384 : public IteratedHashWithStaticTransform<word64, BigEndian, 128, 64, SHA384, 48, true>
177 {
178 public:
179 /// \brief Initialize state array
180 /// \param state the state of the hash
181 /// \details InitState sets a state array to SHA384 initial values
182 /// \details Hashes which derive from IteratedHashWithStaticTransform provide static
183 /// member functions InitState and Transform. External classes, like SEAL and MDC,
184 /// can initialize state with a user provided key and operate the hash on the data
185 /// with the user supplied state.
186 /// \note On Intel platforms the state array must be 16-byte aligned for SSE2.
187 static void CRYPTOPP_API InitState(HashWordType *state);
188 /// \brief Operate the hash
189 /// \param digest the state of the hash
190 /// \param data the data to be digested
191 /// \details Transform operates the hash on <tt>data</tt>. When the call is invoked
192 /// <tt>digest</tt> holds initial state. Upon return <tt>digest</tt> holds the hash
193 /// or updated state.
194 /// \details Hashes which derive from IteratedHashWithStaticTransform provide static
195 /// member functions InitState and Transform. External classes, like SEAL and MDC,
196 /// can initialize state with a user provided key and operate the hash on the data
197 /// with the user supplied state.
198 /// \note On Intel platforms the state array and data must be 16-byte aligned for SSE2.
199 static void CRYPTOPP_API Transform(HashWordType *digest, const HashWordType *data) {SHA512::Transform(digest, data);}
200 /// \brief The algorithm name
201 /// \return C-style string "SHA-384"
202 CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "SHA-384";}
203
204 // Algorithm class
205 std::string AlgorithmProvider() const;
206 };
207
208 NAMESPACE_END
209
210 #endif
SHA384
SHA-384 message digest.
Definition: sha.h:176
SHA224::Transform
static void CRYPTOPP_API Transform(HashWordType *digest, const HashWordType *data)
Operate the hash.
Definition: sha.h:126
SHA256
SHA-256 message digest.
Definition: sha.h:64
IteratedHashWithStaticTransform
Iterated hash with a static transformation function.
Definition: iterhash.h:180
EnumToType
Converts an enumeration to a type suitable for use as a template parameter.
Definition: cryptlib.h:135
config.h
Library configuration file.
AlgorithmImpl< IteratedHash< T_HashWordType, T_Endianness, T_BlockSize >, T_Transform >::StaticAlgorithmName
static std::string CRYPTOPP_API StaticAlgorithmName()
The algorithm name.
Definition: simple.h:46
iterhash.h
Base classes for iterated hashes.
SHA512
SHA-512 message digest.
Definition: sha.h:141
SHA1
SHA-1 message digest.
Definition: sha.h:26
SHA512::Transform
static void CRYPTOPP_API Transform(HashWordType *digest, const HashWordType *data)
Operate the hash.
Definition: sha.cpp:1341
IteratedHashBase< T_HashWordType, HashTransformation >::AlgorithmProvider
virtual std::string AlgorithmProvider() const
Retrieve the provider of this algorithm.
Definition: iterhash.h:94
SHA256::Transform
static void CRYPTOPP_API Transform(HashWordType *digest, const HashWordType *data)
Operate the hash.
Definition: sha.cpp:843
CryptoPP
Crypto++ library namespace.
SHA224
SHA-224 message digest.
Definition: sha.h:103
SHA384::Transform
static void CRYPTOPP_API Transform(HashWordType *digest, const HashWordType *data)
Operate the hash.
Definition: sha.h:199