More VeraCrypt stuff; SHA256 kernels missing and --veracrypt-pim missing
[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 case 13711: return ((char *) HT_13711); break;
5861 case 13712: return ((char *) HT_13712); break;
5862 case 13713: return ((char *) HT_13713); break;
5863 case 13721: return ((char *) HT_13721); break;
5864 case 13722: return ((char *) HT_13722); break;
5865 case 13723: return ((char *) HT_13723); break;
5866 case 13731: return ((char *) HT_13731); break;
5867 case 13732: return ((char *) HT_13732); break;
5868 case 13733: return ((char *) HT_13733); break;
5869 case 13741: return ((char *) HT_13741); break;
5870 case 13742: return ((char *) HT_13742); break;
5871 case 13743: return ((char *) HT_13743); break;
5872 case 13751: return ((char *) HT_13751); break;
5873 case 13752: return ((char *) HT_13752); break;
5874 case 13753: return ((char *) HT_13753); break;
5875 case 13761: return ((char *) HT_13761); break;
5876 case 13762: return ((char *) HT_13762); break;
5877 case 13763: return ((char *) HT_13763); break;
5878 }
5879
5880 return ((char *) "Unknown");
5881 }
5882
5883 char *strstatus (const uint devices_status)
5884 {
5885 switch (devices_status)
5886 {
5887 case STATUS_INIT: return ((char *) ST_0000); break;
5888 case STATUS_STARTING: return ((char *) ST_0001); break;
5889 case STATUS_RUNNING: return ((char *) ST_0002); break;
5890 case STATUS_PAUSED: return ((char *) ST_0003); break;
5891 case STATUS_EXHAUSTED: return ((char *) ST_0004); break;
5892 case STATUS_CRACKED: return ((char *) ST_0005); break;
5893 case STATUS_ABORTED: return ((char *) ST_0006); break;
5894 case STATUS_QUIT: return ((char *) ST_0007); break;
5895 case STATUS_BYPASS: return ((char *) ST_0008); break;
5896 case STATUS_STOP_AT_CHECKPOINT: return ((char *) ST_0009); break;
5897 case STATUS_AUTOTUNE: return ((char *) ST_0010); break;
5898 }
5899
5900 return ((char *) "Unknown");
5901 }
5902
5903 void ascii_digest (char *out_buf, uint salt_pos, uint digest_pos)
5904 {
5905 uint hash_type = data.hash_type;
5906 uint hash_mode = data.hash_mode;
5907 uint salt_type = data.salt_type;
5908 uint opts_type = data.opts_type;
5909 uint opti_type = data.opti_type;
5910 uint dgst_size = data.dgst_size;
5911
5912 char *hashfile = data.hashfile;
5913
5914 uint len = 4096;
5915
5916 uint digest_buf[64] = { 0 };
5917
5918 u64 *digest_buf64 = (u64 *) digest_buf;
5919
5920 char *digests_buf_ptr = (char *) data.digests_buf;
5921
5922 memcpy (digest_buf, digests_buf_ptr + (data.salts_buf[salt_pos].digests_offset * dgst_size) + (digest_pos * dgst_size), dgst_size);
5923
5924 if (opti_type & OPTI_TYPE_PRECOMPUTE_PERMUT)
5925 {
5926 uint tt;
5927
5928 switch (hash_type)
5929 {
5930 case HASH_TYPE_DESCRYPT:
5931 FP (digest_buf[1], digest_buf[0], tt);
5932 break;
5933
5934 case HASH_TYPE_DESRACF:
5935 digest_buf[0] = rotl32 (digest_buf[0], 29);
5936 digest_buf[1] = rotl32 (digest_buf[1], 29);
5937
5938 FP (digest_buf[1], digest_buf[0], tt);
5939 break;
5940
5941 case HASH_TYPE_LM:
5942 FP (digest_buf[1], digest_buf[0], tt);
5943 break;
5944
5945 case HASH_TYPE_NETNTLM:
5946 digest_buf[0] = rotl32 (digest_buf[0], 29);
5947 digest_buf[1] = rotl32 (digest_buf[1], 29);
5948 digest_buf[2] = rotl32 (digest_buf[2], 29);
5949 digest_buf[3] = rotl32 (digest_buf[3], 29);
5950
5951 FP (digest_buf[1], digest_buf[0], tt);
5952 FP (digest_buf[3], digest_buf[2], tt);
5953 break;
5954
5955 case HASH_TYPE_BSDICRYPT:
5956 digest_buf[0] = rotl32 (digest_buf[0], 31);
5957 digest_buf[1] = rotl32 (digest_buf[1], 31);
5958
5959 FP (digest_buf[1], digest_buf[0], tt);
5960 break;
5961 }
5962 }
5963
5964 if (opti_type & OPTI_TYPE_PRECOMPUTE_MERKLE)
5965 {
5966 switch (hash_type)
5967 {
5968 case HASH_TYPE_MD4:
5969 digest_buf[0] += MD4M_A;
5970 digest_buf[1] += MD4M_B;
5971 digest_buf[2] += MD4M_C;
5972 digest_buf[3] += MD4M_D;
5973 break;
5974
5975 case HASH_TYPE_MD5:
5976 digest_buf[0] += MD5M_A;
5977 digest_buf[1] += MD5M_B;
5978 digest_buf[2] += MD5M_C;
5979 digest_buf[3] += MD5M_D;
5980 break;
5981
5982 case HASH_TYPE_SHA1:
5983 digest_buf[0] += SHA1M_A;
5984 digest_buf[1] += SHA1M_B;
5985 digest_buf[2] += SHA1M_C;
5986 digest_buf[3] += SHA1M_D;
5987 digest_buf[4] += SHA1M_E;
5988 break;
5989
5990 case HASH_TYPE_SHA256:
5991 digest_buf[0] += SHA256M_A;
5992 digest_buf[1] += SHA256M_B;
5993 digest_buf[2] += SHA256M_C;
5994 digest_buf[3] += SHA256M_D;
5995 digest_buf[4] += SHA256M_E;
5996 digest_buf[5] += SHA256M_F;
5997 digest_buf[6] += SHA256M_G;
5998 digest_buf[7] += SHA256M_H;
5999 break;
6000
6001 case HASH_TYPE_SHA384:
6002 digest_buf64[0] += SHA384M_A;
6003 digest_buf64[1] += SHA384M_B;
6004 digest_buf64[2] += SHA384M_C;
6005 digest_buf64[3] += SHA384M_D;
6006 digest_buf64[4] += SHA384M_E;
6007 digest_buf64[5] += SHA384M_F;
6008 digest_buf64[6] += 0;
6009 digest_buf64[7] += 0;
6010 break;
6011
6012 case HASH_TYPE_SHA512:
6013 digest_buf64[0] += SHA512M_A;
6014 digest_buf64[1] += SHA512M_B;
6015 digest_buf64[2] += SHA512M_C;
6016 digest_buf64[3] += SHA512M_D;
6017 digest_buf64[4] += SHA512M_E;
6018 digest_buf64[5] += SHA512M_F;
6019 digest_buf64[6] += SHA512M_G;
6020 digest_buf64[7] += SHA512M_H;
6021 break;
6022 }
6023 }
6024
6025 if (opts_type & OPTS_TYPE_PT_GENERATE_LE)
6026 {
6027 if (dgst_size == DGST_SIZE_4_2)
6028 {
6029 for (int i = 0; i < 2; i++) digest_buf[i] = byte_swap_32 (digest_buf[i]);
6030 }
6031 else if (dgst_size == DGST_SIZE_4_4)
6032 {
6033 for (int i = 0; i < 4; i++) digest_buf[i] = byte_swap_32 (digest_buf[i]);
6034 }
6035 else if (dgst_size == DGST_SIZE_4_5)
6036 {
6037 for (int i = 0; i < 5; i++) digest_buf[i] = byte_swap_32 (digest_buf[i]);
6038 }
6039 else if (dgst_size == DGST_SIZE_4_6)
6040 {
6041 for (int i = 0; i < 6; i++) digest_buf[i] = byte_swap_32 (digest_buf[i]);
6042 }
6043 else if (dgst_size == DGST_SIZE_4_8)
6044 {
6045 for (int i = 0; i < 8; i++) digest_buf[i] = byte_swap_32 (digest_buf[i]);
6046 }
6047 else if ((dgst_size == DGST_SIZE_4_16) || (dgst_size == DGST_SIZE_8_8)) // same size, same result :)
6048 {
6049 if (hash_type == HASH_TYPE_WHIRLPOOL)
6050 {
6051 for (int i = 0; i < 16; i++) digest_buf[i] = byte_swap_32 (digest_buf[i]);
6052 }
6053 else if (hash_type == HASH_TYPE_SHA384)
6054 {
6055 for (int i = 0; i < 8; i++) digest_buf64[i] = byte_swap_64 (digest_buf64[i]);
6056 }
6057 else if (hash_type == HASH_TYPE_SHA512)
6058 {
6059 for (int i = 0; i < 8; i++) digest_buf64[i] = byte_swap_64 (digest_buf64[i]);
6060 }
6061 else if (hash_type == HASH_TYPE_GOST)
6062 {
6063 for (int i = 0; i < 16; i++) digest_buf[i] = byte_swap_32 (digest_buf[i]);
6064 }
6065 }
6066 else if (dgst_size == DGST_SIZE_4_64)
6067 {
6068 for (int i = 0; i < 64; i++) digest_buf[i] = byte_swap_32 (digest_buf[i]);
6069 }
6070 else if (dgst_size == DGST_SIZE_8_25)
6071 {
6072 for (int i = 0; i < 25; i++) digest_buf64[i] = byte_swap_64 (digest_buf64[i]);
6073 }
6074 }
6075
6076 uint isSalted = ((data.salt_type == SALT_TYPE_INTERN)
6077 | (data.salt_type == SALT_TYPE_EXTERN)
6078 | (data.salt_type == SALT_TYPE_EMBEDDED));
6079
6080 salt_t salt;
6081
6082 if (isSalted)
6083 {
6084 memset (&salt, 0, sizeof (salt_t));
6085
6086 memcpy (&salt, &data.salts_buf[salt_pos], sizeof (salt_t));
6087
6088 char *ptr = (char *) salt.salt_buf;
6089
6090 uint len = salt.salt_len;
6091
6092 if (opti_type & OPTI_TYPE_PRECOMPUTE_PERMUT)
6093 {
6094 uint tt;
6095
6096 switch (hash_type)
6097 {
6098 case HASH_TYPE_NETNTLM:
6099
6100 salt.salt_buf[0] = rotr32 (salt.salt_buf[0], 3);
6101 salt.salt_buf[1] = rotr32 (salt.salt_buf[1], 3);
6102
6103 FP (salt.salt_buf[1], salt.salt_buf[0], tt);
6104
6105 break;
6106 }
6107 }
6108
6109 if (opts_type & OPTS_TYPE_ST_UNICODE)
6110 {
6111 for (uint i = 0, j = 0; i < len; i += 1, j += 2)
6112 {
6113 ptr[i] = ptr[j];
6114 }
6115
6116 len = len / 2;
6117 }
6118
6119 if (opts_type & OPTS_TYPE_ST_GENERATE_LE)
6120 {
6121 uint max = salt.salt_len / 4;
6122
6123 if (len % 4) max++;
6124
6125 for (uint i = 0; i < max; i++)
6126 {
6127 salt.salt_buf[i] = byte_swap_32 (salt.salt_buf[i]);
6128 }
6129 }
6130
6131 if (opts_type & OPTS_TYPE_ST_HEX)
6132 {
6133 char tmp[64] = { 0 };
6134
6135 for (uint i = 0, j = 0; i < len; i += 1, j += 2)
6136 {
6137 sprintf (tmp + j, "%02x", (unsigned char) ptr[i]);
6138 }
6139
6140 len = len * 2;
6141
6142 memcpy (ptr, tmp, len);
6143 }
6144
6145 uint memset_size = ((48 - (int) len) > 0) ? (48 - len) : 0;
6146
6147 memset (ptr + len, 0, memset_size);
6148
6149 salt.salt_len = len;
6150 }
6151
6152 //
6153 // some modes require special encoding
6154 //
6155
6156 uint out_buf_plain[256] = { 0 };
6157 uint out_buf_salt[256] = { 0 };
6158
6159 char tmp_buf[1024] = { 0 };
6160
6161 char *ptr_plain = (char *) out_buf_plain;
6162 char *ptr_salt = (char *) out_buf_salt;
6163
6164 if (hash_mode == 22)
6165 {
6166 char username[30] = { 0 };
6167
6168 memcpy (username, salt.salt_buf, salt.salt_len - 22);
6169
6170 char sig[6] = { 'n', 'r', 'c', 's', 't', 'n' };
6171
6172 u16 *ptr = (u16 *) digest_buf;
6173
6174 tmp_buf[ 0] = sig[0];
6175 tmp_buf[ 1] = int_to_base64 (((ptr[1]) >> 12) & 0x3f);
6176 tmp_buf[ 2] = int_to_base64 (((ptr[1]) >> 6) & 0x3f);
6177 tmp_buf[ 3] = int_to_base64 (((ptr[1]) >> 0) & 0x3f);
6178 tmp_buf[ 4] = int_to_base64 (((ptr[0]) >> 12) & 0x3f);
6179 tmp_buf[ 5] = int_to_base64 (((ptr[0]) >> 6) & 0x3f);
6180 tmp_buf[ 6] = sig[1];
6181 tmp_buf[ 7] = int_to_base64 (((ptr[0]) >> 0) & 0x3f);
6182 tmp_buf[ 8] = int_to_base64 (((ptr[3]) >> 12) & 0x3f);
6183 tmp_buf[ 9] = int_to_base64 (((ptr[3]) >> 6) & 0x3f);
6184 tmp_buf[10] = int_to_base64 (((ptr[3]) >> 0) & 0x3f);
6185 tmp_buf[11] = int_to_base64 (((ptr[2]) >> 12) & 0x3f);
6186 tmp_buf[12] = sig[2];
6187 tmp_buf[13] = int_to_base64 (((ptr[2]) >> 6) & 0x3f);
6188 tmp_buf[14] = int_to_base64 (((ptr[2]) >> 0) & 0x3f);
6189 tmp_buf[15] = int_to_base64 (((ptr[5]) >> 12) & 0x3f);
6190 tmp_buf[16] = int_to_base64 (((ptr[5]) >> 6) & 0x3f);
6191 tmp_buf[17] = sig[3];
6192 tmp_buf[18] = int_to_base64 (((ptr[5]) >> 0) & 0x3f);
6193 tmp_buf[19] = int_to_base64 (((ptr[4]) >> 12) & 0x3f);
6194 tmp_buf[20] = int_to_base64 (((ptr[4]) >> 6) & 0x3f);
6195 tmp_buf[21] = int_to_base64 (((ptr[4]) >> 0) & 0x3f);
6196 tmp_buf[22] = int_to_base64 (((ptr[7]) >> 12) & 0x3f);
6197 tmp_buf[23] = sig[4];
6198 tmp_buf[24] = int_to_base64 (((ptr[7]) >> 6) & 0x3f);
6199 tmp_buf[25] = int_to_base64 (((ptr[7]) >> 0) & 0x3f);
6200 tmp_buf[26] = int_to_base64 (((ptr[6]) >> 12) & 0x3f);
6201 tmp_buf[27] = int_to_base64 (((ptr[6]) >> 6) & 0x3f);
6202 tmp_buf[28] = int_to_base64 (((ptr[6]) >> 0) & 0x3f);
6203 tmp_buf[29] = sig[5];
6204
6205 snprintf (out_buf, len-1, "%s:%s",
6206 tmp_buf,
6207 username);
6208 }
6209 else if (hash_mode == 23)
6210 {
6211 // do not show the skyper part in output
6212
6213 char *salt_buf_ptr = (char *) salt.salt_buf;
6214
6215 salt_buf_ptr[salt.salt_len - 8] = 0;
6216
6217 snprintf (out_buf, len-1, "%08x%08x%08x%08x:%s",
6218 digest_buf[0],
6219 digest_buf[1],
6220 digest_buf[2],
6221 digest_buf[3],
6222 salt_buf_ptr);
6223 }
6224 else if (hash_mode == 101)
6225 {
6226 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6227
6228 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6229 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6230 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6231 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6232 digest_buf[4] = byte_swap_32 (digest_buf[4]);
6233
6234 memcpy (tmp_buf, digest_buf, 20);
6235
6236 base64_encode (int_to_base64, (const u8 *) tmp_buf, 20, (u8 *) ptr_plain);
6237
6238 snprintf (out_buf, len-1, "{SHA}%s", ptr_plain);
6239 }
6240 else if (hash_mode == 111)
6241 {
6242 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6243
6244 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6245 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6246 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6247 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6248 digest_buf[4] = byte_swap_32 (digest_buf[4]);
6249
6250 memcpy (tmp_buf, digest_buf, 20);
6251 memcpy (tmp_buf + 20, salt.salt_buf, salt.salt_len);
6252
6253 base64_encode (int_to_base64, (const u8 *) tmp_buf, 20 + salt.salt_len, (u8 *) ptr_plain);
6254
6255 snprintf (out_buf, len-1, "{SSHA}%s", ptr_plain);
6256 }
6257 else if ((hash_mode == 122) || (hash_mode == 125))
6258 {
6259 snprintf (out_buf, len-1, "%s%08x%08x%08x%08x%08x",
6260 (char *) salt.salt_buf,
6261 digest_buf[0],
6262 digest_buf[1],
6263 digest_buf[2],
6264 digest_buf[3],
6265 digest_buf[4]);
6266 }
6267 else if (hash_mode == 124)
6268 {
6269 snprintf (out_buf, len-1, "sha1$%s$%08x%08x%08x%08x%08x",
6270 (char *) salt.salt_buf,
6271 digest_buf[0],
6272 digest_buf[1],
6273 digest_buf[2],
6274 digest_buf[3],
6275 digest_buf[4]);
6276 }
6277 else if (hash_mode == 131)
6278 {
6279 snprintf (out_buf, len-1, "0x0100%s%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
6280 (char *) salt.salt_buf,
6281 0, 0, 0, 0, 0,
6282 digest_buf[0],
6283 digest_buf[1],
6284 digest_buf[2],
6285 digest_buf[3],
6286 digest_buf[4]);
6287 }
6288 else if (hash_mode == 132)
6289 {
6290 snprintf (out_buf, len-1, "0x0100%s%08x%08x%08x%08x%08x",
6291 (char *) salt.salt_buf,
6292 digest_buf[0],
6293 digest_buf[1],
6294 digest_buf[2],
6295 digest_buf[3],
6296 digest_buf[4]);
6297 }
6298 else if (hash_mode == 133)
6299 {
6300 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6301
6302 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6303 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6304 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6305 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6306 digest_buf[4] = byte_swap_32 (digest_buf[4]);
6307
6308 memcpy (tmp_buf, digest_buf, 20);
6309
6310 base64_encode (int_to_base64, (const u8 *) tmp_buf, 20, (u8 *) ptr_plain);
6311
6312 snprintf (out_buf, len-1, "%s", ptr_plain);
6313 }
6314 else if (hash_mode == 141)
6315 {
6316 memcpy (tmp_buf, salt.salt_buf, salt.salt_len);
6317
6318 base64_encode (int_to_base64, (const u8 *) tmp_buf, salt.salt_len, (u8 *) ptr_salt);
6319
6320 memset (tmp_buf, 0, sizeof (tmp_buf));
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 digest_buf[4] = byte_swap_32 (digest_buf[4]);
6329
6330 memcpy (tmp_buf, digest_buf, 20);
6331
6332 base64_encode (int_to_base64, (const u8 *) tmp_buf, 20, (u8 *) ptr_plain);
6333
6334 ptr_plain[27] = 0;
6335
6336 snprintf (out_buf, len-1, "%s%s*%s", SIGNATURE_EPISERVER, ptr_salt, ptr_plain);
6337 }
6338 else if (hash_mode == 400)
6339 {
6340 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6341
6342 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6343 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6344 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6345 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6346
6347 phpass_encode ((unsigned char *) digest_buf, (unsigned char *) ptr_plain);
6348
6349 snprintf (out_buf, len-1, "%s%s%s", (char *) salt.salt_sign, (char *) salt.salt_buf, (char *) ptr_plain);
6350 }
6351 else if (hash_mode == 500)
6352 {
6353 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6354
6355 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6356 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6357 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6358 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6359
6360 md5crypt_encode ((unsigned char *) digest_buf, (unsigned char *) ptr_plain);
6361
6362 if (salt.salt_iter == ROUNDS_MD5CRYPT)
6363 {
6364 snprintf (out_buf, len-1, "$1$%s$%s", (char *) salt.salt_buf, (char *) ptr_plain);
6365 }
6366 else
6367 {
6368 snprintf (out_buf, len-1, "$1$rounds=%i$%s$%s", salt.salt_iter, (char *) salt.salt_buf, (char *) ptr_plain);
6369 }
6370 }
6371 else if (hash_mode == 501)
6372 {
6373 uint digest_idx = salt.digests_offset + digest_pos;
6374
6375 hashinfo_t **hashinfo_ptr = data.hash_info;
6376 char *hash_buf = hashinfo_ptr[digest_idx]->orighash;
6377
6378 snprintf (out_buf, len-1, "%s", hash_buf);
6379 }
6380 else if (hash_mode == 1421)
6381 {
6382 u8 *salt_ptr = (u8 *) salt.salt_buf;
6383
6384 snprintf (out_buf, len-1, "%c%c%c%c%c%c%08x%08x%08x%08x%08x%08x%08x%08x",
6385 salt_ptr[0],
6386 salt_ptr[1],
6387 salt_ptr[2],
6388 salt_ptr[3],
6389 salt_ptr[4],
6390 salt_ptr[5],
6391 digest_buf[0],
6392 digest_buf[1],
6393 digest_buf[2],
6394 digest_buf[3],
6395 digest_buf[4],
6396 digest_buf[5],
6397 digest_buf[6],
6398 digest_buf[7]);
6399 }
6400 else if (hash_mode == 1441)
6401 {
6402 memcpy (tmp_buf, salt.salt_buf, salt.salt_len);
6403
6404 base64_encode (int_to_base64, (const u8 *) tmp_buf, salt.salt_len, (u8 *) ptr_salt);
6405
6406 memset (tmp_buf, 0, sizeof (tmp_buf));
6407
6408 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6409
6410 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6411 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6412 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6413 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6414 digest_buf[4] = byte_swap_32 (digest_buf[4]);
6415 digest_buf[5] = byte_swap_32 (digest_buf[5]);
6416 digest_buf[6] = byte_swap_32 (digest_buf[6]);
6417 digest_buf[7] = byte_swap_32 (digest_buf[7]);
6418
6419 memcpy (tmp_buf, digest_buf, 32);
6420
6421 base64_encode (int_to_base64, (const u8 *) tmp_buf, 32, (u8 *) ptr_plain);
6422
6423 ptr_plain[43] = 0;
6424
6425 snprintf (out_buf, len-1, "%s%s*%s", SIGNATURE_EPISERVER4, ptr_salt, ptr_plain);
6426 }
6427 else if (hash_mode == 1500)
6428 {
6429 out_buf[0] = salt.salt_sign[0] & 0xff;
6430 out_buf[1] = salt.salt_sign[1] & 0xff;
6431 //original method, but changed because of this ticket: https://hashcat.net/trac/ticket/269
6432 //out_buf[0] = int_to_itoa64 ((salt.salt_buf[0] >> 0) & 0x3f);
6433 //out_buf[1] = int_to_itoa64 ((salt.salt_buf[0] >> 6) & 0x3f);
6434
6435 memset (tmp_buf, 0, sizeof (tmp_buf));
6436
6437 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6438
6439 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6440 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6441
6442 memcpy (tmp_buf, digest_buf, 8);
6443
6444 base64_encode (int_to_itoa64, (const u8 *) tmp_buf, 8, (u8 *) ptr_plain);
6445
6446 snprintf (out_buf + 2, len-1-2, "%s", ptr_plain);
6447
6448 out_buf[13] = 0;
6449 }
6450 else if (hash_mode == 1600)
6451 {
6452 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6453
6454 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6455 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6456 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6457 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6458
6459 md5crypt_encode ((unsigned char *) digest_buf, (unsigned char *) ptr_plain);
6460
6461 if (salt.salt_iter == ROUNDS_MD5CRYPT)
6462 {
6463 snprintf (out_buf, len-1, "$apr1$%s$%s", (char *) salt.salt_buf, (char *) ptr_plain);
6464 }
6465 else
6466 {
6467 snprintf (out_buf, len-1, "$apr1$rounds=%i$%s$%s", salt.salt_iter, (char *) salt.salt_buf, (char *) ptr_plain);
6468 }
6469 }
6470 else if (hash_mode == 1711)
6471 {
6472 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6473
6474 digest_buf64[0] = byte_swap_64 (digest_buf64[0]);
6475 digest_buf64[1] = byte_swap_64 (digest_buf64[1]);
6476 digest_buf64[2] = byte_swap_64 (digest_buf64[2]);
6477 digest_buf64[3] = byte_swap_64 (digest_buf64[3]);
6478 digest_buf64[4] = byte_swap_64 (digest_buf64[4]);
6479 digest_buf64[5] = byte_swap_64 (digest_buf64[5]);
6480 digest_buf64[6] = byte_swap_64 (digest_buf64[6]);
6481 digest_buf64[7] = byte_swap_64 (digest_buf64[7]);
6482
6483 memcpy (tmp_buf, digest_buf, 64);
6484 memcpy (tmp_buf + 64, salt.salt_buf, salt.salt_len);
6485
6486 base64_encode (int_to_base64, (const u8 *) tmp_buf, 64 + salt.salt_len, (u8 *) ptr_plain);
6487
6488 snprintf (out_buf, len-1, "%s%s", SIGNATURE_SHA512B64S, ptr_plain);
6489 }
6490 else if (hash_mode == 1722)
6491 {
6492 uint *ptr = digest_buf;
6493
6494 snprintf (out_buf, len-1, "%s%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
6495 (unsigned char *) salt.salt_buf,
6496 ptr[ 1], ptr[ 0],
6497 ptr[ 3], ptr[ 2],
6498 ptr[ 5], ptr[ 4],
6499 ptr[ 7], ptr[ 6],
6500 ptr[ 9], ptr[ 8],
6501 ptr[11], ptr[10],
6502 ptr[13], ptr[12],
6503 ptr[15], ptr[14]);
6504 }
6505 else if (hash_mode == 1731)
6506 {
6507 uint *ptr = digest_buf;
6508
6509 snprintf (out_buf, len-1, "0x0200%s%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
6510 (unsigned char *) salt.salt_buf,
6511 ptr[ 1], ptr[ 0],
6512 ptr[ 3], ptr[ 2],
6513 ptr[ 5], ptr[ 4],
6514 ptr[ 7], ptr[ 6],
6515 ptr[ 9], ptr[ 8],
6516 ptr[11], ptr[10],
6517 ptr[13], ptr[12],
6518 ptr[15], ptr[14]);
6519 }
6520 else if (hash_mode == 1800)
6521 {
6522 // temp workaround
6523
6524 digest_buf64[0] = byte_swap_64 (digest_buf64[0]);
6525 digest_buf64[1] = byte_swap_64 (digest_buf64[1]);
6526 digest_buf64[2] = byte_swap_64 (digest_buf64[2]);
6527 digest_buf64[3] = byte_swap_64 (digest_buf64[3]);
6528 digest_buf64[4] = byte_swap_64 (digest_buf64[4]);
6529 digest_buf64[5] = byte_swap_64 (digest_buf64[5]);
6530 digest_buf64[6] = byte_swap_64 (digest_buf64[6]);
6531 digest_buf64[7] = byte_swap_64 (digest_buf64[7]);
6532
6533 sha512crypt_encode ((unsigned char *) digest_buf64, (unsigned char *) ptr_plain);
6534
6535 if (salt.salt_iter == ROUNDS_SHA512CRYPT)
6536 {
6537 snprintf (out_buf, len-1, "$6$%s$%s", (char *) salt.salt_buf, (char *) ptr_plain);
6538 }
6539 else
6540 {
6541 snprintf (out_buf, len-1, "$6$rounds=%i$%s$%s", salt.salt_iter, (char *) salt.salt_buf, (char *) ptr_plain);
6542 }
6543 }
6544 else if (hash_mode == 2100)
6545 {
6546 uint pos = 0;
6547
6548 snprintf (out_buf + pos, len-1, "%s%i#",
6549 SIGNATURE_DCC2,
6550 salt.salt_iter + 1);
6551
6552 uint signature_len = strlen (out_buf);
6553
6554 pos += signature_len;
6555 len -= signature_len;
6556
6557 char *salt_ptr = (char *) salt.salt_buf;
6558
6559 for (uint i = 0; i < salt.salt_len; i++, pos++, len--) snprintf (out_buf + pos, len-1, "%c", salt_ptr[i]);
6560
6561 snprintf (out_buf + pos, len-1, "#%08x%08x%08x%08x",
6562 byte_swap_32 (digest_buf[0]),
6563 byte_swap_32 (digest_buf[1]),
6564 byte_swap_32 (digest_buf[2]),
6565 byte_swap_32 (digest_buf[3]));
6566 }
6567 else if ((hash_mode == 2400) || (hash_mode == 2410))
6568 {
6569 memcpy (tmp_buf, digest_buf, 16);
6570
6571 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6572
6573 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6574 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6575 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6576 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6577
6578 out_buf[ 0] = int_to_itoa64 ((digest_buf[0] >> 0) & 0x3f);
6579 out_buf[ 1] = int_to_itoa64 ((digest_buf[0] >> 6) & 0x3f);
6580 out_buf[ 2] = int_to_itoa64 ((digest_buf[0] >> 12) & 0x3f);
6581 out_buf[ 3] = int_to_itoa64 ((digest_buf[0] >> 18) & 0x3f);
6582
6583 out_buf[ 4] = int_to_itoa64 ((digest_buf[1] >> 0) & 0x3f);
6584 out_buf[ 5] = int_to_itoa64 ((digest_buf[1] >> 6) & 0x3f);
6585 out_buf[ 6] = int_to_itoa64 ((digest_buf[1] >> 12) & 0x3f);
6586 out_buf[ 7] = int_to_itoa64 ((digest_buf[1] >> 18) & 0x3f);
6587
6588 out_buf[ 8] = int_to_itoa64 ((digest_buf[2] >> 0) & 0x3f);
6589 out_buf[ 9] = int_to_itoa64 ((digest_buf[2] >> 6) & 0x3f);
6590 out_buf[10] = int_to_itoa64 ((digest_buf[2] >> 12) & 0x3f);
6591 out_buf[11] = int_to_itoa64 ((digest_buf[2] >> 18) & 0x3f);
6592
6593 out_buf[12] = int_to_itoa64 ((digest_buf[3] >> 0) & 0x3f);
6594 out_buf[13] = int_to_itoa64 ((digest_buf[3] >> 6) & 0x3f);
6595 out_buf[14] = int_to_itoa64 ((digest_buf[3] >> 12) & 0x3f);
6596 out_buf[15] = int_to_itoa64 ((digest_buf[3] >> 18) & 0x3f);
6597
6598 out_buf[16] = 0;
6599 }
6600 else if (hash_mode == 2500)
6601 {
6602 wpa_t *wpas = (wpa_t *) data.esalts_buf;
6603
6604 wpa_t *wpa = &wpas[salt_pos];
6605
6606 snprintf (out_buf, len-1, "%s:%02x%02x%02x%02x%02x%02x:%02x%02x%02x%02x%02x%02x",
6607 (char *) salt.salt_buf,
6608 wpa->orig_mac1[0],
6609 wpa->orig_mac1[1],
6610 wpa->orig_mac1[2],
6611 wpa->orig_mac1[3],
6612 wpa->orig_mac1[4],
6613 wpa->orig_mac1[5],
6614 wpa->orig_mac2[0],
6615 wpa->orig_mac2[1],
6616 wpa->orig_mac2[2],
6617 wpa->orig_mac2[3],
6618 wpa->orig_mac2[4],
6619 wpa->orig_mac2[5]);
6620 }
6621 else if (hash_mode == 4400)
6622 {
6623 snprintf (out_buf, len-1, "%08x%08x%08x%08x",
6624 byte_swap_32 (digest_buf[0]),
6625 byte_swap_32 (digest_buf[1]),
6626 byte_swap_32 (digest_buf[2]),
6627 byte_swap_32 (digest_buf[3]));
6628 }
6629 else if (hash_mode == 4700)
6630 {
6631 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x",
6632 byte_swap_32 (digest_buf[0]),
6633 byte_swap_32 (digest_buf[1]),
6634 byte_swap_32 (digest_buf[2]),
6635 byte_swap_32 (digest_buf[3]),
6636 byte_swap_32 (digest_buf[4]));
6637 }
6638 else if (hash_mode == 4800)
6639 {
6640 u8 chap_id_byte = (u8) salt.salt_buf[4];
6641
6642 snprintf (out_buf, len-1, "%08x%08x%08x%08x:%08x%08x%08x%08x:%02x",
6643 digest_buf[0],
6644 digest_buf[1],
6645 digest_buf[2],
6646 digest_buf[3],
6647 byte_swap_32 (salt.salt_buf[0]),
6648 byte_swap_32 (salt.salt_buf[1]),
6649 byte_swap_32 (salt.salt_buf[2]),
6650 byte_swap_32 (salt.salt_buf[3]),
6651 chap_id_byte);
6652 }
6653 else if (hash_mode == 4900)
6654 {
6655 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x",
6656 byte_swap_32 (digest_buf[0]),
6657 byte_swap_32 (digest_buf[1]),
6658 byte_swap_32 (digest_buf[2]),
6659 byte_swap_32 (digest_buf[3]),
6660 byte_swap_32 (digest_buf[4]));
6661 }
6662 else if (hash_mode == 5100)
6663 {
6664 snprintf (out_buf, len-1, "%08x%08x",
6665 digest_buf[0],
6666 digest_buf[1]);
6667 }
6668 else if (hash_mode == 5200)
6669 {
6670 snprintf (out_buf, len-1, "%s", hashfile);
6671 }
6672 else if (hash_mode == 5300)
6673 {
6674 ikepsk_t *ikepsks = (ikepsk_t *) data.esalts_buf;
6675
6676 ikepsk_t *ikepsk = &ikepsks[salt_pos];
6677
6678 int buf_len = len -1;
6679
6680 // msg_buf
6681
6682 uint ikepsk_msg_len = ikepsk->msg_len / 4;
6683
6684 for (uint i = 0; i < ikepsk_msg_len; i++)
6685 {
6686 if ((i == 32) || (i == 64) || (i == 66) || (i == 68) || (i == 108))
6687 {
6688 snprintf (out_buf, buf_len, ":");
6689
6690 buf_len--;
6691 out_buf++;
6692 }
6693
6694 snprintf (out_buf, buf_len, "%08x", byte_swap_32 (ikepsk->msg_buf[i]));
6695
6696 buf_len -= 8;
6697 out_buf += 8;
6698 }
6699
6700 // nr_buf
6701
6702 uint ikepsk_nr_len = ikepsk->nr_len / 4;
6703
6704 for (uint i = 0; i < ikepsk_nr_len; i++)
6705 {
6706 if ((i == 0) || (i == 5))
6707 {
6708 snprintf (out_buf, buf_len, ":");
6709
6710 buf_len--;
6711 out_buf++;
6712 }
6713
6714 snprintf (out_buf, buf_len, "%08x", byte_swap_32 (ikepsk->nr_buf[i]));
6715
6716 buf_len -= 8;
6717 out_buf += 8;
6718 }
6719
6720 // digest_buf
6721
6722 for (uint i = 0; i < 4; i++)
6723 {
6724 if (i == 0)
6725 {
6726 snprintf (out_buf, buf_len, ":");
6727
6728 buf_len--;
6729 out_buf++;
6730 }
6731
6732 snprintf (out_buf, buf_len, "%08x", digest_buf[i]);
6733
6734 buf_len -= 8;
6735 out_buf += 8;
6736 }
6737 }
6738 else if (hash_mode == 5400)
6739 {
6740 ikepsk_t *ikepsks = (ikepsk_t *) data.esalts_buf;
6741
6742 ikepsk_t *ikepsk = &ikepsks[salt_pos];
6743
6744 int buf_len = len -1;
6745
6746 // msg_buf
6747
6748 uint ikepsk_msg_len = ikepsk->msg_len / 4;
6749
6750 for (uint i = 0; i < ikepsk_msg_len; i++)
6751 {
6752 if ((i == 32) || (i == 64) || (i == 66) || (i == 68) || (i == 108))
6753 {
6754 snprintf (out_buf, buf_len, ":");
6755
6756 buf_len--;
6757 out_buf++;
6758 }
6759
6760 snprintf (out_buf, buf_len, "%08x", byte_swap_32 (ikepsk->msg_buf[i]));
6761
6762 buf_len -= 8;
6763 out_buf += 8;
6764 }
6765
6766 // nr_buf
6767
6768 uint ikepsk_nr_len = ikepsk->nr_len / 4;
6769
6770 for (uint i = 0; i < ikepsk_nr_len; i++)
6771 {
6772 if ((i == 0) || (i == 5))
6773 {
6774 snprintf (out_buf, buf_len, ":");
6775
6776 buf_len--;
6777 out_buf++;
6778 }
6779
6780 snprintf (out_buf, buf_len, "%08x", byte_swap_32 (ikepsk->nr_buf[i]));
6781
6782 buf_len -= 8;
6783 out_buf += 8;
6784 }
6785
6786 // digest_buf
6787
6788 for (uint i = 0; i < 5; i++)
6789 {
6790 if (i == 0)
6791 {
6792 snprintf (out_buf, buf_len, ":");
6793
6794 buf_len--;
6795 out_buf++;
6796 }
6797
6798 snprintf (out_buf, buf_len, "%08x", digest_buf[i]);
6799
6800 buf_len -= 8;
6801 out_buf += 8;
6802 }
6803 }
6804 else if (hash_mode == 5500)
6805 {
6806 netntlm_t *netntlms = (netntlm_t *) data.esalts_buf;
6807
6808 netntlm_t *netntlm = &netntlms[salt_pos];
6809
6810 char user_buf[64] = { 0 };
6811 char domain_buf[64] = { 0 };
6812 char srvchall_buf[1024] = { 0 };
6813 char clichall_buf[1024] = { 0 };
6814
6815 for (uint i = 0, j = 0; j < netntlm->user_len; i += 1, j += 2)
6816 {
6817 char *ptr = (char *) netntlm->userdomain_buf;
6818
6819 user_buf[i] = ptr[j];
6820 }
6821
6822 for (uint i = 0, j = 0; j < netntlm->domain_len; i += 1, j += 2)
6823 {
6824 char *ptr = (char *) netntlm->userdomain_buf;
6825
6826 domain_buf[i] = ptr[netntlm->user_len + j];
6827 }
6828
6829 for (uint i = 0, j = 0; i < netntlm->srvchall_len; i += 1, j += 2)
6830 {
6831 u8 *ptr = (u8 *) netntlm->chall_buf;
6832
6833 sprintf (srvchall_buf + j, "%02x", ptr[i]);
6834 }
6835
6836 for (uint i = 0, j = 0; i < netntlm->clichall_len; i += 1, j += 2)
6837 {
6838 u8 *ptr = (u8 *) netntlm->chall_buf;
6839
6840 sprintf (clichall_buf + j, "%02x", ptr[netntlm->srvchall_len + i]);
6841 }
6842
6843 snprintf (out_buf, len-1, "%s::%s:%s:%08x%08x%08x%08x%08x%08x:%s",
6844 user_buf,
6845 domain_buf,
6846 srvchall_buf,
6847 digest_buf[0],
6848 digest_buf[1],
6849 digest_buf[2],
6850 digest_buf[3],
6851 byte_swap_32 (salt.salt_buf_pc[0]),
6852 byte_swap_32 (salt.salt_buf_pc[1]),
6853 clichall_buf);
6854 }
6855 else if (hash_mode == 5600)
6856 {
6857 netntlm_t *netntlms = (netntlm_t *) data.esalts_buf;
6858
6859 netntlm_t *netntlm = &netntlms[salt_pos];
6860
6861 char user_buf[64] = { 0 };
6862 char domain_buf[64] = { 0 };
6863 char srvchall_buf[1024] = { 0 };
6864 char clichall_buf[1024] = { 0 };
6865
6866 for (uint i = 0, j = 0; j < netntlm->user_len; i += 1, j += 2)
6867 {
6868 char *ptr = (char *) netntlm->userdomain_buf;
6869
6870 user_buf[i] = ptr[j];
6871 }
6872
6873 for (uint i = 0, j = 0; j < netntlm->domain_len; i += 1, j += 2)
6874 {
6875 char *ptr = (char *) netntlm->userdomain_buf;
6876
6877 domain_buf[i] = ptr[netntlm->user_len + j];
6878 }
6879
6880 for (uint i = 0, j = 0; i < netntlm->srvchall_len; i += 1, j += 2)
6881 {
6882 u8 *ptr = (u8 *) netntlm->chall_buf;
6883
6884 sprintf (srvchall_buf + j, "%02x", ptr[i]);
6885 }
6886
6887 for (uint i = 0, j = 0; i < netntlm->clichall_len; i += 1, j += 2)
6888 {
6889 u8 *ptr = (u8 *) netntlm->chall_buf;
6890
6891 sprintf (clichall_buf + j, "%02x", ptr[netntlm->srvchall_len + i]);
6892 }
6893
6894 snprintf (out_buf, len-1, "%s::%s:%s:%08x%08x%08x%08x:%s",
6895 user_buf,
6896 domain_buf,
6897 srvchall_buf,
6898 digest_buf[0],
6899 digest_buf[1],
6900 digest_buf[2],
6901 digest_buf[3],
6902 clichall_buf);
6903 }
6904 else if (hash_mode == 5700)
6905 {
6906 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6907
6908 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6909 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6910 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6911 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6912 digest_buf[4] = byte_swap_32 (digest_buf[4]);
6913 digest_buf[5] = byte_swap_32 (digest_buf[5]);
6914 digest_buf[6] = byte_swap_32 (digest_buf[6]);
6915 digest_buf[7] = byte_swap_32 (digest_buf[7]);
6916
6917 memcpy (tmp_buf, digest_buf, 32);
6918
6919 base64_encode (int_to_itoa64, (const u8 *) tmp_buf, 32, (u8 *) ptr_plain);
6920
6921 ptr_plain[43] = 0;
6922
6923 snprintf (out_buf, len-1, "%s", ptr_plain);
6924 }
6925 else if (hash_mode == 5800)
6926 {
6927 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6928 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6929 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6930 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6931 digest_buf[4] = byte_swap_32 (digest_buf[4]);
6932
6933 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x",
6934 digest_buf[0],
6935 digest_buf[1],
6936 digest_buf[2],
6937 digest_buf[3],
6938 digest_buf[4]);
6939 }
6940 else if ((hash_mode >= 6200) && (hash_mode <= 6299))
6941 {
6942 snprintf (out_buf, len-1, "%s", hashfile);
6943 }
6944 else if (hash_mode == 6300)
6945 {
6946 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6947
6948 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6949 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6950 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6951 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6952
6953 md5crypt_encode ((unsigned char *) digest_buf, (unsigned char *) ptr_plain);
6954
6955 snprintf (out_buf, len-1, "{smd5}%s$%s", (char *) salt.salt_buf, (char *) ptr_plain);
6956 }
6957 else if (hash_mode == 6400)
6958 {
6959 sha256aix_encode ((unsigned char *) digest_buf, (unsigned char *) ptr_plain);
6960
6961 snprintf (out_buf, len-1, "{ssha256}%02d$%s$%s", salt.salt_sign[0], (char *) salt.salt_buf, (char *) ptr_plain);
6962 }
6963 else if (hash_mode == 6500)
6964 {
6965 sha512aix_encode ((unsigned char *) digest_buf64, (unsigned char *) ptr_plain);
6966
6967 snprintf (out_buf, len-1, "{ssha512}%02d$%s$%s", salt.salt_sign[0], (char *) salt.salt_buf, (char *) ptr_plain);
6968 }
6969 else if (hash_mode == 6600)
6970 {
6971 agilekey_t *agilekeys = (agilekey_t *) data.esalts_buf;
6972
6973 agilekey_t *agilekey = &agilekeys[salt_pos];
6974
6975 salt.salt_buf[0] = byte_swap_32 (salt.salt_buf[0]);
6976 salt.salt_buf[1] = byte_swap_32 (salt.salt_buf[1]);
6977
6978 uint buf_len = len - 1;
6979
6980 uint off = snprintf (out_buf, buf_len, "%d:%08x%08x:", salt.salt_iter + 1, salt.salt_buf[0], salt.salt_buf[1]);
6981 buf_len -= 22;
6982
6983 for (uint i = 0, j = off; i < 1040; i++, j += 2)
6984 {
6985 snprintf (out_buf + j, buf_len, "%02x", agilekey->cipher[i]);
6986
6987 buf_len -= 2;
6988 }
6989 }
6990 else if (hash_mode == 6700)
6991 {
6992 sha1aix_encode ((unsigned char *) digest_buf, (unsigned char *) ptr_plain);
6993
6994 snprintf (out_buf, len-1, "{ssha1}%02d$%s$%s", salt.salt_sign[0], (char *) salt.salt_buf, (char *) ptr_plain);
6995 }
6996 else if (hash_mode == 6800)
6997 {
6998 snprintf (out_buf, len-1, "%s", (char *) salt.salt_buf);
6999 }
7000 else if (hash_mode == 7100)
7001 {
7002 uint *ptr = digest_buf;
7003
7004 pbkdf2_sha512_t *pbkdf2_sha512s = (pbkdf2_sha512_t *) data.esalts_buf;
7005
7006 pbkdf2_sha512_t *pbkdf2_sha512 = &pbkdf2_sha512s[salt_pos];
7007
7008 uint esalt[8] = { 0 };
7009
7010 esalt[0] = byte_swap_32 (pbkdf2_sha512->salt_buf[0]);
7011 esalt[1] = byte_swap_32 (pbkdf2_sha512->salt_buf[1]);
7012 esalt[2] = byte_swap_32 (pbkdf2_sha512->salt_buf[2]);
7013 esalt[3] = byte_swap_32 (pbkdf2_sha512->salt_buf[3]);
7014 esalt[4] = byte_swap_32 (pbkdf2_sha512->salt_buf[4]);
7015 esalt[5] = byte_swap_32 (pbkdf2_sha512->salt_buf[5]);
7016 esalt[6] = byte_swap_32 (pbkdf2_sha512->salt_buf[6]);
7017 esalt[7] = byte_swap_32 (pbkdf2_sha512->salt_buf[7]);
7018
7019 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",
7020 SIGNATURE_SHA512OSX,
7021 salt.salt_iter + 1,
7022 esalt[ 0], esalt[ 1],
7023 esalt[ 2], esalt[ 3],
7024 esalt[ 4], esalt[ 5],
7025 esalt[ 6], esalt[ 7],
7026 ptr [ 1], ptr [ 0],
7027 ptr [ 3], ptr [ 2],
7028 ptr [ 5], ptr [ 4],
7029 ptr [ 7], ptr [ 6],
7030 ptr [ 9], ptr [ 8],
7031 ptr [11], ptr [10],
7032 ptr [13], ptr [12],
7033 ptr [15], ptr [14]);
7034 }
7035 else if (hash_mode == 7200)
7036 {
7037 uint *ptr = digest_buf;
7038
7039 pbkdf2_sha512_t *pbkdf2_sha512s = (pbkdf2_sha512_t *) data.esalts_buf;
7040
7041 pbkdf2_sha512_t *pbkdf2_sha512 = &pbkdf2_sha512s[salt_pos];
7042
7043 uint len_used = 0;
7044
7045 snprintf (out_buf + len_used, len - len_used - 1, "%s%i.", SIGNATURE_SHA512GRUB, salt.salt_iter + 1);
7046
7047 len_used = strlen (out_buf);
7048
7049 unsigned char *salt_buf_ptr = (unsigned char *) pbkdf2_sha512->salt_buf;
7050
7051 for (uint i = 0; i < salt.salt_len; i++, len_used += 2)
7052 {
7053 snprintf (out_buf + len_used, len - len_used - 1, "%02x", salt_buf_ptr[i]);
7054 }
7055
7056 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",
7057 ptr [ 1], ptr [ 0],
7058 ptr [ 3], ptr [ 2],
7059 ptr [ 5], ptr [ 4],
7060 ptr [ 7], ptr [ 6],
7061 ptr [ 9], ptr [ 8],
7062 ptr [11], ptr [10],
7063 ptr [13], ptr [12],
7064 ptr [15], ptr [14]);
7065 }
7066 else if (hash_mode == 7300)
7067 {
7068 rakp_t *rakps = (rakp_t *) data.esalts_buf;
7069
7070 rakp_t *rakp = &rakps[salt_pos];
7071
7072 for (uint i = 0, j = 0; (i * 4) < rakp->salt_len; i += 1, j += 8)
7073 {
7074 sprintf (out_buf + j, "%08x", rakp->salt_buf[i]);
7075 }
7076
7077 snprintf (out_buf + rakp->salt_len * 2, len - 1, ":%08x%08x%08x%08x%08x",
7078 digest_buf[0],
7079 digest_buf[1],
7080 digest_buf[2],
7081 digest_buf[3],
7082 digest_buf[4]);
7083 }
7084 else if (hash_mode == 7400)
7085 {
7086 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
7087
7088 digest_buf[0] = byte_swap_32 (digest_buf[0]);
7089 digest_buf[1] = byte_swap_32 (digest_buf[1]);
7090 digest_buf[2] = byte_swap_32 (digest_buf[2]);
7091 digest_buf[3] = byte_swap_32 (digest_buf[3]);
7092 digest_buf[4] = byte_swap_32 (digest_buf[4]);
7093 digest_buf[5] = byte_swap_32 (digest_buf[5]);
7094 digest_buf[6] = byte_swap_32 (digest_buf[6]);
7095 digest_buf[7] = byte_swap_32 (digest_buf[7]);
7096
7097 sha256crypt_encode ((unsigned char *) digest_buf, (unsigned char *) ptr_plain);
7098
7099 if (salt.salt_iter == ROUNDS_SHA256CRYPT)
7100 {
7101 snprintf (out_buf, len-1, "$5$%s$%s", (char *) salt.salt_buf, (char *) ptr_plain);
7102 }
7103 else
7104 {
7105 snprintf (out_buf, len-1, "$5$rounds=%i$%s$%s", salt.salt_iter, (char *) salt.salt_buf, (char *) ptr_plain);
7106 }
7107 }
7108 else if (hash_mode == 7500)
7109 {
7110 krb5pa_t *krb5pas = (krb5pa_t *) data.esalts_buf;
7111
7112 krb5pa_t *krb5pa = &krb5pas[salt_pos];
7113
7114 u8 *ptr_timestamp = (u8 *) krb5pa->timestamp;
7115 u8 *ptr_checksum = (u8 *) krb5pa->checksum;
7116
7117 char data[128] = { 0 };
7118
7119 char *ptr_data = data;
7120
7121 for (uint i = 0; i < 36; i++, ptr_data += 2)
7122 {
7123 sprintf (ptr_data, "%02x", ptr_timestamp[i]);
7124 }
7125
7126 for (uint i = 0; i < 16; i++, ptr_data += 2)
7127 {
7128 sprintf (ptr_data, "%02x", ptr_checksum[i]);
7129 }
7130
7131 *ptr_data = 0;
7132
7133 snprintf (out_buf, len-1, "%s$%s$%s$%s$%s",
7134 SIGNATURE_KRB5PA,
7135 (char *) krb5pa->user,
7136 (char *) krb5pa->realm,
7137 (char *) krb5pa->salt,
7138 data);
7139 }
7140 else if (hash_mode == 7700)
7141 {
7142 snprintf (out_buf, len-1, "%s$%08X%08X",
7143 (char *) salt.salt_buf,
7144 digest_buf[0],
7145 digest_buf[1]);
7146 }
7147 else if (hash_mode == 7800)
7148 {
7149 snprintf (out_buf, len-1, "%s$%08X%08X%08X%08X%08X",
7150 (char *) salt.salt_buf,
7151 digest_buf[0],
7152 digest_buf[1],
7153 digest_buf[2],
7154 digest_buf[3],
7155 digest_buf[4]);
7156 }
7157 else if (hash_mode == 7900)
7158 {
7159 drupal7_encode ((unsigned char *) digest_buf64, (unsigned char *) ptr_plain);
7160
7161 // ugly hack start
7162
7163 char *tmp = (char *) salt.salt_buf_pc;
7164
7165 ptr_plain[42] = tmp[0];
7166
7167 // ugly hack end
7168
7169 ptr_plain[43] = 0;
7170
7171 snprintf (out_buf, len-1, "%s%s%s", (char *) salt.salt_sign, (char *) salt.salt_buf, (char *) ptr_plain);
7172 }
7173 else if (hash_mode == 8000)
7174 {
7175 snprintf (out_buf, len-1, "0xc007%s%08x%08x%08x%08x%08x%08x%08x%08x",
7176 (unsigned char *) salt.salt_buf,
7177 digest_buf[0],
7178 digest_buf[1],
7179 digest_buf[2],
7180 digest_buf[3],
7181 digest_buf[4],
7182 digest_buf[5],
7183 digest_buf[6],
7184 digest_buf[7]);
7185 }
7186 else if (hash_mode == 8100)
7187 {
7188 salt.salt_buf[0] = byte_swap_32 (salt.salt_buf[0]);
7189 salt.salt_buf[1] = byte_swap_32 (salt.salt_buf[1]);
7190
7191 snprintf (out_buf, len-1, "1%s%08x%08x%08x%08x%08x",
7192 (unsigned char *) salt.salt_buf,
7193 digest_buf[0],
7194 digest_buf[1],
7195 digest_buf[2],
7196 digest_buf[3],
7197 digest_buf[4]);
7198 }
7199 else if (hash_mode == 8200)
7200 {
7201 cloudkey_t *cloudkeys = (cloudkey_t *) data.esalts_buf;
7202
7203 cloudkey_t *cloudkey = &cloudkeys[salt_pos];
7204
7205 char data_buf[4096] = { 0 };
7206
7207 for (int i = 0, j = 0; i < 512; i += 1, j += 8)
7208 {
7209 sprintf (data_buf + j, "%08x", cloudkey->data_buf[i]);
7210 }
7211
7212 data_buf[cloudkey->data_len * 2] = 0;
7213
7214 digest_buf[0] = byte_swap_32 (digest_buf[0]);
7215 digest_buf[1] = byte_swap_32 (digest_buf[1]);
7216 digest_buf[2] = byte_swap_32 (digest_buf[2]);
7217 digest_buf[3] = byte_swap_32 (digest_buf[3]);
7218 digest_buf[4] = byte_swap_32 (digest_buf[4]);
7219 digest_buf[5] = byte_swap_32 (digest_buf[5]);
7220 digest_buf[6] = byte_swap_32 (digest_buf[6]);
7221 digest_buf[7] = byte_swap_32 (digest_buf[7]);
7222
7223 salt.salt_buf[0] = byte_swap_32 (salt.salt_buf[0]);
7224 salt.salt_buf[1] = byte_swap_32 (salt.salt_buf[1]);
7225 salt.salt_buf[2] = byte_swap_32 (salt.salt_buf[2]);
7226 salt.salt_buf[3] = byte_swap_32 (salt.salt_buf[3]);
7227
7228 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x%08x%08x%08x:%08x%08x%08x%08x:%u:%s",
7229 digest_buf[0],
7230 digest_buf[1],
7231 digest_buf[2],
7232 digest_buf[3],
7233 digest_buf[4],
7234 digest_buf[5],
7235 digest_buf[6],
7236 digest_buf[7],
7237 salt.salt_buf[0],
7238 salt.salt_buf[1],
7239 salt.salt_buf[2],
7240 salt.salt_buf[3],
7241 salt.salt_iter + 1,
7242 data_buf);
7243 }
7244 else if (hash_mode == 8300)
7245 {
7246 char digest_buf_c[34] = { 0 };
7247
7248 digest_buf[0] = byte_swap_32 (digest_buf[0]);
7249 digest_buf[1] = byte_swap_32 (digest_buf[1]);
7250 digest_buf[2] = byte_swap_32 (digest_buf[2]);
7251 digest_buf[3] = byte_swap_32 (digest_buf[3]);
7252 digest_buf[4] = byte_swap_32 (digest_buf[4]);
7253
7254 base32_encode (int_to_itoa32, (const u8 *) digest_buf, 20, (u8 *) digest_buf_c);
7255
7256 digest_buf_c[32] = 0;
7257
7258 // domain
7259
7260 const uint salt_pc_len = salt.salt_buf_pc[7]; // what a hack
7261
7262 char domain_buf_c[33] = { 0 };
7263
7264 memcpy (domain_buf_c, (char *) salt.salt_buf_pc, salt_pc_len);
7265
7266 for (uint i = 0; i < salt_pc_len; i++)
7267 {
7268 const char next = domain_buf_c[i];
7269
7270 domain_buf_c[i] = '.';
7271
7272 i += next;
7273 }
7274
7275 domain_buf_c[salt_pc_len] = 0;
7276
7277 // final
7278
7279 snprintf (out_buf, len-1, "%s:%s:%s:%u", digest_buf_c, domain_buf_c, (char *) salt.salt_buf, salt.salt_iter);
7280 }
7281 else if (hash_mode == 8500)
7282 {
7283 snprintf (out_buf, len-1, "%s*%s*%08X%08X", SIGNATURE_RACF, (char *) salt.salt_buf, digest_buf[0], digest_buf[1]);
7284 }
7285 else if (hash_mode == 2612)
7286 {
7287 snprintf (out_buf, len-1, "%s%s$%08x%08x%08x%08x",
7288 SIGNATURE_PHPS,
7289 (char *) salt.salt_buf,
7290 digest_buf[0],
7291 digest_buf[1],
7292 digest_buf[2],
7293 digest_buf[3]);
7294 }
7295 else if (hash_mode == 3711)
7296 {
7297 char *salt_ptr = (char *) salt.salt_buf;
7298
7299 salt_ptr[salt.salt_len - 1] = 0;
7300
7301 snprintf (out_buf, len-1, "%s%s$%08x%08x%08x%08x",
7302 SIGNATURE_MEDIAWIKI_B,
7303 salt_ptr,
7304 digest_buf[0],
7305 digest_buf[1],
7306 digest_buf[2],
7307 digest_buf[3]);
7308 }
7309 else if (hash_mode == 8800)
7310 {
7311 androidfde_t *androidfdes = (androidfde_t *) data.esalts_buf;
7312
7313 androidfde_t *androidfde = &androidfdes[salt_pos];
7314
7315 char tmp[3073] = { 0 };
7316
7317 for (uint i = 0, j = 0; i < 384; i += 1, j += 8)
7318 {
7319 sprintf (tmp + j, "%08x", androidfde->data[i]);
7320 }
7321
7322 tmp[3072] = 0;
7323
7324 snprintf (out_buf, len-1, "%s16$%08x%08x%08x%08x$16$%08x%08x%08x%08x$%s",
7325 SIGNATURE_ANDROIDFDE,
7326 byte_swap_32 (salt.salt_buf[0]),
7327 byte_swap_32 (salt.salt_buf[1]),
7328 byte_swap_32 (salt.salt_buf[2]),
7329 byte_swap_32 (salt.salt_buf[3]),
7330 byte_swap_32 (digest_buf[0]),
7331 byte_swap_32 (digest_buf[1]),
7332 byte_swap_32 (digest_buf[2]),
7333 byte_swap_32 (digest_buf[3]),
7334 tmp);
7335 }
7336 else if (hash_mode == 8900)
7337 {
7338 uint N = salt.scrypt_N;
7339 uint r = salt.scrypt_r;
7340 uint p = salt.scrypt_p;
7341
7342 char base64_salt[32] = { 0 };
7343
7344 base64_encode (int_to_base64, (const u8 *) salt.salt_buf, salt.salt_len, (u8 *) base64_salt);
7345
7346 memset (tmp_buf, 0, 46);
7347
7348 digest_buf[0] = byte_swap_32 (digest_buf[0]);
7349 digest_buf[1] = byte_swap_32 (digest_buf[1]);
7350 digest_buf[2] = byte_swap_32 (digest_buf[2]);
7351 digest_buf[3] = byte_swap_32 (digest_buf[3]);
7352 digest_buf[4] = byte_swap_32 (digest_buf[4]);
7353 digest_buf[5] = byte_swap_32 (digest_buf[5]);
7354 digest_buf[6] = byte_swap_32 (digest_buf[6]);
7355 digest_buf[7] = byte_swap_32 (digest_buf[7]);
7356 digest_buf[8] = 0; // needed for base64_encode ()
7357
7358 base64_encode (int_to_base64, (const u8 *) digest_buf, 32, (u8 *) tmp_buf);
7359
7360 snprintf (out_buf, len-1, "%s:%i:%i:%i:%s:%s",
7361 SIGNATURE_SCRYPT,
7362 N,
7363 r,
7364 p,
7365 base64_salt,
7366 tmp_buf);
7367 }
7368 else if (hash_mode == 9000)
7369 {
7370 snprintf (out_buf, len-1, "%s", hashfile);
7371 }
7372 else if (hash_mode == 9200)
7373 {
7374 // salt
7375
7376 pbkdf2_sha256_t *pbkdf2_sha256s = (pbkdf2_sha256_t *) data.esalts_buf;
7377
7378 pbkdf2_sha256_t *pbkdf2_sha256 = &pbkdf2_sha256s[salt_pos];
7379
7380 unsigned char *salt_buf_ptr = (unsigned char *) pbkdf2_sha256->salt_buf;
7381
7382 // hash
7383
7384 digest_buf[0] = byte_swap_32 (digest_buf[0]);
7385 digest_buf[1] = byte_swap_32 (digest_buf[1]);
7386 digest_buf[2] = byte_swap_32 (digest_buf[2]);
7387 digest_buf[3] = byte_swap_32 (digest_buf[3]);
7388 digest_buf[4] = byte_swap_32 (digest_buf[4]);
7389 digest_buf[5] = byte_swap_32 (digest_buf[5]);
7390 digest_buf[6] = byte_swap_32 (digest_buf[6]);
7391 digest_buf[7] = byte_swap_32 (digest_buf[7]);
7392 digest_buf[8] = 0; // needed for base64_encode ()
7393
7394 char tmp_buf[64] = { 0 };
7395
7396 base64_encode (int_to_itoa64, (const u8 *) digest_buf, 32, (u8 *) tmp_buf);
7397 tmp_buf[43] = 0; // cut it here
7398
7399 // output
7400
7401 snprintf (out_buf, len-1, "%s%s$%s", SIGNATURE_CISCO8, salt_buf_ptr, tmp_buf);
7402 }
7403 else if (hash_mode == 9300)
7404 {
7405 digest_buf[0] = byte_swap_32 (digest_buf[0]);
7406 digest_buf[1] = byte_swap_32 (digest_buf[1]);
7407 digest_buf[2] = byte_swap_32 (digest_buf[2]);
7408 digest_buf[3] = byte_swap_32 (digest_buf[3]);
7409 digest_buf[4] = byte_swap_32 (digest_buf[4]);
7410 digest_buf[5] = byte_swap_32 (digest_buf[5]);
7411 digest_buf[6] = byte_swap_32 (digest_buf[6]);
7412 digest_buf[7] = byte_swap_32 (digest_buf[7]);
7413 digest_buf[8] = 0; // needed for base64_encode ()
7414
7415 char tmp_buf[64] = { 0 };
7416
7417 base64_encode (int_to_itoa64, (const u8 *) digest_buf, 32, (u8 *) tmp_buf);
7418 tmp_buf[43] = 0; // cut it here
7419
7420 unsigned char *salt_buf_ptr = (unsigned char *) salt.salt_buf;
7421
7422 snprintf (out_buf, len-1, "%s%s$%s", SIGNATURE_CISCO9, salt_buf_ptr, tmp_buf);
7423 }
7424 else if (hash_mode == 9400)
7425 {
7426 office2007_t *office2007s = (office2007_t *) data.esalts_buf;
7427
7428 office2007_t *office2007 = &office2007s[salt_pos];
7429
7430 snprintf (out_buf, len-1, "%s*%u*%u*%u*%u*%08x%08x%08x%08x*%08x%08x%08x%08x*%08x%08x%08x%08x%08x",
7431 SIGNATURE_OFFICE2007,
7432 2007,
7433 20,
7434 office2007->keySize,
7435 16,
7436 salt.salt_buf[0],
7437 salt.salt_buf[1],
7438 salt.salt_buf[2],
7439 salt.salt_buf[3],
7440 office2007->encryptedVerifier[0],
7441 office2007->encryptedVerifier[1],
7442 office2007->encryptedVerifier[2],
7443 office2007->encryptedVerifier[3],
7444 office2007->encryptedVerifierHash[0],
7445 office2007->encryptedVerifierHash[1],
7446 office2007->encryptedVerifierHash[2],
7447 office2007->encryptedVerifierHash[3],
7448 office2007->encryptedVerifierHash[4]);
7449 }
7450 else if (hash_mode == 9500)
7451 {
7452 office2010_t *office2010s = (office2010_t *) data.esalts_buf;
7453
7454 office2010_t *office2010 = &office2010s[salt_pos];
7455
7456 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,
7457
7458 salt.salt_buf[0],
7459 salt.salt_buf[1],
7460 salt.salt_buf[2],
7461 salt.salt_buf[3],
7462 office2010->encryptedVerifier[0],
7463 office2010->encryptedVerifier[1],
7464 office2010->encryptedVerifier[2],
7465 office2010->encryptedVerifier[3],
7466 office2010->encryptedVerifierHash[0],
7467 office2010->encryptedVerifierHash[1],
7468 office2010->encryptedVerifierHash[2],
7469 office2010->encryptedVerifierHash[3],
7470 office2010->encryptedVerifierHash[4],
7471 office2010->encryptedVerifierHash[5],
7472 office2010->encryptedVerifierHash[6],
7473 office2010->encryptedVerifierHash[7]);
7474 }
7475 else if (hash_mode == 9600)
7476 {
7477 office2013_t *office2013s = (office2013_t *) data.esalts_buf;
7478
7479 office2013_t *office2013 = &office2013s[salt_pos];
7480
7481 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,
7482
7483 salt.salt_buf[0],
7484 salt.salt_buf[1],
7485 salt.salt_buf[2],
7486 salt.salt_buf[3],
7487 office2013->encryptedVerifier[0],
7488 office2013->encryptedVerifier[1],
7489 office2013->encryptedVerifier[2],
7490 office2013->encryptedVerifier[3],
7491 office2013->encryptedVerifierHash[0],
7492 office2013->encryptedVerifierHash[1],
7493 office2013->encryptedVerifierHash[2],
7494 office2013->encryptedVerifierHash[3],
7495 office2013->encryptedVerifierHash[4],
7496 office2013->encryptedVerifierHash[5],
7497 office2013->encryptedVerifierHash[6],
7498 office2013->encryptedVerifierHash[7]);
7499 }
7500 else if (hash_mode == 9700)
7501 {
7502 oldoffice01_t *oldoffice01s = (oldoffice01_t *) data.esalts_buf;
7503
7504 oldoffice01_t *oldoffice01 = &oldoffice01s[salt_pos];
7505
7506 snprintf (out_buf, len-1, "%s*%08x%08x%08x%08x*%08x%08x%08x%08x*%08x%08x%08x%08x",
7507 (oldoffice01->version == 0) ? SIGNATURE_OLDOFFICE0 : SIGNATURE_OLDOFFICE1,
7508 byte_swap_32 (salt.salt_buf[0]),
7509 byte_swap_32 (salt.salt_buf[1]),
7510 byte_swap_32 (salt.salt_buf[2]),
7511 byte_swap_32 (salt.salt_buf[3]),
7512 byte_swap_32 (oldoffice01->encryptedVerifier[0]),
7513 byte_swap_32 (oldoffice01->encryptedVerifier[1]),
7514 byte_swap_32 (oldoffice01->encryptedVerifier[2]),
7515 byte_swap_32 (oldoffice01->encryptedVerifier[3]),
7516 byte_swap_32 (oldoffice01->encryptedVerifierHash[0]),
7517 byte_swap_32 (oldoffice01->encryptedVerifierHash[1]),
7518 byte_swap_32 (oldoffice01->encryptedVerifierHash[2]),
7519 byte_swap_32 (oldoffice01->encryptedVerifierHash[3]));
7520 }
7521 else if (hash_mode == 9710)
7522 {
7523 oldoffice01_t *oldoffice01s = (oldoffice01_t *) data.esalts_buf;
7524
7525 oldoffice01_t *oldoffice01 = &oldoffice01s[salt_pos];
7526
7527 snprintf (out_buf, len-1, "%s*%08x%08x%08x%08x*%08x%08x%08x%08x*%08x%08x%08x%08x",
7528 (oldoffice01->version == 0) ? SIGNATURE_OLDOFFICE0 : SIGNATURE_OLDOFFICE1,
7529 byte_swap_32 (salt.salt_buf[0]),
7530 byte_swap_32 (salt.salt_buf[1]),
7531 byte_swap_32 (salt.salt_buf[2]),
7532 byte_swap_32 (salt.salt_buf[3]),
7533 byte_swap_32 (oldoffice01->encryptedVerifier[0]),
7534 byte_swap_32 (oldoffice01->encryptedVerifier[1]),
7535 byte_swap_32 (oldoffice01->encryptedVerifier[2]),
7536 byte_swap_32 (oldoffice01->encryptedVerifier[3]),
7537 byte_swap_32 (oldoffice01->encryptedVerifierHash[0]),
7538 byte_swap_32 (oldoffice01->encryptedVerifierHash[1]),
7539 byte_swap_32 (oldoffice01->encryptedVerifierHash[2]),
7540 byte_swap_32 (oldoffice01->encryptedVerifierHash[3]));
7541 }
7542 else if (hash_mode == 9720)
7543 {
7544 oldoffice01_t *oldoffice01s = (oldoffice01_t *) data.esalts_buf;
7545
7546 oldoffice01_t *oldoffice01 = &oldoffice01s[salt_pos];
7547
7548 u8 *rc4key = (u8 *) oldoffice01->rc4key;
7549
7550 snprintf (out_buf, len-1, "%s*%08x%08x%08x%08x*%08x%08x%08x%08x*%08x%08x%08x%08x:%02x%02x%02x%02x%02x",
7551 (oldoffice01->version == 0) ? SIGNATURE_OLDOFFICE0 : SIGNATURE_OLDOFFICE1,
7552 byte_swap_32 (salt.salt_buf[0]),
7553 byte_swap_32 (salt.salt_buf[1]),
7554 byte_swap_32 (salt.salt_buf[2]),
7555 byte_swap_32 (salt.salt_buf[3]),
7556 byte_swap_32 (oldoffice01->encryptedVerifier[0]),
7557 byte_swap_32 (oldoffice01->encryptedVerifier[1]),
7558 byte_swap_32 (oldoffice01->encryptedVerifier[2]),
7559 byte_swap_32 (oldoffice01->encryptedVerifier[3]),
7560 byte_swap_32 (oldoffice01->encryptedVerifierHash[0]),
7561 byte_swap_32 (oldoffice01->encryptedVerifierHash[1]),
7562 byte_swap_32 (oldoffice01->encryptedVerifierHash[2]),
7563 byte_swap_32 (oldoffice01->encryptedVerifierHash[3]),
7564 rc4key[0],
7565 rc4key[1],
7566 rc4key[2],
7567 rc4key[3],
7568 rc4key[4]);
7569 }
7570 else if (hash_mode == 9800)
7571 {
7572 oldoffice34_t *oldoffice34s = (oldoffice34_t *) data.esalts_buf;
7573
7574 oldoffice34_t *oldoffice34 = &oldoffice34s[salt_pos];
7575
7576 snprintf (out_buf, len-1, "%s*%08x%08x%08x%08x*%08x%08x%08x%08x*%08x%08x%08x%08x%08x",
7577 (oldoffice34->version == 3) ? SIGNATURE_OLDOFFICE3 : SIGNATURE_OLDOFFICE4,
7578 salt.salt_buf[0],
7579 salt.salt_buf[1],
7580 salt.salt_buf[2],
7581 salt.salt_buf[3],
7582 byte_swap_32 (oldoffice34->encryptedVerifier[0]),
7583 byte_swap_32 (oldoffice34->encryptedVerifier[1]),
7584 byte_swap_32 (oldoffice34->encryptedVerifier[2]),
7585 byte_swap_32 (oldoffice34->encryptedVerifier[3]),
7586 byte_swap_32 (oldoffice34->encryptedVerifierHash[0]),
7587 byte_swap_32 (oldoffice34->encryptedVerifierHash[1]),
7588 byte_swap_32 (oldoffice34->encryptedVerifierHash[2]),
7589 byte_swap_32 (oldoffice34->encryptedVerifierHash[3]),
7590 byte_swap_32 (oldoffice34->encryptedVerifierHash[4]));
7591 }
7592 else if (hash_mode == 9810)
7593 {
7594 oldoffice34_t *oldoffice34s = (oldoffice34_t *) data.esalts_buf;
7595
7596 oldoffice34_t *oldoffice34 = &oldoffice34s[salt_pos];
7597
7598 snprintf (out_buf, len-1, "%s*%08x%08x%08x%08x*%08x%08x%08x%08x*%08x%08x%08x%08x%08x",
7599 (oldoffice34->version == 3) ? SIGNATURE_OLDOFFICE3 : SIGNATURE_OLDOFFICE4,
7600 salt.salt_buf[0],
7601 salt.salt_buf[1],
7602 salt.salt_buf[2],
7603 salt.salt_buf[3],
7604 byte_swap_32 (oldoffice34->encryptedVerifier[0]),
7605 byte_swap_32 (oldoffice34->encryptedVerifier[1]),
7606 byte_swap_32 (oldoffice34->encryptedVerifier[2]),
7607 byte_swap_32 (oldoffice34->encryptedVerifier[3]),
7608 byte_swap_32 (oldoffice34->encryptedVerifierHash[0]),
7609 byte_swap_32 (oldoffice34->encryptedVerifierHash[1]),
7610 byte_swap_32 (oldoffice34->encryptedVerifierHash[2]),
7611 byte_swap_32 (oldoffice34->encryptedVerifierHash[3]),
7612 byte_swap_32 (oldoffice34->encryptedVerifierHash[4]));
7613 }
7614 else if (hash_mode == 9820)
7615 {
7616 oldoffice34_t *oldoffice34s = (oldoffice34_t *) data.esalts_buf;
7617
7618 oldoffice34_t *oldoffice34 = &oldoffice34s[salt_pos];
7619
7620 u8 *rc4key = (u8 *) oldoffice34->rc4key;
7621
7622 snprintf (out_buf, len-1, "%s*%08x%08x%08x%08x*%08x%08x%08x%08x*%08x%08x%08x%08x%08x:%02x%02x%02x%02x%02x",
7623 (oldoffice34->version == 3) ? SIGNATURE_OLDOFFICE3 : SIGNATURE_OLDOFFICE4,
7624 salt.salt_buf[0],
7625 salt.salt_buf[1],
7626 salt.salt_buf[2],
7627 salt.salt_buf[3],
7628 byte_swap_32 (oldoffice34->encryptedVerifier[0]),
7629 byte_swap_32 (oldoffice34->encryptedVerifier[1]),
7630 byte_swap_32 (oldoffice34->encryptedVerifier[2]),
7631 byte_swap_32 (oldoffice34->encryptedVerifier[3]),
7632 byte_swap_32 (oldoffice34->encryptedVerifierHash[0]),
7633 byte_swap_32 (oldoffice34->encryptedVerifierHash[1]),
7634 byte_swap_32 (oldoffice34->encryptedVerifierHash[2]),
7635 byte_swap_32 (oldoffice34->encryptedVerifierHash[3]),
7636 byte_swap_32 (oldoffice34->encryptedVerifierHash[4]),
7637 rc4key[0],
7638 rc4key[1],
7639 rc4key[2],
7640 rc4key[3],
7641 rc4key[4]);
7642 }
7643 else if (hash_mode == 10000)
7644 {
7645 // salt
7646
7647 pbkdf2_sha256_t *pbkdf2_sha256s = (pbkdf2_sha256_t *) data.esalts_buf;
7648
7649 pbkdf2_sha256_t *pbkdf2_sha256 = &pbkdf2_sha256s[salt_pos];
7650
7651 unsigned char *salt_buf_ptr = (unsigned char *) pbkdf2_sha256->salt_buf;
7652
7653 // hash
7654
7655 digest_buf[0] = byte_swap_32 (digest_buf[0]);
7656 digest_buf[1] = byte_swap_32 (digest_buf[1]);
7657 digest_buf[2] = byte_swap_32 (digest_buf[2]);
7658 digest_buf[3] = byte_swap_32 (digest_buf[3]);
7659 digest_buf[4] = byte_swap_32 (digest_buf[4]);
7660 digest_buf[5] = byte_swap_32 (digest_buf[5]);
7661 digest_buf[6] = byte_swap_32 (digest_buf[6]);
7662 digest_buf[7] = byte_swap_32 (digest_buf[7]);
7663 digest_buf[8] = 0; // needed for base64_encode ()
7664
7665 char tmp_buf[64] = { 0 };
7666
7667 base64_encode (int_to_base64, (const u8 *) digest_buf, 32, (u8 *) tmp_buf);
7668
7669 // output
7670
7671 snprintf (out_buf, len-1, "%s%i$%s$%s", SIGNATURE_DJANGOPBKDF2, salt.salt_iter + 1, salt_buf_ptr, tmp_buf);
7672 }
7673 else if (hash_mode == 10100)
7674 {
7675 snprintf (out_buf, len-1, "%08x%08x:%u:%u:%08x%08x%08x%08x",
7676 digest_buf[0],
7677 digest_buf[1],
7678 2,
7679 4,
7680 byte_swap_32 (salt.salt_buf[0]),
7681 byte_swap_32 (salt.salt_buf[1]),
7682 byte_swap_32 (salt.salt_buf[2]),
7683 byte_swap_32 (salt.salt_buf[3]));
7684 }
7685 else if (hash_mode == 10200)
7686 {
7687 cram_md5_t *cram_md5s = (cram_md5_t *) data.esalts_buf;
7688
7689 cram_md5_t *cram_md5 = &cram_md5s[salt_pos];
7690
7691 // challenge
7692
7693 char challenge[100] = { 0 };
7694
7695 base64_encode (int_to_base64, (const u8 *) salt.salt_buf, salt.salt_len, (u8 *) challenge);
7696
7697 // response
7698
7699 char tmp_buf[100] = { 0 };
7700
7701 uint tmp_len = snprintf (tmp_buf, 100, "%s %08x%08x%08x%08x",
7702 (char *) cram_md5->user,
7703 digest_buf[0],
7704 digest_buf[1],
7705 digest_buf[2],
7706 digest_buf[3]);
7707
7708 char response[100] = { 0 };
7709
7710 base64_encode (int_to_base64, (const u8 *) tmp_buf, tmp_len, (u8 *) response);
7711
7712 snprintf (out_buf, len-1, "%s%s$%s", SIGNATURE_CRAM_MD5, challenge, response);
7713 }
7714 else if (hash_mode == 10300)
7715 {
7716 char tmp_buf[100] = { 0 };
7717
7718 memcpy (tmp_buf + 0, digest_buf, 20);
7719 memcpy (tmp_buf + 20, salt.salt_buf, salt.salt_len);
7720
7721 uint tmp_len = 20 + salt.salt_len;
7722
7723 // base64 encode it
7724
7725 char base64_encoded[100] = { 0 };
7726
7727 base64_encode (int_to_base64, (const u8 *) tmp_buf, tmp_len, (u8 *) base64_encoded);
7728
7729 snprintf (out_buf, len-1, "%s%i}%s", SIGNATURE_SAPH_SHA1, salt.salt_iter + 1, base64_encoded);
7730 }
7731 else if (hash_mode == 10400)
7732 {
7733 pdf_t *pdfs = (pdf_t *) data.esalts_buf;
7734
7735 pdf_t *pdf = &pdfs[salt_pos];
7736
7737 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",
7738
7739 pdf->V,
7740 pdf->R,
7741 40,
7742 pdf->P,
7743 pdf->enc_md,
7744 pdf->id_len,
7745 byte_swap_32 (pdf->id_buf[0]),
7746 byte_swap_32 (pdf->id_buf[1]),
7747 byte_swap_32 (pdf->id_buf[2]),
7748 byte_swap_32 (pdf->id_buf[3]),
7749 pdf->u_len,
7750 byte_swap_32 (pdf->u_buf[0]),
7751 byte_swap_32 (pdf->u_buf[1]),
7752 byte_swap_32 (pdf->u_buf[2]),
7753 byte_swap_32 (pdf->u_buf[3]),
7754 byte_swap_32 (pdf->u_buf[4]),
7755 byte_swap_32 (pdf->u_buf[5]),
7756 byte_swap_32 (pdf->u_buf[6]),
7757 byte_swap_32 (pdf->u_buf[7]),
7758 pdf->o_len,
7759 byte_swap_32 (pdf->o_buf[0]),
7760 byte_swap_32 (pdf->o_buf[1]),
7761 byte_swap_32 (pdf->o_buf[2]),
7762 byte_swap_32 (pdf->o_buf[3]),
7763 byte_swap_32 (pdf->o_buf[4]),
7764 byte_swap_32 (pdf->o_buf[5]),
7765 byte_swap_32 (pdf->o_buf[6]),
7766 byte_swap_32 (pdf->o_buf[7])
7767 );
7768 }
7769 else if (hash_mode == 10410)
7770 {
7771 pdf_t *pdfs = (pdf_t *) data.esalts_buf;
7772
7773 pdf_t *pdf = &pdfs[salt_pos];
7774
7775 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",
7776
7777 pdf->V,
7778 pdf->R,
7779 40,
7780 pdf->P,
7781 pdf->enc_md,
7782 pdf->id_len,
7783 byte_swap_32 (pdf->id_buf[0]),
7784 byte_swap_32 (pdf->id_buf[1]),
7785 byte_swap_32 (pdf->id_buf[2]),
7786 byte_swap_32 (pdf->id_buf[3]),
7787 pdf->u_len,
7788 byte_swap_32 (pdf->u_buf[0]),
7789 byte_swap_32 (pdf->u_buf[1]),
7790 byte_swap_32 (pdf->u_buf[2]),
7791 byte_swap_32 (pdf->u_buf[3]),
7792 byte_swap_32 (pdf->u_buf[4]),
7793 byte_swap_32 (pdf->u_buf[5]),
7794 byte_swap_32 (pdf->u_buf[6]),
7795 byte_swap_32 (pdf->u_buf[7]),
7796 pdf->o_len,
7797 byte_swap_32 (pdf->o_buf[0]),
7798 byte_swap_32 (pdf->o_buf[1]),
7799 byte_swap_32 (pdf->o_buf[2]),
7800 byte_swap_32 (pdf->o_buf[3]),
7801 byte_swap_32 (pdf->o_buf[4]),
7802 byte_swap_32 (pdf->o_buf[5]),
7803 byte_swap_32 (pdf->o_buf[6]),
7804 byte_swap_32 (pdf->o_buf[7])
7805 );
7806 }
7807 else if (hash_mode == 10420)
7808 {
7809 pdf_t *pdfs = (pdf_t *) data.esalts_buf;
7810
7811 pdf_t *pdf = &pdfs[salt_pos];
7812
7813 u8 *rc4key = (u8 *) pdf->rc4key;
7814
7815 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",
7816
7817 pdf->V,
7818 pdf->R,
7819 40,
7820 pdf->P,
7821 pdf->enc_md,
7822 pdf->id_len,
7823 byte_swap_32 (pdf->id_buf[0]),
7824 byte_swap_32 (pdf->id_buf[1]),
7825 byte_swap_32 (pdf->id_buf[2]),
7826 byte_swap_32 (pdf->id_buf[3]),
7827 pdf->u_len,
7828 byte_swap_32 (pdf->u_buf[0]),
7829 byte_swap_32 (pdf->u_buf[1]),
7830 byte_swap_32 (pdf->u_buf[2]),
7831 byte_swap_32 (pdf->u_buf[3]),
7832 byte_swap_32 (pdf->u_buf[4]),
7833 byte_swap_32 (pdf->u_buf[5]),
7834 byte_swap_32 (pdf->u_buf[6]),
7835 byte_swap_32 (pdf->u_buf[7]),
7836 pdf->o_len,
7837 byte_swap_32 (pdf->o_buf[0]),
7838 byte_swap_32 (pdf->o_buf[1]),
7839 byte_swap_32 (pdf->o_buf[2]),
7840 byte_swap_32 (pdf->o_buf[3]),
7841 byte_swap_32 (pdf->o_buf[4]),
7842 byte_swap_32 (pdf->o_buf[5]),
7843 byte_swap_32 (pdf->o_buf[6]),
7844 byte_swap_32 (pdf->o_buf[7]),
7845 rc4key[0],
7846 rc4key[1],
7847 rc4key[2],
7848 rc4key[3],
7849 rc4key[4]
7850 );
7851 }
7852 else if (hash_mode == 10500)
7853 {
7854 pdf_t *pdfs = (pdf_t *) data.esalts_buf;
7855
7856 pdf_t *pdf = &pdfs[salt_pos];
7857
7858 if (pdf->id_len == 32)
7859 {
7860 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",
7861
7862 pdf->V,
7863 pdf->R,
7864 128,
7865 pdf->P,
7866 pdf->enc_md,
7867 pdf->id_len,
7868 byte_swap_32 (pdf->id_buf[0]),
7869 byte_swap_32 (pdf->id_buf[1]),
7870 byte_swap_32 (pdf->id_buf[2]),
7871 byte_swap_32 (pdf->id_buf[3]),
7872 byte_swap_32 (pdf->id_buf[4]),
7873 byte_swap_32 (pdf->id_buf[5]),
7874 byte_swap_32 (pdf->id_buf[6]),
7875 byte_swap_32 (pdf->id_buf[7]),
7876 pdf->u_len,
7877 byte_swap_32 (pdf->u_buf[0]),
7878 byte_swap_32 (pdf->u_buf[1]),
7879 byte_swap_32 (pdf->u_buf[2]),
7880 byte_swap_32 (pdf->u_buf[3]),
7881 byte_swap_32 (pdf->u_buf[4]),
7882 byte_swap_32 (pdf->u_buf[5]),
7883 byte_swap_32 (pdf->u_buf[6]),
7884 byte_swap_32 (pdf->u_buf[7]),
7885 pdf->o_len,
7886 byte_swap_32 (pdf->o_buf[0]),
7887 byte_swap_32 (pdf->o_buf[1]),
7888 byte_swap_32 (pdf->o_buf[2]),
7889 byte_swap_32 (pdf->o_buf[3]),
7890 byte_swap_32 (pdf->o_buf[4]),
7891 byte_swap_32 (pdf->o_buf[5]),
7892 byte_swap_32 (pdf->o_buf[6]),
7893 byte_swap_32 (pdf->o_buf[7])
7894 );
7895 }
7896 else
7897 {
7898 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",
7899
7900 pdf->V,
7901 pdf->R,
7902 128,
7903 pdf->P,
7904 pdf->enc_md,
7905 pdf->id_len,
7906 byte_swap_32 (pdf->id_buf[0]),
7907 byte_swap_32 (pdf->id_buf[1]),
7908 byte_swap_32 (pdf->id_buf[2]),
7909 byte_swap_32 (pdf->id_buf[3]),
7910 pdf->u_len,
7911 byte_swap_32 (pdf->u_buf[0]),
7912 byte_swap_32 (pdf->u_buf[1]),
7913 byte_swap_32 (pdf->u_buf[2]),
7914 byte_swap_32 (pdf->u_buf[3]),
7915 byte_swap_32 (pdf->u_buf[4]),
7916 byte_swap_32 (pdf->u_buf[5]),
7917 byte_swap_32 (pdf->u_buf[6]),
7918 byte_swap_32 (pdf->u_buf[7]),
7919 pdf->o_len,
7920 byte_swap_32 (pdf->o_buf[0]),
7921 byte_swap_32 (pdf->o_buf[1]),
7922 byte_swap_32 (pdf->o_buf[2]),
7923 byte_swap_32 (pdf->o_buf[3]),
7924 byte_swap_32 (pdf->o_buf[4]),
7925 byte_swap_32 (pdf->o_buf[5]),
7926 byte_swap_32 (pdf->o_buf[6]),
7927 byte_swap_32 (pdf->o_buf[7])
7928 );
7929 }
7930 }
7931 else if (hash_mode == 10600)
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 == 10700)
7941 {
7942 uint digest_idx = salt.digests_offset + digest_pos;
7943
7944 hashinfo_t **hashinfo_ptr = data.hash_info;
7945 char *hash_buf = hashinfo_ptr[digest_idx]->orighash;
7946
7947 snprintf (out_buf, len-1, "%s", hash_buf);
7948 }
7949 else if (hash_mode == 10900)
7950 {
7951 uint digest_idx = salt.digests_offset + digest_pos;
7952
7953 hashinfo_t **hashinfo_ptr = data.hash_info;
7954 char *hash_buf = hashinfo_ptr[digest_idx]->orighash;
7955
7956 snprintf (out_buf, len-1, "%s", hash_buf);
7957 }
7958 else if (hash_mode == 11100)
7959 {
7960 u32 salt_challenge = salt.salt_buf[0];
7961
7962 salt_challenge = byte_swap_32 (salt_challenge);
7963
7964 unsigned char *user_name = (unsigned char *) (salt.salt_buf + 1);
7965
7966 snprintf (out_buf, len-1, "%s%s*%08x*%08x%08x%08x%08x",
7967 SIGNATURE_POSTGRESQL_AUTH,
7968 user_name,
7969 salt_challenge,
7970 digest_buf[0],
7971 digest_buf[1],
7972 digest_buf[2],
7973 digest_buf[3]);
7974 }
7975 else if (hash_mode == 11200)
7976 {
7977 snprintf (out_buf, len-1, "%s%s*%08x%08x%08x%08x%08x",
7978 SIGNATURE_MYSQL_AUTH,
7979 (unsigned char *) salt.salt_buf,
7980 digest_buf[0],
7981 digest_buf[1],
7982 digest_buf[2],
7983 digest_buf[3],
7984 digest_buf[4]);
7985 }
7986 else if (hash_mode == 11300)
7987 {
7988 bitcoin_wallet_t *bitcoin_wallets = (bitcoin_wallet_t *) data.esalts_buf;
7989
7990 bitcoin_wallet_t *bitcoin_wallet = &bitcoin_wallets[salt_pos];
7991
7992 const uint cry_master_len = bitcoin_wallet->cry_master_len;
7993 const uint ckey_len = bitcoin_wallet->ckey_len;
7994 const uint public_key_len = bitcoin_wallet->public_key_len;
7995
7996 char *cry_master_buf = (char *) mymalloc ((cry_master_len * 2) + 1);
7997 char *ckey_buf = (char *) mymalloc ((ckey_len * 2) + 1);
7998 char *public_key_buf = (char *) mymalloc ((public_key_len * 2) + 1);
7999
8000 for (uint i = 0, j = 0; i < cry_master_len; i += 1, j += 2)
8001 {
8002 const u8 *ptr = (const u8 *) bitcoin_wallet->cry_master_buf;
8003
8004 sprintf (cry_master_buf + j, "%02x", ptr[i]);
8005 }
8006
8007 for (uint i = 0, j = 0; i < ckey_len; i += 1, j += 2)
8008 {
8009 const u8 *ptr = (const u8 *) bitcoin_wallet->ckey_buf;
8010
8011 sprintf (ckey_buf + j, "%02x", ptr[i]);
8012 }
8013
8014 for (uint i = 0, j = 0; i < public_key_len; i += 1, j += 2)
8015 {
8016 const u8 *ptr = (const u8 *) bitcoin_wallet->public_key_buf;
8017
8018 sprintf (public_key_buf + j, "%02x", ptr[i]);
8019 }
8020
8021 snprintf (out_buf, len-1, "%s%d$%s$%d$%s$%d$%d$%s$%d$%s",
8022 SIGNATURE_BITCOIN_WALLET,
8023 cry_master_len * 2,
8024 cry_master_buf,
8025 salt.salt_len,
8026 (unsigned char *) salt.salt_buf,
8027 salt.salt_iter + 1,
8028 ckey_len * 2,
8029 ckey_buf,
8030 public_key_len * 2,
8031 public_key_buf
8032 );
8033
8034 free (cry_master_buf);
8035 free (ckey_buf);
8036 free (public_key_buf);
8037 }
8038 else if (hash_mode == 11400)
8039 {
8040 uint digest_idx = salt.digests_offset + digest_pos;
8041
8042 hashinfo_t **hashinfo_ptr = data.hash_info;
8043 char *hash_buf = hashinfo_ptr[digest_idx]->orighash;
8044
8045 snprintf (out_buf, len-1, "%s", hash_buf);
8046 }
8047 else if (hash_mode == 11600)
8048 {
8049 seven_zip_t *seven_zips = (seven_zip_t *) data.esalts_buf;
8050
8051 seven_zip_t *seven_zip = &seven_zips[salt_pos];
8052
8053 const uint data_len = seven_zip->data_len;
8054
8055 char *data_buf = (char *) mymalloc ((data_len * 2) + 1);
8056
8057 for (uint i = 0, j = 0; i < data_len; i += 1, j += 2)
8058 {
8059 const u8 *ptr = (const u8 *) seven_zip->data_buf;
8060
8061 sprintf (data_buf + j, "%02x", ptr[i]);
8062 }
8063
8064 snprintf (out_buf, len-1, "%s%u$%u$%u$%s$%u$%08x%08x%08x%08x$%u$%u$%u$%s",
8065 SIGNATURE_SEVEN_ZIP,
8066 0,
8067 salt.salt_sign[0],
8068 0,
8069 (char *) seven_zip->salt_buf,
8070 seven_zip->iv_len,
8071 seven_zip->iv_buf[0],
8072 seven_zip->iv_buf[1],
8073 seven_zip->iv_buf[2],
8074 seven_zip->iv_buf[3],
8075 seven_zip->crc,
8076 seven_zip->data_len,
8077 seven_zip->unpack_size,
8078 data_buf);
8079
8080 free (data_buf);
8081 }
8082 else if (hash_mode == 11700)
8083 {
8084 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x%08x%08x%08x",
8085 digest_buf[0],
8086 digest_buf[1],
8087 digest_buf[2],
8088 digest_buf[3],
8089 digest_buf[4],
8090 digest_buf[5],
8091 digest_buf[6],
8092 digest_buf[7]);
8093 }
8094 else if (hash_mode == 11800)
8095 {
8096 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
8097 digest_buf[ 0],
8098 digest_buf[ 1],
8099 digest_buf[ 2],
8100 digest_buf[ 3],
8101 digest_buf[ 4],
8102 digest_buf[ 5],
8103 digest_buf[ 6],
8104 digest_buf[ 7],
8105 digest_buf[ 8],
8106 digest_buf[ 9],
8107 digest_buf[10],
8108 digest_buf[11],
8109 digest_buf[12],
8110 digest_buf[13],
8111 digest_buf[14],
8112 digest_buf[15]);
8113 }
8114 else if (hash_mode == 11900)
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 == 12000)
8124 {
8125 uint digest_idx = salt.digests_offset + digest_pos;
8126
8127 hashinfo_t **hashinfo_ptr = data.hash_info;
8128 char *hash_buf = hashinfo_ptr[digest_idx]->orighash;
8129
8130 snprintf (out_buf, len-1, "%s", hash_buf);
8131 }
8132 else if (hash_mode == 12100)
8133 {
8134 uint digest_idx = salt.digests_offset + digest_pos;
8135
8136 hashinfo_t **hashinfo_ptr = data.hash_info;
8137 char *hash_buf = hashinfo_ptr[digest_idx]->orighash;
8138
8139 snprintf (out_buf, len-1, "%s", hash_buf);
8140 }
8141 else if (hash_mode == 12200)
8142 {
8143 uint *ptr_digest = digest_buf;
8144 uint *ptr_salt = salt.salt_buf;
8145
8146 snprintf (out_buf, len-1, "%s0$1$%08x%08x$%08x%08x",
8147 SIGNATURE_ECRYPTFS,
8148 ptr_salt[0],
8149 ptr_salt[1],
8150 ptr_digest[0],
8151 ptr_digest[1]);
8152 }
8153 else if (hash_mode == 12300)
8154 {
8155 uint *ptr_digest = digest_buf;
8156 uint *ptr_salt = salt.salt_buf;
8157
8158 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",
8159 ptr_digest[ 0], ptr_digest[ 1],
8160 ptr_digest[ 2], ptr_digest[ 3],
8161 ptr_digest[ 4], ptr_digest[ 5],
8162 ptr_digest[ 6], ptr_digest[ 7],
8163 ptr_digest[ 8], ptr_digest[ 9],
8164 ptr_digest[10], ptr_digest[11],
8165 ptr_digest[12], ptr_digest[13],
8166 ptr_digest[14], ptr_digest[15],
8167 ptr_salt[0],
8168 ptr_salt[1],
8169 ptr_salt[2],
8170 ptr_salt[3]);
8171 }
8172 else if (hash_mode == 12400)
8173 {
8174 // encode iteration count
8175
8176 char salt_iter[5] = { 0 };
8177
8178 salt_iter[0] = int_to_itoa64 ((salt.salt_iter ) & 0x3f);
8179 salt_iter[1] = int_to_itoa64 ((salt.salt_iter >> 6) & 0x3f);
8180 salt_iter[2] = int_to_itoa64 ((salt.salt_iter >> 12) & 0x3f);
8181 salt_iter[3] = int_to_itoa64 ((salt.salt_iter >> 18) & 0x3f);
8182 salt_iter[4] = 0;
8183
8184 // encode salt
8185
8186 ptr_salt[0] = int_to_itoa64 ((salt.salt_buf[0] ) & 0x3f);
8187 ptr_salt[1] = int_to_itoa64 ((salt.salt_buf[0] >> 6) & 0x3f);
8188 ptr_salt[2] = int_to_itoa64 ((salt.salt_buf[0] >> 12) & 0x3f);
8189 ptr_salt[3] = int_to_itoa64 ((salt.salt_buf[0] >> 18) & 0x3f);
8190 ptr_salt[4] = 0;
8191
8192 // encode digest
8193
8194 memset (tmp_buf, 0, sizeof (tmp_buf));
8195
8196 digest_buf[0] = byte_swap_32 (digest_buf[0]);
8197 digest_buf[1] = byte_swap_32 (digest_buf[1]);
8198
8199 memcpy (tmp_buf, digest_buf, 8);
8200
8201 base64_encode (int_to_itoa64, (const u8 *) tmp_buf, 8, (u8 *) ptr_plain);
8202
8203 ptr_plain[11] = 0;
8204
8205 // fill the resulting buffer
8206
8207 snprintf (out_buf, len - 1, "_%s%s%s", salt_iter, ptr_salt, ptr_plain);
8208 }
8209 else if (hash_mode == 12500)
8210 {
8211 snprintf (out_buf, len - 1, "%s*0*%08x%08x*%08x%08x%08x%08x",
8212 SIGNATURE_RAR3,
8213 byte_swap_32 (salt.salt_buf[0]),
8214 byte_swap_32 (salt.salt_buf[1]),
8215 salt.salt_buf[2],
8216 salt.salt_buf[3],
8217 salt.salt_buf[4],
8218 salt.salt_buf[5]);
8219 }
8220 else if (hash_mode == 12600)
8221 {
8222 snprintf (out_buf, len - 1, "%08x%08x%08x%08x%08x%08x%08x%08x",
8223 digest_buf[0] + salt.salt_buf_pc[0],
8224 digest_buf[1] + salt.salt_buf_pc[1],
8225 digest_buf[2] + salt.salt_buf_pc[2],
8226 digest_buf[3] + salt.salt_buf_pc[3],
8227 digest_buf[4] + salt.salt_buf_pc[4],
8228 digest_buf[5] + salt.salt_buf_pc[5],
8229 digest_buf[6] + salt.salt_buf_pc[6],
8230 digest_buf[7] + salt.salt_buf_pc[7]);
8231 }
8232 else if (hash_mode == 12700)
8233 {
8234 uint digest_idx = salt.digests_offset + digest_pos;
8235
8236 hashinfo_t **hashinfo_ptr = data.hash_info;
8237 char *hash_buf = hashinfo_ptr[digest_idx]->orighash;
8238
8239 snprintf (out_buf, len-1, "%s", hash_buf);
8240 }
8241 else if (hash_mode == 12800)
8242 {
8243 const u8 *ptr = (const u8 *) salt.salt_buf;
8244
8245 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",
8246 SIGNATURE_MS_DRSR,
8247 ptr[0],
8248 ptr[1],
8249 ptr[2],
8250 ptr[3],
8251 ptr[4],
8252 ptr[5],
8253 ptr[6],
8254 ptr[7],
8255 ptr[8],
8256 ptr[9],
8257 salt.salt_iter + 1,
8258 byte_swap_32 (digest_buf[0]),
8259 byte_swap_32 (digest_buf[1]),
8260 byte_swap_32 (digest_buf[2]),
8261 byte_swap_32 (digest_buf[3]),
8262 byte_swap_32 (digest_buf[4]),
8263 byte_swap_32 (digest_buf[5]),
8264 byte_swap_32 (digest_buf[6]),
8265 byte_swap_32 (digest_buf[7])
8266 );
8267 }
8268 else if (hash_mode == 12900)
8269 {
8270 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",
8271 salt.salt_buf[ 4],
8272 salt.salt_buf[ 5],
8273 salt.salt_buf[ 6],
8274 salt.salt_buf[ 7],
8275 salt.salt_buf[ 8],
8276 salt.salt_buf[ 9],
8277 salt.salt_buf[10],
8278 salt.salt_buf[11],
8279 byte_swap_32 (digest_buf[0]),
8280 byte_swap_32 (digest_buf[1]),
8281 byte_swap_32 (digest_buf[2]),
8282 byte_swap_32 (digest_buf[3]),
8283 byte_swap_32 (digest_buf[4]),
8284 byte_swap_32 (digest_buf[5]),
8285 byte_swap_32 (digest_buf[6]),
8286 byte_swap_32 (digest_buf[7]),
8287 salt.salt_buf[ 0],
8288 salt.salt_buf[ 1],
8289 salt.salt_buf[ 2],
8290 salt.salt_buf[ 3]
8291 );
8292 }
8293 else if (hash_mode == 13000)
8294 {
8295 rar5_t *rar5s = (rar5_t *) data.esalts_buf;
8296
8297 rar5_t *rar5 = &rar5s[salt_pos];
8298
8299 snprintf (out_buf, len-1, "$rar5$16$%08x%08x%08x%08x$%u$%08x%08x%08x%08x$8$%08x%08x",
8300 salt.salt_buf[0],
8301 salt.salt_buf[1],
8302 salt.salt_buf[2],
8303 salt.salt_buf[3],
8304 salt.salt_sign[0],
8305 rar5->iv[0],
8306 rar5->iv[1],
8307 rar5->iv[2],
8308 rar5->iv[3],
8309 byte_swap_32 (digest_buf[0]),
8310 byte_swap_32 (digest_buf[1])
8311 );
8312 }
8313 else if (hash_mode == 13100)
8314 {
8315 krb5tgs_t *krb5tgss = (krb5tgs_t *) data.esalts_buf;
8316
8317 krb5tgs_t *krb5tgs = &krb5tgss[salt_pos];
8318
8319 u8 *ptr_checksum = (u8 *) krb5tgs->checksum;
8320 u8 *ptr_edata2 = (u8 *) krb5tgs->edata2;
8321
8322 char data[2560 * 4 * 2] = { 0 };
8323
8324 char *ptr_data = data;
8325
8326 for (uint i = 0; i < 16; i++, ptr_data += 2)
8327 sprintf (ptr_data, "%02x", ptr_checksum[i]);
8328
8329 /* skip '$' */
8330 ptr_data++;
8331
8332 for (uint i = 0; i < krb5tgs->edata2_len; i++, ptr_data += 2)
8333 sprintf (ptr_data, "%02x", ptr_edata2[i]);
8334
8335 snprintf (out_buf, len-1, "%s$%s$%s$%s",
8336 SIGNATURE_KRB5TGS,
8337 (char *) krb5tgs->account_info,
8338 data,
8339 data + 33);
8340 }
8341 else if (hash_mode == 13200)
8342 {
8343 snprintf (out_buf, len-1, "%s*%d*%08x%08x%08x%08x*%08x%08x%08x%08x%08x%08x",
8344 SIGNATURE_AXCRYPT,
8345 salt.salt_iter,
8346 salt.salt_buf[0],
8347 salt.salt_buf[1],
8348 salt.salt_buf[2],
8349 salt.salt_buf[3],
8350 salt.salt_buf[4],
8351 salt.salt_buf[5],
8352 salt.salt_buf[6],
8353 salt.salt_buf[7],
8354 salt.salt_buf[8],
8355 salt.salt_buf[9]);
8356 }
8357 else if (hash_mode == 13300)
8358 {
8359 snprintf (out_buf, len-1, "%s$%08x%08x%08x%08x",
8360 SIGNATURE_AXCRYPT_SHA1,
8361 digest_buf[0],
8362 digest_buf[1],
8363 digest_buf[2],
8364 digest_buf[3]);
8365 }
8366 else if (hash_mode == 13400)
8367 {
8368 keepass_t *keepasss = (keepass_t *) data.esalts_buf;
8369
8370 keepass_t *keepass = &keepasss[salt_pos];
8371
8372 u32 version = (u32) keepass->version;
8373 u32 rounds = salt.salt_iter;
8374 u32 algorithm = (u32) keepass->algorithm;
8375 u32 keyfile_len = (u32) keepass->keyfile_len;
8376
8377 u32 *ptr_final_random_seed = (u32 *) keepass->final_random_seed ;
8378 u32 *ptr_transf_random_seed = (u32 *) keepass->transf_random_seed ;
8379 u32 *ptr_enc_iv = (u32 *) keepass->enc_iv ;
8380 u32 *ptr_contents_hash = (u32 *) keepass->contents_hash ;
8381 u32 *ptr_keyfile = (u32 *) keepass->keyfile ;
8382
8383 /* specific to version 1 */
8384 u32 contents_len;
8385 u32 *ptr_contents;
8386
8387 /* specific to version 2 */
8388 u32 expected_bytes_len;
8389 u32 *ptr_expected_bytes;
8390
8391 u32 final_random_seed_len;
8392 u32 transf_random_seed_len;
8393 u32 enc_iv_len;
8394 u32 contents_hash_len;
8395
8396 transf_random_seed_len = 8;
8397 enc_iv_len = 4;
8398 contents_hash_len = 8;
8399 final_random_seed_len = 8;
8400
8401 if (version == 1)
8402 final_random_seed_len = 4;
8403
8404 snprintf (out_buf, len-1, "%s*%d*%d*%d",
8405 SIGNATURE_KEEPASS,
8406 version,
8407 rounds,
8408 algorithm);
8409
8410 char *ptr_data = out_buf;
8411
8412 ptr_data += strlen(out_buf);
8413
8414 *ptr_data = '*';
8415 ptr_data++;
8416
8417 for (uint i = 0; i < final_random_seed_len; i++, ptr_data += 8)
8418 sprintf (ptr_data, "%08x", ptr_final_random_seed[i]);
8419
8420 *ptr_data = '*';
8421 ptr_data++;
8422
8423 for (uint i = 0; i < transf_random_seed_len; i++, ptr_data += 8)
8424 sprintf (ptr_data, "%08x", ptr_transf_random_seed[i]);
8425
8426 *ptr_data = '*';
8427 ptr_data++;
8428
8429 for (uint i = 0; i < enc_iv_len; i++, ptr_data += 8)
8430 sprintf (ptr_data, "%08x", ptr_enc_iv[i]);
8431
8432 *ptr_data = '*';
8433 ptr_data++;
8434
8435 if (version == 1)
8436 {
8437 contents_len = (u32) keepass->contents_len;
8438 ptr_contents = (u32 *) keepass->contents;
8439
8440 for (uint i = 0; i < contents_hash_len; i++, ptr_data += 8)
8441 sprintf (ptr_data, "%08x", ptr_contents_hash[i]);
8442
8443 *ptr_data = '*';
8444 ptr_data++;
8445
8446 /* inline flag */
8447 *ptr_data = '1';
8448 ptr_data++;
8449
8450 *ptr_data = '*';
8451 ptr_data++;
8452
8453 char ptr_contents_len[10] = { 0 };
8454
8455 sprintf ((char*) ptr_contents_len, "%d", contents_len);
8456
8457 sprintf (ptr_data, "%d", contents_len);
8458
8459 ptr_data += strlen(ptr_contents_len);
8460
8461 *ptr_data = '*';
8462 ptr_data++;
8463
8464 for (uint i = 0; i < contents_len / 4; i++, ptr_data += 8)
8465 sprintf (ptr_data, "%08x", ptr_contents[i]);
8466 }
8467 else if (version == 2)
8468 {
8469 expected_bytes_len = 8;
8470 ptr_expected_bytes = (u32 *) keepass->expected_bytes ;
8471
8472 for (uint i = 0; i < expected_bytes_len; i++, ptr_data += 8)
8473 sprintf (ptr_data, "%08x", ptr_expected_bytes[i]);
8474
8475 *ptr_data = '*';
8476 ptr_data++;
8477
8478 for (uint i = 0; i < contents_hash_len; i++, ptr_data += 8)
8479 sprintf (ptr_data, "%08x", ptr_contents_hash[i]);
8480 }
8481 if (keyfile_len)
8482 {
8483 *ptr_data = '*';
8484 ptr_data++;
8485
8486 /* inline flag */
8487 *ptr_data = '1';
8488 ptr_data++;
8489
8490 *ptr_data = '*';
8491 ptr_data++;
8492
8493 sprintf (ptr_data, "%d", keyfile_len);
8494
8495 ptr_data += 2;
8496
8497 *ptr_data = '*';
8498 ptr_data++;
8499
8500 for (uint i = 0; i < 8; i++, ptr_data += 8)
8501 sprintf (ptr_data, "%08x", ptr_keyfile[i]);
8502 }
8503 }
8504 else if (hash_mode == 13500)
8505 {
8506 pstoken_t *pstokens = (pstoken_t *) data.esalts_buf;
8507
8508 pstoken_t *pstoken = &pstokens[salt_pos];
8509
8510 const u32 salt_len = (pstoken->salt_len > 512) ? 512 : pstoken->salt_len;
8511
8512 char pstoken_tmp[1024 + 1] = { 0 };
8513
8514 for (uint i = 0, j = 0; i < salt_len; i += 1, j += 2)
8515 {
8516 const u8 *ptr = (const u8 *) pstoken->salt_buf;
8517
8518 sprintf (pstoken_tmp + j, "%02x", ptr[i]);
8519 }
8520
8521 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x:%s",
8522 digest_buf[0],
8523 digest_buf[1],
8524 digest_buf[2],
8525 digest_buf[3],
8526 digest_buf[4],
8527 pstoken_tmp);
8528 }
8529 else if (hash_mode == 13600)
8530 {
8531 zip2_t *zip2s = (zip2_t *) data.esalts_buf;
8532
8533 zip2_t *zip2 = &zip2s[salt_pos];
8534
8535 const u32 salt_len = zip2->salt_len;
8536
8537 char salt_tmp[32 + 1] = { 0 };
8538
8539 for (uint i = 0, j = 0; i < salt_len; i += 1, j += 2)
8540 {
8541 const u8 *ptr = (const u8 *) zip2->salt_buf;
8542
8543 sprintf (salt_tmp + j, "%02x", ptr[i]);
8544 }
8545
8546 const u32 data_len = zip2->data_len;
8547
8548 char data_tmp[8192 + 1] = { 0 };
8549
8550 for (uint i = 0, j = 0; i < data_len; i += 1, j += 2)
8551 {
8552 const u8 *ptr = (const u8 *) zip2->data_buf;
8553
8554 sprintf (data_tmp + j, "%02x", ptr[i]);
8555 }
8556
8557 const u32 auth_len = zip2->auth_len;
8558
8559 char auth_tmp[20 + 1] = { 0 };
8560
8561 for (uint i = 0, j = 0; i < auth_len; i += 1, j += 2)
8562 {
8563 const u8 *ptr = (const u8 *) zip2->auth_buf;
8564
8565 sprintf (auth_tmp + j, "%02x", ptr[i]);
8566 }
8567
8568 snprintf (out_buf, 255, "%s*%u*%u*%u*%s*%x*%u*%s*%s*%s",
8569 SIGNATURE_ZIP2_START,
8570 zip2->type,
8571 zip2->mode,
8572 zip2->magic,
8573 salt_tmp,
8574 zip2->verify_bytes,
8575 zip2->compress_length,
8576 data_tmp,
8577 auth_tmp,
8578 SIGNATURE_ZIP2_STOP);
8579 }
8580 else if ((hash_mode >= 13700) && (hash_mode <= 13799))
8581 {
8582 snprintf (out_buf, len-1, "%s", hashfile);
8583 }
8584 else
8585 {
8586 if (hash_type == HASH_TYPE_MD4)
8587 {
8588 snprintf (out_buf, 255, "%08x%08x%08x%08x",
8589 digest_buf[0],
8590 digest_buf[1],
8591 digest_buf[2],
8592 digest_buf[3]);
8593 }
8594 else if (hash_type == HASH_TYPE_MD5)
8595 {
8596 snprintf (out_buf, len-1, "%08x%08x%08x%08x",
8597 digest_buf[0],
8598 digest_buf[1],
8599 digest_buf[2],
8600 digest_buf[3]);
8601 }
8602 else if (hash_type == HASH_TYPE_SHA1)
8603 {
8604 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x",
8605 digest_buf[0],
8606 digest_buf[1],
8607 digest_buf[2],
8608 digest_buf[3],
8609 digest_buf[4]);
8610 }
8611 else if (hash_type == HASH_TYPE_SHA256)
8612 {
8613 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x%08x%08x%08x",
8614 digest_buf[0],
8615 digest_buf[1],
8616 digest_buf[2],
8617 digest_buf[3],
8618 digest_buf[4],
8619 digest_buf[5],
8620 digest_buf[6],
8621 digest_buf[7]);
8622 }
8623 else if (hash_type == HASH_TYPE_SHA384)
8624 {
8625 uint *ptr = digest_buf;
8626
8627 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
8628 ptr[ 1], ptr[ 0],
8629 ptr[ 3], ptr[ 2],
8630 ptr[ 5], ptr[ 4],
8631 ptr[ 7], ptr[ 6],
8632 ptr[ 9], ptr[ 8],
8633 ptr[11], ptr[10]);
8634 }
8635 else if (hash_type == HASH_TYPE_SHA512)
8636 {
8637 uint *ptr = digest_buf;
8638
8639 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
8640 ptr[ 1], ptr[ 0],
8641 ptr[ 3], ptr[ 2],
8642 ptr[ 5], ptr[ 4],
8643 ptr[ 7], ptr[ 6],
8644 ptr[ 9], ptr[ 8],
8645 ptr[11], ptr[10],
8646 ptr[13], ptr[12],
8647 ptr[15], ptr[14]);
8648 }
8649 else if (hash_type == HASH_TYPE_LM)
8650 {
8651 snprintf (out_buf, len-1, "%08x%08x",
8652 digest_buf[0],
8653 digest_buf[1]);
8654 }
8655 else if (hash_type == HASH_TYPE_ORACLEH)
8656 {
8657 snprintf (out_buf, len-1, "%08X%08X",
8658 digest_buf[0],
8659 digest_buf[1]);
8660 }
8661 else if (hash_type == HASH_TYPE_BCRYPT)
8662 {
8663 base64_encode (int_to_bf64, (const u8 *) salt.salt_buf, 16, (u8 *) tmp_buf + 0);
8664 base64_encode (int_to_bf64, (const u8 *) digest_buf, 23, (u8 *) tmp_buf + 22);
8665
8666 tmp_buf[22 + 31] = 0; // base64_encode wants to pad
8667
8668 snprintf (out_buf, len-1, "%s$%s", (char *) salt.salt_sign, tmp_buf);
8669 }
8670 else if (hash_type == HASH_TYPE_KECCAK)
8671 {
8672 uint *ptr = digest_buf;
8673
8674 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",
8675 ptr[ 1], ptr[ 0],
8676 ptr[ 3], ptr[ 2],
8677 ptr[ 5], ptr[ 4],
8678 ptr[ 7], ptr[ 6],
8679 ptr[ 9], ptr[ 8],
8680 ptr[11], ptr[10],
8681 ptr[13], ptr[12],
8682 ptr[15], ptr[14],
8683 ptr[17], ptr[16],
8684 ptr[19], ptr[18],
8685 ptr[21], ptr[20],
8686 ptr[23], ptr[22],
8687 ptr[25], ptr[24],
8688 ptr[27], ptr[26],
8689 ptr[29], ptr[28],
8690 ptr[31], ptr[30],
8691 ptr[33], ptr[32],
8692 ptr[35], ptr[34],
8693 ptr[37], ptr[36],
8694 ptr[39], ptr[38],
8695 ptr[41], ptr[30],
8696 ptr[43], ptr[42],
8697 ptr[45], ptr[44],
8698 ptr[47], ptr[46],
8699 ptr[49], ptr[48]
8700 );
8701
8702 out_buf[salt.keccak_mdlen * 2] = 0;
8703 }
8704 else if (hash_type == HASH_TYPE_RIPEMD160)
8705 {
8706 snprintf (out_buf, 255, "%08x%08x%08x%08x%08x",
8707 digest_buf[0],
8708 digest_buf[1],
8709 digest_buf[2],
8710 digest_buf[3],
8711 digest_buf[4]);
8712 }
8713 else if (hash_type == HASH_TYPE_WHIRLPOOL)
8714 {
8715 digest_buf[ 0] = digest_buf[ 0];
8716 digest_buf[ 1] = digest_buf[ 1];
8717 digest_buf[ 2] = digest_buf[ 2];
8718 digest_buf[ 3] = digest_buf[ 3];
8719 digest_buf[ 4] = digest_buf[ 4];
8720 digest_buf[ 5] = digest_buf[ 5];
8721 digest_buf[ 6] = digest_buf[ 6];
8722 digest_buf[ 7] = digest_buf[ 7];
8723 digest_buf[ 8] = digest_buf[ 8];
8724 digest_buf[ 9] = digest_buf[ 9];
8725 digest_buf[10] = digest_buf[10];
8726 digest_buf[11] = digest_buf[11];
8727 digest_buf[12] = digest_buf[12];
8728 digest_buf[13] = digest_buf[13];
8729 digest_buf[14] = digest_buf[14];
8730 digest_buf[15] = digest_buf[15];
8731
8732 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
8733 digest_buf[ 0],
8734 digest_buf[ 1],
8735 digest_buf[ 2],
8736 digest_buf[ 3],
8737 digest_buf[ 4],
8738 digest_buf[ 5],
8739 digest_buf[ 6],
8740 digest_buf[ 7],
8741 digest_buf[ 8],
8742 digest_buf[ 9],
8743 digest_buf[10],
8744 digest_buf[11],
8745 digest_buf[12],
8746 digest_buf[13],
8747 digest_buf[14],
8748 digest_buf[15]);
8749 }
8750 else if (hash_type == HASH_TYPE_GOST)
8751 {
8752 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x%08x%08x%08x",
8753 digest_buf[0],
8754 digest_buf[1],
8755 digest_buf[2],
8756 digest_buf[3],
8757 digest_buf[4],
8758 digest_buf[5],
8759 digest_buf[6],
8760 digest_buf[7]);
8761 }
8762 else if (hash_type == HASH_TYPE_MYSQL)
8763 {
8764 snprintf (out_buf, len-1, "%08x%08x",
8765 digest_buf[0],
8766 digest_buf[1]);
8767 }
8768 else if (hash_type == HASH_TYPE_LOTUS5)
8769 {
8770 snprintf (out_buf, len-1, "%08x%08x%08x%08x",
8771 digest_buf[0],
8772 digest_buf[1],
8773 digest_buf[2],
8774 digest_buf[3]);
8775 }
8776 else if (hash_type == HASH_TYPE_LOTUS6)
8777 {
8778 digest_buf[ 0] = byte_swap_32 (digest_buf[ 0]);
8779 digest_buf[ 1] = byte_swap_32 (digest_buf[ 1]);
8780 digest_buf[ 2] = byte_swap_32 (digest_buf[ 2]);
8781 digest_buf[ 3] = byte_swap_32 (digest_buf[ 3]);
8782
8783 char buf[16] = { 0 };
8784
8785 memcpy (buf + 0, salt.salt_buf, 5);
8786 memcpy (buf + 5, digest_buf, 9);
8787
8788 buf[3] -= -4;
8789
8790 base64_encode (int_to_lotus64, (const u8 *) buf, 14, (u8 *) tmp_buf);
8791
8792 tmp_buf[18] = salt.salt_buf_pc[7];
8793 tmp_buf[19] = 0;
8794
8795 snprintf (out_buf, len-1, "(G%s)", tmp_buf);
8796 }
8797 else if (hash_type == HASH_TYPE_LOTUS8)
8798 {
8799 char buf[52] = { 0 };
8800
8801 // salt
8802
8803 memcpy (buf + 0, salt.salt_buf, 16);
8804
8805 buf[3] -= -4;
8806
8807 // iteration
8808
8809 snprintf (buf + 16, 11, "%010i", salt.salt_iter + 1);
8810
8811 // chars
8812
8813 buf[26] = salt.salt_buf_pc[0];
8814 buf[27] = salt.salt_buf_pc[1];
8815
8816 // digest
8817
8818 memcpy (buf + 28, digest_buf, 8);
8819
8820 base64_encode (int_to_lotus64, (const u8 *) buf, 36, (u8 *) tmp_buf);
8821
8822 tmp_buf[49] = 0;
8823
8824 snprintf (out_buf, len-1, "(H%s)", tmp_buf);
8825 }
8826 else if (hash_type == HASH_TYPE_CRC32)
8827 {
8828 snprintf (out_buf, len-1, "%08x", byte_swap_32 (digest_buf[0]));
8829 }
8830 }
8831
8832 if (salt_type == SALT_TYPE_INTERN)
8833 {
8834 size_t pos = strlen (out_buf);
8835
8836 out_buf[pos] = data.separator;
8837
8838 char *ptr = (char *) salt.salt_buf;
8839
8840 memcpy (out_buf + pos + 1, ptr, salt.salt_len);
8841
8842 out_buf[pos + 1 + salt.salt_len] = 0;
8843 }
8844 }
8845
8846 void to_hccap_t (hccap_t *hccap, uint salt_pos, uint digest_pos)
8847 {
8848 memset (hccap, 0, sizeof (hccap_t));
8849
8850 salt_t *salt = &data.salts_buf[salt_pos];
8851
8852 memcpy (hccap->essid, salt->salt_buf, salt->salt_len);
8853
8854 wpa_t *wpas = (wpa_t *) data.esalts_buf;
8855 wpa_t *wpa = &wpas[salt_pos];
8856
8857 hccap->keyver = wpa->keyver;
8858
8859 hccap->eapol_size = wpa->eapol_size;
8860
8861 if (wpa->keyver != 1)
8862 {
8863 uint eapol_tmp[64] = { 0 };
8864
8865 for (uint i = 0; i < 64; i++)
8866 {
8867 eapol_tmp[i] = byte_swap_32 (wpa->eapol[i]);
8868 }
8869
8870 memcpy (hccap->eapol, eapol_tmp, wpa->eapol_size);
8871 }
8872 else
8873 {
8874 memcpy (hccap->eapol, wpa->eapol, wpa->eapol_size);
8875 }
8876
8877 memcpy (hccap->mac1, wpa->orig_mac1, 6);
8878 memcpy (hccap->mac2, wpa->orig_mac2, 6);
8879 memcpy (hccap->nonce1, wpa->orig_nonce1, 32);
8880 memcpy (hccap->nonce2, wpa->orig_nonce2, 32);
8881
8882 char *digests_buf_ptr = (char *) data.digests_buf;
8883
8884 uint dgst_size = data.dgst_size;
8885
8886 uint *digest_ptr = (uint *) (digests_buf_ptr + (data.salts_buf[salt_pos].digests_offset * dgst_size) + (digest_pos * dgst_size));
8887
8888 if (wpa->keyver != 1)
8889 {
8890 uint digest_tmp[4] = { 0 };
8891
8892 digest_tmp[0] = byte_swap_32 (digest_ptr[0]);
8893 digest_tmp[1] = byte_swap_32 (digest_ptr[1]);
8894 digest_tmp[2] = byte_swap_32 (digest_ptr[2]);
8895 digest_tmp[3] = byte_swap_32 (digest_ptr[3]);
8896
8897 memcpy (hccap->keymic, digest_tmp, 16);
8898 }
8899 else
8900 {
8901 memcpy (hccap->keymic, digest_ptr, 16);
8902 }
8903 }
8904
8905 void SuspendThreads ()
8906 {
8907 if (data.devices_status == STATUS_RUNNING)
8908 {
8909 hc_timer_set (&data.timer_paused);
8910
8911 data.devices_status = STATUS_PAUSED;
8912
8913 log_info ("Paused");
8914 }
8915 }
8916
8917 void ResumeThreads ()
8918 {
8919 if (data.devices_status == STATUS_PAUSED)
8920 {
8921 double ms_paused;
8922
8923 hc_timer_get (data.timer_paused, ms_paused);
8924
8925 data.ms_paused += ms_paused;
8926
8927 data.devices_status = STATUS_RUNNING;
8928
8929 log_info ("Resumed");
8930 }
8931 }
8932
8933 void bypass ()
8934 {
8935 if (data.devices_status != STATUS_RUNNING) return;
8936
8937 data.devices_status = STATUS_BYPASS;
8938
8939 log_info ("Next dictionary / mask in queue selected, bypassing current one");
8940 }
8941
8942 void stop_at_checkpoint ()
8943 {
8944 if (data.devices_status != STATUS_STOP_AT_CHECKPOINT)
8945 {
8946 if (data.devices_status != STATUS_RUNNING) return;
8947 }
8948
8949 // this feature only makes sense if --restore-disable was not specified
8950
8951 if (data.restore_disable == 1)
8952 {
8953 log_info ("WARNING: this feature is disabled when --restore-disable was specified");
8954
8955 return;
8956 }
8957
8958 // check if monitoring of Restore Point updates should be enabled or disabled
8959
8960 if (data.devices_status != STATUS_STOP_AT_CHECKPOINT)
8961 {
8962 data.devices_status = STATUS_STOP_AT_CHECKPOINT;
8963
8964 // save the current restore point value
8965
8966 data.checkpoint_cur_words = get_lowest_words_done ();
8967
8968 log_info ("Checkpoint enabled: will quit at next Restore Point update");
8969 }
8970 else
8971 {
8972 data.devices_status = STATUS_RUNNING;
8973
8974 // reset the global value for checkpoint checks
8975
8976 data.checkpoint_cur_words = 0;
8977
8978 log_info ("Checkpoint disabled: Restore Point updates will no longer be monitored");
8979 }
8980 }
8981
8982 void myabort ()
8983 {
8984 if (data.devices_status == STATUS_INIT) return;
8985 if (data.devices_status == STATUS_STARTING) return;
8986
8987 data.devices_status = STATUS_ABORTED;
8988 }
8989
8990 void myquit ()
8991 {
8992 if (data.devices_status == STATUS_INIT) return;
8993 if (data.devices_status == STATUS_STARTING) return;
8994
8995 data.devices_status = STATUS_QUIT;
8996 }
8997
8998 void load_kernel (const char *kernel_file, int num_devices, size_t *kernel_lengths, const u8 **kernel_sources)
8999 {
9000 FILE *fp = fopen (kernel_file, "rb");
9001
9002 if (fp != NULL)
9003 {
9004 struct stat st;
9005
9006 memset (&st, 0, sizeof (st));
9007
9008 stat (kernel_file, &st);
9009
9010 u8 *buf = (u8 *) mymalloc (st.st_size + 1);
9011
9012 size_t num_read = fread (buf, sizeof (u8), st.st_size, fp);
9013
9014 if (num_read != (size_t) st.st_size)
9015 {
9016 log_error ("ERROR: %s: %s", kernel_file, strerror (errno));
9017
9018 exit (-1);
9019 }
9020
9021 fclose (fp);
9022
9023 buf[st.st_size] = 0;
9024
9025 for (int i = 0; i < num_devices; i++)
9026 {
9027 kernel_lengths[i] = (size_t) st.st_size;
9028
9029 kernel_sources[i] = buf;
9030 }
9031 }
9032 else
9033 {
9034 log_error ("ERROR: %s: %s", kernel_file, strerror (errno));
9035
9036 exit (-1);
9037 }
9038
9039 return;
9040 }
9041
9042 void writeProgramBin (char *dst, u8 *binary, size_t binary_size)
9043 {
9044 if (binary_size > 0)
9045 {
9046 FILE *fp = fopen (dst, "wb");
9047
9048 lock_file (fp);
9049 fwrite (binary, sizeof (u8), binary_size, fp);
9050
9051 fflush (fp);
9052 fclose (fp);
9053 }
9054 }
9055
9056 /**
9057 * restore
9058 */
9059
9060 restore_data_t *init_restore (int argc, char **argv)
9061 {
9062 restore_data_t *rd = (restore_data_t *) mymalloc (sizeof (restore_data_t));
9063
9064 if (data.restore_disable == 0)
9065 {
9066 FILE *fp = fopen (data.eff_restore_file, "rb");
9067
9068 if (fp)
9069 {
9070 size_t nread = fread (rd, sizeof (restore_data_t), 1, fp);
9071
9072 if (nread != 1)
9073 {
9074 log_error ("ERROR: cannot read %s", data.eff_restore_file);
9075
9076 exit (-1);
9077 }
9078
9079 fclose (fp);
9080
9081 if (rd->pid)
9082 {
9083 char *pidbin = (char *) mymalloc (HCBUFSIZ);
9084
9085 int pidbin_len = -1;
9086
9087 #ifdef _POSIX
9088 snprintf (pidbin, HCBUFSIZ - 1, "/proc/%d/cmdline", rd->pid);
9089
9090 FILE *fd = fopen (pidbin, "rb");
9091
9092 if (fd)
9093 {
9094 pidbin_len = fread (pidbin, 1, HCBUFSIZ, fd);
9095
9096 pidbin[pidbin_len] = 0;
9097
9098 fclose (fd);
9099
9100 char *argv0_r = strrchr (argv[0], '/');
9101
9102 char *pidbin_r = strrchr (pidbin, '/');
9103
9104 if (argv0_r == NULL) argv0_r = argv[0];
9105
9106 if (pidbin_r == NULL) pidbin_r = pidbin;
9107
9108 if (strcmp (argv0_r, pidbin_r) == 0)
9109 {
9110 log_error ("ERROR: already an instance %s running on pid %d", pidbin, rd->pid);
9111
9112 exit (-1);
9113 }
9114 }
9115
9116 #elif _WIN
9117 HANDLE hProcess = OpenProcess (PROCESS_ALL_ACCESS, FALSE, rd->pid);
9118
9119 char *pidbin2 = (char *) mymalloc (HCBUFSIZ);
9120
9121 int pidbin2_len = -1;
9122
9123 pidbin_len = GetModuleFileName (NULL, pidbin, HCBUFSIZ);
9124 pidbin2_len = GetModuleFileNameEx (hProcess, NULL, pidbin2, HCBUFSIZ);
9125
9126 pidbin[pidbin_len] = 0;
9127 pidbin2[pidbin2_len] = 0;
9128
9129 if (pidbin2_len)
9130 {
9131 if (strcmp (pidbin, pidbin2) == 0)
9132 {
9133 log_error ("ERROR: already an instance %s running on pid %d", pidbin2, rd->pid);
9134
9135 exit (-1);
9136 }
9137 }
9138
9139 myfree (pidbin2);
9140
9141 #endif
9142
9143 myfree (pidbin);
9144 }
9145
9146 if (rd->version_bin < RESTORE_MIN)
9147 {
9148 log_error ("ERROR: cannot use outdated %s. Please remove it.", data.eff_restore_file);
9149
9150 exit (-1);
9151 }
9152 }
9153 }
9154
9155 memset (rd, 0, sizeof (restore_data_t));
9156
9157 rd->version_bin = VERSION_BIN;
9158
9159 #ifdef _POSIX
9160 rd->pid = getpid ();
9161 #elif _WIN
9162 rd->pid = GetCurrentProcessId ();
9163 #endif
9164
9165 if (getcwd (rd->cwd, 255) == NULL)
9166 {
9167 myfree (rd);
9168
9169 return (NULL);
9170 }
9171
9172 rd->argc = argc;
9173 rd->argv = argv;
9174
9175 return (rd);
9176 }
9177
9178 void read_restore (const char *eff_restore_file, restore_data_t *rd)
9179 {
9180 FILE *fp = fopen (eff_restore_file, "rb");
9181
9182 if (fp == NULL)
9183 {
9184 log_error ("ERROR: restore file '%s': %s", eff_restore_file, strerror (errno));
9185
9186 exit (-1);
9187 }
9188
9189 if (fread (rd, sizeof (restore_data_t), 1, fp) != 1)
9190 {
9191 log_error ("ERROR: cannot read %s", eff_restore_file);
9192
9193 exit (-1);
9194 }
9195
9196 rd->argv = (char **) mycalloc (rd->argc, sizeof (char *));
9197
9198 char *buf = (char *) mymalloc (HCBUFSIZ);
9199
9200 for (uint i = 0; i < rd->argc; i++)
9201 {
9202 if (fgets (buf, HCBUFSIZ - 1, fp) == NULL)
9203 {
9204 log_error ("ERROR: cannot read %s", eff_restore_file);
9205
9206 exit (-1);
9207 }
9208
9209 size_t len = strlen (buf);
9210
9211 if (len) buf[len - 1] = 0;
9212
9213 rd->argv[i] = mystrdup (buf);
9214 }
9215
9216 myfree (buf);
9217
9218 fclose (fp);
9219
9220 log_info ("INFO: Changing current working directory to the path found within the .restore file: '%s'", rd->cwd);
9221
9222 if (chdir (rd->cwd))
9223 {
9224 log_error ("ERROR: The directory '%s' does not exist. It is needed to restore (--restore) the session.\n"
9225 " You could either create this directory (or link it) or update the .restore file using e.g. the analyze_hc_restore.pl tool:\n"
9226 " https://github.com/philsmd/analyze_hc_restore\n"
9227 " The directory must be relative to (or contain) all files/folders mentioned within the command line.", rd->cwd);
9228
9229 exit (-1);
9230 }
9231 }
9232
9233 u64 get_lowest_words_done ()
9234 {
9235 u64 words_cur = -1;
9236
9237 for (uint device_id = 0; device_id < data.devices_cnt; device_id++)
9238 {
9239 hc_device_param_t *device_param = &data.devices_param[device_id];
9240
9241 if (device_param->skipped) continue;
9242
9243 const u64 words_done = device_param->words_done;
9244
9245 if (words_done < words_cur) words_cur = words_done;
9246 }
9247
9248 // It's possible that a device's workload isn't finished right after a restore-case.
9249 // In that case, this function would return 0 and overwrite the real restore point
9250 // There's also data.words_cur which is set to rd->words_cur but it changes while
9251 // the attack is running therefore we should stick to rd->words_cur.
9252 // Note that -s influences rd->words_cur we should keep a close look on that.
9253
9254 if (words_cur < data.rd->words_cur) words_cur = data.rd->words_cur;
9255
9256 return words_cur;
9257 }
9258
9259 void write_restore (const char *new_restore_file, restore_data_t *rd)
9260 {
9261 u64 words_cur = get_lowest_words_done ();
9262
9263 rd->words_cur = words_cur;
9264
9265 FILE *fp = fopen (new_restore_file, "wb");
9266
9267 if (fp == NULL)
9268 {
9269 log_error ("ERROR: %s: %s", new_restore_file, strerror (errno));
9270
9271 exit (-1);
9272 }
9273
9274 if (setvbuf (fp, NULL, _IONBF, 0))
9275 {
9276 log_error ("ERROR: setvbuf file '%s': %s", new_restore_file, strerror (errno));
9277
9278 exit (-1);
9279 }
9280
9281 fwrite (rd, sizeof (restore_data_t), 1, fp);
9282
9283 for (uint i = 0; i < rd->argc; i++)
9284 {
9285 fprintf (fp, "%s", rd->argv[i]);
9286 fputc ('\n', fp);
9287 }
9288
9289 fflush (fp);
9290
9291 fsync (fileno (fp));
9292
9293 fclose (fp);
9294 }
9295
9296 void cycle_restore ()
9297 {
9298 const char *eff_restore_file = data.eff_restore_file;
9299 const char *new_restore_file = data.new_restore_file;
9300
9301 restore_data_t *rd = data.rd;
9302
9303 write_restore (new_restore_file, rd);
9304
9305 struct stat st;
9306
9307 memset (&st, 0, sizeof(st));
9308
9309 if (stat (eff_restore_file, &st) == 0)
9310 {
9311 if (unlink (eff_restore_file))
9312 {
9313 log_info ("WARN: unlink file '%s': %s", eff_restore_file, strerror (errno));
9314 }
9315 }
9316
9317 if (rename (new_restore_file, eff_restore_file))
9318 {
9319 log_info ("WARN: rename file '%s' to '%s': %s", new_restore_file, eff_restore_file, strerror (errno));
9320 }
9321 }
9322
9323 void check_checkpoint ()
9324 {
9325 // if (data.restore_disable == 1) break; (this is already implied by previous checks)
9326
9327 u64 words_cur = get_lowest_words_done ();
9328
9329 if (words_cur != data.checkpoint_cur_words)
9330 {
9331 myabort ();
9332 }
9333 }
9334
9335 /**
9336 * tuning db
9337 */
9338
9339 void tuning_db_destroy (tuning_db_t *tuning_db)
9340 {
9341 int i;
9342
9343 for (i = 0; i < tuning_db->alias_cnt; i++)
9344 {
9345 tuning_db_alias_t *alias = &tuning_db->alias_buf[i];
9346
9347 myfree (alias->device_name);
9348 myfree (alias->alias_name);
9349 }
9350
9351 for (i = 0; i < tuning_db->entry_cnt; i++)
9352 {
9353 tuning_db_entry_t *entry = &tuning_db->entry_buf[i];
9354
9355 myfree (entry->device_name);
9356 }
9357
9358 myfree (tuning_db->alias_buf);
9359 myfree (tuning_db->entry_buf);
9360
9361 myfree (tuning_db);
9362 }
9363
9364 tuning_db_t *tuning_db_alloc (FILE *fp)
9365 {
9366 tuning_db_t *tuning_db = (tuning_db_t *) mymalloc (sizeof (tuning_db_t));
9367
9368 int num_lines = count_lines (fp);
9369
9370 // a bit over-allocated
9371
9372 tuning_db->alias_buf = (tuning_db_alias_t *) mycalloc (num_lines + 1, sizeof (tuning_db_alias_t));
9373 tuning_db->alias_cnt = 0;
9374
9375 tuning_db->entry_buf = (tuning_db_entry_t *) mycalloc (num_lines + 1, sizeof (tuning_db_entry_t));
9376 tuning_db->entry_cnt = 0;
9377
9378 return tuning_db;
9379 }
9380
9381 tuning_db_t *tuning_db_init (const char *tuning_db_file)
9382 {
9383 FILE *fp = fopen (tuning_db_file, "rb");
9384
9385 if (fp == NULL)
9386 {
9387 log_error ("%s: %s", tuning_db_file, strerror (errno));
9388
9389 exit (-1);
9390 }
9391
9392 tuning_db_t *tuning_db = tuning_db_alloc (fp);
9393
9394 rewind (fp);
9395
9396 int line_num = 0;
9397
9398 char *buf = (char *) mymalloc (HCBUFSIZ);
9399
9400 while (!feof (fp))
9401 {
9402 char *line_buf = fgets (buf, HCBUFSIZ - 1, fp);
9403
9404 if (line_buf == NULL) break;
9405
9406 line_num++;
9407
9408 const int line_len = in_superchop (line_buf);
9409
9410 if (line_len == 0) continue;
9411
9412 if (line_buf[0] == '#') continue;
9413
9414 // start processing
9415
9416 char *token_ptr[7] = { NULL };
9417
9418 int token_cnt = 0;
9419
9420 char *next = strtok (line_buf, "\t ");
9421
9422 token_ptr[token_cnt] = next;
9423
9424 token_cnt++;
9425
9426 while ((next = strtok (NULL, "\t ")) != NULL)
9427 {
9428 token_ptr[token_cnt] = next;
9429
9430 token_cnt++;
9431 }
9432
9433 if (token_cnt == 2)
9434 {
9435 char *device_name = token_ptr[0];
9436 char *alias_name = token_ptr[1];
9437
9438 tuning_db_alias_t *alias = &tuning_db->alias_buf[tuning_db->alias_cnt];
9439
9440 alias->device_name = mystrdup (device_name);
9441 alias->alias_name = mystrdup (alias_name);
9442
9443 tuning_db->alias_cnt++;
9444 }
9445 else if (token_cnt == 6)
9446 {
9447 if ((token_ptr[1][0] != '0') &&
9448 (token_ptr[1][0] != '1') &&
9449 (token_ptr[1][0] != '3') &&
9450 (token_ptr[1][0] != '*'))
9451 {
9452 log_info ("WARNING: Tuning-db: Invalid attack_mode '%c' in Line '%u'", token_ptr[1][0], line_num);
9453
9454 continue;
9455 }
9456
9457 if ((token_ptr[3][0] != '1') &&
9458 (token_ptr[3][0] != '2') &&
9459 (token_ptr[3][0] != '4') &&
9460 (token_ptr[3][0] != '8') &&
9461 (token_ptr[3][0] != 'N'))
9462 {
9463 log_info ("WARNING: Tuning-db: Invalid vector_width '%c' in Line '%u'", token_ptr[3][0], line_num);
9464
9465 continue;
9466 }
9467
9468 char *device_name = token_ptr[0];
9469
9470 int attack_mode = -1;
9471 int hash_type = -1;
9472 int vector_width = -1;
9473 int kernel_accel = -1;
9474 int kernel_loops = -1;
9475
9476 if (token_ptr[1][0] != '*') attack_mode = atoi (token_ptr[1]);
9477 if (token_ptr[2][0] != '*') hash_type = atoi (token_ptr[2]);
9478 if (token_ptr[3][0] != 'N') vector_width = atoi (token_ptr[3]);
9479
9480 if (token_ptr[4][0] != 'A')
9481 {
9482 kernel_accel = atoi (token_ptr[4]);
9483
9484 if ((kernel_accel < 1) || (kernel_accel > 1024))
9485 {
9486 log_info ("WARNING: Tuning-db: Invalid kernel_accel '%d' in Line '%u'", kernel_accel, line_num);
9487
9488 continue;
9489 }
9490 }
9491 else
9492 {
9493 kernel_accel = 0;
9494 }
9495
9496 if (token_ptr[5][0] != 'A')
9497 {
9498 kernel_loops = atoi (token_ptr[5]);
9499
9500 if ((kernel_loops < 1) || (kernel_loops > 1024))
9501 {
9502 log_info ("WARNING: Tuning-db: Invalid kernel_loops '%d' in Line '%u'", kernel_loops, line_num);
9503
9504 continue;
9505 }
9506 }
9507 else
9508 {
9509 kernel_loops = 0;
9510 }
9511
9512 tuning_db_entry_t *entry = &tuning_db->entry_buf[tuning_db->entry_cnt];
9513
9514 entry->device_name = mystrdup (device_name);
9515 entry->attack_mode = attack_mode;
9516 entry->hash_type = hash_type;
9517 entry->vector_width = vector_width;
9518 entry->kernel_accel = kernel_accel;
9519 entry->kernel_loops = kernel_loops;
9520
9521 tuning_db->entry_cnt++;
9522 }
9523 else
9524 {
9525 log_info ("WARNING: Tuning-db: Invalid number of token in Line '%u'", line_num);
9526
9527 continue;
9528 }
9529 }
9530
9531 myfree (buf);
9532
9533 fclose (fp);
9534
9535 // todo: print loaded 'cnt' message
9536
9537 // sort the database
9538
9539 qsort (tuning_db->alias_buf, tuning_db->alias_cnt, sizeof (tuning_db_alias_t), sort_by_tuning_db_alias);
9540 qsort (tuning_db->entry_buf, tuning_db->entry_cnt, sizeof (tuning_db_entry_t), sort_by_tuning_db_entry);
9541
9542 return tuning_db;
9543 }
9544
9545 tuning_db_entry_t *tuning_db_search (tuning_db_t *tuning_db, hc_device_param_t *device_param, int attack_mode, int hash_type)
9546 {
9547 static tuning_db_entry_t s;
9548
9549 // first we need to convert all spaces in the device_name to underscore
9550
9551 char *device_name_nospace = strdup (device_param->device_name);
9552
9553 int device_name_length = strlen (device_name_nospace);
9554
9555 int i;
9556
9557 for (i = 0; i < device_name_length; i++)
9558 {
9559 if (device_name_nospace[i] == ' ') device_name_nospace[i] = '_';
9560 }
9561
9562 // find out if there's an alias configured
9563
9564 tuning_db_alias_t a;
9565
9566 a.device_name = device_name_nospace;
9567
9568 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);
9569
9570 char *alias_name = (alias == NULL) ? NULL : alias->alias_name;
9571
9572 // attack-mode 6 and 7 are attack-mode 1 basically
9573
9574 if (attack_mode == 6) attack_mode = 1;
9575 if (attack_mode == 7) attack_mode = 1;
9576
9577 // bsearch is not ideal but fast enough
9578
9579 s.device_name = device_name_nospace;
9580 s.attack_mode = attack_mode;
9581 s.hash_type = hash_type;
9582
9583 tuning_db_entry_t *entry = NULL;
9584
9585 // this will produce all 2^3 combinations required
9586
9587 for (i = 0; i < 8; i++)
9588 {
9589 s.device_name = (i & 1) ? "*" : device_name_nospace;
9590 s.attack_mode = (i & 2) ? -1 : attack_mode;
9591 s.hash_type = (i & 4) ? -1 : hash_type;
9592
9593 entry = bsearch (&s, tuning_db->entry_buf, tuning_db->entry_cnt, sizeof (tuning_db_entry_t), sort_by_tuning_db_entry);
9594
9595 if (entry != NULL) break;
9596
9597 // in non-wildcard mode do some additional checks:
9598
9599 if ((i & 1) == 0)
9600 {
9601 // in case we have an alias-name
9602
9603 if (alias_name != NULL)
9604 {
9605 s.device_name = alias_name;
9606
9607 entry = bsearch (&s, tuning_db->entry_buf, tuning_db->entry_cnt, sizeof (tuning_db_entry_t), sort_by_tuning_db_entry);
9608
9609 if (entry != NULL) break;
9610 }
9611
9612 // or by device type
9613
9614 if (device_param->device_type & CL_DEVICE_TYPE_CPU)
9615 {
9616 s.device_name = "DEVICE_TYPE_CPU";
9617 }
9618 else if (device_param->device_type & CL_DEVICE_TYPE_GPU)
9619 {
9620 s.device_name = "DEVICE_TYPE_GPU";
9621 }
9622 else if (device_param->device_type & CL_DEVICE_TYPE_ACCELERATOR)
9623 {
9624 s.device_name = "DEVICE_TYPE_ACCELERATOR";
9625 }
9626
9627 entry = bsearch (&s, tuning_db->entry_buf, tuning_db->entry_cnt, sizeof (tuning_db_entry_t), sort_by_tuning_db_entry);
9628
9629 if (entry != NULL) break;
9630 }
9631 }
9632
9633 // free converted device_name
9634
9635 myfree (device_name_nospace);
9636
9637 return entry;
9638 }
9639
9640 /**
9641 * parser
9642 */
9643
9644 uint parse_and_store_salt (char *out, char *in, uint salt_len)
9645 {
9646 u8 tmp[256] = { 0 };
9647
9648 if (salt_len > sizeof (tmp))
9649 {
9650 return UINT_MAX;
9651 }
9652
9653 memcpy (tmp, in, salt_len);
9654
9655 if (data.opts_type & OPTS_TYPE_ST_HEX)
9656 {
9657 if ((salt_len % 2) == 0)
9658 {
9659 u32 new_salt_len = salt_len / 2;
9660
9661 for (uint i = 0, j = 0; i < new_salt_len; i += 1, j += 2)
9662 {
9663 u8 p0 = tmp[j + 0];
9664 u8 p1 = tmp[j + 1];
9665
9666 tmp[i] = hex_convert (p1) << 0;
9667 tmp[i] |= hex_convert (p0) << 4;
9668 }
9669
9670 salt_len = new_salt_len;
9671 }
9672 else
9673 {
9674 return UINT_MAX;
9675 }
9676 }
9677 else if (data.opts_type & OPTS_TYPE_ST_BASE64)
9678 {
9679 salt_len = base64_decode (base64_to_int, (const u8 *) in, salt_len, (u8 *) tmp);
9680 }
9681
9682 memset (tmp + salt_len, 0, sizeof (tmp) - salt_len);
9683
9684 if (data.opts_type & OPTS_TYPE_ST_UNICODE)
9685 {
9686 if (salt_len < 20)
9687 {
9688 u32 *tmp_uint = (u32 *) tmp;
9689
9690 tmp_uint[9] = ((tmp_uint[4] >> 8) & 0x00FF0000) | ((tmp_uint[4] >> 16) & 0x000000FF);
9691 tmp_uint[8] = ((tmp_uint[4] << 8) & 0x00FF0000) | ((tmp_uint[4] >> 0) & 0x000000FF);
9692 tmp_uint[7] = ((tmp_uint[3] >> 8) & 0x00FF0000) | ((tmp_uint[3] >> 16) & 0x000000FF);
9693 tmp_uint[6] = ((tmp_uint[3] << 8) & 0x00FF0000) | ((tmp_uint[3] >> 0) & 0x000000FF);
9694 tmp_uint[5] = ((tmp_uint[2] >> 8) & 0x00FF0000) | ((tmp_uint[2] >> 16) & 0x000000FF);
9695 tmp_uint[4] = ((tmp_uint[2] << 8) & 0x00FF0000) | ((tmp_uint[2] >> 0) & 0x000000FF);
9696 tmp_uint[3] = ((tmp_uint[1] >> 8) & 0x00FF0000) | ((tmp_uint[1] >> 16) & 0x000000FF);
9697 tmp_uint[2] = ((tmp_uint[1] << 8) & 0x00FF0000) | ((tmp_uint[1] >> 0) & 0x000000FF);
9698 tmp_uint[1] = ((tmp_uint[0] >> 8) & 0x00FF0000) | ((tmp_uint[0] >> 16) & 0x000000FF);
9699 tmp_uint[0] = ((tmp_uint[0] << 8) & 0x00FF0000) | ((tmp_uint[0] >> 0) & 0x000000FF);
9700
9701 salt_len = salt_len * 2;
9702 }
9703 else
9704 {
9705 return UINT_MAX;
9706 }
9707 }
9708
9709 if (data.opts_type & OPTS_TYPE_ST_LOWER)
9710 {
9711 lowercase (tmp, salt_len);
9712 }
9713
9714 if (data.opts_type & OPTS_TYPE_ST_UPPER)
9715 {
9716 uppercase (tmp, salt_len);
9717 }
9718
9719 u32 len = salt_len;
9720
9721 if (data.opts_type & OPTS_TYPE_ST_ADD80)
9722 {
9723 tmp[len++] = 0x80;
9724 }
9725
9726 if (data.opts_type & OPTS_TYPE_ST_ADD01)
9727 {
9728 tmp[len++] = 0x01;
9729 }
9730
9731 if (data.opts_type & OPTS_TYPE_ST_GENERATE_LE)
9732 {
9733 u32 *tmp_uint = (uint *) tmp;
9734
9735 u32 max = len / 4;
9736
9737 if (len % 4) max++;
9738
9739 for (u32 i = 0; i < max; i++)
9740 {
9741 tmp_uint[i] = byte_swap_32 (tmp_uint[i]);
9742 }
9743
9744 // Important: we may need to increase the length of memcpy since
9745 // we don't want to "loose" some swapped bytes (could happen if
9746 // they do not perfectly fit in the 4-byte blocks)
9747 // Memcpy does always copy the bytes in the BE order, but since
9748 // we swapped them, some important bytes could be in positions
9749 // we normally skip with the original len
9750
9751 if (len % 4) len += 4 - (len % 4);
9752 }
9753
9754 memcpy (out, tmp, len);
9755
9756 return (salt_len);
9757 }
9758
9759 int bcrypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
9760 {
9761 if ((input_len < DISPLAY_LEN_MIN_3200) || (input_len > DISPLAY_LEN_MAX_3200)) return (PARSER_GLOBAL_LENGTH);
9762
9763 if ((memcmp (SIGNATURE_BCRYPT1, input_buf, 4)) && (memcmp (SIGNATURE_BCRYPT2, input_buf, 4)) && (memcmp (SIGNATURE_BCRYPT3, input_buf, 4))) return (PARSER_SIGNATURE_UNMATCHED);
9764
9765 u32 *digest = (u32 *) hash_buf->digest;
9766
9767 salt_t *salt = hash_buf->salt;
9768
9769 memcpy ((char *) salt->salt_sign, input_buf, 6);
9770
9771 char *iter_pos = input_buf + 4;
9772
9773 salt->salt_iter = 1 << atoi (iter_pos);
9774
9775 char *salt_pos = strchr (iter_pos, '$');
9776
9777 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
9778
9779 salt_pos++;
9780
9781 uint salt_len = 16;
9782
9783 salt->salt_len = salt_len;
9784
9785 u8 tmp_buf[100] = { 0 };
9786
9787 base64_decode (bf64_to_int, (const u8 *) salt_pos, 22, tmp_buf);
9788
9789 char *salt_buf_ptr = (char *) salt->salt_buf;
9790
9791 memcpy (salt_buf_ptr, tmp_buf, 16);
9792
9793 salt->salt_buf[0] = byte_swap_32 (salt->salt_buf[0]);
9794 salt->salt_buf[1] = byte_swap_32 (salt->salt_buf[1]);
9795 salt->salt_buf[2] = byte_swap_32 (salt->salt_buf[2]);
9796 salt->salt_buf[3] = byte_swap_32 (salt->salt_buf[3]);
9797
9798 char *hash_pos = salt_pos + 22;
9799
9800 memset (tmp_buf, 0, sizeof (tmp_buf));
9801
9802 base64_decode (bf64_to_int, (const u8 *) hash_pos, 31, tmp_buf);
9803
9804 memcpy (digest, tmp_buf, 24);
9805
9806 digest[0] = byte_swap_32 (digest[0]);
9807 digest[1] = byte_swap_32 (digest[1]);
9808 digest[2] = byte_swap_32 (digest[2]);
9809 digest[3] = byte_swap_32 (digest[3]);
9810 digest[4] = byte_swap_32 (digest[4]);
9811 digest[5] = byte_swap_32 (digest[5]);
9812
9813 digest[5] &= ~0xff; // its just 23 not 24 !
9814
9815 return (PARSER_OK);
9816 }
9817
9818 int cisco4_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
9819 {
9820 if ((input_len < DISPLAY_LEN_MIN_5700) || (input_len > DISPLAY_LEN_MAX_5700)) return (PARSER_GLOBAL_LENGTH);
9821
9822 u32 *digest = (u32 *) hash_buf->digest;
9823
9824 u8 tmp_buf[100] = { 0 };
9825
9826 base64_decode (itoa64_to_int, (const u8 *) input_buf, 43, tmp_buf);
9827
9828 memcpy (digest, tmp_buf, 32);
9829
9830 digest[0] = byte_swap_32 (digest[0]);
9831 digest[1] = byte_swap_32 (digest[1]);
9832 digest[2] = byte_swap_32 (digest[2]);
9833 digest[3] = byte_swap_32 (digest[3]);
9834 digest[4] = byte_swap_32 (digest[4]);
9835 digest[5] = byte_swap_32 (digest[5]);
9836 digest[6] = byte_swap_32 (digest[6]);
9837 digest[7] = byte_swap_32 (digest[7]);
9838
9839 digest[0] -= SHA256M_A;
9840 digest[1] -= SHA256M_B;
9841 digest[2] -= SHA256M_C;
9842 digest[3] -= SHA256M_D;
9843 digest[4] -= SHA256M_E;
9844 digest[5] -= SHA256M_F;
9845 digest[6] -= SHA256M_G;
9846 digest[7] -= SHA256M_H;
9847
9848 return (PARSER_OK);
9849 }
9850
9851 int lm_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
9852 {
9853 if ((input_len < DISPLAY_LEN_MIN_3000) || (input_len > DISPLAY_LEN_MAX_3000)) return (PARSER_GLOBAL_LENGTH);
9854
9855 u32 *digest = (u32 *) hash_buf->digest;
9856
9857 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
9858 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
9859
9860 digest[0] = byte_swap_32 (digest[0]);
9861 digest[1] = byte_swap_32 (digest[1]);
9862
9863 uint tt;
9864
9865 IP (digest[0], digest[1], tt);
9866
9867 digest[0] = digest[0];
9868 digest[1] = digest[1];
9869 digest[2] = 0;
9870 digest[3] = 0;
9871
9872 return (PARSER_OK);
9873 }
9874
9875 int arubaos_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
9876 {
9877 if ((input_len < DISPLAY_LEN_MIN_125) || (input_len > DISPLAY_LEN_MAX_125)) return (PARSER_GLOBAL_LENGTH);
9878
9879 if ((input_buf[8] != '0') || (input_buf[9] != '1')) return (PARSER_SIGNATURE_UNMATCHED);
9880
9881 u32 *digest = (u32 *) hash_buf->digest;
9882
9883 salt_t *salt = hash_buf->salt;
9884
9885 char *hash_pos = input_buf + 10;
9886
9887 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
9888 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
9889 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
9890 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
9891 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
9892
9893 digest[0] -= SHA1M_A;
9894 digest[1] -= SHA1M_B;
9895 digest[2] -= SHA1M_C;
9896 digest[3] -= SHA1M_D;
9897 digest[4] -= SHA1M_E;
9898
9899 uint salt_len = 10;
9900
9901 char *salt_buf_ptr = (char *) salt->salt_buf;
9902
9903 salt_len = parse_and_store_salt (salt_buf_ptr, input_buf, salt_len);
9904
9905 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
9906
9907 salt->salt_len = salt_len;
9908
9909 return (PARSER_OK);
9910 }
9911
9912 int osx1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
9913 {
9914 if ((input_len < DISPLAY_LEN_MIN_122) || (input_len > DISPLAY_LEN_MAX_122)) return (PARSER_GLOBAL_LENGTH);
9915
9916 u32 *digest = (u32 *) hash_buf->digest;
9917
9918 salt_t *salt = hash_buf->salt;
9919
9920 char *hash_pos = input_buf + 8;
9921
9922 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
9923 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
9924 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
9925 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
9926 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
9927
9928 digest[0] -= SHA1M_A;
9929 digest[1] -= SHA1M_B;
9930 digest[2] -= SHA1M_C;
9931 digest[3] -= SHA1M_D;
9932 digest[4] -= SHA1M_E;
9933
9934 uint salt_len = 8;
9935
9936 char *salt_buf_ptr = (char *) salt->salt_buf;
9937
9938 salt_len = parse_and_store_salt (salt_buf_ptr, input_buf, salt_len);
9939
9940 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
9941
9942 salt->salt_len = salt_len;
9943
9944 return (PARSER_OK);
9945 }
9946
9947 int osx512_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
9948 {
9949 if ((input_len < DISPLAY_LEN_MIN_1722) || (input_len > DISPLAY_LEN_MAX_1722)) return (PARSER_GLOBAL_LENGTH);
9950
9951 u64 *digest = (u64 *) hash_buf->digest;
9952
9953 salt_t *salt = hash_buf->salt;
9954
9955 char *hash_pos = input_buf + 8;
9956
9957 digest[0] = hex_to_u64 ((const u8 *) &hash_pos[ 0]);
9958 digest[1] = hex_to_u64 ((const u8 *) &hash_pos[ 16]);
9959 digest[2] = hex_to_u64 ((const u8 *) &hash_pos[ 32]);
9960 digest[3] = hex_to_u64 ((const u8 *) &hash_pos[ 48]);
9961 digest[4] = hex_to_u64 ((const u8 *) &hash_pos[ 64]);
9962 digest[5] = hex_to_u64 ((const u8 *) &hash_pos[ 80]);
9963 digest[6] = hex_to_u64 ((const u8 *) &hash_pos[ 96]);
9964 digest[7] = hex_to_u64 ((const u8 *) &hash_pos[112]);
9965
9966 digest[0] -= SHA512M_A;
9967 digest[1] -= SHA512M_B;
9968 digest[2] -= SHA512M_C;
9969 digest[3] -= SHA512M_D;
9970 digest[4] -= SHA512M_E;
9971 digest[5] -= SHA512M_F;
9972 digest[6] -= SHA512M_G;
9973 digest[7] -= SHA512M_H;
9974
9975 uint salt_len = 8;
9976
9977 char *salt_buf_ptr = (char *) salt->salt_buf;
9978
9979 salt_len = parse_and_store_salt (salt_buf_ptr, input_buf, salt_len);
9980
9981 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
9982
9983 salt->salt_len = salt_len;
9984
9985 return (PARSER_OK);
9986 }
9987
9988 int osc_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
9989 {
9990 if (data.opts_type & OPTS_TYPE_ST_HEX)
9991 {
9992 if ((input_len < DISPLAY_LEN_MIN_21H) || (input_len > DISPLAY_LEN_MAX_21H)) return (PARSER_GLOBAL_LENGTH);
9993 }
9994 else
9995 {
9996 if ((input_len < DISPLAY_LEN_MIN_21) || (input_len > DISPLAY_LEN_MAX_21)) return (PARSER_GLOBAL_LENGTH);
9997 }
9998
9999 u32 *digest = (u32 *) hash_buf->digest;
10000
10001 salt_t *salt = hash_buf->salt;
10002
10003 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
10004 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
10005 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
10006 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
10007
10008 digest[0] = byte_swap_32 (digest[0]);
10009 digest[1] = byte_swap_32 (digest[1]);
10010 digest[2] = byte_swap_32 (digest[2]);
10011 digest[3] = byte_swap_32 (digest[3]);
10012
10013 digest[0] -= MD5M_A;
10014 digest[1] -= MD5M_B;
10015 digest[2] -= MD5M_C;
10016 digest[3] -= MD5M_D;
10017
10018 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
10019
10020 uint salt_len = input_len - 32 - 1;
10021
10022 char *salt_buf = input_buf + 32 + 1;
10023
10024 char *salt_buf_ptr = (char *) salt->salt_buf;
10025
10026 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
10027
10028 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
10029
10030 salt->salt_len = salt_len;
10031
10032 return (PARSER_OK);
10033 }
10034
10035 int netscreen_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10036 {
10037 if (data.opts_type & OPTS_TYPE_ST_HEX)
10038 {
10039 if ((input_len < DISPLAY_LEN_MIN_22H) || (input_len > DISPLAY_LEN_MAX_22H)) return (PARSER_GLOBAL_LENGTH);
10040 }
10041 else
10042 {
10043 if ((input_len < DISPLAY_LEN_MIN_22) || (input_len > DISPLAY_LEN_MAX_22)) return (PARSER_GLOBAL_LENGTH);
10044 }
10045
10046 // unscramble
10047
10048 char clean_input_buf[32] = { 0 };
10049
10050 char sig[6] = { 'n', 'r', 'c', 's', 't', 'n' };
10051 int pos[6] = { 0, 6, 12, 17, 23, 29 };
10052
10053 for (int i = 0, j = 0, k = 0; i < 30; i++)
10054 {
10055 if (i == pos[j])
10056 {
10057 if (sig[j] != input_buf[i]) return (PARSER_SIGNATURE_UNMATCHED);
10058
10059 j++;
10060 }
10061 else
10062 {
10063 clean_input_buf[k] = input_buf[i];
10064
10065 k++;
10066 }
10067 }
10068
10069 // base64 decode
10070
10071 u32 *digest = (u32 *) hash_buf->digest;
10072
10073 salt_t *salt = hash_buf->salt;
10074
10075 u32 a, b, c, d, e, f;
10076
10077 a = base64_to_int (clean_input_buf[ 0] & 0x7f);
10078 b = base64_to_int (clean_input_buf[ 1] & 0x7f);
10079 c = base64_to_int (clean_input_buf[ 2] & 0x7f);
10080 d = base64_to_int (clean_input_buf[ 3] & 0x7f);
10081 e = base64_to_int (clean_input_buf[ 4] & 0x7f);
10082 f = base64_to_int (clean_input_buf[ 5] & 0x7f);
10083
10084 digest[0] = (((a << 12) | (b << 6) | (c)) << 16)
10085 | (((d << 12) | (e << 6) | (f)) << 0);
10086
10087 a = base64_to_int (clean_input_buf[ 6] & 0x7f);
10088 b = base64_to_int (clean_input_buf[ 7] & 0x7f);
10089 c = base64_to_int (clean_input_buf[ 8] & 0x7f);
10090 d = base64_to_int (clean_input_buf[ 9] & 0x7f);
10091 e = base64_to_int (clean_input_buf[10] & 0x7f);
10092 f = base64_to_int (clean_input_buf[11] & 0x7f);
10093
10094 digest[1] = (((a << 12) | (b << 6) | (c)) << 16)
10095 | (((d << 12) | (e << 6) | (f)) << 0);
10096
10097 a = base64_to_int (clean_input_buf[12] & 0x7f);
10098 b = base64_to_int (clean_input_buf[13] & 0x7f);
10099 c = base64_to_int (clean_input_buf[14] & 0x7f);
10100 d = base64_to_int (clean_input_buf[15] & 0x7f);
10101 e = base64_to_int (clean_input_buf[16] & 0x7f);
10102 f = base64_to_int (clean_input_buf[17] & 0x7f);
10103
10104 digest[2] = (((a << 12) | (b << 6) | (c)) << 16)
10105 | (((d << 12) | (e << 6) | (f)) << 0);
10106
10107 a = base64_to_int (clean_input_buf[18] & 0x7f);
10108 b = base64_to_int (clean_input_buf[19] & 0x7f);
10109 c = base64_to_int (clean_input_buf[20] & 0x7f);
10110 d = base64_to_int (clean_input_buf[21] & 0x7f);
10111 e = base64_to_int (clean_input_buf[22] & 0x7f);
10112 f = base64_to_int (clean_input_buf[23] & 0x7f);
10113
10114 digest[3] = (((a << 12) | (b << 6) | (c)) << 16)
10115 | (((d << 12) | (e << 6) | (f)) << 0);
10116
10117 digest[0] = byte_swap_32 (digest[0]);
10118 digest[1] = byte_swap_32 (digest[1]);
10119 digest[2] = byte_swap_32 (digest[2]);
10120 digest[3] = byte_swap_32 (digest[3]);
10121
10122 digest[0] -= MD5M_A;
10123 digest[1] -= MD5M_B;
10124 digest[2] -= MD5M_C;
10125 digest[3] -= MD5M_D;
10126
10127 if (input_buf[30] != ':') return (PARSER_SEPARATOR_UNMATCHED);
10128
10129 uint salt_len = input_len - 30 - 1;
10130
10131 char *salt_buf = input_buf + 30 + 1;
10132
10133 char *salt_buf_ptr = (char *) salt->salt_buf;
10134
10135 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
10136
10137 // max. salt length: 55 (max for MD5) - 22 (":Administration Tools:") - 1 (0x80) = 32
10138 // 32 - 4 bytes (to fit w0lr for all attack modes) = 28
10139
10140 if (salt_len > 28) return (PARSER_SALT_LENGTH);
10141
10142 salt->salt_len = salt_len;
10143
10144 memcpy (salt_buf_ptr + salt_len, ":Administration Tools:", 22);
10145
10146 salt->salt_len += 22;
10147
10148 return (PARSER_OK);
10149 }
10150
10151 int smf_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10152 {
10153 if (data.opts_type & OPTS_TYPE_ST_HEX)
10154 {
10155 if ((input_len < DISPLAY_LEN_MIN_121H) || (input_len > DISPLAY_LEN_MAX_121H)) return (PARSER_GLOBAL_LENGTH);
10156 }
10157 else
10158 {
10159 if ((input_len < DISPLAY_LEN_MIN_121) || (input_len > DISPLAY_LEN_MAX_121)) return (PARSER_GLOBAL_LENGTH);
10160 }
10161
10162 u32 *digest = (u32 *) hash_buf->digest;
10163
10164 salt_t *salt = hash_buf->salt;
10165
10166 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
10167 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
10168 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
10169 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
10170 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
10171
10172 digest[0] -= SHA1M_A;
10173 digest[1] -= SHA1M_B;
10174 digest[2] -= SHA1M_C;
10175 digest[3] -= SHA1M_D;
10176 digest[4] -= SHA1M_E;
10177
10178 if (input_buf[40] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
10179
10180 uint salt_len = input_len - 40 - 1;
10181
10182 char *salt_buf = input_buf + 40 + 1;
10183
10184 char *salt_buf_ptr = (char *) salt->salt_buf;
10185
10186 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
10187
10188 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
10189
10190 salt->salt_len = salt_len;
10191
10192 return (PARSER_OK);
10193 }
10194
10195 int dcc2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10196 {
10197 if (data.opts_type & OPTS_TYPE_ST_HEX)
10198 {
10199 if ((input_len < DISPLAY_LEN_MIN_2100H) || (input_len > DISPLAY_LEN_MAX_2100H)) return (PARSER_GLOBAL_LENGTH);
10200 }
10201 else
10202 {
10203 if ((input_len < DISPLAY_LEN_MIN_2100) || (input_len > DISPLAY_LEN_MAX_2100)) return (PARSER_GLOBAL_LENGTH);
10204 }
10205
10206 if (memcmp (SIGNATURE_DCC2, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
10207
10208 char *iter_pos = input_buf + 6;
10209
10210 salt_t *salt = hash_buf->salt;
10211
10212 uint iter = atoi (iter_pos);
10213
10214 if (iter < 1)
10215 {
10216 iter = ROUNDS_DCC2;
10217 }
10218
10219 salt->salt_iter = iter - 1;
10220
10221 char *salt_pos = strchr (iter_pos, '#');
10222
10223 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
10224
10225 salt_pos++;
10226
10227 char *digest_pos = strchr (salt_pos, '#');
10228
10229 if (digest_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
10230
10231 digest_pos++;
10232
10233 uint salt_len = digest_pos - salt_pos - 1;
10234
10235 u32 *digest = (u32 *) hash_buf->digest;
10236
10237 digest[0] = hex_to_u32 ((const u8 *) &digest_pos[ 0]);
10238 digest[1] = hex_to_u32 ((const u8 *) &digest_pos[ 8]);
10239 digest[2] = hex_to_u32 ((const u8 *) &digest_pos[16]);
10240 digest[3] = hex_to_u32 ((const u8 *) &digest_pos[24]);
10241
10242 char *salt_buf_ptr = (char *) salt->salt_buf;
10243
10244 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
10245
10246 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
10247
10248 salt->salt_len = salt_len;
10249
10250 return (PARSER_OK);
10251 }
10252
10253 int wpa_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10254 {
10255 u32 *digest = (u32 *) hash_buf->digest;
10256
10257 salt_t *salt = hash_buf->salt;
10258
10259 wpa_t *wpa = (wpa_t *) hash_buf->esalt;
10260
10261 hccap_t in;
10262
10263 memcpy (&in, input_buf, input_len);
10264
10265 if (in.eapol_size < 1 || in.eapol_size > 255) return (PARSER_HCCAP_EAPOL_SIZE);
10266
10267 memcpy (digest, in.keymic, 16);
10268
10269 /*
10270 http://www.one-net.eu/jsw/j_sec/m_ptype.html
10271 The phrase "Pairwise key expansion"
10272 Access Point Address (referred to as Authenticator Address AA)
10273 Supplicant Address (referred to as Supplicant Address SA)
10274 Access Point Nonce (referred to as Authenticator Anonce)
10275 Wireless Device Nonce (referred to as Supplicant Nonce Snonce)
10276 */
10277
10278 uint salt_len = strlen (in.essid);
10279
10280 if (salt_len > 36)
10281 {
10282 log_info ("WARNING: the length of the ESSID is too long. The hccap file may be invalid or corrupted");
10283
10284 return (PARSER_SALT_LENGTH);
10285 }
10286
10287 memcpy (salt->salt_buf, in.essid, salt_len);
10288
10289 salt->salt_len = salt_len;
10290
10291 salt->salt_iter = ROUNDS_WPA2 - 1;
10292
10293 unsigned char *pke_ptr = (unsigned char *) wpa->pke;
10294
10295 memcpy (pke_ptr, "Pairwise key expansion", 23);
10296
10297 if (memcmp (in.mac1, in.mac2, 6) < 0)
10298 {
10299 memcpy (pke_ptr + 23, in.mac1, 6);
10300 memcpy (pke_ptr + 29, in.mac2, 6);
10301 }
10302 else
10303 {
10304 memcpy (pke_ptr + 23, in.mac2, 6);
10305 memcpy (pke_ptr + 29, in.mac1, 6);
10306 }
10307
10308 if (memcmp (in.nonce1, in.nonce2, 32) < 0)
10309 {
10310 memcpy (pke_ptr + 35, in.nonce1, 32);
10311 memcpy (pke_ptr + 67, in.nonce2, 32);
10312 }
10313 else
10314 {
10315 memcpy (pke_ptr + 35, in.nonce2, 32);
10316 memcpy (pke_ptr + 67, in.nonce1, 32);
10317 }
10318
10319 for (int i = 0; i < 25; i++)
10320 {
10321 wpa->pke[i] = byte_swap_32 (wpa->pke[i]);
10322 }
10323
10324 memcpy (wpa->orig_mac1, in.mac1, 6);
10325 memcpy (wpa->orig_mac2, in.mac2, 6);
10326 memcpy (wpa->orig_nonce1, in.nonce1, 32);
10327 memcpy (wpa->orig_nonce2, in.nonce2, 32);
10328
10329 wpa->keyver = in.keyver;
10330
10331 if (wpa->keyver > 255)
10332 {
10333 log_info ("ATTENTION!");
10334 log_info (" The WPA/WPA2 key version in your .hccap file is invalid!");
10335 log_info (" This could be due to a recent aircrack-ng bug.");
10336 log_info (" The key version was automatically reset to a reasonable value.");
10337 log_info ("");
10338
10339 wpa->keyver &= 0xff;
10340 }
10341
10342 wpa->eapol_size = in.eapol_size;
10343
10344 unsigned char *eapol_ptr = (unsigned char *) wpa->eapol;
10345
10346 memcpy (eapol_ptr, in.eapol, wpa->eapol_size);
10347
10348 memset (eapol_ptr + wpa->eapol_size, 0, 256 - wpa->eapol_size);
10349
10350 eapol_ptr[wpa->eapol_size] = (unsigned char) 0x80;
10351
10352 if (wpa->keyver == 1)
10353 {
10354 // nothing to do
10355 }
10356 else
10357 {
10358 digest[0] = byte_swap_32 (digest[0]);
10359 digest[1] = byte_swap_32 (digest[1]);
10360 digest[2] = byte_swap_32 (digest[2]);
10361 digest[3] = byte_swap_32 (digest[3]);
10362
10363 for (int i = 0; i < 64; i++)
10364 {
10365 wpa->eapol[i] = byte_swap_32 (wpa->eapol[i]);
10366 }
10367 }
10368
10369 uint32_t *p0 = (uint32_t *) in.essid;
10370 uint32_t c0 = 0;
10371 uint32_t c1 = 0;
10372
10373 for (uint i = 0; i < sizeof (in.essid) / sizeof (uint32_t); i++) c0 ^= *p0++;
10374 for (uint i = 0; i < sizeof (wpa->pke) / sizeof (wpa->pke[0]); i++) c1 ^= wpa->pke[i];
10375
10376 salt->salt_buf[10] = c0;
10377 salt->salt_buf[11] = c1;
10378
10379 return (PARSER_OK);
10380 }
10381
10382 int psafe2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10383 {
10384 u32 *digest = (u32 *) hash_buf->digest;
10385
10386 salt_t *salt = hash_buf->salt;
10387
10388 if (input_len == 0)
10389 {
10390 log_error ("Password Safe v2 container not specified");
10391
10392 exit (-1);
10393 }
10394
10395 FILE *fp = fopen (input_buf, "rb");
10396
10397 if (fp == NULL)
10398 {
10399 log_error ("%s: %s", input_buf, strerror (errno));
10400
10401 exit (-1);
10402 }
10403
10404 psafe2_hdr buf;
10405
10406 memset (&buf, 0, sizeof (psafe2_hdr));
10407
10408 int n = fread (&buf, sizeof (psafe2_hdr), 1, fp);
10409
10410 fclose (fp);
10411
10412 if (n != 1) return (PARSER_PSAFE2_FILE_SIZE);
10413
10414 salt->salt_buf[0] = buf.random[0];
10415 salt->salt_buf[1] = buf.random[1];
10416
10417 salt->salt_len = 8;
10418 salt->salt_iter = 1000;
10419
10420 digest[0] = byte_swap_32 (buf.hash[0]);
10421 digest[1] = byte_swap_32 (buf.hash[1]);
10422 digest[2] = byte_swap_32 (buf.hash[2]);
10423 digest[3] = byte_swap_32 (buf.hash[3]);
10424 digest[4] = byte_swap_32 (buf.hash[4]);
10425
10426 return (PARSER_OK);
10427 }
10428
10429 int psafe3_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10430 {
10431 u32 *digest = (u32 *) hash_buf->digest;
10432
10433 salt_t *salt = hash_buf->salt;
10434
10435 if (input_len == 0)
10436 {
10437 log_error (".psafe3 not specified");
10438
10439 exit (-1);
10440 }
10441
10442 FILE *fp = fopen (input_buf, "rb");
10443
10444 if (fp == NULL)
10445 {
10446 log_error ("%s: %s", input_buf, strerror (errno));
10447
10448 exit (-1);
10449 }
10450
10451 psafe3_t in;
10452
10453 int n = fread (&in, sizeof (psafe3_t), 1, fp);
10454
10455 fclose (fp);
10456
10457 data.hashfile = input_buf; // we will need this in case it gets cracked
10458
10459 if (memcmp (SIGNATURE_PSAFE3, in.signature, 4)) return (PARSER_SIGNATURE_UNMATCHED);
10460
10461 if (n != 1) return (PARSER_PSAFE3_FILE_SIZE);
10462
10463 salt->salt_iter = in.iterations + 1;
10464
10465 salt->salt_buf[0] = in.salt_buf[0];
10466 salt->salt_buf[1] = in.salt_buf[1];
10467 salt->salt_buf[2] = in.salt_buf[2];
10468 salt->salt_buf[3] = in.salt_buf[3];
10469 salt->salt_buf[4] = in.salt_buf[4];
10470 salt->salt_buf[5] = in.salt_buf[5];
10471 salt->salt_buf[6] = in.salt_buf[6];
10472 salt->salt_buf[7] = in.salt_buf[7];
10473
10474 salt->salt_len = 32;
10475
10476 digest[0] = in.hash_buf[0];
10477 digest[1] = in.hash_buf[1];
10478 digest[2] = in.hash_buf[2];
10479 digest[3] = in.hash_buf[3];
10480 digest[4] = in.hash_buf[4];
10481 digest[5] = in.hash_buf[5];
10482 digest[6] = in.hash_buf[6];
10483 digest[7] = in.hash_buf[7];
10484
10485 digest[0] = byte_swap_32 (digest[0]);
10486 digest[1] = byte_swap_32 (digest[1]);
10487 digest[2] = byte_swap_32 (digest[2]);
10488 digest[3] = byte_swap_32 (digest[3]);
10489 digest[4] = byte_swap_32 (digest[4]);
10490 digest[5] = byte_swap_32 (digest[5]);
10491 digest[6] = byte_swap_32 (digest[6]);
10492 digest[7] = byte_swap_32 (digest[7]);
10493
10494 return (PARSER_OK);
10495 }
10496
10497 int phpass_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10498 {
10499 if ((input_len < DISPLAY_LEN_MIN_400) || (input_len > DISPLAY_LEN_MAX_400)) return (PARSER_GLOBAL_LENGTH);
10500
10501 if ((memcmp (SIGNATURE_PHPASS1, input_buf, 3)) && (memcmp (SIGNATURE_PHPASS2, input_buf, 3))) return (PARSER_SIGNATURE_UNMATCHED);
10502
10503 u32 *digest = (u32 *) hash_buf->digest;
10504
10505 salt_t *salt = hash_buf->salt;
10506
10507 char *iter_pos = input_buf + 3;
10508
10509 uint salt_iter = 1 << itoa64_to_int (iter_pos[0]);
10510
10511 if (salt_iter > 0x80000000) return (PARSER_SALT_ITERATION);
10512
10513 memcpy ((char *) salt->salt_sign, input_buf, 4);
10514
10515 salt->salt_iter = salt_iter;
10516
10517 char *salt_pos = iter_pos + 1;
10518
10519 uint salt_len = 8;
10520
10521 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
10522
10523 salt->salt_len = salt_len;
10524
10525 char *hash_pos = salt_pos + salt_len;
10526
10527 phpass_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
10528
10529 return (PARSER_OK);
10530 }
10531
10532 int md5crypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10533 {
10534 if (input_len < DISPLAY_LEN_MIN_500) return (PARSER_GLOBAL_LENGTH);
10535
10536 if (memcmp (SIGNATURE_MD5CRYPT, input_buf, 3)) return (PARSER_SIGNATURE_UNMATCHED);
10537
10538 u32 *digest = (u32 *) hash_buf->digest;
10539
10540 salt_t *salt = hash_buf->salt;
10541
10542 char *salt_pos = input_buf + 3;
10543
10544 uint iterations_len = 0;
10545
10546 if (memcmp (salt_pos, "rounds=", 7) == 0)
10547 {
10548 salt_pos += 7;
10549
10550 for (iterations_len = 0; salt_pos[0] >= '0' && salt_pos[0] <= '9' && iterations_len < 7; iterations_len++, salt_pos += 1) continue;
10551
10552 if (iterations_len == 0 ) return (PARSER_SALT_ITERATION);
10553 if (salt_pos[0] != '$') return (PARSER_SIGNATURE_UNMATCHED);
10554
10555 salt_pos[0] = 0x0;
10556
10557 salt->salt_iter = atoi (salt_pos - iterations_len);
10558
10559 salt_pos += 1;
10560
10561 iterations_len += 8;
10562 }
10563 else
10564 {
10565 salt->salt_iter = ROUNDS_MD5CRYPT;
10566 }
10567
10568 if (input_len > (DISPLAY_LEN_MAX_500 + iterations_len)) return (PARSER_GLOBAL_LENGTH);
10569
10570 char *hash_pos = strchr (salt_pos, '$');
10571
10572 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
10573
10574 uint salt_len = hash_pos - salt_pos;
10575
10576 if (salt_len > 8) return (PARSER_SALT_LENGTH);
10577
10578 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
10579
10580 salt->salt_len = salt_len;
10581
10582 hash_pos++;
10583
10584 uint hash_len = input_len - 3 - iterations_len - salt_len - 1;
10585
10586 if (hash_len != 22) return (PARSER_HASH_LENGTH);
10587
10588 md5crypt_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
10589
10590 return (PARSER_OK);
10591 }
10592
10593 int md5apr1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10594 {
10595 if (memcmp (SIGNATURE_MD5APR1, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
10596
10597 u32 *digest = (u32 *) hash_buf->digest;
10598
10599 salt_t *salt = hash_buf->salt;
10600
10601 char *salt_pos = input_buf + 6;
10602
10603 uint iterations_len = 0;
10604
10605 if (memcmp (salt_pos, "rounds=", 7) == 0)
10606 {
10607 salt_pos += 7;
10608
10609 for (iterations_len = 0; salt_pos[0] >= '0' && salt_pos[0] <= '9' && iterations_len < 7; iterations_len++, salt_pos += 1) continue;
10610
10611 if (iterations_len == 0 ) return (PARSER_SALT_ITERATION);
10612 if (salt_pos[0] != '$') return (PARSER_SIGNATURE_UNMATCHED);
10613
10614 salt_pos[0] = 0x0;
10615
10616 salt->salt_iter = atoi (salt_pos - iterations_len);
10617
10618 salt_pos += 1;
10619
10620 iterations_len += 8;
10621 }
10622 else
10623 {
10624 salt->salt_iter = ROUNDS_MD5CRYPT;
10625 }
10626
10627 if ((input_len < DISPLAY_LEN_MIN_1600) || (input_len > DISPLAY_LEN_MAX_1600 + iterations_len)) return (PARSER_GLOBAL_LENGTH);
10628
10629 char *hash_pos = strchr (salt_pos, '$');
10630
10631 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
10632
10633 uint salt_len = hash_pos - salt_pos;
10634
10635 if (salt_len > 8) return (PARSER_SALT_LENGTH);
10636
10637 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
10638
10639 salt->salt_len = salt_len;
10640
10641 hash_pos++;
10642
10643 md5crypt_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
10644
10645 return (PARSER_OK);
10646 }
10647
10648 int episerver_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10649 {
10650 if ((input_len < DISPLAY_LEN_MIN_141) || (input_len > DISPLAY_LEN_MAX_141)) return (PARSER_GLOBAL_LENGTH);
10651
10652 if (memcmp (SIGNATURE_EPISERVER, input_buf, 14)) return (PARSER_SIGNATURE_UNMATCHED);
10653
10654 u32 *digest = (u32 *) hash_buf->digest;
10655
10656 salt_t *salt = hash_buf->salt;
10657
10658 char *salt_pos = input_buf + 14;
10659
10660 char *hash_pos = strchr (salt_pos, '*');
10661
10662 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
10663
10664 hash_pos++;
10665
10666 uint salt_len = hash_pos - salt_pos - 1;
10667
10668 char *salt_buf_ptr = (char *) salt->salt_buf;
10669
10670 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
10671
10672 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
10673
10674 salt->salt_len = salt_len;
10675
10676 u8 tmp_buf[100] = { 0 };
10677
10678 base64_decode (base64_to_int, (const u8 *) hash_pos, 27, tmp_buf);
10679
10680 memcpy (digest, tmp_buf, 20);
10681
10682 digest[0] = byte_swap_32 (digest[0]);
10683 digest[1] = byte_swap_32 (digest[1]);
10684 digest[2] = byte_swap_32 (digest[2]);
10685 digest[3] = byte_swap_32 (digest[3]);
10686 digest[4] = byte_swap_32 (digest[4]);
10687
10688 digest[0] -= SHA1M_A;
10689 digest[1] -= SHA1M_B;
10690 digest[2] -= SHA1M_C;
10691 digest[3] -= SHA1M_D;
10692 digest[4] -= SHA1M_E;
10693
10694 return (PARSER_OK);
10695 }
10696
10697 int descrypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10698 {
10699 if ((input_len < DISPLAY_LEN_MIN_1500) || (input_len > DISPLAY_LEN_MAX_1500)) return (PARSER_GLOBAL_LENGTH);
10700
10701 unsigned char c12 = itoa64_to_int (input_buf[12]);
10702
10703 if (c12 & 3) return (PARSER_HASH_VALUE);
10704
10705 u32 *digest = (u32 *) hash_buf->digest;
10706
10707 salt_t *salt = hash_buf->salt;
10708
10709 // for ascii_digest
10710 salt->salt_sign[0] = input_buf[0];
10711 salt->salt_sign[1] = input_buf[1];
10712
10713 salt->salt_buf[0] = itoa64_to_int (input_buf[0])
10714 | itoa64_to_int (input_buf[1]) << 6;
10715
10716 salt->salt_len = 2;
10717
10718 u8 tmp_buf[100] = { 0 };
10719
10720 base64_decode (itoa64_to_int, (const u8 *) input_buf + 2, 11, tmp_buf);
10721
10722 memcpy (digest, tmp_buf, 8);
10723
10724 uint tt;
10725
10726 IP (digest[0], digest[1], tt);
10727
10728 digest[2] = 0;
10729 digest[3] = 0;
10730
10731 return (PARSER_OK);
10732 }
10733
10734 int md4_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10735 {
10736 if ((input_len < DISPLAY_LEN_MIN_900) || (input_len > DISPLAY_LEN_MAX_900)) return (PARSER_GLOBAL_LENGTH);
10737
10738 u32 *digest = (u32 *) hash_buf->digest;
10739
10740 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
10741 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
10742 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
10743 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
10744
10745 digest[0] = byte_swap_32 (digest[0]);
10746 digest[1] = byte_swap_32 (digest[1]);
10747 digest[2] = byte_swap_32 (digest[2]);
10748 digest[3] = byte_swap_32 (digest[3]);
10749
10750 digest[0] -= MD4M_A;
10751 digest[1] -= MD4M_B;
10752 digest[2] -= MD4M_C;
10753 digest[3] -= MD4M_D;
10754
10755 return (PARSER_OK);
10756 }
10757
10758 int md4s_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10759 {
10760 if (data.opts_type & OPTS_TYPE_ST_HEX)
10761 {
10762 if ((input_len < DISPLAY_LEN_MIN_910H) || (input_len > DISPLAY_LEN_MAX_910H)) return (PARSER_GLOBAL_LENGTH);
10763 }
10764 else
10765 {
10766 if ((input_len < DISPLAY_LEN_MIN_910) || (input_len > DISPLAY_LEN_MAX_910)) return (PARSER_GLOBAL_LENGTH);
10767 }
10768
10769 u32 *digest = (u32 *) hash_buf->digest;
10770
10771 salt_t *salt = hash_buf->salt;
10772
10773 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
10774 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
10775 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
10776 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
10777
10778 digest[0] = byte_swap_32 (digest[0]);
10779 digest[1] = byte_swap_32 (digest[1]);
10780 digest[2] = byte_swap_32 (digest[2]);
10781 digest[3] = byte_swap_32 (digest[3]);
10782
10783 digest[0] -= MD4M_A;
10784 digest[1] -= MD4M_B;
10785 digest[2] -= MD4M_C;
10786 digest[3] -= MD4M_D;
10787
10788 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
10789
10790 uint salt_len = input_len - 32 - 1;
10791
10792 char *salt_buf = input_buf + 32 + 1;
10793
10794 char *salt_buf_ptr = (char *) salt->salt_buf;
10795
10796 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
10797
10798 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
10799
10800 salt->salt_len = salt_len;
10801
10802 return (PARSER_OK);
10803 }
10804
10805 int md5_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10806 {
10807 if ((input_len < DISPLAY_LEN_MIN_0) || (input_len > DISPLAY_LEN_MAX_0)) return (PARSER_GLOBAL_LENGTH);
10808
10809 u32 *digest = (u32 *) hash_buf->digest;
10810
10811 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
10812 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
10813 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
10814 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
10815
10816 digest[0] = byte_swap_32 (digest[0]);
10817 digest[1] = byte_swap_32 (digest[1]);
10818 digest[2] = byte_swap_32 (digest[2]);
10819 digest[3] = byte_swap_32 (digest[3]);
10820
10821 digest[0] -= MD5M_A;
10822 digest[1] -= MD5M_B;
10823 digest[2] -= MD5M_C;
10824 digest[3] -= MD5M_D;
10825
10826 return (PARSER_OK);
10827 }
10828
10829 int md5half_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10830 {
10831 if ((input_len < DISPLAY_LEN_MIN_5100) || (input_len > DISPLAY_LEN_MAX_5100)) return (PARSER_GLOBAL_LENGTH);
10832
10833 u32 *digest = (u32 *) hash_buf->digest;
10834
10835 digest[0] = hex_to_u32 ((const u8 *) &input_buf[0]);
10836 digest[1] = hex_to_u32 ((const u8 *) &input_buf[8]);
10837 digest[2] = 0;
10838 digest[3] = 0;
10839
10840 digest[0] = byte_swap_32 (digest[0]);
10841 digest[1] = byte_swap_32 (digest[1]);
10842
10843 return (PARSER_OK);
10844 }
10845
10846 int md5s_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10847 {
10848 if (data.opts_type & OPTS_TYPE_ST_HEX)
10849 {
10850 if ((input_len < DISPLAY_LEN_MIN_10H) || (input_len > DISPLAY_LEN_MAX_10H)) return (PARSER_GLOBAL_LENGTH);
10851 }
10852 else
10853 {
10854 if ((input_len < DISPLAY_LEN_MIN_10) || (input_len > DISPLAY_LEN_MAX_10)) return (PARSER_GLOBAL_LENGTH);
10855 }
10856
10857 u32 *digest = (u32 *) hash_buf->digest;
10858
10859 salt_t *salt = hash_buf->salt;
10860
10861 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
10862 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
10863 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
10864 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
10865
10866 digest[0] = byte_swap_32 (digest[0]);
10867 digest[1] = byte_swap_32 (digest[1]);
10868 digest[2] = byte_swap_32 (digest[2]);
10869 digest[3] = byte_swap_32 (digest[3]);
10870
10871 digest[0] -= MD5M_A;
10872 digest[1] -= MD5M_B;
10873 digest[2] -= MD5M_C;
10874 digest[3] -= MD5M_D;
10875
10876 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
10877
10878 uint salt_len = input_len - 32 - 1;
10879
10880 char *salt_buf = input_buf + 32 + 1;
10881
10882 char *salt_buf_ptr = (char *) salt->salt_buf;
10883
10884 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
10885
10886 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
10887
10888 salt->salt_len = salt_len;
10889
10890 return (PARSER_OK);
10891 }
10892
10893 int md5pix_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10894 {
10895 if ((input_len < DISPLAY_LEN_MIN_2400) || (input_len > DISPLAY_LEN_MAX_2400)) return (PARSER_GLOBAL_LENGTH);
10896
10897 u32 *digest = (u32 *) hash_buf->digest;
10898
10899 digest[0] = itoa64_to_int (input_buf[ 0]) << 0
10900 | itoa64_to_int (input_buf[ 1]) << 6
10901 | itoa64_to_int (input_buf[ 2]) << 12
10902 | itoa64_to_int (input_buf[ 3]) << 18;
10903 digest[1] = itoa64_to_int (input_buf[ 4]) << 0
10904 | itoa64_to_int (input_buf[ 5]) << 6
10905 | itoa64_to_int (input_buf[ 6]) << 12
10906 | itoa64_to_int (input_buf[ 7]) << 18;
10907 digest[2] = itoa64_to_int (input_buf[ 8]) << 0
10908 | itoa64_to_int (input_buf[ 9]) << 6
10909 | itoa64_to_int (input_buf[10]) << 12
10910 | itoa64_to_int (input_buf[11]) << 18;
10911 digest[3] = itoa64_to_int (input_buf[12]) << 0
10912 | itoa64_to_int (input_buf[13]) << 6
10913 | itoa64_to_int (input_buf[14]) << 12
10914 | itoa64_to_int (input_buf[15]) << 18;
10915
10916 digest[0] -= MD5M_A;
10917 digest[1] -= MD5M_B;
10918 digest[2] -= MD5M_C;
10919 digest[3] -= MD5M_D;
10920
10921 digest[0] &= 0x00ffffff;
10922 digest[1] &= 0x00ffffff;
10923 digest[2] &= 0x00ffffff;
10924 digest[3] &= 0x00ffffff;
10925
10926 return (PARSER_OK);
10927 }
10928
10929 int md5asa_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10930 {
10931 if (data.opts_type & OPTS_TYPE_ST_HEX)
10932 {
10933 if ((input_len < DISPLAY_LEN_MIN_2410H) || (input_len > DISPLAY_LEN_MAX_2410H)) return (PARSER_GLOBAL_LENGTH);
10934 }
10935 else
10936 {
10937 if ((input_len < DISPLAY_LEN_MIN_2410) || (input_len > DISPLAY_LEN_MAX_2410)) return (PARSER_GLOBAL_LENGTH);
10938 }
10939
10940 u32 *digest = (u32 *) hash_buf->digest;
10941
10942 salt_t *salt = hash_buf->salt;
10943
10944 digest[0] = itoa64_to_int (input_buf[ 0]) << 0
10945 | itoa64_to_int (input_buf[ 1]) << 6
10946 | itoa64_to_int (input_buf[ 2]) << 12
10947 | itoa64_to_int (input_buf[ 3]) << 18;
10948 digest[1] = itoa64_to_int (input_buf[ 4]) << 0
10949 | itoa64_to_int (input_buf[ 5]) << 6
10950 | itoa64_to_int (input_buf[ 6]) << 12
10951 | itoa64_to_int (input_buf[ 7]) << 18;
10952 digest[2] = itoa64_to_int (input_buf[ 8]) << 0
10953 | itoa64_to_int (input_buf[ 9]) << 6
10954 | itoa64_to_int (input_buf[10]) << 12
10955 | itoa64_to_int (input_buf[11]) << 18;
10956 digest[3] = itoa64_to_int (input_buf[12]) << 0
10957 | itoa64_to_int (input_buf[13]) << 6
10958 | itoa64_to_int (input_buf[14]) << 12
10959 | itoa64_to_int (input_buf[15]) << 18;
10960
10961 digest[0] -= MD5M_A;
10962 digest[1] -= MD5M_B;
10963 digest[2] -= MD5M_C;
10964 digest[3] -= MD5M_D;
10965
10966 digest[0] &= 0x00ffffff;
10967 digest[1] &= 0x00ffffff;
10968 digest[2] &= 0x00ffffff;
10969 digest[3] &= 0x00ffffff;
10970
10971 if (input_buf[16] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
10972
10973 uint salt_len = input_len - 16 - 1;
10974
10975 char *salt_buf = input_buf + 16 + 1;
10976
10977 char *salt_buf_ptr = (char *) salt->salt_buf;
10978
10979 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
10980
10981 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
10982
10983 salt->salt_len = salt_len;
10984
10985 return (PARSER_OK);
10986 }
10987
10988 void transform_netntlmv1_key (const u8 *nthash, u8 *key)
10989 {
10990 key[0] = (nthash[0] >> 0);
10991 key[1] = (nthash[0] << 7) | (nthash[1] >> 1);
10992 key[2] = (nthash[1] << 6) | (nthash[2] >> 2);
10993 key[3] = (nthash[2] << 5) | (nthash[3] >> 3);
10994 key[4] = (nthash[3] << 4) | (nthash[4] >> 4);
10995 key[5] = (nthash[4] << 3) | (nthash[5] >> 5);
10996 key[6] = (nthash[5] << 2) | (nthash[6] >> 6);
10997 key[7] = (nthash[6] << 1);
10998
10999 key[0] |= 0x01;
11000 key[1] |= 0x01;
11001 key[2] |= 0x01;
11002 key[3] |= 0x01;
11003 key[4] |= 0x01;
11004 key[5] |= 0x01;
11005 key[6] |= 0x01;
11006 key[7] |= 0x01;
11007 }
11008
11009 int netntlmv1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11010 {
11011 if ((input_len < DISPLAY_LEN_MIN_5500) || (input_len > DISPLAY_LEN_MAX_5500)) return (PARSER_GLOBAL_LENGTH);
11012
11013 u32 *digest = (u32 *) hash_buf->digest;
11014
11015 salt_t *salt = hash_buf->salt;
11016
11017 netntlm_t *netntlm = (netntlm_t *) hash_buf->esalt;
11018
11019 /**
11020 * parse line
11021 */
11022
11023 char *user_pos = input_buf;
11024
11025 char *unused_pos = strchr (user_pos, ':');
11026
11027 if (unused_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
11028
11029 uint user_len = unused_pos - user_pos;
11030
11031 if (user_len > 60) return (PARSER_SALT_LENGTH);
11032
11033 unused_pos++;
11034
11035 char *domain_pos = strchr (unused_pos, ':');
11036
11037 if (domain_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
11038
11039 uint unused_len = domain_pos - unused_pos;
11040
11041 if (unused_len != 0) return (PARSER_SALT_LENGTH);
11042
11043 domain_pos++;
11044
11045 char *srvchall_pos = strchr (domain_pos, ':');
11046
11047 if (srvchall_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
11048
11049 uint domain_len = srvchall_pos - domain_pos;
11050
11051 if (domain_len > 45) return (PARSER_SALT_LENGTH);
11052
11053 srvchall_pos++;
11054
11055 char *hash_pos = strchr (srvchall_pos, ':');
11056
11057 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
11058
11059 uint srvchall_len = hash_pos - srvchall_pos;
11060
11061 // if (srvchall_len != 0) return (PARSER_SALT_LENGTH);
11062
11063 hash_pos++;
11064
11065 char *clichall_pos = strchr (hash_pos, ':');
11066
11067 if (clichall_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
11068
11069 uint hash_len = clichall_pos - hash_pos;
11070
11071 if (hash_len != 48) return (PARSER_HASH_LENGTH);
11072
11073 clichall_pos++;
11074
11075 uint clichall_len = input_len - user_len - 1 - unused_len - 1 - domain_len - 1 - srvchall_len - 1 - hash_len - 1;
11076
11077 if (clichall_len != 16) return (PARSER_SALT_LENGTH);
11078
11079 /**
11080 * store some data for later use
11081 */
11082
11083 netntlm->user_len = user_len * 2;
11084 netntlm->domain_len = domain_len * 2;
11085 netntlm->srvchall_len = srvchall_len / 2;
11086 netntlm->clichall_len = clichall_len / 2;
11087
11088 char *userdomain_ptr = (char *) netntlm->userdomain_buf;
11089 char *chall_ptr = (char *) netntlm->chall_buf;
11090
11091 /**
11092 * handle username and domainname
11093 */
11094
11095 for (uint i = 0; i < user_len; i++)
11096 {
11097 *userdomain_ptr++ = user_pos[i];
11098 *userdomain_ptr++ = 0;
11099 }
11100
11101 for (uint i = 0; i < domain_len; i++)
11102 {
11103 *userdomain_ptr++ = domain_pos[i];
11104 *userdomain_ptr++ = 0;
11105 }
11106
11107 /**
11108 * handle server challenge encoding
11109 */
11110
11111 for (uint i = 0; i < srvchall_len; i += 2)
11112 {
11113 const char p0 = srvchall_pos[i + 0];
11114 const char p1 = srvchall_pos[i + 1];
11115
11116 *chall_ptr++ = hex_convert (p1) << 0
11117 | hex_convert (p0) << 4;
11118 }
11119
11120 /**
11121 * handle client challenge encoding
11122 */
11123
11124 for (uint i = 0; i < clichall_len; i += 2)
11125 {
11126 const char p0 = clichall_pos[i + 0];
11127 const char p1 = clichall_pos[i + 1];
11128
11129 *chall_ptr++ = hex_convert (p1) << 0
11130 | hex_convert (p0) << 4;
11131 }
11132
11133 /**
11134 * store data
11135 */
11136
11137 char *salt_buf_ptr = (char *) salt->salt_buf;
11138
11139 uint salt_len = parse_and_store_salt (salt_buf_ptr, clichall_pos, clichall_len);
11140
11141 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11142
11143 salt->salt_len = salt_len;
11144
11145 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
11146 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
11147 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
11148 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
11149
11150 digest[0] = byte_swap_32 (digest[0]);
11151 digest[1] = byte_swap_32 (digest[1]);
11152 digest[2] = byte_swap_32 (digest[2]);
11153 digest[3] = byte_swap_32 (digest[3]);
11154
11155 /* special case, last 8 byte do not need to be checked since they are brute-forced next */
11156
11157 uint digest_tmp[2] = { 0 };
11158
11159 digest_tmp[0] = hex_to_u32 ((const u8 *) &hash_pos[32]);
11160 digest_tmp[1] = hex_to_u32 ((const u8 *) &hash_pos[40]);
11161
11162 digest_tmp[0] = byte_swap_32 (digest_tmp[0]);
11163 digest_tmp[1] = byte_swap_32 (digest_tmp[1]);
11164
11165 /* special case 2: ESS */
11166
11167 if (srvchall_len == 48)
11168 {
11169 if ((netntlm->chall_buf[2] == 0) && (netntlm->chall_buf[3] == 0) && (netntlm->chall_buf[4] == 0) && (netntlm->chall_buf[5] == 0))
11170 {
11171 uint w[16] = { 0 };
11172
11173 w[ 0] = netntlm->chall_buf[6];
11174 w[ 1] = netntlm->chall_buf[7];
11175 w[ 2] = netntlm->chall_buf[0];
11176 w[ 3] = netntlm->chall_buf[1];
11177 w[ 4] = 0x80;
11178 w[14] = 16 * 8;
11179
11180 uint dgst[4] = { 0 };
11181
11182 dgst[0] = MAGIC_A;
11183 dgst[1] = MAGIC_B;
11184 dgst[2] = MAGIC_C;
11185 dgst[3] = MAGIC_D;
11186
11187 md5_64 (w, dgst);
11188
11189 salt->salt_buf[0] = dgst[0];
11190 salt->salt_buf[1] = dgst[1];
11191 }
11192 }
11193
11194 /* precompute netntlmv1 exploit start */
11195
11196 for (uint i = 0; i < 0x10000; i++)
11197 {
11198 uint key_md4[2] = { i, 0 };
11199 uint key_des[2] = { 0, 0 };
11200
11201 transform_netntlmv1_key ((u8 *) key_md4, (u8 *) key_des);
11202
11203 uint Kc[16] = { 0 };
11204 uint Kd[16] = { 0 };
11205
11206 _des_keysetup (key_des, Kc, Kd, c_skb);
11207
11208 uint data3[2] = { salt->salt_buf[0], salt->salt_buf[1] };
11209
11210 _des_encrypt (data3, Kc, Kd, c_SPtrans);
11211
11212 if (data3[0] != digest_tmp[0]) continue;
11213 if (data3[1] != digest_tmp[1]) continue;
11214
11215 salt->salt_buf[2] = i;
11216
11217 salt->salt_len = 24;
11218
11219 break;
11220 }
11221
11222 salt->salt_buf_pc[0] = digest_tmp[0];
11223 salt->salt_buf_pc[1] = digest_tmp[1];
11224
11225 /* precompute netntlmv1 exploit stop */
11226
11227 u32 tt;
11228
11229 IP (digest[0], digest[1], tt);
11230 IP (digest[2], digest[3], tt);
11231
11232 digest[0] = rotr32 (digest[0], 29);
11233 digest[1] = rotr32 (digest[1], 29);
11234 digest[2] = rotr32 (digest[2], 29);
11235 digest[3] = rotr32 (digest[3], 29);
11236
11237 IP (salt->salt_buf[0], salt->salt_buf[1], tt);
11238
11239 salt->salt_buf[0] = rotl32 (salt->salt_buf[0], 3);
11240 salt->salt_buf[1] = rotl32 (salt->salt_buf[1], 3);
11241
11242 return (PARSER_OK);
11243 }
11244
11245 int netntlmv2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11246 {
11247 if ((input_len < DISPLAY_LEN_MIN_5600) || (input_len > DISPLAY_LEN_MAX_5600)) return (PARSER_GLOBAL_LENGTH);
11248
11249 u32 *digest = (u32 *) hash_buf->digest;
11250
11251 salt_t *salt = hash_buf->salt;
11252
11253 netntlm_t *netntlm = (netntlm_t *) hash_buf->esalt;
11254
11255 /**
11256 * parse line
11257 */
11258
11259 char *user_pos = input_buf;
11260
11261 char *unused_pos = strchr (user_pos, ':');
11262
11263 if (unused_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
11264
11265 uint user_len = unused_pos - user_pos;
11266
11267 if (user_len > 60) return (PARSER_SALT_LENGTH);
11268
11269 unused_pos++;
11270
11271 char *domain_pos = strchr (unused_pos, ':');
11272
11273 if (domain_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
11274
11275 uint unused_len = domain_pos - unused_pos;
11276
11277 if (unused_len != 0) return (PARSER_SALT_LENGTH);
11278
11279 domain_pos++;
11280
11281 char *srvchall_pos = strchr (domain_pos, ':');
11282
11283 if (srvchall_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
11284
11285 uint domain_len = srvchall_pos - domain_pos;
11286
11287 if (domain_len > 45) return (PARSER_SALT_LENGTH);
11288
11289 srvchall_pos++;
11290
11291 char *hash_pos = strchr (srvchall_pos, ':');
11292
11293 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
11294
11295 uint srvchall_len = hash_pos - srvchall_pos;
11296
11297 if (srvchall_len != 16) return (PARSER_SALT_LENGTH);
11298
11299 hash_pos++;
11300
11301 char *clichall_pos = strchr (hash_pos, ':');
11302
11303 if (clichall_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
11304
11305 uint hash_len = clichall_pos - hash_pos;
11306
11307 if (hash_len != 32) return (PARSER_HASH_LENGTH);
11308
11309 clichall_pos++;
11310
11311 uint clichall_len = input_len - user_len - 1 - unused_len - 1 - domain_len - 1 - srvchall_len - 1 - hash_len - 1;
11312
11313 if (clichall_len > 1024) return (PARSER_SALT_LENGTH);
11314
11315 if (clichall_len % 2) return (PARSER_SALT_VALUE);
11316
11317 /**
11318 * store some data for later use
11319 */
11320
11321 netntlm->user_len = user_len * 2;
11322 netntlm->domain_len = domain_len * 2;
11323 netntlm->srvchall_len = srvchall_len / 2;
11324 netntlm->clichall_len = clichall_len / 2;
11325
11326 char *userdomain_ptr = (char *) netntlm->userdomain_buf;
11327 char *chall_ptr = (char *) netntlm->chall_buf;
11328
11329 /**
11330 * handle username and domainname
11331 */
11332
11333 for (uint i = 0; i < user_len; i++)
11334 {
11335 *userdomain_ptr++ = toupper (user_pos[i]);
11336 *userdomain_ptr++ = 0;
11337 }
11338
11339 for (uint i = 0; i < domain_len; i++)
11340 {
11341 *userdomain_ptr++ = domain_pos[i];
11342 *userdomain_ptr++ = 0;
11343 }
11344
11345 *userdomain_ptr++ = 0x80;
11346
11347 /**
11348 * handle server challenge encoding
11349 */
11350
11351 for (uint i = 0; i < srvchall_len; i += 2)
11352 {
11353 const char p0 = srvchall_pos[i + 0];
11354 const char p1 = srvchall_pos[i + 1];
11355
11356 *chall_ptr++ = hex_convert (p1) << 0
11357 | hex_convert (p0) << 4;
11358 }
11359
11360 /**
11361 * handle client challenge encoding
11362 */
11363
11364 for (uint i = 0; i < clichall_len; i += 2)
11365 {
11366 const char p0 = clichall_pos[i + 0];
11367 const char p1 = clichall_pos[i + 1];
11368
11369 *chall_ptr++ = hex_convert (p1) << 0
11370 | hex_convert (p0) << 4;
11371 }
11372
11373 *chall_ptr++ = 0x80;
11374
11375 /**
11376 * handle hash itself
11377 */
11378
11379 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
11380 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
11381 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
11382 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
11383
11384 digest[0] = byte_swap_32 (digest[0]);
11385 digest[1] = byte_swap_32 (digest[1]);
11386 digest[2] = byte_swap_32 (digest[2]);
11387 digest[3] = byte_swap_32 (digest[3]);
11388
11389 /**
11390 * reuse challange data as salt_buf, its the buffer that is most likely unique
11391 */
11392
11393 salt->salt_buf[0] = 0;
11394 salt->salt_buf[1] = 0;
11395 salt->salt_buf[2] = 0;
11396 salt->salt_buf[3] = 0;
11397 salt->salt_buf[4] = 0;
11398 salt->salt_buf[5] = 0;
11399 salt->salt_buf[6] = 0;
11400 salt->salt_buf[7] = 0;
11401
11402 uint *uptr;
11403
11404 uptr = (uint *) netntlm->userdomain_buf;
11405
11406 for (uint i = 0; i < 16; i += 16)
11407 {
11408 md5_64 (uptr, salt->salt_buf);
11409 }
11410
11411 uptr = (uint *) netntlm->chall_buf;
11412
11413 for (uint i = 0; i < 256; i += 16)
11414 {
11415 md5_64 (uptr, salt->salt_buf);
11416 }
11417
11418 salt->salt_len = 16;
11419
11420 return (PARSER_OK);
11421 }
11422
11423 int joomla_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11424 {
11425 if (data.opts_type & OPTS_TYPE_ST_HEX)
11426 {
11427 if ((input_len < DISPLAY_LEN_MIN_11H) || (input_len > DISPLAY_LEN_MAX_11H)) return (PARSER_GLOBAL_LENGTH);
11428 }
11429 else
11430 {
11431 if ((input_len < DISPLAY_LEN_MIN_11) || (input_len > DISPLAY_LEN_MAX_11)) return (PARSER_GLOBAL_LENGTH);
11432 }
11433
11434 u32 *digest = (u32 *) hash_buf->digest;
11435
11436 salt_t *salt = hash_buf->salt;
11437
11438 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11439 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11440 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11441 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11442
11443 digest[0] = byte_swap_32 (digest[0]);
11444 digest[1] = byte_swap_32 (digest[1]);
11445 digest[2] = byte_swap_32 (digest[2]);
11446 digest[3] = byte_swap_32 (digest[3]);
11447
11448 digest[0] -= MD5M_A;
11449 digest[1] -= MD5M_B;
11450 digest[2] -= MD5M_C;
11451 digest[3] -= MD5M_D;
11452
11453 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
11454
11455 uint salt_len = input_len - 32 - 1;
11456
11457 char *salt_buf = input_buf + 32 + 1;
11458
11459 char *salt_buf_ptr = (char *) salt->salt_buf;
11460
11461 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
11462
11463 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11464
11465 salt->salt_len = salt_len;
11466
11467 return (PARSER_OK);
11468 }
11469
11470 int postgresql_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11471 {
11472 if (data.opts_type & OPTS_TYPE_ST_HEX)
11473 {
11474 if ((input_len < DISPLAY_LEN_MIN_12H) || (input_len > DISPLAY_LEN_MAX_12H)) return (PARSER_GLOBAL_LENGTH);
11475 }
11476 else
11477 {
11478 if ((input_len < DISPLAY_LEN_MIN_12) || (input_len > DISPLAY_LEN_MAX_12)) return (PARSER_GLOBAL_LENGTH);
11479 }
11480
11481 u32 *digest = (u32 *) hash_buf->digest;
11482
11483 salt_t *salt = hash_buf->salt;
11484
11485 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11486 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11487 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11488 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11489
11490 digest[0] = byte_swap_32 (digest[0]);
11491 digest[1] = byte_swap_32 (digest[1]);
11492 digest[2] = byte_swap_32 (digest[2]);
11493 digest[3] = byte_swap_32 (digest[3]);
11494
11495 digest[0] -= MD5M_A;
11496 digest[1] -= MD5M_B;
11497 digest[2] -= MD5M_C;
11498 digest[3] -= MD5M_D;
11499
11500 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
11501
11502 uint salt_len = input_len - 32 - 1;
11503
11504 char *salt_buf = input_buf + 32 + 1;
11505
11506 char *salt_buf_ptr = (char *) salt->salt_buf;
11507
11508 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
11509
11510 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11511
11512 salt->salt_len = salt_len;
11513
11514 return (PARSER_OK);
11515 }
11516
11517 int md5md5_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11518 {
11519 if ((input_len < DISPLAY_LEN_MIN_2600) || (input_len > DISPLAY_LEN_MAX_2600)) return (PARSER_GLOBAL_LENGTH);
11520
11521 u32 *digest = (u32 *) hash_buf->digest;
11522
11523 salt_t *salt = hash_buf->salt;
11524
11525 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11526 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11527 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11528 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11529
11530 digest[0] = byte_swap_32 (digest[0]);
11531 digest[1] = byte_swap_32 (digest[1]);
11532 digest[2] = byte_swap_32 (digest[2]);
11533 digest[3] = byte_swap_32 (digest[3]);
11534
11535 digest[0] -= MD5M_A;
11536 digest[1] -= MD5M_B;
11537 digest[2] -= MD5M_C;
11538 digest[3] -= MD5M_D;
11539
11540 /**
11541 * This is a virtual salt. While the algorithm is basically not salted
11542 * we can exploit the salt buffer to set the 0x80 and the w[14] value.
11543 * This way we can save a special md5md5 kernel and reuse the one from vbull.
11544 */
11545
11546 char *salt_buf_ptr = (char *) salt->salt_buf;
11547
11548 uint salt_len = parse_and_store_salt (salt_buf_ptr, (char *) "", 0);
11549
11550 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11551
11552 salt->salt_len = salt_len;
11553
11554 return (PARSER_OK);
11555 }
11556
11557 int vb3_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11558 {
11559 if (data.opts_type & OPTS_TYPE_ST_HEX)
11560 {
11561 if ((input_len < DISPLAY_LEN_MIN_2611H) || (input_len > DISPLAY_LEN_MAX_2611H)) return (PARSER_GLOBAL_LENGTH);
11562 }
11563 else
11564 {
11565 if ((input_len < DISPLAY_LEN_MIN_2611) || (input_len > DISPLAY_LEN_MAX_2611)) return (PARSER_GLOBAL_LENGTH);
11566 }
11567
11568 u32 *digest = (u32 *) hash_buf->digest;
11569
11570 salt_t *salt = hash_buf->salt;
11571
11572 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11573 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11574 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11575 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11576
11577 digest[0] = byte_swap_32 (digest[0]);
11578 digest[1] = byte_swap_32 (digest[1]);
11579 digest[2] = byte_swap_32 (digest[2]);
11580 digest[3] = byte_swap_32 (digest[3]);
11581
11582 digest[0] -= MD5M_A;
11583 digest[1] -= MD5M_B;
11584 digest[2] -= MD5M_C;
11585 digest[3] -= MD5M_D;
11586
11587 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
11588
11589 uint salt_len = input_len - 32 - 1;
11590
11591 char *salt_buf = input_buf + 32 + 1;
11592
11593 char *salt_buf_ptr = (char *) salt->salt_buf;
11594
11595 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
11596
11597 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11598
11599 salt->salt_len = salt_len;
11600
11601 return (PARSER_OK);
11602 }
11603
11604 int vb30_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11605 {
11606 if (data.opts_type & OPTS_TYPE_ST_HEX)
11607 {
11608 if ((input_len < DISPLAY_LEN_MIN_2711H) || (input_len > DISPLAY_LEN_MAX_2711H)) return (PARSER_GLOBAL_LENGTH);
11609 }
11610 else
11611 {
11612 if ((input_len < DISPLAY_LEN_MIN_2711) || (input_len > DISPLAY_LEN_MAX_2711)) return (PARSER_GLOBAL_LENGTH);
11613 }
11614
11615 u32 *digest = (u32 *) hash_buf->digest;
11616
11617 salt_t *salt = hash_buf->salt;
11618
11619 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11620 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11621 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11622 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11623
11624 digest[0] = byte_swap_32 (digest[0]);
11625 digest[1] = byte_swap_32 (digest[1]);
11626 digest[2] = byte_swap_32 (digest[2]);
11627 digest[3] = byte_swap_32 (digest[3]);
11628
11629 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
11630
11631 uint salt_len = input_len - 32 - 1;
11632
11633 char *salt_buf = input_buf + 32 + 1;
11634
11635 char *salt_buf_ptr = (char *) salt->salt_buf;
11636
11637 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
11638
11639 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11640
11641 salt->salt_len = salt_len;
11642
11643 return (PARSER_OK);
11644 }
11645
11646 int dcc_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11647 {
11648 if (data.opts_type & OPTS_TYPE_ST_HEX)
11649 {
11650 if ((input_len < DISPLAY_LEN_MIN_1100H) || (input_len > DISPLAY_LEN_MAX_1100H)) return (PARSER_GLOBAL_LENGTH);
11651 }
11652 else
11653 {
11654 if ((input_len < DISPLAY_LEN_MIN_1100) || (input_len > DISPLAY_LEN_MAX_1100)) return (PARSER_GLOBAL_LENGTH);
11655 }
11656
11657 u32 *digest = (u32 *) hash_buf->digest;
11658
11659 salt_t *salt = hash_buf->salt;
11660
11661 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11662 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11663 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11664 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11665
11666 digest[0] = byte_swap_32 (digest[0]);
11667 digest[1] = byte_swap_32 (digest[1]);
11668 digest[2] = byte_swap_32 (digest[2]);
11669 digest[3] = byte_swap_32 (digest[3]);
11670
11671 digest[0] -= MD4M_A;
11672 digest[1] -= MD4M_B;
11673 digest[2] -= MD4M_C;
11674 digest[3] -= MD4M_D;
11675
11676 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
11677
11678 uint salt_len = input_len - 32 - 1;
11679
11680 char *salt_buf = input_buf + 32 + 1;
11681
11682 char *salt_buf_ptr = (char *) salt->salt_buf;
11683
11684 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
11685
11686 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11687
11688 salt->salt_len = salt_len;
11689
11690 return (PARSER_OK);
11691 }
11692
11693 int ipb2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11694 {
11695 if (data.opts_type & OPTS_TYPE_ST_HEX)
11696 {
11697 if ((input_len < DISPLAY_LEN_MIN_2811H) || (input_len > DISPLAY_LEN_MAX_2811H)) return (PARSER_GLOBAL_LENGTH);
11698 }
11699 else
11700 {
11701 if ((input_len < DISPLAY_LEN_MIN_2811) || (input_len > DISPLAY_LEN_MAX_2811)) return (PARSER_GLOBAL_LENGTH);
11702 }
11703
11704 u32 *digest = (u32 *) hash_buf->digest;
11705
11706 salt_t *salt = hash_buf->salt;
11707
11708 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11709 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11710 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11711 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11712
11713 digest[0] = byte_swap_32 (digest[0]);
11714 digest[1] = byte_swap_32 (digest[1]);
11715 digest[2] = byte_swap_32 (digest[2]);
11716 digest[3] = byte_swap_32 (digest[3]);
11717
11718 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
11719
11720 uint salt_len = input_len - 32 - 1;
11721
11722 char *salt_buf = input_buf + 32 + 1;
11723
11724 uint salt_pc_block[16] = { 0 };
11725
11726 char *salt_pc_block_ptr = (char *) salt_pc_block;
11727
11728 salt_len = parse_and_store_salt (salt_pc_block_ptr, salt_buf, salt_len);
11729
11730 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11731
11732 salt_pc_block_ptr[salt_len] = (unsigned char) 0x80;
11733
11734 salt_pc_block[14] = salt_len * 8;
11735
11736 uint salt_pc_digest[4] = { MAGIC_A, MAGIC_B, MAGIC_C, MAGIC_D };
11737
11738 md5_64 (salt_pc_block, salt_pc_digest);
11739
11740 salt_pc_digest[0] = byte_swap_32 (salt_pc_digest[0]);
11741 salt_pc_digest[1] = byte_swap_32 (salt_pc_digest[1]);
11742 salt_pc_digest[2] = byte_swap_32 (salt_pc_digest[2]);
11743 salt_pc_digest[3] = byte_swap_32 (salt_pc_digest[3]);
11744
11745 u8 *salt_buf_ptr = (u8 *) salt->salt_buf;
11746
11747 memcpy (salt_buf_ptr, salt_buf, salt_len);
11748
11749 u8 *salt_buf_pc_ptr = (u8 *) salt->salt_buf_pc;
11750
11751 bin_to_hex_lower (salt_pc_digest[0], salt_buf_pc_ptr + 0);
11752 bin_to_hex_lower (salt_pc_digest[1], salt_buf_pc_ptr + 8);
11753 bin_to_hex_lower (salt_pc_digest[2], salt_buf_pc_ptr + 16);
11754 bin_to_hex_lower (salt_pc_digest[3], salt_buf_pc_ptr + 24);
11755
11756 salt->salt_len = 32; // changed, was salt_len before -- was a bug? 32 should be correct
11757
11758 return (PARSER_OK);
11759 }
11760
11761 int sha1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11762 {
11763 if ((input_len < DISPLAY_LEN_MIN_100) || (input_len > DISPLAY_LEN_MAX_100)) return (PARSER_GLOBAL_LENGTH);
11764
11765 u32 *digest = (u32 *) hash_buf->digest;
11766
11767 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11768 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11769 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11770 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11771 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
11772
11773 digest[0] -= SHA1M_A;
11774 digest[1] -= SHA1M_B;
11775 digest[2] -= SHA1M_C;
11776 digest[3] -= SHA1M_D;
11777 digest[4] -= SHA1M_E;
11778
11779 return (PARSER_OK);
11780 }
11781
11782 int sha1linkedin_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11783 {
11784 if ((input_len < DISPLAY_LEN_MIN_100) || (input_len > DISPLAY_LEN_MAX_100)) return (PARSER_GLOBAL_LENGTH);
11785
11786 u32 *digest = (u32 *) hash_buf->digest;
11787
11788 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11789 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11790 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11791 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11792 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
11793
11794 return (PARSER_OK);
11795 }
11796
11797 int sha1axcrypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11798 {
11799 if ((input_len < DISPLAY_LEN_MIN_13300) || (input_len > DISPLAY_LEN_MAX_13300)) return (PARSER_GLOBAL_LENGTH);
11800
11801 if (memcmp (SIGNATURE_AXCRYPT_SHA1, input_buf, 13)) return (PARSER_SIGNATURE_UNMATCHED);
11802
11803 u32 *digest = (u32 *) hash_buf->digest;
11804
11805 input_buf +=14;
11806
11807 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11808 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11809 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11810 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11811 digest[4] = 0x00000000;
11812
11813 return (PARSER_OK);
11814 }
11815
11816 int sha1s_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11817 {
11818 if (data.opts_type & OPTS_TYPE_ST_HEX)
11819 {
11820 if ((input_len < DISPLAY_LEN_MIN_110H) || (input_len > DISPLAY_LEN_MAX_110H)) return (PARSER_GLOBAL_LENGTH);
11821 }
11822 else
11823 {
11824 if ((input_len < DISPLAY_LEN_MIN_110) || (input_len > DISPLAY_LEN_MAX_110)) return (PARSER_GLOBAL_LENGTH);
11825 }
11826
11827 u32 *digest = (u32 *) hash_buf->digest;
11828
11829 salt_t *salt = hash_buf->salt;
11830
11831 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11832 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11833 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11834 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11835 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
11836
11837 digest[0] -= SHA1M_A;
11838 digest[1] -= SHA1M_B;
11839 digest[2] -= SHA1M_C;
11840 digest[3] -= SHA1M_D;
11841 digest[4] -= SHA1M_E;
11842
11843 if (input_buf[40] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
11844
11845 uint salt_len = input_len - 40 - 1;
11846
11847 char *salt_buf = input_buf + 40 + 1;
11848
11849 char *salt_buf_ptr = (char *) salt->salt_buf;
11850
11851 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
11852
11853 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11854
11855 salt->salt_len = salt_len;
11856
11857 return (PARSER_OK);
11858 }
11859
11860 int pstoken_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11861 {
11862 if ((input_len < DISPLAY_LEN_MIN_13500) || (input_len > DISPLAY_LEN_MAX_13500)) return (PARSER_GLOBAL_LENGTH);
11863
11864 u32 *digest = (u32 *) hash_buf->digest;
11865
11866 salt_t *salt = hash_buf->salt;
11867
11868 pstoken_t *pstoken = (pstoken_t *) hash_buf->esalt;
11869
11870 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11871 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11872 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11873 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11874 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
11875
11876 if (input_buf[40] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
11877
11878 uint salt_len = input_len - 40 - 1;
11879
11880 char *salt_buf = input_buf + 40 + 1;
11881
11882 if (salt_len == UINT_MAX || salt_len % 2 != 0) return (PARSER_SALT_LENGTH);
11883
11884 u8 *pstoken_ptr = (u8 *) pstoken->salt_buf;
11885
11886 for (uint i = 0, j = 0; i < salt_len; i += 2, j += 1)
11887 {
11888 pstoken_ptr[j] = hex_to_u8 ((const u8 *) &salt_buf[i]);
11889 }
11890
11891 pstoken->salt_len = salt_len / 2;
11892
11893 /* some fake salt for the sorting mechanisms */
11894
11895 salt->salt_buf[0] = pstoken->salt_buf[0];
11896 salt->salt_buf[1] = pstoken->salt_buf[1];
11897 salt->salt_buf[2] = pstoken->salt_buf[2];
11898 salt->salt_buf[3] = pstoken->salt_buf[3];
11899 salt->salt_buf[4] = pstoken->salt_buf[4];
11900 salt->salt_buf[5] = pstoken->salt_buf[5];
11901 salt->salt_buf[6] = pstoken->salt_buf[6];
11902 salt->salt_buf[7] = pstoken->salt_buf[7];
11903
11904 salt->salt_len = 32;
11905
11906 /* we need to check if we can precompute some of the data --
11907 this is possible since the scheme is badly designed */
11908
11909 pstoken->pc_digest[0] = SHA1M_A;
11910 pstoken->pc_digest[1] = SHA1M_B;
11911 pstoken->pc_digest[2] = SHA1M_C;
11912 pstoken->pc_digest[3] = SHA1M_D;
11913 pstoken->pc_digest[4] = SHA1M_E;
11914
11915 pstoken->pc_offset = 0;
11916
11917 for (int i = 0; i < (int) pstoken->salt_len - 64; i += 64)
11918 {
11919 uint w[16];
11920
11921 w[ 0] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 0]);
11922 w[ 1] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 1]);
11923 w[ 2] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 2]);
11924 w[ 3] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 3]);
11925 w[ 4] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 4]);
11926 w[ 5] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 5]);
11927 w[ 6] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 6]);
11928 w[ 7] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 7]);
11929 w[ 8] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 8]);
11930 w[ 9] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 9]);
11931 w[10] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 10]);
11932 w[11] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 11]);
11933 w[12] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 12]);
11934 w[13] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 13]);
11935 w[14] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 14]);
11936 w[15] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 15]);
11937
11938 sha1_64 (w, pstoken->pc_digest);
11939
11940 pstoken->pc_offset += 16;
11941 }
11942
11943 return (PARSER_OK);
11944 }
11945
11946 int sha1b64_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11947 {
11948 if ((input_len < DISPLAY_LEN_MIN_101) || (input_len > DISPLAY_LEN_MAX_101)) return (PARSER_GLOBAL_LENGTH);
11949
11950 if (memcmp (SIGNATURE_SHA1B64, input_buf, 5)) return (PARSER_SIGNATURE_UNMATCHED);
11951
11952 u32 *digest = (u32 *) hash_buf->digest;
11953
11954 u8 tmp_buf[100] = { 0 };
11955
11956 base64_decode (base64_to_int, (const u8 *) input_buf + 5, input_len - 5, tmp_buf);
11957
11958 memcpy (digest, tmp_buf, 20);
11959
11960 digest[0] = byte_swap_32 (digest[0]);
11961 digest[1] = byte_swap_32 (digest[1]);
11962 digest[2] = byte_swap_32 (digest[2]);
11963 digest[3] = byte_swap_32 (digest[3]);
11964 digest[4] = byte_swap_32 (digest[4]);
11965
11966 digest[0] -= SHA1M_A;
11967 digest[1] -= SHA1M_B;
11968 digest[2] -= SHA1M_C;
11969 digest[3] -= SHA1M_D;
11970 digest[4] -= SHA1M_E;
11971
11972 return (PARSER_OK);
11973 }
11974
11975 int sha1b64s_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11976 {
11977 if ((input_len < DISPLAY_LEN_MIN_111) || (input_len > DISPLAY_LEN_MAX_111)) return (PARSER_GLOBAL_LENGTH);
11978
11979 if (memcmp (SIGNATURE_SSHA1B64_lower, input_buf, 6) && memcmp (SIGNATURE_SSHA1B64_upper, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
11980
11981 u32 *digest = (u32 *) hash_buf->digest;
11982
11983 salt_t *salt = hash_buf->salt;
11984
11985 u8 tmp_buf[100] = { 0 };
11986
11987 int tmp_len = base64_decode (base64_to_int, (const u8 *) input_buf + 6, input_len - 6, tmp_buf);
11988
11989 if (tmp_len < 20) return (PARSER_HASH_LENGTH);
11990
11991 memcpy (digest, tmp_buf, 20);
11992
11993 int salt_len = tmp_len - 20;
11994
11995 if (salt_len < 0) return (PARSER_SALT_LENGTH);
11996
11997 salt->salt_len = salt_len;
11998
11999 memcpy (salt->salt_buf, tmp_buf + 20, salt->salt_len);
12000
12001 if (data.opts_type & OPTS_TYPE_ST_ADD80)
12002 {
12003 char *ptr = (char *) salt->salt_buf;
12004
12005 ptr[salt->salt_len] = 0x80;
12006 }
12007
12008 digest[0] = byte_swap_32 (digest[0]);
12009 digest[1] = byte_swap_32 (digest[1]);
12010 digest[2] = byte_swap_32 (digest[2]);
12011 digest[3] = byte_swap_32 (digest[3]);
12012 digest[4] = byte_swap_32 (digest[4]);
12013
12014 digest[0] -= SHA1M_A;
12015 digest[1] -= SHA1M_B;
12016 digest[2] -= SHA1M_C;
12017 digest[3] -= SHA1M_D;
12018 digest[4] -= SHA1M_E;
12019
12020 return (PARSER_OK);
12021 }
12022
12023 int mssql2000_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12024 {
12025 if ((input_len < DISPLAY_LEN_MIN_131) || (input_len > DISPLAY_LEN_MAX_131)) return (PARSER_GLOBAL_LENGTH);
12026
12027 if (memcmp (SIGNATURE_MSSQL, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
12028
12029 u32 *digest = (u32 *) hash_buf->digest;
12030
12031 salt_t *salt = hash_buf->salt;
12032
12033 char *salt_buf = input_buf + 6;
12034
12035 uint salt_len = 8;
12036
12037 char *salt_buf_ptr = (char *) salt->salt_buf;
12038
12039 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
12040
12041 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
12042
12043 salt->salt_len = salt_len;
12044
12045 char *hash_pos = input_buf + 6 + 8 + 40;
12046
12047 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
12048 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
12049 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
12050 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
12051 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
12052
12053 digest[0] -= SHA1M_A;
12054 digest[1] -= SHA1M_B;
12055 digest[2] -= SHA1M_C;
12056 digest[3] -= SHA1M_D;
12057 digest[4] -= SHA1M_E;
12058
12059 return (PARSER_OK);
12060 }
12061
12062 int mssql2005_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12063 {
12064 if ((input_len < DISPLAY_LEN_MIN_132) || (input_len > DISPLAY_LEN_MAX_132)) return (PARSER_GLOBAL_LENGTH);
12065
12066 if (memcmp (SIGNATURE_MSSQL, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
12067
12068 u32 *digest = (u32 *) hash_buf->digest;
12069
12070 salt_t *salt = hash_buf->salt;
12071
12072 char *salt_buf = input_buf + 6;
12073
12074 uint salt_len = 8;
12075
12076 char *salt_buf_ptr = (char *) salt->salt_buf;
12077
12078 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
12079
12080 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
12081
12082 salt->salt_len = salt_len;
12083
12084 char *hash_pos = input_buf + 6 + 8;
12085
12086 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
12087 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
12088 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
12089 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
12090 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
12091
12092 digest[0] -= SHA1M_A;
12093 digest[1] -= SHA1M_B;
12094 digest[2] -= SHA1M_C;
12095 digest[3] -= SHA1M_D;
12096 digest[4] -= SHA1M_E;
12097
12098 return (PARSER_OK);
12099 }
12100
12101 int mssql2012_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12102 {
12103 if ((input_len < DISPLAY_LEN_MIN_1731) || (input_len > DISPLAY_LEN_MAX_1731)) return (PARSER_GLOBAL_LENGTH);
12104
12105 if (memcmp (SIGNATURE_MSSQL2012, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
12106
12107 u64 *digest = (u64 *) hash_buf->digest;
12108
12109 salt_t *salt = hash_buf->salt;
12110
12111 char *salt_buf = input_buf + 6;
12112
12113 uint salt_len = 8;
12114
12115 char *salt_buf_ptr = (char *) salt->salt_buf;
12116
12117 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
12118
12119 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
12120
12121 salt->salt_len = salt_len;
12122
12123 char *hash_pos = input_buf + 6 + 8;
12124
12125 digest[0] = hex_to_u64 ((const u8 *) &hash_pos[ 0]);
12126 digest[1] = hex_to_u64 ((const u8 *) &hash_pos[ 16]);
12127 digest[2] = hex_to_u64 ((const u8 *) &hash_pos[ 32]);
12128 digest[3] = hex_to_u64 ((const u8 *) &hash_pos[ 48]);
12129 digest[4] = hex_to_u64 ((const u8 *) &hash_pos[ 64]);
12130 digest[5] = hex_to_u64 ((const u8 *) &hash_pos[ 80]);
12131 digest[6] = hex_to_u64 ((const u8 *) &hash_pos[ 96]);
12132 digest[7] = hex_to_u64 ((const u8 *) &hash_pos[112]);
12133
12134 digest[0] -= SHA512M_A;
12135 digest[1] -= SHA512M_B;
12136 digest[2] -= SHA512M_C;
12137 digest[3] -= SHA512M_D;
12138 digest[4] -= SHA512M_E;
12139 digest[5] -= SHA512M_F;
12140 digest[6] -= SHA512M_G;
12141 digest[7] -= SHA512M_H;
12142
12143 return (PARSER_OK);
12144 }
12145
12146 int oracleh_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12147 {
12148 if (data.opts_type & OPTS_TYPE_ST_HEX)
12149 {
12150 if ((input_len < DISPLAY_LEN_MIN_3100H) || (input_len > DISPLAY_LEN_MAX_3100H)) return (PARSER_GLOBAL_LENGTH);
12151 }
12152 else
12153 {
12154 if ((input_len < DISPLAY_LEN_MIN_3100) || (input_len > DISPLAY_LEN_MAX_3100)) return (PARSER_GLOBAL_LENGTH);
12155 }
12156
12157 u32 *digest = (u32 *) hash_buf->digest;
12158
12159 salt_t *salt = hash_buf->salt;
12160
12161 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
12162 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
12163 digest[2] = 0;
12164 digest[3] = 0;
12165
12166 digest[0] = byte_swap_32 (digest[0]);
12167 digest[1] = byte_swap_32 (digest[1]);
12168
12169 if (input_buf[16] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
12170
12171 uint salt_len = input_len - 16 - 1;
12172
12173 char *salt_buf = input_buf + 16 + 1;
12174
12175 char *salt_buf_ptr = (char *) salt->salt_buf;
12176
12177 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
12178
12179 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
12180
12181 salt->salt_len = salt_len;
12182
12183 return (PARSER_OK);
12184 }
12185
12186 int oracles_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12187 {
12188 if ((input_len < DISPLAY_LEN_MIN_112) || (input_len > DISPLAY_LEN_MAX_112)) return (PARSER_GLOBAL_LENGTH);
12189
12190 u32 *digest = (u32 *) hash_buf->digest;
12191
12192 salt_t *salt = hash_buf->salt;
12193
12194 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
12195 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
12196 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
12197 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
12198 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
12199
12200 digest[0] -= SHA1M_A;
12201 digest[1] -= SHA1M_B;
12202 digest[2] -= SHA1M_C;
12203 digest[3] -= SHA1M_D;
12204 digest[4] -= SHA1M_E;
12205
12206 if (input_buf[40] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
12207
12208 uint salt_len = input_len - 40 - 1;
12209
12210 char *salt_buf = input_buf + 40 + 1;
12211
12212 char *salt_buf_ptr = (char *) salt->salt_buf;
12213
12214 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
12215
12216 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
12217
12218 salt->salt_len = salt_len;
12219
12220 return (PARSER_OK);
12221 }
12222
12223 int oraclet_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12224 {
12225 if ((input_len < DISPLAY_LEN_MIN_12300) || (input_len > DISPLAY_LEN_MAX_12300)) return (PARSER_GLOBAL_LENGTH);
12226
12227 u32 *digest = (u32 *) hash_buf->digest;
12228
12229 salt_t *salt = hash_buf->salt;
12230
12231 char *hash_pos = input_buf;
12232
12233 digest[ 0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
12234 digest[ 1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
12235 digest[ 2] = hex_to_u32 ((const u8 *) &hash_pos[ 16]);
12236 digest[ 3] = hex_to_u32 ((const u8 *) &hash_pos[ 24]);
12237 digest[ 4] = hex_to_u32 ((const u8 *) &hash_pos[ 32]);
12238 digest[ 5] = hex_to_u32 ((const u8 *) &hash_pos[ 40]);
12239 digest[ 6] = hex_to_u32 ((const u8 *) &hash_pos[ 48]);
12240 digest[ 7] = hex_to_u32 ((const u8 *) &hash_pos[ 56]);
12241 digest[ 8] = hex_to_u32 ((const u8 *) &hash_pos[ 64]);
12242 digest[ 9] = hex_to_u32 ((const u8 *) &hash_pos[ 72]);
12243 digest[10] = hex_to_u32 ((const u8 *) &hash_pos[ 80]);
12244 digest[11] = hex_to_u32 ((const u8 *) &hash_pos[ 88]);
12245 digest[12] = hex_to_u32 ((const u8 *) &hash_pos[ 96]);
12246 digest[13] = hex_to_u32 ((const u8 *) &hash_pos[104]);
12247 digest[14] = hex_to_u32 ((const u8 *) &hash_pos[112]);
12248 digest[15] = hex_to_u32 ((const u8 *) &hash_pos[120]);
12249
12250 char *salt_pos = input_buf + 128;
12251
12252 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_pos[ 0]);
12253 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_pos[ 8]);
12254 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &salt_pos[16]);
12255 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &salt_pos[24]);
12256
12257 salt->salt_iter = ROUNDS_ORACLET - 1;
12258 salt->salt_len = 16;
12259
12260 return (PARSER_OK);
12261 }
12262
12263 int sha256_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12264 {
12265 if ((input_len < DISPLAY_LEN_MIN_1400) || (input_len > DISPLAY_LEN_MAX_1400)) return (PARSER_GLOBAL_LENGTH);
12266
12267 u32 *digest = (u32 *) hash_buf->digest;
12268
12269 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
12270 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
12271 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
12272 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
12273 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
12274 digest[5] = hex_to_u32 ((const u8 *) &input_buf[40]);
12275 digest[6] = hex_to_u32 ((const u8 *) &input_buf[48]);
12276 digest[7] = hex_to_u32 ((const u8 *) &input_buf[56]);
12277
12278 digest[0] -= SHA256M_A;
12279 digest[1] -= SHA256M_B;
12280 digest[2] -= SHA256M_C;
12281 digest[3] -= SHA256M_D;
12282 digest[4] -= SHA256M_E;
12283 digest[5] -= SHA256M_F;
12284 digest[6] -= SHA256M_G;
12285 digest[7] -= SHA256M_H;
12286
12287 return (PARSER_OK);
12288 }
12289
12290 int sha256s_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12291 {
12292 if (data.opts_type & OPTS_TYPE_ST_HEX)
12293 {
12294 if ((input_len < DISPLAY_LEN_MIN_1410H) || (input_len > DISPLAY_LEN_MAX_1410H)) return (PARSER_GLOBAL_LENGTH);
12295 }
12296 else
12297 {
12298 if ((input_len < DISPLAY_LEN_MIN_1410) || (input_len > DISPLAY_LEN_MAX_1410)) return (PARSER_GLOBAL_LENGTH);
12299 }
12300
12301 u32 *digest = (u32 *) hash_buf->digest;
12302
12303 salt_t *salt = hash_buf->salt;
12304
12305 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
12306 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
12307 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
12308 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
12309 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
12310 digest[5] = hex_to_u32 ((const u8 *) &input_buf[40]);
12311 digest[6] = hex_to_u32 ((const u8 *) &input_buf[48]);
12312 digest[7] = hex_to_u32 ((const u8 *) &input_buf[56]);
12313
12314 digest[0] -= SHA256M_A;
12315 digest[1] -= SHA256M_B;
12316 digest[2] -= SHA256M_C;
12317 digest[3] -= SHA256M_D;
12318 digest[4] -= SHA256M_E;
12319 digest[5] -= SHA256M_F;
12320 digest[6] -= SHA256M_G;
12321 digest[7] -= SHA256M_H;
12322
12323 if (input_buf[64] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
12324
12325 uint salt_len = input_len - 64 - 1;
12326
12327 char *salt_buf = input_buf + 64 + 1;
12328
12329 char *salt_buf_ptr = (char *) salt->salt_buf;
12330
12331 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
12332
12333 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
12334
12335 salt->salt_len = salt_len;
12336
12337 return (PARSER_OK);
12338 }
12339
12340 int sha384_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12341 {
12342 if ((input_len < DISPLAY_LEN_MIN_10800) || (input_len > DISPLAY_LEN_MAX_10800)) return (PARSER_GLOBAL_LENGTH);
12343
12344 u64 *digest = (u64 *) hash_buf->digest;
12345
12346 digest[0] = hex_to_u64 ((const u8 *) &input_buf[ 0]);
12347 digest[1] = hex_to_u64 ((const u8 *) &input_buf[ 16]);
12348 digest[2] = hex_to_u64 ((const u8 *) &input_buf[ 32]);
12349 digest[3] = hex_to_u64 ((const u8 *) &input_buf[ 48]);
12350 digest[4] = hex_to_u64 ((const u8 *) &input_buf[ 64]);
12351 digest[5] = hex_to_u64 ((const u8 *) &input_buf[ 80]);
12352 digest[6] = 0;
12353 digest[7] = 0;
12354
12355 digest[0] -= SHA384M_A;
12356 digest[1] -= SHA384M_B;
12357 digest[2] -= SHA384M_C;
12358 digest[3] -= SHA384M_D;
12359 digest[4] -= SHA384M_E;
12360 digest[5] -= SHA384M_F;
12361 digest[6] -= 0;
12362 digest[7] -= 0;
12363
12364 return (PARSER_OK);
12365 }
12366
12367 int sha512_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12368 {
12369 if ((input_len < DISPLAY_LEN_MIN_1700) || (input_len > DISPLAY_LEN_MAX_1700)) return (PARSER_GLOBAL_LENGTH);
12370
12371 u64 *digest = (u64 *) hash_buf->digest;
12372
12373 digest[0] = hex_to_u64 ((const u8 *) &input_buf[ 0]);
12374 digest[1] = hex_to_u64 ((const u8 *) &input_buf[ 16]);
12375 digest[2] = hex_to_u64 ((const u8 *) &input_buf[ 32]);
12376 digest[3] = hex_to_u64 ((const u8 *) &input_buf[ 48]);
12377 digest[4] = hex_to_u64 ((const u8 *) &input_buf[ 64]);
12378 digest[5] = hex_to_u64 ((const u8 *) &input_buf[ 80]);
12379 digest[6] = hex_to_u64 ((const u8 *) &input_buf[ 96]);
12380 digest[7] = hex_to_u64 ((const u8 *) &input_buf[112]);
12381
12382 digest[0] -= SHA512M_A;
12383 digest[1] -= SHA512M_B;
12384 digest[2] -= SHA512M_C;
12385 digest[3] -= SHA512M_D;
12386 digest[4] -= SHA512M_E;
12387 digest[5] -= SHA512M_F;
12388 digest[6] -= SHA512M_G;
12389 digest[7] -= SHA512M_H;
12390
12391 return (PARSER_OK);
12392 }
12393
12394 int sha512s_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12395 {
12396 if (data.opts_type & OPTS_TYPE_ST_HEX)
12397 {
12398 if ((input_len < DISPLAY_LEN_MIN_1710H) || (input_len > DISPLAY_LEN_MAX_1710H)) return (PARSER_GLOBAL_LENGTH);
12399 }
12400 else
12401 {
12402 if ((input_len < DISPLAY_LEN_MIN_1710) || (input_len > DISPLAY_LEN_MAX_1710)) return (PARSER_GLOBAL_LENGTH);
12403 }
12404
12405 u64 *digest = (u64 *) hash_buf->digest;
12406
12407 salt_t *salt = hash_buf->salt;
12408
12409 digest[0] = hex_to_u64 ((const u8 *) &input_buf[ 0]);
12410 digest[1] = hex_to_u64 ((const u8 *) &input_buf[ 16]);
12411 digest[2] = hex_to_u64 ((const u8 *) &input_buf[ 32]);
12412 digest[3] = hex_to_u64 ((const u8 *) &input_buf[ 48]);
12413 digest[4] = hex_to_u64 ((const u8 *) &input_buf[ 64]);
12414 digest[5] = hex_to_u64 ((const u8 *) &input_buf[ 80]);
12415 digest[6] = hex_to_u64 ((const u8 *) &input_buf[ 96]);
12416 digest[7] = hex_to_u64 ((const u8 *) &input_buf[112]);
12417
12418 digest[0] -= SHA512M_A;
12419 digest[1] -= SHA512M_B;
12420 digest[2] -= SHA512M_C;
12421 digest[3] -= SHA512M_D;
12422 digest[4] -= SHA512M_E;
12423 digest[5] -= SHA512M_F;
12424 digest[6] -= SHA512M_G;
12425 digest[7] -= SHA512M_H;
12426
12427 if (input_buf[128] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
12428
12429 uint salt_len = input_len - 128 - 1;
12430
12431 char *salt_buf = input_buf + 128 + 1;
12432
12433 char *salt_buf_ptr = (char *) salt->salt_buf;
12434
12435 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
12436
12437 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
12438
12439 salt->salt_len = salt_len;
12440
12441 return (PARSER_OK);
12442 }
12443
12444 int sha512crypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12445 {
12446 if (memcmp (SIGNATURE_SHA512CRYPT, input_buf, 3)) return (PARSER_SIGNATURE_UNMATCHED);
12447
12448 u64 *digest = (u64 *) hash_buf->digest;
12449
12450 salt_t *salt = hash_buf->salt;
12451
12452 char *salt_pos = input_buf + 3;
12453
12454 uint iterations_len = 0;
12455
12456 if (memcmp (salt_pos, "rounds=", 7) == 0)
12457 {
12458 salt_pos += 7;
12459
12460 for (iterations_len = 0; salt_pos[0] >= '0' && salt_pos[0] <= '9' && iterations_len < 7; iterations_len++, salt_pos += 1) continue;
12461
12462 if (iterations_len == 0 ) return (PARSER_SALT_ITERATION);
12463 if (salt_pos[0] != '$') return (PARSER_SIGNATURE_UNMATCHED);
12464
12465 salt_pos[0] = 0x0;
12466
12467 salt->salt_iter = atoi (salt_pos - iterations_len);
12468
12469 salt_pos += 1;
12470
12471 iterations_len += 8;
12472 }
12473 else
12474 {
12475 salt->salt_iter = ROUNDS_SHA512CRYPT;
12476 }
12477
12478 if ((input_len < DISPLAY_LEN_MIN_1800) || (input_len > DISPLAY_LEN_MAX_1800 + iterations_len)) return (PARSER_GLOBAL_LENGTH);
12479
12480 char *hash_pos = strchr (salt_pos, '$');
12481
12482 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12483
12484 uint salt_len = hash_pos - salt_pos;
12485
12486 if (salt_len > 16) return (PARSER_SALT_LENGTH);
12487
12488 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
12489
12490 salt->salt_len = salt_len;
12491
12492 hash_pos++;
12493
12494 sha512crypt_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
12495
12496 return (PARSER_OK);
12497 }
12498
12499 int keccak_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12500 {
12501 if ((input_len < DISPLAY_LEN_MIN_5000) || (input_len > DISPLAY_LEN_MAX_5000)) return (PARSER_GLOBAL_LENGTH);
12502
12503 if (input_len % 16) return (PARSER_GLOBAL_LENGTH);
12504
12505 u64 *digest = (u64 *) hash_buf->digest;
12506
12507 salt_t *salt = hash_buf->salt;
12508
12509 uint keccak_mdlen = input_len / 2;
12510
12511 for (uint i = 0; i < keccak_mdlen / 8; i++)
12512 {
12513 digest[i] = hex_to_u64 ((const u8 *) &input_buf[i * 16]);
12514
12515 digest[i] = byte_swap_64 (digest[i]);
12516 }
12517
12518 salt->keccak_mdlen = keccak_mdlen;
12519
12520 return (PARSER_OK);
12521 }
12522
12523 int ikepsk_md5_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12524 {
12525 if ((input_len < DISPLAY_LEN_MIN_5300) || (input_len > DISPLAY_LEN_MAX_5300)) return (PARSER_GLOBAL_LENGTH);
12526
12527 u32 *digest = (u32 *) hash_buf->digest;
12528
12529 salt_t *salt = hash_buf->salt;
12530
12531 ikepsk_t *ikepsk = (ikepsk_t *) hash_buf->esalt;
12532
12533 /**
12534 * Parse that strange long line
12535 */
12536
12537 char *in_off[9];
12538
12539 size_t in_len[9] = { 0 };
12540
12541 in_off[0] = strtok (input_buf, ":");
12542
12543 if (in_off[0] == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12544
12545 in_len[0] = strlen (in_off[0]);
12546
12547 size_t i;
12548
12549 for (i = 1; i < 9; i++)
12550 {
12551 in_off[i] = strtok (NULL, ":");
12552
12553 if (in_off[i] == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12554
12555 in_len[i] = strlen (in_off[i]);
12556 }
12557
12558 char *ptr = (char *) ikepsk->msg_buf;
12559
12560 for (i = 0; i < in_len[0]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[0] + i);
12561 for (i = 0; i < in_len[1]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[1] + i);
12562 for (i = 0; i < in_len[2]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[2] + i);
12563 for (i = 0; i < in_len[3]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[3] + i);
12564 for (i = 0; i < in_len[4]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[4] + i);
12565 for (i = 0; i < in_len[5]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[5] + i);
12566
12567 *ptr = 0x80;
12568
12569 ikepsk->msg_len = (in_len[0] + in_len[1] + in_len[2] + in_len[3] + in_len[4] + in_len[5]) / 2;
12570
12571 ptr = (char *) ikepsk->nr_buf;
12572
12573 for (i = 0; i < in_len[6]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[6] + i);
12574 for (i = 0; i < in_len[7]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[7] + i);
12575
12576 *ptr = 0x80;
12577
12578 ikepsk->nr_len = (in_len[6] + in_len[7]) / 2;
12579
12580 /**
12581 * Store to database
12582 */
12583
12584 ptr = in_off[8];
12585
12586 digest[0] = hex_to_u32 ((const u8 *) &ptr[ 0]);
12587 digest[1] = hex_to_u32 ((const u8 *) &ptr[ 8]);
12588 digest[2] = hex_to_u32 ((const u8 *) &ptr[16]);
12589 digest[3] = hex_to_u32 ((const u8 *) &ptr[24]);
12590
12591 digest[0] = byte_swap_32 (digest[0]);
12592 digest[1] = byte_swap_32 (digest[1]);
12593 digest[2] = byte_swap_32 (digest[2]);
12594 digest[3] = byte_swap_32 (digest[3]);
12595
12596 salt->salt_len = 32;
12597
12598 salt->salt_buf[0] = ikepsk->nr_buf[0];
12599 salt->salt_buf[1] = ikepsk->nr_buf[1];
12600 salt->salt_buf[2] = ikepsk->nr_buf[2];
12601 salt->salt_buf[3] = ikepsk->nr_buf[3];
12602 salt->salt_buf[4] = ikepsk->nr_buf[4];
12603 salt->salt_buf[5] = ikepsk->nr_buf[5];
12604 salt->salt_buf[6] = ikepsk->nr_buf[6];
12605 salt->salt_buf[7] = ikepsk->nr_buf[7];
12606
12607 return (PARSER_OK);
12608 }
12609
12610 int ikepsk_sha1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12611 {
12612 if ((input_len < DISPLAY_LEN_MIN_5400) || (input_len > DISPLAY_LEN_MAX_5400)) return (PARSER_GLOBAL_LENGTH);
12613
12614 u32 *digest = (u32 *) hash_buf->digest;
12615
12616 salt_t *salt = hash_buf->salt;
12617
12618 ikepsk_t *ikepsk = (ikepsk_t *) hash_buf->esalt;
12619
12620 /**
12621 * Parse that strange long line
12622 */
12623
12624 char *in_off[9];
12625
12626 size_t in_len[9] = { 0 };
12627
12628 in_off[0] = strtok (input_buf, ":");
12629
12630 if (in_off[0] == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12631
12632 in_len[0] = strlen (in_off[0]);
12633
12634 size_t i;
12635
12636 for (i = 1; i < 9; i++)
12637 {
12638 in_off[i] = strtok (NULL, ":");
12639
12640 if (in_off[i] == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12641
12642 in_len[i] = strlen (in_off[i]);
12643 }
12644
12645 char *ptr = (char *) ikepsk->msg_buf;
12646
12647 for (i = 0; i < in_len[0]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[0] + i);
12648 for (i = 0; i < in_len[1]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[1] + i);
12649 for (i = 0; i < in_len[2]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[2] + i);
12650 for (i = 0; i < in_len[3]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[3] + i);
12651 for (i = 0; i < in_len[4]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[4] + i);
12652 for (i = 0; i < in_len[5]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[5] + i);
12653
12654 *ptr = 0x80;
12655
12656 ikepsk->msg_len = (in_len[0] + in_len[1] + in_len[2] + in_len[3] + in_len[4] + in_len[5]) / 2;
12657
12658 ptr = (char *) ikepsk->nr_buf;
12659
12660 for (i = 0; i < in_len[6]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[6] + i);
12661 for (i = 0; i < in_len[7]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[7] + i);
12662
12663 *ptr = 0x80;
12664
12665 ikepsk->nr_len = (in_len[6] + in_len[7]) / 2;
12666
12667 /**
12668 * Store to database
12669 */
12670
12671 ptr = in_off[8];
12672
12673 digest[0] = hex_to_u32 ((const u8 *) &ptr[ 0]);
12674 digest[1] = hex_to_u32 ((const u8 *) &ptr[ 8]);
12675 digest[2] = hex_to_u32 ((const u8 *) &ptr[16]);
12676 digest[3] = hex_to_u32 ((const u8 *) &ptr[24]);
12677 digest[4] = hex_to_u32 ((const u8 *) &ptr[32]);
12678
12679 salt->salt_len = 32;
12680
12681 salt->salt_buf[0] = ikepsk->nr_buf[0];
12682 salt->salt_buf[1] = ikepsk->nr_buf[1];
12683 salt->salt_buf[2] = ikepsk->nr_buf[2];
12684 salt->salt_buf[3] = ikepsk->nr_buf[3];
12685 salt->salt_buf[4] = ikepsk->nr_buf[4];
12686 salt->salt_buf[5] = ikepsk->nr_buf[5];
12687 salt->salt_buf[6] = ikepsk->nr_buf[6];
12688 salt->salt_buf[7] = ikepsk->nr_buf[7];
12689
12690 return (PARSER_OK);
12691 }
12692
12693 int ripemd160_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12694 {
12695 if ((input_len < DISPLAY_LEN_MIN_6000) || (input_len > DISPLAY_LEN_MAX_6000)) return (PARSER_GLOBAL_LENGTH);
12696
12697 u32 *digest = (u32 *) hash_buf->digest;
12698
12699 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
12700 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
12701 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
12702 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
12703 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
12704
12705 digest[0] = byte_swap_32 (digest[0]);
12706 digest[1] = byte_swap_32 (digest[1]);
12707 digest[2] = byte_swap_32 (digest[2]);
12708 digest[3] = byte_swap_32 (digest[3]);
12709 digest[4] = byte_swap_32 (digest[4]);
12710
12711 return (PARSER_OK);
12712 }
12713
12714 int whirlpool_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12715 {
12716 if ((input_len < DISPLAY_LEN_MIN_6100) || (input_len > DISPLAY_LEN_MAX_6100)) return (PARSER_GLOBAL_LENGTH);
12717
12718 u32 *digest = (u32 *) hash_buf->digest;
12719
12720 digest[ 0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
12721 digest[ 1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
12722 digest[ 2] = hex_to_u32 ((const u8 *) &input_buf[ 16]);
12723 digest[ 3] = hex_to_u32 ((const u8 *) &input_buf[ 24]);
12724 digest[ 4] = hex_to_u32 ((const u8 *) &input_buf[ 32]);
12725 digest[ 5] = hex_to_u32 ((const u8 *) &input_buf[ 40]);
12726 digest[ 6] = hex_to_u32 ((const u8 *) &input_buf[ 48]);
12727 digest[ 7] = hex_to_u32 ((const u8 *) &input_buf[ 56]);
12728 digest[ 8] = hex_to_u32 ((const u8 *) &input_buf[ 64]);
12729 digest[ 9] = hex_to_u32 ((const u8 *) &input_buf[ 72]);
12730 digest[10] = hex_to_u32 ((const u8 *) &input_buf[ 80]);
12731 digest[11] = hex_to_u32 ((const u8 *) &input_buf[ 88]);
12732 digest[12] = hex_to_u32 ((const u8 *) &input_buf[ 96]);
12733 digest[13] = hex_to_u32 ((const u8 *) &input_buf[104]);
12734 digest[14] = hex_to_u32 ((const u8 *) &input_buf[112]);
12735 digest[15] = hex_to_u32 ((const u8 *) &input_buf[120]);
12736
12737 return (PARSER_OK);
12738 }
12739
12740 int androidpin_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12741 {
12742 if ((input_len < DISPLAY_LEN_MIN_5800) || (input_len > DISPLAY_LEN_MAX_5800)) return (PARSER_GLOBAL_LENGTH);
12743
12744 u32 *digest = (u32 *) hash_buf->digest;
12745
12746 salt_t *salt = hash_buf->salt;
12747
12748 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
12749 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
12750 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
12751 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
12752 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
12753
12754 if (input_buf[40] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
12755
12756 uint salt_len = input_len - 40 - 1;
12757
12758 char *salt_buf = input_buf + 40 + 1;
12759
12760 char *salt_buf_ptr = (char *) salt->salt_buf;
12761
12762 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
12763
12764 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
12765
12766 salt->salt_len = salt_len;
12767
12768 salt->salt_iter = ROUNDS_ANDROIDPIN - 1;
12769
12770 return (PARSER_OK);
12771 }
12772
12773 int truecrypt_parse_hash_1k (char *input_buf, uint input_len, hash_t *hash_buf)
12774 {
12775 u32 *digest = (u32 *) hash_buf->digest;
12776
12777 salt_t *salt = hash_buf->salt;
12778
12779 tc_t *tc = (tc_t *) hash_buf->esalt;
12780
12781 if (input_len == 0)
12782 {
12783 log_error ("TrueCrypt container not specified");
12784
12785 exit (-1);
12786 }
12787
12788 FILE *fp = fopen (input_buf, "rb");
12789
12790 if (fp == NULL)
12791 {
12792 log_error ("%s: %s", input_buf, strerror (errno));
12793
12794 exit (-1);
12795 }
12796
12797 char buf[512] = { 0 };
12798
12799 int n = fread (buf, 1, sizeof (buf), fp);
12800
12801 fclose (fp);
12802
12803 if (n != 512) return (PARSER_TC_FILE_SIZE);
12804
12805 memcpy (tc->salt_buf, buf, 64);
12806
12807 memcpy (tc->data_buf, buf + 64, 512 - 64);
12808
12809 salt->salt_buf[0] = tc->salt_buf[0];
12810
12811 salt->salt_len = 4;
12812
12813 salt->salt_iter = ROUNDS_TRUECRYPT_1K - 1;
12814
12815 tc->signature = 0x45555254; // "TRUE"
12816
12817 digest[0] = tc->data_buf[0];
12818
12819 return (PARSER_OK);
12820 }
12821
12822 int truecrypt_parse_hash_2k (char *input_buf, uint input_len, hash_t *hash_buf)
12823 {
12824 u32 *digest = (u32 *) hash_buf->digest;
12825
12826 salt_t *salt = hash_buf->salt;
12827
12828 tc_t *tc = (tc_t *) hash_buf->esalt;
12829
12830 if (input_len == 0)
12831 {
12832 log_error ("TrueCrypt container not specified");
12833
12834 exit (-1);
12835 }
12836
12837 FILE *fp = fopen (input_buf, "rb");
12838
12839 if (fp == NULL)
12840 {
12841 log_error ("%s: %s", input_buf, strerror (errno));
12842
12843 exit (-1);
12844 }
12845
12846 char buf[512] = { 0 };
12847
12848 int n = fread (buf, 1, sizeof (buf), fp);
12849
12850 fclose (fp);
12851
12852 if (n != 512) return (PARSER_TC_FILE_SIZE);
12853
12854 memcpy (tc->salt_buf, buf, 64);
12855
12856 memcpy (tc->data_buf, buf + 64, 512 - 64);
12857
12858 salt->salt_buf[0] = tc->salt_buf[0];
12859
12860 salt->salt_len = 4;
12861
12862 salt->salt_iter = ROUNDS_TRUECRYPT_2K - 1;
12863
12864 tc->signature = 0x45555254; // "TRUE"
12865
12866 digest[0] = tc->data_buf[0];
12867
12868 return (PARSER_OK);
12869 }
12870
12871 int veracrypt_parse_hash_200000 (char *input_buf, uint input_len, hash_t *hash_buf)
12872 {
12873 u32 *digest = (u32 *) hash_buf->digest;
12874
12875 salt_t *salt = hash_buf->salt;
12876
12877 tc_t *tc = (tc_t *) hash_buf->esalt;
12878
12879 if (input_len == 0)
12880 {
12881 log_error ("VeraCrypt container not specified");
12882
12883 exit (-1);
12884 }
12885
12886 FILE *fp = fopen (input_buf, "rb");
12887
12888 if (fp == NULL)
12889 {
12890 log_error ("%s: %s", input_buf, strerror (errno));
12891
12892 exit (-1);
12893 }
12894
12895 char buf[512] = { 0 };
12896
12897 int n = fread (buf, 1, sizeof (buf), fp);
12898
12899 fclose (fp);
12900
12901 if (n != 512) return (PARSER_VC_FILE_SIZE);
12902
12903 memcpy (tc->salt_buf, buf, 64);
12904
12905 memcpy (tc->data_buf, buf + 64, 512 - 64);
12906
12907 salt->salt_buf[0] = tc->salt_buf[0];
12908
12909 salt->salt_len = 4;
12910
12911 salt->salt_iter = ROUNDS_VERACRYPT_200000 - 1;
12912
12913 tc->signature = 0x41524556; // "VERA"
12914
12915 digest[0] = tc->data_buf[0];
12916
12917 return (PARSER_OK);
12918 }
12919
12920 int veracrypt_parse_hash_500000 (char *input_buf, uint input_len, hash_t *hash_buf)
12921 {
12922 u32 *digest = (u32 *) hash_buf->digest;
12923
12924 salt_t *salt = hash_buf->salt;
12925
12926 tc_t *tc = (tc_t *) hash_buf->esalt;
12927
12928 if (input_len == 0)
12929 {
12930 log_error ("VeraCrypt container not specified");
12931
12932 exit (-1);
12933 }
12934
12935 FILE *fp = fopen (input_buf, "rb");
12936
12937 if (fp == NULL)
12938 {
12939 log_error ("%s: %s", input_buf, strerror (errno));
12940
12941 exit (-1);
12942 }
12943
12944 char buf[512] = { 0 };
12945
12946 int n = fread (buf, 1, sizeof (buf), fp);
12947
12948 fclose (fp);
12949
12950 if (n != 512) return (PARSER_VC_FILE_SIZE);
12951
12952 memcpy (tc->salt_buf, buf, 64);
12953
12954 memcpy (tc->data_buf, buf + 64, 512 - 64);
12955
12956 salt->salt_buf[0] = tc->salt_buf[0];
12957
12958 salt->salt_len = 4;
12959
12960 salt->salt_iter = ROUNDS_VERACRYPT_500000 - 1;
12961
12962 tc->signature = 0x41524556; // "VERA"
12963
12964 digest[0] = tc->data_buf[0];
12965
12966 return (PARSER_OK);
12967 }
12968
12969 int veracrypt_parse_hash_327661 (char *input_buf, uint input_len, hash_t *hash_buf)
12970 {
12971 u32 *digest = (u32 *) hash_buf->digest;
12972
12973 salt_t *salt = hash_buf->salt;
12974
12975 tc_t *tc = (tc_t *) hash_buf->esalt;
12976
12977 if (input_len == 0)
12978 {
12979 log_error ("VeraCrypt container not specified");
12980
12981 exit (-1);
12982 }
12983
12984 FILE *fp = fopen (input_buf, "rb");
12985
12986 if (fp == NULL)
12987 {
12988 log_error ("%s: %s", input_buf, strerror (errno));
12989
12990 exit (-1);
12991 }
12992
12993 char buf[512] = { 0 };
12994
12995 int n = fread (buf, 1, sizeof (buf), fp);
12996
12997 fclose (fp);
12998
12999 if (n != 512) return (PARSER_VC_FILE_SIZE);
13000
13001 memcpy (tc->salt_buf, buf, 64);
13002
13003 memcpy (tc->data_buf, buf + 64, 512 - 64);
13004
13005 salt->salt_buf[0] = tc->salt_buf[0];
13006
13007 salt->salt_len = 4;
13008
13009 salt->salt_iter = ROUNDS_VERACRYPT_327661 - 1;
13010
13011 tc->signature = 0x41524556; // "VERA"
13012
13013 digest[0] = tc->data_buf[0];
13014
13015 return (PARSER_OK);
13016 }
13017
13018 int veracrypt_parse_hash_655331 (char *input_buf, uint input_len, hash_t *hash_buf)
13019 {
13020 u32 *digest = (u32 *) hash_buf->digest;
13021
13022 salt_t *salt = hash_buf->salt;
13023
13024 tc_t *tc = (tc_t *) hash_buf->esalt;
13025
13026 if (input_len == 0)
13027 {
13028 log_error ("VeraCrypt container not specified");
13029
13030 exit (-1);
13031 }
13032
13033 FILE *fp = fopen (input_buf, "rb");
13034
13035 if (fp == NULL)
13036 {
13037 log_error ("%s: %s", input_buf, strerror (errno));
13038
13039 exit (-1);
13040 }
13041
13042 char buf[512] = { 0 };
13043
13044 int n = fread (buf, 1, sizeof (buf), fp);
13045
13046 fclose (fp);
13047
13048 if (n != 512) return (PARSER_VC_FILE_SIZE);
13049
13050 memcpy (tc->salt_buf, buf, 64);
13051
13052 memcpy (tc->data_buf, buf + 64, 512 - 64);
13053
13054 salt->salt_buf[0] = tc->salt_buf[0];
13055
13056 salt->salt_len = 4;
13057
13058 salt->salt_iter = ROUNDS_VERACRYPT_655331 - 1;
13059
13060 tc->signature = 0x41524556; // "VERA"
13061
13062 digest[0] = tc->data_buf[0];
13063
13064 return (PARSER_OK);
13065 }
13066
13067 int md5aix_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13068 {
13069 if ((input_len < DISPLAY_LEN_MIN_6300) || (input_len > DISPLAY_LEN_MAX_6300)) return (PARSER_GLOBAL_LENGTH);
13070
13071 if (memcmp (SIGNATURE_MD5AIX, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
13072
13073 u32 *digest = (u32 *) hash_buf->digest;
13074
13075 salt_t *salt = hash_buf->salt;
13076
13077 char *salt_pos = input_buf + 6;
13078
13079 char *hash_pos = strchr (salt_pos, '$');
13080
13081 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13082
13083 uint salt_len = hash_pos - salt_pos;
13084
13085 if (salt_len < 8) return (PARSER_SALT_LENGTH);
13086
13087 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
13088
13089 salt->salt_len = salt_len;
13090
13091 salt->salt_iter = 1000;
13092
13093 hash_pos++;
13094
13095 md5crypt_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
13096
13097 return (PARSER_OK);
13098 }
13099
13100 int sha1aix_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13101 {
13102 if ((input_len < DISPLAY_LEN_MIN_6700) || (input_len > DISPLAY_LEN_MAX_6700)) return (PARSER_GLOBAL_LENGTH);
13103
13104 if (memcmp (SIGNATURE_SHA1AIX, input_buf, 7)) return (PARSER_SIGNATURE_UNMATCHED);
13105
13106 u32 *digest = (u32 *) hash_buf->digest;
13107
13108 salt_t *salt = hash_buf->salt;
13109
13110 char *iter_pos = input_buf + 7;
13111
13112 char *salt_pos = strchr (iter_pos, '$');
13113
13114 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13115
13116 salt_pos++;
13117
13118 char *hash_pos = strchr (salt_pos, '$');
13119
13120 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13121
13122 uint salt_len = hash_pos - salt_pos;
13123
13124 if (salt_len < 16) return (PARSER_SALT_LENGTH);
13125
13126 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
13127
13128 salt->salt_len = salt_len;
13129
13130 char salt_iter[3] = { iter_pos[0], iter_pos[1], 0 };
13131
13132 salt->salt_sign[0] = atoi (salt_iter);
13133
13134 salt->salt_iter = (1 << atoi (salt_iter)) - 1;
13135
13136 hash_pos++;
13137
13138 sha1aix_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
13139
13140 digest[0] = byte_swap_32 (digest[0]);
13141 digest[1] = byte_swap_32 (digest[1]);
13142 digest[2] = byte_swap_32 (digest[2]);
13143 digest[3] = byte_swap_32 (digest[3]);
13144 digest[4] = byte_swap_32 (digest[4]);
13145
13146 return (PARSER_OK);
13147 }
13148
13149 int sha256aix_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13150 {
13151 if ((input_len < DISPLAY_LEN_MIN_6400) || (input_len > DISPLAY_LEN_MAX_6400)) return (PARSER_GLOBAL_LENGTH);
13152
13153 if (memcmp (SIGNATURE_SHA256AIX, input_buf, 9)) return (PARSER_SIGNATURE_UNMATCHED);
13154
13155 u32 *digest = (u32 *) hash_buf->digest;
13156
13157 salt_t *salt = hash_buf->salt;
13158
13159 char *iter_pos = input_buf + 9;
13160
13161 char *salt_pos = strchr (iter_pos, '$');
13162
13163 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13164
13165 salt_pos++;
13166
13167 char *hash_pos = strchr (salt_pos, '$');
13168
13169 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13170
13171 uint salt_len = hash_pos - salt_pos;
13172
13173 if (salt_len < 16) return (PARSER_SALT_LENGTH);
13174
13175 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
13176
13177 salt->salt_len = salt_len;
13178
13179 char salt_iter[3] = { iter_pos[0], iter_pos[1], 0 };
13180
13181 salt->salt_sign[0] = atoi (salt_iter);
13182
13183 salt->salt_iter = (1 << atoi (salt_iter)) - 1;
13184
13185 hash_pos++;
13186
13187 sha256aix_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
13188
13189 digest[0] = byte_swap_32 (digest[0]);
13190 digest[1] = byte_swap_32 (digest[1]);
13191 digest[2] = byte_swap_32 (digest[2]);
13192 digest[3] = byte_swap_32 (digest[3]);
13193 digest[4] = byte_swap_32 (digest[4]);
13194 digest[5] = byte_swap_32 (digest[5]);
13195 digest[6] = byte_swap_32 (digest[6]);
13196 digest[7] = byte_swap_32 (digest[7]);
13197
13198 return (PARSER_OK);
13199 }
13200
13201 int sha512aix_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13202 {
13203 if ((input_len < DISPLAY_LEN_MIN_6500) || (input_len > DISPLAY_LEN_MAX_6500)) return (PARSER_GLOBAL_LENGTH);
13204
13205 if (memcmp (SIGNATURE_SHA512AIX, input_buf, 9)) return (PARSER_SIGNATURE_UNMATCHED);
13206
13207 u64 *digest = (u64 *) hash_buf->digest;
13208
13209 salt_t *salt = hash_buf->salt;
13210
13211 char *iter_pos = input_buf + 9;
13212
13213 char *salt_pos = strchr (iter_pos, '$');
13214
13215 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13216
13217 salt_pos++;
13218
13219 char *hash_pos = strchr (salt_pos, '$');
13220
13221 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13222
13223 uint salt_len = hash_pos - salt_pos;
13224
13225 if (salt_len < 16) return (PARSER_SALT_LENGTH);
13226
13227 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
13228
13229 salt->salt_len = salt_len;
13230
13231 char salt_iter[3] = { iter_pos[0], iter_pos[1], 0 };
13232
13233 salt->salt_sign[0] = atoi (salt_iter);
13234
13235 salt->salt_iter = (1 << atoi (salt_iter)) - 1;
13236
13237 hash_pos++;
13238
13239 sha512aix_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
13240
13241 digest[0] = byte_swap_64 (digest[0]);
13242 digest[1] = byte_swap_64 (digest[1]);
13243 digest[2] = byte_swap_64 (digest[2]);
13244 digest[3] = byte_swap_64 (digest[3]);
13245 digest[4] = byte_swap_64 (digest[4]);
13246 digest[5] = byte_swap_64 (digest[5]);
13247 digest[6] = byte_swap_64 (digest[6]);
13248 digest[7] = byte_swap_64 (digest[7]);
13249
13250 return (PARSER_OK);
13251 }
13252
13253 int agilekey_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13254 {
13255 if ((input_len < DISPLAY_LEN_MIN_6600) || (input_len > DISPLAY_LEN_MAX_6600)) return (PARSER_GLOBAL_LENGTH);
13256
13257 u32 *digest = (u32 *) hash_buf->digest;
13258
13259 salt_t *salt = hash_buf->salt;
13260
13261 agilekey_t *agilekey = (agilekey_t *) hash_buf->esalt;
13262
13263 /**
13264 * parse line
13265 */
13266
13267 char *iterations_pos = input_buf;
13268
13269 char *saltbuf_pos = strchr (iterations_pos, ':');
13270
13271 if (saltbuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13272
13273 uint iterations_len = saltbuf_pos - iterations_pos;
13274
13275 if (iterations_len > 6) return (PARSER_SALT_LENGTH);
13276
13277 saltbuf_pos++;
13278
13279 char *cipherbuf_pos = strchr (saltbuf_pos, ':');
13280
13281 if (cipherbuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13282
13283 uint saltbuf_len = cipherbuf_pos - saltbuf_pos;
13284
13285 if (saltbuf_len != 16) return (PARSER_SALT_LENGTH);
13286
13287 uint cipherbuf_len = input_len - iterations_len - 1 - saltbuf_len - 1;
13288
13289 if (cipherbuf_len != 2080) return (PARSER_HASH_LENGTH);
13290
13291 cipherbuf_pos++;
13292
13293 /**
13294 * pbkdf2 iterations
13295 */
13296
13297 salt->salt_iter = atoi (iterations_pos) - 1;
13298
13299 /**
13300 * handle salt encoding
13301 */
13302
13303 char *saltbuf_ptr = (char *) salt->salt_buf;
13304
13305 for (uint i = 0; i < saltbuf_len; i += 2)
13306 {
13307 const char p0 = saltbuf_pos[i + 0];
13308 const char p1 = saltbuf_pos[i + 1];
13309
13310 *saltbuf_ptr++ = hex_convert (p1) << 0
13311 | hex_convert (p0) << 4;
13312 }
13313
13314 salt->salt_len = saltbuf_len / 2;
13315
13316 /**
13317 * handle cipher encoding
13318 */
13319
13320 uint *tmp = (uint *) mymalloc (32);
13321
13322 char *cipherbuf_ptr = (char *) tmp;
13323
13324 for (uint i = 2016; i < cipherbuf_len; i += 2)
13325 {
13326 const char p0 = cipherbuf_pos[i + 0];
13327 const char p1 = cipherbuf_pos[i + 1];
13328
13329 *cipherbuf_ptr++ = hex_convert (p1) << 0
13330 | hex_convert (p0) << 4;
13331 }
13332
13333 // iv is stored at salt_buf 4 (length 16)
13334 // data is stored at salt_buf 8 (length 16)
13335
13336 salt->salt_buf[ 4] = byte_swap_32 (tmp[0]);
13337 salt->salt_buf[ 5] = byte_swap_32 (tmp[1]);
13338 salt->salt_buf[ 6] = byte_swap_32 (tmp[2]);
13339 salt->salt_buf[ 7] = byte_swap_32 (tmp[3]);
13340
13341 salt->salt_buf[ 8] = byte_swap_32 (tmp[4]);
13342 salt->salt_buf[ 9] = byte_swap_32 (tmp[5]);
13343 salt->salt_buf[10] = byte_swap_32 (tmp[6]);
13344 salt->salt_buf[11] = byte_swap_32 (tmp[7]);
13345
13346 free (tmp);
13347
13348 for (uint i = 0, j = 0; i < 1040; i += 1, j += 2)
13349 {
13350 const char p0 = cipherbuf_pos[j + 0];
13351 const char p1 = cipherbuf_pos[j + 1];
13352
13353 agilekey->cipher[i] = hex_convert (p1) << 0
13354 | hex_convert (p0) << 4;
13355 }
13356
13357 /**
13358 * digest buf
13359 */
13360
13361 digest[0] = 0x10101010;
13362 digest[1] = 0x10101010;
13363 digest[2] = 0x10101010;
13364 digest[3] = 0x10101010;
13365
13366 return (PARSER_OK);
13367 }
13368
13369 int lastpass_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13370 {
13371 if ((input_len < DISPLAY_LEN_MIN_6800) || (input_len > DISPLAY_LEN_MAX_6800)) return (PARSER_GLOBAL_LENGTH);
13372
13373 u32 *digest = (u32 *) hash_buf->digest;
13374
13375 salt_t *salt = hash_buf->salt;
13376
13377 char *hashbuf_pos = input_buf;
13378
13379 char *iterations_pos = strchr (hashbuf_pos, ':');
13380
13381 if (iterations_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13382
13383 uint hash_len = iterations_pos - hashbuf_pos;
13384
13385 if ((hash_len != 32) && (hash_len != 64)) return (PARSER_HASH_LENGTH);
13386
13387 iterations_pos++;
13388
13389 char *saltbuf_pos = strchr (iterations_pos, ':');
13390
13391 if (saltbuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13392
13393 uint iterations_len = saltbuf_pos - iterations_pos;
13394
13395 saltbuf_pos++;
13396
13397 uint salt_len = input_len - hash_len - 1 - iterations_len - 1;
13398
13399 if (salt_len > 32) return (PARSER_SALT_LENGTH);
13400
13401 char *salt_buf_ptr = (char *) salt->salt_buf;
13402
13403 salt_len = parse_and_store_salt (salt_buf_ptr, saltbuf_pos, salt_len);
13404
13405 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
13406
13407 salt->salt_len = salt_len;
13408
13409 salt->salt_iter = atoi (iterations_pos) - 1;
13410
13411 digest[0] = hex_to_u32 ((const u8 *) &hashbuf_pos[ 0]);
13412 digest[1] = hex_to_u32 ((const u8 *) &hashbuf_pos[ 8]);
13413 digest[2] = hex_to_u32 ((const u8 *) &hashbuf_pos[16]);
13414 digest[3] = hex_to_u32 ((const u8 *) &hashbuf_pos[24]);
13415
13416 return (PARSER_OK);
13417 }
13418
13419 int gost_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13420 {
13421 if ((input_len < DISPLAY_LEN_MIN_6900) || (input_len > DISPLAY_LEN_MAX_6900)) return (PARSER_GLOBAL_LENGTH);
13422
13423 u32 *digest = (u32 *) hash_buf->digest;
13424
13425 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
13426 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
13427 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
13428 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
13429 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
13430 digest[5] = hex_to_u32 ((const u8 *) &input_buf[40]);
13431 digest[6] = hex_to_u32 ((const u8 *) &input_buf[48]);
13432 digest[7] = hex_to_u32 ((const u8 *) &input_buf[56]);
13433
13434 digest[0] = byte_swap_32 (digest[0]);
13435 digest[1] = byte_swap_32 (digest[1]);
13436 digest[2] = byte_swap_32 (digest[2]);
13437 digest[3] = byte_swap_32 (digest[3]);
13438 digest[4] = byte_swap_32 (digest[4]);
13439 digest[5] = byte_swap_32 (digest[5]);
13440 digest[6] = byte_swap_32 (digest[6]);
13441 digest[7] = byte_swap_32 (digest[7]);
13442
13443 return (PARSER_OK);
13444 }
13445
13446 int sha256crypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13447 {
13448 if (memcmp (SIGNATURE_SHA256CRYPT, input_buf, 3)) return (PARSER_SIGNATURE_UNMATCHED);
13449
13450 u32 *digest = (u32 *) hash_buf->digest;
13451
13452 salt_t *salt = hash_buf->salt;
13453
13454 char *salt_pos = input_buf + 3;
13455
13456 uint iterations_len = 0;
13457
13458 if (memcmp (salt_pos, "rounds=", 7) == 0)
13459 {
13460 salt_pos += 7;
13461
13462 for (iterations_len = 0; salt_pos[0] >= '0' && salt_pos[0] <= '9' && iterations_len < 7; iterations_len++, salt_pos += 1) continue;
13463
13464 if (iterations_len == 0 ) return (PARSER_SALT_ITERATION);
13465 if (salt_pos[0] != '$') return (PARSER_SIGNATURE_UNMATCHED);
13466
13467 salt_pos[0] = 0x0;
13468
13469 salt->salt_iter = atoi (salt_pos - iterations_len);
13470
13471 salt_pos += 1;
13472
13473 iterations_len += 8;
13474 }
13475 else
13476 {
13477 salt->salt_iter = ROUNDS_SHA256CRYPT;
13478 }
13479
13480 if ((input_len < DISPLAY_LEN_MIN_7400) || (input_len > DISPLAY_LEN_MAX_7400 + iterations_len)) return (PARSER_GLOBAL_LENGTH);
13481
13482 char *hash_pos = strchr (salt_pos, '$');
13483
13484 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13485
13486 uint salt_len = hash_pos - salt_pos;
13487
13488 if (salt_len > 16) return (PARSER_SALT_LENGTH);
13489
13490 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
13491
13492 salt->salt_len = salt_len;
13493
13494 hash_pos++;
13495
13496 sha256crypt_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
13497
13498 return (PARSER_OK);
13499 }
13500
13501 int sha512osx_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13502 {
13503 uint max_len = DISPLAY_LEN_MAX_7100 + (2 * 128);
13504
13505 if ((input_len < DISPLAY_LEN_MIN_7100) || (input_len > max_len)) return (PARSER_GLOBAL_LENGTH);
13506
13507 if (memcmp (SIGNATURE_SHA512OSX, input_buf, 4)) return (PARSER_SIGNATURE_UNMATCHED);
13508
13509 u64 *digest = (u64 *) hash_buf->digest;
13510
13511 salt_t *salt = hash_buf->salt;
13512
13513 pbkdf2_sha512_t *pbkdf2_sha512 = (pbkdf2_sha512_t *) hash_buf->esalt;
13514
13515 char *iter_pos = input_buf + 4;
13516
13517 char *salt_pos = strchr (iter_pos, '$');
13518
13519 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13520
13521 salt_pos++;
13522
13523 char *hash_pos = strchr (salt_pos, '$');
13524
13525 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13526
13527 if (((input_len - (hash_pos - input_buf) - 1) % 128) != 0) return (PARSER_GLOBAL_LENGTH);
13528
13529 hash_pos++;
13530
13531 digest[0] = hex_to_u64 ((const u8 *) &hash_pos[ 0]);
13532 digest[1] = hex_to_u64 ((const u8 *) &hash_pos[ 16]);
13533 digest[2] = hex_to_u64 ((const u8 *) &hash_pos[ 32]);
13534 digest[3] = hex_to_u64 ((const u8 *) &hash_pos[ 48]);
13535 digest[4] = hex_to_u64 ((const u8 *) &hash_pos[ 64]);
13536 digest[5] = hex_to_u64 ((const u8 *) &hash_pos[ 80]);
13537 digest[6] = hex_to_u64 ((const u8 *) &hash_pos[ 96]);
13538 digest[7] = hex_to_u64 ((const u8 *) &hash_pos[112]);
13539
13540 uint salt_len = hash_pos - salt_pos - 1;
13541
13542 if ((salt_len % 2) != 0) return (PARSER_SALT_LENGTH);
13543
13544 salt->salt_len = salt_len / 2;
13545
13546 pbkdf2_sha512->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_pos[ 0]);
13547 pbkdf2_sha512->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_pos[ 8]);
13548 pbkdf2_sha512->salt_buf[2] = hex_to_u32 ((const u8 *) &salt_pos[16]);
13549 pbkdf2_sha512->salt_buf[3] = hex_to_u32 ((const u8 *) &salt_pos[24]);
13550 pbkdf2_sha512->salt_buf[4] = hex_to_u32 ((const u8 *) &salt_pos[32]);
13551 pbkdf2_sha512->salt_buf[5] = hex_to_u32 ((const u8 *) &salt_pos[40]);
13552 pbkdf2_sha512->salt_buf[6] = hex_to_u32 ((const u8 *) &salt_pos[48]);
13553 pbkdf2_sha512->salt_buf[7] = hex_to_u32 ((const u8 *) &salt_pos[56]);
13554
13555 pbkdf2_sha512->salt_buf[0] = byte_swap_32 (pbkdf2_sha512->salt_buf[0]);
13556 pbkdf2_sha512->salt_buf[1] = byte_swap_32 (pbkdf2_sha512->salt_buf[1]);
13557 pbkdf2_sha512->salt_buf[2] = byte_swap_32 (pbkdf2_sha512->salt_buf[2]);
13558 pbkdf2_sha512->salt_buf[3] = byte_swap_32 (pbkdf2_sha512->salt_buf[3]);
13559 pbkdf2_sha512->salt_buf[4] = byte_swap_32 (pbkdf2_sha512->salt_buf[4]);
13560 pbkdf2_sha512->salt_buf[5] = byte_swap_32 (pbkdf2_sha512->salt_buf[5]);
13561 pbkdf2_sha512->salt_buf[6] = byte_swap_32 (pbkdf2_sha512->salt_buf[6]);
13562 pbkdf2_sha512->salt_buf[7] = byte_swap_32 (pbkdf2_sha512->salt_buf[7]);
13563 pbkdf2_sha512->salt_buf[8] = 0x01000000;
13564 pbkdf2_sha512->salt_buf[9] = 0x80;
13565
13566 salt->salt_buf[0] = pbkdf2_sha512->salt_buf[0];
13567
13568 salt->salt_iter = atoi (iter_pos) - 1;
13569
13570 return (PARSER_OK);
13571 }
13572
13573 int episerver4_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13574 {
13575 if ((input_len < DISPLAY_LEN_MIN_1441) || (input_len > DISPLAY_LEN_MAX_1441)) return (PARSER_GLOBAL_LENGTH);
13576
13577 if (memcmp (SIGNATURE_EPISERVER4, input_buf, 14)) return (PARSER_SIGNATURE_UNMATCHED);
13578
13579 u32 *digest = (u32 *) hash_buf->digest;
13580
13581 salt_t *salt = hash_buf->salt;
13582
13583 char *salt_pos = input_buf + 14;
13584
13585 char *hash_pos = strchr (salt_pos, '*');
13586
13587 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13588
13589 hash_pos++;
13590
13591 uint salt_len = hash_pos - salt_pos - 1;
13592
13593 char *salt_buf_ptr = (char *) salt->salt_buf;
13594
13595 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
13596
13597 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
13598
13599 salt->salt_len = salt_len;
13600
13601 u8 tmp_buf[100] = { 0 };
13602
13603 base64_decode (base64_to_int, (const u8 *) hash_pos, 43, tmp_buf);
13604
13605 memcpy (digest, tmp_buf, 32);
13606
13607 digest[0] = byte_swap_32 (digest[0]);
13608 digest[1] = byte_swap_32 (digest[1]);
13609 digest[2] = byte_swap_32 (digest[2]);
13610 digest[3] = byte_swap_32 (digest[3]);
13611 digest[4] = byte_swap_32 (digest[4]);
13612 digest[5] = byte_swap_32 (digest[5]);
13613 digest[6] = byte_swap_32 (digest[6]);
13614 digest[7] = byte_swap_32 (digest[7]);
13615
13616 digest[0] -= SHA256M_A;
13617 digest[1] -= SHA256M_B;
13618 digest[2] -= SHA256M_C;
13619 digest[3] -= SHA256M_D;
13620 digest[4] -= SHA256M_E;
13621 digest[5] -= SHA256M_F;
13622 digest[6] -= SHA256M_G;
13623 digest[7] -= SHA256M_H;
13624
13625 return (PARSER_OK);
13626 }
13627
13628 int sha512grub_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13629 {
13630 uint max_len = DISPLAY_LEN_MAX_7200 + (8 * 128);
13631
13632 if ((input_len < DISPLAY_LEN_MIN_7200) || (input_len > max_len)) return (PARSER_GLOBAL_LENGTH);
13633
13634 if (memcmp (SIGNATURE_SHA512GRUB, input_buf, 19)) return (PARSER_SIGNATURE_UNMATCHED);
13635
13636 u64 *digest = (u64 *) hash_buf->digest;
13637
13638 salt_t *salt = hash_buf->salt;
13639
13640 pbkdf2_sha512_t *pbkdf2_sha512 = (pbkdf2_sha512_t *) hash_buf->esalt;
13641
13642 char *iter_pos = input_buf + 19;
13643
13644 char *salt_pos = strchr (iter_pos, '.');
13645
13646 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13647
13648 salt_pos++;
13649
13650 char *hash_pos = strchr (salt_pos, '.');
13651
13652 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13653
13654 if (((input_len - (hash_pos - input_buf) - 1) % 128) != 0) return (PARSER_GLOBAL_LENGTH);
13655
13656 hash_pos++;
13657
13658 digest[0] = hex_to_u64 ((const u8 *) &hash_pos[ 0]);
13659 digest[1] = hex_to_u64 ((const u8 *) &hash_pos[ 16]);
13660 digest[2] = hex_to_u64 ((const u8 *) &hash_pos[ 32]);
13661 digest[3] = hex_to_u64 ((const u8 *) &hash_pos[ 48]);
13662 digest[4] = hex_to_u64 ((const u8 *) &hash_pos[ 64]);
13663 digest[5] = hex_to_u64 ((const u8 *) &hash_pos[ 80]);
13664 digest[6] = hex_to_u64 ((const u8 *) &hash_pos[ 96]);
13665 digest[7] = hex_to_u64 ((const u8 *) &hash_pos[112]);
13666
13667 uint salt_len = hash_pos - salt_pos - 1;
13668
13669 salt_len /= 2;
13670
13671 char *salt_buf_ptr = (char *) pbkdf2_sha512->salt_buf;
13672
13673 uint i;
13674
13675 for (i = 0; i < salt_len; i++)
13676 {
13677 salt_buf_ptr[i] = hex_to_u8 ((const u8 *) &salt_pos[i * 2]);
13678 }
13679
13680 salt_buf_ptr[salt_len + 3] = 0x01;
13681 salt_buf_ptr[salt_len + 4] = 0x80;
13682
13683 salt->salt_buf[0] = pbkdf2_sha512->salt_buf[0];
13684
13685 salt->salt_len = salt_len;
13686
13687 salt->salt_iter = atoi (iter_pos) - 1;
13688
13689 return (PARSER_OK);
13690 }
13691
13692 int sha512b64s_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13693 {
13694 if ((input_len < DISPLAY_LEN_MIN_1711) || (input_len > DISPLAY_LEN_MAX_1711)) return (PARSER_GLOBAL_LENGTH);
13695
13696 if (memcmp (SIGNATURE_SHA512B64S, input_buf, 9)) return (PARSER_SIGNATURE_UNMATCHED);
13697
13698 u64 *digest = (u64 *) hash_buf->digest;
13699
13700 salt_t *salt = hash_buf->salt;
13701
13702 u8 tmp_buf[120] = { 0 };
13703
13704 int tmp_len = base64_decode (base64_to_int, (const u8 *) input_buf + 9, input_len - 9, tmp_buf);
13705
13706 if (tmp_len < 64) return (PARSER_HASH_LENGTH);
13707
13708 memcpy (digest, tmp_buf, 64);
13709
13710 digest[0] = byte_swap_64 (digest[0]);
13711 digest[1] = byte_swap_64 (digest[1]);
13712 digest[2] = byte_swap_64 (digest[2]);
13713 digest[3] = byte_swap_64 (digest[3]);
13714 digest[4] = byte_swap_64 (digest[4]);
13715 digest[5] = byte_swap_64 (digest[5]);
13716 digest[6] = byte_swap_64 (digest[6]);
13717 digest[7] = byte_swap_64 (digest[7]);
13718
13719 digest[0] -= SHA512M_A;
13720 digest[1] -= SHA512M_B;
13721 digest[2] -= SHA512M_C;
13722 digest[3] -= SHA512M_D;
13723 digest[4] -= SHA512M_E;
13724 digest[5] -= SHA512M_F;
13725 digest[6] -= SHA512M_G;
13726 digest[7] -= SHA512M_H;
13727
13728 int salt_len = tmp_len - 64;
13729
13730 if (salt_len < 0) return (PARSER_SALT_LENGTH);
13731
13732 salt->salt_len = salt_len;
13733
13734 memcpy (salt->salt_buf, tmp_buf + 64, salt->salt_len);
13735
13736 if (data.opts_type & OPTS_TYPE_ST_ADD80)
13737 {
13738 char *ptr = (char *) salt->salt_buf;
13739
13740 ptr[salt->salt_len] = 0x80;
13741 }
13742
13743 return (PARSER_OK);
13744 }
13745
13746 int hmacmd5_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13747 {
13748 if (data.opts_type & OPTS_TYPE_ST_HEX)
13749 {
13750 if ((input_len < DISPLAY_LEN_MIN_50H) || (input_len > DISPLAY_LEN_MAX_50H)) return (PARSER_GLOBAL_LENGTH);
13751 }
13752 else
13753 {
13754 if ((input_len < DISPLAY_LEN_MIN_50) || (input_len > DISPLAY_LEN_MAX_50)) return (PARSER_GLOBAL_LENGTH);
13755 }
13756
13757 u32 *digest = (u32 *) hash_buf->digest;
13758
13759 salt_t *salt = hash_buf->salt;
13760
13761 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
13762 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
13763 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
13764 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
13765
13766 digest[0] = byte_swap_32 (digest[0]);
13767 digest[1] = byte_swap_32 (digest[1]);
13768 digest[2] = byte_swap_32 (digest[2]);
13769 digest[3] = byte_swap_32 (digest[3]);
13770
13771 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
13772
13773 uint salt_len = input_len - 32 - 1;
13774
13775 char *salt_buf = input_buf + 32 + 1;
13776
13777 char *salt_buf_ptr = (char *) salt->salt_buf;
13778
13779 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
13780
13781 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
13782
13783 salt->salt_len = salt_len;
13784
13785 return (PARSER_OK);
13786 }
13787
13788 int hmacsha1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13789 {
13790 if (data.opts_type & OPTS_TYPE_ST_HEX)
13791 {
13792 if ((input_len < DISPLAY_LEN_MIN_150H) || (input_len > DISPLAY_LEN_MAX_150H)) return (PARSER_GLOBAL_LENGTH);
13793 }
13794 else
13795 {
13796 if ((input_len < DISPLAY_LEN_MIN_150) || (input_len > DISPLAY_LEN_MAX_150)) return (PARSER_GLOBAL_LENGTH);
13797 }
13798
13799 u32 *digest = (u32 *) hash_buf->digest;
13800
13801 salt_t *salt = hash_buf->salt;
13802
13803 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
13804 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
13805 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
13806 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
13807 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
13808
13809 if (input_buf[40] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
13810
13811 uint salt_len = input_len - 40 - 1;
13812
13813 char *salt_buf = input_buf + 40 + 1;
13814
13815 char *salt_buf_ptr = (char *) salt->salt_buf;
13816
13817 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
13818
13819 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
13820
13821 salt->salt_len = salt_len;
13822
13823 return (PARSER_OK);
13824 }
13825
13826 int hmacsha256_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13827 {
13828 if (data.opts_type & OPTS_TYPE_ST_HEX)
13829 {
13830 if ((input_len < DISPLAY_LEN_MIN_1450H) || (input_len > DISPLAY_LEN_MAX_1450H)) return (PARSER_GLOBAL_LENGTH);
13831 }
13832 else
13833 {
13834 if ((input_len < DISPLAY_LEN_MIN_1450) || (input_len > DISPLAY_LEN_MAX_1450)) return (PARSER_GLOBAL_LENGTH);
13835 }
13836
13837 u32 *digest = (u32 *) hash_buf->digest;
13838
13839 salt_t *salt = hash_buf->salt;
13840
13841 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
13842 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
13843 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
13844 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
13845 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
13846 digest[5] = hex_to_u32 ((const u8 *) &input_buf[40]);
13847 digest[6] = hex_to_u32 ((const u8 *) &input_buf[48]);
13848 digest[7] = hex_to_u32 ((const u8 *) &input_buf[56]);
13849
13850 if (input_buf[64] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
13851
13852 uint salt_len = input_len - 64 - 1;
13853
13854 char *salt_buf = input_buf + 64 + 1;
13855
13856 char *salt_buf_ptr = (char *) salt->salt_buf;
13857
13858 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
13859
13860 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
13861
13862 salt->salt_len = salt_len;
13863
13864 return (PARSER_OK);
13865 }
13866
13867 int hmacsha512_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13868 {
13869 if (data.opts_type & OPTS_TYPE_ST_HEX)
13870 {
13871 if ((input_len < DISPLAY_LEN_MIN_1750H) || (input_len > DISPLAY_LEN_MAX_1750H)) return (PARSER_GLOBAL_LENGTH);
13872 }
13873 else
13874 {
13875 if ((input_len < DISPLAY_LEN_MIN_1750) || (input_len > DISPLAY_LEN_MAX_1750)) return (PARSER_GLOBAL_LENGTH);
13876 }
13877
13878 u64 *digest = (u64 *) hash_buf->digest;
13879
13880 salt_t *salt = hash_buf->salt;
13881
13882 digest[0] = hex_to_u64 ((const u8 *) &input_buf[ 0]);
13883 digest[1] = hex_to_u64 ((const u8 *) &input_buf[ 16]);
13884 digest[2] = hex_to_u64 ((const u8 *) &input_buf[ 32]);
13885 digest[3] = hex_to_u64 ((const u8 *) &input_buf[ 48]);
13886 digest[4] = hex_to_u64 ((const u8 *) &input_buf[ 64]);
13887 digest[5] = hex_to_u64 ((const u8 *) &input_buf[ 80]);
13888 digest[6] = hex_to_u64 ((const u8 *) &input_buf[ 96]);
13889 digest[7] = hex_to_u64 ((const u8 *) &input_buf[112]);
13890
13891 if (input_buf[128] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
13892
13893 uint salt_len = input_len - 128 - 1;
13894
13895 char *salt_buf = input_buf + 128 + 1;
13896
13897 char *salt_buf_ptr = (char *) salt->salt_buf;
13898
13899 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
13900
13901 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
13902
13903 salt->salt_len = salt_len;
13904
13905 return (PARSER_OK);
13906 }
13907
13908 int krb5pa_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13909 {
13910 if ((input_len < DISPLAY_LEN_MIN_7500) || (input_len > DISPLAY_LEN_MAX_7500)) return (PARSER_GLOBAL_LENGTH);
13911
13912 if (memcmp (SIGNATURE_KRB5PA, input_buf, 10)) return (PARSER_SIGNATURE_UNMATCHED);
13913
13914 u32 *digest = (u32 *) hash_buf->digest;
13915
13916 salt_t *salt = hash_buf->salt;
13917
13918 krb5pa_t *krb5pa = (krb5pa_t *) hash_buf->esalt;
13919
13920 /**
13921 * parse line
13922 */
13923
13924 char *user_pos = input_buf + 10 + 1;
13925
13926 char *realm_pos = strchr (user_pos, '$');
13927
13928 if (realm_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13929
13930 uint user_len = realm_pos - user_pos;
13931
13932 if (user_len >= 64) return (PARSER_SALT_LENGTH);
13933
13934 realm_pos++;
13935
13936 char *salt_pos = strchr (realm_pos, '$');
13937
13938 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13939
13940 uint realm_len = salt_pos - realm_pos;
13941
13942 if (realm_len >= 64) return (PARSER_SALT_LENGTH);
13943
13944 salt_pos++;
13945
13946 char *data_pos = strchr (salt_pos, '$');
13947
13948 if (data_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13949
13950 uint salt_len = data_pos - salt_pos;
13951
13952 if (salt_len >= 128) return (PARSER_SALT_LENGTH);
13953
13954 data_pos++;
13955
13956 uint data_len = input_len - 10 - 1 - user_len - 1 - realm_len - 1 - salt_len - 1;
13957
13958 if (data_len != ((36 + 16) * 2)) return (PARSER_SALT_LENGTH);
13959
13960 /**
13961 * copy data
13962 */
13963
13964 memcpy (krb5pa->user, user_pos, user_len);
13965 memcpy (krb5pa->realm, realm_pos, realm_len);
13966 memcpy (krb5pa->salt, salt_pos, salt_len);
13967
13968 char *timestamp_ptr = (char *) krb5pa->timestamp;
13969
13970 for (uint i = 0; i < (36 * 2); i += 2)
13971 {
13972 const char p0 = data_pos[i + 0];
13973 const char p1 = data_pos[i + 1];
13974
13975 *timestamp_ptr++ = hex_convert (p1) << 0
13976 | hex_convert (p0) << 4;
13977 }
13978
13979 char *checksum_ptr = (char *) krb5pa->checksum;
13980
13981 for (uint i = (36 * 2); i < ((36 + 16) * 2); i += 2)
13982 {
13983 const char p0 = data_pos[i + 0];
13984 const char p1 = data_pos[i + 1];
13985
13986 *checksum_ptr++ = hex_convert (p1) << 0
13987 | hex_convert (p0) << 4;
13988 }
13989
13990 /**
13991 * copy some data to generic buffers to make sorting happy
13992 */
13993
13994 salt->salt_buf[0] = krb5pa->timestamp[0];
13995 salt->salt_buf[1] = krb5pa->timestamp[1];
13996 salt->salt_buf[2] = krb5pa->timestamp[2];
13997 salt->salt_buf[3] = krb5pa->timestamp[3];
13998 salt->salt_buf[4] = krb5pa->timestamp[4];
13999 salt->salt_buf[5] = krb5pa->timestamp[5];
14000 salt->salt_buf[6] = krb5pa->timestamp[6];
14001 salt->salt_buf[7] = krb5pa->timestamp[7];
14002 salt->salt_buf[8] = krb5pa->timestamp[8];
14003
14004 salt->salt_len = 36;
14005
14006 digest[0] = krb5pa->checksum[0];
14007 digest[1] = krb5pa->checksum[1];
14008 digest[2] = krb5pa->checksum[2];
14009 digest[3] = krb5pa->checksum[3];
14010
14011 return (PARSER_OK);
14012 }
14013
14014 int sapb_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14015 {
14016 if ((input_len < DISPLAY_LEN_MIN_7700) || (input_len > DISPLAY_LEN_MAX_7700)) return (PARSER_GLOBAL_LENGTH);
14017
14018 u32 *digest = (u32 *) hash_buf->digest;
14019
14020 salt_t *salt = hash_buf->salt;
14021
14022 /**
14023 * parse line
14024 */
14025
14026 char *salt_pos = input_buf;
14027
14028 char *hash_pos = strchr (salt_pos, '$');
14029
14030 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14031
14032 uint salt_len = hash_pos - salt_pos;
14033
14034 if (salt_len >= 40) return (PARSER_SALT_LENGTH);
14035
14036 hash_pos++;
14037
14038 uint hash_len = input_len - 1 - salt_len;
14039
14040 if (hash_len != 16) return (PARSER_HASH_LENGTH);
14041
14042 /**
14043 * valid some data
14044 */
14045
14046 uint user_len = 0;
14047
14048 for (uint i = 0; i < salt_len; i++)
14049 {
14050 if (salt_pos[i] == ' ') continue;
14051
14052 user_len++;
14053 }
14054
14055 // SAP user names cannot be longer than 12 characters
14056 if (user_len > 12) return (PARSER_SALT_LENGTH);
14057
14058 // SAP user name cannot start with ! or ?
14059 if (salt_pos[0] == '!' || salt_pos[0] == '?') return (PARSER_SALT_VALUE);
14060
14061 /**
14062 * copy data
14063 */
14064
14065 char *salt_buf_ptr = (char *) salt->salt_buf;
14066
14067 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
14068
14069 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
14070
14071 salt->salt_len = salt_len;
14072
14073 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[0]);
14074 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[8]);
14075 digest[2] = 0;
14076 digest[3] = 0;
14077
14078 digest[0] = byte_swap_32 (digest[0]);
14079 digest[1] = byte_swap_32 (digest[1]);
14080
14081 return (PARSER_OK);
14082 }
14083
14084 int sapg_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14085 {
14086 if ((input_len < DISPLAY_LEN_MIN_7800) || (input_len > DISPLAY_LEN_MAX_7800)) return (PARSER_GLOBAL_LENGTH);
14087
14088 u32 *digest = (u32 *) hash_buf->digest;
14089
14090 salt_t *salt = hash_buf->salt;
14091
14092 /**
14093 * parse line
14094 */
14095
14096 char *salt_pos = input_buf;
14097
14098 char *hash_pos = strchr (salt_pos, '$');
14099
14100 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14101
14102 uint salt_len = hash_pos - salt_pos;
14103
14104 if (salt_len >= 40) return (PARSER_SALT_LENGTH);
14105
14106 hash_pos++;
14107
14108 uint hash_len = input_len - 1 - salt_len;
14109
14110 if (hash_len != 40) return (PARSER_HASH_LENGTH);
14111
14112 /**
14113 * valid some data
14114 */
14115
14116 uint user_len = 0;
14117
14118 for (uint i = 0; i < salt_len; i++)
14119 {
14120 if (salt_pos[i] == ' ') continue;
14121
14122 user_len++;
14123 }
14124
14125 // SAP user names cannot be longer than 12 characters
14126 // this is kinda buggy. if the username is in utf the length can be up to length 12*3
14127 // so far nobody complained so we stay with this because it helps in optimization
14128 // final string can have a max size of 32 (password) + (10 * 5) = lengthMagicArray + 12 (max salt) + 1 (the 0x80)
14129
14130 if (user_len > 12) return (PARSER_SALT_LENGTH);
14131
14132 // SAP user name cannot start with ! or ?
14133 if (salt_pos[0] == '!' || salt_pos[0] == '?') return (PARSER_SALT_VALUE);
14134
14135 /**
14136 * copy data
14137 */
14138
14139 char *salt_buf_ptr = (char *) salt->salt_buf;
14140
14141 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
14142
14143 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
14144
14145 salt->salt_len = salt_len;
14146
14147 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
14148 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
14149 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
14150 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
14151 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
14152
14153 return (PARSER_OK);
14154 }
14155
14156 int drupal7_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14157 {
14158 if ((input_len < DISPLAY_LEN_MIN_7900) || (input_len > DISPLAY_LEN_MAX_7900)) return (PARSER_GLOBAL_LENGTH);
14159
14160 if (memcmp (SIGNATURE_DRUPAL7, input_buf, 3)) return (PARSER_SIGNATURE_UNMATCHED);
14161
14162 u64 *digest = (u64 *) hash_buf->digest;
14163
14164 salt_t *salt = hash_buf->salt;
14165
14166 char *iter_pos = input_buf + 3;
14167
14168 uint salt_iter = 1 << itoa64_to_int (iter_pos[0]);
14169
14170 if (salt_iter > 0x80000000) return (PARSER_SALT_ITERATION);
14171
14172 memcpy ((char *) salt->salt_sign, input_buf, 4);
14173
14174 salt->salt_iter = salt_iter;
14175
14176 char *salt_pos = iter_pos + 1;
14177
14178 uint salt_len = 8;
14179
14180 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
14181
14182 salt->salt_len = salt_len;
14183
14184 char *hash_pos = salt_pos + salt_len;
14185
14186 drupal7_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
14187
14188 // ugly hack start
14189
14190 char *tmp = (char *) salt->salt_buf_pc;
14191
14192 tmp[0] = hash_pos[42];
14193
14194 // ugly hack end
14195
14196 digest[ 0] = byte_swap_64 (digest[ 0]);
14197 digest[ 1] = byte_swap_64 (digest[ 1]);
14198 digest[ 2] = byte_swap_64 (digest[ 2]);
14199 digest[ 3] = byte_swap_64 (digest[ 3]);
14200 digest[ 4] = 0;
14201 digest[ 5] = 0;
14202 digest[ 6] = 0;
14203 digest[ 7] = 0;
14204
14205 return (PARSER_OK);
14206 }
14207
14208 int sybasease_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14209 {
14210 if ((input_len < DISPLAY_LEN_MIN_8000) || (input_len > DISPLAY_LEN_MAX_8000)) return (PARSER_GLOBAL_LENGTH);
14211
14212 if (memcmp (SIGNATURE_SYBASEASE, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
14213
14214 u32 *digest = (u32 *) hash_buf->digest;
14215
14216 salt_t *salt = hash_buf->salt;
14217
14218 char *salt_buf = input_buf + 6;
14219
14220 uint salt_len = 16;
14221
14222 char *salt_buf_ptr = (char *) salt->salt_buf;
14223
14224 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
14225
14226 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
14227
14228 salt->salt_len = salt_len;
14229
14230 char *hash_pos = input_buf + 6 + 16;
14231
14232 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
14233 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
14234 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
14235 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
14236 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
14237 digest[5] = hex_to_u32 ((const u8 *) &hash_pos[40]);
14238 digest[6] = hex_to_u32 ((const u8 *) &hash_pos[48]);
14239 digest[7] = hex_to_u32 ((const u8 *) &hash_pos[56]);
14240
14241 return (PARSER_OK);
14242 }
14243
14244 int mysql323_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14245 {
14246 if ((input_len < DISPLAY_LEN_MIN_200) || (input_len > DISPLAY_LEN_MAX_200)) return (PARSER_GLOBAL_LENGTH);
14247
14248 u32 *digest = (u32 *) hash_buf->digest;
14249
14250 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
14251 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
14252 digest[2] = 0;
14253 digest[3] = 0;
14254
14255 return (PARSER_OK);
14256 }
14257
14258 int rakp_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14259 {
14260 if ((input_len < DISPLAY_LEN_MIN_7300) || (input_len > DISPLAY_LEN_MAX_7300)) return (PARSER_GLOBAL_LENGTH);
14261
14262 u32 *digest = (u32 *) hash_buf->digest;
14263
14264 salt_t *salt = hash_buf->salt;
14265
14266 rakp_t *rakp = (rakp_t *) hash_buf->esalt;
14267
14268 char *saltbuf_pos = input_buf;
14269
14270 char *hashbuf_pos = strchr (saltbuf_pos, ':');
14271
14272 if (hashbuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14273
14274 uint saltbuf_len = hashbuf_pos - saltbuf_pos;
14275
14276 if (saltbuf_len < 64) return (PARSER_SALT_LENGTH);
14277 if (saltbuf_len > 512) return (PARSER_SALT_LENGTH);
14278
14279 if (saltbuf_len & 1) return (PARSER_SALT_LENGTH); // muss gerade sein wegen hex
14280
14281 hashbuf_pos++;
14282
14283 uint hashbuf_len = input_len - saltbuf_len - 1;
14284
14285 if (hashbuf_len != 40) return (PARSER_HASH_LENGTH);
14286
14287 char *salt_ptr = (char *) saltbuf_pos;
14288 char *rakp_ptr = (char *) rakp->salt_buf;
14289
14290 uint i;
14291 uint j;
14292
14293 for (i = 0, j = 0; i < saltbuf_len; i += 2, j += 1)
14294 {
14295 rakp_ptr[j] = hex_to_u8 ((const u8 *) &salt_ptr[i]);
14296 }
14297
14298 rakp_ptr[j] = 0x80;
14299
14300 rakp->salt_len = j;
14301
14302 for (i = 0; i < 64; i++)
14303 {
14304 rakp->salt_buf[i] = byte_swap_32 (rakp->salt_buf[i]);
14305 }
14306
14307 salt->salt_buf[0] = rakp->salt_buf[0];
14308 salt->salt_buf[1] = rakp->salt_buf[1];
14309 salt->salt_buf[2] = rakp->salt_buf[2];
14310 salt->salt_buf[3] = rakp->salt_buf[3];
14311 salt->salt_buf[4] = rakp->salt_buf[4];
14312 salt->salt_buf[5] = rakp->salt_buf[5];
14313 salt->salt_buf[6] = rakp->salt_buf[6];
14314 salt->salt_buf[7] = rakp->salt_buf[7];
14315
14316 salt->salt_len = 32; // muss min. 32 haben
14317
14318 digest[0] = hex_to_u32 ((const u8 *) &hashbuf_pos[ 0]);
14319 digest[1] = hex_to_u32 ((const u8 *) &hashbuf_pos[ 8]);
14320 digest[2] = hex_to_u32 ((const u8 *) &hashbuf_pos[16]);
14321 digest[3] = hex_to_u32 ((const u8 *) &hashbuf_pos[24]);
14322 digest[4] = hex_to_u32 ((const u8 *) &hashbuf_pos[32]);
14323
14324 return (PARSER_OK);
14325 }
14326
14327 int netscaler_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14328 {
14329 if ((input_len < DISPLAY_LEN_MIN_8100) || (input_len > DISPLAY_LEN_MAX_8100)) return (PARSER_GLOBAL_LENGTH);
14330
14331 u32 *digest = (u32 *) hash_buf->digest;
14332
14333 salt_t *salt = hash_buf->salt;
14334
14335 if (memcmp (SIGNATURE_NETSCALER, input_buf, 1)) return (PARSER_SIGNATURE_UNMATCHED);
14336
14337 char *salt_pos = input_buf + 1;
14338
14339 memcpy (salt->salt_buf, salt_pos, 8);
14340
14341 salt->salt_buf[0] = byte_swap_32 (salt->salt_buf[0]);
14342 salt->salt_buf[1] = byte_swap_32 (salt->salt_buf[1]);
14343
14344 salt->salt_len = 8;
14345
14346 char *hash_pos = salt_pos + 8;
14347
14348 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
14349 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
14350 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
14351 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
14352 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
14353
14354 digest[0] -= SHA1M_A;
14355 digest[1] -= SHA1M_B;
14356 digest[2] -= SHA1M_C;
14357 digest[3] -= SHA1M_D;
14358 digest[4] -= SHA1M_E;
14359
14360 return (PARSER_OK);
14361 }
14362
14363 int chap_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14364 {
14365 if ((input_len < DISPLAY_LEN_MIN_4800) || (input_len > DISPLAY_LEN_MAX_4800)) return (PARSER_GLOBAL_LENGTH);
14366
14367 u32 *digest = (u32 *) hash_buf->digest;
14368
14369 salt_t *salt = hash_buf->salt;
14370
14371 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
14372 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
14373 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
14374 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
14375
14376 digest[0] = byte_swap_32 (digest[0]);
14377 digest[1] = byte_swap_32 (digest[1]);
14378 digest[2] = byte_swap_32 (digest[2]);
14379 digest[3] = byte_swap_32 (digest[3]);
14380
14381 digest[0] -= MD5M_A;
14382 digest[1] -= MD5M_B;
14383 digest[2] -= MD5M_C;
14384 digest[3] -= MD5M_D;
14385
14386 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
14387
14388 char *salt_buf_ptr = input_buf + 32 + 1;
14389
14390 u32 *salt_buf = salt->salt_buf;
14391
14392 salt_buf[0] = hex_to_u32 ((const u8 *) &salt_buf_ptr[ 0]);
14393 salt_buf[1] = hex_to_u32 ((const u8 *) &salt_buf_ptr[ 8]);
14394 salt_buf[2] = hex_to_u32 ((const u8 *) &salt_buf_ptr[16]);
14395 salt_buf[3] = hex_to_u32 ((const u8 *) &salt_buf_ptr[24]);
14396
14397 salt_buf[0] = byte_swap_32 (salt_buf[0]);
14398 salt_buf[1] = byte_swap_32 (salt_buf[1]);
14399 salt_buf[2] = byte_swap_32 (salt_buf[2]);
14400 salt_buf[3] = byte_swap_32 (salt_buf[3]);
14401
14402 salt->salt_len = 16 + 1;
14403
14404 if (input_buf[65] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
14405
14406 char *idbyte_buf_ptr = input_buf + 32 + 1 + 32 + 1;
14407
14408 salt_buf[4] = hex_to_u8 ((const u8 *) &idbyte_buf_ptr[0]) & 0xff;
14409
14410 return (PARSER_OK);
14411 }
14412
14413 int cloudkey_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14414 {
14415 if ((input_len < DISPLAY_LEN_MIN_8200) || (input_len > DISPLAY_LEN_MAX_8200)) return (PARSER_GLOBAL_LENGTH);
14416
14417 u32 *digest = (u32 *) hash_buf->digest;
14418
14419 salt_t *salt = hash_buf->salt;
14420
14421 cloudkey_t *cloudkey = (cloudkey_t *) hash_buf->esalt;
14422
14423 /**
14424 * parse line
14425 */
14426
14427 char *hashbuf_pos = input_buf;
14428
14429 char *saltbuf_pos = strchr (hashbuf_pos, ':');
14430
14431 if (saltbuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14432
14433 const uint hashbuf_len = saltbuf_pos - hashbuf_pos;
14434
14435 if (hashbuf_len != 64) return (PARSER_HASH_LENGTH);
14436
14437 saltbuf_pos++;
14438
14439 char *iteration_pos = strchr (saltbuf_pos, ':');
14440
14441 if (iteration_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14442
14443 const uint saltbuf_len = iteration_pos - saltbuf_pos;
14444
14445 if (saltbuf_len != 32) return (PARSER_SALT_LENGTH);
14446
14447 iteration_pos++;
14448
14449 char *databuf_pos = strchr (iteration_pos, ':');
14450
14451 if (databuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14452
14453 const uint iteration_len = databuf_pos - iteration_pos;
14454
14455 if (iteration_len < 1) return (PARSER_SALT_ITERATION);
14456 if (iteration_len > 8) return (PARSER_SALT_ITERATION);
14457
14458 const uint databuf_len = input_len - hashbuf_len - 1 - saltbuf_len - 1 - iteration_len - 1;
14459
14460 if (databuf_len < 1) return (PARSER_SALT_LENGTH);
14461 if (databuf_len > 2048) return (PARSER_SALT_LENGTH);
14462
14463 databuf_pos++;
14464
14465 // digest
14466
14467 digest[0] = hex_to_u32 ((const u8 *) &hashbuf_pos[ 0]);
14468 digest[1] = hex_to_u32 ((const u8 *) &hashbuf_pos[ 8]);
14469 digest[2] = hex_to_u32 ((const u8 *) &hashbuf_pos[16]);
14470 digest[3] = hex_to_u32 ((const u8 *) &hashbuf_pos[24]);
14471 digest[4] = hex_to_u32 ((const u8 *) &hashbuf_pos[32]);
14472 digest[5] = hex_to_u32 ((const u8 *) &hashbuf_pos[40]);
14473 digest[6] = hex_to_u32 ((const u8 *) &hashbuf_pos[48]);
14474 digest[7] = hex_to_u32 ((const u8 *) &hashbuf_pos[56]);
14475
14476 // salt
14477
14478 char *saltbuf_ptr = (char *) salt->salt_buf;
14479
14480 for (uint i = 0; i < saltbuf_len; i += 2)
14481 {
14482 const char p0 = saltbuf_pos[i + 0];
14483 const char p1 = saltbuf_pos[i + 1];
14484
14485 *saltbuf_ptr++ = hex_convert (p1) << 0
14486 | hex_convert (p0) << 4;
14487 }
14488
14489 salt->salt_buf[4] = 0x01000000;
14490 salt->salt_buf[5] = 0x80;
14491
14492 salt->salt_len = saltbuf_len / 2;
14493
14494 // iteration
14495
14496 salt->salt_iter = atoi (iteration_pos) - 1;
14497
14498 // data
14499
14500 char *databuf_ptr = (char *) cloudkey->data_buf;
14501
14502 for (uint i = 0; i < databuf_len; i += 2)
14503 {
14504 const char p0 = databuf_pos[i + 0];
14505 const char p1 = databuf_pos[i + 1];
14506
14507 *databuf_ptr++ = hex_convert (p1) << 0
14508 | hex_convert (p0) << 4;
14509 }
14510
14511 *databuf_ptr++ = 0x80;
14512
14513 for (uint i = 0; i < 512; i++)
14514 {
14515 cloudkey->data_buf[i] = byte_swap_32 (cloudkey->data_buf[i]);
14516 }
14517
14518 cloudkey->data_len = databuf_len / 2;
14519
14520 return (PARSER_OK);
14521 }
14522
14523 int nsec3_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14524 {
14525 if ((input_len < DISPLAY_LEN_MIN_8300) || (input_len > DISPLAY_LEN_MAX_8300)) return (PARSER_GLOBAL_LENGTH);
14526
14527 u32 *digest = (u32 *) hash_buf->digest;
14528
14529 salt_t *salt = hash_buf->salt;
14530
14531 /**
14532 * parse line
14533 */
14534
14535 char *hashbuf_pos = input_buf;
14536
14537 char *domainbuf_pos = strchr (hashbuf_pos, ':');
14538
14539 if (domainbuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14540
14541 const uint hashbuf_len = domainbuf_pos - hashbuf_pos;
14542
14543 if (hashbuf_len != 32) return (PARSER_HASH_LENGTH);
14544
14545 domainbuf_pos++;
14546
14547 if (domainbuf_pos[0] != '.') return (PARSER_SALT_VALUE);
14548
14549 char *saltbuf_pos = strchr (domainbuf_pos, ':');
14550
14551 if (saltbuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14552
14553 const uint domainbuf_len = saltbuf_pos - domainbuf_pos;
14554
14555 if (domainbuf_len >= 32) return (PARSER_SALT_LENGTH);
14556
14557 saltbuf_pos++;
14558
14559 char *iteration_pos = strchr (saltbuf_pos, ':');
14560
14561 if (iteration_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14562
14563 const uint saltbuf_len = iteration_pos - saltbuf_pos;
14564
14565 if (saltbuf_len >= 28) return (PARSER_SALT_LENGTH); // 28 = 32 - 4; 4 = length
14566
14567 if ((domainbuf_len + saltbuf_len) >= 48) return (PARSER_SALT_LENGTH);
14568
14569 iteration_pos++;
14570
14571 const uint iteration_len = input_len - hashbuf_len - 1 - domainbuf_len - 1 - saltbuf_len - 1;
14572
14573 if (iteration_len < 1) return (PARSER_SALT_ITERATION);
14574 if (iteration_len > 5) return (PARSER_SALT_ITERATION);
14575
14576 // ok, the plan for this algorithm is the following:
14577 // we have 2 salts here, the domain-name and a random salt
14578 // while both are used in the initial transformation,
14579 // only the random salt is used in the following iterations
14580 // so we create two buffer, one that includes domain-name (stored into salt_buf_pc[])
14581 // and one that includes only the real salt (stored into salt_buf[]).
14582 // the domain-name length is put into array position 7 of salt_buf_pc[] since there is not salt_pc_len
14583
14584 u8 tmp_buf[100] = { 0 };
14585
14586 base32_decode (itoa32_to_int, (const u8 *) hashbuf_pos, 32, tmp_buf);
14587
14588 memcpy (digest, tmp_buf, 20);
14589
14590 digest[0] = byte_swap_32 (digest[0]);
14591 digest[1] = byte_swap_32 (digest[1]);
14592 digest[2] = byte_swap_32 (digest[2]);
14593 digest[3] = byte_swap_32 (digest[3]);
14594 digest[4] = byte_swap_32 (digest[4]);
14595
14596 // domain
14597
14598 char *salt_buf_pc_ptr = (char *) salt->salt_buf_pc;
14599
14600 memcpy (salt_buf_pc_ptr, domainbuf_pos, domainbuf_len);
14601
14602 char *len_ptr = NULL;
14603
14604 for (uint i = 0; i < domainbuf_len; i++)
14605 {
14606 if (salt_buf_pc_ptr[i] == '.')
14607 {
14608 len_ptr = &salt_buf_pc_ptr[i];
14609
14610 *len_ptr = 0;
14611 }
14612 else
14613 {
14614 *len_ptr += 1;
14615 }
14616 }
14617
14618 salt->salt_buf_pc[7] = domainbuf_len;
14619
14620 // "real" salt
14621
14622 char *salt_buf_ptr = (char *) salt->salt_buf;
14623
14624 const uint salt_len = parse_and_store_salt (salt_buf_ptr, saltbuf_pos, saltbuf_len);
14625
14626 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
14627
14628 salt->salt_len = salt_len;
14629
14630 // iteration
14631
14632 salt->salt_iter = atoi (iteration_pos);
14633
14634 return (PARSER_OK);
14635 }
14636
14637 int wbb3_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14638 {
14639 if ((input_len < DISPLAY_LEN_MIN_8400) || (input_len > DISPLAY_LEN_MAX_8400)) return (PARSER_GLOBAL_LENGTH);
14640
14641 u32 *digest = (u32 *) hash_buf->digest;
14642
14643 salt_t *salt = hash_buf->salt;
14644
14645 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
14646 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
14647 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
14648 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
14649 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
14650
14651 if (input_buf[40] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
14652
14653 uint salt_len = input_len - 40 - 1;
14654
14655 char *salt_buf = input_buf + 40 + 1;
14656
14657 char *salt_buf_ptr = (char *) salt->salt_buf;
14658
14659 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
14660
14661 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
14662
14663 salt->salt_len = salt_len;
14664
14665 return (PARSER_OK);
14666 }
14667
14668 int racf_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14669 {
14670 const u8 ascii_to_ebcdic[] =
14671 {
14672 0x00, 0x01, 0x02, 0x03, 0x37, 0x2d, 0x2e, 0x2f, 0x16, 0x05, 0x25, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
14673 0x10, 0x11, 0x12, 0x13, 0x3c, 0x3d, 0x32, 0x26, 0x18, 0x19, 0x3f, 0x27, 0x1c, 0x1d, 0x1e, 0x1f,
14674 0x40, 0x4f, 0x7f, 0x7b, 0x5b, 0x6c, 0x50, 0x7d, 0x4d, 0x5d, 0x5c, 0x4e, 0x6b, 0x60, 0x4b, 0x61,
14675 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0x7a, 0x5e, 0x4c, 0x7e, 0x6e, 0x6f,
14676 0x7c, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
14677 0xd7, 0xd8, 0xd9, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0x4a, 0xe0, 0x5a, 0x5f, 0x6d,
14678 0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
14679 0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xc0, 0x6a, 0xd0, 0xa1, 0x07,
14680 0x20, 0x21, 0x22, 0x23, 0x24, 0x15, 0x06, 0x17, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x09, 0x0a, 0x1b,
14681 0x30, 0x31, 0x1a, 0x33, 0x34, 0x35, 0x36, 0x08, 0x38, 0x39, 0x3a, 0x3b, 0x04, 0x14, 0x3e, 0xe1,
14682 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
14683 0x58, 0x59, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75,
14684 0x76, 0x77, 0x78, 0x80, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e,
14685 0x9f, 0xa0, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
14686 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xda, 0xdb,
14687 0xdc, 0xdd, 0xde, 0xdf, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
14688 };
14689
14690 if ((input_len < DISPLAY_LEN_MIN_8500) || (input_len > DISPLAY_LEN_MAX_8500)) return (PARSER_GLOBAL_LENGTH);
14691
14692 if (memcmp (SIGNATURE_RACF, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
14693
14694 u32 *digest = (u32 *) hash_buf->digest;
14695
14696 salt_t *salt = hash_buf->salt;
14697
14698 char *salt_pos = input_buf + 6 + 1;
14699
14700 char *digest_pos = strchr (salt_pos, '*');
14701
14702 if (digest_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14703
14704 uint salt_len = digest_pos - salt_pos;
14705
14706 if (salt_len > 8) return (PARSER_SALT_LENGTH);
14707
14708 uint hash_len = input_len - 1 - salt_len - 1 - 6;
14709
14710 if (hash_len != 16) return (PARSER_HASH_LENGTH);
14711
14712 digest_pos++;
14713
14714 char *salt_buf_ptr = (char *) salt->salt_buf;
14715 char *salt_buf_pc_ptr = (char *) salt->salt_buf_pc;
14716
14717 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
14718
14719 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
14720
14721 salt->salt_len = salt_len;
14722
14723 for (uint i = 0; i < salt_len; i++)
14724 {
14725 salt_buf_pc_ptr[i] = ascii_to_ebcdic[(int) salt_buf_ptr[i]];
14726 }
14727 for (uint i = salt_len; i < 8; i++)
14728 {
14729 salt_buf_pc_ptr[i] = 0x40;
14730 }
14731
14732 uint tt;
14733
14734 IP (salt->salt_buf_pc[0], salt->salt_buf_pc[1], tt);
14735
14736 salt->salt_buf_pc[0] = rotl32 (salt->salt_buf_pc[0], 3u);
14737 salt->salt_buf_pc[1] = rotl32 (salt->salt_buf_pc[1], 3u);
14738
14739 digest[0] = hex_to_u32 ((const u8 *) &digest_pos[ 0]);
14740 digest[1] = hex_to_u32 ((const u8 *) &digest_pos[ 8]);
14741
14742 digest[0] = byte_swap_32 (digest[0]);
14743 digest[1] = byte_swap_32 (digest[1]);
14744
14745 IP (digest[0], digest[1], tt);
14746
14747 digest[0] = rotr32 (digest[0], 29);
14748 digest[1] = rotr32 (digest[1], 29);
14749 digest[2] = 0;
14750 digest[3] = 0;
14751
14752 return (PARSER_OK);
14753 }
14754
14755 int lotus5_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14756 {
14757 if ((input_len < DISPLAY_LEN_MIN_8600) || (input_len > DISPLAY_LEN_MAX_8600)) return (PARSER_GLOBAL_LENGTH);
14758
14759 u32 *digest = (u32 *) hash_buf->digest;
14760
14761 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
14762 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
14763 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
14764 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
14765
14766 digest[0] = byte_swap_32 (digest[0]);
14767 digest[1] = byte_swap_32 (digest[1]);
14768 digest[2] = byte_swap_32 (digest[2]);
14769 digest[3] = byte_swap_32 (digest[3]);
14770
14771 return (PARSER_OK);
14772 }
14773
14774 int lotus6_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14775 {
14776 if ((input_len < DISPLAY_LEN_MIN_8700) || (input_len > DISPLAY_LEN_MAX_8700)) return (PARSER_GLOBAL_LENGTH);
14777
14778 if ((input_buf[0] != '(') || (input_buf[1] != 'G') || (input_buf[21] != ')')) return (PARSER_SIGNATURE_UNMATCHED);
14779
14780 u32 *digest = (u32 *) hash_buf->digest;
14781
14782 salt_t *salt = hash_buf->salt;
14783
14784 u8 tmp_buf[120] = { 0 };
14785
14786 base64_decode (lotus64_to_int, (const u8 *) input_buf + 2, input_len - 3, tmp_buf);
14787
14788 tmp_buf[3] += -4; // dont ask!
14789
14790 memcpy (salt->salt_buf, tmp_buf, 5);
14791
14792 salt->salt_len = 5;
14793
14794 memcpy (digest, tmp_buf + 5, 9);
14795
14796 // yes, only 9 byte are needed to crack, but 10 to display
14797
14798 salt->salt_buf_pc[7] = input_buf[20];
14799
14800 return (PARSER_OK);
14801 }
14802
14803 int lotus8_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14804 {
14805 if ((input_len < DISPLAY_LEN_MIN_9100) || (input_len > DISPLAY_LEN_MAX_9100)) return (PARSER_GLOBAL_LENGTH);
14806
14807 if ((input_buf[0] != '(') || (input_buf[1] != 'H') || (input_buf[DISPLAY_LEN_MAX_9100 - 1] != ')')) return (PARSER_SIGNATURE_UNMATCHED);
14808
14809 u32 *digest = (u32 *) hash_buf->digest;
14810
14811 salt_t *salt = hash_buf->salt;
14812
14813 u8 tmp_buf[120] = { 0 };
14814
14815 base64_decode (lotus64_to_int, (const u8 *) input_buf + 2, input_len - 3, tmp_buf);
14816
14817 tmp_buf[3] += -4; // dont ask!
14818
14819 // salt
14820
14821 memcpy (salt->salt_buf, tmp_buf, 16);
14822
14823 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)
14824
14825 // iteration
14826
14827 char tmp_iter_buf[11] = { 0 };
14828
14829 memcpy (tmp_iter_buf, tmp_buf + 16, 10);
14830
14831 tmp_iter_buf[10] = 0;
14832
14833 salt->salt_iter = atoi (tmp_iter_buf);
14834
14835 if (salt->salt_iter < 1) // well, the limit hopefully is much higher
14836 {
14837 return (PARSER_SALT_ITERATION);
14838 }
14839
14840 salt->salt_iter--; // first round in init
14841
14842 // 2 additional bytes for display only
14843
14844 salt->salt_buf_pc[0] = tmp_buf[26];
14845 salt->salt_buf_pc[1] = tmp_buf[27];
14846
14847 // digest
14848
14849 memcpy (digest, tmp_buf + 28, 8);
14850
14851 digest[0] = byte_swap_32 (digest[0]);
14852 digest[1] = byte_swap_32 (digest[1]);
14853 digest[2] = 0;
14854 digest[3] = 0;
14855
14856 return (PARSER_OK);
14857 }
14858
14859 int hmailserver_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14860 {
14861 if ((input_len < DISPLAY_LEN_MIN_1421) || (input_len > DISPLAY_LEN_MAX_1421)) return (PARSER_GLOBAL_LENGTH);
14862
14863 u32 *digest = (u32 *) hash_buf->digest;
14864
14865 salt_t *salt = hash_buf->salt;
14866
14867 char *salt_buf_pos = input_buf;
14868
14869 char *hash_buf_pos = salt_buf_pos + 6;
14870
14871 digest[0] = hex_to_u32 ((const u8 *) &hash_buf_pos[ 0]);
14872 digest[1] = hex_to_u32 ((const u8 *) &hash_buf_pos[ 8]);
14873 digest[2] = hex_to_u32 ((const u8 *) &hash_buf_pos[16]);
14874 digest[3] = hex_to_u32 ((const u8 *) &hash_buf_pos[24]);
14875 digest[4] = hex_to_u32 ((const u8 *) &hash_buf_pos[32]);
14876 digest[5] = hex_to_u32 ((const u8 *) &hash_buf_pos[40]);
14877 digest[6] = hex_to_u32 ((const u8 *) &hash_buf_pos[48]);
14878 digest[7] = hex_to_u32 ((const u8 *) &hash_buf_pos[56]);
14879
14880 digest[0] -= SHA256M_A;
14881 digest[1] -= SHA256M_B;
14882 digest[2] -= SHA256M_C;
14883 digest[3] -= SHA256M_D;
14884 digest[4] -= SHA256M_E;
14885 digest[5] -= SHA256M_F;
14886 digest[6] -= SHA256M_G;
14887 digest[7] -= SHA256M_H;
14888
14889 char *salt_buf_ptr = (char *) salt->salt_buf;
14890
14891 const uint salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf_pos, 6);
14892
14893 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
14894
14895 salt->salt_len = salt_len;
14896
14897 return (PARSER_OK);
14898 }
14899
14900 int phps_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14901 {
14902 if ((input_len < DISPLAY_LEN_MIN_2612) || (input_len > DISPLAY_LEN_MAX_2612)) return (PARSER_GLOBAL_LENGTH);
14903
14904 u32 *digest = (u32 *) hash_buf->digest;
14905
14906 if (memcmp (SIGNATURE_PHPS, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
14907
14908 salt_t *salt = hash_buf->salt;
14909
14910 char *salt_buf = input_buf + 6;
14911
14912 char *digest_buf = strchr (salt_buf, '$');
14913
14914 if (digest_buf == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14915
14916 uint salt_len = digest_buf - salt_buf;
14917
14918 digest_buf++; // skip the '$' symbol
14919
14920 char *salt_buf_ptr = (char *) salt->salt_buf;
14921
14922 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
14923
14924 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
14925
14926 salt->salt_len = salt_len;
14927
14928 digest[0] = hex_to_u32 ((const u8 *) &digest_buf[ 0]);
14929 digest[1] = hex_to_u32 ((const u8 *) &digest_buf[ 8]);
14930 digest[2] = hex_to_u32 ((const u8 *) &digest_buf[16]);
14931 digest[3] = hex_to_u32 ((const u8 *) &digest_buf[24]);
14932
14933 digest[0] = byte_swap_32 (digest[0]);
14934 digest[1] = byte_swap_32 (digest[1]);
14935 digest[2] = byte_swap_32 (digest[2]);
14936 digest[3] = byte_swap_32 (digest[3]);
14937
14938 digest[0] -= MD5M_A;
14939 digest[1] -= MD5M_B;
14940 digest[2] -= MD5M_C;
14941 digest[3] -= MD5M_D;
14942
14943 return (PARSER_OK);
14944 }
14945
14946 int mediawiki_b_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14947 {
14948 if ((input_len < DISPLAY_LEN_MIN_3711) || (input_len > DISPLAY_LEN_MAX_3711)) return (PARSER_GLOBAL_LENGTH);
14949
14950 if (memcmp (SIGNATURE_MEDIAWIKI_B, input_buf, 3)) return (PARSER_SIGNATURE_UNMATCHED);
14951
14952 u32 *digest = (u32 *) hash_buf->digest;
14953
14954 salt_t *salt = hash_buf->salt;
14955
14956 char *salt_buf = input_buf + 3;
14957
14958 char *digest_buf = strchr (salt_buf, '$');
14959
14960 if (digest_buf == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14961
14962 uint salt_len = digest_buf - salt_buf;
14963
14964 digest_buf++; // skip the '$' symbol
14965
14966 char *salt_buf_ptr = (char *) salt->salt_buf;
14967
14968 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
14969
14970 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
14971
14972 salt_buf_ptr[salt_len] = 0x2d;
14973
14974 salt->salt_len = salt_len + 1;
14975
14976 digest[0] = hex_to_u32 ((const u8 *) &digest_buf[ 0]);
14977 digest[1] = hex_to_u32 ((const u8 *) &digest_buf[ 8]);
14978 digest[2] = hex_to_u32 ((const u8 *) &digest_buf[16]);
14979 digest[3] = hex_to_u32 ((const u8 *) &digest_buf[24]);
14980
14981 digest[0] = byte_swap_32 (digest[0]);
14982 digest[1] = byte_swap_32 (digest[1]);
14983 digest[2] = byte_swap_32 (digest[2]);
14984 digest[3] = byte_swap_32 (digest[3]);
14985
14986 digest[0] -= MD5M_A;
14987 digest[1] -= MD5M_B;
14988 digest[2] -= MD5M_C;
14989 digest[3] -= MD5M_D;
14990
14991 return (PARSER_OK);
14992 }
14993
14994 int peoplesoft_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14995 {
14996 if ((input_len < DISPLAY_LEN_MIN_133) || (input_len > DISPLAY_LEN_MAX_133)) return (PARSER_GLOBAL_LENGTH);
14997
14998 u32 *digest = (u32 *) hash_buf->digest;
14999
15000 salt_t *salt = hash_buf->salt;
15001
15002 u8 tmp_buf[100] = { 0 };
15003
15004 base64_decode (base64_to_int, (const u8 *) input_buf, input_len, tmp_buf);
15005
15006 memcpy (digest, tmp_buf, 20);
15007
15008 digest[0] = byte_swap_32 (digest[0]);
15009 digest[1] = byte_swap_32 (digest[1]);
15010 digest[2] = byte_swap_32 (digest[2]);
15011 digest[3] = byte_swap_32 (digest[3]);
15012 digest[4] = byte_swap_32 (digest[4]);
15013
15014 digest[0] -= SHA1M_A;
15015 digest[1] -= SHA1M_B;
15016 digest[2] -= SHA1M_C;
15017 digest[3] -= SHA1M_D;
15018 digest[4] -= SHA1M_E;
15019
15020 salt->salt_buf[0] = 0x80;
15021
15022 salt->salt_len = 0;
15023
15024 return (PARSER_OK);
15025 }
15026
15027 int skype_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15028 {
15029 if ((input_len < DISPLAY_LEN_MIN_23) || (input_len > DISPLAY_LEN_MAX_23)) return (PARSER_GLOBAL_LENGTH);
15030
15031 u32 *digest = (u32 *) hash_buf->digest;
15032
15033 salt_t *salt = hash_buf->salt;
15034
15035 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
15036 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
15037 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
15038 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
15039
15040 digest[0] = byte_swap_32 (digest[0]);
15041 digest[1] = byte_swap_32 (digest[1]);
15042 digest[2] = byte_swap_32 (digest[2]);
15043 digest[3] = byte_swap_32 (digest[3]);
15044
15045 digest[0] -= MD5M_A;
15046 digest[1] -= MD5M_B;
15047 digest[2] -= MD5M_C;
15048 digest[3] -= MD5M_D;
15049
15050 if (input_buf[32] != ':') return (PARSER_SEPARATOR_UNMATCHED);
15051
15052 uint salt_len = input_len - 32 - 1;
15053
15054 char *salt_buf = input_buf + 32 + 1;
15055
15056 char *salt_buf_ptr = (char *) salt->salt_buf;
15057
15058 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
15059
15060 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
15061
15062 /*
15063 * add static "salt" part
15064 */
15065
15066 memcpy (salt_buf_ptr + salt_len, "\nskyper\n", 8);
15067
15068 salt_len += 8;
15069
15070 salt->salt_len = salt_len;
15071
15072 return (PARSER_OK);
15073 }
15074
15075 int androidfde_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15076 {
15077 if ((input_len < DISPLAY_LEN_MIN_8800) || (input_len > DISPLAY_LEN_MAX_8800)) return (PARSER_GLOBAL_LENGTH);
15078
15079 if (memcmp (SIGNATURE_ANDROIDFDE, input_buf, 5)) return (PARSER_SIGNATURE_UNMATCHED);
15080
15081 u32 *digest = (u32 *) hash_buf->digest;
15082
15083 salt_t *salt = hash_buf->salt;
15084
15085 androidfde_t *androidfde = (androidfde_t *) hash_buf->esalt;
15086
15087 /**
15088 * parse line
15089 */
15090
15091 char *saltlen_pos = input_buf + 1 + 3 + 1;
15092
15093 char *saltbuf_pos = strchr (saltlen_pos, '$');
15094
15095 if (saltbuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15096
15097 uint saltlen_len = saltbuf_pos - saltlen_pos;
15098
15099 if (saltlen_len != 2) return (PARSER_SALT_LENGTH);
15100
15101 saltbuf_pos++;
15102
15103 char *keylen_pos = strchr (saltbuf_pos, '$');
15104
15105 if (keylen_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15106
15107 uint saltbuf_len = keylen_pos - saltbuf_pos;
15108
15109 if (saltbuf_len != 32) return (PARSER_SALT_LENGTH);
15110
15111 keylen_pos++;
15112
15113 char *keybuf_pos = strchr (keylen_pos, '$');
15114
15115 if (keybuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15116
15117 uint keylen_len = keybuf_pos - keylen_pos;
15118
15119 if (keylen_len != 2) return (PARSER_SALT_LENGTH);
15120
15121 keybuf_pos++;
15122
15123 char *databuf_pos = strchr (keybuf_pos, '$');
15124
15125 if (databuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15126
15127 uint keybuf_len = databuf_pos - keybuf_pos;
15128
15129 if (keybuf_len != 32) return (PARSER_SALT_LENGTH);
15130
15131 databuf_pos++;
15132
15133 uint data_len = input_len - 1 - 3 - 1 - saltlen_len - 1 - saltbuf_len - 1 - keylen_len - 1 - keybuf_len - 1;
15134
15135 if (data_len != 3072) return (PARSER_SALT_LENGTH);
15136
15137 /**
15138 * copy data
15139 */
15140
15141 digest[0] = hex_to_u32 ((const u8 *) &keybuf_pos[ 0]);
15142 digest[1] = hex_to_u32 ((const u8 *) &keybuf_pos[ 8]);
15143 digest[2] = hex_to_u32 ((const u8 *) &keybuf_pos[16]);
15144 digest[3] = hex_to_u32 ((const u8 *) &keybuf_pos[24]);
15145
15146 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &saltbuf_pos[ 0]);
15147 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &saltbuf_pos[ 8]);
15148 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &saltbuf_pos[16]);
15149 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &saltbuf_pos[24]);
15150
15151 salt->salt_buf[0] = byte_swap_32 (salt->salt_buf[0]);
15152 salt->salt_buf[1] = byte_swap_32 (salt->salt_buf[1]);
15153 salt->salt_buf[2] = byte_swap_32 (salt->salt_buf[2]);
15154 salt->salt_buf[3] = byte_swap_32 (salt->salt_buf[3]);
15155
15156 salt->salt_len = 16;
15157 salt->salt_iter = ROUNDS_ANDROIDFDE - 1;
15158
15159 for (uint i = 0, j = 0; i < 3072; i += 8, j += 1)
15160 {
15161 androidfde->data[j] = hex_to_u32 ((const u8 *) &databuf_pos[i]);
15162 }
15163
15164 return (PARSER_OK);
15165 }
15166
15167 int scrypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15168 {
15169 if ((input_len < DISPLAY_LEN_MIN_8900) || (input_len > DISPLAY_LEN_MAX_8900)) return (PARSER_GLOBAL_LENGTH);
15170
15171 if (memcmp (SIGNATURE_SCRYPT, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
15172
15173 u32 *digest = (u32 *) hash_buf->digest;
15174
15175 salt_t *salt = hash_buf->salt;
15176
15177 /**
15178 * parse line
15179 */
15180
15181 // first is the N salt parameter
15182
15183 char *N_pos = input_buf + 6;
15184
15185 if (N_pos[0] != ':') return (PARSER_SEPARATOR_UNMATCHED);
15186
15187 N_pos++;
15188
15189 salt->scrypt_N = atoi (N_pos);
15190
15191 // r
15192
15193 char *r_pos = strchr (N_pos, ':');
15194
15195 if (r_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15196
15197 r_pos++;
15198
15199 salt->scrypt_r = atoi (r_pos);
15200
15201 // p
15202
15203 char *p_pos = strchr (r_pos, ':');
15204
15205 if (p_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15206
15207 p_pos++;
15208
15209 salt->scrypt_p = atoi (p_pos);
15210
15211 // salt
15212
15213 char *saltbuf_pos = strchr (p_pos, ':');
15214
15215 if (saltbuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15216
15217 saltbuf_pos++;
15218
15219 char *hash_pos = strchr (saltbuf_pos, ':');
15220
15221 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15222
15223 hash_pos++;
15224
15225 // base64 decode
15226
15227 int salt_len_base64 = hash_pos - saltbuf_pos;
15228
15229 if (salt_len_base64 > 45) return (PARSER_SALT_LENGTH);
15230
15231 u8 tmp_buf[33] = { 0 };
15232
15233 int tmp_len = base64_decode (base64_to_int, (const u8 *) saltbuf_pos, salt_len_base64, tmp_buf);
15234
15235 char *salt_buf_ptr = (char *) salt->salt_buf;
15236
15237 memcpy (salt_buf_ptr, tmp_buf, tmp_len);
15238
15239 salt->salt_len = tmp_len;
15240 salt->salt_iter = 1;
15241
15242 // digest - base64 decode
15243
15244 memset (tmp_buf, 0, sizeof (tmp_buf));
15245
15246 tmp_len = input_len - (hash_pos - input_buf);
15247
15248 if (tmp_len != 44) return (PARSER_GLOBAL_LENGTH);
15249
15250 base64_decode (base64_to_int, (const u8 *) hash_pos, tmp_len, tmp_buf);
15251
15252 memcpy (digest, tmp_buf, 32);
15253
15254 return (PARSER_OK);
15255 }
15256
15257 int juniper_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15258 {
15259 if ((input_len < DISPLAY_LEN_MIN_501) || (input_len > DISPLAY_LEN_MAX_501)) return (PARSER_GLOBAL_LENGTH);
15260
15261 u32 *digest = (u32 *) hash_buf->digest;
15262
15263 salt_t *salt = hash_buf->salt;
15264
15265 /**
15266 * parse line
15267 */
15268
15269 char decrypted[76] = { 0 }; // iv + hash
15270
15271 juniper_decrypt_hash (input_buf, decrypted);
15272
15273 char *md5crypt_hash = decrypted + 12;
15274
15275 if (memcmp (md5crypt_hash, "$1$danastre$", 12)) return (PARSER_SALT_VALUE);
15276
15277 salt->salt_iter = ROUNDS_MD5CRYPT;
15278
15279 char *salt_pos = md5crypt_hash + 3;
15280
15281 char *hash_pos = strchr (salt_pos, '$'); // or simply salt_pos + 8
15282
15283 salt->salt_len = hash_pos - salt_pos; // should be 8
15284
15285 memcpy ((char *) salt->salt_buf, salt_pos, salt->salt_len);
15286
15287 hash_pos++;
15288
15289 md5crypt_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
15290
15291 return (PARSER_OK);
15292 }
15293
15294 int cisco8_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15295 {
15296 if ((input_len < DISPLAY_LEN_MIN_9200) || (input_len > DISPLAY_LEN_MAX_9200)) return (PARSER_GLOBAL_LENGTH);
15297
15298 if (memcmp (SIGNATURE_CISCO8, input_buf, 3)) return (PARSER_SIGNATURE_UNMATCHED);
15299
15300 u32 *digest = (u32 *) hash_buf->digest;
15301
15302 salt_t *salt = hash_buf->salt;
15303
15304 pbkdf2_sha256_t *pbkdf2_sha256 = (pbkdf2_sha256_t *) hash_buf->esalt;
15305
15306 /**
15307 * parse line
15308 */
15309
15310 // first is *raw* salt
15311
15312 char *salt_pos = input_buf + 3;
15313
15314 char *hash_pos = strchr (salt_pos, '$');
15315
15316 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15317
15318 uint salt_len = hash_pos - salt_pos;
15319
15320 if (salt_len != 14) return (PARSER_SALT_LENGTH);
15321
15322 hash_pos++;
15323
15324 char *salt_buf_ptr = (char *) pbkdf2_sha256->salt_buf;
15325
15326 memcpy (salt_buf_ptr, salt_pos, 14);
15327
15328 salt_buf_ptr[17] = 0x01;
15329 salt_buf_ptr[18] = 0x80;
15330
15331 // add some stuff to normal salt to make sorted happy
15332
15333 salt->salt_buf[0] = pbkdf2_sha256->salt_buf[0];
15334 salt->salt_buf[1] = pbkdf2_sha256->salt_buf[1];
15335 salt->salt_buf[2] = pbkdf2_sha256->salt_buf[2];
15336 salt->salt_buf[3] = pbkdf2_sha256->salt_buf[3];
15337
15338 salt->salt_len = salt_len;
15339 salt->salt_iter = ROUNDS_CISCO8 - 1;
15340
15341 // base64 decode hash
15342
15343 u8 tmp_buf[100] = { 0 };
15344
15345 uint hash_len = input_len - 3 - salt_len - 1;
15346
15347 int tmp_len = base64_decode (itoa64_to_int, (const u8 *) hash_pos, hash_len, tmp_buf);
15348
15349 if (tmp_len != 32) return (PARSER_HASH_LENGTH);
15350
15351 memcpy (digest, tmp_buf, 32);
15352
15353 digest[0] = byte_swap_32 (digest[0]);
15354 digest[1] = byte_swap_32 (digest[1]);
15355 digest[2] = byte_swap_32 (digest[2]);
15356 digest[3] = byte_swap_32 (digest[3]);
15357 digest[4] = byte_swap_32 (digest[4]);
15358 digest[5] = byte_swap_32 (digest[5]);
15359 digest[6] = byte_swap_32 (digest[6]);
15360 digest[7] = byte_swap_32 (digest[7]);
15361
15362 return (PARSER_OK);
15363 }
15364
15365 int cisco9_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15366 {
15367 if ((input_len < DISPLAY_LEN_MIN_9300) || (input_len > DISPLAY_LEN_MAX_9300)) return (PARSER_GLOBAL_LENGTH);
15368
15369 if (memcmp (SIGNATURE_CISCO9, input_buf, 3)) return (PARSER_SIGNATURE_UNMATCHED);
15370
15371 u32 *digest = (u32 *) hash_buf->digest;
15372
15373 salt_t *salt = hash_buf->salt;
15374
15375 /**
15376 * parse line
15377 */
15378
15379 // first is *raw* salt
15380
15381 char *salt_pos = input_buf + 3;
15382
15383 char *hash_pos = strchr (salt_pos, '$');
15384
15385 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15386
15387 uint salt_len = hash_pos - salt_pos;
15388
15389 if (salt_len != 14) return (PARSER_SALT_LENGTH);
15390
15391 salt->salt_len = salt_len;
15392 hash_pos++;
15393
15394 char *salt_buf_ptr = (char *) salt->salt_buf;
15395
15396 memcpy (salt_buf_ptr, salt_pos, salt_len);
15397 salt_buf_ptr[salt_len] = 0;
15398
15399 // base64 decode hash
15400
15401 u8 tmp_buf[100] = { 0 };
15402
15403 uint hash_len = input_len - 3 - salt_len - 1;
15404
15405 int tmp_len = base64_decode (itoa64_to_int, (const u8 *) hash_pos, hash_len, tmp_buf);
15406
15407 if (tmp_len != 32) return (PARSER_HASH_LENGTH);
15408
15409 memcpy (digest, tmp_buf, 32);
15410
15411 // fixed:
15412 salt->scrypt_N = 16384;
15413 salt->scrypt_r = 1;
15414 salt->scrypt_p = 1;
15415 salt->salt_iter = 1;
15416
15417 return (PARSER_OK);
15418 }
15419
15420 int office2007_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15421 {
15422 if ((input_len < DISPLAY_LEN_MIN_9400) || (input_len > DISPLAY_LEN_MAX_9400)) return (PARSER_GLOBAL_LENGTH);
15423
15424 if (memcmp (SIGNATURE_OFFICE2007, input_buf, 8)) return (PARSER_SIGNATURE_UNMATCHED);
15425
15426 u32 *digest = (u32 *) hash_buf->digest;
15427
15428 salt_t *salt = hash_buf->salt;
15429
15430 office2007_t *office2007 = (office2007_t *) hash_buf->esalt;
15431
15432 /**
15433 * parse line
15434 */
15435
15436 char *version_pos = input_buf + 8 + 1;
15437
15438 char *verifierHashSize_pos = strchr (version_pos, '*');
15439
15440 if (verifierHashSize_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15441
15442 u32 version_len = verifierHashSize_pos - version_pos;
15443
15444 if (version_len != 4) return (PARSER_SALT_LENGTH);
15445
15446 verifierHashSize_pos++;
15447
15448 char *keySize_pos = strchr (verifierHashSize_pos, '*');
15449
15450 if (keySize_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15451
15452 u32 verifierHashSize_len = keySize_pos - verifierHashSize_pos;
15453
15454 if (verifierHashSize_len != 2) return (PARSER_SALT_LENGTH);
15455
15456 keySize_pos++;
15457
15458 char *saltSize_pos = strchr (keySize_pos, '*');
15459
15460 if (saltSize_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15461
15462 u32 keySize_len = saltSize_pos - keySize_pos;
15463
15464 if (keySize_len != 3) return (PARSER_SALT_LENGTH);
15465
15466 saltSize_pos++;
15467
15468 char *osalt_pos = strchr (saltSize_pos, '*');
15469
15470 if (osalt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15471
15472 u32 saltSize_len = osalt_pos - saltSize_pos;
15473
15474 if (saltSize_len != 2) return (PARSER_SALT_LENGTH);
15475
15476 osalt_pos++;
15477
15478 char *encryptedVerifier_pos = strchr (osalt_pos, '*');
15479
15480 if (encryptedVerifier_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15481
15482 u32 osalt_len = encryptedVerifier_pos - osalt_pos;
15483
15484 if (osalt_len != 32) return (PARSER_SALT_LENGTH);
15485
15486 encryptedVerifier_pos++;
15487
15488 char *encryptedVerifierHash_pos = strchr (encryptedVerifier_pos, '*');
15489
15490 if (encryptedVerifierHash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15491
15492 u32 encryptedVerifier_len = encryptedVerifierHash_pos - encryptedVerifier_pos;
15493
15494 if (encryptedVerifier_len != 32) return (PARSER_SALT_LENGTH);
15495
15496 encryptedVerifierHash_pos++;
15497
15498 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;
15499
15500 if (encryptedVerifierHash_len != 40) return (PARSER_SALT_LENGTH);
15501
15502 const uint version = atoi (version_pos);
15503
15504 if (version != 2007) return (PARSER_SALT_VALUE);
15505
15506 const uint verifierHashSize = atoi (verifierHashSize_pos);
15507
15508 if (verifierHashSize != 20) return (PARSER_SALT_VALUE);
15509
15510 const uint keySize = atoi (keySize_pos);
15511
15512 if ((keySize != 128) && (keySize != 256)) return (PARSER_SALT_VALUE);
15513
15514 office2007->keySize = keySize;
15515
15516 const uint saltSize = atoi (saltSize_pos);
15517
15518 if (saltSize != 16) return (PARSER_SALT_VALUE);
15519
15520 /**
15521 * salt
15522 */
15523
15524 salt->salt_len = 16;
15525 salt->salt_iter = ROUNDS_OFFICE2007;
15526
15527 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &osalt_pos[ 0]);
15528 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &osalt_pos[ 8]);
15529 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &osalt_pos[16]);
15530 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &osalt_pos[24]);
15531
15532 /**
15533 * esalt
15534 */
15535
15536 office2007->encryptedVerifier[0] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 0]);
15537 office2007->encryptedVerifier[1] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 8]);
15538 office2007->encryptedVerifier[2] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[16]);
15539 office2007->encryptedVerifier[3] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[24]);
15540
15541 office2007->encryptedVerifierHash[0] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 0]);
15542 office2007->encryptedVerifierHash[1] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 8]);
15543 office2007->encryptedVerifierHash[2] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[16]);
15544 office2007->encryptedVerifierHash[3] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[24]);
15545 office2007->encryptedVerifierHash[4] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[32]);
15546
15547 /**
15548 * digest
15549 */
15550
15551 digest[0] = office2007->encryptedVerifierHash[0];
15552 digest[1] = office2007->encryptedVerifierHash[1];
15553 digest[2] = office2007->encryptedVerifierHash[2];
15554 digest[3] = office2007->encryptedVerifierHash[3];
15555
15556 return (PARSER_OK);
15557 }
15558
15559 int office2010_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15560 {
15561 if ((input_len < DISPLAY_LEN_MIN_9500) || (input_len > DISPLAY_LEN_MAX_9500)) return (PARSER_GLOBAL_LENGTH);
15562
15563 if (memcmp (SIGNATURE_OFFICE2010, input_buf, 8)) return (PARSER_SIGNATURE_UNMATCHED);
15564
15565 u32 *digest = (u32 *) hash_buf->digest;
15566
15567 salt_t *salt = hash_buf->salt;
15568
15569 office2010_t *office2010 = (office2010_t *) hash_buf->esalt;
15570
15571 /**
15572 * parse line
15573 */
15574
15575 char *version_pos = input_buf + 8 + 1;
15576
15577 char *spinCount_pos = strchr (version_pos, '*');
15578
15579 if (spinCount_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15580
15581 u32 version_len = spinCount_pos - version_pos;
15582
15583 if (version_len != 4) return (PARSER_SALT_LENGTH);
15584
15585 spinCount_pos++;
15586
15587 char *keySize_pos = strchr (spinCount_pos, '*');
15588
15589 if (keySize_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15590
15591 u32 spinCount_len = keySize_pos - spinCount_pos;
15592
15593 if (spinCount_len != 6) return (PARSER_SALT_LENGTH);
15594
15595 keySize_pos++;
15596
15597 char *saltSize_pos = strchr (keySize_pos, '*');
15598
15599 if (saltSize_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15600
15601 u32 keySize_len = saltSize_pos - keySize_pos;
15602
15603 if (keySize_len != 3) return (PARSER_SALT_LENGTH);
15604
15605 saltSize_pos++;
15606
15607 char *osalt_pos = strchr (saltSize_pos, '*');
15608
15609 if (osalt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15610
15611 u32 saltSize_len = osalt_pos - saltSize_pos;
15612
15613 if (saltSize_len != 2) return (PARSER_SALT_LENGTH);
15614
15615 osalt_pos++;
15616
15617 char *encryptedVerifier_pos = strchr (osalt_pos, '*');
15618
15619 if (encryptedVerifier_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15620
15621 u32 osalt_len = encryptedVerifier_pos - osalt_pos;
15622
15623 if (osalt_len != 32) return (PARSER_SALT_LENGTH);
15624
15625 encryptedVerifier_pos++;
15626
15627 char *encryptedVerifierHash_pos = strchr (encryptedVerifier_pos, '*');
15628
15629 if (encryptedVerifierHash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15630
15631 u32 encryptedVerifier_len = encryptedVerifierHash_pos - encryptedVerifier_pos;
15632
15633 if (encryptedVerifier_len != 32) return (PARSER_SALT_LENGTH);
15634
15635 encryptedVerifierHash_pos++;
15636
15637 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;
15638
15639 if (encryptedVerifierHash_len != 64) return (PARSER_SALT_LENGTH);
15640
15641 const uint version = atoi (version_pos);
15642
15643 if (version != 2010) return (PARSER_SALT_VALUE);
15644
15645 const uint spinCount = atoi (spinCount_pos);
15646
15647 if (spinCount != 100000) return (PARSER_SALT_VALUE);
15648
15649 const uint keySize = atoi (keySize_pos);
15650
15651 if (keySize != 128) return (PARSER_SALT_VALUE);
15652
15653 const uint saltSize = atoi (saltSize_pos);
15654
15655 if (saltSize != 16) return (PARSER_SALT_VALUE);
15656
15657 /**
15658 * salt
15659 */
15660
15661 salt->salt_len = 16;
15662 salt->salt_iter = spinCount;
15663
15664 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &osalt_pos[ 0]);
15665 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &osalt_pos[ 8]);
15666 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &osalt_pos[16]);
15667 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &osalt_pos[24]);
15668
15669 /**
15670 * esalt
15671 */
15672
15673 office2010->encryptedVerifier[0] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 0]);
15674 office2010->encryptedVerifier[1] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 8]);
15675 office2010->encryptedVerifier[2] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[16]);
15676 office2010->encryptedVerifier[3] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[24]);
15677
15678 office2010->encryptedVerifierHash[0] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 0]);
15679 office2010->encryptedVerifierHash[1] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 8]);
15680 office2010->encryptedVerifierHash[2] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[16]);
15681 office2010->encryptedVerifierHash[3] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[24]);
15682 office2010->encryptedVerifierHash[4] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[32]);
15683 office2010->encryptedVerifierHash[5] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[40]);
15684 office2010->encryptedVerifierHash[6] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[48]);
15685 office2010->encryptedVerifierHash[7] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[56]);
15686
15687 /**
15688 * digest
15689 */
15690
15691 digest[0] = office2010->encryptedVerifierHash[0];
15692 digest[1] = office2010->encryptedVerifierHash[1];
15693 digest[2] = office2010->encryptedVerifierHash[2];
15694 digest[3] = office2010->encryptedVerifierHash[3];
15695
15696 return (PARSER_OK);
15697 }
15698
15699 int office2013_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15700 {
15701 if ((input_len < DISPLAY_LEN_MIN_9600) || (input_len > DISPLAY_LEN_MAX_9600)) return (PARSER_GLOBAL_LENGTH);
15702
15703 if (memcmp (SIGNATURE_OFFICE2013, input_buf, 8)) return (PARSER_SIGNATURE_UNMATCHED);
15704
15705 u32 *digest = (u32 *) hash_buf->digest;
15706
15707 salt_t *salt = hash_buf->salt;
15708
15709 office2013_t *office2013 = (office2013_t *) hash_buf->esalt;
15710
15711 /**
15712 * parse line
15713 */
15714
15715 char *version_pos = input_buf + 8 + 1;
15716
15717 char *spinCount_pos = strchr (version_pos, '*');
15718
15719 if (spinCount_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15720
15721 u32 version_len = spinCount_pos - version_pos;
15722
15723 if (version_len != 4) return (PARSER_SALT_LENGTH);
15724
15725 spinCount_pos++;
15726
15727 char *keySize_pos = strchr (spinCount_pos, '*');
15728
15729 if (keySize_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15730
15731 u32 spinCount_len = keySize_pos - spinCount_pos;
15732
15733 if (spinCount_len != 6) return (PARSER_SALT_LENGTH);
15734
15735 keySize_pos++;
15736
15737 char *saltSize_pos = strchr (keySize_pos, '*');
15738
15739 if (saltSize_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15740
15741 u32 keySize_len = saltSize_pos - keySize_pos;
15742
15743 if (keySize_len != 3) return (PARSER_SALT_LENGTH);
15744
15745 saltSize_pos++;
15746
15747 char *osalt_pos = strchr (saltSize_pos, '*');
15748
15749 if (osalt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15750
15751 u32 saltSize_len = osalt_pos - saltSize_pos;
15752
15753 if (saltSize_len != 2) return (PARSER_SALT_LENGTH);
15754
15755 osalt_pos++;
15756
15757 char *encryptedVerifier_pos = strchr (osalt_pos, '*');
15758
15759 if (encryptedVerifier_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15760
15761 u32 osalt_len = encryptedVerifier_pos - osalt_pos;
15762
15763 if (osalt_len != 32) return (PARSER_SALT_LENGTH);
15764
15765 encryptedVerifier_pos++;
15766
15767 char *encryptedVerifierHash_pos = strchr (encryptedVerifier_pos, '*');
15768
15769 if (encryptedVerifierHash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15770
15771 u32 encryptedVerifier_len = encryptedVerifierHash_pos - encryptedVerifier_pos;
15772
15773 if (encryptedVerifier_len != 32) return (PARSER_SALT_LENGTH);
15774
15775 encryptedVerifierHash_pos++;
15776
15777 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;
15778
15779 if (encryptedVerifierHash_len != 64) return (PARSER_SALT_LENGTH);
15780
15781 const uint version = atoi (version_pos);
15782
15783 if (version != 2013) return (PARSER_SALT_VALUE);
15784
15785 const uint spinCount = atoi (spinCount_pos);
15786
15787 if (spinCount != 100000) return (PARSER_SALT_VALUE);
15788
15789 const uint keySize = atoi (keySize_pos);
15790
15791 if (keySize != 256) return (PARSER_SALT_VALUE);
15792
15793 const uint saltSize = atoi (saltSize_pos);
15794
15795 if (saltSize != 16) return (PARSER_SALT_VALUE);
15796
15797 /**
15798 * salt
15799 */
15800
15801 salt->salt_len = 16;
15802 salt->salt_iter = spinCount;
15803
15804 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &osalt_pos[ 0]);
15805 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &osalt_pos[ 8]);
15806 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &osalt_pos[16]);
15807 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &osalt_pos[24]);
15808
15809 /**
15810 * esalt
15811 */
15812
15813 office2013->encryptedVerifier[0] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 0]);
15814 office2013->encryptedVerifier[1] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 8]);
15815 office2013->encryptedVerifier[2] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[16]);
15816 office2013->encryptedVerifier[3] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[24]);
15817
15818 office2013->encryptedVerifierHash[0] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 0]);
15819 office2013->encryptedVerifierHash[1] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 8]);
15820 office2013->encryptedVerifierHash[2] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[16]);
15821 office2013->encryptedVerifierHash[3] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[24]);
15822 office2013->encryptedVerifierHash[4] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[32]);
15823 office2013->encryptedVerifierHash[5] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[40]);
15824 office2013->encryptedVerifierHash[6] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[48]);
15825 office2013->encryptedVerifierHash[7] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[56]);
15826
15827 /**
15828 * digest
15829 */
15830
15831 digest[0] = office2013->encryptedVerifierHash[0];
15832 digest[1] = office2013->encryptedVerifierHash[1];
15833 digest[2] = office2013->encryptedVerifierHash[2];
15834 digest[3] = office2013->encryptedVerifierHash[3];
15835
15836 return (PARSER_OK);
15837 }
15838
15839 int oldoffice01_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15840 {
15841 if ((input_len < DISPLAY_LEN_MIN_9700) || (input_len > DISPLAY_LEN_MAX_9700)) return (PARSER_GLOBAL_LENGTH);
15842
15843 if ((memcmp (SIGNATURE_OLDOFFICE0, input_buf, 12)) && (memcmp (SIGNATURE_OLDOFFICE1, input_buf, 12))) return (PARSER_SIGNATURE_UNMATCHED);
15844
15845 u32 *digest = (u32 *) hash_buf->digest;
15846
15847 salt_t *salt = hash_buf->salt;
15848
15849 oldoffice01_t *oldoffice01 = (oldoffice01_t *) hash_buf->esalt;
15850
15851 /**
15852 * parse line
15853 */
15854
15855 char *version_pos = input_buf + 11;
15856
15857 char *osalt_pos = strchr (version_pos, '*');
15858
15859 if (osalt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15860
15861 u32 version_len = osalt_pos - version_pos;
15862
15863 if (version_len != 1) return (PARSER_SALT_LENGTH);
15864
15865 osalt_pos++;
15866
15867 char *encryptedVerifier_pos = strchr (osalt_pos, '*');
15868
15869 if (encryptedVerifier_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15870
15871 u32 osalt_len = encryptedVerifier_pos - osalt_pos;
15872
15873 if (osalt_len != 32) return (PARSER_SALT_LENGTH);
15874
15875 encryptedVerifier_pos++;
15876
15877 char *encryptedVerifierHash_pos = strchr (encryptedVerifier_pos, '*');
15878
15879 if (encryptedVerifierHash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15880
15881 u32 encryptedVerifier_len = encryptedVerifierHash_pos - encryptedVerifier_pos;
15882
15883 if (encryptedVerifier_len != 32) return (PARSER_SALT_LENGTH);
15884
15885 encryptedVerifierHash_pos++;
15886
15887 u32 encryptedVerifierHash_len = input_len - 11 - version_len - 1 - osalt_len - 1 - encryptedVerifier_len - 1;
15888
15889 if (encryptedVerifierHash_len != 32) return (PARSER_SALT_LENGTH);
15890
15891 const uint version = *version_pos - 0x30;
15892
15893 if (version != 0 && version != 1) return (PARSER_SALT_VALUE);
15894
15895 /**
15896 * esalt
15897 */
15898
15899 oldoffice01->version = version;
15900
15901 oldoffice01->encryptedVerifier[0] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 0]);
15902 oldoffice01->encryptedVerifier[1] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 8]);
15903 oldoffice01->encryptedVerifier[2] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[16]);
15904 oldoffice01->encryptedVerifier[3] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[24]);
15905
15906 oldoffice01->encryptedVerifier[0] = byte_swap_32 (oldoffice01->encryptedVerifier[0]);
15907 oldoffice01->encryptedVerifier[1] = byte_swap_32 (oldoffice01->encryptedVerifier[1]);
15908 oldoffice01->encryptedVerifier[2] = byte_swap_32 (oldoffice01->encryptedVerifier[2]);
15909 oldoffice01->encryptedVerifier[3] = byte_swap_32 (oldoffice01->encryptedVerifier[3]);
15910
15911 oldoffice01->encryptedVerifierHash[0] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 0]);
15912 oldoffice01->encryptedVerifierHash[1] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 8]);
15913 oldoffice01->encryptedVerifierHash[2] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[16]);
15914 oldoffice01->encryptedVerifierHash[3] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[24]);
15915
15916 oldoffice01->encryptedVerifierHash[0] = byte_swap_32 (oldoffice01->encryptedVerifierHash[0]);
15917 oldoffice01->encryptedVerifierHash[1] = byte_swap_32 (oldoffice01->encryptedVerifierHash[1]);
15918 oldoffice01->encryptedVerifierHash[2] = byte_swap_32 (oldoffice01->encryptedVerifierHash[2]);
15919 oldoffice01->encryptedVerifierHash[3] = byte_swap_32 (oldoffice01->encryptedVerifierHash[3]);
15920
15921 /**
15922 * salt
15923 */
15924
15925 salt->salt_len = 16;
15926
15927 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &osalt_pos[ 0]);
15928 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &osalt_pos[ 8]);
15929 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &osalt_pos[16]);
15930 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &osalt_pos[24]);
15931
15932 salt->salt_buf[0] = byte_swap_32 (salt->salt_buf[0]);
15933 salt->salt_buf[1] = byte_swap_32 (salt->salt_buf[1]);
15934 salt->salt_buf[2] = byte_swap_32 (salt->salt_buf[2]);
15935 salt->salt_buf[3] = byte_swap_32 (salt->salt_buf[3]);
15936
15937 // this is a workaround as office produces multiple documents with the same salt
15938
15939 salt->salt_len += 32;
15940
15941 salt->salt_buf[ 4] = oldoffice01->encryptedVerifier[0];
15942 salt->salt_buf[ 5] = oldoffice01->encryptedVerifier[1];
15943 salt->salt_buf[ 6] = oldoffice01->encryptedVerifier[2];
15944 salt->salt_buf[ 7] = oldoffice01->encryptedVerifier[3];
15945 salt->salt_buf[ 8] = oldoffice01->encryptedVerifierHash[0];
15946 salt->salt_buf[ 9] = oldoffice01->encryptedVerifierHash[1];
15947 salt->salt_buf[10] = oldoffice01->encryptedVerifierHash[2];
15948 salt->salt_buf[11] = oldoffice01->encryptedVerifierHash[3];
15949
15950 /**
15951 * digest
15952 */
15953
15954 digest[0] = oldoffice01->encryptedVerifierHash[0];
15955 digest[1] = oldoffice01->encryptedVerifierHash[1];
15956 digest[2] = oldoffice01->encryptedVerifierHash[2];
15957 digest[3] = oldoffice01->encryptedVerifierHash[3];
15958
15959 return (PARSER_OK);
15960 }
15961
15962 int oldoffice01cm1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15963 {
15964 return oldoffice01_parse_hash (input_buf, input_len, hash_buf);
15965 }
15966
15967 int oldoffice01cm2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15968 {
15969 if ((input_len < DISPLAY_LEN_MIN_9720) || (input_len > DISPLAY_LEN_MAX_9720)) return (PARSER_GLOBAL_LENGTH);
15970
15971 if ((memcmp (SIGNATURE_OLDOFFICE0, input_buf, 12)) && (memcmp (SIGNATURE_OLDOFFICE1, input_buf, 12))) return (PARSER_SIGNATURE_UNMATCHED);
15972
15973 u32 *digest = (u32 *) hash_buf->digest;
15974
15975 salt_t *salt = hash_buf->salt;
15976
15977 oldoffice01_t *oldoffice01 = (oldoffice01_t *) hash_buf->esalt;
15978
15979 /**
15980 * parse line
15981 */
15982
15983 char *version_pos = input_buf + 11;
15984
15985 char *osalt_pos = strchr (version_pos, '*');
15986
15987 if (osalt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15988
15989 u32 version_len = osalt_pos - version_pos;
15990
15991 if (version_len != 1) return (PARSER_SALT_LENGTH);
15992
15993 osalt_pos++;
15994
15995 char *encryptedVerifier_pos = strchr (osalt_pos, '*');
15996
15997 if (encryptedVerifier_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15998
15999 u32 osalt_len = encryptedVerifier_pos - osalt_pos;
16000
16001 if (osalt_len != 32) return (PARSER_SALT_LENGTH);
16002
16003 encryptedVerifier_pos++;
16004
16005 char *encryptedVerifierHash_pos = strchr (encryptedVerifier_pos, '*');
16006
16007 if (encryptedVerifierHash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16008
16009 u32 encryptedVerifier_len = encryptedVerifierHash_pos - encryptedVerifier_pos;
16010
16011 if (encryptedVerifier_len != 32) return (PARSER_SALT_LENGTH);
16012
16013 encryptedVerifierHash_pos++;
16014
16015 char *rc4key_pos = strchr (encryptedVerifierHash_pos, ':');
16016
16017 if (rc4key_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16018
16019 u32 encryptedVerifierHash_len = rc4key_pos - encryptedVerifierHash_pos;
16020
16021 if (encryptedVerifierHash_len != 32) return (PARSER_SALT_LENGTH);
16022
16023 rc4key_pos++;
16024
16025 u32 rc4key_len = input_len - 11 - version_len - 1 - osalt_len - 1 - encryptedVerifier_len - 1 - encryptedVerifierHash_len - 1;
16026
16027 if (rc4key_len != 10) return (PARSER_SALT_LENGTH);
16028
16029 const uint version = *version_pos - 0x30;
16030
16031 if (version != 0 && version != 1) return (PARSER_SALT_VALUE);
16032
16033 /**
16034 * esalt
16035 */
16036
16037 oldoffice01->version = version;
16038
16039 oldoffice01->encryptedVerifier[0] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 0]);
16040 oldoffice01->encryptedVerifier[1] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 8]);
16041 oldoffice01->encryptedVerifier[2] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[16]);
16042 oldoffice01->encryptedVerifier[3] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[24]);
16043
16044 oldoffice01->encryptedVerifier[0] = byte_swap_32 (oldoffice01->encryptedVerifier[0]);
16045 oldoffice01->encryptedVerifier[1] = byte_swap_32 (oldoffice01->encryptedVerifier[1]);
16046 oldoffice01->encryptedVerifier[2] = byte_swap_32 (oldoffice01->encryptedVerifier[2]);
16047 oldoffice01->encryptedVerifier[3] = byte_swap_32 (oldoffice01->encryptedVerifier[3]);
16048
16049 oldoffice01->encryptedVerifierHash[0] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 0]);
16050 oldoffice01->encryptedVerifierHash[1] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 8]);
16051 oldoffice01->encryptedVerifierHash[2] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[16]);
16052 oldoffice01->encryptedVerifierHash[3] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[24]);
16053
16054 oldoffice01->encryptedVerifierHash[0] = byte_swap_32 (oldoffice01->encryptedVerifierHash[0]);
16055 oldoffice01->encryptedVerifierHash[1] = byte_swap_32 (oldoffice01->encryptedVerifierHash[1]);
16056 oldoffice01->encryptedVerifierHash[2] = byte_swap_32 (oldoffice01->encryptedVerifierHash[2]);
16057 oldoffice01->encryptedVerifierHash[3] = byte_swap_32 (oldoffice01->encryptedVerifierHash[3]);
16058
16059 oldoffice01->rc4key[1] = 0;
16060 oldoffice01->rc4key[0] = 0;
16061
16062 oldoffice01->rc4key[0] |= hex_convert (rc4key_pos[0]) << 28;
16063 oldoffice01->rc4key[0] |= hex_convert (rc4key_pos[1]) << 24;
16064 oldoffice01->rc4key[0] |= hex_convert (rc4key_pos[2]) << 20;
16065 oldoffice01->rc4key[0] |= hex_convert (rc4key_pos[3]) << 16;
16066 oldoffice01->rc4key[0] |= hex_convert (rc4key_pos[4]) << 12;
16067 oldoffice01->rc4key[0] |= hex_convert (rc4key_pos[5]) << 8;
16068 oldoffice01->rc4key[0] |= hex_convert (rc4key_pos[6]) << 4;
16069 oldoffice01->rc4key[0] |= hex_convert (rc4key_pos[7]) << 0;
16070 oldoffice01->rc4key[1] |= hex_convert (rc4key_pos[8]) << 28;
16071 oldoffice01->rc4key[1] |= hex_convert (rc4key_pos[9]) << 24;
16072
16073 oldoffice01->rc4key[0] = byte_swap_32 (oldoffice01->rc4key[0]);
16074 oldoffice01->rc4key[1] = byte_swap_32 (oldoffice01->rc4key[1]);
16075
16076 /**
16077 * salt
16078 */
16079
16080 salt->salt_len = 16;
16081
16082 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &osalt_pos[ 0]);
16083 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &osalt_pos[ 8]);
16084 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &osalt_pos[16]);
16085 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &osalt_pos[24]);
16086
16087 salt->salt_buf[0] = byte_swap_32 (salt->salt_buf[0]);
16088 salt->salt_buf[1] = byte_swap_32 (salt->salt_buf[1]);
16089 salt->salt_buf[2] = byte_swap_32 (salt->salt_buf[2]);
16090 salt->salt_buf[3] = byte_swap_32 (salt->salt_buf[3]);
16091
16092 // this is a workaround as office produces multiple documents with the same salt
16093
16094 salt->salt_len += 32;
16095
16096 salt->salt_buf[ 4] = oldoffice01->encryptedVerifier[0];
16097 salt->salt_buf[ 5] = oldoffice01->encryptedVerifier[1];
16098 salt->salt_buf[ 6] = oldoffice01->encryptedVerifier[2];
16099 salt->salt_buf[ 7] = oldoffice01->encryptedVerifier[3];
16100 salt->salt_buf[ 8] = oldoffice01->encryptedVerifierHash[0];
16101 salt->salt_buf[ 9] = oldoffice01->encryptedVerifierHash[1];
16102 salt->salt_buf[10] = oldoffice01->encryptedVerifierHash[2];
16103 salt->salt_buf[11] = oldoffice01->encryptedVerifierHash[3];
16104
16105 /**
16106 * digest
16107 */
16108
16109 digest[0] = oldoffice01->rc4key[0];
16110 digest[1] = oldoffice01->rc4key[1];
16111 digest[2] = 0;
16112 digest[3] = 0;
16113
16114 return (PARSER_OK);
16115 }
16116
16117 int oldoffice34_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16118 {
16119 if ((input_len < DISPLAY_LEN_MIN_9800) || (input_len > DISPLAY_LEN_MAX_9800)) return (PARSER_GLOBAL_LENGTH);
16120
16121 if ((memcmp (SIGNATURE_OLDOFFICE3, input_buf, 12)) && (memcmp (SIGNATURE_OLDOFFICE4, input_buf, 12))) return (PARSER_SIGNATURE_UNMATCHED);
16122
16123 u32 *digest = (u32 *) hash_buf->digest;
16124
16125 salt_t *salt = hash_buf->salt;
16126
16127 oldoffice34_t *oldoffice34 = (oldoffice34_t *) hash_buf->esalt;
16128
16129 /**
16130 * parse line
16131 */
16132
16133 char *version_pos = input_buf + 11;
16134
16135 char *osalt_pos = strchr (version_pos, '*');
16136
16137 if (osalt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16138
16139 u32 version_len = osalt_pos - version_pos;
16140
16141 if (version_len != 1) return (PARSER_SALT_LENGTH);
16142
16143 osalt_pos++;
16144
16145 char *encryptedVerifier_pos = strchr (osalt_pos, '*');
16146
16147 if (encryptedVerifier_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16148
16149 u32 osalt_len = encryptedVerifier_pos - osalt_pos;
16150
16151 if (osalt_len != 32) return (PARSER_SALT_LENGTH);
16152
16153 encryptedVerifier_pos++;
16154
16155 char *encryptedVerifierHash_pos = strchr (encryptedVerifier_pos, '*');
16156
16157 if (encryptedVerifierHash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16158
16159 u32 encryptedVerifier_len = encryptedVerifierHash_pos - encryptedVerifier_pos;
16160
16161 if (encryptedVerifier_len != 32) return (PARSER_SALT_LENGTH);
16162
16163 encryptedVerifierHash_pos++;
16164
16165 u32 encryptedVerifierHash_len = input_len - 11 - version_len - 1 - osalt_len - 1 - encryptedVerifier_len - 1;
16166
16167 if (encryptedVerifierHash_len != 40) return (PARSER_SALT_LENGTH);
16168
16169 const uint version = *version_pos - 0x30;
16170
16171 if (version != 3 && version != 4) return (PARSER_SALT_VALUE);
16172
16173 /**
16174 * esalt
16175 */
16176
16177 oldoffice34->version = version;
16178
16179 oldoffice34->encryptedVerifier[0] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 0]);
16180 oldoffice34->encryptedVerifier[1] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 8]);
16181 oldoffice34->encryptedVerifier[2] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[16]);
16182 oldoffice34->encryptedVerifier[3] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[24]);
16183
16184 oldoffice34->encryptedVerifier[0] = byte_swap_32 (oldoffice34->encryptedVerifier[0]);
16185 oldoffice34->encryptedVerifier[1] = byte_swap_32 (oldoffice34->encryptedVerifier[1]);
16186 oldoffice34->encryptedVerifier[2] = byte_swap_32 (oldoffice34->encryptedVerifier[2]);
16187 oldoffice34->encryptedVerifier[3] = byte_swap_32 (oldoffice34->encryptedVerifier[3]);
16188
16189 oldoffice34->encryptedVerifierHash[0] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 0]);
16190 oldoffice34->encryptedVerifierHash[1] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 8]);
16191 oldoffice34->encryptedVerifierHash[2] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[16]);
16192 oldoffice34->encryptedVerifierHash[3] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[24]);
16193 oldoffice34->encryptedVerifierHash[4] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[32]);
16194
16195 oldoffice34->encryptedVerifierHash[0] = byte_swap_32 (oldoffice34->encryptedVerifierHash[0]);
16196 oldoffice34->encryptedVerifierHash[1] = byte_swap_32 (oldoffice34->encryptedVerifierHash[1]);
16197 oldoffice34->encryptedVerifierHash[2] = byte_swap_32 (oldoffice34->encryptedVerifierHash[2]);
16198 oldoffice34->encryptedVerifierHash[3] = byte_swap_32 (oldoffice34->encryptedVerifierHash[3]);
16199 oldoffice34->encryptedVerifierHash[4] = byte_swap_32 (oldoffice34->encryptedVerifierHash[4]);
16200
16201 /**
16202 * salt
16203 */
16204
16205 salt->salt_len = 16;
16206
16207 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &osalt_pos[ 0]);
16208 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &osalt_pos[ 8]);
16209 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &osalt_pos[16]);
16210 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &osalt_pos[24]);
16211
16212 // this is a workaround as office produces multiple documents with the same salt
16213
16214 salt->salt_len += 32;
16215
16216 salt->salt_buf[ 4] = oldoffice34->encryptedVerifier[0];
16217 salt->salt_buf[ 5] = oldoffice34->encryptedVerifier[1];
16218 salt->salt_buf[ 6] = oldoffice34->encryptedVerifier[2];
16219 salt->salt_buf[ 7] = oldoffice34->encryptedVerifier[3];
16220 salt->salt_buf[ 8] = oldoffice34->encryptedVerifierHash[0];
16221 salt->salt_buf[ 9] = oldoffice34->encryptedVerifierHash[1];
16222 salt->salt_buf[10] = oldoffice34->encryptedVerifierHash[2];
16223 salt->salt_buf[11] = oldoffice34->encryptedVerifierHash[3];
16224
16225 /**
16226 * digest
16227 */
16228
16229 digest[0] = oldoffice34->encryptedVerifierHash[0];
16230 digest[1] = oldoffice34->encryptedVerifierHash[1];
16231 digest[2] = oldoffice34->encryptedVerifierHash[2];
16232 digest[3] = oldoffice34->encryptedVerifierHash[3];
16233
16234 return (PARSER_OK);
16235 }
16236
16237 int oldoffice34cm1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16238 {
16239 if (memcmp (SIGNATURE_OLDOFFICE3, input_buf, 12)) return (PARSER_SIGNATURE_UNMATCHED);
16240
16241 return oldoffice34_parse_hash (input_buf, input_len, hash_buf);
16242 }
16243
16244 int oldoffice34cm2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16245 {
16246 if ((input_len < DISPLAY_LEN_MIN_9820) || (input_len > DISPLAY_LEN_MAX_9820)) return (PARSER_GLOBAL_LENGTH);
16247
16248 if (memcmp (SIGNATURE_OLDOFFICE3, input_buf, 12)) return (PARSER_SIGNATURE_UNMATCHED);
16249
16250 u32 *digest = (u32 *) hash_buf->digest;
16251
16252 salt_t *salt = hash_buf->salt;
16253
16254 oldoffice34_t *oldoffice34 = (oldoffice34_t *) hash_buf->esalt;
16255
16256 /**
16257 * parse line
16258 */
16259
16260 char *version_pos = input_buf + 11;
16261
16262 char *osalt_pos = strchr (version_pos, '*');
16263
16264 if (osalt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16265
16266 u32 version_len = osalt_pos - version_pos;
16267
16268 if (version_len != 1) return (PARSER_SALT_LENGTH);
16269
16270 osalt_pos++;
16271
16272 char *encryptedVerifier_pos = strchr (osalt_pos, '*');
16273
16274 if (encryptedVerifier_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16275
16276 u32 osalt_len = encryptedVerifier_pos - osalt_pos;
16277
16278 if (osalt_len != 32) return (PARSER_SALT_LENGTH);
16279
16280 encryptedVerifier_pos++;
16281
16282 char *encryptedVerifierHash_pos = strchr (encryptedVerifier_pos, '*');
16283
16284 if (encryptedVerifierHash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16285
16286 u32 encryptedVerifier_len = encryptedVerifierHash_pos - encryptedVerifier_pos;
16287
16288 if (encryptedVerifier_len != 32) return (PARSER_SALT_LENGTH);
16289
16290 encryptedVerifierHash_pos++;
16291
16292 char *rc4key_pos = strchr (encryptedVerifierHash_pos, ':');
16293
16294 if (rc4key_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16295
16296 u32 encryptedVerifierHash_len = rc4key_pos - encryptedVerifierHash_pos;
16297
16298 if (encryptedVerifierHash_len != 40) return (PARSER_SALT_LENGTH);
16299
16300 rc4key_pos++;
16301
16302 u32 rc4key_len = input_len - 11 - version_len - 1 - osalt_len - 1 - encryptedVerifier_len - 1 - encryptedVerifierHash_len - 1;
16303
16304 if (rc4key_len != 10) return (PARSER_SALT_LENGTH);
16305
16306 const uint version = *version_pos - 0x30;
16307
16308 if (version != 3 && version != 4) return (PARSER_SALT_VALUE);
16309
16310 /**
16311 * esalt
16312 */
16313
16314 oldoffice34->version = version;
16315
16316 oldoffice34->encryptedVerifier[0] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 0]);
16317 oldoffice34->encryptedVerifier[1] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 8]);
16318 oldoffice34->encryptedVerifier[2] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[16]);
16319 oldoffice34->encryptedVerifier[3] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[24]);
16320
16321 oldoffice34->encryptedVerifier[0] = byte_swap_32 (oldoffice34->encryptedVerifier[0]);
16322 oldoffice34->encryptedVerifier[1] = byte_swap_32 (oldoffice34->encryptedVerifier[1]);
16323 oldoffice34->encryptedVerifier[2] = byte_swap_32 (oldoffice34->encryptedVerifier[2]);
16324 oldoffice34->encryptedVerifier[3] = byte_swap_32 (oldoffice34->encryptedVerifier[3]);
16325
16326 oldoffice34->encryptedVerifierHash[0] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 0]);
16327 oldoffice34->encryptedVerifierHash[1] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 8]);
16328 oldoffice34->encryptedVerifierHash[2] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[16]);
16329 oldoffice34->encryptedVerifierHash[3] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[24]);
16330 oldoffice34->encryptedVerifierHash[4] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[32]);
16331
16332 oldoffice34->encryptedVerifierHash[0] = byte_swap_32 (oldoffice34->encryptedVerifierHash[0]);
16333 oldoffice34->encryptedVerifierHash[1] = byte_swap_32 (oldoffice34->encryptedVerifierHash[1]);
16334 oldoffice34->encryptedVerifierHash[2] = byte_swap_32 (oldoffice34->encryptedVerifierHash[2]);
16335 oldoffice34->encryptedVerifierHash[3] = byte_swap_32 (oldoffice34->encryptedVerifierHash[3]);
16336 oldoffice34->encryptedVerifierHash[4] = byte_swap_32 (oldoffice34->encryptedVerifierHash[4]);
16337
16338 oldoffice34->rc4key[1] = 0;
16339 oldoffice34->rc4key[0] = 0;
16340
16341 oldoffice34->rc4key[0] |= hex_convert (rc4key_pos[0]) << 28;
16342 oldoffice34->rc4key[0] |= hex_convert (rc4key_pos[1]) << 24;
16343 oldoffice34->rc4key[0] |= hex_convert (rc4key_pos[2]) << 20;
16344 oldoffice34->rc4key[0] |= hex_convert (rc4key_pos[3]) << 16;
16345 oldoffice34->rc4key[0] |= hex_convert (rc4key_pos[4]) << 12;
16346 oldoffice34->rc4key[0] |= hex_convert (rc4key_pos[5]) << 8;
16347 oldoffice34->rc4key[0] |= hex_convert (rc4key_pos[6]) << 4;
16348 oldoffice34->rc4key[0] |= hex_convert (rc4key_pos[7]) << 0;
16349 oldoffice34->rc4key[1] |= hex_convert (rc4key_pos[8]) << 28;
16350 oldoffice34->rc4key[1] |= hex_convert (rc4key_pos[9]) << 24;
16351
16352 oldoffice34->rc4key[0] = byte_swap_32 (oldoffice34->rc4key[0]);
16353 oldoffice34->rc4key[1] = byte_swap_32 (oldoffice34->rc4key[1]);
16354
16355 /**
16356 * salt
16357 */
16358
16359 salt->salt_len = 16;
16360
16361 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &osalt_pos[ 0]);
16362 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &osalt_pos[ 8]);
16363 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &osalt_pos[16]);
16364 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &osalt_pos[24]);
16365
16366 // this is a workaround as office produces multiple documents with the same salt
16367
16368 salt->salt_len += 32;
16369
16370 salt->salt_buf[ 4] = oldoffice34->encryptedVerifier[0];
16371 salt->salt_buf[ 5] = oldoffice34->encryptedVerifier[1];
16372 salt->salt_buf[ 6] = oldoffice34->encryptedVerifier[2];
16373 salt->salt_buf[ 7] = oldoffice34->encryptedVerifier[3];
16374 salt->salt_buf[ 8] = oldoffice34->encryptedVerifierHash[0];
16375 salt->salt_buf[ 9] = oldoffice34->encryptedVerifierHash[1];
16376 salt->salt_buf[10] = oldoffice34->encryptedVerifierHash[2];
16377 salt->salt_buf[11] = oldoffice34->encryptedVerifierHash[3];
16378
16379 /**
16380 * digest
16381 */
16382
16383 digest[0] = oldoffice34->rc4key[0];
16384 digest[1] = oldoffice34->rc4key[1];
16385 digest[2] = 0;
16386 digest[3] = 0;
16387
16388 return (PARSER_OK);
16389 }
16390
16391 int radmin2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16392 {
16393 if ((input_len < DISPLAY_LEN_MIN_9900) || (input_len > DISPLAY_LEN_MAX_9900)) return (PARSER_GLOBAL_LENGTH);
16394
16395 u32 *digest = (u32 *) hash_buf->digest;
16396
16397 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
16398 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
16399 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
16400 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
16401
16402 digest[0] = byte_swap_32 (digest[0]);
16403 digest[1] = byte_swap_32 (digest[1]);
16404 digest[2] = byte_swap_32 (digest[2]);
16405 digest[3] = byte_swap_32 (digest[3]);
16406
16407 return (PARSER_OK);
16408 }
16409
16410 int djangosha1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16411 {
16412 if ((input_len < DISPLAY_LEN_MIN_124) || (input_len > DISPLAY_LEN_MAX_124)) return (PARSER_GLOBAL_LENGTH);
16413
16414 if ((memcmp (SIGNATURE_DJANGOSHA1, input_buf, 5)) && (memcmp (SIGNATURE_DJANGOSHA1, input_buf, 5))) return (PARSER_SIGNATURE_UNMATCHED);
16415
16416 u32 *digest = (u32 *) hash_buf->digest;
16417
16418 salt_t *salt = hash_buf->salt;
16419
16420 char *signature_pos = input_buf;
16421
16422 char *salt_pos = strchr (signature_pos, '$');
16423
16424 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16425
16426 u32 signature_len = salt_pos - signature_pos;
16427
16428 if (signature_len != 4) return (PARSER_SIGNATURE_UNMATCHED);
16429
16430 salt_pos++;
16431
16432 char *hash_pos = strchr (salt_pos, '$');
16433
16434 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16435
16436 u32 salt_len = hash_pos - salt_pos;
16437
16438 if (salt_len > 32) return (PARSER_SALT_LENGTH);
16439
16440 hash_pos++;
16441
16442 u32 hash_len = input_len - signature_len - 1 - salt_len - 1;
16443
16444 if (hash_len != 40) return (PARSER_SALT_LENGTH);
16445
16446 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
16447 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
16448 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
16449 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
16450 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
16451
16452 digest[0] -= SHA1M_A;
16453 digest[1] -= SHA1M_B;
16454 digest[2] -= SHA1M_C;
16455 digest[3] -= SHA1M_D;
16456 digest[4] -= SHA1M_E;
16457
16458 char *salt_buf_ptr = (char *) salt->salt_buf;
16459
16460 memcpy (salt_buf_ptr, salt_pos, salt_len);
16461
16462 salt->salt_len = salt_len;
16463
16464 return (PARSER_OK);
16465 }
16466
16467 int djangopbkdf2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16468 {
16469 if ((input_len < DISPLAY_LEN_MIN_10000) || (input_len > DISPLAY_LEN_MAX_10000)) return (PARSER_GLOBAL_LENGTH);
16470
16471 if (memcmp (SIGNATURE_DJANGOPBKDF2, input_buf, 14)) return (PARSER_SIGNATURE_UNMATCHED);
16472
16473 u32 *digest = (u32 *) hash_buf->digest;
16474
16475 salt_t *salt = hash_buf->salt;
16476
16477 pbkdf2_sha256_t *pbkdf2_sha256 = (pbkdf2_sha256_t *) hash_buf->esalt;
16478
16479 /**
16480 * parse line
16481 */
16482
16483 char *iter_pos = input_buf + 14;
16484
16485 const int iter = atoi (iter_pos);
16486
16487 if (iter < 1) return (PARSER_SALT_ITERATION);
16488
16489 salt->salt_iter = iter - 1;
16490
16491 char *salt_pos = strchr (iter_pos, '$');
16492
16493 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16494
16495 salt_pos++;
16496
16497 char *hash_pos = strchr (salt_pos, '$');
16498
16499 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16500
16501 const uint salt_len = hash_pos - salt_pos;
16502
16503 hash_pos++;
16504
16505 char *salt_buf_ptr = (char *) pbkdf2_sha256->salt_buf;
16506
16507 memcpy (salt_buf_ptr, salt_pos, salt_len);
16508
16509 salt->salt_len = salt_len;
16510
16511 salt_buf_ptr[salt_len + 3] = 0x01;
16512 salt_buf_ptr[salt_len + 4] = 0x80;
16513
16514 // add some stuff to normal salt to make sorted happy
16515
16516 salt->salt_buf[0] = pbkdf2_sha256->salt_buf[0];
16517 salt->salt_buf[1] = pbkdf2_sha256->salt_buf[1];
16518 salt->salt_buf[2] = pbkdf2_sha256->salt_buf[2];
16519 salt->salt_buf[3] = pbkdf2_sha256->salt_buf[3];
16520 salt->salt_buf[4] = salt->salt_iter;
16521
16522 // base64 decode hash
16523
16524 u8 tmp_buf[100] = { 0 };
16525
16526 uint hash_len = input_len - (hash_pos - input_buf);
16527
16528 if (hash_len != 44) return (PARSER_HASH_LENGTH);
16529
16530 base64_decode (base64_to_int, (const u8 *) hash_pos, hash_len, tmp_buf);
16531
16532 memcpy (digest, tmp_buf, 32);
16533
16534 digest[0] = byte_swap_32 (digest[0]);
16535 digest[1] = byte_swap_32 (digest[1]);
16536 digest[2] = byte_swap_32 (digest[2]);
16537 digest[3] = byte_swap_32 (digest[3]);
16538 digest[4] = byte_swap_32 (digest[4]);
16539 digest[5] = byte_swap_32 (digest[5]);
16540 digest[6] = byte_swap_32 (digest[6]);
16541 digest[7] = byte_swap_32 (digest[7]);
16542
16543 return (PARSER_OK);
16544 }
16545
16546 int siphash_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16547 {
16548 if ((input_len < DISPLAY_LEN_MIN_10100) || (input_len > DISPLAY_LEN_MAX_10100)) return (PARSER_GLOBAL_LENGTH);
16549
16550 u32 *digest = (u32 *) hash_buf->digest;
16551
16552 salt_t *salt = hash_buf->salt;
16553
16554 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
16555 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
16556 digest[2] = 0;
16557 digest[3] = 0;
16558
16559 digest[0] = byte_swap_32 (digest[0]);
16560 digest[1] = byte_swap_32 (digest[1]);
16561
16562 if (input_buf[16] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
16563 if (input_buf[18] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
16564 if (input_buf[20] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
16565
16566 char iter_c = input_buf[17];
16567 char iter_d = input_buf[19];
16568
16569 // atm only defaults, let's see if there's more request
16570 if (iter_c != '2') return (PARSER_SALT_ITERATION);
16571 if (iter_d != '4') return (PARSER_SALT_ITERATION);
16572
16573 char *salt_buf = input_buf + 16 + 1 + 1 + 1 + 1 + 1;
16574
16575 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_buf[ 0]);
16576 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_buf[ 8]);
16577 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &salt_buf[16]);
16578 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &salt_buf[24]);
16579
16580 salt->salt_buf[0] = byte_swap_32 (salt->salt_buf[0]);
16581 salt->salt_buf[1] = byte_swap_32 (salt->salt_buf[1]);
16582 salt->salt_buf[2] = byte_swap_32 (salt->salt_buf[2]);
16583 salt->salt_buf[3] = byte_swap_32 (salt->salt_buf[3]);
16584
16585 salt->salt_len = 16;
16586
16587 return (PARSER_OK);
16588 }
16589
16590 int crammd5_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16591 {
16592 if ((input_len < DISPLAY_LEN_MIN_10200) || (input_len > DISPLAY_LEN_MAX_10200)) return (PARSER_GLOBAL_LENGTH);
16593
16594 if (memcmp (SIGNATURE_CRAM_MD5, input_buf, 10)) return (PARSER_SIGNATURE_UNMATCHED);
16595
16596 u32 *digest = (u32 *) hash_buf->digest;
16597
16598 cram_md5_t *cram_md5 = (cram_md5_t *) hash_buf->esalt;
16599
16600 salt_t *salt = hash_buf->salt;
16601
16602 char *salt_pos = input_buf + 10;
16603
16604 char *hash_pos = strchr (salt_pos, '$');
16605
16606 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16607
16608 uint salt_len = hash_pos - salt_pos;
16609
16610 hash_pos++;
16611
16612 uint hash_len = input_len - 10 - salt_len - 1;
16613
16614 // base64 decode salt
16615
16616 if (salt_len > 133) return (PARSER_SALT_LENGTH);
16617
16618 u8 tmp_buf[100] = { 0 };
16619
16620 salt_len = base64_decode (base64_to_int, (const u8 *) salt_pos, salt_len, tmp_buf);
16621
16622 if (salt_len > 55) return (PARSER_SALT_LENGTH);
16623
16624 tmp_buf[salt_len] = 0x80;
16625
16626 memcpy (&salt->salt_buf, tmp_buf, salt_len + 1);
16627
16628 salt->salt_len = salt_len;
16629
16630 // base64 decode hash
16631
16632 if (hash_len > 133) return (PARSER_HASH_LENGTH);
16633
16634 memset (tmp_buf, 0, sizeof (tmp_buf));
16635
16636 hash_len = base64_decode (base64_to_int, (const u8 *) hash_pos, hash_len, tmp_buf);
16637
16638 if (hash_len < 32 + 1) return (PARSER_SALT_LENGTH);
16639
16640 uint user_len = hash_len - 32;
16641
16642 const u8 *tmp_hash = tmp_buf + user_len;
16643
16644 user_len--; // skip the trailing space
16645
16646 digest[0] = hex_to_u32 (&tmp_hash[ 0]);
16647 digest[1] = hex_to_u32 (&tmp_hash[ 8]);
16648 digest[2] = hex_to_u32 (&tmp_hash[16]);
16649 digest[3] = hex_to_u32 (&tmp_hash[24]);
16650
16651 digest[0] = byte_swap_32 (digest[0]);
16652 digest[1] = byte_swap_32 (digest[1]);
16653 digest[2] = byte_swap_32 (digest[2]);
16654 digest[3] = byte_swap_32 (digest[3]);
16655
16656 // store username for host only (output hash if cracked)
16657
16658 memset (cram_md5->user, 0, sizeof (cram_md5->user));
16659 memcpy (cram_md5->user, tmp_buf, user_len);
16660
16661 return (PARSER_OK);
16662 }
16663
16664 int saph_sha1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16665 {
16666 if ((input_len < DISPLAY_LEN_MIN_10300) || (input_len > DISPLAY_LEN_MAX_10300)) return (PARSER_GLOBAL_LENGTH);
16667
16668 if (memcmp (SIGNATURE_SAPH_SHA1, input_buf, 10)) return (PARSER_SIGNATURE_UNMATCHED);
16669
16670 u32 *digest = (u32 *) hash_buf->digest;
16671
16672 salt_t *salt = hash_buf->salt;
16673
16674 char *iter_pos = input_buf + 10;
16675
16676 u32 iter = atoi (iter_pos);
16677
16678 if (iter < 1)
16679 {
16680 return (PARSER_SALT_ITERATION);
16681 }
16682
16683 iter--; // first iteration is special
16684
16685 salt->salt_iter = iter;
16686
16687 char *base64_pos = strchr (iter_pos, '}');
16688
16689 if (base64_pos == NULL)
16690 {
16691 return (PARSER_SIGNATURE_UNMATCHED);
16692 }
16693
16694 base64_pos++;
16695
16696 // base64 decode salt
16697
16698 u32 base64_len = input_len - (base64_pos - input_buf);
16699
16700 u8 tmp_buf[100] = { 0 };
16701
16702 u32 decoded_len = base64_decode (base64_to_int, (const u8 *) base64_pos, base64_len, tmp_buf);
16703
16704 if (decoded_len < 24)
16705 {
16706 return (PARSER_SALT_LENGTH);
16707 }
16708
16709 // copy the salt
16710
16711 uint salt_len = decoded_len - 20;
16712
16713 if (salt_len < 4) return (PARSER_SALT_LENGTH);
16714 if (salt_len > 16) return (PARSER_SALT_LENGTH);
16715
16716 memcpy (&salt->salt_buf, tmp_buf + 20, salt_len);
16717
16718 salt->salt_len = salt_len;
16719
16720 // set digest
16721
16722 u32 *digest_ptr = (u32*) tmp_buf;
16723
16724 digest[0] = byte_swap_32 (digest_ptr[0]);
16725 digest[1] = byte_swap_32 (digest_ptr[1]);
16726 digest[2] = byte_swap_32 (digest_ptr[2]);
16727 digest[3] = byte_swap_32 (digest_ptr[3]);
16728 digest[4] = byte_swap_32 (digest_ptr[4]);
16729
16730 return (PARSER_OK);
16731 }
16732
16733 int redmine_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16734 {
16735 if ((input_len < DISPLAY_LEN_MIN_7600) || (input_len > DISPLAY_LEN_MAX_7600)) return (PARSER_GLOBAL_LENGTH);
16736
16737 u32 *digest = (u32 *) hash_buf->digest;
16738
16739 salt_t *salt = hash_buf->salt;
16740
16741 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
16742 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
16743 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
16744 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
16745 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
16746
16747 if (input_buf[40] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
16748
16749 uint salt_len = input_len - 40 - 1;
16750
16751 char *salt_buf = input_buf + 40 + 1;
16752
16753 char *salt_buf_ptr = (char *) salt->salt_buf;
16754
16755 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
16756
16757 if (salt_len != 32) return (PARSER_SALT_LENGTH);
16758
16759 salt->salt_len = salt_len;
16760
16761 return (PARSER_OK);
16762 }
16763
16764 int pdf11_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16765 {
16766 if ((input_len < DISPLAY_LEN_MIN_10400) || (input_len > DISPLAY_LEN_MAX_10400)) return (PARSER_GLOBAL_LENGTH);
16767
16768 if ((memcmp (SIGNATURE_PDF, input_buf, 5)) && (memcmp (SIGNATURE_PDF, input_buf, 5))) return (PARSER_SIGNATURE_UNMATCHED);
16769
16770 u32 *digest = (u32 *) hash_buf->digest;
16771
16772 salt_t *salt = hash_buf->salt;
16773
16774 pdf_t *pdf = (pdf_t *) hash_buf->esalt;
16775
16776 /**
16777 * parse line
16778 */
16779
16780 char *V_pos = input_buf + 5;
16781
16782 char *R_pos = strchr (V_pos, '*');
16783
16784 if (R_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16785
16786 u32 V_len = R_pos - V_pos;
16787
16788 R_pos++;
16789
16790 char *bits_pos = strchr (R_pos, '*');
16791
16792 if (bits_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16793
16794 u32 R_len = bits_pos - R_pos;
16795
16796 bits_pos++;
16797
16798 char *P_pos = strchr (bits_pos, '*');
16799
16800 if (P_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16801
16802 u32 bits_len = P_pos - bits_pos;
16803
16804 P_pos++;
16805
16806 char *enc_md_pos = strchr (P_pos, '*');
16807
16808 if (enc_md_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16809
16810 u32 P_len = enc_md_pos - P_pos;
16811
16812 enc_md_pos++;
16813
16814 char *id_len_pos = strchr (enc_md_pos, '*');
16815
16816 if (id_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16817
16818 u32 enc_md_len = id_len_pos - enc_md_pos;
16819
16820 id_len_pos++;
16821
16822 char *id_buf_pos = strchr (id_len_pos, '*');
16823
16824 if (id_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16825
16826 u32 id_len_len = id_buf_pos - id_len_pos;
16827
16828 id_buf_pos++;
16829
16830 char *u_len_pos = strchr (id_buf_pos, '*');
16831
16832 if (u_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16833
16834 u32 id_buf_len = u_len_pos - id_buf_pos;
16835
16836 if (id_buf_len != 32) return (PARSER_SALT_LENGTH);
16837
16838 u_len_pos++;
16839
16840 char *u_buf_pos = strchr (u_len_pos, '*');
16841
16842 if (u_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16843
16844 u32 u_len_len = u_buf_pos - u_len_pos;
16845
16846 u_buf_pos++;
16847
16848 char *o_len_pos = strchr (u_buf_pos, '*');
16849
16850 if (o_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16851
16852 u32 u_buf_len = o_len_pos - u_buf_pos;
16853
16854 if (u_buf_len != 64) return (PARSER_SALT_LENGTH);
16855
16856 o_len_pos++;
16857
16858 char *o_buf_pos = strchr (o_len_pos, '*');
16859
16860 if (o_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16861
16862 u32 o_len_len = o_buf_pos - o_len_pos;
16863
16864 o_buf_pos++;
16865
16866 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;
16867
16868 if (o_buf_len != 64) return (PARSER_SALT_LENGTH);
16869
16870 // validate data
16871
16872 const int V = atoi (V_pos);
16873 const int R = atoi (R_pos);
16874 const int P = atoi (P_pos);
16875
16876 if (V != 1) return (PARSER_SALT_VALUE);
16877 if (R != 2) return (PARSER_SALT_VALUE);
16878
16879 const int enc_md = atoi (enc_md_pos);
16880
16881 if ((enc_md != 0) && (enc_md != 1)) return (PARSER_SALT_VALUE);
16882
16883 const int id_len = atoi (id_len_pos);
16884 const int u_len = atoi (u_len_pos);
16885 const int o_len = atoi (o_len_pos);
16886
16887 if (id_len != 16) return (PARSER_SALT_VALUE);
16888 if (u_len != 32) return (PARSER_SALT_VALUE);
16889 if (o_len != 32) return (PARSER_SALT_VALUE);
16890
16891 const int bits = atoi (bits_pos);
16892
16893 if (bits != 40) return (PARSER_SALT_VALUE);
16894
16895 // copy data to esalt
16896
16897 pdf->V = V;
16898 pdf->R = R;
16899 pdf->P = P;
16900
16901 pdf->enc_md = enc_md;
16902
16903 pdf->id_buf[0] = hex_to_u32 ((const u8 *) &id_buf_pos[ 0]);
16904 pdf->id_buf[1] = hex_to_u32 ((const u8 *) &id_buf_pos[ 8]);
16905 pdf->id_buf[2] = hex_to_u32 ((const u8 *) &id_buf_pos[16]);
16906 pdf->id_buf[3] = hex_to_u32 ((const u8 *) &id_buf_pos[24]);
16907 pdf->id_len = id_len;
16908
16909 pdf->u_buf[0] = hex_to_u32 ((const u8 *) &u_buf_pos[ 0]);
16910 pdf->u_buf[1] = hex_to_u32 ((const u8 *) &u_buf_pos[ 8]);
16911 pdf->u_buf[2] = hex_to_u32 ((const u8 *) &u_buf_pos[16]);
16912 pdf->u_buf[3] = hex_to_u32 ((const u8 *) &u_buf_pos[24]);
16913 pdf->u_buf[4] = hex_to_u32 ((const u8 *) &u_buf_pos[32]);
16914 pdf->u_buf[5] = hex_to_u32 ((const u8 *) &u_buf_pos[40]);
16915 pdf->u_buf[6] = hex_to_u32 ((const u8 *) &u_buf_pos[48]);
16916 pdf->u_buf[7] = hex_to_u32 ((const u8 *) &u_buf_pos[56]);
16917 pdf->u_len = u_len;
16918
16919 pdf->o_buf[0] = hex_to_u32 ((const u8 *) &o_buf_pos[ 0]);
16920 pdf->o_buf[1] = hex_to_u32 ((const u8 *) &o_buf_pos[ 8]);
16921 pdf->o_buf[2] = hex_to_u32 ((const u8 *) &o_buf_pos[16]);
16922 pdf->o_buf[3] = hex_to_u32 ((const u8 *) &o_buf_pos[24]);
16923 pdf->o_buf[4] = hex_to_u32 ((const u8 *) &o_buf_pos[32]);
16924 pdf->o_buf[5] = hex_to_u32 ((const u8 *) &o_buf_pos[40]);
16925 pdf->o_buf[6] = hex_to_u32 ((const u8 *) &o_buf_pos[48]);
16926 pdf->o_buf[7] = hex_to_u32 ((const u8 *) &o_buf_pos[56]);
16927 pdf->o_len = o_len;
16928
16929 pdf->id_buf[0] = byte_swap_32 (pdf->id_buf[0]);
16930 pdf->id_buf[1] = byte_swap_32 (pdf->id_buf[1]);
16931 pdf->id_buf[2] = byte_swap_32 (pdf->id_buf[2]);
16932 pdf->id_buf[3] = byte_swap_32 (pdf->id_buf[3]);
16933
16934 pdf->u_buf[0] = byte_swap_32 (pdf->u_buf[0]);
16935 pdf->u_buf[1] = byte_swap_32 (pdf->u_buf[1]);
16936 pdf->u_buf[2] = byte_swap_32 (pdf->u_buf[2]);
16937 pdf->u_buf[3] = byte_swap_32 (pdf->u_buf[3]);
16938 pdf->u_buf[4] = byte_swap_32 (pdf->u_buf[4]);
16939 pdf->u_buf[5] = byte_swap_32 (pdf->u_buf[5]);
16940 pdf->u_buf[6] = byte_swap_32 (pdf->u_buf[6]);
16941 pdf->u_buf[7] = byte_swap_32 (pdf->u_buf[7]);
16942
16943 pdf->o_buf[0] = byte_swap_32 (pdf->o_buf[0]);
16944 pdf->o_buf[1] = byte_swap_32 (pdf->o_buf[1]);
16945 pdf->o_buf[2] = byte_swap_32 (pdf->o_buf[2]);
16946 pdf->o_buf[3] = byte_swap_32 (pdf->o_buf[3]);
16947 pdf->o_buf[4] = byte_swap_32 (pdf->o_buf[4]);
16948 pdf->o_buf[5] = byte_swap_32 (pdf->o_buf[5]);
16949 pdf->o_buf[6] = byte_swap_32 (pdf->o_buf[6]);
16950 pdf->o_buf[7] = byte_swap_32 (pdf->o_buf[7]);
16951
16952 // we use ID for salt, maybe needs to change, we will see...
16953
16954 salt->salt_buf[0] = pdf->id_buf[0];
16955 salt->salt_buf[1] = pdf->id_buf[1];
16956 salt->salt_buf[2] = pdf->id_buf[2];
16957 salt->salt_buf[3] = pdf->id_buf[3];
16958 salt->salt_len = pdf->id_len;
16959
16960 digest[0] = pdf->u_buf[0];
16961 digest[1] = pdf->u_buf[1];
16962 digest[2] = pdf->u_buf[2];
16963 digest[3] = pdf->u_buf[3];
16964
16965 return (PARSER_OK);
16966 }
16967
16968 int pdf11cm1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16969 {
16970 return pdf11_parse_hash (input_buf, input_len, hash_buf);
16971 }
16972
16973 int pdf11cm2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16974 {
16975 if ((input_len < DISPLAY_LEN_MIN_10420) || (input_len > DISPLAY_LEN_MAX_10420)) return (PARSER_GLOBAL_LENGTH);
16976
16977 if ((memcmp (SIGNATURE_PDF, input_buf, 5)) && (memcmp (SIGNATURE_PDF, input_buf, 5))) return (PARSER_SIGNATURE_UNMATCHED);
16978
16979 u32 *digest = (u32 *) hash_buf->digest;
16980
16981 salt_t *salt = hash_buf->salt;
16982
16983 pdf_t *pdf = (pdf_t *) hash_buf->esalt;
16984
16985 /**
16986 * parse line
16987 */
16988
16989 char *V_pos = input_buf + 5;
16990
16991 char *R_pos = strchr (V_pos, '*');
16992
16993 if (R_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16994
16995 u32 V_len = R_pos - V_pos;
16996
16997 R_pos++;
16998
16999 char *bits_pos = strchr (R_pos, '*');
17000
17001 if (bits_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17002
17003 u32 R_len = bits_pos - R_pos;
17004
17005 bits_pos++;
17006
17007 char *P_pos = strchr (bits_pos, '*');
17008
17009 if (P_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17010
17011 u32 bits_len = P_pos - bits_pos;
17012
17013 P_pos++;
17014
17015 char *enc_md_pos = strchr (P_pos, '*');
17016
17017 if (enc_md_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17018
17019 u32 P_len = enc_md_pos - P_pos;
17020
17021 enc_md_pos++;
17022
17023 char *id_len_pos = strchr (enc_md_pos, '*');
17024
17025 if (id_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17026
17027 u32 enc_md_len = id_len_pos - enc_md_pos;
17028
17029 id_len_pos++;
17030
17031 char *id_buf_pos = strchr (id_len_pos, '*');
17032
17033 if (id_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17034
17035 u32 id_len_len = id_buf_pos - id_len_pos;
17036
17037 id_buf_pos++;
17038
17039 char *u_len_pos = strchr (id_buf_pos, '*');
17040
17041 if (u_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17042
17043 u32 id_buf_len = u_len_pos - id_buf_pos;
17044
17045 if (id_buf_len != 32) return (PARSER_SALT_LENGTH);
17046
17047 u_len_pos++;
17048
17049 char *u_buf_pos = strchr (u_len_pos, '*');
17050
17051 if (u_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17052
17053 u32 u_len_len = u_buf_pos - u_len_pos;
17054
17055 u_buf_pos++;
17056
17057 char *o_len_pos = strchr (u_buf_pos, '*');
17058
17059 if (o_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17060
17061 u32 u_buf_len = o_len_pos - u_buf_pos;
17062
17063 if (u_buf_len != 64) return (PARSER_SALT_LENGTH);
17064
17065 o_len_pos++;
17066
17067 char *o_buf_pos = strchr (o_len_pos, '*');
17068
17069 if (o_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17070
17071 u32 o_len_len = o_buf_pos - o_len_pos;
17072
17073 o_buf_pos++;
17074
17075 char *rc4key_pos = strchr (o_buf_pos, ':');
17076
17077 if (rc4key_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17078
17079 u32 o_buf_len = rc4key_pos - o_buf_pos;
17080
17081 if (o_buf_len != 64) return (PARSER_SALT_LENGTH);
17082
17083 rc4key_pos++;
17084
17085 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;
17086
17087 if (rc4key_len != 10) return (PARSER_SALT_LENGTH);
17088
17089 // validate data
17090
17091 const int V = atoi (V_pos);
17092 const int R = atoi (R_pos);
17093 const int P = atoi (P_pos);
17094
17095 if (V != 1) return (PARSER_SALT_VALUE);
17096 if (R != 2) return (PARSER_SALT_VALUE);
17097
17098 const int enc_md = atoi (enc_md_pos);
17099
17100 if ((enc_md != 0) && (enc_md != 1)) return (PARSER_SALT_VALUE);
17101
17102 const int id_len = atoi (id_len_pos);
17103 const int u_len = atoi (u_len_pos);
17104 const int o_len = atoi (o_len_pos);
17105
17106 if (id_len != 16) return (PARSER_SALT_VALUE);
17107 if (u_len != 32) return (PARSER_SALT_VALUE);
17108 if (o_len != 32) return (PARSER_SALT_VALUE);
17109
17110 const int bits = atoi (bits_pos);
17111
17112 if (bits != 40) return (PARSER_SALT_VALUE);
17113
17114 // copy data to esalt
17115
17116 pdf->V = V;
17117 pdf->R = R;
17118 pdf->P = P;
17119
17120 pdf->enc_md = enc_md;
17121
17122 pdf->id_buf[0] = hex_to_u32 ((const u8 *) &id_buf_pos[ 0]);
17123 pdf->id_buf[1] = hex_to_u32 ((const u8 *) &id_buf_pos[ 8]);
17124 pdf->id_buf[2] = hex_to_u32 ((const u8 *) &id_buf_pos[16]);
17125 pdf->id_buf[3] = hex_to_u32 ((const u8 *) &id_buf_pos[24]);
17126 pdf->id_len = id_len;
17127
17128 pdf->u_buf[0] = hex_to_u32 ((const u8 *) &u_buf_pos[ 0]);
17129 pdf->u_buf[1] = hex_to_u32 ((const u8 *) &u_buf_pos[ 8]);
17130 pdf->u_buf[2] = hex_to_u32 ((const u8 *) &u_buf_pos[16]);
17131 pdf->u_buf[3] = hex_to_u32 ((const u8 *) &u_buf_pos[24]);
17132 pdf->u_buf[4] = hex_to_u32 ((const u8 *) &u_buf_pos[32]);
17133 pdf->u_buf[5] = hex_to_u32 ((const u8 *) &u_buf_pos[40]);
17134 pdf->u_buf[6] = hex_to_u32 ((const u8 *) &u_buf_pos[48]);
17135 pdf->u_buf[7] = hex_to_u32 ((const u8 *) &u_buf_pos[56]);
17136 pdf->u_len = u_len;
17137
17138 pdf->o_buf[0] = hex_to_u32 ((const u8 *) &o_buf_pos[ 0]);
17139 pdf->o_buf[1] = hex_to_u32 ((const u8 *) &o_buf_pos[ 8]);
17140 pdf->o_buf[2] = hex_to_u32 ((const u8 *) &o_buf_pos[16]);
17141 pdf->o_buf[3] = hex_to_u32 ((const u8 *) &o_buf_pos[24]);
17142 pdf->o_buf[4] = hex_to_u32 ((const u8 *) &o_buf_pos[32]);
17143 pdf->o_buf[5] = hex_to_u32 ((const u8 *) &o_buf_pos[40]);
17144 pdf->o_buf[6] = hex_to_u32 ((const u8 *) &o_buf_pos[48]);
17145 pdf->o_buf[7] = hex_to_u32 ((const u8 *) &o_buf_pos[56]);
17146 pdf->o_len = o_len;
17147
17148 pdf->id_buf[0] = byte_swap_32 (pdf->id_buf[0]);
17149 pdf->id_buf[1] = byte_swap_32 (pdf->id_buf[1]);
17150 pdf->id_buf[2] = byte_swap_32 (pdf->id_buf[2]);
17151 pdf->id_buf[3] = byte_swap_32 (pdf->id_buf[3]);
17152
17153 pdf->u_buf[0] = byte_swap_32 (pdf->u_buf[0]);
17154 pdf->u_buf[1] = byte_swap_32 (pdf->u_buf[1]);
17155 pdf->u_buf[2] = byte_swap_32 (pdf->u_buf[2]);
17156 pdf->u_buf[3] = byte_swap_32 (pdf->u_buf[3]);
17157 pdf->u_buf[4] = byte_swap_32 (pdf->u_buf[4]);
17158 pdf->u_buf[5] = byte_swap_32 (pdf->u_buf[5]);
17159 pdf->u_buf[6] = byte_swap_32 (pdf->u_buf[6]);
17160 pdf->u_buf[7] = byte_swap_32 (pdf->u_buf[7]);
17161
17162 pdf->o_buf[0] = byte_swap_32 (pdf->o_buf[0]);
17163 pdf->o_buf[1] = byte_swap_32 (pdf->o_buf[1]);
17164 pdf->o_buf[2] = byte_swap_32 (pdf->o_buf[2]);
17165 pdf->o_buf[3] = byte_swap_32 (pdf->o_buf[3]);
17166 pdf->o_buf[4] = byte_swap_32 (pdf->o_buf[4]);
17167 pdf->o_buf[5] = byte_swap_32 (pdf->o_buf[5]);
17168 pdf->o_buf[6] = byte_swap_32 (pdf->o_buf[6]);
17169 pdf->o_buf[7] = byte_swap_32 (pdf->o_buf[7]);
17170
17171 pdf->rc4key[1] = 0;
17172 pdf->rc4key[0] = 0;
17173
17174 pdf->rc4key[0] |= hex_convert (rc4key_pos[0]) << 28;
17175 pdf->rc4key[0] |= hex_convert (rc4key_pos[1]) << 24;
17176 pdf->rc4key[0] |= hex_convert (rc4key_pos[2]) << 20;
17177 pdf->rc4key[0] |= hex_convert (rc4key_pos[3]) << 16;
17178 pdf->rc4key[0] |= hex_convert (rc4key_pos[4]) << 12;
17179 pdf->rc4key[0] |= hex_convert (rc4key_pos[5]) << 8;
17180 pdf->rc4key[0] |= hex_convert (rc4key_pos[6]) << 4;
17181 pdf->rc4key[0] |= hex_convert (rc4key_pos[7]) << 0;
17182 pdf->rc4key[1] |= hex_convert (rc4key_pos[8]) << 28;
17183 pdf->rc4key[1] |= hex_convert (rc4key_pos[9]) << 24;
17184
17185 pdf->rc4key[0] = byte_swap_32 (pdf->rc4key[0]);
17186 pdf->rc4key[1] = byte_swap_32 (pdf->rc4key[1]);
17187
17188 // we use ID for salt, maybe needs to change, we will see...
17189
17190 salt->salt_buf[0] = pdf->id_buf[0];
17191 salt->salt_buf[1] = pdf->id_buf[1];
17192 salt->salt_buf[2] = pdf->id_buf[2];
17193 salt->salt_buf[3] = pdf->id_buf[3];
17194 salt->salt_buf[4] = pdf->u_buf[0];
17195 salt->salt_buf[5] = pdf->u_buf[1];
17196 salt->salt_buf[6] = pdf->o_buf[0];
17197 salt->salt_buf[7] = pdf->o_buf[1];
17198 salt->salt_len = pdf->id_len + 16;
17199
17200 digest[0] = pdf->rc4key[0];
17201 digest[1] = pdf->rc4key[1];
17202 digest[2] = 0;
17203 digest[3] = 0;
17204
17205 return (PARSER_OK);
17206 }
17207
17208 int pdf14_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
17209 {
17210 if ((input_len < DISPLAY_LEN_MIN_10500) || (input_len > DISPLAY_LEN_MAX_10500)) return (PARSER_GLOBAL_LENGTH);
17211
17212 if ((memcmp (SIGNATURE_PDF, input_buf, 5)) && (memcmp (SIGNATURE_PDF, input_buf, 5))) return (PARSER_SIGNATURE_UNMATCHED);
17213
17214 u32 *digest = (u32 *) hash_buf->digest;
17215
17216 salt_t *salt = hash_buf->salt;
17217
17218 pdf_t *pdf = (pdf_t *) hash_buf->esalt;
17219
17220 /**
17221 * parse line
17222 */
17223
17224 char *V_pos = input_buf + 5;
17225
17226 char *R_pos = strchr (V_pos, '*');
17227
17228 if (R_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17229
17230 u32 V_len = R_pos - V_pos;
17231
17232 R_pos++;
17233
17234 char *bits_pos = strchr (R_pos, '*');
17235
17236 if (bits_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17237
17238 u32 R_len = bits_pos - R_pos;
17239
17240 bits_pos++;
17241
17242 char *P_pos = strchr (bits_pos, '*');
17243
17244 if (P_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17245
17246 u32 bits_len = P_pos - bits_pos;
17247
17248 P_pos++;
17249
17250 char *enc_md_pos = strchr (P_pos, '*');
17251
17252 if (enc_md_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17253
17254 u32 P_len = enc_md_pos - P_pos;
17255
17256 enc_md_pos++;
17257
17258 char *id_len_pos = strchr (enc_md_pos, '*');
17259
17260 if (id_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17261
17262 u32 enc_md_len = id_len_pos - enc_md_pos;
17263
17264 id_len_pos++;
17265
17266 char *id_buf_pos = strchr (id_len_pos, '*');
17267
17268 if (id_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17269
17270 u32 id_len_len = id_buf_pos - id_len_pos;
17271
17272 id_buf_pos++;
17273
17274 char *u_len_pos = strchr (id_buf_pos, '*');
17275
17276 if (u_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17277
17278 u32 id_buf_len = u_len_pos - id_buf_pos;
17279
17280 if ((id_buf_len != 32) && (id_buf_len != 64)) return (PARSER_SALT_LENGTH);
17281
17282 u_len_pos++;
17283
17284 char *u_buf_pos = strchr (u_len_pos, '*');
17285
17286 if (u_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17287
17288 u32 u_len_len = u_buf_pos - u_len_pos;
17289
17290 u_buf_pos++;
17291
17292 char *o_len_pos = strchr (u_buf_pos, '*');
17293
17294 if (o_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17295
17296 u32 u_buf_len = o_len_pos - u_buf_pos;
17297
17298 if (u_buf_len != 64) return (PARSER_SALT_LENGTH);
17299
17300 o_len_pos++;
17301
17302 char *o_buf_pos = strchr (o_len_pos, '*');
17303
17304 if (o_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17305
17306 u32 o_len_len = o_buf_pos - o_len_pos;
17307
17308 o_buf_pos++;
17309
17310 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;
17311
17312 if (o_buf_len != 64) return (PARSER_SALT_LENGTH);
17313
17314 // validate data
17315
17316 const int V = atoi (V_pos);
17317 const int R = atoi (R_pos);
17318 const int P = atoi (P_pos);
17319
17320 int vr_ok = 0;
17321
17322 if ((V == 2) && (R == 3)) vr_ok = 1;
17323 if ((V == 4) && (R == 4)) vr_ok = 1;
17324
17325 if (vr_ok == 0) return (PARSER_SALT_VALUE);
17326
17327 const int id_len = atoi (id_len_pos);
17328 const int u_len = atoi (u_len_pos);
17329 const int o_len = atoi (o_len_pos);
17330
17331 if ((id_len != 16) && (id_len != 32)) return (PARSER_SALT_VALUE);
17332
17333 if (u_len != 32) return (PARSER_SALT_VALUE);
17334 if (o_len != 32) return (PARSER_SALT_VALUE);
17335
17336 const int bits = atoi (bits_pos);
17337
17338 if (bits != 128) return (PARSER_SALT_VALUE);
17339
17340 int enc_md = 1;
17341
17342 if (R >= 4)
17343 {
17344 enc_md = atoi (enc_md_pos);
17345 }
17346
17347 // copy data to esalt
17348
17349 pdf->V = V;
17350 pdf->R = R;
17351 pdf->P = P;
17352
17353 pdf->enc_md = enc_md;
17354
17355 pdf->id_buf[0] = hex_to_u32 ((const u8 *) &id_buf_pos[ 0]);
17356 pdf->id_buf[1] = hex_to_u32 ((const u8 *) &id_buf_pos[ 8]);
17357 pdf->id_buf[2] = hex_to_u32 ((const u8 *) &id_buf_pos[16]);
17358 pdf->id_buf[3] = hex_to_u32 ((const u8 *) &id_buf_pos[24]);
17359
17360 if (id_len == 32)
17361 {
17362 pdf->id_buf[4] = hex_to_u32 ((const u8 *) &id_buf_pos[32]);
17363 pdf->id_buf[5] = hex_to_u32 ((const u8 *) &id_buf_pos[40]);
17364 pdf->id_buf[6] = hex_to_u32 ((const u8 *) &id_buf_pos[48]);
17365 pdf->id_buf[7] = hex_to_u32 ((const u8 *) &id_buf_pos[56]);
17366 }
17367
17368 pdf->id_len = id_len;
17369
17370 pdf->u_buf[0] = hex_to_u32 ((const u8 *) &u_buf_pos[ 0]);
17371 pdf->u_buf[1] = hex_to_u32 ((const u8 *) &u_buf_pos[ 8]);
17372 pdf->u_buf[2] = hex_to_u32 ((const u8 *) &u_buf_pos[16]);
17373 pdf->u_buf[3] = hex_to_u32 ((const u8 *) &u_buf_pos[24]);
17374 pdf->u_buf[4] = hex_to_u32 ((const u8 *) &u_buf_pos[32]);
17375 pdf->u_buf[5] = hex_to_u32 ((const u8 *) &u_buf_pos[40]);
17376 pdf->u_buf[6] = hex_to_u32 ((const u8 *) &u_buf_pos[48]);
17377 pdf->u_buf[7] = hex_to_u32 ((const u8 *) &u_buf_pos[56]);
17378 pdf->u_len = u_len;
17379
17380 pdf->o_buf[0] = hex_to_u32 ((const u8 *) &o_buf_pos[ 0]);
17381 pdf->o_buf[1] = hex_to_u32 ((const u8 *) &o_buf_pos[ 8]);
17382 pdf->o_buf[2] = hex_to_u32 ((const u8 *) &o_buf_pos[16]);
17383 pdf->o_buf[3] = hex_to_u32 ((const u8 *) &o_buf_pos[24]);
17384 pdf->o_buf[4] = hex_to_u32 ((const u8 *) &o_buf_pos[32]);
17385 pdf->o_buf[5] = hex_to_u32 ((const u8 *) &o_buf_pos[40]);
17386 pdf->o_buf[6] = hex_to_u32 ((const u8 *) &o_buf_pos[48]);
17387 pdf->o_buf[7] = hex_to_u32 ((const u8 *) &o_buf_pos[56]);
17388 pdf->o_len = o_len;
17389
17390 pdf->id_buf[0] = byte_swap_32 (pdf->id_buf[0]);
17391 pdf->id_buf[1] = byte_swap_32 (pdf->id_buf[1]);
17392 pdf->id_buf[2] = byte_swap_32 (pdf->id_buf[2]);
17393 pdf->id_buf[3] = byte_swap_32 (pdf->id_buf[3]);
17394
17395 if (id_len == 32)
17396 {
17397 pdf->id_buf[4] = byte_swap_32 (pdf->id_buf[4]);
17398 pdf->id_buf[5] = byte_swap_32 (pdf->id_buf[5]);
17399 pdf->id_buf[6] = byte_swap_32 (pdf->id_buf[6]);
17400 pdf->id_buf[7] = byte_swap_32 (pdf->id_buf[7]);
17401 }
17402
17403 pdf->u_buf[0] = byte_swap_32 (pdf->u_buf[0]);
17404 pdf->u_buf[1] = byte_swap_32 (pdf->u_buf[1]);
17405 pdf->u_buf[2] = byte_swap_32 (pdf->u_buf[2]);
17406 pdf->u_buf[3] = byte_swap_32 (pdf->u_buf[3]);
17407 pdf->u_buf[4] = byte_swap_32 (pdf->u_buf[4]);
17408 pdf->u_buf[5] = byte_swap_32 (pdf->u_buf[5]);
17409 pdf->u_buf[6] = byte_swap_32 (pdf->u_buf[6]);
17410 pdf->u_buf[7] = byte_swap_32 (pdf->u_buf[7]);
17411
17412 pdf->o_buf[0] = byte_swap_32 (pdf->o_buf[0]);
17413 pdf->o_buf[1] = byte_swap_32 (pdf->o_buf[1]);
17414 pdf->o_buf[2] = byte_swap_32 (pdf->o_buf[2]);
17415 pdf->o_buf[3] = byte_swap_32 (pdf->o_buf[3]);
17416 pdf->o_buf[4] = byte_swap_32 (pdf->o_buf[4]);
17417 pdf->o_buf[5] = byte_swap_32 (pdf->o_buf[5]);
17418 pdf->o_buf[6] = byte_swap_32 (pdf->o_buf[6]);
17419 pdf->o_buf[7] = byte_swap_32 (pdf->o_buf[7]);
17420
17421 // precompute rc4 data for later use
17422
17423 uint padding[8] =
17424 {
17425 0x5e4ebf28,
17426 0x418a754e,
17427 0x564e0064,
17428 0x0801faff,
17429 0xb6002e2e,
17430 0x803e68d0,
17431 0xfea90c2f,
17432 0x7a695364
17433 };
17434
17435 // md5
17436
17437 uint salt_pc_block[32] = { 0 };
17438
17439 char *salt_pc_ptr = (char *) salt_pc_block;
17440
17441 memcpy (salt_pc_ptr, padding, 32);
17442 memcpy (salt_pc_ptr + 32, pdf->id_buf, pdf->id_len);
17443
17444 uint salt_pc_digest[4] = { 0 };
17445
17446 md5_complete_no_limit (salt_pc_digest, salt_pc_block, 32 + pdf->id_len);
17447
17448 pdf->rc4data[0] = salt_pc_digest[0];
17449 pdf->rc4data[1] = salt_pc_digest[1];
17450
17451 // we use ID for salt, maybe needs to change, we will see...
17452
17453 salt->salt_buf[0] = pdf->id_buf[0];
17454 salt->salt_buf[1] = pdf->id_buf[1];
17455 salt->salt_buf[2] = pdf->id_buf[2];
17456 salt->salt_buf[3] = pdf->id_buf[3];
17457 salt->salt_buf[4] = pdf->u_buf[0];
17458 salt->salt_buf[5] = pdf->u_buf[1];
17459 salt->salt_buf[6] = pdf->o_buf[0];
17460 salt->salt_buf[7] = pdf->o_buf[1];
17461 salt->salt_len = pdf->id_len + 16;
17462
17463 salt->salt_iter = ROUNDS_PDF14;
17464
17465 digest[0] = pdf->u_buf[0];
17466 digest[1] = pdf->u_buf[1];
17467 digest[2] = 0;
17468 digest[3] = 0;
17469
17470 return (PARSER_OK);
17471 }
17472
17473 int pdf17l3_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
17474 {
17475 int ret = pdf17l8_parse_hash (input_buf, input_len, hash_buf);
17476
17477 if (ret != PARSER_OK)
17478 {
17479 return ret;
17480 }
17481
17482 u32 *digest = (u32 *) hash_buf->digest;
17483
17484 salt_t *salt = hash_buf->salt;
17485
17486 digest[0] -= SHA256M_A;
17487 digest[1] -= SHA256M_B;
17488 digest[2] -= SHA256M_C;
17489 digest[3] -= SHA256M_D;
17490 digest[4] -= SHA256M_E;
17491 digest[5] -= SHA256M_F;
17492 digest[6] -= SHA256M_G;
17493 digest[7] -= SHA256M_H;
17494
17495 salt->salt_buf[2] = 0x80;
17496
17497 return (PARSER_OK);
17498 }
17499
17500 int pdf17l8_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
17501 {
17502 if ((input_len < DISPLAY_LEN_MIN_10600) || (input_len > DISPLAY_LEN_MAX_10600)) return (PARSER_GLOBAL_LENGTH);
17503
17504 if ((memcmp (SIGNATURE_PDF, input_buf, 5)) && (memcmp (SIGNATURE_PDF, input_buf, 5))) return (PARSER_SIGNATURE_UNMATCHED);
17505
17506 u32 *digest = (u32 *) hash_buf->digest;
17507
17508 salt_t *salt = hash_buf->salt;
17509
17510 pdf_t *pdf = (pdf_t *) hash_buf->esalt;
17511
17512 /**
17513 * parse line
17514 */
17515
17516 char *V_pos = input_buf + 5;
17517
17518 char *R_pos = strchr (V_pos, '*');
17519
17520 if (R_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17521
17522 u32 V_len = R_pos - V_pos;
17523
17524 R_pos++;
17525
17526 char *bits_pos = strchr (R_pos, '*');
17527
17528 if (bits_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17529
17530 u32 R_len = bits_pos - R_pos;
17531
17532 bits_pos++;
17533
17534 char *P_pos = strchr (bits_pos, '*');
17535
17536 if (P_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17537
17538 u32 bits_len = P_pos - bits_pos;
17539
17540 P_pos++;
17541
17542 char *enc_md_pos = strchr (P_pos, '*');
17543
17544 if (enc_md_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17545
17546 u32 P_len = enc_md_pos - P_pos;
17547
17548 enc_md_pos++;
17549
17550 char *id_len_pos = strchr (enc_md_pos, '*');
17551
17552 if (id_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17553
17554 u32 enc_md_len = id_len_pos - enc_md_pos;
17555
17556 id_len_pos++;
17557
17558 char *id_buf_pos = strchr (id_len_pos, '*');
17559
17560 if (id_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17561
17562 u32 id_len_len = id_buf_pos - id_len_pos;
17563
17564 id_buf_pos++;
17565
17566 char *u_len_pos = strchr (id_buf_pos, '*');
17567
17568 if (u_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17569
17570 u32 id_buf_len = u_len_pos - id_buf_pos;
17571
17572 u_len_pos++;
17573
17574 char *u_buf_pos = strchr (u_len_pos, '*');
17575
17576 if (u_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17577
17578 u32 u_len_len = u_buf_pos - u_len_pos;
17579
17580 u_buf_pos++;
17581
17582 char *o_len_pos = strchr (u_buf_pos, '*');
17583
17584 if (o_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17585
17586 u32 u_buf_len = o_len_pos - u_buf_pos;
17587
17588 o_len_pos++;
17589
17590 char *o_buf_pos = strchr (o_len_pos, '*');
17591
17592 if (o_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17593
17594 u32 o_len_len = o_buf_pos - o_len_pos;
17595
17596 o_buf_pos++;
17597
17598 char *last = strchr (o_buf_pos, '*');
17599
17600 if (last == NULL) last = input_buf + input_len;
17601
17602 u32 o_buf_len = last - o_buf_pos;
17603
17604 // validate data
17605
17606 const int V = atoi (V_pos);
17607 const int R = atoi (R_pos);
17608
17609 int vr_ok = 0;
17610
17611 if ((V == 5) && (R == 5)) vr_ok = 1;
17612 if ((V == 5) && (R == 6)) vr_ok = 1;
17613
17614 if (vr_ok == 0) return (PARSER_SALT_VALUE);
17615
17616 const int bits = atoi (bits_pos);
17617
17618 if (bits != 256) return (PARSER_SALT_VALUE);
17619
17620 int enc_md = atoi (enc_md_pos);
17621
17622 if (enc_md != 1) return (PARSER_SALT_VALUE);
17623
17624 const uint id_len = atoi (id_len_pos);
17625 const uint u_len = atoi (u_len_pos);
17626 const uint o_len = atoi (o_len_pos);
17627
17628 if (V_len > 6) return (PARSER_SALT_LENGTH);
17629 if (R_len > 6) return (PARSER_SALT_LENGTH);
17630 if (P_len > 6) return (PARSER_SALT_LENGTH);
17631 if (id_len_len > 6) return (PARSER_SALT_LENGTH);
17632 if (u_len_len > 6) return (PARSER_SALT_LENGTH);
17633 if (o_len_len > 6) return (PARSER_SALT_LENGTH);
17634 if (bits_len > 6) return (PARSER_SALT_LENGTH);
17635 if (enc_md_len > 6) return (PARSER_SALT_LENGTH);
17636
17637 if ((id_len * 2) != id_buf_len) return (PARSER_SALT_VALUE);
17638 if ((u_len * 2) != u_buf_len) return (PARSER_SALT_VALUE);
17639 if ((o_len * 2) != o_buf_len) return (PARSER_SALT_VALUE);
17640
17641 // copy data to esalt
17642
17643 if (u_len < 40) return (PARSER_SALT_VALUE);
17644
17645 for (int i = 0, j = 0; i < 8 + 2; i += 1, j += 8)
17646 {
17647 pdf->u_buf[i] = hex_to_u32 ((const u8 *) &u_buf_pos[j]);
17648 }
17649
17650 salt->salt_buf[0] = pdf->u_buf[8];
17651 salt->salt_buf[1] = pdf->u_buf[9];
17652
17653 salt->salt_buf[0] = byte_swap_32 (salt->salt_buf[0]);
17654 salt->salt_buf[1] = byte_swap_32 (salt->salt_buf[1]);
17655
17656 salt->salt_len = 8;
17657 salt->salt_iter = ROUNDS_PDF17L8;
17658
17659 digest[0] = pdf->u_buf[0];
17660 digest[1] = pdf->u_buf[1];
17661 digest[2] = pdf->u_buf[2];
17662 digest[3] = pdf->u_buf[3];
17663 digest[4] = pdf->u_buf[4];
17664 digest[5] = pdf->u_buf[5];
17665 digest[6] = pdf->u_buf[6];
17666 digest[7] = pdf->u_buf[7];
17667
17668 return (PARSER_OK);
17669 }
17670
17671 int pbkdf2_sha256_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
17672 {
17673 if ((input_len < DISPLAY_LEN_MIN_10900) || (input_len > DISPLAY_LEN_MAX_10900)) return (PARSER_GLOBAL_LENGTH);
17674
17675 if (memcmp (SIGNATURE_PBKDF2_SHA256, input_buf, 7)) return (PARSER_SIGNATURE_UNMATCHED);
17676
17677 u32 *digest = (u32 *) hash_buf->digest;
17678
17679 salt_t *salt = hash_buf->salt;
17680
17681 pbkdf2_sha256_t *pbkdf2_sha256 = (pbkdf2_sha256_t *) hash_buf->esalt;
17682
17683 /**
17684 * parse line
17685 */
17686
17687 // iterations
17688
17689 char *iter_pos = input_buf + 7;
17690
17691 u32 iter = atoi (iter_pos);
17692
17693 if (iter < 1) return (PARSER_SALT_ITERATION);
17694 if (iter > 999999) return (PARSER_SALT_ITERATION);
17695
17696 // first is *raw* salt
17697
17698 char *salt_pos = strchr (iter_pos, ':');
17699
17700 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17701
17702 salt_pos++;
17703
17704 char *hash_pos = strchr (salt_pos, ':');
17705
17706 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17707
17708 u32 salt_len = hash_pos - salt_pos;
17709
17710 if (salt_len > 64) return (PARSER_SALT_LENGTH);
17711
17712 hash_pos++;
17713
17714 u32 hash_b64_len = input_len - (hash_pos - input_buf);
17715
17716 if (hash_b64_len > 88) return (PARSER_HASH_LENGTH);
17717
17718 // decode salt
17719
17720 char *salt_buf_ptr = (char *) pbkdf2_sha256->salt_buf;
17721
17722 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
17723
17724 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
17725
17726 salt_buf_ptr[salt_len + 3] = 0x01;
17727 salt_buf_ptr[salt_len + 4] = 0x80;
17728
17729 salt->salt_len = salt_len;
17730 salt->salt_iter = iter - 1;
17731
17732 // decode hash
17733
17734 u8 tmp_buf[100] = { 0 };
17735
17736 int hash_len = base64_decode (base64_to_int, (const u8 *) hash_pos, hash_b64_len, tmp_buf);
17737
17738 if (hash_len < 16) return (PARSER_HASH_LENGTH);
17739
17740 memcpy (digest, tmp_buf, 16);
17741
17742 digest[0] = byte_swap_32 (digest[0]);
17743 digest[1] = byte_swap_32 (digest[1]);
17744 digest[2] = byte_swap_32 (digest[2]);
17745 digest[3] = byte_swap_32 (digest[3]);
17746
17747 // add some stuff to normal salt to make sorted happy
17748
17749 salt->salt_buf[0] = pbkdf2_sha256->salt_buf[0];
17750 salt->salt_buf[1] = pbkdf2_sha256->salt_buf[1];
17751 salt->salt_buf[2] = pbkdf2_sha256->salt_buf[2];
17752 salt->salt_buf[3] = pbkdf2_sha256->salt_buf[3];
17753 salt->salt_buf[4] = salt->salt_iter;
17754
17755 return (PARSER_OK);
17756 }
17757
17758 int prestashop_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
17759 {
17760 if ((input_len < DISPLAY_LEN_MIN_11000) || (input_len > DISPLAY_LEN_MAX_11000)) return (PARSER_GLOBAL_LENGTH);
17761
17762 u32 *digest = (u32 *) hash_buf->digest;
17763
17764 salt_t *salt = hash_buf->salt;
17765
17766 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
17767 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
17768 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
17769 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
17770
17771 digest[0] = byte_swap_32 (digest[0]);
17772 digest[1] = byte_swap_32 (digest[1]);
17773 digest[2] = byte_swap_32 (digest[2]);
17774 digest[3] = byte_swap_32 (digest[3]);
17775
17776 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
17777
17778 uint salt_len = input_len - 32 - 1;
17779
17780 char *salt_buf = input_buf + 32 + 1;
17781
17782 char *salt_buf_ptr = (char *) salt->salt_buf;
17783
17784 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
17785
17786 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
17787
17788 salt->salt_len = salt_len;
17789
17790 return (PARSER_OK);
17791 }
17792
17793 int postgresql_auth_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
17794 {
17795 if ((input_len < DISPLAY_LEN_MIN_11100) || (input_len > DISPLAY_LEN_MAX_11100)) return (PARSER_GLOBAL_LENGTH);
17796
17797 if (memcmp (SIGNATURE_POSTGRESQL_AUTH, input_buf, 10)) return (PARSER_SIGNATURE_UNMATCHED);
17798
17799 u32 *digest = (u32 *) hash_buf->digest;
17800
17801 salt_t *salt = hash_buf->salt;
17802
17803 char *user_pos = input_buf + 10;
17804
17805 char *salt_pos = strchr (user_pos, '*');
17806
17807 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17808
17809 salt_pos++;
17810
17811 char *hash_pos = strchr (salt_pos, '*');
17812
17813 hash_pos++;
17814
17815 uint hash_len = input_len - (hash_pos - input_buf);
17816
17817 if (hash_len != 32) return (PARSER_HASH_LENGTH);
17818
17819 uint user_len = salt_pos - user_pos - 1;
17820
17821 uint salt_len = hash_pos - salt_pos - 1;
17822
17823 if (salt_len != 8) return (PARSER_SALT_LENGTH);
17824
17825 /*
17826 * store digest
17827 */
17828
17829 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
17830 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
17831 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
17832 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
17833
17834 digest[0] = byte_swap_32 (digest[0]);
17835 digest[1] = byte_swap_32 (digest[1]);
17836 digest[2] = byte_swap_32 (digest[2]);
17837 digest[3] = byte_swap_32 (digest[3]);
17838
17839 digest[0] -= MD5M_A;
17840 digest[1] -= MD5M_B;
17841 digest[2] -= MD5M_C;
17842 digest[3] -= MD5M_D;
17843
17844 /*
17845 * store salt
17846 */
17847
17848 char *salt_buf_ptr = (char *) salt->salt_buf;
17849
17850 // first 4 bytes are the "challenge"
17851
17852 salt_buf_ptr[0] = hex_to_u8 ((const u8 *) &salt_pos[0]);
17853 salt_buf_ptr[1] = hex_to_u8 ((const u8 *) &salt_pos[2]);
17854 salt_buf_ptr[2] = hex_to_u8 ((const u8 *) &salt_pos[4]);
17855 salt_buf_ptr[3] = hex_to_u8 ((const u8 *) &salt_pos[6]);
17856
17857 // append the user name
17858
17859 user_len = parse_and_store_salt (salt_buf_ptr + 4, user_pos, user_len);
17860
17861 salt->salt_len = 4 + user_len;
17862
17863 return (PARSER_OK);
17864 }
17865
17866 int mysql_auth_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
17867 {
17868 if ((input_len < DISPLAY_LEN_MIN_11200) || (input_len > DISPLAY_LEN_MAX_11200)) return (PARSER_GLOBAL_LENGTH);
17869
17870 if (memcmp (SIGNATURE_MYSQL_AUTH, input_buf, 9)) return (PARSER_SIGNATURE_UNMATCHED);
17871
17872 u32 *digest = (u32 *) hash_buf->digest;
17873
17874 salt_t *salt = hash_buf->salt;
17875
17876 char *salt_pos = input_buf + 9;
17877
17878 char *hash_pos = strchr (salt_pos, '*');
17879
17880 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17881
17882 hash_pos++;
17883
17884 uint hash_len = input_len - (hash_pos - input_buf);
17885
17886 if (hash_len != 40) return (PARSER_HASH_LENGTH);
17887
17888 uint salt_len = hash_pos - salt_pos - 1;
17889
17890 if (salt_len != 40) return (PARSER_SALT_LENGTH);
17891
17892 /*
17893 * store digest
17894 */
17895
17896 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
17897 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
17898 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
17899 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
17900 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
17901
17902 /*
17903 * store salt
17904 */
17905
17906 char *salt_buf_ptr = (char *) salt->salt_buf;
17907
17908 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
17909
17910 salt->salt_len = salt_len;
17911
17912 return (PARSER_OK);
17913 }
17914
17915 int bitcoin_wallet_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
17916 {
17917 if ((input_len < DISPLAY_LEN_MIN_11300) || (input_len > DISPLAY_LEN_MAX_11300)) return (PARSER_GLOBAL_LENGTH);
17918
17919 if (memcmp (SIGNATURE_BITCOIN_WALLET, input_buf, 9)) return (PARSER_SIGNATURE_UNMATCHED);
17920
17921 u32 *digest = (u32 *) hash_buf->digest;
17922
17923 salt_t *salt = hash_buf->salt;
17924
17925 bitcoin_wallet_t *bitcoin_wallet = (bitcoin_wallet_t *) hash_buf->esalt;
17926
17927 /**
17928 * parse line
17929 */
17930
17931 char *cry_master_len_pos = input_buf + 9;
17932
17933 char *cry_master_buf_pos = strchr (cry_master_len_pos, '$');
17934
17935 if (cry_master_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17936
17937 u32 cry_master_len_len = cry_master_buf_pos - cry_master_len_pos;
17938
17939 cry_master_buf_pos++;
17940
17941 char *cry_salt_len_pos = strchr (cry_master_buf_pos, '$');
17942
17943 if (cry_salt_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17944
17945 u32 cry_master_buf_len = cry_salt_len_pos - cry_master_buf_pos;
17946
17947 cry_salt_len_pos++;
17948
17949 char *cry_salt_buf_pos = strchr (cry_salt_len_pos, '$');
17950
17951 if (cry_salt_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17952
17953 u32 cry_salt_len_len = cry_salt_buf_pos - cry_salt_len_pos;
17954
17955 cry_salt_buf_pos++;
17956
17957 char *cry_rounds_pos = strchr (cry_salt_buf_pos, '$');
17958
17959 if (cry_rounds_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17960
17961 u32 cry_salt_buf_len = cry_rounds_pos - cry_salt_buf_pos;
17962
17963 cry_rounds_pos++;
17964
17965 char *ckey_len_pos = strchr (cry_rounds_pos, '$');
17966
17967 if (ckey_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17968
17969 u32 cry_rounds_len = ckey_len_pos - cry_rounds_pos;
17970
17971 ckey_len_pos++;
17972
17973 char *ckey_buf_pos = strchr (ckey_len_pos, '$');
17974
17975 if (ckey_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17976
17977 u32 ckey_len_len = ckey_buf_pos - ckey_len_pos;
17978
17979 ckey_buf_pos++;
17980
17981 char *public_key_len_pos = strchr (ckey_buf_pos, '$');
17982
17983 if (public_key_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17984
17985 u32 ckey_buf_len = public_key_len_pos - ckey_buf_pos;
17986
17987 public_key_len_pos++;
17988
17989 char *public_key_buf_pos = strchr (public_key_len_pos, '$');
17990
17991 if (public_key_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17992
17993 u32 public_key_len_len = public_key_buf_pos - public_key_len_pos;
17994
17995 public_key_buf_pos++;
17996
17997 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;
17998
17999 const uint cry_master_len = atoi (cry_master_len_pos);
18000 const uint cry_salt_len = atoi (cry_salt_len_pos);
18001 const uint ckey_len = atoi (ckey_len_pos);
18002 const uint public_key_len = atoi (public_key_len_pos);
18003
18004 if (cry_master_buf_len != cry_master_len) return (PARSER_SALT_VALUE);
18005 if (cry_salt_buf_len != cry_salt_len) return (PARSER_SALT_VALUE);
18006 if (ckey_buf_len != ckey_len) return (PARSER_SALT_VALUE);
18007 if (public_key_buf_len != public_key_len) return (PARSER_SALT_VALUE);
18008
18009 for (uint i = 0, j = 0; j < cry_master_len; i += 1, j += 8)
18010 {
18011 bitcoin_wallet->cry_master_buf[i] = hex_to_u32 ((const u8 *) &cry_master_buf_pos[j]);
18012
18013 bitcoin_wallet->cry_master_buf[i] = byte_swap_32 (bitcoin_wallet->cry_master_buf[i]);
18014 }
18015
18016 for (uint i = 0, j = 0; j < ckey_len; i += 1, j += 8)
18017 {
18018 bitcoin_wallet->ckey_buf[i] = hex_to_u32 ((const u8 *) &ckey_buf_pos[j]);
18019
18020 bitcoin_wallet->ckey_buf[i] = byte_swap_32 (bitcoin_wallet->ckey_buf[i]);
18021 }
18022
18023 for (uint i = 0, j = 0; j < public_key_len; i += 1, j += 8)
18024 {
18025 bitcoin_wallet->public_key_buf[i] = hex_to_u32 ((const u8 *) &public_key_buf_pos[j]);
18026
18027 bitcoin_wallet->public_key_buf[i] = byte_swap_32 (bitcoin_wallet->public_key_buf[i]);
18028 }
18029
18030 bitcoin_wallet->cry_master_len = cry_master_len / 2;
18031 bitcoin_wallet->ckey_len = ckey_len / 2;
18032 bitcoin_wallet->public_key_len = public_key_len / 2;
18033
18034 /*
18035 * store digest (should be unique enought, hopefully)
18036 */
18037
18038 digest[0] = bitcoin_wallet->cry_master_buf[0];
18039 digest[1] = bitcoin_wallet->cry_master_buf[1];
18040 digest[2] = bitcoin_wallet->cry_master_buf[2];
18041 digest[3] = bitcoin_wallet->cry_master_buf[3];
18042
18043 /*
18044 * store salt
18045 */
18046
18047 if (cry_rounds_len >= 7) return (PARSER_SALT_VALUE);
18048
18049 const uint cry_rounds = atoi (cry_rounds_pos);
18050
18051 salt->salt_iter = cry_rounds - 1;
18052
18053 char *salt_buf_ptr = (char *) salt->salt_buf;
18054
18055 const uint salt_len = parse_and_store_salt (salt_buf_ptr, cry_salt_buf_pos, cry_salt_buf_len);
18056
18057 salt->salt_len = salt_len;
18058
18059 return (PARSER_OK);
18060 }
18061
18062 int sip_auth_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18063 {
18064 if ((input_len < DISPLAY_LEN_MIN_11400) || (input_len > DISPLAY_LEN_MAX_11400)) return (PARSER_GLOBAL_LENGTH);
18065
18066 if (memcmp (SIGNATURE_SIP_AUTH, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
18067
18068 u32 *digest = (u32 *) hash_buf->digest;
18069
18070 salt_t *salt = hash_buf->salt;
18071
18072 sip_t *sip = (sip_t *) hash_buf->esalt;
18073
18074 // work with a temporary copy of input_buf (s.t. we can manipulate it directly)
18075
18076 char *temp_input_buf = (char *) mymalloc (input_len + 1);
18077
18078 memcpy (temp_input_buf, input_buf, input_len);
18079
18080 // URI_server:
18081
18082 char *URI_server_pos = temp_input_buf + 6;
18083
18084 char *URI_client_pos = strchr (URI_server_pos, '*');
18085
18086 if (URI_client_pos == NULL)
18087 {
18088 myfree (temp_input_buf);
18089
18090 return (PARSER_SEPARATOR_UNMATCHED);
18091 }
18092
18093 URI_client_pos[0] = 0;
18094 URI_client_pos++;
18095
18096 uint URI_server_len = strlen (URI_server_pos);
18097
18098 if (URI_server_len > 512)
18099 {
18100 myfree (temp_input_buf);
18101
18102 return (PARSER_SALT_LENGTH);
18103 }
18104
18105 // URI_client:
18106
18107 char *user_pos = strchr (URI_client_pos, '*');
18108
18109 if (user_pos == NULL)
18110 {
18111 myfree (temp_input_buf);
18112
18113 return (PARSER_SEPARATOR_UNMATCHED);
18114 }
18115
18116 user_pos[0] = 0;
18117 user_pos++;
18118
18119 uint URI_client_len = strlen (URI_client_pos);
18120
18121 if (URI_client_len > 512)
18122 {
18123 myfree (temp_input_buf);
18124
18125 return (PARSER_SALT_LENGTH);
18126 }
18127
18128 // user:
18129
18130 char *realm_pos = strchr (user_pos, '*');
18131
18132 if (realm_pos == NULL)
18133 {
18134 myfree (temp_input_buf);
18135
18136 return (PARSER_SEPARATOR_UNMATCHED);
18137 }
18138
18139 realm_pos[0] = 0;
18140 realm_pos++;
18141
18142 uint user_len = strlen (user_pos);
18143
18144 if (user_len > 116)
18145 {
18146 myfree (temp_input_buf);
18147
18148 return (PARSER_SALT_LENGTH);
18149 }
18150
18151 // realm:
18152
18153 char *method_pos = strchr (realm_pos, '*');
18154
18155 if (method_pos == NULL)
18156 {
18157 myfree (temp_input_buf);
18158
18159 return (PARSER_SEPARATOR_UNMATCHED);
18160 }
18161
18162 method_pos[0] = 0;
18163 method_pos++;
18164
18165 uint realm_len = strlen (realm_pos);
18166
18167 if (realm_len > 116)
18168 {
18169 myfree (temp_input_buf);
18170
18171 return (PARSER_SALT_LENGTH);
18172 }
18173
18174 // method:
18175
18176 char *URI_prefix_pos = strchr (method_pos, '*');
18177
18178 if (URI_prefix_pos == NULL)
18179 {
18180 myfree (temp_input_buf);
18181
18182 return (PARSER_SEPARATOR_UNMATCHED);
18183 }
18184
18185 URI_prefix_pos[0] = 0;
18186 URI_prefix_pos++;
18187
18188 uint method_len = strlen (method_pos);
18189
18190 if (method_len > 246)
18191 {
18192 myfree (temp_input_buf);
18193
18194 return (PARSER_SALT_LENGTH);
18195 }
18196
18197 // URI_prefix:
18198
18199 char *URI_resource_pos = strchr (URI_prefix_pos, '*');
18200
18201 if (URI_resource_pos == NULL)
18202 {
18203 myfree (temp_input_buf);
18204
18205 return (PARSER_SEPARATOR_UNMATCHED);
18206 }
18207
18208 URI_resource_pos[0] = 0;
18209 URI_resource_pos++;
18210
18211 uint URI_prefix_len = strlen (URI_prefix_pos);
18212
18213 if (URI_prefix_len > 245)
18214 {
18215 myfree (temp_input_buf);
18216
18217 return (PARSER_SALT_LENGTH);
18218 }
18219
18220 // URI_resource:
18221
18222 char *URI_suffix_pos = strchr (URI_resource_pos, '*');
18223
18224 if (URI_suffix_pos == NULL)
18225 {
18226 myfree (temp_input_buf);
18227
18228 return (PARSER_SEPARATOR_UNMATCHED);
18229 }
18230
18231 URI_suffix_pos[0] = 0;
18232 URI_suffix_pos++;
18233
18234 uint URI_resource_len = strlen (URI_resource_pos);
18235
18236 if (URI_resource_len < 1 || URI_resource_len > 246)
18237 {
18238 myfree (temp_input_buf);
18239
18240 return (PARSER_SALT_LENGTH);
18241 }
18242
18243 // URI_suffix:
18244
18245 char *nonce_pos = strchr (URI_suffix_pos, '*');
18246
18247 if (nonce_pos == NULL)
18248 {
18249 myfree (temp_input_buf);
18250
18251 return (PARSER_SEPARATOR_UNMATCHED);
18252 }
18253
18254 nonce_pos[0] = 0;
18255 nonce_pos++;
18256
18257 uint URI_suffix_len = strlen (URI_suffix_pos);
18258
18259 if (URI_suffix_len > 245)
18260 {
18261 myfree (temp_input_buf);
18262
18263 return (PARSER_SALT_LENGTH);
18264 }
18265
18266 // nonce:
18267
18268 char *nonce_client_pos = strchr (nonce_pos, '*');
18269
18270 if (nonce_client_pos == NULL)
18271 {
18272 myfree (temp_input_buf);
18273
18274 return (PARSER_SEPARATOR_UNMATCHED);
18275 }
18276
18277 nonce_client_pos[0] = 0;
18278 nonce_client_pos++;
18279
18280 uint nonce_len = strlen (nonce_pos);
18281
18282 if (nonce_len < 1 || nonce_len > 50)
18283 {
18284 myfree (temp_input_buf);
18285
18286 return (PARSER_SALT_LENGTH);
18287 }
18288
18289 // nonce_client:
18290
18291 char *nonce_count_pos = strchr (nonce_client_pos, '*');
18292
18293 if (nonce_count_pos == NULL)
18294 {
18295 myfree (temp_input_buf);
18296
18297 return (PARSER_SEPARATOR_UNMATCHED);
18298 }
18299
18300 nonce_count_pos[0] = 0;
18301 nonce_count_pos++;
18302
18303 uint nonce_client_len = strlen (nonce_client_pos);
18304
18305 if (nonce_client_len > 50)
18306 {
18307 myfree (temp_input_buf);
18308
18309 return (PARSER_SALT_LENGTH);
18310 }
18311
18312 // nonce_count:
18313
18314 char *qop_pos = strchr (nonce_count_pos, '*');
18315
18316 if (qop_pos == NULL)
18317 {
18318 myfree (temp_input_buf);
18319
18320 return (PARSER_SEPARATOR_UNMATCHED);
18321 }
18322
18323 qop_pos[0] = 0;
18324 qop_pos++;
18325
18326 uint nonce_count_len = strlen (nonce_count_pos);
18327
18328 if (nonce_count_len > 50)
18329 {
18330 myfree (temp_input_buf);
18331
18332 return (PARSER_SALT_LENGTH);
18333 }
18334
18335 // qop:
18336
18337 char *directive_pos = strchr (qop_pos, '*');
18338
18339 if (directive_pos == NULL)
18340 {
18341 myfree (temp_input_buf);
18342
18343 return (PARSER_SEPARATOR_UNMATCHED);
18344 }
18345
18346 directive_pos[0] = 0;
18347 directive_pos++;
18348
18349 uint qop_len = strlen (qop_pos);
18350
18351 if (qop_len > 50)
18352 {
18353 myfree (temp_input_buf);
18354
18355 return (PARSER_SALT_LENGTH);
18356 }
18357
18358 // directive
18359
18360 char *digest_pos = strchr (directive_pos, '*');
18361
18362 if (digest_pos == NULL)
18363 {
18364 myfree (temp_input_buf);
18365
18366 return (PARSER_SEPARATOR_UNMATCHED);
18367 }
18368
18369 digest_pos[0] = 0;
18370 digest_pos++;
18371
18372 uint directive_len = strlen (directive_pos);
18373
18374 if (directive_len != 3)
18375 {
18376 myfree (temp_input_buf);
18377
18378 return (PARSER_SALT_LENGTH);
18379 }
18380
18381 if (memcmp (directive_pos, "MD5", 3))
18382 {
18383 log_info ("ERROR: only the MD5 directive is currently supported\n");
18384
18385 myfree (temp_input_buf);
18386
18387 return (PARSER_SIP_AUTH_DIRECTIVE);
18388 }
18389
18390 /*
18391 * first (pre-)compute: HA2 = md5 ($method . ":" . $uri)
18392 */
18393
18394 uint md5_len = 0;
18395
18396 uint md5_max_len = 4 * 64;
18397
18398 uint md5_remaining_len = md5_max_len;
18399
18400 uint tmp_md5_buf[64] = { 0 };
18401
18402 char *tmp_md5_ptr = (char *) tmp_md5_buf;
18403
18404 snprintf (tmp_md5_ptr, md5_remaining_len, "%s:", method_pos);
18405
18406 md5_len += method_len + 1;
18407 tmp_md5_ptr += method_len + 1;
18408
18409 if (URI_prefix_len > 0)
18410 {
18411 md5_remaining_len = md5_max_len - md5_len;
18412
18413 snprintf (tmp_md5_ptr, md5_remaining_len + 1, "%s:", URI_prefix_pos);
18414
18415 md5_len += URI_prefix_len + 1;
18416 tmp_md5_ptr += URI_prefix_len + 1;
18417 }
18418
18419 md5_remaining_len = md5_max_len - md5_len;
18420
18421 snprintf (tmp_md5_ptr, md5_remaining_len + 1, "%s", URI_resource_pos);
18422
18423 md5_len += URI_resource_len;
18424 tmp_md5_ptr += URI_resource_len;
18425
18426 if (URI_suffix_len > 0)
18427 {
18428 md5_remaining_len = md5_max_len - md5_len;
18429
18430 snprintf (tmp_md5_ptr, md5_remaining_len + 1, ":%s", URI_suffix_pos);
18431
18432 md5_len += 1 + URI_suffix_len;
18433 }
18434
18435 uint tmp_digest[4] = { 0 };
18436
18437 md5_complete_no_limit (tmp_digest, tmp_md5_buf, md5_len);
18438
18439 tmp_digest[0] = byte_swap_32 (tmp_digest[0]);
18440 tmp_digest[1] = byte_swap_32 (tmp_digest[1]);
18441 tmp_digest[2] = byte_swap_32 (tmp_digest[2]);
18442 tmp_digest[3] = byte_swap_32 (tmp_digest[3]);
18443
18444 /*
18445 * esalt
18446 */
18447
18448 char *esalt_buf_ptr = (char *) sip->esalt_buf;
18449
18450 uint esalt_len = 0;
18451
18452 uint max_esalt_len = sizeof (sip->esalt_buf); // 151 = (64 + 64 + 55) - 32, where 32 is the hexadecimal MD5 HA1 hash
18453
18454 // there are 2 possibilities for the esalt:
18455
18456 if ((strcmp (qop_pos, "auth") == 0) || (strcmp (qop_pos, "auth-int") == 0))
18457 {
18458 esalt_len = 1 + nonce_len + 1 + nonce_count_len + 1 + nonce_client_len + 1 + qop_len + 1 + 32;
18459
18460 if (esalt_len > max_esalt_len)
18461 {
18462 myfree (temp_input_buf);
18463
18464 return (PARSER_SALT_LENGTH);
18465 }
18466
18467 snprintf (esalt_buf_ptr, max_esalt_len, ":%s:%s:%s:%s:%08x%08x%08x%08x",
18468 nonce_pos,
18469 nonce_count_pos,
18470 nonce_client_pos,
18471 qop_pos,
18472 tmp_digest[0],
18473 tmp_digest[1],
18474 tmp_digest[2],
18475 tmp_digest[3]);
18476 }
18477 else
18478 {
18479 esalt_len = 1 + nonce_len + 1 + 32;
18480
18481 if (esalt_len > max_esalt_len)
18482 {
18483 myfree (temp_input_buf);
18484
18485 return (PARSER_SALT_LENGTH);
18486 }
18487
18488 snprintf (esalt_buf_ptr, max_esalt_len, ":%s:%08x%08x%08x%08x",
18489 nonce_pos,
18490 tmp_digest[0],
18491 tmp_digest[1],
18492 tmp_digest[2],
18493 tmp_digest[3]);
18494 }
18495
18496 // add 0x80 to esalt
18497
18498 esalt_buf_ptr[esalt_len] = 0x80;
18499
18500 sip->esalt_len = esalt_len;
18501
18502 /*
18503 * actual salt
18504 */
18505
18506 char *sip_salt_ptr = (char *) sip->salt_buf;
18507
18508 uint salt_len = user_len + 1 + realm_len + 1;
18509
18510 uint max_salt_len = 119;
18511
18512 if (salt_len > max_salt_len)
18513 {
18514 myfree (temp_input_buf);
18515
18516 return (PARSER_SALT_LENGTH);
18517 }
18518
18519 snprintf (sip_salt_ptr, max_salt_len + 1, "%s:%s:", user_pos, realm_pos);
18520
18521 sip->salt_len = salt_len;
18522
18523 /*
18524 * fake salt (for sorting)
18525 */
18526
18527 char *salt_buf_ptr = (char *) salt->salt_buf;
18528
18529 max_salt_len = 55;
18530
18531 uint fake_salt_len = salt_len;
18532
18533 if (fake_salt_len > max_salt_len)
18534 {
18535 fake_salt_len = max_salt_len;
18536 }
18537
18538 snprintf (salt_buf_ptr, max_salt_len + 1, "%s:%s:", user_pos, realm_pos);
18539
18540 salt->salt_len = fake_salt_len;
18541
18542 /*
18543 * digest
18544 */
18545
18546 digest[0] = hex_to_u32 ((const u8 *) &digest_pos[ 0]);
18547 digest[1] = hex_to_u32 ((const u8 *) &digest_pos[ 8]);
18548 digest[2] = hex_to_u32 ((const u8 *) &digest_pos[16]);
18549 digest[3] = hex_to_u32 ((const u8 *) &digest_pos[24]);
18550
18551 digest[0] = byte_swap_32 (digest[0]);
18552 digest[1] = byte_swap_32 (digest[1]);
18553 digest[2] = byte_swap_32 (digest[2]);
18554 digest[3] = byte_swap_32 (digest[3]);
18555
18556 myfree (temp_input_buf);
18557
18558 return (PARSER_OK);
18559 }
18560
18561 int crc32_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18562 {
18563 if ((input_len < DISPLAY_LEN_MIN_11500) || (input_len > DISPLAY_LEN_MAX_11500)) return (PARSER_GLOBAL_LENGTH);
18564
18565 if (input_buf[8] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
18566
18567 u32 *digest = (u32 *) hash_buf->digest;
18568
18569 salt_t *salt = hash_buf->salt;
18570
18571 // digest
18572
18573 char *digest_pos = input_buf;
18574
18575 digest[0] = hex_to_u32 ((const u8 *) &digest_pos[0]);
18576 digest[1] = 0;
18577 digest[2] = 0;
18578 digest[3] = 0;
18579
18580 // salt
18581
18582 char *salt_buf = input_buf + 8 + 1;
18583
18584 uint salt_len = 8;
18585
18586 char *salt_buf_ptr = (char *) salt->salt_buf;
18587
18588 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
18589
18590 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
18591
18592 salt->salt_len = salt_len;
18593
18594 return (PARSER_OK);
18595 }
18596
18597 int seven_zip_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18598 {
18599 if ((input_len < DISPLAY_LEN_MIN_11600) || (input_len > DISPLAY_LEN_MAX_11600)) return (PARSER_GLOBAL_LENGTH);
18600
18601 if (memcmp (SIGNATURE_SEVEN_ZIP, input_buf, 4)) return (PARSER_SIGNATURE_UNMATCHED);
18602
18603 u32 *digest = (u32 *) hash_buf->digest;
18604
18605 salt_t *salt = hash_buf->salt;
18606
18607 seven_zip_t *seven_zip = (seven_zip_t *) hash_buf->esalt;
18608
18609 /**
18610 * parse line
18611 */
18612
18613 char *p_buf_pos = input_buf + 4;
18614
18615 char *NumCyclesPower_pos = strchr (p_buf_pos, '$');
18616
18617 if (NumCyclesPower_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18618
18619 u32 p_buf_len = NumCyclesPower_pos - p_buf_pos;
18620
18621 NumCyclesPower_pos++;
18622
18623 char *salt_len_pos = strchr (NumCyclesPower_pos, '$');
18624
18625 if (salt_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18626
18627 u32 NumCyclesPower_len = salt_len_pos - NumCyclesPower_pos;
18628
18629 salt_len_pos++;
18630
18631 char *salt_buf_pos = strchr (salt_len_pos, '$');
18632
18633 if (salt_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18634
18635 u32 salt_len_len = salt_buf_pos - salt_len_pos;
18636
18637 salt_buf_pos++;
18638
18639 char *iv_len_pos = strchr (salt_buf_pos, '$');
18640
18641 if (iv_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18642
18643 u32 salt_buf_len = iv_len_pos - salt_buf_pos;
18644
18645 iv_len_pos++;
18646
18647 char *iv_buf_pos = strchr (iv_len_pos, '$');
18648
18649 if (iv_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18650
18651 u32 iv_len_len = iv_buf_pos - iv_len_pos;
18652
18653 iv_buf_pos++;
18654
18655 char *crc_buf_pos = strchr (iv_buf_pos, '$');
18656
18657 if (crc_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18658
18659 u32 iv_buf_len = crc_buf_pos - iv_buf_pos;
18660
18661 crc_buf_pos++;
18662
18663 char *data_len_pos = strchr (crc_buf_pos, '$');
18664
18665 if (data_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18666
18667 u32 crc_buf_len = data_len_pos - crc_buf_pos;
18668
18669 data_len_pos++;
18670
18671 char *unpack_size_pos = strchr (data_len_pos, '$');
18672
18673 if (unpack_size_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18674
18675 u32 data_len_len = unpack_size_pos - data_len_pos;
18676
18677 unpack_size_pos++;
18678
18679 char *data_buf_pos = strchr (unpack_size_pos, '$');
18680
18681 if (data_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18682
18683 u32 unpack_size_len = data_buf_pos - unpack_size_pos;
18684
18685 data_buf_pos++;
18686
18687 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;
18688
18689 const uint iter = atoi (NumCyclesPower_pos);
18690 const uint crc = atoi (crc_buf_pos);
18691 const uint p_buf = atoi (p_buf_pos);
18692 const uint salt_len = atoi (salt_len_pos);
18693 const uint iv_len = atoi (iv_len_pos);
18694 const uint unpack_size = atoi (unpack_size_pos);
18695 const uint data_len = atoi (data_len_pos);
18696
18697 /**
18698 * verify some data
18699 */
18700
18701 if (p_buf != 0) return (PARSER_SALT_VALUE);
18702 if (salt_len != 0) return (PARSER_SALT_VALUE);
18703
18704 if ((data_len * 2) != data_buf_len) return (PARSER_SALT_VALUE);
18705
18706 if (data_len > 384) return (PARSER_SALT_VALUE);
18707
18708 if (unpack_size > data_len) return (PARSER_SALT_VALUE);
18709
18710 /**
18711 * store data
18712 */
18713
18714 seven_zip->iv_buf[0] = hex_to_u32 ((const u8 *) &iv_buf_pos[ 0]);
18715 seven_zip->iv_buf[1] = hex_to_u32 ((const u8 *) &iv_buf_pos[ 8]);
18716 seven_zip->iv_buf[2] = hex_to_u32 ((const u8 *) &iv_buf_pos[16]);
18717 seven_zip->iv_buf[3] = hex_to_u32 ((const u8 *) &iv_buf_pos[24]);
18718
18719 seven_zip->iv_len = iv_len;
18720
18721 memcpy (seven_zip->salt_buf, salt_buf_pos, salt_buf_len); // we just need that for later ascii_digest()
18722
18723 seven_zip->salt_len = 0;
18724
18725 seven_zip->crc = crc;
18726
18727 for (uint i = 0, j = 0; j < data_buf_len; i += 1, j += 8)
18728 {
18729 seven_zip->data_buf[i] = hex_to_u32 ((const u8 *) &data_buf_pos[j]);
18730
18731 seven_zip->data_buf[i] = byte_swap_32 (seven_zip->data_buf[i]);
18732 }
18733
18734 seven_zip->data_len = data_len;
18735
18736 seven_zip->unpack_size = unpack_size;
18737
18738 // real salt
18739
18740 salt->salt_buf[0] = seven_zip->data_buf[0];
18741 salt->salt_buf[1] = seven_zip->data_buf[1];
18742 salt->salt_buf[2] = seven_zip->data_buf[2];
18743 salt->salt_buf[3] = seven_zip->data_buf[3];
18744
18745 salt->salt_len = 16;
18746
18747 salt->salt_sign[0] = iter;
18748
18749 salt->salt_iter = 1 << iter;
18750
18751 /**
18752 * digest
18753 */
18754
18755 digest[0] = crc;
18756 digest[1] = 0;
18757 digest[2] = 0;
18758 digest[3] = 0;
18759
18760 return (PARSER_OK);
18761 }
18762
18763 int gost2012sbog_256_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18764 {
18765 if ((input_len < DISPLAY_LEN_MIN_11700) || (input_len > DISPLAY_LEN_MAX_11700)) return (PARSER_GLOBAL_LENGTH);
18766
18767 u32 *digest = (u32 *) hash_buf->digest;
18768
18769 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
18770 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
18771 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
18772 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
18773 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
18774 digest[5] = hex_to_u32 ((const u8 *) &input_buf[40]);
18775 digest[6] = hex_to_u32 ((const u8 *) &input_buf[48]);
18776 digest[7] = hex_to_u32 ((const u8 *) &input_buf[56]);
18777
18778 digest[0] = byte_swap_32 (digest[0]);
18779 digest[1] = byte_swap_32 (digest[1]);
18780 digest[2] = byte_swap_32 (digest[2]);
18781 digest[3] = byte_swap_32 (digest[3]);
18782 digest[4] = byte_swap_32 (digest[4]);
18783 digest[5] = byte_swap_32 (digest[5]);
18784 digest[6] = byte_swap_32 (digest[6]);
18785 digest[7] = byte_swap_32 (digest[7]);
18786
18787 return (PARSER_OK);
18788 }
18789
18790 int gost2012sbog_512_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18791 {
18792 if ((input_len < DISPLAY_LEN_MIN_11800) || (input_len > DISPLAY_LEN_MAX_11800)) return (PARSER_GLOBAL_LENGTH);
18793
18794 u32 *digest = (u32 *) hash_buf->digest;
18795
18796 digest[ 0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
18797 digest[ 1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
18798 digest[ 2] = hex_to_u32 ((const u8 *) &input_buf[ 16]);
18799 digest[ 3] = hex_to_u32 ((const u8 *) &input_buf[ 24]);
18800 digest[ 4] = hex_to_u32 ((const u8 *) &input_buf[ 32]);
18801 digest[ 5] = hex_to_u32 ((const u8 *) &input_buf[ 40]);
18802 digest[ 6] = hex_to_u32 ((const u8 *) &input_buf[ 48]);
18803 digest[ 7] = hex_to_u32 ((const u8 *) &input_buf[ 56]);
18804 digest[ 8] = hex_to_u32 ((const u8 *) &input_buf[ 64]);
18805 digest[ 9] = hex_to_u32 ((const u8 *) &input_buf[ 72]);
18806 digest[10] = hex_to_u32 ((const u8 *) &input_buf[ 80]);
18807 digest[11] = hex_to_u32 ((const u8 *) &input_buf[ 88]);
18808 digest[12] = hex_to_u32 ((const u8 *) &input_buf[ 96]);
18809 digest[13] = hex_to_u32 ((const u8 *) &input_buf[104]);
18810 digest[14] = hex_to_u32 ((const u8 *) &input_buf[112]);
18811 digest[15] = hex_to_u32 ((const u8 *) &input_buf[120]);
18812
18813 digest[ 0] = byte_swap_32 (digest[ 0]);
18814 digest[ 1] = byte_swap_32 (digest[ 1]);
18815 digest[ 2] = byte_swap_32 (digest[ 2]);
18816 digest[ 3] = byte_swap_32 (digest[ 3]);
18817 digest[ 4] = byte_swap_32 (digest[ 4]);
18818 digest[ 5] = byte_swap_32 (digest[ 5]);
18819 digest[ 6] = byte_swap_32 (digest[ 6]);
18820 digest[ 7] = byte_swap_32 (digest[ 7]);
18821 digest[ 8] = byte_swap_32 (digest[ 8]);
18822 digest[ 9] = byte_swap_32 (digest[ 9]);
18823 digest[10] = byte_swap_32 (digest[10]);
18824 digest[11] = byte_swap_32 (digest[11]);
18825 digest[12] = byte_swap_32 (digest[12]);
18826 digest[13] = byte_swap_32 (digest[13]);
18827 digest[14] = byte_swap_32 (digest[14]);
18828 digest[15] = byte_swap_32 (digest[15]);
18829
18830 return (PARSER_OK);
18831 }
18832
18833 int pbkdf2_md5_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18834 {
18835 if ((input_len < DISPLAY_LEN_MIN_11900) || (input_len > DISPLAY_LEN_MAX_11900)) return (PARSER_GLOBAL_LENGTH);
18836
18837 if (memcmp (SIGNATURE_PBKDF2_MD5, input_buf, 4)) return (PARSER_SIGNATURE_UNMATCHED);
18838
18839 u32 *digest = (u32 *) hash_buf->digest;
18840
18841 salt_t *salt = hash_buf->salt;
18842
18843 pbkdf2_md5_t *pbkdf2_md5 = (pbkdf2_md5_t *) hash_buf->esalt;
18844
18845 /**
18846 * parse line
18847 */
18848
18849 // iterations
18850
18851 char *iter_pos = input_buf + 4;
18852
18853 u32 iter = atoi (iter_pos);
18854
18855 if (iter < 1) return (PARSER_SALT_ITERATION);
18856 if (iter > 999999) return (PARSER_SALT_ITERATION);
18857
18858 // first is *raw* salt
18859
18860 char *salt_pos = strchr (iter_pos, ':');
18861
18862 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18863
18864 salt_pos++;
18865
18866 char *hash_pos = strchr (salt_pos, ':');
18867
18868 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18869
18870 u32 salt_len = hash_pos - salt_pos;
18871
18872 if (salt_len > 64) return (PARSER_SALT_LENGTH);
18873
18874 hash_pos++;
18875
18876 u32 hash_b64_len = input_len - (hash_pos - input_buf);
18877
18878 if (hash_b64_len > 88) return (PARSER_HASH_LENGTH);
18879
18880 // decode salt
18881
18882 char *salt_buf_ptr = (char *) pbkdf2_md5->salt_buf;
18883
18884 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
18885
18886 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
18887
18888 salt_buf_ptr[salt_len + 3] = 0x01;
18889 salt_buf_ptr[salt_len + 4] = 0x80;
18890
18891 salt->salt_len = salt_len;
18892 salt->salt_iter = iter - 1;
18893
18894 // decode hash
18895
18896 u8 tmp_buf[100] = { 0 };
18897
18898 int hash_len = base64_decode (base64_to_int, (const u8 *) hash_pos, hash_b64_len, tmp_buf);
18899
18900 if (hash_len < 16) return (PARSER_HASH_LENGTH);
18901
18902 memcpy (digest, tmp_buf, 16);
18903
18904 // add some stuff to normal salt to make sorted happy
18905
18906 salt->salt_buf[0] = pbkdf2_md5->salt_buf[0];
18907 salt->salt_buf[1] = pbkdf2_md5->salt_buf[1];
18908 salt->salt_buf[2] = pbkdf2_md5->salt_buf[2];
18909 salt->salt_buf[3] = pbkdf2_md5->salt_buf[3];
18910 salt->salt_buf[4] = salt->salt_iter;
18911
18912 return (PARSER_OK);
18913 }
18914
18915 int pbkdf2_sha1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18916 {
18917 if ((input_len < DISPLAY_LEN_MIN_12000) || (input_len > DISPLAY_LEN_MAX_12000)) return (PARSER_GLOBAL_LENGTH);
18918
18919 if (memcmp (SIGNATURE_PBKDF2_SHA1, input_buf, 5)) return (PARSER_SIGNATURE_UNMATCHED);
18920
18921 u32 *digest = (u32 *) hash_buf->digest;
18922
18923 salt_t *salt = hash_buf->salt;
18924
18925 pbkdf2_sha1_t *pbkdf2_sha1 = (pbkdf2_sha1_t *) hash_buf->esalt;
18926
18927 /**
18928 * parse line
18929 */
18930
18931 // iterations
18932
18933 char *iter_pos = input_buf + 5;
18934
18935 u32 iter = atoi (iter_pos);
18936
18937 if (iter < 1) return (PARSER_SALT_ITERATION);
18938 if (iter > 999999) return (PARSER_SALT_ITERATION);
18939
18940 // first is *raw* salt
18941
18942 char *salt_pos = strchr (iter_pos, ':');
18943
18944 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18945
18946 salt_pos++;
18947
18948 char *hash_pos = strchr (salt_pos, ':');
18949
18950 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18951
18952 u32 salt_len = hash_pos - salt_pos;
18953
18954 if (salt_len > 64) return (PARSER_SALT_LENGTH);
18955
18956 hash_pos++;
18957
18958 u32 hash_b64_len = input_len - (hash_pos - input_buf);
18959
18960 if (hash_b64_len > 88) return (PARSER_HASH_LENGTH);
18961
18962 // decode salt
18963
18964 char *salt_buf_ptr = (char *) pbkdf2_sha1->salt_buf;
18965
18966 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
18967
18968 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
18969
18970 salt_buf_ptr[salt_len + 3] = 0x01;
18971 salt_buf_ptr[salt_len + 4] = 0x80;
18972
18973 salt->salt_len = salt_len;
18974 salt->salt_iter = iter - 1;
18975
18976 // decode hash
18977
18978 u8 tmp_buf[100] = { 0 };
18979
18980 int hash_len = base64_decode (base64_to_int, (const u8 *) hash_pos, hash_b64_len, tmp_buf);
18981
18982 if (hash_len < 16) return (PARSER_HASH_LENGTH);
18983
18984 memcpy (digest, tmp_buf, 16);
18985
18986 digest[0] = byte_swap_32 (digest[0]);
18987 digest[1] = byte_swap_32 (digest[1]);
18988 digest[2] = byte_swap_32 (digest[2]);
18989 digest[3] = byte_swap_32 (digest[3]);
18990
18991 // add some stuff to normal salt to make sorted happy
18992
18993 salt->salt_buf[0] = pbkdf2_sha1->salt_buf[0];
18994 salt->salt_buf[1] = pbkdf2_sha1->salt_buf[1];
18995 salt->salt_buf[2] = pbkdf2_sha1->salt_buf[2];
18996 salt->salt_buf[3] = pbkdf2_sha1->salt_buf[3];
18997 salt->salt_buf[4] = salt->salt_iter;
18998
18999 return (PARSER_OK);
19000 }
19001
19002 int pbkdf2_sha512_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
19003 {
19004 if ((input_len < DISPLAY_LEN_MIN_12100) || (input_len > DISPLAY_LEN_MAX_12100)) return (PARSER_GLOBAL_LENGTH);
19005
19006 if (memcmp (SIGNATURE_PBKDF2_SHA512, input_buf, 7)) return (PARSER_SIGNATURE_UNMATCHED);
19007
19008 u64 *digest = (u64 *) hash_buf->digest;
19009
19010 salt_t *salt = hash_buf->salt;
19011
19012 pbkdf2_sha512_t *pbkdf2_sha512 = (pbkdf2_sha512_t *) hash_buf->esalt;
19013
19014 /**
19015 * parse line
19016 */
19017
19018 // iterations
19019
19020 char *iter_pos = input_buf + 7;
19021
19022 u32 iter = atoi (iter_pos);
19023
19024 if (iter < 1) return (PARSER_SALT_ITERATION);
19025 if (iter > 999999) return (PARSER_SALT_ITERATION);
19026
19027 // first is *raw* salt
19028
19029 char *salt_pos = strchr (iter_pos, ':');
19030
19031 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19032
19033 salt_pos++;
19034
19035 char *hash_pos = strchr (salt_pos, ':');
19036
19037 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19038
19039 u32 salt_len = hash_pos - salt_pos;
19040
19041 if (salt_len > 64) return (PARSER_SALT_LENGTH);
19042
19043 hash_pos++;
19044
19045 u32 hash_b64_len = input_len - (hash_pos - input_buf);
19046
19047 if (hash_b64_len > 88) return (PARSER_HASH_LENGTH);
19048
19049 // decode salt
19050
19051 char *salt_buf_ptr = (char *) pbkdf2_sha512->salt_buf;
19052
19053 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
19054
19055 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
19056
19057 salt_buf_ptr[salt_len + 3] = 0x01;
19058 salt_buf_ptr[salt_len + 4] = 0x80;
19059
19060 salt->salt_len = salt_len;
19061 salt->salt_iter = iter - 1;
19062
19063 // decode hash
19064
19065 u8 tmp_buf[100] = { 0 };
19066
19067 int hash_len = base64_decode (base64_to_int, (const u8 *) hash_pos, hash_b64_len, tmp_buf);
19068
19069 if (hash_len < 16) return (PARSER_HASH_LENGTH);
19070
19071 memcpy (digest, tmp_buf, 64);
19072
19073 digest[0] = byte_swap_64 (digest[0]);
19074 digest[1] = byte_swap_64 (digest[1]);
19075 digest[2] = byte_swap_64 (digest[2]);
19076 digest[3] = byte_swap_64 (digest[3]);
19077 digest[4] = byte_swap_64 (digest[4]);
19078 digest[5] = byte_swap_64 (digest[5]);
19079 digest[6] = byte_swap_64 (digest[6]);
19080 digest[7] = byte_swap_64 (digest[7]);
19081
19082 // add some stuff to normal salt to make sorted happy
19083
19084 salt->salt_buf[0] = pbkdf2_sha512->salt_buf[0];
19085 salt->salt_buf[1] = pbkdf2_sha512->salt_buf[1];
19086 salt->salt_buf[2] = pbkdf2_sha512->salt_buf[2];
19087 salt->salt_buf[3] = pbkdf2_sha512->salt_buf[3];
19088 salt->salt_buf[4] = salt->salt_iter;
19089
19090 return (PARSER_OK);
19091 }
19092
19093 int ecryptfs_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
19094 {
19095 if ((input_len < DISPLAY_LEN_MIN_12200) || (input_len > DISPLAY_LEN_MAX_12200)) return (PARSER_GLOBAL_LENGTH);
19096
19097 if (memcmp (SIGNATURE_ECRYPTFS, input_buf, 10)) return (PARSER_SIGNATURE_UNMATCHED);
19098
19099 uint *digest = (uint *) hash_buf->digest;
19100
19101 salt_t *salt = hash_buf->salt;
19102
19103 /**
19104 * parse line
19105 */
19106
19107 char *salt_pos = input_buf + 10 + 2 + 2; // skip over "0$" and "1$"
19108
19109 char *hash_pos = strchr (salt_pos, '$');
19110
19111 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19112
19113 u32 salt_len = hash_pos - salt_pos;
19114
19115 if (salt_len != 16) return (PARSER_SALT_LENGTH);
19116
19117 hash_pos++;
19118
19119 u32 hash_len = input_len - 10 - 2 - 2 - salt_len - 1;
19120
19121 if (hash_len != 16) return (PARSER_HASH_LENGTH);
19122
19123 // decode hash
19124
19125 digest[ 0] = hex_to_u32 ((const u8 *) &hash_pos[0]);
19126 digest[ 1] = hex_to_u32 ((const u8 *) &hash_pos[8]);
19127 digest[ 2] = 0;
19128 digest[ 3] = 0;
19129 digest[ 4] = 0;
19130 digest[ 5] = 0;
19131 digest[ 6] = 0;
19132 digest[ 7] = 0;
19133 digest[ 8] = 0;
19134 digest[ 9] = 0;
19135 digest[10] = 0;
19136 digest[11] = 0;
19137 digest[12] = 0;
19138 digest[13] = 0;
19139 digest[14] = 0;
19140 digest[15] = 0;
19141
19142 // decode salt
19143
19144 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_pos[0]);
19145 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_pos[8]);
19146
19147 salt->salt_iter = ROUNDS_ECRYPTFS;
19148 salt->salt_len = 8;
19149
19150 return (PARSER_OK);
19151 }
19152
19153 int bsdicrypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
19154 {
19155 if ((input_len < DISPLAY_LEN_MIN_12400) || (input_len > DISPLAY_LEN_MAX_12400)) return (PARSER_GLOBAL_LENGTH);
19156
19157 if (memcmp (SIGNATURE_BSDICRYPT, input_buf, 1)) return (PARSER_SIGNATURE_UNMATCHED);
19158
19159 unsigned char c19 = itoa64_to_int (input_buf[19]);
19160
19161 if (c19 & 3) return (PARSER_HASH_VALUE);
19162
19163 salt_t *salt = hash_buf->salt;
19164
19165 u32 *digest = (u32 *) hash_buf->digest;
19166
19167 // iteration count
19168
19169 salt->salt_iter = itoa64_to_int (input_buf[1])
19170 | itoa64_to_int (input_buf[2]) << 6
19171 | itoa64_to_int (input_buf[3]) << 12
19172 | itoa64_to_int (input_buf[4]) << 18;
19173
19174 // set salt
19175
19176 salt->salt_buf[0] = itoa64_to_int (input_buf[5])
19177 | itoa64_to_int (input_buf[6]) << 6
19178 | itoa64_to_int (input_buf[7]) << 12
19179 | itoa64_to_int (input_buf[8]) << 18;
19180
19181 salt->salt_len = 4;
19182
19183 u8 tmp_buf[100] = { 0 };
19184
19185 base64_decode (itoa64_to_int, (const u8 *) input_buf + 9, 11, tmp_buf);
19186
19187 memcpy (digest, tmp_buf, 8);
19188
19189 uint tt;
19190
19191 IP (digest[0], digest[1], tt);
19192
19193 digest[0] = rotr32 (digest[0], 31);
19194 digest[1] = rotr32 (digest[1], 31);
19195 digest[2] = 0;
19196 digest[3] = 0;
19197
19198 return (PARSER_OK);
19199 }
19200
19201 int rar3hp_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
19202 {
19203 if ((input_len < DISPLAY_LEN_MIN_12500) || (input_len > DISPLAY_LEN_MAX_12500)) return (PARSER_GLOBAL_LENGTH);
19204
19205 if (memcmp (SIGNATURE_RAR3, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
19206
19207 u32 *digest = (u32 *) hash_buf->digest;
19208
19209 salt_t *salt = hash_buf->salt;
19210
19211 /**
19212 * parse line
19213 */
19214
19215 char *type_pos = input_buf + 6 + 1;
19216
19217 char *salt_pos = strchr (type_pos, '*');
19218
19219 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19220
19221 u32 type_len = salt_pos - type_pos;
19222
19223 if (type_len != 1) return (PARSER_SALT_LENGTH);
19224
19225 salt_pos++;
19226
19227 char *crypted_pos = strchr (salt_pos, '*');
19228
19229 if (crypted_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19230
19231 u32 salt_len = crypted_pos - salt_pos;
19232
19233 if (salt_len != 16) return (PARSER_SALT_LENGTH);
19234
19235 crypted_pos++;
19236
19237 u32 crypted_len = input_len - 6 - 1 - type_len - 1 - salt_len - 1;
19238
19239 if (crypted_len != 32) return (PARSER_SALT_LENGTH);
19240
19241 /**
19242 * copy data
19243 */
19244
19245 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_pos[0]);
19246 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_pos[8]);
19247
19248 salt->salt_buf[0] = byte_swap_32 (salt->salt_buf[0]);
19249 salt->salt_buf[1] = byte_swap_32 (salt->salt_buf[1]);
19250
19251 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &crypted_pos[ 0]);
19252 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &crypted_pos[ 8]);
19253 salt->salt_buf[4] = hex_to_u32 ((const u8 *) &crypted_pos[16]);
19254 salt->salt_buf[5] = hex_to_u32 ((const u8 *) &crypted_pos[24]);
19255
19256 salt->salt_len = 24;
19257 salt->salt_iter = ROUNDS_RAR3;
19258
19259 // there's no hash for rar3. the data which is in crypted_pos is some encrypted data and
19260 // if it matches the value \xc4\x3d\x7b\x00\x40\x07\x00 after decrypt we know that we successfully cracked it.
19261
19262 digest[0] = 0xc43d7b00;
19263 digest[1] = 0x40070000;
19264 digest[2] = 0;
19265 digest[3] = 0;
19266
19267 return (PARSER_OK);
19268 }
19269
19270 int rar5_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
19271 {
19272 if ((input_len < DISPLAY_LEN_MIN_13000) || (input_len > DISPLAY_LEN_MAX_13000)) return (PARSER_GLOBAL_LENGTH);
19273
19274 if (memcmp (SIGNATURE_RAR5, input_buf, 1 + 4 + 1)) return (PARSER_SIGNATURE_UNMATCHED);
19275
19276 u32 *digest = (u32 *) hash_buf->digest;
19277
19278 salt_t *salt = hash_buf->salt;
19279
19280 rar5_t *rar5 = (rar5_t *) hash_buf->esalt;
19281
19282 /**
19283 * parse line
19284 */
19285
19286 char *param0_pos = input_buf + 1 + 4 + 1;
19287
19288 char *param1_pos = strchr (param0_pos, '$');
19289
19290 if (param1_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19291
19292 u32 param0_len = param1_pos - param0_pos;
19293
19294 param1_pos++;
19295
19296 char *param2_pos = strchr (param1_pos, '$');
19297
19298 if (param2_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19299
19300 u32 param1_len = param2_pos - param1_pos;
19301
19302 param2_pos++;
19303
19304 char *param3_pos = strchr (param2_pos, '$');
19305
19306 if (param3_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19307
19308 u32 param2_len = param3_pos - param2_pos;
19309
19310 param3_pos++;
19311
19312 char *param4_pos = strchr (param3_pos, '$');
19313
19314 if (param4_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19315
19316 u32 param3_len = param4_pos - param3_pos;
19317
19318 param4_pos++;
19319
19320 char *param5_pos = strchr (param4_pos, '$');
19321
19322 if (param5_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19323
19324 u32 param4_len = param5_pos - param4_pos;
19325
19326 param5_pos++;
19327
19328 u32 param5_len = input_len - 1 - 4 - 1 - param0_len - 1 - param1_len - 1 - param2_len - 1 - param3_len - 1 - param4_len - 1;
19329
19330 char *salt_buf = param1_pos;
19331 char *iv = param3_pos;
19332 char *pswcheck = param5_pos;
19333
19334 const uint salt_len = atoi (param0_pos);
19335 const uint iterations = atoi (param2_pos);
19336 const uint pswcheck_len = atoi (param4_pos);
19337
19338 /**
19339 * verify some data
19340 */
19341
19342 if (param1_len != 32) return (PARSER_SALT_VALUE);
19343 if (param3_len != 32) return (PARSER_SALT_VALUE);
19344 if (param5_len != 16) return (PARSER_SALT_VALUE);
19345
19346 if (salt_len != 16) return (PARSER_SALT_VALUE);
19347 if (iterations == 0) return (PARSER_SALT_VALUE);
19348 if (pswcheck_len != 8) return (PARSER_SALT_VALUE);
19349
19350 /**
19351 * store data
19352 */
19353
19354 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_buf[ 0]);
19355 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_buf[ 8]);
19356 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &salt_buf[16]);
19357 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &salt_buf[24]);
19358
19359 rar5->iv[0] = hex_to_u32 ((const u8 *) &iv[ 0]);
19360 rar5->iv[1] = hex_to_u32 ((const u8 *) &iv[ 8]);
19361 rar5->iv[2] = hex_to_u32 ((const u8 *) &iv[16]);
19362 rar5->iv[3] = hex_to_u32 ((const u8 *) &iv[24]);
19363
19364 salt->salt_len = 16;
19365
19366 salt->salt_sign[0] = iterations;
19367
19368 salt->salt_iter = ((1 << iterations) + 32) - 1;
19369
19370 /**
19371 * digest buf
19372 */
19373
19374 digest[0] = hex_to_u32 ((const u8 *) &pswcheck[ 0]);
19375 digest[1] = hex_to_u32 ((const u8 *) &pswcheck[ 8]);
19376 digest[2] = 0;
19377 digest[3] = 0;
19378
19379 return (PARSER_OK);
19380 }
19381
19382 int krb5tgs_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
19383 {
19384 if ((input_len < DISPLAY_LEN_MIN_13100) || (input_len > DISPLAY_LEN_MAX_13100)) return (PARSER_GLOBAL_LENGTH);
19385
19386 if (memcmp (SIGNATURE_KRB5TGS, input_buf, 11)) return (PARSER_SIGNATURE_UNMATCHED);
19387
19388 u32 *digest = (u32 *) hash_buf->digest;
19389
19390 salt_t *salt = hash_buf->salt;
19391
19392 krb5tgs_t *krb5tgs = (krb5tgs_t *) hash_buf->esalt;
19393
19394 /**
19395 * parse line
19396 */
19397
19398 /* Skip '$' */
19399 char *account_pos = input_buf + 11 + 1;
19400
19401 char *data_pos;
19402
19403 uint data_len;
19404
19405 if (account_pos[0] == '*')
19406 {
19407 account_pos++;
19408
19409 data_pos = strchr (account_pos, '*');
19410
19411 /* Skip '*' */
19412 data_pos++;
19413
19414 if (data_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19415
19416 uint account_len = data_pos - account_pos + 1;
19417
19418 if (account_len >= 512) return (PARSER_SALT_LENGTH);
19419
19420 /* Skip '$' */
19421 data_pos++;
19422
19423 data_len = input_len - 11 - 1 - account_len - 2;
19424
19425 memcpy (krb5tgs->account_info, account_pos - 1, account_len);
19426 }
19427 else
19428 {
19429 /* assume $krb5tgs$23$checksum$edata2 */
19430 data_pos = account_pos;
19431
19432 memcpy (krb5tgs->account_info, "**", 3);
19433
19434 data_len = input_len - 11 - 1 - 1;
19435 }
19436
19437 if (data_len < ((16 + 32) * 2)) return (PARSER_SALT_LENGTH);
19438
19439 char *checksum_ptr = (char *) krb5tgs->checksum;
19440
19441 for (uint i = 0; i < 16 * 2; i += 2)
19442 {
19443 const char p0 = data_pos[i + 0];
19444 const char p1 = data_pos[i + 1];
19445
19446 *checksum_ptr++ = hex_convert (p1) << 0
19447 | hex_convert (p0) << 4;
19448 }
19449
19450 char *edata_ptr = (char *) krb5tgs->edata2;
19451
19452 krb5tgs->edata2_len = (data_len - 32) / 2 ;
19453
19454 /* skip '$' */
19455 for (uint i = 16 * 2 + 1; i < (krb5tgs->edata2_len * 2) + (16 * 2 + 1); i += 2)
19456 {
19457 const char p0 = data_pos[i + 0];
19458 const char p1 = data_pos[i + 1];
19459 *edata_ptr++ = hex_convert (p1) << 0
19460 | hex_convert (p0) << 4;
19461 }
19462
19463 /* this is needed for hmac_md5 */
19464 *edata_ptr++ = 0x80;
19465
19466 salt->salt_buf[0] = krb5tgs->checksum[0];
19467 salt->salt_buf[1] = krb5tgs->checksum[1];
19468 salt->salt_buf[2] = krb5tgs->checksum[2];
19469 salt->salt_buf[3] = krb5tgs->checksum[3];
19470
19471 salt->salt_len = 32;
19472
19473 digest[0] = krb5tgs->checksum[0];
19474 digest[1] = krb5tgs->checksum[1];
19475 digest[2] = krb5tgs->checksum[2];
19476 digest[3] = krb5tgs->checksum[3];
19477
19478 return (PARSER_OK);
19479 }
19480
19481 int axcrypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
19482 {
19483 if ((input_len < DISPLAY_LEN_MIN_13200) || (input_len > DISPLAY_LEN_MAX_13200)) return (PARSER_GLOBAL_LENGTH);
19484
19485 if (memcmp (SIGNATURE_AXCRYPT, input_buf, 11)) return (PARSER_SIGNATURE_UNMATCHED);
19486
19487 u32 *digest = (u32 *) hash_buf->digest;
19488
19489 salt_t *salt = hash_buf->salt;
19490
19491 /**
19492 * parse line
19493 */
19494
19495 /* Skip '*' */
19496 char *wrapping_rounds_pos = input_buf + 11 + 1;
19497
19498 char *salt_pos;
19499
19500 char *wrapped_key_pos;
19501
19502 char *data_pos;
19503
19504 salt->salt_iter = atoi (wrapping_rounds_pos);
19505
19506 salt_pos = strchr (wrapping_rounds_pos, '*');
19507
19508 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19509
19510 uint wrapping_rounds_len = salt_pos - wrapping_rounds_pos;
19511
19512 /* Skip '*' */
19513 salt_pos++;
19514
19515 data_pos = salt_pos;
19516
19517 wrapped_key_pos = strchr (salt_pos, '*');
19518
19519 if (wrapped_key_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19520
19521 uint salt_len = wrapped_key_pos - salt_pos;
19522
19523 if (salt_len != 32) return (PARSER_SALT_LENGTH);
19524
19525 /* Skip '*' */
19526 wrapped_key_pos++;
19527
19528 uint wrapped_key_len = input_len - 11 - 1 - wrapping_rounds_len - 1 - salt_len - 1;
19529
19530 if (wrapped_key_len != 48) return (PARSER_SALT_LENGTH);
19531
19532 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &data_pos[ 0]);
19533 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &data_pos[ 8]);
19534 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &data_pos[16]);
19535 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &data_pos[24]);
19536
19537 data_pos += 33;
19538
19539 salt->salt_buf[4] = hex_to_u32 ((const u8 *) &data_pos[ 0]);
19540 salt->salt_buf[5] = hex_to_u32 ((const u8 *) &data_pos[ 8]);
19541 salt->salt_buf[6] = hex_to_u32 ((const u8 *) &data_pos[16]);
19542 salt->salt_buf[7] = hex_to_u32 ((const u8 *) &data_pos[24]);
19543 salt->salt_buf[8] = hex_to_u32 ((const u8 *) &data_pos[32]);
19544 salt->salt_buf[9] = hex_to_u32 ((const u8 *) &data_pos[40]);
19545
19546 salt->salt_len = 40;
19547
19548 digest[0] = salt->salt_buf[0];
19549 digest[1] = salt->salt_buf[1];
19550 digest[2] = salt->salt_buf[2];
19551 digest[3] = salt->salt_buf[3];
19552
19553 return (PARSER_OK);
19554 }
19555
19556 int keepass_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
19557 {
19558 if ((input_len < DISPLAY_LEN_MIN_13400) || (input_len > DISPLAY_LEN_MAX_13400)) return (PARSER_GLOBAL_LENGTH);
19559
19560 if (memcmp (SIGNATURE_KEEPASS, input_buf, 9)) return (PARSER_SIGNATURE_UNMATCHED);
19561
19562 u32 *digest = (u32 *) hash_buf->digest;
19563
19564 salt_t *salt = hash_buf->salt;
19565
19566 keepass_t *keepass = (keepass_t *) hash_buf->esalt;
19567
19568 /**
19569 * parse line
19570 */
19571
19572 char *version_pos;
19573
19574 char *rounds_pos;
19575
19576 char *algorithm_pos;
19577
19578 char *final_random_seed_pos;
19579 u32 final_random_seed_len;
19580
19581 char *transf_random_seed_pos;
19582 u32 transf_random_seed_len;
19583
19584 char *enc_iv_pos;
19585 u32 enc_iv_len;
19586
19587 /* default is no keyfile provided */
19588 char *keyfile_len_pos;
19589 u32 keyfile_len = 0;
19590 u32 is_keyfile_present = 0;
19591 char *keyfile_inline_pos;
19592 char *keyfile_pos;
19593
19594 /* specific to version 1 */
19595 char *contents_len_pos;
19596 u32 contents_len;
19597 char *contents_pos;
19598
19599 /* specific to version 2 */
19600 char *expected_bytes_pos;
19601 u32 expected_bytes_len;
19602
19603 char *contents_hash_pos;
19604 u32 contents_hash_len;
19605
19606 version_pos = input_buf + 8 + 1 + 1;
19607
19608 keepass->version = atoi (version_pos);
19609
19610 rounds_pos = strchr (version_pos, '*');
19611
19612 if (rounds_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19613
19614 rounds_pos++;
19615
19616 salt->salt_iter = (atoi (rounds_pos));
19617
19618 algorithm_pos = strchr (rounds_pos, '*');
19619
19620 if (algorithm_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19621
19622 algorithm_pos++;
19623
19624 keepass->algorithm = atoi (algorithm_pos);
19625
19626 final_random_seed_pos = strchr (algorithm_pos, '*');
19627
19628 if (final_random_seed_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19629
19630 final_random_seed_pos++;
19631
19632 keepass->final_random_seed[0] = hex_to_u32 ((const u8 *) &final_random_seed_pos[ 0]);
19633 keepass->final_random_seed[1] = hex_to_u32 ((const u8 *) &final_random_seed_pos[ 8]);
19634 keepass->final_random_seed[2] = hex_to_u32 ((const u8 *) &final_random_seed_pos[16]);
19635 keepass->final_random_seed[3] = hex_to_u32 ((const u8 *) &final_random_seed_pos[24]);
19636
19637 if (keepass->version == 2)
19638 {
19639 keepass->final_random_seed[4] = hex_to_u32 ((const u8 *) &final_random_seed_pos[32]);
19640 keepass->final_random_seed[5] = hex_to_u32 ((const u8 *) &final_random_seed_pos[40]);
19641 keepass->final_random_seed[6] = hex_to_u32 ((const u8 *) &final_random_seed_pos[48]);
19642 keepass->final_random_seed[7] = hex_to_u32 ((const u8 *) &final_random_seed_pos[56]);
19643 }
19644
19645 transf_random_seed_pos = strchr (final_random_seed_pos, '*');
19646
19647 if (transf_random_seed_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19648
19649 final_random_seed_len = transf_random_seed_pos - final_random_seed_pos;
19650
19651 if (keepass->version == 1 && final_random_seed_len != 32) return (PARSER_SALT_LENGTH);
19652 if (keepass->version == 2 && final_random_seed_len != 64) return (PARSER_SALT_LENGTH);
19653
19654 transf_random_seed_pos++;
19655
19656 keepass->transf_random_seed[0] = hex_to_u32 ((const u8 *) &transf_random_seed_pos[ 0]);
19657 keepass->transf_random_seed[1] = hex_to_u32 ((const u8 *) &transf_random_seed_pos[ 8]);
19658 keepass->transf_random_seed[2] = hex_to_u32 ((const u8 *) &transf_random_seed_pos[16]);
19659 keepass->transf_random_seed[3] = hex_to_u32 ((const u8 *) &transf_random_seed_pos[24]);
19660 keepass->transf_random_seed[4] = hex_to_u32 ((const u8 *) &transf_random_seed_pos[32]);
19661 keepass->transf_random_seed[5] = hex_to_u32 ((const u8 *) &transf_random_seed_pos[40]);
19662 keepass->transf_random_seed[6] = hex_to_u32 ((const u8 *) &transf_random_seed_pos[48]);
19663 keepass->transf_random_seed[7] = hex_to_u32 ((const u8 *) &transf_random_seed_pos[56]);
19664
19665 enc_iv_pos = strchr (transf_random_seed_pos, '*');
19666
19667 if (enc_iv_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19668
19669 transf_random_seed_len = enc_iv_pos - transf_random_seed_pos;
19670
19671 if (transf_random_seed_len != 64) return (PARSER_SALT_LENGTH);
19672
19673 enc_iv_pos++;
19674
19675 keepass->enc_iv[0] = hex_to_u32 ((const u8 *) &enc_iv_pos[ 0]);
19676 keepass->enc_iv[1] = hex_to_u32 ((const u8 *) &enc_iv_pos[ 8]);
19677 keepass->enc_iv[2] = hex_to_u32 ((const u8 *) &enc_iv_pos[16]);
19678 keepass->enc_iv[3] = hex_to_u32 ((const u8 *) &enc_iv_pos[24]);
19679
19680 if (keepass->version == 1)
19681 {
19682 contents_hash_pos = strchr (enc_iv_pos, '*');
19683
19684 if (contents_hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19685
19686 enc_iv_len = contents_hash_pos - enc_iv_pos;
19687
19688 if (enc_iv_len != 32) return (PARSER_SALT_LENGTH);
19689
19690 contents_hash_pos++;
19691
19692 keepass->contents_hash[0] = hex_to_u32 ((const u8 *) &contents_hash_pos[ 0]);
19693 keepass->contents_hash[1] = hex_to_u32 ((const u8 *) &contents_hash_pos[ 8]);
19694 keepass->contents_hash[2] = hex_to_u32 ((const u8 *) &contents_hash_pos[16]);
19695 keepass->contents_hash[3] = hex_to_u32 ((const u8 *) &contents_hash_pos[24]);
19696 keepass->contents_hash[4] = hex_to_u32 ((const u8 *) &contents_hash_pos[32]);
19697 keepass->contents_hash[5] = hex_to_u32 ((const u8 *) &contents_hash_pos[40]);
19698 keepass->contents_hash[6] = hex_to_u32 ((const u8 *) &contents_hash_pos[48]);
19699 keepass->contents_hash[7] = hex_to_u32 ((const u8 *) &contents_hash_pos[56]);
19700
19701 /* get length of contents following */
19702 char *inline_flag_pos = strchr (contents_hash_pos, '*');
19703
19704 if (inline_flag_pos == NULL) return (PARSER_SALT_LENGTH);
19705
19706 contents_hash_len = inline_flag_pos - contents_hash_pos;
19707
19708 if (contents_hash_len != 64) return (PARSER_SALT_LENGTH);
19709
19710 inline_flag_pos++;
19711
19712 u32 inline_flag = atoi (inline_flag_pos);
19713
19714 if (inline_flag != 1) return (PARSER_SALT_LENGTH);
19715
19716 contents_len_pos = strchr (inline_flag_pos, '*');
19717
19718 if (contents_len_pos == NULL) return (PARSER_SALT_LENGTH);
19719
19720 contents_len_pos++;
19721
19722 contents_len = atoi (contents_len_pos);
19723
19724 if (contents_len > 50000) return (PARSER_SALT_LENGTH);
19725
19726 contents_pos = strchr (contents_len_pos, '*');
19727
19728 if (contents_pos == NULL) return (PARSER_SALT_LENGTH);
19729
19730 contents_pos++;
19731
19732 u32 i;
19733
19734 keepass->contents_len = contents_len;
19735
19736 contents_len = contents_len / 4;
19737
19738 keyfile_inline_pos = strchr (contents_pos, '*');
19739
19740 u32 real_contents_len;
19741
19742 if (keyfile_inline_pos == NULL)
19743 real_contents_len = input_len - (contents_pos - input_buf);
19744 else
19745 {
19746 real_contents_len = keyfile_inline_pos - contents_pos;
19747 keyfile_inline_pos++;
19748 is_keyfile_present = 1;
19749 }
19750
19751 if (real_contents_len != keepass->contents_len * 2) return (PARSER_SALT_LENGTH);
19752
19753 for (i = 0; i < contents_len; i++)
19754 keepass->contents[i] = hex_to_u32 ((const u8 *) &contents_pos[i * 8]);
19755 }
19756 else if (keepass->version == 2)
19757 {
19758 expected_bytes_pos = strchr (enc_iv_pos, '*');
19759
19760 if (expected_bytes_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19761
19762 enc_iv_len = expected_bytes_pos - enc_iv_pos;
19763
19764 if (enc_iv_len != 32) return (PARSER_SALT_LENGTH);
19765
19766 expected_bytes_pos++;
19767
19768 keepass->expected_bytes[0] = hex_to_u32 ((const u8 *) &expected_bytes_pos[ 0]);
19769 keepass->expected_bytes[1] = hex_to_u32 ((const u8 *) &expected_bytes_pos[ 8]);
19770 keepass->expected_bytes[2] = hex_to_u32 ((const u8 *) &expected_bytes_pos[16]);
19771 keepass->expected_bytes[3] = hex_to_u32 ((const u8 *) &expected_bytes_pos[24]);
19772 keepass->expected_bytes[4] = hex_to_u32 ((const u8 *) &expected_bytes_pos[32]);
19773 keepass->expected_bytes[5] = hex_to_u32 ((const u8 *) &expected_bytes_pos[40]);
19774 keepass->expected_bytes[6] = hex_to_u32 ((const u8 *) &expected_bytes_pos[48]);
19775 keepass->expected_bytes[7] = hex_to_u32 ((const u8 *) &expected_bytes_pos[56]);
19776
19777 contents_hash_pos = strchr (expected_bytes_pos, '*');
19778
19779 if (contents_hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19780
19781 expected_bytes_len = contents_hash_pos - expected_bytes_pos;
19782
19783 if (expected_bytes_len != 64) return (PARSER_SALT_LENGTH);
19784
19785 contents_hash_pos++;
19786
19787 keepass->contents_hash[0] = hex_to_u32 ((const u8 *) &contents_hash_pos[ 0]);
19788 keepass->contents_hash[1] = hex_to_u32 ((const u8 *) &contents_hash_pos[ 8]);
19789 keepass->contents_hash[2] = hex_to_u32 ((const u8 *) &contents_hash_pos[16]);
19790 keepass->contents_hash[3] = hex_to_u32 ((const u8 *) &contents_hash_pos[24]);
19791 keepass->contents_hash[4] = hex_to_u32 ((const u8 *) &contents_hash_pos[32]);
19792 keepass->contents_hash[5] = hex_to_u32 ((const u8 *) &contents_hash_pos[40]);
19793 keepass->contents_hash[6] = hex_to_u32 ((const u8 *) &contents_hash_pos[48]);
19794 keepass->contents_hash[7] = hex_to_u32 ((const u8 *) &contents_hash_pos[56]);
19795
19796 keyfile_inline_pos = strchr (contents_hash_pos, '*');
19797
19798 if (keyfile_inline_pos == NULL)
19799 contents_hash_len = input_len - (int) (contents_hash_pos - input_buf);
19800 else
19801 {
19802 contents_hash_len = keyfile_inline_pos - contents_hash_pos;
19803 keyfile_inline_pos++;
19804 is_keyfile_present = 1;
19805 }
19806 if (contents_hash_len != 64) return (PARSER_SALT_LENGTH);
19807 }
19808
19809 if (is_keyfile_present != 0)
19810 {
19811 keyfile_len_pos = strchr (keyfile_inline_pos, '*');
19812
19813 keyfile_len_pos++;
19814
19815 keyfile_len = atoi (keyfile_len_pos);
19816
19817 keepass->keyfile_len = keyfile_len;
19818
19819 if (keyfile_len != 64) return (PARSER_SALT_LENGTH);
19820
19821 keyfile_pos = strchr (keyfile_len_pos, '*');
19822
19823 if (keyfile_pos == NULL) return (PARSER_SALT_LENGTH);
19824
19825 keyfile_pos++;
19826
19827 u32 real_keyfile_len = input_len - (keyfile_pos - input_buf);
19828
19829 if (real_keyfile_len != 64) return (PARSER_SALT_LENGTH);
19830
19831 keepass->keyfile[0] = hex_to_u32 ((const u8 *) &keyfile_pos[ 0]);
19832 keepass->keyfile[1] = hex_to_u32 ((const u8 *) &keyfile_pos[ 8]);
19833 keepass->keyfile[2] = hex_to_u32 ((const u8 *) &keyfile_pos[16]);
19834 keepass->keyfile[3] = hex_to_u32 ((const u8 *) &keyfile_pos[24]);
19835 keepass->keyfile[4] = hex_to_u32 ((const u8 *) &keyfile_pos[32]);
19836 keepass->keyfile[5] = hex_to_u32 ((const u8 *) &keyfile_pos[40]);
19837 keepass->keyfile[6] = hex_to_u32 ((const u8 *) &keyfile_pos[48]);
19838 keepass->keyfile[7] = hex_to_u32 ((const u8 *) &keyfile_pos[56]);
19839 }
19840
19841 digest[0] = keepass->enc_iv[0];
19842 digest[1] = keepass->enc_iv[1];
19843 digest[2] = keepass->enc_iv[2];
19844 digest[3] = keepass->enc_iv[3];
19845
19846 salt->salt_buf[0] = keepass->transf_random_seed[0];
19847 salt->salt_buf[1] = keepass->transf_random_seed[1];
19848 salt->salt_buf[2] = keepass->transf_random_seed[2];
19849 salt->salt_buf[3] = keepass->transf_random_seed[3];
19850 salt->salt_buf[4] = keepass->transf_random_seed[4];
19851 salt->salt_buf[5] = keepass->transf_random_seed[5];
19852 salt->salt_buf[6] = keepass->transf_random_seed[6];
19853 salt->salt_buf[7] = keepass->transf_random_seed[7];
19854
19855 return (PARSER_OK);
19856 }
19857
19858 int cf10_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
19859 {
19860 if ((input_len < DISPLAY_LEN_MIN_12600) || (input_len > DISPLAY_LEN_MAX_12600)) return (PARSER_GLOBAL_LENGTH);
19861
19862 u32 *digest = (u32 *) hash_buf->digest;
19863
19864 salt_t *salt = hash_buf->salt;
19865
19866 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
19867 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
19868 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
19869 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
19870 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
19871 digest[5] = hex_to_u32 ((const u8 *) &input_buf[40]);
19872 digest[6] = hex_to_u32 ((const u8 *) &input_buf[48]);
19873 digest[7] = hex_to_u32 ((const u8 *) &input_buf[56]);
19874
19875 if (input_buf[64] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
19876
19877 uint salt_len = input_len - 64 - 1;
19878
19879 char *salt_buf = input_buf + 64 + 1;
19880
19881 char *salt_buf_ptr = (char *) salt->salt_buf;
19882
19883 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
19884
19885 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
19886
19887 salt->salt_len = salt_len;
19888
19889 /**
19890 * we can precompute the first sha256 transform
19891 */
19892
19893 uint w[16] = { 0 };
19894
19895 w[ 0] = byte_swap_32 (salt->salt_buf[ 0]);
19896 w[ 1] = byte_swap_32 (salt->salt_buf[ 1]);
19897 w[ 2] = byte_swap_32 (salt->salt_buf[ 2]);
19898 w[ 3] = byte_swap_32 (salt->salt_buf[ 3]);
19899 w[ 4] = byte_swap_32 (salt->salt_buf[ 4]);
19900 w[ 5] = byte_swap_32 (salt->salt_buf[ 5]);
19901 w[ 6] = byte_swap_32 (salt->salt_buf[ 6]);
19902 w[ 7] = byte_swap_32 (salt->salt_buf[ 7]);
19903 w[ 8] = byte_swap_32 (salt->salt_buf[ 8]);
19904 w[ 9] = byte_swap_32 (salt->salt_buf[ 9]);
19905 w[10] = byte_swap_32 (salt->salt_buf[10]);
19906 w[11] = byte_swap_32 (salt->salt_buf[11]);
19907 w[12] = byte_swap_32 (salt->salt_buf[12]);
19908 w[13] = byte_swap_32 (salt->salt_buf[13]);
19909 w[14] = byte_swap_32 (salt->salt_buf[14]);
19910 w[15] = byte_swap_32 (salt->salt_buf[15]);
19911
19912 uint pc256[8] = { SHA256M_A, SHA256M_B, SHA256M_C, SHA256M_D, SHA256M_E, SHA256M_F, SHA256M_G, SHA256M_H };
19913
19914 sha256_64 (w, pc256);
19915
19916 salt->salt_buf_pc[0] = pc256[0];
19917 salt->salt_buf_pc[1] = pc256[1];
19918 salt->salt_buf_pc[2] = pc256[2];
19919 salt->salt_buf_pc[3] = pc256[3];
19920 salt->salt_buf_pc[4] = pc256[4];
19921 salt->salt_buf_pc[5] = pc256[5];
19922 salt->salt_buf_pc[6] = pc256[6];
19923 salt->salt_buf_pc[7] = pc256[7];
19924
19925 digest[0] -= pc256[0];
19926 digest[1] -= pc256[1];
19927 digest[2] -= pc256[2];
19928 digest[3] -= pc256[3];
19929 digest[4] -= pc256[4];
19930 digest[5] -= pc256[5];
19931 digest[6] -= pc256[6];
19932 digest[7] -= pc256[7];
19933
19934 return (PARSER_OK);
19935 }
19936
19937 int mywallet_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
19938 {
19939 if ((input_len < DISPLAY_LEN_MIN_12700) || (input_len > DISPLAY_LEN_MAX_12700)) return (PARSER_GLOBAL_LENGTH);
19940
19941 if (memcmp (SIGNATURE_MYWALLET, input_buf, 12)) return (PARSER_SIGNATURE_UNMATCHED);
19942
19943 u32 *digest = (u32 *) hash_buf->digest;
19944
19945 salt_t *salt = hash_buf->salt;
19946
19947 /**
19948 * parse line
19949 */
19950
19951 char *data_len_pos = input_buf + 1 + 10 + 1;
19952
19953 char *data_buf_pos = strchr (data_len_pos, '$');
19954
19955 if (data_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19956
19957 u32 data_len_len = data_buf_pos - data_len_pos;
19958
19959 if (data_len_len < 1) return (PARSER_SALT_LENGTH);
19960 if (data_len_len > 5) return (PARSER_SALT_LENGTH);
19961
19962 data_buf_pos++;
19963
19964 u32 data_buf_len = input_len - 1 - 10 - 1 - data_len_len - 1;
19965
19966 if (data_buf_len < 64) return (PARSER_HASH_LENGTH);
19967
19968 if (data_buf_len % 16) return (PARSER_HASH_LENGTH);
19969
19970 u32 data_len = atoi (data_len_pos);
19971
19972 if ((data_len * 2) != data_buf_len) return (PARSER_HASH_LENGTH);
19973
19974 /**
19975 * salt
19976 */
19977
19978 char *salt_pos = data_buf_pos;
19979
19980 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_pos[ 0]);
19981 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_pos[ 8]);
19982 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &salt_pos[16]);
19983 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &salt_pos[24]);
19984
19985 // this is actually the CT, which is also the hash later (if matched)
19986
19987 salt->salt_buf[4] = hex_to_u32 ((const u8 *) &salt_pos[32]);
19988 salt->salt_buf[5] = hex_to_u32 ((const u8 *) &salt_pos[40]);
19989 salt->salt_buf[6] = hex_to_u32 ((const u8 *) &salt_pos[48]);
19990 salt->salt_buf[7] = hex_to_u32 ((const u8 *) &salt_pos[56]);
19991
19992 salt->salt_len = 32; // note we need to fix this to 16 in kernel
19993
19994 salt->salt_iter = 10 - 1;
19995
19996 /**
19997 * digest buf
19998 */
19999
20000 digest[0] = salt->salt_buf[4];
20001 digest[1] = salt->salt_buf[5];
20002 digest[2] = salt->salt_buf[6];
20003 digest[3] = salt->salt_buf[7];
20004
20005 return (PARSER_OK);
20006 }
20007
20008 int ms_drsr_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
20009 {
20010 if ((input_len < DISPLAY_LEN_MIN_12800) || (input_len > DISPLAY_LEN_MAX_12800)) return (PARSER_GLOBAL_LENGTH);
20011
20012 if (memcmp (SIGNATURE_MS_DRSR, input_buf, 11)) return (PARSER_SIGNATURE_UNMATCHED);
20013
20014 u32 *digest = (u32 *) hash_buf->digest;
20015
20016 salt_t *salt = hash_buf->salt;
20017
20018 /**
20019 * parse line
20020 */
20021
20022 char *salt_pos = input_buf + 11 + 1;
20023
20024 char *iter_pos = strchr (salt_pos, ',');
20025
20026 if (iter_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
20027
20028 u32 salt_len = iter_pos - salt_pos;
20029
20030 if (salt_len != 20) return (PARSER_SALT_LENGTH);
20031
20032 iter_pos++;
20033
20034 char *hash_pos = strchr (iter_pos, ',');
20035
20036 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
20037
20038 u32 iter_len = hash_pos - iter_pos;
20039
20040 if (iter_len > 5) return (PARSER_SALT_LENGTH);
20041
20042 hash_pos++;
20043
20044 u32 hash_len = input_len - 11 - 1 - salt_len - 1 - iter_len - 1;
20045
20046 if (hash_len != 64) return (PARSER_HASH_LENGTH);
20047
20048 /**
20049 * salt
20050 */
20051
20052 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_pos[ 0]);
20053 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_pos[ 8]);
20054 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &salt_pos[16]) & 0xffff0000;
20055 salt->salt_buf[3] = 0x00018000;
20056
20057 salt->salt_buf[0] = byte_swap_32 (salt->salt_buf[0]);
20058 salt->salt_buf[1] = byte_swap_32 (salt->salt_buf[1]);
20059 salt->salt_buf[2] = byte_swap_32 (salt->salt_buf[2]);
20060 salt->salt_buf[3] = byte_swap_32 (salt->salt_buf[3]);
20061
20062 salt->salt_len = salt_len / 2;
20063
20064 salt->salt_iter = atoi (iter_pos) - 1;
20065
20066 /**
20067 * digest buf
20068 */
20069
20070 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
20071 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
20072 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
20073 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
20074 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
20075 digest[5] = hex_to_u32 ((const u8 *) &hash_pos[40]);
20076 digest[6] = hex_to_u32 ((const u8 *) &hash_pos[48]);
20077 digest[7] = hex_to_u32 ((const u8 *) &hash_pos[56]);
20078
20079 return (PARSER_OK);
20080 }
20081
20082 int androidfde_samsung_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
20083 {
20084 if ((input_len < DISPLAY_LEN_MIN_12900) || (input_len > DISPLAY_LEN_MAX_12900)) return (PARSER_GLOBAL_LENGTH);
20085
20086 u32 *digest = (u32 *) hash_buf->digest;
20087
20088 salt_t *salt = hash_buf->salt;
20089
20090 /**
20091 * parse line
20092 */
20093
20094 char *hash_pos = input_buf + 64;
20095 char *salt1_pos = input_buf + 128;
20096 char *salt2_pos = input_buf;
20097
20098 /**
20099 * salt
20100 */
20101
20102 salt->salt_buf[ 0] = hex_to_u32 ((const u8 *) &salt1_pos[ 0]);
20103 salt->salt_buf[ 1] = hex_to_u32 ((const u8 *) &salt1_pos[ 8]);
20104 salt->salt_buf[ 2] = hex_to_u32 ((const u8 *) &salt1_pos[16]);
20105 salt->salt_buf[ 3] = hex_to_u32 ((const u8 *) &salt1_pos[24]);
20106
20107 salt->salt_buf[ 4] = hex_to_u32 ((const u8 *) &salt2_pos[ 0]);
20108 salt->salt_buf[ 5] = hex_to_u32 ((const u8 *) &salt2_pos[ 8]);
20109 salt->salt_buf[ 6] = hex_to_u32 ((const u8 *) &salt2_pos[16]);
20110 salt->salt_buf[ 7] = hex_to_u32 ((const u8 *) &salt2_pos[24]);
20111
20112 salt->salt_buf[ 8] = hex_to_u32 ((const u8 *) &salt2_pos[32]);
20113 salt->salt_buf[ 9] = hex_to_u32 ((const u8 *) &salt2_pos[40]);
20114 salt->salt_buf[10] = hex_to_u32 ((const u8 *) &salt2_pos[48]);
20115 salt->salt_buf[11] = hex_to_u32 ((const u8 *) &salt2_pos[56]);
20116
20117 salt->salt_len = 48;
20118
20119 salt->salt_iter = ROUNDS_ANDROIDFDE_SAMSUNG - 1;
20120
20121 /**
20122 * digest buf
20123 */
20124
20125 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
20126 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
20127 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
20128 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
20129 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
20130 digest[5] = hex_to_u32 ((const u8 *) &hash_pos[40]);
20131 digest[6] = hex_to_u32 ((const u8 *) &hash_pos[48]);
20132 digest[7] = hex_to_u32 ((const u8 *) &hash_pos[56]);
20133
20134 return (PARSER_OK);
20135 }
20136
20137 int zip2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
20138 {
20139 if ((input_len < DISPLAY_LEN_MIN_13600) || (input_len > DISPLAY_LEN_MAX_13600)) return (PARSER_GLOBAL_LENGTH);
20140
20141 if (memcmp (SIGNATURE_ZIP2_START, input_buf , 6)) return (PARSER_SIGNATURE_UNMATCHED);
20142 if (memcmp (SIGNATURE_ZIP2_STOP , input_buf + input_len - 7, 7)) return (PARSER_SIGNATURE_UNMATCHED);
20143
20144 u32 *digest = (u32 *) hash_buf->digest;
20145
20146 salt_t *salt = hash_buf->salt;
20147
20148 zip2_t *zip2 = (zip2_t *) hash_buf->esalt;
20149
20150 /**
20151 * parse line
20152 */
20153
20154 char *param0_pos = input_buf + 6 + 1;
20155
20156 char *param1_pos = strchr (param0_pos, '*');
20157
20158 if (param1_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
20159
20160 u32 param0_len = param1_pos - param0_pos;
20161
20162 param1_pos++;
20163
20164 char *param2_pos = strchr (param1_pos, '*');
20165
20166 if (param2_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
20167
20168 u32 param1_len = param2_pos - param1_pos;
20169
20170 param2_pos++;
20171
20172 char *param3_pos = strchr (param2_pos, '*');
20173
20174 if (param3_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
20175
20176 u32 param2_len = param3_pos - param2_pos;
20177
20178 param3_pos++;
20179
20180 char *param4_pos = strchr (param3_pos, '*');
20181
20182 if (param4_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
20183
20184 u32 param3_len = param4_pos - param3_pos;
20185
20186 param4_pos++;
20187
20188 char *param5_pos = strchr (param4_pos, '*');
20189
20190 if (param5_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
20191
20192 u32 param4_len = param5_pos - param4_pos;
20193
20194 param5_pos++;
20195
20196 char *param6_pos = strchr (param5_pos, '*');
20197
20198 if (param6_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
20199
20200 u32 param5_len = param6_pos - param5_pos;
20201
20202 param6_pos++;
20203
20204 char *param7_pos = strchr (param6_pos, '*');
20205
20206 if (param7_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
20207
20208 u32 param6_len = param7_pos - param6_pos;
20209
20210 param7_pos++;
20211
20212 char *param8_pos = strchr (param7_pos, '*');
20213
20214 if (param8_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
20215
20216 u32 param7_len = param8_pos - param7_pos;
20217
20218 param8_pos++;
20219
20220 const uint type = atoi (param0_pos);
20221 const uint mode = atoi (param1_pos);
20222 const uint magic = atoi (param2_pos);
20223
20224 char *salt_buf = param3_pos;
20225
20226 uint verify_bytes; sscanf (param4_pos, "%4x*", &verify_bytes);
20227
20228 const uint compress_length = atoi (param5_pos);
20229
20230 char *data_buf = param6_pos;
20231 char *auth = param7_pos;
20232
20233 /**
20234 * verify some data
20235 */
20236
20237 if (param0_len != 1) return (PARSER_SALT_VALUE);
20238
20239 if (param1_len != 1) return (PARSER_SALT_VALUE);
20240
20241 if (param2_len != 1) return (PARSER_SALT_VALUE);
20242
20243 if ((param3_len != 16) && (param3_len != 24) && (param3_len != 32)) return (PARSER_SALT_VALUE);
20244
20245 if (param4_len >= 5) return (PARSER_SALT_VALUE);
20246
20247 if (param5_len >= 5) return (PARSER_SALT_VALUE);
20248
20249 if (param6_len >= 8192) return (PARSER_SALT_VALUE);
20250
20251 if (param6_len & 1) return (PARSER_SALT_VALUE);
20252
20253 if (param7_len != 20) return (PARSER_SALT_VALUE);
20254
20255 if (type != 0) return (PARSER_SALT_VALUE);
20256
20257 if ((mode != 1) && (mode != 2) && (mode != 3)) return (PARSER_SALT_VALUE);
20258
20259 if (magic != 0) return (PARSER_SALT_VALUE);
20260
20261 if (verify_bytes >= 0x10000) return (PARSER_SALT_VALUE);
20262
20263 /**
20264 * store data
20265 */
20266
20267 zip2->type = type;
20268 zip2->mode = mode;
20269 zip2->magic = magic;
20270
20271 if (mode == 1)
20272 {
20273 zip2->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_buf[ 0]);
20274 zip2->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_buf[ 8]);
20275 zip2->salt_buf[2] = 0;
20276 zip2->salt_buf[3] = 0;
20277
20278 zip2->salt_len = 8;
20279 }
20280 else if (mode == 2)
20281 {
20282 zip2->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_buf[ 0]);
20283 zip2->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_buf[ 8]);
20284 zip2->salt_buf[2] = hex_to_u32 ((const u8 *) &salt_buf[16]);
20285 zip2->salt_buf[3] = 0;
20286
20287 zip2->salt_len = 12;
20288 }
20289 else if (mode == 3)
20290 {
20291 zip2->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_buf[ 0]);
20292 zip2->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_buf[ 8]);
20293 zip2->salt_buf[2] = hex_to_u32 ((const u8 *) &salt_buf[16]);
20294 zip2->salt_buf[3] = hex_to_u32 ((const u8 *) &salt_buf[24]);
20295
20296 zip2->salt_len = 16;
20297 }
20298
20299 zip2->salt_buf[0] = byte_swap_32 (zip2->salt_buf[0]);
20300 zip2->salt_buf[1] = byte_swap_32 (zip2->salt_buf[1]);
20301 zip2->salt_buf[2] = byte_swap_32 (zip2->salt_buf[2]);
20302 zip2->salt_buf[3] = byte_swap_32 (zip2->salt_buf[3]);
20303
20304 zip2->verify_bytes = verify_bytes;
20305
20306 zip2->compress_length = compress_length;
20307
20308 char *data_buf_ptr = (char *) zip2->data_buf;
20309
20310 for (uint i = 0; i < param6_len; i += 2)
20311 {
20312 const char p0 = data_buf[i + 0];
20313 const char p1 = data_buf[i + 1];
20314
20315 *data_buf_ptr++ = hex_convert (p1) << 0
20316 | hex_convert (p0) << 4;
20317
20318 zip2->data_len++;
20319 }
20320
20321 *data_buf_ptr = 0x80;
20322
20323 char *auth_ptr = (char *) zip2->auth_buf;
20324
20325 for (uint i = 0; i < param7_len; i += 2)
20326 {
20327 const char p0 = auth[i + 0];
20328 const char p1 = auth[i + 1];
20329
20330 *auth_ptr++ = hex_convert (p1) << 0
20331 | hex_convert (p0) << 4;
20332
20333 zip2->auth_len++;
20334 }
20335
20336 /**
20337 * salt buf (fake)
20338 */
20339
20340 salt->salt_buf[0] = zip2->salt_buf[0];
20341 salt->salt_buf[1] = zip2->salt_buf[1];
20342 salt->salt_buf[2] = zip2->salt_buf[2];
20343 salt->salt_buf[3] = zip2->salt_buf[3];
20344 salt->salt_buf[4] = zip2->data_buf[0];
20345 salt->salt_buf[5] = zip2->data_buf[1];
20346 salt->salt_buf[6] = zip2->data_buf[2];
20347 salt->salt_buf[7] = zip2->data_buf[3];
20348
20349 salt->salt_len = 32;
20350
20351 salt->salt_iter = ROUNDS_ZIP2 - 1;
20352
20353 /**
20354 * digest buf (fake)
20355 */
20356
20357 digest[0] = zip2->auth_buf[0];
20358 digest[1] = zip2->auth_buf[1];
20359 digest[2] = zip2->auth_buf[2];
20360 digest[3] = zip2->auth_buf[3];
20361
20362 return (PARSER_OK);
20363 }
20364
20365 /**
20366 * parallel running threads
20367 */
20368
20369 #ifdef WIN
20370
20371 BOOL WINAPI sigHandler_default (DWORD sig)
20372 {
20373 switch (sig)
20374 {
20375 case CTRL_CLOSE_EVENT:
20376
20377 /*
20378 * special case see: https://stackoverflow.com/questions/3640633/c-setconsolectrlhandler-routine-issue/5610042#5610042
20379 * if the user interacts w/ the user-interface (GUI/cmd), we need to do the finalization job within this signal handler
20380 * function otherwise it is too late (e.g. after returning from this function)
20381 */
20382
20383 myabort ();
20384
20385 SetConsoleCtrlHandler (NULL, TRUE);
20386
20387 hc_sleep (10);
20388
20389 return TRUE;
20390
20391 case CTRL_C_EVENT:
20392 case CTRL_LOGOFF_EVENT:
20393 case CTRL_SHUTDOWN_EVENT:
20394
20395 myabort ();
20396
20397 SetConsoleCtrlHandler (NULL, TRUE);
20398
20399 return TRUE;
20400 }
20401
20402 return FALSE;
20403 }
20404
20405 BOOL WINAPI sigHandler_benchmark (DWORD sig)
20406 {
20407 switch (sig)
20408 {
20409 case CTRL_CLOSE_EVENT:
20410
20411 myabort ();
20412
20413 SetConsoleCtrlHandler (NULL, TRUE);
20414
20415 hc_sleep (10);
20416
20417 return TRUE;
20418
20419 case CTRL_C_EVENT:
20420 case CTRL_LOGOFF_EVENT:
20421 case CTRL_SHUTDOWN_EVENT:
20422
20423 myquit ();
20424
20425 SetConsoleCtrlHandler (NULL, TRUE);
20426
20427 return TRUE;
20428 }
20429
20430 return FALSE;
20431 }
20432
20433 void hc_signal (BOOL WINAPI (callback) (DWORD))
20434 {
20435 if (callback == NULL)
20436 {
20437 SetConsoleCtrlHandler ((PHANDLER_ROUTINE) callback, FALSE);
20438 }
20439 else
20440 {
20441 SetConsoleCtrlHandler ((PHANDLER_ROUTINE) callback, TRUE);
20442 }
20443 }
20444
20445 #else
20446
20447 void sigHandler_default (int sig)
20448 {
20449 myabort ();
20450
20451 signal (sig, NULL);
20452 }
20453
20454 void sigHandler_benchmark (int sig)
20455 {
20456 myquit ();
20457
20458 signal (sig, NULL);
20459 }
20460
20461 void hc_signal (void (callback) (int))
20462 {
20463 if (callback == NULL) callback = SIG_DFL;
20464
20465 signal (SIGINT, callback);
20466 signal (SIGTERM, callback);
20467 signal (SIGABRT, callback);
20468 }
20469
20470 #endif
20471
20472 void status_display ();
20473
20474 void *thread_keypress (void *p)
20475 {
20476 int benchmark = *((int *) p);
20477
20478 uint quiet = data.quiet;
20479
20480 tty_break();
20481
20482 while ((data.devices_status != STATUS_EXHAUSTED) && (data.devices_status != STATUS_CRACKED) && (data.devices_status != STATUS_ABORTED) && (data.devices_status != STATUS_QUIT))
20483 {
20484 int ch = tty_getchar();
20485
20486 if (ch == -1) break;
20487
20488 if (ch == 0) continue;
20489
20490 //https://github.com/hashcat/hashcat/issues/302
20491 //#ifdef _POSIX
20492 //if (ch != '\n')
20493 //#endif
20494
20495 hc_thread_mutex_lock (mux_display);
20496
20497 log_info ("");
20498
20499 switch (ch)
20500 {
20501 case 's':
20502 case '\r':
20503 case '\n':
20504
20505 log_info ("");
20506
20507 status_display ();
20508
20509 log_info ("");
20510
20511 if (quiet == 0) fprintf (stdout, "%s", PROMPT);
20512 if (quiet == 0) fflush (stdout);
20513
20514 break;
20515
20516 case 'b':
20517
20518 log_info ("");
20519
20520 bypass ();
20521
20522 log_info ("");
20523
20524 if (quiet == 0) fprintf (stdout, "%s", PROMPT);
20525 if (quiet == 0) fflush (stdout);
20526
20527 break;
20528
20529 case 'p':
20530
20531 log_info ("");
20532
20533 SuspendThreads ();
20534
20535 log_info ("");
20536
20537 if (quiet == 0) fprintf (stdout, "%s", PROMPT);
20538 if (quiet == 0) fflush (stdout);
20539
20540 break;
20541
20542 case 'r':
20543
20544 log_info ("");
20545
20546 ResumeThreads ();
20547
20548 log_info ("");
20549
20550 if (quiet == 0) fprintf (stdout, "%s", PROMPT);
20551 if (quiet == 0) fflush (stdout);
20552
20553 break;
20554
20555 case 'c':
20556
20557 log_info ("");
20558
20559 if (benchmark == 1) break;
20560
20561 stop_at_checkpoint ();
20562
20563 log_info ("");
20564
20565 if (quiet == 0) fprintf (stdout, "%s", PROMPT);
20566 if (quiet == 0) fflush (stdout);
20567
20568 break;
20569
20570 case 'q':
20571
20572 log_info ("");
20573
20574 if (benchmark == 1)
20575 {
20576 myquit ();
20577 }
20578 else
20579 {
20580 myabort ();
20581 }
20582
20583 break;
20584 }
20585
20586 //https://github.com/hashcat/hashcat/issues/302
20587 //#ifdef _POSIX
20588 //if (ch != '\n')
20589 //#endif
20590
20591 hc_thread_mutex_unlock (mux_display);
20592 }
20593
20594 tty_fix();
20595
20596 return (p);
20597 }
20598
20599 /**
20600 * rules common
20601 */
20602
20603 bool class_num (const u8 c)
20604 {
20605 return ((c >= '0') && (c <= '9'));
20606 }
20607
20608 bool class_lower (const u8 c)
20609 {
20610 return ((c >= 'a') && (c <= 'z'));
20611 }
20612
20613 bool class_upper (const u8 c)
20614 {
20615 return ((c >= 'A') && (c <= 'Z'));
20616 }
20617
20618 bool class_alpha (const u8 c)
20619 {
20620 return (class_lower (c) || class_upper (c));
20621 }
20622
20623 int conv_ctoi (const u8 c)
20624 {
20625 if (class_num (c))
20626 {
20627 return c - '0';
20628 }
20629 else if (class_upper (c))
20630 {
20631 return c - 'A' + 10;
20632 }
20633
20634 return -1;
20635 }
20636
20637 int conv_itoc (const u8 c)
20638 {
20639 if (c < 10)
20640 {
20641 return c + '0';
20642 }
20643 else if (c < 37)
20644 {
20645 return c + 'A' - 10;
20646 }
20647
20648 return -1;
20649 }
20650
20651 /**
20652 * device rules
20653 */
20654
20655 #define INCR_POS if (++rule_pos == rule_len) return (-1)
20656 #define SET_NAME(rule,val) (rule)->cmds[rule_cnt] = ((val) & 0xff) << 0
20657 #define SET_P0(rule,val) INCR_POS; (rule)->cmds[rule_cnt] |= ((val) & 0xff) << 8
20658 #define SET_P1(rule,val) INCR_POS; (rule)->cmds[rule_cnt] |= ((val) & 0xff) << 16
20659 #define MAX_KERNEL_RULES 255
20660 #define GET_NAME(rule) rule_cmd = (((rule)->cmds[rule_cnt] >> 0) & 0xff)
20661 #define GET_P0(rule) INCR_POS; rule_buf[rule_pos] = (((rule)->cmds[rule_cnt] >> 8) & 0xff)
20662 #define GET_P1(rule) INCR_POS; rule_buf[rule_pos] = (((rule)->cmds[rule_cnt] >> 16) & 0xff)
20663
20664 #define SET_P0_CONV(rule,val) INCR_POS; (rule)->cmds[rule_cnt] |= ((conv_ctoi (val)) & 0xff) << 8
20665 #define SET_P1_CONV(rule,val) INCR_POS; (rule)->cmds[rule_cnt] |= ((conv_ctoi (val)) & 0xff) << 16
20666 #define GET_P0_CONV(rule) INCR_POS; rule_buf[rule_pos] = conv_itoc (((rule)->cmds[rule_cnt] >> 8) & 0xff)
20667 #define GET_P1_CONV(rule) INCR_POS; rule_buf[rule_pos] = conv_itoc (((rule)->cmds[rule_cnt] >> 16) & 0xff)
20668
20669 int cpu_rule_to_kernel_rule (char *rule_buf, uint rule_len, kernel_rule_t *rule)
20670 {
20671 uint rule_pos;
20672 uint rule_cnt;
20673
20674 for (rule_pos = 0, rule_cnt = 0; rule_pos < rule_len && rule_cnt < MAX_KERNEL_RULES; rule_pos++, rule_cnt++)
20675 {
20676 switch (rule_buf[rule_pos])
20677 {
20678 case ' ':
20679 rule_cnt--;
20680 break;
20681
20682 case RULE_OP_MANGLE_NOOP:
20683 SET_NAME (rule, rule_buf[rule_pos]);
20684 break;
20685
20686 case RULE_OP_MANGLE_LREST:
20687 SET_NAME (rule, rule_buf[rule_pos]);
20688 break;
20689
20690 case RULE_OP_MANGLE_UREST:
20691 SET_NAME (rule, rule_buf[rule_pos]);
20692 break;
20693
20694 case RULE_OP_MANGLE_LREST_UFIRST:
20695 SET_NAME (rule, rule_buf[rule_pos]);
20696 break;
20697
20698 case RULE_OP_MANGLE_UREST_LFIRST:
20699 SET_NAME (rule, rule_buf[rule_pos]);
20700 break;
20701
20702 case RULE_OP_MANGLE_TREST:
20703 SET_NAME (rule, rule_buf[rule_pos]);
20704 break;
20705
20706 case RULE_OP_MANGLE_TOGGLE_AT:
20707 SET_NAME (rule, rule_buf[rule_pos]);
20708 SET_P0_CONV (rule, rule_buf[rule_pos]);
20709 break;
20710
20711 case RULE_OP_MANGLE_REVERSE:
20712 SET_NAME (rule, rule_buf[rule_pos]);
20713 break;
20714
20715 case RULE_OP_MANGLE_DUPEWORD:
20716 SET_NAME (rule, rule_buf[rule_pos]);
20717 break;
20718
20719 case RULE_OP_MANGLE_DUPEWORD_TIMES:
20720 SET_NAME (rule, rule_buf[rule_pos]);
20721 SET_P0_CONV (rule, rule_buf[rule_pos]);
20722 break;
20723
20724 case RULE_OP_MANGLE_REFLECT:
20725 SET_NAME (rule, rule_buf[rule_pos]);
20726 break;
20727
20728 case RULE_OP_MANGLE_ROTATE_LEFT:
20729 SET_NAME (rule, rule_buf[rule_pos]);
20730 break;
20731
20732 case RULE_OP_MANGLE_ROTATE_RIGHT:
20733 SET_NAME (rule, rule_buf[rule_pos]);
20734 break;
20735
20736 case RULE_OP_MANGLE_APPEND:
20737 SET_NAME (rule, rule_buf[rule_pos]);
20738 SET_P0 (rule, rule_buf[rule_pos]);
20739 break;
20740
20741 case RULE_OP_MANGLE_PREPEND:
20742 SET_NAME (rule, rule_buf[rule_pos]);
20743 SET_P0 (rule, rule_buf[rule_pos]);
20744 break;
20745
20746 case RULE_OP_MANGLE_DELETE_FIRST:
20747 SET_NAME (rule, rule_buf[rule_pos]);
20748 break;
20749
20750 case RULE_OP_MANGLE_DELETE_LAST:
20751 SET_NAME (rule, rule_buf[rule_pos]);
20752 break;
20753
20754 case RULE_OP_MANGLE_DELETE_AT:
20755 SET_NAME (rule, rule_buf[rule_pos]);
20756 SET_P0_CONV (rule, rule_buf[rule_pos]);
20757 break;
20758
20759 case RULE_OP_MANGLE_EXTRACT:
20760 SET_NAME (rule, rule_buf[rule_pos]);
20761 SET_P0_CONV (rule, rule_buf[rule_pos]);
20762 SET_P1_CONV (rule, rule_buf[rule_pos]);
20763 break;
20764
20765 case RULE_OP_MANGLE_OMIT:
20766 SET_NAME (rule, rule_buf[rule_pos]);
20767 SET_P0_CONV (rule, rule_buf[rule_pos]);
20768 SET_P1_CONV (rule, rule_buf[rule_pos]);
20769 break;
20770
20771 case RULE_OP_MANGLE_INSERT:
20772 SET_NAME (rule, rule_buf[rule_pos]);
20773 SET_P0_CONV (rule, rule_buf[rule_pos]);
20774 SET_P1 (rule, rule_buf[rule_pos]);
20775 break;
20776
20777 case RULE_OP_MANGLE_OVERSTRIKE:
20778 SET_NAME (rule, rule_buf[rule_pos]);
20779 SET_P0_CONV (rule, rule_buf[rule_pos]);
20780 SET_P1 (rule, rule_buf[rule_pos]);
20781 break;
20782
20783 case RULE_OP_MANGLE_TRUNCATE_AT:
20784 SET_NAME (rule, rule_buf[rule_pos]);
20785 SET_P0_CONV (rule, rule_buf[rule_pos]);
20786 break;
20787
20788 case RULE_OP_MANGLE_REPLACE:
20789 SET_NAME (rule, rule_buf[rule_pos]);
20790 SET_P0 (rule, rule_buf[rule_pos]);
20791 SET_P1 (rule, rule_buf[rule_pos]);
20792 break;
20793
20794 case RULE_OP_MANGLE_PURGECHAR:
20795 return (-1);
20796 break;
20797
20798 case RULE_OP_MANGLE_TOGGLECASE_REC:
20799 return (-1);
20800 break;
20801
20802 case RULE_OP_MANGLE_DUPECHAR_FIRST:
20803 SET_NAME (rule, rule_buf[rule_pos]);
20804 SET_P0_CONV (rule, rule_buf[rule_pos]);
20805 break;
20806
20807 case RULE_OP_MANGLE_DUPECHAR_LAST:
20808 SET_NAME (rule, rule_buf[rule_pos]);
20809 SET_P0_CONV (rule, rule_buf[rule_pos]);
20810 break;
20811
20812 case RULE_OP_MANGLE_DUPECHAR_ALL:
20813 SET_NAME (rule, rule_buf[rule_pos]);
20814 break;
20815
20816 case RULE_OP_MANGLE_SWITCH_FIRST:
20817 SET_NAME (rule, rule_buf[rule_pos]);
20818 break;
20819
20820 case RULE_OP_MANGLE_SWITCH_LAST:
20821 SET_NAME (rule, rule_buf[rule_pos]);
20822 break;
20823
20824 case RULE_OP_MANGLE_SWITCH_AT:
20825 SET_NAME (rule, rule_buf[rule_pos]);
20826 SET_P0_CONV (rule, rule_buf[rule_pos]);
20827 SET_P1_CONV (rule, rule_buf[rule_pos]);
20828 break;
20829
20830 case RULE_OP_MANGLE_CHR_SHIFTL:
20831 SET_NAME (rule, rule_buf[rule_pos]);
20832 SET_P0_CONV (rule, rule_buf[rule_pos]);
20833 break;
20834
20835 case RULE_OP_MANGLE_CHR_SHIFTR:
20836 SET_NAME (rule, rule_buf[rule_pos]);
20837 SET_P0_CONV (rule, rule_buf[rule_pos]);
20838 break;
20839
20840 case RULE_OP_MANGLE_CHR_INCR:
20841 SET_NAME (rule, rule_buf[rule_pos]);
20842 SET_P0_CONV (rule, rule_buf[rule_pos]);
20843 break;
20844
20845 case RULE_OP_MANGLE_CHR_DECR:
20846 SET_NAME (rule, rule_buf[rule_pos]);
20847 SET_P0_CONV (rule, rule_buf[rule_pos]);
20848 break;
20849
20850 case RULE_OP_MANGLE_REPLACE_NP1:
20851 SET_NAME (rule, rule_buf[rule_pos]);
20852 SET_P0_CONV (rule, rule_buf[rule_pos]);
20853 break;
20854
20855 case RULE_OP_MANGLE_REPLACE_NM1:
20856 SET_NAME (rule, rule_buf[rule_pos]);
20857 SET_P0_CONV (rule, rule_buf[rule_pos]);
20858 break;
20859
20860 case RULE_OP_MANGLE_DUPEBLOCK_FIRST:
20861 SET_NAME (rule, rule_buf[rule_pos]);
20862 SET_P0_CONV (rule, rule_buf[rule_pos]);
20863 break;
20864
20865 case RULE_OP_MANGLE_DUPEBLOCK_LAST:
20866 SET_NAME (rule, rule_buf[rule_pos]);
20867 SET_P0_CONV (rule, rule_buf[rule_pos]);
20868 break;
20869
20870 case RULE_OP_MANGLE_TITLE:
20871 SET_NAME (rule, rule_buf[rule_pos]);
20872 break;
20873
20874 default:
20875 return (-1);
20876 break;
20877 }
20878 }
20879
20880 if (rule_pos < rule_len) return (-1);
20881
20882 return (0);
20883 }
20884
20885 int kernel_rule_to_cpu_rule (char *rule_buf, kernel_rule_t *rule)
20886 {
20887 uint rule_cnt;
20888 uint rule_pos;
20889 uint rule_len = HCBUFSIZ - 1; // maximum possible len
20890
20891 char rule_cmd;
20892
20893 for (rule_cnt = 0, rule_pos = 0; rule_pos < rule_len && rule_cnt < MAX_KERNEL_RULES; rule_pos++, rule_cnt++)
20894 {
20895 GET_NAME (rule);
20896
20897 if (rule_cnt > 0) rule_buf[rule_pos++] = ' ';
20898
20899 switch (rule_cmd)
20900 {
20901 case RULE_OP_MANGLE_NOOP:
20902 rule_buf[rule_pos] = rule_cmd;
20903 break;
20904
20905 case RULE_OP_MANGLE_LREST:
20906 rule_buf[rule_pos] = rule_cmd;
20907 break;
20908
20909 case RULE_OP_MANGLE_UREST:
20910 rule_buf[rule_pos] = rule_cmd;
20911 break;
20912
20913 case RULE_OP_MANGLE_LREST_UFIRST:
20914 rule_buf[rule_pos] = rule_cmd;
20915 break;
20916
20917 case RULE_OP_MANGLE_UREST_LFIRST:
20918 rule_buf[rule_pos] = rule_cmd;
20919 break;
20920
20921 case RULE_OP_MANGLE_TREST:
20922 rule_buf[rule_pos] = rule_cmd;
20923 break;
20924
20925 case RULE_OP_MANGLE_TOGGLE_AT:
20926 rule_buf[rule_pos] = rule_cmd;
20927 GET_P0_CONV (rule);
20928 break;
20929
20930 case RULE_OP_MANGLE_REVERSE:
20931 rule_buf[rule_pos] = rule_cmd;
20932 break;
20933
20934 case RULE_OP_MANGLE_DUPEWORD:
20935 rule_buf[rule_pos] = rule_cmd;
20936 break;
20937
20938 case RULE_OP_MANGLE_DUPEWORD_TIMES:
20939 rule_buf[rule_pos] = rule_cmd;
20940 GET_P0_CONV (rule);
20941 break;
20942
20943 case RULE_OP_MANGLE_REFLECT:
20944 rule_buf[rule_pos] = rule_cmd;
20945 break;
20946
20947 case RULE_OP_MANGLE_ROTATE_LEFT:
20948 rule_buf[rule_pos] = rule_cmd;
20949 break;
20950
20951 case RULE_OP_MANGLE_ROTATE_RIGHT:
20952 rule_buf[rule_pos] = rule_cmd;
20953 break;
20954
20955 case RULE_OP_MANGLE_APPEND:
20956 rule_buf[rule_pos] = rule_cmd;
20957 GET_P0 (rule);
20958 break;
20959
20960 case RULE_OP_MANGLE_PREPEND:
20961 rule_buf[rule_pos] = rule_cmd;
20962 GET_P0 (rule);
20963 break;
20964
20965 case RULE_OP_MANGLE_DELETE_FIRST:
20966 rule_buf[rule_pos] = rule_cmd;
20967 break;
20968
20969 case RULE_OP_MANGLE_DELETE_LAST:
20970 rule_buf[rule_pos] = rule_cmd;
20971 break;
20972
20973 case RULE_OP_MANGLE_DELETE_AT:
20974 rule_buf[rule_pos] = rule_cmd;
20975 GET_P0_CONV (rule);
20976 break;
20977
20978 case RULE_OP_MANGLE_EXTRACT:
20979 rule_buf[rule_pos] = rule_cmd;
20980 GET_P0_CONV (rule);
20981 GET_P1_CONV (rule);
20982 break;
20983
20984 case RULE_OP_MANGLE_OMIT:
20985 rule_buf[rule_pos] = rule_cmd;
20986 GET_P0_CONV (rule);
20987 GET_P1_CONV (rule);
20988 break;
20989
20990 case RULE_OP_MANGLE_INSERT:
20991 rule_buf[rule_pos] = rule_cmd;
20992 GET_P0_CONV (rule);
20993 GET_P1 (rule);
20994 break;
20995
20996 case RULE_OP_MANGLE_OVERSTRIKE:
20997 rule_buf[rule_pos] = rule_cmd;
20998 GET_P0_CONV (rule);
20999 GET_P1 (rule);
21000 break;
21001
21002 case RULE_OP_MANGLE_TRUNCATE_AT:
21003 rule_buf[rule_pos] = rule_cmd;
21004 GET_P0_CONV (rule);
21005 break;
21006
21007 case RULE_OP_MANGLE_REPLACE:
21008 rule_buf[rule_pos] = rule_cmd;
21009 GET_P0 (rule);
21010 GET_P1 (rule);
21011 break;
21012
21013 case RULE_OP_MANGLE_PURGECHAR:
21014 return (-1);
21015 break;
21016
21017 case RULE_OP_MANGLE_TOGGLECASE_REC:
21018 return (-1);
21019 break;
21020
21021 case RULE_OP_MANGLE_DUPECHAR_FIRST:
21022 rule_buf[rule_pos] = rule_cmd;
21023 GET_P0_CONV (rule);
21024 break;
21025
21026 case RULE_OP_MANGLE_DUPECHAR_LAST:
21027 rule_buf[rule_pos] = rule_cmd;
21028 GET_P0_CONV (rule);
21029 break;
21030
21031 case RULE_OP_MANGLE_DUPECHAR_ALL:
21032 rule_buf[rule_pos] = rule_cmd;
21033 break;
21034
21035 case RULE_OP_MANGLE_SWITCH_FIRST:
21036 rule_buf[rule_pos] = rule_cmd;
21037 break;
21038
21039 case RULE_OP_MANGLE_SWITCH_LAST:
21040 rule_buf[rule_pos] = rule_cmd;
21041 break;
21042
21043 case RULE_OP_MANGLE_SWITCH_AT:
21044 rule_buf[rule_pos] = rule_cmd;
21045 GET_P0_CONV (rule);
21046 GET_P1_CONV (rule);
21047 break;
21048
21049 case RULE_OP_MANGLE_CHR_SHIFTL:
21050 rule_buf[rule_pos] = rule_cmd;
21051 GET_P0_CONV (rule);
21052 break;
21053
21054 case RULE_OP_MANGLE_CHR_SHIFTR:
21055 rule_buf[rule_pos] = rule_cmd;
21056 GET_P0_CONV (rule);
21057 break;
21058
21059 case RULE_OP_MANGLE_CHR_INCR:
21060 rule_buf[rule_pos] = rule_cmd;
21061 GET_P0_CONV (rule);
21062 break;
21063
21064 case RULE_OP_MANGLE_CHR_DECR:
21065 rule_buf[rule_pos] = rule_cmd;
21066 GET_P0_CONV (rule);
21067 break;
21068
21069 case RULE_OP_MANGLE_REPLACE_NP1:
21070 rule_buf[rule_pos] = rule_cmd;
21071 GET_P0_CONV (rule);
21072 break;
21073
21074 case RULE_OP_MANGLE_REPLACE_NM1:
21075 rule_buf[rule_pos] = rule_cmd;
21076 GET_P0_CONV (rule);
21077 break;
21078
21079 case RULE_OP_MANGLE_DUPEBLOCK_FIRST:
21080 rule_buf[rule_pos] = rule_cmd;
21081 GET_P0_CONV (rule);
21082 break;
21083
21084 case RULE_OP_MANGLE_DUPEBLOCK_LAST:
21085 rule_buf[rule_pos] = rule_cmd;
21086 GET_P0_CONV (rule);
21087 break;
21088
21089 case RULE_OP_MANGLE_TITLE:
21090 rule_buf[rule_pos] = rule_cmd;
21091 break;
21092
21093 case 0:
21094 return rule_pos - 1;
21095 break;
21096
21097 default:
21098 return (-1);
21099 break;
21100 }
21101 }
21102
21103 if (rule_cnt > 0)
21104 {
21105 return rule_pos;
21106 }
21107
21108 return (-1);
21109 }
21110
21111 /**
21112 * CPU rules : this is from hashcat sources, cpu based rules
21113 */
21114
21115 #define NEXT_RULEPOS(rp) if (++(rp) == rule_len) return (RULE_RC_SYNTAX_ERROR)
21116 #define NEXT_RPTOI(r,rp,up) if (((up) = conv_ctoi ((r)[(rp)])) == -1) return (RULE_RC_SYNTAX_ERROR)
21117
21118 #define MANGLE_TOGGLE_AT(a,p) if (class_alpha ((a)[(p)])) (a)[(p)] ^= 0x20
21119 #define MANGLE_LOWER_AT(a,p) if (class_upper ((a)[(p)])) (a)[(p)] ^= 0x20
21120 #define MANGLE_UPPER_AT(a,p) if (class_lower ((a)[(p)])) (a)[(p)] ^= 0x20
21121
21122 /* #define MANGLE_SWITCH(a,l,r) { char c = (l); arr[(r)] = arr[(l)]; arr[(l)] = c; } */
21123 /* #define MANGLE_SWITCH(a,l,r) { char c = (l); (a)[(r)] = (a)[(l)]; (a)[(l)] = c; } */
21124 #define MANGLE_SWITCH(a,l,r) { char c = (a)[(r)]; (a)[(r)] = (a)[(l)]; (a)[(l)] = c; }
21125
21126 int mangle_lrest (char arr[BLOCK_SIZE], int arr_len)
21127 {
21128 int pos;
21129
21130 for (pos = 0; pos < arr_len; pos++) MANGLE_LOWER_AT (arr, pos);
21131
21132 return (arr_len);
21133 }
21134
21135 int mangle_urest (char arr[BLOCK_SIZE], int arr_len)
21136 {
21137 int pos;
21138
21139 for (pos = 0; pos < arr_len; pos++) MANGLE_UPPER_AT (arr, pos);
21140
21141 return (arr_len);
21142 }
21143
21144 int mangle_trest (char arr[BLOCK_SIZE], int arr_len)
21145 {
21146 int pos;
21147
21148 for (pos = 0; pos < arr_len; pos++) MANGLE_TOGGLE_AT (arr, pos);
21149
21150 return (arr_len);
21151 }
21152
21153 int mangle_reverse (char arr[BLOCK_SIZE], int arr_len)
21154 {
21155 int l;
21156 int r;
21157
21158 for (l = 0; l < arr_len; l++)
21159 {
21160 r = arr_len - 1 - l;
21161
21162 if (l >= r) break;
21163
21164 MANGLE_SWITCH (arr, l, r);
21165 }
21166
21167 return (arr_len);
21168 }
21169
21170 int mangle_double (char arr[BLOCK_SIZE], int arr_len)
21171 {
21172 if ((arr_len * 2) >= BLOCK_SIZE) return (arr_len);
21173
21174 memcpy (&arr[arr_len], arr, (size_t) arr_len);
21175
21176 return (arr_len * 2);
21177 }
21178
21179 int mangle_double_times (char arr[BLOCK_SIZE], int arr_len, int times)
21180 {
21181 if (((arr_len * times) + arr_len) >= BLOCK_SIZE) return (arr_len);
21182
21183 int orig_len = arr_len;
21184
21185 int i;
21186
21187 for (i = 0; i < times; i++)
21188 {
21189 memcpy (&arr[arr_len], arr, orig_len);
21190
21191 arr_len += orig_len;
21192 }
21193
21194 return (arr_len);
21195 }
21196
21197 int mangle_reflect (char arr[BLOCK_SIZE], int arr_len)
21198 {
21199 if ((arr_len * 2) >= BLOCK_SIZE) return (arr_len);
21200
21201 mangle_double (arr, arr_len);
21202
21203 mangle_reverse (arr + arr_len, arr_len);
21204
21205 return (arr_len * 2);
21206 }
21207
21208 int mangle_rotate_left (char arr[BLOCK_SIZE], int arr_len)
21209 {
21210 int l;
21211 int r;
21212
21213 for (l = 0, r = arr_len - 1; r > 0; r--)
21214 {
21215 MANGLE_SWITCH (arr, l, r);
21216 }
21217
21218 return (arr_len);
21219 }
21220
21221 int mangle_rotate_right (char arr[BLOCK_SIZE], int arr_len)
21222 {
21223 int l;
21224 int r;
21225
21226 for (l = 0, r = arr_len - 1; l < r; l++)
21227 {
21228 MANGLE_SWITCH (arr, l, r);
21229 }
21230
21231 return (arr_len);
21232 }
21233
21234 int mangle_append (char arr[BLOCK_SIZE], int arr_len, char c)
21235 {
21236 if ((arr_len + 1) >= BLOCK_SIZE) return (arr_len);
21237
21238 arr[arr_len] = c;
21239
21240 return (arr_len + 1);
21241 }
21242
21243 int mangle_prepend (char arr[BLOCK_SIZE], int arr_len, char c)
21244 {
21245 if ((arr_len + 1) >= BLOCK_SIZE) return (arr_len);
21246
21247 int arr_pos;
21248
21249 for (arr_pos = arr_len - 1; arr_pos > -1; arr_pos--)
21250 {
21251 arr[arr_pos + 1] = arr[arr_pos];
21252 }
21253
21254 arr[0] = c;
21255
21256 return (arr_len + 1);
21257 }
21258
21259 int mangle_delete_at (char arr[BLOCK_SIZE], int arr_len, int upos)
21260 {
21261 if (upos >= arr_len) return (arr_len);
21262
21263 int arr_pos;
21264
21265 for (arr_pos = upos; arr_pos < arr_len - 1; arr_pos++)
21266 {
21267 arr[arr_pos] = arr[arr_pos + 1];
21268 }
21269
21270 return (arr_len - 1);
21271 }
21272
21273 int mangle_extract (char arr[BLOCK_SIZE], int arr_len, int upos, int ulen)
21274 {
21275 if (upos >= arr_len) return (arr_len);
21276
21277 if ((upos + ulen) > arr_len) return (arr_len);
21278
21279 int arr_pos;
21280
21281 for (arr_pos = 0; arr_pos < ulen; arr_pos++)
21282 {
21283 arr[arr_pos] = arr[upos + arr_pos];
21284 }
21285
21286 return (ulen);
21287 }
21288
21289 int mangle_omit (char arr[BLOCK_SIZE], int arr_len, int upos, int ulen)
21290 {
21291 if (upos >= arr_len) return (arr_len);
21292
21293 if ((upos + ulen) >= arr_len) return (arr_len);
21294
21295 int arr_pos;
21296
21297 for (arr_pos = upos; arr_pos < arr_len - ulen; arr_pos++)
21298 {
21299 arr[arr_pos] = arr[arr_pos + ulen];
21300 }
21301
21302 return (arr_len - ulen);
21303 }
21304
21305 int mangle_insert (char arr[BLOCK_SIZE], int arr_len, int upos, char c)
21306 {
21307 if (upos >= arr_len) return (arr_len);
21308
21309 if ((arr_len + 1) >= BLOCK_SIZE) return (arr_len);
21310
21311 int arr_pos;
21312
21313 for (arr_pos = arr_len - 1; arr_pos > upos - 1; arr_pos--)
21314 {
21315 arr[arr_pos + 1] = arr[arr_pos];
21316 }
21317
21318 arr[upos] = c;
21319
21320 return (arr_len + 1);
21321 }
21322
21323 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)
21324 {
21325 if ((arr_len + arr2_cpy) > BLOCK_SIZE) return (RULE_RC_REJECT_ERROR);
21326
21327 if (arr_pos > arr_len) return (RULE_RC_REJECT_ERROR);
21328
21329 if (arr2_pos > arr2_len) return (RULE_RC_REJECT_ERROR);
21330
21331 if ((arr2_pos + arr2_cpy) > arr2_len) return (RULE_RC_REJECT_ERROR);
21332
21333 if (arr2_cpy < 1) return (RULE_RC_SYNTAX_ERROR);
21334
21335 memcpy (arr2, arr2 + arr2_pos, arr2_len - arr2_pos);
21336
21337 memcpy (arr2 + arr2_cpy, arr + arr_pos, arr_len - arr_pos);
21338
21339 memcpy (arr + arr_pos, arr2, arr_len - arr_pos + arr2_cpy);
21340
21341 return (arr_len + arr2_cpy);
21342 }
21343
21344 int mangle_overstrike (char arr[BLOCK_SIZE], int arr_len, int upos, char c)
21345 {
21346 if (upos >= arr_len) return (arr_len);
21347
21348 arr[upos] = c;
21349
21350 return (arr_len);
21351 }
21352
21353 int mangle_truncate_at (char arr[BLOCK_SIZE], int arr_len, int upos)
21354 {
21355 if (upos >= arr_len) return (arr_len);
21356
21357 memset (arr + upos, 0, arr_len - upos);
21358
21359 return (upos);
21360 }
21361
21362 int mangle_replace (char arr[BLOCK_SIZE], int arr_len, char oldc, char newc)
21363 {
21364 int arr_pos;
21365
21366 for (arr_pos = 0; arr_pos < arr_len; arr_pos++)
21367 {
21368 if (arr[arr_pos] != oldc) continue;
21369
21370 arr[arr_pos] = newc;
21371 }
21372
21373 return (arr_len);
21374 }
21375
21376 int mangle_purgechar (char arr[BLOCK_SIZE], int arr_len, char c)
21377 {
21378 int arr_pos;
21379
21380 int ret_len;
21381
21382 for (ret_len = 0, arr_pos = 0; arr_pos < arr_len; arr_pos++)
21383 {
21384 if (arr[arr_pos] == c) continue;
21385
21386 arr[ret_len] = arr[arr_pos];
21387
21388 ret_len++;
21389 }
21390
21391 return (ret_len);
21392 }
21393
21394 int mangle_dupeblock_prepend (char arr[BLOCK_SIZE], int arr_len, int ulen)
21395 {
21396 if (ulen > arr_len) return (arr_len);
21397
21398 if ((arr_len + ulen) >= BLOCK_SIZE) return (arr_len);
21399
21400 char cs[100] = { 0 };
21401
21402 memcpy (cs, arr, ulen);
21403
21404 int i;
21405
21406 for (i = 0; i < ulen; i++)
21407 {
21408 char c = cs[i];
21409
21410 arr_len = mangle_insert (arr, arr_len, i, c);
21411 }
21412
21413 return (arr_len);
21414 }
21415
21416 int mangle_dupeblock_append (char arr[BLOCK_SIZE], int arr_len, int ulen)
21417 {
21418 if (ulen > arr_len) return (arr_len);
21419
21420 if ((arr_len + ulen) >= BLOCK_SIZE) return (arr_len);
21421
21422 int upos = arr_len - ulen;
21423
21424 int i;
21425
21426 for (i = 0; i < ulen; i++)
21427 {
21428 char c = arr[upos + i];
21429
21430 arr_len = mangle_append (arr, arr_len, c);
21431 }
21432
21433 return (arr_len);
21434 }
21435
21436 int mangle_dupechar_at (char arr[BLOCK_SIZE], int arr_len, int upos, int ulen)
21437 {
21438 if ( arr_len == 0) return (arr_len);
21439 if ((arr_len + ulen) >= BLOCK_SIZE) return (arr_len);
21440
21441 char c = arr[upos];
21442
21443 int i;
21444
21445 for (i = 0; i < ulen; i++)
21446 {
21447 arr_len = mangle_insert (arr, arr_len, upos, c);
21448 }
21449
21450 return (arr_len);
21451 }
21452
21453 int mangle_dupechar (char arr[BLOCK_SIZE], int arr_len)
21454 {
21455 if ( arr_len == 0) return (arr_len);
21456 if ((arr_len + arr_len) >= BLOCK_SIZE) return (arr_len);
21457
21458 int arr_pos;
21459
21460 for (arr_pos = arr_len - 1; arr_pos > -1; arr_pos--)
21461 {
21462 int new_pos = arr_pos * 2;
21463
21464 arr[new_pos] = arr[arr_pos];
21465
21466 arr[new_pos + 1] = arr[arr_pos];
21467 }
21468
21469 return (arr_len * 2);
21470 }
21471
21472 int mangle_switch_at_check (char arr[BLOCK_SIZE], int arr_len, int upos, int upos2)
21473 {
21474 if (upos >= arr_len) return (arr_len);
21475 if (upos2 >= arr_len) return (arr_len);
21476
21477 MANGLE_SWITCH (arr, upos, upos2);
21478
21479 return (arr_len);
21480 }
21481
21482 int mangle_switch_at (char arr[BLOCK_SIZE], int arr_len, int upos, int upos2)
21483 {
21484 MANGLE_SWITCH (arr, upos, upos2);
21485
21486 return (arr_len);
21487 }
21488
21489 int mangle_chr_shiftl (char arr[BLOCK_SIZE], int arr_len, int upos)
21490 {
21491 if (upos >= arr_len) return (arr_len);
21492
21493 arr[upos] <<= 1;
21494
21495 return (arr_len);
21496 }
21497
21498 int mangle_chr_shiftr (char arr[BLOCK_SIZE], int arr_len, int upos)
21499 {
21500 if (upos >= arr_len) return (arr_len);
21501
21502 arr[upos] >>= 1;
21503
21504 return (arr_len);
21505 }
21506
21507 int mangle_chr_incr (char arr[BLOCK_SIZE], int arr_len, int upos)
21508 {
21509 if (upos >= arr_len) return (arr_len);
21510
21511 arr[upos] += 1;
21512
21513 return (arr_len);
21514 }
21515
21516 int mangle_chr_decr (char arr[BLOCK_SIZE], int arr_len, int upos)
21517 {
21518 if (upos >= arr_len) return (arr_len);
21519
21520 arr[upos] -= 1;
21521
21522 return (arr_len);
21523 }
21524
21525 int mangle_title (char arr[BLOCK_SIZE], int arr_len)
21526 {
21527 int upper_next = 1;
21528
21529 int pos;
21530
21531 for (pos = 0; pos < arr_len; pos++)
21532 {
21533 if (arr[pos] == ' ')
21534 {
21535 upper_next = 1;
21536
21537 continue;
21538 }
21539
21540 if (upper_next)
21541 {
21542 upper_next = 0;
21543
21544 MANGLE_UPPER_AT (arr, pos);
21545 }
21546 else
21547 {
21548 MANGLE_LOWER_AT (arr, pos);
21549 }
21550 }
21551
21552 return (arr_len);
21553 }
21554
21555 int generate_random_rule (char rule_buf[RP_RULE_BUFSIZ], u32 rp_gen_func_min, u32 rp_gen_func_max)
21556 {
21557 u32 rp_gen_num = get_random_num (rp_gen_func_min, rp_gen_func_max);
21558
21559 u32 j;
21560
21561 u32 rule_pos = 0;
21562
21563 for (j = 0; j < rp_gen_num; j++)
21564 {
21565 u32 r = 0;
21566 u32 p1 = 0;
21567 u32 p2 = 0;
21568 u32 p3 = 0;
21569
21570 switch ((char) get_random_num (0, 9))
21571 {
21572 case 0:
21573 r = get_random_num (0, sizeof (grp_op_nop));
21574 rule_buf[rule_pos++] = grp_op_nop[r];
21575 break;
21576
21577 case 1:
21578 r = get_random_num (0, sizeof (grp_op_pos_p0));
21579 rule_buf[rule_pos++] = grp_op_pos_p0[r];
21580 p1 = get_random_num (0, sizeof (grp_pos));
21581 rule_buf[rule_pos++] = grp_pos[p1];
21582 break;
21583
21584 case 2:
21585 r = get_random_num (0, sizeof (grp_op_pos_p1));
21586 rule_buf[rule_pos++] = grp_op_pos_p1[r];
21587 p1 = get_random_num (1, 6);
21588 rule_buf[rule_pos++] = grp_pos[p1];
21589 break;
21590
21591 case 3:
21592 r = get_random_num (0, sizeof (grp_op_chr));
21593 rule_buf[rule_pos++] = grp_op_chr[r];
21594 p1 = get_random_num (0x20, 0x7e);
21595 rule_buf[rule_pos++] = (char) p1;
21596 break;
21597
21598 case 4:
21599 r = get_random_num (0, sizeof (grp_op_chr_chr));
21600 rule_buf[rule_pos++] = grp_op_chr_chr[r];
21601 p1 = get_random_num (0x20, 0x7e);
21602 rule_buf[rule_pos++] = (char) p1;
21603 p2 = get_random_num (0x20, 0x7e);
21604 while (p1 == p2)
21605 p2 = get_random_num (0x20, 0x7e);
21606 rule_buf[rule_pos++] = (char) p2;
21607 break;
21608
21609 case 5:
21610 r = get_random_num (0, sizeof (grp_op_pos_chr));
21611 rule_buf[rule_pos++] = grp_op_pos_chr[r];
21612 p1 = get_random_num (0, sizeof (grp_pos));
21613 rule_buf[rule_pos++] = grp_pos[p1];
21614 p2 = get_random_num (0x20, 0x7e);
21615 rule_buf[rule_pos++] = (char) p2;
21616 break;
21617
21618 case 6:
21619 r = get_random_num (0, sizeof (grp_op_pos_pos0));
21620 rule_buf[rule_pos++] = grp_op_pos_pos0[r];
21621 p1 = get_random_num (0, sizeof (grp_pos));
21622 rule_buf[rule_pos++] = grp_pos[p1];
21623 p2 = get_random_num (0, sizeof (grp_pos));
21624 while (p1 == p2)
21625 p2 = get_random_num (0, sizeof (grp_pos));
21626 rule_buf[rule_pos++] = grp_pos[p2];
21627 break;
21628
21629 case 7:
21630 r = get_random_num (0, sizeof (grp_op_pos_pos1));
21631 rule_buf[rule_pos++] = grp_op_pos_pos1[r];
21632 p1 = get_random_num (0, sizeof (grp_pos));
21633 rule_buf[rule_pos++] = grp_pos[p1];
21634 p2 = get_random_num (1, sizeof (grp_pos));
21635 while (p1 == p2)
21636 p2 = get_random_num (1, sizeof (grp_pos));
21637 rule_buf[rule_pos++] = grp_pos[p2];
21638 break;
21639
21640 case 8:
21641 r = get_random_num (0, sizeof (grp_op_pos1_pos2_pos3));
21642 rule_buf[rule_pos++] = grp_op_pos1_pos2_pos3[r];
21643 p1 = get_random_num (0, sizeof (grp_pos));
21644 rule_buf[rule_pos++] = grp_pos[p1];
21645 p2 = get_random_num (1, sizeof (grp_pos));
21646 rule_buf[rule_pos++] = grp_pos[p1];
21647 p3 = get_random_num (0, sizeof (grp_pos));
21648 rule_buf[rule_pos++] = grp_pos[p3];
21649 break;
21650 }
21651 }
21652
21653 return (rule_pos);
21654 }
21655
21656 int _old_apply_rule (char *rule, int rule_len, char in[BLOCK_SIZE], int in_len, char out[BLOCK_SIZE])
21657 {
21658 char mem[BLOCK_SIZE] = { 0 };
21659
21660 if (in == NULL) return (RULE_RC_REJECT_ERROR);
21661
21662 if (out == NULL) return (RULE_RC_REJECT_ERROR);
21663
21664 if (in_len < 1 || in_len > BLOCK_SIZE) return (RULE_RC_REJECT_ERROR);
21665
21666 if (rule_len < 1) return (RULE_RC_REJECT_ERROR);
21667
21668 int out_len = in_len;
21669 int mem_len = in_len;
21670
21671 memcpy (out, in, out_len);
21672
21673 int rule_pos;
21674
21675 for (rule_pos = 0; rule_pos < rule_len; rule_pos++)
21676 {
21677 int upos, upos2;
21678 int ulen;
21679
21680 switch (rule[rule_pos])
21681 {
21682 case ' ':
21683 break;
21684
21685 case RULE_OP_MANGLE_NOOP:
21686 break;
21687
21688 case RULE_OP_MANGLE_LREST:
21689 out_len = mangle_lrest (out, out_len);
21690 break;
21691
21692 case RULE_OP_MANGLE_UREST:
21693 out_len = mangle_urest (out, out_len);
21694 break;
21695
21696 case RULE_OP_MANGLE_LREST_UFIRST:
21697 out_len = mangle_lrest (out, out_len);
21698 if (out_len) MANGLE_UPPER_AT (out, 0);
21699 break;
21700
21701 case RULE_OP_MANGLE_UREST_LFIRST:
21702 out_len = mangle_urest (out, out_len);
21703 if (out_len) MANGLE_LOWER_AT (out, 0);
21704 break;
21705
21706 case RULE_OP_MANGLE_TREST:
21707 out_len = mangle_trest (out, out_len);
21708 break;
21709
21710 case RULE_OP_MANGLE_TOGGLE_AT:
21711 NEXT_RULEPOS (rule_pos);
21712 NEXT_RPTOI (rule, rule_pos, upos);
21713 if (upos < out_len) MANGLE_TOGGLE_AT (out, upos);
21714 break;
21715
21716 case RULE_OP_MANGLE_REVERSE:
21717 out_len = mangle_reverse (out, out_len);
21718 break;
21719
21720 case RULE_OP_MANGLE_DUPEWORD:
21721 out_len = mangle_double (out, out_len);
21722 break;
21723
21724 case RULE_OP_MANGLE_DUPEWORD_TIMES:
21725 NEXT_RULEPOS (rule_pos);
21726 NEXT_RPTOI (rule, rule_pos, ulen);
21727 out_len = mangle_double_times (out, out_len, ulen);
21728 break;
21729
21730 case RULE_OP_MANGLE_REFLECT:
21731 out_len = mangle_reflect (out, out_len);
21732 break;
21733
21734 case RULE_OP_MANGLE_ROTATE_LEFT:
21735 mangle_rotate_left (out, out_len);
21736 break;
21737
21738 case RULE_OP_MANGLE_ROTATE_RIGHT:
21739 mangle_rotate_right (out, out_len);
21740 break;
21741
21742 case RULE_OP_MANGLE_APPEND:
21743 NEXT_RULEPOS (rule_pos);
21744 out_len = mangle_append (out, out_len, rule[rule_pos]);
21745 break;
21746
21747 case RULE_OP_MANGLE_PREPEND:
21748 NEXT_RULEPOS (rule_pos);
21749 out_len = mangle_prepend (out, out_len, rule[rule_pos]);
21750 break;
21751
21752 case RULE_OP_MANGLE_DELETE_FIRST:
21753 out_len = mangle_delete_at (out, out_len, 0);
21754 break;
21755
21756 case RULE_OP_MANGLE_DELETE_LAST:
21757 out_len = mangle_delete_at (out, out_len, (out_len) ? out_len - 1 : 0);
21758 break;
21759
21760 case RULE_OP_MANGLE_DELETE_AT:
21761 NEXT_RULEPOS (rule_pos);
21762 NEXT_RPTOI (rule, rule_pos, upos);
21763 out_len = mangle_delete_at (out, out_len, upos);
21764 break;
21765
21766 case RULE_OP_MANGLE_EXTRACT:
21767 NEXT_RULEPOS (rule_pos);
21768 NEXT_RPTOI (rule, rule_pos, upos);
21769 NEXT_RULEPOS (rule_pos);
21770 NEXT_RPTOI (rule, rule_pos, ulen);
21771 out_len = mangle_extract (out, out_len, upos, ulen);
21772 break;
21773
21774 case RULE_OP_MANGLE_OMIT:
21775 NEXT_RULEPOS (rule_pos);
21776 NEXT_RPTOI (rule, rule_pos, upos);
21777 NEXT_RULEPOS (rule_pos);
21778 NEXT_RPTOI (rule, rule_pos, ulen);
21779 out_len = mangle_omit (out, out_len, upos, ulen);
21780 break;
21781
21782 case RULE_OP_MANGLE_INSERT:
21783 NEXT_RULEPOS (rule_pos);
21784 NEXT_RPTOI (rule, rule_pos, upos);
21785 NEXT_RULEPOS (rule_pos);
21786 out_len = mangle_insert (out, out_len, upos, rule[rule_pos]);
21787 break;
21788
21789 case RULE_OP_MANGLE_OVERSTRIKE:
21790 NEXT_RULEPOS (rule_pos);
21791 NEXT_RPTOI (rule, rule_pos, upos);
21792 NEXT_RULEPOS (rule_pos);
21793 out_len = mangle_overstrike (out, out_len, upos, rule[rule_pos]);
21794 break;
21795
21796 case RULE_OP_MANGLE_TRUNCATE_AT:
21797 NEXT_RULEPOS (rule_pos);
21798 NEXT_RPTOI (rule, rule_pos, upos);
21799 out_len = mangle_truncate_at (out, out_len, upos);
21800 break;
21801
21802 case RULE_OP_MANGLE_REPLACE:
21803 NEXT_RULEPOS (rule_pos);
21804 NEXT_RULEPOS (rule_pos);
21805 out_len = mangle_replace (out, out_len, rule[rule_pos - 1], rule[rule_pos]);
21806 break;
21807
21808 case RULE_OP_MANGLE_PURGECHAR:
21809 NEXT_RULEPOS (rule_pos);
21810 out_len = mangle_purgechar (out, out_len, rule[rule_pos]);
21811 break;
21812
21813 case RULE_OP_MANGLE_TOGGLECASE_REC:
21814 /* todo */
21815 break;
21816
21817 case RULE_OP_MANGLE_DUPECHAR_FIRST:
21818 NEXT_RULEPOS (rule_pos);
21819 NEXT_RPTOI (rule, rule_pos, ulen);
21820 out_len = mangle_dupechar_at (out, out_len, 0, ulen);
21821 break;
21822
21823 case RULE_OP_MANGLE_DUPECHAR_LAST:
21824 NEXT_RULEPOS (rule_pos);
21825 NEXT_RPTOI (rule, rule_pos, ulen);
21826 out_len = mangle_dupechar_at (out, out_len, out_len - 1, ulen);
21827 break;
21828
21829 case RULE_OP_MANGLE_DUPECHAR_ALL:
21830 out_len = mangle_dupechar (out, out_len);
21831 break;
21832
21833 case RULE_OP_MANGLE_DUPEBLOCK_FIRST:
21834 NEXT_RULEPOS (rule_pos);
21835 NEXT_RPTOI (rule, rule_pos, ulen);
21836 out_len = mangle_dupeblock_prepend (out, out_len, ulen);
21837 break;
21838
21839 case RULE_OP_MANGLE_DUPEBLOCK_LAST:
21840 NEXT_RULEPOS (rule_pos);
21841 NEXT_RPTOI (rule, rule_pos, ulen);
21842 out_len = mangle_dupeblock_append (out, out_len, ulen);
21843 break;
21844
21845 case RULE_OP_MANGLE_SWITCH_FIRST:
21846 if (out_len >= 2) mangle_switch_at (out, out_len, 0, 1);
21847 break;
21848
21849 case RULE_OP_MANGLE_SWITCH_LAST:
21850 if (out_len >= 2) mangle_switch_at (out, out_len, out_len - 1, out_len - 2);
21851 break;
21852
21853 case RULE_OP_MANGLE_SWITCH_AT:
21854 NEXT_RULEPOS (rule_pos);
21855 NEXT_RPTOI (rule, rule_pos, upos);
21856 NEXT_RULEPOS (rule_pos);
21857 NEXT_RPTOI (rule, rule_pos, upos2);
21858 out_len = mangle_switch_at_check (out, out_len, upos, upos2);
21859 break;
21860
21861 case RULE_OP_MANGLE_CHR_SHIFTL:
21862 NEXT_RULEPOS (rule_pos);
21863 NEXT_RPTOI (rule, rule_pos, upos);
21864 mangle_chr_shiftl (out, out_len, upos);
21865 break;
21866
21867 case RULE_OP_MANGLE_CHR_SHIFTR:
21868 NEXT_RULEPOS (rule_pos);
21869 NEXT_RPTOI (rule, rule_pos, upos);
21870 mangle_chr_shiftr (out, out_len, upos);
21871 break;
21872
21873 case RULE_OP_MANGLE_CHR_INCR:
21874 NEXT_RULEPOS (rule_pos);
21875 NEXT_RPTOI (rule, rule_pos, upos);
21876 mangle_chr_incr (out, out_len, upos);
21877 break;
21878
21879 case RULE_OP_MANGLE_CHR_DECR:
21880 NEXT_RULEPOS (rule_pos);
21881 NEXT_RPTOI (rule, rule_pos, upos);
21882 mangle_chr_decr (out, out_len, upos);
21883 break;
21884
21885 case RULE_OP_MANGLE_REPLACE_NP1:
21886 NEXT_RULEPOS (rule_pos);
21887 NEXT_RPTOI (rule, rule_pos, upos);
21888 if ((upos >= 0) && ((upos + 1) < out_len)) mangle_overstrike (out, out_len, upos, out[upos + 1]);
21889 break;
21890
21891 case RULE_OP_MANGLE_REPLACE_NM1:
21892 NEXT_RULEPOS (rule_pos);
21893 NEXT_RPTOI (rule, rule_pos, upos);
21894 if ((upos >= 1) && ((upos + 0) < out_len)) mangle_overstrike (out, out_len, upos, out[upos - 1]);
21895 break;
21896
21897 case RULE_OP_MANGLE_TITLE:
21898 out_len = mangle_title (out, out_len);
21899 break;
21900
21901 case RULE_OP_MANGLE_EXTRACT_MEMORY:
21902 if (mem_len < 1) return (RULE_RC_REJECT_ERROR);
21903 NEXT_RULEPOS (rule_pos);
21904 NEXT_RPTOI (rule, rule_pos, upos);
21905 NEXT_RULEPOS (rule_pos);
21906 NEXT_RPTOI (rule, rule_pos, ulen);
21907 NEXT_RULEPOS (rule_pos);
21908 NEXT_RPTOI (rule, rule_pos, upos2);
21909 if ((out_len = mangle_insert_multi (out, out_len, upos2, mem, mem_len, upos, ulen)) < 1) return (out_len);
21910 break;
21911
21912 case RULE_OP_MANGLE_APPEND_MEMORY:
21913 if (mem_len < 1) return (RULE_RC_REJECT_ERROR);
21914 if ((out_len + mem_len) > BLOCK_SIZE) return (RULE_RC_REJECT_ERROR);
21915 memcpy (out + out_len, mem, mem_len);
21916 out_len += mem_len;
21917 break;
21918
21919 case RULE_OP_MANGLE_PREPEND_MEMORY:
21920 if (mem_len < 1) return (RULE_RC_REJECT_ERROR);
21921 if ((mem_len + out_len) > BLOCK_SIZE) return (RULE_RC_REJECT_ERROR);
21922 memcpy (mem + mem_len, out, out_len);
21923 out_len += mem_len;
21924 memcpy (out, mem, out_len);
21925 break;
21926
21927 case RULE_OP_MEMORIZE_WORD:
21928 memcpy (mem, out, out_len);
21929 mem_len = out_len;
21930 break;
21931
21932 case RULE_OP_REJECT_LESS:
21933 NEXT_RULEPOS (rule_pos);
21934 NEXT_RPTOI (rule, rule_pos, upos);
21935 if (out_len > upos) return (RULE_RC_REJECT_ERROR);
21936 break;
21937
21938 case RULE_OP_REJECT_GREATER:
21939 NEXT_RULEPOS (rule_pos);
21940 NEXT_RPTOI (rule, rule_pos, upos);
21941 if (out_len < upos) return (RULE_RC_REJECT_ERROR);
21942 break;
21943
21944 case RULE_OP_REJECT_CONTAIN:
21945 NEXT_RULEPOS (rule_pos);
21946 if (strchr (out, rule[rule_pos]) != NULL) return (RULE_RC_REJECT_ERROR);
21947 break;
21948
21949 case RULE_OP_REJECT_NOT_CONTAIN:
21950 NEXT_RULEPOS (rule_pos);
21951 if (strchr (out, rule[rule_pos]) == NULL) return (RULE_RC_REJECT_ERROR);
21952 break;
21953
21954 case RULE_OP_REJECT_EQUAL_FIRST:
21955 NEXT_RULEPOS (rule_pos);
21956 if (out[0] != rule[rule_pos]) return (RULE_RC_REJECT_ERROR);
21957 break;
21958
21959 case RULE_OP_REJECT_EQUAL_LAST:
21960 NEXT_RULEPOS (rule_pos);
21961 if (out[out_len - 1] != rule[rule_pos]) return (RULE_RC_REJECT_ERROR);
21962 break;
21963
21964 case RULE_OP_REJECT_EQUAL_AT:
21965 NEXT_RULEPOS (rule_pos);
21966 NEXT_RPTOI (rule, rule_pos, upos);
21967 if ((upos + 1) > out_len) return (RULE_RC_REJECT_ERROR);
21968 NEXT_RULEPOS (rule_pos);
21969 if (out[upos] != rule[rule_pos]) return (RULE_RC_REJECT_ERROR);
21970 break;
21971
21972 case RULE_OP_REJECT_CONTAINS:
21973 NEXT_RULEPOS (rule_pos);
21974 NEXT_RPTOI (rule, rule_pos, upos);
21975 if ((upos + 1) > out_len) return (RULE_RC_REJECT_ERROR);
21976 NEXT_RULEPOS (rule_pos);
21977 int c; int cnt; for (c = 0, cnt = 0; c < out_len; c++) if (out[c] == rule[rule_pos]) cnt++;
21978 if (cnt < upos) return (RULE_RC_REJECT_ERROR);
21979 break;
21980
21981 case RULE_OP_REJECT_MEMORY:
21982 if ((out_len == mem_len) && (memcmp (out, mem, out_len) == 0)) return (RULE_RC_REJECT_ERROR);
21983 break;
21984
21985 default:
21986 return (RULE_RC_SYNTAX_ERROR);
21987 break;
21988 }
21989 }
21990
21991 memset (out + out_len, 0, BLOCK_SIZE - out_len);
21992
21993 return (out_len);
21994 }