Fix an integer overflow in processing client connections.
[polipo.git] / md5.c
blobf997a6ec1ce342c18db01da2ad45b5c3209ca510
1 /*
2 ***********************************************************************
3 ** md5.c -- the source code for MD5 routines **
4 ** RSA Data Security, Inc. MD5 Message-Digest Algorithm **
5 ** Created: 2/17/90 RLR **
6 ** Revised: 1/91 SRD,AJ,BSK,JT Reference C Version **
7 ** Revised (for MD5): RLR 4/27/91 **
8 ** -- G modified to have y&~z instead of y&z **
9 ** -- FF, GG, HH modified to add in last register done **
10 ** -- Access pattern: round 2 works mod 5, round 3 works mod 3 **
11 ** -- distinct additive constant for each step **
12 ** -- round 4 added, working mod 7 **
13 ***********************************************************************
17 ***********************************************************************
18 ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved. **
19 ** **
20 ** License to copy and use this software is granted provided that **
21 ** it is identified as the "RSA Data Security, Inc. MD5 Message- **
22 ** Digest Algorithm" in all material mentioning or referencing this **
23 ** software or this function. **
24 ** **
25 ** License is also granted to make and use derivative works **
26 ** provided that such works are identified as "derived from the RSA **
27 ** Data Security, Inc. MD5 Message-Digest Algorithm" in all **
28 ** material mentioning or referencing the derived work. **
29 ** **
30 ** RSA Data Security, Inc. makes no representations concerning **
31 ** either the merchantability of this software or the suitability **
32 ** of this software for any particular purpose. It is provided "as **
33 ** is" without express or implied warranty of any kind. **
34 ** **
35 ** These notices must be retained in any copies of any part of this **
36 ** documentation and/or software. **
37 ***********************************************************************
40 #include "md5.h"
43 ***********************************************************************
44 ** Message-digest routines: **
45 ** To form the message digest for a message M **
46 ** (1) Initialize a context buffer mdContext using MD5Init **
47 ** (2) Call MD5Update on mdContext and M **
48 ** (3) Call MD5Final on mdContext **
49 ** The message digest is now in mdContext->digest[0...15] **
50 ***********************************************************************
53 /* forward declaration */
54 static void Transform (UINT4 *, UINT4 *);
56 #ifdef __STDC__
57 static const
58 #else
59 static
60 #endif
61 unsigned char PADDING[64] = {
62 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
64 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
65 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
66 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
67 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
68 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
69 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
72 /* F, G, H and I are basic MD5 functions */
73 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
74 #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
75 #define H(x, y, z) ((x) ^ (y) ^ (z))
76 #define I(x, y, z) ((y) ^ ((x) | (~z)))
78 /* ROTATE_LEFT rotates x left n bits */
79 #if defined(FAST_MD5) && defined(__GNUC__) && defined(mc68000)
81 * If we're on a 68000 based CPU and using a GNU C compiler with
82 * inline assembly code, we can speed this up a bit.
84 inline UINT4 ROTATE_LEFT(UINT4 x, int n)
86 asm("roll %2,%0" : "=d" (x) : "0" (x), "Ir" (n));
87 return x;
89 #else
90 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
91 #endif
94 /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4 */
95 /* Rotation is separate from addition to prevent recomputation */
96 #define FF(a, b, c, d, x, s, ac) \
97 {(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
98 (a) = ROTATE_LEFT ((a), (s)); \
99 (a) += (b); \
101 #define GG(a, b, c, d, x, s, ac) \
102 {(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
103 (a) = ROTATE_LEFT ((a), (s)); \
104 (a) += (b); \
106 #define HH(a, b, c, d, x, s, ac) \
107 {(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
108 (a) = ROTATE_LEFT ((a), (s)); \
109 (a) += (b); \
111 #define II(a, b, c, d, x, s, ac) \
112 {(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
113 (a) = ROTATE_LEFT ((a), (s)); \
114 (a) += (b); \
117 /* The routine MD5Init initializes the message-digest context
118 mdContext. All fields are set to zero.
120 void MD5Init (mdContext)
121 MD5_CTX *mdContext;
123 mdContext->i[0] = mdContext->i[1] = (UINT4)0;
125 /* Load magic initialization constants.
127 mdContext->buf[0] = (UINT4)0x67452301;
128 mdContext->buf[1] = (UINT4)0xefcdab89;
129 mdContext->buf[2] = (UINT4)0x98badcfe;
130 mdContext->buf[3] = (UINT4)0x10325476;
133 /* The routine MD5Update updates the message-digest context to
134 account for the presence of each of the characters inBuf[0..inLen-1]
135 in the message whose digest is being computed.
137 void MD5Update (mdContext, inBuf, inLen)
138 MD5_CTX *mdContext;
139 unsigned const char *inBuf;
140 unsigned int inLen;
142 UINT4 in[16];
143 int mdi;
144 unsigned int i, ii;
146 /* compute number of bytes mod 64 */
147 mdi = (int)((mdContext->i[0] >> 3) & 0x3F);
149 /* update number of bits */
150 if ((mdContext->i[0] + ((UINT4)inLen << 3)) < mdContext->i[0])
151 mdContext->i[1]++;
152 mdContext->i[0] += ((UINT4)inLen << 3);
153 mdContext->i[1] += ((UINT4)inLen >> 29);
155 while (inLen--) {
156 /* add new character to buffer, increment mdi */
157 mdContext->in[mdi++] = *inBuf++;
159 /* transform if necessary */
160 if (mdi == 0x40) {
161 for (i = 0, ii = 0; i < 16; i++, ii += 4)
162 in[i] = (((UINT4)mdContext->in[ii+3]) << 24) |
163 (((UINT4)mdContext->in[ii+2]) << 16) |
164 (((UINT4)mdContext->in[ii+1]) << 8) |
165 ((UINT4)mdContext->in[ii]);
166 Transform (mdContext->buf, in);
167 mdi = 0;
172 /* The routine MD5Final terminates the message-digest computation and
173 ends with the desired message digest in mdContext->digest[0...15].
176 void MD5Final (mdContext)
177 MD5_CTX *mdContext;
179 UINT4 in[16];
180 int mdi;
181 unsigned int i, ii;
182 unsigned int padLen;
184 /* save number of bits */
185 in[14] = mdContext->i[0];
186 in[15] = mdContext->i[1];
188 /* compute number of bytes mod 64 */
189 mdi = (int)((mdContext->i[0] >> 3) & 0x3F);
191 /* pad out to 56 mod 64 */
192 padLen = (mdi < 56) ? (56 - mdi) : (120 - mdi);
193 MD5Update (mdContext, PADDING, padLen);
195 /* append length in bits and transform */
196 for (i = 0, ii = 0; i < 14; i++, ii += 4)
197 in[i] = (((UINT4)mdContext->in[ii+3]) << 24) |
198 (((UINT4)mdContext->in[ii+2]) << 16) |
199 (((UINT4)mdContext->in[ii+1]) << 8) |
200 ((UINT4)mdContext->in[ii]);
201 Transform (mdContext->buf, in);
203 /* store buffer in digest */
204 for (i = 0, ii = 0; i < 4; i++, ii += 4) {
205 mdContext->digest[ii] = (unsigned char)(mdContext->buf[i] & 0xFF);
206 mdContext->digest[ii+1] =
207 (unsigned char)((mdContext->buf[i] >> 8) & 0xFF);
208 mdContext->digest[ii+2] =
209 (unsigned char)((mdContext->buf[i] >> 16) & 0xFF);
210 mdContext->digest[ii+3] =
211 (unsigned char)((mdContext->buf[i] >> 24) & 0xFF);
215 /* Basic MD5 step. Transforms buf based on in.
217 static void Transform (buf, in)
218 UINT4 *buf;
219 UINT4 *in;
221 UINT4 a = buf[0], b = buf[1], c = buf[2], d = buf[3];
223 /* Round 1 */
224 #define S11 7
225 #define S12 12
226 #define S13 17
227 #define S14 22
229 FF ( a, b, c, d, in[ 0], S11, 0xd76aa478); /* 1 */
230 FF ( d, a, b, c, in[ 1], S12, 0xe8c7b756); /* 2 */
231 FF ( c, d, a, b, in[ 2], S13, 0x242070db); /* 3 */
232 FF ( b, c, d, a, in[ 3], S14, 0xc1bdceee); /* 4 */
233 FF ( a, b, c, d, in[ 4], S11, 0xf57c0faf); /* 5 */
234 FF ( d, a, b, c, in[ 5], S12, 0x4787c62a); /* 6 */
235 FF ( c, d, a, b, in[ 6], S13, 0xa8304613); /* 7 */
236 FF ( b, c, d, a, in[ 7], S14, 0xfd469501); /* 8 */
237 FF ( a, b, c, d, in[ 8], S11, 0x698098d8); /* 9 */
238 FF ( d, a, b, c, in[ 9], S12, 0x8b44f7af); /* 10 */
239 FF ( c, d, a, b, in[10], S13, 0xffff5bb1); /* 11 */
240 FF ( b, c, d, a, in[11], S14, 0x895cd7be); /* 12 */
241 FF ( a, b, c, d, in[12], S11, 0x6b901122); /* 13 */
242 FF ( d, a, b, c, in[13], S12, 0xfd987193); /* 14 */
243 FF ( c, d, a, b, in[14], S13, 0xa679438e); /* 15 */
244 FF ( b, c, d, a, in[15], S14, 0x49b40821); /* 16 */
246 /* Round 2 */
247 #define S21 5
248 #define S22 9
249 #define S23 14
250 #define S24 20
251 GG ( a, b, c, d, in[ 1], S21, 0xf61e2562); /* 17 */
252 GG ( d, a, b, c, in[ 6], S22, 0xc040b340); /* 18 */
253 GG ( c, d, a, b, in[11], S23, 0x265e5a51); /* 19 */
254 GG ( b, c, d, a, in[ 0], S24, 0xe9b6c7aa); /* 20 */
255 GG ( a, b, c, d, in[ 5], S21, 0xd62f105d); /* 21 */
256 GG ( d, a, b, c, in[10], S22, 0x2441453); /* 22 */
257 GG ( c, d, a, b, in[15], S23, 0xd8a1e681); /* 23 */
258 GG ( b, c, d, a, in[ 4], S24, 0xe7d3fbc8); /* 24 */
259 GG ( a, b, c, d, in[ 9], S21, 0x21e1cde6); /* 25 */
260 GG ( d, a, b, c, in[14], S22, 0xc33707d6); /* 26 */
261 GG ( c, d, a, b, in[ 3], S23, 0xf4d50d87); /* 27 */
262 GG ( b, c, d, a, in[ 8], S24, 0x455a14ed); /* 28 */
263 GG ( a, b, c, d, in[13], S21, 0xa9e3e905); /* 29 */
264 GG ( d, a, b, c, in[ 2], S22, 0xfcefa3f8); /* 30 */
265 GG ( c, d, a, b, in[ 7], S23, 0x676f02d9); /* 31 */
266 GG ( b, c, d, a, in[12], S24, 0x8d2a4c8a); /* 32 */
268 /* Round 3 */
269 #define S31 4
270 #define S32 11
271 #define S33 16
272 #define S34 23
273 HH ( a, b, c, d, in[ 5], S31, 0xfffa3942); /* 33 */
274 HH ( d, a, b, c, in[ 8], S32, 0x8771f681); /* 34 */
275 HH ( c, d, a, b, in[11], S33, 0x6d9d6122); /* 35 */
276 HH ( b, c, d, a, in[14], S34, 0xfde5380c); /* 36 */
277 HH ( a, b, c, d, in[ 1], S31, 0xa4beea44); /* 37 */
278 HH ( d, a, b, c, in[ 4], S32, 0x4bdecfa9); /* 38 */
279 HH ( c, d, a, b, in[ 7], S33, 0xf6bb4b60); /* 39 */
280 HH ( b, c, d, a, in[10], S34, 0xbebfbc70); /* 40 */
281 HH ( a, b, c, d, in[13], S31, 0x289b7ec6); /* 41 */
282 HH ( d, a, b, c, in[ 0], S32, 0xeaa127fa); /* 42 */
283 HH ( c, d, a, b, in[ 3], S33, 0xd4ef3085); /* 43 */
284 HH ( b, c, d, a, in[ 6], S34, 0x4881d05); /* 44 */
285 HH ( a, b, c, d, in[ 9], S31, 0xd9d4d039); /* 45 */
286 HH ( d, a, b, c, in[12], S32, 0xe6db99e5); /* 46 */
287 HH ( c, d, a, b, in[15], S33, 0x1fa27cf8); /* 47 */
288 HH ( b, c, d, a, in[ 2], S34, 0xc4ac5665); /* 48 */
290 /* Round 4 */
291 #define S41 6
292 #define S42 10
293 #define S43 15
294 #define S44 21
295 II ( a, b, c, d, in[ 0], S41, 0xf4292244); /* 49 */
296 II ( d, a, b, c, in[ 7], S42, 0x432aff97); /* 50 */
297 II ( c, d, a, b, in[14], S43, 0xab9423a7); /* 51 */
298 II ( b, c, d, a, in[ 5], S44, 0xfc93a039); /* 52 */
299 II ( a, b, c, d, in[12], S41, 0x655b59c3); /* 53 */
300 II ( d, a, b, c, in[ 3], S42, 0x8f0ccc92); /* 54 */
301 II ( c, d, a, b, in[10], S43, 0xffeff47d); /* 55 */
302 II ( b, c, d, a, in[ 1], S44, 0x85845dd1); /* 56 */
303 II ( a, b, c, d, in[ 8], S41, 0x6fa87e4f); /* 57 */
304 II ( d, a, b, c, in[15], S42, 0xfe2ce6e0); /* 58 */
305 II ( c, d, a, b, in[ 6], S43, 0xa3014314); /* 59 */
306 II ( b, c, d, a, in[13], S44, 0x4e0811a1); /* 60 */
307 II ( a, b, c, d, in[ 4], S41, 0xf7537e82); /* 61 */
308 II ( d, a, b, c, in[11], S42, 0xbd3af235); /* 62 */
309 II ( c, d, a, b, in[ 2], S43, 0x2ad7d2bb); /* 63 */
310 II ( b, c, d, a, in[ 9], S44, 0xeb86d391); /* 64 */
312 buf[0] += a;
313 buf[1] += b;
314 buf[2] += c;
315 buf[3] += d;
319 ***********************************************************************
320 ** End of md5.c **
321 ******************************** (cut) ********************************