source: trunk/src/sh_tiger0.c@ 137

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

Fix compile errors.

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