source: trunk/src/sh_tiger0.c@ 104

Last change on this file since 104 was 102, checked in by rainer, 17 years ago

Flush before fork (fix for ticket #60), and fix for kcheck on 2.6.21 (ticket #63).

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