1 | #include "config_xor.h"
|
---|
2 |
|
---|
3 | #define USE_MD5
|
---|
4 | #define USE_SHA1
|
---|
5 |
|
---|
6 | #include <stdio.h>
|
---|
7 | #include <string.h>
|
---|
8 |
|
---|
9 | #include <sys/types.h>
|
---|
10 | #ifdef HAVE_UNISTD_H
|
---|
11 | #include <unistd.h>
|
---|
12 | #endif
|
---|
13 | #ifdef HAVE_MEMORY_H
|
---|
14 | #include <memory.h>
|
---|
15 | #endif
|
---|
16 |
|
---|
17 | #if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
|
---|
18 | #include <sys/mman.h>
|
---|
19 | #endif
|
---|
20 |
|
---|
21 | #include "sh_tiger.h"
|
---|
22 |
|
---|
23 | #include "sh_unix.h"
|
---|
24 | #include "sh_error.h"
|
---|
25 | #include "sh_utils.h"
|
---|
26 |
|
---|
27 | #define PRIV_MAX 32768
|
---|
28 |
|
---|
29 | #if defined(TIGER_64_BIT)
|
---|
30 | #if defined(HAVE_LONG_64)
|
---|
31 | typedef unsigned long int word64;
|
---|
32 | #elif defined(HAVE_LONG_LONG_64)
|
---|
33 | typedef unsigned long long int word64;
|
---|
34 | #else
|
---|
35 | #error No 64 bit type found !
|
---|
36 | #endif
|
---|
37 | #endif
|
---|
38 |
|
---|
39 | #if defined(HAVE_INT_32)
|
---|
40 | typedef unsigned int sh_word32;
|
---|
41 | #define MYFORMAT (_("%08X%08X%08X%08X%08X%08X"))
|
---|
42 | #define GPGFORMAT (_("%08X %08X %08X %08X %08X %08X"))
|
---|
43 | #elif defined(HAVE_LONG_32)
|
---|
44 | typedef unsigned long sh_word32;
|
---|
45 | #define MYFORMAT (_("%08lX%08lX%08lX%08lX%08lX%08lX"))
|
---|
46 | #define GPGFORMAT (_("%08lX %08lX %08lX %08lX %08lX %08lX"))
|
---|
47 | #elif defined(HAVE_SHORT_32)
|
---|
48 | typedef unsigned short sh_word32;
|
---|
49 | #define MYFORMAT (_("%08X%08X%08X%08X%08X%08X"))
|
---|
50 | #define GPGFORMAT (_("%08X %08X %08X %08X %08X %08X"))
|
---|
51 | #else
|
---|
52 | #error No 32 bit type found !
|
---|
53 | #endif
|
---|
54 |
|
---|
55 | typedef unsigned char sh_byte;
|
---|
56 |
|
---|
57 | #define SH_KEY_NULL _("000000000000000000000000000000000000000000000000")
|
---|
58 |
|
---|
59 | #undef FIL__
|
---|
60 | #define FIL__ _("sh_tiger0.c")
|
---|
61 |
|
---|
62 | #if defined(TIGER_64_BIT)
|
---|
63 |
|
---|
64 | void tiger_t(word64 *str, word64 length, word64 * res);
|
---|
65 | void tiger(word64 *str, word64 length, word64 * res);
|
---|
66 |
|
---|
67 | #ifdef TIGER_DBG
|
---|
68 | static void tiger_dbg(word64 res[3], int step,
|
---|
69 | unsigned long nblocks, unsigned long ncount)
|
---|
70 | {
|
---|
71 | return;
|
---|
72 | }
|
---|
73 | #endif
|
---|
74 | #else
|
---|
75 | void tiger(sh_word32 *str, sh_word32 length, sh_word32 * res);
|
---|
76 | void tiger_t(sh_word32 *str, sh_word32 length, sh_word32 * res);
|
---|
77 |
|
---|
78 | #ifdef TIGER_DBG
|
---|
79 | static
|
---|
80 | void tiger_dbg(sh_word32 res[6], int step,
|
---|
81 | unsigned long nblocks, unsigned long ncount)
|
---|
82 | {
|
---|
83 | fprintf(stderr,
|
---|
84 | _("ST %d BLK %2ld CT %2ld %08lX %08lX %08lX %08lX %08lX %08lX\n"),
|
---|
85 | step,
|
---|
86 | nblocks,
|
---|
87 | ncount,
|
---|
88 | (sh_word32)(res[1]),
|
---|
89 | (sh_word32)(res[0]),
|
---|
90 | (sh_word32)(res[3]),
|
---|
91 | (sh_word32)(res[2]),
|
---|
92 | (sh_word32)(res[5]),
|
---|
93 | (sh_word32)(res[4]) );
|
---|
94 | }
|
---|
95 | #endif
|
---|
96 | #endif
|
---|
97 |
|
---|
98 | /* this is the wrapper function -- not part of the tiger reference
|
---|
99 | * implementation
|
---|
100 | */
|
---|
101 |
|
---|
102 | static sh_byte buffer[PRIV_MAX + 72];
|
---|
103 |
|
---|
104 | #if defined(TIGER_64_BIT)
|
---|
105 | static
|
---|
106 | word64 * sh_tiger_hash_val (const char * filename, TigerType what,
|
---|
107 | UINT64 Length, int timeout, word64 * res)
|
---|
108 | #else
|
---|
109 | static
|
---|
110 | sh_word32 * sh_tiger_hash_val (const char * filename, TigerType what,
|
---|
111 | UINT64 Length, int timeout, sh_word32 * res)
|
---|
112 | #endif
|
---|
113 | {
|
---|
114 | SL_TICKET fd;
|
---|
115 | int i, j, tt;
|
---|
116 | int count = 0;
|
---|
117 | int blk;
|
---|
118 | char * tmp;
|
---|
119 | sh_byte * bptr;
|
---|
120 | sh_byte bbuf[64];
|
---|
121 | UINT64 bcount = 0;
|
---|
122 |
|
---|
123 | static int lockflag = SL_FALSE;
|
---|
124 |
|
---|
125 | unsigned long pages_read;
|
---|
126 | uid_t euid;
|
---|
127 |
|
---|
128 | unsigned long ncount = 0, nblocks = 0;
|
---|
129 | unsigned long t, msb, lsb;
|
---|
130 |
|
---|
131 | #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
|
---|
132 | /*@-nestedextern@*/
|
---|
133 | extern long IO_Limit;
|
---|
134 | /*@+nestedextern@*/
|
---|
135 | #endif
|
---|
136 |
|
---|
137 | #if defined(TIGER_64_BIT)
|
---|
138 | #define TIGER_CAST (word64*)
|
---|
139 | /* word64 res[3]; */
|
---|
140 | res[0]= (word64) 0x0123456789ABCDEFLL;
|
---|
141 | res[1]= (word64) 0xFEDCBA9876543210LL;
|
---|
142 | res[2]= (word64) 0xF096A5B4C3B2E187LL;
|
---|
143 | #else
|
---|
144 | #define TIGER_CAST (sh_word32*)
|
---|
145 | /* sh_word32 res[6]; */
|
---|
146 | res[0]= (sh_word32) 0x89ABCDEF;
|
---|
147 | res[1]= (sh_word32) 0x01234567;
|
---|
148 | res[2]= (sh_word32) 0x76543210;
|
---|
149 | res[3]= (sh_word32) 0xFEDCBA98;
|
---|
150 | res[4]= (sh_word32) 0xC3B2E187;
|
---|
151 | res[5]= (sh_word32) 0xF096A5B4;
|
---|
152 | #endif
|
---|
153 |
|
---|
154 | SL_ENTER(_("sh_tiger_hash_val"));
|
---|
155 |
|
---|
156 | if (what >= TIGER_FILE)
|
---|
157 | {
|
---|
158 | if (what > TIGER_FD)
|
---|
159 | {
|
---|
160 | fd = what;
|
---|
161 | TPT((0,FIL__, __LINE__, _("msg=<TIGER_FD>, fd=<%ld>\n"), fd));
|
---|
162 | }
|
---|
163 | else
|
---|
164 | {
|
---|
165 | TPT((0,FIL__, __LINE__, _("msg=<TIGER_FILE>, path=<%s>\n"),
|
---|
166 | (filename == NULL ? _("(null)") : filename) ));
|
---|
167 | fd = sl_open_read (filename, SL_YESPRIV);
|
---|
168 | }
|
---|
169 |
|
---|
170 | if (SL_ISERROR (fd))
|
---|
171 | {
|
---|
172 | TPT((0, FIL__, __LINE__, _("msg=<SL_ISERROR (%ld)>\n"), fd));
|
---|
173 | tmp = sh_util_safe_name (filename);
|
---|
174 | (void) sl_get_euid(&euid);
|
---|
175 | sh_error_handle (ShDFLevel[SH_ERR_T_FILE], FIL__, __LINE__, (int)fd,
|
---|
176 | MSG_E_ACCESS, (long) euid, tmp);
|
---|
177 | SH_FREE(tmp);
|
---|
178 | SL_RETURN( NULL, _("sh_tiger_hash_val"));
|
---|
179 | }
|
---|
180 |
|
---|
181 | SH_MUTEX_LOCK(mutex_skey);
|
---|
182 | #if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
|
---|
183 | if ((lockflag == SL_FALSE) && (skey->mlock_failed == SL_FALSE))
|
---|
184 | {
|
---|
185 | lockflag = SL_TRUE;
|
---|
186 | if ( (-1) == sh_unix_mlock(FIL__, __LINE__,
|
---|
187 | (char *)buffer,
|
---|
188 | (PRIV_MAX)*sizeof(sh_byte)))
|
---|
189 | skey->mlock_failed = SL_TRUE;
|
---|
190 | }
|
---|
191 | #else
|
---|
192 | if (lockflag == SL_FALSE && skey->mlock_failed == SL_FALSE)
|
---|
193 | {
|
---|
194 | lockflag = SL_TRUE;
|
---|
195 | skey->mlock_failed = SL_TRUE;
|
---|
196 | }
|
---|
197 | #endif
|
---|
198 | SH_MUTEX_UNLOCK(mutex_skey);
|
---|
199 |
|
---|
200 | #ifdef TIGER_DBG
|
---|
201 | tiger_dbg (res, 0, nblocks, ncount);
|
---|
202 | #endif
|
---|
203 |
|
---|
204 | pages_read = 0;
|
---|
205 |
|
---|
206 | while (1)
|
---|
207 | {
|
---|
208 | if (timeout > 0)
|
---|
209 | count = sl_read_timeout (fd, buffer, PRIV_MAX, timeout, SL_TRUE);
|
---|
210 | else
|
---|
211 | count = sl_read (fd, buffer, PRIV_MAX);
|
---|
212 |
|
---|
213 | ++pages_read;
|
---|
214 |
|
---|
215 | if (SL_ISERROR (count))
|
---|
216 | {
|
---|
217 | if (sig_termfast == 1) {
|
---|
218 | SL_RETURN( NULL, _("sh_tiger_hash_val"));
|
---|
219 | }
|
---|
220 | TPT((0, FIL__ , __LINE__ , _("msg=<SL_ISERROR (%ld)>\n"), count));
|
---|
221 | tmp = sh_util_safe_name (filename);
|
---|
222 | if (count == SL_TIMEOUT)
|
---|
223 | {
|
---|
224 | if (timeout != 7) {
|
---|
225 | sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, count,
|
---|
226 | MSG_E_TIMEOUT, timeout, tmp);
|
---|
227 | }
|
---|
228 | }
|
---|
229 | else
|
---|
230 | sh_error_handle (ShDFLevel[SH_ERR_T_FILE], FIL__, __LINE__,
|
---|
231 | count, MSG_E_READ, tmp);
|
---|
232 | SH_FREE(tmp);
|
---|
233 | memset (bbuf, 0, 64);
|
---|
234 | memset (buffer, 0, PRIV_MAX);
|
---|
235 |
|
---|
236 | SL_RETURN( NULL, _("sh_tiger_hash_val"));
|
---|
237 | }
|
---|
238 |
|
---|
239 | if (Length != TIGER_NOLIM)
|
---|
240 | {
|
---|
241 | bcount += count;
|
---|
242 | if (bcount > Length)
|
---|
243 | count = count - (bcount - Length);
|
---|
244 | count = (count < 0) ? 0 : count;
|
---|
245 | }
|
---|
246 |
|
---|
247 | blk = (count / 64); /* number of 64-byte words */
|
---|
248 |
|
---|
249 | /* nblocks += blk; number of 64-byte words
|
---|
250 | * count cannot be negative here, see 'if (SL_ISERROR (count))'
|
---|
251 | */
|
---|
252 | tt = blk*64;
|
---|
253 |
|
---|
254 | ncount = (unsigned long) (count - tt);
|
---|
255 |
|
---|
256 | nblocks += blk;
|
---|
257 | /* MAY_LOCK */
|
---|
258 | sh.statistics.bytes_hashed += tt;
|
---|
259 |
|
---|
260 | bptr = buffer; tt = 0;
|
---|
261 | for (i = 0; i < blk; ++i)
|
---|
262 | {
|
---|
263 | bptr = &buffer[tt]; tt += 64;
|
---|
264 |
|
---|
265 | tiger_t(TIGER_CAST bptr, 64, res);
|
---|
266 |
|
---|
267 | #ifdef TIGER_DBG
|
---|
268 | tiger_dbg (res, 3, nblocks, ncount);
|
---|
269 | #endif
|
---|
270 | }
|
---|
271 |
|
---|
272 | if (blk < (PRIV_MAX / 64)) /* this must be (PRIV_MAX / 64) */
|
---|
273 | break;
|
---|
274 |
|
---|
275 | #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
|
---|
276 | if (sig_termfast == 1)
|
---|
277 | {
|
---|
278 | memset (bbuf, 0, 64);
|
---|
279 | memset (buffer, 0, PRIV_MAX);
|
---|
280 |
|
---|
281 | SL_RETURN( NULL, _("sh_tiger_hash_val"));
|
---|
282 | }
|
---|
283 | if ((IO_Limit) > 0 && (pages_read == 32)) /* check for I/O limit */
|
---|
284 | {
|
---|
285 | sh_unix_io_pause ();
|
---|
286 | pages_read = 0;
|
---|
287 | }
|
---|
288 | #endif
|
---|
289 | }
|
---|
290 |
|
---|
291 | TPT((0, FIL__, __LINE__ , _("msg=<Last block.>\n")));
|
---|
292 |
|
---|
293 | /* copy incomplete block
|
---|
294 | */
|
---|
295 | j = 0;
|
---|
296 | for (i = 0; i < 64; i += 4)
|
---|
297 | {
|
---|
298 | bbuf[i] = (sh_byte) '\0';
|
---|
299 | bbuf[i+1] = (sh_byte) '\0';
|
---|
300 | bbuf[i+2] = (sh_byte) '\0';
|
---|
301 | bbuf[i+3] = (sh_byte) '\0';
|
---|
302 | }
|
---|
303 | for (i = (count/64) * 64; i < count; ++i)
|
---|
304 | /*@-usedef@*/bbuf[j++] = buffer[i];/*@+usedef@*/
|
---|
305 |
|
---|
306 | #ifdef TIGER_DBG
|
---|
307 | tiger_dbg (res, 5, nblocks, ncount);
|
---|
308 | #endif
|
---|
309 |
|
---|
310 | msb = 0;
|
---|
311 | t = nblocks;
|
---|
312 | if( (lsb = t << 6) < t )
|
---|
313 | msb++;
|
---|
314 | msb += t >> 26;
|
---|
315 | t = lsb;
|
---|
316 | if( (lsb = t + ncount) < t )
|
---|
317 | msb++;
|
---|
318 | t = lsb;
|
---|
319 | if( (lsb = t << 3) < t )
|
---|
320 | msb++;
|
---|
321 | msb += t >> 29;
|
---|
322 |
|
---|
323 | if( j < 56 )
|
---|
324 | {
|
---|
325 | bbuf[j++] = (sh_byte) 0x01; ++ncount;
|
---|
326 | while( j < 56 )
|
---|
327 | { bbuf[j++] = (sh_byte) 0; ++ncount; }
|
---|
328 | }
|
---|
329 | else
|
---|
330 | {
|
---|
331 | bbuf[j++] = (sh_byte) 0x01;
|
---|
332 | while( j < 64 )
|
---|
333 | bbuf[j++] = (sh_byte) 0;
|
---|
334 | tiger_t(TIGER_CAST bbuf, 64, res);
|
---|
335 | /* MAY_LOCK */
|
---|
336 | sh.statistics.bytes_hashed += 64;
|
---|
337 | ++nblocks; ncount = 0;
|
---|
338 | sl_memset(bbuf, 0, 56 );
|
---|
339 | }
|
---|
340 |
|
---|
341 | #ifdef TIGER_DBG
|
---|
342 | tiger_dbg (res, 6, nblocks, ncount);
|
---|
343 | #endif
|
---|
344 |
|
---|
345 | bbuf[56] = (sh_byte) (lsb );
|
---|
346 | bbuf[57] = (sh_byte) (lsb >> 8);
|
---|
347 | bbuf[58] = (sh_byte) (lsb >> 16);
|
---|
348 | bbuf[59] = (sh_byte) (lsb >> 24);
|
---|
349 | bbuf[60] = (sh_byte) (msb );
|
---|
350 | bbuf[61] = (sh_byte) (msb >> 8);
|
---|
351 | bbuf[62] = (sh_byte) (msb >> 16);
|
---|
352 | bbuf[63] = (sh_byte) (msb >> 24);
|
---|
353 |
|
---|
354 | tiger_t(TIGER_CAST bbuf, 64, res);
|
---|
355 | sh.statistics.bytes_hashed += 64;
|
---|
356 |
|
---|
357 | #ifdef TIGER_DBG
|
---|
358 | tiger_dbg (res, 7, nblocks, ncount);
|
---|
359 | #endif
|
---|
360 |
|
---|
361 | sl_memset (bbuf, '\0', sizeof(bbuf));
|
---|
362 | sl_memset (buffer, '\0', sizeof(buffer));
|
---|
363 |
|
---|
364 | if (what == TIGER_FILE)
|
---|
365 | (void) sl_close (fd);
|
---|
366 |
|
---|
367 | SL_RETURN( res, _("sh_tiger_hash_val"));
|
---|
368 | }
|
---|
369 |
|
---|
370 | if (what == TIGER_DATA && filename != NULL)
|
---|
371 | {
|
---|
372 | tiger(TIGER_CAST filename, (sh_word32) Length, res);
|
---|
373 | SL_RETURN(res, _("sh_tiger_hash_val"));
|
---|
374 | }
|
---|
375 | SL_RETURN( NULL, _("sh_tiger_hash_val"));
|
---|
376 | }
|
---|
377 |
|
---|
378 | /* Thu Oct 18 18:53:33 CEST 2001
|
---|
379 | */
|
---|
380 |
|
---|
381 | #ifdef USE_MD5
|
---|
382 | /*@-type@*/
|
---|
383 | /* md5.c - Functions to compute MD5 message digest of files or memory blocks
|
---|
384 | * according to the definition of MD5 in RFC 1321 from April 1992.
|
---|
385 | * Copyright (C) 1995, 1996 Free Software Foundation, Inc.
|
---|
386 | *
|
---|
387 | * NOTE: The canonical source of this file is maintained with the GNU C
|
---|
388 | * Library. Bugs can be reported to bug-glibc@prep.ai.mit.edu.
|
---|
389 | *
|
---|
390 | * This program is free software; you can redistribute it and/or modify it
|
---|
391 | * under the terms of the GNU General Public License as published by the
|
---|
392 | * Free Software Foundation; either version 2, or (at your option) any
|
---|
393 | * later version.
|
---|
394 | *
|
---|
395 | * This program is distributed in the hope that it will be useful,
|
---|
396 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
397 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
398 | * GNU General Public License for more details.
|
---|
399 | *
|
---|
400 | * You should have received a copy of the GNU General Public License
|
---|
401 | * along with this program; if not, write to the Free Software Foundation,
|
---|
402 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
---|
403 | */
|
---|
404 |
|
---|
405 | /* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995. */
|
---|
406 |
|
---|
407 | /* Hacked to work with samhain by R. Wichmann */
|
---|
408 |
|
---|
409 | typedef UINT32 md5_uint32;
|
---|
410 |
|
---|
411 |
|
---|
412 | /* Structure to save state of computation between the single steps. */
|
---|
413 | typedef struct md5_ctx
|
---|
414 | {
|
---|
415 | md5_uint32 A;
|
---|
416 | md5_uint32 B;
|
---|
417 | md5_uint32 C;
|
---|
418 | md5_uint32 D;
|
---|
419 |
|
---|
420 | md5_uint32 total[2];
|
---|
421 | md5_uint32 buflen;
|
---|
422 | char buffer[128];
|
---|
423 | } md5Param;
|
---|
424 |
|
---|
425 | /*
|
---|
426 | * The following three functions are build up the low level used in
|
---|
427 | * the functions `md5_stream' and `md5_buffer'.
|
---|
428 | */
|
---|
429 |
|
---|
430 | /* Initialize structure containing state of computation.
|
---|
431 | (RFC 1321, 3.3: Step 3) */
|
---|
432 | static void md5_init_ctx (struct md5_ctx *ctx);
|
---|
433 |
|
---|
434 | /* Starting with the result of former calls of this function (or the
|
---|
435 | initialization function update the context for the next LEN bytes
|
---|
436 | starting at BUFFER.
|
---|
437 | It is necessary that LEN is a multiple of 64!!! */
|
---|
438 | static void md5_process_block (const void *buffer, size_t len,
|
---|
439 | struct md5_ctx *ctx);
|
---|
440 |
|
---|
441 | /* Starting with the result of former calls of this function (or the
|
---|
442 | initialization function update the context for the next LEN bytes
|
---|
443 | starting at BUFFER.
|
---|
444 | It is NOT required that LEN is a multiple of 64. */
|
---|
445 | static void md5_process_bytes (const void *buffer, size_t len,
|
---|
446 | struct md5_ctx *ctx);
|
---|
447 |
|
---|
448 | /* Process the remaining bytes in the buffer and put result from CTX
|
---|
449 | in first 16 bytes following RESBUF. The result is always in little
|
---|
450 | endian byte order, so that a byte-wise output yields to the wanted
|
---|
451 | ASCII representation of the message digest.
|
---|
452 |
|
---|
453 | IMPORTANT: On some systems it is required that RESBUF is correctly
|
---|
454 | aligned for a 32 bits value. */
|
---|
455 | static void *md5_finish_ctx (struct md5_ctx *ctx, void *resbuf);
|
---|
456 |
|
---|
457 |
|
---|
458 | /* Put result from CTX in first 16 bytes following RESBUF. The result is
|
---|
459 | always in little endian byte order, so that a byte-wise output yields
|
---|
460 | to the wanted ASCII representation of the message digest.
|
---|
461 |
|
---|
462 | IMPORTANT: On some systems it is required that RESBUF is correctly
|
---|
463 | aligned for a 32 bits value. */
|
---|
464 | static void *md5_read_ctx (const struct md5_ctx *ctx, void *resbuf);
|
---|
465 |
|
---|
466 | #if WORDS_BIGENDIAN
|
---|
467 | static md5_uint32 swapu32(md5_uint32 n)
|
---|
468 | {
|
---|
469 | return ( ((n & 0xffU) << 24) |
|
---|
470 | ((n & 0xff00U) << 8) |
|
---|
471 | ((n & 0xff0000U) >> 8) |
|
---|
472 | ((n & 0xff000000U) >> 24) );
|
---|
473 | }
|
---|
474 | #define SWAP(n) swapu32(n)
|
---|
475 | #else
|
---|
476 | #define SWAP(n) (n)
|
---|
477 | #endif
|
---|
478 |
|
---|
479 | /* This array contains the bytes used to pad the buffer to the next
|
---|
480 | 64-byte boundary. (RFC 1321, 3.1: Step 1) */
|
---|
481 | static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
|
---|
482 |
|
---|
483 | /* Initialize structure containing state of computation.
|
---|
484 | (RFC 1321, 3.3: Step 3) */
|
---|
485 | static void md5_init_ctx(struct md5_ctx *ctx)
|
---|
486 | {
|
---|
487 | ctx->A = 0x67452301;
|
---|
488 | ctx->B = 0xefcdab89;
|
---|
489 | ctx->C = 0x98badcfe;
|
---|
490 | ctx->D = 0x10325476;
|
---|
491 |
|
---|
492 | ctx->total[0] = ctx->total[1] = 0;
|
---|
493 | ctx->buflen = 0;
|
---|
494 | }
|
---|
495 |
|
---|
496 | /* Put result from CTX in first 16 bytes following RESBUF. The result
|
---|
497 | must be in little endian byte order.
|
---|
498 |
|
---|
499 | IMPORTANT: On some systems it is required that RESBUF is correctly
|
---|
500 | aligned for a 32 bits value. */
|
---|
501 | static void *md5_read_ctx(const struct md5_ctx *ctx, void *resbuf)
|
---|
502 | {
|
---|
503 | ((md5_uint32 *) resbuf)[0] = SWAP(ctx->A);
|
---|
504 | ((md5_uint32 *) resbuf)[1] = SWAP(ctx->B);
|
---|
505 | ((md5_uint32 *) resbuf)[2] = SWAP(ctx->C);
|
---|
506 | ((md5_uint32 *) resbuf)[3] = SWAP(ctx->D);
|
---|
507 |
|
---|
508 | return resbuf;
|
---|
509 | }
|
---|
510 |
|
---|
511 | /* Process the remaining bytes in the internal buffer and the usual
|
---|
512 | prolog according to the standard and write the result to RESBUF.
|
---|
513 |
|
---|
514 | IMPORTANT: On some systems it is required that RESBUF is correctly
|
---|
515 | aligned for a 32 bits value. */
|
---|
516 | static void *md5_finish_ctx(struct md5_ctx *ctx, void *resbuf)
|
---|
517 | {
|
---|
518 | /* Take yet unprocessed bytes into account. */
|
---|
519 | md5_uint32 bytes = ctx->buflen;
|
---|
520 | size_t pad;
|
---|
521 |
|
---|
522 | /* Now count remaining bytes. */
|
---|
523 | ctx->total[0] += bytes;
|
---|
524 | if (ctx->total[0] < bytes)
|
---|
525 | ++ctx->total[1];
|
---|
526 |
|
---|
527 | pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
|
---|
528 | memcpy(&ctx->buffer[bytes], fillbuf, pad);
|
---|
529 |
|
---|
530 | /* Put the 64-bit file length in *bits* at the end of the buffer. */
|
---|
531 | *(md5_uint32 *) & ctx->buffer[bytes + pad] = SWAP(ctx->total[0] << 3);
|
---|
532 | *(md5_uint32 *) & ctx->buffer[bytes + pad + 4] =
|
---|
533 | SWAP((ctx->total[1] << 3) | (ctx->total[0] >> 29));
|
---|
534 |
|
---|
535 | /* Process last bytes. */
|
---|
536 | md5_process_block(ctx->buffer, bytes + pad + 8, ctx);
|
---|
537 |
|
---|
538 | return md5_read_ctx(ctx, resbuf);
|
---|
539 | }
|
---|
540 |
|
---|
541 | /* Compute MD5 message digest for LEN bytes beginning at BUFFER. The
|
---|
542 | result is always in little endian byte order, so that a byte-wise
|
---|
543 | output yields to the wanted ASCII representation of the message
|
---|
544 | digest. */
|
---|
545 | void *md5_buffer(const char *buffer, size_t len, void *resblock)
|
---|
546 | {
|
---|
547 | struct md5_ctx ctx;
|
---|
548 |
|
---|
549 | /* Initialize the computation context. */
|
---|
550 | md5_init_ctx(&ctx);
|
---|
551 |
|
---|
552 | /* Process whole buffer but last len % 64 bytes. */
|
---|
553 | md5_process_bytes(buffer, len, &ctx);
|
---|
554 |
|
---|
555 | /* Put result in desired memory area. */
|
---|
556 | return md5_finish_ctx(&ctx, resblock);
|
---|
557 | }
|
---|
558 |
|
---|
559 | static void md5_process_bytes(const void *buffer, size_t len, struct md5_ctx *ctx)
|
---|
560 | {
|
---|
561 | /* When we already have some bits in our internal buffer concatenate
|
---|
562 | both inputs first. */
|
---|
563 | if (ctx->buflen != 0) {
|
---|
564 | size_t left_over = ctx->buflen;
|
---|
565 | size_t add = 128 - left_over > len ? len : 128 - left_over;
|
---|
566 |
|
---|
567 | memcpy(&ctx->buffer[left_over], buffer, add);
|
---|
568 | ctx->buflen += add;
|
---|
569 |
|
---|
570 | if (left_over + add > 64) {
|
---|
571 | md5_process_block(ctx->buffer, (left_over + add) & ~63, ctx);
|
---|
572 | /* The regions in the following copy operation cannot overlap. */
|
---|
573 | memcpy(ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
|
---|
574 | (left_over + add) & 63);
|
---|
575 | ctx->buflen = (left_over + add) & 63;
|
---|
576 | }
|
---|
577 |
|
---|
578 | buffer = (const char *) buffer + add;
|
---|
579 | len -= add;
|
---|
580 | }
|
---|
581 |
|
---|
582 | /* Process available complete blocks. */
|
---|
583 | if (len > 64) {
|
---|
584 | md5_process_block(buffer, len & ~63, ctx);
|
---|
585 | buffer = (const char *) buffer + (len & ~63);
|
---|
586 | len &= 63;
|
---|
587 | }
|
---|
588 |
|
---|
589 | /* Move remaining bytes in internal buffer. */
|
---|
590 | if (len > 0) {
|
---|
591 | memcpy(ctx->buffer, buffer, len);
|
---|
592 | ctx->buflen = len;
|
---|
593 | }
|
---|
594 | }
|
---|
595 |
|
---|
596 | /* These are the four functions used in the four steps of the MD5 algorithm
|
---|
597 | and defined in the RFC 1321. The first function is a little bit optimized
|
---|
598 | (as found in Colin Plumbs public domain implementation). */
|
---|
599 | /* #define FF(b, c, d) ((b & c) | (~b & d)) */
|
---|
600 | #define FF(b, c, d) (d ^ (b & (c ^ d)))
|
---|
601 | #define FG(b, c, d) FF (d, b, c)
|
---|
602 | #define FH(b, c, d) (b ^ c ^ d)
|
---|
603 | #define FI(b, c, d) (c ^ (b | ~d))
|
---|
604 |
|
---|
605 | /* Process LEN bytes of BUFFER, accumulating context into CTX.
|
---|
606 | It is assumed that LEN % 64 == 0. */
|
---|
607 | static void md5_process_block(const void *buffer, size_t len, struct md5_ctx *ctx)
|
---|
608 | {
|
---|
609 | md5_uint32 correct_words[16];
|
---|
610 | const md5_uint32 *words = buffer;
|
---|
611 | size_t nwords = len / sizeof(md5_uint32);
|
---|
612 | const md5_uint32 *endp = words + nwords;
|
---|
613 | md5_uint32 A = ctx->A;
|
---|
614 | md5_uint32 B = ctx->B;
|
---|
615 | md5_uint32 C = ctx->C;
|
---|
616 | md5_uint32 D = ctx->D;
|
---|
617 |
|
---|
618 | /* First increment the byte count. RFC 1321 specifies the possible
|
---|
619 | length of the file up to 2^64 bits. Here we only compute the
|
---|
620 | number of bytes. Do a double word increment. */
|
---|
621 | ctx->total[0] += len;
|
---|
622 | if (ctx->total[0] < len)
|
---|
623 | ++ctx->total[1];
|
---|
624 |
|
---|
625 | /* Process all bytes in the buffer with 64 bytes in each round of
|
---|
626 | the loop. */
|
---|
627 | while (words < endp) {
|
---|
628 | md5_uint32 *cwp = correct_words;
|
---|
629 | md5_uint32 A_save = A;
|
---|
630 | md5_uint32 B_save = B;
|
---|
631 | md5_uint32 C_save = C;
|
---|
632 | md5_uint32 D_save = D;
|
---|
633 |
|
---|
634 | /* First round: using the given function, the context and a constant
|
---|
635 | the next context is computed. Because the algorithms processing
|
---|
636 | unit is a 32-bit word and it is determined to work on words in
|
---|
637 | little endian byte order we perhaps have to change the byte order
|
---|
638 | before the computation. To reduce the work for the next steps
|
---|
639 | we store the swapped words in the array CORRECT_WORDS. */
|
---|
640 |
|
---|
641 | #define OP(a, b, c, d, s, T) \
|
---|
642 | do \
|
---|
643 | { \
|
---|
644 | a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T; \
|
---|
645 | ++words; \
|
---|
646 | CYCLIC (a, s); \
|
---|
647 | a += b; \
|
---|
648 | } \
|
---|
649 | while (0)
|
---|
650 |
|
---|
651 | /* It is unfortunate that C does not provide an operator for
|
---|
652 | cyclic rotation. Hope the C compiler is smart enough. */
|
---|
653 | #define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
|
---|
654 |
|
---|
655 | /* Before we start, one word to the strange constants.
|
---|
656 | They are defined in RFC 1321 as
|
---|
657 |
|
---|
658 | T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
|
---|
659 | */
|
---|
660 |
|
---|
661 | /* Round 1. */
|
---|
662 | OP(A, B, C, D, 7, 0xd76aa478);
|
---|
663 | OP(D, A, B, C, 12, 0xe8c7b756);
|
---|
664 | OP(C, D, A, B, 17, 0x242070db);
|
---|
665 | OP(B, C, D, A, 22, 0xc1bdceee);
|
---|
666 | OP(A, B, C, D, 7, 0xf57c0faf);
|
---|
667 | OP(D, A, B, C, 12, 0x4787c62a);
|
---|
668 | OP(C, D, A, B, 17, 0xa8304613);
|
---|
669 | OP(B, C, D, A, 22, 0xfd469501);
|
---|
670 | OP(A, B, C, D, 7, 0x698098d8);
|
---|
671 | OP(D, A, B, C, 12, 0x8b44f7af);
|
---|
672 | OP(C, D, A, B, 17, 0xffff5bb1);
|
---|
673 | OP(B, C, D, A, 22, 0x895cd7be);
|
---|
674 | OP(A, B, C, D, 7, 0x6b901122);
|
---|
675 | OP(D, A, B, C, 12, 0xfd987193);
|
---|
676 | OP(C, D, A, B, 17, 0xa679438e);
|
---|
677 | OP(B, C, D, A, 22, 0x49b40821);
|
---|
678 | /* For the second to fourth round we have the possibly swapped words
|
---|
679 | in CORRECT_WORDS. Redefine the macro to take an additional first
|
---|
680 | argument specifying the function to use. */
|
---|
681 | #undef OP
|
---|
682 | #define OP(f, a, b, c, d, k, s, T) \
|
---|
683 | do \
|
---|
684 | { \
|
---|
685 | a += f (b, c, d) + correct_words[k] + T; \
|
---|
686 | CYCLIC (a, s); \
|
---|
687 | a += b; \
|
---|
688 | } \
|
---|
689 | while (0)
|
---|
690 |
|
---|
691 | /* Round 2. */
|
---|
692 | OP(FG, A, B, C, D, 1, 5, 0xf61e2562);
|
---|
693 | OP(FG, D, A, B, C, 6, 9, 0xc040b340);
|
---|
694 | OP(FG, C, D, A, B, 11, 14, 0x265e5a51);
|
---|
695 | OP(FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
|
---|
696 | OP(FG, A, B, C, D, 5, 5, 0xd62f105d);
|
---|
697 | OP(FG, D, A, B, C, 10, 9, 0x02441453);
|
---|
698 | OP(FG, C, D, A, B, 15, 14, 0xd8a1e681);
|
---|
699 | OP(FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
|
---|
700 | OP(FG, A, B, C, D, 9, 5, 0x21e1cde6);
|
---|
701 | OP(FG, D, A, B, C, 14, 9, 0xc33707d6);
|
---|
702 | OP(FG, C, D, A, B, 3, 14, 0xf4d50d87);
|
---|
703 | OP(FG, B, C, D, A, 8, 20, 0x455a14ed);
|
---|
704 | OP(FG, A, B, C, D, 13, 5, 0xa9e3e905);
|
---|
705 | OP(FG, D, A, B, C, 2, 9, 0xfcefa3f8);
|
---|
706 | OP(FG, C, D, A, B, 7, 14, 0x676f02d9);
|
---|
707 | OP(FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
|
---|
708 |
|
---|
709 | /* Round 3. */
|
---|
710 | OP(FH, A, B, C, D, 5, 4, 0xfffa3942);
|
---|
711 | OP(FH, D, A, B, C, 8, 11, 0x8771f681);
|
---|
712 | OP(FH, C, D, A, B, 11, 16, 0x6d9d6122);
|
---|
713 | OP(FH, B, C, D, A, 14, 23, 0xfde5380c);
|
---|
714 | OP(FH, A, B, C, D, 1, 4, 0xa4beea44);
|
---|
715 | OP(FH, D, A, B, C, 4, 11, 0x4bdecfa9);
|
---|
716 | OP(FH, C, D, A, B, 7, 16, 0xf6bb4b60);
|
---|
717 | OP(FH, B, C, D, A, 10, 23, 0xbebfbc70);
|
---|
718 | OP(FH, A, B, C, D, 13, 4, 0x289b7ec6);
|
---|
719 | OP(FH, D, A, B, C, 0, 11, 0xeaa127fa);
|
---|
720 | OP(FH, C, D, A, B, 3, 16, 0xd4ef3085);
|
---|
721 | OP(FH, B, C, D, A, 6, 23, 0x04881d05);
|
---|
722 | OP(FH, A, B, C, D, 9, 4, 0xd9d4d039);
|
---|
723 | OP(FH, D, A, B, C, 12, 11, 0xe6db99e5);
|
---|
724 | OP(FH, C, D, A, B, 15, 16, 0x1fa27cf8);
|
---|
725 | OP(FH, B, C, D, A, 2, 23, 0xc4ac5665);
|
---|
726 |
|
---|
727 | /* Round 4. */
|
---|
728 | OP(FI, A, B, C, D, 0, 6, 0xf4292244);
|
---|
729 | OP(FI, D, A, B, C, 7, 10, 0x432aff97);
|
---|
730 | OP(FI, C, D, A, B, 14, 15, 0xab9423a7);
|
---|
731 | OP(FI, B, C, D, A, 5, 21, 0xfc93a039);
|
---|
732 | OP(FI, A, B, C, D, 12, 6, 0x655b59c3);
|
---|
733 | OP(FI, D, A, B, C, 3, 10, 0x8f0ccc92);
|
---|
734 | OP(FI, C, D, A, B, 10, 15, 0xffeff47d);
|
---|
735 | OP(FI, B, C, D, A, 1, 21, 0x85845dd1);
|
---|
736 | OP(FI, A, B, C, D, 8, 6, 0x6fa87e4f);
|
---|
737 | OP(FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
|
---|
738 | OP(FI, C, D, A, B, 6, 15, 0xa3014314);
|
---|
739 | OP(FI, B, C, D, A, 13, 21, 0x4e0811a1);
|
---|
740 | OP(FI, A, B, C, D, 4, 6, 0xf7537e82);
|
---|
741 | OP(FI, D, A, B, C, 11, 10, 0xbd3af235);
|
---|
742 | OP(FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
|
---|
743 | OP(FI, B, C, D, A, 9, 21, 0xeb86d391);
|
---|
744 |
|
---|
745 | /* Add the starting values of the context. */
|
---|
746 | A += A_save;
|
---|
747 | B += B_save;
|
---|
748 | C += C_save;
|
---|
749 | D += D_save;
|
---|
750 | }
|
---|
751 |
|
---|
752 | /* Put checksum in context given as argument. */
|
---|
753 | ctx->A = A;
|
---|
754 | ctx->B = B;
|
---|
755 | ctx->C = C;
|
---|
756 | ctx->D = D;
|
---|
757 | }
|
---|
758 |
|
---|
759 |
|
---|
760 | /*----------------------------------------------------------------------------
|
---|
761 | *--------end of md5.c
|
---|
762 | *----------------------------------------------------------------------------*/
|
---|
763 |
|
---|
764 |
|
---|
765 | int md5Reset(register md5Param* p)
|
---|
766 | {
|
---|
767 | unsigned int i;
|
---|
768 |
|
---|
769 | md5_init_ctx(p);
|
---|
770 |
|
---|
771 | for (i = 0; i < 16; i += 8)
|
---|
772 | {
|
---|
773 | p->buffer[i] = 0x00;
|
---|
774 | p->buffer[i+1] = 0x00;
|
---|
775 | p->buffer[i+2] = 0x00;
|
---|
776 | p->buffer[i+3] = 0x00;
|
---|
777 | p->buffer[i+4] = 0x00;
|
---|
778 | p->buffer[i+5] = 0x00;
|
---|
779 | p->buffer[i+6] = 0x00;
|
---|
780 | p->buffer[i+7] = 0x00;
|
---|
781 | }
|
---|
782 |
|
---|
783 | return 0;
|
---|
784 | }
|
---|
785 |
|
---|
786 | int md5Update(md5Param* p, const sh_byte* data, int size)
|
---|
787 | {
|
---|
788 | md5_process_bytes(data, size, p);
|
---|
789 | return 0;
|
---|
790 | }
|
---|
791 |
|
---|
792 | static void md5Finish(md5Param* p, void *resblock)
|
---|
793 | {
|
---|
794 | (void) md5_finish_ctx(p, resblock);
|
---|
795 | }
|
---|
796 |
|
---|
797 | int md5Digest(md5Param* p, md5_uint32* data)
|
---|
798 | {
|
---|
799 | md5Finish(p, data);
|
---|
800 | (void) md5Reset(p);
|
---|
801 | return 0;
|
---|
802 | }
|
---|
803 | /*@+type@*/
|
---|
804 |
|
---|
805 |
|
---|
806 | /* Compute MD5 message digest for bytes read from STREAM. The
|
---|
807 | resulting message digest number will be written into the 16 bytes
|
---|
808 | beginning at RESBLOCK. */
|
---|
809 | static int md5_stream(char * filename, void *resblock,
|
---|
810 | UINT64 Length, int timeout, SL_TICKET fd)
|
---|
811 | {
|
---|
812 | /* Important: BLOCKSIZE must be a multiple of 64. */
|
---|
813 | static const int BLOCKSIZE = 8192;
|
---|
814 | struct md5_ctx ctx;
|
---|
815 | char buffer[8264]; /* BLOCKSIZE + 72 AIX compiler chokes */
|
---|
816 | size_t sum;
|
---|
817 |
|
---|
818 | SL_TICKET fd;
|
---|
819 | char * tmp;
|
---|
820 | uid_t euid;
|
---|
821 | UINT64 bcount = 0;
|
---|
822 |
|
---|
823 | unsigned long pages_read;
|
---|
824 | #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
|
---|
825 | /*@-nestedextern@*/
|
---|
826 | extern long IO_Limit;
|
---|
827 | /*@+nestedextern@*/
|
---|
828 | #endif
|
---|
829 |
|
---|
830 | /* Initialize the computation context. */
|
---|
831 | (void) md5Reset (&ctx);
|
---|
832 |
|
---|
833 | if (SL_ISERROR (fd))
|
---|
834 | {
|
---|
835 | TPT((0, FIL__, __LINE__, _("msg=<SL_ISERROR (%ld)>\n"), fd));
|
---|
836 | tmp = sh_util_safe_name (filename);
|
---|
837 | (void) sl_get_euid(&euid);
|
---|
838 | sh_error_handle (ShDFLevel[SH_ERR_T_FILE], FIL__, __LINE__, fd,
|
---|
839 | MSG_E_ACCESS, (long) euid, tmp);
|
---|
840 | SH_FREE(tmp);
|
---|
841 | return -1;
|
---|
842 | }
|
---|
843 |
|
---|
844 | pages_read = 0;
|
---|
845 |
|
---|
846 | /* Iterate over full file contents. */
|
---|
847 | while (1 == 1) {
|
---|
848 | /* We read the file in blocks of BLOCKSIZE bytes. One call of the
|
---|
849 | computation function processes the whole buffer so that with the
|
---|
850 | next round of the loop another block can be read. */
|
---|
851 | off_t n;
|
---|
852 | sum = 0;
|
---|
853 |
|
---|
854 | /* Read block. Take care for partial reads. */
|
---|
855 | do {
|
---|
856 |
|
---|
857 | n = (off_t) sl_read_timeout (fd, buffer + sum,
|
---|
858 | (size_t) BLOCKSIZE - sum, timeout);
|
---|
859 |
|
---|
860 | if (SL_ISERROR (n))
|
---|
861 | {
|
---|
862 | if (sig_termfast == 1)
|
---|
863 | return -1;
|
---|
864 | TPT((0, FIL__ , __LINE__ , _("msg=<SL_ISERROR (%ld)>\n"), n));
|
---|
865 | tmp = sh_util_safe_name (filename);
|
---|
866 | if (n == SL_TIMEOUT)
|
---|
867 | {
|
---|
868 | if (timeout != 7) {
|
---|
869 | sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, n, MSG_E_TIMEOUT,
|
---|
870 | timeout, tmp);
|
---|
871 | }
|
---|
872 | }
|
---|
873 | else
|
---|
874 | sh_error_handle (ShDFLevel[SH_ERR_T_FILE], FIL__, __LINE__, n,
|
---|
875 | MSG_E_READ, tmp);
|
---|
876 | SH_FREE(tmp);
|
---|
877 | return -1;
|
---|
878 | }
|
---|
879 |
|
---|
880 | if (Length != TIGER_NOLIM)
|
---|
881 | {
|
---|
882 | bcount += n;
|
---|
883 | if (bcount > Length)
|
---|
884 | n = n - (bcount - Length);
|
---|
885 | n = (n < 0) ? 0 : n;
|
---|
886 | }
|
---|
887 |
|
---|
888 | sum += n;
|
---|
889 | }
|
---|
890 | while (sum < (size_t) BLOCKSIZE
|
---|
891 | && n != 0);
|
---|
892 |
|
---|
893 | ++pages_read;
|
---|
894 |
|
---|
895 | /* If end of file is reached, end the loop. */
|
---|
896 | if (n == 0)
|
---|
897 | break;
|
---|
898 |
|
---|
899 | /* Process buffer with BLOCKSIZE bytes. Note that
|
---|
900 | BLOCKSIZE % 64 == 0
|
---|
901 | */
|
---|
902 | md5_process_block(buffer, BLOCKSIZE, &ctx);
|
---|
903 | sh.statistics.bytes_hashed += BLOCKSIZE;
|
---|
904 |
|
---|
905 | #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
|
---|
906 | if ((IO_Limit) > 0 && (pages_read == 32)) /* check for I/O limit */
|
---|
907 | {
|
---|
908 | sh_unix_io_pause ();
|
---|
909 | pages_read = 0;
|
---|
910 | }
|
---|
911 | if (sig_termfast == 1)
|
---|
912 | {
|
---|
913 | return -1;
|
---|
914 | }
|
---|
915 | #endif
|
---|
916 | }
|
---|
917 |
|
---|
918 | /* Add the last bytes if necessary. */
|
---|
919 | if (sum > 0)
|
---|
920 | {
|
---|
921 | md5_process_bytes(buffer, sum, &ctx);
|
---|
922 | sh.statistics.bytes_hashed += BLOCKSIZE;
|
---|
923 | }
|
---|
924 |
|
---|
925 | /* Construct result in desired memory. */
|
---|
926 | (void) md5Digest(&ctx, resblock);
|
---|
927 |
|
---|
928 | return 0;
|
---|
929 | }
|
---|
930 |
|
---|
931 | static
|
---|
932 | char * sh_tiger_md5_hash (char * filename, TigerType what,
|
---|
933 | UINT64 Length, int timeout, char * out, size_t len)
|
---|
934 | {
|
---|
935 | int cnt;
|
---|
936 | char outbuf[KEY_LEN+1];
|
---|
937 | unsigned char md5buffer[16];
|
---|
938 |
|
---|
939 | (void) md5_stream (filename, md5buffer, Length, timeout, what);
|
---|
940 |
|
---|
941 | /*@-bufferoverflowhigh -usedef@*/
|
---|
942 | for (cnt = 0; cnt < 16; ++cnt)
|
---|
943 | sprintf (&outbuf[cnt*2], _("%02X"), /* known to fit */
|
---|
944 | (unsigned int) md5buffer[cnt]);
|
---|
945 | /*@+bufferoverflowhigh +usedef@*/
|
---|
946 | for (cnt = 32; cnt < KEY_LEN; ++cnt)
|
---|
947 | outbuf[cnt] = '0';
|
---|
948 | outbuf[KEY_LEN] = '\0';
|
---|
949 |
|
---|
950 | sl_strlcpy(out, outbuf, len);
|
---|
951 | return out;
|
---|
952 | }
|
---|
953 |
|
---|
954 | /* USE_MD5 */
|
---|
955 | #endif
|
---|
956 |
|
---|
957 | /***************************************************************
|
---|
958 | *
|
---|
959 | * SHA1
|
---|
960 | *
|
---|
961 | ***************************************************************/
|
---|
962 |
|
---|
963 | #ifdef USE_SHA1
|
---|
964 | /*@-type@*/
|
---|
965 |
|
---|
966 | typedef unsigned char sha_word8;
|
---|
967 | typedef sh_word32 sha_word32;
|
---|
968 |
|
---|
969 | /* The SHA block size and message digest sizes, in bytes */
|
---|
970 |
|
---|
971 | #define SHA_DATASIZE 64
|
---|
972 | #define SHA_DATALEN 16
|
---|
973 | #define SHA_DIGESTSIZE 20
|
---|
974 | #define SHA_DIGESTLEN 5
|
---|
975 | /* The structure for storing SHA info */
|
---|
976 |
|
---|
977 | typedef struct sha_ctx {
|
---|
978 | sha_word32 digest[SHA_DIGESTLEN]; /* Message digest */
|
---|
979 | sha_word32 count_l, count_h; /* 64-bit block count */
|
---|
980 | sha_word8 block[SHA_DATASIZE]; /* SHA data buffer */
|
---|
981 | int index; /* index into buffer */
|
---|
982 | } SHA_CTX;
|
---|
983 |
|
---|
984 | static void sha_init(struct sha_ctx *ctx);
|
---|
985 | static void sha_update(struct sha_ctx *ctx, sha_word8 *buffer,sha_word32 len);
|
---|
986 | static void sha_final(struct sha_ctx *ctx);
|
---|
987 | static void sha_digest(struct sha_ctx *ctx, sha_word8 *s);
|
---|
988 |
|
---|
989 |
|
---|
990 | /* The SHA f()-functions. The f1 and f3 functions can be optimized to
|
---|
991 | save one boolean operation each - thanks to Rich Schroeppel,
|
---|
992 | rcs@cs.arizona.edu for discovering this */
|
---|
993 |
|
---|
994 | /*#define f1(x,y,z) ( ( x & y ) | ( ~x & z ) ) // Rounds 0-19 */
|
---|
995 | #define f1(x,y,z) ( z ^ ( x & ( y ^ z ) ) ) /* Rounds 0-19 */
|
---|
996 | #define f2(x,y,z) ( x ^ y ^ z ) /* Rounds 20-39 */
|
---|
997 | /*#define f3(x,y,z) ( ( x & y ) | ( x & z ) | ( y & z ) ) // Rounds 40-59 */
|
---|
998 | #define f3(x,y,z) ( ( x & y ) | ( z & ( x | y ) ) ) /* Rounds 40-59 */
|
---|
999 | #define f4(x,y,z) ( x ^ y ^ z ) /* Rounds 60-79 */
|
---|
1000 |
|
---|
1001 | /* The SHA Mysterious Constants */
|
---|
1002 |
|
---|
1003 | #define K1 0x5A827999L /* Rounds 0-19 */
|
---|
1004 | #define K2 0x6ED9EBA1L /* Rounds 20-39 */
|
---|
1005 | #define K3 0x8F1BBCDCL /* Rounds 40-59 */
|
---|
1006 | #define K4 0xCA62C1D6L /* Rounds 60-79 */
|
---|
1007 |
|
---|
1008 | /* SHA initial values */
|
---|
1009 |
|
---|
1010 | #define h0init 0x67452301L
|
---|
1011 | #define h1init 0xEFCDAB89L
|
---|
1012 | #define h2init 0x98BADCFEL
|
---|
1013 | #define h3init 0x10325476L
|
---|
1014 | #define h4init 0xC3D2E1F0L
|
---|
1015 |
|
---|
1016 | /* 32-bit rotate left - kludged with shifts */
|
---|
1017 |
|
---|
1018 | #define ROTL(n,X) ( ( (X) << (n) ) | ( (X) >> ( 32 - (n) ) ) )
|
---|
1019 |
|
---|
1020 | /* The initial expanding function. The hash function is defined over an
|
---|
1021 | 80-word expanded input array W, where the first 16 are copies of the input
|
---|
1022 | data, and the remaining 64 are defined by
|
---|
1023 |
|
---|
1024 | W[ i ] = W[ i - 16 ] ^ W[ i - 14 ] ^ W[ i - 8 ] ^ W[ i - 3 ]
|
---|
1025 |
|
---|
1026 | This implementation generates these values on the fly in a circular
|
---|
1027 | buffer - thanks to Colin Plumb, colin@nyx10.cs.du.edu for this
|
---|
1028 | optimization.
|
---|
1029 |
|
---|
1030 | The updated SHA changes the expanding function by adding a rotate of 1
|
---|
1031 | bit. Thanks to Jim Gillogly, jim@rand.org, and an anonymous contributor
|
---|
1032 | for this information */
|
---|
1033 |
|
---|
1034 | #define expand(W,i) ( W[ i & 15 ] = \
|
---|
1035 | ROTL( 1, ( W[ i & 15 ] ^ W[ (i - 14) & 15 ] ^ \
|
---|
1036 | W[ (i - 8) & 15 ] ^ W[ (i - 3) & 15 ] ) ) )
|
---|
1037 |
|
---|
1038 |
|
---|
1039 | /* The prototype SHA sub-round. The fundamental sub-round is:
|
---|
1040 |
|
---|
1041 | a' = e + ROTL( 5, a ) + f( b, c, d ) + k + data;
|
---|
1042 | b' = a;
|
---|
1043 | c' = ROTL( 30, b );
|
---|
1044 | d' = c;
|
---|
1045 | e' = d;
|
---|
1046 |
|
---|
1047 | but this is implemented by unrolling the loop 5 times and renaming the
|
---|
1048 | variables ( e, a, b, c, d ) = ( a', b', c', d', e' ) each iteration.
|
---|
1049 | This code is then replicated 20 times for each of the 4 functions, using
|
---|
1050 | the next 20 values from the W[] array each time */
|
---|
1051 |
|
---|
1052 | #define subRound(a, b, c, d, e, f, k, data) \
|
---|
1053 | ( e += ROTL( 5, a ) + f( b, c, d ) + k + data, b = ROTL( 30, b ) )
|
---|
1054 |
|
---|
1055 | /* Initialize the SHA values */
|
---|
1056 |
|
---|
1057 | static void sha_init(struct sha_ctx *ctx)
|
---|
1058 | {
|
---|
1059 | /* Set the h-vars to their initial values */
|
---|
1060 | ctx->digest[ 0 ] = h0init;
|
---|
1061 | ctx->digest[ 1 ] = h1init;
|
---|
1062 | ctx->digest[ 2 ] = h2init;
|
---|
1063 | ctx->digest[ 3 ] = h3init;
|
---|
1064 | ctx->digest[ 4 ] = h4init;
|
---|
1065 |
|
---|
1066 | /* Initialize bit count */
|
---|
1067 | ctx->count_l = ctx->count_h = 0;
|
---|
1068 |
|
---|
1069 | /* Initialize buffer */
|
---|
1070 | ctx->index = 0;
|
---|
1071 | }
|
---|
1072 |
|
---|
1073 | /* Perform the SHA transformation. Note that this code, like MD5, seems to
|
---|
1074 | break some optimizing compilers due to the complexity of the expressions
|
---|
1075 | and the size of the basic block. It may be necessary to split it into
|
---|
1076 | sections, e.g. based on the four subrounds
|
---|
1077 |
|
---|
1078 | Note that this function destroys the data area */
|
---|
1079 |
|
---|
1080 | static void sha_transform(struct sha_ctx *ctx, sha_word32 *data )
|
---|
1081 | {
|
---|
1082 | register sha_word32 A, B, C, D, E; /* Local vars */
|
---|
1083 |
|
---|
1084 | /* Set up first buffer and local data buffer */
|
---|
1085 | A = ctx->digest[0];
|
---|
1086 | B = ctx->digest[1];
|
---|
1087 | C = ctx->digest[2];
|
---|
1088 | D = ctx->digest[3];
|
---|
1089 | E = ctx->digest[4];
|
---|
1090 |
|
---|
1091 | /* Heavy mangling, in 4 sub-rounds of 20 interations each. */
|
---|
1092 | subRound( A, B, C, D, E, f1, K1, data[ 0] );
|
---|
1093 | subRound( E, A, B, C, D, f1, K1, data[ 1] );
|
---|
1094 | subRound( D, E, A, B, C, f1, K1, data[ 2] );
|
---|
1095 | subRound( C, D, E, A, B, f1, K1, data[ 3] );
|
---|
1096 | subRound( B, C, D, E, A, f1, K1, data[ 4] );
|
---|
1097 | subRound( A, B, C, D, E, f1, K1, data[ 5] );
|
---|
1098 | subRound( E, A, B, C, D, f1, K1, data[ 6] );
|
---|
1099 | subRound( D, E, A, B, C, f1, K1, data[ 7] );
|
---|
1100 | subRound( C, D, E, A, B, f1, K1, data[ 8] );
|
---|
1101 | subRound( B, C, D, E, A, f1, K1, data[ 9] );
|
---|
1102 | subRound( A, B, C, D, E, f1, K1, data[10] );
|
---|
1103 | subRound( E, A, B, C, D, f1, K1, data[11] );
|
---|
1104 | subRound( D, E, A, B, C, f1, K1, data[12] );
|
---|
1105 | subRound( C, D, E, A, B, f1, K1, data[13] );
|
---|
1106 | subRound( B, C, D, E, A, f1, K1, data[14] );
|
---|
1107 | subRound( A, B, C, D, E, f1, K1, data[15] );
|
---|
1108 | subRound( E, A, B, C, D, f1, K1, expand( data, 16 ) );
|
---|
1109 | subRound( D, E, A, B, C, f1, K1, expand( data, 17 ) );
|
---|
1110 | subRound( C, D, E, A, B, f1, K1, expand( data, 18 ) );
|
---|
1111 | subRound( B, C, D, E, A, f1, K1, expand( data, 19 ) );
|
---|
1112 |
|
---|
1113 | subRound( A, B, C, D, E, f2, K2, expand( data, 20 ) );
|
---|
1114 | subRound( E, A, B, C, D, f2, K2, expand( data, 21 ) );
|
---|
1115 | subRound( D, E, A, B, C, f2, K2, expand( data, 22 ) );
|
---|
1116 | subRound( C, D, E, A, B, f2, K2, expand( data, 23 ) );
|
---|
1117 | subRound( B, C, D, E, A, f2, K2, expand( data, 24 ) );
|
---|
1118 | subRound( A, B, C, D, E, f2, K2, expand( data, 25 ) );
|
---|
1119 | subRound( E, A, B, C, D, f2, K2, expand( data, 26 ) );
|
---|
1120 | subRound( D, E, A, B, C, f2, K2, expand( data, 27 ) );
|
---|
1121 | subRound( C, D, E, A, B, f2, K2, expand( data, 28 ) );
|
---|
1122 | subRound( B, C, D, E, A, f2, K2, expand( data, 29 ) );
|
---|
1123 | subRound( A, B, C, D, E, f2, K2, expand( data, 30 ) );
|
---|
1124 | subRound( E, A, B, C, D, f2, K2, expand( data, 31 ) );
|
---|
1125 | subRound( D, E, A, B, C, f2, K2, expand( data, 32 ) );
|
---|
1126 | subRound( C, D, E, A, B, f2, K2, expand( data, 33 ) );
|
---|
1127 | subRound( B, C, D, E, A, f2, K2, expand( data, 34 ) );
|
---|
1128 | subRound( A, B, C, D, E, f2, K2, expand( data, 35 ) );
|
---|
1129 | subRound( E, A, B, C, D, f2, K2, expand( data, 36 ) );
|
---|
1130 | subRound( D, E, A, B, C, f2, K2, expand( data, 37 ) );
|
---|
1131 | subRound( C, D, E, A, B, f2, K2, expand( data, 38 ) );
|
---|
1132 | subRound( B, C, D, E, A, f2, K2, expand( data, 39 ) );
|
---|
1133 |
|
---|
1134 | subRound( A, B, C, D, E, f3, K3, expand( data, 40 ) );
|
---|
1135 | subRound( E, A, B, C, D, f3, K3, expand( data, 41 ) );
|
---|
1136 | subRound( D, E, A, B, C, f3, K3, expand( data, 42 ) );
|
---|
1137 | subRound( C, D, E, A, B, f3, K3, expand( data, 43 ) );
|
---|
1138 | subRound( B, C, D, E, A, f3, K3, expand( data, 44 ) );
|
---|
1139 | subRound( A, B, C, D, E, f3, K3, expand( data, 45 ) );
|
---|
1140 | subRound( E, A, B, C, D, f3, K3, expand( data, 46 ) );
|
---|
1141 | subRound( D, E, A, B, C, f3, K3, expand( data, 47 ) );
|
---|
1142 | subRound( C, D, E, A, B, f3, K3, expand( data, 48 ) );
|
---|
1143 | subRound( B, C, D, E, A, f3, K3, expand( data, 49 ) );
|
---|
1144 | subRound( A, B, C, D, E, f3, K3, expand( data, 50 ) );
|
---|
1145 | subRound( E, A, B, C, D, f3, K3, expand( data, 51 ) );
|
---|
1146 | subRound( D, E, A, B, C, f3, K3, expand( data, 52 ) );
|
---|
1147 | subRound( C, D, E, A, B, f3, K3, expand( data, 53 ) );
|
---|
1148 | subRound( B, C, D, E, A, f3, K3, expand( data, 54 ) );
|
---|
1149 | subRound( A, B, C, D, E, f3, K3, expand( data, 55 ) );
|
---|
1150 | subRound( E, A, B, C, D, f3, K3, expand( data, 56 ) );
|
---|
1151 | subRound( D, E, A, B, C, f3, K3, expand( data, 57 ) );
|
---|
1152 | subRound( C, D, E, A, B, f3, K3, expand( data, 58 ) );
|
---|
1153 | subRound( B, C, D, E, A, f3, K3, expand( data, 59 ) );
|
---|
1154 |
|
---|
1155 | subRound( A, B, C, D, E, f4, K4, expand( data, 60 ) );
|
---|
1156 | subRound( E, A, B, C, D, f4, K4, expand( data, 61 ) );
|
---|
1157 | subRound( D, E, A, B, C, f4, K4, expand( data, 62 ) );
|
---|
1158 | subRound( C, D, E, A, B, f4, K4, expand( data, 63 ) );
|
---|
1159 | subRound( B, C, D, E, A, f4, K4, expand( data, 64 ) );
|
---|
1160 | subRound( A, B, C, D, E, f4, K4, expand( data, 65 ) );
|
---|
1161 | subRound( E, A, B, C, D, f4, K4, expand( data, 66 ) );
|
---|
1162 | subRound( D, E, A, B, C, f4, K4, expand( data, 67 ) );
|
---|
1163 | subRound( C, D, E, A, B, f4, K4, expand( data, 68 ) );
|
---|
1164 | subRound( B, C, D, E, A, f4, K4, expand( data, 69 ) );
|
---|
1165 | subRound( A, B, C, D, E, f4, K4, expand( data, 70 ) );
|
---|
1166 | subRound( E, A, B, C, D, f4, K4, expand( data, 71 ) );
|
---|
1167 | subRound( D, E, A, B, C, f4, K4, expand( data, 72 ) );
|
---|
1168 | subRound( C, D, E, A, B, f4, K4, expand( data, 73 ) );
|
---|
1169 | subRound( B, C, D, E, A, f4, K4, expand( data, 74 ) );
|
---|
1170 | subRound( A, B, C, D, E, f4, K4, expand( data, 75 ) );
|
---|
1171 | subRound( E, A, B, C, D, f4, K4, expand( data, 76 ) );
|
---|
1172 | subRound( D, E, A, B, C, f4, K4, expand( data, 77 ) );
|
---|
1173 | subRound( C, D, E, A, B, f4, K4, expand( data, 78 ) );
|
---|
1174 | subRound( B, C, D, E, A, f4, K4, expand( data, 79 ) );
|
---|
1175 |
|
---|
1176 | /* Build message digest */
|
---|
1177 | ctx->digest[0] += A;
|
---|
1178 | ctx->digest[1] += B;
|
---|
1179 | ctx->digest[2] += C;
|
---|
1180 | ctx->digest[3] += D;
|
---|
1181 | ctx->digest[4] += E;
|
---|
1182 | }
|
---|
1183 |
|
---|
1184 | #if 1
|
---|
1185 |
|
---|
1186 | #ifndef EXTRACT_UCHAR
|
---|
1187 | #define EXTRACT_UCHAR(p) (*(unsigned char *)(p))
|
---|
1188 | #endif
|
---|
1189 |
|
---|
1190 | #define STRING2INT(s) ((((((EXTRACT_UCHAR(s) << 8) \
|
---|
1191 | | EXTRACT_UCHAR(s+1)) << 8) \
|
---|
1192 | | EXTRACT_UCHAR(s+2)) << 8) \
|
---|
1193 | | EXTRACT_UCHAR(s+3))
|
---|
1194 | #else
|
---|
1195 | sha_word32 STRING2INT(word8 *s)
|
---|
1196 | {
|
---|
1197 | sha_word32 r;
|
---|
1198 | int i;
|
---|
1199 |
|
---|
1200 | for (i = 0, r = 0; i < 4; i++, s++)
|
---|
1201 | r = (r << 8) | *s;
|
---|
1202 | return r;
|
---|
1203 | }
|
---|
1204 | #endif
|
---|
1205 |
|
---|
1206 | static void sha_block(struct sha_ctx *ctx, sha_word8 *block)
|
---|
1207 | {
|
---|
1208 | sha_word32 data[SHA_DATALEN];
|
---|
1209 | int i;
|
---|
1210 |
|
---|
1211 | /* Update block count */
|
---|
1212 | /*@-boolops@*/
|
---|
1213 | if (!++ctx->count_l)
|
---|
1214 | ++ctx->count_h;
|
---|
1215 | /*@+boolops@*/
|
---|
1216 |
|
---|
1217 | /* Endian independent conversion */
|
---|
1218 | for (i = 0; i<SHA_DATALEN; i++, block += 4)
|
---|
1219 | data[i] = STRING2INT(block);
|
---|
1220 |
|
---|
1221 | sha_transform(ctx, data);
|
---|
1222 | }
|
---|
1223 |
|
---|
1224 | static void sha_update(struct sha_ctx *ctx, sha_word8 *buffer, sha_word32 len)
|
---|
1225 | {
|
---|
1226 | if (ctx->index != 0)
|
---|
1227 | { /* Try to fill partial block */
|
---|
1228 | unsigned left = SHA_DATASIZE - ctx->index;
|
---|
1229 | if (len < left)
|
---|
1230 | {
|
---|
1231 | memmove(ctx->block + ctx->index, buffer, len);
|
---|
1232 | ctx->index += len;
|
---|
1233 | return; /* Finished */
|
---|
1234 | }
|
---|
1235 | else
|
---|
1236 | {
|
---|
1237 | memmove(ctx->block + ctx->index, buffer, left);
|
---|
1238 | sha_block(ctx, ctx->block);
|
---|
1239 | buffer += left;
|
---|
1240 | len -= left;
|
---|
1241 | }
|
---|
1242 | }
|
---|
1243 | while (len >= SHA_DATASIZE)
|
---|
1244 | {
|
---|
1245 | sha_block(ctx, buffer);
|
---|
1246 | buffer += SHA_DATASIZE;
|
---|
1247 | len -= SHA_DATASIZE;
|
---|
1248 | }
|
---|
1249 | /*@-predboolint@*/
|
---|
1250 | if ((ctx->index = len)) /* This assignment is intended */
|
---|
1251 | /*@+predboolint@*/
|
---|
1252 | /* Buffer leftovers */
|
---|
1253 | memmove(ctx->block, buffer, len);
|
---|
1254 | }
|
---|
1255 |
|
---|
1256 | /* Final wrapup - pad to SHA_DATASIZE-byte boundary with the bit pattern
|
---|
1257 | 1 0* (64-bit count of bits processed, MSB-first) */
|
---|
1258 |
|
---|
1259 | static void sha_final(struct sha_ctx *ctx)
|
---|
1260 | {
|
---|
1261 | sha_word32 data[SHA_DATALEN];
|
---|
1262 | int i;
|
---|
1263 | int words;
|
---|
1264 |
|
---|
1265 | i = ctx->index;
|
---|
1266 | /* Set the first char of padding to 0x80. This is safe since there is
|
---|
1267 | always at least one byte free */
|
---|
1268 | ctx->block[i++] = 0x80;
|
---|
1269 |
|
---|
1270 | /* Fill rest of word */
|
---|
1271 | /*@-predboolint@*/
|
---|
1272 | for( ; i & 3; i++)
|
---|
1273 | ctx->block[i] = 0;
|
---|
1274 | /*@+predboolint@*/
|
---|
1275 |
|
---|
1276 | /* i is now a multiple of the word size 4 */
|
---|
1277 | /*@-shiftimplementation@*/
|
---|
1278 | words = i >> 2;
|
---|
1279 | /*@+shiftimplementation@*/
|
---|
1280 | for (i = 0; i < words; i++)
|
---|
1281 | data[i] = STRING2INT(ctx->block + 4*i);
|
---|
1282 |
|
---|
1283 | if (words > (SHA_DATALEN-2))
|
---|
1284 | { /* No room for length in this block. Process it and
|
---|
1285 | * pad with another one */
|
---|
1286 | for (i = words ; i < SHA_DATALEN; i++)
|
---|
1287 | data[i] = 0;
|
---|
1288 | sha_transform(ctx, data);
|
---|
1289 | for (i = 0; i < (SHA_DATALEN-2); i++)
|
---|
1290 | data[i] = 0;
|
---|
1291 | }
|
---|
1292 | else
|
---|
1293 | for (i = words ; i < SHA_DATALEN - 2; i++)
|
---|
1294 | data[i] = 0;
|
---|
1295 | /* Theres 512 = 2^9 bits in one block */
|
---|
1296 | /*@-shiftimplementation@*/
|
---|
1297 | data[SHA_DATALEN-2] = (ctx->count_h << 9) | (ctx->count_l >> 23);
|
---|
1298 | data[SHA_DATALEN-1] = (ctx->count_l << 9) | (ctx->index << 3);
|
---|
1299 | /*@+shiftimplementation@*/
|
---|
1300 | sha_transform(ctx, data);
|
---|
1301 | }
|
---|
1302 |
|
---|
1303 | static void sha_digest(struct sha_ctx *ctx, sha_word8 *s)
|
---|
1304 | {
|
---|
1305 | int i;
|
---|
1306 |
|
---|
1307 | for (i = 0; i < SHA_DIGESTLEN; i++)
|
---|
1308 | {
|
---|
1309 | *s++ = ctx->digest[i] >> 24;
|
---|
1310 | *s++ = 0xff & (ctx->digest[i] >> 16);
|
---|
1311 | *s++ = 0xff & (ctx->digest[i] >> 8);
|
---|
1312 | *s++ = 0xff & ctx->digest[i];
|
---|
1313 | }
|
---|
1314 | }
|
---|
1315 | /*@+type@*/
|
---|
1316 |
|
---|
1317 | /* Compute SHA1 message digest for bytes read from STREAM. The
|
---|
1318 | resulting message digest number will be written into the 16 bytes
|
---|
1319 | beginning at RESBLOCK. */
|
---|
1320 | static int sha1_stream(char * filename, void *resblock,
|
---|
1321 | UINT64 Length, int timeout, SL_TICKET fd)
|
---|
1322 | {
|
---|
1323 | /* Important: BLOCKSIZE must be a multiple of 64. */
|
---|
1324 | static const int BLOCKSIZE = 4096;
|
---|
1325 | struct sha_ctx ctx;
|
---|
1326 | char buffer[4168]; /* BLOCKSIZE + 72 AIX compiler chokes */
|
---|
1327 | off_t sum = 0;
|
---|
1328 | SL_TICKET fd;
|
---|
1329 | char * tmp;
|
---|
1330 | uid_t euid;
|
---|
1331 | UINT64 bcount = 0;
|
---|
1332 |
|
---|
1333 | unsigned long pages_read;
|
---|
1334 | #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
|
---|
1335 | /*@-nestedextern@*/
|
---|
1336 | extern long IO_Limit;
|
---|
1337 | /*@+nestedextern@*/
|
---|
1338 | #endif
|
---|
1339 |
|
---|
1340 | /* Initialize the computation context. */
|
---|
1341 | (void) sha_init(&ctx);
|
---|
1342 |
|
---|
1343 | if (SL_ISERROR (fd))
|
---|
1344 | {
|
---|
1345 | TPT((0, FIL__, __LINE__, _("msg=<SL_ISERROR (%ld)>\n"), fd));
|
---|
1346 | tmp = sh_util_safe_name (filename);
|
---|
1347 | (void) sl_get_euid(&euid);
|
---|
1348 | sh_error_handle (ShDFLevel[SH_ERR_T_FILE], FIL__, __LINE__, fd,
|
---|
1349 | MSG_E_ACCESS, (long) euid, tmp);
|
---|
1350 | SH_FREE(tmp);
|
---|
1351 | return -1;
|
---|
1352 | }
|
---|
1353 |
|
---|
1354 | /* Iterate over full file contents. */
|
---|
1355 |
|
---|
1356 | pages_read = 0;
|
---|
1357 |
|
---|
1358 | while (1 == 1) {
|
---|
1359 | /* We read the file in blocks of BLOCKSIZE bytes. One call of the
|
---|
1360 | computation function processes the whole buffer so that with the
|
---|
1361 | next round of the loop another block can be read. */
|
---|
1362 | off_t n;
|
---|
1363 | sum = 0;
|
---|
1364 |
|
---|
1365 | /* Read block. Take care for partial reads. */
|
---|
1366 | do {
|
---|
1367 | n = (off_t) sl_read_timeout(fd, buffer + sum,
|
---|
1368 | (size_t) BLOCKSIZE - sum, timeout);
|
---|
1369 |
|
---|
1370 | if (SL_ISERROR (n))
|
---|
1371 | {
|
---|
1372 | if (sig_termfast == 1)
|
---|
1373 | return -1;
|
---|
1374 |
|
---|
1375 | TPT((0, FIL__ , __LINE__ , _("msg=<SL_ISERROR (%ld)>\n"), n));
|
---|
1376 | tmp = sh_util_safe_name (filename);
|
---|
1377 | if (n == SL_TIMEOUT)
|
---|
1378 | {
|
---|
1379 | if (timeout != 7) {
|
---|
1380 | sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, n, MSG_E_TIMEOUT,
|
---|
1381 | timeout, tmp);
|
---|
1382 | }
|
---|
1383 | }
|
---|
1384 | else
|
---|
1385 | {
|
---|
1386 | sh_error_handle (ShDFLevel[SH_ERR_T_FILE], FIL__, __LINE__, n,
|
---|
1387 | MSG_E_READ, tmp);
|
---|
1388 | }
|
---|
1389 | SH_FREE(tmp);
|
---|
1390 | return -1;
|
---|
1391 | }
|
---|
1392 |
|
---|
1393 | if (Length != TIGER_NOLIM)
|
---|
1394 | {
|
---|
1395 | bcount += n;
|
---|
1396 | if (bcount > Length)
|
---|
1397 | n = n - (bcount - Length);
|
---|
1398 | n = (n < 0) ? 0 : n;
|
---|
1399 | }
|
---|
1400 |
|
---|
1401 | sum += n;
|
---|
1402 | }
|
---|
1403 | while (sum < (off_t)BLOCKSIZE
|
---|
1404 | && n != 0);
|
---|
1405 |
|
---|
1406 | ++pages_read;
|
---|
1407 |
|
---|
1408 | /* If end of file is reached, end the loop. */
|
---|
1409 | if (n == 0)
|
---|
1410 | break;
|
---|
1411 |
|
---|
1412 | /* Process buffer with BLOCKSIZE bytes. Note that
|
---|
1413 | BLOCKSIZE % 64 == 0
|
---|
1414 | */
|
---|
1415 | sha_update(&ctx, (sha_word8*) buffer, (sha_word32) BLOCKSIZE);
|
---|
1416 | sh.statistics.bytes_hashed += BLOCKSIZE;
|
---|
1417 |
|
---|
1418 | #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
|
---|
1419 | if ((IO_Limit) > 0 && (pages_read == 32)) /* check for I/O limit */
|
---|
1420 | {
|
---|
1421 | sh_unix_io_pause ();
|
---|
1422 | pages_read = 0;
|
---|
1423 | }
|
---|
1424 | if (sig_termfast == 1)
|
---|
1425 | {
|
---|
1426 | return -1;
|
---|
1427 | }
|
---|
1428 | #endif
|
---|
1429 |
|
---|
1430 | }
|
---|
1431 |
|
---|
1432 | /* Add the last bytes if necessary. */
|
---|
1433 | if (sum > 0)
|
---|
1434 | {
|
---|
1435 | sha_update(&ctx, (sha_word8*) buffer, (sha_word32) sum);
|
---|
1436 | sh.statistics.bytes_hashed += sum;
|
---|
1437 | }
|
---|
1438 |
|
---|
1439 | sha_final (&ctx);
|
---|
1440 |
|
---|
1441 | /* Construct result in desired memory. */
|
---|
1442 | sha_digest (&ctx, resblock);
|
---|
1443 | return 0;
|
---|
1444 | }
|
---|
1445 |
|
---|
1446 |
|
---|
1447 | static char * sh_tiger_sha1_hash (char * filename, TigerType what,
|
---|
1448 | UINT64 Length, int timeout,
|
---|
1449 | char * out, size_t len)
|
---|
1450 | {
|
---|
1451 | int cnt = (int) Length; /* fix compiler warning */
|
---|
1452 | char outbuf[KEY_LEN+1];
|
---|
1453 | unsigned char sha1buffer[20];
|
---|
1454 |
|
---|
1455 | (void) sha1_stream (filename, sha1buffer, Length, timeout, what);
|
---|
1456 |
|
---|
1457 | /*@-bufferoverflowhigh -usedef@*/
|
---|
1458 | for (cnt = 0; cnt < 20; ++cnt)
|
---|
1459 | sprintf (&outbuf[cnt*2], _("%02X"), /* known to fit */
|
---|
1460 | (unsigned int) sha1buffer[cnt]);
|
---|
1461 | /*@+bufferoverflowhigh +usedef@*/
|
---|
1462 | for (cnt = 40; cnt < KEY_LEN; ++cnt)
|
---|
1463 | outbuf[cnt] = '0';
|
---|
1464 | outbuf[KEY_LEN] = '\0';
|
---|
1465 |
|
---|
1466 | sl_strlcpy(out, outbuf, len);
|
---|
1467 | return out;
|
---|
1468 | }
|
---|
1469 |
|
---|
1470 | /* ifdef USE_SHA1 */
|
---|
1471 | #endif
|
---|
1472 |
|
---|
1473 | static int hash_type = 0;
|
---|
1474 |
|
---|
1475 | int sh_tiger_get_hashtype ()
|
---|
1476 | {
|
---|
1477 | return hash_type;
|
---|
1478 | }
|
---|
1479 |
|
---|
1480 | int sh_tiger_hashtype (const char * c)
|
---|
1481 | {
|
---|
1482 | SL_ENTER( _("sh_tiger_hashtype"));
|
---|
1483 |
|
---|
1484 | if (!c)
|
---|
1485 | {
|
---|
1486 | SL_RETURN( -1, _("sh_tiger_hashtype"));
|
---|
1487 | }
|
---|
1488 |
|
---|
1489 | if (0 == strcmp(c, _("TIGER192")))
|
---|
1490 | hash_type = 0;
|
---|
1491 | #ifdef USE_MD5
|
---|
1492 | else if (0 == strcmp(c, _("SHA1")))
|
---|
1493 | hash_type = 1;
|
---|
1494 | #endif
|
---|
1495 | #ifdef USE_SHA1
|
---|
1496 | else if (0 == strcmp(c, _("MD5")))
|
---|
1497 | hash_type = 2;
|
---|
1498 | #endif
|
---|
1499 | else
|
---|
1500 | {
|
---|
1501 | SL_RETURN( -1, _("sh_tiger_hashtype"));
|
---|
1502 | }
|
---|
1503 | SL_RETURN( 0, _("sh_tiger_hashtype"));
|
---|
1504 | }
|
---|
1505 |
|
---|
1506 | static char * sh_tiger_hash_internal (const char * filename, TigerType what,
|
---|
1507 | UINT64 Length, int timeout,
|
---|
1508 | char * out, size_t len);
|
---|
1509 |
|
---|
1510 | char * sh_tiger_hash (const char * filename, TigerType what,
|
---|
1511 | UINT64 Length, char * out, size_t len)
|
---|
1512 | {
|
---|
1513 | return sh_tiger_hash_internal (filename, what, Length, 0, out,len);
|
---|
1514 | }
|
---|
1515 |
|
---|
1516 | char * sh_tiger_generic_hash (char * filename, TigerType what,
|
---|
1517 | UINT64 Length, int timeout,
|
---|
1518 | char * out, size_t len)
|
---|
1519 | {
|
---|
1520 | #ifdef USE_SHA1
|
---|
1521 | if (hash_type == 1)
|
---|
1522 | return sh_tiger_sha1_hash (filename, what, Length, timeout, out, len);
|
---|
1523 | #endif
|
---|
1524 | #ifdef USE_MD5
|
---|
1525 | if (hash_type == 2)
|
---|
1526 | return sh_tiger_md5_hash (filename, what, Length, timeout, out, len);
|
---|
1527 | #endif
|
---|
1528 | return sh_tiger_hash_internal (filename, what, Length, timeout, out, len);
|
---|
1529 | }
|
---|
1530 |
|
---|
1531 | /*
|
---|
1532 | * ------- end new --------- */
|
---|
1533 |
|
---|
1534 | static char * sh_tiger_hash_internal (const char * filename, TigerType what,
|
---|
1535 | UINT64 Length, int timeout,
|
---|
1536 | char * out, size_t len)
|
---|
1537 | {
|
---|
1538 | #if defined(TIGER_64_BIT)
|
---|
1539 | word64 res[3];
|
---|
1540 | #else
|
---|
1541 | sh_word32 res[6];
|
---|
1542 | #endif
|
---|
1543 |
|
---|
1544 | SL_ENTER( _("sh_tiger_hash_internal"));
|
---|
1545 |
|
---|
1546 | SH_VALIDATE_GE(len, (KEY_LEN+1));
|
---|
1547 |
|
---|
1548 | if (NULL != sh_tiger_hash_val (filename, what, Length, timeout, res))
|
---|
1549 | {
|
---|
1550 | #if defined(TIGER_64_BIT)
|
---|
1551 | sl_snprintf(out, len,
|
---|
1552 | MYFORMAT,
|
---|
1553 | (sh_word32)(res[0]>>32),
|
---|
1554 | (sh_word32)(res[0]),
|
---|
1555 | (sh_word32)(res[1]>>32),
|
---|
1556 | (sh_word32)(res[1]),
|
---|
1557 | (sh_word32)(res[2]>>32),
|
---|
1558 | (sh_word32)(res[2]) );
|
---|
1559 | #else
|
---|
1560 | sl_snprintf(out, len,
|
---|
1561 | MYFORMAT,
|
---|
1562 | (sh_word32)(res[1]),
|
---|
1563 | (sh_word32)(res[0]),
|
---|
1564 | (sh_word32)(res[3]),
|
---|
1565 | (sh_word32)(res[2]),
|
---|
1566 | (sh_word32)(res[5]),
|
---|
1567 | (sh_word32)(res[4]) );
|
---|
1568 | #endif
|
---|
1569 | out[len-1] = '\0';
|
---|
1570 | SL_RETURN( out, _("sh_tiger_hash_internal"));
|
---|
1571 |
|
---|
1572 | }
|
---|
1573 |
|
---|
1574 | SL_RETURN( SH_KEY_NULL, _("sh_tiger_hash_internal"));
|
---|
1575 | }
|
---|
1576 |
|
---|
1577 | char * sh_tiger_hash_gpg (const char * filename, TigerType what,
|
---|
1578 | UINT64 Length)
|
---|
1579 | {
|
---|
1580 | size_t len;
|
---|
1581 | char * out;
|
---|
1582 | char outhash[48+6+1];
|
---|
1583 | #if defined(TIGER_64_BIT)
|
---|
1584 | word64 res[3];
|
---|
1585 | #else
|
---|
1586 | sh_word32 res[6];
|
---|
1587 | #endif
|
---|
1588 |
|
---|
1589 | SL_ENTER(_("sh_tiger_hash_gpg"));
|
---|
1590 |
|
---|
1591 | if (NULL != sh_tiger_hash_val (filename, what, Length, 0, res))
|
---|
1592 | {
|
---|
1593 | #if defined(TIGER_64_BIT)
|
---|
1594 | sl_snprintf(outhash,
|
---|
1595 | sizeof(outhash),
|
---|
1596 | GPGFORMAT,
|
---|
1597 | (sh_word32)(res[0]>>32),
|
---|
1598 | (sh_word32)(res[0]),
|
---|
1599 | (sh_word32)(res[1]>>32),
|
---|
1600 | (sh_word32)(res[1]),
|
---|
1601 | (sh_word32)(res[2]>>32),
|
---|
1602 | (sh_word32)(res[2]) );
|
---|
1603 | #else
|
---|
1604 | sl_snprintf(outhash,
|
---|
1605 | sizeof(outhash),
|
---|
1606 | GPGFORMAT,
|
---|
1607 | (sh_word32)(res[1]),
|
---|
1608 | (sh_word32)(res[0]),
|
---|
1609 | (sh_word32)(res[3]),
|
---|
1610 | (sh_word32)(res[2]),
|
---|
1611 | (sh_word32)(res[5]),
|
---|
1612 | (sh_word32)(res[4]) );
|
---|
1613 | #endif
|
---|
1614 | outhash[sizeof(outhash)-1] = '\0';
|
---|
1615 | }
|
---|
1616 | else
|
---|
1617 | {
|
---|
1618 | sl_strlcpy(outhash,
|
---|
1619 | _("00000000 00000000 00000000 00000000 00000000 00000000"),
|
---|
1620 | sizeof(outhash));
|
---|
1621 | }
|
---|
1622 |
|
---|
1623 | if (what == TIGER_FILE && sl_ok_adds(sl_strlen (filename), (2 + 48 + 6)))
|
---|
1624 | len = sl_strlen (filename) + 2 + 48 + 6;
|
---|
1625 | else
|
---|
1626 | len = 48 + 6;
|
---|
1627 |
|
---|
1628 | out = SH_ALLOC(len + 1);
|
---|
1629 |
|
---|
1630 | if (what == TIGER_FILE)
|
---|
1631 | {
|
---|
1632 | (void) sl_strlcpy (out, filename, len+1);
|
---|
1633 | (void) sl_strlcat (out, _(": "), len+1);
|
---|
1634 | (void) sl_strlcat (out, outhash, len+1);
|
---|
1635 | }
|
---|
1636 | else
|
---|
1637 | {
|
---|
1638 | (void) sl_strlcpy (out, outhash, len+1);
|
---|
1639 | }
|
---|
1640 | SL_RETURN( out, _("sh_tiger_hash_gpg"));
|
---|
1641 | }
|
---|
1642 |
|
---|
1643 |
|
---|
1644 | UINT32 * sh_tiger_hash_uint32 (char * filename,
|
---|
1645 | TigerType what,
|
---|
1646 | UINT64 Length, UINT32 * out, size_t len)
|
---|
1647 | {
|
---|
1648 | #if defined(TIGER_64_BIT)
|
---|
1649 | word64 res[3];
|
---|
1650 | #else
|
---|
1651 | sh_word32 res[6];
|
---|
1652 | #endif
|
---|
1653 |
|
---|
1654 | SL_ENTER(_("sh_tiger_hash_uint32"));
|
---|
1655 |
|
---|
1656 | SH_VALIDATE_GE(len, 6);
|
---|
1657 |
|
---|
1658 | out[0] = 0; out[1] = 0; out[2] = 0;
|
---|
1659 | out[3] = 0; out[4] = 0; out[5] = 0;
|
---|
1660 |
|
---|
1661 | if (NULL != sh_tiger_hash_val (filename, what, Length, 0, res))
|
---|
1662 | {
|
---|
1663 | #if defined(TIGER_64_BIT)
|
---|
1664 | out[0] = (UINT32)(res[0]>>32);
|
---|
1665 | out[1] = (UINT32)(res[0]);
|
---|
1666 | out[2] = (UINT32)(res[1]>>32);
|
---|
1667 | out[3] = (UINT32)(res[1]);
|
---|
1668 | out[4] = (UINT32)(res[2]>>32);
|
---|
1669 | out[5] = (UINT32)(res[2]);
|
---|
1670 | #else
|
---|
1671 | out[0] = (UINT32)(res[1]);
|
---|
1672 | out[1] = (UINT32)(res[0]);
|
---|
1673 | out[2] = (UINT32)(res[3]);
|
---|
1674 | out[3] = (UINT32)(res[2]);
|
---|
1675 | out[4] = (UINT32)(res[5]);
|
---|
1676 | out[5] = (UINT32)(res[4]);
|
---|
1677 | #endif
|
---|
1678 | }
|
---|
1679 |
|
---|
1680 | SL_RETURN(out, _("sh_tiger_hash_uint32"));
|
---|
1681 | }
|
---|
1682 |
|
---|
1683 |
|
---|
1684 |
|
---|
1685 |
|
---|