Crypto++ 8.9 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
AlgorithmImpl::StaticAlgorithmName
static std::string StaticAlgorithmName()
The algorithm name.
Definition: simple.h:46
IteratedHashWithStaticTransform
Iterated hash with a static transformation function.
Definition: iterhash.h:182
SHA1
SHA-1 message digest.
Definition: sha.h:27
SHA1::Transform
static void Transform(HashWordType *digest, const HashWordType *data)
Operate the hash.
SHA1::InitState
static void InitState(HashWordType *state)
Initialize state array.
SHA224
SHA-224 message digest.
Definition: sha.h:104
SHA224::InitState
static void InitState(HashWordType *state)
Initialize state array.
SHA224::Transform
static void Transform(HashWordType *digest, const HashWordType *data)
Operate the hash.
Definition: sha.h:126
SHA256
SHA-256 message digest.
Definition: sha.h:65
SHA256::InitState
static void InitState(HashWordType *state)
Initialize state array.
SHA256::Transform
static void Transform(HashWordType *digest, const HashWordType *data)
Operate the hash.
SHA384
SHA-384 message digest.
Definition: sha.h:177
SHA384::Transform
static void Transform(HashWordType *digest, const HashWordType *data)
Operate the hash.
Definition: sha.h:199
SHA384::InitState
static void InitState(HashWordType *state)
Initialize state array.
SHA512
SHA-512 message digest.
Definition: sha.h:142
SHA512::Transform
static void Transform(HashWordType *digest, const HashWordType *data)
Operate the hash.
SHA512::InitState
static void InitState(HashWordType *state)
Initialize state array.
config.h
Library configuration file.
CRYPTOPP_API
#define CRYPTOPP_API
Win32 calling convention.
Definition: config_dll.h:119
word32
unsigned int word32
32-bit unsigned datatype
Definition: config_int.h:72
iterhash.h
Base classes for iterated hashes.
CryptoPP
Crypto++ library namespace.
EnumToType
Converts an enumeration to a type suitable for use as a template parameter.
Definition: cryptlib.h:141