summaryrefslogtreecommitdiff
path: root/sha.h
blob: c53213a59a782dbb905e969b3af5a4b50f9b882e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// sha.h - originally written and placed in the public domain by Wei Dai

//! \file sha.h
//! \brief Classes for SHA-1 and SHA-2 family of message digests
//! \since SHA1 since Crypto++ 1.0, SHA2 since Crypto++ 4.0,
//!   ARM SHA since Crypto++ 6.0, Intel SHA since Crypto++ 6.0

#ifndef CRYPTOPP_SHA_H
#define CRYPTOPP_SHA_H

#include "config.h"
#include "iterhash.h"

#if (CRYPTOPP_BOOL_X86)
#  define SHA_X86_ALIGN16  true
#else
#  define SHA_X86_ALIGN16  false
#endif

NAMESPACE_BEGIN(CryptoPP)

//! \class SHA1
//! \brief SHA-1 message digest
//! \sa <a href="http://www.weidai.com/scan-mirror/md.html#SHA-1">SHA-1</a>
//! \since Crypto++ 1.0, Intel SHA extensions since Crypto++ 6.0
class CRYPTOPP_DLL SHA1 : public IteratedHashWithStaticTransform<word32, BigEndian, 64, 20, SHA1>
{
public:
	//! \brief Initialize state array
	//! \param state the state of the hash
	//! \details InitState sets a state array to SHA1 initial values
	//! \details Hashes which derive from IteratedHashWithStaticTransform provide static
	//!   member functions InitState and Transform. External classes, like SEAL and MDC,
	//!   can initialize state with a user provided key and operate the hash on the data
	//!   with the user supplied state.
	//! \note On Intel platforms the state array must be 16-byte aligned for SSE2.
	static void CRYPTOPP_API InitState(HashWordType *state);
	//! \brief Operate the hash
	//! \param digest the state of the hash
	//! \param data the data to be digested
	//! \details Transform operates the hash on <tt>data</tt>. When the call is invoked
	//!   <tt>digest</tt> holds initial state. Upon return <tt>digest</tt> holds the hash
	//!   or updated state.
	//! \details Hashes which derive from IteratedHashWithStaticTransform provide static
	//!   member functions InitState and Transform. External classes, like SEAL and MDC,
	//!   can initialize state with a user provided key and operate the hash on the data
	//!   with the user supplied state.
	//! \note On Intel platforms the state array and data must be 16-byte aligned for SSE2.
	static void CRYPTOPP_API Transform(HashWordType *digest, const HashWordType *data);
	//! \brief The algorithm name
	//! \returns C-style string "SHA-1"
	CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "SHA-1";}

protected:
	size_t HashMultipleBlocks(const HashWordType *input, size_t length);
};

//! \class SHA256
//! \brief SHA-256 message digest
//! \sa <a href="http://www.weidai.com/scan-mirror/md.html#SHA-256">SHA-256</a>
//! \since Crypto++ 4.0, Intel SHA extensions since Crypto++ 6.0
class CRYPTOPP_DLL SHA256 : public IteratedHashWithStaticTransform<word32, BigEndian, 64, 32, SHA256, 32, true>
{
public:
	//! \brief Initialize state array
	//! \param state the state of the hash
	//! \details InitState sets a state array to SHA256 initial values
	//! \details Hashes which derive from IteratedHashWithStaticTransform provide static
	//!   member functions InitState and Transform. External classes, like SEAL and MDC,
	//!   can initialize state with a user provided key and operate the hash on the data
	//!   with the user supplied state.
	//! \note On Intel platforms the state array must be 16-byte aligned for SSE2.
	static void CRYPTOPP_API InitState(HashWordType *state);
	//! \brief Operate the hash
	//! \param digest the state of the hash
	//! \param data the data to be digested
	//! \details Transform operates the hash on <tt>data</tt>. When the call is invoked
	//!   <tt>digest</tt> holds initial state. Upon return <tt>digest</tt> holds the hash
	//!   or updated state.
	//! \details Hashes which derive from IteratedHashWithStaticTransform provide static
	//!   member functions InitState and Transform. External classes, like SEAL and MDC,
	//!   can initialize state with a user provided key and operate the hash on the data
	//!   with the user supplied state.
	//! \note On Intel platforms the state array and data must be 16-byte aligned for SSE2.
	static void CRYPTOPP_API Transform(HashWordType *digest, const HashWordType *data);
	//! \brief The algorithm name
	//! \returns C-style string "SHA-256"
	CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "SHA-256";}

protected:
	size_t HashMultipleBlocks(const HashWordType *input, size_t length);
};

