bridge(4): document net.link.bridge.pfil_onlyip
[dragonfly.git] / sys / kern / md4c.c
blobca9cc61312936461f2179dbd947fbe749481e7c3
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 */
5 /* Copyright (C) 1990-2, RSA Data Security, Inc. All rights reserved.
7 License to copy and use this software is granted provided that it
8 is identified as the "RSA Data Security, Inc. MD4 Message-Digest
9 Algorithm" in all material mentioning or referencing this software
10 or this function.
12 License is also granted to make and use derivative works provided
13 that such works are identified as "derived from the RSA Data
14 Security, Inc. MD4 Message-Digest Algorithm" in all material
15 mentioning or referencing the derived work.
17 RSA Data Security, Inc. makes no representations concerning either
18 the merchantability of this software or the suitability of this
19 software for any particular purpose. It is provided "as is"
20 without express or implied warranty of any kind.
22 These notices must be retained in any copies of any part of this
23 documentation and/or software.
26 #include <sys/param.h>
27 #include <sys/systm.h>
28 #include <sys/md4.h>
30 typedef unsigned char *POINTER;
31 typedef u_int16_t UINT2;
32 typedef u_int32_t UINT4;
35 /* Constants for MD4Transform routine.
37 #define S11 3
38 #define S12 7
39 #define S13 11
40 #define S14 19
41 #define S21 3
42 #define S22 5
43 #define S23 9
44 #define S24 13
45 #define S31 3
46 #define S32 9
47 #define S33 11
48 #define S34 15
50 static void __kern__MD4Transform(UINT4 [4], const unsigned char [64]);
51 static void Encode(unsigned char *, UINT4 *, unsigned int);
52 static void Decode(UINT4 *, const unsigned char *, unsigned int);
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
60 /* F, G and H are basic MD4 functions.
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))
66 /* ROTATE_LEFT rotates x left n bits.
68 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
70 /* FF, GG and HH are transformations for rounds 1, 2 and 3 */
71 /* Rotation is separate from addition to prevent recomputation */
72 #define FF(a, b, c, d, x, s) { \
73 (a) += F ((b), (c), (d)) + (x); \
74 (a) = ROTATE_LEFT ((a), (s)); \
76 #define GG(a, b, c, d, x, s) { \
77 (a) += G ((b), (c), (d)) + (x) + (UINT4)0x5a827999; \
78 (a) = ROTATE_LEFT ((a), (s)); \
80 #define HH(a, b, c, d, x, s) { \
81 (a) += H ((b), (c), (d)) + (x) + (UINT4)0x6ed9eba1; \
82 (a) = ROTATE_LEFT ((a), (s)); \
85 /* MD4 initialization. Begins an MD4 operation, writing a new context.
87 void
88 MD4Init(MD4_CTX *context)
90 context->count[0] = context->count[1] = 0;
92 /* Load magic initialization constants.
94 context->state[0] = 0x67452301;
95 context->state[1] = 0xefcdab89;
96 context->state[2] = 0x98badcfe;
97 context->state[3] = 0x10325476;
100 /* MD4 block update operation. Continues an MD4 message-digest
101 * operation, processing another message block, and updating the
102 * context.
104 * Parameters:
105 * input: input block
106 * inputLen: length of input block
108 void
109 MD4Update(MD4_CTX *context, const unsigned char *input, unsigned int inputLen)
111 unsigned int i, index, partLen;
113 /* Compute number of bytes mod 64 */
114 index = (unsigned int)((context->count[0] >> 3) & 0x3F);
115 /* Update number of bits */
116 if ((context->count[0] += ((UINT4)inputLen << 3))
117 < ((UINT4)inputLen << 3))
118 context->count[1]++;
119 context->count[1] += ((UINT4)inputLen >> 29);
121 partLen = 64 - index;
122 /* Transform as many times as possible.
124 if (inputLen >= partLen) {
125 bcopy(input, &context->buffer[index], partLen);
126 __kern__MD4Transform (context->state, context->buffer);
128 for (i = partLen; i + 63 < inputLen; i += 64)
129 __kern__MD4Transform (context->state, &input[i]);
131 index = 0;
133 else
134 i = 0;
136 /* Buffer remaining input */
137 bcopy(&input[i], &context->buffer[index], inputLen-i);
140 /* MD4 padding. */
141 static void
142 __kern__MD4Pad(MD4_CTX *context)
144 unsigned char bits[8];
145 unsigned int index, padLen;
147 /* Save number of bits */
148 Encode (bits, context->count, 8);
150 /* Pad out to 56 mod 64.
152 index = (unsigned int)((context->count[0] >> 3) & 0x3f);
153 padLen = (index < 56) ? (56 - index) : (120 - index);
154 MD4Update (context, PADDING, padLen);
156 /* Append length (before padding) */
157 MD4Update (context, bits, 8);
160 /* MD4 finalization. Ends an MD4 message-digest operation, writing the
161 * the message digest and zeroizing the context.
163 * Parameters:
164 * digest: message digest
166 void
167 MD4Final(unsigned char digest[16], MD4_CTX *context)
169 /* Do padding */
170 __kern__MD4Pad (context);
172 /* Store state in digest */
173 Encode (digest, context->state, 16);
175 /* Zeroize sensitive information.
177 bzero((POINTER)context, sizeof (*context));
180 /* MD4 basic transformation. Transforms state based on block.
182 static void
183 __kern__MD4Transform(UINT4 state[4], const unsigned char block[64])
185 UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
187 Decode (x, block, 64);
189 /* Round 1 */
190 FF (a, b, c, d, x[ 0], S11); /* 1 */
191 FF (d, a, b, c, x[ 1], S12); /* 2 */
192 FF (c, d, a, b, x[ 2], S13); /* 3 */
193 FF (b, c, d, a, x[ 3], S14); /* 4 */
194 FF (a, b, c, d, x[ 4], S11); /* 5 */
195 FF (d, a, b, c, x[ 5], S12); /* 6 */
196 FF (c, d, a, b, x[ 6], S13); /* 7 */
197 FF (b, c, d, a, x[ 7], S14); /* 8 */
198 FF (a, b, c, d, x[ 8], S11); /* 9 */
199 FF (d, a, b, c, x[ 9], S12); /* 10 */
200 FF (c, d, a, b, x[10], S13); /* 11 */
201 FF (b, c, d, a, x[11], S14); /* 12 */
202 FF (a, b, c, d, x[12], S11); /* 13 */
203 FF (d, a, b, c, x[13], S12); /* 14 */
204 FF (c, d, a, b, x[14], S13); /* 15 */
205 FF (b, c, d, a, x[15], S14); /* 16 */
207 /* Round 2 */
208 GG (a, b, c, d, x[ 0], S21); /* 17 */
209 GG (d, a, b, c, x[ 4], S22); /* 18 */
210 GG (c, d, a, b, x[ 8], S23); /* 19 */
211 GG (b, c, d, a, x[12], S24); /* 20 */
212 GG (a, b, c, d, x[ 1], S21); /* 21 */
213 GG (d, a, b, c, x[ 5], S22); /* 22 */
214 GG (c, d, a, b, x[ 9], S23); /* 23 */
215 GG (b, c, d, a, x[13], S24); /* 24 */
216 GG (a, b, c, d, x[ 2], S21); /* 25 */
217 GG (d, a, b, c, x[ 6], S22); /* 26 */
218 GG (c, d, a, b, x[10], S23); /* 27 */
219 GG (b, c, d, a, x[14], S24); /* 28 */
220 GG (a, b, c, d, x[ 3], S21); /* 29 */
221 GG (d, a, b, c, x[ 7], S22); /* 30 */
222 GG (c, d, a, b, x[11], S23); /* 31 */
223 GG (b, c, d, a, x[15], S24); /* 32 */
225 /* Round 3 */
226 HH (a, b, c, d, x[ 0], S31); /* 33 */
227 HH (d, a, b, c, x[ 8], S32); /* 34 */
228 HH (c, d, a, b, x[ 4], S33); /* 35 */
229 HH (b, c, d, a, x[12], S34); /* 36 */
230 HH (a, b, c, d, x[ 2], S31); /* 37 */
231 HH (d, a, b, c, x[10], S32); /* 38 */
232 HH (c, d, a, b, x[ 6], S33); /* 39 */
233 HH (b, c, d, a, x[14], S34); /* 40 */
234 HH (a, b, c, d, x[ 1], S31); /* 41 */
235 HH (d, a, b, c, x[ 9], S32); /* 42 */
236 HH (c, d, a, b, x[ 5], S33); /* 43 */
237 HH (b, c, d, a, x[13], S34); /* 44 */
238 HH (a, b, c, d, x[ 3], S31); /* 45 */
239 HH (d, a, b, c, x[11], S32); /* 46 */
240 HH (c, d, a, b, x[ 7], S33); /* 47 */
241 HH (b, c, d, a, x[15], S34); /* 48 */
243 state[0] += a;
244 state[1] += b;
245 state[2] += c;
246 state[3] += d;
248 /* Zeroize sensitive information.
250 bzero((POINTER)x, sizeof (x));
253 /* Encodes input (UINT4) into output (unsigned char). Assumes len is
254 a multiple of 4.
256 static void
257 Encode(unsigned char *output, UINT4 *input, unsigned int len)
259 unsigned int i, j;
261 for (i = 0, j = 0; j < len; i++, j += 4) {
262 output[j] = (unsigned char)(input[i] & 0xff);
263 output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
264 output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
265 output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
269 /* Decodes input (unsigned char) into output (UINT4). Assumes len is
270 a multiple of 4.
272 static void
273 Decode(UINT4 *output, const unsigned char *input, unsigned int len)
275 unsigned int i, j;
277 for (i = 0, j = 0; j < len; i++, j += 4)
278 output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
279 (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);