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