source: trunk/src/sh_tiger0.c@ 587

Last change on this file since 587 was 587, checked in by katerina, 21 hours ago

Fix for ticket #475 (compiler warnings about unused variables).

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