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