Prepare for VeraCrypt integration
[hashcat.git] / src / shared.c
1 /**
2 * Authors.....: Jens Steube <jens.steube@gmail.com>
3 * Gabriele Gristina <matrix@hashcat.net>
4 * magnum <john.magnum@hushmail.com>
5 *
6 * License.....: MIT
7 */
8
9 #ifdef OSX
10 #include <stdio.h>
11 #endif
12
13 #include <shared.h>
14 #include <limits.h>
15
16 /**
17 * basic bit handling
18 */
19
20 u32 is_power_of_2(u32 v)
21 {
22 return (v && !(v & (v - 1)));
23 }
24
25 u32 rotl32 (const u32 a, const u32 n)
26 {
27 return ((a << n) | (a >> (32 - n)));
28 }
29
30 u32 rotr32 (const u32 a, const u32 n)
31 {
32 return ((a >> n) | (a << (32 - n)));
33 }
34
35 u64 rotl64 (const u64 a, const u64 n)
36 {
37 return ((a << n) | (a >> (64 - n)));
38 }
39
40 u64 rotr64 (const u64 a, const u64 n)
41 {
42 return ((a >> n) | (a << (64 - n)));
43 }
44
45 u32 byte_swap_32 (const u32 n)
46 {
47 return (n & 0xff000000) >> 24
48 | (n & 0x00ff0000) >> 8
49 | (n & 0x0000ff00) << 8
50 | (n & 0x000000ff) << 24;
51 }
52
53 u64 byte_swap_64 (const u64 n)
54 {
55 return (n & 0xff00000000000000ULL) >> 56
56 | (n & 0x00ff000000000000ULL) >> 40
57 | (n & 0x0000ff0000000000ULL) >> 24
58 | (n & 0x000000ff00000000ULL) >> 8
59 | (n & 0x00000000ff000000ULL) << 8
60 | (n & 0x0000000000ff0000ULL) << 24
61 | (n & 0x000000000000ff00ULL) << 40
62 | (n & 0x00000000000000ffULL) << 56;
63 }
64
65 /**
66 * ciphers for use on cpu
67 */
68
69 #include "cpu-des.c"
70 #include "cpu-aes.c"
71
72 /**
73 * hashes for use on cpu
74 */
75
76 #include "cpu-md5.c"
77 #include "cpu-sha1.c"
78 #include "cpu-sha256.c"
79
80 /**
81 * logging
82 */
83
84 int last_len = 0;
85
86 void log_final (FILE *fp, const char *fmt, va_list ap)
87 {
88 if (last_len)
89 {
90 fputc ('\r', fp);
91
92 for (int i = 0; i < last_len; i++)
93 {
94 fputc (' ', fp);
95 }
96
97 fputc ('\r', fp);
98 }
99
100 char s[4096] = { 0 };
101
102 int max_len = (int) sizeof (s);
103
104 int len = vsnprintf (s, max_len, fmt, ap);
105
106 if (len > max_len) len = max_len;
107
108 fwrite (s, len, 1, fp);
109
110 fflush (fp);
111
112 last_len = len;
113 }
114
115 void log_out_nn (FILE *fp, const char *fmt, ...)
116 {
117 if (SUPPRESS_OUTPUT) return;
118
119 va_list ap;
120
121 va_start (ap, fmt);
122
123 log_final (fp, fmt, ap);
124
125 va_end (ap);
126 }
127
128 void log_info_nn (const char *fmt, ...)
129 {
130 if (SUPPRESS_OUTPUT) return;
131
132 va_list ap;
133
134 va_start (ap, fmt);
135
136 log_final (stdout, fmt, ap);
137
138 va_end (ap);
139 }
140
141 void log_error_nn (const char *fmt, ...)
142 {
143 if (SUPPRESS_OUTPUT) return;
144
145 va_list ap;
146
147 va_start (ap, fmt);
148
149 log_final (stderr, fmt, ap);
150
151 va_end (ap);
152 }
153
154 void log_out (FILE *fp, const char *fmt, ...)
155 {
156 if (SUPPRESS_OUTPUT) return;
157
158 va_list ap;
159
160 va_start (ap, fmt);
161
162 log_final (fp, fmt, ap);
163
164 va_end (ap);
165
166 fputc ('\n', fp);
167
168 last_len = 0;
169 }
170
171 void log_info (const char *fmt, ...)
172 {
173 if (SUPPRESS_OUTPUT) return;
174
175 va_list ap;
176
177 va_start (ap, fmt);
178
179 log_final (stdout, fmt, ap);
180
181 va_end (ap);
182
183 fputc ('\n', stdout);
184
185 last_len = 0;
186 }
187
188 void log_error (const char *fmt, ...)
189 {
190 if (SUPPRESS_OUTPUT) return;
191
192 fputc ('\n', stderr);
193 fputc ('\n', stderr);
194
195 va_list ap;
196
197 va_start (ap, fmt);
198
199 log_final (stderr, fmt, ap);
200
201 va_end (ap);
202
203 fputc ('\n', stderr);
204 fputc ('\n', stderr);
205
206 last_len = 0;
207 }
208
209 /**
210 * converter
211 */
212
213 u8 int_to_base32 (const u8 c)
214 {
215 static const u8 tbl[0x20] =
216 {
217 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50,
218 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
219 };
220
221 return tbl[c];
222 }
223
224 u8 base32_to_int (const u8 c)
225 {
226 if ((c >= 'A') && (c <= 'Z')) return c - 'A';
227 else if ((c >= '2') && (c <= '7')) return c - '2' + 26;
228
229 return 0;
230 }
231
232 u8 int_to_itoa32 (const u8 c)
233 {
234 static const u8 tbl[0x20] =
235 {
236 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
237 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
238 };
239
240 return tbl[c];
241 }
242
243 u8 itoa32_to_int (const u8 c)
244 {
245 if ((c >= '0') && (c <= '9')) return c - '0';
246 else if ((c >= 'a') && (c <= 'v')) return c - 'a' + 10;
247
248 return 0;
249 }
250
251 u8 int_to_itoa64 (const u8 c)
252 {
253 static const u8 tbl[0x40] =
254 {
255 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44,
256 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54,
257 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a,
258 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a,
259 };
260
261 return tbl[c];
262 }
263
264 u8 itoa64_to_int (const u8 c)
265 {
266 static const u8 tbl[0x100] =
267 {
268 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21,
269 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31,
270 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x00, 0x01,
271 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
272 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a,
273 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x20, 0x21, 0x22, 0x23, 0x24,
274 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34,
275 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x00, 0x01, 0x02, 0x03, 0x04,
276 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14,
277 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24,
278 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34,
279 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x00, 0x01, 0x02, 0x03, 0x04,
280 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14,
281 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24,
282 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34,
283 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x00, 0x01, 0x02, 0x03, 0x04,
284 };
285
286 return tbl[c];
287 }
288
289 u8 int_to_base64 (const u8 c)
290 {
291 static const u8 tbl[0x40] =
292 {
293 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50,
294 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
295 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
296 0x77, 0x78, 0x79, 0x7a, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2b, 0x2f,
297 };
298
299 return tbl[c];
300 }
301
302 u8 base64_to_int (const u8 c)
303 {
304 static const u8 tbl[0x100] =
305 {
306 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
307 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
308 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x3f,
309 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
310 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
311 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00,
312 0x00, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
313 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00,
314 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
315 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
316 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
317 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
318 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
319 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
320 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
321 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
322 };
323
324 return tbl[c];
325 }
326
327 u8 int_to_bf64 (const u8 c)
328 {
329 static const u8 tbl[0x40] =
330 {
331 0x2e, 0x2f, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e,
332 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x61, 0x62, 0x63, 0x64,
333 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74,
334 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
335 };
336
337 return tbl[c];
338 }
339
340 u8 bf64_to_int (const u8 c)
341 {
342 static const u8 tbl[0x100] =
343 {
344 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
345 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
346 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
347 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
348 0x00, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
349 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x00, 0x00, 0x00, 0x00, 0x00,
350 0x00, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a,
351 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x00, 0x00, 0x00, 0x00, 0x00,
352 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
353 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
354 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
355 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
356 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
357 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
358 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
359 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
360 };
361
362 return tbl[c];
363 }
364
365 u8 int_to_lotus64 (const u8 c)
366 {
367 if (c < 10) return '0' + c;
368 else if (c < 36) return 'A' + c - 10;
369 else if (c < 62) return 'a' + c - 36;
370 else if (c == 62) return '+';
371 else if (c == 63) return '/';
372
373 return 0;
374 }
375
376 u8 lotus64_to_int (const u8 c)
377 {
378 if ((c >= '0') && (c <= '9')) return c - '0';
379 else if ((c >= 'A') && (c <= 'Z')) return c - 'A' + 10;
380 else if ((c >= 'a') && (c <= 'z')) return c - 'a' + 36;
381 else if (c == '+') return 62;
382 else if (c == '/') return 63;
383 else
384
385 return 0;
386 }
387
388 int base32_decode (u8 (*f) (const u8), const u8 *in_buf, int in_len, u8 *out_buf)
389 {
390 const u8 *in_ptr = in_buf;
391
392 u8 *out_ptr = out_buf;
393
394 for (int i = 0; i < in_len; i += 8)
395 {
396 const u8 out_val0 = f (in_ptr[0] & 0x7f);
397 const u8 out_val1 = f (in_ptr[1] & 0x7f);
398 const u8 out_val2 = f (in_ptr[2] & 0x7f);
399 const u8 out_val3 = f (in_ptr[3] & 0x7f);
400 const u8 out_val4 = f (in_ptr[4] & 0x7f);
401 const u8 out_val5 = f (in_ptr[5] & 0x7f);
402 const u8 out_val6 = f (in_ptr[6] & 0x7f);
403 const u8 out_val7 = f (in_ptr[7] & 0x7f);
404
405 out_ptr[0] = ((out_val0 << 3) & 0xf8) | ((out_val1 >> 2) & 0x07);
406 out_ptr[1] = ((out_val1 << 6) & 0xc0) | ((out_val2 << 1) & 0x3e) | ((out_val3 >> 4) & 0x01);
407 out_ptr[2] = ((out_val3 << 4) & 0xf0) | ((out_val4 >> 1) & 0x0f);
408 out_ptr[3] = ((out_val4 << 7) & 0x80) | ((out_val5 << 2) & 0x7c) | ((out_val6 >> 3) & 0x03);
409 out_ptr[4] = ((out_val6 << 5) & 0xe0) | ((out_val7 >> 0) & 0x1f);
410
411 in_ptr += 8;
412 out_ptr += 5;
413 }
414
415 for (int i = 0; i < in_len; i++)
416 {
417 if (in_buf[i] != '=') continue;
418
419 in_len = i;
420 }
421
422 int out_len = (in_len * 5) / 8;
423
424 return out_len;
425 }
426
427 int base32_encode (u8 (*f) (const u8), const u8 *in_buf, int in_len, u8 *out_buf)
428 {
429 const u8 *in_ptr = in_buf;
430
431 u8 *out_ptr = out_buf;
432
433 for (int i = 0; i < in_len; i += 5)
434 {
435 const u8 out_val0 = f ( ((in_ptr[0] >> 3) & 0x1f));
436 const u8 out_val1 = f (((in_ptr[0] << 2) & 0x1c) | ((in_ptr[1] >> 6) & 0x03));
437 const u8 out_val2 = f ( ((in_ptr[1] >> 1) & 0x1f));
438 const u8 out_val3 = f (((in_ptr[1] << 4) & 0x10) | ((in_ptr[2] >> 4) & 0x0f));
439 const u8 out_val4 = f (((in_ptr[2] << 1) & 0x1e) | ((in_ptr[3] >> 7) & 0x01));
440 const u8 out_val5 = f ( ((in_ptr[3] >> 2) & 0x1f));
441 const u8 out_val6 = f (((in_ptr[3] << 3) & 0x18) | ((in_ptr[4] >> 5) & 0x07));
442 const u8 out_val7 = f ( ((in_ptr[4] >> 0) & 0x1f));
443
444 out_ptr[0] = out_val0 & 0x7f;
445 out_ptr[1] = out_val1 & 0x7f;
446 out_ptr[2] = out_val2 & 0x7f;
447 out_ptr[3] = out_val3 & 0x7f;
448 out_ptr[4] = out_val4 & 0x7f;
449 out_ptr[5] = out_val5 & 0x7f;
450 out_ptr[6] = out_val6 & 0x7f;
451 out_ptr[7] = out_val7 & 0x7f;
452
453 in_ptr += 5;
454 out_ptr += 8;
455 }
456
457 int out_len = (int) (((0.5 + (float) in_len) * 8) / 5); // ceil (in_len * 8 / 5)
458
459 while (out_len % 8)
460 {
461 out_buf[out_len] = '=';
462
463 out_len++;
464 }
465
466 return out_len;
467 }
468
469 int base64_decode (u8 (*f) (const u8), const u8 *in_buf, int in_len, u8 *out_buf)
470 {
471 const u8 *in_ptr = in_buf;
472
473 u8 *out_ptr = out_buf;
474
475 for (int i = 0; i < in_len; i += 4)
476 {
477 const u8 out_val0 = f (in_ptr[0] & 0x7f);
478 const u8 out_val1 = f (in_ptr[1] & 0x7f);
479 const u8 out_val2 = f (in_ptr[2] & 0x7f);
480 const u8 out_val3 = f (in_ptr[3] & 0x7f);
481
482 out_ptr[0] = ((out_val0 << 2) & 0xfc) | ((out_val1 >> 4) & 0x03);
483 out_ptr[1] = ((out_val1 << 4) & 0xf0) | ((out_val2 >> 2) & 0x0f);
484 out_ptr[2] = ((out_val2 << 6) & 0xc0) | ((out_val3 >> 0) & 0x3f);
485
486 in_ptr += 4;
487 out_ptr += 3;
488 }
489
490 for (int i = 0; i < in_len; i++)
491 {
492 if (in_buf[i] != '=') continue;
493
494 in_len = i;
495 }
496
497 int out_len = (in_len * 6) / 8;
498
499 return out_len;
500 }
501
502 int base64_encode (u8 (*f) (const u8), const u8 *in_buf, int in_len, u8 *out_buf)
503 {
504 const u8 *in_ptr = in_buf;
505
506 u8 *out_ptr = out_buf;
507
508 for (int i = 0; i < in_len; i += 3)
509 {
510 const u8 out_val0 = f ( ((in_ptr[0] >> 2) & 0x3f));
511 const u8 out_val1 = f (((in_ptr[0] << 4) & 0x30) | ((in_ptr[1] >> 4) & 0x0f));
512 const u8 out_val2 = f (((in_ptr[1] << 2) & 0x3c) | ((in_ptr[2] >> 6) & 0x03));
513 const u8 out_val3 = f ( ((in_ptr[2] >> 0) & 0x3f));
514
515 out_ptr[0] = out_val0 & 0x7f;
516 out_ptr[1] = out_val1 & 0x7f;
517 out_ptr[2] = out_val2 & 0x7f;
518 out_ptr[3] = out_val3 & 0x7f;
519
520 in_ptr += 3;
521 out_ptr += 4;
522 }
523
524 int out_len = (int) (((0.5 + (float) in_len) * 8) / 6); // ceil (in_len * 8 / 6)
525
526 while (out_len % 4)
527 {
528 out_buf[out_len] = '=';
529
530 out_len++;
531 }
532
533 return out_len;
534 }
535
536 int is_valid_hex_char (const u8 c)
537 {
538 if ((c >= '0') && (c <= '9')) return 1;
539 if ((c >= 'A') && (c <= 'F')) return 1;
540 if ((c >= 'a') && (c <= 'f')) return 1;
541
542 return 0;
543 }
544
545 u8 hex_convert (const u8 c)
546 {
547 return (c & 15) + (c >> 6) * 9;
548 }
549
550 u8 hex_to_u8 (const u8 hex[2])
551 {
552 u8 v = 0;
553
554 v |= (hex_convert (hex[1]) << 0);
555 v |= (hex_convert (hex[0]) << 4);
556
557 return (v);
558 }
559
560 u32 hex_to_u32 (const u8 hex[8])
561 {
562 u32 v = 0;
563
564 v |= ((u32) hex_convert (hex[7])) << 0;
565 v |= ((u32) hex_convert (hex[6])) << 4;
566 v |= ((u32) hex_convert (hex[5])) << 8;
567 v |= ((u32) hex_convert (hex[4])) << 12;
568 v |= ((u32) hex_convert (hex[3])) << 16;
569 v |= ((u32) hex_convert (hex[2])) << 20;
570 v |= ((u32) hex_convert (hex[1])) << 24;
571 v |= ((u32) hex_convert (hex[0])) << 28;
572
573 return (v);
574 }
575
576 u64 hex_to_u64 (const u8 hex[16])
577 {
578 u64 v = 0;
579
580 v |= ((u64) hex_convert (hex[15]) << 0);
581 v |= ((u64) hex_convert (hex[14]) << 4);
582 v |= ((u64) hex_convert (hex[13]) << 8);
583 v |= ((u64) hex_convert (hex[12]) << 12);
584 v |= ((u64) hex_convert (hex[11]) << 16);
585 v |= ((u64) hex_convert (hex[10]) << 20);
586 v |= ((u64) hex_convert (hex[ 9]) << 24);
587 v |= ((u64) hex_convert (hex[ 8]) << 28);
588 v |= ((u64) hex_convert (hex[ 7]) << 32);
589 v |= ((u64) hex_convert (hex[ 6]) << 36);
590 v |= ((u64) hex_convert (hex[ 5]) << 40);
591 v |= ((u64) hex_convert (hex[ 4]) << 44);
592 v |= ((u64) hex_convert (hex[ 3]) << 48);
593 v |= ((u64) hex_convert (hex[ 2]) << 52);
594 v |= ((u64) hex_convert (hex[ 1]) << 56);
595 v |= ((u64) hex_convert (hex[ 0]) << 60);
596
597 return (v);
598 }
599
600 void bin_to_hex_lower (const u32 v, u8 hex[8])
601 {
602 hex[0] = v >> 28 & 15;
603 hex[1] = v >> 24 & 15;
604 hex[2] = v >> 20 & 15;
605 hex[3] = v >> 16 & 15;
606 hex[4] = v >> 12 & 15;
607 hex[5] = v >> 8 & 15;
608 hex[6] = v >> 4 & 15;
609 hex[7] = v >> 0 & 15;
610
611 u32 add;
612
613 hex[0] += 6; add = ((hex[0] & 0x10) >> 4) * 39; hex[0] += 42 + add;
614 hex[1] += 6; add = ((hex[1] & 0x10) >> 4) * 39; hex[1] += 42 + add;
615 hex[2] += 6; add = ((hex[2] & 0x10) >> 4) * 39; hex[2] += 42 + add;
616 hex[3] += 6; add = ((hex[3] & 0x10) >> 4) * 39; hex[3] += 42 + add;
617 hex[4] += 6; add = ((hex[4] & 0x10) >> 4) * 39; hex[4] += 42 + add;
618 hex[5] += 6; add = ((hex[5] & 0x10) >> 4) * 39; hex[5] += 42 + add;
619 hex[6] += 6; add = ((hex[6] & 0x10) >> 4) * 39; hex[6] += 42 + add;
620 hex[7] += 6; add = ((hex[7] & 0x10) >> 4) * 39; hex[7] += 42 + add;
621 }
622
623 /**
624 * decoder
625 */
626
627 static void AES128_decrypt_cbc (const u32 key[4], const u32 iv[4], const u32 in[16], u32 out[16])
628 {
629 AES_KEY skey;
630
631 AES_set_decrypt_key ((const u8 *) key, 128, &skey);
632
633 u32 _iv[4] = { 0 };
634
635 _iv[0] = iv[0];
636 _iv[1] = iv[1];
637 _iv[2] = iv[2];
638 _iv[3] = iv[3];
639
640 for (int i = 0; i < 16; i += 4)
641 {
642 u32 _in[4] = { 0 };
643 u32 _out[4] = { 0 };
644
645 _in[0] = in[i + 0];
646 _in[1] = in[i + 1];
647 _in[2] = in[i + 2];
648 _in[3] = in[i + 3];
649
650 AES_decrypt (&skey, (const u8 *) _in, (u8 *) _out);
651
652 _out[0] ^= _iv[0];
653 _out[1] ^= _iv[1];
654 _out[2] ^= _iv[2];
655 _out[3] ^= _iv[3];
656
657 out[i + 0] = _out[0];
658 out[i + 1] = _out[1];
659 out[i + 2] = _out[2];
660 out[i + 3] = _out[3];
661
662 _iv[0] = _in[0];
663 _iv[1] = _in[1];
664 _iv[2] = _in[2];
665 _iv[3] = _in[3];
666 }
667 }
668
669 static void juniper_decrypt_hash (char *in, char *out)
670 {
671 // base64 decode
672
673 u8 base64_buf[100] = { 0 };
674
675 base64_decode (base64_to_int, (const u8 *) in, DISPLAY_LEN_MIN_501, base64_buf);
676
677 // iv stuff
678
679 u32 juniper_iv[4] = { 0 };
680
681 memcpy (juniper_iv, base64_buf, 12);
682
683 memcpy (out, juniper_iv, 12);
684
685 // reversed key
686
687 u32 juniper_key[4] = { 0 };
688
689 juniper_key[0] = byte_swap_32 (0xa6707a7e);
690 juniper_key[1] = byte_swap_32 (0x8df91059);
691 juniper_key[2] = byte_swap_32 (0xdea70ae5);
692 juniper_key[3] = byte_swap_32 (0x2f9c2442);
693
694 // AES decrypt
695
696 u32 *in_ptr = (u32 *) (base64_buf + 12);
697 u32 *out_ptr = (u32 *) (out + 12);
698
699 AES128_decrypt_cbc (juniper_key, juniper_iv, in_ptr, out_ptr);
700 }
701
702 void phpass_decode (u8 digest[16], u8 buf[22])
703 {
704 int l;
705
706 l = itoa64_to_int (buf[ 0]) << 0;
707 l |= itoa64_to_int (buf[ 1]) << 6;
708 l |= itoa64_to_int (buf[ 2]) << 12;
709 l |= itoa64_to_int (buf[ 3]) << 18;
710
711 digest[ 0] = (l >> 0) & 0xff;
712 digest[ 1] = (l >> 8) & 0xff;
713 digest[ 2] = (l >> 16) & 0xff;
714
715 l = itoa64_to_int (buf[ 4]) << 0;
716 l |= itoa64_to_int (buf[ 5]) << 6;
717 l |= itoa64_to_int (buf[ 6]) << 12;
718 l |= itoa64_to_int (buf[ 7]) << 18;
719
720 digest[ 3] = (l >> 0) & 0xff;
721 digest[ 4] = (l >> 8) & 0xff;
722 digest[ 5] = (l >> 16) & 0xff;
723
724 l = itoa64_to_int (buf[ 8]) << 0;
725 l |= itoa64_to_int (buf[ 9]) << 6;
726 l |= itoa64_to_int (buf[10]) << 12;
727 l |= itoa64_to_int (buf[11]) << 18;
728
729 digest[ 6] = (l >> 0) & 0xff;
730 digest[ 7] = (l >> 8) & 0xff;
731 digest[ 8] = (l >> 16) & 0xff;
732
733 l = itoa64_to_int (buf[12]) << 0;
734 l |= itoa64_to_int (buf[13]) << 6;
735 l |= itoa64_to_int (buf[14]) << 12;
736 l |= itoa64_to_int (buf[15]) << 18;
737
738 digest[ 9] = (l >> 0) & 0xff;
739 digest[10] = (l >> 8) & 0xff;
740 digest[11] = (l >> 16) & 0xff;
741
742 l = itoa64_to_int (buf[16]) << 0;
743 l |= itoa64_to_int (buf[17]) << 6;
744 l |= itoa64_to_int (buf[18]) << 12;
745 l |= itoa64_to_int (buf[19]) << 18;
746
747 digest[12] = (l >> 0) & 0xff;
748 digest[13] = (l >> 8) & 0xff;
749 digest[14] = (l >> 16) & 0xff;
750
751 l = itoa64_to_int (buf[20]) << 0;
752 l |= itoa64_to_int (buf[21]) << 6;
753
754 digest[15] = (l >> 0) & 0xff;
755 }
756
757 void phpass_encode (u8 digest[16], u8 buf[22])
758 {
759 int l;
760
761 l = (digest[ 0] << 0) | (digest[ 1] << 8) | (digest[ 2] << 16);
762
763 buf[ 0] = int_to_itoa64 (l & 0x3f); l >>= 6;
764 buf[ 1] = int_to_itoa64 (l & 0x3f); l >>= 6;
765 buf[ 2] = int_to_itoa64 (l & 0x3f); l >>= 6;
766 buf[ 3] = int_to_itoa64 (l & 0x3f);
767
768 l = (digest[ 3] << 0) | (digest[ 4] << 8) | (digest[ 5] << 16);
769
770 buf[ 4] = int_to_itoa64 (l & 0x3f); l >>= 6;
771 buf[ 5] = int_to_itoa64 (l & 0x3f); l >>= 6;
772 buf[ 6] = int_to_itoa64 (l & 0x3f); l >>= 6;
773 buf[ 7] = int_to_itoa64 (l & 0x3f);
774
775 l = (digest[ 6] << 0) | (digest[ 7] << 8) | (digest[ 8] << 16);
776
777 buf[ 8] = int_to_itoa64 (l & 0x3f); l >>= 6;
778 buf[ 9] = int_to_itoa64 (l & 0x3f); l >>= 6;
779 buf[10] = int_to_itoa64 (l & 0x3f); l >>= 6;
780 buf[11] = int_to_itoa64 (l & 0x3f);
781
782 l = (digest[ 9] << 0) | (digest[10] << 8) | (digest[11] << 16);
783
784 buf[12] = int_to_itoa64 (l & 0x3f); l >>= 6;
785 buf[13] = int_to_itoa64 (l & 0x3f); l >>= 6;
786 buf[14] = int_to_itoa64 (l & 0x3f); l >>= 6;
787 buf[15] = int_to_itoa64 (l & 0x3f);
788
789 l = (digest[12] << 0) | (digest[13] << 8) | (digest[14] << 16);
790
791 buf[16] = int_to_itoa64 (l & 0x3f); l >>= 6;
792 buf[17] = int_to_itoa64 (l & 0x3f); l >>= 6;
793 buf[18] = int_to_itoa64 (l & 0x3f); l >>= 6;
794 buf[19] = int_to_itoa64 (l & 0x3f);
795
796 l = (digest[15] << 0);
797
798 buf[20] = int_to_itoa64 (l & 0x3f); l >>= 6;
799 buf[21] = int_to_itoa64 (l & 0x3f);
800 }
801
802 void md5crypt_decode (u8 digest[16], u8 buf[22])
803 {
804 int l;
805
806 l = itoa64_to_int (buf[ 0]) << 0;
807 l |= itoa64_to_int (buf[ 1]) << 6;
808 l |= itoa64_to_int (buf[ 2]) << 12;
809 l |= itoa64_to_int (buf[ 3]) << 18;
810
811 digest[ 0] = (l >> 16) & 0xff;
812 digest[ 6] = (l >> 8) & 0xff;
813 digest[12] = (l >> 0) & 0xff;
814
815 l = itoa64_to_int (buf[ 4]) << 0;
816 l |= itoa64_to_int (buf[ 5]) << 6;
817 l |= itoa64_to_int (buf[ 6]) << 12;
818 l |= itoa64_to_int (buf[ 7]) << 18;
819
820 digest[ 1] = (l >> 16) & 0xff;
821 digest[ 7] = (l >> 8) & 0xff;
822 digest[13] = (l >> 0) & 0xff;
823
824 l = itoa64_to_int (buf[ 8]) << 0;
825 l |= itoa64_to_int (buf[ 9]) << 6;
826 l |= itoa64_to_int (buf[10]) << 12;
827 l |= itoa64_to_int (buf[11]) << 18;
828
829 digest[ 2] = (l >> 16) & 0xff;
830 digest[ 8] = (l >> 8) & 0xff;
831 digest[14] = (l >> 0) & 0xff;
832
833 l = itoa64_to_int (buf[12]) << 0;
834 l |= itoa64_to_int (buf[13]) << 6;
835 l |= itoa64_to_int (buf[14]) << 12;
836 l |= itoa64_to_int (buf[15]) << 18;
837
838 digest[ 3] = (l >> 16) & 0xff;
839 digest[ 9] = (l >> 8) & 0xff;
840 digest[15] = (l >> 0) & 0xff;
841
842 l = itoa64_to_int (buf[16]) << 0;
843 l |= itoa64_to_int (buf[17]) << 6;
844 l |= itoa64_to_int (buf[18]) << 12;
845 l |= itoa64_to_int (buf[19]) << 18;
846
847 digest[ 4] = (l >> 16) & 0xff;
848 digest[10] = (l >> 8) & 0xff;
849 digest[ 5] = (l >> 0) & 0xff;
850
851 l = itoa64_to_int (buf[20]) << 0;
852 l |= itoa64_to_int (buf[21]) << 6;
853
854 digest[11] = (l >> 0) & 0xff;
855 }
856
857 void md5crypt_encode (u8 digest[16], u8 buf[22])
858 {
859 int l;
860
861 l = (digest[ 0] << 16) | (digest[ 6] << 8) | (digest[12] << 0);
862
863 buf[ 0] = int_to_itoa64 (l & 0x3f); l >>= 6;
864 buf[ 1] = int_to_itoa64 (l & 0x3f); l >>= 6;
865 buf[ 2] = int_to_itoa64 (l & 0x3f); l >>= 6;
866 buf[ 3] = int_to_itoa64 (l & 0x3f); l >>= 6;
867
868 l = (digest[ 1] << 16) | (digest[ 7] << 8) | (digest[13] << 0);
869
870 buf[ 4] = int_to_itoa64 (l & 0x3f); l >>= 6;
871 buf[ 5] = int_to_itoa64 (l & 0x3f); l >>= 6;
872 buf[ 6] = int_to_itoa64 (l & 0x3f); l >>= 6;
873 buf[ 7] = int_to_itoa64 (l & 0x3f); l >>= 6;
874
875 l = (digest[ 2] << 16) | (digest[ 8] << 8) | (digest[14] << 0);
876
877 buf[ 8] = int_to_itoa64 (l & 0x3f); l >>= 6;
878 buf[ 9] = int_to_itoa64 (l & 0x3f); l >>= 6;
879 buf[10] = int_to_itoa64 (l & 0x3f); l >>= 6;
880 buf[11] = int_to_itoa64 (l & 0x3f); l >>= 6;
881
882 l = (digest[ 3] << 16) | (digest[ 9] << 8) | (digest[15] << 0);
883
884 buf[12] = int_to_itoa64 (l & 0x3f); l >>= 6;
885 buf[13] = int_to_itoa64 (l & 0x3f); l >>= 6;
886 buf[14] = int_to_itoa64 (l & 0x3f); l >>= 6;
887 buf[15] = int_to_itoa64 (l & 0x3f); l >>= 6;
888
889 l = (digest[ 4] << 16) | (digest[10] << 8) | (digest[ 5] << 0);
890
891 buf[16] = int_to_itoa64 (l & 0x3f); l >>= 6;
892 buf[17] = int_to_itoa64 (l & 0x3f); l >>= 6;
893 buf[18] = int_to_itoa64 (l & 0x3f); l >>= 6;
894 buf[19] = int_to_itoa64 (l & 0x3f); l >>= 6;
895
896 l = (digest[11] << 0);
897
898 buf[20] = int_to_itoa64 (l & 0x3f); l >>= 6;
899 buf[21] = int_to_itoa64 (l & 0x3f); l >>= 6;
900 }
901
902 void sha512crypt_decode (u8 digest[64], u8 buf[86])
903 {
904 int l;
905
906 l = itoa64_to_int (buf[ 0]) << 0;
907 l |= itoa64_to_int (buf[ 1]) << 6;
908 l |= itoa64_to_int (buf[ 2]) << 12;
909 l |= itoa64_to_int (buf[ 3]) << 18;
910
911 digest[ 0] = (l >> 16) & 0xff;
912 digest[21] = (l >> 8) & 0xff;
913 digest[42] = (l >> 0) & 0xff;
914
915 l = itoa64_to_int (buf[ 4]) << 0;
916 l |= itoa64_to_int (buf[ 5]) << 6;
917 l |= itoa64_to_int (buf[ 6]) << 12;
918 l |= itoa64_to_int (buf[ 7]) << 18;
919
920 digest[22] = (l >> 16) & 0xff;
921 digest[43] = (l >> 8) & 0xff;
922 digest[ 1] = (l >> 0) & 0xff;
923
924 l = itoa64_to_int (buf[ 8]) << 0;
925 l |= itoa64_to_int (buf[ 9]) << 6;
926 l |= itoa64_to_int (buf[10]) << 12;
927 l |= itoa64_to_int (buf[11]) << 18;
928
929 digest[44] = (l >> 16) & 0xff;
930 digest[ 2] = (l >> 8) & 0xff;
931 digest[23] = (l >> 0) & 0xff;
932
933 l = itoa64_to_int (buf[12]) << 0;
934 l |= itoa64_to_int (buf[13]) << 6;
935 l |= itoa64_to_int (buf[14]) << 12;
936 l |= itoa64_to_int (buf[15]) << 18;
937
938 digest[ 3] = (l >> 16) & 0xff;
939 digest[24] = (l >> 8) & 0xff;
940 digest[45] = (l >> 0) & 0xff;
941
942 l = itoa64_to_int (buf[16]) << 0;
943 l |= itoa64_to_int (buf[17]) << 6;
944 l |= itoa64_to_int (buf[18]) << 12;
945 l |= itoa64_to_int (buf[19]) << 18;
946
947 digest[25] = (l >> 16) & 0xff;
948 digest[46] = (l >> 8) & 0xff;
949 digest[ 4] = (l >> 0) & 0xff;
950
951 l = itoa64_to_int (buf[20]) << 0;
952 l |= itoa64_to_int (buf[21]) << 6;
953 l |= itoa64_to_int (buf[22]) << 12;
954 l |= itoa64_to_int (buf[23]) << 18;
955
956 digest[47] = (l >> 16) & 0xff;
957 digest[ 5] = (l >> 8) & 0xff;
958 digest[26] = (l >> 0) & 0xff;
959
960 l = itoa64_to_int (buf[24]) << 0;
961 l |= itoa64_to_int (buf[25]) << 6;
962 l |= itoa64_to_int (buf[26]) << 12;
963 l |= itoa64_to_int (buf[27]) << 18;
964
965 digest[ 6] = (l >> 16) & 0xff;
966 digest[27] = (l >> 8) & 0xff;
967 digest[48] = (l >> 0) & 0xff;
968
969 l = itoa64_to_int (buf[28]) << 0;
970 l |= itoa64_to_int (buf[29]) << 6;
971 l |= itoa64_to_int (buf[30]) << 12;
972 l |= itoa64_to_int (buf[31]) << 18;
973
974 digest[28] = (l >> 16) & 0xff;
975 digest[49] = (l >> 8) & 0xff;
976 digest[ 7] = (l >> 0) & 0xff;
977
978 l = itoa64_to_int (buf[32]) << 0;
979 l |= itoa64_to_int (buf[33]) << 6;
980 l |= itoa64_to_int (buf[34]) << 12;
981 l |= itoa64_to_int (buf[35]) << 18;
982
983 digest[50] = (l >> 16) & 0xff;
984 digest[ 8] = (l >> 8) & 0xff;
985 digest[29] = (l >> 0) & 0xff;
986
987 l = itoa64_to_int (buf[36]) << 0;
988 l |= itoa64_to_int (buf[37]) << 6;
989 l |= itoa64_to_int (buf[38]) << 12;
990 l |= itoa64_to_int (buf[39]) << 18;
991
992 digest[ 9] = (l >> 16) & 0xff;
993 digest[30] = (l >> 8) & 0xff;
994 digest[51] = (l >> 0) & 0xff;
995
996 l = itoa64_to_int (buf[40]) << 0;
997 l |= itoa64_to_int (buf[41]) << 6;
998 l |= itoa64_to_int (buf[42]) << 12;
999 l |= itoa64_to_int (buf[43]) << 18;
1000
1001 digest[31] = (l >> 16) & 0xff;
1002 digest[52] = (l >> 8) & 0xff;
1003 digest[10] = (l >> 0) & 0xff;
1004
1005 l = itoa64_to_int (buf[44]) << 0;
1006 l |= itoa64_to_int (buf[45]) << 6;
1007 l |= itoa64_to_int (buf[46]) << 12;
1008 l |= itoa64_to_int (buf[47]) << 18;
1009
1010 digest[53] = (l >> 16) & 0xff;
1011 digest[11] = (l >> 8) & 0xff;
1012 digest[32] = (l >> 0) & 0xff;
1013
1014 l = itoa64_to_int (buf[48]) << 0;
1015 l |= itoa64_to_int (buf[49]) << 6;
1016 l |= itoa64_to_int (buf[50]) << 12;
1017 l |= itoa64_to_int (buf[51]) << 18;
1018
1019 digest[12] = (l >> 16) & 0xff;
1020 digest[33] = (l >> 8) & 0xff;
1021 digest[54] = (l >> 0) & 0xff;
1022
1023 l = itoa64_to_int (buf[52]) << 0;
1024 l |= itoa64_to_int (buf[53]) << 6;
1025 l |= itoa64_to_int (buf[54]) << 12;
1026 l |= itoa64_to_int (buf[55]) << 18;
1027
1028 digest[34] = (l >> 16) & 0xff;
1029 digest[55] = (l >> 8) & 0xff;
1030 digest[13] = (l >> 0) & 0xff;
1031
1032 l = itoa64_to_int (buf[56]) << 0;
1033 l |= itoa64_to_int (buf[57]) << 6;
1034 l |= itoa64_to_int (buf[58]) << 12;
1035 l |= itoa64_to_int (buf[59]) << 18;
1036
1037 digest[56] = (l >> 16) & 0xff;
1038 digest[14] = (l >> 8) & 0xff;
1039 digest[35] = (l >> 0) & 0xff;
1040
1041 l = itoa64_to_int (buf[60]) << 0;
1042 l |= itoa64_to_int (buf[61]) << 6;
1043 l |= itoa64_to_int (buf[62]) << 12;
1044 l |= itoa64_to_int (buf[63]) << 18;
1045
1046 digest[15] = (l >> 16) & 0xff;
1047 digest[36] = (l >> 8) & 0xff;
1048 digest[57] = (l >> 0) & 0xff;
1049
1050 l = itoa64_to_int (buf[64]) << 0;
1051 l |= itoa64_to_int (buf[65]) << 6;
1052 l |= itoa64_to_int (buf[66]) << 12;
1053 l |= itoa64_to_int (buf[67]) << 18;
1054
1055 digest[37] = (l >> 16) & 0xff;
1056 digest[58] = (l >> 8) & 0xff;
1057 digest[16] = (l >> 0) & 0xff;
1058
1059 l = itoa64_to_int (buf[68]) << 0;
1060 l |= itoa64_to_int (buf[69]) << 6;
1061 l |= itoa64_to_int (buf[70]) << 12;
1062 l |= itoa64_to_int (buf[71]) << 18;
1063
1064 digest[59] = (l >> 16) & 0xff;
1065 digest[17] = (l >> 8) & 0xff;
1066 digest[38] = (l >> 0) & 0xff;
1067
1068 l = itoa64_to_int (buf[72]) << 0;
1069 l |= itoa64_to_int (buf[73]) << 6;
1070 l |= itoa64_to_int (buf[74]) << 12;
1071 l |= itoa64_to_int (buf[75]) << 18;
1072
1073 digest[18] = (l >> 16) & 0xff;
1074 digest[39] = (l >> 8) & 0xff;
1075 digest[60] = (l >> 0) & 0xff;
1076
1077 l = itoa64_to_int (buf[76]) << 0;
1078 l |= itoa64_to_int (buf[77]) << 6;
1079 l |= itoa64_to_int (buf[78]) << 12;
1080 l |= itoa64_to_int (buf[79]) << 18;
1081
1082 digest[40] = (l >> 16) & 0xff;
1083 digest[61] = (l >> 8) & 0xff;
1084 digest[19] = (l >> 0) & 0xff;
1085
1086 l = itoa64_to_int (buf[80]) << 0;
1087 l |= itoa64_to_int (buf[81]) << 6;
1088 l |= itoa64_to_int (buf[82]) << 12;
1089 l |= itoa64_to_int (buf[83]) << 18;
1090
1091 digest[62] = (l >> 16) & 0xff;
1092 digest[20] = (l >> 8) & 0xff;
1093 digest[41] = (l >> 0) & 0xff;
1094
1095 l = itoa64_to_int (buf[84]) << 0;
1096 l |= itoa64_to_int (buf[85]) << 6;
1097
1098 digest[63] = (l >> 0) & 0xff;
1099 }
1100
1101 void sha512crypt_encode (u8 digest[64], u8 buf[86])
1102 {
1103 int l;
1104
1105 l = (digest[ 0] << 16) | (digest[21] << 8) | (digest[42] << 0);
1106
1107 buf[ 0] = int_to_itoa64 (l & 0x3f); l >>= 6;
1108 buf[ 1] = int_to_itoa64 (l & 0x3f); l >>= 6;
1109 buf[ 2] = int_to_itoa64 (l & 0x3f); l >>= 6;
1110 buf[ 3] = int_to_itoa64 (l & 0x3f); l >>= 6;
1111
1112 l = (digest[22] << 16) | (digest[43] << 8) | (digest[ 1] << 0);
1113
1114 buf[ 4] = int_to_itoa64 (l & 0x3f); l >>= 6;
1115 buf[ 5] = int_to_itoa64 (l & 0x3f); l >>= 6;
1116 buf[ 6] = int_to_itoa64 (l & 0x3f); l >>= 6;
1117 buf[ 7] = int_to_itoa64 (l & 0x3f); l >>= 6;
1118
1119 l = (digest[44] << 16) | (digest[ 2] << 8) | (digest[23] << 0);
1120
1121 buf[ 8] = int_to_itoa64 (l & 0x3f); l >>= 6;
1122 buf[ 9] = int_to_itoa64 (l & 0x3f); l >>= 6;
1123 buf[10] = int_to_itoa64 (l & 0x3f); l >>= 6;
1124 buf[11] = int_to_itoa64 (l & 0x3f); l >>= 6;
1125
1126 l = (digest[ 3] << 16) | (digest[24] << 8) | (digest[45] << 0);
1127
1128 buf[12] = int_to_itoa64 (l & 0x3f); l >>= 6;
1129 buf[13] = int_to_itoa64 (l & 0x3f); l >>= 6;
1130 buf[14] = int_to_itoa64 (l & 0x3f); l >>= 6;
1131 buf[15] = int_to_itoa64 (l & 0x3f); l >>= 6;
1132
1133 l = (digest[25] << 16) | (digest[46] << 8) | (digest[ 4] << 0);
1134
1135 buf[16] = int_to_itoa64 (l & 0x3f); l >>= 6;
1136 buf[17] = int_to_itoa64 (l & 0x3f); l >>= 6;
1137 buf[18] = int_to_itoa64 (l & 0x3f); l >>= 6;
1138 buf[19] = int_to_itoa64 (l & 0x3f); l >>= 6;
1139
1140 l = (digest[47] << 16) | (digest[ 5] << 8) | (digest[26] << 0);
1141
1142 buf[20] = int_to_itoa64 (l & 0x3f); l >>= 6;
1143 buf[21] = int_to_itoa64 (l & 0x3f); l >>= 6;
1144 buf[22] = int_to_itoa64 (l & 0x3f); l >>= 6;
1145 buf[23] = int_to_itoa64 (l & 0x3f); l >>= 6;
1146
1147 l = (digest[ 6] << 16) | (digest[27] << 8) | (digest[48] << 0);
1148
1149 buf[24] = int_to_itoa64 (l & 0x3f); l >>= 6;
1150 buf[25] = int_to_itoa64 (l & 0x3f); l >>= 6;
1151 buf[26] = int_to_itoa64 (l & 0x3f); l >>= 6;
1152 buf[27] = int_to_itoa64 (l & 0x3f); l >>= 6;
1153
1154 l = (digest[28] << 16) | (digest[49] << 8) | (digest[ 7] << 0);
1155
1156 buf[28] = int_to_itoa64 (l & 0x3f); l >>= 6;
1157 buf[29] = int_to_itoa64 (l & 0x3f); l >>= 6;
1158 buf[30] = int_to_itoa64 (l & 0x3f); l >>= 6;
1159 buf[31] = int_to_itoa64 (l & 0x3f); l >>= 6;
1160
1161 l = (digest[50] << 16) | (digest[ 8] << 8) | (digest[29] << 0);
1162
1163 buf[32] = int_to_itoa64 (l & 0x3f); l >>= 6;
1164 buf[33] = int_to_itoa64 (l & 0x3f); l >>= 6;
1165 buf[34] = int_to_itoa64 (l & 0x3f); l >>= 6;
1166 buf[35] = int_to_itoa64 (l & 0x3f); l >>= 6;
1167
1168 l = (digest[ 9] << 16) | (digest[30] << 8) | (digest[51] << 0);
1169
1170 buf[36] = int_to_itoa64 (l & 0x3f); l >>= 6;
1171 buf[37] = int_to_itoa64 (l & 0x3f); l >>= 6;
1172 buf[38] = int_to_itoa64 (l & 0x3f); l >>= 6;
1173 buf[39] = int_to_itoa64 (l & 0x3f); l >>= 6;
1174
1175 l = (digest[31] << 16) | (digest[52] << 8) | (digest[10] << 0);
1176
1177 buf[40] = int_to_itoa64 (l & 0x3f); l >>= 6;
1178 buf[41] = int_to_itoa64 (l & 0x3f); l >>= 6;
1179 buf[42] = int_to_itoa64 (l & 0x3f); l >>= 6;
1180 buf[43] = int_to_itoa64 (l & 0x3f); l >>= 6;
1181
1182 l = (digest[53] << 16) | (digest[11] << 8) | (digest[32] << 0);
1183
1184 buf[44] = int_to_itoa64 (l & 0x3f); l >>= 6;
1185 buf[45] = int_to_itoa64 (l & 0x3f); l >>= 6;
1186 buf[46] = int_to_itoa64 (l & 0x3f); l >>= 6;
1187 buf[47] = int_to_itoa64 (l & 0x3f); l >>= 6;
1188
1189 l = (digest[12] << 16) | (digest[33] << 8) | (digest[54] << 0);
1190
1191 buf[48] = int_to_itoa64 (l & 0x3f); l >>= 6;
1192 buf[49] = int_to_itoa64 (l & 0x3f); l >>= 6;
1193 buf[50] = int_to_itoa64 (l & 0x3f); l >>= 6;
1194 buf[51] = int_to_itoa64 (l & 0x3f); l >>= 6;
1195
1196 l = (digest[34] << 16) | (digest[55] << 8) | (digest[13] << 0);
1197
1198 buf[52] = int_to_itoa64 (l & 0x3f); l >>= 6;
1199 buf[53] = int_to_itoa64 (l & 0x3f); l >>= 6;
1200 buf[54] = int_to_itoa64 (l & 0x3f); l >>= 6;
1201 buf[55] = int_to_itoa64 (l & 0x3f); l >>= 6;
1202
1203 l = (digest[56] << 16) | (digest[14] << 8) | (digest[35] << 0);
1204
1205 buf[56] = int_to_itoa64 (l & 0x3f); l >>= 6;
1206 buf[57] = int_to_itoa64 (l & 0x3f); l >>= 6;
1207 buf[58] = int_to_itoa64 (l & 0x3f); l >>= 6;
1208 buf[59] = int_to_itoa64 (l & 0x3f); l >>= 6;
1209
1210 l = (digest[15] << 16) | (digest[36] << 8) | (digest[57] << 0);
1211
1212 buf[60] = int_to_itoa64 (l & 0x3f); l >>= 6;
1213 buf[61] = int_to_itoa64 (l & 0x3f); l >>= 6;
1214 buf[62] = int_to_itoa64 (l & 0x3f); l >>= 6;
1215 buf[63] = int_to_itoa64 (l & 0x3f); l >>= 6;
1216
1217 l = (digest[37] << 16) | (digest[58] << 8) | (digest[16] << 0);
1218
1219 buf[64] = int_to_itoa64 (l & 0x3f); l >>= 6;
1220 buf[65] = int_to_itoa64 (l & 0x3f); l >>= 6;
1221 buf[66] = int_to_itoa64 (l & 0x3f); l >>= 6;
1222 buf[67] = int_to_itoa64 (l & 0x3f); l >>= 6;
1223
1224 l = (digest[59] << 16) | (digest[17] << 8) | (digest[38] << 0);
1225
1226 buf[68] = int_to_itoa64 (l & 0x3f); l >>= 6;
1227 buf[69] = int_to_itoa64 (l & 0x3f); l >>= 6;
1228 buf[70] = int_to_itoa64 (l & 0x3f); l >>= 6;
1229 buf[71] = int_to_itoa64 (l & 0x3f); l >>= 6;
1230
1231 l = (digest[18] << 16) | (digest[39] << 8) | (digest[60] << 0);
1232
1233 buf[72] = int_to_itoa64 (l & 0x3f); l >>= 6;
1234 buf[73] = int_to_itoa64 (l & 0x3f); l >>= 6;
1235 buf[74] = int_to_itoa64 (l & 0x3f); l >>= 6;
1236 buf[75] = int_to_itoa64 (l & 0x3f); l >>= 6;
1237
1238 l = (digest[40] << 16) | (digest[61] << 8) | (digest[19] << 0);
1239
1240 buf[76] = int_to_itoa64 (l & 0x3f); l >>= 6;
1241 buf[77] = int_to_itoa64 (l & 0x3f); l >>= 6;
1242 buf[78] = int_to_itoa64 (l & 0x3f); l >>= 6;
1243 buf[79] = int_to_itoa64 (l & 0x3f); l >>= 6;
1244
1245 l = (digest[62] << 16) | (digest[20] << 8) | (digest[41] << 0);
1246
1247 buf[80] = int_to_itoa64 (l & 0x3f); l >>= 6;
1248 buf[81] = int_to_itoa64 (l & 0x3f); l >>= 6;
1249 buf[82] = int_to_itoa64 (l & 0x3f); l >>= 6;
1250 buf[83] = int_to_itoa64 (l & 0x3f); l >>= 6;
1251
1252 l = 0 | 0 | (digest[63] << 0);
1253
1254 buf[84] = int_to_itoa64 (l & 0x3f); l >>= 6;
1255 buf[85] = int_to_itoa64 (l & 0x3f); l >>= 6;
1256 }
1257
1258 void sha1aix_decode (u8 digest[20], u8 buf[27])
1259 {
1260 int l;
1261
1262 l = itoa64_to_int (buf[ 0]) << 0;
1263 l |= itoa64_to_int (buf[ 1]) << 6;
1264 l |= itoa64_to_int (buf[ 2]) << 12;
1265 l |= itoa64_to_int (buf[ 3]) << 18;
1266
1267 digest[ 2] = (l >> 0) & 0xff;
1268 digest[ 1] = (l >> 8) & 0xff;
1269 digest[ 0] = (l >> 16) & 0xff;
1270
1271 l = itoa64_to_int (buf[ 4]) << 0;
1272 l |= itoa64_to_int (buf[ 5]) << 6;
1273 l |= itoa64_to_int (buf[ 6]) << 12;
1274 l |= itoa64_to_int (buf[ 7]) << 18;
1275
1276 digest[ 5] = (l >> 0) & 0xff;
1277 digest[ 4] = (l >> 8) & 0xff;
1278 digest[ 3] = (l >> 16) & 0xff;
1279
1280 l = itoa64_to_int (buf[ 8]) << 0;
1281 l |= itoa64_to_int (buf[ 9]) << 6;
1282 l |= itoa64_to_int (buf[10]) << 12;
1283 l |= itoa64_to_int (buf[11]) << 18;
1284
1285 digest[ 8] = (l >> 0) & 0xff;
1286 digest[ 7] = (l >> 8) & 0xff;
1287 digest[ 6] = (l >> 16) & 0xff;
1288
1289 l = itoa64_to_int (buf[12]) << 0;
1290 l |= itoa64_to_int (buf[13]) << 6;
1291 l |= itoa64_to_int (buf[14]) << 12;
1292 l |= itoa64_to_int (buf[15]) << 18;
1293
1294 digest[11] = (l >> 0) & 0xff;
1295 digest[10] = (l >> 8) & 0xff;
1296 digest[ 9] = (l >> 16) & 0xff;
1297
1298 l = itoa64_to_int (buf[16]) << 0;
1299 l |= itoa64_to_int (buf[17]) << 6;
1300 l |= itoa64_to_int (buf[18]) << 12;
1301 l |= itoa64_to_int (buf[19]) << 18;
1302
1303 digest[14] = (l >> 0) & 0xff;
1304 digest[13] = (l >> 8) & 0xff;
1305 digest[12] = (l >> 16) & 0xff;
1306
1307 l = itoa64_to_int (buf[20]) << 0;
1308 l |= itoa64_to_int (buf[21]) << 6;
1309 l |= itoa64_to_int (buf[22]) << 12;
1310 l |= itoa64_to_int (buf[23]) << 18;
1311
1312 digest[17] = (l >> 0) & 0xff;
1313 digest[16] = (l >> 8) & 0xff;
1314 digest[15] = (l >> 16) & 0xff;
1315
1316 l = itoa64_to_int (buf[24]) << 0;
1317 l |= itoa64_to_int (buf[25]) << 6;
1318 l |= itoa64_to_int (buf[26]) << 12;
1319
1320 digest[19] = (l >> 8) & 0xff;
1321 digest[18] = (l >> 16) & 0xff;
1322 }
1323
1324 void sha1aix_encode (u8 digest[20], u8 buf[27])
1325 {
1326 int l;
1327
1328 l = (digest[ 2] << 0) | (digest[ 1] << 8) | (digest[ 0] << 16);
1329
1330 buf[ 0] = int_to_itoa64 (l & 0x3f); l >>= 6;
1331 buf[ 1] = int_to_itoa64 (l & 0x3f); l >>= 6;
1332 buf[ 2] = int_to_itoa64 (l & 0x3f); l >>= 6;
1333 buf[ 3] = int_to_itoa64 (l & 0x3f);
1334
1335 l = (digest[ 5] << 0) | (digest[ 4] << 8) | (digest[ 3] << 16);
1336
1337 buf[ 4] = int_to_itoa64 (l & 0x3f); l >>= 6;
1338 buf[ 5] = int_to_itoa64 (l & 0x3f); l >>= 6;
1339 buf[ 6] = int_to_itoa64 (l & 0x3f); l >>= 6;
1340 buf[ 7] = int_to_itoa64 (l & 0x3f);
1341
1342 l = (digest[ 8] << 0) | (digest[ 7] << 8) | (digest[ 6] << 16);
1343
1344 buf[ 8] = int_to_itoa64 (l & 0x3f); l >>= 6;
1345 buf[ 9] = int_to_itoa64 (l & 0x3f); l >>= 6;
1346 buf[10] = int_to_itoa64 (l & 0x3f); l >>= 6;
1347 buf[11] = int_to_itoa64 (l & 0x3f);
1348
1349 l = (digest[11] << 0) | (digest[10] << 8) | (digest[ 9] << 16);
1350
1351 buf[12] = int_to_itoa64 (l & 0x3f); l >>= 6;
1352 buf[13] = int_to_itoa64 (l & 0x3f); l >>= 6;
1353 buf[14] = int_to_itoa64 (l & 0x3f); l >>= 6;
1354 buf[15] = int_to_itoa64 (l & 0x3f);
1355
1356 l = (digest[14] << 0) | (digest[13] << 8) | (digest[12] << 16);
1357
1358 buf[16] = int_to_itoa64 (l & 0x3f); l >>= 6;
1359 buf[17] = int_to_itoa64 (l & 0x3f); l >>= 6;
1360 buf[18] = int_to_itoa64 (l & 0x3f); l >>= 6;
1361 buf[19] = int_to_itoa64 (l & 0x3f);
1362
1363 l = (digest[17] << 0) | (digest[16] << 8) | (digest[15] << 16);
1364
1365 buf[20] = int_to_itoa64 (l & 0x3f); l >>= 6;
1366 buf[21] = int_to_itoa64 (l & 0x3f); l >>= 6;
1367 buf[22] = int_to_itoa64 (l & 0x3f); l >>= 6;
1368 buf[23] = int_to_itoa64 (l & 0x3f);
1369
1370 l = 0 | (digest[19] << 8) | (digest[18] << 16);
1371
1372 buf[24] = int_to_itoa64 (l & 0x3f); l >>= 6;
1373 buf[25] = int_to_itoa64 (l & 0x3f); l >>= 6;
1374 buf[26] = int_to_itoa64 (l & 0x3f);
1375 }
1376
1377 void sha256aix_decode (u8 digest[32], u8 buf[43])
1378 {
1379 int l;
1380
1381 l = itoa64_to_int (buf[ 0]) << 0;
1382 l |= itoa64_to_int (buf[ 1]) << 6;
1383 l |= itoa64_to_int (buf[ 2]) << 12;
1384 l |= itoa64_to_int (buf[ 3]) << 18;
1385
1386 digest[ 2] = (l >> 0) & 0xff;
1387 digest[ 1] = (l >> 8) & 0xff;
1388 digest[ 0] = (l >> 16) & 0xff;
1389
1390 l = itoa64_to_int (buf[ 4]) << 0;
1391 l |= itoa64_to_int (buf[ 5]) << 6;
1392 l |= itoa64_to_int (buf[ 6]) << 12;
1393 l |= itoa64_to_int (buf[ 7]) << 18;
1394
1395 digest[ 5] = (l >> 0) & 0xff;
1396 digest[ 4] = (l >> 8) & 0xff;
1397 digest[ 3] = (l >> 16) & 0xff;
1398
1399 l = itoa64_to_int (buf[ 8]) << 0;
1400 l |= itoa64_to_int (buf[ 9]) << 6;
1401 l |= itoa64_to_int (buf[10]) << 12;
1402 l |= itoa64_to_int (buf[11]) << 18;
1403
1404 digest[ 8] = (l >> 0) & 0xff;
1405 digest[ 7] = (l >> 8) & 0xff;
1406 digest[ 6] = (l >> 16) & 0xff;
1407
1408 l = itoa64_to_int (buf[12]) << 0;
1409 l |= itoa64_to_int (buf[13]) << 6;
1410 l |= itoa64_to_int (buf[14]) << 12;
1411 l |= itoa64_to_int (buf[15]) << 18;
1412
1413 digest[11] = (l >> 0) & 0xff;
1414 digest[10] = (l >> 8) & 0xff;
1415 digest[ 9] = (l >> 16) & 0xff;
1416
1417 l = itoa64_to_int (buf[16]) << 0;
1418 l |= itoa64_to_int (buf[17]) << 6;
1419 l |= itoa64_to_int (buf[18]) << 12;
1420 l |= itoa64_to_int (buf[19]) << 18;
1421
1422 digest[14] = (l >> 0) & 0xff;
1423 digest[13] = (l >> 8) & 0xff;
1424 digest[12] = (l >> 16) & 0xff;
1425
1426 l = itoa64_to_int (buf[20]) << 0;
1427 l |= itoa64_to_int (buf[21]) << 6;
1428 l |= itoa64_to_int (buf[22]) << 12;
1429 l |= itoa64_to_int (buf[23]) << 18;
1430
1431 digest[17] = (l >> 0) & 0xff;
1432 digest[16] = (l >> 8) & 0xff;
1433 digest[15] = (l >> 16) & 0xff;
1434
1435 l = itoa64_to_int (buf[24]) << 0;
1436 l |= itoa64_to_int (buf[25]) << 6;
1437 l |= itoa64_to_int (buf[26]) << 12;
1438 l |= itoa64_to_int (buf[27]) << 18;
1439
1440 digest[20] = (l >> 0) & 0xff;
1441 digest[19] = (l >> 8) & 0xff;
1442 digest[18] = (l >> 16) & 0xff;
1443
1444 l = itoa64_to_int (buf[28]) << 0;
1445 l |= itoa64_to_int (buf[29]) << 6;
1446 l |= itoa64_to_int (buf[30]) << 12;
1447 l |= itoa64_to_int (buf[31]) << 18;
1448
1449 digest[23] = (l >> 0) & 0xff;
1450 digest[22] = (l >> 8) & 0xff;
1451 digest[21] = (l >> 16) & 0xff;
1452
1453 l = itoa64_to_int (buf[32]) << 0;
1454 l |= itoa64_to_int (buf[33]) << 6;
1455 l |= itoa64_to_int (buf[34]) << 12;
1456 l |= itoa64_to_int (buf[35]) << 18;
1457
1458 digest[26] = (l >> 0) & 0xff;
1459 digest[25] = (l >> 8) & 0xff;
1460 digest[24] = (l >> 16) & 0xff;
1461
1462 l = itoa64_to_int (buf[36]) << 0;
1463 l |= itoa64_to_int (buf[37]) << 6;
1464 l |= itoa64_to_int (buf[38]) << 12;
1465 l |= itoa64_to_int (buf[39]) << 18;
1466
1467 digest[29] = (l >> 0) & 0xff;
1468 digest[28] = (l >> 8) & 0xff;
1469 digest[27] = (l >> 16) & 0xff;
1470
1471 l = itoa64_to_int (buf[40]) << 0;
1472 l |= itoa64_to_int (buf[41]) << 6;
1473 l |= itoa64_to_int (buf[42]) << 12;
1474
1475 //digest[32] = (l >> 0) & 0xff;
1476 digest[31] = (l >> 8) & 0xff;
1477 digest[30] = (l >> 16) & 0xff;
1478 }
1479
1480 void sha256aix_encode (u8 digest[32], u8 buf[43])
1481 {
1482 int l;
1483
1484 l = (digest[ 2] << 0) | (digest[ 1] << 8) | (digest[ 0] << 16);
1485
1486 buf[ 0] = int_to_itoa64 (l & 0x3f); l >>= 6;
1487 buf[ 1] = int_to_itoa64 (l & 0x3f); l >>= 6;
1488 buf[ 2] = int_to_itoa64 (l & 0x3f); l >>= 6;
1489 buf[ 3] = int_to_itoa64 (l & 0x3f);
1490
1491 l = (digest[ 5] << 0) | (digest[ 4] << 8) | (digest[ 3] << 16);
1492
1493 buf[ 4] = int_to_itoa64 (l & 0x3f); l >>= 6;
1494 buf[ 5] = int_to_itoa64 (l & 0x3f); l >>= 6;
1495 buf[ 6] = int_to_itoa64 (l & 0x3f); l >>= 6;
1496 buf[ 7] = int_to_itoa64 (l & 0x3f);
1497
1498 l = (digest[ 8] << 0) | (digest[ 7] << 8) | (digest[ 6] << 16);
1499
1500 buf[ 8] = int_to_itoa64 (l & 0x3f); l >>= 6;
1501 buf[ 9] = int_to_itoa64 (l & 0x3f); l >>= 6;
1502 buf[10] = int_to_itoa64 (l & 0x3f); l >>= 6;
1503 buf[11] = int_to_itoa64 (l & 0x3f);
1504
1505 l = (digest[11] << 0) | (digest[10] << 8) | (digest[ 9] << 16);
1506
1507 buf[12] = int_to_itoa64 (l & 0x3f); l >>= 6;
1508 buf[13] = int_to_itoa64 (l & 0x3f); l >>= 6;
1509 buf[14] = int_to_itoa64 (l & 0x3f); l >>= 6;
1510 buf[15] = int_to_itoa64 (l & 0x3f);
1511
1512 l = (digest[14] << 0) | (digest[13] << 8) | (digest[12] << 16);
1513
1514 buf[16] = int_to_itoa64 (l & 0x3f); l >>= 6;
1515 buf[17] = int_to_itoa64 (l & 0x3f); l >>= 6;
1516 buf[18] = int_to_itoa64 (l & 0x3f); l >>= 6;
1517 buf[19] = int_to_itoa64 (l & 0x3f);
1518
1519 l = (digest[17] << 0) | (digest[16] << 8) | (digest[15] << 16);
1520
1521 buf[20] = int_to_itoa64 (l & 0x3f); l >>= 6;
1522 buf[21] = int_to_itoa64 (l & 0x3f); l >>= 6;
1523 buf[22] = int_to_itoa64 (l & 0x3f); l >>= 6;
1524 buf[23] = int_to_itoa64 (l & 0x3f);
1525
1526 l = (digest[20] << 0) | (digest[19] << 8) | (digest[18] << 16);
1527
1528 buf[24] = int_to_itoa64 (l & 0x3f); l >>= 6;
1529 buf[25] = int_to_itoa64 (l & 0x3f); l >>= 6;
1530 buf[26] = int_to_itoa64 (l & 0x3f); l >>= 6;
1531 buf[27] = int_to_itoa64 (l & 0x3f);
1532
1533 l = (digest[23] << 0) | (digest[22] << 8) | (digest[21] << 16);
1534
1535 buf[28] = int_to_itoa64 (l & 0x3f); l >>= 6;
1536 buf[29] = int_to_itoa64 (l & 0x3f); l >>= 6;
1537 buf[30] = int_to_itoa64 (l & 0x3f); l >>= 6;
1538 buf[31] = int_to_itoa64 (l & 0x3f);
1539
1540 l = (digest[26] << 0) | (digest[25] << 8) | (digest[24] << 16);
1541
1542 buf[32] = int_to_itoa64 (l & 0x3f); l >>= 6;
1543 buf[33] = int_to_itoa64 (l & 0x3f); l >>= 6;
1544 buf[34] = int_to_itoa64 (l & 0x3f); l >>= 6;
1545 buf[35] = int_to_itoa64 (l & 0x3f);
1546
1547 l = (digest[29] << 0) | (digest[28] << 8) | (digest[27] << 16);
1548
1549 buf[36] = int_to_itoa64 (l & 0x3f); l >>= 6;
1550 buf[37] = int_to_itoa64 (l & 0x3f); l >>= 6;
1551 buf[38] = int_to_itoa64 (l & 0x3f); l >>= 6;
1552 buf[39] = int_to_itoa64 (l & 0x3f);
1553
1554 l = 0 | (digest[31] << 8) | (digest[30] << 16);
1555
1556 buf[40] = int_to_itoa64 (l & 0x3f); l >>= 6;
1557 buf[41] = int_to_itoa64 (l & 0x3f); l >>= 6;
1558 buf[42] = int_to_itoa64 (l & 0x3f);
1559 }
1560
1561 void sha512aix_decode (u8 digest[64], u8 buf[86])
1562 {
1563 int l;
1564
1565 l = itoa64_to_int (buf[ 0]) << 0;
1566 l |= itoa64_to_int (buf[ 1]) << 6;
1567 l |= itoa64_to_int (buf[ 2]) << 12;
1568 l |= itoa64_to_int (buf[ 3]) << 18;
1569
1570 digest[ 2] = (l >> 0) & 0xff;
1571 digest[ 1] = (l >> 8) & 0xff;
1572 digest[ 0] = (l >> 16) & 0xff;
1573
1574 l = itoa64_to_int (buf[ 4]) << 0;
1575 l |= itoa64_to_int (buf[ 5]) << 6;
1576 l |= itoa64_to_int (buf[ 6]) << 12;
1577 l |= itoa64_to_int (buf[ 7]) << 18;
1578
1579 digest[ 5] = (l >> 0) & 0xff;
1580 digest[ 4] = (l >> 8) & 0xff;
1581 digest[ 3] = (l >> 16) & 0xff;
1582
1583 l = itoa64_to_int (buf[ 8]) << 0;
1584 l |= itoa64_to_int (buf[ 9]) << 6;
1585 l |= itoa64_to_int (buf[10]) << 12;
1586 l |= itoa64_to_int (buf[11]) << 18;
1587
1588 digest[ 8] = (l >> 0) & 0xff;
1589 digest[ 7] = (l >> 8) & 0xff;
1590 digest[ 6] = (l >> 16) & 0xff;
1591
1592 l = itoa64_to_int (buf[12]) << 0;
1593 l |= itoa64_to_int (buf[13]) << 6;
1594 l |= itoa64_to_int (buf[14]) << 12;
1595 l |= itoa64_to_int (buf[15]) << 18;
1596
1597 digest[11] = (l >> 0) & 0xff;
1598 digest[10] = (l >> 8) & 0xff;
1599 digest[ 9] = (l >> 16) & 0xff;
1600
1601 l = itoa64_to_int (buf[16]) << 0;
1602 l |= itoa64_to_int (buf[17]) << 6;
1603 l |= itoa64_to_int (buf[18]) << 12;
1604 l |= itoa64_to_int (buf[19]) << 18;
1605
1606 digest[14] = (l >> 0) & 0xff;
1607 digest[13] = (l >> 8) & 0xff;
1608 digest[12] = (l >> 16) & 0xff;
1609
1610 l = itoa64_to_int (buf[20]) << 0;
1611 l |= itoa64_to_int (buf[21]) << 6;
1612 l |= itoa64_to_int (buf[22]) << 12;
1613 l |= itoa64_to_int (buf[23]) << 18;
1614
1615 digest[17] = (l >> 0) & 0xff;
1616 digest[16] = (l >> 8) & 0xff;
1617 digest[15] = (l >> 16) & 0xff;
1618
1619 l = itoa64_to_int (buf[24]) << 0;
1620 l |= itoa64_to_int (buf[25]) << 6;
1621 l |= itoa64_to_int (buf[26]) << 12;
1622 l |= itoa64_to_int (buf[27]) << 18;
1623
1624 digest[20] = (l >> 0) & 0xff;
1625 digest[19] = (l >> 8) & 0xff;
1626 digest[18] = (l >> 16) & 0xff;
1627
1628 l = itoa64_to_int (buf[28]) << 0;
1629 l |= itoa64_to_int (buf[29]) << 6;
1630 l |= itoa64_to_int (buf[30]) << 12;
1631 l |= itoa64_to_int (buf[31]) << 18;
1632
1633 digest[23] = (l >> 0) & 0xff;
1634 digest[22] = (l >> 8) & 0xff;
1635 digest[21] = (l >> 16) & 0xff;
1636
1637 l = itoa64_to_int (buf[32]) << 0;
1638 l |= itoa64_to_int (buf[33]) << 6;
1639 l |= itoa64_to_int (buf[34]) << 12;
1640 l |= itoa64_to_int (buf[35]) << 18;
1641
1642 digest[26] = (l >> 0) & 0xff;
1643 digest[25] = (l >> 8) & 0xff;
1644 digest[24] = (l >> 16) & 0xff;
1645
1646 l = itoa64_to_int (buf[36]) << 0;
1647 l |= itoa64_to_int (buf[37]) << 6;
1648 l |= itoa64_to_int (buf[38]) << 12;
1649 l |= itoa64_to_int (buf[39]) << 18;
1650
1651 digest[29] = (l >> 0) & 0xff;
1652 digest[28] = (l >> 8) & 0xff;
1653 digest[27] = (l >> 16) & 0xff;
1654
1655 l = itoa64_to_int (buf[40]) << 0;
1656 l |= itoa64_to_int (buf[41]) << 6;
1657 l |= itoa64_to_int (buf[42]) << 12;
1658 l |= itoa64_to_int (buf[43]) << 18;
1659
1660 digest[32] = (l >> 0) & 0xff;
1661 digest[31] = (l >> 8) & 0xff;
1662 digest[30] = (l >> 16) & 0xff;
1663
1664 l = itoa64_to_int (buf[44]) << 0;
1665 l |= itoa64_to_int (buf[45]) << 6;
1666 l |= itoa64_to_int (buf[46]) << 12;
1667 l |= itoa64_to_int (buf[47]) << 18;
1668
1669 digest[35] = (l >> 0) & 0xff;
1670 digest[34] = (l >> 8) & 0xff;
1671 digest[33] = (l >> 16) & 0xff;
1672
1673 l = itoa64_to_int (buf[48]) << 0;
1674 l |= itoa64_to_int (buf[49]) << 6;
1675 l |= itoa64_to_int (buf[50]) << 12;
1676 l |= itoa64_to_int (buf[51]) << 18;
1677
1678 digest[38] = (l >> 0) & 0xff;
1679 digest[37] = (l >> 8) & 0xff;
1680 digest[36] = (l >> 16) & 0xff;
1681
1682 l = itoa64_to_int (buf[52]) << 0;
1683 l |= itoa64_to_int (buf[53]) << 6;
1684 l |= itoa64_to_int (buf[54]) << 12;
1685 l |= itoa64_to_int (buf[55]) << 18;
1686
1687 digest[41] = (l >> 0) & 0xff;
1688 digest[40] = (l >> 8) & 0xff;
1689 digest[39] = (l >> 16) & 0xff;
1690
1691 l = itoa64_to_int (buf[56]) << 0;
1692 l |= itoa64_to_int (buf[57]) << 6;
1693 l |= itoa64_to_int (buf[58]) << 12;
1694 l |= itoa64_to_int (buf[59]) << 18;
1695
1696 digest[44] = (l >> 0) & 0xff;
1697 digest[43] = (l >> 8) & 0xff;
1698 digest[42] = (l >> 16) & 0xff;
1699
1700 l = itoa64_to_int (buf[60]) << 0;
1701 l |= itoa64_to_int (buf[61]) << 6;
1702 l |= itoa64_to_int (buf[62]) << 12;
1703 l |= itoa64_to_int (buf[63]) << 18;
1704
1705 digest[47] = (l >> 0) & 0xff;
1706 digest[46] = (l >> 8) & 0xff;
1707 digest[45] = (l >> 16) & 0xff;
1708
1709 l = itoa64_to_int (buf[64]) << 0;
1710 l |= itoa64_to_int (buf[65]) << 6;
1711 l |= itoa64_to_int (buf[66]) << 12;
1712 l |= itoa64_to_int (buf[67]) << 18;
1713
1714 digest[50] = (l >> 0) & 0xff;
1715 digest[49] = (l >> 8) & 0xff;
1716 digest[48] = (l >> 16) & 0xff;
1717
1718 l = itoa64_to_int (buf[68]) << 0;
1719 l |= itoa64_to_int (buf[69]) << 6;
1720 l |= itoa64_to_int (buf[70]) << 12;
1721 l |= itoa64_to_int (buf[71]) << 18;
1722
1723 digest[53] = (l >> 0) & 0xff;
1724 digest[52] = (l >> 8) & 0xff;
1725 digest[51] = (l >> 16) & 0xff;
1726
1727 l = itoa64_to_int (buf[72]) << 0;
1728 l |= itoa64_to_int (buf[73]) << 6;
1729 l |= itoa64_to_int (buf[74]) << 12;
1730 l |= itoa64_to_int (buf[75]) << 18;
1731
1732 digest[56] = (l >> 0) & 0xff;
1733 digest[55] = (l >> 8) & 0xff;
1734 digest[54] = (l >> 16) & 0xff;
1735
1736 l = itoa64_to_int (buf[76]) << 0;
1737 l |= itoa64_to_int (buf[77]) << 6;
1738 l |= itoa64_to_int (buf[78]) << 12;
1739 l |= itoa64_to_int (buf[79]) << 18;
1740
1741 digest[59] = (l >> 0) & 0xff;
1742 digest[58] = (l >> 8) & 0xff;
1743 digest[57] = (l >> 16) & 0xff;
1744
1745 l = itoa64_to_int (buf[80]) << 0;
1746 l |= itoa64_to_int (buf[81]) << 6;
1747 l |= itoa64_to_int (buf[82]) << 12;
1748 l |= itoa64_to_int (buf[83]) << 18;
1749
1750 digest[62] = (l >> 0) & 0xff;
1751 digest[61] = (l >> 8) & 0xff;
1752 digest[60] = (l >> 16) & 0xff;
1753
1754 l = itoa64_to_int (buf[84]) << 0;
1755 l |= itoa64_to_int (buf[85]) << 6;
1756
1757 digest[63] = (l >> 16) & 0xff;
1758 }
1759
1760 void sha512aix_encode (u8 digest[64], u8 buf[86])
1761 {
1762 int l;
1763
1764 l = (digest[ 2] << 0) | (digest[ 1] << 8) | (digest[ 0] << 16);
1765
1766 buf[ 0] = int_to_itoa64 (l & 0x3f); l >>= 6;
1767 buf[ 1] = int_to_itoa64 (l & 0x3f); l >>= 6;
1768 buf[ 2] = int_to_itoa64 (l & 0x3f); l >>= 6;
1769 buf[ 3] = int_to_itoa64 (l & 0x3f);
1770
1771 l = (digest[ 5] << 0) | (digest[ 4] << 8) | (digest[ 3] << 16);
1772
1773 buf[ 4] = int_to_itoa64 (l & 0x3f); l >>= 6;
1774 buf[ 5] = int_to_itoa64 (l & 0x3f); l >>= 6;
1775 buf[ 6] = int_to_itoa64 (l & 0x3f); l >>= 6;
1776 buf[ 7] = int_to_itoa64 (l & 0x3f);
1777
1778 l = (digest[ 8] << 0) | (digest[ 7] << 8) | (digest[ 6] << 16);
1779
1780 buf[ 8] = int_to_itoa64 (l & 0x3f); l >>= 6;
1781 buf[ 9] = int_to_itoa64 (l & 0x3f); l >>= 6;
1782 buf[10] = int_to_itoa64 (l & 0x3f); l >>= 6;
1783 buf[11] = int_to_itoa64 (l & 0x3f);
1784
1785 l = (digest[11] << 0) | (digest[10] << 8) | (digest[ 9] << 16);
1786
1787 buf[12] = int_to_itoa64 (l & 0x3f); l >>= 6;
1788 buf[13] = int_to_itoa64 (l & 0x3f); l >>= 6;
1789 buf[14] = int_to_itoa64 (l & 0x3f); l >>= 6;
1790 buf[15] = int_to_itoa64 (l & 0x3f);
1791
1792 l = (digest[14] << 0) | (digest[13] << 8) | (digest[12] << 16);
1793
1794 buf[16] = int_to_itoa64 (l & 0x3f); l >>= 6;
1795 buf[17] = int_to_itoa64 (l & 0x3f); l >>= 6;
1796 buf[18] = int_to_itoa64 (l & 0x3f); l >>= 6;
1797 buf[19] = int_to_itoa64 (l & 0x3f);
1798
1799 l = (digest[17] << 0) | (digest[16] << 8) | (digest[15] << 16);
1800
1801 buf[20] = int_to_itoa64 (l & 0x3f); l >>= 6;
1802 buf[21] = int_to_itoa64 (l & 0x3f); l >>= 6;
1803 buf[22] = int_to_itoa64 (l & 0x3f); l >>= 6;
1804 buf[23] = int_to_itoa64 (l & 0x3f);
1805
1806 l = (digest[20] << 0) | (digest[19] << 8) | (digest[18] << 16);
1807
1808 buf[24] = int_to_itoa64 (l & 0x3f); l >>= 6;
1809 buf[25] = int_to_itoa64 (l & 0x3f); l >>= 6;
1810 buf[26] = int_to_itoa64 (l & 0x3f); l >>= 6;
1811 buf[27] = int_to_itoa64 (l & 0x3f);
1812
1813 l = (digest[23] << 0) | (digest[22] << 8) | (digest[21] << 16);
1814
1815 buf[28] = int_to_itoa64 (l & 0x3f); l >>= 6;
1816 buf[29] = int_to_itoa64 (l & 0x3f); l >>= 6;
1817 buf[30] = int_to_itoa64 (l & 0x3f); l >>= 6;
1818 buf[31] = int_to_itoa64 (l & 0x3f);
1819
1820 l = (digest[26] << 0) | (digest[25] << 8) | (digest[24] << 16);
1821
1822 buf[32] = int_to_itoa64 (l & 0x3f); l >>= 6;
1823 buf[33] = int_to_itoa64 (l & 0x3f); l >>= 6;
1824 buf[34] = int_to_itoa64 (l & 0x3f); l >>= 6;
1825 buf[35] = int_to_itoa64 (l & 0x3f);
1826
1827 l = (digest[29] << 0) | (digest[28] << 8) | (digest[27] << 16);
1828
1829 buf[36] = int_to_itoa64 (l & 0x3f); l >>= 6;
1830 buf[37] = int_to_itoa64 (l & 0x3f); l >>= 6;
1831 buf[38] = int_to_itoa64 (l & 0x3f); l >>= 6;
1832 buf[39] = int_to_itoa64 (l & 0x3f);
1833
1834 l = (digest[32] << 0) | (digest[31] << 8) | (digest[30] << 16);
1835
1836 buf[40] = int_to_itoa64 (l & 0x3f); l >>= 6;
1837 buf[41] = int_to_itoa64 (l & 0x3f); l >>= 6;
1838 buf[42] = int_to_itoa64 (l & 0x3f); l >>= 6;
1839 buf[43] = int_to_itoa64 (l & 0x3f);
1840
1841 l = (digest[35] << 0) | (digest[34] << 8) | (digest[33] << 16);
1842
1843 buf[44] = int_to_itoa64 (l & 0x3f); l >>= 6;
1844 buf[45] = int_to_itoa64 (l & 0x3f); l >>= 6;
1845 buf[46] = int_to_itoa64 (l & 0x3f); l >>= 6;
1846 buf[47] = int_to_itoa64 (l & 0x3f);
1847
1848 l = (digest[38] << 0) | (digest[37] << 8) | (digest[36] << 16);
1849
1850 buf[48] = int_to_itoa64 (l & 0x3f); l >>= 6;
1851 buf[49] = int_to_itoa64 (l & 0x3f); l >>= 6;
1852 buf[50] = int_to_itoa64 (l & 0x3f); l >>= 6;
1853 buf[51] = int_to_itoa64 (l & 0x3f);
1854
1855 l = (digest[41] << 0) | (digest[40] << 8) | (digest[39] << 16);
1856
1857 buf[52] = int_to_itoa64 (l & 0x3f); l >>= 6;
1858 buf[53] = int_to_itoa64 (l & 0x3f); l >>= 6;
1859 buf[54] = int_to_itoa64 (l & 0x3f); l >>= 6;
1860 buf[55] = int_to_itoa64 (l & 0x3f);
1861
1862 l = (digest[44] << 0) | (digest[43] << 8) | (digest[42] << 16);
1863
1864 buf[56] = int_to_itoa64 (l & 0x3f); l >>= 6;
1865 buf[57] = int_to_itoa64 (l & 0x3f); l >>= 6;
1866 buf[58] = int_to_itoa64 (l & 0x3f); l >>= 6;
1867 buf[59] = int_to_itoa64 (l & 0x3f);
1868
1869 l = (digest[47] << 0) | (digest[46] << 8) | (digest[45] << 16);
1870
1871 buf[60] = int_to_itoa64 (l & 0x3f); l >>= 6;
1872 buf[61] = int_to_itoa64 (l & 0x3f); l >>= 6;
1873 buf[62] = int_to_itoa64 (l & 0x3f); l >>= 6;
1874 buf[63] = int_to_itoa64 (l & 0x3f);
1875
1876 l = (digest[50] << 0) | (digest[49] << 8) | (digest[48] << 16);
1877
1878 buf[64] = int_to_itoa64 (l & 0x3f); l >>= 6;
1879 buf[65] = int_to_itoa64 (l & 0x3f); l >>= 6;
1880 buf[66] = int_to_itoa64 (l & 0x3f); l >>= 6;
1881 buf[67] = int_to_itoa64 (l & 0x3f);
1882
1883 l = (digest[53] << 0) | (digest[52] << 8) | (digest[51] << 16);
1884
1885 buf[68] = int_to_itoa64 (l & 0x3f); l >>= 6;
1886 buf[69] = int_to_itoa64 (l & 0x3f); l >>= 6;
1887 buf[70] = int_to_itoa64 (l & 0x3f); l >>= 6;
1888 buf[71] = int_to_itoa64 (l & 0x3f);
1889
1890 l = (digest[56] << 0) | (digest[55] << 8) | (digest[54] << 16);
1891
1892 buf[72] = int_to_itoa64 (l & 0x3f); l >>= 6;
1893 buf[73] = int_to_itoa64 (l & 0x3f); l >>= 6;
1894 buf[74] = int_to_itoa64 (l & 0x3f); l >>= 6;
1895 buf[75] = int_to_itoa64 (l & 0x3f);
1896
1897 l = (digest[59] << 0) | (digest[58] << 8) | (digest[57] << 16);
1898
1899 buf[76] = int_to_itoa64 (l & 0x3f); l >>= 6;
1900 buf[77] = int_to_itoa64 (l & 0x3f); l >>= 6;
1901 buf[78] = int_to_itoa64 (l & 0x3f); l >>= 6;
1902 buf[79] = int_to_itoa64 (l & 0x3f);
1903
1904 l = (digest[62] << 0) | (digest[61] << 8) | (digest[60] << 16);
1905
1906 buf[80] = int_to_itoa64 (l & 0x3f); l >>= 6;
1907 buf[81] = int_to_itoa64 (l & 0x3f); l >>= 6;
1908 buf[82] = int_to_itoa64 (l & 0x3f); l >>= 6;
1909 buf[83] = int_to_itoa64 (l & 0x3f);
1910
1911 l = 0 | 0 | (digest[63] << 16);
1912
1913 buf[84] = int_to_itoa64 (l & 0x3f); l >>= 6;
1914 buf[85] = int_to_itoa64 (l & 0x3f); l >>= 6;
1915 }
1916
1917 void sha256crypt_decode (u8 digest[32], u8 buf[43])
1918 {
1919 int l;
1920
1921 l = itoa64_to_int (buf[ 0]) << 0;
1922 l |= itoa64_to_int (buf[ 1]) << 6;
1923 l |= itoa64_to_int (buf[ 2]) << 12;
1924 l |= itoa64_to_int (buf[ 3]) << 18;
1925
1926 digest[ 0] = (l >> 16) & 0xff;
1927 digest[10] = (l >> 8) & 0xff;
1928 digest[20] = (l >> 0) & 0xff;
1929
1930 l = itoa64_to_int (buf[ 4]) << 0;
1931 l |= itoa64_to_int (buf[ 5]) << 6;
1932 l |= itoa64_to_int (buf[ 6]) << 12;
1933 l |= itoa64_to_int (buf[ 7]) << 18;
1934
1935 digest[21] = (l >> 16) & 0xff;
1936 digest[ 1] = (l >> 8) & 0xff;
1937 digest[11] = (l >> 0) & 0xff;
1938
1939 l = itoa64_to_int (buf[ 8]) << 0;
1940 l |= itoa64_to_int (buf[ 9]) << 6;
1941 l |= itoa64_to_int (buf[10]) << 12;
1942 l |= itoa64_to_int (buf[11]) << 18;
1943
1944 digest[12] = (l >> 16) & 0xff;
1945 digest[22] = (l >> 8) & 0xff;
1946 digest[ 2] = (l >> 0) & 0xff;
1947
1948 l = itoa64_to_int (buf[12]) << 0;
1949 l |= itoa64_to_int (buf[13]) << 6;
1950 l |= itoa64_to_int (buf[14]) << 12;
1951 l |= itoa64_to_int (buf[15]) << 18;
1952
1953 digest[ 3] = (l >> 16) & 0xff;
1954 digest[13] = (l >> 8) & 0xff;
1955 digest[23] = (l >> 0) & 0xff;
1956
1957 l = itoa64_to_int (buf[16]) << 0;
1958 l |= itoa64_to_int (buf[17]) << 6;
1959 l |= itoa64_to_int (buf[18]) << 12;
1960 l |= itoa64_to_int (buf[19]) << 18;
1961
1962 digest[24] = (l >> 16) & 0xff;
1963 digest[ 4] = (l >> 8) & 0xff;
1964 digest[14] = (l >> 0) & 0xff;
1965
1966 l = itoa64_to_int (buf[20]) << 0;
1967 l |= itoa64_to_int (buf[21]) << 6;
1968 l |= itoa64_to_int (buf[22]) << 12;
1969 l |= itoa64_to_int (buf[23]) << 18;
1970
1971 digest[15] = (l >> 16) & 0xff;
1972 digest[25] = (l >> 8) & 0xff;
1973 digest[ 5] = (l >> 0) & 0xff;
1974
1975 l = itoa64_to_int (buf[24]) << 0;
1976 l |= itoa64_to_int (buf[25]) << 6;
1977 l |= itoa64_to_int (buf[26]) << 12;
1978 l |= itoa64_to_int (buf[27]) << 18;
1979
1980 digest[ 6] = (l >> 16) & 0xff;
1981 digest[16] = (l >> 8) & 0xff;
1982 digest[26] = (l >> 0) & 0xff;
1983
1984 l = itoa64_to_int (buf[28]) << 0;
1985 l |= itoa64_to_int (buf[29]) << 6;
1986 l |= itoa64_to_int (buf[30]) << 12;
1987 l |= itoa64_to_int (buf[31]) << 18;
1988
1989 digest[27] = (l >> 16) & 0xff;
1990 digest[ 7] = (l >> 8) & 0xff;
1991 digest[17] = (l >> 0) & 0xff;
1992
1993 l = itoa64_to_int (buf[32]) << 0;
1994 l |= itoa64_to_int (buf[33]) << 6;
1995 l |= itoa64_to_int (buf[34]) << 12;
1996 l |= itoa64_to_int (buf[35]) << 18;
1997
1998 digest[18] = (l >> 16) & 0xff;
1999 digest[28] = (l >> 8) & 0xff;
2000 digest[ 8] = (l >> 0) & 0xff;
2001
2002 l = itoa64_to_int (buf[36]) << 0;
2003 l |= itoa64_to_int (buf[37]) << 6;
2004 l |= itoa64_to_int (buf[38]) << 12;
2005 l |= itoa64_to_int (buf[39]) << 18;
2006
2007 digest[ 9] = (l >> 16) & 0xff;
2008 digest[19] = (l >> 8) & 0xff;
2009 digest[29] = (l >> 0) & 0xff;
2010
2011 l = itoa64_to_int (buf[40]) << 0;
2012 l |= itoa64_to_int (buf[41]) << 6;
2013 l |= itoa64_to_int (buf[42]) << 12;
2014
2015 digest[31] = (l >> 8) & 0xff;
2016 digest[30] = (l >> 0) & 0xff;
2017 }
2018
2019 void sha256crypt_encode (u8 digest[32], u8 buf[43])
2020 {
2021 int l;
2022
2023 l = (digest[ 0] << 16) | (digest[10] << 8) | (digest[20] << 0);
2024
2025 buf[ 0] = int_to_itoa64 (l & 0x3f); l >>= 6;
2026 buf[ 1] = int_to_itoa64 (l & 0x3f); l >>= 6;
2027 buf[ 2] = int_to_itoa64 (l & 0x3f); l >>= 6;
2028 buf[ 3] = int_to_itoa64 (l & 0x3f); l >>= 6;
2029
2030 l = (digest[21] << 16) | (digest[ 1] << 8) | (digest[11] << 0);
2031
2032 buf[ 4] = int_to_itoa64 (l & 0x3f); l >>= 6;
2033 buf[ 5] = int_to_itoa64 (l & 0x3f); l >>= 6;
2034 buf[ 6] = int_to_itoa64 (l & 0x3f); l >>= 6;
2035 buf[ 7] = int_to_itoa64 (l & 0x3f); l >>= 6;
2036
2037 l = (digest[12] << 16) | (digest[22] << 8) | (digest[ 2] << 0);
2038
2039 buf[ 8] = int_to_itoa64 (l & 0x3f); l >>= 6;
2040 buf[ 9] = int_to_itoa64 (l & 0x3f); l >>= 6;
2041 buf[10] = int_to_itoa64 (l & 0x3f); l >>= 6;
2042 buf[11] = int_to_itoa64 (l & 0x3f); l >>= 6;
2043
2044 l = (digest[ 3] << 16) | (digest[13] << 8) | (digest[23] << 0);
2045
2046 buf[12] = int_to_itoa64 (l & 0x3f); l >>= 6;
2047 buf[13] = int_to_itoa64 (l & 0x3f); l >>= 6;
2048 buf[14] = int_to_itoa64 (l & 0x3f); l >>= 6;
2049 buf[15] = int_to_itoa64 (l & 0x3f); l >>= 6;
2050
2051 l = (digest[24] << 16) | (digest[ 4] << 8) | (digest[14] << 0);
2052
2053 buf[16] = int_to_itoa64 (l & 0x3f); l >>= 6;
2054 buf[17] = int_to_itoa64 (l & 0x3f); l >>= 6;
2055 buf[18] = int_to_itoa64 (l & 0x3f); l >>= 6;
2056 buf[19] = int_to_itoa64 (l & 0x3f); l >>= 6;
2057
2058 l = (digest[15] << 16) | (digest[25] << 8) | (digest[ 5] << 0);
2059
2060 buf[20] = int_to_itoa64 (l & 0x3f); l >>= 6;
2061 buf[21] = int_to_itoa64 (l & 0x3f); l >>= 6;
2062 buf[22] = int_to_itoa64 (l & 0x3f); l >>= 6;
2063 buf[23] = int_to_itoa64 (l & 0x3f); l >>= 6;
2064
2065 l = (digest[ 6] << 16) | (digest[16] << 8) | (digest[26] << 0);
2066
2067 buf[24] = int_to_itoa64 (l & 0x3f); l >>= 6;
2068 buf[25] = int_to_itoa64 (l & 0x3f); l >>= 6;
2069 buf[26] = int_to_itoa64 (l & 0x3f); l >>= 6;
2070 buf[27] = int_to_itoa64 (l & 0x3f); l >>= 6;
2071
2072 l = (digest[27] << 16) | (digest[ 7] << 8) | (digest[17] << 0);
2073
2074 buf[28] = int_to_itoa64 (l & 0x3f); l >>= 6;
2075 buf[29] = int_to_itoa64 (l & 0x3f); l >>= 6;
2076 buf[30] = int_to_itoa64 (l & 0x3f); l >>= 6;
2077 buf[31] = int_to_itoa64 (l & 0x3f); l >>= 6;
2078
2079 l = (digest[18] << 16) | (digest[28] << 8) | (digest[ 8] << 0);
2080
2081 buf[32] = int_to_itoa64 (l & 0x3f); l >>= 6;
2082 buf[33] = int_to_itoa64 (l & 0x3f); l >>= 6;
2083 buf[34] = int_to_itoa64 (l & 0x3f); l >>= 6;
2084 buf[35] = int_to_itoa64 (l & 0x3f); l >>= 6;
2085
2086 l = (digest[ 9] << 16) | (digest[19] << 8) | (digest[29] << 0);
2087
2088 buf[36] = int_to_itoa64 (l & 0x3f); l >>= 6;
2089 buf[37] = int_to_itoa64 (l & 0x3f); l >>= 6;
2090 buf[38] = int_to_itoa64 (l & 0x3f); l >>= 6;
2091 buf[39] = int_to_itoa64 (l & 0x3f); l >>= 6;
2092
2093 l = 0 | (digest[31] << 8) | (digest[30] << 0);
2094
2095 buf[40] = int_to_itoa64 (l & 0x3f); l >>= 6;
2096 buf[41] = int_to_itoa64 (l & 0x3f); l >>= 6;
2097 buf[42] = int_to_itoa64 (l & 0x3f);
2098 }
2099
2100 void drupal7_decode (u8 digest[64], u8 buf[44])
2101 {
2102 int l;
2103
2104 l = itoa64_to_int (buf[ 0]) << 0;
2105 l |= itoa64_to_int (buf[ 1]) << 6;
2106 l |= itoa64_to_int (buf[ 2]) << 12;
2107 l |= itoa64_to_int (buf[ 3]) << 18;
2108
2109 digest[ 0] = (l >> 0) & 0xff;
2110 digest[ 1] = (l >> 8) & 0xff;
2111 digest[ 2] = (l >> 16) & 0xff;
2112
2113 l = itoa64_to_int (buf[ 4]) << 0;
2114 l |= itoa64_to_int (buf[ 5]) << 6;
2115 l |= itoa64_to_int (buf[ 6]) << 12;
2116 l |= itoa64_to_int (buf[ 7]) << 18;
2117
2118 digest[ 3] = (l >> 0) & 0xff;
2119 digest[ 4] = (l >> 8) & 0xff;
2120 digest[ 5] = (l >> 16) & 0xff;
2121
2122 l = itoa64_to_int (buf[ 8]) << 0;
2123 l |= itoa64_to_int (buf[ 9]) << 6;
2124 l |= itoa64_to_int (buf[10]) << 12;
2125 l |= itoa64_to_int (buf[11]) << 18;
2126
2127 digest[ 6] = (l >> 0) & 0xff;
2128 digest[ 7] = (l >> 8) & 0xff;
2129 digest[ 8] = (l >> 16) & 0xff;
2130
2131 l = itoa64_to_int (buf[12]) << 0;
2132 l |= itoa64_to_int (buf[13]) << 6;
2133 l |= itoa64_to_int (buf[14]) << 12;
2134 l |= itoa64_to_int (buf[15]) << 18;
2135
2136 digest[ 9] = (l >> 0) & 0xff;
2137 digest[10] = (l >> 8) & 0xff;
2138 digest[11] = (l >> 16) & 0xff;
2139
2140 l = itoa64_to_int (buf[16]) << 0;
2141 l |= itoa64_to_int (buf[17]) << 6;
2142 l |= itoa64_to_int (buf[18]) << 12;
2143 l |= itoa64_to_int (buf[19]) << 18;
2144
2145 digest[12] = (l >> 0) & 0xff;
2146 digest[13] = (l >> 8) & 0xff;
2147 digest[14] = (l >> 16) & 0xff;
2148
2149 l = itoa64_to_int (buf[20]) << 0;
2150 l |= itoa64_to_int (buf[21]) << 6;
2151 l |= itoa64_to_int (buf[22]) << 12;
2152 l |= itoa64_to_int (buf[23]) << 18;
2153
2154 digest[15] = (l >> 0) & 0xff;
2155 digest[16] = (l >> 8) & 0xff;
2156 digest[17] = (l >> 16) & 0xff;
2157
2158 l = itoa64_to_int (buf[24]) << 0;
2159 l |= itoa64_to_int (buf[25]) << 6;
2160 l |= itoa64_to_int (buf[26]) << 12;
2161 l |= itoa64_to_int (buf[27]) << 18;
2162
2163 digest[18] = (l >> 0) & 0xff;
2164 digest[19] = (l >> 8) & 0xff;
2165 digest[20] = (l >> 16) & 0xff;
2166
2167 l = itoa64_to_int (buf[28]) << 0;
2168 l |= itoa64_to_int (buf[29]) << 6;
2169 l |= itoa64_to_int (buf[30]) << 12;
2170 l |= itoa64_to_int (buf[31]) << 18;
2171
2172 digest[21] = (l >> 0) & 0xff;
2173 digest[22] = (l >> 8) & 0xff;
2174 digest[23] = (l >> 16) & 0xff;
2175
2176 l = itoa64_to_int (buf[32]) << 0;
2177 l |= itoa64_to_int (buf[33]) << 6;
2178 l |= itoa64_to_int (buf[34]) << 12;
2179 l |= itoa64_to_int (buf[35]) << 18;
2180
2181 digest[24] = (l >> 0) & 0xff;
2182 digest[25] = (l >> 8) & 0xff;
2183 digest[26] = (l >> 16) & 0xff;
2184
2185 l = itoa64_to_int (buf[36]) << 0;
2186 l |= itoa64_to_int (buf[37]) << 6;
2187 l |= itoa64_to_int (buf[38]) << 12;
2188 l |= itoa64_to_int (buf[39]) << 18;
2189
2190 digest[27] = (l >> 0) & 0xff;
2191 digest[28] = (l >> 8) & 0xff;
2192 digest[29] = (l >> 16) & 0xff;
2193
2194 l = itoa64_to_int (buf[40]) << 0;
2195 l |= itoa64_to_int (buf[41]) << 6;
2196 l |= itoa64_to_int (buf[42]) << 12;
2197 l |= itoa64_to_int (buf[43]) << 18;
2198
2199 digest[30] = (l >> 0) & 0xff;
2200 digest[31] = (l >> 8) & 0xff;
2201 digest[32] = (l >> 16) & 0xff;
2202
2203 digest[33] = 0;
2204 digest[34] = 0;
2205 digest[35] = 0;
2206 digest[36] = 0;
2207 digest[37] = 0;
2208 digest[38] = 0;
2209 digest[39] = 0;
2210 digest[40] = 0;
2211 digest[41] = 0;
2212 digest[42] = 0;
2213 digest[43] = 0;
2214 digest[44] = 0;
2215 digest[45] = 0;
2216 digest[46] = 0;
2217 digest[47] = 0;
2218 digest[48] = 0;
2219 digest[49] = 0;
2220 digest[50] = 0;
2221 digest[51] = 0;
2222 digest[52] = 0;
2223 digest[53] = 0;
2224 digest[54] = 0;
2225 digest[55] = 0;
2226 digest[56] = 0;
2227 digest[57] = 0;
2228 digest[58] = 0;
2229 digest[59] = 0;
2230 digest[60] = 0;
2231 digest[61] = 0;
2232 digest[62] = 0;
2233 digest[63] = 0;
2234 }
2235
2236 void drupal7_encode (u8 digest[64], u8 buf[43])
2237 {
2238 int l;
2239
2240 l = (digest[ 0] << 0) | (digest[ 1] << 8) | (digest[ 2] << 16);
2241
2242 buf[ 0] = int_to_itoa64 (l & 0x3f); l >>= 6;
2243 buf[ 1] = int_to_itoa64 (l & 0x3f); l >>= 6;
2244 buf[ 2] = int_to_itoa64 (l & 0x3f); l >>= 6;
2245 buf[ 3] = int_to_itoa64 (l & 0x3f);
2246
2247 l = (digest[ 3] << 0) | (digest[ 4] << 8) | (digest[ 5] << 16);
2248
2249 buf[ 4] = int_to_itoa64 (l & 0x3f); l >>= 6;
2250 buf[ 5] = int_to_itoa64 (l & 0x3f); l >>= 6;
2251 buf[ 6] = int_to_itoa64 (l & 0x3f); l >>= 6;
2252 buf[ 7] = int_to_itoa64 (l & 0x3f);
2253
2254 l = (digest[ 6] << 0) | (digest[ 7] << 8) | (digest[ 8] << 16);
2255
2256 buf[ 8] = int_to_itoa64 (l & 0x3f); l >>= 6;
2257 buf[ 9] = int_to_itoa64 (l & 0x3f); l >>= 6;
2258 buf[10] = int_to_itoa64 (l & 0x3f); l >>= 6;
2259 buf[11] = int_to_itoa64 (l & 0x3f);
2260
2261 l = (digest[ 9] << 0) | (digest[10] << 8) | (digest[11] << 16);
2262
2263 buf[12] = int_to_itoa64 (l & 0x3f); l >>= 6;
2264 buf[13] = int_to_itoa64 (l & 0x3f); l >>= 6;
2265 buf[14] = int_to_itoa64 (l & 0x3f); l >>= 6;
2266 buf[15] = int_to_itoa64 (l & 0x3f);
2267
2268 l = (digest[12] << 0) | (digest[13] << 8) | (digest[14] << 16);
2269
2270 buf[16] = int_to_itoa64 (l & 0x3f); l >>= 6;
2271 buf[17] = int_to_itoa64 (l & 0x3f); l >>= 6;
2272 buf[18] = int_to_itoa64 (l & 0x3f); l >>= 6;
2273 buf[19] = int_to_itoa64 (l & 0x3f);
2274
2275 l = (digest[15] << 0) | (digest[16] << 8) | (digest[17] << 16);
2276
2277 buf[20] = int_to_itoa64 (l & 0x3f); l >>= 6;
2278 buf[21] = int_to_itoa64 (l & 0x3f); l >>= 6;
2279 buf[22] = int_to_itoa64 (l & 0x3f); l >>= 6;
2280 buf[23] = int_to_itoa64 (l & 0x3f);
2281
2282 l = (digest[18] << 0) | (digest[19] << 8) | (digest[20] << 16);
2283
2284 buf[24] = int_to_itoa64 (l & 0x3f); l >>= 6;
2285 buf[25] = int_to_itoa64 (l & 0x3f); l >>= 6;
2286 buf[26] = int_to_itoa64 (l & 0x3f); l >>= 6;
2287 buf[27] = int_to_itoa64 (l & 0x3f);
2288
2289 l = (digest[21] << 0) | (digest[22] << 8) | (digest[23] << 16);
2290
2291 buf[28] = int_to_itoa64 (l & 0x3f); l >>= 6;
2292 buf[29] = int_to_itoa64 (l & 0x3f); l >>= 6;
2293 buf[30] = int_to_itoa64 (l & 0x3f); l >>= 6;
2294 buf[31] = int_to_itoa64 (l & 0x3f);
2295
2296 l = (digest[24] << 0) | (digest[25] << 8) | (digest[26] << 16);
2297
2298 buf[32] = int_to_itoa64 (l & 0x3f); l >>= 6;
2299 buf[33] = int_to_itoa64 (l & 0x3f); l >>= 6;
2300 buf[34] = int_to_itoa64 (l & 0x3f); l >>= 6;
2301 buf[35] = int_to_itoa64 (l & 0x3f);
2302
2303 l = (digest[27] << 0) | (digest[28] << 8) | (digest[29] << 16);
2304
2305 buf[36] = int_to_itoa64 (l & 0x3f); l >>= 6;
2306 buf[37] = int_to_itoa64 (l & 0x3f); l >>= 6;
2307 buf[38] = int_to_itoa64 (l & 0x3f); l >>= 6;
2308 buf[39] = int_to_itoa64 (l & 0x3f);
2309
2310 l = (digest[30] << 0) | (digest[31] << 8) | (digest[32] << 16);
2311
2312 buf[40] = int_to_itoa64 (l & 0x3f); l >>= 6;
2313 buf[41] = int_to_itoa64 (l & 0x3f); l >>= 6;
2314 buf[42] = int_to_itoa64 (l & 0x3f); l >>= 6;
2315 //buf[43] = int_to_itoa64 (l & 0x3f);
2316 }
2317
2318 /**
2319 * tty
2320 */
2321
2322 #ifdef LINUX
2323 static struct termio savemodes;
2324 static int havemodes = 0;
2325
2326 int tty_break()
2327 {
2328 struct termio modmodes;
2329
2330 if (ioctl (fileno (stdin), TCGETA, &savemodes) < 0) return -1;
2331
2332 havemodes = 1;
2333
2334 modmodes = savemodes;
2335 modmodes.c_lflag &= ~ICANON;
2336 modmodes.c_cc[VMIN] = 1;
2337 modmodes.c_cc[VTIME] = 0;
2338
2339 return ioctl (fileno (stdin), TCSETAW, &modmodes);
2340 }
2341
2342 int tty_getchar()
2343 {
2344 fd_set rfds;
2345
2346 FD_ZERO (&rfds);
2347
2348 FD_SET (fileno (stdin), &rfds);
2349
2350 struct timeval tv;
2351
2352 tv.tv_sec = 1;
2353 tv.tv_usec = 0;
2354
2355 int retval = select (1, &rfds, NULL, NULL, &tv);
2356
2357 if (retval == 0) return 0;
2358 if (retval == -1) return -1;
2359
2360 return getchar();
2361 }
2362
2363 int tty_fix()
2364 {
2365 if (!havemodes) return 0;
2366
2367 return ioctl (fileno (stdin), TCSETAW, &savemodes);
2368 }
2369 #endif
2370
2371 #ifdef OSX
2372 static struct termios savemodes;
2373 static int havemodes = 0;
2374
2375 int tty_break()
2376 {
2377 struct termios modmodes;
2378
2379 if (ioctl (fileno (stdin), TIOCGETA, &savemodes) < 0) return -1;
2380
2381 havemodes = 1;
2382
2383 modmodes = savemodes;
2384 modmodes.c_lflag &= ~ICANON;
2385 modmodes.c_cc[VMIN] = 1;
2386 modmodes.c_cc[VTIME] = 0;
2387
2388 return ioctl (fileno (stdin), TIOCSETAW, &modmodes);
2389 }
2390
2391 int tty_getchar()
2392 {
2393 fd_set rfds;
2394
2395 FD_ZERO (&rfds);
2396
2397 FD_SET (fileno (stdin), &rfds);
2398
2399 struct timeval tv;
2400
2401 tv.tv_sec = 1;
2402 tv.tv_usec = 0;
2403
2404 int retval = select (1, &rfds, NULL, NULL, &tv);
2405
2406 if (retval == 0) return 0;
2407 if (retval == -1) return -1;
2408
2409 return getchar();
2410 }
2411
2412 int tty_fix()
2413 {
2414 if (!havemodes) return 0;
2415
2416 return ioctl (fileno (stdin), TIOCSETAW, &savemodes);
2417 }
2418 #endif
2419
2420 #ifdef WIN
2421 static DWORD saveMode = 0;
2422
2423 int tty_break()
2424 {
2425 HANDLE stdinHandle = GetStdHandle (STD_INPUT_HANDLE);
2426
2427 GetConsoleMode (stdinHandle, &saveMode);
2428 SetConsoleMode (stdinHandle, ENABLE_PROCESSED_INPUT);
2429
2430 return 0;
2431 }
2432
2433 int tty_getchar()
2434 {
2435 HANDLE stdinHandle = GetStdHandle (STD_INPUT_HANDLE);
2436
2437 DWORD rc = WaitForSingleObject (stdinHandle, 1000);
2438
2439 if (rc == WAIT_TIMEOUT) return 0;
2440 if (rc == WAIT_ABANDONED) return -1;
2441 if (rc == WAIT_FAILED) return -1;
2442
2443 // The whole ReadConsoleInput () part is a workaround.
2444 // For some unknown reason, maybe a mingw bug, a random signal
2445 // is sent to stdin which unblocks WaitForSingleObject () and sets rc 0.
2446 // Then it wants to read with getche () a keyboard input
2447 // which has never been made.
2448
2449 INPUT_RECORD buf[100];
2450
2451 DWORD num = 0;
2452
2453 memset (buf, 0, sizeof (buf));
2454
2455 ReadConsoleInput (stdinHandle, buf, 100, &num);
2456
2457 FlushConsoleInputBuffer (stdinHandle);
2458
2459 for (uint i = 0; i < num; i++)
2460 {
2461 if (buf[i].EventType != KEY_EVENT) continue;
2462
2463 KEY_EVENT_RECORD KeyEvent = buf[i].Event.KeyEvent;
2464
2465 if (KeyEvent.bKeyDown != TRUE) continue;
2466
2467 return KeyEvent.uChar.AsciiChar;
2468 }
2469
2470 return 0;
2471 }
2472
2473 int tty_fix()
2474 {
2475 HANDLE stdinHandle = GetStdHandle (STD_INPUT_HANDLE);
2476
2477 SetConsoleMode (stdinHandle, saveMode);
2478
2479 return 0;
2480 }
2481 #endif
2482
2483 /**
2484 * mem alloc
2485 */
2486
2487 #define MSG_ENOMEM "Insufficient memory available"
2488
2489 void *mycalloc (size_t nmemb, size_t size)
2490 {
2491 void *p = calloc (nmemb, size);
2492
2493 if (p == NULL)
2494 {
2495 log_error ("ERROR: %s", MSG_ENOMEM);
2496
2497 exit (-1);
2498 }
2499
2500 return (p);
2501 }
2502
2503 void *mymalloc (size_t size)
2504 {
2505 void *p = malloc (size);
2506
2507 if (p == NULL)
2508 {
2509 log_error ("ERROR: %s", MSG_ENOMEM);
2510
2511 exit (-1);
2512 }
2513
2514 memset (p, 0, size);
2515
2516 return (p);
2517 }
2518
2519 void myfree (void *ptr)
2520 {
2521 if (ptr == NULL) return;
2522
2523 free (ptr);
2524 }
2525
2526 void *myrealloc (void *ptr, size_t oldsz, size_t add)
2527 {
2528 void *p = realloc (ptr, oldsz + add);
2529
2530 if (p == NULL)
2531 {
2532 log_error ("ERROR: %s", MSG_ENOMEM);
2533
2534 exit (-1);
2535 }
2536
2537 memset ((char *) p + oldsz, 0, add);
2538
2539 return (p);
2540 }
2541
2542 char *mystrdup (const char *s)
2543 {
2544 const size_t len = strlen (s);
2545
2546 char *b = (char *) mymalloc (len + 1);
2547
2548 memcpy (b, s, len);
2549
2550 return (b);
2551 }
2552
2553 FILE *logfile_open (char *logfile)
2554 {
2555 FILE *fp = fopen (logfile, "ab");
2556
2557 if (fp == NULL)
2558 {
2559 fp = stdout;
2560 }
2561
2562 return fp;
2563 }
2564
2565 void logfile_close (FILE *fp)
2566 {
2567 if (fp == stdout) return;
2568
2569 fclose (fp);
2570 }
2571
2572 void logfile_append (const char *fmt, ...)
2573 {
2574 if (data.logfile_disable == 1) return;
2575
2576 FILE *fp = logfile_open (data.logfile);
2577
2578 va_list ap;
2579
2580 va_start (ap, fmt);
2581
2582 vfprintf (fp, fmt, ap);
2583
2584 va_end (ap);
2585
2586 fputc ('\n', fp);
2587
2588 fflush (fp);
2589
2590 logfile_close (fp);
2591 }
2592
2593 int logfile_generate_id ()
2594 {
2595 const int n = rand ();
2596
2597 time_t t;
2598
2599 time (&t);
2600
2601 return t + n;
2602 }
2603
2604 char *logfile_generate_topid ()
2605 {
2606 const int id = logfile_generate_id ();
2607
2608 char *topid = (char *) mymalloc (1 + 16 + 1);
2609
2610 snprintf (topid, 1 + 16, "TOP%08x", id);
2611
2612 return topid;
2613 }
2614
2615 char *logfile_generate_subid ()
2616 {
2617 const int id = logfile_generate_id ();
2618
2619 char *subid = (char *) mymalloc (1 + 16 + 1);
2620
2621 snprintf (subid, 1 + 16, "SUB%08x", id);
2622
2623 return subid;
2624 }
2625
2626 /**
2627 * system
2628 */
2629
2630 #if F_SETLKW
2631 void lock_file (FILE *fp)
2632 {
2633 struct flock lock;
2634
2635 memset (&lock, 0, sizeof (struct flock));
2636
2637 lock.l_type = F_WRLCK;
2638 while (fcntl(fileno(fp), F_SETLKW, &lock))
2639 {
2640 if (errno != EINTR)
2641 {
2642 log_error ("ERROR: failed acquiring write lock: %s", strerror (errno));
2643
2644 exit (-1);
2645 }
2646 }
2647 }
2648
2649 void unlock_file (FILE *fp)
2650 {
2651 struct flock lock;
2652
2653 memset (&lock, 0, sizeof (struct flock));
2654
2655 lock.l_type = F_UNLCK;
2656 fcntl(fileno(fp), F_SETLK, &lock);
2657 }
2658 #endif // F_SETLKW
2659
2660 #ifdef _WIN
2661 void fsync (int fd)
2662 {
2663 HANDLE h = (HANDLE) _get_osfhandle (fd);
2664
2665 FlushFileBuffers (h);
2666 }
2667 #endif
2668
2669 /**
2670 * thermal
2671 */
2672
2673 #ifdef HAVE_HWMON
2674 #if defined(_WIN) && defined(HAVE_NVAPI)
2675 int hm_get_adapter_index_nv (HM_ADAPTER_NV nvGPUHandle[DEVICES_MAX])
2676 {
2677 NvU32 pGpuCount;
2678
2679 if (hm_NvAPI_EnumPhysicalGPUs (data.hm_nv, nvGPUHandle, &pGpuCount) != NVAPI_OK) return (0);
2680
2681 if (pGpuCount == 0)
2682 {
2683 log_info ("WARN: No NvAPI adapters found");
2684
2685 return (0);
2686 }
2687
2688 return (pGpuCount);
2689 }
2690 #endif // _WIN && HAVE_NVAPI
2691
2692 #if defined(LINUX) && defined(HAVE_NVML)
2693 int hm_get_adapter_index_nv (HM_ADAPTER_NV nvGPUHandle[DEVICES_MAX])
2694 {
2695 int pGpuCount = 0;
2696
2697 for (uint i = 0; i < DEVICES_MAX; i++)
2698 {
2699 if (hm_NVML_nvmlDeviceGetHandleByIndex (data.hm_nv, 1, i, &nvGPUHandle[i]) != NVML_SUCCESS) break;
2700
2701 // can be used to determine if the device by index matches the cuda device by index
2702 // char name[100]; memset (name, 0, sizeof (name));
2703 // hm_NVML_nvmlDeviceGetName (data.hm_nv, nvGPUHandle[i], name, sizeof (name) - 1);
2704
2705 pGpuCount++;
2706 }
2707
2708 if (pGpuCount == 0)
2709 {
2710 log_info ("WARN: No NVML adapters found");
2711
2712 return (0);
2713 }
2714
2715 return (pGpuCount);
2716 }
2717 #endif // LINUX && HAVE_NVML
2718
2719 #ifdef HAVE_ADL
2720 int get_adapters_num_amd (void *adl, int *iNumberAdapters)
2721 {
2722 if (hm_ADL_Adapter_NumberOfAdapters_Get ((ADL_PTR *) adl, iNumberAdapters) != ADL_OK) return -1;
2723
2724 if (iNumberAdapters == 0)
2725 {
2726 log_info ("WARN: No ADL adapters found.");
2727
2728 return -1;
2729 }
2730
2731 return 0;
2732 }
2733
2734 /*
2735 int hm_show_performance_level (HM_LIB hm_dll, int iAdapterIndex)
2736 {
2737 ADLODPerformanceLevels *lpOdPerformanceLevels = NULL;
2738 ADLODParameters lpOdParameters;
2739
2740 lpOdParameters.iSize = sizeof (ADLODParameters);
2741 size_t plevels_size = 0;
2742
2743 if (hm_ADL_Overdrive_ODParameters_Get (hm_dll, iAdapterIndex, &lpOdParameters) != ADL_OK) return -1;
2744
2745 log_info ("[DEBUG] %s, adapter %d performance level (%d) : %s %s",
2746 __func__, iAdapterIndex,
2747 lpOdParameters.iNumberOfPerformanceLevels,
2748 (lpOdParameters.iActivityReportingSupported) ? "activity reporting" : "",
2749 (lpOdParameters.iDiscretePerformanceLevels) ? "discrete performance levels" : "performance ranges");
2750
2751 plevels_size = sizeof (ADLODPerformanceLevels) + sizeof (ADLODPerformanceLevel) * (lpOdParameters.iNumberOfPerformanceLevels - 1);
2752
2753 lpOdPerformanceLevels = (ADLODPerformanceLevels *) mymalloc (plevels_size);
2754
2755 lpOdPerformanceLevels->iSize = sizeof (ADLODPerformanceLevels) + sizeof (ADLODPerformanceLevel) * (lpOdParameters.iNumberOfPerformanceLevels - 1);
2756
2757 if (hm_ADL_Overdrive_ODPerformanceLevels_Get (hm_dll, iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK) return -1;
2758
2759 for (int j = 0; j < lpOdParameters.iNumberOfPerformanceLevels; j++)
2760 log_info ("[DEBUG] %s, adapter %d, level %d : engine %d, memory %d, voltage: %d",
2761 __func__, iAdapterIndex, j,
2762 lpOdPerformanceLevels->aLevels[j].iEngineClock / 100, lpOdPerformanceLevels->aLevels[j].iMemoryClock / 100, lpOdPerformanceLevels->aLevels[j].iVddc);
2763
2764 myfree (lpOdPerformanceLevels);
2765
2766 return 0;
2767 }
2768 */
2769
2770 LPAdapterInfo hm_get_adapter_info_amd (void *adl, int iNumberAdapters)
2771 {
2772 size_t AdapterInfoSize = iNumberAdapters * sizeof (AdapterInfo);
2773
2774 LPAdapterInfo lpAdapterInfo = (LPAdapterInfo) mymalloc (AdapterInfoSize);
2775
2776 if (hm_ADL_Adapter_AdapterInfo_Get ((ADL_PTR *) adl, lpAdapterInfo, AdapterInfoSize) != ADL_OK) return NULL;
2777
2778 return lpAdapterInfo;
2779 }
2780
2781 /*
2782 //
2783 // does not help at all, since AMD does not assign different bus id, device id when we have multi GPU setups
2784 //
2785
2786 int hm_get_opencl_device_index (hm_attrs_t *hm_device, uint num_adl_adapters, int bus_num, int dev_num)
2787 {
2788 u32 idx = -1;
2789
2790 for (uint i = 0; i < num_adl_adapters; i++)
2791 {
2792 int opencl_bus_num = hm_device[i].busid;
2793 int opencl_dev_num = hm_device[i].devid;
2794
2795 if ((opencl_bus_num == bus_num) && (opencl_dev_num == dev_num))
2796 {
2797 idx = i;
2798
2799 break;
2800 }
2801 }
2802
2803 if (idx >= DEVICES_MAX) return -1;
2804
2805 return idx;
2806 }
2807
2808 void hm_get_opencl_busid_devid (hm_attrs_t *hm_device, uint opencl_num_devices, cl_device_id *devices)
2809 {
2810 for (uint i = 0; i < opencl_num_devices; i++)
2811 {
2812 cl_device_topology_amd device_topology;
2813
2814 hc_clGetDeviceInfo (devices[i], CL_DEVICE_TOPOLOGY_AMD, sizeof (device_topology), &device_topology, NULL);
2815
2816 hm_device[i].busid = device_topology.pcie.bus;
2817 hm_device[i].devid = device_topology.pcie.device;
2818 }
2819 }
2820 */
2821
2822 void hm_sort_adl_adapters_by_busid_devid (u32 *valid_adl_device_list, int num_adl_adapters, LPAdapterInfo lpAdapterInfo)
2823 {
2824 // basically bubble sort
2825
2826 for (int i = 0; i < num_adl_adapters; i++)
2827 {
2828 for (int j = 0; j < num_adl_adapters - 1; j++)
2829 {
2830 // get info of adapter [x]
2831
2832 u32 adapter_index_x = valid_adl_device_list[j];
2833 AdapterInfo info_x = lpAdapterInfo[adapter_index_x];
2834
2835 u32 bus_num_x = info_x.iBusNumber;
2836 u32 dev_num_x = info_x.iDeviceNumber;
2837
2838 // get info of adapter [y]
2839
2840 u32 adapter_index_y = valid_adl_device_list[j + 1];
2841 AdapterInfo info_y = lpAdapterInfo[adapter_index_y];
2842
2843 u32 bus_num_y = info_y.iBusNumber;
2844 u32 dev_num_y = info_y.iDeviceNumber;
2845
2846 uint need_swap = 0;
2847
2848 if (bus_num_y < bus_num_x)
2849 {
2850 need_swap = 1;
2851 }
2852 else if (bus_num_y == bus_num_x)
2853 {
2854 if (dev_num_y < dev_num_x)
2855 {
2856 need_swap = 1;
2857 }
2858 }
2859
2860 if (need_swap == 1)
2861 {
2862 u32 temp = valid_adl_device_list[j + 1];
2863
2864 valid_adl_device_list[j + 1] = valid_adl_device_list[j];
2865 valid_adl_device_list[j + 0] = temp;
2866 }
2867 }
2868 }
2869 }
2870
2871 u32 *hm_get_list_valid_adl_adapters (int iNumberAdapters, int *num_adl_adapters, LPAdapterInfo lpAdapterInfo)
2872 {
2873 *num_adl_adapters = 0;
2874
2875 u32 *adl_adapters = NULL;
2876
2877 int *bus_numbers = NULL;
2878 int *device_numbers = NULL;
2879
2880 for (int i = 0; i < iNumberAdapters; i++)
2881 {
2882 AdapterInfo info = lpAdapterInfo[i];
2883
2884 if (strlen (info.strUDID) < 1) continue;
2885
2886 #ifdef WIN
2887 if (info.iVendorID != 1002) continue;
2888 #else
2889 if (info.iVendorID != 0x1002) continue;
2890 #endif
2891
2892 if (info.iBusNumber < 0) continue;
2893 if (info.iDeviceNumber < 0) continue;
2894
2895 int found = 0;
2896
2897 for (int pos = 0; pos < *num_adl_adapters; pos++)
2898 {
2899 if ((bus_numbers[pos] == info.iBusNumber) && (device_numbers[pos] == info.iDeviceNumber))
2900 {
2901 found = 1;
2902 break;
2903 }
2904 }
2905
2906 if (found) continue;
2907
2908 // add it to the list
2909
2910 adl_adapters = (u32 *) myrealloc (adl_adapters, (*num_adl_adapters) * sizeof (int), sizeof (int));
2911
2912 adl_adapters[*num_adl_adapters] = i;
2913
2914 // rest is just bookkeeping
2915
2916 bus_numbers = (int*) myrealloc (bus_numbers, (*num_adl_adapters) * sizeof (int), sizeof (int));
2917 device_numbers = (int*) myrealloc (device_numbers, (*num_adl_adapters) * sizeof (int), sizeof (int));
2918
2919 bus_numbers[*num_adl_adapters] = info.iBusNumber;
2920 device_numbers[*num_adl_adapters] = info.iDeviceNumber;
2921
2922 (*num_adl_adapters)++;
2923 }
2924
2925 myfree (bus_numbers);
2926 myfree (device_numbers);
2927
2928 // sort the list by increasing bus id, device id number
2929
2930 hm_sort_adl_adapters_by_busid_devid (adl_adapters, *num_adl_adapters, lpAdapterInfo);
2931
2932 return adl_adapters;
2933 }
2934
2935 int hm_check_fanspeed_control (void *adl, hm_attrs_t *hm_device, u32 *valid_adl_device_list, int num_adl_adapters, LPAdapterInfo lpAdapterInfo)
2936 {
2937 // loop through all valid devices
2938
2939 for (int i = 0; i < num_adl_adapters; i++)
2940 {
2941 u32 adapter_index = valid_adl_device_list[i];
2942
2943 // get AdapterInfo
2944
2945 AdapterInfo info = lpAdapterInfo[adapter_index];
2946
2947 // unfortunately this doesn't work since bus id and dev id are not unique
2948 // int opencl_device_index = hm_get_opencl_device_index (hm_device, num_adl_adapters, info.iBusNumber, info.iDeviceNumber);
2949 // if (opencl_device_index == -1) continue;
2950
2951 int opencl_device_index = i;
2952
2953 // if (hm_show_performance_level (adl, info.iAdapterIndex) != 0) return -1;
2954
2955 // get fanspeed info
2956
2957 if (hm_device[opencl_device_index].od_version == 5)
2958 {
2959 ADLFanSpeedInfo FanSpeedInfo;
2960
2961 memset (&FanSpeedInfo, 0, sizeof (ADLFanSpeedInfo));
2962
2963 FanSpeedInfo.iSize = sizeof (ADLFanSpeedInfo);
2964
2965 if (hm_ADL_Overdrive5_FanSpeedInfo_Get (adl, info.iAdapterIndex, 0, &FanSpeedInfo) != ADL_OK) return -1;
2966
2967 // check read and write capability in fanspeedinfo
2968
2969 if ((FanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_PERCENT_READ) &&
2970 (FanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_PERCENT_WRITE))
2971 {
2972 hm_device[opencl_device_index].fan_supported = 1;
2973 }
2974 else
2975 {
2976 hm_device[opencl_device_index].fan_supported = 0;
2977 }
2978 }
2979 else // od_version == 6
2980 {
2981 ADLOD6FanSpeedInfo faninfo;
2982
2983 memset (&faninfo, 0, sizeof (faninfo));
2984
2985 if (hm_ADL_Overdrive6_FanSpeed_Get (adl, info.iAdapterIndex, &faninfo) != ADL_OK) return -1;
2986
2987 // check read capability in fanspeedinfo
2988
2989 if (faninfo.iSpeedType & ADL_OD6_FANSPEED_TYPE_PERCENT)
2990 {
2991 hm_device[opencl_device_index].fan_supported = 1;
2992 }
2993 else
2994 {
2995 hm_device[opencl_device_index].fan_supported = 0;
2996 }
2997 }
2998 }
2999
3000 return 0;
3001 }
3002
3003 int hm_get_overdrive_version (void *adl, hm_attrs_t *hm_device, u32 *valid_adl_device_list, int num_adl_adapters, LPAdapterInfo lpAdapterInfo)
3004 {
3005 for (int i = 0; i < num_adl_adapters; i++)
3006 {
3007 u32 adapter_index = valid_adl_device_list[i];
3008
3009 // get AdapterInfo
3010
3011 AdapterInfo info = lpAdapterInfo[adapter_index];
3012
3013 // get overdrive version
3014
3015 int od_supported = 0;
3016 int od_enabled = 0;
3017 int od_version = 0;
3018
3019 if (hm_ADL_Overdrive_Caps (adl, info.iAdapterIndex, &od_supported, &od_enabled, &od_version) != ADL_OK) return -1;
3020
3021 // store the overdrive version in hm_device
3022
3023 // unfortunately this doesn't work since bus id and dev id are not unique
3024 // int opencl_device_index = hm_get_opencl_device_index (hm_device, num_adl_adapters, info.iBusNumber, info.iDeviceNumber);
3025 // if (opencl_device_index == -1) continue;
3026
3027 int opencl_device_index = i;
3028
3029 hm_device[opencl_device_index].od_version = od_version;
3030 }
3031
3032 return 0;
3033 }
3034
3035 int hm_get_adapter_index_amd (hm_attrs_t *hm_device, u32 *valid_adl_device_list, int num_adl_adapters, LPAdapterInfo lpAdapterInfo)
3036 {
3037 for (int i = 0; i < num_adl_adapters; i++)
3038 {
3039 u32 adapter_index = valid_adl_device_list[i];
3040
3041 // get AdapterInfo
3042
3043 AdapterInfo info = lpAdapterInfo[adapter_index];
3044
3045 // store the iAdapterIndex in hm_device
3046
3047 // unfortunately this doesn't work since bus id and dev id are not unique
3048 // int opencl_device_index = hm_get_opencl_device_index (hm_device, num_adl_adapters, info.iBusNumber, info.iDeviceNumber);
3049 // if (opencl_device_index == -1) continue;
3050
3051 int opencl_device_index = i;
3052
3053 hm_device[opencl_device_index].adapter_index.amd = info.iAdapterIndex;
3054 }
3055
3056 return num_adl_adapters;
3057 }
3058 #endif // HAVE_ADL
3059
3060 int hm_get_temperature_with_device_id (const uint device_id)
3061 {
3062 if ((data.devices_param[device_id].device_type & CL_DEVICE_TYPE_GPU) == 0) return -1;
3063
3064 #ifdef HAVE_ADL
3065 if (data.devices_param[device_id].vendor_id == VENDOR_ID_AMD)
3066 {
3067 if (data.hm_amd)
3068 {
3069 if (data.hm_device[device_id].od_version == 5)
3070 {
3071 ADLTemperature Temperature;
3072
3073 Temperature.iSize = sizeof (ADLTemperature);
3074
3075 if (hm_ADL_Overdrive5_Temperature_Get (data.hm_amd, data.hm_device[device_id].adapter_index.amd, 0, &Temperature) != ADL_OK) return -1;
3076
3077 return Temperature.iTemperature / 1000;
3078 }
3079 else if (data.hm_device[device_id].od_version == 6)
3080 {
3081 int Temperature = 0;
3082
3083 if (hm_ADL_Overdrive6_Temperature_Get (data.hm_amd, data.hm_device[device_id].adapter_index.amd, &Temperature) != ADL_OK) return -1;
3084
3085 return Temperature / 1000;
3086 }
3087 }
3088 }
3089 #endif
3090
3091 #if defined(HAVE_NVML) || defined(HAVE_NVAPI)
3092 if (data.devices_param[device_id].vendor_id == VENDOR_ID_NV)
3093 {
3094 #if defined(LINUX) && defined(HAVE_NVML)
3095 int temperature = 0;
3096
3097 hm_NVML_nvmlDeviceGetTemperature (data.hm_nv, data.hm_device[device_id].adapter_index.nv, NVML_TEMPERATURE_GPU, (uint *) &temperature);
3098
3099 return temperature;
3100 #endif
3101
3102 #if defined(WIN) && defined(HAVE_NVAPI)
3103 NV_GPU_THERMAL_SETTINGS pThermalSettings;
3104
3105 pThermalSettings.version = NV_GPU_THERMAL_SETTINGS_VER;
3106 pThermalSettings.count = NVAPI_MAX_THERMAL_SENSORS_PER_GPU;
3107 pThermalSettings.sensor[0].controller = NVAPI_THERMAL_CONTROLLER_UNKNOWN;
3108 pThermalSettings.sensor[0].target = NVAPI_THERMAL_TARGET_GPU;
3109
3110 if (hm_NvAPI_GPU_GetThermalSettings (data.hm_nv, data.hm_device[device_id].adapter_index.nv, 0, &pThermalSettings) != NVAPI_OK) return -1;
3111
3112 return pThermalSettings.sensor[0].currentTemp;
3113 #endif // WIN && HAVE_NVAPI
3114 }
3115 #endif // HAVE_NVML || HAVE_NVAPI
3116
3117 return -1;
3118 }
3119
3120 int hm_get_fanspeed_with_device_id (const uint device_id)
3121 {
3122 // we shouldn't really need this extra CL_DEVICE_TYPE_GPU check, because fan_supported should not be set w/ CPUs
3123 if ((data.devices_param[device_id].device_type & CL_DEVICE_TYPE_GPU) == 0) return -1;
3124
3125 if (data.hm_device[device_id].fan_supported == 1)
3126 {
3127 #ifdef HAVE_ADL
3128 if (data.devices_param[device_id].vendor_id == VENDOR_ID_AMD)
3129 {
3130 if (data.hm_amd)
3131 {
3132 if (data.hm_device[device_id].od_version == 5)
3133 {
3134 ADLFanSpeedValue lpFanSpeedValue;
3135
3136 memset (&lpFanSpeedValue, 0, sizeof (lpFanSpeedValue));
3137
3138 lpFanSpeedValue.iSize = sizeof (lpFanSpeedValue);
3139 lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
3140 lpFanSpeedValue.iFlags = ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED;
3141
3142 if (hm_ADL_Overdrive5_FanSpeed_Get (data.hm_amd, data.hm_device[device_id].adapter_index.amd, 0, &lpFanSpeedValue) != ADL_OK) return -1;
3143
3144 return lpFanSpeedValue.iFanSpeed;
3145 }
3146 else // od_version == 6
3147 {
3148 ADLOD6FanSpeedInfo faninfo;
3149
3150 memset (&faninfo, 0, sizeof (faninfo));
3151
3152 if (hm_ADL_Overdrive6_FanSpeed_Get (data.hm_amd, data.hm_device[device_id].adapter_index.amd, &faninfo) != ADL_OK) return -1;
3153
3154 return faninfo.iFanSpeedPercent;
3155 }
3156 }
3157 }
3158 #endif // HAVE_ADL
3159
3160 #if defined(HAVE_NVML) || defined(HAVE_NVAPI)
3161 if (data.devices_param[device_id].vendor_id == VENDOR_ID_NV)
3162 {
3163 #if defined(LINUX) && defined(HAVE_NVML)
3164 int speed = 0;
3165
3166 hm_NVML_nvmlDeviceGetFanSpeed (data.hm_nv, 1, data.hm_device[device_id].adapter_index.nv, (uint *) &speed);
3167
3168 return speed;
3169 #endif
3170
3171 #if defined(WIN) && defined(HAVE_NVAPI)
3172
3173 NV_GPU_COOLER_SETTINGS pCoolerSettings;
3174
3175 pCoolerSettings.Version = GPU_COOLER_SETTINGS_VER | sizeof (NV_GPU_COOLER_SETTINGS);
3176
3177 hm_NvAPI_GPU_GetCoolerSettings (data.hm_nv, data.hm_device[device_id].adapter_index.nv, 0, &pCoolerSettings);
3178
3179 return pCoolerSettings.Cooler[0].CurrentLevel;
3180 #endif
3181 }
3182 #endif // HAVE_NVML || HAVE_NVAPI
3183 }
3184
3185 return -1;
3186 }
3187
3188 int hm_get_utilization_with_device_id (const uint device_id)
3189 {
3190 if ((data.devices_param[device_id].device_type & CL_DEVICE_TYPE_GPU) == 0) return -1;
3191
3192 #ifdef HAVE_ADL
3193 if (data.devices_param[device_id].vendor_id == VENDOR_ID_AMD)
3194 {
3195 if (data.hm_amd)
3196 {
3197 ADLPMActivity PMActivity;
3198
3199 PMActivity.iSize = sizeof (ADLPMActivity);
3200
3201 if (hm_ADL_Overdrive_CurrentActivity_Get (data.hm_amd, data.hm_device[device_id].adapter_index.amd, &PMActivity) != ADL_OK) return -1;
3202
3203 return PMActivity.iActivityPercent;
3204 }
3205 }
3206 #endif // HAVE_ADL
3207
3208 #if defined(HAVE_NVML) || defined(HAVE_NVAPI)
3209 if (data.devices_param[device_id].vendor_id == VENDOR_ID_NV)
3210 {
3211 #if defined(LINUX) && defined(HAVE_NVML)
3212 nvmlUtilization_t utilization;
3213
3214 hm_NVML_nvmlDeviceGetUtilizationRates (data.hm_nv, data.hm_device[device_id].adapter_index.nv, &utilization);
3215
3216 return utilization.gpu;
3217 #endif
3218
3219 #if defined(WIN) && defined(HAVE_NVAPI)
3220 NV_GPU_DYNAMIC_PSTATES_INFO_EX pDynamicPstatesInfoEx;
3221
3222 pDynamicPstatesInfoEx.version = NV_GPU_DYNAMIC_PSTATES_INFO_EX_VER;
3223
3224 if (hm_NvAPI_GPU_GetDynamicPstatesInfoEx (data.hm_nv, data.hm_device[device_id].adapter_index.nv, &pDynamicPstatesInfoEx) != NVAPI_OK) return -1;
3225
3226 return pDynamicPstatesInfoEx.utilization[0].percentage;
3227 #endif
3228 }
3229 #endif // HAVE_NVML || HAVE_NVAPI
3230
3231 return -1;
3232 }
3233
3234 #ifdef HAVE_ADL
3235 int hm_set_fanspeed_with_device_id_amd (const uint device_id, const int fanspeed)
3236 {
3237 if (data.hm_device[device_id].fan_supported == 1)
3238 {
3239 if (data.hm_amd)
3240 {
3241 if (data.hm_device[device_id].od_version == 5)
3242 {
3243 ADLFanSpeedValue lpFanSpeedValue;
3244
3245 memset (&lpFanSpeedValue, 0, sizeof (lpFanSpeedValue));
3246
3247 lpFanSpeedValue.iSize = sizeof (lpFanSpeedValue);
3248 lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
3249 lpFanSpeedValue.iFlags = ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED;
3250 lpFanSpeedValue.iFanSpeed = fanspeed;
3251
3252 if (hm_ADL_Overdrive5_FanSpeed_Set (data.hm_amd, data.hm_device[device_id].adapter_index.amd, 0, &lpFanSpeedValue) != ADL_OK) return -1;
3253
3254 return 0;
3255 }
3256 else // od_version == 6
3257 {
3258 ADLOD6FanSpeedValue fan_speed_value;
3259
3260 memset (&fan_speed_value, 0, sizeof (fan_speed_value));
3261
3262 fan_speed_value.iSpeedType = ADL_OD6_FANSPEED_TYPE_PERCENT;
3263 fan_speed_value.iFanSpeed = fanspeed;
3264
3265 if (hm_ADL_Overdrive6_FanSpeed_Set (data.hm_amd, data.hm_device[device_id].adapter_index.amd, &fan_speed_value) != ADL_OK) return -1;
3266
3267 return 0;
3268 }
3269 }
3270 }
3271
3272 return -1;
3273 }
3274 #endif
3275
3276 // helper function for status display
3277
3278 void hm_device_val_to_str (char *target_buf, int max_buf_size, char *suffix, int value)
3279 {
3280 #define VALUE_NOT_AVAILABLE "N/A"
3281
3282 if (value == -1)
3283 {
3284 snprintf (target_buf, max_buf_size, VALUE_NOT_AVAILABLE);
3285 }
3286 else
3287 {
3288 snprintf (target_buf, max_buf_size, "%2d%s", value, suffix);
3289 }
3290 }
3291 #endif // HAVE_HWMON
3292
3293 /**
3294 * maskprocessor
3295 */
3296
3297 void mp_css_to_uniq_tbl (uint css_cnt, cs_t *css, uint uniq_tbls[SP_PW_MAX][CHARSIZ])
3298 {
3299 /* generates a lookup table where key is the char itself for fastest possible lookup performance */
3300
3301 if (css_cnt > SP_PW_MAX)
3302 {
3303 log_error ("ERROR: mask length is too long");
3304
3305 exit (-1);
3306 }
3307
3308 for (uint css_pos = 0; css_pos < css_cnt; css_pos++)
3309 {
3310 uint *uniq_tbl = uniq_tbls[css_pos];
3311
3312 uint *cs_buf = css[css_pos].cs_buf;
3313 uint cs_len = css[css_pos].cs_len;
3314
3315 for (uint cs_pos = 0; cs_pos < cs_len; cs_pos++)
3316 {
3317 uint c = cs_buf[cs_pos] & 0xff;
3318
3319 uniq_tbl[c] = 1;
3320 }
3321 }
3322 }
3323
3324 void mp_add_cs_buf (uint *in_buf, size_t in_len, cs_t *css, int css_cnt)
3325 {
3326 cs_t *cs = &css[css_cnt];
3327
3328 size_t css_uniq_sz = CHARSIZ * sizeof (uint);
3329
3330 uint *css_uniq = (uint *) mymalloc (css_uniq_sz);
3331
3332 size_t i;
3333
3334 for (i = 0; i < cs->cs_len; i++)
3335 {
3336 const uint u = cs->cs_buf[i];
3337
3338 css_uniq[u] = 1;
3339 }
3340
3341 for (i = 0; i < in_len; i++)
3342 {
3343 uint u = in_buf[i] & 0xff;
3344
3345 if (data.opts_type & OPTS_TYPE_PT_UPPER) u = toupper (u);
3346
3347 if (css_uniq[u] == 1) continue;
3348
3349 css_uniq[u] = 1;
3350
3351 cs->cs_buf[cs->cs_len] = u;
3352
3353 cs->cs_len++;
3354 }
3355
3356 myfree (css_uniq);
3357 }
3358
3359 void mp_expand (char *in_buf, size_t in_len, cs_t *mp_sys, cs_t *mp_usr, int mp_usr_offset, int interpret)
3360 {
3361 size_t in_pos;
3362
3363 for (in_pos = 0; in_pos < in_len; in_pos++)
3364 {
3365 uint p0 = in_buf[in_pos] & 0xff;
3366
3367 if (interpret == 1 && p0 == '?')
3368 {
3369 in_pos++;
3370
3371 if (in_pos == in_len) break;
3372
3373 uint p1 = in_buf[in_pos] & 0xff;
3374
3375 switch (p1)
3376 {
3377 case 'l': mp_add_cs_buf (mp_sys[0].cs_buf, mp_sys[0].cs_len, mp_usr, mp_usr_offset);
3378 break;
3379 case 'u': mp_add_cs_buf (mp_sys[1].cs_buf, mp_sys[1].cs_len, mp_usr, mp_usr_offset);
3380 break;
3381 case 'd': mp_add_cs_buf (mp_sys[2].cs_buf, mp_sys[2].cs_len, mp_usr, mp_usr_offset);
3382 break;
3383 case 's': mp_add_cs_buf (mp_sys[3].cs_buf, mp_sys[3].cs_len, mp_usr, mp_usr_offset);
3384 break;
3385 case 'a': mp_add_cs_buf (mp_sys[4].cs_buf, mp_sys[4].cs_len, mp_usr, mp_usr_offset);
3386 break;
3387 case 'b': mp_add_cs_buf (mp_sys[5].cs_buf, mp_sys[5].cs_len, mp_usr, mp_usr_offset);
3388 break;
3389 case '1': if (mp_usr[0].cs_len == 0) { log_error ("ERROR: Custom-charset 1 is undefined\n"); exit (-1); }
3390 mp_add_cs_buf (mp_usr[0].cs_buf, mp_usr[0].cs_len, mp_usr, mp_usr_offset);
3391 break;
3392 case '2': if (mp_usr[1].cs_len == 0) { log_error ("ERROR: Custom-charset 2 is undefined\n"); exit (-1); }
3393 mp_add_cs_buf (mp_usr[1].cs_buf, mp_usr[1].cs_len, mp_usr, mp_usr_offset);
3394 break;
3395 case '3': if (mp_usr[2].cs_len == 0) { log_error ("ERROR: Custom-charset 3 is undefined\n"); exit (-1); }
3396 mp_add_cs_buf (mp_usr[2].cs_buf, mp_usr[2].cs_len, mp_usr, mp_usr_offset);
3397 break;
3398 case '4': if (mp_usr[3].cs_len == 0) { log_error ("ERROR: Custom-charset 4 is undefined\n"); exit (-1); }
3399 mp_add_cs_buf (mp_usr[3].cs_buf, mp_usr[3].cs_len, mp_usr, mp_usr_offset);
3400 break;
3401 case '?': mp_add_cs_buf (&p0, 1, mp_usr, mp_usr_offset);
3402 break;
3403 default: log_error ("Syntax error: %s", in_buf);
3404 exit (-1);
3405 }
3406 }
3407 else
3408 {
3409 if (data.hex_charset)
3410 {
3411 in_pos++;
3412
3413 if (in_pos == in_len)
3414 {
3415 log_error ("ERROR: the hex-charset option always expects couples of exactly 2 hexadecimal chars, failed mask: %s", in_buf);
3416
3417 exit (-1);
3418 }
3419
3420 uint p1 = in_buf[in_pos] & 0xff;
3421
3422 if ((is_valid_hex_char (p0) == 0) || (is_valid_hex_char (p1) == 0))
3423 {
3424 log_error ("ERROR: invalid hex character detected in mask %s", in_buf);
3425
3426 exit (-1);
3427 }
3428
3429 uint chr = 0;
3430
3431 chr = hex_convert (p1) << 0;
3432 chr |= hex_convert (p0) << 4;
3433
3434 mp_add_cs_buf (&chr, 1, mp_usr, mp_usr_offset);
3435 }
3436 else
3437 {
3438 uint chr = p0;
3439
3440 mp_add_cs_buf (&chr, 1, mp_usr, mp_usr_offset);
3441 }
3442 }
3443 }
3444 }
3445
3446 u64 mp_get_sum (uint css_cnt, cs_t *css)
3447 {
3448 u64 sum = 1;
3449
3450 for (uint css_pos = 0; css_pos < css_cnt; css_pos++)
3451 {
3452 sum *= css[css_pos].cs_len;
3453 }
3454
3455 return (sum);
3456 }
3457
3458 cs_t *mp_gen_css (char *mask_buf, size_t mask_len, cs_t *mp_sys, cs_t *mp_usr, uint *css_cnt)
3459 {
3460 cs_t *css = (cs_t *) mycalloc (256, sizeof (cs_t));
3461
3462 uint mask_pos;
3463 uint css_pos;
3464
3465 for (mask_pos = 0, css_pos = 0; mask_pos < mask_len; mask_pos++, css_pos++)
3466 {
3467 char p0 = mask_buf[mask_pos];
3468
3469 if (p0 == '?')
3470 {
3471 mask_pos++;
3472
3473 if (mask_pos == mask_len) break;
3474
3475 char p1 = mask_buf[mask_pos];
3476
3477 uint chr = p1;
3478
3479 switch (p1)
3480 {
3481 case 'l': mp_add_cs_buf (mp_sys[0].cs_buf, mp_sys[0].cs_len, css, css_pos);
3482 break;
3483 case 'u': mp_add_cs_buf (mp_sys[1].cs_buf, mp_sys[1].cs_len, css, css_pos);
3484 break;
3485 case 'd': mp_add_cs_buf (mp_sys[2].cs_buf, mp_sys[2].cs_len, css, css_pos);
3486 break;
3487 case 's': mp_add_cs_buf (mp_sys[3].cs_buf, mp_sys[3].cs_len, css, css_pos);
3488 break;
3489 case 'a': mp_add_cs_buf (mp_sys[4].cs_buf, mp_sys[4].cs_len, css, css_pos);
3490 break;
3491 case 'b': mp_add_cs_buf (mp_sys[5].cs_buf, mp_sys[5].cs_len, css, css_pos);
3492 break;
3493 case '1': if (mp_usr[0].cs_len == 0) { log_error ("ERROR: Custom-charset 1 is undefined\n"); exit (-1); }
3494 mp_add_cs_buf (mp_usr[0].cs_buf, mp_usr[0].cs_len, css, css_pos);
3495 break;
3496 case '2': if (mp_usr[1].cs_len == 0) { log_error ("ERROR: Custom-charset 2 is undefined\n"); exit (-1); }
3497 mp_add_cs_buf (mp_usr[1].cs_buf, mp_usr[1].cs_len, css, css_pos);
3498 break;
3499 case '3': if (mp_usr[2].cs_len == 0) { log_error ("ERROR: Custom-charset 3 is undefined\n"); exit (-1); }
3500 mp_add_cs_buf (mp_usr[2].cs_buf, mp_usr[2].cs_len, css, css_pos);
3501 break;
3502 case '4': if (mp_usr[3].cs_len == 0) { log_error ("ERROR: Custom-charset 4 is undefined\n"); exit (-1); }
3503 mp_add_cs_buf (mp_usr[3].cs_buf, mp_usr[3].cs_len, css, css_pos);
3504 break;
3505 case '?': mp_add_cs_buf (&chr, 1, css, css_pos);
3506 break;
3507 default: log_error ("ERROR: syntax error: %s", mask_buf);
3508 exit (-1);
3509 }
3510 }
3511 else
3512 {
3513 if (data.hex_charset)
3514 {
3515 mask_pos++;
3516
3517 // if there is no 2nd hex character, show an error:
3518
3519 if (mask_pos == mask_len)
3520 {
3521 log_error ("ERROR: the hex-charset option always expects couples of exactly 2 hexadecimal chars, failed mask: %s", mask_buf);
3522
3523 exit (-1);
3524 }
3525
3526 char p1 = mask_buf[mask_pos];
3527
3528 // if they are not valid hex character, show an error:
3529
3530 if ((is_valid_hex_char (p0) == 0) || (is_valid_hex_char (p1) == 0))
3531 {
3532 log_error ("ERROR: invalid hex character detected in mask %s", mask_buf);
3533
3534 exit (-1);
3535 }
3536
3537 uint chr = 0;
3538
3539 chr |= hex_convert (p1) << 0;
3540 chr |= hex_convert (p0) << 4;
3541
3542 mp_add_cs_buf (&chr, 1, css, css_pos);
3543 }
3544 else
3545 {
3546 uint chr = p0;
3547
3548 mp_add_cs_buf (&chr, 1, css, css_pos);
3549 }
3550 }
3551 }
3552
3553 if (css_pos == 0)
3554 {
3555 log_error ("ERROR: invalid mask length (0)");
3556
3557 exit (-1);
3558 }
3559
3560 *css_cnt = css_pos;
3561
3562 return (css);
3563 }
3564
3565 void mp_exec (u64 val, char *buf, cs_t *css, int css_cnt)
3566 {
3567 for (int i = 0; i < css_cnt; i++)
3568 {
3569 uint len = css[i].cs_len;
3570 u64 next = val / len;
3571 uint pos = val % len;
3572 buf[i] = (char) css[i].cs_buf[pos] & 0xff;
3573 val = next;
3574 }
3575 }
3576
3577 void mp_cut_at (char *mask, uint max)
3578 {
3579 uint i;
3580 uint j;
3581 uint mask_len = strlen (mask);
3582
3583 for (i = 0, j = 0; i < mask_len && j < max; i++, j++)
3584 {
3585 if (mask[i] == '?') i++;
3586 }
3587
3588 mask[i] = 0;
3589 }
3590
3591 void mp_setup_sys (cs_t *mp_sys)
3592 {
3593 uint pos;
3594 uint chr;
3595 uint donec[CHARSIZ] = { 0 };
3596
3597 for (pos = 0, chr = 'a'; chr <= 'z'; chr++) { donec[chr] = 1;
3598 mp_sys[0].cs_buf[pos++] = chr;
3599 mp_sys[0].cs_len = pos; }
3600
3601 for (pos = 0, chr = 'A'; chr <= 'Z'; chr++) { donec[chr] = 1;
3602 mp_sys[1].cs_buf[pos++] = chr;
3603 mp_sys[1].cs_len = pos; }
3604
3605 for (pos = 0, chr = '0'; chr <= '9'; chr++) { donec[chr] = 1;
3606 mp_sys[2].cs_buf[pos++] = chr;
3607 mp_sys[2].cs_len = pos; }
3608
3609 for (pos = 0, chr = 0x20; chr <= 0x7e; chr++) { if (donec[chr]) continue;
3610 mp_sys[3].cs_buf[pos++] = chr;
3611 mp_sys[3].cs_len = pos; }
3612
3613 for (pos = 0, chr = 0x20; chr <= 0x7e; chr++) { mp_sys[4].cs_buf[pos++] = chr;
3614 mp_sys[4].cs_len = pos; }
3615
3616 for (pos = 0, chr = 0x00; chr <= 0xff; chr++) { mp_sys[5].cs_buf[pos++] = chr;
3617 mp_sys[5].cs_len = pos; }
3618 }
3619
3620 void mp_setup_usr (cs_t *mp_sys, cs_t *mp_usr, char *buf, uint index)
3621 {
3622 FILE *fp = fopen (buf, "rb");
3623
3624 if (fp == NULL || feof (fp)) // feof() in case if file is empty
3625 {
3626 mp_expand (buf, strlen (buf), mp_sys, mp_usr, index, 1);
3627 }
3628 else
3629 {
3630 char mp_file[1024] = { 0 };
3631
3632 size_t len = fread (mp_file, 1, sizeof (mp_file) - 1, fp);
3633
3634 fclose (fp);
3635
3636 len = in_superchop (mp_file);
3637
3638 if (len == 0)
3639 {
3640 log_info ("WARNING: charset file corrupted");
3641
3642 mp_expand (buf, strlen (buf), mp_sys, mp_usr, index, 1);
3643 }
3644 else
3645 {
3646 mp_expand (mp_file, len, mp_sys, mp_usr, index, 0);
3647 }
3648 }
3649 }
3650
3651 void mp_reset_usr (cs_t *mp_usr, uint index)
3652 {
3653 mp_usr[index].cs_len = 0;
3654
3655 memset (mp_usr[index].cs_buf, 0, sizeof (mp_usr[index].cs_buf));
3656 }
3657
3658 char *mp_get_truncated_mask (char *mask_buf, size_t mask_len, uint len)
3659 {
3660 char *new_mask_buf = (char *) mymalloc (256);
3661
3662 uint mask_pos;
3663
3664 uint css_pos;
3665
3666 for (mask_pos = 0, css_pos = 0; mask_pos < mask_len; mask_pos++, css_pos++)
3667 {
3668 if (css_pos == len) break;
3669
3670 char p0 = mask_buf[mask_pos];
3671
3672 new_mask_buf[mask_pos] = p0;
3673
3674 if (p0 == '?')
3675 {
3676 mask_pos++;
3677
3678 if (mask_pos == mask_len) break;
3679
3680 new_mask_buf[mask_pos] = mask_buf[mask_pos];
3681 }
3682 else
3683 {
3684 if (data.hex_charset)
3685 {
3686 mask_pos++;
3687
3688 if (mask_pos == mask_len)
3689 {
3690 log_error ("ERROR: the hex-charset option always expects couples of exactly 2 hexadecimal chars, failed mask: %s", mask_buf);
3691
3692 exit (-1);
3693 }
3694
3695 char p1 = mask_buf[mask_pos];
3696
3697 // if they are not valid hex character, show an error:
3698
3699 if ((is_valid_hex_char (p0) == 0) || (is_valid_hex_char (p1) == 0))
3700 {
3701 log_error ("ERROR: invalid hex character detected in mask: %s", mask_buf);
3702
3703 exit (-1);
3704 }
3705
3706 new_mask_buf[mask_pos] = p1;
3707 }
3708 }
3709 }
3710
3711 if (css_pos == len) return (new_mask_buf);
3712
3713 myfree (new_mask_buf);
3714
3715 return (NULL);
3716 }
3717
3718 /**
3719 * statprocessor
3720 */
3721
3722 u64 sp_get_sum (uint start, uint stop, cs_t *root_css_buf)
3723 {
3724 u64 sum = 1;
3725
3726 uint i;
3727
3728 for (i = start; i < stop; i++)
3729 {
3730 sum *= root_css_buf[i].cs_len;
3731 }
3732
3733 return (sum);
3734 }
3735
3736 void sp_exec (u64 ctx, char *pw_buf, cs_t *root_css_buf, cs_t *markov_css_buf, uint start, uint stop)
3737 {
3738 u64 v = ctx;
3739
3740 cs_t *cs = &root_css_buf[start];
3741
3742 uint i;
3743
3744 for (i = start; i < stop; i++)
3745 {
3746 const u64 m = v % cs->cs_len;
3747 const u64 d = v / cs->cs_len;
3748
3749 v = d;
3750
3751 const uint k = cs->cs_buf[m];
3752
3753 pw_buf[i - start] = (char) k;
3754
3755 cs = &markov_css_buf[(i * CHARSIZ) + k];
3756 }
3757 }
3758
3759 int sp_comp_val (const void *p1, const void *p2)
3760 {
3761 hcstat_table_t *b1 = (hcstat_table_t *) p1;
3762 hcstat_table_t *b2 = (hcstat_table_t *) p2;
3763
3764 return b2->val - b1->val;
3765 }
3766
3767 void sp_setup_tbl (const char *shared_dir, char *hcstat, uint disable, uint classic, hcstat_table_t *root_table_buf, hcstat_table_t *markov_table_buf)
3768 {
3769 uint i;
3770 uint j;
3771 uint k;
3772
3773 /**
3774 * Initialize hcstats
3775 */
3776
3777 u64 *root_stats_buf = (u64 *) mycalloc (SP_ROOT_CNT, sizeof (u64));
3778
3779 u64 *root_stats_ptr = root_stats_buf;
3780
3781 u64 *root_stats_buf_by_pos[SP_PW_MAX];
3782
3783 for (i = 0; i < SP_PW_MAX; i++)
3784 {
3785 root_stats_buf_by_pos[i] = root_stats_ptr;
3786
3787 root_stats_ptr += CHARSIZ;
3788 }
3789
3790 u64 *markov_stats_buf = (u64 *) mycalloc (SP_MARKOV_CNT, sizeof (u64));
3791
3792 u64 *markov_stats_ptr = markov_stats_buf;
3793
3794 u64 *markov_stats_buf_by_key[SP_PW_MAX][CHARSIZ];
3795
3796 for (i = 0; i < SP_PW_MAX; i++)
3797 {
3798 for (j = 0; j < CHARSIZ; j++)
3799 {
3800 markov_stats_buf_by_key[i][j] = markov_stats_ptr;
3801
3802 markov_stats_ptr += CHARSIZ;
3803 }
3804 }
3805
3806 /**
3807 * Load hcstats File
3808 */
3809
3810 if (hcstat == NULL)
3811 {
3812 char hcstat_tmp[256] = { 0 };
3813
3814 snprintf (hcstat_tmp, sizeof (hcstat_tmp) - 1, "%s/%s", shared_dir, SP_HCSTAT);
3815
3816 hcstat = hcstat_tmp;
3817 }
3818
3819 FILE *fd = fopen (hcstat, "rb");
3820
3821 if (fd == NULL)
3822 {
3823 log_error ("%s: %s", hcstat, strerror (errno));
3824
3825 exit (-1);
3826 }
3827
3828 if (fread (root_stats_buf, sizeof (u64), SP_ROOT_CNT, fd) != SP_ROOT_CNT)
3829 {
3830 log_error ("%s: Could not load data", hcstat);
3831
3832 fclose (fd);
3833
3834 exit (-1);
3835 }
3836
3837 if (fread (markov_stats_buf, sizeof (u64), SP_MARKOV_CNT, fd) != SP_MARKOV_CNT)
3838 {
3839 log_error ("%s: Could not load data", hcstat);
3840
3841 fclose (fd);
3842
3843 exit (-1);
3844 }
3845
3846 fclose (fd);
3847
3848 /**
3849 * Markov modifier of hcstat_table on user request
3850 */
3851
3852 if (disable)
3853 {
3854 memset (root_stats_buf, 0, SP_ROOT_CNT * sizeof (u64));
3855 memset (markov_stats_buf, 0, SP_MARKOV_CNT * sizeof (u64));
3856 }
3857
3858 if (classic)
3859 {
3860 /* Add all stats to first position */
3861
3862 for (i = 1; i < SP_PW_MAX; i++)
3863 {
3864 u64 *out = root_stats_buf_by_pos[0];
3865 u64 *in = root_stats_buf_by_pos[i];
3866
3867 for (j = 0; j < CHARSIZ; j++)
3868 {
3869 *out++ += *in++;
3870 }
3871 }
3872
3873 for (i = 1; i < SP_PW_MAX; i++)
3874 {
3875 u64 *out = markov_stats_buf_by_key[0][0];
3876 u64 *in = markov_stats_buf_by_key[i][0];
3877
3878 for (j = 0; j < CHARSIZ; j++)
3879 {
3880 for (k = 0; k < CHARSIZ; k++)
3881 {
3882 *out++ += *in++;
3883 }
3884 }
3885 }
3886
3887 /* copy them to all pw_positions */
3888
3889 for (i = 1; i < SP_PW_MAX; i++)
3890 {
3891 memcpy (root_stats_buf_by_pos[i], root_stats_buf_by_pos[0], CHARSIZ * sizeof (u64));
3892 }
3893
3894 for (i = 1; i < SP_PW_MAX; i++)
3895 {
3896 memcpy (markov_stats_buf_by_key[i][0], markov_stats_buf_by_key[0][0], CHARSIZ * CHARSIZ * sizeof (u64));
3897 }
3898 }
3899
3900 /**
3901 * Initialize tables
3902 */
3903
3904 hcstat_table_t *root_table_ptr = root_table_buf;
3905
3906 hcstat_table_t *root_table_buf_by_pos[SP_PW_MAX];
3907
3908 for (i = 0; i < SP_PW_MAX; i++)
3909 {
3910 root_table_buf_by_pos[i] = root_table_ptr;
3911
3912 root_table_ptr += CHARSIZ;
3913 }
3914
3915 hcstat_table_t *markov_table_ptr = markov_table_buf;
3916
3917 hcstat_table_t *markov_table_buf_by_key[SP_PW_MAX][CHARSIZ];
3918
3919 for (i = 0; i < SP_PW_MAX; i++)
3920 {
3921 for (j = 0; j < CHARSIZ; j++)
3922 {
3923 markov_table_buf_by_key[i][j] = markov_table_ptr;
3924
3925 markov_table_ptr += CHARSIZ;
3926 }
3927 }
3928
3929 /**
3930 * Convert hcstat to tables
3931 */
3932
3933 for (i = 0; i < SP_ROOT_CNT; i++)
3934 {
3935 uint key = i % CHARSIZ;
3936
3937 root_table_buf[i].key = key;
3938 root_table_buf[i].val = root_stats_buf[i];
3939 }
3940
3941 for (i = 0; i < SP_MARKOV_CNT; i++)
3942 {
3943 uint key = i % CHARSIZ;
3944
3945 markov_table_buf[i].key = key;
3946 markov_table_buf[i].val = markov_stats_buf[i];
3947 }
3948
3949 myfree (root_stats_buf);
3950 myfree (markov_stats_buf);
3951
3952 /**
3953 * Finally sort them
3954 */
3955
3956 for (i = 0; i < SP_PW_MAX; i++)
3957 {
3958 qsort (root_table_buf_by_pos[i], CHARSIZ, sizeof (hcstat_table_t), sp_comp_val);
3959 }
3960
3961 for (i = 0; i < SP_PW_MAX; i++)
3962 {
3963 for (j = 0; j < CHARSIZ; j++)
3964 {
3965 qsort (markov_table_buf_by_key[i][j], CHARSIZ, sizeof (hcstat_table_t), sp_comp_val);
3966 }
3967 }
3968 }
3969
3970 void sp_tbl_to_css (hcstat_table_t *root_table_buf, hcstat_table_t *markov_table_buf, cs_t *root_css_buf, cs_t *markov_css_buf, uint threshold, uint uniq_tbls[SP_PW_MAX][CHARSIZ])
3971 {
3972 /**
3973 * Convert tables to css
3974 */
3975
3976 for (uint i = 0; i < SP_ROOT_CNT; i++)
3977 {
3978 uint pw_pos = i / CHARSIZ;
3979
3980 cs_t *cs = &root_css_buf[pw_pos];
3981
3982 if (cs->cs_len == threshold) continue;
3983
3984 uint key = root_table_buf[i].key;
3985
3986 if (uniq_tbls[pw_pos][key] == 0) continue;
3987
3988 cs->cs_buf[cs->cs_len] = key;
3989
3990 cs->cs_len++;
3991 }
3992
3993 /**
3994 * Convert table to css
3995 */
3996
3997 for (uint i = 0; i < SP_MARKOV_CNT; i++)
3998 {
3999 uint c = i / CHARSIZ;
4000
4001 cs_t *cs = &markov_css_buf[c];
4002
4003 if (cs->cs_len == threshold) continue;
4004
4005 uint pw_pos = c / CHARSIZ;
4006
4007 uint key = markov_table_buf[i].key;
4008
4009 if ((pw_pos + 1) < SP_PW_MAX) if (uniq_tbls[pw_pos + 1][key] == 0) continue;
4010
4011 cs->cs_buf[cs->cs_len] = key;
4012
4013 cs->cs_len++;
4014 }
4015
4016 /*
4017 for (uint i = 0; i < 8; i++)
4018 {
4019 for (uint j = 0x20; j < 0x80; j++)
4020 {
4021 cs_t *ptr = &markov_css_buf[(i * CHARSIZ) + j];
4022
4023 printf ("pos:%u key:%u len:%u\n", i, j, ptr->cs_len);
4024
4025 for (uint k = 0; k < 10; k++)
4026 {
4027 printf (" %u\n", ptr->cs_buf[k]);
4028 }
4029 }
4030 }
4031 */
4032 }
4033
4034 void sp_stretch_root (hcstat_table_t *in, hcstat_table_t *out)
4035 {
4036 for (uint i = 0; i < SP_PW_MAX; i += 2)
4037 {
4038 memcpy (out, in, CHARSIZ * sizeof (hcstat_table_t));
4039
4040 out += CHARSIZ;
4041 in += CHARSIZ;
4042
4043 out->key = 0;
4044 out->val = 1;
4045
4046 out++;
4047
4048 for (uint j = 1; j < CHARSIZ; j++)
4049 {
4050 out->key = j;
4051 out->val = 0;
4052
4053 out++;
4054 }
4055 }
4056 }
4057
4058 void sp_stretch_markov (hcstat_table_t *in, hcstat_table_t *out)
4059 {
4060 for (uint i = 0; i < SP_PW_MAX; i += 2)
4061 {
4062 memcpy (out, in, CHARSIZ * CHARSIZ * sizeof (hcstat_table_t));
4063
4064 out += CHARSIZ * CHARSIZ;
4065 in += CHARSIZ * CHARSIZ;
4066
4067 for (uint j = 0; j < CHARSIZ; j++)
4068 {
4069 out->key = 0;
4070 out->val = 1;
4071
4072 out++;
4073
4074 for (uint k = 1; k < CHARSIZ; k++)
4075 {
4076 out->key = k;
4077 out->val = 0;
4078
4079 out++;
4080 }
4081 }
4082 }
4083 }
4084
4085 /**
4086 * mixed shared functions
4087 */
4088
4089 void dump_hex (const u8 *s, const int sz)
4090 {
4091 for (int i = 0; i < sz; i++)
4092 {
4093 log_info_nn ("%02x ", s[i]);
4094 }
4095
4096 log_info ("");
4097 }
4098
4099 void usage_mini_print (const char *progname)
4100 {
4101 for (uint i = 0; USAGE_MINI[i] != NULL; i++) log_info (USAGE_MINI[i], progname);
4102 }
4103
4104 void usage_big_print (const char *progname)
4105 {
4106 for (uint i = 0; USAGE_BIG[i] != NULL; i++) log_info (USAGE_BIG[i], progname);
4107 }
4108
4109 char *get_exec_path ()
4110 {
4111 int exec_path_len = 1024;
4112
4113 char *exec_path = (char *) mymalloc (exec_path_len);
4114
4115 #ifdef LINUX
4116
4117 char tmp[32] = { 0 };
4118
4119 snprintf (tmp, sizeof (tmp) - 1, "/proc/%d/exe", getpid ());
4120
4121 const int len = readlink (tmp, exec_path, exec_path_len - 1);
4122
4123 #elif WIN
4124
4125 const int len = GetModuleFileName (NULL, exec_path, exec_path_len - 1);
4126
4127 #elif OSX
4128
4129 uint size = exec_path_len;
4130
4131 if (_NSGetExecutablePath (exec_path, &size) != 0)
4132 {
4133 log_error("! executable path buffer too small\n");
4134
4135 exit (-1);
4136 }
4137
4138 const int len = strlen (exec_path);
4139
4140 #else
4141 #error Your Operating System is not supported or detected
4142 #endif
4143
4144 exec_path[len] = 0;
4145
4146 return exec_path;
4147 }
4148
4149 char *get_install_dir (const char *progname)
4150 {
4151 char *install_dir = mystrdup (progname);
4152 char *last_slash = NULL;
4153
4154 if ((last_slash = strrchr (install_dir, '/')) != NULL)
4155 {
4156 *last_slash = 0;
4157 }
4158 else if ((last_slash = strrchr (install_dir, '\\')) != NULL)
4159 {
4160 *last_slash = 0;
4161 }
4162 else
4163 {
4164 install_dir[0] = '.';
4165 install_dir[1] = 0;
4166 }
4167
4168 return (install_dir);
4169 }
4170
4171 char *get_profile_dir (const char *homedir)
4172 {
4173 #define DOT_HASHCAT ".hashcat"
4174
4175 size_t len = strlen (homedir) + 1 + strlen (DOT_HASHCAT) + 1;
4176
4177 char *profile_dir = (char *) mymalloc (len + 1);
4178
4179 snprintf (profile_dir, len, "%s/%s", homedir, DOT_HASHCAT);
4180
4181 return profile_dir;
4182 }
4183
4184 char *get_session_dir (const char *profile_dir)
4185 {
4186 #define SESSIONS_FOLDER "sessions"
4187
4188 size_t len = strlen (profile_dir) + 1 + strlen (SESSIONS_FOLDER) + 1;
4189
4190 char *session_dir = (char *) mymalloc (len + 1);
4191
4192 snprintf (session_dir, len, "%s/%s", profile_dir, SESSIONS_FOLDER);
4193
4194 return session_dir;
4195 }
4196
4197 uint count_lines (FILE *fd)
4198 {
4199 uint cnt = 0;
4200
4201 char *buf = (char *) mymalloc (HCBUFSIZ + 1);
4202
4203 char prev = '\n';
4204
4205 while (!feof (fd))
4206 {
4207 size_t nread = fread (buf, sizeof (char), HCBUFSIZ, fd);
4208
4209 if (nread < 1) continue;
4210
4211 size_t i;
4212
4213 for (i = 0; i < nread; i++)
4214 {
4215 if (prev == '\n') cnt++;
4216
4217 prev = buf[i];
4218 }
4219 }
4220
4221 myfree (buf);
4222
4223 return cnt;
4224 }
4225
4226 void truecrypt_crc32 (const char *filename, u8 keytab[64])
4227 {
4228 uint crc = ~0;
4229
4230 FILE *fd = fopen (filename, "rb");
4231
4232 if (fd == NULL)
4233 {
4234 log_error ("%s: %s", filename, strerror (errno));
4235
4236 exit (-1);
4237 }
4238
4239 #define MAX_KEY_SIZE (1024 * 1024)
4240
4241 u8 *buf = (u8 *) mymalloc (MAX_KEY_SIZE + 1);
4242
4243 int nread = fread (buf, sizeof (u8), MAX_KEY_SIZE, fd);
4244
4245 fclose (fd);
4246
4247 int kpos = 0;
4248
4249 for (int fpos = 0; fpos < nread; fpos++)
4250 {
4251 crc = crc32tab[(crc ^ buf[fpos]) & 0xff] ^ (crc >> 8);
4252
4253 keytab[kpos++] += (crc >> 24) & 0xff;
4254 keytab[kpos++] += (crc >> 16) & 0xff;
4255 keytab[kpos++] += (crc >> 8) & 0xff;
4256 keytab[kpos++] += (crc >> 0) & 0xff;
4257
4258 if (kpos >= 64) kpos = 0;
4259 }
4260
4261 myfree (buf);
4262 }
4263
4264 #ifdef OSX
4265 int pthread_setaffinity_np (pthread_t thread, size_t cpu_size, cpu_set_t *cpu_set)
4266 {
4267 int core;
4268
4269 for (core = 0; core < (8 * (int)cpu_size); core++)
4270 if (CPU_ISSET(core, cpu_set)) break;
4271
4272 thread_affinity_policy_data_t policy = { core };
4273
4274 const int rc = thread_policy_set (pthread_mach_thread_np (thread), THREAD_AFFINITY_POLICY, (thread_policy_t) &policy, 1);
4275
4276 if (data.quiet == 0)
4277 {
4278 if (rc != KERN_SUCCESS)
4279 {
4280 log_error ("ERROR: %s : %d", "thread_policy_set()", rc);
4281 }
4282 }
4283
4284 return rc;
4285 }
4286 #endif
4287
4288 void set_cpu_affinity (char *cpu_affinity)
4289 {
4290 #ifdef WIN
4291 DWORD_PTR aff_mask = 0;
4292 #elif _POSIX
4293 cpu_set_t cpuset;
4294 CPU_ZERO (&cpuset);
4295 #endif
4296
4297 if (cpu_affinity)
4298 {
4299 char *devices = strdup (cpu_affinity);
4300
4301 char *next = strtok (devices, ",");
4302
4303 do
4304 {
4305 uint cpu_id = atoi (next);
4306
4307 if (cpu_id == 0)
4308 {
4309 #ifdef WIN
4310 aff_mask = 0;
4311 #elif _POSIX
4312 CPU_ZERO (&cpuset);
4313 #endif
4314
4315 break;
4316 }
4317
4318 if (cpu_id > 32)
4319 {
4320 log_error ("ERROR: invalid cpu_id %u specified", cpu_id);
4321
4322 exit (-1);
4323 }
4324
4325 #ifdef WIN
4326 aff_mask |= 1 << (cpu_id - 1);
4327 #elif _POSIX
4328 CPU_SET ((cpu_id - 1), &cpuset);
4329 #endif
4330
4331 } while ((next = strtok (NULL, ",")) != NULL);
4332
4333 free (devices);
4334 }
4335
4336 #ifdef WIN
4337 SetProcessAffinityMask (GetCurrentProcess (), aff_mask);
4338 SetThreadAffinityMask (GetCurrentThread (), aff_mask);
4339 #elif _POSIX
4340 pthread_t thread = pthread_self ();
4341 pthread_setaffinity_np (thread, sizeof (cpu_set_t), &cpuset);
4342 #endif
4343 }
4344
4345 void *rulefind (const void *key, void *base, int nmemb, size_t size, int (*compar) (const void *, const void *))
4346 {
4347 char *element, *end;
4348
4349 end = (char *) base + nmemb * size;
4350
4351 for (element = (char *) base; element < end; element += size)
4352 if (!compar (element, key))
4353 return element;
4354
4355 return NULL;
4356 }
4357
4358 int sort_by_u32 (const void *v1, const void *v2)
4359 {
4360 const u32 *s1 = (const u32 *) v1;
4361 const u32 *s2 = (const u32 *) v2;
4362
4363 return *s1 - *s2;
4364 }
4365
4366 int sort_by_salt (const void *v1, const void *v2)
4367 {
4368 const salt_t *s1 = (const salt_t *) v1;
4369 const salt_t *s2 = (const salt_t *) v2;
4370
4371 const int res1 = s1->salt_len - s2->salt_len;
4372
4373 if (res1 != 0) return (res1);
4374
4375 const int res2 = s1->salt_iter - s2->salt_iter;
4376
4377 if (res2 != 0) return (res2);
4378
4379 uint n;
4380
4381 n = 16;
4382
4383 while (n--)
4384 {
4385 if (s1->salt_buf[n] > s2->salt_buf[n]) return ( 1);
4386 if (s1->salt_buf[n] < s2->salt_buf[n]) return (-1);
4387 }
4388
4389 n = 8;
4390
4391 while (n--)
4392 {
4393 if (s1->salt_buf_pc[n] > s2->salt_buf_pc[n]) return ( 1);
4394 if (s1->salt_buf_pc[n] < s2->salt_buf_pc[n]) return (-1);
4395 }
4396
4397 return (0);
4398 }
4399
4400 int sort_by_salt_buf (const void *v1, const void *v2)
4401 {
4402 const pot_t *p1 = (const pot_t *) v1;
4403 const pot_t *p2 = (const pot_t *) v2;
4404
4405 const hash_t *h1 = &p1->hash;
4406 const hash_t *h2 = &p2->hash;
4407
4408 const salt_t *s1 = h1->salt;
4409 const salt_t *s2 = h2->salt;
4410
4411 uint n = 16;
4412
4413 while (n--)
4414 {
4415 if (s1->salt_buf[n] > s2->salt_buf[n]) return ( 1);
4416 if (s1->salt_buf[n] < s2->salt_buf[n]) return (-1);
4417 }
4418
4419 return 0;
4420 }
4421
4422 int sort_by_hash_t_salt (const void *v1, const void *v2)
4423 {
4424 const hash_t *h1 = (const hash_t *) v1;
4425 const hash_t *h2 = (const hash_t *) v2;
4426
4427 const salt_t *s1 = h1->salt;
4428 const salt_t *s2 = h2->salt;
4429
4430 // testphase: this should work
4431 uint n = 16;
4432
4433 while (n--)
4434 {
4435 if (s1->salt_buf[n] > s2->salt_buf[n]) return ( 1);
4436 if (s1->salt_buf[n] < s2->salt_buf[n]) return (-1);
4437 }
4438
4439 /* original code, seems buggy since salt_len can be very big (had a case with 131 len)
4440 also it thinks salt_buf[x] is a char but its a uint so salt_len should be / 4
4441 if (s1->salt_len > s2->salt_len) return ( 1);
4442 if (s1->salt_len < s2->salt_len) return (-1);
4443
4444 uint n = s1->salt_len;
4445
4446 while (n--)
4447 {
4448 if (s1->salt_buf[n] > s2->salt_buf[n]) return ( 1);
4449 if (s1->salt_buf[n] < s2->salt_buf[n]) return (-1);
4450 }
4451 */
4452
4453 return 0;
4454 }
4455
4456 int sort_by_hash_t_salt_hccap (const void *v1, const void *v2)
4457 {
4458 const hash_t *h1 = (const hash_t *) v1;
4459 const hash_t *h2 = (const hash_t *) v2;
4460
4461 const salt_t *s1 = h1->salt;
4462 const salt_t *s2 = h2->salt;
4463
4464 // 16 - 2 (since last 2 uints contain the digest)
4465 uint n = 14;
4466
4467 while (n--)
4468 {
4469 if (s1->salt_buf[n] > s2->salt_buf[n]) return ( 1);
4470 if (s1->salt_buf[n] < s2->salt_buf[n]) return (-1);
4471 }
4472
4473 return 0;
4474 }
4475
4476 int sort_by_hash_no_salt (const void *v1, const void *v2)
4477 {
4478 const hash_t *h1 = (const hash_t *) v1;
4479 const hash_t *h2 = (const hash_t *) v2;
4480
4481 const void *d1 = h1->digest;
4482 const void *d2 = h2->digest;
4483
4484 return data.sort_by_digest (d1, d2);
4485 }
4486
4487 int sort_by_hash (const void *v1, const void *v2)
4488 {
4489 const hash_t *h1 = (const hash_t *) v1;
4490 const hash_t *h2 = (const hash_t *) v2;
4491
4492 if (data.isSalted)
4493 {
4494 const salt_t *s1 = h1->salt;
4495 const salt_t *s2 = h2->salt;
4496
4497 int res = sort_by_salt (s1, s2);
4498
4499 if (res != 0) return (res);
4500 }
4501
4502 const void *d1 = h1->digest;
4503 const void *d2 = h2->digest;
4504
4505 return data.sort_by_digest (d1, d2);
4506 }
4507
4508 int sort_by_pot (const void *v1, const void *v2)
4509 {
4510 const pot_t *p1 = (const pot_t *) v1;
4511 const pot_t *p2 = (const pot_t *) v2;
4512
4513 const hash_t *h1 = &p1->hash;
4514 const hash_t *h2 = &p2->hash;
4515
4516 return sort_by_hash (h1, h2);
4517 }
4518
4519 int sort_by_mtime (const void *p1, const void *p2)
4520 {
4521 const char **f1 = (const char **) p1;
4522 const char **f2 = (const char **) p2;
4523
4524 struct stat s1; stat (*f1, &s1);
4525 struct stat s2; stat (*f2, &s2);
4526
4527 return s2.st_mtime - s1.st_mtime;
4528 }
4529
4530 int sort_by_cpu_rule (const void *p1, const void *p2)
4531 {
4532 const cpu_rule_t *r1 = (const cpu_rule_t *) p1;
4533 const cpu_rule_t *r2 = (const cpu_rule_t *) p2;
4534
4535 return memcmp (r1, r2, sizeof (cpu_rule_t));
4536 }
4537
4538 int sort_by_kernel_rule (const void *p1, const void *p2)
4539 {
4540 const kernel_rule_t *r1 = (const kernel_rule_t *) p1;
4541 const kernel_rule_t *r2 = (const kernel_rule_t *) p2;
4542
4543 return memcmp (r1, r2, sizeof (kernel_rule_t));
4544 }
4545
4546 int sort_by_stringptr (const void *p1, const void *p2)
4547 {
4548 const char **s1 = (const char **) p1;
4549 const char **s2 = (const char **) p2;
4550
4551 return strcmp (*s1, *s2);
4552 }
4553
4554 int sort_by_dictstat (const void *s1, const void *s2)
4555 {
4556 dictstat_t *d1 = (dictstat_t *) s1;
4557 dictstat_t *d2 = (dictstat_t *) s2;
4558
4559 #ifdef LINUX
4560 d2->stat.st_atim = d1->stat.st_atim;
4561 #else
4562 d2->stat.st_atime = d1->stat.st_atime;
4563 #endif
4564
4565 return memcmp (&d1->stat, &d2->stat, sizeof (struct stat));
4566 }
4567
4568 int sort_by_bitmap (const void *p1, const void *p2)
4569 {
4570 const bitmap_result_t *b1 = (const bitmap_result_t *) p1;
4571 const bitmap_result_t *b2 = (const bitmap_result_t *) p2;
4572
4573 return b1->collisions - b2->collisions;
4574 }
4575
4576 int sort_by_digest_4_2 (const void *v1, const void *v2)
4577 {
4578 const u32 *d1 = (const u32 *) v1;
4579 const u32 *d2 = (const u32 *) v2;
4580
4581 uint n = 2;
4582
4583 while (n--)
4584 {
4585 if (d1[n] > d2[n]) return ( 1);
4586 if (d1[n] < d2[n]) return (-1);
4587 }
4588
4589 return (0);
4590 }
4591
4592 int sort_by_digest_4_4 (const void *v1, const void *v2)
4593 {
4594 const u32 *d1 = (const u32 *) v1;
4595 const u32 *d2 = (const u32 *) v2;
4596
4597 uint n = 4;
4598
4599 while (n--)
4600 {
4601 if (d1[n] > d2[n]) return ( 1);
4602 if (d1[n] < d2[n]) return (-1);
4603 }
4604
4605 return (0);
4606 }
4607
4608 int sort_by_digest_4_5 (const void *v1, const void *v2)
4609 {
4610 const u32 *d1 = (const u32 *) v1;
4611 const u32 *d2 = (const u32 *) v2;
4612
4613 uint n = 5;
4614
4615 while (n--)
4616 {
4617 if (d1[n] > d2[n]) return ( 1);
4618 if (d1[n] < d2[n]) return (-1);
4619 }
4620
4621 return (0);
4622 }
4623
4624 int sort_by_digest_4_6 (const void *v1, const void *v2)
4625 {
4626 const u32 *d1 = (const u32 *) v1;
4627 const u32 *d2 = (const u32 *) v2;
4628
4629 uint n = 6;
4630
4631 while (n--)
4632 {
4633 if (d1[n] > d2[n]) return ( 1);
4634 if (d1[n] < d2[n]) return (-1);
4635 }
4636
4637 return (0);
4638 }
4639
4640 int sort_by_digest_4_8 (const void *v1, const void *v2)
4641 {
4642 const u32 *d1 = (const u32 *) v1;
4643 const u32 *d2 = (const u32 *) v2;
4644
4645 uint n = 8;
4646
4647 while (n--)
4648 {
4649 if (d1[n] > d2[n]) return ( 1);
4650 if (d1[n] < d2[n]) return (-1);
4651 }
4652
4653 return (0);
4654 }
4655
4656 int sort_by_digest_4_16 (const void *v1, const void *v2)
4657 {
4658 const u32 *d1 = (const u32 *) v1;
4659 const u32 *d2 = (const u32 *) v2;
4660
4661 uint n = 16;
4662
4663 while (n--)
4664 {
4665 if (d1[n] > d2[n]) return ( 1);
4666 if (d1[n] < d2[n]) return (-1);
4667 }
4668
4669 return (0);
4670 }
4671
4672 int sort_by_digest_4_32 (const void *v1, const void *v2)
4673 {
4674 const u32 *d1 = (const u32 *) v1;
4675 const u32 *d2 = (const u32 *) v2;
4676
4677 uint n = 32;
4678
4679 while (n--)
4680 {
4681 if (d1[n] > d2[n]) return ( 1);
4682 if (d1[n] < d2[n]) return (-1);
4683 }
4684
4685 return (0);
4686 }
4687
4688 int sort_by_digest_4_64 (const void *v1, const void *v2)
4689 {
4690 const u32 *d1 = (const u32 *) v1;
4691 const u32 *d2 = (const u32 *) v2;
4692
4693 uint n = 64;
4694
4695 while (n--)
4696 {
4697 if (d1[n] > d2[n]) return ( 1);
4698 if (d1[n] < d2[n]) return (-1);
4699 }
4700
4701 return (0);
4702 }
4703
4704 int sort_by_digest_8_8 (const void *v1, const void *v2)
4705 {
4706 const u64 *d1 = (const u64 *) v1;
4707 const u64 *d2 = (const u64 *) v2;
4708
4709 uint n = 8;
4710
4711 while (n--)
4712 {
4713 if (d1[n] > d2[n]) return ( 1);
4714 if (d1[n] < d2[n]) return (-1);
4715 }
4716
4717 return (0);
4718 }
4719
4720 int sort_by_digest_8_16 (const void *v1, const void *v2)
4721 {
4722 const u64 *d1 = (const u64 *) v1;
4723 const u64 *d2 = (const u64 *) v2;
4724
4725 uint n = 16;
4726
4727 while (n--)
4728 {
4729 if (d1[n] > d2[n]) return ( 1);
4730 if (d1[n] < d2[n]) return (-1);
4731 }
4732
4733 return (0);
4734 }
4735
4736 int sort_by_digest_8_25 (const void *v1, const void *v2)
4737 {
4738 const u64 *d1 = (const u64 *) v1;
4739 const u64 *d2 = (const u64 *) v2;
4740
4741 uint n = 25;
4742
4743 while (n--)
4744 {
4745 if (d1[n] > d2[n]) return ( 1);
4746 if (d1[n] < d2[n]) return (-1);
4747 }
4748
4749 return (0);
4750 }
4751
4752 int sort_by_digest_p0p1 (const void *v1, const void *v2)
4753 {
4754 const u32 *d1 = (const u32 *) v1;
4755 const u32 *d2 = (const u32 *) v2;
4756
4757 const uint dgst_pos0 = data.dgst_pos0;
4758 const uint dgst_pos1 = data.dgst_pos1;
4759 const uint dgst_pos2 = data.dgst_pos2;
4760 const uint dgst_pos3 = data.dgst_pos3;
4761
4762 if (d1[dgst_pos3] > d2[dgst_pos3]) return ( 1);
4763 if (d1[dgst_pos3] < d2[dgst_pos3]) return (-1);
4764 if (d1[dgst_pos2] > d2[dgst_pos2]) return ( 1);
4765 if (d1[dgst_pos2] < d2[dgst_pos2]) return (-1);
4766 if (d1[dgst_pos1] > d2[dgst_pos1]) return ( 1);
4767 if (d1[dgst_pos1] < d2[dgst_pos1]) return (-1);
4768 if (d1[dgst_pos0] > d2[dgst_pos0]) return ( 1);
4769 if (d1[dgst_pos0] < d2[dgst_pos0]) return (-1);
4770
4771 return (0);
4772 }
4773
4774 int sort_by_tuning_db_alias (const void *v1, const void *v2)
4775 {
4776 const tuning_db_alias_t *t1 = (const tuning_db_alias_t *) v1;
4777 const tuning_db_alias_t *t2 = (const tuning_db_alias_t *) v2;
4778
4779 const int res1 = strcmp (t1->device_name, t2->device_name);
4780
4781 if (res1 != 0) return (res1);
4782
4783 return 0;
4784 }
4785
4786 int sort_by_tuning_db_entry (const void *v1, const void *v2)
4787 {
4788 const tuning_db_entry_t *t1 = (const tuning_db_entry_t *) v1;
4789 const tuning_db_entry_t *t2 = (const tuning_db_entry_t *) v2;
4790
4791 const int res1 = strcmp (t1->device_name, t2->device_name);
4792
4793 if (res1 != 0) return (res1);
4794
4795 const int res2 = t1->attack_mode
4796 - t2->attack_mode;
4797
4798 if (res2 != 0) return (res2);
4799
4800 const int res3 = t1->hash_type
4801 - t2->hash_type;
4802
4803 if (res3 != 0) return (res3);
4804
4805 return 0;
4806 }
4807
4808 void format_debug (char *debug_file, uint debug_mode, unsigned char *orig_plain_ptr, uint orig_plain_len, unsigned char *mod_plain_ptr, uint mod_plain_len, char *rule_buf, int rule_len)
4809 {
4810 uint outfile_autohex = data.outfile_autohex;
4811
4812 unsigned char *rule_ptr = (unsigned char *) rule_buf;
4813
4814 FILE *debug_fp = NULL;
4815
4816 if (debug_file != NULL)
4817 {
4818 debug_fp = fopen (debug_file, "ab");
4819
4820 lock_file (debug_fp);
4821 }
4822 else
4823 {
4824 debug_fp = stderr;
4825 }
4826
4827 if (debug_fp == NULL)
4828 {
4829 log_info ("WARNING: Could not open debug-file for writing");
4830 }
4831 else
4832 {
4833 if ((debug_mode == 2) || (debug_mode == 3) || (debug_mode == 4))
4834 {
4835 format_plain (debug_fp, orig_plain_ptr, orig_plain_len, outfile_autohex);
4836
4837 if ((debug_mode == 3) || (debug_mode == 4)) fputc (':', debug_fp);
4838 }
4839
4840 fwrite (rule_ptr, rule_len, 1, debug_fp);
4841
4842 if (debug_mode == 4)
4843 {
4844 fputc (':', debug_fp);
4845
4846 format_plain (debug_fp, mod_plain_ptr, mod_plain_len, outfile_autohex);
4847 }
4848
4849 fputc ('\n', debug_fp);
4850
4851 if (debug_file != NULL) fclose (debug_fp);
4852 }
4853 }
4854
4855 void format_plain (FILE *fp, unsigned char *plain_ptr, uint plain_len, uint outfile_autohex)
4856 {
4857 int needs_hexify = 0;
4858
4859 if (outfile_autohex == 1)
4860 {
4861 for (uint i = 0; i < plain_len; i++)
4862 {
4863 if (plain_ptr[i] < 0x20)
4864 {
4865 needs_hexify = 1;
4866
4867 break;
4868 }
4869
4870 if (plain_ptr[i] > 0x7f)
4871 {
4872 needs_hexify = 1;
4873
4874 break;
4875 }
4876 }
4877 }
4878
4879 if (needs_hexify == 1)
4880 {
4881 fprintf (fp, "$HEX[");
4882
4883 for (uint i = 0; i < plain_len; i++)
4884 {
4885 fprintf (fp, "%02x", plain_ptr[i]);
4886 }
4887
4888 fprintf (fp, "]");
4889 }
4890 else
4891 {
4892 fwrite (plain_ptr, plain_len, 1, fp);
4893 }
4894 }
4895
4896 void format_output (FILE *out_fp, char *out_buf, unsigned char *plain_ptr, const uint plain_len, const u64 crackpos, unsigned char *username, const uint user_len)
4897 {
4898 uint outfile_format = data.outfile_format;
4899
4900 char separator = data.separator;
4901
4902 if (outfile_format & OUTFILE_FMT_HASH)
4903 {
4904 fprintf (out_fp, "%s", out_buf);
4905
4906 if (outfile_format & (OUTFILE_FMT_PLAIN | OUTFILE_FMT_HEXPLAIN | OUTFILE_FMT_CRACKPOS))
4907 {
4908 fputc (separator, out_fp);
4909 }
4910 }
4911 else if (data.username)
4912 {
4913 if (username != NULL)
4914 {
4915 for (uint i = 0; i < user_len; i++)
4916 {
4917 fprintf (out_fp, "%c", username[i]);
4918 }
4919
4920 if (outfile_format & (OUTFILE_FMT_PLAIN | OUTFILE_FMT_HEXPLAIN | OUTFILE_FMT_CRACKPOS))
4921 {
4922 fputc (separator, out_fp);
4923 }
4924 }
4925 }
4926
4927 if (outfile_format & OUTFILE_FMT_PLAIN)
4928 {
4929 format_plain (out_fp, plain_ptr, plain_len, data.outfile_autohex);
4930
4931 if (outfile_format & (OUTFILE_FMT_HEXPLAIN | OUTFILE_FMT_CRACKPOS))
4932 {
4933 fputc (separator, out_fp);
4934 }
4935 }
4936
4937 if (outfile_format & OUTFILE_FMT_HEXPLAIN)
4938 {
4939 for (uint i = 0; i < plain_len; i++)
4940 {
4941 fprintf (out_fp, "%02x", plain_ptr[i]);
4942 }
4943
4944 if (outfile_format & (OUTFILE_FMT_CRACKPOS))
4945 {
4946 fputc (separator, out_fp);
4947 }
4948 }
4949
4950 if (outfile_format & OUTFILE_FMT_CRACKPOS)
4951 {
4952 #ifdef _WIN
4953 __mingw_fprintf (out_fp, "%llu", crackpos);
4954 #endif
4955
4956 #ifdef _POSIX
4957 #ifdef __x86_64__
4958 fprintf (out_fp, "%lu", (unsigned long) crackpos);
4959 #else
4960 fprintf (out_fp, "%llu", crackpos);
4961 #endif
4962 #endif
4963 }
4964
4965 fputc ('\n', out_fp);
4966 }
4967
4968 void handle_show_request (pot_t *pot, uint pot_cnt, char *input_buf, int input_len, hash_t *hashes_buf, int (*sort_by_pot) (const void *, const void *), FILE *out_fp)
4969 {
4970 pot_t pot_key;
4971
4972 pot_key.hash.salt = hashes_buf->salt;
4973 pot_key.hash.digest = hashes_buf->digest;
4974
4975 pot_t *pot_ptr = (pot_t *) bsearch (&pot_key, pot, pot_cnt, sizeof (pot_t), sort_by_pot);
4976
4977 if (pot_ptr)
4978 {
4979 log_info_nn ("");
4980
4981 input_buf[input_len] = 0;
4982
4983 // user
4984 unsigned char *username = NULL;
4985 uint user_len = 0;
4986
4987 if (data.username)
4988 {
4989 user_t *user = hashes_buf->hash_info->user;
4990
4991 if (user)
4992 {
4993 username = (unsigned char *) (user->user_name);
4994
4995 user_len = user->user_len;
4996 }
4997 }
4998
4999 // do output the line
5000 format_output (out_fp, input_buf, (unsigned char *) pot_ptr->plain_buf, pot_ptr->plain_len, 0, username, user_len);
5001 }
5002 }
5003
5004 #define LM_WEAK_HASH "\x4e\xcf\x0d\x0c\x0a\xe2\xfb\xc1"
5005 #define LM_MASKED_PLAIN "[notfound]"
5006
5007 void handle_show_request_lm (pot_t *pot, uint pot_cnt, char *input_buf, int input_len, hash_t *hash_left, hash_t *hash_right, int (*sort_by_pot) (const void *, const void *), FILE *out_fp)
5008 {
5009 // left
5010
5011 pot_t pot_left_key;
5012
5013 pot_left_key.hash.salt = hash_left->salt;
5014 pot_left_key.hash.digest = hash_left->digest;
5015
5016 pot_t *pot_left_ptr = (pot_t *) bsearch (&pot_left_key, pot, pot_cnt, sizeof (pot_t), sort_by_pot);
5017
5018 // right
5019
5020 uint weak_hash_found = 0;
5021
5022 pot_t pot_right_key;
5023
5024 pot_right_key.hash.salt = hash_right->salt;
5025 pot_right_key.hash.digest = hash_right->digest;
5026
5027 pot_t *pot_right_ptr = (pot_t *) bsearch (&pot_right_key, pot, pot_cnt, sizeof (pot_t), sort_by_pot);
5028
5029 if (pot_right_ptr == NULL)
5030 {
5031 // special case, if "weak hash"
5032
5033 if (memcmp (hash_right->digest, LM_WEAK_HASH, 8) == 0)
5034 {
5035 weak_hash_found = 1;
5036
5037 pot_right_ptr = (pot_t *) mycalloc (1, sizeof (pot_t));
5038
5039 // in theory this is not needed, but we are paranoia:
5040
5041 memset (pot_right_ptr->plain_buf, 0, sizeof (pot_right_ptr->plain_buf));
5042 pot_right_ptr->plain_len = 0;
5043 }
5044 }
5045
5046 if ((pot_left_ptr == NULL) && (pot_right_ptr == NULL))
5047 {
5048 if (weak_hash_found == 1) myfree (pot_right_ptr); // this shouldn't happen at all: if weak_hash_found == 1, than pot_right_ptr is not NULL for sure
5049
5050 return;
5051 }
5052
5053 // at least one half was found:
5054
5055 log_info_nn ("");
5056
5057 input_buf[input_len] = 0;
5058
5059 // user
5060
5061 unsigned char *username = NULL;
5062 uint user_len = 0;
5063
5064 if (data.username)
5065 {
5066 user_t *user = hash_left->hash_info->user;
5067
5068 if (user)
5069 {
5070 username = (unsigned char *) (user->user_name);
5071
5072 user_len = user->user_len;
5073 }
5074 }
5075
5076 // mask the part which was not found
5077
5078 uint left_part_masked = 0;
5079 uint right_part_masked = 0;
5080
5081 uint mask_plain_len = strlen (LM_MASKED_PLAIN);
5082
5083 if (pot_left_ptr == NULL)
5084 {
5085 left_part_masked = 1;
5086
5087 pot_left_ptr = (pot_t *) mycalloc (1, sizeof (pot_t));
5088
5089 memset (pot_left_ptr->plain_buf, 0, sizeof (pot_left_ptr->plain_buf));
5090
5091 memcpy (pot_left_ptr->plain_buf, LM_MASKED_PLAIN, mask_plain_len);
5092 pot_left_ptr->plain_len = mask_plain_len;
5093 }
5094
5095 if (pot_right_ptr == NULL)
5096 {
5097 right_part_masked = 1;
5098
5099 pot_right_ptr = (pot_t *) mycalloc (1, sizeof (pot_t));
5100
5101 memset (pot_right_ptr->plain_buf, 0, sizeof (pot_right_ptr->plain_buf));
5102
5103 memcpy (pot_right_ptr->plain_buf, LM_MASKED_PLAIN, mask_plain_len);
5104 pot_right_ptr->plain_len = mask_plain_len;
5105 }
5106
5107 // create the pot_ptr out of pot_left_ptr and pot_right_ptr
5108
5109 pot_t pot_ptr;
5110
5111 pot_ptr.plain_len = pot_left_ptr->plain_len + pot_right_ptr->plain_len;
5112
5113 memcpy (pot_ptr.plain_buf, pot_left_ptr->plain_buf, pot_left_ptr->plain_len);
5114
5115 memcpy (pot_ptr.plain_buf + pot_left_ptr->plain_len, pot_right_ptr->plain_buf, pot_right_ptr->plain_len);
5116
5117 // do output the line
5118
5119 format_output (out_fp, input_buf, (unsigned char *) pot_ptr.plain_buf, pot_ptr.plain_len, 0, username, user_len);
5120
5121 if (weak_hash_found == 1) myfree (pot_right_ptr);
5122
5123 if (left_part_masked == 1) myfree (pot_left_ptr);
5124 if (right_part_masked == 1) myfree (pot_right_ptr);
5125 }
5126
5127 void handle_left_request (pot_t *pot, uint pot_cnt, char *input_buf, int input_len, hash_t *hashes_buf, int (*sort_by_pot) (const void *, const void *), FILE *out_fp)
5128 {
5129 pot_t pot_key;
5130
5131 memcpy (&pot_key.hash, hashes_buf, sizeof (hash_t));
5132
5133 pot_t *pot_ptr = (pot_t *) bsearch (&pot_key, pot, pot_cnt, sizeof (pot_t), sort_by_pot);
5134
5135 if (pot_ptr == NULL)
5136 {
5137 log_info_nn ("");
5138
5139 input_buf[input_len] = 0;
5140
5141 format_output (out_fp, input_buf, NULL, 0, 0, NULL, 0);
5142 }
5143 }
5144
5145 void handle_left_request_lm (pot_t *pot, uint pot_cnt, char *input_buf, int input_len, hash_t *hash_left, hash_t *hash_right, int (*sort_by_pot) (const void *, const void *), FILE *out_fp)
5146 {
5147 // left
5148
5149 pot_t pot_left_key;
5150
5151 memcpy (&pot_left_key.hash, hash_left, sizeof (hash_t));
5152
5153 pot_t *pot_left_ptr = (pot_t *) bsearch (&pot_left_key, pot, pot_cnt, sizeof (pot_t), sort_by_pot);
5154
5155 // right
5156
5157 pot_t pot_right_key;
5158
5159 memcpy (&pot_right_key.hash, hash_right, sizeof (hash_t));
5160
5161 pot_t *pot_right_ptr = (pot_t *) bsearch (&pot_right_key, pot, pot_cnt, sizeof (pot_t), sort_by_pot);
5162
5163 uint weak_hash_found = 0;
5164
5165 if (pot_right_ptr == NULL)
5166 {
5167 // special case, if "weak hash"
5168
5169 if (memcmp (hash_right->digest, LM_WEAK_HASH, 8) == 0)
5170 {
5171 weak_hash_found = 1;
5172
5173 // we just need that pot_right_ptr is not a NULL pointer
5174
5175 pot_right_ptr = (pot_t *) mycalloc (1, sizeof (pot_t));
5176 }
5177 }
5178
5179 if ((pot_left_ptr != NULL) && (pot_right_ptr != NULL))
5180 {
5181 if (weak_hash_found == 1) myfree (pot_right_ptr);
5182
5183 return;
5184 }
5185
5186 // ... at least one part was not cracked
5187
5188 log_info_nn ("");
5189
5190 input_buf[input_len] = 0;
5191
5192 // only show the hash part which is still not cracked
5193
5194 uint user_len = input_len - 32;
5195
5196 char *hash_output = (char *) mymalloc (33);
5197
5198 memcpy (hash_output, input_buf, input_len);
5199
5200 if (pot_left_ptr != NULL)
5201 {
5202 // only show right part (because left part was already found)
5203
5204 memcpy (hash_output + user_len, input_buf + user_len + 16, 16);
5205
5206 hash_output[user_len + 16] = 0;
5207 }
5208
5209 if (pot_right_ptr != NULL)
5210 {
5211 // only show left part (because right part was already found)
5212
5213 memcpy (hash_output + user_len, input_buf + user_len, 16);
5214
5215 hash_output[user_len + 16] = 0;
5216 }
5217
5218 format_output (out_fp, hash_output, NULL, 0, 0, NULL, 0);
5219
5220 myfree (hash_output);
5221
5222 if (weak_hash_found == 1) myfree (pot_right_ptr);
5223 }
5224
5225 uint setup_opencl_platforms_filter (char *opencl_platforms)
5226 {
5227 uint opencl_platforms_filter = 0;
5228
5229 if (opencl_platforms)
5230 {
5231 char *platforms = strdup (opencl_platforms);
5232
5233 char *next = strtok (platforms, ",");
5234
5235 do
5236 {
5237 int platform = atoi (next);
5238
5239 if (platform < 1 || platform > 32)
5240 {
5241 log_error ("ERROR: invalid OpenCL platform %u specified", platform);
5242
5243 exit (-1);
5244 }
5245
5246 opencl_platforms_filter |= 1 << (platform - 1);
5247
5248 } while ((next = strtok (NULL, ",")) != NULL);
5249
5250 free (platforms);
5251 }
5252 else
5253 {
5254 opencl_platforms_filter = -1;
5255 }
5256
5257 return opencl_platforms_filter;
5258 }
5259
5260 u32 setup_devices_filter (char *opencl_devices)
5261 {
5262 u32 devices_filter = 0;
5263
5264 if (opencl_devices)
5265 {
5266 char *devices = strdup (opencl_devices);
5267
5268 char *next = strtok (devices, ",");
5269
5270 do
5271 {
5272 int device_id = atoi (next);
5273
5274 if (device_id < 1 || device_id > 32)
5275 {
5276 log_error ("ERROR: invalid device_id %u specified", device_id);
5277
5278 exit (-1);
5279 }
5280
5281 devices_filter |= 1 << (device_id - 1);
5282
5283 } while ((next = strtok (NULL, ",")) != NULL);
5284
5285 free (devices);
5286 }
5287 else
5288 {
5289 devices_filter = -1;
5290 }
5291
5292 return devices_filter;
5293 }
5294
5295 cl_device_type setup_device_types_filter (char *opencl_device_types)
5296 {
5297 cl_device_type device_types_filter = 0;
5298
5299 if (opencl_device_types)
5300 {
5301 char *device_types = strdup (opencl_device_types);
5302
5303 char *next = strtok (device_types, ",");
5304
5305 do
5306 {
5307 int device_type = atoi (next);
5308
5309 if (device_type < 1 || device_type > 3)
5310 {
5311 log_error ("ERROR: invalid device_type %u specified", device_type);
5312
5313 exit (-1);
5314 }
5315
5316 device_types_filter |= 1 << device_type;
5317
5318 } while ((next = strtok (NULL, ",")) != NULL);
5319
5320 free (device_types);
5321 }
5322 else
5323 {
5324 // Do not use CPU by default, this often reduces GPU performance because
5325 // the CPU is too busy to handle GPU synchronization
5326
5327 device_types_filter = CL_DEVICE_TYPE_ALL & ~CL_DEVICE_TYPE_CPU;
5328 }
5329
5330 return device_types_filter;
5331 }
5332
5333 u32 get_random_num (const u32 min, const u32 max)
5334 {
5335 if (min == max) return (min);
5336
5337 return ((rand () % (max - min)) + min);
5338 }
5339
5340 u32 mydivc32 (const u32 dividend, const u32 divisor)
5341 {
5342 u32 quotient = dividend / divisor;
5343
5344 if (dividend % divisor) quotient++;
5345
5346 return quotient;
5347 }
5348
5349 u64 mydivc64 (const u64 dividend, const u64 divisor)
5350 {
5351 u64 quotient = dividend / divisor;
5352
5353 if (dividend % divisor) quotient++;
5354
5355 return quotient;
5356 }
5357
5358 void format_timer_display (struct tm *tm, char *buf, size_t len)
5359 {
5360 const char *time_entities_s[] = { "year", "day", "hour", "min", "sec" };
5361 const char *time_entities_m[] = { "years", "days", "hours", "mins", "secs" };
5362
5363 if (tm->tm_year - 70)
5364 {
5365 char *time_entity1 = ((tm->tm_year - 70) == 1) ? (char *) time_entities_s[0] : (char *) time_entities_m[0];
5366 char *time_entity2 = ( tm->tm_yday == 1) ? (char *) time_entities_s[1] : (char *) time_entities_m[1];
5367
5368 snprintf (buf, len - 1, "%d %s, %d %s", tm->tm_year - 70, time_entity1, tm->tm_yday, time_entity2);
5369 }
5370 else if (tm->tm_yday)
5371 {
5372 char *time_entity1 = (tm->tm_yday == 1) ? (char *) time_entities_s[1] : (char *) time_entities_m[1];
5373 char *time_entity2 = (tm->tm_hour == 1) ? (char *) time_entities_s[2] : (char *) time_entities_m[2];
5374
5375 snprintf (buf, len - 1, "%d %s, %d %s", tm->tm_yday, time_entity1, tm->tm_hour, time_entity2);
5376 }
5377 else if (tm->tm_hour)
5378 {
5379 char *time_entity1 = (tm->tm_hour == 1) ? (char *) time_entities_s[2] : (char *) time_entities_m[2];
5380 char *time_entity2 = (tm->tm_min == 1) ? (char *) time_entities_s[3] : (char *) time_entities_m[3];
5381
5382 snprintf (buf, len - 1, "%d %s, %d %s", tm->tm_hour, time_entity1, tm->tm_min, time_entity2);
5383 }
5384 else if (tm->tm_min)
5385 {
5386 char *time_entity1 = (tm->tm_min == 1) ? (char *) time_entities_s[3] : (char *) time_entities_m[3];
5387 char *time_entity2 = (tm->tm_sec == 1) ? (char *) time_entities_s[4] : (char *) time_entities_m[4];
5388
5389 snprintf (buf, len - 1, "%d %s, %d %s", tm->tm_min, time_entity1, tm->tm_sec, time_entity2);
5390 }
5391 else
5392 {
5393 char *time_entity1 = (tm->tm_sec == 1) ? (char *) time_entities_s[4] : (char *) time_entities_m[4];
5394
5395 snprintf (buf, len - 1, "%d %s", tm->tm_sec, time_entity1);
5396 }
5397 }
5398
5399 void format_speed_display (float val, char *buf, size_t len)
5400 {
5401 if (val <= 0)
5402 {
5403 buf[0] = '0';
5404 buf[1] = ' ';
5405 buf[2] = 0;
5406
5407 return;
5408 }
5409
5410 char units[7] = { ' ', 'k', 'M', 'G', 'T', 'P', 'E' };
5411
5412 uint level = 0;
5413
5414 while (val > 99999)
5415 {
5416 val /= 1000;
5417
5418 level++;
5419 }
5420
5421 /* generate output */
5422
5423 if (level == 0)
5424 {
5425 snprintf (buf, len - 1, "%.0f ", val);
5426 }
5427 else
5428 {
5429 snprintf (buf, len - 1, "%.1f %c", val, units[level]);
5430 }
5431 }
5432
5433 void lowercase (u8 *buf, int len)
5434 {
5435 for (int i = 0; i < len; i++) buf[i] = tolower (buf[i]);
5436 }
5437
5438 void uppercase (u8 *buf, int len)
5439 {
5440 for (int i = 0; i < len; i++) buf[i] = toupper (buf[i]);
5441 }
5442
5443 int fgetl (FILE *fp, char *line_buf)
5444 {
5445 int line_len = 0;
5446
5447 while (!feof (fp))
5448 {
5449 const int c = fgetc (fp);
5450
5451 if (c == EOF) break;
5452
5453 line_buf[line_len] = (char) c;
5454
5455 line_len++;
5456
5457 if (line_len == HCBUFSIZ) line_len--;
5458
5459 if (c == '\n') break;
5460 }
5461
5462 if (line_len == 0) return 0;
5463
5464 if (line_buf[line_len - 1] == '\n')
5465 {
5466 line_len--;
5467
5468 line_buf[line_len] = 0;
5469 }
5470
5471 if (line_len == 0) return 0;
5472
5473 if (line_buf[line_len - 1] == '\r')
5474 {
5475 line_len--;
5476
5477 line_buf[line_len] = 0;
5478 }
5479
5480 return (line_len);
5481 }
5482
5483 int in_superchop (char *buf)
5484 {
5485 int len = strlen (buf);
5486
5487 while (len)
5488 {
5489 if (buf[len - 1] == '\n')
5490 {
5491 len--;
5492
5493 continue;
5494 }
5495
5496 if (buf[len - 1] == '\r')
5497 {
5498 len--;
5499
5500 continue;
5501 }
5502
5503 break;
5504 }
5505
5506 buf[len] = 0;
5507
5508 return len;
5509 }
5510
5511 char **scan_directory (const char *path)
5512 {
5513 char *tmp_path = mystrdup (path);
5514
5515 size_t tmp_path_len = strlen (tmp_path);
5516
5517 while (tmp_path[tmp_path_len - 1] == '/' || tmp_path[tmp_path_len - 1] == '\\')
5518 {
5519 tmp_path[tmp_path_len - 1] = 0;
5520
5521 tmp_path_len = strlen (tmp_path);
5522 }
5523
5524 char **files = NULL;
5525
5526 int num_files = 0;
5527
5528 DIR *d = NULL;
5529
5530 if ((d = opendir (tmp_path)) != NULL)
5531 {
5532 #ifdef OSX
5533 struct dirent e;
5534
5535 for (;;) {
5536 memset (&e, 0, sizeof (e));
5537 struct dirent *de = NULL;
5538
5539 if (readdir_r (d, &e, &de) != 0)
5540 {
5541 log_error ("ERROR: readdir_r() failed");
5542
5543 break;
5544 }
5545
5546 if (de == NULL) break;
5547 #else
5548 struct dirent *de;
5549
5550 while ((de = readdir (d)) != NULL)
5551 {
5552 #endif
5553 if ((strcmp (de->d_name, ".") == 0) || (strcmp (de->d_name, "..") == 0)) continue;
5554
5555 int path_size = strlen (tmp_path) + 1 + strlen (de->d_name);
5556
5557 char *path_file = (char *) mymalloc (path_size + 1);
5558
5559 snprintf (path_file, path_size + 1, "%s/%s", tmp_path, de->d_name);
5560
5561 path_file[path_size] = 0;
5562
5563 DIR *d_test;
5564
5565 if ((d_test = opendir (path_file)) != NULL)
5566 {
5567 closedir (d_test);
5568
5569 myfree (path_file);
5570 }
5571 else
5572 {
5573 files = (char **) myrealloc (files, num_files * sizeof (char *), sizeof (char *));
5574
5575 num_files++;
5576
5577 files[num_files - 1] = path_file;
5578 }
5579 }
5580
5581 closedir (d);
5582 }
5583 else if (errno == ENOTDIR)
5584 {
5585 files = (char **) myrealloc (files, num_files * sizeof (char *), sizeof (char *));
5586
5587 num_files++;
5588
5589 files[num_files - 1] = mystrdup (path);
5590 }
5591
5592 files = (char **) myrealloc (files, num_files * sizeof (char *), sizeof (char *));
5593
5594 num_files++;
5595
5596 files[num_files - 1] = NULL;
5597
5598 myfree (tmp_path);
5599
5600 return (files);
5601 }
5602
5603 int count_dictionaries (char **dictionary_files)
5604 {
5605 if (dictionary_files == NULL) return 0;
5606
5607 int cnt = 0;
5608
5609 for (int d = 0; dictionary_files[d] != NULL; d++)
5610 {
5611 cnt++;
5612 }
5613
5614 return (cnt);
5615 }
5616
5617 char *stroptitype (const uint opti_type)
5618 {
5619 switch (opti_type)
5620 {
5621 case OPTI_TYPE_ZERO_BYTE: return ((char *) OPTI_STR_ZERO_BYTE); break;
5622 case OPTI_TYPE_PRECOMPUTE_INIT: return ((char *) OPTI_STR_PRECOMPUTE_INIT); break;
5623 case OPTI_TYPE_PRECOMPUTE_MERKLE: return ((char *) OPTI_STR_PRECOMPUTE_MERKLE); break;
5624 case OPTI_TYPE_PRECOMPUTE_PERMUT: return ((char *) OPTI_STR_PRECOMPUTE_PERMUT); break;
5625 case OPTI_TYPE_MEET_IN_MIDDLE: return ((char *) OPTI_STR_MEET_IN_MIDDLE); break;
5626 case OPTI_TYPE_EARLY_SKIP: return ((char *) OPTI_STR_EARLY_SKIP); break;
5627 case OPTI_TYPE_NOT_SALTED: return ((char *) OPTI_STR_NOT_SALTED); break;
5628 case OPTI_TYPE_NOT_ITERATED: return ((char *) OPTI_STR_NOT_ITERATED); break;
5629 case OPTI_TYPE_PREPENDED_SALT: return ((char *) OPTI_STR_PREPENDED_SALT); break;
5630 case OPTI_TYPE_APPENDED_SALT: return ((char *) OPTI_STR_APPENDED_SALT); break;
5631 case OPTI_TYPE_SINGLE_HASH: return ((char *) OPTI_STR_SINGLE_HASH); break;
5632 case OPTI_TYPE_SINGLE_SALT: return ((char *) OPTI_STR_SINGLE_SALT); break;
5633 case OPTI_TYPE_BRUTE_FORCE: return ((char *) OPTI_STR_BRUTE_FORCE); break;
5634 case OPTI_TYPE_RAW_HASH: return ((char *) OPTI_STR_RAW_HASH); break;
5635 case OPTI_TYPE_SLOW_HASH_SIMD: return ((char *) OPTI_STR_SLOW_HASH_SIMD); break;
5636 case OPTI_TYPE_USES_BITS_8: return ((char *) OPTI_STR_USES_BITS_8); break;
5637 case OPTI_TYPE_USES_BITS_16: return ((char *) OPTI_STR_USES_BITS_16); break;
5638 case OPTI_TYPE_USES_BITS_32: return ((char *) OPTI_STR_USES_BITS_32); break;
5639 case OPTI_TYPE_USES_BITS_64: return ((char *) OPTI_STR_USES_BITS_64); break;
5640 }
5641
5642 return (NULL);
5643 }
5644
5645 char *strparser (const uint parser_status)
5646 {
5647 switch (parser_status)
5648 {
5649 case PARSER_OK: return ((char *) PA_000); break;
5650 case PARSER_COMMENT: return ((char *) PA_001); break;
5651 case PARSER_GLOBAL_ZERO: return ((char *) PA_002); break;
5652 case PARSER_GLOBAL_LENGTH: return ((char *) PA_003); break;
5653 case PARSER_HASH_LENGTH: return ((char *) PA_004); break;
5654 case PARSER_HASH_VALUE: return ((char *) PA_005); break;
5655 case PARSER_SALT_LENGTH: return ((char *) PA_006); break;
5656 case PARSER_SALT_VALUE: return ((char *) PA_007); break;
5657 case PARSER_SALT_ITERATION: return ((char *) PA_008); break;
5658 case PARSER_SEPARATOR_UNMATCHED: return ((char *) PA_009); break;
5659 case PARSER_SIGNATURE_UNMATCHED: return ((char *) PA_010); break;
5660 case PARSER_HCCAP_FILE_SIZE: return ((char *) PA_011); break;
5661 case PARSER_HCCAP_EAPOL_SIZE: return ((char *) PA_012); break;
5662 case PARSER_PSAFE2_FILE_SIZE: return ((char *) PA_013); break;
5663 case PARSER_PSAFE3_FILE_SIZE: return ((char *) PA_014); break;
5664 case PARSER_TC_FILE_SIZE: return ((char *) PA_015); break;
5665 case PARSER_SIP_AUTH_DIRECTIVE: return ((char *) PA_016); break;
5666 }
5667
5668 return ((char *) PA_255);
5669 }
5670
5671 char *strhashtype (const uint hash_mode)
5672 {
5673 switch (hash_mode)
5674 {
5675 case 0: return ((char *) HT_00000); break;
5676 case 10: return ((char *) HT_00010); break;
5677 case 11: return ((char *) HT_00011); break;
5678 case 12: return ((char *) HT_00012); break;
5679 case 20: return ((char *) HT_00020); break;
5680 case 21: return ((char *) HT_00021); break;
5681 case 22: return ((char *) HT_00022); break;
5682 case 23: return ((char *) HT_00023); break;
5683 case 30: return ((char *) HT_00030); break;
5684 case 40: return ((char *) HT_00040); break;
5685 case 50: return ((char *) HT_00050); break;
5686 case 60: return ((char *) HT_00060); break;
5687 case 100: return ((char *) HT_00100); break;
5688 case 101: return ((char *) HT_00101); break;
5689 case 110: return ((char *) HT_00110); break;
5690 case 111: return ((char *) HT_00111); break;
5691 case 112: return ((char *) HT_00112); break;
5692 case 120: return ((char *) HT_00120); break;
5693 case 121: return ((char *) HT_00121); break;
5694 case 122: return ((char *) HT_00122); break;
5695 case 124: return ((char *) HT_00124); break;
5696 case 125: return ((char *) HT_00125); break;
5697 case 130: return ((char *) HT_00130); break;
5698 case 131: return ((char *) HT_00131); break;
5699 case 132: return ((char *) HT_00132); break;
5700 case 133: return ((char *) HT_00133); break;
5701 case 140: return ((char *) HT_00140); break;
5702 case 141: return ((char *) HT_00141); break;
5703 case 150: return ((char *) HT_00150); break;
5704 case 160: return ((char *) HT_00160); break;
5705 case 190: return ((char *) HT_00190); break;
5706 case 200: return ((char *) HT_00200); break;
5707 case 300: return ((char *) HT_00300); break;
5708 case 400: return ((char *) HT_00400); break;
5709 case 500: return ((char *) HT_00500); break;
5710 case 501: return ((char *) HT_00501); break;
5711 case 900: return ((char *) HT_00900); break;
5712 case 910: return ((char *) HT_00910); break;
5713 case 1000: return ((char *) HT_01000); break;
5714 case 1100: return ((char *) HT_01100); break;
5715 case 1400: return ((char *) HT_01400); break;
5716 case 1410: return ((char *) HT_01410); break;
5717 case 1420: return ((char *) HT_01420); break;
5718 case 1421: return ((char *) HT_01421); break;
5719 case 1430: return ((char *) HT_01430); break;
5720 case 1440: return ((char *) HT_01440); break;
5721 case 1441: return ((char *) HT_01441); break;
5722 case 1450: return ((char *) HT_01450); break;
5723 case 1460: return ((char *) HT_01460); break;
5724 case 1500: return ((char *) HT_01500); break;
5725 case 1600: return ((char *) HT_01600); break;
5726 case 1700: return ((char *) HT_01700); break;
5727 case 1710: return ((char *) HT_01710); break;
5728 case 1711: return ((char *) HT_01711); break;
5729 case 1720: return ((char *) HT_01720); break;
5730 case 1722: return ((char *) HT_01722); break;
5731 case 1730: return ((char *) HT_01730); break;
5732 case 1731: return ((char *) HT_01731); break;
5733 case 1740: return ((char *) HT_01740); break;
5734 case 1750: return ((char *) HT_01750); break;
5735 case 1760: return ((char *) HT_01760); break;
5736 case 1800: return ((char *) HT_01800); break;
5737 case 2100: return ((char *) HT_02100); break;
5738 case 2400: return ((char *) HT_02400); break;
5739 case 2410: return ((char *) HT_02410); break;
5740 case 2500: return ((char *) HT_02500); break;
5741 case 2600: return ((char *) HT_02600); break;
5742 case 2611: return ((char *) HT_02611); break;
5743 case 2612: return ((char *) HT_02612); break;
5744 case 2711: return ((char *) HT_02711); break;
5745 case 2811: return ((char *) HT_02811); break;
5746 case 3000: return ((char *) HT_03000); break;
5747 case 3100: return ((char *) HT_03100); break;
5748 case 3200: return ((char *) HT_03200); break;
5749 case 3710: return ((char *) HT_03710); break;
5750 case 3711: return ((char *) HT_03711); break;
5751 case 3800: return ((char *) HT_03800); break;
5752 case 4300: return ((char *) HT_04300); break;
5753 case 4400: return ((char *) HT_04400); break;
5754 case 4500: return ((char *) HT_04500); break;
5755 case 4700: return ((char *) HT_04700); break;
5756 case 4800: return ((char *) HT_04800); break;
5757 case 4900: return ((char *) HT_04900); break;
5758 case 5000: return ((char *) HT_05000); break;
5759 case 5100: return ((char *) HT_05100); break;
5760 case 5200: return ((char *) HT_05200); break;
5761 case 5300: return ((char *) HT_05300); break;
5762 case 5400: return ((char *) HT_05400); break;
5763 case 5500: return ((char *) HT_05500); break;
5764 case 5600: return ((char *) HT_05600); break;
5765 case 5700: return ((char *) HT_05700); break;
5766 case 5800: return ((char *) HT_05800); break;
5767 case 6000: return ((char *) HT_06000); break;
5768 case 6100: return ((char *) HT_06100); break;
5769 case 6211: return ((char *) HT_06211); break;
5770 case 6212: return ((char *) HT_06212); break;
5771 case 6213: return ((char *) HT_06213); break;
5772 case 6221: return ((char *) HT_06221); break;
5773 case 6222: return ((char *) HT_06222); break;
5774 case 6223: return ((char *) HT_06223); break;
5775 case 6231: return ((char *) HT_06231); break;
5776 case 6232: return ((char *) HT_06232); break;
5777 case 6233: return ((char *) HT_06233); break;
5778 case 6241: return ((char *) HT_06241); break;
5779 case 6242: return ((char *) HT_06242); break;
5780 case 6243: return ((char *) HT_06243); break;
5781 case 6300: return ((char *) HT_06300); break;
5782 case 6400: return ((char *) HT_06400); break;
5783 case 6500: return ((char *) HT_06500); break;
5784 case 6600: return ((char *) HT_06600); break;
5785 case 6700: return ((char *) HT_06700); break;
5786 case 6800: return ((char *) HT_06800); break;
5787 case 6900: return ((char *) HT_06900); break;
5788 case 7100: return ((char *) HT_07100); break;
5789 case 7200: return ((char *) HT_07200); break;
5790 case 7300: return ((char *) HT_07300); break;
5791 case 7400: return ((char *) HT_07400); break;
5792 case 7500: return ((char *) HT_07500); break;
5793 case 7600: return ((char *) HT_07600); break;
5794 case 7700: return ((char *) HT_07700); break;
5795 case 7800: return ((char *) HT_07800); break;
5796 case 7900: return ((char *) HT_07900); break;
5797 case 8000: return ((char *) HT_08000); break;
5798 case 8100: return ((char *) HT_08100); break;
5799 case 8200: return ((char *) HT_08200); break;
5800 case 8300: return ((char *) HT_08300); break;
5801 case 8400: return ((char *) HT_08400); break;
5802 case 8500: return ((char *) HT_08500); break;
5803 case 8600: return ((char *) HT_08600); break;
5804 case 8700: return ((char *) HT_08700); break;
5805 case 8800: return ((char *) HT_08800); break;
5806 case 8900: return ((char *) HT_08900); break;
5807 case 9000: return ((char *) HT_09000); break;
5808 case 9100: return ((char *) HT_09100); break;
5809 case 9200: return ((char *) HT_09200); break;
5810 case 9300: return ((char *) HT_09300); break;
5811 case 9400: return ((char *) HT_09400); break;
5812 case 9500: return ((char *) HT_09500); break;
5813 case 9600: return ((char *) HT_09600); break;
5814 case 9700: return ((char *) HT_09700); break;
5815 case 9710: return ((char *) HT_09710); break;
5816 case 9720: return ((char *) HT_09720); break;
5817 case 9800: return ((char *) HT_09800); break;
5818 case 9810: return ((char *) HT_09810); break;
5819 case 9820: return ((char *) HT_09820); break;
5820 case 9900: return ((char *) HT_09900); break;
5821 case 10000: return ((char *) HT_10000); break;
5822 case 10100: return ((char *) HT_10100); break;
5823 case 10200: return ((char *) HT_10200); break;
5824 case 10300: return ((char *) HT_10300); break;
5825 case 10400: return ((char *) HT_10400); break;
5826 case 10410: return ((char *) HT_10410); break;
5827 case 10420: return ((char *) HT_10420); break;
5828 case 10500: return ((char *) HT_10500); break;
5829 case 10600: return ((char *) HT_10600); break;
5830 case 10700: return ((char *) HT_10700); break;
5831 case 10800: return ((char *) HT_10800); break;
5832 case 10900: return ((char *) HT_10900); break;
5833 case 11000: return ((char *) HT_11000); break;
5834 case 11100: return ((char *) HT_11100); break;
5835 case 11200: return ((char *) HT_11200); break;
5836 case 11300: return ((char *) HT_11300); break;
5837 case 11400: return ((char *) HT_11400); break;
5838 case 11500: return ((char *) HT_11500); break;
5839 case 11600: return ((char *) HT_11600); break;
5840 case 11700: return ((char *) HT_11700); break;
5841 case 11800: return ((char *) HT_11800); break;
5842 case 11900: return ((char *) HT_11900); break;
5843 case 12000: return ((char *) HT_12000); break;
5844 case 12100: return ((char *) HT_12100); break;
5845 case 12200: return ((char *) HT_12200); break;
5846 case 12300: return ((char *) HT_12300); break;
5847 case 12400: return ((char *) HT_12400); break;
5848 case 12500: return ((char *) HT_12500); break;
5849 case 12600: return ((char *) HT_12600); break;
5850 case 12700: return ((char *) HT_12700); break;
5851 case 12800: return ((char *) HT_12800); break;
5852 case 12900: return ((char *) HT_12900); break;
5853 case 13000: return ((char *) HT_13000); break;
5854 case 13100: return ((char *) HT_13100); break;
5855 case 13200: return ((char *) HT_13200); break;
5856 case 13300: return ((char *) HT_13300); break;
5857 case 13400: return ((char *) HT_13400); break;
5858 case 13500: return ((char *) HT_13500); break;
5859 case 13600: return ((char *) HT_13600); break;
5860 }
5861
5862 return ((char *) "Unknown");
5863 }
5864
5865 char *strstatus (const uint devices_status)
5866 {
5867 switch (devices_status)
5868 {
5869 case STATUS_INIT: return ((char *) ST_0000); break;
5870 case STATUS_STARTING: return ((char *) ST_0001); break;
5871 case STATUS_RUNNING: return ((char *) ST_0002); break;
5872 case STATUS_PAUSED: return ((char *) ST_0003); break;
5873 case STATUS_EXHAUSTED: return ((char *) ST_0004); break;
5874 case STATUS_CRACKED: return ((char *) ST_0005); break;
5875 case STATUS_ABORTED: return ((char *) ST_0006); break;
5876 case STATUS_QUIT: return ((char *) ST_0007); break;
5877 case STATUS_BYPASS: return ((char *) ST_0008); break;
5878 case STATUS_STOP_AT_CHECKPOINT: return ((char *) ST_0009); break;
5879 case STATUS_AUTOTUNE: return ((char *) ST_0010); break;
5880 }
5881
5882 return ((char *) "Unknown");
5883 }
5884
5885 void ascii_digest (char *out_buf, uint salt_pos, uint digest_pos)
5886 {
5887 uint hash_type = data.hash_type;
5888 uint hash_mode = data.hash_mode;
5889 uint salt_type = data.salt_type;
5890 uint opts_type = data.opts_type;
5891 uint opti_type = data.opti_type;
5892 uint dgst_size = data.dgst_size;
5893
5894 char *hashfile = data.hashfile;
5895
5896 uint len = 4096;
5897
5898 uint digest_buf[64] = { 0 };
5899
5900 u64 *digest_buf64 = (u64 *) digest_buf;
5901
5902 char *digests_buf_ptr = (char *) data.digests_buf;
5903
5904 memcpy (digest_buf, digests_buf_ptr + (data.salts_buf[salt_pos].digests_offset * dgst_size) + (digest_pos * dgst_size), dgst_size);
5905
5906 if (opti_type & OPTI_TYPE_PRECOMPUTE_PERMUT)
5907 {
5908 uint tt;
5909
5910 switch (hash_type)
5911 {
5912 case HASH_TYPE_DESCRYPT:
5913 FP (digest_buf[1], digest_buf[0], tt);
5914 break;
5915
5916 case HASH_TYPE_DESRACF:
5917 digest_buf[0] = rotl32 (digest_buf[0], 29);
5918 digest_buf[1] = rotl32 (digest_buf[1], 29);
5919
5920 FP (digest_buf[1], digest_buf[0], tt);
5921 break;
5922
5923 case HASH_TYPE_LM:
5924 FP (digest_buf[1], digest_buf[0], tt);
5925 break;
5926
5927 case HASH_TYPE_NETNTLM:
5928 digest_buf[0] = rotl32 (digest_buf[0], 29);
5929 digest_buf[1] = rotl32 (digest_buf[1], 29);
5930 digest_buf[2] = rotl32 (digest_buf[2], 29);
5931 digest_buf[3] = rotl32 (digest_buf[3], 29);
5932
5933 FP (digest_buf[1], digest_buf[0], tt);
5934 FP (digest_buf[3], digest_buf[2], tt);
5935 break;
5936
5937 case HASH_TYPE_BSDICRYPT:
5938 digest_buf[0] = rotl32 (digest_buf[0], 31);
5939 digest_buf[1] = rotl32 (digest_buf[1], 31);
5940
5941 FP (digest_buf[1], digest_buf[0], tt);
5942 break;
5943 }
5944 }
5945
5946 if (opti_type & OPTI_TYPE_PRECOMPUTE_MERKLE)
5947 {
5948 switch (hash_type)
5949 {
5950 case HASH_TYPE_MD4:
5951 digest_buf[0] += MD4M_A;
5952 digest_buf[1] += MD4M_B;
5953 digest_buf[2] += MD4M_C;
5954 digest_buf[3] += MD4M_D;
5955 break;
5956
5957 case HASH_TYPE_MD5:
5958 digest_buf[0] += MD5M_A;
5959 digest_buf[1] += MD5M_B;
5960 digest_buf[2] += MD5M_C;
5961 digest_buf[3] += MD5M_D;
5962 break;
5963
5964 case HASH_TYPE_SHA1:
5965 digest_buf[0] += SHA1M_A;
5966 digest_buf[1] += SHA1M_B;
5967 digest_buf[2] += SHA1M_C;
5968 digest_buf[3] += SHA1M_D;
5969 digest_buf[4] += SHA1M_E;
5970 break;
5971
5972 case HASH_TYPE_SHA256:
5973 digest_buf[0] += SHA256M_A;
5974 digest_buf[1] += SHA256M_B;
5975 digest_buf[2] += SHA256M_C;
5976 digest_buf[3] += SHA256M_D;
5977 digest_buf[4] += SHA256M_E;
5978 digest_buf[5] += SHA256M_F;
5979 digest_buf[6] += SHA256M_G;
5980 digest_buf[7] += SHA256M_H;
5981 break;
5982
5983 case HASH_TYPE_SHA384:
5984 digest_buf64[0] += SHA384M_A;
5985 digest_buf64[1] += SHA384M_B;
5986 digest_buf64[2] += SHA384M_C;
5987 digest_buf64[3] += SHA384M_D;
5988 digest_buf64[4] += SHA384M_E;
5989 digest_buf64[5] += SHA384M_F;
5990 digest_buf64[6] += 0;
5991 digest_buf64[7] += 0;
5992 break;
5993
5994 case HASH_TYPE_SHA512:
5995 digest_buf64[0] += SHA512M_A;
5996 digest_buf64[1] += SHA512M_B;
5997 digest_buf64[2] += SHA512M_C;
5998 digest_buf64[3] += SHA512M_D;
5999 digest_buf64[4] += SHA512M_E;
6000 digest_buf64[5] += SHA512M_F;
6001 digest_buf64[6] += SHA512M_G;
6002 digest_buf64[7] += SHA512M_H;
6003 break;
6004 }
6005 }
6006
6007 if (opts_type & OPTS_TYPE_PT_GENERATE_LE)
6008 {
6009 if (dgst_size == DGST_SIZE_4_2)
6010 {
6011 for (int i = 0; i < 2; i++) digest_buf[i] = byte_swap_32 (digest_buf[i]);
6012 }
6013 else if (dgst_size == DGST_SIZE_4_4)
6014 {
6015 for (int i = 0; i < 4; i++) digest_buf[i] = byte_swap_32 (digest_buf[i]);
6016 }
6017 else if (dgst_size == DGST_SIZE_4_5)
6018 {
6019 for (int i = 0; i < 5; i++) digest_buf[i] = byte_swap_32 (digest_buf[i]);
6020 }
6021 else if (dgst_size == DGST_SIZE_4_6)
6022 {
6023 for (int i = 0; i < 6; i++) digest_buf[i] = byte_swap_32 (digest_buf[i]);
6024 }
6025 else if (dgst_size == DGST_SIZE_4_8)
6026 {
6027 for (int i = 0; i < 8; i++) digest_buf[i] = byte_swap_32 (digest_buf[i]);
6028 }
6029 else if ((dgst_size == DGST_SIZE_4_16) || (dgst_size == DGST_SIZE_8_8)) // same size, same result :)
6030 {
6031 if (hash_type == HASH_TYPE_WHIRLPOOL)
6032 {
6033 for (int i = 0; i < 16; i++) digest_buf[i] = byte_swap_32 (digest_buf[i]);
6034 }
6035 else if (hash_type == HASH_TYPE_SHA384)
6036 {
6037 for (int i = 0; i < 8; i++) digest_buf64[i] = byte_swap_64 (digest_buf64[i]);
6038 }
6039 else if (hash_type == HASH_TYPE_SHA512)
6040 {
6041 for (int i = 0; i < 8; i++) digest_buf64[i] = byte_swap_64 (digest_buf64[i]);
6042 }
6043 else if (hash_type == HASH_TYPE_GOST)
6044 {
6045 for (int i = 0; i < 16; i++) digest_buf[i] = byte_swap_32 (digest_buf[i]);
6046 }
6047 }
6048 else if (dgst_size == DGST_SIZE_4_64)
6049 {
6050 for (int i = 0; i < 64; i++) digest_buf[i] = byte_swap_32 (digest_buf[i]);
6051 }
6052 else if (dgst_size == DGST_SIZE_8_25)
6053 {
6054 for (int i = 0; i < 25; i++) digest_buf64[i] = byte_swap_64 (digest_buf64[i]);
6055 }
6056 }
6057
6058 uint isSalted = ((data.salt_type == SALT_TYPE_INTERN)
6059 | (data.salt_type == SALT_TYPE_EXTERN)
6060 | (data.salt_type == SALT_TYPE_EMBEDDED));
6061
6062 salt_t salt;
6063
6064 if (isSalted)
6065 {
6066 memset (&salt, 0, sizeof (salt_t));
6067
6068 memcpy (&salt, &data.salts_buf[salt_pos], sizeof (salt_t));
6069
6070 char *ptr = (char *) salt.salt_buf;
6071
6072 uint len = salt.salt_len;
6073
6074 if (opti_type & OPTI_TYPE_PRECOMPUTE_PERMUT)
6075 {
6076 uint tt;
6077
6078 switch (hash_type)
6079 {
6080 case HASH_TYPE_NETNTLM:
6081
6082 salt.salt_buf[0] = rotr32 (salt.salt_buf[0], 3);
6083 salt.salt_buf[1] = rotr32 (salt.salt_buf[1], 3);
6084
6085 FP (salt.salt_buf[1], salt.salt_buf[0], tt);
6086
6087 break;
6088 }
6089 }
6090
6091 if (opts_type & OPTS_TYPE_ST_UNICODE)
6092 {
6093 for (uint i = 0, j = 0; i < len; i += 1, j += 2)
6094 {
6095 ptr[i] = ptr[j];
6096 }
6097
6098 len = len / 2;
6099 }
6100
6101 if (opts_type & OPTS_TYPE_ST_GENERATE_LE)
6102 {
6103 uint max = salt.salt_len / 4;
6104
6105 if (len % 4) max++;
6106
6107 for (uint i = 0; i < max; i++)
6108 {
6109 salt.salt_buf[i] = byte_swap_32 (salt.salt_buf[i]);
6110 }
6111 }
6112
6113 if (opts_type & OPTS_TYPE_ST_HEX)
6114 {
6115 char tmp[64] = { 0 };
6116
6117 for (uint i = 0, j = 0; i < len; i += 1, j += 2)
6118 {
6119 sprintf (tmp + j, "%02x", (unsigned char) ptr[i]);
6120 }
6121
6122 len = len * 2;
6123
6124 memcpy (ptr, tmp, len);
6125 }
6126
6127 uint memset_size = ((48 - (int) len) > 0) ? (48 - len) : 0;
6128
6129 memset (ptr + len, 0, memset_size);
6130
6131 salt.salt_len = len;
6132 }
6133
6134 //
6135 // some modes require special encoding
6136 //
6137
6138 uint out_buf_plain[256] = { 0 };
6139 uint out_buf_salt[256] = { 0 };
6140
6141 char tmp_buf[1024] = { 0 };
6142
6143 char *ptr_plain = (char *) out_buf_plain;
6144 char *ptr_salt = (char *) out_buf_salt;
6145
6146 if (hash_mode == 22)
6147 {
6148 char username[30] = { 0 };
6149
6150 memcpy (username, salt.salt_buf, salt.salt_len - 22);
6151
6152 char sig[6] = { 'n', 'r', 'c', 's', 't', 'n' };
6153
6154 u16 *ptr = (u16 *) digest_buf;
6155
6156 tmp_buf[ 0] = sig[0];
6157 tmp_buf[ 1] = int_to_base64 (((ptr[1]) >> 12) & 0x3f);
6158 tmp_buf[ 2] = int_to_base64 (((ptr[1]) >> 6) & 0x3f);
6159 tmp_buf[ 3] = int_to_base64 (((ptr[1]) >> 0) & 0x3f);
6160 tmp_buf[ 4] = int_to_base64 (((ptr[0]) >> 12) & 0x3f);
6161 tmp_buf[ 5] = int_to_base64 (((ptr[0]) >> 6) & 0x3f);
6162 tmp_buf[ 6] = sig[1];
6163 tmp_buf[ 7] = int_to_base64 (((ptr[0]) >> 0) & 0x3f);
6164 tmp_buf[ 8] = int_to_base64 (((ptr[3]) >> 12) & 0x3f);
6165 tmp_buf[ 9] = int_to_base64 (((ptr[3]) >> 6) & 0x3f);
6166 tmp_buf[10] = int_to_base64 (((ptr[3]) >> 0) & 0x3f);
6167 tmp_buf[11] = int_to_base64 (((ptr[2]) >> 12) & 0x3f);
6168 tmp_buf[12] = sig[2];
6169 tmp_buf[13] = int_to_base64 (((ptr[2]) >> 6) & 0x3f);
6170 tmp_buf[14] = int_to_base64 (((ptr[2]) >> 0) & 0x3f);
6171 tmp_buf[15] = int_to_base64 (((ptr[5]) >> 12) & 0x3f);
6172 tmp_buf[16] = int_to_base64 (((ptr[5]) >> 6) & 0x3f);
6173 tmp_buf[17] = sig[3];
6174 tmp_buf[18] = int_to_base64 (((ptr[5]) >> 0) & 0x3f);
6175 tmp_buf[19] = int_to_base64 (((ptr[4]) >> 12) & 0x3f);
6176 tmp_buf[20] = int_to_base64 (((ptr[4]) >> 6) & 0x3f);
6177 tmp_buf[21] = int_to_base64 (((ptr[4]) >> 0) & 0x3f);
6178 tmp_buf[22] = int_to_base64 (((ptr[7]) >> 12) & 0x3f);
6179 tmp_buf[23] = sig[4];
6180 tmp_buf[24] = int_to_base64 (((ptr[7]) >> 6) & 0x3f);
6181 tmp_buf[25] = int_to_base64 (((ptr[7]) >> 0) & 0x3f);
6182 tmp_buf[26] = int_to_base64 (((ptr[6]) >> 12) & 0x3f);
6183 tmp_buf[27] = int_to_base64 (((ptr[6]) >> 6) & 0x3f);
6184 tmp_buf[28] = int_to_base64 (((ptr[6]) >> 0) & 0x3f);
6185 tmp_buf[29] = sig[5];
6186
6187 snprintf (out_buf, len-1, "%s:%s",
6188 tmp_buf,
6189 username);
6190 }
6191 else if (hash_mode == 23)
6192 {
6193 // do not show the skyper part in output
6194
6195 char *salt_buf_ptr = (char *) salt.salt_buf;
6196
6197 salt_buf_ptr[salt.salt_len - 8] = 0;
6198
6199 snprintf (out_buf, len-1, "%08x%08x%08x%08x:%s",
6200 digest_buf[0],
6201 digest_buf[1],
6202 digest_buf[2],
6203 digest_buf[3],
6204 salt_buf_ptr);
6205 }
6206 else if (hash_mode == 101)
6207 {
6208 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6209
6210 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6211 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6212 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6213 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6214 digest_buf[4] = byte_swap_32 (digest_buf[4]);
6215
6216 memcpy (tmp_buf, digest_buf, 20);
6217
6218 base64_encode (int_to_base64, (const u8 *) tmp_buf, 20, (u8 *) ptr_plain);
6219
6220 snprintf (out_buf, len-1, "{SHA}%s", ptr_plain);
6221 }
6222 else if (hash_mode == 111)
6223 {
6224 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6225
6226 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6227 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6228 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6229 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6230 digest_buf[4] = byte_swap_32 (digest_buf[4]);
6231
6232 memcpy (tmp_buf, digest_buf, 20);
6233 memcpy (tmp_buf + 20, salt.salt_buf, salt.salt_len);
6234
6235 base64_encode (int_to_base64, (const u8 *) tmp_buf, 20 + salt.salt_len, (u8 *) ptr_plain);
6236
6237 snprintf (out_buf, len-1, "{SSHA}%s", ptr_plain);
6238 }
6239 else if ((hash_mode == 122) || (hash_mode == 125))
6240 {
6241 snprintf (out_buf, len-1, "%s%08x%08x%08x%08x%08x",
6242 (char *) salt.salt_buf,
6243 digest_buf[0],
6244 digest_buf[1],
6245 digest_buf[2],
6246 digest_buf[3],
6247 digest_buf[4]);
6248 }
6249 else if (hash_mode == 124)
6250 {
6251 snprintf (out_buf, len-1, "sha1$%s$%08x%08x%08x%08x%08x",
6252 (char *) salt.salt_buf,
6253 digest_buf[0],
6254 digest_buf[1],
6255 digest_buf[2],
6256 digest_buf[3],
6257 digest_buf[4]);
6258 }
6259 else if (hash_mode == 131)
6260 {
6261 snprintf (out_buf, len-1, "0x0100%s%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
6262 (char *) salt.salt_buf,
6263 0, 0, 0, 0, 0,
6264 digest_buf[0],
6265 digest_buf[1],
6266 digest_buf[2],
6267 digest_buf[3],
6268 digest_buf[4]);
6269 }
6270 else if (hash_mode == 132)
6271 {
6272 snprintf (out_buf, len-1, "0x0100%s%08x%08x%08x%08x%08x",
6273 (char *) salt.salt_buf,
6274 digest_buf[0],
6275 digest_buf[1],
6276 digest_buf[2],
6277 digest_buf[3],
6278 digest_buf[4]);
6279 }
6280 else if (hash_mode == 133)
6281 {
6282 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6283
6284 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6285 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6286 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6287 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6288 digest_buf[4] = byte_swap_32 (digest_buf[4]);
6289
6290 memcpy (tmp_buf, digest_buf, 20);
6291
6292 base64_encode (int_to_base64, (const u8 *) tmp_buf, 20, (u8 *) ptr_plain);
6293
6294 snprintf (out_buf, len-1, "%s", ptr_plain);
6295 }
6296 else if (hash_mode == 141)
6297 {
6298 memcpy (tmp_buf, salt.salt_buf, salt.salt_len);
6299
6300 base64_encode (int_to_base64, (const u8 *) tmp_buf, salt.salt_len, (u8 *) ptr_salt);
6301
6302 memset (tmp_buf, 0, sizeof (tmp_buf));
6303
6304 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6305
6306 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6307 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6308 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6309 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6310 digest_buf[4] = byte_swap_32 (digest_buf[4]);
6311
6312 memcpy (tmp_buf, digest_buf, 20);
6313
6314 base64_encode (int_to_base64, (const u8 *) tmp_buf, 20, (u8 *) ptr_plain);
6315
6316 ptr_plain[27] = 0;
6317
6318 snprintf (out_buf, len-1, "%s%s*%s", SIGNATURE_EPISERVER, ptr_salt, ptr_plain);
6319 }
6320 else if (hash_mode == 400)
6321 {
6322 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6323
6324 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6325 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6326 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6327 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6328
6329 phpass_encode ((unsigned char *) digest_buf, (unsigned char *) ptr_plain);
6330
6331 snprintf (out_buf, len-1, "%s%s%s", (char *) salt.salt_sign, (char *) salt.salt_buf, (char *) ptr_plain);
6332 }
6333 else if (hash_mode == 500)
6334 {
6335 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6336
6337 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6338 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6339 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6340 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6341
6342 md5crypt_encode ((unsigned char *) digest_buf, (unsigned char *) ptr_plain);
6343
6344 if (salt.salt_iter == ROUNDS_MD5CRYPT)
6345 {
6346 snprintf (out_buf, len-1, "$1$%s$%s", (char *) salt.salt_buf, (char *) ptr_plain);
6347 }
6348 else
6349 {
6350 snprintf (out_buf, len-1, "$1$rounds=%i$%s$%s", salt.salt_iter, (char *) salt.salt_buf, (char *) ptr_plain);
6351 }
6352 }
6353 else if (hash_mode == 501)
6354 {
6355 uint digest_idx = salt.digests_offset + digest_pos;
6356
6357 hashinfo_t **hashinfo_ptr = data.hash_info;
6358 char *hash_buf = hashinfo_ptr[digest_idx]->orighash;
6359
6360 snprintf (out_buf, len-1, "%s", hash_buf);
6361 }
6362 else if (hash_mode == 1421)
6363 {
6364 u8 *salt_ptr = (u8 *) salt.salt_buf;
6365
6366 snprintf (out_buf, len-1, "%c%c%c%c%c%c%08x%08x%08x%08x%08x%08x%08x%08x",
6367 salt_ptr[0],
6368 salt_ptr[1],
6369 salt_ptr[2],
6370 salt_ptr[3],
6371 salt_ptr[4],
6372 salt_ptr[5],
6373 digest_buf[0],
6374 digest_buf[1],
6375 digest_buf[2],
6376 digest_buf[3],
6377 digest_buf[4],
6378 digest_buf[5],
6379 digest_buf[6],
6380 digest_buf[7]);
6381 }
6382 else if (hash_mode == 1441)
6383 {
6384 memcpy (tmp_buf, salt.salt_buf, salt.salt_len);
6385
6386 base64_encode (int_to_base64, (const u8 *) tmp_buf, salt.salt_len, (u8 *) ptr_salt);
6387
6388 memset (tmp_buf, 0, sizeof (tmp_buf));
6389
6390 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6391
6392 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6393 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6394 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6395 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6396 digest_buf[4] = byte_swap_32 (digest_buf[4]);
6397 digest_buf[5] = byte_swap_32 (digest_buf[5]);
6398 digest_buf[6] = byte_swap_32 (digest_buf[6]);
6399 digest_buf[7] = byte_swap_32 (digest_buf[7]);
6400
6401 memcpy (tmp_buf, digest_buf, 32);
6402
6403 base64_encode (int_to_base64, (const u8 *) tmp_buf, 32, (u8 *) ptr_plain);
6404
6405 ptr_plain[43] = 0;
6406
6407 snprintf (out_buf, len-1, "%s%s*%s", SIGNATURE_EPISERVER4, ptr_salt, ptr_plain);
6408 }
6409 else if (hash_mode == 1500)
6410 {
6411 out_buf[0] = salt.salt_sign[0] & 0xff;
6412 out_buf[1] = salt.salt_sign[1] & 0xff;
6413 //original method, but changed because of this ticket: https://hashcat.net/trac/ticket/269
6414 //out_buf[0] = int_to_itoa64 ((salt.salt_buf[0] >> 0) & 0x3f);
6415 //out_buf[1] = int_to_itoa64 ((salt.salt_buf[0] >> 6) & 0x3f);
6416
6417 memset (tmp_buf, 0, sizeof (tmp_buf));
6418
6419 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6420
6421 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6422 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6423
6424 memcpy (tmp_buf, digest_buf, 8);
6425
6426 base64_encode (int_to_itoa64, (const u8 *) tmp_buf, 8, (u8 *) ptr_plain);
6427
6428 snprintf (out_buf + 2, len-1-2, "%s", ptr_plain);
6429
6430 out_buf[13] = 0;
6431 }
6432 else if (hash_mode == 1600)
6433 {
6434 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6435
6436 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6437 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6438 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6439 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6440
6441 md5crypt_encode ((unsigned char *) digest_buf, (unsigned char *) ptr_plain);
6442
6443 if (salt.salt_iter == ROUNDS_MD5CRYPT)
6444 {
6445 snprintf (out_buf, len-1, "$apr1$%s$%s", (char *) salt.salt_buf, (char *) ptr_plain);
6446 }
6447 else
6448 {
6449 snprintf (out_buf, len-1, "$apr1$rounds=%i$%s$%s", salt.salt_iter, (char *) salt.salt_buf, (char *) ptr_plain);
6450 }
6451 }
6452 else if (hash_mode == 1711)
6453 {
6454 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6455
6456 digest_buf64[0] = byte_swap_64 (digest_buf64[0]);
6457 digest_buf64[1] = byte_swap_64 (digest_buf64[1]);
6458 digest_buf64[2] = byte_swap_64 (digest_buf64[2]);
6459 digest_buf64[3] = byte_swap_64 (digest_buf64[3]);
6460 digest_buf64[4] = byte_swap_64 (digest_buf64[4]);
6461 digest_buf64[5] = byte_swap_64 (digest_buf64[5]);
6462 digest_buf64[6] = byte_swap_64 (digest_buf64[6]);
6463 digest_buf64[7] = byte_swap_64 (digest_buf64[7]);
6464
6465 memcpy (tmp_buf, digest_buf, 64);
6466 memcpy (tmp_buf + 64, salt.salt_buf, salt.salt_len);
6467
6468 base64_encode (int_to_base64, (const u8 *) tmp_buf, 64 + salt.salt_len, (u8 *) ptr_plain);
6469
6470 snprintf (out_buf, len-1, "%s%s", SIGNATURE_SHA512B64S, ptr_plain);
6471 }
6472 else if (hash_mode == 1722)
6473 {
6474 uint *ptr = digest_buf;
6475
6476 snprintf (out_buf, len-1, "%s%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
6477 (unsigned char *) salt.salt_buf,
6478 ptr[ 1], ptr[ 0],
6479 ptr[ 3], ptr[ 2],
6480 ptr[ 5], ptr[ 4],
6481 ptr[ 7], ptr[ 6],
6482 ptr[ 9], ptr[ 8],
6483 ptr[11], ptr[10],
6484 ptr[13], ptr[12],
6485 ptr[15], ptr[14]);
6486 }
6487 else if (hash_mode == 1731)
6488 {
6489 uint *ptr = digest_buf;
6490
6491 snprintf (out_buf, len-1, "0x0200%s%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
6492 (unsigned char *) salt.salt_buf,
6493 ptr[ 1], ptr[ 0],
6494 ptr[ 3], ptr[ 2],
6495 ptr[ 5], ptr[ 4],
6496 ptr[ 7], ptr[ 6],
6497 ptr[ 9], ptr[ 8],
6498 ptr[11], ptr[10],
6499 ptr[13], ptr[12],
6500 ptr[15], ptr[14]);
6501 }
6502 else if (hash_mode == 1800)
6503 {
6504 // temp workaround
6505
6506 digest_buf64[0] = byte_swap_64 (digest_buf64[0]);
6507 digest_buf64[1] = byte_swap_64 (digest_buf64[1]);
6508 digest_buf64[2] = byte_swap_64 (digest_buf64[2]);
6509 digest_buf64[3] = byte_swap_64 (digest_buf64[3]);
6510 digest_buf64[4] = byte_swap_64 (digest_buf64[4]);
6511 digest_buf64[5] = byte_swap_64 (digest_buf64[5]);
6512 digest_buf64[6] = byte_swap_64 (digest_buf64[6]);
6513 digest_buf64[7] = byte_swap_64 (digest_buf64[7]);
6514
6515 sha512crypt_encode ((unsigned char *) digest_buf64, (unsigned char *) ptr_plain);
6516
6517 if (salt.salt_iter == ROUNDS_SHA512CRYPT)
6518 {
6519 snprintf (out_buf, len-1, "$6$%s$%s", (char *) salt.salt_buf, (char *) ptr_plain);
6520 }
6521 else
6522 {
6523 snprintf (out_buf, len-1, "$6$rounds=%i$%s$%s", salt.salt_iter, (char *) salt.salt_buf, (char *) ptr_plain);
6524 }
6525 }
6526 else if (hash_mode == 2100)
6527 {
6528 uint pos = 0;
6529
6530 snprintf (out_buf + pos, len-1, "%s%i#",
6531 SIGNATURE_DCC2,
6532 salt.salt_iter + 1);
6533
6534 uint signature_len = strlen (out_buf);
6535
6536 pos += signature_len;
6537 len -= signature_len;
6538
6539 char *salt_ptr = (char *) salt.salt_buf;
6540
6541 for (uint i = 0; i < salt.salt_len; i++, pos++, len--) snprintf (out_buf + pos, len-1, "%c", salt_ptr[i]);
6542
6543 snprintf (out_buf + pos, len-1, "#%08x%08x%08x%08x",
6544 byte_swap_32 (digest_buf[0]),
6545 byte_swap_32 (digest_buf[1]),
6546 byte_swap_32 (digest_buf[2]),
6547 byte_swap_32 (digest_buf[3]));
6548 }
6549 else if ((hash_mode == 2400) || (hash_mode == 2410))
6550 {
6551 memcpy (tmp_buf, digest_buf, 16);
6552
6553 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6554
6555 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6556 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6557 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6558 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6559
6560 out_buf[ 0] = int_to_itoa64 ((digest_buf[0] >> 0) & 0x3f);
6561 out_buf[ 1] = int_to_itoa64 ((digest_buf[0] >> 6) & 0x3f);
6562 out_buf[ 2] = int_to_itoa64 ((digest_buf[0] >> 12) & 0x3f);
6563 out_buf[ 3] = int_to_itoa64 ((digest_buf[0] >> 18) & 0x3f);
6564
6565 out_buf[ 4] = int_to_itoa64 ((digest_buf[1] >> 0) & 0x3f);
6566 out_buf[ 5] = int_to_itoa64 ((digest_buf[1] >> 6) & 0x3f);
6567 out_buf[ 6] = int_to_itoa64 ((digest_buf[1] >> 12) & 0x3f);
6568 out_buf[ 7] = int_to_itoa64 ((digest_buf[1] >> 18) & 0x3f);
6569
6570 out_buf[ 8] = int_to_itoa64 ((digest_buf[2] >> 0) & 0x3f);
6571 out_buf[ 9] = int_to_itoa64 ((digest_buf[2] >> 6) & 0x3f);
6572 out_buf[10] = int_to_itoa64 ((digest_buf[2] >> 12) & 0x3f);
6573 out_buf[11] = int_to_itoa64 ((digest_buf[2] >> 18) & 0x3f);
6574
6575 out_buf[12] = int_to_itoa64 ((digest_buf[3] >> 0) & 0x3f);
6576 out_buf[13] = int_to_itoa64 ((digest_buf[3] >> 6) & 0x3f);
6577 out_buf[14] = int_to_itoa64 ((digest_buf[3] >> 12) & 0x3f);
6578 out_buf[15] = int_to_itoa64 ((digest_buf[3] >> 18) & 0x3f);
6579
6580 out_buf[16] = 0;
6581 }
6582 else if (hash_mode == 2500)
6583 {
6584 wpa_t *wpas = (wpa_t *) data.esalts_buf;
6585
6586 wpa_t *wpa = &wpas[salt_pos];
6587
6588 snprintf (out_buf, len-1, "%s:%02x%02x%02x%02x%02x%02x:%02x%02x%02x%02x%02x%02x",
6589 (char *) salt.salt_buf,
6590 wpa->orig_mac1[0],
6591 wpa->orig_mac1[1],
6592 wpa->orig_mac1[2],
6593 wpa->orig_mac1[3],
6594 wpa->orig_mac1[4],
6595 wpa->orig_mac1[5],
6596 wpa->orig_mac2[0],
6597 wpa->orig_mac2[1],
6598 wpa->orig_mac2[2],
6599 wpa->orig_mac2[3],
6600 wpa->orig_mac2[4],
6601 wpa->orig_mac2[5]);
6602 }
6603 else if (hash_mode == 4400)
6604 {
6605 snprintf (out_buf, len-1, "%08x%08x%08x%08x",
6606 byte_swap_32 (digest_buf[0]),
6607 byte_swap_32 (digest_buf[1]),
6608 byte_swap_32 (digest_buf[2]),
6609 byte_swap_32 (digest_buf[3]));
6610 }
6611 else if (hash_mode == 4700)
6612 {
6613 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x",
6614 byte_swap_32 (digest_buf[0]),
6615 byte_swap_32 (digest_buf[1]),
6616 byte_swap_32 (digest_buf[2]),
6617 byte_swap_32 (digest_buf[3]),
6618 byte_swap_32 (digest_buf[4]));
6619 }
6620 else if (hash_mode == 4800)
6621 {
6622 u8 chap_id_byte = (u8) salt.salt_buf[4];
6623
6624 snprintf (out_buf, len-1, "%08x%08x%08x%08x:%08x%08x%08x%08x:%02x",
6625 digest_buf[0],
6626 digest_buf[1],
6627 digest_buf[2],
6628 digest_buf[3],
6629 byte_swap_32 (salt.salt_buf[0]),
6630 byte_swap_32 (salt.salt_buf[1]),
6631 byte_swap_32 (salt.salt_buf[2]),
6632 byte_swap_32 (salt.salt_buf[3]),
6633 chap_id_byte);
6634 }
6635 else if (hash_mode == 4900)
6636 {
6637 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x",
6638 byte_swap_32 (digest_buf[0]),
6639 byte_swap_32 (digest_buf[1]),
6640 byte_swap_32 (digest_buf[2]),
6641 byte_swap_32 (digest_buf[3]),
6642 byte_swap_32 (digest_buf[4]));
6643 }
6644 else if (hash_mode == 5100)
6645 {
6646 snprintf (out_buf, len-1, "%08x%08x",
6647 digest_buf[0],
6648 digest_buf[1]);
6649 }
6650 else if (hash_mode == 5200)
6651 {
6652 snprintf (out_buf, len-1, "%s", hashfile);
6653 }
6654 else if (hash_mode == 5300)
6655 {
6656 ikepsk_t *ikepsks = (ikepsk_t *) data.esalts_buf;
6657
6658 ikepsk_t *ikepsk = &ikepsks[salt_pos];
6659
6660 int buf_len = len -1;
6661
6662 // msg_buf
6663
6664 uint ikepsk_msg_len = ikepsk->msg_len / 4;
6665
6666 for (uint i = 0; i < ikepsk_msg_len; i++)
6667 {
6668 if ((i == 32) || (i == 64) || (i == 66) || (i == 68) || (i == 108))
6669 {
6670 snprintf (out_buf, buf_len, ":");
6671
6672 buf_len--;
6673 out_buf++;
6674 }
6675
6676 snprintf (out_buf, buf_len, "%08x", byte_swap_32 (ikepsk->msg_buf[i]));
6677
6678 buf_len -= 8;
6679 out_buf += 8;
6680 }
6681
6682 // nr_buf
6683
6684 uint ikepsk_nr_len = ikepsk->nr_len / 4;
6685
6686 for (uint i = 0; i < ikepsk_nr_len; i++)
6687 {
6688 if ((i == 0) || (i == 5))
6689 {
6690 snprintf (out_buf, buf_len, ":");
6691
6692 buf_len--;
6693 out_buf++;
6694 }
6695
6696 snprintf (out_buf, buf_len, "%08x", byte_swap_32 (ikepsk->nr_buf[i]));
6697
6698 buf_len -= 8;
6699 out_buf += 8;
6700 }
6701
6702 // digest_buf
6703
6704 for (uint i = 0; i < 4; i++)
6705 {
6706 if (i == 0)
6707 {
6708 snprintf (out_buf, buf_len, ":");
6709
6710 buf_len--;
6711 out_buf++;
6712 }
6713
6714 snprintf (out_buf, buf_len, "%08x", digest_buf[i]);
6715
6716 buf_len -= 8;
6717 out_buf += 8;
6718 }
6719 }
6720 else if (hash_mode == 5400)
6721 {
6722 ikepsk_t *ikepsks = (ikepsk_t *) data.esalts_buf;
6723
6724 ikepsk_t *ikepsk = &ikepsks[salt_pos];
6725
6726 int buf_len = len -1;
6727
6728 // msg_buf
6729
6730 uint ikepsk_msg_len = ikepsk->msg_len / 4;
6731
6732 for (uint i = 0; i < ikepsk_msg_len; i++)
6733 {
6734 if ((i == 32) || (i == 64) || (i == 66) || (i == 68) || (i == 108))
6735 {
6736 snprintf (out_buf, buf_len, ":");
6737
6738 buf_len--;
6739 out_buf++;
6740 }
6741
6742 snprintf (out_buf, buf_len, "%08x", byte_swap_32 (ikepsk->msg_buf[i]));
6743
6744 buf_len -= 8;
6745 out_buf += 8;
6746 }
6747
6748 // nr_buf
6749
6750 uint ikepsk_nr_len = ikepsk->nr_len / 4;
6751
6752 for (uint i = 0; i < ikepsk_nr_len; i++)
6753 {
6754 if ((i == 0) || (i == 5))
6755 {
6756 snprintf (out_buf, buf_len, ":");
6757
6758 buf_len--;
6759 out_buf++;
6760 }
6761
6762 snprintf (out_buf, buf_len, "%08x", byte_swap_32 (ikepsk->nr_buf[i]));
6763
6764 buf_len -= 8;
6765 out_buf += 8;
6766 }
6767
6768 // digest_buf
6769
6770 for (uint i = 0; i < 5; i++)
6771 {
6772 if (i == 0)
6773 {
6774 snprintf (out_buf, buf_len, ":");
6775
6776 buf_len--;
6777 out_buf++;
6778 }
6779
6780 snprintf (out_buf, buf_len, "%08x", digest_buf[i]);
6781
6782 buf_len -= 8;
6783 out_buf += 8;
6784 }
6785 }
6786 else if (hash_mode == 5500)
6787 {
6788 netntlm_t *netntlms = (netntlm_t *) data.esalts_buf;
6789
6790 netntlm_t *netntlm = &netntlms[salt_pos];
6791
6792 char user_buf[64] = { 0 };
6793 char domain_buf[64] = { 0 };
6794 char srvchall_buf[1024] = { 0 };
6795 char clichall_buf[1024] = { 0 };
6796
6797 for (uint i = 0, j = 0; j < netntlm->user_len; i += 1, j += 2)
6798 {
6799 char *ptr = (char *) netntlm->userdomain_buf;
6800
6801 user_buf[i] = ptr[j];
6802 }
6803
6804 for (uint i = 0, j = 0; j < netntlm->domain_len; i += 1, j += 2)
6805 {
6806 char *ptr = (char *) netntlm->userdomain_buf;
6807
6808 domain_buf[i] = ptr[netntlm->user_len + j];
6809 }
6810
6811 for (uint i = 0, j = 0; i < netntlm->srvchall_len; i += 1, j += 2)
6812 {
6813 u8 *ptr = (u8 *) netntlm->chall_buf;
6814
6815 sprintf (srvchall_buf + j, "%02x", ptr[i]);
6816 }
6817
6818 for (uint i = 0, j = 0; i < netntlm->clichall_len; i += 1, j += 2)
6819 {
6820 u8 *ptr = (u8 *) netntlm->chall_buf;
6821
6822 sprintf (clichall_buf + j, "%02x", ptr[netntlm->srvchall_len + i]);
6823 }
6824
6825 snprintf (out_buf, len-1, "%s::%s:%s:%08x%08x%08x%08x%08x%08x:%s",
6826 user_buf,
6827 domain_buf,
6828 srvchall_buf,
6829 digest_buf[0],
6830 digest_buf[1],
6831 digest_buf[2],
6832 digest_buf[3],
6833 byte_swap_32 (salt.salt_buf_pc[0]),
6834 byte_swap_32 (salt.salt_buf_pc[1]),
6835 clichall_buf);
6836 }
6837 else if (hash_mode == 5600)
6838 {
6839 netntlm_t *netntlms = (netntlm_t *) data.esalts_buf;
6840
6841 netntlm_t *netntlm = &netntlms[salt_pos];
6842
6843 char user_buf[64] = { 0 };
6844 char domain_buf[64] = { 0 };
6845 char srvchall_buf[1024] = { 0 };
6846 char clichall_buf[1024] = { 0 };
6847
6848 for (uint i = 0, j = 0; j < netntlm->user_len; i += 1, j += 2)
6849 {
6850 char *ptr = (char *) netntlm->userdomain_buf;
6851
6852 user_buf[i] = ptr[j];
6853 }
6854
6855 for (uint i = 0, j = 0; j < netntlm->domain_len; i += 1, j += 2)
6856 {
6857 char *ptr = (char *) netntlm->userdomain_buf;
6858
6859 domain_buf[i] = ptr[netntlm->user_len + j];
6860 }
6861
6862 for (uint i = 0, j = 0; i < netntlm->srvchall_len; i += 1, j += 2)
6863 {
6864 u8 *ptr = (u8 *) netntlm->chall_buf;
6865
6866 sprintf (srvchall_buf + j, "%02x", ptr[i]);
6867 }
6868
6869 for (uint i = 0, j = 0; i < netntlm->clichall_len; i += 1, j += 2)
6870 {
6871 u8 *ptr = (u8 *) netntlm->chall_buf;
6872
6873 sprintf (clichall_buf + j, "%02x", ptr[netntlm->srvchall_len + i]);
6874 }
6875
6876 snprintf (out_buf, len-1, "%s::%s:%s:%08x%08x%08x%08x:%s",
6877 user_buf,
6878 domain_buf,
6879 srvchall_buf,
6880 digest_buf[0],
6881 digest_buf[1],
6882 digest_buf[2],
6883 digest_buf[3],
6884 clichall_buf);
6885 }
6886 else if (hash_mode == 5700)
6887 {
6888 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6889
6890 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6891 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6892 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6893 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6894 digest_buf[4] = byte_swap_32 (digest_buf[4]);
6895 digest_buf[5] = byte_swap_32 (digest_buf[5]);
6896 digest_buf[6] = byte_swap_32 (digest_buf[6]);
6897 digest_buf[7] = byte_swap_32 (digest_buf[7]);
6898
6899 memcpy (tmp_buf, digest_buf, 32);
6900
6901 base64_encode (int_to_itoa64, (const u8 *) tmp_buf, 32, (u8 *) ptr_plain);
6902
6903 ptr_plain[43] = 0;
6904
6905 snprintf (out_buf, len-1, "%s", ptr_plain);
6906 }
6907 else if (hash_mode == 5800)
6908 {
6909 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6910 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6911 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6912 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6913 digest_buf[4] = byte_swap_32 (digest_buf[4]);
6914
6915 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x",
6916 digest_buf[0],
6917 digest_buf[1],
6918 digest_buf[2],
6919 digest_buf[3],
6920 digest_buf[4]);
6921 }
6922 else if ((hash_mode >= 6200) && (hash_mode <= 6299))
6923 {
6924 snprintf (out_buf, len-1, "%s", hashfile);
6925 }
6926 else if (hash_mode == 6300)
6927 {
6928 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6929
6930 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6931 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6932 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6933 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6934
6935 md5crypt_encode ((unsigned char *) digest_buf, (unsigned char *) ptr_plain);
6936
6937 snprintf (out_buf, len-1, "{smd5}%s$%s", (char *) salt.salt_buf, (char *) ptr_plain);
6938 }
6939 else if (hash_mode == 6400)
6940 {
6941 sha256aix_encode ((unsigned char *) digest_buf, (unsigned char *) ptr_plain);
6942
6943 snprintf (out_buf, len-1, "{ssha256}%02d$%s$%s", salt.salt_sign[0], (char *) salt.salt_buf, (char *) ptr_plain);
6944 }
6945 else if (hash_mode == 6500)
6946 {
6947 sha512aix_encode ((unsigned char *) digest_buf64, (unsigned char *) ptr_plain);
6948
6949 snprintf (out_buf, len-1, "{ssha512}%02d$%s$%s", salt.salt_sign[0], (char *) salt.salt_buf, (char *) ptr_plain);
6950 }
6951 else if (hash_mode == 6600)
6952 {
6953 agilekey_t *agilekeys = (agilekey_t *) data.esalts_buf;
6954
6955 agilekey_t *agilekey = &agilekeys[salt_pos];
6956
6957 salt.salt_buf[0] = byte_swap_32 (salt.salt_buf[0]);
6958 salt.salt_buf[1] = byte_swap_32 (salt.salt_buf[1]);
6959
6960 uint buf_len = len - 1;
6961
6962 uint off = snprintf (out_buf, buf_len, "%d:%08x%08x:", salt.salt_iter + 1, salt.salt_buf[0], salt.salt_buf[1]);
6963 buf_len -= 22;
6964
6965 for (uint i = 0, j = off; i < 1040; i++, j += 2)
6966 {
6967 snprintf (out_buf + j, buf_len, "%02x", agilekey->cipher[i]);
6968
6969 buf_len -= 2;
6970 }
6971 }
6972 else if (hash_mode == 6700)
6973 {
6974 sha1aix_encode ((unsigned char *) digest_buf, (unsigned char *) ptr_plain);
6975
6976 snprintf (out_buf, len-1, "{ssha1}%02d$%s$%s", salt.salt_sign[0], (char *) salt.salt_buf, (char *) ptr_plain);
6977 }
6978 else if (hash_mode == 6800)
6979 {
6980 snprintf (out_buf, len-1, "%s", (char *) salt.salt_buf);
6981 }
6982 else if (hash_mode == 7100)
6983 {
6984 uint *ptr = digest_buf;
6985
6986 pbkdf2_sha512_t *pbkdf2_sha512s = (pbkdf2_sha512_t *) data.esalts_buf;
6987
6988 pbkdf2_sha512_t *pbkdf2_sha512 = &pbkdf2_sha512s[salt_pos];
6989
6990 uint esalt[8] = { 0 };
6991
6992 esalt[0] = byte_swap_32 (pbkdf2_sha512->salt_buf[0]);
6993 esalt[1] = byte_swap_32 (pbkdf2_sha512->salt_buf[1]);
6994 esalt[2] = byte_swap_32 (pbkdf2_sha512->salt_buf[2]);
6995 esalt[3] = byte_swap_32 (pbkdf2_sha512->salt_buf[3]);
6996 esalt[4] = byte_swap_32 (pbkdf2_sha512->salt_buf[4]);
6997 esalt[5] = byte_swap_32 (pbkdf2_sha512->salt_buf[5]);
6998 esalt[6] = byte_swap_32 (pbkdf2_sha512->salt_buf[6]);
6999 esalt[7] = byte_swap_32 (pbkdf2_sha512->salt_buf[7]);
7000
7001 snprintf (out_buf, len-1, "%s%i$%08x%08x%08x%08x%08x%08x%08x%08x$%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
7002 SIGNATURE_SHA512OSX,
7003 salt.salt_iter + 1,
7004 esalt[ 0], esalt[ 1],
7005 esalt[ 2], esalt[ 3],
7006 esalt[ 4], esalt[ 5],
7007 esalt[ 6], esalt[ 7],
7008 ptr [ 1], ptr [ 0],
7009 ptr [ 3], ptr [ 2],
7010 ptr [ 5], ptr [ 4],
7011 ptr [ 7], ptr [ 6],
7012 ptr [ 9], ptr [ 8],
7013 ptr [11], ptr [10],
7014 ptr [13], ptr [12],
7015 ptr [15], ptr [14]);
7016 }
7017 else if (hash_mode == 7200)
7018 {
7019 uint *ptr = digest_buf;
7020
7021 pbkdf2_sha512_t *pbkdf2_sha512s = (pbkdf2_sha512_t *) data.esalts_buf;
7022
7023 pbkdf2_sha512_t *pbkdf2_sha512 = &pbkdf2_sha512s[salt_pos];
7024
7025 uint len_used = 0;
7026
7027 snprintf (out_buf + len_used, len - len_used - 1, "%s%i.", SIGNATURE_SHA512GRUB, salt.salt_iter + 1);
7028
7029 len_used = strlen (out_buf);
7030
7031 unsigned char *salt_buf_ptr = (unsigned char *) pbkdf2_sha512->salt_buf;
7032
7033 for (uint i = 0; i < salt.salt_len; i++, len_used += 2)
7034 {
7035 snprintf (out_buf + len_used, len - len_used - 1, "%02x", salt_buf_ptr[i]);
7036 }
7037
7038 snprintf (out_buf + len_used, len - len_used - 1, ".%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
7039 ptr [ 1], ptr [ 0],
7040 ptr [ 3], ptr [ 2],
7041 ptr [ 5], ptr [ 4],
7042 ptr [ 7], ptr [ 6],
7043 ptr [ 9], ptr [ 8],
7044 ptr [11], ptr [10],
7045 ptr [13], ptr [12],
7046 ptr [15], ptr [14]);
7047 }
7048 else if (hash_mode == 7300)
7049 {
7050 rakp_t *rakps = (rakp_t *) data.esalts_buf;
7051
7052 rakp_t *rakp = &rakps[salt_pos];
7053
7054 for (uint i = 0, j = 0; (i * 4) < rakp->salt_len; i += 1, j += 8)
7055 {
7056 sprintf (out_buf + j, "%08x", rakp->salt_buf[i]);
7057 }
7058
7059 snprintf (out_buf + rakp->salt_len * 2, len - 1, ":%08x%08x%08x%08x%08x",
7060 digest_buf[0],
7061 digest_buf[1],
7062 digest_buf[2],
7063 digest_buf[3],
7064 digest_buf[4]);
7065 }
7066 else if (hash_mode == 7400)
7067 {
7068 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
7069
7070 digest_buf[0] = byte_swap_32 (digest_buf[0]);
7071 digest_buf[1] = byte_swap_32 (digest_buf[1]);
7072 digest_buf[2] = byte_swap_32 (digest_buf[2]);
7073 digest_buf[3] = byte_swap_32 (digest_buf[3]);
7074 digest_buf[4] = byte_swap_32 (digest_buf[4]);
7075 digest_buf[5] = byte_swap_32 (digest_buf[5]);
7076 digest_buf[6] = byte_swap_32 (digest_buf[6]);
7077 digest_buf[7] = byte_swap_32 (digest_buf[7]);
7078
7079 sha256crypt_encode ((unsigned char *) digest_buf, (unsigned char *) ptr_plain);
7080
7081 if (salt.salt_iter == ROUNDS_SHA256CRYPT)
7082 {
7083 snprintf (out_buf, len-1, "$5$%s$%s", (char *) salt.salt_buf, (char *) ptr_plain);
7084 }
7085 else
7086 {
7087 snprintf (out_buf, len-1, "$5$rounds=%i$%s$%s", salt.salt_iter, (char *) salt.salt_buf, (char *) ptr_plain);
7088 }
7089 }
7090 else if (hash_mode == 7500)
7091 {
7092 krb5pa_t *krb5pas = (krb5pa_t *) data.esalts_buf;
7093
7094 krb5pa_t *krb5pa = &krb5pas[salt_pos];
7095
7096 u8 *ptr_timestamp = (u8 *) krb5pa->timestamp;
7097 u8 *ptr_checksum = (u8 *) krb5pa->checksum;
7098
7099 char data[128] = { 0 };
7100
7101 char *ptr_data = data;
7102
7103 for (uint i = 0; i < 36; i++, ptr_data += 2)
7104 {
7105 sprintf (ptr_data, "%02x", ptr_timestamp[i]);
7106 }
7107
7108 for (uint i = 0; i < 16; i++, ptr_data += 2)
7109 {
7110 sprintf (ptr_data, "%02x", ptr_checksum[i]);
7111 }
7112
7113 *ptr_data = 0;
7114
7115 snprintf (out_buf, len-1, "%s$%s$%s$%s$%s",
7116 SIGNATURE_KRB5PA,
7117 (char *) krb5pa->user,
7118 (char *) krb5pa->realm,
7119 (char *) krb5pa->salt,
7120 data);
7121 }
7122 else if (hash_mode == 7700)
7123 {
7124 snprintf (out_buf, len-1, "%s$%08X%08X",
7125 (char *) salt.salt_buf,
7126 digest_buf[0],
7127 digest_buf[1]);
7128 }
7129 else if (hash_mode == 7800)
7130 {
7131 snprintf (out_buf, len-1, "%s$%08X%08X%08X%08X%08X",
7132 (char *) salt.salt_buf,
7133 digest_buf[0],
7134 digest_buf[1],
7135 digest_buf[2],
7136 digest_buf[3],
7137 digest_buf[4]);
7138 }
7139 else if (hash_mode == 7900)
7140 {
7141 drupal7_encode ((unsigned char *) digest_buf64, (unsigned char *) ptr_plain);
7142
7143 // ugly hack start
7144
7145 char *tmp = (char *) salt.salt_buf_pc;
7146
7147 ptr_plain[42] = tmp[0];
7148
7149 // ugly hack end
7150
7151 ptr_plain[43] = 0;
7152
7153 snprintf (out_buf, len-1, "%s%s%s", (char *) salt.salt_sign, (char *) salt.salt_buf, (char *) ptr_plain);
7154 }
7155 else if (hash_mode == 8000)
7156 {
7157 snprintf (out_buf, len-1, "0xc007%s%08x%08x%08x%08x%08x%08x%08x%08x",
7158 (unsigned char *) salt.salt_buf,
7159 digest_buf[0],
7160 digest_buf[1],
7161 digest_buf[2],
7162 digest_buf[3],
7163 digest_buf[4],
7164 digest_buf[5],
7165 digest_buf[6],
7166 digest_buf[7]);
7167 }
7168 else if (hash_mode == 8100)
7169 {
7170 salt.salt_buf[0] = byte_swap_32 (salt.salt_buf[0]);
7171 salt.salt_buf[1] = byte_swap_32 (salt.salt_buf[1]);
7172
7173 snprintf (out_buf, len-1, "1%s%08x%08x%08x%08x%08x",
7174 (unsigned char *) salt.salt_buf,
7175 digest_buf[0],
7176 digest_buf[1],
7177 digest_buf[2],
7178 digest_buf[3],
7179 digest_buf[4]);
7180 }
7181 else if (hash_mode == 8200)
7182 {
7183 cloudkey_t *cloudkeys = (cloudkey_t *) data.esalts_buf;
7184
7185 cloudkey_t *cloudkey = &cloudkeys[salt_pos];
7186
7187 char data_buf[4096] = { 0 };
7188
7189 for (int i = 0, j = 0; i < 512; i += 1, j += 8)
7190 {
7191 sprintf (data_buf + j, "%08x", cloudkey->data_buf[i]);
7192 }
7193
7194 data_buf[cloudkey->data_len * 2] = 0;
7195
7196 digest_buf[0] = byte_swap_32 (digest_buf[0]);
7197 digest_buf[1] = byte_swap_32 (digest_buf[1]);
7198 digest_buf[2] = byte_swap_32 (digest_buf[2]);
7199 digest_buf[3] = byte_swap_32 (digest_buf[3]);
7200 digest_buf[4] = byte_swap_32 (digest_buf[4]);
7201 digest_buf[5] = byte_swap_32 (digest_buf[5]);
7202 digest_buf[6] = byte_swap_32 (digest_buf[6]);
7203 digest_buf[7] = byte_swap_32 (digest_buf[7]);
7204
7205 salt.salt_buf[0] = byte_swap_32 (salt.salt_buf[0]);
7206 salt.salt_buf[1] = byte_swap_32 (salt.salt_buf[1]);
7207 salt.salt_buf[2] = byte_swap_32 (salt.salt_buf[2]);
7208 salt.salt_buf[3] = byte_swap_32 (salt.salt_buf[3]);
7209
7210 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x%08x%08x%08x:%08x%08x%08x%08x:%u:%s",
7211 digest_buf[0],
7212 digest_buf[1],
7213 digest_buf[2],
7214 digest_buf[3],
7215 digest_buf[4],
7216 digest_buf[5],
7217 digest_buf[6],
7218 digest_buf[7],
7219 salt.salt_buf[0],
7220 salt.salt_buf[1],
7221 salt.salt_buf[2],
7222 salt.salt_buf[3],
7223 salt.salt_iter + 1,
7224 data_buf);
7225 }
7226 else if (hash_mode == 8300)
7227 {
7228 char digest_buf_c[34] = { 0 };
7229
7230 digest_buf[0] = byte_swap_32 (digest_buf[0]);
7231 digest_buf[1] = byte_swap_32 (digest_buf[1]);
7232 digest_buf[2] = byte_swap_32 (digest_buf[2]);
7233 digest_buf[3] = byte_swap_32 (digest_buf[3]);
7234 digest_buf[4] = byte_swap_32 (digest_buf[4]);
7235
7236 base32_encode (int_to_itoa32, (const u8 *) digest_buf, 20, (u8 *) digest_buf_c);
7237
7238 digest_buf_c[32] = 0;
7239
7240 // domain
7241
7242 const uint salt_pc_len = salt.salt_buf_pc[7]; // what a hack
7243
7244 char domain_buf_c[33] = { 0 };
7245
7246 memcpy (domain_buf_c, (char *) salt.salt_buf_pc, salt_pc_len);
7247
7248 for (uint i = 0; i < salt_pc_len; i++)
7249 {
7250 const char next = domain_buf_c[i];
7251
7252 domain_buf_c[i] = '.';
7253
7254 i += next;
7255 }
7256
7257 domain_buf_c[salt_pc_len] = 0;
7258
7259 // final
7260
7261 snprintf (out_buf, len-1, "%s:%s:%s:%u", digest_buf_c, domain_buf_c, (char *) salt.salt_buf, salt.salt_iter);
7262 }
7263 else if (hash_mode == 8500)
7264 {
7265 snprintf (out_buf, len-1, "%s*%s*%08X%08X", SIGNATURE_RACF, (char *) salt.salt_buf, digest_buf[0], digest_buf[1]);
7266 }
7267 else if (hash_mode == 2612)
7268 {
7269 snprintf (out_buf, len-1, "%s%s$%08x%08x%08x%08x",
7270 SIGNATURE_PHPS,
7271 (char *) salt.salt_buf,
7272 digest_buf[0],
7273 digest_buf[1],
7274 digest_buf[2],
7275 digest_buf[3]);
7276 }
7277 else if (hash_mode == 3711)
7278 {
7279 char *salt_ptr = (char *) salt.salt_buf;
7280
7281 salt_ptr[salt.salt_len - 1] = 0;
7282
7283 snprintf (out_buf, len-1, "%s%s$%08x%08x%08x%08x",
7284 SIGNATURE_MEDIAWIKI_B,
7285 salt_ptr,
7286 digest_buf[0],
7287 digest_buf[1],
7288 digest_buf[2],
7289 digest_buf[3]);
7290 }
7291 else if (hash_mode == 8800)
7292 {
7293 androidfde_t *androidfdes = (androidfde_t *) data.esalts_buf;
7294
7295 androidfde_t *androidfde = &androidfdes[salt_pos];
7296
7297 char tmp[3073] = { 0 };
7298
7299 for (uint i = 0, j = 0; i < 384; i += 1, j += 8)
7300 {
7301 sprintf (tmp + j, "%08x", androidfde->data[i]);
7302 }
7303
7304 tmp[3072] = 0;
7305
7306 snprintf (out_buf, len-1, "%s16$%08x%08x%08x%08x$16$%08x%08x%08x%08x$%s",
7307 SIGNATURE_ANDROIDFDE,
7308 byte_swap_32 (salt.salt_buf[0]),
7309 byte_swap_32 (salt.salt_buf[1]),
7310 byte_swap_32 (salt.salt_buf[2]),
7311 byte_swap_32 (salt.salt_buf[3]),
7312 byte_swap_32 (digest_buf[0]),
7313 byte_swap_32 (digest_buf[1]),
7314 byte_swap_32 (digest_buf[2]),
7315 byte_swap_32 (digest_buf[3]),
7316 tmp);
7317 }
7318 else if (hash_mode == 8900)
7319 {
7320 uint N = salt.scrypt_N;
7321 uint r = salt.scrypt_r;
7322 uint p = salt.scrypt_p;
7323
7324 char base64_salt[32] = { 0 };
7325
7326 base64_encode (int_to_base64, (const u8 *) salt.salt_buf, salt.salt_len, (u8 *) base64_salt);
7327
7328 memset (tmp_buf, 0, 46);
7329
7330 digest_buf[0] = byte_swap_32 (digest_buf[0]);
7331 digest_buf[1] = byte_swap_32 (digest_buf[1]);
7332 digest_buf[2] = byte_swap_32 (digest_buf[2]);
7333 digest_buf[3] = byte_swap_32 (digest_buf[3]);
7334 digest_buf[4] = byte_swap_32 (digest_buf[4]);
7335 digest_buf[5] = byte_swap_32 (digest_buf[5]);
7336 digest_buf[6] = byte_swap_32 (digest_buf[6]);
7337 digest_buf[7] = byte_swap_32 (digest_buf[7]);
7338 digest_buf[8] = 0; // needed for base64_encode ()
7339
7340 base64_encode (int_to_base64, (const u8 *) digest_buf, 32, (u8 *) tmp_buf);
7341
7342 snprintf (out_buf, len-1, "%s:%i:%i:%i:%s:%s",
7343 SIGNATURE_SCRYPT,
7344 N,
7345 r,
7346 p,
7347 base64_salt,
7348 tmp_buf);
7349 }
7350 else if (hash_mode == 9000)
7351 {
7352 snprintf (out_buf, len-1, "%s", hashfile);
7353 }
7354 else if (hash_mode == 9200)
7355 {
7356 // salt
7357
7358 pbkdf2_sha256_t *pbkdf2_sha256s = (pbkdf2_sha256_t *) data.esalts_buf;
7359
7360 pbkdf2_sha256_t *pbkdf2_sha256 = &pbkdf2_sha256s[salt_pos];
7361
7362 unsigned char *salt_buf_ptr = (unsigned char *) pbkdf2_sha256->salt_buf;
7363
7364 // hash
7365
7366 digest_buf[0] = byte_swap_32 (digest_buf[0]);
7367 digest_buf[1] = byte_swap_32 (digest_buf[1]);
7368 digest_buf[2] = byte_swap_32 (digest_buf[2]);
7369 digest_buf[3] = byte_swap_32 (digest_buf[3]);
7370 digest_buf[4] = byte_swap_32 (digest_buf[4]);
7371 digest_buf[5] = byte_swap_32 (digest_buf[5]);
7372 digest_buf[6] = byte_swap_32 (digest_buf[6]);
7373 digest_buf[7] = byte_swap_32 (digest_buf[7]);
7374 digest_buf[8] = 0; // needed for base64_encode ()
7375
7376 char tmp_buf[64] = { 0 };
7377
7378 base64_encode (int_to_itoa64, (const u8 *) digest_buf, 32, (u8 *) tmp_buf);
7379 tmp_buf[43] = 0; // cut it here
7380
7381 // output
7382
7383 snprintf (out_buf, len-1, "%s%s$%s", SIGNATURE_CISCO8, salt_buf_ptr, tmp_buf);
7384 }
7385 else if (hash_mode == 9300)
7386 {
7387 digest_buf[0] = byte_swap_32 (digest_buf[0]);
7388 digest_buf[1] = byte_swap_32 (digest_buf[1]);
7389 digest_buf[2] = byte_swap_32 (digest_buf[2]);
7390 digest_buf[3] = byte_swap_32 (digest_buf[3]);
7391 digest_buf[4] = byte_swap_32 (digest_buf[4]);
7392 digest_buf[5] = byte_swap_32 (digest_buf[5]);
7393 digest_buf[6] = byte_swap_32 (digest_buf[6]);
7394 digest_buf[7] = byte_swap_32 (digest_buf[7]);
7395 digest_buf[8] = 0; // needed for base64_encode ()
7396
7397 char tmp_buf[64] = { 0 };
7398
7399 base64_encode (int_to_itoa64, (const u8 *) digest_buf, 32, (u8 *) tmp_buf);
7400 tmp_buf[43] = 0; // cut it here
7401
7402 unsigned char *salt_buf_ptr = (unsigned char *) salt.salt_buf;
7403
7404 snprintf (out_buf, len-1, "%s%s$%s", SIGNATURE_CISCO9, salt_buf_ptr, tmp_buf);
7405 }
7406 else if (hash_mode == 9400)
7407 {
7408 office2007_t *office2007s = (office2007_t *) data.esalts_buf;
7409
7410 office2007_t *office2007 = &office2007s[salt_pos];
7411
7412 snprintf (out_buf, len-1, "%s*%u*%u*%u*%u*%08x%08x%08x%08x*%08x%08x%08x%08x*%08x%08x%08x%08x%08x",
7413 SIGNATURE_OFFICE2007,
7414 2007,
7415 20,
7416 office2007->keySize,
7417 16,
7418 salt.salt_buf[0],
7419 salt.salt_buf[1],
7420 salt.salt_buf[2],
7421 salt.salt_buf[3],
7422 office2007->encryptedVerifier[0],
7423 office2007->encryptedVerifier[1],
7424 office2007->encryptedVerifier[2],
7425 office2007->encryptedVerifier[3],
7426 office2007->encryptedVerifierHash[0],
7427 office2007->encryptedVerifierHash[1],
7428 office2007->encryptedVerifierHash[2],
7429 office2007->encryptedVerifierHash[3],
7430 office2007->encryptedVerifierHash[4]);
7431 }
7432 else if (hash_mode == 9500)
7433 {
7434 office2010_t *office2010s = (office2010_t *) data.esalts_buf;
7435
7436 office2010_t *office2010 = &office2010s[salt_pos];
7437
7438 snprintf (out_buf, len-1, "%s*%u*%u*%u*%u*%08x%08x%08x%08x*%08x%08x%08x%08x*%08x%08x%08x%08x%08x%08x%08x%08x", SIGNATURE_OFFICE2010, 2010, 100000, 128, 16,
7439
7440 salt.salt_buf[0],
7441 salt.salt_buf[1],
7442 salt.salt_buf[2],
7443 salt.salt_buf[3],
7444 office2010->encryptedVerifier[0],
7445 office2010->encryptedVerifier[1],
7446 office2010->encryptedVerifier[2],
7447 office2010->encryptedVerifier[3],
7448 office2010->encryptedVerifierHash[0],
7449 office2010->encryptedVerifierHash[1],
7450 office2010->encryptedVerifierHash[2],
7451 office2010->encryptedVerifierHash[3],
7452 office2010->encryptedVerifierHash[4],
7453 office2010->encryptedVerifierHash[5],
7454 office2010->encryptedVerifierHash[6],
7455 office2010->encryptedVerifierHash[7]);
7456 }
7457 else if (hash_mode == 9600)
7458 {
7459 office2013_t *office2013s = (office2013_t *) data.esalts_buf;
7460
7461 office2013_t *office2013 = &office2013s[salt_pos];
7462
7463 snprintf (out_buf, len-1, "%s*%u*%u*%u*%u*%08x%08x%08x%08x*%08x%08x%08x%08x*%08x%08x%08x%08x%08x%08x%08x%08x", SIGNATURE_OFFICE2013, 2013, 100000, 256, 16,
7464
7465 salt.salt_buf[0],
7466 salt.salt_buf[1],
7467 salt.salt_buf[2],
7468 salt.salt_buf[3],
7469 office2013->encryptedVerifier[0],
7470 office2013->encryptedVerifier[1],
7471 office2013->encryptedVerifier[2],
7472 office2013->encryptedVerifier[3],
7473 office2013->encryptedVerifierHash[0],
7474 office2013->encryptedVerifierHash[1],
7475 office2013->encryptedVerifierHash[2],
7476 office2013->encryptedVerifierHash[3],
7477 office2013->encryptedVerifierHash[4],
7478 office2013->encryptedVerifierHash[5],
7479 office2013->encryptedVerifierHash[6],
7480 office2013->encryptedVerifierHash[7]);
7481 }
7482 else if (hash_mode == 9700)
7483 {
7484 oldoffice01_t *oldoffice01s = (oldoffice01_t *) data.esalts_buf;
7485
7486 oldoffice01_t *oldoffice01 = &oldoffice01s[salt_pos];
7487
7488 snprintf (out_buf, len-1, "%s*%08x%08x%08x%08x*%08x%08x%08x%08x*%08x%08x%08x%08x",
7489 (oldoffice01->version == 0) ? SIGNATURE_OLDOFFICE0 : SIGNATURE_OLDOFFICE1,
7490 byte_swap_32 (salt.salt_buf[0]),
7491 byte_swap_32 (salt.salt_buf[1]),
7492 byte_swap_32 (salt.salt_buf[2]),
7493 byte_swap_32 (salt.salt_buf[3]),
7494 byte_swap_32 (oldoffice01->encryptedVerifier[0]),
7495 byte_swap_32 (oldoffice01->encryptedVerifier[1]),
7496 byte_swap_32 (oldoffice01->encryptedVerifier[2]),
7497 byte_swap_32 (oldoffice01->encryptedVerifier[3]),
7498 byte_swap_32 (oldoffice01->encryptedVerifierHash[0]),
7499 byte_swap_32 (oldoffice01->encryptedVerifierHash[1]),
7500 byte_swap_32 (oldoffice01->encryptedVerifierHash[2]),
7501 byte_swap_32 (oldoffice01->encryptedVerifierHash[3]));
7502 }
7503 else if (hash_mode == 9710)
7504 {
7505 oldoffice01_t *oldoffice01s = (oldoffice01_t *) data.esalts_buf;
7506
7507 oldoffice01_t *oldoffice01 = &oldoffice01s[salt_pos];
7508
7509 snprintf (out_buf, len-1, "%s*%08x%08x%08x%08x*%08x%08x%08x%08x*%08x%08x%08x%08x",
7510 (oldoffice01->version == 0) ? SIGNATURE_OLDOFFICE0 : SIGNATURE_OLDOFFICE1,
7511 byte_swap_32 (salt.salt_buf[0]),
7512 byte_swap_32 (salt.salt_buf[1]),
7513 byte_swap_32 (salt.salt_buf[2]),
7514 byte_swap_32 (salt.salt_buf[3]),
7515 byte_swap_32 (oldoffice01->encryptedVerifier[0]),
7516 byte_swap_32 (oldoffice01->encryptedVerifier[1]),
7517 byte_swap_32 (oldoffice01->encryptedVerifier[2]),
7518 byte_swap_32 (oldoffice01->encryptedVerifier[3]),
7519 byte_swap_32 (oldoffice01->encryptedVerifierHash[0]),
7520 byte_swap_32 (oldoffice01->encryptedVerifierHash[1]),
7521 byte_swap_32 (oldoffice01->encryptedVerifierHash[2]),
7522 byte_swap_32 (oldoffice01->encryptedVerifierHash[3]));
7523 }
7524 else if (hash_mode == 9720)
7525 {
7526 oldoffice01_t *oldoffice01s = (oldoffice01_t *) data.esalts_buf;
7527
7528 oldoffice01_t *oldoffice01 = &oldoffice01s[salt_pos];
7529
7530 u8 *rc4key = (u8 *) oldoffice01->rc4key;
7531
7532 snprintf (out_buf, len-1, "%s*%08x%08x%08x%08x*%08x%08x%08x%08x*%08x%08x%08x%08x:%02x%02x%02x%02x%02x",
7533 (oldoffice01->version == 0) ? SIGNATURE_OLDOFFICE0 : SIGNATURE_OLDOFFICE1,
7534 byte_swap_32 (salt.salt_buf[0]),
7535 byte_swap_32 (salt.salt_buf[1]),
7536 byte_swap_32 (salt.salt_buf[2]),
7537 byte_swap_32 (salt.salt_buf[3]),
7538 byte_swap_32 (oldoffice01->encryptedVerifier[0]),
7539 byte_swap_32 (oldoffice01->encryptedVerifier[1]),
7540 byte_swap_32 (oldoffice01->encryptedVerifier[2]),
7541 byte_swap_32 (oldoffice01->encryptedVerifier[3]),
7542 byte_swap_32 (oldoffice01->encryptedVerifierHash[0]),
7543 byte_swap_32 (oldoffice01->encryptedVerifierHash[1]),
7544 byte_swap_32 (oldoffice01->encryptedVerifierHash[2]),
7545 byte_swap_32 (oldoffice01->encryptedVerifierHash[3]),
7546 rc4key[0],
7547 rc4key[1],
7548 rc4key[2],
7549 rc4key[3],
7550 rc4key[4]);
7551 }
7552 else if (hash_mode == 9800)
7553 {
7554 oldoffice34_t *oldoffice34s = (oldoffice34_t *) data.esalts_buf;
7555
7556 oldoffice34_t *oldoffice34 = &oldoffice34s[salt_pos];
7557
7558 snprintf (out_buf, len-1, "%s*%08x%08x%08x%08x*%08x%08x%08x%08x*%08x%08x%08x%08x%08x",
7559 (oldoffice34->version == 3) ? SIGNATURE_OLDOFFICE3 : SIGNATURE_OLDOFFICE4,
7560 salt.salt_buf[0],
7561 salt.salt_buf[1],
7562 salt.salt_buf[2],
7563 salt.salt_buf[3],
7564 byte_swap_32 (oldoffice34->encryptedVerifier[0]),
7565 byte_swap_32 (oldoffice34->encryptedVerifier[1]),
7566 byte_swap_32 (oldoffice34->encryptedVerifier[2]),
7567 byte_swap_32 (oldoffice34->encryptedVerifier[3]),
7568 byte_swap_32 (oldoffice34->encryptedVerifierHash[0]),
7569 byte_swap_32 (oldoffice34->encryptedVerifierHash[1]),
7570 byte_swap_32 (oldoffice34->encryptedVerifierHash[2]),
7571 byte_swap_32 (oldoffice34->encryptedVerifierHash[3]),
7572 byte_swap_32 (oldoffice34->encryptedVerifierHash[4]));
7573 }
7574 else if (hash_mode == 9810)
7575 {
7576 oldoffice34_t *oldoffice34s = (oldoffice34_t *) data.esalts_buf;
7577
7578 oldoffice34_t *oldoffice34 = &oldoffice34s[salt_pos];
7579
7580 snprintf (out_buf, len-1, "%s*%08x%08x%08x%08x*%08x%08x%08x%08x*%08x%08x%08x%08x%08x",
7581 (oldoffice34->version == 3) ? SIGNATURE_OLDOFFICE3 : SIGNATURE_OLDOFFICE4,
7582 salt.salt_buf[0],
7583 salt.salt_buf[1],
7584 salt.salt_buf[2],
7585 salt.salt_buf[3],
7586 byte_swap_32 (oldoffice34->encryptedVerifier[0]),
7587 byte_swap_32 (oldoffice34->encryptedVerifier[1]),
7588 byte_swap_32 (oldoffice34->encryptedVerifier[2]),
7589 byte_swap_32 (oldoffice34->encryptedVerifier[3]),
7590 byte_swap_32 (oldoffice34->encryptedVerifierHash[0]),
7591 byte_swap_32 (oldoffice34->encryptedVerifierHash[1]),
7592 byte_swap_32 (oldoffice34->encryptedVerifierHash[2]),
7593 byte_swap_32 (oldoffice34->encryptedVerifierHash[3]),
7594 byte_swap_32 (oldoffice34->encryptedVerifierHash[4]));
7595 }
7596 else if (hash_mode == 9820)
7597 {
7598 oldoffice34_t *oldoffice34s = (oldoffice34_t *) data.esalts_buf;
7599
7600 oldoffice34_t *oldoffice34 = &oldoffice34s[salt_pos];
7601
7602 u8 *rc4key = (u8 *) oldoffice34->rc4key;
7603
7604 snprintf (out_buf, len-1, "%s*%08x%08x%08x%08x*%08x%08x%08x%08x*%08x%08x%08x%08x%08x:%02x%02x%02x%02x%02x",
7605 (oldoffice34->version == 3) ? SIGNATURE_OLDOFFICE3 : SIGNATURE_OLDOFFICE4,
7606 salt.salt_buf[0],
7607 salt.salt_buf[1],
7608 salt.salt_buf[2],
7609 salt.salt_buf[3],
7610 byte_swap_32 (oldoffice34->encryptedVerifier[0]),
7611 byte_swap_32 (oldoffice34->encryptedVerifier[1]),
7612 byte_swap_32 (oldoffice34->encryptedVerifier[2]),
7613 byte_swap_32 (oldoffice34->encryptedVerifier[3]),
7614 byte_swap_32 (oldoffice34->encryptedVerifierHash[0]),
7615 byte_swap_32 (oldoffice34->encryptedVerifierHash[1]),
7616 byte_swap_32 (oldoffice34->encryptedVerifierHash[2]),
7617 byte_swap_32 (oldoffice34->encryptedVerifierHash[3]),
7618 byte_swap_32 (oldoffice34->encryptedVerifierHash[4]),
7619 rc4key[0],
7620 rc4key[1],
7621 rc4key[2],
7622 rc4key[3],
7623 rc4key[4]);
7624 }
7625 else if (hash_mode == 10000)
7626 {
7627 // salt
7628
7629 pbkdf2_sha256_t *pbkdf2_sha256s = (pbkdf2_sha256_t *) data.esalts_buf;
7630
7631 pbkdf2_sha256_t *pbkdf2_sha256 = &pbkdf2_sha256s[salt_pos];
7632
7633 unsigned char *salt_buf_ptr = (unsigned char *) pbkdf2_sha256->salt_buf;
7634
7635 // hash
7636
7637 digest_buf[0] = byte_swap_32 (digest_buf[0]);
7638 digest_buf[1] = byte_swap_32 (digest_buf[1]);
7639 digest_buf[2] = byte_swap_32 (digest_buf[2]);
7640 digest_buf[3] = byte_swap_32 (digest_buf[3]);
7641 digest_buf[4] = byte_swap_32 (digest_buf[4]);
7642 digest_buf[5] = byte_swap_32 (digest_buf[5]);
7643 digest_buf[6] = byte_swap_32 (digest_buf[6]);
7644 digest_buf[7] = byte_swap_32 (digest_buf[7]);
7645 digest_buf[8] = 0; // needed for base64_encode ()
7646
7647 char tmp_buf[64] = { 0 };
7648
7649 base64_encode (int_to_base64, (const u8 *) digest_buf, 32, (u8 *) tmp_buf);
7650
7651 // output
7652
7653 snprintf (out_buf, len-1, "%s%i$%s$%s", SIGNATURE_DJANGOPBKDF2, salt.salt_iter + 1, salt_buf_ptr, tmp_buf);
7654 }
7655 else if (hash_mode == 10100)
7656 {
7657 snprintf (out_buf, len-1, "%08x%08x:%u:%u:%08x%08x%08x%08x",
7658 digest_buf[0],
7659 digest_buf[1],
7660 2,
7661 4,
7662 byte_swap_32 (salt.salt_buf[0]),
7663 byte_swap_32 (salt.salt_buf[1]),
7664 byte_swap_32 (salt.salt_buf[2]),
7665 byte_swap_32 (salt.salt_buf[3]));
7666 }
7667 else if (hash_mode == 10200)
7668 {
7669 cram_md5_t *cram_md5s = (cram_md5_t *) data.esalts_buf;
7670
7671 cram_md5_t *cram_md5 = &cram_md5s[salt_pos];
7672
7673 // challenge
7674
7675 char challenge[100] = { 0 };
7676
7677 base64_encode (int_to_base64, (const u8 *) salt.salt_buf, salt.salt_len, (u8 *) challenge);
7678
7679 // response
7680
7681 char tmp_buf[100] = { 0 };
7682
7683 uint tmp_len = snprintf (tmp_buf, 100, "%s %08x%08x%08x%08x",
7684 (char *) cram_md5->user,
7685 digest_buf[0],
7686 digest_buf[1],
7687 digest_buf[2],
7688 digest_buf[3]);
7689
7690 char response[100] = { 0 };
7691
7692 base64_encode (int_to_base64, (const u8 *) tmp_buf, tmp_len, (u8 *) response);
7693
7694 snprintf (out_buf, len-1, "%s%s$%s", SIGNATURE_CRAM_MD5, challenge, response);
7695 }
7696 else if (hash_mode == 10300)
7697 {
7698 char tmp_buf[100] = { 0 };
7699
7700 memcpy (tmp_buf + 0, digest_buf, 20);
7701 memcpy (tmp_buf + 20, salt.salt_buf, salt.salt_len);
7702
7703 uint tmp_len = 20 + salt.salt_len;
7704
7705 // base64 encode it
7706
7707 char base64_encoded[100] = { 0 };
7708
7709 base64_encode (int_to_base64, (const u8 *) tmp_buf, tmp_len, (u8 *) base64_encoded);
7710
7711 snprintf (out_buf, len-1, "%s%i}%s", SIGNATURE_SAPH_SHA1, salt.salt_iter + 1, base64_encoded);
7712 }
7713 else if (hash_mode == 10400)
7714 {
7715 pdf_t *pdfs = (pdf_t *) data.esalts_buf;
7716
7717 pdf_t *pdf = &pdfs[salt_pos];
7718
7719 snprintf (out_buf, len-1, "$pdf$%d*%d*%d*%d*%d*%d*%08x%08x%08x%08x*%d*%08x%08x%08x%08x%08x%08x%08x%08x*%d*%08x%08x%08x%08x%08x%08x%08x%08x",
7720
7721 pdf->V,
7722 pdf->R,
7723 40,
7724 pdf->P,
7725 pdf->enc_md,
7726 pdf->id_len,
7727 byte_swap_32 (pdf->id_buf[0]),
7728 byte_swap_32 (pdf->id_buf[1]),
7729 byte_swap_32 (pdf->id_buf[2]),
7730 byte_swap_32 (pdf->id_buf[3]),
7731 pdf->u_len,
7732 byte_swap_32 (pdf->u_buf[0]),
7733 byte_swap_32 (pdf->u_buf[1]),
7734 byte_swap_32 (pdf->u_buf[2]),
7735 byte_swap_32 (pdf->u_buf[3]),
7736 byte_swap_32 (pdf->u_buf[4]),
7737 byte_swap_32 (pdf->u_buf[5]),
7738 byte_swap_32 (pdf->u_buf[6]),
7739 byte_swap_32 (pdf->u_buf[7]),
7740 pdf->o_len,
7741 byte_swap_32 (pdf->o_buf[0]),
7742 byte_swap_32 (pdf->o_buf[1]),
7743 byte_swap_32 (pdf->o_buf[2]),
7744 byte_swap_32 (pdf->o_buf[3]),
7745 byte_swap_32 (pdf->o_buf[4]),
7746 byte_swap_32 (pdf->o_buf[5]),
7747 byte_swap_32 (pdf->o_buf[6]),
7748 byte_swap_32 (pdf->o_buf[7])
7749 );
7750 }
7751 else if (hash_mode == 10410)
7752 {
7753 pdf_t *pdfs = (pdf_t *) data.esalts_buf;
7754
7755 pdf_t *pdf = &pdfs[salt_pos];
7756
7757 snprintf (out_buf, len-1, "$pdf$%d*%d*%d*%d*%d*%d*%08x%08x%08x%08x*%d*%08x%08x%08x%08x%08x%08x%08x%08x*%d*%08x%08x%08x%08x%08x%08x%08x%08x",
7758
7759 pdf->V,
7760 pdf->R,
7761 40,
7762 pdf->P,
7763 pdf->enc_md,
7764 pdf->id_len,
7765 byte_swap_32 (pdf->id_buf[0]),
7766 byte_swap_32 (pdf->id_buf[1]),
7767 byte_swap_32 (pdf->id_buf[2]),
7768 byte_swap_32 (pdf->id_buf[3]),
7769 pdf->u_len,
7770 byte_swap_32 (pdf->u_buf[0]),
7771 byte_swap_32 (pdf->u_buf[1]),
7772 byte_swap_32 (pdf->u_buf[2]),
7773 byte_swap_32 (pdf->u_buf[3]),
7774 byte_swap_32 (pdf->u_buf[4]),
7775 byte_swap_32 (pdf->u_buf[5]),
7776 byte_swap_32 (pdf->u_buf[6]),
7777 byte_swap_32 (pdf->u_buf[7]),
7778 pdf->o_len,
7779 byte_swap_32 (pdf->o_buf[0]),
7780 byte_swap_32 (pdf->o_buf[1]),
7781 byte_swap_32 (pdf->o_buf[2]),
7782 byte_swap_32 (pdf->o_buf[3]),
7783 byte_swap_32 (pdf->o_buf[4]),
7784 byte_swap_32 (pdf->o_buf[5]),
7785 byte_swap_32 (pdf->o_buf[6]),
7786 byte_swap_32 (pdf->o_buf[7])
7787 );
7788 }
7789 else if (hash_mode == 10420)
7790 {
7791 pdf_t *pdfs = (pdf_t *) data.esalts_buf;
7792
7793 pdf_t *pdf = &pdfs[salt_pos];
7794
7795 u8 *rc4key = (u8 *) pdf->rc4key;
7796
7797 snprintf (out_buf, len-1, "$pdf$%d*%d*%d*%d*%d*%d*%08x%08x%08x%08x*%d*%08x%08x%08x%08x%08x%08x%08x%08x*%d*%08x%08x%08x%08x%08x%08x%08x%08x:%02x%02x%02x%02x%02x",
7798
7799 pdf->V,
7800 pdf->R,
7801 40,
7802 pdf->P,
7803 pdf->enc_md,
7804 pdf->id_len,
7805 byte_swap_32 (pdf->id_buf[0]),
7806 byte_swap_32 (pdf->id_buf[1]),
7807 byte_swap_32 (pdf->id_buf[2]),
7808 byte_swap_32 (pdf->id_buf[3]),
7809 pdf->u_len,
7810 byte_swap_32 (pdf->u_buf[0]),
7811 byte_swap_32 (pdf->u_buf[1]),
7812 byte_swap_32 (pdf->u_buf[2]),
7813 byte_swap_32 (pdf->u_buf[3]),
7814 byte_swap_32 (pdf->u_buf[4]),
7815 byte_swap_32 (pdf->u_buf[5]),
7816 byte_swap_32 (pdf->u_buf[6]),
7817 byte_swap_32 (pdf->u_buf[7]),
7818 pdf->o_len,
7819 byte_swap_32 (pdf->o_buf[0]),
7820 byte_swap_32 (pdf->o_buf[1]),
7821 byte_swap_32 (pdf->o_buf[2]),
7822 byte_swap_32 (pdf->o_buf[3]),
7823 byte_swap_32 (pdf->o_buf[4]),
7824 byte_swap_32 (pdf->o_buf[5]),
7825 byte_swap_32 (pdf->o_buf[6]),
7826 byte_swap_32 (pdf->o_buf[7]),
7827 rc4key[0],
7828 rc4key[1],
7829 rc4key[2],
7830 rc4key[3],
7831 rc4key[4]
7832 );
7833 }
7834 else if (hash_mode == 10500)
7835 {
7836 pdf_t *pdfs = (pdf_t *) data.esalts_buf;
7837
7838 pdf_t *pdf = &pdfs[salt_pos];
7839
7840 if (pdf->id_len == 32)
7841 {
7842 snprintf (out_buf, len-1, "$pdf$%d*%d*%d*%d*%d*%d*%08x%08x%08x%08x%08x%08x%08x%08x*%d*%08x%08x%08x%08x%08x%08x%08x%08x*%d*%08x%08x%08x%08x%08x%08x%08x%08x",
7843
7844 pdf->V,
7845 pdf->R,
7846 128,
7847 pdf->P,
7848 pdf->enc_md,
7849 pdf->id_len,
7850 byte_swap_32 (pdf->id_buf[0]),
7851 byte_swap_32 (pdf->id_buf[1]),
7852 byte_swap_32 (pdf->id_buf[2]),
7853 byte_swap_32 (pdf->id_buf[3]),
7854 byte_swap_32 (pdf->id_buf[4]),
7855 byte_swap_32 (pdf->id_buf[5]),
7856 byte_swap_32 (pdf->id_buf[6]),
7857 byte_swap_32 (pdf->id_buf[7]),
7858 pdf->u_len,
7859 byte_swap_32 (pdf->u_buf[0]),
7860 byte_swap_32 (pdf->u_buf[1]),
7861 byte_swap_32 (pdf->u_buf[2]),
7862 byte_swap_32 (pdf->u_buf[3]),
7863 byte_swap_32 (pdf->u_buf[4]),
7864 byte_swap_32 (pdf->u_buf[5]),
7865 byte_swap_32 (pdf->u_buf[6]),
7866 byte_swap_32 (pdf->u_buf[7]),
7867 pdf->o_len,
7868 byte_swap_32 (pdf->o_buf[0]),
7869 byte_swap_32 (pdf->o_buf[1]),
7870 byte_swap_32 (pdf->o_buf[2]),
7871 byte_swap_32 (pdf->o_buf[3]),
7872 byte_swap_32 (pdf->o_buf[4]),
7873 byte_swap_32 (pdf->o_buf[5]),
7874 byte_swap_32 (pdf->o_buf[6]),
7875 byte_swap_32 (pdf->o_buf[7])
7876 );
7877 }
7878 else
7879 {
7880 snprintf (out_buf, len-1, "$pdf$%d*%d*%d*%d*%d*%d*%08x%08x%08x%08x*%d*%08x%08x%08x%08x%08x%08x%08x%08x*%d*%08x%08x%08x%08x%08x%08x%08x%08x",
7881
7882 pdf->V,
7883 pdf->R,
7884 128,
7885 pdf->P,
7886 pdf->enc_md,
7887 pdf->id_len,
7888 byte_swap_32 (pdf->id_buf[0]),
7889 byte_swap_32 (pdf->id_buf[1]),
7890 byte_swap_32 (pdf->id_buf[2]),
7891 byte_swap_32 (pdf->id_buf[3]),
7892 pdf->u_len,
7893 byte_swap_32 (pdf->u_buf[0]),
7894 byte_swap_32 (pdf->u_buf[1]),
7895 byte_swap_32 (pdf->u_buf[2]),
7896 byte_swap_32 (pdf->u_buf[3]),
7897 byte_swap_32 (pdf->u_buf[4]),
7898 byte_swap_32 (pdf->u_buf[5]),
7899 byte_swap_32 (pdf->u_buf[6]),
7900 byte_swap_32 (pdf->u_buf[7]),
7901 pdf->o_len,
7902 byte_swap_32 (pdf->o_buf[0]),
7903 byte_swap_32 (pdf->o_buf[1]),
7904 byte_swap_32 (pdf->o_buf[2]),
7905 byte_swap_32 (pdf->o_buf[3]),
7906 byte_swap_32 (pdf->o_buf[4]),
7907 byte_swap_32 (pdf->o_buf[5]),
7908 byte_swap_32 (pdf->o_buf[6]),
7909 byte_swap_32 (pdf->o_buf[7])
7910 );
7911 }
7912 }
7913 else if (hash_mode == 10600)
7914 {
7915 uint digest_idx = salt.digests_offset + digest_pos;
7916
7917 hashinfo_t **hashinfo_ptr = data.hash_info;
7918 char *hash_buf = hashinfo_ptr[digest_idx]->orighash;
7919
7920 snprintf (out_buf, len-1, "%s", hash_buf);
7921 }
7922 else if (hash_mode == 10700)
7923 {
7924 uint digest_idx = salt.digests_offset + digest_pos;
7925
7926 hashinfo_t **hashinfo_ptr = data.hash_info;
7927 char *hash_buf = hashinfo_ptr[digest_idx]->orighash;
7928
7929 snprintf (out_buf, len-1, "%s", hash_buf);
7930 }
7931 else if (hash_mode == 10900)
7932 {
7933 uint digest_idx = salt.digests_offset + digest_pos;
7934
7935 hashinfo_t **hashinfo_ptr = data.hash_info;
7936 char *hash_buf = hashinfo_ptr[digest_idx]->orighash;
7937
7938 snprintf (out_buf, len-1, "%s", hash_buf);
7939 }
7940 else if (hash_mode == 11100)
7941 {
7942 u32 salt_challenge = salt.salt_buf[0];
7943
7944 salt_challenge = byte_swap_32 (salt_challenge);
7945
7946 unsigned char *user_name = (unsigned char *) (salt.salt_buf + 1);
7947
7948 snprintf (out_buf, len-1, "%s%s*%08x*%08x%08x%08x%08x",
7949 SIGNATURE_POSTGRESQL_AUTH,
7950 user_name,
7951 salt_challenge,
7952 digest_buf[0],
7953 digest_buf[1],
7954 digest_buf[2],
7955 digest_buf[3]);
7956 }
7957 else if (hash_mode == 11200)
7958 {
7959 snprintf (out_buf, len-1, "%s%s*%08x%08x%08x%08x%08x",
7960 SIGNATURE_MYSQL_AUTH,
7961 (unsigned char *) salt.salt_buf,
7962 digest_buf[0],
7963 digest_buf[1],
7964 digest_buf[2],
7965 digest_buf[3],
7966 digest_buf[4]);
7967 }
7968 else if (hash_mode == 11300)
7969 {
7970 bitcoin_wallet_t *bitcoin_wallets = (bitcoin_wallet_t *) data.esalts_buf;
7971
7972 bitcoin_wallet_t *bitcoin_wallet = &bitcoin_wallets[salt_pos];
7973
7974 const uint cry_master_len = bitcoin_wallet->cry_master_len;
7975 const uint ckey_len = bitcoin_wallet->ckey_len;
7976 const uint public_key_len = bitcoin_wallet->public_key_len;
7977
7978 char *cry_master_buf = (char *) mymalloc ((cry_master_len * 2) + 1);
7979 char *ckey_buf = (char *) mymalloc ((ckey_len * 2) + 1);
7980 char *public_key_buf = (char *) mymalloc ((public_key_len * 2) + 1);
7981
7982 for (uint i = 0, j = 0; i < cry_master_len; i += 1, j += 2)
7983 {
7984 const u8 *ptr = (const u8 *) bitcoin_wallet->cry_master_buf;
7985
7986 sprintf (cry_master_buf + j, "%02x", ptr[i]);
7987 }
7988
7989 for (uint i = 0, j = 0; i < ckey_len; i += 1, j += 2)
7990 {
7991 const u8 *ptr = (const u8 *) bitcoin_wallet->ckey_buf;
7992
7993 sprintf (ckey_buf + j, "%02x", ptr[i]);
7994 }
7995
7996 for (uint i = 0, j = 0; i < public_key_len; i += 1, j += 2)
7997 {
7998 const u8 *ptr = (const u8 *) bitcoin_wallet->public_key_buf;
7999
8000 sprintf (public_key_buf + j, "%02x", ptr[i]);
8001 }
8002
8003 snprintf (out_buf, len-1, "%s%d$%s$%d$%s$%d$%d$%s$%d$%s",
8004 SIGNATURE_BITCOIN_WALLET,
8005 cry_master_len * 2,
8006 cry_master_buf,
8007 salt.salt_len,
8008 (unsigned char *) salt.salt_buf,
8009 salt.salt_iter + 1,
8010 ckey_len * 2,
8011 ckey_buf,
8012 public_key_len * 2,
8013 public_key_buf
8014 );
8015
8016 free (cry_master_buf);
8017 free (ckey_buf);
8018 free (public_key_buf);
8019 }
8020 else if (hash_mode == 11400)
8021 {
8022 uint digest_idx = salt.digests_offset + digest_pos;
8023
8024 hashinfo_t **hashinfo_ptr = data.hash_info;
8025 char *hash_buf = hashinfo_ptr[digest_idx]->orighash;
8026
8027 snprintf (out_buf, len-1, "%s", hash_buf);
8028 }
8029 else if (hash_mode == 11600)
8030 {
8031 seven_zip_t *seven_zips = (seven_zip_t *) data.esalts_buf;
8032
8033 seven_zip_t *seven_zip = &seven_zips[salt_pos];
8034
8035 const uint data_len = seven_zip->data_len;
8036
8037 char *data_buf = (char *) mymalloc ((data_len * 2) + 1);
8038
8039 for (uint i = 0, j = 0; i < data_len; i += 1, j += 2)
8040 {
8041 const u8 *ptr = (const u8 *) seven_zip->data_buf;
8042
8043 sprintf (data_buf + j, "%02x", ptr[i]);
8044 }
8045
8046 snprintf (out_buf, len-1, "%s%u$%u$%u$%s$%u$%08x%08x%08x%08x$%u$%u$%u$%s",
8047 SIGNATURE_SEVEN_ZIP,
8048 0,
8049 salt.salt_sign[0],
8050 0,
8051 (char *) seven_zip->salt_buf,
8052 seven_zip->iv_len,
8053 seven_zip->iv_buf[0],
8054 seven_zip->iv_buf[1],
8055 seven_zip->iv_buf[2],
8056 seven_zip->iv_buf[3],
8057 seven_zip->crc,
8058 seven_zip->data_len,
8059 seven_zip->unpack_size,
8060 data_buf);
8061
8062 free (data_buf);
8063 }
8064 else if (hash_mode == 11700)
8065 {
8066 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x%08x%08x%08x",
8067 digest_buf[0],
8068 digest_buf[1],
8069 digest_buf[2],
8070 digest_buf[3],
8071 digest_buf[4],
8072 digest_buf[5],
8073 digest_buf[6],
8074 digest_buf[7]);
8075 }
8076 else if (hash_mode == 11800)
8077 {
8078 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
8079 digest_buf[ 0],
8080 digest_buf[ 1],
8081 digest_buf[ 2],
8082 digest_buf[ 3],
8083 digest_buf[ 4],
8084 digest_buf[ 5],
8085 digest_buf[ 6],
8086 digest_buf[ 7],
8087 digest_buf[ 8],
8088 digest_buf[ 9],
8089 digest_buf[10],
8090 digest_buf[11],
8091 digest_buf[12],
8092 digest_buf[13],
8093 digest_buf[14],
8094 digest_buf[15]);
8095 }
8096 else if (hash_mode == 11900)
8097 {
8098 uint digest_idx = salt.digests_offset + digest_pos;
8099
8100 hashinfo_t **hashinfo_ptr = data.hash_info;
8101 char *hash_buf = hashinfo_ptr[digest_idx]->orighash;
8102
8103 snprintf (out_buf, len-1, "%s", hash_buf);
8104 }
8105 else if (hash_mode == 12000)
8106 {
8107 uint digest_idx = salt.digests_offset + digest_pos;
8108
8109 hashinfo_t **hashinfo_ptr = data.hash_info;
8110 char *hash_buf = hashinfo_ptr[digest_idx]->orighash;
8111
8112 snprintf (out_buf, len-1, "%s", hash_buf);
8113 }
8114 else if (hash_mode == 12100)
8115 {
8116 uint digest_idx = salt.digests_offset + digest_pos;
8117
8118 hashinfo_t **hashinfo_ptr = data.hash_info;
8119 char *hash_buf = hashinfo_ptr[digest_idx]->orighash;
8120
8121 snprintf (out_buf, len-1, "%s", hash_buf);
8122 }
8123 else if (hash_mode == 12200)
8124 {
8125 uint *ptr_digest = digest_buf;
8126 uint *ptr_salt = salt.salt_buf;
8127
8128 snprintf (out_buf, len-1, "%s0$1$%08x%08x$%08x%08x",
8129 SIGNATURE_ECRYPTFS,
8130 ptr_salt[0],
8131 ptr_salt[1],
8132 ptr_digest[0],
8133 ptr_digest[1]);
8134 }
8135 else if (hash_mode == 12300)
8136 {
8137 uint *ptr_digest = digest_buf;
8138 uint *ptr_salt = salt.salt_buf;
8139
8140 snprintf (out_buf, len-1, "%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X",
8141 ptr_digest[ 0], ptr_digest[ 1],
8142 ptr_digest[ 2], ptr_digest[ 3],
8143 ptr_digest[ 4], ptr_digest[ 5],
8144 ptr_digest[ 6], ptr_digest[ 7],
8145 ptr_digest[ 8], ptr_digest[ 9],
8146 ptr_digest[10], ptr_digest[11],
8147 ptr_digest[12], ptr_digest[13],
8148 ptr_digest[14], ptr_digest[15],
8149 ptr_salt[0],
8150 ptr_salt[1],
8151 ptr_salt[2],
8152 ptr_salt[3]);
8153 }
8154 else if (hash_mode == 12400)
8155 {
8156 // encode iteration count
8157
8158 char salt_iter[5] = { 0 };
8159
8160 salt_iter[0] = int_to_itoa64 ((salt.salt_iter ) & 0x3f);
8161 salt_iter[1] = int_to_itoa64 ((salt.salt_iter >> 6) & 0x3f);
8162 salt_iter[2] = int_to_itoa64 ((salt.salt_iter >> 12) & 0x3f);
8163 salt_iter[3] = int_to_itoa64 ((salt.salt_iter >> 18) & 0x3f);
8164 salt_iter[4] = 0;
8165
8166 // encode salt
8167
8168 ptr_salt[0] = int_to_itoa64 ((salt.salt_buf[0] ) & 0x3f);
8169 ptr_salt[1] = int_to_itoa64 ((salt.salt_buf[0] >> 6) & 0x3f);
8170 ptr_salt[2] = int_to_itoa64 ((salt.salt_buf[0] >> 12) & 0x3f);
8171 ptr_salt[3] = int_to_itoa64 ((salt.salt_buf[0] >> 18) & 0x3f);
8172 ptr_salt[4] = 0;
8173
8174 // encode digest
8175
8176 memset (tmp_buf, 0, sizeof (tmp_buf));
8177
8178 digest_buf[0] = byte_swap_32 (digest_buf[0]);
8179 digest_buf[1] = byte_swap_32 (digest_buf[1]);
8180
8181 memcpy (tmp_buf, digest_buf, 8);
8182
8183 base64_encode (int_to_itoa64, (const u8 *) tmp_buf, 8, (u8 *) ptr_plain);
8184
8185 ptr_plain[11] = 0;
8186
8187 // fill the resulting buffer
8188
8189 snprintf (out_buf, len - 1, "_%s%s%s", salt_iter, ptr_salt, ptr_plain);
8190 }
8191 else if (hash_mode == 12500)
8192 {
8193 snprintf (out_buf, len - 1, "%s*0*%08x%08x*%08x%08x%08x%08x",
8194 SIGNATURE_RAR3,
8195 byte_swap_32 (salt.salt_buf[0]),
8196 byte_swap_32 (salt.salt_buf[1]),
8197 salt.salt_buf[2],
8198 salt.salt_buf[3],
8199 salt.salt_buf[4],
8200 salt.salt_buf[5]);
8201 }
8202 else if (hash_mode == 12600)
8203 {
8204 snprintf (out_buf, len - 1, "%08x%08x%08x%08x%08x%08x%08x%08x",
8205 digest_buf[0] + salt.salt_buf_pc[0],
8206 digest_buf[1] + salt.salt_buf_pc[1],
8207 digest_buf[2] + salt.salt_buf_pc[2],
8208 digest_buf[3] + salt.salt_buf_pc[3],
8209 digest_buf[4] + salt.salt_buf_pc[4],
8210 digest_buf[5] + salt.salt_buf_pc[5],
8211 digest_buf[6] + salt.salt_buf_pc[6],
8212 digest_buf[7] + salt.salt_buf_pc[7]);
8213 }
8214 else if (hash_mode == 12700)
8215 {
8216 uint digest_idx = salt.digests_offset + digest_pos;
8217
8218 hashinfo_t **hashinfo_ptr = data.hash_info;
8219 char *hash_buf = hashinfo_ptr[digest_idx]->orighash;
8220
8221 snprintf (out_buf, len-1, "%s", hash_buf);
8222 }
8223 else if (hash_mode == 12800)
8224 {
8225 const u8 *ptr = (const u8 *) salt.salt_buf;
8226
8227 snprintf (out_buf, len-1, "%s,%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x,%d,%08x%08x%08x%08x%08x%08x%08x%08x",
8228 SIGNATURE_MS_DRSR,
8229 ptr[0],
8230 ptr[1],
8231 ptr[2],
8232 ptr[3],
8233 ptr[4],
8234 ptr[5],
8235 ptr[6],
8236 ptr[7],
8237 ptr[8],
8238 ptr[9],
8239 salt.salt_iter + 1,
8240 byte_swap_32 (digest_buf[0]),
8241 byte_swap_32 (digest_buf[1]),
8242 byte_swap_32 (digest_buf[2]),
8243 byte_swap_32 (digest_buf[3]),
8244 byte_swap_32 (digest_buf[4]),
8245 byte_swap_32 (digest_buf[5]),
8246 byte_swap_32 (digest_buf[6]),
8247 byte_swap_32 (digest_buf[7])
8248 );
8249 }
8250 else if (hash_mode == 12900)
8251 {
8252 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
8253 salt.salt_buf[ 4],
8254 salt.salt_buf[ 5],
8255 salt.salt_buf[ 6],
8256 salt.salt_buf[ 7],
8257 salt.salt_buf[ 8],
8258 salt.salt_buf[ 9],
8259 salt.salt_buf[10],
8260 salt.salt_buf[11],
8261 byte_swap_32 (digest_buf[0]),
8262 byte_swap_32 (digest_buf[1]),
8263 byte_swap_32 (digest_buf[2]),
8264 byte_swap_32 (digest_buf[3]),
8265 byte_swap_32 (digest_buf[4]),
8266 byte_swap_32 (digest_buf[5]),
8267 byte_swap_32 (digest_buf[6]),
8268 byte_swap_32 (digest_buf[7]),
8269 salt.salt_buf[ 0],
8270 salt.salt_buf[ 1],
8271 salt.salt_buf[ 2],
8272 salt.salt_buf[ 3]
8273 );
8274 }
8275 else if (hash_mode == 13000)
8276 {
8277 rar5_t *rar5s = (rar5_t *) data.esalts_buf;
8278
8279 rar5_t *rar5 = &rar5s[salt_pos];
8280
8281 snprintf (out_buf, len-1, "$rar5$16$%08x%08x%08x%08x$%u$%08x%08x%08x%08x$8$%08x%08x",
8282 salt.salt_buf[0],
8283 salt.salt_buf[1],
8284 salt.salt_buf[2],
8285 salt.salt_buf[3],
8286 salt.salt_sign[0],
8287 rar5->iv[0],
8288 rar5->iv[1],
8289 rar5->iv[2],
8290 rar5->iv[3],
8291 byte_swap_32 (digest_buf[0]),
8292 byte_swap_32 (digest_buf[1])
8293 );
8294 }
8295 else if (hash_mode == 13100)
8296 {
8297 krb5tgs_t *krb5tgss = (krb5tgs_t *) data.esalts_buf;
8298
8299 krb5tgs_t *krb5tgs = &krb5tgss[salt_pos];
8300
8301 u8 *ptr_checksum = (u8 *) krb5tgs->checksum;
8302 u8 *ptr_edata2 = (u8 *) krb5tgs->edata2;
8303
8304 char data[2560 * 4 * 2] = { 0 };
8305
8306 char *ptr_data = data;
8307
8308 for (uint i = 0; i < 16; i++, ptr_data += 2)
8309 sprintf (ptr_data, "%02x", ptr_checksum[i]);
8310
8311 /* skip '$' */
8312 ptr_data++;
8313
8314 for (uint i = 0; i < krb5tgs->edata2_len; i++, ptr_data += 2)
8315 sprintf (ptr_data, "%02x", ptr_edata2[i]);
8316
8317 snprintf (out_buf, len-1, "%s$%s$%s$%s",
8318 SIGNATURE_KRB5TGS,
8319 (char *) krb5tgs->account_info,
8320 data,
8321 data + 33);
8322 }
8323 else if (hash_mode == 13200)
8324 {
8325 snprintf (out_buf, len-1, "%s*%d*%08x%08x%08x%08x*%08x%08x%08x%08x%08x%08x",
8326 SIGNATURE_AXCRYPT,
8327 salt.salt_iter,
8328 salt.salt_buf[0],
8329 salt.salt_buf[1],
8330 salt.salt_buf[2],
8331 salt.salt_buf[3],
8332 salt.salt_buf[4],
8333 salt.salt_buf[5],
8334 salt.salt_buf[6],
8335 salt.salt_buf[7],
8336 salt.salt_buf[8],
8337 salt.salt_buf[9]);
8338 }
8339 else if (hash_mode == 13300)
8340 {
8341 snprintf (out_buf, len-1, "%s$%08x%08x%08x%08x",
8342 SIGNATURE_AXCRYPT_SHA1,
8343 digest_buf[0],
8344 digest_buf[1],
8345 digest_buf[2],
8346 digest_buf[3]);
8347 }
8348 else if (hash_mode == 13400)
8349 {
8350 keepass_t *keepasss = (keepass_t *) data.esalts_buf;
8351
8352 keepass_t *keepass = &keepasss[salt_pos];
8353
8354 u32 version = (u32) keepass->version;
8355 u32 rounds = salt.salt_iter;
8356 u32 algorithm = (u32) keepass->algorithm;
8357 u32 keyfile_len = (u32) keepass->keyfile_len;
8358
8359 u32 *ptr_final_random_seed = (u32 *) keepass->final_random_seed ;
8360 u32 *ptr_transf_random_seed = (u32 *) keepass->transf_random_seed ;
8361 u32 *ptr_enc_iv = (u32 *) keepass->enc_iv ;
8362 u32 *ptr_contents_hash = (u32 *) keepass->contents_hash ;
8363 u32 *ptr_keyfile = (u32 *) keepass->keyfile ;
8364
8365 /* specific to version 1 */
8366 u32 contents_len;
8367 u32 *ptr_contents;
8368
8369 /* specific to version 2 */
8370 u32 expected_bytes_len;
8371 u32 *ptr_expected_bytes;
8372
8373 u32 final_random_seed_len;
8374 u32 transf_random_seed_len;
8375 u32 enc_iv_len;
8376 u32 contents_hash_len;
8377
8378 transf_random_seed_len = 8;
8379 enc_iv_len = 4;
8380 contents_hash_len = 8;
8381 final_random_seed_len = 8;
8382
8383 if (version == 1)
8384 final_random_seed_len = 4;
8385
8386 snprintf (out_buf, len-1, "%s*%d*%d*%d",
8387 SIGNATURE_KEEPASS,
8388 version,
8389 rounds,
8390 algorithm);
8391
8392 char *ptr_data = out_buf;
8393
8394 ptr_data += strlen(out_buf);
8395
8396 *ptr_data = '*';
8397 ptr_data++;
8398
8399 for (uint i = 0; i < final_random_seed_len; i++, ptr_data += 8)
8400 sprintf (ptr_data, "%08x", ptr_final_random_seed[i]);
8401
8402 *ptr_data = '*';
8403 ptr_data++;
8404
8405 for (uint i = 0; i < transf_random_seed_len; i++, ptr_data += 8)
8406 sprintf (ptr_data, "%08x", ptr_transf_random_seed[i]);
8407
8408 *ptr_data = '*';
8409 ptr_data++;
8410
8411 for (uint i = 0; i < enc_iv_len; i++, ptr_data += 8)
8412 sprintf (ptr_data, "%08x", ptr_enc_iv[i]);
8413
8414 *ptr_data = '*';
8415 ptr_data++;
8416
8417 if (version == 1)
8418 {
8419 contents_len = (u32) keepass->contents_len;
8420 ptr_contents = (u32 *) keepass->contents;
8421
8422 for (uint i = 0; i < contents_hash_len; i++, ptr_data += 8)
8423 sprintf (ptr_data, "%08x", ptr_contents_hash[i]);
8424
8425 *ptr_data = '*';
8426 ptr_data++;
8427
8428 /* inline flag */
8429 *ptr_data = '1';
8430 ptr_data++;
8431
8432 *ptr_data = '*';
8433 ptr_data++;
8434
8435 char ptr_contents_len[10] = { 0 };
8436
8437 sprintf ((char*) ptr_contents_len, "%d", contents_len);
8438
8439 sprintf (ptr_data, "%d", contents_len);
8440
8441 ptr_data += strlen(ptr_contents_len);
8442
8443 *ptr_data = '*';
8444 ptr_data++;
8445
8446 for (uint i = 0; i < contents_len / 4; i++, ptr_data += 8)
8447 sprintf (ptr_data, "%08x", ptr_contents[i]);
8448 }
8449 else if (version == 2)
8450 {
8451 expected_bytes_len = 8;
8452 ptr_expected_bytes = (u32 *) keepass->expected_bytes ;
8453
8454 for (uint i = 0; i < expected_bytes_len; i++, ptr_data += 8)
8455 sprintf (ptr_data, "%08x", ptr_expected_bytes[i]);
8456
8457 *ptr_data = '*';
8458 ptr_data++;
8459
8460 for (uint i = 0; i < contents_hash_len; i++, ptr_data += 8)
8461 sprintf (ptr_data, "%08x", ptr_contents_hash[i]);
8462 }
8463 if (keyfile_len)
8464 {
8465 *ptr_data = '*';
8466 ptr_data++;
8467
8468 /* inline flag */
8469 *ptr_data = '1';
8470 ptr_data++;
8471
8472 *ptr_data = '*';
8473 ptr_data++;
8474
8475 sprintf (ptr_data, "%d", keyfile_len);
8476
8477 ptr_data += 2;
8478
8479 *ptr_data = '*';
8480 ptr_data++;
8481
8482 for (uint i = 0; i < 8; i++, ptr_data += 8)
8483 sprintf (ptr_data, "%08x", ptr_keyfile[i]);
8484 }
8485 }
8486 else if (hash_mode == 13500)
8487 {
8488 pstoken_t *pstokens = (pstoken_t *) data.esalts_buf;
8489
8490 pstoken_t *pstoken = &pstokens[salt_pos];
8491
8492 const u32 salt_len = (pstoken->salt_len > 512) ? 512 : pstoken->salt_len;
8493
8494 char pstoken_tmp[1024 + 1] = { 0 };
8495
8496 for (uint i = 0, j = 0; i < salt_len; i += 1, j += 2)
8497 {
8498 const u8 *ptr = (const u8 *) pstoken->salt_buf;
8499
8500 sprintf (pstoken_tmp + j, "%02x", ptr[i]);
8501 }
8502
8503 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x:%s",
8504 digest_buf[0],
8505 digest_buf[1],
8506 digest_buf[2],
8507 digest_buf[3],
8508 digest_buf[4],
8509 pstoken_tmp);
8510 }
8511 else if (hash_mode == 13600)
8512 {
8513 zip2_t *zip2s = (zip2_t *) data.esalts_buf;
8514
8515 zip2_t *zip2 = &zip2s[salt_pos];
8516
8517 const u32 salt_len = zip2->salt_len;
8518
8519 char salt_tmp[32 + 1] = { 0 };
8520
8521 for (uint i = 0, j = 0; i < salt_len; i += 1, j += 2)
8522 {
8523 const u8 *ptr = (const u8 *) zip2->salt_buf;
8524
8525 sprintf (salt_tmp + j, "%02x", ptr[i]);
8526 }
8527
8528 const u32 data_len = zip2->data_len;
8529
8530 char data_tmp[8192 + 1] = { 0 };
8531
8532 for (uint i = 0, j = 0; i < data_len; i += 1, j += 2)
8533 {
8534 const u8 *ptr = (const u8 *) zip2->data_buf;
8535
8536 sprintf (data_tmp + j, "%02x", ptr[i]);
8537 }
8538
8539 const u32 auth_len = zip2->auth_len;
8540
8541 char auth_tmp[20 + 1] = { 0 };
8542
8543 for (uint i = 0, j = 0; i < auth_len; i += 1, j += 2)
8544 {
8545 const u8 *ptr = (const u8 *) zip2->auth_buf;
8546
8547 sprintf (auth_tmp + j, "%02x", ptr[i]);
8548 }
8549
8550 snprintf (out_buf, 255, "%s*%u*%u*%u*%s*%x*%u*%s*%s*%s",
8551 SIGNATURE_ZIP2_START,
8552 zip2->type,
8553 zip2->mode,
8554 zip2->magic,
8555 salt_tmp,
8556 zip2->verify_bytes,
8557 zip2->compress_length,
8558 data_tmp,
8559 auth_tmp,
8560 SIGNATURE_ZIP2_STOP);
8561 }
8562 else
8563 {
8564 if (hash_type == HASH_TYPE_MD4)
8565 {
8566 snprintf (out_buf, 255, "%08x%08x%08x%08x",
8567 digest_buf[0],
8568 digest_buf[1],
8569 digest_buf[2],
8570 digest_buf[3]);
8571 }
8572 else if (hash_type == HASH_TYPE_MD5)
8573 {
8574 snprintf (out_buf, len-1, "%08x%08x%08x%08x",
8575 digest_buf[0],
8576 digest_buf[1],
8577 digest_buf[2],
8578 digest_buf[3]);
8579 }
8580 else if (hash_type == HASH_TYPE_SHA1)
8581 {
8582 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x",
8583 digest_buf[0],
8584 digest_buf[1],
8585 digest_buf[2],
8586 digest_buf[3],
8587 digest_buf[4]);
8588 }
8589 else if (hash_type == HASH_TYPE_SHA256)
8590 {
8591 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x%08x%08x%08x",
8592 digest_buf[0],
8593 digest_buf[1],
8594 digest_buf[2],
8595 digest_buf[3],
8596 digest_buf[4],
8597 digest_buf[5],
8598 digest_buf[6],
8599 digest_buf[7]);
8600 }
8601 else if (hash_type == HASH_TYPE_SHA384)
8602 {
8603 uint *ptr = digest_buf;
8604
8605 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
8606 ptr[ 1], ptr[ 0],
8607 ptr[ 3], ptr[ 2],
8608 ptr[ 5], ptr[ 4],
8609 ptr[ 7], ptr[ 6],
8610 ptr[ 9], ptr[ 8],
8611 ptr[11], ptr[10]);
8612 }
8613 else if (hash_type == HASH_TYPE_SHA512)
8614 {
8615 uint *ptr = digest_buf;
8616
8617 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
8618 ptr[ 1], ptr[ 0],
8619 ptr[ 3], ptr[ 2],
8620 ptr[ 5], ptr[ 4],
8621 ptr[ 7], ptr[ 6],
8622 ptr[ 9], ptr[ 8],
8623 ptr[11], ptr[10],
8624 ptr[13], ptr[12],
8625 ptr[15], ptr[14]);
8626 }
8627 else if (hash_type == HASH_TYPE_LM)
8628 {
8629 snprintf (out_buf, len-1, "%08x%08x",
8630 digest_buf[0],
8631 digest_buf[1]);
8632 }
8633 else if (hash_type == HASH_TYPE_ORACLEH)
8634 {
8635 snprintf (out_buf, len-1, "%08X%08X",
8636 digest_buf[0],
8637 digest_buf[1]);
8638 }
8639 else if (hash_type == HASH_TYPE_BCRYPT)
8640 {
8641 base64_encode (int_to_bf64, (const u8 *) salt.salt_buf, 16, (u8 *) tmp_buf + 0);
8642 base64_encode (int_to_bf64, (const u8 *) digest_buf, 23, (u8 *) tmp_buf + 22);
8643
8644 tmp_buf[22 + 31] = 0; // base64_encode wants to pad
8645
8646 snprintf (out_buf, len-1, "%s$%s", (char *) salt.salt_sign, tmp_buf);
8647 }
8648 else if (hash_type == HASH_TYPE_KECCAK)
8649 {
8650 uint *ptr = digest_buf;
8651
8652 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
8653 ptr[ 1], ptr[ 0],
8654 ptr[ 3], ptr[ 2],
8655 ptr[ 5], ptr[ 4],
8656 ptr[ 7], ptr[ 6],
8657 ptr[ 9], ptr[ 8],
8658 ptr[11], ptr[10],
8659 ptr[13], ptr[12],
8660 ptr[15], ptr[14],
8661 ptr[17], ptr[16],
8662 ptr[19], ptr[18],
8663 ptr[21], ptr[20],
8664 ptr[23], ptr[22],
8665 ptr[25], ptr[24],
8666 ptr[27], ptr[26],
8667 ptr[29], ptr[28],
8668 ptr[31], ptr[30],
8669 ptr[33], ptr[32],
8670 ptr[35], ptr[34],
8671 ptr[37], ptr[36],
8672 ptr[39], ptr[38],
8673 ptr[41], ptr[30],
8674 ptr[43], ptr[42],
8675 ptr[45], ptr[44],
8676 ptr[47], ptr[46],
8677 ptr[49], ptr[48]
8678 );
8679
8680 out_buf[salt.keccak_mdlen * 2] = 0;
8681 }
8682 else if (hash_type == HASH_TYPE_RIPEMD160)
8683 {
8684 snprintf (out_buf, 255, "%08x%08x%08x%08x%08x",
8685 digest_buf[0],
8686 digest_buf[1],
8687 digest_buf[2],
8688 digest_buf[3],
8689 digest_buf[4]);
8690 }
8691 else if (hash_type == HASH_TYPE_WHIRLPOOL)
8692 {
8693 digest_buf[ 0] = digest_buf[ 0];
8694 digest_buf[ 1] = digest_buf[ 1];
8695 digest_buf[ 2] = digest_buf[ 2];
8696 digest_buf[ 3] = digest_buf[ 3];
8697 digest_buf[ 4] = digest_buf[ 4];
8698 digest_buf[ 5] = digest_buf[ 5];
8699 digest_buf[ 6] = digest_buf[ 6];
8700 digest_buf[ 7] = digest_buf[ 7];
8701 digest_buf[ 8] = digest_buf[ 8];
8702 digest_buf[ 9] = digest_buf[ 9];
8703 digest_buf[10] = digest_buf[10];
8704 digest_buf[11] = digest_buf[11];
8705 digest_buf[12] = digest_buf[12];
8706 digest_buf[13] = digest_buf[13];
8707 digest_buf[14] = digest_buf[14];
8708 digest_buf[15] = digest_buf[15];
8709
8710 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
8711 digest_buf[ 0],
8712 digest_buf[ 1],
8713 digest_buf[ 2],
8714 digest_buf[ 3],
8715 digest_buf[ 4],
8716 digest_buf[ 5],
8717 digest_buf[ 6],
8718 digest_buf[ 7],
8719 digest_buf[ 8],
8720 digest_buf[ 9],
8721 digest_buf[10],
8722 digest_buf[11],
8723 digest_buf[12],
8724 digest_buf[13],
8725 digest_buf[14],
8726 digest_buf[15]);
8727 }
8728 else if (hash_type == HASH_TYPE_GOST)
8729 {
8730 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x%08x%08x%08x",
8731 digest_buf[0],
8732 digest_buf[1],
8733 digest_buf[2],
8734 digest_buf[3],
8735 digest_buf[4],
8736 digest_buf[5],
8737 digest_buf[6],
8738 digest_buf[7]);
8739 }
8740 else if (hash_type == HASH_TYPE_MYSQL)
8741 {
8742 snprintf (out_buf, len-1, "%08x%08x",
8743 digest_buf[0],
8744 digest_buf[1]);
8745 }
8746 else if (hash_type == HASH_TYPE_LOTUS5)
8747 {
8748 snprintf (out_buf, len-1, "%08x%08x%08x%08x",
8749 digest_buf[0],
8750 digest_buf[1],
8751 digest_buf[2],
8752 digest_buf[3]);
8753 }
8754 else if (hash_type == HASH_TYPE_LOTUS6)
8755 {
8756 digest_buf[ 0] = byte_swap_32 (digest_buf[ 0]);
8757 digest_buf[ 1] = byte_swap_32 (digest_buf[ 1]);
8758 digest_buf[ 2] = byte_swap_32 (digest_buf[ 2]);
8759 digest_buf[ 3] = byte_swap_32 (digest_buf[ 3]);
8760
8761 char buf[16] = { 0 };
8762
8763 memcpy (buf + 0, salt.salt_buf, 5);
8764 memcpy (buf + 5, digest_buf, 9);
8765
8766 buf[3] -= -4;
8767
8768 base64_encode (int_to_lotus64, (const u8 *) buf, 14, (u8 *) tmp_buf);
8769
8770 tmp_buf[18] = salt.salt_buf_pc[7];
8771 tmp_buf[19] = 0;
8772
8773 snprintf (out_buf, len-1, "(G%s)", tmp_buf);
8774 }
8775 else if (hash_type == HASH_TYPE_LOTUS8)
8776 {
8777 char buf[52] = { 0 };
8778
8779 // salt
8780
8781 memcpy (buf + 0, salt.salt_buf, 16);
8782
8783 buf[3] -= -4;
8784
8785 // iteration
8786
8787 snprintf (buf + 16, 11, "%010i", salt.salt_iter + 1);
8788
8789 // chars
8790
8791 buf[26] = salt.salt_buf_pc[0];
8792 buf[27] = salt.salt_buf_pc[1];
8793
8794 // digest
8795
8796 memcpy (buf + 28, digest_buf, 8);
8797
8798 base64_encode (int_to_lotus64, (const u8 *) buf, 36, (u8 *) tmp_buf);
8799
8800 tmp_buf[49] = 0;
8801
8802 snprintf (out_buf, len-1, "(H%s)", tmp_buf);
8803 }
8804 else if (hash_type == HASH_TYPE_CRC32)
8805 {
8806 snprintf (out_buf, len-1, "%08x", byte_swap_32 (digest_buf[0]));
8807 }
8808 }
8809
8810 if (salt_type == SALT_TYPE_INTERN)
8811 {
8812 size_t pos = strlen (out_buf);
8813
8814 out_buf[pos] = data.separator;
8815
8816 char *ptr = (char *) salt.salt_buf;
8817
8818 memcpy (out_buf + pos + 1, ptr, salt.salt_len);
8819
8820 out_buf[pos + 1 + salt.salt_len] = 0;
8821 }
8822 }
8823
8824 void to_hccap_t (hccap_t *hccap, uint salt_pos, uint digest_pos)
8825 {
8826 memset (hccap, 0, sizeof (hccap_t));
8827
8828 salt_t *salt = &data.salts_buf[salt_pos];
8829
8830 memcpy (hccap->essid, salt->salt_buf, salt->salt_len);
8831
8832 wpa_t *wpas = (wpa_t *) data.esalts_buf;
8833 wpa_t *wpa = &wpas[salt_pos];
8834
8835 hccap->keyver = wpa->keyver;
8836
8837 hccap->eapol_size = wpa->eapol_size;
8838
8839 if (wpa->keyver != 1)
8840 {
8841 uint eapol_tmp[64] = { 0 };
8842
8843 for (uint i = 0; i < 64; i++)
8844 {
8845 eapol_tmp[i] = byte_swap_32 (wpa->eapol[i]);
8846 }
8847
8848 memcpy (hccap->eapol, eapol_tmp, wpa->eapol_size);
8849 }
8850 else
8851 {
8852 memcpy (hccap->eapol, wpa->eapol, wpa->eapol_size);
8853 }
8854
8855 memcpy (hccap->mac1, wpa->orig_mac1, 6);
8856 memcpy (hccap->mac2, wpa->orig_mac2, 6);
8857 memcpy (hccap->nonce1, wpa->orig_nonce1, 32);
8858 memcpy (hccap->nonce2, wpa->orig_nonce2, 32);
8859
8860 char *digests_buf_ptr = (char *) data.digests_buf;
8861
8862 uint dgst_size = data.dgst_size;
8863
8864 uint *digest_ptr = (uint *) (digests_buf_ptr + (data.salts_buf[salt_pos].digests_offset * dgst_size) + (digest_pos * dgst_size));
8865
8866 if (wpa->keyver != 1)
8867 {
8868 uint digest_tmp[4] = { 0 };
8869
8870 digest_tmp[0] = byte_swap_32 (digest_ptr[0]);
8871 digest_tmp[1] = byte_swap_32 (digest_ptr[1]);
8872 digest_tmp[2] = byte_swap_32 (digest_ptr[2]);
8873 digest_tmp[3] = byte_swap_32 (digest_ptr[3]);
8874
8875 memcpy (hccap->keymic, digest_tmp, 16);
8876 }
8877 else
8878 {
8879 memcpy (hccap->keymic, digest_ptr, 16);
8880 }
8881 }
8882
8883 void SuspendThreads ()
8884 {
8885 if (data.devices_status == STATUS_RUNNING)
8886 {
8887 hc_timer_set (&data.timer_paused);
8888
8889 data.devices_status = STATUS_PAUSED;
8890
8891 log_info ("Paused");
8892 }
8893 }
8894
8895 void ResumeThreads ()
8896 {
8897 if (data.devices_status == STATUS_PAUSED)
8898 {
8899 double ms_paused;
8900
8901 hc_timer_get (data.timer_paused, ms_paused);
8902
8903 data.ms_paused += ms_paused;
8904
8905 data.devices_status = STATUS_RUNNING;
8906
8907 log_info ("Resumed");
8908 }
8909 }
8910
8911 void bypass ()
8912 {
8913 if (data.devices_status != STATUS_RUNNING) return;
8914
8915 data.devices_status = STATUS_BYPASS;
8916
8917 log_info ("Next dictionary / mask in queue selected, bypassing current one");
8918 }
8919
8920 void stop_at_checkpoint ()
8921 {
8922 if (data.devices_status != STATUS_STOP_AT_CHECKPOINT)
8923 {
8924 if (data.devices_status != STATUS_RUNNING) return;
8925 }
8926
8927 // this feature only makes sense if --restore-disable was not specified
8928
8929 if (data.restore_disable == 1)
8930 {
8931 log_info ("WARNING: this feature is disabled when --restore-disable was specified");
8932
8933 return;
8934 }
8935
8936 // check if monitoring of Restore Point updates should be enabled or disabled
8937
8938 if (data.devices_status != STATUS_STOP_AT_CHECKPOINT)
8939 {
8940 data.devices_status = STATUS_STOP_AT_CHECKPOINT;
8941
8942 // save the current restore point value
8943
8944 data.checkpoint_cur_words = get_lowest_words_done ();
8945
8946 log_info ("Checkpoint enabled: will quit at next Restore Point update");
8947 }
8948 else
8949 {
8950 data.devices_status = STATUS_RUNNING;
8951
8952 // reset the global value for checkpoint checks
8953
8954 data.checkpoint_cur_words = 0;
8955
8956 log_info ("Checkpoint disabled: Restore Point updates will no longer be monitored");
8957 }
8958 }
8959
8960 void myabort ()
8961 {
8962 if (data.devices_status == STATUS_INIT) return;
8963 if (data.devices_status == STATUS_STARTING) return;
8964
8965 data.devices_status = STATUS_ABORTED;
8966 }
8967
8968 void myquit ()
8969 {
8970 if (data.devices_status == STATUS_INIT) return;
8971 if (data.devices_status == STATUS_STARTING) return;
8972
8973 data.devices_status = STATUS_QUIT;
8974 }
8975
8976 void load_kernel (const char *kernel_file, int num_devices, size_t *kernel_lengths, const u8 **kernel_sources)
8977 {
8978 FILE *fp = fopen (kernel_file, "rb");
8979
8980 if (fp != NULL)
8981 {
8982 struct stat st;
8983
8984 memset (&st, 0, sizeof (st));
8985
8986 stat (kernel_file, &st);
8987
8988 u8 *buf = (u8 *) mymalloc (st.st_size + 1);
8989
8990 size_t num_read = fread (buf, sizeof (u8), st.st_size, fp);
8991
8992 if (num_read != (size_t) st.st_size)
8993 {
8994 log_error ("ERROR: %s: %s", kernel_file, strerror (errno));
8995
8996 exit (-1);
8997 }
8998
8999 fclose (fp);
9000
9001 buf[st.st_size] = 0;
9002
9003 for (int i = 0; i < num_devices; i++)
9004 {
9005 kernel_lengths[i] = (size_t) st.st_size;
9006
9007 kernel_sources[i] = buf;
9008 }
9009 }
9010 else
9011 {
9012 log_error ("ERROR: %s: %s", kernel_file, strerror (errno));
9013
9014 exit (-1);
9015 }
9016
9017 return;
9018 }
9019
9020 void writeProgramBin (char *dst, u8 *binary, size_t binary_size)
9021 {
9022 if (binary_size > 0)
9023 {
9024 FILE *fp = fopen (dst, "wb");
9025
9026 lock_file (fp);
9027 fwrite (binary, sizeof (u8), binary_size, fp);
9028
9029 fflush (fp);
9030 fclose (fp);
9031 }
9032 }
9033
9034 /**
9035 * restore
9036 */
9037
9038 restore_data_t *init_restore (int argc, char **argv)
9039 {
9040 restore_data_t *rd = (restore_data_t *) mymalloc (sizeof (restore_data_t));
9041
9042 if (data.restore_disable == 0)
9043 {
9044 FILE *fp = fopen (data.eff_restore_file, "rb");
9045
9046 if (fp)
9047 {
9048 size_t nread = fread (rd, sizeof (restore_data_t), 1, fp);
9049
9050 if (nread != 1)
9051 {
9052 log_error ("ERROR: cannot read %s", data.eff_restore_file);
9053
9054 exit (-1);
9055 }
9056
9057 fclose (fp);
9058
9059 if (rd->pid)
9060 {
9061 char *pidbin = (char *) mymalloc (HCBUFSIZ);
9062
9063 int pidbin_len = -1;
9064
9065 #ifdef _POSIX
9066 snprintf (pidbin, HCBUFSIZ - 1, "/proc/%d/cmdline", rd->pid);
9067
9068 FILE *fd = fopen (pidbin, "rb");
9069
9070 if (fd)
9071 {
9072 pidbin_len = fread (pidbin, 1, HCBUFSIZ, fd);
9073
9074 pidbin[pidbin_len] = 0;
9075
9076 fclose (fd);
9077
9078 char *argv0_r = strrchr (argv[0], '/');
9079
9080 char *pidbin_r = strrchr (pidbin, '/');
9081
9082 if (argv0_r == NULL) argv0_r = argv[0];
9083
9084 if (pidbin_r == NULL) pidbin_r = pidbin;
9085
9086 if (strcmp (argv0_r, pidbin_r) == 0)
9087 {
9088 log_error ("ERROR: already an instance %s running on pid %d", pidbin, rd->pid);
9089
9090 exit (-1);
9091 }
9092 }
9093
9094 #elif _WIN
9095 HANDLE hProcess = OpenProcess (PROCESS_ALL_ACCESS, FALSE, rd->pid);
9096
9097 char *pidbin2 = (char *) mymalloc (HCBUFSIZ);
9098
9099 int pidbin2_len = -1;
9100
9101 pidbin_len = GetModuleFileName (NULL, pidbin, HCBUFSIZ);
9102 pidbin2_len = GetModuleFileNameEx (hProcess, NULL, pidbin2, HCBUFSIZ);
9103
9104 pidbin[pidbin_len] = 0;
9105 pidbin2[pidbin2_len] = 0;
9106
9107 if (pidbin2_len)
9108 {
9109 if (strcmp (pidbin, pidbin2) == 0)
9110 {
9111 log_error ("ERROR: already an instance %s running on pid %d", pidbin2, rd->pid);
9112
9113 exit (-1);
9114 }
9115 }
9116
9117 myfree (pidbin2);
9118
9119 #endif
9120
9121 myfree (pidbin);
9122 }
9123
9124 if (rd->version_bin < RESTORE_MIN)
9125 {
9126 log_error ("ERROR: cannot use outdated %s. Please remove it.", data.eff_restore_file);
9127
9128 exit (-1);
9129 }
9130 }
9131 }
9132
9133 memset (rd, 0, sizeof (restore_data_t));
9134
9135 rd->version_bin = VERSION_BIN;
9136
9137 #ifdef _POSIX
9138 rd->pid = getpid ();
9139 #elif _WIN
9140 rd->pid = GetCurrentProcessId ();
9141 #endif
9142
9143 if (getcwd (rd->cwd, 255) == NULL)
9144 {
9145 myfree (rd);
9146
9147 return (NULL);
9148 }
9149
9150 rd->argc = argc;
9151 rd->argv = argv;
9152
9153 return (rd);
9154 }
9155
9156 void read_restore (const char *eff_restore_file, restore_data_t *rd)
9157 {
9158 FILE *fp = fopen (eff_restore_file, "rb");
9159
9160 if (fp == NULL)
9161 {
9162 log_error ("ERROR: restore file '%s': %s", eff_restore_file, strerror (errno));
9163
9164 exit (-1);
9165 }
9166
9167 if (fread (rd, sizeof (restore_data_t), 1, fp) != 1)
9168 {
9169 log_error ("ERROR: cannot read %s", eff_restore_file);
9170
9171 exit (-1);
9172 }
9173
9174 rd->argv = (char **) mycalloc (rd->argc, sizeof (char *));
9175
9176 char *buf = (char *) mymalloc (HCBUFSIZ);
9177
9178 for (uint i = 0; i < rd->argc; i++)
9179 {
9180 if (fgets (buf, HCBUFSIZ - 1, fp) == NULL)
9181 {
9182 log_error ("ERROR: cannot read %s", eff_restore_file);
9183
9184 exit (-1);
9185 }
9186
9187 size_t len = strlen (buf);
9188
9189 if (len) buf[len - 1] = 0;
9190
9191 rd->argv[i] = mystrdup (buf);
9192 }
9193
9194 myfree (buf);
9195
9196 fclose (fp);
9197
9198 log_info ("INFO: Changing current working directory to the path found within the .restore file: '%s'", rd->cwd);
9199
9200 if (chdir (rd->cwd))
9201 {
9202 log_error ("ERROR: The directory '%s' does not exist. It is needed to restore (--restore) the session.\n"
9203 " You could either create this directory (or link it) or update the .restore file using e.g. the analyze_hc_restore.pl tool:\n"
9204 " https://github.com/philsmd/analyze_hc_restore\n"
9205 " The directory must be relative to (or contain) all files/folders mentioned within the command line.", rd->cwd);
9206
9207 exit (-1);
9208 }
9209 }
9210
9211 u64 get_lowest_words_done ()
9212 {
9213 u64 words_cur = -1;
9214
9215 for (uint device_id = 0; device_id < data.devices_cnt; device_id++)
9216 {
9217 hc_device_param_t *device_param = &data.devices_param[device_id];
9218
9219 if (device_param->skipped) continue;
9220
9221 const u64 words_done = device_param->words_done;
9222
9223 if (words_done < words_cur) words_cur = words_done;
9224 }
9225
9226 // It's possible that a device's workload isn't finished right after a restore-case.
9227 // In that case, this function would return 0 and overwrite the real restore point
9228 // There's also data.words_cur which is set to rd->words_cur but it changes while
9229 // the attack is running therefore we should stick to rd->words_cur.
9230 // Note that -s influences rd->words_cur we should keep a close look on that.
9231
9232 if (words_cur < data.rd->words_cur) words_cur = data.rd->words_cur;
9233
9234 return words_cur;
9235 }
9236
9237 void write_restore (const char *new_restore_file, restore_data_t *rd)
9238 {
9239 u64 words_cur = get_lowest_words_done ();
9240
9241 rd->words_cur = words_cur;
9242
9243 FILE *fp = fopen (new_restore_file, "wb");
9244
9245 if (fp == NULL)
9246 {
9247 log_error ("ERROR: %s: %s", new_restore_file, strerror (errno));
9248
9249 exit (-1);
9250 }
9251
9252 if (setvbuf (fp, NULL, _IONBF, 0))
9253 {
9254 log_error ("ERROR: setvbuf file '%s': %s", new_restore_file, strerror (errno));
9255
9256 exit (-1);
9257 }
9258
9259 fwrite (rd, sizeof (restore_data_t), 1, fp);
9260
9261 for (uint i = 0; i < rd->argc; i++)
9262 {
9263 fprintf (fp, "%s", rd->argv[i]);
9264 fputc ('\n', fp);
9265 }
9266
9267 fflush (fp);
9268
9269 fsync (fileno (fp));
9270
9271 fclose (fp);
9272 }
9273
9274 void cycle_restore ()
9275 {
9276 const char *eff_restore_file = data.eff_restore_file;
9277 const char *new_restore_file = data.new_restore_file;
9278
9279 restore_data_t *rd = data.rd;
9280
9281 write_restore (new_restore_file, rd);
9282
9283 struct stat st;
9284
9285 memset (&st, 0, sizeof(st));
9286
9287 if (stat (eff_restore_file, &st) == 0)
9288 {
9289 if (unlink (eff_restore_file))
9290 {
9291 log_info ("WARN: unlink file '%s': %s", eff_restore_file, strerror (errno));
9292 }
9293 }
9294
9295 if (rename (new_restore_file, eff_restore_file))
9296 {
9297 log_info ("WARN: rename file '%s' to '%s': %s", new_restore_file, eff_restore_file, strerror (errno));
9298 }
9299 }
9300
9301 void check_checkpoint ()
9302 {
9303 // if (data.restore_disable == 1) break; (this is already implied by previous checks)
9304
9305 u64 words_cur = get_lowest_words_done ();
9306
9307 if (words_cur != data.checkpoint_cur_words)
9308 {
9309 myabort ();
9310 }
9311 }
9312
9313 /**
9314 * tuning db
9315 */
9316
9317 void tuning_db_destroy (tuning_db_t *tuning_db)
9318 {
9319 int i;
9320
9321 for (i = 0; i < tuning_db->alias_cnt; i++)
9322 {
9323 tuning_db_alias_t *alias = &tuning_db->alias_buf[i];
9324
9325 myfree (alias->device_name);
9326 myfree (alias->alias_name);
9327 }
9328
9329 for (i = 0; i < tuning_db->entry_cnt; i++)
9330 {
9331 tuning_db_entry_t *entry = &tuning_db->entry_buf[i];
9332
9333 myfree (entry->device_name);
9334 }
9335
9336 myfree (tuning_db->alias_buf);
9337 myfree (tuning_db->entry_buf);
9338
9339 myfree (tuning_db);
9340 }
9341
9342 tuning_db_t *tuning_db_alloc (FILE *fp)
9343 {
9344 tuning_db_t *tuning_db = (tuning_db_t *) mymalloc (sizeof (tuning_db_t));
9345
9346 int num_lines = count_lines (fp);
9347
9348 // a bit over-allocated
9349
9350 tuning_db->alias_buf = (tuning_db_alias_t *) mycalloc (num_lines + 1, sizeof (tuning_db_alias_t));
9351 tuning_db->alias_cnt = 0;
9352
9353 tuning_db->entry_buf = (tuning_db_entry_t *) mycalloc (num_lines + 1, sizeof (tuning_db_entry_t));
9354 tuning_db->entry_cnt = 0;
9355
9356 return tuning_db;
9357 }
9358
9359 tuning_db_t *tuning_db_init (const char *tuning_db_file)
9360 {
9361 FILE *fp = fopen (tuning_db_file, "rb");
9362
9363 if (fp == NULL)
9364 {
9365 log_error ("%s: %s", tuning_db_file, strerror (errno));
9366
9367 exit (-1);
9368 }
9369
9370 tuning_db_t *tuning_db = tuning_db_alloc (fp);
9371
9372 rewind (fp);
9373
9374 int line_num = 0;
9375
9376 char *buf = (char *) mymalloc (HCBUFSIZ);
9377
9378 while (!feof (fp))
9379 {
9380 char *line_buf = fgets (buf, HCBUFSIZ - 1, fp);
9381
9382 if (line_buf == NULL) break;
9383
9384 line_num++;
9385
9386 const int line_len = in_superchop (line_buf);
9387
9388 if (line_len == 0) continue;
9389
9390 if (line_buf[0] == '#') continue;
9391
9392 // start processing
9393
9394 char *token_ptr[7] = { NULL };
9395
9396 int token_cnt = 0;
9397
9398 char *next = strtok (line_buf, "\t ");
9399
9400 token_ptr[token_cnt] = next;
9401
9402 token_cnt++;
9403
9404 while ((next = strtok (NULL, "\t ")) != NULL)
9405 {
9406 token_ptr[token_cnt] = next;
9407
9408 token_cnt++;
9409 }
9410
9411 if (token_cnt == 2)
9412 {
9413 char *device_name = token_ptr[0];
9414 char *alias_name = token_ptr[1];
9415
9416 tuning_db_alias_t *alias = &tuning_db->alias_buf[tuning_db->alias_cnt];
9417
9418 alias->device_name = mystrdup (device_name);
9419 alias->alias_name = mystrdup (alias_name);
9420
9421 tuning_db->alias_cnt++;
9422 }
9423 else if (token_cnt == 6)
9424 {
9425 if ((token_ptr[1][0] != '0') &&
9426 (token_ptr[1][0] != '1') &&
9427 (token_ptr[1][0] != '3') &&
9428 (token_ptr[1][0] != '*'))
9429 {
9430 log_info ("WARNING: Tuning-db: Invalid attack_mode '%c' in Line '%u'", token_ptr[1][0], line_num);
9431
9432 continue;
9433 }
9434
9435 if ((token_ptr[3][0] != '1') &&
9436 (token_ptr[3][0] != '2') &&
9437 (token_ptr[3][0] != '4') &&
9438 (token_ptr[3][0] != '8') &&
9439 (token_ptr[3][0] != 'N'))
9440 {
9441 log_info ("WARNING: Tuning-db: Invalid vector_width '%c' in Line '%u'", token_ptr[3][0], line_num);
9442
9443 continue;
9444 }
9445
9446 char *device_name = token_ptr[0];
9447
9448 int attack_mode = -1;
9449 int hash_type = -1;
9450 int vector_width = -1;
9451 int kernel_accel = -1;
9452 int kernel_loops = -1;
9453
9454 if (token_ptr[1][0] != '*') attack_mode = atoi (token_ptr[1]);
9455 if (token_ptr[2][0] != '*') hash_type = atoi (token_ptr[2]);
9456 if (token_ptr[3][0] != 'N') vector_width = atoi (token_ptr[3]);
9457
9458 if (token_ptr[4][0] != 'A')
9459 {
9460 kernel_accel = atoi (token_ptr[4]);
9461
9462 if ((kernel_accel < 1) || (kernel_accel > 1024))
9463 {
9464 log_info ("WARNING: Tuning-db: Invalid kernel_accel '%d' in Line '%u'", kernel_accel, line_num);
9465
9466 continue;
9467 }
9468 }
9469 else
9470 {
9471 kernel_accel = 0;
9472 }
9473
9474 if (token_ptr[5][0] != 'A')
9475 {
9476 kernel_loops = atoi (token_ptr[5]);
9477
9478 if ((kernel_loops < 1) || (kernel_loops > 1024))
9479 {
9480 log_info ("WARNING: Tuning-db: Invalid kernel_loops '%d' in Line '%u'", kernel_loops, line_num);
9481
9482 continue;
9483 }
9484 }
9485 else
9486 {
9487 kernel_loops = 0;
9488 }
9489
9490 tuning_db_entry_t *entry = &tuning_db->entry_buf[tuning_db->entry_cnt];
9491
9492 entry->device_name = mystrdup (device_name);
9493 entry->attack_mode = attack_mode;
9494 entry->hash_type = hash_type;
9495 entry->vector_width = vector_width;
9496 entry->kernel_accel = kernel_accel;
9497 entry->kernel_loops = kernel_loops;
9498
9499 tuning_db->entry_cnt++;
9500 }
9501 else
9502 {
9503 log_info ("WARNING: Tuning-db: Invalid number of token in Line '%u'", line_num);
9504
9505 continue;
9506 }
9507 }
9508
9509 myfree (buf);
9510
9511 fclose (fp);
9512
9513 // todo: print loaded 'cnt' message
9514
9515 // sort the database
9516
9517 qsort (tuning_db->alias_buf, tuning_db->alias_cnt, sizeof (tuning_db_alias_t), sort_by_tuning_db_alias);
9518 qsort (tuning_db->entry_buf, tuning_db->entry_cnt, sizeof (tuning_db_entry_t), sort_by_tuning_db_entry);
9519
9520 return tuning_db;
9521 }
9522
9523 tuning_db_entry_t *tuning_db_search (tuning_db_t *tuning_db, hc_device_param_t *device_param, int attack_mode, int hash_type)
9524 {
9525 static tuning_db_entry_t s;
9526
9527 // first we need to convert all spaces in the device_name to underscore
9528
9529 char *device_name_nospace = strdup (device_param->device_name);
9530
9531 int device_name_length = strlen (device_name_nospace);
9532
9533 int i;
9534
9535 for (i = 0; i < device_name_length; i++)
9536 {
9537 if (device_name_nospace[i] == ' ') device_name_nospace[i] = '_';
9538 }
9539
9540 // find out if there's an alias configured
9541
9542 tuning_db_alias_t a;
9543
9544 a.device_name = device_name_nospace;
9545
9546 tuning_db_alias_t *alias = bsearch (&a, tuning_db->alias_buf, tuning_db->alias_cnt, sizeof (tuning_db_alias_t), sort_by_tuning_db_alias);
9547
9548 char *alias_name = (alias == NULL) ? NULL : alias->alias_name;
9549
9550 // attack-mode 6 and 7 are attack-mode 1 basically
9551
9552 if (attack_mode == 6) attack_mode = 1;
9553 if (attack_mode == 7) attack_mode = 1;
9554
9555 // bsearch is not ideal but fast enough
9556
9557 s.device_name = device_name_nospace;
9558 s.attack_mode = attack_mode;
9559 s.hash_type = hash_type;
9560
9561 tuning_db_entry_t *entry = NULL;
9562
9563 // this will produce all 2^3 combinations required
9564
9565 for (i = 0; i < 8; i++)
9566 {
9567 s.device_name = (i & 1) ? "*" : device_name_nospace;
9568 s.attack_mode = (i & 2) ? -1 : attack_mode;
9569 s.hash_type = (i & 4) ? -1 : hash_type;
9570
9571 entry = bsearch (&s, tuning_db->entry_buf, tuning_db->entry_cnt, sizeof (tuning_db_entry_t), sort_by_tuning_db_entry);
9572
9573 if (entry != NULL) break;
9574
9575 // in non-wildcard mode do some additional checks:
9576
9577 if ((i & 1) == 0)
9578 {
9579 // in case we have an alias-name
9580
9581 if (alias_name != NULL)
9582 {
9583 s.device_name = alias_name;
9584
9585 entry = bsearch (&s, tuning_db->entry_buf, tuning_db->entry_cnt, sizeof (tuning_db_entry_t), sort_by_tuning_db_entry);
9586
9587 if (entry != NULL) break;
9588 }
9589
9590 // or by device type
9591
9592 if (device_param->device_type & CL_DEVICE_TYPE_CPU)
9593 {
9594 s.device_name = "DEVICE_TYPE_CPU";
9595 }
9596 else if (device_param->device_type & CL_DEVICE_TYPE_GPU)
9597 {
9598 s.device_name = "DEVICE_TYPE_GPU";
9599 }
9600 else if (device_param->device_type & CL_DEVICE_TYPE_ACCELERATOR)
9601 {
9602 s.device_name = "DEVICE_TYPE_ACCELERATOR";
9603 }
9604
9605 entry = bsearch (&s, tuning_db->entry_buf, tuning_db->entry_cnt, sizeof (tuning_db_entry_t), sort_by_tuning_db_entry);
9606
9607 if (entry != NULL) break;
9608 }
9609 }
9610
9611 // free converted device_name
9612
9613 myfree (device_name_nospace);
9614
9615 return entry;
9616 }
9617
9618 /**
9619 * parser
9620 */
9621
9622 uint parse_and_store_salt (char *out, char *in, uint salt_len)
9623 {
9624 u8 tmp[256] = { 0 };
9625
9626 if (salt_len > sizeof (tmp))
9627 {
9628 return UINT_MAX;
9629 }
9630
9631 memcpy (tmp, in, salt_len);
9632
9633 if (data.opts_type & OPTS_TYPE_ST_HEX)
9634 {
9635 if ((salt_len % 2) == 0)
9636 {
9637 u32 new_salt_len = salt_len / 2;
9638
9639 for (uint i = 0, j = 0; i < new_salt_len; i += 1, j += 2)
9640 {
9641 u8 p0 = tmp[j + 0];
9642 u8 p1 = tmp[j + 1];
9643
9644 tmp[i] = hex_convert (p1) << 0;
9645 tmp[i] |= hex_convert (p0) << 4;
9646 }
9647
9648 salt_len = new_salt_len;
9649 }
9650 else
9651 {
9652 return UINT_MAX;
9653 }
9654 }
9655 else if (data.opts_type & OPTS_TYPE_ST_BASE64)
9656 {
9657 salt_len = base64_decode (base64_to_int, (const u8 *) in, salt_len, (u8 *) tmp);
9658 }
9659
9660 memset (tmp + salt_len, 0, sizeof (tmp) - salt_len);
9661
9662 if (data.opts_type & OPTS_TYPE_ST_UNICODE)
9663 {
9664 if (salt_len < 20)
9665 {
9666 u32 *tmp_uint = (u32 *) tmp;
9667
9668 tmp_uint[9] = ((tmp_uint[4] >> 8) & 0x00FF0000) | ((tmp_uint[4] >> 16) & 0x000000FF);
9669 tmp_uint[8] = ((tmp_uint[4] << 8) & 0x00FF0000) | ((tmp_uint[4] >> 0) & 0x000000FF);
9670 tmp_uint[7] = ((tmp_uint[3] >> 8) & 0x00FF0000) | ((tmp_uint[3] >> 16) & 0x000000FF);
9671 tmp_uint[6] = ((tmp_uint[3] << 8) & 0x00FF0000) | ((tmp_uint[3] >> 0) & 0x000000FF);
9672 tmp_uint[5] = ((tmp_uint[2] >> 8) & 0x00FF0000) | ((tmp_uint[2] >> 16) & 0x000000FF);
9673 tmp_uint[4] = ((tmp_uint[2] << 8) & 0x00FF0000) | ((tmp_uint[2] >> 0) & 0x000000FF);
9674 tmp_uint[3] = ((tmp_uint[1] >> 8) & 0x00FF0000) | ((tmp_uint[1] >> 16) & 0x000000FF);
9675 tmp_uint[2] = ((tmp_uint[1] << 8) & 0x00FF0000) | ((tmp_uint[1] >> 0) & 0x000000FF);
9676 tmp_uint[1] = ((tmp_uint[0] >> 8) & 0x00FF0000) | ((tmp_uint[0] >> 16) & 0x000000FF);
9677 tmp_uint[0] = ((tmp_uint[0] << 8) & 0x00FF0000) | ((tmp_uint[0] >> 0) & 0x000000FF);
9678
9679 salt_len = salt_len * 2;
9680 }
9681 else
9682 {
9683 return UINT_MAX;
9684 }
9685 }
9686
9687 if (data.opts_type & OPTS_TYPE_ST_LOWER)
9688 {
9689 lowercase (tmp, salt_len);
9690 }
9691
9692 if (data.opts_type & OPTS_TYPE_ST_UPPER)
9693 {
9694 uppercase (tmp, salt_len);
9695 }
9696
9697 u32 len = salt_len;
9698
9699 if (data.opts_type & OPTS_TYPE_ST_ADD80)
9700 {
9701 tmp[len++] = 0x80;
9702 }
9703
9704 if (data.opts_type & OPTS_TYPE_ST_ADD01)
9705 {
9706 tmp[len++] = 0x01;
9707 }
9708
9709 if (data.opts_type & OPTS_TYPE_ST_GENERATE_LE)
9710 {
9711 u32 *tmp_uint = (uint *) tmp;
9712
9713 u32 max = len / 4;
9714
9715 if (len % 4) max++;
9716
9717 for (u32 i = 0; i < max; i++)
9718 {
9719 tmp_uint[i] = byte_swap_32 (tmp_uint[i]);
9720 }
9721
9722 // Important: we may need to increase the length of memcpy since
9723 // we don't want to "loose" some swapped bytes (could happen if
9724 // they do not perfectly fit in the 4-byte blocks)
9725 // Memcpy does always copy the bytes in the BE order, but since
9726 // we swapped them, some important bytes could be in positions
9727 // we normally skip with the original len
9728
9729 if (len % 4) len += 4 - (len % 4);
9730 }
9731
9732 memcpy (out, tmp, len);
9733
9734 return (salt_len);
9735 }
9736
9737 int bcrypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
9738 {
9739 if ((input_len < DISPLAY_LEN_MIN_3200) || (input_len > DISPLAY_LEN_MAX_3200)) return (PARSER_GLOBAL_LENGTH);
9740
9741 if ((memcmp (SIGNATURE_BCRYPT1, input_buf, 4)) && (memcmp (SIGNATURE_BCRYPT2, input_buf, 4)) && (memcmp (SIGNATURE_BCRYPT3, input_buf, 4))) return (PARSER_SIGNATURE_UNMATCHED);
9742
9743 u32 *digest = (u32 *) hash_buf->digest;
9744
9745 salt_t *salt = hash_buf->salt;
9746
9747 memcpy ((char *) salt->salt_sign, input_buf, 6);
9748
9749 char *iter_pos = input_buf + 4;
9750
9751 salt->salt_iter = 1 << atoi (iter_pos);
9752
9753 char *salt_pos = strchr (iter_pos, '$');
9754
9755 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
9756
9757 salt_pos++;
9758
9759 uint salt_len = 16;
9760
9761 salt->salt_len = salt_len;
9762
9763 u8 tmp_buf[100] = { 0 };
9764
9765 base64_decode (bf64_to_int, (const u8 *) salt_pos, 22, tmp_buf);
9766
9767 char *salt_buf_ptr = (char *) salt->salt_buf;
9768
9769 memcpy (salt_buf_ptr, tmp_buf, 16);
9770
9771 salt->salt_buf[0] = byte_swap_32 (salt->salt_buf[0]);
9772 salt->salt_buf[1] = byte_swap_32 (salt->salt_buf[1]);
9773 salt->salt_buf[2] = byte_swap_32 (salt->salt_buf[2]);
9774 salt->salt_buf[3] = byte_swap_32 (salt->salt_buf[3]);
9775
9776 char *hash_pos = salt_pos + 22;
9777
9778 memset (tmp_buf, 0, sizeof (tmp_buf));
9779
9780 base64_decode (bf64_to_int, (const u8 *) hash_pos, 31, tmp_buf);
9781
9782 memcpy (digest, tmp_buf, 24);
9783
9784 digest[0] = byte_swap_32 (digest[0]);
9785 digest[1] = byte_swap_32 (digest[1]);
9786 digest[2] = byte_swap_32 (digest[2]);
9787 digest[3] = byte_swap_32 (digest[3]);
9788 digest[4] = byte_swap_32 (digest[4]);
9789 digest[5] = byte_swap_32 (digest[5]);
9790
9791 digest[5] &= ~0xff; // its just 23 not 24 !
9792
9793 return (PARSER_OK);
9794 }
9795
9796 int cisco4_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
9797 {
9798 if ((input_len < DISPLAY_LEN_MIN_5700) || (input_len > DISPLAY_LEN_MAX_5700)) return (PARSER_GLOBAL_LENGTH);
9799
9800 u32 *digest = (u32 *) hash_buf->digest;
9801
9802 u8 tmp_buf[100] = { 0 };
9803
9804 base64_decode (itoa64_to_int, (const u8 *) input_buf, 43, tmp_buf);
9805
9806 memcpy (digest, tmp_buf, 32);
9807
9808 digest[0] = byte_swap_32 (digest[0]);
9809 digest[1] = byte_swap_32 (digest[1]);
9810 digest[2] = byte_swap_32 (digest[2]);
9811 digest[3] = byte_swap_32 (digest[3]);
9812 digest[4] = byte_swap_32 (digest[4]);
9813 digest[5] = byte_swap_32 (digest[5]);
9814 digest[6] = byte_swap_32 (digest[6]);
9815 digest[7] = byte_swap_32 (digest[7]);
9816
9817 digest[0] -= SHA256M_A;
9818 digest[1] -= SHA256M_B;
9819 digest[2] -= SHA256M_C;
9820 digest[3] -= SHA256M_D;
9821 digest[4] -= SHA256M_E;
9822 digest[5] -= SHA256M_F;
9823 digest[6] -= SHA256M_G;
9824 digest[7] -= SHA256M_H;
9825
9826 return (PARSER_OK);
9827 }
9828
9829 int lm_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
9830 {
9831 if ((input_len < DISPLAY_LEN_MIN_3000) || (input_len > DISPLAY_LEN_MAX_3000)) return (PARSER_GLOBAL_LENGTH);
9832
9833 u32 *digest = (u32 *) hash_buf->digest;
9834
9835 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
9836 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
9837
9838 digest[0] = byte_swap_32 (digest[0]);
9839 digest[1] = byte_swap_32 (digest[1]);
9840
9841 uint tt;
9842
9843 IP (digest[0], digest[1], tt);
9844
9845 digest[0] = digest[0];
9846 digest[1] = digest[1];
9847 digest[2] = 0;
9848 digest[3] = 0;
9849
9850 return (PARSER_OK);
9851 }
9852
9853 int arubaos_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
9854 {
9855 if ((input_len < DISPLAY_LEN_MIN_125) || (input_len > DISPLAY_LEN_MAX_125)) return (PARSER_GLOBAL_LENGTH);
9856
9857 if ((input_buf[8] != '0') || (input_buf[9] != '1')) return (PARSER_SIGNATURE_UNMATCHED);
9858
9859 u32 *digest = (u32 *) hash_buf->digest;
9860
9861 salt_t *salt = hash_buf->salt;
9862
9863 char *hash_pos = input_buf + 10;
9864
9865 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
9866 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
9867 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
9868 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
9869 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
9870
9871 digest[0] -= SHA1M_A;
9872 digest[1] -= SHA1M_B;
9873 digest[2] -= SHA1M_C;
9874 digest[3] -= SHA1M_D;
9875 digest[4] -= SHA1M_E;
9876
9877 uint salt_len = 10;
9878
9879 char *salt_buf_ptr = (char *) salt->salt_buf;
9880
9881 salt_len = parse_and_store_salt (salt_buf_ptr, input_buf, salt_len);
9882
9883 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
9884
9885 salt->salt_len = salt_len;
9886
9887 return (PARSER_OK);
9888 }
9889
9890 int osx1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
9891 {
9892 if ((input_len < DISPLAY_LEN_MIN_122) || (input_len > DISPLAY_LEN_MAX_122)) return (PARSER_GLOBAL_LENGTH);
9893
9894 u32 *digest = (u32 *) hash_buf->digest;
9895
9896 salt_t *salt = hash_buf->salt;
9897
9898 char *hash_pos = input_buf + 8;
9899
9900 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
9901 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
9902 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
9903 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
9904 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
9905
9906 digest[0] -= SHA1M_A;
9907 digest[1] -= SHA1M_B;
9908 digest[2] -= SHA1M_C;
9909 digest[3] -= SHA1M_D;
9910 digest[4] -= SHA1M_E;
9911
9912 uint salt_len = 8;
9913
9914 char *salt_buf_ptr = (char *) salt->salt_buf;
9915
9916 salt_len = parse_and_store_salt (salt_buf_ptr, input_buf, salt_len);
9917
9918 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
9919
9920 salt->salt_len = salt_len;
9921
9922 return (PARSER_OK);
9923 }
9924
9925 int osx512_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
9926 {
9927 if ((input_len < DISPLAY_LEN_MIN_1722) || (input_len > DISPLAY_LEN_MAX_1722)) return (PARSER_GLOBAL_LENGTH);
9928
9929 u64 *digest = (u64 *) hash_buf->digest;
9930
9931 salt_t *salt = hash_buf->salt;
9932
9933 char *hash_pos = input_buf + 8;
9934
9935 digest[0] = hex_to_u64 ((const u8 *) &hash_pos[ 0]);
9936 digest[1] = hex_to_u64 ((const u8 *) &hash_pos[ 16]);
9937 digest[2] = hex_to_u64 ((const u8 *) &hash_pos[ 32]);
9938 digest[3] = hex_to_u64 ((const u8 *) &hash_pos[ 48]);
9939 digest[4] = hex_to_u64 ((const u8 *) &hash_pos[ 64]);
9940 digest[5] = hex_to_u64 ((const u8 *) &hash_pos[ 80]);
9941 digest[6] = hex_to_u64 ((const u8 *) &hash_pos[ 96]);
9942 digest[7] = hex_to_u64 ((const u8 *) &hash_pos[112]);
9943
9944 digest[0] -= SHA512M_A;
9945 digest[1] -= SHA512M_B;
9946 digest[2] -= SHA512M_C;
9947 digest[3] -= SHA512M_D;
9948 digest[4] -= SHA512M_E;
9949 digest[5] -= SHA512M_F;
9950 digest[6] -= SHA512M_G;
9951 digest[7] -= SHA512M_H;
9952
9953 uint salt_len = 8;
9954
9955 char *salt_buf_ptr = (char *) salt->salt_buf;
9956
9957 salt_len = parse_and_store_salt (salt_buf_ptr, input_buf, salt_len);
9958
9959 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
9960
9961 salt->salt_len = salt_len;
9962
9963 return (PARSER_OK);
9964 }
9965
9966 int osc_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
9967 {
9968 if (data.opts_type & OPTS_TYPE_ST_HEX)
9969 {
9970 if ((input_len < DISPLAY_LEN_MIN_21H) || (input_len > DISPLAY_LEN_MAX_21H)) return (PARSER_GLOBAL_LENGTH);
9971 }
9972 else
9973 {
9974 if ((input_len < DISPLAY_LEN_MIN_21) || (input_len > DISPLAY_LEN_MAX_21)) return (PARSER_GLOBAL_LENGTH);
9975 }
9976
9977 u32 *digest = (u32 *) hash_buf->digest;
9978
9979 salt_t *salt = hash_buf->salt;
9980
9981 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
9982 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
9983 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
9984 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
9985
9986 digest[0] = byte_swap_32 (digest[0]);
9987 digest[1] = byte_swap_32 (digest[1]);
9988 digest[2] = byte_swap_32 (digest[2]);
9989 digest[3] = byte_swap_32 (digest[3]);
9990
9991 digest[0] -= MD5M_A;
9992 digest[1] -= MD5M_B;
9993 digest[2] -= MD5M_C;
9994 digest[3] -= MD5M_D;
9995
9996 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
9997
9998 uint salt_len = input_len - 32 - 1;
9999
10000 char *salt_buf = input_buf + 32 + 1;
10001
10002 char *salt_buf_ptr = (char *) salt->salt_buf;
10003
10004 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
10005
10006 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
10007
10008 salt->salt_len = salt_len;
10009
10010 return (PARSER_OK);
10011 }
10012
10013 int netscreen_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10014 {
10015 if (data.opts_type & OPTS_TYPE_ST_HEX)
10016 {
10017 if ((input_len < DISPLAY_LEN_MIN_22H) || (input_len > DISPLAY_LEN_MAX_22H)) return (PARSER_GLOBAL_LENGTH);
10018 }
10019 else
10020 {
10021 if ((input_len < DISPLAY_LEN_MIN_22) || (input_len > DISPLAY_LEN_MAX_22)) return (PARSER_GLOBAL_LENGTH);
10022 }
10023
10024 // unscramble
10025
10026 char clean_input_buf[32] = { 0 };
10027
10028 char sig[6] = { 'n', 'r', 'c', 's', 't', 'n' };
10029 int pos[6] = { 0, 6, 12, 17, 23, 29 };
10030
10031 for (int i = 0, j = 0, k = 0; i < 30; i++)
10032 {
10033 if (i == pos[j])
10034 {
10035 if (sig[j] != input_buf[i]) return (PARSER_SIGNATURE_UNMATCHED);
10036
10037 j++;
10038 }
10039 else
10040 {
10041 clean_input_buf[k] = input_buf[i];
10042
10043 k++;
10044 }
10045 }
10046
10047 // base64 decode
10048
10049 u32 *digest = (u32 *) hash_buf->digest;
10050
10051 salt_t *salt = hash_buf->salt;
10052
10053 u32 a, b, c, d, e, f;
10054
10055 a = base64_to_int (clean_input_buf[ 0] & 0x7f);
10056 b = base64_to_int (clean_input_buf[ 1] & 0x7f);
10057 c = base64_to_int (clean_input_buf[ 2] & 0x7f);
10058 d = base64_to_int (clean_input_buf[ 3] & 0x7f);
10059 e = base64_to_int (clean_input_buf[ 4] & 0x7f);
10060 f = base64_to_int (clean_input_buf[ 5] & 0x7f);
10061
10062 digest[0] = (((a << 12) | (b << 6) | (c)) << 16)
10063 | (((d << 12) | (e << 6) | (f)) << 0);
10064
10065 a = base64_to_int (clean_input_buf[ 6] & 0x7f);
10066 b = base64_to_int (clean_input_buf[ 7] & 0x7f);
10067 c = base64_to_int (clean_input_buf[ 8] & 0x7f);
10068 d = base64_to_int (clean_input_buf[ 9] & 0x7f);
10069 e = base64_to_int (clean_input_buf[10] & 0x7f);
10070 f = base64_to_int (clean_input_buf[11] & 0x7f);
10071
10072 digest[1] = (((a << 12) | (b << 6) | (c)) << 16)
10073 | (((d << 12) | (e << 6) | (f)) << 0);
10074
10075 a = base64_to_int (clean_input_buf[12] & 0x7f);
10076 b = base64_to_int (clean_input_buf[13] & 0x7f);
10077 c = base64_to_int (clean_input_buf[14] & 0x7f);
10078 d = base64_to_int (clean_input_buf[15] & 0x7f);
10079 e = base64_to_int (clean_input_buf[16] & 0x7f);
10080 f = base64_to_int (clean_input_buf[17] & 0x7f);
10081
10082 digest[2] = (((a << 12) | (b << 6) | (c)) << 16)
10083 | (((d << 12) | (e << 6) | (f)) << 0);
10084
10085 a = base64_to_int (clean_input_buf[18] & 0x7f);
10086 b = base64_to_int (clean_input_buf[19] & 0x7f);
10087 c = base64_to_int (clean_input_buf[20] & 0x7f);
10088 d = base64_to_int (clean_input_buf[21] & 0x7f);
10089 e = base64_to_int (clean_input_buf[22] & 0x7f);
10090 f = base64_to_int (clean_input_buf[23] & 0x7f);
10091
10092 digest[3] = (((a << 12) | (b << 6) | (c)) << 16)
10093 | (((d << 12) | (e << 6) | (f)) << 0);
10094
10095 digest[0] = byte_swap_32 (digest[0]);
10096 digest[1] = byte_swap_32 (digest[1]);
10097 digest[2] = byte_swap_32 (digest[2]);
10098 digest[3] = byte_swap_32 (digest[3]);
10099
10100 digest[0] -= MD5M_A;
10101 digest[1] -= MD5M_B;
10102 digest[2] -= MD5M_C;
10103 digest[3] -= MD5M_D;
10104
10105 if (input_buf[30] != ':') return (PARSER_SEPARATOR_UNMATCHED);
10106
10107 uint salt_len = input_len - 30 - 1;
10108
10109 char *salt_buf = input_buf + 30 + 1;
10110
10111 char *salt_buf_ptr = (char *) salt->salt_buf;
10112
10113 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
10114
10115 // max. salt length: 55 (max for MD5) - 22 (":Administration Tools:") - 1 (0x80) = 32
10116 // 32 - 4 bytes (to fit w0lr for all attack modes) = 28
10117
10118 if (salt_len > 28) return (PARSER_SALT_LENGTH);
10119
10120 salt->salt_len = salt_len;
10121
10122 memcpy (salt_buf_ptr + salt_len, ":Administration Tools:", 22);
10123
10124 salt->salt_len += 22;
10125
10126 return (PARSER_OK);
10127 }
10128
10129 int smf_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10130 {
10131 if (data.opts_type & OPTS_TYPE_ST_HEX)
10132 {
10133 if ((input_len < DISPLAY_LEN_MIN_121H) || (input_len > DISPLAY_LEN_MAX_121H)) return (PARSER_GLOBAL_LENGTH);
10134 }
10135 else
10136 {
10137 if ((input_len < DISPLAY_LEN_MIN_121) || (input_len > DISPLAY_LEN_MAX_121)) return (PARSER_GLOBAL_LENGTH);
10138 }
10139
10140 u32 *digest = (u32 *) hash_buf->digest;
10141
10142 salt_t *salt = hash_buf->salt;
10143
10144 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
10145 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
10146 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
10147 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
10148 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
10149
10150 digest[0] -= SHA1M_A;
10151 digest[1] -= SHA1M_B;
10152 digest[2] -= SHA1M_C;
10153 digest[3] -= SHA1M_D;
10154 digest[4] -= SHA1M_E;
10155
10156 if (input_buf[40] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
10157
10158 uint salt_len = input_len - 40 - 1;
10159
10160 char *salt_buf = input_buf + 40 + 1;
10161
10162 char *salt_buf_ptr = (char *) salt->salt_buf;
10163
10164 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
10165
10166 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
10167
10168 salt->salt_len = salt_len;
10169
10170 return (PARSER_OK);
10171 }
10172
10173 int dcc2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10174 {
10175 if (data.opts_type & OPTS_TYPE_ST_HEX)
10176 {
10177 if ((input_len < DISPLAY_LEN_MIN_2100H) || (input_len > DISPLAY_LEN_MAX_2100H)) return (PARSER_GLOBAL_LENGTH);
10178 }
10179 else
10180 {
10181 if ((input_len < DISPLAY_LEN_MIN_2100) || (input_len > DISPLAY_LEN_MAX_2100)) return (PARSER_GLOBAL_LENGTH);
10182 }
10183
10184 if (memcmp (SIGNATURE_DCC2, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
10185
10186 char *iter_pos = input_buf + 6;
10187
10188 salt_t *salt = hash_buf->salt;
10189
10190 uint iter = atoi (iter_pos);
10191
10192 if (iter < 1)
10193 {
10194 iter = ROUNDS_DCC2;
10195 }
10196
10197 salt->salt_iter = iter - 1;
10198
10199 char *salt_pos = strchr (iter_pos, '#');
10200
10201 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
10202
10203 salt_pos++;
10204
10205 char *digest_pos = strchr (salt_pos, '#');
10206
10207 if (digest_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
10208
10209 digest_pos++;
10210
10211 uint salt_len = digest_pos - salt_pos - 1;
10212
10213 u32 *digest = (u32 *) hash_buf->digest;
10214
10215 digest[0] = hex_to_u32 ((const u8 *) &digest_pos[ 0]);
10216 digest[1] = hex_to_u32 ((const u8 *) &digest_pos[ 8]);
10217 digest[2] = hex_to_u32 ((const u8 *) &digest_pos[16]);
10218 digest[3] = hex_to_u32 ((const u8 *) &digest_pos[24]);
10219
10220 char *salt_buf_ptr = (char *) salt->salt_buf;
10221
10222 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
10223
10224 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
10225
10226 salt->salt_len = salt_len;
10227
10228 return (PARSER_OK);
10229 }
10230
10231 int wpa_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10232 {
10233 u32 *digest = (u32 *) hash_buf->digest;
10234
10235 salt_t *salt = hash_buf->salt;
10236
10237 wpa_t *wpa = (wpa_t *) hash_buf->esalt;
10238
10239 hccap_t in;
10240
10241 memcpy (&in, input_buf, input_len);
10242
10243 if (in.eapol_size < 1 || in.eapol_size > 255) return (PARSER_HCCAP_EAPOL_SIZE);
10244
10245 memcpy (digest, in.keymic, 16);
10246
10247 /*
10248 http://www.one-net.eu/jsw/j_sec/m_ptype.html
10249 The phrase "Pairwise key expansion"
10250 Access Point Address (referred to as Authenticator Address AA)
10251 Supplicant Address (referred to as Supplicant Address SA)
10252 Access Point Nonce (referred to as Authenticator Anonce)
10253 Wireless Device Nonce (referred to as Supplicant Nonce Snonce)
10254 */
10255
10256 uint salt_len = strlen (in.essid);
10257
10258 if (salt_len > 36)
10259 {
10260 log_info ("WARNING: the length of the ESSID is too long. The hccap file may be invalid or corrupted");
10261
10262 return (PARSER_SALT_LENGTH);
10263 }
10264
10265 memcpy (salt->salt_buf, in.essid, salt_len);
10266
10267 salt->salt_len = salt_len;
10268
10269 salt->salt_iter = ROUNDS_WPA2 - 1;
10270
10271 unsigned char *pke_ptr = (unsigned char *) wpa->pke;
10272
10273 memcpy (pke_ptr, "Pairwise key expansion", 23);
10274
10275 if (memcmp (in.mac1, in.mac2, 6) < 0)
10276 {
10277 memcpy (pke_ptr + 23, in.mac1, 6);
10278 memcpy (pke_ptr + 29, in.mac2, 6);
10279 }
10280 else
10281 {
10282 memcpy (pke_ptr + 23, in.mac2, 6);
10283 memcpy (pke_ptr + 29, in.mac1, 6);
10284 }
10285
10286 if (memcmp (in.nonce1, in.nonce2, 32) < 0)
10287 {
10288 memcpy (pke_ptr + 35, in.nonce1, 32);
10289 memcpy (pke_ptr + 67, in.nonce2, 32);
10290 }
10291 else
10292 {
10293 memcpy (pke_ptr + 35, in.nonce2, 32);
10294 memcpy (pke_ptr + 67, in.nonce1, 32);
10295 }
10296
10297 for (int i = 0; i < 25; i++)
10298 {
10299 wpa->pke[i] = byte_swap_32 (wpa->pke[i]);
10300 }
10301
10302 memcpy (wpa->orig_mac1, in.mac1, 6);
10303 memcpy (wpa->orig_mac2, in.mac2, 6);
10304 memcpy (wpa->orig_nonce1, in.nonce1, 32);
10305 memcpy (wpa->orig_nonce2, in.nonce2, 32);
10306
10307 wpa->keyver = in.keyver;
10308
10309 if (wpa->keyver > 255)
10310 {
10311 log_info ("ATTENTION!");
10312 log_info (" The WPA/WPA2 key version in your .hccap file is invalid!");
10313 log_info (" This could be due to a recent aircrack-ng bug.");
10314 log_info (" The key version was automatically reset to a reasonable value.");
10315 log_info ("");
10316
10317 wpa->keyver &= 0xff;
10318 }
10319
10320 wpa->eapol_size = in.eapol_size;
10321
10322 unsigned char *eapol_ptr = (unsigned char *) wpa->eapol;
10323
10324 memcpy (eapol_ptr, in.eapol, wpa->eapol_size);
10325
10326 memset (eapol_ptr + wpa->eapol_size, 0, 256 - wpa->eapol_size);
10327
10328 eapol_ptr[wpa->eapol_size] = (unsigned char) 0x80;
10329
10330 if (wpa->keyver == 1)
10331 {
10332 // nothing to do
10333 }
10334 else
10335 {
10336 digest[0] = byte_swap_32 (digest[0]);
10337 digest[1] = byte_swap_32 (digest[1]);
10338 digest[2] = byte_swap_32 (digest[2]);
10339 digest[3] = byte_swap_32 (digest[3]);
10340
10341 for (int i = 0; i < 64; i++)
10342 {
10343 wpa->eapol[i] = byte_swap_32 (wpa->eapol[i]);
10344 }
10345 }
10346
10347 uint32_t *p0 = (uint32_t *) in.essid;
10348 uint32_t c0 = 0;
10349 uint32_t c1 = 0;
10350
10351 for (uint i = 0; i < sizeof (in.essid) / sizeof (uint32_t); i++) c0 ^= *p0++;
10352 for (uint i = 0; i < sizeof (wpa->pke) / sizeof (wpa->pke[0]); i++) c1 ^= wpa->pke[i];
10353
10354 salt->salt_buf[10] = c0;
10355 salt->salt_buf[11] = c1;
10356
10357 return (PARSER_OK);
10358 }
10359
10360 int psafe2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10361 {
10362 u32 *digest = (u32 *) hash_buf->digest;
10363
10364 salt_t *salt = hash_buf->salt;
10365
10366 if (input_len == 0)
10367 {
10368 log_error ("Password Safe v2 container not specified");
10369
10370 exit (-1);
10371 }
10372
10373 FILE *fp = fopen (input_buf, "rb");
10374
10375 if (fp == NULL)
10376 {
10377 log_error ("%s: %s", input_buf, strerror (errno));
10378
10379 exit (-1);
10380 }
10381
10382 psafe2_hdr buf;
10383
10384 memset (&buf, 0, sizeof (psafe2_hdr));
10385
10386 int n = fread (&buf, sizeof (psafe2_hdr), 1, fp);
10387
10388 fclose (fp);
10389
10390 if (n != 1) return (PARSER_PSAFE2_FILE_SIZE);
10391
10392 salt->salt_buf[0] = buf.random[0];
10393 salt->salt_buf[1] = buf.random[1];
10394
10395 salt->salt_len = 8;
10396 salt->salt_iter = 1000;
10397
10398 digest[0] = byte_swap_32 (buf.hash[0]);
10399 digest[1] = byte_swap_32 (buf.hash[1]);
10400 digest[2] = byte_swap_32 (buf.hash[2]);
10401 digest[3] = byte_swap_32 (buf.hash[3]);
10402 digest[4] = byte_swap_32 (buf.hash[4]);
10403
10404 return (PARSER_OK);
10405 }
10406
10407 int psafe3_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10408 {
10409 u32 *digest = (u32 *) hash_buf->digest;
10410
10411 salt_t *salt = hash_buf->salt;
10412
10413 if (input_len == 0)
10414 {
10415 log_error (".psafe3 not specified");
10416
10417 exit (-1);
10418 }
10419
10420 FILE *fp = fopen (input_buf, "rb");
10421
10422 if (fp == NULL)
10423 {
10424 log_error ("%s: %s", input_buf, strerror (errno));
10425
10426 exit (-1);
10427 }
10428
10429 psafe3_t in;
10430
10431 int n = fread (&in, sizeof (psafe3_t), 1, fp);
10432
10433 fclose (fp);
10434
10435 data.hashfile = input_buf; // we will need this in case it gets cracked
10436
10437 if (memcmp (SIGNATURE_PSAFE3, in.signature, 4)) return (PARSER_SIGNATURE_UNMATCHED);
10438
10439 if (n != 1) return (PARSER_PSAFE3_FILE_SIZE);
10440
10441 salt->salt_iter = in.iterations + 1;
10442
10443 salt->salt_buf[0] = in.salt_buf[0];
10444 salt->salt_buf[1] = in.salt_buf[1];
10445 salt->salt_buf[2] = in.salt_buf[2];
10446 salt->salt_buf[3] = in.salt_buf[3];
10447 salt->salt_buf[4] = in.salt_buf[4];
10448 salt->salt_buf[5] = in.salt_buf[5];
10449 salt->salt_buf[6] = in.salt_buf[6];
10450 salt->salt_buf[7] = in.salt_buf[7];
10451
10452 salt->salt_len = 32;
10453
10454 digest[0] = in.hash_buf[0];
10455 digest[1] = in.hash_buf[1];
10456 digest[2] = in.hash_buf[2];
10457 digest[3] = in.hash_buf[3];
10458 digest[4] = in.hash_buf[4];
10459 digest[5] = in.hash_buf[5];
10460 digest[6] = in.hash_buf[6];
10461 digest[7] = in.hash_buf[7];
10462
10463 digest[0] = byte_swap_32 (digest[0]);
10464 digest[1] = byte_swap_32 (digest[1]);
10465 digest[2] = byte_swap_32 (digest[2]);
10466 digest[3] = byte_swap_32 (digest[3]);
10467 digest[4] = byte_swap_32 (digest[4]);
10468 digest[5] = byte_swap_32 (digest[5]);
10469 digest[6] = byte_swap_32 (digest[6]);
10470 digest[7] = byte_swap_32 (digest[7]);
10471
10472 return (PARSER_OK);
10473 }
10474
10475 int phpass_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10476 {
10477 if ((input_len < DISPLAY_LEN_MIN_400) || (input_len > DISPLAY_LEN_MAX_400)) return (PARSER_GLOBAL_LENGTH);
10478
10479 if ((memcmp (SIGNATURE_PHPASS1, input_buf, 3)) && (memcmp (SIGNATURE_PHPASS2, input_buf, 3))) return (PARSER_SIGNATURE_UNMATCHED);
10480
10481 u32 *digest = (u32 *) hash_buf->digest;
10482
10483 salt_t *salt = hash_buf->salt;
10484
10485 char *iter_pos = input_buf + 3;
10486
10487 uint salt_iter = 1 << itoa64_to_int (iter_pos[0]);
10488
10489 if (salt_iter > 0x80000000) return (PARSER_SALT_ITERATION);
10490
10491 memcpy ((char *) salt->salt_sign, input_buf, 4);
10492
10493 salt->salt_iter = salt_iter;
10494
10495 char *salt_pos = iter_pos + 1;
10496
10497 uint salt_len = 8;
10498
10499 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
10500
10501 salt->salt_len = salt_len;
10502
10503 char *hash_pos = salt_pos + salt_len;
10504
10505 phpass_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
10506
10507 return (PARSER_OK);
10508 }
10509
10510 int md5crypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10511 {
10512 if (input_len < DISPLAY_LEN_MIN_500) return (PARSER_GLOBAL_LENGTH);
10513
10514 if (memcmp (SIGNATURE_MD5CRYPT, input_buf, 3)) return (PARSER_SIGNATURE_UNMATCHED);
10515
10516 u32 *digest = (u32 *) hash_buf->digest;
10517
10518 salt_t *salt = hash_buf->salt;
10519
10520 char *salt_pos = input_buf + 3;
10521
10522 uint iterations_len = 0;
10523
10524 if (memcmp (salt_pos, "rounds=", 7) == 0)
10525 {
10526 salt_pos += 7;
10527
10528 for (iterations_len = 0; salt_pos[0] >= '0' && salt_pos[0] <= '9' && iterations_len < 7; iterations_len++, salt_pos += 1) continue;
10529
10530 if (iterations_len == 0 ) return (PARSER_SALT_ITERATION);
10531 if (salt_pos[0] != '$') return (PARSER_SIGNATURE_UNMATCHED);
10532
10533 salt_pos[0] = 0x0;
10534
10535 salt->salt_iter = atoi (salt_pos - iterations_len);
10536
10537 salt_pos += 1;
10538
10539 iterations_len += 8;
10540 }
10541 else
10542 {
10543 salt->salt_iter = ROUNDS_MD5CRYPT;
10544 }
10545
10546 if (input_len > (DISPLAY_LEN_MAX_500 + iterations_len)) return (PARSER_GLOBAL_LENGTH);
10547
10548 char *hash_pos = strchr (salt_pos, '$');
10549
10550 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
10551
10552 uint salt_len = hash_pos - salt_pos;
10553
10554 if (salt_len > 8) return (PARSER_SALT_LENGTH);
10555
10556 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
10557
10558 salt->salt_len = salt_len;
10559
10560 hash_pos++;
10561
10562 uint hash_len = input_len - 3 - iterations_len - salt_len - 1;
10563
10564 if (hash_len != 22) return (PARSER_HASH_LENGTH);
10565
10566 md5crypt_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
10567
10568 return (PARSER_OK);
10569 }
10570
10571 int md5apr1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10572 {
10573 if (memcmp (SIGNATURE_MD5APR1, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
10574
10575 u32 *digest = (u32 *) hash_buf->digest;
10576
10577 salt_t *salt = hash_buf->salt;
10578
10579 char *salt_pos = input_buf + 6;
10580
10581 uint iterations_len = 0;
10582
10583 if (memcmp (salt_pos, "rounds=", 7) == 0)
10584 {
10585 salt_pos += 7;
10586
10587 for (iterations_len = 0; salt_pos[0] >= '0' && salt_pos[0] <= '9' && iterations_len < 7; iterations_len++, salt_pos += 1) continue;
10588
10589 if (iterations_len == 0 ) return (PARSER_SALT_ITERATION);
10590 if (salt_pos[0] != '$') return (PARSER_SIGNATURE_UNMATCHED);
10591
10592 salt_pos[0] = 0x0;
10593
10594 salt->salt_iter = atoi (salt_pos - iterations_len);
10595
10596 salt_pos += 1;
10597
10598 iterations_len += 8;
10599 }
10600 else
10601 {
10602 salt->salt_iter = ROUNDS_MD5CRYPT;
10603 }
10604
10605 if ((input_len < DISPLAY_LEN_MIN_1600) || (input_len > DISPLAY_LEN_MAX_1600 + iterations_len)) return (PARSER_GLOBAL_LENGTH);
10606
10607 char *hash_pos = strchr (salt_pos, '$');
10608
10609 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
10610
10611 uint salt_len = hash_pos - salt_pos;
10612
10613 if (salt_len > 8) return (PARSER_SALT_LENGTH);
10614
10615 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
10616
10617 salt->salt_len = salt_len;
10618
10619 hash_pos++;
10620
10621 md5crypt_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
10622
10623 return (PARSER_OK);
10624 }
10625
10626 int episerver_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10627 {
10628 if ((input_len < DISPLAY_LEN_MIN_141) || (input_len > DISPLAY_LEN_MAX_141)) return (PARSER_GLOBAL_LENGTH);
10629
10630 if (memcmp (SIGNATURE_EPISERVER, input_buf, 14)) return (PARSER_SIGNATURE_UNMATCHED);
10631
10632 u32 *digest = (u32 *) hash_buf->digest;
10633
10634 salt_t *salt = hash_buf->salt;
10635
10636 char *salt_pos = input_buf + 14;
10637
10638 char *hash_pos = strchr (salt_pos, '*');
10639
10640 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
10641
10642 hash_pos++;
10643
10644 uint salt_len = hash_pos - salt_pos - 1;
10645
10646 char *salt_buf_ptr = (char *) salt->salt_buf;
10647
10648 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
10649
10650 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
10651
10652 salt->salt_len = salt_len;
10653
10654 u8 tmp_buf[100] = { 0 };
10655
10656 base64_decode (base64_to_int, (const u8 *) hash_pos, 27, tmp_buf);
10657
10658 memcpy (digest, tmp_buf, 20);
10659
10660 digest[0] = byte_swap_32 (digest[0]);
10661 digest[1] = byte_swap_32 (digest[1]);
10662 digest[2] = byte_swap_32 (digest[2]);
10663 digest[3] = byte_swap_32 (digest[3]);
10664 digest[4] = byte_swap_32 (digest[4]);
10665
10666 digest[0] -= SHA1M_A;
10667 digest[1] -= SHA1M_B;
10668 digest[2] -= SHA1M_C;
10669 digest[3] -= SHA1M_D;
10670 digest[4] -= SHA1M_E;
10671
10672 return (PARSER_OK);
10673 }
10674
10675 int descrypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10676 {
10677 if ((input_len < DISPLAY_LEN_MIN_1500) || (input_len > DISPLAY_LEN_MAX_1500)) return (PARSER_GLOBAL_LENGTH);
10678
10679 unsigned char c12 = itoa64_to_int (input_buf[12]);
10680
10681 if (c12 & 3) return (PARSER_HASH_VALUE);
10682
10683 u32 *digest = (u32 *) hash_buf->digest;
10684
10685 salt_t *salt = hash_buf->salt;
10686
10687 // for ascii_digest
10688 salt->salt_sign[0] = input_buf[0];
10689 salt->salt_sign[1] = input_buf[1];
10690
10691 salt->salt_buf[0] = itoa64_to_int (input_buf[0])
10692 | itoa64_to_int (input_buf[1]) << 6;
10693
10694 salt->salt_len = 2;
10695
10696 u8 tmp_buf[100] = { 0 };
10697
10698 base64_decode (itoa64_to_int, (const u8 *) input_buf + 2, 11, tmp_buf);
10699
10700 memcpy (digest, tmp_buf, 8);
10701
10702 uint tt;
10703
10704 IP (digest[0], digest[1], tt);
10705
10706 digest[2] = 0;
10707 digest[3] = 0;
10708
10709 return (PARSER_OK);
10710 }
10711
10712 int md4_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10713 {
10714 if ((input_len < DISPLAY_LEN_MIN_900) || (input_len > DISPLAY_LEN_MAX_900)) return (PARSER_GLOBAL_LENGTH);
10715
10716 u32 *digest = (u32 *) hash_buf->digest;
10717
10718 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
10719 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
10720 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
10721 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
10722
10723 digest[0] = byte_swap_32 (digest[0]);
10724 digest[1] = byte_swap_32 (digest[1]);
10725 digest[2] = byte_swap_32 (digest[2]);
10726 digest[3] = byte_swap_32 (digest[3]);
10727
10728 digest[0] -= MD4M_A;
10729 digest[1] -= MD4M_B;
10730 digest[2] -= MD4M_C;
10731 digest[3] -= MD4M_D;
10732
10733 return (PARSER_OK);
10734 }
10735
10736 int md4s_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10737 {
10738 if (data.opts_type & OPTS_TYPE_ST_HEX)
10739 {
10740 if ((input_len < DISPLAY_LEN_MIN_910H) || (input_len > DISPLAY_LEN_MAX_910H)) return (PARSER_GLOBAL_LENGTH);
10741 }
10742 else
10743 {
10744 if ((input_len < DISPLAY_LEN_MIN_910) || (input_len > DISPLAY_LEN_MAX_910)) return (PARSER_GLOBAL_LENGTH);
10745 }
10746
10747 u32 *digest = (u32 *) hash_buf->digest;
10748
10749 salt_t *salt = hash_buf->salt;
10750
10751 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
10752 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
10753 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
10754 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
10755
10756 digest[0] = byte_swap_32 (digest[0]);
10757 digest[1] = byte_swap_32 (digest[1]);
10758 digest[2] = byte_swap_32 (digest[2]);
10759 digest[3] = byte_swap_32 (digest[3]);
10760
10761 digest[0] -= MD4M_A;
10762 digest[1] -= MD4M_B;
10763 digest[2] -= MD4M_C;
10764 digest[3] -= MD4M_D;
10765
10766 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
10767
10768 uint salt_len = input_len - 32 - 1;
10769
10770 char *salt_buf = input_buf + 32 + 1;
10771
10772 char *salt_buf_ptr = (char *) salt->salt_buf;
10773
10774 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
10775
10776 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
10777
10778 salt->salt_len = salt_len;
10779
10780 return (PARSER_OK);
10781 }
10782
10783 int md5_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10784 {
10785 if ((input_len < DISPLAY_LEN_MIN_0) || (input_len > DISPLAY_LEN_MAX_0)) return (PARSER_GLOBAL_LENGTH);
10786
10787 u32 *digest = (u32 *) hash_buf->digest;
10788
10789 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
10790 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
10791 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
10792 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
10793
10794 digest[0] = byte_swap_32 (digest[0]);
10795 digest[1] = byte_swap_32 (digest[1]);
10796 digest[2] = byte_swap_32 (digest[2]);
10797 digest[3] = byte_swap_32 (digest[3]);
10798
10799 digest[0] -= MD5M_A;
10800 digest[1] -= MD5M_B;
10801 digest[2] -= MD5M_C;
10802 digest[3] -= MD5M_D;
10803
10804 return (PARSER_OK);
10805 }
10806
10807 int md5half_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10808 {
10809 if ((input_len < DISPLAY_LEN_MIN_5100) || (input_len > DISPLAY_LEN_MAX_5100)) return (PARSER_GLOBAL_LENGTH);
10810
10811 u32 *digest = (u32 *) hash_buf->digest;
10812
10813 digest[0] = hex_to_u32 ((const u8 *) &input_buf[0]);
10814 digest[1] = hex_to_u32 ((const u8 *) &input_buf[8]);
10815 digest[2] = 0;
10816 digest[3] = 0;
10817
10818 digest[0] = byte_swap_32 (digest[0]);
10819 digest[1] = byte_swap_32 (digest[1]);
10820
10821 return (PARSER_OK);
10822 }
10823
10824 int md5s_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10825 {
10826 if (data.opts_type & OPTS_TYPE_ST_HEX)
10827 {
10828 if ((input_len < DISPLAY_LEN_MIN_10H) || (input_len > DISPLAY_LEN_MAX_10H)) return (PARSER_GLOBAL_LENGTH);
10829 }
10830 else
10831 {
10832 if ((input_len < DISPLAY_LEN_MIN_10) || (input_len > DISPLAY_LEN_MAX_10)) return (PARSER_GLOBAL_LENGTH);
10833 }
10834
10835 u32 *digest = (u32 *) hash_buf->digest;
10836
10837 salt_t *salt = hash_buf->salt;
10838
10839 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
10840 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
10841 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
10842 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
10843
10844 digest[0] = byte_swap_32 (digest[0]);
10845 digest[1] = byte_swap_32 (digest[1]);
10846 digest[2] = byte_swap_32 (digest[2]);
10847 digest[3] = byte_swap_32 (digest[3]);
10848
10849 digest[0] -= MD5M_A;
10850 digest[1] -= MD5M_B;
10851 digest[2] -= MD5M_C;
10852 digest[3] -= MD5M_D;
10853
10854 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
10855
10856 uint salt_len = input_len - 32 - 1;
10857
10858 char *salt_buf = input_buf + 32 + 1;
10859
10860 char *salt_buf_ptr = (char *) salt->salt_buf;
10861
10862 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
10863
10864 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
10865
10866 salt->salt_len = salt_len;
10867
10868 return (PARSER_OK);
10869 }
10870
10871 int md5pix_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10872 {
10873 if ((input_len < DISPLAY_LEN_MIN_2400) || (input_len > DISPLAY_LEN_MAX_2400)) return (PARSER_GLOBAL_LENGTH);
10874
10875 u32 *digest = (u32 *) hash_buf->digest;
10876
10877 digest[0] = itoa64_to_int (input_buf[ 0]) << 0
10878 | itoa64_to_int (input_buf[ 1]) << 6
10879 | itoa64_to_int (input_buf[ 2]) << 12
10880 | itoa64_to_int (input_buf[ 3]) << 18;
10881 digest[1] = itoa64_to_int (input_buf[ 4]) << 0
10882 | itoa64_to_int (input_buf[ 5]) << 6
10883 | itoa64_to_int (input_buf[ 6]) << 12
10884 | itoa64_to_int (input_buf[ 7]) << 18;
10885 digest[2] = itoa64_to_int (input_buf[ 8]) << 0
10886 | itoa64_to_int (input_buf[ 9]) << 6
10887 | itoa64_to_int (input_buf[10]) << 12
10888 | itoa64_to_int (input_buf[11]) << 18;
10889 digest[3] = itoa64_to_int (input_buf[12]) << 0
10890 | itoa64_to_int (input_buf[13]) << 6
10891 | itoa64_to_int (input_buf[14]) << 12
10892 | itoa64_to_int (input_buf[15]) << 18;
10893
10894 digest[0] -= MD5M_A;
10895 digest[1] -= MD5M_B;
10896 digest[2] -= MD5M_C;
10897 digest[3] -= MD5M_D;
10898
10899 digest[0] &= 0x00ffffff;
10900 digest[1] &= 0x00ffffff;
10901 digest[2] &= 0x00ffffff;
10902 digest[3] &= 0x00ffffff;
10903
10904 return (PARSER_OK);
10905 }
10906
10907 int md5asa_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10908 {
10909 if (data.opts_type & OPTS_TYPE_ST_HEX)
10910 {
10911 if ((input_len < DISPLAY_LEN_MIN_2410H) || (input_len > DISPLAY_LEN_MAX_2410H)) return (PARSER_GLOBAL_LENGTH);
10912 }
10913 else
10914 {
10915 if ((input_len < DISPLAY_LEN_MIN_2410) || (input_len > DISPLAY_LEN_MAX_2410)) return (PARSER_GLOBAL_LENGTH);
10916 }
10917
10918 u32 *digest = (u32 *) hash_buf->digest;
10919
10920 salt_t *salt = hash_buf->salt;
10921
10922 digest[0] = itoa64_to_int (input_buf[ 0]) << 0
10923 | itoa64_to_int (input_buf[ 1]) << 6
10924 | itoa64_to_int (input_buf[ 2]) << 12
10925 | itoa64_to_int (input_buf[ 3]) << 18;
10926 digest[1] = itoa64_to_int (input_buf[ 4]) << 0
10927 | itoa64_to_int (input_buf[ 5]) << 6
10928 | itoa64_to_int (input_buf[ 6]) << 12
10929 | itoa64_to_int (input_buf[ 7]) << 18;
10930 digest[2] = itoa64_to_int (input_buf[ 8]) << 0
10931 | itoa64_to_int (input_buf[ 9]) << 6
10932 | itoa64_to_int (input_buf[10]) << 12
10933 | itoa64_to_int (input_buf[11]) << 18;
10934 digest[3] = itoa64_to_int (input_buf[12]) << 0
10935 | itoa64_to_int (input_buf[13]) << 6
10936 | itoa64_to_int (input_buf[14]) << 12
10937 | itoa64_to_int (input_buf[15]) << 18;
10938
10939 digest[0] -= MD5M_A;
10940 digest[1] -= MD5M_B;
10941 digest[2] -= MD5M_C;
10942 digest[3] -= MD5M_D;
10943
10944 digest[0] &= 0x00ffffff;
10945 digest[1] &= 0x00ffffff;
10946 digest[2] &= 0x00ffffff;
10947 digest[3] &= 0x00ffffff;
10948
10949 if (input_buf[16] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
10950
10951 uint salt_len = input_len - 16 - 1;
10952
10953 char *salt_buf = input_buf + 16 + 1;
10954
10955 char *salt_buf_ptr = (char *) salt->salt_buf;
10956
10957 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
10958
10959 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
10960
10961 salt->salt_len = salt_len;
10962
10963 return (PARSER_OK);
10964 }
10965
10966 void transform_netntlmv1_key (const u8 *nthash, u8 *key)
10967 {
10968 key[0] = (nthash[0] >> 0);
10969 key[1] = (nthash[0] << 7) | (nthash[1] >> 1);
10970 key[2] = (nthash[1] << 6) | (nthash[2] >> 2);
10971 key[3] = (nthash[2] << 5) | (nthash[3] >> 3);
10972 key[4] = (nthash[3] << 4) | (nthash[4] >> 4);
10973 key[5] = (nthash[4] << 3) | (nthash[5] >> 5);
10974 key[6] = (nthash[5] << 2) | (nthash[6] >> 6);
10975 key[7] = (nthash[6] << 1);
10976
10977 key[0] |= 0x01;
10978 key[1] |= 0x01;
10979 key[2] |= 0x01;
10980 key[3] |= 0x01;
10981 key[4] |= 0x01;
10982 key[5] |= 0x01;
10983 key[6] |= 0x01;
10984 key[7] |= 0x01;
10985 }
10986
10987 int netntlmv1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10988 {
10989 if ((input_len < DISPLAY_LEN_MIN_5500) || (input_len > DISPLAY_LEN_MAX_5500)) return (PARSER_GLOBAL_LENGTH);
10990
10991 u32 *digest = (u32 *) hash_buf->digest;
10992
10993 salt_t *salt = hash_buf->salt;
10994
10995 netntlm_t *netntlm = (netntlm_t *) hash_buf->esalt;
10996
10997 /**
10998 * parse line
10999 */
11000
11001 char *user_pos = input_buf;
11002
11003 char *unused_pos = strchr (user_pos, ':');
11004
11005 if (unused_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
11006
11007 uint user_len = unused_pos - user_pos;
11008
11009 if (user_len > 60) return (PARSER_SALT_LENGTH);
11010
11011 unused_pos++;
11012
11013 char *domain_pos = strchr (unused_pos, ':');
11014
11015 if (domain_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
11016
11017 uint unused_len = domain_pos - unused_pos;
11018
11019 if (unused_len != 0) return (PARSER_SALT_LENGTH);
11020
11021 domain_pos++;
11022
11023 char *srvchall_pos = strchr (domain_pos, ':');
11024
11025 if (srvchall_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
11026
11027 uint domain_len = srvchall_pos - domain_pos;
11028
11029 if (domain_len > 45) return (PARSER_SALT_LENGTH);
11030
11031 srvchall_pos++;
11032
11033 char *hash_pos = strchr (srvchall_pos, ':');
11034
11035 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
11036
11037 uint srvchall_len = hash_pos - srvchall_pos;
11038
11039 // if (srvchall_len != 0) return (PARSER_SALT_LENGTH);
11040
11041 hash_pos++;
11042
11043 char *clichall_pos = strchr (hash_pos, ':');
11044
11045 if (clichall_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
11046
11047 uint hash_len = clichall_pos - hash_pos;
11048
11049 if (hash_len != 48) return (PARSER_HASH_LENGTH);
11050
11051 clichall_pos++;
11052
11053 uint clichall_len = input_len - user_len - 1 - unused_len - 1 - domain_len - 1 - srvchall_len - 1 - hash_len - 1;
11054
11055 if (clichall_len != 16) return (PARSER_SALT_LENGTH);
11056
11057 /**
11058 * store some data for later use
11059 */
11060
11061 netntlm->user_len = user_len * 2;
11062 netntlm->domain_len = domain_len * 2;
11063 netntlm->srvchall_len = srvchall_len / 2;
11064 netntlm->clichall_len = clichall_len / 2;
11065
11066 char *userdomain_ptr = (char *) netntlm->userdomain_buf;
11067 char *chall_ptr = (char *) netntlm->chall_buf;
11068
11069 /**
11070 * handle username and domainname
11071 */
11072
11073 for (uint i = 0; i < user_len; i++)
11074 {
11075 *userdomain_ptr++ = user_pos[i];
11076 *userdomain_ptr++ = 0;
11077 }
11078
11079 for (uint i = 0; i < domain_len; i++)
11080 {
11081 *userdomain_ptr++ = domain_pos[i];
11082 *userdomain_ptr++ = 0;
11083 }
11084
11085 /**
11086 * handle server challenge encoding
11087 */
11088
11089 for (uint i = 0; i < srvchall_len; i += 2)
11090 {
11091 const char p0 = srvchall_pos[i + 0];
11092 const char p1 = srvchall_pos[i + 1];
11093
11094 *chall_ptr++ = hex_convert (p1) << 0
11095 | hex_convert (p0) << 4;
11096 }
11097
11098 /**
11099 * handle client challenge encoding
11100 */
11101
11102 for (uint i = 0; i < clichall_len; i += 2)
11103 {
11104 const char p0 = clichall_pos[i + 0];
11105 const char p1 = clichall_pos[i + 1];
11106
11107 *chall_ptr++ = hex_convert (p1) << 0
11108 | hex_convert (p0) << 4;
11109 }
11110
11111 /**
11112 * store data
11113 */
11114
11115 char *salt_buf_ptr = (char *) salt->salt_buf;
11116
11117 uint salt_len = parse_and_store_salt (salt_buf_ptr, clichall_pos, clichall_len);
11118
11119 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11120
11121 salt->salt_len = salt_len;
11122
11123 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
11124 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
11125 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
11126 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
11127
11128 digest[0] = byte_swap_32 (digest[0]);
11129 digest[1] = byte_swap_32 (digest[1]);
11130 digest[2] = byte_swap_32 (digest[2]);
11131 digest[3] = byte_swap_32 (digest[3]);
11132
11133 /* special case, last 8 byte do not need to be checked since they are brute-forced next */
11134
11135 uint digest_tmp[2] = { 0 };
11136
11137 digest_tmp[0] = hex_to_u32 ((const u8 *) &hash_pos[32]);
11138 digest_tmp[1] = hex_to_u32 ((const u8 *) &hash_pos[40]);
11139
11140 digest_tmp[0] = byte_swap_32 (digest_tmp[0]);
11141 digest_tmp[1] = byte_swap_32 (digest_tmp[1]);
11142
11143 /* special case 2: ESS */
11144
11145 if (srvchall_len == 48)
11146 {
11147 if ((netntlm->chall_buf[2] == 0) && (netntlm->chall_buf[3] == 0) && (netntlm->chall_buf[4] == 0) && (netntlm->chall_buf[5] == 0))
11148 {
11149 uint w[16] = { 0 };
11150
11151 w[ 0] = netntlm->chall_buf[6];
11152 w[ 1] = netntlm->chall_buf[7];
11153 w[ 2] = netntlm->chall_buf[0];
11154 w[ 3] = netntlm->chall_buf[1];
11155 w[ 4] = 0x80;
11156 w[14] = 16 * 8;
11157
11158 uint dgst[4] = { 0 };
11159
11160 dgst[0] = MAGIC_A;
11161 dgst[1] = MAGIC_B;
11162 dgst[2] = MAGIC_C;
11163 dgst[3] = MAGIC_D;
11164
11165 md5_64 (w, dgst);
11166
11167 salt->salt_buf[0] = dgst[0];
11168 salt->salt_buf[1] = dgst[1];
11169 }
11170 }
11171
11172 /* precompute netntlmv1 exploit start */
11173
11174 for (uint i = 0; i < 0x10000; i++)
11175 {
11176 uint key_md4[2] = { i, 0 };
11177 uint key_des[2] = { 0, 0 };
11178
11179 transform_netntlmv1_key ((u8 *) key_md4, (u8 *) key_des);
11180
11181 uint Kc[16] = { 0 };
11182 uint Kd[16] = { 0 };
11183
11184 _des_keysetup (key_des, Kc, Kd, c_skb);
11185
11186 uint data3[2] = { salt->salt_buf[0], salt->salt_buf[1] };
11187
11188 _des_encrypt (data3, Kc, Kd, c_SPtrans);
11189
11190 if (data3[0] != digest_tmp[0]) continue;
11191 if (data3[1] != digest_tmp[1]) continue;
11192
11193 salt->salt_buf[2] = i;
11194
11195 salt->salt_len = 24;
11196
11197 break;
11198 }
11199
11200 salt->salt_buf_pc[0] = digest_tmp[0];
11201 salt->salt_buf_pc[1] = digest_tmp[1];
11202
11203 /* precompute netntlmv1 exploit stop */
11204
11205 u32 tt;
11206
11207 IP (digest[0], digest[1], tt);
11208 IP (digest[2], digest[3], tt);
11209
11210 digest[0] = rotr32 (digest[0], 29);
11211 digest[1] = rotr32 (digest[1], 29);
11212 digest[2] = rotr32 (digest[2], 29);
11213 digest[3] = rotr32 (digest[3], 29);
11214
11215 IP (salt->salt_buf[0], salt->salt_buf[1], tt);
11216
11217 salt->salt_buf[0] = rotl32 (salt->salt_buf[0], 3);
11218 salt->salt_buf[1] = rotl32 (salt->salt_buf[1], 3);
11219
11220 return (PARSER_OK);
11221 }
11222
11223 int netntlmv2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11224 {
11225 if ((input_len < DISPLAY_LEN_MIN_5600) || (input_len > DISPLAY_LEN_MAX_5600)) return (PARSER_GLOBAL_LENGTH);
11226
11227 u32 *digest = (u32 *) hash_buf->digest;
11228
11229 salt_t *salt = hash_buf->salt;
11230
11231 netntlm_t *netntlm = (netntlm_t *) hash_buf->esalt;
11232
11233 /**
11234 * parse line
11235 */
11236
11237 char *user_pos = input_buf;
11238
11239 char *unused_pos = strchr (user_pos, ':');
11240
11241 if (unused_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
11242
11243 uint user_len = unused_pos - user_pos;
11244
11245 if (user_len > 60) return (PARSER_SALT_LENGTH);
11246
11247 unused_pos++;
11248
11249 char *domain_pos = strchr (unused_pos, ':');
11250
11251 if (domain_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
11252
11253 uint unused_len = domain_pos - unused_pos;
11254
11255 if (unused_len != 0) return (PARSER_SALT_LENGTH);
11256
11257 domain_pos++;
11258
11259 char *srvchall_pos = strchr (domain_pos, ':');
11260
11261 if (srvchall_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
11262
11263 uint domain_len = srvchall_pos - domain_pos;
11264
11265 if (domain_len > 45) return (PARSER_SALT_LENGTH);
11266
11267 srvchall_pos++;
11268
11269 char *hash_pos = strchr (srvchall_pos, ':');
11270
11271 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
11272
11273 uint srvchall_len = hash_pos - srvchall_pos;
11274
11275 if (srvchall_len != 16) return (PARSER_SALT_LENGTH);
11276
11277 hash_pos++;
11278
11279 char *clichall_pos = strchr (hash_pos, ':');
11280
11281 if (clichall_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
11282
11283 uint hash_len = clichall_pos - hash_pos;
11284
11285 if (hash_len != 32) return (PARSER_HASH_LENGTH);
11286
11287 clichall_pos++;
11288
11289 uint clichall_len = input_len - user_len - 1 - unused_len - 1 - domain_len - 1 - srvchall_len - 1 - hash_len - 1;
11290
11291 if (clichall_len > 1024) return (PARSER_SALT_LENGTH);
11292
11293 if (clichall_len % 2) return (PARSER_SALT_VALUE);
11294
11295 /**
11296 * store some data for later use
11297 */
11298
11299 netntlm->user_len = user_len * 2;
11300 netntlm->domain_len = domain_len * 2;
11301 netntlm->srvchall_len = srvchall_len / 2;
11302 netntlm->clichall_len = clichall_len / 2;
11303
11304 char *userdomain_ptr = (char *) netntlm->userdomain_buf;
11305 char *chall_ptr = (char *) netntlm->chall_buf;
11306
11307 /**
11308 * handle username and domainname
11309 */
11310
11311 for (uint i = 0; i < user_len; i++)
11312 {
11313 *userdomain_ptr++ = toupper (user_pos[i]);
11314 *userdomain_ptr++ = 0;
11315 }
11316
11317 for (uint i = 0; i < domain_len; i++)
11318 {
11319 *userdomain_ptr++ = domain_pos[i];
11320 *userdomain_ptr++ = 0;
11321 }
11322
11323 *userdomain_ptr++ = 0x80;
11324
11325 /**
11326 * handle server challenge encoding
11327 */
11328
11329 for (uint i = 0; i < srvchall_len; i += 2)
11330 {
11331 const char p0 = srvchall_pos[i + 0];
11332 const char p1 = srvchall_pos[i + 1];
11333
11334 *chall_ptr++ = hex_convert (p1) << 0
11335 | hex_convert (p0) << 4;
11336 }
11337
11338 /**
11339 * handle client challenge encoding
11340 */
11341
11342 for (uint i = 0; i < clichall_len; i += 2)
11343 {
11344 const char p0 = clichall_pos[i + 0];
11345 const char p1 = clichall_pos[i + 1];
11346
11347 *chall_ptr++ = hex_convert (p1) << 0
11348 | hex_convert (p0) << 4;
11349 }
11350
11351 *chall_ptr++ = 0x80;
11352
11353 /**
11354 * handle hash itself
11355 */
11356
11357 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
11358 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
11359 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
11360 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
11361
11362 digest[0] = byte_swap_32 (digest[0]);
11363 digest[1] = byte_swap_32 (digest[1]);
11364 digest[2] = byte_swap_32 (digest[2]);
11365 digest[3] = byte_swap_32 (digest[3]);
11366
11367 /**
11368 * reuse challange data as salt_buf, its the buffer that is most likely unique
11369 */
11370
11371 salt->salt_buf[0] = 0;
11372 salt->salt_buf[1] = 0;
11373 salt->salt_buf[2] = 0;
11374 salt->salt_buf[3] = 0;
11375 salt->salt_buf[4] = 0;
11376 salt->salt_buf[5] = 0;
11377 salt->salt_buf[6] = 0;
11378 salt->salt_buf[7] = 0;
11379
11380 uint *uptr;
11381
11382 uptr = (uint *) netntlm->userdomain_buf;
11383
11384 for (uint i = 0; i < 16; i += 16)
11385 {
11386 md5_64 (uptr, salt->salt_buf);
11387 }
11388
11389 uptr = (uint *) netntlm->chall_buf;
11390
11391 for (uint i = 0; i < 256; i += 16)
11392 {
11393 md5_64 (uptr, salt->salt_buf);
11394 }
11395
11396 salt->salt_len = 16;
11397
11398 return (PARSER_OK);
11399 }
11400
11401 int joomla_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11402 {
11403 if (data.opts_type & OPTS_TYPE_ST_HEX)
11404 {
11405 if ((input_len < DISPLAY_LEN_MIN_11H) || (input_len > DISPLAY_LEN_MAX_11H)) return (PARSER_GLOBAL_LENGTH);
11406 }
11407 else
11408 {
11409 if ((input_len < DISPLAY_LEN_MIN_11) || (input_len > DISPLAY_LEN_MAX_11)) return (PARSER_GLOBAL_LENGTH);
11410 }
11411
11412 u32 *digest = (u32 *) hash_buf->digest;
11413
11414 salt_t *salt = hash_buf->salt;
11415
11416 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11417 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11418 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11419 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11420
11421 digest[0] = byte_swap_32 (digest[0]);
11422 digest[1] = byte_swap_32 (digest[1]);
11423 digest[2] = byte_swap_32 (digest[2]);
11424 digest[3] = byte_swap_32 (digest[3]);
11425
11426 digest[0] -= MD5M_A;
11427 digest[1] -= MD5M_B;
11428 digest[2] -= MD5M_C;
11429 digest[3] -= MD5M_D;
11430
11431 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
11432
11433 uint salt_len = input_len - 32 - 1;
11434
11435 char *salt_buf = input_buf + 32 + 1;
11436
11437 char *salt_buf_ptr = (char *) salt->salt_buf;
11438
11439 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
11440
11441 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11442
11443 salt->salt_len = salt_len;
11444
11445 return (PARSER_OK);
11446 }
11447
11448 int postgresql_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11449 {
11450 if (data.opts_type & OPTS_TYPE_ST_HEX)
11451 {
11452 if ((input_len < DISPLAY_LEN_MIN_12H) || (input_len > DISPLAY_LEN_MAX_12H)) return (PARSER_GLOBAL_LENGTH);
11453 }
11454 else
11455 {
11456 if ((input_len < DISPLAY_LEN_MIN_12) || (input_len > DISPLAY_LEN_MAX_12)) return (PARSER_GLOBAL_LENGTH);
11457 }
11458
11459 u32 *digest = (u32 *) hash_buf->digest;
11460
11461 salt_t *salt = hash_buf->salt;
11462
11463 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11464 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11465 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11466 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11467
11468 digest[0] = byte_swap_32 (digest[0]);
11469 digest[1] = byte_swap_32 (digest[1]);
11470 digest[2] = byte_swap_32 (digest[2]);
11471 digest[3] = byte_swap_32 (digest[3]);
11472
11473 digest[0] -= MD5M_A;
11474 digest[1] -= MD5M_B;
11475 digest[2] -= MD5M_C;
11476 digest[3] -= MD5M_D;
11477
11478 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
11479
11480 uint salt_len = input_len - 32 - 1;
11481
11482 char *salt_buf = input_buf + 32 + 1;
11483
11484 char *salt_buf_ptr = (char *) salt->salt_buf;
11485
11486 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
11487
11488 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11489
11490 salt->salt_len = salt_len;
11491
11492 return (PARSER_OK);
11493 }
11494
11495 int md5md5_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11496 {
11497 if ((input_len < DISPLAY_LEN_MIN_2600) || (input_len > DISPLAY_LEN_MAX_2600)) return (PARSER_GLOBAL_LENGTH);
11498
11499 u32 *digest = (u32 *) hash_buf->digest;
11500
11501 salt_t *salt = hash_buf->salt;
11502
11503 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11504 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11505 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11506 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11507
11508 digest[0] = byte_swap_32 (digest[0]);
11509 digest[1] = byte_swap_32 (digest[1]);
11510 digest[2] = byte_swap_32 (digest[2]);
11511 digest[3] = byte_swap_32 (digest[3]);
11512
11513 digest[0] -= MD5M_A;
11514 digest[1] -= MD5M_B;
11515 digest[2] -= MD5M_C;
11516 digest[3] -= MD5M_D;
11517
11518 /**
11519 * This is a virtual salt. While the algorithm is basically not salted
11520 * we can exploit the salt buffer to set the 0x80 and the w[14] value.
11521 * This way we can save a special md5md5 kernel and reuse the one from vbull.
11522 */
11523
11524 char *salt_buf_ptr = (char *) salt->salt_buf;
11525
11526 uint salt_len = parse_and_store_salt (salt_buf_ptr, (char *) "", 0);
11527
11528 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11529
11530 salt->salt_len = salt_len;
11531
11532 return (PARSER_OK);
11533 }
11534
11535 int vb3_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11536 {
11537 if (data.opts_type & OPTS_TYPE_ST_HEX)
11538 {
11539 if ((input_len < DISPLAY_LEN_MIN_2611H) || (input_len > DISPLAY_LEN_MAX_2611H)) return (PARSER_GLOBAL_LENGTH);
11540 }
11541 else
11542 {
11543 if ((input_len < DISPLAY_LEN_MIN_2611) || (input_len > DISPLAY_LEN_MAX_2611)) return (PARSER_GLOBAL_LENGTH);
11544 }
11545
11546 u32 *digest = (u32 *) hash_buf->digest;
11547
11548 salt_t *salt = hash_buf->salt;
11549
11550 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11551 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11552 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11553 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11554
11555 digest[0] = byte_swap_32 (digest[0]);
11556 digest[1] = byte_swap_32 (digest[1]);
11557 digest[2] = byte_swap_32 (digest[2]);
11558 digest[3] = byte_swap_32 (digest[3]);
11559
11560 digest[0] -= MD5M_A;
11561 digest[1] -= MD5M_B;
11562 digest[2] -= MD5M_C;
11563 digest[3] -= MD5M_D;
11564
11565 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
11566
11567 uint salt_len = input_len - 32 - 1;
11568
11569 char *salt_buf = input_buf + 32 + 1;
11570
11571 char *salt_buf_ptr = (char *) salt->salt_buf;
11572
11573 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
11574
11575 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11576
11577 salt->salt_len = salt_len;
11578
11579 return (PARSER_OK);
11580 }
11581
11582 int vb30_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11583 {
11584 if (data.opts_type & OPTS_TYPE_ST_HEX)
11585 {
11586 if ((input_len < DISPLAY_LEN_MIN_2711H) || (input_len > DISPLAY_LEN_MAX_2711H)) return (PARSER_GLOBAL_LENGTH);
11587 }
11588 else
11589 {
11590 if ((input_len < DISPLAY_LEN_MIN_2711) || (input_len > DISPLAY_LEN_MAX_2711)) return (PARSER_GLOBAL_LENGTH);
11591 }
11592
11593 u32 *digest = (u32 *) hash_buf->digest;
11594
11595 salt_t *salt = hash_buf->salt;
11596
11597 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11598 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11599 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11600 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11601
11602 digest[0] = byte_swap_32 (digest[0]);
11603 digest[1] = byte_swap_32 (digest[1]);
11604 digest[2] = byte_swap_32 (digest[2]);
11605 digest[3] = byte_swap_32 (digest[3]);
11606
11607 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
11608
11609 uint salt_len = input_len - 32 - 1;
11610
11611 char *salt_buf = input_buf + 32 + 1;
11612
11613 char *salt_buf_ptr = (char *) salt->salt_buf;
11614
11615 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
11616
11617 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11618
11619 salt->salt_len = salt_len;
11620
11621 return (PARSER_OK);
11622 }
11623
11624 int dcc_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11625 {
11626 if (data.opts_type & OPTS_TYPE_ST_HEX)
11627 {
11628 if ((input_len < DISPLAY_LEN_MIN_1100H) || (input_len > DISPLAY_LEN_MAX_1100H)) return (PARSER_GLOBAL_LENGTH);
11629 }
11630 else
11631 {
11632 if ((input_len < DISPLAY_LEN_MIN_1100) || (input_len > DISPLAY_LEN_MAX_1100)) return (PARSER_GLOBAL_LENGTH);
11633 }
11634
11635 u32 *digest = (u32 *) hash_buf->digest;
11636
11637 salt_t *salt = hash_buf->salt;
11638
11639 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11640 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11641 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11642 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11643
11644 digest[0] = byte_swap_32 (digest[0]);
11645 digest[1] = byte_swap_32 (digest[1]);
11646 digest[2] = byte_swap_32 (digest[2]);
11647 digest[3] = byte_swap_32 (digest[3]);
11648
11649 digest[0] -= MD4M_A;
11650 digest[1] -= MD4M_B;
11651 digest[2] -= MD4M_C;
11652 digest[3] -= MD4M_D;
11653
11654 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
11655
11656 uint salt_len = input_len - 32 - 1;
11657
11658 char *salt_buf = input_buf + 32 + 1;
11659
11660 char *salt_buf_ptr = (char *) salt->salt_buf;
11661
11662 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
11663
11664 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11665
11666 salt->salt_len = salt_len;
11667
11668 return (PARSER_OK);
11669 }
11670
11671 int ipb2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11672 {
11673 if (data.opts_type & OPTS_TYPE_ST_HEX)
11674 {
11675 if ((input_len < DISPLAY_LEN_MIN_2811H) || (input_len > DISPLAY_LEN_MAX_2811H)) return (PARSER_GLOBAL_LENGTH);
11676 }
11677 else
11678 {
11679 if ((input_len < DISPLAY_LEN_MIN_2811) || (input_len > DISPLAY_LEN_MAX_2811)) return (PARSER_GLOBAL_LENGTH);
11680 }
11681
11682 u32 *digest = (u32 *) hash_buf->digest;
11683
11684 salt_t *salt = hash_buf->salt;
11685
11686 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11687 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11688 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11689 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11690
11691 digest[0] = byte_swap_32 (digest[0]);
11692 digest[1] = byte_swap_32 (digest[1]);
11693 digest[2] = byte_swap_32 (digest[2]);
11694 digest[3] = byte_swap_32 (digest[3]);
11695
11696 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
11697
11698 uint salt_len = input_len - 32 - 1;
11699
11700 char *salt_buf = input_buf + 32 + 1;
11701
11702 uint salt_pc_block[16] = { 0 };
11703
11704 char *salt_pc_block_ptr = (char *) salt_pc_block;
11705
11706 salt_len = parse_and_store_salt (salt_pc_block_ptr, salt_buf, salt_len);
11707
11708 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11709
11710 salt_pc_block_ptr[salt_len] = (unsigned char) 0x80;
11711
11712 salt_pc_block[14] = salt_len * 8;
11713
11714 uint salt_pc_digest[4] = { MAGIC_A, MAGIC_B, MAGIC_C, MAGIC_D };
11715
11716 md5_64 (salt_pc_block, salt_pc_digest);
11717
11718 salt_pc_digest[0] = byte_swap_32 (salt_pc_digest[0]);
11719 salt_pc_digest[1] = byte_swap_32 (salt_pc_digest[1]);
11720 salt_pc_digest[2] = byte_swap_32 (salt_pc_digest[2]);
11721 salt_pc_digest[3] = byte_swap_32 (salt_pc_digest[3]);
11722
11723 u8 *salt_buf_ptr = (u8 *) salt->salt_buf;
11724
11725 memcpy (salt_buf_ptr, salt_buf, salt_len);
11726
11727 u8 *salt_buf_pc_ptr = (u8 *) salt->salt_buf_pc;
11728
11729 bin_to_hex_lower (salt_pc_digest[0], salt_buf_pc_ptr + 0);
11730 bin_to_hex_lower (salt_pc_digest[1], salt_buf_pc_ptr + 8);
11731 bin_to_hex_lower (salt_pc_digest[2], salt_buf_pc_ptr + 16);
11732 bin_to_hex_lower (salt_pc_digest[3], salt_buf_pc_ptr + 24);
11733
11734 salt->salt_len = 32; // changed, was salt_len before -- was a bug? 32 should be correct
11735
11736 return (PARSER_OK);
11737 }
11738
11739 int sha1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11740 {
11741 if ((input_len < DISPLAY_LEN_MIN_100) || (input_len > DISPLAY_LEN_MAX_100)) return (PARSER_GLOBAL_LENGTH);
11742
11743 u32 *digest = (u32 *) hash_buf->digest;
11744
11745 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11746 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11747 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11748 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11749 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
11750
11751 digest[0] -= SHA1M_A;
11752 digest[1] -= SHA1M_B;
11753 digest[2] -= SHA1M_C;
11754 digest[3] -= SHA1M_D;
11755 digest[4] -= SHA1M_E;
11756
11757 return (PARSER_OK);
11758 }
11759
11760 int sha1linkedin_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11761 {
11762 if ((input_len < DISPLAY_LEN_MIN_100) || (input_len > DISPLAY_LEN_MAX_100)) return (PARSER_GLOBAL_LENGTH);
11763
11764 u32 *digest = (u32 *) hash_buf->digest;
11765
11766 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11767 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11768 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11769 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11770 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
11771
11772 return (PARSER_OK);
11773 }
11774
11775 int sha1axcrypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11776 {
11777 if ((input_len < DISPLAY_LEN_MIN_13300) || (input_len > DISPLAY_LEN_MAX_13300)) return (PARSER_GLOBAL_LENGTH);
11778
11779 if (memcmp (SIGNATURE_AXCRYPT_SHA1, input_buf, 13)) return (PARSER_SIGNATURE_UNMATCHED);
11780
11781 u32 *digest = (u32 *) hash_buf->digest;
11782
11783 input_buf +=14;
11784
11785 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11786 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11787 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11788 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11789 digest[4] = 0x00000000;
11790
11791 return (PARSER_OK);
11792 }
11793
11794 int sha1s_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11795 {
11796 if (data.opts_type & OPTS_TYPE_ST_HEX)
11797 {
11798 if ((input_len < DISPLAY_LEN_MIN_110H) || (input_len > DISPLAY_LEN_MAX_110H)) return (PARSER_GLOBAL_LENGTH);
11799 }
11800 else
11801 {
11802 if ((input_len < DISPLAY_LEN_MIN_110) || (input_len > DISPLAY_LEN_MAX_110)) return (PARSER_GLOBAL_LENGTH);
11803 }
11804
11805 u32 *digest = (u32 *) hash_buf->digest;
11806
11807 salt_t *salt = hash_buf->salt;
11808
11809 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11810 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11811 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11812 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11813 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
11814
11815 digest[0] -= SHA1M_A;
11816 digest[1] -= SHA1M_B;
11817 digest[2] -= SHA1M_C;
11818 digest[3] -= SHA1M_D;
11819 digest[4] -= SHA1M_E;
11820
11821 if (input_buf[40] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
11822
11823 uint salt_len = input_len - 40 - 1;
11824
11825 char *salt_buf = input_buf + 40 + 1;
11826
11827 char *salt_buf_ptr = (char *) salt->salt_buf;
11828
11829 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
11830
11831 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11832
11833 salt->salt_len = salt_len;
11834
11835 return (PARSER_OK);
11836 }
11837
11838 int pstoken_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11839 {
11840 if ((input_len < DISPLAY_LEN_MIN_13500) || (input_len > DISPLAY_LEN_MAX_13500)) return (PARSER_GLOBAL_LENGTH);
11841
11842 u32 *digest = (u32 *) hash_buf->digest;
11843
11844 salt_t *salt = hash_buf->salt;
11845
11846 pstoken_t *pstoken = (pstoken_t *) hash_buf->esalt;
11847
11848 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11849 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11850 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11851 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11852 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
11853
11854 if (input_buf[40] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
11855
11856 uint salt_len = input_len - 40 - 1;
11857
11858 char *salt_buf = input_buf + 40 + 1;
11859
11860 if (salt_len == UINT_MAX || salt_len % 2 != 0) return (PARSER_SALT_LENGTH);
11861
11862 u8 *pstoken_ptr = (u8 *) pstoken->salt_buf;
11863
11864 for (uint i = 0, j = 0; i < salt_len; i += 2, j += 1)
11865 {
11866 pstoken_ptr[j] = hex_to_u8 ((const u8 *) &salt_buf[i]);
11867 }
11868
11869 pstoken->salt_len = salt_len / 2;
11870
11871 /* some fake salt for the sorting mechanisms */
11872
11873 salt->salt_buf[0] = pstoken->salt_buf[0];
11874 salt->salt_buf[1] = pstoken->salt_buf[1];
11875 salt->salt_buf[2] = pstoken->salt_buf[2];
11876 salt->salt_buf[3] = pstoken->salt_buf[3];
11877 salt->salt_buf[4] = pstoken->salt_buf[4];
11878 salt->salt_buf[5] = pstoken->salt_buf[5];
11879 salt->salt_buf[6] = pstoken->salt_buf[6];
11880 salt->salt_buf[7] = pstoken->salt_buf[7];
11881
11882 salt->salt_len = 32;
11883
11884 /* we need to check if we can precompute some of the data --
11885 this is possible since the scheme is badly designed */
11886
11887 pstoken->pc_digest[0] = SHA1M_A;
11888 pstoken->pc_digest[1] = SHA1M_B;
11889 pstoken->pc_digest[2] = SHA1M_C;
11890 pstoken->pc_digest[3] = SHA1M_D;
11891 pstoken->pc_digest[4] = SHA1M_E;
11892
11893 pstoken->pc_offset = 0;
11894
11895 for (int i = 0; i < (int) pstoken->salt_len - 64; i += 64)
11896 {
11897 uint w[16];
11898
11899 w[ 0] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 0]);
11900 w[ 1] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 1]);
11901 w[ 2] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 2]);
11902 w[ 3] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 3]);
11903 w[ 4] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 4]);
11904 w[ 5] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 5]);
11905 w[ 6] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 6]);
11906 w[ 7] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 7]);
11907 w[ 8] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 8]);
11908 w[ 9] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 9]);
11909 w[10] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 10]);
11910 w[11] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 11]);
11911 w[12] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 12]);
11912 w[13] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 13]);
11913 w[14] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 14]);
11914 w[15] = byte_swap_32 (pstoken->salt_buf[pstoken->pc_offset + 15]);
11915
11916 sha1_64 (w, pstoken->pc_digest);
11917
11918 pstoken->pc_offset += 16;
11919 }
11920
11921 return (PARSER_OK);
11922 }
11923
11924 int sha1b64_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11925 {
11926 if ((input_len < DISPLAY_LEN_MIN_101) || (input_len > DISPLAY_LEN_MAX_101)) return (PARSER_GLOBAL_LENGTH);
11927
11928 if (memcmp (SIGNATURE_SHA1B64, input_buf, 5)) return (PARSER_SIGNATURE_UNMATCHED);
11929
11930 u32 *digest = (u32 *) hash_buf->digest;
11931
11932 u8 tmp_buf[100] = { 0 };
11933
11934 base64_decode (base64_to_int, (const u8 *) input_buf + 5, input_len - 5, tmp_buf);
11935
11936 memcpy (digest, tmp_buf, 20);
11937
11938 digest[0] = byte_swap_32 (digest[0]);
11939 digest[1] = byte_swap_32 (digest[1]);
11940 digest[2] = byte_swap_32 (digest[2]);
11941 digest[3] = byte_swap_32 (digest[3]);
11942 digest[4] = byte_swap_32 (digest[4]);
11943
11944 digest[0] -= SHA1M_A;
11945 digest[1] -= SHA1M_B;
11946 digest[2] -= SHA1M_C;
11947 digest[3] -= SHA1M_D;
11948 digest[4] -= SHA1M_E;
11949
11950 return (PARSER_OK);
11951 }
11952
11953 int sha1b64s_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11954 {
11955 if ((input_len < DISPLAY_LEN_MIN_111) || (input_len > DISPLAY_LEN_MAX_111)) return (PARSER_GLOBAL_LENGTH);
11956
11957 if (memcmp (SIGNATURE_SSHA1B64_lower, input_buf, 6) && memcmp (SIGNATURE_SSHA1B64_upper, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
11958
11959 u32 *digest = (u32 *) hash_buf->digest;
11960
11961 salt_t *salt = hash_buf->salt;
11962
11963 u8 tmp_buf[100] = { 0 };
11964
11965 int tmp_len = base64_decode (base64_to_int, (const u8 *) input_buf + 6, input_len - 6, tmp_buf);
11966
11967 if (tmp_len < 20) return (PARSER_HASH_LENGTH);
11968
11969 memcpy (digest, tmp_buf, 20);
11970
11971 int salt_len = tmp_len - 20;
11972
11973 if (salt_len < 0) return (PARSER_SALT_LENGTH);
11974
11975 salt->salt_len = salt_len;
11976
11977 memcpy (salt->salt_buf, tmp_buf + 20, salt->salt_len);
11978
11979 if (data.opts_type & OPTS_TYPE_ST_ADD80)
11980 {
11981 char *ptr = (char *) salt->salt_buf;
11982
11983 ptr[salt->salt_len] = 0x80;
11984 }
11985
11986 digest[0] = byte_swap_32 (digest[0]);
11987 digest[1] = byte_swap_32 (digest[1]);
11988 digest[2] = byte_swap_32 (digest[2]);
11989 digest[3] = byte_swap_32 (digest[3]);
11990 digest[4] = byte_swap_32 (digest[4]);
11991
11992 digest[0] -= SHA1M_A;
11993 digest[1] -= SHA1M_B;
11994 digest[2] -= SHA1M_C;
11995 digest[3] -= SHA1M_D;
11996 digest[4] -= SHA1M_E;
11997
11998 return (PARSER_OK);
11999 }
12000
12001 int mssql2000_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12002 {
12003 if ((input_len < DISPLAY_LEN_MIN_131) || (input_len > DISPLAY_LEN_MAX_131)) return (PARSER_GLOBAL_LENGTH);
12004
12005 if (memcmp (SIGNATURE_MSSQL, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
12006
12007 u32 *digest = (u32 *) hash_buf->digest;
12008
12009 salt_t *salt = hash_buf->salt;
12010
12011 char *salt_buf = input_buf + 6;
12012
12013 uint salt_len = 8;
12014
12015 char *salt_buf_ptr = (char *) salt->salt_buf;
12016
12017 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
12018
12019 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
12020
12021 salt->salt_len = salt_len;
12022
12023 char *hash_pos = input_buf + 6 + 8 + 40;
12024
12025 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
12026 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
12027 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
12028 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
12029 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
12030
12031 digest[0] -= SHA1M_A;
12032 digest[1] -= SHA1M_B;
12033 digest[2] -= SHA1M_C;
12034 digest[3] -= SHA1M_D;
12035 digest[4] -= SHA1M_E;
12036
12037 return (PARSER_OK);
12038 }
12039
12040 int mssql2005_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12041 {
12042 if ((input_len < DISPLAY_LEN_MIN_132) || (input_len > DISPLAY_LEN_MAX_132)) return (PARSER_GLOBAL_LENGTH);
12043
12044 if (memcmp (SIGNATURE_MSSQL, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
12045
12046 u32 *digest = (u32 *) hash_buf->digest;
12047
12048 salt_t *salt = hash_buf->salt;
12049
12050 char *salt_buf = input_buf + 6;
12051
12052 uint salt_len = 8;
12053
12054 char *salt_buf_ptr = (char *) salt->salt_buf;
12055
12056 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
12057
12058 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
12059
12060 salt->salt_len = salt_len;
12061
12062 char *hash_pos = input_buf + 6 + 8;
12063
12064 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
12065 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
12066 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
12067 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
12068 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
12069
12070 digest[0] -= SHA1M_A;
12071 digest[1] -= SHA1M_B;
12072 digest[2] -= SHA1M_C;
12073 digest[3] -= SHA1M_D;
12074 digest[4] -= SHA1M_E;
12075
12076 return (PARSER_OK);
12077 }
12078
12079 int mssql2012_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12080 {
12081 if ((input_len < DISPLAY_LEN_MIN_1731) || (input_len > DISPLAY_LEN_MAX_1731)) return (PARSER_GLOBAL_LENGTH);
12082
12083 if (memcmp (SIGNATURE_MSSQL2012, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
12084
12085 u64 *digest = (u64 *) hash_buf->digest;
12086
12087 salt_t *salt = hash_buf->salt;
12088
12089 char *salt_buf = input_buf + 6;
12090
12091 uint salt_len = 8;
12092
12093 char *salt_buf_ptr = (char *) salt->salt_buf;
12094
12095 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
12096
12097 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
12098
12099 salt->salt_len = salt_len;
12100
12101 char *hash_pos = input_buf + 6 + 8;
12102
12103 digest[0] = hex_to_u64 ((const u8 *) &hash_pos[ 0]);
12104 digest[1] = hex_to_u64 ((const u8 *) &hash_pos[ 16]);
12105 digest[2] = hex_to_u64 ((const u8 *) &hash_pos[ 32]);
12106 digest[3] = hex_to_u64 ((const u8 *) &hash_pos[ 48]);
12107 digest[4] = hex_to_u64 ((const u8 *) &hash_pos[ 64]);
12108 digest[5] = hex_to_u64 ((const u8 *) &hash_pos[ 80]);
12109 digest[6] = hex_to_u64 ((const u8 *) &hash_pos[ 96]);
12110 digest[7] = hex_to_u64 ((const u8 *) &hash_pos[112]);
12111
12112 digest[0] -= SHA512M_A;
12113 digest[1] -= SHA512M_B;
12114 digest[2] -= SHA512M_C;
12115 digest[3] -= SHA512M_D;
12116 digest[4] -= SHA512M_E;
12117 digest[5] -= SHA512M_F;
12118 digest[6] -= SHA512M_G;
12119 digest[7] -= SHA512M_H;
12120
12121 return (PARSER_OK);
12122 }
12123
12124 int oracleh_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12125 {
12126 if (data.opts_type & OPTS_TYPE_ST_HEX)
12127 {
12128 if ((input_len < DISPLAY_LEN_MIN_3100H) || (input_len > DISPLAY_LEN_MAX_3100H)) return (PARSER_GLOBAL_LENGTH);
12129 }
12130 else
12131 {
12132 if ((input_len < DISPLAY_LEN_MIN_3100) || (input_len > DISPLAY_LEN_MAX_3100)) return (PARSER_GLOBAL_LENGTH);
12133 }
12134
12135 u32 *digest = (u32 *) hash_buf->digest;
12136
12137 salt_t *salt = hash_buf->salt;
12138
12139 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
12140 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
12141 digest[2] = 0;
12142 digest[3] = 0;
12143
12144 digest[0] = byte_swap_32 (digest[0]);
12145 digest[1] = byte_swap_32 (digest[1]);
12146
12147 if (input_buf[16] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
12148
12149 uint salt_len = input_len - 16 - 1;
12150
12151 char *salt_buf = input_buf + 16 + 1;
12152
12153 char *salt_buf_ptr = (char *) salt->salt_buf;
12154
12155 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
12156
12157 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
12158
12159 salt->salt_len = salt_len;
12160
12161 return (PARSER_OK);
12162 }
12163
12164 int oracles_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12165 {
12166 if ((input_len < DISPLAY_LEN_MIN_112) || (input_len > DISPLAY_LEN_MAX_112)) return (PARSER_GLOBAL_LENGTH);
12167
12168 u32 *digest = (u32 *) hash_buf->digest;
12169
12170 salt_t *salt = hash_buf->salt;
12171
12172 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
12173 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
12174 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
12175 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
12176 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
12177
12178 digest[0] -= SHA1M_A;
12179 digest[1] -= SHA1M_B;
12180 digest[2] -= SHA1M_C;
12181 digest[3] -= SHA1M_D;
12182 digest[4] -= SHA1M_E;
12183
12184 if (input_buf[40] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
12185
12186 uint salt_len = input_len - 40 - 1;
12187
12188 char *salt_buf = input_buf + 40 + 1;
12189
12190 char *salt_buf_ptr = (char *) salt->salt_buf;
12191
12192 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
12193
12194 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
12195
12196 salt->salt_len = salt_len;
12197
12198 return (PARSER_OK);
12199 }
12200
12201 int oraclet_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12202 {
12203 if ((input_len < DISPLAY_LEN_MIN_12300) || (input_len > DISPLAY_LEN_MAX_12300)) return (PARSER_GLOBAL_LENGTH);
12204
12205 u32 *digest = (u32 *) hash_buf->digest;
12206
12207 salt_t *salt = hash_buf->salt;
12208
12209 char *hash_pos = input_buf;
12210
12211 digest[ 0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
12212 digest[ 1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
12213 digest[ 2] = hex_to_u32 ((const u8 *) &hash_pos[ 16]);
12214 digest[ 3] = hex_to_u32 ((const u8 *) &hash_pos[ 24]);
12215 digest[ 4] = hex_to_u32 ((const u8 *) &hash_pos[ 32]);
12216 digest[ 5] = hex_to_u32 ((const u8 *) &hash_pos[ 40]);
12217 digest[ 6] = hex_to_u32 ((const u8 *) &hash_pos[ 48]);
12218 digest[ 7] = hex_to_u32 ((const u8 *) &hash_pos[ 56]);
12219 digest[ 8] = hex_to_u32 ((const u8 *) &hash_pos[ 64]);
12220 digest[ 9] = hex_to_u32 ((const u8 *) &hash_pos[ 72]);
12221 digest[10] = hex_to_u32 ((const u8 *) &hash_pos[ 80]);
12222 digest[11] = hex_to_u32 ((const u8 *) &hash_pos[ 88]);
12223 digest[12] = hex_to_u32 ((const u8 *) &hash_pos[ 96]);
12224 digest[13] = hex_to_u32 ((const u8 *) &hash_pos[104]);
12225 digest[14] = hex_to_u32 ((const u8 *) &hash_pos[112]);
12226 digest[15] = hex_to_u32 ((const u8 *) &hash_pos[120]);
12227
12228 char *salt_pos = input_buf + 128;
12229
12230 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_pos[ 0]);
12231 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_pos[ 8]);
12232 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &salt_pos[16]);
12233 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &salt_pos[24]);
12234
12235 salt->salt_iter = ROUNDS_ORACLET - 1;
12236 salt->salt_len = 16;
12237
12238 return (PARSER_OK);
12239 }
12240
12241 int sha256_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12242 {
12243 if ((input_len < DISPLAY_LEN_MIN_1400) || (input_len > DISPLAY_LEN_MAX_1400)) return (PARSER_GLOBAL_LENGTH);
12244
12245 u32 *digest = (u32 *) hash_buf->digest;
12246
12247 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
12248 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
12249 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
12250 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
12251 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
12252 digest[5] = hex_to_u32 ((const u8 *) &input_buf[40]);
12253 digest[6] = hex_to_u32 ((const u8 *) &input_buf[48]);
12254 digest[7] = hex_to_u32 ((const u8 *) &input_buf[56]);
12255
12256 digest[0] -= SHA256M_A;
12257 digest[1] -= SHA256M_B;
12258 digest[2] -= SHA256M_C;
12259 digest[3] -= SHA256M_D;
12260 digest[4] -= SHA256M_E;
12261 digest[5] -= SHA256M_F;
12262 digest[6] -= SHA256M_G;
12263 digest[7] -= SHA256M_H;
12264
12265 return (PARSER_OK);
12266 }
12267
12268 int sha256s_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12269 {
12270 if (data.opts_type & OPTS_TYPE_ST_HEX)
12271 {
12272 if ((input_len < DISPLAY_LEN_MIN_1410H) || (input_len > DISPLAY_LEN_MAX_1410H)) return (PARSER_GLOBAL_LENGTH);
12273 }
12274 else
12275 {
12276 if ((input_len < DISPLAY_LEN_MIN_1410) || (input_len > DISPLAY_LEN_MAX_1410)) return (PARSER_GLOBAL_LENGTH);
12277 }
12278
12279 u32 *digest = (u32 *) hash_buf->digest;
12280
12281 salt_t *salt = hash_buf->salt;
12282
12283 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
12284 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
12285 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
12286 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
12287 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
12288 digest[5] = hex_to_u32 ((const u8 *) &input_buf[40]);
12289 digest[6] = hex_to_u32 ((const u8 *) &input_buf[48]);
12290 digest[7] = hex_to_u32 ((const u8 *) &input_buf[56]);
12291
12292 digest[0] -= SHA256M_A;
12293 digest[1] -= SHA256M_B;
12294 digest[2] -= SHA256M_C;
12295 digest[3] -= SHA256M_D;
12296 digest[4] -= SHA256M_E;
12297 digest[5] -= SHA256M_F;
12298 digest[6] -= SHA256M_G;
12299 digest[7] -= SHA256M_H;
12300
12301 if (input_buf[64] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
12302
12303 uint salt_len = input_len - 64 - 1;
12304
12305 char *salt_buf = input_buf + 64 + 1;
12306
12307 char *salt_buf_ptr = (char *) salt->salt_buf;
12308
12309 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
12310
12311 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
12312
12313 salt->salt_len = salt_len;
12314
12315 return (PARSER_OK);
12316 }
12317
12318 int sha384_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12319 {
12320 if ((input_len < DISPLAY_LEN_MIN_10800) || (input_len > DISPLAY_LEN_MAX_10800)) return (PARSER_GLOBAL_LENGTH);
12321
12322 u64 *digest = (u64 *) hash_buf->digest;
12323
12324 digest[0] = hex_to_u64 ((const u8 *) &input_buf[ 0]);
12325 digest[1] = hex_to_u64 ((const u8 *) &input_buf[ 16]);
12326 digest[2] = hex_to_u64 ((const u8 *) &input_buf[ 32]);
12327 digest[3] = hex_to_u64 ((const u8 *) &input_buf[ 48]);
12328 digest[4] = hex_to_u64 ((const u8 *) &input_buf[ 64]);
12329 digest[5] = hex_to_u64 ((const u8 *) &input_buf[ 80]);
12330 digest[6] = 0;
12331 digest[7] = 0;
12332
12333 digest[0] -= SHA384M_A;
12334 digest[1] -= SHA384M_B;
12335 digest[2] -= SHA384M_C;
12336 digest[3] -= SHA384M_D;
12337 digest[4] -= SHA384M_E;
12338 digest[5] -= SHA384M_F;
12339 digest[6] -= 0;
12340 digest[7] -= 0;
12341
12342 return (PARSER_OK);
12343 }
12344
12345 int sha512_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12346 {
12347 if ((input_len < DISPLAY_LEN_MIN_1700) || (input_len > DISPLAY_LEN_MAX_1700)) return (PARSER_GLOBAL_LENGTH);
12348
12349 u64 *digest = (u64 *) hash_buf->digest;
12350
12351 digest[0] = hex_to_u64 ((const u8 *) &input_buf[ 0]);
12352 digest[1] = hex_to_u64 ((const u8 *) &input_buf[ 16]);
12353 digest[2] = hex_to_u64 ((const u8 *) &input_buf[ 32]);
12354 digest[3] = hex_to_u64 ((const u8 *) &input_buf[ 48]);
12355 digest[4] = hex_to_u64 ((const u8 *) &input_buf[ 64]);
12356 digest[5] = hex_to_u64 ((const u8 *) &input_buf[ 80]);
12357 digest[6] = hex_to_u64 ((const u8 *) &input_buf[ 96]);
12358 digest[7] = hex_to_u64 ((const u8 *) &input_buf[112]);
12359
12360 digest[0] -= SHA512M_A;
12361 digest[1] -= SHA512M_B;
12362 digest[2] -= SHA512M_C;
12363 digest[3] -= SHA512M_D;
12364 digest[4] -= SHA512M_E;
12365 digest[5] -= SHA512M_F;
12366 digest[6] -= SHA512M_G;
12367 digest[7] -= SHA512M_H;
12368
12369 return (PARSER_OK);
12370 }
12371
12372 int sha512s_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12373 {
12374 if (data.opts_type & OPTS_TYPE_ST_HEX)
12375 {
12376 if ((input_len < DISPLAY_LEN_MIN_1710H) || (input_len > DISPLAY_LEN_MAX_1710H)) return (PARSER_GLOBAL_LENGTH);
12377 }
12378 else
12379 {
12380 if ((input_len < DISPLAY_LEN_MIN_1710) || (input_len > DISPLAY_LEN_MAX_1710)) return (PARSER_GLOBAL_LENGTH);
12381 }
12382
12383 u64 *digest = (u64 *) hash_buf->digest;
12384
12385 salt_t *salt = hash_buf->salt;
12386
12387 digest[0] = hex_to_u64 ((const u8 *) &input_buf[ 0]);
12388 digest[1] = hex_to_u64 ((const u8 *) &input_buf[ 16]);
12389 digest[2] = hex_to_u64 ((const u8 *) &input_buf[ 32]);
12390 digest[3] = hex_to_u64 ((const u8 *) &input_buf[ 48]);
12391 digest[4] = hex_to_u64 ((const u8 *) &input_buf[ 64]);
12392 digest[5] = hex_to_u64 ((const u8 *) &input_buf[ 80]);
12393 digest[6] = hex_to_u64 ((const u8 *) &input_buf[ 96]);
12394 digest[7] = hex_to_u64 ((const u8 *) &input_buf[112]);
12395
12396 digest[0] -= SHA512M_A;
12397 digest[1] -= SHA512M_B;
12398 digest[2] -= SHA512M_C;
12399 digest[3] -= SHA512M_D;
12400 digest[4] -= SHA512M_E;
12401 digest[5] -= SHA512M_F;
12402 digest[6] -= SHA512M_G;
12403 digest[7] -= SHA512M_H;
12404
12405 if (input_buf[128] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
12406
12407 uint salt_len = input_len - 128 - 1;
12408
12409 char *salt_buf = input_buf + 128 + 1;
12410
12411 char *salt_buf_ptr = (char *) salt->salt_buf;
12412
12413 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
12414
12415 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
12416
12417 salt->salt_len = salt_len;
12418
12419 return (PARSER_OK);
12420 }
12421
12422 int sha512crypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12423 {
12424 if (memcmp (SIGNATURE_SHA512CRYPT, input_buf, 3)) return (PARSER_SIGNATURE_UNMATCHED);
12425
12426 u64 *digest = (u64 *) hash_buf->digest;
12427
12428 salt_t *salt = hash_buf->salt;
12429
12430 char *salt_pos = input_buf + 3;
12431
12432 uint iterations_len = 0;
12433
12434 if (memcmp (salt_pos, "rounds=", 7) == 0)
12435 {
12436 salt_pos += 7;
12437
12438 for (iterations_len = 0; salt_pos[0] >= '0' && salt_pos[0] <= '9' && iterations_len < 7; iterations_len++, salt_pos += 1) continue;
12439
12440 if (iterations_len == 0 ) return (PARSER_SALT_ITERATION);
12441 if (salt_pos[0] != '$') return (PARSER_SIGNATURE_UNMATCHED);
12442
12443 salt_pos[0] = 0x0;
12444
12445 salt->salt_iter = atoi (salt_pos - iterations_len);
12446
12447 salt_pos += 1;
12448
12449 iterations_len += 8;
12450 }
12451 else
12452 {
12453 salt->salt_iter = ROUNDS_SHA512CRYPT;
12454 }
12455
12456 if ((input_len < DISPLAY_LEN_MIN_1800) || (input_len > DISPLAY_LEN_MAX_1800 + iterations_len)) return (PARSER_GLOBAL_LENGTH);
12457
12458 char *hash_pos = strchr (salt_pos, '$');
12459
12460 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12461
12462 uint salt_len = hash_pos - salt_pos;
12463
12464 if (salt_len > 16) return (PARSER_SALT_LENGTH);
12465
12466 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
12467
12468 salt->salt_len = salt_len;
12469
12470 hash_pos++;
12471
12472 sha512crypt_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
12473
12474 return (PARSER_OK);
12475 }
12476
12477 int keccak_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12478 {
12479 if ((input_len < DISPLAY_LEN_MIN_5000) || (input_len > DISPLAY_LEN_MAX_5000)) return (PARSER_GLOBAL_LENGTH);
12480
12481 if (input_len % 16) return (PARSER_GLOBAL_LENGTH);
12482
12483 u64 *digest = (u64 *) hash_buf->digest;
12484
12485 salt_t *salt = hash_buf->salt;
12486
12487 uint keccak_mdlen = input_len / 2;
12488
12489 for (uint i = 0; i < keccak_mdlen / 8; i++)
12490 {
12491 digest[i] = hex_to_u64 ((const u8 *) &input_buf[i * 16]);
12492
12493 digest[i] = byte_swap_64 (digest[i]);
12494 }
12495
12496 salt->keccak_mdlen = keccak_mdlen;
12497
12498 return (PARSER_OK);
12499 }
12500
12501 int ikepsk_md5_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12502 {
12503 if ((input_len < DISPLAY_LEN_MIN_5300) || (input_len > DISPLAY_LEN_MAX_5300)) return (PARSER_GLOBAL_LENGTH);
12504
12505 u32 *digest = (u32 *) hash_buf->digest;
12506
12507 salt_t *salt = hash_buf->salt;
12508
12509 ikepsk_t *ikepsk = (ikepsk_t *) hash_buf->esalt;
12510
12511 /**
12512 * Parse that strange long line
12513 */
12514
12515 char *in_off[9];
12516
12517 size_t in_len[9] = { 0 };
12518
12519 in_off[0] = strtok (input_buf, ":");
12520
12521 if (in_off[0] == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12522
12523 in_len[0] = strlen (in_off[0]);
12524
12525 size_t i;
12526
12527 for (i = 1; i < 9; i++)
12528 {
12529 in_off[i] = strtok (NULL, ":");
12530
12531 if (in_off[i] == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12532
12533 in_len[i] = strlen (in_off[i]);
12534 }
12535
12536 char *ptr = (char *) ikepsk->msg_buf;
12537
12538 for (i = 0; i < in_len[0]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[0] + i);
12539 for (i = 0; i < in_len[1]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[1] + i);
12540 for (i = 0; i < in_len[2]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[2] + i);
12541 for (i = 0; i < in_len[3]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[3] + i);
12542 for (i = 0; i < in_len[4]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[4] + i);
12543 for (i = 0; i < in_len[5]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[5] + i);
12544
12545 *ptr = 0x80;
12546
12547 ikepsk->msg_len = (in_len[0] + in_len[1] + in_len[2] + in_len[3] + in_len[4] + in_len[5]) / 2;
12548
12549 ptr = (char *) ikepsk->nr_buf;
12550
12551 for (i = 0; i < in_len[6]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[6] + i);
12552 for (i = 0; i < in_len[7]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[7] + i);
12553
12554 *ptr = 0x80;
12555
12556 ikepsk->nr_len = (in_len[6] + in_len[7]) / 2;
12557
12558 /**
12559 * Store to database
12560 */
12561
12562 ptr = in_off[8];
12563
12564 digest[0] = hex_to_u32 ((const u8 *) &ptr[ 0]);
12565 digest[1] = hex_to_u32 ((const u8 *) &ptr[ 8]);
12566 digest[2] = hex_to_u32 ((const u8 *) &ptr[16]);
12567 digest[3] = hex_to_u32 ((const u8 *) &ptr[24]);
12568
12569 digest[0] = byte_swap_32 (digest[0]);
12570 digest[1] = byte_swap_32 (digest[1]);
12571 digest[2] = byte_swap_32 (digest[2]);
12572 digest[3] = byte_swap_32 (digest[3]);
12573
12574 salt->salt_len = 32;
12575
12576 salt->salt_buf[0] = ikepsk->nr_buf[0];
12577 salt->salt_buf[1] = ikepsk->nr_buf[1];
12578 salt->salt_buf[2] = ikepsk->nr_buf[2];
12579 salt->salt_buf[3] = ikepsk->nr_buf[3];
12580 salt->salt_buf[4] = ikepsk->nr_buf[4];
12581 salt->salt_buf[5] = ikepsk->nr_buf[5];
12582 salt->salt_buf[6] = ikepsk->nr_buf[6];
12583 salt->salt_buf[7] = ikepsk->nr_buf[7];
12584
12585 return (PARSER_OK);
12586 }
12587
12588 int ikepsk_sha1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12589 {
12590 if ((input_len < DISPLAY_LEN_MIN_5400) || (input_len > DISPLAY_LEN_MAX_5400)) return (PARSER_GLOBAL_LENGTH);
12591
12592 u32 *digest = (u32 *) hash_buf->digest;
12593
12594 salt_t *salt = hash_buf->salt;
12595
12596 ikepsk_t *ikepsk = (ikepsk_t *) hash_buf->esalt;
12597
12598 /**
12599 * Parse that strange long line
12600 */
12601
12602 char *in_off[9];
12603
12604 size_t in_len[9] = { 0 };
12605
12606 in_off[0] = strtok (input_buf, ":");
12607
12608 if (in_off[0] == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12609
12610 in_len[0] = strlen (in_off[0]);
12611
12612 size_t i;
12613
12614 for (i = 1; i < 9; i++)
12615 {
12616 in_off[i] = strtok (NULL, ":");
12617
12618 if (in_off[i] == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12619
12620 in_len[i] = strlen (in_off[i]);
12621 }
12622
12623 char *ptr = (char *) ikepsk->msg_buf;
12624
12625 for (i = 0; i < in_len[0]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[0] + i);
12626 for (i = 0; i < in_len[1]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[1] + i);
12627 for (i = 0; i < in_len[2]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[2] + i);
12628 for (i = 0; i < in_len[3]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[3] + i);
12629 for (i = 0; i < in_len[4]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[4] + i);
12630 for (i = 0; i < in_len[5]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[5] + i);
12631
12632 *ptr = 0x80;
12633
12634 ikepsk->msg_len = (in_len[0] + in_len[1] + in_len[2] + in_len[3] + in_len[4] + in_len[5]) / 2;
12635
12636 ptr = (char *) ikepsk->nr_buf;
12637
12638 for (i = 0; i < in_len[6]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[6] + i);
12639 for (i = 0; i < in_len[7]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[7] + i);
12640
12641 *ptr = 0x80;
12642
12643 ikepsk->nr_len = (in_len[6] + in_len[7]) / 2;
12644
12645 /**
12646 * Store to database
12647 */
12648
12649 ptr = in_off[8];
12650
12651 digest[0] = hex_to_u32 ((const u8 *) &ptr[ 0]);
12652 digest[1] = hex_to_u32 ((const u8 *) &ptr[ 8]);
12653 digest[2] = hex_to_u32 ((const u8 *) &ptr[16]);
12654 digest[3] = hex_to_u32 ((const u8 *) &ptr[24]);
12655 digest[4] = hex_to_u32 ((const u8 *) &ptr[32]);
12656
12657 salt->salt_len = 32;
12658
12659 salt->salt_buf[0] = ikepsk->nr_buf[0];
12660 salt->salt_buf[1] = ikepsk->nr_buf[1];
12661 salt->salt_buf[2] = ikepsk->nr_buf[2];
12662 salt->salt_buf[3] = ikepsk->nr_buf[3];
12663 salt->salt_buf[4] = ikepsk->nr_buf[4];
12664 salt->salt_buf[5] = ikepsk->nr_buf[5];
12665 salt->salt_buf[6] = ikepsk->nr_buf[6];
12666 salt->salt_buf[7] = ikepsk->nr_buf[7];
12667
12668 return (PARSER_OK);
12669 }
12670
12671 int ripemd160_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12672 {
12673 if ((input_len < DISPLAY_LEN_MIN_6000) || (input_len > DISPLAY_LEN_MAX_6000)) return (PARSER_GLOBAL_LENGTH);
12674
12675 u32 *digest = (u32 *) hash_buf->digest;
12676
12677 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
12678 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
12679 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
12680 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
12681 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
12682
12683 digest[0] = byte_swap_32 (digest[0]);
12684 digest[1] = byte_swap_32 (digest[1]);
12685 digest[2] = byte_swap_32 (digest[2]);
12686 digest[3] = byte_swap_32 (digest[3]);
12687 digest[4] = byte_swap_32 (digest[4]);
12688
12689 return (PARSER_OK);
12690 }
12691
12692 int whirlpool_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12693 {
12694 if ((input_len < DISPLAY_LEN_MIN_6100) || (input_len > DISPLAY_LEN_MAX_6100)) return (PARSER_GLOBAL_LENGTH);
12695
12696 u32 *digest = (u32 *) hash_buf->digest;
12697
12698 digest[ 0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
12699 digest[ 1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
12700 digest[ 2] = hex_to_u32 ((const u8 *) &input_buf[ 16]);
12701 digest[ 3] = hex_to_u32 ((const u8 *) &input_buf[ 24]);
12702 digest[ 4] = hex_to_u32 ((const u8 *) &input_buf[ 32]);
12703 digest[ 5] = hex_to_u32 ((const u8 *) &input_buf[ 40]);
12704 digest[ 6] = hex_to_u32 ((const u8 *) &input_buf[ 48]);
12705 digest[ 7] = hex_to_u32 ((const u8 *) &input_buf[ 56]);
12706 digest[ 8] = hex_to_u32 ((const u8 *) &input_buf[ 64]);
12707 digest[ 9] = hex_to_u32 ((const u8 *) &input_buf[ 72]);
12708 digest[10] = hex_to_u32 ((const u8 *) &input_buf[ 80]);
12709 digest[11] = hex_to_u32 ((const u8 *) &input_buf[ 88]);
12710 digest[12] = hex_to_u32 ((const u8 *) &input_buf[ 96]);
12711 digest[13] = hex_to_u32 ((const u8 *) &input_buf[104]);
12712 digest[14] = hex_to_u32 ((const u8 *) &input_buf[112]);
12713 digest[15] = hex_to_u32 ((const u8 *) &input_buf[120]);
12714
12715 return (PARSER_OK);
12716 }
12717
12718 int androidpin_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12719 {
12720 if ((input_len < DISPLAY_LEN_MIN_5800) || (input_len > DISPLAY_LEN_MAX_5800)) return (PARSER_GLOBAL_LENGTH);
12721
12722 u32 *digest = (u32 *) hash_buf->digest;
12723
12724 salt_t *salt = hash_buf->salt;
12725
12726 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
12727 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
12728 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
12729 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
12730 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
12731
12732 if (input_buf[40] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
12733
12734 uint salt_len = input_len - 40 - 1;
12735
12736 char *salt_buf = input_buf + 40 + 1;
12737
12738 char *salt_buf_ptr = (char *) salt->salt_buf;
12739
12740 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
12741
12742 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
12743
12744 salt->salt_len = salt_len;
12745
12746 salt->salt_iter = ROUNDS_ANDROIDPIN - 1;
12747
12748 return (PARSER_OK);
12749 }
12750
12751 int truecrypt_parse_hash_1k (char *input_buf, uint input_len, hash_t *hash_buf)
12752 {
12753 u32 *digest = (u32 *) hash_buf->digest;
12754
12755 salt_t *salt = hash_buf->salt;
12756
12757 tc_t *tc = (tc_t *) hash_buf->esalt;
12758
12759 if (input_len == 0)
12760 {
12761 log_error ("TrueCrypt container not specified");
12762
12763 exit (-1);
12764 }
12765
12766 FILE *fp = fopen (input_buf, "rb");
12767
12768 if (fp == NULL)
12769 {
12770 log_error ("%s: %s", input_buf, strerror (errno));
12771
12772 exit (-1);
12773 }
12774
12775 char buf[512] = { 0 };
12776
12777 int n = fread (buf, 1, sizeof (buf), fp);
12778
12779 fclose (fp);
12780
12781 if (n != 512) return (PARSER_TC_FILE_SIZE);
12782
12783 memcpy (tc->salt_buf, buf, 64);
12784
12785 memcpy (tc->data_buf, buf + 64, 512 - 64);
12786
12787 salt->salt_buf[0] = tc->salt_buf[0];
12788
12789 salt->salt_len = 4;
12790
12791 salt->salt_iter = ROUNDS_TRUECRYPT_1K - 1;
12792
12793 tc->signature = 0x45555254; // "TRUE"
12794
12795 digest[0] = tc->data_buf[0];
12796
12797 return (PARSER_OK);
12798 }
12799
12800 int truecrypt_parse_hash_2k (char *input_buf, uint input_len, hash_t *hash_buf)
12801 {
12802 u32 *digest = (u32 *) hash_buf->digest;
12803
12804 salt_t *salt = hash_buf->salt;
12805
12806 tc_t *tc = (tc_t *) hash_buf->esalt;
12807
12808 if (input_len == 0)
12809 {
12810 log_error ("TrueCrypt container not specified");
12811
12812 exit (-1);
12813 }
12814
12815 FILE *fp = fopen (input_buf, "rb");
12816
12817 if (fp == NULL)
12818 {
12819 log_error ("%s: %s", input_buf, strerror (errno));
12820
12821 exit (-1);
12822 }
12823
12824 char buf[512] = { 0 };
12825
12826 int n = fread (buf, 1, sizeof (buf), fp);
12827
12828 fclose (fp);
12829
12830 if (n != 512) return (PARSER_TC_FILE_SIZE);
12831
12832 memcpy (tc->salt_buf, buf, 64);
12833
12834 memcpy (tc->data_buf, buf + 64, 512 - 64);
12835
12836 salt->salt_buf[0] = tc->salt_buf[0];
12837
12838 salt->salt_len = 4;
12839
12840 salt->salt_iter = ROUNDS_TRUECRYPT_2K - 1;
12841
12842 tc->signature = 0x45555254; // "TRUE"
12843
12844 digest[0] = tc->data_buf[0];
12845
12846 return (PARSER_OK);
12847 }
12848
12849 int md5aix_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12850 {
12851 if ((input_len < DISPLAY_LEN_MIN_6300) || (input_len > DISPLAY_LEN_MAX_6300)) return (PARSER_GLOBAL_LENGTH);
12852
12853 if (memcmp (SIGNATURE_MD5AIX, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
12854
12855 u32 *digest = (u32 *) hash_buf->digest;
12856
12857 salt_t *salt = hash_buf->salt;
12858
12859 char *salt_pos = input_buf + 6;
12860
12861 char *hash_pos = strchr (salt_pos, '$');
12862
12863 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12864
12865 uint salt_len = hash_pos - salt_pos;
12866
12867 if (salt_len < 8) return (PARSER_SALT_LENGTH);
12868
12869 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
12870
12871 salt->salt_len = salt_len;
12872
12873 salt->salt_iter = 1000;
12874
12875 hash_pos++;
12876
12877 md5crypt_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
12878
12879 return (PARSER_OK);
12880 }
12881
12882 int sha1aix_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12883 {
12884 if ((input_len < DISPLAY_LEN_MIN_6700) || (input_len > DISPLAY_LEN_MAX_6700)) return (PARSER_GLOBAL_LENGTH);
12885
12886 if (memcmp (SIGNATURE_SHA1AIX, input_buf, 7)) return (PARSER_SIGNATURE_UNMATCHED);
12887
12888 u32 *digest = (u32 *) hash_buf->digest;
12889
12890 salt_t *salt = hash_buf->salt;
12891
12892 char *iter_pos = input_buf + 7;
12893
12894 char *salt_pos = strchr (iter_pos, '$');
12895
12896 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12897
12898 salt_pos++;
12899
12900 char *hash_pos = strchr (salt_pos, '$');
12901
12902 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12903
12904 uint salt_len = hash_pos - salt_pos;
12905
12906 if (salt_len < 16) return (PARSER_SALT_LENGTH);
12907
12908 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
12909
12910 salt->salt_len = salt_len;
12911
12912 char salt_iter[3] = { iter_pos[0], iter_pos[1], 0 };
12913
12914 salt->salt_sign[0] = atoi (salt_iter);
12915
12916 salt->salt_iter = (1 << atoi (salt_iter)) - 1;
12917
12918 hash_pos++;
12919
12920 sha1aix_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
12921
12922 digest[0] = byte_swap_32 (digest[0]);
12923 digest[1] = byte_swap_32 (digest[1]);
12924 digest[2] = byte_swap_32 (digest[2]);
12925 digest[3] = byte_swap_32 (digest[3]);
12926 digest[4] = byte_swap_32 (digest[4]);
12927
12928 return (PARSER_OK);
12929 }
12930
12931 int sha256aix_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12932 {
12933 if ((input_len < DISPLAY_LEN_MIN_6400) || (input_len > DISPLAY_LEN_MAX_6400)) return (PARSER_GLOBAL_LENGTH);
12934
12935 if (memcmp (SIGNATURE_SHA256AIX, input_buf, 9)) return (PARSER_SIGNATURE_UNMATCHED);
12936
12937 u32 *digest = (u32 *) hash_buf->digest;
12938
12939 salt_t *salt = hash_buf->salt;
12940
12941 char *iter_pos = input_buf + 9;
12942
12943 char *salt_pos = strchr (iter_pos, '$');
12944
12945 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12946
12947 salt_pos++;
12948
12949 char *hash_pos = strchr (salt_pos, '$');
12950
12951 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12952
12953 uint salt_len = hash_pos - salt_pos;
12954
12955 if (salt_len < 16) return (PARSER_SALT_LENGTH);
12956
12957 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
12958
12959 salt->salt_len = salt_len;
12960
12961 char salt_iter[3] = { iter_pos[0], iter_pos[1], 0 };
12962
12963 salt->salt_sign[0] = atoi (salt_iter);
12964
12965 salt->salt_iter = (1 << atoi (salt_iter)) - 1;
12966
12967 hash_pos++;
12968
12969 sha256aix_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
12970
12971 digest[0] = byte_swap_32 (digest[0]);
12972 digest[1] = byte_swap_32 (digest[1]);
12973 digest[2] = byte_swap_32 (digest[2]);
12974 digest[3] = byte_swap_32 (digest[3]);
12975 digest[4] = byte_swap_32 (digest[4]);
12976 digest[5] = byte_swap_32 (digest[5]);
12977 digest[6] = byte_swap_32 (digest[6]);
12978 digest[7] = byte_swap_32 (digest[7]);
12979
12980 return (PARSER_OK);
12981 }
12982
12983 int sha512aix_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12984 {
12985 if ((input_len < DISPLAY_LEN_MIN_6500) || (input_len > DISPLAY_LEN_MAX_6500)) return (PARSER_GLOBAL_LENGTH);
12986
12987 if (memcmp (SIGNATURE_SHA512AIX, input_buf, 9)) return (PARSER_SIGNATURE_UNMATCHED);
12988
12989 u64 *digest = (u64 *) hash_buf->digest;
12990
12991 salt_t *salt = hash_buf->salt;
12992
12993 char *iter_pos = input_buf + 9;
12994
12995 char *salt_pos = strchr (iter_pos, '$');
12996
12997 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12998
12999 salt_pos++;
13000
13001 char *hash_pos = strchr (salt_pos, '$');
13002
13003 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13004
13005 uint salt_len = hash_pos - salt_pos;
13006
13007 if (salt_len < 16) return (PARSER_SALT_LENGTH);
13008
13009 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
13010
13011 salt->salt_len = salt_len;
13012
13013 char salt_iter[3] = { iter_pos[0], iter_pos[1], 0 };
13014
13015 salt->salt_sign[0] = atoi (salt_iter);
13016
13017 salt->salt_iter = (1 << atoi (salt_iter)) - 1;
13018
13019 hash_pos++;
13020
13021 sha512aix_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
13022
13023 digest[0] = byte_swap_64 (digest[0]);
13024 digest[1] = byte_swap_64 (digest[1]);
13025 digest[2] = byte_swap_64 (digest[2]);
13026 digest[3] = byte_swap_64 (digest[3]);
13027 digest[4] = byte_swap_64 (digest[4]);
13028 digest[5] = byte_swap_64 (digest[5]);
13029 digest[6] = byte_swap_64 (digest[6]);
13030 digest[7] = byte_swap_64 (digest[7]);
13031
13032 return (PARSER_OK);
13033 }
13034
13035 int agilekey_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13036 {
13037 if ((input_len < DISPLAY_LEN_MIN_6600) || (input_len > DISPLAY_LEN_MAX_6600)) return (PARSER_GLOBAL_LENGTH);
13038
13039 u32 *digest = (u32 *) hash_buf->digest;
13040
13041 salt_t *salt = hash_buf->salt;
13042
13043 agilekey_t *agilekey = (agilekey_t *) hash_buf->esalt;
13044
13045 /**
13046 * parse line
13047 */
13048
13049 char *iterations_pos = input_buf;
13050
13051 char *saltbuf_pos = strchr (iterations_pos, ':');
13052
13053 if (saltbuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13054
13055 uint iterations_len = saltbuf_pos - iterations_pos;
13056
13057 if (iterations_len > 6) return (PARSER_SALT_LENGTH);
13058
13059 saltbuf_pos++;
13060
13061 char *cipherbuf_pos = strchr (saltbuf_pos, ':');
13062
13063 if (cipherbuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13064
13065 uint saltbuf_len = cipherbuf_pos - saltbuf_pos;
13066
13067 if (saltbuf_len != 16) return (PARSER_SALT_LENGTH);
13068
13069 uint cipherbuf_len = input_len - iterations_len - 1 - saltbuf_len - 1;
13070
13071 if (cipherbuf_len != 2080) return (PARSER_HASH_LENGTH);
13072
13073 cipherbuf_pos++;
13074
13075 /**
13076 * pbkdf2 iterations
13077 */
13078
13079 salt->salt_iter = atoi (iterations_pos) - 1;
13080
13081 /**
13082 * handle salt encoding
13083 */
13084
13085 char *saltbuf_ptr = (char *) salt->salt_buf;
13086
13087 for (uint i = 0; i < saltbuf_len; i += 2)
13088 {
13089 const char p0 = saltbuf_pos[i + 0];
13090 const char p1 = saltbuf_pos[i + 1];
13091
13092 *saltbuf_ptr++ = hex_convert (p1) << 0
13093 | hex_convert (p0) << 4;
13094 }
13095
13096 salt->salt_len = saltbuf_len / 2;
13097
13098 /**
13099 * handle cipher encoding
13100 */
13101
13102 uint *tmp = (uint *) mymalloc (32);
13103
13104 char *cipherbuf_ptr = (char *) tmp;
13105
13106 for (uint i = 2016; i < cipherbuf_len; i += 2)
13107 {
13108 const char p0 = cipherbuf_pos[i + 0];
13109 const char p1 = cipherbuf_pos[i + 1];
13110
13111 *cipherbuf_ptr++ = hex_convert (p1) << 0
13112 | hex_convert (p0) << 4;
13113 }
13114
13115 // iv is stored at salt_buf 4 (length 16)
13116 // data is stored at salt_buf 8 (length 16)
13117
13118 salt->salt_buf[ 4] = byte_swap_32 (tmp[0]);
13119 salt->salt_buf[ 5] = byte_swap_32 (tmp[1]);
13120 salt->salt_buf[ 6] = byte_swap_32 (tmp[2]);
13121 salt->salt_buf[ 7] = byte_swap_32 (tmp[3]);
13122
13123 salt->salt_buf[ 8] = byte_swap_32 (tmp[4]);
13124 salt->salt_buf[ 9] = byte_swap_32 (tmp[5]);
13125 salt->salt_buf[10] = byte_swap_32 (tmp[6]);
13126 salt->salt_buf[11] = byte_swap_32 (tmp[7]);
13127
13128 free (tmp);
13129
13130 for (uint i = 0, j = 0; i < 1040; i += 1, j += 2)
13131 {
13132 const char p0 = cipherbuf_pos[j + 0];
13133 const char p1 = cipherbuf_pos[j + 1];
13134
13135 agilekey->cipher[i] = hex_convert (p1) << 0
13136 | hex_convert (p0) << 4;
13137 }
13138
13139 /**
13140 * digest buf
13141 */
13142
13143 digest[0] = 0x10101010;
13144 digest[1] = 0x10101010;
13145 digest[2] = 0x10101010;
13146 digest[3] = 0x10101010;
13147
13148 return (PARSER_OK);
13149 }
13150
13151 int lastpass_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13152 {
13153 if ((input_len < DISPLAY_LEN_MIN_6800) || (input_len > DISPLAY_LEN_MAX_6800)) return (PARSER_GLOBAL_LENGTH);
13154
13155 u32 *digest = (u32 *) hash_buf->digest;
13156
13157 salt_t *salt = hash_buf->salt;
13158
13159 char *hashbuf_pos = input_buf;
13160
13161 char *iterations_pos = strchr (hashbuf_pos, ':');
13162
13163 if (iterations_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13164
13165 uint hash_len = iterations_pos - hashbuf_pos;
13166
13167 if ((hash_len != 32) && (hash_len != 64)) return (PARSER_HASH_LENGTH);
13168
13169 iterations_pos++;
13170
13171 char *saltbuf_pos = strchr (iterations_pos, ':');
13172
13173 if (saltbuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13174
13175 uint iterations_len = saltbuf_pos - iterations_pos;
13176
13177 saltbuf_pos++;
13178
13179 uint salt_len = input_len - hash_len - 1 - iterations_len - 1;
13180
13181 if (salt_len > 32) return (PARSER_SALT_LENGTH);
13182
13183 char *salt_buf_ptr = (char *) salt->salt_buf;
13184
13185 salt_len = parse_and_store_salt (salt_buf_ptr, saltbuf_pos, salt_len);
13186
13187 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
13188
13189 salt->salt_len = salt_len;
13190
13191 salt->salt_iter = atoi (iterations_pos) - 1;
13192
13193 digest[0] = hex_to_u32 ((const u8 *) &hashbuf_pos[ 0]);
13194 digest[1] = hex_to_u32 ((const u8 *) &hashbuf_pos[ 8]);
13195 digest[2] = hex_to_u32 ((const u8 *) &hashbuf_pos[16]);
13196 digest[3] = hex_to_u32 ((const u8 *) &hashbuf_pos[24]);
13197
13198 return (PARSER_OK);
13199 }
13200
13201 int gost_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13202 {
13203 if ((input_len < DISPLAY_LEN_MIN_6900) || (input_len > DISPLAY_LEN_MAX_6900)) return (PARSER_GLOBAL_LENGTH);
13204
13205 u32 *digest = (u32 *) hash_buf->digest;
13206
13207 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
13208 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
13209 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
13210 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
13211 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
13212 digest[5] = hex_to_u32 ((const u8 *) &input_buf[40]);
13213 digest[6] = hex_to_u32 ((const u8 *) &input_buf[48]);
13214 digest[7] = hex_to_u32 ((const u8 *) &input_buf[56]);
13215
13216 digest[0] = byte_swap_32 (digest[0]);
13217 digest[1] = byte_swap_32 (digest[1]);
13218 digest[2] = byte_swap_32 (digest[2]);
13219 digest[3] = byte_swap_32 (digest[3]);
13220 digest[4] = byte_swap_32 (digest[4]);
13221 digest[5] = byte_swap_32 (digest[5]);
13222 digest[6] = byte_swap_32 (digest[6]);
13223 digest[7] = byte_swap_32 (digest[7]);
13224
13225 return (PARSER_OK);
13226 }
13227
13228 int sha256crypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13229 {
13230 if (memcmp (SIGNATURE_SHA256CRYPT, input_buf, 3)) return (PARSER_SIGNATURE_UNMATCHED);
13231
13232 u32 *digest = (u32 *) hash_buf->digest;
13233
13234 salt_t *salt = hash_buf->salt;
13235
13236 char *salt_pos = input_buf + 3;
13237
13238 uint iterations_len = 0;
13239
13240 if (memcmp (salt_pos, "rounds=", 7) == 0)
13241 {
13242 salt_pos += 7;
13243
13244 for (iterations_len = 0; salt_pos[0] >= '0' && salt_pos[0] <= '9' && iterations_len < 7; iterations_len++, salt_pos += 1) continue;
13245
13246 if (iterations_len == 0 ) return (PARSER_SALT_ITERATION);
13247 if (salt_pos[0] != '$') return (PARSER_SIGNATURE_UNMATCHED);
13248
13249 salt_pos[0] = 0x0;
13250
13251 salt->salt_iter = atoi (salt_pos - iterations_len);
13252
13253 salt_pos += 1;
13254
13255 iterations_len += 8;
13256 }
13257 else
13258 {
13259 salt->salt_iter = ROUNDS_SHA256CRYPT;
13260 }
13261
13262 if ((input_len < DISPLAY_LEN_MIN_7400) || (input_len > DISPLAY_LEN_MAX_7400 + iterations_len)) return (PARSER_GLOBAL_LENGTH);
13263
13264 char *hash_pos = strchr (salt_pos, '$');
13265
13266 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13267
13268 uint salt_len = hash_pos - salt_pos;
13269
13270 if (salt_len > 16) return (PARSER_SALT_LENGTH);
13271
13272 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
13273
13274 salt->salt_len = salt_len;
13275
13276 hash_pos++;
13277
13278 sha256crypt_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
13279
13280 return (PARSER_OK);
13281 }
13282
13283 int sha512osx_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13284 {
13285 uint max_len = DISPLAY_LEN_MAX_7100 + (2 * 128);
13286
13287 if ((input_len < DISPLAY_LEN_MIN_7100) || (input_len > max_len)) return (PARSER_GLOBAL_LENGTH);
13288
13289 if (memcmp (SIGNATURE_SHA512OSX, input_buf, 4)) return (PARSER_SIGNATURE_UNMATCHED);
13290
13291 u64 *digest = (u64 *) hash_buf->digest;
13292
13293 salt_t *salt = hash_buf->salt;
13294
13295 pbkdf2_sha512_t *pbkdf2_sha512 = (pbkdf2_sha512_t *) hash_buf->esalt;
13296
13297 char *iter_pos = input_buf + 4;
13298
13299 char *salt_pos = strchr (iter_pos, '$');
13300
13301 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13302
13303 salt_pos++;
13304
13305 char *hash_pos = strchr (salt_pos, '$');
13306
13307 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13308
13309 if (((input_len - (hash_pos - input_buf) - 1) % 128) != 0) return (PARSER_GLOBAL_LENGTH);
13310
13311 hash_pos++;
13312
13313 digest[0] = hex_to_u64 ((const u8 *) &hash_pos[ 0]);
13314 digest[1] = hex_to_u64 ((const u8 *) &hash_pos[ 16]);
13315 digest[2] = hex_to_u64 ((const u8 *) &hash_pos[ 32]);
13316 digest[3] = hex_to_u64 ((const u8 *) &hash_pos[ 48]);
13317 digest[4] = hex_to_u64 ((const u8 *) &hash_pos[ 64]);
13318 digest[5] = hex_to_u64 ((const u8 *) &hash_pos[ 80]);
13319 digest[6] = hex_to_u64 ((const u8 *) &hash_pos[ 96]);
13320 digest[7] = hex_to_u64 ((const u8 *) &hash_pos[112]);
13321
13322 uint salt_len = hash_pos - salt_pos - 1;
13323
13324 if ((salt_len % 2) != 0) return (PARSER_SALT_LENGTH);
13325
13326 salt->salt_len = salt_len / 2;
13327
13328 pbkdf2_sha512->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_pos[ 0]);
13329 pbkdf2_sha512->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_pos[ 8]);
13330 pbkdf2_sha512->salt_buf[2] = hex_to_u32 ((const u8 *) &salt_pos[16]);
13331 pbkdf2_sha512->salt_buf[3] = hex_to_u32 ((const u8 *) &salt_pos[24]);
13332 pbkdf2_sha512->salt_buf[4] = hex_to_u32 ((const u8 *) &salt_pos[32]);
13333 pbkdf2_sha512->salt_buf[5] = hex_to_u32 ((const u8 *) &salt_pos[40]);
13334 pbkdf2_sha512->salt_buf[6] = hex_to_u32 ((const u8 *) &salt_pos[48]);
13335 pbkdf2_sha512->salt_buf[7] = hex_to_u32 ((const u8 *) &salt_pos[56]);
13336
13337 pbkdf2_sha512->salt_buf[0] = byte_swap_32 (pbkdf2_sha512->salt_buf[0]);
13338 pbkdf2_sha512->salt_buf[1] = byte_swap_32 (pbkdf2_sha512->salt_buf[1]);
13339 pbkdf2_sha512->salt_buf[2] = byte_swap_32 (pbkdf2_sha512->salt_buf[2]);
13340 pbkdf2_sha512->salt_buf[3] = byte_swap_32 (pbkdf2_sha512->salt_buf[3]);
13341 pbkdf2_sha512->salt_buf[4] = byte_swap_32 (pbkdf2_sha512->salt_buf[4]);
13342 pbkdf2_sha512->salt_buf[5] = byte_swap_32 (pbkdf2_sha512->salt_buf[5]);
13343 pbkdf2_sha512->salt_buf[6] = byte_swap_32 (pbkdf2_sha512->salt_buf[6]);
13344 pbkdf2_sha512->salt_buf[7] = byte_swap_32 (pbkdf2_sha512->salt_buf[7]);
13345 pbkdf2_sha512->salt_buf[8] = 0x01000000;
13346 pbkdf2_sha512->salt_buf[9] = 0x80;
13347
13348 salt->salt_buf[0] = pbkdf2_sha512->salt_buf[0];
13349
13350 salt->salt_iter = atoi (iter_pos) - 1;
13351
13352 return (PARSER_OK);
13353 }
13354
13355 int episerver4_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13356 {
13357 if ((input_len < DISPLAY_LEN_MIN_1441) || (input_len > DISPLAY_LEN_MAX_1441)) return (PARSER_GLOBAL_LENGTH);
13358
13359 if (memcmp (SIGNATURE_EPISERVER4, input_buf, 14)) return (PARSER_SIGNATURE_UNMATCHED);
13360
13361 u32 *digest = (u32 *) hash_buf->digest;
13362
13363 salt_t *salt = hash_buf->salt;
13364
13365 char *salt_pos = input_buf + 14;
13366
13367 char *hash_pos = strchr (salt_pos, '*');
13368
13369 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13370
13371 hash_pos++;
13372
13373 uint salt_len = hash_pos - salt_pos - 1;
13374
13375 char *salt_buf_ptr = (char *) salt->salt_buf;
13376
13377 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
13378
13379 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
13380
13381 salt->salt_len = salt_len;
13382
13383 u8 tmp_buf[100] = { 0 };
13384
13385 base64_decode (base64_to_int, (const u8 *) hash_pos, 43, tmp_buf);
13386
13387 memcpy (digest, tmp_buf, 32);
13388
13389 digest[0] = byte_swap_32 (digest[0]);
13390 digest[1] = byte_swap_32 (digest[1]);
13391 digest[2] = byte_swap_32 (digest[2]);
13392 digest[3] = byte_swap_32 (digest[3]);
13393 digest[4] = byte_swap_32 (digest[4]);
13394 digest[5] = byte_swap_32 (digest[5]);
13395 digest[6] = byte_swap_32 (digest[6]);
13396 digest[7] = byte_swap_32 (digest[7]);
13397
13398 digest[0] -= SHA256M_A;
13399 digest[1] -= SHA256M_B;
13400 digest[2] -= SHA256M_C;
13401 digest[3] -= SHA256M_D;
13402 digest[4] -= SHA256M_E;
13403 digest[5] -= SHA256M_F;
13404 digest[6] -= SHA256M_G;
13405 digest[7] -= SHA256M_H;
13406
13407 return (PARSER_OK);
13408 }
13409
13410 int sha512grub_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13411 {
13412 uint max_len = DISPLAY_LEN_MAX_7200 + (8 * 128);
13413
13414 if ((input_len < DISPLAY_LEN_MIN_7200) || (input_len > max_len)) return (PARSER_GLOBAL_LENGTH);
13415
13416 if (memcmp (SIGNATURE_SHA512GRUB, input_buf, 19)) return (PARSER_SIGNATURE_UNMATCHED);
13417
13418 u64 *digest = (u64 *) hash_buf->digest;
13419
13420 salt_t *salt = hash_buf->salt;
13421
13422 pbkdf2_sha512_t *pbkdf2_sha512 = (pbkdf2_sha512_t *) hash_buf->esalt;
13423
13424 char *iter_pos = input_buf + 19;
13425
13426 char *salt_pos = strchr (iter_pos, '.');
13427
13428 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13429
13430 salt_pos++;
13431
13432 char *hash_pos = strchr (salt_pos, '.');
13433
13434 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13435
13436 if (((input_len - (hash_pos - input_buf) - 1) % 128) != 0) return (PARSER_GLOBAL_LENGTH);
13437
13438 hash_pos++;
13439
13440 digest[0] = hex_to_u64 ((const u8 *) &hash_pos[ 0]);
13441 digest[1] = hex_to_u64 ((const u8 *) &hash_pos[ 16]);
13442 digest[2] = hex_to_u64 ((const u8 *) &hash_pos[ 32]);
13443 digest[3] = hex_to_u64 ((const u8 *) &hash_pos[ 48]);
13444 digest[4] = hex_to_u64 ((const u8 *) &hash_pos[ 64]);
13445 digest[5] = hex_to_u64 ((const u8 *) &hash_pos[ 80]);
13446 digest[6] = hex_to_u64 ((const u8 *) &hash_pos[ 96]);
13447 digest[7] = hex_to_u64 ((const u8 *) &hash_pos[112]);
13448
13449 uint salt_len = hash_pos - salt_pos - 1;
13450
13451 salt_len /= 2;
13452
13453 char *salt_buf_ptr = (char *) pbkdf2_sha512->salt_buf;
13454
13455 uint i;
13456
13457 for (i = 0; i < salt_len; i++)
13458 {
13459 salt_buf_ptr[i] = hex_to_u8 ((const u8 *) &salt_pos[i * 2]);
13460 }
13461
13462 salt_buf_ptr[salt_len + 3] = 0x01;
13463 salt_buf_ptr[salt_len + 4] = 0x80;
13464
13465 salt->salt_buf[0] = pbkdf2_sha512->salt_buf[0];
13466
13467 salt->salt_len = salt_len;
13468
13469 salt->salt_iter = atoi (iter_pos) - 1;
13470
13471 return (PARSER_OK);
13472 }
13473
13474 int sha512b64s_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13475 {
13476 if ((input_len < DISPLAY_LEN_MIN_1711) || (input_len > DISPLAY_LEN_MAX_1711)) return (PARSER_GLOBAL_LENGTH);
13477
13478 if (memcmp (SIGNATURE_SHA512B64S, input_buf, 9)) return (PARSER_SIGNATURE_UNMATCHED);
13479
13480 u64 *digest = (u64 *) hash_buf->digest;
13481
13482 salt_t *salt = hash_buf->salt;
13483
13484 u8 tmp_buf[120] = { 0 };
13485
13486 int tmp_len = base64_decode (base64_to_int, (const u8 *) input_buf + 9, input_len - 9, tmp_buf);
13487
13488 if (tmp_len < 64) return (PARSER_HASH_LENGTH);
13489
13490 memcpy (digest, tmp_buf, 64);
13491
13492 digest[0] = byte_swap_64 (digest[0]);
13493 digest[1] = byte_swap_64 (digest[1]);
13494 digest[2] = byte_swap_64 (digest[2]);
13495 digest[3] = byte_swap_64 (digest[3]);
13496 digest[4] = byte_swap_64 (digest[4]);
13497 digest[5] = byte_swap_64 (digest[5]);
13498 digest[6] = byte_swap_64 (digest[6]);
13499 digest[7] = byte_swap_64 (digest[7]);
13500
13501 digest[0] -= SHA512M_A;
13502 digest[1] -= SHA512M_B;
13503 digest[2] -= SHA512M_C;
13504 digest[3] -= SHA512M_D;
13505 digest[4] -= SHA512M_E;
13506 digest[5] -= SHA512M_F;
13507 digest[6] -= SHA512M_G;
13508 digest[7] -= SHA512M_H;
13509
13510 int salt_len = tmp_len - 64;
13511
13512 if (salt_len < 0) return (PARSER_SALT_LENGTH);
13513
13514 salt->salt_len = salt_len;
13515
13516 memcpy (salt->salt_buf, tmp_buf + 64, salt->salt_len);
13517
13518 if (data.opts_type & OPTS_TYPE_ST_ADD80)
13519 {
13520 char *ptr = (char *) salt->salt_buf;
13521
13522 ptr[salt->salt_len] = 0x80;
13523 }
13524
13525 return (PARSER_OK);
13526 }
13527
13528 int hmacmd5_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13529 {
13530 if (data.opts_type & OPTS_TYPE_ST_HEX)
13531 {
13532 if ((input_len < DISPLAY_LEN_MIN_50H) || (input_len > DISPLAY_LEN_MAX_50H)) return (PARSER_GLOBAL_LENGTH);
13533 }
13534 else
13535 {
13536 if ((input_len < DISPLAY_LEN_MIN_50) || (input_len > DISPLAY_LEN_MAX_50)) return (PARSER_GLOBAL_LENGTH);
13537 }
13538
13539 u32 *digest = (u32 *) hash_buf->digest;
13540
13541 salt_t *salt = hash_buf->salt;
13542
13543 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
13544 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
13545 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
13546 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
13547
13548 digest[0] = byte_swap_32 (digest[0]);
13549 digest[1] = byte_swap_32 (digest[1]);
13550 digest[2] = byte_swap_32 (digest[2]);
13551 digest[3] = byte_swap_32 (digest[3]);
13552
13553 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
13554
13555 uint salt_len = input_len - 32 - 1;
13556
13557 char *salt_buf = input_buf + 32 + 1;
13558
13559 char *salt_buf_ptr = (char *) salt->salt_buf;
13560
13561 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
13562
13563 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
13564
13565 salt->salt_len = salt_len;
13566
13567 return (PARSER_OK);
13568 }
13569
13570 int hmacsha1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13571 {
13572 if (data.opts_type & OPTS_TYPE_ST_HEX)
13573 {
13574 if ((input_len < DISPLAY_LEN_MIN_150H) || (input_len > DISPLAY_LEN_MAX_150H)) return (PARSER_GLOBAL_LENGTH);
13575 }
13576 else
13577 {
13578 if ((input_len < DISPLAY_LEN_MIN_150) || (input_len > DISPLAY_LEN_MAX_150)) return (PARSER_GLOBAL_LENGTH);
13579 }
13580
13581 u32 *digest = (u32 *) hash_buf->digest;
13582
13583 salt_t *salt = hash_buf->salt;
13584
13585 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
13586 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
13587 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
13588 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
13589 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
13590
13591 if (input_buf[40] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
13592
13593 uint salt_len = input_len - 40 - 1;
13594
13595 char *salt_buf = input_buf + 40 + 1;
13596
13597 char *salt_buf_ptr = (char *) salt->salt_buf;
13598
13599 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
13600
13601 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
13602
13603 salt->salt_len = salt_len;
13604
13605 return (PARSER_OK);
13606 }
13607
13608 int hmacsha256_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13609 {
13610 if (data.opts_type & OPTS_TYPE_ST_HEX)
13611 {
13612 if ((input_len < DISPLAY_LEN_MIN_1450H) || (input_len > DISPLAY_LEN_MAX_1450H)) return (PARSER_GLOBAL_LENGTH);
13613 }
13614 else
13615 {
13616 if ((input_len < DISPLAY_LEN_MIN_1450) || (input_len > DISPLAY_LEN_MAX_1450)) return (PARSER_GLOBAL_LENGTH);
13617 }
13618
13619 u32 *digest = (u32 *) hash_buf->digest;
13620
13621 salt_t *salt = hash_buf->salt;
13622
13623 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
13624 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
13625 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
13626 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
13627 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
13628 digest[5] = hex_to_u32 ((const u8 *) &input_buf[40]);
13629 digest[6] = hex_to_u32 ((const u8 *) &input_buf[48]);
13630 digest[7] = hex_to_u32 ((const u8 *) &input_buf[56]);
13631
13632 if (input_buf[64] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
13633
13634 uint salt_len = input_len - 64 - 1;
13635
13636 char *salt_buf = input_buf + 64 + 1;
13637
13638 char *salt_buf_ptr = (char *) salt->salt_buf;
13639
13640 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
13641
13642 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
13643
13644 salt->salt_len = salt_len;
13645
13646 return (PARSER_OK);
13647 }
13648
13649 int hmacsha512_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13650 {
13651 if (data.opts_type & OPTS_TYPE_ST_HEX)
13652 {
13653 if ((input_len < DISPLAY_LEN_MIN_1750H) || (input_len > DISPLAY_LEN_MAX_1750H)) return (PARSER_GLOBAL_LENGTH);
13654 }
13655 else
13656 {
13657 if ((input_len < DISPLAY_LEN_MIN_1750) || (input_len > DISPLAY_LEN_MAX_1750)) return (PARSER_GLOBAL_LENGTH);
13658 }
13659
13660 u64 *digest = (u64 *) hash_buf->digest;
13661
13662 salt_t *salt = hash_buf->salt;
13663
13664 digest[0] = hex_to_u64 ((const u8 *) &input_buf[ 0]);
13665 digest[1] = hex_to_u64 ((const u8 *) &input_buf[ 16]);
13666 digest[2] = hex_to_u64 ((const u8 *) &input_buf[ 32]);
13667 digest[3] = hex_to_u64 ((const u8 *) &input_buf[ 48]);
13668 digest[4] = hex_to_u64 ((const u8 *) &input_buf[ 64]);
13669 digest[5] = hex_to_u64 ((const u8 *) &input_buf[ 80]);
13670 digest[6] = hex_to_u64 ((const u8 *) &input_buf[ 96]);
13671 digest[7] = hex_to_u64 ((const u8 *) &input_buf[112]);
13672
13673 if (input_buf[128] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
13674
13675 uint salt_len = input_len - 128 - 1;
13676
13677 char *salt_buf = input_buf + 128 + 1;
13678
13679 char *salt_buf_ptr = (char *) salt->salt_buf;
13680
13681 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
13682
13683 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
13684
13685 salt->salt_len = salt_len;
13686
13687 return (PARSER_OK);
13688 }
13689
13690 int krb5pa_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13691 {
13692 if ((input_len < DISPLAY_LEN_MIN_7500) || (input_len > DISPLAY_LEN_MAX_7500)) return (PARSER_GLOBAL_LENGTH);
13693
13694 if (memcmp (SIGNATURE_KRB5PA, input_buf, 10)) return (PARSER_SIGNATURE_UNMATCHED);
13695
13696 u32 *digest = (u32 *) hash_buf->digest;
13697
13698 salt_t *salt = hash_buf->salt;
13699
13700 krb5pa_t *krb5pa = (krb5pa_t *) hash_buf->esalt;
13701
13702 /**
13703 * parse line
13704 */
13705
13706 char *user_pos = input_buf + 10 + 1;
13707
13708 char *realm_pos = strchr (user_pos, '$');
13709
13710 if (realm_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13711
13712 uint user_len = realm_pos - user_pos;
13713
13714 if (user_len >= 64) return (PARSER_SALT_LENGTH);
13715
13716 realm_pos++;
13717
13718 char *salt_pos = strchr (realm_pos, '$');
13719
13720 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13721
13722 uint realm_len = salt_pos - realm_pos;
13723
13724 if (realm_len >= 64) return (PARSER_SALT_LENGTH);
13725
13726 salt_pos++;
13727
13728 char *data_pos = strchr (salt_pos, '$');
13729
13730 if (data_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13731
13732 uint salt_len = data_pos - salt_pos;
13733
13734 if (salt_len >= 128) return (PARSER_SALT_LENGTH);
13735
13736 data_pos++;
13737
13738 uint data_len = input_len - 10 - 1 - user_len - 1 - realm_len - 1 - salt_len - 1;
13739
13740 if (data_len != ((36 + 16) * 2)) return (PARSER_SALT_LENGTH);
13741
13742 /**
13743 * copy data
13744 */
13745
13746 memcpy (krb5pa->user, user_pos, user_len);
13747 memcpy (krb5pa->realm, realm_pos, realm_len);
13748 memcpy (krb5pa->salt, salt_pos, salt_len);
13749
13750 char *timestamp_ptr = (char *) krb5pa->timestamp;
13751
13752 for (uint i = 0; i < (36 * 2); i += 2)
13753 {
13754 const char p0 = data_pos[i + 0];
13755 const char p1 = data_pos[i + 1];
13756
13757 *timestamp_ptr++ = hex_convert (p1) << 0
13758 | hex_convert (p0) << 4;
13759 }
13760
13761 char *checksum_ptr = (char *) krb5pa->checksum;
13762
13763 for (uint i = (36 * 2); i < ((36 + 16) * 2); i += 2)
13764 {
13765 const char p0 = data_pos[i + 0];
13766 const char p1 = data_pos[i + 1];
13767
13768 *checksum_ptr++ = hex_convert (p1) << 0
13769 | hex_convert (p0) << 4;
13770 }
13771
13772 /**
13773 * copy some data to generic buffers to make sorting happy
13774 */
13775
13776 salt->salt_buf[0] = krb5pa->timestamp[0];
13777 salt->salt_buf[1] = krb5pa->timestamp[1];
13778 salt->salt_buf[2] = krb5pa->timestamp[2];
13779 salt->salt_buf[3] = krb5pa->timestamp[3];
13780 salt->salt_buf[4] = krb5pa->timestamp[4];
13781 salt->salt_buf[5] = krb5pa->timestamp[5];
13782 salt->salt_buf[6] = krb5pa->timestamp[6];
13783 salt->salt_buf[7] = krb5pa->timestamp[7];
13784 salt->salt_buf[8] = krb5pa->timestamp[8];
13785
13786 salt->salt_len = 36;
13787
13788 digest[0] = krb5pa->checksum[0];
13789 digest[1] = krb5pa->checksum[1];
13790 digest[2] = krb5pa->checksum[2];
13791 digest[3] = krb5pa->checksum[3];
13792
13793 return (PARSER_OK);
13794 }
13795
13796 int sapb_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13797 {
13798 if ((input_len < DISPLAY_LEN_MIN_7700) || (input_len > DISPLAY_LEN_MAX_7700)) return (PARSER_GLOBAL_LENGTH);
13799
13800 u32 *digest = (u32 *) hash_buf->digest;
13801
13802 salt_t *salt = hash_buf->salt;
13803
13804 /**
13805 * parse line
13806 */
13807
13808 char *salt_pos = input_buf;
13809
13810 char *hash_pos = strchr (salt_pos, '$');
13811
13812 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13813
13814 uint salt_len = hash_pos - salt_pos;
13815
13816 if (salt_len >= 40) return (PARSER_SALT_LENGTH);
13817
13818 hash_pos++;
13819
13820 uint hash_len = input_len - 1 - salt_len;
13821
13822 if (hash_len != 16) return (PARSER_HASH_LENGTH);
13823
13824 /**
13825 * valid some data
13826 */
13827
13828 uint user_len = 0;
13829
13830 for (uint i = 0; i < salt_len; i++)
13831 {
13832 if (salt_pos[i] == ' ') continue;
13833
13834 user_len++;
13835 }
13836
13837 // SAP user names cannot be longer than 12 characters
13838 if (user_len > 12) return (PARSER_SALT_LENGTH);
13839
13840 // SAP user name cannot start with ! or ?
13841 if (salt_pos[0] == '!' || salt_pos[0] == '?') return (PARSER_SALT_VALUE);
13842
13843 /**
13844 * copy data
13845 */
13846
13847 char *salt_buf_ptr = (char *) salt->salt_buf;
13848
13849 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
13850
13851 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
13852
13853 salt->salt_len = salt_len;
13854
13855 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[0]);
13856 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[8]);
13857 digest[2] = 0;
13858 digest[3] = 0;
13859
13860 digest[0] = byte_swap_32 (digest[0]);
13861 digest[1] = byte_swap_32 (digest[1]);
13862
13863 return (PARSER_OK);
13864 }
13865
13866 int sapg_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13867 {
13868 if ((input_len < DISPLAY_LEN_MIN_7800) || (input_len > DISPLAY_LEN_MAX_7800)) return (PARSER_GLOBAL_LENGTH);
13869
13870 u32 *digest = (u32 *) hash_buf->digest;
13871
13872 salt_t *salt = hash_buf->salt;
13873
13874 /**
13875 * parse line
13876 */
13877
13878 char *salt_pos = input_buf;
13879
13880 char *hash_pos = strchr (salt_pos, '$');
13881
13882 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13883
13884 uint salt_len = hash_pos - salt_pos;
13885
13886 if (salt_len >= 40) return (PARSER_SALT_LENGTH);
13887
13888 hash_pos++;
13889
13890 uint hash_len = input_len - 1 - salt_len;
13891
13892 if (hash_len != 40) return (PARSER_HASH_LENGTH);
13893
13894 /**
13895 * valid some data
13896 */
13897
13898 uint user_len = 0;
13899
13900 for (uint i = 0; i < salt_len; i++)
13901 {
13902 if (salt_pos[i] == ' ') continue;
13903
13904 user_len++;
13905 }
13906
13907 // SAP user names cannot be longer than 12 characters
13908 // this is kinda buggy. if the username is in utf the length can be up to length 12*3
13909 // so far nobody complained so we stay with this because it helps in optimization
13910 // final string can have a max size of 32 (password) + (10 * 5) = lengthMagicArray + 12 (max salt) + 1 (the 0x80)
13911
13912 if (user_len > 12) return (PARSER_SALT_LENGTH);
13913
13914 // SAP user name cannot start with ! or ?
13915 if (salt_pos[0] == '!' || salt_pos[0] == '?') return (PARSER_SALT_VALUE);
13916
13917 /**
13918 * copy data
13919 */
13920
13921 char *salt_buf_ptr = (char *) salt->salt_buf;
13922
13923 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
13924
13925 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
13926
13927 salt->salt_len = salt_len;
13928
13929 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
13930 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
13931 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
13932 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
13933 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
13934
13935 return (PARSER_OK);
13936 }
13937
13938 int drupal7_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13939 {
13940 if ((input_len < DISPLAY_LEN_MIN_7900) || (input_len > DISPLAY_LEN_MAX_7900)) return (PARSER_GLOBAL_LENGTH);
13941
13942 if (memcmp (SIGNATURE_DRUPAL7, input_buf, 3)) return (PARSER_SIGNATURE_UNMATCHED);
13943
13944 u64 *digest = (u64 *) hash_buf->digest;
13945
13946 salt_t *salt = hash_buf->salt;
13947
13948 char *iter_pos = input_buf + 3;
13949
13950 uint salt_iter = 1 << itoa64_to_int (iter_pos[0]);
13951
13952 if (salt_iter > 0x80000000) return (PARSER_SALT_ITERATION);
13953
13954 memcpy ((char *) salt->salt_sign, input_buf, 4);
13955
13956 salt->salt_iter = salt_iter;
13957
13958 char *salt_pos = iter_pos + 1;
13959
13960 uint salt_len = 8;
13961
13962 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
13963
13964 salt->salt_len = salt_len;
13965
13966 char *hash_pos = salt_pos + salt_len;
13967
13968 drupal7_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
13969
13970 // ugly hack start
13971
13972 char *tmp = (char *) salt->salt_buf_pc;
13973
13974 tmp[0] = hash_pos[42];
13975
13976 // ugly hack end
13977
13978 digest[ 0] = byte_swap_64 (digest[ 0]);
13979 digest[ 1] = byte_swap_64 (digest[ 1]);
13980 digest[ 2] = byte_swap_64 (digest[ 2]);
13981 digest[ 3] = byte_swap_64 (digest[ 3]);
13982 digest[ 4] = 0;
13983 digest[ 5] = 0;
13984 digest[ 6] = 0;
13985 digest[ 7] = 0;
13986
13987 return (PARSER_OK);
13988 }
13989
13990 int sybasease_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13991 {
13992 if ((input_len < DISPLAY_LEN_MIN_8000) || (input_len > DISPLAY_LEN_MAX_8000)) return (PARSER_GLOBAL_LENGTH);
13993
13994 if (memcmp (SIGNATURE_SYBASEASE, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
13995
13996 u32 *digest = (u32 *) hash_buf->digest;
13997
13998 salt_t *salt = hash_buf->salt;
13999
14000 char *salt_buf = input_buf + 6;
14001
14002 uint salt_len = 16;
14003
14004 char *salt_buf_ptr = (char *) salt->salt_buf;
14005
14006 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
14007
14008 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
14009
14010 salt->salt_len = salt_len;
14011
14012 char *hash_pos = input_buf + 6 + 16;
14013
14014 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
14015 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
14016 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
14017 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
14018 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
14019 digest[5] = hex_to_u32 ((const u8 *) &hash_pos[40]);
14020 digest[6] = hex_to_u32 ((const u8 *) &hash_pos[48]);
14021 digest[7] = hex_to_u32 ((const u8 *) &hash_pos[56]);
14022
14023 return (PARSER_OK);
14024 }
14025
14026 int mysql323_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14027 {
14028 if ((input_len < DISPLAY_LEN_MIN_200) || (input_len > DISPLAY_LEN_MAX_200)) return (PARSER_GLOBAL_LENGTH);
14029
14030 u32 *digest = (u32 *) hash_buf->digest;
14031
14032 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
14033 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
14034 digest[2] = 0;
14035 digest[3] = 0;
14036
14037 return (PARSER_OK);
14038 }
14039
14040 int rakp_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14041 {
14042 if ((input_len < DISPLAY_LEN_MIN_7300) || (input_len > DISPLAY_LEN_MAX_7300)) return (PARSER_GLOBAL_LENGTH);
14043
14044 u32 *digest = (u32 *) hash_buf->digest;
14045
14046 salt_t *salt = hash_buf->salt;
14047
14048 rakp_t *rakp = (rakp_t *) hash_buf->esalt;
14049
14050 char *saltbuf_pos = input_buf;
14051
14052 char *hashbuf_pos = strchr (saltbuf_pos, ':');
14053
14054 if (hashbuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14055
14056 uint saltbuf_len = hashbuf_pos - saltbuf_pos;
14057
14058 if (saltbuf_len < 64) return (PARSER_SALT_LENGTH);
14059 if (saltbuf_len > 512) return (PARSER_SALT_LENGTH);
14060
14061 if (saltbuf_len & 1) return (PARSER_SALT_LENGTH); // muss gerade sein wegen hex
14062
14063 hashbuf_pos++;
14064
14065 uint hashbuf_len = input_len - saltbuf_len - 1;
14066
14067 if (hashbuf_len != 40) return (PARSER_HASH_LENGTH);
14068
14069 char *salt_ptr = (char *) saltbuf_pos;
14070 char *rakp_ptr = (char *) rakp->salt_buf;
14071
14072 uint i;
14073 uint j;
14074
14075 for (i = 0, j = 0; i < saltbuf_len; i += 2, j += 1)
14076 {
14077 rakp_ptr[j] = hex_to_u8 ((const u8 *) &salt_ptr[i]);
14078 }
14079
14080 rakp_ptr[j] = 0x80;
14081
14082 rakp->salt_len = j;
14083
14084 for (i = 0; i < 64; i++)
14085 {
14086 rakp->salt_buf[i] = byte_swap_32 (rakp->salt_buf[i]);
14087 }
14088
14089 salt->salt_buf[0] = rakp->salt_buf[0];
14090 salt->salt_buf[1] = rakp->salt_buf[1];
14091 salt->salt_buf[2] = rakp->salt_buf[2];
14092 salt->salt_buf[3] = rakp->salt_buf[3];
14093 salt->salt_buf[4] = rakp->salt_buf[4];
14094 salt->salt_buf[5] = rakp->salt_buf[5];
14095 salt->salt_buf[6] = rakp->salt_buf[6];
14096 salt->salt_buf[7] = rakp->salt_buf[7];
14097
14098 salt->salt_len = 32; // muss min. 32 haben
14099
14100 digest[0] = hex_to_u32 ((const u8 *) &hashbuf_pos[ 0]);
14101 digest[1] = hex_to_u32 ((const u8 *) &hashbuf_pos[ 8]);
14102 digest[2] = hex_to_u32 ((const u8 *) &hashbuf_pos[16]);
14103 digest[3] = hex_to_u32 ((const u8 *) &hashbuf_pos[24]);
14104 digest[4] = hex_to_u32 ((const u8 *) &hashbuf_pos[32]);
14105
14106 return (PARSER_OK);
14107 }
14108
14109 int netscaler_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14110 {
14111 if ((input_len < DISPLAY_LEN_MIN_8100) || (input_len > DISPLAY_LEN_MAX_8100)) return (PARSER_GLOBAL_LENGTH);
14112
14113 u32 *digest = (u32 *) hash_buf->digest;
14114
14115 salt_t *salt = hash_buf->salt;
14116
14117 if (memcmp (SIGNATURE_NETSCALER, input_buf, 1)) return (PARSER_SIGNATURE_UNMATCHED);
14118
14119 char *salt_pos = input_buf + 1;
14120
14121 memcpy (salt->salt_buf, salt_pos, 8);
14122
14123 salt->salt_buf[0] = byte_swap_32 (salt->salt_buf[0]);
14124 salt->salt_buf[1] = byte_swap_32 (salt->salt_buf[1]);
14125
14126 salt->salt_len = 8;
14127
14128 char *hash_pos = salt_pos + 8;
14129
14130 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
14131 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
14132 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
14133 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
14134 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
14135
14136 digest[0] -= SHA1M_A;
14137 digest[1] -= SHA1M_B;
14138 digest[2] -= SHA1M_C;
14139 digest[3] -= SHA1M_D;
14140 digest[4] -= SHA1M_E;
14141
14142 return (PARSER_OK);
14143 }
14144
14145 int chap_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14146 {
14147 if ((input_len < DISPLAY_LEN_MIN_4800) || (input_len > DISPLAY_LEN_MAX_4800)) return (PARSER_GLOBAL_LENGTH);
14148
14149 u32 *digest = (u32 *) hash_buf->digest;
14150
14151 salt_t *salt = hash_buf->salt;
14152
14153 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
14154 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
14155 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
14156 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
14157
14158 digest[0] = byte_swap_32 (digest[0]);
14159 digest[1] = byte_swap_32 (digest[1]);
14160 digest[2] = byte_swap_32 (digest[2]);
14161 digest[3] = byte_swap_32 (digest[3]);
14162
14163 digest[0] -= MD5M_A;
14164 digest[1] -= MD5M_B;
14165 digest[2] -= MD5M_C;
14166 digest[3] -= MD5M_D;
14167
14168 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
14169
14170 char *salt_buf_ptr = input_buf + 32 + 1;
14171
14172 u32 *salt_buf = salt->salt_buf;
14173
14174 salt_buf[0] = hex_to_u32 ((const u8 *) &salt_buf_ptr[ 0]);
14175 salt_buf[1] = hex_to_u32 ((const u8 *) &salt_buf_ptr[ 8]);
14176 salt_buf[2] = hex_to_u32 ((const u8 *) &salt_buf_ptr[16]);
14177 salt_buf[3] = hex_to_u32 ((const u8 *) &salt_buf_ptr[24]);
14178
14179 salt_buf[0] = byte_swap_32 (salt_buf[0]);
14180 salt_buf[1] = byte_swap_32 (salt_buf[1]);
14181 salt_buf[2] = byte_swap_32 (salt_buf[2]);
14182 salt_buf[3] = byte_swap_32 (salt_buf[3]);
14183
14184 salt->salt_len = 16 + 1;
14185
14186 if (input_buf[65] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
14187
14188 char *idbyte_buf_ptr = input_buf + 32 + 1 + 32 + 1;
14189
14190 salt_buf[4] = hex_to_u8 ((const u8 *) &idbyte_buf_ptr[0]) & 0xff;
14191
14192 return (PARSER_OK);
14193 }
14194
14195 int cloudkey_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14196 {
14197 if ((input_len < DISPLAY_LEN_MIN_8200) || (input_len > DISPLAY_LEN_MAX_8200)) return (PARSER_GLOBAL_LENGTH);
14198
14199 u32 *digest = (u32 *) hash_buf->digest;
14200
14201 salt_t *salt = hash_buf->salt;
14202
14203 cloudkey_t *cloudkey = (cloudkey_t *) hash_buf->esalt;
14204
14205 /**
14206 * parse line
14207 */
14208
14209 char *hashbuf_pos = input_buf;
14210
14211 char *saltbuf_pos = strchr (hashbuf_pos, ':');
14212
14213 if (saltbuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14214
14215 const uint hashbuf_len = saltbuf_pos - hashbuf_pos;
14216
14217 if (hashbuf_len != 64) return (PARSER_HASH_LENGTH);
14218
14219 saltbuf_pos++;
14220
14221 char *iteration_pos = strchr (saltbuf_pos, ':');
14222
14223 if (iteration_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14224
14225 const uint saltbuf_len = iteration_pos - saltbuf_pos;
14226
14227 if (saltbuf_len != 32) return (PARSER_SALT_LENGTH);
14228
14229 iteration_pos++;
14230
14231 char *databuf_pos = strchr (iteration_pos, ':');
14232
14233 if (databuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14234
14235 const uint iteration_len = databuf_pos - iteration_pos;
14236
14237 if (iteration_len < 1) return (PARSER_SALT_ITERATION);
14238 if (iteration_len > 8) return (PARSER_SALT_ITERATION);
14239
14240 const uint databuf_len = input_len - hashbuf_len - 1 - saltbuf_len - 1 - iteration_len - 1;
14241
14242 if (databuf_len < 1) return (PARSER_SALT_LENGTH);
14243 if (databuf_len > 2048) return (PARSER_SALT_LENGTH);
14244
14245 databuf_pos++;
14246
14247 // digest
14248
14249 digest[0] = hex_to_u32 ((const u8 *) &hashbuf_pos[ 0]);
14250 digest[1] = hex_to_u32 ((const u8 *) &hashbuf_pos[ 8]);
14251 digest[2] = hex_to_u32 ((const u8 *) &hashbuf_pos[16]);
14252 digest[3] = hex_to_u32 ((const u8 *) &hashbuf_pos[24]);
14253 digest[4] = hex_to_u32 ((const u8 *) &hashbuf_pos[32]);
14254 digest[5] = hex_to_u32 ((const u8 *) &hashbuf_pos[40]);
14255 digest[6] = hex_to_u32 ((const u8 *) &hashbuf_pos[48]);
14256 digest[7] = hex_to_u32 ((const u8 *) &hashbuf_pos[56]);
14257
14258 // salt
14259
14260 char *saltbuf_ptr = (char *) salt->salt_buf;
14261
14262 for (uint i = 0; i < saltbuf_len; i += 2)
14263 {
14264 const char p0 = saltbuf_pos[i + 0];
14265 const char p1 = saltbuf_pos[i + 1];
14266
14267 *saltbuf_ptr++ = hex_convert (p1) << 0
14268 | hex_convert (p0) << 4;
14269 }
14270
14271 salt->salt_buf[4] = 0x01000000;
14272 salt->salt_buf[5] = 0x80;
14273
14274 salt->salt_len = saltbuf_len / 2;
14275
14276 // iteration
14277
14278 salt->salt_iter = atoi (iteration_pos) - 1;
14279
14280 // data
14281
14282 char *databuf_ptr = (char *) cloudkey->data_buf;
14283
14284 for (uint i = 0; i < databuf_len; i += 2)
14285 {
14286 const char p0 = databuf_pos[i + 0];
14287 const char p1 = databuf_pos[i + 1];
14288
14289 *databuf_ptr++ = hex_convert (p1) << 0
14290 | hex_convert (p0) << 4;
14291 }
14292
14293 *databuf_ptr++ = 0x80;
14294
14295 for (uint i = 0; i < 512; i++)
14296 {
14297 cloudkey->data_buf[i] = byte_swap_32 (cloudkey->data_buf[i]);
14298 }
14299
14300 cloudkey->data_len = databuf_len / 2;
14301
14302 return (PARSER_OK);
14303 }
14304
14305 int nsec3_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14306 {
14307 if ((input_len < DISPLAY_LEN_MIN_8300) || (input_len > DISPLAY_LEN_MAX_8300)) return (PARSER_GLOBAL_LENGTH);
14308
14309 u32 *digest = (u32 *) hash_buf->digest;
14310
14311 salt_t *salt = hash_buf->salt;
14312
14313 /**
14314 * parse line
14315 */
14316
14317 char *hashbuf_pos = input_buf;
14318
14319 char *domainbuf_pos = strchr (hashbuf_pos, ':');
14320
14321 if (domainbuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14322
14323 const uint hashbuf_len = domainbuf_pos - hashbuf_pos;
14324
14325 if (hashbuf_len != 32) return (PARSER_HASH_LENGTH);
14326
14327 domainbuf_pos++;
14328
14329 if (domainbuf_pos[0] != '.') return (PARSER_SALT_VALUE);
14330
14331 char *saltbuf_pos = strchr (domainbuf_pos, ':');
14332
14333 if (saltbuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14334
14335 const uint domainbuf_len = saltbuf_pos - domainbuf_pos;
14336
14337 if (domainbuf_len >= 32) return (PARSER_SALT_LENGTH);
14338
14339 saltbuf_pos++;
14340
14341 char *iteration_pos = strchr (saltbuf_pos, ':');
14342
14343 if (iteration_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14344
14345 const uint saltbuf_len = iteration_pos - saltbuf_pos;
14346
14347 if (saltbuf_len >= 28) return (PARSER_SALT_LENGTH); // 28 = 32 - 4; 4 = length
14348
14349 if ((domainbuf_len + saltbuf_len) >= 48) return (PARSER_SALT_LENGTH);
14350
14351 iteration_pos++;
14352
14353 const uint iteration_len = input_len - hashbuf_len - 1 - domainbuf_len - 1 - saltbuf_len - 1;
14354
14355 if (iteration_len < 1) return (PARSER_SALT_ITERATION);
14356 if (iteration_len > 5) return (PARSER_SALT_ITERATION);
14357
14358 // ok, the plan for this algorithm is the following:
14359 // we have 2 salts here, the domain-name and a random salt
14360 // while both are used in the initial transformation,
14361 // only the random salt is used in the following iterations
14362 // so we create two buffer, one that includes domain-name (stored into salt_buf_pc[])
14363 // and one that includes only the real salt (stored into salt_buf[]).
14364 // the domain-name length is put into array position 7 of salt_buf_pc[] since there is not salt_pc_len
14365
14366 u8 tmp_buf[100] = { 0 };
14367
14368 base32_decode (itoa32_to_int, (const u8 *) hashbuf_pos, 32, tmp_buf);
14369
14370 memcpy (digest, tmp_buf, 20);
14371
14372 digest[0] = byte_swap_32 (digest[0]);
14373 digest[1] = byte_swap_32 (digest[1]);
14374 digest[2] = byte_swap_32 (digest[2]);
14375 digest[3] = byte_swap_32 (digest[3]);
14376 digest[4] = byte_swap_32 (digest[4]);
14377
14378 // domain
14379
14380 char *salt_buf_pc_ptr = (char *) salt->salt_buf_pc;
14381
14382 memcpy (salt_buf_pc_ptr, domainbuf_pos, domainbuf_len);
14383
14384 char *len_ptr = NULL;
14385
14386 for (uint i = 0; i < domainbuf_len; i++)
14387 {
14388 if (salt_buf_pc_ptr[i] == '.')
14389 {
14390 len_ptr = &salt_buf_pc_ptr[i];
14391
14392 *len_ptr = 0;
14393 }
14394 else
14395 {
14396 *len_ptr += 1;
14397 }
14398 }
14399
14400 salt->salt_buf_pc[7] = domainbuf_len;
14401
14402 // "real" salt
14403
14404 char *salt_buf_ptr = (char *) salt->salt_buf;
14405
14406 const uint salt_len = parse_and_store_salt (salt_buf_ptr, saltbuf_pos, saltbuf_len);
14407
14408 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
14409
14410 salt->salt_len = salt_len;
14411
14412 // iteration
14413
14414 salt->salt_iter = atoi (iteration_pos);
14415
14416 return (PARSER_OK);
14417 }
14418
14419 int wbb3_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14420 {
14421 if ((input_len < DISPLAY_LEN_MIN_8400) || (input_len > DISPLAY_LEN_MAX_8400)) return (PARSER_GLOBAL_LENGTH);
14422
14423 u32 *digest = (u32 *) hash_buf->digest;
14424
14425 salt_t *salt = hash_buf->salt;
14426
14427 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
14428 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
14429 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
14430 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
14431 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
14432
14433 if (input_buf[40] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
14434
14435 uint salt_len = input_len - 40 - 1;
14436
14437 char *salt_buf = input_buf + 40 + 1;
14438
14439 char *salt_buf_ptr = (char *) salt->salt_buf;
14440
14441 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
14442
14443 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
14444
14445 salt->salt_len = salt_len;
14446
14447 return (PARSER_OK);
14448 }
14449
14450 int racf_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14451 {
14452 const u8 ascii_to_ebcdic[] =
14453 {
14454 0x00, 0x01, 0x02, 0x03, 0x37, 0x2d, 0x2e, 0x2f, 0x16, 0x05, 0x25, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
14455 0x10, 0x11, 0x12, 0x13, 0x3c, 0x3d, 0x32, 0x26, 0x18, 0x19, 0x3f, 0x27, 0x1c, 0x1d, 0x1e, 0x1f,
14456 0x40, 0x4f, 0x7f, 0x7b, 0x5b, 0x6c, 0x50, 0x7d, 0x4d, 0x5d, 0x5c, 0x4e, 0x6b, 0x60, 0x4b, 0x61,
14457 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0x7a, 0x5e, 0x4c, 0x7e, 0x6e, 0x6f,
14458 0x7c, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
14459 0xd7, 0xd8, 0xd9, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0x4a, 0xe0, 0x5a, 0x5f, 0x6d,
14460 0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
14461 0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xc0, 0x6a, 0xd0, 0xa1, 0x07,
14462 0x20, 0x21, 0x22, 0x23, 0x24, 0x15, 0x06, 0x17, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x09, 0x0a, 0x1b,
14463 0x30, 0x31, 0x1a, 0x33, 0x34, 0x35, 0x36, 0x08, 0x38, 0x39, 0x3a, 0x3b, 0x04, 0x14, 0x3e, 0xe1,
14464 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
14465 0x58, 0x59, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75,
14466 0x76, 0x77, 0x78, 0x80, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e,
14467 0x9f, 0xa0, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
14468 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xda, 0xdb,
14469 0xdc, 0xdd, 0xde, 0xdf, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
14470 };
14471
14472 if ((input_len < DISPLAY_LEN_MIN_8500) || (input_len > DISPLAY_LEN_MAX_8500)) return (PARSER_GLOBAL_LENGTH);
14473
14474 if (memcmp (SIGNATURE_RACF, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
14475
14476 u32 *digest = (u32 *) hash_buf->digest;
14477
14478 salt_t *salt = hash_buf->salt;
14479
14480 char *salt_pos = input_buf + 6 + 1;
14481
14482 char *digest_pos = strchr (salt_pos, '*');
14483
14484 if (digest_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14485
14486 uint salt_len = digest_pos - salt_pos;
14487
14488 if (salt_len > 8) return (PARSER_SALT_LENGTH);
14489
14490 uint hash_len = input_len - 1 - salt_len - 1 - 6;
14491
14492 if (hash_len != 16) return (PARSER_HASH_LENGTH);
14493
14494 digest_pos++;
14495
14496 char *salt_buf_ptr = (char *) salt->salt_buf;
14497 char *salt_buf_pc_ptr = (char *) salt->salt_buf_pc;
14498
14499 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
14500
14501 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
14502
14503 salt->salt_len = salt_len;
14504
14505 for (uint i = 0; i < salt_len; i++)
14506 {
14507 salt_buf_pc_ptr[i] = ascii_to_ebcdic[(int) salt_buf_ptr[i]];
14508 }
14509 for (uint i = salt_len; i < 8; i++)
14510 {
14511 salt_buf_pc_ptr[i] = 0x40;
14512 }
14513
14514 uint tt;
14515
14516 IP (salt->salt_buf_pc[0], salt->salt_buf_pc[1], tt);
14517
14518 salt->salt_buf_pc[0] = rotl32 (salt->salt_buf_pc[0], 3u);
14519 salt->salt_buf_pc[1] = rotl32 (salt->salt_buf_pc[1], 3u);
14520
14521 digest[0] = hex_to_u32 ((const u8 *) &digest_pos[ 0]);
14522 digest[1] = hex_to_u32 ((const u8 *) &digest_pos[ 8]);
14523
14524 digest[0] = byte_swap_32 (digest[0]);
14525 digest[1] = byte_swap_32 (digest[1]);
14526
14527 IP (digest[0], digest[1], tt);
14528
14529 digest[0] = rotr32 (digest[0], 29);
14530 digest[1] = rotr32 (digest[1], 29);
14531 digest[2] = 0;
14532 digest[3] = 0;
14533
14534 return (PARSER_OK);
14535 }
14536
14537 int lotus5_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14538 {
14539 if ((input_len < DISPLAY_LEN_MIN_8600) || (input_len > DISPLAY_LEN_MAX_8600)) return (PARSER_GLOBAL_LENGTH);
14540
14541 u32 *digest = (u32 *) hash_buf->digest;
14542
14543 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
14544 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
14545 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
14546 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
14547
14548 digest[0] = byte_swap_32 (digest[0]);
14549 digest[1] = byte_swap_32 (digest[1]);
14550 digest[2] = byte_swap_32 (digest[2]);
14551 digest[3] = byte_swap_32 (digest[3]);
14552
14553 return (PARSER_OK);
14554 }
14555
14556 int lotus6_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14557 {
14558 if ((input_len < DISPLAY_LEN_MIN_8700) || (input_len > DISPLAY_LEN_MAX_8700)) return (PARSER_GLOBAL_LENGTH);
14559
14560 if ((input_buf[0] != '(') || (input_buf[1] != 'G') || (input_buf[21] != ')')) return (PARSER_SIGNATURE_UNMATCHED);
14561
14562 u32 *digest = (u32 *) hash_buf->digest;
14563
14564 salt_t *salt = hash_buf->salt;
14565
14566 u8 tmp_buf[120] = { 0 };
14567
14568 base64_decode (lotus64_to_int, (const u8 *) input_buf + 2, input_len - 3, tmp_buf);
14569
14570 tmp_buf[3] += -4; // dont ask!
14571
14572 memcpy (salt->salt_buf, tmp_buf, 5);
14573
14574 salt->salt_len = 5;
14575
14576 memcpy (digest, tmp_buf + 5, 9);
14577
14578 // yes, only 9 byte are needed to crack, but 10 to display
14579
14580 salt->salt_buf_pc[7] = input_buf[20];
14581
14582 return (PARSER_OK);
14583 }
14584
14585 int lotus8_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14586 {
14587 if ((input_len < DISPLAY_LEN_MIN_9100) || (input_len > DISPLAY_LEN_MAX_9100)) return (PARSER_GLOBAL_LENGTH);
14588
14589 if ((input_buf[0] != '(') || (input_buf[1] != 'H') || (input_buf[DISPLAY_LEN_MAX_9100 - 1] != ')')) return (PARSER_SIGNATURE_UNMATCHED);
14590
14591 u32 *digest = (u32 *) hash_buf->digest;
14592
14593 salt_t *salt = hash_buf->salt;
14594
14595 u8 tmp_buf[120] = { 0 };
14596
14597 base64_decode (lotus64_to_int, (const u8 *) input_buf + 2, input_len - 3, tmp_buf);
14598
14599 tmp_buf[3] += -4; // dont ask!
14600
14601 // salt
14602
14603 memcpy (salt->salt_buf, tmp_buf, 16);
14604
14605 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)
14606
14607 // iteration
14608
14609 char tmp_iter_buf[11] = { 0 };
14610
14611 memcpy (tmp_iter_buf, tmp_buf + 16, 10);
14612
14613 tmp_iter_buf[10] = 0;
14614
14615 salt->salt_iter = atoi (tmp_iter_buf);
14616
14617 if (salt->salt_iter < 1) // well, the limit hopefully is much higher
14618 {
14619 return (PARSER_SALT_ITERATION);
14620 }
14621
14622 salt->salt_iter--; // first round in init
14623
14624 // 2 additional bytes for display only
14625
14626 salt->salt_buf_pc[0] = tmp_buf[26];
14627 salt->salt_buf_pc[1] = tmp_buf[27];
14628
14629 // digest
14630
14631 memcpy (digest, tmp_buf + 28, 8);
14632
14633 digest[0] = byte_swap_32 (digest[0]);
14634 digest[1] = byte_swap_32 (digest[1]);
14635 digest[2] = 0;
14636 digest[3] = 0;
14637
14638 return (PARSER_OK);
14639 }
14640
14641 int hmailserver_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14642 {
14643 if ((input_len < DISPLAY_LEN_MIN_1421) || (input_len > DISPLAY_LEN_MAX_1421)) return (PARSER_GLOBAL_LENGTH);
14644
14645 u32 *digest = (u32 *) hash_buf->digest;
14646
14647 salt_t *salt = hash_buf->salt;
14648
14649 char *salt_buf_pos = input_buf;
14650
14651 char *hash_buf_pos = salt_buf_pos + 6;
14652
14653 digest[0] = hex_to_u32 ((const u8 *) &hash_buf_pos[ 0]);
14654 digest[1] = hex_to_u32 ((const u8 *) &hash_buf_pos[ 8]);
14655 digest[2] = hex_to_u32 ((const u8 *) &hash_buf_pos[16]);
14656 digest[3] = hex_to_u32 ((const u8 *) &hash_buf_pos[24]);
14657 digest[4] = hex_to_u32 ((const u8 *) &hash_buf_pos[32]);
14658 digest[5] = hex_to_u32 ((const u8 *) &hash_buf_pos[40]);
14659 digest[6] = hex_to_u32 ((const u8 *) &hash_buf_pos[48]);
14660 digest[7] = hex_to_u32 ((const u8 *) &hash_buf_pos[56]);
14661
14662 digest[0] -= SHA256M_A;
14663 digest[1] -= SHA256M_B;
14664 digest[2] -= SHA256M_C;
14665 digest[3] -= SHA256M_D;
14666 digest[4] -= SHA256M_E;
14667 digest[5] -= SHA256M_F;
14668 digest[6] -= SHA256M_G;
14669 digest[7] -= SHA256M_H;
14670
14671 char *salt_buf_ptr = (char *) salt->salt_buf;
14672
14673 const uint salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf_pos, 6);
14674
14675 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
14676
14677 salt->salt_len = salt_len;
14678
14679 return (PARSER_OK);
14680 }
14681
14682 int phps_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14683 {
14684 if ((input_len < DISPLAY_LEN_MIN_2612) || (input_len > DISPLAY_LEN_MAX_2612)) return (PARSER_GLOBAL_LENGTH);
14685
14686 u32 *digest = (u32 *) hash_buf->digest;
14687
14688 if (memcmp (SIGNATURE_PHPS, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
14689
14690 salt_t *salt = hash_buf->salt;
14691
14692 char *salt_buf = input_buf + 6;
14693
14694 char *digest_buf = strchr (salt_buf, '$');
14695
14696 if (digest_buf == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14697
14698 uint salt_len = digest_buf - salt_buf;
14699
14700 digest_buf++; // skip the '$' symbol
14701
14702 char *salt_buf_ptr = (char *) salt->salt_buf;
14703
14704 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
14705
14706 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
14707
14708 salt->salt_len = salt_len;
14709
14710 digest[0] = hex_to_u32 ((const u8 *) &digest_buf[ 0]);
14711 digest[1] = hex_to_u32 ((const u8 *) &digest_buf[ 8]);
14712 digest[2] = hex_to_u32 ((const u8 *) &digest_buf[16]);
14713 digest[3] = hex_to_u32 ((const u8 *) &digest_buf[24]);
14714
14715 digest[0] = byte_swap_32 (digest[0]);
14716 digest[1] = byte_swap_32 (digest[1]);
14717 digest[2] = byte_swap_32 (digest[2]);
14718 digest[3] = byte_swap_32 (digest[3]);
14719
14720 digest[0] -= MD5M_A;
14721 digest[1] -= MD5M_B;
14722 digest[2] -= MD5M_C;
14723 digest[3] -= MD5M_D;
14724
14725 return (PARSER_OK);
14726 }
14727
14728 int mediawiki_b_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14729 {
14730 if ((input_len < DISPLAY_LEN_MIN_3711) || (input_len > DISPLAY_LEN_MAX_3711)) return (PARSER_GLOBAL_LENGTH);
14731
14732 if (memcmp (SIGNATURE_MEDIAWIKI_B, input_buf, 3)) return (PARSER_SIGNATURE_UNMATCHED);
14733
14734 u32 *digest = (u32 *) hash_buf->digest;
14735
14736 salt_t *salt = hash_buf->salt;
14737
14738 char *salt_buf = input_buf + 3;
14739
14740 char *digest_buf = strchr (salt_buf, '$');
14741
14742 if (digest_buf == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14743
14744 uint salt_len = digest_buf - salt_buf;
14745
14746 digest_buf++; // skip the '$' symbol
14747
14748 char *salt_buf_ptr = (char *) salt->salt_buf;
14749
14750 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
14751
14752 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
14753
14754 salt_buf_ptr[salt_len] = 0x2d;
14755
14756 salt->salt_len = salt_len + 1;
14757
14758 digest[0] = hex_to_u32 ((const u8 *) &digest_buf[ 0]);
14759 digest[1] = hex_to_u32 ((const u8 *) &digest_buf[ 8]);
14760 digest[2] = hex_to_u32 ((const u8 *) &digest_buf[16]);
14761 digest[3] = hex_to_u32 ((const u8 *) &digest_buf[24]);
14762
14763 digest[0] = byte_swap_32 (digest[0]);
14764 digest[1] = byte_swap_32 (digest[1]);
14765 digest[2] = byte_swap_32 (digest[2]);
14766 digest[3] = byte_swap_32 (digest[3]);
14767
14768 digest[0] -= MD5M_A;
14769 digest[1] -= MD5M_B;
14770 digest[2] -= MD5M_C;
14771 digest[3] -= MD5M_D;
14772
14773 return (PARSER_OK);
14774 }
14775
14776 int peoplesoft_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14777 {
14778 if ((input_len < DISPLAY_LEN_MIN_133) || (input_len > DISPLAY_LEN_MAX_133)) return (PARSER_GLOBAL_LENGTH);
14779
14780 u32 *digest = (u32 *) hash_buf->digest;
14781
14782 salt_t *salt = hash_buf->salt;
14783
14784 u8 tmp_buf[100] = { 0 };
14785
14786 base64_decode (base64_to_int, (const u8 *) input_buf, input_len, tmp_buf);
14787
14788 memcpy (digest, tmp_buf, 20);
14789
14790 digest[0] = byte_swap_32 (digest[0]);
14791 digest[1] = byte_swap_32 (digest[1]);
14792 digest[2] = byte_swap_32 (digest[2]);
14793 digest[3] = byte_swap_32 (digest[3]);
14794 digest[4] = byte_swap_32 (digest[4]);
14795
14796 digest[0] -= SHA1M_A;
14797 digest[1] -= SHA1M_B;
14798 digest[2] -= SHA1M_C;
14799 digest[3] -= SHA1M_D;
14800 digest[4] -= SHA1M_E;
14801
14802 salt->salt_buf[0] = 0x80;
14803
14804 salt->salt_len = 0;
14805
14806 return (PARSER_OK);
14807 }
14808
14809 int skype_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14810 {
14811 if ((input_len < DISPLAY_LEN_MIN_23) || (input_len > DISPLAY_LEN_MAX_23)) return (PARSER_GLOBAL_LENGTH);
14812
14813 u32 *digest = (u32 *) hash_buf->digest;
14814
14815 salt_t *salt = hash_buf->salt;
14816
14817 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
14818 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
14819 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
14820 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
14821
14822 digest[0] = byte_swap_32 (digest[0]);
14823 digest[1] = byte_swap_32 (digest[1]);
14824 digest[2] = byte_swap_32 (digest[2]);
14825 digest[3] = byte_swap_32 (digest[3]);
14826
14827 digest[0] -= MD5M_A;
14828 digest[1] -= MD5M_B;
14829 digest[2] -= MD5M_C;
14830 digest[3] -= MD5M_D;
14831
14832 if (input_buf[32] != ':') return (PARSER_SEPARATOR_UNMATCHED);
14833
14834 uint salt_len = input_len - 32 - 1;
14835
14836 char *salt_buf = input_buf + 32 + 1;
14837
14838 char *salt_buf_ptr = (char *) salt->salt_buf;
14839
14840 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
14841
14842 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
14843
14844 /*
14845 * add static "salt" part
14846 */
14847
14848 memcpy (salt_buf_ptr + salt_len, "\nskyper\n", 8);
14849
14850 salt_len += 8;
14851
14852 salt->salt_len = salt_len;
14853
14854 return (PARSER_OK);
14855 }
14856
14857 int androidfde_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14858 {
14859 if ((input_len < DISPLAY_LEN_MIN_8800) || (input_len > DISPLAY_LEN_MAX_8800)) return (PARSER_GLOBAL_LENGTH);
14860
14861 if (memcmp (SIGNATURE_ANDROIDFDE, input_buf, 5)) return (PARSER_SIGNATURE_UNMATCHED);
14862
14863 u32 *digest = (u32 *) hash_buf->digest;
14864
14865 salt_t *salt = hash_buf->salt;
14866
14867 androidfde_t *androidfde = (androidfde_t *) hash_buf->esalt;
14868
14869 /**
14870 * parse line
14871 */
14872
14873 char *saltlen_pos = input_buf + 1 + 3 + 1;
14874
14875 char *saltbuf_pos = strchr (saltlen_pos, '$');
14876
14877 if (saltbuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14878
14879 uint saltlen_len = saltbuf_pos - saltlen_pos;
14880
14881 if (saltlen_len != 2) return (PARSER_SALT_LENGTH);
14882
14883 saltbuf_pos++;
14884
14885 char *keylen_pos = strchr (saltbuf_pos, '$');
14886
14887 if (keylen_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14888
14889 uint saltbuf_len = keylen_pos - saltbuf_pos;
14890
14891 if (saltbuf_len != 32) return (PARSER_SALT_LENGTH);
14892
14893 keylen_pos++;
14894
14895 char *keybuf_pos = strchr (keylen_pos, '$');
14896
14897 if (keybuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14898
14899 uint keylen_len = keybuf_pos - keylen_pos;
14900
14901 if (keylen_len != 2) return (PARSER_SALT_LENGTH);
14902
14903 keybuf_pos++;
14904
14905 char *databuf_pos = strchr (keybuf_pos, '$');
14906
14907 if (databuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14908
14909 uint keybuf_len = databuf_pos - keybuf_pos;
14910
14911 if (keybuf_len != 32) return (PARSER_SALT_LENGTH);
14912
14913 databuf_pos++;
14914
14915 uint data_len = input_len - 1 - 3 - 1 - saltlen_len - 1 - saltbuf_len - 1 - keylen_len - 1 - keybuf_len - 1;
14916
14917 if (data_len != 3072) return (PARSER_SALT_LENGTH);
14918
14919 /**
14920 * copy data
14921 */
14922
14923 digest[0] = hex_to_u32 ((const u8 *) &keybuf_pos[ 0]);
14924 digest[1] = hex_to_u32 ((const u8 *) &keybuf_pos[ 8]);
14925 digest[2] = hex_to_u32 ((const u8 *) &keybuf_pos[16]);
14926 digest[3] = hex_to_u32 ((const u8 *) &keybuf_pos[24]);
14927
14928 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &saltbuf_pos[ 0]);
14929 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &saltbuf_pos[ 8]);
14930 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &saltbuf_pos[16]);
14931 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &saltbuf_pos[24]);
14932
14933 salt->salt_buf[0] = byte_swap_32 (salt->salt_buf[0]);
14934 salt->salt_buf[1] = byte_swap_32 (salt->salt_buf[1]);
14935 salt->salt_buf[2] = byte_swap_32 (salt->salt_buf[2]);
14936 salt->salt_buf[3] = byte_swap_32 (salt->salt_buf[3]);
14937
14938 salt->salt_len = 16;
14939 salt->salt_iter = ROUNDS_ANDROIDFDE - 1;
14940
14941 for (uint i = 0, j = 0; i < 3072; i += 8, j += 1)
14942 {
14943 androidfde->data[j] = hex_to_u32 ((const u8 *) &databuf_pos[i]);
14944 }
14945
14946 return (PARSER_OK);
14947 }
14948
14949 int scrypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14950 {
14951 if ((input_len < DISPLAY_LEN_MIN_8900) || (input_len > DISPLAY_LEN_MAX_8900)) return (PARSER_GLOBAL_LENGTH);
14952
14953 if (memcmp (SIGNATURE_SCRYPT, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
14954
14955 u32 *digest = (u32 *) hash_buf->digest;
14956
14957 salt_t *salt = hash_buf->salt;
14958
14959 /**
14960 * parse line
14961 */
14962
14963 // first is the N salt parameter
14964
14965 char *N_pos = input_buf + 6;
14966
14967 if (N_pos[0] != ':') return (PARSER_SEPARATOR_UNMATCHED);
14968
14969 N_pos++;
14970
14971 salt->scrypt_N = atoi (N_pos);
14972
14973 // r
14974
14975 char *r_pos = strchr (N_pos, ':');
14976
14977 if (r_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14978
14979 r_pos++;
14980
14981 salt->scrypt_r = atoi (r_pos);
14982
14983 // p
14984
14985 char *p_pos = strchr (r_pos, ':');
14986
14987 if (p_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14988
14989 p_pos++;
14990
14991 salt->scrypt_p = atoi (p_pos);
14992
14993 // salt
14994
14995 char *saltbuf_pos = strchr (p_pos, ':');
14996
14997 if (saltbuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14998
14999 saltbuf_pos++;
15000
15001 char *hash_pos = strchr (saltbuf_pos, ':');
15002
15003 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15004
15005 hash_pos++;
15006
15007 // base64 decode
15008
15009 int salt_len_base64 = hash_pos - saltbuf_pos;
15010
15011 if (salt_len_base64 > 45) return (PARSER_SALT_LENGTH);
15012
15013 u8 tmp_buf[33] = { 0 };
15014
15015 int tmp_len = base64_decode (base64_to_int, (const u8 *) saltbuf_pos, salt_len_base64, tmp_buf);
15016
15017 char *salt_buf_ptr = (char *) salt->salt_buf;
15018
15019 memcpy (salt_buf_ptr, tmp_buf, tmp_len);
15020
15021 salt->salt_len = tmp_len;
15022 salt->salt_iter = 1;
15023
15024 // digest - base64 decode
15025
15026 memset (tmp_buf, 0, sizeof (tmp_buf));
15027
15028 tmp_len = input_len - (hash_pos - input_buf);
15029
15030 if (tmp_len != 44) return (PARSER_GLOBAL_LENGTH);
15031
15032 base64_decode (base64_to_int, (const u8 *) hash_pos, tmp_len, tmp_buf);
15033
15034 memcpy (digest, tmp_buf, 32);
15035
15036 return (PARSER_OK);
15037 }
15038
15039 int juniper_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15040 {
15041 if ((input_len < DISPLAY_LEN_MIN_501) || (input_len > DISPLAY_LEN_MAX_501)) return (PARSER_GLOBAL_LENGTH);
15042
15043 u32 *digest = (u32 *) hash_buf->digest;
15044
15045 salt_t *salt = hash_buf->salt;
15046
15047 /**
15048 * parse line
15049 */
15050
15051 char decrypted[76] = { 0 }; // iv + hash
15052
15053 juniper_decrypt_hash (input_buf, decrypted);
15054
15055 char *md5crypt_hash = decrypted + 12;
15056
15057 if (memcmp (md5crypt_hash, "$1$danastre$", 12)) return (PARSER_SALT_VALUE);
15058
15059 salt->salt_iter = ROUNDS_MD5CRYPT;
15060
15061 char *salt_pos = md5crypt_hash + 3;
15062
15063 char *hash_pos = strchr (salt_pos, '$'); // or simply salt_pos + 8
15064
15065 salt->salt_len = hash_pos - salt_pos; // should be 8
15066
15067 memcpy ((char *) salt->salt_buf, salt_pos, salt->salt_len);
15068
15069 hash_pos++;
15070
15071 md5crypt_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
15072
15073 return (PARSER_OK);
15074 }
15075
15076 int cisco8_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15077 {
15078 if ((input_len < DISPLAY_LEN_MIN_9200) || (input_len > DISPLAY_LEN_MAX_9200)) return (PARSER_GLOBAL_LENGTH);
15079
15080 if (memcmp (SIGNATURE_CISCO8, input_buf, 3)) return (PARSER_SIGNATURE_UNMATCHED);
15081
15082 u32 *digest = (u32 *) hash_buf->digest;
15083
15084 salt_t *salt = hash_buf->salt;
15085
15086 pbkdf2_sha256_t *pbkdf2_sha256 = (pbkdf2_sha256_t *) hash_buf->esalt;
15087
15088 /**
15089 * parse line
15090 */
15091
15092 // first is *raw* salt
15093
15094 char *salt_pos = input_buf + 3;
15095
15096 char *hash_pos = strchr (salt_pos, '$');
15097
15098 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15099
15100 uint salt_len = hash_pos - salt_pos;
15101
15102 if (salt_len != 14) return (PARSER_SALT_LENGTH);
15103
15104 hash_pos++;
15105
15106 char *salt_buf_ptr = (char *) pbkdf2_sha256->salt_buf;
15107
15108 memcpy (salt_buf_ptr, salt_pos, 14);
15109
15110 salt_buf_ptr[17] = 0x01;
15111 salt_buf_ptr[18] = 0x80;
15112
15113 // add some stuff to normal salt to make sorted happy
15114
15115 salt->salt_buf[0] = pbkdf2_sha256->salt_buf[0];
15116 salt->salt_buf[1] = pbkdf2_sha256->salt_buf[1];
15117 salt->salt_buf[2] = pbkdf2_sha256->salt_buf[2];
15118 salt->salt_buf[3] = pbkdf2_sha256->salt_buf[3];
15119
15120 salt->salt_len = salt_len;
15121 salt->salt_iter = ROUNDS_CISCO8 - 1;
15122
15123 // base64 decode hash
15124
15125 u8 tmp_buf[100] = { 0 };
15126
15127 uint hash_len = input_len - 3 - salt_len - 1;
15128
15129 int tmp_len = base64_decode (itoa64_to_int, (const u8 *) hash_pos, hash_len, tmp_buf);
15130
15131 if (tmp_len != 32) return (PARSER_HASH_LENGTH);
15132
15133 memcpy (digest, tmp_buf, 32);
15134
15135 digest[0] = byte_swap_32 (digest[0]);
15136 digest[1] = byte_swap_32 (digest[1]);
15137 digest[2] = byte_swap_32 (digest[2]);
15138 digest[3] = byte_swap_32 (digest[3]);
15139 digest[4] = byte_swap_32 (digest[4]);
15140 digest[5] = byte_swap_32 (digest[5]);
15141 digest[6] = byte_swap_32 (digest[6]);
15142 digest[7] = byte_swap_32 (digest[7]);
15143
15144 return (PARSER_OK);
15145 }
15146
15147 int cisco9_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15148 {
15149 if ((input_len < DISPLAY_LEN_MIN_9300) || (input_len > DISPLAY_LEN_MAX_9300)) return (PARSER_GLOBAL_LENGTH);
15150
15151 if (memcmp (SIGNATURE_CISCO9, input_buf, 3)) return (PARSER_SIGNATURE_UNMATCHED);
15152
15153 u32 *digest = (u32 *) hash_buf->digest;
15154
15155 salt_t *salt = hash_buf->salt;
15156
15157 /**
15158 * parse line
15159 */
15160
15161 // first is *raw* salt
15162
15163 char *salt_pos = input_buf + 3;
15164
15165 char *hash_pos = strchr (salt_pos, '$');
15166
15167 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15168
15169 uint salt_len = hash_pos - salt_pos;
15170
15171 if (salt_len != 14) return (PARSER_SALT_LENGTH);
15172
15173 salt->salt_len = salt_len;
15174 hash_pos++;
15175
15176 char *salt_buf_ptr = (char *) salt->salt_buf;
15177
15178 memcpy (salt_buf_ptr, salt_pos, salt_len);
15179 salt_buf_ptr[salt_len] = 0;
15180
15181 // base64 decode hash
15182
15183 u8 tmp_buf[100] = { 0 };
15184
15185 uint hash_len = input_len - 3 - salt_len - 1;
15186
15187 int tmp_len = base64_decode (itoa64_to_int, (const u8 *) hash_pos, hash_len, tmp_buf);
15188
15189 if (tmp_len != 32) return (PARSER_HASH_LENGTH);
15190
15191 memcpy (digest, tmp_buf, 32);
15192
15193 // fixed:
15194 salt->scrypt_N = 16384;
15195 salt->scrypt_r = 1;
15196 salt->scrypt_p = 1;
15197 salt->salt_iter = 1;
15198
15199 return (PARSER_OK);
15200 }
15201
15202 int office2007_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15203 {
15204 if ((input_len < DISPLAY_LEN_MIN_9400) || (input_len > DISPLAY_LEN_MAX_9400)) return (PARSER_GLOBAL_LENGTH);
15205
15206 if (memcmp (SIGNATURE_OFFICE2007, input_buf, 8)) return (PARSER_SIGNATURE_UNMATCHED);
15207
15208 u32 *digest = (u32 *) hash_buf->digest;
15209
15210 salt_t *salt = hash_buf->salt;
15211
15212 office2007_t *office2007 = (office2007_t *) hash_buf->esalt;
15213
15214 /**
15215 * parse line
15216 */
15217
15218 char *version_pos = input_buf + 8 + 1;
15219
15220 char *verifierHashSize_pos = strchr (version_pos, '*');
15221
15222 if (verifierHashSize_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15223
15224 u32 version_len = verifierHashSize_pos - version_pos;
15225
15226 if (version_len != 4) return (PARSER_SALT_LENGTH);
15227
15228 verifierHashSize_pos++;
15229
15230 char *keySize_pos = strchr (verifierHashSize_pos, '*');
15231
15232 if (keySize_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15233
15234 u32 verifierHashSize_len = keySize_pos - verifierHashSize_pos;
15235
15236 if (verifierHashSize_len != 2) return (PARSER_SALT_LENGTH);
15237
15238 keySize_pos++;
15239
15240 char *saltSize_pos = strchr (keySize_pos, '*');
15241
15242 if (saltSize_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15243
15244 u32 keySize_len = saltSize_pos - keySize_pos;
15245
15246 if (keySize_len != 3) return (PARSER_SALT_LENGTH);
15247
15248 saltSize_pos++;
15249
15250 char *osalt_pos = strchr (saltSize_pos, '*');
15251
15252 if (osalt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15253
15254 u32 saltSize_len = osalt_pos - saltSize_pos;
15255
15256 if (saltSize_len != 2) return (PARSER_SALT_LENGTH);
15257
15258 osalt_pos++;
15259
15260 char *encryptedVerifier_pos = strchr (osalt_pos, '*');
15261
15262 if (encryptedVerifier_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15263
15264 u32 osalt_len = encryptedVerifier_pos - osalt_pos;
15265
15266 if (osalt_len != 32) return (PARSER_SALT_LENGTH);
15267
15268 encryptedVerifier_pos++;
15269
15270 char *encryptedVerifierHash_pos = strchr (encryptedVerifier_pos, '*');
15271
15272 if (encryptedVerifierHash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15273
15274 u32 encryptedVerifier_len = encryptedVerifierHash_pos - encryptedVerifier_pos;
15275
15276 if (encryptedVerifier_len != 32) return (PARSER_SALT_LENGTH);
15277
15278 encryptedVerifierHash_pos++;
15279
15280 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;
15281
15282 if (encryptedVerifierHash_len != 40) return (PARSER_SALT_LENGTH);
15283
15284 const uint version = atoi (version_pos);
15285
15286 if (version != 2007) return (PARSER_SALT_VALUE);
15287
15288 const uint verifierHashSize = atoi (verifierHashSize_pos);
15289
15290 if (verifierHashSize != 20) return (PARSER_SALT_VALUE);
15291
15292 const uint keySize = atoi (keySize_pos);
15293
15294 if ((keySize != 128) && (keySize != 256)) return (PARSER_SALT_VALUE);
15295
15296 office2007->keySize = keySize;
15297
15298 const uint saltSize = atoi (saltSize_pos);
15299
15300 if (saltSize != 16) return (PARSER_SALT_VALUE);
15301
15302 /**
15303 * salt
15304 */
15305
15306 salt->salt_len = 16;
15307 salt->salt_iter = ROUNDS_OFFICE2007;
15308
15309 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &osalt_pos[ 0]);
15310 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &osalt_pos[ 8]);
15311 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &osalt_pos[16]);
15312 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &osalt_pos[24]);
15313
15314 /**
15315 * esalt
15316 */
15317
15318 office2007->encryptedVerifier[0] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 0]);
15319 office2007->encryptedVerifier[1] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 8]);
15320 office2007->encryptedVerifier[2] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[16]);
15321 office2007->encryptedVerifier[3] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[24]);
15322
15323 office2007->encryptedVerifierHash[0] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 0]);
15324 office2007->encryptedVerifierHash[1] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 8]);
15325 office2007->encryptedVerifierHash[2] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[16]);
15326 office2007->encryptedVerifierHash[3] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[24]);
15327 office2007->encryptedVerifierHash[4] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[32]);
15328
15329 /**
15330 * digest
15331 */
15332
15333 digest[0] = office2007->encryptedVerifierHash[0];
15334 digest[1] = office2007->encryptedVerifierHash[1];
15335 digest[2] = office2007->encryptedVerifierHash[2];
15336 digest[3] = office2007->encryptedVerifierHash[3];
15337
15338 return (PARSER_OK);
15339 }
15340
15341 int office2010_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15342 {
15343 if ((input_len < DISPLAY_LEN_MIN_9500) || (input_len > DISPLAY_LEN_MAX_9500)) return (PARSER_GLOBAL_LENGTH);
15344
15345 if (memcmp (SIGNATURE_OFFICE2010, input_buf, 8)) return (PARSER_SIGNATURE_UNMATCHED);
15346
15347 u32 *digest = (u32 *) hash_buf->digest;
15348
15349 salt_t *salt = hash_buf->salt;
15350
15351 office2010_t *office2010 = (office2010_t *) hash_buf->esalt;
15352
15353 /**
15354 * parse line
15355 */
15356
15357 char *version_pos = input_buf + 8 + 1;
15358
15359 char *spinCount_pos = strchr (version_pos, '*');
15360
15361 if (spinCount_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15362
15363 u32 version_len = spinCount_pos - version_pos;
15364
15365 if (version_len != 4) return (PARSER_SALT_LENGTH);
15366
15367 spinCount_pos++;
15368
15369 char *keySize_pos = strchr (spinCount_pos, '*');
15370
15371 if (keySize_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15372
15373 u32 spinCount_len = keySize_pos - spinCount_pos;
15374
15375 if (spinCount_len != 6) return (PARSER_SALT_LENGTH);
15376
15377 keySize_pos++;
15378
15379 char *saltSize_pos = strchr (keySize_pos, '*');
15380
15381 if (saltSize_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15382
15383 u32 keySize_len = saltSize_pos - keySize_pos;
15384
15385 if (keySize_len != 3) return (PARSER_SALT_LENGTH);
15386
15387 saltSize_pos++;
15388
15389 char *osalt_pos = strchr (saltSize_pos, '*');
15390
15391 if (osalt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15392
15393 u32 saltSize_len = osalt_pos - saltSize_pos;
15394
15395 if (saltSize_len != 2) return (PARSER_SALT_LENGTH);
15396
15397 osalt_pos++;
15398
15399 char *encryptedVerifier_pos = strchr (osalt_pos, '*');
15400
15401 if (encryptedVerifier_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15402
15403 u32 osalt_len = encryptedVerifier_pos - osalt_pos;
15404
15405 if (osalt_len != 32) return (PARSER_SALT_LENGTH);
15406
15407 encryptedVerifier_pos++;
15408
15409 char *encryptedVerifierHash_pos = strchr (encryptedVerifier_pos, '*');
15410
15411 if (encryptedVerifierHash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15412
15413 u32 encryptedVerifier_len = encryptedVerifierHash_pos - encryptedVerifier_pos;
15414
15415 if (encryptedVerifier_len != 32) return (PARSER_SALT_LENGTH);
15416
15417 encryptedVerifierHash_pos++;
15418
15419 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;
15420
15421 if (encryptedVerifierHash_len != 64) return (PARSER_SALT_LENGTH);
15422
15423 const uint version = atoi (version_pos);
15424
15425 if (version != 2010) return (PARSER_SALT_VALUE);
15426
15427 const uint spinCount = atoi (spinCount_pos);
15428
15429 if (spinCount != 100000) return (PARSER_SALT_VALUE);
15430
15431 const uint keySize = atoi (keySize_pos);
15432
15433 if (keySize != 128) return (PARSER_SALT_VALUE);
15434
15435 const uint saltSize = atoi (saltSize_pos);
15436
15437 if (saltSize != 16) return (PARSER_SALT_VALUE);
15438
15439 /**
15440 * salt
15441 */
15442
15443 salt->salt_len = 16;
15444 salt->salt_iter = spinCount;
15445
15446 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &osalt_pos[ 0]);
15447 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &osalt_pos[ 8]);
15448 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &osalt_pos[16]);
15449 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &osalt_pos[24]);
15450
15451 /**
15452 * esalt
15453 */
15454
15455 office2010->encryptedVerifier[0] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 0]);
15456 office2010->encryptedVerifier[1] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 8]);
15457 office2010->encryptedVerifier[2] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[16]);
15458 office2010->encryptedVerifier[3] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[24]);
15459
15460 office2010->encryptedVerifierHash[0] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 0]);
15461 office2010->encryptedVerifierHash[1] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 8]);
15462 office2010->encryptedVerifierHash[2] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[16]);
15463 office2010->encryptedVerifierHash[3] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[24]);
15464 office2010->encryptedVerifierHash[4] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[32]);
15465 office2010->encryptedVerifierHash[5] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[40]);
15466 office2010->encryptedVerifierHash[6] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[48]);
15467 office2010->encryptedVerifierHash[7] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[56]);
15468
15469 /**
15470 * digest
15471 */
15472
15473 digest[0] = office2010->encryptedVerifierHash[0];
15474 digest[1] = office2010->encryptedVerifierHash[1];
15475 digest[2] = office2010->encryptedVerifierHash[2];
15476 digest[3] = office2010->encryptedVerifierHash[3];
15477
15478 return (PARSER_OK);
15479 }
15480
15481 int office2013_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15482 {
15483 if ((input_len < DISPLAY_LEN_MIN_9600) || (input_len > DISPLAY_LEN_MAX_9600)) return (PARSER_GLOBAL_LENGTH);
15484
15485 if (memcmp (SIGNATURE_OFFICE2013, input_buf, 8)) return (PARSER_SIGNATURE_UNMATCHED);
15486
15487 u32 *digest = (u32 *) hash_buf->digest;
15488
15489 salt_t *salt = hash_buf->salt;
15490
15491 office2013_t *office2013 = (office2013_t *) hash_buf->esalt;
15492
15493 /**
15494 * parse line
15495 */
15496
15497 char *version_pos = input_buf + 8 + 1;
15498
15499 char *spinCount_pos = strchr (version_pos, '*');
15500
15501 if (spinCount_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15502
15503 u32 version_len = spinCount_pos - version_pos;
15504
15505 if (version_len != 4) return (PARSER_SALT_LENGTH);
15506
15507 spinCount_pos++;
15508
15509 char *keySize_pos = strchr (spinCount_pos, '*');
15510
15511 if (keySize_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15512
15513 u32 spinCount_len = keySize_pos - spinCount_pos;
15514
15515 if (spinCount_len != 6) return (PARSER_SALT_LENGTH);
15516
15517 keySize_pos++;
15518
15519 char *saltSize_pos = strchr (keySize_pos, '*');
15520
15521 if (saltSize_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15522
15523 u32 keySize_len = saltSize_pos - keySize_pos;
15524
15525 if (keySize_len != 3) return (PARSER_SALT_LENGTH);
15526
15527 saltSize_pos++;
15528
15529 char *osalt_pos = strchr (saltSize_pos, '*');
15530
15531 if (osalt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15532
15533 u32 saltSize_len = osalt_pos - saltSize_pos;
15534
15535 if (saltSize_len != 2) return (PARSER_SALT_LENGTH);
15536
15537 osalt_pos++;
15538
15539 char *encryptedVerifier_pos = strchr (osalt_pos, '*');
15540
15541 if (encryptedVerifier_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15542
15543 u32 osalt_len = encryptedVerifier_pos - osalt_pos;
15544
15545 if (osalt_len != 32) return (PARSER_SALT_LENGTH);
15546
15547 encryptedVerifier_pos++;
15548
15549 char *encryptedVerifierHash_pos = strchr (encryptedVerifier_pos, '*');
15550
15551 if (encryptedVerifierHash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15552
15553 u32 encryptedVerifier_len = encryptedVerifierHash_pos - encryptedVerifier_pos;
15554
15555 if (encryptedVerifier_len != 32) return (PARSER_SALT_LENGTH);
15556
15557 encryptedVerifierHash_pos++;
15558
15559 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;
15560
15561 if (encryptedVerifierHash_len != 64) return (PARSER_SALT_LENGTH);
15562
15563 const uint version = atoi (version_pos);
15564
15565 if (version != 2013) return (PARSER_SALT_VALUE);
15566
15567 const uint spinCount = atoi (spinCount_pos);
15568
15569 if (spinCount != 100000) return (PARSER_SALT_VALUE);
15570
15571 const uint keySize = atoi (keySize_pos);
15572
15573 if (keySize != 256) return (PARSER_SALT_VALUE);
15574
15575 const uint saltSize = atoi (saltSize_pos);
15576
15577 if (saltSize != 16) return (PARSER_SALT_VALUE);
15578
15579 /**
15580 * salt
15581 */
15582
15583 salt->salt_len = 16;
15584 salt->salt_iter = spinCount;
15585
15586 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &osalt_pos[ 0]);
15587 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &osalt_pos[ 8]);
15588 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &osalt_pos[16]);
15589 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &osalt_pos[24]);
15590
15591 /**
15592 * esalt
15593 */
15594
15595 office2013->encryptedVerifier[0] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 0]);
15596 office2013->encryptedVerifier[1] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 8]);
15597 office2013->encryptedVerifier[2] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[16]);
15598 office2013->encryptedVerifier[3] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[24]);
15599
15600 office2013->encryptedVerifierHash[0] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 0]);
15601 office2013->encryptedVerifierHash[1] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 8]);
15602 office2013->encryptedVerifierHash[2] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[16]);
15603 office2013->encryptedVerifierHash[3] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[24]);
15604 office2013->encryptedVerifierHash[4] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[32]);
15605 office2013->encryptedVerifierHash[5] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[40]);
15606 office2013->encryptedVerifierHash[6] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[48]);
15607 office2013->encryptedVerifierHash[7] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[56]);
15608
15609 /**
15610 * digest
15611 */
15612
15613 digest[0] = office2013->encryptedVerifierHash[0];
15614 digest[1] = office2013->encryptedVerifierHash[1];
15615 digest[2] = office2013->encryptedVerifierHash[2];
15616 digest[3] = office2013->encryptedVerifierHash[3];
15617
15618 return (PARSER_OK);
15619 }
15620
15621 int oldoffice01_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15622 {
15623 if ((input_len < DISPLAY_LEN_MIN_9700) || (input_len > DISPLAY_LEN_MAX_9700)) return (PARSER_GLOBAL_LENGTH);
15624
15625 if ((memcmp (SIGNATURE_OLDOFFICE0, input_buf, 12)) && (memcmp (SIGNATURE_OLDOFFICE1, input_buf, 12))) return (PARSER_SIGNATURE_UNMATCHED);
15626
15627 u32 *digest = (u32 *) hash_buf->digest;
15628
15629 salt_t *salt = hash_buf->salt;
15630
15631 oldoffice01_t *oldoffice01 = (oldoffice01_t *) hash_buf->esalt;
15632
15633 /**
15634 * parse line
15635 */
15636
15637 char *version_pos = input_buf + 11;
15638
15639 char *osalt_pos = strchr (version_pos, '*');
15640
15641 if (osalt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15642
15643 u32 version_len = osalt_pos - version_pos;
15644
15645 if (version_len != 1) return (PARSER_SALT_LENGTH);
15646
15647 osalt_pos++;
15648
15649 char *encryptedVerifier_pos = strchr (osalt_pos, '*');
15650
15651 if (encryptedVerifier_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15652
15653 u32 osalt_len = encryptedVerifier_pos - osalt_pos;
15654
15655 if (osalt_len != 32) return (PARSER_SALT_LENGTH);
15656
15657 encryptedVerifier_pos++;
15658
15659 char *encryptedVerifierHash_pos = strchr (encryptedVerifier_pos, '*');
15660
15661 if (encryptedVerifierHash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15662
15663 u32 encryptedVerifier_len = encryptedVerifierHash_pos - encryptedVerifier_pos;
15664
15665 if (encryptedVerifier_len != 32) return (PARSER_SALT_LENGTH);
15666
15667 encryptedVerifierHash_pos++;
15668
15669 u32 encryptedVerifierHash_len = input_len - 11 - version_len - 1 - osalt_len - 1 - encryptedVerifier_len - 1;
15670
15671 if (encryptedVerifierHash_len != 32) return (PARSER_SALT_LENGTH);
15672
15673 const uint version = *version_pos - 0x30;
15674
15675 if (version != 0 && version != 1) return (PARSER_SALT_VALUE);
15676
15677 /**
15678 * esalt
15679 */
15680
15681 oldoffice01->version = version;
15682
15683 oldoffice01->encryptedVerifier[0] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 0]);
15684 oldoffice01->encryptedVerifier[1] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 8]);
15685 oldoffice01->encryptedVerifier[2] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[16]);
15686 oldoffice01->encryptedVerifier[3] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[24]);
15687
15688 oldoffice01->encryptedVerifier[0] = byte_swap_32 (oldoffice01->encryptedVerifier[0]);
15689 oldoffice01->encryptedVerifier[1] = byte_swap_32 (oldoffice01->encryptedVerifier[1]);
15690 oldoffice01->encryptedVerifier[2] = byte_swap_32 (oldoffice01->encryptedVerifier[2]);
15691 oldoffice01->encryptedVerifier[3] = byte_swap_32 (oldoffice01->encryptedVerifier[3]);
15692
15693 oldoffice01->encryptedVerifierHash[0] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 0]);
15694 oldoffice01->encryptedVerifierHash[1] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 8]);
15695 oldoffice01->encryptedVerifierHash[2] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[16]);
15696 oldoffice01->encryptedVerifierHash[3] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[24]);
15697
15698 oldoffice01->encryptedVerifierHash[0] = byte_swap_32 (oldoffice01->encryptedVerifierHash[0]);
15699 oldoffice01->encryptedVerifierHash[1] = byte_swap_32 (oldoffice01->encryptedVerifierHash[1]);
15700 oldoffice01->encryptedVerifierHash[2] = byte_swap_32 (oldoffice01->encryptedVerifierHash[2]);
15701 oldoffice01->encryptedVerifierHash[3] = byte_swap_32 (oldoffice01->encryptedVerifierHash[3]);
15702
15703 /**
15704 * salt
15705 */
15706
15707 salt->salt_len = 16;
15708
15709 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &osalt_pos[ 0]);
15710 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &osalt_pos[ 8]);
15711 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &osalt_pos[16]);
15712 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &osalt_pos[24]);
15713
15714 salt->salt_buf[0] = byte_swap_32 (salt->salt_buf[0]);
15715 salt->salt_buf[1] = byte_swap_32 (salt->salt_buf[1]);
15716 salt->salt_buf[2] = byte_swap_32 (salt->salt_buf[2]);
15717 salt->salt_buf[3] = byte_swap_32 (salt->salt_buf[3]);
15718
15719 // this is a workaround as office produces multiple documents with the same salt
15720
15721 salt->salt_len += 32;
15722
15723 salt->salt_buf[ 4] = oldoffice01->encryptedVerifier[0];
15724 salt->salt_buf[ 5] = oldoffice01->encryptedVerifier[1];
15725 salt->salt_buf[ 6] = oldoffice01->encryptedVerifier[2];
15726 salt->salt_buf[ 7] = oldoffice01->encryptedVerifier[3];
15727 salt->salt_buf[ 8] = oldoffice01->encryptedVerifierHash[0];
15728 salt->salt_buf[ 9] = oldoffice01->encryptedVerifierHash[1];
15729 salt->salt_buf[10] = oldoffice01->encryptedVerifierHash[2];
15730 salt->salt_buf[11] = oldoffice01->encryptedVerifierHash[3];
15731
15732 /**
15733 * digest
15734 */
15735
15736 digest[0] = oldoffice01->encryptedVerifierHash[0];
15737 digest[1] = oldoffice01->encryptedVerifierHash[1];
15738 digest[2] = oldoffice01->encryptedVerifierHash[2];
15739 digest[3] = oldoffice01->encryptedVerifierHash[3];
15740
15741 return (PARSER_OK);
15742 }
15743
15744 int oldoffice01cm1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15745 {
15746 return oldoffice01_parse_hash (input_buf, input_len, hash_buf);
15747 }
15748
15749 int oldoffice01cm2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15750 {
15751 if ((input_len < DISPLAY_LEN_MIN_9720) || (input_len > DISPLAY_LEN_MAX_9720)) return (PARSER_GLOBAL_LENGTH);
15752
15753 if ((memcmp (SIGNATURE_OLDOFFICE0, input_buf, 12)) && (memcmp (SIGNATURE_OLDOFFICE1, input_buf, 12))) return (PARSER_SIGNATURE_UNMATCHED);
15754
15755 u32 *digest = (u32 *) hash_buf->digest;
15756
15757 salt_t *salt = hash_buf->salt;
15758
15759 oldoffice01_t *oldoffice01 = (oldoffice01_t *) hash_buf->esalt;
15760
15761 /**
15762 * parse line
15763 */
15764
15765 char *version_pos = input_buf + 11;
15766
15767 char *osalt_pos = strchr (version_pos, '*');
15768
15769 if (osalt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15770
15771 u32 version_len = osalt_pos - version_pos;
15772
15773 if (version_len != 1) return (PARSER_SALT_LENGTH);
15774
15775 osalt_pos++;
15776
15777 char *encryptedVerifier_pos = strchr (osalt_pos, '*');
15778
15779 if (encryptedVerifier_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15780
15781 u32 osalt_len = encryptedVerifier_pos - osalt_pos;
15782
15783 if (osalt_len != 32) return (PARSER_SALT_LENGTH);
15784
15785 encryptedVerifier_pos++;
15786
15787 char *encryptedVerifierHash_pos = strchr (encryptedVerifier_pos, '*');
15788
15789 if (encryptedVerifierHash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15790
15791 u32 encryptedVerifier_len = encryptedVerifierHash_pos - encryptedVerifier_pos;
15792
15793 if (encryptedVerifier_len != 32) return (PARSER_SALT_LENGTH);
15794
15795 encryptedVerifierHash_pos++;
15796
15797 char *rc4key_pos = strchr (encryptedVerifierHash_pos, ':');
15798
15799 if (rc4key_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15800
15801 u32 encryptedVerifierHash_len = rc4key_pos - encryptedVerifierHash_pos;
15802
15803 if (encryptedVerifierHash_len != 32) return (PARSER_SALT_LENGTH);
15804
15805 rc4key_pos++;
15806
15807 u32 rc4key_len = input_len - 11 - version_len - 1 - osalt_len - 1 - encryptedVerifier_len - 1 - encryptedVerifierHash_len - 1;
15808
15809 if (rc4key_len != 10) return (PARSER_SALT_LENGTH);
15810
15811 const uint version = *version_pos - 0x30;
15812
15813 if (version != 0 && version != 1) return (PARSER_SALT_VALUE);
15814
15815 /**
15816 * esalt
15817 */
15818
15819 oldoffice01->version = version;
15820
15821 oldoffice01->encryptedVerifier[0] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 0]);
15822 oldoffice01->encryptedVerifier[1] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 8]);
15823 oldoffice01->encryptedVerifier[2] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[16]);
15824 oldoffice01->encryptedVerifier[3] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[24]);
15825
15826 oldoffice01->encryptedVerifier[0] = byte_swap_32 (oldoffice01->encryptedVerifier[0]);
15827 oldoffice01->encryptedVerifier[1] = byte_swap_32 (oldoffice01->encryptedVerifier[1]);
15828 oldoffice01->encryptedVerifier[2] = byte_swap_32 (oldoffice01->encryptedVerifier[2]);
15829 oldoffice01->encryptedVerifier[3] = byte_swap_32 (oldoffice01->encryptedVerifier[3]);
15830
15831 oldoffice01->encryptedVerifierHash[0] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 0]);
15832 oldoffice01->encryptedVerifierHash[1] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 8]);
15833 oldoffice01->encryptedVerifierHash[2] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[16]);
15834 oldoffice01->encryptedVerifierHash[3] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[24]);
15835
15836 oldoffice01->encryptedVerifierHash[0] = byte_swap_32 (oldoffice01->encryptedVerifierHash[0]);
15837 oldoffice01->encryptedVerifierHash[1] = byte_swap_32 (oldoffice01->encryptedVerifierHash[1]);
15838 oldoffice01->encryptedVerifierHash[2] = byte_swap_32 (oldoffice01->encryptedVerifierHash[2]);
15839 oldoffice01->encryptedVerifierHash[3] = byte_swap_32 (oldoffice01->encryptedVerifierHash[3]);
15840
15841 oldoffice01->rc4key[1] = 0;
15842 oldoffice01->rc4key[0] = 0;
15843
15844 oldoffice01->rc4key[0] |= hex_convert (rc4key_pos[0]) << 28;
15845 oldoffice01->rc4key[0] |= hex_convert (rc4key_pos[1]) << 24;
15846 oldoffice01->rc4key[0] |= hex_convert (rc4key_pos[2]) << 20;
15847 oldoffice01->rc4key[0] |= hex_convert (rc4key_pos[3]) << 16;
15848 oldoffice01->rc4key[0] |= hex_convert (rc4key_pos[4]) << 12;
15849 oldoffice01->rc4key[0] |= hex_convert (rc4key_pos[5]) << 8;
15850 oldoffice01->rc4key[0] |= hex_convert (rc4key_pos[6]) << 4;
15851 oldoffice01->rc4key[0] |= hex_convert (rc4key_pos[7]) << 0;
15852 oldoffice01->rc4key[1] |= hex_convert (rc4key_pos[8]) << 28;
15853 oldoffice01->rc4key[1] |= hex_convert (rc4key_pos[9]) << 24;
15854
15855 oldoffice01->rc4key[0] = byte_swap_32 (oldoffice01->rc4key[0]);
15856 oldoffice01->rc4key[1] = byte_swap_32 (oldoffice01->rc4key[1]);
15857
15858 /**
15859 * salt
15860 */
15861
15862 salt->salt_len = 16;
15863
15864 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &osalt_pos[ 0]);
15865 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &osalt_pos[ 8]);
15866 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &osalt_pos[16]);
15867 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &osalt_pos[24]);
15868
15869 salt->salt_buf[0] = byte_swap_32 (salt->salt_buf[0]);
15870 salt->salt_buf[1] = byte_swap_32 (salt->salt_buf[1]);
15871 salt->salt_buf[2] = byte_swap_32 (salt->salt_buf[2]);
15872 salt->salt_buf[3] = byte_swap_32 (salt->salt_buf[3]);
15873
15874 // this is a workaround as office produces multiple documents with the same salt
15875
15876 salt->salt_len += 32;
15877
15878 salt->salt_buf[ 4] = oldoffice01->encryptedVerifier[0];
15879 salt->salt_buf[ 5] = oldoffice01->encryptedVerifier[1];
15880 salt->salt_buf[ 6] = oldoffice01->encryptedVerifier[2];
15881 salt->salt_buf[ 7] = oldoffice01->encryptedVerifier[3];
15882 salt->salt_buf[ 8] = oldoffice01->encryptedVerifierHash[0];
15883 salt->salt_buf[ 9] = oldoffice01->encryptedVerifierHash[1];
15884 salt->salt_buf[10] = oldoffice01->encryptedVerifierHash[2];
15885 salt->salt_buf[11] = oldoffice01->encryptedVerifierHash[3];
15886
15887 /**
15888 * digest
15889 */
15890
15891 digest[0] = oldoffice01->rc4key[0];
15892 digest[1] = oldoffice01->rc4key[1];
15893 digest[2] = 0;
15894 digest[3] = 0;
15895
15896 return (PARSER_OK);
15897 }
15898
15899 int oldoffice34_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15900 {
15901 if ((input_len < DISPLAY_LEN_MIN_9800) || (input_len > DISPLAY_LEN_MAX_9800)) return (PARSER_GLOBAL_LENGTH);
15902
15903 if ((memcmp (SIGNATURE_OLDOFFICE3, input_buf, 12)) && (memcmp (SIGNATURE_OLDOFFICE4, input_buf, 12))) return (PARSER_SIGNATURE_UNMATCHED);
15904
15905 u32 *digest = (u32 *) hash_buf->digest;
15906
15907 salt_t *salt = hash_buf->salt;
15908
15909 oldoffice34_t *oldoffice34 = (oldoffice34_t *) hash_buf->esalt;
15910
15911 /**
15912 * parse line
15913 */
15914
15915 char *version_pos = input_buf + 11;
15916
15917 char *osalt_pos = strchr (version_pos, '*');
15918
15919 if (osalt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15920
15921 u32 version_len = osalt_pos - version_pos;
15922
15923 if (version_len != 1) return (PARSER_SALT_LENGTH);
15924
15925 osalt_pos++;
15926
15927 char *encryptedVerifier_pos = strchr (osalt_pos, '*');
15928
15929 if (encryptedVerifier_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15930
15931 u32 osalt_len = encryptedVerifier_pos - osalt_pos;
15932
15933 if (osalt_len != 32) return (PARSER_SALT_LENGTH);
15934
15935 encryptedVerifier_pos++;
15936
15937 char *encryptedVerifierHash_pos = strchr (encryptedVerifier_pos, '*');
15938
15939 if (encryptedVerifierHash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15940
15941 u32 encryptedVerifier_len = encryptedVerifierHash_pos - encryptedVerifier_pos;
15942
15943 if (encryptedVerifier_len != 32) return (PARSER_SALT_LENGTH);
15944
15945 encryptedVerifierHash_pos++;
15946
15947 u32 encryptedVerifierHash_len = input_len - 11 - version_len - 1 - osalt_len - 1 - encryptedVerifier_len - 1;
15948
15949 if (encryptedVerifierHash_len != 40) return (PARSER_SALT_LENGTH);
15950
15951 const uint version = *version_pos - 0x30;
15952
15953 if (version != 3 && version != 4) return (PARSER_SALT_VALUE);
15954
15955 /**
15956 * esalt
15957 */
15958
15959 oldoffice34->version = version;
15960
15961 oldoffice34->encryptedVerifier[0] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 0]);
15962 oldoffice34->encryptedVerifier[1] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 8]);
15963 oldoffice34->encryptedVerifier[2] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[16]);
15964 oldoffice34->encryptedVerifier[3] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[24]);
15965
15966 oldoffice34->encryptedVerifier[0] = byte_swap_32 (oldoffice34->encryptedVerifier[0]);
15967 oldoffice34->encryptedVerifier[1] = byte_swap_32 (oldoffice34->encryptedVerifier[1]);
15968 oldoffice34->encryptedVerifier[2] = byte_swap_32 (oldoffice34->encryptedVerifier[2]);
15969 oldoffice34->encryptedVerifier[3] = byte_swap_32 (oldoffice34->encryptedVerifier[3]);
15970
15971 oldoffice34->encryptedVerifierHash[0] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 0]);
15972 oldoffice34->encryptedVerifierHash[1] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 8]);
15973 oldoffice34->encryptedVerifierHash[2] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[16]);
15974 oldoffice34->encryptedVerifierHash[3] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[24]);
15975 oldoffice34->encryptedVerifierHash[4] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[32]);
15976
15977 oldoffice34->encryptedVerifierHash[0] = byte_swap_32 (oldoffice34->encryptedVerifierHash[0]);
15978 oldoffice34->encryptedVerifierHash[1] = byte_swap_32 (oldoffice34->encryptedVerifierHash[1]);
15979 oldoffice34->encryptedVerifierHash[2] = byte_swap_32 (oldoffice34->encryptedVerifierHash[2]);
15980 oldoffice34->encryptedVerifierHash[3] = byte_swap_32 (oldoffice34->encryptedVerifierHash[3]);
15981 oldoffice34->encryptedVerifierHash[4] = byte_swap_32 (oldoffice34->encryptedVerifierHash[4]);
15982
15983 /**
15984 * salt
15985 */
15986
15987 salt->salt_len = 16;
15988
15989 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &osalt_pos[ 0]);
15990 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &osalt_pos[ 8]);
15991 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &osalt_pos[16]);
15992 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &osalt_pos[24]);
15993
15994 // this is a workaround as office produces multiple documents with the same salt
15995
15996 salt->salt_len += 32;
15997
15998 salt->salt_buf[ 4] = oldoffice34->encryptedVerifier[0];
15999 salt->salt_buf[ 5] = oldoffice34->encryptedVerifier[1];
16000 salt->salt_buf[ 6] = oldoffice34->encryptedVerifier[2];
16001 salt->salt_buf[ 7] = oldoffice34->encryptedVerifier[3];
16002 salt->salt_buf[ 8] = oldoffice34->encryptedVerifierHash[0];
16003 salt->salt_buf[ 9] = oldoffice34->encryptedVerifierHash[1];
16004 salt->salt_buf[10] = oldoffice34->encryptedVerifierHash[2];
16005 salt->salt_buf[11] = oldoffice34->encryptedVerifierHash[3];
16006
16007 /**
16008 * digest
16009 */
16010
16011 digest[0] = oldoffice34->encryptedVerifierHash[0];
16012 digest[1] = oldoffice34->encryptedVerifierHash[1];
16013 digest[2] = oldoffice34->encryptedVerifierHash[2];
16014 digest[3] = oldoffice34->encryptedVerifierHash[3];
16015
16016 return (PARSER_OK);
16017 }
16018
16019 int oldoffice34cm1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16020 {
16021 if (memcmp (SIGNATURE_OLDOFFICE3, input_buf, 12)) return (PARSER_SIGNATURE_UNMATCHED);
16022
16023 return oldoffice34_parse_hash (input_buf, input_len, hash_buf);
16024 }
16025
16026 int oldoffice34cm2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16027 {
16028 if ((input_len < DISPLAY_LEN_MIN_9820) || (input_len > DISPLAY_LEN_MAX_9820)) return (PARSER_GLOBAL_LENGTH);
16029
16030 if (memcmp (SIGNATURE_OLDOFFICE3, input_buf, 12)) return (PARSER_SIGNATURE_UNMATCHED);
16031
16032 u32 *digest = (u32 *) hash_buf->digest;
16033
16034 salt_t *salt = hash_buf->salt;
16035
16036 oldoffice34_t *oldoffice34 = (oldoffice34_t *) hash_buf->esalt;
16037
16038 /**
16039 * parse line
16040 */
16041
16042 char *version_pos = input_buf + 11;
16043
16044 char *osalt_pos = strchr (version_pos, '*');
16045
16046 if (osalt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16047
16048 u32 version_len = osalt_pos - version_pos;
16049
16050 if (version_len != 1) return (PARSER_SALT_LENGTH);
16051
16052 osalt_pos++;
16053
16054 char *encryptedVerifier_pos = strchr (osalt_pos, '*');
16055
16056 if (encryptedVerifier_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16057
16058 u32 osalt_len = encryptedVerifier_pos - osalt_pos;
16059
16060 if (osalt_len != 32) return (PARSER_SALT_LENGTH);
16061
16062 encryptedVerifier_pos++;
16063
16064 char *encryptedVerifierHash_pos = strchr (encryptedVerifier_pos, '*');
16065
16066 if (encryptedVerifierHash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16067
16068 u32 encryptedVerifier_len = encryptedVerifierHash_pos - encryptedVerifier_pos;
16069
16070 if (encryptedVerifier_len != 32) return (PARSER_SALT_LENGTH);
16071
16072 encryptedVerifierHash_pos++;
16073
16074 char *rc4key_pos = strchr (encryptedVerifierHash_pos, ':');
16075
16076 if (rc4key_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16077
16078 u32 encryptedVerifierHash_len = rc4key_pos - encryptedVerifierHash_pos;
16079
16080 if (encryptedVerifierHash_len != 40) return (PARSER_SALT_LENGTH);
16081
16082 rc4key_pos++;
16083
16084 u32 rc4key_len = input_len - 11 - version_len - 1 - osalt_len - 1 - encryptedVerifier_len - 1 - encryptedVerifierHash_len - 1;
16085
16086 if (rc4key_len != 10) return (PARSER_SALT_LENGTH);
16087
16088 const uint version = *version_pos - 0x30;
16089
16090 if (version != 3 && version != 4) return (PARSER_SALT_VALUE);
16091
16092 /**
16093 * esalt
16094 */
16095
16096 oldoffice34->version = version;
16097
16098 oldoffice34->encryptedVerifier[0] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 0]);
16099 oldoffice34->encryptedVerifier[1] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 8]);
16100 oldoffice34->encryptedVerifier[2] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[16]);
16101 oldoffice34->encryptedVerifier[3] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[24]);
16102
16103 oldoffice34->encryptedVerifier[0] = byte_swap_32 (oldoffice34->encryptedVerifier[0]);
16104 oldoffice34->encryptedVerifier[1] = byte_swap_32 (oldoffice34->encryptedVerifier[1]);
16105 oldoffice34->encryptedVerifier[2] = byte_swap_32 (oldoffice34->encryptedVerifier[2]);
16106 oldoffice34->encryptedVerifier[3] = byte_swap_32 (oldoffice34->encryptedVerifier[3]);
16107
16108 oldoffice34->encryptedVerifierHash[0] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 0]);
16109 oldoffice34->encryptedVerifierHash[1] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 8]);
16110 oldoffice34->encryptedVerifierHash[2] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[16]);
16111 oldoffice34->encryptedVerifierHash[3] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[24]);
16112 oldoffice34->encryptedVerifierHash[4] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[32]);
16113
16114 oldoffice34->encryptedVerifierHash[0] = byte_swap_32 (oldoffice34->encryptedVerifierHash[0]);
16115 oldoffice34->encryptedVerifierHash[1] = byte_swap_32 (oldoffice34->encryptedVerifierHash[1]);
16116 oldoffice34->encryptedVerifierHash[2] = byte_swap_32 (oldoffice34->encryptedVerifierHash[2]);
16117 oldoffice34->encryptedVerifierHash[3] = byte_swap_32 (oldoffice34->encryptedVerifierHash[3]);
16118 oldoffice34->encryptedVerifierHash[4] = byte_swap_32 (oldoffice34->encryptedVerifierHash[4]);
16119
16120 oldoffice34->rc4key[1] = 0;
16121 oldoffice34->rc4key[0] = 0;
16122
16123 oldoffice34->rc4key[0] |= hex_convert (rc4key_pos[0]) << 28;
16124 oldoffice34->rc4key[0] |= hex_convert (rc4key_pos[1]) << 24;
16125 oldoffice34->rc4key[0] |= hex_convert (rc4key_pos[2]) << 20;
16126 oldoffice34->rc4key[0] |= hex_convert (rc4key_pos[3]) << 16;
16127 oldoffice34->rc4key[0] |= hex_convert (rc4key_pos[4]) << 12;
16128 oldoffice34->rc4key[0] |= hex_convert (rc4key_pos[5]) << 8;
16129 oldoffice34->rc4key[0] |= hex_convert (rc4key_pos[6]) << 4;
16130 oldoffice34->rc4key[0] |= hex_convert (rc4key_pos[7]) << 0;
16131 oldoffice34->rc4key[1] |= hex_convert (rc4key_pos[8]) << 28;
16132 oldoffice34->rc4key[1] |= hex_convert (rc4key_pos[9]) << 24;
16133
16134 oldoffice34->rc4key[0] = byte_swap_32 (oldoffice34->rc4key[0]);
16135 oldoffice34->rc4key[1] = byte_swap_32 (oldoffice34->rc4key[1]);
16136
16137 /**
16138 * salt
16139 */
16140
16141 salt->salt_len = 16;
16142
16143 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &osalt_pos[ 0]);
16144 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &osalt_pos[ 8]);
16145 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &osalt_pos[16]);
16146 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &osalt_pos[24]);
16147
16148 // this is a workaround as office produces multiple documents with the same salt
16149
16150 salt->salt_len += 32;
16151
16152 salt->salt_buf[ 4] = oldoffice34->encryptedVerifier[0];
16153 salt->salt_buf[ 5] = oldoffice34->encryptedVerifier[1];
16154 salt->salt_buf[ 6] = oldoffice34->encryptedVerifier[2];
16155 salt->salt_buf[ 7] = oldoffice34->encryptedVerifier[3];
16156 salt->salt_buf[ 8] = oldoffice34->encryptedVerifierHash[0];
16157 salt->salt_buf[ 9] = oldoffice34->encryptedVerifierHash[1];
16158 salt->salt_buf[10] = oldoffice34->encryptedVerifierHash[2];
16159 salt->salt_buf[11] = oldoffice34->encryptedVerifierHash[3];
16160
16161 /**
16162 * digest
16163 */
16164
16165 digest[0] = oldoffice34->rc4key[0];
16166 digest[1] = oldoffice34->rc4key[1];
16167 digest[2] = 0;
16168 digest[3] = 0;
16169
16170 return (PARSER_OK);
16171 }
16172
16173 int radmin2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16174 {
16175 if ((input_len < DISPLAY_LEN_MIN_9900) || (input_len > DISPLAY_LEN_MAX_9900)) return (PARSER_GLOBAL_LENGTH);
16176
16177 u32 *digest = (u32 *) hash_buf->digest;
16178
16179 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
16180 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
16181 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
16182 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
16183
16184 digest[0] = byte_swap_32 (digest[0]);
16185 digest[1] = byte_swap_32 (digest[1]);
16186 digest[2] = byte_swap_32 (digest[2]);
16187 digest[3] = byte_swap_32 (digest[3]);
16188
16189 return (PARSER_OK);
16190 }
16191
16192 int djangosha1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16193 {
16194 if ((input_len < DISPLAY_LEN_MIN_124) || (input_len > DISPLAY_LEN_MAX_124)) return (PARSER_GLOBAL_LENGTH);
16195
16196 if ((memcmp (SIGNATURE_DJANGOSHA1, input_buf, 5)) && (memcmp (SIGNATURE_DJANGOSHA1, input_buf, 5))) return (PARSER_SIGNATURE_UNMATCHED);
16197
16198 u32 *digest = (u32 *) hash_buf->digest;
16199
16200 salt_t *salt = hash_buf->salt;
16201
16202 char *signature_pos = input_buf;
16203
16204 char *salt_pos = strchr (signature_pos, '$');
16205
16206 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16207
16208 u32 signature_len = salt_pos - signature_pos;
16209
16210 if (signature_len != 4) return (PARSER_SIGNATURE_UNMATCHED);
16211
16212 salt_pos++;
16213
16214 char *hash_pos = strchr (salt_pos, '$');
16215
16216 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16217
16218 u32 salt_len = hash_pos - salt_pos;
16219
16220 if (salt_len > 32) return (PARSER_SALT_LENGTH);
16221
16222 hash_pos++;
16223
16224 u32 hash_len = input_len - signature_len - 1 - salt_len - 1;
16225
16226 if (hash_len != 40) return (PARSER_SALT_LENGTH);
16227
16228 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
16229 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
16230 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
16231 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
16232 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
16233
16234 digest[0] -= SHA1M_A;
16235 digest[1] -= SHA1M_B;
16236 digest[2] -= SHA1M_C;
16237 digest[3] -= SHA1M_D;
16238 digest[4] -= SHA1M_E;
16239
16240 char *salt_buf_ptr = (char *) salt->salt_buf;
16241
16242 memcpy (salt_buf_ptr, salt_pos, salt_len);
16243
16244 salt->salt_len = salt_len;
16245
16246 return (PARSER_OK);
16247 }
16248
16249 int djangopbkdf2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16250 {
16251 if ((input_len < DISPLAY_LEN_MIN_10000) || (input_len > DISPLAY_LEN_MAX_10000)) return (PARSER_GLOBAL_LENGTH);
16252
16253 if (memcmp (SIGNATURE_DJANGOPBKDF2, input_buf, 14)) return (PARSER_SIGNATURE_UNMATCHED);
16254
16255 u32 *digest = (u32 *) hash_buf->digest;
16256
16257 salt_t *salt = hash_buf->salt;
16258
16259 pbkdf2_sha256_t *pbkdf2_sha256 = (pbkdf2_sha256_t *) hash_buf->esalt;
16260
16261 /**
16262 * parse line
16263 */
16264
16265 char *iter_pos = input_buf + 14;
16266
16267 const int iter = atoi (iter_pos);
16268
16269 if (iter < 1) return (PARSER_SALT_ITERATION);
16270
16271 salt->salt_iter = iter - 1;
16272
16273 char *salt_pos = strchr (iter_pos, '$');
16274
16275 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16276
16277 salt_pos++;
16278
16279 char *hash_pos = strchr (salt_pos, '$');
16280
16281 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16282
16283 const uint salt_len = hash_pos - salt_pos;
16284
16285 hash_pos++;
16286
16287 char *salt_buf_ptr = (char *) pbkdf2_sha256->salt_buf;
16288
16289 memcpy (salt_buf_ptr, salt_pos, salt_len);
16290
16291 salt->salt_len = salt_len;
16292
16293 salt_buf_ptr[salt_len + 3] = 0x01;
16294 salt_buf_ptr[salt_len + 4] = 0x80;
16295
16296 // add some stuff to normal salt to make sorted happy
16297
16298 salt->salt_buf[0] = pbkdf2_sha256->salt_buf[0];
16299 salt->salt_buf[1] = pbkdf2_sha256->salt_buf[1];
16300 salt->salt_buf[2] = pbkdf2_sha256->salt_buf[2];
16301 salt->salt_buf[3] = pbkdf2_sha256->salt_buf[3];
16302 salt->salt_buf[4] = salt->salt_iter;
16303
16304 // base64 decode hash
16305
16306 u8 tmp_buf[100] = { 0 };
16307
16308 uint hash_len = input_len - (hash_pos - input_buf);
16309
16310 if (hash_len != 44) return (PARSER_HASH_LENGTH);
16311
16312 base64_decode (base64_to_int, (const u8 *) hash_pos, hash_len, tmp_buf);
16313
16314 memcpy (digest, tmp_buf, 32);
16315
16316 digest[0] = byte_swap_32 (digest[0]);
16317 digest[1] = byte_swap_32 (digest[1]);
16318 digest[2] = byte_swap_32 (digest[2]);
16319 digest[3] = byte_swap_32 (digest[3]);
16320 digest[4] = byte_swap_32 (digest[4]);
16321 digest[5] = byte_swap_32 (digest[5]);
16322 digest[6] = byte_swap_32 (digest[6]);
16323 digest[7] = byte_swap_32 (digest[7]);
16324
16325 return (PARSER_OK);
16326 }
16327
16328 int siphash_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16329 {
16330 if ((input_len < DISPLAY_LEN_MIN_10100) || (input_len > DISPLAY_LEN_MAX_10100)) return (PARSER_GLOBAL_LENGTH);
16331
16332 u32 *digest = (u32 *) hash_buf->digest;
16333
16334 salt_t *salt = hash_buf->salt;
16335
16336 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
16337 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
16338 digest[2] = 0;
16339 digest[3] = 0;
16340
16341 digest[0] = byte_swap_32 (digest[0]);
16342 digest[1] = byte_swap_32 (digest[1]);
16343
16344 if (input_buf[16] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
16345 if (input_buf[18] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
16346 if (input_buf[20] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
16347
16348 char iter_c = input_buf[17];
16349 char iter_d = input_buf[19];
16350
16351 // atm only defaults, let's see if there's more request
16352 if (iter_c != '2') return (PARSER_SALT_ITERATION);
16353 if (iter_d != '4') return (PARSER_SALT_ITERATION);
16354
16355 char *salt_buf = input_buf + 16 + 1 + 1 + 1 + 1 + 1;
16356
16357 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_buf[ 0]);
16358 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_buf[ 8]);
16359 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &salt_buf[16]);
16360 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &salt_buf[24]);
16361
16362 salt->salt_buf[0] = byte_swap_32 (salt->salt_buf[0]);
16363 salt->salt_buf[1] = byte_swap_32 (salt->salt_buf[1]);
16364 salt->salt_buf[2] = byte_swap_32 (salt->salt_buf[2]);
16365 salt->salt_buf[3] = byte_swap_32 (salt->salt_buf[3]);
16366
16367 salt->salt_len = 16;
16368
16369 return (PARSER_OK);
16370 }
16371
16372 int crammd5_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16373 {
16374 if ((input_len < DISPLAY_LEN_MIN_10200) || (input_len > DISPLAY_LEN_MAX_10200)) return (PARSER_GLOBAL_LENGTH);
16375
16376 if (memcmp (SIGNATURE_CRAM_MD5, input_buf, 10)) return (PARSER_SIGNATURE_UNMATCHED);
16377
16378 u32 *digest = (u32 *) hash_buf->digest;
16379
16380 cram_md5_t *cram_md5 = (cram_md5_t *) hash_buf->esalt;
16381
16382 salt_t *salt = hash_buf->salt;
16383
16384 char *salt_pos = input_buf + 10;
16385
16386 char *hash_pos = strchr (salt_pos, '$');
16387
16388 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16389
16390 uint salt_len = hash_pos - salt_pos;
16391
16392 hash_pos++;
16393
16394 uint hash_len = input_len - 10 - salt_len - 1;
16395
16396 // base64 decode salt
16397
16398 if (salt_len > 133) return (PARSER_SALT_LENGTH);
16399
16400 u8 tmp_buf[100] = { 0 };
16401
16402 salt_len = base64_decode (base64_to_int, (const u8 *) salt_pos, salt_len, tmp_buf);
16403
16404 if (salt_len > 55) return (PARSER_SALT_LENGTH);
16405
16406 tmp_buf[salt_len] = 0x80;
16407
16408 memcpy (&salt->salt_buf, tmp_buf, salt_len + 1);
16409
16410 salt->salt_len = salt_len;
16411
16412 // base64 decode hash
16413
16414 if (hash_len > 133) return (PARSER_HASH_LENGTH);
16415
16416 memset (tmp_buf, 0, sizeof (tmp_buf));
16417
16418 hash_len = base64_decode (base64_to_int, (const u8 *) hash_pos, hash_len, tmp_buf);
16419
16420 if (hash_len < 32 + 1) return (PARSER_SALT_LENGTH);
16421
16422 uint user_len = hash_len - 32;
16423
16424 const u8 *tmp_hash = tmp_buf + user_len;
16425
16426 user_len--; // skip the trailing space
16427
16428 digest[0] = hex_to_u32 (&tmp_hash[ 0]);
16429 digest[1] = hex_to_u32 (&tmp_hash[ 8]);
16430 digest[2] = hex_to_u32 (&tmp_hash[16]);
16431 digest[3] = hex_to_u32 (&tmp_hash[24]);
16432
16433 digest[0] = byte_swap_32 (digest[0]);
16434 digest[1] = byte_swap_32 (digest[1]);
16435 digest[2] = byte_swap_32 (digest[2]);
16436 digest[3] = byte_swap_32 (digest[3]);
16437
16438 // store username for host only (output hash if cracked)
16439
16440 memset (cram_md5->user, 0, sizeof (cram_md5->user));
16441 memcpy (cram_md5->user, tmp_buf, user_len);
16442
16443 return (PARSER_OK);
16444 }
16445
16446 int saph_sha1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16447 {
16448 if ((input_len < DISPLAY_LEN_MIN_10300) || (input_len > DISPLAY_LEN_MAX_10300)) return (PARSER_GLOBAL_LENGTH);
16449
16450 if (memcmp (SIGNATURE_SAPH_SHA1, input_buf, 10)) return (PARSER_SIGNATURE_UNMATCHED);
16451
16452 u32 *digest = (u32 *) hash_buf->digest;
16453
16454 salt_t *salt = hash_buf->salt;
16455
16456 char *iter_pos = input_buf + 10;
16457
16458 u32 iter = atoi (iter_pos);
16459
16460 if (iter < 1)
16461 {
16462 return (PARSER_SALT_ITERATION);
16463 }
16464
16465 iter--; // first iteration is special
16466
16467 salt->salt_iter = iter;
16468
16469 char *base64_pos = strchr (iter_pos, '}');
16470
16471 if (base64_pos == NULL)
16472 {
16473 return (PARSER_SIGNATURE_UNMATCHED);
16474 }
16475
16476 base64_pos++;
16477
16478 // base64 decode salt
16479
16480 u32 base64_len = input_len - (base64_pos - input_buf);
16481
16482 u8 tmp_buf[100] = { 0 };
16483
16484 u32 decoded_len = base64_decode (base64_to_int, (const u8 *) base64_pos, base64_len, tmp_buf);
16485
16486 if (decoded_len < 24)
16487 {
16488 return (PARSER_SALT_LENGTH);
16489 }
16490
16491 // copy the salt
16492
16493 uint salt_len = decoded_len - 20;
16494
16495 if (salt_len < 4) return (PARSER_SALT_LENGTH);
16496 if (salt_len > 16) return (PARSER_SALT_LENGTH);
16497
16498 memcpy (&salt->salt_buf, tmp_buf + 20, salt_len);
16499
16500 salt->salt_len = salt_len;
16501
16502 // set digest
16503
16504 u32 *digest_ptr = (u32*) tmp_buf;
16505
16506 digest[0] = byte_swap_32 (digest_ptr[0]);
16507 digest[1] = byte_swap_32 (digest_ptr[1]);
16508 digest[2] = byte_swap_32 (digest_ptr[2]);
16509 digest[3] = byte_swap_32 (digest_ptr[3]);
16510 digest[4] = byte_swap_32 (digest_ptr[4]);
16511
16512 return (PARSER_OK);
16513 }
16514
16515 int redmine_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16516 {
16517 if ((input_len < DISPLAY_LEN_MIN_7600) || (input_len > DISPLAY_LEN_MAX_7600)) return (PARSER_GLOBAL_LENGTH);
16518
16519 u32 *digest = (u32 *) hash_buf->digest;
16520
16521 salt_t *salt = hash_buf->salt;
16522
16523 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
16524 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
16525 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
16526 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
16527 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
16528
16529 if (input_buf[40] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
16530
16531 uint salt_len = input_len - 40 - 1;
16532
16533 char *salt_buf = input_buf + 40 + 1;
16534
16535 char *salt_buf_ptr = (char *) salt->salt_buf;
16536
16537 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
16538
16539 if (salt_len != 32) return (PARSER_SALT_LENGTH);
16540
16541 salt->salt_len = salt_len;
16542
16543 return (PARSER_OK);
16544 }
16545
16546 int pdf11_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16547 {
16548 if ((input_len < DISPLAY_LEN_MIN_10400) || (input_len > DISPLAY_LEN_MAX_10400)) return (PARSER_GLOBAL_LENGTH);
16549
16550 if ((memcmp (SIGNATURE_PDF, input_buf, 5)) && (memcmp (SIGNATURE_PDF, input_buf, 5))) return (PARSER_SIGNATURE_UNMATCHED);
16551
16552 u32 *digest = (u32 *) hash_buf->digest;
16553
16554 salt_t *salt = hash_buf->salt;
16555
16556 pdf_t *pdf = (pdf_t *) hash_buf->esalt;
16557
16558 /**
16559 * parse line
16560 */
16561
16562 char *V_pos = input_buf + 5;
16563
16564 char *R_pos = strchr (V_pos, '*');
16565
16566 if (R_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16567
16568 u32 V_len = R_pos - V_pos;
16569
16570 R_pos++;
16571
16572 char *bits_pos = strchr (R_pos, '*');
16573
16574 if (bits_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16575
16576 u32 R_len = bits_pos - R_pos;
16577
16578 bits_pos++;
16579
16580 char *P_pos = strchr (bits_pos, '*');
16581
16582 if (P_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16583
16584 u32 bits_len = P_pos - bits_pos;
16585
16586 P_pos++;
16587
16588 char *enc_md_pos = strchr (P_pos, '*');
16589
16590 if (enc_md_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16591
16592 u32 P_len = enc_md_pos - P_pos;
16593
16594 enc_md_pos++;
16595
16596 char *id_len_pos = strchr (enc_md_pos, '*');
16597
16598 if (id_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16599
16600 u32 enc_md_len = id_len_pos - enc_md_pos;
16601
16602 id_len_pos++;
16603
16604 char *id_buf_pos = strchr (id_len_pos, '*');
16605
16606 if (id_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16607
16608 u32 id_len_len = id_buf_pos - id_len_pos;
16609
16610 id_buf_pos++;
16611
16612 char *u_len_pos = strchr (id_buf_pos, '*');
16613
16614 if (u_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16615
16616 u32 id_buf_len = u_len_pos - id_buf_pos;
16617
16618 if (id_buf_len != 32) return (PARSER_SALT_LENGTH);
16619
16620 u_len_pos++;
16621
16622 char *u_buf_pos = strchr (u_len_pos, '*');
16623
16624 if (u_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16625
16626 u32 u_len_len = u_buf_pos - u_len_pos;
16627
16628 u_buf_pos++;
16629
16630 char *o_len_pos = strchr (u_buf_pos, '*');
16631
16632 if (o_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16633
16634 u32 u_buf_len = o_len_pos - u_buf_pos;
16635
16636 if (u_buf_len != 64) return (PARSER_SALT_LENGTH);
16637
16638 o_len_pos++;
16639
16640 char *o_buf_pos = strchr (o_len_pos, '*');
16641
16642 if (o_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16643
16644 u32 o_len_len = o_buf_pos - o_len_pos;
16645
16646 o_buf_pos++;
16647
16648 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;
16649
16650 if (o_buf_len != 64) return (PARSER_SALT_LENGTH);
16651
16652 // validate data
16653
16654 const int V = atoi (V_pos);
16655 const int R = atoi (R_pos);
16656 const int P = atoi (P_pos);
16657
16658 if (V != 1) return (PARSER_SALT_VALUE);
16659 if (R != 2) return (PARSER_SALT_VALUE);
16660
16661 const int enc_md = atoi (enc_md_pos);
16662
16663 if ((enc_md != 0) && (enc_md != 1)) return (PARSER_SALT_VALUE);
16664
16665 const int id_len = atoi (id_len_pos);
16666 const int u_len = atoi (u_len_pos);
16667 const int o_len = atoi (o_len_pos);
16668
16669 if (id_len != 16) return (PARSER_SALT_VALUE);
16670 if (u_len != 32) return (PARSER_SALT_VALUE);
16671 if (o_len != 32) return (PARSER_SALT_VALUE);
16672
16673 const int bits = atoi (bits_pos);
16674
16675 if (bits != 40) return (PARSER_SALT_VALUE);
16676
16677 // copy data to esalt
16678
16679 pdf->V = V;
16680 pdf->R = R;
16681 pdf->P = P;
16682
16683 pdf->enc_md = enc_md;
16684
16685 pdf->id_buf[0] = hex_to_u32 ((const u8 *) &id_buf_pos[ 0]);
16686 pdf->id_buf[1] = hex_to_u32 ((const u8 *) &id_buf_pos[ 8]);
16687 pdf->id_buf[2] = hex_to_u32 ((const u8 *) &id_buf_pos[16]);
16688 pdf->id_buf[3] = hex_to_u32 ((const u8 *) &id_buf_pos[24]);
16689 pdf->id_len = id_len;
16690
16691 pdf->u_buf[0] = hex_to_u32 ((const u8 *) &u_buf_pos[ 0]);
16692 pdf->u_buf[1] = hex_to_u32 ((const u8 *) &u_buf_pos[ 8]);
16693 pdf->u_buf[2] = hex_to_u32 ((const u8 *) &u_buf_pos[16]);
16694 pdf->u_buf[3] = hex_to_u32 ((const u8 *) &u_buf_pos[24]);
16695 pdf->u_buf[4] = hex_to_u32 ((const u8 *) &u_buf_pos[32]);
16696 pdf->u_buf[5] = hex_to_u32 ((const u8 *) &u_buf_pos[40]);
16697 pdf->u_buf[6] = hex_to_u32 ((const u8 *) &u_buf_pos[48]);
16698 pdf->u_buf[7] = hex_to_u32 ((const u8 *) &u_buf_pos[56]);
16699 pdf->u_len = u_len;
16700
16701 pdf->o_buf[0] = hex_to_u32 ((const u8 *) &o_buf_pos[ 0]);
16702 pdf->o_buf[1] = hex_to_u32 ((const u8 *) &o_buf_pos[ 8]);
16703 pdf->o_buf[2] = hex_to_u32 ((const u8 *) &o_buf_pos[16]);
16704 pdf->o_buf[3] = hex_to_u32 ((const u8 *) &o_buf_pos[24]);
16705 pdf->o_buf[4] = hex_to_u32 ((const u8 *) &o_buf_pos[32]);
16706 pdf->o_buf[5] = hex_to_u32 ((const u8 *) &o_buf_pos[40]);
16707 pdf->o_buf[6] = hex_to_u32 ((const u8 *) &o_buf_pos[48]);
16708 pdf->o_buf[7] = hex_to_u32 ((const u8 *) &o_buf_pos[56]);
16709 pdf->o_len = o_len;
16710
16711 pdf->id_buf[0] = byte_swap_32 (pdf->id_buf[0]);
16712 pdf->id_buf[1] = byte_swap_32 (pdf->id_buf[1]);
16713 pdf->id_buf[2] = byte_swap_32 (pdf->id_buf[2]);
16714 pdf->id_buf[3] = byte_swap_32 (pdf->id_buf[3]);
16715
16716 pdf->u_buf[0] = byte_swap_32 (pdf->u_buf[0]);
16717 pdf->u_buf[1] = byte_swap_32 (pdf->u_buf[1]);
16718 pdf->u_buf[2] = byte_swap_32 (pdf->u_buf[2]);
16719 pdf->u_buf[3] = byte_swap_32 (pdf->u_buf[3]);
16720 pdf->u_buf[4] = byte_swap_32 (pdf->u_buf[4]);
16721 pdf->u_buf[5] = byte_swap_32 (pdf->u_buf[5]);
16722 pdf->u_buf[6] = byte_swap_32 (pdf->u_buf[6]);
16723 pdf->u_buf[7] = byte_swap_32 (pdf->u_buf[7]);
16724
16725 pdf->o_buf[0] = byte_swap_32 (pdf->o_buf[0]);
16726 pdf->o_buf[1] = byte_swap_32 (pdf->o_buf[1]);
16727 pdf->o_buf[2] = byte_swap_32 (pdf->o_buf[2]);
16728 pdf->o_buf[3] = byte_swap_32 (pdf->o_buf[3]);
16729 pdf->o_buf[4] = byte_swap_32 (pdf->o_buf[4]);
16730 pdf->o_buf[5] = byte_swap_32 (pdf->o_buf[5]);
16731 pdf->o_buf[6] = byte_swap_32 (pdf->o_buf[6]);
16732 pdf->o_buf[7] = byte_swap_32 (pdf->o_buf[7]);
16733
16734 // we use ID for salt, maybe needs to change, we will see...
16735
16736 salt->salt_buf[0] = pdf->id_buf[0];
16737 salt->salt_buf[1] = pdf->id_buf[1];
16738 salt->salt_buf[2] = pdf->id_buf[2];
16739 salt->salt_buf[3] = pdf->id_buf[3];
16740 salt->salt_len = pdf->id_len;
16741
16742 digest[0] = pdf->u_buf[0];
16743 digest[1] = pdf->u_buf[1];
16744 digest[2] = pdf->u_buf[2];
16745 digest[3] = pdf->u_buf[3];
16746
16747 return (PARSER_OK);
16748 }
16749
16750 int pdf11cm1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16751 {
16752 return pdf11_parse_hash (input_buf, input_len, hash_buf);
16753 }
16754
16755 int pdf11cm2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16756 {
16757 if ((input_len < DISPLAY_LEN_MIN_10420) || (input_len > DISPLAY_LEN_MAX_10420)) return (PARSER_GLOBAL_LENGTH);
16758
16759 if ((memcmp (SIGNATURE_PDF, input_buf, 5)) && (memcmp (SIGNATURE_PDF, input_buf, 5))) return (PARSER_SIGNATURE_UNMATCHED);
16760
16761 u32 *digest = (u32 *) hash_buf->digest;
16762
16763 salt_t *salt = hash_buf->salt;
16764
16765 pdf_t *pdf = (pdf_t *) hash_buf->esalt;
16766
16767 /**
16768 * parse line
16769 */
16770
16771 char *V_pos = input_buf + 5;
16772
16773 char *R_pos = strchr (V_pos, '*');
16774
16775 if (R_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16776
16777 u32 V_len = R_pos - V_pos;
16778
16779 R_pos++;
16780
16781 char *bits_pos = strchr (R_pos, '*');
16782
16783 if (bits_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16784
16785 u32 R_len = bits_pos - R_pos;
16786
16787 bits_pos++;
16788
16789 char *P_pos = strchr (bits_pos, '*');
16790
16791 if (P_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16792
16793 u32 bits_len = P_pos - bits_pos;
16794
16795 P_pos++;
16796
16797 char *enc_md_pos = strchr (P_pos, '*');
16798
16799 if (enc_md_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16800
16801 u32 P_len = enc_md_pos - P_pos;
16802
16803 enc_md_pos++;
16804
16805 char *id_len_pos = strchr (enc_md_pos, '*');
16806
16807 if (id_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16808
16809 u32 enc_md_len = id_len_pos - enc_md_pos;
16810
16811 id_len_pos++;
16812
16813 char *id_buf_pos = strchr (id_len_pos, '*');
16814
16815 if (id_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16816
16817 u32 id_len_len = id_buf_pos - id_len_pos;
16818
16819 id_buf_pos++;
16820
16821 char *u_len_pos = strchr (id_buf_pos, '*');
16822
16823 if (u_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16824
16825 u32 id_buf_len = u_len_pos - id_buf_pos;
16826
16827 if (id_buf_len != 32) return (PARSER_SALT_LENGTH);
16828
16829 u_len_pos++;
16830
16831 char *u_buf_pos = strchr (u_len_pos, '*');
16832
16833 if (u_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16834
16835 u32 u_len_len = u_buf_pos - u_len_pos;
16836
16837 u_buf_pos++;
16838
16839 char *o_len_pos = strchr (u_buf_pos, '*');
16840
16841 if (o_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16842
16843 u32 u_buf_len = o_len_pos - u_buf_pos;
16844
16845 if (u_buf_len != 64) return (PARSER_SALT_LENGTH);
16846
16847 o_len_pos++;
16848
16849 char *o_buf_pos = strchr (o_len_pos, '*');
16850
16851 if (o_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16852
16853 u32 o_len_len = o_buf_pos - o_len_pos;
16854
16855 o_buf_pos++;
16856
16857 char *rc4key_pos = strchr (o_buf_pos, ':');
16858
16859 if (rc4key_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16860
16861 u32 o_buf_len = rc4key_pos - o_buf_pos;
16862
16863 if (o_buf_len != 64) return (PARSER_SALT_LENGTH);
16864
16865 rc4key_pos++;
16866
16867 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;
16868
16869 if (rc4key_len != 10) return (PARSER_SALT_LENGTH);
16870
16871 // validate data
16872
16873 const int V = atoi (V_pos);
16874 const int R = atoi (R_pos);
16875 const int P = atoi (P_pos);
16876
16877 if (V != 1) return (PARSER_SALT_VALUE);
16878 if (R != 2) return (PARSER_SALT_VALUE);
16879
16880 const int enc_md = atoi (enc_md_pos);
16881
16882 if ((enc_md != 0) && (enc_md != 1)) return (PARSER_SALT_VALUE);
16883
16884 const int id_len = atoi (id_len_pos);
16885 const int u_len = atoi (u_len_pos);
16886 const int o_len = atoi (o_len_pos);
16887
16888 if (id_len != 16) return (PARSER_SALT_VALUE);
16889 if (u_len != 32) return (PARSER_SALT_VALUE);
16890 if (o_len != 32) return (PARSER_SALT_VALUE);
16891
16892 const int bits = atoi (bits_pos);
16893
16894 if (bits != 40) return (PARSER_SALT_VALUE);
16895
16896 // copy data to esalt
16897
16898 pdf->V = V;
16899 pdf->R = R;
16900 pdf->P = P;
16901
16902 pdf->enc_md = enc_md;
16903
16904 pdf->id_buf[0] = hex_to_u32 ((const u8 *) &id_buf_pos[ 0]);
16905 pdf->id_buf[1] = hex_to_u32 ((const u8 *) &id_buf_pos[ 8]);
16906 pdf->id_buf[2] = hex_to_u32 ((const u8 *) &id_buf_pos[16]);
16907 pdf->id_buf[3] = hex_to_u32 ((const u8 *) &id_buf_pos[24]);
16908 pdf->id_len = id_len;
16909
16910 pdf->u_buf[0] = hex_to_u32 ((const u8 *) &u_buf_pos[ 0]);
16911 pdf->u_buf[1] = hex_to_u32 ((const u8 *) &u_buf_pos[ 8]);
16912 pdf->u_buf[2] = hex_to_u32 ((const u8 *) &u_buf_pos[16]);
16913 pdf->u_buf[3] = hex_to_u32 ((const u8 *) &u_buf_pos[24]);
16914 pdf->u_buf[4] = hex_to_u32 ((const u8 *) &u_buf_pos[32]);
16915 pdf->u_buf[5] = hex_to_u32 ((const u8 *) &u_buf_pos[40]);
16916 pdf->u_buf[6] = hex_to_u32 ((const u8 *) &u_buf_pos[48]);
16917 pdf->u_buf[7] = hex_to_u32 ((const u8 *) &u_buf_pos[56]);
16918 pdf->u_len = u_len;
16919
16920 pdf->o_buf[0] = hex_to_u32 ((const u8 *) &o_buf_pos[ 0]);
16921 pdf->o_buf[1] = hex_to_u32 ((const u8 *) &o_buf_pos[ 8]);
16922 pdf->o_buf[2] = hex_to_u32 ((const u8 *) &o_buf_pos[16]);
16923 pdf->o_buf[3] = hex_to_u32 ((const u8 *) &o_buf_pos[24]);
16924 pdf->o_buf[4] = hex_to_u32 ((const u8 *) &o_buf_pos[32]);
16925 pdf->o_buf[5] = hex_to_u32 ((const u8 *) &o_buf_pos[40]);
16926 pdf->o_buf[6] = hex_to_u32 ((const u8 *) &o_buf_pos[48]);
16927 pdf->o_buf[7] = hex_to_u32 ((const u8 *) &o_buf_pos[56]);
16928 pdf->o_len = o_len;
16929
16930 pdf->id_buf[0] = byte_swap_32 (pdf->id_buf[0]);
16931 pdf->id_buf[1] = byte_swap_32 (pdf->id_buf[1]);
16932 pdf->id_buf[2] = byte_swap_32 (pdf->id_buf[2]);
16933 pdf->id_buf[3] = byte_swap_32 (pdf->id_buf[3]);
16934
16935 pdf->u_buf[0] = byte_swap_32 (pdf->u_buf[0]);
16936 pdf->u_buf[1] = byte_swap_32 (pdf->u_buf[1]);
16937 pdf->u_buf[2] = byte_swap_32 (pdf->u_buf[2]);
16938 pdf->u_buf[3] = byte_swap_32 (pdf->u_buf[3]);
16939 pdf->u_buf[4] = byte_swap_32 (pdf->u_buf[4]);
16940 pdf->u_buf[5] = byte_swap_32 (pdf->u_buf[5]);
16941 pdf->u_buf[6] = byte_swap_32 (pdf->u_buf[6]);
16942 pdf->u_buf[7] = byte_swap_32 (pdf->u_buf[7]);
16943
16944 pdf->o_buf[0] = byte_swap_32 (pdf->o_buf[0]);
16945 pdf->o_buf[1] = byte_swap_32 (pdf->o_buf[1]);
16946 pdf->o_buf[2] = byte_swap_32 (pdf->o_buf[2]);
16947 pdf->o_buf[3] = byte_swap_32 (pdf->o_buf[3]);
16948 pdf->o_buf[4] = byte_swap_32 (pdf->o_buf[4]);
16949 pdf->o_buf[5] = byte_swap_32 (pdf->o_buf[5]);
16950 pdf->o_buf[6] = byte_swap_32 (pdf->o_buf[6]);
16951 pdf->o_buf[7] = byte_swap_32 (pdf->o_buf[7]);
16952
16953 pdf->rc4key[1] = 0;
16954 pdf->rc4key[0] = 0;
16955
16956 pdf->rc4key[0] |= hex_convert (rc4key_pos[0]) << 28;
16957 pdf->rc4key[0] |= hex_convert (rc4key_pos[1]) << 24;
16958 pdf->rc4key[0] |= hex_convert (rc4key_pos[2]) << 20;
16959 pdf->rc4key[0] |= hex_convert (rc4key_pos[3]) << 16;
16960 pdf->rc4key[0] |= hex_convert (rc4key_pos[4]) << 12;
16961 pdf->rc4key[0] |= hex_convert (rc4key_pos[5]) << 8;
16962 pdf->rc4key[0] |= hex_convert (rc4key_pos[6]) << 4;
16963 pdf->rc4key[0] |= hex_convert (rc4key_pos[7]) << 0;
16964 pdf->rc4key[1] |= hex_convert (rc4key_pos[8]) << 28;
16965 pdf->rc4key[1] |= hex_convert (rc4key_pos[9]) << 24;
16966
16967 pdf->rc4key[0] = byte_swap_32 (pdf->rc4key[0]);
16968 pdf->rc4key[1] = byte_swap_32 (pdf->rc4key[1]);
16969
16970 // we use ID for salt, maybe needs to change, we will see...
16971
16972 salt->salt_buf[0] = pdf->id_buf[0];
16973 salt->salt_buf[1] = pdf->id_buf[1];
16974 salt->salt_buf[2] = pdf->id_buf[2];
16975 salt->salt_buf[3] = pdf->id_buf[3];
16976 salt->salt_buf[4] = pdf->u_buf[0];
16977 salt->salt_buf[5] = pdf->u_buf[1];
16978 salt->salt_buf[6] = pdf->o_buf[0];
16979 salt->salt_buf[7] = pdf->o_buf[1];
16980 salt->salt_len = pdf->id_len + 16;
16981
16982 digest[0] = pdf->rc4key[0];
16983 digest[1] = pdf->rc4key[1];
16984 digest[2] = 0;
16985 digest[3] = 0;
16986
16987 return (PARSER_OK);
16988 }
16989
16990 int pdf14_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16991 {
16992 if ((input_len < DISPLAY_LEN_MIN_10500) || (input_len > DISPLAY_LEN_MAX_10500)) return (PARSER_GLOBAL_LENGTH);
16993
16994 if ((memcmp (SIGNATURE_PDF, input_buf, 5)) && (memcmp (SIGNATURE_PDF, input_buf, 5))) return (PARSER_SIGNATURE_UNMATCHED);
16995
16996 u32 *digest = (u32 *) hash_buf->digest;
16997
16998 salt_t *salt = hash_buf->salt;
16999
17000 pdf_t *pdf = (pdf_t *) hash_buf->esalt;
17001
17002 /**
17003 * parse line
17004 */
17005
17006 char *V_pos = input_buf + 5;
17007
17008 char *R_pos = strchr (V_pos, '*');
17009
17010 if (R_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17011
17012 u32 V_len = R_pos - V_pos;
17013
17014 R_pos++;
17015
17016 char *bits_pos = strchr (R_pos, '*');
17017
17018 if (bits_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17019
17020 u32 R_len = bits_pos - R_pos;
17021
17022 bits_pos++;
17023
17024 char *P_pos = strchr (bits_pos, '*');
17025
17026 if (P_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17027
17028 u32 bits_len = P_pos - bits_pos;
17029
17030 P_pos++;
17031
17032 char *enc_md_pos = strchr (P_pos, '*');
17033
17034 if (enc_md_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17035
17036 u32 P_len = enc_md_pos - P_pos;
17037
17038 enc_md_pos++;
17039
17040 char *id_len_pos = strchr (enc_md_pos, '*');
17041
17042 if (id_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17043
17044 u32 enc_md_len = id_len_pos - enc_md_pos;
17045
17046 id_len_pos++;
17047
17048 char *id_buf_pos = strchr (id_len_pos, '*');
17049
17050 if (id_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17051
17052 u32 id_len_len = id_buf_pos - id_len_pos;
17053
17054 id_buf_pos++;
17055
17056 char *u_len_pos = strchr (id_buf_pos, '*');
17057
17058 if (u_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17059
17060 u32 id_buf_len = u_len_pos - id_buf_pos;
17061
17062 if ((id_buf_len != 32) && (id_buf_len != 64)) return (PARSER_SALT_LENGTH);
17063
17064 u_len_pos++;
17065
17066 char *u_buf_pos = strchr (u_len_pos, '*');
17067
17068 if (u_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17069
17070 u32 u_len_len = u_buf_pos - u_len_pos;
17071
17072 u_buf_pos++;
17073
17074 char *o_len_pos = strchr (u_buf_pos, '*');
17075
17076 if (o_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17077
17078 u32 u_buf_len = o_len_pos - u_buf_pos;
17079
17080 if (u_buf_len != 64) return (PARSER_SALT_LENGTH);
17081
17082 o_len_pos++;
17083
17084 char *o_buf_pos = strchr (o_len_pos, '*');
17085
17086 if (o_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17087
17088 u32 o_len_len = o_buf_pos - o_len_pos;
17089
17090 o_buf_pos++;
17091
17092 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;
17093
17094 if (o_buf_len != 64) return (PARSER_SALT_LENGTH);
17095
17096 // validate data
17097
17098 const int V = atoi (V_pos);
17099 const int R = atoi (R_pos);
17100 const int P = atoi (P_pos);
17101
17102 int vr_ok = 0;
17103
17104 if ((V == 2) && (R == 3)) vr_ok = 1;
17105 if ((V == 4) && (R == 4)) vr_ok = 1;
17106
17107 if (vr_ok == 0) return (PARSER_SALT_VALUE);
17108
17109 const int id_len = atoi (id_len_pos);
17110 const int u_len = atoi (u_len_pos);
17111 const int o_len = atoi (o_len_pos);
17112
17113 if ((id_len != 16) && (id_len != 32)) return (PARSER_SALT_VALUE);
17114
17115 if (u_len != 32) return (PARSER_SALT_VALUE);
17116 if (o_len != 32) return (PARSER_SALT_VALUE);
17117
17118 const int bits = atoi (bits_pos);
17119
17120 if (bits != 128) return (PARSER_SALT_VALUE);
17121
17122 int enc_md = 1;
17123
17124 if (R >= 4)
17125 {
17126 enc_md = atoi (enc_md_pos);
17127 }
17128
17129 // copy data to esalt
17130
17131 pdf->V = V;
17132 pdf->R = R;
17133 pdf->P = P;
17134
17135 pdf->enc_md = enc_md;
17136
17137 pdf->id_buf[0] = hex_to_u32 ((const u8 *) &id_buf_pos[ 0]);
17138 pdf->id_buf[1] = hex_to_u32 ((const u8 *) &id_buf_pos[ 8]);
17139 pdf->id_buf[2] = hex_to_u32 ((const u8 *) &id_buf_pos[16]);
17140 pdf->id_buf[3] = hex_to_u32 ((const u8 *) &id_buf_pos[24]);
17141
17142 if (id_len == 32)
17143 {
17144 pdf->id_buf[4] = hex_to_u32 ((const u8 *) &id_buf_pos[32]);
17145 pdf->id_buf[5] = hex_to_u32 ((const u8 *) &id_buf_pos[40]);
17146 pdf->id_buf[6] = hex_to_u32 ((const u8 *) &id_buf_pos[48]);
17147 pdf->id_buf[7] = hex_to_u32 ((const u8 *) &id_buf_pos[56]);
17148 }
17149
17150 pdf->id_len = id_len;
17151
17152 pdf->u_buf[0] = hex_to_u32 ((const u8 *) &u_buf_pos[ 0]);
17153 pdf->u_buf[1] = hex_to_u32 ((const u8 *) &u_buf_pos[ 8]);
17154 pdf->u_buf[2] = hex_to_u32 ((const u8 *) &u_buf_pos[16]);
17155 pdf->u_buf[3] = hex_to_u32 ((const u8 *) &u_buf_pos[24]);
17156 pdf->u_buf[4] = hex_to_u32 ((const u8 *) &u_buf_pos[32]);
17157 pdf->u_buf[5] = hex_to_u32 ((const u8 *) &u_buf_pos[40]);
17158 pdf->u_buf[6] = hex_to_u32 ((const u8 *) &u_buf_pos[48]);
17159 pdf->u_buf[7] = hex_to_u32 ((const u8 *) &u_buf_pos[56]);
17160 pdf->u_len = u_len;
17161
17162 pdf->o_buf[0] = hex_to_u32 ((const u8 *) &o_buf_pos[ 0]);
17163 pdf->o_buf[1] = hex_to_u32 ((const u8 *) &o_buf_pos[ 8]);
17164 pdf->o_buf[2] = hex_to_u32 ((const u8 *) &o_buf_pos[16]);
17165 pdf->o_buf[3] = hex_to_u32 ((const u8 *) &o_buf_pos[24]);
17166 pdf->o_buf[4] = hex_to_u32 ((const u8 *) &o_buf_pos[32]);
17167 pdf->o_buf[5] = hex_to_u32 ((const u8 *) &o_buf_pos[40]);
17168 pdf->o_buf[6] = hex_to_u32 ((const u8 *) &o_buf_pos[48]);
17169 pdf->o_buf[7] = hex_to_u32 ((const u8 *) &o_buf_pos[56]);
17170 pdf->o_len = o_len;
17171
17172 pdf->id_buf[0] = byte_swap_32 (pdf->id_buf[0]);
17173 pdf->id_buf[1] = byte_swap_32 (pdf->id_buf[1]);
17174 pdf->id_buf[2] = byte_swap_32 (pdf->id_buf[2]);
17175 pdf->id_buf[3] = byte_swap_32 (pdf->id_buf[3]);
17176
17177 if (id_len == 32)
17178 {
17179 pdf->id_buf[4] = byte_swap_32 (pdf->id_buf[4]);
17180 pdf->id_buf[5] = byte_swap_32 (pdf->id_buf[5]);
17181 pdf->id_buf[6] = byte_swap_32 (pdf->id_buf[6]);
17182 pdf->id_buf[7] = byte_swap_32 (pdf->id_buf[7]);
17183 }
17184
17185 pdf->u_buf[0] = byte_swap_32 (pdf->u_buf[0]);
17186 pdf->u_buf[1] = byte_swap_32 (pdf->u_buf[1]);
17187 pdf->u_buf[2] = byte_swap_32 (pdf->u_buf[2]);
17188 pdf->u_buf[3] = byte_swap_32 (pdf->u_buf[3]);
17189 pdf->u_buf[4] = byte_swap_32 (pdf->u_buf[4]);
17190 pdf->u_buf[5] = byte_swap_32 (pdf->u_buf[5]);
17191 pdf->u_buf[6] = byte_swap_32 (pdf->u_buf[6]);
17192 pdf->u_buf[7] = byte_swap_32 (pdf->u_buf[7]);
17193
17194 pdf->o_buf[0] = byte_swap_32 (pdf->o_buf[0]);
17195 pdf->o_buf[1] = byte_swap_32 (pdf->o_buf[1]);
17196 pdf->o_buf[2] = byte_swap_32 (pdf->o_buf[2]);
17197 pdf->o_buf[3] = byte_swap_32 (pdf->o_buf[3]);
17198 pdf->o_buf[4] = byte_swap_32 (pdf->o_buf[4]);
17199 pdf->o_buf[5] = byte_swap_32 (pdf->o_buf[5]);
17200 pdf->o_buf[6] = byte_swap_32 (pdf->o_buf[6]);
17201 pdf->o_buf[7] = byte_swap_32 (pdf->o_buf[7]);
17202
17203 // precompute rc4 data for later use
17204
17205 uint padding[8] =
17206 {
17207 0x5e4ebf28,
17208 0x418a754e,
17209 0x564e0064,
17210 0x0801faff,
17211 0xb6002e2e,
17212 0x803e68d0,
17213 0xfea90c2f,
17214 0x7a695364
17215 };
17216
17217 // md5
17218
17219 uint salt_pc_block[32] = { 0 };
17220
17221 char *salt_pc_ptr = (char *) salt_pc_block;
17222
17223 memcpy (salt_pc_ptr, padding, 32);
17224 memcpy (salt_pc_ptr + 32, pdf->id_buf, pdf->id_len);
17225
17226 uint salt_pc_digest[4] = { 0 };
17227
17228 md5_complete_no_limit (salt_pc_digest, salt_pc_block, 32 + pdf->id_len);
17229
17230 pdf->rc4data[0] = salt_pc_digest[0];
17231 pdf->rc4data[1] = salt_pc_digest[1];
17232
17233 // we use ID for salt, maybe needs to change, we will see...
17234
17235 salt->salt_buf[0] = pdf->id_buf[0];
17236 salt->salt_buf[1] = pdf->id_buf[1];
17237 salt->salt_buf[2] = pdf->id_buf[2];
17238 salt->salt_buf[3] = pdf->id_buf[3];
17239 salt->salt_buf[4] = pdf->u_buf[0];
17240 salt->salt_buf[5] = pdf->u_buf[1];
17241 salt->salt_buf[6] = pdf->o_buf[0];
17242 salt->salt_buf[7] = pdf->o_buf[1];
17243 salt->salt_len = pdf->id_len + 16;
17244
17245 salt->salt_iter = ROUNDS_PDF14;
17246
17247 digest[0] = pdf->u_buf[0];
17248 digest[1] = pdf->u_buf[1];
17249 digest[2] = 0;
17250 digest[3] = 0;
17251
17252 return (PARSER_OK);
17253 }
17254
17255 int pdf17l3_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
17256 {
17257 int ret = pdf17l8_parse_hash (input_buf, input_len, hash_buf);
17258
17259 if (ret != PARSER_OK)
17260 {
17261 return ret;
17262 }
17263
17264 u32 *digest = (u32 *) hash_buf->digest;
17265
17266 salt_t *salt = hash_buf->salt;
17267
17268 digest[0] -= SHA256M_A;
17269 digest[1] -= SHA256M_B;
17270 digest[2] -= SHA256M_C;
17271 digest[3] -= SHA256M_D;
17272 digest[4] -= SHA256M_E;
17273 digest[5] -= SHA256M_F;
17274 digest[6] -= SHA256M_G;
17275 digest[7] -= SHA256M_H;
17276
17277 salt->salt_buf[2] = 0x80;
17278
17279 return (PARSER_OK);
17280 }
17281
17282 int pdf17l8_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
17283 {
17284 if ((input_len < DISPLAY_LEN_MIN_10600) || (input_len > DISPLAY_LEN_MAX_10600)) return (PARSER_GLOBAL_LENGTH);
17285
17286 if ((memcmp (SIGNATURE_PDF, input_buf, 5)) && (memcmp (SIGNATURE_PDF, input_buf, 5))) return (PARSER_SIGNATURE_UNMATCHED);
17287
17288 u32 *digest = (u32 *) hash_buf->digest;
17289
17290 salt_t *salt = hash_buf->salt;
17291
17292 pdf_t *pdf = (pdf_t *) hash_buf->esalt;
17293
17294 /**
17295 * parse line
17296 */
17297
17298 char *V_pos = input_buf + 5;
17299
17300 char *R_pos = strchr (V_pos, '*');
17301
17302 if (R_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17303
17304 u32 V_len = R_pos - V_pos;
17305
17306 R_pos++;
17307
17308 char *bits_pos = strchr (R_pos, '*');
17309
17310 if (bits_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17311
17312 u32 R_len = bits_pos - R_pos;
17313
17314 bits_pos++;
17315
17316 char *P_pos = strchr (bits_pos, '*');
17317
17318 if (P_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17319
17320 u32 bits_len = P_pos - bits_pos;
17321
17322 P_pos++;
17323
17324 char *enc_md_pos = strchr (P_pos, '*');
17325
17326 if (enc_md_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17327
17328 u32 P_len = enc_md_pos - P_pos;
17329
17330 enc_md_pos++;
17331
17332 char *id_len_pos = strchr (enc_md_pos, '*');
17333
17334 if (id_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17335
17336 u32 enc_md_len = id_len_pos - enc_md_pos;
17337
17338 id_len_pos++;
17339
17340 char *id_buf_pos = strchr (id_len_pos, '*');
17341
17342 if (id_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17343
17344 u32 id_len_len = id_buf_pos - id_len_pos;
17345
17346 id_buf_pos++;
17347
17348 char *u_len_pos = strchr (id_buf_pos, '*');
17349
17350 if (u_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17351
17352 u32 id_buf_len = u_len_pos - id_buf_pos;
17353
17354 u_len_pos++;
17355
17356 char *u_buf_pos = strchr (u_len_pos, '*');
17357
17358 if (u_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17359
17360 u32 u_len_len = u_buf_pos - u_len_pos;
17361
17362 u_buf_pos++;
17363
17364 char *o_len_pos = strchr (u_buf_pos, '*');
17365
17366 if (o_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17367
17368 u32 u_buf_len = o_len_pos - u_buf_pos;
17369
17370 o_len_pos++;
17371
17372 char *o_buf_pos = strchr (o_len_pos, '*');
17373
17374 if (o_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17375
17376 u32 o_len_len = o_buf_pos - o_len_pos;
17377
17378 o_buf_pos++;
17379
17380 char *last = strchr (o_buf_pos, '*');
17381
17382 if (last == NULL) last = input_buf + input_len;
17383
17384 u32 o_buf_len = last - o_buf_pos;
17385
17386 // validate data
17387
17388 const int V = atoi (V_pos);
17389 const int R = atoi (R_pos);
17390
17391 int vr_ok = 0;
17392
17393 if ((V == 5) && (R == 5)) vr_ok = 1;
17394 if ((V == 5) && (R == 6)) vr_ok = 1;
17395
17396 if (vr_ok == 0) return (PARSER_SALT_VALUE);
17397
17398 const int bits = atoi (bits_pos);
17399
17400 if (bits != 256) return (PARSER_SALT_VALUE);
17401
17402 int enc_md = atoi (enc_md_pos);
17403
17404 if (enc_md != 1) return (PARSER_SALT_VALUE);
17405
17406 const uint id_len = atoi (id_len_pos);
17407 const uint u_len = atoi (u_len_pos);
17408 const uint o_len = atoi (o_len_pos);
17409
17410 if (V_len > 6) return (PARSER_SALT_LENGTH);
17411 if (R_len > 6) return (PARSER_SALT_LENGTH);
17412 if (P_len > 6) return (PARSER_SALT_LENGTH);
17413 if (id_len_len > 6) return (PARSER_SALT_LENGTH);
17414 if (u_len_len > 6) return (PARSER_SALT_LENGTH);
17415 if (o_len_len > 6) return (PARSER_SALT_LENGTH);
17416 if (bits_len > 6) return (PARSER_SALT_LENGTH);
17417 if (enc_md_len > 6) return (PARSER_SALT_LENGTH);
17418
17419 if ((id_len * 2) != id_buf_len) return (PARSER_SALT_VALUE);
17420 if ((u_len * 2) != u_buf_len) return (PARSER_SALT_VALUE);
17421 if ((o_len * 2) != o_buf_len) return (PARSER_SALT_VALUE);
17422
17423 // copy data to esalt
17424
17425 if (u_len < 40) return (PARSER_SALT_VALUE);
17426
17427 for (int i = 0, j = 0; i < 8 + 2; i += 1, j += 8)
17428 {
17429 pdf->u_buf[i] = hex_to_u32 ((const u8 *) &u_buf_pos[j]);
17430 }
17431
17432 salt->salt_buf[0] = pdf->u_buf[8];
17433 salt->salt_buf[1] = pdf->u_buf[9];
17434
17435 salt->salt_buf[0] = byte_swap_32 (salt->salt_buf[0]);
17436 salt->salt_buf[1] = byte_swap_32 (salt->salt_buf[1]);
17437
17438 salt->salt_len = 8;
17439 salt->salt_iter = ROUNDS_PDF17L8;
17440
17441 digest[0] = pdf->u_buf[0];
17442 digest[1] = pdf->u_buf[1];
17443 digest[2] = pdf->u_buf[2];
17444 digest[3] = pdf->u_buf[3];
17445 digest[4] = pdf->u_buf[4];
17446 digest[5] = pdf->u_buf[5];
17447 digest[6] = pdf->u_buf[6];
17448 digest[7] = pdf->u_buf[7];
17449
17450 return (PARSER_OK);
17451 }
17452
17453 int pbkdf2_sha256_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
17454 {
17455 if ((input_len < DISPLAY_LEN_MIN_10900) || (input_len > DISPLAY_LEN_MAX_10900)) return (PARSER_GLOBAL_LENGTH);
17456
17457 if (memcmp (SIGNATURE_PBKDF2_SHA256, input_buf, 7)) return (PARSER_SIGNATURE_UNMATCHED);
17458
17459 u32 *digest = (u32 *) hash_buf->digest;
17460
17461 salt_t *salt = hash_buf->salt;
17462
17463 pbkdf2_sha256_t *pbkdf2_sha256 = (pbkdf2_sha256_t *) hash_buf->esalt;
17464
17465 /**
17466 * parse line
17467 */
17468
17469 // iterations
17470
17471 char *iter_pos = input_buf + 7;
17472
17473 u32 iter = atoi (iter_pos);
17474
17475 if (iter < 1) return (PARSER_SALT_ITERATION);
17476 if (iter > 999999) return (PARSER_SALT_ITERATION);
17477
17478 // first is *raw* salt
17479
17480 char *salt_pos = strchr (iter_pos, ':');
17481
17482 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17483
17484 salt_pos++;
17485
17486 char *hash_pos = strchr (salt_pos, ':');
17487
17488 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17489
17490 u32 salt_len = hash_pos - salt_pos;
17491
17492 if (salt_len > 64) return (PARSER_SALT_LENGTH);
17493
17494 hash_pos++;
17495
17496 u32 hash_b64_len = input_len - (hash_pos - input_buf);
17497
17498 if (hash_b64_len > 88) return (PARSER_HASH_LENGTH);
17499
17500 // decode salt
17501
17502 char *salt_buf_ptr = (char *) pbkdf2_sha256->salt_buf;
17503
17504 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
17505
17506 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
17507
17508 salt_buf_ptr[salt_len + 3] = 0x01;
17509 salt_buf_ptr[salt_len + 4] = 0x80;
17510
17511 salt->salt_len = salt_len;
17512 salt->salt_iter = iter - 1;
17513
17514 // decode hash
17515
17516 u8 tmp_buf[100] = { 0 };
17517
17518 int hash_len = base64_decode (base64_to_int, (const u8 *) hash_pos, hash_b64_len, tmp_buf);
17519
17520 if (hash_len < 16) return (PARSER_HASH_LENGTH);
17521
17522 memcpy (digest, tmp_buf, 16);
17523
17524 digest[0] = byte_swap_32 (digest[0]);
17525 digest[1] = byte_swap_32 (digest[1]);
17526 digest[2] = byte_swap_32 (digest[2]);
17527 digest[3] = byte_swap_32 (digest[3]);
17528
17529 // add some stuff to normal salt to make sorted happy
17530
17531 salt->salt_buf[0] = pbkdf2_sha256->salt_buf[0];
17532 salt->salt_buf[1] = pbkdf2_sha256->salt_buf[1];
17533 salt->salt_buf[2] = pbkdf2_sha256->salt_buf[2];
17534 salt->salt_buf[3] = pbkdf2_sha256->salt_buf[3];
17535 salt->salt_buf[4] = salt->salt_iter;
17536
17537 return (PARSER_OK);
17538 }
17539
17540 int prestashop_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
17541 {
17542 if ((input_len < DISPLAY_LEN_MIN_11000) || (input_len > DISPLAY_LEN_MAX_11000)) return (PARSER_GLOBAL_LENGTH);
17543
17544 u32 *digest = (u32 *) hash_buf->digest;
17545
17546 salt_t *salt = hash_buf->salt;
17547
17548 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
17549 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
17550 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
17551 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
17552
17553 digest[0] = byte_swap_32 (digest[0]);
17554 digest[1] = byte_swap_32 (digest[1]);
17555 digest[2] = byte_swap_32 (digest[2]);
17556 digest[3] = byte_swap_32 (digest[3]);
17557
17558 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
17559
17560 uint salt_len = input_len - 32 - 1;
17561
17562 char *salt_buf = input_buf + 32 + 1;
17563
17564 char *salt_buf_ptr = (char *) salt->salt_buf;
17565
17566 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
17567
17568 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
17569
17570 salt->salt_len = salt_len;
17571
17572 return (PARSER_OK);
17573 }
17574
17575 int postgresql_auth_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
17576 {
17577 if ((input_len < DISPLAY_LEN_MIN_11100) || (input_len > DISPLAY_LEN_MAX_11100)) return (PARSER_GLOBAL_LENGTH);
17578
17579 if (memcmp (SIGNATURE_POSTGRESQL_AUTH, input_buf, 10)) return (PARSER_SIGNATURE_UNMATCHED);
17580
17581 u32 *digest = (u32 *) hash_buf->digest;
17582
17583 salt_t *salt = hash_buf->salt;
17584
17585 char *user_pos = input_buf + 10;
17586
17587 char *salt_pos = strchr (user_pos, '*');
17588
17589 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17590
17591 salt_pos++;
17592
17593 char *hash_pos = strchr (salt_pos, '*');
17594
17595 hash_pos++;
17596
17597 uint hash_len = input_len - (hash_pos - input_buf);
17598
17599 if (hash_len != 32) return (PARSER_HASH_LENGTH);
17600
17601 uint user_len = salt_pos - user_pos - 1;
17602
17603 uint salt_len = hash_pos - salt_pos - 1;
17604
17605 if (salt_len != 8) return (PARSER_SALT_LENGTH);
17606
17607 /*
17608 * store digest
17609 */
17610
17611 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
17612 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
17613 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
17614 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
17615
17616 digest[0] = byte_swap_32 (digest[0]);
17617 digest[1] = byte_swap_32 (digest[1]);
17618 digest[2] = byte_swap_32 (digest[2]);
17619 digest[3] = byte_swap_32 (digest[3]);
17620
17621 digest[0] -= MD5M_A;
17622 digest[1] -= MD5M_B;
17623 digest[2] -= MD5M_C;
17624 digest[3] -= MD5M_D;
17625
17626 /*
17627 * store salt
17628 */
17629
17630 char *salt_buf_ptr = (char *) salt->salt_buf;
17631
17632 // first 4 bytes are the "challenge"
17633
17634 salt_buf_ptr[0] = hex_to_u8 ((const u8 *) &salt_pos[0]);
17635 salt_buf_ptr[1] = hex_to_u8 ((const u8 *) &salt_pos[2]);
17636 salt_buf_ptr[2] = hex_to_u8 ((const u8 *) &salt_pos[4]);
17637 salt_buf_ptr[3] = hex_to_u8 ((const u8 *) &salt_pos[6]);
17638
17639 // append the user name
17640
17641 user_len = parse_and_store_salt (salt_buf_ptr + 4, user_pos, user_len);
17642
17643 salt->salt_len = 4 + user_len;
17644
17645 return (PARSER_OK);
17646 }
17647
17648 int mysql_auth_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
17649 {
17650 if ((input_len < DISPLAY_LEN_MIN_11200) || (input_len > DISPLAY_LEN_MAX_11200)) return (PARSER_GLOBAL_LENGTH);
17651
17652 if (memcmp (SIGNATURE_MYSQL_AUTH, input_buf, 9)) return (PARSER_SIGNATURE_UNMATCHED);
17653
17654 u32 *digest = (u32 *) hash_buf->digest;
17655
17656 salt_t *salt = hash_buf->salt;
17657
17658 char *salt_pos = input_buf + 9;
17659
17660 char *hash_pos = strchr (salt_pos, '*');
17661
17662 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17663
17664 hash_pos++;
17665
17666 uint hash_len = input_len - (hash_pos - input_buf);
17667
17668 if (hash_len != 40) return (PARSER_HASH_LENGTH);
17669
17670 uint salt_len = hash_pos - salt_pos - 1;
17671
17672 if (salt_len != 40) return (PARSER_SALT_LENGTH);
17673
17674 /*
17675 * store digest
17676 */
17677
17678 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
17679 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
17680 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
17681 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
17682 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
17683
17684 /*
17685 * store salt
17686 */
17687
17688 char *salt_buf_ptr = (char *) salt->salt_buf;
17689
17690 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
17691
17692 salt->salt_len = salt_len;
17693
17694 return (PARSER_OK);
17695 }
17696
17697 int bitcoin_wallet_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
17698 {
17699 if ((input_len < DISPLAY_LEN_MIN_11300) || (input_len > DISPLAY_LEN_MAX_11300)) return (PARSER_GLOBAL_LENGTH);
17700
17701 if (memcmp (SIGNATURE_BITCOIN_WALLET, input_buf, 9)) return (PARSER_SIGNATURE_UNMATCHED);
17702
17703 u32 *digest = (u32 *) hash_buf->digest;
17704
17705 salt_t *salt = hash_buf->salt;
17706
17707 bitcoin_wallet_t *bitcoin_wallet = (bitcoin_wallet_t *) hash_buf->esalt;
17708
17709 /**
17710 * parse line
17711 */
17712
17713 char *cry_master_len_pos = input_buf + 9;
17714
17715 char *cry_master_buf_pos = strchr (cry_master_len_pos, '$');
17716
17717 if (cry_master_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17718
17719 u32 cry_master_len_len = cry_master_buf_pos - cry_master_len_pos;
17720
17721 cry_master_buf_pos++;
17722
17723 char *cry_salt_len_pos = strchr (cry_master_buf_pos, '$');
17724
17725 if (cry_salt_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17726
17727 u32 cry_master_buf_len = cry_salt_len_pos - cry_master_buf_pos;
17728
17729 cry_salt_len_pos++;
17730
17731 char *cry_salt_buf_pos = strchr (cry_salt_len_pos, '$');
17732
17733 if (cry_salt_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17734
17735 u32 cry_salt_len_len = cry_salt_buf_pos - cry_salt_len_pos;
17736
17737 cry_salt_buf_pos++;
17738
17739 char *cry_rounds_pos = strchr (cry_salt_buf_pos, '$');
17740
17741 if (cry_rounds_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17742
17743 u32 cry_salt_buf_len = cry_rounds_pos - cry_salt_buf_pos;
17744
17745 cry_rounds_pos++;
17746
17747 char *ckey_len_pos = strchr (cry_rounds_pos, '$');
17748
17749 if (ckey_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17750
17751 u32 cry_rounds_len = ckey_len_pos - cry_rounds_pos;
17752
17753 ckey_len_pos++;
17754
17755 char *ckey_buf_pos = strchr (ckey_len_pos, '$');
17756
17757 if (ckey_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17758
17759 u32 ckey_len_len = ckey_buf_pos - ckey_len_pos;
17760
17761 ckey_buf_pos++;
17762
17763 char *public_key_len_pos = strchr (ckey_buf_pos, '$');
17764
17765 if (public_key_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17766
17767 u32 ckey_buf_len = public_key_len_pos - ckey_buf_pos;
17768
17769 public_key_len_pos++;
17770
17771 char *public_key_buf_pos = strchr (public_key_len_pos, '$');
17772
17773 if (public_key_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17774
17775 u32 public_key_len_len = public_key_buf_pos - public_key_len_pos;
17776
17777 public_key_buf_pos++;
17778
17779 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;
17780
17781 const uint cry_master_len = atoi (cry_master_len_pos);
17782 const uint cry_salt_len = atoi (cry_salt_len_pos);
17783 const uint ckey_len = atoi (ckey_len_pos);
17784 const uint public_key_len = atoi (public_key_len_pos);
17785
17786 if (cry_master_buf_len != cry_master_len) return (PARSER_SALT_VALUE);
17787 if (cry_salt_buf_len != cry_salt_len) return (PARSER_SALT_VALUE);
17788 if (ckey_buf_len != ckey_len) return (PARSER_SALT_VALUE);
17789 if (public_key_buf_len != public_key_len) return (PARSER_SALT_VALUE);
17790
17791 for (uint i = 0, j = 0; j < cry_master_len; i += 1, j += 8)
17792 {
17793 bitcoin_wallet->cry_master_buf[i] = hex_to_u32 ((const u8 *) &cry_master_buf_pos[j]);
17794
17795 bitcoin_wallet->cry_master_buf[i] = byte_swap_32 (bitcoin_wallet->cry_master_buf[i]);
17796 }
17797
17798 for (uint i = 0, j = 0; j < ckey_len; i += 1, j += 8)
17799 {
17800 bitcoin_wallet->ckey_buf[i] = hex_to_u32 ((const u8 *) &ckey_buf_pos[j]);
17801
17802 bitcoin_wallet->ckey_buf[i] = byte_swap_32 (bitcoin_wallet->ckey_buf[i]);
17803 }
17804
17805 for (uint i = 0, j = 0; j < public_key_len; i += 1, j += 8)
17806 {
17807 bitcoin_wallet->public_key_buf[i] = hex_to_u32 ((const u8 *) &public_key_buf_pos[j]);
17808
17809 bitcoin_wallet->public_key_buf[i] = byte_swap_32 (bitcoin_wallet->public_key_buf[i]);
17810 }
17811
17812 bitcoin_wallet->cry_master_len = cry_master_len / 2;
17813 bitcoin_wallet->ckey_len = ckey_len / 2;
17814 bitcoin_wallet->public_key_len = public_key_len / 2;
17815
17816 /*
17817 * store digest (should be unique enought, hopefully)
17818 */
17819
17820 digest[0] = bitcoin_wallet->cry_master_buf[0];
17821 digest[1] = bitcoin_wallet->cry_master_buf[1];
17822 digest[2] = bitcoin_wallet->cry_master_buf[2];
17823 digest[3] = bitcoin_wallet->cry_master_buf[3];
17824
17825 /*
17826 * store salt
17827 */
17828
17829 if (cry_rounds_len >= 7) return (PARSER_SALT_VALUE);
17830
17831 const uint cry_rounds = atoi (cry_rounds_pos);
17832
17833 salt->salt_iter = cry_rounds - 1;
17834
17835 char *salt_buf_ptr = (char *) salt->salt_buf;
17836
17837 const uint salt_len = parse_and_store_salt (salt_buf_ptr, cry_salt_buf_pos, cry_salt_buf_len);
17838
17839 salt->salt_len = salt_len;
17840
17841 return (PARSER_OK);
17842 }
17843
17844 int sip_auth_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
17845 {
17846 if ((input_len < DISPLAY_LEN_MIN_11400) || (input_len > DISPLAY_LEN_MAX_11400)) return (PARSER_GLOBAL_LENGTH);
17847
17848 if (memcmp (SIGNATURE_SIP_AUTH, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
17849
17850 u32 *digest = (u32 *) hash_buf->digest;
17851
17852 salt_t *salt = hash_buf->salt;
17853
17854 sip_t *sip = (sip_t *) hash_buf->esalt;
17855
17856 // work with a temporary copy of input_buf (s.t. we can manipulate it directly)
17857
17858 char *temp_input_buf = (char *) mymalloc (input_len + 1);
17859
17860 memcpy (temp_input_buf, input_buf, input_len);
17861
17862 // URI_server:
17863
17864 char *URI_server_pos = temp_input_buf + 6;
17865
17866 char *URI_client_pos = strchr (URI_server_pos, '*');
17867
17868 if (URI_client_pos == NULL)
17869 {
17870 myfree (temp_input_buf);
17871
17872 return (PARSER_SEPARATOR_UNMATCHED);
17873 }
17874
17875 URI_client_pos[0] = 0;
17876 URI_client_pos++;
17877
17878 uint URI_server_len = strlen (URI_server_pos);
17879
17880 if (URI_server_len > 512)
17881 {
17882 myfree (temp_input_buf);
17883
17884 return (PARSER_SALT_LENGTH);
17885 }
17886
17887 // URI_client:
17888
17889 char *user_pos = strchr (URI_client_pos, '*');
17890
17891 if (user_pos == NULL)
17892 {
17893 myfree (temp_input_buf);
17894
17895 return (PARSER_SEPARATOR_UNMATCHED);
17896 }
17897
17898 user_pos[0] = 0;
17899 user_pos++;
17900
17901 uint URI_client_len = strlen (URI_client_pos);
17902
17903 if (URI_client_len > 512)
17904 {
17905 myfree (temp_input_buf);
17906
17907 return (PARSER_SALT_LENGTH);
17908 }
17909
17910 // user:
17911
17912 char *realm_pos = strchr (user_pos, '*');
17913
17914 if (realm_pos == NULL)
17915 {
17916 myfree (temp_input_buf);
17917
17918 return (PARSER_SEPARATOR_UNMATCHED);
17919 }
17920
17921 realm_pos[0] = 0;
17922 realm_pos++;
17923
17924 uint user_len = strlen (user_pos);
17925
17926 if (user_len > 116)
17927 {
17928 myfree (temp_input_buf);
17929
17930 return (PARSER_SALT_LENGTH);
17931 }
17932
17933 // realm:
17934
17935 char *method_pos = strchr (realm_pos, '*');
17936
17937 if (method_pos == NULL)
17938 {
17939 myfree (temp_input_buf);
17940
17941 return (PARSER_SEPARATOR_UNMATCHED);
17942 }
17943
17944 method_pos[0] = 0;
17945 method_pos++;
17946
17947 uint realm_len = strlen (realm_pos);
17948
17949 if (realm_len > 116)
17950 {
17951 myfree (temp_input_buf);
17952
17953 return (PARSER_SALT_LENGTH);
17954 }
17955
17956 // method:
17957
17958 char *URI_prefix_pos = strchr (method_pos, '*');
17959
17960 if (URI_prefix_pos == NULL)
17961 {
17962 myfree (temp_input_buf);
17963
17964 return (PARSER_SEPARATOR_UNMATCHED);
17965 }
17966
17967 URI_prefix_pos[0] = 0;
17968 URI_prefix_pos++;
17969
17970 uint method_len = strlen (method_pos);
17971
17972 if (method_len > 246)
17973 {
17974 myfree (temp_input_buf);
17975
17976 return (PARSER_SALT_LENGTH);
17977 }
17978
17979 // URI_prefix:
17980
17981 char *URI_resource_pos = strchr (URI_prefix_pos, '*');
17982
17983 if (URI_resource_pos == NULL)
17984 {
17985 myfree (temp_input_buf);
17986
17987 return (PARSER_SEPARATOR_UNMATCHED);
17988 }
17989
17990 URI_resource_pos[0] = 0;
17991 URI_resource_pos++;
17992
17993 uint URI_prefix_len = strlen (URI_prefix_pos);
17994
17995 if (URI_prefix_len > 245)
17996 {
17997 myfree (temp_input_buf);
17998
17999 return (PARSER_SALT_LENGTH);
18000 }
18001
18002 // URI_resource:
18003
18004 char *URI_suffix_pos = strchr (URI_resource_pos, '*');
18005
18006 if (URI_suffix_pos == NULL)
18007 {
18008 myfree (temp_input_buf);
18009
18010 return (PARSER_SEPARATOR_UNMATCHED);
18011 }
18012
18013 URI_suffix_pos[0] = 0;
18014 URI_suffix_pos++;
18015
18016 uint URI_resource_len = strlen (URI_resource_pos);
18017
18018 if (URI_resource_len < 1 || URI_resource_len > 246)
18019 {
18020 myfree (temp_input_buf);
18021
18022 return (PARSER_SALT_LENGTH);
18023 }
18024
18025 // URI_suffix:
18026
18027 char *nonce_pos = strchr (URI_suffix_pos, '*');
18028
18029 if (nonce_pos == NULL)
18030 {
18031 myfree (temp_input_buf);
18032
18033 return (PARSER_SEPARATOR_UNMATCHED);
18034 }
18035
18036 nonce_pos[0] = 0;
18037 nonce_pos++;
18038
18039 uint URI_suffix_len = strlen (URI_suffix_pos);
18040
18041 if (URI_suffix_len > 245)
18042 {
18043 myfree (temp_input_buf);
18044
18045 return (PARSER_SALT_LENGTH);
18046 }
18047
18048 // nonce:
18049
18050 char *nonce_client_pos = strchr (nonce_pos, '*');
18051
18052 if (nonce_client_pos == NULL)
18053 {
18054 myfree (temp_input_buf);
18055
18056 return (PARSER_SEPARATOR_UNMATCHED);
18057 }
18058
18059 nonce_client_pos[0] = 0;
18060 nonce_client_pos++;
18061
18062 uint nonce_len = strlen (nonce_pos);
18063
18064 if (nonce_len < 1 || nonce_len > 50)
18065 {
18066 myfree (temp_input_buf);
18067
18068 return (PARSER_SALT_LENGTH);
18069 }
18070
18071 // nonce_client:
18072
18073 char *nonce_count_pos = strchr (nonce_client_pos, '*');
18074
18075 if (nonce_count_pos == NULL)
18076 {
18077 myfree (temp_input_buf);
18078
18079 return (PARSER_SEPARATOR_UNMATCHED);
18080 }
18081
18082 nonce_count_pos[0] = 0;
18083 nonce_count_pos++;
18084
18085 uint nonce_client_len = strlen (nonce_client_pos);
18086
18087 if (nonce_client_len > 50)
18088 {
18089 myfree (temp_input_buf);
18090
18091 return (PARSER_SALT_LENGTH);
18092 }
18093
18094 // nonce_count:
18095
18096 char *qop_pos = strchr (nonce_count_pos, '*');
18097
18098 if (qop_pos == NULL)
18099 {
18100 myfree (temp_input_buf);
18101
18102 return (PARSER_SEPARATOR_UNMATCHED);
18103 }
18104
18105 qop_pos[0] = 0;
18106 qop_pos++;
18107
18108 uint nonce_count_len = strlen (nonce_count_pos);
18109
18110 if (nonce_count_len > 50)
18111 {
18112 myfree (temp_input_buf);
18113
18114 return (PARSER_SALT_LENGTH);
18115 }
18116
18117 // qop:
18118
18119 char *directive_pos = strchr (qop_pos, '*');
18120
18121 if (directive_pos == NULL)
18122 {
18123 myfree (temp_input_buf);
18124
18125 return (PARSER_SEPARATOR_UNMATCHED);
18126 }
18127
18128 directive_pos[0] = 0;
18129 directive_pos++;
18130
18131 uint qop_len = strlen (qop_pos);
18132
18133 if (qop_len > 50)
18134 {
18135 myfree (temp_input_buf);
18136
18137 return (PARSER_SALT_LENGTH);
18138 }
18139
18140 // directive
18141
18142 char *digest_pos = strchr (directive_pos, '*');
18143
18144 if (digest_pos == NULL)
18145 {
18146 myfree (temp_input_buf);
18147
18148 return (PARSER_SEPARATOR_UNMATCHED);
18149 }
18150
18151 digest_pos[0] = 0;
18152 digest_pos++;
18153
18154 uint directive_len = strlen (directive_pos);
18155
18156 if (directive_len != 3)
18157 {
18158 myfree (temp_input_buf);
18159
18160 return (PARSER_SALT_LENGTH);
18161 }
18162
18163 if (memcmp (directive_pos, "MD5", 3))
18164 {
18165 log_info ("ERROR: only the MD5 directive is currently supported\n");
18166
18167 myfree (temp_input_buf);
18168
18169 return (PARSER_SIP_AUTH_DIRECTIVE);
18170 }
18171
18172 /*
18173 * first (pre-)compute: HA2 = md5 ($method . ":" . $uri)
18174 */
18175
18176 uint md5_len = 0;
18177
18178 uint md5_max_len = 4 * 64;
18179
18180 uint md5_remaining_len = md5_max_len;
18181
18182 uint tmp_md5_buf[64] = { 0 };
18183
18184 char *tmp_md5_ptr = (char *) tmp_md5_buf;
18185
18186 snprintf (tmp_md5_ptr, md5_remaining_len, "%s:", method_pos);
18187
18188 md5_len += method_len + 1;
18189 tmp_md5_ptr += method_len + 1;
18190
18191 if (URI_prefix_len > 0)
18192 {
18193 md5_remaining_len = md5_max_len - md5_len;
18194
18195 snprintf (tmp_md5_ptr, md5_remaining_len + 1, "%s:", URI_prefix_pos);
18196
18197 md5_len += URI_prefix_len + 1;
18198 tmp_md5_ptr += URI_prefix_len + 1;
18199 }
18200
18201 md5_remaining_len = md5_max_len - md5_len;
18202
18203 snprintf (tmp_md5_ptr, md5_remaining_len + 1, "%s", URI_resource_pos);
18204
18205 md5_len += URI_resource_len;
18206 tmp_md5_ptr += URI_resource_len;
18207
18208 if (URI_suffix_len > 0)
18209 {
18210 md5_remaining_len = md5_max_len - md5_len;
18211
18212 snprintf (tmp_md5_ptr, md5_remaining_len + 1, ":%s", URI_suffix_pos);
18213
18214 md5_len += 1 + URI_suffix_len;
18215 }
18216
18217 uint tmp_digest[4] = { 0 };
18218
18219 md5_complete_no_limit (tmp_digest, tmp_md5_buf, md5_len);
18220
18221 tmp_digest[0] = byte_swap_32 (tmp_digest[0]);
18222 tmp_digest[1] = byte_swap_32 (tmp_digest[1]);
18223 tmp_digest[2] = byte_swap_32 (tmp_digest[2]);
18224 tmp_digest[3] = byte_swap_32 (tmp_digest[3]);
18225
18226 /*
18227 * esalt
18228 */
18229
18230 char *esalt_buf_ptr = (char *) sip->esalt_buf;
18231
18232 uint esalt_len = 0;
18233
18234 uint max_esalt_len = sizeof (sip->esalt_buf); // 151 = (64 + 64 + 55) - 32, where 32 is the hexadecimal MD5 HA1 hash
18235
18236 // there are 2 possibilities for the esalt:
18237
18238 if ((strcmp (qop_pos, "auth") == 0) || (strcmp (qop_pos, "auth-int") == 0))
18239 {
18240 esalt_len = 1 + nonce_len + 1 + nonce_count_len + 1 + nonce_client_len + 1 + qop_len + 1 + 32;
18241
18242 if (esalt_len > max_esalt_len)
18243 {
18244 myfree (temp_input_buf);
18245
18246 return (PARSER_SALT_LENGTH);
18247 }
18248
18249 snprintf (esalt_buf_ptr, max_esalt_len, ":%s:%s:%s:%s:%08x%08x%08x%08x",
18250 nonce_pos,
18251 nonce_count_pos,
18252 nonce_client_pos,
18253 qop_pos,
18254 tmp_digest[0],
18255 tmp_digest[1],
18256 tmp_digest[2],
18257 tmp_digest[3]);
18258 }
18259 else
18260 {
18261 esalt_len = 1 + nonce_len + 1 + 32;
18262
18263 if (esalt_len > max_esalt_len)
18264 {
18265 myfree (temp_input_buf);
18266
18267 return (PARSER_SALT_LENGTH);
18268 }
18269
18270 snprintf (esalt_buf_ptr, max_esalt_len, ":%s:%08x%08x%08x%08x",
18271 nonce_pos,
18272 tmp_digest[0],
18273 tmp_digest[1],
18274 tmp_digest[2],
18275 tmp_digest[3]);
18276 }
18277
18278 // add 0x80 to esalt
18279
18280 esalt_buf_ptr[esalt_len] = 0x80;
18281
18282 sip->esalt_len = esalt_len;
18283
18284 /*
18285 * actual salt
18286 */
18287
18288 char *sip_salt_ptr = (char *) sip->salt_buf;
18289
18290 uint salt_len = user_len + 1 + realm_len + 1;
18291
18292 uint max_salt_len = 119;
18293
18294 if (salt_len > max_salt_len)
18295 {
18296 myfree (temp_input_buf);
18297
18298 return (PARSER_SALT_LENGTH);
18299 }
18300
18301 snprintf (sip_salt_ptr, max_salt_len + 1, "%s:%s:", user_pos, realm_pos);
18302
18303 sip->salt_len = salt_len;
18304
18305 /*
18306 * fake salt (for sorting)
18307 */
18308
18309 char *salt_buf_ptr = (char *) salt->salt_buf;
18310
18311 max_salt_len = 55;
18312
18313 uint fake_salt_len = salt_len;
18314
18315 if (fake_salt_len > max_salt_len)
18316 {
18317 fake_salt_len = max_salt_len;
18318 }
18319
18320 snprintf (salt_buf_ptr, max_salt_len + 1, "%s:%s:", user_pos, realm_pos);
18321
18322 salt->salt_len = fake_salt_len;
18323
18324 /*
18325 * digest
18326 */
18327
18328 digest[0] = hex_to_u32 ((const u8 *) &digest_pos[ 0]);
18329 digest[1] = hex_to_u32 ((const u8 *) &digest_pos[ 8]);
18330 digest[2] = hex_to_u32 ((const u8 *) &digest_pos[16]);
18331 digest[3] = hex_to_u32 ((const u8 *) &digest_pos[24]);
18332
18333 digest[0] = byte_swap_32 (digest[0]);
18334 digest[1] = byte_swap_32 (digest[1]);
18335 digest[2] = byte_swap_32 (digest[2]);
18336 digest[3] = byte_swap_32 (digest[3]);
18337
18338 myfree (temp_input_buf);
18339
18340 return (PARSER_OK);
18341 }
18342
18343 int crc32_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18344 {
18345 if ((input_len < DISPLAY_LEN_MIN_11500) || (input_len > DISPLAY_LEN_MAX_11500)) return (PARSER_GLOBAL_LENGTH);
18346
18347 if (input_buf[8] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
18348
18349 u32 *digest = (u32 *) hash_buf->digest;
18350
18351 salt_t *salt = hash_buf->salt;
18352
18353 // digest
18354
18355 char *digest_pos = input_buf;
18356
18357 digest[0] = hex_to_u32 ((const u8 *) &digest_pos[0]);
18358 digest[1] = 0;
18359 digest[2] = 0;
18360 digest[3] = 0;
18361
18362 // salt
18363
18364 char *salt_buf = input_buf + 8 + 1;
18365
18366 uint salt_len = 8;
18367
18368 char *salt_buf_ptr = (char *) salt->salt_buf;
18369
18370 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
18371
18372 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
18373
18374 salt->salt_len = salt_len;
18375
18376 return (PARSER_OK);
18377 }
18378
18379 int seven_zip_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18380 {
18381 if ((input_len < DISPLAY_LEN_MIN_11600) || (input_len > DISPLAY_LEN_MAX_11600)) return (PARSER_GLOBAL_LENGTH);
18382
18383 if (memcmp (SIGNATURE_SEVEN_ZIP, input_buf, 4)) return (PARSER_SIGNATURE_UNMATCHED);
18384
18385 u32 *digest = (u32 *) hash_buf->digest;
18386
18387 salt_t *salt = hash_buf->salt;
18388
18389 seven_zip_t *seven_zip = (seven_zip_t *) hash_buf->esalt;
18390
18391 /**
18392 * parse line
18393 */
18394
18395 char *p_buf_pos = input_buf + 4;
18396
18397 char *NumCyclesPower_pos = strchr (p_buf_pos, '$');
18398
18399 if (NumCyclesPower_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18400
18401 u32 p_buf_len = NumCyclesPower_pos - p_buf_pos;
18402
18403 NumCyclesPower_pos++;
18404
18405 char *salt_len_pos = strchr (NumCyclesPower_pos, '$');
18406
18407 if (salt_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18408
18409 u32 NumCyclesPower_len = salt_len_pos - NumCyclesPower_pos;
18410
18411 salt_len_pos++;
18412
18413 char *salt_buf_pos = strchr (salt_len_pos, '$');
18414
18415 if (salt_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18416
18417 u32 salt_len_len = salt_buf_pos - salt_len_pos;
18418
18419 salt_buf_pos++;
18420
18421 char *iv_len_pos = strchr (salt_buf_pos, '$');
18422
18423 if (iv_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18424
18425 u32 salt_buf_len = iv_len_pos - salt_buf_pos;
18426
18427 iv_len_pos++;
18428
18429 char *iv_buf_pos = strchr (iv_len_pos, '$');
18430
18431 if (iv_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18432
18433 u32 iv_len_len = iv_buf_pos - iv_len_pos;
18434
18435 iv_buf_pos++;
18436
18437 char *crc_buf_pos = strchr (iv_buf_pos, '$');
18438
18439 if (crc_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18440
18441 u32 iv_buf_len = crc_buf_pos - iv_buf_pos;
18442
18443 crc_buf_pos++;
18444
18445 char *data_len_pos = strchr (crc_buf_pos, '$');
18446
18447 if (data_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18448
18449 u32 crc_buf_len = data_len_pos - crc_buf_pos;
18450
18451 data_len_pos++;
18452
18453 char *unpack_size_pos = strchr (data_len_pos, '$');
18454
18455 if (unpack_size_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18456
18457 u32 data_len_len = unpack_size_pos - data_len_pos;
18458
18459 unpack_size_pos++;
18460
18461 char *data_buf_pos = strchr (unpack_size_pos, '$');
18462
18463 if (data_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18464
18465 u32 unpack_size_len = data_buf_pos - unpack_size_pos;
18466
18467 data_buf_pos++;
18468
18469 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;
18470
18471 const uint iter = atoi (NumCyclesPower_pos);
18472 const uint crc = atoi (crc_buf_pos);
18473 const uint p_buf = atoi (p_buf_pos);
18474 const uint salt_len = atoi (salt_len_pos);
18475 const uint iv_len = atoi (iv_len_pos);
18476 const uint unpack_size = atoi (unpack_size_pos);
18477 const uint data_len = atoi (data_len_pos);
18478
18479 /**
18480 * verify some data
18481 */
18482
18483 if (p_buf != 0) return (PARSER_SALT_VALUE);
18484 if (salt_len != 0) return (PARSER_SALT_VALUE);
18485
18486 if ((data_len * 2) != data_buf_len) return (PARSER_SALT_VALUE);
18487
18488 if (data_len > 384) return (PARSER_SALT_VALUE);
18489
18490 if (unpack_size > data_len) return (PARSER_SALT_VALUE);
18491
18492 /**
18493 * store data
18494 */
18495
18496 seven_zip->iv_buf[0] = hex_to_u32 ((const u8 *) &iv_buf_pos[ 0]);
18497 seven_zip->iv_buf[1] = hex_to_u32 ((const u8 *) &iv_buf_pos[ 8]);
18498 seven_zip->iv_buf[2] = hex_to_u32 ((const u8 *) &iv_buf_pos[16]);
18499 seven_zip->iv_buf[3] = hex_to_u32 ((const u8 *) &iv_buf_pos[24]);
18500
18501 seven_zip->iv_len = iv_len;
18502
18503 memcpy (seven_zip->salt_buf, salt_buf_pos, salt_buf_len); // we just need that for later ascii_digest()
18504
18505 seven_zip->salt_len = 0;
18506
18507 seven_zip->crc = crc;
18508
18509 for (uint i = 0, j = 0; j < data_buf_len; i += 1, j += 8)
18510 {
18511 seven_zip->data_buf[i] = hex_to_u32 ((const u8 *) &data_buf_pos[j]);
18512
18513 seven_zip->data_buf[i] = byte_swap_32 (seven_zip->data_buf[i]);
18514 }
18515
18516 seven_zip->data_len = data_len;
18517
18518 seven_zip->unpack_size = unpack_size;
18519
18520 // real salt
18521
18522 salt->salt_buf[0] = seven_zip->data_buf[0];
18523 salt->salt_buf[1] = seven_zip->data_buf[1];
18524 salt->salt_buf[2] = seven_zip->data_buf[2];
18525 salt->salt_buf[3] = seven_zip->data_buf[3];
18526
18527 salt->salt_len = 16;
18528
18529 salt->salt_sign[0] = iter;
18530
18531 salt->salt_iter = 1 << iter;
18532
18533 /**
18534 * digest
18535 */
18536
18537 digest[0] = crc;
18538 digest[1] = 0;
18539 digest[2] = 0;
18540 digest[3] = 0;
18541
18542 return (PARSER_OK);
18543 }
18544
18545 int gost2012sbog_256_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18546 {
18547 if ((input_len < DISPLAY_LEN_MIN_11700) || (input_len > DISPLAY_LEN_MAX_11700)) return (PARSER_GLOBAL_LENGTH);
18548
18549 u32 *digest = (u32 *) hash_buf->digest;
18550
18551 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
18552 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
18553 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
18554 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
18555 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
18556 digest[5] = hex_to_u32 ((const u8 *) &input_buf[40]);
18557 digest[6] = hex_to_u32 ((const u8 *) &input_buf[48]);
18558 digest[7] = hex_to_u32 ((const u8 *) &input_buf[56]);
18559
18560 digest[0] = byte_swap_32 (digest[0]);
18561 digest[1] = byte_swap_32 (digest[1]);
18562 digest[2] = byte_swap_32 (digest[2]);
18563 digest[3] = byte_swap_32 (digest[3]);
18564 digest[4] = byte_swap_32 (digest[4]);
18565 digest[5] = byte_swap_32 (digest[5]);
18566 digest[6] = byte_swap_32 (digest[6]);
18567 digest[7] = byte_swap_32 (digest[7]);
18568
18569 return (PARSER_OK);
18570 }
18571
18572 int gost2012sbog_512_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18573 {
18574 if ((input_len < DISPLAY_LEN_MIN_11800) || (input_len > DISPLAY_LEN_MAX_11800)) return (PARSER_GLOBAL_LENGTH);
18575
18576 u32 *digest = (u32 *) hash_buf->digest;
18577
18578 digest[ 0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
18579 digest[ 1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
18580 digest[ 2] = hex_to_u32 ((const u8 *) &input_buf[ 16]);
18581 digest[ 3] = hex_to_u32 ((const u8 *) &input_buf[ 24]);
18582 digest[ 4] = hex_to_u32 ((const u8 *) &input_buf[ 32]);
18583 digest[ 5] = hex_to_u32 ((const u8 *) &input_buf[ 40]);
18584 digest[ 6] = hex_to_u32 ((const u8 *) &input_buf[ 48]);
18585 digest[ 7] = hex_to_u32 ((const u8 *) &input_buf[ 56]);
18586 digest[ 8] = hex_to_u32 ((const u8 *) &input_buf[ 64]);
18587 digest[ 9] = hex_to_u32 ((const u8 *) &input_buf[ 72]);
18588 digest[10] = hex_to_u32 ((const u8 *) &input_buf[ 80]);
18589 digest[11] = hex_to_u32 ((const u8 *) &input_buf[ 88]);
18590 digest[12] = hex_to_u32 ((const u8 *) &input_buf[ 96]);
18591 digest[13] = hex_to_u32 ((const u8 *) &input_buf[104]);
18592 digest[14] = hex_to_u32 ((const u8 *) &input_buf[112]);
18593 digest[15] = hex_to_u32 ((const u8 *) &input_buf[120]);
18594
18595 digest[ 0] = byte_swap_32 (digest[ 0]);
18596 digest[ 1] = byte_swap_32 (digest[ 1]);
18597 digest[ 2] = byte_swap_32 (digest[ 2]);
18598 digest[ 3] = byte_swap_32 (digest[ 3]);
18599 digest[ 4] = byte_swap_32 (digest[ 4]);
18600 digest[ 5] = byte_swap_32 (digest[ 5]);
18601 digest[ 6] = byte_swap_32 (digest[ 6]);
18602 digest[ 7] = byte_swap_32 (digest[ 7]);
18603 digest[ 8] = byte_swap_32 (digest[ 8]);
18604 digest[ 9] = byte_swap_32 (digest[ 9]);
18605 digest[10] = byte_swap_32 (digest[10]);
18606 digest[11] = byte_swap_32 (digest[11]);
18607 digest[12] = byte_swap_32 (digest[12]);
18608 digest[13] = byte_swap_32 (digest[13]);
18609 digest[14] = byte_swap_32 (digest[14]);
18610 digest[15] = byte_swap_32 (digest[15]);
18611
18612 return (PARSER_OK);
18613 }
18614
18615 int pbkdf2_md5_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18616 {
18617 if ((input_len < DISPLAY_LEN_MIN_11900) || (input_len > DISPLAY_LEN_MAX_11900)) return (PARSER_GLOBAL_LENGTH);
18618
18619 if (memcmp (SIGNATURE_PBKDF2_MD5, input_buf, 4)) return (PARSER_SIGNATURE_UNMATCHED);
18620
18621 u32 *digest = (u32 *) hash_buf->digest;
18622
18623 salt_t *salt = hash_buf->salt;
18624
18625 pbkdf2_md5_t *pbkdf2_md5 = (pbkdf2_md5_t *) hash_buf->esalt;
18626
18627 /**
18628 * parse line
18629 */
18630
18631 // iterations
18632
18633 char *iter_pos = input_buf + 4;
18634
18635 u32 iter = atoi (iter_pos);
18636
18637 if (iter < 1) return (PARSER_SALT_ITERATION);
18638 if (iter > 999999) return (PARSER_SALT_ITERATION);
18639
18640 // first is *raw* salt
18641
18642 char *salt_pos = strchr (iter_pos, ':');
18643
18644 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18645
18646 salt_pos++;
18647
18648 char *hash_pos = strchr (salt_pos, ':');
18649
18650 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18651
18652 u32 salt_len = hash_pos - salt_pos;
18653
18654 if (salt_len > 64) return (PARSER_SALT_LENGTH);
18655
18656 hash_pos++;
18657
18658 u32 hash_b64_len = input_len - (hash_pos - input_buf);
18659
18660 if (hash_b64_len > 88) return (PARSER_HASH_LENGTH);
18661
18662 // decode salt
18663
18664 char *salt_buf_ptr = (char *) pbkdf2_md5->salt_buf;
18665
18666 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
18667
18668 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
18669
18670 salt_buf_ptr[salt_len + 3] = 0x01;
18671 salt_buf_ptr[salt_len + 4] = 0x80;
18672
18673 salt->salt_len = salt_len;
18674 salt->salt_iter = iter - 1;
18675
18676 // decode hash
18677
18678 u8 tmp_buf[100] = { 0 };
18679
18680 int hash_len = base64_decode (base64_to_int, (const u8 *) hash_pos, hash_b64_len, tmp_buf);
18681
18682 if (hash_len < 16) return (PARSER_HASH_LENGTH);
18683
18684 memcpy (digest, tmp_buf, 16);
18685
18686 // add some stuff to normal salt to make sorted happy
18687
18688 salt->salt_buf[0] = pbkdf2_md5->salt_buf[0];
18689 salt->salt_buf[1] = pbkdf2_md5->salt_buf[1];
18690 salt->salt_buf[2] = pbkdf2_md5->salt_buf[2];
18691 salt->salt_buf[3] = pbkdf2_md5->salt_buf[3];
18692 salt->salt_buf[4] = salt->salt_iter;
18693
18694 return (PARSER_OK);
18695 }
18696
18697 int pbkdf2_sha1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18698 {
18699 if ((input_len < DISPLAY_LEN_MIN_12000) || (input_len > DISPLAY_LEN_MAX_12000)) return (PARSER_GLOBAL_LENGTH);
18700
18701 if (memcmp (SIGNATURE_PBKDF2_SHA1, input_buf, 5)) return (PARSER_SIGNATURE_UNMATCHED);
18702
18703 u32 *digest = (u32 *) hash_buf->digest;
18704
18705 salt_t *salt = hash_buf->salt;
18706
18707 pbkdf2_sha1_t *pbkdf2_sha1 = (pbkdf2_sha1_t *) hash_buf->esalt;
18708
18709 /**
18710 * parse line
18711 */
18712
18713 // iterations
18714
18715 char *iter_pos = input_buf + 5;
18716
18717 u32 iter = atoi (iter_pos);
18718
18719 if (iter < 1) return (PARSER_SALT_ITERATION);
18720 if (iter > 999999) return (PARSER_SALT_ITERATION);
18721
18722 // first is *raw* salt
18723
18724 char *salt_pos = strchr (iter_pos, ':');
18725
18726 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18727
18728 salt_pos++;
18729
18730 char *hash_pos = strchr (salt_pos, ':');
18731
18732 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18733
18734 u32 salt_len = hash_pos - salt_pos;
18735
18736 if (salt_len > 64) return (PARSER_SALT_LENGTH);
18737
18738 hash_pos++;
18739
18740 u32 hash_b64_len = input_len - (hash_pos - input_buf);
18741
18742 if (hash_b64_len > 88) return (PARSER_HASH_LENGTH);
18743
18744 // decode salt
18745
18746 char *salt_buf_ptr = (char *) pbkdf2_sha1->salt_buf;
18747
18748 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
18749
18750 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
18751
18752 salt_buf_ptr[salt_len + 3] = 0x01;
18753 salt_buf_ptr[salt_len + 4] = 0x80;
18754
18755 salt->salt_len = salt_len;
18756 salt->salt_iter = iter - 1;
18757
18758 // decode hash
18759
18760 u8 tmp_buf[100] = { 0 };
18761
18762 int hash_len = base64_decode (base64_to_int, (const u8 *) hash_pos, hash_b64_len, tmp_buf);
18763
18764 if (hash_len < 16) return (PARSER_HASH_LENGTH);
18765
18766 memcpy (digest, tmp_buf, 16);
18767
18768 digest[0] = byte_swap_32 (digest[0]);
18769 digest[1] = byte_swap_32 (digest[1]);
18770 digest[2] = byte_swap_32 (digest[2]);
18771 digest[3] = byte_swap_32 (digest[3]);
18772
18773 // add some stuff to normal salt to make sorted happy
18774
18775 salt->salt_buf[0] = pbkdf2_sha1->salt_buf[0];
18776 salt->salt_buf[1] = pbkdf2_sha1->salt_buf[1];
18777 salt->salt_buf[2] = pbkdf2_sha1->salt_buf[2];
18778 salt->salt_buf[3] = pbkdf2_sha1->salt_buf[3];
18779 salt->salt_buf[4] = salt->salt_iter;
18780
18781 return (PARSER_OK);
18782 }
18783
18784 int pbkdf2_sha512_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18785 {
18786 if ((input_len < DISPLAY_LEN_MIN_12100) || (input_len > DISPLAY_LEN_MAX_12100)) return (PARSER_GLOBAL_LENGTH);
18787
18788 if (memcmp (SIGNATURE_PBKDF2_SHA512, input_buf, 7)) return (PARSER_SIGNATURE_UNMATCHED);
18789
18790 u64 *digest = (u64 *) hash_buf->digest;
18791
18792 salt_t *salt = hash_buf->salt;
18793
18794 pbkdf2_sha512_t *pbkdf2_sha512 = (pbkdf2_sha512_t *) hash_buf->esalt;
18795
18796 /**
18797 * parse line
18798 */
18799
18800 // iterations
18801
18802 char *iter_pos = input_buf + 7;
18803
18804 u32 iter = atoi (iter_pos);
18805
18806 if (iter < 1) return (PARSER_SALT_ITERATION);
18807 if (iter > 999999) return (PARSER_SALT_ITERATION);
18808
18809 // first is *raw* salt
18810
18811 char *salt_pos = strchr (iter_pos, ':');
18812
18813 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18814
18815 salt_pos++;
18816
18817 char *hash_pos = strchr (salt_pos, ':');
18818
18819 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18820
18821 u32 salt_len = hash_pos - salt_pos;
18822
18823 if (salt_len > 64) return (PARSER_SALT_LENGTH);
18824
18825 hash_pos++;
18826
18827 u32 hash_b64_len = input_len - (hash_pos - input_buf);
18828
18829 if (hash_b64_len > 88) return (PARSER_HASH_LENGTH);
18830
18831 // decode salt
18832
18833 char *salt_buf_ptr = (char *) pbkdf2_sha512->salt_buf;
18834
18835 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
18836
18837 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
18838
18839 salt_buf_ptr[salt_len + 3] = 0x01;
18840 salt_buf_ptr[salt_len + 4] = 0x80;
18841
18842 salt->salt_len = salt_len;
18843 salt->salt_iter = iter - 1;
18844
18845 // decode hash
18846
18847 u8 tmp_buf[100] = { 0 };
18848
18849 int hash_len = base64_decode (base64_to_int, (const u8 *) hash_pos, hash_b64_len, tmp_buf);
18850
18851 if (hash_len < 16) return (PARSER_HASH_LENGTH);
18852
18853 memcpy (digest, tmp_buf, 64);
18854
18855 digest[0] = byte_swap_64 (digest[0]);
18856 digest[1] = byte_swap_64 (digest[1]);
18857 digest[2] = byte_swap_64 (digest[2]);
18858 digest[3] = byte_swap_64 (digest[3]);
18859 digest[4] = byte_swap_64 (digest[4]);
18860 digest[5] = byte_swap_64 (digest[5]);
18861 digest[6] = byte_swap_64 (digest[6]);
18862 digest[7] = byte_swap_64 (digest[7]);
18863
18864 // add some stuff to normal salt to make sorted happy
18865
18866 salt->salt_buf[0] = pbkdf2_sha512->salt_buf[0];
18867 salt->salt_buf[1] = pbkdf2_sha512->salt_buf[1];
18868 salt->salt_buf[2] = pbkdf2_sha512->salt_buf[2];
18869 salt->salt_buf[3] = pbkdf2_sha512->salt_buf[3];
18870 salt->salt_buf[4] = salt->salt_iter;
18871
18872 return (PARSER_OK);
18873 }
18874
18875 int ecryptfs_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18876 {
18877 if ((input_len < DISPLAY_LEN_MIN_12200) || (input_len > DISPLAY_LEN_MAX_12200)) return (PARSER_GLOBAL_LENGTH);
18878
18879 if (memcmp (SIGNATURE_ECRYPTFS, input_buf, 10)) return (PARSER_SIGNATURE_UNMATCHED);
18880
18881 uint *digest = (uint *) hash_buf->digest;
18882
18883 salt_t *salt = hash_buf->salt;
18884
18885 /**
18886 * parse line
18887 */
18888
18889 char *salt_pos = input_buf + 10 + 2 + 2; // skip over "0$" and "1$"
18890
18891 char *hash_pos = strchr (salt_pos, '$');
18892
18893 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18894
18895 u32 salt_len = hash_pos - salt_pos;
18896
18897 if (salt_len != 16) return (PARSER_SALT_LENGTH);
18898
18899 hash_pos++;
18900
18901 u32 hash_len = input_len - 10 - 2 - 2 - salt_len - 1;
18902
18903 if (hash_len != 16) return (PARSER_HASH_LENGTH);
18904
18905 // decode hash
18906
18907 digest[ 0] = hex_to_u32 ((const u8 *) &hash_pos[0]);
18908 digest[ 1] = hex_to_u32 ((const u8 *) &hash_pos[8]);
18909 digest[ 2] = 0;
18910 digest[ 3] = 0;
18911 digest[ 4] = 0;
18912 digest[ 5] = 0;
18913 digest[ 6] = 0;
18914 digest[ 7] = 0;
18915 digest[ 8] = 0;
18916 digest[ 9] = 0;
18917 digest[10] = 0;
18918 digest[11] = 0;
18919 digest[12] = 0;
18920 digest[13] = 0;
18921 digest[14] = 0;
18922 digest[15] = 0;
18923
18924 // decode salt
18925
18926 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_pos[0]);
18927 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_pos[8]);
18928
18929 salt->salt_iter = ROUNDS_ECRYPTFS;
18930 salt->salt_len = 8;
18931
18932 return (PARSER_OK);
18933 }
18934
18935 int bsdicrypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18936 {
18937 if ((input_len < DISPLAY_LEN_MIN_12400) || (input_len > DISPLAY_LEN_MAX_12400)) return (PARSER_GLOBAL_LENGTH);
18938
18939 if (memcmp (SIGNATURE_BSDICRYPT, input_buf, 1)) return (PARSER_SIGNATURE_UNMATCHED);
18940
18941 unsigned char c19 = itoa64_to_int (input_buf[19]);
18942
18943 if (c19 & 3) return (PARSER_HASH_VALUE);
18944
18945 salt_t *salt = hash_buf->salt;
18946
18947 u32 *digest = (u32 *) hash_buf->digest;
18948
18949 // iteration count
18950
18951 salt->salt_iter = itoa64_to_int (input_buf[1])
18952 | itoa64_to_int (input_buf[2]) << 6
18953 | itoa64_to_int (input_buf[3]) << 12
18954 | itoa64_to_int (input_buf[4]) << 18;
18955
18956 // set salt
18957
18958 salt->salt_buf[0] = itoa64_to_int (input_buf[5])
18959 | itoa64_to_int (input_buf[6]) << 6
18960 | itoa64_to_int (input_buf[7]) << 12
18961 | itoa64_to_int (input_buf[8]) << 18;
18962
18963 salt->salt_len = 4;
18964
18965 u8 tmp_buf[100] = { 0 };
18966
18967 base64_decode (itoa64_to_int, (const u8 *) input_buf + 9, 11, tmp_buf);
18968
18969 memcpy (digest, tmp_buf, 8);
18970
18971 uint tt;
18972
18973 IP (digest[0], digest[1], tt);
18974
18975 digest[0] = rotr32 (digest[0], 31);
18976 digest[1] = rotr32 (digest[1], 31);
18977 digest[2] = 0;
18978 digest[3] = 0;
18979
18980 return (PARSER_OK);
18981 }
18982
18983 int rar3hp_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18984 {
18985 if ((input_len < DISPLAY_LEN_MIN_12500) || (input_len > DISPLAY_LEN_MAX_12500)) return (PARSER_GLOBAL_LENGTH);
18986
18987 if (memcmp (SIGNATURE_RAR3, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
18988
18989 u32 *digest = (u32 *) hash_buf->digest;
18990
18991 salt_t *salt = hash_buf->salt;
18992
18993 /**
18994 * parse line
18995 */
18996
18997 char *type_pos = input_buf + 6 + 1;
18998
18999 char *salt_pos = strchr (type_pos, '*');
19000
19001 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19002
19003 u32 type_len = salt_pos - type_pos;
19004
19005 if (type_len != 1) return (PARSER_SALT_LENGTH);
19006
19007 salt_pos++;
19008
19009 char *crypted_pos = strchr (salt_pos, '*');
19010
19011 if (crypted_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19012
19013 u32 salt_len = crypted_pos - salt_pos;
19014
19015 if (salt_len != 16) return (PARSER_SALT_LENGTH);
19016
19017 crypted_pos++;
19018
19019 u32 crypted_len = input_len - 6 - 1 - type_len - 1 - salt_len - 1;
19020
19021 if (crypted_len != 32) return (PARSER_SALT_LENGTH);
19022
19023 /**
19024 * copy data
19025 */
19026
19027 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_pos[0]);
19028 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_pos[8]);
19029
19030 salt->salt_buf[0] = byte_swap_32 (salt->salt_buf[0]);
19031 salt->salt_buf[1] = byte_swap_32 (salt->salt_buf[1]);
19032
19033 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &crypted_pos[ 0]);
19034 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &crypted_pos[ 8]);
19035 salt->salt_buf[4] = hex_to_u32 ((const u8 *) &crypted_pos[16]);
19036 salt->salt_buf[5] = hex_to_u32 ((const u8 *) &crypted_pos[24]);
19037
19038 salt->salt_len = 24;
19039 salt->salt_iter = ROUNDS_RAR3;
19040
19041 // there's no hash for rar3. the data which is in crypted_pos is some encrypted data and
19042 // if it matches the value \xc4\x3d\x7b\x00\x40\x07\x00 after decrypt we know that we successfully cracked it.
19043
19044 digest[0] = 0xc43d7b00;
19045 digest[1] = 0x40070000;
19046 digest[2] = 0;
19047 digest[3] = 0;
19048
19049 return (PARSER_OK);
19050 }
19051
19052 int rar5_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
19053 {
19054 if ((input_len < DISPLAY_LEN_MIN_13000) || (input_len > DISPLAY_LEN_MAX_13000)) return (PARSER_GLOBAL_LENGTH);
19055
19056 if (memcmp (SIGNATURE_RAR5, input_buf, 1 + 4 + 1)) return (PARSER_SIGNATURE_UNMATCHED);
19057
19058 u32 *digest = (u32 *) hash_buf->digest;
19059
19060 salt_t *salt = hash_buf->salt;
19061
19062 rar5_t *rar5 = (rar5_t *) hash_buf->esalt;
19063
19064 /**
19065 * parse line
19066 */
19067
19068 char *param0_pos = input_buf + 1 + 4 + 1;
19069
19070 char *param1_pos = strchr (param0_pos, '$');
19071
19072 if (param1_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19073
19074 u32 param0_len = param1_pos - param0_pos;
19075
19076 param1_pos++;
19077
19078 char *param2_pos = strchr (param1_pos, '$');
19079
19080 if (param2_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19081
19082 u32 param1_len = param2_pos - param1_pos;
19083
19084 param2_pos++;
19085
19086 char *param3_pos = strchr (param2_pos, '$');
19087
19088 if (param3_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19089
19090 u32 param2_len = param3_pos - param2_pos;
19091
19092 param3_pos++;
19093
19094 char *param4_pos = strchr (param3_pos, '$');
19095
19096 if (param4_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19097
19098 u32 param3_len = param4_pos - param3_pos;
19099
19100 param4_pos++;
19101
19102 char *param5_pos = strchr (param4_pos, '$');
19103
19104 if (param5_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19105
19106 u32 param4_len = param5_pos - param4_pos;
19107
19108 param5_pos++;
19109
19110 u32 param5_len = input_len - 1 - 4 - 1 - param0_len - 1 - param1_len - 1 - param2_len - 1 - param3_len - 1 - param4_len - 1;
19111
19112 char *salt_buf = param1_pos;
19113 char *iv = param3_pos;
19114 char *pswcheck = param5_pos;
19115
19116 const uint salt_len = atoi (param0_pos);
19117 const uint iterations = atoi (param2_pos);
19118 const uint pswcheck_len = atoi (param4_pos);
19119
19120 /**
19121 * verify some data
19122 */
19123
19124 if (param1_len != 32) return (PARSER_SALT_VALUE);
19125 if (param3_len != 32) return (PARSER_SALT_VALUE);
19126 if (param5_len != 16) return (PARSER_SALT_VALUE);
19127
19128 if (salt_len != 16) return (PARSER_SALT_VALUE);
19129 if (iterations == 0) return (PARSER_SALT_VALUE);
19130 if (pswcheck_len != 8) return (PARSER_SALT_VALUE);
19131
19132 /**
19133 * store data
19134 */
19135
19136 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_buf[ 0]);
19137 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_buf[ 8]);
19138 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &salt_buf[16]);
19139 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &salt_buf[24]);
19140
19141 rar5->iv[0] = hex_to_u32 ((const u8 *) &iv[ 0]);
19142 rar5->iv[1] = hex_to_u32 ((const u8 *) &iv[ 8]);
19143 rar5->iv[2] = hex_to_u32 ((const u8 *) &iv[16]);
19144 rar5->iv[3] = hex_to_u32 ((const u8 *) &iv[24]);
19145
19146 salt->salt_len = 16;
19147
19148 salt->salt_sign[0] = iterations;
19149
19150 salt->salt_iter = ((1 << iterations) + 32) - 1;
19151
19152 /**
19153 * digest buf
19154 */
19155
19156 digest[0] = hex_to_u32 ((const u8 *) &pswcheck[ 0]);
19157 digest[1] = hex_to_u32 ((const u8 *) &pswcheck[ 8]);
19158 digest[2] = 0;
19159 digest[3] = 0;
19160
19161 return (PARSER_OK);
19162 }
19163
19164 int krb5tgs_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
19165 {
19166 if ((input_len < DISPLAY_LEN_MIN_13100) || (input_len > DISPLAY_LEN_MAX_13100)) return (PARSER_GLOBAL_LENGTH);
19167
19168 if (memcmp (SIGNATURE_KRB5TGS, input_buf, 11)) return (PARSER_SIGNATURE_UNMATCHED);
19169
19170 u32 *digest = (u32 *) hash_buf->digest;
19171
19172 salt_t *salt = hash_buf->salt;
19173
19174 krb5tgs_t *krb5tgs = (krb5tgs_t *) hash_buf->esalt;
19175
19176 /**
19177 * parse line
19178 */
19179
19180 /* Skip '$' */
19181 char *account_pos = input_buf + 11 + 1;
19182
19183 char *data_pos;
19184
19185 uint data_len;
19186
19187 if (account_pos[0] == '*')
19188 {
19189 account_pos++;
19190
19191 data_pos = strchr (account_pos, '*');
19192
19193 /* Skip '*' */
19194 data_pos++;
19195
19196 if (data_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19197
19198 uint account_len = data_pos - account_pos + 1;
19199
19200 if (account_len >= 512) return (PARSER_SALT_LENGTH);
19201
19202 /* Skip '$' */
19203 data_pos++;
19204
19205 data_len = input_len - 11 - 1 - account_len - 2;
19206
19207 memcpy (krb5tgs->account_info, account_pos - 1, account_len);
19208 }
19209 else
19210 {
19211 /* assume $krb5tgs$23$checksum$edata2 */
19212 data_pos = account_pos;
19213
19214 memcpy (krb5tgs->account_info, "**", 3);
19215
19216 data_len = input_len - 11 - 1 - 1;
19217 }
19218
19219 if (data_len < ((16 + 32) * 2)) return (PARSER_SALT_LENGTH);
19220
19221 char *checksum_ptr = (char *) krb5tgs->checksum;
19222
19223 for (uint i = 0; i < 16 * 2; i += 2)
19224 {
19225 const char p0 = data_pos[i + 0];
19226 const char p1 = data_pos[i + 1];
19227
19228 *checksum_ptr++ = hex_convert (p1) << 0
19229 | hex_convert (p0) << 4;
19230 }
19231
19232 char *edata_ptr = (char *) krb5tgs->edata2;
19233
19234 krb5tgs->edata2_len = (data_len - 32) / 2 ;
19235
19236 /* skip '$' */
19237 for (uint i = 16 * 2 + 1; i < (krb5tgs->edata2_len * 2) + (16 * 2 + 1); i += 2)
19238 {
19239 const char p0 = data_pos[i + 0];
19240 const char p1 = data_pos[i + 1];
19241 *edata_ptr++ = hex_convert (p1) << 0
19242 | hex_convert (p0) << 4;
19243 }
19244
19245 /* this is needed for hmac_md5 */
19246 *edata_ptr++ = 0x80;
19247
19248 salt->salt_buf[0] = krb5tgs->checksum[0];
19249 salt->salt_buf[1] = krb5tgs->checksum[1];
19250 salt->salt_buf[2] = krb5tgs->checksum[2];
19251 salt->salt_buf[3] = krb5tgs->checksum[3];
19252
19253 salt->salt_len = 32;
19254
19255 digest[0] = krb5tgs->checksum[0];
19256 digest[1] = krb5tgs->checksum[1];
19257 digest[2] = krb5tgs->checksum[2];
19258 digest[3] = krb5tgs->checksum[3];
19259
19260 return (PARSER_OK);
19261 }
19262
19263 int axcrypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
19264 {
19265 if ((input_len < DISPLAY_LEN_MIN_13200) || (input_len > DISPLAY_LEN_MAX_13200)) return (PARSER_GLOBAL_LENGTH);
19266
19267 if (memcmp (SIGNATURE_AXCRYPT, input_buf, 11)) return (PARSER_SIGNATURE_UNMATCHED);
19268
19269 u32 *digest = (u32 *) hash_buf->digest;
19270
19271 salt_t *salt = hash_buf->salt;
19272
19273 /**
19274 * parse line
19275 */
19276
19277 /* Skip '*' */
19278 char *wrapping_rounds_pos = input_buf + 11 + 1;
19279
19280 char *salt_pos;
19281
19282 char *wrapped_key_pos;
19283
19284 char *data_pos;
19285
19286 salt->salt_iter = atoi (wrapping_rounds_pos);
19287
19288 salt_pos = strchr (wrapping_rounds_pos, '*');
19289
19290 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19291
19292 uint wrapping_rounds_len = salt_pos - wrapping_rounds_pos;
19293
19294 /* Skip '*' */
19295 salt_pos++;
19296
19297 data_pos = salt_pos;
19298
19299 wrapped_key_pos = strchr (salt_pos, '*');
19300
19301 if (wrapped_key_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19302
19303 uint salt_len = wrapped_key_pos - salt_pos;
19304
19305 if (salt_len != 32) return (PARSER_SALT_LENGTH);
19306
19307 /* Skip '*' */
19308 wrapped_key_pos++;
19309
19310 uint wrapped_key_len = input_len - 11 - 1 - wrapping_rounds_len - 1 - salt_len - 1;
19311
19312 if (wrapped_key_len != 48) return (PARSER_SALT_LENGTH);
19313
19314 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &data_pos[ 0]);
19315 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &data_pos[ 8]);
19316 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &data_pos[16]);
19317 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &data_pos[24]);
19318
19319 data_pos += 33;
19320
19321 salt->salt_buf[4] = hex_to_u32 ((const u8 *) &data_pos[ 0]);
19322 salt->salt_buf[5] = hex_to_u32 ((const u8 *) &data_pos[ 8]);
19323 salt->salt_buf[6] = hex_to_u32 ((const u8 *) &data_pos[16]);
19324 salt->salt_buf[7] = hex_to_u32 ((const u8 *) &data_pos[24]);
19325 salt->salt_buf[8] = hex_to_u32 ((const u8 *) &data_pos[32]);
19326 salt->salt_buf[9] = hex_to_u32 ((const u8 *) &data_pos[40]);
19327
19328 salt->salt_len = 40;
19329
19330 digest[0] = salt->salt_buf[0];
19331 digest[1] = salt->salt_buf[1];
19332 digest[2] = salt->salt_buf[2];
19333 digest[3] = salt->salt_buf[3];
19334
19335 return (PARSER_OK);
19336 }
19337
19338 int keepass_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
19339 {
19340 if ((input_len < DISPLAY_LEN_MIN_13400) || (input_len > DISPLAY_LEN_MAX_13400)) return (PARSER_GLOBAL_LENGTH);
19341
19342 if (memcmp (SIGNATURE_KEEPASS, input_buf, 9)) return (PARSER_SIGNATURE_UNMATCHED);
19343
19344 u32 *digest = (u32 *) hash_buf->digest;
19345
19346 salt_t *salt = hash_buf->salt;
19347
19348 keepass_t *keepass = (keepass_t *) hash_buf->esalt;
19349
19350 /**
19351 * parse line
19352 */
19353
19354 char *version_pos;
19355
19356 char *rounds_pos;
19357
19358 char *algorithm_pos;
19359
19360 char *final_random_seed_pos;
19361 u32 final_random_seed_len;
19362
19363 char *transf_random_seed_pos;
19364 u32 transf_random_seed_len;
19365
19366 char *enc_iv_pos;
19367 u32 enc_iv_len;
19368
19369 /* default is no keyfile provided */
19370 char *keyfile_len_pos;
19371 u32 keyfile_len = 0;
19372 u32 is_keyfile_present = 0;
19373 char *keyfile_inline_pos;
19374 char *keyfile_pos;
19375
19376 /* specific to version 1 */
19377 char *contents_len_pos;
19378 u32 contents_len;
19379 char *contents_pos;
19380
19381 /* specific to version 2 */
19382 char *expected_bytes_pos;
19383 u32 expected_bytes_len;
19384
19385 char *contents_hash_pos;
19386 u32 contents_hash_len;
19387
19388 version_pos = input_buf + 8 + 1 + 1;
19389
19390 keepass->version = atoi (version_pos);
19391
19392 rounds_pos = strchr (version_pos, '*');
19393
19394 if (rounds_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19395
19396 rounds_pos++;
19397
19398 salt->salt_iter = (atoi (rounds_pos));
19399
19400 algorithm_pos = strchr (rounds_pos, '*');
19401
19402 if (algorithm_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19403
19404 algorithm_pos++;
19405
19406 keepass->algorithm = atoi (algorithm_pos);
19407
19408 final_random_seed_pos = strchr (algorithm_pos, '*');
19409
19410 if (final_random_seed_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19411
19412 final_random_seed_pos++;
19413
19414 keepass->final_random_seed[0] = hex_to_u32 ((const u8 *) &final_random_seed_pos[ 0]);
19415 keepass->final_random_seed[1] = hex_to_u32 ((const u8 *) &final_random_seed_pos[ 8]);
19416 keepass->final_random_seed[2] = hex_to_u32 ((const u8 *) &final_random_seed_pos[16]);
19417 keepass->final_random_seed[3] = hex_to_u32 ((const u8 *) &final_random_seed_pos[24]);
19418
19419 if (keepass->version == 2)
19420 {
19421 keepass->final_random_seed[4] = hex_to_u32 ((const u8 *) &final_random_seed_pos[32]);
19422 keepass->final_random_seed[5] = hex_to_u32 ((const u8 *) &final_random_seed_pos[40]);
19423 keepass->final_random_seed[6] = hex_to_u32 ((const u8 *) &final_random_seed_pos[48]);
19424 keepass->final_random_seed[7] = hex_to_u32 ((const u8 *) &final_random_seed_pos[56]);
19425 }
19426
19427 transf_random_seed_pos = strchr (final_random_seed_pos, '*');
19428
19429 if (transf_random_seed_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19430
19431 final_random_seed_len = transf_random_seed_pos - final_random_seed_pos;
19432
19433 if (keepass->version == 1 && final_random_seed_len != 32) return (PARSER_SALT_LENGTH);
19434 if (keepass->version == 2 && final_random_seed_len != 64) return (PARSER_SALT_LENGTH);
19435
19436 transf_random_seed_pos++;
19437
19438 keepass->transf_random_seed[0] = hex_to_u32 ((const u8 *) &transf_random_seed_pos[ 0]);
19439 keepass->transf_random_seed[1] = hex_to_u32 ((const u8 *) &transf_random_seed_pos[ 8]);
19440 keepass->transf_random_seed[2] = hex_to_u32 ((const u8 *) &transf_random_seed_pos[16]);
19441 keepass->transf_random_seed[3] = hex_to_u32 ((const u8 *) &transf_random_seed_pos[24]);
19442 keepass->transf_random_seed[4] = hex_to_u32 ((const u8 *) &transf_random_seed_pos[32]);
19443 keepass->transf_random_seed[5] = hex_to_u32 ((const u8 *) &transf_random_seed_pos[40]);
19444 keepass->transf_random_seed[6] = hex_to_u32 ((const u8 *) &transf_random_seed_pos[48]);
19445 keepass->transf_random_seed[7] = hex_to_u32 ((const u8 *) &transf_random_seed_pos[56]);
19446
19447 enc_iv_pos = strchr (transf_random_seed_pos, '*');
19448
19449 if (enc_iv_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19450
19451 transf_random_seed_len = enc_iv_pos - transf_random_seed_pos;
19452
19453 if (transf_random_seed_len != 64) return (PARSER_SALT_LENGTH);
19454
19455 enc_iv_pos++;
19456
19457 keepass->enc_iv[0] = hex_to_u32 ((const u8 *) &enc_iv_pos[ 0]);
19458 keepass->enc_iv[1] = hex_to_u32 ((const u8 *) &enc_iv_pos[ 8]);
19459 keepass->enc_iv[2] = hex_to_u32 ((const u8 *) &enc_iv_pos[16]);
19460 keepass->enc_iv[3] = hex_to_u32 ((const u8 *) &enc_iv_pos[24]);
19461
19462 if (keepass->version == 1)
19463 {
19464 contents_hash_pos = strchr (enc_iv_pos, '*');
19465
19466 if (contents_hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19467
19468 enc_iv_len = contents_hash_pos - enc_iv_pos;
19469
19470 if (enc_iv_len != 32) return (PARSER_SALT_LENGTH);
19471
19472 contents_hash_pos++;
19473
19474 keepass->contents_hash[0] = hex_to_u32 ((const u8 *) &contents_hash_pos[ 0]);
19475 keepass->contents_hash[1] = hex_to_u32 ((const u8 *) &contents_hash_pos[ 8]);
19476 keepass->contents_hash[2] = hex_to_u32 ((const u8 *) &contents_hash_pos[16]);
19477 keepass->contents_hash[3] = hex_to_u32 ((const u8 *) &contents_hash_pos[24]);
19478 keepass->contents_hash[4] = hex_to_u32 ((const u8 *) &contents_hash_pos[32]);
19479 keepass->contents_hash[5] = hex_to_u32 ((const u8 *) &contents_hash_pos[40]);
19480 keepass->contents_hash[6] = hex_to_u32 ((const u8 *) &contents_hash_pos[48]);
19481 keepass->contents_hash[7] = hex_to_u32 ((const u8 *) &contents_hash_pos[56]);
19482
19483 /* get length of contents following */
19484 char *inline_flag_pos = strchr (contents_hash_pos, '*');
19485
19486 if (inline_flag_pos == NULL) return (PARSER_SALT_LENGTH);
19487
19488 contents_hash_len = inline_flag_pos - contents_hash_pos;
19489
19490 if (contents_hash_len != 64) return (PARSER_SALT_LENGTH);
19491
19492 inline_flag_pos++;
19493
19494 u32 inline_flag = atoi (inline_flag_pos);
19495
19496 if (inline_flag != 1) return (PARSER_SALT_LENGTH);
19497
19498 contents_len_pos = strchr (inline_flag_pos, '*');
19499
19500 if (contents_len_pos == NULL) return (PARSER_SALT_LENGTH);
19501
19502 contents_len_pos++;
19503
19504 contents_len = atoi (contents_len_pos);
19505
19506 if (contents_len > 50000) return (PARSER_SALT_LENGTH);
19507
19508 contents_pos = strchr (contents_len_pos, '*');
19509
19510 if (contents_pos == NULL) return (PARSER_SALT_LENGTH);
19511
19512 contents_pos++;
19513
19514 u32 i;
19515
19516 keepass->contents_len = contents_len;
19517
19518 contents_len = contents_len / 4;
19519
19520 keyfile_inline_pos = strchr (contents_pos, '*');
19521
19522 u32 real_contents_len;
19523
19524 if (keyfile_inline_pos == NULL)
19525 real_contents_len = input_len - (contents_pos - input_buf);
19526 else
19527 {
19528 real_contents_len = keyfile_inline_pos - contents_pos;
19529 keyfile_inline_pos++;
19530 is_keyfile_present = 1;
19531 }
19532
19533 if (real_contents_len != keepass->contents_len * 2) return (PARSER_SALT_LENGTH);
19534
19535 for (i = 0; i < contents_len; i++)
19536 keepass->contents[i] = hex_to_u32 ((const u8 *) &contents_pos[i * 8]);
19537 }
19538 else if (keepass->version == 2)
19539 {
19540 expected_bytes_pos = strchr (enc_iv_pos, '*');
19541
19542 if (expected_bytes_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19543
19544 enc_iv_len = expected_bytes_pos - enc_iv_pos;
19545
19546 if (enc_iv_len != 32) return (PARSER_SALT_LENGTH);
19547
19548 expected_bytes_pos++;
19549
19550 keepass->expected_bytes[0] = hex_to_u32 ((const u8 *) &expected_bytes_pos[ 0]);
19551 keepass->expected_bytes[1] = hex_to_u32 ((const u8 *) &expected_bytes_pos[ 8]);
19552 keepass->expected_bytes[2] = hex_to_u32 ((const u8 *) &expected_bytes_pos[16]);
19553 keepass->expected_bytes[3] = hex_to_u32 ((const u8 *) &expected_bytes_pos[24]);
19554 keepass->expected_bytes[4] = hex_to_u32 ((const u8 *) &expected_bytes_pos[32]);
19555 keepass->expected_bytes[5] = hex_to_u32 ((const u8 *) &expected_bytes_pos[40]);
19556 keepass->expected_bytes[6] = hex_to_u32 ((const u8 *) &expected_bytes_pos[48]);
19557 keepass->expected_bytes[7] = hex_to_u32 ((const u8 *) &expected_bytes_pos[56]);
19558
19559 contents_hash_pos = strchr (expected_bytes_pos, '*');
19560
19561 if (contents_hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19562
19563 expected_bytes_len = contents_hash_pos - expected_bytes_pos;
19564
19565 if (expected_bytes_len != 64) return (PARSER_SALT_LENGTH);
19566
19567 contents_hash_pos++;
19568
19569 keepass->contents_hash[0] = hex_to_u32 ((const u8 *) &contents_hash_pos[ 0]);
19570 keepass->contents_hash[1] = hex_to_u32 ((const u8 *) &contents_hash_pos[ 8]);
19571 keepass->contents_hash[2] = hex_to_u32 ((const u8 *) &contents_hash_pos[16]);
19572 keepass->contents_hash[3] = hex_to_u32 ((const u8 *) &contents_hash_pos[24]);
19573 keepass->contents_hash[4] = hex_to_u32 ((const u8 *) &contents_hash_pos[32]);
19574 keepass->contents_hash[5] = hex_to_u32 ((const u8 *) &contents_hash_pos[40]);
19575 keepass->contents_hash[6] = hex_to_u32 ((const u8 *) &contents_hash_pos[48]);
19576 keepass->contents_hash[7] = hex_to_u32 ((const u8 *) &contents_hash_pos[56]);
19577
19578 keyfile_inline_pos = strchr (contents_hash_pos, '*');
19579
19580 if (keyfile_inline_pos == NULL)
19581 contents_hash_len = input_len - (int) (contents_hash_pos - input_buf);
19582 else
19583 {
19584 contents_hash_len = keyfile_inline_pos - contents_hash_pos;
19585 keyfile_inline_pos++;
19586 is_keyfile_present = 1;
19587 }
19588 if (contents_hash_len != 64) return (PARSER_SALT_LENGTH);
19589 }
19590
19591 if (is_keyfile_present != 0)
19592 {
19593 keyfile_len_pos = strchr (keyfile_inline_pos, '*');
19594
19595 keyfile_len_pos++;
19596
19597 keyfile_len = atoi (keyfile_len_pos);
19598
19599 keepass->keyfile_len = keyfile_len;
19600
19601 if (keyfile_len != 64) return (PARSER_SALT_LENGTH);
19602
19603 keyfile_pos = strchr (keyfile_len_pos, '*');
19604
19605 if (keyfile_pos == NULL) return (PARSER_SALT_LENGTH);
19606
19607 keyfile_pos++;
19608
19609 u32 real_keyfile_len = input_len - (keyfile_pos - input_buf);
19610
19611 if (real_keyfile_len != 64) return (PARSER_SALT_LENGTH);
19612
19613 keepass->keyfile[0] = hex_to_u32 ((const u8 *) &keyfile_pos[ 0]);
19614 keepass->keyfile[1] = hex_to_u32 ((const u8 *) &keyfile_pos[ 8]);
19615 keepass->keyfile[2] = hex_to_u32 ((const u8 *) &keyfile_pos[16]);
19616 keepass->keyfile[3] = hex_to_u32 ((const u8 *) &keyfile_pos[24]);
19617 keepass->keyfile[4] = hex_to_u32 ((const u8 *) &keyfile_pos[32]);
19618 keepass->keyfile[5] = hex_to_u32 ((const u8 *) &keyfile_pos[40]);
19619 keepass->keyfile[6] = hex_to_u32 ((const u8 *) &keyfile_pos[48]);
19620 keepass->keyfile[7] = hex_to_u32 ((const u8 *) &keyfile_pos[56]);
19621 }
19622
19623 digest[0] = keepass->enc_iv[0];
19624 digest[1] = keepass->enc_iv[1];
19625 digest[2] = keepass->enc_iv[2];
19626 digest[3] = keepass->enc_iv[3];
19627
19628 salt->salt_buf[0] = keepass->transf_random_seed[0];
19629 salt->salt_buf[1] = keepass->transf_random_seed[1];
19630 salt->salt_buf[2] = keepass->transf_random_seed[2];
19631 salt->salt_buf[3] = keepass->transf_random_seed[3];
19632 salt->salt_buf[4] = keepass->transf_random_seed[4];
19633 salt->salt_buf[5] = keepass->transf_random_seed[5];
19634 salt->salt_buf[6] = keepass->transf_random_seed[6];
19635 salt->salt_buf[7] = keepass->transf_random_seed[7];
19636
19637 return (PARSER_OK);
19638 }
19639
19640 int cf10_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
19641 {
19642 if ((input_len < DISPLAY_LEN_MIN_12600) || (input_len > DISPLAY_LEN_MAX_12600)) return (PARSER_GLOBAL_LENGTH);
19643
19644 u32 *digest = (u32 *) hash_buf->digest;
19645
19646 salt_t *salt = hash_buf->salt;
19647
19648 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
19649 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
19650 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
19651 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
19652 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
19653 digest[5] = hex_to_u32 ((const u8 *) &input_buf[40]);
19654 digest[6] = hex_to_u32 ((const u8 *) &input_buf[48]);
19655 digest[7] = hex_to_u32 ((const u8 *) &input_buf[56]);
19656
19657 if (input_buf[64] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
19658
19659 uint salt_len = input_len - 64 - 1;
19660
19661 char *salt_buf = input_buf + 64 + 1;
19662
19663 char *salt_buf_ptr = (char *) salt->salt_buf;
19664
19665 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
19666
19667 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
19668
19669 salt->salt_len = salt_len;
19670
19671 /**
19672 * we can precompute the first sha256 transform
19673 */
19674
19675 uint w[16] = { 0 };
19676
19677 w[ 0] = byte_swap_32 (salt->salt_buf[ 0]);
19678 w[ 1] = byte_swap_32 (salt->salt_buf[ 1]);
19679 w[ 2] = byte_swap_32 (salt->salt_buf[ 2]);
19680 w[ 3] = byte_swap_32 (salt->salt_buf[ 3]);
19681 w[ 4] = byte_swap_32 (salt->salt_buf[ 4]);
19682 w[ 5] = byte_swap_32 (salt->salt_buf[ 5]);
19683 w[ 6] = byte_swap_32 (salt->salt_buf[ 6]);
19684 w[ 7] = byte_swap_32 (salt->salt_buf[ 7]);
19685 w[ 8] = byte_swap_32 (salt->salt_buf[ 8]);
19686 w[ 9] = byte_swap_32 (salt->salt_buf[ 9]);
19687 w[10] = byte_swap_32 (salt->salt_buf[10]);
19688 w[11] = byte_swap_32 (salt->salt_buf[11]);
19689 w[12] = byte_swap_32 (salt->salt_buf[12]);
19690 w[13] = byte_swap_32 (salt->salt_buf[13]);
19691 w[14] = byte_swap_32 (salt->salt_buf[14]);
19692 w[15] = byte_swap_32 (salt->salt_buf[15]);
19693
19694 uint pc256[8] = { SHA256M_A, SHA256M_B, SHA256M_C, SHA256M_D, SHA256M_E, SHA256M_F, SHA256M_G, SHA256M_H };
19695
19696 sha256_64 (w, pc256);
19697
19698 salt->salt_buf_pc[0] = pc256[0];
19699 salt->salt_buf_pc[1] = pc256[1];
19700 salt->salt_buf_pc[2] = pc256[2];
19701 salt->salt_buf_pc[3] = pc256[3];
19702 salt->salt_buf_pc[4] = pc256[4];
19703 salt->salt_buf_pc[5] = pc256[5];
19704 salt->salt_buf_pc[6] = pc256[6];
19705 salt->salt_buf_pc[7] = pc256[7];
19706
19707 digest[0] -= pc256[0];
19708 digest[1] -= pc256[1];
19709 digest[2] -= pc256[2];
19710 digest[3] -= pc256[3];
19711 digest[4] -= pc256[4];
19712 digest[5] -= pc256[5];
19713 digest[6] -= pc256[6];
19714 digest[7] -= pc256[7];
19715
19716 return (PARSER_OK);
19717 }
19718
19719 int mywallet_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
19720 {
19721 if ((input_len < DISPLAY_LEN_MIN_12700) || (input_len > DISPLAY_LEN_MAX_12700)) return (PARSER_GLOBAL_LENGTH);
19722
19723 if (memcmp (SIGNATURE_MYWALLET, input_buf, 12)) return (PARSER_SIGNATURE_UNMATCHED);
19724
19725 u32 *digest = (u32 *) hash_buf->digest;
19726
19727 salt_t *salt = hash_buf->salt;
19728
19729 /**
19730 * parse line
19731 */
19732
19733 char *data_len_pos = input_buf + 1 + 10 + 1;
19734
19735 char *data_buf_pos = strchr (data_len_pos, '$');
19736
19737 if (data_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19738
19739 u32 data_len_len = data_buf_pos - data_len_pos;
19740
19741 if (data_len_len < 1) return (PARSER_SALT_LENGTH);
19742 if (data_len_len > 5) return (PARSER_SALT_LENGTH);
19743
19744 data_buf_pos++;
19745
19746 u32 data_buf_len = input_len - 1 - 10 - 1 - data_len_len - 1;
19747
19748 if (data_buf_len < 64) return (PARSER_HASH_LENGTH);
19749
19750 if (data_buf_len % 16) return (PARSER_HASH_LENGTH);
19751
19752 u32 data_len = atoi (data_len_pos);
19753
19754 if ((data_len * 2) != data_buf_len) return (PARSER_HASH_LENGTH);
19755
19756 /**
19757 * salt
19758 */
19759
19760 char *salt_pos = data_buf_pos;
19761
19762 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_pos[ 0]);
19763 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_pos[ 8]);
19764 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &salt_pos[16]);
19765 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &salt_pos[24]);
19766
19767 // this is actually the CT, which is also the hash later (if matched)
19768
19769 salt->salt_buf[4] = hex_to_u32 ((const u8 *) &salt_pos[32]);
19770 salt->salt_buf[5] = hex_to_u32 ((const u8 *) &salt_pos[40]);
19771 salt->salt_buf[6] = hex_to_u32 ((const u8 *) &salt_pos[48]);
19772 salt->salt_buf[7] = hex_to_u32 ((const u8 *) &salt_pos[56]);
19773
19774 salt->salt_len = 32; // note we need to fix this to 16 in kernel
19775
19776 salt->salt_iter = 10 - 1;
19777
19778 /**
19779 * digest buf
19780 */
19781
19782 digest[0] = salt->salt_buf[4];
19783 digest[1] = salt->salt_buf[5];
19784 digest[2] = salt->salt_buf[6];
19785 digest[3] = salt->salt_buf[7];
19786
19787 return (PARSER_OK);
19788 }
19789
19790 int ms_drsr_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
19791 {
19792 if ((input_len < DISPLAY_LEN_MIN_12800) || (input_len > DISPLAY_LEN_MAX_12800)) return (PARSER_GLOBAL_LENGTH);
19793
19794 if (memcmp (SIGNATURE_MS_DRSR, input_buf, 11)) return (PARSER_SIGNATURE_UNMATCHED);
19795
19796 u32 *digest = (u32 *) hash_buf->digest;
19797
19798 salt_t *salt = hash_buf->salt;
19799
19800 /**
19801 * parse line
19802 */
19803
19804 char *salt_pos = input_buf + 11 + 1;
19805
19806 char *iter_pos = strchr (salt_pos, ',');
19807
19808 if (iter_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19809
19810 u32 salt_len = iter_pos - salt_pos;
19811
19812 if (salt_len != 20) return (PARSER_SALT_LENGTH);
19813
19814 iter_pos++;
19815
19816 char *hash_pos = strchr (iter_pos, ',');
19817
19818 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19819
19820 u32 iter_len = hash_pos - iter_pos;
19821
19822 if (iter_len > 5) return (PARSER_SALT_LENGTH);
19823
19824 hash_pos++;
19825
19826 u32 hash_len = input_len - 11 - 1 - salt_len - 1 - iter_len - 1;
19827
19828 if (hash_len != 64) return (PARSER_HASH_LENGTH);
19829
19830 /**
19831 * salt
19832 */
19833
19834 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_pos[ 0]);
19835 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_pos[ 8]);
19836 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &salt_pos[16]) & 0xffff0000;
19837 salt->salt_buf[3] = 0x00018000;
19838
19839 salt->salt_buf[0] = byte_swap_32 (salt->salt_buf[0]);
19840 salt->salt_buf[1] = byte_swap_32 (salt->salt_buf[1]);
19841 salt->salt_buf[2] = byte_swap_32 (salt->salt_buf[2]);
19842 salt->salt_buf[3] = byte_swap_32 (salt->salt_buf[3]);
19843
19844 salt->salt_len = salt_len / 2;
19845
19846 salt->salt_iter = atoi (iter_pos) - 1;
19847
19848 /**
19849 * digest buf
19850 */
19851
19852 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
19853 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
19854 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
19855 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
19856 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
19857 digest[5] = hex_to_u32 ((const u8 *) &hash_pos[40]);
19858 digest[6] = hex_to_u32 ((const u8 *) &hash_pos[48]);
19859 digest[7] = hex_to_u32 ((const u8 *) &hash_pos[56]);
19860
19861 return (PARSER_OK);
19862 }
19863
19864 int androidfde_samsung_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
19865 {
19866 if ((input_len < DISPLAY_LEN_MIN_12900) || (input_len > DISPLAY_LEN_MAX_12900)) return (PARSER_GLOBAL_LENGTH);
19867
19868 u32 *digest = (u32 *) hash_buf->digest;
19869
19870 salt_t *salt = hash_buf->salt;
19871
19872 /**
19873 * parse line
19874 */
19875
19876 char *hash_pos = input_buf + 64;
19877 char *salt1_pos = input_buf + 128;
19878 char *salt2_pos = input_buf;
19879
19880 /**
19881 * salt
19882 */
19883
19884 salt->salt_buf[ 0] = hex_to_u32 ((const u8 *) &salt1_pos[ 0]);
19885 salt->salt_buf[ 1] = hex_to_u32 ((const u8 *) &salt1_pos[ 8]);
19886 salt->salt_buf[ 2] = hex_to_u32 ((const u8 *) &salt1_pos[16]);
19887 salt->salt_buf[ 3] = hex_to_u32 ((const u8 *) &salt1_pos[24]);
19888
19889 salt->salt_buf[ 4] = hex_to_u32 ((const u8 *) &salt2_pos[ 0]);
19890 salt->salt_buf[ 5] = hex_to_u32 ((const u8 *) &salt2_pos[ 8]);
19891 salt->salt_buf[ 6] = hex_to_u32 ((const u8 *) &salt2_pos[16]);
19892 salt->salt_buf[ 7] = hex_to_u32 ((const u8 *) &salt2_pos[24]);
19893
19894 salt->salt_buf[ 8] = hex_to_u32 ((const u8 *) &salt2_pos[32]);
19895 salt->salt_buf[ 9] = hex_to_u32 ((const u8 *) &salt2_pos[40]);
19896 salt->salt_buf[10] = hex_to_u32 ((const u8 *) &salt2_pos[48]);
19897 salt->salt_buf[11] = hex_to_u32 ((const u8 *) &salt2_pos[56]);
19898
19899 salt->salt_len = 48;
19900
19901 salt->salt_iter = ROUNDS_ANDROIDFDE_SAMSUNG - 1;
19902
19903 /**
19904 * digest buf
19905 */
19906
19907 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
19908 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
19909 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
19910 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
19911 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
19912 digest[5] = hex_to_u32 ((const u8 *) &hash_pos[40]);
19913 digest[6] = hex_to_u32 ((const u8 *) &hash_pos[48]);
19914 digest[7] = hex_to_u32 ((const u8 *) &hash_pos[56]);
19915
19916 return (PARSER_OK);
19917 }
19918
19919 int zip2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
19920 {
19921 if ((input_len < DISPLAY_LEN_MIN_13600) || (input_len > DISPLAY_LEN_MAX_13600)) return (PARSER_GLOBAL_LENGTH);
19922
19923 if (memcmp (SIGNATURE_ZIP2_START, input_buf , 6)) return (PARSER_SIGNATURE_UNMATCHED);
19924 if (memcmp (SIGNATURE_ZIP2_STOP , input_buf + input_len - 7, 7)) return (PARSER_SIGNATURE_UNMATCHED);
19925
19926 u32 *digest = (u32 *) hash_buf->digest;
19927
19928 salt_t *salt = hash_buf->salt;
19929
19930 zip2_t *zip2 = (zip2_t *) hash_buf->esalt;
19931
19932 /**
19933 * parse line
19934 */
19935
19936 char *param0_pos = input_buf + 6 + 1;
19937
19938 char *param1_pos = strchr (param0_pos, '*');
19939
19940 if (param1_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19941
19942 u32 param0_len = param1_pos - param0_pos;
19943
19944 param1_pos++;
19945
19946 char *param2_pos = strchr (param1_pos, '*');
19947
19948 if (param2_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19949
19950 u32 param1_len = param2_pos - param1_pos;
19951
19952 param2_pos++;
19953
19954 char *param3_pos = strchr (param2_pos, '*');
19955
19956 if (param3_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19957
19958 u32 param2_len = param3_pos - param2_pos;
19959
19960 param3_pos++;
19961
19962 char *param4_pos = strchr (param3_pos, '*');
19963
19964 if (param4_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19965
19966 u32 param3_len = param4_pos - param3_pos;
19967
19968 param4_pos++;
19969
19970 char *param5_pos = strchr (param4_pos, '*');
19971
19972 if (param5_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19973
19974 u32 param4_len = param5_pos - param4_pos;
19975
19976 param5_pos++;
19977
19978 char *param6_pos = strchr (param5_pos, '*');
19979
19980 if (param6_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19981
19982 u32 param5_len = param6_pos - param5_pos;
19983
19984 param6_pos++;
19985
19986 char *param7_pos = strchr (param6_pos, '*');
19987
19988 if (param7_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19989
19990 u32 param6_len = param7_pos - param6_pos;
19991
19992 param7_pos++;
19993
19994 char *param8_pos = strchr (param7_pos, '*');
19995
19996 if (param8_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19997
19998 u32 param7_len = param8_pos - param7_pos;
19999
20000 param8_pos++;
20001
20002 const uint type = atoi (param0_pos);
20003 const uint mode = atoi (param1_pos);
20004 const uint magic = atoi (param2_pos);
20005
20006 char *salt_buf = param3_pos;
20007
20008 uint verify_bytes; sscanf (param4_pos, "%4x*", &verify_bytes);
20009
20010 const uint compress_length = atoi (param5_pos);
20011
20012 char *data_buf = param6_pos;
20013 char *auth = param7_pos;
20014
20015 /**
20016 * verify some data
20017 */
20018
20019 if (param0_len != 1) return (PARSER_SALT_VALUE);
20020
20021 if (param1_len != 1) return (PARSER_SALT_VALUE);
20022
20023 if (param2_len != 1) return (PARSER_SALT_VALUE);
20024
20025 if ((param3_len != 16) && (param3_len != 24) && (param3_len != 32)) return (PARSER_SALT_VALUE);
20026
20027 if (param4_len >= 5) return (PARSER_SALT_VALUE);
20028
20029 if (param5_len >= 5) return (PARSER_SALT_VALUE);
20030
20031 if (param6_len >= 8192) return (PARSER_SALT_VALUE);
20032
20033 if (param6_len & 1) return (PARSER_SALT_VALUE);
20034
20035 if (param7_len != 20) return (PARSER_SALT_VALUE);
20036
20037 if (type != 0) return (PARSER_SALT_VALUE);
20038
20039 if ((mode != 1) && (mode != 2) && (mode != 3)) return (PARSER_SALT_VALUE);
20040
20041 if (magic != 0) return (PARSER_SALT_VALUE);
20042
20043 if (verify_bytes >= 0x10000) return (PARSER_SALT_VALUE);
20044
20045 /**
20046 * store data
20047 */
20048
20049 zip2->type = type;
20050 zip2->mode = mode;
20051 zip2->magic = magic;
20052
20053 if (mode == 1)
20054 {
20055 zip2->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_buf[ 0]);
20056 zip2->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_buf[ 8]);
20057 zip2->salt_buf[2] = 0;
20058 zip2->salt_buf[3] = 0;
20059
20060 zip2->salt_len = 8;
20061 }
20062 else if (mode == 2)
20063 {
20064 zip2->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_buf[ 0]);
20065 zip2->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_buf[ 8]);
20066 zip2->salt_buf[2] = hex_to_u32 ((const u8 *) &salt_buf[16]);
20067 zip2->salt_buf[3] = 0;
20068
20069 zip2->salt_len = 12;
20070 }
20071 else if (mode == 3)
20072 {
20073 zip2->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_buf[ 0]);
20074 zip2->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_buf[ 8]);
20075 zip2->salt_buf[2] = hex_to_u32 ((const u8 *) &salt_buf[16]);
20076 zip2->salt_buf[3] = hex_to_u32 ((const u8 *) &salt_buf[24]);
20077
20078 zip2->salt_len = 16;
20079 }
20080
20081 zip2->salt_buf[0] = byte_swap_32 (zip2->salt_buf[0]);
20082 zip2->salt_buf[1] = byte_swap_32 (zip2->salt_buf[1]);
20083 zip2->salt_buf[2] = byte_swap_32 (zip2->salt_buf[2]);
20084 zip2->salt_buf[3] = byte_swap_32 (zip2->salt_buf[3]);
20085
20086 zip2->verify_bytes = verify_bytes;
20087
20088 zip2->compress_length = compress_length;
20089
20090 char *data_buf_ptr = (char *) zip2->data_buf;
20091
20092 for (uint i = 0; i < param6_len; i += 2)
20093 {
20094 const char p0 = data_buf[i + 0];
20095 const char p1 = data_buf[i + 1];
20096
20097 *data_buf_ptr++ = hex_convert (p1) << 0
20098 | hex_convert (p0) << 4;
20099
20100 zip2->data_len++;
20101 }
20102
20103 *data_buf_ptr = 0x80;
20104
20105 char *auth_ptr = (char *) zip2->auth_buf;
20106
20107 for (uint i = 0; i < param7_len; i += 2)
20108 {
20109 const char p0 = auth[i + 0];
20110 const char p1 = auth[i + 1];
20111
20112 *auth_ptr++ = hex_convert (p1) << 0
20113 | hex_convert (p0) << 4;
20114
20115 zip2->auth_len++;
20116 }
20117
20118 /**
20119 * salt buf (fake)
20120 */
20121
20122 salt->salt_buf[0] = zip2->salt_buf[0];
20123 salt->salt_buf[1] = zip2->salt_buf[1];
20124 salt->salt_buf[2] = zip2->salt_buf[2];
20125 salt->salt_buf[3] = zip2->salt_buf[3];
20126 salt->salt_buf[4] = zip2->data_buf[0];
20127 salt->salt_buf[5] = zip2->data_buf[1];
20128 salt->salt_buf[6] = zip2->data_buf[2];
20129 salt->salt_buf[7] = zip2->data_buf[3];
20130
20131 salt->salt_len = 32;
20132
20133 salt->salt_iter = ROUNDS_ZIP2 - 1;
20134
20135 /**
20136 * digest buf (fake)
20137 */
20138
20139 digest[0] = zip2->auth_buf[0];
20140 digest[1] = zip2->auth_buf[1];
20141 digest[2] = zip2->auth_buf[2];
20142 digest[3] = zip2->auth_buf[3];
20143
20144 return (PARSER_OK);
20145 }
20146
20147 /**
20148 * parallel running threads
20149 */
20150
20151 #ifdef WIN
20152
20153 BOOL WINAPI sigHandler_default (DWORD sig)
20154 {
20155 switch (sig)
20156 {
20157 case CTRL_CLOSE_EVENT:
20158
20159 /*
20160 * special case see: https://stackoverflow.com/questions/3640633/c-setconsolectrlhandler-routine-issue/5610042#5610042
20161 * if the user interacts w/ the user-interface (GUI/cmd), we need to do the finalization job within this signal handler
20162 * function otherwise it is too late (e.g. after returning from this function)
20163 */
20164
20165 myabort ();
20166
20167 SetConsoleCtrlHandler (NULL, TRUE);
20168
20169 hc_sleep (10);
20170
20171 return TRUE;
20172
20173 case CTRL_C_EVENT:
20174 case CTRL_LOGOFF_EVENT:
20175 case CTRL_SHUTDOWN_EVENT:
20176
20177 myabort ();
20178
20179 SetConsoleCtrlHandler (NULL, TRUE);
20180
20181 return TRUE;
20182 }
20183
20184 return FALSE;
20185 }
20186
20187 BOOL WINAPI sigHandler_benchmark (DWORD sig)
20188 {
20189 switch (sig)
20190 {
20191 case CTRL_CLOSE_EVENT:
20192
20193 myabort ();
20194
20195 SetConsoleCtrlHandler (NULL, TRUE);
20196
20197 hc_sleep (10);
20198
20199 return TRUE;
20200
20201 case CTRL_C_EVENT:
20202 case CTRL_LOGOFF_EVENT:
20203 case CTRL_SHUTDOWN_EVENT:
20204
20205 myquit ();
20206
20207 SetConsoleCtrlHandler (NULL, TRUE);
20208
20209 return TRUE;
20210 }
20211
20212 return FALSE;
20213 }
20214
20215 void hc_signal (BOOL WINAPI (callback) (DWORD))
20216 {
20217 if (callback == NULL)
20218 {
20219 SetConsoleCtrlHandler ((PHANDLER_ROUTINE) callback, FALSE);
20220 }
20221 else
20222 {
20223 SetConsoleCtrlHandler ((PHANDLER_ROUTINE) callback, TRUE);
20224 }
20225 }
20226
20227 #else
20228
20229 void sigHandler_default (int sig)
20230 {
20231 myabort ();
20232
20233 signal (sig, NULL);
20234 }
20235
20236 void sigHandler_benchmark (int sig)
20237 {
20238 myquit ();
20239
20240 signal (sig, NULL);
20241 }
20242
20243 void hc_signal (void (callback) (int))
20244 {
20245 if (callback == NULL) callback = SIG_DFL;
20246
20247 signal (SIGINT, callback);
20248 signal (SIGTERM, callback);
20249 signal (SIGABRT, callback);
20250 }
20251
20252 #endif
20253
20254 void status_display ();
20255
20256 void *thread_keypress (void *p)
20257 {
20258 int benchmark = *((int *) p);
20259
20260 uint quiet = data.quiet;
20261
20262 tty_break();
20263
20264 while ((data.devices_status != STATUS_EXHAUSTED) && (data.devices_status != STATUS_CRACKED) && (data.devices_status != STATUS_ABORTED) && (data.devices_status != STATUS_QUIT))
20265 {
20266 int ch = tty_getchar();
20267
20268 if (ch == -1) break;
20269
20270 if (ch == 0) continue;
20271
20272 //https://github.com/hashcat/hashcat/issues/302
20273 //#ifdef _POSIX
20274 //if (ch != '\n')
20275 //#endif
20276
20277 hc_thread_mutex_lock (mux_display);
20278
20279 log_info ("");
20280
20281 switch (ch)
20282 {
20283 case 's':
20284 case '\r':
20285 case '\n':
20286
20287 log_info ("");
20288
20289 status_display ();
20290
20291 log_info ("");
20292
20293 if (quiet == 0) fprintf (stdout, "%s", PROMPT);
20294 if (quiet == 0) fflush (stdout);
20295
20296 break;
20297
20298 case 'b':
20299
20300 log_info ("");
20301
20302 bypass ();
20303
20304 log_info ("");
20305
20306 if (quiet == 0) fprintf (stdout, "%s", PROMPT);
20307 if (quiet == 0) fflush (stdout);
20308
20309 break;
20310
20311 case 'p':
20312
20313 log_info ("");
20314
20315 SuspendThreads ();
20316
20317 log_info ("");
20318
20319 if (quiet == 0) fprintf (stdout, "%s", PROMPT);
20320 if (quiet == 0) fflush (stdout);
20321
20322 break;
20323
20324 case 'r':
20325
20326 log_info ("");
20327
20328 ResumeThreads ();
20329
20330 log_info ("");
20331
20332 if (quiet == 0) fprintf (stdout, "%s", PROMPT);
20333 if (quiet == 0) fflush (stdout);
20334
20335 break;
20336
20337 case 'c':
20338
20339 log_info ("");
20340
20341 if (benchmark == 1) break;
20342
20343 stop_at_checkpoint ();
20344
20345 log_info ("");
20346
20347 if (quiet == 0) fprintf (stdout, "%s", PROMPT);
20348 if (quiet == 0) fflush (stdout);
20349
20350 break;
20351
20352 case 'q':
20353
20354 log_info ("");
20355
20356 if (benchmark == 1)
20357 {
20358 myquit ();
20359 }
20360 else
20361 {
20362 myabort ();
20363 }
20364
20365 break;
20366 }
20367
20368 //https://github.com/hashcat/hashcat/issues/302
20369 //#ifdef _POSIX
20370 //if (ch != '\n')
20371 //#endif
20372
20373 hc_thread_mutex_unlock (mux_display);
20374 }
20375
20376 tty_fix();
20377
20378 return (p);
20379 }
20380
20381 /**
20382 * rules common
20383 */
20384
20385 bool class_num (const u8 c)
20386 {
20387 return ((c >= '0') && (c <= '9'));
20388 }
20389
20390 bool class_lower (const u8 c)
20391 {
20392 return ((c >= 'a') && (c <= 'z'));
20393 }
20394
20395 bool class_upper (const u8 c)
20396 {
20397 return ((c >= 'A') && (c <= 'Z'));
20398 }
20399
20400 bool class_alpha (const u8 c)
20401 {
20402 return (class_lower (c) || class_upper (c));
20403 }
20404
20405 int conv_ctoi (const u8 c)
20406 {
20407 if (class_num (c))
20408 {
20409 return c - '0';
20410 }
20411 else if (class_upper (c))
20412 {
20413 return c - 'A' + 10;
20414 }
20415
20416 return -1;
20417 }
20418
20419 int conv_itoc (const u8 c)
20420 {
20421 if (c < 10)
20422 {
20423 return c + '0';
20424 }
20425 else if (c < 37)
20426 {
20427 return c + 'A' - 10;
20428 }
20429
20430 return -1;
20431 }
20432
20433 /**
20434 * device rules
20435 */
20436
20437 #define INCR_POS if (++rule_pos == rule_len) return (-1)
20438 #define SET_NAME(rule,val) (rule)->cmds[rule_cnt] = ((val) & 0xff) << 0
20439 #define SET_P0(rule,val) INCR_POS; (rule)->cmds[rule_cnt] |= ((val) & 0xff) << 8
20440 #define SET_P1(rule,val) INCR_POS; (rule)->cmds[rule_cnt] |= ((val) & 0xff) << 16
20441 #define MAX_KERNEL_RULES 255
20442 #define GET_NAME(rule) rule_cmd = (((rule)->cmds[rule_cnt] >> 0) & 0xff)
20443 #define GET_P0(rule) INCR_POS; rule_buf[rule_pos] = (((rule)->cmds[rule_cnt] >> 8) & 0xff)
20444 #define GET_P1(rule) INCR_POS; rule_buf[rule_pos] = (((rule)->cmds[rule_cnt] >> 16) & 0xff)
20445
20446 #define SET_P0_CONV(rule,val) INCR_POS; (rule)->cmds[rule_cnt] |= ((conv_ctoi (val)) & 0xff) << 8
20447 #define SET_P1_CONV(rule,val) INCR_POS; (rule)->cmds[rule_cnt] |= ((conv_ctoi (val)) & 0xff) << 16
20448 #define GET_P0_CONV(rule) INCR_POS; rule_buf[rule_pos] = conv_itoc (((rule)->cmds[rule_cnt] >> 8) & 0xff)
20449 #define GET_P1_CONV(rule) INCR_POS; rule_buf[rule_pos] = conv_itoc (((rule)->cmds[rule_cnt] >> 16) & 0xff)
20450
20451 int cpu_rule_to_kernel_rule (char *rule_buf, uint rule_len, kernel_rule_t *rule)
20452 {
20453 uint rule_pos;
20454 uint rule_cnt;
20455
20456 for (rule_pos = 0, rule_cnt = 0; rule_pos < rule_len && rule_cnt < MAX_KERNEL_RULES; rule_pos++, rule_cnt++)
20457 {
20458 switch (rule_buf[rule_pos])
20459 {
20460 case ' ':
20461 rule_cnt--;
20462 break;
20463
20464 case RULE_OP_MANGLE_NOOP:
20465 SET_NAME (rule, rule_buf[rule_pos]);
20466 break;
20467
20468 case RULE_OP_MANGLE_LREST:
20469 SET_NAME (rule, rule_buf[rule_pos]);
20470 break;
20471
20472 case RULE_OP_MANGLE_UREST:
20473 SET_NAME (rule, rule_buf[rule_pos]);
20474 break;
20475
20476 case RULE_OP_MANGLE_LREST_UFIRST:
20477 SET_NAME (rule, rule_buf[rule_pos]);
20478 break;
20479
20480 case RULE_OP_MANGLE_UREST_LFIRST:
20481 SET_NAME (rule, rule_buf[rule_pos]);
20482 break;
20483
20484 case RULE_OP_MANGLE_TREST:
20485 SET_NAME (rule, rule_buf[rule_pos]);
20486 break;
20487
20488 case RULE_OP_MANGLE_TOGGLE_AT:
20489 SET_NAME (rule, rule_buf[rule_pos]);
20490 SET_P0_CONV (rule, rule_buf[rule_pos]);
20491 break;
20492
20493 case RULE_OP_MANGLE_REVERSE:
20494 SET_NAME (rule, rule_buf[rule_pos]);
20495 break;
20496
20497 case RULE_OP_MANGLE_DUPEWORD:
20498 SET_NAME (rule, rule_buf[rule_pos]);
20499 break;
20500
20501 case RULE_OP_MANGLE_DUPEWORD_TIMES:
20502 SET_NAME (rule, rule_buf[rule_pos]);
20503 SET_P0_CONV (rule, rule_buf[rule_pos]);
20504 break;
20505
20506 case RULE_OP_MANGLE_REFLECT:
20507 SET_NAME (rule, rule_buf[rule_pos]);
20508 break;
20509
20510 case RULE_OP_MANGLE_ROTATE_LEFT:
20511 SET_NAME (rule, rule_buf[rule_pos]);
20512 break;
20513
20514 case RULE_OP_MANGLE_ROTATE_RIGHT:
20515 SET_NAME (rule, rule_buf[rule_pos]);
20516 break;
20517
20518 case RULE_OP_MANGLE_APPEND:
20519 SET_NAME (rule, rule_buf[rule_pos]);
20520 SET_P0 (rule, rule_buf[rule_pos]);
20521 break;
20522
20523 case RULE_OP_MANGLE_PREPEND:
20524 SET_NAME (rule, rule_buf[rule_pos]);
20525 SET_P0 (rule, rule_buf[rule_pos]);
20526 break;
20527
20528 case RULE_OP_MANGLE_DELETE_FIRST:
20529 SET_NAME (rule, rule_buf[rule_pos]);
20530 break;
20531
20532 case RULE_OP_MANGLE_DELETE_LAST:
20533 SET_NAME (rule, rule_buf[rule_pos]);
20534 break;
20535
20536 case RULE_OP_MANGLE_DELETE_AT:
20537 SET_NAME (rule, rule_buf[rule_pos]);
20538 SET_P0_CONV (rule, rule_buf[rule_pos]);
20539 break;
20540
20541 case RULE_OP_MANGLE_EXTRACT:
20542 SET_NAME (rule, rule_buf[rule_pos]);
20543 SET_P0_CONV (rule, rule_buf[rule_pos]);
20544 SET_P1_CONV (rule, rule_buf[rule_pos]);
20545 break;
20546
20547 case RULE_OP_MANGLE_OMIT:
20548 SET_NAME (rule, rule_buf[rule_pos]);
20549 SET_P0_CONV (rule, rule_buf[rule_pos]);
20550 SET_P1_CONV (rule, rule_buf[rule_pos]);
20551 break;
20552
20553 case RULE_OP_MANGLE_INSERT:
20554 SET_NAME (rule, rule_buf[rule_pos]);
20555 SET_P0_CONV (rule, rule_buf[rule_pos]);
20556 SET_P1 (rule, rule_buf[rule_pos]);
20557 break;
20558
20559 case RULE_OP_MANGLE_OVERSTRIKE:
20560 SET_NAME (rule, rule_buf[rule_pos]);
20561 SET_P0_CONV (rule, rule_buf[rule_pos]);
20562 SET_P1 (rule, rule_buf[rule_pos]);
20563 break;
20564
20565 case RULE_OP_MANGLE_TRUNCATE_AT:
20566 SET_NAME (rule, rule_buf[rule_pos]);
20567 SET_P0_CONV (rule, rule_buf[rule_pos]);
20568 break;
20569
20570 case RULE_OP_MANGLE_REPLACE:
20571 SET_NAME (rule, rule_buf[rule_pos]);
20572 SET_P0 (rule, rule_buf[rule_pos]);
20573 SET_P1 (rule, rule_buf[rule_pos]);
20574 break;
20575
20576 case RULE_OP_MANGLE_PURGECHAR:
20577 return (-1);
20578 break;
20579
20580 case RULE_OP_MANGLE_TOGGLECASE_REC:
20581 return (-1);
20582 break;
20583
20584 case RULE_OP_MANGLE_DUPECHAR_FIRST:
20585 SET_NAME (rule, rule_buf[rule_pos]);
20586 SET_P0_CONV (rule, rule_buf[rule_pos]);
20587 break;
20588
20589 case RULE_OP_MANGLE_DUPECHAR_LAST:
20590 SET_NAME (rule, rule_buf[rule_pos]);
20591 SET_P0_CONV (rule, rule_buf[rule_pos]);
20592 break;
20593
20594 case RULE_OP_MANGLE_DUPECHAR_ALL:
20595 SET_NAME (rule, rule_buf[rule_pos]);
20596 break;
20597
20598 case RULE_OP_MANGLE_SWITCH_FIRST:
20599 SET_NAME (rule, rule_buf[rule_pos]);
20600 break;
20601
20602 case RULE_OP_MANGLE_SWITCH_LAST:
20603 SET_NAME (rule, rule_buf[rule_pos]);
20604 break;
20605
20606 case RULE_OP_MANGLE_SWITCH_AT:
20607 SET_NAME (rule, rule_buf[rule_pos]);
20608 SET_P0_CONV (rule, rule_buf[rule_pos]);
20609 SET_P1_CONV (rule, rule_buf[rule_pos]);
20610 break;
20611
20612 case RULE_OP_MANGLE_CHR_SHIFTL:
20613 SET_NAME (rule, rule_buf[rule_pos]);
20614 SET_P0_CONV (rule, rule_buf[rule_pos]);
20615 break;
20616
20617 case RULE_OP_MANGLE_CHR_SHIFTR:
20618 SET_NAME (rule, rule_buf[rule_pos]);
20619 SET_P0_CONV (rule, rule_buf[rule_pos]);
20620 break;
20621
20622 case RULE_OP_MANGLE_CHR_INCR:
20623 SET_NAME (rule, rule_buf[rule_pos]);
20624 SET_P0_CONV (rule, rule_buf[rule_pos]);
20625 break;
20626
20627 case RULE_OP_MANGLE_CHR_DECR:
20628 SET_NAME (rule, rule_buf[rule_pos]);
20629 SET_P0_CONV (rule, rule_buf[rule_pos]);
20630 break;
20631
20632 case RULE_OP_MANGLE_REPLACE_NP1:
20633 SET_NAME (rule, rule_buf[rule_pos]);
20634 SET_P0_CONV (rule, rule_buf[rule_pos]);
20635 break;
20636
20637 case RULE_OP_MANGLE_REPLACE_NM1:
20638 SET_NAME (rule, rule_buf[rule_pos]);
20639 SET_P0_CONV (rule, rule_buf[rule_pos]);
20640 break;
20641
20642 case RULE_OP_MANGLE_DUPEBLOCK_FIRST:
20643 SET_NAME (rule, rule_buf[rule_pos]);
20644 SET_P0_CONV (rule, rule_buf[rule_pos]);
20645 break;
20646
20647 case RULE_OP_MANGLE_DUPEBLOCK_LAST:
20648 SET_NAME (rule, rule_buf[rule_pos]);
20649 SET_P0_CONV (rule, rule_buf[rule_pos]);
20650 break;
20651
20652 case RULE_OP_MANGLE_TITLE:
20653 SET_NAME (rule, rule_buf[rule_pos]);
20654 break;
20655
20656 default:
20657 return (-1);
20658 break;
20659 }
20660 }
20661
20662 if (rule_pos < rule_len) return (-1);
20663
20664 return (0);
20665 }
20666
20667 int kernel_rule_to_cpu_rule (char *rule_buf, kernel_rule_t *rule)
20668 {
20669 uint rule_cnt;
20670 uint rule_pos;
20671 uint rule_len = HCBUFSIZ - 1; // maximum possible len
20672
20673 char rule_cmd;
20674
20675 for (rule_cnt = 0, rule_pos = 0; rule_pos < rule_len && rule_cnt < MAX_KERNEL_RULES; rule_pos++, rule_cnt++)
20676 {
20677 GET_NAME (rule);
20678
20679 if (rule_cnt > 0) rule_buf[rule_pos++] = ' ';
20680
20681 switch (rule_cmd)
20682 {
20683 case RULE_OP_MANGLE_NOOP:
20684 rule_buf[rule_pos] = rule_cmd;
20685 break;
20686
20687 case RULE_OP_MANGLE_LREST:
20688 rule_buf[rule_pos] = rule_cmd;
20689 break;
20690
20691 case RULE_OP_MANGLE_UREST:
20692 rule_buf[rule_pos] = rule_cmd;
20693 break;
20694
20695 case RULE_OP_MANGLE_LREST_UFIRST:
20696 rule_buf[rule_pos] = rule_cmd;
20697 break;
20698
20699 case RULE_OP_MANGLE_UREST_LFIRST:
20700 rule_buf[rule_pos] = rule_cmd;
20701 break;
20702
20703 case RULE_OP_MANGLE_TREST:
20704 rule_buf[rule_pos] = rule_cmd;
20705 break;
20706
20707 case RULE_OP_MANGLE_TOGGLE_AT:
20708 rule_buf[rule_pos] = rule_cmd;
20709 GET_P0_CONV (rule);
20710 break;
20711
20712 case RULE_OP_MANGLE_REVERSE:
20713 rule_buf[rule_pos] = rule_cmd;
20714 break;
20715
20716 case RULE_OP_MANGLE_DUPEWORD:
20717 rule_buf[rule_pos] = rule_cmd;
20718 break;
20719
20720 case RULE_OP_MANGLE_DUPEWORD_TIMES:
20721 rule_buf[rule_pos] = rule_cmd;
20722 GET_P0_CONV (rule);
20723 break;
20724
20725 case RULE_OP_MANGLE_REFLECT:
20726 rule_buf[rule_pos] = rule_cmd;
20727 break;
20728
20729 case RULE_OP_MANGLE_ROTATE_LEFT:
20730 rule_buf[rule_pos] = rule_cmd;
20731 break;
20732
20733 case RULE_OP_MANGLE_ROTATE_RIGHT:
20734 rule_buf[rule_pos] = rule_cmd;
20735 break;
20736
20737 case RULE_OP_MANGLE_APPEND:
20738 rule_buf[rule_pos] = rule_cmd;
20739 GET_P0 (rule);
20740 break;
20741
20742 case RULE_OP_MANGLE_PREPEND:
20743 rule_buf[rule_pos] = rule_cmd;
20744 GET_P0 (rule);
20745 break;
20746
20747 case RULE_OP_MANGLE_DELETE_FIRST:
20748 rule_buf[rule_pos] = rule_cmd;
20749 break;
20750
20751 case RULE_OP_MANGLE_DELETE_LAST:
20752 rule_buf[rule_pos] = rule_cmd;
20753 break;
20754
20755 case RULE_OP_MANGLE_DELETE_AT:
20756 rule_buf[rule_pos] = rule_cmd;
20757 GET_P0_CONV (rule);
20758 break;
20759
20760 case RULE_OP_MANGLE_EXTRACT:
20761 rule_buf[rule_pos] = rule_cmd;
20762 GET_P0_CONV (rule);
20763 GET_P1_CONV (rule);
20764 break;
20765
20766 case RULE_OP_MANGLE_OMIT:
20767 rule_buf[rule_pos] = rule_cmd;
20768 GET_P0_CONV (rule);
20769 GET_P1_CONV (rule);
20770 break;
20771
20772 case RULE_OP_MANGLE_INSERT:
20773 rule_buf[rule_pos] = rule_cmd;
20774 GET_P0_CONV (rule);
20775 GET_P1 (rule);
20776 break;
20777
20778 case RULE_OP_MANGLE_OVERSTRIKE:
20779 rule_buf[rule_pos] = rule_cmd;
20780 GET_P0_CONV (rule);
20781 GET_P1 (rule);
20782 break;
20783
20784 case RULE_OP_MANGLE_TRUNCATE_AT:
20785 rule_buf[rule_pos] = rule_cmd;
20786 GET_P0_CONV (rule);
20787 break;
20788
20789 case RULE_OP_MANGLE_REPLACE:
20790 rule_buf[rule_pos] = rule_cmd;
20791 GET_P0 (rule);
20792 GET_P1 (rule);
20793 break;
20794
20795 case RULE_OP_MANGLE_PURGECHAR:
20796 return (-1);
20797 break;
20798
20799 case RULE_OP_MANGLE_TOGGLECASE_REC:
20800 return (-1);
20801 break;
20802
20803 case RULE_OP_MANGLE_DUPECHAR_FIRST:
20804 rule_buf[rule_pos] = rule_cmd;
20805 GET_P0_CONV (rule);
20806 break;
20807
20808 case RULE_OP_MANGLE_DUPECHAR_LAST:
20809 rule_buf[rule_pos] = rule_cmd;
20810 GET_P0_CONV (rule);
20811 break;
20812
20813 case RULE_OP_MANGLE_DUPECHAR_ALL:
20814 rule_buf[rule_pos] = rule_cmd;
20815 break;
20816
20817 case RULE_OP_MANGLE_SWITCH_FIRST:
20818 rule_buf[rule_pos] = rule_cmd;
20819 break;
20820
20821 case RULE_OP_MANGLE_SWITCH_LAST:
20822 rule_buf[rule_pos] = rule_cmd;
20823 break;
20824
20825 case RULE_OP_MANGLE_SWITCH_AT:
20826 rule_buf[rule_pos] = rule_cmd;
20827 GET_P0_CONV (rule);
20828 GET_P1_CONV (rule);
20829 break;
20830
20831 case RULE_OP_MANGLE_CHR_SHIFTL:
20832 rule_buf[rule_pos] = rule_cmd;
20833 GET_P0_CONV (rule);
20834 break;
20835
20836 case RULE_OP_MANGLE_CHR_SHIFTR:
20837 rule_buf[rule_pos] = rule_cmd;
20838 GET_P0_CONV (rule);
20839 break;
20840
20841 case RULE_OP_MANGLE_CHR_INCR:
20842 rule_buf[rule_pos] = rule_cmd;
20843 GET_P0_CONV (rule);
20844 break;
20845
20846 case RULE_OP_MANGLE_CHR_DECR:
20847 rule_buf[rule_pos] = rule_cmd;
20848 GET_P0_CONV (rule);
20849 break;
20850
20851 case RULE_OP_MANGLE_REPLACE_NP1:
20852 rule_buf[rule_pos] = rule_cmd;
20853 GET_P0_CONV (rule);
20854 break;
20855
20856 case RULE_OP_MANGLE_REPLACE_NM1:
20857 rule_buf[rule_pos] = rule_cmd;
20858 GET_P0_CONV (rule);
20859 break;
20860
20861 case RULE_OP_MANGLE_DUPEBLOCK_FIRST:
20862 rule_buf[rule_pos] = rule_cmd;
20863 GET_P0_CONV (rule);
20864 break;
20865
20866 case RULE_OP_MANGLE_DUPEBLOCK_LAST:
20867 rule_buf[rule_pos] = rule_cmd;
20868 GET_P0_CONV (rule);
20869 break;
20870
20871 case RULE_OP_MANGLE_TITLE:
20872 rule_buf[rule_pos] = rule_cmd;
20873 break;
20874
20875 case 0:
20876 return rule_pos - 1;
20877 break;
20878
20879 default:
20880 return (-1);
20881 break;
20882 }
20883 }
20884
20885 if (rule_cnt > 0)
20886 {
20887 return rule_pos;
20888 }
20889
20890 return (-1);
20891 }
20892
20893 /**
20894 * CPU rules : this is from hashcat sources, cpu based rules
20895 */
20896
20897 #define NEXT_RULEPOS(rp) if (++(rp) == rule_len) return (RULE_RC_SYNTAX_ERROR)
20898 #define NEXT_RPTOI(r,rp,up) if (((up) = conv_ctoi ((r)[(rp)])) == -1) return (RULE_RC_SYNTAX_ERROR)
20899
20900 #define MANGLE_TOGGLE_AT(a,p) if (class_alpha ((a)[(p)])) (a)[(p)] ^= 0x20
20901 #define MANGLE_LOWER_AT(a,p) if (class_upper ((a)[(p)])) (a)[(p)] ^= 0x20
20902 #define MANGLE_UPPER_AT(a,p) if (class_lower ((a)[(p)])) (a)[(p)] ^= 0x20
20903
20904 /* #define MANGLE_SWITCH(a,l,r) { char c = (l); arr[(r)] = arr[(l)]; arr[(l)] = c; } */
20905 /* #define MANGLE_SWITCH(a,l,r) { char c = (l); (a)[(r)] = (a)[(l)]; (a)[(l)] = c; } */
20906 #define MANGLE_SWITCH(a,l,r) { char c = (a)[(r)]; (a)[(r)] = (a)[(l)]; (a)[(l)] = c; }
20907
20908 int mangle_lrest (char arr[BLOCK_SIZE], int arr_len)
20909 {
20910 int pos;
20911
20912 for (pos = 0; pos < arr_len; pos++) MANGLE_LOWER_AT (arr, pos);
20913
20914 return (arr_len);
20915 }
20916
20917 int mangle_urest (char arr[BLOCK_SIZE], int arr_len)
20918 {
20919 int pos;
20920
20921 for (pos = 0; pos < arr_len; pos++) MANGLE_UPPER_AT (arr, pos);
20922
20923 return (arr_len);
20924 }
20925
20926 int mangle_trest (char arr[BLOCK_SIZE], int arr_len)
20927 {
20928 int pos;
20929
20930 for (pos = 0; pos < arr_len; pos++) MANGLE_TOGGLE_AT (arr, pos);
20931
20932 return (arr_len);
20933 }
20934
20935 int mangle_reverse (char arr[BLOCK_SIZE], int arr_len)
20936 {
20937 int l;
20938 int r;
20939
20940 for (l = 0; l < arr_len; l++)
20941 {
20942 r = arr_len - 1 - l;
20943
20944 if (l >= r) break;
20945
20946 MANGLE_SWITCH (arr, l, r);
20947 }
20948
20949 return (arr_len);
20950 }
20951
20952 int mangle_double (char arr[BLOCK_SIZE], int arr_len)
20953 {
20954 if ((arr_len * 2) >= BLOCK_SIZE) return (arr_len);
20955
20956 memcpy (&arr[arr_len], arr, (size_t) arr_len);
20957
20958 return (arr_len * 2);
20959 }
20960
20961 int mangle_double_times (char arr[BLOCK_SIZE], int arr_len, int times)
20962 {
20963 if (((arr_len * times) + arr_len) >= BLOCK_SIZE) return (arr_len);
20964
20965 int orig_len = arr_len;
20966
20967 int i;
20968
20969 for (i = 0; i < times; i++)
20970 {
20971 memcpy (&arr[arr_len], arr, orig_len);
20972
20973 arr_len += orig_len;
20974 }
20975
20976 return (arr_len);
20977 }
20978
20979 int mangle_reflect (char arr[BLOCK_SIZE], int arr_len)
20980 {
20981 if ((arr_len * 2) >= BLOCK_SIZE) return (arr_len);
20982
20983 mangle_double (arr, arr_len);
20984
20985 mangle_reverse (arr + arr_len, arr_len);
20986
20987 return (arr_len * 2);
20988 }
20989
20990 int mangle_rotate_left (char arr[BLOCK_SIZE], int arr_len)
20991 {
20992 int l;
20993 int r;
20994
20995 for (l = 0, r = arr_len - 1; r > 0; r--)
20996 {
20997 MANGLE_SWITCH (arr, l, r);
20998 }
20999
21000 return (arr_len);
21001 }
21002
21003 int mangle_rotate_right (char arr[BLOCK_SIZE], int arr_len)
21004 {
21005 int l;
21006 int r;
21007
21008 for (l = 0, r = arr_len - 1; l < r; l++)
21009 {
21010 MANGLE_SWITCH (arr, l, r);
21011 }
21012
21013 return (arr_len);
21014 }
21015
21016 int mangle_append (char arr[BLOCK_SIZE], int arr_len, char c)
21017 {
21018 if ((arr_len + 1) >= BLOCK_SIZE) return (arr_len);
21019
21020 arr[arr_len] = c;
21021
21022 return (arr_len + 1);
21023 }
21024
21025 int mangle_prepend (char arr[BLOCK_SIZE], int arr_len, char c)
21026 {
21027 if ((arr_len + 1) >= BLOCK_SIZE) return (arr_len);
21028
21029 int arr_pos;
21030
21031 for (arr_pos = arr_len - 1; arr_pos > -1; arr_pos--)
21032 {
21033 arr[arr_pos + 1] = arr[arr_pos];
21034 }
21035
21036 arr[0] = c;
21037
21038 return (arr_len + 1);
21039 }
21040
21041 int mangle_delete_at (char arr[BLOCK_SIZE], int arr_len, int upos)
21042 {
21043 if (upos >= arr_len) return (arr_len);
21044
21045 int arr_pos;
21046
21047 for (arr_pos = upos; arr_pos < arr_len - 1; arr_pos++)
21048 {
21049 arr[arr_pos] = arr[arr_pos + 1];
21050 }
21051
21052 return (arr_len - 1);
21053 }
21054
21055 int mangle_extract (char arr[BLOCK_SIZE], int arr_len, int upos, int ulen)
21056 {
21057 if (upos >= arr_len) return (arr_len);
21058
21059 if ((upos + ulen) > arr_len) return (arr_len);
21060
21061 int arr_pos;
21062
21063 for (arr_pos = 0; arr_pos < ulen; arr_pos++)
21064 {
21065 arr[arr_pos] = arr[upos + arr_pos];
21066 }
21067
21068 return (ulen);
21069 }
21070
21071 int mangle_omit (char arr[BLOCK_SIZE], int arr_len, int upos, int ulen)
21072 {
21073 if (upos >= arr_len) return (arr_len);
21074
21075 if ((upos + ulen) >= arr_len) return (arr_len);
21076
21077 int arr_pos;
21078
21079 for (arr_pos = upos; arr_pos < arr_len - ulen; arr_pos++)
21080 {
21081 arr[arr_pos] = arr[arr_pos + ulen];
21082 }
21083
21084 return (arr_len - ulen);
21085 }
21086
21087 int mangle_insert (char arr[BLOCK_SIZE], int arr_len, int upos, char c)
21088 {
21089 if (upos >= arr_len) return (arr_len);
21090
21091 if ((arr_len + 1) >= BLOCK_SIZE) return (arr_len);
21092
21093 int arr_pos;
21094
21095 for (arr_pos = arr_len - 1; arr_pos > upos - 1; arr_pos--)
21096 {
21097 arr[arr_pos + 1] = arr[arr_pos];
21098 }
21099
21100 arr[upos] = c;
21101
21102 return (arr_len + 1);
21103 }
21104
21105 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)
21106 {
21107 if ((arr_len + arr2_cpy) > BLOCK_SIZE) return (RULE_RC_REJECT_ERROR);
21108
21109 if (arr_pos > arr_len) return (RULE_RC_REJECT_ERROR);
21110
21111 if (arr2_pos > arr2_len) return (RULE_RC_REJECT_ERROR);
21112
21113 if ((arr2_pos + arr2_cpy) > arr2_len) return (RULE_RC_REJECT_ERROR);
21114
21115 if (arr2_cpy < 1) return (RULE_RC_SYNTAX_ERROR);
21116
21117 memcpy (arr2, arr2 + arr2_pos, arr2_len - arr2_pos);
21118
21119 memcpy (arr2 + arr2_cpy, arr + arr_pos, arr_len - arr_pos);
21120
21121 memcpy (arr + arr_pos, arr2, arr_len - arr_pos + arr2_cpy);
21122
21123 return (arr_len + arr2_cpy);
21124 }
21125
21126 int mangle_overstrike (char arr[BLOCK_SIZE], int arr_len, int upos, char c)
21127 {
21128 if (upos >= arr_len) return (arr_len);
21129
21130 arr[upos] = c;
21131
21132 return (arr_len);
21133 }
21134
21135 int mangle_truncate_at (char arr[BLOCK_SIZE], int arr_len, int upos)
21136 {
21137 if (upos >= arr_len) return (arr_len);
21138
21139 memset (arr + upos, 0, arr_len - upos);
21140
21141 return (upos);
21142 }
21143
21144 int mangle_replace (char arr[BLOCK_SIZE], int arr_len, char oldc, char newc)
21145 {
21146 int arr_pos;
21147
21148 for (arr_pos = 0; arr_pos < arr_len; arr_pos++)
21149 {
21150 if (arr[arr_pos] != oldc) continue;
21151
21152 arr[arr_pos] = newc;
21153 }
21154
21155 return (arr_len);
21156 }
21157
21158 int mangle_purgechar (char arr[BLOCK_SIZE], int arr_len, char c)
21159 {
21160 int arr_pos;
21161
21162 int ret_len;
21163
21164 for (ret_len = 0, arr_pos = 0; arr_pos < arr_len; arr_pos++)
21165 {
21166 if (arr[arr_pos] == c) continue;
21167
21168 arr[ret_len] = arr[arr_pos];
21169
21170 ret_len++;
21171 }
21172
21173 return (ret_len);
21174 }
21175
21176 int mangle_dupeblock_prepend (char arr[BLOCK_SIZE], int arr_len, int ulen)
21177 {
21178 if (ulen > arr_len) return (arr_len);
21179
21180 if ((arr_len + ulen) >= BLOCK_SIZE) return (arr_len);
21181
21182 char cs[100] = { 0 };
21183
21184 memcpy (cs, arr, ulen);
21185
21186 int i;
21187
21188 for (i = 0; i < ulen; i++)
21189 {
21190 char c = cs[i];
21191
21192 arr_len = mangle_insert (arr, arr_len, i, c);
21193 }
21194
21195 return (arr_len);
21196 }
21197
21198 int mangle_dupeblock_append (char arr[BLOCK_SIZE], int arr_len, int ulen)
21199 {
21200 if (ulen > arr_len) return (arr_len);
21201
21202 if ((arr_len + ulen) >= BLOCK_SIZE) return (arr_len);
21203
21204 int upos = arr_len - ulen;
21205
21206 int i;
21207
21208 for (i = 0; i < ulen; i++)
21209 {
21210 char c = arr[upos + i];
21211
21212 arr_len = mangle_append (arr, arr_len, c);
21213 }
21214
21215 return (arr_len);
21216 }
21217
21218 int mangle_dupechar_at (char arr[BLOCK_SIZE], int arr_len, int upos, int ulen)
21219 {
21220 if ( arr_len == 0) return (arr_len);
21221 if ((arr_len + ulen) >= BLOCK_SIZE) return (arr_len);
21222
21223 char c = arr[upos];
21224
21225 int i;
21226
21227 for (i = 0; i < ulen; i++)
21228 {
21229 arr_len = mangle_insert (arr, arr_len, upos, c);
21230 }
21231
21232 return (arr_len);
21233 }
21234
21235 int mangle_dupechar (char arr[BLOCK_SIZE], int arr_len)
21236 {
21237 if ( arr_len == 0) return (arr_len);
21238 if ((arr_len + arr_len) >= BLOCK_SIZE) return (arr_len);
21239
21240 int arr_pos;
21241
21242 for (arr_pos = arr_len - 1; arr_pos > -1; arr_pos--)
21243 {
21244 int new_pos = arr_pos * 2;
21245
21246 arr[new_pos] = arr[arr_pos];
21247
21248 arr[new_pos + 1] = arr[arr_pos];
21249 }
21250
21251 return (arr_len * 2);
21252 }
21253
21254 int mangle_switch_at_check (char arr[BLOCK_SIZE], int arr_len, int upos, int upos2)
21255 {
21256 if (upos >= arr_len) return (arr_len);
21257 if (upos2 >= arr_len) return (arr_len);
21258
21259 MANGLE_SWITCH (arr, upos, upos2);
21260
21261 return (arr_len);
21262 }
21263
21264 int mangle_switch_at (char arr[BLOCK_SIZE], int arr_len, int upos, int upos2)
21265 {
21266 MANGLE_SWITCH (arr, upos, upos2);
21267
21268 return (arr_len);
21269 }
21270
21271 int mangle_chr_shiftl (char arr[BLOCK_SIZE], int arr_len, int upos)
21272 {
21273 if (upos >= arr_len) return (arr_len);
21274
21275 arr[upos] <<= 1;
21276
21277 return (arr_len);
21278 }
21279
21280 int mangle_chr_shiftr (char arr[BLOCK_SIZE], int arr_len, int upos)
21281 {
21282 if (upos >= arr_len) return (arr_len);
21283
21284 arr[upos] >>= 1;
21285
21286 return (arr_len);
21287 }
21288
21289 int mangle_chr_incr (char arr[BLOCK_SIZE], int arr_len, int upos)
21290 {
21291 if (upos >= arr_len) return (arr_len);
21292
21293 arr[upos] += 1;
21294
21295 return (arr_len);
21296 }
21297
21298 int mangle_chr_decr (char arr[BLOCK_SIZE], int arr_len, int upos)
21299 {
21300 if (upos >= arr_len) return (arr_len);
21301
21302 arr[upos] -= 1;
21303
21304 return (arr_len);
21305 }
21306
21307 int mangle_title (char arr[BLOCK_SIZE], int arr_len)
21308 {
21309 int upper_next = 1;
21310
21311 int pos;
21312
21313 for (pos = 0; pos < arr_len; pos++)
21314 {
21315 if (arr[pos] == ' ')
21316 {
21317 upper_next = 1;
21318
21319 continue;
21320 }
21321
21322 if (upper_next)
21323 {
21324 upper_next = 0;
21325
21326 MANGLE_UPPER_AT (arr, pos);
21327 }
21328 else
21329 {
21330 MANGLE_LOWER_AT (arr, pos);
21331 }
21332 }
21333
21334 return (arr_len);
21335 }
21336
21337 int generate_random_rule (char rule_buf[RP_RULE_BUFSIZ], u32 rp_gen_func_min, u32 rp_gen_func_max)
21338 {
21339 u32 rp_gen_num = get_random_num (rp_gen_func_min, rp_gen_func_max);
21340
21341 u32 j;
21342
21343 u32 rule_pos = 0;
21344
21345 for (j = 0; j < rp_gen_num; j++)
21346 {
21347 u32 r = 0;
21348 u32 p1 = 0;
21349 u32 p2 = 0;
21350 u32 p3 = 0;
21351
21352 switch ((char) get_random_num (0, 9))
21353 {
21354 case 0:
21355 r = get_random_num (0, sizeof (grp_op_nop));
21356 rule_buf[rule_pos++] = grp_op_nop[r];
21357 break;
21358
21359 case 1:
21360 r = get_random_num (0, sizeof (grp_op_pos_p0));
21361 rule_buf[rule_pos++] = grp_op_pos_p0[r];
21362 p1 = get_random_num (0, sizeof (grp_pos));
21363 rule_buf[rule_pos++] = grp_pos[p1];
21364 break;
21365
21366 case 2:
21367 r = get_random_num (0, sizeof (grp_op_pos_p1));
21368 rule_buf[rule_pos++] = grp_op_pos_p1[r];
21369 p1 = get_random_num (1, 6);
21370 rule_buf[rule_pos++] = grp_pos[p1];
21371 break;
21372
21373 case 3:
21374 r = get_random_num (0, sizeof (grp_op_chr));
21375 rule_buf[rule_pos++] = grp_op_chr[r];
21376 p1 = get_random_num (0x20, 0x7e);
21377 rule_buf[rule_pos++] = (char) p1;
21378 break;
21379
21380 case 4:
21381 r = get_random_num (0, sizeof (grp_op_chr_chr));
21382 rule_buf[rule_pos++] = grp_op_chr_chr[r];
21383 p1 = get_random_num (0x20, 0x7e);
21384 rule_buf[rule_pos++] = (char) p1;
21385 p2 = get_random_num (0x20, 0x7e);
21386 while (p1 == p2)
21387 p2 = get_random_num (0x20, 0x7e);
21388 rule_buf[rule_pos++] = (char) p2;
21389 break;
21390
21391 case 5:
21392 r = get_random_num (0, sizeof (grp_op_pos_chr));
21393 rule_buf[rule_pos++] = grp_op_pos_chr[r];
21394 p1 = get_random_num (0, sizeof (grp_pos));
21395 rule_buf[rule_pos++] = grp_pos[p1];
21396 p2 = get_random_num (0x20, 0x7e);
21397 rule_buf[rule_pos++] = (char) p2;
21398 break;
21399
21400 case 6:
21401 r = get_random_num (0, sizeof (grp_op_pos_pos0));
21402 rule_buf[rule_pos++] = grp_op_pos_pos0[r];
21403 p1 = get_random_num (0, sizeof (grp_pos));
21404 rule_buf[rule_pos++] = grp_pos[p1];
21405 p2 = get_random_num (0, sizeof (grp_pos));
21406 while (p1 == p2)
21407 p2 = get_random_num (0, sizeof (grp_pos));
21408 rule_buf[rule_pos++] = grp_pos[p2];
21409 break;
21410
21411 case 7:
21412 r = get_random_num (0, sizeof (grp_op_pos_pos1));
21413 rule_buf[rule_pos++] = grp_op_pos_pos1[r];
21414 p1 = get_random_num (0, sizeof (grp_pos));
21415 rule_buf[rule_pos++] = grp_pos[p1];
21416 p2 = get_random_num (1, sizeof (grp_pos));
21417 while (p1 == p2)
21418 p2 = get_random_num (1, sizeof (grp_pos));
21419 rule_buf[rule_pos++] = grp_pos[p2];
21420 break;
21421
21422 case 8:
21423 r = get_random_num (0, sizeof (grp_op_pos1_pos2_pos3));
21424 rule_buf[rule_pos++] = grp_op_pos1_pos2_pos3[r];
21425 p1 = get_random_num (0, sizeof (grp_pos));
21426 rule_buf[rule_pos++] = grp_pos[p1];
21427 p2 = get_random_num (1, sizeof (grp_pos));
21428 rule_buf[rule_pos++] = grp_pos[p1];
21429 p3 = get_random_num (0, sizeof (grp_pos));
21430 rule_buf[rule_pos++] = grp_pos[p3];
21431 break;
21432 }
21433 }
21434
21435 return (rule_pos);
21436 }
21437
21438 int _old_apply_rule (char *rule, int rule_len, char in[BLOCK_SIZE], int in_len, char out[BLOCK_SIZE])
21439 {
21440 char mem[BLOCK_SIZE] = { 0 };
21441
21442 if (in == NULL) return (RULE_RC_REJECT_ERROR);
21443
21444 if (out == NULL) return (RULE_RC_REJECT_ERROR);
21445
21446 if (in_len < 1 || in_len > BLOCK_SIZE) return (RULE_RC_REJECT_ERROR);
21447
21448 if (rule_len < 1) return (RULE_RC_REJECT_ERROR);
21449
21450 int out_len = in_len;
21451 int mem_len = in_len;
21452
21453 memcpy (out, in, out_len);
21454
21455 int rule_pos;
21456
21457 for (rule_pos = 0; rule_pos < rule_len; rule_pos++)
21458 {
21459 int upos, upos2;
21460 int ulen;
21461
21462 switch (rule[rule_pos])
21463 {
21464 case ' ':
21465 break;
21466
21467 case RULE_OP_MANGLE_NOOP:
21468 break;
21469
21470 case RULE_OP_MANGLE_LREST:
21471 out_len = mangle_lrest (out, out_len);
21472 break;
21473
21474 case RULE_OP_MANGLE_UREST:
21475 out_len = mangle_urest (out, out_len);
21476 break;
21477
21478 case RULE_OP_MANGLE_LREST_UFIRST:
21479 out_len = mangle_lrest (out, out_len);
21480 if (out_len) MANGLE_UPPER_AT (out, 0);
21481 break;
21482
21483 case RULE_OP_MANGLE_UREST_LFIRST:
21484 out_len = mangle_urest (out, out_len);
21485 if (out_len) MANGLE_LOWER_AT (out, 0);
21486 break;
21487
21488 case RULE_OP_MANGLE_TREST:
21489 out_len = mangle_trest (out, out_len);
21490 break;
21491
21492 case RULE_OP_MANGLE_TOGGLE_AT:
21493 NEXT_RULEPOS (rule_pos);
21494 NEXT_RPTOI (rule, rule_pos, upos);
21495 if (upos < out_len) MANGLE_TOGGLE_AT (out, upos);
21496 break;
21497
21498 case RULE_OP_MANGLE_REVERSE:
21499 out_len = mangle_reverse (out, out_len);
21500 break;
21501
21502 case RULE_OP_MANGLE_DUPEWORD:
21503 out_len = mangle_double (out, out_len);
21504 break;
21505
21506 case RULE_OP_MANGLE_DUPEWORD_TIMES:
21507 NEXT_RULEPOS (rule_pos);
21508 NEXT_RPTOI (rule, rule_pos, ulen);
21509 out_len = mangle_double_times (out, out_len, ulen);
21510 break;
21511
21512 case RULE_OP_MANGLE_REFLECT:
21513 out_len = mangle_reflect (out, out_len);
21514 break;
21515
21516 case RULE_OP_MANGLE_ROTATE_LEFT:
21517 mangle_rotate_left (out, out_len);
21518 break;
21519
21520 case RULE_OP_MANGLE_ROTATE_RIGHT:
21521 mangle_rotate_right (out, out_len);
21522 break;
21523
21524 case RULE_OP_MANGLE_APPEND:
21525 NEXT_RULEPOS (rule_pos);
21526 out_len = mangle_append (out, out_len, rule[rule_pos]);
21527 break;
21528
21529 case RULE_OP_MANGLE_PREPEND:
21530 NEXT_RULEPOS (rule_pos);
21531 out_len = mangle_prepend (out, out_len, rule[rule_pos]);
21532 break;
21533
21534 case RULE_OP_MANGLE_DELETE_FIRST:
21535 out_len = mangle_delete_at (out, out_len, 0);
21536 break;
21537
21538 case RULE_OP_MANGLE_DELETE_LAST:
21539 out_len = mangle_delete_at (out, out_len, (out_len) ? out_len - 1 : 0);
21540 break;
21541
21542 case RULE_OP_MANGLE_DELETE_AT:
21543 NEXT_RULEPOS (rule_pos);
21544 NEXT_RPTOI (rule, rule_pos, upos);
21545 out_len = mangle_delete_at (out, out_len, upos);
21546 break;
21547
21548 case RULE_OP_MANGLE_EXTRACT:
21549 NEXT_RULEPOS (rule_pos);
21550 NEXT_RPTOI (rule, rule_pos, upos);
21551 NEXT_RULEPOS (rule_pos);
21552 NEXT_RPTOI (rule, rule_pos, ulen);
21553 out_len = mangle_extract (out, out_len, upos, ulen);
21554 break;
21555
21556 case RULE_OP_MANGLE_OMIT:
21557 NEXT_RULEPOS (rule_pos);
21558 NEXT_RPTOI (rule, rule_pos, upos);
21559 NEXT_RULEPOS (rule_pos);
21560 NEXT_RPTOI (rule, rule_pos, ulen);
21561 out_len = mangle_omit (out, out_len, upos, ulen);
21562 break;
21563
21564 case RULE_OP_MANGLE_INSERT:
21565 NEXT_RULEPOS (rule_pos);
21566 NEXT_RPTOI (rule, rule_pos, upos);
21567 NEXT_RULEPOS (rule_pos);
21568 out_len = mangle_insert (out, out_len, upos, rule[rule_pos]);
21569 break;
21570
21571 case RULE_OP_MANGLE_OVERSTRIKE:
21572 NEXT_RULEPOS (rule_pos);
21573 NEXT_RPTOI (rule, rule_pos, upos);
21574 NEXT_RULEPOS (rule_pos);
21575 out_len = mangle_overstrike (out, out_len, upos, rule[rule_pos]);
21576 break;
21577
21578 case RULE_OP_MANGLE_TRUNCATE_AT:
21579 NEXT_RULEPOS (rule_pos);
21580 NEXT_RPTOI (rule, rule_pos, upos);
21581 out_len = mangle_truncate_at (out, out_len, upos);
21582 break;
21583
21584 case RULE_OP_MANGLE_REPLACE:
21585 NEXT_RULEPOS (rule_pos);
21586 NEXT_RULEPOS (rule_pos);
21587 out_len = mangle_replace (out, out_len, rule[rule_pos - 1], rule[rule_pos]);
21588 break;
21589
21590 case RULE_OP_MANGLE_PURGECHAR:
21591 NEXT_RULEPOS (rule_pos);
21592 out_len = mangle_purgechar (out, out_len, rule[rule_pos]);
21593 break;
21594
21595 case RULE_OP_MANGLE_TOGGLECASE_REC:
21596 /* todo */
21597 break;
21598
21599 case RULE_OP_MANGLE_DUPECHAR_FIRST:
21600 NEXT_RULEPOS (rule_pos);
21601 NEXT_RPTOI (rule, rule_pos, ulen);
21602 out_len = mangle_dupechar_at (out, out_len, 0, ulen);
21603 break;
21604
21605 case RULE_OP_MANGLE_DUPECHAR_LAST:
21606 NEXT_RULEPOS (rule_pos);
21607 NEXT_RPTOI (rule, rule_pos, ulen);
21608 out_len = mangle_dupechar_at (out, out_len, out_len - 1, ulen);
21609 break;
21610
21611 case RULE_OP_MANGLE_DUPECHAR_ALL:
21612 out_len = mangle_dupechar (out, out_len);
21613 break;
21614
21615 case RULE_OP_MANGLE_DUPEBLOCK_FIRST:
21616 NEXT_RULEPOS (rule_pos);
21617 NEXT_RPTOI (rule, rule_pos, ulen);
21618 out_len = mangle_dupeblock_prepend (out, out_len, ulen);
21619 break;
21620
21621 case RULE_OP_MANGLE_DUPEBLOCK_LAST:
21622 NEXT_RULEPOS (rule_pos);
21623 NEXT_RPTOI (rule, rule_pos, ulen);
21624 out_len = mangle_dupeblock_append (out, out_len, ulen);
21625 break;
21626
21627 case RULE_OP_MANGLE_SWITCH_FIRST:
21628 if (out_len >= 2) mangle_switch_at (out, out_len, 0, 1);
21629 break;
21630
21631 case RULE_OP_MANGLE_SWITCH_LAST:
21632 if (out_len >= 2) mangle_switch_at (out, out_len, out_len - 1, out_len - 2);
21633 break;
21634
21635 case RULE_OP_MANGLE_SWITCH_AT:
21636 NEXT_RULEPOS (rule_pos);
21637 NEXT_RPTOI (rule, rule_pos, upos);
21638 NEXT_RULEPOS (rule_pos);
21639 NEXT_RPTOI (rule, rule_pos, upos2);
21640 out_len = mangle_switch_at_check (out, out_len, upos, upos2);
21641 break;
21642
21643 case RULE_OP_MANGLE_CHR_SHIFTL:
21644 NEXT_RULEPOS (rule_pos);
21645 NEXT_RPTOI (rule, rule_pos, upos);
21646 mangle_chr_shiftl (out, out_len, upos);
21647 break;
21648
21649 case RULE_OP_MANGLE_CHR_SHIFTR:
21650 NEXT_RULEPOS (rule_pos);
21651 NEXT_RPTOI (rule, rule_pos, upos);
21652 mangle_chr_shiftr (out, out_len, upos);
21653 break;
21654
21655 case RULE_OP_MANGLE_CHR_INCR:
21656 NEXT_RULEPOS (rule_pos);
21657 NEXT_RPTOI (rule, rule_pos, upos);
21658 mangle_chr_incr (out, out_len, upos);
21659 break;
21660
21661 case RULE_OP_MANGLE_CHR_DECR:
21662 NEXT_RULEPOS (rule_pos);
21663 NEXT_RPTOI (rule, rule_pos, upos);
21664 mangle_chr_decr (out, out_len, upos);
21665 break;
21666
21667 case RULE_OP_MANGLE_REPLACE_NP1:
21668 NEXT_RULEPOS (rule_pos);
21669 NEXT_RPTOI (rule, rule_pos, upos);
21670 if ((upos >= 0) && ((upos + 1) < out_len)) mangle_overstrike (out, out_len, upos, out[upos + 1]);
21671 break;
21672
21673 case RULE_OP_MANGLE_REPLACE_NM1:
21674 NEXT_RULEPOS (rule_pos);
21675 NEXT_RPTOI (rule, rule_pos, upos);
21676 if ((upos >= 1) && ((upos + 0) < out_len)) mangle_overstrike (out, out_len, upos, out[upos - 1]);
21677 break;
21678
21679 case RULE_OP_MANGLE_TITLE:
21680 out_len = mangle_title (out, out_len);
21681 break;
21682
21683 case RULE_OP_MANGLE_EXTRACT_MEMORY:
21684 if (mem_len < 1) return (RULE_RC_REJECT_ERROR);
21685 NEXT_RULEPOS (rule_pos);
21686 NEXT_RPTOI (rule, rule_pos, upos);
21687 NEXT_RULEPOS (rule_pos);
21688 NEXT_RPTOI (rule, rule_pos, ulen);
21689 NEXT_RULEPOS (rule_pos);
21690 NEXT_RPTOI (rule, rule_pos, upos2);
21691 if ((out_len = mangle_insert_multi (out, out_len, upos2, mem, mem_len, upos, ulen)) < 1) return (out_len);
21692 break;
21693
21694 case RULE_OP_MANGLE_APPEND_MEMORY:
21695 if (mem_len < 1) return (RULE_RC_REJECT_ERROR);
21696 if ((out_len + mem_len) > BLOCK_SIZE) return (RULE_RC_REJECT_ERROR);
21697 memcpy (out + out_len, mem, mem_len);
21698 out_len += mem_len;
21699 break;
21700
21701 case RULE_OP_MANGLE_PREPEND_MEMORY:
21702 if (mem_len < 1) return (RULE_RC_REJECT_ERROR);
21703 if ((mem_len + out_len) > BLOCK_SIZE) return (RULE_RC_REJECT_ERROR);
21704 memcpy (mem + mem_len, out, out_len);
21705 out_len += mem_len;
21706 memcpy (out, mem, out_len);
21707 break;
21708
21709 case RULE_OP_MEMORIZE_WORD:
21710 memcpy (mem, out, out_len);
21711 mem_len = out_len;
21712 break;
21713
21714 case RULE_OP_REJECT_LESS:
21715 NEXT_RULEPOS (rule_pos);
21716 NEXT_RPTOI (rule, rule_pos, upos);
21717 if (out_len > upos) return (RULE_RC_REJECT_ERROR);
21718 break;
21719
21720 case RULE_OP_REJECT_GREATER:
21721 NEXT_RULEPOS (rule_pos);
21722 NEXT_RPTOI (rule, rule_pos, upos);
21723 if (out_len < upos) return (RULE_RC_REJECT_ERROR);
21724 break;
21725
21726 case RULE_OP_REJECT_CONTAIN:
21727 NEXT_RULEPOS (rule_pos);
21728 if (strchr (out, rule[rule_pos]) != NULL) return (RULE_RC_REJECT_ERROR);
21729 break;
21730
21731 case RULE_OP_REJECT_NOT_CONTAIN:
21732 NEXT_RULEPOS (rule_pos);
21733 if (strchr (out, rule[rule_pos]) == NULL) return (RULE_RC_REJECT_ERROR);
21734 break;
21735
21736 case RULE_OP_REJECT_EQUAL_FIRST:
21737 NEXT_RULEPOS (rule_pos);
21738 if (out[0] != rule[rule_pos]) return (RULE_RC_REJECT_ERROR);
21739 break;
21740
21741 case RULE_OP_REJECT_EQUAL_LAST:
21742 NEXT_RULEPOS (rule_pos);
21743 if (out[out_len - 1] != rule[rule_pos]) return (RULE_RC_REJECT_ERROR);
21744 break;
21745
21746 case RULE_OP_REJECT_EQUAL_AT:
21747 NEXT_RULEPOS (rule_pos);
21748 NEXT_RPTOI (rule, rule_pos, upos);
21749 if ((upos + 1) > out_len) return (RULE_RC_REJECT_ERROR);
21750 NEXT_RULEPOS (rule_pos);
21751 if (out[upos] != rule[rule_pos]) return (RULE_RC_REJECT_ERROR);
21752 break;
21753
21754 case RULE_OP_REJECT_CONTAINS:
21755 NEXT_RULEPOS (rule_pos);
21756 NEXT_RPTOI (rule, rule_pos, upos);
21757 if ((upos + 1) > out_len) return (RULE_RC_REJECT_ERROR);
21758 NEXT_RULEPOS (rule_pos);
21759 int c; int cnt; for (c = 0, cnt = 0; c < out_len; c++) if (out[c] == rule[rule_pos]) cnt++;
21760 if (cnt < upos) return (RULE_RC_REJECT_ERROR);
21761 break;
21762
21763 case RULE_OP_REJECT_MEMORY:
21764 if ((out_len == mem_len) && (memcmp (out, mem, out_len) == 0)) return (RULE_RC_REJECT_ERROR);
21765 break;
21766
21767 default:
21768 return (RULE_RC_SYNTAX_ERROR);
21769 break;
21770 }
21771 }
21772
21773 memset (out + out_len, 0, BLOCK_SIZE - out_len);
21774
21775 return (out_len);
21776 }