source: trunk/src/sh_tiger0.c@ 479

Last change on this file since 479 was 444, checked in by katerina, 11 years ago

Support for sha2-256 checksum (ticket #348).

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