1 /* @(#)md4.c	1.3 09/07/11 2009 J. Schilling */
2 #include <schily/mconfig.h>
3 #ifndef lint
4 static	UConst char sccsid[] =
5 	"@(#)md4.c	1.3 09/07/11 2009 J. Schilling";
6 #endif
7 /*
8  * MD5 hash code taken from OpenBSD
9  *
10  * Portions Copyright (c) 2009 J. Schilling
11  */
12 /*	$OpenBSD: md4.c,v 1.7 2005/08/08 08:05:35 espie Exp $	*/
13 
14 /*
15  * This code implements the MD4 message-digest algorithm.
16  * The algorithm is due to Ron Rivest.	This code was
17  * written by Colin Plumb in 1993, no copyright is claimed.
18  * This code is in the public domain; do with it what you wish.
19  * Todd C. Miller modified the MD5 code to do MD4 based on RFC 1186.
20  *
21  * Equivalent code is available from RSA Data Security, Inc.
22  * This code has been tested against that, and is equivalent,
23  * except that you don't need to include two pages of legalese
24  * with every copy.
25  *
26  * To compute the message digest of a chunk of bytes, declare an
27  * MD4Context structure, pass it to MD4Init, call MD4Update as
28  * needed on buffers full of bytes, and then call MD4Final, which
29  * will fill a supplied 16-byte array with the digest.
30  */
31 
32 #include <schily/types.h>
33 #include <schily/string.h>
34 #include <schily/md4.h>
35 
36 #if !defined(HAVE_MEMCPY) || !defined(HAVE_MEMSET)
37 #include <schily/schily.h>
38 #endif
39 #if !defined(HAVE_MEMCPY) && !defined(memcpy)
40 #define	memcpy(s1, s2, n)	movebytes(s2, s1, n)
41 #endif
42 #if !defined(HAVE_MEMSET) && !defined(memset)
43 #define	memset(s, c, n)		fillbytes(s, n, c)
44 #endif
45 
46 
47 #define	PUT_64BIT_LE(cp, value) do {					\
48 	(cp)[7] = (value)[1] >> 24;					\
49 	(cp)[6] = (value)[1] >> 16;					\
50 	(cp)[5] = (value)[1] >> 8;					\
51 	(cp)[4] = (value)[1];						\
52 	(cp)[3] = (value)[0] >> 24;					\
53 	(cp)[2] = (value)[0] >> 16;					\
54 	(cp)[1] = (value)[0] >> 8;					\
55 	(cp)[0] = (value)[0]; } while (0)
56 
57 #define	PUT_32BIT_LE(cp, value) do {					\
58 	(cp)[3] = (value) >> 24;					\
59 	(cp)[2] = (value) >> 16;					\
60 	(cp)[1] = (value) >> 8;						\
61 	(cp)[0] = (value); } while (0)
62 
63 static UInt8_t PADDING[MD4_BLOCK_LENGTH] = {
64 	0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
65 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
66 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
67 };
68 
69 /*
70  * Start MD4 accumulation.
71  * Set bit count to 0 and buffer to mysterious initialization constants.
72  */
73 void
MD4Init(ctx)74 MD4Init(ctx)
75 	MD4_CTX	*ctx;
76 {
77 	ctx->count[0] = ctx->count[1] = 0;
78 	ctx->state[0] = 0x67452301;
79 	ctx->state[1] = 0xefcdab89;
80 	ctx->state[2] = 0x98badcfe;
81 	ctx->state[3] = 0x10325476;
82 }
83 
84 /*
85  * Update context to reflect the concatenation of another buffer full
86  * of bytes.
87  */
88 void
MD4Update(ctx,inputv,len)89 MD4Update(ctx, inputv, len)
90 	MD4_CTX		*ctx;
91 	const void	*inputv;
92 	size_t		len;
93 {
94 	const unsigned char	*input = inputv;
95 	size_t have, need;
96 
97 	/* Check how many bytes we already have and how many more we need. */
98 	have = (size_t)((ctx->count[0] >> 3) & (MD4_BLOCK_LENGTH - 1));
99 	need = MD4_BLOCK_LENGTH - have;
100 
101 	/* Update bitcount */
102 	if ((ctx->count[0] += (UInt32_t)len << 3) < ((UInt32_t)len << 3))
103 		ctx->count[1] += 1;
104 
105 	if (len >= need) {
106 		if (have != 0) {
107 			memcpy(ctx->buffer + have, input, need);
108 			MD4Transform(ctx->state, ctx->buffer);
109 			input += need;
110 			len -= need;
111 			have = 0;
112 		}
113 
114 		/* Process data in MD4_BLOCK_LENGTH-byte chunks. */
115 		while (len >= MD4_BLOCK_LENGTH) {
116 			MD4Transform(ctx->state, input);
117 			input += MD4_BLOCK_LENGTH;
118 			len -= MD4_BLOCK_LENGTH;
119 		}
120 	}
121 
122 	/* Handle any remaining bytes of data. */
123 	if (len != 0)
124 		memcpy(ctx->buffer + have, input, len);
125 }
126 
127 /*
128  * Pad pad to 64-byte boundary with the bit pattern
129  * 1 0* (64-bit count of bits processed, MSB-first)
130  */
131 void
MD4Pad(ctx)132 MD4Pad(ctx)
133 	MD4_CTX	*ctx;
134 {
135 	UInt8_t count[8];
136 	size_t padlen;
137 
138 	/* Convert count to 8 bytes in little endian order. */
139 	PUT_64BIT_LE(count, ctx->count);
140 
141 	/* Pad out to 56 mod 64. */
142 	padlen = MD4_BLOCK_LENGTH -
143 	    ((ctx->count[0] >> 3) & (MD4_BLOCK_LENGTH - 1));
144 	if (padlen < 1 + 8)
145 		padlen += MD4_BLOCK_LENGTH;
146 	MD4Update(ctx, PADDING, padlen - 8);		/* padlen - 8 <= 64 */
147 	MD4Update(ctx, count, 8);
148 }
149 
150 /*
151  * Final wrapup--call MD4Pad, fill in digest and zero out ctx.
152  */
153 void
MD4Final(digest,ctx)154 MD4Final(digest, ctx)
155 	unsigned char	digest[MD4_DIGEST_LENGTH];
156 	MD4_CTX		*ctx;
157 {
158 	int i;
159 
160 	MD4Pad(ctx);
161 	if (digest != NULL) {
162 		for (i = 0; i < 4; i++)
163 			PUT_32BIT_LE(digest + i * 4, ctx->state[i]);
164 		memset(ctx, 0, sizeof (*ctx));
165 	}
166 }
167 
168 
169 /* The three core functions - F1 is optimized somewhat */
170 
171 /* #define F1(x, y, z) (x & y | ~x & z) */
172 #define	F1(x, y, z) (z ^ (x & (y ^ z)))
173 #define	F2(x, y, z) ((x & y) | (x & z) | (y & z))
174 #define	F3(x, y, z) (x ^ y ^ z)
175 
176 /* This is the central step in the MD4 algorithm. */
177 #define	MD4STEP(f, w, x, y, z, data, s) \
178 	(w += f(x, y, z) + data,  w = w<<s | w>>(32-s))
179 
180 /*
181  * The core of the MD4 algorithm, this alters an existing MD4 hash to
182  * reflect the addition of 16 longwords of new data.  MD4Update blocks
183  * the data and converts bytes into longwords for this routine.
184  */
185 void
MD4Transform(state,block)186 MD4Transform(state, block)
187 	UInt32_t	state[4];
188 	const UInt8_t	block[MD4_BLOCK_LENGTH];
189 {
190 	UInt32_t a, b, c, d, in[MD4_BLOCK_LENGTH / 4];
191 
192 #ifndef	WORDS_BIGENDIAN
193 	memcpy(in, block, sizeof (in));
194 #else
195 	for (a = 0; a < MD4_BLOCK_LENGTH / 4; a++) {
196 		in[a] = (UInt32_t)(
197 		    (UInt32_t)(block[a * 4 + 0]) |
198 		    (UInt32_t)(block[a * 4 + 1]) <<  8 |
199 		    (UInt32_t)(block[a * 4 + 2]) << 16 |
200 		    (UInt32_t)(block[a * 4 + 3]) << 24);
201 	}
202 #endif
203 
204 	a = state[0];
205 	b = state[1];
206 	c = state[2];
207 	d = state[3];
208 
209 	MD4STEP(F1, a, b, c, d, in[ 0],  3);
210 	MD4STEP(F1, d, a, b, c, in[ 1],  7);
211 	MD4STEP(F1, c, d, a, b, in[ 2], 11);
212 	MD4STEP(F1, b, c, d, a, in[ 3], 19);
213 	MD4STEP(F1, a, b, c, d, in[ 4],  3);
214 	MD4STEP(F1, d, a, b, c, in[ 5],  7);
215 	MD4STEP(F1, c, d, a, b, in[ 6], 11);
216 	MD4STEP(F1, b, c, d, a, in[ 7], 19);
217 	MD4STEP(F1, a, b, c, d, in[ 8],  3);
218 	MD4STEP(F1, d, a, b, c, in[ 9],  7);
219 	MD4STEP(F1, c, d, a, b, in[10], 11);
220 	MD4STEP(F1, b, c, d, a, in[11], 19);
221 	MD4STEP(F1, a, b, c, d, in[12],  3);
222 	MD4STEP(F1, d, a, b, c, in[13],  7);
223 	MD4STEP(F1, c, d, a, b, in[14], 11);
224 	MD4STEP(F1, b, c, d, a, in[15], 19);
225 
226 	MD4STEP(F2, a, b, c, d, in[ 0] + 0x5a827999,  3);
227 	MD4STEP(F2, d, a, b, c, in[ 4] + 0x5a827999,  5);
228 	MD4STEP(F2, c, d, a, b, in[ 8] + 0x5a827999,  9);
229 	MD4STEP(F2, b, c, d, a, in[12] + 0x5a827999, 13);
230 	MD4STEP(F2, a, b, c, d, in[ 1] + 0x5a827999,  3);
231 	MD4STEP(F2, d, a, b, c, in[ 5] + 0x5a827999,  5);
232 	MD4STEP(F2, c, d, a, b, in[ 9] + 0x5a827999,  9);
233 	MD4STEP(F2, b, c, d, a, in[13] + 0x5a827999, 13);
234 	MD4STEP(F2, a, b, c, d, in[ 2] + 0x5a827999,  3);
235 	MD4STEP(F2, d, a, b, c, in[ 6] + 0x5a827999,  5);
236 	MD4STEP(F2, c, d, a, b, in[10] + 0x5a827999,  9);
237 	MD4STEP(F2, b, c, d, a, in[14] + 0x5a827999, 13);
238 	MD4STEP(F2, a, b, c, d, in[ 3] + 0x5a827999,  3);
239 	MD4STEP(F2, d, a, b, c, in[ 7] + 0x5a827999,  5);
240 	MD4STEP(F2, c, d, a, b, in[11] + 0x5a827999,  9);
241 	MD4STEP(F2, b, c, d, a, in[15] + 0x5a827999, 13);
242 
243 	MD4STEP(F3, a, b, c, d, in[ 0] + 0x6ed9eba1,  3);
244 	MD4STEP(F3, d, a, b, c, in[ 8] + 0x6ed9eba1,  9);
245 	MD4STEP(F3, c, d, a, b, in[ 4] + 0x6ed9eba1, 11);
246 	MD4STEP(F3, b, c, d, a, in[12] + 0x6ed9eba1, 15);
247 	MD4STEP(F3, a, b, c, d, in[ 2] + 0x6ed9eba1,  3);
248 	MD4STEP(F3, d, a, b, c, in[10] + 0x6ed9eba1,  9);
249 	MD4STEP(F3, c, d, a, b, in[ 6] + 0x6ed9eba1, 11);
250 	MD4STEP(F3, b, c, d, a, in[14] + 0x6ed9eba1, 15);
251 	MD4STEP(F3, a, b, c, d, in[ 1] + 0x6ed9eba1,  3);
252 	MD4STEP(F3, d, a, b, c, in[ 9] + 0x6ed9eba1,  9);
253 	MD4STEP(F3, c, d, a, b, in[ 5] + 0x6ed9eba1, 11);
254 	MD4STEP(F3, b, c, d, a, in[13] + 0x6ed9eba1, 15);
255 	MD4STEP(F3, a, b, c, d, in[ 3] + 0x6ed9eba1,  3);
256 	MD4STEP(F3, d, a, b, c, in[11] + 0x6ed9eba1,  9);
257 	MD4STEP(F3, c, d, a, b, in[ 7] + 0x6ed9eba1, 11);
258 	MD4STEP(F3, b, c, d, a, in[15] + 0x6ed9eba1, 15);
259 
260 	state[0] += a;
261 	state[1] += b;
262 	state[2] += c;
263 	state[3] += d;
264 }
265