do not use 'long' to store 4 bytes, as 64bit architectures have 64bit longs.
[platform/upstream/curl.git] / lib / md5.c
1 /***************************************************************************
2  *                                  _   _ ____  _     
3  *  Project                     ___| | | |  _ \| |    
4  *                             / __| | | | |_) | |    
5  *                            | (__| |_| |  _ <| |___ 
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2003, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
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.
13  * 
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.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  * $Id$
22  ***************************************************************************/
23
24 #include "setup.h"
25
26 #ifndef USE_SSLEAY
27 /* This code segment is only used if OpenSSL is not provided, as if it is
28    we use the MD5-function provided there instead. No good duplicating
29    code! */
30
31 /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
32 rights reserved.
33
34 License to copy and use this software is granted provided that it
35 is identified as the "RSA Data Security, Inc. MD5 Message-Digest
36 Algorithm" in all material mentioning or referencing this software
37 or this function.
38
39 License is also granted to make and use derivative works provided
40 that such works are identified as "derived from the RSA Data
41 Security, Inc. MD5 Message-Digest Algorithm" in all material
42 mentioning or referencing the derived work.
43
44 RSA Data Security, Inc. makes no representations concerning either
45 the merchantability of this software or the suitability of this
46 software for any particular purpose. It is provided "as is"
47 without express or implied warranty of any kind.
48
49 These notices must be retained in any copies of any part of this
50 documentation and/or software.
51  */
52
53 #include <string.h>
54
55 /* UINT4 defines a four byte word */
56 typedef unsigned int UINT4;
57
58 /* MD5 context. */
59 struct md5_ctx {
60   UINT4 state[4];                                   /* state (ABCD) */
61   UINT4 count[2];        /* number of bits, modulo 2^64 (lsb first) */
62   unsigned char buffer[64];                         /* input buffer */
63 };
64
65 typedef struct md5_ctx MD5_CTX;
66
67 static void MD5_Init(struct md5_ctx *);
68 static void MD5_Update(struct md5_ctx *, unsigned char *, unsigned int);
69 static void MD5_Final(unsigned char [16], struct md5_ctx *);
70
71 /* Constants for MD5Transform routine.
72  */
73
74 #define S11 7
75 #define S12 12
76 #define S13 17
77 #define S14 22
78 #define S21 5
79 #define S22 9
80 #define S23 14
81 #define S24 20
82 #define S31 4
83 #define S32 11
84 #define S33 16
85 #define S34 23
86 #define S41 6
87 #define S42 10
88 #define S43 15
89 #define S44 21
90
91 static void MD5Transform(UINT4 [4], unsigned char [64]);
92 static void Encode(unsigned char *, UINT4 *, unsigned int);
93 static void Decode(UINT4 *, unsigned char *, unsigned int);
94
95 #define MD5_memcpy(dst,src,len) memcpy(dst,src,len)
96 #define MD5_memset(dst,val,len) memset(dst,val,len)
97
98 static unsigned char PADDING[64] = {
99   0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
100   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
101   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
102 };
103
104 /* F, G, H and I are basic MD5 functions.
105  */
106 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
107 #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
108 #define H(x, y, z) ((x) ^ (y) ^ (z))
109 #define I(x, y, z) ((y) ^ ((x) | (~z)))
110
111 /* ROTATE_LEFT rotates x left n bits.
112  */
113 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
114
115 /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
116 Rotation is separate from addition to prevent recomputation.
117  */
118 #define FF(a, b, c, d, x, s, ac) { \
119  (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
120  (a) = ROTATE_LEFT ((a), (s)); \
121  (a) += (b); \
122   }
123 #define GG(a, b, c, d, x, s, ac) { \
124  (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
125  (a) = ROTATE_LEFT ((a), (s)); \
126  (a) += (b); \
127   }
128 #define HH(a, b, c, d, x, s, ac) { \
129  (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
130  (a) = ROTATE_LEFT ((a), (s)); \
131  (a) += (b); \
132   }
133 #define II(a, b, c, d, x, s, ac) { \
134  (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
135  (a) = ROTATE_LEFT ((a), (s)); \
136  (a) += (b); \
137   }
138
139 /* MD5 initialization. Begins an MD5 operation, writing a new context.
140  */
141 static void MD5_Init (context)
142 struct md5_ctx *context;                                        /* context */
143 {
144   context->count[0] = context->count[1] = 0;
145   /* Load magic initialization constants.
146 */
147   context->state[0] = 0x67452301;
148   context->state[1] = 0xefcdab89;
149   context->state[2] = 0x98badcfe;
150   context->state[3] = 0x10325476;
151 }
152
153 /* MD5 block update operation. Continues an MD5 message-digest
154   operation, processing another message block, and updating the
155   context.
156  */
157 static void MD5_Update (context, input, inputLen)
158 struct md5_ctx *context;                                        /* context */
159 unsigned char *input;                                /* input block */
160 unsigned int inputLen;                     /* length of input block */
161 {
162   unsigned int i, index, partLen;
163
164   /* Compute number of bytes mod 64 */
165   index = (unsigned int)((context->count[0] >> 3) & 0x3F);
166
167   /* Update number of bits */
168   if ((context->count[0] += ((UINT4)inputLen << 3))
169       < ((UINT4)inputLen << 3))
170     context->count[1]++;
171   context->count[1] += ((UINT4)inputLen >> 29);
172   
173   partLen = 64 - index;
174
175   /* Transform as many times as possible. */
176   if (inputLen >= partLen) {
177     MD5_memcpy((void *)&context->buffer[index], (void *)input, partLen);
178     MD5Transform(context->state, context->buffer);
179     
180     for (i = partLen; i + 63 < inputLen; i += 64)
181       MD5Transform(context->state, &input[i]);
182     
183     index = 0;
184   }
185   else
186     i = 0;
187
188   /* Buffer remaining input */
189   MD5_memcpy((void *)&context->buffer[index], (void *)&input[i],
190              inputLen-i);
191 }
192
193 /* MD5 finalization. Ends an MD5 message-digest operation, writing the
194   the message digest and zeroizing the context.
195  */
196 static void MD5_Final (digest, context)
197 unsigned char digest[16];                         /* message digest */
198 struct md5_ctx *context;                                       /* context */
199 {
200   unsigned char bits[8];
201   unsigned int index, padLen;
202
203   /* Save number of bits */
204   Encode (bits, context->count, 8);
205
206   /* Pad out to 56 mod 64. */
207   index = (unsigned int)((context->count[0] >> 3) & 0x3f);
208   padLen = (index < 56) ? (56 - index) : (120 - index);
209   MD5_Update (context, PADDING, padLen);
210
211   /* Append length (before padding) */
212   MD5_Update (context, bits, 8);
213
214   /* Store state in digest */
215   Encode (digest, context->state, 16);
216
217   /* Zeroize sensitive information. */
218   MD5_memset ((void *)context, 0, sizeof (*context));
219 }
220
221 /* MD5 basic transformation. Transforms state based on block. */
222 static void MD5Transform (state, block)
223 UINT4 state[4];
224 unsigned char block[64];
225 {
226   UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
227
228   Decode (x, block, 64);
229
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 */
247
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 */
265
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 */
283
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 */
301
302   state[0] += a;
303   state[1] += b;
304   state[2] += c;
305   state[3] += d;
306
307   /* Zeroize sensitive information. */
308   MD5_memset ((void *)x, 0, sizeof (x));
309 }
310
311 /* Encodes input (UINT4) into output (unsigned char). Assumes len is
312   a multiple of 4.
313  */
314 static void Encode (unsigned char *output,
315                     UINT4 *input,
316                     unsigned int len)
317 {
318   unsigned int i, j;
319
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);
325   }
326 }
327
328 /* Decodes input (unsigned char) into output (UINT4). Assumes len is
329    a multiple of 4.
330 */
331 static void Decode (UINT4 *output,
332                     unsigned char *input,
333                     unsigned int len)
334 {
335   unsigned int i, j;
336
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);
340 }
341
342 #else
343 /* If OpenSSL is present */
344 #include <openssl/md5.h>
345 #include <string.h>
346 #endif
347
348
349 void Curl_md5it(unsigned char *outbuffer, /* 16 bytes */
350                 unsigned char *input)
351 {
352   MD5_CTX ctx;
353   MD5_Init(&ctx);
354   MD5_Update(&ctx, input, strlen((char *)input));
355   MD5_Final(outbuffer, &ctx);
356 }