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