source: trunk/src/sh_tiger0.c@ 167

Last change on this file since 167 was 167, checked in by katerina, 17 years ago

First parts of changes for MODI_TXT

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