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