Mercurial > dropbear
comparison libtomcrypt/src/hashes/md4.c @ 302:973fccb59ea4 ucc-axis-hack
propagate from branch 'au.asn.ucc.matt.dropbear' (head 11034278bd1917bebcbdc69cf53b1891ce9db121)
to branch 'au.asn.ucc.matt.dropbear.ucc-axis-hack' (head 10a1f614fec73d0820c3f61160d9db409b9beb46)
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 25 Mar 2006 12:59:58 +0000 |
parents | 1b9e69c058d2 |
children | 0cbe8f6dbf9e |
comparison
equal
deleted
inserted
replaced
299:740e782679be | 302:973fccb59ea4 |
---|---|
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis | |
2 * | |
3 * LibTomCrypt is a library that provides various cryptographic | |
4 * algorithms in a highly modular and flexible manner. | |
5 * | |
6 * The library is free for all purposes without any express | |
7 * guarantee it works. | |
8 * | |
9 * Tom St Denis, [email protected], http://libtomcrypt.org | |
10 */ | |
11 #include "tomcrypt.h" | |
12 | |
13 /** | |
14 @param md4.c | |
15 Submitted by Dobes Vandermeer ([email protected]) | |
16 */ | |
17 | |
18 #ifdef MD4 | |
19 | |
20 const struct ltc_hash_descriptor md4_desc = | |
21 { | |
22 "md4", | |
23 6, | |
24 16, | |
25 64, | |
26 | |
27 /* OID */ | |
28 { 1, 2, 840, 113549, 2, 4, }, | |
29 6, | |
30 | |
31 &md4_init, | |
32 &md4_process, | |
33 &md4_done, | |
34 &md4_test | |
35 }; | |
36 | |
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 | |
49 | |
50 /* F, G and H are basic MD4 functions. */ | |
51 #define F(x, y, z) (z ^ (x & (y ^ z))) | |
52 #define G(x, y, z) ((x & y) | (z & (x | y))) | |
53 #define H(x, y, z) ((x) ^ (y) ^ (z)) | |
54 | |
55 /* ROTATE_LEFT rotates x left n bits. */ | |
56 #define ROTATE_LEFT(x, n) ROLc(x, n) | |
57 | |
58 /* FF, GG and HH are transformations for rounds 1, 2 and 3 */ | |
59 /* Rotation is separate from addition to prevent recomputation */ | |
60 | |
61 #define FF(a, b, c, d, x, s) { \ | |
62 (a) += F ((b), (c), (d)) + (x); \ | |
63 (a) = ROTATE_LEFT ((a), (s)); \ | |
64 } | |
65 #define GG(a, b, c, d, x, s) { \ | |
66 (a) += G ((b), (c), (d)) + (x) + 0x5a827999UL; \ | |
67 (a) = ROTATE_LEFT ((a), (s)); \ | |
68 } | |
69 #define HH(a, b, c, d, x, s) { \ | |
70 (a) += H ((b), (c), (d)) + (x) + 0x6ed9eba1UL; \ | |
71 (a) = ROTATE_LEFT ((a), (s)); \ | |
72 } | |
73 | |
74 #ifdef LTC_CLEAN_STACK | |
75 static int _md4_compress(hash_state *md, unsigned char *buf) | |
76 #else | |
77 static int md4_compress(hash_state *md, unsigned char *buf) | |
78 #endif | |
79 { | |
80 ulong32 x[16], a, b, c, d; | |
81 int i; | |
82 | |
83 /* copy state */ | |
84 a = md->md4.state[0]; | |
85 b = md->md4.state[1]; | |
86 c = md->md4.state[2]; | |
87 d = md->md4.state[3]; | |
88 | |
89 /* copy the state into 512-bits into W[0..15] */ | |
90 for (i = 0; i < 16; i++) { | |
91 LOAD32L(x[i], buf + (4*i)); | |
92 } | |
93 | |
94 /* Round 1 */ | |
95 FF (a, b, c, d, x[ 0], S11); /* 1 */ | |
96 FF (d, a, b, c, x[ 1], S12); /* 2 */ | |
97 FF (c, d, a, b, x[ 2], S13); /* 3 */ | |
98 FF (b, c, d, a, x[ 3], S14); /* 4 */ | |
99 FF (a, b, c, d, x[ 4], S11); /* 5 */ | |
100 FF (d, a, b, c, x[ 5], S12); /* 6 */ | |
101 FF (c, d, a, b, x[ 6], S13); /* 7 */ | |
102 FF (b, c, d, a, x[ 7], S14); /* 8 */ | |
103 FF (a, b, c, d, x[ 8], S11); /* 9 */ | |
104 FF (d, a, b, c, x[ 9], S12); /* 10 */ | |
105 FF (c, d, a, b, x[10], S13); /* 11 */ | |
106 FF (b, c, d, a, x[11], S14); /* 12 */ | |
107 FF (a, b, c, d, x[12], S11); /* 13 */ | |
108 FF (d, a, b, c, x[13], S12); /* 14 */ | |
109 FF (c, d, a, b, x[14], S13); /* 15 */ | |
110 FF (b, c, d, a, x[15], S14); /* 16 */ | |
111 | |
112 /* Round 2 */ | |
113 GG (a, b, c, d, x[ 0], S21); /* 17 */ | |
114 GG (d, a, b, c, x[ 4], S22); /* 18 */ | |
115 GG (c, d, a, b, x[ 8], S23); /* 19 */ | |
116 GG (b, c, d, a, x[12], S24); /* 20 */ | |
117 GG (a, b, c, d, x[ 1], S21); /* 21 */ | |
118 GG (d, a, b, c, x[ 5], S22); /* 22 */ | |
119 GG (c, d, a, b, x[ 9], S23); /* 23 */ | |
120 GG (b, c, d, a, x[13], S24); /* 24 */ | |
121 GG (a, b, c, d, x[ 2], S21); /* 25 */ | |
122 GG (d, a, b, c, x[ 6], S22); /* 26 */ | |
123 GG (c, d, a, b, x[10], S23); /* 27 */ | |
124 GG (b, c, d, a, x[14], S24); /* 28 */ | |
125 GG (a, b, c, d, x[ 3], S21); /* 29 */ | |
126 GG (d, a, b, c, x[ 7], S22); /* 30 */ | |
127 GG (c, d, a, b, x[11], S23); /* 31 */ | |
128 GG (b, c, d, a, x[15], S24); /* 32 */ | |
129 | |
130 /* Round 3 */ | |
131 HH (a, b, c, d, x[ 0], S31); /* 33 */ | |
132 HH (d, a, b, c, x[ 8], S32); /* 34 */ | |
133 HH (c, d, a, b, x[ 4], S33); /* 35 */ | |
134 HH (b, c, d, a, x[12], S34); /* 36 */ | |
135 HH (a, b, c, d, x[ 2], S31); /* 37 */ | |
136 HH (d, a, b, c, x[10], S32); /* 38 */ | |
137 HH (c, d, a, b, x[ 6], S33); /* 39 */ | |
138 HH (b, c, d, a, x[14], S34); /* 40 */ | |
139 HH (a, b, c, d, x[ 1], S31); /* 41 */ | |
140 HH (d, a, b, c, x[ 9], S32); /* 42 */ | |
141 HH (c, d, a, b, x[ 5], S33); /* 43 */ | |
142 HH (b, c, d, a, x[13], S34); /* 44 */ | |
143 HH (a, b, c, d, x[ 3], S31); /* 45 */ | |
144 HH (d, a, b, c, x[11], S32); /* 46 */ | |
145 HH (c, d, a, b, x[ 7], S33); /* 47 */ | |
146 HH (b, c, d, a, x[15], S34); /* 48 */ | |
147 | |
148 | |
149 /* Update our state */ | |
150 md->md4.state[0] = md->md4.state[0] + a; | |
151 md->md4.state[1] = md->md4.state[1] + b; | |
152 md->md4.state[2] = md->md4.state[2] + c; | |
153 md->md4.state[3] = md->md4.state[3] + d; | |
154 | |
155 return CRYPT_OK; | |
156 } | |
157 | |
158 #ifdef LTC_CLEAN_STACK | |
159 static int md4_compress(hash_state *md, unsigned char *buf) | |
160 { | |
161 int err; | |
162 err = _md4_compress(md, buf); | |
163 burn_stack(sizeof(ulong32) * 20 + sizeof(int)); | |
164 return err; | |
165 } | |
166 #endif | |
167 | |
168 /** | |
169 Initialize the hash state | |
170 @param md The hash state you wish to initialize | |
171 @return CRYPT_OK if successful | |
172 */ | |
173 int md4_init(hash_state * md) | |
174 { | |
175 LTC_ARGCHK(md != NULL); | |
176 md->md4.state[0] = 0x67452301UL; | |
177 md->md4.state[1] = 0xefcdab89UL; | |
178 md->md4.state[2] = 0x98badcfeUL; | |
179 md->md4.state[3] = 0x10325476UL; | |
180 md->md4.length = 0; | |
181 md->md4.curlen = 0; | |
182 return CRYPT_OK; | |
183 } | |
184 | |
185 /** | |
186 Process a block of memory though the hash | |
187 @param md The hash state | |
188 @param in The data to hash | |
189 @param inlen The length of the data (octets) | |
190 @return CRYPT_OK if successful | |
191 */ | |
192 HASH_PROCESS(md4_process, md4_compress, md4, 64) | |
193 | |
194 /** | |
195 Terminate the hash to get the digest | |
196 @param md The hash state | |
197 @param out [out] The destination of the hash (16 bytes) | |
198 @return CRYPT_OK if successful | |
199 */ | |
200 int md4_done(hash_state * md, unsigned char *out) | |
201 { | |
202 int i; | |
203 | |
204 LTC_ARGCHK(md != NULL); | |
205 LTC_ARGCHK(out != NULL); | |
206 | |
207 if (md->md4.curlen >= sizeof(md->md4.buf)) { | |
208 return CRYPT_INVALID_ARG; | |
209 } | |
210 | |
211 /* increase the length of the message */ | |
212 md->md4.length += md->md4.curlen * 8; | |
213 | |
214 /* append the '1' bit */ | |
215 md->md4.buf[md->md4.curlen++] = (unsigned char)0x80; | |
216 | |
217 /* if the length is currently above 56 bytes we append zeros | |
218 * then compress. Then we can fall back to padding zeros and length | |
219 * encoding like normal. | |
220 */ | |
221 if (md->md4.curlen > 56) { | |
222 while (md->md4.curlen < 64) { | |
223 md->md4.buf[md->md4.curlen++] = (unsigned char)0; | |
224 } | |
225 md4_compress(md, md->md4.buf); | |
226 md->md4.curlen = 0; | |
227 } | |
228 | |
229 /* pad upto 56 bytes of zeroes */ | |
230 while (md->md4.curlen < 56) { | |
231 md->md4.buf[md->md4.curlen++] = (unsigned char)0; | |
232 } | |
233 | |
234 /* store length */ | |
235 STORE64L(md->md4.length, md->md4.buf+56); | |
236 md4_compress(md, md->md4.buf); | |
237 | |
238 /* copy output */ | |
239 for (i = 0; i < 4; i++) { | |
240 STORE32L(md->md4.state[i], out+(4*i)); | |
241 } | |
242 #ifdef LTC_CLEAN_STACK | |
243 zeromem(md, sizeof(hash_state)); | |
244 #endif | |
245 return CRYPT_OK; | |
246 } | |
247 | |
248 /** | |
249 Self-test the hash | |
250 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled | |
251 */ | |
252 int md4_test(void) | |
253 { | |
254 #ifndef LTC_TEST | |
255 return CRYPT_NOP; | |
256 #else | |
257 static const struct md4_test_case { | |
258 char *input; | |
259 unsigned char digest[16]; | |
260 } cases[] = { | |
261 { "", | |
262 {0x31, 0xd6, 0xcf, 0xe0, 0xd1, 0x6a, 0xe9, 0x31, | |
263 0xb7, 0x3c, 0x59, 0xd7, 0xe0, 0xc0, 0x89, 0xc0} }, | |
264 { "a", | |
265 {0xbd, 0xe5, 0x2c, 0xb3, 0x1d, 0xe3, 0x3e, 0x46, | |
266 0x24, 0x5e, 0x05, 0xfb, 0xdb, 0xd6, 0xfb, 0x24} }, | |
267 { "abc", | |
268 {0xa4, 0x48, 0x01, 0x7a, 0xaf, 0x21, 0xd8, 0x52, | |
269 0x5f, 0xc1, 0x0a, 0xe8, 0x7a, 0xa6, 0x72, 0x9d} }, | |
270 { "message digest", | |
271 {0xd9, 0x13, 0x0a, 0x81, 0x64, 0x54, 0x9f, 0xe8, | |
272 0x18, 0x87, 0x48, 0x06, 0xe1, 0xc7, 0x01, 0x4b} }, | |
273 { "abcdefghijklmnopqrstuvwxyz", | |
274 {0xd7, 0x9e, 0x1c, 0x30, 0x8a, 0xa5, 0xbb, 0xcd, | |
275 0xee, 0xa8, 0xed, 0x63, 0xdf, 0x41, 0x2d, 0xa9} }, | |
276 { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", | |
277 {0x04, 0x3f, 0x85, 0x82, 0xf2, 0x41, 0xdb, 0x35, | |
278 0x1c, 0xe6, 0x27, 0xe1, 0x53, 0xe7, 0xf0, 0xe4} }, | |
279 { "12345678901234567890123456789012345678901234567890123456789012345678901234567890", | |
280 {0xe3, 0x3b, 0x4d, 0xdc, 0x9c, 0x38, 0xf2, 0x19, | |
281 0x9c, 0x3e, 0x7b, 0x16, 0x4f, 0xcc, 0x05, 0x36} }, | |
282 }; | |
283 int i; | |
284 hash_state md; | |
285 unsigned char digest[16]; | |
286 | |
287 for(i = 0; i < (int)(sizeof(cases) / sizeof(cases[0])); i++) { | |
288 md4_init(&md); | |
289 md4_process(&md, (unsigned char *)cases[i].input, (unsigned long)strlen(cases[i].input)); | |
290 md4_done(&md, digest); | |
291 if (memcmp(digest, cases[i].digest, 16) != 0) { | |
292 return CRYPT_FAIL_TESTVECTOR; | |
293 } | |
294 | |
295 } | |
296 return CRYPT_OK; | |
297 #endif | |
298 } | |
299 | |
300 #endif | |
301 | |
302 | |
303 | |
304 /* $Source: /cvs/libtom/libtomcrypt/src/hashes/md4.c,v $ */ | |
305 /* $Revision: 1.5 $ */ | |
306 /* $Date: 2005/05/23 02:42:07 $ */ |