1 /*
2  * This file is part of John the Ripper password cracker,
3  * Copyright (c) 1996-2001,2003,2006,2011 by Solar Designer
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted.
7  *
8  * There's ABSOLUTELY NO WARRANTY, express or implied.
9  *
10  * This implementation of FreeBSD-style MD5-based crypt(3) password hashing
11  * supports passwords of up to 15 characters long only since this lets us use a
12  * significantly faster algorithm. -- SD
13  */
14 
15 #include <string.h>
16 
17 #include "arch.h"
18 #include "common.h"
19 #include "MD5_std.h"
20 #include "md5crypt_common.h"
21 
22 #if MD5_std_mt
23 #include <omp.h>
24 int MD5_std_min_kpc, MD5_std_max_kpc;
25 int MD5_std_nt;
26 MD5_std_combined *MD5_std_all_p = NULL;
27 static char saved_salt[9];
28 static int salt_changed;
29 #else
30 MD5_std_combined CC_CACHE_ALIGN MD5_std_all;
31 #endif
32 
33 #if !MD5_IMM
34 static const MD5_data MD5_data_init = {
35 	{
36 		0xd76aa477, 0xf8fa0bcc, 0xbcdb4dd9, 0xb18b7a77,
37 		0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
38 		0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
39 		0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
40 		0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
41 		0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
42 		0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
43 		0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
44 		0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
45 		0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
46 		0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05,
47 		0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
48 		0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
49 		0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
50 		0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
51 		0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
52 	}, {
53 		0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476
54 	}, {
55 		0x77777777, 0x00ff00ff
56 	}
57 };
58 #endif
59 
60 #if !MD5_ASM
61 
62 #define S11				7
63 #define S12				12
64 #define S13				17
65 #define S14				22
66 #define S21				5
67 #define S22				9
68 #define S23				14
69 #define S24				20
70 #define S31				4
71 #define S32				11
72 #define S33				16
73 #define S34				23
74 #define S41				6
75 #define S42				10
76 #define S43				15
77 #define S44				21
78 
79 #if MD5_IMM
80 
81 /*
82  * Using immediate values is good for CISC.
83  */
84 
85 #define AC1				0xd76aa477
86 #define AC2pCd				0xf8fa0bcc
87 #define AC3pCc				0xbcdb4dd9
88 #define AC4pCb				0xb18b7a77
89 #define AC5				0xf57c0faf
90 #define AC6				0x4787c62a
91 #define AC7				0xa8304613
92 #define AC8				0xfd469501
93 #define AC9				0x698098d8
94 #define AC10				0x8b44f7af
95 #define AC11				0xffff5bb1
96 #define AC12				0x895cd7be
97 #define AC13				0x6b901122
98 #define AC14				0xfd987193
99 #define AC15				0xa679438e
100 #define AC16				0x49b40821
101 #define AC17				0xf61e2562
102 #define AC18				0xc040b340
103 #define AC19				0x265e5a51
104 #define AC20				0xe9b6c7aa
105 #define AC21				0xd62f105d
106 #define AC22				0x02441453
107 #define AC23				0xd8a1e681
108 #define AC24				0xe7d3fbc8
109 #define AC25				0x21e1cde6
110 #define AC26				0xc33707d6
111 #define AC27				0xf4d50d87
112 #define AC28				0x455a14ed
113 #define AC29				0xa9e3e905
114 #define AC30				0xfcefa3f8
115 #define AC31				0x676f02d9
116 #define AC32				0x8d2a4c8a
117 #define AC33				0xfffa3942
118 #define AC34				0x8771f681
119 #define AC35				0x6d9d6122
120 #define AC36				0xfde5380c
121 #define AC37				0xa4beea44
122 #define AC38				0x4bdecfa9
123 #define AC39				0xf6bb4b60
124 #define AC40				0xbebfbc70
125 #define AC41				0x289b7ec6
126 #define AC42				0xeaa127fa
127 #define AC43				0xd4ef3085
128 #define AC44				0x04881d05
129 #define AC45				0xd9d4d039
130 #define AC46				0xe6db99e5
131 #define AC47				0x1fa27cf8
132 #define AC48				0xc4ac5665
133 #define AC49				0xf4292244
134 #define AC50				0x432aff97
135 #define AC51				0xab9423a7
136 #define AC52				0xfc93a039
137 #define AC53				0x655b59c3
138 #define AC54				0x8f0ccc92
139 #define AC55				0xffeff47d
140 #define AC56				0x85845dd1
141 #define AC57				0x6fa87e4f
142 #define AC58				0xfe2ce6e0
143 #define AC59				0xa3014314
144 #define AC60				0x4e0811a1
145 #define AC61				0xf7537e82
146 #define AC62				0xbd3af235
147 #define AC63				0x2ad7d2bb
148 #define AC64				0xeb86d391
149 
150 #define Ca				0x67452301
151 #define Cb				0xefcdab89
152 #define Cc				0x98badcfe
153 #define Cd				0x10325476
154 
155 #define MASK1				0x77777777
156 
157 #define OOFFOOFF			0x00ff00ff
158 
159 #else
160 
161 /*
162  * If we used immediate values on RISC with 32-bit instruction size, it would
163  * take about twice more instructions to load all the values.
164  */
165 
166 #define MD5_AC				MD5_std_all.data.AC
167 #define AC1				MD5_AC[0]
168 #define AC2pCd				MD5_AC[1]
169 #define AC3pCc				MD5_AC[2]
170 #define AC4pCb				MD5_AC[3]
171 #define AC5				MD5_AC[4]
172 #define AC6				MD5_AC[5]
173 #define AC7				MD5_AC[6]
174 #define AC8				MD5_AC[7]
175 #define AC9				MD5_AC[8]
176 #define AC10				MD5_AC[9]
177 #define AC11				MD5_AC[10]
178 #define AC12				MD5_AC[11]
179 #define AC13				MD5_AC[12]
180 #define AC14				MD5_AC[13]
181 #define AC15				MD5_AC[14]
182 #define AC16				MD5_AC[15]
183 #define AC17				MD5_AC[16]
184 #define AC18				MD5_AC[17]
185 #define AC19				MD5_AC[18]
186 #define AC20				MD5_AC[19]
187 #define AC21				MD5_AC[20]
188 #define AC22				MD5_AC[21]
189 #define AC23				MD5_AC[22]
190 #define AC24				MD5_AC[23]
191 #define AC25				MD5_AC[24]
192 #define AC26				MD5_AC[25]
193 #define AC27				MD5_AC[26]
194 #define AC28				MD5_AC[27]
195 #define AC29				MD5_AC[28]
196 #define AC30				MD5_AC[29]
197 #define AC31				MD5_AC[30]
198 #define AC32				MD5_AC[31]
199 #define AC33				MD5_AC[32]
200 #define AC34				MD5_AC[33]
201 #define AC35				MD5_AC[34]
202 #define AC36				MD5_AC[35]
203 #define AC37				MD5_AC[36]
204 #define AC38				MD5_AC[37]
205 #define AC39				MD5_AC[38]
206 #define AC40				MD5_AC[39]
207 #define AC41				MD5_AC[40]
208 #define AC42				MD5_AC[41]
209 #define AC43				MD5_AC[42]
210 #define AC44				MD5_AC[43]
211 #define AC45				MD5_AC[44]
212 #define AC46				MD5_AC[45]
213 #define AC47				MD5_AC[46]
214 #define AC48				MD5_AC[47]
215 #define AC49				MD5_AC[48]
216 #define AC50				MD5_AC[49]
217 #define AC51				MD5_AC[50]
218 #define AC52				MD5_AC[51]
219 #define AC53				MD5_AC[52]
220 #define AC54				MD5_AC[53]
221 #define AC55				MD5_AC[54]
222 #define AC56				MD5_AC[55]
223 #define AC57				MD5_AC[56]
224 #define AC58				MD5_AC[57]
225 #define AC59				MD5_AC[58]
226 #define AC60				MD5_AC[59]
227 #define AC61				MD5_AC[60]
228 #define AC62				MD5_AC[61]
229 #define AC63				MD5_AC[62]
230 #define AC64				MD5_AC[63]
231 
232 #define MD5_IV				MD5_std_all.data.IV
233 #define Ca				MD5_IV[0]
234 #define Cb				MD5_IV[1]
235 #define Cc				MD5_IV[2]
236 #define Cd				MD5_IV[3]
237 
238 #define MASK1				MD5_std_all.data.masks[0]
239 
240 #define OOFFOOFF			MD5_std_all.data.masks[1]
241 
242 #endif
243 
244 /*
245  * F, G, H and I are basic MD5 functions.
246  */
247 #define F(x, y, z)			((z) ^ ((x) & ((y) ^ (z))))
248 #define G(x, y, z)			((y) ^ ((z) & ((x) ^ (y))))
249 #define H(x, y, z)			(((x) ^ (y)) ^ (z))
250 #define H2(x, y, z)			((x) ^ ((y) ^ (z)))
251 #define I(x, y, z)			((y) ^ ((x) | ~(z)))
252 
253 /*
254  * ROTATE_LEFT rotates x left n bits.
255  */
256 #define ROTATE_LEFT(x, n) \
257 	(x) = (((x) << (n)) | ((MD5_word)(x) >> (32 - (n))))
258 
259 /*
260  * FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
261  * Rotation is separate from addition to prevent recomputation.
262  */
263 
264 #define FF(a, b, c, d, x, s, ac) \
265 	(a) += F ((b), (c), (d)) + (x) + (ac); \
266 	ROTATE_LEFT ((a), (s)); \
267 	(a) += (b);
268 
269 #define GG(a, b, c, d, x, s, ac) \
270 	(a) += G ((b), (c), (d)) + (x) + (ac); \
271 	ROTATE_LEFT ((a), (s)); \
272 	(a) += (b);
273 
274 #define HH(a, b, c, d, x, s, ac) \
275 	(a) += H ((b), (c), (d)) + (x) + (ac); \
276 	ROTATE_LEFT ((a), (s)); \
277 	(a) += (b);
278 
279 #define HH2(a, b, c, d, x, s, ac) \
280 	(a) += H2 ((b), (c), (d)) + (x) + (ac); \
281 	ROTATE_LEFT ((a), (s)); \
282 	(a) += (b);
283 
284 #define II(a, b, c, d, x, s, ac) \
285 	(a) += I ((b), (c), (d)) + (x) + (ac); \
286 	ROTATE_LEFT ((a), (s)); \
287 	(a) += (b);
288 
289 #endif
290 
291 static const unsigned char PADDING[56] = {
292 	0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
293 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
294 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
295 };
296 
297 #if ARCH_LITTLE_ENDIAN
298 
299 #define MD5_swap(dst, src, count)
300 
301 #else
302 
303 #define MD5_swap(dst, src, count) \
304 { \
305 	MD5_word *dptr = (dst), *sptr = (src); \
306 	int loop_count = (count); \
307 	MD5_word mask = OOFFOOFF; \
308 	do { \
309 		MD5_word tmp = *sptr++; \
310 		ROTATE_LEFT(tmp, 16); \
311 		*dptr++ = ((tmp & mask) << 8) | ((tmp >> 8) & mask); \
312 		tmp = *sptr++; \
313 		ROTATE_LEFT(tmp, 16); \
314 		*dptr++ = ((tmp & mask) << 8) | ((tmp >> 8) & mask); \
315 	} while ((loop_count -= 2)); \
316 }
317 
318 #endif
319 
320 #define order				MD5_std_all._order
321 #define pool				MD5_std_all._pool
322 #define block				MD5_std_all._block
323 #define prefix				MD5_std_all.prefix
324 #define prelen				MD5_std_all.prelen
325 
MD5_std_init(struct fmt_main * self)326 void MD5_std_init(struct fmt_main *self)
327 {
328 	int index;
329 	MD5_pool *current;
330 #if MD5_std_mt
331 	int t, n;
332 
333 	// Note, $dynamic_n$ will call here for setup.  If set are !MD5_IMM,
334 	// $dynamic_n$ will NOT be able to use the MD5 functions.
335 	// but since I do not know if this function can be called multiple
336 	// times, I simply added a static, so the init WILL get run, but
337 	// only 1 time.
338 	static int bFirst = 1;
339 	if (!bFirst) return;
340 	bFirst = 0;
341 
342 	if (!MD5_std_all_p) {
343 		n = omp_get_max_threads();
344 		if (n < 1)
345 			n = 1;
346 		if (n > MD5_std_mt_max)
347 			n = MD5_std_mt_max;
348 		MD5_std_min_kpc = n * MD5_N;
349 		{
350 			int max = n * MD5_std_cpt;
351 			while (max > MD5_std_mt_max)
352 				max -= n;
353 			n = max;
354 		}
355 		MD5_std_max_kpc = n * MD5_N;
356 /*
357  * The array of MD5_std_all's is not exactly tiny, but we use mem_alloc_tiny()
358  * for its alignment support and error checking.  We do not need to free() this
359  * memory anyway.
360  */
361 		MD5_std_all_p = mem_alloc_tiny(n * MD5_std_all_size,
362 		    MEM_ALIGN_PAGE);
363 		MD5_std_nt = n;
364 	}
365 #endif
366 
367 	for_each_t(MD5_std_nt) {
368 #if !MD5_IMM
369 		MD5_std_all.data = MD5_data_init;
370 #endif
371 
372 		current = pool;
373 		for (index = 0; index < MD5_N; index++) {
374 #define init_line(line, init_even, init_odd) \
375 	order[line][index].even = init_even; \
376 	order[line][index].odd = init_odd;
377 			init_line(0, &current->e.p, &current->o.psp);
378 			init_line(1, &current->e.spp, &current->o.pp);
379 			init_line(2, &current->e.spp, &current->o.psp);
380 			init_line(3, &current->e.pp, &current->o.ps);
381 			init_line(4, &current->e.spp, &current->o.pp);
382 			init_line(5, &current->e.spp, &current->o.psp);
383 			init_line(6, &current->e.pp, &current->o.psp);
384 			init_line(7, &current->e.sp, &current->o.pp);
385 			init_line(8, &current->e.spp, &current->o.psp);
386 			init_line(9, &current->e.pp, &current->o.psp);
387 			init_line(10, &current->e.spp, &current->o.p);
388 			init_line(11, &current->e.spp, &current->o.psp);
389 			init_line(12, &current->e.pp, &current->o.psp);
390 			init_line(13, &current->e.spp, &current->o.pp);
391 			init_line(14, &current->e.sp, &current->o.psp);
392 			init_line(15, &current->e.pp, &current->o.psp);
393 			init_line(16, &current->e.spp, &current->o.pp);
394 			init_line(17, &current->e.spp, &current->o.ps);
395 			init_line(18, &current->e.pp, &current->o.psp);
396 			init_line(19, &current->e.spp, &current->o.pp);
397 			init_line(20, &current->e.spp, &current->o.psp);
398 #undef init_line
399 			current++;
400 		}
401 	}
402 }
403 
404 #if MD5_std_mt
MD5_std_set_salt_for_thread(int t,char * salt)405 static MAYBE_INLINE void MD5_std_set_salt_for_thread(int t, char *salt)
406 #else
407 void MD5_std_set_salt(char *salt)
408 #endif
409 {
410 	int length;
411 
412 	for (length = 0; length < 8 && salt[length]; length++);
413 
414 	memcpy(pool[0].s, salt, pool[0].l.s = length);
415 #if MD5_X2
416 	memcpy(pool[1].s, salt, pool[1].l.s = length);
417 #endif
418 
419 	if (salt[8] == MD5_TYPE_STD) {
420 		prefix = md5_salt_prefix;
421 		prelen = md5_salt_prefix_len;
422 	} else if (salt[8] == MD5_TYPE_APACHE) {
423 		prefix = apr1_salt_prefix;
424 		prelen = apr1_salt_prefix_len;
425 	} else if (salt[8] == MD5_TYPE_AIX) {
426 		prefix = "";
427 		prelen = 0;
428 	}
429 }
430 
431 #if MD5_std_mt
MD5_std_set_salt(char * salt)432 void MD5_std_set_salt(char *salt)
433 {
434 	memcpy(saved_salt, salt, sizeof(saved_salt));
435 	salt_changed = 1;
436 }
437 #endif
438 
MD5_std_set_key(char * key,int index)439 void MD5_std_set_key(char *key, int index)
440 {
441 	int length;
442 	MD5_pool *current;
443 
444 	init_t();
445 
446 	for (length = 0; key[length] && length < 15; length++);
447 	current = &pool[index];
448 
449 	memcpy(current->o.p.b, key, current->l.p = length);
450 	memcpy(&current->o.p.b[length + 16], PADDING, 40 - length);
451 	current->o.p.w[14] = (length + 16) << 3;
452 
453 	memcpy(current->o.pp.b, key, length);
454 	memcpy(&current->o.pp.b[length], key, length);
455 	current->l.pp = length << 1;
456 	memcpy(&current->o.pp.b[current->l.pp + 16], PADDING,
457 		40 - current->l.pp);
458 	current->o.pp.w[14] = (current->l.pp + 16) << 3;
459 
460 	memcpy(&current->e.p.b[16], key, length);
461 	memcpy(&current->e.p.b[16 + length], PADDING, 40 - length);
462 	current->e.p.w[14] = (length + 16) << 3;
463 	MD5_swap(current->e.p.w, current->e.p.w, 14);
464 
465 	memcpy(&current->e.pp.b[16], current->o.pp.b, current->l.pp);
466 	memcpy(&current->e.pp.b[16 + current->l.pp], PADDING,
467 		40 - current->l.pp);
468 	current->e.pp.w[14] = (current->l.pp + 16) << 3;
469 	MD5_swap(current->e.pp.w, current->e.pp.w, 14);
470 
471 	order[1][index].length = current->l.pp;
472 	order[4][index].length = current->l.pp;
473 	order[7][index].length = current->l.pp;
474 	order[10][index].length = length;
475 	order[13][index].length = current->l.pp;
476 	order[16][index].length = current->l.pp;
477 	order[19][index].length = current->l.pp;
478 }
479 
480 #if MD5_ASM
481 extern void MD5_body(MD5_word x[15], MD5_word out[4]);
482 #else
483 
484 /*
485  * x86-64 implies a fairly recent CPU, so presumably its L1 instruction cache
486  * is large enough.
487  */
488 #ifdef __x86_64__
489 #define MAYBE_INLINE_BODY MAYBE_INLINE
490 #else
491 #define MAYBE_INLINE_BODY
492 #endif
493 
494 #if !MD5_X2
495 
496 #if MD5_std_mt
497 #define MD5_body(x, out) \
498 	MD5_body_for_thread(t, x, out)
MD5_body_for_thread(int t,MD5_word x[15],MD5_word out[4])499 void MD5_body_for_thread(int t,
500 	MD5_word x[15], MD5_word out[4])
501 #else
502 void MD5_body(MD5_word x[15], MD5_word out[4])
503 #endif
504 {
505 	MD5_word a, b = Cb, c = Cc, d;
506 
507 /* Round 1 */
508 	a = AC1 + x[0];
509 	ROTATE_LEFT (a, S11); a += b;			/* 1 */
510 	d = (c ^ (a & MASK1)) + x[1] + AC2pCd;
511 	ROTATE_LEFT (d, S12); d += a;			/* 2 */
512 	c = F(d, a, b) + x[2] + AC3pCc;
513 	ROTATE_LEFT(c, S13); c += d;			/* 3 */
514 	b = F(c, d, a) + x[3] + AC4pCb;
515 	ROTATE_LEFT(b, S14); b += c;			/* 4 */
516 	FF (a, b, c, d, x[ 4], S11, AC5);		/* 5 */
517 	FF (d, a, b, c, x[ 5], S12, AC6);		/* 6 */
518 	FF (c, d, a, b, x[ 6], S13, AC7);		/* 7 */
519 	FF (b, c, d, a, x[ 7], S14, AC8);		/* 8 */
520 	FF (a, b, c, d, x[ 8], S11, AC9);		/* 9 */
521 	FF (d, a, b, c, x[ 9], S12, AC10);		/* 10 */
522 	FF (c, d, a, b, x[10], S13, AC11);		/* 11 */
523 	FF (b, c, d, a, x[11], S14, AC12);		/* 12 */
524 	FF (a, b, c, d, x[12], S11, AC13);		/* 13 */
525 	FF (d, a, b, c, x[13], S12, AC14);		/* 14 */
526 	FF (c, d, a, b, x[14], S13, AC15);		/* 15 */
527 	b += F (c, d, a) + AC16;
528 	ROTATE_LEFT (b, S14); b += c;			/* 16 */
529 
530 /* Round 2 */
531 	GG (a, b, c, d, x[ 1], S21, AC17);		/* 17 */
532 	GG (d, a, b, c, x[ 6], S22, AC18);		/* 18 */
533 	GG (c, d, a, b, x[11], S23, AC19);		/* 19 */
534 	GG (b, c, d, a, x[ 0], S24, AC20);		/* 20 */
535 	GG (a, b, c, d, x[ 5], S21, AC21);		/* 21 */
536 	GG (d, a, b, c, x[10], S22, AC22);		/* 22 */
537 	c += G (d, a, b) + AC23;
538 	ROTATE_LEFT (c, S23); c += d;			/* 23 */
539 	GG (b, c, d, a, x[ 4], S24, AC24);		/* 24 */
540 	GG (a, b, c, d, x[ 9], S21, AC25);		/* 25 */
541 	GG (d, a, b, c, x[14], S22, AC26);		/* 26 */
542 	GG (c, d, a, b, x[ 3], S23, AC27);		/* 27 */
543 	GG (b, c, d, a, x[ 8], S24, AC28);		/* 28 */
544 	GG (a, b, c, d, x[13], S21, AC29);		/* 29 */
545 	GG (d, a, b, c, x[ 2], S22, AC30);		/* 30 */
546 	GG (c, d, a, b, x[ 7], S23, AC31);		/* 31 */
547 	GG (b, c, d, a, x[12], S24, AC32);		/* 32 */
548 
549 /* Round 3 */
550 	HH (a, b, c, d, x[ 5], S31, AC33);		/* 33 */
551 	HH2 (d, a, b, c, x[ 8], S32, AC34);		/* 34 */
552 	HH (c, d, a, b, x[11], S33, AC35);		/* 35 */
553 	HH2 (b, c, d, a, x[14], S34, AC36);		/* 36 */
554 	HH (a, b, c, d, x[ 1], S31, AC37);		/* 37 */
555 	HH2 (d, a, b, c, x[ 4], S32, AC38);		/* 38 */
556 	HH (c, d, a, b, x[ 7], S33, AC39);		/* 39 */
557 	HH2 (b, c, d, a, x[10], S34, AC40);		/* 40 */
558 	HH (a, b, c, d, x[13], S31, AC41);		/* 41 */
559 	HH2 (d, a, b, c, x[ 0], S32, AC42);		/* 42 */
560 	HH (c, d, a, b, x[ 3], S33, AC43);		/* 43 */
561 	HH2 (b, c, d, a, x[ 6], S34, AC44);		/* 44 */
562 	HH (a, b, c, d, x[ 9], S31, AC45);		/* 45 */
563 	HH2 (d, a, b, c, x[12], S32, AC46);		/* 46 */
564 	c += H (d, a, b) + AC47;
565 	ROTATE_LEFT (c, S33); c += d;			/* 47 */
566 	HH2 (b, c, d, a, x[ 2], S34, AC48);		/* 48 */
567 
568 /* Round 4 */
569 	II (a, b, c, d, x[ 0], S41, AC49);		/* 49 */
570 	II (d, a, b, c, x[ 7], S42, AC50);		/* 50 */
571 	II (c, d, a, b, x[14], S43, AC51);		/* 51 */
572 	II (b, c, d, a, x[ 5], S44, AC52);		/* 52 */
573 	II (a, b, c, d, x[12], S41, AC53);		/* 53 */
574 	II (d, a, b, c, x[ 3], S42, AC54);		/* 54 */
575 	II (c, d, a, b, x[10], S43, AC55);		/* 55 */
576 	II (b, c, d, a, x[ 1], S44, AC56);		/* 56 */
577 	II (a, b, c, d, x[ 8], S41, AC57);		/* 57 */
578 	d += I (a, b, c) + AC58;
579 	ROTATE_LEFT (d, S42); d += a;			/* 58 */
580 	II (c, d, a, b, x[ 6], S43, AC59);		/* 59 */
581 	II (b, c, d, a, x[13], S44, AC60);		/* 60 */
582 	II (a, b, c, d, x[ 4], S41, AC61);		/* 61 */
583 	II (d, a, b, c, x[11], S42, AC62);		/* 62 */
584 	II (c, d, a, b, x[ 2], S43, AC63);		/* 63 */
585 	II (b, c, d, a, x[ 9], S44, AC64);		/* 64 */
586 
587 	out[0] = Ca + a;
588 	out[1] = Cb + b;
589 	out[2] = Cc + c;
590 	out[3] = Cd + d;
591 }
592 
593 #else
594 
595 #if MD5_std_mt
596 #define MD5_body(x0, x1, out0, out1) \
597 	MD5_body_for_thread(t, x0, x1, out0, out1)
MD5_body_for_thread(int t,MD5_word x0[15],MD5_word x1[15],MD5_word out0[4],MD5_word out1[4])598 void MD5_body_for_thread(int t,
599 	MD5_word x0[15], MD5_word x1[15],
600 	MD5_word out0[4], MD5_word out1[4])
601 #else
602 void MD5_body(MD5_word x0[15], MD5_word x1[15],
603 	MD5_word out0[4], MD5_word out1[4])
604 #endif
605 {
606 	MD5_word a0, b0 = Cb, c0 = Cc, d0;
607 	MD5_word a1, b1, c1, d1;
608 	MD5_word u, v;
609 
610 /* Round 1 */
611 	a0 = (u = AC1) + x0[0];
612 	ROTATE_LEFT (a0, S11); a0 += b0;		/* 1 */
613 	a1 = u + x1[0];
614 	ROTATE_LEFT (a1, S11); a1 += b0;		/* 1 */
615 	d0 = (c0 ^ (a0 & (u = MASK1))) + x0[1] + (v = AC2pCd);
616 	ROTATE_LEFT (d0, S12); d0 += a0;		/* 2 */
617 	d1 = (c0 ^ (a1 & u)) + x1[1] + v;
618 	ROTATE_LEFT (d1, S12); d1 += a1;		/* 2 */
619 	c0 = F(d0, a0, b0) + x0[2] + (u = AC3pCc);
620 	ROTATE_LEFT(c0, S13); c0 += d0;			/* 3 */
621 	c1 = F(d1, a1, b0) + x1[2] + u;
622 	ROTATE_LEFT(c1, S13); c1 += d1;			/* 3 */
623 	b0 = F(c0, d0, a0) + x0[3] + (u = AC4pCb);
624 	ROTATE_LEFT(b0, S14); b0 += c0;			/* 4 */
625 	b1 = F(c1, d1, a1) + x1[3] + u;
626 	ROTATE_LEFT(b1, S14); b1 += c1;			/* 4 */
627 	FF (a0, b0, c0, d0, x0[ 4], S11, (u = AC5));	/* 5 */
628 	FF (a1, b1, c1, d1, x1[ 4], S11, u);		/* 5 */
629 	FF (d0, a0, b0, c0, x0[ 5], S12, (u = AC6));	/* 6 */
630 	FF (d1, a1, b1, c1, x1[ 5], S12, u);		/* 6 */
631 	FF (c0, d0, a0, b0, x0[ 6], S13, (u = AC7));	/* 7 */
632 	FF (c1, d1, a1, b1, x1[ 6], S13, u);		/* 7 */
633 	FF (b0, c0, d0, a0, x0[ 7], S14, (u = AC8));	/* 8 */
634 	FF (b1, c1, d1, a1, x1[ 7], S14, u);		/* 8 */
635 	FF (a0, b0, c0, d0, x0[ 8], S11, (u = AC9));	/* 9 */
636 	FF (a1, b1, c1, d1, x1[ 8], S11, u);		/* 9 */
637 	FF (d0, a0, b0, c0, x0[ 9], S12, (u = AC10));	/* 10 */
638 	FF (d1, a1, b1, c1, x1[ 9], S12, u);		/* 10 */
639 	FF (c0, d0, a0, b0, x0[10], S13, (u = AC11));	/* 11 */
640 	FF (c1, d1, a1, b1, x1[10], S13, u);		/* 11 */
641 	FF (b0, c0, d0, a0, x0[11], S14, (u = AC12));	/* 12 */
642 	FF (b1, c1, d1, a1, x1[11], S14, u);		/* 12 */
643 	FF (a0, b0, c0, d0, x0[12], S11, (u = AC13));	/* 13 */
644 	FF (a1, b1, c1, d1, x1[12], S11, u);		/* 13 */
645 	FF (d0, a0, b0, c0, x0[13], S12, (u = AC14));	/* 14 */
646 	FF (d1, a1, b1, c1, x1[13], S12, u);		/* 14 */
647 	FF (c0, d0, a0, b0, x0[14], S13, (u = AC15));	/* 15 */
648 	FF (c1, d1, a1, b1, x1[14], S13, u);		/* 15 */
649 	b0 += F (c0, d0, a0) + (u = AC16);
650 	ROTATE_LEFT (b0, S14); b0 += c0;		/* 16 */
651 	b1 += F (c1, d1, a1) + u;
652 	ROTATE_LEFT (b1, S14); b1 += c1;		/* 16 */
653 
654 /* Round 2 */
655 	GG (a0, b0, c0, d0, x0[ 1], S21, (u = AC17));	/* 17 */
656 	GG (a1, b1, c1, d1, x1[ 1], S21, u);		/* 17 */
657 	GG (d0, a0, b0, c0, x0[ 6], S22, (u = AC18));	/* 18 */
658 	GG (d1, a1, b1, c1, x1[ 6], S22, u);		/* 18 */
659 	GG (c0, d0, a0, b0, x0[11], S23, (u = AC19));	/* 19 */
660 	GG (c1, d1, a1, b1, x1[11], S23, u);		/* 19 */
661 	GG (b0, c0, d0, a0, x0[ 0], S24, (u = AC20));	/* 20 */
662 	GG (b1, c1, d1, a1, x1[ 0], S24, u);		/* 20 */
663 	GG (a0, b0, c0, d0, x0[ 5], S21, (u = AC21));	/* 21 */
664 	GG (a1, b1, c1, d1, x1[ 5], S21, u);		/* 21 */
665 	GG (d0, a0, b0, c0, x0[10], S22, (u = AC22));	/* 22 */
666 	GG (d1, a1, b1, c1, x1[10], S22, u);		/* 22 */
667 	c0 += G (d0, a0, b0) + (u = AC23);
668 	ROTATE_LEFT (c0, S23); c0 += d0;		/* 23 */
669 	c1 += G (d1, a1, b1) + u;
670 	ROTATE_LEFT (c1, S23); c1 += d1;		/* 23 */
671 	GG (b0, c0, d0, a0, x0[ 4], S24, (u = AC24));	/* 24 */
672 	GG (b1, c1, d1, a1, x1[ 4], S24, u);		/* 24 */
673 	GG (a0, b0, c0, d0, x0[ 9], S21, (u = AC25));	/* 25 */
674 	GG (a1, b1, c1, d1, x1[ 9], S21, u);		/* 25 */
675 	GG (d0, a0, b0, c0, x0[14], S22, (u = AC26));	/* 26 */
676 	GG (d1, a1, b1, c1, x1[14], S22, u);		/* 26 */
677 	GG (c0, d0, a0, b0, x0[ 3], S23, (u = AC27));	/* 27 */
678 	GG (c1, d1, a1, b1, x1[ 3], S23, u);		/* 27 */
679 	GG (b0, c0, d0, a0, x0[ 8], S24, (u = AC28));	/* 28 */
680 	GG (b1, c1, d1, a1, x1[ 8], S24, u);		/* 28 */
681 	GG (a0, b0, c0, d0, x0[13], S21, (u = AC29));	/* 29 */
682 	GG (a1, b1, c1, d1, x1[13], S21, u);		/* 29 */
683 	GG (d0, a0, b0, c0, x0[ 2], S22, (u = AC30));	/* 30 */
684 	GG (d1, a1, b1, c1, x1[ 2], S22, u);		/* 30 */
685 	GG (c0, d0, a0, b0, x0[ 7], S23, (u = AC31));	/* 31 */
686 	GG (c1, d1, a1, b1, x1[ 7], S23, u);		/* 31 */
687 	GG (b0, c0, d0, a0, x0[12], S24, (u = AC32));	/* 32 */
688 	GG (b1, c1, d1, a1, x1[12], S24, u);		/* 32 */
689 
690 /* Round 3 */
691 	HH (a0, b0, c0, d0, x0[ 5], S31, (u = AC33));	/* 33 */
692 	HH (a1, b1, c1, d1, x1[ 5], S31, u);		/* 33 */
693 	HH (d0, a0, b0, c0, x0[ 8], S32, (u = AC34));	/* 34 */
694 	HH (d1, a1, b1, c1, x1[ 8], S32, u);		/* 34 */
695 	HH (c0, d0, a0, b0, x0[11], S33, (u = AC35));	/* 35 */
696 	HH (c1, d1, a1, b1, x1[11], S33, u);		/* 35 */
697 	HH (b0, c0, d0, a0, x0[14], S34, (u = AC36));	/* 36 */
698 	HH (b1, c1, d1, a1, x1[14], S34, u);		/* 36 */
699 	HH (a0, b0, c0, d0, x0[ 1], S31, (u = AC37));	/* 37 */
700 	HH (a1, b1, c1, d1, x1[ 1], S31, u);		/* 37 */
701 	HH (d0, a0, b0, c0, x0[ 4], S32, (u = AC38));	/* 38 */
702 	HH (d1, a1, b1, c1, x1[ 4], S32, u);		/* 38 */
703 	HH (c0, d0, a0, b0, x0[ 7], S33, (u = AC39));	/* 39 */
704 	HH (c1, d1, a1, b1, x1[ 7], S33, u);		/* 39 */
705 	HH (b0, c0, d0, a0, x0[10], S34, (u = AC40));	/* 40 */
706 	HH (b1, c1, d1, a1, x1[10], S34, u);		/* 40 */
707 	HH (a0, b0, c0, d0, x0[13], S31, (u = AC41));	/* 41 */
708 	HH (a1, b1, c1, d1, x1[13], S31, u);		/* 41 */
709 	HH (d0, a0, b0, c0, x0[ 0], S32, (u = AC42));	/* 42 */
710 	HH (d1, a1, b1, c1, x1[ 0], S32, u);		/* 42 */
711 	HH (c0, d0, a0, b0, x0[ 3], S33, (u = AC43));	/* 43 */
712 	HH (c1, d1, a1, b1, x1[ 3], S33, u);		/* 43 */
713 	HH (b0, c0, d0, a0, x0[ 6], S34, (u = AC44));	/* 44 */
714 	HH (b1, c1, d1, a1, x1[ 6], S34, u);		/* 44 */
715 	HH (a0, b0, c0, d0, x0[ 9], S31, (u = AC45));	/* 45 */
716 	HH (a1, b1, c1, d1, x1[ 9], S31, u);		/* 45 */
717 	HH (d0, a0, b0, c0, x0[12], S32, (u = AC46));	/* 46 */
718 	HH (d1, a1, b1, c1, x1[12], S32, u);		/* 46 */
719 	c0 += H (d0, a0, b0) + (u = AC47);
720 	ROTATE_LEFT (c0, S33); c0 += d0;		/* 47 */
721 	c1 += H (d1, a1, b1) + u;
722 	ROTATE_LEFT (c1, S33); c1 += d1;		/* 47 */
723 	HH (b0, c0, d0, a0, x0[ 2], S34, (u = AC48));	/* 48 */
724 	HH (b1, c1, d1, a1, x1[ 2], S34, u);		/* 48 */
725 
726 /* Round 4 */
727 	II (a0, b0, c0, d0, x0[ 0], S41, (u = AC49));	/* 49 */
728 	II (a1, b1, c1, d1, x1[ 0], S41, u);		/* 49 */
729 	II (d0, a0, b0, c0, x0[ 7], S42, (u = AC50));	/* 50 */
730 	II (d1, a1, b1, c1, x1[ 7], S42, u);		/* 50 */
731 	II (c0, d0, a0, b0, x0[14], S43, (u = AC51));	/* 51 */
732 	II (c1, d1, a1, b1, x1[14], S43, u);		/* 51 */
733 	II (b0, c0, d0, a0, x0[ 5], S44, (u = AC52));	/* 52 */
734 	II (b1, c1, d1, a1, x1[ 5], S44, u);		/* 52 */
735 	II (a0, b0, c0, d0, x0[12], S41, (u = AC53));	/* 53 */
736 	II (a1, b1, c1, d1, x1[12], S41, u);		/* 53 */
737 	II (d0, a0, b0, c0, x0[ 3], S42, (u = AC54));	/* 54 */
738 	II (d1, a1, b1, c1, x1[ 3], S42, u);		/* 54 */
739 	II (c0, d0, a0, b0, x0[10], S43, (u = AC55));	/* 55 */
740 	II (c1, d1, a1, b1, x1[10], S43, u);		/* 55 */
741 	II (b0, c0, d0, a0, x0[ 1], S44, (u = AC56));	/* 56 */
742 	II (b1, c1, d1, a1, x1[ 1], S44, u);		/* 56 */
743 	II (a0, b0, c0, d0, x0[ 8], S41, (u = AC57));	/* 57 */
744 	II (a1, b1, c1, d1, x1[ 8], S41, u);		/* 57 */
745 	d0 += I (a0, b0, c0) + (u = AC58);
746 	ROTATE_LEFT (d0, S42); d0 += a0;		/* 58 */
747 	d1 += I (a1, b1, c1) + u;
748 	ROTATE_LEFT (d1, S42); d1 += a1;		/* 58 */
749 	II (c0, d0, a0, b0, x0[ 6], S43, (u = AC59));	/* 59 */
750 	II (c1, d1, a1, b1, x1[ 6], S43, u);		/* 59 */
751 	II (b0, c0, d0, a0, x0[13], S44, (u = AC60));	/* 60 */
752 	II (b1, c1, d1, a1, x1[13], S44, u);		/* 60 */
753 	II (a0, b0, c0, d0, x0[ 4], S41, (u = AC61));	/* 61 */
754 	II (a1, b1, c1, d1, x1[ 4], S41, u);		/* 61 */
755 	II (d0, a0, b0, c0, x0[11], S42, (u = AC62));	/* 62 */
756 	II (d1, a1, b1, c1, x1[11], S42, u);		/* 62 */
757 	II (c0, d0, a0, b0, x0[ 2], S43, (u = AC63));	/* 63 */
758 	II (c1, d1, a1, b1, x1[ 2], S43, u);		/* 63 */
759 	II (b0, c0, d0, a0, x0[ 9], S44, (u = AC64));	/* 64 */
760 	II (b1, c1, d1, a1, x1[ 9], S44, u);		/* 64 */
761 
762 	out1[3] = Cd + d1;
763 
764 	out0[0] = Ca + a0;
765 	out0[1] = Cb + b0;
766 	out0[2] = Cc + c0;
767 	out0[3] = Cd + d0;
768 
769 	out1[0] = Ca + a1;
770 	out1[1] = Cb + b1;
771 	out1[2] = Cc + c1;
772 }
773 
774 #endif
775 
776 #endif
777 
778 #if MD5_std_mt
MD5_std_crypt_for_thread(int t)779 static MAYBE_INLINE void MD5_std_crypt_for_thread(int t)
780 #else
781 void MD5_std_crypt(int count)
782 #endif
783 {
784 	int length, index, mask;
785 	MD5_pattern *line;
786 #if ARCH_LITTLE_ENDIAN
787 	MD5_word *last0;
788 #endif
789 #if MD5_X2
790 	MD5_pool *key;
791 #if ARCH_LITTLE_ENDIAN
792 	MD5_word *last1;
793 #endif
794 #endif
795 
796 #if MD5_X2
797 	for (index = 0, key = pool; index < MD5_N; index++, key++) {
798 #else
799 #define index	0
800 #define key	pool
801 #endif
802 		memcpy(key->o.ps.b, key->o.p.b, key->l.p);
803 		memcpy(&key->o.ps.b[key->l.p], key->s, key->l.s);
804 		key->l.ps = key->l.p + key->l.s;
805 		memcpy(&key->o.ps.b[key->l.ps + 16], PADDING,
806 			40 - key->l.ps);
807 		key->o.ps.w[14] = (key->l.ps + 16) << 3;
808 
809 		memcpy(key->o.psp.b, key->o.ps.b, key->l.ps);
810 		memcpy(&key->o.psp.b[key->l.ps], key->o.p.b, key->l.p);
811 		key->l.psp = key->l.ps + key->l.p;
812 		memcpy(&key->o.psp.b[key->l.psp + 16], PADDING,
813 			40 - key->l.psp);
814 		key->o.psp.w[14] = (key->l.psp + 16) << 3;
815 
816 		memcpy(&key->e.sp.b[16], key->s, key->l.s);
817 		memcpy(&key->e.sp.b[16 + key->l.s], key->o.p.b,
818 			key->l.p);
819 		memcpy(&key->e.sp.b[16 + key->l.ps], PADDING,
820 			40 - key->l.ps);
821 		key->e.sp.w[14] = (key->l.ps + 16) << 3;
822 		MD5_swap(key->e.sp.w, key->e.sp.w, 14);
823 
824 		memcpy(&key->e.spp.b[16], key->s, key->l.s);
825 		memcpy(&key->e.spp.b[16 + key->l.s], key->o.pp.b,
826 			key->l.pp);
827 		memcpy(&key->e.spp.b[16 + key->l.psp], PADDING,
828 			40 - key->l.psp);
829 		key->e.spp.w[14] = (key->l.psp + 16) << 3;
830 		MD5_swap(key->e.spp.w, key->e.spp.w, 14);
831 
832 		order[0][index].length = key->l.psp;
833 		order[2][index].length = key->l.psp;
834 		order[3][index].length = key->l.ps;
835 		order[5][index].length = key->l.psp;
836 		order[6][index].length = key->l.psp;
837 		order[8][index].length = key->l.psp;
838 		order[9][index].length = key->l.psp;
839 		order[11][index].length = key->l.psp;
840 		order[12][index].length = key->l.psp;
841 		order[14][index].length = key->l.psp;
842 		order[15][index].length = key->l.psp;
843 		order[17][index].length = key->l.ps;
844 		order[18][index].length = key->l.psp;
845 		order[20][index].length = key->l.psp;
846 
847 		memcpy(&block[index], key->o.psp.b, key->l.psp);
848 		memcpy(&block[index].b[key->l.psp], PADDING, 56 - key->l.psp);
849 		block[index].w[14] = key->l.psp << 3;
850 		MD5_swap(block[index].w, block[index].w, 14);
851 #if MD5_X2
852 	}
853 
854 	MD5_body(block[0].w, block[1].w, MD5_out[0], MD5_out[1]);
855 	MD5_swap(MD5_out[0], MD5_out[0], 8);
856 #else
857 	MD5_body(block[0].w, MD5_out[0]);
858 	MD5_swap(MD5_out[0], MD5_out[0], 4);
859 #endif
860 
861 #if MD5_X2
862 	for (index = 0, key = pool; index < MD5_N; index++, key++) {
863 #endif
864 		memcpy(&block[index], key->o.p.b, key->l.p);
865 		memcpy(&block[index].b[key->l.p], prefix, prelen);
866 		memcpy(&block[index].b[key->l.p + prelen], key->s, key->l.s);
867 		memcpy(&block[index].b[key->l.ps + prelen],
868 			MD5_out[index], key->l.p);
869 		length = key->l.psp + prelen;
870 		if ((mask = key->l.p))
871 		do {
872 			block[index].b[length++] =
873 				(mask & 1) ? 0 : key->o.p.b[0];
874 		} while (mask >>= 1);
875 		memcpy(&block[index].b[length], PADDING, 56 - length);
876 		block[index].w[14] = length << 3;
877 		MD5_swap(block[index].w, block[index].w, 14);
878 #if MD5_X2
879 	}
880 #else
881 #undef index
882 #undef key
883 #endif
884 
885 #if MD5_X2
886 	MD5_body(block[0].w, block[1].w,
887 		order[0][0].even->w, order[0][1].even->w);
888 #else
889 	MD5_body(block[0].w, order[0][0].even->w);
890 #endif
891 
892 	index = 500; line = order[0];
893 	do {
894 #if ARCH_LITTLE_ENDIAN
895 #if ARCH_ALLOWS_UNALIGNED
896 #if MD5_X2
897 		MD5_body(line[0].even->w, line[1].even->w,
898 			(MD5_word *)&line[0].odd->b[line[0].length],
899 			(MD5_word *)&line[1].odd->b[line[1].length]);
900 #else
901 		MD5_body(line[0].even->w,
902 			(MD5_word *)&line[0].odd->b[line[0].length]);
903 #endif
904 #else
905 #if MD5_X2
906 		MD5_body(line[0].even->w, line[1].even->w,
907 			MD5_out[0], MD5_out[1]);
908 		memcpy(&line[0].odd->b[line[0].length], MD5_out[0], 16);
909 		memcpy(&line[1].odd->b[line[1].length], MD5_out[1], 16);
910 #else
911 		if (((ARCH_WORD)&line[0].odd->b[line[0].length]) & 3) {
912 			MD5_body(line[0].even->w, MD5_out[0]);
913 			memcpy(&line[0].odd->b[line[0].length],
914 				MD5_out[0], 16);
915 		} else {
916 			MD5_body(line[0].even->w,
917 				(MD5_word *)&line[0].odd->b[line[0].length]);
918 		}
919 #endif
920 #endif
921 		last0 = line[0].odd->w;
922 #if MD5_X2
923 		last1 = line[1].odd->w;
924 		if ((line += 2) > &order[20][MD5_N - 1]) line = order[0];
925 		MD5_body(last0, last1, line[0].even->w, line[1].even->w);
926 #else
927 		if (++line > &order[20][0]) line = order[0];
928 		MD5_body(last0, line[0].even->w);
929 #endif
930 #else
931 #if MD5_X2
932 		MD5_body(line[0].even->w, line[1].even->w,
933 			MD5_out[0], MD5_out[1]);
934 		MD5_swap(MD5_out[0], MD5_out[0], 8);
935 #else
936 		MD5_body(line[0].even->w, MD5_out[0]);
937 		MD5_swap(MD5_out[0], MD5_out[0], 4);
938 #endif
939 		memcpy(&line[0].odd->b[line[0].length], MD5_out[0], 16);
940 #if MD5_X2
941 		memcpy(&line[1].odd->b[line[1].length], MD5_out[1], 16);
942 #endif
943 		MD5_swap(block[0].w, line[0].odd->w, 14);
944 		block[0].w[14] = line[0].odd->w[14];
945 #if MD5_X2
946 		MD5_swap(block[1].w, line[1].odd->w, 14);
947 		block[1].w[14] = line[1].odd->w[14];
948 		if ((line += 2) > &order[20][MD5_N - 1]) line = order[0];
949 		MD5_body(block[0].w, block[1].w,
950 			line[0].even->w, line[1].even->w);
951 #else
952 		if (++line > &order[20][0]) line = order[0];
953 		MD5_body(block[0].w, line[0].even->w);
954 #endif
955 #endif
956 	} while (--index);
957 
958 	memcpy(MD5_out[0], line[0].even, 16);
959 #if MD5_X2
960 	memcpy(MD5_out[1], line[1].even, 16);
961 #endif
962 }
963 
964 #if MD5_std_mt
MD5_std_crypt(int count)965 void MD5_std_crypt(int count)
966 {
967 #if MD5_std_mt
968 	int t, n = (count + (MD5_N - 1)) / MD5_N;
969 #endif
970 
971 #ifdef _OPENMP
972 #pragma omp parallel for default(none) private(t) shared(n, salt_changed, saved_salt)
973 #endif
974 	for_each_t(n) {
975 /*
976  * We could move the salt_changed check out of the parallel region (and have
977  * two specialized parallel regions instead), but MD5_std_crypt_for_thread()
978  * does so much work that the salt_changed check is negligible.
979  */
980 		if (salt_changed)
981 			MD5_std_set_salt_for_thread(t, saved_salt);
982 		MD5_std_crypt_for_thread(t);
983 	}
984 
985 	salt_changed = 0;
986 }
987 #endif
988 
MD5_std_get_salt(char * ciphertext)989 char *MD5_std_get_salt(char *ciphertext)
990 {
991 	static char out[9];
992 	char *p, *q;
993 	int i;
994 
995 	if (!strncmp(ciphertext, apr1_salt_prefix, apr1_salt_prefix_len)) {
996 		out[8] = MD5_TYPE_APACHE;
997 		p = ciphertext + apr1_salt_prefix_len;
998 	} else
999 	if (!strncmp(ciphertext, smd5_salt_prefix, smd5_salt_prefix_len)) {
1000 		out[8] = MD5_TYPE_AIX;
1001 		p = ciphertext + smd5_salt_prefix_len;
1002 	} else {
1003 		out[8] = MD5_TYPE_STD;
1004 		p = ciphertext + md5_salt_prefix_len;
1005 	}
1006 	q = out;
1007 	for (i = 0; *p != '$' && i < 8; i++)
1008 		*q++ = *p++;
1009 	while (i++ < 8)
1010 		*q++ = 0;
1011 
1012 	return out;
1013 }
1014 
1015 #define TO_BINARY(b1, b2, b3) \
1016 	value = \
1017 		(MD5_word)atoi64[ARCH_INDEX(pos[0])] | \
1018 		((MD5_word)atoi64[ARCH_INDEX(pos[1])] << 6) | \
1019 		((MD5_word)atoi64[ARCH_INDEX(pos[2])] << 12) | \
1020 		((MD5_word)atoi64[ARCH_INDEX(pos[3])] << 18); \
1021 	pos += 4; \
1022 	out.b[b1] = value >> 16; \
1023 	out.b[b2] = value >> 8; \
1024 	out.b[b3] = value;
1025 
MD5_std_get_binary(char * ciphertext)1026 MD5_word *MD5_std_get_binary(char *ciphertext)
1027 {
1028 	static union {
1029 		MD5_binary w;
1030 		char b[16];
1031 	} out;
1032 	char *pos;
1033 	MD5_word value;
1034 
1035 	pos = ciphertext + md5_salt_prefix_len;
1036 	if (!strncmp(ciphertext, apr1_salt_prefix, apr1_salt_prefix_len) ||
1037 	    !strncmp(ciphertext, smd5_salt_prefix, smd5_salt_prefix_len))
1038 		pos = ciphertext + apr1_salt_prefix_len;
1039 
1040 	while (*pos++ != '$');
1041 
1042 	TO_BINARY(0, 6, 12);
1043 	TO_BINARY(1, 7, 13);
1044 	TO_BINARY(2, 8, 14);
1045 	TO_BINARY(3, 9, 15);
1046 	TO_BINARY(4, 10, 5);
1047 	out.b[11] =
1048 		(MD5_word)atoi64[ARCH_INDEX(pos[0])] |
1049 		((MD5_word)atoi64[ARCH_INDEX(pos[1])] << 6);
1050 
1051 #undef OOFFOOFF
1052 #define OOFFOOFF 0x00ff00ff
1053 	MD5_swap(out.w, out.w, 4);
1054 #undef OOFFOOFF
1055 
1056 	return out.w;
1057 }
1058