source: trunk/src/sh_tiger0.c@ 154

Last change on this file since 154 was 151, checked in by katerina, 17 years ago

Checksum functions modified to return length of file hashed. Fixes ticket #85 (GrowingLogfiles bug).

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