|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 /* (PD) 2001 The Bitzi Corporation |
|
18 * Please see file COPYING or http://bitzi.com/publicdomain |
|
19 * for more info. |
|
20 * |
|
21 * NIST Secure Hash Algorithm |
|
22 * heavily modified by Uwe Hollerbach <uh@alumni.caltech edu> |
|
23 * from Peter C. Gutmann's implementation as found in |
|
24 * Applied Cryptography by Bruce Schneier |
|
25 * Further modifications to include the "UNRAVEL" stuff, below |
|
26 * |
|
27 * This code is in the public domain |
|
28 * |
|
29 * $Id: sha1.c,v 1.2 2008-02-27 10:42:08 slomo Exp $ |
|
30 */ |
|
31 |
|
32 #ifdef HAVE_CONFIG_H |
|
33 #include "config.h" |
|
34 #endif |
|
35 #include <glib.h> |
|
36 #define SHA_BYTE_ORDER G_BYTE_ORDER |
|
37 |
|
38 #include <string.h> |
|
39 #include "sha1.h" |
|
40 |
|
41 /* UNRAVEL should be fastest & biggest */ |
|
42 /* UNROLL_LOOPS should be just as big, but slightly slower */ |
|
43 /* both undefined should be smallest and slowest */ |
|
44 |
|
45 #define UNRAVEL |
|
46 /* #define UNROLL_LOOPS */ |
|
47 |
|
48 /* SHA f()-functions */ |
|
49 |
|
50 #define f1(x,y,z) ((x & y) | (~x & z)) |
|
51 #define f2(x,y,z) (x ^ y ^ z) |
|
52 #define f3(x,y,z) ((x & y) | (x & z) | (y & z)) |
|
53 #define f4(x,y,z) (x ^ y ^ z) |
|
54 |
|
55 /* SHA constants */ |
|
56 |
|
57 #define CONST1 0x5a827999L |
|
58 #define CONST2 0x6ed9eba1L |
|
59 #define CONST3 0x8f1bbcdcL |
|
60 #define CONST4 0xca62c1d6L |
|
61 |
|
62 /* truncate to 32 bits -- should be a null op on 32-bit machines */ |
|
63 |
|
64 #define T32(x) ((x) & 0xffffffffL) |
|
65 |
|
66 /* 32-bit rotate */ |
|
67 |
|
68 #define R32(x,n) T32(((x << n) | (x >> (32 - n)))) |
|
69 |
|
70 /* the generic case, for when the overall rotation is not unraveled */ |
|
71 |
|
72 #define FG(n) \ |
|
73 T = T32(R32(A,5) + f##n(B,C,D) + E + *WP++ + CONST##n); \ |
|
74 E = D; D = C; C = R32(B,30); B = A; A = T |
|
75 |
|
76 /* specific cases, for when the overall rotation is unraveled */ |
|
77 |
|
78 #define FA(n) \ |
|
79 T = T32(R32(A,5) + f##n(B,C,D) + E + *WP++ + CONST##n); B = R32(B,30) |
|
80 |
|
81 #define FB(n) \ |
|
82 E = T32(R32(T,5) + f##n(A,B,C) + D + *WP++ + CONST##n); A = R32(A,30) |
|
83 |
|
84 #define FC(n) \ |
|
85 D = T32(R32(E,5) + f##n(T,A,B) + C + *WP++ + CONST##n); T = R32(T,30) |
|
86 |
|
87 #define FD(n) \ |
|
88 C = T32(R32(D,5) + f##n(E,T,A) + B + *WP++ + CONST##n); E = R32(E,30) |
|
89 |
|
90 #define FE(n) \ |
|
91 B = T32(R32(C,5) + f##n(D,E,T) + A + *WP++ + CONST##n); D = R32(D,30) |
|
92 |
|
93 #define FT(n) \ |
|
94 A = T32(R32(B,5) + f##n(C,D,E) + T + *WP++ + CONST##n); C = R32(C,30) |
|
95 |
|
96 /* do SHA transformation */ |
|
97 |
|
98 static void |
|
99 sha_transform (SHA_INFO * sha_info) |
|
100 { |
|
101 int i; |
|
102 SHA_BYTE *dp; |
|
103 SHA_LONG T, A, B, C, D, E, W[80], *WP; |
|
104 |
|
105 dp = sha_info->data; |
|
106 |
|
107 /* |
|
108 the following makes sure that at least one code block below is |
|
109 traversed or an error is reported, without the necessity for nested |
|
110 preprocessor if/else/endif blocks, which are a great pain in the |
|
111 nether regions of the anatomy... |
|
112 */ |
|
113 #undef SWAP_DONE |
|
114 |
|
115 #if (SHA_BYTE_ORDER == 1234) |
|
116 #define SWAP_DONE |
|
117 for (i = 0; i < 16; ++i) { |
|
118 memcpy (&T, dp, sizeof (SHA_LONG)); |
|
119 dp += 4; |
|
120 W[i] = ((T << 24) & 0xff000000) | ((T << 8) & 0x00ff0000) | |
|
121 ((T >> 8) & 0x0000ff00) | ((T >> 24) & 0x000000ff); |
|
122 } |
|
123 #endif /* SHA_BYTE_ORDER == 1234 */ |
|
124 |
|
125 #if (SHA_BYTE_ORDER == 4321) |
|
126 #define SWAP_DONE |
|
127 for (i = 0; i < 16; ++i) { |
|
128 memcpy (&T, dp, sizeof (SHA_LONG)); |
|
129 dp += 4; |
|
130 W[i] = T32 (T); |
|
131 } |
|
132 #endif /* SHA_BYTE_ORDER == 4321 */ |
|
133 |
|
134 #if (SHA_BYTE_ORDER == 12345678) |
|
135 #define SWAP_DONE |
|
136 for (i = 0; i < 16; i += 2) { |
|
137 memcpy (&T, dp, sizeof (SHA_LONG)); |
|
138 dp += 8; |
|
139 W[i] = ((T << 24) & 0xff000000) | ((T << 8) & 0x00ff0000) | |
|
140 ((T >> 8) & 0x0000ff00) | ((T >> 24) & 0x000000ff); |
|
141 T >>= 32; |
|
142 W[i + 1] = ((T << 24) & 0xff000000) | ((T << 8) & 0x00ff0000) | |
|
143 ((T >> 8) & 0x0000ff00) | ((T >> 24) & 0x000000ff); |
|
144 } |
|
145 #endif /* SHA_BYTE_ORDER == 12345678 */ |
|
146 |
|
147 #if (SHA_BYTE_ORDER == 87654321) |
|
148 #define SWAP_DONE |
|
149 for (i = 0; i < 16; i += 2) { |
|
150 memcpy (&T, dp, sizeof (SHA_LONG)); |
|
151 dp += 8; |
|
152 W[i] = T32 (T >> 32); |
|
153 W[i + 1] = T32 (T); |
|
154 } |
|
155 #endif /* SHA_BYTE_ORDER == 87654321 */ |
|
156 |
|
157 #ifndef SWAP_DONE |
|
158 #error Unknown byte order -- you need to add code here |
|
159 #endif /* SWAP_DONE */ |
|
160 |
|
161 for (i = 16; i < 80; ++i) { |
|
162 W[i] = W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16]; |
|
163 #if (SHA_VERSION == 1) |
|
164 W[i] = R32 (W[i], 1); |
|
165 #endif /* SHA_VERSION */ |
|
166 } |
|
167 A = sha_info->digest[0]; |
|
168 B = sha_info->digest[1]; |
|
169 C = sha_info->digest[2]; |
|
170 D = sha_info->digest[3]; |
|
171 E = sha_info->digest[4]; |
|
172 WP = W; |
|
173 #ifdef UNRAVEL |
|
174 FA (1); |
|
175 FB (1); |
|
176 FC (1); |
|
177 FD (1); |
|
178 FE (1); |
|
179 FT (1); |
|
180 FA (1); |
|
181 FB (1); |
|
182 FC (1); |
|
183 FD (1); |
|
184 FE (1); |
|
185 FT (1); |
|
186 FA (1); |
|
187 FB (1); |
|
188 FC (1); |
|
189 FD (1); |
|
190 FE (1); |
|
191 FT (1); |
|
192 FA (1); |
|
193 FB (1); |
|
194 FC (2); |
|
195 FD (2); |
|
196 FE (2); |
|
197 FT (2); |
|
198 FA (2); |
|
199 FB (2); |
|
200 FC (2); |
|
201 FD (2); |
|
202 FE (2); |
|
203 FT (2); |
|
204 FA (2); |
|
205 FB (2); |
|
206 FC (2); |
|
207 FD (2); |
|
208 FE (2); |
|
209 FT (2); |
|
210 FA (2); |
|
211 FB (2); |
|
212 FC (2); |
|
213 FD (2); |
|
214 FE (3); |
|
215 FT (3); |
|
216 FA (3); |
|
217 FB (3); |
|
218 FC (3); |
|
219 FD (3); |
|
220 FE (3); |
|
221 FT (3); |
|
222 FA (3); |
|
223 FB (3); |
|
224 FC (3); |
|
225 FD (3); |
|
226 FE (3); |
|
227 FT (3); |
|
228 FA (3); |
|
229 FB (3); |
|
230 FC (3); |
|
231 FD (3); |
|
232 FE (3); |
|
233 FT (3); |
|
234 FA (4); |
|
235 FB (4); |
|
236 FC (4); |
|
237 FD (4); |
|
238 FE (4); |
|
239 FT (4); |
|
240 FA (4); |
|
241 FB (4); |
|
242 FC (4); |
|
243 FD (4); |
|
244 FE (4); |
|
245 FT (4); |
|
246 FA (4); |
|
247 FB (4); |
|
248 FC (4); |
|
249 FD (4); |
|
250 FE (4); |
|
251 FT (4); |
|
252 FA (4); |
|
253 FB (4); |
|
254 sha_info->digest[0] = T32 (sha_info->digest[0] + E); |
|
255 sha_info->digest[1] = T32 (sha_info->digest[1] + T); |
|
256 sha_info->digest[2] = T32 (sha_info->digest[2] + A); |
|
257 sha_info->digest[3] = T32 (sha_info->digest[3] + B); |
|
258 sha_info->digest[4] = T32 (sha_info->digest[4] + C); |
|
259 #else /* !UNRAVEL */ |
|
260 #ifdef UNROLL_LOOPS |
|
261 FG (1); |
|
262 FG (1); |
|
263 FG (1); |
|
264 FG (1); |
|
265 FG (1); |
|
266 FG (1); |
|
267 FG (1); |
|
268 FG (1); |
|
269 FG (1); |
|
270 FG (1); |
|
271 FG (1); |
|
272 FG (1); |
|
273 FG (1); |
|
274 FG (1); |
|
275 FG (1); |
|
276 FG (1); |
|
277 FG (1); |
|
278 FG (1); |
|
279 FG (1); |
|
280 FG (1); |
|
281 FG (2); |
|
282 FG (2); |
|
283 FG (2); |
|
284 FG (2); |
|
285 FG (2); |
|
286 FG (2); |
|
287 FG (2); |
|
288 FG (2); |
|
289 FG (2); |
|
290 FG (2); |
|
291 FG (2); |
|
292 FG (2); |
|
293 FG (2); |
|
294 FG (2); |
|
295 FG (2); |
|
296 FG (2); |
|
297 FG (2); |
|
298 FG (2); |
|
299 FG (2); |
|
300 FG (2); |
|
301 FG (3); |
|
302 FG (3); |
|
303 FG (3); |
|
304 FG (3); |
|
305 FG (3); |
|
306 FG (3); |
|
307 FG (3); |
|
308 FG (3); |
|
309 FG (3); |
|
310 FG (3); |
|
311 FG (3); |
|
312 FG (3); |
|
313 FG (3); |
|
314 FG (3); |
|
315 FG (3); |
|
316 FG (3); |
|
317 FG (3); |
|
318 FG (3); |
|
319 FG (3); |
|
320 FG (3); |
|
321 FG (4); |
|
322 FG (4); |
|
323 FG (4); |
|
324 FG (4); |
|
325 FG (4); |
|
326 FG (4); |
|
327 FG (4); |
|
328 FG (4); |
|
329 FG (4); |
|
330 FG (4); |
|
331 FG (4); |
|
332 FG (4); |
|
333 FG (4); |
|
334 FG (4); |
|
335 FG (4); |
|
336 FG (4); |
|
337 FG (4); |
|
338 FG (4); |
|
339 FG (4); |
|
340 FG (4); |
|
341 #else /* !UNROLL_LOOPS */ |
|
342 for (i = 0; i < 20; ++i) { |
|
343 FG (1); |
|
344 } |
|
345 for (i = 20; i < 40; ++i) { |
|
346 FG (2); |
|
347 } |
|
348 for (i = 40; i < 60; ++i) { |
|
349 FG (3); |
|
350 } |
|
351 for (i = 60; i < 80; ++i) { |
|
352 FG (4); |
|
353 } |
|
354 #endif /* !UNROLL_LOOPS */ |
|
355 sha_info->digest[0] = T32 (sha_info->digest[0] + A); |
|
356 sha_info->digest[1] = T32 (sha_info->digest[1] + B); |
|
357 sha_info->digest[2] = T32 (sha_info->digest[2] + C); |
|
358 sha_info->digest[3] = T32 (sha_info->digest[3] + D); |
|
359 sha_info->digest[4] = T32 (sha_info->digest[4] + E); |
|
360 #endif /* !UNRAVEL */ |
|
361 } |
|
362 |
|
363 /* initialize the SHA digest */ |
|
364 |
|
365 void |
|
366 sha_init (SHA_INFO * sha_info) |
|
367 { |
|
368 sha_info->digest[0] = 0x67452301L; |
|
369 sha_info->digest[1] = 0xefcdab89L; |
|
370 sha_info->digest[2] = 0x98badcfeL; |
|
371 sha_info->digest[3] = 0x10325476L; |
|
372 sha_info->digest[4] = 0xc3d2e1f0L; |
|
373 sha_info->count_lo = 0L; |
|
374 sha_info->count_hi = 0L; |
|
375 sha_info->local = 0; |
|
376 } |
|
377 |
|
378 /* update the SHA digest */ |
|
379 |
|
380 void |
|
381 sha_update (SHA_INFO * sha_info, SHA_BYTE * buffer, int count) |
|
382 { |
|
383 int i; |
|
384 SHA_LONG clo; |
|
385 |
|
386 clo = T32 (sha_info->count_lo + ((SHA_LONG) count << 3)); |
|
387 if (clo < sha_info->count_lo) { |
|
388 ++sha_info->count_hi; |
|
389 } |
|
390 sha_info->count_lo = clo; |
|
391 sha_info->count_hi += (SHA_LONG) count >> 29; |
|
392 if (sha_info->local) { |
|
393 i = SHA_BLOCKSIZE - sha_info->local; |
|
394 if (i > count) { |
|
395 i = count; |
|
396 } |
|
397 memcpy (((SHA_BYTE *) sha_info->data) + sha_info->local, buffer, i); |
|
398 count -= i; |
|
399 buffer += i; |
|
400 sha_info->local += i; |
|
401 if (sha_info->local == SHA_BLOCKSIZE) { |
|
402 sha_transform (sha_info); |
|
403 } else { |
|
404 return; |
|
405 } |
|
406 } |
|
407 while (count >= SHA_BLOCKSIZE) { |
|
408 memcpy (sha_info->data, buffer, SHA_BLOCKSIZE); |
|
409 buffer += SHA_BLOCKSIZE; |
|
410 count -= SHA_BLOCKSIZE; |
|
411 sha_transform (sha_info); |
|
412 } |
|
413 memcpy (sha_info->data, buffer, count); |
|
414 sha_info->local = count; |
|
415 } |
|
416 |
|
417 /* finish computing the SHA digest */ |
|
418 |
|
419 void |
|
420 sha_final (unsigned char digest[20], SHA_INFO * sha_info) |
|
421 { |
|
422 int count; |
|
423 SHA_LONG lo_bit_count, hi_bit_count; |
|
424 |
|
425 lo_bit_count = sha_info->count_lo; |
|
426 hi_bit_count = sha_info->count_hi; |
|
427 count = (int) ((lo_bit_count >> 3) & 0x3f); |
|
428 ((SHA_BYTE *) sha_info->data)[count++] = 0x80; |
|
429 if (count > SHA_BLOCKSIZE - 8) { |
|
430 memset (((SHA_BYTE *) sha_info->data) + count, 0, SHA_BLOCKSIZE - count); |
|
431 sha_transform (sha_info); |
|
432 memset ((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 8); |
|
433 } else { |
|
434 memset (((SHA_BYTE *) sha_info->data) + count, 0, |
|
435 SHA_BLOCKSIZE - 8 - count); |
|
436 } |
|
437 sha_info->data[56] = (unsigned char) ((hi_bit_count >> 24) & 0xff); |
|
438 sha_info->data[57] = (unsigned char) ((hi_bit_count >> 16) & 0xff); |
|
439 sha_info->data[58] = (unsigned char) ((hi_bit_count >> 8) & 0xff); |
|
440 sha_info->data[59] = (unsigned char) ((hi_bit_count >> 0) & 0xff); |
|
441 sha_info->data[60] = (unsigned char) ((lo_bit_count >> 24) & 0xff); |
|
442 sha_info->data[61] = (unsigned char) ((lo_bit_count >> 16) & 0xff); |
|
443 sha_info->data[62] = (unsigned char) ((lo_bit_count >> 8) & 0xff); |
|
444 sha_info->data[63] = (unsigned char) ((lo_bit_count >> 0) & 0xff); |
|
445 sha_transform (sha_info); |
|
446 digest[0] = (unsigned char) ((sha_info->digest[0] >> 24) & 0xff); |
|
447 digest[1] = (unsigned char) ((sha_info->digest[0] >> 16) & 0xff); |
|
448 digest[2] = (unsigned char) ((sha_info->digest[0] >> 8) & 0xff); |
|
449 digest[3] = (unsigned char) ((sha_info->digest[0]) & 0xff); |
|
450 digest[4] = (unsigned char) ((sha_info->digest[1] >> 24) & 0xff); |
|
451 digest[5] = (unsigned char) ((sha_info->digest[1] >> 16) & 0xff); |
|
452 digest[6] = (unsigned char) ((sha_info->digest[1] >> 8) & 0xff); |
|
453 digest[7] = (unsigned char) ((sha_info->digest[1]) & 0xff); |
|
454 digest[8] = (unsigned char) ((sha_info->digest[2] >> 24) & 0xff); |
|
455 digest[9] = (unsigned char) ((sha_info->digest[2] >> 16) & 0xff); |
|
456 digest[10] = (unsigned char) ((sha_info->digest[2] >> 8) & 0xff); |
|
457 digest[11] = (unsigned char) ((sha_info->digest[2]) & 0xff); |
|
458 digest[12] = (unsigned char) ((sha_info->digest[3] >> 24) & 0xff); |
|
459 digest[13] = (unsigned char) ((sha_info->digest[3] >> 16) & 0xff); |
|
460 digest[14] = (unsigned char) ((sha_info->digest[3] >> 8) & 0xff); |
|
461 digest[15] = (unsigned char) ((sha_info->digest[3]) & 0xff); |
|
462 digest[16] = (unsigned char) ((sha_info->digest[4] >> 24) & 0xff); |
|
463 digest[17] = (unsigned char) ((sha_info->digest[4] >> 16) & 0xff); |
|
464 digest[18] = (unsigned char) ((sha_info->digest[4] >> 8) & 0xff); |
|
465 digest[19] = (unsigned char) ((sha_info->digest[4]) & 0xff); |
|
466 } |