Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/sh_tiger0.c

    r30 r20  
    404404#ifdef USE_MD5
    405405/*@-type@*/
    406 /* md5.c - Functions to compute MD5 message digest of files or memory blocks
    407  *         according to the definition of MD5 in RFC 1321 from April 1992.
    408  * Copyright (C) 1995, 1996 Free Software Foundation, Inc.
     406/************************************************************************
    409407 *
    410  * NOTE: The canonical source of this file is maintained with the GNU C
    411  * Library.  Bugs can be reported to bug-glibc@prep.ai.mit.edu.
     408 *  md5.h - Declaration of functions and data types used for MD5 sum
     409 *  computing library functions.
    412410 *
    413  * This program is free software; you can redistribute it and/or modify it
    414  * under the terms of the GNU General Public License as published by the
    415  * Free Software Foundation; either version 2, or (at your option) any
    416  * later version.
    417  *
    418  * This program is distributed in the hope that it will be useful,
    419  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    420  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    421  * GNU General Public License for more details.
    422  *
    423  * You should have received a copy of the GNU General Public License
    424  * along with this program; if not, write to the Free Software Foundation,
    425  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    426  */
    427 
    428 /* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.  */
    429 
     411 ************************************************************************/
     412
     413/* Written Bob Deblier <bob@virtualunlimited.com>         */
    430414/* Hacked to work with samhain by R. Wichmann             */
    431 
    432 typedef UINT32 md5_uint32;
     415/* Need for 64bit type removed, fix for Mac OS X compiler */
     416
     417typedef sh_word32     uint32;
     418typedef unsigned char uint8;
     419
     420
     421
    433422
    434423
    435424/* Structure to save state of computation between the single steps.  */
    436 typedef struct md5_ctx
    437 {
    438   md5_uint32 A;
    439   md5_uint32 B;
    440   md5_uint32 C;
    441   md5_uint32 D;
    442 
    443   md5_uint32 total[2];
    444   md5_uint32 buflen;
    445   char buffer[128];
     425typedef struct
     426{
     427        uint32 h[4];
     428        uint32 data[16];
     429        uint8  offset;
     430        uint32  nblocks;
     431        int  count;
    446432} md5Param;
    447433
    448 /*
    449  * The following three functions are build up the low level used in
    450  * the functions `md5_stream' and `md5_buffer'.
    451  */
    452 
    453 /* Initialize structure containing state of computation.
    454    (RFC 1321, 3.3: Step 3)  */
    455 static void md5_init_ctx (struct md5_ctx *ctx);
    456 
    457 /* Starting with the result of former calls of this function (or the
    458    initialization function update the context for the next LEN bytes
    459    starting at BUFFER.
    460    It is necessary that LEN is a multiple of 64!!! */
    461 static void md5_process_block (const void *buffer, size_t len,
    462                                     struct md5_ctx *ctx);
    463 
    464 /* Starting with the result of former calls of this function (or the
    465    initialization function update the context for the next LEN bytes
    466    starting at BUFFER.
    467    It is NOT required that LEN is a multiple of 64.  */
    468 static void md5_process_bytes (const void *buffer, size_t len,
    469                                     struct md5_ctx *ctx);
    470 
    471 /* Process the remaining bytes in the buffer and put result from CTX
    472    in first 16 bytes following RESBUF.  The result is always in little
    473    endian byte order, so that a byte-wise output yields to the wanted
    474    ASCII representation of the message digest.
    475 
    476    IMPORTANT: On some systems it is required that RESBUF is correctly
    477    aligned for a 32 bits value.  */
    478 static void *md5_finish_ctx (struct md5_ctx *ctx, void *resbuf);
    479 
    480 
    481 /* Put result from CTX in first 16 bytes following RESBUF.  The result is
    482    always in little endian byte order, so that a byte-wise output yields
    483    to the wanted ASCII representation of the message digest.
    484 
    485    IMPORTANT: On some systems it is required that RESBUF is correctly
    486    aligned for a 32 bits value.  */
    487 static void *md5_read_ctx (const struct md5_ctx *ctx, void *resbuf);
    488 
    489 #if WORDS_BIGENDIAN
    490 static md5_uint32 swapu32(md5_uint32 n)
    491 {
    492   return (    ((n & 0xffU) << 24) |
    493               ((n & 0xff00U) << 8) |
    494               ((n & 0xff0000U) >> 8) |
    495               ((n & 0xff000000U) >> 24) );
    496 }
    497 #define SWAP(n) swapu32(n)
    498 #else
    499 #define SWAP(n) (n)
    500 #endif
    501 
    502 /* This array contains the bytes used to pad the buffer to the next
    503    64-byte boundary.  (RFC 1321, 3.1: Step 1)  */
    504 static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ...  */  };
    505 
    506 /* Initialize structure containing state of computation.
    507    (RFC 1321, 3.3: Step 3)  */
    508 static void md5_init_ctx(struct md5_ctx *ctx)
    509 {
    510   ctx->A = 0x67452301;
    511   ctx->B = 0xefcdab89;
    512   ctx->C = 0x98badcfe;
    513   ctx->D = 0x10325476;
    514 
    515   ctx->total[0] = ctx->total[1] = 0;
    516   ctx->buflen = 0;
    517 }
    518 
    519 /* Put result from CTX in first 16 bytes following RESBUF.  The result
    520    must be in little endian byte order.
    521 
    522    IMPORTANT: On some systems it is required that RESBUF is correctly
    523    aligned for a 32 bits value.  */
    524 static void *md5_read_ctx(const struct md5_ctx *ctx, void *resbuf)
    525 {
    526   ((md5_uint32 *) resbuf)[0] = SWAP(ctx->A);
    527   ((md5_uint32 *) resbuf)[1] = SWAP(ctx->B);
    528   ((md5_uint32 *) resbuf)[2] = SWAP(ctx->C);
    529   ((md5_uint32 *) resbuf)[3] = SWAP(ctx->D);
    530 
    531   return resbuf;
    532 }
    533 
    534 /* Process the remaining bytes in the internal buffer and the usual
    535    prolog according to the standard and write the result to RESBUF.
    536 
    537    IMPORTANT: On some systems it is required that RESBUF is correctly
    538    aligned for a 32 bits value.  */
    539 static void *md5_finish_ctx(struct md5_ctx *ctx, void *resbuf)
    540 {
    541   /* Take yet unprocessed bytes into account.  */
    542   md5_uint32 bytes = ctx->buflen;
    543   size_t pad;
    544 
    545   /* Now count remaining bytes.  */
    546   ctx->total[0] += bytes;
    547   if (ctx->total[0] < bytes)
    548     ++ctx->total[1];
    549 
    550   pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
    551   memcpy(&ctx->buffer[bytes], fillbuf, pad);
    552 
    553   /* Put the 64-bit file length in *bits* at the end of the buffer.  */
    554   *(md5_uint32 *) & ctx->buffer[bytes + pad] = SWAP(ctx->total[0] << 3);
    555   *(md5_uint32 *) & ctx->buffer[bytes + pad + 4] =
    556     SWAP((ctx->total[1] << 3) | (ctx->total[0] >> 29));
    557 
    558   /* Process last bytes.  */
    559   md5_process_block(ctx->buffer, bytes + pad + 8, ctx);
    560 
    561   return md5_read_ctx(ctx, resbuf);
    562 }
    563 
    564 /* Compute MD5 message digest for LEN bytes beginning at BUFFER.  The
    565    result is always in little endian byte order, so that a byte-wise
    566    output yields to the wanted ASCII representation of the message
    567    digest.  */
    568 void *md5_buffer(const char *buffer, size_t len, void *resblock)
    569 {
    570   struct md5_ctx ctx;
    571 
    572   /* Initialize the computation context.  */
    573   md5_init_ctx(&ctx);
    574 
    575   /* Process whole buffer but last len % 64 bytes.  */
    576   md5_process_bytes(buffer, len, &ctx);
    577 
    578   /* Put result in desired memory area.  */
    579   return md5_finish_ctx(&ctx, resblock);
    580 }
    581 
    582 static void md5_process_bytes(const void *buffer, size_t len, struct md5_ctx *ctx)
    583 {
    584   /* When we already have some bits in our internal buffer concatenate
    585      both inputs first.  */
    586   if (ctx->buflen != 0) {
    587     size_t left_over = ctx->buflen;
    588     size_t add = 128 - left_over > len ? len : 128 - left_over;
    589 
    590     memcpy(&ctx->buffer[left_over], buffer, add);
    591     ctx->buflen += add;
    592 
    593     if (left_over + add > 64) {
    594       md5_process_block(ctx->buffer, (left_over + add) & ~63, ctx);
    595       /* The regions in the following copy operation cannot overlap.  */
    596       memcpy(ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
    597              (left_over + add) & 63);
    598       ctx->buflen = (left_over + add) & 63;
    599     }
    600 
    601     buffer = (const char *) buffer + add;
    602     len -= add;
    603   }
    604 
    605   /* Process available complete blocks.  */
    606   if (len > 64) {
    607     md5_process_block(buffer, len & ~63, ctx);
    608     buffer = (const char *) buffer + (len & ~63);
    609     len &= 63;
    610   }
    611 
    612   /* Move remaining bytes in internal buffer.  */
    613   if (len > 0) {
    614     memcpy(ctx->buffer, buffer, len);
    615     ctx->buflen = len;
    616   }
    617 }
    618 
    619 /* These are the four functions used in the four steps of the MD5 algorithm
    620    and defined in the RFC 1321.  The first function is a little bit optimized
    621    (as found in Colin Plumbs public domain implementation).  */
    622 /* #define FF(b, c, d) ((b & c) | (~b & d)) */
    623 #define FF(b, c, d) (d ^ (b & (c ^ d)))
    624 #define FG(b, c, d) FF (d, b, c)
    625 #define FH(b, c, d) (b ^ c ^ d)
    626 #define FI(b, c, d) (c ^ (b | ~d))
    627 
    628 /* Process LEN bytes of BUFFER, accumulating context into CTX.
    629    It is assumed that LEN % 64 == 0.  */
    630 static void md5_process_block(const void *buffer, size_t len, struct md5_ctx *ctx)
    631 {
    632   md5_uint32 correct_words[16];
    633   const md5_uint32 *words = buffer;
    634   size_t nwords = len / sizeof(md5_uint32);
    635   const md5_uint32 *endp = words + nwords;
    636   md5_uint32 A = ctx->A;
    637   md5_uint32 B = ctx->B;
    638   md5_uint32 C = ctx->C;
    639   md5_uint32 D = ctx->D;
    640 
    641   /* First increment the byte count.  RFC 1321 specifies the possible
    642      length of the file up to 2^64 bits.  Here we only compute the
    643      number of bytes.  Do a double word increment.  */
    644   ctx->total[0] += len;
    645   if (ctx->total[0] < len)
    646     ++ctx->total[1];
    647 
    648   /* Process all bytes in the buffer with 64 bytes in each round of
    649      the loop.  */
    650   while (words < endp) {
    651     md5_uint32 *cwp = correct_words;
    652     md5_uint32 A_save = A;
    653     md5_uint32 B_save = B;
    654     md5_uint32 C_save = C;
    655     md5_uint32 D_save = D;
    656 
    657     /* First round: using the given function, the context and a constant
    658        the next context is computed.  Because the algorithms processing
    659        unit is a 32-bit word and it is determined to work on words in
    660        little endian byte order we perhaps have to change the byte order
    661        before the computation.  To reduce the work for the next steps
    662        we store the swapped words in the array CORRECT_WORDS.  */
    663 
    664 #define OP(a, b, c, d, s, T)                                            \
    665       do                                                                \
    666         {                                                               \
    667           a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T;             \
    668           ++words;                                                      \
    669           CYCLIC (a, s);                                                \
    670           a += b;                                                       \
    671         }                                                               \
    672       while (0)
    673 
    674     /* It is unfortunate that C does not provide an operator for
    675        cyclic rotation.  Hope the C compiler is smart enough.  */
    676 #define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
    677 
    678     /* Before we start, one word to the strange constants.
    679        They are defined in RFC 1321 as
    680 
    681        T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
    682     */
    683 
    684     /* Round 1.  */
    685     OP(A, B, C, D, 7, 0xd76aa478);
    686     OP(D, A, B, C, 12, 0xe8c7b756);
    687     OP(C, D, A, B, 17, 0x242070db);
    688     OP(B, C, D, A, 22, 0xc1bdceee);
    689     OP(A, B, C, D, 7, 0xf57c0faf);
    690     OP(D, A, B, C, 12, 0x4787c62a);
    691     OP(C, D, A, B, 17, 0xa8304613);
    692     OP(B, C, D, A, 22, 0xfd469501);
    693     OP(A, B, C, D, 7, 0x698098d8);
    694     OP(D, A, B, C, 12, 0x8b44f7af);
    695     OP(C, D, A, B, 17, 0xffff5bb1);
    696     OP(B, C, D, A, 22, 0x895cd7be);
    697     OP(A, B, C, D, 7, 0x6b901122);
    698     OP(D, A, B, C, 12, 0xfd987193);
    699     OP(C, D, A, B, 17, 0xa679438e);
    700     OP(B, C, D, A, 22, 0x49b40821);
    701     /* For the second to fourth round we have the possibly swapped words
    702        in CORRECT_WORDS.  Redefine the macro to take an additional first
    703        argument specifying the function to use.  */
    704 #undef OP
    705 #define OP(f, a, b, c, d, k, s, T)                                      \
    706       do                                                                \
    707         {                                                               \
    708           a += f (b, c, d) + correct_words[k] + T;                      \
    709           CYCLIC (a, s);                                                \
    710           a += b;                                                       \
    711         }                                                               \
    712       while (0)
    713 
    714     /* Round 2.  */
    715     OP(FG, A, B, C, D, 1, 5, 0xf61e2562);
    716     OP(FG, D, A, B, C, 6, 9, 0xc040b340);
    717     OP(FG, C, D, A, B, 11, 14, 0x265e5a51);
    718     OP(FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
    719     OP(FG, A, B, C, D, 5, 5, 0xd62f105d);
    720     OP(FG, D, A, B, C, 10, 9, 0x02441453);
    721     OP(FG, C, D, A, B, 15, 14, 0xd8a1e681);
    722     OP(FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
    723     OP(FG, A, B, C, D, 9, 5, 0x21e1cde6);
    724     OP(FG, D, A, B, C, 14, 9, 0xc33707d6);
    725     OP(FG, C, D, A, B, 3, 14, 0xf4d50d87);
    726     OP(FG, B, C, D, A, 8, 20, 0x455a14ed);
    727     OP(FG, A, B, C, D, 13, 5, 0xa9e3e905);
    728     OP(FG, D, A, B, C, 2, 9, 0xfcefa3f8);
    729     OP(FG, C, D, A, B, 7, 14, 0x676f02d9);
    730     OP(FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
    731 
    732     /* Round 3.  */
    733     OP(FH, A, B, C, D, 5, 4, 0xfffa3942);
    734     OP(FH, D, A, B, C, 8, 11, 0x8771f681);
    735     OP(FH, C, D, A, B, 11, 16, 0x6d9d6122);
    736     OP(FH, B, C, D, A, 14, 23, 0xfde5380c);
    737     OP(FH, A, B, C, D, 1, 4, 0xa4beea44);
    738     OP(FH, D, A, B, C, 4, 11, 0x4bdecfa9);
    739     OP(FH, C, D, A, B, 7, 16, 0xf6bb4b60);
    740     OP(FH, B, C, D, A, 10, 23, 0xbebfbc70);
    741     OP(FH, A, B, C, D, 13, 4, 0x289b7ec6);
    742     OP(FH, D, A, B, C, 0, 11, 0xeaa127fa);
    743     OP(FH, C, D, A, B, 3, 16, 0xd4ef3085);
    744     OP(FH, B, C, D, A, 6, 23, 0x04881d05);
    745     OP(FH, A, B, C, D, 9, 4, 0xd9d4d039);
    746     OP(FH, D, A, B, C, 12, 11, 0xe6db99e5);
    747     OP(FH, C, D, A, B, 15, 16, 0x1fa27cf8);
    748     OP(FH, B, C, D, A, 2, 23, 0xc4ac5665);
    749 
    750     /* Round 4.  */
    751     OP(FI, A, B, C, D, 0, 6, 0xf4292244);
    752     OP(FI, D, A, B, C, 7, 10, 0x432aff97);
    753     OP(FI, C, D, A, B, 14, 15, 0xab9423a7);
    754     OP(FI, B, C, D, A, 5, 21, 0xfc93a039);
    755     OP(FI, A, B, C, D, 12, 6, 0x655b59c3);
    756     OP(FI, D, A, B, C, 3, 10, 0x8f0ccc92);
    757     OP(FI, C, D, A, B, 10, 15, 0xffeff47d);
    758     OP(FI, B, C, D, A, 1, 21, 0x85845dd1);
    759     OP(FI, A, B, C, D, 8, 6, 0x6fa87e4f);
    760     OP(FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
    761     OP(FI, C, D, A, B, 6, 15, 0xa3014314);
    762     OP(FI, B, C, D, A, 13, 21, 0x4e0811a1);
    763     OP(FI, A, B, C, D, 4, 6, 0xf7537e82);
    764     OP(FI, D, A, B, C, 11, 10, 0xbd3af235);
    765     OP(FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
    766     OP(FI, B, C, D, A, 9, 21, 0xeb86d391);
    767 
    768     /* Add the starting values of the context.  */
    769     A += A_save;
    770     B += B_save;
    771     C += C_save;
    772     D += D_save;
    773   }
    774 
    775   /* Put checksum in context given as argument.  */
    776   ctx->A = A;
    777   ctx->B = B;
    778   ctx->C = C;
    779   ctx->D = D;
    780 }
    781 
    782 
    783 /*----------------------------------------------------------------------------
    784  *--------end of md5.c
    785  *----------------------------------------------------------------------------*/
     434static uint32 md5hinit[4] = { 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476 };
    786435
    787436 
     
    789438{
    790439        unsigned int i;
    791 
    792         md5_init_ctx(p);
     440        memcpy(p->h, md5hinit, 16);
    793441       
    794442        for (i = 0; i < 16; i += 8)
    795443          {
    796             p->buffer[i]   = 0x00;
    797             p->buffer[i+1] = 0x00;
    798             p->buffer[i+2] = 0x00;
    799             p->buffer[i+3] = 0x00;
    800             p->buffer[i+4] = 0x00;
    801             p->buffer[i+5] = 0x00;
    802             p->buffer[i+6] = 0x00;
    803             p->buffer[i+7] = 0x00;
     444            p->data[i]   = 0x00;
     445            p->data[i+1] = 0x00;
     446            p->data[i+2] = 0x00;
     447            p->data[i+3] = 0x00;
     448            p->data[i+4] = 0x00;
     449            p->data[i+5] = 0x00;
     450            p->data[i+6] = 0x00;
     451            p->data[i+7] = 0x00;
    804452          }
    805453       
     454        /* memset(p->data, 0x00, 64); */
     455        p->offset = (uint8) 0;
     456        p->nblocks = 0;
    806457        return 0;
    807458}
    808459
     460#if defined(__GNUC__) && defined(__i386__)
     461static inline UINT32
     462ROTL32( UINT32 x, int s)
     463{
     464        __asm__("roll %%cl,%0"
     465                :"=r" (x)
     466                :"0" (x),"c" (s));
     467        return x;
     468}
     469#else
     470#define ROTL32(x, s) (((x) << (s)) | ((x) >> (32 - (s))))
     471#endif
     472
     473
     474#define FF(a, b, c, d, w, s, t) \
     475        a += ((b&(c^d))^d) + w + t;     \
     476        a = ROTL32(a, s);       \
     477        a += b;
     478
     479#define GG(a, b, c, d, w, s, t) \
     480        a += ((d&(b^c))^c) + w + t;     \
     481        a = ROTL32(a, s);       \
     482        a += b;
     483
     484#define HH(a, b, c, d, w, s, t) \
     485        a += (b^c^d) + w + t;   \
     486        a = ROTL32(a, s);       \
     487        a += b;
     488
     489#define II(a, b, c, d, w, s, t) \
     490        a += (c^(b|~d)) + w + t;        \
     491        a = ROTL32(a, s);       \
     492        a += b;
     493
     494#if WORDS_BIGENDIAN
     495uint32 swapu32(uint32 n)
     496{
     497        return (    ((n & 0xffU) << 24) |
     498                                ((n & 0xff00U) << 8) |
     499                                ((n & 0xff0000U) >> 8) |
     500                                ((n & 0xff000000U) >> 24) );
     501}
     502#endif
     503
     504static
     505void md5Process(md5Param* p)
     506{
     507        register uint32 a,b,c,d;
     508        register uint32* w;
     509        #if WORDS_BIGENDIAN
     510        register sh_byte t;
     511        #endif
     512
     513        w = p->data;
     514        #if WORDS_BIGENDIAN
     515        t = 16;
     516        while (t--)
     517        {
     518                register uint32 temp = swapu32(*w);
     519                *(w++) = temp;
     520        }
     521        w = p->data;
     522        #endif
     523
     524        a = p->h[0]; b = p->h[1]; c = p->h[2]; d = p->h[3];
     525
     526        FF(a, b, c, d, (*w++),  7, 0xd76aa478);
     527        FF(d, a, b, c, (*w++), 12, 0xe8c7b756);
     528        FF(c, d, a, b, (*w++), 17, 0x242070db);
     529        FF(b, c, d, a, (*w++), 22, 0xc1bdceee);
     530        FF(a, b, c, d, (*w++),  7, 0xf57c0faf);
     531        FF(d, a, b, c, (*w++), 12, 0x4787c62a);
     532        FF(c, d, a, b, (*w++), 17, 0xa8304613);
     533        FF(b, c, d, a, (*w++), 22, 0xfd469501);
     534        FF(a, b, c, d, (*w++),  7, 0x698098d8);
     535        FF(d, a, b, c, (*w++), 12, 0x8b44f7af);
     536        FF(c, d, a, b, (*w++), 17, 0xffff5bb1);
     537        FF(b, c, d, a, (*w++), 22, 0x895cd7be);
     538        FF(a, b, c, d, (*w++),  7, 0x6b901122);
     539        FF(d, a, b, c, (*w++), 12, 0xfd987193);
     540        FF(c, d, a, b, (*w++), 17, 0xa679438e);
     541        FF(b, c, d, a, (*w++), 22, 0x49b40821);
     542
     543        w = p->data;
     544
     545        GG(a, b, c, d, w[ 1],  5, 0xf61e2562);
     546        GG(d, a, b, c, w[ 6],  9, 0xc040b340);
     547        GG(c, d, a, b, w[11], 14, 0x265e5a51);
     548        GG(b, c, d, a, w[ 0], 20, 0xe9b6c7aa);
     549        GG(a, b, c, d, w[ 5],  5, 0xd62f105d);
     550        GG(d, a, b, c, w[10],  9, 0x02441453);
     551        GG(c, d, a, b, w[15], 14, 0xd8a1e681);
     552        GG(b, c, d, a, w[ 4], 20, 0xe7d3fbc8);
     553        GG(a, b, c, d, w[ 9],  5, 0x21e1cde6);
     554        GG(d, a, b, c, w[14],  9, 0xc33707d6);
     555        GG(c, d, a, b, w[ 3], 14, 0xf4d50d87);
     556        GG(b, c, d, a, w[ 8], 20, 0x455a14ed);
     557        GG(a, b, c, d, w[13],  5, 0xa9e3e905);
     558        GG(d, a, b, c, w[ 2],  9, 0xfcefa3f8);
     559        GG(c, d, a, b, w[ 7], 14, 0x676f02d9);
     560        GG(b, c, d, a, w[12], 20, 0x8d2a4c8a);
     561
     562        HH(a, b, c, d, w[ 5],  4, 0xfffa3942);
     563        HH(d, a, b, c, w[ 8], 11, 0x8771f681);
     564        HH(c, d, a, b, w[11], 16, 0x6d9d6122);
     565        HH(b, c, d, a, w[14], 23, 0xfde5380c);
     566        HH(a, b, c, d, w[ 1],  4, 0xa4beea44);
     567        HH(d, a, b, c, w[ 4], 11, 0x4bdecfa9);
     568        HH(c, d, a, b, w[ 7], 16, 0xf6bb4b60);
     569        HH(b, c, d, a, w[10], 23, 0xbebfbc70);
     570        HH(a, b, c, d, w[13],  4, 0x289b7ec6);
     571        HH(d, a, b, c, w[ 0], 11, 0xeaa127fa);
     572        HH(c, d, a, b, w[ 3], 16, 0xd4ef3085);
     573        HH(b, c, d, a, w[ 6], 23, 0x04881d05);
     574        HH(a, b, c, d, w[ 9],  4, 0xd9d4d039);
     575        HH(d, a, b, c, w[12], 11, 0xe6db99e5);
     576        HH(c, d, a, b, w[15], 16, 0x1fa27cf8);
     577        HH(b, c, d, a, w[ 2], 23, 0xc4ac5665);
     578
     579        II(a, b, c, d, w[ 0],  6, 0xf4292244);
     580        II(d, a, b, c, w[ 7], 10, 0x432aff97);
     581        II(c, d, a, b, w[14], 15, 0xab9423a7);
     582        II(b, c, d, a, w[ 5], 21, 0xfc93a039);
     583        II(a, b, c, d, w[12],  6, 0x655b59c3);
     584        II(d, a, b, c, w[ 3], 10, 0x8f0ccc92);
     585        II(c, d, a, b, w[10], 15, 0xffeff47d);
     586        II(b, c, d, a, w[ 1], 21, 0x85845dd1);
     587        II(a, b, c, d, w[ 8],  6, 0x6fa87e4f);
     588        II(d, a, b, c, w[15], 10, 0xfe2ce6e0);
     589        II(c, d, a, b, w[ 6], 15, 0xa3014314);
     590        II(b, c, d, a, w[13], 21, 0x4e0811a1);
     591        II(a, b, c, d, w[ 4],  6, 0xf7537e82);
     592        II(d, a, b, c, w[11], 10, 0xbd3af235);
     593        II(c, d, a, b, w[ 2], 15, 0x2ad7d2bb);
     594        II(b, c, d, a, w[ 9], 21, 0xeb86d391);
     595
     596        p->h[0] += a;
     597        p->h[1] += b;
     598        p->h[2] += c;
     599        p->h[3] += d;
     600}
     601
    809602int md5Update(md5Param* p, const sh_byte* data, int size)
    810603{
    811   md5_process_bytes(data, size, p);
    812   return 0;
    813 }
    814 
    815 static void md5Finish(md5Param* p, void *resblock)
    816 {
    817   (void) md5_finish_ctx(p, resblock);
    818 }
    819 
    820 int md5Digest(md5Param* p, md5_uint32* data)
    821 {
    822         md5Finish(p, data);
     604        register int proclength;
     605
     606        while (size > 0)
     607        {
     608          proclength = (((int)p->offset + size) > 64) ?
     609            (64 - (int)p->offset) : size;
     610          memcpy(((sh_byte *) p->data) + p->offset, data, (size_t) proclength);
     611          size -= proclength;
     612          data += proclength;
     613          p->offset += proclength;
     614         
     615          if (p->offset == (uint8) 64)
     616            {
     617              md5Process(p);
     618              p->offset = (uint8) 0;
     619              p->nblocks++;
     620            }
     621        }
     622        return 0;
     623}
     624
     625static void md5Finish(md5Param* p)
     626{
     627        uint32 t, msb, lsb;
     628        uint8 * pp;
     629        register uint8 *ptr;
     630
     631        msb = 0;
     632        t = p->nblocks;
     633        if( (lsb = t << 6) < t ) /* multiply by 64 to make a byte count */
     634          msb++;
     635        msb += t >> 26;
     636        t = lsb;
     637        if( (lsb = t + (uint32)p->offset) < t ) /* add the count */
     638          msb++;
     639        t = lsb;
     640        if( (lsb = t << 3) < t ) /* multiply by 8 to make a bit count */
     641          msb++;
     642        msb += t >> 29;
     643
     644        ptr = ((uint8 *) p->data) + p->offset++;
     645
     646 
     647        *(ptr++) = (uint8) 0x80;
     648
     649        if (p->offset > (uint8)56)
     650        {
     651                while (p->offset++ < 64)
     652                        *(ptr++) = 0;
     653
     654                md5Process(p);
     655                p->offset = 0;
     656        }
     657
     658        ptr = ((uint8 *) p->data) + p->offset;
     659        while (p->offset++ < 56)
     660                *(ptr++) = 0;
     661
     662        /* append the 64 bit count */
     663        *(ptr++) = lsb     ;
     664        *(ptr++) = lsb >>  8;
     665        *(ptr++) = lsb >> 16;
     666        *(ptr++) = lsb >> 24;
     667        *(ptr++) = msb     ;
     668        *(ptr++) = msb >>  8;
     669        *(ptr++) = msb >> 16;
     670        *(ptr++) = msb >> 24;
     671
     672        md5Process(p);
     673
     674        pp = (uint8 *) p->data;
     675#ifdef WORDS_BIGENDIAN
     676#define X(a) do { *pp++ = (*p).a; *pp++ = (*p).a >> 8;      \
     677                  *pp++ = (*p).a >> 16; *pp++ = (*p).a >> 24; } while(0)
     678#else /* little endian */
     679    /*#define X(a) do { *(uint32*)p = p->##a ; p += 4; } while(0)*/
     680    /* Unixware's cpp doesn't like the above construct so we do it his way:
     681     * (reported by Allan Clark) */
     682#define X(a) do { *(uint32*)pp = (*p).a ; pp += 4; } while(0)
     683#endif
     684        X(h[0]);
     685        X(h[1]);
     686        X(h[2]);
     687        X(h[3]);
     688#undef X
     689
     690        p->offset = 0;
     691}
     692
     693int md5Digest(md5Param* p, uint32* data)
     694{
     695        md5Finish(p);
     696        memcpy(data, p->h, 16);
    823697        (void) md5Reset(p);
    824698        return 0;
    825699}
    826700/*@+type@*/
    827 
    828701
    829702/* Compute MD5 message digest for bytes read from STREAM.  The
     
    835708  /* Important: BLOCKSIZE must be a multiple of 64.  */
    836709  static const int BLOCKSIZE = 8192;
    837   struct md5_ctx ctx;
     710  md5Param ctx;
    838711  char buffer[8264]; /* BLOCKSIZE + 72  AIX compiler chokes */
    839   size_t sum;
    840 
     712  off_t sum = 0;
    841713  SL_TICKET  fd;
    842714  char * tmp;
     
    925797       BLOCKSIZE % 64 == 0
    926798    */
    927     md5_process_block(buffer, BLOCKSIZE, &ctx);
     799    (void) md5Update(&ctx, (sh_byte*) buffer, BLOCKSIZE);
    928800    sh.statistics.bytes_hashed += BLOCKSIZE;
    929801
     
    944816  if (sum > 0)
    945817    {
    946       md5_process_bytes(buffer, sum, &ctx);
     818      (void) md5Update(&ctx, (sh_byte*) buffer, (int) sum);
    947819      sh.statistics.bytes_hashed += BLOCKSIZE;
    948820    }
     
    15201392}
    15211393
    1522 int sh_tiger_hashtype (const char * c)
     1394int sh_tiger_hashtype (char * c)
    15231395{
    15241396  SL_ENTER( _("sh_tiger_hashtype"));
     
    15881460  if (res != NULL)
    15891461    {
     1462      /*@-bufferoverflowhigh -formatconst@*/
    15901463#if defined(TIGER_64_BIT)
    1591       sl_snprintf(out,
    1592                   sizeof(out),
    1593                   MYFORMAT,
    1594                   (sh_word32)(res[0]>>32),
    1595                   (sh_word32)(res[0]),
    1596                   (sh_word32)(res[1]>>32),
    1597                   (sh_word32)(res[1]),
    1598                   (sh_word32)(res[2]>>32),
    1599                   (sh_word32)(res[2]) );
     1464      sprintf(out,                                   /* known to fit  */
     1465              MYFORMAT,
     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]) );
    16001472#else
    1601       sl_snprintf(out,
    1602                   sizeof(out),
    1603                   MYFORMAT,
    1604                   (sh_word32)(res[1]),
    1605                   (sh_word32)(res[0]),
    1606                   (sh_word32)(res[3]),
    1607                   (sh_word32)(res[2]),
    1608                   (sh_word32)(res[5]),
    1609                   (sh_word32)(res[4]) );
    1610 #endif
    1611       out[sizeof(out)-1] = '\0';
     1473      sprintf(out,                                   /* known to fit  */
     1474              MYFORMAT,
     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      out[KEY_LEN] = '\0';
    16121484      SL_RETURN( out, _("sh_tiger_hash_internal"));
    16131485
     
    16351507  if (res != NULL)
    16361508    {
     1509      /*@-bufferoverflowhigh -formatconst@*/
    16371510#if defined(TIGER_64_BIT)
    1638       sl_snprintf(outhash,
    1639                   sizeof(outhash),
    1640                   GPGFORMAT,
    1641                   (sh_word32)(res[0]>>32),
    1642                   (sh_word32)(res[0]),
    1643                   (sh_word32)(res[1]>>32),
    1644                   (sh_word32)(res[1]),
    1645                   (sh_word32)(res[2]>>32),
    1646                   (sh_word32)(res[2]) );
     1511      sprintf(outhash,                               /* known to fit  */
     1512              GPGFORMAT,
     1513              (sh_word32)(res[0]>>32),
     1514              (sh_word32)(res[0]),
     1515              (sh_word32)(res[1]>>32),
     1516              (sh_word32)(res[1]),
     1517              (sh_word32)(res[2]>>32),
     1518              (sh_word32)(res[2]) );
    16471519#else
    1648       sl_snprintf(outhash,
    1649                   sizeof(outhash),
    1650                   GPGFORMAT,
    1651                   (sh_word32)(res[1]),
    1652                   (sh_word32)(res[0]),
    1653                   (sh_word32)(res[3]),
    1654                   (sh_word32)(res[2]),
    1655                   (sh_word32)(res[5]),
    1656                   (sh_word32)(res[4]) );
    1657 #endif
    1658       outhash[sizeof(outhash)-1] = '\0';
     1520      sprintf(outhash,                               /* known to fit  */
     1521              GPGFORMAT,
     1522              (sh_word32)(res[1]),
     1523              (sh_word32)(res[0]),
     1524              (sh_word32)(res[3]),
     1525              (sh_word32)(res[2]),
     1526              (sh_word32)(res[5]),
     1527              (sh_word32)(res[4]) );
     1528#endif
     1529      /*@+bufferoverflowhigh@*/
     1530      outhash[48 + 6] = '\0';
    16591531    }
    16601532  else
    16611533    {
    1662       sl_strlcpy(outhash,
    1663                  _("00000000 00000000 00000000  00000000 00000000 00000000"),
    1664                  sizeof(outhash));
     1534      /*@-bufferoverflowhigh@*/
     1535      sprintf(outhash,                               /* known to fit  */
     1536              _("00000000 00000000 00000000  00000000 00000000 00000000"));
     1537      /*@+bufferoverflowhigh@*/
    16651538    }
    16661539
Note: See TracChangeset for help on using the changeset viewer.