Add WinZip test.pl and test.sh
[hashcat.git] / src / shared.c
1 /**
2 * Authors.....: Jens Steube <jens.steube@gmail.com>
3 * Gabriele Gristina <matrix@hashcat.net>
4 * magnum <john.magnum@hushmail.com>
5 *
6 * License.....: MIT
7 */
8
9 #ifdef OSX
10 #include <stdio.h>
11 #endif
12
13 #include <shared.h>
14 #include <limits.h>
15
16 /**
17 * basic bit handling
18 */
19
20 u32 is_power_of_2(u32 v)
21 {
22 return (v && !(v & (v - 1)));
23 }
24
25 u32 rotl32 (const u32 a, const u32 n)
26 {
27 return ((a << n) | (a >> (32 - n)));
28 }
29
30 u32 rotr32 (const u32 a, const u32 n)
31 {
32 return ((a >> n) | (a << (32 - n)));
33 }
34
35 u64 rotl64 (const u64 a, const u64 n)
36 {
37 return ((a << n) | (a >> (64 - n)));
38 }
39
40 u64 rotr64 (const u64 a, const u64 n)
41 {
42 return ((a >> n) | (a << (64 - n)));
43 }
44
45 u32 byte_swap_32 (const u32 n)
46 {
47 return (n & 0xff000000) >> 24
48 | (n & 0x00ff0000) >> 8
49 | (n & 0x0000ff00) << 8
50 | (n & 0x000000ff) << 24;
51 }
52
53 u64 byte_swap_64 (const u64 n)
54 {
55 return (n & 0xff00000000000000ULL) >> 56
56 | (n & 0x00ff000000000000ULL) >> 40
57 | (n & 0x0000ff0000000000ULL) >> 24
58 | (n & 0x000000ff00000000ULL) >> 8
59 | (n & 0x00000000ff000000ULL) << 8
60 | (n & 0x0000000000ff0000ULL) << 24
61 | (n & 0x000000000000ff00ULL) << 40
62 | (n & 0x00000000000000ffULL) << 56;
63 }
64
65 /**
66 * ciphers for use on cpu
67 */
68
69 #include "cpu-des.c"
70 #include "cpu-aes.c"
71
72 /**
73 * hashes for use on cpu
74 */
75
76 #include "cpu-md5.c"
77 #include "cpu-sha1.c"
78 #include "cpu-sha256.c"
79
80 /**
81 * logging
82 */
83
84 int last_len = 0;
85
86 void log_final (FILE *fp, const char *fmt, va_list ap)
87 {
88 if (last_len)
89 {
90 fputc ('\r', fp);
91
92 for (int i = 0; i < last_len; i++)
93 {
94 fputc (' ', fp);
95 }
96
97 fputc ('\r', fp);
98 }
99
100 char s[4096] = { 0 };
101
102 int max_len = (int) sizeof (s);
103
104 int len = vsnprintf (s, max_len, fmt, ap);
105
106 if (len > max_len) len = max_len;
107
108 fwrite (s, len, 1, fp);
109
110 fflush (fp);
111
112 last_len = len;
113 }
114
115 void log_out_nn (FILE *fp, const char *fmt, ...)
116 {
117 if (SUPPRESS_OUTPUT) return;
118
119 va_list ap;
120
121 va_start (ap, fmt);
122
123 log_final (fp, fmt, ap);
124
125 va_end (ap);
126 }
127
128 void log_info_nn (const char *fmt, ...)
129 {
130 if (SUPPRESS_OUTPUT) return;
131
132 va_list ap;
133
134 va_start (ap, fmt);
135
136 log_final (stdout, fmt, ap);
137
138 va_end (ap);
139 }
140
141 void log_error_nn (const char *fmt, ...)
142 {
143 if (SUPPRESS_OUTPUT) return;
144
145 va_list ap;
146
147 va_start (ap, fmt);
148
149 log_final (stderr, fmt, ap);
150
151 va_end (ap);
152 }
153
154 void log_out (FILE *fp, const char *fmt, ...)
155 {
156 if (SUPPRESS_OUTPUT) return;
157
158 va_list ap;
159
160 va_start (ap, fmt);
161
162 log_final (fp, fmt, ap);
163
164 va_end (ap);
165
166 fputc ('\n', fp);
167
168 last_len = 0;
169 }
170
171 void log_info (const char *fmt, ...)
172 {
173 if (SUPPRESS_OUTPUT) return;
174
175 va_list ap;
176
177 va_start (ap, fmt);
178
179 log_final (stdout, fmt, ap);
180
181 va_end (ap);
182
183 fputc ('\n', stdout);
184
185 last_len = 0;
186 }
187
188 void log_error (const char *fmt, ...)
189 {
190 if (SUPPRESS_OUTPUT) return;
191
192 fputc ('\n', stderr);
193 fputc ('\n', stderr);
194
195 va_list ap;
196
197 va_start (ap, fmt);
198
199 log_final (stderr, fmt, ap);
200
201 va_end (ap);
202
203 fputc ('\n', stderr);
204 fputc ('\n', stderr);
205
206 last_len = 0;
207 }
208
209 /**
210 * converter
211 */
212
213 u8 int_to_base32 (const u8 c)
214 {
215 static const u8 tbl[0x20] =
216 {
217 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50,
218 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
219 };
220
221 return tbl[c];
222 }
223
224 u8 base32_to_int (const u8 c)
225 {
226 if ((c >= 'A') && (c <= 'Z')) return c - 'A';
227 else if ((c >= '2') && (c <= '7')) return c - '2' + 26;
228
229 return 0;
230 }
231
232 u8 int_to_itoa32 (const u8 c)
233 {
234 static const u8 tbl[0x20] =
235 {
236 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
237 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
238 };
239
240 return tbl[c];
241 }
242
243 u8 itoa32_to_int (const u8 c)
244 {
245 if ((c >= '0') && (c <= '9')) return c - '0';
246 else if ((c >= 'a') && (c <= 'v')) return c - 'a' + 10;
247
248 return 0;
249 }
250
251 u8 int_to_itoa64 (const u8 c)
252 {
253 static const u8 tbl[0x40] =
254 {
255 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44,
256 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54,
257 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a,
258 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a,
259 };
260
261 return tbl[c];
262 }
263
264 u8 itoa64_to_int (const u8 c)
265 {
266 static const u8 tbl[0x100] =
267 {
268 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21,
269 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31,
270 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x00, 0x01,
271 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
272 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a,
273 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x20, 0x21, 0x22, 0x23, 0x24,
274 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34,
275 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x00, 0x01, 0x02, 0x03, 0x04,
276 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14,
277 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24,
278 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34,
279 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x00, 0x01, 0x02, 0x03, 0x04,
280 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14,
281 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24,
282 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34,
283 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x00, 0x01, 0x02, 0x03, 0x04,
284 };
285
286 return tbl[c];
287 }
288
289 u8 int_to_base64 (const u8 c)
290 {
291 static const u8 tbl[0x40] =
292 {
293 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50,
294 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
295 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
296 0x77, 0x78, 0x79, 0x7a, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2b, 0x2f,
297 };
298
299 return tbl[c];
300 }
301
302 u8 base64_to_int (const u8 c)
303 {
304 static const u8 tbl[0x100] =
305 {
306 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
307 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
308 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x3f,
309 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
310 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
311 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00,
312 0x00, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
313 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00,
314 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
315 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
316 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
317 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
318 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
319 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
320 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
321 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
322 };
323
324 return tbl[c];
325 }
326
327 u8 int_to_bf64 (const u8 c)
328 {
329 static const u8 tbl[0x40] =
330 {
331 0x2e, 0x2f, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e,
332 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x61, 0x62, 0x63, 0x64,
333 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74,
334 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
335 };
336
337 return tbl[c];
338 }
339
340 u8 bf64_to_int (const u8 c)
341 {
342 static const u8 tbl[0x100] =
343 {
344 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
345 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
346 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
347 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
348 0x00, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
349 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x00, 0x00, 0x00, 0x00, 0x00,
350 0x00, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a,
351 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x00, 0x00, 0x00, 0x00, 0x00,
352 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
353 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
354 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
355 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
356 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
357 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
358 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
359 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
360 };
361
362 return tbl[c];
363 }
364
365 u8 int_to_lotus64 (const u8 c)
366 {
367 if (c < 10) return '0' + c;
368 else if (c < 36) return 'A' + c - 10;
369 else if (c < 62) return 'a' + c - 36;
370 else if (c == 62) return '+';
371 else if (c == 63) return '/';
372
373 return 0;
374 }
375
376 u8 lotus64_to_int (const u8 c)
377 {
378 if ((c >= '0') && (c <= '9')) return c - '0';
379 else if ((c >= 'A') && (c <= 'Z')) return c - 'A' + 10;
380 else if ((c >= 'a') && (c <= 'z')) return c - 'a' + 36;
381 else if (c == '+') return 62;
382 else if (c == '/') return 63;
383 else
384
385 return 0;
386 }
387
388 int base32_decode (u8 (*f) (const u8), const u8 *in_buf, int in_len, u8 *out_buf)
389 {
390 const u8 *in_ptr = in_buf;
391
392 u8 *out_ptr = out_buf;
393
394 for (int i = 0; i < in_len; i += 8)
395 {
396 const u8 out_val0 = f (in_ptr[0] & 0x7f);
397 const u8 out_val1 = f (in_ptr[1] & 0x7f);
398 const u8 out_val2 = f (in_ptr[2] & 0x7f);
399 const u8 out_val3 = f (in_ptr[3] & 0x7f);
400 const u8 out_val4 = f (in_ptr[4] & 0x7f);
401 const u8 out_val5 = f (in_ptr[5] & 0x7f);
402 const u8 out_val6 = f (in_ptr[6] & 0x7f);
403 const u8 out_val7 = f (in_ptr[7] & 0x7f);
404
405 out_ptr[0] = ((out_val0 << 3) & 0xf8) | ((out_val1 >> 2) & 0x07);
406 out_ptr[1] = ((out_val1 << 6) & 0xc0) | ((out_val2 << 1) & 0x3e) | ((out_val3 >> 4) & 0x01);
407 out_ptr[2] = ((out_val3 << 4) & 0xf0) | ((out_val4 >> 1) & 0x0f);
408 out_ptr[3] = ((out_val4 << 7) & 0x80) | ((out_val5 << 2) & 0x7c) | ((out_val6 >> 3) & 0x03);
409 out_ptr[4] = ((out_val6 << 5) & 0xe0) | ((out_val7 >> 0) & 0x1f);
410
411 in_ptr += 8;
412 out_ptr += 5;
413 }
414
415 for (int i = 0; i < in_len; i++)
416 {
417 if (in_buf[i] != '=') continue;
418
419 in_len = i;
420 }
421
422 int out_len = (in_len * 5) / 8;
423
424 return out_len;
425 }
426
427 int base32_encode (u8 (*f) (const u8), const u8 *in_buf, int in_len, u8 *out_buf)
428 {
429 const u8 *in_ptr = in_buf;
430
431 u8 *out_ptr = out_buf;
432
433 for (int i = 0; i < in_len; i += 5)
434 {
435 const u8 out_val0 = f ( ((in_ptr[0] >> 3) & 0x1f));
436 const u8 out_val1 = f (((in_ptr[0] << 2) & 0x1c) | ((in_ptr[1] >> 6) & 0x03));
437 const u8 out_val2 = f ( ((in_ptr[1] >> 1) & 0x1f));
438 const u8 out_val3 = f (((in_ptr[1] << 4) & 0x10) | ((in_ptr[2] >> 4) & 0x0f));
439 const u8 out_val4 = f (((in_ptr[2] << 1) & 0x1e) | ((in_ptr[3] >> 7) & 0x01));
440 const u8 out_val5 = f ( ((in_ptr[3] >> 2) & 0x1f));
441 const u8 out_val6 = f (((in_ptr[3] << 3) & 0x18) | ((in_ptr[4] >> 5) & 0x07));
442 const u8 out_val7 = f ( ((in_ptr[4] >> 0) & 0x1f));
443
444 out_ptr[0] = out_val0 & 0x7f;
445 out_ptr[1] = out_val1 & 0x7f;
446 out_ptr[2] = out_val2 & 0x7f;
447 out_ptr[3] = out_val3 & 0x7f;
448 out_ptr[4] = out_val4 & 0x7f;
449 out_ptr[5] = out_val5 & 0x7f;
450 out_ptr[6] = out_val6 & 0x7f;
451 out_ptr[7] = out_val7 & 0x7f;
452
453 in_ptr += 5;
454 out_ptr += 8;
455 }
456
457 int out_len = (int) (((0.5 + (float) in_len) * 8) / 5); // ceil (in_len * 8 / 5)
458
459 while (out_len % 8)
460 {
461 out_buf[out_len] = '=';
462
463 out_len++;
464 }
465
466 return out_len;
467 }
468
469 int base64_decode (u8 (*f) (const u8), const u8 *in_buf, int in_len, u8 *out_buf)
470 {
471 const u8 *in_ptr = in_buf;
472
473 u8 *out_ptr = out_buf;
474
475 for (int i = 0; i < in_len; i += 4)
476 {
477 const u8 out_val0 = f (in_ptr[0] & 0x7f);
478 const u8 out_val1 = f (in_ptr[1] & 0x7f);
479 const u8 out_val2 = f (in_ptr[2] & 0x7f);
480 const u8 out_val3 = f (in_ptr[3] & 0x7f);
481
482 out_ptr[0] = ((out_val0 << 2) & 0xfc) | ((out_val1 >> 4) & 0x03);
483 out_ptr[1] = ((out_val1 << 4) & 0xf0) | ((out_val2 >> 2) & 0x0f);
484 out_ptr[2] = ((out_val2 << 6) & 0xc0) | ((out_val3 >> 0) & 0x3f);
485
486 in_ptr += 4;
487 out_ptr += 3;
488 }
489
490 for (int i = 0; i < in_len; i++)
491 {
492 if (in_buf[i] != '=') continue;
493
494 in_len = i;
495 }
496
497 int out_len = (in_len * 6) / 8;
498
499 return out_len;
500 }
501
502 int base64_encode (u8 (*f) (const u8), const u8 *in_buf, int in_len, u8 *out_buf)
503 {
504 const u8 *in_ptr = in_buf;
505
506 u8 *out_ptr = out_buf;
507
508 for (int i = 0; i < in_len; i += 3)
509 {
510 const u8 out_val0 = f ( ((in_ptr[0] >> 2) & 0x3f));
511 const u8 out_val1 = f (((in_ptr[0] << 4) & 0x30) | ((in_ptr[1] >> 4) & 0x0f));
512 const u8 out_val2 = f (((in_ptr[1] << 2) & 0x3c) | ((in_ptr[2] >> 6) & 0x03));
513 const u8 out_val3 = f ( ((in_ptr[2] >> 0) & 0x3f));
514
515 out_ptr[0] = out_val0 & 0x7f;
516 out_ptr[1] = out_val1 & 0x7f;
517 out_ptr[2] = out_val2 & 0x7f;
518 out_ptr[3] = out_val3 & 0x7f;
519
520 in_ptr += 3;
521 out_ptr += 4;
522 }
523
524 int out_len = (int) (((0.5 + (float) in_len) * 8) / 6); // ceil (in_len * 8 / 6)
525
526 while (out_len % 4)
527 {
528 out_buf[out_len] = '=';
529
530 out_len++;
531 }
532
533 return out_len;
534 }
535
536 int is_valid_hex_char (const u8 c)
537 {
538 if ((c >= '0') && (c <= '9')) return 1;
539 if ((c >= 'A') && (c <= 'F')) return 1;
540 if ((c >= 'a') && (c <= 'f')) return 1;
541
542 return 0;
543 }
544
545 u8 hex_convert (const u8 c)
546 {
547 return (c & 15) + (c >> 6) * 9;
548 }
549
550 u8 hex_to_u8 (const u8 hex[2])
551 {
552 u8 v = 0;
553
554 v |= (hex_convert (hex[1]) << 0);
555 v |= (hex_convert (hex[0]) << 4);
556
557 return (v);
558 }
559
560 u32 hex_to_u32 (const u8 hex[8])
561 {
562 u32 v = 0;
563
564 v |= ((u32) hex_convert (hex[7])) << 0;
565 v |= ((u32) hex_convert (hex[6])) << 4;
566 v |= ((u32) hex_convert (hex[5])) << 8;
567 v |= ((u32) hex_convert (hex[4])) << 12;
568 v |= ((u32) hex_convert (hex[3])) << 16;
569 v |= ((u32) hex_convert (hex[2])) << 20;
570 v |= ((u32) hex_convert (hex[1])) << 24;
571 v |= ((u32) hex_convert (hex[0])) << 28;
572
573 return (v);
574 }
575
576 u64 hex_to_u64 (const u8 hex[16])
577 {
578 u64 v = 0;
579
580 v |= ((u64) hex_convert (hex[15]) << 0);
581 v |= ((u64) hex_convert (hex[14]) << 4);
582 v |= ((u64) hex_convert (hex[13]) << 8);
583 v |= ((u64) hex_convert (hex[12]) << 12);
584 v |= ((u64) hex_convert (hex[11]) << 16);
585 v |= ((u64) hex_convert (hex[10]) << 20);
586 v |= ((u64) hex_convert (hex[ 9]) << 24);
587 v |= ((u64) hex_convert (hex[ 8]) << 28);
588 v |= ((u64) hex_convert (hex[ 7]) << 32);
589 v |= ((u64) hex_convert (hex[ 6]) << 36);
590 v |= ((u64) hex_convert (hex[ 5]) << 40);
591 v |= ((u64) hex_convert (hex[ 4]) << 44);
592 v |= ((u64) hex_convert (hex[ 3]) << 48);
593 v |= ((u64) hex_convert (hex[ 2]) << 52);
594 v |= ((u64) hex_convert (hex[ 1]) << 56);
595 v |= ((u64) hex_convert (hex[ 0]) << 60);
596
597 return (v);
598 }
599
600 void bin_to_hex_lower (const u32 v, u8 hex[8])
601 {
602 hex[0] = v >> 28 & 15;
603 hex[1] = v >> 24 & 15;
604 hex[2] = v >> 20 & 15;
605 hex[3] = v >> 16 & 15;
606 hex[4] = v >> 12 & 15;
607 hex[5] = v >> 8 & 15;
608 hex[6] = v >> 4 & 15;
609 hex[7] = v >> 0 & 15;
610
611 u32 add;
612
613 hex[0] += 6; add = ((hex[0] & 0x10) >> 4) * 39; hex[0] += 42 + add;
614 hex[1] += 6; add = ((hex[1] & 0x10) >> 4) * 39; hex[1] += 42 + add;
615 hex[2] += 6; add = ((hex[2] & 0x10) >> 4) * 39; hex[2] += 42 + add;
616 hex[3] += 6; add = ((hex[3] & 0x10) >> 4) * 39; hex[3] += 42 + add;
617 hex[4] += 6; add = ((hex[4] & 0x10) >> 4) * 39; hex[4] += 42 + add;
618 hex[5] += 6; add = ((hex[5] & 0x10) >> 4) * 39; hex[5] += 42 + add;
619 hex[6] += 6; add = ((hex[6] & 0x10) >> 4) * 39; hex[6] += 42 + add;
620 hex[7] += 6; add = ((hex[7] & 0x10) >> 4) * 39; hex[7] += 42 + add;
621 }
622
623 /**
624 * decoder
625 */
626
627 static void AES128_decrypt_cbc (const u32 key[4], const u32 iv[4], const u32 in[16], u32 out[16])
628 {
629 AES_KEY skey;
630
631 AES_set_decrypt_key ((const u8 *) key, 128, &skey);
632
633 u32 _iv[4] = { 0 };
634
635 _iv[0] = iv[0];
636 _iv[1] = iv[1];
637 _iv[2] = iv[2];
638 _iv[3] = iv[3];
639
640 for (int i = 0; i < 16; i += 4)
641 {
642 u32 _in[4] = { 0 };
643 u32 _out[4] = { 0 };
644
645 _in[0] = in[i + 0];
646 _in[1] = in[i + 1];
647 _in[2] = in[i + 2];
648 _in[3] = in[i + 3];
649
650 AES_decrypt (&skey, (const u8 *) _in, (u8 *) _out);
651
652 _out[0] ^= _iv[0];
653 _out[1] ^= _iv[1];
654 _out[2] ^= _iv[2];
655 _out[3] ^= _iv[3];
656
657 out[i + 0] = _out[0];
658 out[i + 1] = _out[1];
659 out[i + 2] = _out[2];
660 out[i + 3] = _out[3];
661
662 _iv[0] = _in[0];
663 _iv[1] = _in[1];
664 _iv[2] = _in[2];
665 _iv[3] = _in[3];
666 }
667 }
668
669 static void juniper_decrypt_hash (char *in, char *out)
670 {
671 // base64 decode
672
673 u8 base64_buf[100] = { 0 };
674
675 base64_decode (base64_to_int, (const u8 *) in, DISPLAY_LEN_MIN_501, base64_buf);
676
677 // iv stuff
678
679 u32 juniper_iv[4] = { 0 };
680
681 memcpy (juniper_iv, base64_buf, 12);
682
683 memcpy (out, juniper_iv, 12);
684
685 // reversed key
686
687 u32 juniper_key[4] = { 0 };
688
689 juniper_key[0] = byte_swap_32 (0xa6707a7e);
690 juniper_key[1] = byte_swap_32 (0x8df91059);
691 juniper_key[2] = byte_swap_32 (0xdea70ae5);
692 juniper_key[3] = byte_swap_32 (0x2f9c2442);
693
694 // AES decrypt
695
696 u32 *in_ptr = (u32 *) (base64_buf + 12);
697 u32 *out_ptr = (u32 *) (out + 12);
698
699 AES128_decrypt_cbc (juniper_key, juniper_iv, in_ptr, out_ptr);
700 }
701
702 void phpass_decode (u8 digest[16], u8 buf[22])
703 {
704 int l;
705
706 l = itoa64_to_int (buf[ 0]) << 0;
707 l |= itoa64_to_int (buf[ 1]) << 6;
708 l |= itoa64_to_int (buf[ 2]) << 12;
709 l |= itoa64_to_int (buf[ 3]) << 18;
710
711 digest[ 0] = (l >> 0) & 0xff;
712 digest[ 1] = (l >> 8) & 0xff;
713 digest[ 2] = (l >> 16) & 0xff;
714
715 l = itoa64_to_int (buf[ 4]) << 0;
716 l |= itoa64_to_int (buf[ 5]) << 6;
717 l |= itoa64_to_int (buf[ 6]) << 12;
718 l |= itoa64_to_int (buf[ 7]) << 18;
719
720 digest[ 3] = (l >> 0) & 0xff;
721 digest[ 4] = (l >> 8) & 0xff;
722 digest[ 5] = (l >> 16) & 0xff;
723
724 l = itoa64_to_int (buf[ 8]) << 0;
725 l |= itoa64_to_int (buf[ 9]) << 6;
726 l |= itoa64_to_int (buf[10]) << 12;
727 l |= itoa64_to_int (buf[11]) << 18;
728
729 digest[ 6] = (l >> 0) & 0xff;
730 digest[ 7] = (l >> 8) & 0xff;
731 digest[ 8] = (l >> 16) & 0xff;
732
733 l = itoa64_to_int (buf[12]) << 0;
734 l |= itoa64_to_int (buf[13]) << 6;
735 l |= itoa64_to_int (buf[14]) << 12;
736 l |= itoa64_to_int (buf[15]) << 18;
737
738 digest[ 9] = (l >> 0) & 0xff;
739 digest[10] = (l >> 8) & 0xff;
740 digest[11] = (l >> 16) & 0xff;
741
742 l = itoa64_to_int (buf[16]) << 0;
743 l |= itoa64_to_int (buf[17]) << 6;
744 l |= itoa64_to_int (buf[18]) << 12;
745 l |= itoa64_to_int (buf[19]) << 18;
746
747 digest[12] = (l >> 0) & 0xff;
748 digest[13] = (l >> 8) & 0xff;
749 digest[14] = (l >> 16) & 0xff;
750
751 l = itoa64_to_int (buf[20]) << 0;
752 l |= itoa64_to_int (buf[21]) << 6;
753
754 digest[15] = (l >> 0) & 0xff;
755 }
756
757 void phpass_encode (u8 digest[16], u8 buf[22])
758 {
759 int l;
760
761 l = (digest[ 0] << 0) | (digest[ 1] << 8) | (digest[ 2] << 16);
762
763 buf[ 0] = int_to_itoa64 (l & 0x3f); l >>= 6;
764 buf[ 1] = int_to_itoa64 (l & 0x3f); l >>= 6;
765 buf[ 2] = int_to_itoa64 (l & 0x3f); l >>= 6;
766 buf[ 3] = int_to_itoa64 (l & 0x3f);
767
768 l = (digest[ 3] << 0) | (digest[ 4] << 8) | (digest[ 5] << 16);
769
770 buf[ 4] = int_to_itoa64 (l & 0x3f); l >>= 6;
771 buf[ 5] = int_to_itoa64 (l & 0x3f); l >>= 6;
772 buf[ 6] = int_to_itoa64 (l & 0x3f); l >>= 6;
773 buf[ 7] = int_to_itoa64 (l & 0x3f);
774
775 l = (digest[ 6] << 0) | (digest[ 7] << 8) | (digest[ 8] << 16);
776
777 buf[ 8] = int_to_itoa64 (l & 0x3f); l >>= 6;
778 buf[ 9] = int_to_itoa64 (l & 0x3f); l >>= 6;
779 buf[10] = int_to_itoa64 (l & 0x3f); l >>= 6;
780 buf[11] = int_to_itoa64 (l & 0x3f);
781
782 l = (digest[ 9] << 0) | (digest[10] << 8) | (digest[11] << 16);
783
784 buf[12] = int_to_itoa64 (l & 0x3f); l >>= 6;
785 buf[13] = int_to_itoa64 (l & 0x3f); l >>= 6;
786 buf[14] = int_to_itoa64 (l & 0x3f); l >>= 6;
787 buf[15] = int_to_itoa64 (l & 0x3f);
788
789 l = (digest[12] << 0) | (digest[13] << 8) | (digest[14] << 16);
790
791 buf[16] = int_to_itoa64 (l & 0x3f); l >>= 6;
792 buf[17] = int_to_itoa64 (l & 0x3f); l >>= 6;
793 buf[18] = int_to_itoa64 (l & 0x3f); l >>= 6;
794 buf[19] = int_to_itoa64 (l & 0x3f);
795
796 l = (digest[15] << 0);
797
798 buf[20] = int_to_itoa64 (l & 0x3f); l >>= 6;
799 buf[21] = int_to_itoa64 (l & 0x3f);
800 }
801
802 void md5crypt_decode (u8 digest[16], u8 buf[22])
803 {
804 int l;
805
806 l = itoa64_to_int (buf[ 0]) << 0;
807 l |= itoa64_to_int (buf[ 1]) << 6;
808 l |= itoa64_to_int (buf[ 2]) << 12;
809 l |= itoa64_to_int (buf[ 3]) << 18;
810
811 digest[ 0] = (l >> 16) & 0xff;
812 digest[ 6] = (l >> 8) & 0xff;
813 digest[12] = (l >> 0) & 0xff;
814
815 l = itoa64_to_int (buf[ 4]) << 0;
816 l |= itoa64_to_int (buf[ 5]) << 6;
817 l |= itoa64_to_int (buf[ 6]) << 12;
818 l |= itoa64_to_int (buf[ 7]) << 18;
819
820 digest[ 1] = (l >> 16) & 0xff;
821 digest[ 7] = (l >> 8) & 0xff;
822 digest[13] = (l >> 0) & 0xff;
823
824 l = itoa64_to_int (buf[ 8]) << 0;
825 l |= itoa64_to_int (buf[ 9]) << 6;
826 l |= itoa64_to_int (buf[10]) << 12;
827 l |= itoa64_to_int (buf[11]) << 18;
828
829 digest[ 2] = (l >> 16) & 0xff;
830 digest[ 8] = (l >> 8) & 0xff;
831 digest[14] = (l >> 0) & 0xff;
832
833 l = itoa64_to_int (buf[12]) << 0;
834 l |= itoa64_to_int (buf[13]) << 6;
835 l |= itoa64_to_int (buf[14]) << 12;
836 l |= itoa64_to_int (buf[15]) << 18;
837
838 digest[ 3] = (l >> 16) & 0xff;
839 digest[ 9] = (l >> 8) & 0xff;
840 digest[15] = (l >> 0) & 0xff;
841
842 l = itoa64_to_int (buf[16]) << 0;
843 l |= itoa64_to_int (buf[17]) << 6;
844 l |= itoa64_to_int (buf[18]) << 12;
845 l |= itoa64_to_int (buf[19]) << 18;
846
847 digest[ 4] = (l >> 16) & 0xff;
848 digest[10] = (l >> 8) & 0xff;
849 digest[ 5] = (l >> 0) & 0xff;
850
851 l = itoa64_to_int (buf[20]) << 0;
852 l |= itoa64_to_int (buf[21]) << 6;
853
854 digest[11] = (l >> 0) & 0xff;
855 }
856
857 void md5crypt_encode (u8 digest[16], u8 buf[22])
858 {
859 int l;
860
861 l = (digest[ 0] << 16) | (digest[ 6] << 8) | (digest[12] << 0);
862
863 buf[ 0] = int_to_itoa64 (l & 0x3f); l >>= 6;
864 buf[ 1] = int_to_itoa64 (l & 0x3f); l >>= 6;
865 buf[ 2] = int_to_itoa64 (l & 0x3f); l >>= 6;
866 buf[ 3] = int_to_itoa64 (l & 0x3f); l >>= 6;
867
868 l = (digest[ 1] << 16) | (digest[ 7] << 8) | (digest[13] << 0);
869
870 buf[ 4] = int_to_itoa64 (l & 0x3f); l >>= 6;
871 buf[ 5] = int_to_itoa64 (l & 0x3f); l >>= 6;
872 buf[ 6] = int_to_itoa64 (l & 0x3f); l >>= 6;
873 buf[ 7] = int_to_itoa64 (l & 0x3f); l >>= 6;
874
875 l = (digest[ 2] << 16) | (digest[ 8] << 8) | (digest[14] << 0);
876
877 buf[ 8] = int_to_itoa64 (l & 0x3f); l >>= 6;
878 buf[ 9] = int_to_itoa64 (l & 0x3f); l >>= 6;
879 buf[10] = int_to_itoa64 (l & 0x3f); l >>= 6;
880 buf[11] = int_to_itoa64 (l & 0x3f); l >>= 6;
881
882 l = (digest[ 3] << 16) | (digest[ 9] << 8) | (digest[15] << 0);
883
884 buf[12] = int_to_itoa64 (l & 0x3f); l >>= 6;
885 buf[13] = int_to_itoa64 (l & 0x3f); l >>= 6;
886 buf[14] = int_to_itoa64 (l & 0x3f); l >>= 6;
887 buf[15] = int_to_itoa64 (l & 0x3f); l >>= 6;
888
889 l = (digest[ 4] << 16) | (digest[10] << 8) | (digest[ 5] << 0);
890
891 buf[16] = int_to_itoa64 (l & 0x3f); l >>= 6;
892 buf[17] = int_to_itoa64 (l & 0x3f); l >>= 6;
893 buf[18] = int_to_itoa64 (l & 0x3f); l >>= 6;
894 buf[19] = int_to_itoa64 (l & 0x3f); l >>= 6;
895
896 l = (digest[11] << 0);
897
898 buf[20] = int_to_itoa64 (l & 0x3f); l >>= 6;
899 buf[21] = int_to_itoa64 (l & 0x3f); l >>= 6;
900 }
901
902 void sha512crypt_decode (u8 digest[64], u8 buf[86])
903 {
904 int l;
905
906 l = itoa64_to_int (buf[ 0]) << 0;
907 l |= itoa64_to_int (buf[ 1]) << 6;
908 l |= itoa64_to_int (buf[ 2]) << 12;
909 l |= itoa64_to_int (buf[ 3]) << 18;
910
911 digest[ 0] = (l >> 16) & 0xff;
912 digest[21] = (l >> 8) & 0xff;
913 digest[42] = (l >> 0) & 0xff;
914
915 l = itoa64_to_int (buf[ 4]) << 0;
916 l |= itoa64_to_int (buf[ 5]) << 6;
917 l |= itoa64_to_int (buf[ 6]) << 12;
918 l |= itoa64_to_int (buf[ 7]) << 18;
919
920 digest[22] = (l >> 16) & 0xff;
921 digest[43] = (l >> 8) & 0xff;
922 digest[ 1] = (l >> 0) & 0xff;
923
924 l = itoa64_to_int (buf[ 8]) << 0;
925 l |= itoa64_to_int (buf[ 9]) << 6;
926 l |= itoa64_to_int (buf[10]) << 12;
927 l |= itoa64_to_int (buf[11]) << 18;
928
929 digest[44] = (l >> 16) & 0xff;
930 digest[ 2] = (l >> 8) & 0xff;
931 digest[23] = (l >> 0) & 0xff;
932
933 l = itoa64_to_int (buf[12]) << 0;
934 l |= itoa64_to_int (buf[13]) << 6;
935 l |= itoa64_to_int (buf[14]) << 12;
936 l |= itoa64_to_int (buf[15]) << 18;
937
938 digest[ 3] = (l >> 16) & 0xff;
939 digest[24] = (l >> 8) & 0xff;
940 digest[45] = (l >> 0) & 0xff;
941
942 l = itoa64_to_int (buf[16]) << 0;
943 l |= itoa64_to_int (buf[17]) << 6;
944 l |= itoa64_to_int (buf[18]) << 12;
945 l |= itoa64_to_int (buf[19]) << 18;
946
947 digest[25] = (l >> 16) & 0xff;
948 digest[46] = (l >> 8) & 0xff;
949 digest[ 4] = (l >> 0) & 0xff;
950
951 l = itoa64_to_int (buf[20]) << 0;
952 l |= itoa64_to_int (buf[21]) << 6;
953 l |= itoa64_to_int (buf[22]) << 12;
954 l |= itoa64_to_int (buf[23]) << 18;
955
956 digest[47] = (l >> 16) & 0xff;
957 digest[ 5] = (l >> 8) & 0xff;
958 digest[26] = (l >> 0) & 0xff;
959
960 l = itoa64_to_int (buf[24]) << 0;
961 l |= itoa64_to_int (buf[25]) << 6;
962 l |= itoa64_to_int (buf[26]) << 12;
963 l |= itoa64_to_int (buf[27]) << 18;
964
965 digest[ 6] = (l >> 16) & 0xff;
966 digest[27] = (l >> 8) & 0xff;
967 digest[48] = (l >> 0) & 0xff;
968
969 l = itoa64_to_int (buf[28]) << 0;
970 l |= itoa64_to_int (buf[29]) << 6;
971 l |= itoa64_to_int (buf[30]) << 12;
972 l |= itoa64_to_int (buf[31]) << 18;
973
974 digest[28] = (l >> 16) & 0xff;
975 digest[49] = (l >> 8) & 0xff;
976 digest[ 7] = (l >> 0) & 0xff;
977
978 l = itoa64_to_int (buf[32]) << 0;
979 l |= itoa64_to_int (buf[33]) << 6;
980 l |= itoa64_to_int (buf[34]) << 12;
981 l |= itoa64_to_int (buf[35]) << 18;
982
983 digest[50] = (l >> 16) & 0xff;
984 digest[ 8] = (l >> 8) & 0xff;
985 digest[29] = (l >> 0) & 0xff;
986
987 l = itoa64_to_int (buf[36]) << 0;
988 l |= itoa64_to_int (buf[37]) << 6;
989 l |= itoa64_to_int (buf[38]) << 12;
990 l |= itoa64_to_int (buf[39]) << 18;
991
992 digest[ 9] = (l >> 16) & 0xff;
993 digest[30] = (l >> 8) & 0xff;
994 digest[51] = (l >> 0) & 0xff;
995
996 l = itoa64_to_int (buf[40]) << 0;
997 l |= itoa64_to_int (buf[41]) << 6;
998 l |= itoa64_to_int (buf[42]) << 12;
999 l |= itoa64_to_int (buf[43]) << 18;
1000
1001 digest[31] = (l >> 16) & 0xff;
1002 digest[52] = (l >> 8) & 0xff;
1003 digest[10] = (l >> 0) & 0xff;
1004
1005 l = itoa64_to_int (buf[44]) << 0;
1006 l |= itoa64_to_int (buf[45]) << 6;
1007 l |= itoa64_to_int (buf[46]) << 12;
1008 l |= itoa64_to_int (buf[47]) << 18;
1009
1010 digest[53] = (l >> 16) & 0xff;
1011 digest[11] = (l >> 8) & 0xff;
1012 digest[32] = (l >> 0) & 0xff;
1013
1014 l = itoa64_to_int (buf[48]) << 0;
1015 l |= itoa64_to_int (buf[49]) << 6;
1016 l |= itoa64_to_int (buf[50]) << 12;
1017 l |= itoa64_to_int (buf[51]) << 18;
1018
1019 digest[12] = (l >> 16) & 0xff;
1020 digest[33] = (l >> 8) & 0xff;
1021 digest[54] = (l >> 0) & 0xff;
1022
1023 l = itoa64_to_int (buf[52]) << 0;
1024 l |= itoa64_to_int (buf[53]) << 6;
1025 l |= itoa64_to_int (buf[54]) << 12;
1026 l |= itoa64_to_int (buf[55]) << 18;
1027
1028 digest[34] = (l >> 16) & 0xff;
1029 digest[55] = (l >> 8) & 0xff;
1030 digest[13] = (l >> 0) & 0xff;
1031
1032 l = itoa64_to_int (buf[56]) << 0;
1033 l |= itoa64_to_int (buf[57]) << 6;
1034 l |= itoa64_to_int (buf[58]) << 12;
1035 l |= itoa64_to_int (buf[59]) << 18;
1036
1037 digest[56] = (l >> 16) & 0xff;
1038 digest[14] = (l >> 8) & 0xff;
1039 digest[35] = (l >> 0) & 0xff;
1040
1041 l = itoa64_to_int (buf[60]) << 0;
1042 l |= itoa64_to_int (buf[61]) << 6;
1043 l |= itoa64_to_int (buf[62]) << 12;
1044 l |= itoa64_to_int (buf[63]) << 18;
1045
1046 digest[15] = (l >> 16) & 0xff;
1047 digest[36] = (l >> 8) & 0xff;
1048 digest[57] = (l >> 0) & 0xff;
1049
1050 l = itoa64_to_int (buf[64]) << 0;
1051 l |= itoa64_to_int (buf[65]) << 6;
1052 l |= itoa64_to_int (buf[66]) << 12;
1053 l |= itoa64_to_int (buf[67]) << 18;
1054
1055 digest[37] = (l >> 16) & 0xff;
1056 digest[58] = (l >> 8) & 0xff;
1057 digest[16] = (l >> 0) & 0xff;
1058
1059 l = itoa64_to_int (buf[68]) << 0;
1060 l |= itoa64_to_int (buf[69]) << 6;
1061 l |= itoa64_to_int (buf[70]) << 12;
1062 l |= itoa64_to_int (buf[71]) << 18;
1063
1064 digest[59] = (l >> 16) & 0xff;
1065 digest[17] = (l >> 8) & 0xff;
1066 digest[38] = (l >> 0) & 0xff;
1067
1068 l = itoa64_to_int (buf[72]) << 0;
1069 l |= itoa64_to_int (buf[73]) << 6;
1070 l |= itoa64_to_int (buf[74]) << 12;
1071 l |= itoa64_to_int (buf[75]) << 18;
1072
1073 digest[18] = (l >> 16) & 0xff;
1074 digest[39] = (l >> 8) & 0xff;
1075 digest[60] = (l >> 0) & 0xff;
1076
1077 l = itoa64_to_int (buf[76]) << 0;
1078 l |= itoa64_to_int (buf[77]) << 6;
1079 l |= itoa64_to_int (buf[78]) << 12;
1080 l |= itoa64_to_int (buf[79]) << 18;
1081
1082 digest[40] = (l >> 16) & 0xff;
1083 digest[61] = (l >> 8) & 0xff;
1084 digest[19] = (l >> 0) & 0xff;
1085
1086 l = itoa64_to_int (buf[80]) << 0;
1087 l |= itoa64_to_int (buf[81]) << 6;
1088 l |= itoa64_to_int (buf[82]) << 12;
1089 l |= itoa64_to_int (buf[83]) << 18;
1090
1091 digest[62] = (l >> 16) & 0xff;
1092 digest[20] = (l >> 8) & 0xff;
1093 digest[41] = (l >> 0) & 0xff;
1094
1095 l = itoa64_to_int (buf[84]) << 0;
1096 l |= itoa64_to_int (buf[85]) << 6;
1097
1098 digest[63] = (l >> 0) & 0xff;
1099 }
1100
1101 void sha512crypt_encode (u8 digest[64], u8 buf[86])
1102 {
1103 int l;
1104
1105 l = (digest[ 0] << 16) | (digest[21] << 8) | (digest[42] << 0);
1106
1107 buf[ 0] = int_to_itoa64 (l & 0x3f); l >>= 6;
1108 buf[ 1] = int_to_itoa64 (l & 0x3f); l >>= 6;
1109 buf[ 2] = int_to_itoa64 (l & 0x3f); l >>= 6;
1110 buf[ 3] = int_to_itoa64 (l & 0x3f); l >>= 6;
1111
1112 l = (digest[22] << 16) | (digest[43] << 8) | (digest[ 1] << 0);
1113
1114 buf[ 4] = int_to_itoa64 (l & 0x3f); l >>= 6;
1115 buf[ 5] = int_to_itoa64 (l & 0x3f); l >>= 6;
1116 buf[ 6] = int_to_itoa64 (l & 0x3f); l >>= 6;
1117 buf[ 7] = int_to_itoa64 (l & 0x3f); l >>= 6;
1118
1119 l = (digest[44] << 16) | (digest[ 2] << 8) | (digest[23] << 0);
1120
1121 buf[ 8] = int_to_itoa64 (l & 0x3f); l >>= 6;
1122 buf[ 9] = int_to_itoa64 (l & 0x3f); l >>= 6;
1123 buf[10] = int_to_itoa64 (l & 0x3f); l >>= 6;
1124 buf[11] = int_to_itoa64 (l & 0x3f); l >>= 6;
1125
1126 l = (digest[ 3] << 16) | (digest[24] << 8) | (digest[45] << 0);
1127
1128 buf[12] = int_to_itoa64 (l & 0x3f); l >>= 6;
1129 buf[13] = int_to_itoa64 (l & 0x3f); l >>= 6;
1130 buf[14] = int_to_itoa64 (l & 0x3f); l >>= 6;
1131 buf[15] = int_to_itoa64 (l & 0x3f); l >>= 6;
1132
1133 l = (digest[25] << 16) | (digest[46] << 8) | (digest[ 4] << 0);
1134
1135 buf[16] = int_to_itoa64 (l & 0x3f); l >>= 6;
1136 buf[17] = int_to_itoa64 (l & 0x3f); l >>= 6;
1137 buf[18] = int_to_itoa64 (l & 0x3f); l >>= 6;
1138 buf[19] = int_to_itoa64 (l & 0x3f); l >>= 6;
1139
1140 l = (digest[47] << 16) | (digest[ 5] << 8) | (digest[26] << 0);
1141
1142 buf[20] = int_to_itoa64 (l & 0x3f); l >>= 6;
1143 buf[21] = int_to_itoa64 (l & 0x3f); l >>= 6;
1144 buf[22] = int_to_itoa64 (l & 0x3f); l >>= 6;
1145 buf[23] = int_to_itoa64 (l & 0x3f); l >>= 6;
1146
1147 l = (digest[ 6] << 16) | (digest[27] << 8) | (digest[48] << 0);
1148
1149 buf[24] = int_to_itoa64 (l & 0x3f); l >>= 6;
1150 buf[25] = int_to_itoa64 (l & 0x3f); l >>= 6;
1151 buf[26] = int_to_itoa64 (l & 0x3f); l >>= 6;
1152 buf[27] = int_to_itoa64 (l & 0x3f); l >>= 6;
1153
1154 l = (digest[28] << 16) | (digest[49] << 8) | (digest[ 7] << 0);
1155
1156 buf[28] = int_to_itoa64 (l & 0x3f); l >>= 6;
1157 buf[29] = int_to_itoa64 (l & 0x3f); l >>= 6;
1158 buf[30] = int_to_itoa64 (l & 0x3f); l >>= 6;
1159 buf[31] = int_to_itoa64 (l & 0x3f); l >>= 6;
1160
1161 l = (digest[50] << 16) | (digest[ 8] << 8) | (digest[29] << 0);
1162
1163 buf[32] = int_to_itoa64 (l & 0x3f); l >>= 6;
1164 buf[33] = int_to_itoa64 (l & 0x3f); l >>= 6;
1165 buf[34] = int_to_itoa64 (l & 0x3f); l >>= 6;
1166 buf[35] = int_to_itoa64 (l & 0x3f); l >>= 6;
1167
1168 l = (digest[ 9] << 16) | (digest[30] << 8) | (digest[51] << 0);
1169
1170 buf[36] = int_to_itoa64 (l & 0x3f); l >>= 6;
1171 buf[37] = int_to_itoa64 (l & 0x3f); l >>= 6;
1172 buf[38] = int_to_itoa64 (l & 0x3f); l >>= 6;
1173 buf[39] = int_to_itoa64 (l & 0x3f); l >>= 6;
1174
1175 l = (digest[31] << 16) | (digest[52] << 8) | (digest[10] << 0);
1176
1177 buf[40] = int_to_itoa64 (l & 0x3f); l >>= 6;
1178 buf[41] = int_to_itoa64 (l & 0x3f); l >>= 6;
1179 buf[42] = int_to_itoa64 (l & 0x3f); l >>= 6;
1180 buf[43] = int_to_itoa64 (l & 0x3f); l >>= 6;
1181
1182 l = (digest[53] << 16) | (digest[11] << 8) | (digest[32] << 0);
1183
1184 buf[44] = int_to_itoa64 (l & 0x3f); l >>= 6;
1185 buf[45] = int_to_itoa64 (l & 0x3f); l >>= 6;
1186 buf[46] = int_to_itoa64 (l & 0x3f); l >>= 6;
1187 buf[47] = int_to_itoa64 (l & 0x3f); l >>= 6;
1188
1189 l = (digest[12] << 16) | (digest[33] << 8) | (digest[54] << 0);
1190
1191 buf[48] = int_to_itoa64 (l & 0x3f); l >>= 6;
1192 buf[49] = int_to_itoa64 (l & 0x3f); l >>= 6;
1193 buf[50] = int_to_itoa64 (l & 0x3f); l >>= 6;
1194 buf[51] = int_to_itoa64 (l & 0x3f); l >>= 6;
1195
1196 l = (digest[34] << 16) | (digest[55] << 8) | (digest[13] << 0);
1197
1198 buf[52] = int_to_itoa64 (l & 0x3f); l >>= 6;
1199 buf[53] = int_to_itoa64 (l & 0x3f); l >>= 6;
1200 buf[54] = int_to_itoa64 (l & 0x3f); l >>= 6;
1201 buf[55] = int_to_itoa64 (l & 0x3f); l >>= 6;
1202
1203 l = (digest[56] << 16) | (digest[14] << 8) | (digest[35] << 0);
1204
1205 buf[56] = int_to_itoa64 (l & 0x3f); l >>= 6;
1206 buf[57] = int_to_itoa64 (l & 0x3f); l >>= 6;
1207 buf[58] = int_to_itoa64 (l & 0x3f); l >>= 6;
1208 buf[59] = int_to_itoa64 (l & 0x3f); l >>= 6;
1209
1210 l = (digest[15] << 16) | (digest[36] << 8) | (digest[57] << 0);
1211
1212 buf[60] = int_to_itoa64 (l & 0x3f); l >>= 6;
1213 buf[61] = int_to_itoa64 (l & 0x3f); l >>= 6;
1214 buf[62] = int_to_itoa64 (l & 0x3f); l >>= 6;
1215 buf[63] = int_to_itoa64 (l & 0x3f); l >>= 6;
1216
1217 l = (digest[37] << 16) | (digest[58] << 8) | (digest[16] << 0);
1218
1219 buf[64] = int_to_itoa64 (l & 0x3f); l >>= 6;
1220 buf[65] = int_to_itoa64 (l & 0x3f); l >>= 6;
1221 buf[66] = int_to_itoa64 (l & 0x3f); l >>= 6;
1222 buf[67] = int_to_itoa64 (l & 0x3f); l >>= 6;
1223
1224 l = (digest[59] << 16) | (digest[17] << 8) | (digest[38] << 0);
1225
1226 buf[68] = int_to_itoa64 (l & 0x3f); l >>= 6;
1227 buf[69] = int_to_itoa64 (l & 0x3f); l >>= 6;
1228 buf[70] = int_to_itoa64 (l & 0x3f); l >>= 6;
1229 buf[71] = int_to_itoa64 (l & 0x3f); l >>= 6;
1230
1231 l = (digest[18] << 16) | (digest[39] << 8) | (digest[60] << 0);
1232
1233 buf[72] = int_to_itoa64 (l & 0x3f); l >>= 6;
1234 buf[73] = int_to_itoa64 (l & 0x3f); l >>= 6;
1235 buf[74] = int_to_itoa64 (l & 0x3f); l >>= 6;
1236 buf[75] = int_to_itoa64 (l & 0x3f); l >>= 6;
1237
1238 l = (digest[40] << 16) | (digest[61] << 8) | (digest[19] << 0);
1239
1240 buf[76] = int_to_itoa64 (l & 0x3f); l >>= 6;
1241 buf[77] = int_to_itoa64 (l & 0x3f); l >>= 6;
1242 buf[78] = int_to_itoa64 (l & 0x3f); l >>= 6;
1243 buf[79] = int_to_itoa64 (l & 0x3f); l >>= 6;
1244
1245 l = (digest[62] << 16) | (digest[20] << 8) | (digest[41] << 0);
1246
1247 buf[80] = int_to_itoa64 (l & 0x3f); l >>= 6;
1248 buf[81] = int_to_itoa64 (l & 0x3f); l >>= 6;
1249 buf[82] = int_to_itoa64 (l & 0x3f); l >>= 6;
1250 buf[83] = int_to_itoa64 (l & 0x3f); l >>= 6;
1251
1252 l = 0 | 0 | (digest[63] << 0);
1253
1254 buf[84] = int_to_itoa64 (l & 0x3f); l >>= 6;
1255 buf[85] = int_to_itoa64 (l & 0x3f); l >>= 6;
1256 }
1257
1258 void sha1aix_decode (u8 digest[20], u8 buf[27])
1259 {
1260 int l;
1261
1262 l = itoa64_to_int (buf[ 0]) << 0;
1263 l |= itoa64_to_int (buf[ 1]) << 6;
1264 l |= itoa64_to_int (buf[ 2]) << 12;
1265 l |= itoa64_to_int (buf[ 3]) << 18;
1266
1267 digest[ 2] = (l >> 0) & 0xff;
1268 digest[ 1] = (l >> 8) & 0xff;
1269 digest[ 0] = (l >> 16) & 0xff;
1270
1271 l = itoa64_to_int (buf[ 4]) << 0;
1272 l |= itoa64_to_int (buf[ 5]) << 6;
1273 l |= itoa64_to_int (buf[ 6]) << 12;
1274 l |= itoa64_to_int (buf[ 7]) << 18;
1275
1276 digest[ 5] = (l >> 0) & 0xff;
1277 digest[ 4] = (l >> 8) & 0xff;
1278 digest[ 3] = (l >> 16) & 0xff;
1279
1280 l = itoa64_to_int (buf[ 8]) << 0;
1281 l |= itoa64_to_int (buf[ 9]) << 6;
1282 l |= itoa64_to_int (buf[10]) << 12;
1283 l |= itoa64_to_int (buf[11]) << 18;
1284
1285 digest[ 8] = (l >> 0) & 0xff;
1286 digest[ 7] = (l >> 8) & 0xff;
1287 digest[ 6] = (l >> 16) & 0xff;
1288
1289 l = itoa64_to_int (buf[12]) << 0;
1290 l |= itoa64_to_int (buf[13]) << 6;
1291 l |= itoa64_to_int (buf[14]) << 12;
1292 l |= itoa64_to_int (buf[15]) << 18;
1293
1294 digest[11] = (l >> 0) & 0xff;
1295 digest[10] = (l >> 8) & 0xff;
1296 digest[ 9] = (l >> 16) & 0xff;
1297
1298 l = itoa64_to_int (buf[16]) << 0;
1299 l |= itoa64_to_int (buf[17]) << 6;
1300 l |= itoa64_to_int (buf[18]) << 12;
1301 l |= itoa64_to_int (buf[19]) << 18;
1302
1303 digest[14] = (l >> 0) & 0xff;
1304 digest[13] = (l >> 8) & 0xff;
1305 digest[12] = (l >> 16) & 0xff;
1306
1307 l = itoa64_to_int (buf[20]) << 0;
1308 l |= itoa64_to_int (buf[21]) << 6;
1309 l |= itoa64_to_int (buf[22]) << 12;
1310 l |= itoa64_to_int (buf[23]) << 18;
1311
1312 digest[17] = (l >> 0) & 0xff;
1313 digest[16] = (l >> 8) & 0xff;
1314 digest[15] = (l >> 16) & 0xff;
1315
1316 l = itoa64_to_int (buf[24]) << 0;
1317 l |= itoa64_to_int (buf[25]) << 6;
1318 l |= itoa64_to_int (buf[26]) << 12;
1319
1320 digest[19] = (l >> 8) & 0xff;
1321 digest[18] = (l >> 16) & 0xff;
1322 }
1323
1324 void sha1aix_encode (u8 digest[20], u8 buf[27])
1325 {
1326 int l;
1327
1328 l = (digest[ 2] << 0) | (digest[ 1] << 8) | (digest[ 0] << 16);
1329
1330 buf[ 0] = int_to_itoa64 (l & 0x3f); l >>= 6;
1331 buf[ 1] = int_to_itoa64 (l & 0x3f); l >>= 6;
1332 buf[ 2] = int_to_itoa64 (l & 0x3f); l >>= 6;
1333 buf[ 3] = int_to_itoa64 (l & 0x3f);
1334
1335 l = (digest[ 5] << 0) | (digest[ 4] << 8) | (digest[ 3] << 16);
1336
1337 buf[ 4] = int_to_itoa64 (l & 0x3f); l >>= 6;
1338 buf[ 5] = int_to_itoa64 (l & 0x3f); l >>= 6;
1339 buf[ 6] = int_to_itoa64 (l & 0x3f); l >>= 6;
1340 buf[ 7] = int_to_itoa64 (l & 0x3f);
1341
1342 l = (digest[ 8] << 0) | (digest[ 7] << 8) | (digest[ 6] << 16);
1343
1344 buf[ 8] = int_to_itoa64 (l & 0x3f); l >>= 6;
1345 buf[ 9] = int_to_itoa64 (l & 0x3f); l >>= 6;
1346 buf[10] = int_to_itoa64 (l & 0x3f); l >>= 6;
1347 buf[11] = int_to_itoa64 (l & 0x3f);
1348
1349 l = (digest[11] << 0) | (digest[10] << 8) | (digest[ 9] << 16);
1350
1351 buf[12] = int_to_itoa64 (l & 0x3f); l >>= 6;
1352 buf[13] = int_to_itoa64 (l & 0x3f); l >>= 6;
1353 buf[14] = int_to_itoa64 (l & 0x3f); l >>= 6;
1354 buf[15] = int_to_itoa64 (l & 0x3f);
1355
1356 l = (digest[14] << 0) | (digest[13] << 8) | (digest[12] << 16);
1357
1358 buf[16] = int_to_itoa64 (l & 0x3f); l >>= 6;
1359 buf[17] = int_to_itoa64 (l & 0x3f); l >>= 6;
1360 buf[18] = int_to_itoa64 (l & 0x3f); l >>= 6;
1361 buf[19] = int_to_itoa64 (l & 0x3f);
1362
1363 l = (digest[17] << 0) | (digest[16] << 8) | (digest[15] << 16);
1364
1365 buf[20] = int_to_itoa64 (l & 0x3f); l >>= 6;
1366 buf[21] = int_to_itoa64 (l & 0x3f); l >>= 6;
1367 buf[22] = int_to_itoa64 (l & 0x3f); l >>= 6;
1368 buf[23] = int_to_itoa64 (l & 0x3f);
1369
1370 l = 0 | (digest[19] << 8) | (digest[18] << 16);
1371
1372 buf[24] = int_to_itoa64 (l & 0x3f); l >>= 6;
1373 buf[25] = int_to_itoa64 (l & 0x3f); l >>= 6;
1374 buf[26] = int_to_itoa64 (l & 0x3f);
1375 }
1376
1377 void sha256aix_decode (u8 digest[32], u8 buf[43])
1378 {
1379 int l;
1380
1381 l = itoa64_to_int (buf[ 0]) << 0;
1382 l |= itoa64_to_int (buf[ 1]) << 6;
1383 l |= itoa64_to_int (buf[ 2]) << 12;
1384 l |= itoa64_to_int (buf[ 3]) << 18;
1385
1386 digest[ 2] = (l >> 0) & 0xff;
1387 digest[ 1] = (l >> 8) & 0xff;
1388 digest[ 0] = (l >> 16) & 0xff;
1389
1390 l = itoa64_to_int (buf[ 4]) << 0;
1391 l |= itoa64_to_int (buf[ 5]) << 6;
1392 l |= itoa64_to_int (buf[ 6]) << 12;
1393 l |= itoa64_to_int (buf[ 7]) << 18;
1394
1395 digest[ 5] = (l >> 0) & 0xff;
1396 digest[ 4] = (l >> 8) & 0xff;
1397 digest[ 3] = (l >> 16) & 0xff;
1398
1399 l = itoa64_to_int (buf[ 8]) << 0;
1400 l |= itoa64_to_int (buf[ 9]) << 6;
1401 l |= itoa64_to_int (buf[10]) << 12;
1402 l |= itoa64_to_int (buf[11]) << 18;
1403
1404 digest[ 8] = (l >> 0) & 0xff;
1405 digest[ 7] = (l >> 8) & 0xff;
1406 digest[ 6] = (l >> 16) & 0xff;
1407
1408 l = itoa64_to_int (buf[12]) << 0;
1409 l |= itoa64_to_int (buf[13]) << 6;
1410 l |= itoa64_to_int (buf[14]) << 12;
1411 l |= itoa64_to_int (buf[15]) << 18;
1412
1413 digest[11] = (l >> 0) & 0xff;
1414 digest[10] = (l >> 8) & 0xff;
1415 digest[ 9] = (l >> 16) & 0xff;
1416
1417 l = itoa64_to_int (buf[16]) << 0;
1418 l |= itoa64_to_int (buf[17]) << 6;
1419 l |= itoa64_to_int (buf[18]) << 12;
1420 l |= itoa64_to_int (buf[19]) << 18;
1421
1422 digest[14] = (l >> 0) & 0xff;
1423 digest[13] = (l >> 8) & 0xff;
1424 digest[12] = (l >> 16) & 0xff;
1425
1426 l = itoa64_to_int (buf[20]) << 0;
1427 l |= itoa64_to_int (buf[21]) << 6;
1428 l |= itoa64_to_int (buf[22]) << 12;
1429 l |= itoa64_to_int (buf[23]) << 18;
1430
1431 digest[17] = (l >> 0) & 0xff;
1432 digest[16] = (l >> 8) & 0xff;
1433 digest[15] = (l >> 16) & 0xff;
1434
1435 l = itoa64_to_int (buf[24]) << 0;
1436 l |= itoa64_to_int (buf[25]) << 6;
1437 l |= itoa64_to_int (buf[26]) << 12;
1438 l |= itoa64_to_int (buf[27]) << 18;
1439
1440 digest[20] = (l >> 0) & 0xff;
1441 digest[19] = (l >> 8) & 0xff;
1442 digest[18] = (l >> 16) & 0xff;
1443
1444 l = itoa64_to_int (buf[28]) << 0;
1445 l |= itoa64_to_int (buf[29]) << 6;
1446 l |= itoa64_to_int (buf[30]) << 12;
1447 l |= itoa64_to_int (buf[31]) << 18;
1448
1449 digest[23] = (l >> 0) & 0xff;
1450 digest[22] = (l >> 8) & 0xff;
1451 digest[21] = (l >> 16) & 0xff;
1452
1453 l = itoa64_to_int (buf[32]) << 0;
1454 l |= itoa64_to_int (buf[33]) << 6;
1455 l |= itoa64_to_int (buf[34]) << 12;
1456 l |= itoa64_to_int (buf[35]) << 18;
1457
1458 digest[26] = (l >> 0) & 0xff;
1459 digest[25] = (l >> 8) & 0xff;
1460 digest[24] = (l >> 16) & 0xff;
1461
1462 l = itoa64_to_int (buf[36]) << 0;
1463 l |= itoa64_to_int (buf[37]) << 6;
1464 l |= itoa64_to_int (buf[38]) << 12;
1465 l |= itoa64_to_int (buf[39]) << 18;
1466
1467 digest[29] = (l >> 0) & 0xff;
1468 digest[28] = (l >> 8) & 0xff;
1469 digest[27] = (l >> 16) & 0xff;
1470
1471 l = itoa64_to_int (buf[40]) << 0;
1472 l |= itoa64_to_int (buf[41]) << 6;
1473 l |= itoa64_to_int (buf[42]) << 12;
1474
1475 //digest[32] = (l >> 0) & 0xff;
1476 digest[31] = (l >> 8) & 0xff;
1477 digest[30] = (l >> 16) & 0xff;
1478 }
1479
1480 void sha256aix_encode (u8 digest[32], u8 buf[43])
1481 {
1482 int l;
1483
1484 l = (digest[ 2] << 0) | (digest[ 1] << 8) | (digest[ 0] << 16);
1485
1486 buf[ 0] = int_to_itoa64 (l & 0x3f); l >>= 6;
1487 buf[ 1] = int_to_itoa64 (l & 0x3f); l >>= 6;
1488 buf[ 2] = int_to_itoa64 (l & 0x3f); l >>= 6;
1489 buf[ 3] = int_to_itoa64 (l & 0x3f);
1490
1491 l = (digest[ 5] << 0) | (digest[ 4] << 8) | (digest[ 3] << 16);
1492
1493 buf[ 4] = int_to_itoa64 (l & 0x3f); l >>= 6;
1494 buf[ 5] = int_to_itoa64 (l & 0x3f); l >>= 6;
1495 buf[ 6] = int_to_itoa64 (l & 0x3f); l >>= 6;
1496 buf[ 7] = int_to_itoa64 (l & 0x3f);
1497
1498 l = (digest[ 8] << 0) | (digest[ 7] << 8) | (digest[ 6] << 16);
1499
1500 buf[ 8] = int_to_itoa64 (l & 0x3f); l >>= 6;
1501 buf[ 9] = int_to_itoa64 (l & 0x3f); l >>= 6;
1502 buf[10] = int_to_itoa64 (l & 0x3f); l >>= 6;
1503 buf[11] = int_to_itoa64 (l & 0x3f);
1504
1505 l = (digest[11] << 0) | (digest[10] << 8) | (digest[ 9] << 16);
1506
1507 buf[12] = int_to_itoa64 (l & 0x3f); l >>= 6;
1508 buf[13] = int_to_itoa64 (l & 0x3f); l >>= 6;
1509 buf[14] = int_to_itoa64 (l & 0x3f); l >>= 6;
1510 buf[15] = int_to_itoa64 (l & 0x3f);
1511
1512 l = (digest[14] << 0) | (digest[13] << 8) | (digest[12] << 16);
1513
1514 buf[16] = int_to_itoa64 (l & 0x3f); l >>= 6;
1515 buf[17] = int_to_itoa64 (l & 0x3f); l >>= 6;
1516 buf[18] = int_to_itoa64 (l & 0x3f); l >>= 6;
1517 buf[19] = int_to_itoa64 (l & 0x3f);
1518
1519 l = (digest[17] << 0) | (digest[16] << 8) | (digest[15] << 16);
1520
1521 buf[20] = int_to_itoa64 (l & 0x3f); l >>= 6;
1522 buf[21] = int_to_itoa64 (l & 0x3f); l >>= 6;
1523 buf[22] = int_to_itoa64 (l & 0x3f); l >>= 6;
1524 buf[23] = int_to_itoa64 (l & 0x3f);
1525
1526 l = (digest[20] << 0) | (digest[19] << 8) | (digest[18] << 16);
1527
1528 buf[24] = int_to_itoa64 (l & 0x3f); l >>= 6;
1529 buf[25] = int_to_itoa64 (l & 0x3f); l >>= 6;
1530 buf[26] = int_to_itoa64 (l & 0x3f); l >>= 6;
1531 buf[27] = int_to_itoa64 (l & 0x3f);
1532
1533 l = (digest[23] << 0) | (digest[22] << 8) | (digest[21] << 16);
1534
1535 buf[28] = int_to_itoa64 (l & 0x3f); l >>= 6;
1536 buf[29] = int_to_itoa64 (l & 0x3f); l >>= 6;
1537 buf[30] = int_to_itoa64 (l & 0x3f); l >>= 6;
1538 buf[31] = int_to_itoa64 (l & 0x3f);
1539
1540 l = (digest[26] << 0) | (digest[25] << 8) | (digest[24] << 16);
1541
1542 buf[32] = int_to_itoa64 (l & 0x3f); l >>= 6;
1543 buf[33] = int_to_itoa64 (l & 0x3f); l >>= 6;
1544 buf[34] = int_to_itoa64 (l & 0x3f); l >>= 6;
1545 buf[35] = int_to_itoa64 (l & 0x3f);
1546
1547 l = (digest[29] << 0) | (digest[28] << 8) | (digest[27] << 16);
1548
1549 buf[36] = int_to_itoa64 (l & 0x3f); l >>= 6;
1550 buf[37] = int_to_itoa64 (l & 0x3f); l >>= 6;
1551 buf[38] = int_to_itoa64 (l & 0x3f); l >>= 6;
1552 buf[39] = int_to_itoa64 (l & 0x3f);
1553
1554 l = 0 | (digest[31] << 8) | (digest[30] << 16);
1555
1556 buf[40] = int_to_itoa64 (l & 0x3f); l >>= 6;
1557 buf[41] = int_to_itoa64 (l & 0x3f); l >>= 6;
1558 buf[42] = int_to_itoa64 (l & 0x3f);
1559 }
1560
1561 void sha512aix_decode (u8 digest[64], u8 buf[86])
1562 {
1563 int l;
1564
1565 l = itoa64_to_int (buf[ 0]) << 0;
1566 l |= itoa64_to_int (buf[ 1]) << 6;
1567 l |= itoa64_to_int (buf[ 2]) << 12;
1568 l |= itoa64_to_int (buf[ 3]) << 18;
1569
1570 digest[ 2] = (l >> 0) & 0xff;
1571 digest[ 1] = (l >> 8) & 0xff;
1572 digest[ 0] = (l >> 16) & 0xff;
1573
1574 l = itoa64_to_int (buf[ 4]) << 0;
1575 l |= itoa64_to_int (buf[ 5]) << 6;
1576 l |= itoa64_to_int (buf[ 6]) << 12;
1577 l |= itoa64_to_int (buf[ 7]) << 18;
1578
1579 digest[ 5] = (l >> 0) & 0xff;
1580 digest[ 4] = (l >> 8) & 0xff;
1581 digest[ 3] = (l >> 16) & 0xff;
1582
1583 l = itoa64_to_int (buf[ 8]) << 0;
1584 l |= itoa64_to_int (buf[ 9]) << 6;
1585 l |= itoa64_to_int (buf[10]) << 12;
1586 l |= itoa64_to_int (buf[11]) << 18;
1587
1588 digest[ 8] = (l >> 0) & 0xff;
1589 digest[ 7] = (l >> 8) & 0xff;
1590 digest[ 6] = (l >> 16) & 0xff;
1591
1592 l = itoa64_to_int (buf[12]) << 0;
1593 l |= itoa64_to_int (buf[13]) << 6;
1594 l |= itoa64_to_int (buf[14]) << 12;
1595 l |= itoa64_to_int (buf[15]) << 18;
1596
1597 digest[11] = (l >> 0) & 0xff;
1598 digest[10] = (l >> 8) & 0xff;
1599 digest[ 9] = (l >> 16) & 0xff;
1600
1601 l = itoa64_to_int (buf[16]) << 0;
1602 l |= itoa64_to_int (buf[17]) << 6;
1603 l |= itoa64_to_int (buf[18]) << 12;
1604 l |= itoa64_to_int (buf[19]) << 18;
1605
1606 digest[14] = (l >> 0) & 0xff;
1607 digest[13] = (l >> 8) & 0xff;
1608 digest[12] = (l >> 16) & 0xff;
1609
1610 l = itoa64_to_int (buf[20]) << 0;
1611 l |= itoa64_to_int (buf[21]) << 6;
1612 l |= itoa64_to_int (buf[22]) << 12;
1613 l |= itoa64_to_int (buf[23]) << 18;
1614
1615 digest[17] = (l >> 0) & 0xff;
1616 digest[16] = (l >> 8) & 0xff;
1617 digest[15] = (l >> 16) & 0xff;
1618
1619 l = itoa64_to_int (buf[24]) << 0;
1620 l |= itoa64_to_int (buf[25]) << 6;
1621 l |= itoa64_to_int (buf[26]) << 12;
1622 l |= itoa64_to_int (buf[27]) << 18;
1623
1624 digest[20] = (l >> 0) & 0xff;
1625 digest[19] = (l >> 8) & 0xff;
1626 digest[18] = (l >> 16) & 0xff;
1627
1628 l = itoa64_to_int (buf[28]) << 0;
1629 l |= itoa64_to_int (buf[29]) << 6;
1630 l |= itoa64_to_int (buf[30]) << 12;
1631 l |= itoa64_to_int (buf[31]) << 18;
1632
1633 digest[23] = (l >> 0) & 0xff;
1634 digest[22] = (l >> 8) & 0xff;
1635 digest[21] = (l >> 16) & 0xff;
1636
1637 l = itoa64_to_int (buf[32]) << 0;
1638 l |= itoa64_to_int (buf[33]) << 6;
1639 l |= itoa64_to_int (buf[34]) << 12;
1640 l |= itoa64_to_int (buf[35]) << 18;
1641
1642 digest[26] = (l >> 0) & 0xff;
1643 digest[25] = (l >> 8) & 0xff;
1644 digest[24] = (l >> 16) & 0xff;
1645
1646 l = itoa64_to_int (buf[36]) << 0;
1647 l |= itoa64_to_int (buf[37]) << 6;
1648 l |= itoa64_to_int (buf[38]) << 12;
1649 l |= itoa64_to_int (buf[39]) << 18;
1650
1651 digest[29] = (l >> 0) & 0xff;
1652 digest[28] = (l >> 8) & 0xff;
1653 digest[27] = (l >> 16) & 0xff;
1654
1655 l = itoa64_to_int (buf[40]) << 0;
1656 l |= itoa64_to_int (buf[41]) << 6;
1657 l |= itoa64_to_int (buf[42]) << 12;
1658 l |= itoa64_to_int (buf[43]) << 18;
1659
1660 digest[32] = (l >> 0) & 0xff;
1661 digest[31] = (l >> 8) & 0xff;
1662 digest[30] = (l >> 16) & 0xff;
1663
1664 l = itoa64_to_int (buf[44]) << 0;
1665 l |= itoa64_to_int (buf[45]) << 6;
1666 l |= itoa64_to_int (buf[46]) << 12;
1667 l |= itoa64_to_int (buf[47]) << 18;
1668
1669 digest[35] = (l >> 0) & 0xff;
1670 digest[34] = (l >> 8) & 0xff;
1671 digest[33] = (l >> 16) & 0xff;
1672
1673 l = itoa64_to_int (buf[48]) << 0;
1674 l |= itoa64_to_int (buf[49]) << 6;
1675 l |= itoa64_to_int (buf[50]) << 12;
1676 l |= itoa64_to_int (buf[51]) << 18;
1677
1678 digest[38] = (l >> 0) & 0xff;
1679 digest[37] = (l >> 8) & 0xff;
1680 digest[36] = (l >> 16) & 0xff;
1681
1682 l = itoa64_to_int (buf[52]) << 0;
1683 l |= itoa64_to_int (buf[53]) << 6;
1684 l |= itoa64_to_int (buf[54]) << 12;
1685 l |= itoa64_to_int (buf[55]) << 18;
1686
1687 digest[41] = (l >> 0) & 0xff;
1688 digest[40] = (l >> 8) & 0xff;
1689 digest[39] = (l >> 16) & 0xff;
1690
1691 l = itoa64_to_int (buf[56]) << 0;
1692 l |= itoa64_to_int (buf[57]) << 6;
1693 l |= itoa64_to_int (buf[58]) << 12;
1694 l |= itoa64_to_int (buf[59]) << 18;
1695
1696 digest[44] = (l >> 0) & 0xff;
1697 digest[43] = (l >> 8) & 0xff;
1698 digest[42] = (l >> 16) & 0xff;
1699
1700 l = itoa64_to_int (buf[60]) << 0;
1701 l |= itoa64_to_int (buf[61]) << 6;
1702 l |= itoa64_to_int (buf[62]) << 12;
1703 l |= itoa64_to_int (buf[63]) << 18;
1704
1705 digest[47] = (l >> 0) & 0xff;
1706 digest[46] = (l >> 8) & 0xff;
1707 digest[45] = (l >> 16) & 0xff;
1708
1709 l = itoa64_to_int (buf[64]) << 0;
1710 l |= itoa64_to_int (buf[65]) << 6;
1711 l |= itoa64_to_int (buf[66]) << 12;
1712 l |= itoa64_to_int (buf[67]) << 18;
1713
1714 digest[50] = (l >> 0) & 0xff;
1715 digest[49] = (l >> 8) & 0xff;
1716 digest[48] = (l >> 16) & 0xff;
1717
1718 l = itoa64_to_int (buf[68]) << 0;
1719 l |= itoa64_to_int (buf[69]) << 6;
1720 l |= itoa64_to_int (buf[70]) << 12;
1721 l |= itoa64_to_int (buf[71]) << 18;
1722
1723 digest[53] = (l >> 0) & 0xff;
1724 digest[52] = (l >> 8) & 0xff;
1725 digest[51] = (l >> 16) & 0xff;
1726
1727 l = itoa64_to_int (buf[72]) << 0;
1728 l |= itoa64_to_int (buf[73]) << 6;
1729 l |= itoa64_to_int (buf[74]) << 12;
1730 l |= itoa64_to_int (buf[75]) << 18;
1731
1732 digest[56] = (l >> 0) & 0xff;
1733 digest[55] = (l >> 8) & 0xff;
1734 digest[54] = (l >> 16) & 0xff;
1735
1736 l = itoa64_to_int (buf[76]) << 0;
1737 l |= itoa64_to_int (buf[77]) << 6;
1738 l |= itoa64_to_int (buf[78]) << 12;
1739 l |= itoa64_to_int (buf[79]) << 18;
1740
1741 digest[59] = (l >> 0) & 0xff;
1742 digest[58] = (l >> 8) & 0xff;
1743 digest[57] = (l >> 16) & 0xff;
1744
1745 l = itoa64_to_int (buf[80]) << 0;
1746 l |= itoa64_to_int (buf[81]) << 6;
1747 l |= itoa64_to_int (buf[82]) << 12;
1748 l |= itoa64_to_int (buf[83]) << 18;
1749
1750 digest[62] = (l >> 0) & 0xff;
1751 digest[61] = (l >> 8) & 0xff;
1752 digest[60] = (l >> 16) & 0xff;
1753
1754 l = itoa64_to_int (buf[84]) << 0;
1755 l |= itoa64_to_int (buf[85]) << 6;
1756
1757 digest[63] = (l >> 16) & 0xff;
1758 }
1759
1760 void sha512aix_encode (u8 digest[64], u8 buf[86])
1761 {
1762 int l;
1763
1764 l = (digest[ 2] << 0) | (digest[ 1] << 8) | (digest[ 0] << 16);
1765
1766 buf[ 0] = int_to_itoa64 (l & 0x3f); l >>= 6;
1767 buf[ 1] = int_to_itoa64 (l & 0x3f); l >>= 6;
1768 buf[ 2] = int_to_itoa64 (l & 0x3f); l >>= 6;
1769 buf[ 3] = int_to_itoa64 (l & 0x3f);
1770
1771 l = (digest[ 5] << 0) | (digest[ 4] << 8) | (digest[ 3] << 16);
1772
1773 buf[ 4] = int_to_itoa64 (l & 0x3f); l >>= 6;
1774 buf[ 5] = int_to_itoa64 (l & 0x3f); l >>= 6;
1775 buf[ 6] = int_to_itoa64 (l & 0x3f); l >>= 6;
1776 buf[ 7] = int_to_itoa64 (l & 0x3f);
1777
1778 l = (digest[ 8] << 0) | (digest[ 7] << 8) | (digest[ 6] << 16);
1779
1780 buf[ 8] = int_to_itoa64 (l & 0x3f); l >>= 6;
1781 buf[ 9] = int_to_itoa64 (l & 0x3f); l >>= 6;
1782 buf[10] = int_to_itoa64 (l & 0x3f); l >>= 6;
1783 buf[11] = int_to_itoa64 (l & 0x3f);
1784
1785 l = (digest[11] << 0) | (digest[10] << 8) | (digest[ 9] << 16);
1786
1787 buf[12] = int_to_itoa64 (l & 0x3f); l >>= 6;
1788 buf[13] = int_to_itoa64 (l & 0x3f); l >>= 6;
1789 buf[14] = int_to_itoa64 (l & 0x3f); l >>= 6;
1790 buf[15] = int_to_itoa64 (l & 0x3f);
1791
1792 l = (digest[14] << 0) | (digest[13] << 8) | (digest[12] << 16);
1793
1794 buf[16] = int_to_itoa64 (l & 0x3f); l >>= 6;
1795 buf[17] = int_to_itoa64 (l & 0x3f); l >>= 6;
1796 buf[18] = int_to_itoa64 (l & 0x3f); l >>= 6;
1797 buf[19] = int_to_itoa64 (l & 0x3f);
1798
1799 l = (digest[17] << 0) | (digest[16] << 8) | (digest[15] << 16);
1800
1801 buf[20] = int_to_itoa64 (l & 0x3f); l >>= 6;
1802 buf[21] = int_to_itoa64 (l & 0x3f); l >>= 6;
1803 buf[22] = int_to_itoa64 (l & 0x3f); l >>= 6;
1804 buf[23] = int_to_itoa64 (l & 0x3f);
1805
1806 l = (digest[20] << 0) | (digest[19] << 8) | (digest[18] << 16);
1807
1808 buf[24] = int_to_itoa64 (l & 0x3f); l >>= 6;
1809 buf[25] = int_to_itoa64 (l & 0x3f); l >>= 6;
1810 buf[26] = int_to_itoa64 (l & 0x3f); l >>= 6;
1811 buf[27] = int_to_itoa64 (l & 0x3f);
1812
1813 l = (digest[23] << 0) | (digest[22] << 8) | (digest[21] << 16);
1814
1815 buf[28] = int_to_itoa64 (l & 0x3f); l >>= 6;
1816 buf[29] = int_to_itoa64 (l & 0x3f); l >>= 6;
1817 buf[30] = int_to_itoa64 (l & 0x3f); l >>= 6;
1818 buf[31] = int_to_itoa64 (l & 0x3f);
1819
1820 l = (digest[26] << 0) | (digest[25] << 8) | (digest[24] << 16);
1821
1822 buf[32] = int_to_itoa64 (l & 0x3f); l >>= 6;
1823 buf[33] = int_to_itoa64 (l & 0x3f); l >>= 6;
1824 buf[34] = int_to_itoa64 (l & 0x3f); l >>= 6;
1825 buf[35] = int_to_itoa64 (l & 0x3f);
1826
1827 l = (digest[29] << 0) | (digest[28] << 8) | (digest[27] << 16);
1828
1829 buf[36] = int_to_itoa64 (l & 0x3f); l >>= 6;
1830 buf[37] = int_to_itoa64 (l & 0x3f); l >>= 6;
1831 buf[38] = int_to_itoa64 (l & 0x3f); l >>= 6;
1832 buf[39] = int_to_itoa64 (l & 0x3f);
1833
1834 l = (digest[32] << 0) | (digest[31] << 8) | (digest[30] << 16);
1835
1836 buf[40] = int_to_itoa64 (l & 0x3f); l >>= 6;
1837 buf[41] = int_to_itoa64 (l & 0x3f); l >>= 6;
1838 buf[42] = int_to_itoa64 (l & 0x3f); l >>= 6;
1839 buf[43] = int_to_itoa64 (l & 0x3f);
1840
1841 l = (digest[35] << 0) | (digest[34] << 8) | (digest[33] << 16);
1842
1843 buf[44] = int_to_itoa64 (l & 0x3f); l >>= 6;
1844 buf[45] = int_to_itoa64 (l & 0x3f); l >>= 6;
1845 buf[46] = int_to_itoa64 (l & 0x3f); l >>= 6;
1846 buf[47] = int_to_itoa64 (l & 0x3f);
1847
1848 l = (digest[38] << 0) | (digest[37] << 8) | (digest[36] << 16);
1849
1850 buf[48] = int_to_itoa64 (l & 0x3f); l >>= 6;
1851 buf[49] = int_to_itoa64 (l & 0x3f); l >>= 6;
1852 buf[50] = int_to_itoa64 (l & 0x3f); l >>= 6;
1853 buf[51] = int_to_itoa64 (l & 0x3f);
1854
1855 l = (digest[41] << 0) | (digest[40] << 8) | (digest[39] << 16);
1856
1857 buf[52] = int_to_itoa64 (l & 0x3f); l >>= 6;
1858 buf[53] = int_to_itoa64 (l & 0x3f); l >>= 6;
1859 buf[54] = int_to_itoa64 (l & 0x3f); l >>= 6;
1860 buf[55] = int_to_itoa64 (l & 0x3f);
1861
1862 l = (digest[44] << 0) | (digest[43] << 8) | (digest[42] << 16);
1863
1864 buf[56] = int_to_itoa64 (l & 0x3f); l >>= 6;
1865 buf[57] = int_to_itoa64 (l & 0x3f); l >>= 6;
1866 buf[58] = int_to_itoa64 (l & 0x3f); l >>= 6;
1867 buf[59] = int_to_itoa64 (l & 0x3f);
1868
1869 l = (digest[47] << 0) | (digest[46] << 8) | (digest[45] << 16);
1870
1871 buf[60] = int_to_itoa64 (l & 0x3f); l >>= 6;
1872 buf[61] = int_to_itoa64 (l & 0x3f); l >>= 6;
1873 buf[62] = int_to_itoa64 (l & 0x3f); l >>= 6;
1874 buf[63] = int_to_itoa64 (l & 0x3f);
1875
1876 l = (digest[50] << 0) | (digest[49] << 8) | (digest[48] << 16);
1877
1878 buf[64] = int_to_itoa64 (l & 0x3f); l >>= 6;
1879 buf[65] = int_to_itoa64 (l & 0x3f); l >>= 6;
1880 buf[66] = int_to_itoa64 (l & 0x3f); l >>= 6;
1881 buf[67] = int_to_itoa64 (l & 0x3f);
1882
1883 l = (digest[53] << 0) | (digest[52] << 8) | (digest[51] << 16);
1884
1885 buf[68] = int_to_itoa64 (l & 0x3f); l >>= 6;
1886 buf[69] = int_to_itoa64 (l & 0x3f); l >>= 6;
1887 buf[70] = int_to_itoa64 (l & 0x3f); l >>= 6;
1888 buf[71] = int_to_itoa64 (l & 0x3f);
1889
1890 l = (digest[56] << 0) | (digest[55] << 8) | (digest[54] << 16);
1891
1892 buf[72] = int_to_itoa64 (l & 0x3f); l >>= 6;
1893 buf[73] = int_to_itoa64 (l & 0x3f); l >>= 6;
1894 buf[74] = int_to_itoa64 (l & 0x3f); l >>= 6;
1895 buf[75] = int_to_itoa64 (l & 0x3f);
1896
1897 l = (digest[59] << 0) | (digest[58] << 8) | (digest[57] << 16);
1898
1899 buf[76] = int_to_itoa64 (l & 0x3f); l >>= 6;
1900 buf[77] = int_to_itoa64 (l & 0x3f); l >>= 6;
1901 buf[78] = int_to_itoa64 (l & 0x3f); l >>= 6;
1902 buf[79] = int_to_itoa64 (l & 0x3f);
1903
1904 l = (digest[62] << 0) | (digest[61] << 8) | (digest[60] << 16);
1905
1906 buf[80] = int_to_itoa64 (l & 0x3f); l >>= 6;
1907 buf[81] = int_to_itoa64 (l & 0x3f); l >>= 6;
1908 buf[82] = int_to_itoa64 (l & 0x3f); l >>= 6;
1909 buf[83] = int_to_itoa64 (l & 0x3f);
1910
1911 l = 0 | 0 | (digest[63] << 16);
1912
1913 buf[84] = int_to_itoa64 (l & 0x3f); l >>= 6;
1914 buf[85] = int_to_itoa64 (l & 0x3f); l >>= 6;
1915 }
1916
1917 void sha256crypt_decode (u8 digest[32], u8 buf[43])
1918 {
1919 int l;
1920
1921 l = itoa64_to_int (buf[ 0]) << 0;
1922 l |= itoa64_to_int (buf[ 1]) << 6;
1923 l |= itoa64_to_int (buf[ 2]) << 12;
1924 l |= itoa64_to_int (buf[ 3]) << 18;
1925
1926 digest[ 0] = (l >> 16) & 0xff;
1927 digest[10] = (l >> 8) & 0xff;
1928 digest[20] = (l >> 0) & 0xff;
1929
1930 l = itoa64_to_int (buf[ 4]) << 0;
1931 l |= itoa64_to_int (buf[ 5]) << 6;
1932 l |= itoa64_to_int (buf[ 6]) << 12;
1933 l |= itoa64_to_int (buf[ 7]) << 18;
1934
1935 digest[21] = (l >> 16) & 0xff;
1936 digest[ 1] = (l >> 8) & 0xff;
1937 digest[11] = (l >> 0) & 0xff;
1938
1939 l = itoa64_to_int (buf[ 8]) << 0;
1940 l |= itoa64_to_int (buf[ 9]) << 6;
1941 l |= itoa64_to_int (buf[10]) << 12;
1942 l |= itoa64_to_int (buf[11]) << 18;
1943
1944 digest[12] = (l >> 16) & 0xff;
1945 digest[22] = (l >> 8) & 0xff;
1946 digest[ 2] = (l >> 0) & 0xff;
1947
1948 l = itoa64_to_int (buf[12]) << 0;
1949 l |= itoa64_to_int (buf[13]) << 6;
1950 l |= itoa64_to_int (buf[14]) << 12;
1951 l |= itoa64_to_int (buf[15]) << 18;
1952
1953 digest[ 3] = (l >> 16) & 0xff;
1954 digest[13] = (l >> 8) & 0xff;
1955 digest[23] = (l >> 0) & 0xff;
1956
1957 l = itoa64_to_int (buf[16]) << 0;
1958 l |= itoa64_to_int (buf[17]) << 6;
1959 l |= itoa64_to_int (buf[18]) << 12;
1960 l |= itoa64_to_int (buf[19]) << 18;
1961
1962 digest[24] = (l >> 16) & 0xff;
1963 digest[ 4] = (l >> 8) & 0xff;
1964 digest[14] = (l >> 0) & 0xff;
1965
1966 l = itoa64_to_int (buf[20]) << 0;
1967 l |= itoa64_to_int (buf[21]) << 6;
1968 l |= itoa64_to_int (buf[22]) << 12;
1969 l |= itoa64_to_int (buf[23]) << 18;
1970
1971 digest[15] = (l >> 16) & 0xff;
1972 digest[25] = (l >> 8) & 0xff;
1973 digest[ 5] = (l >> 0) & 0xff;
1974
1975 l = itoa64_to_int (buf[24]) << 0;
1976 l |= itoa64_to_int (buf[25]) << 6;
1977 l |= itoa64_to_int (buf[26]) << 12;
1978 l |= itoa64_to_int (buf[27]) << 18;
1979
1980 digest[ 6] = (l >> 16) & 0xff;
1981 digest[16] = (l >> 8) & 0xff;
1982 digest[26] = (l >> 0) & 0xff;
1983
1984 l = itoa64_to_int (buf[28]) << 0;
1985 l |= itoa64_to_int (buf[29]) << 6;
1986 l |= itoa64_to_int (buf[30]) << 12;
1987 l |= itoa64_to_int (buf[31]) << 18;
1988
1989 digest[27] = (l >> 16) & 0xff;
1990 digest[ 7] = (l >> 8) & 0xff;
1991 digest[17] = (l >> 0) & 0xff;
1992
1993 l = itoa64_to_int (buf[32]) << 0;
1994 l |= itoa64_to_int (buf[33]) << 6;
1995 l |= itoa64_to_int (buf[34]) << 12;
1996 l |= itoa64_to_int (buf[35]) << 18;
1997
1998 digest[18] = (l >> 16) & 0xff;
1999 digest[28] = (l >> 8) & 0xff;
2000 digest[ 8] = (l >> 0) & 0xff;
2001
2002 l = itoa64_to_int (buf[36]) << 0;
2003 l |= itoa64_to_int (buf[37]) << 6;
2004 l |= itoa64_to_int (buf[38]) << 12;
2005 l |= itoa64_to_int (buf[39]) << 18;
2006
2007 digest[ 9] = (l >> 16) & 0xff;
2008 digest[19] = (l >> 8) & 0xff;
2009 digest[29] = (l >> 0) & 0xff;
2010
2011 l = itoa64_to_int (buf[40]) << 0;
2012 l |= itoa64_to_int (buf[41]) << 6;
2013 l |= itoa64_to_int (buf[42]) << 12;
2014
2015 digest[31] = (l >> 8) & 0xff;
2016 digest[30] = (l >> 0) & 0xff;
2017 }
2018
2019 void sha256crypt_encode (u8 digest[32], u8 buf[43])
2020 {
2021 int l;
2022
2023 l = (digest[ 0] << 16) | (digest[10] << 8) | (digest[20] << 0);
2024
2025 buf[ 0] = int_to_itoa64 (l & 0x3f); l >>= 6;
2026 buf[ 1] = int_to_itoa64 (l & 0x3f); l >>= 6;
2027 buf[ 2] = int_to_itoa64 (l & 0x3f); l >>= 6;
2028 buf[ 3] = int_to_itoa64 (l & 0x3f); l >>= 6;
2029
2030 l = (digest[21] << 16) | (digest[ 1] << 8) | (digest[11] << 0);
2031
2032 buf[ 4] = int_to_itoa64 (l & 0x3f); l >>= 6;
2033 buf[ 5] = int_to_itoa64 (l & 0x3f); l >>= 6;
2034 buf[ 6] = int_to_itoa64 (l & 0x3f); l >>= 6;
2035 buf[ 7] = int_to_itoa64 (l & 0x3f); l >>= 6;
2036
2037 l = (digest[12] << 16) | (digest[22] << 8) | (digest[ 2] << 0);
2038
2039 buf[ 8] = int_to_itoa64 (l & 0x3f); l >>= 6;
2040 buf[ 9] = int_to_itoa64 (l & 0x3f); l >>= 6;
2041 buf[10] = int_to_itoa64 (l & 0x3f); l >>= 6;
2042 buf[11] = int_to_itoa64 (l & 0x3f); l >>= 6;
2043
2044 l = (digest[ 3] << 16) | (digest[13] << 8) | (digest[23] << 0);
2045
2046 buf[12] = int_to_itoa64 (l & 0x3f); l >>= 6;
2047 buf[13] = int_to_itoa64 (l & 0x3f); l >>= 6;
2048 buf[14] = int_to_itoa64 (l & 0x3f); l >>= 6;
2049 buf[15] = int_to_itoa64 (l & 0x3f); l >>= 6;
2050
2051 l = (digest[24] << 16) | (digest[ 4] << 8) | (digest[14] << 0);
2052
2053 buf[16] = int_to_itoa64 (l & 0x3f); l >>= 6;
2054 buf[17] = int_to_itoa64 (l & 0x3f); l >>= 6;
2055 buf[18] = int_to_itoa64 (l & 0x3f); l >>= 6;
2056 buf[19] = int_to_itoa64 (l & 0x3f); l >>= 6;
2057
2058 l = (digest[15] << 16) | (digest[25] << 8) | (digest[ 5] << 0);
2059
2060 buf[20] = int_to_itoa64 (l & 0x3f); l >>= 6;
2061 buf[21] = int_to_itoa64 (l & 0x3f); l >>= 6;
2062 buf[22] = int_to_itoa64 (l & 0x3f); l >>= 6;
2063 buf[23] = int_to_itoa64 (l & 0x3f); l >>= 6;
2064
2065 l = (digest[ 6] << 16) | (digest[16] << 8) | (digest[26] << 0);
2066
2067 buf[24] = int_to_itoa64 (l & 0x3f); l >>= 6;
2068 buf[25] = int_to_itoa64 (l & 0x3f); l >>= 6;
2069 buf[26] = int_to_itoa64 (l & 0x3f); l >>= 6;
2070 buf[27] = int_to_itoa64 (l & 0x3f); l >>= 6;
2071
2072 l = (digest[27] << 16) | (digest[ 7] << 8) | (digest[17] << 0);
2073
2074 buf[28] = int_to_itoa64 (l & 0x3f); l >>= 6;
2075 buf[29] = int_to_itoa64 (l & 0x3f); l >>= 6;
2076 buf[30] = int_to_itoa64 (l & 0x3f); l >>= 6;
2077 buf[31] = int_to_itoa64 (l & 0x3f); l >>= 6;
2078
2079 l = (digest[18] << 16) | (digest[28] << 8) | (digest[ 8] << 0);
2080
2081 buf[32] = int_to_itoa64 (l & 0x3f); l >>= 6;
2082 buf[33] = int_to_itoa64 (l & 0x3f); l >>= 6;
2083 buf[34] = int_to_itoa64 (l & 0x3f); l >>= 6;
2084 buf[35] = int_to_itoa64 (l & 0x3f); l >>= 6;
2085
2086 l = (digest[ 9] << 16) | (digest[19] << 8) | (digest[29] << 0);
2087
2088 buf[36] = int_to_itoa64 (l & 0x3f); l >>= 6;
2089 buf[37] = int_to_itoa64 (l & 0x3f); l >>= 6;
2090 buf[38] = int_to_itoa64 (l & 0x3f); l >>= 6;
2091 buf[39] = int_to_itoa64 (l & 0x3f); l >>= 6;
2092
2093 l = 0 | (digest[31] << 8) | (digest[30] << 0);
2094
2095 buf[40] = int_to_itoa64 (l & 0x3f); l >>= 6;
2096 buf[41] = int_to_itoa64 (l & 0x3f); l >>= 6;
2097 buf[42] = int_to_itoa64 (l & 0x3f);
2098 }
2099
2100 void drupal7_decode (u8 digest[64], u8 buf[44])
2101 {
2102 int l;
2103
2104 l = itoa64_to_int (buf[ 0]) << 0;
2105 l |= itoa64_to_int (buf[ 1]) << 6;
2106 l |= itoa64_to_int (buf[ 2]) << 12;
2107 l |= itoa64_to_int (buf[ 3]) << 18;
2108
2109 digest[ 0] = (l >> 0) & 0xff;
2110 digest[ 1] = (l >> 8) & 0xff;
2111 digest[ 2] = (l >> 16) & 0xff;
2112
2113 l = itoa64_to_int (buf[ 4]) << 0;
2114 l |= itoa64_to_int (buf[ 5]) << 6;
2115 l |= itoa64_to_int (buf[ 6]) << 12;
2116 l |= itoa64_to_int (buf[ 7]) << 18;
2117
2118 digest[ 3] = (l >> 0) & 0xff;
2119 digest[ 4] = (l >> 8) & 0xff;
2120 digest[ 5] = (l >> 16) & 0xff;
2121
2122 l = itoa64_to_int (buf[ 8]) << 0;
2123 l |= itoa64_to_int (buf[ 9]) << 6;
2124 l |= itoa64_to_int (buf[10]) << 12;
2125 l |= itoa64_to_int (buf[11]) << 18;
2126
2127 digest[ 6] = (l >> 0) & 0xff;
2128 digest[ 7] = (l >> 8) & 0xff;
2129 digest[ 8] = (l >> 16) & 0xff;
2130
2131 l = itoa64_to_int (buf[12]) << 0;
2132 l |= itoa64_to_int (buf[13]) << 6;
2133 l |= itoa64_to_int (buf[14]) << 12;
2134 l |= itoa64_to_int (buf[15]) << 18;
2135
2136 digest[ 9] = (l >> 0) & 0xff;
2137 digest[10] = (l >> 8) & 0xff;
2138 digest[11] = (l >> 16) & 0xff;
2139
2140 l = itoa64_to_int (buf[16]) << 0;
2141 l |= itoa64_to_int (buf[17]) << 6;
2142 l |= itoa64_to_int (buf[18]) << 12;
2143 l |= itoa64_to_int (buf[19]) << 18;
2144
2145 digest[12] = (l >> 0) & 0xff;
2146 digest[13] = (l >> 8) & 0xff;
2147 digest[14] = (l >> 16) & 0xff;
2148
2149 l = itoa64_to_int (buf[20]) << 0;
2150 l |= itoa64_to_int (buf[21]) << 6;
2151 l |= itoa64_to_int (buf[22]) << 12;
2152 l |= itoa64_to_int (buf[23]) << 18;
2153
2154 digest[15] = (l >> 0) & 0xff;
2155 digest[16] = (l >> 8) & 0xff;
2156 digest[17] = (l >> 16) & 0xff;
2157
2158 l = itoa64_to_int (buf[24]) << 0;
2159 l |= itoa64_to_int (buf[25]) << 6;
2160 l |= itoa64_to_int (buf[26]) << 12;
2161 l |= itoa64_to_int (buf[27]) << 18;
2162
2163 digest[18] = (l >> 0) & 0xff;
2164 digest[19] = (l >> 8) & 0xff;
2165 digest[20] = (l >> 16) & 0xff;
2166
2167 l = itoa64_to_int (buf[28]) << 0;
2168 l |= itoa64_to_int (buf[29]) << 6;
2169 l |= itoa64_to_int (buf[30]) << 12;
2170 l |= itoa64_to_int (buf[31]) << 18;
2171
2172 digest[21] = (l >> 0) & 0xff;
2173 digest[22] = (l >> 8) & 0xff;
2174 digest[23] = (l >> 16) & 0xff;
2175
2176 l = itoa64_to_int (buf[32]) << 0;
2177 l |= itoa64_to_int (buf[33]) << 6;
2178 l |= itoa64_to_int (buf[34]) << 12;
2179 l |= itoa64_to_int (buf[35]) << 18;
2180
2181 digest[24] = (l >> 0) & 0xff;
2182 digest[25] = (l >> 8) & 0xff;
2183 digest[26] = (l >> 16) & 0xff;
2184
2185 l = itoa64_to_int (buf[36]) << 0;
2186 l |= itoa64_to_int (buf[37]) << 6;
2187 l |= itoa64_to_int (buf[38]) << 12;
2188 l |= itoa64_to_int (buf[39]) << 18;
2189
2190 digest[27] = (l >> 0) & 0xff;
2191 digest[28] = (l >> 8) & 0xff;
2192 digest[29] = (l >> 16) & 0xff;
2193
2194 l = itoa64_to_int (buf[40]) << 0;
2195 l |= itoa64_to_int (buf[41]) << 6;
2196 l |= itoa64_to_int (buf[42]) << 12;
2197 l |= itoa64_to_int (buf[43]) << 18;
2198
2199 digest[30] = (l >> 0) & 0xff;
2200 digest[31] = (l >> 8) & 0xff;
2201 digest[32] = (l >> 16) & 0xff;
2202
2203 digest[33] = 0;
2204 digest[34] = 0;
2205 digest[35] = 0;
2206 digest[36] = 0;
2207 digest[37] = 0;
2208 digest[38] = 0;
2209 digest[39] = 0;
2210 digest[40] = 0;
2211 digest[41] = 0;
2212 digest[42] = 0;
2213 digest[43] = 0;
2214 digest[44] = 0;
2215 digest[45] = 0;
2216 digest[46] = 0;
2217 digest[47] = 0;
2218 digest[48] = 0;
2219 digest[49] = 0;
2220 digest[50] = 0;
2221 digest[51] = 0;
2222 digest[52] = 0;
2223 digest[53] = 0;
2224 digest[54] = 0;
2225 digest[55] = 0;
2226 digest[56] = 0;
2227 digest[57] = 0;
2228 digest[58] = 0;
2229 digest[59] = 0;
2230 digest[60] = 0;
2231 digest[61] = 0;
2232 digest[62] = 0;
2233 digest[63] = 0;
2234 }
2235
2236 void drupal7_encode (u8 digest[64], u8 buf[43])
2237 {
2238 int l;
2239
2240 l = (digest[ 0] << 0) | (digest[ 1] << 8) | (digest[ 2] << 16);
2241
2242 buf[ 0] = int_to_itoa64 (l & 0x3f); l >>= 6;
2243 buf[ 1] = int_to_itoa64 (l & 0x3f); l >>= 6;
2244 buf[ 2] = int_to_itoa64 (l & 0x3f); l >>= 6;
2245 buf[ 3] = int_to_itoa64 (l & 0x3f);
2246
2247 l = (digest[ 3] << 0) | (digest[ 4] << 8) | (digest[ 5] << 16);
2248
2249 buf[ 4] = int_to_itoa64 (l & 0x3f); l >>= 6;
2250 buf[ 5] = int_to_itoa64 (l & 0x3f); l >>= 6;
2251 buf[ 6] = int_to_itoa64 (l & 0x3f); l >>= 6;
2252 buf[ 7] = int_to_itoa64 (l & 0x3f);
2253
2254 l = (digest[ 6] << 0) | (digest[ 7] << 8) | (digest[ 8] << 16);
2255
2256 buf[ 8] = int_to_itoa64 (l & 0x3f); l >>= 6;
2257 buf[ 9] = int_to_itoa64 (l & 0x3f); l >>= 6;
2258 buf[10] = int_to_itoa64 (l & 0x3f); l >>= 6;
2259 buf[11] = int_to_itoa64 (l & 0x3f);
2260
2261 l = (digest[ 9] << 0) | (digest[10] << 8) | (digest[11] << 16);
2262
2263 buf[12] = int_to_itoa64 (l & 0x3f); l >>= 6;
2264 buf[13] = int_to_itoa64 (l & 0x3f); l >>= 6;
2265 buf[14] = int_to_itoa64 (l & 0x3f); l >>= 6;
2266 buf[15] = int_to_itoa64 (l & 0x3f);
2267
2268 l = (digest[12] << 0) | (digest[13] << 8) | (digest[14] << 16);
2269
2270 buf[16] = int_to_itoa64 (l & 0x3f); l >>= 6;
2271 buf[17] = int_to_itoa64 (l & 0x3f); l >>= 6;
2272 buf[18] = int_to_itoa64 (l & 0x3f); l >>= 6;
2273 buf[19] = int_to_itoa64 (l & 0x3f);
2274
2275 l = (digest[15] << 0) | (digest[16] << 8) | (digest[17] << 16);
2276
2277 buf[20] = int_to_itoa64 (l & 0x3f); l >>= 6;
2278 buf[21] = int_to_itoa64 (l & 0x3f); l >>= 6;
2279 buf[22] = int_to_itoa64 (l & 0x3f); l >>= 6;
2280 buf[23] = int_to_itoa64 (l & 0x3f);
2281
2282 l = (digest[18] << 0) | (digest[19] << 8) | (digest[20] << 16);
2283
2284 buf[24] = int_to_itoa64 (l & 0x3f); l >>= 6;
2285 buf[25] = int_to_itoa64 (l & 0x3f); l >>= 6;
2286 buf[26] = int_to_itoa64 (l & 0x3f); l >>= 6;
2287 buf[27] = int_to_itoa64 (l & 0x3f);
2288
2289 l = (digest[21] << 0) | (digest[22] << 8) | (digest[23] << 16);
2290
2291 buf[28] = int_to_itoa64 (l & 0x3f); l >>= 6;
2292 buf[29] = int_to_itoa64 (l & 0x3f); l >>= 6;
2293 buf[30] = int_to_itoa64 (l & 0x3f); l >>= 6;
2294 buf[31] = int_to_itoa64 (l & 0x3f);
2295
2296 l = (digest[24] << 0) | (digest[25] << 8) | (digest[26] << 16);
2297
2298 buf[32] = int_to_itoa64 (l & 0x3f); l >>= 6;
2299 buf[33] = int_to_itoa64 (l & 0x3f); l >>= 6;
2300 buf[34] = int_to_itoa64 (l & 0x3f); l >>= 6;
2301 buf[35] = int_to_itoa64 (l & 0x3f);
2302
2303 l = (digest[27] << 0) | (digest[28] << 8) | (digest[29] << 16);
2304
2305 buf[36] = int_to_itoa64 (l & 0x3f); l >>= 6;
2306 buf[37] = int_to_itoa64 (l & 0x3f); l >>= 6;
2307 buf[38] = int_to_itoa64 (l & 0x3f); l >>= 6;
2308 buf[39] = int_to_itoa64 (l & 0x3f);
2309
2310 l = (digest[30] << 0) | (digest[31] << 8) | (digest[32] << 16);
2311
2312 buf[40] = int_to_itoa64 (l & 0x3f); l >>= 6;
2313 buf[41] = int_to_itoa64 (l & 0x3f); l >>= 6;
2314 buf[42] = int_to_itoa64 (l & 0x3f); l >>= 6;
2315 //buf[43] = int_to_itoa64 (l & 0x3f);
2316 }
2317
2318 /**
2319 * tty
2320 */
2321
2322 #ifdef LINUX
2323 static struct termio savemodes;
2324 static int havemodes = 0;
2325
2326 int tty_break()
2327 {
2328 struct termio modmodes;
2329
2330 if (ioctl (fileno (stdin), TCGETA, &savemodes) < 0) return -1;
2331
2332 havemodes = 1;
2333
2334 modmodes = savemodes;
2335 modmodes.c_lflag &= ~ICANON;
2336 modmodes.c_cc[VMIN] = 1;
2337 modmodes.c_cc[VTIME] = 0;
2338
2339 return ioctl (fileno (stdin), TCSETAW, &modmodes);
2340 }
2341
2342 int tty_getchar()
2343 {
2344 fd_set rfds;
2345
2346 FD_ZERO (&rfds);
2347
2348 FD_SET (fileno (stdin), &rfds);
2349
2350 struct timeval tv;
2351
2352 tv.tv_sec = 1;
2353 tv.tv_usec = 0;
2354
2355 int retval = select (1, &rfds, NULL, NULL, &tv);
2356
2357 if (retval == 0) return 0;
2358 if (retval == -1) return -1;
2359
2360 return getchar();
2361 }
2362
2363 int tty_fix()
2364 {
2365 if (!havemodes) return 0;
2366
2367 return ioctl (fileno (stdin), TCSETAW, &savemodes);
2368 }
2369 #endif
2370
2371 #ifdef OSX
2372 static struct termios savemodes;
2373 static int havemodes = 0;
2374
2375 int tty_break()
2376 {
2377 struct termios modmodes;
2378
2379 if (ioctl (fileno (stdin), TIOCGETA, &savemodes) < 0) return -1;
2380
2381 havemodes = 1;
2382
2383 modmodes = savemodes;
2384 modmodes.c_lflag &= ~ICANON;
2385 modmodes.c_cc[VMIN] = 1;
2386 modmodes.c_cc[VTIME] = 0;
2387
2388 return ioctl (fileno (stdin), TIOCSETAW, &modmodes);
2389 }
2390
2391 int tty_getchar()
2392 {
2393 fd_set rfds;
2394
2395 FD_ZERO (&rfds);
2396
2397 FD_SET (fileno (stdin), &rfds);
2398
2399 struct timeval tv;
2400
2401 tv.tv_sec = 1;
2402 tv.tv_usec = 0;
2403
2404 int retval = select (1, &rfds, NULL, NULL, &tv);
2405
2406 if (retval == 0) return 0;
2407 if (retval == -1) return -1;
2408
2409 return getchar();
2410 }
2411
2412 int tty_fix()
2413 {
2414 if (!havemodes) return 0;
2415
2416 return ioctl (fileno (stdin), TIOCSETAW, &savemodes);
2417 }
2418 #endif
2419
2420 #ifdef WIN
2421 static DWORD saveMode = 0;
2422
2423 int tty_break()
2424 {
2425 HANDLE stdinHandle = GetStdHandle (STD_INPUT_HANDLE);
2426
2427 GetConsoleMode (stdinHandle, &saveMode);
2428 SetConsoleMode (stdinHandle, ENABLE_PROCESSED_INPUT);
2429
2430 return 0;
2431 }
2432
2433 int tty_getchar()
2434 {
2435 HANDLE stdinHandle = GetStdHandle (STD_INPUT_HANDLE);
2436
2437 DWORD rc = WaitForSingleObject (stdinHandle, 1000);
2438
2439 if (rc == WAIT_TIMEOUT) return 0;
2440 if (rc == WAIT_ABANDONED) return -1;
2441 if (rc == WAIT_FAILED) return -1;
2442
2443 // The whole ReadConsoleInput () part is a workaround.
2444 // For some unknown reason, maybe a mingw bug, a random signal
2445 // is sent to stdin which unblocks WaitForSingleObject () and sets rc 0.
2446 // Then it wants to read with getche () a keyboard input
2447 // which has never been made.
2448
2449 INPUT_RECORD buf[100];
2450
2451 DWORD num = 0;
2452
2453 memset (buf, 0, sizeof (buf));
2454
2455 ReadConsoleInput (stdinHandle, buf, 100, &num);
2456
2457 FlushConsoleInputBuffer (stdinHandle);
2458
2459 for (uint i = 0; i < num; i++)
2460 {
2461 if (buf[i].EventType != KEY_EVENT) continue;
2462
2463 KEY_EVENT_RECORD KeyEvent = buf[i].Event.KeyEvent;
2464
2465 if (KeyEvent.bKeyDown != TRUE) continue;
2466
2467 return KeyEvent.uChar.AsciiChar;
2468 }
2469
2470 return 0;
2471 }
2472
2473 int tty_fix()
2474 {
2475 HANDLE stdinHandle = GetStdHandle (STD_INPUT_HANDLE);
2476
2477 SetConsoleMode (stdinHandle, saveMode);
2478
2479 return 0;
2480 }
2481 #endif
2482
2483 /**
2484 * mem alloc
2485 */
2486
2487 #define MSG_ENOMEM "Insufficient memory available"
2488
2489 void *mycalloc (size_t nmemb, size_t size)
2490 {
2491 void *p = calloc (nmemb, size);
2492
2493 if (p == NULL)
2494 {
2495 log_error ("ERROR: %s", MSG_ENOMEM);
2496
2497 exit (-1);
2498 }
2499
2500 return (p);
2501 }
2502
2503 void *mymalloc (size_t size)
2504 {
2505 void *p = malloc (size);
2506
2507 if (p == NULL)
2508 {
2509 log_error ("ERROR: %s", MSG_ENOMEM);
2510
2511 exit (-1);
2512 }
2513
2514 memset (p, 0, size);
2515
2516 return (p);
2517 }
2518
2519 void myfree (void *ptr)
2520 {
2521 if (ptr == NULL) return;
2522
2523 free (ptr);
2524 }
2525
2526 void *myrealloc (void *ptr, size_t oldsz, size_t add)
2527 {
2528 void *p = realloc (ptr, oldsz + add);
2529
2530 if (p == NULL)
2531 {
2532 log_error ("ERROR: %s", MSG_ENOMEM);
2533
2534 exit (-1);
2535 }
2536
2537 memset ((char *) p + oldsz, 0, add);
2538
2539 return (p);
2540 }
2541
2542 char *mystrdup (const char *s)
2543 {
2544 const size_t len = strlen (s);
2545
2546 char *b = (char *) mymalloc (len + 1);
2547
2548 memcpy (b, s, len);
2549
2550 return (b);
2551 }
2552
2553 FILE *logfile_open (char *logfile)
2554 {
2555 FILE *fp = fopen (logfile, "ab");
2556
2557 if (fp == NULL)
2558 {
2559 fp = stdout;
2560 }
2561
2562 return fp;
2563 }
2564
2565 void logfile_close (FILE *fp)
2566 {
2567 if (fp == stdout) return;
2568
2569 fclose (fp);
2570 }
2571
2572 void logfile_append (const char *fmt, ...)
2573 {
2574 if (data.logfile_disable == 1) return;
2575
2576 FILE *fp = logfile_open (data.logfile);
2577
2578 va_list ap;
2579
2580 va_start (ap, fmt);
2581
2582 vfprintf (fp, fmt, ap);
2583
2584 va_end (ap);
2585
2586 fputc ('\n', fp);
2587
2588 fflush (fp);
2589
2590 logfile_close (fp);
2591 }
2592
2593 int logfile_generate_id ()
2594 {
2595 const int n = rand ();
2596
2597 time_t t;
2598
2599 time (&t);
2600
2601 return t + n;
2602 }
2603
2604 char *logfile_generate_topid ()
2605 {
2606 const int id = logfile_generate_id ();
2607
2608 char *topid = (char *) mymalloc (1 + 16 + 1);
2609
2610 snprintf (topid, 1 + 16, "TOP%08x", id);
2611
2612 return topid;
2613 }
2614
2615 char *logfile_generate_subid ()
2616 {
2617 const int id = logfile_generate_id ();
2618
2619 char *subid = (char *) mymalloc (1 + 16 + 1);
2620
2621 snprintf (subid, 1 + 16, "SUB%08x", id);
2622
2623 return subid;
2624 }
2625
2626 /**
2627 * system
2628 */
2629
2630 #if F_SETLKW
2631 void lock_file (FILE *fp)
2632 {
2633 struct flock lock;
2634
2635 memset (&lock, 0, sizeof (struct flock));
2636
2637 lock.l_type = F_WRLCK;
2638 while (fcntl(fileno(fp), F_SETLKW, &lock))
2639 {
2640 if (errno != EINTR)
2641 {
2642 log_error ("ERROR: failed acquiring write lock: %s", strerror (errno));
2643
2644 exit (-1);
2645 }
2646 }
2647 }
2648
2649 void unlock_file (FILE *fp)
2650 {
2651 struct flock lock;
2652
2653 memset (&lock, 0, sizeof (struct flock));
2654
2655 lock.l_type = F_UNLCK;
2656 fcntl(fileno(fp), F_SETLK, &lock);
2657 }
2658 #endif // F_SETLKW
2659
2660 #ifdef _WIN
2661 void fsync (int fd)
2662 {
2663 HANDLE h = (HANDLE) _get_osfhandle (fd);
2664
2665 FlushFileBuffers (h);
2666 }
2667 #endif
2668
2669 /**
2670 * thermal
2671 */
2672
2673 #ifdef HAVE_HWMON
2674 #if defined(_WIN) && defined(HAVE_NVAPI)
2675 int hm_get_adapter_index_nv (HM_ADAPTER_NV nvGPUHandle[DEVICES_MAX])
2676 {
2677 NvU32 pGpuCount;
2678
2679 if (hm_NvAPI_EnumPhysicalGPUs (data.hm_nv, nvGPUHandle, &pGpuCount) != NVAPI_OK) return (0);
2680
2681 if (pGpuCount == 0)
2682 {
2683 log_info ("WARN: No NvAPI adapters found");
2684
2685 return (0);
2686 }
2687
2688 return (pGpuCount);
2689 }
2690 #endif // _WIN && HAVE_NVAPI
2691
2692 #if defined(LINUX) && defined(HAVE_NVML)
2693 int hm_get_adapter_index_nv (HM_ADAPTER_NV nvGPUHandle[DEVICES_MAX])
2694 {
2695 int pGpuCount = 0;
2696
2697 for (uint i = 0; i < DEVICES_MAX; i++)
2698 {
2699 if (hm_NVML_nvmlDeviceGetHandleByIndex (data.hm_nv, 1, i, &nvGPUHandle[i]) != NVML_SUCCESS) break;
2700
2701 // can be used to determine if the device by index matches the cuda device by index
2702 // char name[100]; memset (name, 0, sizeof (name));
2703 // hm_NVML_nvmlDeviceGetName (data.hm_nv, nvGPUHandle[i], name, sizeof (name) - 1);
2704
2705 pGpuCount++;
2706 }
2707
2708 if (pGpuCount == 0)
2709 {
2710 log_info ("WARN: No NVML adapters found");
2711
2712 return (0);
2713 }
2714
2715 return (pGpuCount);
2716 }
2717 #endif // LINUX && HAVE_NVML
2718
2719 #ifdef HAVE_ADL
2720 int get_adapters_num_amd (void *adl, int *iNumberAdapters)
2721 {
2722 if (hm_ADL_Adapter_NumberOfAdapters_Get ((ADL_PTR *) adl, iNumberAdapters) != ADL_OK) return -1;
2723
2724 if (iNumberAdapters == 0)
2725 {
2726 log_info ("WARN: No ADL adapters found.");
2727
2728 return -1;
2729 }
2730
2731 return 0;
2732 }
2733
2734 /*
2735 int hm_show_performance_level (HM_LIB hm_dll, int iAdapterIndex)
2736 {
2737 ADLODPerformanceLevels *lpOdPerformanceLevels = NULL;
2738 ADLODParameters lpOdParameters;
2739
2740 lpOdParameters.iSize = sizeof (ADLODParameters);
2741 size_t plevels_size = 0;
2742
2743 if (hm_ADL_Overdrive_ODParameters_Get (hm_dll, iAdapterIndex, &lpOdParameters) != ADL_OK) return -1;
2744
2745 log_info ("[DEBUG] %s, adapter %d performance level (%d) : %s %s",
2746 __func__, iAdapterIndex,
2747 lpOdParameters.iNumberOfPerformanceLevels,
2748 (lpOdParameters.iActivityReportingSupported) ? "activity reporting" : "",
2749 (lpOdParameters.iDiscretePerformanceLevels) ? "discrete performance levels" : "performance ranges");
2750
2751 plevels_size = sizeof (ADLODPerformanceLevels) + sizeof (ADLODPerformanceLevel) * (lpOdParameters.iNumberOfPerformanceLevels - 1);
2752
2753 lpOdPerformanceLevels = (ADLODPerformanceLevels *) mymalloc (plevels_size);
2754
2755 lpOdPerformanceLevels->iSize = sizeof (ADLODPerformanceLevels) + sizeof (ADLODPerformanceLevel) * (lpOdParameters.iNumberOfPerformanceLevels - 1);
2756
2757 if (hm_ADL_Overdrive_ODPerformanceLevels_Get (hm_dll, iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK) return -1;
2758
2759 for (int j = 0; j < lpOdParameters.iNumberOfPerformanceLevels; j++)
2760 log_info ("[DEBUG] %s, adapter %d, level %d : engine %d, memory %d, voltage: %d",
2761 __func__, iAdapterIndex, j,
2762 lpOdPerformanceLevels->aLevels[j].iEngineClock / 100, lpOdPerformanceLevels->aLevels[j].iMemoryClock / 100, lpOdPerformanceLevels->aLevels[j].iVddc);
2763
2764 myfree (lpOdPerformanceLevels);
2765
2766 return 0;
2767 }
2768 */
2769
2770 LPAdapterInfo hm_get_adapter_info_amd (void *adl, int iNumberAdapters)
2771 {
2772 size_t AdapterInfoSize = iNumberAdapters * sizeof (AdapterInfo);
2773
2774 LPAdapterInfo lpAdapterInfo = (LPAdapterInfo) mymalloc (AdapterInfoSize);
2775
2776 if (hm_ADL_Adapter_AdapterInfo_Get ((ADL_PTR *) adl, lpAdapterInfo, AdapterInfoSize) != ADL_OK) return NULL;
2777
2778 return lpAdapterInfo;
2779 }
2780
2781 /*
2782 //
2783 // does not help at all, since AMD does not assign different bus id, device id when we have multi GPU setups
2784 //
2785
2786 int hm_get_opencl_device_index (hm_attrs_t *hm_device, uint num_adl_adapters, int bus_num, int dev_num)
2787 {
2788 u32 idx = -1;
2789
2790 for (uint i = 0; i < num_adl_adapters; i++)
2791 {
2792 int opencl_bus_num = hm_device[i].busid;
2793 int opencl_dev_num = hm_device[i].devid;
2794
2795 if ((opencl_bus_num == bus_num) && (opencl_dev_num == dev_num))
2796 {
2797 idx = i;
2798
2799 break;
2800 }
2801 }
2802
2803 if (idx >= DEVICES_MAX) return -1;
2804
2805 return idx;
2806 }
2807
2808 void hm_get_opencl_busid_devid (hm_attrs_t *hm_device, uint opencl_num_devices, cl_device_id *devices)
2809 {
2810 for (uint i = 0; i < opencl_num_devices; i++)
2811 {
2812 cl_device_topology_amd device_topology;
2813
2814 hc_clGetDeviceInfo (devices[i], CL_DEVICE_TOPOLOGY_AMD, sizeof (device_topology), &device_topology, NULL);
2815
2816 hm_device[i].busid = device_topology.pcie.bus;
2817 hm_device[i].devid = device_topology.pcie.device;
2818 }
2819 }
2820 */
2821
2822 void hm_sort_adl_adapters_by_busid_devid (u32 *valid_adl_device_list, int num_adl_adapters, LPAdapterInfo lpAdapterInfo)
2823 {
2824 // basically bubble sort
2825
2826 for (int i = 0; i < num_adl_adapters; i++)
2827 {
2828 for (int j = 0; j < num_adl_adapters - 1; j++)
2829 {
2830 // get info of adapter [x]
2831
2832 u32 adapter_index_x = valid_adl_device_list[j];
2833 AdapterInfo info_x = lpAdapterInfo[adapter_index_x];
2834
2835 u32 bus_num_x = info_x.iBusNumber;
2836 u32 dev_num_x = info_x.iDeviceNumber;
2837
2838 // get info of adapter [y]
2839
2840 u32 adapter_index_y = valid_adl_device_list[j + 1];
2841 AdapterInfo info_y = lpAdapterInfo[adapter_index_y];
2842
2843 u32 bus_num_y = info_y.iBusNumber;
2844 u32 dev_num_y = info_y.iDeviceNumber;
2845
2846 uint need_swap = 0;
2847
2848 if (bus_num_y < bus_num_x)
2849 {
2850 need_swap = 1;
2851 }
2852 else if (bus_num_y == bus_num_x)
2853 {
2854 if (dev_num_y < dev_num_x)
2855 {
2856 need_swap = 1;
2857 }
2858 }
2859
2860 if (need_swap == 1)
2861 {
2862 u32 temp = valid_adl_device_list[j + 1];
2863
2864 valid_adl_device_list[j + 1] = valid_adl_device_list[j];
2865 valid_adl_device_list[j + 0] = temp;
2866 }
2867 }
2868 }
2869 }
2870
2871 u32 *hm_get_list_valid_adl_adapters (int iNumberAdapters, int *num_adl_adapters, LPAdapterInfo lpAdapterInfo)
2872 {
2873 *num_adl_adapters = 0;
2874
2875 u32 *adl_adapters = NULL;
2876
2877 int *bus_numbers = NULL;
2878 int *device_numbers = NULL;
2879
2880 for (int i = 0; i < iNumberAdapters; i++)
2881 {
2882 AdapterInfo info = lpAdapterInfo[i];
2883
2884 if (strlen (info.strUDID) < 1) continue;
2885
2886 #ifdef WIN
2887 if (info.iVendorID != 1002) continue;
2888 #else
2889 if (info.iVendorID != 0x1002) continue;
2890 #endif
2891
2892 if (info.iBusNumber < 0) continue;
2893 if (info.iDeviceNumber < 0) continue;
2894
2895 int found = 0;
2896
2897 for (int pos = 0; pos < *num_adl_adapters; pos++)
2898 {
2899 if ((bus_numbers[pos] == info.iBusNumber) && (device_numbers[pos] == info.iDeviceNumber))
2900 {
2901 found = 1;
2902 break;
2903 }
2904 }
2905
2906 if (found) continue;
2907
2908 // add it to the list
2909
2910 adl_adapters = (u32 *) myrealloc (adl_adapters, (*num_adl_adapters) * sizeof (int), sizeof (int));
2911
2912 adl_adapters[*num_adl_adapters] = i;
2913
2914 // rest is just bookkeeping
2915
2916 bus_numbers = (int*) myrealloc (bus_numbers, (*num_adl_adapters) * sizeof (int), sizeof (int));
2917 device_numbers = (int*) myrealloc (device_numbers, (*num_adl_adapters) * sizeof (int), sizeof (int));
2918
2919 bus_numbers[*num_adl_adapters] = info.iBusNumber;
2920 device_numbers[*num_adl_adapters] = info.iDeviceNumber;
2921
2922 (*num_adl_adapters)++;
2923 }
2924
2925 myfree (bus_numbers);
2926 myfree (device_numbers);
2927
2928 // sort the list by increasing bus id, device id number
2929
2930 hm_sort_adl_adapters_by_busid_devid (adl_adapters, *num_adl_adapters, lpAdapterInfo);
2931
2932 return adl_adapters;
2933 }
2934
2935 int hm_check_fanspeed_control (void *adl, hm_attrs_t *hm_device, u32 *valid_adl_device_list, int num_adl_adapters, LPAdapterInfo lpAdapterInfo)
2936 {
2937 // loop through all valid devices
2938
2939 for (int i = 0; i < num_adl_adapters; i++)
2940 {
2941 u32 adapter_index = valid_adl_device_list[i];
2942
2943 // get AdapterInfo
2944
2945 AdapterInfo info = lpAdapterInfo[adapter_index];
2946
2947 // unfortunately this doesn't work since bus id and dev id are not unique
2948 // int opencl_device_index = hm_get_opencl_device_index (hm_device, num_adl_adapters, info.iBusNumber, info.iDeviceNumber);
2949 // if (opencl_device_index == -1) continue;
2950
2951 int opencl_device_index = i;
2952
2953 // if (hm_show_performance_level (adl, info.iAdapterIndex) != 0) return -1;
2954
2955 // get fanspeed info
2956
2957 if (hm_device[opencl_device_index].od_version == 5)
2958 {
2959 ADLFanSpeedInfo FanSpeedInfo;
2960
2961 memset (&FanSpeedInfo, 0, sizeof (ADLFanSpeedInfo));
2962
2963 FanSpeedInfo.iSize = sizeof (ADLFanSpeedInfo);
2964
2965 if (hm_ADL_Overdrive5_FanSpeedInfo_Get (adl, info.iAdapterIndex, 0, &FanSpeedInfo) != ADL_OK) return -1;
2966
2967 // check read and write capability in fanspeedinfo
2968
2969 if ((FanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_PERCENT_READ) &&
2970 (FanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_PERCENT_WRITE))
2971 {
2972 hm_device[opencl_device_index].fan_supported = 1;
2973 }
2974 else
2975 {
2976 hm_device[opencl_device_index].fan_supported = 0;
2977 }
2978 }
2979 else // od_version == 6
2980 {
2981 ADLOD6FanSpeedInfo faninfo;
2982
2983 memset (&faninfo, 0, sizeof (faninfo));
2984
2985 if (hm_ADL_Overdrive6_FanSpeed_Get (adl, info.iAdapterIndex, &faninfo) != ADL_OK) return -1;
2986
2987 // check read capability in fanspeedinfo
2988
2989 if (faninfo.iSpeedType & ADL_OD6_FANSPEED_TYPE_PERCENT)
2990 {
2991 hm_device[opencl_device_index].fan_supported = 1;
2992 }
2993 else
2994 {
2995 hm_device[opencl_device_index].fan_supported = 0;
2996 }
2997 }
2998 }
2999
3000 return 0;
3001 }
3002
3003 int hm_get_overdrive_version (void *adl, hm_attrs_t *hm_device, u32 *valid_adl_device_list, int num_adl_adapters, LPAdapterInfo lpAdapterInfo)
3004 {
3005 for (int i = 0; i < num_adl_adapters; i++)
3006 {
3007 u32 adapter_index = valid_adl_device_list[i];
3008
3009 // get AdapterInfo
3010
3011 AdapterInfo info = lpAdapterInfo[adapter_index];
3012
3013 // get overdrive version
3014
3015 int od_supported = 0;
3016 int od_enabled = 0;
3017 int od_version = 0;
3018
3019 if (hm_ADL_Overdrive_Caps (adl, info.iAdapterIndex, &od_supported, &od_enabled, &od_version) != ADL_OK) return -1;
3020
3021 // store the overdrive version in hm_device
3022
3023 // unfortunately this doesn't work since bus id and dev id are not unique
3024 // int opencl_device_index = hm_get_opencl_device_index (hm_device, num_adl_adapters, info.iBusNumber, info.iDeviceNumber);
3025 // if (opencl_device_index == -1) continue;
3026
3027 int opencl_device_index = i;
3028
3029 hm_device[opencl_device_index].od_version = od_version;
3030 }
3031
3032 return 0;
3033 }
3034
3035 int hm_get_adapter_index_amd (hm_attrs_t *hm_device, u32 *valid_adl_device_list, int num_adl_adapters, LPAdapterInfo lpAdapterInfo)
3036 {
3037 for (int i = 0; i < num_adl_adapters; i++)
3038 {
3039 u32 adapter_index = valid_adl_device_list[i];
3040
3041 // get AdapterInfo
3042
3043 AdapterInfo info = lpAdapterInfo[adapter_index];
3044
3045 // store the iAdapterIndex in hm_device
3046
3047 // unfortunately this doesn't work since bus id and dev id are not unique
3048 // int opencl_device_index = hm_get_opencl_device_index (hm_device, num_adl_adapters, info.iBusNumber, info.iDeviceNumber);
3049 // if (opencl_device_index == -1) continue;
3050
3051 int opencl_device_index = i;
3052
3053 hm_device[opencl_device_index].adapter_index.amd = info.iAdapterIndex;
3054 }
3055
3056 return num_adl_adapters;
3057 }
3058 #endif // HAVE_ADL
3059
3060 int hm_get_temperature_with_device_id (const uint device_id)
3061 {
3062 if ((data.devices_param[device_id].device_type & CL_DEVICE_TYPE_GPU) == 0) return -1;
3063
3064 #ifdef HAVE_ADL
3065 if (data.devices_param[device_id].vendor_id == VENDOR_ID_AMD)
3066 {
3067 if (data.hm_amd)
3068 {
3069 if (data.hm_device[device_id].od_version == 5)
3070 {
3071 ADLTemperature Temperature;
3072
3073 Temperature.iSize = sizeof (ADLTemperature);
3074
3075 if (hm_ADL_Overdrive5_Temperature_Get (data.hm_amd, data.hm_device[device_id].adapter_index.amd, 0, &Temperature) != ADL_OK) return -1;
3076
3077 return Temperature.iTemperature / 1000;
3078 }
3079 else if (data.hm_device[device_id].od_version == 6)
3080 {
3081 int Temperature = 0;
3082
3083 if (hm_ADL_Overdrive6_Temperature_Get (data.hm_amd, data.hm_device[device_id].adapter_index.amd, &Temperature) != ADL_OK) return -1;
3084
3085 return Temperature / 1000;
3086 }
3087 }
3088 }
3089 #endif
3090
3091 #if defined(HAVE_NVML) || defined(HAVE_NVAPI)
3092 if (data.devices_param[device_id].vendor_id == VENDOR_ID_NV)
3093 {
3094 #if defined(LINUX) && defined(HAVE_NVML)
3095 int temperature = 0;
3096
3097 hm_NVML_nvmlDeviceGetTemperature (data.hm_nv, data.hm_device[device_id].adapter_index.nv, NVML_TEMPERATURE_GPU, (uint *) &temperature);
3098
3099 return temperature;
3100 #endif
3101
3102 #if defined(WIN) && defined(HAVE_NVAPI)
3103 NV_GPU_THERMAL_SETTINGS pThermalSettings;
3104
3105 pThermalSettings.version = NV_GPU_THERMAL_SETTINGS_VER;
3106 pThermalSettings.count = NVAPI_MAX_THERMAL_SENSORS_PER_GPU;
3107 pThermalSettings.sensor[0].controller = NVAPI_THERMAL_CONTROLLER_UNKNOWN;
3108 pThermalSettings.sensor[0].target = NVAPI_THERMAL_TARGET_GPU;
3109
3110 if (hm_NvAPI_GPU_GetThermalSettings (data.hm_nv, data.hm_device[device_id].adapter_index.nv, 0, &pThermalSettings) != NVAPI_OK) return -1;
3111
3112 return pThermalSettings.sensor[0].currentTemp;
3113 #endif // WIN && HAVE_NVAPI
3114 }
3115 #endif // HAVE_NVML || HAVE_NVAPI
3116
3117 return -1;
3118 }
3119
3120 int hm_get_fanspeed_with_device_id (const uint device_id)
3121 {
3122 // we shouldn't really need this extra CL_DEVICE_TYPE_GPU check, because fan_supported should not be set w/ CPUs
3123 if ((data.devices_param[device_id].device_type & CL_DEVICE_TYPE_GPU) == 0) return -1;
3124
3125 if (data.hm_device[device_id].fan_supported == 1)
3126 {
3127 #ifdef HAVE_ADL
3128 if (data.devices_param[device_id].vendor_id == VENDOR_ID_AMD)
3129 {
3130 if (data.hm_amd)
3131 {
3132 if (data.hm_device[device_id].od_version == 5)
3133 {
3134 ADLFanSpeedValue lpFanSpeedValue;
3135
3136 memset (&lpFanSpeedValue, 0, sizeof (lpFanSpeedValue));
3137
3138 lpFanSpeedValue.iSize = sizeof (lpFanSpeedValue);
3139 lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
3140 lpFanSpeedValue.iFlags = ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED;
3141
3142 if (hm_ADL_Overdrive5_FanSpeed_Get (data.hm_amd, data.hm_device[device_id].adapter_index.amd, 0, &lpFanSpeedValue) != ADL_OK) return -1;
3143
3144 return lpFanSpeedValue.iFanSpeed;
3145 }
3146 else // od_version == 6
3147 {
3148 ADLOD6FanSpeedInfo faninfo;
3149
3150 memset (&faninfo, 0, sizeof (faninfo));
3151
3152 if (hm_ADL_Overdrive6_FanSpeed_Get (data.hm_amd, data.hm_device[device_id].adapter_index.amd, &faninfo) != ADL_OK) return -1;
3153
3154 return faninfo.iFanSpeedPercent;
3155 }
3156 }
3157 }
3158 #endif // HAVE_ADL
3159
3160 #if defined(HAVE_NVML) || defined(HAVE_NVAPI)
3161 if (data.devices_param[device_id].vendor_id == VENDOR_ID_NV)
3162 {
3163 #if defined(LINUX) && defined(HAVE_NVML)
3164 int speed = 0;
3165
3166 hm_NVML_nvmlDeviceGetFanSpeed (data.hm_nv, 1, data.hm_device[device_id].adapter_index.nv, (uint *) &speed);
3167
3168 return speed;
3169 #endif
3170
3171 #if defined(WIN) && defined(HAVE_NVAPI)
3172
3173 NV_GPU_COOLER_SETTINGS pCoolerSettings;
3174
3175 pCoolerSettings.Version = GPU_COOLER_SETTINGS_VER | sizeof (NV_GPU_COOLER_SETTINGS);
3176
3177 hm_NvAPI_GPU_GetCoolerSettings (data.hm_nv, data.hm_device[device_id].adapter_index.nv, 0, &pCoolerSettings);
3178
3179 return pCoolerSettings.Cooler[0].CurrentLevel;
3180 #endif
3181 }
3182 #endif // HAVE_NVML || HAVE_NVAPI
3183 }
3184
3185 return -1;
3186 }
3187
3188 int hm_get_utilization_with_device_id (const uint device_id)
3189 {
3190 if ((data.devices_param[device_id].device_type & CL_DEVICE_TYPE_GPU) == 0) return -1;
3191
3192 #ifdef HAVE_ADL
3193 if (data.devices_param[device_id].vendor_id == VENDOR_ID_AMD)
3194 {
3195 if (data.hm_amd)
3196 {
3197 ADLPMActivity PMActivity;
3198
3199 PMActivity.iSize = sizeof (ADLPMActivity);
3200
3201 if (hm_ADL_Overdrive_CurrentActivity_Get (data.hm_amd, data.hm_device[device_id].adapter_index.amd, &PMActivity) != ADL_OK) return -1;
3202
3203 return PMActivity.iActivityPercent;
3204 }
3205 }
3206 #endif // HAVE_ADL
3207
3208 #if defined(HAVE_NVML) || defined(HAVE_NVAPI)
3209 if (data.devices_param[device_id].vendor_id == VENDOR_ID_NV)
3210 {
3211 #if defined(LINUX) && defined(HAVE_NVML)
3212 nvmlUtilization_t utilization;
3213
3214 hm_NVML_nvmlDeviceGetUtilizationRates (data.hm_nv, data.hm_device[device_id].adapter_index.nv, &utilization);
3215
3216 return utilization.gpu;
3217 #endif
3218
3219 #if defined(WIN) && defined(HAVE_NVAPI)
3220 NV_GPU_DYNAMIC_PSTATES_INFO_EX pDynamicPstatesInfoEx;
3221
3222 pDynamicPstatesInfoEx.version = NV_GPU_DYNAMIC_PSTATES_INFO_EX_VER;
3223
3224 if (hm_NvAPI_GPU_GetDynamicPstatesInfoEx (data.hm_nv, data.hm_device[device_id].adapter_index.nv, &pDynamicPstatesInfoEx) != NVAPI_OK) return -1;
3225
3226 return pDynamicPstatesInfoEx.utilization[0].percentage;
3227 #endif
3228 }
3229 #endif // HAVE_NVML || HAVE_NVAPI
3230
3231 return -1;
3232 }
3233
3234 #ifdef HAVE_ADL
3235 int hm_set_fanspeed_with_device_id_amd (const uint device_id, const int fanspeed)
3236 {
3237 if (data.hm_device[device_id].fan_supported == 1)
3238 {
3239 if (data.hm_amd)
3240 {
3241 if (data.hm_device[device_id].od_version == 5)
3242 {
3243 ADLFanSpeedValue lpFanSpeedValue;
3244
3245 memset (&lpFanSpeedValue, 0, sizeof (lpFanSpeedValue));
3246
3247 lpFanSpeedValue.iSize = sizeof (lpFanSpeedValue);
3248 lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
3249 lpFanSpeedValue.iFlags = ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED;
3250 lpFanSpeedValue.iFanSpeed = fanspeed;
3251
3252 if (hm_ADL_Overdrive5_FanSpeed_Set (data.hm_amd, data.hm_device[device_id].adapter_index.amd, 0, &lpFanSpeedValue) != ADL_OK) return -1;
3253
3254 return 0;
3255 }
3256 else // od_version == 6
3257 {
3258 ADLOD6FanSpeedValue fan_speed_value;
3259
3260 memset (&fan_speed_value, 0, sizeof (fan_speed_value));
3261
3262 fan_speed_value.iSpeedType = ADL_OD6_FANSPEED_TYPE_PERCENT;
3263 fan_speed_value.iFanSpeed = fanspeed;
3264
3265 if (hm_ADL_Overdrive6_FanSpeed_Set (data.hm_amd, data.hm_device[device_id].adapter_index.amd, &fan_speed_value) != ADL_OK) return -1;
3266
3267 return 0;
3268 }
3269 }
3270 }
3271
3272 return -1;
3273 }
3274 #endif
3275
3276 // helper function for status display
3277
3278 void hm_device_val_to_str (char *target_buf, int max_buf_size, char *suffix, int value)
3279 {
3280 #define VALUE_NOT_AVAILABLE "N/A"
3281
3282 if (value == -1)
3283 {
3284 snprintf (target_buf, max_buf_size, VALUE_NOT_AVAILABLE);
3285 }
3286 else
3287 {
3288 snprintf (target_buf, max_buf_size, "%2d%s", value, suffix);
3289 }
3290 }
3291 #endif // HAVE_HWMON
3292
3293 /**
3294 * maskprocessor
3295 */
3296
3297 void mp_css_to_uniq_tbl (uint css_cnt, cs_t *css, uint uniq_tbls[SP_PW_MAX][CHARSIZ])
3298 {
3299 /* generates a lookup table where key is the char itself for fastest possible lookup performance */
3300
3301 if (css_cnt > SP_PW_MAX)
3302 {
3303 log_error ("ERROR: mask length is too long");
3304
3305 exit (-1);
3306 }
3307
3308 for (uint css_pos = 0; css_pos < css_cnt; css_pos++)
3309 {
3310 uint *uniq_tbl = uniq_tbls[css_pos];
3311
3312 uint *cs_buf = css[css_pos].cs_buf;
3313 uint cs_len = css[css_pos].cs_len;
3314
3315 for (uint cs_pos = 0; cs_pos < cs_len; cs_pos++)
3316 {
3317 uint c = cs_buf[cs_pos] & 0xff;
3318
3319 uniq_tbl[c] = 1;
3320 }
3321 }
3322 }
3323
3324 void mp_add_cs_buf (uint *in_buf, size_t in_len, cs_t *css, int css_cnt)
3325 {
3326 cs_t *cs = &css[css_cnt];
3327
3328 size_t css_uniq_sz = CHARSIZ * sizeof (uint);
3329
3330 uint *css_uniq = (uint *) mymalloc (css_uniq_sz);
3331
3332 size_t i;
3333
3334 for (i = 0; i < cs->cs_len; i++)
3335 {
3336 const uint u = cs->cs_buf[i];
3337
3338 css_uniq[u] = 1;
3339 }
3340
3341 for (i = 0; i < in_len; i++)
3342 {
3343 uint u = in_buf[i] & 0xff;
3344
3345 if (data.opts_type & OPTS_TYPE_PT_UPPER) u = toupper (u);
3346
3347 if (css_uniq[u] == 1) continue;
3348
3349 css_uniq[u] = 1;
3350
3351 cs->cs_buf[cs->cs_len] = u;
3352
3353 cs->cs_len++;
3354 }
3355
3356 myfree (css_uniq);
3357 }
3358
3359 void mp_expand (char *in_buf, size_t in_len, cs_t *mp_sys, cs_t *mp_usr, int mp_usr_offset, int interpret)
3360 {
3361 size_t in_pos;
3362
3363 for (in_pos = 0; in_pos < in_len; in_pos++)
3364 {
3365 uint p0 = in_buf[in_pos] & 0xff;
3366
3367 if (interpret == 1 && p0 == '?')
3368 {
3369 in_pos++;
3370
3371 if (in_pos == in_len) break;
3372
3373 uint p1 = in_buf[in_pos] & 0xff;
3374
3375 switch (p1)
3376 {
3377 case 'l': mp_add_cs_buf (mp_sys[0].cs_buf, mp_sys[0].cs_len, mp_usr, mp_usr_offset);
3378 break;
3379 case 'u': mp_add_cs_buf (mp_sys[1].cs_buf, mp_sys[1].cs_len, mp_usr, mp_usr_offset);
3380 break;
3381 case 'd': mp_add_cs_buf (mp_sys[2].cs_buf, mp_sys[2].cs_len, mp_usr, mp_usr_offset);
3382 break;
3383 case 's': mp_add_cs_buf (mp_sys[3].cs_buf, mp_sys[3].cs_len, mp_usr, mp_usr_offset);
3384 break;
3385 case 'a': mp_add_cs_buf (mp_sys[4].cs_buf, mp_sys[4].cs_len, mp_usr, mp_usr_offset);
3386 break;
3387 case 'b': mp_add_cs_buf (mp_sys[5].cs_buf, mp_sys[5].cs_len, mp_usr, mp_usr_offset);
3388 break;
3389 case '1': if (mp_usr[0].cs_len == 0) { log_error ("ERROR: Custom-charset 1 is undefined\n"); exit (-1); }
3390 mp_add_cs_buf (mp_usr[0].cs_buf, mp_usr[0].cs_len, mp_usr, mp_usr_offset);
3391 break;
3392 case '2': if (mp_usr[1].cs_len == 0) { log_error ("ERROR: Custom-charset 2 is undefined\n"); exit (-1); }
3393 mp_add_cs_buf (mp_usr[1].cs_buf, mp_usr[1].cs_len, mp_usr, mp_usr_offset);
3394 break;
3395 case '3': if (mp_usr[2].cs_len == 0) { log_error ("ERROR: Custom-charset 3 is undefined\n"); exit (-1); }
3396 mp_add_cs_buf (mp_usr[2].cs_buf, mp_usr[2].cs_len, mp_usr, mp_usr_offset);
3397 break;
3398 case '4': if (mp_usr[3].cs_len == 0) { log_error ("ERROR: Custom-charset 4 is undefined\n"); exit (-1); }
3399 mp_add_cs_buf (mp_usr[3].cs_buf, mp_usr[3].cs_len, mp_usr, mp_usr_offset);
3400 break;
3401 case '?': mp_add_cs_buf (&p0, 1, mp_usr, mp_usr_offset);
3402 break;
3403 default: log_error ("Syntax error: %s", in_buf);
3404 exit (-1);
3405 }
3406 }
3407 else
3408 {
3409 if (data.hex_charset)
3410 {
3411 in_pos++;
3412
3413 if (in_pos == in_len)
3414 {
3415 log_error ("ERROR: the hex-charset option always expects couples of exactly 2 hexadecimal chars, failed mask: %s", in_buf);
3416
3417 exit (-1);
3418 }
3419
3420 uint p1 = in_buf[in_pos] & 0xff;
3421
3422 if ((is_valid_hex_char (p0) == 0) || (is_valid_hex_char (p1) == 0))
3423 {
3424 log_error ("ERROR: invalid hex character detected in mask %s", in_buf);
3425
3426 exit (-1);
3427 }
3428
3429 uint chr = 0;
3430
3431 chr = hex_convert (p1) << 0;
3432 chr |= hex_convert (p0) << 4;
3433
3434 mp_add_cs_buf (&chr, 1, mp_usr, mp_usr_offset);
3435 }
3436 else
3437 {
3438 uint chr = p0;
3439
3440 mp_add_cs_buf (&chr, 1, mp_usr, mp_usr_offset);
3441 }
3442 }
3443 }
3444 }
3445
3446 u64 mp_get_sum (uint css_cnt, cs_t *css)
3447 {
3448 u64 sum = 1;
3449
3450 for (uint css_pos = 0; css_pos < css_cnt; css_pos++)
3451 {
3452 sum *= css[css_pos].cs_len;
3453 }
3454
3455 return (sum);
3456 }
3457
3458 cs_t *mp_gen_css (char *mask_buf, size_t mask_len, cs_t *mp_sys, cs_t *mp_usr, uint *css_cnt)
3459 {
3460 cs_t *css = (cs_t *) mycalloc (256, sizeof (cs_t));
3461
3462 uint mask_pos;
3463 uint css_pos;
3464
3465 for (mask_pos = 0, css_pos = 0; mask_pos < mask_len; mask_pos++, css_pos++)
3466 {
3467 char p0 = mask_buf[mask_pos];
3468
3469 if (p0 == '?')
3470 {
3471 mask_pos++;
3472
3473 if (mask_pos == mask_len) break;
3474
3475 char p1 = mask_buf[mask_pos];
3476
3477 uint chr = p1;
3478
3479 switch (p1)
3480 {
3481 case 'l': mp_add_cs_buf (mp_sys[0].cs_buf, mp_sys[0].cs_len, css, css_pos);
3482 break;
3483 case 'u': mp_add_cs_buf (mp_sys[1].cs_buf, mp_sys[1].cs_len, css, css_pos);
3484 break;
3485 case 'd': mp_add_cs_buf (mp_sys[2].cs_buf, mp_sys[2].cs_len, css, css_pos);
3486 break;
3487 case 's': mp_add_cs_buf (mp_sys[3].cs_buf, mp_sys[3].cs_len, css, css_pos);
3488 break;
3489 case 'a': mp_add_cs_buf (mp_sys[4].cs_buf, mp_sys[4].cs_len, css, css_pos);
3490 break;
3491 case 'b': mp_add_cs_buf (mp_sys[5].cs_buf, mp_sys[5].cs_len, css, css_pos);
3492 break;
3493 case '1': if (mp_usr[0].cs_len == 0) { log_error ("ERROR: Custom-charset 1 is undefined\n"); exit (-1); }
3494 mp_add_cs_buf (mp_usr[0].cs_buf, mp_usr[0].cs_len, css, css_pos);
3495 break;
3496 case '2': if (mp_usr[1].cs_len == 0) { log_error ("ERROR: Custom-charset 2 is undefined\n"); exit (-1); }
3497 mp_add_cs_buf (mp_usr[1].cs_buf, mp_usr[1].cs_len, css, css_pos);
3498 break;
3499 case '3': if (mp_usr[2].cs_len == 0) { log_error ("ERROR: Custom-charset 3 is undefined\n"); exit (-1); }
3500 mp_add_cs_buf (mp_usr[2].cs_buf, mp_usr[2].cs_len, css, css_pos);
3501 break;
3502 case '4': if (mp_usr[3].cs_len == 0) { log_error ("ERROR: Custom-charset 4 is undefined\n"); exit (-1); }
3503 mp_add_cs_buf (mp_usr[3].cs_buf, mp_usr[3].cs_len, css, css_pos);
3504 break;
3505 case '?': mp_add_cs_buf (&chr, 1, css, css_pos);
3506 break;
3507 default: log_error ("ERROR: syntax error: %s", mask_buf);
3508 exit (-1);
3509 }
3510 }
3511 else
3512 {
3513 if (data.hex_charset)
3514 {
3515 mask_pos++;
3516
3517 // if there is no 2nd hex character, show an error:
3518
3519 if (mask_pos == mask_len)
3520 {
3521 log_error ("ERROR: the hex-charset option always expects couples of exactly 2 hexadecimal chars, failed mask: %s", mask_buf);
3522
3523 exit (-1);
3524 }
3525
3526 char p1 = mask_buf[mask_pos];
3527
3528 // if they are not valid hex character, show an error:
3529
3530 if ((is_valid_hex_char (p0) == 0) || (is_valid_hex_char (p1) == 0))
3531 {
3532 log_error ("ERROR: invalid hex character detected in mask %s", mask_buf);
3533
3534 exit (-1);
3535 }
3536
3537 uint chr = 0;
3538
3539 chr |= hex_convert (p1) << 0;
3540 chr |= hex_convert (p0) << 4;
3541
3542 mp_add_cs_buf (&chr, 1, css, css_pos);
3543 }
3544 else
3545 {
3546 uint chr = p0;
3547
3548 mp_add_cs_buf (&chr, 1, css, css_pos);
3549 }
3550 }
3551 }
3552
3553 if (css_pos == 0)
3554 {
3555 log_error ("ERROR: invalid mask length (0)");
3556
3557 exit (-1);
3558 }
3559
3560 *css_cnt = css_pos;
3561
3562 return (css);
3563 }
3564
3565 void mp_exec (u64 val, char *buf, cs_t *css, int css_cnt)
3566 {
3567 for (int i = 0; i < css_cnt; i++)
3568 {
3569 uint len = css[i].cs_len;
3570 u64 next = val / len;
3571 uint pos = val % len;
3572 buf[i] = (char) css[i].cs_buf[pos] & 0xff;
3573 val = next;
3574 }
3575 }
3576
3577 void mp_cut_at (char *mask, uint max)
3578 {
3579 uint i;
3580 uint j;
3581 uint mask_len = strlen (mask);
3582
3583 for (i = 0, j = 0; i < mask_len && j < max; i++, j++)
3584 {
3585 if (mask[i] == '?') i++;
3586 }
3587
3588 mask[i] = 0;
3589 }
3590
3591 void mp_setup_sys (cs_t *mp_sys)
3592 {
3593 uint pos;
3594 uint chr;
3595 uint donec[CHARSIZ] = { 0 };
3596
3597 for (pos = 0, chr = 'a'; chr <= 'z'; chr++) { donec[chr] = 1;
3598 mp_sys[0].cs_buf[pos++] = chr;
3599 mp_sys[0].cs_len = pos; }
3600
3601 for (pos = 0, chr = 'A'; chr <= 'Z'; chr++) { donec[chr] = 1;
3602 mp_sys[1].cs_buf[pos++] = chr;
3603 mp_sys[1].cs_len = pos; }
3604
3605 for (pos = 0, chr = '0'; chr <= '9'; chr++) { donec[chr] = 1;
3606 mp_sys[2].cs_buf[pos++] = chr;
3607 mp_sys[2].cs_len = pos; }
3608
3609 for (pos = 0, chr = 0x20; chr <= 0x7e; chr++) { if (donec[chr]) continue;
3610 mp_sys[3].cs_buf[pos++] = chr;
3611 mp_sys[3].cs_len = pos; }
3612
3613 for (pos = 0, chr = 0x20; chr <= 0x7e; chr++) { mp_sys[4].cs_buf[pos++] = chr;
3614 mp_sys[4].cs_len = pos; }
3615
3616 for (pos = 0, chr = 0x00; chr <= 0xff; chr++) { mp_sys[5].cs_buf[pos++] = chr;
3617 mp_sys[5].cs_len = pos; }
3618 }
3619
3620 void mp_setup_usr (cs_t *mp_sys, cs_t *mp_usr, char *buf, uint index)
3621 {
3622 FILE *fp = fopen (buf, "rb");
3623
3624 if (fp == NULL || feof (fp)) // feof() in case if file is empty
3625 {
3626 mp_expand (buf, strlen (buf), mp_sys, mp_usr, index, 1);
3627 }
3628 else
3629 {
3630 char mp_file[1024] = { 0 };
3631
3632 size_t len = fread (mp_file, 1, sizeof (mp_file) - 1, fp);
3633
3634 fclose (fp);
3635
3636 len = in_superchop (mp_file);
3637
3638 if (len == 0)
3639 {
3640 log_info ("WARNING: charset file corrupted");
3641
3642 mp_expand (buf, strlen (buf), mp_sys, mp_usr, index, 1);
3643 }
3644 else
3645 {
3646 mp_expand (mp_file, len, mp_sys, mp_usr, index, 0);
3647 }
3648 }
3649 }
3650
3651 void mp_reset_usr (cs_t *mp_usr, uint index)
3652 {
3653 mp_usr[index].cs_len = 0;
3654
3655 memset (mp_usr[index].cs_buf, 0, sizeof (mp_usr[index].cs_buf));
3656 }
3657
3658 char *mp_get_truncated_mask (char *mask_buf, size_t mask_len, uint len)
3659 {
3660 char *new_mask_buf = (char *) mymalloc (256);
3661
3662 uint mask_pos;
3663
3664 uint css_pos;
3665
3666 for (mask_pos = 0, css_pos = 0; mask_pos < mask_len; mask_pos++, css_pos++)
3667 {
3668 if (css_pos == len) break;
3669
3670 char p0 = mask_buf[mask_pos];
3671
3672 new_mask_buf[mask_pos] = p0;
3673
3674 if (p0 == '?')
3675 {
3676 mask_pos++;
3677
3678 if (mask_pos == mask_len) break;
3679
3680 new_mask_buf[mask_pos] = mask_buf[mask_pos];
3681 }
3682 else
3683 {
3684 if (data.hex_charset)
3685 {
3686 mask_pos++;
3687
3688 if (mask_pos == mask_len)
3689 {
3690 log_error ("ERROR: the hex-charset option always expects couples of exactly 2 hexadecimal chars, failed mask: %s", mask_buf);
3691
3692 exit (-1);
3693 }
3694
3695 char p1 = mask_buf[mask_pos];
3696
3697 // if they are not valid hex character, show an error:
3698
3699 if ((is_valid_hex_char (p0) == 0) || (is_valid_hex_char (p1) == 0))
3700 {
3701 log_error ("ERROR: invalid hex character detected in mask: %s", mask_buf);
3702
3703 exit (-1);
3704 }
3705
3706 new_mask_buf[mask_pos] = p1;
3707 }
3708 }
3709 }
3710
3711 if (css_pos == len) return (new_mask_buf);
3712
3713 myfree (new_mask_buf);
3714
3715 return (NULL);
3716 }
3717
3718 /**
3719 * statprocessor
3720 */
3721
3722 u64 sp_get_sum (uint start, uint stop, cs_t *root_css_buf)
3723 {
3724 u64 sum = 1;
3725
3726 uint i;
3727
3728 for (i = start; i < stop; i++)
3729 {
3730 sum *= root_css_buf[i].cs_len;
3731 }
3732
3733 return (sum);
3734 }
3735
3736 void sp_exec (u64 ctx, char *pw_buf, cs_t *root_css_buf, cs_t *markov_css_buf, uint start, uint stop)
3737 {
3738 u64 v = ctx;
3739
3740 cs_t *cs = &root_css_buf[start];
3741
3742 uint i;
3743
3744 for (i = start; i < stop; i++)
3745 {
3746 const u64 m = v % cs->cs_len;
3747 const u64 d = v / cs->cs_len;
3748
3749 v = d;
3750
3751 const uint k = cs->cs_buf[m];
3752
3753 pw_buf[i - start] = (char) k;
3754
3755 cs = &markov_css_buf[(i * CHARSIZ) + k];
3756 }
3757 }
3758
3759 int sp_comp_val (const void *p1, const void *p2)
3760 {
3761 hcstat_table_t *b1 = (hcstat_table_t *) p1;
3762 hcstat_table_t *b2 = (hcstat_table_t *) p2;
3763
3764 return b2->val - b1->val;
3765 }
3766
3767 void sp_setup_tbl (const char *shared_dir, char *hcstat, uint disable, uint classic, hcstat_table_t *root_table_buf, hcstat_table_t *markov_table_buf)
3768 {
3769 uint i;
3770 uint j;
3771 uint k;
3772
3773 /**
3774 * Initialize hcstats
3775 */
3776
3777 u64 *root_stats_buf = (u64 *) mycalloc (SP_ROOT_CNT, sizeof (u64));
3778
3779 u64 *root_stats_ptr = root_stats_buf;
3780
3781 u64 *root_stats_buf_by_pos[SP_PW_MAX];
3782
3783 for (i = 0; i < SP_PW_MAX; i++)
3784 {
3785 root_stats_buf_by_pos[i] = root_stats_ptr;
3786
3787 root_stats_ptr += CHARSIZ;
3788 }
3789
3790 u64 *markov_stats_buf = (u64 *) mycalloc (SP_MARKOV_CNT, sizeof (u64));
3791
3792 u64 *markov_stats_ptr = markov_stats_buf;
3793
3794 u64 *markov_stats_buf_by_key[SP_PW_MAX][CHARSIZ];
3795
3796 for (i = 0; i < SP_PW_MAX; i++)
3797 {
3798 for (j = 0; j < CHARSIZ; j++)
3799 {
3800 markov_stats_buf_by_key[i][j] = markov_stats_ptr;
3801
3802 markov_stats_ptr += CHARSIZ;
3803 }
3804 }
3805
3806 /**
3807 * Load hcstats File
3808 */
3809
3810 if (hcstat == NULL)
3811 {
3812 char hcstat_tmp[256] = { 0 };
3813
3814 snprintf (hcstat_tmp, sizeof (hcstat_tmp) - 1, "%s/%s", shared_dir, SP_HCSTAT);
3815
3816 hcstat = hcstat_tmp;
3817 }
3818
3819 FILE *fd = fopen (hcstat, "rb");
3820
3821 if (fd == NULL)
3822 {
3823 log_error ("%s: %s", hcstat, strerror (errno));
3824
3825 exit (-1);
3826 }
3827
3828 if (fread (root_stats_buf, sizeof (u64), SP_ROOT_CNT, fd) != SP_ROOT_CNT)
3829 {
3830 log_error ("%s: Could not load data", hcstat);
3831
3832 fclose (fd);
3833
3834 exit (-1);
3835 }
3836
3837 if (fread (markov_stats_buf, sizeof (u64), SP_MARKOV_CNT, fd) != SP_MARKOV_CNT)
3838 {
3839 log_error ("%s: Could not load data", hcstat);
3840
3841 fclose (fd);
3842
3843 exit (-1);
3844 }
3845
3846 fclose (fd);
3847
3848 /**
3849 * Markov modifier of hcstat_table on user request
3850 */
3851
3852 if (disable)
3853 {
3854 memset (root_stats_buf, 0, SP_ROOT_CNT * sizeof (u64));
3855 memset (markov_stats_buf, 0, SP_MARKOV_CNT * sizeof (u64));
3856 }
3857
3858 if (classic)
3859 {
3860 /* Add all stats to first position */
3861
3862 for (i = 1; i < SP_PW_MAX; i++)
3863 {
3864 u64 *out = root_stats_buf_by_pos[0];
3865 u64 *in = root_stats_buf_by_pos[i];
3866
3867 for (j = 0; j < CHARSIZ; j++)
3868 {
3869 *out++ += *in++;
3870 }
3871 }
3872
3873 for (i = 1; i < SP_PW_MAX; i++)
3874 {
3875 u64 *out = markov_stats_buf_by_key[0][0];
3876 u64 *in = markov_stats_buf_by_key[i][0];
3877
3878 for (j = 0; j < CHARSIZ; j++)
3879 {
3880 for (k = 0; k < CHARSIZ; k++)
3881 {
3882 *out++ += *in++;
3883 }
3884 }
3885 }
3886
3887 /* copy them to all pw_positions */
3888
3889 for (i = 1; i < SP_PW_MAX; i++)
3890 {
3891 memcpy (root_stats_buf_by_pos[i], root_stats_buf_by_pos[0], CHARSIZ * sizeof (u64));
3892 }
3893
3894 for (i = 1; i < SP_PW_MAX; i++)
3895 {
3896 memcpy (markov_stats_buf_by_key[i][0], markov_stats_buf_by_key[0][0], CHARSIZ * CHARSIZ * sizeof (u64));
3897 }
3898 }
3899
3900 /**
3901 * Initialize tables
3902 */
3903
3904 hcstat_table_t *root_table_ptr = root_table_buf;
3905
3906 hcstat_table_t *root_table_buf_by_pos[SP_PW_MAX];
3907
3908 for (i = 0; i < SP_PW_MAX; i++)
3909 {
3910 root_table_buf_by_pos[i] = root_table_ptr;
3911
3912 root_table_ptr += CHARSIZ;
3913 }
3914
3915 hcstat_table_t *markov_table_ptr = markov_table_buf;
3916
3917 hcstat_table_t *markov_table_buf_by_key[SP_PW_MAX][CHARSIZ];
3918
3919 for (i = 0; i < SP_PW_MAX; i++)
3920 {
3921 for (j = 0; j < CHARSIZ; j++)
3922 {
3923 markov_table_buf_by_key[i][j] = markov_table_ptr;
3924
3925 markov_table_ptr += CHARSIZ;
3926 }
3927 }
3928
3929 /**
3930 * Convert hcstat to tables
3931 */
3932
3933 for (i = 0; i < SP_ROOT_CNT; i++)
3934 {
3935 uint key = i % CHARSIZ;
3936
3937 root_table_buf[i].key = key;
3938 root_table_buf[i].val = root_stats_buf[i];
3939 }
3940
3941 for (i = 0; i < SP_MARKOV_CNT; i++)
3942 {
3943 uint key = i % CHARSIZ;
3944
3945 markov_table_buf[i].key = key;
3946 markov_table_buf[i].val = markov_stats_buf[i];
3947 }
3948
3949 myfree (root_stats_buf);
3950 myfree (markov_stats_buf);
3951
3952 /**
3953 * Finally sort them
3954 */
3955
3956 for (i = 0; i < SP_PW_MAX; i++)
3957 {
3958 qsort (root_table_buf_by_pos[i], CHARSIZ, sizeof (hcstat_table_t), sp_comp_val);
3959 }
3960
3961 for (i = 0; i < SP_PW_MAX; i++)
3962 {
3963 for (j = 0; j < CHARSIZ; j++)
3964 {
3965 qsort (markov_table_buf_by_key[i][j], CHARSIZ, sizeof (hcstat_table_t), sp_comp_val);
3966 }
3967 }
3968 }
3969
3970 void sp_tbl_to_css (hcstat_table_t *root_table_buf, hcstat_table_t *markov_table_buf, cs_t *root_css_buf, cs_t *markov_css_buf, uint threshold, uint uniq_tbls[SP_PW_MAX][CHARSIZ])
3971 {
3972 /**
3973 * Convert tables to css
3974 */
3975
3976 for (uint i = 0; i < SP_ROOT_CNT; i++)
3977 {
3978 uint pw_pos = i / CHARSIZ;
3979
3980 cs_t *cs = &root_css_buf[pw_pos];
3981
3982 if (cs->cs_len == threshold) continue;
3983
3984 uint key = root_table_buf[i].key;
3985
3986 if (uniq_tbls[pw_pos][key] == 0) continue;
3987
3988 cs->cs_buf[cs->cs_len] = key;
3989
3990 cs->cs_len++;
3991 }
3992
3993 /**
3994 * Convert table to css
3995 */
3996
3997 for (uint i = 0; i < SP_MARKOV_CNT; i++)
3998 {
3999 uint c = i / CHARSIZ;
4000
4001 cs_t *cs = &markov_css_buf[c];
4002
4003 if (cs->cs_len == threshold) continue;
4004
4005 uint pw_pos = c / CHARSIZ;
4006
4007 uint key = markov_table_buf[i].key;
4008
4009 if ((pw_pos + 1) < SP_PW_MAX) if (uniq_tbls[pw_pos + 1][key] == 0) continue;
4010
4011 cs->cs_buf[cs->cs_len] = key;
4012
4013 cs->cs_len++;
4014 }
4015
4016 /*
4017 for (uint i = 0; i < 8; i++)
4018 {
4019 for (uint j = 0x20; j < 0x80; j++)
4020 {
4021 cs_t *ptr = &markov_css_buf[(i * CHARSIZ) + j];
4022
4023 printf ("pos:%u key:%u len:%u\n", i, j, ptr->cs_len);
4024
4025 for (uint k = 0; k < 10; k++)
4026 {
4027 printf (" %u\n", ptr->cs_buf[k]);
4028 }
4029 }
4030 }
4031 */
4032 }
4033
4034 void sp_stretch_root (hcstat_table_t *in, hcstat_table_t *out)
4035 {
4036 for (uint i = 0; i < SP_PW_MAX; i += 2)
4037 {
4038 memcpy (out, in, CHARSIZ * sizeof (hcstat_table_t));
4039
4040 out += CHARSIZ;
4041 in += CHARSIZ;
4042
4043 out->key = 0;
4044 out->val = 1;
4045
4046 out++;
4047
4048 for (uint j = 1; j < CHARSIZ; j++)
4049 {
4050 out->key = j;
4051 out->val = 0;
4052
4053 out++;
4054 }
4055 }
4056 }
4057
4058 void sp_stretch_markov (hcstat_table_t *in, hcstat_table_t *out)
4059 {
4060 for (uint i = 0; i < SP_PW_MAX; i += 2)
4061 {
4062 memcpy (out, in, CHARSIZ * CHARSIZ * sizeof (hcstat_table_t));
4063
4064 out += CHARSIZ * CHARSIZ;
4065 in += CHARSIZ * CHARSIZ;
4066
4067 for (uint j = 0; j < CHARSIZ; j++)
4068 {
4069 out->key = 0;
4070 out->val = 1;
4071
4072 out++;
4073
4074 for (uint k = 1; k < CHARSIZ; k++)
4075 {
4076 out->key = k;
4077 out->val = 0;
4078
4079 out++;
4080 }
4081 }
4082 }
4083 }
4084
4085 /**
4086 * mixed shared functions
4087 */
4088
4089 void dump_hex (const u8 *s, const int sz)
4090 {
4091 for (int i = 0; i < sz; i++)
4092 {
4093 log_info_nn ("%02x ", s[i]);
4094 }
4095
4096 log_info ("");
4097 }
4098
4099 void usage_mini_print (const char *progname)
4100 {
4101 for (uint i = 0; USAGE_MINI[i] != NULL; i++) log_info (USAGE_MINI[i], progname);
4102 }
4103
4104 void usage_big_print (const char *progname)
4105 {
4106 for (uint i = 0; USAGE_BIG[i] != NULL; i++) log_info (USAGE_BIG[i], progname);
4107 }
4108
4109 char *get_exec_path ()
4110 {
4111 int exec_path_len = 1024;
4112
4113 char *exec_path = (char *) mymalloc (exec_path_len);
4114
4115 #ifdef LINUX
4116
4117 char tmp[32] = { 0 };
4118
4119 snprintf (tmp, sizeof (tmp) - 1, "/proc/%d/exe", getpid ());
4120
4121 const int len = readlink (tmp, exec_path, exec_path_len - 1);
4122
4123 #elif WIN
4124
4125 const int len = GetModuleFileName (NULL, exec_path, exec_path_len - 1);
4126
4127 #elif OSX
4128
4129 uint size = exec_path_len;
4130
4131 if (_NSGetExecutablePath (exec_path, &size) != 0)
4132 {
4133 log_error("! executable path buffer too small\n");
4134
4135 exit (-1);
4136 }
4137
4138 const int len = strlen (exec_path);
4139
4140 #else
4141 #error Your Operating System is not supported or detected
4142 #endif
4143
4144 exec_path[len] = 0;
4145
4146 return exec_path;
4147 }
4148
4149 char *get_install_dir (const char *progname)
4150 {
4151 char *install_dir = mystrdup (progname);
4152 char *last_slash = NULL;
4153
4154 if ((last_slash = strrchr (install_dir, '/')) != NULL)
4155 {
4156 *last_slash = 0;
4157 }
4158 else if ((last_slash = strrchr (install_dir, '\\')) != NULL)
4159 {
4160 *last_slash = 0;
4161 }
4162 else
4163 {
4164 install_dir[0] = '.';
4165 install_dir[1] = 0;
4166 }
4167
4168 return (install_dir);
4169 }
4170
4171 char *get_profile_dir (const char *homedir)
4172 {
4173 #define DOT_HASHCAT ".hashcat"
4174
4175 size_t len = strlen (homedir) + 1 + strlen (DOT_HASHCAT) + 1;
4176
4177 char *profile_dir = (char *) mymalloc (len + 1);
4178
4179 snprintf (profile_dir, len, "%s/%s", homedir, DOT_HASHCAT);
4180
4181 return profile_dir;
4182 }
4183
4184 char *get_session_dir (const char *profile_dir)
4185 {
4186 #define SESSIONS_FOLDER "sessions"
4187
4188 size_t len = strlen (profile_dir) + 1 + strlen (SESSIONS_FOLDER) + 1;
4189
4190 char *session_dir = (char *) mymalloc (len + 1);
4191
4192 snprintf (session_dir, len, "%s/%s", profile_dir, SESSIONS_FOLDER);
4193
4194 return session_dir;
4195 }
4196
4197 uint count_lines (FILE *fd)
4198 {
4199 uint cnt = 0;
4200
4201 char *buf = (char *) mymalloc (HCBUFSIZ + 1);
4202
4203 char prev = '\n';
4204
4205 while (!feof (fd))
4206 {
4207 size_t nread = fread (buf, sizeof (char), HCBUFSIZ, fd);
4208
4209 if (nread < 1) continue;
4210
4211 size_t i;
4212
4213 for (i = 0; i < nread; i++)
4214 {
4215 if (prev == '\n') cnt++;
4216
4217 prev = buf[i];
4218 }
4219 }
4220
4221 myfree (buf);
4222
4223 return cnt;
4224 }
4225
4226 void truecrypt_crc32 (const char *filename, u8 keytab[64])
4227 {
4228 uint crc = ~0;
4229
4230 FILE *fd = fopen (filename, "rb");
4231
4232 if (fd == NULL)
4233 {
4234 log_error ("%s: %s", filename, strerror (errno));
4235
4236 exit (-1);
4237 }
4238
4239 #define MAX_KEY_SIZE (1024 * 1024)
4240
4241 u8 *buf = (u8 *) mymalloc (MAX_KEY_SIZE + 1);
4242
4243 int nread = fread (buf, sizeof (u8), MAX_KEY_SIZE, fd);
4244
4245 fclose (fd);
4246
4247 int kpos = 0;
4248
4249 for (int fpos = 0; fpos < nread; fpos++)
4250 {
4251 crc = crc32tab[(crc ^ buf[fpos]) & 0xff] ^ (crc >> 8);
4252
4253 keytab[kpos++] += (crc >> 24) & 0xff;
4254 keytab[kpos++] += (crc >> 16) & 0xff;
4255 keytab[kpos++] += (crc >> 8) & 0xff;
4256 keytab[kpos++] += (crc >> 0) & 0xff;
4257
4258 if (kpos >= 64) kpos = 0;
4259 }
4260
4261 myfree (buf);
4262 }
4263
4264 #ifdef OSX
4265 int pthread_setaffinity_np (pthread_t thread, size_t cpu_size, cpu_set_t *cpu_set)
4266 {
4267 int core;
4268
4269 for (core = 0; core < (8 * (int)cpu_size); core++)
4270 if (CPU_ISSET(core, cpu_set)) break;
4271
4272 thread_affinity_policy_data_t policy = { core };
4273
4274 const int rc = thread_policy_set (pthread_mach_thread_np (thread), THREAD_AFFINITY_POLICY, (thread_policy_t) &policy, 1);
4275
4276 if (data.quiet == 0)
4277 {
4278 if (rc != KERN_SUCCESS)
4279 {
4280 log_error ("ERROR: %s : %d", "thread_policy_set()", rc);
4281 }
4282 }
4283
4284 return rc;
4285 }
4286 #endif
4287
4288 void set_cpu_affinity (char *cpu_affinity)
4289 {
4290 #ifdef WIN
4291 DWORD_PTR aff_mask = 0;
4292 #elif _POSIX
4293 cpu_set_t cpuset;
4294 CPU_ZERO (&cpuset);
4295 #endif
4296
4297 if (cpu_affinity)
4298 {
4299 char *devices = strdup (cpu_affinity);
4300
4301 char *next = strtok (devices, ",");
4302
4303 do
4304 {
4305 uint cpu_id = atoi (next);
4306
4307 if (cpu_id == 0)
4308 {
4309 #ifdef WIN
4310 aff_mask = 0;
4311 #elif _POSIX
4312 CPU_ZERO (&cpuset);
4313 #endif
4314
4315 break;
4316 }
4317
4318 if (cpu_id > 32)
4319 {
4320 log_error ("ERROR: invalid cpu_id %u specified", cpu_id);
4321
4322 exit (-1);
4323 }
4324
4325 #ifdef WIN
4326 aff_mask |= 1 << (cpu_id - 1);
4327 #elif _POSIX
4328 CPU_SET ((cpu_id - 1), &cpuset);
4329 #endif
4330
4331 } while ((next = strtok (NULL, ",")) != NULL);
4332
4333 free (devices);
4334 }
4335
4336 #ifdef WIN
4337 SetProcessAffinityMask (GetCurrentProcess (), aff_mask);
4338 SetThreadAffinityMask (GetCurrentThread (), aff_mask);
4339 #elif _POSIX
4340 pthread_t thread = pthread_self ();
4341 pthread_setaffinity_np (thread, sizeof (cpu_set_t), &cpuset);
4342 #endif
4343 }
4344
4345 void *rulefind (const void *key, void *base, int nmemb, size_t size, int (*compar) (const void *, const void *))
4346 {
4347 char *element, *end;
4348
4349 end = (char *) base + nmemb * size;
4350
4351 for (element = (char *) base; element < end; element += size)
4352 if (!compar (element, key))
4353 return element;
4354
4355 return NULL;
4356 }
4357
4358 int sort_by_u32 (const void *v1, const void *v2)
4359 {
4360 const u32 *s1 = (const u32 *) v1;
4361 const u32 *s2 = (const u32 *) v2;
4362
4363 return *s1 - *s2;
4364 }
4365
4366 int sort_by_salt (const void *v1, const void *v2)
4367 {
4368 const salt_t *s1 = (const salt_t *) v1;
4369 const salt_t *s2 = (const salt_t *) v2;
4370
4371 const int res1 = s1->salt_len - s2->salt_len;
4372
4373 if (res1 != 0) return (res1);
4374
4375 const int res2 = s1->salt_iter - s2->salt_iter;
4376
4377 if (res2 != 0) return (res2);
4378
4379 uint n;
4380
4381 n = 16;
4382
4383 while (n--)
4384 {
4385 if (s1->salt_buf[n] > s2->salt_buf[n]) return ( 1);
4386 if (s1->salt_buf[n] < s2->salt_buf[n]) return (-1);
4387 }
4388
4389 n = 8;
4390
4391 while (n--)
4392 {
4393 if (s1->salt_buf_pc[n] > s2->salt_buf_pc[n]) return ( 1);
4394 if (s1->salt_buf_pc[n] < s2->salt_buf_pc[n]) return (-1);
4395 }
4396
4397 return (0);
4398 }
4399
4400 int sort_by_salt_buf (const void *v1, const void *v2)
4401 {
4402 const pot_t *p1 = (const pot_t *) v1;
4403 const pot_t *p2 = (const pot_t *) v2;
4404
4405 const hash_t *h1 = &p1->hash;
4406 const hash_t *h2 = &p2->hash;
4407
4408 const salt_t *s1 = h1->salt;
4409 const salt_t *s2 = h2->salt;
4410
4411 uint n = 16;
4412
4413 while (n--)
4414 {
4415 if (s1->salt_buf[n] > s2->salt_buf[n]) return ( 1);
4416 if (s1->salt_buf[n] < s2->salt_buf[n]) return (-1);
4417 }
4418
4419 return 0;
4420 }
4421
4422 int sort_by_hash_t_salt (const void *v1, const void *v2)
4423 {
4424 const hash_t *h1 = (const hash_t *) v1;
4425 const hash_t *h2 = (const hash_t *) v2;
4426
4427 const salt_t *s1 = h1->salt;
4428 const salt_t *s2 = h2->salt;
4429
4430 // testphase: this should work
4431 uint n = 16;
4432
4433 while (n--)
4434 {
4435 if (s1->salt_buf[n] > s2->salt_buf[n]) return ( 1);
4436 if (s1->salt_buf[n] < s2->salt_buf[n]) return (-1);
4437 }
4438
4439 /* original code, seems buggy since salt_len can be very big (had a case with 131 len)
4440 also it thinks salt_buf[x] is a char but its a uint so salt_len should be / 4
4441 if (s1->salt_len > s2->salt_len) return ( 1);
4442 if (s1->salt_len < s2->salt_len) return (-1);
4443
4444 uint n = s1->salt_len;
4445
4446 while (n--)
4447 {
4448 if (s1->salt_buf[n] > s2->salt_buf[n]) return ( 1);
4449 if (s1->salt_buf[n] < s2->salt_buf[n]) return (-1);
4450 }
4451 */
4452
4453 return 0;
4454 }
4455
4456 int sort_by_hash_t_salt_hccap (const void *v1, const void *v2)
4457 {
4458 const hash_t *h1 = (const hash_t *) v1;
4459 const hash_t *h2 = (const hash_t *) v2;
4460
4461 const salt_t *s1 = h1->salt;
4462 const salt_t *s2 = h2->salt;
4463
4464 // 16 - 2 (since last 2 uints contain the digest)
4465 uint n = 14;
4466
4467 while (n--)
4468 {
4469 if (s1->salt_buf[n] > s2->salt_buf[n]) return ( 1);
4470 if (s1->salt_buf[n] < s2->salt_buf[n]) return (-1);
4471 }
4472
4473 return 0;
4474 }
4475
4476 int sort_by_hash_no_salt (const void *v1, const void *v2)
4477 {
4478 const hash_t *h1 = (const hash_t *) v1;
4479 const hash_t *h2 = (const hash_t *) v2;
4480
4481 const void *d1 = h1->digest;
4482 const void *d2 = h2->digest;
4483
4484 return data.sort_by_digest (d1, d2);
4485 }
4486
4487 int sort_by_hash (const void *v1, const void *v2)
4488 {
4489 const hash_t *h1 = (const hash_t *) v1;
4490 const hash_t *h2 = (const hash_t *) v2;
4491
4492 if (data.isSalted)
4493 {
4494 const salt_t *s1 = h1->salt;
4495 const salt_t *s2 = h2->salt;
4496
4497 int res = sort_by_salt (s1, s2);
4498
4499 if (res != 0) return (res);
4500 }
4501
4502 const void *d1 = h1->digest;
4503 const void *d2 = h2->digest;
4504
4505 return data.sort_by_digest (d1, d2);
4506 }
4507
4508 int sort_by_pot (const void *v1, const void *v2)
4509 {
4510 const pot_t *p1 = (const pot_t *) v1;
4511 const pot_t *p2 = (const pot_t *) v2;
4512
4513 const hash_t *h1 = &p1->hash;
4514 const hash_t *h2 = &p2->hash;
4515
4516 return sort_by_hash (h1, h2);
4517 }
4518
4519 int sort_by_mtime (const void *p1, const void *p2)
4520 {
4521 const char **f1 = (const char **) p1;
4522 const char **f2 = (const char **) p2;
4523
4524 struct stat s1; stat (*f1, &s1);
4525 struct stat s2; stat (*f2, &s2);
4526
4527 return s2.st_mtime - s1.st_mtime;
4528 }
4529
4530 int sort_by_cpu_rule (const void *p1, const void *p2)
4531 {
4532 const cpu_rule_t *r1 = (const cpu_rule_t *) p1;
4533 const cpu_rule_t *r2 = (const cpu_rule_t *) p2;
4534
4535 return memcmp (r1, r2, sizeof (cpu_rule_t));
4536 }
4537
4538 int sort_by_kernel_rule (const void *p1, const void *p2)
4539 {
4540 const kernel_rule_t *r1 = (const kernel_rule_t *) p1;
4541 const kernel_rule_t *r2 = (const kernel_rule_t *) p2;
4542
4543 return memcmp (r1, r2, sizeof (kernel_rule_t));
4544 }
4545
4546 int sort_by_stringptr (const void *p1, const void *p2)
4547 {
4548 const char **s1 = (const char **) p1;
4549 const char **s2 = (const char **) p2;
4550
4551 return strcmp (*s1, *s2);
4552 }
4553
4554 int sort_by_dictstat (const void *s1, const void *s2)
4555 {
4556 dictstat_t *d1 = (dictstat_t *) s1;
4557 dictstat_t *d2 = (dictstat_t *) s2;
4558
4559 #ifdef LINUX
4560 d2->stat.st_atim = d1->stat.st_atim;
4561 #else
4562 d2->stat.st_atime = d1->stat.st_atime;
4563 #endif
4564
4565 return memcmp (&d1->stat, &d2->stat, sizeof (struct stat));
4566 }
4567
4568 int sort_by_bitmap (const void *p1, const void *p2)
4569 {
4570 const bitmap_result_t *b1 = (const bitmap_result_t *) p1;
4571 const bitmap_result_t *b2 = (const bitmap_result_t *) p2;
4572
4573 return b1->collisions - b2->collisions;
4574 }
4575
4576 int sort_by_digest_4_2 (const void *v1, const void *v2)
4577 {
4578 const u32 *d1 = (const u32 *) v1;
4579 const u32 *d2 = (const u32 *) v2;
4580
4581 uint n = 2;
4582
4583 while (n--)
4584 {
4585 if (d1[n] > d2[n]) return ( 1);
4586 if (d1[n] < d2[n]) return (-1);
4587 }
4588
4589 return (0);
4590 }
4591
4592 int sort_by_digest_4_4 (const void *v1, const void *v2)
4593 {
4594 const u32 *d1 = (const u32 *) v1;
4595 const u32 *d2 = (const u32 *) v2;
4596
4597 uint n = 4;
4598
4599 while (n--)
4600 {
4601 if (d1[n] > d2[n]) return ( 1);
4602 if (d1[n] < d2[n]) return (-1);
4603 }
4604
4605 return (0);
4606 }
4607
4608 int sort_by_digest_4_5 (const void *v1, const void *v2)
4609 {
4610 const u32 *d1 = (const u32 *) v1;
4611 const u32 *d2 = (const u32 *) v2;
4612
4613 uint n = 5;
4614
4615 while (n--)
4616 {
4617 if (d1[n] > d2[n]) return ( 1);
4618 if (d1[n] < d2[n]) return (-1);
4619 }
4620
4621 return (0);
4622 }
4623
4624 int sort_by_digest_4_6 (const void *v1, const void *v2)
4625 {
4626 const u32 *d1 = (const u32 *) v1;
4627 const u32 *d2 = (const u32 *) v2;
4628
4629 uint n = 6;
4630
4631 while (n--)
4632 {
4633 if (d1[n] > d2[n]) return ( 1);
4634 if (d1[n] < d2[n]) return (-1);
4635 }
4636
4637 return (0);
4638 }
4639
4640 int sort_by_digest_4_8 (const void *v1, const void *v2)
4641 {
4642 const u32 *d1 = (const u32 *) v1;
4643 const u32 *d2 = (const u32 *) v2;
4644
4645 uint n = 8;
4646
4647 while (n--)
4648 {
4649 if (d1[n] > d2[n]) return ( 1);
4650 if (d1[n] < d2[n]) return (-1);
4651 }
4652
4653 return (0);
4654 }
4655
4656 int sort_by_digest_4_16 (const void *v1, const void *v2)
4657 {
4658 const u32 *d1 = (const u32 *) v1;
4659 const u32 *d2 = (const u32 *) v2;
4660
4661 uint n = 16;
4662
4663 while (n--)
4664 {
4665 if (d1[n] > d2[n]) return ( 1);
4666 if (d1[n] < d2[n]) return (-1);
4667 }
4668
4669 return (0);
4670 }
4671
4672 int sort_by_digest_4_32 (const void *v1, const void *v2)
4673 {
4674 const u32 *d1 = (const u32 *) v1;
4675 const u32 *d2 = (const u32 *) v2;
4676
4677 uint n = 32;
4678
4679 while (n--)
4680 {
4681 if (d1[n] > d2[n]) return ( 1);
4682 if (d1[n] < d2[n]) return (-1);
4683 }
4684
4685 return (0);
4686 }
4687
4688 int sort_by_digest_4_64 (const void *v1, const void *v2)
4689 {
4690 const u32 *d1 = (const u32 *) v1;
4691 const u32 *d2 = (const u32 *) v2;
4692
4693 uint n = 64;
4694
4695 while (n--)
4696 {
4697 if (d1[n] > d2[n]) return ( 1);
4698 if (d1[n] < d2[n]) return (-1);
4699 }
4700
4701 return (0);
4702 }
4703
4704 int sort_by_digest_8_8 (const void *v1, const void *v2)
4705 {
4706 const u64 *d1 = (const u64 *) v1;
4707 const u64 *d2 = (const u64 *) v2;
4708
4709 uint n = 8;
4710
4711 while (n--)
4712 {
4713 if (d1[n] > d2[n]) return ( 1);
4714 if (d1[n] < d2[n]) return (-1);
4715 }
4716
4717 return (0);
4718 }
4719
4720 int sort_by_digest_8_16 (const void *v1, const void *v2)
4721 {
4722 const u64 *d1 = (const u64 *) v1;
4723 const u64 *d2 = (const u64 *) v2;
4724
4725 uint n = 16;
4726
4727 while (n--)
4728 {
4729 if (d1[n] > d2[n]) return ( 1);
4730 if (d1[n] < d2[n]) return (-1);
4731 }
4732
4733 return (0);
4734 }
4735
4736 int sort_by_digest_8_25 (const void *v1, const void *v2)
4737 {
4738 const u64 *d1 = (const u64 *) v1;
4739 const u64 *d2 = (const u64 *) v2;
4740
4741 uint n = 25;
4742
4743 while (n--)
4744 {
4745 if (d1[n] > d2[n]) return ( 1);
4746 if (d1[n] < d2[n]) return (-1);
4747 }
4748
4749 return (0);
4750 }
4751
4752 int sort_by_digest_p0p1 (const void *v1, const void *v2)
4753 {
4754 const u32 *d1 = (const u32 *) v1;
4755 const u32 *d2 = (const u32 *) v2;
4756
4757 const uint dgst_pos0 = data.dgst_pos0;
4758 const uint dgst_pos1 = data.dgst_pos1;
4759 const uint dgst_pos2 = data.dgst_pos2;
4760 const uint dgst_pos3 = data.dgst_pos3;
4761
4762 if (d1[dgst_pos3] > d2[dgst_pos3]) return ( 1);
4763 if (d1[dgst_pos3] < d2[dgst_pos3]) return (-1);
4764 if (d1[dgst_pos2] > d2[dgst_pos2]) return ( 1);
4765 if (d1[dgst_pos2] < d2[dgst_pos2]) return (-1);
4766 if (d1[dgst_pos1] > d2[dgst_pos1]) return ( 1);
4767 if (d1[dgst_pos1] < d2[dgst_pos1]) return (-1);
4768 if (d1[dgst_pos0] > d2[dgst_pos0]) return ( 1);
4769 if (d1[dgst_pos0] < d2[dgst_pos0]) return (-1);
4770
4771 return (0);
4772 }
4773
4774 int sort_by_tuning_db_alias (const void *v1, const void *v2)
4775 {
4776 const tuning_db_alias_t *t1 = (const tuning_db_alias_t *) v1;
4777 const tuning_db_alias_t *t2 = (const tuning_db_alias_t *) v2;
4778
4779 const int res1 = strcmp (t1->device_name, t2->device_name);
4780
4781 if (res1 != 0) return (res1);
4782
4783 return 0;
4784 }
4785
4786 int sort_by_tuning_db_entry (const void *v1, const void *v2)
4787 {
4788 const tuning_db_entry_t *t1 = (const tuning_db_entry_t *) v1;
4789 const tuning_db_entry_t *t2 = (const tuning_db_entry_t *) v2;
4790
4791 const int res1 = strcmp (t1->device_name, t2->device_name);
4792
4793 if (res1 != 0) return (res1);
4794
4795 const int res2 = t1->attack_mode
4796 - t2->attack_mode;
4797
4798 if (res2 != 0) return (res2);
4799
4800 const int res3 = t1->hash_type
4801 - t2->hash_type;
4802
4803 if (res3 != 0) return (res3);
4804
4805 return 0;
4806 }
4807
4808 void format_debug (char *debug_file, uint debug_mode, unsigned char *orig_plain_ptr, uint orig_plain_len, unsigned char *mod_plain_ptr, uint mod_plain_len, char *rule_buf, int rule_len)
4809 {
4810 uint outfile_autohex = data.outfile_autohex;
4811
4812 unsigned char *rule_ptr = (unsigned char *) rule_buf;
4813
4814 FILE *debug_fp = NULL;
4815
4816 if (debug_file != NULL)
4817 {
4818 debug_fp = fopen (debug_file, "ab");
4819
4820 lock_file (debug_fp);
4821 }
4822 else
4823 {
4824 debug_fp = stderr;
4825 }
4826
4827 if (debug_fp == NULL)
4828 {
4829 log_info ("WARNING: Could not open debug-file for writing");
4830 }
4831 else
4832 {
4833 if ((debug_mode == 2) || (debug_mode == 3) || (debug_mode == 4))
4834 {
4835 format_plain (debug_fp, orig_plain_ptr, orig_plain_len, outfile_autohex);
4836
4837 if ((debug_mode == 3) || (debug_mode == 4)) fputc (':', debug_fp);
4838 }
4839
4840 fwrite (rule_ptr, rule_len, 1, debug_fp);
4841
4842 if (debug_mode == 4)
4843 {
4844 fputc (':', debug_fp);
4845
4846 format_plain (debug_fp, mod_plain_ptr, mod_plain_len, outfile_autohex);
4847 }
4848
4849 fputc ('\n', debug_fp);
4850
4851 if (debug_file != NULL) fclose (debug_fp);
4852 }
4853 }
4854
4855 void format_plain (FILE *fp, unsigned char *plain_ptr, uint plain_len, uint outfile_autohex)
4856 {
4857 int needs_hexify = 0;
4858
4859 if (outfile_autohex == 1)
4860 {
4861 for (uint i = 0; i < plain_len; i++)
4862 {
4863 if (plain_ptr[i] < 0x20)
4864 {
4865 needs_hexify = 1;
4866
4867 break;
4868 }
4869
4870 if (plain_ptr[i] > 0x7f)
4871 {
4872 needs_hexify = 1;
4873
4874 break;
4875 }
4876 }
4877 }
4878
4879 if (needs_hexify == 1)
4880 {
4881 fprintf (fp, "$HEX[");
4882
4883 for (uint i = 0; i < plain_len; i++)
4884 {
4885 fprintf (fp, "%02x", plain_ptr[i]);
4886 }
4887
4888 fprintf (fp, "]");
4889 }
4890 else
4891 {
4892 fwrite (plain_ptr, plain_len, 1, fp);
4893 }
4894 }
4895
4896 void format_output (FILE *out_fp, char *out_buf, unsigned char *plain_ptr, const uint plain_len, const u64 crackpos, unsigned char *username, const uint user_len)
4897 {
4898 uint outfile_format = data.outfile_format;
4899
4900 char separator = data.separator;
4901
4902 if (outfile_format & OUTFILE_FMT_HASH)
4903 {
4904 fprintf (out_fp, "%s", out_buf);
4905
4906 if (outfile_format & (OUTFILE_FMT_PLAIN | OUTFILE_FMT_HEXPLAIN | OUTFILE_FMT_CRACKPOS))
4907 {
4908 fputc (separator, out_fp);
4909 }
4910 }
4911 else if (data.username)
4912 {
4913 if (username != NULL)
4914 {
4915 for (uint i = 0; i < user_len; i++)
4916 {
4917 fprintf (out_fp, "%c", username[i]);
4918 }
4919
4920 if (outfile_format & (OUTFILE_FMT_PLAIN | OUTFILE_FMT_HEXPLAIN | OUTFILE_FMT_CRACKPOS))
4921 {
4922 fputc (separator, out_fp);
4923 }
4924 }
4925 }
4926
4927 if (outfile_format & OUTFILE_FMT_PLAIN)
4928 {
4929 format_plain (out_fp, plain_ptr, plain_len, data.outfile_autohex);
4930
4931 if (outfile_format & (OUTFILE_FMT_HEXPLAIN | OUTFILE_FMT_CRACKPOS))
4932 {
4933 fputc (separator, out_fp);
4934 }
4935 }
4936
4937 if (outfile_format & OUTFILE_FMT_HEXPLAIN)
4938 {
4939 for (uint i = 0; i < plain_len; i++)
4940 {
4941 fprintf (out_fp, "%02x", plain_ptr[i]);
4942 }
4943
4944 if (outfile_format & (OUTFILE_FMT_CRACKPOS))
4945 {
4946 fputc (separator, out_fp);
4947 }
4948 }
4949
4950 if (outfile_format & OUTFILE_FMT_CRACKPOS)
4951 {
4952 #ifdef _WIN
4953 __mingw_fprintf (out_fp, "%llu", crackpos);
4954 #endif
4955
4956 #ifdef _POSIX
4957 #ifdef __x86_64__
4958 fprintf (out_fp, "%lu", (unsigned long) crackpos);
4959 #else
4960 fprintf (out_fp, "%llu", crackpos);
4961 #endif
4962 #endif
4963 }
4964
4965 fputc ('\n', out_fp);
4966 }
4967
4968 void handle_show_request (pot_t *pot, uint pot_cnt, char *input_buf, int input_len, hash_t *hashes_buf, int (*sort_by_pot) (const void *, const void *), FILE *out_fp)
4969 {
4970 pot_t pot_key;
4971
4972 pot_key.hash.salt = hashes_buf->salt;
4973 pot_key.hash.digest = hashes_buf->digest;
4974
4975 pot_t *pot_ptr = (pot_t *) bsearch (&pot_key, pot, pot_cnt, sizeof (pot_t), sort_by_pot);
4976
4977 if (pot_ptr)
4978 {
4979 log_info_nn ("");
4980
4981 input_buf[input_len] = 0;
4982
4983 // user
4984 unsigned char *username = NULL;
4985 uint user_len = 0;
4986
4987 if (data.username)
4988 {
4989 user_t *user = hashes_buf->hash_info->user;
4990
4991 if (user)
4992 {
4993 username = (unsigned char *) (user->user_name);
4994
4995 user_len = user->user_len;
4996 }
4997 }
4998
4999 // do output the line
5000 format_output (out_fp, input_buf, (unsigned char *) pot_ptr->plain_buf, pot_ptr->plain_len, 0, username, user_len);
5001 }
5002 }
5003
5004 #define LM_WEAK_HASH "\x4e\xcf\x0d\x0c\x0a\xe2\xfb\xc1"
5005 #define LM_MASKED_PLAIN "[notfound]"
5006
5007 void handle_show_request_lm (pot_t *pot, uint pot_cnt, char *input_buf, int input_len, hash_t *hash_left, hash_t *hash_right, int (*sort_by_pot) (const void *, const void *), FILE *out_fp)
5008 {
5009 // left
5010
5011 pot_t pot_left_key;
5012
5013 pot_left_key.hash.salt = hash_left->salt;
5014 pot_left_key.hash.digest = hash_left->digest;
5015
5016 pot_t *pot_left_ptr = (pot_t *) bsearch (&pot_left_key, pot, pot_cnt, sizeof (pot_t), sort_by_pot);
5017
5018 // right
5019
5020 uint weak_hash_found = 0;
5021
5022 pot_t pot_right_key;
5023
5024 pot_right_key.hash.salt = hash_right->salt;
5025 pot_right_key.hash.digest = hash_right->digest;
5026
5027 pot_t *pot_right_ptr = (pot_t *) bsearch (&pot_right_key, pot, pot_cnt, sizeof (pot_t), sort_by_pot);
5028
5029 if (pot_right_ptr == NULL)
5030 {
5031 // special case, if "weak hash"
5032
5033 if (memcmp (hash_right->digest, LM_WEAK_HASH, 8) == 0)
5034 {
5035 weak_hash_found = 1;
5036
5037 pot_right_ptr = (pot_t *) mycalloc (1, sizeof (pot_t));
5038
5039 // in theory this is not needed, but we are paranoia:
5040
5041 memset (pot_right_ptr->plain_buf, 0, sizeof (pot_right_ptr->plain_buf));
5042 pot_right_ptr->plain_len = 0;
5043 }
5044 }
5045
5046 if ((pot_left_ptr == NULL) && (pot_right_ptr == NULL))
5047 {
5048 if (weak_hash_found == 1) myfree (pot_right_ptr); // this shouldn't happen at all: if weak_hash_found == 1, than pot_right_ptr is not NULL for sure
5049
5050 return;
5051 }
5052
5053 // at least one half was found:
5054
5055 log_info_nn ("");
5056
5057 input_buf[input_len] = 0;
5058
5059 // user
5060
5061 unsigned char *username = NULL;
5062 uint user_len = 0;
5063
5064 if (data.username)
5065 {
5066 user_t *user = hash_left->hash_info->user;
5067
5068 if (user)
5069 {
5070 username = (unsigned char *) (user->user_name);
5071
5072 user_len = user->user_len;
5073 }
5074 }
5075
5076 // mask the part which was not found
5077
5078 uint left_part_masked = 0;
5079 uint right_part_masked = 0;
5080
5081 uint mask_plain_len = strlen (LM_MASKED_PLAIN);
5082
5083 if (pot_left_ptr == NULL)
5084 {
5085 left_part_masked = 1;
5086
5087 pot_left_ptr = (pot_t *) mycalloc (1, sizeof (pot_t));
5088
5089 memset (pot_left_ptr->plain_buf, 0, sizeof (pot_left_ptr->plain_buf));
5090
5091 memcpy (pot_left_ptr->plain_buf, LM_MASKED_PLAIN, mask_plain_len);
5092 pot_left_ptr->plain_len = mask_plain_len;
5093 }
5094
5095 if (pot_right_ptr == NULL)
5096 {
5097 right_part_masked = 1;
5098
5099 pot_right_ptr = (pot_t *) mycalloc (1, sizeof (pot_t));
5100
5101 memset (pot_right_ptr->plain_buf, 0, sizeof (pot_right_ptr->plain_buf));
5102
5103 memcpy (pot_right_ptr->plain_buf, LM_MASKED_PLAIN, mask_plain_len);
5104 pot_right_ptr->plain_len = mask_plain_len;
5105 }
5106
5107 // create the pot_ptr out of pot_left_ptr and pot_right_ptr
5108
5109 pot_t pot_ptr;
5110
5111 pot_ptr.plain_len = pot_left_ptr->plain_len + pot_right_ptr->plain_len;
5112
5113 memcpy (pot_ptr.plain_buf, pot_left_ptr->plain_buf, pot_left_ptr->plain_len);
5114
5115 memcpy (pot_ptr.plain_buf + pot_left_ptr->plain_len, pot_right_ptr->plain_buf, pot_right_ptr->plain_len);
5116
5117 // do output the line
5118
5119 format_output (out_fp, input_buf, (unsigned char *) pot_ptr.plain_buf, pot_ptr.plain_len, 0, username, user_len);
5120
5121 if (weak_hash_found == 1) myfree (pot_right_ptr);
5122
5123 if (left_part_masked == 1) myfree (pot_left_ptr);
5124 if (right_part_masked == 1) myfree (pot_right_ptr);
5125 }
5126
5127 void handle_left_request (pot_t *pot, uint pot_cnt, char *input_buf, int input_len, hash_t *hashes_buf, int (*sort_by_pot) (const void *, const void *), FILE *out_fp)
5128 {
5129 pot_t pot_key;
5130
5131 memcpy (&pot_key.hash, hashes_buf, sizeof (hash_t));
5132
5133 pot_t *pot_ptr = (pot_t *) bsearch (&pot_key, pot, pot_cnt, sizeof (pot_t), sort_by_pot);
5134
5135 if (pot_ptr == NULL)
5136 {
5137 log_info_nn ("");
5138
5139 input_buf[input_len] = 0;
5140
5141 format_output (out_fp, input_buf, NULL, 0, 0, NULL, 0);
5142 }
5143 }
5144
5145 void handle_left_request_lm (pot_t *pot, uint pot_cnt, char *input_buf, int input_len, hash_t *hash_left, hash_t *hash_right, int (*sort_by_pot) (const void *, const void *), FILE *out_fp)
5146 {
5147 // left
5148
5149 pot_t pot_left_key;
5150
5151 memcpy (&pot_left_key.hash, hash_left, sizeof (hash_t));
5152
5153 pot_t *pot_left_ptr = (pot_t *) bsearch (&pot_left_key, pot, pot_cnt, sizeof (pot_t), sort_by_pot);
5154
5155 // right
5156
5157 pot_t pot_right_key;
5158
5159 memcpy (&pot_right_key.hash, hash_right, sizeof (hash_t));
5160
5161 pot_t *pot_right_ptr = (pot_t *) bsearch (&pot_right_key, pot, pot_cnt, sizeof (pot_t), sort_by_pot);
5162
5163 uint weak_hash_found = 0;
5164
5165 if (pot_right_ptr == NULL)
5166 {
5167 // special case, if "weak hash"
5168
5169 if (memcmp (hash_right->digest, LM_WEAK_HASH, 8) == 0)
5170 {
5171 weak_hash_found = 1;
5172
5173 // we just need that pot_right_ptr is not a NULL pointer
5174
5175 pot_right_ptr = (pot_t *) mycalloc (1, sizeof (pot_t));
5176 }
5177 }
5178
5179 if ((pot_left_ptr != NULL) && (pot_right_ptr != NULL))
5180 {
5181 if (weak_hash_found == 1) myfree (pot_right_ptr);
5182
5183 return;
5184 }
5185
5186 // ... at least one part was not cracked
5187
5188 log_info_nn ("");
5189
5190 input_buf[input_len] = 0;
5191
5192 // only show the hash part which is still not cracked
5193
5194 uint user_len = input_len - 32;
5195
5196 char *hash_output = (char *) mymalloc (33);
5197
5198 memcpy (hash_output, input_buf, input_len);
5199
5200 if (pot_left_ptr != NULL)
5201 {
5202 // only show right part (because left part was already found)
5203
5204 memcpy (hash_output + user_len, input_buf + user_len + 16, 16);
5205
5206 hash_output[user_len + 16] = 0;
5207 }
5208
5209 if (pot_right_ptr != NULL)
5210 {
5211 // only show left part (because right part was already found)
5212
5213 memcpy (hash_output + user_len, input_buf + user_len, 16);
5214
5215 hash_output[user_len + 16] = 0;
5216 }
5217
5218 format_output (out_fp, hash_output, NULL, 0, 0, NULL, 0);
5219
5220 myfree (hash_output);
5221
5222 if (weak_hash_found == 1) myfree (pot_right_ptr);
5223 }
5224
5225 uint setup_opencl_platforms_filter (char *opencl_platforms)
5226 {
5227 uint opencl_platforms_filter = 0;
5228
5229 if (opencl_platforms)
5230 {
5231 char *platforms = strdup (opencl_platforms);
5232
5233 char *next = strtok (platforms, ",");
5234
5235 do
5236 {
5237 int platform = atoi (next);
5238
5239 if (platform < 1 || platform > 32)
5240 {
5241 log_error ("ERROR: invalid OpenCL platform %u specified", platform);
5242
5243 exit (-1);
5244 }
5245
5246 opencl_platforms_filter |= 1 << (platform - 1);
5247
5248 } while ((next = strtok (NULL, ",")) != NULL);
5249
5250 free (platforms);
5251 }
5252 else
5253 {
5254 opencl_platforms_filter = -1;
5255 }
5256
5257 return opencl_platforms_filter;
5258 }
5259
5260 u32 setup_devices_filter (char *opencl_devices)
5261 {
5262 u32 devices_filter = 0;
5263
5264 if (opencl_devices)
5265 {
5266 char *devices = strdup (opencl_devices);
5267
5268 char *next = strtok (devices, ",");
5269
5270 do
5271 {
5272 int device_id = atoi (next);
5273
5274 if (device_id < 1 || device_id > 32)
5275 {
5276 log_error ("ERROR: invalid device_id %u specified", device_id);
5277
5278 exit (-1);
5279 }
5280
5281 devices_filter |= 1 << (device_id - 1);
5282
5283 } while ((next = strtok (NULL, ",")) != NULL);
5284
5285 free (devices);
5286 }
5287 else
5288 {
5289 devices_filter = -1;
5290 }
5291
5292 return devices_filter;
5293 }
5294
5295 cl_device_type setup_device_types_filter (char *opencl_device_types)
5296 {
5297 cl_device_type device_types_filter = 0;
5298
5299 if (opencl_device_types)
5300 {
5301 char *device_types = strdup (opencl_device_types);
5302
5303 char *next = strtok (device_types, ",");
5304
5305 do
5306 {
5307 int device_type = atoi (next);
5308
5309 if (device_type < 1 || device_type > 3)
5310 {
5311 log_error ("ERROR: invalid device_type %u specified", device_type);
5312
5313 exit (-1);
5314 }
5315
5316 device_types_filter |= 1 << device_type;
5317
5318 } while ((next = strtok (NULL, ",")) != NULL);
5319
5320 free (device_types);
5321 }
5322 else
5323 {
5324 // Do not use CPU by default, this often reduces GPU performance because
5325 // the CPU is too busy to handle GPU synchronization
5326
5327 device_types_filter = CL_DEVICE_TYPE_ALL & ~CL_DEVICE_TYPE_CPU;
5328 }
5329
5330 return device_types_filter;
5331 }
5332
5333 u32 get_random_num (const u32 min, const u32 max)
5334 {
5335 if (min == max) return (min);
5336
5337 return ((rand () % (max - min)) + min);
5338 }
5339
5340 u32 mydivc32 (const u32 dividend, const u32 divisor)
5341 {
5342 u32 quotient = dividend / divisor;
5343
5344 if (dividend % divisor) quotient++;
5345
5346 return quotient;
5347 }
5348
5349 u64 mydivc64 (const u64 dividend, const u64 divisor)
5350 {
5351 u64 quotient = dividend / divisor;
5352
5353 if (dividend % divisor) quotient++;
5354
5355 return quotient;
5356 }
5357
5358 void format_timer_display (struct tm *tm, char *buf, size_t len)
5359 {
5360 const char *time_entities_s[] = { "year", "day", "hour", "min", "sec" };
5361 const char *time_entities_m[] = { "years", "days", "hours", "mins", "secs" };
5362
5363 if (tm->tm_year - 70)
5364 {
5365 char *time_entity1 = ((tm->tm_year - 70) == 1) ? (char *) time_entities_s[0] : (char *) time_entities_m[0];
5366 char *time_entity2 = ( tm->tm_yday == 1) ? (char *) time_entities_s[1] : (char *) time_entities_m[1];
5367
5368 snprintf (buf, len - 1, "%d %s, %d %s", tm->tm_year - 70, time_entity1, tm->tm_yday, time_entity2);
5369 }
5370 else if (tm->tm_yday)
5371 {
5372 char *time_entity1 = (tm->tm_yday == 1) ? (char *) time_entities_s[1] : (char *) time_entities_m[1];
5373 char *time_entity2 = (tm->tm_hour == 1) ? (char *) time_entities_s[2] : (char *) time_entities_m[2];
5374
5375 snprintf (buf, len - 1, "%d %s, %d %s", tm->tm_yday, time_entity1, tm->tm_hour, time_entity2);
5376 }
5377 else if (tm->tm_hour)
5378 {
5379 char *time_entity1 = (tm->tm_hour == 1) ? (char *) time_entities_s[2] : (char *) time_entities_m[2];
5380 char *time_entity2 = (tm->tm_min == 1) ? (char *) time_entities_s[3] : (char *) time_entities_m[3];
5381
5382 snprintf (buf, len - 1, "%d %s, %d %s", tm->tm_hour, time_entity1, tm->tm_min, time_entity2);
5383 }
5384 else if (tm->tm_min)
5385 {
5386 char *time_entity1 = (tm->tm_min == 1) ? (char *) time_entities_s[3] : (char *) time_entities_m[3];
5387 char *time_entity2 = (tm->tm_sec == 1) ? (char *) time_entities_s[4] : (char *) time_entities_m[4];
5388
5389 snprintf (buf, len - 1, "%d %s, %d %s", tm->tm_min, time_entity1, tm->tm_sec, time_entity2);
5390 }
5391 else
5392 {
5393 char *time_entity1 = (tm->tm_sec == 1) ? (char *) time_entities_s[4] : (char *) time_entities_m[4];
5394
5395 snprintf (buf, len - 1, "%d %s", tm->tm_sec, time_entity1);
5396 }
5397 }
5398
5399 void format_speed_display (float val, char *buf, size_t len)
5400 {
5401 if (val <= 0)
5402 {
5403 buf[0] = '0';
5404 buf[1] = ' ';
5405 buf[2] = 0;
5406
5407 return;
5408 }
5409
5410 char units[7] = { ' ', 'k', 'M', 'G', 'T', 'P', 'E' };
5411
5412 uint level = 0;
5413
5414 while (val > 99999)
5415 {
5416 val /= 1000;
5417
5418 level++;
5419 }
5420
5421 /* generate output */
5422
5423 if (level == 0)
5424 {
5425 snprintf (buf, len - 1, "%.0f ", val);
5426 }
5427 else
5428 {
5429 snprintf (buf, len - 1, "%.1f %c", val, units[level]);
5430 }
5431 }
5432
5433 void lowercase (u8 *buf, int len)
5434 {
5435 for (int i = 0; i < len; i++) buf[i] = tolower (buf[i]);
5436 }
5437
5438 void uppercase (u8 *buf, int len)
5439 {
5440 for (int i = 0; i < len; i++) buf[i] = toupper (buf[i]);
5441 }
5442
5443 int fgetl (FILE *fp, char *line_buf)
5444 {
5445 int line_len = 0;
5446
5447 while (!feof (fp))
5448 {
5449 const int c = fgetc (fp);
5450
5451 if (c == EOF) break;
5452
5453 line_buf[line_len] = (char) c;
5454
5455 line_len++;
5456
5457 if (line_len == HCBUFSIZ) line_len--;
5458
5459 if (c == '\n') break;
5460 }
5461
5462 if (line_len == 0) return 0;
5463
5464 if (line_buf[line_len - 1] == '\n')
5465 {
5466 line_len--;
5467
5468 line_buf[line_len] = 0;
5469 }
5470
5471 if (line_len == 0) return 0;
5472
5473 if (line_buf[line_len - 1] == '\r')
5474 {
5475 line_len--;
5476
5477 line_buf[line_len] = 0;
5478 }
5479
5480 return (line_len);
5481 }
5482
5483 int in_superchop (char *buf)
5484 {
5485 int len = strlen (buf);
5486
5487 while (len)
5488 {
5489 if (buf[len - 1] == '\n')
5490 {
5491 len--;
5492
5493 continue;
5494 }
5495
5496 if (buf[len - 1] == '\r')
5497 {
5498 len--;
5499
5500 continue;
5501 }
5502
5503 break;
5504 }
5505
5506 buf[len] = 0;
5507
5508 return len;
5509 }
5510
5511 char **scan_directory (const char *path)
5512 {
5513 char *tmp_path = mystrdup (path);
5514
5515 size_t tmp_path_len = strlen (tmp_path);
5516
5517 while (tmp_path[tmp_path_len - 1] == '/' || tmp_path[tmp_path_len - 1] == '\\')
5518 {
5519 tmp_path[tmp_path_len - 1] = 0;
5520
5521 tmp_path_len = strlen (tmp_path);
5522 }
5523
5524 char **files = NULL;
5525
5526 int num_files = 0;
5527
5528 DIR *d = NULL;
5529
5530 if ((d = opendir (tmp_path)) != NULL)
5531 {
5532 #ifdef OSX
5533 struct dirent e;
5534
5535 for (;;) {
5536 memset (&e, 0, sizeof (e));
5537 struct dirent *de = NULL;
5538
5539 if (readdir_r (d, &e, &de) != 0)
5540 {
5541 log_error ("ERROR: readdir_r() failed");
5542
5543 break;
5544 }
5545
5546 if (de == NULL) break;
5547 #else
5548 struct dirent *de;
5549
5550 while ((de = readdir (d)) != NULL)
5551 {
5552 #endif
5553 if ((strcmp (de->d_name, ".") == 0) || (strcmp (de->d_name, "..") == 0)) continue;
5554
5555 int path_size = strlen (tmp_path) + 1 + strlen (de->d_name);
5556
5557 char *path_file = (char *) mymalloc (path_size + 1);
5558
5559 snprintf (path_file, path_size + 1, "%s/%s", tmp_path, de->d_name);
5560
5561 path_file[path_size] = 0;
5562
5563 DIR *d_test;
5564
5565 if ((d_test = opendir (path_file)) != NULL)
5566 {
5567 closedir (d_test);
5568
5569 myfree (path_file);
5570 }
5571 else
5572 {
5573 files = (char **) myrealloc (files, num_files * sizeof (char *), sizeof (char *));
5574
5575 num_files++;
5576
5577 files[num_files - 1] = path_file;
5578 }
5579 }
5580
5581 closedir (d);
5582 }
5583 else if (errno == ENOTDIR)
5584 {
5585 files = (char **) myrealloc (files, num_files * sizeof (char *), sizeof (char *));
5586
5587 num_files++;
5588
5589 files[num_files - 1] = mystrdup (path);
5590 }
5591
5592 files = (char **) myrealloc (files, num_files * sizeof (char *), sizeof (char *));
5593
5594 num_files++;
5595
5596 files[num_files - 1] = NULL;
5597
5598 myfree (tmp_path);
5599
5600 return (files);
5601 }
5602
5603 int count_dictionaries (char **dictionary_files)
5604 {
5605 if (dictionary_files == NULL) return 0;
5606
5607 int cnt = 0;
5608
5609 for (int d = 0; dictionary_files[d] != NULL; d++)
5610 {
5611 cnt++;
5612 }
5613
5614 return (cnt);
5615 }
5616
5617 char *stroptitype (const uint opti_type)
5618 {
5619 switch (opti_type)
5620 {
5621 case OPTI_TYPE_ZERO_BYTE: return ((char *) OPTI_STR_ZERO_BYTE); break;
5622 case OPTI_TYPE_PRECOMPUTE_INIT: return ((char *) OPTI_STR_PRECOMPUTE_INIT); break;
5623 case OPTI_TYPE_PRECOMPUTE_MERKLE: return ((char *) OPTI_STR_PRECOMPUTE_MERKLE); break;
5624 case OPTI_TYPE_PRECOMPUTE_PERMUT: return ((char *) OPTI_STR_PRECOMPUTE_PERMUT); break;
5625 case OPTI_TYPE_MEET_IN_MIDDLE: return ((char *) OPTI_STR_MEET_IN_MIDDLE); break;
5626 case OPTI_TYPE_EARLY_SKIP: return ((char *) OPTI_STR_EARLY_SKIP); break;
5627 case OPTI_TYPE_NOT_SALTED: return ((char *) OPTI_STR_NOT_SALTED); break;
5628 case OPTI_TYPE_NOT_ITERATED: return ((char *) OPTI_STR_NOT_ITERATED); break;
5629 case OPTI_TYPE_PREPENDED_SALT: return ((char *) OPTI_STR_PREPENDED_SALT); break;
5630 case OPTI_TYPE_APPENDED_SALT: return ((char *) OPTI_STR_APPENDED_SALT); break;
5631 case OPTI_TYPE_SINGLE_HASH: return ((char *) OPTI_STR_SINGLE_HASH); break;
5632 case OPTI_TYPE_SINGLE_SALT: return ((char *) OPTI_STR_SINGLE_SALT); break;
5633 case OPTI_TYPE_BRUTE_FORCE: return ((char *) OPTI_STR_BRUTE_FORCE); break;
5634 case OPTI_TYPE_RAW_HASH: return ((char *) OPTI_STR_RAW_HASH); break;
5635 case OPTI_TYPE_SLOW_HASH_SIMD: return ((char *) OPTI_STR_SLOW_HASH_SIMD); break;
5636 case OPTI_TYPE_USES_BITS_8: return ((char *) OPTI_STR_USES_BITS_8); break;
5637 case OPTI_TYPE_USES_BITS_16: return ((char *) OPTI_STR_USES_BITS_16); break;
5638 case OPTI_TYPE_USES_BITS_32: return ((char *) OPTI_STR_USES_BITS_32); break;
5639 case OPTI_TYPE_USES_BITS_64: return ((char *) OPTI_STR_USES_BITS_64); break;
5640 }
5641
5642 return (NULL);
5643 }
5644
5645 char *strparser (const uint parser_status)
5646 {
5647 switch (parser_status)
5648 {
5649 case PARSER_OK: return ((char *) PA_000); break;
5650 case PARSER_COMMENT: return ((char *) PA_001); break;
5651 case PARSER_GLOBAL_ZERO: return ((char *) PA_002); break;
5652 case PARSER_GLOBAL_LENGTH: return ((char *) PA_003); break;
5653 case PARSER_HASH_LENGTH: return ((char *) PA_004); break;
5654 case PARSER_HASH_VALUE: return ((char *) PA_005); break;
5655 case PARSER_SALT_LENGTH: return ((char *) PA_006); break;
5656 case PARSER_SALT_VALUE: return ((char *) PA_007); break;
5657 case PARSER_SALT_ITERATION: return ((char *) PA_008); break;
5658 case PARSER_SEPARATOR_UNMATCHED: return ((char *) PA_009); break;
5659 case PARSER_SIGNATURE_UNMATCHED: return ((char *) PA_010); break;
5660 case PARSER_HCCAP_FILE_SIZE: return ((char *) PA_011); break;
5661 case PARSER_HCCAP_EAPOL_SIZE: return ((char *) PA_012); break;
5662 case PARSER_PSAFE2_FILE_SIZE: return ((char *) PA_013); break;
5663 case PARSER_PSAFE3_FILE_SIZE: return ((char *) PA_014); break;
5664 case PARSER_TC_FILE_SIZE: return ((char *) PA_015); break;
5665 case PARSER_SIP_AUTH_DIRECTIVE: return ((char *) PA_016); break;
5666 }
5667
5668 return ((char *) PA_255);
5669 }
5670
5671 char *strhashtype (const uint hash_mode)
5672 {
5673 switch (hash_mode)
5674 {
5675 case 0: return ((char *) HT_00000); break;
5676 case 10: return ((char *) HT_00010); break;
5677 case 11: return ((char *) HT_00011); break;
5678 case 12: return ((char *) HT_00012); break;
5679 case 20: return ((char *) HT_00020); break;
5680 case 21: return ((char *) HT_00021); break;
5681 case 22: return ((char *) HT_00022); break;
5682 case 23: return ((char *) HT_00023); break;
5683 case 30: return ((char *) HT_00030); break;
5684 case 40: return ((char *) HT_00040); break;
5685 case 50: return ((char *) HT_00050); break;
5686 case 60: return ((char *) HT_00060); break;
5687 case 100: return ((char *) HT_00100); break;
5688 case 101: return ((char *) HT_00101); break;
5689 case 110: return ((char *) HT_00110); break;
5690 case 111: return ((char *) HT_00111); break;
5691 case 112: return ((char *) HT_00112); break;
5692 case 120: return ((char *) HT_00120); break;
5693 case 121: return ((char *) HT_00121); break;
5694 case 122: return ((char *) HT_00122); break;
5695 case 124: return ((char *) HT_00124); break;
5696 case 125: return ((char *) HT_00125); break;
5697 case 130: return ((char *) HT_00130); break;
5698 case 131: return ((char *) HT_00131); break;
5699 case 132: return ((char *) HT_00132); break;
5700 case 133: return ((char *) HT_00133); break;
5701 case 140: return ((char *) HT_00140); break;
5702 case 141: return ((char *) HT_00141); break;
5703 case 150: return ((char *) HT_00150); break;
5704 case 160: return ((char *) HT_00160); break;
5705 case 190: return ((char *) HT_00190); break;
5706 case 200: return ((char *) HT_00200); break;
5707 case 300: return ((char *) HT_00300); break;
5708 case 400: return ((char *) HT_00400); break;
5709 case 500: return ((char *) HT_00500); break;
5710 case 501: return ((char *) HT_00501); break;
5711 case 900: return ((char *) HT_00900); break;
5712 case 910: return ((char *) HT_00910); break;
5713 case 1000: return ((char *) HT_01000); break;
5714 case 1100: return ((char *) HT_01100); break;
5715 case 1400: return ((char *) HT_01400); break;
5716 case 1410: return ((char *) HT_01410); break;
5717 case 1420: return ((char *) HT_01420); break;
5718 case 1421: return ((char *) HT_01421); break;
5719 case 1430: return ((char *) HT_01430); break;
5720 case 1440: return ((char *) HT_01440); break;
5721 case 1441: return ((char *) HT_01441); break;
5722 case 1450: return ((char *) HT_01450); break;
5723 case 1460: return ((char *) HT_01460); break;
5724 case 1500: return ((char *) HT_01500); break;
5725 case 1600: return ((char *) HT_01600); break;
5726 case 1700: return ((char *) HT_01700); break;
5727 case 1710: return ((char *) HT_01710); break;
5728 case 1711: return ((char *) HT_01711); break;
5729 case 1720: return ((char *) HT_01720); break;
5730 case 1722: return ((char *) HT_01722); break;
5731 case 1730: return ((char *) HT_01730); break;
5732 case 1731: return ((char *) HT_01731); break;
5733 case 1740: return ((char *) HT_01740); break;
5734 case 1750: return ((char *) HT_01750); break;
5735 case 1760: return ((char *) HT_01760); break;
5736 case 1800: return ((char *) HT_01800); break;
5737 case 2100: return ((char *) HT_02100); break;
5738 case 2400: return ((char *) HT_02400); break;
5739 case 2410: return ((char *) HT_02410); break;
5740 case 2500: return ((char *) HT_02500); break;
5741 case 2600: return ((char *) HT_02600); break;
5742 case 2611: return ((char *) HT_02611); break;
5743 case 2612: return ((char *) HT_02612); break;
5744 case 2711: return ((char *) HT_02711); break;
5745 case 2811: return ((char *) HT_02811); break;
5746 case 3000: return ((char *) HT_03000); break;
5747 case 3100: return ((char *) HT_03100); break;
5748 case 3200: return ((char *) HT_03200); break;
5749 case 3710: return ((char *) HT_03710); break;
5750 case 3711: return ((char *) HT_03711); break;
5751 case 3800: return ((char *) HT_03800); break;
5752 case 4300: return ((char *) HT_04300); break;
5753 case 4400: return ((char *) HT_04400); break;
5754 case 4500: return ((char *) HT_04500); break;
5755 case 4700: return ((char *) HT_04700); break;
5756 case 4800: return ((char *) HT_04800); break;
5757 case 4900: return ((char *) HT_04900); break;
5758 case 5000: return ((char *) HT_05000); break;
5759 case 5100: return ((char *) HT_05100); break;
5760 case 5200: return ((char *) HT_05200); break;
5761 case 5300: return ((char *) HT_05300); break;
5762 case 5400: return ((char *) HT_05400); break;
5763 case 5500: return ((char *) HT_05500); break;
5764 case 5600: return ((char *) HT_05600); break;
5765 case 5700: return ((char *) HT_05700); break;
5766 case 5800: return ((char *) HT_05800); break;
5767 case 6000: return ((char *) HT_06000); break;
5768 case 6100: return ((char *) HT_06100); break;
5769 case 6211: return ((char *) HT_06211); break;
5770 case 6212: return ((char *) HT_06212); break;
5771 case 6213: return ((char *) HT_06213); break;
5772 case 6221: return ((char *) HT_06221); break;
5773 case 6222: return ((char *) HT_06222); break;
5774 case 6223: return ((char *) HT_06223); break;
5775 case 6231: return ((char *) HT_06231); break;
5776 case 6232: return ((char *) HT_06232); break;
5777 case 6233: return ((char *) HT_06233); break;
5778 case 6241: return ((char *) HT_06241); break;
5779 case 6242: return ((char *) HT_06242); break;
5780 case 6243: return ((char *) HT_06243); break;
5781 case 6300: return ((char *) HT_06300); break;
5782 case 6400: return ((char *) HT_06400); break;
5783 case 6500: return ((char *) HT_06500); break;
5784 case 6600: return ((char *) HT_06600); break;
5785 case 6700: return ((char *) HT_06700); break;
5786 case 6800: return ((char *) HT_06800); break;
5787 case 6900: return ((char *) HT_06900); break;
5788 case 7100: return ((char *) HT_07100); break;
5789 case 7200: return ((char *) HT_07200); break;
5790 case 7300: return ((char *) HT_07300); break;
5791 case 7400: return ((char *) HT_07400); break;
5792 case 7500: return ((char *) HT_07500); break;
5793 case 7600: return ((char *) HT_07600); break;
5794 case 7700: return ((char *) HT_07700); break;
5795 case 7800: return ((char *) HT_07800); break;
5796 case 7900: return ((char *) HT_07900); break;
5797 case 8000: return ((char *) HT_08000); break;
5798 case 8100: return ((char *) HT_08100); break;
5799 case 8200: return ((char *) HT_08200); break;
5800 case 8300: return ((char *) HT_08300); break;
5801 case 8400: return ((char *) HT_08400); break;
5802 case 8500: return ((char *) HT_08500); break;
5803 case 8600: return ((char *) HT_08600); break;
5804 case 8700: return ((char *) HT_08700); break;
5805 case 8800: return ((char *) HT_08800); break;
5806 case 8900: return ((char *) HT_08900); break;
5807 case 9000: return ((char *) HT_09000); break;
5808 case 9100: return ((char *) HT_09100); break;
5809 case 9200: return ((char *) HT_09200); break;
5810 case 9300: return ((char *) HT_09300); break;
5811 case 9400: return ((char *) HT_09400); break;
5812 case 9500: return ((char *) HT_09500); break;
5813 case 9600: return ((char *) HT_09600); break;
5814 case 9700: return ((char *) HT_09700); break;
5815 case 9710: return ((char *) HT_09710); break;
5816 case 9720: return ((char *) HT_09720); break;
5817 case 9800: return ((char *) HT_09800); break;
5818 case 9810: return ((char *) HT_09810); break;
5819 case 9820: return ((char *) HT_09820); break;
5820 case 9900: return ((char *) HT_09900); break;
5821 case 10000: return ((char *) HT_10000); break;
5822 case 10100: return ((char *) HT_10100); break;
5823 case 10200: return ((char *) HT_10200); break;
5824 case 10300: return ((char *) HT_10300); break;
5825 case 10400: return ((char *) HT_10400); break;
5826 case 10410: return ((char *) HT_10410); break;
5827 case 10420: return ((char *) HT_10420); break;
5828 case 10500: return ((char *) HT_10500); break;
5829 case 10600: return ((char *) HT_10600); break;
5830 case 10700: return ((char *) HT_10700); break;
5831 case 10800: return ((char *) HT_10800); break;
5832 case 10900: return ((char *) HT_10900); break;
5833 case 11000: return ((char *) HT_11000); break;
5834 case 11100: return ((char *) HT_11100); break;
5835 case 11200: return ((char *) HT_11200); break;
5836 case 11300: return ((char *) HT_11300); break;
5837 case 11400: return ((char *) HT_11400); break;
5838 case 11500: return ((char *) HT_11500); break;
5839 case 11600: return ((char *) HT_11600); break;
5840 case 11700: return ((char *) HT_11700); break;
5841 case 11800: return ((char *) HT_11800); break;
5842 case 11900: return ((char *) HT_11900); break;
5843 case 12000: return ((char *) HT_12000); break;
5844 case 12100: return ((char *) HT_12100); break;
5845 case 12200: return ((char *) HT_12200); break;
5846 case 12300: return ((char *) HT_12300); break;
5847 case 12400: return ((char *) HT_12400); break;
5848 case 12500: return ((char *) HT_12500); break;
5849 case 12600: return ((char *) HT_12600); break;
5850 case 12700: return ((char *) HT_12700); break;
5851 case 12800: return ((char *) HT_12800); break;
5852 case 12900: return ((char *) HT_12900); break;
5853 case 13000: return ((char *) HT_13000); break;
5854 case 13100: return ((char *) HT_13100); break;
5855 case 13200: return ((char *) HT_13200); break;
5856 case 13300: return ((char *) HT_13300); break;
5857 case 13400: return ((char *) HT_13400); break;
5858 case 13500: return ((char *) HT_13500); break;
5859 case 13600: return ((char *) HT_13600); break;
5860 }
5861
5862 return ((char *) "Unknown");
5863 }
5864
5865 char *strstatus (const uint devices_status)
5866 {
5867 switch (devices_status)
5868 {
5869 case STATUS_INIT: return ((char *) ST_0000); break;
5870 case STATUS_STARTING: return ((char *) ST_0001); break;
5871 case STATUS_RUNNING: return ((char *) ST_0002); break;
5872 case STATUS_PAUSED: return ((char *) ST_0003); break;
5873 case STATUS_EXHAUSTED: return ((char *) ST_0004); break;
5874 case STATUS_CRACKED: return ((char *) ST_0005); break;
5875 case STATUS_ABORTED: return ((char *) ST_0006); break;
5876 case STATUS_QUIT: return ((char *) ST_0007); break;
5877 case STATUS_BYPASS: return ((char *) ST_0008); break;
5878 case STATUS_STOP_AT_CHECKPOINT: return ((char *) ST_0009); break;
5879 case STATUS_AUTOTUNE: return ((char *) ST_0010); break;
5880 }
5881
5882 return ((char *) "Unknown");
5883 }
5884
5885 void ascii_digest (char *out_buf, uint salt_pos, uint digest_pos)
5886 {
5887 uint hash_type = data.hash_type;
5888 uint hash_mode = data.hash_mode;
5889 uint salt_type = data.salt_type;
5890 uint opts_type = data.opts_type;
5891 uint opti_type = data.opti_type;
5892 uint dgst_size = data.dgst_size;
5893
5894 char *hashfile = data.hashfile;
5895
5896 uint len = 4096;
5897
5898 uint digest_buf[64] = { 0 };
5899
5900 u64 *digest_buf64 = (u64 *) digest_buf;
5901
5902 char *digests_buf_ptr = (char *) data.digests_buf;
5903
5904 memcpy (digest_buf, digests_buf_ptr + (data.salts_buf[salt_pos].digests_offset * dgst_size) + (digest_pos * dgst_size), dgst_size);
5905
5906 if (opti_type & OPTI_TYPE_PRECOMPUTE_PERMUT)
5907 {
5908 uint tt;
5909
5910 switch (hash_type)
5911 {
5912 case HASH_TYPE_DESCRYPT:
5913 FP (digest_buf[1], digest_buf[0], tt);
5914 break;
5915
5916 case HASH_TYPE_DESRACF:
5917 digest_buf[0] = rotl32 (digest_buf[0], 29);
5918 digest_buf[1] = rotl32 (digest_buf[1], 29);
5919
5920 FP (digest_buf[1], digest_buf[0], tt);
5921 break;
5922
5923 case HASH_TYPE_LM:
5924 FP (digest_buf[1], digest_buf[0], tt);
5925 break;
5926
5927 case HASH_TYPE_NETNTLM:
5928 digest_buf[0] = rotl32 (digest_buf[0], 29);
5929 digest_buf[1] = rotl32 (digest_buf[1], 29);
5930 digest_buf[2] = rotl32 (digest_buf[2], 29);
5931 digest_buf[3] = rotl32 (digest_buf[3], 29);
5932
5933 FP (digest_buf[1], digest_buf[0], tt);
5934 FP (digest_buf[3], digest_buf[2], tt);
5935 break;
5936
5937 case HASH_TYPE_BSDICRYPT:
5938 digest_buf[0] = rotl32 (digest_buf[0], 31);
5939 digest_buf[1] = rotl32 (digest_buf[1], 31);
5940
5941 FP (digest_buf[1], digest_buf[0], tt);
5942 break;
5943 }
5944 }
5945
5946 if (opti_type & OPTI_TYPE_PRECOMPUTE_MERKLE)
5947 {
5948 switch (hash_type)
5949 {
5950 case HASH_TYPE_MD4:
5951 digest_buf[0] += MD4M_A;
5952 digest_buf[1] += MD4M_B;
5953 digest_buf[2] += MD4M_C;
5954 digest_buf[3] += MD4M_D;
5955 break;
5956
5957 case HASH_TYPE_MD5:
5958 digest_buf[0] += MD5M_A;
5959 digest_buf[1] += MD5M_B;
5960 digest_buf[2] += MD5M_C;
5961 digest_buf[3] += MD5M_D;
5962 break;
5963
5964 case HASH_TYPE_SHA1:
5965 digest_buf[0] += SHA1M_A;
5966 digest_buf[1] += SHA1M_B;
5967 digest_buf[2] += SHA1M_C;
5968 digest_buf[3] += SHA1M_D;
5969 digest_buf[4] += SHA1M_E;
5970 break;
5971
5972 case HASH_TYPE_SHA256:
5973 digest_buf[0] += SHA256M_A;
5974 digest_buf[1] += SHA256M_B;
5975 digest_buf[2] += SHA256M_C;
5976 digest_buf[3] += SHA256M_D;
5977 digest_buf[4] += SHA256M_E;
5978 digest_buf[5] += SHA256M_F;
5979 digest_buf[6] += SHA256M_G;
5980 digest_buf[7] += SHA256M_H;
5981 break;
5982
5983 case HASH_TYPE_SHA384:
5984 digest_buf64[0] += SHA384M_A;
5985 digest_buf64[1] += SHA384M_B;
5986 digest_buf64[2] += SHA384M_C;
5987 digest_buf64[3] += SHA384M_D;
5988 digest_buf64[4] += SHA384M_E;
5989 digest_buf64[5] += SHA384M_F;
5990 digest_buf64[6] += 0;
5991 digest_buf64[7] += 0;
5992 break;
5993
5994 case HASH_TYPE_SHA512:
5995 digest_buf64[0] += SHA512M_A;
5996 digest_buf64[1] += SHA512M_B;
5997 digest_buf64[2] += SHA512M_C;
5998 digest_buf64[3] += SHA512M_D;
5999 digest_buf64[4] += SHA512M_E;
6000 digest_buf64[5] += SHA512M_F;
6001 digest_buf64[6] += SHA512M_G;
6002 digest_buf64[7] += SHA512M_H;
6003 break;
6004 }
6005 }
6006
6007 if (opts_type & OPTS_TYPE_PT_GENERATE_LE)
6008 {
6009 if (dgst_size == DGST_SIZE_4_2)
6010 {
6011 for (int i = 0; i < 2; i++) digest_buf[i] = byte_swap_32 (digest_buf[i]);
6012 }
6013 else if (dgst_size == DGST_SIZE_4_4)
6014 {
6015 for (int i = 0; i < 4; i++) digest_buf[i] = byte_swap_32 (digest_buf[i]);
6016 }
6017 else if (dgst_size == DGST_SIZE_4_5)
6018 {
6019 for (int i = 0; i < 5; i++) digest_buf[i] = byte_swap_32 (digest_buf[i]);
6020 }
6021 else if (dgst_size == DGST_SIZE_4_6)
6022 {
6023 for (int i = 0; i < 6; i++) digest_buf[i] = byte_swap_32 (digest_buf[i]);
6024 }
6025 else if (dgst_size == DGST_SIZE_4_8)
6026 {
6027 for (int i = 0; i < 8; i++) digest_buf[i] = byte_swap_32 (digest_buf[i]);
6028 }
6029 else if ((dgst_size == DGST_SIZE_4_16) || (dgst_size == DGST_SIZE_8_8)) // same size, same result :)
6030 {
6031 if (hash_type == HASH_TYPE_WHIRLPOOL)
6032 {
6033 for (int i = 0; i < 16; i++) digest_buf[i] = byte_swap_32 (digest_buf[i]);
6034 }
6035 else if (hash_type == HASH_TYPE_SHA384)
6036 {
6037 for (int i = 0; i < 8; i++) digest_buf64[i] = byte_swap_64 (digest_buf64[i]);
6038 }
6039 else if (hash_type == HASH_TYPE_SHA512)
6040 {
6041 for (int i = 0; i < 8; i++) digest_buf64[i] = byte_swap_64 (digest_buf64[i]);
6042 }
6043 else if (hash_type == HASH_TYPE_GOST)
6044 {
6045 for (int i = 0; i < 16; i++) digest_buf[i] = byte_swap_32 (digest_buf[i]);
6046 }
6047 }
6048 else if (dgst_size == DGST_SIZE_4_64)
6049 {
6050 for (int i = 0; i < 64; i++) digest_buf[i] = byte_swap_32 (digest_buf[i]);
6051 }
6052 else if (dgst_size == DGST_SIZE_8_25)
6053 {
6054 for (int i = 0; i < 25; i++) digest_buf64[i] = byte_swap_64 (digest_buf64[i]);
6055 }
6056 }
6057
6058 uint isSalted = ((data.salt_type == SALT_TYPE_INTERN)
6059 | (data.salt_type == SALT_TYPE_EXTERN)
6060 | (data.salt_type == SALT_TYPE_EMBEDDED));
6061
6062 salt_t salt;
6063
6064 if (isSalted)
6065 {
6066 memset (&salt, 0, sizeof (salt_t));
6067
6068 memcpy (&salt, &data.salts_buf[salt_pos], sizeof (salt_t));
6069
6070 char *ptr = (char *) salt.salt_buf;
6071
6072 uint len = salt.salt_len;
6073
6074 if (opti_type & OPTI_TYPE_PRECOMPUTE_PERMUT)
6075 {
6076 uint tt;
6077
6078 switch (hash_type)
6079 {
6080 case HASH_TYPE_NETNTLM:
6081
6082 salt.salt_buf[0] = rotr32 (salt.salt_buf[0], 3);
6083 salt.salt_buf[1] = rotr32 (salt.salt_buf[1], 3);
6084
6085 FP (salt.salt_buf[1], salt.salt_buf[0], tt);
6086
6087 break;
6088 }
6089 }
6090
6091 if (opts_type & OPTS_TYPE_ST_UNICODE)
6092 {
6093 for (uint i = 0, j = 0; i < len; i += 1, j += 2)
6094 {
6095 ptr[i] = ptr[j];
6096 }
6097
6098 len = len / 2;
6099 }
6100
6101 if (opts_type & OPTS_TYPE_ST_GENERATE_LE)
6102 {
6103 uint max = salt.salt_len / 4;
6104
6105 if (len % 4) max++;
6106
6107 for (uint i = 0; i < max; i++)
6108 {
6109 salt.salt_buf[i] = byte_swap_32 (salt.salt_buf[i]);
6110 }
6111 }
6112
6113 if (opts_type & OPTS_TYPE_ST_HEX)
6114 {
6115 char tmp[64] = { 0 };
6116
6117 for (uint i = 0, j = 0; i < len; i += 1, j += 2)
6118 {
6119 sprintf (tmp + j, "%02x", (unsigned char) ptr[i]);
6120 }
6121
6122 len = len * 2;
6123
6124 memcpy (ptr, tmp, len);
6125 }
6126
6127 uint memset_size = ((48 - (int) len) > 0) ? (48 - len) : 0;
6128
6129 memset (ptr + len, 0, memset_size);
6130
6131 salt.salt_len = len;
6132 }
6133
6134 //
6135 // some modes require special encoding
6136 //
6137
6138 uint out_buf_plain[256] = { 0 };
6139 uint out_buf_salt[256] = { 0 };
6140
6141 char tmp_buf[1024] = { 0 };
6142
6143 char *ptr_plain = (char *) out_buf_plain;
6144 char *ptr_salt = (char *) out_buf_salt;
6145
6146 if (hash_mode == 22)
6147 {
6148 char username[30] = { 0 };
6149
6150 memcpy (username, salt.salt_buf, salt.salt_len - 22);
6151
6152 char sig[6] = { 'n', 'r', 'c', 's', 't', 'n' };
6153
6154 u16 *ptr = (u16 *) digest_buf;
6155
6156 tmp_buf[ 0] = sig[0];
6157 tmp_buf[ 1] = int_to_base64 (((ptr[1]) >> 12) & 0x3f);
6158 tmp_buf[ 2] = int_to_base64 (((ptr[1]) >> 6) & 0x3f);
6159 tmp_buf[ 3] = int_to_base64 (((ptr[1]) >> 0) & 0x3f);
6160 tmp_buf[ 4] = int_to_base64 (((ptr[0]) >> 12) & 0x3f);
6161 tmp_buf[ 5] = int_to_base64 (((ptr[0]) >> 6) & 0x3f);
6162 tmp_buf[ 6] = sig[1];
6163 tmp_buf[ 7] = int_to_base64 (((ptr[0]) >> 0) & 0x3f);
6164 tmp_buf[ 8] = int_to_base64 (((ptr[3]) >> 12) & 0x3f);
6165 tmp_buf[ 9] = int_to_base64 (((ptr[3]) >> 6) & 0x3f);
6166 tmp_buf[10] = int_to_base64 (((ptr[3]) >> 0) & 0x3f);
6167 tmp_buf[11] = int_to_base64 (((ptr[2]) >> 12) & 0x3f);
6168 tmp_buf[12] = sig[2];
6169 tmp_buf[13] = int_to_base64 (((ptr[2]) >> 6) & 0x3f);
6170 tmp_buf[14] = int_to_base64 (((ptr[2]) >> 0) & 0x3f);
6171 tmp_buf[15] = int_to_base64 (((ptr[5]) >> 12) & 0x3f);
6172 tmp_buf[16] = int_to_base64 (((ptr[5]) >> 6) & 0x3f);
6173 tmp_buf[17] = sig[3];
6174 tmp_buf[18] = int_to_base64 (((ptr[5]) >> 0) & 0x3f);
6175 tmp_buf[19] = int_to_base64 (((ptr[4]) >> 12) & 0x3f);
6176 tmp_buf[20] = int_to_base64 (((ptr[4]) >> 6) & 0x3f);
6177 tmp_buf[21] = int_to_base64 (((ptr[4]) >> 0) & 0x3f);
6178 tmp_buf[22] = int_to_base64 (((ptr[7]) >> 12) & 0x3f);
6179 tmp_buf[23] = sig[4];
6180 tmp_buf[24] = int_to_base64 (((ptr[7]) >> 6) & 0x3f);
6181 tmp_buf[25] = int_to_base64 (((ptr[7]) >> 0) & 0x3f);
6182 tmp_buf[26] = int_to_base64 (((ptr[6]) >> 12) & 0x3f);
6183 tmp_buf[27] = int_to_base64 (((ptr[6]) >> 6) & 0x3f);
6184 tmp_buf[28] = int_to_base64 (((ptr[6]) >> 0) & 0x3f);
6185 tmp_buf[29] = sig[5];
6186
6187 snprintf (out_buf, len-1, "%s:%s",
6188 tmp_buf,
6189 username);
6190 }
6191 else if (hash_mode == 23)
6192 {
6193 // do not show the skyper part in output
6194
6195 char *salt_buf_ptr = (char *) salt.salt_buf;
6196
6197 salt_buf_ptr[salt.salt_len - 8] = 0;
6198
6199 snprintf (out_buf, len-1, "%08x%08x%08x%08x:%s",
6200 digest_buf[0],
6201 digest_buf[1],
6202 digest_buf[2],
6203 digest_buf[3],
6204 salt_buf_ptr);
6205 }
6206 else if (hash_mode == 101)
6207 {
6208 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6209
6210 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6211 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6212 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6213 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6214 digest_buf[4] = byte_swap_32 (digest_buf[4]);
6215
6216 memcpy (tmp_buf, digest_buf, 20);
6217
6218 base64_encode (int_to_base64, (const u8 *) tmp_buf, 20, (u8 *) ptr_plain);
6219
6220 snprintf (out_buf, len-1, "{SHA}%s", ptr_plain);
6221 }
6222 else if (hash_mode == 111)
6223 {
6224 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6225
6226 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6227 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6228 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6229 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6230 digest_buf[4] = byte_swap_32 (digest_buf[4]);
6231
6232 memcpy (tmp_buf, digest_buf, 20);
6233 memcpy (tmp_buf + 20, salt.salt_buf, salt.salt_len);
6234
6235 base64_encode (int_to_base64, (const u8 *) tmp_buf, 20 + salt.salt_len, (u8 *) ptr_plain);
6236
6237 snprintf (out_buf, len-1, "{SSHA}%s", ptr_plain);
6238 }
6239 else if ((hash_mode == 122) || (hash_mode == 125))
6240 {
6241 snprintf (out_buf, len-1, "%s%08x%08x%08x%08x%08x",
6242 (char *) salt.salt_buf,
6243 digest_buf[0],
6244 digest_buf[1],
6245 digest_buf[2],
6246 digest_buf[3],
6247 digest_buf[4]);
6248 }
6249 else if (hash_mode == 124)
6250 {
6251 snprintf (out_buf, len-1, "sha1$%s$%08x%08x%08x%08x%08x",
6252 (char *) salt.salt_buf,
6253 digest_buf[0],
6254 digest_buf[1],
6255 digest_buf[2],
6256 digest_buf[3],
6257 digest_buf[4]);
6258 }
6259 else if (hash_mode == 131)
6260 {
6261 snprintf (out_buf, len-1, "0x0100%s%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
6262 (char *) salt.salt_buf,
6263 0, 0, 0, 0, 0,
6264 digest_buf[0],
6265 digest_buf[1],
6266 digest_buf[2],
6267 digest_buf[3],
6268 digest_buf[4]);
6269 }
6270 else if (hash_mode == 132)
6271 {
6272 snprintf (out_buf, len-1, "0x0100%s%08x%08x%08x%08x%08x",
6273 (char *) salt.salt_buf,
6274 digest_buf[0],
6275 digest_buf[1],
6276 digest_buf[2],
6277 digest_buf[3],
6278 digest_buf[4]);
6279 }
6280 else if (hash_mode == 133)
6281 {
6282 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6283
6284 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6285 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6286 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6287 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6288 digest_buf[4] = byte_swap_32 (digest_buf[4]);
6289
6290 memcpy (tmp_buf, digest_buf, 20);
6291
6292 base64_encode (int_to_base64, (const u8 *) tmp_buf, 20, (u8 *) ptr_plain);
6293
6294 snprintf (out_buf, len-1, "%s", ptr_plain);
6295 }
6296 else if (hash_mode == 141)
6297 {
6298 memcpy (tmp_buf, salt.salt_buf, salt.salt_len);
6299
6300 base64_encode (int_to_base64, (const u8 *) tmp_buf, salt.salt_len, (u8 *) ptr_salt);
6301
6302 memset (tmp_buf, 0, sizeof (tmp_buf));
6303
6304 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6305
6306 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6307 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6308 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6309 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6310 digest_buf[4] = byte_swap_32 (digest_buf[4]);
6311
6312 memcpy (tmp_buf, digest_buf, 20);
6313
6314 base64_encode (int_to_base64, (const u8 *) tmp_buf, 20, (u8 *) ptr_plain);
6315
6316 ptr_plain[27] = 0;
6317
6318 snprintf (out_buf, len-1, "%s%s*%s", SIGNATURE_EPISERVER, ptr_salt, ptr_plain);
6319 }
6320 else if (hash_mode == 400)
6321 {
6322 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6323
6324 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6325 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6326 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6327 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6328
6329 phpass_encode ((unsigned char *) digest_buf, (unsigned char *) ptr_plain);
6330
6331 snprintf (out_buf, len-1, "%s%s%s", (char *) salt.salt_sign, (char *) salt.salt_buf, (char *) ptr_plain);
6332 }
6333 else if (hash_mode == 500)
6334 {
6335 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6336
6337 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6338 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6339 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6340 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6341
6342 md5crypt_encode ((unsigned char *) digest_buf, (unsigned char *) ptr_plain);
6343
6344 if (salt.salt_iter == ROUNDS_MD5CRYPT)
6345 {
6346 snprintf (out_buf, len-1, "$1$%s$%s", (char *) salt.salt_buf, (char *) ptr_plain);
6347 }
6348 else
6349 {
6350 snprintf (out_buf, len-1, "$1$rounds=%i$%s$%s", salt.salt_iter, (char *) salt.salt_buf, (char *) ptr_plain);
6351 }
6352 }
6353 else if (hash_mode == 501)
6354 {
6355 uint digest_idx = salt.digests_offset + digest_pos;
6356
6357 hashinfo_t **hashinfo_ptr = data.hash_info;
6358 char *hash_buf = hashinfo_ptr[digest_idx]->orighash;
6359
6360 snprintf (out_buf, len-1, "%s", hash_buf);
6361 }
6362 else if (hash_mode == 1421)
6363 {
6364 u8 *salt_ptr = (u8 *) salt.salt_buf;
6365
6366 snprintf (out_buf, len-1, "%c%c%c%c%c%c%08x%08x%08x%08x%08x%08x%08x%08x",
6367 salt_ptr[0],
6368 salt_ptr[1],
6369 salt_ptr[2],
6370 salt_ptr[3],
6371 salt_ptr[4],
6372 salt_ptr[5],
6373 digest_buf[0],
6374 digest_buf[1],
6375 digest_buf[2],
6376 digest_buf[3],
6377 digest_buf[4],
6378 digest_buf[5],
6379 digest_buf[6],
6380 digest_buf[7]);
6381 }
6382 else if (hash_mode == 1441)
6383 {
6384 memcpy (tmp_buf, salt.salt_buf, salt.salt_len);
6385
6386 base64_encode (int_to_base64, (const u8 *) tmp_buf, salt.salt_len, (u8 *) ptr_salt);
6387
6388 memset (tmp_buf, 0, sizeof (tmp_buf));
6389
6390 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6391
6392 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6393 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6394 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6395 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6396 digest_buf[4] = byte_swap_32 (digest_buf[4]);
6397 digest_buf[5] = byte_swap_32 (digest_buf[5]);
6398 digest_buf[6] = byte_swap_32 (digest_buf[6]);
6399 digest_buf[7] = byte_swap_32 (digest_buf[7]);
6400
6401 memcpy (tmp_buf, digest_buf, 32);
6402
6403 base64_encode (int_to_base64, (const u8 *) tmp_buf, 32, (u8 *) ptr_plain);
6404
6405 ptr_plain[43] = 0;
6406
6407 snprintf (out_buf, len-1, "%s%s*%s", SIGNATURE_EPISERVER4, ptr_salt, ptr_plain);
6408 }
6409 else if (hash_mode == 1500)
6410 {
6411 out_buf[0] = salt.salt_sign[0] & 0xff;
6412 out_buf[1] = salt.salt_sign[1] & 0xff;
6413 //original method, but changed because of this ticket: https://hashcat.net/trac/ticket/269
6414 //out_buf[0] = int_to_itoa64 ((salt.salt_buf[0] >> 0) & 0x3f);
6415 //out_buf[1] = int_to_itoa64 ((salt.salt_buf[0] >> 6) & 0x3f);
6416
6417 memset (tmp_buf, 0, sizeof (tmp_buf));
6418
6419 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6420
6421 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6422 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6423
6424 memcpy (tmp_buf, digest_buf, 8);
6425
6426 base64_encode (int_to_itoa64, (const u8 *) tmp_buf, 8, (u8 *) ptr_plain);
6427
6428 snprintf (out_buf + 2, len-1-2, "%s", ptr_plain);
6429
6430 out_buf[13] = 0;
6431 }
6432 else if (hash_mode == 1600)
6433 {
6434 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6435
6436 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6437 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6438 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6439 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6440
6441 md5crypt_encode ((unsigned char *) digest_buf, (unsigned char *) ptr_plain);
6442
6443 if (salt.salt_iter == ROUNDS_MD5CRYPT)
6444 {
6445 snprintf (out_buf, len-1, "$apr1$%s$%s", (char *) salt.salt_buf, (char *) ptr_plain);
6446 }
6447 else
6448 {
6449 snprintf (out_buf, len-1, "$apr1$rounds=%i$%s$%s", salt.salt_iter, (char *) salt.salt_buf, (char *) ptr_plain);
6450 }
6451 }
6452 else if (hash_mode == 1711)
6453 {
6454 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6455
6456 digest_buf64[0] = byte_swap_64 (digest_buf64[0]);
6457 digest_buf64[1] = byte_swap_64 (digest_buf64[1]);
6458 digest_buf64[2] = byte_swap_64 (digest_buf64[2]);
6459 digest_buf64[3] = byte_swap_64 (digest_buf64[3]);
6460 digest_buf64[4] = byte_swap_64 (digest_buf64[4]);
6461 digest_buf64[5] = byte_swap_64 (digest_buf64[5]);
6462 digest_buf64[6] = byte_swap_64 (digest_buf64[6]);
6463 digest_buf64[7] = byte_swap_64 (digest_buf64[7]);
6464
6465 memcpy (tmp_buf, digest_buf, 64);
6466 memcpy (tmp_buf + 64, salt.salt_buf, salt.salt_len);
6467
6468 base64_encode (int_to_base64, (const u8 *) tmp_buf, 64 + salt.salt_len, (u8 *) ptr_plain);
6469
6470 snprintf (out_buf, len-1, "%s%s", SIGNATURE_SHA512B64S, ptr_plain);
6471 }
6472 else if (hash_mode == 1722)
6473 {
6474 uint *ptr = digest_buf;
6475
6476 snprintf (out_buf, len-1, "%s%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
6477 (unsigned char *) salt.salt_buf,
6478 ptr[ 1], ptr[ 0],
6479 ptr[ 3], ptr[ 2],
6480 ptr[ 5], ptr[ 4],
6481 ptr[ 7], ptr[ 6],
6482 ptr[ 9], ptr[ 8],
6483 ptr[11], ptr[10],
6484 ptr[13], ptr[12],
6485 ptr[15], ptr[14]);
6486 }
6487 else if (hash_mode == 1731)
6488 {
6489 uint *ptr = digest_buf;
6490
6491 snprintf (out_buf, len-1, "0x0200%s%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
6492 (unsigned char *) salt.salt_buf,
6493 ptr[ 1], ptr[ 0],
6494 ptr[ 3], ptr[ 2],
6495 ptr[ 5], ptr[ 4],
6496 ptr[ 7], ptr[ 6],
6497 ptr[ 9], ptr[ 8],
6498 ptr[11], ptr[10],
6499 ptr[13], ptr[12],
6500 ptr[15], ptr[14]);
6501 }
6502 else if (hash_mode == 1800)
6503 {
6504 // temp workaround
6505
6506 digest_buf64[0] = byte_swap_64 (digest_buf64[0]);
6507 digest_buf64[1] = byte_swap_64 (digest_buf64[1]);
6508 digest_buf64[2] = byte_swap_64 (digest_buf64[2]);
6509 digest_buf64[3] = byte_swap_64 (digest_buf64[3]);
6510 digest_buf64[4] = byte_swap_64 (digest_buf64[4]);
6511 digest_buf64[5] = byte_swap_64 (digest_buf64[5]);
6512 digest_buf64[6] = byte_swap_64 (digest_buf64[6]);
6513 digest_buf64[7] = byte_swap_64 (digest_buf64[7]);
6514
6515 sha512crypt_encode ((unsigned char *) digest_buf64, (unsigned char *) ptr_plain);
6516
6517 if (salt.salt_iter == ROUNDS_SHA512CRYPT)
6518 {
6519 snprintf (out_buf, len-1, "$6$%s$%s", (char *) salt.salt_buf, (char *) ptr_plain);
6520 }
6521 else
6522 {
6523 snprintf (out_buf, len-1, "$6$rounds=%i$%s$%s", salt.salt_iter, (char *) salt.salt_buf, (char *) ptr_plain);
6524 }
6525 }
6526 else if (hash_mode == 2100)
6527 {
6528 uint pos = 0;
6529
6530 snprintf (out_buf + pos, len-1, "%s%i#",
6531 SIGNATURE_DCC2,
6532 salt.salt_iter + 1);
6533
6534 uint signature_len = strlen (out_buf);
6535
6536 pos += signature_len;
6537 len -= signature_len;
6538
6539 char *salt_ptr = (char *) salt.salt_buf;
6540
6541 for (uint i = 0; i < salt.salt_len; i++, pos++, len--) snprintf (out_buf + pos, len-1, "%c", salt_ptr[i]);
6542
6543 snprintf (out_buf + pos, len-1, "#%08x%08x%08x%08x",
6544 byte_swap_32 (digest_buf[0]),
6545 byte_swap_32 (digest_buf[1]),
6546 byte_swap_32 (digest_buf[2]),
6547 byte_swap_32 (digest_buf[3]));
6548 }
6549 else if ((hash_mode == 2400) || (hash_mode == 2410))
6550 {
6551 memcpy (tmp_buf, digest_buf, 16);
6552
6553 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6554
6555 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6556 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6557 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6558 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6559
6560 out_buf[ 0] = int_to_itoa64 ((digest_buf[0] >> 0) & 0x3f);
6561 out_buf[ 1] = int_to_itoa64 ((digest_buf[0] >> 6) & 0x3f);
6562 out_buf[ 2] = int_to_itoa64 ((digest_buf[0] >> 12) & 0x3f);
6563 out_buf[ 3] = int_to_itoa64 ((digest_buf[0] >> 18) & 0x3f);
6564
6565 out_buf[ 4] = int_to_itoa64 ((digest_buf[1] >> 0) & 0x3f);
6566 out_buf[ 5] = int_to_itoa64 ((digest_buf[1] >> 6) & 0x3f);
6567 out_buf[ 6] = int_to_itoa64 ((digest_buf[1] >> 12) & 0x3f);
6568 out_buf[ 7] = int_to_itoa64 ((digest_buf[1] >> 18) & 0x3f);
6569
6570 out_buf[ 8] = int_to_itoa64 ((digest_buf[2] >> 0) & 0x3f);
6571 out_buf[ 9] = int_to_itoa64 ((digest_buf[2] >> 6) & 0x3f);
6572 out_buf[10] = int_to_itoa64 ((digest_buf[2] >> 12) & 0x3f);
6573 out_buf[11] = int_to_itoa64 ((digest_buf[2] >> 18) & 0x3f);
6574
6575 out_buf[12] = int_to_itoa64 ((digest_buf[3] >> 0) & 0x3f);
6576 out_buf[13] = int_to_itoa64 ((digest_buf[3] >> 6) & 0x3f);
6577 out_buf[14] = int_to_itoa64 ((digest_buf[3] >> 12) & 0x3f);
6578 out_buf[15] = int_to_itoa64 ((digest_buf[3] >> 18) & 0x3f);
6579
6580 out_buf[16] = 0;
6581 }
6582 else if (hash_mode == 2500)
6583 {
6584 wpa_t *wpas = (wpa_t *) data.esalts_buf;
6585
6586 wpa_t *wpa = &wpas[salt_pos];
6587
6588 snprintf (out_buf, len-1, "%s:%02x%02x%02x%02x%02x%02x:%02x%02x%02x%02x%02x%02x",
6589 (char *) salt.salt_buf,
6590 wpa->orig_mac1[0],
6591 wpa->orig_mac1[1],
6592 wpa->orig_mac1[2],
6593 wpa->orig_mac1[3],
6594 wpa->orig_mac1[4],
6595 wpa->orig_mac1[5],
6596 wpa->orig_mac2[0],
6597 wpa->orig_mac2[1],
6598 wpa->orig_mac2[2],
6599 wpa->orig_mac2[3],
6600 wpa->orig_mac2[4],
6601 wpa->orig_mac2[5]);
6602 }
6603 else if (hash_mode == 4400)
6604 {
6605 snprintf (out_buf, len-1, "%08x%08x%08x%08x",
6606 byte_swap_32 (digest_buf[0]),
6607 byte_swap_32 (digest_buf[1]),
6608 byte_swap_32 (digest_buf[2]),
6609 byte_swap_32 (digest_buf[3]));
6610 }
6611 else if (hash_mode == 4700)
6612 {
6613 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x",
6614 byte_swap_32 (digest_buf[0]),
6615 byte_swap_32 (digest_buf[1]),
6616 byte_swap_32 (digest_buf[2]),
6617 byte_swap_32 (digest_buf[3]),
6618 byte_swap_32 (digest_buf[4]));
6619 }
6620 else if (hash_mode == 4800)
6621 {
6622 u8 chap_id_byte = (u8) salt.salt_buf[4];
6623
6624 snprintf (out_buf, len-1, "%08x%08x%08x%08x:%08x%08x%08x%08x:%02x",
6625 digest_buf[0],
6626 digest_buf[1],
6627 digest_buf[2],
6628 digest_buf[3],
6629 byte_swap_32 (salt.salt_buf[0]),
6630 byte_swap_32 (salt.salt_buf[1]),
6631 byte_swap_32 (salt.salt_buf[2]),
6632 byte_swap_32 (salt.salt_buf[3]),
6633 chap_id_byte);
6634 }
6635 else if (hash_mode == 4900)
6636 {
6637 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x",
6638 byte_swap_32 (digest_buf[0]),
6639 byte_swap_32 (digest_buf[1]),
6640 byte_swap_32 (digest_buf[2]),
6641 byte_swap_32 (digest_buf[3]),
6642 byte_swap_32 (digest_buf[4]));
6643 }
6644 else if (hash_mode == 5100)
6645 {
6646 snprintf (out_buf, len-1, "%08x%08x",
6647 digest_buf[0],
6648 digest_buf[1]);
6649 }
6650 else if (hash_mode == 5200)
6651 {
6652 snprintf (out_buf, len-1, "%s", hashfile);
6653 }
6654 else if (hash_mode == 5300)
6655 {
6656 ikepsk_t *ikepsks = (ikepsk_t *) data.esalts_buf;
6657
6658 ikepsk_t *ikepsk = &ikepsks[salt_pos];
6659
6660 int buf_len = len -1;
6661
6662 // msg_buf
6663
6664 uint ikepsk_msg_len = ikepsk->msg_len / 4;
6665
6666 for (uint i = 0; i < ikepsk_msg_len; i++)
6667 {
6668 if ((i == 32) || (i == 64) || (i == 66) || (i == 68) || (i == 108))
6669 {
6670 snprintf (out_buf, buf_len, ":");
6671
6672 buf_len--;
6673 out_buf++;
6674 }
6675
6676 snprintf (out_buf, buf_len, "%08x", byte_swap_32 (ikepsk->msg_buf[i]));
6677
6678 buf_len -= 8;
6679 out_buf += 8;
6680 }
6681
6682 // nr_buf
6683
6684 uint ikepsk_nr_len = ikepsk->nr_len / 4;
6685
6686 for (uint i = 0; i < ikepsk_nr_len; i++)
6687 {
6688 if ((i == 0) || (i == 5))
6689 {
6690 snprintf (out_buf, buf_len, ":");
6691
6692 buf_len--;
6693 out_buf++;
6694 }
6695
6696 snprintf (out_buf, buf_len, "%08x", byte_swap_32 (ikepsk->nr_buf[i]));
6697
6698 buf_len -= 8;
6699 out_buf += 8;
6700 }
6701
6702 // digest_buf
6703
6704 for (uint i = 0; i < 4; i++)
6705 {
6706 if (i == 0)
6707 {
6708 snprintf (out_buf, buf_len, ":");
6709
6710 buf_len--;
6711 out_buf++;
6712 }
6713
6714 snprintf (out_buf, buf_len, "%08x", digest_buf[i]);
6715
6716 buf_len -= 8;
6717 out_buf += 8;
6718 }
6719 }
6720 else if (hash_mode == 5400)
6721 {
6722 ikepsk_t *ikepsks = (ikepsk_t *) data.esalts_buf;
6723
6724 ikepsk_t *ikepsk = &ikepsks[salt_pos];
6725
6726 int buf_len = len -1;
6727
6728 // msg_buf
6729
6730 uint ikepsk_msg_len = ikepsk->msg_len / 4;
6731
6732 for (uint i = 0; i < ikepsk_msg_len; i++)
6733 {
6734 if ((i == 32) || (i == 64) || (i == 66) || (i == 68) || (i == 108))
6735 {
6736 snprintf (out_buf, buf_len, ":");
6737
6738 buf_len--;
6739 out_buf++;
6740 }
6741
6742 snprintf (out_buf, buf_len, "%08x", byte_swap_32 (ikepsk->msg_buf[i]));
6743
6744 buf_len -= 8;
6745 out_buf += 8;
6746 }
6747
6748 // nr_buf
6749
6750 uint ikepsk_nr_len = ikepsk->nr_len / 4;
6751
6752 for (uint i = 0; i < ikepsk_nr_len; i++)
6753 {
6754 if ((i == 0) || (i == 5))
6755 {
6756 snprintf (out_buf, buf_len, ":");
6757
6758 buf_len--;
6759 out_buf++;
6760 }
6761
6762 snprintf (out_buf, buf_len, "%08x", byte_swap_32 (ikepsk->nr_buf[i]));
6763
6764 buf_len -= 8;
6765 out_buf += 8;
6766 }
6767
6768 // digest_buf
6769
6770 for (uint i = 0; i < 5; i++)
6771 {
6772 if (i == 0)
6773 {
6774 snprintf (out_buf, buf_len, ":");
6775
6776 buf_len--;
6777 out_buf++;
6778 }
6779
6780 snprintf (out_buf, buf_len, "%08x", digest_buf[i]);
6781
6782 buf_len -= 8;
6783 out_buf += 8;
6784 }
6785 }
6786 else if (hash_mode == 5500)
6787 {
6788 netntlm_t *netntlms = (netntlm_t *) data.esalts_buf;
6789
6790 netntlm_t *netntlm = &netntlms[salt_pos];
6791
6792 char user_buf[64] = { 0 };
6793 char domain_buf[64] = { 0 };
6794 char srvchall_buf[1024] = { 0 };
6795 char clichall_buf[1024] = { 0 };
6796
6797 for (uint i = 0, j = 0; j < netntlm->user_len; i += 1, j += 2)
6798 {
6799 char *ptr = (char *) netntlm->userdomain_buf;
6800
6801 user_buf[i] = ptr[j];
6802 }
6803
6804 for (uint i = 0, j = 0; j < netntlm->domain_len; i += 1, j += 2)
6805 {
6806 char *ptr = (char *) netntlm->userdomain_buf;
6807
6808 domain_buf[i] = ptr[netntlm->user_len + j];
6809 }
6810
6811 for (uint i = 0, j = 0; i < netntlm->srvchall_len; i += 1, j += 2)
6812 {
6813 u8 *ptr = (u8 *) netntlm->chall_buf;
6814
6815 sprintf (srvchall_buf + j, "%02x", ptr[i]);
6816 }
6817
6818 for (uint i = 0, j = 0; i < netntlm->clichall_len; i += 1, j += 2)
6819 {
6820 u8 *ptr = (u8 *) netntlm->chall_buf;
6821
6822 sprintf (clichall_buf + j, "%02x", ptr[netntlm->srvchall_len + i]);
6823 }
6824
6825 snprintf (out_buf, len-1, "%s::%s:%s:%08x%08x%08x%08x%08x%08x:%s",
6826 user_buf,
6827 domain_buf,
6828 srvchall_buf,
6829 digest_buf[0],
6830 digest_buf[1],
6831 digest_buf[2],
6832 digest_buf[3],
6833 byte_swap_32 (salt.salt_buf_pc[0]),
6834 byte_swap_32 (salt.salt_buf_pc[1]),
6835 clichall_buf);
6836 }
6837 else if (hash_mode == 5600)
6838 {
6839 netntlm_t *netntlms = (netntlm_t *) data.esalts_buf;
6840
6841 netntlm_t *netntlm = &netntlms[salt_pos];
6842
6843 char user_buf[64] = { 0 };
6844 char domain_buf[64] = { 0 };
6845 char srvchall_buf[1024] = { 0 };
6846 char clichall_buf[1024] = { 0 };
6847
6848 for (uint i = 0, j = 0; j < netntlm->user_len; i += 1, j += 2)
6849 {
6850 char *ptr = (char *) netntlm->userdomain_buf;
6851
6852 user_buf[i] = ptr[j];
6853 }
6854
6855 for (uint i = 0, j = 0; j < netntlm->domain_len; i += 1, j += 2)
6856 {
6857 char *ptr = (char *) netntlm->userdomain_buf;
6858
6859 domain_buf[i] = ptr[netntlm->user_len + j];
6860 }
6861
6862 for (uint i = 0, j = 0; i < netntlm->srvchall_len; i += 1, j += 2)
6863 {
6864 u8 *ptr = (u8 *) netntlm->chall_buf;
6865
6866 sprintf (srvchall_buf + j, "%02x", ptr[i]);
6867 }
6868
6869 for (uint i = 0, j = 0; i < netntlm->clichall_len; i += 1, j += 2)
6870 {
6871 u8 *ptr = (u8 *) netntlm->chall_buf;
6872
6873 sprintf (clichall_buf + j, "%02x", ptr[netntlm->srvchall_len + i]);
6874 }
6875
6876 snprintf (out_buf, len-1, "%s::%s:%s:%08x%08x%08x%08x:%s",
6877 user_buf,
6878 domain_buf,
6879 srvchall_buf,
6880 digest_buf[0],
6881 digest_buf[1],
6882 digest_buf[2],
6883 digest_buf[3],
6884 clichall_buf);
6885 }
6886 else if (hash_mode == 5700)
6887 {
6888 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6889
6890 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6891 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6892 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6893 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6894 digest_buf[4] = byte_swap_32 (digest_buf[4]);
6895 digest_buf[5] = byte_swap_32 (digest_buf[5]);
6896 digest_buf[6] = byte_swap_32 (digest_buf[6]);
6897 digest_buf[7] = byte_swap_32 (digest_buf[7]);
6898
6899 memcpy (tmp_buf, digest_buf, 32);
6900
6901 base64_encode (int_to_itoa64, (const u8 *) tmp_buf, 32, (u8 *) ptr_plain);
6902
6903 ptr_plain[43] = 0;
6904
6905 snprintf (out_buf, len-1, "%s", ptr_plain);
6906 }
6907 else if (hash_mode == 5800)
6908 {
6909 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6910 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6911 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6912 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6913 digest_buf[4] = byte_swap_32 (digest_buf[4]);
6914
6915 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x",
6916 digest_buf[0],
6917 digest_buf[1],
6918 digest_buf[2],
6919 digest_buf[3],
6920 digest_buf[4]);
6921 }
6922 else if ((hash_mode >= 6200) && (hash_mode <= 6299))
6923 {
6924 snprintf (out_buf, len-1, "%s", hashfile);
6925 }
6926 else if (hash_mode == 6300)
6927 {
6928 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6929
6930 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6931 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6932 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6933 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6934
6935 md5crypt_encode ((unsigned char *) digest_buf, (unsigned char *) ptr_plain);
6936
6937 snprintf (out_buf, len-1, "{smd5}%s$%s", (char *) salt.salt_buf, (char *) ptr_plain);
6938 }
6939 else if (hash_mode == 6400)
6940 {
6941 sha256aix_encode ((unsigned char *) digest_buf, (unsigned char *) ptr_plain);
6942
6943 snprintf (out_buf, len-1, "{ssha256}%02d$%s$%s", salt.salt_sign[0], (char *) salt.salt_buf, (char *) ptr_plain);
6944 }
6945 else if (hash_mode == 6500)
6946 {
6947 sha512aix_encode ((unsigned char *) digest_buf64, (unsigned char *) ptr_plain);
6948
6949 snprintf (out_buf, len-1, "{ssha512}%02d$%s$%s", salt.salt_sign[0], (char *) salt.salt_buf, (char *) ptr_plain);
6950 }
6951 else if (hash_mode == 6600)
6952 {
6953 agilekey_t *agilekeys = (agilekey_t *) data.esalts_buf;
6954
6955 agilekey_t *agilekey = &agilekeys[salt_pos];
6956
6957 salt.salt_buf[0] = byte_swap_32 (salt.salt_buf[0]);
6958 salt.salt_buf[1] = byte_swap_32 (salt.salt_buf[1]);
6959
6960 uint buf_len = len - 1;
6961
6962 uint off = snprintf (out_buf, buf_len, "%d:%08x%08x:", salt.salt_iter + 1, salt.salt_buf[0], salt.salt_buf[1]);
6963 buf_len -= 22;
6964
6965 for (uint i = 0, j = off; i < 1040; i++, j += 2)
6966 {
6967 snprintf (out_buf + j, buf_len, "%02x", agilekey->cipher[i]);
6968
6969 buf_len -= 2;
6970 }
6971 }
6972 else if (hash_mode == 6700)
6973 {
6974 sha1aix_encode ((unsigned char *) digest_buf, (unsigned char *) ptr_plain);
6975
6976 snprintf (out_buf, len-1, "{ssha1}%02d$%s$%s", salt.salt_sign[0], (char *) salt.salt_buf, (char *) ptr_plain);
6977 }
6978 else if (hash_mode == 6800)
6979 {
6980 snprintf (out_buf, len-1, "%s", (char *) salt.salt_buf);
6981 }
6982 else if (hash_mode == 7100)
6983 {
6984 uint *ptr = digest_buf;
6985
6986 pbkdf2_sha512_t *pbkdf2_sha512s = (pbkdf2_sha512_t *) data.esalts_buf;
6987
6988 pbkdf2_sha512_t *pbkdf2_sha512 = &pbkdf2_sha512s[salt_pos];
6989
6990 uint esalt[8] = { 0 };
6991
6992 esalt[0] = byte_swap_32 (pbkdf2_sha512->salt_buf[0]);
6993 esalt[1] = byte_swap_32 (pbkdf2_sha512->salt_buf[1]);
6994 esalt[2] = byte_swap_32 (pbkdf2_sha512->salt_buf[2]);
6995 esalt[3] = byte_swap_32 (pbkdf2_sha512->salt_buf[3]);
6996 esalt[4] = byte_swap_32 (pbkdf2_sha512->salt_buf[4]);
6997 esalt[5] = byte_swap_32 (pbkdf2_sha512->salt_buf[5]);
6998 esalt[6] = byte_swap_32 (pbkdf2_sha512->salt_buf[6]);
6999 esalt[7] = byte_swap_32 (pbkdf2_sha512->salt_buf[7]);
7000
7001 snprintf (out_buf, len-1, "%s%i$%08x%08x%08x%08x%08x%08x%08x%08x$%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
7002 SIGNATURE_SHA512OSX,
7003 salt.salt_iter + 1,
7004 esalt[ 0], esalt[ 1],
7005 esalt[ 2], esalt[ 3],
7006 esalt[ 4], esalt[ 5],
7007 esalt[ 6], esalt[ 7],
7008 ptr [ 1], ptr [ 0],
7009 ptr [ 3], ptr [ 2],
7010 ptr [ 5], ptr [ 4],
7011 ptr [ 7], ptr [ 6],
7012 ptr [ 9], ptr [ 8],
7013 ptr [11], ptr [10],
7014 ptr [13], ptr [12],
7015 ptr [15], ptr [14]);
7016 }
7017 else if (hash_mode == 7200)
7018 {
7019 uint *ptr = digest_buf;
7020
7021 pbkdf2_sha512_t *pbkdf2_sha512s = (pbkdf2_sha512_t *) data.esalts_buf;
7022
7023 pbkdf2_sha512_t *pbkdf2_sha512 = &pbkdf2_sha512s[salt_pos];
7024
7025 uint len_used = 0;
7026
7027 snprintf (out_buf + len_used, len - len_used - 1, "%s%i.", SIGNATURE_SHA512GRUB, salt.salt_iter + 1);
7028
7029 len_used = strlen (out_buf);
7030
7031 unsigned char *salt_buf_ptr = (unsigned char *) pbkdf2_sha512->salt_buf;
7032
7033 for (uint i = 0; i < salt.salt_len; i++, len_used += 2)
7034 {
7035 snprintf (out_buf + len_used, len - len_used - 1, "%02x", salt_buf_ptr[i]);
7036 }
7037
7038 snprintf (out_buf + len_used, len - len_used - 1, ".%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
7039 ptr [ 1], ptr [ 0],
7040 ptr [ 3], ptr [ 2],
7041 ptr [ 5], ptr [ 4],
7042 ptr [ 7], ptr [ 6],
7043 ptr [ 9], ptr [ 8],
7044 ptr [11], ptr [10],
7045 ptr [13], ptr [12],
7046 ptr [15], ptr [14]);
7047 }
7048 else if (hash_mode == 7300)
7049 {
7050 rakp_t *rakps = (rakp_t *) data.esalts_buf;
7051
7052 rakp_t *rakp = &rakps[salt_pos];
7053
7054 for (uint i = 0, j = 0; (i * 4) < rakp->salt_len; i += 1, j += 8)
7055 {
7056 sprintf (out_buf + j, "%08x", rakp->salt_buf[i]);
7057 }
7058
7059 snprintf (out_buf + rakp->salt_len * 2, len - 1, ":%08x%08x%08x%08x%08x",
7060 digest_buf[0],
7061 digest_buf[1],
7062 digest_buf[2],
7063 digest_buf[3],
7064 digest_buf[4]);
7065 }
7066 else if (hash_mode == 7400)
7067 {
7068 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
7069
7070 digest_buf[0] = byte_swap_32 (digest_buf[0]);
7071 digest_buf[1] = byte_swap_32 (digest_buf[1]);
7072 digest_buf[2] = byte_swap_32 (digest_buf[2]);
7073 digest_buf[3] = byte_swap_32 (digest_buf[3]);
7074 digest_buf[4] = byte_swap_32 (digest_buf[4]);
7075 digest_buf[5] = byte_swap_32 (digest_buf[5]);
7076 digest_buf[6] = byte_swap_32 (digest_buf[6]);
7077 digest_buf[7] = byte_swap_32 (digest_buf[7]);
7078
7079 sha256crypt_encode ((unsigned char *) digest_buf, (unsigned char *) ptr_plain);
7080
7081 if (salt.salt_iter == ROUNDS_SHA256CRYPT)
7082 {
7083 snprintf (out_buf, len-1, "$5$%s$%s", (char *) salt.salt_buf, (char *) ptr_plain);
7084 }
7085 else
7086 {
7087 snprintf (out_buf, len-1, "$5$rounds=%i$%s$%s", salt.salt_iter, (char *) salt.salt_buf, (char *) ptr_plain);
7088 }
7089 }
7090 else if (hash_mode == 7500)
7091 {
7092 krb5pa_t *krb5pas = (krb5pa_t *) data.esalts_buf;
7093
7094 krb5pa_t *krb5pa = &krb5pas[salt_pos];
7095
7096 u8 *ptr_timestamp = (u8 *) krb5pa->timestamp;
7097 u8 *ptr_checksum = (u8 *) krb5pa->checksum;
7098
7099 char data[128] = { 0 };
7100
7101 char *ptr_data = data;
7102
7103 for (uint i = 0; i < 36; i++, ptr_data += 2)
7104 {
7105 sprintf (ptr_data, "%02x", ptr_timestamp[i]);
7106 }
7107
7108 for (uint i = 0; i < 16; i++, ptr_data += 2)
7109 {
7110 sprintf (ptr_data, "%02x", ptr_checksum[i]);
7111 }
7112
7113 *ptr_data = 0;
7114
7115 snprintf (out_buf, len-1, "%s$%s$%s$%s$%s",
7116 SIGNATURE_KRB5PA,
7117 (char *) krb5pa->user,
7118 (char *) krb5pa->realm,
7119 (char *) krb5pa->salt,
7120 data);
7121 }
7122 else if (hash_mode == 7700)
7123 {
7124 snprintf (out_buf, len-1, "%s$%08X%08X",
7125 (char *) salt.salt_buf,
7126 digest_buf[0],
7127 digest_buf[1]);
7128 }
7129 else if (hash_mode == 7800)
7130 {
7131 snprintf (out_buf, len-1, "%s$%08X%08X%08X%08X%08X",
7132 (char *) salt.salt_buf,
7133 digest_buf[0],
7134 digest_buf[1],
7135 digest_buf[2],
7136 digest_buf[3],
7137 digest_buf[4]);
7138 }
7139 else if (hash_mode == 7900)
7140 {
7141 drupal7_encode ((unsigned char *) digest_buf64, (unsigned char *) ptr_plain);
7142
7143 // ugly hack start
7144
7145 char *tmp = (char *) salt.salt_buf_pc;
7146
7147 ptr_plain[42] = tmp[0];
7148
7149 // ugly hack end
7150
7151 ptr_plain[43] = 0;
7152
7153 snprintf (out_buf, len-1, "%s%s%s", (char *) salt.salt_sign, (char *) salt.salt_buf, (char *) ptr_plain);
7154 }
7155 else if (hash_mode == 8000)
7156 {
7157 snprintf (out_buf, len-1, "0xc007%s%08x%08x%08x%08x%08x%08x%08x%08x",
7158 (unsigned char *) salt.salt_buf,
7159 digest_buf[0],
7160 digest_buf[1],
7161 digest_buf[2],
7162 digest_buf[3],
7163 digest_buf[4],
7164 digest_buf[5],
7165 digest_buf[6],
7166 digest_buf[7]);
7167 }
7168 else if (hash_mode == 8100)
7169 {
7170 salt.salt_buf[0] = byte_swap_32 (salt.salt_buf[0]);
7171 salt.salt_buf[1] = byte_swap_32 (salt.salt_buf[1]);
7172
7173 snprintf (out_buf, len-1, "1%s%08x%08x%08x%08x%08x",
7174 (unsigned char *) salt.salt_buf,
7175 digest_buf[0],
7176 digest_buf[1],
7177 digest_buf[2],
7178 digest_buf[3],
7179 digest_buf[4]);
7180 }
7181 else if (hash_mode == 8200)
7182 {
7183 cloudkey_t *cloudkeys = (cloudkey_t *) data.esalts_buf;
7184
7185 cloudkey_t *cloudkey = &cloudkeys[salt_pos];
7186
7187 char data_buf[4096] = { 0 };
7188
7189 for (int i = 0, j = 0; i < 512; i += 1, j += 8)
7190 {
7191 sprintf (data_buf + j, "%08x", cloudkey->data_buf[i]);
7192 }
7193
7194 data_buf[cloudkey->data_len * 2] = 0;
7195
7196 digest_buf[0] = byte_swap_32 (digest_buf[0]);
7197 digest_buf[1] = byte_swap_32 (digest_buf[1]);
7198 digest_buf[2] = byte_swap_32 (digest_buf[2]);
7199 digest_buf[3] = byte_swap_32 (digest_buf[3]);
7200 digest_buf[4] = byte_swap_32 (digest_buf[4]);
7201 digest_buf[5] = byte_swap_32 (digest_buf[5]);
7202 digest_buf[6] = byte_swap_32 (digest_buf[6]);
7203 digest_buf[7] = byte_swap_32 (digest_buf[7]);
7204
7205 salt.salt_buf[0] = byte_swap_32 (salt.salt_buf[0]);
7206 salt.salt_buf[1] = byte_swap_32 (salt.salt_buf[1]);
7207 salt.salt_buf[2] = byte_swap_32 (salt.salt_buf[2]);
7208 salt.salt_buf[3] = byte_swap_32 (salt.salt_buf[3]);
7209
7210 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x%08x%08x%08x:%08x%08x%08x%08x:%u:%s",
7211 digest_buf[0],
7212 digest_buf[1],
7213 digest_buf[2],
7214 digest_buf[3],
7215 digest_buf[4],
7216 digest_buf[5],
7217 digest_buf[6],
7218 digest_buf[7],
7219 salt.salt_buf[0],
7220 salt.salt_buf[1],
7221 salt.salt_buf[2],
7222 salt.salt_buf[3],
7223 salt.salt_iter + 1,
7224 data_buf);
7225 }
7226 else if (hash_mode == 8300)
7227 {
7228 char digest_buf_c[34] = { 0 };
7229
7230 digest_buf[0] = byte_swap_32 (digest_buf[0]);
7231 digest_buf[1] = byte_swap_32 (digest_buf[1]);
7232 digest_buf[2] = byte_swap_32 (digest_buf[2]);
7233 digest_buf[3] = byte_swap_32 (digest_buf[3]);
7234 digest_buf[4] = byte_swap_32 (digest_buf[4]);
7235
7236 base32_encode (int_to_itoa32, (const u8 *) digest_buf, 20, (u8 *) digest_buf_c);
7237
7238 digest_buf_c[32] = 0;
7239
7240 // domain
7241
7242 const uint salt_pc_len = salt.salt_buf_pc[7]; // what a hack
7243
7244 char domain_buf_c[33] = { 0 };
7245
7246 memcpy (domain_buf_c, (char *) salt.salt_buf_pc, salt_pc_len);
7247
7248 for (uint i = 0; i < salt_pc_len; i++)
7249 {
7250 const char next = domain_buf_c[i];
7251
7252 domain_buf_c[i] = '.';
7253
7254 i += next;
7255 }
7256
7257 domain_buf_c[salt_pc_len] = 0;
7258
7259 // final
7260
7261 snprintf (out_buf, len-1, "%s:%s:%s:%u", digest_buf_c, domain_buf_c, (char *) salt.salt_buf, salt.salt_iter);
7262 }
7263 else if (hash_mode == 8500)
7264 {
7265 snprintf (out_buf, len-1, "%s*%s*%08X%08X", SIGNATURE_RACF, (char *) salt.salt_buf, digest_buf[0], digest_buf[1]);
7266 }
7267 else if (hash_mode == 2612)
7268 {
7269 snprintf (out_buf, len-1, "%s%s$%08x%08x%08x%08x",
7270 SIGNATURE_PHPS,
7271 (char *) salt.salt_buf,
7272 digest_buf[0],
7273 digest_buf[1],
7274 digest_buf[2],
7275 digest_buf[3]);
7276 }
7277 else if (hash_mode == 3711)
7278 {
7279 char *salt_ptr = (char *) salt.salt_buf;
7280
7281 salt_ptr[salt.salt_len - 1] = 0;
7282
7283 snprintf (out_buf, len-1, "%s%s$%08x%08x%08x%08x",
7284 SIGNATURE_MEDIAWIKI_B,
7285 salt_ptr,
7286 digest_buf[0],
7287 digest_buf[1],
7288 digest_buf[2],
7289 digest_buf[3]);
7290 }
7291 else if (hash_mode == 8800)
7292 {
7293 androidfde_t *androidfdes = (androidfde_t *) data.esalts_buf;
7294
7295 androidfde_t *androidfde = &androidfdes[salt_pos];
7296
7297 char tmp[3073] = { 0 };
7298
7299 for (uint i = 0, j = 0; i < 384; i += 1, j += 8)
7300 {
7301 sprintf (tmp + j, "%08x", androidfde->data[i]);
7302 }
7303
7304 tmp[3072] = 0;
7305
7306 snprintf (out_buf, len-1, "%s16$%08x%08x%08x%08x$16$%08x%08x%08x%08x$%s",
7307 SIGNATURE_ANDROIDFDE,
7308 byte_swap_32 (salt.salt_buf[0]),
7309 byte_swap_32 (salt.salt_buf[1]),
7310 byte_swap_32 (salt.salt_buf[2]),
7311 byte_swap_32 (salt.salt_buf[3]),
7312 byte_swap_32 (digest_buf[0]),
7313 byte_swap_32 (digest_buf[1]),
7314 byte_swap_32 (digest_buf[2]),
7315 byte_swap_32 (digest_buf[3]),
7316 tmp);
7317 }
7318 else if (hash_mode == 8900)
7319 {
7320 uint N = salt.scrypt_N;
7321 uint r = salt.scrypt_r;
7322 uint p = salt.scrypt_p;
7323
7324 char base64_salt[32] = { 0 };
7325
7326 base64_encode (int_to_base64, (const u8 *) salt.salt_buf, salt.salt_len, (u8 *) base64_salt);
7327
7328 memset (tmp_buf, 0, 46);
7329
7330 digest_buf[0] = byte_swap_32 (digest_buf[0]);
7331 digest_buf[1] = byte_swap_32 (digest_buf[1]);
7332 digest_buf[2] = byte_swap_32 (digest_buf[2]);
7333 digest_buf[3] = byte_swap_32 (digest_buf[3]);
7334 digest_buf[4] = byte_swap_32 (digest_buf[4]);
7335 digest_buf[5] = byte_swap_32 (digest_buf[5]);
7336 digest_buf[6] = byte_swap_32 (digest_buf[6]);
7337 digest_buf[7] = byte_swap_32 (digest_buf[7]);
7338 digest_buf[8] = 0; // needed for base64_encode ()
7339
7340 base64_encode (int_to_base64, (const u8 *) digest_buf, 32, (u8 *) tmp_buf);
7341
7342 snprintf (out_buf, len-1, "%s:%i:%i:%i:%s:%s",
7343 SIGNATURE_SCRYPT,
7344 N,
7345 r,
7346 p,
7347 base64_salt,
7348 tmp_buf);
7349 }
7350 else if (hash_mode == 9000)
7351 {
7352 snprintf (out_buf, len-1, "%s", hashfile);
7353 }
7354 else if (hash_mode == 9200)
7355 {
7356 // salt
7357
7358 pbkdf2_sha256_t *pbkdf2_sha256s = (pbkdf2_sha256_t *) data.esalts_buf;
7359
7360 pbkdf2_sha256_t *pbkdf2_sha256 = &pbkdf2_sha256s[salt_pos];
7361
7362 unsigned char *salt_buf_ptr = (unsigned char *) pbkdf2_sha256->salt_buf;
7363
7364 // hash
7365
7366 digest_buf[0] = byte_swap_32 (digest_buf[0]);
7367 digest_buf[1] = byte_swap_32 (digest_buf[1]);
7368 digest_buf[2] = byte_swap_32 (digest_buf[2]);
7369 digest_buf[3] = byte_swap_32 (digest_buf[3]);
7370 digest_buf[4] = byte_swap_32 (digest_buf[4]);
7371 digest_buf[5] = byte_swap_32 (digest_buf[5]);
7372 digest_buf[6] = byte_swap_32 (digest_buf[6]);
7373 digest_buf[7] = byte_swap_32 (digest_buf[7]);
7374 digest_buf[8] = 0; // needed for base64_encode ()
7375
7376 char tmp_buf[64] = { 0 };
7377
7378 base64_encode (int_to_itoa64, (const u8 *) digest_buf, 32, (u8 *) tmp_buf);
7379 tmp_buf[43] = 0; // cut it here
7380
7381 // output
7382
7383 snprintf (out_buf, len-1, "%s%s$%s", SIGNATURE_CISCO8, salt_buf_ptr, tmp_buf);
7384 }
7385 else if (hash_mode == 9300)
7386 {
7387 digest_buf[0] = byte_swap_32 (digest_buf[0]);
7388 digest_buf[1] = byte_swap_32 (digest_buf[1]);
7389 digest_buf[2] = byte_swap_32 (digest_buf[2]);
7390 digest_buf[3] = byte_swap_32 (digest_buf[3]);
7391 digest_buf[4] = byte_swap_32 (digest_buf[4]);
7392 digest_buf[5] = byte_swap_32 (digest_buf[5]);
7393 digest_buf[6] = byte_swap_32 (digest_buf[6]);
7394 digest_buf[7] = byte_swap_32 (digest_buf[7]);
7395 digest_buf[8] = 0; // needed for base64_encode ()
7396
7397 char tmp_buf[64] = { 0 };
7398
7399 base64_encode (int_to_itoa64, (const u8 *) digest_buf, 32, (u8 *) tmp_buf);
7400 tmp_buf[43] = 0; // cut it here
7401
7402 unsigned char *salt_buf_ptr = (unsigned char *) salt.salt_buf;
7403
7404 snprintf (out_buf, len-1, "%s%s$%s", SIGNATURE_CISCO9, salt_buf_ptr, tmp_buf);
7405 }
7406 else if (hash_mode == 9400)
7407 {
7408 office2007_t *office2007s = (office2007_t *) data.esalts_buf;
7409
7410 office2007_t *office2007 = &office2007s[salt_pos];
7411
7412 snprintf (out_buf, len-1, "%s*%u*%u*%u*%u*%08x%08x%08x%08x*%08x%08x%08x%08x*%08x%08x%08x%08x%08x",
7413 SIGNATURE_OFFICE2007,
7414 2007,
7415 20,
7416 office2007->keySize,
7417 16,
7418 salt.salt_buf[0],
7419 salt.salt_buf[1],
7420 salt.salt_buf[2],
7421 salt.salt_buf[3],
7422 office2007->encryptedVerifier[0],
7423 office2007->encryptedVerifier[1],
7424 office2007->encryptedVerifier[2],
7425 office2007->encryptedVerifier[3],
7426 office2007->encryptedVerifierHash[0],
7427 office2007->encryptedVerifierHash[1],
7428 office2007->encryptedVerifierHash[2],
7429 office2007->encryptedVerifierHash[3],
7430 office2007->encryptedVerifierHash[4]);
7431 }
7432 else if (hash_mode == 9500)
7433 {
7434 office2010_t *office2010s = (office2010_t *) data.esalts_buf;
7435
7436 office2010_t *office2010 = &office2010s[salt_pos];
7437
7438 snprintf (out_buf, len-1, "%s*%u*%u*%u*%u*%08x%08x%08x%08x*%08x%08x%08x%08x*%08x%08x%08x%08x%08x%08x%08x%08x", SIGNATURE_OFFICE2010, 2010, 100000, 128, 16,
7439
7440 salt.salt_buf[0],
7441 salt.salt_buf[1],
7442 salt.salt_buf[2],
7443 salt.salt_buf[3],
7444 office2010->encryptedVerifier[0],
7445 office2010->encryptedVerifier[1],
7446 office2010->encryptedVerifier[2],
7447 office2010->encryptedVerifier[3],
7448 office2010->encryptedVerifierHash[0],
7449 office2010->encryptedVerifierHash[1],
7450 office2010->encryptedVerifierHash[2],
7451 office2010->encryptedVerifierHash[3],
7452 office2010->encryptedVerifierHash[4],
7453 office2010->encryptedVerifierHash[5],
7454 office2010->encryptedVerifierHash[6],
7455 office2010->encryptedVerifierHash[7]);
7456 }
7457 else if (hash_mode == 9600)
7458 {
7459 office2013_t *office2013s = (office2013_t *) data.esalts_buf;
7460
7461 office2013_t *office2013 = &office2013s[salt_pos];
7462
7463 snprintf (out_buf, len-1, "%s*%u*%u*%u*%u*%08x%08x%08x%08x*%08x%08x%08x%08x*%08x%08x%08x%08x%08x%08x%08x%08x", SIGNATURE_OFFICE2013, 2013, 100000, 256, 16,
7464
7465 salt.salt_buf[0],
7466 salt.salt_buf[1],
7467 salt.salt_buf[2],
7468 salt.salt_buf[3],
7469 office2013->encryptedVerifier[0],
7470 office2013->encryptedVerifier[1],
7471 office2013->encryptedVerifier[2],
7472 office2013->encryptedVerifier[3],
7473 office2013->encryptedVerifierHash[0],
7474 office2013->encryptedVerifierHash[1],
7475 office2013->encryptedVerifierHash[2],
7476 office2013->encryptedVerifierHash[3],
7477 office2013->encryptedVerifierHash[4],
7478 office2013->encryptedVerifierHash[5],
7479 office2013->encryptedVerifierHash[6],
7480 office2013->encryptedVerifierHash[7]);
7481 }
7482 else if (hash_mode == 9700)
7483 {
7484 oldoffice01_t *oldoffice01s = (oldoffice01_t *) data.esalts_buf;
7485
7486 oldoffice01_t *oldoffice01 = &oldoffice01s[salt_pos];
7487
7488 snprintf (out_buf, len-1, "%s*%08x%08x%08x%08x*%08x%08x%08x%08x*%08x%08x%08x%08x",
7489 (oldoffice01->version == 0) ? SIGNATURE_OLDOFFICE0 : SIGNATURE_OLDOFFICE1,
7490 byte_swap_32 (salt.salt_buf[0]),
7491 byte_swap_32 (salt.salt_buf[1]),
7492 byte_swap_32 (salt.salt_buf[2]),
7493 byte_swap_32 (salt.salt_buf[3]),
7494 byte_swap_32 (oldoffice01->encryptedVerifier[0]),
7495 byte_swap_32 (oldoffice01->encryptedVerifier[1]),
7496 byte_swap_32 (oldoffice01->encryptedVerifier[2]),
7497 byte_swap_32 (oldoffice01->encryptedVerifier[3]),
7498 byte_swap_32 (oldoffice01->encryptedVerifierHash[0]),
7499 byte_swap_32 (oldoffice01->encryptedVerifierHash[1]),
7500 byte_swap_32 (oldoffice01->encryptedVerifierHash[2]),
7501 byte_swap_32 (oldoffice01->encryptedVerifierHash[3]));
7502 }
7503 else if (hash_mode == 9710)
7504 {
7505 oldoffice01_t *oldoffice01s = (oldoffice01_t *) data.esalts_buf;
7506
7507 oldoffice01_t *oldoffice01 = &oldoffice01s[salt_pos];
7508
7509 snprintf (out_buf, len-1, "%s*%08x%08x%08x%08x*%08x%08x%08x%08x*%08x%08x%08x%08x",
7510 (oldoffice01->version == 0) ? SIGNATURE_OLDOFFICE0 : SIGNATURE_OLDOFFICE1,
7511 byte_swap_32 (salt.salt_buf[0]),
7512 byte_swap_32 (salt.salt_buf[1]),
7513 byte_swap_32 (salt.salt_buf[2]),
7514 byte_swap_32 (salt.salt_buf[3]),
7515 byte_swap_32 (oldoffice01->encryptedVerifier[0]),
7516 byte_swap_32 (oldoffice01->encryptedVerifier[1]),
7517 byte_swap_32 (oldoffice01->encryptedVerifier[2]),
7518 byte_swap_32 (oldoffice01->encryptedVerifier[3]),
7519 byte_swap_32 (oldoffice01->encryptedVerifierHash[0]),
7520 byte_swap_32 (oldoffice01->encryptedVerifierHash[1]),
7521 byte_swap_32 (oldoffice01->encryptedVerifierHash[2]),
7522 byte_swap_32 (oldoffice01->encryptedVerifierHash[3]));
7523 }
7524 else if (hash_mode == 9720)
7525 {
7526 oldoffice01_t *oldoffice01s = (oldoffice01_t *) data.esalts_buf;
7527
7528 oldoffice01_t *oldoffice01 = &oldoffice01s[salt_pos];
7529
7530 u8 *rc4key = (u8 *) oldoffice01->rc4key;
7531
7532 snprintf (out_buf, len-1, "%s*%08x%08x%08x%08x*%08x%08x%08x%08x*%08x%08x%08x%08x:%02x%02x%02x%02x%02x",
7533 (oldoffice01->version == 0) ? SIGNATURE_OLDOFFICE0 : SIGNATURE_OLDOFFICE1,
7534 byte_swap_32 (salt.salt_buf[0]),
7535 byte_swap_32 (salt.salt_buf[1]),
7536 byte_swap_32 (salt.salt_buf[2]),
7537 byte_swap_32 (salt.salt_buf[3]),
7538 byte_swap_32 (oldoffice01->encryptedVerifier[0]),
7539 byte_swap_32 (oldoffice01->encryptedVerifier[1]),
7540 byte_swap_32 (oldoffice01->encryptedVerifier[2]),
7541 byte_swap_32 (oldoffice01->encryptedVerifier[3]),
7542 byte_swap_32 (oldoffice01->encryptedVerifierHash[0]),
7543 byte_swap_32 (oldoffice01->encryptedVerifierHash[1]),
7544 byte_swap_32 (oldoffice01->encryptedVerifierHash[2]),
7545 byte_swap_32 (oldoffice01->encryptedVerifierHash[3]),
7546 rc4key[0],
7547 rc4key[1],
7548 rc4key[2],
7549 rc4key[3],
7550 rc4key[4]);
7551 }
7552 else if (hash_mode == 9800)
7553 {
7554 oldoffice34_t *oldoffice34s = (oldoffice34_t *) data.esalts_buf;
7555
7556 oldoffice34_t *oldoffice34 = &oldoffice34s[salt_pos];
7557
7558 snprintf (out_buf, len-1, "%s*%08x%08x%08x%08x*%08x%08x%08x%08x*%08x%08x%08x%08x%08x",
7559 (oldoffice34->version == 3) ? SIGNATURE_OLDOFFICE3 : SIGNATURE_OLDOFFICE4,
7560 salt.salt_buf[0],
7561 salt.salt_buf[1],
7562 salt.salt_buf[2],
7563 salt.salt_buf[3],
7564 byte_swap_32 (oldoffice34->encryptedVerifier[0]),
7565 byte_swap_32 (oldoffice34->encryptedVerifier[1]),
7566 byte_swap_32 (oldoffice34->encryptedVerifier[2]),
7567 byte_swap_32 (oldoffice34->encryptedVerifier[3]),
7568 byte_swap_32 (oldoffice34->encryptedVerifierHash[0]),
7569 byte_swap_32 (oldoffice34->encryptedVerifierHash[1]),
7570 byte_swap_32 (oldoffice34->encryptedVerifierHash[2]),
7571 byte_swap_32 (oldoffice34->encryptedVerifierHash[3]),
7572 byte_swap_32 (oldoffice34->encryptedVerifierHash[4]));
7573 }
7574 else if (hash_mode == 9810)
7575 {
7576 oldoffice34_t *oldoffice34s = (oldoffice34_t *) data.esalts_buf;
7577
7578 oldoffice34_t *oldoffice34 = &oldoffice34s[salt_pos];
7579
7580 snprintf (out_buf, len-1, "%s*%08x%08x%08x%08x*%08x%08x%08x%08x*%08x%08x%08x%08x%08x",
7581 (oldoffice34->version == 3) ? SIGNATURE_OLDOFFICE3 : SIGNATURE_OLDOFFICE4,
7582 salt.salt_buf[0],
7583 salt.salt_buf[1],
7584 salt.salt_buf[2],
7585 salt.salt_buf[3],
7586 byte_swap_32 (oldoffice34->encryptedVerifier[0]),
7587 byte_swap_32 (oldoffice34->encryptedVerifier[1]),
7588 byte_swap_32 (oldoffice34->encryptedVerifier[2]),
7589 byte_swap_32 (oldoffice34->encryptedVerifier[3]),
7590 byte_swap_32 (oldoffice34->encryptedVerifierHash[0]),
7591 byte_swap_32 (oldoffice34->encryptedVerifierHash[1]),
7592 byte_swap_32 (oldoffice34->encryptedVerifierHash[2]),
7593 byte_swap_32 (oldoffice34->encryptedVerifierHash[3]),
7594 byte_swap_32 (oldoffice34->encryptedVerifierHash[4]));
7595 }
7596 else if (hash_mode == 9820)
7597 {
7598 oldoffice34_t *oldoffice34s = (oldoffice34_t *) data.esalts_buf;
7599
7600 oldoffice34_t *oldoffice34 = &oldoffice34s[salt_pos];
7601
7602 u8 *rc4key = (u8 *) oldoffice34->rc4key;
7603
7604 snprintf (out_buf, len-1, "%s*%08x%08x%08x%08x*%08x%08x%08x%08x*%08x%08x%08x%08x%08x:%02x%02x%02x%02x%02x",
7605 (oldoffice34->version == 3) ? SIGNATURE_OLDOFFICE3 : SIGNATURE_OLDOFFICE4,
7606 salt.salt_buf[0],
7607 salt.salt_buf[1],
7608 salt.salt_buf[2],
7609 salt.salt_buf[3],
7610 byte_swap_32 (oldoffice34->encryptedVerifier[0]),
7611 byte_swap_32 (oldoffice34->encryptedVerifier[1]),
7612 byte_swap_32 (oldoffice34->encryptedVerifier[2]),
7613 byte_swap_32 (oldoffice34->encryptedVerifier[3]),
7614 byte_swap_32 (oldoffice34->encryptedVerifierHash[0]),
7615 byte_swap_32 (oldoffice34->encryptedVerifierHash[1]),
7616 byte_swap_32 (oldoffice34->encryptedVerifierHash[2]),
7617 byte_swap_32 (oldoffice34->encryptedVerifierHash[3]),
7618 byte_swap_32 (oldoffice34->encryptedVerifierHash[4]),
7619 rc4key[0],
7620 rc4key[1],
7621 rc4key[2],
7622 rc4key[3],
7623 rc4key[4]);
7624 }
7625 else if (hash_mode == 10000)
7626 {
7627 // salt
7628
7629 pbkdf2_sha256_t *pbkdf2_sha256s = (pbkdf2_sha256_t *) data.esalts_buf;
7630
7631 pbkdf2_sha256_t *pbkdf2_sha256 = &pbkdf2_sha256s[salt_pos];
7632
7633 unsigned char *salt_buf_ptr = (unsigned char *) pbkdf2_sha256->salt_buf;
7634
7635 // hash
7636
7637 digest_buf[0] = byte_swap_32 (digest_buf[0]);
7638 digest_buf[1] = byte_swap_32 (digest_buf[1]);
7639 digest_buf[2] = byte_swap_32 (digest_buf[2]);
7640 digest_buf[3] = byte_swap_32 (digest_buf[3]);
7641 digest_buf[4] = byte_swap_32 (digest_buf[4]);
7642 digest_buf[5] = byte_swap_32 (digest_buf[5]);
7643 digest_buf[6] = byte_swap_32 (digest_buf[6]);
7644 digest_buf[7] = byte_swap_32 (digest_buf[7]);
7645 digest_buf[8] = 0; // needed for base64_encode ()
7646
7647 char tmp_buf[64] = { 0 };
7648
7649 base64_encode (int_to_base64, (const u8 *) digest_buf, 32, (u8 *) tmp_buf);
7650
7651 // output
7652
7653 snprintf (out_buf, len-1, "%s%i$%s$%s", SIGNATURE_DJANGOPBKDF2, salt.salt_iter + 1, salt_buf_ptr, tmp_buf);
7654 }
7655 else if (hash_mode == 10100)
7656 {
7657 snprintf (out_buf, len-1, "%08x%08x:%u:%u:%08x%08x%08x%08x",
7658 digest_buf[0],
7659 digest_buf[1],
7660 2,
7661 4,
7662 byte_swap_32 (salt.salt_buf[0]),
7663 byte_swap_32 (salt.salt_buf[1]),
7664 byte_swap_32 (salt.salt_buf[2]),
7665 byte_swap_32 (salt.salt_buf[3]));
7666 }
7667 else if (hash_mode == 10200)
7668 {
7669 cram_md5_t *cram_md5s = (cram_md5_t *) data.esalts_buf;
7670
7671 cram_md5_t *cram_md5 = &cram_md5s[salt_pos];
7672
7673 // challenge
7674
7675 char challenge[100] = { 0 };
7676
7677 base64_encode (int_to_base64, (const u8 *) salt.salt_buf, salt.salt_len, (u8 *) challenge);
7678
7679 // response
7680
7681 char tmp_buf[100] = { 0 };
7682
7683 uint tmp_len = snprintf (tmp_buf, 100, "%s %08x%08x%08x%08x",
7684 (char *) cram_md5->user,
7685 digest_buf[0],
7686 digest_buf[1],
7687 digest_buf[2],
7688 digest_buf[3]);
7689
7690 char response[100] = { 0 };
7691
7692 base64_encode (int_to_base64, (const u8 *) tmp_buf, tmp_len, (u8 *) response);
7693
7694 snprintf (out_buf, len-1, "%s%s$%s", SIGNATURE_CRAM_MD5, challenge, response);
7695 }
7696 else if (hash_mode == 10300)
7697 {
7698 char tmp_buf[100] = { 0 };
7699
7700 memcpy (tmp_buf + 0, digest_buf, 20);
7701 memcpy (tmp_buf + 20, salt.salt_buf, salt.salt_len);
7702
7703 uint tmp_len = 20 + salt.salt_len;
7704
7705 // base64 encode it
7706
7707 char base64_encoded[100] = { 0 };
7708
7709 base64_encode (int_to_base64, (const u8 *) tmp_buf, tmp_len, (u8 *) base64_encoded);
7710
7711 snprintf (out_buf, len-1, "%s%i}%s", SIGNATURE_SAPH_SHA1, salt.salt_iter + 1, base64_encoded);
7712 }
7713 else if (hash_mode == 10400)
7714 {
7715 pdf_t *pdfs = (pdf_t *) data.esalts_buf;
7716
7717 pdf_t *pdf = &pdfs[salt_pos];
7718
7719 snprintf (out_buf, len-1, "$pdf$%d*%d*%d*%d*%d*%d*%08x%08x%08x%08x*%d*%08x%08x%08x%08x%08x%08x%08x%08x*%d*%08x%08x%08x%08x%08x%08x%08x%08x",
7720
7721 pdf->V,
7722 pdf->R,
7723 40,
7724 pdf->P,
7725 pdf->enc_md,
7726 pdf->id_len,
7727 byte_swap_32 (pdf->id_buf[0]),
7728 byte_swap_32 (pdf->id_buf[1]),
7729 byte_swap_32 (pdf->id_buf[2]),
7730 byte_swap_32 (pdf->id_buf[3]),
7731 pdf->u_len,
7732 byte_swap_32 (pdf->u_buf[0]),
7733 byte_swap_32 (pdf->u_buf[1]),
7734 byte_swap_32 (pdf->u_buf[2]),
7735 byte_swap_32 (pdf->u_buf[3]),
7736 byte_swap_32 (pdf->u_buf[4]),
7737 byte_swap_32 (pdf->u_buf[5]),
7738 byte_swap_32 (pdf->u_buf[6]),
7739 byte_swap_32 (pdf->u_buf[7]),
7740 pdf->o_len,
7741 byte_swap_32 (pdf->o_buf[0]),
7742 byte_swap_32 (pdf->o_buf[1]),
7743 byte_swap_32 (pdf->o_buf[2]),
7744 byte_swap_32 (pdf->o_buf[3]),
7745 byte_swap_32 (pdf->o_buf[4]),
7746 byte_swap_32 (pdf->o_buf[5]),
7747 byte_swap_32 (pdf->o_buf[6]),
7748 byte_swap_32 (pdf->o_buf[7])
7749 );
7750 }
7751 else if (hash_mode == 10410)
7752 {
7753 pdf_t *pdfs = (pdf_t *) data.esalts_buf;
7754
7755 pdf_t *pdf = &pdfs[salt_pos];
7756
7757 snprintf (out_buf, len-1, "$pdf$%d*%d*%d*%d*%d*%d*%08x%08x%08x%08x*%d*%08x%08x%08x%08x%08x%08x%08x%08x*%d*%08x%08x%08x%08x%08x%08x%08x%08x",
7758
7759 pdf->V,
7760 pdf->R,
7761 40,
7762 pdf->P,
7763 pdf->enc_md,
7764 pdf->id_len,
7765 byte_swap_32 (pdf->id_buf[0]),
7766 byte_swap_32 (pdf->id_buf[1]),
7767 byte_swap_32 (pdf->id_buf[2]),
7768 byte_swap_32 (pdf->id_buf[3]),
7769 pdf->u_len,
7770 byte_swap_32 (pdf->u_buf[0]),
7771 byte_swap_32 (pdf->u_buf[1]),
7772 byte_swap_32 (pdf->u_buf[2]),
7773 byte_swap_32 (pdf->u_buf[3]),
7774 byte_swap_32 (pdf->u_buf[4]),
7775 byte_swap_32 (pdf->u_buf[5]),
7776 byte_swap_32 (pdf->u_buf[6]),
7777 byte_swap_32 (pdf->u_buf[7]),
7778 pdf->o_len,
7779 byte_swap_32 (pdf->o_buf[0]),
7780 byte_swap_32 (pdf->o_buf[1]),
7781 byte_swap_32 (pdf->o_buf[2]),
7782 byte_swap_32 (pdf->o_buf[3]),
7783 byte_swap_32 (pdf->o_buf[4]),
7784 byte_swap_32 (pdf->o_buf[5]),
7785 byte_swap_32 (pdf->o_buf[6]),
7786 byte_swap_32 (pdf->o_buf[7])
7787 );
7788 }
7789 else if (hash_mode == 10420)
7790 {
7791 pdf_t *pdfs = (pdf_t *) data.esalts_buf;
7792
7793 pdf_t *pdf = &pdfs[salt_pos];
7794
7795 u8 *rc4key = (u8 *) pdf->rc4key;
7796
7797 snprintf (out_buf, len-1, "$pdf$%d*%d*%d*%d*%d*%d*%08x%08x%08x%08x*%d*%08x%08x%08x%08x%08x%08x%08x%08x*%d*%08x%08x%08x%08x%08x%08x%08x%08x:%02x%02x%02x%02x%02x",
7798
7799 pdf->V,
7800 pdf->R,
7801 40,
7802 pdf->P,
7803 pdf->enc_md,
7804 pdf->id_len,
7805 byte_swap_32 (pdf->id_buf[0]),
7806 byte_swap_32 (pdf->id_buf[1]),
7807 byte_swap_32 (pdf->id_buf[2]),
7808 byte_swap_32 (pdf->id_buf[3]),
7809 pdf->u_len,
7810 byte_swap_32 (pdf->u_buf[0]),
7811 byte_swap_32 (pdf->u_buf[1]),
7812 byte_swap_32 (pdf->u_buf[2]),
7813 byte_swap_32 (pdf->u_buf[3]),
7814 byte_swap_32 (pdf->u_buf[4]),
7815 byte_swap_32 (pdf->u_buf[5]),
7816 byte_swap_32 (pdf->u_buf[6]),
7817 byte_swap_32 (pdf->u_buf[7]),
7818 pdf->o_len,
7819 byte_swap_32 (pdf->o_buf[0]),
7820 byte_swap_32 (pdf->o_buf[1]),
7821 byte_swap_32 (pdf->o_buf[2]),
7822 byte_swap_32 (pdf->o_buf[3]),
7823 byte_swap_32 (pdf->o_buf[4]),
7824 byte_swap_32 (pdf->o_buf[5]),
7825 byte_swap_32 (pdf->o_buf[6]),
7826 byte_swap_32 (pdf->o_buf[7]),
7827 rc4key[0],
7828 rc4key[1],
7829 rc4key[2],
7830 rc4key[3],
7831 rc4key[4]
7832 );
7833 }
7834 else if (hash_mode == 10500)
7835 {
7836 pdf_t *pdfs = (pdf_t *) data.esalts_buf;
7837
7838 pdf_t *pdf = &pdfs[salt_pos];
7839
7840 if (pdf->id_len == 32)
7841 {
7842 snprintf (out_buf, len-1, "$pdf$%d*%d*%d*%d*%d*%d*%08x%08x%08x%08x%08x%08x%08x%08x*%d*%08x%08x%08x%08x%08x%08x%08x%08x*%d*%08x%08x%08x%08x%08x%08x%08x%08x",
7843
7844 pdf->V,
7845 pdf->R,
7846 128,
7847 pdf->P,
7848 pdf->enc_md,
7849 pdf->id_len,
7850 byte_swap_32 (pdf->id_buf[0]),
7851 byte_swap_32 (pdf->id_buf[1]),
7852 byte_swap_32 (pdf->id_buf[2]),
7853 byte_swap_32 (pdf->id_buf[3]),
7854 byte_swap_32 (pdf->id_buf[4]),
7855 byte_swap_32 (pdf->id_buf[5]),
7856 byte_swap_32 (pdf->id_buf[6]),
7857 byte_swap_32 (pdf->id_buf[7]),
7858 pdf->u_len,
7859 byte_swap_32 (pdf->u_buf[0]),
7860 byte_swap_32 (pdf->u_buf[1]),
7861 byte_swap_32 (pdf->u_buf[2]),
7862 byte_swap_32 (pdf->u_buf[3]),
7863 byte_swap_32 (pdf->u_buf[4]),
7864 byte_swap_32 (pdf->u_buf[5]),
7865 byte_swap_32 (pdf->u_buf[6]),
7866 byte_swap_32 (pdf->u_buf[7]),
7867 pdf->o_len,
7868 byte_swap_32 (pdf->o_buf[0]),
7869 byte_swap_32 (pdf->o_buf[1]),
7870 byte_swap_32 (pdf->o_buf[2]),
7871 byte_swap_32 (pdf->o_buf[3]),
7872 byte_swap_32 (pdf->o_buf[4]),
7873 byte_swap_32 (pdf->o_buf[5]),
7874 byte_swap_32 (pdf->o_buf[6]),
7875 byte_swap_32 (pdf->o_buf[7])
7876 );
7877 }
7878 else
7879 {
7880 snprintf (out_buf, len-1, "$pdf$%d*%d*%d*%d*%d*%d*%08x%08x%08x%08x*%d*%08x%08x%08x%08x%08x%08x%08x%08x*%d*%08x%08x%08x%08x%08x%08x%08x%08x",
7881
7882 pdf->V,
7883 pdf->R,
7884 128,
7885 pdf->P,
7886 pdf->enc_md,
7887 pdf->id_len,
7888 byte_swap_32 (pdf->id_buf[0]),
7889 byte_swap_32 (pdf->id_buf[1]),
7890 byte_swap_32 (pdf->id_buf[2]),
7891 byte_swap_32 (pdf->id_buf[3]),
7892 pdf->u_len,
7893 byte_swap_32 (pdf->u_buf[0]),
7894 byte_swap_32 (pdf->u_buf[1]),
7895 byte_swap_32 (pdf->u_buf[2]),
7896 byte_swap_32 (pdf->u_buf[3]),
7897 byte_swap_32 (pdf->u_buf[4]),
7898 byte_swap_32 (pdf->u_buf[5]),
7899 byte_swap_32 (pdf->u_buf[6]),
7900 byte_swap_32 (pdf->u_buf[7]),
7901 pdf->o_len,
7902 byte_swap_32 (pdf->o_buf[0]),
7903 byte_swap_32 (pdf->o_buf[1]),
7904 byte_swap_32 (pdf->o_buf[2]),
7905 byte_swap_32 (pdf->o_buf[3]),
7906 byte_swap_32 (pdf->o_buf[4]),
7907 byte_swap_32 (pdf->o_buf[5]),
7908 byte_swap_32 (pdf->o_buf[6]),
7909 byte_swap_32 (pdf->o_buf[7])
7910 );
7911 }
7912 }
7913 else if (hash_mode == 10600)
7914 {
7915 uint digest_idx = salt.digests_offset + digest_pos;
7916
7917 hashinfo_t **hashinfo_ptr = data.hash_info;
7918 char *hash_buf = hashinfo_ptr[digest_idx]->orighash;
7919
7920 snprintf (out_buf, len-1, "%s", hash_buf);
7921 }
7922 else if (hash_mode == 10700)
7923 {
7924 uint digest_idx = salt.digests_offset + digest_pos;
7925
7926 hashinfo_t **hashinfo_ptr = data.hash_info;
7927 char *hash_buf = hashinfo_ptr[digest_idx]->orighash;
7928
7929 snprintf (out_buf, len-1, "%s", hash_buf);
7930 }
7931 else if (hash_mode == 10900)
7932 {
7933 uint digest_idx = salt.digests_offset + digest_pos;
7934
7935 hashinfo_t **hashinfo_ptr = data.hash_info;
7936 char *hash_buf = hashinfo_ptr[digest_idx]->orighash;
7937
7938 snprintf (out_buf, len-1, "%s", hash_buf);
7939 }
7940 else if (hash_mode == 11100)
7941 {
7942 u32 salt_challenge = salt.salt_buf[0];
7943
7944 salt_challenge = byte_swap_32 (salt_challenge);
7945
7946 unsigned char *user_name = (unsigned char *) (salt.salt_buf + 1);
7947
7948 snprintf (out_buf, len-1, "%s%s*%08x*%08x%08x%08x%08x",
7949 SIGNATURE_POSTGRESQL_AUTH,
7950 user_name,
7951 salt_challenge,
7952 digest_buf[0],
7953 digest_buf[1],
7954 digest_buf[2],
7955 digest_buf[3]);
7956 }
7957 else if (hash_mode == 11200)
7958 {
7959 snprintf (out_buf, len-1, "%s%s*%08x%08x%08x%08x%08x",
7960 SIGNATURE_MYSQL_AUTH,
7961 (unsigned char *) salt.salt_buf,
7962 digest_buf[0],
7963 digest_buf[1],
7964 digest_buf[2],
7965 digest_buf[3],
7966 digest_buf[4]);
7967 }
7968 else if (hash_mode == 11300)
7969 {
7970 bitcoin_wallet_t *bitcoin_wallets = (bitcoin_wallet_t *) data.esalts_buf;
7971
7972 bitcoin_wallet_t *bitcoin_wallet = &bitcoin_wallets[salt_pos];
7973
7974 const uint cry_master_len = bitcoin_wallet->cry_master_len;
7975 const uint ckey_len = bitcoin_wallet->ckey_len;
7976 const uint public_key_len = bitcoin_wallet->public_key_len;
7977
7978 char *cry_master_buf = (char *) mymalloc ((cry_master_len * 2) + 1);
7979 char *ckey_buf = (char *) mymalloc ((ckey_len * 2) + 1);
7980 char *public_key_buf = (char *) mymalloc ((public_key_len * 2) + 1);
7981
7982 for (uint i = 0, j = 0; i < cry_master_len; i += 1, j += 2)
7983 {
7984 const u8 *ptr = (const u8 *) bitcoin_wallet->cry_master_buf;
7985
7986 sprintf (cry_master_buf + j, "%02x", ptr[i]);
7987 }
7988
7989 for (uint i = 0, j = 0; i < ckey_len; i += 1, j += 2)
7990 {
7991 const u8 *ptr = (const u8 *) bitcoin_wallet->ckey_buf;
7992
7993 sprintf (ckey_buf + j, "%02x", ptr[i]);
7994 }
7995
7996 for (uint i = 0, j = 0; i < public_key_len; i += 1, j += 2)
7997 {
7998 const u8 *ptr = (const u8 *) bitcoin_wallet->public_key_buf;
7999
8000 sprintf (public_key_buf + j, "%02x", ptr[i]);
8001 }
8002
8003 snprintf (out_buf, len-1, "%s%d$%s$%d$%s$%d$%d$%s$%d$%s",
8004 SIGNATURE_BITCOIN_WALLET,
8005 cry_master_len * 2,
8006 cry_master_buf,
8007 salt.salt_len,
8008 (unsigned char *) salt.salt_buf,
8009 salt.salt_iter + 1,
8010 ckey_len * 2,
8011 ckey_buf,
8012 public_key_len * 2,
8013 public_key_buf
8014 );
8015
8016 free (cry_master_buf);
8017 free (ckey_buf);
8018 free (public_key_buf);
8019 }
8020 else if (hash_mode == 11400)
8021 {
8022 uint digest_idx = salt.digests_offset + digest_pos;
8023
8024 hashinfo_t **hashinfo_ptr = data.hash_info;
8025 char *hash_buf = hashinfo_ptr[digest_idx]->orighash;
8026
8027 snprintf (out_buf, len-1, "%s", hash_buf);
8028 }
8029 else if (hash_mode == 11600)
8030 {
8031 seven_zip_t *seven_zips = (seven_zip_t *) data.esalts_buf;
8032
8033 seven_zip_t *seven_zip = &seven_zips[salt_pos];
8034
8035 const uint data_len = seven_zip->data_len;
8036
8037 char *data_buf = (char *) mymalloc ((data_len * 2) + 1);
8038
8039 for (uint i = 0, j = 0; i < data_len; i += 1, j += 2)
8040 {
8041 const u8 *ptr = (const u8 *) seven_zip->data_buf;
8042
8043 sprintf (data_buf + j, "%02x", ptr[i]);
8044 }
8045
8046 snprintf (out_buf, len-1, "%s%u$%u$%u$%s$%u$%08x%08x%08x%08x$%u$%u$%u$%s",
8047 SIGNATURE_SEVEN_ZIP,
8048 0,
8049 salt.salt_sign[0],
8050 0,
8051 (char *) seven_zip->salt_buf,
8052 seven_zip->iv_len,
8053 seven_zip->iv_buf[0],
8054 seven_zip->iv_buf[1],
8055 seven_zip->iv_buf[2],
8056 seven_zip->iv_buf[3],
8057 seven_zip->crc,
8058 seven_zip->data_len,
8059 seven_zip->unpack_size,
8060 data_buf);
8061
8062 free (data_buf);
8063 }
8064 else if (hash_mode == 11700)
8065 {
8066 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x%08x%08x%08x",
8067 digest_buf[0],
8068 digest_buf[1],
8069 digest_buf[2],
8070 digest_buf[3],
8071 digest_buf[4],
8072 digest_buf[5],
8073 digest_buf[6],
8074 digest_buf[7]);
8075 }
8076 else if (hash_mode == 11800)
8077 {
8078 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
8079 digest_buf[ 0],
8080 digest_buf[ 1],
8081 digest_buf[ 2],
8082 digest_buf[ 3],
8083 digest_buf[ 4],
8084 digest_buf[ 5],
8085 digest_buf[ 6],
8086 digest_buf[ 7],
8087 digest_buf[ 8],
8088 digest_buf[ 9],
8089 digest_buf[10],
8090 digest_buf[11],
8091 digest_buf[12],
8092 digest_buf[13],
8093 digest_buf[14],
8094 digest_buf[15]);
8095 }
8096 else if (hash_mode == 11900)
8097 {
8098 uint digest_idx = salt.digests_offset + digest_pos;
8099
8100 hashinfo_t **hashinfo_ptr = data.hash_info;
8101 char *hash_buf = hashinfo_ptr[digest_idx]->orighash;
8102
8103 snprintf (out_buf, len-1, "%s", hash_buf);
8104 }
8105 else if (hash_mode == 12000)
8106 {
8107 uint digest_idx = salt.digests_offset + digest_pos;
8108
8109 hashinfo_t **hashinfo_ptr = data.hash_info;
8110 char *hash_buf = hashinfo_ptr[digest_idx]->orighash;
8111
8112 snprintf (out_buf, len-1, "%s", hash_buf);
8113 }
8114 else if (hash_mode == 12100)
8115 {
8116 uint digest_idx = salt.digests_offset + digest_pos;
8117
8118 hashinfo_t **hashinfo_ptr = data.hash_info;
8119 char *hash_buf = hashinfo_ptr[digest_idx]->orighash;
8120
8121 snprintf (out_buf, len-1, "%s", hash_buf);
8122 }
8123 else if (hash_mode == 12200)
8124 {
8125 uint *ptr_digest = digest_buf;
8126 uint *ptr_salt = salt.salt_buf;
8127
8128 snprintf (out_buf, len-1, "%s0$1$%08x%08x$%08x%08x",
8129 SIGNATURE_ECRYPTFS,
8130 ptr_salt[0],
8131 ptr_salt[1],
8132 ptr_digest[0],
8133 ptr_digest[1]);
8134 }
8135 else if (hash_mode == 12300)
8136 {
8137 uint *ptr_digest = digest_buf;
8138 uint *ptr_salt = salt.salt_buf;
8139
8140 snprintf (out_buf, len-1, "%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X",
8141 ptr_digest[ 0], ptr_digest[ 1],
8142 ptr_digest[ 2], ptr_digest[ 3],
8143 ptr_digest[ 4], ptr_digest[ 5],
8144 ptr_digest[ 6], ptr_digest[ 7],
8145 ptr_digest[ 8], ptr_digest[ 9],
8146 ptr_digest[10], ptr_digest[11],
8147 ptr_digest[12], ptr_digest[13],
8148 ptr_digest[14], ptr_digest[15],
8149 ptr_salt[0],
8150 ptr_salt[1],
8151 ptr_salt[2],
8152 ptr_salt[3]);
8153 }
8154 else if (hash_mode == 12400)
8155 {
8156 // encode iteration count
8157
8158 char salt_iter[5] = { 0 };
8159
8160 salt_iter[0] = int_to_itoa64 ((salt.salt_iter ) & 0x3f);
8161 salt_iter[1] = int_to_itoa64 ((salt.salt_iter >> 6) & 0x3f);
8162 salt_iter[2] = int_to_itoa64 ((salt.salt_iter >> 12) & 0x3f);
8163 salt_iter[3] = int_to_itoa64 ((salt.salt_iter >> 18) & 0x3f);
8164 salt_iter[4] = 0;
8165
8166 // encode salt
8167
8168 ptr_salt[0] = int_to_itoa64 ((salt.salt_buf[0] ) & 0x3f);
8169 ptr_salt[1] = int_to_itoa64 ((salt.salt_buf[0] >> 6) & 0x3f);
8170 ptr_salt[2] = int_to_itoa64 ((salt.salt_buf[0] >> 12) & 0x3f);
8171 ptr_salt[3] = int_to_itoa64 ((salt.salt_buf[0] >> 18) & 0x3f);
8172 ptr_salt[4] = 0;
8173
8174 // encode digest
8175
8176 memset (tmp_buf, 0, sizeof (tmp_buf));
8177
8178 digest_buf[0] = byte_swap_32 (digest_buf[0]);
8179 digest_buf[1] = byte_swap_32 (digest_buf[1]);
8180
8181 memcpy (tmp_buf, digest_buf, 8);
8182
8183 base64_encode (int_to_itoa64, (const u8 *) tmp_buf, 8, (u8 *) ptr_plain);
8184
8185 ptr_plain[11] = 0;
8186
8187 // fill the resulting buffer
8188
8189 snprintf (out_buf, len - 1, "_%s%s%s", salt_iter, ptr_salt, ptr_plain);
8190 }
8191 else if (hash_mode == 12500)
8192 {
8193 snprintf (out_buf, len - 1, "%s*0*%08x%08x*%08x%08x%08x%08x",
8194 SIGNATURE_RAR3,
8195 byte_swap_32 (salt.salt_buf[0]),
8196 byte_swap_32 (salt.salt_buf[1]),
8197 salt.salt_buf[2],
8198 salt.salt_buf[3],
8199 salt.salt_buf[4],
8200 salt.salt_buf[5]);
8201 }
8202 else if (hash_mode == 12600)
8203 {
8204 snprintf (out_buf, len - 1, "%08x%08x%08x%08x%08x%08x%08x%08x",
8205 digest_buf[0] + salt.salt_buf_pc[0],
8206 digest_buf[1] + salt.salt_buf_pc[1],
8207 digest_buf[2] + salt.salt_buf_pc[2],
8208 digest_buf[3] + salt.salt_buf_pc[3],
8209 digest_buf[4] + salt.salt_buf_pc[4],
8210 digest_buf[5] + salt.salt_buf_pc[5],
8211 digest_buf[6] + salt.salt_buf_pc[6],
8212 digest_buf[7] + salt.salt_buf_pc[7]);
8213 }
8214 else if (hash_mode == 12700)
8215 {
8216 uint digest_idx = salt.digests_offset + digest_pos;
8217
8218 hashinfo_t **hashinfo_ptr = data.hash_info;
8219 char *hash_buf = hashinfo_ptr[digest_idx]->orighash;
8220
8221 snprintf (out_buf, len-1, "%s", hash_buf);
8222 }
8223 else if (hash_mode == 12800)
8224 {
8225 const u8 *ptr = (const u8 *) salt.salt_buf;
8226
8227 snprintf (out_buf, len-1, "%s,%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x,%d,%08x%08x%08x%08x%08x%08x%08x%08x",
8228 SIGNATURE_MS_DRSR,
8229 ptr[0],
8230 ptr[1],
8231 ptr[2],
8232 ptr[3],
8233 ptr[4],
8234 ptr[5],
8235 ptr[6],
8236 ptr[7],
8237 ptr[8],
8238 ptr[9],
8239 salt.salt_iter + 1,
8240 byte_swap_32 (digest_buf[0]),
8241 byte_swap_32 (digest_buf[1]),
8242 byte_swap_32 (digest_buf[2]),
8243 byte_swap_32 (digest_buf[3]),
8244 byte_swap_32 (digest_buf[4]),
8245 byte_swap_32 (digest_buf[5]),
8246 byte_swap_32 (digest_buf[6]),
8247 byte_swap_32 (digest_buf[7])
8248 );
8249 }
8250 else if (hash_mode == 12900)
8251 {
8252 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
8253 salt.salt_buf[ 4],
8254 salt.salt_buf[ 5],
8255 salt.salt_buf[ 6],
8256 salt.salt_buf[ 7],
8257 salt.salt_buf[ 8],
8258 salt.salt_buf[ 9],
8259 salt.salt_buf[10],
8260 salt.salt_buf[11],
8261 byte_swap_32 (digest_buf[0]),
8262 byte_swap_32 (digest_buf[1]),
8263 byte_swap_32 (digest_buf[2]),
8264 byte_swap_32 (digest_buf[3]),
8265 byte_swap_32 (digest_buf[4]),
8266 byte_swap_32 (digest_buf[5]),
8267 byte_swap_32 (digest_buf[6]),
8268 byte_swap_32 (digest_buf[7]),
8269 salt.salt_buf[ 0],
8270 salt.salt_buf[ 1],
8271 salt.salt_buf[ 2],
8272 salt.salt_buf[ 3]
8273 );
8274 }
8275 else if (hash_mode == 13000)
8276 {
8277 rar5_t *rar5s = (rar5_t *) data.esalts_buf;
8278
8279 rar5_t *rar5 = &rar5s[salt_pos];
8280
8281 snprintf (out_buf, len-1, "$rar5$16$%08x%08x%08x%08x$%u$%08x%08x%08x%08x$8$%08x%08x",
8282 salt.salt_buf[0],
8283 salt.salt_buf[1],
8284 salt.salt_buf[2],
8285 salt.salt_buf[3],
8286 salt.salt_sign[0],
8287 rar5->iv[0],
8288 rar5->iv[1],
8289 rar5->iv[2],
8290 rar5->iv[3],
8291 byte_swap_32 (digest_buf[0]),
8292 byte_swap_32 (digest_buf[1])
8293 );
8294 }
8295 else if (hash_mode == 13100)
8296 {
8297 krb5tgs_t *krb5tgss = (krb5tgs_t *) data.esalts_buf;
8298
8299 krb5tgs_t *krb5tgs = &krb5tgss[salt_pos];
8300
8301 u8 *ptr_checksum = (u8 *) krb5tgs->checksum;
8302 u8 *ptr_edata2 = (u8 *) krb5tgs->edata2;
8303
8304 char data[2560 * 4 * 2] = { 0 };
8305
8306 char *ptr_data = data;
8307
8308 for (uint i = 0; i < 16; i++, ptr_data += 2)
8309 sprintf (ptr_data, "%02x", ptr_checksum[i]);
8310
8311 /* skip '$' */
8312 ptr_data++;
8313
8314 for (uint i = 0; i < krb5tgs->edata2_len; i++, ptr_data += 2)
8315 sprintf (ptr_data, "%02x", ptr_edata2[i]);
8316
8317 snprintf (out_buf, len-1, "%s$%s$%s$%s",
8318 SIGNATURE_KRB5TGS,
8319 (char *) krb5tgs->account_info,
8320 data,
8321 data + 33);
8322 }
8323 else if (hash_mode == 13200)
8324 {
8325 snprintf (out_buf, len-1, "%s*%d*%08x%08x%08x%08x*%08x%08x%08x%08x%08x%08x",
8326 SIGNATURE_AXCRYPT,
8327 salt.salt_iter,
8328 salt.salt_buf[0],
8329 salt.salt_buf[1],
8330 salt.salt_buf[2],
8331 salt.salt_buf[3],
8332 salt.salt_buf[4],
8333 salt.salt_buf[5],
8334 salt.salt_buf[6],
8335 salt.salt_buf[7],
8336 salt.salt_buf[8],
8337 salt.salt_buf[9]);
8338 }
8339 else if (hash_mode == 13300)
8340 {
8341 snprintf (out_buf, len-1, "%s$%08x%08x%08x%08x",
8342 SIGNATURE_AXCRYPT_SHA1,
8343 digest_buf[0],
8344 digest_buf[1],
8345 digest_buf[2],
8346 digest_buf[3]);
8347 }
8348 else if (hash_mode == 13400)
8349 {
8350 keepass_t *keepasss = (keepass_t *) data.esalts_buf;
8351
8352 keepass_t *keepass = &keepasss[salt_pos];
8353
8354 u32 version = (u32) keepass->version;
8355 u32 rounds = salt.salt_iter;
8356 u32 algorithm = (u32) keepass->algorithm;
8357 u32 keyfile_len = (u32) keepass->keyfile_len;
8358
8359 u32 *ptr_final_random_seed = (u32 *) keepass->final_random_seed ;
8360 u32 *ptr_transf_random_seed = (u32 *) keepass->transf_random_seed ;
8361 u32 *ptr_enc_iv = (u32 *) keepass->enc_iv ;
8362 u32 *ptr_contents_hash = (u32 *) keepass->contents_hash ;
8363 u32 *ptr_keyfile = (u32 *) keepass->keyfile ;
8364
8365 /* specific to version 1 */
8366 u32 contents_len;
8367 u32 *ptr_contents;
8368
8369 /* specific to version 2 */
8370 u32 expected_bytes_len;
8371 u32 *ptr_expected_bytes;
8372
8373 u32 final_random_seed_len;
8374 u32 transf_random_seed_len;
8375 u32 enc_iv_len;
8376 u32 contents_hash_len;
8377
8378 transf_random_seed_len = 8;
8379 enc_iv_len = 4;
8380 contents_hash_len = 8;
8381 final_random_seed_len = 8;
8382
8383 if (version == 1)
8384 final_random_seed_len = 4;
8385
8386 snprintf (out_buf, len-1, "%s*%d*%d*%d",
8387 SIGNATURE_KEEPASS,
8388 version,
8389 rounds,
8390 algorithm);
8391
8392 char *ptr_data = out_buf;
8393
8394 ptr_data += strlen(out_buf);
8395
8396 *ptr_data = '*';
8397 ptr_data++;
8398
8399 for (uint i = 0; i < final_random_seed_len; i++, ptr_data += 8)
8400 sprintf (ptr_data, "%08x", ptr_final_random_seed[i]);
8401
8402 *ptr_data = '*';
8403 ptr_data++;
8404
8405 for (uint i = 0; i < transf_random_seed_len; i++, ptr_data += 8)
8406 sprintf (ptr_data, "%08x", ptr_transf_random_seed[i]);
8407
8408 *ptr_data = '*';
8409 ptr_data++;
8410
8411 for (uint i = 0; i < enc_iv_len; i++, ptr_data += 8)
8412 sprintf (ptr_data, "%08x", ptr_enc_iv[i]);
8413
8414 *ptr_data = '*';
8415 ptr_data++;
8416
8417 if (version == 1)
8418 {
8419 contents_len = (u32) keepass->contents_len;
8420 ptr_contents = (u32 *) keepass->contents;
8421
8422 for (uint i = 0; i < contents_hash_len; i++, ptr_data += 8)
8423 sprintf (ptr_data, "%08x", ptr_contents_hash[i]);
8424
8425 *ptr_data = '*';
8426 ptr_data++;
8427
8428 /* inline flag */
8429 *ptr_data = '1';
8430 ptr_data++;
8431
8432 *ptr_data = '*';
8433 ptr_data++;
8434
8435 char ptr_contents_len[10] = { 0 };
8436
8437 sprintf ((char*) ptr_contents_len, "%d", contents_len);
8438
8439 sprintf (ptr_data, "%d", contents_len);
8440
8441 ptr_data += strlen(ptr_contents_len);
8442
8443 *ptr_data = '*';
8444 ptr_data++;
8445
8446 for (uint i = 0; i < contents_len / 4; i++, ptr_data += 8)
8447 sprintf (ptr_data, "%08x", ptr_contents[i]);
8448 }
8449 else if (version == 2)
8450 {
8451 expected_bytes_len = 8;
8452 ptr_expected_bytes = (u32 *) keepass->expected_bytes ;
8453
8454 for (uint i = 0; i < expected_bytes_len; i++, ptr_data += 8)
8455 sprintf (ptr_data, "%08x", ptr_expected_bytes[i]);
8456
8457 *ptr_data = '*';
8458 ptr_data++;
8459
8460 for (uint i = 0; i < contents_hash_len; i++, ptr_data += 8)
8461 sprintf (ptr_data, "%08x", ptr_contents_hash[i]);
8462 }
8463 if (keyfile_len)
8464 {
8465 *ptr_data = '*';
8466 ptr_data++;
8467
8468 /* inline flag */
8469 *ptr_data = '1';
8470 ptr_data++;
8471
8472 *ptr_data = '*';
8473 ptr_data++;
8474
8475 sprintf (ptr_data, "%d", keyfile_len);
8476
8477 ptr_data += 2;
8478
8479 *ptr_data = '*';
8480 ptr_data++;
8481
8482 for (uint i = 0; i < 8; i++, ptr_data += 8)
8483 sprintf (ptr_data, "%08x", ptr_keyfile[i]);
8484 }
8485 }
8486 else if (hash_mode == 13500)
8487 {
8488 pstoken_t *pstokens = (pstoken_t *) data.esalts_buf;
8489
8490 pstoken_t *pstoken = &pstokens[salt_pos];
8491
8492 const u32 salt_len = (pstoken->salt_len > 512) ? 512 : pstoken->salt_len;
8493
8494 char pstoken_tmp[1024 + 1] = { 0 };
8495
8496 for (uint i = 0, j = 0; i < salt_len; i += 1, j += 2)
8497 {
8498 const u8 *ptr = (const u8 *) pstoken->salt_buf;
8499
8500 sprintf (pstoken_tmp + j, "%02x", ptr[i]);
8501 }
8502
8503 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x:%s",
8504 digest_buf[0],
8505 digest_buf[1],
8506 digest_buf[2],
8507 digest_buf[3],
8508 digest_buf[4],
8509 pstoken_tmp);
8510 }
8511 else if (hash_mode == 13600)
8512 {
8513 zip2_t *zip2s = (zip2_t *) data.esalts_buf;
8514
8515 zip2_t *zip2 = &zip2s[salt_pos];
8516
8517 const u32 salt_len = zip2->salt_len;
8518
8519 char salt_tmp[32 + 1] = { 0 };
8520
8521 for (uint i = 0, j = 0; i < salt_len; i += 1, j += 2)
8522 {
8523 const u8 *ptr = (const u8 *) zip2->salt_buf;
8524
8525 sprintf (salt_tmp + j, "%02x", ptr[i]);
8526 }
8527
8528 const u32 data_len = zip2->data_len;
8529
8530 char data_tmp[8192 + 1] = { 0 };
8531
8532 for (uint i = 0, j = 0; i < data_len; i += 1, j += 2)
8533 {
8534 const u8 *ptr = (const u8 *) zip2->data_buf;
8535
8536 sprintf (data_tmp + j, "%02x", ptr[i]);
8537 }
8538
8539 const u32 auth_len = zip2->auth_len;
8540
8541 char auth_tmp[20 + 1] = { 0 };
8542
8543 for (uint i = 0, j = 0; i < auth_len; i += 1, j += 2)
8544 {
8545 const u8 *ptr = (const u8 *) zip2->auth_buf;
8546
8547 sprintf (auth_tmp + j, "%02x", ptr[i]);
8548 }
8549
8550 snprintf (out_buf, 255, "%s*%u*%u*%u*%s*%x*%u*%s*%s*%s",
8551 SIGNATURE_ZIP2_START,
8552 zip2->type,
8553 zip2->mode,
8554 zip2->magic,
8555 salt_tmp,
8556 zip2->verify_bytes,
8557 zip2->compress_length,
8558 data_tmp,
8559 auth_tmp,
8560 SIGNATURE_ZIP2_STOP);
8561 }
8562 else
8563 {
8564 if (hash_type == HASH_TYPE_MD4)
8565 {
8566 snprintf (out_buf, 255, "%08x%08x%08x%08x",
8567 digest_buf[0],
8568 digest_buf[1],
8569 digest_buf[2],
8570 digest_buf[3]);
8571 }
8572 else if (hash_type == HASH_TYPE_MD5)
8573 {
8574 snprintf (out_buf, len-1, "%08x%08x%08x%08x",
8575 digest_buf[0],
8576 digest_buf[1],
8577 digest_buf[2],
8578 digest_buf[3]);
8579 }
8580 else if (hash_type == HASH_TYPE_SHA1)
8581 {
8582 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x",
8583 digest_buf[0],
8584 digest_buf[1],
8585 digest_buf[2],
8586 digest_buf[3],
8587 digest_buf[4]);
8588 }
8589 else if (hash_type == HASH_TYPE_SHA256)
8590 {
8591 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x%08x%08x%08x",
8592 digest_buf[0],
8593 digest_buf[1],
8594 digest_buf[2],
8595 digest_buf[3],
8596 digest_buf[4],
8597 digest_buf[5],
8598 digest_buf[6],
8599 digest_buf[7]);
8600 }
8601 else if (hash_type == HASH_TYPE_SHA384)
8602 {
8603 uint *ptr = digest_buf;
8604
8605 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
8606 ptr[ 1], ptr[ 0],
8607 ptr[ 3], ptr[ 2],
8608 ptr[ 5], ptr[ 4],
8609 ptr[ 7], ptr[ 6],
8610 ptr[ 9], ptr[ 8],
8611 ptr[11], ptr[10]);
8612 }
8613 else if (hash_type == HASH_TYPE_SHA512)
8614 {
8615 uint *ptr = digest_buf;
8616
8617 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
8618 ptr[ 1], ptr[ 0],
8619 ptr[ 3], ptr[ 2],
8620 ptr[ 5], ptr[ 4],
8621 ptr[ 7], ptr[ 6],
8622 ptr[ 9], ptr[ 8],
8623 ptr[11], ptr[10],
8624 ptr[13], ptr[12],
8625 ptr[15], ptr[14]);
8626 }
8627 else if (hash_type == HASH_TYPE_LM)
8628 {
8629 snprintf (out_buf, len-1, "%08x%08x",
8630 digest_buf[0],
8631 digest_buf[1]);
8632 }
8633 else if (hash_type == HASH_TYPE_ORACLEH)
8634 {
8635 snprintf (out_buf, len-1, "%08X%08X",
8636 digest_buf[0],
8637 digest_buf[1]);
8638 }
8639 else if (hash_type == HASH_TYPE_BCRYPT)
8640 {
8641 base64_encode (int_to_bf64, (const u8 *) salt.salt_buf, 16, (u8 *) tmp_buf + 0);
8642 base64_encode (int_to_bf64, (const u8 *) digest_buf, 23, (u8 *) tmp_buf + 22);
8643
8644 tmp_buf[22 + 31] = 0; // base64_encode wants to pad
8645
8646 snprintf (out_buf, len-1, "%s$%s", (char *) salt.salt_sign, tmp_buf);
8647 }
8648 else if (hash_type == HASH_TYPE_KECCAK)
8649 {
8650 uint *ptr = digest_buf;
8651
8652 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
8653 ptr[ 1], ptr[ 0],
8654 ptr[ 3], ptr[ 2],
8655 ptr[ 5], ptr[ 4],
8656 ptr[ 7], ptr[ 6],
8657 ptr[ 9], ptr[ 8],
8658 ptr[11], ptr[10],
8659 ptr[13], ptr[12],
8660 ptr[15], ptr[14],
8661 ptr[17], ptr[16],
8662 ptr[19], ptr[18],
8663 ptr[21], ptr[20],
8664 ptr[23], ptr[22],
8665 ptr[25], ptr[24],
8666 ptr[27], ptr[26],
8667 ptr[29], ptr[28],
8668 ptr[31], ptr[30],
8669 ptr[33], ptr[32],
8670 ptr[35], ptr[34],
8671 ptr[37], ptr[36],
8672 ptr[39], ptr[38],
8673 ptr[41], ptr[30],
8674 ptr[43], ptr[42],
8675 ptr[45], ptr[44],
8676 ptr[47], ptr[46],
8677 ptr[49], ptr[48]
8678 );
8679
8680 out_buf[salt.keccak_mdlen * 2] = 0;
8681 }
8682 else if (hash_type == HASH_TYPE_RIPEMD160)
8683 {
8684 snprintf (out_buf, 255, "%08x%08x%08x%08x%08x",
8685 digest_buf[0],
8686 digest_buf[1],
8687 digest_buf[2],
8688 digest_buf[3],
8689 digest_buf[4]);
8690 }
8691 else if (hash_type == HASH_TYPE_WHIRLPOOL)
8692 {
8693 digest_buf[ 0] = digest_buf[ 0];
8694 digest_buf[ 1] = digest_buf[ 1];
8695 digest_buf[ 2] = digest_buf[ 2];
8696 digest_buf[ 3] = digest_buf[ 3];
8697 digest_buf[ 4] = digest_buf[ 4];
8698 digest_buf[ 5] = digest_buf[ 5];
8699 digest_buf[ 6] = digest_buf[ 6];
8700 digest_buf[ 7] = digest_buf[ 7];
8701 digest_buf[ 8] = digest_buf[ 8];
8702 digest_buf[ 9] = digest_buf[ 9];
8703 digest_buf[10] = digest_buf[10];
8704 digest_buf[11] = digest_buf[11];
8705 digest_buf[12] = digest_buf[12];
8706 digest_buf[13] = digest_buf[13];
8707 digest_buf[14] = digest_buf[14];
8708 digest_buf[15] = digest_buf[15];
8709
8710 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
8711 digest_buf[ 0],
8712 digest_buf[ 1],
8713 digest_buf[ 2],
8714 digest_buf[ 3],
8715 digest_buf[ 4],
8716 digest_buf[ 5],
8717 digest_buf[ 6],
8718 digest_buf[ 7],
8719 digest_buf[ 8],
8720 digest_buf[ 9],
8721 digest_buf[10],
8722 digest_buf[11],
8723 digest_buf[12],
8724 digest_buf[13],
8725 digest_buf[14],
8726 digest_buf[15]);
8727 }
8728 else if (hash_type == HASH_TYPE_GOST)
8729 {
8730 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x%08x%08x%08x",
8731 digest_buf[0],
8732 digest_buf[1],
8733 digest_buf[2],
8734 digest_buf[3],
8735 digest_buf[4],
8736 digest_buf[5],
8737 digest_buf[6],
8738 digest_buf[7]);
8739 }
8740 else if (hash_type == HASH_TYPE_MYSQL)
8741 {
8742 snprintf (out_buf, len-1, "%08x%08x",
8743 digest_buf[0],
8744 digest_buf[1]);
8745 }
8746 else if (hash_type == HASH_TYPE_LOTUS5)
8747 {
8748 snprintf (out_buf, len-1, "%08x%08x%08x%08x",
8749 digest_buf[0],
8750 digest_buf[1],
8751 digest_buf[2],
8752 digest_buf[3]);
8753 }
8754 else if (hash_type == HASH_TYPE_LOTUS6)
8755 {
8756 digest_buf[ 0] = byte_swap_32 (digest_buf[ 0]);
8757 digest_buf[ 1] = byte_swap_32 (digest_buf[ 1]);
8758 digest_buf[ 2] = byte_swap_32 (digest_buf[ 2]);
8759 digest_buf[ 3] = byte_swap_32 (digest_buf[ 3]);
8760
8761 char buf[16] = { 0 };
8762
8763 memcpy (buf + 0, salt.salt_buf, 5);
8764 memcpy (buf + 5, digest_buf, 9);
8765
8766 buf[3] -= -4;
8767
8768 base64_encode (int_to_lotus64, (const u8 *) buf, 14, (u8 *) tmp_buf);
8769
8770 tmp_buf[18] = salt.salt_buf_pc[7];
8771 tmp_buf[19] = 0;
8772
8773 snprintf (out_buf, len-1, "(G%s)", tmp_buf);
8774 }
8775 else if (hash_type == HASH_TYPE_LOTUS8)
8776 {
8777 char buf[52] = { 0 };
8778
8779 // salt
8780
8781 memcpy (buf + 0, salt.salt_buf, 16);
8782
8783 buf[3] -= -4;
8784
8785 // iteration
8786
8787 snprintf (buf + 16, 11, "%010i", salt.salt_iter + 1);
8788
8789 // chars
8790
8791 buf[26] = salt.salt_buf_pc[0];
8792 buf[27] = salt.salt_buf_pc[1];
8793
8794 // digest
8795
8796 memcpy (buf + 28, digest_buf, 8);
8797
8798 base64_encode (int_to_lotus64, (const u8 *) buf, 36, (u8 *) tmp_buf);
8799
8800 tmp_buf[49] = 0;
8801
8802 snprintf (out_buf, len-1, "(H%s)", tmp_buf);
8803 }
8804 else if (hash_type == HASH_TYPE_CRC32)
8805 {
8806 snprintf (out_buf, len-1, "%08x", byte_swap_32 (digest_buf[0]));
8807 }
8808 }
8809
8810 if (salt_type == SALT_TYPE_INTERN)
8811 {
8812 size_t pos = strlen (out_buf);
8813
8814 out_buf[pos] = data.separator;
8815
8816 char *ptr = (char *) salt.salt_buf;
8817
8818 memcpy (out_buf + pos + 1, ptr, salt.salt_len);
8819
8820 out_buf[pos + 1 + salt.salt_len] = 0;
8821 }
8822 }
8823
8824 void to_hccap_t (hccap_t *hccap, uint salt_pos, uint digest_pos)
8825 {
8826 memset (hccap, 0, sizeof (hccap_t));
8827
8828 salt_t *salt = &data.salts_buf[salt_pos];
8829
8830 memcpy (hccap->essid, salt->salt_buf, salt->salt_len);
8831
8832 wpa_t *wpas = (wpa_t *) data.esalts_buf;
8833 wpa_t *wpa = &wpas[salt_pos];
8834
8835 hccap->keyver = wpa->keyver;
8836
8837 hccap->eapol_size = wpa->eapol_size;
8838
8839 if (wpa->keyver != 1)
8840 {
8841 uint eapol_tmp[64] = { 0 };
8842
8843 for (uint i = 0; i < 64; i++)
8844 {
8845 eapol_tmp[i] = byte_swap_32 (wpa->eapol[i]);
8846 }
8847
8848 memcpy (hccap->eapol, eapol_tmp, wpa->eapol_size);
8849 }
8850 else
8851 {
8852 memcpy (hccap->eapol, wpa->eapol, wpa->eapol_size);
8853 }
8854
8855 memcpy (hccap->mac1, wpa->orig_mac1, 6);
8856 memcpy (hccap->mac2, wpa->orig_mac2, 6);
8857 memcpy (hccap->nonce1, wpa->orig_nonce1, 32);
8858 memcpy (hccap->nonce2, wpa->orig_nonce2, 32);
8859
8860 char *digests_buf_ptr = (char *) data.digests_buf;
8861
8862 uint dgst_size = data.dgst_size;
8863
8864 uint *digest_ptr = (uint *) (digests_buf_ptr + (data.salts_buf[salt_pos].digests_offset * dgst_size) + (digest_pos * dgst_size));
8865
8866 if (wpa->keyver != 1)
8867 {
8868 uint digest_tmp[4] = { 0 };
8869
8870 digest_tmp[0] = byte_swap_32 (digest_ptr[0]);
8871 digest_tmp[1] = byte_swap_32 (digest_ptr[1]);
8872 digest_tmp[2] = byte_swap_32 (digest_ptr[2]);
8873 digest_tmp[3] = byte_swap_32 (digest_ptr[3]);
8874
8875 memcpy (hccap->keymic, digest_tmp, 16);
8876 }
8877 else
8878 {
8879 memcpy (hccap->keymic, digest_ptr, 16);
8880 }
8881 }
8882
8883 void SuspendThreads ()
8884 {
8885 if (data.devices_status == STATUS_RUNNING)
8886 {
8887 hc_timer_set (&data.timer_paused);
8888
8889 data.devices_status = STATUS_PAUSED;
8890
8891 log_info ("Paused");
8892 }
8893 }
8894
8895 void ResumeThreads ()
8896 {
8897 if (data.devices_status == STATUS_PAUSED)
8898 {
8899 double ms_paused;
8900
8901 hc_timer_get (data.timer_paused, ms_paused);
8902
8903 data.ms_paused += ms_paused;
8904
8905 data.devices_status = STATUS_RUNNING;
8906
8907 log_info ("Resumed");
8908 }
8909 }
8910
8911 void bypass ()
8912 {
8913 if (data.devices_status != STATUS_RUNNING) return;
8914
8915 data.devices_status = STATUS_BYPASS;
8916
8917 log_info ("Next dictionary / mask in queue selected, bypassing current one");
8918 }
8919
8920 void stop_at_checkpoint ()
8921 {
8922 if (data.devices_status != STATUS_STOP_AT_CHECKPOINT)
8923 {
8924 if (data.devices_status != STATUS_RUNNING) return;
8925 }
8926
8927 // this feature only makes sense if --restore-disable was not specified
8928
8929 if (data.restore_disable == 1)
8930 {
8931 log_info ("WARNING: this feature is disabled when --restore-disable was specified");
8932
8933 return;
8934 }
8935
8936 // check if monitoring of Restore Point updates should be enabled or disabled
8937
8938 if (data.devices_status != STATUS_STOP_AT_CHECKPOINT)
8939 {
8940 data.devices_status = STATUS_STOP_AT_CHECKPOINT;
8941
8942 // save the current restore point value
8943
8944 data.checkpoint_cur_words = get_lowest_words_done ();
8945
8946 log_info ("Checkpoint enabled: will quit at next Restore Point update");
8947 }
8948 else
8949 {
8950 data.devices_status = STATUS_RUNNING;
8951
8952 // reset the global value for checkpoint checks
8953
8954 data.checkpoint_cur_words = 0;
8955
8956 log_info ("Checkpoint disabled: Restore Point updates will no longer be monitored");
8957 }
8958 }
8959
8960 void myabort ()
8961 {
8962 if (data.devices_status == STATUS_INIT) return;
8963 if (data.devices_status == STATUS_STARTING) return;
8964
8965 data.devices_status = STATUS_ABORTED;
8966 }
8967
8968 void myquit ()
8969 {
8970 if (data.devices_status == STATUS_INIT) return;
8971 if (data.devices_status == STATUS_STARTING) return;
8972
8973 data.devices_status = STATUS_QUIT;
8974 }
8975
8976 void load_kernel (const char *kernel_file, int num_devices, size_t *kernel_lengths, const u8 **kernel_sources)
8977 {
8978 FILE *fp = fopen (kernel_file, "rb");
8979
8980 if (fp != NULL)
8981 {
8982 struct stat st;
8983
8984 memset (&st, 0, sizeof (st));
8985
8986 stat (kernel_file, &st);
8987
8988 u8 *buf = (u8 *) mymalloc (st.st_size + 1);
8989
8990 size_t num_read = fread (buf, sizeof (u8), st.st_size, fp);
8991
8992 if (num_read != (size_t) st.st_size)
8993 {
8994 log_error ("ERROR: %s: %s", kernel_file, strerror (errno));
8995
8996 exit (-1);
8997 }
8998
8999 fclose (fp);
9000
9001 buf[st.st_size] = 0;
9002
9003 for (int i = 0; i < num_devices; i++)
9004 {
9005 kernel_lengths[i] = (size_t) st.st_size;
9006
9007 kernel_sources[i] = buf;
9008 }
9009 }
9010 else
9011 {
9012 log_error ("ERROR: %s: %s", kernel_file, strerror (errno));
9013
9014 exit (-1);
9015 }
9016
9017 return;
9018 }
9019
9020 void writeProgramBin (char *dst, u8 *binary, size_t binary_size)
9021 {
9022 if (binary_size > 0)
9023 {
9024 FILE *fp = fopen (dst, "wb");
9025
9026 lock_file (fp);
9027 fwrite (binary, sizeof (u8), binary_size, fp);
9028
9029 fflush (fp);
9030 fclose (fp);
9031 }
9032 }
9033
9034 /**
9035 * restore
9036 */
9037
9038 restore_data_t *init_restore (int argc, char **argv)
9039 {
9040 restore_data_t *rd = (restore_data_t *) mymalloc (sizeof (restore_data_t));
9041
9042 if (data.restore_disable == 0)
9043 {
9044 FILE *fp = fopen (data.eff_restore_file, "rb");
9045
9046 if (fp)
9047 {
9048 size_t nread = fread (rd, sizeof (restore_data_t), 1, fp);
9049
9050 if (nread != 1)
9051 {
9052 log_error ("ERROR: cannot read %s", data.eff_restore_file);
9053
9054 exit (-1);
9055 }
9056
9057 fclose (fp);
9058
9059 if (rd->pid)
9060 {
9061 char *pidbin = (char *) mymalloc (HCBUFSIZ);
9062
9063 int pidbin_len = -1;
9064
9065 #ifdef _POSIX
9066 snprintf (pidbin, HCBUFSIZ - 1, "/proc/%d/cmdline", rd->pid);
9067
9068 FILE *fd = fopen (pidbin, "rb");
9069
9070 if (fd)
9071 {
9072 pidbin_len = fread (pidbin, 1, HCBUFSIZ, fd);
9073
9074 pidbin[pidbin_len] = 0;
9075
9076 fclose (fd);
9077
9078 char *argv0_r = strrchr (argv[0], '/');
9079
9080 char *pidbin_r = strrchr (pidbin, '/');
9081
9082 if (argv0_r == NULL) argv0_r = argv[0];
9083
9084 if (pidbin_r == NULL) pidbin_r = pidbin;
9085
9086 if (strcmp (argv0_r, pidbin_r) == 0)
9087 {
9088 log_error ("ERROR: already an instance %s running on pid %d", pidbin, rd->pid);
9089
9090 exit (-1);
9091 }
9092 }
9093
9094 #elif _WIN
9095 HANDLE hProcess = OpenProcess (PROCESS_ALL_ACCESS, FALSE, rd->pid);
9096
9097 char *pidbin2 = (char *) mymalloc (HCBUFSIZ);
9098
9099 int pidbin2_len = -1;
9100
9101 pidbin_len = GetModuleFileName (NULL, pidbin, HCBUFSIZ);
9102 pidbin2_len = GetModuleFileNameEx (hProcess, NULL, pidbin2, HCBUFSIZ);
9103
9104 pidbin[pidbin_len] = 0;
9105 pidbin2[pidbin2_len] = 0;
9106
9107 if (pidbin2_len)
9108 {
9109 if (strcmp (pidbin, pidbin2) == 0)
9110 {
9111 log_error ("ERROR: already an instance %s running on pid %d", pidbin2, rd->pid);
9112
9113 exit (-1);
9114 }
9115 }
9116
9117 myfree (pidbin2);
9118
9119 #endif
9120
9121 myfree (pidbin);
9122 }
9123
9124 if (rd->version_bin < RESTORE_MIN)
9125 {
9126 log_error ("ERROR: cannot use outdated %s. Please remove it.", data.eff_restore_file);
9127
9128 exit (-1);
9129 }
9130 }
9131 }
9132
9133 memset (rd, 0, sizeof (restore_data_t));
9134
9135 rd->version_bin = VERSION_BIN;
9136
9137 #ifdef _POSIX
9138 rd->pid = getpid ();
9139 #elif _WIN
9140 rd->pid = GetCurrentProcessId ();
9141 #endif
9142
9143 if (getcwd (rd->cwd, 255) == NULL)
9144 {
9145 myfree (rd);
9146
9147 return (NULL);
9148 }
9149
9150 rd->argc = argc;
9151 rd->argv = argv;
9152
9153 return (rd);
9154 }
9155
9156 void read_restore (const char *eff_restore_file, restore_data_t *rd)
9157 {
9158 FILE *fp = fopen (eff_restore_file, "rb");
9159
9160 if (fp == NULL)
9161 {
9162 log_error ("ERROR: restore file '%s': %s", eff_restore_file, strerror (errno));
9163
9164 exit (-1);
9165 }
9166
9167 if (fread (rd, sizeof (restore_data_t), 1, fp) != 1)
9168 {
9169 log_error ("ERROR: cannot read %s", eff_restore_file);
9170
9171 exit (-1);
9172 }
9173
9174 rd->argv = (char **) mycalloc (rd->argc, sizeof (char *));
9175
9176 char *buf = (char *) mymalloc (HCBUFSIZ);
9177
9178 for (uint i = 0; i < rd->argc; i++)
9179 {
9180 if (fgets (buf, HCBUFSIZ - 1, fp) == NULL)
9181 {
9182 log_error ("ERROR: cannot read %s", eff_restore_file);
9183
9184 exit (-1);
9185 }
9186
9187 size_t len = strlen (buf);
9188
9189 if (len) buf[len - 1] = 0;
9190
9191 rd->argv[i] = mystrdup (buf);
9192 }
9193
9194 myfree (buf);
9195
9196 fclose (fp);
9197
9198 log_info ("INFO: Changing current working directory to the path found within the .restore file: '%s'", rd->cwd);
9199
9200 if (chdir (rd->cwd))
9201 {
9202 log_error ("ERROR: The directory '%s' does not exist. It is needed to restore (--restore) the session.\n"
9203 " You could either create this directory (or link it) or update the .restore file using e.g. the analyze_hc_restore.pl tool:\n"
9204 " https://github.com/philsmd/analyze_hc_restore\n"
9205 " The directory must be relative to (or contain) all files/folders mentioned within the command line.", rd->cwd);
9206
9207 exit (-1);
9208 }
9209 }
9210
9211 u64 get_lowest_words_done ()
9212 {
9213 u64 words_cur = -1;
9214
9215 for (uint device_id = 0; device_id < data.devices_cnt; device_id++)
9216 {
9217 hc_device_param_t *device_param = &data.devices_param[device_id];
9218
9219 if (device_param->skipped) continue;
9220
9221 const u64 words_done = device_param->words_done;
9222
9223 if (words_done < words_cur) words_cur = words_done;
9224 }
9225
9226 // It's possible that a device's workload isn't finished right after a restore-case.
9227 // In that case, this function would return 0 and overwrite the real restore point
9228 // There's also data.words_cur which is set to rd->words_cur but it changes while
9229 // the attack is running therefore we should stick to rd->words_cur.
9230 // Note that -s influences rd->words_cur we should keep a close look on that.
9231
9232 if (words_cur < data.rd->words_cur) words_cur = data.rd->words_cur;
9233
9234 return words_cur;
9235 }
9236
9237 void write_restore (const char *new_restore_file, restore_data_t *rd)
9238 {
9239 u64 words_cur = get_lowest_words_done ();
9240
9241 rd->words_cur = words_cur;
9242
9243 FILE *fp = fopen (new_restore_file, "wb");
9244
9245 if (fp == NULL)
9246 {
9247 log_error ("ERROR: %s: %s", new_restore_file, strerror (errno));
9248
9249 exit (-1);
9250 }
9251
9252 if (setvbuf (fp, NULL, _IONBF, 0))
9253 {
9254 log_error ("ERROR: setvbuf file '%s': %s", new_restore_file, strerror (errno));
9255
9256 exit (-1);
9257 }
9258
9259 fwrite (rd, sizeof (restore_data_t), 1, fp);
9260
9261 for (uint i = 0; i < rd->argc; i++)
9262 {
9263 fprintf (fp, "%s", rd->argv[i]);
9264 fputc ('\n', fp);
9265 }
9266
9267 fflush (fp);
9268
9269 fsync (fileno (fp));
9270
9271 fclose (fp);
9272 }
9273
9274 void cycle_restore ()
9275 {
9276 const char *eff_restore_file = data.eff_restore_file;
9277 const char *new_restore_file = data.new_restore_file;
9278
9279 restore_data_t *rd = data.rd;
9280
9281 write_restore (new_restore_file, rd);
9282
9283 struct stat st;
9284
9285 memset (&st, 0, sizeof(st));
9286
9287 if (stat (eff_restore_file, &st) == 0)
9288 {
9289 if (unlink (eff_restore_file))
9290 {
9291 log_info ("WARN: unlink file '%s': %s", eff_restore_file, strerror (errno));
9292 }
9293 }
9294
9295 if (rename (new_restore_file, eff_restore_file))
9296 {
9297 log_info ("WARN: rename file '%s' to '%s': %s", new_restore_file, eff_restore_file, strerror (errno));
9298 }
9299 }
9300
9301 void check_checkpoint ()
9302 {
9303 // if (data.restore_disable == 1) break; (this is already implied by previous checks)
9304
9305 u64 words_cur = get_lowest_words_done ();
9306
9307 if (words_cur != data.checkpoint_cur_words)
9308 {
9309 myabort ();
9310 }
9311 }
9312
9313 /**
9314 * tuning db
9315 */
9316
9317 void tuning_db_destroy (tuning_db_t *tuning_db)
9318 {
9319 int i;
9320
9321 for (i = 0; i < tuning_db->alias_cnt; i++)
9322 {
9323 tuning_db_alias_t *alias = &tuning_db->alias_buf[i];
9324
9325 myfree (alias->device_name);
9326 myfree (alias->alias_name);
9327 }
9328
9329 for (i = 0; i < tuning_db->entry_cnt; i++)
9330 {
9331 tuning_db_entry_t *entry = &tuning_db->entry_buf[i];
9332
9333 myfree (entry->device_name);
9334 }
9335
9336 myfree (tuning_db->alias_buf);
9337 myfree (tuning_db->entry_buf);
9338
9339 myfree (tuning_db);
9340 }
9341
9342 tuning_db_t *tuning_db_alloc (FILE *fp)
9343 {
9344 tuning_db_t *tuning_db = (tuning_db_t *) mymalloc (sizeof (tuning_db_t));
9345
9346 int num_lines = count_lines (fp);
9347
9348 // a bit over-allocated
9349
9350 tuning_db->alias_buf = (tuning_db_alias_t *) mycalloc (num_lines + 1, sizeof (tuning_db_alias_t));
9351 tuning_db->alias_cnt = 0;
9352
9353 tuning_db->entry_buf = (tuning_db_entry_t *) mycalloc (num_lines + 1, sizeof (tuning_db_entry_t));
9354 tuning_db->entry_cnt = 0;
9355
9356 return tuning_db;
9357 }
9358
9359 tuning_db_t *tuning_db_init (const char *tuning_db_file)
9360 {
9361 FILE *fp = fopen (tuning_db_file, "rb");
9362
9363 if (fp == NULL)
9364 {
9365 log_error ("%s: %s", tuning_db_file, strerror (errno));
9366
9367 exit (-1);
9368 }
9369
9370 tuning_db_t *tuning_db = tuning_db_alloc (fp);
9371
9372 rewind (fp);
9373
9374 int line_num = 0;
9375
9376 char *buf = (char *) mymalloc (HCBUFSIZ);
9377
9378 while (!feof (fp))
9379 {
9380 char *line_buf = fgets (buf, HCBUFSIZ - 1, fp);
9381
9382 if (line_buf == NULL) break;
9383
9384 line_num++;
9385
9386 const int line_len = in_superchop (line_buf);
9387
9388 if (line_len == 0) continue;
9389
9390 if (line_buf[0] == '#') continue;
9391
9392 // start processing
9393
9394 char *token_ptr[7] = { NULL };
9395
9396 int token_cnt = 0;
9397
9398 char *next = strtok (line_buf, "\t ");
9399
9400 token_ptr[token_cnt] = next;
9401
9402 token_cnt++;
9403
9404 while ((next = strtok (NULL, "\t ")) != NULL)
9405 {
9406 token_ptr[token_cnt] = next;
9407
9408 token_cnt++;
9409 }
9410
9411 if (token_cnt == 2)
9412 {
9413 char *device_name = token_ptr[0];
9414 char *alias_name = token_ptr[1];
9415
9416 tuning_db_alias_t *alias = &tuning_db->alias_buf[tuning_db->alias_cnt];
9417
9418 alias->device_name = mystrdup (device_name);
9419 alias->alias_name = mystrdup (alias_name);
9420
9421 tuning_db->alias_cnt++;
9422 }
9423 else if (token_cnt == 6)
9424 {
9425 if ((token_ptr[1][0] != '0') &&
9426 (token_ptr[1][0] != '1') &&
9427 (token_ptr[1][0] != '3') &&
9428 (token_ptr[1][0] != '*'))
9429 {
9430 log_info ("WARNING: Tuning-db: Invalid attack_mode '%c' in Line '%u'", token_ptr[1][0], line_num);
9431
9432 continue;
9433 }
9434
9435 if ((token_ptr[3][0] != '1') &&
9436 (token_ptr[3][0] != '2') &&
9437 (token_ptr[3][0] != '4') &&
9438 (token_ptr[3][0] != '8') &&
9439 (token_ptr[3][0] != 'N'))
9440 {
9441 log_info ("WARNING: Tuning-db: Invalid vector_width '%c' in Line '%u'", token_ptr[3][0], line_num);
9442
9443 continue;
9444 }
9445
9446 char *device_name = token_ptr[0];
9447
9448 int attack_mode = -1;
9449 int hash_type = -1;
9450 int vector_width = -1;
9451 int kernel_accel = -1;
9452 int kernel_loops = -1;
9453
9454 if (token_ptr[1][0] != '*') attack_mode = atoi (token_ptr[1]);
9455 if (token_ptr[2][0] != '*') hash_type = atoi (token_ptr[2]);
9456 if (token_ptr[3][0] != 'N') vector_width = atoi (token_ptr[3]);
9457
9458 if (token_ptr[4][0] != 'A')
9459 {
9460 kernel_accel = atoi (token_ptr[4]);
9461
9462 if ((kernel_accel < 1) || (kernel_accel > 1024))
9463 {
9464 log_info ("WARNING: Tuning-db: Invalid kernel_accel '%d' in Line '%u'", kernel_accel, line_num);
9465
9466 continue;
9467 }
9468 }
9469 else
9470 {
9471 kernel_accel = 0;
9472 }
9473
9474 if (token_ptr[5][0] != 'A')
9475 {
9476 kernel_loops = atoi (token_ptr[5]);
9477
9478 if ((kernel_loops < 1) || (kernel_loops > 1024))
9479 {
9480 log_info ("WARNING: Tuning-db: Invalid kernel_loops '%d' in Line '%u'", kernel_loops, line_num);
9481
9482 continue;
9483 }
9484 }
9485 else
9486 {
9487 kernel_loops = 0;
9488 }
9489
9490 tuning_db_entry_t *entry = &tuning_db->entry_buf[tuning_db->entry_cnt];
9491
9492 entry->device_name = mystrdup (device_name);
9493 entry->attack_mode = attack_mode;
9494 entry->hash_type = hash_type;
9495 entry->vector_width = vector_width;
9496 entry->kernel_accel = kernel_accel;
9497 entry->kernel_loops = kernel_loops;
9498
9499 tuning_db->entry_cnt++;
9500 }
9501 else
9502 {
9503 log_info ("WARNING: Tuning-db: Invalid number of token in Line '%u'", line_num);
9504
9505 continue;
9506 }
9507 }
9508
9509 myfree (buf);
9510
9511 fclose (fp);
9512
9513 // todo: print loaded 'cnt' message
9514
9515 // sort the database
9516
9517 qsort (tuning_db->alias_buf, tuning_db->alias_cnt, sizeof (tuning_db_alias_t), sort_by_tuning_db_alias);
9518 qsort (tuning_db->entry_buf, tuning_db->entry_cnt, sizeof (tuning_db_entry_t), sort_by_tuning_db_entry);
9519
9520 return tuning_db;
9521 }
9522
9523 tuning_db_entry_t *tuning_db_search (tuning_db_t *tuning_db, hc_device_param_t *device_param, int attack_mode, int hash_type)
9524 {
9525 static tuning_db_entry_t s;
9526
9527 // first we need to convert all spaces in the device_name to underscore
9528
9529 char *device_name_nospace = strdup (device_param->device_name);
9530
9531 int device_name_length = strlen (device_name_nospace);
9532
9533 int i;
9534
9535 for (i = 0; i < device_name_length; i++)
9536 {
9537 if (device_name_nospace[i] == ' ') device_name_nospace[i] = '_';
9538 }
9539
9540 // find out if there's an alias configured
9541
9542 tuning_db_alias_t a;
9543
9544 a.device_name = device_name_nospace;
9545
9546 tuning_db_alias_t *alias = bsearch (&a, tuning_db->alias_buf, tuning_db->alias_cnt, sizeof (tuning_db_alias_t), sort_by_tuning_db_alias);
9547
9548 char *alias_name = (alias == NULL) ? NULL : alias->alias_name;
9549
9550 // attack-mode 6 and 7 are attack-mode 1 basically
9551
9552 if (attack_mode == 6) attack_mode = 1;
9553 if (attack_mode == 7) attack_mode = 1;
9554
9555 // bsearch is not ideal but fast enough
9556
9557 s.device_name = device_name_nospace;
9558 s.attack_mode = attack_mode;
9559 s.hash_type = hash_type;
9560
9561 tuning_db_entry_t *entry = NULL;
9562
9563 // this will produce all 2^3 combinations required
9564
9565 for (i = 0; i < 8; i++)
9566 {
9567 s.device_name = (i & 1) ? "*" : device_name_nospace;
9568 s.attack_mode = (i & 2) ? -1 : attack_mode;
9569 s.hash_type = (i & 4) ? -1 : hash_type;
9570
9571 entry = bsearch (&s, tuning_db->entry_buf, tuning_db->entry_cnt, sizeof (tuning_db_entry_t), sort_by_tuning_db_entry);
9572
9573 if (entry != NULL) break;
9574
9575 // in non-wildcard mode do some additional checks:
9576
9577 if ((i & 1) == 0)
9578 {
9579 // in case we have an alias-name
9580
9581 if (alias_name != NULL)
9582 {
9583 s.device_name = alias_name;
9584
9585 entry = bsearch (&s, tuning_db->entry_buf, tuning_db->entry_cnt, sizeof (tuning_db_entry_t), sort_by_tuning_db_entry);
9586
9587 if (entry != NULL) break;
9588 }
9589
9590 // or by device type
9591
9592 if (device_param->device_type & CL_DEVICE_TYPE_CPU)
9593 {
9594 s.device_name = "DEVICE_TYPE_CPU";
9595 }
9596 else if (device_param->device_type & CL_DEVICE_TYPE_GPU)
9597 {
9598 s.device_name = "DEVICE_TYPE_GPU";
9599 }
9600 else if (device_param->device_type & CL_DEVICE_TYPE_ACCELERATOR)
9601 {
9602 s.device_name = "DEVICE_TYPE_ACCELERATOR";
9603 }
9604
9605 entry = bsearch (&s, tuning_db->entry_buf, tuning_db->entry_cnt, sizeof (tuning_db_entry_t), sort_by_tuning_db_entry);
9606
9607 if (entry != NULL) break;
9608 }
9609 }
9610
9611 // free converted device_name
9612
9613 myfree (device_name_nospace);
9614
9615 return entry;
9616 }
9617
9618 /**
9619 * parser
9620 */
9621
9622 uint parse_and_store_salt (char *out, char *in, uint salt_len)
9623 {
9624 u8 tmp[256] = { 0 };
9625
9626 if (salt_len > sizeof (tmp))
9627 {
9628 return UINT_MAX;
9629 }
9630
9631 memcpy (tmp, in, salt_len);
9632
9633 if (data.opts_type & OPTS_TYPE_ST_HEX)
9634 {
9635 if ((salt_len % 2) == 0)
9636 {
9637 u32 new_salt_len = salt_len / 2;
9638
9639 for (uint i = 0, j = 0; i < new_salt_len; i += 1, j += 2)
9640 {
9641 u8 p0 = tmp[j + 0];
9642 u8 p1 = tmp[j + 1];
9643
9644 tmp[i] = hex_convert (p1) << 0;
9645 tmp[i] |= hex_convert (p0) << 4;
9646 }
9647
9648 salt_len = new_salt_len;
9649 }
9650 else
9651 {
9652 return UINT_MAX;
9653 }
9654 }
9655 else if (data.opts_type & OPTS_TYPE_ST_BASE64)
9656 {
9657 salt_len = base64_decode (base64_to_int, (const u8 *) in, salt_len, (u8 *) tmp);
9658 }
9659
9660 memset (tmp + salt_len, 0, sizeof (tmp) - salt_len);
9661
9662 if (data.opts_type & OPTS_TYPE_ST_UNICODE)
9663 {
9664 if (salt_len < 20)
9665 {
9666 u32 *tmp_uint = (u32 *) tmp;
9667
9668 tmp_uint[9] = ((tmp_uint[4] >> 8) & 0x00FF0000) | ((tmp_uint[4] >> 16) & 0x000000FF);
9669 tmp_uint[8] = ((tmp_uint[4] << 8) & 0x00FF0000) | ((tmp_uint[4] >> 0) & 0x000000FF);
9670 tmp_uint[7] = ((tmp_uint[3] >> 8) & 0x00FF0000) | ((tmp_uint[3] >> 16) & 0x000000FF);
9671 tmp_uint[6] = ((tmp_uint[3] << 8) & 0x00FF0000) | ((tmp_uint[3] >> 0) & 0x000000FF);
9672 tmp_uint[5] = ((tmp_uint[2] >> 8) & 0x00FF0000) | ((tmp_uint[2] >> 16) & 0x000000FF);
9673 tmp_uint[4] = ((tmp_uint[2] << 8) & 0x00FF0000) | ((tmp_uint[2] >> 0) & 0x000000FF);
9674 tmp_uint[3] = ((tmp_uint[1] >> 8) & 0x00FF0000) | ((tmp_uint[1] >> 16) & 0x000000FF);
9675 tmp_uint[2] = ((tmp_uint[1] << 8) & 0x00FF0000) | ((tmp_uint[1] >> 0) & 0x000000FF);
9676 tmp_uint[1] = ((tmp_uint[0] >> 8) & 0x00FF0000) | ((tmp_uint[0] >> 16) & 0x000000FF);
9677 tmp_uint[0] = ((tmp_uint[0] << 8) & 0x00FF0000) | ((tmp_uint[0] >> 0) & 0x000000FF);
9678
9679 salt_len = salt_len * 2;
9680 }
9681 else
9682 {
9683 return UINT_MAX;
9684 }
9685 }
9686
9687 if (data.opts_type & OPTS_TYPE_ST_LOWER)
9688 {
9689 lowercase (tmp, salt_len);
9690 }
9691
9692 if (data.opts_type & OPTS_TYPE_ST_UPPER)
9693 {
9694 uppercase (tmp, salt_len);
9695 }
9696
9697 u32 len = salt_len;
9698
9699 if (data.opts_type & OPTS_TYPE_ST_ADD80)
9700 {
9701 tmp[len++] = 0x80;
9702 }
9703
9704 if (data.opts_type & OPTS_TYPE_ST_ADD01)
9705 {
9706 tmp[len++] = 0x01;
9707 }
9708
9709 if (data.opts_type & OPTS_TYPE_ST_GENERATE_LE)
9710 {
9711 u32 *tmp_uint = (uint *) tmp;
9712
9713 u32 max = len / 4;
9714
9715 if (len % 4) max++;
9716
9717 for (u32 i = 0; i < max; i++)
9718 {
9719 tmp_uint[i] = byte_swap_32 (tmp_uint[i]);
9720 }
9721
9722 // Important: we may need to increase the length of memcpy since
9723 // we don't want to "loose" some swapped bytes (could happen if
9724 // they do not perfectly fit in the 4-byte blocks)
9725 // Memcpy does always copy the bytes in the BE order, but since
9726 // we swapped them, some important bytes could be in positions
9727 // we normally skip with the original len
9728
9729 if (len % 4) len += 4 - (len % 4);
9730 }
9731
9732 memcpy (out, tmp, len);
9733
9734 return (salt_len);
9735 }
9736
9737 int bcrypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
9738 {
9739 if ((input_len < DISPLAY_LEN_MIN_3200) || (input_len > DISPLAY_LEN_MAX_3200)) return (PARSER_GLOBAL_LENGTH);
9740
9741 if ((memcmp (SIGNATURE_BCRYPT1, input_buf, 4)) && (memcmp (SIGNATURE_BCRYPT2, input_buf, 4)) && (memcmp (SIGNATURE_BCRYPT3, input_buf, 4))) return (PARSER_SIGNATURE_UNMATCHED);
9742
9743 u32 *digest = (u32 *) hash_buf->digest;
9744
9745 salt_t *salt = hash_buf->salt;
9746
9747 memcpy ((char *) salt->salt_sign, input_buf, 6);
9748
9749 char *iter_pos = input_buf + 4;
9750
9751 salt->salt_iter = 1 << atoi (iter_pos);
9752
9753 char *salt_pos = strchr (iter_pos, '$');
9754
9755 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
9756
9757 salt_pos++;
9758
9759 uint salt_len = 16;
9760
9761 salt->salt_len = salt_len;
9762
9763 u8 tmp_buf[100] = { 0 };
9764
9765 base64_decode (bf64_to_int, (const u8 *) salt_pos, 22, tmp_buf);
9766
9767 char *salt_buf_ptr = (char *) salt->salt_buf;
9768
9769 memcpy (salt_buf_ptr, tmp_buf, 16);
9770
9771 salt->salt_buf[0] = byte_swap_32 (salt->salt_buf[0]);
9772 salt->salt_buf[1] = byte_swap_32 (salt->salt_buf[1]);
9773 salt->salt_buf[2] = byte_swap_32 (salt->salt_buf[2]);
9774 salt->salt_buf[3] = byte_swap_32 (salt->salt_buf[3]);
9775
9776 char *hash_pos = salt_pos + 22;
9777
9778 memset (tmp_buf, 0, sizeof (tmp_buf));
9779
9780 base64_decode (bf64_to_int, (const u8 *) hash_pos, 31, tmp_buf);
9781
9782 memcpy (digest, tmp_buf, 24);
9783
9784 digest[0] = byte_swap_32 (digest[0]);
9785 digest[1] = byte_swap_32 (digest[1]);
9786 digest[2] = byte_swap_32 (digest[2]);
9787 digest[3] = byte_swap_32 (digest[3]);
9788 digest[4] = byte_swap_32 (digest[4]);
9789 digest[5] = byte_swap_32 (digest[5]);
9790
9791 digest[5] &= ~0xff; // its just 23 not 24 !
9792
9793 return (PARSER_OK);
9794 }
9795
9796 int cisco4_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
9797 {
9798 if ((input_len < DISPLAY_LEN_MIN_5700) || (input_len > DISPLAY_LEN_MAX_5700)) return (PARSER_GLOBAL_LENGTH);
9799
9800 u32 *digest = (u32 *) hash_buf->digest;
9801
9802 u8 tmp_buf[100] = { 0 };
9803
9804 base64_decode (itoa64_to_int, (const u8 *) input_buf, 43, tmp_buf);
9805
9806 memcpy (digest, tmp_buf, 32);
9807
9808 digest[0] = byte_swap_32 (digest[0]);
9809 digest[1] = byte_swap_32 (digest[1]);
9810 digest[2] = byte_swap_32 (digest[2]);
9811 digest[3] = byte_swap_32 (digest[3]);
9812 digest[4] = byte_swap_32 (digest[4]);
9813 digest[5] = byte_swap_32 (digest[5]);
9814 digest[6] = byte_swap_32 (digest[6]);
9815 digest[7] = byte_swap_32 (digest[7]);
9816
9817 digest[0] -= SHA256M_A;
9818 digest[1] -= SHA256M_B;
9819 digest[2] -= SHA256M_C;
9820 digest[3] -= SHA256M_D;
9821 digest[4] -= SHA256M_E;
9822 digest[5] -= SHA256M_F;
9823 digest[6] -= SHA256M_G;
9824 digest[7] -= SHA256M_H;
9825
9826 return (PARSER_OK);
9827 }
9828
9829 int lm_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
9830 {
9831 if ((input_len < DISPLAY_LEN_MIN_3000) || (input_len > DISPLAY_LEN_MAX_3000)) return (PARSER_GLOBAL_LENGTH);
9832
9833 u32 *digest = (u32 *) hash_buf->digest;
9834
9835 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
9836 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
9837
9838 digest[0] = byte_swap_32 (digest[0]);
9839 digest[1] = byte_swap_32 (digest[1]);
9840
9841 uint tt;
9842
9843 IP (digest[0], digest[1], tt);
9844
9845 digest[0] = digest[0];
9846 digest[1] = digest[1];
9847 digest[2] = 0;
9848 digest[3] = 0;
9849
9850 return (PARSER_OK);
9851 }
9852
9853 int arubaos_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
9854 {
9855 if ((input_len < DISPLAY_LEN_MIN_125) || (input_len > DISPLAY_LEN_MAX_125)) return (PARSER_GLOBAL_LENGTH);
9856
9857 if ((input_buf[8] != '0') || (input_buf[9] != '1')) return (PARSER_SIGNATURE_UNMATCHED);
9858
9859 u32 *digest = (u32 *) hash_buf->digest;
9860
9861 salt_t *salt = hash_buf->salt;
9862
9863 char *hash_pos = input_buf + 10;
9864
9865 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
9866 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
9867 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
9868 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
9869 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
9870
9871 digest[0] -= SHA1M_A;
9872 digest[1] -= SHA1M_B;
9873 digest[2] -= SHA1M_C;
9874 digest[3] -= SHA1M_D;
9875 digest[4] -= SHA1M_E;
9876
9877 uint salt_len = 10;
9878
9879 char *salt_buf_ptr = (char *) salt->salt_buf;
9880
9881 salt_len = parse_and_store_salt (salt_buf_ptr, input_buf, salt_len);
9882
9883 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
9884
9885 salt->salt_len = salt_len;
9886
9887 return (PARSER_OK);
9888 }
9889
9890 int osx1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
9891 {
9892 if ((input_len < DISPLAY_LEN_MIN_122) || (input_len > DISPLAY_LEN_MAX_122)) return (PARSER_GLOBAL_LENGTH);
9893
9894 u32 *digest = (u32 *) hash_buf->digest;
9895
9896 salt_t *salt = hash_buf->salt;
9897
9898 char *hash_pos = input_buf + 8;
9899
9900 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
9901 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
9902 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
9903 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
9904 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
9905
9906 digest[0] -= SHA1M_A;
9907 digest[1] -= SHA1M_B;
9908 digest[2] -= SHA1M_C;
9909 digest[3] -= SHA1M_D;
9910 digest[4] -= SHA1M_E;
9911
9912 uint salt_len = 8;
9913
9914 char *salt_buf_ptr = (char *) salt->salt_buf;
9915
9916 salt_len = parse_and_store_salt (salt_buf_ptr, input_buf, salt_len);
9917
9918 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
9919
9920 salt->salt_len = salt_len;
9921
9922 return (PARSER_OK);
9923 }
9924
9925 int osx512_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
9926 {
9927 if ((input_len < DISPLAY_LEN_MIN_1722) || (input_len > DISPLAY_LEN_MAX_1722)) return (PARSER_GLOBAL_LENGTH);
9928
9929 u64 *digest = (u64 *) hash_buf->digest;
9930
9931 salt_t *salt = hash_buf->salt;
9932
9933 char *hash_pos = input_buf + 8;
9934
9935 digest[0] = hex_to_u64 ((const u8 *) &hash_pos[ 0]);
9936 digest[1] = hex_to_u64 ((const u8 *) &hash_pos[ 16]);
9937 digest[2] = hex_to_u64 ((const u8 *) &hash_pos[ 32]);
9938 digest[3] = hex_to_u64 ((const u8 *) &hash_pos[ 48]);
9939 digest[4] = hex_to_u64 ((const u8 *) &hash_pos[ 64]);
9940 digest[5] = hex_to_u64 ((const u8 *) &hash_pos[ 80]);
9941 digest[6] = hex_to_u64 ((const u8 *) &hash_pos[ 96]);
9942 digest[7] = hex_to_u64 ((const u8 *) &hash_pos[112]);
9943
9944 digest[0] -= SHA512M_A;
9945 digest[1] -= SHA512M_B;
9946 digest[2] -= SHA512M_C;
9947 digest[3] -= SHA512M_D;
9948 digest[4] -= SHA512M_E;
9949 digest[5] -= SHA512M_F;
9950 digest[6] -= SHA512M_G;
9951 digest[7] -= SHA512M_H;
9952
9953 uint salt_len = 8;
9954
9955 char *salt_buf_ptr = (char *) salt->salt_buf;
9956
9957 salt_len = parse_and_store_salt (salt_buf_ptr, input_buf, salt_len);
9958
9959 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
9960
9961 salt->salt_len = salt_len;
9962
9963 return (PARSER_OK);
9964 }
9965
9966 int osc_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
9967 {
9968 if (data.opts_type & OPTS_TYPE_ST_HEX)
9969 {
9970 if ((input_len < DISPLAY_LEN_MIN_21H) || (input_len > DISPLAY_LEN_MAX_21H)) return (PARSER_GLOBAL_LENGTH);
9971 }
9972 else
9973 {
9974 if ((input_len < DISPLAY_LEN_MIN_21) || (input_len > DISPLAY_LEN_MAX_21)) return (PARSER_GLOBAL_LENGTH);
9975 }
9976
9977 u32 *digest = (u32 *) hash_buf->digest;
9978
9979 salt_t *salt = hash_buf->salt;
9980
9981 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
9982 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
9983 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
9984 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
9985
9986 digest[0] = byte_swap_32 (digest[0]);
9987 digest[1] = byte_swap_32 (digest[1]);
9988 digest[2] = byte_swap_32 (digest[2]);
9989 digest[3] = byte_swap_32 (digest[3]);
9990
9991 digest[0] -= MD5M_A;
9992 digest[1] -= MD5M_B;
9993 digest[2] -= MD5M_C;
9994 digest[3] -= MD5M_D;
9995
9996 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
9997
9998 uint salt_len = input_len - 32 - 1;
9999
10000 char *salt_buf = input_buf + 32 + 1;
10001
10002 char *salt_buf_ptr = (char *) salt->salt_buf;
10003
10004 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
10005
10006 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
10007
10008 salt->salt_len = salt_len;
10009
10010 return (PARSER_OK);
10011 }
10012
10013 int netscreen_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10014 {
10015 if (data.opts_type & OPTS_TYPE_ST_HEX)
10016 {
10017 if ((input_len < DISPLAY_LEN_MIN_22H) || (input_len > DISPLAY_LEN_MAX_22H)) return (PARSER_GLOBAL_LENGTH);
10018 }
10019 else
10020 {
10021 if ((input_len < DISPLAY_LEN_MIN_22) || (input_len > DISPLAY_LEN_MAX_22)) return (PARSER_GLOBAL_LENGTH);
10022 }
10023
10024 // unscramble
10025
10026 char clean_input_buf[32] = { 0 };
10027
10028 char sig[6] = { 'n', 'r', 'c', 's', 't', 'n' };
10029 int pos[6] = { 0, 6, 12, 17, 23, 29 };
10030
10031 for (int i = 0, j = 0, k = 0; i < 30; i++)
10032 {
10033 if (i == pos[j])
10034 {
10035 if (sig[j] != input_buf[i]) return (PARSER_SIGNATURE_UNMATCHED);
10036
10037 j++;
10038 }
10039 else
10040 {
10041 clean_input_buf[k] = input_buf[i];
10042
10043 k++;
10044 }
10045 }
10046
10047 // base64 decode
10048
10049 u32 *digest = (u32 *) hash_buf->digest;
10050
10051 salt_t *salt = hash_buf->salt;
10052
10053 u32 a, b, c, d, e, f;
10054
10055 a = base64_to_int (clean_input_buf[ 0] & 0x7f);
10056 b = base64_to_int (clean_input_buf[ 1] & 0x7f);
10057 c = base64_to_int (clean_input_buf[ 2] & 0x7f);
10058 d = base64_to_int (clean_input_buf[ 3] & 0x7f);
10059 e = base64_to_int (clean_input_buf[ 4] & 0x7f);
10060 f = base64_to_int (clean_input_buf[ 5] & 0x7f);
10061
10062 digest[0] = (((a << 12) | (b << 6) | (c)) << 16)
10063 | (((d << 12) | (e << 6) | (f)) << 0);
10064
10065 a = base64_to_int (clean_input_buf[ 6] & 0x7f);
10066 b = base64_to_int (clean_input_buf[ 7] & 0x7f);
10067 c = base64_to_int (clean_input_buf[ 8] & 0x7f);
10068 d = base64_to_int (clean_input_buf[ 9] & 0x7f);
10069 e = base64_to_int (clean_input_buf[10] & 0x7f);
10070 f = base64_to_int (clean_input_buf[11] & 0x7f);
10071
10072 digest[1] = (((a << 12) | (b << 6) | (c)) << 16)
10073 | (((d << 12) | (e << 6) | (f)) << 0);
10074
10075 a = base64_to_int (clean_input_buf[12] & 0x7f);
10076 b = base64_to_int (clean_input_buf[13] & 0x7f);
10077 c = base64_to_int (clean_input_buf[14] & 0x7f);
10078 d = base64_to_int (clean_input_buf[15] & 0x7f);
10079 e = base64_to_int (clean_input_buf[16] & 0x7f);
10080 f = base64_to_int (clean_input_buf[17] & 0x7f);
10081
10082 digest[2] = (((a << 12) | (b << 6) | (c)) << 16)
10083 | (((d << 12) | (e << 6) | (f)) << 0);
10084
10085 a = base64_to_int (clean_input_buf[18] & 0x7f);
10086 b = base64_to_int (clean_input_buf[19] & 0x7f);
10087 c = base64_to_int (clean_input_buf[20] & 0x7f);
10088 d = base64_to_int (clean_input_buf[21] & 0x7f);
10089 e = base64_to_int (clean_input_buf[22] & 0x7f);
10090 f = base64_to_int (clean_input_buf[23] & 0x7f);
10091
10092 digest[3] = (((a << 12) | (b << 6) | (c)) << 16)
10093 | (((d << 12) | (e << 6) | (f)) << 0);
10094
10095 digest[0] = byte_swap_32 (digest[0]);
10096 digest[1] = byte_swap_32 (digest[1]);
10097 digest[2] = byte_swap_32 (digest[2]);
10098 digest[3] = byte_swap_32 (digest[3]);
10099
10100 digest[0] -= MD5M_A;
10101 digest[1] -= MD5M_B;
10102 digest[2] -= MD5M_C;
10103 digest[3] -= MD5M_D;
10104
10105 if (input_buf[30] != ':') return (PARSER_SEPARATOR_UNMATCHED);
10106
10107 uint salt_len = input_len - 30 - 1;
10108
10109 char *salt_buf = input_buf + 30 + 1;
10110
10111 char *salt_buf_ptr = (char *) salt->salt_buf;
10112
10113 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
10114
10115 // max. salt length: 55 (max for MD5) - 22 (":Administration Tools:") - 1 (0x80) = 32
10116 // 32 - 4 bytes (to fit w0lr for all attack modes) = 28
10117
10118 if (salt_len > 28) return (PARSER_SALT_LENGTH);
10119
10120 salt->salt_len = salt_len;
10121
10122 memcpy (salt_buf_ptr + salt_len, ":Administration Tools:", 22);
10123
10124 salt->salt_len += 22;
10125
10126 return (PARSER_OK);
10127 }
10128
10129 int smf_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10130 {
10131 if (data.opts_type & OPTS_TYPE_ST_HEX)
10132 {
10133 if ((input_len < DISPLAY_LEN_MIN_121H) || (input_len > DISPLAY_LEN_MAX_121H)) return (PARSER_GLOBAL_LENGTH);
10134 }
10135 else
10136 {
10137 if ((input_len < DISPLAY_LEN_MIN_121) || (input_len > DISPLAY_LEN_MAX_121)) return (PARSER_GLOBAL_LENGTH);
10138 }
10139
10140 u32 *digest = (u32 *) hash_buf->digest;
10141
10142 salt_t *salt = hash_buf->salt;
10143
10144 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
10145 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
10146 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
10147 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
10148 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
10149
10150 digest[0] -= SHA1M_A;
10151 digest[1] -= SHA1M_B;
10152 digest[2] -= SHA1M_C;
10153 digest[3] -= SHA1M_D;
10154 digest[4] -= SHA1M_E;
10155
10156 if (input_buf[40] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
10157
10158 uint salt_len = input_len - 40 - 1;
10159
10160 char *salt_buf = input_buf + 40 + 1;
10161
10162 char *salt_buf_ptr = (char *) salt->salt_buf;
10163
10164 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
10165
10166 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
10167
10168 salt->salt_len = salt_len;
10169
10170 return (PARSER_OK);
10171 }
10172
10173 int dcc2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10174 {
10175 if (data.opts_type & OPTS_TYPE_ST_HEX)
10176 {
10177 if ((input_len < DISPLAY_LEN_MIN_2100H) || (input_len > DISPLAY_LEN_MAX_2100H)) return (PARSER_GLOBAL_LENGTH);
10178 }
10179 else
10180 {
10181 if ((input_len < DISPLAY_LEN_MIN_2100) || (input_len > DISPLAY_LEN_MAX_2100)) return (PARSER_GLOBAL_LENGTH);
10182 }
10183
10184 if (memcmp (SIGNATURE_DCC2, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
10185
10186 char *iter_pos = input_buf + 6;
10187
10188 salt_t *salt = hash_buf->salt;
10189
10190 uint iter = atoi (iter_pos);
10191
10192 if (iter < 1)
10193 {
10194 iter = ROUNDS_DCC2;
10195 }
10196
10197 salt->salt_iter = iter - 1;
10198
10199 char *salt_pos = strchr (iter_pos, '#');
10200
10201 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
10202
10203 salt_pos++;
10204
10205 char *digest_pos = strchr (salt_pos, '#');
10206
10207 if (digest_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
10208
10209 digest_pos++;
10210
10211 uint salt_len = digest_pos - salt_pos - 1;
10212
10213 u32 *digest = (u32 *) hash_buf->digest;
10214
10215 digest[0] = hex_to_u32 ((const u8 *) &digest_pos[ 0]);
10216 digest[1] = hex_to_u32 ((const u8 *) &digest_pos[ 8]);
10217 digest[2] = hex_to_u32 ((const u8 *) &digest_pos[16]);
10218 digest[3] = hex_to_u32 ((const u8 *) &digest_pos[24]);
10219
10220 char *salt_buf_ptr = (char *) salt->salt_buf;
10221
10222 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
10223
10224 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
10225
10226 salt->salt_len = salt_len;
10227
10228 return (PARSER_OK);
10229 }
10230
10231 int wpa_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10232 {
10233 u32 *digest = (u32 *) hash_buf->digest;
10234
10235 salt_t *salt = hash_buf->salt;
10236
10237 wpa_t *wpa = (wpa_t *) hash_buf->esalt;
10238
10239 hccap_t in;
10240
10241 memcpy (&in, input_buf, input_len);
10242
10243 if (in.eapol_size < 1 || in.eapol_size > 255) return (PARSER_HCCAP_EAPOL_SIZE);
10244
10245 memcpy (digest, in.keymic, 16);
10246
10247 /*
10248 http://www.one-net.eu/jsw/j_sec/m_ptype.html
10249 The phrase "Pairwise key expansion"
10250 Access Point Address (referred to as Authenticator Address AA)
10251 Supplicant Address (referred to as Supplicant Address SA)
10252 Access Point Nonce (referred to as Authenticator Anonce)
10253 Wireless Device Nonce (referred to as Supplicant Nonce Snonce)
10254 */
10255
10256 uint salt_len = strlen (in.essid);
10257
10258 if (salt_len > 36)
10259 {
10260 log_info ("WARNING: the length of the ESSID is too long. The hccap file may be invalid or corrupted");
10261
10262 return (PARSER_SALT_LENGTH);
10263 }
10264
10265 memcpy (salt->salt_buf, in.essid, salt_len);
10266
10267 salt->salt_len = salt_len;
10268
10269 salt->salt_iter = ROUNDS_WPA2 - 1;
10270
10271 unsigned char *pke_ptr = (unsigned char *) wpa->pke;
10272
10273 memcpy (pke_ptr, "Pairwise key expansion", 23);
10274
10275 if (memcmp (in.mac1, in.mac2, 6) < 0)
10276 {
10277 memcpy (pke_ptr + 23, in.mac1, 6);
10278 memcpy (pke_ptr + 29, in.mac2, 6);
10279 }
10280 else
10281 {
10282 memcpy (pke_ptr + 23, in.mac2, 6);
10283 memcpy (pke_ptr + 29, in.mac1, 6);
10284 }
10285
10286 if (memcmp (in.nonce1, in.nonce2, 32) < 0)
10287 {
10288 memcpy (pke_ptr + 35, in.nonce1, 32);
10289 memcpy (pke_ptr + 67, in.nonce2, 32);
10290 }
10291 else
10292 {
10293 memcpy (pke_ptr + 35, in.nonce2, 32);
10294 memcpy (pke_ptr + 67, in.nonce1, 32);
10295 }
10296
10297 for (int i = 0; i < 25; i++)
10298 {
10299 wpa->pke[i] = byte_swap_32 (wpa->pke[i]);
10300 }
10301
10302 memcpy (wpa->orig_mac1, in.mac1, 6);
10303 memcpy (wpa->orig_mac2, in.mac2, 6);
10304 memcpy (wpa->orig_nonce1, in.nonce1, 32);
10305 memcpy (wpa->orig_nonce2, in.nonce2, 32);
10306
10307 wpa->keyver = in.keyver;
10308
10309 if (wpa->keyver > 255)
10310 {
10311 log_info ("ATTENTION!");
10312 log_info (" The WPA/WPA2 key version in your .hccap file is invalid!");
10313 log_info (" This could be due to a recent aircrack-ng bug.");
10314 log_info (" The key version was automatically reset to a reasonable value.");
10315 log_info ("");
10316
10317 wpa->keyver &= 0xff;
10318 }
10319
10320 wpa->eapol_size = in.eapol_size;
10321
10322 unsigned char *eapol_ptr = (unsigned char *) wpa->eapol;
10323
10324 memcpy (eapol_ptr, in.eapol, wpa->eapol_size);
10325
10326 memset (eapol_ptr + wpa->eapol_size, 0, 256 - wpa->eapol_size);
10327
10328 eapol_ptr[wpa->eapol_size] = (unsigned char) 0x80;
10329
10330 if (wpa->keyver == 1)
10331 {
10332 // nothing to do
10333 }
10334 else
10335 {
10336 digest[0] = byte_swap_32 (digest[0]);
10337 digest[1] = byte_swap_32 (digest[1]);
10338 digest[2] = byte_swap_32 (digest[2]);
10339 digest[3] = byte_swap_32 (digest[3]);
10340
10341 for (int i = 0; i < 64; i++)
10342 {
10343 wpa->eapol[i] = byte_swap_32 (wpa->eapol[i]);
10344 }
10345 }
10346
10347 uint32_t *p0 = (uint32_t *) in.essid;
10348 uint32_t c0 = 0;
10349 uint32_t c1 = 0;
10350
10351 for (uint i = 0; i < sizeof (in.essid) / sizeof (uint32_t); i++) c0 ^= *p0++;
10352 for (uint i = 0; i < sizeof (wpa->pke) / sizeof (wpa->pke[0]); i++) c1 ^= wpa->pke[i];
10353
10354 salt->salt_buf[10] = c0;
10355 salt->salt_buf[11] = c1;
10356
10357 return (PARSER_OK);
10358 }
10359
10360 int psafe2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10361 {
10362 u32 *digest = (u32 *) hash_buf->digest;
10363
10364 salt_t *salt = hash_buf->salt;
10365
10366 if (input_len == 0)
10367 {
10368 log_error ("Password Safe v2 container not specified");
10369
10370 exit (-1);
10371 }
10372
10373 FILE *fp = fopen (input_buf, "rb");
10374
10375 if (fp == NULL)
10376 {
10377 log_error ("%s: %s", input_buf, strerror (errno));
10378
10379 exit (-1);
10380 }
10381
10382 psafe2_hdr buf;
10383
10384 memset (&buf, 0, sizeof (psafe2_hdr));
10385
10386 int n = fread (&buf, sizeof (psafe2_hdr), 1, fp);
10387
10388 fclose (fp);
10389
10390 if (n != 1) return (PARSER_PSAFE2_FILE_SIZE);
10391
10392 salt->salt_buf[0] = buf.random[0];
10393 salt->salt_buf[1] = buf.random[1];
10394
10395 salt->salt_len = 8;
10396 salt->salt_iter = 1000;
10397
10398 digest[0] = byte_swap_32 (buf.hash[0]);
10399 digest[1] = byte_swap_32 (buf.hash[1]);
10400 digest[2] = byte_swap_32 (buf.hash[2]);
10401 digest[3] = byte_swap_32 (buf.hash[3]);
10402 digest[4] = byte_swap_32 (buf.hash[4]);
10403
10404 return (PARSER_OK);
10405 }
10406
10407 int psafe3_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10408 {
10409 u32 *digest = (u32 *) hash_buf->digest;
10410
10411 salt_t *salt = hash_buf->salt;
10412
10413 if (input_len == 0)
10414 {
10415 log_error (".psafe3 not specified");
10416
10417 exit (-1);
10418 }
10419
10420 FILE *fp = fopen (input_buf, "rb");
10421
10422 if (fp == NULL)
10423 {
10424 log_error ("%s: %s", input_buf, strerror (errno));
10425
10426 exit (-1);
10427 }
10428
10429 psafe3_t in;
10430
10431 int n = fread (&in, sizeof (psafe3_t), 1, fp);
10432
10433 fclose (fp);
10434
10435 data.hashfile = input_buf; // we will need this in case it gets cracked
10436
10437 if (memcmp (SIGNATURE_PSAFE3, in.signature, 4)) return (PARSER_SIGNATURE_UNMATCHED);
10438
10439 if (n != 1) return (PARSER_PSAFE3_FILE_SIZE);
10440
10441 salt->salt_iter = in.iterations + 1;
10442
10443 salt->salt_buf[0] = in.salt_buf[0];
10444 salt->salt_buf[1] = in.salt_buf[1];
10445 salt->salt_buf[2] = in.salt_buf[2];
10446 salt->salt_buf[3] = in.salt_buf[3];
10447 salt->salt_buf[4] = in.salt_buf[4];
10448 salt->salt_buf[5] = in.salt_buf[5];
10449 salt->salt_buf[6] = in.salt_buf[6];
10450 salt->salt_buf[7] = in.salt_buf[7];
10451
10452 salt->salt_len = 32;
10453
10454 digest[0] = in.hash_buf[0];
10455 digest[1] = in.hash_buf[1];
10456 digest[2] = in.hash_buf[2];
10457 digest[3] = in.hash_buf[3];
10458 digest[4] = in.hash_buf[4];
10459 digest[5] = in.hash_buf[5];
10460 digest[6] = in.hash_buf[6];
10461 digest[7] = in.hash_buf[7];
10462
10463 digest[0] = byte_swap_32 (digest[0]);
10464 digest[1] = byte_swap_32 (digest[1]);
10465 digest[2] = byte_swap_32 (digest[2]);
10466 digest[3] = byte_swap_32 (digest[3]);
10467 digest[4] = byte_swap_32 (digest[4]);
10468 digest[5] = byte_swap_32 (digest[5]);
10469 digest[6] = byte_swap_32 (digest[6]);
10470 digest[7] = byte_swap_32 (digest[7]);
10471
10472 return (PARSER_OK);
10473 }
10474
10475 int phpass_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10476 {
10477 if ((input_len < DISPLAY_LEN_MIN_400) || (input_len > DISPLAY_LEN_MAX_400)) return (PARSER_GLOBAL_LENGTH);
10478
10479 if ((memcmp (SIGNATURE_PHPASS1, input_buf, 3)) && (memcmp (SIGNATURE_PHPASS2, input_buf, 3))) return (PARSER_SIGNATURE_UNMATCHED);
10480
10481 u32 *digest = (u32 *) hash_buf->digest;
10482
10483 salt_t *salt = hash_buf->salt;
10484
10485 char *iter_pos = input_buf + 3;
10486
10487 uint salt_iter = 1 << itoa64_to_int (iter_pos[0]);
10488
10489 if (salt_iter > 0x80000000) return (PARSER_SALT_ITERATION);
10490
10491 memcpy ((char *) salt->salt_sign, input_buf, 4);
10492
10493 salt->salt_iter = salt_iter;
10494
10495 char *salt_pos = iter_pos + 1;
10496
10497 uint salt_len = 8;
10498
10499 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
10500
10501 salt->salt_len = salt_len;
10502
10503 char *hash_pos = salt_pos + salt_len;
10504
10505 phpass_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
10506
10507 return (PARSER_OK);
10508 }
10509
10510 int md5crypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10511 {
10512 if (input_len < DISPLAY_LEN_MIN_500) return (PARSER_GLOBAL_LENGTH);
10513
10514 if (memcmp (SIGNATURE_MD5CRYPT, input_buf, 3)) return (PARSER_SIGNATURE_UNMATCHED);
10515
10516 u32 *digest = (u32 *) hash_buf->digest;
10517
10518 salt_t *salt = hash_buf->salt;
10519
10520 char *salt_pos = input_buf + 3;
10521
10522 uint iterations_len = 0;
10523
10524 if (memcmp (salt_pos, "rounds=", 7) == 0)
10525 {
10526 salt_pos += 7;
10527
10528 for (iterations_len = 0; salt_pos[0] >= '0' && salt_pos[0] <= '9' && iterations_len < 7; iterations_len++, salt_pos += 1) continue;
10529
10530 if (iterations_len == 0 ) return (PARSER_SALT_ITERATION);
10531 if (salt_pos[0] != '$') return (PARSER_SIGNATURE_UNMATCHED);
10532
10533 salt_pos[0] = 0x0;
10534
10535 salt->salt_iter = atoi (salt_pos - iterations_len);
10536
10537 salt_pos += 1;
10538
10539 iterations_len += 8;
10540 }
10541 else
10542 {
10543 salt->salt_iter = ROUNDS_MD5CRYPT;
10544 }
10545
10546 if (input_len > (DISPLAY_LEN_MAX_500 + iterations_len)) return (PARSER_GLOBAL_LENGTH);
10547
10548 char *hash_pos = strchr (salt_pos, '$');
10549
10550 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
10551
10552 uint salt_len = hash_pos - salt_pos;
10553
10554 if (salt_len > 8) return (PARSER_SALT_LENGTH);
10555
10556 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
10557
10558 salt->salt_len = salt_len;
10559
10560 hash_pos++;
10561
10562 uint hash_len = input_len - 3 - iterations_len - salt_len - 1;
10563
10564 if (hash_len != 22) return (PARSER_HASH_LENGTH);
10565
10566 md5crypt_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
10567
10568 return (PARSER_OK);
10569 }
10570
10571 int md5apr1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10572 {
10573 if (memcmp (SIGNATURE_MD5APR1, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
10574
10575 u32 *digest = (u32 *) hash_buf->digest;
10576
10577 salt_t *salt = hash_buf->salt;
10578
10579 char *salt_pos = input_buf + 6;
10580
10581 uint iterations_len = 0;
10582
10583 if (memcmp (salt_pos, "rounds=", 7) == 0)
10584 {
10585 salt_pos += 7;
10586
10587 for (iterations_len = 0; salt_pos[0] >= '0' && salt_pos[0] <= '9' && iterations_len < 7; iterations_len++, salt_pos += 1) continue;
10588
10589 if (iterations_len == 0 ) return (PARSER_SALT_ITERATION);
10590 if (salt_pos[0] != '$') return (PARSER_SIGNATURE_UNMATCHED);
10591
10592 salt_pos[0] = 0x0;
10593
10594 salt->salt_iter = atoi (salt_pos - iterations_len);
10595
10596 salt_pos += 1;
10597
10598 iterations_len += 8;
10599 }
10600 else
10601 {
10602 salt->salt_iter = ROUNDS_MD5CRYPT;
10603 }
10604
10605 if ((input_len < DISPLAY_LEN_MIN_1600) || (input_len > DISPLAY_LEN_MAX_1600 + iterations_len)) return (PARSER_GLOBAL_LENGTH);
10606
10607 char *hash_pos = strchr (salt_pos, '$');
10608
10609 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
10610
10611 uint salt_len = hash_pos - salt_pos;
10612
10613 if (salt_len > 8) return (PARSER_SALT_LENGTH);
10614
10615 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
10616
10617 salt->salt_len = salt_len;
10618
10619 hash_pos++;
10620
10621 md5crypt_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
10622
10623 return (PARSER_OK);
10624 }
10625
10626 int episerver_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10627 {
10628 if ((input_len < DISPLAY_LEN_MIN_141) || (input_len > DISPLAY_LEN_MAX_141)) return (PARSER_GLOBAL_LENGTH);
10629
10630 if (memcmp (SIGNATURE_EPISERVER, input_buf, 14)) return (PARSER_SIGNATURE_UNMATCHED);
10631
10632 u32 *digest = (u32 *) hash_buf->digest;
10633
10634 salt_t *salt = hash_buf->salt;
10635
10636 char *salt_pos = input_buf + 14;
10637
10638 char *hash_pos = strchr (salt_pos, '*');
10639
10640 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
10641
10642 hash_pos++;
10643
10644 uint salt_len = hash_pos - salt_pos - 1;
10645
10646 char *salt_buf_ptr = (char *) salt->salt_buf;
10647
10648 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
10649
10650 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
10651
10652 salt->salt_len = salt_len;
10653
10654 u8 tmp_buf[100] = { 0 };
10655
10656 base64_decode (base64_to_int, (const u8 *) hash_pos, 27, tmp_buf);
10657
10658 memcpy (digest, tmp_buf, 20);
10659
10660 digest[0] = byte_swap_32 (digest[0]);
10661 digest[1] = byte_swap_32 (digest[1]);
10662 digest[2] = byte_swap_32 (digest[2]);
10663 digest[3] = byte_swap_32 (digest[3]);
10664 digest[4] = byte_swap_32 (digest[4]);
10665
10666 digest[0] -= SHA1M_A;
10667 digest[1] -= SHA1M_B;
10668 digest[2] -= SHA1M_C;
10669 digest[3] -= SHA1M_D;
10670 digest[4] -= SHA1M_E;
10671
10672 return (PARSER_OK);
10673 }
10674
10675 int descrypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10676 {
10677 if ((input_len < DISPLAY_LEN_MIN_1500) || (input_len > DISPLAY_LEN_MAX_1500)) return (PARSER_GLOBAL_LENGTH);
10678
10679 unsigned char c12 = itoa64_to_int (input_buf[12]);
10680
10681 if (c12 & 3) return (PARSER_HASH_VALUE);
10682
10683 u32 *digest = (u32 *) hash_buf->digest;
10684
10685 salt_t *salt = hash_buf->salt;
10686
10687 // for ascii_digest
10688 salt->salt_sign[0] = input_buf[0];
10689 salt->salt_sign[1] = input_buf[1];
10690
10691 salt->salt_buf[0] = itoa64_to_int (input_buf[0])
10692 | itoa64_to_int (input_buf[1]) << 6;
10693
10694 salt->salt_len = 2;
10695
10696 u8 tmp_buf[100] = { 0 };
10697
10698 base64_decode (itoa64_to_int, (const u8 *) input_buf + 2, 11, tmp_buf);
10699
10700 memcpy (digest, tmp_buf, 8);
10701
10702 uint tt;
10703
10704 IP (digest[0], digest[1], tt);
10705
10706 digest[2] = 0;
10707 digest[3] = 0;
10708
10709 return (PARSER_OK);
10710 }
10711
10712 int md4_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10713 {
10714 if ((input_len < DISPLAY_LEN_MIN_900) || (input_len > DISPLAY_LEN_MAX_900)) return (PARSER_GLOBAL_LENGTH);
10715
10716 u32 *digest = (u32 *) hash_buf->digest;
10717
10718 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
10719 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
10720 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
10721 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
10722
10723 digest[0] = byte_swap_32 (digest[0]);
10724 digest[1] = byte_swap_32 (digest[1]);
10725 digest[2] = byte_swap_32 (digest[2]);
10726 digest[3] = byte_swap_32 (digest[3]);
10727
10728 digest[0] -= MD4M_A;
10729 digest[1] -= MD4M_B;
10730 digest[2] -= MD4M_C;
10731 digest[3] -= MD4M_D;
10732
10733 return (PARSER_OK);
10734 }
10735
10736 int md4s_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10737 {
10738 if (data.opts_type & OPTS_TYPE_ST_HEX)
10739 {
10740 if ((input_len < DISPLAY_LEN_MIN_910H) || (input_len > DISPLAY_LEN_MAX_910H)) return (PARSER_GLOBAL_LENGTH);
10741 }
10742 else
10743 {
10744 if ((input_len < DISPLAY_LEN_MIN_910) || (input_len > DISPLAY_LEN_MAX_910)) return (PARSER_GLOBAL_LENGTH);
10745 }
10746
10747 u32 *digest = (u32 *) hash_buf->digest;
10748
10749 salt_t *salt = hash_buf->salt;
10750
10751 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
10752 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
10753 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
10754 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
10755
10756 digest[0] = byte_swap_32 (digest[0]);
10757 digest[1] = byte_swap_32 (digest[1]);
10758 digest[2] = byte_swap_32 (digest[2]);
10759 digest[3] = byte_swap_32 (digest[3]);
10760
10761 digest[0] -= MD4M_A;
10762 digest[1] -= MD4M_B;
10763 digest[2] -= MD4M_C;
10764 digest[3] -= MD4M_D;
10765
10766 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
10767
10768 uint salt_len = input_len - 32 - 1;
10769
10770 char *salt_buf = input_buf + 32 + 1;
10771
10772 char *salt_buf_ptr = (char *) salt->salt_buf;
10773
10774 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
10775
10776 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
10777
10778 salt->salt_len = salt_len;
10779
10780 return (PARSER_OK);
10781 }
10782
10783 int md5_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10784 {
10785 if ((input_len < DISPLAY_LEN_MIN_0) || (input_len > DISPLAY_LEN_MAX_0)) return (PARSER_GLOBAL_LENGTH);
10786
10787 u32 *digest = (u32 *) hash_buf->digest;
10788
10789 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
10790 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
10791 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
10792 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
10793
10794 digest[0] = byte_swap_32 (digest[0]);
10795 digest[1] = byte_swap_32 (digest[1]);
10796 digest[2] = byte_swap_32 (digest[2]);
10797 digest[3] = byte_swap_32 (digest[3]);
10798
10799 digest[0] -= MD5M_A;
10800 digest[1] -= MD5M_B;
10801 digest[2] -= MD5M_C;
10802 digest[3] -= MD5M_D;
10803
10804 return (PARSER_OK);
10805 }
10806
10807 int md5half_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10808 {
10809 if ((input_len < DISPLAY_LEN_MIN_5100) || (input_len > DISPLAY_LEN_MAX_5100)) return (PARSER_GLOBAL_LENGTH);
10810
10811 u32 *digest = (u32 *) hash_buf->digest;
10812
10813 digest[0] = hex_to_u32 ((const u8 *) &input_buf[0]);
10814 digest[1] = hex_to_u32 ((const u8 *) &input_buf[8]);
10815 digest[2] = 0;
10816 digest[3] = 0;
10817
10818 digest[0] = byte_swap_32 (digest[0]);
10819 digest[1] = byte_swap_32 (digest[1]);
10820
10821 return (PARSER_OK);
10822 }
10823
10824 int md5s_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10825 {
10826 if (data.opts_type & OPTS_TYPE_ST_HEX)
10827 {
10828 if ((input_len < DISPLAY_LEN_MIN_10H) || (input_len > DISPLAY_LEN_MAX_10H)) return (PARSER_GLOBAL_LENGTH);
10829 }
10830 else
10831 {
10832 if ((input_len < DISPLAY_LEN_MIN_10) || (input_len > DISPLAY_LEN_MAX_10)) return (PARSER_GLOBAL_LENGTH);
10833 }
10834
10835 u32 *digest = (u32 *) hash_buf->digest;
10836
10837 salt_t *salt = hash_buf->salt;
10838
10839 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
10840 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
10841 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
10842 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
10843
10844 digest[0] = byte_swap_32 (digest[0]);
10845 digest[1] = byte_swap_32 (digest[1]);
10846 digest[2] = byte_swap_32 (digest[2]);
10847 digest[3] = byte_swap_32 (digest[3]);
10848
10849 digest[0] -= MD5M_A;
10850 digest[1] -= MD5M_B;
10851 digest[2] -= MD5M_C;
10852 digest[3] -= MD5M_D;
10853
10854 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
10855
10856 uint salt_len = input_len - 32 - 1;
10857
10858 char *salt_buf = input_buf + 32 + 1;
10859
10860 char *salt_buf_ptr = (char *) salt->salt_buf;
10861
10862 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
10863
10864 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
10865
10866 salt->salt_len = salt_len;
10867
10868 return (PARSER_OK);
10869 }
10870
10871 int md5pix_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10872 {
10873 if ((input_len < DISPLAY_LEN_MIN_2400) || (input_len > DISPLAY_LEN_MAX_2400)) return (PARSER_GLOBAL_LENGTH);
10874
10875 u32 *digest = (u32 *) hash_buf->digest;
10876
10877 digest[0] = itoa64_to_int (input_buf[ 0]) << 0
10878 | itoa64_to_int (input_buf[ 1]) << 6
10879 | itoa64_to_int (input_buf[ 2]) << 12
10880 | itoa64_to_int (input_buf[ 3]) << 18;
10881 digest[1] = itoa64_to_int (input_buf[ 4]) << 0
10882 | itoa64_to_int (input_buf[ 5]) << 6
10883 | itoa64_to_int (input_buf[ 6]) << 12
10884 | itoa64_to_int (input_buf[ 7]) << 18;
10885 digest[2] = itoa64_to_int (input_buf[ 8]) << 0
10886 | itoa64_to_int (input_buf[ 9]) << 6
10887 | itoa64_to_int (input_buf[10]) << 12
10888 | itoa64_to_int (input_buf[11]) << 18;
10889 digest[3] = itoa64_to_int (input_buf[12]) << 0
10890 | itoa64_to_int (input_buf[13]) << 6
10891 | itoa64_to_int (input_buf[14]) << 12
10892 | itoa64_to_int (input_buf[15]) << 18;
10893
10894 digest[0] -= MD5M_A;
10895 digest[1] -= MD5M_B;
10896 digest[2] -= MD5M_C;
10897 digest[3] -= MD5M_D;
10898
10899 digest[0] &= 0x00ffffff;
10900 digest[1] &= 0x00ffffff;
10901 digest[2] &= 0x00ffffff;
10902 digest[3] &= 0x00ffffff;
10903
10904 return (PARSER_OK);
10905 }
10906
10907 int md5asa_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10908 {
10909 if (data.opts_type & OPTS_TYPE_ST_HEX)
10910 {
10911 if ((input_len < DISPLAY_LEN_MIN_2410H) || (input_len > DISPLAY_LEN_MAX_2410H)) return (PARSER_GLOBAL_LENGTH);
10912 }
10913 else
10914 {
10915 if ((input_len < DISPLAY_LEN_MIN_2410) || (input_len > DISPLAY_LEN_MAX_2410)) return (PARSER_GLOBAL_LENGTH);
10916 }
10917
10918 u32 *digest = (u32 *) hash_buf->digest;
10919
10920 salt_t *salt = hash_buf->salt;
10921
10922 digest[0] = itoa64_to_int (input_buf[ 0]) << 0
10923 | itoa64_to_int (input_buf[ 1]) << 6
10924 | itoa64_to_int (input_buf[ 2]) << 12
10925 | itoa64_to_int (input_buf[ 3]) << 18;
10926 digest[1] = itoa64_to_int (input_buf[ 4]) << 0
10927 | itoa64_to_int (input_buf[ 5]) << 6
10928 | itoa64_to_int (input_buf[ 6]) << 12
10929 | itoa64_to_int (input_buf[ 7]) << 18;
10930 digest[2] = itoa64_to_int (input_buf[ 8]) << 0
10931 | itoa64_to_int (input_buf[ 9]) << 6
10932 | itoa64_to_int (input_buf[10]) << 12
10933 | itoa64_to_int (input_buf[11]) << 18;
10934 digest[3] = itoa64_to_int (input_buf[12]) << 0
10935 | itoa64_to_int (input_buf[13]) << 6
10936 | itoa64_to_int (input_buf[14]) << 12
10937 | itoa64_to_int (input_buf[15]) << 18;
10938
10939 digest[0] -= MD5M_A;
10940 digest[1] -= MD5M_B;
10941 digest[2] -= MD5M_C;
10942 digest[3] -= MD5M_D;
10943
10944 digest[0] &= 0x00ffffff;
10945 digest[1] &= 0x00ffffff;
10946 digest[2] &= 0x00ffffff;
10947 digest[3] &= 0x00ffffff;
10948
10949 if (input_buf[16] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
10950
10951 uint salt_len = input_len - 16 - 1;
10952
10953 char *salt_buf = input_buf + 16 + 1;
10954
10955 char *salt_buf_ptr = (char *) salt->salt_buf;
10956
10957 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
10958
10959 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
10960
10961 salt->salt_len = salt_len;
10962
10963 return (PARSER_OK);
10964 }
10965
10966 void transform_netntlmv1_key (const u8 *nthash, u8 *key)
10967 {
10968 key[0] = (nthash[0] >> 0);
10969 key[1] = (nthash[0] << 7) | (nthash[1] >> 1);
10970 key[2] = (nthash[1] << 6) | (nthash[2] >> 2);
10971 key[3] = (nthash[2] << 5) | (nthash[3] >> 3);
10972 key[4] = (nthash[3] << 4) | (nthash[4] >> 4);
10973 key[5] = (nthash[4] << 3) | (nthash[5] >> 5);
10974 key[6] = (nthash[5] << 2) | (nthash[6] >> 6);
10975 key[7] = (nthash[6] << 1);
10976
10977 key[0] |= 0x01;
10978 key[1] |= 0x01;
10979 key[2] |= 0x01;
10980 key[3] |= 0x01;
10981 key[4] |= 0x01;
10982 key[5] |= 0x01;
10983 key[6] |= 0x01;
10984 key[7] |= 0x01;
10985 }
10986
10987 int netntlmv1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10988 {
10989 if ((input_len < DISPLAY_LEN_MIN_5500) || (input_len > DISPLAY_LEN_MAX_5500)) return (PARSER_GLOBAL_LENGTH);
10990
10991 u32 *digest = (u32 *) hash_buf->digest;
10992
10993 salt_t *salt = hash_buf->salt;
10994
10995 netntlm_t *netntlm = (netntlm_t *) hash_buf->esalt;
10996
10997 /**
10998 * parse line
10999 */
11000
11001 char *user_pos = input_buf;
11002
11003 char *unused_pos = strchr (user_pos, ':');
11004
11005 if (unused_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
11006
11007 uint user_len = unused_pos - user_pos;
11008
11009 if (user_len > 60) return (PARSER_SALT_LENGTH);
11010
11011 unused_pos++;
11012
11013 char *domain_pos = strchr (unused_pos, ':');
11014
11015 if (domain_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
11016
11017 uint unused_len = domain_pos - unused_pos;
11018
11019 if (unused_len != 0) return (PARSER_SALT_LENGTH);
11020
11021 domain_pos++;
11022
11023 char *srvchall_pos = strchr (domain_pos, ':');
11024
11025 if (srvchall_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
11026
11027 uint domain_len = srvchall_pos - domain_pos;
11028
11029 if (domain_len > 45) return (PARSER_SALT_LENGTH);
11030
11031 srvchall_pos++;
11032
11033 char *hash_pos = strchr (srvchall_pos, ':');
11034
11035 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
11036
11037 uint srvchall_len = hash_pos - srvchall_pos;
11038
11039 // if (srvchall_len != 0) return (PARSER_SALT_LENGTH);
11040
11041 hash_pos++;
11042
11043 char *clichall_pos = strchr (hash_pos, ':');
11044
11045 if (clichall_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
11046
11047 uint hash_len = clichall_pos - hash_pos;
11048
11049 if (hash_len != 48) return (PARSER_HASH_LENGTH);
11050
11051 clichall_pos++;
11052
11053 uint clichall_len = input_len - user_len - 1 - unused_len - 1 - domain_len - 1 - srvchall_len - 1 - hash_len - 1;
11054
11055 if (clichall_len != 16) return (PARSER_SALT_LENGTH);
11056
11057 /**
11058 * store some data for later use
11059 */
11060
11061 netntlm->user_len = user_len * 2;
11062 netntlm->domain_len = domain_len * 2;
11063 netntlm->srvchall_len = srvchall_len / 2;
11064 netntlm->clichall_len = clichall_len / 2;
11065
11066 char *userdomain_ptr = (char *) netntlm->userdomain_buf;
11067 char *chall_ptr = (char *) netntlm->chall_buf;
11068
11069 /**
11070 * handle username and domainname
11071 */
11072
11073 for (uint i = 0; i < user_len; i++)
11074 {
11075 *userdomain_ptr++ = user_pos[i];
11076 *userdomain_ptr++ = 0;
11077 }
11078
11079 for (uint i = 0; i < domain_len; i++)
11080 {
11081 *userdomain_ptr++ = domain_pos[i];
11082 *userdomain_ptr++ = 0;
11083 }
11084
11085 /**
11086 * handle server challenge encoding
11087 */
11088
11089 for (uint i = 0; i < srvchall_len; i += 2)
11090 {
11091 const char p0 = srvchall_pos[i + 0];
11092 const char p1 = srvchall_pos[i + 1];
11093
11094 *chall_ptr++ = hex_convert (p1) << 0
11095 | hex_convert (p0) << 4;
11096 }
11097
11098 /**
11099 * handle client challenge encoding
11100 */
11101
11102 for (uint i = 0; i < clichall_len; i += 2)
11103 {
11104 const char p0 = clichall_pos[i + 0];
11105 const char p1 = clichall_pos[i + 1];
11106
11107 *chall_ptr++ = hex_convert (p1) << 0
11108 | hex_convert (p0) << 4;
11109 }
11110
11111 /**
11112 * store data
11113 */
11114
11115 char *salt_buf_ptr = (char *) salt->salt_buf;
11116
11117 uint salt_len = parse_and_store_salt (salt_buf_ptr, clichall_pos, clichall_len);
11118
11119 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11120
11121 salt->salt_len = salt_len;
11122
11123 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
11124 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
11125 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
11126 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
11127
11128 digest[0] = byte_swap_32 (digest[0]);
11129 digest[1] = byte_swap_32 (digest[1]);
11130 digest[2] = byte_swap_32 (digest[2]);
11131 digest[3] = byte_swap_32 (digest[3]);
11132
11133 /* special case, last 8 byte do not need to be checked since they are brute-forced next */
11134
11135 uint digest_tmp[2] = { 0 };
11136
11137 digest_tmp[0] = hex_to_u32 ((const u8 *) &hash_pos[32]);
11138 digest_tmp[1] = hex_to_u32 ((const u8 *) &hash_pos[40]);
11139
11140 digest_tmp[0] = byte_swap_32 (digest_tmp[0]);
11141 digest_tmp[1] = byte_swap_32 (digest_tmp[1]);
11142
11143 /* special case 2: ESS */
11144
11145 if (srvchall_len == 48)
11146 {
11147 if ((netntlm->chall_buf[2] == 0) && (netntlm->chall_buf[3] == 0) && (netntlm->chall_buf[4] == 0) && (netntlm->chall_buf[5] == 0))
11148 {
11149 uint w[16] = { 0 };
11150
11151 w[ 0] = netntlm->chall_buf[6];
11152 w[ 1] = netntlm->chall_buf[7];
11153 w[ 2] = netntlm->chall_buf[0];
11154 w[ 3] = netntlm->chall_buf[1];
11155 w[ 4] = 0x80;
11156 w[14] = 16 * 8;
11157
11158 uint dgst[4] = { 0 };
11159
11160 dgst[0] = MAGIC_A;
11161 dgst[1] = MAGIC_B;
11162 dgst[2] = MAGIC_C;
11163 dgst[3] = MAGIC_D;
11164
11165 md5_64 (w, dgst);
11166
11167 salt->salt_buf[0] = dgst[0];
11168 salt->salt_buf[1] = dgst[1];
11169 }
11170 }
11171
11172 /* precompute netntlmv1 exploit start */
11173
11174 for (uint i = 0; i < 0x10000; i++)
11175 {
11176 uint key_md4[2] = { i, 0 };
11177 uint key_des[2] = { 0, 0 };
11178
11179 transform_netntlmv1_key ((u8 *) key_md4, (u8 *) key_des);
11180
11181 uint Kc[16] = { 0 };
11182 uint Kd[16] = { 0 };
11183
11184 _des_keysetup (key_des, Kc, Kd, c_skb);
11185
11186 uint data3[2] = { salt->salt_buf[0], salt->salt_buf[1] };
11187
11188 _des_encrypt (data3, Kc, Kd, c_SPtrans);
11189
11190 if (data3[0] != digest_tmp[0]) continue;
11191 if (data3[1] != digest_tmp[1]) continue;
11192
11193 salt->salt_buf[2] = i;
11194
11195 salt->salt_len = 24;
11196
11197 break;
11198 }
11199
11200 salt->salt_buf_pc[0] = digest_tmp[0];
11201 salt->salt_buf_pc[1] = digest_tmp[1];
11202
11203 /* precompute netntlmv1 exploit stop */
11204
11205 u32 tt;
11206
11207 IP (digest[0], digest[1], tt);
11208 IP (digest[2], digest[3], tt);
11209
11210 digest[0] = rotr32 (digest[0], 29);
11211 digest[1] = rotr32 (digest[1], 29);
11212 digest[2] = rotr32 (digest[2], 29);
11213 digest[3] = rotr32 (digest[3], 29);
11214
11215 IP (salt->salt_buf[0], salt->salt_buf[1], tt);
11216
11217 salt->salt_buf[0] = rotl32 (salt->salt_buf[0], 3);
11218 salt->salt_buf[1] = rotl32 (salt->salt_buf[1], 3);
11219
11220 return (PARSER_OK);
11221 }
11222
11223 int netntlmv2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11224 {
11225 if ((input_len < DISPLAY_LEN_MIN_5600) || (input_len > DISPLAY_LEN_MAX_5600)) return (PARSER_GLOBAL_LENGTH);
11226
11227 u32 *digest = (u32 *) hash_buf->digest;
11228
11229 salt_t *salt = hash_buf->salt;
11230
11231 netntlm_t *netntlm = (netntlm_t *) hash_buf->esalt;
11232
11233 /**
11234 * parse line
11235 */
11236
11237 char *user_pos = input_buf;
11238
11239 char *unused_pos = strchr (user_pos, ':');
11240
11241 if (unused_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
11242
11243 uint user_len = unused_pos - user_pos;
11244
11245 if (user_len > 60) return (PARSER_SALT_LENGTH);
11246
11247 unused_pos++;
11248
11249 char *domain_pos = strchr (unused_pos, ':');
11250
11251 if (domain_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
11252
11253 uint unused_len = domain_pos - unused_pos;
11254
11255 if (unused_len != 0) return (PARSER_SALT_LENGTH);
11256
11257 domain_pos++;
11258
11259 char *srvchall_pos = strchr (domain_pos, ':');
11260
11261 if (srvchall_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
11262
11263 uint domain_len = srvchall_pos - domain_pos;
11264
11265 if (domain_len > 45) return (PARSER_SALT_LENGTH);
11266
11267 srvchall_pos++;
11268
11269 char *hash_pos = strchr (srvchall_pos, ':');
11270
11271 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
11272
11273 uint srvchall_len = hash_pos - srvchall_pos;
11274
11275 if (srvchall_len != 16) return (PARSER_SALT_LENGTH);
11276
11277 hash_pos++;
11278
11279 char *clichall_pos = strchr (hash_pos, ':');
11280
11281 if (clichall_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
11282
11283 uint hash_len = clichall_pos - hash_pos;
11284
11285 if (hash_len != 32) return (PARSER_HASH_LENGTH);
11286
11287 clichall_pos++;
11288
11289 uint clichall_len = input_len - user_len - 1 - unused_len - 1 - domain_len - 1 - srvchall_len - 1 - hash_len - 1;
11290
11291 if (clichall_len > 1024) return (PARSER_SALT_LENGTH);
11292
11293 if (clichall_len % 2) return (PARSER_SALT_VALUE);
11294
11295 /**
11296 * store some data for later use
11297 */
11298
11299 netntlm->user_len = user_len * 2;
11300 netntlm->domain_len = domain_len * 2;
11301 netntlm->srvchall_len = srvchall_len / 2;
11302 netntlm->clichall_len = clichall_len / 2;
11303
11304 char *userdomain_ptr = (char *) netntlm->userdomain_buf;
11305 char *chall_ptr = (char *) netntlm->chall_buf;
11306
11307 /**
11308 * handle username and domainname
11309 */
11310
11311 for (uint i = 0; i < user_len; i++)
11312 {
11313 *userdomain_ptr++ = toupper (user_pos[i]);
11314 *userdomain_ptr++ = 0;
11315 }
11316
11317 for (uint i = 0; i < domain_len; i++)
11318 {
11319 *userdomain_ptr++ = domain_pos[i];
11320 *userdomain_ptr++ = 0;
11321 }
11322
11323 *userdomain_ptr++ = 0x80;
11324
11325 /**
11326 * handle server challenge encoding
11327 */
11328
11329 for (uint i = 0; i < srvchall_len; i += 2)
11330 {
11331 const char p0 = srvchall_pos[i + 0];
11332 const char p1 = srvchall_pos[i + 1];
11333
11334 *chall_ptr++ = hex_convert (p1) << 0
11335 | hex_convert (p0) << 4;
11336 }
11337
11338 /**
11339 * handle client challenge encoding
11340 */
11341
11342 for (uint i = 0; i < clichall_len; i += 2)
11343 {
11344 const char p0 = clichall_pos[i + 0];
11345 const char p1 = clichall_pos[i + 1];
11346
11347 *chall_ptr++ = hex_convert (p1) << 0
11348 | hex_convert (p0) << 4;
11349 }
11350
11351 *chall_ptr++ = 0x80;
11352
11353 /**
11354 * handle hash itself
11355 */
11356
11357 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
11358 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
11359 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
11360 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
11361
11362 digest[0] = byte_swap_32 (digest[0]);
11363 digest[1] = byte_swap_32 (digest[1]);
11364 digest[2] = byte_swap_32 (digest[2]);
11365 digest[3] = byte_swap_32 (digest[3]);
11366
11367 /**
11368 * reuse challange data as salt_buf, its the buffer that is most likely unique
11369 */
11370
11371 salt->salt_buf[0] = 0;
11372 salt->salt_buf[1] = 0;
11373 salt->salt_buf[2] = 0;
11374 salt->salt_buf[3] = 0;
11375 salt->salt_buf[4] = 0;
11376 salt->salt_buf[5] = 0;
11377 salt->salt_buf[6] = 0;
11378 salt->salt_buf[7] = 0;
11379
11380 uint *uptr;
11381
11382 uptr = (uint *) netntlm->userdomain_buf;
11383
11384 for (uint i = 0; i < 16; i += 16)
11385 {
11386 md5_64 (uptr, salt->salt_buf);
11387 }
11388
11389 uptr = (uint *) netntlm->chall_buf;
11390
11391 for (uint i = 0; i < 256; i += 16)
11392 {
11393 md5_64 (uptr, salt->salt_buf);
11394 }
11395
11396 salt->salt_len = 16;
11397
11398 return (PARSER_OK);
11399 }
11400
11401 int joomla_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11402 {
11403 if (data.opts_type & OPTS_TYPE_ST_HEX)
11404 {
11405 if ((input_len < DISPLAY_LEN_MIN_11H) || (input_len > DISPLAY_LEN_MAX_11H)) return (PARSER_GLOBAL_LENGTH);
11406 }
11407 else
11408 {
11409 if ((input_len < DISPLAY_LEN_MIN_11) || (input_len > DISPLAY_LEN_MAX_11)) return (PARSER_GLOBAL_LENGTH);
11410 }
11411
11412 u32 *digest = (u32 *) hash_buf->digest;
11413
11414 salt_t *salt = hash_buf->salt;
11415
11416 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11417 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11418 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11419 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11420
11421 digest[0] = byte_swap_32 (digest[0]);
11422 digest[1] = byte_swap_32 (digest[1]);
11423 digest[2] = byte_swap_32 (digest[2]);
11424 digest[3] = byte_swap_32 (digest[3]);
11425
11426 digest[0] -= MD5M_A;
11427 digest[1] -= MD5M_B;
11428 digest[2] -= MD5M_C;
11429 digest[3] -= MD5M_D;
11430
11431 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
11432
11433 uint salt_len = input_len - 32 - 1;
11434
11435 char *salt_buf = input_buf + 32 + 1;
11436
11437 char *salt_buf_ptr = (char *) salt->salt_buf;
11438
11439 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
11440
11441 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11442
11443 salt->salt_len = salt_len;
11444
11445 return (PARSER_OK);
11446 }
11447
11448 int postgresql_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11449 {
11450 if (data.opts_type & OPTS_TYPE_ST_HEX)
11451 {
11452 if ((input_len < DISPLAY_LEN_MIN_12H) || (input_len > DISPLAY_LEN_MAX_12H)) return (PARSER_GLOBAL_LENGTH);
11453 }
11454 else
11455 {
11456 if ((input_len < DISPLAY_LEN_MIN_12) || (input_len > DISPLAY_LEN_MAX_12)) return (PARSER_GLOBAL_LENGTH);
11457 }
11458
11459 u32 *digest = (u32 *) hash_buf->digest;
11460
11461 salt_t *salt = hash_buf->salt;
11462
11463 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11464 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11465 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11466 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11467
11468 digest[0] = byte_swap_32 (digest[0]);
11469 digest[1] = byte_swap_32 (digest[1]);
11470 digest[2] = byte_swap_32 (digest[2]);
11471 digest[3] = byte_swap_32 (digest[3]);
11472
11473 digest[0] -= MD5M_A;
11474 digest[1] -= MD5M_B;
11475 digest[2] -= MD5M_C;
11476 digest[3] -= MD5M_D;
11477
11478 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
11479
11480 uint salt_len = input_len - 32 - 1;
11481
11482 char *salt_buf = input_buf + 32 + 1;
11483
11484 char *salt_buf_ptr = (char *) salt->salt_buf;
11485
11486 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
11487
11488 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11489
11490 salt->salt_len = salt_len;
11491
11492 return (PARSER_OK);
11493 }
11494
11495 int md5md5_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11496 {
11497 if ((input_len < DISPLAY_LEN_MIN_2600) || (input_len > DISPLAY_LEN_MAX_2600)) return (PARSER_GLOBAL_LENGTH);
11498
11499 u32 *digest = (u32 *) hash_buf->digest;
11500
11501 salt_t *salt = hash_buf->salt;
11502
11503 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11504 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11505 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11506 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11507
11508 digest[0] = byte_swap_32 (digest[0]);
11509 digest[1] = byte_swap_32 (digest[1]);
11510 digest[2] = byte_swap_32 (digest[2]);
11511 digest[3] = byte_swap_32 (digest[3]);
11512
11513 digest[0] -= MD5M_A;
11514 digest[1] -= MD5M_B;
11515 digest[2] -= MD5M_C;
11516 digest[3] -= MD5M_D;
11517
11518 /**
11519 * This is a virtual salt. While the algorithm is basically not salted
11520 * we can exploit the salt buffer to set the 0x80 and the w[14] value.
11521 * This way we can save a special md5md5 kernel and reuse the one from vbull.
11522 */
11523
11524 char *salt_buf_ptr = (char *) salt->salt_buf;
11525
11526 uint salt_len = parse_and_store_salt (salt_buf_ptr, (char *) "", 0);
11527
11528 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11529
11530 salt->salt_len = salt_len;
11531
11532 return (PARSER_OK);
11533 }
11534
11535 int vb3_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11536 {
11537 if (data.opts_type & OPTS_TYPE_ST_HEX)
11538 {
11539 if ((input_len < DISPLAY_LEN_MIN_2611H) || (input_len > DISPLAY_LEN_MAX_2611H)) return (PARSER_GLOBAL_LENGTH);
11540 }
11541 else
11542 {
11543 if ((input_len < DISPLAY_LEN_MIN_2611) || (input_len > DISPLAY_LEN_MAX_2611)) return (PARSER_GLOBAL_LENGTH);
11544 }
11545
11546 u32 *digest = (u32 *) hash_buf->digest;
11547
11548 salt_t *salt = hash_buf->salt;
11549
11550 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11551 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11552 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11553 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11554
11555 digest[0] = byte_swap_32 (digest[0]);
11556 digest[1] = byte_swap_32 (digest[1]);
11557 digest[2] = byte_swap_32 (digest[2]);
11558 digest[3] = byte_swap_32 (digest[3]);
11559
11560 digest[0] -= MD5M_A;
11561 digest[1] -= MD5M_B;
11562 digest[2] -= MD5M_C;
11563 digest[3] -= MD5M_D;
11564
11565 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
11566
11567 uint salt_len = input_len - 32 - 1;
11568
11569 char *salt_buf = input_buf + 32 + 1;
11570
11571 char *salt_buf_ptr = (char *) salt->salt_buf;
11572
11573 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
11574
11575 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11576
11577 salt->salt_len = salt_len;
11578
11579 return (PARSER_OK);
11580 }
11581
11582 int vb30_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11583 {
11584 if (data.opts_type & OPTS_TYPE_ST_HEX)
11585 {
11586 if ((input_len < DISPLAY_LEN_MIN_2711H) || (input_len > DISPLAY_LEN_MAX_2711H)) return (PARSER_GLOBAL_LENGTH);
11587 }
11588 else
11589 {
11590 if ((input_len < DISPLAY_LEN_MIN_2711) || (input_len > DISPLAY_LEN_MAX_2711)) return (PARSER_GLOBAL_LENGTH);
11591 }
11592
11593 u32 *digest = (u32 *) hash_buf->digest;
11594
11595 salt_t *salt = hash_buf->salt;
11596
11597 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11598 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11599 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11600 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11601
11602 digest[0] = byte_swap_32 (digest[0]);
11603 digest[1] = byte_swap_32 (digest[1]);
11604 digest[2] = byte_swap_32 (digest[2]);
11605 digest[3] = byte_swap_32 (digest[3]);
11606
11607 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
11608
11609 uint salt_len = input_len - 32 - 1;
11610
11611 char *salt_buf = input_buf + 32 + 1;
11612
11613 char *salt_buf_ptr = (char *) salt->salt_buf;
11614
11615 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
11616
11617 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11618
11619 salt->salt_len = salt_len;
11620
11621 return (PARSER_OK);
11622 }
11623
11624 int dcc_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11625 {
11626 if (data.opts_type & OPTS_TYPE_ST_HEX)
11627 {
11628 if ((input_len < DISPLAY_LEN_MIN_1100H) || (input_len > DISPLAY_LEN_MAX_1100H)) return (PARSER_GLOBAL_LENGTH);
11629 }
11630 else
11631 {
11632 if ((input_len < DISPLAY_LEN_MIN_1100) || (input_len > DISPLAY_LEN_MAX_1100)) return (PARSER_GLOBAL_LENGTH);
11633 }
11634
11635 u32 *digest = (u32 *) hash_buf->digest;
11636
11637 salt_t *salt = hash_buf->salt;
11638
11639 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11640 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11641 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11642 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11643
11644 digest[0] = byte_swap_32 (digest[0]);
11645 digest[1] = byte_swap_32 (digest[1]);
11646 digest[2] = byte_swap_32 (digest[2]);
11647 digest[3] = byte_swap_32 (digest[3]);
11648
11649 digest[0] -= MD4M_A;
11650 digest[1] -= MD4M_B;
11651 digest[2] -= MD4M_C;
11652 digest[3] -= MD4M_D;
11653
11654 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
11655
11656 uint salt_len = input_len - 32 - 1;
11657
11658 char *salt_buf = input_buf + 32 + 1;
11659
11660 char *salt_buf_ptr = (char *) salt->salt_buf;
11661
11662 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
11663
11664 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11665
11666 salt->salt_len = salt_len;
11667
11668 return (PARSER_OK);
11669 }
11670
11671 int ipb2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11672 {
11673 if (data.opts_type & OPTS_TYPE_ST_HEX)
11674 {
11675 if ((input_len < DISPLAY_LEN_MIN_2811H) || (input_len > DISPLAY_LEN_MAX_2811H)) return (PARSER_GLOBAL_LENGTH);
11676 }
11677 else
11678 {
11679 if ((input_len < DISPLAY_LEN_MIN_2811) || (input_len > DISPLAY_LEN_MAX_2811)) return (PARSER_GLOBAL_LENGTH);
11680 }
11681
11682 u32 *digest = (u32 *) hash_buf->digest;
11683
11684 salt_t *salt = hash_buf->salt;
11685
11686 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11687 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11688 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11689 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11690
11691 digest[0] = byte_swap_32 (digest[0]);
11692 digest[1] = byte_swap_32 (digest[1]);
11693 digest[2] = byte_swap_32 (digest[2]);
11694 digest[3] = byte_swap_32 (digest[3]);
11695
11696 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
11697
11698 uint salt_len = input_len - 32 - 1;
11699
11700 char *salt_buf = input_buf + 32 + 1;
11701
11702 uint salt_pc_block[16] = { 0 };
11703
11704 char *salt_pc_block_ptr = (char *) salt_pc_block;
11705
11706 salt_len = parse_and_store_salt (salt_pc_block_ptr, salt_buf, salt_len);
11707
11708 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11709
11710 salt_pc_block_ptr[salt_len] = (unsigned char) 0x80;
11711
11712 salt_pc_block[14] = salt_len * 8;
11713
11714 uint salt_pc_digest[4] = { MAGIC_A, MAGIC_B, MAGIC_C, MAGIC_D };
11715
11716 md5_64 (salt_pc_block, salt_pc_digest);
11717
11718 salt_pc_digest[0] = byte_swap_32 (salt_pc_digest[0]);
11719 salt_pc_digest[1] = byte_swap_32 (salt_pc_digest[1]);
11720 salt_pc_digest[2] = byte_swap_32 (salt_pc_digest[2]);
11721 salt_pc_digest[3] = byte_swap_32 (salt_pc_digest[3]);
11722
11723 u8 *salt_buf_ptr = (u8 *) salt->salt_buf;
11724
11725 memcpy (salt_buf_ptr, salt_buf, salt_len);
11726
11727 u8 *salt_buf_pc_ptr = (u8 *) salt->salt_buf_pc;
11728
11729 bin_to_hex_lower (salt_pc_digest[0], salt_buf_pc_ptr + 0);
11730 bin_to_hex_lower (salt_pc_digest[1], salt_buf_pc_ptr + 8);
11731 bin_to_hex_lower (salt_pc_digest[2], salt_buf_pc_ptr + 16);
11732 bin_to_hex_lower (salt_pc_digest[3], salt_buf_pc_ptr + 24);
11733
11734 salt->salt_len = 32; // changed, was salt_len before -- was a bug? 32 should be correct
11735
11736 return (PARSER_OK);
11737 }
11738
11739 int sha1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11740 {
11741 if ((input_len < DISPLAY_LEN_MIN_100) || (input_len > DISPLAY_LEN_MAX_100)) return (PARSER_GLOBAL_LENGTH);
11742
11743 u32 *digest = (u32 *) hash_buf->digest;
11744
11745 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11746 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11747 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11748 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11749 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
11750
11751 digest[0] -= SHA1M_A;
11752 digest[1] -= SHA1M_B;
11753 digest[2] -= SHA1M_C;
11754 digest[3] -= SHA1M_D;
11755 digest[4] -= SHA1M_E;
11756
11757 return (PARSER_OK);
11758 }
11759
11760 int sha1linkedin_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11761 {
11762 if ((input_len < DISPLAY_LEN_MIN_100) || (input_len > DISPLAY_LEN_MAX_100)) return (PARSER_GLOBAL_LENGTH);
11763
11764 u32 *digest = (u32 *) hash_buf->digest;
11765
11766 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11767 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11768 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11769 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11770 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
11771
11772 return (PARSER_OK);
11773 }
11774
11775 int sha1axcrypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11776 {
11777 if ((input_len < DISPLAY_LEN_MIN_13300) || (input_len > DISPLAY_LEN_MAX_13300)) return (PARSER_GLOBAL_LENGTH);
11778
11779 if (memcmp (SIGNATURE_AXCRYPT_SHA1, input_buf, 13)) return (PARSER_SIGNATURE_UNMATCHED);
11780
11781 u32 *digest = (u32 *) hash_buf->digest;
11782
11783 input_buf +=14;
11784
11785 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11786 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11787 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11788 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11789 digest[4] = 0x00000000;
11790
11791 return (PARSER_OK);
11792 }
11793
11794 int sha1s_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11795 {
11796 if (data.opts_type & OPTS_TYPE_ST_HEX)
11797 {
11798 if ((input_len < DISPLAY_LEN_MIN_110H) || (input_len > DISPLAY_LEN_MAX_110H)) return (PARSER_GLOBAL_LENGTH);
11799 }
11800 else
11801 {
11802 if ((input_len < DISPLAY_LEN_MIN_110) || (input_len > DISPLAY_LEN_MAX_110)) return (PARSER_GLOBAL_LENGTH);
11803 }
11804
11805 u32 *digest = (u32 *) hash_buf->digest;
11806
11807 salt_t *salt = hash_buf->salt;
11808
11809 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11810 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11811 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11812 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11813 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
11814
11815 digest[0] -= SHA1M_A;
11816 digest[1] -= SHA1M_B;
11817 digest[2] -= SHA1M_C;
11818 digest[3] -= SHA1M_D;
11819 digest[4] -= SHA1M_E;
11820
11821 if (input_buf[40] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
11822
11823 uint salt_len = input_len - 40 - 1;
11824
11825 char *salt_buf = input_buf + 40 + 1;
11826
11827 char *salt_buf_ptr = (char *) salt->salt_buf;
11828
11829 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
11830
11831 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11832
11833 salt->salt_len = salt_len;
11834
11835 return (PARSER_OK);
11836 }
11837
11838 int pstoken_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11839 {
11840 if ((input_len < DISPLAY_LEN_MIN_13500) || (input_len > DISPLAY_LEN_MAX_13500)) return (PARSER_GLOBAL_LENGTH);
11841
11842 u32 *digest = (u32 *) hash_buf->digest;
11843
11844 salt_t *salt = hash_buf->salt;
11845
11846 pstoken_t *pstoken = (pstoken_t *) hash_buf->esalt;
11847
11848 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11849 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11850 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11851 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11852 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
11853
11854 if (input_buf[40] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
11855
11856 uint salt_len = input_len - 40 - 1;
11857
11858 char *salt_buf = input_buf + 40 + 1;
11859
11860 if (salt_len == UINT_MAX || salt_len % 2 != 0) return (PARSER_SALT_LENGTH);
11861
11862 u8 *pstoken_ptr = (u8 *) pstoken->salt_buf;
11863
11864 for (uint i = 0, j = 0; i < salt_len; i += 2, j += 1)
11865 {
11866 pstoken_ptr[j] = hex_to_u8 ((const u8 *) &salt_buf[i]);
11867 }
11868
11869 pstoken->salt_len = salt_len / 2;
11870
11871 /* some fake salt for the sorting mechanisms */
11872
11873 salt->salt_buf[0] = pstoken->salt_buf[0];
11874 salt->salt_buf[1] = pstoken->salt_buf[1];
11875 salt->salt_buf[2] = pstoken->salt_buf[2];
11876 salt->salt_buf[3] = pstoken->salt_buf[3];
11877 salt->salt_buf[4] = pstoken->salt_buf[4];
11878 salt->salt_buf[5] = pstoken->salt_buf[5];
11879 salt->salt_buf[6] = pstoken->salt_buf[6];
11880 salt->salt_buf[7] = pstoken->salt_buf[7];
11881
11882 salt->salt_len = 32;
11883
11884 /* we need to check if we can precompute some of the data --
11885 this is possible since the scheme is badly designed */
11886
11887 pstoken->pc_digest[0] = SHA1M_A;
11888 pstoken->pc_digest[1] = SHA1M_B;
11889 pstoken->pc_digest[2] = SHA1M_C;
11890 pstoken->pc_digest[3] = SHA1M_D;
11891 pstoken->pc_digest[4] = SHA1M_E;
11892
11893 pstoken->pc_offset = 0;
11894
11895 for (int i = 0; i < (int) pstoken->salt_len - 64; i += 64)
11896 {
11897 uint w[16];
11898
11899 w[ 0] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 0]);
11900 w[ 1] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 1]);
11901 w[ 2] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 2]);
11902 w[ 3] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 3]);
11903 w[ 4] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 4]);
11904 w[ 5] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 5]);
11905 w[ 6] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 6]);
11906 w[ 7] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 7]);
11907 w[ 8] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 8]);
11908 w[ 9] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 9]);
11909 w[10] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 10]);
11910 w[11] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 11]);
11911 w[12] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 12]);
11912 w[13] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 13]);
11913 w[14] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 14]);
11914 w[15] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 15]);
11915
11916 sha1_64 (w, pstoken->pc_digest);
11917
11918 pstoken->pc_offset += 16;
11919 }
11920
11921 return (PARSER_OK);
11922 }
11923
11924 int sha1b64_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11925 {
11926 if ((input_len < DISPLAY_LEN_MIN_101) || (input_len > DISPLAY_LEN_MAX_101)) return (PARSER_GLOBAL_LENGTH);
11927
11928 if (memcmp (SIGNATURE_SHA1B64, input_buf, 5)) return (PARSER_SIGNATURE_UNMATCHED);
11929
11930 u32 *digest = (u32 *) hash_buf->digest;
11931
11932 u8 tmp_buf[100] = { 0 };
11933
11934 base64_decode (base64_to_int, (const u8 *) input_buf + 5, input_len - 5, tmp_buf);
11935
11936 memcpy (digest, tmp_buf, 20);
11937
11938 digest[0] = byte_swap_32 (digest[0]);
11939 digest[1] = byte_swap_32 (digest[1]);
11940 digest[2] = byte_swap_32 (digest[2]);
11941 digest[3] = byte_swap_32 (digest[3]);
11942 digest[4] = byte_swap_32 (digest[4]);
11943
11944 digest[0] -= SHA1M_A;
11945 digest[1] -= SHA1M_B;
11946 digest[2] -= SHA1M_C;
11947 digest[3] -= SHA1M_D;
11948 digest[4] -= SHA1M_E;
11949
11950 return (PARSER_OK);
11951 }
11952
11953 int sha1b64s_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11954 {
11955 if ((input_len < DISPLAY_LEN_MIN_111) || (input_len > DISPLAY_LEN_MAX_111)) return (PARSER_GLOBAL_LENGTH);
11956
11957 if (memcmp (SIGNATURE_SSHA1B64_lower, input_buf, 6) && memcmp (SIGNATURE_SSHA1B64_upper, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
11958
11959 u32 *digest = (u32 *) hash_buf->digest;
11960
11961 salt_t *salt = hash_buf->salt;
11962
11963 u8 tmp_buf[100] = { 0 };
11964
11965 int tmp_len = base64_decode (base64_to_int, (const u8 *) input_buf + 6, input_len - 6, tmp_buf);
11966
11967 if (tmp_len < 20) return (PARSER_HASH_LENGTH);
11968
11969 memcpy (digest, tmp_buf, 20);
11970
11971 int salt_len = tmp_len - 20;
11972
11973 if (salt_len < 0) return (PARSER_SALT_LENGTH);
11974
11975 salt->salt_len = salt_len;
11976
11977 memcpy (salt->salt_buf, tmp_buf + 20, salt->salt_len);
11978
11979 if (data.opts_type & OPTS_TYPE_ST_ADD80)
11980 {
11981 char *ptr = (char *) salt->salt_buf;
11982
11983 ptr[salt->salt_len] = 0x80;
11984 }
11985
11986 digest[0] = byte_swap_32 (digest[0]);
11987 digest[1] = byte_swap_32 (digest[1]);
11988 digest[2] = byte_swap_32 (digest[2]);
11989 digest[3] = byte_swap_32 (digest[3]);
11990 digest[4] = byte_swap_32 (digest[4]);
11991
11992 digest[0] -= SHA1M_A;
11993 digest[1] -= SHA1M_B;
11994 digest[2] -= SHA1M_C;
11995 digest[3] -= SHA1M_D;
11996 digest[4] -= SHA1M_E;
11997
11998 return (PARSER_OK);
11999 }
12000
12001 int mssql2000_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12002 {
12003 if ((input_len < DISPLAY_LEN_MIN_131) || (input_len > DISPLAY_LEN_MAX_131)) return (PARSER_GLOBAL_LENGTH);
12004
12005 if (memcmp (SIGNATURE_MSSQL, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
12006
12007 u32 *digest = (u32 *) hash_buf->digest;
12008
12009 salt_t *salt = hash_buf->salt;
12010
12011 char *salt_buf = input_buf + 6;
12012
12013 uint salt_len = 8;
12014
12015 char *salt_buf_ptr = (char *) salt->salt_buf;
12016
12017 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
12018
12019 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
12020
12021 salt->salt_len = salt_len;
12022
12023 char *hash_pos = input_buf + 6 + 8 + 40;
12024
12025 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
12026 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
12027 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
12028 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
12029 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
12030
12031 digest[0] -= SHA1M_A;
12032 digest[1] -= SHA1M_B;
12033 digest[2] -= SHA1M_C;
12034 digest[3] -= SHA1M_D;
12035 digest[4] -= SHA1M_E;
12036
12037 return (PARSER_OK);
12038 }
12039
12040 int mssql2005_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12041 {
12042 if ((input_len < DISPLAY_LEN_MIN_132) || (input_len > DISPLAY_LEN_MAX_132)) return (PARSER_GLOBAL_LENGTH);
12043
12044 if (memcmp (SIGNATURE_MSSQL, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
12045
12046 u32 *digest = (u32 *) hash_buf->digest;
12047
12048 salt_t *salt = hash_buf->salt;
12049
12050 char *salt_buf = input_buf + 6;
12051
12052 uint salt_len = 8;
12053
12054 char *salt_buf_ptr = (char *) salt->salt_buf;
12055
12056 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
12057
12058 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
12059
12060 salt->salt_len = salt_len;
12061
12062 char *hash_pos = input_buf + 6 + 8;
12063
12064 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
12065 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
12066 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
12067 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
12068 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
12069
12070 digest[0] -= SHA1M_A;
12071 digest[1] -= SHA1M_B;
12072 digest[2] -= SHA1M_C;
12073 digest[3] -= SHA1M_D;
12074 digest[4] -= SHA1M_E;
12075
12076 return (PARSER_OK);
12077 }
12078
12079 int mssql2012_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12080 {
12081 if ((input_len < DISPLAY_LEN_MIN_1731) || (input_len > DISPLAY_LEN_MAX_1731)) return (PARSER_GLOBAL_LENGTH);
12082
12083 if (memcmp (SIGNATURE_MSSQL2012, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
12084
12085 u64 *digest = (u64 *) hash_buf->digest;
12086
12087 salt_t *salt = hash_buf->salt;
12088
12089 char *salt_buf = input_buf + 6;
12090
12091 uint salt_len = 8;
12092
12093 char *salt_buf_ptr = (char *) salt->salt_buf;
12094
12095 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
12096
12097 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
12098
12099 salt->salt_len = salt_len;
12100
12101 char *hash_pos = input_buf + 6 + 8;
12102
12103 digest[0] = hex_to_u64 ((const u8 *) &hash_pos[ 0]);
12104 digest[1] = hex_to_u64 ((const u8 *) &hash_pos[ 16]);
12105 digest[2] = hex_to_u64 ((const u8 *) &hash_pos[ 32]);
12106 digest[3] = hex_to_u64 ((const u8 *) &hash_pos[ 48]);
12107 digest[4] = hex_to_u64 ((const u8 *) &hash_pos[ 64]);
12108 digest[5] = hex_to_u64 ((const u8 *) &hash_pos[ 80]);
12109 digest[6] = hex_to_u64 ((const u8 *) &hash_pos[ 96]);
12110 digest[7] = hex_to_u64 ((const u8 *) &hash_pos[112]);
12111
12112 digest[0] -= SHA512M_A;
12113 digest[1] -= SHA512M_B;
12114 digest[2] -= SHA512M_C;
12115 digest[3] -= SHA512M_D;
12116 digest[4] -= SHA512M_E;
12117 digest[5] -= SHA512M_F;
12118 digest[6] -= SHA512M_G;
12119 digest[7] -= SHA512M_H;
12120
12121 return (PARSER_OK);
12122 }
12123
12124 int oracleh_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12125 {
12126 if (data.opts_type & OPTS_TYPE_ST_HEX)
12127 {
12128 if ((input_len < DISPLAY_LEN_MIN_3100H) || (input_len > DISPLAY_LEN_MAX_3100H)) return (PARSER_GLOBAL_LENGTH);
12129 }
12130 else
12131 {
12132 if ((input_len < DISPLAY_LEN_MIN_3100) || (input_len > DISPLAY_LEN_MAX_3100)) return (PARSER_GLOBAL_LENGTH);
12133 }
12134
12135 u32 *digest = (u32 *) hash_buf->digest;
12136
12137 salt_t *salt = hash_buf->salt;
12138
12139 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
12140 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
12141 digest[2] = 0;
12142 digest[3] = 0;
12143
12144 digest[0] = byte_swap_32 (digest[0]);
12145 digest[1] = byte_swap_32 (digest[1]);
12146
12147 if (input_buf[16] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
12148
12149 uint salt_len = input_len - 16 - 1;
12150
12151 char *salt_buf = input_buf + 16 + 1;
12152
12153 char *salt_buf_ptr = (char *) salt->salt_buf;
12154
12155 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
12156
12157 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
12158
12159 salt->salt_len = salt_len;
12160
12161 return (PARSER_OK);
12162 }
12163
12164 int oracles_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12165 {
12166 if ((input_len < DISPLAY_LEN_MIN_112) || (input_len > DISPLAY_LEN_MAX_112)) return (PARSER_GLOBAL_LENGTH);
12167
12168 u32 *digest = (u32 *) hash_buf->digest;
12169
12170 salt_t *salt = hash_buf->salt;
12171
12172 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
12173 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
12174 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
12175 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
12176 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
12177
12178 digest[0] -= SHA1M_A;
12179 digest[1] -= SHA1M_B;
12180 digest[2] -= SHA1M_C;
12181 digest[3] -= SHA1M_D;
12182 digest[4] -= SHA1M_E;
12183
12184 if (input_buf[40] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
12185
12186 uint salt_len = input_len - 40 - 1;
12187
12188 char *salt_buf = input_buf + 40 + 1;
12189
12190 char *salt_buf_ptr = (char *) salt->salt_buf;
12191
12192 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
12193
12194 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
12195
12196 salt->salt_len = salt_len;
12197
12198 return (PARSER_OK);
12199 }
12200
12201 int oraclet_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12202 {
12203 if ((input_len < DISPLAY_LEN_MIN_12300) || (input_len > DISPLAY_LEN_MAX_12300)) return (PARSER_GLOBAL_LENGTH);
12204
12205 u32 *digest = (u32 *) hash_buf->digest;
12206
12207 salt_t *salt = hash_buf->salt;
12208
12209 char *hash_pos = input_buf;
12210
12211 digest[ 0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
12212 digest[ 1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
12213 digest[ 2] = hex_to_u32 ((const u8 *) &hash_pos[ 16]);
12214 digest[ 3] = hex_to_u32 ((const u8 *) &hash_pos[ 24]);
12215 digest[ 4] = hex_to_u32 ((const u8 *) &hash_pos[ 32]);
12216 digest[ 5] = hex_to_u32 ((const u8 *) &hash_pos[ 40]);
12217 digest[ 6] = hex_to_u32 ((const u8 *) &hash_pos[ 48]);
12218 digest[ 7] = hex_to_u32 ((const u8 *) &hash_pos[ 56]);
12219 digest[ 8] = hex_to_u32 ((const u8 *) &hash_pos[ 64]);
12220 digest[ 9] = hex_to_u32 ((const u8 *) &hash_pos[ 72]);
12221 digest[10] = hex_to_u32 ((const u8 *) &hash_pos[ 80]);
12222 digest[11] = hex_to_u32 ((const u8 *) &hash_pos[ 88]);
12223 digest[12] = hex_to_u32 ((const u8 *) &hash_pos[ 96]);
12224 digest[13] = hex_to_u32 ((const u8 *) &hash_pos[104]);
12225 digest[14] = hex_to_u32 ((const u8 *) &hash_pos[112]);
12226 digest[15] = hex_to_u32 ((const u8 *) &hash_pos[120]);
12227
12228 char *salt_pos = input_buf + 128;
12229
12230 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_pos[ 0]);
12231 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_pos[ 8]);
12232 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &salt_pos[16]);
12233 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &salt_pos[24]);
12234
12235 salt->salt_iter = ROUNDS_ORACLET - 1;
12236 salt->salt_len = 16;
12237
12238 return (PARSER_OK);
12239 }
12240
12241 int sha256_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12242 {
12243 if ((input_len < DISPLAY_LEN_MIN_1400) || (input_len > DISPLAY_LEN_MAX_1400)) return (PARSER_GLOBAL_LENGTH);
12244
12245 u32 *digest = (u32 *) hash_buf->digest;
12246
12247 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
12248 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
12249 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
12250 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
12251 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
12252 digest[5] = hex_to_u32 ((const u8 *) &input_buf[40]);
12253 digest[6] = hex_to_u32 ((const u8 *) &input_buf[48]);
12254 digest[7] = hex_to_u32 ((const u8 *) &input_buf[56]);
12255
12256 digest[0] -= SHA256M_A;
12257 digest[1] -= SHA256M_B;
12258 digest[2] -= SHA256M_C;
12259 digest[3] -= SHA256M_D;
12260 digest[4] -= SHA256M_E;
12261 digest[5] -= SHA256M_F;
12262 digest[6] -= SHA256M_G;
12263 digest[7] -= SHA256M_H;
12264
12265 return (PARSER_OK);
12266 }
12267
12268 int sha256s_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12269 {
12270 if (data.opts_type & OPTS_TYPE_ST_HEX)
12271 {
12272 if ((input_len < DISPLAY_LEN_MIN_1410H) || (input_len > DISPLAY_LEN_MAX_1410H)) return (PARSER_GLOBAL_LENGTH);
12273 }
12274 else
12275 {
12276 if ((input_len < DISPLAY_LEN_MIN_1410) || (input_len > DISPLAY_LEN_MAX_1410)) return (PARSER_GLOBAL_LENGTH);
12277 }
12278
12279 u32 *digest = (u32 *) hash_buf->digest;
12280
12281 salt_t *salt = hash_buf->salt;
12282
12283 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
12284 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
12285 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
12286 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
12287 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
12288 digest[5] = hex_to_u32 ((const u8 *) &input_buf[40]);
12289 digest[6] = hex_to_u32 ((const u8 *) &input_buf[48]);
12290 digest[7] = hex_to_u32 ((const u8 *) &input_buf[56]);
12291
12292 digest[0] -= SHA256M_A;
12293 digest[1] -= SHA256M_B;
12294 digest[2] -= SHA256M_C;
12295 digest[3] -= SHA256M_D;
12296 digest[4] -= SHA256M_E;
12297 digest[5] -= SHA256M_F;
12298 digest[6] -= SHA256M_G;
12299 digest[7] -= SHA256M_H;
12300
12301 if (input_buf[64] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
12302
12303 uint salt_len = input_len - 64 - 1;
12304
12305 char *salt_buf = input_buf + 64 + 1;
12306
12307 char *salt_buf_ptr = (char *) salt->salt_buf;
12308
12309 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
12310
12311 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
12312
12313 salt->salt_len = salt_len;
12314
12315 return (PARSER_OK);
12316 }
12317
12318 int sha384_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12319 {
12320 if ((input_len < DISPLAY_LEN_MIN_10800) || (input_len > DISPLAY_LEN_MAX_10800)) return (PARSER_GLOBAL_LENGTH);
12321
12322 u64 *digest = (u64 *) hash_buf->digest;
12323
12324 digest[0] = hex_to_u64 ((const u8 *) &input_buf[ 0]);
12325 digest[1] = hex_to_u64 ((const u8 *) &input_buf[ 16]);
12326 digest[2] = hex_to_u64 ((const u8 *) &input_buf[ 32]);
12327 digest[3] = hex_to_u64 ((const u8 *) &input_buf[ 48]);
12328 digest[4] = hex_to_u64 ((const u8 *) &input_buf[ 64]);
12329 digest[5] = hex_to_u64 ((const u8 *) &input_buf[ 80]);
12330 digest[6] = 0;
12331 digest[7] = 0;
12332
12333 digest[0] -= SHA384M_A;
12334 digest[1] -= SHA384M_B;
12335 digest[2] -= SHA384M_C;
12336 digest[3] -= SHA384M_D;
12337 digest[4] -= SHA384M_E;
12338 digest[5] -= SHA384M_F;
12339 digest[6] -= 0;
12340 digest[7] -= 0;
12341
12342 return (PARSER_OK);
12343 }
12344
12345 int sha512_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12346 {
12347 if ((input_len < DISPLAY_LEN_MIN_1700) || (input_len > DISPLAY_LEN_MAX_1700)) return (PARSER_GLOBAL_LENGTH);
12348
12349 u64 *digest = (u64 *) hash_buf->digest;
12350
12351 digest[0] = hex_to_u64 ((const u8 *) &input_buf[ 0]);
12352 digest[1] = hex_to_u64 ((const u8 *) &input_buf[ 16]);
12353 digest[2] = hex_to_u64 ((const u8 *) &input_buf[ 32]);
12354 digest[3] = hex_to_u64 ((const u8 *) &input_buf[ 48]);
12355 digest[4] = hex_to_u64 ((const u8 *) &input_buf[ 64]);
12356 digest[5] = hex_to_u64 ((const u8 *) &input_buf[ 80]);
12357 digest[6] = hex_to_u64 ((const u8 *) &input_buf[ 96]);
12358 digest[7] = hex_to_u64 ((const u8 *) &input_buf[112]);
12359
12360 digest[0] -= SHA512M_A;
12361 digest[1] -= SHA512M_B;
12362 digest[2] -= SHA512M_C;
12363 digest[3] -= SHA512M_D;
12364 digest[4] -= SHA512M_E;
12365 digest[5] -= SHA512M_F;
12366 digest[6] -= SHA512M_G;
12367 digest[7] -= SHA512M_H;
12368
12369 return (PARSER_OK);
12370 }
12371
12372 int sha512s_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12373 {
12374 if (data.opts_type & OPTS_TYPE_ST_HEX)
12375 {
12376 if ((input_len < DISPLAY_LEN_MIN_1710H) || (input_len > DISPLAY_LEN_MAX_1710H)) return (PARSER_GLOBAL_LENGTH);
12377 }
12378 else
12379 {
12380 if ((input_len < DISPLAY_LEN_MIN_1710) || (input_len > DISPLAY_LEN_MAX_1710)) return (PARSER_GLOBAL_LENGTH);
12381 }
12382
12383 u64 *digest = (u64 *) hash_buf->digest;
12384
12385 salt_t *salt = hash_buf->salt;
12386
12387 digest[0] = hex_to_u64 ((const u8 *) &input_buf[ 0]);
12388 digest[1] = hex_to_u64 ((const u8 *) &input_buf[ 16]);
12389 digest[2] = hex_to_u64 ((const u8 *) &input_buf[ 32]);
12390 digest[3] = hex_to_u64 ((const u8 *) &input_buf[ 48]);
12391 digest[4] = hex_to_u64 ((const u8 *) &input_buf[ 64]);
12392 digest[5] = hex_to_u64 ((const u8 *) &input_buf[ 80]);
12393 digest[6] = hex_to_u64 ((const u8 *) &input_buf[ 96]);
12394 digest[7] = hex_to_u64 ((const u8 *) &input_buf[112]);
12395
12396 digest[0] -= SHA512M_A;
12397 digest[1] -= SHA512M_B;
12398 digest[2] -= SHA512M_C;
12399 digest[3] -= SHA512M_D;
12400 digest[4] -= SHA512M_E;
12401 digest[5] -= SHA512M_F;
12402 digest[6] -= SHA512M_G;
12403 digest[7] -= SHA512M_H;
12404
12405 if (input_buf[128] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
12406
12407 uint salt_len = input_len - 128 - 1;
12408
12409 char *salt_buf = input_buf + 128 + 1;
12410
12411 char *salt_buf_ptr = (char *) salt->salt_buf;
12412
12413 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
12414
12415 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
12416
12417 salt->salt_len = salt_len;
12418
12419 return (PARSER_OK);
12420 }
12421
12422 int sha512crypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12423 {
12424 if (memcmp (SIGNATURE_SHA512CRYPT, input_buf, 3)) return (PARSER_SIGNATURE_UNMATCHED);
12425
12426 u64 *digest = (u64 *) hash_buf->digest;
12427
12428 salt_t *salt = hash_buf->salt;
12429
12430 char *salt_pos = input_buf + 3;
12431
12432 uint iterations_len = 0;
12433
12434 if (memcmp (salt_pos, "rounds=", 7) == 0)
12435 {
12436 salt_pos += 7;
12437
12438 for (iterations_len = 0; salt_pos[0] >= '0' && salt_pos[0] <= '9' && iterations_len < 7; iterations_len++, salt_pos += 1) continue;
12439
12440 if (iterations_len == 0 ) return (PARSER_SALT_ITERATION);
12441 if (salt_pos[0] != '$') return (PARSER_SIGNATURE_UNMATCHED);
12442
12443 salt_pos[0] = 0x0;
12444
12445 salt->salt_iter = atoi (salt_pos - iterations_len);
12446
12447 salt_pos += 1;
12448
12449 iterations_len += 8;
12450 }
12451 else
12452 {
12453 salt->salt_iter = ROUNDS_SHA512CRYPT;
12454 }
12455
12456 if ((input_len < DISPLAY_LEN_MIN_1800) || (input_len > DISPLAY_LEN_MAX_1800 + iterations_len)) return (PARSER_GLOBAL_LENGTH);
12457
12458 char *hash_pos = strchr (salt_pos, '$');
12459
12460 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12461
12462 uint salt_len = hash_pos - salt_pos;
12463
12464 if (salt_len > 16) return (PARSER_SALT_LENGTH);
12465
12466 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
12467
12468 salt->salt_len = salt_len;
12469
12470 hash_pos++;
12471
12472 sha512crypt_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
12473
12474 return (PARSER_OK);
12475 }
12476
12477 int keccak_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12478 {
12479 if ((input_len < DISPLAY_LEN_MIN_5000) || (input_len > DISPLAY_LEN_MAX_5000)) return (PARSER_GLOBAL_LENGTH);
12480
12481 if (input_len % 16) return (PARSER_GLOBAL_LENGTH);
12482
12483 u64 *digest = (u64 *) hash_buf->digest;
12484
12485 salt_t *salt = hash_buf->salt;
12486
12487 uint keccak_mdlen = input_len / 2;
12488
12489 for (uint i = 0; i < keccak_mdlen / 8; i++)
12490 {
12491 digest[i] = hex_to_u64 ((const u8 *) &input_buf[i * 16]);
12492
12493 digest[i] = byte_swap_64 (digest[i]);
12494 }
12495
12496 salt->keccak_mdlen = keccak_mdlen;
12497
12498 return (PARSER_OK);
12499 }
12500
12501 int ikepsk_md5_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12502 {
12503 if ((input_len < DISPLAY_LEN_MIN_5300) || (input_len > DISPLAY_LEN_MAX_5300)) return (PARSER_GLOBAL_LENGTH);
12504
12505 u32 *digest = (u32 *) hash_buf->digest;
12506
12507 salt_t *salt = hash_buf->salt;
12508
12509 ikepsk_t *ikepsk = (ikepsk_t *) hash_buf->esalt;
12510
12511 /**
12512 * Parse that strange long line
12513 */
12514
12515 char *in_off[9];
12516
12517 size_t in_len[9] = { 0 };
12518
12519 in_off[0] = strtok (input_buf, ":");
12520
12521 if (in_off[0] == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12522
12523 in_len[0] = strlen (in_off[0]);
12524
12525 size_t i;
12526
12527 for (i = 1; i < 9; i++)
12528 {
12529 in_off[i] = strtok (NULL, ":");
12530
12531 if (in_off[i] == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12532
12533 in_len[i] = strlen (in_off[i]);
12534 }
12535
12536 char *ptr = (char *) ikepsk->msg_buf;
12537
12538 for (i = 0; i < in_len[0]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[0] + i);
12539 for (i = 0; i < in_len[1]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[1] + i);
12540 for (i = 0; i < in_len[2]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[2] + i);
12541 for (i = 0; i < in_len[3]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[3] + i);
12542 for (i = 0; i < in_len[4]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[4] + i);
12543 for (i = 0; i < in_len[5]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[5] + i);
12544
12545 *ptr = 0x80;
12546
12547 ikepsk->msg_len = (in_len[0] + in_len[1] + in_len[2] + in_len[3] + in_len[4] + in_len[5]) / 2;
12548
12549 ptr = (char *) ikepsk->nr_buf;
12550
12551 for (i = 0; i < in_len[6]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[6] + i);
12552 for (i = 0; i < in_len[7]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[7] + i);
12553
12554 *ptr = 0x80;
12555
12556 ikepsk->nr_len = (in_len[6] + in_len[7]) / 2;
12557
12558 /**
12559 * Store to database
12560 */
12561
12562 ptr = in_off[8];
12563
12564 digest[0] = hex_to_u32 ((const u8 *) &ptr[ 0]);
12565 digest[1] = hex_to_u32 ((const u8 *) &ptr[ 8]);
12566 digest[2] = hex_to_u32 ((const u8 *) &ptr[16]);
12567 digest[3] = hex_to_u32 ((const u8 *) &ptr[24]);
12568
12569 digest[0] = byte_swap_32 (digest[0]);
12570 digest[1] = byte_swap_32 (digest[1]);
12571 digest[2] = byte_swap_32 (digest[2]);
12572 digest[3] = byte_swap_32 (digest[3]);
12573
12574 salt->salt_len = 32;
12575
12576 salt->salt_buf[0] = ikepsk->nr_buf[0];
12577 salt->salt_buf[1] = ikepsk->nr_buf[1];
12578 salt->salt_buf[2] = ikepsk->nr_buf[2];
12579 salt->salt_buf[3] = ikepsk->nr_buf[3];
12580 salt->salt_buf[4] = ikepsk->nr_buf[4];
12581 salt->salt_buf[5] = ikepsk->nr_buf[5];
12582 salt->salt_buf[6] = ikepsk->nr_buf[6];
12583 salt->salt_buf[7] = ikepsk->nr_buf[7];
12584
12585 return (PARSER_OK);
12586 }
12587
12588 int ikepsk_sha1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12589 {
12590 if ((input_len < DISPLAY_LEN_MIN_5400) || (input_len > DISPLAY_LEN_MAX_5400)) return (PARSER_GLOBAL_LENGTH);
12591
12592 u32 *digest = (u32 *) hash_buf->digest;
12593
12594 salt_t *salt = hash_buf->salt;
12595
12596 ikepsk_t *ikepsk = (ikepsk_t *) hash_buf->esalt;
12597
12598 /**
12599 * Parse that strange long line
12600 */
12601
12602 char *in_off[9];
12603
12604 size_t in_len[9] = { 0 };
12605
12606 in_off[0] = strtok (input_buf, ":");
12607
12608 if (in_off[0] == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12609
12610 in_len[0] = strlen (in_off[0]);
12611
12612 size_t i;
12613
12614 for (i = 1; i < 9; i++)
12615 {
12616 in_off[i] = strtok (NULL, ":");
12617
12618 if (in_off[i] == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12619
12620 in_len[i] = strlen (in_off[i]);
12621 }
12622
12623 char *ptr = (char *) ikepsk->msg_buf;
12624
12625 for (i = 0; i < in_len[0]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[0] + i);
12626 for (i = 0; i < in_len[1]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[1] + i);
12627 for (i = 0; i < in_len[2]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[2] + i);
12628 for (i = 0; i < in_len[3]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[3] + i);
12629 for (i = 0; i < in_len[4]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[4] + i);
12630 for (i = 0; i < in_len[5]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[5] + i);
12631
12632 *ptr = 0x80;
12633
12634 ikepsk->msg_len = (in_len[0] + in_len[1] + in_len[2] + in_len[3] + in_len[4] + in_len[5]) / 2;
12635
12636 ptr = (char *) ikepsk->nr_buf;
12637
12638 for (i = 0; i < in_len[6]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[6] + i);
12639 for (i = 0; i < in_len[7]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[7] + i);
12640
12641 *ptr = 0x80;
12642
12643 ikepsk->nr_len = (in_len[6] + in_len[7]) / 2;
12644
12645 /**
12646 * Store to database
12647 */
12648
12649 ptr = in_off[8];
12650
12651 digest[0] = hex_to_u32 ((const u8 *) &ptr[ 0]);
12652 digest[1] = hex_to_u32 ((const u8 *) &ptr[ 8]);
12653 digest[2] = hex_to_u32 ((const u8 *) &ptr[16]);
12654 digest[3] = hex_to_u32 ((const u8 *) &ptr[24]);
12655 digest[4] = hex_to_u32 ((const u8 *) &ptr[32]);
12656
12657 salt->salt_len = 32;
12658
12659 salt->salt_buf[0] = ikepsk->nr_buf[0];
12660 salt->salt_buf[1] = ikepsk->nr_buf[1];
12661 salt->salt_buf[2] = ikepsk->nr_buf[2];
12662 salt->salt_buf[3] = ikepsk->nr_buf[3];
12663 salt->salt_buf[4] = ikepsk->nr_buf[4];
12664 salt->salt_buf[5] = ikepsk->nr_buf[5];
12665 salt->salt_buf[6] = ikepsk->nr_buf[6];
12666 salt->salt_buf[7] = ikepsk->nr_buf[7];
12667
12668 return (PARSER_OK);
12669 }
12670
12671 int ripemd160_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12672 {
12673 if ((input_len < DISPLAY_LEN_MIN_6000) || (input_len > DISPLAY_LEN_MAX_6000)) return (PARSER_GLOBAL_LENGTH);
12674
12675 u32 *digest = (u32 *) hash_buf->digest;
12676
12677 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
12678 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
12679 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
12680 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
12681 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
12682
12683 digest[0] = byte_swap_32 (digest[0]);
12684 digest[1] = byte_swap_32 (digest[1]);
12685 digest[2] = byte_swap_32 (digest[2]);
12686 digest[3] = byte_swap_32 (digest[3]);
12687 digest[4] = byte_swap_32 (digest[4]);
12688
12689 return (PARSER_OK);
12690 }
12691
12692 int whirlpool_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12693 {
12694 if ((input_len < DISPLAY_LEN_MIN_6100) || (input_len > DISPLAY_LEN_MAX_6100)) return (PARSER_GLOBAL_LENGTH);
12695
12696 u32 *digest = (u32 *) hash_buf->digest;
12697
12698 digest[ 0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
12699 digest[ 1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
12700 digest[ 2] = hex_to_u32 ((const u8 *) &input_buf[ 16]);
12701 digest[ 3] = hex_to_u32 ((const u8 *) &input_buf[ 24]);
12702 digest[ 4] = hex_to_u32 ((const u8 *) &input_buf[ 32]);
12703 digest[ 5] = hex_to_u32 ((const u8 *) &input_buf[ 40]);
12704 digest[ 6] = hex_to_u32 ((const u8 *) &input_buf[ 48]);
12705 digest[ 7] = hex_to_u32 ((const u8 *) &input_buf[ 56]);
12706 digest[ 8] = hex_to_u32 ((const u8 *) &input_buf[ 64]);
12707 digest[ 9] = hex_to_u32 ((const u8 *) &input_buf[ 72]);
12708 digest[10] = hex_to_u32 ((const u8 *) &input_buf[ 80]);
12709 digest[11] = hex_to_u32 ((const u8 *) &input_buf[ 88]);
12710 digest[12] = hex_to_u32 ((const u8 *) &input_buf[ 96]);
12711 digest[13] = hex_to_u32 ((const u8 *) &input_buf[104]);
12712 digest[14] = hex_to_u32 ((const u8 *) &input_buf[112]);
12713 digest[15] = hex_to_u32 ((const u8 *) &input_buf[120]);
12714
12715 return (PARSER_OK);
12716 }
12717
12718 int androidpin_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12719 {
12720 if ((input_len < DISPLAY_LEN_MIN_5800) || (input_len > DISPLAY_LEN_MAX_5800)) return (PARSER_GLOBAL_LENGTH);
12721
12722 u32 *digest = (u32 *) hash_buf->digest;
12723
12724 salt_t *salt = hash_buf->salt;
12725
12726 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
12727 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
12728 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
12729 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
12730 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
12731
12732 if (input_buf[40] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
12733
12734 uint salt_len = input_len - 40 - 1;
12735
12736 char *salt_buf = input_buf + 40 + 1;
12737
12738 char *salt_buf_ptr = (char *) salt->salt_buf;
12739
12740 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
12741
12742 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
12743
12744 salt->salt_len = salt_len;
12745
12746 salt->salt_iter = ROUNDS_ANDROIDPIN - 1;
12747
12748 return (PARSER_OK);
12749 }
12750
12751 int truecrypt_parse_hash_1k (char *input_buf, uint input_len, hash_t *hash_buf)
12752 {
12753 u32 *digest = (u32 *) hash_buf->digest;
12754
12755 salt_t *salt = hash_buf->salt;
12756
12757 tc_t *tc = (tc_t *) hash_buf->esalt;
12758
12759 if (input_len == 0)
12760 {
12761 log_error ("TrueCrypt container not specified");
12762
12763 exit (-1);
12764 }
12765
12766 FILE *fp = fopen (input_buf, "rb");
12767
12768 if (fp == NULL)
12769 {
12770 log_error ("%s: %s", input_buf, strerror (errno));
12771
12772 exit (-1);
12773 }
12774
12775 char buf[512] = { 0 };
12776
12777 int n = fread (buf, 1, sizeof (buf), fp);
12778
12779 fclose (fp);
12780
12781 if (n != 512) return (PARSER_TC_FILE_SIZE);
12782
12783 memcpy (tc->salt_buf, buf, 64);
12784
12785 memcpy (tc->data_buf, buf + 64, 512 - 64);
12786
12787 salt->salt_buf[0] = tc->salt_buf[0];
12788
12789 salt->salt_len = 4;
12790
12791 salt->salt_iter = 1000 - 1;
12792
12793 digest[0] = tc->data_buf[0];
12794
12795 return (PARSER_OK);
12796 }
12797
12798 int truecrypt_parse_hash_2k (char *input_buf, uint input_len, hash_t *hash_buf)
12799 {
12800 u32 *digest = (u32 *) hash_buf->digest;
12801
12802 salt_t *salt = hash_buf->salt;
12803
12804 tc_t *tc = (tc_t *) hash_buf->esalt;
12805
12806 if (input_len == 0)
12807 {
12808 log_error ("TrueCrypt container not specified");
12809
12810 exit (-1);
12811 }
12812
12813 FILE *fp = fopen (input_buf, "rb");
12814
12815 if (fp == NULL)
12816 {
12817 log_error ("%s: %s", input_buf, strerror (errno));
12818
12819 exit (-1);
12820 }
12821
12822 char buf[512] = { 0 };
12823
12824 int n = fread (buf, 1, sizeof (buf), fp);
12825
12826 fclose (fp);
12827
12828 if (n != 512) return (PARSER_TC_FILE_SIZE);
12829
12830 memcpy (tc->salt_buf, buf, 64);
12831
12832 memcpy (tc->data_buf, buf + 64, 512 - 64);
12833
12834 salt->salt_buf[0] = tc->salt_buf[0];
12835
12836 salt->salt_len = 4;
12837
12838 salt->salt_iter = 2000 - 1;
12839
12840 digest[0] = tc->data_buf[0];
12841
12842 return (PARSER_OK);
12843 }
12844
12845 int md5aix_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12846 {
12847 if ((input_len < DISPLAY_LEN_MIN_6300) || (input_len > DISPLAY_LEN_MAX_6300)) return (PARSER_GLOBAL_LENGTH);
12848
12849 if (memcmp (SIGNATURE_MD5AIX, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
12850
12851 u32 *digest = (u32 *) hash_buf->digest;
12852
12853 salt_t *salt = hash_buf->salt;
12854
12855 char *salt_pos = input_buf + 6;
12856
12857 char *hash_pos = strchr (salt_pos, '$');
12858
12859 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12860
12861 uint salt_len = hash_pos - salt_pos;
12862
12863 if (salt_len < 8) return (PARSER_SALT_LENGTH);
12864
12865 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
12866
12867 salt->salt_len = salt_len;
12868
12869 salt->salt_iter = 1000;
12870
12871 hash_pos++;
12872
12873 md5crypt_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
12874
12875 return (PARSER_OK);
12876 }
12877
12878 int sha1aix_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12879 {
12880 if ((input_len < DISPLAY_LEN_MIN_6700) || (input_len > DISPLAY_LEN_MAX_6700)) return (PARSER_GLOBAL_LENGTH);
12881
12882 if (memcmp (SIGNATURE_SHA1AIX, input_buf, 7)) return (PARSER_SIGNATURE_UNMATCHED);
12883
12884 u32 *digest = (u32 *) hash_buf->digest;
12885
12886 salt_t *salt = hash_buf->salt;
12887
12888 char *iter_pos = input_buf + 7;
12889
12890 char *salt_pos = strchr (iter_pos, '$');
12891
12892 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12893
12894 salt_pos++;
12895
12896 char *hash_pos = strchr (salt_pos, '$');
12897
12898 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12899
12900 uint salt_len = hash_pos - salt_pos;
12901
12902 if (salt_len < 16) return (PARSER_SALT_LENGTH);
12903
12904 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
12905
12906 salt->salt_len = salt_len;
12907
12908 char salt_iter[3] = { iter_pos[0], iter_pos[1], 0 };
12909
12910 salt->salt_sign[0] = atoi (salt_iter);
12911
12912 salt->salt_iter = (1 << atoi (salt_iter)) - 1;
12913
12914 hash_pos++;
12915
12916 sha1aix_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
12917
12918 digest[0] = byte_swap_32 (digest[0]);
12919 digest[1] = byte_swap_32 (digest[1]);
12920 digest[2] = byte_swap_32 (digest[2]);
12921 digest[3] = byte_swap_32 (digest[3]);
12922 digest[4] = byte_swap_32 (digest[4]);
12923
12924 return (PARSER_OK);
12925 }
12926
12927 int sha256aix_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12928 {
12929 if ((input_len < DISPLAY_LEN_MIN_6400) || (input_len > DISPLAY_LEN_MAX_6400)) return (PARSER_GLOBAL_LENGTH);
12930
12931 if (memcmp (SIGNATURE_SHA256AIX, input_buf, 9)) return (PARSER_SIGNATURE_UNMATCHED);
12932
12933 u32 *digest = (u32 *) hash_buf->digest;
12934
12935 salt_t *salt = hash_buf->salt;
12936
12937 char *iter_pos = input_buf + 9;
12938
12939 char *salt_pos = strchr (iter_pos, '$');
12940
12941 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12942
12943 salt_pos++;
12944
12945 char *hash_pos = strchr (salt_pos, '$');
12946
12947 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12948
12949 uint salt_len = hash_pos - salt_pos;
12950
12951 if (salt_len < 16) return (PARSER_SALT_LENGTH);
12952
12953 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
12954
12955 salt->salt_len = salt_len;
12956
12957 char salt_iter[3] = { iter_pos[0], iter_pos[1], 0 };
12958
12959 salt->salt_sign[0] = atoi (salt_iter);
12960
12961 salt->salt_iter = (1 << atoi (salt_iter)) - 1;
12962
12963 hash_pos++;
12964
12965 sha256aix_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
12966
12967 digest[0] = byte_swap_32 (digest[0]);
12968 digest[1] = byte_swap_32 (digest[1]);
12969 digest[2] = byte_swap_32 (digest[2]);
12970 digest[3] = byte_swap_32 (digest[3]);
12971 digest[4] = byte_swap_32 (digest[4]);
12972 digest[5] = byte_swap_32 (digest[5]);
12973 digest[6] = byte_swap_32 (digest[6]);
12974 digest[7] = byte_swap_32 (digest[7]);
12975
12976 return (PARSER_OK);
12977 }
12978
12979 int sha512aix_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12980 {
12981 if ((input_len < DISPLAY_LEN_MIN_6500) || (input_len > DISPLAY_LEN_MAX_6500)) return (PARSER_GLOBAL_LENGTH);
12982
12983 if (memcmp (SIGNATURE_SHA512AIX, input_buf, 9)) return (PARSER_SIGNATURE_UNMATCHED);
12984
12985 u64 *digest = (u64 *) hash_buf->digest;
12986
12987 salt_t *salt = hash_buf->salt;
12988
12989 char *iter_pos = input_buf + 9;
12990
12991 char *salt_pos = strchr (iter_pos, '$');
12992
12993 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12994
12995 salt_pos++;
12996
12997 char *hash_pos = strchr (salt_pos, '$');
12998
12999 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13000
13001 uint salt_len = hash_pos - salt_pos;
13002
13003 if (salt_len < 16) return (PARSER_SALT_LENGTH);
13004
13005 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
13006
13007 salt->salt_len = salt_len;
13008
13009 char salt_iter[3] = { iter_pos[0], iter_pos[1], 0 };
13010
13011 salt->salt_sign[0] = atoi (salt_iter);
13012
13013 salt->salt_iter = (1 << atoi (salt_iter)) - 1;
13014
13015 hash_pos++;
13016
13017 sha512aix_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
13018
13019 digest[0] = byte_swap_64 (digest[0]);
13020 digest[1] = byte_swap_64 (digest[1]);
13021 digest[2] = byte_swap_64 (digest[2]);
13022 digest[3] = byte_swap_64 (digest[3]);
13023 digest[4] = byte_swap_64 (digest[4]);
13024 digest[5] = byte_swap_64 (digest[5]);
13025 digest[6] = byte_swap_64 (digest[6]);
13026 digest[7] = byte_swap_64 (digest[7]);
13027
13028 return (PARSER_OK);
13029 }
13030
13031 int agilekey_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13032 {
13033 if ((input_len < DISPLAY_LEN_MIN_6600) || (input_len > DISPLAY_LEN_MAX_6600)) return (PARSER_GLOBAL_LENGTH);
13034
13035 u32 *digest = (u32 *) hash_buf->digest;
13036
13037 salt_t *salt = hash_buf->salt;
13038
13039 agilekey_t *agilekey = (agilekey_t *) hash_buf->esalt;
13040
13041 /**
13042 * parse line
13043 */
13044
13045 char *iterations_pos = input_buf;
13046
13047 char *saltbuf_pos = strchr (iterations_pos, ':');
13048
13049 if (saltbuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13050
13051 uint iterations_len = saltbuf_pos - iterations_pos;
13052
13053 if (iterations_len > 6) return (PARSER_SALT_LENGTH);
13054
13055 saltbuf_pos++;
13056
13057 char *cipherbuf_pos = strchr (saltbuf_pos, ':');
13058
13059 if (cipherbuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13060
13061 uint saltbuf_len = cipherbuf_pos - saltbuf_pos;
13062
13063 if (saltbuf_len != 16) return (PARSER_SALT_LENGTH);
13064
13065 uint cipherbuf_len = input_len - iterations_len - 1 - saltbuf_len - 1;
13066
13067 if (cipherbuf_len != 2080) return (PARSER_HASH_LENGTH);
13068
13069 cipherbuf_pos++;
13070
13071 /**
13072 * pbkdf2 iterations
13073 */
13074
13075 salt->salt_iter = atoi (iterations_pos) - 1;
13076
13077 /**
13078 * handle salt encoding
13079 */
13080
13081 char *saltbuf_ptr = (char *) salt->salt_buf;
13082
13083 for (uint i = 0; i < saltbuf_len; i += 2)
13084 {
13085 const char p0 = saltbuf_pos[i + 0];
13086 const char p1 = saltbuf_pos[i + 1];
13087
13088 *saltbuf_ptr++ = hex_convert (p1) << 0
13089 | hex_convert (p0) << 4;
13090 }
13091
13092 salt->salt_len = saltbuf_len / 2;
13093
13094 /**
13095 * handle cipher encoding
13096 */
13097
13098 uint *tmp = (uint *) mymalloc (32);
13099
13100 char *cipherbuf_ptr = (char *) tmp;
13101
13102 for (uint i = 2016; i < cipherbuf_len; i += 2)
13103 {
13104 const char p0 = cipherbuf_pos[i + 0];
13105 const char p1 = cipherbuf_pos[i + 1];
13106
13107 *cipherbuf_ptr++ = hex_convert (p1) << 0
13108 | hex_convert (p0) << 4;
13109 }
13110
13111 // iv is stored at salt_buf 4 (length 16)
13112 // data is stored at salt_buf 8 (length 16)
13113
13114 salt->salt_buf[ 4] = byte_swap_32 (tmp[0]);
13115 salt->salt_buf[ 5] = byte_swap_32 (tmp[1]);
13116 salt->salt_buf[ 6] = byte_swap_32 (tmp[2]);
13117 salt->salt_buf[ 7] = byte_swap_32 (tmp[3]);
13118
13119 salt->salt_buf[ 8] = byte_swap_32 (tmp[4]);
13120 salt->salt_buf[ 9] = byte_swap_32 (tmp[5]);
13121 salt->salt_buf[10] = byte_swap_32 (tmp[6]);
13122 salt->salt_buf[11] = byte_swap_32 (tmp[7]);
13123
13124 free (tmp);
13125
13126 for (uint i = 0, j = 0; i < 1040; i += 1, j += 2)
13127 {
13128 const char p0 = cipherbuf_pos[j + 0];
13129 const char p1 = cipherbuf_pos[j + 1];
13130
13131 agilekey->cipher[i] = hex_convert (p1) << 0
13132 | hex_convert (p0) << 4;
13133 }
13134
13135 /**
13136 * digest buf
13137 */
13138
13139 digest[0] = 0x10101010;
13140 digest[1] = 0x10101010;
13141 digest[2] = 0x10101010;
13142 digest[3] = 0x10101010;
13143
13144 return (PARSER_OK);
13145 }
13146
13147 int lastpass_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13148 {
13149 if ((input_len < DISPLAY_LEN_MIN_6800) || (input_len > DISPLAY_LEN_MAX_6800)) return (PARSER_GLOBAL_LENGTH);
13150
13151 u32 *digest = (u32 *) hash_buf->digest;
13152
13153 salt_t *salt = hash_buf->salt;
13154
13155 char *hashbuf_pos = input_buf;
13156
13157 char *iterations_pos = strchr (hashbuf_pos, ':');
13158
13159 if (iterations_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13160
13161 uint hash_len = iterations_pos - hashbuf_pos;
13162
13163 if ((hash_len != 32) && (hash_len != 64)) return (PARSER_HASH_LENGTH);
13164
13165 iterations_pos++;
13166
13167 char *saltbuf_pos = strchr (iterations_pos, ':');
13168
13169 if (saltbuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13170
13171 uint iterations_len = saltbuf_pos - iterations_pos;
13172
13173 saltbuf_pos++;
13174
13175 uint salt_len = input_len - hash_len - 1 - iterations_len - 1;
13176
13177 if (salt_len > 32) return (PARSER_SALT_LENGTH);
13178
13179 char *salt_buf_ptr = (char *) salt->salt_buf;
13180
13181 salt_len = parse_and_store_salt (salt_buf_ptr, saltbuf_pos, salt_len);
13182
13183 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
13184
13185 salt->salt_len = salt_len;
13186
13187 salt->salt_iter = atoi (iterations_pos) - 1;
13188
13189 digest[0] = hex_to_u32 ((const u8 *) &hashbuf_pos[ 0]);
13190 digest[1] = hex_to_u32 ((const u8 *) &hashbuf_pos[ 8]);
13191 digest[2] = hex_to_u32 ((const u8 *) &hashbuf_pos[16]);
13192 digest[3] = hex_to_u32 ((const u8 *) &hashbuf_pos[24]);
13193
13194 return (PARSER_OK);
13195 }
13196
13197 int gost_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13198 {
13199 if ((input_len < DISPLAY_LEN_MIN_6900) || (input_len > DISPLAY_LEN_MAX_6900)) return (PARSER_GLOBAL_LENGTH);
13200
13201 u32 *digest = (u32 *) hash_buf->digest;
13202
13203 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
13204 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
13205 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
13206 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
13207 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
13208 digest[5] = hex_to_u32 ((const u8 *) &input_buf[40]);
13209 digest[6] = hex_to_u32 ((const u8 *) &input_buf[48]);
13210 digest[7] = hex_to_u32 ((const u8 *) &input_buf[56]);
13211
13212 digest[0] = byte_swap_32 (digest[0]);
13213 digest[1] = byte_swap_32 (digest[1]);
13214 digest[2] = byte_swap_32 (digest[2]);
13215 digest[3] = byte_swap_32 (digest[3]);
13216 digest[4] = byte_swap_32 (digest[4]);
13217 digest[5] = byte_swap_32 (digest[5]);
13218 digest[6] = byte_swap_32 (digest[6]);
13219 digest[7] = byte_swap_32 (digest[7]);
13220
13221 return (PARSER_OK);
13222 }
13223
13224 int sha256crypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13225 {
13226 if (memcmp (SIGNATURE_SHA256CRYPT, input_buf, 3)) return (PARSER_SIGNATURE_UNMATCHED);
13227
13228 u32 *digest = (u32 *) hash_buf->digest;
13229
13230 salt_t *salt = hash_buf->salt;
13231
13232 char *salt_pos = input_buf + 3;
13233
13234 uint iterations_len = 0;
13235
13236 if (memcmp (salt_pos, "rounds=", 7) == 0)
13237 {
13238 salt_pos += 7;
13239
13240 for (iterations_len = 0; salt_pos[0] >= '0' && salt_pos[0] <= '9' && iterations_len < 7; iterations_len++, salt_pos += 1) continue;
13241
13242 if (iterations_len == 0 ) return (PARSER_SALT_ITERATION);
13243 if (salt_pos[0] != '$') return (PARSER_SIGNATURE_UNMATCHED);
13244
13245 salt_pos[0] = 0x0;
13246
13247 salt->salt_iter = atoi (salt_pos - iterations_len);
13248
13249 salt_pos += 1;
13250
13251 iterations_len += 8;
13252 }
13253 else
13254 {
13255 salt->salt_iter = ROUNDS_SHA256CRYPT;
13256 }
13257
13258 if ((input_len < DISPLAY_LEN_MIN_7400) || (input_len > DISPLAY_LEN_MAX_7400 + iterations_len)) return (PARSER_GLOBAL_LENGTH);
13259
13260 char *hash_pos = strchr (salt_pos, '$');
13261
13262 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13263
13264 uint salt_len = hash_pos - salt_pos;
13265
13266 if (salt_len > 16) return (PARSER_SALT_LENGTH);
13267
13268 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
13269
13270 salt->salt_len = salt_len;
13271
13272 hash_pos++;
13273
13274 sha256crypt_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
13275
13276 return (PARSER_OK);
13277 }
13278
13279 int sha512osx_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13280 {
13281 uint max_len = DISPLAY_LEN_MAX_7100 + (2 * 128);
13282
13283 if ((input_len < DISPLAY_LEN_MIN_7100) || (input_len > max_len)) return (PARSER_GLOBAL_LENGTH);
13284
13285 if (memcmp (SIGNATURE_SHA512OSX, input_buf, 4)) return (PARSER_SIGNATURE_UNMATCHED);
13286
13287 u64 *digest = (u64 *) hash_buf->digest;
13288
13289 salt_t *salt = hash_buf->salt;
13290
13291 pbkdf2_sha512_t *pbkdf2_sha512 = (pbkdf2_sha512_t *) hash_buf->esalt;
13292
13293 char *iter_pos = input_buf + 4;
13294
13295 char *salt_pos = strchr (iter_pos, '$');
13296
13297 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13298
13299 salt_pos++;
13300
13301 char *hash_pos = strchr (salt_pos, '$');
13302
13303 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13304
13305 if (((input_len - (hash_pos - input_buf) - 1) % 128) != 0) return (PARSER_GLOBAL_LENGTH);
13306
13307 hash_pos++;
13308
13309 digest[0] = hex_to_u64 ((const u8 *) &hash_pos[ 0]);
13310 digest[1] = hex_to_u64 ((const u8 *) &hash_pos[ 16]);
13311 digest[2] = hex_to_u64 ((const u8 *) &hash_pos[ 32]);
13312 digest[3] = hex_to_u64 ((const u8 *) &hash_pos[ 48]);
13313 digest[4] = hex_to_u64 ((const u8 *) &hash_pos[ 64]);
13314 digest[5] = hex_to_u64 ((const u8 *) &hash_pos[ 80]);
13315 digest[6] = hex_to_u64 ((const u8 *) &hash_pos[ 96]);
13316 digest[7] = hex_to_u64 ((const u8 *) &hash_pos[112]);
13317
13318 uint salt_len = hash_pos - salt_pos - 1;
13319
13320 if ((salt_len % 2) != 0) return (PARSER_SALT_LENGTH);
13321
13322 salt->salt_len = salt_len / 2;
13323
13324 pbkdf2_sha512->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_pos[ 0]);
13325 pbkdf2_sha512->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_pos[ 8]);
13326 pbkdf2_sha512->salt_buf[2] = hex_to_u32 ((const u8 *) &salt_pos[16]);
13327 pbkdf2_sha512->salt_buf[3] = hex_to_u32 ((const u8 *) &salt_pos[24]);
13328 pbkdf2_sha512->salt_buf[4] = hex_to_u32 ((const u8 *) &salt_pos[32]);
13329 pbkdf2_sha512->salt_buf[5] = hex_to_u32 ((const u8 *) &salt_pos[40]);
13330 pbkdf2_sha512->salt_buf[6] = hex_to_u32 ((const u8 *) &salt_pos[48]);
13331 pbkdf2_sha512->salt_buf[7] = hex_to_u32 ((const u8 *) &salt_pos[56]);
13332
13333 pbkdf2_sha512->salt_buf[0] = byte_swap_32 (pbkdf2_sha512->salt_buf[0]);
13334 pbkdf2_sha512->salt_buf[1] = byte_swap_32 (pbkdf2_sha512->salt_buf[1]);
13335 pbkdf2_sha512->salt_buf[2] = byte_swap_32 (pbkdf2_sha512->salt_buf[2]);
13336 pbkdf2_sha512->salt_buf[3] = byte_swap_32 (pbkdf2_sha512->salt_buf[3]);
13337 pbkdf2_sha512->salt_buf[4] = byte_swap_32 (pbkdf2_sha512->salt_buf[4]);
13338 pbkdf2_sha512->salt_buf[5] = byte_swap_32 (pbkdf2_sha512->salt_buf[5]);
13339 pbkdf2_sha512->salt_buf[6] = byte_swap_32 (pbkdf2_sha512->salt_buf[6]);
13340 pbkdf2_sha512->salt_buf[7] = byte_swap_32 (pbkdf2_sha512->salt_buf[7]);
13341 pbkdf2_sha512->salt_buf[8] = 0x01000000;
13342 pbkdf2_sha512->salt_buf[9] = 0x80;
13343
13344 salt->salt_buf[0] = pbkdf2_sha512->salt_buf[0];
13345
13346 salt->salt_iter = atoi (iter_pos) - 1;
13347
13348 return (PARSER_OK);
13349 }
13350
13351 int episerver4_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13352 {
13353 if ((input_len < DISPLAY_LEN_MIN_1441) || (input_len > DISPLAY_LEN_MAX_1441)) return (PARSER_GLOBAL_LENGTH);
13354
13355 if (memcmp (SIGNATURE_EPISERVER4, input_buf, 14)) return (PARSER_SIGNATURE_UNMATCHED);
13356
13357 u32 *digest = (u32 *) hash_buf->digest;
13358
13359 salt_t *salt = hash_buf->salt;
13360
13361 char *salt_pos = input_buf + 14;
13362
13363 char *hash_pos = strchr (salt_pos, '*');
13364
13365 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13366
13367 hash_pos++;
13368
13369 uint salt_len = hash_pos - salt_pos - 1;
13370
13371 char *salt_buf_ptr = (char *) salt->salt_buf;
13372
13373 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
13374
13375 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
13376
13377 salt->salt_len = salt_len;
13378
13379 u8 tmp_buf[100] = { 0 };
13380
13381 base64_decode (base64_to_int, (const u8 *) hash_pos, 43, tmp_buf);
13382
13383 memcpy (digest, tmp_buf, 32);
13384
13385 digest[0] = byte_swap_32 (digest[0]);
13386 digest[1] = byte_swap_32 (digest[1]);
13387 digest[2] = byte_swap_32 (digest[2]);
13388 digest[3] = byte_swap_32 (digest[3]);
13389 digest[4] = byte_swap_32 (digest[4]);
13390 digest[5] = byte_swap_32 (digest[5]);
13391 digest[6] = byte_swap_32 (digest[6]);
13392 digest[7] = byte_swap_32 (digest[7]);
13393
13394 digest[0] -= SHA256M_A;
13395 digest[1] -= SHA256M_B;
13396 digest[2] -= SHA256M_C;
13397 digest[3] -= SHA256M_D;
13398 digest[4] -= SHA256M_E;
13399 digest[5] -= SHA256M_F;
13400 digest[6] -= SHA256M_G;
13401 digest[7] -= SHA256M_H;
13402
13403 return (PARSER_OK);
13404 }
13405
13406 int sha512grub_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13407 {
13408 uint max_len = DISPLAY_LEN_MAX_7200 + (8 * 128);
13409
13410 if ((input_len < DISPLAY_LEN_MIN_7200) || (input_len > max_len)) return (PARSER_GLOBAL_LENGTH);
13411
13412 if (memcmp (SIGNATURE_SHA512GRUB, input_buf, 19)) return (PARSER_SIGNATURE_UNMATCHED);
13413
13414 u64 *digest = (u64 *) hash_buf->digest;
13415
13416 salt_t *salt = hash_buf->salt;
13417
13418 pbkdf2_sha512_t *pbkdf2_sha512 = (pbkdf2_sha512_t *) hash_buf->esalt;
13419
13420 char *iter_pos = input_buf + 19;
13421
13422 char *salt_pos = strchr (iter_pos, '.');
13423
13424 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13425
13426 salt_pos++;
13427
13428 char *hash_pos = strchr (salt_pos, '.');
13429
13430 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13431
13432 if (((input_len - (hash_pos - input_buf) - 1) % 128) != 0) return (PARSER_GLOBAL_LENGTH);
13433
13434 hash_pos++;
13435
13436 digest[0] = hex_to_u64 ((const u8 *) &hash_pos[ 0]);
13437 digest[1] = hex_to_u64 ((const u8 *) &hash_pos[ 16]);
13438 digest[2] = hex_to_u64 ((const u8 *) &hash_pos[ 32]);
13439 digest[3] = hex_to_u64 ((const u8 *) &hash_pos[ 48]);
13440 digest[4] = hex_to_u64 ((const u8 *) &hash_pos[ 64]);
13441 digest[5] = hex_to_u64 ((const u8 *) &hash_pos[ 80]);
13442 digest[6] = hex_to_u64 ((const u8 *) &hash_pos[ 96]);
13443 digest[7] = hex_to_u64 ((const u8 *) &hash_pos[112]);
13444
13445 uint salt_len = hash_pos - salt_pos - 1;
13446
13447 salt_len /= 2;
13448
13449 char *salt_buf_ptr = (char *) pbkdf2_sha512->salt_buf;
13450
13451 uint i;
13452
13453 for (i = 0; i < salt_len; i++)
13454 {
13455 salt_buf_ptr[i] = hex_to_u8 ((const u8 *) &salt_pos[i * 2]);
13456 }
13457
13458 salt_buf_ptr[salt_len + 3] = 0x01;
13459 salt_buf_ptr[salt_len + 4] = 0x80;
13460
13461 salt->salt_buf[0] = pbkdf2_sha512->salt_buf[0];
13462
13463 salt->salt_len = salt_len;
13464
13465 salt->salt_iter = atoi (iter_pos) - 1;
13466
13467 return (PARSER_OK);
13468 }
13469
13470 int sha512b64s_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13471 {
13472 if ((input_len < DISPLAY_LEN_MIN_1711) || (input_len > DISPLAY_LEN_MAX_1711)) return (PARSER_GLOBAL_LENGTH);
13473
13474 if (memcmp (SIGNATURE_SHA512B64S, input_buf, 9)) return (PARSER_SIGNATURE_UNMATCHED);
13475
13476 u64 *digest = (u64 *) hash_buf->digest;
13477
13478 salt_t *salt = hash_buf->salt;
13479
13480 u8 tmp_buf[120] = { 0 };
13481
13482 int tmp_len = base64_decode (base64_to_int, (const u8 *) input_buf + 9, input_len - 9, tmp_buf);
13483
13484 if (tmp_len < 64) return (PARSER_HASH_LENGTH);
13485
13486 memcpy (digest, tmp_buf, 64);
13487
13488 digest[0] = byte_swap_64 (digest[0]);
13489 digest[1] = byte_swap_64 (digest[1]);
13490 digest[2] = byte_swap_64 (digest[2]);
13491 digest[3] = byte_swap_64 (digest[3]);
13492 digest[4] = byte_swap_64 (digest[4]);
13493 digest[5] = byte_swap_64 (digest[5]);
13494 digest[6] = byte_swap_64 (digest[6]);
13495 digest[7] = byte_swap_64 (digest[7]);
13496
13497 digest[0] -= SHA512M_A;
13498 digest[1] -= SHA512M_B;
13499 digest[2] -= SHA512M_C;
13500 digest[3] -= SHA512M_D;
13501 digest[4] -= SHA512M_E;
13502 digest[5] -= SHA512M_F;
13503 digest[6] -= SHA512M_G;
13504 digest[7] -= SHA512M_H;
13505
13506 int salt_len = tmp_len - 64;
13507
13508 if (salt_len < 0) return (PARSER_SALT_LENGTH);
13509
13510 salt->salt_len = salt_len;
13511
13512 memcpy (salt->salt_buf, tmp_buf + 64, salt->salt_len);
13513
13514 if (data.opts_type & OPTS_TYPE_ST_ADD80)
13515 {
13516 char *ptr = (char *) salt->salt_buf;
13517
13518 ptr[salt->salt_len] = 0x80;
13519 }
13520
13521 return (PARSER_OK);
13522 }
13523
13524 int hmacmd5_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13525 {
13526 if (data.opts_type & OPTS_TYPE_ST_HEX)
13527 {
13528 if ((input_len < DISPLAY_LEN_MIN_50H) || (input_len > DISPLAY_LEN_MAX_50H)) return (PARSER_GLOBAL_LENGTH);
13529 }
13530 else
13531 {
13532 if ((input_len < DISPLAY_LEN_MIN_50) || (input_len > DISPLAY_LEN_MAX_50)) return (PARSER_GLOBAL_LENGTH);
13533 }
13534
13535 u32 *digest = (u32 *) hash_buf->digest;
13536
13537 salt_t *salt = hash_buf->salt;
13538
13539 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
13540 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
13541 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
13542 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
13543
13544 digest[0] = byte_swap_32 (digest[0]);
13545 digest[1] = byte_swap_32 (digest[1]);
13546 digest[2] = byte_swap_32 (digest[2]);
13547 digest[3] = byte_swap_32 (digest[3]);
13548
13549 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
13550
13551 uint salt_len = input_len - 32 - 1;
13552
13553 char *salt_buf = input_buf + 32 + 1;
13554
13555 char *salt_buf_ptr = (char *) salt->salt_buf;
13556
13557 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
13558
13559 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
13560
13561 salt->salt_len = salt_len;
13562
13563 return (PARSER_OK);
13564 }
13565
13566 int hmacsha1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13567 {
13568 if (data.opts_type & OPTS_TYPE_ST_HEX)
13569 {
13570 if ((input_len < DISPLAY_LEN_MIN_150H) || (input_len > DISPLAY_LEN_MAX_150H)) return (PARSER_GLOBAL_LENGTH);
13571 }
13572 else
13573 {
13574 if ((input_len < DISPLAY_LEN_MIN_150) || (input_len > DISPLAY_LEN_MAX_150)) return (PARSER_GLOBAL_LENGTH);
13575 }
13576
13577 u32 *digest = (u32 *) hash_buf->digest;
13578
13579 salt_t *salt = hash_buf->salt;
13580
13581 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
13582 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
13583 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
13584 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
13585 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
13586
13587 if (input_buf[40] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
13588
13589 uint salt_len = input_len - 40 - 1;
13590
13591 char *salt_buf = input_buf + 40 + 1;
13592
13593 char *salt_buf_ptr = (char *) salt->salt_buf;
13594
13595 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
13596
13597 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
13598
13599 salt->salt_len = salt_len;
13600
13601 return (PARSER_OK);
13602 }
13603
13604 int hmacsha256_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13605 {
13606 if (data.opts_type & OPTS_TYPE_ST_HEX)
13607 {
13608 if ((input_len < DISPLAY_LEN_MIN_1450H) || (input_len > DISPLAY_LEN_MAX_1450H)) return (PARSER_GLOBAL_LENGTH);
13609 }
13610 else
13611 {
13612 if ((input_len < DISPLAY_LEN_MIN_1450) || (input_len > DISPLAY_LEN_MAX_1450)) return (PARSER_GLOBAL_LENGTH);
13613 }
13614
13615 u32 *digest = (u32 *) hash_buf->digest;
13616
13617 salt_t *salt = hash_buf->salt;
13618
13619 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
13620 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
13621 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
13622 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
13623 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
13624 digest[5] = hex_to_u32 ((const u8 *) &input_buf[40]);
13625 digest[6] = hex_to_u32 ((const u8 *) &input_buf[48]);
13626 digest[7] = hex_to_u32 ((const u8 *) &input_buf[56]);
13627
13628 if (input_buf[64] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
13629
13630 uint salt_len = input_len - 64 - 1;
13631
13632 char *salt_buf = input_buf + 64 + 1;
13633
13634 char *salt_buf_ptr = (char *) salt->salt_buf;
13635
13636 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
13637
13638 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
13639
13640 salt->salt_len = salt_len;
13641
13642 return (PARSER_OK);
13643 }
13644
13645 int hmacsha512_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13646 {
13647 if (data.opts_type & OPTS_TYPE_ST_HEX)
13648 {
13649 if ((input_len < DISPLAY_LEN_MIN_1750H) || (input_len > DISPLAY_LEN_MAX_1750H)) return (PARSER_GLOBAL_LENGTH);
13650 }
13651 else
13652 {
13653 if ((input_len < DISPLAY_LEN_MIN_1750) || (input_len > DISPLAY_LEN_MAX_1750)) return (PARSER_GLOBAL_LENGTH);
13654 }
13655
13656 u64 *digest = (u64 *) hash_buf->digest;
13657
13658 salt_t *salt = hash_buf->salt;
13659
13660 digest[0] = hex_to_u64 ((const u8 *) &input_buf[ 0]);
13661 digest[1] = hex_to_u64 ((const u8 *) &input_buf[ 16]);
13662 digest[2] = hex_to_u64 ((const u8 *) &input_buf[ 32]);
13663 digest[3] = hex_to_u64 ((const u8 *) &input_buf[ 48]);
13664 digest[4] = hex_to_u64 ((const u8 *) &input_buf[ 64]);
13665 digest[5] = hex_to_u64 ((const u8 *) &input_buf[ 80]);
13666 digest[6] = hex_to_u64 ((const u8 *) &input_buf[ 96]);
13667 digest[7] = hex_to_u64 ((const u8 *) &input_buf[112]);
13668
13669 if (input_buf[128] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
13670
13671 uint salt_len = input_len - 128 - 1;
13672
13673 char *salt_buf = input_buf + 128 + 1;
13674
13675 char *salt_buf_ptr = (char *) salt->salt_buf;
13676
13677 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
13678
13679 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
13680
13681 salt->salt_len = salt_len;
13682
13683 return (PARSER_OK);
13684 }
13685
13686 int krb5pa_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13687 {
13688 if ((input_len < DISPLAY_LEN_MIN_7500) || (input_len > DISPLAY_LEN_MAX_7500)) return (PARSER_GLOBAL_LENGTH);
13689
13690 if (memcmp (SIGNATURE_KRB5PA, input_buf, 10)) return (PARSER_SIGNATURE_UNMATCHED);
13691
13692 u32 *digest = (u32 *) hash_buf->digest;
13693
13694 salt_t *salt = hash_buf->salt;
13695
13696 krb5pa_t *krb5pa = (krb5pa_t *) hash_buf->esalt;
13697
13698 /**
13699 * parse line
13700 */
13701
13702 char *user_pos = input_buf + 10 + 1;
13703
13704 char *realm_pos = strchr (user_pos, '$');
13705
13706 if (realm_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13707
13708 uint user_len = realm_pos - user_pos;
13709
13710 if (user_len >= 64) return (PARSER_SALT_LENGTH);
13711
13712 realm_pos++;
13713
13714 char *salt_pos = strchr (realm_pos, '$');
13715
13716 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13717
13718 uint realm_len = salt_pos - realm_pos;
13719
13720 if (realm_len >= 64) return (PARSER_SALT_LENGTH);
13721
13722 salt_pos++;
13723
13724 char *data_pos = strchr (salt_pos, '$');
13725
13726 if (data_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13727
13728 uint salt_len = data_pos - salt_pos;
13729
13730 if (salt_len >= 128) return (PARSER_SALT_LENGTH);
13731
13732 data_pos++;
13733
13734 uint data_len = input_len - 10 - 1 - user_len - 1 - realm_len - 1 - salt_len - 1;
13735
13736 if (data_len != ((36 + 16) * 2)) return (PARSER_SALT_LENGTH);
13737
13738 /**
13739 * copy data
13740 */
13741
13742 memcpy (krb5pa->user, user_pos, user_len);
13743 memcpy (krb5pa->realm, realm_pos, realm_len);
13744 memcpy (krb5pa->salt, salt_pos, salt_len);
13745
13746 char *timestamp_ptr = (char *) krb5pa->timestamp;
13747
13748 for (uint i = 0; i < (36 * 2); i += 2)
13749 {
13750 const char p0 = data_pos[i + 0];
13751 const char p1 = data_pos[i + 1];
13752
13753 *timestamp_ptr++ = hex_convert (p1) << 0
13754 | hex_convert (p0) << 4;
13755 }
13756
13757 char *checksum_ptr = (char *) krb5pa->checksum;
13758
13759 for (uint i = (36 * 2); i < ((36 + 16) * 2); i += 2)
13760 {
13761 const char p0 = data_pos[i + 0];
13762 const char p1 = data_pos[i + 1];
13763
13764 *checksum_ptr++ = hex_convert (p1) << 0
13765 | hex_convert (p0) << 4;
13766 }
13767
13768 /**
13769 * copy some data to generic buffers to make sorting happy
13770 */
13771
13772 salt->salt_buf[0] = krb5pa->timestamp[0];
13773 salt->salt_buf[1] = krb5pa->timestamp[1];
13774 salt->salt_buf[2] = krb5pa->timestamp[2];
13775 salt->salt_buf[3] = krb5pa->timestamp[3];
13776 salt->salt_buf[4] = krb5pa->timestamp[4];
13777 salt->salt_buf[5] = krb5pa->timestamp[5];
13778 salt->salt_buf[6] = krb5pa->timestamp[6];
13779 salt->salt_buf[7] = krb5pa->timestamp[7];
13780 salt->salt_buf[8] = krb5pa->timestamp[8];
13781
13782 salt->salt_len = 36;
13783
13784 digest[0] = krb5pa->checksum[0];
13785 digest[1] = krb5pa->checksum[1];
13786 digest[2] = krb5pa->checksum[2];
13787 digest[3] = krb5pa->checksum[3];
13788
13789 return (PARSER_OK);
13790 }
13791
13792 int sapb_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13793 {
13794 if ((input_len < DISPLAY_LEN_MIN_7700) || (input_len > DISPLAY_LEN_MAX_7700)) return (PARSER_GLOBAL_LENGTH);
13795
13796 u32 *digest = (u32 *) hash_buf->digest;
13797
13798 salt_t *salt = hash_buf->salt;
13799
13800 /**
13801 * parse line
13802 */
13803
13804 char *salt_pos = input_buf;
13805
13806 char *hash_pos = strchr (salt_pos, '$');
13807
13808 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13809
13810 uint salt_len = hash_pos - salt_pos;
13811
13812 if (salt_len >= 40) return (PARSER_SALT_LENGTH);
13813
13814 hash_pos++;
13815
13816 uint hash_len = input_len - 1 - salt_len;
13817
13818 if (hash_len != 16) return (PARSER_HASH_LENGTH);
13819
13820 /**
13821 * valid some data
13822 */
13823
13824 uint user_len = 0;
13825
13826 for (uint i = 0; i < salt_len; i++)
13827 {
13828 if (salt_pos[i] == ' ') continue;
13829
13830 user_len++;
13831 }
13832
13833 // SAP user names cannot be longer than 12 characters
13834 if (user_len > 12) return (PARSER_SALT_LENGTH);
13835
13836 // SAP user name cannot start with ! or ?
13837 if (salt_pos[0] == '!' || salt_pos[0] == '?') return (PARSER_SALT_VALUE);
13838
13839 /**
13840 * copy data
13841 */
13842
13843 char *salt_buf_ptr = (char *) salt->salt_buf;
13844
13845 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
13846
13847 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
13848
13849 salt->salt_len = salt_len;
13850
13851 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[0]);
13852 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[8]);
13853 digest[2] = 0;
13854 digest[3] = 0;
13855
13856 digest[0] = byte_swap_32 (digest[0]);
13857 digest[1] = byte_swap_32 (digest[1]);
13858
13859 return (PARSER_OK);
13860 }
13861
13862 int sapg_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13863 {
13864 if ((input_len < DISPLAY_LEN_MIN_7800) || (input_len > DISPLAY_LEN_MAX_7800)) return (PARSER_GLOBAL_LENGTH);
13865
13866 u32 *digest = (u32 *) hash_buf->digest;
13867
13868 salt_t *salt = hash_buf->salt;
13869
13870 /**
13871 * parse line
13872 */
13873
13874 char *salt_pos = input_buf;
13875
13876 char *hash_pos = strchr (salt_pos, '$');
13877
13878 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13879
13880 uint salt_len = hash_pos - salt_pos;
13881
13882 if (salt_len >= 40) return (PARSER_SALT_LENGTH);
13883
13884 hash_pos++;
13885
13886 uint hash_len = input_len - 1 - salt_len;
13887
13888 if (hash_len != 40) return (PARSER_HASH_LENGTH);
13889
13890 /**
13891 * valid some data
13892 */
13893
13894 uint user_len = 0;
13895
13896 for (uint i = 0; i < salt_len; i++)
13897 {
13898 if (salt_pos[i] == ' ') continue;
13899
13900 user_len++;
13901 }
13902
13903 // SAP user names cannot be longer than 12 characters
13904 // this is kinda buggy. if the username is in utf the length can be up to length 12*3
13905 // so far nobody complained so we stay with this because it helps in optimization
13906 // final string can have a max size of 32 (password) + (10 * 5) = lengthMagicArray + 12 (max salt) + 1 (the 0x80)
13907
13908 if (user_len > 12) return (PARSER_SALT_LENGTH);
13909
13910 // SAP user name cannot start with ! or ?
13911 if (salt_pos[0] == '!' || salt_pos[0] == '?') return (PARSER_SALT_VALUE);
13912
13913 /**
13914 * copy data
13915 */
13916
13917 char *salt_buf_ptr = (char *) salt->salt_buf;
13918
13919 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
13920
13921 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
13922
13923 salt->salt_len = salt_len;
13924
13925 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
13926 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
13927 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
13928 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
13929 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
13930
13931 return (PARSER_OK);
13932 }
13933
13934 int drupal7_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13935 {
13936 if ((input_len < DISPLAY_LEN_MIN_7900) || (input_len > DISPLAY_LEN_MAX_7900)) return (PARSER_GLOBAL_LENGTH);
13937
13938 if (memcmp (SIGNATURE_DRUPAL7, input_buf, 3)) return (PARSER_SIGNATURE_UNMATCHED);
13939
13940 u64 *digest = (u64 *) hash_buf->digest;
13941
13942 salt_t *salt = hash_buf->salt;
13943
13944 char *iter_pos = input_buf + 3;
13945
13946 uint salt_iter = 1 << itoa64_to_int (iter_pos[0]);
13947
13948 if (salt_iter > 0x80000000) return (PARSER_SALT_ITERATION);
13949
13950 memcpy ((char *) salt->salt_sign, input_buf, 4);
13951
13952 salt->salt_iter = salt_iter;
13953
13954 char *salt_pos = iter_pos + 1;
13955
13956 uint salt_len = 8;
13957
13958 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
13959
13960 salt->salt_len = salt_len;
13961
13962 char *hash_pos = salt_pos + salt_len;
13963
13964 drupal7_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
13965
13966 // ugly hack start
13967
13968 char *tmp = (char *) salt->salt_buf_pc;
13969
13970 tmp[0] = hash_pos[42];
13971
13972 // ugly hack end
13973
13974 digest[ 0] = byte_swap_64 (digest[ 0]);
13975 digest[ 1] = byte_swap_64 (digest[ 1]);
13976 digest[ 2] = byte_swap_64 (digest[ 2]);
13977 digest[ 3] = byte_swap_64 (digest[ 3]);
13978 digest[ 4] = 0;
13979 digest[ 5] = 0;
13980 digest[ 6] = 0;
13981 digest[ 7] = 0;
13982
13983 return (PARSER_OK);
13984 }
13985
13986 int sybasease_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13987 {
13988 if ((input_len < DISPLAY_LEN_MIN_8000) || (input_len > DISPLAY_LEN_MAX_8000)) return (PARSER_GLOBAL_LENGTH);
13989
13990 if (memcmp (SIGNATURE_SYBASEASE, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
13991
13992 u32 *digest = (u32 *) hash_buf->digest;
13993
13994 salt_t *salt = hash_buf->salt;
13995
13996 char *salt_buf = input_buf + 6;
13997
13998 uint salt_len = 16;
13999
14000 char *salt_buf_ptr = (char *) salt->salt_buf;
14001
14002 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
14003
14004 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
14005
14006 salt->salt_len = salt_len;
14007
14008 char *hash_pos = input_buf + 6 + 16;
14009
14010 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
14011 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
14012 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
14013 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
14014 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
14015 digest[5] = hex_to_u32 ((const u8 *) &hash_pos[40]);
14016 digest[6] = hex_to_u32 ((const u8 *) &hash_pos[48]);
14017 digest[7] = hex_to_u32 ((const u8 *) &hash_pos[56]);
14018
14019 return (PARSER_OK);
14020 }
14021
14022 int mysql323_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14023 {
14024 if ((input_len < DISPLAY_LEN_MIN_200) || (input_len > DISPLAY_LEN_MAX_200)) return (PARSER_GLOBAL_LENGTH);
14025
14026 u32 *digest = (u32 *) hash_buf->digest;
14027
14028 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
14029 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
14030 digest[2] = 0;
14031 digest[3] = 0;
14032
14033 return (PARSER_OK);
14034 }
14035
14036 int rakp_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14037 {
14038 if ((input_len < DISPLAY_LEN_MIN_7300) || (input_len > DISPLAY_LEN_MAX_7300)) return (PARSER_GLOBAL_LENGTH);
14039
14040 u32 *digest = (u32 *) hash_buf->digest;
14041
14042 salt_t *salt = hash_buf->salt;
14043
14044 rakp_t *rakp = (rakp_t *) hash_buf->esalt;
14045
14046 char *saltbuf_pos = input_buf;
14047
14048 char *hashbuf_pos = strchr (saltbuf_pos, ':');
14049
14050 if (hashbuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14051
14052 uint saltbuf_len = hashbuf_pos - saltbuf_pos;
14053
14054 if (saltbuf_len < 64) return (PARSER_SALT_LENGTH);
14055 if (saltbuf_len > 512) return (PARSER_SALT_LENGTH);
14056
14057 if (saltbuf_len & 1) return (PARSER_SALT_LENGTH); // muss gerade sein wegen hex
14058
14059 hashbuf_pos++;
14060
14061 uint hashbuf_len = input_len - saltbuf_len - 1;
14062
14063 if (hashbuf_len != 40) return (PARSER_HASH_LENGTH);
14064
14065 char *salt_ptr = (char *) saltbuf_pos;
14066 char *rakp_ptr = (char *) rakp->salt_buf;
14067
14068 uint i;
14069 uint j;
14070
14071 for (i = 0, j = 0; i < saltbuf_len; i += 2, j += 1)
14072 {
14073 rakp_ptr[j] = hex_to_u8 ((const u8 *) &salt_ptr[i]);
14074 }
14075
14076 rakp_ptr[j] = 0x80;
14077
14078 rakp->salt_len = j;
14079
14080 for (i = 0; i < 64; i++)
14081 {
14082 rakp->salt_buf[i] = byte_swap_32 (rakp->salt_buf[i]);
14083 }
14084
14085 salt->salt_buf[0] = rakp->salt_buf[0];
14086 salt->salt_buf[1] = rakp->salt_buf[1];
14087 salt->salt_buf[2] = rakp->salt_buf[2];
14088 salt->salt_buf[3] = rakp->salt_buf[3];
14089 salt->salt_buf[4] = rakp->salt_buf[4];
14090 salt->salt_buf[5] = rakp->salt_buf[5];
14091 salt->salt_buf[6] = rakp->salt_buf[6];
14092 salt->salt_buf[7] = rakp->salt_buf[7];
14093
14094 salt->salt_len = 32; // muss min. 32 haben
14095
14096 digest[0] = hex_to_u32 ((const u8 *) &hashbuf_pos[ 0]);
14097 digest[1] = hex_to_u32 ((const u8 *) &hashbuf_pos[ 8]);
14098 digest[2] = hex_to_u32 ((const u8 *) &hashbuf_pos[16]);
14099 digest[3] = hex_to_u32 ((const u8 *) &hashbuf_pos[24]);
14100 digest[4] = hex_to_u32 ((const u8 *) &hashbuf_pos[32]);
14101
14102 return (PARSER_OK);
14103 }
14104
14105 int netscaler_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14106 {
14107 if ((input_len < DISPLAY_LEN_MIN_8100) || (input_len > DISPLAY_LEN_MAX_8100)) return (PARSER_GLOBAL_LENGTH);
14108
14109 u32 *digest = (u32 *) hash_buf->digest;
14110
14111 salt_t *salt = hash_buf->salt;
14112
14113 if (memcmp (SIGNATURE_NETSCALER, input_buf, 1)) return (PARSER_SIGNATURE_UNMATCHED);
14114
14115 char *salt_pos = input_buf + 1;
14116
14117 memcpy (salt->salt_buf, salt_pos, 8);
14118
14119 salt->salt_buf[0] = byte_swap_32 (salt->salt_buf[0]);
14120 salt->salt_buf[1] = byte_swap_32 (salt->salt_buf[1]);
14121
14122 salt->salt_len = 8;
14123
14124 char *hash_pos = salt_pos + 8;
14125
14126 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
14127 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
14128 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
14129 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
14130 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
14131
14132 digest[0] -= SHA1M_A;
14133 digest[1] -= SHA1M_B;
14134 digest[2] -= SHA1M_C;
14135 digest[3] -= SHA1M_D;
14136 digest[4] -= SHA1M_E;
14137
14138 return (PARSER_OK);
14139 }
14140
14141 int chap_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14142 {
14143 if ((input_len < DISPLAY_LEN_MIN_4800) || (input_len > DISPLAY_LEN_MAX_4800)) return (PARSER_GLOBAL_LENGTH);
14144
14145 u32 *digest = (u32 *) hash_buf->digest;
14146
14147 salt_t *salt = hash_buf->salt;
14148
14149 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
14150 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
14151 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
14152 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
14153
14154 digest[0] = byte_swap_32 (digest[0]);
14155 digest[1] = byte_swap_32 (digest[1]);
14156 digest[2] = byte_swap_32 (digest[2]);
14157 digest[3] = byte_swap_32 (digest[3]);
14158
14159 digest[0] -= MD5M_A;
14160 digest[1] -= MD5M_B;
14161 digest[2] -= MD5M_C;
14162 digest[3] -= MD5M_D;
14163
14164 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
14165
14166 char *salt_buf_ptr = input_buf + 32 + 1;
14167
14168 u32 *salt_buf = salt->salt_buf;
14169
14170 salt_buf[0] = hex_to_u32 ((const u8 *) &salt_buf_ptr[ 0]);
14171 salt_buf[1] = hex_to_u32 ((const u8 *) &salt_buf_ptr[ 8]);
14172 salt_buf[2] = hex_to_u32 ((const u8 *) &salt_buf_ptr[16]);
14173 salt_buf[3] = hex_to_u32 ((const u8 *) &salt_buf_ptr[24]);
14174
14175 salt_buf[0] = byte_swap_32 (salt_buf[0]);
14176 salt_buf[1] = byte_swap_32 (salt_buf[1]);
14177 salt_buf[2] = byte_swap_32 (salt_buf[2]);
14178 salt_buf[3] = byte_swap_32 (salt_buf[3]);
14179
14180 salt->salt_len = 16 + 1;
14181
14182 if (input_buf[65] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
14183
14184 char *idbyte_buf_ptr = input_buf + 32 + 1 + 32 + 1;
14185
14186 salt_buf[4] = hex_to_u8 ((const u8 *) &idbyte_buf_ptr[0]) & 0xff;
14187
14188 return (PARSER_OK);
14189 }
14190
14191 int cloudkey_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14192 {
14193 if ((input_len < DISPLAY_LEN_MIN_8200) || (input_len > DISPLAY_LEN_MAX_8200)) return (PARSER_GLOBAL_LENGTH);
14194
14195 u32 *digest = (u32 *) hash_buf->digest;
14196
14197 salt_t *salt = hash_buf->salt;
14198
14199 cloudkey_t *cloudkey = (cloudkey_t *) hash_buf->esalt;
14200
14201 /**
14202 * parse line
14203 */
14204
14205 char *hashbuf_pos = input_buf;
14206
14207 char *saltbuf_pos = strchr (hashbuf_pos, ':');
14208
14209 if (saltbuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14210
14211 const uint hashbuf_len = saltbuf_pos - hashbuf_pos;
14212
14213 if (hashbuf_len != 64) return (PARSER_HASH_LENGTH);
14214
14215 saltbuf_pos++;
14216
14217 char *iteration_pos = strchr (saltbuf_pos, ':');
14218
14219 if (iteration_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14220
14221 const uint saltbuf_len = iteration_pos - saltbuf_pos;
14222
14223 if (saltbuf_len != 32) return (PARSER_SALT_LENGTH);
14224
14225 iteration_pos++;
14226
14227 char *databuf_pos = strchr (iteration_pos, ':');
14228
14229 if (databuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14230
14231 const uint iteration_len = databuf_pos - iteration_pos;
14232
14233 if (iteration_len < 1) return (PARSER_SALT_ITERATION);
14234 if (iteration_len > 8) return (PARSER_SALT_ITERATION);
14235
14236 const uint databuf_len = input_len - hashbuf_len - 1 - saltbuf_len - 1 - iteration_len - 1;
14237
14238 if (databuf_len < 1) return (PARSER_SALT_LENGTH);
14239 if (databuf_len > 2048) return (PARSER_SALT_LENGTH);
14240
14241 databuf_pos++;
14242
14243 // digest
14244
14245 digest[0] = hex_to_u32 ((const u8 *) &hashbuf_pos[ 0]);
14246 digest[1] = hex_to_u32 ((const u8 *) &hashbuf_pos[ 8]);
14247 digest[2] = hex_to_u32 ((const u8 *) &hashbuf_pos[16]);
14248 digest[3] = hex_to_u32 ((const u8 *) &hashbuf_pos[24]);
14249 digest[4] = hex_to_u32 ((const u8 *) &hashbuf_pos[32]);
14250 digest[5] = hex_to_u32 ((const u8 *) &hashbuf_pos[40]);
14251 digest[6] = hex_to_u32 ((const u8 *) &hashbuf_pos[48]);
14252 digest[7] = hex_to_u32 ((const u8 *) &hashbuf_pos[56]);
14253
14254 // salt
14255
14256 char *saltbuf_ptr = (char *) salt->salt_buf;
14257
14258 for (uint i = 0; i < saltbuf_len; i += 2)
14259 {
14260 const char p0 = saltbuf_pos[i + 0];
14261 const char p1 = saltbuf_pos[i + 1];
14262
14263 *saltbuf_ptr++ = hex_convert (p1) << 0
14264 | hex_convert (p0) << 4;
14265 }
14266
14267 salt->salt_buf[4] = 0x01000000;
14268 salt->salt_buf[5] = 0x80;
14269
14270 salt->salt_len = saltbuf_len / 2;
14271
14272 // iteration
14273
14274 salt->salt_iter = atoi (iteration_pos) - 1;
14275
14276 // data
14277
14278 char *databuf_ptr = (char *) cloudkey->data_buf;
14279
14280 for (uint i = 0; i < databuf_len; i += 2)
14281 {
14282 const char p0 = databuf_pos[i + 0];
14283 const char p1 = databuf_pos[i + 1];
14284
14285 *databuf_ptr++ = hex_convert (p1) << 0
14286 | hex_convert (p0) << 4;
14287 }
14288
14289 *databuf_ptr++ = 0x80;
14290
14291 for (uint i = 0; i < 512; i++)
14292 {
14293 cloudkey->data_buf[i] = byte_swap_32 (cloudkey->data_buf[i]);
14294 }
14295
14296 cloudkey->data_len = databuf_len / 2;
14297
14298 return (PARSER_OK);
14299 }
14300
14301 int nsec3_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14302 {
14303 if ((input_len < DISPLAY_LEN_MIN_8300) || (input_len > DISPLAY_LEN_MAX_8300)) return (PARSER_GLOBAL_LENGTH);
14304
14305 u32 *digest = (u32 *) hash_buf->digest;
14306
14307 salt_t *salt = hash_buf->salt;
14308
14309 /**
14310 * parse line
14311 */
14312
14313 char *hashbuf_pos = input_buf;
14314
14315 char *domainbuf_pos = strchr (hashbuf_pos, ':');
14316
14317 if (domainbuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14318
14319 const uint hashbuf_len = domainbuf_pos - hashbuf_pos;
14320
14321 if (hashbuf_len != 32) return (PARSER_HASH_LENGTH);
14322
14323 domainbuf_pos++;
14324
14325 if (domainbuf_pos[0] != '.') return (PARSER_SALT_VALUE);
14326
14327 char *saltbuf_pos = strchr (domainbuf_pos, ':');
14328
14329 if (saltbuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14330
14331 const uint domainbuf_len = saltbuf_pos - domainbuf_pos;
14332
14333 if (domainbuf_len >= 32) return (PARSER_SALT_LENGTH);
14334
14335 saltbuf_pos++;
14336
14337 char *iteration_pos = strchr (saltbuf_pos, ':');
14338
14339 if (iteration_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14340
14341 const uint saltbuf_len = iteration_pos - saltbuf_pos;
14342
14343 if (saltbuf_len >= 28) return (PARSER_SALT_LENGTH); // 28 = 32 - 4; 4 = length
14344
14345 if ((domainbuf_len + saltbuf_len) >= 48) return (PARSER_SALT_LENGTH);
14346
14347 iteration_pos++;
14348
14349 const uint iteration_len = input_len - hashbuf_len - 1 - domainbuf_len - 1 - saltbuf_len - 1;
14350
14351 if (iteration_len < 1) return (PARSER_SALT_ITERATION);
14352 if (iteration_len > 5) return (PARSER_SALT_ITERATION);
14353
14354 // ok, the plan for this algorithm is the following:
14355 // we have 2 salts here, the domain-name and a random salt
14356 // while both are used in the initial transformation,
14357 // only the random salt is used in the following iterations
14358 // so we create two buffer, one that includes domain-name (stored into salt_buf_pc[])
14359 // and one that includes only the real salt (stored into salt_buf[]).
14360 // the domain-name length is put into array position 7 of salt_buf_pc[] since there is not salt_pc_len
14361
14362 u8 tmp_buf[100] = { 0 };
14363
14364 base32_decode (itoa32_to_int, (const u8 *) hashbuf_pos, 32, tmp_buf);
14365
14366 memcpy (digest, tmp_buf, 20);
14367
14368 digest[0] = byte_swap_32 (digest[0]);
14369 digest[1] = byte_swap_32 (digest[1]);
14370 digest[2] = byte_swap_32 (digest[2]);
14371 digest[3] = byte_swap_32 (digest[3]);
14372 digest[4] = byte_swap_32 (digest[4]);
14373
14374 // domain
14375
14376 char *salt_buf_pc_ptr = (char *) salt->salt_buf_pc;
14377
14378 memcpy (salt_buf_pc_ptr, domainbuf_pos, domainbuf_len);
14379
14380 char *len_ptr = NULL;
14381
14382 for (uint i = 0; i < domainbuf_len; i++)
14383 {
14384 if (salt_buf_pc_ptr[i] == '.')
14385 {
14386 len_ptr = &salt_buf_pc_ptr[i];
14387
14388 *len_ptr = 0;
14389 }
14390 else
14391 {
14392 *len_ptr += 1;
14393 }
14394 }
14395
14396 salt->salt_buf_pc[7] = domainbuf_len;
14397
14398 // "real" salt
14399
14400 char *salt_buf_ptr = (char *) salt->salt_buf;
14401
14402 const uint salt_len = parse_and_store_salt (salt_buf_ptr, saltbuf_pos, saltbuf_len);
14403
14404 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
14405
14406 salt->salt_len = salt_len;
14407
14408 // iteration
14409
14410 salt->salt_iter = atoi (iteration_pos);
14411
14412 return (PARSER_OK);
14413 }
14414
14415 int wbb3_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14416 {
14417 if ((input_len < DISPLAY_LEN_MIN_8400) || (input_len > DISPLAY_LEN_MAX_8400)) return (PARSER_GLOBAL_LENGTH);
14418
14419 u32 *digest = (u32 *) hash_buf->digest;
14420
14421 salt_t *salt = hash_buf->salt;
14422
14423 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
14424 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
14425 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
14426 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
14427 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
14428
14429 if (input_buf[40] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
14430
14431 uint salt_len = input_len - 40 - 1;
14432
14433 char *salt_buf = input_buf + 40 + 1;
14434
14435 char *salt_buf_ptr = (char *) salt->salt_buf;
14436
14437 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
14438
14439 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
14440
14441 salt->salt_len = salt_len;
14442
14443 return (PARSER_OK);
14444 }
14445
14446 int racf_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14447 {
14448 const u8 ascii_to_ebcdic[] =
14449 {
14450 0x00, 0x01, 0x02, 0x03, 0x37, 0x2d, 0x2e, 0x2f, 0x16, 0x05, 0x25, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
14451 0x10, 0x11, 0x12, 0x13, 0x3c, 0x3d, 0x32, 0x26, 0x18, 0x19, 0x3f, 0x27, 0x1c, 0x1d, 0x1e, 0x1f,
14452 0x40, 0x4f, 0x7f, 0x7b, 0x5b, 0x6c, 0x50, 0x7d, 0x4d, 0x5d, 0x5c, 0x4e, 0x6b, 0x60, 0x4b, 0x61,
14453 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0x7a, 0x5e, 0x4c, 0x7e, 0x6e, 0x6f,
14454 0x7c, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
14455 0xd7, 0xd8, 0xd9, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0x4a, 0xe0, 0x5a, 0x5f, 0x6d,
14456 0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
14457 0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xc0, 0x6a, 0xd0, 0xa1, 0x07,
14458 0x20, 0x21, 0x22, 0x23, 0x24, 0x15, 0x06, 0x17, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x09, 0x0a, 0x1b,
14459 0x30, 0x31, 0x1a, 0x33, 0x34, 0x35, 0x36, 0x08, 0x38, 0x39, 0x3a, 0x3b, 0x04, 0x14, 0x3e, 0xe1,
14460 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
14461 0x58, 0x59, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75,
14462 0x76, 0x77, 0x78, 0x80, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e,
14463 0x9f, 0xa0, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
14464 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xda, 0xdb,
14465 0xdc, 0xdd, 0xde, 0xdf, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
14466 };
14467
14468 if ((input_len < DISPLAY_LEN_MIN_8500) || (input_len > DISPLAY_LEN_MAX_8500)) return (PARSER_GLOBAL_LENGTH);
14469
14470 if (memcmp (SIGNATURE_RACF, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
14471
14472 u32 *digest = (u32 *) hash_buf->digest;
14473
14474 salt_t *salt = hash_buf->salt;
14475
14476 char *salt_pos = input_buf + 6 + 1;
14477
14478 char *digest_pos = strchr (salt_pos, '*');
14479
14480 if (digest_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14481
14482 uint salt_len = digest_pos - salt_pos;
14483
14484 if (salt_len > 8) return (PARSER_SALT_LENGTH);
14485
14486 uint hash_len = input_len - 1 - salt_len - 1 - 6;
14487
14488 if (hash_len != 16) return (PARSER_HASH_LENGTH);
14489
14490 digest_pos++;
14491
14492 char *salt_buf_ptr = (char *) salt->salt_buf;
14493 char *salt_buf_pc_ptr = (char *) salt->salt_buf_pc;
14494
14495 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
14496
14497 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
14498
14499 salt->salt_len = salt_len;
14500
14501 for (uint i = 0; i < salt_len; i++)
14502 {
14503 salt_buf_pc_ptr[i] = ascii_to_ebcdic[(int) salt_buf_ptr[i]];
14504 }
14505 for (uint i = salt_len; i < 8; i++)
14506 {
14507 salt_buf_pc_ptr[i] = 0x40;
14508 }
14509
14510 uint tt;
14511
14512 IP (salt->salt_buf_pc[0], salt->salt_buf_pc[1], tt);
14513
14514 salt->salt_buf_pc[0] = rotl32 (salt->salt_buf_pc[0], 3u);
14515 salt->salt_buf_pc[1] = rotl32 (salt->salt_buf_pc[1], 3u);
14516
14517 digest[0] = hex_to_u32 ((const u8 *) &digest_pos[ 0]);
14518 digest[1] = hex_to_u32 ((const u8 *) &digest_pos[ 8]);
14519
14520 digest[0] = byte_swap_32 (digest[0]);
14521 digest[1] = byte_swap_32 (digest[1]);
14522
14523 IP (digest[0], digest[1], tt);
14524
14525 digest[0] = rotr32 (digest[0], 29);
14526 digest[1] = rotr32 (digest[1], 29);
14527 digest[2] = 0;
14528 digest[3] = 0;
14529
14530 return (PARSER_OK);
14531 }
14532
14533 int lotus5_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14534 {
14535 if ((input_len < DISPLAY_LEN_MIN_8600) || (input_len > DISPLAY_LEN_MAX_8600)) return (PARSER_GLOBAL_LENGTH);
14536
14537 u32 *digest = (u32 *) hash_buf->digest;
14538
14539 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
14540 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
14541 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
14542 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
14543
14544 digest[0] = byte_swap_32 (digest[0]);
14545 digest[1] = byte_swap_32 (digest[1]);
14546 digest[2] = byte_swap_32 (digest[2]);
14547 digest[3] = byte_swap_32 (digest[3]);
14548
14549 return (PARSER_OK);
14550 }
14551
14552 int lotus6_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14553 {
14554 if ((input_len < DISPLAY_LEN_MIN_8700) || (input_len > DISPLAY_LEN_MAX_8700)) return (PARSER_GLOBAL_LENGTH);
14555
14556 if ((input_buf[0] != '(') || (input_buf[1] != 'G') || (input_buf[21] != ')')) return (PARSER_SIGNATURE_UNMATCHED);
14557
14558 u32 *digest = (u32 *) hash_buf->digest;
14559
14560 salt_t *salt = hash_buf->salt;
14561
14562 u8 tmp_buf[120] = { 0 };
14563
14564 base64_decode (lotus64_to_int, (const u8 *) input_buf + 2, input_len - 3, tmp_buf);
14565
14566 tmp_buf[3] += -4; // dont ask!
14567
14568 memcpy (salt->salt_buf, tmp_buf, 5);
14569
14570 salt->salt_len = 5;
14571
14572 memcpy (digest, tmp_buf + 5, 9);
14573
14574 // yes, only 9 byte are needed to crack, but 10 to display
14575
14576 salt->salt_buf_pc[7] = input_buf[20];
14577
14578 return (PARSER_OK);
14579 }
14580
14581 int lotus8_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14582 {
14583 if ((input_len < DISPLAY_LEN_MIN_9100) || (input_len > DISPLAY_LEN_MAX_9100)) return (PARSER_GLOBAL_LENGTH);
14584
14585 if ((input_buf[0] != '(') || (input_buf[1] != 'H') || (input_buf[DISPLAY_LEN_MAX_9100 - 1] != ')')) return (PARSER_SIGNATURE_UNMATCHED);
14586
14587 u32 *digest = (u32 *) hash_buf->digest;
14588
14589 salt_t *salt = hash_buf->salt;
14590
14591 u8 tmp_buf[120] = { 0 };
14592
14593 base64_decode (lotus64_to_int, (const u8 *) input_buf + 2, input_len - 3, tmp_buf);
14594
14595 tmp_buf[3] += -4; // dont ask!
14596
14597 // salt
14598
14599 memcpy (salt->salt_buf, tmp_buf, 16);
14600
14601 salt->salt_len = 16; // Attention: in theory we have 2 salt_len, one for the -m 8700 part (len: 8), 2nd for the 9100 part (len: 16)
14602
14603 // iteration
14604
14605 char tmp_iter_buf[11] = { 0 };
14606
14607 memcpy (tmp_iter_buf, tmp_buf + 16, 10);
14608
14609 tmp_iter_buf[10] = 0;
14610
14611 salt->salt_iter = atoi (tmp_iter_buf);
14612
14613 if (salt->salt_iter < 1) // well, the limit hopefully is much higher
14614 {
14615 return (PARSER_SALT_ITERATION);
14616 }
14617
14618 salt->salt_iter--; // first round in init
14619
14620 // 2 additional bytes for display only
14621
14622 salt->salt_buf_pc[0] = tmp_buf[26];
14623 salt->salt_buf_pc[1] = tmp_buf[27];
14624
14625 // digest
14626
14627 memcpy (digest, tmp_buf + 28, 8);
14628
14629 digest[0] = byte_swap_32 (digest[0]);
14630 digest[1] = byte_swap_32 (digest[1]);
14631 digest[2] = 0;
14632 digest[3] = 0;
14633
14634 return (PARSER_OK);
14635 }
14636
14637 int hmailserver_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14638 {
14639 if ((input_len < DISPLAY_LEN_MIN_1421) || (input_len > DISPLAY_LEN_MAX_1421)) return (PARSER_GLOBAL_LENGTH);
14640
14641 u32 *digest = (u32 *) hash_buf->digest;
14642
14643 salt_t *salt = hash_buf->salt;
14644
14645 char *salt_buf_pos = input_buf;
14646
14647 char *hash_buf_pos = salt_buf_pos + 6;
14648
14649 digest[0] = hex_to_u32 ((const u8 *) &hash_buf_pos[ 0]);
14650 digest[1] = hex_to_u32 ((const u8 *) &hash_buf_pos[ 8]);
14651 digest[2] = hex_to_u32 ((const u8 *) &hash_buf_pos[16]);
14652 digest[3] = hex_to_u32 ((const u8 *) &hash_buf_pos[24]);
14653 digest[4] = hex_to_u32 ((const u8 *) &hash_buf_pos[32]);
14654 digest[5] = hex_to_u32 ((const u8 *) &hash_buf_pos[40]);
14655 digest[6] = hex_to_u32 ((const u8 *) &hash_buf_pos[48]);
14656 digest[7] = hex_to_u32 ((const u8 *) &hash_buf_pos[56]);
14657
14658 digest[0] -= SHA256M_A;
14659 digest[1] -= SHA256M_B;
14660 digest[2] -= SHA256M_C;
14661 digest[3] -= SHA256M_D;
14662 digest[4] -= SHA256M_E;
14663 digest[5] -= SHA256M_F;
14664 digest[6] -= SHA256M_G;
14665 digest[7] -= SHA256M_H;
14666
14667 char *salt_buf_ptr = (char *) salt->salt_buf;
14668
14669 const uint salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf_pos, 6);
14670
14671 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
14672
14673 salt->salt_len = salt_len;
14674
14675 return (PARSER_OK);
14676 }
14677
14678 int phps_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14679 {
14680 if ((input_len < DISPLAY_LEN_MIN_2612) || (input_len > DISPLAY_LEN_MAX_2612)) return (PARSER_GLOBAL_LENGTH);
14681
14682 u32 *digest = (u32 *) hash_buf->digest;
14683
14684 if (memcmp (SIGNATURE_PHPS, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
14685
14686 salt_t *salt = hash_buf->salt;
14687
14688 char *salt_buf = input_buf + 6;
14689
14690 char *digest_buf = strchr (salt_buf, '$');
14691
14692 if (digest_buf == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14693
14694 uint salt_len = digest_buf - salt_buf;
14695
14696 digest_buf++; // skip the '$' symbol
14697
14698 char *salt_buf_ptr = (char *) salt->salt_buf;
14699
14700 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
14701
14702 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
14703
14704 salt->salt_len = salt_len;
14705
14706 digest[0] = hex_to_u32 ((const u8 *) &digest_buf[ 0]);
14707 digest[1] = hex_to_u32 ((const u8 *) &digest_buf[ 8]);
14708 digest[2] = hex_to_u32 ((const u8 *) &digest_buf[16]);
14709 digest[3] = hex_to_u32 ((const u8 *) &digest_buf[24]);
14710
14711 digest[0] = byte_swap_32 (digest[0]);
14712 digest[1] = byte_swap_32 (digest[1]);
14713 digest[2] = byte_swap_32 (digest[2]);
14714 digest[3] = byte_swap_32 (digest[3]);
14715
14716 digest[0] -= MD5M_A;
14717 digest[1] -= MD5M_B;
14718 digest[2] -= MD5M_C;
14719 digest[3] -= MD5M_D;
14720
14721 return (PARSER_OK);
14722 }
14723
14724 int mediawiki_b_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14725 {
14726 if ((input_len < DISPLAY_LEN_MIN_3711) || (input_len > DISPLAY_LEN_MAX_3711)) return (PARSER_GLOBAL_LENGTH);
14727
14728 if (memcmp (SIGNATURE_MEDIAWIKI_B, input_buf, 3)) return (PARSER_SIGNATURE_UNMATCHED);
14729
14730 u32 *digest = (u32 *) hash_buf->digest;
14731
14732 salt_t *salt = hash_buf->salt;
14733
14734 char *salt_buf = input_buf + 3;
14735
14736 char *digest_buf = strchr (salt_buf, '$');
14737
14738 if (digest_buf == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14739
14740 uint salt_len = digest_buf - salt_buf;
14741
14742 digest_buf++; // skip the '$' symbol
14743
14744 char *salt_buf_ptr = (char *) salt->salt_buf;
14745
14746 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
14747
14748 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
14749
14750 salt_buf_ptr[salt_len] = 0x2d;
14751
14752 salt->salt_len = salt_len + 1;
14753
14754 digest[0] = hex_to_u32 ((const u8 *) &digest_buf[ 0]);
14755 digest[1] = hex_to_u32 ((const u8 *) &digest_buf[ 8]);
14756 digest[2] = hex_to_u32 ((const u8 *) &digest_buf[16]);
14757 digest[3] = hex_to_u32 ((const u8 *) &digest_buf[24]);
14758
14759 digest[0] = byte_swap_32 (digest[0]);
14760 digest[1] = byte_swap_32 (digest[1]);
14761 digest[2] = byte_swap_32 (digest[2]);
14762 digest[3] = byte_swap_32 (digest[3]);
14763
14764 digest[0] -= MD5M_A;
14765 digest[1] -= MD5M_B;
14766 digest[2] -= MD5M_C;
14767 digest[3] -= MD5M_D;
14768
14769 return (PARSER_OK);
14770 }
14771
14772 int peoplesoft_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14773 {
14774 if ((input_len < DISPLAY_LEN_MIN_133) || (input_len > DISPLAY_LEN_MAX_133)) return (PARSER_GLOBAL_LENGTH);
14775
14776 u32 *digest = (u32 *) hash_buf->digest;
14777
14778 salt_t *salt = hash_buf->salt;
14779
14780 u8 tmp_buf[100] = { 0 };
14781
14782 base64_decode (base64_to_int, (const u8 *) input_buf, input_len, tmp_buf);
14783
14784 memcpy (digest, tmp_buf, 20);
14785
14786 digest[0] = byte_swap_32 (digest[0]);
14787 digest[1] = byte_swap_32 (digest[1]);
14788 digest[2] = byte_swap_32 (digest[2]);
14789 digest[3] = byte_swap_32 (digest[3]);
14790 digest[4] = byte_swap_32 (digest[4]);
14791
14792 digest[0] -= SHA1M_A;
14793 digest[1] -= SHA1M_B;
14794 digest[2] -= SHA1M_C;
14795 digest[3] -= SHA1M_D;
14796 digest[4] -= SHA1M_E;
14797
14798 salt->salt_buf[0] = 0x80;
14799
14800 salt->salt_len = 0;
14801
14802 return (PARSER_OK);
14803 }
14804
14805 int skype_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14806 {
14807 if ((input_len < DISPLAY_LEN_MIN_23) || (input_len > DISPLAY_LEN_MAX_23)) return (PARSER_GLOBAL_LENGTH);
14808
14809 u32 *digest = (u32 *) hash_buf->digest;
14810
14811 salt_t *salt = hash_buf->salt;
14812
14813 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
14814 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
14815 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
14816 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
14817
14818 digest[0] = byte_swap_32 (digest[0]);
14819 digest[1] = byte_swap_32 (digest[1]);
14820 digest[2] = byte_swap_32 (digest[2]);
14821 digest[3] = byte_swap_32 (digest[3]);
14822
14823 digest[0] -= MD5M_A;
14824 digest[1] -= MD5M_B;
14825 digest[2] -= MD5M_C;
14826 digest[3] -= MD5M_D;
14827
14828 if (input_buf[32] != ':') return (PARSER_SEPARATOR_UNMATCHED);
14829
14830 uint salt_len = input_len - 32 - 1;
14831
14832 char *salt_buf = input_buf + 32 + 1;
14833
14834 char *salt_buf_ptr = (char *) salt->salt_buf;
14835
14836 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
14837
14838 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
14839
14840 /*
14841 * add static "salt" part
14842 */
14843
14844 memcpy (salt_buf_ptr + salt_len, "\nskyper\n", 8);
14845
14846 salt_len += 8;
14847
14848 salt->salt_len = salt_len;
14849
14850 return (PARSER_OK);
14851 }
14852
14853 int androidfde_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14854 {
14855 if ((input_len < DISPLAY_LEN_MIN_8800) || (input_len > DISPLAY_LEN_MAX_8800)) return (PARSER_GLOBAL_LENGTH);
14856
14857 if (memcmp (SIGNATURE_ANDROIDFDE, input_buf, 5)) return (PARSER_SIGNATURE_UNMATCHED);
14858
14859 u32 *digest = (u32 *) hash_buf->digest;
14860
14861 salt_t *salt = hash_buf->salt;
14862
14863 androidfde_t *androidfde = (androidfde_t *) hash_buf->esalt;
14864
14865 /**
14866 * parse line
14867 */
14868
14869 char *saltlen_pos = input_buf + 1 + 3 + 1;
14870
14871 char *saltbuf_pos = strchr (saltlen_pos, '$');
14872
14873 if (saltbuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14874
14875 uint saltlen_len = saltbuf_pos - saltlen_pos;
14876
14877 if (saltlen_len != 2) return (PARSER_SALT_LENGTH);
14878
14879 saltbuf_pos++;
14880
14881 char *keylen_pos = strchr (saltbuf_pos, '$');
14882
14883 if (keylen_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14884
14885 uint saltbuf_len = keylen_pos - saltbuf_pos;
14886
14887 if (saltbuf_len != 32) return (PARSER_SALT_LENGTH);
14888
14889 keylen_pos++;
14890
14891 char *keybuf_pos = strchr (keylen_pos, '$');
14892
14893 if (keybuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14894
14895 uint keylen_len = keybuf_pos - keylen_pos;
14896
14897 if (keylen_len != 2) return (PARSER_SALT_LENGTH);
14898
14899 keybuf_pos++;
14900
14901 char *databuf_pos = strchr (keybuf_pos, '$');
14902
14903 if (databuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14904
14905 uint keybuf_len = databuf_pos - keybuf_pos;
14906
14907 if (keybuf_len != 32) return (PARSER_SALT_LENGTH);
14908
14909 databuf_pos++;
14910
14911 uint data_len = input_len - 1 - 3 - 1 - saltlen_len - 1 - saltbuf_len - 1 - keylen_len - 1 - keybuf_len - 1;
14912
14913 if (data_len != 3072) return (PARSER_SALT_LENGTH);
14914
14915 /**
14916 * copy data
14917 */
14918
14919 digest[0] = hex_to_u32 ((const u8 *) &keybuf_pos[ 0]);
14920 digest[1] = hex_to_u32 ((const u8 *) &keybuf_pos[ 8]);
14921 digest[2] = hex_to_u32 ((const u8 *) &keybuf_pos[16]);
14922 digest[3] = hex_to_u32 ((const u8 *) &keybuf_pos[24]);
14923
14924 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &saltbuf_pos[ 0]);
14925 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &saltbuf_pos[ 8]);
14926 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &saltbuf_pos[16]);
14927 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &saltbuf_pos[24]);
14928
14929 salt->salt_buf[0] = byte_swap_32 (salt->salt_buf[0]);
14930 salt->salt_buf[1] = byte_swap_32 (salt->salt_buf[1]);
14931 salt->salt_buf[2] = byte_swap_32 (salt->salt_buf[2]);
14932 salt->salt_buf[3] = byte_swap_32 (salt->salt_buf[3]);
14933
14934 salt->salt_len = 16;
14935 salt->salt_iter = ROUNDS_ANDROIDFDE - 1;
14936
14937 for (uint i = 0, j = 0; i < 3072; i += 8, j += 1)
14938 {
14939 androidfde->data[j] = hex_to_u32 ((const u8 *) &databuf_pos[i]);
14940 }
14941
14942 return (PARSER_OK);
14943 }
14944
14945 int scrypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14946 {
14947 if ((input_len < DISPLAY_LEN_MIN_8900) || (input_len > DISPLAY_LEN_MAX_8900)) return (PARSER_GLOBAL_LENGTH);
14948
14949 if (memcmp (SIGNATURE_SCRYPT, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
14950
14951 u32 *digest = (u32 *) hash_buf->digest;
14952
14953 salt_t *salt = hash_buf->salt;
14954
14955 /**
14956 * parse line
14957 */
14958
14959 // first is the N salt parameter
14960
14961 char *N_pos = input_buf + 6;
14962
14963 if (N_pos[0] != ':') return (PARSER_SEPARATOR_UNMATCHED);
14964
14965 N_pos++;
14966
14967 salt->scrypt_N = atoi (N_pos);
14968
14969 // r
14970
14971 char *r_pos = strchr (N_pos, ':');
14972
14973 if (r_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14974
14975 r_pos++;
14976
14977 salt->scrypt_r = atoi (r_pos);
14978
14979 // p
14980
14981 char *p_pos = strchr (r_pos, ':');
14982
14983 if (p_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14984
14985 p_pos++;
14986
14987 salt->scrypt_p = atoi (p_pos);
14988
14989 // salt
14990
14991 char *saltbuf_pos = strchr (p_pos, ':');
14992
14993 if (saltbuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14994
14995 saltbuf_pos++;
14996
14997 char *hash_pos = strchr (saltbuf_pos, ':');
14998
14999 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15000
15001 hash_pos++;
15002
15003 // base64 decode
15004
15005 int salt_len_base64 = hash_pos - saltbuf_pos;
15006
15007 if (salt_len_base64 > 45) return (PARSER_SALT_LENGTH);
15008
15009 u8 tmp_buf[33] = { 0 };
15010
15011 int tmp_len = base64_decode (base64_to_int, (const u8 *) saltbuf_pos, salt_len_base64, tmp_buf);
15012
15013 char *salt_buf_ptr = (char *) salt->salt_buf;
15014
15015 memcpy (salt_buf_ptr, tmp_buf, tmp_len);
15016
15017 salt->salt_len = tmp_len;
15018 salt->salt_iter = 1;
15019
15020 // digest - base64 decode
15021
15022 memset (tmp_buf, 0, sizeof (tmp_buf));
15023
15024 tmp_len = input_len - (hash_pos - input_buf);
15025
15026 if (tmp_len != 44) return (PARSER_GLOBAL_LENGTH);
15027
15028 base64_decode (base64_to_int, (const u8 *) hash_pos, tmp_len, tmp_buf);
15029
15030 memcpy (digest, tmp_buf, 32);
15031
15032 return (PARSER_OK);
15033 }
15034
15035 int juniper_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15036 {
15037 if ((input_len < DISPLAY_LEN_MIN_501) || (input_len > DISPLAY_LEN_MAX_501)) return (PARSER_GLOBAL_LENGTH);
15038
15039 u32 *digest = (u32 *) hash_buf->digest;
15040
15041 salt_t *salt = hash_buf->salt;
15042
15043 /**
15044 * parse line
15045 */
15046
15047 char decrypted[76] = { 0 }; // iv + hash
15048
15049 juniper_decrypt_hash (input_buf, decrypted);
15050
15051 char *md5crypt_hash = decrypted + 12;
15052
15053 if (memcmp (md5crypt_hash, "$1$danastre$", 12)) return (PARSER_SALT_VALUE);
15054
15055 salt->salt_iter = ROUNDS_MD5CRYPT;
15056
15057 char *salt_pos = md5crypt_hash + 3;
15058
15059 char *hash_pos = strchr (salt_pos, '$'); // or simply salt_pos + 8
15060
15061 salt->salt_len = hash_pos - salt_pos; // should be 8
15062
15063 memcpy ((char *) salt->salt_buf, salt_pos, salt->salt_len);
15064
15065 hash_pos++;
15066
15067 md5crypt_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
15068
15069 return (PARSER_OK);
15070 }
15071
15072 int cisco8_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15073 {
15074 if ((input_len < DISPLAY_LEN_MIN_9200) || (input_len > DISPLAY_LEN_MAX_9200)) return (PARSER_GLOBAL_LENGTH);
15075
15076 if (memcmp (SIGNATURE_CISCO8, input_buf, 3)) return (PARSER_SIGNATURE_UNMATCHED);
15077
15078 u32 *digest = (u32 *) hash_buf->digest;
15079
15080 salt_t *salt = hash_buf->salt;
15081
15082 pbkdf2_sha256_t *pbkdf2_sha256 = (pbkdf2_sha256_t *) hash_buf->esalt;
15083
15084 /**
15085 * parse line
15086 */
15087
15088 // first is *raw* salt
15089
15090 char *salt_pos = input_buf + 3;
15091
15092 char *hash_pos = strchr (salt_pos, '$');
15093
15094 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15095
15096 uint salt_len = hash_pos - salt_pos;
15097
15098 if (salt_len != 14) return (PARSER_SALT_LENGTH);
15099
15100 hash_pos++;
15101
15102 char *salt_buf_ptr = (char *) pbkdf2_sha256->salt_buf;
15103
15104 memcpy (salt_buf_ptr, salt_pos, 14);
15105
15106 salt_buf_ptr[17] = 0x01;
15107 salt_buf_ptr[18] = 0x80;
15108
15109 // add some stuff to normal salt to make sorted happy
15110
15111 salt->salt_buf[0] = pbkdf2_sha256->salt_buf[0];
15112 salt->salt_buf[1] = pbkdf2_sha256->salt_buf[1];
15113 salt->salt_buf[2] = pbkdf2_sha256->salt_buf[2];
15114 salt->salt_buf[3] = pbkdf2_sha256->salt_buf[3];
15115
15116 salt->salt_len = salt_len;
15117 salt->salt_iter = ROUNDS_CISCO8 - 1;
15118
15119 // base64 decode hash
15120
15121 u8 tmp_buf[100] = { 0 };
15122
15123 uint hash_len = input_len - 3 - salt_len - 1;
15124
15125 int tmp_len = base64_decode (itoa64_to_int, (const u8 *) hash_pos, hash_len, tmp_buf);
15126
15127 if (tmp_len != 32) return (PARSER_HASH_LENGTH);
15128
15129 memcpy (digest, tmp_buf, 32);
15130
15131 digest[0] = byte_swap_32 (digest[0]);
15132 digest[1] = byte_swap_32 (digest[1]);
15133 digest[2] = byte_swap_32 (digest[2]);
15134 digest[3] = byte_swap_32 (digest[3]);
15135 digest[4] = byte_swap_32 (digest[4]);
15136 digest[5] = byte_swap_32 (digest[5]);
15137 digest[6] = byte_swap_32 (digest[6]);
15138 digest[7] = byte_swap_32 (digest[7]);
15139
15140 return (PARSER_OK);
15141 }
15142
15143 int cisco9_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15144 {
15145 if ((input_len < DISPLAY_LEN_MIN_9300) || (input_len > DISPLAY_LEN_MAX_9300)) return (PARSER_GLOBAL_LENGTH);
15146
15147 if (memcmp (SIGNATURE_CISCO9, input_buf, 3)) return (PARSER_SIGNATURE_UNMATCHED);
15148
15149 u32 *digest = (u32 *) hash_buf->digest;
15150
15151 salt_t *salt = hash_buf->salt;
15152
15153 /**
15154 * parse line
15155 */
15156
15157 // first is *raw* salt
15158
15159 char *salt_pos = input_buf + 3;
15160
15161 char *hash_pos = strchr (salt_pos, '$');
15162
15163 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15164
15165 uint salt_len = hash_pos - salt_pos;
15166
15167 if (salt_len != 14) return (PARSER_SALT_LENGTH);
15168
15169 salt->salt_len = salt_len;
15170 hash_pos++;
15171
15172 char *salt_buf_ptr = (char *) salt->salt_buf;
15173
15174 memcpy (salt_buf_ptr, salt_pos, salt_len);
15175 salt_buf_ptr[salt_len] = 0;
15176
15177 // base64 decode hash
15178
15179 u8 tmp_buf[100] = { 0 };
15180
15181 uint hash_len = input_len - 3 - salt_len - 1;
15182
15183 int tmp_len = base64_decode (itoa64_to_int, (const u8 *) hash_pos, hash_len, tmp_buf);
15184
15185 if (tmp_len != 32) return (PARSER_HASH_LENGTH);
15186
15187 memcpy (digest, tmp_buf, 32);
15188
15189 // fixed:
15190 salt->scrypt_N = 16384;
15191 salt->scrypt_r = 1;
15192 salt->scrypt_p = 1;
15193 salt->salt_iter = 1;
15194
15195 return (PARSER_OK);
15196 }
15197
15198 int office2007_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15199 {
15200 if ((input_len < DISPLAY_LEN_MIN_9400) || (input_len > DISPLAY_LEN_MAX_9400)) return (PARSER_GLOBAL_LENGTH);
15201
15202 if (memcmp (SIGNATURE_OFFICE2007, input_buf, 8)) return (PARSER_SIGNATURE_UNMATCHED);
15203
15204 u32 *digest = (u32 *) hash_buf->digest;
15205
15206 salt_t *salt = hash_buf->salt;
15207
15208 office2007_t *office2007 = (office2007_t *) hash_buf->esalt;
15209
15210 /**
15211 * parse line
15212 */
15213
15214 char *version_pos = input_buf + 8 + 1;
15215
15216 char *verifierHashSize_pos = strchr (version_pos, '*');
15217
15218 if (verifierHashSize_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15219
15220 u32 version_len = verifierHashSize_pos - version_pos;
15221
15222 if (version_len != 4) return (PARSER_SALT_LENGTH);
15223
15224 verifierHashSize_pos++;
15225
15226 char *keySize_pos = strchr (verifierHashSize_pos, '*');
15227
15228 if (keySize_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15229
15230 u32 verifierHashSize_len = keySize_pos - verifierHashSize_pos;
15231
15232 if (verifierHashSize_len != 2) return (PARSER_SALT_LENGTH);
15233
15234 keySize_pos++;
15235
15236 char *saltSize_pos = strchr (keySize_pos, '*');
15237
15238 if (saltSize_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15239
15240 u32 keySize_len = saltSize_pos - keySize_pos;
15241
15242 if (keySize_len != 3) return (PARSER_SALT_LENGTH);
15243
15244 saltSize_pos++;
15245
15246 char *osalt_pos = strchr (saltSize_pos, '*');
15247
15248 if (osalt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15249
15250 u32 saltSize_len = osalt_pos - saltSize_pos;
15251
15252 if (saltSize_len != 2) return (PARSER_SALT_LENGTH);
15253
15254 osalt_pos++;
15255
15256 char *encryptedVerifier_pos = strchr (osalt_pos, '*');
15257
15258 if (encryptedVerifier_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15259
15260 u32 osalt_len = encryptedVerifier_pos - osalt_pos;
15261
15262 if (osalt_len != 32) return (PARSER_SALT_LENGTH);
15263
15264 encryptedVerifier_pos++;
15265
15266 char *encryptedVerifierHash_pos = strchr (encryptedVerifier_pos, '*');
15267
15268 if (encryptedVerifierHash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15269
15270 u32 encryptedVerifier_len = encryptedVerifierHash_pos - encryptedVerifier_pos;
15271
15272 if (encryptedVerifier_len != 32) return (PARSER_SALT_LENGTH);
15273
15274 encryptedVerifierHash_pos++;
15275
15276 u32 encryptedVerifierHash_len = input_len - 8 - 1 - version_len - 1 - verifierHashSize_len - 1 - keySize_len - 1 - saltSize_len - 1 - osalt_len - 1 - encryptedVerifier_len - 1;
15277
15278 if (encryptedVerifierHash_len != 40) return (PARSER_SALT_LENGTH);
15279
15280 const uint version = atoi (version_pos);
15281
15282 if (version != 2007) return (PARSER_SALT_VALUE);
15283
15284 const uint verifierHashSize = atoi (verifierHashSize_pos);
15285
15286 if (verifierHashSize != 20) return (PARSER_SALT_VALUE);
15287
15288 const uint keySize = atoi (keySize_pos);
15289
15290 if ((keySize != 128) && (keySize != 256)) return (PARSER_SALT_VALUE);
15291
15292 office2007->keySize = keySize;
15293
15294 const uint saltSize = atoi (saltSize_pos);
15295
15296 if (saltSize != 16) return (PARSER_SALT_VALUE);
15297
15298 /**
15299 * salt
15300 */
15301
15302 salt->salt_len = 16;
15303 salt->salt_iter = ROUNDS_OFFICE2007;
15304
15305 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &osalt_pos[ 0]);
15306 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &osalt_pos[ 8]);
15307 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &osalt_pos[16]);
15308 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &osalt_pos[24]);
15309
15310 /**
15311 * esalt
15312 */
15313
15314 office2007->encryptedVerifier[0] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 0]);
15315 office2007->encryptedVerifier[1] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 8]);
15316 office2007->encryptedVerifier[2] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[16]);
15317 office2007->encryptedVerifier[3] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[24]);
15318
15319 office2007->encryptedVerifierHash[0] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 0]);
15320 office2007->encryptedVerifierHash[1] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 8]);
15321 office2007->encryptedVerifierHash[2] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[16]);
15322 office2007->encryptedVerifierHash[3] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[24]);
15323 office2007->encryptedVerifierHash[4] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[32]);
15324
15325 /**
15326 * digest
15327 */
15328
15329 digest[0] = office2007->encryptedVerifierHash[0];
15330 digest[1] = office2007->encryptedVerifierHash[1];
15331 digest[2] = office2007->encryptedVerifierHash[2];
15332 digest[3] = office2007->encryptedVerifierHash[3];
15333
15334 return (PARSER_OK);
15335 }
15336
15337 int office2010_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15338 {
15339 if ((input_len < DISPLAY_LEN_MIN_9500) || (input_len > DISPLAY_LEN_MAX_9500)) return (PARSER_GLOBAL_LENGTH);
15340
15341 if (memcmp (SIGNATURE_OFFICE2010, input_buf, 8)) return (PARSER_SIGNATURE_UNMATCHED);
15342
15343 u32 *digest = (u32 *) hash_buf->digest;
15344
15345 salt_t *salt = hash_buf->salt;
15346
15347 office2010_t *office2010 = (office2010_t *) hash_buf->esalt;
15348
15349 /**
15350 * parse line
15351 */
15352
15353 char *version_pos = input_buf + 8 + 1;
15354
15355 char *spinCount_pos = strchr (version_pos, '*');
15356
15357 if (spinCount_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15358
15359 u32 version_len = spinCount_pos - version_pos;
15360
15361 if (version_len != 4) return (PARSER_SALT_LENGTH);
15362
15363 spinCount_pos++;
15364
15365 char *keySize_pos = strchr (spinCount_pos, '*');
15366
15367 if (keySize_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15368
15369 u32 spinCount_len = keySize_pos - spinCount_pos;
15370
15371 if (spinCount_len != 6) return (PARSER_SALT_LENGTH);
15372
15373 keySize_pos++;
15374
15375 char *saltSize_pos = strchr (keySize_pos, '*');
15376
15377 if (saltSize_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15378
15379 u32 keySize_len = saltSize_pos - keySize_pos;
15380
15381 if (keySize_len != 3) return (PARSER_SALT_LENGTH);
15382
15383 saltSize_pos++;
15384
15385 char *osalt_pos = strchr (saltSize_pos, '*');
15386
15387 if (osalt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15388
15389 u32 saltSize_len = osalt_pos - saltSize_pos;
15390
15391 if (saltSize_len != 2) return (PARSER_SALT_LENGTH);
15392
15393 osalt_pos++;
15394
15395 char *encryptedVerifier_pos = strchr (osalt_pos, '*');
15396
15397 if (encryptedVerifier_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15398
15399 u32 osalt_len = encryptedVerifier_pos - osalt_pos;
15400
15401 if (osalt_len != 32) return (PARSER_SALT_LENGTH);
15402
15403 encryptedVerifier_pos++;
15404
15405 char *encryptedVerifierHash_pos = strchr (encryptedVerifier_pos, '*');
15406
15407 if (encryptedVerifierHash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15408
15409 u32 encryptedVerifier_len = encryptedVerifierHash_pos - encryptedVerifier_pos;
15410
15411 if (encryptedVerifier_len != 32) return (PARSER_SALT_LENGTH);
15412
15413 encryptedVerifierHash_pos++;
15414
15415 u32 encryptedVerifierHash_len = input_len - 8 - 1 - version_len - 1 - spinCount_len - 1 - keySize_len - 1 - saltSize_len - 1 - osalt_len - 1 - encryptedVerifier_len - 1;
15416
15417 if (encryptedVerifierHash_len != 64) return (PARSER_SALT_LENGTH);
15418
15419 const uint version = atoi (version_pos);
15420
15421 if (version != 2010) return (PARSER_SALT_VALUE);
15422
15423 const uint spinCount = atoi (spinCount_pos);
15424
15425 if (spinCount != 100000) return (PARSER_SALT_VALUE);
15426
15427 const uint keySize = atoi (keySize_pos);
15428
15429 if (keySize != 128) return (PARSER_SALT_VALUE);
15430
15431 const uint saltSize = atoi (saltSize_pos);
15432
15433 if (saltSize != 16) return (PARSER_SALT_VALUE);
15434
15435 /**
15436 * salt
15437 */
15438
15439 salt->salt_len = 16;
15440 salt->salt_iter = spinCount;
15441
15442 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &osalt_pos[ 0]);
15443 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &osalt_pos[ 8]);
15444 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &osalt_pos[16]);
15445 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &osalt_pos[24]);
15446
15447 /**
15448 * esalt
15449 */
15450
15451 office2010->encryptedVerifier[0] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 0]);
15452 office2010->encryptedVerifier[1] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 8]);
15453 office2010->encryptedVerifier[2] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[16]);
15454 office2010->encryptedVerifier[3] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[24]);
15455
15456 office2010->encryptedVerifierHash[0] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 0]);
15457 office2010->encryptedVerifierHash[1] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 8]);
15458 office2010->encryptedVerifierHash[2] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[16]);
15459 office2010->encryptedVerifierHash[3] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[24]);
15460 office2010->encryptedVerifierHash[4] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[32]);
15461 office2010->encryptedVerifierHash[5] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[40]);
15462 office2010->encryptedVerifierHash[6] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[48]);
15463 office2010->encryptedVerifierHash[7] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[56]);
15464
15465 /**
15466 * digest
15467 */
15468
15469 digest[0] = office2010->encryptedVerifierHash[0];
15470 digest[1] = office2010->encryptedVerifierHash[1];
15471 digest[2] = office2010->encryptedVerifierHash[2];
15472 digest[3] = office2010->encryptedVerifierHash[3];
15473
15474 return (PARSER_OK);
15475 }
15476
15477 int office2013_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15478 {
15479 if ((input_len < DISPLAY_LEN_MIN_9600) || (input_len > DISPLAY_LEN_MAX_9600)) return (PARSER_GLOBAL_LENGTH);
15480
15481 if (memcmp (SIGNATURE_OFFICE2013, input_buf, 8)) return (PARSER_SIGNATURE_UNMATCHED);
15482
15483 u32 *digest = (u32 *) hash_buf->digest;
15484
15485 salt_t *salt = hash_buf->salt;
15486
15487 office2013_t *office2013 = (office2013_t *) hash_buf->esalt;
15488
15489 /**
15490 * parse line
15491 */
15492
15493 char *version_pos = input_buf + 8 + 1;
15494
15495 char *spinCount_pos = strchr (version_pos, '*');
15496
15497 if (spinCount_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15498
15499 u32 version_len = spinCount_pos - version_pos;
15500
15501 if (version_len != 4) return (PARSER_SALT_LENGTH);
15502
15503 spinCount_pos++;
15504
15505 char *keySize_pos = strchr (spinCount_pos, '*');
15506
15507 if (keySize_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15508
15509 u32 spinCount_len = keySize_pos - spinCount_pos;
15510
15511 if (spinCount_len != 6) return (PARSER_SALT_LENGTH);
15512
15513 keySize_pos++;
15514
15515 char *saltSize_pos = strchr (keySize_pos, '*');
15516
15517 if (saltSize_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15518
15519 u32 keySize_len = saltSize_pos - keySize_pos;
15520
15521 if (keySize_len != 3) return (PARSER_SALT_LENGTH);
15522
15523 saltSize_pos++;
15524
15525 char *osalt_pos = strchr (saltSize_pos, '*');
15526
15527 if (osalt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15528
15529 u32 saltSize_len = osalt_pos - saltSize_pos;
15530
15531 if (saltSize_len != 2) return (PARSER_SALT_LENGTH);
15532
15533 osalt_pos++;
15534
15535 char *encryptedVerifier_pos = strchr (osalt_pos, '*');
15536
15537 if (encryptedVerifier_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15538
15539 u32 osalt_len = encryptedVerifier_pos - osalt_pos;
15540
15541 if (osalt_len != 32) return (PARSER_SALT_LENGTH);
15542
15543 encryptedVerifier_pos++;
15544
15545 char *encryptedVerifierHash_pos = strchr (encryptedVerifier_pos, '*');
15546
15547 if (encryptedVerifierHash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15548
15549 u32 encryptedVerifier_len = encryptedVerifierHash_pos - encryptedVerifier_pos;
15550
15551 if (encryptedVerifier_len != 32) return (PARSER_SALT_LENGTH);
15552
15553 encryptedVerifierHash_pos++;
15554
15555 u32 encryptedVerifierHash_len = input_len - 8 - 1 - version_len - 1 - spinCount_len - 1 - keySize_len - 1 - saltSize_len - 1 - osalt_len - 1 - encryptedVerifier_len - 1;
15556
15557 if (encryptedVerifierHash_len != 64) return (PARSER_SALT_LENGTH);
15558
15559 const uint version = atoi (version_pos);
15560
15561 if (version != 2013) return (PARSER_SALT_VALUE);
15562
15563 const uint spinCount = atoi (spinCount_pos);
15564
15565 if (spinCount != 100000) return (PARSER_SALT_VALUE);
15566
15567 const uint keySize = atoi (keySize_pos);
15568
15569 if (keySize != 256) return (PARSER_SALT_VALUE);
15570
15571 const uint saltSize = atoi (saltSize_pos);
15572
15573 if (saltSize != 16) return (PARSER_SALT_VALUE);
15574
15575 /**
15576 * salt
15577 */
15578
15579 salt->salt_len = 16;
15580 salt->salt_iter = spinCount;
15581
15582 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &osalt_pos[ 0]);
15583 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &osalt_pos[ 8]);
15584 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &osalt_pos[16]);
15585 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &osalt_pos[24]);
15586
15587 /**
15588 * esalt
15589 */
15590
15591 office2013->encryptedVerifier[0] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 0]);
15592 office2013->encryptedVerifier[1] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 8]);
15593 office2013->encryptedVerifier[2] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[16]);
15594 office2013->encryptedVerifier[3] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[24]);
15595
15596 office2013->encryptedVerifierHash[0] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 0]);
15597 office2013->encryptedVerifierHash[1] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 8]);
15598 office2013->encryptedVerifierHash[2] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[16]);
15599 office2013->encryptedVerifierHash[3] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[24]);
15600 office2013->encryptedVerifierHash[4] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[32]);
15601 office2013->encryptedVerifierHash[5] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[40]);
15602 office2013->encryptedVerifierHash[6] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[48]);
15603 office2013->encryptedVerifierHash[7] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[56]);
15604
15605 /**
15606 * digest
15607 */
15608
15609 digest[0] = office2013->encryptedVerifierHash[0];
15610 digest[1] = office2013->encryptedVerifierHash[1];
15611 digest[2] = office2013->encryptedVerifierHash[2];
15612 digest[3] = office2013->encryptedVerifierHash[3];
15613
15614 return (PARSER_OK);
15615 }
15616
15617 int oldoffice01_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15618 {
15619 if ((input_len < DISPLAY_LEN_MIN_9700) || (input_len > DISPLAY_LEN_MAX_9700)) return (PARSER_GLOBAL_LENGTH);
15620
15621 if ((memcmp (SIGNATURE_OLDOFFICE0, input_buf, 12)) && (memcmp (SIGNATURE_OLDOFFICE1, input_buf, 12))) return (PARSER_SIGNATURE_UNMATCHED);
15622
15623 u32 *digest = (u32 *) hash_buf->digest;
15624
15625 salt_t *salt = hash_buf->salt;
15626
15627 oldoffice01_t *oldoffice01 = (oldoffice01_t *) hash_buf->esalt;
15628
15629 /**
15630 * parse line
15631 */
15632
15633 char *version_pos = input_buf + 11;
15634
15635 char *osalt_pos = strchr (version_pos, '*');
15636
15637 if (osalt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15638
15639 u32 version_len = osalt_pos - version_pos;
15640
15641 if (version_len != 1) return (PARSER_SALT_LENGTH);
15642
15643 osalt_pos++;
15644
15645 char *encryptedVerifier_pos = strchr (osalt_pos, '*');
15646
15647 if (encryptedVerifier_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15648
15649 u32 osalt_len = encryptedVerifier_pos - osalt_pos;
15650
15651 if (osalt_len != 32) return (PARSER_SALT_LENGTH);
15652
15653 encryptedVerifier_pos++;
15654
15655 char *encryptedVerifierHash_pos = strchr (encryptedVerifier_pos, '*');
15656
15657 if (encryptedVerifierHash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15658
15659 u32 encryptedVerifier_len = encryptedVerifierHash_pos - encryptedVerifier_pos;
15660
15661 if (encryptedVerifier_len != 32) return (PARSER_SALT_LENGTH);
15662
15663 encryptedVerifierHash_pos++;
15664
15665 u32 encryptedVerifierHash_len = input_len - 11 - version_len - 1 - osalt_len - 1 - encryptedVerifier_len - 1;
15666
15667 if (encryptedVerifierHash_len != 32) return (PARSER_SALT_LENGTH);
15668
15669 const uint version = *version_pos - 0x30;
15670
15671 if (version != 0 && version != 1) return (PARSER_SALT_VALUE);
15672
15673 /**
15674 * esalt
15675 */
15676
15677 oldoffice01->version = version;
15678
15679 oldoffice01->encryptedVerifier[0] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 0]);
15680 oldoffice01->encryptedVerifier[1] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 8]);
15681 oldoffice01->encryptedVerifier[2] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[16]);
15682 oldoffice01->encryptedVerifier[3] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[24]);
15683
15684 oldoffice01->encryptedVerifier[0] = byte_swap_32 (oldoffice01->encryptedVerifier[0]);
15685 oldoffice01->encryptedVerifier[1] = byte_swap_32 (oldoffice01->encryptedVerifier[1]);
15686 oldoffice01->encryptedVerifier[2] = byte_swap_32 (oldoffice01->encryptedVerifier[2]);
15687 oldoffice01->encryptedVerifier[3] = byte_swap_32 (oldoffice01->encryptedVerifier[3]);
15688
15689 oldoffice01->encryptedVerifierHash[0] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 0]);
15690 oldoffice01->encryptedVerifierHash[1] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 8]);
15691 oldoffice01->encryptedVerifierHash[2] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[16]);
15692 oldoffice01->encryptedVerifierHash[3] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[24]);
15693
15694 oldoffice01->encryptedVerifierHash[0] = byte_swap_32 (oldoffice01->encryptedVerifierHash[0]);
15695 oldoffice01->encryptedVerifierHash[1] = byte_swap_32 (oldoffice01->encryptedVerifierHash[1]);
15696 oldoffice01->encryptedVerifierHash[2] = byte_swap_32 (oldoffice01->encryptedVerifierHash[2]);
15697 oldoffice01->encryptedVerifierHash[3] = byte_swap_32 (oldoffice01->encryptedVerifierHash[3]);
15698
15699 /**
15700 * salt
15701 */
15702
15703 salt->salt_len = 16;
15704
15705 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &osalt_pos[ 0]);
15706 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &osalt_pos[ 8]);
15707 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &osalt_pos[16]);
15708 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &osalt_pos[24]);
15709
15710 salt->salt_buf[0] = byte_swap_32 (salt->salt_buf[0]);
15711 salt->salt_buf[1] = byte_swap_32 (salt->salt_buf[1]);
15712 salt->salt_buf[2] = byte_swap_32 (salt->salt_buf[2]);
15713 salt->salt_buf[3] = byte_swap_32 (salt->salt_buf[3]);
15714
15715 // this is a workaround as office produces multiple documents with the same salt
15716
15717 salt->salt_len += 32;
15718
15719 salt->salt_buf[ 4] = oldoffice01->encryptedVerifier[0];
15720 salt->salt_buf[ 5] = oldoffice01->encryptedVerifier[1];
15721 salt->salt_buf[ 6] = oldoffice01->encryptedVerifier[2];
15722 salt->salt_buf[ 7] = oldoffice01->encryptedVerifier[3];
15723 salt->salt_buf[ 8] = oldoffice01->encryptedVerifierHash[0];
15724 salt->salt_buf[ 9] = oldoffice01->encryptedVerifierHash[1];
15725 salt->salt_buf[10] = oldoffice01->encryptedVerifierHash[2];
15726 salt->salt_buf[11] = oldoffice01->encryptedVerifierHash[3];
15727
15728 /**
15729 * digest
15730 */
15731
15732 digest[0] = oldoffice01->encryptedVerifierHash[0];
15733 digest[1] = oldoffice01->encryptedVerifierHash[1];
15734 digest[2] = oldoffice01->encryptedVerifierHash[2];
15735 digest[3] = oldoffice01->encryptedVerifierHash[3];
15736
15737 return (PARSER_OK);
15738 }
15739
15740 int oldoffice01cm1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15741 {
15742 return oldoffice01_parse_hash (input_buf, input_len, hash_buf);
15743 }
15744
15745 int oldoffice01cm2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15746 {
15747 if ((input_len < DISPLAY_LEN_MIN_9720) || (input_len > DISPLAY_LEN_MAX_9720)) return (PARSER_GLOBAL_LENGTH);
15748
15749 if ((memcmp (SIGNATURE_OLDOFFICE0, input_buf, 12)) && (memcmp (SIGNATURE_OLDOFFICE1, input_buf, 12))) return (PARSER_SIGNATURE_UNMATCHED);
15750
15751 u32 *digest = (u32 *) hash_buf->digest;
15752
15753 salt_t *salt = hash_buf->salt;
15754
15755 oldoffice01_t *oldoffice01 = (oldoffice01_t *) hash_buf->esalt;
15756
15757 /**
15758 * parse line
15759 */
15760
15761 char *version_pos = input_buf + 11;
15762
15763 char *osalt_pos = strchr (version_pos, '*');
15764
15765 if (osalt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15766
15767 u32 version_len = osalt_pos - version_pos;
15768
15769 if (version_len != 1) return (PARSER_SALT_LENGTH);
15770
15771 osalt_pos++;
15772
15773 char *encryptedVerifier_pos = strchr (osalt_pos, '*');
15774
15775 if (encryptedVerifier_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15776
15777 u32 osalt_len = encryptedVerifier_pos - osalt_pos;
15778
15779 if (osalt_len != 32) return (PARSER_SALT_LENGTH);
15780
15781 encryptedVerifier_pos++;
15782
15783 char *encryptedVerifierHash_pos = strchr (encryptedVerifier_pos, '*');
15784
15785 if (encryptedVerifierHash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15786
15787 u32 encryptedVerifier_len = encryptedVerifierHash_pos - encryptedVerifier_pos;
15788
15789 if (encryptedVerifier_len != 32) return (PARSER_SALT_LENGTH);
15790
15791 encryptedVerifierHash_pos++;
15792
15793 char *rc4key_pos = strchr (encryptedVerifierHash_pos, ':');
15794
15795 if (rc4key_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15796
15797 u32 encryptedVerifierHash_len = rc4key_pos - encryptedVerifierHash_pos;
15798
15799 if (encryptedVerifierHash_len != 32) return (PARSER_SALT_LENGTH);
15800
15801 rc4key_pos++;
15802
15803 u32 rc4key_len = input_len - 11 - version_len - 1 - osalt_len - 1 - encryptedVerifier_len - 1 - encryptedVerifierHash_len - 1;
15804
15805 if (rc4key_len != 10) return (PARSER_SALT_LENGTH);
15806
15807 const uint version = *version_pos - 0x30;
15808
15809 if (version != 0 && version != 1) return (PARSER_SALT_VALUE);
15810
15811 /**
15812 * esalt
15813 */
15814
15815 oldoffice01->version = version;
15816
15817 oldoffice01->encryptedVerifier[0] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 0]);
15818 oldoffice01->encryptedVerifier[1] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 8]);
15819 oldoffice01->encryptedVerifier[2] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[16]);
15820 oldoffice01->encryptedVerifier[3] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[24]);
15821
15822 oldoffice01->encryptedVerifier[0] = byte_swap_32 (oldoffice01->encryptedVerifier[0]);
15823 oldoffice01->encryptedVerifier[1] = byte_swap_32 (oldoffice01->encryptedVerifier[1]);
15824 oldoffice01->encryptedVerifier[2] = byte_swap_32 (oldoffice01->encryptedVerifier[2]);
15825 oldoffice01->encryptedVerifier[3] = byte_swap_32 (oldoffice01->encryptedVerifier[3]);
15826
15827 oldoffice01->encryptedVerifierHash[0] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 0]);
15828 oldoffice01->encryptedVerifierHash[1] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 8]);
15829 oldoffice01->encryptedVerifierHash[2] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[16]);
15830 oldoffice01->encryptedVerifierHash[3] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[24]);
15831
15832 oldoffice01->encryptedVerifierHash[0] = byte_swap_32 (oldoffice01->encryptedVerifierHash[0]);
15833 oldoffice01->encryptedVerifierHash[1] = byte_swap_32 (oldoffice01->encryptedVerifierHash[1]);
15834 oldoffice01->encryptedVerifierHash[2] = byte_swap_32 (oldoffice01->encryptedVerifierHash[2]);
15835 oldoffice01->encryptedVerifierHash[3] = byte_swap_32 (oldoffice01->encryptedVerifierHash[3]);
15836
15837 oldoffice01->rc4key[1] = 0;
15838 oldoffice01->rc4key[0] = 0;
15839
15840 oldoffice01->rc4key[0] |= hex_convert (rc4key_pos[0]) << 28;
15841 oldoffice01->rc4key[0] |= hex_convert (rc4key_pos[1]) << 24;
15842 oldoffice01->rc4key[0] |= hex_convert (rc4key_pos[2]) << 20;
15843 oldoffice01->rc4key[0] |= hex_convert (rc4key_pos[3]) << 16;
15844 oldoffice01->rc4key[0] |= hex_convert (rc4key_pos[4]) << 12;
15845 oldoffice01->rc4key[0] |= hex_convert (rc4key_pos[5]) << 8;
15846 oldoffice01->rc4key[0] |= hex_convert (rc4key_pos[6]) << 4;
15847 oldoffice01->rc4key[0] |= hex_convert (rc4key_pos[7]) << 0;
15848 oldoffice01->rc4key[1] |= hex_convert (rc4key_pos[8]) << 28;
15849 oldoffice01->rc4key[1] |= hex_convert (rc4key_pos[9]) << 24;
15850
15851 oldoffice01->rc4key[0] = byte_swap_32 (oldoffice01->rc4key[0]);
15852 oldoffice01->rc4key[1] = byte_swap_32 (oldoffice01->rc4key[1]);
15853
15854 /**
15855 * salt
15856 */
15857
15858 salt->salt_len = 16;
15859
15860 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &osalt_pos[ 0]);
15861 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &osalt_pos[ 8]);
15862 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &osalt_pos[16]);
15863 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &osalt_pos[24]);
15864
15865 salt->salt_buf[0] = byte_swap_32 (salt->salt_buf[0]);
15866 salt->salt_buf[1] = byte_swap_32 (salt->salt_buf[1]);
15867 salt->salt_buf[2] = byte_swap_32 (salt->salt_buf[2]);
15868 salt->salt_buf[3] = byte_swap_32 (salt->salt_buf[3]);
15869
15870 // this is a workaround as office produces multiple documents with the same salt
15871
15872 salt->salt_len += 32;
15873
15874 salt->salt_buf[ 4] = oldoffice01->encryptedVerifier[0];
15875 salt->salt_buf[ 5] = oldoffice01->encryptedVerifier[1];
15876 salt->salt_buf[ 6] = oldoffice01->encryptedVerifier[2];
15877 salt->salt_buf[ 7] = oldoffice01->encryptedVerifier[3];
15878 salt->salt_buf[ 8] = oldoffice01->encryptedVerifierHash[0];
15879 salt->salt_buf[ 9] = oldoffice01->encryptedVerifierHash[1];
15880 salt->salt_buf[10] = oldoffice01->encryptedVerifierHash[2];
15881 salt->salt_buf[11] = oldoffice01->encryptedVerifierHash[3];
15882
15883 /**
15884 * digest
15885 */
15886
15887 digest[0] = oldoffice01->rc4key[0];
15888 digest[1] = oldoffice01->rc4key[1];
15889 digest[2] = 0;
15890 digest[3] = 0;
15891
15892 return (PARSER_OK);
15893 }
15894
15895 int oldoffice34_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15896 {
15897 if ((input_len < DISPLAY_LEN_MIN_9800) || (input_len > DISPLAY_LEN_MAX_9800)) return (PARSER_GLOBAL_LENGTH);
15898
15899 if ((memcmp (SIGNATURE_OLDOFFICE3, input_buf, 12)) && (memcmp (SIGNATURE_OLDOFFICE4, input_buf, 12))) return (PARSER_SIGNATURE_UNMATCHED);
15900
15901 u32 *digest = (u32 *) hash_buf->digest;
15902
15903 salt_t *salt = hash_buf->salt;
15904
15905 oldoffice34_t *oldoffice34 = (oldoffice34_t *) hash_buf->esalt;
15906
15907 /**
15908 * parse line
15909 */
15910
15911 char *version_pos = input_buf + 11;
15912
15913 char *osalt_pos = strchr (version_pos, '*');
15914
15915 if (osalt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15916
15917 u32 version_len = osalt_pos - version_pos;
15918
15919 if (version_len != 1) return (PARSER_SALT_LENGTH);
15920
15921 osalt_pos++;
15922
15923 char *encryptedVerifier_pos = strchr (osalt_pos, '*');
15924
15925 if (encryptedVerifier_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15926
15927 u32 osalt_len = encryptedVerifier_pos - osalt_pos;
15928
15929 if (osalt_len != 32) return (PARSER_SALT_LENGTH);
15930
15931 encryptedVerifier_pos++;
15932
15933 char *encryptedVerifierHash_pos = strchr (encryptedVerifier_pos, '*');
15934
15935 if (encryptedVerifierHash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15936
15937 u32 encryptedVerifier_len = encryptedVerifierHash_pos - encryptedVerifier_pos;
15938
15939 if (encryptedVerifier_len != 32) return (PARSER_SALT_LENGTH);
15940
15941 encryptedVerifierHash_pos++;
15942
15943 u32 encryptedVerifierHash_len = input_len - 11 - version_len - 1 - osalt_len - 1 - encryptedVerifier_len - 1;
15944
15945 if (encryptedVerifierHash_len != 40) return (PARSER_SALT_LENGTH);
15946
15947 const uint version = *version_pos - 0x30;
15948
15949 if (version != 3 && version != 4) return (PARSER_SALT_VALUE);
15950
15951 /**
15952 * esalt
15953 */
15954
15955 oldoffice34->version = version;
15956
15957 oldoffice34->encryptedVerifier[0] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 0]);
15958 oldoffice34->encryptedVerifier[1] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 8]);
15959 oldoffice34->encryptedVerifier[2] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[16]);
15960 oldoffice34->encryptedVerifier[3] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[24]);
15961
15962 oldoffice34->encryptedVerifier[0] = byte_swap_32 (oldoffice34->encryptedVerifier[0]);
15963 oldoffice34->encryptedVerifier[1] = byte_swap_32 (oldoffice34->encryptedVerifier[1]);
15964 oldoffice34->encryptedVerifier[2] = byte_swap_32 (oldoffice34->encryptedVerifier[2]);
15965 oldoffice34->encryptedVerifier[3] = byte_swap_32 (oldoffice34->encryptedVerifier[3]);
15966
15967 oldoffice34->encryptedVerifierHash[0] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 0]);
15968 oldoffice34->encryptedVerifierHash[1] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 8]);
15969 oldoffice34->encryptedVerifierHash[2] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[16]);
15970 oldoffice34->encryptedVerifierHash[3] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[24]);
15971 oldoffice34->encryptedVerifierHash[4] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[32]);
15972
15973 oldoffice34->encryptedVerifierHash[0] = byte_swap_32 (oldoffice34->encryptedVerifierHash[0]);
15974 oldoffice34->encryptedVerifierHash[1] = byte_swap_32 (oldoffice34->encryptedVerifierHash[1]);
15975 oldoffice34->encryptedVerifierHash[2] = byte_swap_32 (oldoffice34->encryptedVerifierHash[2]);
15976 oldoffice34->encryptedVerifierHash[3] = byte_swap_32 (oldoffice34->encryptedVerifierHash[3]);
15977 oldoffice34->encryptedVerifierHash[4] = byte_swap_32 (oldoffice34->encryptedVerifierHash[4]);
15978
15979 /**
15980 * salt
15981 */
15982
15983 salt->salt_len = 16;
15984
15985 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &osalt_pos[ 0]);
15986 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &osalt_pos[ 8]);
15987 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &osalt_pos[16]);
15988 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &osalt_pos[24]);
15989
15990 // this is a workaround as office produces multiple documents with the same salt
15991
15992 salt->salt_len += 32;
15993
15994 salt->salt_buf[ 4] = oldoffice34->encryptedVerifier[0];
15995 salt->salt_buf[ 5] = oldoffice34->encryptedVerifier[1];
15996 salt->salt_buf[ 6] = oldoffice34->encryptedVerifier[2];
15997 salt->salt_buf[ 7] = oldoffice34->encryptedVerifier[3];
15998 salt->salt_buf[ 8] = oldoffice34->encryptedVerifierHash[0];
15999 salt->salt_buf[ 9] = oldoffice34->encryptedVerifierHash[1];
16000 salt->salt_buf[10] = oldoffice34->encryptedVerifierHash[2];
16001 salt->salt_buf[11] = oldoffice34->encryptedVerifierHash[3];
16002
16003 /**
16004 * digest
16005 */
16006
16007 digest[0] = oldoffice34->encryptedVerifierHash[0];
16008 digest[1] = oldoffice34->encryptedVerifierHash[1];
16009 digest[2] = oldoffice34->encryptedVerifierHash[2];
16010 digest[3] = oldoffice34->encryptedVerifierHash[3];
16011
16012 return (PARSER_OK);
16013 }
16014
16015 int oldoffice34cm1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16016 {
16017 if (memcmp (SIGNATURE_OLDOFFICE3, input_buf, 12)) return (PARSER_SIGNATURE_UNMATCHED);
16018
16019 return oldoffice34_parse_hash (input_buf, input_len, hash_buf);
16020 }
16021
16022 int oldoffice34cm2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16023 {
16024 if ((input_len < DISPLAY_LEN_MIN_9820) || (input_len > DISPLAY_LEN_MAX_9820)) return (PARSER_GLOBAL_LENGTH);
16025
16026 if (memcmp (SIGNATURE_OLDOFFICE3, input_buf, 12)) return (PARSER_SIGNATURE_UNMATCHED);
16027
16028 u32 *digest = (u32 *) hash_buf->digest;
16029
16030 salt_t *salt = hash_buf->salt;
16031
16032 oldoffice34_t *oldoffice34 = (oldoffice34_t *) hash_buf->esalt;
16033
16034 /**
16035 * parse line
16036 */
16037
16038 char *version_pos = input_buf + 11;
16039
16040 char *osalt_pos = strchr (version_pos, '*');
16041
16042 if (osalt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16043
16044 u32 version_len = osalt_pos - version_pos;
16045
16046 if (version_len != 1) return (PARSER_SALT_LENGTH);
16047
16048 osalt_pos++;
16049
16050 char *encryptedVerifier_pos = strchr (osalt_pos, '*');
16051
16052 if (encryptedVerifier_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16053
16054 u32 osalt_len = encryptedVerifier_pos - osalt_pos;
16055
16056 if (osalt_len != 32) return (PARSER_SALT_LENGTH);
16057
16058 encryptedVerifier_pos++;
16059
16060 char *encryptedVerifierHash_pos = strchr (encryptedVerifier_pos, '*');
16061
16062 if (encryptedVerifierHash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16063
16064 u32 encryptedVerifier_len = encryptedVerifierHash_pos - encryptedVerifier_pos;
16065
16066 if (encryptedVerifier_len != 32) return (PARSER_SALT_LENGTH);
16067
16068 encryptedVerifierHash_pos++;
16069
16070 char *rc4key_pos = strchr (encryptedVerifierHash_pos, ':');
16071
16072 if (rc4key_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16073
16074 u32 encryptedVerifierHash_len = rc4key_pos - encryptedVerifierHash_pos;
16075
16076 if (encryptedVerifierHash_len != 40) return (PARSER_SALT_LENGTH);
16077
16078 rc4key_pos++;
16079
16080 u32 rc4key_len = input_len - 11 - version_len - 1 - osalt_len - 1 - encryptedVerifier_len - 1 - encryptedVerifierHash_len - 1;
16081
16082 if (rc4key_len != 10) return (PARSER_SALT_LENGTH);
16083
16084 const uint version = *version_pos - 0x30;
16085
16086 if (version != 3 && version != 4) return (PARSER_SALT_VALUE);
16087
16088 /**
16089 * esalt
16090 */
16091
16092 oldoffice34->version = version;
16093
16094 oldoffice34->encryptedVerifier[0] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 0]);
16095 oldoffice34->encryptedVerifier[1] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 8]);
16096 oldoffice34->encryptedVerifier[2] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[16]);
16097 oldoffice34->encryptedVerifier[3] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[24]);
16098
16099 oldoffice34->encryptedVerifier[0] = byte_swap_32 (oldoffice34->encryptedVerifier[0]);
16100 oldoffice34->encryptedVerifier[1] = byte_swap_32 (oldoffice34->encryptedVerifier[1]);
16101 oldoffice34->encryptedVerifier[2] = byte_swap_32 (oldoffice34->encryptedVerifier[2]);
16102 oldoffice34->encryptedVerifier[3] = byte_swap_32 (oldoffice34->encryptedVerifier[3]);
16103
16104 oldoffice34->encryptedVerifierHash[0] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 0]);
16105 oldoffice34->encryptedVerifierHash[1] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 8]);
16106 oldoffice34->encryptedVerifierHash[2] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[16]);
16107 oldoffice34->encryptedVerifierHash[3] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[24]);
16108 oldoffice34->encryptedVerifierHash[4] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[32]);
16109
16110 oldoffice34->encryptedVerifierHash[0] = byte_swap_32 (oldoffice34->encryptedVerifierHash[0]);
16111 oldoffice34->encryptedVerifierHash[1] = byte_swap_32 (oldoffice34->encryptedVerifierHash[1]);
16112 oldoffice34->encryptedVerifierHash[2] = byte_swap_32 (oldoffice34->encryptedVerifierHash[2]);
16113 oldoffice34->encryptedVerifierHash[3] = byte_swap_32 (oldoffice34->encryptedVerifierHash[3]);
16114 oldoffice34->encryptedVerifierHash[4] = byte_swap_32 (oldoffice34->encryptedVerifierHash[4]);
16115
16116 oldoffice34->rc4key[1] = 0;
16117 oldoffice34->rc4key[0] = 0;
16118
16119 oldoffice34->rc4key[0] |= hex_convert (rc4key_pos[0]) << 28;
16120 oldoffice34->rc4key[0] |= hex_convert (rc4key_pos[1]) << 24;
16121 oldoffice34->rc4key[0] |= hex_convert (rc4key_pos[2]) << 20;
16122 oldoffice34->rc4key[0] |= hex_convert (rc4key_pos[3]) << 16;
16123 oldoffice34->rc4key[0] |= hex_convert (rc4key_pos[4]) << 12;
16124 oldoffice34->rc4key[0] |= hex_convert (rc4key_pos[5]) << 8;
16125 oldoffice34->rc4key[0] |= hex_convert (rc4key_pos[6]) << 4;
16126 oldoffice34->rc4key[0] |= hex_convert (rc4key_pos[7]) << 0;
16127 oldoffice34->rc4key[1] |= hex_convert (rc4key_pos[8]) << 28;
16128 oldoffice34->rc4key[1] |= hex_convert (rc4key_pos[9]) << 24;
16129
16130 oldoffice34->rc4key[0] = byte_swap_32 (oldoffice34->rc4key[0]);
16131 oldoffice34->rc4key[1] = byte_swap_32 (oldoffice34->rc4key[1]);
16132
16133 /**
16134 * salt
16135 */
16136
16137 salt->salt_len = 16;
16138
16139 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &osalt_pos[ 0]);
16140 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &osalt_pos[ 8]);
16141 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &osalt_pos[16]);
16142 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &osalt_pos[24]);
16143
16144 // this is a workaround as office produces multiple documents with the same salt
16145
16146 salt->salt_len += 32;
16147
16148 salt->salt_buf[ 4] = oldoffice34->encryptedVerifier[0];
16149 salt->salt_buf[ 5] = oldoffice34->encryptedVerifier[1];
16150 salt->salt_buf[ 6] = oldoffice34->encryptedVerifier[2];
16151 salt->salt_buf[ 7] = oldoffice34->encryptedVerifier[3];
16152 salt->salt_buf[ 8] = oldoffice34->encryptedVerifierHash[0];
16153 salt->salt_buf[ 9] = oldoffice34->encryptedVerifierHash[1];
16154 salt->salt_buf[10] = oldoffice34->encryptedVerifierHash[2];
16155 salt->salt_buf[11] = oldoffice34->encryptedVerifierHash[3];
16156
16157 /**
16158 * digest
16159 */
16160
16161 digest[0] = oldoffice34->rc4key[0];
16162 digest[1] = oldoffice34->rc4key[1];
16163 digest[2] = 0;
16164 digest[3] = 0;
16165
16166 return (PARSER_OK);
16167 }
16168
16169 int radmin2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16170 {
16171 if ((input_len < DISPLAY_LEN_MIN_9900) || (input_len > DISPLAY_LEN_MAX_9900)) return (PARSER_GLOBAL_LENGTH);
16172
16173 u32 *digest = (u32 *) hash_buf->digest;
16174
16175 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
16176 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
16177 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
16178 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
16179
16180 digest[0] = byte_swap_32 (digest[0]);
16181 digest[1] = byte_swap_32 (digest[1]);
16182 digest[2] = byte_swap_32 (digest[2]);
16183 digest[3] = byte_swap_32 (digest[3]);
16184
16185 return (PARSER_OK);
16186 }
16187
16188 int djangosha1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16189 {
16190 if ((input_len < DISPLAY_LEN_MIN_124) || (input_len > DISPLAY_LEN_MAX_124)) return (PARSER_GLOBAL_LENGTH);
16191
16192 if ((memcmp (SIGNATURE_DJANGOSHA1, input_buf, 5)) && (memcmp (SIGNATURE_DJANGOSHA1, input_buf, 5))) return (PARSER_SIGNATURE_UNMATCHED);
16193
16194 u32 *digest = (u32 *) hash_buf->digest;
16195
16196 salt_t *salt = hash_buf->salt;
16197
16198 char *signature_pos = input_buf;
16199
16200 char *salt_pos = strchr (signature_pos, '$');
16201
16202 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16203
16204 u32 signature_len = salt_pos - signature_pos;
16205
16206 if (signature_len != 4) return (PARSER_SIGNATURE_UNMATCHED);
16207
16208 salt_pos++;
16209
16210 char *hash_pos = strchr (salt_pos, '$');
16211
16212 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16213
16214 u32 salt_len = hash_pos - salt_pos;
16215
16216 if (salt_len > 32) return (PARSER_SALT_LENGTH);
16217
16218 hash_pos++;
16219
16220 u32 hash_len = input_len - signature_len - 1 - salt_len - 1;
16221
16222 if (hash_len != 40) return (PARSER_SALT_LENGTH);
16223
16224 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
16225 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
16226 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
16227 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
16228 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
16229
16230 digest[0] -= SHA1M_A;
16231 digest[1] -= SHA1M_B;
16232 digest[2] -= SHA1M_C;
16233 digest[3] -= SHA1M_D;
16234 digest[4] -= SHA1M_E;
16235
16236 char *salt_buf_ptr = (char *) salt->salt_buf;
16237
16238 memcpy (salt_buf_ptr, salt_pos, salt_len);
16239
16240 salt->salt_len = salt_len;
16241
16242 return (PARSER_OK);
16243 }
16244
16245 int djangopbkdf2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16246 {
16247 if ((input_len < DISPLAY_LEN_MIN_10000) || (input_len > DISPLAY_LEN_MAX_10000)) return (PARSER_GLOBAL_LENGTH);
16248
16249 if (memcmp (SIGNATURE_DJANGOPBKDF2, input_buf, 14)) return (PARSER_SIGNATURE_UNMATCHED);
16250
16251 u32 *digest = (u32 *) hash_buf->digest;
16252
16253 salt_t *salt = hash_buf->salt;
16254
16255 pbkdf2_sha256_t *pbkdf2_sha256 = (pbkdf2_sha256_t *) hash_buf->esalt;
16256
16257 /**
16258 * parse line
16259 */
16260
16261 char *iter_pos = input_buf + 14;
16262
16263 const int iter = atoi (iter_pos);
16264
16265 if (iter < 1) return (PARSER_SALT_ITERATION);
16266
16267 salt->salt_iter = iter - 1;
16268
16269 char *salt_pos = strchr (iter_pos, '$');
16270
16271 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16272
16273 salt_pos++;
16274
16275 char *hash_pos = strchr (salt_pos, '$');
16276
16277 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16278
16279 const uint salt_len = hash_pos - salt_pos;
16280
16281 hash_pos++;
16282
16283 char *salt_buf_ptr = (char *) pbkdf2_sha256->salt_buf;
16284
16285 memcpy (salt_buf_ptr, salt_pos, salt_len);
16286
16287 salt->salt_len = salt_len;
16288
16289 salt_buf_ptr[salt_len + 3] = 0x01;
16290 salt_buf_ptr[salt_len + 4] = 0x80;
16291
16292 // add some stuff to normal salt to make sorted happy
16293
16294 salt->salt_buf[0] = pbkdf2_sha256->salt_buf[0];
16295 salt->salt_buf[1] = pbkdf2_sha256->salt_buf[1];
16296 salt->salt_buf[2] = pbkdf2_sha256->salt_buf[2];
16297 salt->salt_buf[3] = pbkdf2_sha256->salt_buf[3];
16298 salt->salt_buf[4] = salt->salt_iter;
16299
16300 // base64 decode hash
16301
16302 u8 tmp_buf[100] = { 0 };
16303
16304 uint hash_len = input_len - (hash_pos - input_buf);
16305
16306 if (hash_len != 44) return (PARSER_HASH_LENGTH);
16307
16308 base64_decode (base64_to_int, (const u8 *) hash_pos, hash_len, tmp_buf);
16309
16310 memcpy (digest, tmp_buf, 32);
16311
16312 digest[0] = byte_swap_32 (digest[0]);
16313 digest[1] = byte_swap_32 (digest[1]);
16314 digest[2] = byte_swap_32 (digest[2]);
16315 digest[3] = byte_swap_32 (digest[3]);
16316 digest[4] = byte_swap_32 (digest[4]);
16317 digest[5] = byte_swap_32 (digest[5]);
16318 digest[6] = byte_swap_32 (digest[6]);
16319 digest[7] = byte_swap_32 (digest[7]);
16320
16321 return (PARSER_OK);
16322 }
16323
16324 int siphash_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16325 {
16326 if ((input_len < DISPLAY_LEN_MIN_10100) || (input_len > DISPLAY_LEN_MAX_10100)) return (PARSER_GLOBAL_LENGTH);
16327
16328 u32 *digest = (u32 *) hash_buf->digest;
16329
16330 salt_t *salt = hash_buf->salt;
16331
16332 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
16333 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
16334 digest[2] = 0;
16335 digest[3] = 0;
16336
16337 digest[0] = byte_swap_32 (digest[0]);
16338 digest[1] = byte_swap_32 (digest[1]);
16339
16340 if (input_buf[16] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
16341 if (input_buf[18] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
16342 if (input_buf[20] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
16343
16344 char iter_c = input_buf[17];
16345 char iter_d = input_buf[19];
16346
16347 // atm only defaults, let's see if there's more request
16348 if (iter_c != '2') return (PARSER_SALT_ITERATION);
16349 if (iter_d != '4') return (PARSER_SALT_ITERATION);
16350
16351 char *salt_buf = input_buf + 16 + 1 + 1 + 1 + 1 + 1;
16352
16353 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_buf[ 0]);
16354 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_buf[ 8]);
16355 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &salt_buf[16]);
16356 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &salt_buf[24]);
16357
16358 salt->salt_buf[0] = byte_swap_32 (salt->salt_buf[0]);
16359 salt->salt_buf[1] = byte_swap_32 (salt->salt_buf[1]);
16360 salt->salt_buf[2] = byte_swap_32 (salt->salt_buf[2]);
16361 salt->salt_buf[3] = byte_swap_32 (salt->salt_buf[3]);
16362
16363 salt->salt_len = 16;
16364
16365 return (PARSER_OK);
16366 }
16367
16368 int crammd5_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16369 {
16370 if ((input_len < DISPLAY_LEN_MIN_10200) || (input_len > DISPLAY_LEN_MAX_10200)) return (PARSER_GLOBAL_LENGTH);
16371
16372 if (memcmp (SIGNATURE_CRAM_MD5, input_buf, 10)) return (PARSER_SIGNATURE_UNMATCHED);
16373
16374 u32 *digest = (u32 *) hash_buf->digest;
16375
16376 cram_md5_t *cram_md5 = (cram_md5_t *) hash_buf->esalt;
16377
16378 salt_t *salt = hash_buf->salt;
16379
16380 char *salt_pos = input_buf + 10;
16381
16382 char *hash_pos = strchr (salt_pos, '$');
16383
16384 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16385
16386 uint salt_len = hash_pos - salt_pos;
16387
16388 hash_pos++;
16389
16390 uint hash_len = input_len - 10 - salt_len - 1;
16391
16392 // base64 decode salt
16393
16394 if (salt_len > 133) return (PARSER_SALT_LENGTH);
16395
16396 u8 tmp_buf[100] = { 0 };
16397
16398 salt_len = base64_decode (base64_to_int, (const u8 *) salt_pos, salt_len, tmp_buf);
16399
16400 if (salt_len > 55) return (PARSER_SALT_LENGTH);
16401
16402 tmp_buf[salt_len] = 0x80;
16403
16404 memcpy (&salt->salt_buf, tmp_buf, salt_len + 1);
16405
16406 salt->salt_len = salt_len;
16407
16408 // base64 decode hash
16409
16410 if (hash_len > 133) return (PARSER_HASH_LENGTH);
16411
16412 memset (tmp_buf, 0, sizeof (tmp_buf));
16413
16414 hash_len = base64_decode (base64_to_int, (const u8 *) hash_pos, hash_len, tmp_buf);
16415
16416 if (hash_len < 32 + 1) return (PARSER_SALT_LENGTH);
16417
16418 uint user_len = hash_len - 32;
16419
16420 const u8 *tmp_hash = tmp_buf + user_len;
16421
16422 user_len--; // skip the trailing space
16423
16424 digest[0] = hex_to_u32 (&tmp_hash[ 0]);
16425 digest[1] = hex_to_u32 (&tmp_hash[ 8]);
16426 digest[2] = hex_to_u32 (&tmp_hash[16]);
16427 digest[3] = hex_to_u32 (&tmp_hash[24]);
16428
16429 digest[0] = byte_swap_32 (digest[0]);
16430 digest[1] = byte_swap_32 (digest[1]);
16431 digest[2] = byte_swap_32 (digest[2]);
16432 digest[3] = byte_swap_32 (digest[3]);
16433
16434 // store username for host only (output hash if cracked)
16435
16436 memset (cram_md5->user, 0, sizeof (cram_md5->user));
16437 memcpy (cram_md5->user, tmp_buf, user_len);
16438
16439 return (PARSER_OK);
16440 }
16441
16442 int saph_sha1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16443 {
16444 if ((input_len < DISPLAY_LEN_MIN_10300) || (input_len > DISPLAY_LEN_MAX_10300)) return (PARSER_GLOBAL_LENGTH);
16445
16446 if (memcmp (SIGNATURE_SAPH_SHA1, input_buf, 10)) return (PARSER_SIGNATURE_UNMATCHED);
16447
16448 u32 *digest = (u32 *) hash_buf->digest;
16449
16450 salt_t *salt = hash_buf->salt;
16451
16452 char *iter_pos = input_buf + 10;
16453
16454 u32 iter = atoi (iter_pos);
16455
16456 if (iter < 1)
16457 {
16458 return (PARSER_SALT_ITERATION);
16459 }
16460
16461 iter--; // first iteration is special
16462
16463 salt->salt_iter = iter;
16464
16465 char *base64_pos = strchr (iter_pos, '}');
16466
16467 if (base64_pos == NULL)
16468 {
16469 return (PARSER_SIGNATURE_UNMATCHED);
16470 }
16471
16472 base64_pos++;
16473
16474 // base64 decode salt
16475
16476 u32 base64_len = input_len - (base64_pos - input_buf);
16477
16478 u8 tmp_buf[100] = { 0 };
16479
16480 u32 decoded_len = base64_decode (base64_to_int, (const u8 *) base64_pos, base64_len, tmp_buf);
16481
16482 if (decoded_len < 24)
16483 {
16484 return (PARSER_SALT_LENGTH);
16485 }
16486
16487 // copy the salt
16488
16489 uint salt_len = decoded_len - 20;
16490
16491 if (salt_len < 4) return (PARSER_SALT_LENGTH);
16492 if (salt_len > 16) return (PARSER_SALT_LENGTH);
16493
16494 memcpy (&salt->salt_buf, tmp_buf + 20, salt_len);
16495
16496 salt->salt_len = salt_len;
16497
16498 // set digest
16499
16500 u32 *digest_ptr = (u32*) tmp_buf;
16501
16502 digest[0] = byte_swap_32 (digest_ptr[0]);
16503 digest[1] = byte_swap_32 (digest_ptr[1]);
16504 digest[2] = byte_swap_32 (digest_ptr[2]);
16505 digest[3] = byte_swap_32 (digest_ptr[3]);
16506 digest[4] = byte_swap_32 (digest_ptr[4]);
16507
16508 return (PARSER_OK);
16509 }
16510
16511 int redmine_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16512 {
16513 if ((input_len < DISPLAY_LEN_MIN_7600) || (input_len > DISPLAY_LEN_MAX_7600)) return (PARSER_GLOBAL_LENGTH);
16514
16515 u32 *digest = (u32 *) hash_buf->digest;
16516
16517 salt_t *salt = hash_buf->salt;
16518
16519 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
16520 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
16521 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
16522 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
16523 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
16524
16525 if (input_buf[40] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
16526
16527 uint salt_len = input_len - 40 - 1;
16528
16529 char *salt_buf = input_buf + 40 + 1;
16530
16531 char *salt_buf_ptr = (char *) salt->salt_buf;
16532
16533 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
16534
16535 if (salt_len != 32) return (PARSER_SALT_LENGTH);
16536
16537 salt->salt_len = salt_len;
16538
16539 return (PARSER_OK);
16540 }
16541
16542 int pdf11_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16543 {
16544 if ((input_len < DISPLAY_LEN_MIN_10400) || (input_len > DISPLAY_LEN_MAX_10400)) return (PARSER_GLOBAL_LENGTH);
16545
16546 if ((memcmp (SIGNATURE_PDF, input_buf, 5)) && (memcmp (SIGNATURE_PDF, input_buf, 5))) return (PARSER_SIGNATURE_UNMATCHED);
16547
16548 u32 *digest = (u32 *) hash_buf->digest;
16549
16550 salt_t *salt = hash_buf->salt;
16551
16552 pdf_t *pdf = (pdf_t *) hash_buf->esalt;
16553
16554 /**
16555 * parse line
16556 */
16557
16558 char *V_pos = input_buf + 5;
16559
16560 char *R_pos = strchr (V_pos, '*');
16561
16562 if (R_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16563
16564 u32 V_len = R_pos - V_pos;
16565
16566 R_pos++;
16567
16568 char *bits_pos = strchr (R_pos, '*');
16569
16570 if (bits_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16571
16572 u32 R_len = bits_pos - R_pos;
16573
16574 bits_pos++;
16575
16576 char *P_pos = strchr (bits_pos, '*');
16577
16578 if (P_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16579
16580 u32 bits_len = P_pos - bits_pos;
16581
16582 P_pos++;
16583
16584 char *enc_md_pos = strchr (P_pos, '*');
16585
16586 if (enc_md_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16587
16588 u32 P_len = enc_md_pos - P_pos;
16589
16590 enc_md_pos++;
16591
16592 char *id_len_pos = strchr (enc_md_pos, '*');
16593
16594 if (id_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16595
16596 u32 enc_md_len = id_len_pos - enc_md_pos;
16597
16598 id_len_pos++;
16599
16600 char *id_buf_pos = strchr (id_len_pos, '*');
16601
16602 if (id_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16603
16604 u32 id_len_len = id_buf_pos - id_len_pos;
16605
16606 id_buf_pos++;
16607
16608 char *u_len_pos = strchr (id_buf_pos, '*');
16609
16610 if (u_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16611
16612 u32 id_buf_len = u_len_pos - id_buf_pos;
16613
16614 if (id_buf_len != 32) return (PARSER_SALT_LENGTH);
16615
16616 u_len_pos++;
16617
16618 char *u_buf_pos = strchr (u_len_pos, '*');
16619
16620 if (u_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16621
16622 u32 u_len_len = u_buf_pos - u_len_pos;
16623
16624 u_buf_pos++;
16625
16626 char *o_len_pos = strchr (u_buf_pos, '*');
16627
16628 if (o_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16629
16630 u32 u_buf_len = o_len_pos - u_buf_pos;
16631
16632 if (u_buf_len != 64) return (PARSER_SALT_LENGTH);
16633
16634 o_len_pos++;
16635
16636 char *o_buf_pos = strchr (o_len_pos, '*');
16637
16638 if (o_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16639
16640 u32 o_len_len = o_buf_pos - o_len_pos;
16641
16642 o_buf_pos++;
16643
16644 u32 o_buf_len = input_len - 5 - V_len - 1 - R_len - 1 - bits_len - 1 - P_len - 1 - enc_md_len - 1 - id_len_len - 1 - id_buf_len - 1 - u_len_len - 1 - u_buf_len - 1 - o_len_len - 1;
16645
16646 if (o_buf_len != 64) return (PARSER_SALT_LENGTH);
16647
16648 // validate data
16649
16650 const int V = atoi (V_pos);
16651 const int R = atoi (R_pos);
16652 const int P = atoi (P_pos);
16653
16654 if (V != 1) return (PARSER_SALT_VALUE);
16655 if (R != 2) return (PARSER_SALT_VALUE);
16656
16657 const int enc_md = atoi (enc_md_pos);
16658
16659 if ((enc_md != 0) && (enc_md != 1)) return (PARSER_SALT_VALUE);
16660
16661 const int id_len = atoi (id_len_pos);
16662 const int u_len = atoi (u_len_pos);
16663 const int o_len = atoi (o_len_pos);
16664
16665 if (id_len != 16) return (PARSER_SALT_VALUE);
16666 if (u_len != 32) return (PARSER_SALT_VALUE);
16667 if (o_len != 32) return (PARSER_SALT_VALUE);
16668
16669 const int bits = atoi (bits_pos);
16670
16671 if (bits != 40) return (PARSER_SALT_VALUE);
16672
16673 // copy data to esalt
16674
16675 pdf->V = V;
16676 pdf->R = R;
16677 pdf->P = P;
16678
16679 pdf->enc_md = enc_md;
16680
16681 pdf->id_buf[0] = hex_to_u32 ((const u8 *) &id_buf_pos[ 0]);
16682 pdf->id_buf[1] = hex_to_u32 ((const u8 *) &id_buf_pos[ 8]);
16683 pdf->id_buf[2] = hex_to_u32 ((const u8 *) &id_buf_pos[16]);
16684 pdf->id_buf[3] = hex_to_u32 ((const u8 *) &id_buf_pos[24]);
16685 pdf->id_len = id_len;
16686
16687 pdf->u_buf[0] = hex_to_u32 ((const u8 *) &u_buf_pos[ 0]);
16688 pdf->u_buf[1] = hex_to_u32 ((const u8 *) &u_buf_pos[ 8]);
16689 pdf->u_buf[2] = hex_to_u32 ((const u8 *) &u_buf_pos[16]);
16690 pdf->u_buf[3] = hex_to_u32 ((const u8 *) &u_buf_pos[24]);
16691 pdf->u_buf[4] = hex_to_u32 ((const u8 *) &u_buf_pos[32]);
16692 pdf->u_buf[5] = hex_to_u32 ((const u8 *) &u_buf_pos[40]);
16693 pdf->u_buf[6] = hex_to_u32 ((const u8 *) &u_buf_pos[48]);
16694 pdf->u_buf[7] = hex_to_u32 ((const u8 *) &u_buf_pos[56]);
16695 pdf->u_len = u_len;
16696
16697 pdf->o_buf[0] = hex_to_u32 ((const u8 *) &o_buf_pos[ 0]);
16698 pdf->o_buf[1] = hex_to_u32 ((const u8 *) &o_buf_pos[ 8]);
16699 pdf->o_buf[2] = hex_to_u32 ((const u8 *) &o_buf_pos[16]);
16700 pdf->o_buf[3] = hex_to_u32 ((const u8 *) &o_buf_pos[24]);
16701 pdf->o_buf[4] = hex_to_u32 ((const u8 *) &o_buf_pos[32]);
16702 pdf->o_buf[5] = hex_to_u32 ((const u8 *) &o_buf_pos[40]);
16703 pdf->o_buf[6] = hex_to_u32 ((const u8 *) &o_buf_pos[48]);
16704 pdf->o_buf[7] = hex_to_u32 ((const u8 *) &o_buf_pos[56]);
16705 pdf->o_len = o_len;
16706
16707 pdf->id_buf[0] = byte_swap_32 (pdf->id_buf[0]);
16708 pdf->id_buf[1] = byte_swap_32 (pdf->id_buf[1]);
16709 pdf->id_buf[2] = byte_swap_32 (pdf->id_buf[2]);
16710 pdf->id_buf[3] = byte_swap_32 (pdf->id_buf[3]);
16711
16712 pdf->u_buf[0] = byte_swap_32 (pdf->u_buf[0]);
16713 pdf->u_buf[1] = byte_swap_32 (pdf->u_buf[1]);
16714 pdf->u_buf[2] = byte_swap_32 (pdf->u_buf[2]);
16715 pdf->u_buf[3] = byte_swap_32 (pdf->u_buf[3]);
16716 pdf->u_buf[4] = byte_swap_32 (pdf->u_buf[4]);
16717 pdf->u_buf[5] = byte_swap_32 (pdf->u_buf[5]);
16718 pdf->u_buf[6] = byte_swap_32 (pdf->u_buf[6]);
16719 pdf->u_buf[7] = byte_swap_32 (pdf->u_buf[7]);
16720
16721 pdf->o_buf[0] = byte_swap_32 (pdf->o_buf[0]);
16722 pdf->o_buf[1] = byte_swap_32 (pdf->o_buf[1]);
16723 pdf->o_buf[2] = byte_swap_32 (pdf->o_buf[2]);
16724 pdf->o_buf[3] = byte_swap_32 (pdf->o_buf[3]);
16725 pdf->o_buf[4] = byte_swap_32 (pdf->o_buf[4]);
16726 pdf->o_buf[5] = byte_swap_32 (pdf->o_buf[5]);
16727 pdf->o_buf[6] = byte_swap_32 (pdf->o_buf[6]);
16728 pdf->o_buf[7] = byte_swap_32 (pdf->o_buf[7]);
16729
16730 // we use ID for salt, maybe needs to change, we will see...
16731
16732 salt->salt_buf[0] = pdf->id_buf[0];
16733 salt->salt_buf[1] = pdf->id_buf[1];
16734 salt->salt_buf[2] = pdf->id_buf[2];
16735 salt->salt_buf[3] = pdf->id_buf[3];
16736 salt->salt_len = pdf->id_len;
16737
16738 digest[0] = pdf->u_buf[0];
16739 digest[1] = pdf->u_buf[1];
16740 digest[2] = pdf->u_buf[2];
16741 digest[3] = pdf->u_buf[3];
16742
16743 return (PARSER_OK);
16744 }
16745
16746 int pdf11cm1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16747 {
16748 return pdf11_parse_hash (input_buf, input_len, hash_buf);
16749 }
16750
16751 int pdf11cm2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16752 {
16753 if ((input_len < DISPLAY_LEN_MIN_10420) || (input_len > DISPLAY_LEN_MAX_10420)) return (PARSER_GLOBAL_LENGTH);
16754
16755 if ((memcmp (SIGNATURE_PDF, input_buf, 5)) && (memcmp (SIGNATURE_PDF, input_buf, 5))) return (PARSER_SIGNATURE_UNMATCHED);
16756
16757 u32 *digest = (u32 *) hash_buf->digest;
16758
16759 salt_t *salt = hash_buf->salt;
16760
16761 pdf_t *pdf = (pdf_t *) hash_buf->esalt;
16762
16763 /**
16764 * parse line
16765 */
16766
16767 char *V_pos = input_buf + 5;
16768
16769 char *R_pos = strchr (V_pos, '*');
16770
16771 if (R_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16772
16773 u32 V_len = R_pos - V_pos;
16774
16775 R_pos++;
16776
16777 char *bits_pos = strchr (R_pos, '*');
16778
16779 if (bits_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16780
16781 u32 R_len = bits_pos - R_pos;
16782
16783 bits_pos++;
16784
16785 char *P_pos = strchr (bits_pos, '*');
16786
16787 if (P_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16788
16789 u32 bits_len = P_pos - bits_pos;
16790
16791 P_pos++;
16792
16793 char *enc_md_pos = strchr (P_pos, '*');
16794
16795 if (enc_md_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16796
16797 u32 P_len = enc_md_pos - P_pos;
16798
16799 enc_md_pos++;
16800
16801 char *id_len_pos = strchr (enc_md_pos, '*');
16802
16803 if (id_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16804
16805 u32 enc_md_len = id_len_pos - enc_md_pos;
16806
16807 id_len_pos++;
16808
16809 char *id_buf_pos = strchr (id_len_pos, '*');
16810
16811 if (id_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16812
16813 u32 id_len_len = id_buf_pos - id_len_pos;
16814
16815 id_buf_pos++;
16816
16817 char *u_len_pos = strchr (id_buf_pos, '*');
16818
16819 if (u_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16820
16821 u32 id_buf_len = u_len_pos - id_buf_pos;
16822
16823 if (id_buf_len != 32) return (PARSER_SALT_LENGTH);
16824
16825 u_len_pos++;
16826
16827 char *u_buf_pos = strchr (u_len_pos, '*');
16828
16829 if (u_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16830
16831 u32 u_len_len = u_buf_pos - u_len_pos;
16832
16833 u_buf_pos++;
16834
16835 char *o_len_pos = strchr (u_buf_pos, '*');
16836
16837 if (o_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16838
16839 u32 u_buf_len = o_len_pos - u_buf_pos;
16840
16841 if (u_buf_len != 64) return (PARSER_SALT_LENGTH);
16842
16843 o_len_pos++;
16844
16845 char *o_buf_pos = strchr (o_len_pos, '*');
16846
16847 if (o_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16848
16849 u32 o_len_len = o_buf_pos - o_len_pos;
16850
16851 o_buf_pos++;
16852
16853 char *rc4key_pos = strchr (o_buf_pos, ':');
16854
16855 if (rc4key_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16856
16857 u32 o_buf_len = rc4key_pos - o_buf_pos;
16858
16859 if (o_buf_len != 64) return (PARSER_SALT_LENGTH);
16860
16861 rc4key_pos++;
16862
16863 u32 rc4key_len = input_len - 5 - V_len - 1 - R_len - 1 - bits_len - 1 - P_len - 1 - enc_md_len - 1 - id_len_len - 1 - id_buf_len - 1 - u_len_len - 1 - u_buf_len - 1 - o_len_len - 1 - o_buf_len - 1;
16864
16865 if (rc4key_len != 10) return (PARSER_SALT_LENGTH);
16866
16867 // validate data
16868
16869 const int V = atoi (V_pos);
16870 const int R = atoi (R_pos);
16871 const int P = atoi (P_pos);
16872
16873 if (V != 1) return (PARSER_SALT_VALUE);
16874 if (R != 2) return (PARSER_SALT_VALUE);
16875
16876 const int enc_md = atoi (enc_md_pos);
16877
16878 if ((enc_md != 0) && (enc_md != 1)) return (PARSER_SALT_VALUE);
16879
16880 const int id_len = atoi (id_len_pos);
16881 const int u_len = atoi (u_len_pos);
16882 const int o_len = atoi (o_len_pos);
16883
16884 if (id_len != 16) return (PARSER_SALT_VALUE);
16885 if (u_len != 32) return (PARSER_SALT_VALUE);
16886 if (o_len != 32) return (PARSER_SALT_VALUE);
16887
16888 const int bits = atoi (bits_pos);
16889
16890 if (bits != 40) return (PARSER_SALT_VALUE);
16891
16892 // copy data to esalt
16893
16894 pdf->V = V;
16895 pdf->R = R;
16896 pdf->P = P;
16897
16898 pdf->enc_md = enc_md;
16899
16900 pdf->id_buf[0] = hex_to_u32 ((const u8 *) &id_buf_pos[ 0]);
16901 pdf->id_buf[1] = hex_to_u32 ((const u8 *) &id_buf_pos[ 8]);
16902 pdf->id_buf[2] = hex_to_u32 ((const u8 *) &id_buf_pos[16]);
16903 pdf->id_buf[3] = hex_to_u32 ((const u8 *) &id_buf_pos[24]);
16904 pdf->id_len = id_len;
16905
16906 pdf->u_buf[0] = hex_to_u32 ((const u8 *) &u_buf_pos[ 0]);
16907 pdf->u_buf[1] = hex_to_u32 ((const u8 *) &u_buf_pos[ 8]);
16908 pdf->u_buf[2] = hex_to_u32 ((const u8 *) &u_buf_pos[16]);
16909 pdf->u_buf[3] = hex_to_u32 ((const u8 *) &u_buf_pos[24]);
16910 pdf->u_buf[4] = hex_to_u32 ((const u8 *) &u_buf_pos[32]);
16911 pdf->u_buf[5] = hex_to_u32 ((const u8 *) &u_buf_pos[40]);
16912 pdf->u_buf[6] = hex_to_u32 ((const u8 *) &u_buf_pos[48]);
16913 pdf->u_buf[7] = hex_to_u32 ((const u8 *) &u_buf_pos[56]);
16914 pdf->u_len = u_len;
16915
16916 pdf->o_buf[0] = hex_to_u32 ((const u8 *) &o_buf_pos[ 0]);
16917 pdf->o_buf[1] = hex_to_u32 ((const u8 *) &o_buf_pos[ 8]);
16918 pdf->o_buf[2] = hex_to_u32 ((const u8 *) &o_buf_pos[16]);
16919 pdf->o_buf[3] = hex_to_u32 ((const u8 *) &o_buf_pos[24]);
16920 pdf->o_buf[4] = hex_to_u32 ((const u8 *) &o_buf_pos[32]);
16921 pdf->o_buf[5] = hex_to_u32 ((const u8 *) &o_buf_pos[40]);
16922 pdf->o_buf[6] = hex_to_u32 ((const u8 *) &o_buf_pos[48]);
16923 pdf->o_buf[7] = hex_to_u32 ((const u8 *) &o_buf_pos[56]);
16924 pdf->o_len = o_len;
16925
16926 pdf->id_buf[0] = byte_swap_32 (pdf->id_buf[0]);
16927 pdf->id_buf[1] = byte_swap_32 (pdf->id_buf[1]);
16928 pdf->id_buf[2] = byte_swap_32 (pdf->id_buf[2]);
16929 pdf->id_buf[3] = byte_swap_32 (pdf->id_buf[3]);
16930
16931 pdf->u_buf[0] = byte_swap_32 (pdf->u_buf[0]);
16932 pdf->u_buf[1] = byte_swap_32 (pdf->u_buf[1]);
16933 pdf->u_buf[2] = byte_swap_32 (pdf->u_buf[2]);
16934 pdf->u_buf[3] = byte_swap_32 (pdf->u_buf[3]);
16935 pdf->u_buf[4] = byte_swap_32 (pdf->u_buf[4]);
16936 pdf->u_buf[5] = byte_swap_32 (pdf->u_buf[5]);
16937 pdf->u_buf[6] = byte_swap_32 (pdf->u_buf[6]);
16938 pdf->u_buf[7] = byte_swap_32 (pdf->u_buf[7]);
16939
16940 pdf->o_buf[0] = byte_swap_32 (pdf->o_buf[0]);
16941 pdf->o_buf[1] = byte_swap_32 (pdf->o_buf[1]);
16942 pdf->o_buf[2] = byte_swap_32 (pdf->o_buf[2]);
16943 pdf->o_buf[3] = byte_swap_32 (pdf->o_buf[3]);
16944 pdf->o_buf[4] = byte_swap_32 (pdf->o_buf[4]);
16945 pdf->o_buf[5] = byte_swap_32 (pdf->o_buf[5]);
16946 pdf->o_buf[6] = byte_swap_32 (pdf->o_buf[6]);
16947 pdf->o_buf[7] = byte_swap_32 (pdf->o_buf[7]);
16948
16949 pdf->rc4key[1] = 0;
16950 pdf->rc4key[0] = 0;
16951
16952 pdf->rc4key[0] |= hex_convert (rc4key_pos[0]) << 28;
16953 pdf->rc4key[0] |= hex_convert (rc4key_pos[1]) << 24;
16954 pdf->rc4key[0] |= hex_convert (rc4key_pos[2]) << 20;
16955 pdf->rc4key[0] |= hex_convert (rc4key_pos[3]) << 16;
16956 pdf->rc4key[0] |= hex_convert (rc4key_pos[4]) << 12;
16957 pdf->rc4key[0] |= hex_convert (rc4key_pos[5]) << 8;
16958 pdf->rc4key[0] |= hex_convert (rc4key_pos[6]) << 4;
16959 pdf->rc4key[0] |= hex_convert (rc4key_pos[7]) << 0;
16960 pdf->rc4key[1] |= hex_convert (rc4key_pos[8]) << 28;
16961 pdf->rc4key[1] |= hex_convert (rc4key_pos[9]) << 24;
16962
16963 pdf->rc4key[0] = byte_swap_32 (pdf->rc4key[0]);
16964 pdf->rc4key[1] = byte_swap_32 (pdf->rc4key[1]);
16965
16966 // we use ID for salt, maybe needs to change, we will see...
16967
16968 salt->salt_buf[0] = pdf->id_buf[0];
16969 salt->salt_buf[1] = pdf->id_buf[1];
16970 salt->salt_buf[2] = pdf->id_buf[2];
16971 salt->salt_buf[3] = pdf->id_buf[3];
16972 salt->salt_buf[4] = pdf->u_buf[0];
16973 salt->salt_buf[5] = pdf->u_buf[1];
16974 salt->salt_buf[6] = pdf->o_buf[0];
16975 salt->salt_buf[7] = pdf->o_buf[1];
16976 salt->salt_len = pdf->id_len + 16;
16977
16978 digest[0] = pdf->rc4key[0];
16979 digest[1] = pdf->rc4key[1];
16980 digest[2] = 0;
16981 digest[3] = 0;
16982
16983 return (PARSER_OK);
16984 }
16985
16986 int pdf14_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16987 {
16988 if ((input_len < DISPLAY_LEN_MIN_10500) || (input_len > DISPLAY_LEN_MAX_10500)) return (PARSER_GLOBAL_LENGTH);
16989
16990 if ((memcmp (SIGNATURE_PDF, input_buf, 5)) && (memcmp (SIGNATURE_PDF, input_buf, 5))) return (PARSER_SIGNATURE_UNMATCHED);
16991
16992 u32 *digest = (u32 *) hash_buf->digest;
16993
16994 salt_t *salt = hash_buf->salt;
16995
16996 pdf_t *pdf = (pdf_t *) hash_buf->esalt;
16997
16998 /**
16999 * parse line
17000 */
17001
17002 char *V_pos = input_buf + 5;
17003
17004 char *R_pos = strchr (V_pos, '*');
17005
17006 if (R_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17007
17008 u32 V_len = R_pos - V_pos;
17009
17010 R_pos++;
17011
17012 char *bits_pos = strchr (R_pos, '*');
17013
17014 if (bits_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17015
17016 u32 R_len = bits_pos - R_pos;
17017
17018 bits_pos++;
17019
17020 char *P_pos = strchr (bits_pos, '*');
17021
17022 if (P_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17023
17024 u32 bits_len = P_pos - bits_pos;
17025
17026 P_pos++;
17027
17028 char *enc_md_pos = strchr (P_pos, '*');
17029
17030 if (enc_md_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17031
17032 u32 P_len = enc_md_pos - P_pos;
17033
17034 enc_md_pos++;
17035
17036 char *id_len_pos = strchr (enc_md_pos, '*');
17037
17038 if (id_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17039
17040 u32 enc_md_len = id_len_pos - enc_md_pos;
17041
17042 id_len_pos++;
17043
17044 char *id_buf_pos = strchr (id_len_pos, '*');
17045
17046 if (id_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17047
17048 u32 id_len_len = id_buf_pos - id_len_pos;
17049
17050 id_buf_pos++;
17051
17052 char *u_len_pos = strchr (id_buf_pos, '*');
17053
17054 if (u_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17055
17056 u32 id_buf_len = u_len_pos - id_buf_pos;
17057
17058 if ((id_buf_len != 32) && (id_buf_len != 64)) return (PARSER_SALT_LENGTH);
17059
17060 u_len_pos++;
17061
17062 char *u_buf_pos = strchr (u_len_pos, '*');
17063
17064 if (u_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17065
17066 u32 u_len_len = u_buf_pos - u_len_pos;
17067
17068 u_buf_pos++;
17069
17070 char *o_len_pos = strchr (u_buf_pos, '*');
17071
17072 if (o_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17073
17074 u32 u_buf_len = o_len_pos - u_buf_pos;
17075
17076 if (u_buf_len != 64) return (PARSER_SALT_LENGTH);
17077
17078 o_len_pos++;
17079
17080 char *o_buf_pos = strchr (o_len_pos, '*');
17081
17082 if (o_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17083
17084 u32 o_len_len = o_buf_pos - o_len_pos;
17085
17086 o_buf_pos++;
17087
17088 u32 o_buf_len = input_len - 5 - V_len - 1 - R_len - 1 - bits_len - 1 - P_len - 1 - enc_md_len - 1 - id_len_len - 1 - id_buf_len - 1 - u_len_len - 1 - u_buf_len - 1 - o_len_len - 1;
17089
17090 if (o_buf_len != 64) return (PARSER_SALT_LENGTH);
17091
17092 // validate data
17093
17094 const int V = atoi (V_pos);
17095 const int R = atoi (R_pos);
17096 const int P = atoi (P_pos);
17097
17098 int vr_ok = 0;
17099
17100 if ((V == 2) && (R == 3)) vr_ok = 1;
17101 if ((V == 4) && (R == 4)) vr_ok = 1;
17102
17103 if (vr_ok == 0) return (PARSER_SALT_VALUE);
17104
17105 const int id_len = atoi (id_len_pos);
17106 const int u_len = atoi (u_len_pos);
17107 const int o_len = atoi (o_len_pos);
17108
17109 if ((id_len != 16) && (id_len != 32)) return (PARSER_SALT_VALUE);
17110
17111 if (u_len != 32) return (PARSER_SALT_VALUE);
17112 if (o_len != 32) return (PARSER_SALT_VALUE);
17113
17114 const int bits = atoi (bits_pos);
17115
17116 if (bits != 128) return (PARSER_SALT_VALUE);
17117
17118 int enc_md = 1;
17119
17120 if (R >= 4)
17121 {
17122 enc_md = atoi (enc_md_pos);
17123 }
17124
17125 // copy data to esalt
17126
17127 pdf->V = V;
17128 pdf->R = R;
17129 pdf->P = P;
17130
17131 pdf->enc_md = enc_md;
17132
17133 pdf->id_buf[0] = hex_to_u32 ((const u8 *) &id_buf_pos[ 0]);
17134 pdf->id_buf[1] = hex_to_u32 ((const u8 *) &id_buf_pos[ 8]);
17135 pdf->id_buf[2] = hex_to_u32 ((const u8 *) &id_buf_pos[16]);
17136 pdf->id_buf[3] = hex_to_u32 ((const u8 *) &id_buf_pos[24]);
17137
17138 if (id_len == 32)
17139 {
17140 pdf->id_buf[4] = hex_to_u32 ((const u8 *) &id_buf_pos[32]);
17141 pdf->id_buf[5] = hex_to_u32 ((const u8 *) &id_buf_pos[40]);
17142 pdf->id_buf[6] = hex_to_u32 ((const u8 *) &id_buf_pos[48]);
17143 pdf->id_buf[7] = hex_to_u32 ((const u8 *) &id_buf_pos[56]);
17144 }
17145
17146 pdf->id_len = id_len;
17147
17148 pdf->u_buf[0] = hex_to_u32 ((const u8 *) &u_buf_pos[ 0]);
17149 pdf->u_buf[1] = hex_to_u32 ((const u8 *) &u_buf_pos[ 8]);
17150 pdf->u_buf[2] = hex_to_u32 ((const u8 *) &u_buf_pos[16]);
17151 pdf->u_buf[3] = hex_to_u32 ((const u8 *) &u_buf_pos[24]);
17152 pdf->u_buf[4] = hex_to_u32 ((const u8 *) &u_buf_pos[32]);
17153 pdf->u_buf[5] = hex_to_u32 ((const u8 *) &u_buf_pos[40]);
17154 pdf->u_buf[6] = hex_to_u32 ((const u8 *) &u_buf_pos[48]);
17155 pdf->u_buf[7] = hex_to_u32 ((const u8 *) &u_buf_pos[56]);
17156 pdf->u_len = u_len;
17157
17158 pdf->o_buf[0] = hex_to_u32 ((const u8 *) &o_buf_pos[ 0]);
17159 pdf->o_buf[1] = hex_to_u32 ((const u8 *) &o_buf_pos[ 8]);
17160 pdf->o_buf[2] = hex_to_u32 ((const u8 *) &o_buf_pos[16]);
17161 pdf->o_buf[3] = hex_to_u32 ((const u8 *) &o_buf_pos[24]);
17162 pdf->o_buf[4] = hex_to_u32 ((const u8 *) &o_buf_pos[32]);
17163 pdf->o_buf[5] = hex_to_u32 ((const u8 *) &o_buf_pos[40]);
17164 pdf->o_buf[6] = hex_to_u32 ((const u8 *) &o_buf_pos[48]);
17165 pdf->o_buf[7] = hex_to_u32 ((const u8 *) &o_buf_pos[56]);
17166 pdf->o_len = o_len;
17167
17168 pdf->id_buf[0] = byte_swap_32 (pdf->id_buf[0]);
17169 pdf->id_buf[1] = byte_swap_32 (pdf->id_buf[1]);
17170 pdf->id_buf[2] = byte_swap_32 (pdf->id_buf[2]);
17171 pdf->id_buf[3] = byte_swap_32 (pdf->id_buf[3]);
17172
17173 if (id_len == 32)
17174 {
17175 pdf->id_buf[4] = byte_swap_32 (pdf->id_buf[4]);
17176 pdf->id_buf[5] = byte_swap_32 (pdf->id_buf[5]);
17177 pdf->id_buf[6] = byte_swap_32 (pdf->id_buf[6]);
17178 pdf->id_buf[7] = byte_swap_32 (pdf->id_buf[7]);
17179 }
17180
17181 pdf->u_buf[0] = byte_swap_32 (pdf->u_buf[0]);
17182 pdf->u_buf[1] = byte_swap_32 (pdf->u_buf[1]);
17183 pdf->u_buf[2] = byte_swap_32 (pdf->u_buf[2]);
17184 pdf->u_buf[3] = byte_swap_32 (pdf->u_buf[3]);
17185 pdf->u_buf[4] = byte_swap_32 (pdf->u_buf[4]);
17186 pdf->u_buf[5] = byte_swap_32 (pdf->u_buf[5]);
17187 pdf->u_buf[6] = byte_swap_32 (pdf->u_buf[6]);
17188 pdf->u_buf[7] = byte_swap_32 (pdf->u_buf[7]);
17189
17190 pdf->o_buf[0] = byte_swap_32 (pdf->o_buf[0]);
17191 pdf->o_buf[1] = byte_swap_32 (pdf->o_buf[1]);
17192 pdf->o_buf[2] = byte_swap_32 (pdf->o_buf[2]);
17193 pdf->o_buf[3] = byte_swap_32 (pdf->o_buf[3]);
17194 pdf->o_buf[4] = byte_swap_32 (pdf->o_buf[4]);
17195 pdf->o_buf[5] = byte_swap_32 (pdf->o_buf[5]);
17196 pdf->o_buf[6] = byte_swap_32 (pdf->o_buf[6]);
17197 pdf->o_buf[7] = byte_swap_32 (pdf->o_buf[7]);
17198
17199 // precompute rc4 data for later use
17200
17201 uint padding[8] =
17202 {
17203 0x5e4ebf28,
17204 0x418a754e,
17205 0x564e0064,
17206 0x0801faff,
17207 0xb6002e2e,
17208 0x803e68d0,
17209 0xfea90c2f,
17210 0x7a695364
17211 };
17212
17213 // md5
17214
17215 uint salt_pc_block[32] = { 0 };
17216
17217 char *salt_pc_ptr = (char *) salt_pc_block;
17218
17219 memcpy (salt_pc_ptr, padding, 32);
17220 memcpy (salt_pc_ptr + 32, pdf->id_buf, pdf->id_len);
17221
17222 uint salt_pc_digest[4] = { 0 };
17223
17224 md5_complete_no_limit (salt_pc_digest, salt_pc_block, 32 + pdf->id_len);
17225
17226 pdf->rc4data[0] = salt_pc_digest[0];
17227 pdf->rc4data[1] = salt_pc_digest[1];
17228
17229 // we use ID for salt, maybe needs to change, we will see...
17230
17231 salt->salt_buf[0] = pdf->id_buf[0];
17232 salt->salt_buf[1] = pdf->id_buf[1];
17233 salt->salt_buf[2] = pdf->id_buf[2];
17234 salt->salt_buf[3] = pdf->id_buf[3];
17235 salt->salt_buf[4] = pdf->u_buf[0];
17236 salt->salt_buf[5] = pdf->u_buf[1];
17237 salt->salt_buf[6] = pdf->o_buf[0];
17238 salt->salt_buf[7] = pdf->o_buf[1];
17239 salt->salt_len = pdf->id_len + 16;
17240
17241 salt->salt_iter = ROUNDS_PDF14;
17242
17243 digest[0] = pdf->u_buf[0];
17244 digest[1] = pdf->u_buf[1];
17245 digest[2] = 0;
17246 digest[3] = 0;
17247
17248 return (PARSER_OK);
17249 }
17250
17251 int pdf17l3_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
17252 {
17253 int ret = pdf17l8_parse_hash (input_buf, input_len, hash_buf);
17254
17255 if (ret != PARSER_OK)
17256 {
17257 return ret;
17258 }
17259
17260 u32 *digest = (u32 *) hash_buf->digest;
17261
17262 salt_t *salt = hash_buf->salt;
17263
17264 digest[0] -= SHA256M_A;
17265 digest[1] -= SHA256M_B;
17266 digest[2] -= SHA256M_C;
17267 digest[3] -= SHA256M_D;
17268 digest[4] -= SHA256M_E;
17269 digest[5] -= SHA256M_F;
17270 digest[6] -= SHA256M_G;
17271 digest[7] -= SHA256M_H;
17272
17273 salt->salt_buf[2] = 0x80;
17274
17275 return (PARSER_OK);
17276 }
17277
17278 int pdf17l8_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
17279 {
17280 if ((input_len < DISPLAY_LEN_MIN_10600) || (input_len > DISPLAY_LEN_MAX_10600)) return (PARSER_GLOBAL_LENGTH);
17281
17282 if ((memcmp (SIGNATURE_PDF, input_buf, 5)) && (memcmp (SIGNATURE_PDF, input_buf, 5))) return (PARSER_SIGNATURE_UNMATCHED);
17283
17284 u32 *digest = (u32 *) hash_buf->digest;
17285
17286 salt_t *salt = hash_buf->salt;
17287
17288 pdf_t *pdf = (pdf_t *) hash_buf->esalt;
17289
17290 /**
17291 * parse line
17292 */
17293
17294 char *V_pos = input_buf + 5;
17295
17296 char *R_pos = strchr (V_pos, '*');
17297
17298 if (R_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17299
17300 u32 V_len = R_pos - V_pos;
17301
17302 R_pos++;
17303
17304 char *bits_pos = strchr (R_pos, '*');
17305
17306 if (bits_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17307
17308 u32 R_len = bits_pos - R_pos;
17309
17310 bits_pos++;
17311
17312 char *P_pos = strchr (bits_pos, '*');
17313
17314 if (P_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17315
17316 u32 bits_len = P_pos - bits_pos;
17317
17318 P_pos++;
17319
17320 char *enc_md_pos = strchr (P_pos, '*');
17321
17322 if (enc_md_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17323
17324 u32 P_len = enc_md_pos - P_pos;
17325
17326 enc_md_pos++;
17327
17328 char *id_len_pos = strchr (enc_md_pos, '*');
17329
17330 if (id_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17331
17332 u32 enc_md_len = id_len_pos - enc_md_pos;
17333
17334 id_len_pos++;
17335
17336 char *id_buf_pos = strchr (id_len_pos, '*');
17337
17338 if (id_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17339
17340 u32 id_len_len = id_buf_pos - id_len_pos;
17341
17342 id_buf_pos++;
17343
17344 char *u_len_pos = strchr (id_buf_pos, '*');
17345
17346 if (u_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17347
17348 u32 id_buf_len = u_len_pos - id_buf_pos;
17349
17350 u_len_pos++;
17351
17352 char *u_buf_pos = strchr (u_len_pos, '*');
17353
17354 if (u_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17355
17356 u32 u_len_len = u_buf_pos - u_len_pos;
17357
17358 u_buf_pos++;
17359
17360 char *o_len_pos = strchr (u_buf_pos, '*');
17361
17362 if (o_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17363
17364 u32 u_buf_len = o_len_pos - u_buf_pos;
17365
17366 o_len_pos++;
17367
17368 char *o_buf_pos = strchr (o_len_pos, '*');
17369
17370 if (o_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17371
17372 u32 o_len_len = o_buf_pos - o_len_pos;
17373
17374 o_buf_pos++;
17375
17376 char *last = strchr (o_buf_pos, '*');
17377
17378 if (last == NULL) last = input_buf + input_len;
17379
17380 u32 o_buf_len = last - o_buf_pos;
17381
17382 // validate data
17383
17384 const int V = atoi (V_pos);
17385 const int R = atoi (R_pos);
17386
17387 int vr_ok = 0;
17388
17389 if ((V == 5) && (R == 5)) vr_ok = 1;
17390 if ((V == 5) && (R == 6)) vr_ok = 1;
17391
17392 if (vr_ok == 0) return (PARSER_SALT_VALUE);
17393
17394 const int bits = atoi (bits_pos);
17395
17396 if (bits != 256) return (PARSER_SALT_VALUE);
17397
17398 int enc_md = atoi (enc_md_pos);
17399
17400 if (enc_md != 1) return (PARSER_SALT_VALUE);
17401
17402 const uint id_len = atoi (id_len_pos);
17403 const uint u_len = atoi (u_len_pos);
17404 const uint o_len = atoi (o_len_pos);
17405
17406 if (V_len > 6) return (PARSER_SALT_LENGTH);
17407 if (R_len > 6) return (PARSER_SALT_LENGTH);
17408 if (P_len > 6) return (PARSER_SALT_LENGTH);
17409 if (id_len_len > 6) return (PARSER_SALT_LENGTH);
17410 if (u_len_len > 6) return (PARSER_SALT_LENGTH);
17411 if (o_len_len > 6) return (PARSER_SALT_LENGTH);
17412 if (bits_len > 6) return (PARSER_SALT_LENGTH);
17413 if (enc_md_len > 6) return (PARSER_SALT_LENGTH);
17414
17415 if ((id_len * 2) != id_buf_len) return (PARSER_SALT_VALUE);
17416 if ((u_len * 2) != u_buf_len) return (PARSER_SALT_VALUE);
17417 if ((o_len * 2) != o_buf_len) return (PARSER_SALT_VALUE);
17418
17419 // copy data to esalt
17420
17421 if (u_len < 40) return (PARSER_SALT_VALUE);
17422
17423 for (int i = 0, j = 0; i < 8 + 2; i += 1, j += 8)
17424 {
17425 pdf->u_buf[i] = hex_to_u32 ((const u8 *) &u_buf_pos[j]);
17426 }
17427
17428 salt->salt_buf[0] = pdf->u_buf[8];
17429 salt->salt_buf[1] = pdf->u_buf[9];
17430
17431 salt->salt_buf[0] = byte_swap_32 (salt->salt_buf[0]);
17432 salt->salt_buf[1] = byte_swap_32 (salt->salt_buf[1]);
17433
17434 salt->salt_len = 8;
17435 salt->salt_iter = ROUNDS_PDF17L8;
17436
17437 digest[0] = pdf->u_buf[0];
17438 digest[1] = pdf->u_buf[1];
17439 digest[2] = pdf->u_buf[2];
17440 digest[3] = pdf->u_buf[3];
17441 digest[4] = pdf->u_buf[4];
17442 digest[5] = pdf->u_buf[5];
17443 digest[6] = pdf->u_buf[6];
17444 digest[7] = pdf->u_buf[7];
17445
17446 return (PARSER_OK);
17447 }
17448
17449 int pbkdf2_sha256_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
17450 {
17451 if ((input_len < DISPLAY_LEN_MIN_10900) || (input_len > DISPLAY_LEN_MAX_10900)) return (PARSER_GLOBAL_LENGTH);
17452
17453 if (memcmp (SIGNATURE_PBKDF2_SHA256, input_buf, 7)) return (PARSER_SIGNATURE_UNMATCHED);
17454
17455 u32 *digest = (u32 *) hash_buf->digest;
17456
17457 salt_t *salt = hash_buf->salt;
17458
17459 pbkdf2_sha256_t *pbkdf2_sha256 = (pbkdf2_sha256_t *) hash_buf->esalt;
17460
17461 /**
17462 * parse line
17463 */
17464
17465 // iterations
17466
17467 char *iter_pos = input_buf + 7;
17468
17469 u32 iter = atoi (iter_pos);
17470
17471 if (iter < 1) return (PARSER_SALT_ITERATION);
17472 if (iter > 999999) return (PARSER_SALT_ITERATION);
17473
17474 // first is *raw* salt
17475
17476 char *salt_pos = strchr (iter_pos, ':');
17477
17478 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17479
17480 salt_pos++;
17481
17482 char *hash_pos = strchr (salt_pos, ':');
17483
17484 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17485
17486 u32 salt_len = hash_pos - salt_pos;
17487
17488 if (salt_len > 64) return (PARSER_SALT_LENGTH);
17489
17490 hash_pos++;
17491
17492 u32 hash_b64_len = input_len - (hash_pos - input_buf);
17493
17494 if (hash_b64_len > 88) return (PARSER_HASH_LENGTH);
17495
17496 // decode salt
17497
17498 char *salt_buf_ptr = (char *) pbkdf2_sha256->salt_buf;
17499
17500 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
17501
17502 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
17503
17504 salt_buf_ptr[salt_len + 3] = 0x01;
17505 salt_buf_ptr[salt_len + 4] = 0x80;
17506
17507 salt->salt_len = salt_len;
17508 salt->salt_iter = iter - 1;
17509
17510 // decode hash
17511
17512 u8 tmp_buf[100] = { 0 };
17513
17514 int hash_len = base64_decode (base64_to_int, (const u8 *) hash_pos, hash_b64_len, tmp_buf);
17515
17516 if (hash_len < 16) return (PARSER_HASH_LENGTH);
17517
17518 memcpy (digest, tmp_buf, 16);
17519
17520 digest[0] = byte_swap_32 (digest[0]);
17521 digest[1] = byte_swap_32 (digest[1]);
17522 digest[2] = byte_swap_32 (digest[2]);
17523 digest[3] = byte_swap_32 (digest[3]);
17524
17525 // add some stuff to normal salt to make sorted happy
17526
17527 salt->salt_buf[0] = pbkdf2_sha256->salt_buf[0];
17528 salt->salt_buf[1] = pbkdf2_sha256->salt_buf[1];
17529 salt->salt_buf[2] = pbkdf2_sha256->salt_buf[2];
17530 salt->salt_buf[3] = pbkdf2_sha256->salt_buf[3];
17531 salt->salt_buf[4] = salt->salt_iter;
17532
17533 return (PARSER_OK);
17534 }
17535
17536 int prestashop_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
17537 {
17538 if ((input_len < DISPLAY_LEN_MIN_11000) || (input_len > DISPLAY_LEN_MAX_11000)) return (PARSER_GLOBAL_LENGTH);
17539
17540 u32 *digest = (u32 *) hash_buf->digest;
17541
17542 salt_t *salt = hash_buf->salt;
17543
17544 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
17545 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
17546 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
17547 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
17548
17549 digest[0] = byte_swap_32 (digest[0]);
17550 digest[1] = byte_swap_32 (digest[1]);
17551 digest[2] = byte_swap_32 (digest[2]);
17552 digest[3] = byte_swap_32 (digest[3]);
17553
17554 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
17555
17556 uint salt_len = input_len - 32 - 1;
17557
17558 char *salt_buf = input_buf + 32 + 1;
17559
17560 char *salt_buf_ptr = (char *) salt->salt_buf;
17561
17562 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
17563
17564 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
17565
17566 salt->salt_len = salt_len;
17567
17568 return (PARSER_OK);
17569 }
17570
17571 int postgresql_auth_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
17572 {
17573 if ((input_len < DISPLAY_LEN_MIN_11100) || (input_len > DISPLAY_LEN_MAX_11100)) return (PARSER_GLOBAL_LENGTH);
17574
17575 if (memcmp (SIGNATURE_POSTGRESQL_AUTH, input_buf, 10)) return (PARSER_SIGNATURE_UNMATCHED);
17576
17577 u32 *digest = (u32 *) hash_buf->digest;
17578
17579 salt_t *salt = hash_buf->salt;
17580
17581 char *user_pos = input_buf + 10;
17582
17583 char *salt_pos = strchr (user_pos, '*');
17584
17585 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17586
17587 salt_pos++;
17588
17589 char *hash_pos = strchr (salt_pos, '*');
17590
17591 hash_pos++;
17592
17593 uint hash_len = input_len - (hash_pos - input_buf);
17594
17595 if (hash_len != 32) return (PARSER_HASH_LENGTH);
17596
17597 uint user_len = salt_pos - user_pos - 1;
17598
17599 uint salt_len = hash_pos - salt_pos - 1;
17600
17601 if (salt_len != 8) return (PARSER_SALT_LENGTH);
17602
17603 /*
17604 * store digest
17605 */
17606
17607 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
17608 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
17609 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
17610 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
17611
17612 digest[0] = byte_swap_32 (digest[0]);
17613 digest[1] = byte_swap_32 (digest[1]);
17614 digest[2] = byte_swap_32 (digest[2]);
17615 digest[3] = byte_swap_32 (digest[3]);
17616
17617 digest[0] -= MD5M_A;
17618 digest[1] -= MD5M_B;
17619 digest[2] -= MD5M_C;
17620 digest[3] -= MD5M_D;
17621
17622 /*
17623 * store salt
17624 */
17625
17626 char *salt_buf_ptr = (char *) salt->salt_buf;
17627
17628 // first 4 bytes are the "challenge"
17629
17630 salt_buf_ptr[0] = hex_to_u8 ((const u8 *) &salt_pos[0]);
17631 salt_buf_ptr[1] = hex_to_u8 ((const u8 *) &salt_pos[2]);
17632 salt_buf_ptr[2] = hex_to_u8 ((const u8 *) &salt_pos[4]);
17633 salt_buf_ptr[3] = hex_to_u8 ((const u8 *) &salt_pos[6]);
17634
17635 // append the user name
17636
17637 user_len = parse_and_store_salt (salt_buf_ptr + 4, user_pos, user_len);
17638
17639 salt->salt_len = 4 + user_len;
17640
17641 return (PARSER_OK);
17642 }
17643
17644 int mysql_auth_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
17645 {
17646 if ((input_len < DISPLAY_LEN_MIN_11200) || (input_len > DISPLAY_LEN_MAX_11200)) return (PARSER_GLOBAL_LENGTH);
17647
17648 if (memcmp (SIGNATURE_MYSQL_AUTH, input_buf, 9)) return (PARSER_SIGNATURE_UNMATCHED);
17649
17650 u32 *digest = (u32 *) hash_buf->digest;
17651
17652 salt_t *salt = hash_buf->salt;
17653
17654 char *salt_pos = input_buf + 9;
17655
17656 char *hash_pos = strchr (salt_pos, '*');
17657
17658 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17659
17660 hash_pos++;
17661
17662 uint hash_len = input_len - (hash_pos - input_buf);
17663
17664 if (hash_len != 40) return (PARSER_HASH_LENGTH);
17665
17666 uint salt_len = hash_pos - salt_pos - 1;
17667
17668 if (salt_len != 40) return (PARSER_SALT_LENGTH);
17669
17670 /*
17671 * store digest
17672 */
17673
17674 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
17675 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
17676 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
17677 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
17678 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
17679
17680 /*
17681 * store salt
17682 */
17683
17684 char *salt_buf_ptr = (char *) salt->salt_buf;
17685
17686 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
17687
17688 salt->salt_len = salt_len;
17689
17690 return (PARSER_OK);
17691 }
17692
17693 int bitcoin_wallet_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
17694 {
17695 if ((input_len < DISPLAY_LEN_MIN_11300) || (input_len > DISPLAY_LEN_MAX_11300)) return (PARSER_GLOBAL_LENGTH);
17696
17697 if (memcmp (SIGNATURE_BITCOIN_WALLET, input_buf, 9)) return (PARSER_SIGNATURE_UNMATCHED);
17698
17699 u32 *digest = (u32 *) hash_buf->digest;
17700
17701 salt_t *salt = hash_buf->salt;
17702
17703 bitcoin_wallet_t *bitcoin_wallet = (bitcoin_wallet_t *) hash_buf->esalt;
17704
17705 /**
17706 * parse line
17707 */
17708
17709 char *cry_master_len_pos = input_buf + 9;
17710
17711 char *cry_master_buf_pos = strchr (cry_master_len_pos, '$');
17712
17713 if (cry_master_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17714
17715 u32 cry_master_len_len = cry_master_buf_pos - cry_master_len_pos;
17716
17717 cry_master_buf_pos++;
17718
17719 char *cry_salt_len_pos = strchr (cry_master_buf_pos, '$');
17720
17721 if (cry_salt_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17722
17723 u32 cry_master_buf_len = cry_salt_len_pos - cry_master_buf_pos;
17724
17725 cry_salt_len_pos++;
17726
17727 char *cry_salt_buf_pos = strchr (cry_salt_len_pos, '$');
17728
17729 if (cry_salt_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17730
17731 u32 cry_salt_len_len = cry_salt_buf_pos - cry_salt_len_pos;
17732
17733 cry_salt_buf_pos++;
17734
17735 char *cry_rounds_pos = strchr (cry_salt_buf_pos, '$');
17736
17737 if (cry_rounds_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17738
17739 u32 cry_salt_buf_len = cry_rounds_pos - cry_salt_buf_pos;
17740
17741 cry_rounds_pos++;
17742
17743 char *ckey_len_pos = strchr (cry_rounds_pos, '$');
17744
17745 if (ckey_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17746
17747 u32 cry_rounds_len = ckey_len_pos - cry_rounds_pos;
17748
17749 ckey_len_pos++;
17750
17751 char *ckey_buf_pos = strchr (ckey_len_pos, '$');
17752
17753 if (ckey_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17754
17755 u32 ckey_len_len = ckey_buf_pos - ckey_len_pos;
17756
17757 ckey_buf_pos++;
17758
17759 char *public_key_len_pos = strchr (ckey_buf_pos, '$');
17760
17761 if (public_key_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17762
17763 u32 ckey_buf_len = public_key_len_pos - ckey_buf_pos;
17764
17765 public_key_len_pos++;
17766
17767 char *public_key_buf_pos = strchr (public_key_len_pos, '$');
17768
17769 if (public_key_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17770
17771 u32 public_key_len_len = public_key_buf_pos - public_key_len_pos;
17772
17773 public_key_buf_pos++;
17774
17775 u32 public_key_buf_len = input_len - 1 - 7 - 1 - cry_master_len_len - 1 - cry_master_buf_len - 1 - cry_salt_len_len - 1 - cry_salt_buf_len - 1 - cry_rounds_len - 1 - ckey_len_len - 1 - ckey_buf_len - 1 - public_key_len_len - 1;
17776
17777 const uint cry_master_len = atoi (cry_master_len_pos);
17778 const uint cry_salt_len = atoi (cry_salt_len_pos);
17779 const uint ckey_len = atoi (ckey_len_pos);
17780 const uint public_key_len = atoi (public_key_len_pos);
17781
17782 if (cry_master_buf_len != cry_master_len) return (PARSER_SALT_VALUE);
17783 if (cry_salt_buf_len != cry_salt_len) return (PARSER_SALT_VALUE);
17784 if (ckey_buf_len != ckey_len) return (PARSER_SALT_VALUE);
17785 if (public_key_buf_len != public_key_len) return (PARSER_SALT_VALUE);
17786
17787 for (uint i = 0, j = 0; j < cry_master_len; i += 1, j += 8)
17788 {
17789 bitcoin_wallet->cry_master_buf[i] = hex_to_u32 ((const u8 *) &cry_master_buf_pos[j]);
17790
17791 bitcoin_wallet->cry_master_buf[i] = byte_swap_32 (bitcoin_wallet->cry_master_buf[i]);
17792 }
17793
17794 for (uint i = 0, j = 0; j < ckey_len; i += 1, j += 8)
17795 {
17796 bitcoin_wallet->ckey_buf[i] = hex_to_u32 ((const u8 *) &ckey_buf_pos[j]);
17797
17798 bitcoin_wallet->ckey_buf[i] = byte_swap_32 (bitcoin_wallet->ckey_buf[i]);
17799 }
17800
17801 for (uint i = 0, j = 0; j < public_key_len; i += 1, j += 8)
17802 {
17803 bitcoin_wallet->public_key_buf[i] = hex_to_u32 ((const u8 *) &public_key_buf_pos[j]);
17804
17805 bitcoin_wallet->public_key_buf[i] = byte_swap_32 (bitcoin_wallet->public_key_buf[i]);
17806 }
17807
17808 bitcoin_wallet->cry_master_len = cry_master_len / 2;
17809 bitcoin_wallet->ckey_len = ckey_len / 2;
17810 bitcoin_wallet->public_key_len = public_key_len / 2;
17811
17812 /*
17813 * store digest (should be unique enought, hopefully)
17814 */
17815
17816 digest[0] = bitcoin_wallet->cry_master_buf[0];
17817 digest[1] = bitcoin_wallet->cry_master_buf[1];
17818 digest[2] = bitcoin_wallet->cry_master_buf[2];
17819 digest[3] = bitcoin_wallet->cry_master_buf[3];
17820
17821 /*
17822 * store salt
17823 */
17824
17825 if (cry_rounds_len >= 7) return (PARSER_SALT_VALUE);
17826
17827 const uint cry_rounds = atoi (cry_rounds_pos);
17828
17829 salt->salt_iter = cry_rounds - 1;
17830
17831 char *salt_buf_ptr = (char *) salt->salt_buf;
17832
17833 const uint salt_len = parse_and_store_salt (salt_buf_ptr, cry_salt_buf_pos, cry_salt_buf_len);
17834
17835 salt->salt_len = salt_len;
17836
17837 return (PARSER_OK);
17838 }
17839
17840 int sip_auth_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
17841 {
17842 if ((input_len < DISPLAY_LEN_MIN_11400) || (input_len > DISPLAY_LEN_MAX_11400)) return (PARSER_GLOBAL_LENGTH);
17843
17844 if (memcmp (SIGNATURE_SIP_AUTH, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
17845
17846 u32 *digest = (u32 *) hash_buf->digest;
17847
17848 salt_t *salt = hash_buf->salt;
17849
17850 sip_t *sip = (sip_t *) hash_buf->esalt;
17851
17852 // work with a temporary copy of input_buf (s.t. we can manipulate it directly)
17853
17854 char *temp_input_buf = (char *) mymalloc (input_len + 1);
17855
17856 memcpy (temp_input_buf, input_buf, input_len);
17857
17858 // URI_server:
17859
17860 char *URI_server_pos = temp_input_buf + 6;
17861
17862 char *URI_client_pos = strchr (URI_server_pos, '*');
17863
17864 if (URI_client_pos == NULL)
17865 {
17866 myfree (temp_input_buf);
17867
17868 return (PARSER_SEPARATOR_UNMATCHED);
17869 }
17870
17871 URI_client_pos[0] = 0;
17872 URI_client_pos++;
17873
17874 uint URI_server_len = strlen (URI_server_pos);
17875
17876 if (URI_server_len > 512)
17877 {
17878 myfree (temp_input_buf);
17879
17880 return (PARSER_SALT_LENGTH);
17881 }
17882
17883 // URI_client:
17884
17885 char *user_pos = strchr (URI_client_pos, '*');
17886
17887 if (user_pos == NULL)
17888 {
17889 myfree (temp_input_buf);
17890
17891 return (PARSER_SEPARATOR_UNMATCHED);
17892 }
17893
17894 user_pos[0] = 0;
17895 user_pos++;
17896
17897 uint URI_client_len = strlen (URI_client_pos);
17898
17899 if (URI_client_len > 512)
17900 {
17901 myfree (temp_input_buf);
17902
17903 return (PARSER_SALT_LENGTH);
17904 }
17905
17906 // user:
17907
17908 char *realm_pos = strchr (user_pos, '*');
17909
17910 if (realm_pos == NULL)
17911 {
17912 myfree (temp_input_buf);
17913
17914 return (PARSER_SEPARATOR_UNMATCHED);
17915 }
17916
17917 realm_pos[0] = 0;
17918 realm_pos++;
17919
17920 uint user_len = strlen (user_pos);
17921
17922 if (user_len > 116)
17923 {
17924 myfree (temp_input_buf);
17925
17926 return (PARSER_SALT_LENGTH);
17927 }
17928
17929 // realm:
17930
17931 char *method_pos = strchr (realm_pos, '*');
17932
17933 if (method_pos == NULL)
17934 {
17935 myfree (temp_input_buf);
17936
17937 return (PARSER_SEPARATOR_UNMATCHED);
17938 }
17939
17940 method_pos[0] = 0;
17941 method_pos++;
17942
17943 uint realm_len = strlen (realm_pos);
17944
17945 if (realm_len > 116)
17946 {
17947 myfree (temp_input_buf);
17948
17949 return (PARSER_SALT_LENGTH);
17950 }
17951
17952 // method:
17953
17954 char *URI_prefix_pos = strchr (method_pos, '*');
17955
17956 if (URI_prefix_pos == NULL)
17957 {
17958 myfree (temp_input_buf);
17959
17960 return (PARSER_SEPARATOR_UNMATCHED);
17961 }
17962
17963 URI_prefix_pos[0] = 0;
17964 URI_prefix_pos++;
17965
17966 uint method_len = strlen (method_pos);
17967
17968 if (method_len > 246)
17969 {
17970 myfree (temp_input_buf);
17971
17972 return (PARSER_SALT_LENGTH);
17973 }
17974
17975 // URI_prefix:
17976
17977 char *URI_resource_pos = strchr (URI_prefix_pos, '*');
17978
17979 if (URI_resource_pos == NULL)
17980 {
17981 myfree (temp_input_buf);
17982
17983 return (PARSER_SEPARATOR_UNMATCHED);
17984 }
17985
17986 URI_resource_pos[0] = 0;
17987 URI_resource_pos++;
17988
17989 uint URI_prefix_len = strlen (URI_prefix_pos);
17990
17991 if (URI_prefix_len > 245)
17992 {
17993 myfree (temp_input_buf);
17994
17995 return (PARSER_SALT_LENGTH);
17996 }
17997
17998 // URI_resource:
17999
18000 char *URI_suffix_pos = strchr (URI_resource_pos, '*');
18001
18002 if (URI_suffix_pos == NULL)
18003 {
18004 myfree (temp_input_buf);
18005
18006 return (PARSER_SEPARATOR_UNMATCHED);
18007 }
18008
18009 URI_suffix_pos[0] = 0;
18010 URI_suffix_pos++;
18011
18012 uint URI_resource_len = strlen (URI_resource_pos);
18013
18014 if (URI_resource_len < 1 || URI_resource_len > 246)
18015 {
18016 myfree (temp_input_buf);
18017
18018 return (PARSER_SALT_LENGTH);
18019 }
18020
18021 // URI_suffix:
18022
18023 char *nonce_pos = strchr (URI_suffix_pos, '*');
18024
18025 if (nonce_pos == NULL)
18026 {
18027 myfree (temp_input_buf);
18028
18029 return (PARSER_SEPARATOR_UNMATCHED);
18030 }
18031
18032 nonce_pos[0] = 0;
18033 nonce_pos++;
18034
18035 uint URI_suffix_len = strlen (URI_suffix_pos);
18036
18037 if (URI_suffix_len > 245)
18038 {
18039 myfree (temp_input_buf);
18040
18041 return (PARSER_SALT_LENGTH);
18042 }
18043
18044 // nonce:
18045
18046 char *nonce_client_pos = strchr (nonce_pos, '*');
18047
18048 if (nonce_client_pos == NULL)
18049 {
18050 myfree (temp_input_buf);
18051
18052 return (PARSER_SEPARATOR_UNMATCHED);
18053 }
18054
18055 nonce_client_pos[0] = 0;
18056 nonce_client_pos++;
18057
18058 uint nonce_len = strlen (nonce_pos);
18059
18060 if (nonce_len < 1 || nonce_len > 50)
18061 {
18062 myfree (temp_input_buf);
18063
18064 return (PARSER_SALT_LENGTH);
18065 }
18066
18067 // nonce_client:
18068
18069 char *nonce_count_pos = strchr (nonce_client_pos, '*');
18070
18071 if (nonce_count_pos == NULL)
18072 {
18073 myfree (temp_input_buf);
18074
18075 return (PARSER_SEPARATOR_UNMATCHED);
18076 }
18077
18078 nonce_count_pos[0] = 0;
18079 nonce_count_pos++;
18080
18081 uint nonce_client_len = strlen (nonce_client_pos);
18082
18083 if (nonce_client_len > 50)
18084 {
18085 myfree (temp_input_buf);
18086
18087 return (PARSER_SALT_LENGTH);
18088 }
18089
18090 // nonce_count:
18091
18092 char *qop_pos = strchr (nonce_count_pos, '*');
18093
18094 if (qop_pos == NULL)
18095 {
18096 myfree (temp_input_buf);
18097
18098 return (PARSER_SEPARATOR_UNMATCHED);
18099 }
18100
18101 qop_pos[0] = 0;
18102 qop_pos++;
18103
18104 uint nonce_count_len = strlen (nonce_count_pos);
18105
18106 if (nonce_count_len > 50)
18107 {
18108 myfree (temp_input_buf);
18109
18110 return (PARSER_SALT_LENGTH);
18111 }
18112
18113 // qop:
18114
18115 char *directive_pos = strchr (qop_pos, '*');
18116
18117 if (directive_pos == NULL)
18118 {
18119 myfree (temp_input_buf);
18120
18121 return (PARSER_SEPARATOR_UNMATCHED);
18122 }
18123
18124 directive_pos[0] = 0;
18125 directive_pos++;
18126
18127 uint qop_len = strlen (qop_pos);
18128
18129 if (qop_len > 50)
18130 {
18131 myfree (temp_input_buf);
18132
18133 return (PARSER_SALT_LENGTH);
18134 }
18135
18136 // directive
18137
18138 char *digest_pos = strchr (directive_pos, '*');
18139
18140 if (digest_pos == NULL)
18141 {
18142 myfree (temp_input_buf);
18143
18144 return (PARSER_SEPARATOR_UNMATCHED);
18145 }
18146
18147 digest_pos[0] = 0;
18148 digest_pos++;
18149
18150 uint directive_len = strlen (directive_pos);
18151
18152 if (directive_len != 3)
18153 {
18154 myfree (temp_input_buf);
18155
18156 return (PARSER_SALT_LENGTH);
18157 }
18158
18159 if (memcmp (directive_pos, "MD5", 3))
18160 {
18161 log_info ("ERROR: only the MD5 directive is currently supported\n");
18162
18163 myfree (temp_input_buf);
18164
18165 return (PARSER_SIP_AUTH_DIRECTIVE);
18166 }
18167
18168 /*
18169 * first (pre-)compute: HA2 = md5 ($method . ":" . $uri)
18170 */
18171
18172 uint md5_len = 0;
18173
18174 uint md5_max_len = 4 * 64;
18175
18176 uint md5_remaining_len = md5_max_len;
18177
18178 uint tmp_md5_buf[64] = { 0 };
18179
18180 char *tmp_md5_ptr = (char *) tmp_md5_buf;
18181
18182 snprintf (tmp_md5_ptr, md5_remaining_len, "%s:", method_pos);
18183
18184 md5_len += method_len + 1;
18185 tmp_md5_ptr += method_len + 1;
18186
18187 if (URI_prefix_len > 0)
18188 {
18189 md5_remaining_len = md5_max_len - md5_len;
18190
18191 snprintf (tmp_md5_ptr, md5_remaining_len + 1, "%s:", URI_prefix_pos);
18192
18193 md5_len += URI_prefix_len + 1;
18194 tmp_md5_ptr += URI_prefix_len + 1;
18195 }
18196
18197 md5_remaining_len = md5_max_len - md5_len;
18198
18199 snprintf (tmp_md5_ptr, md5_remaining_len + 1, "%s", URI_resource_pos);
18200
18201 md5_len += URI_resource_len;
18202 tmp_md5_ptr += URI_resource_len;
18203
18204 if (URI_suffix_len > 0)
18205 {
18206 md5_remaining_len = md5_max_len - md5_len;
18207
18208 snprintf (tmp_md5_ptr, md5_remaining_len + 1, ":%s", URI_suffix_pos);
18209
18210 md5_len += 1 + URI_suffix_len;
18211 }
18212
18213 uint tmp_digest[4] = { 0 };
18214
18215 md5_complete_no_limit (tmp_digest, tmp_md5_buf, md5_len);
18216
18217 tmp_digest[0] = byte_swap_32 (tmp_digest[0]);
18218 tmp_digest[1] = byte_swap_32 (tmp_digest[1]);
18219 tmp_digest[2] = byte_swap_32 (tmp_digest[2]);
18220 tmp_digest[3] = byte_swap_32 (tmp_digest[3]);
18221
18222 /*
18223 * esalt
18224 */
18225
18226 char *esalt_buf_ptr = (char *) sip->esalt_buf;
18227
18228 uint esalt_len = 0;
18229
18230 uint max_esalt_len = sizeof (sip->esalt_buf); // 151 = (64 + 64 + 55) - 32, where 32 is the hexadecimal MD5 HA1 hash
18231
18232 // there are 2 possibilities for the esalt:
18233
18234 if ((strcmp (qop_pos, "auth") == 0) || (strcmp (qop_pos, "auth-int") == 0))
18235 {
18236 esalt_len = 1 + nonce_len + 1 + nonce_count_len + 1 + nonce_client_len + 1 + qop_len + 1 + 32;
18237
18238 if (esalt_len > max_esalt_len)
18239 {
18240 myfree (temp_input_buf);
18241
18242 return (PARSER_SALT_LENGTH);
18243 }
18244
18245 snprintf (esalt_buf_ptr, max_esalt_len, ":%s:%s:%s:%s:%08x%08x%08x%08x",
18246 nonce_pos,
18247 nonce_count_pos,
18248 nonce_client_pos,
18249 qop_pos,
18250 tmp_digest[0],
18251 tmp_digest[1],
18252 tmp_digest[2],
18253 tmp_digest[3]);
18254 }
18255 else
18256 {
18257 esalt_len = 1 + nonce_len + 1 + 32;
18258
18259 if (esalt_len > max_esalt_len)
18260 {
18261 myfree (temp_input_buf);
18262
18263 return (PARSER_SALT_LENGTH);
18264 }
18265
18266 snprintf (esalt_buf_ptr, max_esalt_len, ":%s:%08x%08x%08x%08x",
18267 nonce_pos,
18268 tmp_digest[0],
18269 tmp_digest[1],
18270 tmp_digest[2],
18271 tmp_digest[3]);
18272 }
18273
18274 // add 0x80 to esalt
18275
18276 esalt_buf_ptr[esalt_len] = 0x80;
18277
18278 sip->esalt_len = esalt_len;
18279
18280 /*
18281 * actual salt
18282 */
18283
18284 char *sip_salt_ptr = (char *) sip->salt_buf;
18285
18286 uint salt_len = user_len + 1 + realm_len + 1;
18287
18288 uint max_salt_len = 119;
18289
18290 if (salt_len > max_salt_len)
18291 {
18292 myfree (temp_input_buf);
18293
18294 return (PARSER_SALT_LENGTH);
18295 }
18296
18297 snprintf (sip_salt_ptr, max_salt_len + 1, "%s:%s:", user_pos, realm_pos);
18298
18299 sip->salt_len = salt_len;
18300
18301 /*
18302 * fake salt (for sorting)
18303 */
18304
18305 char *salt_buf_ptr = (char *) salt->salt_buf;
18306
18307 max_salt_len = 55;
18308
18309 uint fake_salt_len = salt_len;
18310
18311 if (fake_salt_len > max_salt_len)
18312 {
18313 fake_salt_len = max_salt_len;
18314 }
18315
18316 snprintf (salt_buf_ptr, max_salt_len + 1, "%s:%s:", user_pos, realm_pos);
18317
18318 salt->salt_len = fake_salt_len;
18319
18320 /*
18321 * digest
18322 */
18323
18324 digest[0] = hex_to_u32 ((const u8 *) &digest_pos[ 0]);
18325 digest[1] = hex_to_u32 ((const u8 *) &digest_pos[ 8]);
18326 digest[2] = hex_to_u32 ((const u8 *) &digest_pos[16]);
18327 digest[3] = hex_to_u32 ((const u8 *) &digest_pos[24]);
18328
18329 digest[0] = byte_swap_32 (digest[0]);
18330 digest[1] = byte_swap_32 (digest[1]);
18331 digest[2] = byte_swap_32 (digest[2]);
18332 digest[3] = byte_swap_32 (digest[3]);
18333
18334 myfree (temp_input_buf);
18335
18336 return (PARSER_OK);
18337 }
18338
18339 int crc32_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18340 {
18341 if ((input_len < DISPLAY_LEN_MIN_11500) || (input_len > DISPLAY_LEN_MAX_11500)) return (PARSER_GLOBAL_LENGTH);
18342
18343 if (input_buf[8] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
18344
18345 u32 *digest = (u32 *) hash_buf->digest;
18346
18347 salt_t *salt = hash_buf->salt;
18348
18349 // digest
18350
18351 char *digest_pos = input_buf;
18352
18353 digest[0] = hex_to_u32 ((const u8 *) &digest_pos[0]);
18354 digest[1] = 0;
18355 digest[2] = 0;
18356 digest[3] = 0;
18357
18358 // salt
18359
18360 char *salt_buf = input_buf + 8 + 1;
18361
18362 uint salt_len = 8;
18363
18364 char *salt_buf_ptr = (char *) salt->salt_buf;
18365
18366 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
18367
18368 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
18369
18370 salt->salt_len = salt_len;
18371
18372 return (PARSER_OK);
18373 }
18374
18375 int seven_zip_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18376 {
18377 if ((input_len < DISPLAY_LEN_MIN_11600) || (input_len > DISPLAY_LEN_MAX_11600)) return (PARSER_GLOBAL_LENGTH);
18378
18379 if (memcmp (SIGNATURE_SEVEN_ZIP, input_buf, 4)) return (PARSER_SIGNATURE_UNMATCHED);
18380
18381 u32 *digest = (u32 *) hash_buf->digest;
18382
18383 salt_t *salt = hash_buf->salt;
18384
18385 seven_zip_t *seven_zip = (seven_zip_t *) hash_buf->esalt;
18386
18387 /**
18388 * parse line
18389 */
18390
18391 char *p_buf_pos = input_buf + 4;
18392
18393 char *NumCyclesPower_pos = strchr (p_buf_pos, '$');
18394
18395 if (NumCyclesPower_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18396
18397 u32 p_buf_len = NumCyclesPower_pos - p_buf_pos;
18398
18399 NumCyclesPower_pos++;
18400
18401 char *salt_len_pos = strchr (NumCyclesPower_pos, '$');
18402
18403 if (salt_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18404
18405 u32 NumCyclesPower_len = salt_len_pos - NumCyclesPower_pos;
18406
18407 salt_len_pos++;
18408
18409 char *salt_buf_pos = strchr (salt_len_pos, '$');
18410
18411 if (salt_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18412
18413 u32 salt_len_len = salt_buf_pos - salt_len_pos;
18414
18415 salt_buf_pos++;
18416
18417 char *iv_len_pos = strchr (salt_buf_pos, '$');
18418
18419 if (iv_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18420
18421 u32 salt_buf_len = iv_len_pos - salt_buf_pos;
18422
18423 iv_len_pos++;
18424
18425 char *iv_buf_pos = strchr (iv_len_pos, '$');
18426
18427 if (iv_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18428
18429 u32 iv_len_len = iv_buf_pos - iv_len_pos;
18430
18431 iv_buf_pos++;
18432
18433 char *crc_buf_pos = strchr (iv_buf_pos, '$');
18434
18435 if (crc_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18436
18437 u32 iv_buf_len = crc_buf_pos - iv_buf_pos;
18438
18439 crc_buf_pos++;
18440
18441 char *data_len_pos = strchr (crc_buf_pos, '$');
18442
18443 if (data_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18444
18445 u32 crc_buf_len = data_len_pos - crc_buf_pos;
18446
18447 data_len_pos++;
18448
18449 char *unpack_size_pos = strchr (data_len_pos, '$');
18450
18451 if (unpack_size_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18452
18453 u32 data_len_len = unpack_size_pos - data_len_pos;
18454
18455 unpack_size_pos++;
18456
18457 char *data_buf_pos = strchr (unpack_size_pos, '$');
18458
18459 if (data_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18460
18461 u32 unpack_size_len = data_buf_pos - unpack_size_pos;
18462
18463 data_buf_pos++;
18464
18465 u32 data_buf_len = input_len - 1 - 2 - 1 - p_buf_len - 1 - NumCyclesPower_len - 1 - salt_len_len - 1 - salt_buf_len - 1 - iv_len_len - 1 - iv_buf_len - 1 - crc_buf_len - 1 - data_len_len - 1 - unpack_size_len - 1;
18466
18467 const uint iter = atoi (NumCyclesPower_pos);
18468 const uint crc = atoi (crc_buf_pos);
18469 const uint p_buf = atoi (p_buf_pos);
18470 const uint salt_len = atoi (salt_len_pos);
18471 const uint iv_len = atoi (iv_len_pos);
18472 const uint unpack_size = atoi (unpack_size_pos);
18473 const uint data_len = atoi (data_len_pos);
18474
18475 /**
18476 * verify some data
18477 */
18478
18479 if (p_buf != 0) return (PARSER_SALT_VALUE);
18480 if (salt_len != 0) return (PARSER_SALT_VALUE);
18481
18482 if ((data_len * 2) != data_buf_len) return (PARSER_SALT_VALUE);
18483
18484 if (data_len > 384) return (PARSER_SALT_VALUE);
18485
18486 if (unpack_size > data_len) return (PARSER_SALT_VALUE);
18487
18488 /**
18489 * store data
18490 */
18491
18492 seven_zip->iv_buf[0] = hex_to_u32 ((const u8 *) &iv_buf_pos[ 0]);
18493 seven_zip->iv_buf[1] = hex_to_u32 ((const u8 *) &iv_buf_pos[ 8]);
18494 seven_zip->iv_buf[2] = hex_to_u32 ((const u8 *) &iv_buf_pos[16]);
18495 seven_zip->iv_buf[3] = hex_to_u32 ((const u8 *) &iv_buf_pos[24]);
18496
18497 seven_zip->iv_len = iv_len;
18498
18499 memcpy (seven_zip->salt_buf, salt_buf_pos, salt_buf_len); // we just need that for later ascii_digest()
18500
18501 seven_zip->salt_len = 0;
18502
18503 seven_zip->crc = crc;
18504
18505 for (uint i = 0, j = 0; j < data_buf_len; i += 1, j += 8)
18506 {
18507 seven_zip->data_buf[i] = hex_to_u32 ((const u8 *) &data_buf_pos[j]);
18508
18509 seven_zip->data_buf[i] = byte_swap_32 (seven_zip->data_buf[i]);
18510 }
18511
18512 seven_zip->data_len = data_len;
18513
18514 seven_zip->unpack_size = unpack_size;
18515
18516 // real salt
18517
18518 salt->salt_buf[0] = seven_zip->data_buf[0];
18519 salt->salt_buf[1] = seven_zip->data_buf[1];
18520 salt->salt_buf[2] = seven_zip->data_buf[2];
18521 salt->salt_buf[3] = seven_zip->data_buf[3];
18522
18523 salt->salt_len = 16;
18524
18525 salt->salt_sign[0] = iter;
18526
18527 salt->salt_iter = 1 << iter;
18528
18529 /**
18530 * digest
18531 */
18532
18533 digest[0] = crc;
18534 digest[1] = 0;
18535 digest[2] = 0;
18536 digest[3] = 0;
18537
18538 return (PARSER_OK);
18539 }
18540
18541 int gost2012sbog_256_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18542 {
18543 if ((input_len < DISPLAY_LEN_MIN_11700) || (input_len > DISPLAY_LEN_MAX_11700)) return (PARSER_GLOBAL_LENGTH);
18544
18545 u32 *digest = (u32 *) hash_buf->digest;
18546
18547 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
18548 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
18549 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
18550 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
18551 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
18552 digest[5] = hex_to_u32 ((const u8 *) &input_buf[40]);
18553 digest[6] = hex_to_u32 ((const u8 *) &input_buf[48]);
18554 digest[7] = hex_to_u32 ((const u8 *) &input_buf[56]);
18555
18556 digest[0] = byte_swap_32 (digest[0]);
18557 digest[1] = byte_swap_32 (digest[1]);
18558 digest[2] = byte_swap_32 (digest[2]);
18559 digest[3] = byte_swap_32 (digest[3]);
18560 digest[4] = byte_swap_32 (digest[4]);
18561 digest[5] = byte_swap_32 (digest[5]);
18562 digest[6] = byte_swap_32 (digest[6]);
18563 digest[7] = byte_swap_32 (digest[7]);
18564
18565 return (PARSER_OK);
18566 }
18567
18568 int gost2012sbog_512_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18569 {
18570 if ((input_len < DISPLAY_LEN_MIN_11800) || (input_len > DISPLAY_LEN_MAX_11800)) return (PARSER_GLOBAL_LENGTH);
18571
18572 u32 *digest = (u32 *) hash_buf->digest;
18573
18574 digest[ 0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
18575 digest[ 1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
18576 digest[ 2] = hex_to_u32 ((const u8 *) &input_buf[ 16]);
18577 digest[ 3] = hex_to_u32 ((const u8 *) &input_buf[ 24]);
18578 digest[ 4] = hex_to_u32 ((const u8 *) &input_buf[ 32]);
18579 digest[ 5] = hex_to_u32 ((const u8 *) &input_buf[ 40]);
18580 digest[ 6] = hex_to_u32 ((const u8 *) &input_buf[ 48]);
18581 digest[ 7] = hex_to_u32 ((const u8 *) &input_buf[ 56]);
18582 digest[ 8] = hex_to_u32 ((const u8 *) &input_buf[ 64]);
18583 digest[ 9] = hex_to_u32 ((const u8 *) &input_buf[ 72]);
18584 digest[10] = hex_to_u32 ((const u8 *) &input_buf[ 80]);
18585 digest[11] = hex_to_u32 ((const u8 *) &input_buf[ 88]);
18586 digest[12] = hex_to_u32 ((const u8 *) &input_buf[ 96]);
18587 digest[13] = hex_to_u32 ((const u8 *) &input_buf[104]);
18588 digest[14] = hex_to_u32 ((const u8 *) &input_buf[112]);
18589 digest[15] = hex_to_u32 ((const u8 *) &input_buf[120]);
18590
18591 digest[ 0] = byte_swap_32 (digest[ 0]);
18592 digest[ 1] = byte_swap_32 (digest[ 1]);
18593 digest[ 2] = byte_swap_32 (digest[ 2]);
18594 digest[ 3] = byte_swap_32 (digest[ 3]);
18595 digest[ 4] = byte_swap_32 (digest[ 4]);
18596 digest[ 5] = byte_swap_32 (digest[ 5]);
18597 digest[ 6] = byte_swap_32 (digest[ 6]);
18598 digest[ 7] = byte_swap_32 (digest[ 7]);
18599 digest[ 8] = byte_swap_32 (digest[ 8]);
18600 digest[ 9] = byte_swap_32 (digest[ 9]);
18601 digest[10] = byte_swap_32 (digest[10]);
18602 digest[11] = byte_swap_32 (digest[11]);
18603 digest[12] = byte_swap_32 (digest[12]);
18604 digest[13] = byte_swap_32 (digest[13]);
18605 digest[14] = byte_swap_32 (digest[14]);
18606 digest[15] = byte_swap_32 (digest[15]);
18607
18608 return (PARSER_OK);
18609 }
18610
18611 int pbkdf2_md5_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18612 {
18613 if ((input_len < DISPLAY_LEN_MIN_11900) || (input_len > DISPLAY_LEN_MAX_11900)) return (PARSER_GLOBAL_LENGTH);
18614
18615 if (memcmp (SIGNATURE_PBKDF2_MD5, input_buf, 4)) return (PARSER_SIGNATURE_UNMATCHED);
18616
18617 u32 *digest = (u32 *) hash_buf->digest;
18618
18619 salt_t *salt = hash_buf->salt;
18620
18621 pbkdf2_md5_t *pbkdf2_md5 = (pbkdf2_md5_t *) hash_buf->esalt;
18622
18623 /**
18624 * parse line
18625 */
18626
18627 // iterations
18628
18629 char *iter_pos = input_buf + 4;
18630
18631 u32 iter = atoi (iter_pos);
18632
18633 if (iter < 1) return (PARSER_SALT_ITERATION);
18634 if (iter > 999999) return (PARSER_SALT_ITERATION);
18635
18636 // first is *raw* salt
18637
18638 char *salt_pos = strchr (iter_pos, ':');
18639
18640 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18641
18642 salt_pos++;
18643
18644 char *hash_pos = strchr (salt_pos, ':');
18645
18646 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18647
18648 u32 salt_len = hash_pos - salt_pos;
18649
18650 if (salt_len > 64) return (PARSER_SALT_LENGTH);
18651
18652 hash_pos++;
18653
18654 u32 hash_b64_len = input_len - (hash_pos - input_buf);
18655
18656 if (hash_b64_len > 88) return (PARSER_HASH_LENGTH);
18657
18658 // decode salt
18659
18660 char *salt_buf_ptr = (char *) pbkdf2_md5->salt_buf;
18661
18662 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
18663
18664 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
18665
18666 salt_buf_ptr[salt_len + 3] = 0x01;
18667 salt_buf_ptr[salt_len + 4] = 0x80;
18668
18669 salt->salt_len = salt_len;
18670 salt->salt_iter = iter - 1;
18671
18672 // decode hash
18673
18674 u8 tmp_buf[100] = { 0 };
18675
18676 int hash_len = base64_decode (base64_to_int, (const u8 *) hash_pos, hash_b64_len, tmp_buf);
18677
18678 if (hash_len < 16) return (PARSER_HASH_LENGTH);
18679
18680 memcpy (digest, tmp_buf, 16);
18681
18682 // add some stuff to normal salt to make sorted happy
18683
18684 salt->salt_buf[0] = pbkdf2_md5->salt_buf[0];
18685 salt->salt_buf[1] = pbkdf2_md5->salt_buf[1];
18686 salt->salt_buf[2] = pbkdf2_md5->salt_buf[2];
18687 salt->salt_buf[3] = pbkdf2_md5->salt_buf[3];
18688 salt->salt_buf[4] = salt->salt_iter;
18689
18690 return (PARSER_OK);
18691 }
18692
18693 int pbkdf2_sha1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18694 {
18695 if ((input_len < DISPLAY_LEN_MIN_12000) || (input_len > DISPLAY_LEN_MAX_12000)) return (PARSER_GLOBAL_LENGTH);
18696
18697 if (memcmp (SIGNATURE_PBKDF2_SHA1, input_buf, 5)) return (PARSER_SIGNATURE_UNMATCHED);
18698
18699 u32 *digest = (u32 *) hash_buf->digest;
18700
18701 salt_t *salt = hash_buf->salt;
18702
18703 pbkdf2_sha1_t *pbkdf2_sha1 = (pbkdf2_sha1_t *) hash_buf->esalt;
18704
18705 /**
18706 * parse line
18707 */
18708
18709 // iterations
18710
18711 char *iter_pos = input_buf + 5;
18712
18713 u32 iter = atoi (iter_pos);
18714
18715 if (iter < 1) return (PARSER_SALT_ITERATION);
18716 if (iter > 999999) return (PARSER_SALT_ITERATION);
18717
18718 // first is *raw* salt
18719
18720 char *salt_pos = strchr (iter_pos, ':');
18721
18722 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18723
18724 salt_pos++;
18725
18726 char *hash_pos = strchr (salt_pos, ':');
18727
18728 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18729
18730 u32 salt_len = hash_pos - salt_pos;
18731
18732 if (salt_len > 64) return (PARSER_SALT_LENGTH);
18733
18734 hash_pos++;
18735
18736 u32 hash_b64_len = input_len - (hash_pos - input_buf);
18737
18738 if (hash_b64_len > 88) return (PARSER_HASH_LENGTH);
18739
18740 // decode salt
18741
18742 char *salt_buf_ptr = (char *) pbkdf2_sha1->salt_buf;
18743
18744 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
18745
18746 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
18747
18748 salt_buf_ptr[salt_len + 3] = 0x01;
18749 salt_buf_ptr[salt_len + 4] = 0x80;
18750
18751 salt->salt_len = salt_len;
18752 salt->salt_iter = iter - 1;
18753
18754 // decode hash
18755
18756 u8 tmp_buf[100] = { 0 };
18757
18758 int hash_len = base64_decode (base64_to_int, (const u8 *) hash_pos, hash_b64_len, tmp_buf);
18759
18760 if (hash_len < 16) return (PARSER_HASH_LENGTH);
18761
18762 memcpy (digest, tmp_buf, 16);
18763
18764 digest[0] = byte_swap_32 (digest[0]);
18765 digest[1] = byte_swap_32 (digest[1]);
18766 digest[2] = byte_swap_32 (digest[2]);
18767 digest[3] = byte_swap_32 (digest[3]);
18768
18769 // add some stuff to normal salt to make sorted happy
18770
18771 salt->salt_buf[0] = pbkdf2_sha1->salt_buf[0];
18772 salt->salt_buf[1] = pbkdf2_sha1->salt_buf[1];
18773 salt->salt_buf[2] = pbkdf2_sha1->salt_buf[2];
18774 salt->salt_buf[3] = pbkdf2_sha1->salt_buf[3];
18775 salt->salt_buf[4] = salt->salt_iter;
18776
18777 return (PARSER_OK);
18778 }
18779
18780 int pbkdf2_sha512_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18781 {
18782 if ((input_len < DISPLAY_LEN_MIN_12100) || (input_len > DISPLAY_LEN_MAX_12100)) return (PARSER_GLOBAL_LENGTH);
18783
18784 if (memcmp (SIGNATURE_PBKDF2_SHA512, input_buf, 7)) return (PARSER_SIGNATURE_UNMATCHED);
18785
18786 u64 *digest = (u64 *) hash_buf->digest;
18787
18788 salt_t *salt = hash_buf->salt;
18789
18790 pbkdf2_sha512_t *pbkdf2_sha512 = (pbkdf2_sha512_t *) hash_buf->esalt;
18791
18792 /**
18793 * parse line
18794 */
18795
18796 // iterations
18797
18798 char *iter_pos = input_buf + 7;
18799
18800 u32 iter = atoi (iter_pos);
18801
18802 if (iter < 1) return (PARSER_SALT_ITERATION);
18803 if (iter > 999999) return (PARSER_SALT_ITERATION);
18804
18805 // first is *raw* salt
18806
18807 char *salt_pos = strchr (iter_pos, ':');
18808
18809 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18810
18811 salt_pos++;
18812
18813 char *hash_pos = strchr (salt_pos, ':');
18814
18815 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18816
18817 u32 salt_len = hash_pos - salt_pos;
18818
18819 if (salt_len > 64) return (PARSER_SALT_LENGTH);
18820
18821 hash_pos++;
18822
18823 u32 hash_b64_len = input_len - (hash_pos - input_buf);
18824
18825 if (hash_b64_len > 88) return (PARSER_HASH_LENGTH);
18826
18827 // decode salt
18828
18829 char *salt_buf_ptr = (char *) pbkdf2_sha512->salt_buf;
18830
18831 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
18832
18833 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
18834
18835 salt_buf_ptr[salt_len + 3] = 0x01;
18836 salt_buf_ptr[salt_len + 4] = 0x80;
18837
18838 salt->salt_len = salt_len;
18839 salt->salt_iter = iter - 1;
18840
18841 // decode hash
18842
18843 u8 tmp_buf[100] = { 0 };
18844
18845 int hash_len = base64_decode (base64_to_int, (const u8 *) hash_pos, hash_b64_len, tmp_buf);
18846
18847 if (hash_len < 16) return (PARSER_HASH_LENGTH);
18848
18849 memcpy (digest, tmp_buf, 64);
18850
18851 digest[0] = byte_swap_64 (digest[0]);
18852 digest[1] = byte_swap_64 (digest[1]);
18853 digest[2] = byte_swap_64 (digest[2]);
18854 digest[3] = byte_swap_64 (digest[3]);
18855 digest[4] = byte_swap_64 (digest[4]);
18856 digest[5] = byte_swap_64 (digest[5]);
18857 digest[6] = byte_swap_64 (digest[6]);
18858 digest[7] = byte_swap_64 (digest[7]);
18859
18860 // add some stuff to normal salt to make sorted happy
18861
18862 salt->salt_buf[0] = pbkdf2_sha512->salt_buf[0];
18863 salt->salt_buf[1] = pbkdf2_sha512->salt_buf[1];
18864 salt->salt_buf[2] = pbkdf2_sha512->salt_buf[2];
18865 salt->salt_buf[3] = pbkdf2_sha512->salt_buf[3];
18866 salt->salt_buf[4] = salt->salt_iter;
18867
18868 return (PARSER_OK);
18869 }
18870
18871 int ecryptfs_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18872 {
18873 if ((input_len < DISPLAY_LEN_MIN_12200) || (input_len > DISPLAY_LEN_MAX_12200)) return (PARSER_GLOBAL_LENGTH);
18874
18875 if (memcmp (SIGNATURE_ECRYPTFS, input_buf, 10)) return (PARSER_SIGNATURE_UNMATCHED);
18876
18877 uint *digest = (uint *) hash_buf->digest;
18878
18879 salt_t *salt = hash_buf->salt;
18880
18881 /**
18882 * parse line
18883 */
18884
18885 char *salt_pos = input_buf + 10 + 2 + 2; // skip over "0$" and "1$"
18886
18887 char *hash_pos = strchr (salt_pos, '$');
18888
18889 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18890
18891 u32 salt_len = hash_pos - salt_pos;
18892
18893 if (salt_len != 16) return (PARSER_SALT_LENGTH);
18894
18895 hash_pos++;
18896
18897 u32 hash_len = input_len - 10 - 2 - 2 - salt_len - 1;
18898
18899 if (hash_len != 16) return (PARSER_HASH_LENGTH);
18900
18901 // decode hash
18902
18903 digest[ 0] = hex_to_u32 ((const u8 *) &hash_pos[0]);
18904 digest[ 1] = hex_to_u32 ((const u8 *) &hash_pos[8]);
18905 digest[ 2] = 0;
18906 digest[ 3] = 0;
18907 digest[ 4] = 0;
18908 digest[ 5] = 0;
18909 digest[ 6] = 0;
18910 digest[ 7] = 0;
18911 digest[ 8] = 0;
18912 digest[ 9] = 0;
18913 digest[10] = 0;
18914 digest[11] = 0;
18915 digest[12] = 0;
18916 digest[13] = 0;
18917 digest[14] = 0;
18918 digest[15] = 0;
18919
18920 // decode salt
18921
18922 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_pos[0]);
18923 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_pos[8]);
18924
18925 salt->salt_iter = ROUNDS_ECRYPTFS;
18926 salt->salt_len = 8;
18927
18928 return (PARSER_OK);
18929 }
18930
18931 int bsdicrypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18932 {
18933 if ((input_len < DISPLAY_LEN_MIN_12400) || (input_len > DISPLAY_LEN_MAX_12400)) return (PARSER_GLOBAL_LENGTH);
18934
18935 if (memcmp (SIGNATURE_BSDICRYPT, input_buf, 1)) return (PARSER_SIGNATURE_UNMATCHED);
18936
18937 unsigned char c19 = itoa64_to_int (input_buf[19]);
18938
18939 if (c19 & 3) return (PARSER_HASH_VALUE);
18940
18941 salt_t *salt = hash_buf->salt;
18942
18943 u32 *digest = (u32 *) hash_buf->digest;
18944
18945 // iteration count
18946
18947 salt->salt_iter = itoa64_to_int (input_buf[1])
18948 | itoa64_to_int (input_buf[2]) << 6
18949 | itoa64_to_int (input_buf[3]) << 12
18950 | itoa64_to_int (input_buf[4]) << 18;
18951
18952 // set salt
18953
18954 salt->salt_buf[0] = itoa64_to_int (input_buf[5])
18955 | itoa64_to_int (input_buf[6]) << 6
18956 | itoa64_to_int (input_buf[7]) << 12
18957 | itoa64_to_int (input_buf[8]) << 18;
18958
18959 salt->salt_len = 4;
18960
18961 u8 tmp_buf[100] = { 0 };
18962
18963 base64_decode (itoa64_to_int, (const u8 *) input_buf + 9, 11, tmp_buf);
18964
18965 memcpy (digest, tmp_buf, 8);
18966
18967 uint tt;
18968
18969 IP (digest[0], digest[1], tt);
18970
18971 digest[0] = rotr32 (digest[0], 31);
18972 digest[1] = rotr32 (digest[1], 31);
18973 digest[2] = 0;
18974 digest[3] = 0;
18975
18976 return (PARSER_OK);
18977 }
18978
18979 int rar3hp_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18980 {
18981 if ((input_len < DISPLAY_LEN_MIN_12500) || (input_len > DISPLAY_LEN_MAX_12500)) return (PARSER_GLOBAL_LENGTH);
18982
18983 if (memcmp (SIGNATURE_RAR3, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
18984
18985 u32 *digest = (u32 *) hash_buf->digest;
18986
18987 salt_t *salt = hash_buf->salt;
18988
18989 /**
18990 * parse line
18991 */
18992
18993 char *type_pos = input_buf + 6 + 1;
18994
18995 char *salt_pos = strchr (type_pos, '*');
18996
18997 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18998
18999 u32 type_len = salt_pos - type_pos;
19000
19001 if (type_len != 1) return (PARSER_SALT_LENGTH);
19002
19003 salt_pos++;
19004
19005 char *crypted_pos = strchr (salt_pos, '*');
19006
19007 if (crypted_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19008
19009 u32 salt_len = crypted_pos - salt_pos;
19010
19011 if (salt_len != 16) return (PARSER_SALT_LENGTH);
19012
19013 crypted_pos++;
19014
19015 u32 crypted_len = input_len - 6 - 1 - type_len - 1 - salt_len - 1;
19016
19017 if (crypted_len != 32) return (PARSER_SALT_LENGTH);
19018
19019 /**
19020 * copy data
19021 */
19022
19023 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_pos[0]);
19024 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_pos[8]);
19025
19026 salt->salt_buf[0] = byte_swap_32 (salt->salt_buf[0]);
19027 salt->salt_buf[1] = byte_swap_32 (salt->salt_buf[1]);
19028
19029 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &crypted_pos[ 0]);
19030 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &crypted_pos[ 8]);
19031 salt->salt_buf[4] = hex_to_u32 ((const u8 *) &crypted_pos[16]);
19032 salt->salt_buf[5] = hex_to_u32 ((const u8 *) &crypted_pos[24]);
19033
19034 salt->salt_len = 24;
19035 salt->salt_iter = ROUNDS_RAR3;
19036
19037 // there's no hash for rar3. the data which is in crypted_pos is some encrypted data and
19038 // if it matches the value \xc4\x3d\x7b\x00\x40\x07\x00 after decrypt we know that we successfully cracked it.
19039
19040 digest[0] = 0xc43d7b00;
19041 digest[1] = 0x40070000;
19042 digest[2] = 0;
19043 digest[3] = 0;
19044
19045 return (PARSER_OK);
19046 }
19047
19048 int rar5_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
19049 {
19050 if ((input_len < DISPLAY_LEN_MIN_13000) || (input_len > DISPLAY_LEN_MAX_13000)) return (PARSER_GLOBAL_LENGTH);
19051
19052 if (memcmp (SIGNATURE_RAR5, input_buf, 1 + 4 + 1)) return (PARSER_SIGNATURE_UNMATCHED);
19053
19054 u32 *digest = (u32 *) hash_buf->digest;
19055
19056 salt_t *salt = hash_buf->salt;
19057
19058 rar5_t *rar5 = (rar5_t *) hash_buf->esalt;
19059
19060 /**
19061 * parse line
19062 */
19063
19064 char *param0_pos = input_buf + 1 + 4 + 1;
19065
19066 char *param1_pos = strchr (param0_pos, '$');
19067
19068 if (param1_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19069
19070 u32 param0_len = param1_pos - param0_pos;
19071
19072 param1_pos++;
19073
19074 char *param2_pos = strchr (param1_pos, '$');
19075
19076 if (param2_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19077
19078 u32 param1_len = param2_pos - param1_pos;
19079
19080 param2_pos++;
19081
19082 char *param3_pos = strchr (param2_pos, '$');
19083
19084 if (param3_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19085
19086 u32 param2_len = param3_pos - param2_pos;
19087
19088 param3_pos++;
19089
19090 char *param4_pos = strchr (param3_pos, '$');
19091
19092 if (param4_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19093
19094 u32 param3_len = param4_pos - param3_pos;
19095
19096 param4_pos++;
19097
19098 char *param5_pos = strchr (param4_pos, '$');
19099
19100 if (param5_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19101
19102 u32 param4_len = param5_pos - param4_pos;
19103
19104 param5_pos++;
19105
19106 u32 param5_len = input_len - 1 - 4 - 1 - param0_len - 1 - param1_len - 1 - param2_len - 1 - param3_len - 1 - param4_len - 1;
19107
19108 char *salt_buf = param1_pos;
19109 char *iv = param3_pos;
19110 char *pswcheck = param5_pos;
19111
19112 const uint salt_len = atoi (param0_pos);
19113 const uint iterations = atoi (param2_pos);
19114 const uint pswcheck_len = atoi (param4_pos);
19115
19116 /**
19117 * verify some data
19118 */
19119
19120 if (param1_len != 32) return (PARSER_SALT_VALUE);
19121 if (param3_len != 32) return (PARSER_SALT_VALUE);
19122 if (param5_len != 16) return (PARSER_SALT_VALUE);
19123
19124 if (salt_len != 16) return (PARSER_SALT_VALUE);
19125 if (iterations == 0) return (PARSER_SALT_VALUE);
19126 if (pswcheck_len != 8) return (PARSER_SALT_VALUE);
19127
19128 /**
19129 * store data
19130 */
19131
19132 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_buf[ 0]);
19133 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_buf[ 8]);
19134 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &salt_buf[16]);
19135 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &salt_buf[24]);
19136
19137 rar5->iv[0] = hex_to_u32 ((const u8 *) &iv[ 0]);
19138 rar5->iv[1] = hex_to_u32 ((const u8 *) &iv[ 8]);
19139 rar5->iv[2] = hex_to_u32 ((const u8 *) &iv[16]);
19140 rar5->iv[3] = hex_to_u32 ((const u8 *) &iv[24]);
19141
19142 salt->salt_len = 16;
19143
19144 salt->salt_sign[0] = iterations;
19145
19146 salt->salt_iter = ((1 << iterations) + 32) - 1;
19147
19148 /**
19149 * digest buf
19150 */
19151
19152 digest[0] = hex_to_u32 ((const u8 *) &pswcheck[ 0]);
19153 digest[1] = hex_to_u32 ((const u8 *) &pswcheck[ 8]);
19154 digest[2] = 0;
19155 digest[3] = 0;
19156
19157 return (PARSER_OK);
19158 }
19159
19160 int krb5tgs_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
19161 {
19162 if ((input_len < DISPLAY_LEN_MIN_13100) || (input_len > DISPLAY_LEN_MAX_13100)) return (PARSER_GLOBAL_LENGTH);
19163
19164 if (memcmp (SIGNATURE_KRB5TGS, input_buf, 11)) return (PARSER_SIGNATURE_UNMATCHED);
19165
19166 u32 *digest = (u32 *) hash_buf->digest;
19167
19168 salt_t *salt = hash_buf->salt;
19169
19170 krb5tgs_t *krb5tgs = (krb5tgs_t *) hash_buf->esalt;
19171
19172 /**
19173 * parse line
19174 */
19175
19176 /* Skip '$' */
19177 char *account_pos = input_buf + 11 + 1;
19178
19179 char *data_pos;
19180
19181 uint data_len;
19182
19183 if (account_pos[0] == '*')
19184 {
19185 account_pos++;
19186
19187 data_pos = strchr (account_pos, '*');
19188
19189 /* Skip '*' */
19190 data_pos++;
19191
19192 if (data_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19193
19194 uint account_len = data_pos - account_pos + 1;
19195
19196 if (account_len >= 512) return (PARSER_SALT_LENGTH);
19197
19198 /* Skip '$' */
19199 data_pos++;
19200
19201 data_len = input_len - 11 - 1 - account_len - 2;
19202
19203 memcpy (krb5tgs->account_info, account_pos - 1, account_len);
19204 }
19205 else
19206 {
19207 /* assume $krb5tgs$23$checksum$edata2 */
19208 data_pos = account_pos;
19209
19210 memcpy (krb5tgs->account_info, "**", 3);
19211
19212 data_len = input_len - 11 - 1 - 1;
19213 }
19214
19215 if (data_len < ((16 + 32) * 2)) return (PARSER_SALT_LENGTH);
19216
19217 char *checksum_ptr = (char *) krb5tgs->checksum;
19218
19219 for (uint i = 0; i < 16 * 2; i += 2)
19220 {
19221 const char p0 = data_pos[i + 0];
19222 const char p1 = data_pos[i + 1];
19223
19224 *checksum_ptr++ = hex_convert (p1) << 0
19225 | hex_convert (p0) << 4;
19226 }
19227
19228 char *edata_ptr = (char *) krb5tgs->edata2;
19229
19230 krb5tgs->edata2_len = (data_len - 32) / 2 ;
19231
19232 /* skip '$' */
19233 for (uint i = 16 * 2 + 1; i < (krb5tgs->edata2_len * 2) + (16 * 2 + 1); i += 2)
19234 {
19235 const char p0 = data_pos[i + 0];
19236 const char p1 = data_pos[i + 1];
19237 *edata_ptr++ = hex_convert (p1) << 0
19238 | hex_convert (p0) << 4;
19239 }
19240
19241 /* this is needed for hmac_md5 */
19242 *edata_ptr++ = 0x80;
19243
19244 salt->salt_buf[0] = krb5tgs->checksum[0];
19245 salt->salt_buf[1] = krb5tgs->checksum[1];
19246 salt->salt_buf[2] = krb5tgs->checksum[2];
19247 salt->salt_buf[3] = krb5tgs->checksum[3];
19248
19249 salt->salt_len = 32;
19250
19251 digest[0] = krb5tgs->checksum[0];
19252 digest[1] = krb5tgs->checksum[1];
19253 digest[2] = krb5tgs->checksum[2];
19254 digest[3] = krb5tgs->checksum[3];
19255
19256 return (PARSER_OK);
19257 }
19258
19259 int axcrypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
19260 {
19261 if ((input_len < DISPLAY_LEN_MIN_13200) || (input_len > DISPLAY_LEN_MAX_13200)) return (PARSER_GLOBAL_LENGTH);
19262
19263 if (memcmp (SIGNATURE_AXCRYPT, input_buf, 11)) return (PARSER_SIGNATURE_UNMATCHED);
19264
19265 u32 *digest = (u32 *) hash_buf->digest;
19266
19267 salt_t *salt = hash_buf->salt;
19268
19269 /**
19270 * parse line
19271 */
19272
19273 /* Skip '*' */
19274 char *wrapping_rounds_pos = input_buf + 11 + 1;
19275
19276 char *salt_pos;
19277
19278 char *wrapped_key_pos;
19279
19280 char *data_pos;
19281
19282 salt->salt_iter = atoi (wrapping_rounds_pos);
19283
19284 salt_pos = strchr (wrapping_rounds_pos, '*');
19285
19286 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19287
19288 uint wrapping_rounds_len = salt_pos - wrapping_rounds_pos;
19289
19290 /* Skip '*' */
19291 salt_pos++;
19292
19293 data_pos = salt_pos;
19294
19295 wrapped_key_pos = strchr (salt_pos, '*');
19296
19297 if (wrapped_key_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19298
19299 uint salt_len = wrapped_key_pos - salt_pos;
19300
19301 if (salt_len != 32) return (PARSER_SALT_LENGTH);
19302
19303 /* Skip '*' */
19304 wrapped_key_pos++;
19305
19306 uint wrapped_key_len = input_len - 11 - 1 - wrapping_rounds_len - 1 - salt_len - 1;
19307
19308 if (wrapped_key_len != 48) return (PARSER_SALT_LENGTH);
19309
19310 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &data_pos[ 0]);
19311 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &data_pos[ 8]);
19312 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &data_pos[16]);
19313 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &data_pos[24]);
19314
19315 data_pos += 33;
19316
19317 salt->salt_buf[4] = hex_to_u32 ((const u8 *) &data_pos[ 0]);
19318 salt->salt_buf[5] = hex_to_u32 ((const u8 *) &data_pos[ 8]);
19319 salt->salt_buf[6] = hex_to_u32 ((const u8 *) &data_pos[16]);
19320 salt->salt_buf[7] = hex_to_u32 ((const u8 *) &data_pos[24]);
19321 salt->salt_buf[8] = hex_to_u32 ((const u8 *) &data_pos[32]);
19322 salt->salt_buf[9] = hex_to_u32 ((const u8 *) &data_pos[40]);
19323
19324 salt->salt_len = 40;
19325
19326 digest[0] = salt->salt_buf[0];
19327 digest[1] = salt->salt_buf[1];
19328 digest[2] = salt->salt_buf[2];
19329 digest[3] = salt->salt_buf[3];
19330
19331 return (PARSER_OK);
19332 }
19333
19334 int keepass_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
19335 {
19336 if ((input_len < DISPLAY_LEN_MIN_13400) || (input_len > DISPLAY_LEN_MAX_13400)) return (PARSER_GLOBAL_LENGTH);
19337
19338 if (memcmp (SIGNATURE_KEEPASS, input_buf, 9)) return (PARSER_SIGNATURE_UNMATCHED);
19339
19340 u32 *digest = (u32 *) hash_buf->digest;
19341
19342 salt_t *salt = hash_buf->salt;
19343
19344 keepass_t *keepass = (keepass_t *) hash_buf->esalt;
19345
19346 /**
19347 * parse line
19348 */
19349
19350 char *version_pos;
19351
19352 char *rounds_pos;
19353
19354 char *algorithm_pos;
19355
19356 char *final_random_seed_pos;
19357 u32 final_random_seed_len;
19358
19359 char *transf_random_seed_pos;
19360 u32 transf_random_seed_len;
19361
19362 char *enc_iv_pos;
19363 u32 enc_iv_len;
19364
19365 /* default is no keyfile provided */
19366 char *keyfile_len_pos;
19367 u32 keyfile_len = 0;
19368 u32 is_keyfile_present = 0;
19369 char *keyfile_inline_pos;
19370 char *keyfile_pos;
19371
19372 /* specific to version 1 */
19373 char *contents_len_pos;
19374 u32 contents_len;
19375 char *contents_pos;
19376
19377 /* specific to version 2 */
19378 char *expected_bytes_pos;
19379 u32 expected_bytes_len;
19380
19381 char *contents_hash_pos;
19382 u32 contents_hash_len;
19383
19384 version_pos = input_buf + 8 + 1 + 1;
19385
19386 keepass->version = atoi (version_pos);
19387
19388 rounds_pos = strchr (version_pos, '*');
19389
19390 if (rounds_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19391
19392 rounds_pos++;
19393
19394 salt->salt_iter = (atoi (rounds_pos));
19395
19396 algorithm_pos = strchr (rounds_pos, '*');
19397
19398 if (algorithm_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19399
19400 algorithm_pos++;
19401
19402 keepass->algorithm = atoi (algorithm_pos);
19403
19404 final_random_seed_pos = strchr (algorithm_pos, '*');
19405
19406 if (final_random_seed_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19407
19408 final_random_seed_pos++;
19409
19410 keepass->final_random_seed[0] = hex_to_u32 ((const u8 *) &final_random_seed_pos[ 0]);
19411 keepass->final_random_seed[1] = hex_to_u32 ((const u8 *) &final_random_seed_pos[ 8]);
19412 keepass->final_random_seed[2] = hex_to_u32 ((const u8 *) &final_random_seed_pos[16]);
19413 keepass->final_random_seed[3] = hex_to_u32 ((const u8 *) &final_random_seed_pos[24]);
19414
19415 if (keepass->version == 2)
19416 {
19417 keepass->final_random_seed[4] = hex_to_u32 ((const u8 *) &final_random_seed_pos[32]);
19418 keepass->final_random_seed[5] = hex_to_u32 ((const u8 *) &final_random_seed_pos[40]);
19419 keepass->final_random_seed[6] = hex_to_u32 ((const u8 *) &final_random_seed_pos[48]);
19420 keepass->final_random_seed[7] = hex_to_u32 ((const u8 *) &final_random_seed_pos[56]);
19421 }
19422
19423 transf_random_seed_pos = strchr (final_random_seed_pos, '*');
19424
19425 if (transf_random_seed_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19426
19427 final_random_seed_len = transf_random_seed_pos - final_random_seed_pos;
19428
19429 if (keepass->version == 1 && final_random_seed_len != 32) return (PARSER_SALT_LENGTH);
19430 if (keepass->version == 2 && final_random_seed_len != 64) return (PARSER_SALT_LENGTH);
19431
19432 transf_random_seed_pos++;
19433
19434 keepass->transf_random_seed[0] = hex_to_u32 ((const u8 *) &transf_random_seed_pos[ 0]);
19435 keepass->transf_random_seed[1] = hex_to_u32 ((const u8 *) &transf_random_seed_pos[ 8]);
19436 keepass->transf_random_seed[2] = hex_to_u32 ((const u8 *) &transf_random_seed_pos[16]);
19437 keepass->transf_random_seed[3] = hex_to_u32 ((const u8 *) &transf_random_seed_pos[24]);
19438 keepass->transf_random_seed[4] = hex_to_u32 ((const u8 *) &transf_random_seed_pos[32]);
19439 keepass->transf_random_seed[5] = hex_to_u32 ((const u8 *) &transf_random_seed_pos[40]);
19440 keepass->transf_random_seed[6] = hex_to_u32 ((const u8 *) &transf_random_seed_pos[48]);
19441 keepass->transf_random_seed[7] = hex_to_u32 ((const u8 *) &transf_random_seed_pos[56]);
19442
19443 enc_iv_pos = strchr (transf_random_seed_pos, '*');
19444
19445 if (enc_iv_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19446
19447 transf_random_seed_len = enc_iv_pos - transf_random_seed_pos;
19448
19449 if (transf_random_seed_len != 64) return (PARSER_SALT_LENGTH);
19450
19451 enc_iv_pos++;
19452
19453 keepass->enc_iv[0] = hex_to_u32 ((const u8 *) &enc_iv_pos[ 0]);
19454 keepass->enc_iv[1] = hex_to_u32 ((const u8 *) &enc_iv_pos[ 8]);
19455 keepass->enc_iv[2] = hex_to_u32 ((const u8 *) &enc_iv_pos[16]);
19456 keepass->enc_iv[3] = hex_to_u32 ((const u8 *) &enc_iv_pos[24]);
19457
19458 if (keepass->version == 1)
19459 {
19460 contents_hash_pos = strchr (enc_iv_pos, '*');
19461
19462 if (contents_hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19463
19464 enc_iv_len = contents_hash_pos - enc_iv_pos;
19465
19466 if (enc_iv_len != 32) return (PARSER_SALT_LENGTH);
19467
19468 contents_hash_pos++;
19469
19470 keepass->contents_hash[0] = hex_to_u32 ((const u8 *) &contents_hash_pos[ 0]);
19471 keepass->contents_hash[1] = hex_to_u32 ((const u8 *) &contents_hash_pos[ 8]);
19472 keepass->contents_hash[2] = hex_to_u32 ((const u8 *) &contents_hash_pos[16]);
19473 keepass->contents_hash[3] = hex_to_u32 ((const u8 *) &contents_hash_pos[24]);
19474 keepass->contents_hash[4] = hex_to_u32 ((const u8 *) &contents_hash_pos[32]);
19475 keepass->contents_hash[5] = hex_to_u32 ((const u8 *) &contents_hash_pos[40]);
19476 keepass->contents_hash[6] = hex_to_u32 ((const u8 *) &contents_hash_pos[48]);
19477 keepass->contents_hash[7] = hex_to_u32 ((const u8 *) &contents_hash_pos[56]);
19478
19479 /* get length of contents following */
19480 char *inline_flag_pos = strchr (contents_hash_pos, '*');
19481
19482 if (inline_flag_pos == NULL) return (PARSER_SALT_LENGTH);
19483
19484 contents_hash_len = inline_flag_pos - contents_hash_pos;
19485
19486 if (contents_hash_len != 64) return (PARSER_SALT_LENGTH);
19487
19488 inline_flag_pos++;
19489
19490 u32 inline_flag = atoi (inline_flag_pos);
19491
19492 if (inline_flag != 1) return (PARSER_SALT_LENGTH);
19493
19494 contents_len_pos = strchr (inline_flag_pos, '*');
19495
19496 if (contents_len_pos == NULL) return (PARSER_SALT_LENGTH);
19497
19498 contents_len_pos++;
19499
19500 contents_len = atoi (contents_len_pos);
19501
19502 if (contents_len > 50000) return (PARSER_SALT_LENGTH);
19503
19504 contents_pos = strchr (contents_len_pos, '*');
19505
19506 if (contents_pos == NULL) return (PARSER_SALT_LENGTH);
19507
19508 contents_pos++;
19509
19510 u32 i;
19511
19512 keepass->contents_len = contents_len;
19513
19514 contents_len = contents_len / 4;
19515
19516 keyfile_inline_pos = strchr (contents_pos, '*');
19517
19518 u32 real_contents_len;
19519
19520 if (keyfile_inline_pos == NULL)
19521 real_contents_len = input_len - (contents_pos - input_buf);
19522 else
19523 {
19524 real_contents_len = keyfile_inline_pos - contents_pos;
19525 keyfile_inline_pos++;
19526 is_keyfile_present = 1;
19527 }
19528
19529 if (real_contents_len != keepass->contents_len * 2) return (PARSER_SALT_LENGTH);
19530
19531 for (i = 0; i < contents_len; i++)
19532 keepass->contents[i] = hex_to_u32 ((const u8 *) &contents_pos[i * 8]);
19533 }
19534 else if (keepass->version == 2)
19535 {
19536 expected_bytes_pos = strchr (enc_iv_pos, '*');
19537
19538 if (expected_bytes_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19539
19540 enc_iv_len = expected_bytes_pos - enc_iv_pos;
19541
19542 if (enc_iv_len != 32) return (PARSER_SALT_LENGTH);
19543
19544 expected_bytes_pos++;
19545
19546 keepass->expected_bytes[0] = hex_to_u32 ((const u8 *) &expected_bytes_pos[ 0]);
19547 keepass->expected_bytes[1] = hex_to_u32 ((const u8 *) &expected_bytes_pos[ 8]);
19548 keepass->expected_bytes[2] = hex_to_u32 ((const u8 *) &expected_bytes_pos[16]);
19549 keepass->expected_bytes[3] = hex_to_u32 ((const u8 *) &expected_bytes_pos[24]);
19550 keepass->expected_bytes[4] = hex_to_u32 ((const u8 *) &expected_bytes_pos[32]);
19551 keepass->expected_bytes[5] = hex_to_u32 ((const u8 *) &expected_bytes_pos[40]);
19552 keepass->expected_bytes[6] = hex_to_u32 ((const u8 *) &expected_bytes_pos[48]);
19553 keepass->expected_bytes[7] = hex_to_u32 ((const u8 *) &expected_bytes_pos[56]);
19554
19555 contents_hash_pos = strchr (expected_bytes_pos, '*');
19556
19557 if (contents_hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19558
19559 expected_bytes_len = contents_hash_pos - expected_bytes_pos;
19560
19561 if (expected_bytes_len != 64) return (PARSER_SALT_LENGTH);
19562
19563 contents_hash_pos++;
19564
19565 keepass->contents_hash[0] = hex_to_u32 ((const u8 *) &contents_hash_pos[ 0]);
19566 keepass->contents_hash[1] = hex_to_u32 ((const u8 *) &contents_hash_pos[ 8]);
19567 keepass->contents_hash[2] = hex_to_u32 ((const u8 *) &contents_hash_pos[16]);
19568 keepass->contents_hash[3] = hex_to_u32 ((const u8 *) &contents_hash_pos[24]);
19569 keepass->contents_hash[4] = hex_to_u32 ((const u8 *) &contents_hash_pos[32]);
19570 keepass->contents_hash[5] = hex_to_u32 ((const u8 *) &contents_hash_pos[40]);
19571 keepass->contents_hash[6] = hex_to_u32 ((const u8 *) &contents_hash_pos[48]);
19572 keepass->contents_hash[7] = hex_to_u32 ((const u8 *) &contents_hash_pos[56]);
19573
19574 keyfile_inline_pos = strchr (contents_hash_pos, '*');
19575
19576 if (keyfile_inline_pos == NULL)
19577 contents_hash_len = input_len - (int) (contents_hash_pos - input_buf);
19578 else
19579 {
19580 contents_hash_len = keyfile_inline_pos - contents_hash_pos;
19581 keyfile_inline_pos++;
19582 is_keyfile_present = 1;
19583 }
19584 if (contents_hash_len != 64) return (PARSER_SALT_LENGTH);
19585 }
19586
19587 if (is_keyfile_present != 0)
19588 {
19589 keyfile_len_pos = strchr (keyfile_inline_pos, '*');
19590
19591 keyfile_len_pos++;
19592
19593 keyfile_len = atoi (keyfile_len_pos);
19594
19595 keepass->keyfile_len = keyfile_len;
19596
19597 if (keyfile_len != 64) return (PARSER_SALT_LENGTH);
19598
19599 keyfile_pos = strchr (keyfile_len_pos, '*');
19600
19601 if (keyfile_pos == NULL) return (PARSER_SALT_LENGTH);
19602
19603 keyfile_pos++;
19604
19605 u32 real_keyfile_len = input_len - (keyfile_pos - input_buf);
19606
19607 if (real_keyfile_len != 64) return (PARSER_SALT_LENGTH);
19608
19609 keepass->keyfile[0] = hex_to_u32 ((const u8 *) &keyfile_pos[ 0]);
19610 keepass->keyfile[1] = hex_to_u32 ((const u8 *) &keyfile_pos[ 8]);
19611 keepass->keyfile[2] = hex_to_u32 ((const u8 *) &keyfile_pos[16]);
19612 keepass->keyfile[3] = hex_to_u32 ((const u8 *) &keyfile_pos[24]);
19613 keepass->keyfile[4] = hex_to_u32 ((const u8 *) &keyfile_pos[32]);
19614 keepass->keyfile[5] = hex_to_u32 ((const u8 *) &keyfile_pos[40]);
19615 keepass->keyfile[6] = hex_to_u32 ((const u8 *) &keyfile_pos[48]);
19616 keepass->keyfile[7] = hex_to_u32 ((const u8 *) &keyfile_pos[56]);
19617 }
19618
19619 digest[0] = keepass->enc_iv[0];
19620 digest[1] = keepass->enc_iv[1];
19621 digest[2] = keepass->enc_iv[2];
19622 digest[3] = keepass->enc_iv[3];
19623
19624 salt->salt_buf[0] = keepass->transf_random_seed[0];
19625 salt->salt_buf[1] = keepass->transf_random_seed[1];
19626 salt->salt_buf[2] = keepass->transf_random_seed[2];
19627 salt->salt_buf[3] = keepass->transf_random_seed[3];
19628 salt->salt_buf[4] = keepass->transf_random_seed[4];
19629 salt->salt_buf[5] = keepass->transf_random_seed[5];
19630 salt->salt_buf[6] = keepass->transf_random_seed[6];
19631 salt->salt_buf[7] = keepass->transf_random_seed[7];
19632
19633 return (PARSER_OK);
19634 }
19635
19636 int cf10_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
19637 {
19638 if ((input_len < DISPLAY_LEN_MIN_12600) || (input_len > DISPLAY_LEN_MAX_12600)) return (PARSER_GLOBAL_LENGTH);
19639
19640 u32 *digest = (u32 *) hash_buf->digest;
19641
19642 salt_t *salt = hash_buf->salt;
19643
19644 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
19645 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
19646 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
19647 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
19648 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
19649 digest[5] = hex_to_u32 ((const u8 *) &input_buf[40]);
19650 digest[6] = hex_to_u32 ((const u8 *) &input_buf[48]);
19651 digest[7] = hex_to_u32 ((const u8 *) &input_buf[56]);
19652
19653 if (input_buf[64] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
19654
19655 uint salt_len = input_len - 64 - 1;
19656
19657 char *salt_buf = input_buf + 64 + 1;
19658
19659 char *salt_buf_ptr = (char *) salt->salt_buf;
19660
19661 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
19662
19663 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
19664
19665 salt->salt_len = salt_len;
19666
19667 /**
19668 * we can precompute the first sha256 transform
19669 */
19670
19671 uint w[16] = { 0 };
19672
19673 w[ 0] = byte_swap_32 (salt->salt_buf[ 0]);
19674 w[ 1] = byte_swap_32 (salt->salt_buf[ 1]);
19675 w[ 2] = byte_swap_32 (salt->salt_buf[ 2]);
19676 w[ 3] = byte_swap_32 (salt->salt_buf[ 3]);
19677 w[ 4] = byte_swap_32 (salt->salt_buf[ 4]);
19678 w[ 5] = byte_swap_32 (salt->salt_buf[ 5]);
19679 w[ 6] = byte_swap_32 (salt->salt_buf[ 6]);
19680 w[ 7] = byte_swap_32 (salt->salt_buf[ 7]);
19681 w[ 8] = byte_swap_32 (salt->salt_buf[ 8]);
19682 w[ 9] = byte_swap_32 (salt->salt_buf[ 9]);
19683 w[10] = byte_swap_32 (salt->salt_buf[10]);
19684 w[11] = byte_swap_32 (salt->salt_buf[11]);
19685 w[12] = byte_swap_32 (salt->salt_buf[12]);
19686 w[13] = byte_swap_32 (salt->salt_buf[13]);
19687 w[14] = byte_swap_32 (salt->salt_buf[14]);
19688 w[15] = byte_swap_32 (salt->salt_buf[15]);
19689
19690 uint pc256[8] = { SHA256M_A, SHA256M_B, SHA256M_C, SHA256M_D, SHA256M_E, SHA256M_F, SHA256M_G, SHA256M_H };
19691
19692 sha256_64 (w, pc256);
19693
19694 salt->salt_buf_pc[0] = pc256[0];
19695 salt->salt_buf_pc[1] = pc256[1];
19696 salt->salt_buf_pc[2] = pc256[2];
19697 salt->salt_buf_pc[3] = pc256[3];
19698 salt->salt_buf_pc[4] = pc256[4];
19699 salt->salt_buf_pc[5] = pc256[5];
19700 salt->salt_buf_pc[6] = pc256[6];
19701 salt->salt_buf_pc[7] = pc256[7];
19702
19703 digest[0] -= pc256[0];
19704 digest[1] -= pc256[1];
19705 digest[2] -= pc256[2];
19706 digest[3] -= pc256[3];
19707 digest[4] -= pc256[4];
19708 digest[5] -= pc256[5];
19709 digest[6] -= pc256[6];
19710 digest[7] -= pc256[7];
19711
19712 return (PARSER_OK);
19713 }
19714
19715 int mywallet_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
19716 {
19717 if ((input_len < DISPLAY_LEN_MIN_12700) || (input_len > DISPLAY_LEN_MAX_12700)) return (PARSER_GLOBAL_LENGTH);
19718
19719 if (memcmp (SIGNATURE_MYWALLET, input_buf, 12)) return (PARSER_SIGNATURE_UNMATCHED);
19720
19721 u32 *digest = (u32 *) hash_buf->digest;
19722
19723 salt_t *salt = hash_buf->salt;
19724
19725 /**
19726 * parse line
19727 */
19728
19729 char *data_len_pos = input_buf + 1 + 10 + 1;
19730
19731 char *data_buf_pos = strchr (data_len_pos, '$');
19732
19733 if (data_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19734
19735 u32 data_len_len = data_buf_pos - data_len_pos;
19736
19737 if (data_len_len < 1) return (PARSER_SALT_LENGTH);
19738 if (data_len_len > 5) return (PARSER_SALT_LENGTH);
19739
19740 data_buf_pos++;
19741
19742 u32 data_buf_len = input_len - 1 - 10 - 1 - data_len_len - 1;
19743
19744 if (data_buf_len < 64) return (PARSER_HASH_LENGTH);
19745
19746 if (data_buf_len % 16) return (PARSER_HASH_LENGTH);
19747
19748 u32 data_len = atoi (data_len_pos);
19749
19750 if ((data_len * 2) != data_buf_len) return (PARSER_HASH_LENGTH);
19751
19752 /**
19753 * salt
19754 */
19755
19756 char *salt_pos = data_buf_pos;
19757
19758 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_pos[ 0]);
19759 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_pos[ 8]);
19760 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &salt_pos[16]);
19761 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &salt_pos[24]);
19762
19763 // this is actually the CT, which is also the hash later (if matched)
19764
19765 salt->salt_buf[4] = hex_to_u32 ((const u8 *) &salt_pos[32]);
19766 salt->salt_buf[5] = hex_to_u32 ((const u8 *) &salt_pos[40]);
19767 salt->salt_buf[6] = hex_to_u32 ((const u8 *) &salt_pos[48]);
19768 salt->salt_buf[7] = hex_to_u32 ((const u8 *) &salt_pos[56]);
19769
19770 salt->salt_len = 32; // note we need to fix this to 16 in kernel
19771
19772 salt->salt_iter = 10 - 1;
19773
19774 /**
19775 * digest buf
19776 */
19777
19778 digest[0] = salt->salt_buf[4];
19779 digest[1] = salt->salt_buf[5];
19780 digest[2] = salt->salt_buf[6];
19781 digest[3] = salt->salt_buf[7];
19782
19783 return (PARSER_OK);
19784 }
19785
19786 int ms_drsr_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
19787 {
19788 if ((input_len < DISPLAY_LEN_MIN_12800) || (input_len > DISPLAY_LEN_MAX_12800)) return (PARSER_GLOBAL_LENGTH);
19789
19790 if (memcmp (SIGNATURE_MS_DRSR, input_buf, 11)) return (PARSER_SIGNATURE_UNMATCHED);
19791
19792 u32 *digest = (u32 *) hash_buf->digest;
19793
19794 salt_t *salt = hash_buf->salt;
19795
19796 /**
19797 * parse line
19798 */
19799
19800 char *salt_pos = input_buf + 11 + 1;
19801
19802 char *iter_pos = strchr (salt_pos, ',');
19803
19804 if (iter_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19805
19806 u32 salt_len = iter_pos - salt_pos;
19807
19808 if (salt_len != 20) return (PARSER_SALT_LENGTH);
19809
19810 iter_pos++;
19811
19812 char *hash_pos = strchr (iter_pos, ',');
19813
19814 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19815
19816 u32 iter_len = hash_pos - iter_pos;
19817
19818 if (iter_len > 5) return (PARSER_SALT_LENGTH);
19819
19820 hash_pos++;
19821
19822 u32 hash_len = input_len - 11 - 1 - salt_len - 1 - iter_len - 1;
19823
19824 if (hash_len != 64) return (PARSER_HASH_LENGTH);
19825
19826 /**
19827 * salt
19828 */
19829
19830 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_pos[ 0]);
19831 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_pos[ 8]);
19832 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &salt_pos[16]) & 0xffff0000;
19833 salt->salt_buf[3] = 0x00018000;
19834
19835 salt->salt_buf[0] = byte_swap_32 (salt->salt_buf[0]);
19836 salt->salt_buf[1] = byte_swap_32 (salt->salt_buf[1]);
19837 salt->salt_buf[2] = byte_swap_32 (salt->salt_buf[2]);
19838 salt->salt_buf[3] = byte_swap_32 (salt->salt_buf[3]);
19839
19840 salt->salt_len = salt_len / 2;
19841
19842 salt->salt_iter = atoi (iter_pos) - 1;
19843
19844 /**
19845 * digest buf
19846 */
19847
19848 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
19849 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
19850 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
19851 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
19852 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
19853 digest[5] = hex_to_u32 ((const u8 *) &hash_pos[40]);
19854 digest[6] = hex_to_u32 ((const u8 *) &hash_pos[48]);
19855 digest[7] = hex_to_u32 ((const u8 *) &hash_pos[56]);
19856
19857 return (PARSER_OK);
19858 }
19859
19860 int androidfde_samsung_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
19861 {
19862 if ((input_len < DISPLAY_LEN_MIN_12900) || (input_len > DISPLAY_LEN_MAX_12900)) return (PARSER_GLOBAL_LENGTH);
19863
19864 u32 *digest = (u32 *) hash_buf->digest;
19865
19866 salt_t *salt = hash_buf->salt;
19867
19868 /**
19869 * parse line
19870 */
19871
19872 char *hash_pos = input_buf + 64;
19873 char *salt1_pos = input_buf + 128;
19874 char *salt2_pos = input_buf;
19875
19876 /**
19877 * salt
19878 */
19879
19880 salt->salt_buf[ 0] = hex_to_u32 ((const u8 *) &salt1_pos[ 0]);
19881 salt->salt_buf[ 1] = hex_to_u32 ((const u8 *) &salt1_pos[ 8]);
19882 salt->salt_buf[ 2] = hex_to_u32 ((const u8 *) &salt1_pos[16]);
19883 salt->salt_buf[ 3] = hex_to_u32 ((const u8 *) &salt1_pos[24]);
19884
19885 salt->salt_buf[ 4] = hex_to_u32 ((const u8 *) &salt2_pos[ 0]);
19886 salt->salt_buf[ 5] = hex_to_u32 ((const u8 *) &salt2_pos[ 8]);
19887 salt->salt_buf[ 6] = hex_to_u32 ((const u8 *) &salt2_pos[16]);
19888 salt->salt_buf[ 7] = hex_to_u32 ((const u8 *) &salt2_pos[24]);
19889
19890 salt->salt_buf[ 8] = hex_to_u32 ((const u8 *) &salt2_pos[32]);
19891 salt->salt_buf[ 9] = hex_to_u32 ((const u8 *) &salt2_pos[40]);
19892 salt->salt_buf[10] = hex_to_u32 ((const u8 *) &salt2_pos[48]);
19893 salt->salt_buf[11] = hex_to_u32 ((const u8 *) &salt2_pos[56]);
19894
19895 salt->salt_len = 48;
19896
19897 salt->salt_iter = ROUNDS_ANDROIDFDE_SAMSUNG - 1;
19898
19899 /**
19900 * digest buf
19901 */
19902
19903 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
19904 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
19905 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
19906 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
19907 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
19908 digest[5] = hex_to_u32 ((const u8 *) &hash_pos[40]);
19909 digest[6] = hex_to_u32 ((const u8 *) &hash_pos[48]);
19910 digest[7] = hex_to_u32 ((const u8 *) &hash_pos[56]);
19911
19912 return (PARSER_OK);
19913 }
19914
19915 int zip2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
19916 {
19917 if ((input_len < DISPLAY_LEN_MIN_13600) || (input_len > DISPLAY_LEN_MAX_13600)) return (PARSER_GLOBAL_LENGTH);
19918
19919 if (memcmp (SIGNATURE_ZIP2_START, input_buf , 6)) return (PARSER_SIGNATURE_UNMATCHED);
19920 if (memcmp (SIGNATURE_ZIP2_STOP , input_buf + input_len - 7, 7)) return (PARSER_SIGNATURE_UNMATCHED);
19921
19922 u32 *digest = (u32 *) hash_buf->digest;
19923
19924 salt_t *salt = hash_buf->salt;
19925
19926 zip2_t *zip2 = (zip2_t *) hash_buf->esalt;
19927
19928 /**
19929 * parse line
19930 */
19931
19932 char *param0_pos = input_buf + 6 + 1;
19933
19934 char *param1_pos = strchr (param0_pos, '*');
19935
19936 if (param1_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19937
19938 u32 param0_len = param1_pos - param0_pos;
19939
19940 param1_pos++;
19941
19942 char *param2_pos = strchr (param1_pos, '*');
19943
19944 if (param2_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19945
19946 u32 param1_len = param2_pos - param1_pos;
19947
19948 param2_pos++;
19949
19950 char *param3_pos = strchr (param2_pos, '*');
19951
19952 if (param3_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19953
19954 u32 param2_len = param3_pos - param2_pos;
19955
19956 param3_pos++;
19957
19958 char *param4_pos = strchr (param3_pos, '*');
19959
19960 if (param4_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19961
19962 u32 param3_len = param4_pos - param3_pos;
19963
19964 param4_pos++;
19965
19966 char *param5_pos = strchr (param4_pos, '*');
19967
19968 if (param5_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19969
19970 u32 param4_len = param5_pos - param4_pos;
19971
19972 param5_pos++;
19973
19974 char *param6_pos = strchr (param5_pos, '*');
19975
19976 if (param6_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19977
19978 u32 param5_len = param6_pos - param5_pos;
19979
19980 param6_pos++;
19981
19982 char *param7_pos = strchr (param6_pos, '*');
19983
19984 if (param7_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19985
19986 u32 param6_len = param7_pos - param6_pos;
19987
19988 param7_pos++;
19989
19990 char *param8_pos = strchr (param7_pos, '*');
19991
19992 if (param8_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19993
19994 u32 param7_len = param8_pos - param7_pos;
19995
19996 param8_pos++;
19997
19998 const uint type = atoi (param0_pos);
19999 const uint mode = atoi (param1_pos);
20000 const uint magic = atoi (param2_pos);
20001
20002 char *salt_buf = param3_pos;
20003
20004 uint verify_bytes; sscanf (param4_pos, "%4x*", &verify_bytes);
20005
20006 const uint compress_length = atoi (param5_pos);
20007
20008 char *data_buf = param6_pos;
20009 char *auth = param7_pos;
20010
20011 /**
20012 * verify some data
20013 */
20014
20015 if (param0_len != 1) return (PARSER_SALT_VALUE);
20016
20017 if (param1_len != 1) return (PARSER_SALT_VALUE);
20018
20019 if (param2_len != 1) return (PARSER_SALT_VALUE);
20020
20021 if ((param3_len != 16) && (param3_len != 24) && (param3_len != 32)) return (PARSER_SALT_VALUE);
20022
20023 if (param4_len >= 5) return (PARSER_SALT_VALUE);
20024
20025 if (param5_len >= 5) return (PARSER_SALT_VALUE);
20026
20027 if (param6_len >= 8192) return (PARSER_SALT_VALUE);
20028
20029 if (param6_len & 1) return (PARSER_SALT_VALUE);
20030
20031 if (param7_len != 20) return (PARSER_SALT_VALUE);
20032
20033 if (type != 0) return (PARSER_SALT_VALUE);
20034
20035 if ((mode != 1) && (mode != 2) && (mode != 3)) return (PARSER_SALT_VALUE);
20036
20037 if (magic != 0) return (PARSER_SALT_VALUE);
20038
20039 if (verify_bytes >= 0x10000) return (PARSER_SALT_VALUE);
20040
20041 /**
20042 * store data
20043 */
20044
20045 zip2->type = type;
20046 zip2->mode = mode;
20047 zip2->magic = magic;
20048
20049 if (mode == 1)
20050 {
20051 zip2->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_buf[ 0]);
20052 zip2->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_buf[ 8]);
20053 zip2->salt_buf[2] = 0;
20054 zip2->salt_buf[3] = 0;
20055
20056 zip2->salt_len = 8;
20057 }
20058 else if (mode == 2)
20059 {
20060 zip2->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_buf[ 0]);
20061 zip2->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_buf[ 8]);
20062 zip2->salt_buf[2] = hex_to_u32 ((const u8 *) &salt_buf[16]);
20063 zip2->salt_buf[3] = 0;
20064
20065 zip2->salt_len = 12;
20066 }
20067 else if (mode == 3)
20068 {
20069 zip2->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_buf[ 0]);
20070 zip2->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_buf[ 8]);
20071 zip2->salt_buf[2] = hex_to_u32 ((const u8 *) &salt_buf[16]);
20072 zip2->salt_buf[3] = hex_to_u32 ((const u8 *) &salt_buf[24]);
20073
20074 zip2->salt_len = 16;
20075 }
20076
20077 zip2->salt_buf[0] = byte_swap_32 (zip2->salt_buf[0]);
20078 zip2->salt_buf[1] = byte_swap_32 (zip2->salt_buf[1]);
20079 zip2->salt_buf[2] = byte_swap_32 (zip2->salt_buf[2]);
20080 zip2->salt_buf[3] = byte_swap_32 (zip2->salt_buf[3]);
20081
20082 zip2->verify_bytes = verify_bytes;
20083
20084 zip2->compress_length = compress_length;
20085
20086 char *data_buf_ptr = (char *) zip2->data_buf;
20087
20088 for (uint i = 0; i < param6_len; i += 2)
20089 {
20090 const char p0 = data_buf[i + 0];
20091 const char p1 = data_buf[i + 1];
20092
20093 *data_buf_ptr++ = hex_convert (p1) << 0
20094 | hex_convert (p0) << 4;
20095
20096 zip2->data_len++;
20097 }
20098
20099 *data_buf_ptr = 0x80;
20100
20101 char *auth_ptr = (char *) zip2->auth_buf;
20102
20103 for (uint i = 0; i < param7_len; i += 2)
20104 {
20105 const char p0 = auth[i + 0];
20106 const char p1 = auth[i + 1];
20107
20108 *auth_ptr++ = hex_convert (p1) << 0
20109 | hex_convert (p0) << 4;
20110
20111 zip2->auth_len++;
20112 }
20113
20114 /**
20115 * salt buf (fake)
20116 */
20117
20118 salt->salt_buf[0] = zip2->salt_buf[0];
20119 salt->salt_buf[1] = zip2->salt_buf[1];
20120 salt->salt_buf[2] = zip2->salt_buf[2];
20121 salt->salt_buf[3] = zip2->salt_buf[3];
20122 salt->salt_buf[4] = zip2->data_buf[0];
20123 salt->salt_buf[5] = zip2->data_buf[1];
20124 salt->salt_buf[6] = zip2->data_buf[2];
20125 salt->salt_buf[7] = zip2->data_buf[3];
20126
20127 salt->salt_len = 32;
20128
20129 salt->salt_iter = ROUNDS_ZIP2 - 1;
20130
20131 /**
20132 * digest buf (fake)
20133 */
20134
20135 digest[0] = zip2->auth_buf[0];
20136 digest[1] = zip2->auth_buf[1];
20137 digest[2] = zip2->auth_buf[2];
20138 digest[3] = zip2->auth_buf[3];
20139
20140 return (PARSER_OK);
20141 }
20142
20143 /**
20144 * parallel running threads
20145 */
20146
20147 #ifdef WIN
20148
20149 BOOL WINAPI sigHandler_default (DWORD sig)
20150 {
20151 switch (sig)
20152 {
20153 case CTRL_CLOSE_EVENT:
20154
20155 /*
20156 * special case see: https://stackoverflow.com/questions/3640633/c-setconsolectrlhandler-routine-issue/5610042#5610042
20157 * if the user interacts w/ the user-interface (GUI/cmd), we need to do the finalization job within this signal handler
20158 * function otherwise it is too late (e.g. after returning from this function)
20159 */
20160
20161 myabort ();
20162
20163 SetConsoleCtrlHandler (NULL, TRUE);
20164
20165 hc_sleep (10);
20166
20167 return TRUE;
20168
20169 case CTRL_C_EVENT:
20170 case CTRL_LOGOFF_EVENT:
20171 case CTRL_SHUTDOWN_EVENT:
20172
20173 myabort ();
20174
20175 SetConsoleCtrlHandler (NULL, TRUE);
20176
20177 return TRUE;
20178 }
20179
20180 return FALSE;
20181 }
20182
20183 BOOL WINAPI sigHandler_benchmark (DWORD sig)
20184 {
20185 switch (sig)
20186 {
20187 case CTRL_CLOSE_EVENT:
20188
20189 myabort ();
20190
20191 SetConsoleCtrlHandler (NULL, TRUE);
20192
20193 hc_sleep (10);
20194
20195 return TRUE;
20196
20197 case CTRL_C_EVENT:
20198 case CTRL_LOGOFF_EVENT:
20199 case CTRL_SHUTDOWN_EVENT:
20200
20201 myquit ();
20202
20203 SetConsoleCtrlHandler (NULL, TRUE);
20204
20205 return TRUE;
20206 }
20207
20208 return FALSE;
20209 }
20210
20211 void hc_signal (BOOL WINAPI (callback) (DWORD))
20212 {
20213 if (callback == NULL)
20214 {
20215 SetConsoleCtrlHandler ((PHANDLER_ROUTINE) callback, FALSE);
20216 }
20217 else
20218 {
20219 SetConsoleCtrlHandler ((PHANDLER_ROUTINE) callback, TRUE);
20220 }
20221 }
20222
20223 #else
20224
20225 void sigHandler_default (int sig)
20226 {
20227 myabort ();
20228
20229 signal (sig, NULL);
20230 }
20231
20232 void sigHandler_benchmark (int sig)
20233 {
20234 myquit ();
20235
20236 signal (sig, NULL);
20237 }
20238
20239 void hc_signal (void (callback) (int))
20240 {
20241 if (callback == NULL) callback = SIG_DFL;
20242
20243 signal (SIGINT, callback);
20244 signal (SIGTERM, callback);
20245 signal (SIGABRT, callback);
20246 }
20247
20248 #endif
20249
20250 void status_display ();
20251
20252 void *thread_keypress (void *p)
20253 {
20254 int benchmark = *((int *) p);
20255
20256 uint quiet = data.quiet;
20257
20258 tty_break();
20259
20260 while ((data.devices_status != STATUS_EXHAUSTED) && (data.devices_status != STATUS_CRACKED) && (data.devices_status != STATUS_ABORTED) && (data.devices_status != STATUS_QUIT))
20261 {
20262 int ch = tty_getchar();
20263
20264 if (ch == -1) break;
20265
20266 if (ch == 0) continue;
20267
20268 //https://github.com/hashcat/hashcat/issues/302
20269 //#ifdef _POSIX
20270 //if (ch != '\n')
20271 //#endif
20272
20273 hc_thread_mutex_lock (mux_display);
20274
20275 log_info ("");
20276
20277 switch (ch)
20278 {
20279 case 's':
20280 case '\r':
20281 case '\n':
20282
20283 log_info ("");
20284
20285 status_display ();
20286
20287 log_info ("");
20288
20289 if (quiet == 0) fprintf (stdout, "%s", PROMPT);
20290 if (quiet == 0) fflush (stdout);
20291
20292 break;
20293
20294 case 'b':
20295
20296 log_info ("");
20297
20298 bypass ();
20299
20300 log_info ("");
20301
20302 if (quiet == 0) fprintf (stdout, "%s", PROMPT);
20303 if (quiet == 0) fflush (stdout);
20304
20305 break;
20306
20307 case 'p':
20308
20309 log_info ("");
20310
20311 SuspendThreads ();
20312
20313 log_info ("");
20314
20315 if (quiet == 0) fprintf (stdout, "%s", PROMPT);
20316 if (quiet == 0) fflush (stdout);
20317
20318 break;
20319
20320 case 'r':
20321
20322 log_info ("");
20323
20324 ResumeThreads ();
20325
20326 log_info ("");
20327
20328 if (quiet == 0) fprintf (stdout, "%s", PROMPT);
20329 if (quiet == 0) fflush (stdout);
20330
20331 break;
20332
20333 case 'c':
20334
20335 log_info ("");
20336
20337 if (benchmark == 1) break;
20338
20339 stop_at_checkpoint ();
20340
20341 log_info ("");
20342
20343 if (quiet == 0) fprintf (stdout, "%s", PROMPT);
20344 if (quiet == 0) fflush (stdout);
20345
20346 break;
20347
20348 case 'q':
20349
20350 log_info ("");
20351
20352 if (benchmark == 1)
20353 {
20354 myquit ();
20355 }
20356 else
20357 {
20358 myabort ();
20359 }
20360
20361 break;
20362 }
20363
20364 //https://github.com/hashcat/hashcat/issues/302
20365 //#ifdef _POSIX
20366 //if (ch != '\n')
20367 //#endif
20368
20369 hc_thread_mutex_unlock (mux_display);
20370 }
20371
20372 tty_fix();
20373
20374 return (p);
20375 }
20376
20377 /**
20378 * rules common
20379 */
20380
20381 bool class_num (const u8 c)
20382 {
20383 return ((c >= '0') && (c <= '9'));
20384 }
20385
20386 bool class_lower (const u8 c)
20387 {
20388 return ((c >= 'a') && (c <= 'z'));
20389 }
20390
20391 bool class_upper (const u8 c)
20392 {
20393 return ((c >= 'A') && (c <= 'Z'));
20394 }
20395
20396 bool class_alpha (const u8 c)
20397 {
20398 return (class_lower (c) || class_upper (c));
20399 }
20400
20401 int conv_ctoi (const u8 c)
20402 {
20403 if (class_num (c))
20404 {
20405 return c - '0';
20406 }
20407 else if (class_upper (c))
20408 {
20409 return c - 'A' + 10;
20410 }
20411
20412 return -1;
20413 }
20414
20415 int conv_itoc (const u8 c)
20416 {
20417 if (c < 10)
20418 {
20419 return c + '0';
20420 }
20421 else if (c < 37)
20422 {
20423 return c + 'A' - 10;
20424 }
20425
20426 return -1;
20427 }
20428
20429 /**
20430 * device rules
20431 */
20432
20433 #define INCR_POS if (++rule_pos == rule_len) return (-1)
20434 #define SET_NAME(rule,val) (rule)->cmds[rule_cnt] = ((val) & 0xff) << 0
20435 #define SET_P0(rule,val) INCR_POS; (rule)->cmds[rule_cnt] |= ((val) & 0xff) << 8
20436 #define SET_P1(rule,val) INCR_POS; (rule)->cmds[rule_cnt] |= ((val) & 0xff) << 16
20437 #define MAX_KERNEL_RULES 255
20438 #define GET_NAME(rule) rule_cmd = (((rule)->cmds[rule_cnt] >> 0) & 0xff)
20439 #define GET_P0(rule) INCR_POS; rule_buf[rule_pos] = (((rule)->cmds[rule_cnt] >> 8) & 0xff)
20440 #define GET_P1(rule) INCR_POS; rule_buf[rule_pos] = (((rule)->cmds[rule_cnt] >> 16) & 0xff)
20441
20442 #define SET_P0_CONV(rule,val) INCR_POS; (rule)->cmds[rule_cnt] |= ((conv_ctoi (val)) & 0xff) << 8
20443 #define SET_P1_CONV(rule,val) INCR_POS; (rule)->cmds[rule_cnt] |= ((conv_ctoi (val)) & 0xff) << 16
20444 #define GET_P0_CONV(rule) INCR_POS; rule_buf[rule_pos] = conv_itoc (((rule)->cmds[rule_cnt] >> 8) & 0xff)
20445 #define GET_P1_CONV(rule) INCR_POS; rule_buf[rule_pos] = conv_itoc (((rule)->cmds[rule_cnt] >> 16) & 0xff)
20446
20447 int cpu_rule_to_kernel_rule (char *rule_buf, uint rule_len, kernel_rule_t *rule)
20448 {
20449 uint rule_pos;
20450 uint rule_cnt;
20451
20452 for (rule_pos = 0, rule_cnt = 0; rule_pos < rule_len && rule_cnt < MAX_KERNEL_RULES; rule_pos++, rule_cnt++)
20453 {
20454 switch (rule_buf[rule_pos])
20455 {
20456 case ' ':
20457 rule_cnt--;
20458 break;
20459
20460 case RULE_OP_MANGLE_NOOP:
20461 SET_NAME (rule, rule_buf[rule_pos]);
20462 break;
20463
20464 case RULE_OP_MANGLE_LREST:
20465 SET_NAME (rule, rule_buf[rule_pos]);
20466 break;
20467
20468 case RULE_OP_MANGLE_UREST:
20469 SET_NAME (rule, rule_buf[rule_pos]);
20470 break;
20471
20472 case RULE_OP_MANGLE_LREST_UFIRST:
20473 SET_NAME (rule, rule_buf[rule_pos]);
20474 break;
20475
20476 case RULE_OP_MANGLE_UREST_LFIRST:
20477 SET_NAME (rule, rule_buf[rule_pos]);
20478 break;
20479
20480 case RULE_OP_MANGLE_TREST:
20481 SET_NAME (rule, rule_buf[rule_pos]);
20482 break;
20483
20484 case RULE_OP_MANGLE_TOGGLE_AT:
20485 SET_NAME (rule, rule_buf[rule_pos]);
20486 SET_P0_CONV (rule, rule_buf[rule_pos]);
20487 break;
20488
20489 case RULE_OP_MANGLE_REVERSE:
20490 SET_NAME (rule, rule_buf[rule_pos]);
20491 break;
20492
20493 case RULE_OP_MANGLE_DUPEWORD:
20494 SET_NAME (rule, rule_buf[rule_pos]);
20495 break;
20496
20497 case RULE_OP_MANGLE_DUPEWORD_TIMES:
20498 SET_NAME (rule, rule_buf[rule_pos]);
20499 SET_P0_CONV (rule, rule_buf[rule_pos]);
20500 break;
20501
20502 case RULE_OP_MANGLE_REFLECT:
20503 SET_NAME (rule, rule_buf[rule_pos]);
20504 break;
20505
20506 case RULE_OP_MANGLE_ROTATE_LEFT:
20507 SET_NAME (rule, rule_buf[rule_pos]);
20508 break;
20509
20510 case RULE_OP_MANGLE_ROTATE_RIGHT:
20511 SET_NAME (rule, rule_buf[rule_pos]);
20512 break;
20513
20514 case RULE_OP_MANGLE_APPEND:
20515 SET_NAME (rule, rule_buf[rule_pos]);
20516 SET_P0 (rule, rule_buf[rule_pos]);
20517 break;
20518
20519 case RULE_OP_MANGLE_PREPEND:
20520 SET_NAME (rule, rule_buf[rule_pos]);
20521 SET_P0 (rule, rule_buf[rule_pos]);
20522 break;
20523
20524 case RULE_OP_MANGLE_DELETE_FIRST:
20525 SET_NAME (rule, rule_buf[rule_pos]);
20526 break;
20527
20528 case RULE_OP_MANGLE_DELETE_LAST:
20529 SET_NAME (rule, rule_buf[rule_pos]);
20530 break;
20531
20532 case RULE_OP_MANGLE_DELETE_AT:
20533 SET_NAME (rule, rule_buf[rule_pos]);
20534 SET_P0_CONV (rule, rule_buf[rule_pos]);
20535 break;
20536
20537 case RULE_OP_MANGLE_EXTRACT:
20538 SET_NAME (rule, rule_buf[rule_pos]);
20539 SET_P0_CONV (rule, rule_buf[rule_pos]);
20540 SET_P1_CONV (rule, rule_buf[rule_pos]);
20541 break;
20542
20543 case RULE_OP_MANGLE_OMIT:
20544 SET_NAME (rule, rule_buf[rule_pos]);
20545 SET_P0_CONV (rule, rule_buf[rule_pos]);
20546 SET_P1_CONV (rule, rule_buf[rule_pos]);
20547 break;
20548
20549 case RULE_OP_MANGLE_INSERT:
20550 SET_NAME (rule, rule_buf[rule_pos]);
20551 SET_P0_CONV (rule, rule_buf[rule_pos]);
20552 SET_P1 (rule, rule_buf[rule_pos]);
20553 break;
20554
20555 case RULE_OP_MANGLE_OVERSTRIKE:
20556 SET_NAME (rule, rule_buf[rule_pos]);
20557 SET_P0_CONV (rule, rule_buf[rule_pos]);
20558 SET_P1 (rule, rule_buf[rule_pos]);
20559 break;
20560
20561 case RULE_OP_MANGLE_TRUNCATE_AT:
20562 SET_NAME (rule, rule_buf[rule_pos]);
20563 SET_P0_CONV (rule, rule_buf[rule_pos]);
20564 break;
20565
20566 case RULE_OP_MANGLE_REPLACE:
20567 SET_NAME (rule, rule_buf[rule_pos]);
20568 SET_P0 (rule, rule_buf[rule_pos]);
20569 SET_P1 (rule, rule_buf[rule_pos]);
20570 break;
20571
20572 case RULE_OP_MANGLE_PURGECHAR:
20573 return (-1);
20574 break;
20575
20576 case RULE_OP_MANGLE_TOGGLECASE_REC:
20577 return (-1);
20578 break;
20579
20580 case RULE_OP_MANGLE_DUPECHAR_FIRST:
20581 SET_NAME (rule, rule_buf[rule_pos]);
20582 SET_P0_CONV (rule, rule_buf[rule_pos]);
20583 break;
20584
20585 case RULE_OP_MANGLE_DUPECHAR_LAST:
20586 SET_NAME (rule, rule_buf[rule_pos]);
20587 SET_P0_CONV (rule, rule_buf[rule_pos]);
20588 break;
20589
20590 case RULE_OP_MANGLE_DUPECHAR_ALL:
20591 SET_NAME (rule, rule_buf[rule_pos]);
20592 break;
20593
20594 case RULE_OP_MANGLE_SWITCH_FIRST:
20595 SET_NAME (rule, rule_buf[rule_pos]);
20596 break;
20597
20598 case RULE_OP_MANGLE_SWITCH_LAST:
20599 SET_NAME (rule, rule_buf[rule_pos]);
20600 break;
20601
20602 case RULE_OP_MANGLE_SWITCH_AT:
20603 SET_NAME (rule, rule_buf[rule_pos]);
20604 SET_P0_CONV (rule, rule_buf[rule_pos]);
20605 SET_P1_CONV (rule, rule_buf[rule_pos]);
20606 break;
20607
20608 case RULE_OP_MANGLE_CHR_SHIFTL:
20609 SET_NAME (rule, rule_buf[rule_pos]);
20610 SET_P0_CONV (rule, rule_buf[rule_pos]);
20611 break;
20612
20613 case RULE_OP_MANGLE_CHR_SHIFTR:
20614 SET_NAME (rule, rule_buf[rule_pos]);
20615 SET_P0_CONV (rule, rule_buf[rule_pos]);
20616 break;
20617
20618 case RULE_OP_MANGLE_CHR_INCR:
20619 SET_NAME (rule, rule_buf[rule_pos]);
20620 SET_P0_CONV (rule, rule_buf[rule_pos]);
20621 break;
20622
20623 case RULE_OP_MANGLE_CHR_DECR:
20624 SET_NAME (rule, rule_buf[rule_pos]);
20625 SET_P0_CONV (rule, rule_buf[rule_pos]);
20626 break;
20627
20628 case RULE_OP_MANGLE_REPLACE_NP1:
20629 SET_NAME (rule, rule_buf[rule_pos]);
20630 SET_P0_CONV (rule, rule_buf[rule_pos]);
20631 break;
20632
20633 case RULE_OP_MANGLE_REPLACE_NM1:
20634 SET_NAME (rule, rule_buf[rule_pos]);
20635 SET_P0_CONV (rule, rule_buf[rule_pos]);
20636 break;
20637
20638 case RULE_OP_MANGLE_DUPEBLOCK_FIRST:
20639 SET_NAME (rule, rule_buf[rule_pos]);
20640 SET_P0_CONV (rule, rule_buf[rule_pos]);
20641 break;
20642
20643 case RULE_OP_MANGLE_DUPEBLOCK_LAST:
20644 SET_NAME (rule, rule_buf[rule_pos]);
20645 SET_P0_CONV (rule, rule_buf[rule_pos]);
20646 break;
20647
20648 case RULE_OP_MANGLE_TITLE:
20649 SET_NAME (rule, rule_buf[rule_pos]);
20650 break;
20651
20652 default:
20653 return (-1);
20654 break;
20655 }
20656 }
20657
20658 if (rule_pos < rule_len) return (-1);
20659
20660 return (0);
20661 }
20662
20663 int kernel_rule_to_cpu_rule (char *rule_buf, kernel_rule_t *rule)
20664 {
20665 uint rule_cnt;
20666 uint rule_pos;
20667 uint rule_len = HCBUFSIZ - 1; // maximum possible len
20668
20669 char rule_cmd;
20670
20671 for (rule_cnt = 0, rule_pos = 0; rule_pos < rule_len && rule_cnt < MAX_KERNEL_RULES; rule_pos++, rule_cnt++)
20672 {
20673 GET_NAME (rule);
20674
20675 if (rule_cnt > 0) rule_buf[rule_pos++] = ' ';
20676
20677 switch (rule_cmd)
20678 {
20679 case RULE_OP_MANGLE_NOOP:
20680 rule_buf[rule_pos] = rule_cmd;
20681 break;
20682
20683 case RULE_OP_MANGLE_LREST:
20684 rule_buf[rule_pos] = rule_cmd;
20685 break;
20686
20687 case RULE_OP_MANGLE_UREST:
20688 rule_buf[rule_pos] = rule_cmd;
20689 break;
20690
20691 case RULE_OP_MANGLE_LREST_UFIRST:
20692 rule_buf[rule_pos] = rule_cmd;
20693 break;
20694
20695 case RULE_OP_MANGLE_UREST_LFIRST:
20696 rule_buf[rule_pos] = rule_cmd;
20697 break;
20698
20699 case RULE_OP_MANGLE_TREST:
20700 rule_buf[rule_pos] = rule_cmd;
20701 break;
20702
20703 case RULE_OP_MANGLE_TOGGLE_AT:
20704 rule_buf[rule_pos] = rule_cmd;
20705 GET_P0_CONV (rule);
20706 break;
20707
20708 case RULE_OP_MANGLE_REVERSE:
20709 rule_buf[rule_pos] = rule_cmd;
20710 break;
20711
20712 case RULE_OP_MANGLE_DUPEWORD:
20713 rule_buf[rule_pos] = rule_cmd;
20714 break;
20715
20716 case RULE_OP_MANGLE_DUPEWORD_TIMES:
20717 rule_buf[rule_pos] = rule_cmd;
20718 GET_P0_CONV (rule);
20719 break;
20720
20721 case RULE_OP_MANGLE_REFLECT:
20722 rule_buf[rule_pos] = rule_cmd;
20723 break;
20724
20725 case RULE_OP_MANGLE_ROTATE_LEFT:
20726 rule_buf[rule_pos] = rule_cmd;
20727 break;
20728
20729 case RULE_OP_MANGLE_ROTATE_RIGHT:
20730 rule_buf[rule_pos] = rule_cmd;
20731 break;
20732
20733 case RULE_OP_MANGLE_APPEND:
20734 rule_buf[rule_pos] = rule_cmd;
20735 GET_P0 (rule);
20736 break;
20737
20738 case RULE_OP_MANGLE_PREPEND:
20739 rule_buf[rule_pos] = rule_cmd;
20740 GET_P0 (rule);
20741 break;
20742
20743 case RULE_OP_MANGLE_DELETE_FIRST:
20744 rule_buf[rule_pos] = rule_cmd;
20745 break;
20746
20747 case RULE_OP_MANGLE_DELETE_LAST:
20748 rule_buf[rule_pos] = rule_cmd;
20749 break;
20750
20751 case RULE_OP_MANGLE_DELETE_AT:
20752 rule_buf[rule_pos] = rule_cmd;
20753 GET_P0_CONV (rule);
20754 break;
20755
20756 case RULE_OP_MANGLE_EXTRACT:
20757 rule_buf[rule_pos] = rule_cmd;
20758 GET_P0_CONV (rule);
20759 GET_P1_CONV (rule);
20760 break;
20761
20762 case RULE_OP_MANGLE_OMIT:
20763 rule_buf[rule_pos] = rule_cmd;
20764 GET_P0_CONV (rule);
20765 GET_P1_CONV (rule);
20766 break;
20767
20768 case RULE_OP_MANGLE_INSERT:
20769 rule_buf[rule_pos] = rule_cmd;
20770 GET_P0_CONV (rule);
20771 GET_P1 (rule);
20772 break;
20773
20774 case RULE_OP_MANGLE_OVERSTRIKE:
20775 rule_buf[rule_pos] = rule_cmd;
20776 GET_P0_CONV (rule);
20777 GET_P1 (rule);
20778 break;
20779
20780 case RULE_OP_MANGLE_TRUNCATE_AT:
20781 rule_buf[rule_pos] = rule_cmd;
20782 GET_P0_CONV (rule);
20783 break;
20784
20785 case RULE_OP_MANGLE_REPLACE:
20786 rule_buf[rule_pos] = rule_cmd;
20787 GET_P0 (rule);
20788 GET_P1 (rule);
20789 break;
20790
20791 case RULE_OP_MANGLE_PURGECHAR:
20792 return (-1);
20793 break;
20794
20795 case RULE_OP_MANGLE_TOGGLECASE_REC:
20796 return (-1);
20797 break;
20798
20799 case RULE_OP_MANGLE_DUPECHAR_FIRST:
20800 rule_buf[rule_pos] = rule_cmd;
20801 GET_P0_CONV (rule);
20802 break;
20803
20804 case RULE_OP_MANGLE_DUPECHAR_LAST:
20805 rule_buf[rule_pos] = rule_cmd;
20806 GET_P0_CONV (rule);
20807 break;
20808
20809 case RULE_OP_MANGLE_DUPECHAR_ALL:
20810 rule_buf[rule_pos] = rule_cmd;
20811 break;
20812
20813 case RULE_OP_MANGLE_SWITCH_FIRST:
20814 rule_buf[rule_pos] = rule_cmd;
20815 break;
20816
20817 case RULE_OP_MANGLE_SWITCH_LAST:
20818 rule_buf[rule_pos] = rule_cmd;
20819 break;
20820
20821 case RULE_OP_MANGLE_SWITCH_AT:
20822 rule_buf[rule_pos] = rule_cmd;
20823 GET_P0_CONV (rule);
20824 GET_P1_CONV (rule);
20825 break;
20826
20827 case RULE_OP_MANGLE_CHR_SHIFTL:
20828 rule_buf[rule_pos] = rule_cmd;
20829 GET_P0_CONV (rule);
20830 break;
20831
20832 case RULE_OP_MANGLE_CHR_SHIFTR:
20833 rule_buf[rule_pos] = rule_cmd;
20834 GET_P0_CONV (rule);
20835 break;
20836
20837 case RULE_OP_MANGLE_CHR_INCR:
20838 rule_buf[rule_pos] = rule_cmd;
20839 GET_P0_CONV (rule);
20840 break;
20841
20842 case RULE_OP_MANGLE_CHR_DECR:
20843 rule_buf[rule_pos] = rule_cmd;
20844 GET_P0_CONV (rule);
20845 break;
20846
20847 case RULE_OP_MANGLE_REPLACE_NP1:
20848 rule_buf[rule_pos] = rule_cmd;
20849 GET_P0_CONV (rule);
20850 break;
20851
20852 case RULE_OP_MANGLE_REPLACE_NM1:
20853 rule_buf[rule_pos] = rule_cmd;
20854 GET_P0_CONV (rule);
20855 break;
20856
20857 case RULE_OP_MANGLE_DUPEBLOCK_FIRST:
20858 rule_buf[rule_pos] = rule_cmd;
20859 GET_P0_CONV (rule);
20860 break;
20861
20862 case RULE_OP_MANGLE_DUPEBLOCK_LAST:
20863 rule_buf[rule_pos] = rule_cmd;
20864 GET_P0_CONV (rule);
20865 break;
20866
20867 case RULE_OP_MANGLE_TITLE:
20868 rule_buf[rule_pos] = rule_cmd;
20869 break;
20870
20871 case 0:
20872 return rule_pos - 1;
20873 break;
20874
20875 default:
20876 return (-1);
20877 break;
20878 }
20879 }
20880
20881 if (rule_cnt > 0)
20882 {
20883 return rule_pos;
20884 }
20885
20886 return (-1);
20887 }
20888
20889 /**
20890 * CPU rules : this is from hashcat sources, cpu based rules
20891 */
20892
20893 #define NEXT_RULEPOS(rp) if (++(rp) == rule_len) return (RULE_RC_SYNTAX_ERROR)
20894 #define NEXT_RPTOI(r,rp,up) if (((up) = conv_ctoi ((r)[(rp)])) == -1) return (RULE_RC_SYNTAX_ERROR)
20895
20896 #define MANGLE_TOGGLE_AT(a,p) if (class_alpha ((a)[(p)])) (a)[(p)] ^= 0x20
20897 #define MANGLE_LOWER_AT(a,p) if (class_upper ((a)[(p)])) (a)[(p)] ^= 0x20
20898 #define MANGLE_UPPER_AT(a,p) if (class_lower ((a)[(p)])) (a)[(p)] ^= 0x20
20899
20900 /* #define MANGLE_SWITCH(a,l,r) { char c = (l); arr[(r)] = arr[(l)]; arr[(l)] = c; } */
20901 /* #define MANGLE_SWITCH(a,l,r) { char c = (l); (a)[(r)] = (a)[(l)]; (a)[(l)] = c; } */
20902 #define MANGLE_SWITCH(a,l,r) { char c = (a)[(r)]; (a)[(r)] = (a)[(l)]; (a)[(l)] = c; }
20903
20904 int mangle_lrest (char arr[BLOCK_SIZE], int arr_len)
20905 {
20906 int pos;
20907
20908 for (pos = 0; pos < arr_len; pos++) MANGLE_LOWER_AT (arr, pos);
20909
20910 return (arr_len);
20911 }
20912
20913 int mangle_urest (char arr[BLOCK_SIZE], int arr_len)
20914 {
20915 int pos;
20916
20917 for (pos = 0; pos < arr_len; pos++) MANGLE_UPPER_AT (arr, pos);
20918
20919 return (arr_len);
20920 }
20921
20922 int mangle_trest (char arr[BLOCK_SIZE], int arr_len)
20923 {
20924 int pos;
20925
20926 for (pos = 0; pos < arr_len; pos++) MANGLE_TOGGLE_AT (arr, pos);
20927
20928 return (arr_len);
20929 }
20930
20931 int mangle_reverse (char arr[BLOCK_SIZE], int arr_len)
20932 {
20933 int l;
20934 int r;
20935
20936 for (l = 0; l < arr_len; l++)
20937 {
20938 r = arr_len - 1 - l;
20939
20940 if (l >= r) break;
20941
20942 MANGLE_SWITCH (arr, l, r);
20943 }
20944
20945 return (arr_len);
20946 }
20947
20948 int mangle_double (char arr[BLOCK_SIZE], int arr_len)
20949 {
20950 if ((arr_len * 2) >= BLOCK_SIZE) return (arr_len);
20951
20952 memcpy (&arr[arr_len], arr, (size_t) arr_len);
20953
20954 return (arr_len * 2);
20955 }
20956
20957 int mangle_double_times (char arr[BLOCK_SIZE], int arr_len, int times)
20958 {
20959 if (((arr_len * times) + arr_len) >= BLOCK_SIZE) return (arr_len);
20960
20961 int orig_len = arr_len;
20962
20963 int i;
20964
20965 for (i = 0; i < times; i++)
20966 {
20967 memcpy (&arr[arr_len], arr, orig_len);
20968
20969 arr_len += orig_len;
20970 }
20971
20972 return (arr_len);
20973 }
20974
20975 int mangle_reflect (char arr[BLOCK_SIZE], int arr_len)
20976 {
20977 if ((arr_len * 2) >= BLOCK_SIZE) return (arr_len);
20978
20979 mangle_double (arr, arr_len);
20980
20981 mangle_reverse (arr + arr_len, arr_len);
20982
20983 return (arr_len * 2);
20984 }
20985
20986 int mangle_rotate_left (char arr[BLOCK_SIZE], int arr_len)
20987 {
20988 int l;
20989 int r;
20990
20991 for (l = 0, r = arr_len - 1; r > 0; r--)
20992 {
20993 MANGLE_SWITCH (arr, l, r);
20994 }
20995
20996 return (arr_len);
20997 }
20998
20999 int mangle_rotate_right (char arr[BLOCK_SIZE], int arr_len)
21000 {
21001 int l;
21002 int r;
21003
21004 for (l = 0, r = arr_len - 1; l < r; l++)
21005 {
21006 MANGLE_SWITCH (arr, l, r);
21007 }
21008
21009 return (arr_len);
21010 }
21011
21012 int mangle_append (char arr[BLOCK_SIZE], int arr_len, char c)
21013 {
21014 if ((arr_len + 1) >= BLOCK_SIZE) return (arr_len);
21015
21016 arr[arr_len] = c;
21017
21018 return (arr_len + 1);
21019 }
21020
21021 int mangle_prepend (char arr[BLOCK_SIZE], int arr_len, char c)
21022 {
21023 if ((arr_len + 1) >= BLOCK_SIZE) return (arr_len);
21024
21025 int arr_pos;
21026
21027 for (arr_pos = arr_len - 1; arr_pos > -1; arr_pos--)
21028 {
21029 arr[arr_pos + 1] = arr[arr_pos];
21030 }
21031
21032 arr[0] = c;
21033
21034 return (arr_len + 1);
21035 }
21036
21037 int mangle_delete_at (char arr[BLOCK_SIZE], int arr_len, int upos)
21038 {
21039 if (upos >= arr_len) return (arr_len);
21040
21041 int arr_pos;
21042
21043 for (arr_pos = upos; arr_pos < arr_len - 1; arr_pos++)
21044 {
21045 arr[arr_pos] = arr[arr_pos + 1];
21046 }
21047
21048 return (arr_len - 1);
21049 }
21050
21051 int mangle_extract (char arr[BLOCK_SIZE], int arr_len, int upos, int ulen)
21052 {
21053 if (upos >= arr_len) return (arr_len);
21054
21055 if ((upos + ulen) > arr_len) return (arr_len);
21056
21057 int arr_pos;
21058
21059 for (arr_pos = 0; arr_pos < ulen; arr_pos++)
21060 {
21061 arr[arr_pos] = arr[upos + arr_pos];
21062 }
21063
21064 return (ulen);
21065 }
21066
21067 int mangle_omit (char arr[BLOCK_SIZE], int arr_len, int upos, int ulen)
21068 {
21069 if (upos >= arr_len) return (arr_len);
21070
21071 if ((upos + ulen) >= arr_len) return (arr_len);
21072
21073 int arr_pos;
21074
21075 for (arr_pos = upos; arr_pos < arr_len - ulen; arr_pos++)
21076 {
21077 arr[arr_pos] = arr[arr_pos + ulen];
21078 }
21079
21080 return (arr_len - ulen);
21081 }
21082
21083 int mangle_insert (char arr[BLOCK_SIZE], int arr_len, int upos, char c)
21084 {
21085 if (upos >= arr_len) return (arr_len);
21086
21087 if ((arr_len + 1) >= BLOCK_SIZE) return (arr_len);
21088
21089 int arr_pos;
21090
21091 for (arr_pos = arr_len - 1; arr_pos > upos - 1; arr_pos--)
21092 {
21093 arr[arr_pos + 1] = arr[arr_pos];
21094 }
21095
21096 arr[upos] = c;
21097
21098 return (arr_len + 1);
21099 }
21100
21101 int mangle_insert_multi (char arr[BLOCK_SIZE], int arr_len, int arr_pos, char arr2[BLOCK_SIZE], int arr2_len, int arr2_pos, int arr2_cpy)
21102 {
21103 if ((arr_len + arr2_cpy) > BLOCK_SIZE) return (RULE_RC_REJECT_ERROR);
21104
21105 if (arr_pos > arr_len) return (RULE_RC_REJECT_ERROR);
21106
21107 if (arr2_pos > arr2_len) return (RULE_RC_REJECT_ERROR);
21108
21109 if ((arr2_pos + arr2_cpy) > arr2_len) return (RULE_RC_REJECT_ERROR);
21110
21111 if (arr2_cpy < 1) return (RULE_RC_SYNTAX_ERROR);
21112
21113 memcpy (arr2, arr2 + arr2_pos, arr2_len - arr2_pos);
21114
21115 memcpy (arr2 + arr2_cpy, arr + arr_pos, arr_len - arr_pos);
21116
21117 memcpy (arr + arr_pos, arr2, arr_len - arr_pos + arr2_cpy);
21118
21119 return (arr_len + arr2_cpy);
21120 }
21121
21122 int mangle_overstrike (char arr[BLOCK_SIZE], int arr_len, int upos, char c)
21123 {
21124 if (upos >= arr_len) return (arr_len);
21125
21126 arr[upos] = c;
21127
21128 return (arr_len);
21129 }
21130
21131 int mangle_truncate_at (char arr[BLOCK_SIZE], int arr_len, int upos)
21132 {
21133 if (upos >= arr_len) return (arr_len);
21134
21135 memset (arr + upos, 0, arr_len - upos);
21136
21137 return (upos);
21138 }
21139
21140 int mangle_replace (char arr[BLOCK_SIZE], int arr_len, char oldc, char newc)
21141 {
21142 int arr_pos;
21143
21144 for (arr_pos = 0; arr_pos < arr_len; arr_pos++)
21145 {
21146 if (arr[arr_pos] != oldc) continue;
21147
21148 arr[arr_pos] = newc;
21149 }
21150
21151 return (arr_len);
21152 }
21153
21154 int mangle_purgechar (char arr[BLOCK_SIZE], int arr_len, char c)
21155 {
21156 int arr_pos;
21157
21158 int ret_len;
21159
21160 for (ret_len = 0, arr_pos = 0; arr_pos < arr_len; arr_pos++)
21161 {
21162 if (arr[arr_pos] == c) continue;
21163
21164 arr[ret_len] = arr[arr_pos];
21165
21166 ret_len++;
21167 }
21168
21169 return (ret_len);
21170 }
21171
21172 int mangle_dupeblock_prepend (char arr[BLOCK_SIZE], int arr_len, int ulen)
21173 {
21174 if (ulen > arr_len) return (arr_len);
21175
21176 if ((arr_len + ulen) >= BLOCK_SIZE) return (arr_len);
21177
21178 char cs[100] = { 0 };
21179
21180 memcpy (cs, arr, ulen);
21181
21182 int i;
21183
21184 for (i = 0; i < ulen; i++)
21185 {
21186 char c = cs[i];
21187
21188 arr_len = mangle_insert (arr, arr_len, i, c);
21189 }
21190
21191 return (arr_len);
21192 }
21193
21194 int mangle_dupeblock_append (char arr[BLOCK_SIZE], int arr_len, int ulen)
21195 {
21196 if (ulen > arr_len) return (arr_len);
21197
21198 if ((arr_len + ulen) >= BLOCK_SIZE) return (arr_len);
21199
21200 int upos = arr_len - ulen;
21201
21202 int i;
21203
21204 for (i = 0; i < ulen; i++)
21205 {
21206 char c = arr[upos + i];
21207
21208 arr_len = mangle_append (arr, arr_len, c);
21209 }
21210
21211 return (arr_len);
21212 }
21213
21214 int mangle_dupechar_at (char arr[BLOCK_SIZE], int arr_len, int upos, int ulen)
21215 {
21216 if ( arr_len == 0) return (arr_len);
21217 if ((arr_len + ulen) >= BLOCK_SIZE) return (arr_len);
21218
21219 char c = arr[upos];
21220
21221 int i;
21222
21223 for (i = 0; i < ulen; i++)
21224 {
21225 arr_len = mangle_insert (arr, arr_len, upos, c);
21226 }
21227
21228 return (arr_len);
21229 }
21230
21231 int mangle_dupechar (char arr[BLOCK_SIZE], int arr_len)
21232 {
21233 if ( arr_len == 0) return (arr_len);
21234 if ((arr_len + arr_len) >= BLOCK_SIZE) return (arr_len);
21235
21236 int arr_pos;
21237
21238 for (arr_pos = arr_len - 1; arr_pos > -1; arr_pos--)
21239 {
21240 int new_pos = arr_pos * 2;
21241
21242 arr[new_pos] = arr[arr_pos];
21243
21244 arr[new_pos + 1] = arr[arr_pos];
21245 }
21246
21247 return (arr_len * 2);
21248 }
21249
21250 int mangle_switch_at_check (char arr[BLOCK_SIZE], int arr_len, int upos, int upos2)
21251 {
21252 if (upos >= arr_len) return (arr_len);
21253 if (upos2 >= arr_len) return (arr_len);
21254
21255 MANGLE_SWITCH (arr, upos, upos2);
21256
21257 return (arr_len);
21258 }
21259
21260 int mangle_switch_at (char arr[BLOCK_SIZE], int arr_len, int upos, int upos2)
21261 {
21262 MANGLE_SWITCH (arr, upos, upos2);
21263
21264 return (arr_len);
21265 }
21266
21267 int mangle_chr_shiftl (char arr[BLOCK_SIZE], int arr_len, int upos)
21268 {
21269 if (upos >= arr_len) return (arr_len);
21270
21271 arr[upos] <<= 1;
21272
21273 return (arr_len);
21274 }
21275
21276 int mangle_chr_shiftr (char arr[BLOCK_SIZE], int arr_len, int upos)
21277 {
21278 if (upos >= arr_len) return (arr_len);
21279
21280 arr[upos] >>= 1;
21281
21282 return (arr_len);
21283 }
21284
21285 int mangle_chr_incr (char arr[BLOCK_SIZE], int arr_len, int upos)
21286 {
21287 if (upos >= arr_len) return (arr_len);
21288
21289 arr[upos] += 1;
21290
21291 return (arr_len);
21292 }
21293
21294 int mangle_chr_decr (char arr[BLOCK_SIZE], int arr_len, int upos)
21295 {
21296 if (upos >= arr_len) return (arr_len);
21297
21298 arr[upos] -= 1;
21299
21300 return (arr_len);
21301 }
21302
21303 int mangle_title (char arr[BLOCK_SIZE], int arr_len)
21304 {
21305 int upper_next = 1;
21306
21307 int pos;
21308
21309 for (pos = 0; pos < arr_len; pos++)
21310 {
21311 if (arr[pos] == ' ')
21312 {
21313 upper_next = 1;
21314
21315 continue;
21316 }
21317
21318 if (upper_next)
21319 {
21320 upper_next = 0;
21321
21322 MANGLE_UPPER_AT (arr, pos);
21323 }
21324 else
21325 {
21326 MANGLE_LOWER_AT (arr, pos);
21327 }
21328 }
21329
21330 return (arr_len);
21331 }
21332
21333 int generate_random_rule (char rule_buf[RP_RULE_BUFSIZ], u32 rp_gen_func_min, u32 rp_gen_func_max)
21334 {
21335 u32 rp_gen_num = get_random_num (rp_gen_func_min, rp_gen_func_max);
21336
21337 u32 j;
21338
21339 u32 rule_pos = 0;
21340
21341 for (j = 0; j < rp_gen_num; j++)
21342 {
21343 u32 r = 0;
21344 u32 p1 = 0;
21345 u32 p2 = 0;
21346 u32 p3 = 0;
21347
21348 switch ((char) get_random_num (0, 9))
21349 {
21350 case 0:
21351 r = get_random_num (0, sizeof (grp_op_nop));
21352 rule_buf[rule_pos++] = grp_op_nop[r];
21353 break;
21354
21355 case 1:
21356 r = get_random_num (0, sizeof (grp_op_pos_p0));
21357 rule_buf[rule_pos++] = grp_op_pos_p0[r];
21358 p1 = get_random_num (0, sizeof (grp_pos));
21359 rule_buf[rule_pos++] = grp_pos[p1];
21360 break;
21361
21362 case 2:
21363 r = get_random_num (0, sizeof (grp_op_pos_p1));
21364 rule_buf[rule_pos++] = grp_op_pos_p1[r];
21365 p1 = get_random_num (1, 6);
21366 rule_buf[rule_pos++] = grp_pos[p1];
21367 break;
21368
21369 case 3:
21370 r = get_random_num (0, sizeof (grp_op_chr));
21371 rule_buf[rule_pos++] = grp_op_chr[r];
21372 p1 = get_random_num (0x20, 0x7e);
21373 rule_buf[rule_pos++] = (char) p1;
21374 break;
21375
21376 case 4:
21377 r = get_random_num (0, sizeof (grp_op_chr_chr));
21378 rule_buf[rule_pos++] = grp_op_chr_chr[r];
21379 p1 = get_random_num (0x20, 0x7e);
21380 rule_buf[rule_pos++] = (char) p1;
21381 p2 = get_random_num (0x20, 0x7e);
21382 while (p1 == p2)
21383 p2 = get_random_num (0x20, 0x7e);
21384 rule_buf[rule_pos++] = (char) p2;
21385 break;
21386
21387 case 5:
21388 r = get_random_num (0, sizeof (grp_op_pos_chr));
21389 rule_buf[rule_pos++] = grp_op_pos_chr[r];
21390 p1 = get_random_num (0, sizeof (grp_pos));
21391 rule_buf[rule_pos++] = grp_pos[p1];
21392 p2 = get_random_num (0x20, 0x7e);
21393 rule_buf[rule_pos++] = (char) p2;
21394 break;
21395
21396 case 6:
21397 r = get_random_num (0, sizeof (grp_op_pos_pos0));
21398 rule_buf[rule_pos++] = grp_op_pos_pos0[r];
21399 p1 = get_random_num (0, sizeof (grp_pos));
21400 rule_buf[rule_pos++] = grp_pos[p1];
21401 p2 = get_random_num (0, sizeof (grp_pos));
21402 while (p1 == p2)
21403 p2 = get_random_num (0, sizeof (grp_pos));
21404 rule_buf[rule_pos++] = grp_pos[p2];
21405 break;
21406
21407 case 7:
21408 r = get_random_num (0, sizeof (grp_op_pos_pos1));
21409 rule_buf[rule_pos++] = grp_op_pos_pos1[r];
21410 p1 = get_random_num (0, sizeof (grp_pos));
21411 rule_buf[rule_pos++] = grp_pos[p1];
21412 p2 = get_random_num (1, sizeof (grp_pos));
21413 while (p1 == p2)
21414 p2 = get_random_num (1, sizeof (grp_pos));
21415 rule_buf[rule_pos++] = grp_pos[p2];
21416 break;
21417
21418 case 8:
21419 r = get_random_num (0, sizeof (grp_op_pos1_pos2_pos3));
21420 rule_buf[rule_pos++] = grp_op_pos1_pos2_pos3[r];
21421 p1 = get_random_num (0, sizeof (grp_pos));
21422 rule_buf[rule_pos++] = grp_pos[p1];
21423 p2 = get_random_num (1, sizeof (grp_pos));
21424 rule_buf[rule_pos++] = grp_pos[p1];
21425 p3 = get_random_num (0, sizeof (grp_pos));
21426 rule_buf[rule_pos++] = grp_pos[p3];
21427 break;
21428 }
21429 }
21430
21431 return (rule_pos);
21432 }
21433
21434 int _old_apply_rule (char *rule, int rule_len, char in[BLOCK_SIZE], int in_len, char out[BLOCK_SIZE])
21435 {
21436 char mem[BLOCK_SIZE] = { 0 };
21437
21438 if (in == NULL) return (RULE_RC_REJECT_ERROR);
21439
21440 if (out == NULL) return (RULE_RC_REJECT_ERROR);
21441
21442 if (in_len < 1 || in_len > BLOCK_SIZE) return (RULE_RC_REJECT_ERROR);
21443
21444 if (rule_len < 1) return (RULE_RC_REJECT_ERROR);
21445
21446 int out_len = in_len;
21447 int mem_len = in_len;
21448
21449 memcpy (out, in, out_len);
21450
21451 int rule_pos;
21452
21453 for (rule_pos = 0; rule_pos < rule_len; rule_pos++)
21454 {
21455 int upos, upos2;
21456 int ulen;
21457
21458 switch (rule[rule_pos])
21459 {
21460 case ' ':
21461 break;
21462
21463 case RULE_OP_MANGLE_NOOP:
21464 break;
21465
21466 case RULE_OP_MANGLE_LREST:
21467 out_len = mangle_lrest (out, out_len);
21468 break;
21469
21470 case RULE_OP_MANGLE_UREST:
21471 out_len = mangle_urest (out, out_len);
21472 break;
21473
21474 case RULE_OP_MANGLE_LREST_UFIRST:
21475 out_len = mangle_lrest (out, out_len);
21476 if (out_len) MANGLE_UPPER_AT (out, 0);
21477 break;
21478
21479 case RULE_OP_MANGLE_UREST_LFIRST:
21480 out_len = mangle_urest (out, out_len);
21481 if (out_len) MANGLE_LOWER_AT (out, 0);
21482 break;
21483
21484 case RULE_OP_MANGLE_TREST:
21485 out_len = mangle_trest (out, out_len);
21486 break;
21487
21488 case RULE_OP_MANGLE_TOGGLE_AT:
21489 NEXT_RULEPOS (rule_pos);
21490 NEXT_RPTOI (rule, rule_pos, upos);
21491 if (upos < out_len) MANGLE_TOGGLE_AT (out, upos);
21492 break;
21493
21494 case RULE_OP_MANGLE_REVERSE:
21495 out_len = mangle_reverse (out, out_len);
21496 break;
21497
21498 case RULE_OP_MANGLE_DUPEWORD:
21499 out_len = mangle_double (out, out_len);
21500 break;
21501
21502 case RULE_OP_MANGLE_DUPEWORD_TIMES:
21503 NEXT_RULEPOS (rule_pos);
21504 NEXT_RPTOI (rule, rule_pos, ulen);
21505 out_len = mangle_double_times (out, out_len, ulen);
21506 break;
21507
21508 case RULE_OP_MANGLE_REFLECT:
21509 out_len = mangle_reflect (out, out_len);
21510 break;
21511
21512 case RULE_OP_MANGLE_ROTATE_LEFT:
21513 mangle_rotate_left (out, out_len);
21514 break;
21515
21516 case RULE_OP_MANGLE_ROTATE_RIGHT:
21517 mangle_rotate_right (out, out_len);
21518 break;
21519
21520 case RULE_OP_MANGLE_APPEND:
21521 NEXT_RULEPOS (rule_pos);
21522 out_len = mangle_append (out, out_len, rule[rule_pos]);
21523 break;
21524
21525 case RULE_OP_MANGLE_PREPEND:
21526 NEXT_RULEPOS (rule_pos);
21527 out_len = mangle_prepend (out, out_len, rule[rule_pos]);
21528 break;
21529
21530 case RULE_OP_MANGLE_DELETE_FIRST:
21531 out_len = mangle_delete_at (out, out_len, 0);
21532 break;
21533
21534 case RULE_OP_MANGLE_DELETE_LAST:
21535 out_len = mangle_delete_at (out, out_len, (out_len) ? out_len - 1 : 0);
21536 break;
21537
21538 case RULE_OP_MANGLE_DELETE_AT:
21539 NEXT_RULEPOS (rule_pos);
21540 NEXT_RPTOI (rule, rule_pos, upos);
21541 out_len = mangle_delete_at (out, out_len, upos);
21542 break;
21543
21544 case RULE_OP_MANGLE_EXTRACT:
21545 NEXT_RULEPOS (rule_pos);
21546 NEXT_RPTOI (rule, rule_pos, upos);
21547 NEXT_RULEPOS (rule_pos);
21548 NEXT_RPTOI (rule, rule_pos, ulen);
21549 out_len = mangle_extract (out, out_len, upos, ulen);
21550 break;
21551
21552 case RULE_OP_MANGLE_OMIT:
21553 NEXT_RULEPOS (rule_pos);
21554 NEXT_RPTOI (rule, rule_pos, upos);
21555 NEXT_RULEPOS (rule_pos);
21556 NEXT_RPTOI (rule, rule_pos, ulen);
21557 out_len = mangle_omit (out, out_len, upos, ulen);
21558 break;
21559
21560 case RULE_OP_MANGLE_INSERT:
21561 NEXT_RULEPOS (rule_pos);
21562 NEXT_RPTOI (rule, rule_pos, upos);
21563 NEXT_RULEPOS (rule_pos);
21564 out_len = mangle_insert (out, out_len, upos, rule[rule_pos]);
21565 break;
21566
21567 case RULE_OP_MANGLE_OVERSTRIKE:
21568 NEXT_RULEPOS (rule_pos);
21569 NEXT_RPTOI (rule, rule_pos, upos);
21570 NEXT_RULEPOS (rule_pos);
21571 out_len = mangle_overstrike (out, out_len, upos, rule[rule_pos]);
21572 break;
21573
21574 case RULE_OP_MANGLE_TRUNCATE_AT:
21575 NEXT_RULEPOS (rule_pos);
21576 NEXT_RPTOI (rule, rule_pos, upos);
21577 out_len = mangle_truncate_at (out, out_len, upos);
21578 break;
21579
21580 case RULE_OP_MANGLE_REPLACE:
21581 NEXT_RULEPOS (rule_pos);
21582 NEXT_RULEPOS (rule_pos);
21583 out_len = mangle_replace (out, out_len, rule[rule_pos - 1], rule[rule_pos]);
21584 break;
21585
21586 case RULE_OP_MANGLE_PURGECHAR:
21587 NEXT_RULEPOS (rule_pos);
21588 out_len = mangle_purgechar (out, out_len, rule[rule_pos]);
21589 break;
21590
21591 case RULE_OP_MANGLE_TOGGLECASE_REC:
21592 /* todo */
21593 break;
21594
21595 case RULE_OP_MANGLE_DUPECHAR_FIRST:
21596 NEXT_RULEPOS (rule_pos);
21597 NEXT_RPTOI (rule, rule_pos, ulen);
21598 out_len = mangle_dupechar_at (out, out_len, 0, ulen);
21599 break;
21600
21601 case RULE_OP_MANGLE_DUPECHAR_LAST:
21602 NEXT_RULEPOS (rule_pos);
21603 NEXT_RPTOI (rule, rule_pos, ulen);
21604 out_len = mangle_dupechar_at (out, out_len, out_len - 1, ulen);
21605 break;
21606
21607 case RULE_OP_MANGLE_DUPECHAR_ALL:
21608 out_len = mangle_dupechar (out, out_len);
21609 break;
21610
21611 case RULE_OP_MANGLE_DUPEBLOCK_FIRST:
21612 NEXT_RULEPOS (rule_pos);
21613 NEXT_RPTOI (rule, rule_pos, ulen);
21614 out_len = mangle_dupeblock_prepend (out, out_len, ulen);
21615 break;
21616
21617 case RULE_OP_MANGLE_DUPEBLOCK_LAST:
21618 NEXT_RULEPOS (rule_pos);
21619 NEXT_RPTOI (rule, rule_pos, ulen);
21620 out_len = mangle_dupeblock_append (out, out_len, ulen);
21621 break;
21622
21623 case RULE_OP_MANGLE_SWITCH_FIRST:
21624 if (out_len >= 2) mangle_switch_at (out, out_len, 0, 1);
21625 break;
21626
21627 case RULE_OP_MANGLE_SWITCH_LAST:
21628 if (out_len >= 2) mangle_switch_at (out, out_len, out_len - 1, out_len - 2);
21629 break;
21630
21631 case RULE_OP_MANGLE_SWITCH_AT:
21632 NEXT_RULEPOS (rule_pos);
21633 NEXT_RPTOI (rule, rule_pos, upos);
21634 NEXT_RULEPOS (rule_pos);
21635 NEXT_RPTOI (rule, rule_pos, upos2);
21636 out_len = mangle_switch_at_check (out, out_len, upos, upos2);
21637 break;
21638
21639 case RULE_OP_MANGLE_CHR_SHIFTL:
21640 NEXT_RULEPOS (rule_pos);
21641 NEXT_RPTOI (rule, rule_pos, upos);
21642 mangle_chr_shiftl (out, out_len, upos);
21643 break;
21644
21645 case RULE_OP_MANGLE_CHR_SHIFTR:
21646 NEXT_RULEPOS (rule_pos);
21647 NEXT_RPTOI (rule, rule_pos, upos);
21648 mangle_chr_shiftr (out, out_len, upos);
21649 break;
21650
21651 case RULE_OP_MANGLE_CHR_INCR:
21652 NEXT_RULEPOS (rule_pos);
21653 NEXT_RPTOI (rule, rule_pos, upos);
21654 mangle_chr_incr (out, out_len, upos);
21655 break;
21656
21657 case RULE_OP_MANGLE_CHR_DECR:
21658 NEXT_RULEPOS (rule_pos);
21659 NEXT_RPTOI (rule, rule_pos, upos);
21660 mangle_chr_decr (out, out_len, upos);
21661 break;
21662
21663 case RULE_OP_MANGLE_REPLACE_NP1:
21664 NEXT_RULEPOS (rule_pos);
21665 NEXT_RPTOI (rule, rule_pos, upos);
21666 if ((upos >= 0) && ((upos + 1) < out_len)) mangle_overstrike (out, out_len, upos, out[upos + 1]);
21667 break;
21668
21669 case RULE_OP_MANGLE_REPLACE_NM1:
21670 NEXT_RULEPOS (rule_pos);
21671 NEXT_RPTOI (rule, rule_pos, upos);
21672 if ((upos >= 1) && ((upos + 0) < out_len)) mangle_overstrike (out, out_len, upos, out[upos - 1]);
21673 break;
21674
21675 case RULE_OP_MANGLE_TITLE:
21676 out_len = mangle_title (out, out_len);
21677 break;
21678
21679 case RULE_OP_MANGLE_EXTRACT_MEMORY:
21680 if (mem_len < 1) return (RULE_RC_REJECT_ERROR);
21681 NEXT_RULEPOS (rule_pos);
21682 NEXT_RPTOI (rule, rule_pos, upos);
21683 NEXT_RULEPOS (rule_pos);
21684 NEXT_RPTOI (rule, rule_pos, ulen);
21685 NEXT_RULEPOS (rule_pos);
21686 NEXT_RPTOI (rule, rule_pos, upos2);
21687 if ((out_len = mangle_insert_multi (out, out_len, upos2, mem, mem_len, upos, ulen)) < 1) return (out_len);
21688 break;
21689
21690 case RULE_OP_MANGLE_APPEND_MEMORY:
21691 if (mem_len < 1) return (RULE_RC_REJECT_ERROR);
21692 if ((out_len + mem_len) > BLOCK_SIZE) return (RULE_RC_REJECT_ERROR);
21693 memcpy (out + out_len, mem, mem_len);
21694 out_len += mem_len;
21695 break;
21696
21697 case RULE_OP_MANGLE_PREPEND_MEMORY:
21698 if (mem_len < 1) return (RULE_RC_REJECT_ERROR);
21699 if ((mem_len + out_len) > BLOCK_SIZE) return (RULE_RC_REJECT_ERROR);
21700 memcpy (mem + mem_len, out, out_len);
21701 out_len += mem_len;
21702 memcpy (out, mem, out_len);
21703 break;
21704
21705 case RULE_OP_MEMORIZE_WORD:
21706 memcpy (mem, out, out_len);
21707 mem_len = out_len;
21708 break;
21709
21710 case RULE_OP_REJECT_LESS:
21711 NEXT_RULEPOS (rule_pos);
21712 NEXT_RPTOI (rule, rule_pos, upos);
21713 if (out_len > upos) return (RULE_RC_REJECT_ERROR);
21714 break;
21715
21716 case RULE_OP_REJECT_GREATER:
21717 NEXT_RULEPOS (rule_pos);
21718 NEXT_RPTOI (rule, rule_pos, upos);
21719 if (out_len < upos) return (RULE_RC_REJECT_ERROR);
21720 break;
21721
21722 case RULE_OP_REJECT_CONTAIN:
21723 NEXT_RULEPOS (rule_pos);
21724 if (strchr (out, rule[rule_pos]) != NULL) return (RULE_RC_REJECT_ERROR);
21725 break;
21726
21727 case RULE_OP_REJECT_NOT_CONTAIN:
21728 NEXT_RULEPOS (rule_pos);
21729 if (strchr (out, rule[rule_pos]) == NULL) return (RULE_RC_REJECT_ERROR);
21730 break;
21731
21732 case RULE_OP_REJECT_EQUAL_FIRST:
21733 NEXT_RULEPOS (rule_pos);
21734 if (out[0] != rule[rule_pos]) return (RULE_RC_REJECT_ERROR);
21735 break;
21736
21737 case RULE_OP_REJECT_EQUAL_LAST:
21738 NEXT_RULEPOS (rule_pos);
21739 if (out[out_len - 1] != rule[rule_pos]) return (RULE_RC_REJECT_ERROR);
21740 break;
21741
21742 case RULE_OP_REJECT_EQUAL_AT:
21743 NEXT_RULEPOS (rule_pos);
21744 NEXT_RPTOI (rule, rule_pos, upos);
21745 if ((upos + 1) > out_len) return (RULE_RC_REJECT_ERROR);
21746 NEXT_RULEPOS (rule_pos);
21747 if (out[upos] != rule[rule_pos]) return (RULE_RC_REJECT_ERROR);
21748 break;
21749
21750 case RULE_OP_REJECT_CONTAINS:
21751 NEXT_RULEPOS (rule_pos);
21752 NEXT_RPTOI (rule, rule_pos, upos);
21753 if ((upos + 1) > out_len) return (RULE_RC_REJECT_ERROR);
21754 NEXT_RULEPOS (rule_pos);
21755 int c; int cnt; for (c = 0, cnt = 0; c < out_len; c++) if (out[c] == rule[rule_pos]) cnt++;
21756 if (cnt < upos) return (RULE_RC_REJECT_ERROR);
21757 break;
21758
21759 case RULE_OP_REJECT_MEMORY:
21760 if ((out_len == mem_len) && (memcmp (out, mem, out_len) == 0)) return (RULE_RC_REJECT_ERROR);
21761 break;
21762
21763 default:
21764 return (RULE_RC_SYNTAX_ERROR);
21765 break;
21766 }
21767 }
21768
21769 memset (out + out_len, 0, BLOCK_SIZE - out_len);
21770
21771 return (out_len);
21772 }