//! \class SHA224
//! \brief SHA-224 message digest
//! \sa <a href="http://www.weidai.com/scan-mirror/md.html#SHA-224">SHA-224</a>
//! \since Crypto++ 4.0, Intel SHA extensions since Crypto++ 6.0
class CRYPTOPP_DLL SHA224 : public IteratedHashWithStaticTransform<word32, BigEndian, 64, 32, SHA224, 28, true>
{
public:
	//! \brief Initialize state array
	//! \param state the state of the hash
	//! \details InitState sets a state array to SHA224 initial values
	//! \details Hashes which derive from IteratedHashWithStaticTransform provide static
	//!   member functions InitState and Transform. External classes, like SEAL and MDC,
	//!   can initialize state with a user provided key and operate the hash on the data
	//!   with the user supplied state.
	//! \note On Intel platforms the state array must be 16-byte aligned for SSE2.
	static void CRYPTOPP_API InitState(HashWordType *state);
	//! \brief Operate the hash
	//! \param digest the state of the hash
	//! \param data the data to be digested
	//! \details Transform operates the hash on <tt>data</tt>. When the call is invoked
	//!   <tt>digest</tt> holds initial state. Upon return <tt>digest</tt> holds the hash
	//!   or updated state.
	//! \details Hashes which derive from IteratedHashWithStaticTransform provide static
	//!   member functions InitState and Transform. External classes, like SEAL and MDC,
	//!   can initialize state with a user provided key and operate the hash on the data
	//!   with the user supplied state.
	//! \note On Intel platforms the state array and data must be 16-byte aligned for SSE2.
	static void CRYPTOPP_API Transform(HashWordType *digest, const HashWordType *data) {SHA256::Transform(digest, data);}
	//! \brief The algorithm name
	//! \returns C-style string "SHA-224"
	CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "SHA-224";}

protected:
	size_t HashMultipleBlocks(const HashWordType *input, size_t length);
};

//! \class SHA512
//! \brief SHA-512 message digest
//! \sa <a href="http://www.weidai.com/scan-mirror/md.html#SHA-512">SHA-512</a>
//! \since Crypto++ 4.0
class CRYPTOPP_DLL SHA512 : public IteratedHashWithStaticTransform<word64, BigEndian, 128, 64, SHA512, 64, SHA_X86_ALIGN16>
{
public:
	//! \brief Initialize state array
	//! \param state the state of the hash
	//! \details InitState sets a state array to SHA512 initial values
	//! \details Hashes which derive from IteratedHashWithStaticTransform provide static
	//!   member functions InitState and Transform. External classes, like SEAL and MDC,
	//!   can initialize state with a user provided key and operate the hash on the data
	//!   with the user supplied state.
	//! \note On Intel platforms the state array must be 16-byte aligned for SSE2.
	static void CRYPTOPP_API InitState(HashWordType *state);
	//! \brief Operate the hash
	//! \param digest the state of the hash
	//! \param data the data to be digested
	//! \details Transform operates the hash on <tt>data</tt>. When the call is invoked
	//!   <tt>digest</tt> holds initial state. Upon return <tt>digest</tt> holds the hash
	//!   or updated state.
	//! \details Hashes which derive from IteratedHashWithStaticTransform provide static
	//!   member functions InitState and Transform. External classes, like SEAL and MDC,
	//!   can initialize state with a user provided key and operate the hash on the data
	//!   with the user supplied state.
	//! \note On Intel platforms the state array and data must be 16-byte aligned for SSE2.
	static void CRYPTOPP_API Transform(HashWordType *digest, const HashWordType *data);
	//! \brief The algorithm name
	//! \returns C-style string "SHA-512"
	CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "SHA-512";}
};

//! \class SHA384
//! \brief SHA-384 message digest
//! \sa <a href="http://www.weidai.com/scan-mirror/md.html#SHA-384">SHA-384</a>
//! \since Crypto++ 4.0
class CRYPTOPP_DLL SHA384 : public IteratedHashWithStaticTransform<word64, BigEndian, 128, 64, SHA384, 48, SHA_X86_ALIGN16>
{
public:
	//! \brief Initialize state array
	//! \param state the state of the hash
	//! \details InitState sets a state array to SHA384 initial values
	//! \details Hashes which derive from IteratedHashWithStaticTransform provide static
	//!   member functions InitState and Transform. External classes, like SEAL and MDC,
	//!   can initialize state with a user provided key and operate the hash on the data
	//!   with the user supplied state.
	//! \note On Intel platforms the state array must be 16-byte aligned for SSE2.
	static void CRYPTOPP_API InitState(HashWordType *state);
	//! \brief Operate the hash
	//! \param digest the state of the hash
	//! \param data the data to be digested
	//! \details Transform operates the hash on <tt>data</tt>. When the call is invoked
	//!   <tt>digest</tt> holds initial state. Upon return <tt>digest</tt> holds the hash
	//!   or updated state.
	//! \details Hashes which derive from IteratedHashWithStaticTransform provide static
	//!   member functions InitState and Transform. External classes, like SEAL and MDC,
	//!   can initialize state with a user provided key and operate the hash on the data
	//!   with the user supplied state.
	//! \note On Intel platforms the state array and data must be 16-byte aligned for SSE2.
	static void CRYPTOPP_API Transform(HashWordType *digest, const HashWordType *data) {SHA512::Transform(digest, data);}
	//! \brief The algorithm name
	//! \returns C-style string "SHA-384"
	CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "SHA-384";}
};

NAMESPACE_END

#endif