Annotation of sbbs/src/smblib/md5.c, revision 1.1.1.1

1.1       root        1: /* md5.c - RSA Data Security, Inc., MD5 Message-Digest Algorithm */
                      2: 
                      3: /* $Id: md5.c,v 1.4 2005/06/06 23:08:21 deuce Exp $ */
                      4: 
                      5: /* NOTE: Numerous changes have been made; the following notice is
                      6: included to satisfy legal requirements.
                      7: 
                      8: Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
                      9: rights reserved.
                     10: 
                     11: License to copy and use this software is granted provided that it
                     12: is identified as the "RSA Data Security, Inc. MD5 Message-Digest
                     13: Algorithm" in all material mentioning or referencing this software
                     14: or this function.
                     15: 
                     16: License is also granted to make and use derivative works provided
                     17: that such works are identified as "derived from the RSA Data
                     18: Security, Inc. MD5 Message-Digest Algorithm" in all material
                     19: mentioning or referencing the derived work.
                     20: 
                     21: RSA Data Security, Inc. makes no representations concerning either
                     22: the merchantability of this software or the suitability of this
                     23: software for any particular purpose. It is provided "as is"
                     24: without express or implied warranty of any kind.
                     25: 
                     26: These notices must be retained in any copies of any part of this
                     27: documentation and/or software.
                     28: */
                     29: 
                     30: #include <memory.h>
                     31: #include "md5.h"
                     32: 
                     33: #if !defined(BIG_ENDIAN)
                     34:        #define LITTLE_ENDIAN   /* Little Endian by default */
                     35: #endif
                     36: 
                     37: void MD5CALL MD5_open(MD5 *md5)
                     38: {
                     39:   md5->count[0] = md5->count[1] = 0;
                     40:   /* Load magic initialization constants.*/
                     41:   md5->state[0] = 0x67452301;
                     42:   md5->state[1] = 0xefcdab89;
                     43:   md5->state[2] = 0x98badcfe;
                     44:   md5->state[3] = 0x10325476;
                     45: }
                     46: 
                     47: /* Constants for MD5Transform routine. */
                     48: 
                     49: #define S11 7
                     50: #define S12 12
                     51: #define S13 17
                     52: #define S14 22
                     53: #define S21 5
                     54: #define S22 9
                     55: #define S23 14
                     56: #define S24 20
                     57: #define S31 4
                     58: #define S32 11
                     59: #define S33 16
                     60: #define S34 23
                     61: #define S41 6
                     62: #define S42 10
                     63: #define S43 15
                     64: #define S44 21
                     65: 
                     66: /* F, G, H and I are basic MD5 functions. */
                     67: 
                     68: #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
                     69: #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
                     70: #define H(x, y, z) ((x) ^ (y) ^ (z))
                     71: #define I(x, y, z) ((y) ^ ((x) | (~z)))
                     72: 
                     73: /* ROTATE_LEFT rotates x left n bits. */
                     74: 
                     75: #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
                     76: 
                     77: /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
                     78:    Rotation is separate from addition to prevent recomputation.
                     79: */
                     80: 
                     81: #define FF(a, b, c, d, x, s, ac) { \
                     82:  (a) += F((b), (c), (d)) + (x) + (UINT4)(ac); \
                     83:  (a) = ROTATE_LEFT((a), (s)); \
                     84:  (a) += (b); \
                     85:   }
                     86: #define GG(a, b, c, d, x, s, ac) { \
                     87:  (a) += G((b), (c), (d)) + (x) + (UINT4)(ac); \
                     88:  (a) = ROTATE_LEFT((a), (s)); \
                     89:  (a) += (b); \
                     90:   }
                     91: #define HH(a, b, c, d, x, s, ac) { \
                     92:  (a) += H((b), (c), (d)) + (x) + (UINT4)(ac); \
                     93:  (a) = ROTATE_LEFT((a), (s)); \
                     94:  (a) += (b); \
                     95:   }
                     96: #define II(a, b, c, d, x, s, ac) { \
                     97:  (a) += I((b), (c), (d)) + (x) + (UINT4)(ac); \
                     98:  (a) = ROTATE_LEFT((a), (s)); \
                     99:  (a) += (b); \
                    100:   }
                    101: 
                    102: 
                    103: /* MD5 basic transformation. Transforms state based on block. */
                    104: 
                    105: static void MD5Transform(UINT4 state[4], const BYTE block[64])
                    106: {
                    107:   UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[MD5_DIGEST_SIZE];
                    108:   /* Move contents of block to x, putting bytes in little-endian order. */
                    109:   #ifdef LITTLE_ENDIAN
                    110:     memcpy(x, block, 64);
                    111:   #else
                    112:   {
                    113:     unsigned int i, j;
                    114:     for (i = j = 0; i < MD5_DIGEST_SIZE; i++, j+= 4)
                    115:     {
                    116:       x[i] = (UINT4) block[j] | (UINT4) block[j+1] << 8 |
                    117:         (UINT4) block[j+2] << 16 | (UINT4) block[j+3] << 24;
                    118:     }
                    119:   }
                    120:   #endif
                    121:   /* Round 1 */
                    122:   FF(a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
                    123:   FF(d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
                    124:   FF(c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
                    125:   FF(b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
                    126:   FF(a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
                    127:   FF(d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
                    128:   FF(c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
                    129:   FF(b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
                    130:   FF(a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
                    131:   FF(d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
                    132:   FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
                    133:   FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
                    134:   FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
                    135:   FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
                    136:   FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
                    137:   FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
                    138:   /* Round 2 */
                    139:   GG(a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
                    140:   GG(d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
                    141:   GG(c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
                    142:   GG(b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
                    143:   GG(a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
                    144:   GG(d, a, b, c, x[10], S22,  0x2441453); /* 22 */
                    145:   GG(c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
                    146:   GG(b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
                    147:   GG(a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
                    148:   GG(d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
                    149:   GG(c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
                    150:   GG(b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
                    151:   GG(a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
                    152:   GG(d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
                    153:   GG(c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
                    154:   GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
                    155:   /* Round 3 */
                    156:   HH(a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
                    157:   HH(d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
                    158:   HH(c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
                    159:   HH(b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
                    160:   HH(a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
                    161:   HH(d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
                    162:   HH(c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
                    163:   HH(b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
                    164:   HH(a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
                    165:   HH(d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
                    166:   HH(c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
                    167:   HH(b, c, d, a, x[ 6], S34,  0x4881d05); /* 44 */
                    168:   HH(a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
                    169:   HH(d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
                    170:   HH(c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
                    171:   HH(b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
                    172:   /* Round 4 */
                    173:   II(a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
                    174:   II(d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
                    175:   II(c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
                    176:   II(b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
                    177:   II(a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
                    178:   II(d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
                    179:   II(c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
                    180:   II(b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
                    181:   II(a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
                    182:   II(d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
                    183:   II(c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
                    184:   II(b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
                    185:   II(a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
                    186:   II(d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
                    187:   II(c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
                    188:   II(b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
                    189:   state[0] += a;
                    190:   state[1] += b;
                    191:   state[2] += c;
                    192:   state[3] += d;
                    193:   /* Zeroize sensitive information. */
                    194:   memset(x, 0, sizeof(x));
                    195: }
                    196: 
                    197: void MD5CALL MD5_digest(MD5 *md5, const void *input, size_t inputLen)
                    198: {
                    199:   unsigned int i, index, partLen;
                    200:   /* Compute number of bytes mod 64 */
                    201:   index = (unsigned int)((md5->count[0] >> 3) & 0x3F);
                    202:   /* Update number of bits */
                    203:   if ((md5->count[0] += ((UINT4)inputLen << 3)) < ((UINT4)inputLen << 3))
                    204:     md5->count[1]++;
                    205:   md5->count[1] += ((UINT4)inputLen >> 29);
                    206:   partLen = 64 - index;
                    207:   /* Transform as many times as possible.*/
                    208:   if (inputLen >= partLen)
                    209:   {
                    210:     memcpy(&md5->buffer[index], input, partLen);
                    211:     MD5Transform(md5->state, md5->buffer);
                    212:     for (i = partLen; i + 63 < inputLen; i += 64)
                    213:       MD5Transform(md5->state, (char *) input + i);
                    214:     index = 0;
                    215:   }
                    216:   else
                    217:     i = 0;
                    218:   /* Buffer remaining input */
                    219:   memcpy(&md5->buffer[index], (char *) input + i, inputLen-i);
                    220: }
                    221: 
                    222: /* ENCODE packs a 32-bit unsigned integer into 4 bytes in little-endian
                    223:    order.
                    224: */
                    225: 
                    226: #ifdef LITTLE_ENDIAN
                    227: #define ENCODE(p,n) *(UINT4 *)(p) = n
                    228: #else
                    229: #define ENCODE(p,n) (p)[0]=n,(p)[1]=n>>8,(p)[2]=n>>16,(p)[3]=n>>24
                    230: #endif
                    231: 
                    232: void MD5CALL MD5_close(MD5 *md5, BYTE digest[MD5_DIGEST_SIZE])
                    233: {
                    234:   BYTE bits[8];
                    235:   unsigned int index, padLen;
                    236:   static BYTE PADDING[64] =
                    237:   {
                    238:     0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    239:     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    240:     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
                    241:   };
                    242:   /* Save number of bits */
                    243:   ENCODE(bits, md5->count[0]);
                    244:   ENCODE(bits+4, md5->count[1]);
                    245:   /* Pad out to 56 mod 64. */
                    246:   index = (unsigned int)((md5->count[0] >> 3) & 0x3f);
                    247:   padLen = (index < 56) ? (56 - index) : (120 - index);
                    248:   MD5_digest(md5, PADDING, padLen);
                    249:   /* Append length (before padding) */
                    250:   MD5_digest(md5, bits, 8);
                    251:   /* Store state in digest */
                    252:   ENCODE(digest, md5->state[0]);
                    253:   ENCODE(digest+4, md5->state[1]);
                    254:   ENCODE(digest+8, md5->state[2]);
                    255:   ENCODE(digest+12, md5->state[3]);
                    256:   /* Zeroize sensitive information. */
                    257:   memset(md5, 0, sizeof(MD5));
                    258: }
                    259: 
                    260: BYTE* MD5CALL MD5_calc(BYTE digest[MD5_DIGEST_SIZE], const void* buf, size_t len)
                    261: {
                    262:        MD5 ctx;
                    263: 
                    264:        MD5_open(&ctx);
                    265:        MD5_digest(&ctx,buf,len);
                    266:        MD5_close(&ctx,digest);
                    267: 
                    268:        return(digest);
                    269: }
                    270: 
                    271: /* conversion for 16 character binary md5 to hex */
                    272: 
                    273: BYTE* MD5CALL MD5_hex(BYTE* to, const BYTE digest[MD5_DIGEST_SIZE])
                    274: {
                    275:        BYTE const* from = digest;
                    276:     static char *hexdigits = "0123456789abcdef";
                    277:     const BYTE *end = digest + MD5_DIGEST_SIZE;
                    278:     char *d = (char *)to;
                    279: 
                    280:     while (from < end) {
                    281:                *d++ = hexdigits[(*from >> 4)];
                    282:                *d++ = hexdigits[(*from & 0x0F)];
                    283:                from++;
                    284:     }
                    285:     *d = '\0';
                    286:     return to;
                    287: }
                    288: 
                    289: #ifdef MD5_TEST
                    290: 
                    291: int main(int argc, char**argv)
                    292: {
                    293:        int             i;
                    294:        char    hexbuf[(MD5_DIGEST_SIZE*2)+1];
                    295:        BYTE    digest[MD5_DIGEST_SIZE];
                    296: 
                    297:        for(i=1;i<argc;i++)
                    298:                printf("%s\n"
                    299:                        ,MD5_hex(hexbuf,MD5_calc(digest,argv[i],strlen(argv[i]))));
                    300: 
                    301:        return 0;
                    302: }
                    303: 
                    304: #endif

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.