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