author | Senko Rasic <senko.rasic@collabora.co.uk> |
Thu, 03 Jan 2008 18:03:35 +0100 | |
changeset 326 | 33e5f0c5ba10 |
parent 162 | 16d29898f97b |
child 496 | 76a465d81893 |
permissions | -rw-r--r-- |
1 | 1 |
/*- |
2 |
* Copyright (c) 2001, 2002 Allan Saddi <allan@saddi.com> |
|
3 |
* All rights reserved. |
|
4 |
* |
|
5 |
* Redistribution and use in source and binary forms, with or without |
|
6 |
* modification, are permitted provided that the following conditions |
|
7 |
* are met: |
|
8 |
* 1. Redistributions of source code must retain the above copyright |
|
9 |
* notice, this list of conditions and the following disclaimer. |
|
10 |
* 2. Redistributions in binary form must reproduce the above copyright |
|
11 |
* notice, this list of conditions and the following disclaimer in the |
|
12 |
* documentation and/or other materials provided with the distribution. |
|
13 |
* |
|
14 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
|
15 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
16 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
17 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
|
18 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|
19 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
|
20 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|
21 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
22 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
|
23 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|
24 |
* SUCH DAMAGE. |
|
25 |
* |
|
26 |
*/ |
|
27 |
||
28 |
/* |
|
29 |
* Define WORDS_BIGENDIAN if compiling on a big-endian architecture. |
|
30 |
* |
|
31 |
* Define SHA1_TEST to test the implementation using the NIST's |
|
32 |
* sample messages. The output should be: |
|
33 |
* |
|
34 |
* a9993e36 4706816a ba3e2571 7850c26c 9cd0d89d |
|
35 |
* 84983e44 1c3bd26e baae4aa1 f95129e5 e54670f1 |
|
36 |
* 34aa973c d4c4daa4 f61eeb2b dbad2731 6534016f |
|
37 |
*/ |
|
38 |
||
39 |
#ifdef HAVE_CONFIG_H |
|
40 |
#include <config.h> |
|
41 |
#endif /* HAVE_CONFIG_H */ |
|
42 |
||
43 |
#include <string.h> |
|
44 |
#include <stdio.h> |
|
45 |
#include <glib.h> |
|
46 |
||
47 |
#include "lm-sha.h" |
|
48 |
||
49 |
#define SHA1_HASH_SIZE 20 |
|
50 |
||
51 |
/* Hash size in 32-bit words */ |
|
52 |
#define SHA1_HASH_WORDS 5 |
|
53 |
||
54 |
struct _SHA1Context { |
|
55 |
guint64 totalLength; |
|
56 |
guint32 hash[SHA1_HASH_WORDS]; |
|
57 |
guint32 bufferLength; |
|
58 |
union { |
|
59 |
guint32 words[16]; |
|
60 |
guint8 bytes[64]; |
|
61 |
} buffer; |
|
62 |
}; |
|
63 |
||
64 |
typedef struct _SHA1Context SHA1Context; |
|
65 |
||
66 |
#ifdef __cplusplus |
|
67 |
extern "C" { |
|
68 |
#endif |
|
69 |
||
162
16d29898f97b
2006-08-03 Richard Hult <richard@imendio.com>
hallski <hallski>
parents:
140
diff
changeset
|
70 |
static void SHA1Init (SHA1Context *sc); |
16d29898f97b
2006-08-03 Richard Hult <richard@imendio.com>
hallski <hallski>
parents:
140
diff
changeset
|
71 |
static void SHA1Update (SHA1Context *sc, const void *udata, guint32 len); |
16d29898f97b
2006-08-03 Richard Hult <richard@imendio.com>
hallski <hallski>
parents:
140
diff
changeset
|
72 |
static void SHA1Final (SHA1Context *sc, guint8 hash[SHA1_HASH_SIZE]); |
1 | 73 |
|
74 |
#ifdef __cplusplus |
|
75 |
} |
|
76 |
#endif |
|
77 |
||
78 |
#ifndef lint |
|
79 |
static const char rcsid[] = |
|
80 |
"$Id$"; |
|
81 |
#endif /* !lint */ |
|
82 |
||
83 |
#define ROTL(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) |
|
84 |
#define ROTR(x, n) (((x) >> (n)) | ((x) << (32 - (n)))) |
|
85 |
||
86 |
#define F_0_19(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) |
|
87 |
#define F_20_39(x, y, z) ((x) ^ (y) ^ (z)) |
|
88 |
#define F_40_59(x, y, z) (((x) & ((y) | (z))) | ((y) & (z))) |
|
89 |
#define F_60_79(x, y, z) ((x) ^ (y) ^ (z)) |
|
90 |
||
91 |
#define DO_ROUND(F, K) { \ |
|
92 |
temp = ROTL(a, 5) + F(b, c, d) + e + *(W++) + K; \ |
|
93 |
e = d; \ |
|
94 |
d = c; \ |
|
95 |
c = ROTL(b, 30); \ |
|
96 |
b = a; \ |
|
97 |
a = temp; \ |
|
98 |
} |
|
99 |
||
100 |
#define K_0_19 0x5a827999L |
|
101 |
#define K_20_39 0x6ed9eba1L |
|
102 |
#define K_40_59 0x8f1bbcdcL |
|
103 |
#define K_60_79 0xca62c1d6L |
|
104 |
||
105 |
#ifndef RUNTIME_ENDIAN |
|
106 |
||
107 |
#ifdef WORDS_BIGENDIAN |
|
108 |
||
109 |
#define BYTESWAP(x) (x) |
|
110 |
#define BYTESWAP64(x) (x) |
|
111 |
||
112 |
#else /* WORDS_BIGENDIAN */ |
|
113 |
||
114 |
#define BYTESWAP(x) ((ROTR((x), 8) & 0xff00ff00L) | \ |
|
115 |
(ROTL((x), 8) & 0x00ff00ffL)) |
|
116 |
#define BYTESWAP64(x) _byteswap64(x) |
|
117 |
||
118 |
static inline guint64 _byteswap64(guint64 x) |
|
119 |
{ |
|
120 |
guint32 a = x >> 32; |
|
121 |
guint32 b = (guint32) x; |
|
122 |
return ((guint64) BYTESWAP(b) << 32) | (guint64) BYTESWAP(a); |
|
123 |
} |
|
124 |
||
125 |
#endif /* WORDS_BIGENDIAN */ |
|
126 |
||
127 |
#else /* !RUNTIME_ENDIAN */ |
|
128 |
||
129 |
static int littleEndian; |
|
130 |
||
131 |
#define BYTESWAP(x) _byteswap(x) |
|
132 |
#define BYTESWAP64(x) _byteswap64(x) |
|
133 |
||
134 |
#define _BYTESWAP(x) ((ROTR((x), 8) & 0xff00ff00L) | \ |
|
135 |
(ROTL((x), 8) & 0x00ff00ffL)) |
|
136 |
#define _BYTESWAP64(x) __byteswap64(x) |
|
137 |
||
138 |
static inline guint64 __byteswap64(guint64 x) |
|
139 |
{ |
|
140 |
guint32 a = x >> 32; |
|
141 |
guint32 b = (guint32) x; |
|
142 |
return ((guint64) _BYTESWAP(b) << 32) | (guint64) _BYTESWAP(a); |
|
143 |
} |
|
144 |
||
145 |
static inline guint32 _byteswap(guint32 x) |
|
146 |
{ |
|
147 |
if (!littleEndian) |
|
148 |
return x; |
|
149 |
else |
|
150 |
return _BYTESWAP(x); |
|
151 |
} |
|
152 |
||
153 |
static inline guint64 _byteswap64(guint64 x) |
|
154 |
{ |
|
155 |
if (!littleEndian) |
|
156 |
return x; |
|
157 |
else |
|
158 |
return _BYTESWAP64(x); |
|
159 |
} |
|
160 |
||
161 |
static inline void setEndian(void) |
|
162 |
{ |
|
163 |
union { |
|
164 |
guint32 w; |
|
165 |
guint8 b[4]; |
|
166 |
} endian; |
|
167 |
||
168 |
endian.w = 1L; |
|
169 |
littleEndian = endian.b[0] != 0; |
|
170 |
} |
|
171 |
||
172 |
#endif /* !RUNTIME_ENDIAN */ |
|
173 |
||
174 |
static const guint8 padding[64] = { |
|
175 |
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
176 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
177 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
178 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
179 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
180 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
181 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
|
182 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
|
183 |
}; |
|
184 |
||
162
16d29898f97b
2006-08-03 Richard Hult <richard@imendio.com>
hallski <hallski>
parents:
140
diff
changeset
|
185 |
static void |
1 | 186 |
SHA1Init (SHA1Context *sc) |
187 |
{ |
|
188 |
#ifdef RUNTIME_ENDIAN |
|
189 |
setEndian (); |
|
190 |
#endif /* RUNTIME_ENDIAN */ |
|
191 |
||
140
103227122f45
2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
86
diff
changeset
|
192 |
#ifdef G_OS_WIN32 |
9
2e14e3ab411b
2003-06-24 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
1
diff
changeset
|
193 |
sc->totalLength = 0L; |
2e14e3ab411b
2003-06-24 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
1
diff
changeset
|
194 |
#else |
1 | 195 |
sc->totalLength = 0LL; |
9
2e14e3ab411b
2003-06-24 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
1
diff
changeset
|
196 |
#endif |
1 | 197 |
sc->hash[0] = 0x67452301L; |
198 |
sc->hash[1] = 0xefcdab89L; |
|
199 |
sc->hash[2] = 0x98badcfeL; |
|
200 |
sc->hash[3] = 0x10325476L; |
|
201 |
sc->hash[4] = 0xc3d2e1f0L; |
|
202 |
sc->bufferLength = 0L; |
|
203 |
} |
|
204 |
||
205 |
static void |
|
206 |
burnStack (int size) |
|
207 |
{ |
|
208 |
char buf[128]; |
|
209 |
||
210 |
memset (buf, 0, sizeof (buf)); |
|
211 |
size -= sizeof (buf); |
|
212 |
if (size > 0) |
|
213 |
burnStack (size); |
|
214 |
} |
|
215 |
||
216 |
static void |
|
217 |
SHA1Guts (SHA1Context *sc, const guint32 *cbuf) |
|
218 |
{ |
|
219 |
guint32 buf[80]; |
|
220 |
guint32 *W, *W3, *W8, *W14, *W16; |
|
221 |
guint32 a, b, c, d, e, temp; |
|
222 |
int i; |
|
223 |
||
224 |
W = buf; |
|
225 |
||
226 |
for (i = 15; i >= 0; i--) { |
|
227 |
*(W++) = BYTESWAP(*cbuf); |
|
228 |
cbuf++; |
|
229 |
} |
|
230 |
||
231 |
W16 = &buf[0]; |
|
232 |
W14 = &buf[2]; |
|
233 |
W8 = &buf[8]; |
|
234 |
W3 = &buf[13]; |
|
235 |
||
236 |
for (i = 63; i >= 0; i--) { |
|
237 |
*W = *(W3++) ^ *(W8++) ^ *(W14++) ^ *(W16++); |
|
238 |
*W = ROTL(*W, 1); |
|
239 |
W++; |
|
240 |
} |
|
241 |
||
242 |
a = sc->hash[0]; |
|
243 |
b = sc->hash[1]; |
|
244 |
c = sc->hash[2]; |
|
245 |
d = sc->hash[3]; |
|
246 |
e = sc->hash[4]; |
|
247 |
||
248 |
W = buf; |
|
249 |
||
250 |
#ifndef SHA1_UNROLL |
|
251 |
#define SHA1_UNROLL 20 |
|
252 |
#endif /* !SHA1_UNROLL */ |
|
253 |
||
254 |
#if SHA1_UNROLL == 1 |
|
255 |
for (i = 19; i >= 0; i--) |
|
256 |
DO_ROUND(F_0_19, K_0_19); |
|
257 |
||
258 |
for (i = 19; i >= 0; i--) |
|
259 |
DO_ROUND(F_20_39, K_20_39); |
|
260 |
||
261 |
for (i = 19; i >= 0; i--) |
|
262 |
DO_ROUND(F_40_59, K_40_59); |
|
263 |
||
264 |
for (i = 19; i >= 0; i--) |
|
265 |
DO_ROUND(F_60_79, K_60_79); |
|
266 |
#elif SHA1_UNROLL == 2 |
|
267 |
for (i = 9; i >= 0; i--) { |
|
268 |
DO_ROUND(F_0_19, K_0_19); |
|
269 |
DO_ROUND(F_0_19, K_0_19); |
|
270 |
} |
|
271 |
||
272 |
for (i = 9; i >= 0; i--) { |
|
273 |
DO_ROUND(F_20_39, K_20_39); |
|
274 |
DO_ROUND(F_20_39, K_20_39); |
|
275 |
} |
|
276 |
||
277 |
for (i = 9; i >= 0; i--) { |
|
278 |
DO_ROUND(F_40_59, K_40_59); |
|
279 |
DO_ROUND(F_40_59, K_40_59); |
|
280 |
} |
|
281 |
||
282 |
for (i = 9; i >= 0; i--) { |
|
283 |
DO_ROUND(F_60_79, K_60_79); |
|
284 |
DO_ROUND(F_60_79, K_60_79); |
|
285 |
} |
|
286 |
#elif SHA1_UNROLL == 4 |
|
287 |
for (i = 4; i >= 0; i--) { |
|
288 |
DO_ROUND(F_0_19, K_0_19); |
|
289 |
DO_ROUND(F_0_19, K_0_19); |
|
290 |
DO_ROUND(F_0_19, K_0_19); |
|
291 |
DO_ROUND(F_0_19, K_0_19); |
|
292 |
} |
|
293 |
||
294 |
for (i = 4; i >= 0; i--) { |
|
295 |
DO_ROUND(F_20_39, K_20_39); |
|
296 |
DO_ROUND(F_20_39, K_20_39); |
|
297 |
DO_ROUND(F_20_39, K_20_39); |
|
298 |
DO_ROUND(F_20_39, K_20_39); |
|
299 |
} |
|
300 |
||
301 |
for (i = 4; i >= 0; i--) { |
|
302 |
DO_ROUND(F_40_59, K_40_59); |
|
303 |
DO_ROUND(F_40_59, K_40_59); |
|
304 |
DO_ROUND(F_40_59, K_40_59); |
|
305 |
DO_ROUND(F_40_59, K_40_59); |
|
306 |
} |
|
307 |
||
308 |
for (i = 4; i >= 0; i--) { |
|
309 |
DO_ROUND(F_60_79, K_60_79); |
|
310 |
DO_ROUND(F_60_79, K_60_79); |
|
311 |
DO_ROUND(F_60_79, K_60_79); |
|
312 |
DO_ROUND(F_60_79, K_60_79); |
|
313 |
} |
|
314 |
#elif SHA1_UNROLL == 5 |
|
315 |
for (i = 3; i >= 0; i--) { |
|
316 |
DO_ROUND(F_0_19, K_0_19); |
|
317 |
DO_ROUND(F_0_19, K_0_19); |
|
318 |
DO_ROUND(F_0_19, K_0_19); |
|
319 |
DO_ROUND(F_0_19, K_0_19); |
|
320 |
DO_ROUND(F_0_19, K_0_19); |
|
321 |
} |
|
322 |
||
323 |
for (i = 3; i >= 0; i--) { |
|
324 |
DO_ROUND(F_20_39, K_20_39); |
|
325 |
DO_ROUND(F_20_39, K_20_39); |
|
326 |
DO_ROUND(F_20_39, K_20_39); |
|
327 |
DO_ROUND(F_20_39, K_20_39); |
|
328 |
DO_ROUND(F_20_39, K_20_39); |
|
329 |
} |
|
330 |
||
331 |
for (i = 3; i >= 0; i--) { |
|
332 |
DO_ROUND(F_40_59, K_40_59); |
|
333 |
DO_ROUND(F_40_59, K_40_59); |
|
334 |
DO_ROUND(F_40_59, K_40_59); |
|
335 |
DO_ROUND(F_40_59, K_40_59); |
|
336 |
DO_ROUND(F_40_59, K_40_59); |
|
337 |
} |
|
338 |
||
339 |
for (i = 3; i >= 0; i--) { |
|
340 |
DO_ROUND(F_60_79, K_60_79); |
|
341 |
DO_ROUND(F_60_79, K_60_79); |
|
342 |
DO_ROUND(F_60_79, K_60_79); |
|
343 |
DO_ROUND(F_60_79, K_60_79); |
|
344 |
DO_ROUND(F_60_79, K_60_79); |
|
345 |
} |
|
346 |
#elif SHA1_UNROLL == 10 |
|
347 |
for (i = 1; i >= 0; i--) { |
|
348 |
DO_ROUND(F_0_19, K_0_19); |
|
349 |
DO_ROUND(F_0_19, K_0_19); |
|
350 |
DO_ROUND(F_0_19, K_0_19); |
|
351 |
DO_ROUND(F_0_19, K_0_19); |
|
352 |
DO_ROUND(F_0_19, K_0_19); |
|
353 |
DO_ROUND(F_0_19, K_0_19); |
|
354 |
DO_ROUND(F_0_19, K_0_19); |
|
355 |
DO_ROUND(F_0_19, K_0_19); |
|
356 |
DO_ROUND(F_0_19, K_0_19); |
|
357 |
DO_ROUND(F_0_19, K_0_19); |
|
358 |
} |
|
359 |
||
360 |
for (i = 1; i >= 0; i--) { |
|
361 |
DO_ROUND(F_20_39, K_20_39); |
|
362 |
DO_ROUND(F_20_39, K_20_39); |
|
363 |
DO_ROUND(F_20_39, K_20_39); |
|
364 |
DO_ROUND(F_20_39, K_20_39); |
|
365 |
DO_ROUND(F_20_39, K_20_39); |
|
366 |
DO_ROUND(F_20_39, K_20_39); |
|
367 |
DO_ROUND(F_20_39, K_20_39); |
|
368 |
DO_ROUND(F_20_39, K_20_39); |
|
369 |
DO_ROUND(F_20_39, K_20_39); |
|
370 |
DO_ROUND(F_20_39, K_20_39); |
|
371 |
} |
|
372 |
||
373 |
for (i = 1; i >= 0; i--) { |
|
374 |
DO_ROUND(F_40_59, K_40_59); |
|
375 |
DO_ROUND(F_40_59, K_40_59); |
|
376 |
DO_ROUND(F_40_59, K_40_59); |
|
377 |
DO_ROUND(F_40_59, K_40_59); |
|
378 |
DO_ROUND(F_40_59, K_40_59); |
|
379 |
DO_ROUND(F_40_59, K_40_59); |
|
380 |
DO_ROUND(F_40_59, K_40_59); |
|
381 |
DO_ROUND(F_40_59, K_40_59); |
|
382 |
DO_ROUND(F_40_59, K_40_59); |
|
383 |
DO_ROUND(F_40_59, K_40_59); |
|
384 |
} |
|
385 |
||
386 |
for (i = 1; i >= 0; i--) { |
|
387 |
DO_ROUND(F_60_79, K_60_79); |
|
388 |
DO_ROUND(F_60_79, K_60_79); |
|
389 |
DO_ROUND(F_60_79, K_60_79); |
|
390 |
DO_ROUND(F_60_79, K_60_79); |
|
391 |
DO_ROUND(F_60_79, K_60_79); |
|
392 |
DO_ROUND(F_60_79, K_60_79); |
|
393 |
DO_ROUND(F_60_79, K_60_79); |
|
394 |
DO_ROUND(F_60_79, K_60_79); |
|
395 |
DO_ROUND(F_60_79, K_60_79); |
|
396 |
DO_ROUND(F_60_79, K_60_79); |
|
397 |
} |
|
398 |
#elif SHA1_UNROLL == 20 |
|
399 |
DO_ROUND(F_0_19, K_0_19); |
|
400 |
DO_ROUND(F_0_19, K_0_19); |
|
401 |
DO_ROUND(F_0_19, K_0_19); |
|
402 |
DO_ROUND(F_0_19, K_0_19); |
|
403 |
DO_ROUND(F_0_19, K_0_19); |
|
404 |
DO_ROUND(F_0_19, K_0_19); |
|
405 |
DO_ROUND(F_0_19, K_0_19); |
|
406 |
DO_ROUND(F_0_19, K_0_19); |
|
407 |
DO_ROUND(F_0_19, K_0_19); |
|
408 |
DO_ROUND(F_0_19, K_0_19); |
|
409 |
DO_ROUND(F_0_19, K_0_19); |
|
410 |
DO_ROUND(F_0_19, K_0_19); |
|
411 |
DO_ROUND(F_0_19, K_0_19); |
|
412 |
DO_ROUND(F_0_19, K_0_19); |
|
413 |
DO_ROUND(F_0_19, K_0_19); |
|
414 |
DO_ROUND(F_0_19, K_0_19); |
|
415 |
DO_ROUND(F_0_19, K_0_19); |
|
416 |
DO_ROUND(F_0_19, K_0_19); |
|
417 |
DO_ROUND(F_0_19, K_0_19); |
|
418 |
DO_ROUND(F_0_19, K_0_19); |
|
419 |
||
420 |
DO_ROUND(F_20_39, K_20_39); |
|
421 |
DO_ROUND(F_20_39, K_20_39); |
|
422 |
DO_ROUND(F_20_39, K_20_39); |
|
423 |
DO_ROUND(F_20_39, K_20_39); |
|
424 |
DO_ROUND(F_20_39, K_20_39); |
|
425 |
DO_ROUND(F_20_39, K_20_39); |
|
426 |
DO_ROUND(F_20_39, K_20_39); |
|
427 |
DO_ROUND(F_20_39, K_20_39); |
|
428 |
DO_ROUND(F_20_39, K_20_39); |
|
429 |
DO_ROUND(F_20_39, K_20_39); |
|
430 |
DO_ROUND(F_20_39, K_20_39); |
|
431 |
DO_ROUND(F_20_39, K_20_39); |
|
432 |
DO_ROUND(F_20_39, K_20_39); |
|
433 |
DO_ROUND(F_20_39, K_20_39); |
|
434 |
DO_ROUND(F_20_39, K_20_39); |
|
435 |
DO_ROUND(F_20_39, K_20_39); |
|
436 |
DO_ROUND(F_20_39, K_20_39); |
|
437 |
DO_ROUND(F_20_39, K_20_39); |
|
438 |
DO_ROUND(F_20_39, K_20_39); |
|
439 |
DO_ROUND(F_20_39, K_20_39); |
|
440 |
||
441 |
DO_ROUND(F_40_59, K_40_59); |
|
442 |
DO_ROUND(F_40_59, K_40_59); |
|
443 |
DO_ROUND(F_40_59, K_40_59); |
|
444 |
DO_ROUND(F_40_59, K_40_59); |
|
445 |
DO_ROUND(F_40_59, K_40_59); |
|
446 |
DO_ROUND(F_40_59, K_40_59); |
|
447 |
DO_ROUND(F_40_59, K_40_59); |
|
448 |
DO_ROUND(F_40_59, K_40_59); |
|
449 |
DO_ROUND(F_40_59, K_40_59); |
|
450 |
DO_ROUND(F_40_59, K_40_59); |
|
451 |
DO_ROUND(F_40_59, K_40_59); |
|
452 |
DO_ROUND(F_40_59, K_40_59); |
|
453 |
DO_ROUND(F_40_59, K_40_59); |
|
454 |
DO_ROUND(F_40_59, K_40_59); |
|
455 |
DO_ROUND(F_40_59, K_40_59); |
|
456 |
DO_ROUND(F_40_59, K_40_59); |
|
457 |
DO_ROUND(F_40_59, K_40_59); |
|
458 |
DO_ROUND(F_40_59, K_40_59); |
|
459 |
DO_ROUND(F_40_59, K_40_59); |
|
460 |
DO_ROUND(F_40_59, K_40_59); |
|
461 |
||
462 |
DO_ROUND(F_60_79, K_60_79); |
|
463 |
DO_ROUND(F_60_79, K_60_79); |
|
464 |
DO_ROUND(F_60_79, K_60_79); |
|
465 |
DO_ROUND(F_60_79, K_60_79); |
|
466 |
DO_ROUND(F_60_79, K_60_79); |
|
467 |
DO_ROUND(F_60_79, K_60_79); |
|
468 |
DO_ROUND(F_60_79, K_60_79); |
|
469 |
DO_ROUND(F_60_79, K_60_79); |
|
470 |
DO_ROUND(F_60_79, K_60_79); |
|
471 |
DO_ROUND(F_60_79, K_60_79); |
|
472 |
DO_ROUND(F_60_79, K_60_79); |
|
473 |
DO_ROUND(F_60_79, K_60_79); |
|
474 |
DO_ROUND(F_60_79, K_60_79); |
|
475 |
DO_ROUND(F_60_79, K_60_79); |
|
476 |
DO_ROUND(F_60_79, K_60_79); |
|
477 |
DO_ROUND(F_60_79, K_60_79); |
|
478 |
DO_ROUND(F_60_79, K_60_79); |
|
479 |
DO_ROUND(F_60_79, K_60_79); |
|
480 |
DO_ROUND(F_60_79, K_60_79); |
|
481 |
DO_ROUND(F_60_79, K_60_79); |
|
482 |
#else /* SHA1_UNROLL */ |
|
483 |
#error SHA1_UNROLL must be 1, 2, 4, 5, 10 or 20! |
|
484 |
#endif |
|
485 |
||
486 |
sc->hash[0] += a; |
|
487 |
sc->hash[1] += b; |
|
488 |
sc->hash[2] += c; |
|
489 |
sc->hash[3] += d; |
|
490 |
sc->hash[4] += e; |
|
491 |
} |
|
492 |
||
162
16d29898f97b
2006-08-03 Richard Hult <richard@imendio.com>
hallski <hallski>
parents:
140
diff
changeset
|
493 |
static void |
84
7ae7b690aa89
2004-05-28 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
9
diff
changeset
|
494 |
SHA1Update (SHA1Context *sc, const void *udata, guint32 len) |
1 | 495 |
{ |
496 |
guint32 bufferBytesLeft; |
|
497 |
guint32 bytesToCopy; |
|
498 |
int needBurn = 0; |
|
84
7ae7b690aa89
2004-05-28 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
9
diff
changeset
|
499 |
guint8 *data = (guint8 *)udata; |
1 | 500 |
|
501 |
#ifdef SHA1_FAST_COPY |
|
502 |
if (sc->bufferLength) { |
|
503 |
bufferBytesLeft = 64L - sc->bufferLength; |
|
504 |
||
505 |
bytesToCopy = bufferBytesLeft; |
|
506 |
if (bytesToCopy > len) |
|
507 |
bytesToCopy = len; |
|
508 |
||
509 |
memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); |
|
510 |
||
511 |
sc->totalLength += bytesToCopy * 8L; |
|
512 |
||
513 |
sc->bufferLength += bytesToCopy; |
|
84
7ae7b690aa89
2004-05-28 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
9
diff
changeset
|
514 |
data += bytesToCopy; |
1 | 515 |
len -= bytesToCopy; |
516 |
||
517 |
if (sc->bufferLength == 64L) { |
|
518 |
SHA1Guts (sc, sc->buffer.words); |
|
519 |
needBurn = 1; |
|
520 |
sc->bufferLength = 0L; |
|
521 |
} |
|
522 |
} |
|
523 |
||
524 |
while (len > 63) { |
|
525 |
sc->totalLength += 512L; |
|
526 |
||
527 |
SHA1Guts (sc, data); |
|
528 |
needBurn = 1; |
|
529 |
||
84
7ae7b690aa89
2004-05-28 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
9
diff
changeset
|
530 |
data += 64L; |
1 | 531 |
len -= 64L; |
532 |
} |
|
533 |
||
534 |
if (len) { |
|
535 |
memcpy (&sc->buffer.bytes[sc->bufferLength], data, len); |
|
536 |
||
537 |
sc->totalLength += len * 8L; |
|
538 |
||
539 |
sc->bufferLength += len; |
|
540 |
} |
|
541 |
#else /* SHA1_FAST_COPY */ |
|
542 |
while (len) { |
|
543 |
bufferBytesLeft = 64L - sc->bufferLength; |
|
544 |
||
545 |
bytesToCopy = bufferBytesLeft; |
|
546 |
if (bytesToCopy > len) |
|
547 |
bytesToCopy = len; |
|
548 |
||
549 |
memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); |
|
550 |
||
551 |
sc->totalLength += bytesToCopy * 8L; |
|
552 |
||
553 |
sc->bufferLength += bytesToCopy; |
|
84
7ae7b690aa89
2004-05-28 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
9
diff
changeset
|
554 |
data += bytesToCopy; |
1 | 555 |
len -= bytesToCopy; |
556 |
||
557 |
if (sc->bufferLength == 64L) { |
|
558 |
SHA1Guts (sc, sc->buffer.words); |
|
559 |
needBurn = 1; |
|
560 |
sc->bufferLength = 0L; |
|
561 |
} |
|
562 |
} |
|
563 |
#endif /* SHA1_FAST_COPY */ |
|
564 |
||
565 |
if (needBurn) |
|
566 |
burnStack (sizeof (guint32[86]) + sizeof (guint32 *[5]) + sizeof (int)); |
|
567 |
} |
|
568 |
||
162
16d29898f97b
2006-08-03 Richard Hult <richard@imendio.com>
hallski <hallski>
parents:
140
diff
changeset
|
569 |
static void |
1 | 570 |
SHA1Final (SHA1Context *sc, guint8 hash[SHA1_HASH_SIZE]) |
571 |
{ |
|
572 |
guint32 bytesToPad; |
|
573 |
guint64 lengthPad; |
|
574 |
int i; |
|
575 |
||
576 |
bytesToPad = 120L - sc->bufferLength; |
|
577 |
if (bytesToPad > 64L) |
|
578 |
bytesToPad -= 64L; |
|
579 |
||
580 |
lengthPad = BYTESWAP64(sc->totalLength); |
|
581 |
||
582 |
SHA1Update (sc, padding, bytesToPad); |
|
583 |
SHA1Update (sc, &lengthPad, 8L); |
|
584 |
||
585 |
if (hash) { |
|
586 |
for (i = 0; i < SHA1_HASH_WORDS; i++) { |
|
587 |
#ifdef SHA1_FAST_COPY |
|
588 |
*((guint32 *) hash) = BYTESWAP(sc->hash[i]); |
|
589 |
#else /* SHA1_FAST_COPY */ |
|
590 |
hash[0] = (guint8) (sc->hash[i] >> 24); |
|
591 |
hash[1] = (guint8) (sc->hash[i] >> 16); |
|
592 |
hash[2] = (guint8) (sc->hash[i] >> 8); |
|
593 |
hash[3] = (guint8) sc->hash[i]; |
|
594 |
#endif /* SHA1_FAST_COPY */ |
|
595 |
hash += 4; |
|
596 |
} |
|
597 |
} |
|
598 |
} |
|
599 |
||
140
103227122f45
2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
86
diff
changeset
|
600 |
#ifdef G_OS_WIN32 |
9
2e14e3ab411b
2003-06-24 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
1
diff
changeset
|
601 |
#define snprintf _snprintf |
2e14e3ab411b
2003-06-24 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
1
diff
changeset
|
602 |
#endif |
2e14e3ab411b
2003-06-24 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
1
diff
changeset
|
603 |
|
1 | 604 |
const gchar * |
605 |
lm_sha_hash (const gchar *str) |
|
606 |
{ |
|
607 |
static gchar ret_val[41]; |
|
608 |
SHA1Context ctx; |
|
609 |
guint8 hash[SHA1_HASH_SIZE]; |
|
610 |
gchar *ch; |
|
611 |
guint i; |
|
612 |
||
613 |
SHA1Init (&ctx); |
|
614 |
SHA1Update (&ctx, str, strlen (str)); |
|
615 |
SHA1Final (&ctx, hash); |
|
616 |
||
617 |
ch = ret_val; |
|
618 |
||
619 |
for (i = 0; i < SHA1_HASH_SIZE; ++i) { |
|
620 |
snprintf (ch, 3, "%02x", hash[i]); |
|
621 |
ch += 2; |
|
622 |
} |
|
623 |
||
624 |
return (const gchar *) ret_val; |
|
625 |
} |