source: trunk/src/sh_tiger0.c@ 181

Last change on this file since 181 was 171, checked in by katerina, 16 years ago

Include dnmalloc (ticket #108) and fix bugs #106 (EINPROGRESS) and #107 (compressBound).

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