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