Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / lib / md5.c
blobef35767f67c7ba97efa690ac9c0d40abb3df0d6c
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * $Id: md5.c,v 1.1.1.1 2008-09-23 16:32:05 hoffman Exp $
22 ***************************************************************************/
24 #include "setup.h"
26 #ifndef CURL_DISABLE_CRYPTO_AUTH
28 #include <string.h>
30 #ifdef USE_SSLEAY
31 /* When OpenSSL is available we use the MD5-function from OpenSSL */
33 # ifdef USE_OPENSSL
34 # include <openssl/md5.h>
35 # else
36 # include <md5.h>
37 # endif
39 #else /* USE_SSLEAY */
40 /* When OpenSSL is not available we use this code segment */
42 /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
43 rights reserved.
45 License to copy and use this software is granted provided that it
46 is identified as the "RSA Data Security, Inc. MD5 Message-Digest
47 Algorithm" in all material mentioning or referencing this software
48 or this function.
50 License is also granted to make and use derivative works provided
51 that such works are identified as "derived from the RSA Data
52 Security, Inc. MD5 Message-Digest Algorithm" in all material
53 mentioning or referencing the derived work.
55 RSA Data Security, Inc. makes no representations concerning either
56 the merchantability of this software or the suitability of this
57 software for any particular purpose. It is provided "as is"
58 without express or implied warranty of any kind.
60 These notices must be retained in any copies of any part of this
61 documentation and/or software.
64 /* UINT4 defines a four byte word */
65 typedef unsigned int UINT4;
67 /* MD5 context. */
68 struct md5_ctx {
69 UINT4 state[4]; /* state (ABCD) */
70 UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */
71 unsigned char buffer[64]; /* input buffer */
74 typedef struct md5_ctx MD5_CTX;
76 static void MD5_Init(struct md5_ctx *);
77 static void MD5_Update(struct md5_ctx *, const unsigned char *, unsigned int);
78 static void MD5_Final(unsigned char [16], struct md5_ctx *);
80 /* Constants for MD5Transform routine.
83 #define S11 7
84 #define S12 12
85 #define S13 17
86 #define S14 22
87 #define S21 5
88 #define S22 9
89 #define S23 14
90 #define S24 20
91 #define S31 4
92 #define S32 11
93 #define S33 16
94 #define S34 23
95 #define S41 6
96 #define S42 10
97 #define S43 15
98 #define S44 21
100 static void MD5Transform(UINT4 [4], const unsigned char [64]);
101 static void Encode(unsigned char *, UINT4 *, unsigned int);
102 static void Decode(UINT4 *, const unsigned char *, unsigned int);
104 static const unsigned char PADDING[64] = {
105 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
106 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
107 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
110 /* F, G, H and I are basic MD5 functions.
112 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
113 #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
114 #define H(x, y, z) ((x) ^ (y) ^ (z))
115 #define I(x, y, z) ((y) ^ ((x) | (~z)))
117 /* ROTATE_LEFT rotates x left n bits.
119 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
121 /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
122 Rotation is separate from addition to prevent recomputation.
124 #define FF(a, b, c, d, x, s, ac) { \
125 (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
126 (a) = ROTATE_LEFT ((a), (s)); \
127 (a) += (b); \
129 #define GG(a, b, c, d, x, s, ac) { \
130 (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
131 (a) = ROTATE_LEFT ((a), (s)); \
132 (a) += (b); \
134 #define HH(a, b, c, d, x, s, ac) { \
135 (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
136 (a) = ROTATE_LEFT ((a), (s)); \
137 (a) += (b); \
139 #define II(a, b, c, d, x, s, ac) { \
140 (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
141 (a) = ROTATE_LEFT ((a), (s)); \
142 (a) += (b); \
145 /* MD5 initialization. Begins an MD5 operation, writing a new context.
147 static void MD5_Init(struct md5_ctx *context)
149 context->count[0] = context->count[1] = 0;
150 /* Load magic initialization constants. */
151 context->state[0] = 0x67452301;
152 context->state[1] = 0xefcdab89;
153 context->state[2] = 0x98badcfe;
154 context->state[3] = 0x10325476;
157 /* MD5 block update operation. Continues an MD5 message-digest
158 operation, processing another message block, and updating the
159 context.
161 static void MD5_Update (struct md5_ctx *context, /* context */
162 const unsigned char *input, /* input block */
163 unsigned int inputLen) /* length of input block */
165 unsigned int i, bufindex, partLen;
167 /* Compute number of bytes mod 64 */
168 bufindex = (unsigned int)((context->count[0] >> 3) & 0x3F);
170 /* Update number of bits */
171 if((context->count[0] += ((UINT4)inputLen << 3))
172 < ((UINT4)inputLen << 3))
173 context->count[1]++;
174 context->count[1] += ((UINT4)inputLen >> 29);
176 partLen = 64 - bufindex;
178 /* Transform as many times as possible. */
179 if(inputLen >= partLen) {
180 memcpy((void *)&context->buffer[bufindex], (void *)input, partLen);
181 MD5Transform(context->state, context->buffer);
183 for (i = partLen; i + 63 < inputLen; i += 64)
184 MD5Transform(context->state, &input[i]);
186 bufindex = 0;
188 else
189 i = 0;
191 /* Buffer remaining input */
192 memcpy((void *)&context->buffer[bufindex], (void *)&input[i], inputLen-i);
195 /* MD5 finalization. Ends an MD5 message-digest operation, writing the
196 the message digest and zeroizing the context.
198 static void MD5_Final(unsigned char digest[16], /* message digest */
199 struct md5_ctx *context) /* context */
201 unsigned char bits[8];
202 unsigned int count, padLen;
204 /* Save number of bits */
205 Encode (bits, context->count, 8);
207 /* Pad out to 56 mod 64. */
208 count = (unsigned int)((context->count[0] >> 3) & 0x3f);
209 padLen = (count < 56) ? (56 - count) : (120 - count);
210 MD5_Update (context, PADDING, padLen);
212 /* Append length (before padding) */
213 MD5_Update (context, bits, 8);
215 /* Store state in digest */
216 Encode (digest, context->state, 16);
218 /* Zeroize sensitive information. */
219 memset ((void *)context, 0, sizeof (*context));
222 /* MD5 basic transformation. Transforms state based on block. */
223 static void MD5Transform(UINT4 state[4],
224 const unsigned char block[64])
226 UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
228 Decode (x, block, 64);
230 /* Round 1 */
231 FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
232 FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
233 FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
234 FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
235 FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
236 FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
237 FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
238 FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
239 FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
240 FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
241 FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
242 FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
243 FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
244 FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
245 FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
246 FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
248 /* Round 2 */
249 GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
250 GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
251 GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
252 GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
253 GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
254 GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
255 GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
256 GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
257 GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
258 GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
259 GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
260 GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
261 GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
262 GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
263 GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
264 GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
266 /* Round 3 */
267 HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
268 HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
269 HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
270 HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
271 HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
272 HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
273 HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
274 HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
275 HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
276 HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
277 HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
278 HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
279 HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
280 HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
281 HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
282 HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
284 /* Round 4 */
285 II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
286 II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
287 II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
288 II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
289 II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
290 II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
291 II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
292 II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
293 II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
294 II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
295 II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
296 II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
297 II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
298 II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
299 II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
300 II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
302 state[0] += a;
303 state[1] += b;
304 state[2] += c;
305 state[3] += d;
307 /* Zeroize sensitive information. */
308 memset((void *)x, 0, sizeof (x));
311 /* Encodes input (UINT4) into output (unsigned char). Assumes len is
312 a multiple of 4.
314 static void Encode (unsigned char *output,
315 UINT4 *input,
316 unsigned int len)
318 unsigned int i, j;
320 for (i = 0, j = 0; j < len; i++, j += 4) {
321 output[j] = (unsigned char)(input[i] & 0xff);
322 output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
323 output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
324 output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
328 /* Decodes input (unsigned char) into output (UINT4). Assumes len is
329 a multiple of 4.
331 static void Decode (UINT4 *output,
332 const unsigned char *input,
333 unsigned int len)
335 unsigned int i, j;
337 for (i = 0, j = 0; j < len; i++, j += 4)
338 output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
339 (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
342 #endif /* USE_SSLEAY */
344 #include "curl_md5.h"
346 void Curl_md5it(unsigned char *outbuffer, /* 16 bytes */
347 const unsigned char *input)
349 MD5_CTX ctx;
350 MD5_Init(&ctx);
351 MD5_Update(&ctx, input, (unsigned int)strlen((char *)input));
352 MD5_Final(outbuffer, &ctx);
355 #endif