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