source: trunk/src/sh_tiger0.c@ 90

Last change on this file since 90 was 76, checked in by rainer, 18 years ago

Fix for ticket #38 (csv escaping) and #39 (building on cygwin). Also optimize a bit.

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