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