cc6607950d199156fd4003e37e2755a0c4ce829d
[exim.git] / src / src / md5.c
1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
4
5 /* Copyright (c) University of Cambridge 1995 - 2018 */
6 /* See the file NOTICE for conditions of use and distribution. */
7
8 #ifndef STAND_ALONE
9 #include "exim.h"
10
11 /* For stand-alone testing, we need to have the structure defined, and
12 to be able to do I/O */
13
14 #else
15 #include <stdio.h>
16 #include "../mytypes.h"
17 typedef struct md5 {
18 unsigned int length;
19 unsigned int abcd[4];
20 }
21 md5;
22 #endif
23
24
25
26 /*************************************************
27 * Start off a new MD5 computation. *
28 *************************************************/
29
30 /*
31 Argument: pointer to md5 storage structure
32 Returns: nothing
33 */
34
35 void
36 md5_start(md5 *base)
37 {
38 base->abcd[0] = 0x67452301;
39 base->abcd[1] = 0xefcdab89;
40 base->abcd[2] = 0x98badcfe;
41 base->abcd[3] = 0x10325476;
42 base->length = 0;
43 }
44
45
46
47 /*************************************************
48 * Process another 64-byte block *
49 *************************************************/
50
51 /* This function implements central part of the algorithm which is described
52 in RFC 1321.
53
54 Arguments:
55 base pointer to md5 storage structure
56 text pointer to next 64 bytes of subject text
57
58 Returns: nothing
59 */
60
61 void
62 md5_mid(md5 *base, const uschar *text)
63 {
64 register unsigned int a = base->abcd[0];
65 register unsigned int b = base->abcd[1];
66 register unsigned int c = base->abcd[2];
67 register unsigned int d = base->abcd[3];
68 unsigned int X[16];
69 base->length += 64;
70
71 /* Load the 64 bytes into a set of working integers, treating them as 32-bit
72 numbers in little-endian order. */
73
74 for (int i = 0; i < 16; i++)
75 {
76 X[i] = (unsigned int)(text[0]) |
77 ((unsigned int)(text[1]) << 8) |
78 ((unsigned int)(text[2]) << 16) |
79 ((unsigned int)(text[3]) << 24);
80 text += 4;
81 }
82
83 /* For each round of processing there is a function to be applied. We define it
84 as a macro each time round. */
85
86 /***********************************************
87 * Round 1 *
88 * F(X,Y,Z) = XY v not(X) Z *
89 * a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s) *
90 ***********************************************/
91
92 #define OP(a, b, c, d, k, s, ti) \
93 a += ((b & c) | (~b & d)) + X[k] + (unsigned int)ti; \
94 a = b + ((a << s) | (a >> (32 - s)))
95
96 OP(a, b, c, d, 0, 7, 0xd76aa478);
97 OP(d, a, b, c, 1, 12, 0xe8c7b756);
98 OP(c, d, a, b, 2, 17, 0x242070db);
99 OP(b, c, d, a, 3, 22, 0xc1bdceee);
100 OP(a, b, c, d, 4, 7, 0xf57c0faf);
101 OP(d, a, b, c, 5, 12, 0x4787c62a);
102 OP(c, d, a, b, 6, 17, 0xa8304613);
103 OP(b, c, d, a, 7, 22, 0xfd469501);
104 OP(a, b, c, d, 8, 7, 0x698098d8);
105 OP(d, a, b, c, 9, 12, 0x8b44f7af);
106 OP(c, d, a, b, 10, 17, 0xffff5bb1);
107 OP(b, c, d, a, 11, 22, 0x895cd7be);
108 OP(a, b, c, d, 12, 7, 0x6b901122);
109 OP(d, a, b, c, 13, 12, 0xfd987193);
110 OP(c, d, a, b, 14, 17, 0xa679438e);
111 OP(b, c, d, a, 15, 22, 0x49b40821);
112
113 #undef OP
114
115 /***********************************************
116 * Round 2 *
117 * F(X,Y,Z) = XZ v Y not(Z) *
118 * a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s) *
119 ***********************************************/
120
121 #define OP(a, b, c, d, k, s, ti) \
122 a += ((b & d) | (c & ~d)) + X[k] + (unsigned int)ti; \
123 a = b + ((a << s) | (a >> (32 - s)))
124
125 OP(a, b, c, d, 1, 5, 0xf61e2562);
126 OP(d, a, b, c, 6, 9, 0xc040b340);
127 OP(c, d, a, b, 11, 14, 0x265e5a51);
128 OP(b, c, d, a, 0, 20, 0xe9b6c7aa);
129 OP(a, b, c, d, 5, 5, 0xd62f105d);
130 OP(d, a, b, c, 10, 9, 0x02441453);
131 OP(c, d, a, b, 15, 14, 0xd8a1e681);
132 OP(b, c, d, a, 4, 20, 0xe7d3fbc8);
133 OP(a, b, c, d, 9, 5, 0x21e1cde6);
134 OP(d, a, b, c, 14, 9, 0xc33707d6);
135 OP(c, d, a, b, 3, 14, 0xf4d50d87);
136 OP(b, c, d, a, 8, 20, 0x455a14ed);
137 OP(a, b, c, d, 13, 5, 0xa9e3e905);
138 OP(d, a, b, c, 2, 9, 0xfcefa3f8);
139 OP(c, d, a, b, 7, 14, 0x676f02d9);
140 OP(b, c, d, a, 12, 20, 0x8d2a4c8a);
141
142 #undef OP
143
144 /***********************************************
145 * Round 3 *
146 * F(X,Y,Z) = X xor Y xor Z *
147 * a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s) *
148 ***********************************************/
149
150 #define OP(a, b, c, d, k, s, ti) \
151 a += (b ^ c ^ d) + X[k] + (unsigned int)ti; \
152 a = b + ((a << s) | (a >> (32 - s)))
153
154 OP(a, b, c, d, 5, 4, 0xfffa3942);
155 OP(d, a, b, c, 8, 11, 0x8771f681);
156 OP(c, d, a, b, 11, 16, 0x6d9d6122);
157 OP(b, c, d, a, 14, 23, 0xfde5380c);
158 OP(a, b, c, d, 1, 4, 0xa4beea44);
159 OP(d, a, b, c, 4, 11, 0x4bdecfa9);
160 OP(c, d, a, b, 7, 16, 0xf6bb4b60);
161 OP(b, c, d, a, 10, 23, 0xbebfbc70);
162 OP(a, b, c, d, 13, 4, 0x289b7ec6);
163 OP(d, a, b, c, 0, 11, 0xeaa127fa);
164 OP(c, d, a, b, 3, 16, 0xd4ef3085);
165 OP(b, c, d, a, 6, 23, 0x04881d05);
166 OP(a, b, c, d, 9, 4, 0xd9d4d039);
167 OP(d, a, b, c, 12, 11, 0xe6db99e5);
168 OP(c, d, a, b, 15, 16, 0x1fa27cf8);
169 OP(b, c, d, a, 2, 23, 0xc4ac5665);
170
171 #undef OP
172
173 /***********************************************
174 * Round 4 *
175 * F(X,Y,Z) = Y xor (X v not(Z)) *
176 * a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s) *
177 ***********************************************/
178
179 #define OP(a, b, c, d, k, s, ti) \
180 a += (c ^ (b | ~d)) + X[k] + (unsigned int)ti; \
181 a = b + ((a << s) | (a >> (32 - s)))
182
183 OP(a, b, c, d, 0, 6, 0xf4292244);
184 OP(d, a, b, c, 7, 10, 0x432aff97);
185 OP(c, d, a, b, 14, 15, 0xab9423a7);
186 OP(b, c, d, a, 5, 21, 0xfc93a039);
187 OP(a, b, c, d, 12, 6, 0x655b59c3);
188 OP(d, a, b, c, 3, 10, 0x8f0ccc92);
189 OP(c, d, a, b, 10, 15, 0xffeff47d);
190 OP(b, c, d, a, 1, 21, 0x85845dd1);
191 OP(a, b, c, d, 8, 6, 0x6fa87e4f);
192 OP(d, a, b, c, 15, 10, 0xfe2ce6e0);
193 OP(c, d, a, b, 6, 15, 0xa3014314);
194 OP(b, c, d, a, 13, 21, 0x4e0811a1);
195 OP(a, b, c, d, 4, 6, 0xf7537e82);
196 OP(d, a, b, c, 11, 10, 0xbd3af235);
197 OP(c, d, a, b, 2, 15, 0x2ad7d2bb);
198 OP(b, c, d, a, 9, 21, 0xeb86d391);
199
200 #undef OP
201
202 /* Add the new values back into the accumulators. */
203
204 base->abcd[0] += a;
205 base->abcd[1] += b;
206 base->abcd[2] += c;
207 base->abcd[3] += d;
208 }
209
210
211
212
213 /*************************************************
214 * Process the final text string *
215 *************************************************/
216
217 /* The string may be of any length. It is padded out according to the rules
218 for computing MD5 digests. The final result is then converted to text form
219 and returned.
220
221 Arguments:
222 base pointer to the md5 storage structure
223 text pointer to the final text vector
224 length length of the final text vector
225 digest points to 16 bytes in which to place the result
226
227 Returns: nothing
228 */
229
230 void
231 md5_end(md5 *base, const uschar *text, int length, uschar *digest)
232 {
233 uschar work[64];
234
235 /* Process in chunks of 64 until we have less than 64 bytes left. */
236
237 while (length >= 64)
238 {
239 md5_mid(base, text);
240 text += 64;
241 length -= 64;
242 }
243
244 /* If the remaining string contains more than 55 bytes, we must pad it
245 out to 64, process it, and then set up the final chunk as 56 bytes of
246 padding. If it has less than 56 bytes, we pad it out to 56 bytes as the
247 final chunk. */
248
249 memcpy(work, text, length);
250 work[length] = 0x80;
251
252 if (length > 55)
253 {
254 memset(work+length+1, 0, 63-length);
255 md5_mid(base, work);
256 base->length -= 64;
257 memset(work, 0, 56);
258 }
259 else
260 {
261 memset(work+length+1, 0, 55-length);
262 }
263
264 /* The final 8 bytes of the final chunk are a 64-bit representation of the
265 length of the input string *bits*, before padding, low order word first, and
266 low order bytes first in each word. This implementation is designed for short
267 strings, and so operates with a single int counter only. */
268
269 length += base->length; /* Total length in bytes */
270 length <<= 3; /* Total length in bits */
271
272 work[56] = length & 0xff;
273 work[57] = (length >> 8) & 0xff;
274 work[58] = (length >> 16) & 0xff;
275 work[59] = (length >> 24) & 0xff;
276
277 memset(work+60, 0, 4);
278
279 /* Process the final 64-byte chunk */
280
281 md5_mid(base, work);
282
283 /* Pass back the result, low-order byte first in each word. */
284
285 for (int i = 0; i < 4; i++)
286 {
287 register int x = base->abcd[i];
288 *digest++ = x & 0xff;
289 *digest++ = (x >> 8) & 0xff;
290 *digest++ = (x >> 16) & 0xff;
291 *digest++ = (x >> 24) & 0xff;
292 }
293 }
294
295
296
297 /*************************************************
298 **************************************************
299 * Stand-alone test program *
300 **************************************************
301 *************************************************/
302
303 #if defined STAND_ALONE & !defined CRAM_STAND_ALONE
304
305 /* Test values */
306
307 static uschar *tests[] = {
308 "", "d41d8cd98f00b204e9800998ecf8427e",
309
310 "a", "0cc175b9c0f1b6a831c399e269772661",
311
312 "abc", "900150983cd24fb0d6963f7d28e17f72",
313
314 "message digest", "f96b697d7cb7938d525a2f31aaf161d0",
315
316 "abcdefghijklmnopqrstuvwxyz", "c3fcd3d76192e4007dfb496cca67e13b",
317
318 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
319 "d174ab98d277d9f5a5611c2c9f419d9f",
320
321 "1234567890123456789012345678901234567890123456789012345678901234567890"
322 "1234567890",
323 "57edf4a22be3c955ac49da2e2107b67a",
324
325 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
326 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
327 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
328 "a0842fcc02167127b0bb9a7c38e71ba8"
329 };
330
331 int main(void)
332 {
333 md5 base;
334 int i = 0x01020304;
335 uschar *ctest = US (&i);
336 uschar buffer[256];
337 uschar digest[16];
338 printf("Checking md5: %s-endian\n", (ctest[0] == 0x04)? "little" : "big");
339
340 for (i = 0; i < sizeof(tests)/sizeof(uschar *); i += 2)
341 {
342 uschar s[33];
343 printf("%s\nShould be: %s\n", tests[i], tests[i+1]);
344 md5_start(&base);
345 md5_end(&base, tests[i], strlen(tests[i]), digest);
346 for (int j = 0; j < 16; j++) sprintf(s+2*j, "%02x", digest[j]);
347 printf("Computed: %s\n", s);
348 if (strcmp(s, tests[i+1]) != 0) printf("*** No match ***\n");
349 printf("\n");
350 }
351 }
352 #endif
353
354 /* End of md5.c */