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