source: trunk/src/sh_tiger0.c@ 541

Last change on this file since 541 was 541, checked in by katerina, 6 years ago

Fix for ticket #433 (coding standardisation).

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