26 #include <sys/cdefs.h>
29 #include <sys/param.h>
30 #include <sys/systm.h>
37 #define PROTO_LIST(list) list
56 ((
unsigned char *,
UINT4 *,
unsigned int));
58 ((
UINT4 *,
const unsigned char *,
unsigned int));
61 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
62 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
63 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
68 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
69 #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
70 #define H(x, y, z) ((x) ^ (y) ^ (z))
74 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
78 #define FF(a, b, c, d, x, s) { \
79 (a) += F ((b), (c), (d)) + (x); \
80 (a) = ROTATE_LEFT ((a), (s)); \
82 #define GG(a, b, c, d, x, s) { \
83 (a) += G ((b), (c), (d)) + (x) + (UINT4)0x5a827999; \
84 (a) = ROTATE_LEFT ((a), (s)); \
86 #define HH(a, b, c, d, x, s) { \
87 (a) += H ((b), (c), (d)) + (x) + (UINT4)0x6ed9eba1; \
88 (a) = ROTATE_LEFT ((a), (s)); \
96 context->count[0] = context->count[1] = 0;
100 context->state[0] = 0x67452301;
101 context->state[1] = 0xefcdab89;
102 context->state[2] = 0x98badcfe;
103 context->state[3] = 0x10325476;
112 const
unsigned char *input;
113 unsigned int inputLen;
115 unsigned int i, index, partLen;
118 index = (
unsigned int)((context->count[0] >> 3) & 0x3F);
120 if ((context->count[0] += ((
UINT4)inputLen << 3))
121 < ((
UINT4)inputLen << 3))
123 context->count[1] += ((
UINT4)inputLen >> 29);
125 partLen = 64 - index;
128 if (inputLen >= partLen) {
129 bcopy(input, &context->buffer[index], partLen);
132 for (i = partLen; i + 63 < inputLen; i += 64)
141 bcopy(&input[i], &context->buffer[index], inputLen-i);
148 unsigned char bits[8];
149 unsigned int index, padLen;
152 Encode (bits, context->count, 8);
156 index = (
unsigned int)((context->count[0] >> 3) & 0x3f);
157 padLen = (index < 56) ? (56 - index) : (120 - index);
168 unsigned
char digest[16];
175 Encode (digest, context->state, 16);
179 bzero((
POINTER)context,
sizeof (*context));
186 const
unsigned char block[64];
188 UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
193 FF (a, b, c, d, x[ 0],
S11);
194 FF (d, a, b, c, x[ 1],
S12);
195 FF (c, d, a, b, x[ 2],
S13);
196 FF (b, c, d, a, x[ 3],
S14);
197 FF (a, b, c, d, x[ 4],
S11);
198 FF (d, a, b, c, x[ 5],
S12);
199 FF (c, d, a, b, x[ 6],
S13);
200 FF (b, c, d, a, x[ 7],
S14);
201 FF (a, b, c, d, x[ 8],
S11);
202 FF (d, a, b, c, x[ 9],
S12);
203 FF (c, d, a, b, x[10],
S13);
204 FF (b, c, d, a, x[11],
S14);
205 FF (a, b, c, d, x[12],
S11);
206 FF (d, a, b, c, x[13],
S12);
207 FF (c, d, a, b, x[14],
S13);
208 FF (b, c, d, a, x[15],
S14);
211 GG (a, b, c, d, x[ 0],
S21);
212 GG (d, a, b, c, x[ 4],
S22);
213 GG (c, d, a, b, x[ 8],
S23);
214 GG (b, c, d, a, x[12],
S24);
215 GG (a, b, c, d, x[ 1],
S21);
216 GG (d, a, b, c, x[ 5],
S22);
217 GG (c, d, a, b, x[ 9],
S23);
218 GG (b, c, d, a, x[13],
S24);
219 GG (a, b, c, d, x[ 2],
S21);
220 GG (d, a, b, c, x[ 6],
S22);
221 GG (c, d, a, b, x[10],
S23);
222 GG (b, c, d, a, x[14],
S24);
223 GG (a, b, c, d, x[ 3],
S21);
224 GG (d, a, b, c, x[ 7],
S22);
225 GG (c, d, a, b, x[11],
S23);
226 GG (b, c, d, a, x[15],
S24);
229 HH (a, b, c, d, x[ 0],
S31);
230 HH (d, a, b, c, x[ 8],
S32);
231 HH (c, d, a, b, x[ 4],
S33);
232 HH (b, c, d, a, x[12],
S34);
233 HH (a, b, c, d, x[ 2],
S31);
234 HH (d, a, b, c, x[10],
S32);
235 HH (c, d, a, b, x[ 6],
S33);
236 HH (b, c, d, a, x[14],
S34);
237 HH (a, b, c, d, x[ 1],
S31);
238 HH (d, a, b, c, x[ 9],
S32);
239 HH (c, d, a, b, x[ 5],
S33);
240 HH (b, c, d, a, x[13],
S34);
241 HH (a, b, c, d, x[ 3],
S31);
242 HH (d, a, b, c, x[11],
S32);
243 HH (c, d, a, b, x[ 7],
S33);
244 HH (b, c, d, a, x[15],
S34);
260 unsigned
char *output;
266 for (i = 0, j = 0; j < len; i++, j += 4) {
267 output[j] = (
unsigned char)(input[i] & 0xff);
268 output[j+1] = (
unsigned char)((input[i] >> 8) & 0xff);
269 output[j+2] = (
unsigned char)((input[i] >> 16) & 0xff);
270 output[j+3] = (
unsigned char)((input[i] >> 24) & 0xff);
280 const
unsigned char *input;
285 for (i = 0, j = 0; j < len; i++, j += 4)
286 output[i] = ((
UINT4)input[j]) | (((
UINT4)input[j+1]) << 8) |
287 (((
UINT4)input[j+2]) << 16) | (((
UINT4)input[j+3]) << 24);
static void Decode(UINT4 *output, const unsigned char *input, unsigned int len)
#define HH(a, b, c, d, x, s)
void MD4Final(digest, MD4_CTX *context)
void MD4Pad(MD4_CTX *context)
static void MD4Transform(state, block)
void MD4Init(MD4_CTX *context)
static unsigned char PADDING[64]
void MD4Update(MD4_CTX *context, const unsigned char *input, unsigned int inputLen)
static void Encode(unsigned char *output, UINT4 *input, unsigned int len)
#define FF(a, b, c, d, x, s)
#define GG(a, b, c, d, x, s)