source: trunk/src/sh_tiger0.c@ 6

Last change on this file since 6 was 1, checked in by katerina, 19 years ago

Initial import

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