Add another configuration directive: access_attr_used_for_allow. If it is set to
[freeradius.git] / src / modules / rlm_mschap / md4c.c
1 /*
2  * md4c.c       MD4 message-digest algorithm
3  *
4  * Version:     $Id$
5  *
6  *   License to copy and use this software is granted provided that it
7  *   is identified as the "RSA Data Security, Inc. MD4 Message-Digest
8  *   Algorithm" in all material mentioning or referencing this software
9  *   or this function.
10  *
11  *   License is also granted to make and use derivative works provided
12  *   that such works are identified as "derived from the RSA Data
13  *   Security, Inc. MD4 Message-Digest Algorithm" in all material
14  *   mentioning or referencing the derived work.
15  *
16  *   RSA Data Security, Inc. makes no representations concerning either
17  *   the merchantability of this software or the suitability of this
18  *   software for any particular purpose. It is provided "as is"
19  *   without express or implied warranty of any kind.
20  *
21  *   These notices must be retained in any copies of any part of this
22  *   documentation and/or software.
23  *
24  * Copyright 1990,1991,1992  RSA Data Security, Inc.
25  */
26
27
28 /*#include "global.h"*/
29 #include "md4.h"
30
31 /* Constants for MD4Transform routine.
32  */
33 #define S11 3
34 #define S12 7
35 #define S13 11
36 #define S14 19
37 #define S21 3
38 #define S22 5
39 #define S23 9
40 #define S24 13
41 #define S31 3
42 #define S32 9
43 #define S33 11
44 #define S34 15
45
46 static void MD4Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
47 static void Encode PROTO_LIST
48   ((unsigned char *, UINT4 *, unsigned int));
49 static void Decode PROTO_LIST
50   ((UINT4 *, unsigned char *, unsigned int));
51 static void MD4_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
52 static void MD4_memset PROTO_LIST ((POINTER, int, unsigned int));
53
54 static unsigned char PADDING[64] = {
55   0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
56   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
57   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
58 };
59
60 /* F, G and H are basic MD4 functions.
61  */
62 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
63 #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
64 #define H(x, y, z) ((x) ^ (y) ^ (z))
65
66 /* ROTATE_LEFT rotates x left n bits.
67  */
68 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
69
70 /* FF, GG and HH are transformations for rounds 1, 2 and 3 */
71 /* Rotation is separate from addition to prevent recomputation */
72
73 #define FF(a, b, c, d, x, s) { \
74     (a) += F ((b), (c), (d)) + (x); \
75     (a) = ROTATE_LEFT ((a), (s)); \
76   }
77 #define GG(a, b, c, d, x, s) { \
78     (a) += G ((b), (c), (d)) + (x) + (UINT4)0x5a827999; \
79     (a) = ROTATE_LEFT ((a), (s)); \
80   }
81 #define HH(a, b, c, d, x, s) { \
82     (a) += H ((b), (c), (d)) + (x) + (UINT4)0x6ed9eba1; \
83     (a) = ROTATE_LEFT ((a), (s)); \
84   }
85
86 void md4_calc(output, input, inlen)
87 unsigned char *output;
88 unsigned char *input;                                /* input block */
89 unsigned int inlen;                     /* length of input block */
90 {
91         MD4_CTX context;
92
93         MD4Init(&context);
94         MD4Update(&context, input, inlen);
95         MD4Final(output, &context);
96 }
97
98 /* MD4 initialization. Begins an MD4 operation, writing a new context.
99  */
100 void MD4Init (context)
101 MD4_CTX *context;                                        /* context */
102 {
103   context->count[0] = context->count[1] = 0;
104
105   /* Load magic initialization constants.
106    */
107   context->state[0] = 0x67452301;
108   context->state[1] = 0xefcdab89;
109   context->state[2] = 0x98badcfe;
110   context->state[3] = 0x10325476;
111 }
112
113 /* MD4 block update operation. Continues an MD4 message-digest
114      operation, processing another message block, and updating the
115      context.
116  */
117 void MD4Update (context, input, inputLen)
118 MD4_CTX *context;                                        /* context */
119 unsigned char *input;                                /* input block */
120 unsigned int inputLen;                     /* length of input block */
121 {
122   unsigned int i, index, partLen;
123
124   /* Compute number of bytes mod 64 */
125   index = (unsigned int)((context->count[0] >> 3) & 0x3F);
126   /* Update number of bits */
127   if ((context->count[0] += ((UINT4)inputLen << 3))
128       < ((UINT4)inputLen << 3))
129     context->count[1]++;
130   context->count[1] += ((UINT4)inputLen >> 29);
131
132   partLen = 64 - index;
133
134   /* Transform as many times as possible.
135    */
136   if (inputLen >= partLen) {
137     MD4_memcpy
138       ((POINTER)&context->buffer[index], (POINTER)input, partLen);
139     MD4Transform (context->state, context->buffer);
140
141     for (i = partLen; i + 63 < inputLen; i += 64)
142       MD4Transform (context->state, &input[i]);
143
144     index = 0;
145   }
146   else
147     i = 0;
148
149   /* Buffer remaining input */
150   MD4_memcpy
151     ((POINTER)&context->buffer[index], (POINTER)&input[i],
152      inputLen-i);
153 }
154
155 /* MD4 finalization. Ends an MD4 message-digest operation, writing the
156      the message digest and zeroizing the context.
157  */
158 void MD4Final (digest, context)
159 unsigned char digest[16];                         /* message digest */
160 MD4_CTX *context;                                        /* context */
161 {
162   unsigned char bits[8];
163   unsigned int index, padLen;
164
165   /* Save number of bits */
166   Encode (bits, context->count, 8);
167
168   /* Pad out to 56 mod 64.
169    */
170   index = (unsigned int)((context->count[0] >> 3) & 0x3f);
171   padLen = (index < 56) ? (56 - index) : (120 - index);
172   MD4Update (context, PADDING, padLen);
173
174   /* Append length (before padding) */
175   MD4Update (context, bits, 8);
176   /* Store state in digest */
177   Encode (digest, context->state, 16);
178
179   /* Zeroize sensitive information.
180    */
181   MD4_memset ((POINTER)context, 0, sizeof (*context));
182 }
183
184 /* MD4 basic transformation. Transforms state based on block.
185  */
186 static void MD4Transform (state, block)
187 UINT4 state[4];
188 unsigned char block[64];
189 {
190   UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
191
192   Decode (x, block, 64);
193
194   /* Round 1 */
195   FF (a, b, c, d, x[ 0], S11); /* 1 */
196   FF (d, a, b, c, x[ 1], S12); /* 2 */
197   FF (c, d, a, b, x[ 2], S13); /* 3 */
198   FF (b, c, d, a, x[ 3], S14); /* 4 */
199   FF (a, b, c, d, x[ 4], S11); /* 5 */
200   FF (d, a, b, c, x[ 5], S12); /* 6 */
201   FF (c, d, a, b, x[ 6], S13); /* 7 */
202   FF (b, c, d, a, x[ 7], S14); /* 8 */
203   FF (a, b, c, d, x[ 8], S11); /* 9 */
204   FF (d, a, b, c, x[ 9], S12); /* 10 */
205   FF (c, d, a, b, x[10], S13); /* 11 */
206   FF (b, c, d, a, x[11], S14); /* 12 */
207   FF (a, b, c, d, x[12], S11); /* 13 */
208   FF (d, a, b, c, x[13], S12); /* 14 */
209   FF (c, d, a, b, x[14], S13); /* 15 */
210   FF (b, c, d, a, x[15], S14); /* 16 */
211
212   /* Round 2 */
213   GG (a, b, c, d, x[ 0], S21); /* 17 */
214   GG (d, a, b, c, x[ 4], S22); /* 18 */
215   GG (c, d, a, b, x[ 8], S23); /* 19 */
216   GG (b, c, d, a, x[12], S24); /* 20 */
217   GG (a, b, c, d, x[ 1], S21); /* 21 */
218   GG (d, a, b, c, x[ 5], S22); /* 22 */
219   GG (c, d, a, b, x[ 9], S23); /* 23 */
220   GG (b, c, d, a, x[13], S24); /* 24 */
221   GG (a, b, c, d, x[ 2], S21); /* 25 */
222   GG (d, a, b, c, x[ 6], S22); /* 26 */
223   GG (c, d, a, b, x[10], S23); /* 27 */
224   GG (b, c, d, a, x[14], S24); /* 28 */
225   GG (a, b, c, d, x[ 3], S21); /* 29 */
226   GG (d, a, b, c, x[ 7], S22); /* 30 */
227   GG (c, d, a, b, x[11], S23); /* 31 */
228   GG (b, c, d, a, x[15], S24); /* 32 */
229
230   /* Round 3 */
231   HH (a, b, c, d, x[ 0], S31); /* 33 */
232   HH (d, a, b, c, x[ 8], S32); /* 34 */
233   HH (c, d, a, b, x[ 4], S33); /* 35 */
234   HH (b, c, d, a, x[12], S34); /* 36 */
235   HH (a, b, c, d, x[ 2], S31); /* 37 */
236   HH (d, a, b, c, x[10], S32); /* 38 */
237   HH (c, d, a, b, x[ 6], S33); /* 39 */
238   HH (b, c, d, a, x[14], S34); /* 40 */
239   HH (a, b, c, d, x[ 1], S31); /* 41 */
240   HH (d, a, b, c, x[ 9], S32); /* 42 */
241   HH (c, d, a, b, x[ 5], S33); /* 43 */
242   HH (b, c, d, a, x[13], S34); /* 44 */
243   HH (a, b, c, d, x[ 3], S31); /* 45 */
244   HH (d, a, b, c, x[11], S32); /* 46 */
245   HH (c, d, a, b, x[ 7], S33); /* 47 */
246   HH (b, c, d, a, x[15], S34); /* 48 */
247
248   state[0] += a;
249   state[1] += b;
250   state[2] += c;
251   state[3] += d;
252
253   /* Zeroize sensitive information.
254    */
255   MD4_memset ((POINTER)x, 0, sizeof (x));
256 }
257
258 /* Encodes input (UINT4) into output (unsigned char). Assumes len is
259      a multiple of 4.
260  */
261 static void Encode (output, input, len)
262 unsigned char *output;
263 UINT4 *input;
264 unsigned int len;
265 {
266   unsigned int i, j;
267
268   for (i = 0, j = 0; j < len; i++, j += 4) {
269     output[j] = (unsigned char)(input[i] & 0xff);
270     output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
271     output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
272     output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
273   }
274 }
275
276 /* Decodes input (unsigned char) into output (UINT4). Assumes len is
277      a multiple of 4.
278  */
279 static void Decode (output, input, len)
280
281 UINT4 *output;
282 unsigned char *input;
283 unsigned int len;
284 {
285   unsigned int i, j;
286
287   for (i = 0, j = 0; j < len; i++, j += 4)
288     output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
289       (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
290 }
291
292 /* Note: Replace "for loop" with standard memcpy if possible.
293  */
294 static void MD4_memcpy (output, input, len)
295 POINTER output;
296 POINTER input;
297 unsigned int len;
298 {
299   unsigned int i;
300
301   for (i = 0; i < len; i++)
302     output[i] = input[i];
303 }
304
305 /* Note: Replace "for loop" with standard memset if possible.
306  */
307 static void MD4_memset (output, value, len)
308 POINTER output;
309 int value;
310 unsigned int len;
311 {
312   unsigned int i;
313
314   for (i = 0; i < len; i++)
315     ((char *)output)[i] = (char)value;
316 }