source: trunk/src/sh_tiger0.c@ 440

Last change on this file since 440 was 440, checked in by katerina, 11 years ago

Fix for ticket #344 (Problems with Ubuntu 13.04)

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