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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
/***************************************************************************
* Copyright (C) 2005 by Joris Guisson *
* [email protected] *
* *
* This program 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. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include <string.h>
#include <arpa/inet.h>
#include "sha1hashgen.h"
#include "functions.h"
namespace bt
{
static inline Uint32 LeftRotate(Uint32 x,Uint32 n)
{
return ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)));
}
SHA1HashGen::SHA1HashGen() : tmp_len(0),total_len(0)
{
}
SHA1HashGen::~SHA1HashGen()
{}
SHA1Hash SHA1HashGen::generate(const Uint8* data,Uint32 len)
{
h0 = 0x67452301;
h1 = 0xEFCDAB89;
h2 = 0x98BADCFE;
h3 = 0x10325476;
h4 = 0xC3D2E1F0;
Uint32 num_64_byte_chunks = len / 64;
Uint32 left_over = len % 64;
// proces regular data
for (Uint32 i = 0;i < num_64_byte_chunks;i++)
{
processChunk(data + (64*i));
}
// calculate the low and high byte of the data length
Uint32 total[2] = {0,0};
total[0] += len;
total[0] &= 0xFFFFFFFF;
if (total[0] < len)
total[1]++;
Uint32 high = ( total[0] >> 29 ) | ( total[1] << 3 );
Uint32 low = ( total[0] << 3 );
if (left_over == 0)
{
tmp[0] = 0x80;
for (Uint32 i = 1;i < 56;i++)
tmp[i] = 0;
// put in the length as 64-bit integer (BIG-ENDIAN)
WriteUint32(tmp,56,high);
WriteUint32(tmp,60,low);
// process the padding
processChunk(tmp);
}
else if (left_over < 56)
{
Uint32 off = num_64_byte_chunks * 64;
// copy left over bytes in tmp
memcpy(tmp,data + off, left_over);
tmp[left_over] = 0x80;
for (Uint32 i = left_over + 1;i < 56;i++)
tmp[i] = 0;
// put in the length as 64-bit integer (BIG-ENDIAN)
WriteUint32(tmp,56,high);
WriteUint32(tmp,60,low);
// process the padding
processChunk(tmp);
}
else
{
// now we need to process 2 chunks
Uint32 off = num_64_byte_chunks * 64;
// copy left over bytes in tmp
memcpy(tmp,data + off, left_over);
tmp[left_over] = 0x80;
for (Uint32 i = left_over + 1;i < 64;i++)
tmp[i] = 0;
// process first chunk
processChunk(tmp);
for (Uint32 i = 0;i < 56;i++)
tmp[i] = 0;
// put in the length as 64-bit integer (BIG-ENDIAN)
WriteUint32(tmp,56,high);
WriteUint32(tmp,60,low);
// process the second chunk
processChunk(tmp);
}
// construct final message
Uint8 hash[20];
WriteUint32(hash,0,h0);
WriteUint32(hash,4,h1);
WriteUint32(hash,8,h2);
WriteUint32(hash,12,h3);
WriteUint32(hash,16,h4);
return SHA1Hash(hash);
}
void SHA1HashGen::processChunk(const Uint8* chunk)
{
Uint32 w[80];
for (int i = 0;i < 80;i++)
{
if (i < 16)
{
w[i] = ntohl(*(const Uint32*)(chunk + (4*i)));
/* w[i] = (chunk[4*i] << 24) |
(chunk[4*i + 1] << 16) |
(chunk[4*i + 2] << 8) |
chunk[4*i + 3];
*/
}
else
{
w[i] = LeftRotate(w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16],1);
}
}
Uint32 a = h0;
Uint32 b = h1;
Uint32 c = h2;
Uint32 d = h3;
Uint32 e = h4;
for (int i = 0;i < 80;i++)
{
Uint32 f,k;
if (i < 20)
{
f = (b & c) | ((~b) & d);
k = 0x5A827999;
}
else if (i < 40)
{
f = b ^ c ^ d;
k = 0x6ED9EBA1;
}
else if (i < 60)
{
f = (b & c) | (b & d) | (c & d);
k = 0x8F1BBCDC;
}
else
{
f = b ^ c ^ d;
k = 0xCA62C1D6;
}
Uint32 temp = LeftRotate(a,5) + f + e + k + w[i];
e = d;
d = c;
c = LeftRotate(b,30);
b = a;
a = temp;
}
h0 = (h0 + a) & 0xffffffff;
h1 = (h1 + b) & 0xffffffff;
h2 = (h2 + c) & 0xffffffff;
h3 = (h3 + d) & 0xffffffff;
h4 = (h4 + e) & 0xffffffff;
}
void SHA1HashGen::start()
{
h0 = 0x67452301;
h1 = 0xEFCDAB89;
h2 = 0x98BADCFE;
h3 = 0x10325476;
h4 = 0xC3D2E1F0;
tmp_len = total_len = 0;
memset(tmp,0,64);
}
void SHA1HashGen::update(const Uint8* data,Uint32 len)
{
if (tmp_len == 0)
{
Uint32 num_64_byte_chunks = len / 64;
Uint32 left_over = len % 64;
// proces data in chunks of 64 byte
for (Uint32 i = 0;i < num_64_byte_chunks;i++)
{
processChunk(data + (64*i));
}
if (left_over > 0)
{
// if there is anything left over, copy it in tmp
memcpy(tmp,data + (64 * num_64_byte_chunks),left_over);
tmp_len = left_over;
}
total_len += len;
}
else
{
if (tmp_len + len < 64)
{
// special case, not enough of data to fill tmp completely
memcpy(tmp + tmp_len,data,len);
tmp_len += len;
total_len += len;
}
else
{
// copy start of data in tmp and process it
Uint32 off = 64 - tmp_len;
memcpy(tmp + tmp_len,data, 64 - tmp_len);
processChunk(tmp);
tmp_len = 0;
Uint32 num_64_byte_chunks = (len - off) / 64;
Uint32 left_over = (len - off) % 64;
for (Uint32 i = 0;i < num_64_byte_chunks;i++)
{
processChunk(data + (off + (64*i)));
}
if (left_over > 0)
{
// if there is anything left over, copy it in tmp
memcpy(tmp,data + (off + 64 * num_64_byte_chunks),left_over);
tmp_len = left_over;
}
total_len += len;
}
}
}
void SHA1HashGen::end()
{
// calculate the low and high byte of the data length
Uint32 total[2] = {0,0};
total[0] += total_len;
total[0] &= 0xFFFFFFFF;
if (total[0] < total_len)
total[1]++;
Uint32 high = ( total[0] >> 29 ) | ( total[1] << 3 );
Uint32 low = ( total[0] << 3 );
if (tmp_len == 0)
{
tmp[0] = 0x80;
for (Uint32 i = 1;i < 56;i++)
tmp[i] = 0;
// put in the length as 64-bit integer (BIG-ENDIAN)
WriteUint32(tmp,56,high);
WriteUint32(tmp,60,low);
// process the padding
processChunk(tmp);
}
else if (tmp_len < 56)
{
tmp[tmp_len] = 0x80;
for (Uint32 i = tmp_len + 1;i < 56;i++)
tmp[i] = 0;
// put in the length as 64-bit integer (BIG-ENDIAN)
WriteUint32(tmp,56,high);
WriteUint32(tmp,60,low);
// process the padding
processChunk(tmp);
}
else
{
// now we need to process 2 chunks
tmp[tmp_len] = 0x80;
for (Uint32 i = tmp_len + 1;i < 56;i++)
tmp[i] = 0;
// process first chunk
processChunk(tmp);
for (Uint32 i = 0;i < 56;i++)
tmp[i] = 0;
// put in the length as 64-bit integer (BIG-ENDIAN)
WriteUint32(tmp,56,high);
WriteUint32(tmp,60,low);
// process the second chunk
processChunk(tmp);
}
}
SHA1Hash SHA1HashGen::get() const
{
// construct final message
Uint8 hash[20];
WriteUint32(hash,0,h0);
WriteUint32(hash,4,h1);
WriteUint32(hash,8,h2);
WriteUint32(hash,12,h3);
WriteUint32(hash,16,h4);
return SHA1Hash(hash);
}
}
|