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