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