00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 #include <string.h>
00047 #include "md5.h"
00048
00049
00050 typedef unsigned char *POINTER;
00051
00052
00053
00054 #define S11 7
00055 #define S12 12
00056 #define S13 17
00057 #define S14 22
00058 #define S21 5
00059 #define S22 9
00060 #define S23 14
00061 #define S24 20
00062 #define S31 4
00063 #define S32 11
00064 #define S33 16
00065 #define S34 23
00066 #define S41 6
00067 #define S42 10
00068 #define S43 15
00069 #define S44 21
00070
00071 static void MD5Transform (uint32_t [4], const unsigned char [64]);
00072
00073 #if BYTE_ORDER == LITTLE_ENDIAN
00074 #define Encode memcpy
00075 #define Decode memcpy
00076 #else
00077 static void Encode (unsigned char *, uint32_t *, unsigned int);
00078 static void Decode (uint32_t *, const unsigned char *, unsigned int);
00079 #endif
00080
00081 static unsigned char PADDING[64] = {
00082 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00083 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00084 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
00085 };
00086
00087
00088
00089 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
00090 #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
00091 #define H(x, y, z) ((x) ^ (y) ^ (z))
00092 #define I(x, y, z) ((y) ^ ((x) | (~z)))
00093
00094
00095
00096 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
00097
00098
00099
00100
00101 #define FF(a, b, c, d, x, s, ac) { \
00102 (a) += F ((b), (c), (d)) + (x) + (uint32_t)(ac); \
00103 (a) = ROTATE_LEFT ((a), (s)); \
00104 (a) += (b); \
00105 }
00106 #define GG(a, b, c, d, x, s, ac) { \
00107 (a) += G ((b), (c), (d)) + (x) + (uint32_t)(ac); \
00108 (a) = ROTATE_LEFT ((a), (s)); \
00109 (a) += (b); \
00110 }
00111 #define HH(a, b, c, d, x, s, ac) { \
00112 (a) += H ((b), (c), (d)) + (x) + (uint32_t)(ac); \
00113 (a) = ROTATE_LEFT ((a), (s)); \
00114 (a) += (b); \
00115 }
00116 #define II(a, b, c, d, x, s, ac) { \
00117 (a) += I ((b), (c), (d)) + (x) + (uint32_t)(ac); \
00118 (a) = ROTATE_LEFT ((a), (s)); \
00119 (a) += (b); \
00120 }
00121
00122 #if BYTE_ORDER != LITTLE_ENDIAN
00123
00124
00125
00126 static void Encode (output, input, len)
00127 unsigned char *output;
00128 uint32_t *input;
00129 unsigned int len;
00130 {
00131 unsigned int i, j;
00132
00133 for (i = 0, j = 0; j < len; i++, j += 4) {
00134 output[j] = (unsigned char)(input[i] & 0xff);
00135 output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
00136 output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
00137 output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
00138 }
00139 }
00140
00141
00142
00143
00144 static void Decode (output, input, len)
00145 uint32_t *output;
00146 const unsigned char *input;
00147 unsigned int len;
00148 {
00149 unsigned int i, j;
00150
00151 for (i = 0, j = 0; j < len; i++, j += 4)
00152 output[i] = ((uint32_t)input[j]) | (((uint32_t)input[j+1]) << 8) |
00153 (((uint32_t)input[j+2]) << 16) | (((uint32_t)input[j+3]) << 24);
00154 }
00155 #endif
00156
00157
00158
00159 void MD5Init (context)
00160 MD5_CTX *context;
00161 {
00162 context->count[0] = context->count[1] = 0;
00163
00164 context->state[0] = 0x67452301;
00165 context->state[1] = 0xefcdab89;
00166 context->state[2] = 0x98badcfe;
00167 context->state[3] = 0x10325476;
00168 }
00169
00170
00171
00172
00173
00174 void MD5Update (context, input, inputLen)
00175 MD5_CTX *context;
00176 const unsigned char *input;
00177 unsigned int inputLen;
00178 {
00179 unsigned int i, index, partLen;
00180
00181
00182 index = (unsigned int)((context->count[0] >> 3) & 0x3F);
00183
00184
00185 if ((context->count[0] += ((uint32_t)inputLen << 3))
00186 < ((uint32_t)inputLen << 3))
00187 context->count[1]++;
00188 context->count[1] += ((uint32_t)inputLen >> 29);
00189
00190 partLen = 64 - index;
00191
00192
00193 if (inputLen >= partLen) {
00194 memcpy ((POINTER)&context->buffer[index], (POINTER)input, partLen);
00195 MD5Transform (context->state, context->buffer);
00196
00197 for (i = partLen; i + 63 < inputLen; i += 64)
00198 MD5Transform (context->state, &input[i]);
00199
00200 index = 0;
00201 }
00202 else
00203 i = 0;
00204
00205
00206 memcpy((POINTER)&context->buffer[index], (POINTER)&input[i],
00207 inputLen-i);
00208 }
00209
00210
00211
00212
00213 void MD5Final (digest, context)
00214 unsigned char digest[16];
00215 MD5_CTX *context;
00216 {
00217 unsigned char bits[8];
00218 unsigned int index, padLen;
00219
00220
00221 Encode (bits, context->count, 8);
00222
00223
00224 index = (unsigned int)((context->count[0] >> 3) & 0x3f);
00225 padLen = (index < 56) ? (56 - index) : (120 - index);
00226 MD5Update (context, PADDING, padLen);
00227
00228
00229 MD5Update (context, bits, 8);
00230
00231 Encode (digest, context->state, 16);
00232
00233
00234 memset ((POINTER)context, 0, sizeof (*context));
00235 }
00236
00237
00238
00239 static void MD5Transform (state, block)
00240 uint32_t state[4];
00241 const unsigned char block[64];
00242 {
00243 uint32_t a = state[0], b = state[1], c = state[2], d = state[3], x[16];
00244
00245 Decode (x, block, 64);
00246
00247
00248 FF (a, b, c, d, x[ 0], S11, 0xd76aa478);
00249 FF (d, a, b, c, x[ 1], S12, 0xe8c7b756);
00250 FF (c, d, a, b, x[ 2], S13, 0x242070db);
00251 FF (b, c, d, a, x[ 3], S14, 0xc1bdceee);
00252 FF (a, b, c, d, x[ 4], S11, 0xf57c0faf);
00253 FF (d, a, b, c, x[ 5], S12, 0x4787c62a);
00254 FF (c, d, a, b, x[ 6], S13, 0xa8304613);
00255 FF (b, c, d, a, x[ 7], S14, 0xfd469501);
00256 FF (a, b, c, d, x[ 8], S11, 0x698098d8);
00257 FF (d, a, b, c, x[ 9], S12, 0x8b44f7af);
00258 FF (c, d, a, b, x[10], S13, 0xffff5bb1);
00259 FF (b, c, d, a, x[11], S14, 0x895cd7be);
00260 FF (a, b, c, d, x[12], S11, 0x6b901122);
00261 FF (d, a, b, c, x[13], S12, 0xfd987193);
00262 FF (c, d, a, b, x[14], S13, 0xa679438e);
00263 FF (b, c, d, a, x[15], S14, 0x49b40821);
00264
00265
00266 GG (a, b, c, d, x[ 1], S21, 0xf61e2562);
00267 GG (d, a, b, c, x[ 6], S22, 0xc040b340);
00268 GG (c, d, a, b, x[11], S23, 0x265e5a51);
00269 GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa);
00270 GG (a, b, c, d, x[ 5], S21, 0xd62f105d);
00271 GG (d, a, b, c, x[10], S22, 0x2441453);
00272 GG (c, d, a, b, x[15], S23, 0xd8a1e681);
00273 GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8);
00274 GG (a, b, c, d, x[ 9], S21, 0x21e1cde6);
00275 GG (d, a, b, c, x[14], S22, 0xc33707d6);
00276 GG (c, d, a, b, x[ 3], S23, 0xf4d50d87);
00277 GG (b, c, d, a, x[ 8], S24, 0x455a14ed);
00278 GG (a, b, c, d, x[13], S21, 0xa9e3e905);
00279 GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8);
00280 GG (c, d, a, b, x[ 7], S23, 0x676f02d9);
00281 GG (b, c, d, a, x[12], S24, 0x8d2a4c8a);
00282
00283
00284 HH (a, b, c, d, x[ 5], S31, 0xfffa3942);
00285 HH (d, a, b, c, x[ 8], S32, 0x8771f681);
00286 HH (c, d, a, b, x[11], S33, 0x6d9d6122);
00287 HH (b, c, d, a, x[14], S34, 0xfde5380c);
00288 HH (a, b, c, d, x[ 1], S31, 0xa4beea44);
00289 HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9);
00290 HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60);
00291 HH (b, c, d, a, x[10], S34, 0xbebfbc70);
00292 HH (a, b, c, d, x[13], S31, 0x289b7ec6);
00293 HH (d, a, b, c, x[ 0], S32, 0xeaa127fa);
00294 HH (c, d, a, b, x[ 3], S33, 0xd4ef3085);
00295 HH (b, c, d, a, x[ 6], S34, 0x4881d05);
00296 HH (a, b, c, d, x[ 9], S31, 0xd9d4d039);
00297 HH (d, a, b, c, x[12], S32, 0xe6db99e5);
00298 HH (c, d, a, b, x[15], S33, 0x1fa27cf8);
00299 HH (b, c, d, a, x[ 2], S34, 0xc4ac5665);
00300
00301
00302 II (a, b, c, d, x[ 0], S41, 0xf4292244);
00303 II (d, a, b, c, x[ 7], S42, 0x432aff97);
00304 II (c, d, a, b, x[14], S43, 0xab9423a7);
00305 II (b, c, d, a, x[ 5], S44, 0xfc93a039);
00306 II (a, b, c, d, x[12], S41, 0x655b59c3);
00307 II (d, a, b, c, x[ 3], S42, 0x8f0ccc92);
00308 II (c, d, a, b, x[10], S43, 0xffeff47d);
00309 II (b, c, d, a, x[ 1], S44, 0x85845dd1);
00310 II (a, b, c, d, x[ 8], S41, 0x6fa87e4f);
00311 II (d, a, b, c, x[15], S42, 0xfe2ce6e0);
00312 II (c, d, a, b, x[ 6], S43, 0xa3014314);
00313 II (b, c, d, a, x[13], S44, 0x4e0811a1);
00314 II (a, b, c, d, x[ 4], S41, 0xf7537e82);
00315 II (d, a, b, c, x[11], S42, 0xbd3af235);
00316 II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb);
00317 II (b, c, d, a, x[ 9], S44, 0xeb86d391);
00318
00319 state[0] += a;
00320 state[1] += b;
00321 state[2] += c;
00322 state[3] += d;
00323
00324
00325 memset ((POINTER)x, 0, sizeof (x));
00326 }
00327