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