Expansion item ${listquote }. Bug 1066
[exim.git] / src / src / md5.c
CommitLineData
0756eb3c
PH
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
f9ba5e22 5/* Copyright (c) University of Cambridge 1995 - 2018 */
0756eb3c
PH
6/* See the file NOTICE for conditions of use and distribution. */
7
8#ifndef STAND_ALONE
d6e81e5f 9#include "exim.h"
0756eb3c
PH
10
11/* For stand-alone testing, we need to have the structure defined, and
12to be able to do I/O */
13
14#else
15#include <stdio.h>
16#include "../mytypes.h"
17typedef struct md5 {
18 unsigned int length;
19 unsigned int abcd[4];
20 }
21md5;
22#endif
23
24
25
26/*************************************************
27* Start off a new MD5 computation. *
28*************************************************/
29
30/*
31Argument: pointer to md5 storage structure
32Returns: nothing
33*/
34
35void
36md5_start(md5 *base)
37{
38base->abcd[0] = 0x67452301;
39base->abcd[1] = 0xefcdab89;
40base->abcd[2] = 0x98badcfe;
41base->abcd[3] = 0x10325476;
42base->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
52in RFC 1321.
53
54Arguments:
55 base pointer to md5 storage structure
56 text pointer to next 64 bytes of subject text
57
58Returns: nothing
59*/
60
61void
62md5_mid(md5 *base, const uschar *text)
63{
64register unsigned int a = base->abcd[0];
65register unsigned int b = base->abcd[1];
66register unsigned int c = base->abcd[2];
67register unsigned int d = base->abcd[3];
0756eb3c
PH
68unsigned int X[16];
69base->length += 64;
70
71/* Load the 64 bytes into a set of working integers, treating them as 32-bit
72numbers in little-endian order. */
73
d7978c0f 74for (int i = 0; i < 16; i++)
0756eb3c
PH
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
84as 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
96OP(a, b, c, d, 0, 7, 0xd76aa478);
97OP(d, a, b, c, 1, 12, 0xe8c7b756);
98OP(c, d, a, b, 2, 17, 0x242070db);
99OP(b, c, d, a, 3, 22, 0xc1bdceee);
100OP(a, b, c, d, 4, 7, 0xf57c0faf);
101OP(d, a, b, c, 5, 12, 0x4787c62a);
102OP(c, d, a, b, 6, 17, 0xa8304613);
103OP(b, c, d, a, 7, 22, 0xfd469501);
104OP(a, b, c, d, 8, 7, 0x698098d8);
105OP(d, a, b, c, 9, 12, 0x8b44f7af);
106OP(c, d, a, b, 10, 17, 0xffff5bb1);
107OP(b, c, d, a, 11, 22, 0x895cd7be);
108OP(a, b, c, d, 12, 7, 0x6b901122);
109OP(d, a, b, c, 13, 12, 0xfd987193);
110OP(c, d, a, b, 14, 17, 0xa679438e);
111OP(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
125OP(a, b, c, d, 1, 5, 0xf61e2562);
126OP(d, a, b, c, 6, 9, 0xc040b340);
127OP(c, d, a, b, 11, 14, 0x265e5a51);
128OP(b, c, d, a, 0, 20, 0xe9b6c7aa);
129OP(a, b, c, d, 5, 5, 0xd62f105d);
130OP(d, a, b, c, 10, 9, 0x02441453);
131OP(c, d, a, b, 15, 14, 0xd8a1e681);
132OP(b, c, d, a, 4, 20, 0xe7d3fbc8);
133OP(a, b, c, d, 9, 5, 0x21e1cde6);
134OP(d, a, b, c, 14, 9, 0xc33707d6);
135OP(c, d, a, b, 3, 14, 0xf4d50d87);
136OP(b, c, d, a, 8, 20, 0x455a14ed);
137OP(a, b, c, d, 13, 5, 0xa9e3e905);
138OP(d, a, b, c, 2, 9, 0xfcefa3f8);
139OP(c, d, a, b, 7, 14, 0x676f02d9);
140OP(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
154OP(a, b, c, d, 5, 4, 0xfffa3942);
155OP(d, a, b, c, 8, 11, 0x8771f681);
156OP(c, d, a, b, 11, 16, 0x6d9d6122);
157OP(b, c, d, a, 14, 23, 0xfde5380c);
158OP(a, b, c, d, 1, 4, 0xa4beea44);
159OP(d, a, b, c, 4, 11, 0x4bdecfa9);
160OP(c, d, a, b, 7, 16, 0xf6bb4b60);
161OP(b, c, d, a, 10, 23, 0xbebfbc70);
162OP(a, b, c, d, 13, 4, 0x289b7ec6);
163OP(d, a, b, c, 0, 11, 0xeaa127fa);
164OP(c, d, a, b, 3, 16, 0xd4ef3085);
165OP(b, c, d, a, 6, 23, 0x04881d05);
166OP(a, b, c, d, 9, 4, 0xd9d4d039);
167OP(d, a, b, c, 12, 11, 0xe6db99e5);
168OP(c, d, a, b, 15, 16, 0x1fa27cf8);
169OP(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
183OP(a, b, c, d, 0, 6, 0xf4292244);
184OP(d, a, b, c, 7, 10, 0x432aff97);
185OP(c, d, a, b, 14, 15, 0xab9423a7);
186OP(b, c, d, a, 5, 21, 0xfc93a039);
187OP(a, b, c, d, 12, 6, 0x655b59c3);
188OP(d, a, b, c, 3, 10, 0x8f0ccc92);
189OP(c, d, a, b, 10, 15, 0xffeff47d);
190OP(b, c, d, a, 1, 21, 0x85845dd1);
191OP(a, b, c, d, 8, 6, 0x6fa87e4f);
192OP(d, a, b, c, 15, 10, 0xfe2ce6e0);
193OP(c, d, a, b, 6, 15, 0xa3014314);
194OP(b, c, d, a, 13, 21, 0x4e0811a1);
195OP(a, b, c, d, 4, 6, 0xf7537e82);
196OP(d, a, b, c, 11, 10, 0xbd3af235);
197OP(c, d, a, b, 2, 15, 0x2ad7d2bb);
198OP(b, c, d, a, 9, 21, 0xeb86d391);
199
200#undef OP
201
202/* Add the new values back into the accumulators. */
203
204base->abcd[0] += a;
205base->abcd[1] += b;
206base->abcd[2] += c;
207base->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
218for computing MD5 digests. The final result is then converted to text form
219and returned.
220
221Arguments:
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
227Returns: nothing
228*/
229
230void
231md5_end(md5 *base, const uschar *text, int length, uschar *digest)
232{
0756eb3c
PH
233uschar work[64];
234
235/* Process in chunks of 64 until we have less than 64 bytes left. */
236
237while (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
245out to 64, process it, and then set up the final chunk as 56 bytes of
246padding. If it has less than 56 bytes, we pad it out to 56 bytes as the
247final chunk. */
248
249memcpy(work, text, length);
250work[length] = 0x80;
251
252if (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 }
259else
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
265length of the input string *bits*, before padding, low order word first, and
266low order bytes first in each word. This implementation is designed for short
267strings, and so operates with a single int counter only. */
268
269length += base->length; /* Total length in bytes */
270length <<= 3; /* Total length in bits */
271
272work[56] = length & 0xff;
273work[57] = (length >> 8) & 0xff;
274work[58] = (length >> 16) & 0xff;
275work[59] = (length >> 24) & 0xff;
276
277memset(work+60, 0, 4);
278
279/* Process the final 64-byte chunk */
280
281md5_mid(base, work);
282
283/* Pass back the result, low-order byte first in each word. */
284
d7978c0f 285for (int i = 0; i < 4; i++)
0756eb3c
PH
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
307static 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
331int main(void)
332{
333md5 base;
334int i = 0x01020304;
5903c6ff 335uschar *ctest = US (&i);
0756eb3c
PH
336uschar buffer[256];
337uschar digest[16];
338printf("Checking md5: %s-endian\n", (ctest[0] == 0x04)? "little" : "big");
339
340for (i = 0; i < sizeof(tests)/sizeof(uschar *); i += 2)
341 {
0756eb3c
PH
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);
d7978c0f 346 for (int j = 0; j < 16; j++) sprintf(s+2*j, "%02x", digest[j]);
0756eb3c
PH
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 */