1 /* MSCASH patch for john (performance improvement)
2 *
3 * Modified for utf-8 support by magnum in 2011, same terms as below
4 *
5 * Written by Alain Espinosa <alainesp at gmail.com> in 2007. No copyright
6 * is claimed, and the software is hereby placed in the public domain.
7 * In case this attempt to disclaim copyright and place the software in the
8 * public domain is deemed null and void, then the software is
9 * Copyright (c) 2007 Alain Espinosa and it is hereby released to the
10 * general public under the following terms:
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted.
14 *
15 * There's ABSOLUTELY NO WARRANTY, express or implied.
16 *
17 * (This is a heavily cut-down "BSD license".)
18 */
19
20 #if FMT_EXTERNS_H
21 extern struct fmt_main fmt_mscash;
22 #elif FMT_REGISTERS_H
23 john_register_one(&fmt_mscash);
24 #else
25
26 #include <string.h>
27
28 #ifdef _OPENMP
29 #include <omp.h>
30 #endif
31
32 #include "arch.h"
33 #include "misc.h"
34 #include "memory.h"
35 #include "common.h"
36 #include "formats.h"
37 #include "unicode.h"
38 #include "options.h"
39 #include "loader.h"
40 #include "johnswap.h"
41 #include "mscash_common.h"
42
43 #define FORMAT_LABEL "mscash"
44 #define FORMAT_NAME "MS Cache Hash (DCC)"
45 #define ALGORITHM_NAME "MD4 32/" ARCH_BITS_STR
46
47 #define PLAINTEXT_LENGTH 27
48 #define SALT_SIZE (11*4)
49
50 #ifndef OMP_SCALE
51 #define OMP_SCALE 2 // Tuned w/ MKPC for core i7
52 #endif
53
54 #define MIN_KEYS_PER_CRYPT 1
55 #define MAX_KEYS_PER_CRYPT 512
56
57 static unsigned int *ms_buffer1x;
58 static unsigned int *output1x;
59 static unsigned int *crypt_out;
60 static unsigned int *last;
61 static unsigned int *last_i;
62
63 static unsigned int *salt_buffer;
64 static unsigned int new_key;
65
66 //Init values
67 #define INIT_A 0x67452301
68 #define INIT_B 0xefcdab89
69 #define INIT_C 0x98badcfe
70 #define INIT_D 0x10325476
71
72 #define SQRT_2 0x5a827999
73 #define SQRT_3 0x6ed9eba1
74
75 static void set_key_utf8(char *_key, int index);
76 static void set_key_encoding(char *_key, int index);
77 struct fmt_main fmt_mscash;
78
79 #if !ARCH_LITTLE_ENDIAN
swap(unsigned int * x,int count)80 inline static void swap(unsigned int *x, int count)
81 {
82 while (count--) {
83 *x = JOHNSWAP(*x);
84 x++;
85 }
86 }
87 #endif
88
init(struct fmt_main * self)89 static void init(struct fmt_main *self)
90 {
91 omp_autotune(self, OMP_SCALE);
92
93 ms_buffer1x = mem_calloc(sizeof(ms_buffer1x[0]), 16*fmt_mscash.params.max_keys_per_crypt);
94 output1x = mem_calloc(sizeof(output1x[0]) , 4*fmt_mscash.params.max_keys_per_crypt);
95 crypt_out = mem_calloc(sizeof(crypt_out[0]) , 4*fmt_mscash.params.max_keys_per_crypt);
96 last = mem_calloc(sizeof(last[0]) , 4*fmt_mscash.params.max_keys_per_crypt);
97 last_i = mem_calloc(sizeof(last_i[0]) , fmt_mscash.params.max_keys_per_crypt);
98
99 new_key=1;
100
101 mscash1_adjust_tests(self, options.target_enc, PLAINTEXT_LENGTH,
102 set_key_utf8, set_key_encoding);
103 }
104
done(void)105 static void done(void)
106 {
107 MEM_FREE(last_i);
108 MEM_FREE(last);
109 MEM_FREE(crypt_out);
110 MEM_FREE(output1x);
111 MEM_FREE(ms_buffer1x);
112 }
113
set_salt(void * salt)114 static void set_salt(void *salt)
115 {
116 salt_buffer=salt;
117 }
118
get_salt(char * ciphertext)119 static void *get_salt(char *ciphertext)
120 {
121 unsigned char input[19*3+1];
122 int i, utf16len;
123 char *lasth = strrchr(ciphertext, '#');
124 union {
125 UTF16 u16[22];
126 uint32_t w[11];
127 } static out;
128
129 memset(out.u16, 0, sizeof(out.u16));
130
131 ciphertext += FORMAT_TAG_LEN;
132
133 for (i = 0; &ciphertext[i] < lasth; i++)
134 input[i] = ciphertext[i];
135 input[i] = 0;
136
137 utf16len = enc_to_utf16(out.u16, 19, input, i);
138 if (utf16len < 0)
139 utf16len = strlen16(out.u16);
140
141 #if ARCH_LITTLE_ENDIAN
142 out.u16[utf16len] = 0x80;
143 #else
144 out.u16[utf16len] = 0x8000;
145 swap(out.w, (i>>1)+1);
146 #endif
147
148 out.w[10] = (8 + utf16len) << 4;
149
150 // dump_stuff(out.w, 44);
151
152 return out.u16;
153 }
154
get_binary(char * ciphertext)155 static void *get_binary(char *ciphertext)
156 {
157 static unsigned int out[BINARY_SIZE/sizeof(unsigned int)];
158 unsigned int i=0;
159 unsigned int temp;
160 unsigned int *salt=fmt_mscash.methods.salt(ciphertext);
161
162 /* We need to allow salt containing '#' so we search backwards */
163 ciphertext = strrchr(ciphertext, '#') + 1;
164
165 for (; i<4 ;i++)
166 {
167 temp = ((unsigned int)(atoi16[ARCH_INDEX(ciphertext[i*8+0])]))<<4;
168 temp |= ((unsigned int)(atoi16[ARCH_INDEX(ciphertext[i*8+1])]));
169
170 temp |= ((unsigned int)(atoi16[ARCH_INDEX(ciphertext[i*8+2])]))<<12;
171 temp |= ((unsigned int)(atoi16[ARCH_INDEX(ciphertext[i*8+3])]))<<8;
172
173 temp |= ((unsigned int)(atoi16[ARCH_INDEX(ciphertext[i*8+4])]))<<20;
174 temp |= ((unsigned int)(atoi16[ARCH_INDEX(ciphertext[i*8+5])]))<<16;
175
176 temp |= ((unsigned int)(atoi16[ARCH_INDEX(ciphertext[i*8+6])]))<<28;
177 temp |= ((unsigned int)(atoi16[ARCH_INDEX(ciphertext[i*8+7])]))<<24;
178
179 out[i]=temp;
180 }
181
182 out[0] -= INIT_A;
183 out[1] -= INIT_B;
184 out[2] -= INIT_C;
185 out[3] -= INIT_D;
186
187 // Reversed b += (c ^ d ^ a) + salt_buffer[11] + SQRT_3; b = (b << 15) | (b >> 17);
188 out[1] = (out[1] >> 15) | (out[1] << 17);
189 out[1] -= SQRT_3 + (out[2] ^ out[3] ^ out[0]);
190 // Reversed c += (d ^ a ^ b) + salt_buffer[3] + SQRT_3; c = (c << 11) | (c >> 21);
191 out[2] = (out[2] << 21) | (out[2] >> 11);
192 out[2]-= SQRT_3 + (out[3] ^ out[0] ^ out[1]) + salt[3];
193 // Reversed d += (a ^ b ^ c) + salt_buffer[7] + SQRT_3; d = (d << 9 ) | (d >> 23);
194 out[3] = (out[3] << 23) | (out[3] >> 9);
195 out[3] -= SQRT_3 + (out[0] ^ out[1] ^ out[2]) + salt[7];
196 //+ SQRT_3; d = (d << 9 ) | (d >> 23);
197 out[3]=(out[3] << 23 ) | (out[3] >> 9);
198 out[3]-=SQRT_3;
199
200 return out;
201 }
202
203
binary_hash_0(void * binary)204 static int binary_hash_0(void *binary) { return ((unsigned int*)binary)[3] & PH_MASK_0; }
binary_hash_1(void * binary)205 static int binary_hash_1(void *binary) { return ((unsigned int*)binary)[3] & PH_MASK_1; }
binary_hash_2(void * binary)206 static int binary_hash_2(void *binary) { return ((unsigned int*)binary)[3] & PH_MASK_2; }
binary_hash_3(void * binary)207 static int binary_hash_3(void *binary) { return ((unsigned int*)binary)[3] & PH_MASK_3; }
binary_hash_4(void * binary)208 static int binary_hash_4(void *binary) { return ((unsigned int*)binary)[3] & PH_MASK_4; }
binary_hash_5(void * binary)209 static int binary_hash_5(void *binary) { return ((unsigned int*)binary)[3] & PH_MASK_5; }
binary_hash_6(void * binary)210 static int binary_hash_6(void *binary) { return ((unsigned int*)binary)[3] & PH_MASK_6; }
211
get_hash_0(int index)212 static int get_hash_0(int index) { return output1x[4*index+3] & PH_MASK_0; }
get_hash_1(int index)213 static int get_hash_1(int index) { return output1x[4*index+3] & PH_MASK_1; }
get_hash_2(int index)214 static int get_hash_2(int index) { return output1x[4*index+3] & PH_MASK_2; }
get_hash_3(int index)215 static int get_hash_3(int index) { return output1x[4*index+3] & PH_MASK_3; }
get_hash_4(int index)216 static int get_hash_4(int index) { return output1x[4*index+3] & PH_MASK_4; }
get_hash_5(int index)217 static int get_hash_5(int index) { return output1x[4*index+3] & PH_MASK_5; }
get_hash_6(int index)218 static int get_hash_6(int index) { return output1x[4*index+3] & PH_MASK_6; }
219
nt_hash(int count)220 static void nt_hash(int count)
221 {
222 int i;
223
224 #if defined(_OPENMP)
225 #pragma omp parallel for default(none) private(i) shared(count, ms_buffer1x, crypt_out, last)
226 #endif
227 for (i = 0; i < count; i++) {
228 unsigned int a;
229 unsigned int b;
230 unsigned int c;
231 unsigned int d;
232
233 /* Round 1 */
234 a = 0xFFFFFFFF + ms_buffer1x[16*i+0];a = (a << 3 ) | (a >> 29);
235 d = INIT_D + (INIT_C ^ (a & 0x77777777)) + ms_buffer1x[16*i+1];d = (d << 7 ) | (d >> 25);
236 c = INIT_C + (INIT_B ^ (d & (a ^ INIT_B)))+ ms_buffer1x[16*i+2];c = (c << 11) | (c >> 21);
237 b = INIT_B + (a ^ (c & (d ^ a))) + ms_buffer1x[16*i+3];b = (b << 19) | (b >> 13);
238
239 a += (d ^ (b & (c ^ d))) + ms_buffer1x[16*i+4] ;a = (a << 3 ) | (a >> 29);
240 d += (c ^ (a & (b ^ c))) + ms_buffer1x[16*i+5] ;d = (d << 7 ) | (d >> 25);
241 c += (b ^ (d & (a ^ b))) + ms_buffer1x[16*i+6] ;c = (c << 11) | (c >> 21);
242 b += (a ^ (c & (d ^ a))) + ms_buffer1x[16*i+7] ;b = (b << 19) | (b >> 13);
243
244 a += (d ^ (b & (c ^ d))) + ms_buffer1x[16*i+8] ;a = (a << 3 ) | (a >> 29);
245 d += (c ^ (a & (b ^ c))) + ms_buffer1x[16*i+9] ;d = (d << 7 ) | (d >> 25);
246 c += (b ^ (d & (a ^ b))) + ms_buffer1x[16*i+10] ;c = (c << 11) | (c >> 21);
247 b += (a ^ (c & (d ^ a))) + ms_buffer1x[16*i+11] ;b = (b << 19) | (b >> 13);
248
249 a += (d ^ (b & (c ^ d))) + ms_buffer1x[16*i+12] ;a = (a << 3 ) | (a >> 29);
250 d += (c ^ (a & (b ^ c))) + ms_buffer1x[16*i+13] ;d = (d << 7 ) | (d >> 25);
251 c += (b ^ (d & (a ^ b))) + ms_buffer1x[16*i+14] ;c = (c << 11) | (c >> 21);
252 b += (a ^ (c & (d ^ a)))/*+ms_buffer1x[16*i+15]*/;b = (b << 19) | (b >> 13);
253
254 /* Round 2 */
255 a += ((b & (c | d)) | (c & d)) + ms_buffer1x[16*i+0] + SQRT_2; a = (a << 3 ) | (a >> 29);
256 d += ((a & (b | c)) | (b & c)) + ms_buffer1x[16*i+4] + SQRT_2; d = (d << 5 ) | (d >> 27);
257 c += ((d & (a | b)) | (a & b)) + ms_buffer1x[16*i+8] + SQRT_2; c = (c << 9 ) | (c >> 23);
258 b += ((c & (d | a)) | (d & a)) + ms_buffer1x[16*i+12] + SQRT_2; b = (b << 13) | (b >> 19);
259
260 a += ((b & (c | d)) | (c & d)) + ms_buffer1x[16*i+1] + SQRT_2; a = (a << 3 ) | (a >> 29);
261 d += ((a & (b | c)) | (b & c)) + ms_buffer1x[16*i+5] + SQRT_2; d = (d << 5 ) | (d >> 27);
262 c += ((d & (a | b)) | (a & b)) + ms_buffer1x[16*i+9] + SQRT_2; c = (c << 9 ) | (c >> 23);
263 b += ((c & (d | a)) | (d & a)) + ms_buffer1x[16*i+13] + SQRT_2; b = (b << 13) | (b >> 19);
264
265 a += ((b & (c | d)) | (c & d)) + ms_buffer1x[16*i+2] + SQRT_2; a = (a << 3 ) | (a >> 29);
266 d += ((a & (b | c)) | (b & c)) + ms_buffer1x[16*i+6] + SQRT_2; d = (d << 5 ) | (d >> 27);
267 c += ((d & (a | b)) | (a & b)) + ms_buffer1x[16*i+10] + SQRT_2; c = (c << 9 ) | (c >> 23);
268 b += ((c & (d | a)) | (d & a)) + ms_buffer1x[16*i+14] + SQRT_2; b = (b << 13) | (b >> 19);
269
270 a += ((b & (c | d)) | (c & d)) + ms_buffer1x[16*i+3] + SQRT_2; a = (a << 3 ) | (a >> 29);
271 d += ((a & (b | c)) | (b & c)) + ms_buffer1x[16*i+7] + SQRT_2; d = (d << 5 ) | (d >> 27);
272 c += ((d & (a | b)) | (a & b)) + ms_buffer1x[16*i+11] + SQRT_2; c = (c << 9 ) | (c >> 23);
273 b += ((c & (d | a)) | (d & a))/*+ms_buffer1x[16*i+15]*/+SQRT_2; b = (b << 13) | (b >> 19);
274
275 /* Round 3 */
276 a += (b ^ c ^ d) + ms_buffer1x[16*i+0] + SQRT_3; a = (a << 3 ) | (a >> 29);
277 d += (a ^ b ^ c) + ms_buffer1x[16*i+8] + SQRT_3; d = (d << 9 ) | (d >> 23);
278 c += (d ^ a ^ b) + ms_buffer1x[16*i+4] + SQRT_3; c = (c << 11) | (c >> 21);
279 b += (c ^ d ^ a) + ms_buffer1x[16*i+12] + SQRT_3; b = (b << 15) | (b >> 17);
280
281 a += (b ^ c ^ d) + ms_buffer1x[16*i+2] + SQRT_3; a = (a << 3 ) | (a >> 29);
282 d += (a ^ b ^ c) + ms_buffer1x[16*i+10] + SQRT_3; d = (d << 9 ) | (d >> 23);
283 c += (d ^ a ^ b) + ms_buffer1x[16*i+6] + SQRT_3; c = (c << 11) | (c >> 21);
284 b += (c ^ d ^ a) + ms_buffer1x[16*i+14] + SQRT_3; b = (b << 15) | (b >> 17);
285
286 a += (b ^ c ^ d) + ms_buffer1x[16*i+1] + SQRT_3; a = (a << 3 ) | (a >> 29);
287 d += (a ^ b ^ c) + ms_buffer1x[16*i+9] + SQRT_3; d = (d << 9 ) | (d >> 23);
288 c += (d ^ a ^ b) + ms_buffer1x[16*i+5] + SQRT_3; c = (c << 11) | (c >> 21);
289 b += (c ^ d ^ a) + ms_buffer1x[16*i+13] + SQRT_3; b = (b << 15) | (b >> 17);
290
291 a += (b ^ c ^ d) + ms_buffer1x[16*i+3] + SQRT_3; a = (a << 3 ) | (a >> 29);
292 d += (a ^ b ^ c) + ms_buffer1x[16*i+11] + SQRT_3; d = (d << 9 ) | (d >> 23);
293 c += (d ^ a ^ b) + ms_buffer1x[16*i+7] + SQRT_3; c = (c << 11) | (c >> 21);
294 b += (c ^ d ^ a) /*+ ms_buffer1x[16*i+15] */+ SQRT_3; b = (b << 15) | (b >> 17);
295
296 crypt_out[4*i+0] = a + INIT_A;
297 crypt_out[4*i+1] = b + INIT_B;
298 crypt_out[4*i+2] = c + INIT_C;
299 crypt_out[4*i+3] = d + INIT_D;
300
301 //Another MD4_crypt for the salt
302 /* Round 1 */
303 a= 0xFFFFFFFF +crypt_out[4*i+0]; a=(a<<3 )|(a>>29);
304 d=INIT_D + ( INIT_C ^ ( a & 0x77777777)) +crypt_out[4*i+1]; d=(d<<7 )|(d>>25);
305 c=INIT_C + ( INIT_B ^ ( d & ( a ^ INIT_B))) +crypt_out[4*i+2]; c=(c<<11)|(c>>21);
306 b=INIT_B + ( a ^ ( c & ( d ^ a ))) +crypt_out[4*i+3]; b=(b<<19)|(b>>13);
307
308 last[4*i+0]=a;
309 last[4*i+1]=b;
310 last[4*i+2]=c;
311 last[4*i+3]=d;
312 }
313 }
314
crypt_all(int * pcount,struct db_salt * salt)315 static int crypt_all(int *pcount, struct db_salt *salt)
316 {
317 int count = *pcount;
318 int i;
319
320 if (new_key)
321 {
322 new_key=0;
323 nt_hash(count);
324 }
325
326 #if defined(_OPENMP)
327 #pragma omp parallel for default(none) private(i) shared(count, last, crypt_out, salt_buffer, output1x)
328 #endif
329 for (i = 0; i < count; i++) {
330 unsigned int a;
331 unsigned int b;
332 unsigned int c;
333 unsigned int d;
334
335 a = last[4*i+0];
336 b = last[4*i+1];
337 c = last[4*i+2];
338 d = last[4*i+3];
339
340 a += (d ^ (b & (c ^ d))) + salt_buffer[0] ;a = (a << 3 ) | (a >> 29);
341 d += (c ^ (a & (b ^ c))) + salt_buffer[1] ;d = (d << 7 ) | (d >> 25);
342 c += (b ^ (d & (a ^ b))) + salt_buffer[2] ;c = (c << 11) | (c >> 21);
343 b += (a ^ (c & (d ^ a))) + salt_buffer[3] ;b = (b << 19) | (b >> 13);
344
345 a += (d ^ (b & (c ^ d))) + salt_buffer[4] ;a = (a << 3 ) | (a >> 29);
346 d += (c ^ (a & (b ^ c))) + salt_buffer[5] ;d = (d << 7 ) | (d >> 25);
347 c += (b ^ (d & (a ^ b))) + salt_buffer[6] ;c = (c << 11) | (c >> 21);
348 b += (a ^ (c & (d ^ a))) + salt_buffer[7] ;b = (b << 19) | (b >> 13);
349
350 a += (d ^ (b & (c ^ d))) + salt_buffer[8] ;a = (a << 3 ) | (a >> 29);
351 d += (c ^ (a & (b ^ c))) + salt_buffer[9] ;d = (d << 7 ) | (d >> 25);
352 c += (b ^ (d & (a ^ b))) + salt_buffer[10] ;c = (c << 11) | (c >> 21);
353 b += (a ^ (c & (d ^ a)))/*+salt_buffer[11]*/;b = (b << 19) | (b >> 13);
354
355 /* Round 2 */
356 a += ((b & (c | d)) | (c & d)) + crypt_out[4*i+0] + SQRT_2; a = (a << 3 ) | (a >> 29);
357 d += ((a & (b | c)) | (b & c)) + salt_buffer[0] + SQRT_2; d = (d << 5 ) | (d >> 27);
358 c += ((d & (a | b)) | (a & b)) + salt_buffer[4] + SQRT_2; c = (c << 9 ) | (c >> 23);
359 b += ((c & (d | a)) | (d & a)) + salt_buffer[8] + SQRT_2; b = (b << 13) | (b >> 19);
360
361 a += ((b & (c | d)) | (c & d)) + crypt_out[4*i+1] + SQRT_2; a = (a << 3 ) | (a >> 29);
362 d += ((a & (b | c)) | (b & c)) + salt_buffer[1] + SQRT_2; d = (d << 5 ) | (d >> 27);
363 c += ((d & (a | b)) | (a & b)) + salt_buffer[5] + SQRT_2; c = (c << 9 ) | (c >> 23);
364 b += ((c & (d | a)) | (d & a)) + salt_buffer[9] + SQRT_2; b = (b << 13) | (b >> 19);
365
366 a += ((b & (c | d)) | (c & d)) + crypt_out[4*i+2] + SQRT_2; a = (a << 3 ) | (a >> 29);
367 d += ((a & (b | c)) | (b & c)) + salt_buffer[2] + SQRT_2; d = (d << 5 ) | (d >> 27);
368 c += ((d & (a | b)) | (a & b)) + salt_buffer[6] + SQRT_2; c = (c << 9 ) | (c >> 23);
369 b += ((c & (d | a)) | (d & a)) + salt_buffer[10] + SQRT_2; b = (b << 13) | (b >> 19);
370
371 a += ((b & (c | d)) | (c & d)) + crypt_out[4*i+3] + SQRT_2; a = (a << 3 ) | (a >> 29);
372 d += ((a & (b | c)) | (b & c)) + salt_buffer[3] + SQRT_2; d = (d << 5 ) | (d >> 27);
373 c += ((d & (a | b)) | (a & b)) + salt_buffer[7] + SQRT_2; c = (c << 9 ) | (c >> 23);
374 b += ((c & (d | a)) | (d & a))/*+ salt_buffer[11]*/+ SQRT_2; b = (b << 13) | (b >> 19);
375
376 /* Round 3 */
377 a += (b ^ c ^ d) + crypt_out[4*i+0] + SQRT_3; a = (a << 3 ) | (a >> 29);
378 d += (a ^ b ^ c) + salt_buffer[4] + SQRT_3; d = (d << 9 ) | (d >> 23);
379 c += (d ^ a ^ b) + salt_buffer[0] + SQRT_3; c = (c << 11) | (c >> 21);
380 b += (c ^ d ^ a) + salt_buffer[8] + SQRT_3; b = (b << 15) | (b >> 17);
381
382 a += (b ^ c ^ d) + crypt_out[4*i+2] + SQRT_3; a = (a << 3 ) | (a >> 29);
383 d += (a ^ b ^ c) + salt_buffer[6] + SQRT_3; d = (d << 9 ) | (d >> 23);
384 c += (d ^ a ^ b) + salt_buffer[2] + SQRT_3; c = (c << 11) | (c >> 21);
385 b += (c ^ d ^ a) + salt_buffer[10] + SQRT_3; b = (b << 15) | (b >> 17);
386
387 a += (b ^ c ^ d) + crypt_out[4*i+1] + SQRT_3; a = (a << 3 ) | (a >> 29);
388 d += (a ^ b ^ c) + salt_buffer[5];
389
390 output1x[4*i+0]=a;
391 output1x[4*i+1]=b;
392 output1x[4*i+2]=c;
393 output1x[4*i+3]=d;
394 }
395 return count;
396 }
397
cmp_all(void * binary,int count)398 static int cmp_all(void *binary, int count)
399 {
400 unsigned int i=0;
401 unsigned int d=((unsigned int *)binary)[3];
402
403 for (;i<count;i++)
404 if (d==output1x[i*4+3])
405 return 1;
406
407 return 0;
408 }
409
cmp_one(void * binary,int index)410 static int cmp_one(void * binary, int index)
411 {
412 unsigned int *t=(unsigned int *)binary;
413 unsigned int a=output1x[4*index+0];
414 unsigned int b=output1x[4*index+1];
415 unsigned int c=output1x[4*index+2];
416 unsigned int d=output1x[4*index+3];
417
418 if (d!=t[3])
419 return 0;
420 d+=SQRT_3;d = (d << 9 ) | (d >> 23);
421
422 c += (d ^ a ^ b) + salt_buffer[1] + SQRT_3; c = (c << 11) | (c >> 21);
423 if (c!=t[2])
424 return 0;
425
426 b += (c ^ d ^ a) + salt_buffer[9] + SQRT_3; b = (b << 15) | (b >> 17);
427 if (b!=t[1])
428 return 0;
429
430 a += (b ^ c ^ d) + crypt_out[4*index+3]+ SQRT_3; a = (a << 3 ) | (a >> 29);
431 return (a==t[0]);
432 }
433
cmp_exact(char * source,int index)434 static int cmp_exact(char *source, int index)
435 {
436 // This check is for the unreal case of collisions.
437 // It verifies that the salts are the same.
438 unsigned int *salt=fmt_mscash.methods.salt(source);
439 unsigned int i=0;
440 for (;i<11;i++)
441 if (salt[i]!=salt_buffer[i])
442 return 0;
443 return 1;
444 }
445
446 // This is common code for the SSE/MMX/generic variants of non-UTF8 set_key
set_key_helper(unsigned int * keybuffer,unsigned int xBuf,const unsigned char * key,unsigned int lenStoreOffset,unsigned int * last_length)447 inline static void set_key_helper(unsigned int * keybuffer,
448 unsigned int xBuf,
449 const unsigned char * key,
450 unsigned int lenStoreOffset,
451 unsigned int *last_length)
452 {
453 unsigned int i=0;
454 unsigned int md4_size=0;
455 for (; key[md4_size] && md4_size < PLAINTEXT_LENGTH; i += xBuf, md4_size++)
456 {
457 unsigned int temp;
458 if ((temp = key[++md4_size]))
459 {
460 keybuffer[i] = key[md4_size-1] | (temp << 16);
461 }
462 else
463 {
464 keybuffer[i] = key[md4_size-1] | 0x800000;
465 goto key_cleaning;
466 }
467 }
468 keybuffer[i] = 0x80;
469
470 key_cleaning:
471 i += xBuf;
472 for (;i <= *last_length; i += xBuf)
473 keybuffer[i] = 0;
474
475 *last_length = (md4_size >> 1)+1;
476
477 keybuffer[lenStoreOffset] = md4_size << 4;
478 }
479
set_key(char * _key,int index)480 static void set_key(char *_key, int index)
481 {
482 set_key_helper(&ms_buffer1x[index << 4], 1, (unsigned char *)_key, 14,
483 &last_i[index]);
484 //new password_candidate
485 new_key=1;
486 }
487
488 // UTF-8 conversion right into key buffer
489 // This is common code for the SSE/MMX/generic variants
set_key_helper_utf8(unsigned int * keybuffer,unsigned int xBuf,const UTF8 * source,unsigned int lenStoreOffset,unsigned int * lastlen)490 inline static void set_key_helper_utf8(unsigned int * keybuffer, unsigned int xBuf,
491 const UTF8 * source, unsigned int lenStoreOffset, unsigned int *lastlen)
492 {
493 unsigned int *target = keybuffer;
494 UTF32 chl, chh = 0x80;
495 unsigned int outlen = 0;
496
497 while (*source) {
498 chl = *source;
499 if (chl >= 0xC0) {
500 unsigned int extraBytesToRead = opt_trailingBytesUTF8[chl & 0x3f];
501 switch (extraBytesToRead) {
502 case 3:
503 ++source;
504 if (*source) {
505 chl <<= 6;
506 chl += *source;
507 } else {
508 *lastlen = ((PLAINTEXT_LENGTH >> 1) + 1) * xBuf;
509 return;
510 }
511 case 2:
512 ++source;
513 if (*source) {
514 chl <<= 6;
515 chl += *source;
516 } else {
517 *lastlen = ((PLAINTEXT_LENGTH >> 1) + 1) * xBuf;
518 return;
519 }
520 case 1:
521 ++source;
522 if (*source) {
523 chl <<= 6;
524 chl += *source;
525 } else {
526 *lastlen = ((PLAINTEXT_LENGTH >> 1) + 1) * xBuf;
527 return;
528 }
529 case 0:
530 break;
531 default:
532 *lastlen = ((PLAINTEXT_LENGTH >> 1) + 1) * xBuf;
533 return;
534 }
535 chl -= offsetsFromUTF8[extraBytesToRead];
536 }
537 source++;
538 outlen++;
539 if (chl > UNI_MAX_BMP) {
540 if (outlen == PLAINTEXT_LENGTH) {
541 chh = 0x80;
542 *target = (chh << 16) | chl;
543 target += xBuf;
544 *lastlen = ((PLAINTEXT_LENGTH >> 1) + 1) * xBuf;
545 break;
546 }
547 #define halfBase 0x0010000UL
548 #define halfShift 10
549 #define halfMask 0x3FFUL
550 #define UNI_SUR_HIGH_START (UTF32)0xD800
551 #define UNI_SUR_LOW_START (UTF32)0xDC00
552 chl -= halfBase;
553 chh = (UTF16)((chl & halfMask) + UNI_SUR_LOW_START);;
554 chl = (UTF16)((chl >> halfShift) + UNI_SUR_HIGH_START);
555 outlen++;
556 } else if (*source && outlen < PLAINTEXT_LENGTH) {
557 chh = *source;
558 if (chh >= 0xC0) {
559 unsigned int extraBytesToRead =
560 opt_trailingBytesUTF8[chh & 0x3f];
561 switch (extraBytesToRead) {
562 case 3:
563 ++source;
564 if (*source) {
565 chl <<= 6;
566 chl += *source;
567 } else {
568 *lastlen = ((PLAINTEXT_LENGTH >> 1) + 1) * xBuf;
569 return;
570 }
571 case 2:
572 ++source;
573 if (*source) {
574 chh <<= 6;
575 chh += *source;
576 } else {
577 *lastlen = ((PLAINTEXT_LENGTH >> 1) + 1) * xBuf;
578 return;
579 }
580 case 1:
581 ++source;
582 if (*source) {
583 chh <<= 6;
584 chh += *source;
585 } else {
586 *lastlen = ((PLAINTEXT_LENGTH >> 1) + 1) * xBuf;
587 return;
588 }
589 case 0:
590 break;
591 default:
592 *lastlen = ((PLAINTEXT_LENGTH >> 1) + 1) * xBuf;
593 return;
594 }
595 chh -= offsetsFromUTF8[extraBytesToRead];
596 }
597 source++;
598 outlen++;
599 } else {
600 chh = 0x80;
601 *target = chh << 16 | chl;
602 target += xBuf;
603 break;
604 }
605 *target = chh << 16 | chl;
606 target += xBuf;
607 }
608 if (chh != 0x80 || outlen == 0) {
609 *target = 0x80;
610 target += xBuf;
611 }
612
613 while(target < &keybuffer[*lastlen]) {
614 *target = 0;
615 target += xBuf;
616 }
617
618 *lastlen = ((outlen >> 1) + 1) * xBuf;
619 keybuffer[lenStoreOffset] = outlen << 4;
620 }
621
set_key_utf8(char * _key,int index)622 static void set_key_utf8(char *_key, int index)
623 {
624 set_key_helper_utf8(&ms_buffer1x[index << 4], 1, (UTF8 *)_key, 14,
625 &last_i[index]);
626 //new password_candidate
627 new_key=1;
628 }
629
630 // This is common code for the SSE/MMX/generic variants of non-UTF8 non-ISO-8859-1 set_key
set_key_helper_encoding(unsigned int * keybuffer,unsigned int xBuf,const unsigned char * key,unsigned int lenStoreOffset,unsigned int * last_length)631 inline static void set_key_helper_encoding(unsigned int * keybuffer,
632 unsigned int xBuf,
633 const unsigned char * key,
634 unsigned int lenStoreOffset,
635 unsigned int *last_length)
636 {
637 unsigned int i=0;
638 int md4_size;
639 md4_size = enc_to_utf16( (UTF16 *)keybuffer, PLAINTEXT_LENGTH, (UTF8 *) key, strlen((char*)key));
640 if (md4_size < 0)
641 md4_size = strlen16((UTF16 *)keybuffer);
642
643 #if ARCH_LITTLE_ENDIAN
644 ((UTF16*)keybuffer)[md4_size] = 0x80;
645 #else
646 ((UTF16*)keybuffer)[md4_size] = 0x8000;
647 #endif
648 ((UTF16*)keybuffer)[md4_size+1] = 0;
649 #if !ARCH_LITTLE_ENDIAN
650 ((UTF16*)keybuffer)[md4_size+2] = 0;
651 #endif
652 i = md4_size>>1;
653
654 i += xBuf;
655 for (;i <= *last_length; i += xBuf)
656 keybuffer[i] = 0;
657
658 #if !ARCH_LITTLE_ENDIAN
659 swap(keybuffer, (md4_size>>1)+1);
660 #endif
661
662 *last_length = (md4_size >> 1) + 1;
663
664 keybuffer[lenStoreOffset] = md4_size << 4;
665 }
666
set_key_encoding(char * _key,int index)667 static void set_key_encoding(char *_key, int index)
668 {
669 set_key_helper_encoding(&ms_buffer1x[index << 4], 1, (unsigned char *)_key, 14,
670 &last_i[index]);
671 //new password_candidate
672 new_key=1;
673 }
674
675
676 // Get the key back from the key buffer, from UCS-2 LE
get_key(int index)677 static char *get_key(int index)
678 {
679 static union {
680 UTF16 u16[PLAINTEXT_LENGTH + 1];
681 unsigned int u32[(PLAINTEXT_LENGTH + 1 + 1) / 2];
682 } key;
683 unsigned int * keybuffer = &ms_buffer1x[index << 4];
684 unsigned int md4_size;
685 unsigned int i=0;
686 int len = keybuffer[14] >> 4;
687
688 for (md4_size = 0; md4_size < len; i++, md4_size += 2)
689 {
690 #if ARCH_LITTLE_ENDIAN
691 key.u16[md4_size] = keybuffer[i];
692 key.u16[md4_size+1] = keybuffer[i] >> 16;
693 #else
694 key.u16[md4_size] = keybuffer[i] >> 16;
695 key.u16[md4_size+1] = keybuffer[i];
696 #endif
697 }
698 #if !ARCH_LITTLE_ENDIAN
699 swap(key.u32, md4_size >> 1);
700 #endif
701 key.u16[len] = 0x00;
702
703 return (char *)utf16_to_enc(key.u16);
704 }
705
706 // Public domain hash function by DJ Bernstein (salt is a username)
salt_hash(void * salt)707 static int salt_hash(void *salt)
708 {
709 UTF16 *s = salt;
710 unsigned int hash = 5381;
711
712 while (*s != 0x80)
713 hash = ((hash << 5) + hash) ^ *s++;
714
715 return hash & (SALT_HASH_SIZE - 1);
716 }
717
718 struct fmt_main fmt_mscash = {
719 {
720 FORMAT_LABEL,
721 FORMAT_NAME,
722 ALGORITHM_NAME,
723 BENCHMARK_COMMENT,
724 BENCHMARK_LENGTH,
725 0,
726 PLAINTEXT_LENGTH,
727 BINARY_SIZE,
728 BINARY_ALIGN,
729 SALT_SIZE,
730 SALT_ALIGN,
731 MIN_KEYS_PER_CRYPT,
732 MAX_KEYS_PER_CRYPT,
733 FMT_CASE | FMT_8_BIT | FMT_SPLIT_UNIFIES_CASE | FMT_OMP | FMT_UNICODE | FMT_ENC,
734 { NULL },
735 { FORMAT_TAG },
736 mscash1_common_tests
737 }, {
738 init,
739 done,
740 fmt_default_reset,
741 mscash1_common_prepare,
742 mscash1_common_valid,
743 mscash1_common_split,
744 get_binary, // NOTE, not using the 'common' binary function.
745 get_salt,
746 { NULL },
747 fmt_default_source,
748 {
749 binary_hash_0,
750 binary_hash_1,
751 binary_hash_2,
752 binary_hash_3,
753 binary_hash_4,
754 binary_hash_5,
755 binary_hash_6
756 },
757 salt_hash,
758 NULL,
759 set_salt,
760 set_key,
761 get_key,
762 fmt_default_clear_keys,
763 crypt_all,
764 {
765 get_hash_0,
766 get_hash_1,
767 get_hash_2,
768 get_hash_3,
769 get_hash_4,
770 get_hash_5,
771 get_hash_6
772 },
773 cmp_all,
774 cmp_one,
775 cmp_exact
776 }
777 };
778
779 #endif /* plugin stanza */
780