-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsha.cpp
256 lines (218 loc) · 7.08 KB
/
sha.cpp
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
* @file
*
* @author CCHyper
* @author OmniBlade
*
* @brief SHA1 Hashing engine.
*
* @copyright SetSail is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version
* 2 of the License, or (at your option) any later version.
* A full copy of the GNU General Public License can be found in
* LICENSE
*/
#include "sha.h"
#include "endiantype.h"
#include <cstdio>
#include <cstring>
using std::memcpy;
using std::memset;
using std::sprintf;
SHAEngine::SHAEngine() : m_computed(false), m_processedBytes(0), m_unprocessedBytes(0)
{
// make sure that the data type is the right size
static_assert((sizeof(uint32_t) * 5) == SHA_DIGEST_LENGTH, "Data type used for holding the hash is not expected size.");
// initialize hash constants
m_digest.H0 = 0x67452301;
m_digest.H1 = 0xEFCDAB89;
m_digest.H2 = 0x98BADCFE;
m_digest.H3 = 0x10325476;
m_digest.H4 = 0xC3D2E1F0;
}
SHAEngine::~SHAEngine()
{
// clear hash constants
m_digest.H0 = 0;
m_digest.H1 = 0;
m_digest.H2 = 0;
m_digest.H3 = 0;
m_digest.H4 = 0;
}
/**
* @brief Process data for the hash that doesn't fit into the 64bit processing block..
*/
void SHAEngine::Process_Partial(const void *&data, int &length)
{
unsigned int bytestoprocess;
if (length && data) {
if (m_unprocessedBytes || length < SHA_BLOCK_LENGTH) {
bytestoprocess = SHA_BLOCK_LENGTH - m_unprocessedBytes;
if (length < (int)(SHA_BLOCK_LENGTH - m_unprocessedBytes)) {
bytestoprocess = length;
}
memcpy(m_messageBlock + m_unprocessedBytes, data, bytestoprocess);
data = static_cast<const char *>(data) + bytestoprocess;
m_unprocessedBytes += bytestoprocess;
length -= bytestoprocess;
if (m_unprocessedBytes == SHA_BLOCK_LENGTH) {
SHAEngine::Process_Block(m_messageBlock, m_digest);
m_processedBytes += SHA_BLOCK_LENGTH;
m_unprocessedBytes = 0;
}
}
}
}
/**
* @brief Process data for the hash.
*/
void SHAEngine::Hash(const void *data, int length)
{
m_computed = false;
Process_Partial(data, length);
if (length) {
if (length / SHA_BLOCK_LENGTH > 0) {
int datablocks = length / SHA_BLOCK_LENGTH;
for (int i = 0; i < datablocks; ++i) {
SHAEngine::Process_Block(data, m_digest);
data = static_cast<const char *>(data) + SHA_BLOCK_LENGTH;
m_processedBytes += SHA_BLOCK_LENGTH;
length -= SHA_BLOCK_LENGTH;
}
}
Process_Partial(data, length);
}
}
/**
* @brief Finalise the hash and outputs it to the provided buffer.
*/
int SHAEngine::Result(void *output)
{
int bytesremaining;
uint32_t *finalp;
SHADigest digest;
char msgblock[SHA_BLOCK_LENGTH];
char *mblockp;
// If we already computed the hash, don't waste time doing it again
if (m_computed) {
memcpy(output, &m_finalDigest, SHA_DIGEST_LENGTH);
} else {
// These copies allow us to continue hashing data if we want
memcpy(msgblock, m_messageBlock, SHA_BLOCK_LENGTH);
uint32_t totalbytes = m_unprocessedBytes + m_processedBytes;
// start padding with first bit set
msgblock[m_unprocessedBytes++] = -128;
// These copies allow us to continue hashing data if we want
memcpy(&digest, &m_digest, sizeof(digest));
bytesremaining = m_unprocessedBytes;
// Message block must have at least 8bytes after padding
// If it won't have enough space, pad with 0 and process
if ((SHA_BLOCK_LENGTH - m_unprocessedBytes) < 8) {
if (m_unprocessedBytes < SHA_BLOCK_LENGTH) {
mblockp = msgblock;
memset(mblockp + bytesremaining, 0, SHA_BLOCK_LENGTH - bytesremaining);
m_unprocessedBytes = SHA_BLOCK_LENGTH;
}
SHAEngine::Process_Block(msgblock, digest);
bytesremaining = 0;
m_unprocessedBytes = 0;
}
memset(msgblock + bytesremaining, 0, 56 - bytesremaining);
mblockp = &msgblock[56];
// pad total length of data into last 8 bytes
*reinterpret_cast<uint32_t *>(msgblock + 56) = htobe32(totalbytes >> 29);
*reinterpret_cast<uint32_t *>(msgblock + 60) = htobe32(totalbytes << 3);
m_unprocessedBytes = SHA_BLOCK_LENGTH;
SHAEngine::Process_Block(&msgblock[0], digest);
m_unprocessedBytes = 0;
// format the digest into the correct byte order, big endian
memcpy(&m_finalDigest, &digest, sizeof(m_finalDigest));
finalp = reinterpret_cast<uint32_t *>(&m_finalDigest.H0);
for (int i = 0; i < 5; ++i) {
finalp[i] = htobe32(finalp[i]);
}
m_computed = true;
memcpy(output, &m_finalDigest, SHA_DIGEST_LENGTH);
}
return 20;
}
/**
* @brief Processes a 64 byte block into the hash..
*/
void SHAEngine::Process_Block(const void *input, SHADigest &digest) const
{
const uint8_t *data = static_cast<const uint8_t *>(input);
int t;
uint32_t a;
uint32_t b;
uint32_t c;
uint32_t d;
uint32_t e;
uint32_t K;
uint32_t f;
uint32_t W[80];
// Copy and expand the message block (Rounds 0 to 15?)
for (t = 0; t < 16; ++t) {
W[t] = (data[t * 4] << 24) + (data[t * 4 + 1] << 16) + (data[t * 4 + 2] << 8) + data[t * 4 + 3];
}
for (; t < 80; ++t) {
W[t] = __rotl32(W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16], 1);
}
// Initialize registers with the previous intermediate value.
a = digest.H0;
b = digest.H1;
c = digest.H2;
d = digest.H3;
e = digest.H4;
// main loop
for (t = 0; t < 80; ++t) {
if (t < 20) {
K = 0x5A827999;
f = (b & c) | ((~b) & d);
} else if (t < 40) {
K = 0x6ED9EBA1;
f = b ^ c ^ d;
} else if (t < 60) {
K = 0x8F1BBCDC;
f = (b & c) | (b & d) | (c & d);
} else {
K = 0xCA62C1D6;
f = b ^ c ^ d;
}
uint32_t temp = __rotl32(a, 5) + f + e + W[t] + K;
e = d;
d = c;
c = __rotl32(b, 30);
b = a;
a = temp;
}
// Compute the current intermediate hash value.
digest.H0 += a;
digest.H1 += b;
digest.H2 += c;
digest.H3 += d;
digest.H4 += e;
}
/**
* @brief Formats the hash into a string for printing.
*/
void SHAEngine::Print(const void *buffer, char *output)
{
for (int i = 0; i < 20; i++) {
sprintf(&output[2 * i], "%02x", *(static_cast<const uint8_t *>(buffer) + i));
}
}
/**
* @brief Prints the SHA1 hash to the provided buffer as a C string.
*
* @param output Pointer to the destination buffer.
* @return Length of the string in bytes.
*/
int SHAEngine::Print_Result(char *output)
{
uint8_t buffer[20];
int retval = Result(buffer);
Print(buffer, output);
return retval;
}