source: branches/samhain-2_2-branch/src/sh_tiger0.c@ 551

Last change on this file since 551 was 46, checked in by rainer, 18 years ago

Fix x86_64 build failure with gcc 4.x (as well as some gcc 4.x warnings)

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