source: trunk/src/sh_tiger0.c@ 28

Last change on this file since 28 was 22, checked in by rainer, 19 years ago

Minor code revisions.

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