source: trunk/src/sh_tiger0.c@ 282

Last change on this file since 282 was 248, checked in by katerina, 15 years ago

Code to track down originating site for ticket #163.

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