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