Massively cleaned up #include's, so they're in a consistent
[freeradius.git] / src / lib / md5.c
1 /*
2  *  md5.c       MD5 message-digest algorithm
3  *
4  *  Version:    $Id$
5  *
6  *  This file is licensed under the LGPL, but is largely derived
7  *  from public domain source code.
8  */
9
10 #include <freeradius-devel/ident.h>
11 RCSID("$Id$")
12
13 #include <freeradius-devel/libradius.h>
14
15 /*
16  *  FORCE MD5 TO USE OUR MD5 HEADER FILE!
17  *  If we don't do this, it might pick up the systems broken MD5.
18  */
19 #include "../include/md5.h"
20
21 void librad_md5_calc(uint8_t *output, const uint8_t *input,
22                      unsigned int inlen)
23 {
24         lrad_MD5_CTX    context;
25
26         lrad_MD5Init(&context);
27         lrad_MD5Update(&context, input, inlen);
28         lrad_MD5Final(output, &context);
29 }
30
31 /*      The below was retrieved from
32  *      http://www.openbsd.org/cgi-bin/cvsweb/~checkout~/src/sys/crypto/md5.c?rev=1.1
33  *      with the following changes:
34  *      #includes commented out.
35  *      Support context->count as uint32_t[2] instead of uint64_t
36  *      u_int* to uint*
37  */
38
39 /*
40  * This code implements the MD5 message-digest algorithm.
41  * The algorithm is due to Ron Rivest.  This code was
42  * written by Colin Plumb in 1993, no copyright is claimed.
43  * This code is in the public domain; do with it what you wish.
44  *
45  * Equivalent code is available from RSA Data Security, Inc.
46  * This code has been tested against that, and is equivalent,
47  * except that you don't need to include two pages of legalese
48  * with every copy.
49  *
50  * To compute the message digest of a chunk of bytes, declare an
51  * MD5Context structure, pass it to MD5Init, call MD5Update as
52  * needed on buffers full of bytes, and then call MD5Final, which
53  * will fill a supplied 16-byte array with the digest.
54  */
55
56 /*#include <sys/param.h>*/
57 /*#include <sys/systm.h>*/
58 /*#include <crypto/md5.h>*/
59
60 #define PUT_64BIT_LE(cp, value) do {                            \
61         (cp)[7] = (value)[1] >> 24;                                     \
62         (cp)[6] = (value)[1] >> 16;                                     \
63         (cp)[5] = (value)[1] >> 8;                                      \
64         (cp)[4] = (value)[1];                                           \
65         (cp)[3] = (value)[0] >> 24;                                     \
66         (cp)[2] = (value)[0] >> 16;                                     \
67         (cp)[1] = (value)[0] >> 8;                                      \
68         (cp)[0] = (value)[0]; } while (0)
69
70 #define PUT_32BIT_LE(cp, value) do {                                    \
71         (cp)[3] = (value) >> 24;                                        \
72         (cp)[2] = (value) >> 16;                                        \
73         (cp)[1] = (value) >> 8;                                         \
74         (cp)[0] = (value); } while (0)
75
76 static const uint8_t PADDING[MD5_BLOCK_LENGTH] = {
77         0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
78         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
79         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
80 };
81
82 /*
83  * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
84  * initialization constants.
85  */
86 void
87 lrad_MD5Init(lrad_MD5_CTX *ctx)
88 {
89         ctx->count[0] = 0;
90         ctx->count[1] = 0;
91         ctx->state[0] = 0x67452301;
92         ctx->state[1] = 0xefcdab89;
93         ctx->state[2] = 0x98badcfe;
94         ctx->state[3] = 0x10325476;
95 }
96
97 /*
98  * Update context to reflect the concatenation of another buffer full
99  * of bytes.
100  */
101 void
102 lrad_MD5Update(lrad_MD5_CTX *ctx, const unsigned char *input, size_t len)
103 {
104         size_t have, need;
105
106         /* Check how many bytes we already have and how many more we need. */
107         have = (size_t)((ctx->count[0] >> 3) & (MD5_BLOCK_LENGTH - 1));
108         need = MD5_BLOCK_LENGTH - have;
109
110         /* Update bitcount */
111 /*      ctx->count += (uint64_t)len << 3;*/
112         if ((ctx->count[0] += ((uint32_t)len << 3)) < (uint32_t)len) {
113         /* Overflowed ctx->count[0] */
114                 ctx->count[1]++;
115         }
116         ctx->count[1] += ((uint32_t)len >> 29);
117
118         if (len >= need) {
119                 if (have != 0) {
120                         memcpy(ctx->buffer + have, input, need);
121                         lrad_MD5Transform(ctx->state, ctx->buffer);
122                         input += need;
123                         len -= need;
124                         have = 0;
125                 }
126
127                 /* Process data in MD5_BLOCK_LENGTH-byte chunks. */
128                 while (len >= MD5_BLOCK_LENGTH) {
129                         lrad_MD5Transform(ctx->state, input);
130                         input += MD5_BLOCK_LENGTH;
131                         len -= MD5_BLOCK_LENGTH;
132                 }
133         }
134
135         /* Handle any remaining bytes of data. */
136         if (len != 0)
137                 memcpy(ctx->buffer + have, input, len);
138 }
139
140 /*
141  * Final wrapup - pad to 64-byte boundary with the bit pattern
142  * 1 0* (64-bit count of bits processed, MSB-first)
143  */
144 void
145 lrad_MD5Final(uint8_t digest[MD5_DIGEST_LENGTH], MD5_CTX *ctx)
146 {
147         uint8_t count[8];
148         size_t padlen;
149         int i;
150
151         /* Convert count to 8 bytes in little endian order. */
152         PUT_64BIT_LE(count, ctx->count);
153
154         /* Pad out to 56 mod 64. */
155         padlen = MD5_BLOCK_LENGTH -
156             ((ctx->count[0] >> 3) & (MD5_BLOCK_LENGTH - 1));
157         if (padlen < 1 + 8)
158                 padlen += MD5_BLOCK_LENGTH;
159         lrad_MD5Update(ctx, PADDING, padlen - 8); /* padlen - 8 <= 64 */
160         lrad_MD5Update(ctx, count, 8);
161
162         if (digest != NULL) {
163                 for (i = 0; i < 4; i++)
164                         PUT_32BIT_LE(digest + i * 4, ctx->state[i]);
165         }
166         memset(ctx, 0, sizeof(*ctx));   /* in case it's sensitive */
167 }
168
169
170 /* The four core functions - F1 is optimized somewhat */
171
172 /* #define F1(x, y, z) (x & y | ~x & z) */
173 #define F1(x, y, z) (z ^ (x & (y ^ z)))
174 #define F2(x, y, z) F1(z, x, y)
175 #define F3(x, y, z) (x ^ y ^ z)
176 #define F4(x, y, z) (y ^ (x | ~z))
177
178 /* This is the central step in the MD5 algorithm. */
179 #define MD5STEP(f, w, x, y, z, data, s) \
180         ( w += f(x, y, z) + data,  w = w<<s | w>>(32-s),  w += x )
181
182 /*
183  * The core of the MD5 algorithm, this alters an existing MD5 hash to
184  * reflect the addition of 16 longwords of new data.  MD5Update blocks
185  * the data and converts bytes into longwords for this routine.
186  */
187 void
188 lrad_MD5Transform(uint32_t state[4], const uint8_t block[MD5_BLOCK_LENGTH])
189 {
190         uint32_t a, b, c, d, in[MD5_BLOCK_LENGTH / 4];
191
192         for (a = 0; a < MD5_BLOCK_LENGTH / 4; a++) {
193                 in[a] = (uint32_t)(
194                     (uint32_t)(block[a * 4 + 0]) |
195                     (uint32_t)(block[a * 4 + 1]) <<  8 |
196                     (uint32_t)(block[a * 4 + 2]) << 16 |
197                     (uint32_t)(block[a * 4 + 3]) << 24);
198         }
199
200         a = state[0];
201         b = state[1];
202         c = state[2];
203         d = state[3];
204
205         MD5STEP(F1, a, b, c, d, in[ 0] + 0xd76aa478,  7);
206         MD5STEP(F1, d, a, b, c, in[ 1] + 0xe8c7b756, 12);
207         MD5STEP(F1, c, d, a, b, in[ 2] + 0x242070db, 17);
208         MD5STEP(F1, b, c, d, a, in[ 3] + 0xc1bdceee, 22);
209         MD5STEP(F1, a, b, c, d, in[ 4] + 0xf57c0faf,  7);
210         MD5STEP(F1, d, a, b, c, in[ 5] + 0x4787c62a, 12);
211         MD5STEP(F1, c, d, a, b, in[ 6] + 0xa8304613, 17);
212         MD5STEP(F1, b, c, d, a, in[ 7] + 0xfd469501, 22);
213         MD5STEP(F1, a, b, c, d, in[ 8] + 0x698098d8,  7);
214         MD5STEP(F1, d, a, b, c, in[ 9] + 0x8b44f7af, 12);
215         MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
216         MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
217         MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122,  7);
218         MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
219         MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
220         MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
221
222         MD5STEP(F2, a, b, c, d, in[ 1] + 0xf61e2562,  5);
223         MD5STEP(F2, d, a, b, c, in[ 6] + 0xc040b340,  9);
224         MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
225         MD5STEP(F2, b, c, d, a, in[ 0] + 0xe9b6c7aa, 20);
226         MD5STEP(F2, a, b, c, d, in[ 5] + 0xd62f105d,  5);
227         MD5STEP(F2, d, a, b, c, in[10] + 0x02441453,  9);
228         MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
229         MD5STEP(F2, b, c, d, a, in[ 4] + 0xe7d3fbc8, 20);
230         MD5STEP(F2, a, b, c, d, in[ 9] + 0x21e1cde6,  5);
231         MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6,  9);
232         MD5STEP(F2, c, d, a, b, in[ 3] + 0xf4d50d87, 14);
233         MD5STEP(F2, b, c, d, a, in[ 8] + 0x455a14ed, 20);
234         MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905,  5);
235         MD5STEP(F2, d, a, b, c, in[ 2] + 0xfcefa3f8,  9);
236         MD5STEP(F2, c, d, a, b, in[ 7] + 0x676f02d9, 14);
237         MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
238
239         MD5STEP(F3, a, b, c, d, in[ 5] + 0xfffa3942,  4);
240         MD5STEP(F3, d, a, b, c, in[ 8] + 0x8771f681, 11);
241         MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
242         MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
243         MD5STEP(F3, a, b, c, d, in[ 1] + 0xa4beea44,  4);
244         MD5STEP(F3, d, a, b, c, in[ 4] + 0x4bdecfa9, 11);
245         MD5STEP(F3, c, d, a, b, in[ 7] + 0xf6bb4b60, 16);
246         MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
247         MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6,  4);
248         MD5STEP(F3, d, a, b, c, in[ 0] + 0xeaa127fa, 11);
249         MD5STEP(F3, c, d, a, b, in[ 3] + 0xd4ef3085, 16);
250         MD5STEP(F3, b, c, d, a, in[ 6] + 0x04881d05, 23);
251         MD5STEP(F3, a, b, c, d, in[ 9] + 0xd9d4d039,  4);
252         MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
253         MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
254         MD5STEP(F3, b, c, d, a, in[2 ] + 0xc4ac5665, 23);
255
256         MD5STEP(F4, a, b, c, d, in[ 0] + 0xf4292244,  6);
257         MD5STEP(F4, d, a, b, c, in[7 ] + 0x432aff97, 10);
258         MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
259         MD5STEP(F4, b, c, d, a, in[5 ] + 0xfc93a039, 21);
260         MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3,  6);
261         MD5STEP(F4, d, a, b, c, in[3 ] + 0x8f0ccc92, 10);
262         MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
263         MD5STEP(F4, b, c, d, a, in[1 ] + 0x85845dd1, 21);
264         MD5STEP(F4, a, b, c, d, in[8 ] + 0x6fa87e4f,  6);
265         MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
266         MD5STEP(F4, c, d, a, b, in[6 ] + 0xa3014314, 15);
267         MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
268         MD5STEP(F4, a, b, c, d, in[4 ] + 0xf7537e82,  6);
269         MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
270         MD5STEP(F4, c, d, a, b, in[2 ] + 0x2ad7d2bb, 15);
271         MD5STEP(F4, b, c, d, a, in[9 ] + 0xeb86d391, 21);
272
273         state[0] += a;
274         state[1] += b;
275         state[2] += c;
276         state[3] += d;
277 }