source: trunk/src/sh_tiger0.c@ 136

Last change on this file since 136 was 134, checked in by rainer, 17 years ago

More thread-safety changes.

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