Mercurial > dropbear
comparison src/hashes/md5.c @ 191:1c15b283127b libtomcrypt-orig
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 06 May 2005 13:23:02 +0000 |
parents | |
children | 9cc34777b479 39d5d58461d6 |
comparison
equal
deleted
inserted
replaced
143:5d99163f7e32 | 191:1c15b283127b |
---|---|
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 /** | |
15 @file md5.c | |
16 MD5 hash function by Tom St Denis | |
17 */ | |
18 | |
19 #ifdef MD5 | |
20 | |
21 const struct ltc_hash_descriptor md5_desc = | |
22 { | |
23 "md5", | |
24 3, | |
25 16, | |
26 64, | |
27 | |
28 /* DER identifier */ | |
29 { 0x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, | |
30 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x05, 0x05, 0x00, | |
31 0x04, 0x10 }, | |
32 18, | |
33 | |
34 &md5_init, | |
35 &md5_process, | |
36 &md5_done, | |
37 &md5_test | |
38 }; | |
39 | |
40 #define F(x,y,z) (z ^ (x & (y ^ z))) | |
41 #define G(x,y,z) (y ^ (z & (y ^ x))) | |
42 #define H(x,y,z) (x^y^z) | |
43 #define I(x,y,z) (y^(x|(~z))) | |
44 | |
45 #ifdef LTC_SMALL_CODE | |
46 | |
47 #define FF(a,b,c,d,M,s,t) \ | |
48 a = (a + F(b,c,d) + M + t); a = ROL(a, s) + b; | |
49 | |
50 #define GG(a,b,c,d,M,s,t) \ | |
51 a = (a + G(b,c,d) + M + t); a = ROL(a, s) + b; | |
52 | |
53 #define HH(a,b,c,d,M,s,t) \ | |
54 a = (a + H(b,c,d) + M + t); a = ROL(a, s) + b; | |
55 | |
56 #define II(a,b,c,d,M,s,t) \ | |
57 a = (a + I(b,c,d) + M + t); a = ROL(a, s) + b; | |
58 | |
59 static const unsigned char Worder[64] = { | |
60 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, | |
61 1,6,11,0,5,10,15,4,9,14,3,8,13,2,7,12, | |
62 5,8,11,14,1,4,7,10,13,0,3,6,9,12,15,2, | |
63 0,7,14,5,12,3,10,1,8,15,6,13,4,11,2,9 | |
64 }; | |
65 | |
66 static const unsigned char Rorder[64] = { | |
67 7,12,17,22,7,12,17,22,7,12,17,22,7,12,17,22, | |
68 5,9,14,20,5,9,14,20,5,9,14,20,5,9,14,20, | |
69 4,11,16,23,4,11,16,23,4,11,16,23,4,11,16,23, | |
70 6,10,15,21,6,10,15,21,6,10,15,21,6,10,15,21 | |
71 }; | |
72 | |
73 static const ulong32 Korder[64] = { | |
74 0xd76aa478UL, 0xe8c7b756UL, 0x242070dbUL, 0xc1bdceeeUL, 0xf57c0fafUL, 0x4787c62aUL, 0xa8304613UL, 0xfd469501UL, | |
75 0x698098d8UL, 0x8b44f7afUL, 0xffff5bb1UL, 0x895cd7beUL, 0x6b901122UL, 0xfd987193UL, 0xa679438eUL, 0x49b40821UL, | |
76 0xf61e2562UL, 0xc040b340UL, 0x265e5a51UL, 0xe9b6c7aaUL, 0xd62f105dUL, 0x02441453UL, 0xd8a1e681UL, 0xe7d3fbc8UL, | |
77 0x21e1cde6UL, 0xc33707d6UL, 0xf4d50d87UL, 0x455a14edUL, 0xa9e3e905UL, 0xfcefa3f8UL, 0x676f02d9UL, 0x8d2a4c8aUL, | |
78 0xfffa3942UL, 0x8771f681UL, 0x6d9d6122UL, 0xfde5380cUL, 0xa4beea44UL, 0x4bdecfa9UL, 0xf6bb4b60UL, 0xbebfbc70UL, | |
79 0x289b7ec6UL, 0xeaa127faUL, 0xd4ef3085UL, 0x04881d05UL, 0xd9d4d039UL, 0xe6db99e5UL, 0x1fa27cf8UL, 0xc4ac5665UL, | |
80 0xf4292244UL, 0x432aff97UL, 0xab9423a7UL, 0xfc93a039UL, 0x655b59c3UL, 0x8f0ccc92UL, 0xffeff47dUL, 0x85845dd1UL, | |
81 0x6fa87e4fUL, 0xfe2ce6e0UL, 0xa3014314UL, 0x4e0811a1UL, 0xf7537e82UL, 0xbd3af235UL, 0x2ad7d2bbUL, 0xeb86d391UL | |
82 }; | |
83 | |
84 #else | |
85 | |
86 #define FF(a,b,c,d,M,s,t) \ | |
87 a = (a + F(b,c,d) + M + t); a = ROLc(a, s) + b; | |
88 | |
89 #define GG(a,b,c,d,M,s,t) \ | |
90 a = (a + G(b,c,d) + M + t); a = ROLc(a, s) + b; | |
91 | |
92 #define HH(a,b,c,d,M,s,t) \ | |
93 a = (a + H(b,c,d) + M + t); a = ROLc(a, s) + b; | |
94 | |
95 #define II(a,b,c,d,M,s,t) \ | |
96 a = (a + I(b,c,d) + M + t); a = ROLc(a, s) + b; | |
97 | |
98 | |
99 #endif | |
100 | |
101 #ifdef LTC_CLEAN_STACK | |
102 static int _md5_compress(hash_state *md, unsigned char *buf) | |
103 #else | |
104 static int md5_compress(hash_state *md, unsigned char *buf) | |
105 #endif | |
106 { | |
107 ulong32 i, W[16], a, b, c, d; | |
108 #ifdef LTC_SMALL_CODE | |
109 ulong32 t; | |
110 #endif | |
111 | |
112 /* copy the state into 512-bits into W[0..15] */ | |
113 for (i = 0; i < 16; i++) { | |
114 LOAD32L(W[i], buf + (4*i)); | |
115 } | |
116 | |
117 /* copy state */ | |
118 a = md->md5.state[0]; | |
119 b = md->md5.state[1]; | |
120 c = md->md5.state[2]; | |
121 d = md->md5.state[3]; | |
122 | |
123 #ifdef LTC_SMALL_CODE | |
124 for (i = 0; i < 16; ++i) { | |
125 FF(a,b,c,d,W[Worder[i]],Rorder[i],Korder[i]); | |
126 t = d; d = c; c = b; b = a; a = t; | |
127 } | |
128 | |
129 for (; i < 32; ++i) { | |
130 GG(a,b,c,d,W[Worder[i]],Rorder[i],Korder[i]); | |
131 t = d; d = c; c = b; b = a; a = t; | |
132 } | |
133 | |
134 for (; i < 48; ++i) { | |
135 HH(a,b,c,d,W[Worder[i]],Rorder[i],Korder[i]); | |
136 t = d; d = c; c = b; b = a; a = t; | |
137 } | |
138 | |
139 for (; i < 64; ++i) { | |
140 II(a,b,c,d,W[Worder[i]],Rorder[i],Korder[i]); | |
141 t = d; d = c; c = b; b = a; a = t; | |
142 } | |
143 | |
144 #else | |
145 FF(a,b,c,d,W[0],7,0xd76aa478UL) | |
146 FF(d,a,b,c,W[1],12,0xe8c7b756UL) | |
147 FF(c,d,a,b,W[2],17,0x242070dbUL) | |
148 FF(b,c,d,a,W[3],22,0xc1bdceeeUL) | |
149 FF(a,b,c,d,W[4],7,0xf57c0fafUL) | |
150 FF(d,a,b,c,W[5],12,0x4787c62aUL) | |
151 FF(c,d,a,b,W[6],17,0xa8304613UL) | |
152 FF(b,c,d,a,W[7],22,0xfd469501UL) | |
153 FF(a,b,c,d,W[8],7,0x698098d8UL) | |
154 FF(d,a,b,c,W[9],12,0x8b44f7afUL) | |
155 FF(c,d,a,b,W[10],17,0xffff5bb1UL) | |
156 FF(b,c,d,a,W[11],22,0x895cd7beUL) | |
157 FF(a,b,c,d,W[12],7,0x6b901122UL) | |
158 FF(d,a,b,c,W[13],12,0xfd987193UL) | |
159 FF(c,d,a,b,W[14],17,0xa679438eUL) | |
160 FF(b,c,d,a,W[15],22,0x49b40821UL) | |
161 GG(a,b,c,d,W[1],5,0xf61e2562UL) | |
162 GG(d,a,b,c,W[6],9,0xc040b340UL) | |
163 GG(c,d,a,b,W[11],14,0x265e5a51UL) | |
164 GG(b,c,d,a,W[0],20,0xe9b6c7aaUL) | |
165 GG(a,b,c,d,W[5],5,0xd62f105dUL) | |
166 GG(d,a,b,c,W[10],9,0x02441453UL) | |
167 GG(c,d,a,b,W[15],14,0xd8a1e681UL) | |
168 GG(b,c,d,a,W[4],20,0xe7d3fbc8UL) | |
169 GG(a,b,c,d,W[9],5,0x21e1cde6UL) | |
170 GG(d,a,b,c,W[14],9,0xc33707d6UL) | |
171 GG(c,d,a,b,W[3],14,0xf4d50d87UL) | |
172 GG(b,c,d,a,W[8],20,0x455a14edUL) | |
173 GG(a,b,c,d,W[13],5,0xa9e3e905UL) | |
174 GG(d,a,b,c,W[2],9,0xfcefa3f8UL) | |
175 GG(c,d,a,b,W[7],14,0x676f02d9UL) | |
176 GG(b,c,d,a,W[12],20,0x8d2a4c8aUL) | |
177 HH(a,b,c,d,W[5],4,0xfffa3942UL) | |
178 HH(d,a,b,c,W[8],11,0x8771f681UL) | |
179 HH(c,d,a,b,W[11],16,0x6d9d6122UL) | |
180 HH(b,c,d,a,W[14],23,0xfde5380cUL) | |
181 HH(a,b,c,d,W[1],4,0xa4beea44UL) | |
182 HH(d,a,b,c,W[4],11,0x4bdecfa9UL) | |
183 HH(c,d,a,b,W[7],16,0xf6bb4b60UL) | |
184 HH(b,c,d,a,W[10],23,0xbebfbc70UL) | |
185 HH(a,b,c,d,W[13],4,0x289b7ec6UL) | |
186 HH(d,a,b,c,W[0],11,0xeaa127faUL) | |
187 HH(c,d,a,b,W[3],16,0xd4ef3085UL) | |
188 HH(b,c,d,a,W[6],23,0x04881d05UL) | |
189 HH(a,b,c,d,W[9],4,0xd9d4d039UL) | |
190 HH(d,a,b,c,W[12],11,0xe6db99e5UL) | |
191 HH(c,d,a,b,W[15],16,0x1fa27cf8UL) | |
192 HH(b,c,d,a,W[2],23,0xc4ac5665UL) | |
193 II(a,b,c,d,W[0],6,0xf4292244UL) | |
194 II(d,a,b,c,W[7],10,0x432aff97UL) | |
195 II(c,d,a,b,W[14],15,0xab9423a7UL) | |
196 II(b,c,d,a,W[5],21,0xfc93a039UL) | |
197 II(a,b,c,d,W[12],6,0x655b59c3UL) | |
198 II(d,a,b,c,W[3],10,0x8f0ccc92UL) | |
199 II(c,d,a,b,W[10],15,0xffeff47dUL) | |
200 II(b,c,d,a,W[1],21,0x85845dd1UL) | |
201 II(a,b,c,d,W[8],6,0x6fa87e4fUL) | |
202 II(d,a,b,c,W[15],10,0xfe2ce6e0UL) | |
203 II(c,d,a,b,W[6],15,0xa3014314UL) | |
204 II(b,c,d,a,W[13],21,0x4e0811a1UL) | |
205 II(a,b,c,d,W[4],6,0xf7537e82UL) | |
206 II(d,a,b,c,W[11],10,0xbd3af235UL) | |
207 II(c,d,a,b,W[2],15,0x2ad7d2bbUL) | |
208 II(b,c,d,a,W[9],21,0xeb86d391UL) | |
209 #endif | |
210 | |
211 md->md5.state[0] = md->md5.state[0] + a; | |
212 md->md5.state[1] = md->md5.state[1] + b; | |
213 md->md5.state[2] = md->md5.state[2] + c; | |
214 md->md5.state[3] = md->md5.state[3] + d; | |
215 | |
216 return CRYPT_OK; | |
217 } | |
218 | |
219 #ifdef LTC_CLEAN_STACK | |
220 static int md5_compress(hash_state *md, unsigned char *buf) | |
221 { | |
222 int err; | |
223 err = _md5_compress(md, buf); | |
224 burn_stack(sizeof(ulong32) * 21); | |
225 return err; | |
226 } | |
227 #endif | |
228 | |
229 /** | |
230 Initialize the hash state | |
231 @param md The hash state you wish to initialize | |
232 @return CRYPT_OK if successful | |
233 */ | |
234 int md5_init(hash_state * md) | |
235 { | |
236 LTC_ARGCHK(md != NULL); | |
237 md->md5.state[0] = 0x67452301UL; | |
238 md->md5.state[1] = 0xefcdab89UL; | |
239 md->md5.state[2] = 0x98badcfeUL; | |
240 md->md5.state[3] = 0x10325476UL; | |
241 md->md5.curlen = 0; | |
242 md->md5.length = 0; | |
243 return CRYPT_OK; | |
244 } | |
245 | |
246 /** | |
247 Process a block of memory though the hash | |
248 @param md The hash state | |
249 @param in The data to hash | |
250 @param inlen The length of the data (octets) | |
251 @return CRYPT_OK if successful | |
252 */ | |
253 HASH_PROCESS(md5_process, md5_compress, md5, 64) | |
254 | |
255 /** | |
256 Terminate the hash to get the digest | |
257 @param md The hash state | |
258 @param out [out] The destination of the hash (16 bytes) | |
259 @return CRYPT_OK if successful | |
260 */ | |
261 int md5_done(hash_state * md, unsigned char *out) | |
262 { | |
263 int i; | |
264 | |
265 LTC_ARGCHK(md != NULL); | |
266 LTC_ARGCHK(out != NULL); | |
267 | |
268 if (md->md5.curlen >= sizeof(md->md5.buf)) { | |
269 return CRYPT_INVALID_ARG; | |
270 } | |
271 | |
272 | |
273 /* increase the length of the message */ | |
274 md->md5.length += md->md5.curlen * 8; | |
275 | |
276 /* append the '1' bit */ | |
277 md->md5.buf[md->md5.curlen++] = (unsigned char)0x80; | |
278 | |
279 /* if the length is currently above 56 bytes we append zeros | |
280 * then compress. Then we can fall back to padding zeros and length | |
281 * encoding like normal. | |
282 */ | |
283 if (md->md5.curlen > 56) { | |
284 while (md->md5.curlen < 64) { | |
285 md->md5.buf[md->md5.curlen++] = (unsigned char)0; | |
286 } | |
287 md5_compress(md, md->md5.buf); | |
288 md->md5.curlen = 0; | |
289 } | |
290 | |
291 /* pad upto 56 bytes of zeroes */ | |
292 while (md->md5.curlen < 56) { | |
293 md->md5.buf[md->md5.curlen++] = (unsigned char)0; | |
294 } | |
295 | |
296 /* store length */ | |
297 STORE64L(md->md5.length, md->md5.buf+56); | |
298 md5_compress(md, md->md5.buf); | |
299 | |
300 /* copy output */ | |
301 for (i = 0; i < 4; i++) { | |
302 STORE32L(md->md5.state[i], out+(4*i)); | |
303 } | |
304 #ifdef LTC_CLEAN_STACK | |
305 zeromem(md, sizeof(hash_state)); | |
306 #endif | |
307 return CRYPT_OK; | |
308 } | |
309 | |
310 /** | |
311 Self-test the hash | |
312 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled | |
313 */ | |
314 int md5_test(void) | |
315 { | |
316 #ifndef LTC_TEST | |
317 return CRYPT_NOP; | |
318 #else | |
319 static const struct { | |
320 char *msg; | |
321 unsigned char hash[16]; | |
322 } tests[] = { | |
323 { "", | |
324 { 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, | |
325 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e } }, | |
326 { "a", | |
327 {0x0c, 0xc1, 0x75, 0xb9, 0xc0, 0xf1, 0xb6, 0xa8, | |
328 0x31, 0xc3, 0x99, 0xe2, 0x69, 0x77, 0x26, 0x61 } }, | |
329 { "abc", | |
330 { 0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0, | |
331 0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72 } }, | |
332 { "message digest", | |
333 { 0xf9, 0x6b, 0x69, 0x7d, 0x7c, 0xb7, 0x93, 0x8d, | |
334 0x52, 0x5a, 0x2f, 0x31, 0xaa, 0xf1, 0x61, 0xd0 } }, | |
335 { "abcdefghijklmnopqrstuvwxyz", | |
336 { 0xc3, 0xfc, 0xd3, 0xd7, 0x61, 0x92, 0xe4, 0x00, | |
337 0x7d, 0xfb, 0x49, 0x6c, 0xca, 0x67, 0xe1, 0x3b } }, | |
338 { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", | |
339 { 0xd1, 0x74, 0xab, 0x98, 0xd2, 0x77, 0xd9, 0xf5, | |
340 0xa5, 0x61, 0x1c, 0x2c, 0x9f, 0x41, 0x9d, 0x9f } }, | |
341 { "12345678901234567890123456789012345678901234567890123456789012345678901234567890", | |
342 { 0x57, 0xed, 0xf4, 0xa2, 0x2b, 0xe3, 0xc9, 0x55, | |
343 0xac, 0x49, 0xda, 0x2e, 0x21, 0x07, 0xb6, 0x7a } }, | |
344 { NULL, { 0 } } | |
345 }; | |
346 | |
347 int i; | |
348 unsigned char tmp[16]; | |
349 hash_state md; | |
350 | |
351 for (i = 0; tests[i].msg != NULL; i++) { | |
352 md5_init(&md); | |
353 md5_process(&md, (unsigned char *)tests[i].msg, (unsigned long)strlen(tests[i].msg)); | |
354 md5_done(&md, tmp); | |
355 if (memcmp(tmp, tests[i].hash, 16) != 0) { | |
356 return CRYPT_FAIL_TESTVECTOR; | |
357 } | |
358 } | |
359 return CRYPT_OK; | |
360 #endif | |
361 } | |
362 | |
363 #endif | |
364 | |
365 |