5 * This MD4 is implemented from the description in Stinson's Cryptography,
6 * theory and practice. -- presotto
10 * Rotate ammounts used in the algorithm
30 typedef struct MD4Table MD4Table;
33 uchar x; /* index into data block */
34 uchar rot; /* amount to rotate left by */
37 static MD4Table tab[] =
94 static void encode(uchar*, u32int*, ulong);
95 static void decode(u32int*, uchar*, ulong);
98 md4block(uchar *p, ulong len, MD4state *s)
101 u32int a, b, c, d, tmp;
106 for(end = p+len; p < end; p += 64){
114 for(i = 0; i < 48; i++){
118 a += (b & c) | (~b & d);
121 a += ((b & c) | (b & d) | (c & d)) + 0x5A827999;
124 a += (b ^ c ^ d) + 0x6ED9EBA1;
128 a = (a << t->rot) | (a >> (32 - t->rot));
130 /* rotate variables */
148 md4(uchar *p, ulong len, uchar *digest, MD4state *s)
156 s = malloc(sizeof(*s));
159 memset(s, 0, sizeof(*s));
164 /* seed the state, these constants would look nicer big-endian */
165 s->state[0] = 0x67452301;
166 s->state[1] = 0xefcdab89;
167 s->state[2] = 0x98badcfe;
168 s->state[3] = 0x10325476;
172 /* fill out the partial 64 byte block from previous calls */
177 memmove(s->buf + s->blen, p, i);
182 md4block(s->buf, s->blen, s);
187 /* do 64 byte blocks */
195 /* save the left overs if not last call */
198 memmove(s->buf, p, len);
205 * this is the last time through, pad what's left with 0x80,
206 * 0's, and the input count to create a multiple of 64 bytes
212 memmove(buf, p, len);
225 /* append the count */
230 /* digest the last part */
231 md4block(p, len+8, s);
233 /* return result and free state */
234 encode(digest, s->state, MD4dlen);
241 * encodes input (u32int) into output (uchar). Assumes len is
245 encode(uchar *output, u32int *input, ulong len)
250 for(e = output + len; output < e;) {
260 * decodes input (uchar) into output (u32int). Assumes len is
264 decode(u32int *output, uchar *input, ulong len)
268 for(e = input+len; input < e; input += 4)
269 *output++ = input[0] | (input[1] << 8) |
270 (input[2] << 16) | (input[3] << 24);