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