Merge pull request #272 from philsmd/master
[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-sha256.c"
78
79 /**
80 * logging
81 */
82
83 int last_len = 0;
84
85 void log_final (FILE *fp, const char *fmt, va_list ap)
86 {
87 if (last_len)
88 {
89 fputc ('\r', fp);
90
91 for (int i = 0; i < last_len; i++)
92 {
93 fputc (' ', fp);
94 }
95
96 fputc ('\r', fp);
97 }
98
99 char s[4096] = { 0 };
100
101 int max_len = (int) sizeof (s);
102
103 int len = vsnprintf (s, max_len, fmt, ap);
104
105 if (len > max_len) len = max_len;
106
107 fwrite (s, len, 1, fp);
108
109 fflush (fp);
110
111 last_len = len;
112 }
113
114 void log_out_nn (FILE *fp, const char *fmt, ...)
115 {
116 if (SUPPRESS_OUTPUT) return;
117
118 va_list ap;
119
120 va_start (ap, fmt);
121
122 log_final (fp, fmt, ap);
123
124 va_end (ap);
125 }
126
127 void log_info_nn (const char *fmt, ...)
128 {
129 if (SUPPRESS_OUTPUT) return;
130
131 va_list ap;
132
133 va_start (ap, fmt);
134
135 log_final (stdout, fmt, ap);
136
137 va_end (ap);
138 }
139
140 void log_error_nn (const char *fmt, ...)
141 {
142 if (SUPPRESS_OUTPUT) return;
143
144 va_list ap;
145
146 va_start (ap, fmt);
147
148 log_final (stderr, fmt, ap);
149
150 va_end (ap);
151 }
152
153 void log_out (FILE *fp, const char *fmt, ...)
154 {
155 if (SUPPRESS_OUTPUT) return;
156
157 va_list ap;
158
159 va_start (ap, fmt);
160
161 log_final (fp, fmt, ap);
162
163 va_end (ap);
164
165 fputc ('\n', fp);
166
167 last_len = 0;
168 }
169
170 void log_info (const char *fmt, ...)
171 {
172 if (SUPPRESS_OUTPUT) return;
173
174 va_list ap;
175
176 va_start (ap, fmt);
177
178 log_final (stdout, fmt, ap);
179
180 va_end (ap);
181
182 fputc ('\n', stdout);
183
184 last_len = 0;
185 }
186
187 void log_error (const char *fmt, ...)
188 {
189 if (SUPPRESS_OUTPUT) return;
190
191 fputc ('\n', stderr);
192 fputc ('\n', stderr);
193
194 va_list ap;
195
196 va_start (ap, fmt);
197
198 log_final (stderr, fmt, ap);
199
200 va_end (ap);
201
202 fputc ('\n', stderr);
203 fputc ('\n', stderr);
204
205 last_len = 0;
206 }
207
208 /**
209 * converter
210 */
211
212 u8 int_to_base32 (const u8 c)
213 {
214 static const u8 tbl[0x20] =
215 {
216 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50,
217 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
218 };
219
220 return tbl[c];
221 }
222
223 u8 base32_to_int (const u8 c)
224 {
225 if ((c >= 'A') && (c <= 'Z')) return c - 'A';
226 else if ((c >= '2') && (c <= '7')) return c - '2' + 26;
227
228 return 0;
229 }
230
231 u8 int_to_itoa32 (const u8 c)
232 {
233 static const u8 tbl[0x20] =
234 {
235 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
236 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
237 };
238
239 return tbl[c];
240 }
241
242 u8 itoa32_to_int (const u8 c)
243 {
244 if ((c >= '0') && (c <= '9')) return c - '0';
245 else if ((c >= 'a') && (c <= 'v')) return c - 'a' + 10;
246
247 return 0;
248 }
249
250 u8 int_to_itoa64 (const u8 c)
251 {
252 static const u8 tbl[0x40] =
253 {
254 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44,
255 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54,
256 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a,
257 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a,
258 };
259
260 return tbl[c];
261 }
262
263 u8 itoa64_to_int (const u8 c)
264 {
265 static const u8 tbl[0x100] =
266 {
267 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21,
268 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31,
269 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x00, 0x01,
270 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
271 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a,
272 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x20, 0x21, 0x22, 0x23, 0x24,
273 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34,
274 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x00, 0x01, 0x02, 0x03, 0x04,
275 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14,
276 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24,
277 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34,
278 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x00, 0x01, 0x02, 0x03, 0x04,
279 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14,
280 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24,
281 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34,
282 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x00, 0x01, 0x02, 0x03, 0x04,
283 };
284
285 return tbl[c];
286 }
287
288 u8 int_to_base64 (const u8 c)
289 {
290 static const u8 tbl[0x40] =
291 {
292 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50,
293 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
294 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
295 0x77, 0x78, 0x79, 0x7a, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2b, 0x2f,
296 };
297
298 return tbl[c];
299 }
300
301 u8 base64_to_int (const u8 c)
302 {
303 static const u8 tbl[0x100] =
304 {
305 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
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, 0x3e, 0x00, 0x00, 0x00, 0x3f,
308 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
309 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
310 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00,
311 0x00, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
312 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00,
313 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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 };
322
323 return tbl[c];
324 }
325
326 u8 int_to_bf64 (const u8 c)
327 {
328 static const u8 tbl[0x40] =
329 {
330 0x2e, 0x2f, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e,
331 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x61, 0x62, 0x63, 0x64,
332 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74,
333 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
334 };
335
336 return tbl[c];
337 }
338
339 u8 bf64_to_int (const u8 c)
340 {
341 static const u8 tbl[0x100] =
342 {
343 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
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, 0x01,
346 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
347 0x00, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
348 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x00, 0x00, 0x00, 0x00, 0x00,
349 0x00, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a,
350 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x00, 0x00, 0x00, 0x00, 0x00,
351 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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 };
360
361 return tbl[c];
362 }
363
364 u8 int_to_lotus64 (const u8 c)
365 {
366 if (c < 10) return '0' + c;
367 else if (c < 36) return 'A' + c - 10;
368 else if (c < 62) return 'a' + c - 36;
369 else if (c == 62) return '+';
370 else if (c == 63) return '/';
371
372 return 0;
373 }
374
375 u8 lotus64_to_int (const u8 c)
376 {
377 if ((c >= '0') && (c <= '9')) return c - '0';
378 else if ((c >= 'A') && (c <= 'Z')) return c - 'A' + 10;
379 else if ((c >= 'a') && (c <= 'z')) return c - 'a' + 36;
380 else if (c == '+') return 62;
381 else if (c == '/') return 63;
382 else
383
384 return 0;
385 }
386
387 int base32_decode (u8 (*f) (const u8), const u8 *in_buf, int in_len, u8 *out_buf)
388 {
389 const u8 *in_ptr = in_buf;
390
391 u8 *out_ptr = out_buf;
392
393 for (int i = 0; i < in_len; i += 8)
394 {
395 const u8 out_val0 = f (in_ptr[0] & 0x7f);
396 const u8 out_val1 = f (in_ptr[1] & 0x7f);
397 const u8 out_val2 = f (in_ptr[2] & 0x7f);
398 const u8 out_val3 = f (in_ptr[3] & 0x7f);
399 const u8 out_val4 = f (in_ptr[4] & 0x7f);
400 const u8 out_val5 = f (in_ptr[5] & 0x7f);
401 const u8 out_val6 = f (in_ptr[6] & 0x7f);
402 const u8 out_val7 = f (in_ptr[7] & 0x7f);
403
404 out_ptr[0] = ((out_val0 << 3) & 0xf8) | ((out_val1 >> 2) & 0x07);
405 out_ptr[1] = ((out_val1 << 6) & 0xc0) | ((out_val2 << 1) & 0x3e) | ((out_val3 >> 4) & 0x01);
406 out_ptr[2] = ((out_val3 << 4) & 0xf0) | ((out_val4 >> 1) & 0x0f);
407 out_ptr[3] = ((out_val4 << 7) & 0x80) | ((out_val5 << 2) & 0x7c) | ((out_val6 >> 3) & 0x03);
408 out_ptr[4] = ((out_val6 << 5) & 0xe0) | ((out_val7 >> 0) & 0x1f);
409
410 in_ptr += 8;
411 out_ptr += 5;
412 }
413
414 for (int i = 0; i < in_len; i++)
415 {
416 if (in_buf[i] != '=') continue;
417
418 in_len = i;
419 }
420
421 int out_len = (in_len * 5) / 8;
422
423 return out_len;
424 }
425
426 int base32_encode (u8 (*f) (const u8), const u8 *in_buf, int in_len, u8 *out_buf)
427 {
428 const u8 *in_ptr = in_buf;
429
430 u8 *out_ptr = out_buf;
431
432 for (int i = 0; i < in_len; i += 5)
433 {
434 const u8 out_val0 = f ( ((in_ptr[0] >> 3) & 0x1f));
435 const u8 out_val1 = f (((in_ptr[0] << 2) & 0x1c) | ((in_ptr[1] >> 6) & 0x03));
436 const u8 out_val2 = f ( ((in_ptr[1] >> 1) & 0x1f));
437 const u8 out_val3 = f (((in_ptr[1] << 4) & 0x10) | ((in_ptr[2] >> 4) & 0x0f));
438 const u8 out_val4 = f (((in_ptr[2] << 1) & 0x1e) | ((in_ptr[3] >> 7) & 0x01));
439 const u8 out_val5 = f ( ((in_ptr[3] >> 2) & 0x1f));
440 const u8 out_val6 = f (((in_ptr[3] << 3) & 0x18) | ((in_ptr[4] >> 5) & 0x07));
441 const u8 out_val7 = f ( ((in_ptr[4] >> 0) & 0x1f));
442
443 out_ptr[0] = out_val0 & 0x7f;
444 out_ptr[1] = out_val1 & 0x7f;
445 out_ptr[2] = out_val2 & 0x7f;
446 out_ptr[3] = out_val3 & 0x7f;
447 out_ptr[4] = out_val4 & 0x7f;
448 out_ptr[5] = out_val5 & 0x7f;
449 out_ptr[6] = out_val6 & 0x7f;
450 out_ptr[7] = out_val7 & 0x7f;
451
452 in_ptr += 5;
453 out_ptr += 8;
454 }
455
456 int out_len = (int) (((0.5 + (float) in_len) * 8) / 5); // ceil (in_len * 8 / 5)
457
458 while (out_len % 8)
459 {
460 out_buf[out_len] = '=';
461
462 out_len++;
463 }
464
465 return out_len;
466 }
467
468 int base64_decode (u8 (*f) (const u8), const u8 *in_buf, int in_len, u8 *out_buf)
469 {
470 const u8 *in_ptr = in_buf;
471
472 u8 *out_ptr = out_buf;
473
474 for (int i = 0; i < in_len; i += 4)
475 {
476 const u8 out_val0 = f (in_ptr[0] & 0x7f);
477 const u8 out_val1 = f (in_ptr[1] & 0x7f);
478 const u8 out_val2 = f (in_ptr[2] & 0x7f);
479 const u8 out_val3 = f (in_ptr[3] & 0x7f);
480
481 out_ptr[0] = ((out_val0 << 2) & 0xfc) | ((out_val1 >> 4) & 0x03);
482 out_ptr[1] = ((out_val1 << 4) & 0xf0) | ((out_val2 >> 2) & 0x0f);
483 out_ptr[2] = ((out_val2 << 6) & 0xc0) | ((out_val3 >> 0) & 0x3f);
484
485 in_ptr += 4;
486 out_ptr += 3;
487 }
488
489 for (int i = 0; i < in_len; i++)
490 {
491 if (in_buf[i] != '=') continue;
492
493 in_len = i;
494 }
495
496 int out_len = (in_len * 6) / 8;
497
498 return out_len;
499 }
500
501 int base64_encode (u8 (*f) (const u8), const u8 *in_buf, int in_len, u8 *out_buf)
502 {
503 const u8 *in_ptr = in_buf;
504
505 u8 *out_ptr = out_buf;
506
507 for (int i = 0; i < in_len; i += 3)
508 {
509 const u8 out_val0 = f ( ((in_ptr[0] >> 2) & 0x3f));
510 const u8 out_val1 = f (((in_ptr[0] << 4) & 0x30) | ((in_ptr[1] >> 4) & 0x0f));
511 const u8 out_val2 = f (((in_ptr[1] << 2) & 0x3c) | ((in_ptr[2] >> 6) & 0x03));
512 const u8 out_val3 = f ( ((in_ptr[2] >> 0) & 0x3f));
513
514 out_ptr[0] = out_val0 & 0x7f;
515 out_ptr[1] = out_val1 & 0x7f;
516 out_ptr[2] = out_val2 & 0x7f;
517 out_ptr[3] = out_val3 & 0x7f;
518
519 in_ptr += 3;
520 out_ptr += 4;
521 }
522
523 int out_len = (int) (((0.5 + (float) in_len) * 8) / 6); // ceil (in_len * 8 / 6)
524
525 while (out_len % 4)
526 {
527 out_buf[out_len] = '=';
528
529 out_len++;
530 }
531
532 return out_len;
533 }
534
535 int is_valid_hex_char (const u8 c)
536 {
537 if ((c >= '0') && (c <= '9')) return 1;
538 if ((c >= 'A') && (c <= 'F')) return 1;
539 if ((c >= 'a') && (c <= 'f')) return 1;
540
541 return 0;
542 }
543
544 u8 hex_convert (const u8 c)
545 {
546 return (c & 15) + (c >> 6) * 9;
547 }
548
549 u8 hex_to_u8 (const u8 hex[2])
550 {
551 u8 v = 0;
552
553 v |= (hex_convert (hex[1]) << 0);
554 v |= (hex_convert (hex[0]) << 4);
555
556 return (v);
557 }
558
559 u32 hex_to_u32 (const u8 hex[8])
560 {
561 u32 v = 0;
562
563 v |= ((u32) hex_convert (hex[7])) << 0;
564 v |= ((u32) hex_convert (hex[6])) << 4;
565 v |= ((u32) hex_convert (hex[5])) << 8;
566 v |= ((u32) hex_convert (hex[4])) << 12;
567 v |= ((u32) hex_convert (hex[3])) << 16;
568 v |= ((u32) hex_convert (hex[2])) << 20;
569 v |= ((u32) hex_convert (hex[1])) << 24;
570 v |= ((u32) hex_convert (hex[0])) << 28;
571
572 return (v);
573 }
574
575 u64 hex_to_u64 (const u8 hex[16])
576 {
577 u64 v = 0;
578
579 v |= ((u64) hex_convert (hex[15]) << 0);
580 v |= ((u64) hex_convert (hex[14]) << 4);
581 v |= ((u64) hex_convert (hex[13]) << 8);
582 v |= ((u64) hex_convert (hex[12]) << 12);
583 v |= ((u64) hex_convert (hex[11]) << 16);
584 v |= ((u64) hex_convert (hex[10]) << 20);
585 v |= ((u64) hex_convert (hex[ 9]) << 24);
586 v |= ((u64) hex_convert (hex[ 8]) << 28);
587 v |= ((u64) hex_convert (hex[ 7]) << 32);
588 v |= ((u64) hex_convert (hex[ 6]) << 36);
589 v |= ((u64) hex_convert (hex[ 5]) << 40);
590 v |= ((u64) hex_convert (hex[ 4]) << 44);
591 v |= ((u64) hex_convert (hex[ 3]) << 48);
592 v |= ((u64) hex_convert (hex[ 2]) << 52);
593 v |= ((u64) hex_convert (hex[ 1]) << 56);
594 v |= ((u64) hex_convert (hex[ 0]) << 60);
595
596 return (v);
597 }
598
599 void bin_to_hex_lower (const u32 v, u8 hex[8])
600 {
601 hex[0] = v >> 28 & 15;
602 hex[1] = v >> 24 & 15;
603 hex[2] = v >> 20 & 15;
604 hex[3] = v >> 16 & 15;
605 hex[4] = v >> 12 & 15;
606 hex[5] = v >> 8 & 15;
607 hex[6] = v >> 4 & 15;
608 hex[7] = v >> 0 & 15;
609
610 u32 add;
611
612 hex[0] += 6; add = ((hex[0] & 0x10) >> 4) * 39; hex[0] += 42 + add;
613 hex[1] += 6; add = ((hex[1] & 0x10) >> 4) * 39; hex[1] += 42 + add;
614 hex[2] += 6; add = ((hex[2] & 0x10) >> 4) * 39; hex[2] += 42 + add;
615 hex[3] += 6; add = ((hex[3] & 0x10) >> 4) * 39; hex[3] += 42 + add;
616 hex[4] += 6; add = ((hex[4] & 0x10) >> 4) * 39; hex[4] += 42 + add;
617 hex[5] += 6; add = ((hex[5] & 0x10) >> 4) * 39; hex[5] += 42 + add;
618 hex[6] += 6; add = ((hex[6] & 0x10) >> 4) * 39; hex[6] += 42 + add;
619 hex[7] += 6; add = ((hex[7] & 0x10) >> 4) * 39; hex[7] += 42 + add;
620 }
621
622 /**
623 * decoder
624 */
625
626 static void AES128_decrypt_cbc (const u32 key[4], const u32 iv[4], const u32 in[16], u32 out[16])
627 {
628 AES_KEY skey;
629
630 AES_set_decrypt_key ((const u8 *) key, 128, &skey);
631
632 u32 _iv[4] = { 0 };
633
634 _iv[0] = iv[0];
635 _iv[1] = iv[1];
636 _iv[2] = iv[2];
637 _iv[3] = iv[3];
638
639 for (int i = 0; i < 16; i += 4)
640 {
641 u32 _in[4] = { 0 };
642 u32 _out[4] = { 0 };
643
644 _in[0] = in[i + 0];
645 _in[1] = in[i + 1];
646 _in[2] = in[i + 2];
647 _in[3] = in[i + 3];
648
649 AES_decrypt (&skey, (const u8 *) _in, (u8 *) _out);
650
651 _out[0] ^= _iv[0];
652 _out[1] ^= _iv[1];
653 _out[2] ^= _iv[2];
654 _out[3] ^= _iv[3];
655
656 out[i + 0] = _out[0];
657 out[i + 1] = _out[1];
658 out[i + 2] = _out[2];
659 out[i + 3] = _out[3];
660
661 _iv[0] = _in[0];
662 _iv[1] = _in[1];
663 _iv[2] = _in[2];
664 _iv[3] = _in[3];
665 }
666 }
667
668 static void juniper_decrypt_hash (char *in, char *out)
669 {
670 // base64 decode
671
672 u8 base64_buf[100] = { 0 };
673
674 base64_decode (base64_to_int, (const u8 *) in, DISPLAY_LEN_MIN_501, base64_buf);
675
676 // iv stuff
677
678 u32 juniper_iv[4] = { 0 };
679
680 memcpy (juniper_iv, base64_buf, 12);
681
682 memcpy (out, juniper_iv, 12);
683
684 // reversed key
685
686 u32 juniper_key[4] = { 0 };
687
688 juniper_key[0] = byte_swap_32 (0xa6707a7e);
689 juniper_key[1] = byte_swap_32 (0x8df91059);
690 juniper_key[2] = byte_swap_32 (0xdea70ae5);
691 juniper_key[3] = byte_swap_32 (0x2f9c2442);
692
693 // AES decrypt
694
695 u32 *in_ptr = (u32 *) (base64_buf + 12);
696 u32 *out_ptr = (u32 *) (out + 12);
697
698 AES128_decrypt_cbc (juniper_key, juniper_iv, in_ptr, out_ptr);
699 }
700
701 void phpass_decode (u8 digest[16], u8 buf[22])
702 {
703 int l;
704
705 l = itoa64_to_int (buf[ 0]) << 0;
706 l |= itoa64_to_int (buf[ 1]) << 6;
707 l |= itoa64_to_int (buf[ 2]) << 12;
708 l |= itoa64_to_int (buf[ 3]) << 18;
709
710 digest[ 0] = (l >> 0) & 0xff;
711 digest[ 1] = (l >> 8) & 0xff;
712 digest[ 2] = (l >> 16) & 0xff;
713
714 l = itoa64_to_int (buf[ 4]) << 0;
715 l |= itoa64_to_int (buf[ 5]) << 6;
716 l |= itoa64_to_int (buf[ 6]) << 12;
717 l |= itoa64_to_int (buf[ 7]) << 18;
718
719 digest[ 3] = (l >> 0) & 0xff;
720 digest[ 4] = (l >> 8) & 0xff;
721 digest[ 5] = (l >> 16) & 0xff;
722
723 l = itoa64_to_int (buf[ 8]) << 0;
724 l |= itoa64_to_int (buf[ 9]) << 6;
725 l |= itoa64_to_int (buf[10]) << 12;
726 l |= itoa64_to_int (buf[11]) << 18;
727
728 digest[ 6] = (l >> 0) & 0xff;
729 digest[ 7] = (l >> 8) & 0xff;
730 digest[ 8] = (l >> 16) & 0xff;
731
732 l = itoa64_to_int (buf[12]) << 0;
733 l |= itoa64_to_int (buf[13]) << 6;
734 l |= itoa64_to_int (buf[14]) << 12;
735 l |= itoa64_to_int (buf[15]) << 18;
736
737 digest[ 9] = (l >> 0) & 0xff;
738 digest[10] = (l >> 8) & 0xff;
739 digest[11] = (l >> 16) & 0xff;
740
741 l = itoa64_to_int (buf[16]) << 0;
742 l |= itoa64_to_int (buf[17]) << 6;
743 l |= itoa64_to_int (buf[18]) << 12;
744 l |= itoa64_to_int (buf[19]) << 18;
745
746 digest[12] = (l >> 0) & 0xff;
747 digest[13] = (l >> 8) & 0xff;
748 digest[14] = (l >> 16) & 0xff;
749
750 l = itoa64_to_int (buf[20]) << 0;
751 l |= itoa64_to_int (buf[21]) << 6;
752
753 digest[15] = (l >> 0) & 0xff;
754 }
755
756 void phpass_encode (u8 digest[16], u8 buf[22])
757 {
758 int l;
759
760 l = (digest[ 0] << 0) | (digest[ 1] << 8) | (digest[ 2] << 16);
761
762 buf[ 0] = int_to_itoa64 (l & 0x3f); l >>= 6;
763 buf[ 1] = int_to_itoa64 (l & 0x3f); l >>= 6;
764 buf[ 2] = int_to_itoa64 (l & 0x3f); l >>= 6;
765 buf[ 3] = int_to_itoa64 (l & 0x3f);
766
767 l = (digest[ 3] << 0) | (digest[ 4] << 8) | (digest[ 5] << 16);
768
769 buf[ 4] = int_to_itoa64 (l & 0x3f); l >>= 6;
770 buf[ 5] = int_to_itoa64 (l & 0x3f); l >>= 6;
771 buf[ 6] = int_to_itoa64 (l & 0x3f); l >>= 6;
772 buf[ 7] = int_to_itoa64 (l & 0x3f);
773
774 l = (digest[ 6] << 0) | (digest[ 7] << 8) | (digest[ 8] << 16);
775
776 buf[ 8] = int_to_itoa64 (l & 0x3f); l >>= 6;
777 buf[ 9] = int_to_itoa64 (l & 0x3f); l >>= 6;
778 buf[10] = int_to_itoa64 (l & 0x3f); l >>= 6;
779 buf[11] = int_to_itoa64 (l & 0x3f);
780
781 l = (digest[ 9] << 0) | (digest[10] << 8) | (digest[11] << 16);
782
783 buf[12] = int_to_itoa64 (l & 0x3f); l >>= 6;
784 buf[13] = int_to_itoa64 (l & 0x3f); l >>= 6;
785 buf[14] = int_to_itoa64 (l & 0x3f); l >>= 6;
786 buf[15] = int_to_itoa64 (l & 0x3f);
787
788 l = (digest[12] << 0) | (digest[13] << 8) | (digest[14] << 16);
789
790 buf[16] = int_to_itoa64 (l & 0x3f); l >>= 6;
791 buf[17] = int_to_itoa64 (l & 0x3f); l >>= 6;
792 buf[18] = int_to_itoa64 (l & 0x3f); l >>= 6;
793 buf[19] = int_to_itoa64 (l & 0x3f);
794
795 l = (digest[15] << 0);
796
797 buf[20] = int_to_itoa64 (l & 0x3f); l >>= 6;
798 buf[21] = int_to_itoa64 (l & 0x3f);
799 }
800
801 void md5crypt_decode (u8 digest[16], u8 buf[22])
802 {
803 int l;
804
805 l = itoa64_to_int (buf[ 0]) << 0;
806 l |= itoa64_to_int (buf[ 1]) << 6;
807 l |= itoa64_to_int (buf[ 2]) << 12;
808 l |= itoa64_to_int (buf[ 3]) << 18;
809
810 digest[ 0] = (l >> 16) & 0xff;
811 digest[ 6] = (l >> 8) & 0xff;
812 digest[12] = (l >> 0) & 0xff;
813
814 l = itoa64_to_int (buf[ 4]) << 0;
815 l |= itoa64_to_int (buf[ 5]) << 6;
816 l |= itoa64_to_int (buf[ 6]) << 12;
817 l |= itoa64_to_int (buf[ 7]) << 18;
818
819 digest[ 1] = (l >> 16) & 0xff;
820 digest[ 7] = (l >> 8) & 0xff;
821 digest[13] = (l >> 0) & 0xff;
822
823 l = itoa64_to_int (buf[ 8]) << 0;
824 l |= itoa64_to_int (buf[ 9]) << 6;
825 l |= itoa64_to_int (buf[10]) << 12;
826 l |= itoa64_to_int (buf[11]) << 18;
827
828 digest[ 2] = (l >> 16) & 0xff;
829 digest[ 8] = (l >> 8) & 0xff;
830 digest[14] = (l >> 0) & 0xff;
831
832 l = itoa64_to_int (buf[12]) << 0;
833 l |= itoa64_to_int (buf[13]) << 6;
834 l |= itoa64_to_int (buf[14]) << 12;
835 l |= itoa64_to_int (buf[15]) << 18;
836
837 digest[ 3] = (l >> 16) & 0xff;
838 digest[ 9] = (l >> 8) & 0xff;
839 digest[15] = (l >> 0) & 0xff;
840
841 l = itoa64_to_int (buf[16]) << 0;
842 l |= itoa64_to_int (buf[17]) << 6;
843 l |= itoa64_to_int (buf[18]) << 12;
844 l |= itoa64_to_int (buf[19]) << 18;
845
846 digest[ 4] = (l >> 16) & 0xff;
847 digest[10] = (l >> 8) & 0xff;
848 digest[ 5] = (l >> 0) & 0xff;
849
850 l = itoa64_to_int (buf[20]) << 0;
851 l |= itoa64_to_int (buf[21]) << 6;
852
853 digest[11] = (l >> 0) & 0xff;
854 }
855
856 void md5crypt_encode (u8 digest[16], u8 buf[22])
857 {
858 int l;
859
860 l = (digest[ 0] << 16) | (digest[ 6] << 8) | (digest[12] << 0);
861
862 buf[ 0] = int_to_itoa64 (l & 0x3f); l >>= 6;
863 buf[ 1] = int_to_itoa64 (l & 0x3f); l >>= 6;
864 buf[ 2] = int_to_itoa64 (l & 0x3f); l >>= 6;
865 buf[ 3] = int_to_itoa64 (l & 0x3f); l >>= 6;
866
867 l = (digest[ 1] << 16) | (digest[ 7] << 8) | (digest[13] << 0);
868
869 buf[ 4] = int_to_itoa64 (l & 0x3f); l >>= 6;
870 buf[ 5] = int_to_itoa64 (l & 0x3f); l >>= 6;
871 buf[ 6] = int_to_itoa64 (l & 0x3f); l >>= 6;
872 buf[ 7] = int_to_itoa64 (l & 0x3f); l >>= 6;
873
874 l = (digest[ 2] << 16) | (digest[ 8] << 8) | (digest[14] << 0);
875
876 buf[ 8] = int_to_itoa64 (l & 0x3f); l >>= 6;
877 buf[ 9] = int_to_itoa64 (l & 0x3f); l >>= 6;
878 buf[10] = int_to_itoa64 (l & 0x3f); l >>= 6;
879 buf[11] = int_to_itoa64 (l & 0x3f); l >>= 6;
880
881 l = (digest[ 3] << 16) | (digest[ 9] << 8) | (digest[15] << 0);
882
883 buf[12] = int_to_itoa64 (l & 0x3f); l >>= 6;
884 buf[13] = int_to_itoa64 (l & 0x3f); l >>= 6;
885 buf[14] = int_to_itoa64 (l & 0x3f); l >>= 6;
886 buf[15] = int_to_itoa64 (l & 0x3f); l >>= 6;
887
888 l = (digest[ 4] << 16) | (digest[10] << 8) | (digest[ 5] << 0);
889
890 buf[16] = int_to_itoa64 (l & 0x3f); l >>= 6;
891 buf[17] = int_to_itoa64 (l & 0x3f); l >>= 6;
892 buf[18] = int_to_itoa64 (l & 0x3f); l >>= 6;
893 buf[19] = int_to_itoa64 (l & 0x3f); l >>= 6;
894
895 l = (digest[11] << 0);
896
897 buf[20] = int_to_itoa64 (l & 0x3f); l >>= 6;
898 buf[21] = int_to_itoa64 (l & 0x3f); l >>= 6;
899 }
900
901 void sha512crypt_decode (u8 digest[64], u8 buf[86])
902 {
903 int l;
904
905 l = itoa64_to_int (buf[ 0]) << 0;
906 l |= itoa64_to_int (buf[ 1]) << 6;
907 l |= itoa64_to_int (buf[ 2]) << 12;
908 l |= itoa64_to_int (buf[ 3]) << 18;
909
910 digest[ 0] = (l >> 16) & 0xff;
911 digest[21] = (l >> 8) & 0xff;
912 digest[42] = (l >> 0) & 0xff;
913
914 l = itoa64_to_int (buf[ 4]) << 0;
915 l |= itoa64_to_int (buf[ 5]) << 6;
916 l |= itoa64_to_int (buf[ 6]) << 12;
917 l |= itoa64_to_int (buf[ 7]) << 18;
918
919 digest[22] = (l >> 16) & 0xff;
920 digest[43] = (l >> 8) & 0xff;
921 digest[ 1] = (l >> 0) & 0xff;
922
923 l = itoa64_to_int (buf[ 8]) << 0;
924 l |= itoa64_to_int (buf[ 9]) << 6;
925 l |= itoa64_to_int (buf[10]) << 12;
926 l |= itoa64_to_int (buf[11]) << 18;
927
928 digest[44] = (l >> 16) & 0xff;
929 digest[ 2] = (l >> 8) & 0xff;
930 digest[23] = (l >> 0) & 0xff;
931
932 l = itoa64_to_int (buf[12]) << 0;
933 l |= itoa64_to_int (buf[13]) << 6;
934 l |= itoa64_to_int (buf[14]) << 12;
935 l |= itoa64_to_int (buf[15]) << 18;
936
937 digest[ 3] = (l >> 16) & 0xff;
938 digest[24] = (l >> 8) & 0xff;
939 digest[45] = (l >> 0) & 0xff;
940
941 l = itoa64_to_int (buf[16]) << 0;
942 l |= itoa64_to_int (buf[17]) << 6;
943 l |= itoa64_to_int (buf[18]) << 12;
944 l |= itoa64_to_int (buf[19]) << 18;
945
946 digest[25] = (l >> 16) & 0xff;
947 digest[46] = (l >> 8) & 0xff;
948 digest[ 4] = (l >> 0) & 0xff;
949
950 l = itoa64_to_int (buf[20]) << 0;
951 l |= itoa64_to_int (buf[21]) << 6;
952 l |= itoa64_to_int (buf[22]) << 12;
953 l |= itoa64_to_int (buf[23]) << 18;
954
955 digest[47] = (l >> 16) & 0xff;
956 digest[ 5] = (l >> 8) & 0xff;
957 digest[26] = (l >> 0) & 0xff;
958
959 l = itoa64_to_int (buf[24]) << 0;
960 l |= itoa64_to_int (buf[25]) << 6;
961 l |= itoa64_to_int (buf[26]) << 12;
962 l |= itoa64_to_int (buf[27]) << 18;
963
964 digest[ 6] = (l >> 16) & 0xff;
965 digest[27] = (l >> 8) & 0xff;
966 digest[48] = (l >> 0) & 0xff;
967
968 l = itoa64_to_int (buf[28]) << 0;
969 l |= itoa64_to_int (buf[29]) << 6;
970 l |= itoa64_to_int (buf[30]) << 12;
971 l |= itoa64_to_int (buf[31]) << 18;
972
973 digest[28] = (l >> 16) & 0xff;
974 digest[49] = (l >> 8) & 0xff;
975 digest[ 7] = (l >> 0) & 0xff;
976
977 l = itoa64_to_int (buf[32]) << 0;
978 l |= itoa64_to_int (buf[33]) << 6;
979 l |= itoa64_to_int (buf[34]) << 12;
980 l |= itoa64_to_int (buf[35]) << 18;
981
982 digest[50] = (l >> 16) & 0xff;
983 digest[ 8] = (l >> 8) & 0xff;
984 digest[29] = (l >> 0) & 0xff;
985
986 l = itoa64_to_int (buf[36]) << 0;
987 l |= itoa64_to_int (buf[37]) << 6;
988 l |= itoa64_to_int (buf[38]) << 12;
989 l |= itoa64_to_int (buf[39]) << 18;
990
991 digest[ 9] = (l >> 16) & 0xff;
992 digest[30] = (l >> 8) & 0xff;
993 digest[51] = (l >> 0) & 0xff;
994
995 l = itoa64_to_int (buf[40]) << 0;
996 l |= itoa64_to_int (buf[41]) << 6;
997 l |= itoa64_to_int (buf[42]) << 12;
998 l |= itoa64_to_int (buf[43]) << 18;
999
1000 digest[31] = (l >> 16) & 0xff;
1001 digest[52] = (l >> 8) & 0xff;
1002 digest[10] = (l >> 0) & 0xff;
1003
1004 l = itoa64_to_int (buf[44]) << 0;
1005 l |= itoa64_to_int (buf[45]) << 6;
1006 l |= itoa64_to_int (buf[46]) << 12;
1007 l |= itoa64_to_int (buf[47]) << 18;
1008
1009 digest[53] = (l >> 16) & 0xff;
1010 digest[11] = (l >> 8) & 0xff;
1011 digest[32] = (l >> 0) & 0xff;
1012
1013 l = itoa64_to_int (buf[48]) << 0;
1014 l |= itoa64_to_int (buf[49]) << 6;
1015 l |= itoa64_to_int (buf[50]) << 12;
1016 l |= itoa64_to_int (buf[51]) << 18;
1017
1018 digest[12] = (l >> 16) & 0xff;
1019 digest[33] = (l >> 8) & 0xff;
1020 digest[54] = (l >> 0) & 0xff;
1021
1022 l = itoa64_to_int (buf[52]) << 0;
1023 l |= itoa64_to_int (buf[53]) << 6;
1024 l |= itoa64_to_int (buf[54]) << 12;
1025 l |= itoa64_to_int (buf[55]) << 18;
1026
1027 digest[34] = (l >> 16) & 0xff;
1028 digest[55] = (l >> 8) & 0xff;
1029 digest[13] = (l >> 0) & 0xff;
1030
1031 l = itoa64_to_int (buf[56]) << 0;
1032 l |= itoa64_to_int (buf[57]) << 6;
1033 l |= itoa64_to_int (buf[58]) << 12;
1034 l |= itoa64_to_int (buf[59]) << 18;
1035
1036 digest[56] = (l >> 16) & 0xff;
1037 digest[14] = (l >> 8) & 0xff;
1038 digest[35] = (l >> 0) & 0xff;
1039
1040 l = itoa64_to_int (buf[60]) << 0;
1041 l |= itoa64_to_int (buf[61]) << 6;
1042 l |= itoa64_to_int (buf[62]) << 12;
1043 l |= itoa64_to_int (buf[63]) << 18;
1044
1045 digest[15] = (l >> 16) & 0xff;
1046 digest[36] = (l >> 8) & 0xff;
1047 digest[57] = (l >> 0) & 0xff;
1048
1049 l = itoa64_to_int (buf[64]) << 0;
1050 l |= itoa64_to_int (buf[65]) << 6;
1051 l |= itoa64_to_int (buf[66]) << 12;
1052 l |= itoa64_to_int (buf[67]) << 18;
1053
1054 digest[37] = (l >> 16) & 0xff;
1055 digest[58] = (l >> 8) & 0xff;
1056 digest[16] = (l >> 0) & 0xff;
1057
1058 l = itoa64_to_int (buf[68]) << 0;
1059 l |= itoa64_to_int (buf[69]) << 6;
1060 l |= itoa64_to_int (buf[70]) << 12;
1061 l |= itoa64_to_int (buf[71]) << 18;
1062
1063 digest[59] = (l >> 16) & 0xff;
1064 digest[17] = (l >> 8) & 0xff;
1065 digest[38] = (l >> 0) & 0xff;
1066
1067 l = itoa64_to_int (buf[72]) << 0;
1068 l |= itoa64_to_int (buf[73]) << 6;
1069 l |= itoa64_to_int (buf[74]) << 12;
1070 l |= itoa64_to_int (buf[75]) << 18;
1071
1072 digest[18] = (l >> 16) & 0xff;
1073 digest[39] = (l >> 8) & 0xff;
1074 digest[60] = (l >> 0) & 0xff;
1075
1076 l = itoa64_to_int (buf[76]) << 0;
1077 l |= itoa64_to_int (buf[77]) << 6;
1078 l |= itoa64_to_int (buf[78]) << 12;
1079 l |= itoa64_to_int (buf[79]) << 18;
1080
1081 digest[40] = (l >> 16) & 0xff;
1082 digest[61] = (l >> 8) & 0xff;
1083 digest[19] = (l >> 0) & 0xff;
1084
1085 l = itoa64_to_int (buf[80]) << 0;
1086 l |= itoa64_to_int (buf[81]) << 6;
1087 l |= itoa64_to_int (buf[82]) << 12;
1088 l |= itoa64_to_int (buf[83]) << 18;
1089
1090 digest[62] = (l >> 16) & 0xff;
1091 digest[20] = (l >> 8) & 0xff;
1092 digest[41] = (l >> 0) & 0xff;
1093
1094 l = itoa64_to_int (buf[84]) << 0;
1095 l |= itoa64_to_int (buf[85]) << 6;
1096
1097 digest[63] = (l >> 0) & 0xff;
1098 }
1099
1100 void sha512crypt_encode (u8 digest[64], u8 buf[86])
1101 {
1102 int l;
1103
1104 l = (digest[ 0] << 16) | (digest[21] << 8) | (digest[42] << 0);
1105
1106 buf[ 0] = int_to_itoa64 (l & 0x3f); l >>= 6;
1107 buf[ 1] = int_to_itoa64 (l & 0x3f); l >>= 6;
1108 buf[ 2] = int_to_itoa64 (l & 0x3f); l >>= 6;
1109 buf[ 3] = int_to_itoa64 (l & 0x3f); l >>= 6;
1110
1111 l = (digest[22] << 16) | (digest[43] << 8) | (digest[ 1] << 0);
1112
1113 buf[ 4] = int_to_itoa64 (l & 0x3f); l >>= 6;
1114 buf[ 5] = int_to_itoa64 (l & 0x3f); l >>= 6;
1115 buf[ 6] = int_to_itoa64 (l & 0x3f); l >>= 6;
1116 buf[ 7] = int_to_itoa64 (l & 0x3f); l >>= 6;
1117
1118 l = (digest[44] << 16) | (digest[ 2] << 8) | (digest[23] << 0);
1119
1120 buf[ 8] = int_to_itoa64 (l & 0x3f); l >>= 6;
1121 buf[ 9] = int_to_itoa64 (l & 0x3f); l >>= 6;
1122 buf[10] = int_to_itoa64 (l & 0x3f); l >>= 6;
1123 buf[11] = int_to_itoa64 (l & 0x3f); l >>= 6;
1124
1125 l = (digest[ 3] << 16) | (digest[24] << 8) | (digest[45] << 0);
1126
1127 buf[12] = int_to_itoa64 (l & 0x3f); l >>= 6;
1128 buf[13] = int_to_itoa64 (l & 0x3f); l >>= 6;
1129 buf[14] = int_to_itoa64 (l & 0x3f); l >>= 6;
1130 buf[15] = int_to_itoa64 (l & 0x3f); l >>= 6;
1131
1132 l = (digest[25] << 16) | (digest[46] << 8) | (digest[ 4] << 0);
1133
1134 buf[16] = int_to_itoa64 (l & 0x3f); l >>= 6;
1135 buf[17] = int_to_itoa64 (l & 0x3f); l >>= 6;
1136 buf[18] = int_to_itoa64 (l & 0x3f); l >>= 6;
1137 buf[19] = int_to_itoa64 (l & 0x3f); l >>= 6;
1138
1139 l = (digest[47] << 16) | (digest[ 5] << 8) | (digest[26] << 0);
1140
1141 buf[20] = int_to_itoa64 (l & 0x3f); l >>= 6;
1142 buf[21] = int_to_itoa64 (l & 0x3f); l >>= 6;
1143 buf[22] = int_to_itoa64 (l & 0x3f); l >>= 6;
1144 buf[23] = int_to_itoa64 (l & 0x3f); l >>= 6;
1145
1146 l = (digest[ 6] << 16) | (digest[27] << 8) | (digest[48] << 0);
1147
1148 buf[24] = int_to_itoa64 (l & 0x3f); l >>= 6;
1149 buf[25] = int_to_itoa64 (l & 0x3f); l >>= 6;
1150 buf[26] = int_to_itoa64 (l & 0x3f); l >>= 6;
1151 buf[27] = int_to_itoa64 (l & 0x3f); l >>= 6;
1152
1153 l = (digest[28] << 16) | (digest[49] << 8) | (digest[ 7] << 0);
1154
1155 buf[28] = int_to_itoa64 (l & 0x3f); l >>= 6;
1156 buf[29] = int_to_itoa64 (l & 0x3f); l >>= 6;
1157 buf[30] = int_to_itoa64 (l & 0x3f); l >>= 6;
1158 buf[31] = int_to_itoa64 (l & 0x3f); l >>= 6;
1159
1160 l = (digest[50] << 16) | (digest[ 8] << 8) | (digest[29] << 0);
1161
1162 buf[32] = int_to_itoa64 (l & 0x3f); l >>= 6;
1163 buf[33] = int_to_itoa64 (l & 0x3f); l >>= 6;
1164 buf[34] = int_to_itoa64 (l & 0x3f); l >>= 6;
1165 buf[35] = int_to_itoa64 (l & 0x3f); l >>= 6;
1166
1167 l = (digest[ 9] << 16) | (digest[30] << 8) | (digest[51] << 0);
1168
1169 buf[36] = int_to_itoa64 (l & 0x3f); l >>= 6;
1170 buf[37] = int_to_itoa64 (l & 0x3f); l >>= 6;
1171 buf[38] = int_to_itoa64 (l & 0x3f); l >>= 6;
1172 buf[39] = int_to_itoa64 (l & 0x3f); l >>= 6;
1173
1174 l = (digest[31] << 16) | (digest[52] << 8) | (digest[10] << 0);
1175
1176 buf[40] = int_to_itoa64 (l & 0x3f); l >>= 6;
1177 buf[41] = int_to_itoa64 (l & 0x3f); l >>= 6;
1178 buf[42] = int_to_itoa64 (l & 0x3f); l >>= 6;
1179 buf[43] = int_to_itoa64 (l & 0x3f); l >>= 6;
1180
1181 l = (digest[53] << 16) | (digest[11] << 8) | (digest[32] << 0);
1182
1183 buf[44] = int_to_itoa64 (l & 0x3f); l >>= 6;
1184 buf[45] = int_to_itoa64 (l & 0x3f); l >>= 6;
1185 buf[46] = int_to_itoa64 (l & 0x3f); l >>= 6;
1186 buf[47] = int_to_itoa64 (l & 0x3f); l >>= 6;
1187
1188 l = (digest[12] << 16) | (digest[33] << 8) | (digest[54] << 0);
1189
1190 buf[48] = int_to_itoa64 (l & 0x3f); l >>= 6;
1191 buf[49] = int_to_itoa64 (l & 0x3f); l >>= 6;
1192 buf[50] = int_to_itoa64 (l & 0x3f); l >>= 6;
1193 buf[51] = int_to_itoa64 (l & 0x3f); l >>= 6;
1194
1195 l = (digest[34] << 16) | (digest[55] << 8) | (digest[13] << 0);
1196
1197 buf[52] = int_to_itoa64 (l & 0x3f); l >>= 6;
1198 buf[53] = int_to_itoa64 (l & 0x3f); l >>= 6;
1199 buf[54] = int_to_itoa64 (l & 0x3f); l >>= 6;
1200 buf[55] = int_to_itoa64 (l & 0x3f); l >>= 6;
1201
1202 l = (digest[56] << 16) | (digest[14] << 8) | (digest[35] << 0);
1203
1204 buf[56] = int_to_itoa64 (l & 0x3f); l >>= 6;
1205 buf[57] = int_to_itoa64 (l & 0x3f); l >>= 6;
1206 buf[58] = int_to_itoa64 (l & 0x3f); l >>= 6;
1207 buf[59] = int_to_itoa64 (l & 0x3f); l >>= 6;
1208
1209 l = (digest[15] << 16) | (digest[36] << 8) | (digest[57] << 0);
1210
1211 buf[60] = int_to_itoa64 (l & 0x3f); l >>= 6;
1212 buf[61] = int_to_itoa64 (l & 0x3f); l >>= 6;
1213 buf[62] = int_to_itoa64 (l & 0x3f); l >>= 6;
1214 buf[63] = int_to_itoa64 (l & 0x3f); l >>= 6;
1215
1216 l = (digest[37] << 16) | (digest[58] << 8) | (digest[16] << 0);
1217
1218 buf[64] = int_to_itoa64 (l & 0x3f); l >>= 6;
1219 buf[65] = int_to_itoa64 (l & 0x3f); l >>= 6;
1220 buf[66] = int_to_itoa64 (l & 0x3f); l >>= 6;
1221 buf[67] = int_to_itoa64 (l & 0x3f); l >>= 6;
1222
1223 l = (digest[59] << 16) | (digest[17] << 8) | (digest[38] << 0);
1224
1225 buf[68] = int_to_itoa64 (l & 0x3f); l >>= 6;
1226 buf[69] = int_to_itoa64 (l & 0x3f); l >>= 6;
1227 buf[70] = int_to_itoa64 (l & 0x3f); l >>= 6;
1228 buf[71] = int_to_itoa64 (l & 0x3f); l >>= 6;
1229
1230 l = (digest[18] << 16) | (digest[39] << 8) | (digest[60] << 0);
1231
1232 buf[72] = int_to_itoa64 (l & 0x3f); l >>= 6;
1233 buf[73] = int_to_itoa64 (l & 0x3f); l >>= 6;
1234 buf[74] = int_to_itoa64 (l & 0x3f); l >>= 6;
1235 buf[75] = int_to_itoa64 (l & 0x3f); l >>= 6;
1236
1237 l = (digest[40] << 16) | (digest[61] << 8) | (digest[19] << 0);
1238
1239 buf[76] = int_to_itoa64 (l & 0x3f); l >>= 6;
1240 buf[77] = int_to_itoa64 (l & 0x3f); l >>= 6;
1241 buf[78] = int_to_itoa64 (l & 0x3f); l >>= 6;
1242 buf[79] = int_to_itoa64 (l & 0x3f); l >>= 6;
1243
1244 l = (digest[62] << 16) | (digest[20] << 8) | (digest[41] << 0);
1245
1246 buf[80] = int_to_itoa64 (l & 0x3f); l >>= 6;
1247 buf[81] = int_to_itoa64 (l & 0x3f); l >>= 6;
1248 buf[82] = int_to_itoa64 (l & 0x3f); l >>= 6;
1249 buf[83] = int_to_itoa64 (l & 0x3f); l >>= 6;
1250
1251 l = 0 | 0 | (digest[63] << 0);
1252
1253 buf[84] = int_to_itoa64 (l & 0x3f); l >>= 6;
1254 buf[85] = int_to_itoa64 (l & 0x3f); l >>= 6;
1255 }
1256
1257 void sha1aix_decode (u8 digest[20], u8 buf[27])
1258 {
1259 int l;
1260
1261 l = itoa64_to_int (buf[ 0]) << 0;
1262 l |= itoa64_to_int (buf[ 1]) << 6;
1263 l |= itoa64_to_int (buf[ 2]) << 12;
1264 l |= itoa64_to_int (buf[ 3]) << 18;
1265
1266 digest[ 2] = (l >> 0) & 0xff;
1267 digest[ 1] = (l >> 8) & 0xff;
1268 digest[ 0] = (l >> 16) & 0xff;
1269
1270 l = itoa64_to_int (buf[ 4]) << 0;
1271 l |= itoa64_to_int (buf[ 5]) << 6;
1272 l |= itoa64_to_int (buf[ 6]) << 12;
1273 l |= itoa64_to_int (buf[ 7]) << 18;
1274
1275 digest[ 5] = (l >> 0) & 0xff;
1276 digest[ 4] = (l >> 8) & 0xff;
1277 digest[ 3] = (l >> 16) & 0xff;
1278
1279 l = itoa64_to_int (buf[ 8]) << 0;
1280 l |= itoa64_to_int (buf[ 9]) << 6;
1281 l |= itoa64_to_int (buf[10]) << 12;
1282 l |= itoa64_to_int (buf[11]) << 18;
1283
1284 digest[ 8] = (l >> 0) & 0xff;
1285 digest[ 7] = (l >> 8) & 0xff;
1286 digest[ 6] = (l >> 16) & 0xff;
1287
1288 l = itoa64_to_int (buf[12]) << 0;
1289 l |= itoa64_to_int (buf[13]) << 6;
1290 l |= itoa64_to_int (buf[14]) << 12;
1291 l |= itoa64_to_int (buf[15]) << 18;
1292
1293 digest[11] = (l >> 0) & 0xff;
1294 digest[10] = (l >> 8) & 0xff;
1295 digest[ 9] = (l >> 16) & 0xff;
1296
1297 l = itoa64_to_int (buf[16]) << 0;
1298 l |= itoa64_to_int (buf[17]) << 6;
1299 l |= itoa64_to_int (buf[18]) << 12;
1300 l |= itoa64_to_int (buf[19]) << 18;
1301
1302 digest[14] = (l >> 0) & 0xff;
1303 digest[13] = (l >> 8) & 0xff;
1304 digest[12] = (l >> 16) & 0xff;
1305
1306 l = itoa64_to_int (buf[20]) << 0;
1307 l |= itoa64_to_int (buf[21]) << 6;
1308 l |= itoa64_to_int (buf[22]) << 12;
1309 l |= itoa64_to_int (buf[23]) << 18;
1310
1311 digest[17] = (l >> 0) & 0xff;
1312 digest[16] = (l >> 8) & 0xff;
1313 digest[15] = (l >> 16) & 0xff;
1314
1315 l = itoa64_to_int (buf[24]) << 0;
1316 l |= itoa64_to_int (buf[25]) << 6;
1317 l |= itoa64_to_int (buf[26]) << 12;
1318
1319 digest[19] = (l >> 8) & 0xff;
1320 digest[18] = (l >> 16) & 0xff;
1321 }
1322
1323 void sha1aix_encode (u8 digest[20], u8 buf[27])
1324 {
1325 int l;
1326
1327 l = (digest[ 2] << 0) | (digest[ 1] << 8) | (digest[ 0] << 16);
1328
1329 buf[ 0] = int_to_itoa64 (l & 0x3f); l >>= 6;
1330 buf[ 1] = int_to_itoa64 (l & 0x3f); l >>= 6;
1331 buf[ 2] = int_to_itoa64 (l & 0x3f); l >>= 6;
1332 buf[ 3] = int_to_itoa64 (l & 0x3f);
1333
1334 l = (digest[ 5] << 0) | (digest[ 4] << 8) | (digest[ 3] << 16);
1335
1336 buf[ 4] = int_to_itoa64 (l & 0x3f); l >>= 6;
1337 buf[ 5] = int_to_itoa64 (l & 0x3f); l >>= 6;
1338 buf[ 6] = int_to_itoa64 (l & 0x3f); l >>= 6;
1339 buf[ 7] = int_to_itoa64 (l & 0x3f);
1340
1341 l = (digest[ 8] << 0) | (digest[ 7] << 8) | (digest[ 6] << 16);
1342
1343 buf[ 8] = int_to_itoa64 (l & 0x3f); l >>= 6;
1344 buf[ 9] = int_to_itoa64 (l & 0x3f); l >>= 6;
1345 buf[10] = int_to_itoa64 (l & 0x3f); l >>= 6;
1346 buf[11] = int_to_itoa64 (l & 0x3f);
1347
1348 l = (digest[11] << 0) | (digest[10] << 8) | (digest[ 9] << 16);
1349
1350 buf[12] = int_to_itoa64 (l & 0x3f); l >>= 6;
1351 buf[13] = int_to_itoa64 (l & 0x3f); l >>= 6;
1352 buf[14] = int_to_itoa64 (l & 0x3f); l >>= 6;
1353 buf[15] = int_to_itoa64 (l & 0x3f);
1354
1355 l = (digest[14] << 0) | (digest[13] << 8) | (digest[12] << 16);
1356
1357 buf[16] = int_to_itoa64 (l & 0x3f); l >>= 6;
1358 buf[17] = int_to_itoa64 (l & 0x3f); l >>= 6;
1359 buf[18] = int_to_itoa64 (l & 0x3f); l >>= 6;
1360 buf[19] = int_to_itoa64 (l & 0x3f);
1361
1362 l = (digest[17] << 0) | (digest[16] << 8) | (digest[15] << 16);
1363
1364 buf[20] = int_to_itoa64 (l & 0x3f); l >>= 6;
1365 buf[21] = int_to_itoa64 (l & 0x3f); l >>= 6;
1366 buf[22] = int_to_itoa64 (l & 0x3f); l >>= 6;
1367 buf[23] = int_to_itoa64 (l & 0x3f);
1368
1369 l = 0 | (digest[19] << 8) | (digest[18] << 16);
1370
1371 buf[24] = int_to_itoa64 (l & 0x3f); l >>= 6;
1372 buf[25] = int_to_itoa64 (l & 0x3f); l >>= 6;
1373 buf[26] = int_to_itoa64 (l & 0x3f);
1374 }
1375
1376 void sha256aix_decode (u8 digest[32], u8 buf[43])
1377 {
1378 int l;
1379
1380 l = itoa64_to_int (buf[ 0]) << 0;
1381 l |= itoa64_to_int (buf[ 1]) << 6;
1382 l |= itoa64_to_int (buf[ 2]) << 12;
1383 l |= itoa64_to_int (buf[ 3]) << 18;
1384
1385 digest[ 2] = (l >> 0) & 0xff;
1386 digest[ 1] = (l >> 8) & 0xff;
1387 digest[ 0] = (l >> 16) & 0xff;
1388
1389 l = itoa64_to_int (buf[ 4]) << 0;
1390 l |= itoa64_to_int (buf[ 5]) << 6;
1391 l |= itoa64_to_int (buf[ 6]) << 12;
1392 l |= itoa64_to_int (buf[ 7]) << 18;
1393
1394 digest[ 5] = (l >> 0) & 0xff;
1395 digest[ 4] = (l >> 8) & 0xff;
1396 digest[ 3] = (l >> 16) & 0xff;
1397
1398 l = itoa64_to_int (buf[ 8]) << 0;
1399 l |= itoa64_to_int (buf[ 9]) << 6;
1400 l |= itoa64_to_int (buf[10]) << 12;
1401 l |= itoa64_to_int (buf[11]) << 18;
1402
1403 digest[ 8] = (l >> 0) & 0xff;
1404 digest[ 7] = (l >> 8) & 0xff;
1405 digest[ 6] = (l >> 16) & 0xff;
1406
1407 l = itoa64_to_int (buf[12]) << 0;
1408 l |= itoa64_to_int (buf[13]) << 6;
1409 l |= itoa64_to_int (buf[14]) << 12;
1410 l |= itoa64_to_int (buf[15]) << 18;
1411
1412 digest[11] = (l >> 0) & 0xff;
1413 digest[10] = (l >> 8) & 0xff;
1414 digest[ 9] = (l >> 16) & 0xff;
1415
1416 l = itoa64_to_int (buf[16]) << 0;
1417 l |= itoa64_to_int (buf[17]) << 6;
1418 l |= itoa64_to_int (buf[18]) << 12;
1419 l |= itoa64_to_int (buf[19]) << 18;
1420
1421 digest[14] = (l >> 0) & 0xff;
1422 digest[13] = (l >> 8) & 0xff;
1423 digest[12] = (l >> 16) & 0xff;
1424
1425 l = itoa64_to_int (buf[20]) << 0;
1426 l |= itoa64_to_int (buf[21]) << 6;
1427 l |= itoa64_to_int (buf[22]) << 12;
1428 l |= itoa64_to_int (buf[23]) << 18;
1429
1430 digest[17] = (l >> 0) & 0xff;
1431 digest[16] = (l >> 8) & 0xff;
1432 digest[15] = (l >> 16) & 0xff;
1433
1434 l = itoa64_to_int (buf[24]) << 0;
1435 l |= itoa64_to_int (buf[25]) << 6;
1436 l |= itoa64_to_int (buf[26]) << 12;
1437 l |= itoa64_to_int (buf[27]) << 18;
1438
1439 digest[20] = (l >> 0) & 0xff;
1440 digest[19] = (l >> 8) & 0xff;
1441 digest[18] = (l >> 16) & 0xff;
1442
1443 l = itoa64_to_int (buf[28]) << 0;
1444 l |= itoa64_to_int (buf[29]) << 6;
1445 l |= itoa64_to_int (buf[30]) << 12;
1446 l |= itoa64_to_int (buf[31]) << 18;
1447
1448 digest[23] = (l >> 0) & 0xff;
1449 digest[22] = (l >> 8) & 0xff;
1450 digest[21] = (l >> 16) & 0xff;
1451
1452 l = itoa64_to_int (buf[32]) << 0;
1453 l |= itoa64_to_int (buf[33]) << 6;
1454 l |= itoa64_to_int (buf[34]) << 12;
1455 l |= itoa64_to_int (buf[35]) << 18;
1456
1457 digest[26] = (l >> 0) & 0xff;
1458 digest[25] = (l >> 8) & 0xff;
1459 digest[24] = (l >> 16) & 0xff;
1460
1461 l = itoa64_to_int (buf[36]) << 0;
1462 l |= itoa64_to_int (buf[37]) << 6;
1463 l |= itoa64_to_int (buf[38]) << 12;
1464 l |= itoa64_to_int (buf[39]) << 18;
1465
1466 digest[29] = (l >> 0) & 0xff;
1467 digest[28] = (l >> 8) & 0xff;
1468 digest[27] = (l >> 16) & 0xff;
1469
1470 l = itoa64_to_int (buf[40]) << 0;
1471 l |= itoa64_to_int (buf[41]) << 6;
1472 l |= itoa64_to_int (buf[42]) << 12;
1473
1474 //digest[32] = (l >> 0) & 0xff;
1475 digest[31] = (l >> 8) & 0xff;
1476 digest[30] = (l >> 16) & 0xff;
1477 }
1478
1479 void sha256aix_encode (u8 digest[32], u8 buf[43])
1480 {
1481 int l;
1482
1483 l = (digest[ 2] << 0) | (digest[ 1] << 8) | (digest[ 0] << 16);
1484
1485 buf[ 0] = int_to_itoa64 (l & 0x3f); l >>= 6;
1486 buf[ 1] = int_to_itoa64 (l & 0x3f); l >>= 6;
1487 buf[ 2] = int_to_itoa64 (l & 0x3f); l >>= 6;
1488 buf[ 3] = int_to_itoa64 (l & 0x3f);
1489
1490 l = (digest[ 5] << 0) | (digest[ 4] << 8) | (digest[ 3] << 16);
1491
1492 buf[ 4] = int_to_itoa64 (l & 0x3f); l >>= 6;
1493 buf[ 5] = int_to_itoa64 (l & 0x3f); l >>= 6;
1494 buf[ 6] = int_to_itoa64 (l & 0x3f); l >>= 6;
1495 buf[ 7] = int_to_itoa64 (l & 0x3f);
1496
1497 l = (digest[ 8] << 0) | (digest[ 7] << 8) | (digest[ 6] << 16);
1498
1499 buf[ 8] = int_to_itoa64 (l & 0x3f); l >>= 6;
1500 buf[ 9] = int_to_itoa64 (l & 0x3f); l >>= 6;
1501 buf[10] = int_to_itoa64 (l & 0x3f); l >>= 6;
1502 buf[11] = int_to_itoa64 (l & 0x3f);
1503
1504 l = (digest[11] << 0) | (digest[10] << 8) | (digest[ 9] << 16);
1505
1506 buf[12] = int_to_itoa64 (l & 0x3f); l >>= 6;
1507 buf[13] = int_to_itoa64 (l & 0x3f); l >>= 6;
1508 buf[14] = int_to_itoa64 (l & 0x3f); l >>= 6;
1509 buf[15] = int_to_itoa64 (l & 0x3f);
1510
1511 l = (digest[14] << 0) | (digest[13] << 8) | (digest[12] << 16);
1512
1513 buf[16] = int_to_itoa64 (l & 0x3f); l >>= 6;
1514 buf[17] = int_to_itoa64 (l & 0x3f); l >>= 6;
1515 buf[18] = int_to_itoa64 (l & 0x3f); l >>= 6;
1516 buf[19] = int_to_itoa64 (l & 0x3f);
1517
1518 l = (digest[17] << 0) | (digest[16] << 8) | (digest[15] << 16);
1519
1520 buf[20] = int_to_itoa64 (l & 0x3f); l >>= 6;
1521 buf[21] = int_to_itoa64 (l & 0x3f); l >>= 6;
1522 buf[22] = int_to_itoa64 (l & 0x3f); l >>= 6;
1523 buf[23] = int_to_itoa64 (l & 0x3f);
1524
1525 l = (digest[20] << 0) | (digest[19] << 8) | (digest[18] << 16);
1526
1527 buf[24] = int_to_itoa64 (l & 0x3f); l >>= 6;
1528 buf[25] = int_to_itoa64 (l & 0x3f); l >>= 6;
1529 buf[26] = int_to_itoa64 (l & 0x3f); l >>= 6;
1530 buf[27] = int_to_itoa64 (l & 0x3f);
1531
1532 l = (digest[23] << 0) | (digest[22] << 8) | (digest[21] << 16);
1533
1534 buf[28] = int_to_itoa64 (l & 0x3f); l >>= 6;
1535 buf[29] = int_to_itoa64 (l & 0x3f); l >>= 6;
1536 buf[30] = int_to_itoa64 (l & 0x3f); l >>= 6;
1537 buf[31] = int_to_itoa64 (l & 0x3f);
1538
1539 l = (digest[26] << 0) | (digest[25] << 8) | (digest[24] << 16);
1540
1541 buf[32] = int_to_itoa64 (l & 0x3f); l >>= 6;
1542 buf[33] = int_to_itoa64 (l & 0x3f); l >>= 6;
1543 buf[34] = int_to_itoa64 (l & 0x3f); l >>= 6;
1544 buf[35] = int_to_itoa64 (l & 0x3f);
1545
1546 l = (digest[29] << 0) | (digest[28] << 8) | (digest[27] << 16);
1547
1548 buf[36] = int_to_itoa64 (l & 0x3f); l >>= 6;
1549 buf[37] = int_to_itoa64 (l & 0x3f); l >>= 6;
1550 buf[38] = int_to_itoa64 (l & 0x3f); l >>= 6;
1551 buf[39] = int_to_itoa64 (l & 0x3f);
1552
1553 l = 0 | (digest[31] << 8) | (digest[30] << 16);
1554
1555 buf[40] = int_to_itoa64 (l & 0x3f); l >>= 6;
1556 buf[41] = int_to_itoa64 (l & 0x3f); l >>= 6;
1557 buf[42] = int_to_itoa64 (l & 0x3f);
1558 }
1559
1560 void sha512aix_decode (u8 digest[64], u8 buf[86])
1561 {
1562 int l;
1563
1564 l = itoa64_to_int (buf[ 0]) << 0;
1565 l |= itoa64_to_int (buf[ 1]) << 6;
1566 l |= itoa64_to_int (buf[ 2]) << 12;
1567 l |= itoa64_to_int (buf[ 3]) << 18;
1568
1569 digest[ 2] = (l >> 0) & 0xff;
1570 digest[ 1] = (l >> 8) & 0xff;
1571 digest[ 0] = (l >> 16) & 0xff;
1572
1573 l = itoa64_to_int (buf[ 4]) << 0;
1574 l |= itoa64_to_int (buf[ 5]) << 6;
1575 l |= itoa64_to_int (buf[ 6]) << 12;
1576 l |= itoa64_to_int (buf[ 7]) << 18;
1577
1578 digest[ 5] = (l >> 0) & 0xff;
1579 digest[ 4] = (l >> 8) & 0xff;
1580 digest[ 3] = (l >> 16) & 0xff;
1581
1582 l = itoa64_to_int (buf[ 8]) << 0;
1583 l |= itoa64_to_int (buf[ 9]) << 6;
1584 l |= itoa64_to_int (buf[10]) << 12;
1585 l |= itoa64_to_int (buf[11]) << 18;
1586
1587 digest[ 8] = (l >> 0) & 0xff;
1588 digest[ 7] = (l >> 8) & 0xff;
1589 digest[ 6] = (l >> 16) & 0xff;
1590
1591 l = itoa64_to_int (buf[12]) << 0;
1592 l |= itoa64_to_int (buf[13]) << 6;
1593 l |= itoa64_to_int (buf[14]) << 12;
1594 l |= itoa64_to_int (buf[15]) << 18;
1595
1596 digest[11] = (l >> 0) & 0xff;
1597 digest[10] = (l >> 8) & 0xff;
1598 digest[ 9] = (l >> 16) & 0xff;
1599
1600 l = itoa64_to_int (buf[16]) << 0;
1601 l |= itoa64_to_int (buf[17]) << 6;
1602 l |= itoa64_to_int (buf[18]) << 12;
1603 l |= itoa64_to_int (buf[19]) << 18;
1604
1605 digest[14] = (l >> 0) & 0xff;
1606 digest[13] = (l >> 8) & 0xff;
1607 digest[12] = (l >> 16) & 0xff;
1608
1609 l = itoa64_to_int (buf[20]) << 0;
1610 l |= itoa64_to_int (buf[21]) << 6;
1611 l |= itoa64_to_int (buf[22]) << 12;
1612 l |= itoa64_to_int (buf[23]) << 18;
1613
1614 digest[17] = (l >> 0) & 0xff;
1615 digest[16] = (l >> 8) & 0xff;
1616 digest[15] = (l >> 16) & 0xff;
1617
1618 l = itoa64_to_int (buf[24]) << 0;
1619 l |= itoa64_to_int (buf[25]) << 6;
1620 l |= itoa64_to_int (buf[26]) << 12;
1621 l |= itoa64_to_int (buf[27]) << 18;
1622
1623 digest[20] = (l >> 0) & 0xff;
1624 digest[19] = (l >> 8) & 0xff;
1625 digest[18] = (l >> 16) & 0xff;
1626
1627 l = itoa64_to_int (buf[28]) << 0;
1628 l |= itoa64_to_int (buf[29]) << 6;
1629 l |= itoa64_to_int (buf[30]) << 12;
1630 l |= itoa64_to_int (buf[31]) << 18;
1631
1632 digest[23] = (l >> 0) & 0xff;
1633 digest[22] = (l >> 8) & 0xff;
1634 digest[21] = (l >> 16) & 0xff;
1635
1636 l = itoa64_to_int (buf[32]) << 0;
1637 l |= itoa64_to_int (buf[33]) << 6;
1638 l |= itoa64_to_int (buf[34]) << 12;
1639 l |= itoa64_to_int (buf[35]) << 18;
1640
1641 digest[26] = (l >> 0) & 0xff;
1642 digest[25] = (l >> 8) & 0xff;
1643 digest[24] = (l >> 16) & 0xff;
1644
1645 l = itoa64_to_int (buf[36]) << 0;
1646 l |= itoa64_to_int (buf[37]) << 6;
1647 l |= itoa64_to_int (buf[38]) << 12;
1648 l |= itoa64_to_int (buf[39]) << 18;
1649
1650 digest[29] = (l >> 0) & 0xff;
1651 digest[28] = (l >> 8) & 0xff;
1652 digest[27] = (l >> 16) & 0xff;
1653
1654 l = itoa64_to_int (buf[40]) << 0;
1655 l |= itoa64_to_int (buf[41]) << 6;
1656 l |= itoa64_to_int (buf[42]) << 12;
1657 l |= itoa64_to_int (buf[43]) << 18;
1658
1659 digest[32] = (l >> 0) & 0xff;
1660 digest[31] = (l >> 8) & 0xff;
1661 digest[30] = (l >> 16) & 0xff;
1662
1663 l = itoa64_to_int (buf[44]) << 0;
1664 l |= itoa64_to_int (buf[45]) << 6;
1665 l |= itoa64_to_int (buf[46]) << 12;
1666 l |= itoa64_to_int (buf[47]) << 18;
1667
1668 digest[35] = (l >> 0) & 0xff;
1669 digest[34] = (l >> 8) & 0xff;
1670 digest[33] = (l >> 16) & 0xff;
1671
1672 l = itoa64_to_int (buf[48]) << 0;
1673 l |= itoa64_to_int (buf[49]) << 6;
1674 l |= itoa64_to_int (buf[50]) << 12;
1675 l |= itoa64_to_int (buf[51]) << 18;
1676
1677 digest[38] = (l >> 0) & 0xff;
1678 digest[37] = (l >> 8) & 0xff;
1679 digest[36] = (l >> 16) & 0xff;
1680
1681 l = itoa64_to_int (buf[52]) << 0;
1682 l |= itoa64_to_int (buf[53]) << 6;
1683 l |= itoa64_to_int (buf[54]) << 12;
1684 l |= itoa64_to_int (buf[55]) << 18;
1685
1686 digest[41] = (l >> 0) & 0xff;
1687 digest[40] = (l >> 8) & 0xff;
1688 digest[39] = (l >> 16) & 0xff;
1689
1690 l = itoa64_to_int (buf[56]) << 0;
1691 l |= itoa64_to_int (buf[57]) << 6;
1692 l |= itoa64_to_int (buf[58]) << 12;
1693 l |= itoa64_to_int (buf[59]) << 18;
1694
1695 digest[44] = (l >> 0) & 0xff;
1696 digest[43] = (l >> 8) & 0xff;
1697 digest[42] = (l >> 16) & 0xff;
1698
1699 l = itoa64_to_int (buf[60]) << 0;
1700 l |= itoa64_to_int (buf[61]) << 6;
1701 l |= itoa64_to_int (buf[62]) << 12;
1702 l |= itoa64_to_int (buf[63]) << 18;
1703
1704 digest[47] = (l >> 0) & 0xff;
1705 digest[46] = (l >> 8) & 0xff;
1706 digest[45] = (l >> 16) & 0xff;
1707
1708 l = itoa64_to_int (buf[64]) << 0;
1709 l |= itoa64_to_int (buf[65]) << 6;
1710 l |= itoa64_to_int (buf[66]) << 12;
1711 l |= itoa64_to_int (buf[67]) << 18;
1712
1713 digest[50] = (l >> 0) & 0xff;
1714 digest[49] = (l >> 8) & 0xff;
1715 digest[48] = (l >> 16) & 0xff;
1716
1717 l = itoa64_to_int (buf[68]) << 0;
1718 l |= itoa64_to_int (buf[69]) << 6;
1719 l |= itoa64_to_int (buf[70]) << 12;
1720 l |= itoa64_to_int (buf[71]) << 18;
1721
1722 digest[53] = (l >> 0) & 0xff;
1723 digest[52] = (l >> 8) & 0xff;
1724 digest[51] = (l >> 16) & 0xff;
1725
1726 l = itoa64_to_int (buf[72]) << 0;
1727 l |= itoa64_to_int (buf[73]) << 6;
1728 l |= itoa64_to_int (buf[74]) << 12;
1729 l |= itoa64_to_int (buf[75]) << 18;
1730
1731 digest[56] = (l >> 0) & 0xff;
1732 digest[55] = (l >> 8) & 0xff;
1733 digest[54] = (l >> 16) & 0xff;
1734
1735 l = itoa64_to_int (buf[76]) << 0;
1736 l |= itoa64_to_int (buf[77]) << 6;
1737 l |= itoa64_to_int (buf[78]) << 12;
1738 l |= itoa64_to_int (buf[79]) << 18;
1739
1740 digest[59] = (l >> 0) & 0xff;
1741 digest[58] = (l >> 8) & 0xff;
1742 digest[57] = (l >> 16) & 0xff;
1743
1744 l = itoa64_to_int (buf[80]) << 0;
1745 l |= itoa64_to_int (buf[81]) << 6;
1746 l |= itoa64_to_int (buf[82]) << 12;
1747 l |= itoa64_to_int (buf[83]) << 18;
1748
1749 digest[62] = (l >> 0) & 0xff;
1750 digest[61] = (l >> 8) & 0xff;
1751 digest[60] = (l >> 16) & 0xff;
1752
1753 l = itoa64_to_int (buf[84]) << 0;
1754 l |= itoa64_to_int (buf[85]) << 6;
1755
1756 digest[63] = (l >> 16) & 0xff;
1757 }
1758
1759 void sha512aix_encode (u8 digest[64], u8 buf[86])
1760 {
1761 int l;
1762
1763 l = (digest[ 2] << 0) | (digest[ 1] << 8) | (digest[ 0] << 16);
1764
1765 buf[ 0] = int_to_itoa64 (l & 0x3f); l >>= 6;
1766 buf[ 1] = int_to_itoa64 (l & 0x3f); l >>= 6;
1767 buf[ 2] = int_to_itoa64 (l & 0x3f); l >>= 6;
1768 buf[ 3] = int_to_itoa64 (l & 0x3f);
1769
1770 l = (digest[ 5] << 0) | (digest[ 4] << 8) | (digest[ 3] << 16);
1771
1772 buf[ 4] = int_to_itoa64 (l & 0x3f); l >>= 6;
1773 buf[ 5] = int_to_itoa64 (l & 0x3f); l >>= 6;
1774 buf[ 6] = int_to_itoa64 (l & 0x3f); l >>= 6;
1775 buf[ 7] = int_to_itoa64 (l & 0x3f);
1776
1777 l = (digest[ 8] << 0) | (digest[ 7] << 8) | (digest[ 6] << 16);
1778
1779 buf[ 8] = int_to_itoa64 (l & 0x3f); l >>= 6;
1780 buf[ 9] = int_to_itoa64 (l & 0x3f); l >>= 6;
1781 buf[10] = int_to_itoa64 (l & 0x3f); l >>= 6;
1782 buf[11] = int_to_itoa64 (l & 0x3f);
1783
1784 l = (digest[11] << 0) | (digest[10] << 8) | (digest[ 9] << 16);
1785
1786 buf[12] = int_to_itoa64 (l & 0x3f); l >>= 6;
1787 buf[13] = int_to_itoa64 (l & 0x3f); l >>= 6;
1788 buf[14] = int_to_itoa64 (l & 0x3f); l >>= 6;
1789 buf[15] = int_to_itoa64 (l & 0x3f);
1790
1791 l = (digest[14] << 0) | (digest[13] << 8) | (digest[12] << 16);
1792
1793 buf[16] = int_to_itoa64 (l & 0x3f); l >>= 6;
1794 buf[17] = int_to_itoa64 (l & 0x3f); l >>= 6;
1795 buf[18] = int_to_itoa64 (l & 0x3f); l >>= 6;
1796 buf[19] = int_to_itoa64 (l & 0x3f);
1797
1798 l = (digest[17] << 0) | (digest[16] << 8) | (digest[15] << 16);
1799
1800 buf[20] = int_to_itoa64 (l & 0x3f); l >>= 6;
1801 buf[21] = int_to_itoa64 (l & 0x3f); l >>= 6;
1802 buf[22] = int_to_itoa64 (l & 0x3f); l >>= 6;
1803 buf[23] = int_to_itoa64 (l & 0x3f);
1804
1805 l = (digest[20] << 0) | (digest[19] << 8) | (digest[18] << 16);
1806
1807 buf[24] = int_to_itoa64 (l & 0x3f); l >>= 6;
1808 buf[25] = int_to_itoa64 (l & 0x3f); l >>= 6;
1809 buf[26] = int_to_itoa64 (l & 0x3f); l >>= 6;
1810 buf[27] = int_to_itoa64 (l & 0x3f);
1811
1812 l = (digest[23] << 0) | (digest[22] << 8) | (digest[21] << 16);
1813
1814 buf[28] = int_to_itoa64 (l & 0x3f); l >>= 6;
1815 buf[29] = int_to_itoa64 (l & 0x3f); l >>= 6;
1816 buf[30] = int_to_itoa64 (l & 0x3f); l >>= 6;
1817 buf[31] = int_to_itoa64 (l & 0x3f);
1818
1819 l = (digest[26] << 0) | (digest[25] << 8) | (digest[24] << 16);
1820
1821 buf[32] = int_to_itoa64 (l & 0x3f); l >>= 6;
1822 buf[33] = int_to_itoa64 (l & 0x3f); l >>= 6;
1823 buf[34] = int_to_itoa64 (l & 0x3f); l >>= 6;
1824 buf[35] = int_to_itoa64 (l & 0x3f);
1825
1826 l = (digest[29] << 0) | (digest[28] << 8) | (digest[27] << 16);
1827
1828 buf[36] = int_to_itoa64 (l & 0x3f); l >>= 6;
1829 buf[37] = int_to_itoa64 (l & 0x3f); l >>= 6;
1830 buf[38] = int_to_itoa64 (l & 0x3f); l >>= 6;
1831 buf[39] = int_to_itoa64 (l & 0x3f);
1832
1833 l = (digest[32] << 0) | (digest[31] << 8) | (digest[30] << 16);
1834
1835 buf[40] = int_to_itoa64 (l & 0x3f); l >>= 6;
1836 buf[41] = int_to_itoa64 (l & 0x3f); l >>= 6;
1837 buf[42] = int_to_itoa64 (l & 0x3f); l >>= 6;
1838 buf[43] = int_to_itoa64 (l & 0x3f);
1839
1840 l = (digest[35] << 0) | (digest[34] << 8) | (digest[33] << 16);
1841
1842 buf[44] = int_to_itoa64 (l & 0x3f); l >>= 6;
1843 buf[45] = int_to_itoa64 (l & 0x3f); l >>= 6;
1844 buf[46] = int_to_itoa64 (l & 0x3f); l >>= 6;
1845 buf[47] = int_to_itoa64 (l & 0x3f);
1846
1847 l = (digest[38] << 0) | (digest[37] << 8) | (digest[36] << 16);
1848
1849 buf[48] = int_to_itoa64 (l & 0x3f); l >>= 6;
1850 buf[49] = int_to_itoa64 (l & 0x3f); l >>= 6;
1851 buf[50] = int_to_itoa64 (l & 0x3f); l >>= 6;
1852 buf[51] = int_to_itoa64 (l & 0x3f);
1853
1854 l = (digest[41] << 0) | (digest[40] << 8) | (digest[39] << 16);
1855
1856 buf[52] = int_to_itoa64 (l & 0x3f); l >>= 6;
1857 buf[53] = int_to_itoa64 (l & 0x3f); l >>= 6;
1858 buf[54] = int_to_itoa64 (l & 0x3f); l >>= 6;
1859 buf[55] = int_to_itoa64 (l & 0x3f);
1860
1861 l = (digest[44] << 0) | (digest[43] << 8) | (digest[42] << 16);
1862
1863 buf[56] = int_to_itoa64 (l & 0x3f); l >>= 6;
1864 buf[57] = int_to_itoa64 (l & 0x3f); l >>= 6;
1865 buf[58] = int_to_itoa64 (l & 0x3f); l >>= 6;
1866 buf[59] = int_to_itoa64 (l & 0x3f);
1867
1868 l = (digest[47] << 0) | (digest[46] << 8) | (digest[45] << 16);
1869
1870 buf[60] = int_to_itoa64 (l & 0x3f); l >>= 6;
1871 buf[61] = int_to_itoa64 (l & 0x3f); l >>= 6;
1872 buf[62] = int_to_itoa64 (l & 0x3f); l >>= 6;
1873 buf[63] = int_to_itoa64 (l & 0x3f);
1874
1875 l = (digest[50] << 0) | (digest[49] << 8) | (digest[48] << 16);
1876
1877 buf[64] = int_to_itoa64 (l & 0x3f); l >>= 6;
1878 buf[65] = int_to_itoa64 (l & 0x3f); l >>= 6;
1879 buf[66] = int_to_itoa64 (l & 0x3f); l >>= 6;
1880 buf[67] = int_to_itoa64 (l & 0x3f);
1881
1882 l = (digest[53] << 0) | (digest[52] << 8) | (digest[51] << 16);
1883
1884 buf[68] = int_to_itoa64 (l & 0x3f); l >>= 6;
1885 buf[69] = int_to_itoa64 (l & 0x3f); l >>= 6;
1886 buf[70] = int_to_itoa64 (l & 0x3f); l >>= 6;
1887 buf[71] = int_to_itoa64 (l & 0x3f);
1888
1889 l = (digest[56] << 0) | (digest[55] << 8) | (digest[54] << 16);
1890
1891 buf[72] = int_to_itoa64 (l & 0x3f); l >>= 6;
1892 buf[73] = int_to_itoa64 (l & 0x3f); l >>= 6;
1893 buf[74] = int_to_itoa64 (l & 0x3f); l >>= 6;
1894 buf[75] = int_to_itoa64 (l & 0x3f);
1895
1896 l = (digest[59] << 0) | (digest[58] << 8) | (digest[57] << 16);
1897
1898 buf[76] = int_to_itoa64 (l & 0x3f); l >>= 6;
1899 buf[77] = int_to_itoa64 (l & 0x3f); l >>= 6;
1900 buf[78] = int_to_itoa64 (l & 0x3f); l >>= 6;
1901 buf[79] = int_to_itoa64 (l & 0x3f);
1902
1903 l = (digest[62] << 0) | (digest[61] << 8) | (digest[60] << 16);
1904
1905 buf[80] = int_to_itoa64 (l & 0x3f); l >>= 6;
1906 buf[81] = int_to_itoa64 (l & 0x3f); l >>= 6;
1907 buf[82] = int_to_itoa64 (l & 0x3f); l >>= 6;
1908 buf[83] = int_to_itoa64 (l & 0x3f);
1909
1910 l = 0 | 0 | (digest[63] << 16);
1911
1912 buf[84] = int_to_itoa64 (l & 0x3f); l >>= 6;
1913 buf[85] = int_to_itoa64 (l & 0x3f); l >>= 6;
1914 }
1915
1916 void sha256crypt_decode (u8 digest[32], u8 buf[43])
1917 {
1918 int l;
1919
1920 l = itoa64_to_int (buf[ 0]) << 0;
1921 l |= itoa64_to_int (buf[ 1]) << 6;
1922 l |= itoa64_to_int (buf[ 2]) << 12;
1923 l |= itoa64_to_int (buf[ 3]) << 18;
1924
1925 digest[ 0] = (l >> 16) & 0xff;
1926 digest[10] = (l >> 8) & 0xff;
1927 digest[20] = (l >> 0) & 0xff;
1928
1929 l = itoa64_to_int (buf[ 4]) << 0;
1930 l |= itoa64_to_int (buf[ 5]) << 6;
1931 l |= itoa64_to_int (buf[ 6]) << 12;
1932 l |= itoa64_to_int (buf[ 7]) << 18;
1933
1934 digest[21] = (l >> 16) & 0xff;
1935 digest[ 1] = (l >> 8) & 0xff;
1936 digest[11] = (l >> 0) & 0xff;
1937
1938 l = itoa64_to_int (buf[ 8]) << 0;
1939 l |= itoa64_to_int (buf[ 9]) << 6;
1940 l |= itoa64_to_int (buf[10]) << 12;
1941 l |= itoa64_to_int (buf[11]) << 18;
1942
1943 digest[12] = (l >> 16) & 0xff;
1944 digest[22] = (l >> 8) & 0xff;
1945 digest[ 2] = (l >> 0) & 0xff;
1946
1947 l = itoa64_to_int (buf[12]) << 0;
1948 l |= itoa64_to_int (buf[13]) << 6;
1949 l |= itoa64_to_int (buf[14]) << 12;
1950 l |= itoa64_to_int (buf[15]) << 18;
1951
1952 digest[ 3] = (l >> 16) & 0xff;
1953 digest[13] = (l >> 8) & 0xff;
1954 digest[23] = (l >> 0) & 0xff;
1955
1956 l = itoa64_to_int (buf[16]) << 0;
1957 l |= itoa64_to_int (buf[17]) << 6;
1958 l |= itoa64_to_int (buf[18]) << 12;
1959 l |= itoa64_to_int (buf[19]) << 18;
1960
1961 digest[24] = (l >> 16) & 0xff;
1962 digest[ 4] = (l >> 8) & 0xff;
1963 digest[14] = (l >> 0) & 0xff;
1964
1965 l = itoa64_to_int (buf[20]) << 0;
1966 l |= itoa64_to_int (buf[21]) << 6;
1967 l |= itoa64_to_int (buf[22]) << 12;
1968 l |= itoa64_to_int (buf[23]) << 18;
1969
1970 digest[15] = (l >> 16) & 0xff;
1971 digest[25] = (l >> 8) & 0xff;
1972 digest[ 5] = (l >> 0) & 0xff;
1973
1974 l = itoa64_to_int (buf[24]) << 0;
1975 l |= itoa64_to_int (buf[25]) << 6;
1976 l |= itoa64_to_int (buf[26]) << 12;
1977 l |= itoa64_to_int (buf[27]) << 18;
1978
1979 digest[ 6] = (l >> 16) & 0xff;
1980 digest[16] = (l >> 8) & 0xff;
1981 digest[26] = (l >> 0) & 0xff;
1982
1983 l = itoa64_to_int (buf[28]) << 0;
1984 l |= itoa64_to_int (buf[29]) << 6;
1985 l |= itoa64_to_int (buf[30]) << 12;
1986 l |= itoa64_to_int (buf[31]) << 18;
1987
1988 digest[27] = (l >> 16) & 0xff;
1989 digest[ 7] = (l >> 8) & 0xff;
1990 digest[17] = (l >> 0) & 0xff;
1991
1992 l = itoa64_to_int (buf[32]) << 0;
1993 l |= itoa64_to_int (buf[33]) << 6;
1994 l |= itoa64_to_int (buf[34]) << 12;
1995 l |= itoa64_to_int (buf[35]) << 18;
1996
1997 digest[18] = (l >> 16) & 0xff;
1998 digest[28] = (l >> 8) & 0xff;
1999 digest[ 8] = (l >> 0) & 0xff;
2000
2001 l = itoa64_to_int (buf[36]) << 0;
2002 l |= itoa64_to_int (buf[37]) << 6;
2003 l |= itoa64_to_int (buf[38]) << 12;
2004 l |= itoa64_to_int (buf[39]) << 18;
2005
2006 digest[ 9] = (l >> 16) & 0xff;
2007 digest[19] = (l >> 8) & 0xff;
2008 digest[29] = (l >> 0) & 0xff;
2009
2010 l = itoa64_to_int (buf[40]) << 0;
2011 l |= itoa64_to_int (buf[41]) << 6;
2012 l |= itoa64_to_int (buf[42]) << 12;
2013
2014 digest[31] = (l >> 8) & 0xff;
2015 digest[30] = (l >> 0) & 0xff;
2016 }
2017
2018 void sha256crypt_encode (u8 digest[32], u8 buf[43])
2019 {
2020 int l;
2021
2022 l = (digest[ 0] << 16) | (digest[10] << 8) | (digest[20] << 0);
2023
2024 buf[ 0] = int_to_itoa64 (l & 0x3f); l >>= 6;
2025 buf[ 1] = int_to_itoa64 (l & 0x3f); l >>= 6;
2026 buf[ 2] = int_to_itoa64 (l & 0x3f); l >>= 6;
2027 buf[ 3] = int_to_itoa64 (l & 0x3f); l >>= 6;
2028
2029 l = (digest[21] << 16) | (digest[ 1] << 8) | (digest[11] << 0);
2030
2031 buf[ 4] = int_to_itoa64 (l & 0x3f); l >>= 6;
2032 buf[ 5] = int_to_itoa64 (l & 0x3f); l >>= 6;
2033 buf[ 6] = int_to_itoa64 (l & 0x3f); l >>= 6;
2034 buf[ 7] = int_to_itoa64 (l & 0x3f); l >>= 6;
2035
2036 l = (digest[12] << 16) | (digest[22] << 8) | (digest[ 2] << 0);
2037
2038 buf[ 8] = int_to_itoa64 (l & 0x3f); l >>= 6;
2039 buf[ 9] = int_to_itoa64 (l & 0x3f); l >>= 6;
2040 buf[10] = int_to_itoa64 (l & 0x3f); l >>= 6;
2041 buf[11] = int_to_itoa64 (l & 0x3f); l >>= 6;
2042
2043 l = (digest[ 3] << 16) | (digest[13] << 8) | (digest[23] << 0);
2044
2045 buf[12] = int_to_itoa64 (l & 0x3f); l >>= 6;
2046 buf[13] = int_to_itoa64 (l & 0x3f); l >>= 6;
2047 buf[14] = int_to_itoa64 (l & 0x3f); l >>= 6;
2048 buf[15] = int_to_itoa64 (l & 0x3f); l >>= 6;
2049
2050 l = (digest[24] << 16) | (digest[ 4] << 8) | (digest[14] << 0);
2051
2052 buf[16] = int_to_itoa64 (l & 0x3f); l >>= 6;
2053 buf[17] = int_to_itoa64 (l & 0x3f); l >>= 6;
2054 buf[18] = int_to_itoa64 (l & 0x3f); l >>= 6;
2055 buf[19] = int_to_itoa64 (l & 0x3f); l >>= 6;
2056
2057 l = (digest[15] << 16) | (digest[25] << 8) | (digest[ 5] << 0);
2058
2059 buf[20] = int_to_itoa64 (l & 0x3f); l >>= 6;
2060 buf[21] = int_to_itoa64 (l & 0x3f); l >>= 6;
2061 buf[22] = int_to_itoa64 (l & 0x3f); l >>= 6;
2062 buf[23] = int_to_itoa64 (l & 0x3f); l >>= 6;
2063
2064 l = (digest[ 6] << 16) | (digest[16] << 8) | (digest[26] << 0);
2065
2066 buf[24] = int_to_itoa64 (l & 0x3f); l >>= 6;
2067 buf[25] = int_to_itoa64 (l & 0x3f); l >>= 6;
2068 buf[26] = int_to_itoa64 (l & 0x3f); l >>= 6;
2069 buf[27] = int_to_itoa64 (l & 0x3f); l >>= 6;
2070
2071 l = (digest[27] << 16) | (digest[ 7] << 8) | (digest[17] << 0);
2072
2073 buf[28] = int_to_itoa64 (l & 0x3f); l >>= 6;
2074 buf[29] = int_to_itoa64 (l & 0x3f); l >>= 6;
2075 buf[30] = int_to_itoa64 (l & 0x3f); l >>= 6;
2076 buf[31] = int_to_itoa64 (l & 0x3f); l >>= 6;
2077
2078 l = (digest[18] << 16) | (digest[28] << 8) | (digest[ 8] << 0);
2079
2080 buf[32] = int_to_itoa64 (l & 0x3f); l >>= 6;
2081 buf[33] = int_to_itoa64 (l & 0x3f); l >>= 6;
2082 buf[34] = int_to_itoa64 (l & 0x3f); l >>= 6;
2083 buf[35] = int_to_itoa64 (l & 0x3f); l >>= 6;
2084
2085 l = (digest[ 9] << 16) | (digest[19] << 8) | (digest[29] << 0);
2086
2087 buf[36] = int_to_itoa64 (l & 0x3f); l >>= 6;
2088 buf[37] = int_to_itoa64 (l & 0x3f); l >>= 6;
2089 buf[38] = int_to_itoa64 (l & 0x3f); l >>= 6;
2090 buf[39] = int_to_itoa64 (l & 0x3f); l >>= 6;
2091
2092 l = 0 | (digest[31] << 8) | (digest[30] << 0);
2093
2094 buf[40] = int_to_itoa64 (l & 0x3f); l >>= 6;
2095 buf[41] = int_to_itoa64 (l & 0x3f); l >>= 6;
2096 buf[42] = int_to_itoa64 (l & 0x3f);
2097 }
2098
2099 void drupal7_decode (u8 digest[64], u8 buf[44])
2100 {
2101 int l;
2102
2103 l = itoa64_to_int (buf[ 0]) << 0;
2104 l |= itoa64_to_int (buf[ 1]) << 6;
2105 l |= itoa64_to_int (buf[ 2]) << 12;
2106 l |= itoa64_to_int (buf[ 3]) << 18;
2107
2108 digest[ 0] = (l >> 0) & 0xff;
2109 digest[ 1] = (l >> 8) & 0xff;
2110 digest[ 2] = (l >> 16) & 0xff;
2111
2112 l = itoa64_to_int (buf[ 4]) << 0;
2113 l |= itoa64_to_int (buf[ 5]) << 6;
2114 l |= itoa64_to_int (buf[ 6]) << 12;
2115 l |= itoa64_to_int (buf[ 7]) << 18;
2116
2117 digest[ 3] = (l >> 0) & 0xff;
2118 digest[ 4] = (l >> 8) & 0xff;
2119 digest[ 5] = (l >> 16) & 0xff;
2120
2121 l = itoa64_to_int (buf[ 8]) << 0;
2122 l |= itoa64_to_int (buf[ 9]) << 6;
2123 l |= itoa64_to_int (buf[10]) << 12;
2124 l |= itoa64_to_int (buf[11]) << 18;
2125
2126 digest[ 6] = (l >> 0) & 0xff;
2127 digest[ 7] = (l >> 8) & 0xff;
2128 digest[ 8] = (l >> 16) & 0xff;
2129
2130 l = itoa64_to_int (buf[12]) << 0;
2131 l |= itoa64_to_int (buf[13]) << 6;
2132 l |= itoa64_to_int (buf[14]) << 12;
2133 l |= itoa64_to_int (buf[15]) << 18;
2134
2135 digest[ 9] = (l >> 0) & 0xff;
2136 digest[10] = (l >> 8) & 0xff;
2137 digest[11] = (l >> 16) & 0xff;
2138
2139 l = itoa64_to_int (buf[16]) << 0;
2140 l |= itoa64_to_int (buf[17]) << 6;
2141 l |= itoa64_to_int (buf[18]) << 12;
2142 l |= itoa64_to_int (buf[19]) << 18;
2143
2144 digest[12] = (l >> 0) & 0xff;
2145 digest[13] = (l >> 8) & 0xff;
2146 digest[14] = (l >> 16) & 0xff;
2147
2148 l = itoa64_to_int (buf[20]) << 0;
2149 l |= itoa64_to_int (buf[21]) << 6;
2150 l |= itoa64_to_int (buf[22]) << 12;
2151 l |= itoa64_to_int (buf[23]) << 18;
2152
2153 digest[15] = (l >> 0) & 0xff;
2154 digest[16] = (l >> 8) & 0xff;
2155 digest[17] = (l >> 16) & 0xff;
2156
2157 l = itoa64_to_int (buf[24]) << 0;
2158 l |= itoa64_to_int (buf[25]) << 6;
2159 l |= itoa64_to_int (buf[26]) << 12;
2160 l |= itoa64_to_int (buf[27]) << 18;
2161
2162 digest[18] = (l >> 0) & 0xff;
2163 digest[19] = (l >> 8) & 0xff;
2164 digest[20] = (l >> 16) & 0xff;
2165
2166 l = itoa64_to_int (buf[28]) << 0;
2167 l |= itoa64_to_int (buf[29]) << 6;
2168 l |= itoa64_to_int (buf[30]) << 12;
2169 l |= itoa64_to_int (buf[31]) << 18;
2170
2171 digest[21] = (l >> 0) & 0xff;
2172 digest[22] = (l >> 8) & 0xff;
2173 digest[23] = (l >> 16) & 0xff;
2174
2175 l = itoa64_to_int (buf[32]) << 0;
2176 l |= itoa64_to_int (buf[33]) << 6;
2177 l |= itoa64_to_int (buf[34]) << 12;
2178 l |= itoa64_to_int (buf[35]) << 18;
2179
2180 digest[24] = (l >> 0) & 0xff;
2181 digest[25] = (l >> 8) & 0xff;
2182 digest[26] = (l >> 16) & 0xff;
2183
2184 l = itoa64_to_int (buf[36]) << 0;
2185 l |= itoa64_to_int (buf[37]) << 6;
2186 l |= itoa64_to_int (buf[38]) << 12;
2187 l |= itoa64_to_int (buf[39]) << 18;
2188
2189 digest[27] = (l >> 0) & 0xff;
2190 digest[28] = (l >> 8) & 0xff;
2191 digest[29] = (l >> 16) & 0xff;
2192
2193 l = itoa64_to_int (buf[40]) << 0;
2194 l |= itoa64_to_int (buf[41]) << 6;
2195 l |= itoa64_to_int (buf[42]) << 12;
2196 l |= itoa64_to_int (buf[43]) << 18;
2197
2198 digest[30] = (l >> 0) & 0xff;
2199 digest[31] = (l >> 8) & 0xff;
2200 digest[32] = (l >> 16) & 0xff;
2201
2202 digest[33] = 0;
2203 digest[34] = 0;
2204 digest[35] = 0;
2205 digest[36] = 0;
2206 digest[37] = 0;
2207 digest[38] = 0;
2208 digest[39] = 0;
2209 digest[40] = 0;
2210 digest[41] = 0;
2211 digest[42] = 0;
2212 digest[43] = 0;
2213 digest[44] = 0;
2214 digest[45] = 0;
2215 digest[46] = 0;
2216 digest[47] = 0;
2217 digest[48] = 0;
2218 digest[49] = 0;
2219 digest[50] = 0;
2220 digest[51] = 0;
2221 digest[52] = 0;
2222 digest[53] = 0;
2223 digest[54] = 0;
2224 digest[55] = 0;
2225 digest[56] = 0;
2226 digest[57] = 0;
2227 digest[58] = 0;
2228 digest[59] = 0;
2229 digest[60] = 0;
2230 digest[61] = 0;
2231 digest[62] = 0;
2232 digest[63] = 0;
2233 }
2234
2235 void drupal7_encode (u8 digest[64], u8 buf[43])
2236 {
2237 int l;
2238
2239 l = (digest[ 0] << 0) | (digest[ 1] << 8) | (digest[ 2] << 16);
2240
2241 buf[ 0] = int_to_itoa64 (l & 0x3f); l >>= 6;
2242 buf[ 1] = int_to_itoa64 (l & 0x3f); l >>= 6;
2243 buf[ 2] = int_to_itoa64 (l & 0x3f); l >>= 6;
2244 buf[ 3] = int_to_itoa64 (l & 0x3f);
2245
2246 l = (digest[ 3] << 0) | (digest[ 4] << 8) | (digest[ 5] << 16);
2247
2248 buf[ 4] = int_to_itoa64 (l & 0x3f); l >>= 6;
2249 buf[ 5] = int_to_itoa64 (l & 0x3f); l >>= 6;
2250 buf[ 6] = int_to_itoa64 (l & 0x3f); l >>= 6;
2251 buf[ 7] = int_to_itoa64 (l & 0x3f);
2252
2253 l = (digest[ 6] << 0) | (digest[ 7] << 8) | (digest[ 8] << 16);
2254
2255 buf[ 8] = int_to_itoa64 (l & 0x3f); l >>= 6;
2256 buf[ 9] = int_to_itoa64 (l & 0x3f); l >>= 6;
2257 buf[10] = int_to_itoa64 (l & 0x3f); l >>= 6;
2258 buf[11] = int_to_itoa64 (l & 0x3f);
2259
2260 l = (digest[ 9] << 0) | (digest[10] << 8) | (digest[11] << 16);
2261
2262 buf[12] = int_to_itoa64 (l & 0x3f); l >>= 6;
2263 buf[13] = int_to_itoa64 (l & 0x3f); l >>= 6;
2264 buf[14] = int_to_itoa64 (l & 0x3f); l >>= 6;
2265 buf[15] = int_to_itoa64 (l & 0x3f);
2266
2267 l = (digest[12] << 0) | (digest[13] << 8) | (digest[14] << 16);
2268
2269 buf[16] = int_to_itoa64 (l & 0x3f); l >>= 6;
2270 buf[17] = int_to_itoa64 (l & 0x3f); l >>= 6;
2271 buf[18] = int_to_itoa64 (l & 0x3f); l >>= 6;
2272 buf[19] = int_to_itoa64 (l & 0x3f);
2273
2274 l = (digest[15] << 0) | (digest[16] << 8) | (digest[17] << 16);
2275
2276 buf[20] = int_to_itoa64 (l & 0x3f); l >>= 6;
2277 buf[21] = int_to_itoa64 (l & 0x3f); l >>= 6;
2278 buf[22] = int_to_itoa64 (l & 0x3f); l >>= 6;
2279 buf[23] = int_to_itoa64 (l & 0x3f);
2280
2281 l = (digest[18] << 0) | (digest[19] << 8) | (digest[20] << 16);
2282
2283 buf[24] = int_to_itoa64 (l & 0x3f); l >>= 6;
2284 buf[25] = int_to_itoa64 (l & 0x3f); l >>= 6;
2285 buf[26] = int_to_itoa64 (l & 0x3f); l >>= 6;
2286 buf[27] = int_to_itoa64 (l & 0x3f);
2287
2288 l = (digest[21] << 0) | (digest[22] << 8) | (digest[23] << 16);
2289
2290 buf[28] = int_to_itoa64 (l & 0x3f); l >>= 6;
2291 buf[29] = int_to_itoa64 (l & 0x3f); l >>= 6;
2292 buf[30] = int_to_itoa64 (l & 0x3f); l >>= 6;
2293 buf[31] = int_to_itoa64 (l & 0x3f);
2294
2295 l = (digest[24] << 0) | (digest[25] << 8) | (digest[26] << 16);
2296
2297 buf[32] = int_to_itoa64 (l & 0x3f); l >>= 6;
2298 buf[33] = int_to_itoa64 (l & 0x3f); l >>= 6;
2299 buf[34] = int_to_itoa64 (l & 0x3f); l >>= 6;
2300 buf[35] = int_to_itoa64 (l & 0x3f);
2301
2302 l = (digest[27] << 0) | (digest[28] << 8) | (digest[29] << 16);
2303
2304 buf[36] = int_to_itoa64 (l & 0x3f); l >>= 6;
2305 buf[37] = int_to_itoa64 (l & 0x3f); l >>= 6;
2306 buf[38] = int_to_itoa64 (l & 0x3f); l >>= 6;
2307 buf[39] = int_to_itoa64 (l & 0x3f);
2308
2309 l = (digest[30] << 0) | (digest[31] << 8) | (digest[32] << 16);
2310
2311 buf[40] = int_to_itoa64 (l & 0x3f); l >>= 6;
2312 buf[41] = int_to_itoa64 (l & 0x3f); l >>= 6;
2313 buf[42] = int_to_itoa64 (l & 0x3f); l >>= 6;
2314 //buf[43] = int_to_itoa64 (l & 0x3f);
2315 }
2316
2317 /**
2318 * tty
2319 */
2320
2321 #ifdef LINUX
2322 static struct termio savemodes;
2323 static int havemodes = 0;
2324
2325 int tty_break()
2326 {
2327 struct termio modmodes;
2328
2329 if (ioctl (fileno (stdin), TCGETA, &savemodes) < 0) return -1;
2330
2331 havemodes = 1;
2332
2333 modmodes = savemodes;
2334 modmodes.c_lflag &= ~ICANON;
2335 modmodes.c_cc[VMIN] = 1;
2336 modmodes.c_cc[VTIME] = 0;
2337
2338 return ioctl (fileno (stdin), TCSETAW, &modmodes);
2339 }
2340
2341 int tty_getchar()
2342 {
2343 fd_set rfds;
2344
2345 FD_ZERO (&rfds);
2346
2347 FD_SET (fileno (stdin), &rfds);
2348
2349 struct timeval tv;
2350
2351 tv.tv_sec = 1;
2352 tv.tv_usec = 0;
2353
2354 int retval = select (1, &rfds, NULL, NULL, &tv);
2355
2356 if (retval == 0) return 0;
2357 if (retval == -1) return -1;
2358
2359 return getchar();
2360 }
2361
2362 int tty_fix()
2363 {
2364 if (!havemodes) return 0;
2365
2366 return ioctl (fileno (stdin), TCSETAW, &savemodes);
2367 }
2368 #endif
2369
2370 #ifdef OSX
2371 static struct termios savemodes;
2372 static int havemodes = 0;
2373
2374 int tty_break()
2375 {
2376 struct termios modmodes;
2377
2378 if (ioctl (fileno (stdin), TIOCGETA, &savemodes) < 0) return -1;
2379
2380 havemodes = 1;
2381
2382 modmodes = savemodes;
2383 modmodes.c_lflag &= ~ICANON;
2384 modmodes.c_cc[VMIN] = 1;
2385 modmodes.c_cc[VTIME] = 0;
2386
2387 return ioctl (fileno (stdin), TIOCSETAW, &modmodes);
2388 }
2389
2390 int tty_getchar()
2391 {
2392 fd_set rfds;
2393
2394 FD_ZERO (&rfds);
2395
2396 FD_SET (fileno (stdin), &rfds);
2397
2398 struct timeval tv;
2399
2400 tv.tv_sec = 1;
2401 tv.tv_usec = 0;
2402
2403 int retval = select (1, &rfds, NULL, NULL, &tv);
2404
2405 if (retval == 0) return 0;
2406 if (retval == -1) return -1;
2407
2408 return getchar();
2409 }
2410
2411 int tty_fix()
2412 {
2413 if (!havemodes) return 0;
2414
2415 return ioctl (fileno (stdin), TIOCSETAW, &savemodes);
2416 }
2417 #endif
2418
2419 #ifdef WIN
2420 static DWORD saveMode = 0;
2421
2422 int tty_break()
2423 {
2424 HANDLE stdinHandle = GetStdHandle (STD_INPUT_HANDLE);
2425
2426 GetConsoleMode (stdinHandle, &saveMode);
2427 SetConsoleMode (stdinHandle, ENABLE_PROCESSED_INPUT);
2428
2429 return 0;
2430 }
2431
2432 int tty_getchar()
2433 {
2434 HANDLE stdinHandle = GetStdHandle (STD_INPUT_HANDLE);
2435
2436 DWORD rc = WaitForSingleObject (stdinHandle, 1000);
2437
2438 if (rc == WAIT_TIMEOUT) return 0;
2439 if (rc == WAIT_ABANDONED) return -1;
2440 if (rc == WAIT_FAILED) return -1;
2441
2442 // The whole ReadConsoleInput () part is a workaround.
2443 // For some unknown reason, maybe a mingw bug, a random signal
2444 // is sent to stdin which unblocks WaitForSingleObject () and sets rc 0.
2445 // Then it wants to read with getche () a keyboard input
2446 // which has never been made.
2447
2448 INPUT_RECORD buf[100];
2449
2450 DWORD num = 0;
2451
2452 memset (buf, 0, sizeof (buf));
2453
2454 ReadConsoleInput (stdinHandle, buf, 100, &num);
2455
2456 FlushConsoleInputBuffer (stdinHandle);
2457
2458 for (uint i = 0; i < num; i++)
2459 {
2460 if (buf[i].EventType != KEY_EVENT) continue;
2461
2462 KEY_EVENT_RECORD KeyEvent = buf[i].Event.KeyEvent;
2463
2464 if (KeyEvent.bKeyDown != TRUE) continue;
2465
2466 return KeyEvent.uChar.AsciiChar;
2467 }
2468
2469 return 0;
2470 }
2471
2472 int tty_fix()
2473 {
2474 HANDLE stdinHandle = GetStdHandle (STD_INPUT_HANDLE);
2475
2476 SetConsoleMode (stdinHandle, saveMode);
2477
2478 return 0;
2479 }
2480 #endif
2481
2482 /**
2483 * mem alloc
2484 */
2485
2486 #define MSG_ENOMEM "Insufficient memory available"
2487
2488 void *mycalloc (size_t nmemb, size_t size)
2489 {
2490 void *p = calloc (nmemb, size);
2491
2492 if (p == NULL)
2493 {
2494 log_error ("ERROR: %s", MSG_ENOMEM);
2495
2496 exit (-1);
2497 }
2498
2499 return (p);
2500 }
2501
2502 void *mymalloc (size_t size)
2503 {
2504 void *p = malloc (size);
2505
2506 if (p == NULL)
2507 {
2508 log_error ("ERROR: %s", MSG_ENOMEM);
2509
2510 exit (-1);
2511 }
2512
2513 memset (p, 0, size);
2514
2515 return (p);
2516 }
2517
2518 void myfree (void *ptr)
2519 {
2520 if (ptr == NULL) return;
2521
2522 free (ptr);
2523 }
2524
2525 void *myrealloc (void *ptr, size_t oldsz, size_t add)
2526 {
2527 void *p = realloc (ptr, oldsz + add);
2528
2529 if (p == NULL)
2530 {
2531 log_error ("ERROR: %s", MSG_ENOMEM);
2532
2533 exit (-1);
2534 }
2535
2536 memset ((char *) p + oldsz, 0, add);
2537
2538 return (p);
2539 }
2540
2541 char *mystrdup (const char *s)
2542 {
2543 const size_t len = strlen (s);
2544
2545 char *b = (char *) mymalloc (len + 1);
2546
2547 memcpy (b, s, len);
2548
2549 return (b);
2550 }
2551
2552 FILE *logfile_open (char *logfile)
2553 {
2554 FILE *fp = fopen (logfile, "ab");
2555
2556 if (fp == NULL)
2557 {
2558 fp = stdout;
2559 }
2560
2561 return fp;
2562 }
2563
2564 void logfile_close (FILE *fp)
2565 {
2566 if (fp == stdout) return;
2567
2568 fclose (fp);
2569 }
2570
2571 void logfile_append (const char *fmt, ...)
2572 {
2573 if (data.logfile_disable == 1) return;
2574
2575 FILE *fp = logfile_open (data.logfile);
2576
2577 va_list ap;
2578
2579 va_start (ap, fmt);
2580
2581 vfprintf (fp, fmt, ap);
2582
2583 va_end (ap);
2584
2585 fputc ('\n', fp);
2586
2587 fflush (fp);
2588
2589 logfile_close (fp);
2590 }
2591
2592 int logfile_generate_id ()
2593 {
2594 const int n = rand ();
2595
2596 time_t t;
2597
2598 time (&t);
2599
2600 return t + n;
2601 }
2602
2603 char *logfile_generate_topid ()
2604 {
2605 const int id = logfile_generate_id ();
2606
2607 char *topid = (char *) mymalloc (1 + 16 + 1);
2608
2609 snprintf (topid, 1 + 16, "TOP%08x", id);
2610
2611 return topid;
2612 }
2613
2614 char *logfile_generate_subid ()
2615 {
2616 const int id = logfile_generate_id ();
2617
2618 char *subid = (char *) mymalloc (1 + 16 + 1);
2619
2620 snprintf (subid, 1 + 16, "SUB%08x", id);
2621
2622 return subid;
2623 }
2624
2625 /**
2626 * system
2627 */
2628
2629 #if F_SETLKW
2630 void lock_file (FILE *fp)
2631 {
2632 struct flock lock;
2633
2634 memset (&lock, 0, sizeof (struct flock));
2635
2636 lock.l_type = F_WRLCK;
2637 while (fcntl(fileno(fp), F_SETLKW, &lock))
2638 {
2639 if (errno != EINTR)
2640 {
2641 log_error ("ERROR: failed acquiring write lock: %s", strerror (errno));
2642
2643 exit (-1);
2644 }
2645 }
2646 }
2647
2648 void unlock_file (FILE *fp)
2649 {
2650 struct flock lock;
2651
2652 memset (&lock, 0, sizeof (struct flock));
2653
2654 lock.l_type = F_UNLCK;
2655 fcntl(fileno(fp), F_SETLK, &lock);
2656 }
2657 #endif // F_SETLKW
2658
2659 #ifdef _WIN
2660 void fsync (int fd)
2661 {
2662 HANDLE h = (HANDLE) _get_osfhandle (fd);
2663
2664 FlushFileBuffers (h);
2665 }
2666 #endif
2667
2668 /**
2669 * thermal
2670 */
2671
2672 #ifdef HAVE_HWMON
2673 #if defined(_WIN) && defined(HAVE_NVAPI)
2674 int hm_get_adapter_index_nv (HM_ADAPTER_NV nvGPUHandle[DEVICES_MAX])
2675 {
2676 NvU32 pGpuCount;
2677
2678 if (hm_NvAPI_EnumPhysicalGPUs (data.hm_nv, nvGPUHandle, &pGpuCount) != NVAPI_OK) return (0);
2679
2680 if (pGpuCount == 0)
2681 {
2682 log_info ("WARN: No NvAPI adapters found");
2683
2684 return (0);
2685 }
2686
2687 return (pGpuCount);
2688 }
2689 #endif // _WIN && HAVE_NVAPI
2690
2691 #if defined(LINUX) && defined(HAVE_NVML)
2692 int hm_get_adapter_index_nv (HM_ADAPTER_NV nvGPUHandle[DEVICES_MAX])
2693 {
2694 int pGpuCount = 0;
2695
2696 for (uint i = 0; i < DEVICES_MAX; i++)
2697 {
2698 if (hm_NVML_nvmlDeviceGetHandleByIndex (data.hm_nv, 1, i, &nvGPUHandle[i]) != NVML_SUCCESS) break;
2699
2700 // can be used to determine if the device by index matches the cuda device by index
2701 // char name[100]; memset (name, 0, sizeof (name));
2702 // hm_NVML_nvmlDeviceGetName (data.hm_nv, nvGPUHandle[i], name, sizeof (name) - 1);
2703
2704 pGpuCount++;
2705 }
2706
2707 if (pGpuCount == 0)
2708 {
2709 log_info ("WARN: No NVML adapters found");
2710
2711 return (0);
2712 }
2713
2714 return (pGpuCount);
2715 }
2716 #endif // LINUX && HAVE_NVML
2717
2718 #ifdef HAVE_ADL
2719 int get_adapters_num_amd (void *adl, int *iNumberAdapters)
2720 {
2721 if (hm_ADL_Adapter_NumberOfAdapters_Get ((ADL_PTR *) adl, iNumberAdapters) != ADL_OK) return -1;
2722
2723 if (iNumberAdapters == 0)
2724 {
2725 log_info ("WARN: No ADL adapters found.");
2726
2727 return -1;
2728 }
2729
2730 return 0;
2731 }
2732
2733 /*
2734 int hm_show_performance_level (HM_LIB hm_dll, int iAdapterIndex)
2735 {
2736 ADLODPerformanceLevels *lpOdPerformanceLevels = NULL;
2737 ADLODParameters lpOdParameters;
2738
2739 lpOdParameters.iSize = sizeof (ADLODParameters);
2740 size_t plevels_size = 0;
2741
2742 if (hm_ADL_Overdrive_ODParameters_Get (hm_dll, iAdapterIndex, &lpOdParameters) != ADL_OK) return -1;
2743
2744 log_info ("[DEBUG] %s, adapter %d performance level (%d) : %s %s",
2745 __func__, iAdapterIndex,
2746 lpOdParameters.iNumberOfPerformanceLevels,
2747 (lpOdParameters.iActivityReportingSupported) ? "activity reporting" : "",
2748 (lpOdParameters.iDiscretePerformanceLevels) ? "discrete performance levels" : "performance ranges");
2749
2750 plevels_size = sizeof (ADLODPerformanceLevels) + sizeof (ADLODPerformanceLevel) * (lpOdParameters.iNumberOfPerformanceLevels - 1);
2751
2752 lpOdPerformanceLevels = (ADLODPerformanceLevels *) mymalloc (plevels_size);
2753
2754 lpOdPerformanceLevels->iSize = sizeof (ADLODPerformanceLevels) + sizeof (ADLODPerformanceLevel) * (lpOdParameters.iNumberOfPerformanceLevels - 1);
2755
2756 if (hm_ADL_Overdrive_ODPerformanceLevels_Get (hm_dll, iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK) return -1;
2757
2758 for (int j = 0; j < lpOdParameters.iNumberOfPerformanceLevels; j++)
2759 log_info ("[DEBUG] %s, adapter %d, level %d : engine %d, memory %d, voltage: %d",
2760 __func__, iAdapterIndex, j,
2761 lpOdPerformanceLevels->aLevels[j].iEngineClock / 100, lpOdPerformanceLevels->aLevels[j].iMemoryClock / 100, lpOdPerformanceLevels->aLevels[j].iVddc);
2762
2763 myfree (lpOdPerformanceLevels);
2764
2765 return 0;
2766 }
2767 */
2768
2769 LPAdapterInfo hm_get_adapter_info_amd (void *adl, int iNumberAdapters)
2770 {
2771 size_t AdapterInfoSize = iNumberAdapters * sizeof (AdapterInfo);
2772
2773 LPAdapterInfo lpAdapterInfo = (LPAdapterInfo) mymalloc (AdapterInfoSize);
2774
2775 if (hm_ADL_Adapter_AdapterInfo_Get ((ADL_PTR *) adl, lpAdapterInfo, AdapterInfoSize) != ADL_OK) return NULL;
2776
2777 return lpAdapterInfo;
2778 }
2779
2780 /*
2781 //
2782 // does not help at all, since AMD does not assign different bus id, device id when we have multi GPU setups
2783 //
2784
2785 int hm_get_opencl_device_index (hm_attrs_t *hm_device, uint num_adl_adapters, int bus_num, int dev_num)
2786 {
2787 u32 idx = -1;
2788
2789 for (uint i = 0; i < num_adl_adapters; i++)
2790 {
2791 int opencl_bus_num = hm_device[i].busid;
2792 int opencl_dev_num = hm_device[i].devid;
2793
2794 if ((opencl_bus_num == bus_num) && (opencl_dev_num == dev_num))
2795 {
2796 idx = i;
2797
2798 break;
2799 }
2800 }
2801
2802 if (idx >= DEVICES_MAX) return -1;
2803
2804 return idx;
2805 }
2806
2807 void hm_get_opencl_busid_devid (hm_attrs_t *hm_device, uint opencl_num_devices, cl_device_id *devices)
2808 {
2809 for (uint i = 0; i < opencl_num_devices; i++)
2810 {
2811 cl_device_topology_amd device_topology;
2812
2813 hc_clGetDeviceInfo (devices[i], CL_DEVICE_TOPOLOGY_AMD, sizeof (device_topology), &device_topology, NULL);
2814
2815 hm_device[i].busid = device_topology.pcie.bus;
2816 hm_device[i].devid = device_topology.pcie.device;
2817 }
2818 }
2819 */
2820
2821 void hm_sort_adl_adapters_by_busid_devid (u32 *valid_adl_device_list, int num_adl_adapters, LPAdapterInfo lpAdapterInfo)
2822 {
2823 // basically bubble sort
2824
2825 for (int i = 0; i < num_adl_adapters; i++)
2826 {
2827 for (int j = 0; j < num_adl_adapters - 1; j++)
2828 {
2829 // get info of adapter [x]
2830
2831 u32 adapter_index_x = valid_adl_device_list[j];
2832 AdapterInfo info_x = lpAdapterInfo[adapter_index_x];
2833
2834 u32 bus_num_x = info_x.iBusNumber;
2835 u32 dev_num_x = info_x.iDeviceNumber;
2836
2837 // get info of adapter [y]
2838
2839 u32 adapter_index_y = valid_adl_device_list[j + 1];
2840 AdapterInfo info_y = lpAdapterInfo[adapter_index_y];
2841
2842 u32 bus_num_y = info_y.iBusNumber;
2843 u32 dev_num_y = info_y.iDeviceNumber;
2844
2845 uint need_swap = 0;
2846
2847 if (bus_num_y < bus_num_x)
2848 {
2849 need_swap = 1;
2850 }
2851 else if (bus_num_y == bus_num_x)
2852 {
2853 if (dev_num_y < dev_num_x)
2854 {
2855 need_swap = 1;
2856 }
2857 }
2858
2859 if (need_swap == 1)
2860 {
2861 u32 temp = valid_adl_device_list[j + 1];
2862
2863 valid_adl_device_list[j + 1] = valid_adl_device_list[j];
2864 valid_adl_device_list[j + 0] = temp;
2865 }
2866 }
2867 }
2868 }
2869
2870 u32 *hm_get_list_valid_adl_adapters (int iNumberAdapters, int *num_adl_adapters, LPAdapterInfo lpAdapterInfo)
2871 {
2872 *num_adl_adapters = 0;
2873
2874 u32 *adl_adapters = NULL;
2875
2876 int *bus_numbers = NULL;
2877 int *device_numbers = NULL;
2878
2879 for (int i = 0; i < iNumberAdapters; i++)
2880 {
2881 AdapterInfo info = lpAdapterInfo[i];
2882
2883 if (strlen (info.strUDID) < 1) continue;
2884
2885 #ifdef WIN
2886 if (info.iVendorID != 1002) continue;
2887 #else
2888 if (info.iVendorID != 0x1002) continue;
2889 #endif
2890
2891 if (info.iBusNumber < 0) continue;
2892 if (info.iDeviceNumber < 0) continue;
2893
2894 int found = 0;
2895
2896 for (int pos = 0; pos < *num_adl_adapters; pos++)
2897 {
2898 if ((bus_numbers[pos] == info.iBusNumber) && (device_numbers[pos] == info.iDeviceNumber))
2899 {
2900 found = 1;
2901 break;
2902 }
2903 }
2904
2905 if (found) continue;
2906
2907 // add it to the list
2908
2909 adl_adapters = (u32 *) myrealloc (adl_adapters, (*num_adl_adapters) * sizeof (int), sizeof (int));
2910
2911 adl_adapters[*num_adl_adapters] = i;
2912
2913 // rest is just bookkeeping
2914
2915 bus_numbers = (int*) myrealloc (bus_numbers, (*num_adl_adapters) * sizeof (int), sizeof (int));
2916 device_numbers = (int*) myrealloc (device_numbers, (*num_adl_adapters) * sizeof (int), sizeof (int));
2917
2918 bus_numbers[*num_adl_adapters] = info.iBusNumber;
2919 device_numbers[*num_adl_adapters] = info.iDeviceNumber;
2920
2921 (*num_adl_adapters)++;
2922 }
2923
2924 myfree (bus_numbers);
2925 myfree (device_numbers);
2926
2927 // sort the list by increasing bus id, device id number
2928
2929 hm_sort_adl_adapters_by_busid_devid (adl_adapters, *num_adl_adapters, lpAdapterInfo);
2930
2931 return adl_adapters;
2932 }
2933
2934 int hm_check_fanspeed_control (void *adl, hm_attrs_t *hm_device, u32 *valid_adl_device_list, int num_adl_adapters, LPAdapterInfo lpAdapterInfo)
2935 {
2936 // loop through all valid devices
2937
2938 for (int i = 0; i < num_adl_adapters; i++)
2939 {
2940 u32 adapter_index = valid_adl_device_list[i];
2941
2942 // get AdapterInfo
2943
2944 AdapterInfo info = lpAdapterInfo[adapter_index];
2945
2946 // unfortunately this doesn't work since bus id and dev id are not unique
2947 // int opencl_device_index = hm_get_opencl_device_index (hm_device, num_adl_adapters, info.iBusNumber, info.iDeviceNumber);
2948 // if (opencl_device_index == -1) continue;
2949
2950 int opencl_device_index = i;
2951
2952 // if (hm_show_performance_level (adl, info.iAdapterIndex) != 0) return -1;
2953
2954 // get fanspeed info
2955
2956 if (hm_device[opencl_device_index].od_version == 5)
2957 {
2958 ADLFanSpeedInfo FanSpeedInfo;
2959
2960 memset (&FanSpeedInfo, 0, sizeof (ADLFanSpeedInfo));
2961
2962 FanSpeedInfo.iSize = sizeof (ADLFanSpeedInfo);
2963
2964 if (hm_ADL_Overdrive5_FanSpeedInfo_Get (adl, info.iAdapterIndex, 0, &FanSpeedInfo) != ADL_OK) return -1;
2965
2966 // check read and write capability in fanspeedinfo
2967
2968 if ((FanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_PERCENT_READ) &&
2969 (FanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_PERCENT_WRITE))
2970 {
2971 hm_device[opencl_device_index].fan_supported = 1;
2972 }
2973 else
2974 {
2975 hm_device[opencl_device_index].fan_supported = 0;
2976 }
2977 }
2978 else // od_version == 6
2979 {
2980 ADLOD6FanSpeedInfo faninfo;
2981
2982 memset (&faninfo, 0, sizeof (faninfo));
2983
2984 if (hm_ADL_Overdrive6_FanSpeed_Get (adl, info.iAdapterIndex, &faninfo) != ADL_OK) return -1;
2985
2986 // check read capability in fanspeedinfo
2987
2988 if (faninfo.iSpeedType & ADL_OD6_FANSPEED_TYPE_PERCENT)
2989 {
2990 hm_device[opencl_device_index].fan_supported = 1;
2991 }
2992 else
2993 {
2994 hm_device[opencl_device_index].fan_supported = 0;
2995 }
2996 }
2997 }
2998
2999 return 0;
3000 }
3001
3002 int hm_get_overdrive_version (void *adl, hm_attrs_t *hm_device, u32 *valid_adl_device_list, int num_adl_adapters, LPAdapterInfo lpAdapterInfo)
3003 {
3004 for (int i = 0; i < num_adl_adapters; i++)
3005 {
3006 u32 adapter_index = valid_adl_device_list[i];
3007
3008 // get AdapterInfo
3009
3010 AdapterInfo info = lpAdapterInfo[adapter_index];
3011
3012 // get overdrive version
3013
3014 int od_supported = 0;
3015 int od_enabled = 0;
3016 int od_version = 0;
3017
3018 if (hm_ADL_Overdrive_Caps (adl, info.iAdapterIndex, &od_supported, &od_enabled, &od_version) != ADL_OK) return -1;
3019
3020 // store the overdrive version in hm_device
3021
3022 // unfortunately this doesn't work since bus id and dev id are not unique
3023 // int opencl_device_index = hm_get_opencl_device_index (hm_device, num_adl_adapters, info.iBusNumber, info.iDeviceNumber);
3024 // if (opencl_device_index == -1) continue;
3025
3026 int opencl_device_index = i;
3027
3028 hm_device[opencl_device_index].od_version = od_version;
3029 }
3030
3031 return 0;
3032 }
3033
3034 int hm_get_adapter_index_amd (hm_attrs_t *hm_device, u32 *valid_adl_device_list, int num_adl_adapters, LPAdapterInfo lpAdapterInfo)
3035 {
3036 for (int i = 0; i < num_adl_adapters; i++)
3037 {
3038 u32 adapter_index = valid_adl_device_list[i];
3039
3040 // get AdapterInfo
3041
3042 AdapterInfo info = lpAdapterInfo[adapter_index];
3043
3044 // store the iAdapterIndex in hm_device
3045
3046 // unfortunately this doesn't work since bus id and dev id are not unique
3047 // int opencl_device_index = hm_get_opencl_device_index (hm_device, num_adl_adapters, info.iBusNumber, info.iDeviceNumber);
3048 // if (opencl_device_index == -1) continue;
3049
3050 int opencl_device_index = i;
3051
3052 hm_device[opencl_device_index].adapter_index.amd = info.iAdapterIndex;
3053 }
3054
3055 return num_adl_adapters;
3056 }
3057 #endif // HAVE_ADL
3058
3059 int hm_get_temperature_with_device_id (const uint device_id)
3060 {
3061 if ((data.devices_param[device_id].device_type & CL_DEVICE_TYPE_GPU) == 0) return -1;
3062
3063 #ifdef HAVE_ADL
3064 if (data.devices_param[device_id].vendor_id == VENDOR_ID_AMD)
3065 {
3066 if (data.hm_amd)
3067 {
3068 if (data.hm_device[device_id].od_version == 5)
3069 {
3070 ADLTemperature Temperature;
3071
3072 Temperature.iSize = sizeof (ADLTemperature);
3073
3074 if (hm_ADL_Overdrive5_Temperature_Get (data.hm_amd, data.hm_device[device_id].adapter_index.amd, 0, &Temperature) != ADL_OK) return -1;
3075
3076 return Temperature.iTemperature / 1000;
3077 }
3078 else if (data.hm_device[device_id].od_version == 6)
3079 {
3080 int Temperature = 0;
3081
3082 if (hm_ADL_Overdrive6_Temperature_Get (data.hm_amd, data.hm_device[device_id].adapter_index.amd, &Temperature) != ADL_OK) return -1;
3083
3084 return Temperature / 1000;
3085 }
3086 }
3087 }
3088 #endif
3089
3090 #if defined(HAVE_NVML) || defined(HAVE_NVAPI)
3091 if (data.devices_param[device_id].vendor_id == VENDOR_ID_NV)
3092 {
3093 #if defined(LINUX) && defined(HAVE_NVML)
3094 int temperature = 0;
3095
3096 hm_NVML_nvmlDeviceGetTemperature (data.hm_nv, data.hm_device[device_id].adapter_index.nv, NVML_TEMPERATURE_GPU, (uint *) &temperature);
3097
3098 return temperature;
3099 #endif
3100
3101 #if defined(WIN) && defined(HAVE_NVAPI)
3102 NV_GPU_THERMAL_SETTINGS pThermalSettings;
3103
3104 pThermalSettings.version = NV_GPU_THERMAL_SETTINGS_VER;
3105 pThermalSettings.count = NVAPI_MAX_THERMAL_SENSORS_PER_GPU;
3106 pThermalSettings.sensor[0].controller = NVAPI_THERMAL_CONTROLLER_UNKNOWN;
3107 pThermalSettings.sensor[0].target = NVAPI_THERMAL_TARGET_GPU;
3108
3109 if (hm_NvAPI_GPU_GetThermalSettings (data.hm_nv, data.hm_device[device_id].adapter_index.nv, 0, &pThermalSettings) != NVAPI_OK) return -1;
3110
3111 return pThermalSettings.sensor[0].currentTemp;
3112 #endif // WIN && HAVE_NVAPI
3113 }
3114 #endif // HAVE_NVML || HAVE_NVAPI
3115
3116 return -1;
3117 }
3118
3119 int hm_get_fanspeed_with_device_id (const uint device_id)
3120 {
3121 // we shouldn't really need this extra CL_DEVICE_TYPE_GPU check, because fan_supported should not be set w/ CPUs
3122 if ((data.devices_param[device_id].device_type & CL_DEVICE_TYPE_GPU) == 0) return -1;
3123
3124 if (data.hm_device[device_id].fan_supported == 1)
3125 {
3126 #ifdef HAVE_ADL
3127 if (data.devices_param[device_id].vendor_id == VENDOR_ID_AMD)
3128 {
3129 if (data.hm_amd)
3130 {
3131 if (data.hm_device[device_id].od_version == 5)
3132 {
3133 ADLFanSpeedValue lpFanSpeedValue;
3134
3135 memset (&lpFanSpeedValue, 0, sizeof (lpFanSpeedValue));
3136
3137 lpFanSpeedValue.iSize = sizeof (lpFanSpeedValue);
3138 lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
3139 lpFanSpeedValue.iFlags = ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED;
3140
3141 if (hm_ADL_Overdrive5_FanSpeed_Get (data.hm_amd, data.hm_device[device_id].adapter_index.amd, 0, &lpFanSpeedValue) != ADL_OK) return -1;
3142
3143 return lpFanSpeedValue.iFanSpeed;
3144 }
3145 else // od_version == 6
3146 {
3147 ADLOD6FanSpeedInfo faninfo;
3148
3149 memset (&faninfo, 0, sizeof (faninfo));
3150
3151 if (hm_ADL_Overdrive6_FanSpeed_Get (data.hm_amd, data.hm_device[device_id].adapter_index.amd, &faninfo) != ADL_OK) return -1;
3152
3153 return faninfo.iFanSpeedPercent;
3154 }
3155 }
3156 }
3157 #endif // HAVE_ADL
3158
3159 #if defined(HAVE_NVML) || defined(HAVE_NVAPI)
3160 if (data.devices_param[device_id].vendor_id == VENDOR_ID_NV)
3161 {
3162 #if defined(LINUX) && defined(HAVE_NVML)
3163 int speed = 0;
3164
3165 hm_NVML_nvmlDeviceGetFanSpeed (data.hm_nv, 1, data.hm_device[device_id].adapter_index.nv, (uint *) &speed);
3166
3167 return speed;
3168 #endif
3169
3170 #if defined(WIN) && defined(HAVE_NVAPI)
3171
3172 NV_GPU_COOLER_SETTINGS pCoolerSettings;
3173
3174 pCoolerSettings.Version = GPU_COOLER_SETTINGS_VER | sizeof (NV_GPU_COOLER_SETTINGS);
3175
3176 hm_NvAPI_GPU_GetCoolerSettings (data.hm_nv, data.hm_device[device_id].adapter_index.nv, 0, &pCoolerSettings);
3177
3178 return pCoolerSettings.Cooler[0].CurrentLevel;
3179 #endif
3180 }
3181 #endif // HAVE_NVML || HAVE_NVAPI
3182 }
3183
3184 return -1;
3185 }
3186
3187 int hm_get_utilization_with_device_id (const uint device_id)
3188 {
3189 if ((data.devices_param[device_id].device_type & CL_DEVICE_TYPE_GPU) == 0) return -1;
3190
3191 #ifdef HAVE_ADL
3192 if (data.devices_param[device_id].vendor_id == VENDOR_ID_AMD)
3193 {
3194 if (data.hm_amd)
3195 {
3196 ADLPMActivity PMActivity;
3197
3198 PMActivity.iSize = sizeof (ADLPMActivity);
3199
3200 if (hm_ADL_Overdrive_CurrentActivity_Get (data.hm_amd, data.hm_device[device_id].adapter_index.amd, &PMActivity) != ADL_OK) return -1;
3201
3202 return PMActivity.iActivityPercent;
3203 }
3204 }
3205 #endif // HAVE_ADL
3206
3207 #if defined(HAVE_NVML) || defined(HAVE_NVAPI)
3208 if (data.devices_param[device_id].vendor_id == VENDOR_ID_NV)
3209 {
3210 #if defined(LINUX) && defined(HAVE_NVML)
3211 nvmlUtilization_t utilization;
3212
3213 hm_NVML_nvmlDeviceGetUtilizationRates (data.hm_nv, data.hm_device[device_id].adapter_index.nv, &utilization);
3214
3215 return utilization.gpu;
3216 #endif
3217
3218 #if defined(WIN) && defined(HAVE_NVAPI)
3219 NV_GPU_DYNAMIC_PSTATES_INFO_EX pDynamicPstatesInfoEx;
3220
3221 pDynamicPstatesInfoEx.version = NV_GPU_DYNAMIC_PSTATES_INFO_EX_VER;
3222
3223 if (hm_NvAPI_GPU_GetDynamicPstatesInfoEx (data.hm_nv, data.hm_device[device_id].adapter_index.nv, &pDynamicPstatesInfoEx) != NVAPI_OK) return -1;
3224
3225 return pDynamicPstatesInfoEx.utilization[0].percentage;
3226 #endif
3227 }
3228 #endif // HAVE_NVML || HAVE_NVAPI
3229
3230 return -1;
3231 }
3232
3233 #ifdef HAVE_ADL
3234 int hm_set_fanspeed_with_device_id_amd (const uint device_id, const int fanspeed)
3235 {
3236 if (data.hm_device[device_id].fan_supported == 1)
3237 {
3238 if (data.hm_amd)
3239 {
3240 if (data.hm_device[device_id].od_version == 5)
3241 {
3242 ADLFanSpeedValue lpFanSpeedValue;
3243
3244 memset (&lpFanSpeedValue, 0, sizeof (lpFanSpeedValue));
3245
3246 lpFanSpeedValue.iSize = sizeof (lpFanSpeedValue);
3247 lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
3248 lpFanSpeedValue.iFlags = ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED;
3249 lpFanSpeedValue.iFanSpeed = fanspeed;
3250
3251 if (hm_ADL_Overdrive5_FanSpeed_Set (data.hm_amd, data.hm_device[device_id].adapter_index.amd, 0, &lpFanSpeedValue) != ADL_OK) return -1;
3252
3253 return 0;
3254 }
3255 else // od_version == 6
3256 {
3257 ADLOD6FanSpeedValue fan_speed_value;
3258
3259 memset (&fan_speed_value, 0, sizeof (fan_speed_value));
3260
3261 fan_speed_value.iSpeedType = ADL_OD6_FANSPEED_TYPE_PERCENT;
3262 fan_speed_value.iFanSpeed = fanspeed;
3263
3264 if (hm_ADL_Overdrive6_FanSpeed_Set (data.hm_amd, data.hm_device[device_id].adapter_index.amd, &fan_speed_value) != ADL_OK) return -1;
3265
3266 return 0;
3267 }
3268 }
3269 }
3270
3271 return -1;
3272 }
3273 #endif
3274
3275 // helper function for status display
3276
3277 void hm_device_val_to_str (char *target_buf, int max_buf_size, char *suffix, int value)
3278 {
3279 #define VALUE_NOT_AVAILABLE "N/A"
3280
3281 if (value == -1)
3282 {
3283 snprintf (target_buf, max_buf_size, VALUE_NOT_AVAILABLE);
3284 }
3285 else
3286 {
3287 snprintf (target_buf, max_buf_size, "%2d%s", value, suffix);
3288 }
3289 }
3290 #endif // HAVE_HWMON
3291
3292 /**
3293 * maskprocessor
3294 */
3295
3296 void mp_css_to_uniq_tbl (uint css_cnt, cs_t *css, uint uniq_tbls[SP_PW_MAX][CHARSIZ])
3297 {
3298 /* generates a lookup table where key is the char itself for fastest possible lookup performance */
3299
3300 if (css_cnt > SP_PW_MAX)
3301 {
3302 log_error ("ERROR: mask length is too long");
3303
3304 exit (-1);
3305 }
3306
3307 for (uint css_pos = 0; css_pos < css_cnt; css_pos++)
3308 {
3309 uint *uniq_tbl = uniq_tbls[css_pos];
3310
3311 uint *cs_buf = css[css_pos].cs_buf;
3312 uint cs_len = css[css_pos].cs_len;
3313
3314 for (uint cs_pos = 0; cs_pos < cs_len; cs_pos++)
3315 {
3316 uint c = cs_buf[cs_pos] & 0xff;
3317
3318 uniq_tbl[c] = 1;
3319 }
3320 }
3321 }
3322
3323 void mp_add_cs_buf (uint *in_buf, size_t in_len, cs_t *css, int css_cnt)
3324 {
3325 cs_t *cs = &css[css_cnt];
3326
3327 size_t css_uniq_sz = CHARSIZ * sizeof (uint);
3328
3329 uint *css_uniq = (uint *) mymalloc (css_uniq_sz);
3330
3331 size_t i;
3332
3333 for (i = 0; i < cs->cs_len; i++)
3334 {
3335 const uint u = cs->cs_buf[i];
3336
3337 css_uniq[u] = 1;
3338 }
3339
3340 for (i = 0; i < in_len; i++)
3341 {
3342 uint u = in_buf[i] & 0xff;
3343
3344 if (data.opts_type & OPTS_TYPE_PT_UPPER) u = toupper (u);
3345
3346 if (css_uniq[u] == 1) continue;
3347
3348 css_uniq[u] = 1;
3349
3350 cs->cs_buf[cs->cs_len] = u;
3351
3352 cs->cs_len++;
3353 }
3354
3355 myfree (css_uniq);
3356 }
3357
3358 void mp_expand (char *in_buf, size_t in_len, cs_t *mp_sys, cs_t *mp_usr, int mp_usr_offset, int interpret)
3359 {
3360 size_t in_pos;
3361
3362 for (in_pos = 0; in_pos < in_len; in_pos++)
3363 {
3364 uint p0 = in_buf[in_pos] & 0xff;
3365
3366 if (interpret == 1 && p0 == '?')
3367 {
3368 in_pos++;
3369
3370 if (in_pos == in_len) break;
3371
3372 uint p1 = in_buf[in_pos] & 0xff;
3373
3374 switch (p1)
3375 {
3376 case 'l': mp_add_cs_buf (mp_sys[0].cs_buf, mp_sys[0].cs_len, mp_usr, mp_usr_offset);
3377 break;
3378 case 'u': mp_add_cs_buf (mp_sys[1].cs_buf, mp_sys[1].cs_len, mp_usr, mp_usr_offset);
3379 break;
3380 case 'd': mp_add_cs_buf (mp_sys[2].cs_buf, mp_sys[2].cs_len, mp_usr, mp_usr_offset);
3381 break;
3382 case 's': mp_add_cs_buf (mp_sys[3].cs_buf, mp_sys[3].cs_len, mp_usr, mp_usr_offset);
3383 break;
3384 case 'a': mp_add_cs_buf (mp_sys[4].cs_buf, mp_sys[4].cs_len, mp_usr, mp_usr_offset);
3385 break;
3386 case 'b': mp_add_cs_buf (mp_sys[5].cs_buf, mp_sys[5].cs_len, mp_usr, mp_usr_offset);
3387 break;
3388 case '1': if (mp_usr[0].cs_len == 0) { log_error ("ERROR: Custom-charset 1 is undefined\n"); exit (-1); }
3389 mp_add_cs_buf (mp_usr[0].cs_buf, mp_usr[0].cs_len, mp_usr, mp_usr_offset);
3390 break;
3391 case '2': if (mp_usr[1].cs_len == 0) { log_error ("ERROR: Custom-charset 2 is undefined\n"); exit (-1); }
3392 mp_add_cs_buf (mp_usr[1].cs_buf, mp_usr[1].cs_len, mp_usr, mp_usr_offset);
3393 break;
3394 case '3': if (mp_usr[2].cs_len == 0) { log_error ("ERROR: Custom-charset 3 is undefined\n"); exit (-1); }
3395 mp_add_cs_buf (mp_usr[2].cs_buf, mp_usr[2].cs_len, mp_usr, mp_usr_offset);
3396 break;
3397 case '4': if (mp_usr[3].cs_len == 0) { log_error ("ERROR: Custom-charset 4 is undefined\n"); exit (-1); }
3398 mp_add_cs_buf (mp_usr[3].cs_buf, mp_usr[3].cs_len, mp_usr, mp_usr_offset);
3399 break;
3400 case '?': mp_add_cs_buf (&p0, 1, mp_usr, mp_usr_offset);
3401 break;
3402 default: log_error ("Syntax error: %s", in_buf);
3403 exit (-1);
3404 }
3405 }
3406 else
3407 {
3408 if (data.hex_charset)
3409 {
3410 in_pos++;
3411
3412 if (in_pos == in_len)
3413 {
3414 log_error ("ERROR: the hex-charset option always expects couples of exactly 2 hexadecimal chars, failed mask: %s", in_buf);
3415
3416 exit (-1);
3417 }
3418
3419 uint p1 = in_buf[in_pos] & 0xff;
3420
3421 if ((is_valid_hex_char (p0) == 0) || (is_valid_hex_char (p1) == 0))
3422 {
3423 log_error ("ERROR: invalid hex character detected in mask %s", in_buf);
3424
3425 exit (-1);
3426 }
3427
3428 uint chr = 0;
3429
3430 chr = hex_convert (p1) << 0;
3431 chr |= hex_convert (p0) << 4;
3432
3433 mp_add_cs_buf (&chr, 1, mp_usr, mp_usr_offset);
3434 }
3435 else
3436 {
3437 uint chr = p0;
3438
3439 mp_add_cs_buf (&chr, 1, mp_usr, mp_usr_offset);
3440 }
3441 }
3442 }
3443 }
3444
3445 u64 mp_get_sum (uint css_cnt, cs_t *css)
3446 {
3447 u64 sum = 1;
3448
3449 for (uint css_pos = 0; css_pos < css_cnt; css_pos++)
3450 {
3451 sum *= css[css_pos].cs_len;
3452 }
3453
3454 return (sum);
3455 }
3456
3457 cs_t *mp_gen_css (char *mask_buf, size_t mask_len, cs_t *mp_sys, cs_t *mp_usr, uint *css_cnt)
3458 {
3459 cs_t *css = (cs_t *) mycalloc (256, sizeof (cs_t));
3460
3461 uint mask_pos;
3462 uint css_pos;
3463
3464 for (mask_pos = 0, css_pos = 0; mask_pos < mask_len; mask_pos++, css_pos++)
3465 {
3466 char p0 = mask_buf[mask_pos];
3467
3468 if (p0 == '?')
3469 {
3470 mask_pos++;
3471
3472 if (mask_pos == mask_len) break;
3473
3474 char p1 = mask_buf[mask_pos];
3475
3476 uint chr = p1;
3477
3478 switch (p1)
3479 {
3480 case 'l': mp_add_cs_buf (mp_sys[0].cs_buf, mp_sys[0].cs_len, css, css_pos);
3481 break;
3482 case 'u': mp_add_cs_buf (mp_sys[1].cs_buf, mp_sys[1].cs_len, css, css_pos);
3483 break;
3484 case 'd': mp_add_cs_buf (mp_sys[2].cs_buf, mp_sys[2].cs_len, css, css_pos);
3485 break;
3486 case 's': mp_add_cs_buf (mp_sys[3].cs_buf, mp_sys[3].cs_len, css, css_pos);
3487 break;
3488 case 'a': mp_add_cs_buf (mp_sys[4].cs_buf, mp_sys[4].cs_len, css, css_pos);
3489 break;
3490 case 'b': mp_add_cs_buf (mp_sys[5].cs_buf, mp_sys[5].cs_len, css, css_pos);
3491 break;
3492 case '1': if (mp_usr[0].cs_len == 0) { log_error ("ERROR: Custom-charset 1 is undefined\n"); exit (-1); }
3493 mp_add_cs_buf (mp_usr[0].cs_buf, mp_usr[0].cs_len, css, css_pos);
3494 break;
3495 case '2': if (mp_usr[1].cs_len == 0) { log_error ("ERROR: Custom-charset 2 is undefined\n"); exit (-1); }
3496 mp_add_cs_buf (mp_usr[1].cs_buf, mp_usr[1].cs_len, css, css_pos);
3497 break;
3498 case '3': if (mp_usr[2].cs_len == 0) { log_error ("ERROR: Custom-charset 3 is undefined\n"); exit (-1); }
3499 mp_add_cs_buf (mp_usr[2].cs_buf, mp_usr[2].cs_len, css, css_pos);
3500 break;
3501 case '4': if (mp_usr[3].cs_len == 0) { log_error ("ERROR: Custom-charset 4 is undefined\n"); exit (-1); }
3502 mp_add_cs_buf (mp_usr[3].cs_buf, mp_usr[3].cs_len, css, css_pos);
3503 break;
3504 case '?': mp_add_cs_buf (&chr, 1, css, css_pos);
3505 break;
3506 default: log_error ("ERROR: syntax error: %s", mask_buf);
3507 exit (-1);
3508 }
3509 }
3510 else
3511 {
3512 if (data.hex_charset)
3513 {
3514 mask_pos++;
3515
3516 // if there is no 2nd hex character, show an error:
3517
3518 if (mask_pos == mask_len)
3519 {
3520 log_error ("ERROR: the hex-charset option always expects couples of exactly 2 hexadecimal chars, failed mask: %s", mask_buf);
3521
3522 exit (-1);
3523 }
3524
3525 char p1 = mask_buf[mask_pos];
3526
3527 // if they are not valid hex character, show an error:
3528
3529 if ((is_valid_hex_char (p0) == 0) || (is_valid_hex_char (p1) == 0))
3530 {
3531 log_error ("ERROR: invalid hex character detected in mask %s", mask_buf);
3532
3533 exit (-1);
3534 }
3535
3536 uint chr = 0;
3537
3538 chr |= hex_convert (p1) << 0;
3539 chr |= hex_convert (p0) << 4;
3540
3541 mp_add_cs_buf (&chr, 1, css, css_pos);
3542 }
3543 else
3544 {
3545 uint chr = p0;
3546
3547 mp_add_cs_buf (&chr, 1, css, css_pos);
3548 }
3549 }
3550 }
3551
3552 if (css_pos == 0)
3553 {
3554 log_error ("ERROR: invalid mask length (0)");
3555
3556 exit (-1);
3557 }
3558
3559 *css_cnt = css_pos;
3560
3561 return (css);
3562 }
3563
3564 void mp_exec (u64 val, char *buf, cs_t *css, int css_cnt)
3565 {
3566 for (int i = 0; i < css_cnt; i++)
3567 {
3568 uint len = css[i].cs_len;
3569 u64 next = val / len;
3570 uint pos = val % len;
3571 buf[i] = (char) css[i].cs_buf[pos] & 0xff;
3572 val = next;
3573 }
3574 }
3575
3576 void mp_cut_at (char *mask, uint max)
3577 {
3578 uint i;
3579 uint j;
3580 uint mask_len = strlen (mask);
3581
3582 for (i = 0, j = 0; i < mask_len && j < max; i++, j++)
3583 {
3584 if (mask[i] == '?') i++;
3585 }
3586
3587 mask[i] = 0;
3588 }
3589
3590 void mp_setup_sys (cs_t *mp_sys)
3591 {
3592 uint pos;
3593 uint chr;
3594 uint donec[CHARSIZ] = { 0 };
3595
3596 for (pos = 0, chr = 'a'; chr <= 'z'; chr++) { donec[chr] = 1;
3597 mp_sys[0].cs_buf[pos++] = chr;
3598 mp_sys[0].cs_len = pos; }
3599
3600 for (pos = 0, chr = 'A'; chr <= 'Z'; chr++) { donec[chr] = 1;
3601 mp_sys[1].cs_buf[pos++] = chr;
3602 mp_sys[1].cs_len = pos; }
3603
3604 for (pos = 0, chr = '0'; chr <= '9'; chr++) { donec[chr] = 1;
3605 mp_sys[2].cs_buf[pos++] = chr;
3606 mp_sys[2].cs_len = pos; }
3607
3608 for (pos = 0, chr = 0x20; chr <= 0x7e; chr++) { if (donec[chr]) continue;
3609 mp_sys[3].cs_buf[pos++] = chr;
3610 mp_sys[3].cs_len = pos; }
3611
3612 for (pos = 0, chr = 0x20; chr <= 0x7e; chr++) { mp_sys[4].cs_buf[pos++] = chr;
3613 mp_sys[4].cs_len = pos; }
3614
3615 for (pos = 0, chr = 0x00; chr <= 0xff; chr++) { mp_sys[5].cs_buf[pos++] = chr;
3616 mp_sys[5].cs_len = pos; }
3617 }
3618
3619 void mp_setup_usr (cs_t *mp_sys, cs_t *mp_usr, char *buf, uint index)
3620 {
3621 FILE *fp = fopen (buf, "rb");
3622
3623 if (fp == NULL || feof (fp)) // feof() in case if file is empty
3624 {
3625 mp_expand (buf, strlen (buf), mp_sys, mp_usr, index, 1);
3626 }
3627 else
3628 {
3629 char mp_file[1024] = { 0 };
3630
3631 size_t len = fread (mp_file, 1, sizeof (mp_file) - 1, fp);
3632
3633 fclose (fp);
3634
3635 len = in_superchop (mp_file);
3636
3637 if (len == 0)
3638 {
3639 log_info ("WARNING: charset file corrupted");
3640
3641 mp_expand (buf, strlen (buf), mp_sys, mp_usr, index, 1);
3642 }
3643 else
3644 {
3645 mp_expand (mp_file, len, mp_sys, mp_usr, index, 0);
3646 }
3647 }
3648 }
3649
3650 void mp_reset_usr (cs_t *mp_usr, uint index)
3651 {
3652 mp_usr[index].cs_len = 0;
3653
3654 memset (mp_usr[index].cs_buf, 0, sizeof (mp_usr[index].cs_buf));
3655 }
3656
3657 char *mp_get_truncated_mask (char *mask_buf, size_t mask_len, uint len)
3658 {
3659 char *new_mask_buf = (char *) mymalloc (256);
3660
3661 uint mask_pos;
3662
3663 uint css_pos;
3664
3665 for (mask_pos = 0, css_pos = 0; mask_pos < mask_len; mask_pos++, css_pos++)
3666 {
3667 if (css_pos == len) break;
3668
3669 char p0 = mask_buf[mask_pos];
3670
3671 new_mask_buf[mask_pos] = p0;
3672
3673 if (p0 == '?')
3674 {
3675 mask_pos++;
3676
3677 if (mask_pos == mask_len) break;
3678
3679 new_mask_buf[mask_pos] = mask_buf[mask_pos];
3680 }
3681 else
3682 {
3683 if (data.hex_charset)
3684 {
3685 mask_pos++;
3686
3687 if (mask_pos == mask_len)
3688 {
3689 log_error ("ERROR: the hex-charset option always expects couples of exactly 2 hexadecimal chars, failed mask: %s", mask_buf);
3690
3691 exit (-1);
3692 }
3693
3694 char p1 = mask_buf[mask_pos];
3695
3696 // if they are not valid hex character, show an error:
3697
3698 if ((is_valid_hex_char (p0) == 0) || (is_valid_hex_char (p1) == 0))
3699 {
3700 log_error ("ERROR: invalid hex character detected in mask: %s", mask_buf);
3701
3702 exit (-1);
3703 }
3704
3705 new_mask_buf[mask_pos] = p1;
3706 }
3707 }
3708 }
3709
3710 if (css_pos == len) return (new_mask_buf);
3711
3712 myfree (new_mask_buf);
3713
3714 return (NULL);
3715 }
3716
3717 /**
3718 * statprocessor
3719 */
3720
3721 u64 sp_get_sum (uint start, uint stop, cs_t *root_css_buf)
3722 {
3723 u64 sum = 1;
3724
3725 uint i;
3726
3727 for (i = start; i < stop; i++)
3728 {
3729 sum *= root_css_buf[i].cs_len;
3730 }
3731
3732 return (sum);
3733 }
3734
3735 void sp_exec (u64 ctx, char *pw_buf, cs_t *root_css_buf, cs_t *markov_css_buf, uint start, uint stop)
3736 {
3737 u64 v = ctx;
3738
3739 cs_t *cs = &root_css_buf[start];
3740
3741 uint i;
3742
3743 for (i = start; i < stop; i++)
3744 {
3745 const u64 m = v % cs->cs_len;
3746 const u64 d = v / cs->cs_len;
3747
3748 v = d;
3749
3750 const uint k = cs->cs_buf[m];
3751
3752 pw_buf[i - start] = (char) k;
3753
3754 cs = &markov_css_buf[(i * CHARSIZ) + k];
3755 }
3756 }
3757
3758 int sp_comp_val (const void *p1, const void *p2)
3759 {
3760 hcstat_table_t *b1 = (hcstat_table_t *) p1;
3761 hcstat_table_t *b2 = (hcstat_table_t *) p2;
3762
3763 return b2->val - b1->val;
3764 }
3765
3766 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)
3767 {
3768 uint i;
3769 uint j;
3770 uint k;
3771
3772 /**
3773 * Initialize hcstats
3774 */
3775
3776 u64 *root_stats_buf = (u64 *) mycalloc (SP_ROOT_CNT, sizeof (u64));
3777
3778 u64 *root_stats_ptr = root_stats_buf;
3779
3780 u64 *root_stats_buf_by_pos[SP_PW_MAX];
3781
3782 for (i = 0; i < SP_PW_MAX; i++)
3783 {
3784 root_stats_buf_by_pos[i] = root_stats_ptr;
3785
3786 root_stats_ptr += CHARSIZ;
3787 }
3788
3789 u64 *markov_stats_buf = (u64 *) mycalloc (SP_MARKOV_CNT, sizeof (u64));
3790
3791 u64 *markov_stats_ptr = markov_stats_buf;
3792
3793 u64 *markov_stats_buf_by_key[SP_PW_MAX][CHARSIZ];
3794
3795 for (i = 0; i < SP_PW_MAX; i++)
3796 {
3797 for (j = 0; j < CHARSIZ; j++)
3798 {
3799 markov_stats_buf_by_key[i][j] = markov_stats_ptr;
3800
3801 markov_stats_ptr += CHARSIZ;
3802 }
3803 }
3804
3805 /**
3806 * Load hcstats File
3807 */
3808
3809 if (hcstat == NULL)
3810 {
3811 char hcstat_tmp[256] = { 0 };
3812
3813 snprintf (hcstat_tmp, sizeof (hcstat_tmp) - 1, "%s/%s", shared_dir, SP_HCSTAT);
3814
3815 hcstat = hcstat_tmp;
3816 }
3817
3818 FILE *fd = fopen (hcstat, "rb");
3819
3820 if (fd == NULL)
3821 {
3822 log_error ("%s: %s", hcstat, strerror (errno));
3823
3824 exit (-1);
3825 }
3826
3827 if (fread (root_stats_buf, sizeof (u64), SP_ROOT_CNT, fd) != SP_ROOT_CNT)
3828 {
3829 log_error ("%s: Could not load data", hcstat);
3830
3831 fclose (fd);
3832
3833 exit (-1);
3834 }
3835
3836 if (fread (markov_stats_buf, sizeof (u64), SP_MARKOV_CNT, fd) != SP_MARKOV_CNT)
3837 {
3838 log_error ("%s: Could not load data", hcstat);
3839
3840 fclose (fd);
3841
3842 exit (-1);
3843 }
3844
3845 fclose (fd);
3846
3847 /**
3848 * Markov modifier of hcstat_table on user request
3849 */
3850
3851 if (disable)
3852 {
3853 memset (root_stats_buf, 0, SP_ROOT_CNT * sizeof (u64));
3854 memset (markov_stats_buf, 0, SP_MARKOV_CNT * sizeof (u64));
3855 }
3856
3857 if (classic)
3858 {
3859 /* Add all stats to first position */
3860
3861 for (i = 1; i < SP_PW_MAX; i++)
3862 {
3863 u64 *out = root_stats_buf_by_pos[0];
3864 u64 *in = root_stats_buf_by_pos[i];
3865
3866 for (j = 0; j < CHARSIZ; j++)
3867 {
3868 *out++ += *in++;
3869 }
3870 }
3871
3872 for (i = 1; i < SP_PW_MAX; i++)
3873 {
3874 u64 *out = markov_stats_buf_by_key[0][0];
3875 u64 *in = markov_stats_buf_by_key[i][0];
3876
3877 for (j = 0; j < CHARSIZ; j++)
3878 {
3879 for (k = 0; k < CHARSIZ; k++)
3880 {
3881 *out++ += *in++;
3882 }
3883 }
3884 }
3885
3886 /* copy them to all pw_positions */
3887
3888 for (i = 1; i < SP_PW_MAX; i++)
3889 {
3890 memcpy (root_stats_buf_by_pos[i], root_stats_buf_by_pos[0], CHARSIZ * sizeof (u64));
3891 }
3892
3893 for (i = 1; i < SP_PW_MAX; i++)
3894 {
3895 memcpy (markov_stats_buf_by_key[i][0], markov_stats_buf_by_key[0][0], CHARSIZ * CHARSIZ * sizeof (u64));
3896 }
3897 }
3898
3899 /**
3900 * Initialize tables
3901 */
3902
3903 hcstat_table_t *root_table_ptr = root_table_buf;
3904
3905 hcstat_table_t *root_table_buf_by_pos[SP_PW_MAX];
3906
3907 for (i = 0; i < SP_PW_MAX; i++)
3908 {
3909 root_table_buf_by_pos[i] = root_table_ptr;
3910
3911 root_table_ptr += CHARSIZ;
3912 }
3913
3914 hcstat_table_t *markov_table_ptr = markov_table_buf;
3915
3916 hcstat_table_t *markov_table_buf_by_key[SP_PW_MAX][CHARSIZ];
3917
3918 for (i = 0; i < SP_PW_MAX; i++)
3919 {
3920 for (j = 0; j < CHARSIZ; j++)
3921 {
3922 markov_table_buf_by_key[i][j] = markov_table_ptr;
3923
3924 markov_table_ptr += CHARSIZ;
3925 }
3926 }
3927
3928 /**
3929 * Convert hcstat to tables
3930 */
3931
3932 for (i = 0; i < SP_ROOT_CNT; i++)
3933 {
3934 uint key = i % CHARSIZ;
3935
3936 root_table_buf[i].key = key;
3937 root_table_buf[i].val = root_stats_buf[i];
3938 }
3939
3940 for (i = 0; i < SP_MARKOV_CNT; i++)
3941 {
3942 uint key = i % CHARSIZ;
3943
3944 markov_table_buf[i].key = key;
3945 markov_table_buf[i].val = markov_stats_buf[i];
3946 }
3947
3948 myfree (root_stats_buf);
3949 myfree (markov_stats_buf);
3950
3951 /**
3952 * Finally sort them
3953 */
3954
3955 for (i = 0; i < SP_PW_MAX; i++)
3956 {
3957 qsort (root_table_buf_by_pos[i], CHARSIZ, sizeof (hcstat_table_t), sp_comp_val);
3958 }
3959
3960 for (i = 0; i < SP_PW_MAX; i++)
3961 {
3962 for (j = 0; j < CHARSIZ; j++)
3963 {
3964 qsort (markov_table_buf_by_key[i][j], CHARSIZ, sizeof (hcstat_table_t), sp_comp_val);
3965 }
3966 }
3967 }
3968
3969 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])
3970 {
3971 /**
3972 * Convert tables to css
3973 */
3974
3975 for (uint i = 0; i < SP_ROOT_CNT; i++)
3976 {
3977 uint pw_pos = i / CHARSIZ;
3978
3979 cs_t *cs = &root_css_buf[pw_pos];
3980
3981 if (cs->cs_len == threshold) continue;
3982
3983 uint key = root_table_buf[i].key;
3984
3985 if (uniq_tbls[pw_pos][key] == 0) continue;
3986
3987 cs->cs_buf[cs->cs_len] = key;
3988
3989 cs->cs_len++;
3990 }
3991
3992 /**
3993 * Convert table to css
3994 */
3995
3996 for (uint i = 0; i < SP_MARKOV_CNT; i++)
3997 {
3998 uint c = i / CHARSIZ;
3999
4000 cs_t *cs = &markov_css_buf[c];
4001
4002 if (cs->cs_len == threshold) continue;
4003
4004 uint pw_pos = c / CHARSIZ;
4005
4006 uint key = markov_table_buf[i].key;
4007
4008 if ((pw_pos + 1) < SP_PW_MAX) if (uniq_tbls[pw_pos + 1][key] == 0) continue;
4009
4010 cs->cs_buf[cs->cs_len] = key;
4011
4012 cs->cs_len++;
4013 }
4014
4015 /*
4016 for (uint i = 0; i < 8; i++)
4017 {
4018 for (uint j = 0x20; j < 0x80; j++)
4019 {
4020 cs_t *ptr = &markov_css_buf[(i * CHARSIZ) + j];
4021
4022 printf ("pos:%u key:%u len:%u\n", i, j, ptr->cs_len);
4023
4024 for (uint k = 0; k < 10; k++)
4025 {
4026 printf (" %u\n", ptr->cs_buf[k]);
4027 }
4028 }
4029 }
4030 */
4031 }
4032
4033 void sp_stretch_root (hcstat_table_t *in, hcstat_table_t *out)
4034 {
4035 for (uint i = 0; i < SP_PW_MAX; i += 2)
4036 {
4037 memcpy (out, in, CHARSIZ * sizeof (hcstat_table_t));
4038
4039 out += CHARSIZ;
4040 in += CHARSIZ;
4041
4042 out->key = 0;
4043 out->val = 1;
4044
4045 out++;
4046
4047 for (uint j = 1; j < CHARSIZ; j++)
4048 {
4049 out->key = j;
4050 out->val = 0;
4051
4052 out++;
4053 }
4054 }
4055 }
4056
4057 void sp_stretch_markov (hcstat_table_t *in, hcstat_table_t *out)
4058 {
4059 for (uint i = 0; i < SP_PW_MAX; i += 2)
4060 {
4061 memcpy (out, in, CHARSIZ * CHARSIZ * sizeof (hcstat_table_t));
4062
4063 out += CHARSIZ * CHARSIZ;
4064 in += CHARSIZ * CHARSIZ;
4065
4066 for (uint j = 0; j < CHARSIZ; j++)
4067 {
4068 out->key = 0;
4069 out->val = 1;
4070
4071 out++;
4072
4073 for (uint k = 1; k < CHARSIZ; k++)
4074 {
4075 out->key = k;
4076 out->val = 0;
4077
4078 out++;
4079 }
4080 }
4081 }
4082 }
4083
4084 /**
4085 * mixed shared functions
4086 */
4087
4088 void dump_hex (const u8 *s, const int sz)
4089 {
4090 for (int i = 0; i < sz; i++)
4091 {
4092 log_info_nn ("%02x ", s[i]);
4093 }
4094
4095 log_info ("");
4096 }
4097
4098 void usage_mini_print (const char *progname)
4099 {
4100 for (uint i = 0; USAGE_MINI[i] != NULL; i++) log_info (USAGE_MINI[i], progname);
4101 }
4102
4103 void usage_big_print (const char *progname)
4104 {
4105 for (uint i = 0; USAGE_BIG[i] != NULL; i++) log_info (USAGE_BIG[i], progname);
4106 }
4107
4108 char *get_exec_path ()
4109 {
4110 int exec_path_len = 1024;
4111
4112 char *exec_path = (char *) mymalloc (exec_path_len);
4113
4114 #ifdef LINUX
4115
4116 char tmp[32] = { 0 };
4117
4118 snprintf (tmp, sizeof (tmp) - 1, "/proc/%d/exe", getpid ());
4119
4120 const int len = readlink (tmp, exec_path, exec_path_len - 1);
4121
4122 #elif WIN
4123
4124 const int len = GetModuleFileName (NULL, exec_path, exec_path_len - 1);
4125
4126 #elif OSX
4127
4128 uint size = exec_path_len;
4129
4130 if (_NSGetExecutablePath (exec_path, &size) != 0)
4131 {
4132 log_error("! executable path buffer too small\n");
4133
4134 exit (-1);
4135 }
4136
4137 const int len = strlen (exec_path);
4138
4139 #else
4140 #error Your Operating System is not supported or detected
4141 #endif
4142
4143 exec_path[len] = 0;
4144
4145 return exec_path;
4146 }
4147
4148 char *get_install_dir (const char *progname)
4149 {
4150 char *install_dir = mystrdup (progname);
4151 char *last_slash = NULL;
4152
4153 if ((last_slash = strrchr (install_dir, '/')) != NULL)
4154 {
4155 *last_slash = 0;
4156 }
4157 else if ((last_slash = strrchr (install_dir, '\\')) != NULL)
4158 {
4159 *last_slash = 0;
4160 }
4161 else
4162 {
4163 install_dir[0] = '.';
4164 install_dir[1] = 0;
4165 }
4166
4167 return (install_dir);
4168 }
4169
4170 char *get_profile_dir (const char *homedir)
4171 {
4172 #define DOT_HASHCAT ".hashcat"
4173
4174 size_t len = strlen (homedir) + 1 + strlen (DOT_HASHCAT) + 1;
4175
4176 char *profile_dir = (char *) mymalloc (len + 1);
4177
4178 snprintf (profile_dir, len, "%s/%s", homedir, DOT_HASHCAT);
4179
4180 return profile_dir;
4181 }
4182
4183 char *get_session_dir (const char *profile_dir)
4184 {
4185 #define SESSIONS_FOLDER "sessions"
4186
4187 size_t len = strlen (profile_dir) + 1 + strlen (SESSIONS_FOLDER) + 1;
4188
4189 char *session_dir = (char *) mymalloc (len + 1);
4190
4191 snprintf (session_dir, len, "%s/%s", profile_dir, SESSIONS_FOLDER);
4192
4193 return session_dir;
4194 }
4195
4196 uint count_lines (FILE *fd)
4197 {
4198 uint cnt = 0;
4199
4200 char *buf = (char *) mymalloc (BUFSIZ + 1);
4201
4202 char prev = '\n';
4203
4204 while (!feof (fd))
4205 {
4206 size_t nread = fread (buf, sizeof (char), BUFSIZ, fd);
4207
4208 if (nread < 1) continue;
4209
4210 size_t i;
4211
4212 for (i = 0; i < nread; i++)
4213 {
4214 if (prev == '\n') cnt++;
4215
4216 prev = buf[i];
4217 }
4218 }
4219
4220 myfree (buf);
4221
4222 return cnt;
4223 }
4224
4225 void truecrypt_crc32 (const char *filename, u8 keytab[64])
4226 {
4227 uint crc = ~0;
4228
4229 FILE *fd = fopen (filename, "rb");
4230
4231 if (fd == NULL)
4232 {
4233 log_error ("%s: %s", filename, strerror (errno));
4234
4235 exit (-1);
4236 }
4237
4238 #define MAX_KEY_SIZE (1024 * 1024)
4239
4240 u8 *buf = (u8 *) mymalloc (MAX_KEY_SIZE + 1);
4241
4242 int nread = fread (buf, sizeof (u8), MAX_KEY_SIZE, fd);
4243
4244 fclose (fd);
4245
4246 int kpos = 0;
4247
4248 for (int fpos = 0; fpos < nread; fpos++)
4249 {
4250 crc = crc32tab[(crc ^ buf[fpos]) & 0xff] ^ (crc >> 8);
4251
4252 keytab[kpos++] += (crc >> 24) & 0xff;
4253 keytab[kpos++] += (crc >> 16) & 0xff;
4254 keytab[kpos++] += (crc >> 8) & 0xff;
4255 keytab[kpos++] += (crc >> 0) & 0xff;
4256
4257 if (kpos >= 64) kpos = 0;
4258 }
4259
4260 myfree (buf);
4261 }
4262
4263 #ifdef OSX
4264 int pthread_setaffinity_np (pthread_t thread, size_t cpu_size, cpu_set_t *cpu_set)
4265 {
4266 int core;
4267
4268 for (core = 0; core < (8 * (int)cpu_size); core++)
4269 if (CPU_ISSET(core, cpu_set)) break;
4270
4271 thread_affinity_policy_data_t policy = { core };
4272
4273 const int rc = thread_policy_set (pthread_mach_thread_np (thread), THREAD_AFFINITY_POLICY, (thread_policy_t) &policy, 1);
4274
4275 if (data.quiet == 0)
4276 {
4277 if (rc != KERN_SUCCESS)
4278 {
4279 log_error ("ERROR: %s : %d", "thread_policy_set()", rc);
4280 }
4281 }
4282
4283 return rc;
4284 }
4285 #endif
4286
4287 void set_cpu_affinity (char *cpu_affinity)
4288 {
4289 #ifdef WIN
4290 DWORD_PTR aff_mask = 0;
4291 #elif _POSIX
4292 cpu_set_t cpuset;
4293 CPU_ZERO (&cpuset);
4294 #endif
4295
4296 if (cpu_affinity)
4297 {
4298 char *devices = strdup (cpu_affinity);
4299
4300 char *next = strtok (devices, ",");
4301
4302 do
4303 {
4304 uint cpu_id = atoi (next);
4305
4306 if (cpu_id == 0)
4307 {
4308 #ifdef WIN
4309 aff_mask = 0;
4310 #elif _POSIX
4311 CPU_ZERO (&cpuset);
4312 #endif
4313
4314 break;
4315 }
4316
4317 if (cpu_id > 32)
4318 {
4319 log_error ("ERROR: invalid cpu_id %u specified", cpu_id);
4320
4321 exit (-1);
4322 }
4323
4324 #ifdef WIN
4325 aff_mask |= 1 << (cpu_id - 1);
4326 #elif _POSIX
4327 CPU_SET ((cpu_id - 1), &cpuset);
4328 #endif
4329
4330 } while ((next = strtok (NULL, ",")) != NULL);
4331
4332 free (devices);
4333 }
4334
4335 #ifdef WIN
4336 SetProcessAffinityMask (GetCurrentProcess (), aff_mask);
4337 SetThreadAffinityMask (GetCurrentThread (), aff_mask);
4338 #elif _POSIX
4339 pthread_t thread = pthread_self ();
4340 pthread_setaffinity_np (thread, sizeof (cpu_set_t), &cpuset);
4341 #endif
4342 }
4343
4344 void *rulefind (const void *key, void *base, int nmemb, size_t size, int (*compar) (const void *, const void *))
4345 {
4346 char *element, *end;
4347
4348 end = (char *) base + nmemb * size;
4349
4350 for (element = (char *) base; element < end; element += size)
4351 if (!compar (element, key))
4352 return element;
4353
4354 return NULL;
4355 }
4356
4357 int sort_by_u32 (const void *v1, const void *v2)
4358 {
4359 const u32 *s1 = (const u32 *) v1;
4360 const u32 *s2 = (const u32 *) v2;
4361
4362 return *s1 - *s2;
4363 }
4364
4365 int sort_by_salt (const void *v1, const void *v2)
4366 {
4367 const salt_t *s1 = (const salt_t *) v1;
4368 const salt_t *s2 = (const salt_t *) v2;
4369
4370 const int res1 = s1->salt_len - s2->salt_len;
4371
4372 if (res1 != 0) return (res1);
4373
4374 const int res2 = s1->salt_iter - s2->salt_iter;
4375
4376 if (res2 != 0) return (res2);
4377
4378 uint n;
4379
4380 n = 16;
4381
4382 while (n--)
4383 {
4384 if (s1->salt_buf[n] > s2->salt_buf[n]) return ( 1);
4385 if (s1->salt_buf[n] < s2->salt_buf[n]) return (-1);
4386 }
4387
4388 n = 8;
4389
4390 while (n--)
4391 {
4392 if (s1->salt_buf_pc[n] > s2->salt_buf_pc[n]) return ( 1);
4393 if (s1->salt_buf_pc[n] < s2->salt_buf_pc[n]) return (-1);
4394 }
4395
4396 return (0);
4397 }
4398
4399 int sort_by_salt_buf (const void *v1, const void *v2)
4400 {
4401 const pot_t *p1 = (const pot_t *) v1;
4402 const pot_t *p2 = (const pot_t *) v2;
4403
4404 const hash_t *h1 = &p1->hash;
4405 const hash_t *h2 = &p2->hash;
4406
4407 const salt_t *s1 = h1->salt;
4408 const salt_t *s2 = h2->salt;
4409
4410 uint n = 16;
4411
4412 while (n--)
4413 {
4414 if (s1->salt_buf[n] > s2->salt_buf[n]) return ( 1);
4415 if (s1->salt_buf[n] < s2->salt_buf[n]) return (-1);
4416 }
4417
4418 return 0;
4419 }
4420
4421 int sort_by_hash_t_salt (const void *v1, const void *v2)
4422 {
4423 const hash_t *h1 = (const hash_t *) v1;
4424 const hash_t *h2 = (const hash_t *) v2;
4425
4426 const salt_t *s1 = h1->salt;
4427 const salt_t *s2 = h2->salt;
4428
4429 // testphase: this should work
4430 uint n = 16;
4431
4432 while (n--)
4433 {
4434 if (s1->salt_buf[n] > s2->salt_buf[n]) return ( 1);
4435 if (s1->salt_buf[n] < s2->salt_buf[n]) return (-1);
4436 }
4437
4438 /* original code, seems buggy since salt_len can be very big (had a case with 131 len)
4439 also it thinks salt_buf[x] is a char but its a uint so salt_len should be / 4
4440 if (s1->salt_len > s2->salt_len) return ( 1);
4441 if (s1->salt_len < s2->salt_len) return (-1);
4442
4443 uint n = s1->salt_len;
4444
4445 while (n--)
4446 {
4447 if (s1->salt_buf[n] > s2->salt_buf[n]) return ( 1);
4448 if (s1->salt_buf[n] < s2->salt_buf[n]) return (-1);
4449 }
4450 */
4451
4452 return 0;
4453 }
4454
4455 int sort_by_hash_t_salt_hccap (const void *v1, const void *v2)
4456 {
4457 const hash_t *h1 = (const hash_t *) v1;
4458 const hash_t *h2 = (const hash_t *) v2;
4459
4460 const salt_t *s1 = h1->salt;
4461 const salt_t *s2 = h2->salt;
4462
4463 // 16 - 2 (since last 2 uints contain the digest)
4464 uint n = 14;
4465
4466 while (n--)
4467 {
4468 if (s1->salt_buf[n] > s2->salt_buf[n]) return ( 1);
4469 if (s1->salt_buf[n] < s2->salt_buf[n]) return (-1);
4470 }
4471
4472 return 0;
4473 }
4474
4475 int sort_by_hash_no_salt (const void *v1, const void *v2)
4476 {
4477 const hash_t *h1 = (const hash_t *) v1;
4478 const hash_t *h2 = (const hash_t *) v2;
4479
4480 const void *d1 = h1->digest;
4481 const void *d2 = h2->digest;
4482
4483 return data.sort_by_digest (d1, d2);
4484 }
4485
4486 int sort_by_hash (const void *v1, const void *v2)
4487 {
4488 const hash_t *h1 = (const hash_t *) v1;
4489 const hash_t *h2 = (const hash_t *) v2;
4490
4491 if (data.isSalted)
4492 {
4493 const salt_t *s1 = h1->salt;
4494 const salt_t *s2 = h2->salt;
4495
4496 int res = sort_by_salt (s1, s2);
4497
4498 if (res != 0) return (res);
4499 }
4500
4501 const void *d1 = h1->digest;
4502 const void *d2 = h2->digest;
4503
4504 return data.sort_by_digest (d1, d2);
4505 }
4506
4507 int sort_by_pot (const void *v1, const void *v2)
4508 {
4509 const pot_t *p1 = (const pot_t *) v1;
4510 const pot_t *p2 = (const pot_t *) v2;
4511
4512 const hash_t *h1 = &p1->hash;
4513 const hash_t *h2 = &p2->hash;
4514
4515 return sort_by_hash (h1, h2);
4516 }
4517
4518 int sort_by_mtime (const void *p1, const void *p2)
4519 {
4520 const char **f1 = (const char **) p1;
4521 const char **f2 = (const char **) p2;
4522
4523 struct stat s1; stat (*f1, &s1);
4524 struct stat s2; stat (*f2, &s2);
4525
4526 return s2.st_mtime - s1.st_mtime;
4527 }
4528
4529 int sort_by_cpu_rule (const void *p1, const void *p2)
4530 {
4531 const cpu_rule_t *r1 = (const cpu_rule_t *) p1;
4532 const cpu_rule_t *r2 = (const cpu_rule_t *) p2;
4533
4534 return memcmp (r1, r2, sizeof (cpu_rule_t));
4535 }
4536
4537 int sort_by_kernel_rule (const void *p1, const void *p2)
4538 {
4539 const kernel_rule_t *r1 = (const kernel_rule_t *) p1;
4540 const kernel_rule_t *r2 = (const kernel_rule_t *) p2;
4541
4542 return memcmp (r1, r2, sizeof (kernel_rule_t));
4543 }
4544
4545 int sort_by_stringptr (const void *p1, const void *p2)
4546 {
4547 const char **s1 = (const char **) p1;
4548 const char **s2 = (const char **) p2;
4549
4550 return strcmp (*s1, *s2);
4551 }
4552
4553 int sort_by_dictstat (const void *s1, const void *s2)
4554 {
4555 dictstat_t *d1 = (dictstat_t *) s1;
4556 dictstat_t *d2 = (dictstat_t *) s2;
4557
4558 #ifdef LINUX
4559 d2->stat.st_atim = d1->stat.st_atim;
4560 #else
4561 d2->stat.st_atime = d1->stat.st_atime;
4562 #endif
4563
4564 return memcmp (&d1->stat, &d2->stat, sizeof (struct stat));
4565 }
4566
4567 int sort_by_bitmap (const void *p1, const void *p2)
4568 {
4569 const bitmap_result_t *b1 = (const bitmap_result_t *) p1;
4570 const bitmap_result_t *b2 = (const bitmap_result_t *) p2;
4571
4572 return b1->collisions - b2->collisions;
4573 }
4574
4575 int sort_by_digest_4_2 (const void *v1, const void *v2)
4576 {
4577 const u32 *d1 = (const u32 *) v1;
4578 const u32 *d2 = (const u32 *) v2;
4579
4580 uint n = 2;
4581
4582 while (n--)
4583 {
4584 if (d1[n] > d2[n]) return ( 1);
4585 if (d1[n] < d2[n]) return (-1);
4586 }
4587
4588 return (0);
4589 }
4590
4591 int sort_by_digest_4_4 (const void *v1, const void *v2)
4592 {
4593 const u32 *d1 = (const u32 *) v1;
4594 const u32 *d2 = (const u32 *) v2;
4595
4596 uint n = 4;
4597
4598 while (n--)
4599 {
4600 if (d1[n] > d2[n]) return ( 1);
4601 if (d1[n] < d2[n]) return (-1);
4602 }
4603
4604 return (0);
4605 }
4606
4607 int sort_by_digest_4_5 (const void *v1, const void *v2)
4608 {
4609 const u32 *d1 = (const u32 *) v1;
4610 const u32 *d2 = (const u32 *) v2;
4611
4612 uint n = 5;
4613
4614 while (n--)
4615 {
4616 if (d1[n] > d2[n]) return ( 1);
4617 if (d1[n] < d2[n]) return (-1);
4618 }
4619
4620 return (0);
4621 }
4622
4623 int sort_by_digest_4_6 (const void *v1, const void *v2)
4624 {
4625 const u32 *d1 = (const u32 *) v1;
4626 const u32 *d2 = (const u32 *) v2;
4627
4628 uint n = 6;
4629
4630 while (n--)
4631 {
4632 if (d1[n] > d2[n]) return ( 1);
4633 if (d1[n] < d2[n]) return (-1);
4634 }
4635
4636 return (0);
4637 }
4638
4639 int sort_by_digest_4_8 (const void *v1, const void *v2)
4640 {
4641 const u32 *d1 = (const u32 *) v1;
4642 const u32 *d2 = (const u32 *) v2;
4643
4644 uint n = 8;
4645
4646 while (n--)
4647 {
4648 if (d1[n] > d2[n]) return ( 1);
4649 if (d1[n] < d2[n]) return (-1);
4650 }
4651
4652 return (0);
4653 }
4654
4655 int sort_by_digest_4_16 (const void *v1, const void *v2)
4656 {
4657 const u32 *d1 = (const u32 *) v1;
4658 const u32 *d2 = (const u32 *) v2;
4659
4660 uint n = 16;
4661
4662 while (n--)
4663 {
4664 if (d1[n] > d2[n]) return ( 1);
4665 if (d1[n] < d2[n]) return (-1);
4666 }
4667
4668 return (0);
4669 }
4670
4671 int sort_by_digest_4_32 (const void *v1, const void *v2)
4672 {
4673 const u32 *d1 = (const u32 *) v1;
4674 const u32 *d2 = (const u32 *) v2;
4675
4676 uint n = 32;
4677
4678 while (n--)
4679 {
4680 if (d1[n] > d2[n]) return ( 1);
4681 if (d1[n] < d2[n]) return (-1);
4682 }
4683
4684 return (0);
4685 }
4686
4687 int sort_by_digest_4_64 (const void *v1, const void *v2)
4688 {
4689 const u32 *d1 = (const u32 *) v1;
4690 const u32 *d2 = (const u32 *) v2;
4691
4692 uint n = 64;
4693
4694 while (n--)
4695 {
4696 if (d1[n] > d2[n]) return ( 1);
4697 if (d1[n] < d2[n]) return (-1);
4698 }
4699
4700 return (0);
4701 }
4702
4703 int sort_by_digest_8_8 (const void *v1, const void *v2)
4704 {
4705 const u64 *d1 = (const u64 *) v1;
4706 const u64 *d2 = (const u64 *) v2;
4707
4708 uint n = 8;
4709
4710 while (n--)
4711 {
4712 if (d1[n] > d2[n]) return ( 1);
4713 if (d1[n] < d2[n]) return (-1);
4714 }
4715
4716 return (0);
4717 }
4718
4719 int sort_by_digest_8_16 (const void *v1, const void *v2)
4720 {
4721 const u64 *d1 = (const u64 *) v1;
4722 const u64 *d2 = (const u64 *) v2;
4723
4724 uint n = 16;
4725
4726 while (n--)
4727 {
4728 if (d1[n] > d2[n]) return ( 1);
4729 if (d1[n] < d2[n]) return (-1);
4730 }
4731
4732 return (0);
4733 }
4734
4735 int sort_by_digest_8_25 (const void *v1, const void *v2)
4736 {
4737 const u64 *d1 = (const u64 *) v1;
4738 const u64 *d2 = (const u64 *) v2;
4739
4740 uint n = 25;
4741
4742 while (n--)
4743 {
4744 if (d1[n] > d2[n]) return ( 1);
4745 if (d1[n] < d2[n]) return (-1);
4746 }
4747
4748 return (0);
4749 }
4750
4751 int sort_by_digest_p0p1 (const void *v1, const void *v2)
4752 {
4753 const u32 *d1 = (const u32 *) v1;
4754 const u32 *d2 = (const u32 *) v2;
4755
4756 const uint dgst_pos0 = data.dgst_pos0;
4757 const uint dgst_pos1 = data.dgst_pos1;
4758 const uint dgst_pos2 = data.dgst_pos2;
4759 const uint dgst_pos3 = data.dgst_pos3;
4760
4761 if (d1[dgst_pos3] > d2[dgst_pos3]) return ( 1);
4762 if (d1[dgst_pos3] < d2[dgst_pos3]) return (-1);
4763 if (d1[dgst_pos2] > d2[dgst_pos2]) return ( 1);
4764 if (d1[dgst_pos2] < d2[dgst_pos2]) return (-1);
4765 if (d1[dgst_pos1] > d2[dgst_pos1]) return ( 1);
4766 if (d1[dgst_pos1] < d2[dgst_pos1]) return (-1);
4767 if (d1[dgst_pos0] > d2[dgst_pos0]) return ( 1);
4768 if (d1[dgst_pos0] < d2[dgst_pos0]) return (-1);
4769
4770 return (0);
4771 }
4772
4773 int sort_by_tuning_db_alias (const void *v1, const void *v2)
4774 {
4775 const tuning_db_alias_t *t1 = (const tuning_db_alias_t *) v1;
4776 const tuning_db_alias_t *t2 = (const tuning_db_alias_t *) v2;
4777
4778 const int res1 = strcmp (t1->device_name, t2->device_name);
4779
4780 if (res1 != 0) return (res1);
4781
4782 return 0;
4783 }
4784
4785 int sort_by_tuning_db_entry (const void *v1, const void *v2)
4786 {
4787 const tuning_db_entry_t *t1 = (const tuning_db_entry_t *) v1;
4788 const tuning_db_entry_t *t2 = (const tuning_db_entry_t *) v2;
4789
4790 const int res1 = strcmp (t1->device_name, t2->device_name);
4791
4792 if (res1 != 0) return (res1);
4793
4794 const int res2 = t1->attack_mode
4795 - t2->attack_mode;
4796
4797 if (res2 != 0) return (res2);
4798
4799 const int res3 = t1->hash_type
4800 - t2->hash_type;
4801
4802 if (res3 != 0) return (res3);
4803
4804 return 0;
4805 }
4806
4807 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)
4808 {
4809 uint outfile_autohex = data.outfile_autohex;
4810
4811 unsigned char *rule_ptr = (unsigned char *) rule_buf;
4812
4813 FILE *debug_fp = NULL;
4814
4815 if (debug_file != NULL)
4816 {
4817 debug_fp = fopen (debug_file, "ab");
4818
4819 lock_file (debug_fp);
4820 }
4821 else
4822 {
4823 debug_fp = stderr;
4824 }
4825
4826 if (debug_fp == NULL)
4827 {
4828 log_info ("WARNING: Could not open debug-file for writing");
4829 }
4830 else
4831 {
4832 if ((debug_mode == 2) || (debug_mode == 3) || (debug_mode == 4))
4833 {
4834 format_plain (debug_fp, orig_plain_ptr, orig_plain_len, outfile_autohex);
4835
4836 if ((debug_mode == 3) || (debug_mode == 4)) fputc (':', debug_fp);
4837 }
4838
4839 fwrite (rule_ptr, rule_len, 1, debug_fp);
4840
4841 if (debug_mode == 4)
4842 {
4843 fputc (':', debug_fp);
4844
4845 format_plain (debug_fp, mod_plain_ptr, mod_plain_len, outfile_autohex);
4846 }
4847
4848 fputc ('\n', debug_fp);
4849
4850 if (debug_file != NULL) fclose (debug_fp);
4851 }
4852 }
4853
4854 void format_plain (FILE *fp, unsigned char *plain_ptr, uint plain_len, uint outfile_autohex)
4855 {
4856 int needs_hexify = 0;
4857
4858 if (outfile_autohex == 1)
4859 {
4860 for (uint i = 0; i < plain_len; i++)
4861 {
4862 if (plain_ptr[i] < 0x20)
4863 {
4864 needs_hexify = 1;
4865
4866 break;
4867 }
4868
4869 if (plain_ptr[i] > 0x7f)
4870 {
4871 needs_hexify = 1;
4872
4873 break;
4874 }
4875 }
4876 }
4877
4878 if (needs_hexify == 1)
4879 {
4880 fprintf (fp, "$HEX[");
4881
4882 for (uint i = 0; i < plain_len; i++)
4883 {
4884 fprintf (fp, "%02x", plain_ptr[i]);
4885 }
4886
4887 fprintf (fp, "]");
4888 }
4889 else
4890 {
4891 fwrite (plain_ptr, plain_len, 1, fp);
4892 }
4893 }
4894
4895 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)
4896 {
4897 uint outfile_format = data.outfile_format;
4898
4899 char separator = data.separator;
4900
4901 if (outfile_format & OUTFILE_FMT_HASH)
4902 {
4903 fprintf (out_fp, "%s", out_buf);
4904
4905 if (outfile_format & (OUTFILE_FMT_PLAIN | OUTFILE_FMT_HEXPLAIN | OUTFILE_FMT_CRACKPOS))
4906 {
4907 fputc (separator, out_fp);
4908 }
4909 }
4910 else if (data.username)
4911 {
4912 if (username != NULL)
4913 {
4914 for (uint i = 0; i < user_len; i++)
4915 {
4916 fprintf (out_fp, "%c", username[i]);
4917 }
4918
4919 if (outfile_format & (OUTFILE_FMT_PLAIN | OUTFILE_FMT_HEXPLAIN | OUTFILE_FMT_CRACKPOS))
4920 {
4921 fputc (separator, out_fp);
4922 }
4923 }
4924 }
4925
4926 if (outfile_format & OUTFILE_FMT_PLAIN)
4927 {
4928 format_plain (out_fp, plain_ptr, plain_len, data.outfile_autohex);
4929
4930 if (outfile_format & (OUTFILE_FMT_HEXPLAIN | OUTFILE_FMT_CRACKPOS))
4931 {
4932 fputc (separator, out_fp);
4933 }
4934 }
4935
4936 if (outfile_format & OUTFILE_FMT_HEXPLAIN)
4937 {
4938 for (uint i = 0; i < plain_len; i++)
4939 {
4940 fprintf (out_fp, "%02x", plain_ptr[i]);
4941 }
4942
4943 if (outfile_format & (OUTFILE_FMT_CRACKPOS))
4944 {
4945 fputc (separator, out_fp);
4946 }
4947 }
4948
4949 if (outfile_format & OUTFILE_FMT_CRACKPOS)
4950 {
4951 #ifdef _WIN
4952 __mingw_fprintf (out_fp, "%llu", crackpos);
4953 #endif
4954
4955 #ifdef _POSIX
4956 #ifdef __x86_64__
4957 fprintf (out_fp, "%lu", (unsigned long) crackpos);
4958 #else
4959 fprintf (out_fp, "%llu", crackpos);
4960 #endif
4961 #endif
4962 }
4963
4964 fputc ('\n', out_fp);
4965 }
4966
4967 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)
4968 {
4969 pot_t pot_key;
4970
4971 pot_key.hash.salt = hashes_buf->salt;
4972 pot_key.hash.digest = hashes_buf->digest;
4973
4974 pot_t *pot_ptr = (pot_t *) bsearch (&pot_key, pot, pot_cnt, sizeof (pot_t), sort_by_pot);
4975
4976 if (pot_ptr)
4977 {
4978 log_info_nn ("");
4979
4980 input_buf[input_len] = 0;
4981
4982 // user
4983 unsigned char *username = NULL;
4984 uint user_len = 0;
4985
4986 if (data.username)
4987 {
4988 user_t *user = hashes_buf->hash_info->user;
4989
4990 if (user)
4991 {
4992 username = (unsigned char *) (user->user_name);
4993
4994 user_len = user->user_len;
4995 }
4996 }
4997
4998 // do output the line
4999 format_output (out_fp, input_buf, (unsigned char *) pot_ptr->plain_buf, pot_ptr->plain_len, 0, username, user_len);
5000 }
5001 }
5002
5003 #define LM_WEAK_HASH "\x4e\xcf\x0d\x0c\x0a\xe2\xfb\xc1"
5004 #define LM_MASKED_PLAIN "[notfound]"
5005
5006 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)
5007 {
5008 // left
5009
5010 pot_t pot_left_key;
5011
5012 pot_left_key.hash.salt = hash_left->salt;
5013 pot_left_key.hash.digest = hash_left->digest;
5014
5015 pot_t *pot_left_ptr = (pot_t *) bsearch (&pot_left_key, pot, pot_cnt, sizeof (pot_t), sort_by_pot);
5016
5017 // right
5018
5019 uint weak_hash_found = 0;
5020
5021 pot_t pot_right_key;
5022
5023 pot_right_key.hash.salt = hash_right->salt;
5024 pot_right_key.hash.digest = hash_right->digest;
5025
5026 pot_t *pot_right_ptr = (pot_t *) bsearch (&pot_right_key, pot, pot_cnt, sizeof (pot_t), sort_by_pot);
5027
5028 if (pot_right_ptr == NULL)
5029 {
5030 // special case, if "weak hash"
5031
5032 if (memcmp (hash_right->digest, LM_WEAK_HASH, 8) == 0)
5033 {
5034 weak_hash_found = 1;
5035
5036 pot_right_ptr = (pot_t *) mycalloc (1, sizeof (pot_t));
5037
5038 // in theory this is not needed, but we are paranoia:
5039
5040 memset (pot_right_ptr->plain_buf, 0, sizeof (pot_right_ptr->plain_buf));
5041 pot_right_ptr->plain_len = 0;
5042 }
5043 }
5044
5045 if ((pot_left_ptr == NULL) && (pot_right_ptr == NULL))
5046 {
5047 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
5048
5049 return;
5050 }
5051
5052 // at least one half was found:
5053
5054 log_info_nn ("");
5055
5056 input_buf[input_len] = 0;
5057
5058 // user
5059
5060 unsigned char *username = NULL;
5061 uint user_len = 0;
5062
5063 if (data.username)
5064 {
5065 user_t *user = hash_left->hash_info->user;
5066
5067 if (user)
5068 {
5069 username = (unsigned char *) (user->user_name);
5070
5071 user_len = user->user_len;
5072 }
5073 }
5074
5075 // mask the part which was not found
5076
5077 uint left_part_masked = 0;
5078 uint right_part_masked = 0;
5079
5080 uint mask_plain_len = strlen (LM_MASKED_PLAIN);
5081
5082 if (pot_left_ptr == NULL)
5083 {
5084 left_part_masked = 1;
5085
5086 pot_left_ptr = (pot_t *) mycalloc (1, sizeof (pot_t));
5087
5088 memset (pot_left_ptr->plain_buf, 0, sizeof (pot_left_ptr->plain_buf));
5089
5090 memcpy (pot_left_ptr->plain_buf, LM_MASKED_PLAIN, mask_plain_len);
5091 pot_left_ptr->plain_len = mask_plain_len;
5092 }
5093
5094 if (pot_right_ptr == NULL)
5095 {
5096 right_part_masked = 1;
5097
5098 pot_right_ptr = (pot_t *) mycalloc (1, sizeof (pot_t));
5099
5100 memset (pot_right_ptr->plain_buf, 0, sizeof (pot_right_ptr->plain_buf));
5101
5102 memcpy (pot_right_ptr->plain_buf, LM_MASKED_PLAIN, mask_plain_len);
5103 pot_right_ptr->plain_len = mask_plain_len;
5104 }
5105
5106 // create the pot_ptr out of pot_left_ptr and pot_right_ptr
5107
5108 pot_t pot_ptr;
5109
5110 pot_ptr.plain_len = pot_left_ptr->plain_len + pot_right_ptr->plain_len;
5111
5112 memcpy (pot_ptr.plain_buf, pot_left_ptr->plain_buf, pot_left_ptr->plain_len);
5113
5114 memcpy (pot_ptr.plain_buf + pot_left_ptr->plain_len, pot_right_ptr->plain_buf, pot_right_ptr->plain_len);
5115
5116 // do output the line
5117
5118 format_output (out_fp, input_buf, (unsigned char *) pot_ptr.plain_buf, pot_ptr.plain_len, 0, username, user_len);
5119
5120 if (weak_hash_found == 1) myfree (pot_right_ptr);
5121
5122 if (left_part_masked == 1) myfree (pot_left_ptr);
5123 if (right_part_masked == 1) myfree (pot_right_ptr);
5124 }
5125
5126 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)
5127 {
5128 pot_t pot_key;
5129
5130 memcpy (&pot_key.hash, hashes_buf, sizeof (hash_t));
5131
5132 pot_t *pot_ptr = (pot_t *) bsearch (&pot_key, pot, pot_cnt, sizeof (pot_t), sort_by_pot);
5133
5134 if (pot_ptr == NULL)
5135 {
5136 log_info_nn ("");
5137
5138 input_buf[input_len] = 0;
5139
5140 format_output (out_fp, input_buf, NULL, 0, 0, NULL, 0);
5141 }
5142 }
5143
5144 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)
5145 {
5146 // left
5147
5148 pot_t pot_left_key;
5149
5150 memcpy (&pot_left_key.hash, hash_left, sizeof (hash_t));
5151
5152 pot_t *pot_left_ptr = (pot_t *) bsearch (&pot_left_key, pot, pot_cnt, sizeof (pot_t), sort_by_pot);
5153
5154 // right
5155
5156 pot_t pot_right_key;
5157
5158 memcpy (&pot_right_key.hash, hash_right, sizeof (hash_t));
5159
5160 pot_t *pot_right_ptr = (pot_t *) bsearch (&pot_right_key, pot, pot_cnt, sizeof (pot_t), sort_by_pot);
5161
5162 uint weak_hash_found = 0;
5163
5164 if (pot_right_ptr == NULL)
5165 {
5166 // special case, if "weak hash"
5167
5168 if (memcmp (hash_right->digest, LM_WEAK_HASH, 8) == 0)
5169 {
5170 weak_hash_found = 1;
5171
5172 // we just need that pot_right_ptr is not a NULL pointer
5173
5174 pot_right_ptr = (pot_t *) mycalloc (1, sizeof (pot_t));
5175 }
5176 }
5177
5178 if ((pot_left_ptr != NULL) && (pot_right_ptr != NULL))
5179 {
5180 if (weak_hash_found == 1) myfree (pot_right_ptr);
5181
5182 return;
5183 }
5184
5185 // ... at least one part was not cracked
5186
5187 log_info_nn ("");
5188
5189 input_buf[input_len] = 0;
5190
5191 // only show the hash part which is still not cracked
5192
5193 uint user_len = input_len - 32;
5194
5195 char *hash_output = (char *) mymalloc (33);
5196
5197 memcpy (hash_output, input_buf, input_len);
5198
5199 if (pot_left_ptr != NULL)
5200 {
5201 // only show right part (because left part was already found)
5202
5203 memcpy (hash_output + user_len, input_buf + user_len + 16, 16);
5204
5205 hash_output[user_len + 16] = 0;
5206 }
5207
5208 if (pot_right_ptr != NULL)
5209 {
5210 // only show left part (because right part was already found)
5211
5212 memcpy (hash_output + user_len, input_buf + user_len, 16);
5213
5214 hash_output[user_len + 16] = 0;
5215 }
5216
5217 format_output (out_fp, hash_output, NULL, 0, 0, NULL, 0);
5218
5219 myfree (hash_output);
5220
5221 if (weak_hash_found == 1) myfree (pot_right_ptr);
5222 }
5223
5224 uint setup_opencl_platforms_filter (char *opencl_platforms)
5225 {
5226 uint opencl_platforms_filter = 0;
5227
5228 if (opencl_platforms)
5229 {
5230 char *platforms = strdup (opencl_platforms);
5231
5232 char *next = strtok (platforms, ",");
5233
5234 do
5235 {
5236 int platform = atoi (next);
5237
5238 if (platform < 1 || platform > 32)
5239 {
5240 log_error ("ERROR: invalid OpenCL platform %u specified", platform);
5241
5242 exit (-1);
5243 }
5244
5245 opencl_platforms_filter |= 1 << (platform - 1);
5246
5247 } while ((next = strtok (NULL, ",")) != NULL);
5248
5249 free (platforms);
5250 }
5251 else
5252 {
5253 opencl_platforms_filter = -1;
5254 }
5255
5256 return opencl_platforms_filter;
5257 }
5258
5259 u32 setup_devices_filter (char *opencl_devices)
5260 {
5261 u32 devices_filter = 0;
5262
5263 if (opencl_devices)
5264 {
5265 char *devices = strdup (opencl_devices);
5266
5267 char *next = strtok (devices, ",");
5268
5269 do
5270 {
5271 int device_id = atoi (next);
5272
5273 if (device_id < 1 || device_id > 32)
5274 {
5275 log_error ("ERROR: invalid device_id %u specified", device_id);
5276
5277 exit (-1);
5278 }
5279
5280 devices_filter |= 1 << (device_id - 1);
5281
5282 } while ((next = strtok (NULL, ",")) != NULL);
5283
5284 free (devices);
5285 }
5286 else
5287 {
5288 devices_filter = -1;
5289 }
5290
5291 return devices_filter;
5292 }
5293
5294 cl_device_type setup_device_types_filter (char *opencl_device_types)
5295 {
5296 cl_device_type device_types_filter = 0;
5297
5298 if (opencl_device_types)
5299 {
5300 char *device_types = strdup (opencl_device_types);
5301
5302 char *next = strtok (device_types, ",");
5303
5304 do
5305 {
5306 int device_type = atoi (next);
5307
5308 if (device_type < 1 || device_type > 3)
5309 {
5310 log_error ("ERROR: invalid device_type %u specified", device_type);
5311
5312 exit (-1);
5313 }
5314
5315 device_types_filter |= 1 << device_type;
5316
5317 } while ((next = strtok (NULL, ",")) != NULL);
5318
5319 free (device_types);
5320 }
5321 else
5322 {
5323 // Do not use CPU by default, this often reduces GPU performance because
5324 // the CPU is too busy to handle GPU synchronization
5325
5326 device_types_filter = CL_DEVICE_TYPE_ALL & ~CL_DEVICE_TYPE_CPU;
5327 }
5328
5329 return device_types_filter;
5330 }
5331
5332 u32 get_random_num (const u32 min, const u32 max)
5333 {
5334 if (min == max) return (min);
5335
5336 return ((rand () % (max - min)) + min);
5337 }
5338
5339 u32 mydivc32 (const u32 dividend, const u32 divisor)
5340 {
5341 u32 quotient = dividend / divisor;
5342
5343 if (dividend % divisor) quotient++;
5344
5345 return quotient;
5346 }
5347
5348 u64 mydivc64 (const u64 dividend, const u64 divisor)
5349 {
5350 u64 quotient = dividend / divisor;
5351
5352 if (dividend % divisor) quotient++;
5353
5354 return quotient;
5355 }
5356
5357 void format_timer_display (struct tm *tm, char *buf, size_t len)
5358 {
5359 const char *time_entities_s[] = { "year", "day", "hour", "min", "sec" };
5360 const char *time_entities_m[] = { "years", "days", "hours", "mins", "secs" };
5361
5362 if (tm->tm_year - 70)
5363 {
5364 char *time_entity1 = ((tm->tm_year - 70) == 1) ? (char *) time_entities_s[0] : (char *) time_entities_m[0];
5365 char *time_entity2 = ( tm->tm_yday == 1) ? (char *) time_entities_s[1] : (char *) time_entities_m[1];
5366
5367 snprintf (buf, len - 1, "%d %s, %d %s", tm->tm_year - 70, time_entity1, tm->tm_yday, time_entity2);
5368 }
5369 else if (tm->tm_yday)
5370 {
5371 char *time_entity1 = (tm->tm_yday == 1) ? (char *) time_entities_s[1] : (char *) time_entities_m[1];
5372 char *time_entity2 = (tm->tm_hour == 1) ? (char *) time_entities_s[2] : (char *) time_entities_m[2];
5373
5374 snprintf (buf, len - 1, "%d %s, %d %s", tm->tm_yday, time_entity1, tm->tm_hour, time_entity2);
5375 }
5376 else if (tm->tm_hour)
5377 {
5378 char *time_entity1 = (tm->tm_hour == 1) ? (char *) time_entities_s[2] : (char *) time_entities_m[2];
5379 char *time_entity2 = (tm->tm_min == 1) ? (char *) time_entities_s[3] : (char *) time_entities_m[3];
5380
5381 snprintf (buf, len - 1, "%d %s, %d %s", tm->tm_hour, time_entity1, tm->tm_min, time_entity2);
5382 }
5383 else if (tm->tm_min)
5384 {
5385 char *time_entity1 = (tm->tm_min == 1) ? (char *) time_entities_s[3] : (char *) time_entities_m[3];
5386 char *time_entity2 = (tm->tm_sec == 1) ? (char *) time_entities_s[4] : (char *) time_entities_m[4];
5387
5388 snprintf (buf, len - 1, "%d %s, %d %s", tm->tm_min, time_entity1, tm->tm_sec, time_entity2);
5389 }
5390 else
5391 {
5392 char *time_entity1 = (tm->tm_sec == 1) ? (char *) time_entities_s[4] : (char *) time_entities_m[4];
5393
5394 snprintf (buf, len - 1, "%d %s", tm->tm_sec, time_entity1);
5395 }
5396 }
5397
5398 void format_speed_display (float val, char *buf, size_t len)
5399 {
5400 if (val <= 0)
5401 {
5402 buf[0] = '0';
5403 buf[1] = ' ';
5404 buf[2] = 0;
5405
5406 return;
5407 }
5408
5409 char units[7] = { ' ', 'k', 'M', 'G', 'T', 'P', 'E' };
5410
5411 uint level = 0;
5412
5413 while (val > 99999)
5414 {
5415 val /= 1000;
5416
5417 level++;
5418 }
5419
5420 /* generate output */
5421
5422 if (level == 0)
5423 {
5424 snprintf (buf, len - 1, "%.0f ", val);
5425 }
5426 else
5427 {
5428 snprintf (buf, len - 1, "%.1f %c", val, units[level]);
5429 }
5430 }
5431
5432 void lowercase (u8 *buf, int len)
5433 {
5434 for (int i = 0; i < len; i++) buf[i] = tolower (buf[i]);
5435 }
5436
5437 void uppercase (u8 *buf, int len)
5438 {
5439 for (int i = 0; i < len; i++) buf[i] = toupper (buf[i]);
5440 }
5441
5442 int fgetl (FILE *fp, char *line_buf)
5443 {
5444 int line_len = 0;
5445
5446 while (!feof (fp))
5447 {
5448 const int c = fgetc (fp);
5449
5450 if (c == EOF) break;
5451
5452 line_buf[line_len] = (char) c;
5453
5454 line_len++;
5455
5456 if (line_len == BUFSIZ) line_len--;
5457
5458 if (c == '\n') break;
5459 }
5460
5461 if (line_len == 0) return 0;
5462
5463 if (line_buf[line_len - 1] == '\n')
5464 {
5465 line_len--;
5466
5467 line_buf[line_len] = 0;
5468 }
5469
5470 if (line_len == 0) return 0;
5471
5472 if (line_buf[line_len - 1] == '\r')
5473 {
5474 line_len--;
5475
5476 line_buf[line_len] = 0;
5477 }
5478
5479 return (line_len);
5480 }
5481
5482 int in_superchop (char *buf)
5483 {
5484 int len = strlen (buf);
5485
5486 while (len)
5487 {
5488 if (buf[len - 1] == '\n')
5489 {
5490 len--;
5491
5492 continue;
5493 }
5494
5495 if (buf[len - 1] == '\r')
5496 {
5497 len--;
5498
5499 continue;
5500 }
5501
5502 break;
5503 }
5504
5505 buf[len] = 0;
5506
5507 return len;
5508 }
5509
5510 char **scan_directory (const char *path)
5511 {
5512 char *tmp_path = mystrdup (path);
5513
5514 size_t tmp_path_len = strlen (tmp_path);
5515
5516 while (tmp_path[tmp_path_len - 1] == '/' || tmp_path[tmp_path_len - 1] == '\\')
5517 {
5518 tmp_path[tmp_path_len - 1] = 0;
5519
5520 tmp_path_len = strlen (tmp_path);
5521 }
5522
5523 char **files = NULL;
5524
5525 int num_files = 0;
5526
5527 DIR *d = NULL;
5528
5529 if ((d = opendir (tmp_path)) != NULL)
5530 {
5531 #ifdef OSX
5532 struct dirent e;
5533
5534 for (;;) {
5535 memset (&e, 0, sizeof (e));
5536 struct dirent *de = NULL;
5537
5538 if (readdir_r (d, &e, &de) != 0)
5539 {
5540 log_error ("ERROR: readdir_r() failed");
5541
5542 break;
5543 }
5544
5545 if (de == NULL) break;
5546 #else
5547 struct dirent *de;
5548
5549 while ((de = readdir (d)) != NULL)
5550 {
5551 #endif
5552 if ((strcmp (de->d_name, ".") == 0) || (strcmp (de->d_name, "..") == 0)) continue;
5553
5554 int path_size = strlen (tmp_path) + 1 + strlen (de->d_name);
5555
5556 char *path_file = (char *) mymalloc (path_size + 1);
5557
5558 snprintf (path_file, path_size + 1, "%s/%s", tmp_path, de->d_name);
5559
5560 path_file[path_size] = 0;
5561
5562 DIR *d_test;
5563
5564 if ((d_test = opendir (path_file)) != NULL)
5565 {
5566 closedir (d_test);
5567
5568 myfree (path_file);
5569 }
5570 else
5571 {
5572 files = (char **) myrealloc (files, num_files * sizeof (char *), sizeof (char *));
5573
5574 num_files++;
5575
5576 files[num_files - 1] = path_file;
5577 }
5578 }
5579
5580 closedir (d);
5581 }
5582 else if (errno == ENOTDIR)
5583 {
5584 files = (char **) myrealloc (files, num_files * sizeof (char *), sizeof (char *));
5585
5586 num_files++;
5587
5588 files[num_files - 1] = mystrdup (path);
5589 }
5590
5591 files = (char **) myrealloc (files, num_files * sizeof (char *), sizeof (char *));
5592
5593 num_files++;
5594
5595 files[num_files - 1] = NULL;
5596
5597 myfree (tmp_path);
5598
5599 return (files);
5600 }
5601
5602 int count_dictionaries (char **dictionary_files)
5603 {
5604 if (dictionary_files == NULL) return 0;
5605
5606 int cnt = 0;
5607
5608 for (int d = 0; dictionary_files[d] != NULL; d++)
5609 {
5610 cnt++;
5611 }
5612
5613 return (cnt);
5614 }
5615
5616 char *stroptitype (const uint opti_type)
5617 {
5618 switch (opti_type)
5619 {
5620 case OPTI_TYPE_ZERO_BYTE: return ((char *) OPTI_STR_ZERO_BYTE); break;
5621 case OPTI_TYPE_PRECOMPUTE_INIT: return ((char *) OPTI_STR_PRECOMPUTE_INIT); break;
5622 case OPTI_TYPE_PRECOMPUTE_MERKLE: return ((char *) OPTI_STR_PRECOMPUTE_MERKLE); break;
5623 case OPTI_TYPE_PRECOMPUTE_PERMUT: return ((char *) OPTI_STR_PRECOMPUTE_PERMUT); break;
5624 case OPTI_TYPE_MEET_IN_MIDDLE: return ((char *) OPTI_STR_MEET_IN_MIDDLE); break;
5625 case OPTI_TYPE_EARLY_SKIP: return ((char *) OPTI_STR_EARLY_SKIP); break;
5626 case OPTI_TYPE_NOT_SALTED: return ((char *) OPTI_STR_NOT_SALTED); break;
5627 case OPTI_TYPE_NOT_ITERATED: return ((char *) OPTI_STR_NOT_ITERATED); break;
5628 case OPTI_TYPE_PREPENDED_SALT: return ((char *) OPTI_STR_PREPENDED_SALT); break;
5629 case OPTI_TYPE_APPENDED_SALT: return ((char *) OPTI_STR_APPENDED_SALT); break;
5630 case OPTI_TYPE_SINGLE_HASH: return ((char *) OPTI_STR_SINGLE_HASH); break;
5631 case OPTI_TYPE_SINGLE_SALT: return ((char *) OPTI_STR_SINGLE_SALT); break;
5632 case OPTI_TYPE_BRUTE_FORCE: return ((char *) OPTI_STR_BRUTE_FORCE); break;
5633 case OPTI_TYPE_RAW_HASH: return ((char *) OPTI_STR_RAW_HASH); break;
5634 case OPTI_TYPE_USES_BITS_8: return ((char *) OPTI_STR_USES_BITS_8); break;
5635 case OPTI_TYPE_USES_BITS_16: return ((char *) OPTI_STR_USES_BITS_16); break;
5636 case OPTI_TYPE_USES_BITS_32: return ((char *) OPTI_STR_USES_BITS_32); break;
5637 case OPTI_TYPE_USES_BITS_64: return ((char *) OPTI_STR_USES_BITS_64); break;
5638 }
5639
5640 return (NULL);
5641 }
5642
5643 char *strparser (const uint parser_status)
5644 {
5645 switch (parser_status)
5646 {
5647 case PARSER_OK: return ((char *) PA_000); break;
5648 case PARSER_COMMENT: return ((char *) PA_001); break;
5649 case PARSER_GLOBAL_ZERO: return ((char *) PA_002); break;
5650 case PARSER_GLOBAL_LENGTH: return ((char *) PA_003); break;
5651 case PARSER_HASH_LENGTH: return ((char *) PA_004); break;
5652 case PARSER_HASH_VALUE: return ((char *) PA_005); break;
5653 case PARSER_SALT_LENGTH: return ((char *) PA_006); break;
5654 case PARSER_SALT_VALUE: return ((char *) PA_007); break;
5655 case PARSER_SALT_ITERATION: return ((char *) PA_008); break;
5656 case PARSER_SEPARATOR_UNMATCHED: return ((char *) PA_009); break;
5657 case PARSER_SIGNATURE_UNMATCHED: return ((char *) PA_010); break;
5658 case PARSER_HCCAP_FILE_SIZE: return ((char *) PA_011); break;
5659 case PARSER_HCCAP_EAPOL_SIZE: return ((char *) PA_012); break;
5660 case PARSER_PSAFE2_FILE_SIZE: return ((char *) PA_013); break;
5661 case PARSER_PSAFE3_FILE_SIZE: return ((char *) PA_014); break;
5662 case PARSER_TC_FILE_SIZE: return ((char *) PA_015); break;
5663 case PARSER_SIP_AUTH_DIRECTIVE: return ((char *) PA_016); break;
5664 }
5665
5666 return ((char *) PA_255);
5667 }
5668
5669 char *strhashtype (const uint hash_mode)
5670 {
5671 switch (hash_mode)
5672 {
5673 case 0: return ((char *) HT_00000); break;
5674 case 10: return ((char *) HT_00010); break;
5675 case 11: return ((char *) HT_00011); break;
5676 case 12: return ((char *) HT_00012); break;
5677 case 20: return ((char *) HT_00020); break;
5678 case 21: return ((char *) HT_00021); break;
5679 case 22: return ((char *) HT_00022); break;
5680 case 23: return ((char *) HT_00023); break;
5681 case 30: return ((char *) HT_00030); break;
5682 case 40: return ((char *) HT_00040); break;
5683 case 50: return ((char *) HT_00050); break;
5684 case 60: return ((char *) HT_00060); break;
5685 case 100: return ((char *) HT_00100); break;
5686 case 101: return ((char *) HT_00101); break;
5687 case 110: return ((char *) HT_00110); break;
5688 case 111: return ((char *) HT_00111); break;
5689 case 112: return ((char *) HT_00112); break;
5690 case 120: return ((char *) HT_00120); break;
5691 case 121: return ((char *) HT_00121); break;
5692 case 122: return ((char *) HT_00122); break;
5693 case 124: return ((char *) HT_00124); break;
5694 case 130: return ((char *) HT_00130); break;
5695 case 131: return ((char *) HT_00131); break;
5696 case 132: return ((char *) HT_00132); break;
5697 case 133: return ((char *) HT_00133); break;
5698 case 140: return ((char *) HT_00140); break;
5699 case 141: return ((char *) HT_00141); break;
5700 case 150: return ((char *) HT_00150); break;
5701 case 160: return ((char *) HT_00160); break;
5702 case 190: return ((char *) HT_00190); break;
5703 case 200: return ((char *) HT_00200); break;
5704 case 300: return ((char *) HT_00300); break;
5705 case 400: return ((char *) HT_00400); break;
5706 case 500: return ((char *) HT_00500); break;
5707 case 501: return ((char *) HT_00501); break;
5708 case 900: return ((char *) HT_00900); break;
5709 case 910: return ((char *) HT_00910); break;
5710 case 1000: return ((char *) HT_01000); break;
5711 case 1100: return ((char *) HT_01100); break;
5712 case 1400: return ((char *) HT_01400); break;
5713 case 1410: return ((char *) HT_01410); break;
5714 case 1420: return ((char *) HT_01420); break;
5715 case 1421: return ((char *) HT_01421); break;
5716 case 1430: return ((char *) HT_01430); break;
5717 case 1440: return ((char *) HT_01440); break;
5718 case 1441: return ((char *) HT_01441); break;
5719 case 1450: return ((char *) HT_01450); break;
5720 case 1460: return ((char *) HT_01460); break;
5721 case 1500: return ((char *) HT_01500); break;
5722 case 1600: return ((char *) HT_01600); break;
5723 case 1700: return ((char *) HT_01700); break;
5724 case 1710: return ((char *) HT_01710); break;
5725 case 1711: return ((char *) HT_01711); break;
5726 case 1720: return ((char *) HT_01720); break;
5727 case 1722: return ((char *) HT_01722); break;
5728 case 1730: return ((char *) HT_01730); break;
5729 case 1731: return ((char *) HT_01731); break;
5730 case 1740: return ((char *) HT_01740); break;
5731 case 1750: return ((char *) HT_01750); break;
5732 case 1760: return ((char *) HT_01760); break;
5733 case 1800: return ((char *) HT_01800); break;
5734 case 2100: return ((char *) HT_02100); break;
5735 case 2400: return ((char *) HT_02400); break;
5736 case 2410: return ((char *) HT_02410); break;
5737 case 2500: return ((char *) HT_02500); break;
5738 case 2600: return ((char *) HT_02600); break;
5739 case 2611: return ((char *) HT_02611); break;
5740 case 2612: return ((char *) HT_02612); break;
5741 case 2711: return ((char *) HT_02711); break;
5742 case 2811: return ((char *) HT_02811); break;
5743 case 3000: return ((char *) HT_03000); break;
5744 case 3100: return ((char *) HT_03100); break;
5745 case 3200: return ((char *) HT_03200); break;
5746 case 3710: return ((char *) HT_03710); break;
5747 case 3711: return ((char *) HT_03711); break;
5748 case 3800: return ((char *) HT_03800); break;
5749 case 4300: return ((char *) HT_04300); break;
5750 case 4400: return ((char *) HT_04400); break;
5751 case 4500: return ((char *) HT_04500); break;
5752 case 4700: return ((char *) HT_04700); break;
5753 case 4800: return ((char *) HT_04800); break;
5754 case 4900: return ((char *) HT_04900); break;
5755 case 5000: return ((char *) HT_05000); break;
5756 case 5100: return ((char *) HT_05100); break;
5757 case 5200: return ((char *) HT_05200); break;
5758 case 5300: return ((char *) HT_05300); break;
5759 case 5400: return ((char *) HT_05400); break;
5760 case 5500: return ((char *) HT_05500); break;
5761 case 5600: return ((char *) HT_05600); break;
5762 case 5700: return ((char *) HT_05700); break;
5763 case 5800: return ((char *) HT_05800); break;
5764 case 6000: return ((char *) HT_06000); break;
5765 case 6100: return ((char *) HT_06100); break;
5766 case 6211: return ((char *) HT_06211); break;
5767 case 6212: return ((char *) HT_06212); break;
5768 case 6213: return ((char *) HT_06213); break;
5769 case 6221: return ((char *) HT_06221); break;
5770 case 6222: return ((char *) HT_06222); break;
5771 case 6223: return ((char *) HT_06223); break;
5772 case 6231: return ((char *) HT_06231); break;
5773 case 6232: return ((char *) HT_06232); break;
5774 case 6233: return ((char *) HT_06233); break;
5775 case 6241: return ((char *) HT_06241); break;
5776 case 6242: return ((char *) HT_06242); break;
5777 case 6243: return ((char *) HT_06243); break;
5778 case 6300: return ((char *) HT_06300); break;
5779 case 6400: return ((char *) HT_06400); break;
5780 case 6500: return ((char *) HT_06500); break;
5781 case 6600: return ((char *) HT_06600); break;
5782 case 6700: return ((char *) HT_06700); break;
5783 case 6800: return ((char *) HT_06800); break;
5784 case 6900: return ((char *) HT_06900); break;
5785 case 7100: return ((char *) HT_07100); break;
5786 case 7200: return ((char *) HT_07200); break;
5787 case 7300: return ((char *) HT_07300); break;
5788 case 7400: return ((char *) HT_07400); break;
5789 case 7500: return ((char *) HT_07500); break;
5790 case 7600: return ((char *) HT_07600); break;
5791 case 7700: return ((char *) HT_07700); break;
5792 case 7800: return ((char *) HT_07800); break;
5793 case 7900: return ((char *) HT_07900); break;
5794 case 8000: return ((char *) HT_08000); break;
5795 case 8100: return ((char *) HT_08100); break;
5796 case 8200: return ((char *) HT_08200); break;
5797 case 8300: return ((char *) HT_08300); break;
5798 case 8400: return ((char *) HT_08400); break;
5799 case 8500: return ((char *) HT_08500); break;
5800 case 8600: return ((char *) HT_08600); break;
5801 case 8700: return ((char *) HT_08700); break;
5802 case 8800: return ((char *) HT_08800); break;
5803 case 8900: return ((char *) HT_08900); break;
5804 case 9000: return ((char *) HT_09000); break;
5805 case 9100: return ((char *) HT_09100); break;
5806 case 9200: return ((char *) HT_09200); break;
5807 case 9300: return ((char *) HT_09300); break;
5808 case 9400: return ((char *) HT_09400); break;
5809 case 9500: return ((char *) HT_09500); break;
5810 case 9600: return ((char *) HT_09600); break;
5811 case 9700: return ((char *) HT_09700); break;
5812 case 9710: return ((char *) HT_09710); break;
5813 case 9720: return ((char *) HT_09720); break;
5814 case 9800: return ((char *) HT_09800); break;
5815 case 9810: return ((char *) HT_09810); break;
5816 case 9820: return ((char *) HT_09820); break;
5817 case 9900: return ((char *) HT_09900); break;
5818 case 10000: return ((char *) HT_10000); break;
5819 case 10100: return ((char *) HT_10100); break;
5820 case 10200: return ((char *) HT_10200); break;
5821 case 10300: return ((char *) HT_10300); break;
5822 case 10400: return ((char *) HT_10400); break;
5823 case 10410: return ((char *) HT_10410); break;
5824 case 10420: return ((char *) HT_10420); break;
5825 case 10500: return ((char *) HT_10500); break;
5826 case 10600: return ((char *) HT_10600); break;
5827 case 10700: return ((char *) HT_10700); break;
5828 case 10800: return ((char *) HT_10800); break;
5829 case 10900: return ((char *) HT_10900); break;
5830 case 11000: return ((char *) HT_11000); break;
5831 case 11100: return ((char *) HT_11100); break;
5832 case 11200: return ((char *) HT_11200); break;
5833 case 11300: return ((char *) HT_11300); break;
5834 case 11400: return ((char *) HT_11400); break;
5835 case 11500: return ((char *) HT_11500); break;
5836 case 11600: return ((char *) HT_11600); break;
5837 case 11700: return ((char *) HT_11700); break;
5838 case 11800: return ((char *) HT_11800); break;
5839 case 11900: return ((char *) HT_11900); break;
5840 case 12000: return ((char *) HT_12000); break;
5841 case 12100: return ((char *) HT_12100); break;
5842 case 12200: return ((char *) HT_12200); break;
5843 case 12300: return ((char *) HT_12300); break;
5844 case 12400: return ((char *) HT_12400); break;
5845 case 12500: return ((char *) HT_12500); break;
5846 case 12600: return ((char *) HT_12600); break;
5847 case 12700: return ((char *) HT_12700); break;
5848 case 12800: return ((char *) HT_12800); break;
5849 case 12900: return ((char *) HT_12900); break;
5850 case 13000: return ((char *) HT_13000); break;
5851 case 13100: return ((char *) HT_13100); break;
5852 case 13200: return ((char *) HT_13200); break;
5853 case 13300: return ((char *) HT_13300); break;
5854 }
5855
5856 return ((char *) "Unknown");
5857 }
5858
5859 char *strstatus (const uint devices_status)
5860 {
5861 switch (devices_status)
5862 {
5863 case STATUS_INIT: return ((char *) ST_0000); break;
5864 case STATUS_STARTING: return ((char *) ST_0001); break;
5865 case STATUS_RUNNING: return ((char *) ST_0002); break;
5866 case STATUS_PAUSED: return ((char *) ST_0003); break;
5867 case STATUS_EXHAUSTED: return ((char *) ST_0004); break;
5868 case STATUS_CRACKED: return ((char *) ST_0005); break;
5869 case STATUS_ABORTED: return ((char *) ST_0006); break;
5870 case STATUS_QUIT: return ((char *) ST_0007); break;
5871 case STATUS_BYPASS: return ((char *) ST_0008); break;
5872 case STATUS_STOP_AT_CHECKPOINT: return ((char *) ST_0009); break;
5873 case STATUS_AUTOTUNE: return ((char *) ST_0010); break;
5874 }
5875
5876 return ((char *) "Unknown");
5877 }
5878
5879 void ascii_digest (char out_buf[4096], uint salt_pos, uint digest_pos)
5880 {
5881 uint hash_type = data.hash_type;
5882 uint hash_mode = data.hash_mode;
5883 uint salt_type = data.salt_type;
5884 uint opts_type = data.opts_type;
5885 uint opti_type = data.opti_type;
5886 uint dgst_size = data.dgst_size;
5887
5888 char *hashfile = data.hashfile;
5889
5890 uint len = 4096;
5891
5892 uint digest_buf[64] = { 0 };
5893
5894 u64 *digest_buf64 = (u64 *) digest_buf;
5895
5896 char *digests_buf_ptr = (char *) data.digests_buf;
5897
5898 memcpy (digest_buf, digests_buf_ptr + (data.salts_buf[salt_pos].digests_offset * dgst_size) + (digest_pos * dgst_size), dgst_size);
5899
5900 if (opti_type & OPTI_TYPE_PRECOMPUTE_PERMUT)
5901 {
5902 uint tt;
5903
5904 switch (hash_type)
5905 {
5906 case HASH_TYPE_DESCRYPT:
5907 FP (digest_buf[1], digest_buf[0], tt);
5908 break;
5909
5910 case HASH_TYPE_DESRACF:
5911 digest_buf[0] = rotl32 (digest_buf[0], 29);
5912 digest_buf[1] = rotl32 (digest_buf[1], 29);
5913
5914 FP (digest_buf[1], digest_buf[0], tt);
5915 break;
5916
5917 case HASH_TYPE_LM:
5918 FP (digest_buf[1], digest_buf[0], tt);
5919 break;
5920
5921 case HASH_TYPE_NETNTLM:
5922 digest_buf[0] = rotl32 (digest_buf[0], 29);
5923 digest_buf[1] = rotl32 (digest_buf[1], 29);
5924 digest_buf[2] = rotl32 (digest_buf[2], 29);
5925 digest_buf[3] = rotl32 (digest_buf[3], 29);
5926
5927 FP (digest_buf[1], digest_buf[0], tt);
5928 FP (digest_buf[3], digest_buf[2], tt);
5929 break;
5930
5931 case HASH_TYPE_BSDICRYPT:
5932 digest_buf[0] = rotl32 (digest_buf[0], 31);
5933 digest_buf[1] = rotl32 (digest_buf[1], 31);
5934
5935 FP (digest_buf[1], digest_buf[0], tt);
5936 break;
5937 }
5938 }
5939
5940 if (opti_type & OPTI_TYPE_PRECOMPUTE_MERKLE)
5941 {
5942 switch (hash_type)
5943 {
5944 case HASH_TYPE_MD4:
5945 digest_buf[0] += MD4M_A;
5946 digest_buf[1] += MD4M_B;
5947 digest_buf[2] += MD4M_C;
5948 digest_buf[3] += MD4M_D;
5949 break;
5950
5951 case HASH_TYPE_MD5:
5952 digest_buf[0] += MD5M_A;
5953 digest_buf[1] += MD5M_B;
5954 digest_buf[2] += MD5M_C;
5955 digest_buf[3] += MD5M_D;
5956 break;
5957
5958 case HASH_TYPE_SHA1:
5959 digest_buf[0] += SHA1M_A;
5960 digest_buf[1] += SHA1M_B;
5961 digest_buf[2] += SHA1M_C;
5962 digest_buf[3] += SHA1M_D;
5963 digest_buf[4] += SHA1M_E;
5964 break;
5965
5966 case HASH_TYPE_SHA256:
5967 digest_buf[0] += SHA256M_A;
5968 digest_buf[1] += SHA256M_B;
5969 digest_buf[2] += SHA256M_C;
5970 digest_buf[3] += SHA256M_D;
5971 digest_buf[4] += SHA256M_E;
5972 digest_buf[5] += SHA256M_F;
5973 digest_buf[6] += SHA256M_G;
5974 digest_buf[7] += SHA256M_H;
5975 break;
5976
5977 case HASH_TYPE_SHA384:
5978 digest_buf64[0] += SHA384M_A;
5979 digest_buf64[1] += SHA384M_B;
5980 digest_buf64[2] += SHA384M_C;
5981 digest_buf64[3] += SHA384M_D;
5982 digest_buf64[4] += SHA384M_E;
5983 digest_buf64[5] += SHA384M_F;
5984 digest_buf64[6] += 0;
5985 digest_buf64[7] += 0;
5986 break;
5987
5988 case HASH_TYPE_SHA512:
5989 digest_buf64[0] += SHA512M_A;
5990 digest_buf64[1] += SHA512M_B;
5991 digest_buf64[2] += SHA512M_C;
5992 digest_buf64[3] += SHA512M_D;
5993 digest_buf64[4] += SHA512M_E;
5994 digest_buf64[5] += SHA512M_F;
5995 digest_buf64[6] += SHA512M_G;
5996 digest_buf64[7] += SHA512M_H;
5997 break;
5998 }
5999 }
6000
6001 if (opts_type & OPTS_TYPE_PT_GENERATE_LE)
6002 {
6003 if (dgst_size == DGST_SIZE_4_2)
6004 {
6005 for (int i = 0; i < 2; i++) digest_buf[i] = byte_swap_32 (digest_buf[i]);
6006 }
6007 else if (dgst_size == DGST_SIZE_4_4)
6008 {
6009 for (int i = 0; i < 4; i++) digest_buf[i] = byte_swap_32 (digest_buf[i]);
6010 }
6011 else if (dgst_size == DGST_SIZE_4_5)
6012 {
6013 for (int i = 0; i < 5; i++) digest_buf[i] = byte_swap_32 (digest_buf[i]);
6014 }
6015 else if (dgst_size == DGST_SIZE_4_6)
6016 {
6017 for (int i = 0; i < 6; i++) digest_buf[i] = byte_swap_32 (digest_buf[i]);
6018 }
6019 else if (dgst_size == DGST_SIZE_4_8)
6020 {
6021 for (int i = 0; i < 8; i++) digest_buf[i] = byte_swap_32 (digest_buf[i]);
6022 }
6023 else if ((dgst_size == DGST_SIZE_4_16) || (dgst_size == DGST_SIZE_8_8)) // same size, same result :)
6024 {
6025 if (hash_type == HASH_TYPE_WHIRLPOOL)
6026 {
6027 for (int i = 0; i < 16; i++) digest_buf[i] = byte_swap_32 (digest_buf[i]);
6028 }
6029 else if (hash_type == HASH_TYPE_SHA384)
6030 {
6031 for (int i = 0; i < 8; i++) digest_buf64[i] = byte_swap_64 (digest_buf64[i]);
6032 }
6033 else if (hash_type == HASH_TYPE_SHA512)
6034 {
6035 for (int i = 0; i < 8; i++) digest_buf64[i] = byte_swap_64 (digest_buf64[i]);
6036 }
6037 else if (hash_type == HASH_TYPE_GOST)
6038 {
6039 for (int i = 0; i < 16; i++) digest_buf[i] = byte_swap_32 (digest_buf[i]);
6040 }
6041 }
6042 else if (dgst_size == DGST_SIZE_4_64)
6043 {
6044 for (int i = 0; i < 64; i++) digest_buf[i] = byte_swap_32 (digest_buf[i]);
6045 }
6046 else if (dgst_size == DGST_SIZE_8_25)
6047 {
6048 for (int i = 0; i < 25; i++) digest_buf64[i] = byte_swap_64 (digest_buf64[i]);
6049 }
6050 }
6051
6052 uint isSalted = ((data.salt_type == SALT_TYPE_INTERN)
6053 | (data.salt_type == SALT_TYPE_EXTERN)
6054 | (data.salt_type == SALT_TYPE_EMBEDDED));
6055
6056 salt_t salt;
6057
6058 if (isSalted)
6059 {
6060 memset (&salt, 0, sizeof (salt_t));
6061
6062 memcpy (&salt, &data.salts_buf[salt_pos], sizeof (salt_t));
6063
6064 char *ptr = (char *) salt.salt_buf;
6065
6066 uint len = salt.salt_len;
6067
6068 if (opti_type & OPTI_TYPE_PRECOMPUTE_PERMUT)
6069 {
6070 uint tt;
6071
6072 switch (hash_type)
6073 {
6074 case HASH_TYPE_NETNTLM:
6075
6076 salt.salt_buf[0] = rotr32 (salt.salt_buf[0], 3);
6077 salt.salt_buf[1] = rotr32 (salt.salt_buf[1], 3);
6078
6079 FP (salt.salt_buf[1], salt.salt_buf[0], tt);
6080
6081 break;
6082 }
6083 }
6084
6085 if (opts_type & OPTS_TYPE_ST_UNICODE)
6086 {
6087 for (uint i = 0, j = 0; i < len; i += 1, j += 2)
6088 {
6089 ptr[i] = ptr[j];
6090 }
6091
6092 len = len / 2;
6093 }
6094
6095 if (opts_type & OPTS_TYPE_ST_GENERATE_LE)
6096 {
6097 uint max = salt.salt_len / 4;
6098
6099 if (len % 4) max++;
6100
6101 for (uint i = 0; i < max; i++)
6102 {
6103 salt.salt_buf[i] = byte_swap_32 (salt.salt_buf[i]);
6104 }
6105 }
6106
6107 if (opts_type & OPTS_TYPE_ST_HEX)
6108 {
6109 char tmp[64] = { 0 };
6110
6111 for (uint i = 0, j = 0; i < len; i += 1, j += 2)
6112 {
6113 sprintf (tmp + j, "%02x", (unsigned char) ptr[i]);
6114 }
6115
6116 len = len * 2;
6117
6118 memcpy (ptr, tmp, len);
6119 }
6120
6121 uint memset_size = ((48 - (int) len) > 0) ? (48 - len) : 0;
6122
6123 memset (ptr + len, 0, memset_size);
6124
6125 salt.salt_len = len;
6126 }
6127
6128 //
6129 // some modes require special encoding
6130 //
6131
6132 uint out_buf_plain[256] = { 0 };
6133 uint out_buf_salt[256] = { 0 };
6134
6135 char tmp_buf[1024] = { 0 };
6136
6137 char *ptr_plain = (char *) out_buf_plain;
6138 char *ptr_salt = (char *) out_buf_salt;
6139
6140 if (hash_mode == 22)
6141 {
6142 char username[30] = { 0 };
6143
6144 memcpy (username, salt.salt_buf, salt.salt_len - 22);
6145
6146 char sig[6] = { 'n', 'r', 'c', 's', 't', 'n' };
6147
6148 u16 *ptr = (u16 *) digest_buf;
6149
6150 tmp_buf[ 0] = sig[0];
6151 tmp_buf[ 1] = int_to_base64 (((ptr[1]) >> 12) & 0x3f);
6152 tmp_buf[ 2] = int_to_base64 (((ptr[1]) >> 6) & 0x3f);
6153 tmp_buf[ 3] = int_to_base64 (((ptr[1]) >> 0) & 0x3f);
6154 tmp_buf[ 4] = int_to_base64 (((ptr[0]) >> 12) & 0x3f);
6155 tmp_buf[ 5] = int_to_base64 (((ptr[0]) >> 6) & 0x3f);
6156 tmp_buf[ 6] = sig[1];
6157 tmp_buf[ 7] = int_to_base64 (((ptr[0]) >> 0) & 0x3f);
6158 tmp_buf[ 8] = int_to_base64 (((ptr[3]) >> 12) & 0x3f);
6159 tmp_buf[ 9] = int_to_base64 (((ptr[3]) >> 6) & 0x3f);
6160 tmp_buf[10] = int_to_base64 (((ptr[3]) >> 0) & 0x3f);
6161 tmp_buf[11] = int_to_base64 (((ptr[2]) >> 12) & 0x3f);
6162 tmp_buf[12] = sig[2];
6163 tmp_buf[13] = int_to_base64 (((ptr[2]) >> 6) & 0x3f);
6164 tmp_buf[14] = int_to_base64 (((ptr[2]) >> 0) & 0x3f);
6165 tmp_buf[15] = int_to_base64 (((ptr[5]) >> 12) & 0x3f);
6166 tmp_buf[16] = int_to_base64 (((ptr[5]) >> 6) & 0x3f);
6167 tmp_buf[17] = sig[3];
6168 tmp_buf[18] = int_to_base64 (((ptr[5]) >> 0) & 0x3f);
6169 tmp_buf[19] = int_to_base64 (((ptr[4]) >> 12) & 0x3f);
6170 tmp_buf[20] = int_to_base64 (((ptr[4]) >> 6) & 0x3f);
6171 tmp_buf[21] = int_to_base64 (((ptr[4]) >> 0) & 0x3f);
6172 tmp_buf[22] = int_to_base64 (((ptr[7]) >> 12) & 0x3f);
6173 tmp_buf[23] = sig[4];
6174 tmp_buf[24] = int_to_base64 (((ptr[7]) >> 6) & 0x3f);
6175 tmp_buf[25] = int_to_base64 (((ptr[7]) >> 0) & 0x3f);
6176 tmp_buf[26] = int_to_base64 (((ptr[6]) >> 12) & 0x3f);
6177 tmp_buf[27] = int_to_base64 (((ptr[6]) >> 6) & 0x3f);
6178 tmp_buf[28] = int_to_base64 (((ptr[6]) >> 0) & 0x3f);
6179 tmp_buf[29] = sig[5];
6180
6181 snprintf (out_buf, len-1, "%s:%s",
6182 tmp_buf,
6183 username);
6184 }
6185 else if (hash_mode == 23)
6186 {
6187 // do not show the \nskyper\n part in output
6188
6189 char *salt_buf_ptr = (char *) salt.salt_buf;
6190
6191 salt_buf_ptr[salt.salt_len - 8] = 0;
6192
6193 snprintf (out_buf, len-1, "%08x%08x%08x%08x:%s",
6194 digest_buf[0],
6195 digest_buf[1],
6196 digest_buf[2],
6197 digest_buf[3],
6198 salt_buf_ptr);
6199 }
6200 else if (hash_mode == 101)
6201 {
6202 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6203
6204 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6205 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6206 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6207 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6208 digest_buf[4] = byte_swap_32 (digest_buf[4]);
6209
6210 memcpy (tmp_buf, digest_buf, 20);
6211
6212 base64_encode (int_to_base64, (const u8 *) tmp_buf, 20, (u8 *) ptr_plain);
6213
6214 snprintf (out_buf, len-1, "{SHA}%s", ptr_plain);
6215 }
6216 else if (hash_mode == 111)
6217 {
6218 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6219
6220 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6221 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6222 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6223 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6224 digest_buf[4] = byte_swap_32 (digest_buf[4]);
6225
6226 memcpy (tmp_buf, digest_buf, 20);
6227 memcpy (tmp_buf + 20, salt.salt_buf, salt.salt_len);
6228
6229 base64_encode (int_to_base64, (const u8 *) tmp_buf, 20 + salt.salt_len, (u8 *) ptr_plain);
6230
6231 snprintf (out_buf, len-1, "{SSHA}%s", ptr_plain);
6232 }
6233 else if (hash_mode == 122)
6234 {
6235 snprintf (out_buf, len-1, "%s%08x%08x%08x%08x%08x",
6236 (char *) salt.salt_buf,
6237 digest_buf[0],
6238 digest_buf[1],
6239 digest_buf[2],
6240 digest_buf[3],
6241 digest_buf[4]);
6242 }
6243 else if (hash_mode == 124)
6244 {
6245 snprintf (out_buf, len-1, "sha1$%s$%08x%08x%08x%08x%08x",
6246 (char *) salt.salt_buf,
6247 digest_buf[0],
6248 digest_buf[1],
6249 digest_buf[2],
6250 digest_buf[3],
6251 digest_buf[4]);
6252 }
6253 else if (hash_mode == 131)
6254 {
6255 snprintf (out_buf, len-1, "0x0100%s%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
6256 (char *) salt.salt_buf,
6257 0, 0, 0, 0, 0,
6258 digest_buf[0],
6259 digest_buf[1],
6260 digest_buf[2],
6261 digest_buf[3],
6262 digest_buf[4]);
6263 }
6264 else if (hash_mode == 132)
6265 {
6266 snprintf (out_buf, len-1, "0x0100%s%08x%08x%08x%08x%08x",
6267 (char *) salt.salt_buf,
6268 digest_buf[0],
6269 digest_buf[1],
6270 digest_buf[2],
6271 digest_buf[3],
6272 digest_buf[4]);
6273 }
6274 else if (hash_mode == 133)
6275 {
6276 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6277
6278 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6279 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6280 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6281 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6282 digest_buf[4] = byte_swap_32 (digest_buf[4]);
6283
6284 memcpy (tmp_buf, digest_buf, 20);
6285
6286 base64_encode (int_to_base64, (const u8 *) tmp_buf, 20, (u8 *) ptr_plain);
6287
6288 snprintf (out_buf, len-1, "%s", ptr_plain);
6289 }
6290 else if (hash_mode == 141)
6291 {
6292 memcpy (tmp_buf, salt.salt_buf, salt.salt_len);
6293
6294 base64_encode (int_to_base64, (const u8 *) tmp_buf, salt.salt_len, (u8 *) ptr_salt);
6295
6296 memset (tmp_buf, 0, sizeof (tmp_buf));
6297
6298 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6299
6300 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6301 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6302 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6303 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6304 digest_buf[4] = byte_swap_32 (digest_buf[4]);
6305
6306 memcpy (tmp_buf, digest_buf, 20);
6307
6308 base64_encode (int_to_base64, (const u8 *) tmp_buf, 20, (u8 *) ptr_plain);
6309
6310 ptr_plain[27] = 0;
6311
6312 snprintf (out_buf, len-1, "%s%s*%s", SIGNATURE_EPISERVER, ptr_salt, ptr_plain);
6313 }
6314 else if (hash_mode == 400)
6315 {
6316 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6317
6318 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6319 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6320 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6321 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6322
6323 phpass_encode ((unsigned char *) digest_buf, (unsigned char *) ptr_plain);
6324
6325 snprintf (out_buf, len-1, "%s%s%s", (char *) salt.salt_sign, (char *) salt.salt_buf, (char *) ptr_plain);
6326 }
6327 else if (hash_mode == 500)
6328 {
6329 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6330
6331 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6332 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6333 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6334 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6335
6336 md5crypt_encode ((unsigned char *) digest_buf, (unsigned char *) ptr_plain);
6337
6338 if (salt.salt_iter == ROUNDS_MD5CRYPT)
6339 {
6340 snprintf (out_buf, len-1, "$1$%s$%s", (char *) salt.salt_buf, (char *) ptr_plain);
6341 }
6342 else
6343 {
6344 snprintf (out_buf, len-1, "$1$rounds=%i$%s$%s", salt.salt_iter, (char *) salt.salt_buf, (char *) ptr_plain);
6345 }
6346 }
6347 else if (hash_mode == 501)
6348 {
6349 uint digest_idx = salt.digests_offset + digest_pos;
6350
6351 hashinfo_t **hashinfo_ptr = data.hash_info;
6352 char *hash_buf = hashinfo_ptr[digest_idx]->orighash;
6353
6354 snprintf (out_buf, len-1, "%s", hash_buf);
6355 }
6356 else if (hash_mode == 1421)
6357 {
6358 u8 *salt_ptr = (u8 *) salt.salt_buf;
6359
6360 snprintf (out_buf, len-1, "%c%c%c%c%c%c%08x%08x%08x%08x%08x%08x%08x%08x",
6361 salt_ptr[0],
6362 salt_ptr[1],
6363 salt_ptr[2],
6364 salt_ptr[3],
6365 salt_ptr[4],
6366 salt_ptr[5],
6367 digest_buf[0],
6368 digest_buf[1],
6369 digest_buf[2],
6370 digest_buf[3],
6371 digest_buf[4],
6372 digest_buf[5],
6373 digest_buf[6],
6374 digest_buf[7]);
6375 }
6376 else if (hash_mode == 1441)
6377 {
6378 memcpy (tmp_buf, salt.salt_buf, salt.salt_len);
6379
6380 base64_encode (int_to_base64, (const u8 *) tmp_buf, salt.salt_len, (u8 *) ptr_salt);
6381
6382 memset (tmp_buf, 0, sizeof (tmp_buf));
6383
6384 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6385
6386 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6387 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6388 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6389 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6390 digest_buf[4] = byte_swap_32 (digest_buf[4]);
6391 digest_buf[5] = byte_swap_32 (digest_buf[5]);
6392 digest_buf[6] = byte_swap_32 (digest_buf[6]);
6393 digest_buf[7] = byte_swap_32 (digest_buf[7]);
6394
6395 memcpy (tmp_buf, digest_buf, 32);
6396
6397 base64_encode (int_to_base64, (const u8 *) tmp_buf, 32, (u8 *) ptr_plain);
6398
6399 ptr_plain[43] = 0;
6400
6401 snprintf (out_buf, len-1, "%s%s*%s", SIGNATURE_EPISERVER4, ptr_salt, ptr_plain);
6402 }
6403 else if (hash_mode == 1500)
6404 {
6405 out_buf[0] = salt.salt_sign[0] & 0xff;
6406 out_buf[1] = salt.salt_sign[1] & 0xff;
6407 //original method, but changed because of this ticket: https://hashcat.net/trac/ticket/269
6408 //out_buf[0] = int_to_itoa64 ((salt.salt_buf[0] >> 0) & 0x3f);
6409 //out_buf[1] = int_to_itoa64 ((salt.salt_buf[0] >> 6) & 0x3f);
6410
6411 memset (tmp_buf, 0, sizeof (tmp_buf));
6412
6413 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6414
6415 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6416 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6417
6418 memcpy (tmp_buf, digest_buf, 8);
6419
6420 base64_encode (int_to_itoa64, (const u8 *) tmp_buf, 8, (u8 *) ptr_plain);
6421
6422 snprintf (out_buf + 2, len-1-2, "%s", ptr_plain);
6423
6424 out_buf[13] = 0;
6425 }
6426 else if (hash_mode == 1600)
6427 {
6428 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6429
6430 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6431 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6432 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6433 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6434
6435 md5crypt_encode ((unsigned char *) digest_buf, (unsigned char *) ptr_plain);
6436
6437 if (salt.salt_iter == ROUNDS_MD5CRYPT)
6438 {
6439 snprintf (out_buf, len-1, "$apr1$%s$%s", (char *) salt.salt_buf, (char *) ptr_plain);
6440 }
6441 else
6442 {
6443 snprintf (out_buf, len-1, "$apr1$rounds=%i$%s$%s", salt.salt_iter, (char *) salt.salt_buf, (char *) ptr_plain);
6444 }
6445 }
6446 else if (hash_mode == 1711)
6447 {
6448 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6449
6450 digest_buf64[0] = byte_swap_64 (digest_buf64[0]);
6451 digest_buf64[1] = byte_swap_64 (digest_buf64[1]);
6452 digest_buf64[2] = byte_swap_64 (digest_buf64[2]);
6453 digest_buf64[3] = byte_swap_64 (digest_buf64[3]);
6454 digest_buf64[4] = byte_swap_64 (digest_buf64[4]);
6455 digest_buf64[5] = byte_swap_64 (digest_buf64[5]);
6456 digest_buf64[6] = byte_swap_64 (digest_buf64[6]);
6457 digest_buf64[7] = byte_swap_64 (digest_buf64[7]);
6458
6459 memcpy (tmp_buf, digest_buf, 64);
6460 memcpy (tmp_buf + 64, salt.salt_buf, salt.salt_len);
6461
6462 base64_encode (int_to_base64, (const u8 *) tmp_buf, 64 + salt.salt_len, (u8 *) ptr_plain);
6463
6464 snprintf (out_buf, len-1, "%s%s", SIGNATURE_SHA512B64S, ptr_plain);
6465 }
6466 else if (hash_mode == 1722)
6467 {
6468 uint *ptr = digest_buf;
6469
6470 snprintf (out_buf, len-1, "%s%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
6471 (unsigned char *) salt.salt_buf,
6472 ptr[ 1], ptr[ 0],
6473 ptr[ 3], ptr[ 2],
6474 ptr[ 5], ptr[ 4],
6475 ptr[ 7], ptr[ 6],
6476 ptr[ 9], ptr[ 8],
6477 ptr[11], ptr[10],
6478 ptr[13], ptr[12],
6479 ptr[15], ptr[14]);
6480 }
6481 else if (hash_mode == 1731)
6482 {
6483 uint *ptr = digest_buf;
6484
6485 snprintf (out_buf, len-1, "0x0200%s%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
6486 (unsigned char *) salt.salt_buf,
6487 ptr[ 1], ptr[ 0],
6488 ptr[ 3], ptr[ 2],
6489 ptr[ 5], ptr[ 4],
6490 ptr[ 7], ptr[ 6],
6491 ptr[ 9], ptr[ 8],
6492 ptr[11], ptr[10],
6493 ptr[13], ptr[12],
6494 ptr[15], ptr[14]);
6495 }
6496 else if (hash_mode == 1800)
6497 {
6498 // temp workaround
6499
6500 digest_buf64[0] = byte_swap_64 (digest_buf64[0]);
6501 digest_buf64[1] = byte_swap_64 (digest_buf64[1]);
6502 digest_buf64[2] = byte_swap_64 (digest_buf64[2]);
6503 digest_buf64[3] = byte_swap_64 (digest_buf64[3]);
6504 digest_buf64[4] = byte_swap_64 (digest_buf64[4]);
6505 digest_buf64[5] = byte_swap_64 (digest_buf64[5]);
6506 digest_buf64[6] = byte_swap_64 (digest_buf64[6]);
6507 digest_buf64[7] = byte_swap_64 (digest_buf64[7]);
6508
6509 sha512crypt_encode ((unsigned char *) digest_buf64, (unsigned char *) ptr_plain);
6510
6511 if (salt.salt_iter == ROUNDS_SHA512CRYPT)
6512 {
6513 snprintf (out_buf, len-1, "$6$%s$%s", (char *) salt.salt_buf, (char *) ptr_plain);
6514 }
6515 else
6516 {
6517 snprintf (out_buf, len-1, "$6$rounds=%i$%s$%s", salt.salt_iter, (char *) salt.salt_buf, (char *) ptr_plain);
6518 }
6519 }
6520 else if (hash_mode == 2100)
6521 {
6522 uint pos = 0;
6523
6524 snprintf (out_buf + pos, len-1, "%s%i#",
6525 SIGNATURE_DCC2,
6526 salt.salt_iter + 1);
6527
6528 uint signature_len = strlen (out_buf);
6529
6530 pos += signature_len;
6531 len -= signature_len;
6532
6533 char *salt_ptr = (char *) salt.salt_buf;
6534
6535 for (uint i = 0; i < salt.salt_len; i++, pos++, len--) snprintf (out_buf + pos, len-1, "%c", salt_ptr[i]);
6536
6537 snprintf (out_buf + pos, len-1, "#%08x%08x%08x%08x",
6538 byte_swap_32 (digest_buf[0]),
6539 byte_swap_32 (digest_buf[1]),
6540 byte_swap_32 (digest_buf[2]),
6541 byte_swap_32 (digest_buf[3]));
6542 }
6543 else if ((hash_mode == 2400) || (hash_mode == 2410))
6544 {
6545 memcpy (tmp_buf, digest_buf, 16);
6546
6547 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6548
6549 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6550 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6551 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6552 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6553
6554 out_buf[ 0] = int_to_itoa64 ((digest_buf[0] >> 0) & 0x3f);
6555 out_buf[ 1] = int_to_itoa64 ((digest_buf[0] >> 6) & 0x3f);
6556 out_buf[ 2] = int_to_itoa64 ((digest_buf[0] >> 12) & 0x3f);
6557 out_buf[ 3] = int_to_itoa64 ((digest_buf[0] >> 18) & 0x3f);
6558
6559 out_buf[ 4] = int_to_itoa64 ((digest_buf[1] >> 0) & 0x3f);
6560 out_buf[ 5] = int_to_itoa64 ((digest_buf[1] >> 6) & 0x3f);
6561 out_buf[ 6] = int_to_itoa64 ((digest_buf[1] >> 12) & 0x3f);
6562 out_buf[ 7] = int_to_itoa64 ((digest_buf[1] >> 18) & 0x3f);
6563
6564 out_buf[ 8] = int_to_itoa64 ((digest_buf[2] >> 0) & 0x3f);
6565 out_buf[ 9] = int_to_itoa64 ((digest_buf[2] >> 6) & 0x3f);
6566 out_buf[10] = int_to_itoa64 ((digest_buf[2] >> 12) & 0x3f);
6567 out_buf[11] = int_to_itoa64 ((digest_buf[2] >> 18) & 0x3f);
6568
6569 out_buf[12] = int_to_itoa64 ((digest_buf[3] >> 0) & 0x3f);
6570 out_buf[13] = int_to_itoa64 ((digest_buf[3] >> 6) & 0x3f);
6571 out_buf[14] = int_to_itoa64 ((digest_buf[3] >> 12) & 0x3f);
6572 out_buf[15] = int_to_itoa64 ((digest_buf[3] >> 18) & 0x3f);
6573
6574 out_buf[16] = 0;
6575 }
6576 else if (hash_mode == 2500)
6577 {
6578 wpa_t *wpas = (wpa_t *) data.esalts_buf;
6579
6580 wpa_t *wpa = &wpas[salt_pos];
6581
6582 uint pke[25] = { 0 };
6583
6584 char *pke_ptr = (char *) pke;
6585
6586 for (uint i = 0; i < 25; i++)
6587 {
6588 pke[i] = byte_swap_32 (wpa->pke[i]);
6589 }
6590
6591 unsigned char mac1[6] = { 0 };
6592 unsigned char mac2[6] = { 0 };
6593
6594 memcpy (mac1, pke_ptr + 23, 6);
6595 memcpy (mac2, pke_ptr + 29, 6);
6596
6597 snprintf (out_buf, len-1, "%s:%02x%02x%02x%02x%02x%02x:%02x%02x%02x%02x%02x%02x",
6598 (char *) salt.salt_buf,
6599 mac1[0],
6600 mac1[1],
6601 mac1[2],
6602 mac1[3],
6603 mac1[4],
6604 mac1[5],
6605 mac2[0],
6606 mac2[1],
6607 mac2[2],
6608 mac2[3],
6609 mac2[4],
6610 mac2[5]);
6611 }
6612 else if (hash_mode == 4400)
6613 {
6614 snprintf (out_buf, len-1, "%08x%08x%08x%08x",
6615 byte_swap_32 (digest_buf[0]),
6616 byte_swap_32 (digest_buf[1]),
6617 byte_swap_32 (digest_buf[2]),
6618 byte_swap_32 (digest_buf[3]));
6619 }
6620 else if (hash_mode == 4700)
6621 {
6622 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x",
6623 byte_swap_32 (digest_buf[0]),
6624 byte_swap_32 (digest_buf[1]),
6625 byte_swap_32 (digest_buf[2]),
6626 byte_swap_32 (digest_buf[3]),
6627 byte_swap_32 (digest_buf[4]));
6628 }
6629 else if (hash_mode == 4800)
6630 {
6631 u8 chap_id_byte = (u8) salt.salt_buf[4];
6632
6633 snprintf (out_buf, len-1, "%08x%08x%08x%08x:%08x%08x%08x%08x:%02x",
6634 digest_buf[0],
6635 digest_buf[1],
6636 digest_buf[2],
6637 digest_buf[3],
6638 byte_swap_32 (salt.salt_buf[0]),
6639 byte_swap_32 (salt.salt_buf[1]),
6640 byte_swap_32 (salt.salt_buf[2]),
6641 byte_swap_32 (salt.salt_buf[3]),
6642 chap_id_byte);
6643 }
6644 else if (hash_mode == 4900)
6645 {
6646 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x",
6647 byte_swap_32 (digest_buf[0]),
6648 byte_swap_32 (digest_buf[1]),
6649 byte_swap_32 (digest_buf[2]),
6650 byte_swap_32 (digest_buf[3]),
6651 byte_swap_32 (digest_buf[4]));
6652 }
6653 else if (hash_mode == 5100)
6654 {
6655 snprintf (out_buf, len-1, "%08x%08x",
6656 digest_buf[0],
6657 digest_buf[1]);
6658 }
6659 else if (hash_mode == 5200)
6660 {
6661 snprintf (out_buf, len-1, "%s", hashfile);
6662 }
6663 else if (hash_mode == 5300)
6664 {
6665 ikepsk_t *ikepsks = (ikepsk_t *) data.esalts_buf;
6666
6667 ikepsk_t *ikepsk = &ikepsks[salt_pos];
6668
6669 int buf_len = len -1;
6670
6671 // msg_buf
6672
6673 uint ikepsk_msg_len = ikepsk->msg_len / 4;
6674
6675 for (uint i = 0; i < ikepsk_msg_len; i++)
6676 {
6677 if ((i == 32) || (i == 64) || (i == 66) || (i == 68) || (i == 108))
6678 {
6679 snprintf (out_buf, buf_len, ":");
6680
6681 buf_len--;
6682 out_buf++;
6683 }
6684
6685 snprintf (out_buf, buf_len, "%08x", byte_swap_32 (ikepsk->msg_buf[i]));
6686
6687 buf_len -= 8;
6688 out_buf += 8;
6689 }
6690
6691 // nr_buf
6692
6693 uint ikepsk_nr_len = ikepsk->nr_len / 4;
6694
6695 for (uint i = 0; i < ikepsk_nr_len; i++)
6696 {
6697 if ((i == 0) || (i == 5))
6698 {
6699 snprintf (out_buf, buf_len, ":");
6700
6701 buf_len--;
6702 out_buf++;
6703 }
6704
6705 snprintf (out_buf, buf_len, "%08x", byte_swap_32 (ikepsk->nr_buf[i]));
6706
6707 buf_len -= 8;
6708 out_buf += 8;
6709 }
6710
6711 // digest_buf
6712
6713 for (uint i = 0; i < 4; i++)
6714 {
6715 if (i == 0)
6716 {
6717 snprintf (out_buf, buf_len, ":");
6718
6719 buf_len--;
6720 out_buf++;
6721 }
6722
6723 snprintf (out_buf, buf_len, "%08x", digest_buf[i]);
6724
6725 buf_len -= 8;
6726 out_buf += 8;
6727 }
6728 }
6729 else if (hash_mode == 5400)
6730 {
6731 ikepsk_t *ikepsks = (ikepsk_t *) data.esalts_buf;
6732
6733 ikepsk_t *ikepsk = &ikepsks[salt_pos];
6734
6735 int buf_len = len -1;
6736
6737 // msg_buf
6738
6739 uint ikepsk_msg_len = ikepsk->msg_len / 4;
6740
6741 for (uint i = 0; i < ikepsk_msg_len; i++)
6742 {
6743 if ((i == 32) || (i == 64) || (i == 66) || (i == 68) || (i == 108))
6744 {
6745 snprintf (out_buf, buf_len, ":");
6746
6747 buf_len--;
6748 out_buf++;
6749 }
6750
6751 snprintf (out_buf, buf_len, "%08x", byte_swap_32 (ikepsk->msg_buf[i]));
6752
6753 buf_len -= 8;
6754 out_buf += 8;
6755 }
6756
6757 // nr_buf
6758
6759 uint ikepsk_nr_len = ikepsk->nr_len / 4;
6760
6761 for (uint i = 0; i < ikepsk_nr_len; i++)
6762 {
6763 if ((i == 0) || (i == 5))
6764 {
6765 snprintf (out_buf, buf_len, ":");
6766
6767 buf_len--;
6768 out_buf++;
6769 }
6770
6771 snprintf (out_buf, buf_len, "%08x", byte_swap_32 (ikepsk->nr_buf[i]));
6772
6773 buf_len -= 8;
6774 out_buf += 8;
6775 }
6776
6777 // digest_buf
6778
6779 for (uint i = 0; i < 5; i++)
6780 {
6781 if (i == 0)
6782 {
6783 snprintf (out_buf, buf_len, ":");
6784
6785 buf_len--;
6786 out_buf++;
6787 }
6788
6789 snprintf (out_buf, buf_len, "%08x", digest_buf[i]);
6790
6791 buf_len -= 8;
6792 out_buf += 8;
6793 }
6794 }
6795 else if (hash_mode == 5500)
6796 {
6797 netntlm_t *netntlms = (netntlm_t *) data.esalts_buf;
6798
6799 netntlm_t *netntlm = &netntlms[salt_pos];
6800
6801 char user_buf[64] = { 0 };
6802 char domain_buf[64] = { 0 };
6803 char srvchall_buf[1024] = { 0 };
6804 char clichall_buf[1024] = { 0 };
6805
6806 for (uint i = 0, j = 0; j < netntlm->user_len; i += 1, j += 2)
6807 {
6808 char *ptr = (char *) netntlm->userdomain_buf;
6809
6810 user_buf[i] = ptr[j];
6811 }
6812
6813 for (uint i = 0, j = 0; j < netntlm->domain_len; i += 1, j += 2)
6814 {
6815 char *ptr = (char *) netntlm->userdomain_buf;
6816
6817 domain_buf[i] = ptr[netntlm->user_len + j];
6818 }
6819
6820 for (uint i = 0, j = 0; i < netntlm->srvchall_len; i += 1, j += 2)
6821 {
6822 u8 *ptr = (u8 *) netntlm->chall_buf;
6823
6824 sprintf (srvchall_buf + j, "%02x", ptr[i]);
6825 }
6826
6827 for (uint i = 0, j = 0; i < netntlm->clichall_len; i += 1, j += 2)
6828 {
6829 u8 *ptr = (u8 *) netntlm->chall_buf;
6830
6831 sprintf (clichall_buf + j, "%02x", ptr[netntlm->srvchall_len + i]);
6832 }
6833
6834 snprintf (out_buf, len-1, "%s::%s:%s:%08x%08x%08x%08x%08x%08x:%s",
6835 user_buf,
6836 domain_buf,
6837 srvchall_buf,
6838 digest_buf[0],
6839 digest_buf[1],
6840 digest_buf[2],
6841 digest_buf[3],
6842 byte_swap_32 (salt.salt_buf_pc[0]),
6843 byte_swap_32 (salt.salt_buf_pc[1]),
6844 clichall_buf);
6845 }
6846 else if (hash_mode == 5600)
6847 {
6848 netntlm_t *netntlms = (netntlm_t *) data.esalts_buf;
6849
6850 netntlm_t *netntlm = &netntlms[salt_pos];
6851
6852 char user_buf[64] = { 0 };
6853 char domain_buf[64] = { 0 };
6854 char srvchall_buf[1024] = { 0 };
6855 char clichall_buf[1024] = { 0 };
6856
6857 for (uint i = 0, j = 0; j < netntlm->user_len; i += 1, j += 2)
6858 {
6859 char *ptr = (char *) netntlm->userdomain_buf;
6860
6861 user_buf[i] = ptr[j];
6862 }
6863
6864 for (uint i = 0, j = 0; j < netntlm->domain_len; i += 1, j += 2)
6865 {
6866 char *ptr = (char *) netntlm->userdomain_buf;
6867
6868 domain_buf[i] = ptr[netntlm->user_len + j];
6869 }
6870
6871 for (uint i = 0, j = 0; i < netntlm->srvchall_len; i += 1, j += 2)
6872 {
6873 u8 *ptr = (u8 *) netntlm->chall_buf;
6874
6875 sprintf (srvchall_buf + j, "%02x", ptr[i]);
6876 }
6877
6878 for (uint i = 0, j = 0; i < netntlm->clichall_len; i += 1, j += 2)
6879 {
6880 u8 *ptr = (u8 *) netntlm->chall_buf;
6881
6882 sprintf (clichall_buf + j, "%02x", ptr[netntlm->srvchall_len + i]);
6883 }
6884
6885 snprintf (out_buf, len-1, "%s::%s:%s:%08x%08x%08x%08x:%s",
6886 user_buf,
6887 domain_buf,
6888 srvchall_buf,
6889 digest_buf[0],
6890 digest_buf[1],
6891 digest_buf[2],
6892 digest_buf[3],
6893 clichall_buf);
6894 }
6895 else if (hash_mode == 5700)
6896 {
6897 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6898
6899 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6900 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6901 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6902 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6903 digest_buf[4] = byte_swap_32 (digest_buf[4]);
6904 digest_buf[5] = byte_swap_32 (digest_buf[5]);
6905 digest_buf[6] = byte_swap_32 (digest_buf[6]);
6906 digest_buf[7] = byte_swap_32 (digest_buf[7]);
6907
6908 memcpy (tmp_buf, digest_buf, 32);
6909
6910 base64_encode (int_to_itoa64, (const u8 *) tmp_buf, 32, (u8 *) ptr_plain);
6911
6912 ptr_plain[43] = 0;
6913
6914 snprintf (out_buf, len-1, "%s", ptr_plain);
6915 }
6916 else if (hash_mode == 5800)
6917 {
6918 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6919 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6920 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6921 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6922 digest_buf[4] = byte_swap_32 (digest_buf[4]);
6923
6924 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x",
6925 digest_buf[0],
6926 digest_buf[1],
6927 digest_buf[2],
6928 digest_buf[3],
6929 digest_buf[4]);
6930 }
6931 else if ((hash_mode >= 6200) && (hash_mode <= 6299))
6932 {
6933 snprintf (out_buf, len-1, "%s", hashfile);
6934 }
6935 else if (hash_mode == 6300)
6936 {
6937 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
6938
6939 digest_buf[0] = byte_swap_32 (digest_buf[0]);
6940 digest_buf[1] = byte_swap_32 (digest_buf[1]);
6941 digest_buf[2] = byte_swap_32 (digest_buf[2]);
6942 digest_buf[3] = byte_swap_32 (digest_buf[3]);
6943
6944 md5crypt_encode ((unsigned char *) digest_buf, (unsigned char *) ptr_plain);
6945
6946 snprintf (out_buf, len-1, "{smd5}%s$%s", (char *) salt.salt_buf, (char *) ptr_plain);
6947 }
6948 else if (hash_mode == 6400)
6949 {
6950 sha256aix_encode ((unsigned char *) digest_buf, (unsigned char *) ptr_plain);
6951
6952 snprintf (out_buf, len-1, "{ssha256}%02d$%s$%s", salt.salt_sign[0], (char *) salt.salt_buf, (char *) ptr_plain);
6953 }
6954 else if (hash_mode == 6500)
6955 {
6956 sha512aix_encode ((unsigned char *) digest_buf64, (unsigned char *) ptr_plain);
6957
6958 snprintf (out_buf, len-1, "{ssha512}%02d$%s$%s", salt.salt_sign[0], (char *) salt.salt_buf, (char *) ptr_plain);
6959 }
6960 else if (hash_mode == 6600)
6961 {
6962 agilekey_t *agilekeys = (agilekey_t *) data.esalts_buf;
6963
6964 agilekey_t *agilekey = &agilekeys[salt_pos];
6965
6966 salt.salt_buf[0] = byte_swap_32 (salt.salt_buf[0]);
6967 salt.salt_buf[1] = byte_swap_32 (salt.salt_buf[1]);
6968
6969 uint buf_len = len - 1;
6970
6971 uint off = snprintf (out_buf, buf_len, "%d:%08x%08x:", salt.salt_iter + 1, salt.salt_buf[0], salt.salt_buf[1]);
6972 buf_len -= 22;
6973
6974 for (uint i = 0, j = off; i < 1040; i++, j += 2)
6975 {
6976 snprintf (out_buf + j, buf_len, "%02x", agilekey->cipher[i]);
6977
6978 buf_len -= 2;
6979 }
6980 }
6981 else if (hash_mode == 6700)
6982 {
6983 sha1aix_encode ((unsigned char *) digest_buf, (unsigned char *) ptr_plain);
6984
6985 snprintf (out_buf, len-1, "{ssha1}%02d$%s$%s", salt.salt_sign[0], (char *) salt.salt_buf, (char *) ptr_plain);
6986 }
6987 else if (hash_mode == 6800)
6988 {
6989 snprintf (out_buf, len-1, "%s", (char *) salt.salt_buf);
6990 }
6991 else if (hash_mode == 7100)
6992 {
6993 uint *ptr = digest_buf;
6994
6995 pbkdf2_sha512_t *pbkdf2_sha512s = (pbkdf2_sha512_t *) data.esalts_buf;
6996
6997 pbkdf2_sha512_t *pbkdf2_sha512 = &pbkdf2_sha512s[salt_pos];
6998
6999 uint esalt[8] = { 0 };
7000
7001 esalt[0] = byte_swap_32 (pbkdf2_sha512->salt_buf[0]);
7002 esalt[1] = byte_swap_32 (pbkdf2_sha512->salt_buf[1]);
7003 esalt[2] = byte_swap_32 (pbkdf2_sha512->salt_buf[2]);
7004 esalt[3] = byte_swap_32 (pbkdf2_sha512->salt_buf[3]);
7005 esalt[4] = byte_swap_32 (pbkdf2_sha512->salt_buf[4]);
7006 esalt[5] = byte_swap_32 (pbkdf2_sha512->salt_buf[5]);
7007 esalt[6] = byte_swap_32 (pbkdf2_sha512->salt_buf[6]);
7008 esalt[7] = byte_swap_32 (pbkdf2_sha512->salt_buf[7]);
7009
7010 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",
7011 SIGNATURE_SHA512OSX,
7012 salt.salt_iter + 1,
7013 esalt[ 0], esalt[ 1],
7014 esalt[ 2], esalt[ 3],
7015 esalt[ 4], esalt[ 5],
7016 esalt[ 6], esalt[ 7],
7017 ptr [ 1], ptr [ 0],
7018 ptr [ 3], ptr [ 2],
7019 ptr [ 5], ptr [ 4],
7020 ptr [ 7], ptr [ 6],
7021 ptr [ 9], ptr [ 8],
7022 ptr [11], ptr [10],
7023 ptr [13], ptr [12],
7024 ptr [15], ptr [14]);
7025 }
7026 else if (hash_mode == 7200)
7027 {
7028 uint *ptr = digest_buf;
7029
7030 pbkdf2_sha512_t *pbkdf2_sha512s = (pbkdf2_sha512_t *) data.esalts_buf;
7031
7032 pbkdf2_sha512_t *pbkdf2_sha512 = &pbkdf2_sha512s[salt_pos];
7033
7034 uint len_used = 0;
7035
7036 snprintf (out_buf + len_used, len - len_used - 1, "%s%i.", SIGNATURE_SHA512GRUB, salt.salt_iter + 1);
7037
7038 len_used = strlen (out_buf);
7039
7040 unsigned char *salt_buf_ptr = (unsigned char *) pbkdf2_sha512->salt_buf;
7041
7042 for (uint i = 0; i < salt.salt_len; i++, len_used += 2)
7043 {
7044 snprintf (out_buf + len_used, len - len_used - 1, "%02x", salt_buf_ptr[i]);
7045 }
7046
7047 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",
7048 ptr [ 1], ptr [ 0],
7049 ptr [ 3], ptr [ 2],
7050 ptr [ 5], ptr [ 4],
7051 ptr [ 7], ptr [ 6],
7052 ptr [ 9], ptr [ 8],
7053 ptr [11], ptr [10],
7054 ptr [13], ptr [12],
7055 ptr [15], ptr [14]);
7056 }
7057 else if (hash_mode == 7300)
7058 {
7059 rakp_t *rakps = (rakp_t *) data.esalts_buf;
7060
7061 rakp_t *rakp = &rakps[salt_pos];
7062
7063 for (uint i = 0, j = 0; (i * 4) < rakp->salt_len; i += 1, j += 8)
7064 {
7065 sprintf (out_buf + j, "%08x", rakp->salt_buf[i]);
7066 }
7067
7068 snprintf (out_buf + rakp->salt_len * 2, len - 1, ":%08x%08x%08x%08x%08x",
7069 digest_buf[0],
7070 digest_buf[1],
7071 digest_buf[2],
7072 digest_buf[3],
7073 digest_buf[4]);
7074 }
7075 else if (hash_mode == 7400)
7076 {
7077 // the encoder is a bit too intelligent, it expects the input data in the wrong BOM
7078
7079 digest_buf[0] = byte_swap_32 (digest_buf[0]);
7080 digest_buf[1] = byte_swap_32 (digest_buf[1]);
7081 digest_buf[2] = byte_swap_32 (digest_buf[2]);
7082 digest_buf[3] = byte_swap_32 (digest_buf[3]);
7083 digest_buf[4] = byte_swap_32 (digest_buf[4]);
7084 digest_buf[5] = byte_swap_32 (digest_buf[5]);
7085 digest_buf[6] = byte_swap_32 (digest_buf[6]);
7086 digest_buf[7] = byte_swap_32 (digest_buf[7]);
7087
7088 sha256crypt_encode ((unsigned char *) digest_buf, (unsigned char *) ptr_plain);
7089
7090 if (salt.salt_iter == ROUNDS_SHA256CRYPT)
7091 {
7092 snprintf (out_buf, len-1, "$5$%s$%s", (char *) salt.salt_buf, (char *) ptr_plain);
7093 }
7094 else
7095 {
7096 snprintf (out_buf, len-1, "$5$rounds=%i$%s$%s", salt.salt_iter, (char *) salt.salt_buf, (char *) ptr_plain);
7097 }
7098 }
7099 else if (hash_mode == 7500)
7100 {
7101 krb5pa_t *krb5pas = (krb5pa_t *) data.esalts_buf;
7102
7103 krb5pa_t *krb5pa = &krb5pas[salt_pos];
7104
7105 u8 *ptr_timestamp = (u8 *) krb5pa->timestamp;
7106 u8 *ptr_checksum = (u8 *) krb5pa->checksum;
7107
7108 char data[128] = { 0 };
7109
7110 char *ptr_data = data;
7111
7112 for (uint i = 0; i < 36; i++, ptr_data += 2)
7113 {
7114 sprintf (ptr_data, "%02x", ptr_timestamp[i]);
7115 }
7116
7117 for (uint i = 0; i < 16; i++, ptr_data += 2)
7118 {
7119 sprintf (ptr_data, "%02x", ptr_checksum[i]);
7120 }
7121
7122 *ptr_data = 0;
7123
7124 snprintf (out_buf, len-1, "%s$%s$%s$%s$%s",
7125 SIGNATURE_KRB5PA,
7126 (char *) krb5pa->user,
7127 (char *) krb5pa->realm,
7128 (char *) krb5pa->salt,
7129 data);
7130 }
7131 else if (hash_mode == 7700)
7132 {
7133 snprintf (out_buf, len-1, "%s$%08X%08X",
7134 (char *) salt.salt_buf,
7135 digest_buf[0],
7136 digest_buf[1]);
7137 }
7138 else if (hash_mode == 7800)
7139 {
7140 snprintf (out_buf, len-1, "%s$%08X%08X%08X%08X%08X",
7141 (char *) salt.salt_buf,
7142 digest_buf[0],
7143 digest_buf[1],
7144 digest_buf[2],
7145 digest_buf[3],
7146 digest_buf[4]);
7147 }
7148 else if (hash_mode == 7900)
7149 {
7150 drupal7_encode ((unsigned char *) digest_buf64, (unsigned char *) ptr_plain);
7151
7152 // ugly hack start
7153
7154 char *tmp = (char *) salt.salt_buf_pc;
7155
7156 ptr_plain[42] = tmp[0];
7157
7158 // ugly hack end
7159
7160 ptr_plain[43] = 0;
7161
7162 snprintf (out_buf, len-1, "%s%s%s", (char *) salt.salt_sign, (char *) salt.salt_buf, (char *) ptr_plain);
7163 }
7164 else if (hash_mode == 8000)
7165 {
7166 snprintf (out_buf, len-1, "0xc007%s%08x%08x%08x%08x%08x%08x%08x%08x",
7167 (unsigned char *) salt.salt_buf,
7168 digest_buf[0],
7169 digest_buf[1],
7170 digest_buf[2],
7171 digest_buf[3],
7172 digest_buf[4],
7173 digest_buf[5],
7174 digest_buf[6],
7175 digest_buf[7]);
7176 }
7177 else if (hash_mode == 8100)
7178 {
7179 salt.salt_buf[0] = byte_swap_32 (salt.salt_buf[0]);
7180 salt.salt_buf[1] = byte_swap_32 (salt.salt_buf[1]);
7181
7182 snprintf (out_buf, len-1, "1%s%08x%08x%08x%08x%08x",
7183 (unsigned char *) salt.salt_buf,
7184 digest_buf[0],
7185 digest_buf[1],
7186 digest_buf[2],
7187 digest_buf[3],
7188 digest_buf[4]);
7189 }
7190 else if (hash_mode == 8200)
7191 {
7192 cloudkey_t *cloudkeys = (cloudkey_t *) data.esalts_buf;
7193
7194 cloudkey_t *cloudkey = &cloudkeys[salt_pos];
7195
7196 char data_buf[4096] = { 0 };
7197
7198 for (int i = 0, j = 0; i < 512; i += 1, j += 8)
7199 {
7200 sprintf (data_buf + j, "%08x", cloudkey->data_buf[i]);
7201 }
7202
7203 data_buf[cloudkey->data_len * 2] = 0;
7204
7205 digest_buf[0] = byte_swap_32 (digest_buf[0]);
7206 digest_buf[1] = byte_swap_32 (digest_buf[1]);
7207 digest_buf[2] = byte_swap_32 (digest_buf[2]);
7208 digest_buf[3] = byte_swap_32 (digest_buf[3]);
7209 digest_buf[4] = byte_swap_32 (digest_buf[4]);
7210 digest_buf[5] = byte_swap_32 (digest_buf[5]);
7211 digest_buf[6] = byte_swap_32 (digest_buf[6]);
7212 digest_buf[7] = byte_swap_32 (digest_buf[7]);
7213
7214 salt.salt_buf[0] = byte_swap_32 (salt.salt_buf[0]);
7215 salt.salt_buf[1] = byte_swap_32 (salt.salt_buf[1]);
7216 salt.salt_buf[2] = byte_swap_32 (salt.salt_buf[2]);
7217 salt.salt_buf[3] = byte_swap_32 (salt.salt_buf[3]);
7218
7219 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x%08x%08x%08x:%08x%08x%08x%08x:%u:%s",
7220 digest_buf[0],
7221 digest_buf[1],
7222 digest_buf[2],
7223 digest_buf[3],
7224 digest_buf[4],
7225 digest_buf[5],
7226 digest_buf[6],
7227 digest_buf[7],
7228 salt.salt_buf[0],
7229 salt.salt_buf[1],
7230 salt.salt_buf[2],
7231 salt.salt_buf[3],
7232 salt.salt_iter + 1,
7233 data_buf);
7234 }
7235 else if (hash_mode == 8300)
7236 {
7237 char digest_buf_c[34] = { 0 };
7238
7239 base32_encode (int_to_itoa32, (const u8 *) digest_buf, 20, (u8 *) digest_buf_c);
7240
7241 digest_buf_c[32] = 0;
7242
7243 // domain
7244
7245 const uint salt_pc_len = salt.salt_buf_pc[7]; // what a hack
7246
7247 char domain_buf_c[33] = { 0 };
7248
7249 memcpy (domain_buf_c, (char *) salt.salt_buf_pc, salt_pc_len);
7250
7251 for (uint i = 0; i < salt_pc_len; i++)
7252 {
7253 const char next = domain_buf_c[i];
7254
7255 domain_buf_c[i] = '.';
7256
7257 i += next;
7258 }
7259
7260 domain_buf_c[salt_pc_len] = 0;
7261
7262 // final
7263
7264 snprintf (out_buf, len-1, "%s:%s:%s:%u", digest_buf_c, domain_buf_c, (char *) salt.salt_buf, salt.salt_iter);
7265 }
7266 else if (hash_mode == 8500)
7267 {
7268 snprintf (out_buf, len-1, "%s*%s*%08X%08X", SIGNATURE_RACF, (char *) salt.salt_buf, digest_buf[0], digest_buf[1]);
7269 }
7270 else if (hash_mode == 2612)
7271 {
7272 snprintf (out_buf, len-1, "%s%s$%08x%08x%08x%08x",
7273 SIGNATURE_PHPS,
7274 (char *) salt.salt_buf,
7275 digest_buf[0],
7276 digest_buf[1],
7277 digest_buf[2],
7278 digest_buf[3]);
7279 }
7280 else if (hash_mode == 3711)
7281 {
7282 char *salt_ptr = (char *) salt.salt_buf;
7283
7284 salt_ptr[salt.salt_len - 1] = 0;
7285
7286 snprintf (out_buf, len-1, "%s%s$%08x%08x%08x%08x",
7287 SIGNATURE_MEDIAWIKI_B,
7288 salt_ptr,
7289 digest_buf[0],
7290 digest_buf[1],
7291 digest_buf[2],
7292 digest_buf[3]);
7293 }
7294 else if (hash_mode == 8800)
7295 {
7296 androidfde_t *androidfdes = (androidfde_t *) data.esalts_buf;
7297
7298 androidfde_t *androidfde = &androidfdes[salt_pos];
7299
7300 char tmp[3073] = { 0 };
7301
7302 for (uint i = 0, j = 0; i < 384; i += 1, j += 8)
7303 {
7304 sprintf (tmp + j, "%08x", androidfde->data[i]);
7305 }
7306
7307 tmp[3072] = 0;
7308
7309 snprintf (out_buf, len-1, "%s16$%08x%08x%08x%08x$16$%08x%08x%08x%08x$%s",
7310 SIGNATURE_ANDROIDFDE,
7311 byte_swap_32 (salt.salt_buf[0]),
7312 byte_swap_32 (salt.salt_buf[1]),
7313 byte_swap_32 (salt.salt_buf[2]),
7314 byte_swap_32 (salt.salt_buf[3]),
7315 byte_swap_32 (digest_buf[0]),
7316 byte_swap_32 (digest_buf[1]),
7317 byte_swap_32 (digest_buf[2]),
7318 byte_swap_32 (digest_buf[3]),
7319 tmp);
7320 }
7321 else if (hash_mode == 8900)
7322 {
7323 uint N = salt.scrypt_N;
7324 uint r = salt.scrypt_r;
7325 uint p = salt.scrypt_p;
7326
7327 char base64_salt[32] = { 0 };
7328
7329 base64_encode (int_to_base64, (const u8 *) salt.salt_buf, salt.salt_len, (u8 *) base64_salt);
7330
7331 memset (tmp_buf, 0, 46);
7332
7333 digest_buf[0] = byte_swap_32 (digest_buf[0]);
7334 digest_buf[1] = byte_swap_32 (digest_buf[1]);
7335 digest_buf[2] = byte_swap_32 (digest_buf[2]);
7336 digest_buf[3] = byte_swap_32 (digest_buf[3]);
7337 digest_buf[4] = byte_swap_32 (digest_buf[4]);
7338 digest_buf[5] = byte_swap_32 (digest_buf[5]);
7339 digest_buf[6] = byte_swap_32 (digest_buf[6]);
7340 digest_buf[7] = byte_swap_32 (digest_buf[7]);
7341 digest_buf[8] = 0; // needed for base64_encode ()
7342
7343 base64_encode (int_to_base64, (const u8 *) digest_buf, 32, (u8 *) tmp_buf);
7344
7345 snprintf (out_buf, len-1, "%s:%i:%i:%i:%s:%s",
7346 SIGNATURE_SCRYPT,
7347 N,
7348 r,
7349 p,
7350 base64_salt,
7351 tmp_buf);
7352 }
7353 else if (hash_mode == 9000)
7354 {
7355 snprintf (out_buf, len-1, "%s", hashfile);
7356 }
7357 else if (hash_mode == 9200)
7358 {
7359 // salt
7360
7361 pbkdf2_sha256_t *pbkdf2_sha256s = (pbkdf2_sha256_t *) data.esalts_buf;
7362
7363 pbkdf2_sha256_t *pbkdf2_sha256 = &pbkdf2_sha256s[salt_pos];
7364
7365 unsigned char *salt_buf_ptr = (unsigned char *) pbkdf2_sha256->salt_buf;
7366
7367 // hash
7368
7369 digest_buf[0] = byte_swap_32 (digest_buf[0]);
7370 digest_buf[1] = byte_swap_32 (digest_buf[1]);
7371 digest_buf[2] = byte_swap_32 (digest_buf[2]);
7372 digest_buf[3] = byte_swap_32 (digest_buf[3]);
7373 digest_buf[4] = byte_swap_32 (digest_buf[4]);
7374 digest_buf[5] = byte_swap_32 (digest_buf[5]);
7375 digest_buf[6] = byte_swap_32 (digest_buf[6]);
7376 digest_buf[7] = byte_swap_32 (digest_buf[7]);
7377 digest_buf[8] = 0; // needed for base64_encode ()
7378
7379 char tmp_buf[64] = { 0 };
7380
7381 base64_encode (int_to_itoa64, (const u8 *) digest_buf, 32, (u8 *) tmp_buf);
7382 tmp_buf[43] = 0; // cut it here
7383
7384 // output
7385
7386 snprintf (out_buf, len-1, "%s%s$%s", SIGNATURE_CISCO8, salt_buf_ptr, tmp_buf);
7387 }
7388 else if (hash_mode == 9300)
7389 {
7390 digest_buf[0] = byte_swap_32 (digest_buf[0]);
7391 digest_buf[1] = byte_swap_32 (digest_buf[1]);
7392 digest_buf[2] = byte_swap_32 (digest_buf[2]);
7393 digest_buf[3] = byte_swap_32 (digest_buf[3]);
7394 digest_buf[4] = byte_swap_32 (digest_buf[4]);
7395 digest_buf[5] = byte_swap_32 (digest_buf[5]);
7396 digest_buf[6] = byte_swap_32 (digest_buf[6]);
7397 digest_buf[7] = byte_swap_32 (digest_buf[7]);
7398 digest_buf[8] = 0; // needed for base64_encode ()
7399
7400 char tmp_buf[64] = { 0 };
7401
7402 base64_encode (int_to_itoa64, (const u8 *) digest_buf, 32, (u8 *) tmp_buf);
7403 tmp_buf[43] = 0; // cut it here
7404
7405 unsigned char *salt_buf_ptr = (unsigned char *) salt.salt_buf;
7406
7407 snprintf (out_buf, len-1, "%s%s$%s", SIGNATURE_CISCO9, salt_buf_ptr, tmp_buf);
7408 }
7409 else if (hash_mode == 9400)
7410 {
7411 office2007_t *office2007s = (office2007_t *) data.esalts_buf;
7412
7413 office2007_t *office2007 = &office2007s[salt_pos];
7414
7415 snprintf (out_buf, len-1, "%s*%u*%u*%u*%u*%08x%08x%08x%08x*%08x%08x%08x%08x*%08x%08x%08x%08x%08x",
7416 SIGNATURE_OFFICE2007,
7417 2007,
7418 20,
7419 office2007->keySize,
7420 16,
7421 salt.salt_buf[0],
7422 salt.salt_buf[1],
7423 salt.salt_buf[2],
7424 salt.salt_buf[3],
7425 office2007->encryptedVerifier[0],
7426 office2007->encryptedVerifier[1],
7427 office2007->encryptedVerifier[2],
7428 office2007->encryptedVerifier[3],
7429 office2007->encryptedVerifierHash[0],
7430 office2007->encryptedVerifierHash[1],
7431 office2007->encryptedVerifierHash[2],
7432 office2007->encryptedVerifierHash[3],
7433 office2007->encryptedVerifierHash[4]);
7434 }
7435 else if (hash_mode == 9500)
7436 {
7437 office2010_t *office2010s = (office2010_t *) data.esalts_buf;
7438
7439 office2010_t *office2010 = &office2010s[salt_pos];
7440
7441 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,
7442
7443 salt.salt_buf[0],
7444 salt.salt_buf[1],
7445 salt.salt_buf[2],
7446 salt.salt_buf[3],
7447 office2010->encryptedVerifier[0],
7448 office2010->encryptedVerifier[1],
7449 office2010->encryptedVerifier[2],
7450 office2010->encryptedVerifier[3],
7451 office2010->encryptedVerifierHash[0],
7452 office2010->encryptedVerifierHash[1],
7453 office2010->encryptedVerifierHash[2],
7454 office2010->encryptedVerifierHash[3],
7455 office2010->encryptedVerifierHash[4],
7456 office2010->encryptedVerifierHash[5],
7457 office2010->encryptedVerifierHash[6],
7458 office2010->encryptedVerifierHash[7]);
7459 }
7460 else if (hash_mode == 9600)
7461 {
7462 office2013_t *office2013s = (office2013_t *) data.esalts_buf;
7463
7464 office2013_t *office2013 = &office2013s[salt_pos];
7465
7466 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,
7467
7468 salt.salt_buf[0],
7469 salt.salt_buf[1],
7470 salt.salt_buf[2],
7471 salt.salt_buf[3],
7472 office2013->encryptedVerifier[0],
7473 office2013->encryptedVerifier[1],
7474 office2013->encryptedVerifier[2],
7475 office2013->encryptedVerifier[3],
7476 office2013->encryptedVerifierHash[0],
7477 office2013->encryptedVerifierHash[1],
7478 office2013->encryptedVerifierHash[2],
7479 office2013->encryptedVerifierHash[3],
7480 office2013->encryptedVerifierHash[4],
7481 office2013->encryptedVerifierHash[5],
7482 office2013->encryptedVerifierHash[6],
7483 office2013->encryptedVerifierHash[7]);
7484 }
7485 else if (hash_mode == 9700)
7486 {
7487 oldoffice01_t *oldoffice01s = (oldoffice01_t *) data.esalts_buf;
7488
7489 oldoffice01_t *oldoffice01 = &oldoffice01s[salt_pos];
7490
7491 snprintf (out_buf, len-1, "%s*%08x%08x%08x%08x*%08x%08x%08x%08x*%08x%08x%08x%08x",
7492 (oldoffice01->version == 0) ? SIGNATURE_OLDOFFICE0 : SIGNATURE_OLDOFFICE1,
7493 byte_swap_32 (salt.salt_buf[0]),
7494 byte_swap_32 (salt.salt_buf[1]),
7495 byte_swap_32 (salt.salt_buf[2]),
7496 byte_swap_32 (salt.salt_buf[3]),
7497 byte_swap_32 (oldoffice01->encryptedVerifier[0]),
7498 byte_swap_32 (oldoffice01->encryptedVerifier[1]),
7499 byte_swap_32 (oldoffice01->encryptedVerifier[2]),
7500 byte_swap_32 (oldoffice01->encryptedVerifier[3]),
7501 byte_swap_32 (oldoffice01->encryptedVerifierHash[0]),
7502 byte_swap_32 (oldoffice01->encryptedVerifierHash[1]),
7503 byte_swap_32 (oldoffice01->encryptedVerifierHash[2]),
7504 byte_swap_32 (oldoffice01->encryptedVerifierHash[3]));
7505 }
7506 else if (hash_mode == 9710)
7507 {
7508 oldoffice01_t *oldoffice01s = (oldoffice01_t *) data.esalts_buf;
7509
7510 oldoffice01_t *oldoffice01 = &oldoffice01s[salt_pos];
7511
7512 snprintf (out_buf, len-1, "%s*%08x%08x%08x%08x*%08x%08x%08x%08x*%08x%08x%08x%08x",
7513 (oldoffice01->version == 0) ? SIGNATURE_OLDOFFICE0 : SIGNATURE_OLDOFFICE1,
7514 byte_swap_32 (salt.salt_buf[0]),
7515 byte_swap_32 (salt.salt_buf[1]),
7516 byte_swap_32 (salt.salt_buf[2]),
7517 byte_swap_32 (salt.salt_buf[3]),
7518 byte_swap_32 (oldoffice01->encryptedVerifier[0]),
7519 byte_swap_32 (oldoffice01->encryptedVerifier[1]),
7520 byte_swap_32 (oldoffice01->encryptedVerifier[2]),
7521 byte_swap_32 (oldoffice01->encryptedVerifier[3]),
7522 byte_swap_32 (oldoffice01->encryptedVerifierHash[0]),
7523 byte_swap_32 (oldoffice01->encryptedVerifierHash[1]),
7524 byte_swap_32 (oldoffice01->encryptedVerifierHash[2]),
7525 byte_swap_32 (oldoffice01->encryptedVerifierHash[3]));
7526 }
7527 else if (hash_mode == 9720)
7528 {
7529 oldoffice01_t *oldoffice01s = (oldoffice01_t *) data.esalts_buf;
7530
7531 oldoffice01_t *oldoffice01 = &oldoffice01s[salt_pos];
7532
7533 u8 *rc4key = (u8 *) oldoffice01->rc4key;
7534
7535 snprintf (out_buf, len-1, "%s*%08x%08x%08x%08x*%08x%08x%08x%08x*%08x%08x%08x%08x:%02x%02x%02x%02x%02x",
7536 (oldoffice01->version == 0) ? SIGNATURE_OLDOFFICE0 : SIGNATURE_OLDOFFICE1,
7537 byte_swap_32 (salt.salt_buf[0]),
7538 byte_swap_32 (salt.salt_buf[1]),
7539 byte_swap_32 (salt.salt_buf[2]),
7540 byte_swap_32 (salt.salt_buf[3]),
7541 byte_swap_32 (oldoffice01->encryptedVerifier[0]),
7542 byte_swap_32 (oldoffice01->encryptedVerifier[1]),
7543 byte_swap_32 (oldoffice01->encryptedVerifier[2]),
7544 byte_swap_32 (oldoffice01->encryptedVerifier[3]),
7545 byte_swap_32 (oldoffice01->encryptedVerifierHash[0]),
7546 byte_swap_32 (oldoffice01->encryptedVerifierHash[1]),
7547 byte_swap_32 (oldoffice01->encryptedVerifierHash[2]),
7548 byte_swap_32 (oldoffice01->encryptedVerifierHash[3]),
7549 rc4key[0],
7550 rc4key[1],
7551 rc4key[2],
7552 rc4key[3],
7553 rc4key[4]);
7554 }
7555 else if (hash_mode == 9800)
7556 {
7557 oldoffice34_t *oldoffice34s = (oldoffice34_t *) data.esalts_buf;
7558
7559 oldoffice34_t *oldoffice34 = &oldoffice34s[salt_pos];
7560
7561 snprintf (out_buf, len-1, "%s*%08x%08x%08x%08x*%08x%08x%08x%08x*%08x%08x%08x%08x%08x",
7562 (oldoffice34->version == 3) ? SIGNATURE_OLDOFFICE3 : SIGNATURE_OLDOFFICE4,
7563 salt.salt_buf[0],
7564 salt.salt_buf[1],
7565 salt.salt_buf[2],
7566 salt.salt_buf[3],
7567 byte_swap_32 (oldoffice34->encryptedVerifier[0]),
7568 byte_swap_32 (oldoffice34->encryptedVerifier[1]),
7569 byte_swap_32 (oldoffice34->encryptedVerifier[2]),
7570 byte_swap_32 (oldoffice34->encryptedVerifier[3]),
7571 byte_swap_32 (oldoffice34->encryptedVerifierHash[0]),
7572 byte_swap_32 (oldoffice34->encryptedVerifierHash[1]),
7573 byte_swap_32 (oldoffice34->encryptedVerifierHash[2]),
7574 byte_swap_32 (oldoffice34->encryptedVerifierHash[3]),
7575 byte_swap_32 (oldoffice34->encryptedVerifierHash[4]));
7576 }
7577 else if (hash_mode == 9810)
7578 {
7579 oldoffice34_t *oldoffice34s = (oldoffice34_t *) data.esalts_buf;
7580
7581 oldoffice34_t *oldoffice34 = &oldoffice34s[salt_pos];
7582
7583 snprintf (out_buf, len-1, "%s*%08x%08x%08x%08x*%08x%08x%08x%08x*%08x%08x%08x%08x%08x",
7584 (oldoffice34->version == 3) ? SIGNATURE_OLDOFFICE3 : SIGNATURE_OLDOFFICE4,
7585 salt.salt_buf[0],
7586 salt.salt_buf[1],
7587 salt.salt_buf[2],
7588 salt.salt_buf[3],
7589 byte_swap_32 (oldoffice34->encryptedVerifier[0]),
7590 byte_swap_32 (oldoffice34->encryptedVerifier[1]),
7591 byte_swap_32 (oldoffice34->encryptedVerifier[2]),
7592 byte_swap_32 (oldoffice34->encryptedVerifier[3]),
7593 byte_swap_32 (oldoffice34->encryptedVerifierHash[0]),
7594 byte_swap_32 (oldoffice34->encryptedVerifierHash[1]),
7595 byte_swap_32 (oldoffice34->encryptedVerifierHash[2]),
7596 byte_swap_32 (oldoffice34->encryptedVerifierHash[3]),
7597 byte_swap_32 (oldoffice34->encryptedVerifierHash[4]));
7598 }
7599 else if (hash_mode == 9820)
7600 {
7601 oldoffice34_t *oldoffice34s = (oldoffice34_t *) data.esalts_buf;
7602
7603 oldoffice34_t *oldoffice34 = &oldoffice34s[salt_pos];
7604
7605 u8 *rc4key = (u8 *) oldoffice34->rc4key;
7606
7607 snprintf (out_buf, len-1, "%s*%08x%08x%08x%08x*%08x%08x%08x%08x*%08x%08x%08x%08x%08x:%02x%02x%02x%02x%02x",
7608 (oldoffice34->version == 3) ? SIGNATURE_OLDOFFICE3 : SIGNATURE_OLDOFFICE4,
7609 salt.salt_buf[0],
7610 salt.salt_buf[1],
7611 salt.salt_buf[2],
7612 salt.salt_buf[3],
7613 byte_swap_32 (oldoffice34->encryptedVerifier[0]),
7614 byte_swap_32 (oldoffice34->encryptedVerifier[1]),
7615 byte_swap_32 (oldoffice34->encryptedVerifier[2]),
7616 byte_swap_32 (oldoffice34->encryptedVerifier[3]),
7617 byte_swap_32 (oldoffice34->encryptedVerifierHash[0]),
7618 byte_swap_32 (oldoffice34->encryptedVerifierHash[1]),
7619 byte_swap_32 (oldoffice34->encryptedVerifierHash[2]),
7620 byte_swap_32 (oldoffice34->encryptedVerifierHash[3]),
7621 byte_swap_32 (oldoffice34->encryptedVerifierHash[4]),
7622 rc4key[0],
7623 rc4key[1],
7624 rc4key[2],
7625 rc4key[3],
7626 rc4key[4]);
7627 }
7628 else if (hash_mode == 10000)
7629 {
7630 // salt
7631
7632 pbkdf2_sha256_t *pbkdf2_sha256s = (pbkdf2_sha256_t *) data.esalts_buf;
7633
7634 pbkdf2_sha256_t *pbkdf2_sha256 = &pbkdf2_sha256s[salt_pos];
7635
7636 unsigned char *salt_buf_ptr = (unsigned char *) pbkdf2_sha256->salt_buf;
7637
7638 // hash
7639
7640 digest_buf[0] = byte_swap_32 (digest_buf[0]);
7641 digest_buf[1] = byte_swap_32 (digest_buf[1]);
7642 digest_buf[2] = byte_swap_32 (digest_buf[2]);
7643 digest_buf[3] = byte_swap_32 (digest_buf[3]);
7644 digest_buf[4] = byte_swap_32 (digest_buf[4]);
7645 digest_buf[5] = byte_swap_32 (digest_buf[5]);
7646 digest_buf[6] = byte_swap_32 (digest_buf[6]);
7647 digest_buf[7] = byte_swap_32 (digest_buf[7]);
7648 digest_buf[8] = 0; // needed for base64_encode ()
7649
7650 char tmp_buf[64] = { 0 };
7651
7652 base64_encode (int_to_base64, (const u8 *) digest_buf, 32, (u8 *) tmp_buf);
7653
7654 // output
7655
7656 snprintf (out_buf, len-1, "%s%i$%s$%s", SIGNATURE_DJANGOPBKDF2, salt.salt_iter + 1, salt_buf_ptr, tmp_buf);
7657 }
7658 else if (hash_mode == 10100)
7659 {
7660 snprintf (out_buf, len-1, "%08x%08x:%u:%u:%08x%08x%08x%08x",
7661 digest_buf[0],
7662 digest_buf[1],
7663 2,
7664 4,
7665 byte_swap_32 (salt.salt_buf[0]),
7666 byte_swap_32 (salt.salt_buf[1]),
7667 byte_swap_32 (salt.salt_buf[2]),
7668 byte_swap_32 (salt.salt_buf[3]));
7669 }
7670 else if (hash_mode == 10200)
7671 {
7672 cram_md5_t *cram_md5s = (cram_md5_t *) data.esalts_buf;
7673
7674 cram_md5_t *cram_md5 = &cram_md5s[salt_pos];
7675
7676 // challenge
7677
7678 char challenge[100] = { 0 };
7679
7680 base64_encode (int_to_base64, (const u8 *) salt.salt_buf, salt.salt_len, (u8 *) challenge);
7681
7682 // response
7683
7684 char tmp_buf[100] = { 0 };
7685
7686 uint tmp_len = snprintf (tmp_buf, 100, "%s %08x%08x%08x%08x",
7687 (char *) cram_md5->user,
7688 digest_buf[0],
7689 digest_buf[1],
7690 digest_buf[2],
7691 digest_buf[3]);
7692
7693 char response[100] = { 0 };
7694
7695 base64_encode (int_to_base64, (const u8 *) tmp_buf, tmp_len, (u8 *) response);
7696
7697 snprintf (out_buf, len-1, "%s%s$%s", SIGNATURE_CRAM_MD5, challenge, response);
7698 }
7699 else if (hash_mode == 10300)
7700 {
7701 char tmp_buf[100] = { 0 };
7702
7703 memcpy (tmp_buf + 0, digest_buf, 20);
7704 memcpy (tmp_buf + 20, salt.salt_buf, salt.salt_len);
7705
7706 uint tmp_len = 20 + salt.salt_len;
7707
7708 // base64 encode it
7709
7710 char base64_encoded[100] = { 0 };
7711
7712 base64_encode (int_to_base64, (const u8 *) tmp_buf, tmp_len, (u8 *) base64_encoded);
7713
7714 snprintf (out_buf, len-1, "%s%i}%s", SIGNATURE_SAPH_SHA1, salt.salt_iter + 1, base64_encoded);
7715 }
7716 else if (hash_mode == 10400)
7717 {
7718 pdf_t *pdfs = (pdf_t *) data.esalts_buf;
7719
7720 pdf_t *pdf = &pdfs[salt_pos];
7721
7722 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",
7723
7724 pdf->V,
7725 pdf->R,
7726 40,
7727 pdf->P,
7728 pdf->enc_md,
7729 pdf->id_len,
7730 byte_swap_32 (pdf->id_buf[0]),
7731 byte_swap_32 (pdf->id_buf[1]),
7732 byte_swap_32 (pdf->id_buf[2]),
7733 byte_swap_32 (pdf->id_buf[3]),
7734 pdf->u_len,
7735 byte_swap_32 (pdf->u_buf[0]),
7736 byte_swap_32 (pdf->u_buf[1]),
7737 byte_swap_32 (pdf->u_buf[2]),
7738 byte_swap_32 (pdf->u_buf[3]),
7739 byte_swap_32 (pdf->u_buf[4]),
7740 byte_swap_32 (pdf->u_buf[5]),
7741 byte_swap_32 (pdf->u_buf[6]),
7742 byte_swap_32 (pdf->u_buf[7]),
7743 pdf->o_len,
7744 byte_swap_32 (pdf->o_buf[0]),
7745 byte_swap_32 (pdf->o_buf[1]),
7746 byte_swap_32 (pdf->o_buf[2]),
7747 byte_swap_32 (pdf->o_buf[3]),
7748 byte_swap_32 (pdf->o_buf[4]),
7749 byte_swap_32 (pdf->o_buf[5]),
7750 byte_swap_32 (pdf->o_buf[6]),
7751 byte_swap_32 (pdf->o_buf[7])
7752 );
7753 }
7754 else if (hash_mode == 10410)
7755 {
7756 pdf_t *pdfs = (pdf_t *) data.esalts_buf;
7757
7758 pdf_t *pdf = &pdfs[salt_pos];
7759
7760 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",
7761
7762 pdf->V,
7763 pdf->R,
7764 40,
7765 pdf->P,
7766 pdf->enc_md,
7767 pdf->id_len,
7768 byte_swap_32 (pdf->id_buf[0]),
7769 byte_swap_32 (pdf->id_buf[1]),
7770 byte_swap_32 (pdf->id_buf[2]),
7771 byte_swap_32 (pdf->id_buf[3]),
7772 pdf->u_len,
7773 byte_swap_32 (pdf->u_buf[0]),
7774 byte_swap_32 (pdf->u_buf[1]),
7775 byte_swap_32 (pdf->u_buf[2]),
7776 byte_swap_32 (pdf->u_buf[3]),
7777 byte_swap_32 (pdf->u_buf[4]),
7778 byte_swap_32 (pdf->u_buf[5]),
7779 byte_swap_32 (pdf->u_buf[6]),
7780 byte_swap_32 (pdf->u_buf[7]),
7781 pdf->o_len,
7782 byte_swap_32 (pdf->o_buf[0]),
7783 byte_swap_32 (pdf->o_buf[1]),
7784 byte_swap_32 (pdf->o_buf[2]),
7785 byte_swap_32 (pdf->o_buf[3]),
7786 byte_swap_32 (pdf->o_buf[4]),
7787 byte_swap_32 (pdf->o_buf[5]),
7788 byte_swap_32 (pdf->o_buf[6]),
7789 byte_swap_32 (pdf->o_buf[7])
7790 );
7791 }
7792 else if (hash_mode == 10420)
7793 {
7794 pdf_t *pdfs = (pdf_t *) data.esalts_buf;
7795
7796 pdf_t *pdf = &pdfs[salt_pos];
7797
7798 u8 *rc4key = (u8 *) pdf->rc4key;
7799
7800 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",
7801
7802 pdf->V,
7803 pdf->R,
7804 40,
7805 pdf->P,
7806 pdf->enc_md,
7807 pdf->id_len,
7808 byte_swap_32 (pdf->id_buf[0]),
7809 byte_swap_32 (pdf->id_buf[1]),
7810 byte_swap_32 (pdf->id_buf[2]),
7811 byte_swap_32 (pdf->id_buf[3]),
7812 pdf->u_len,
7813 byte_swap_32 (pdf->u_buf[0]),
7814 byte_swap_32 (pdf->u_buf[1]),
7815 byte_swap_32 (pdf->u_buf[2]),
7816 byte_swap_32 (pdf->u_buf[3]),
7817 byte_swap_32 (pdf->u_buf[4]),
7818 byte_swap_32 (pdf->u_buf[5]),
7819 byte_swap_32 (pdf->u_buf[6]),
7820 byte_swap_32 (pdf->u_buf[7]),
7821 pdf->o_len,
7822 byte_swap_32 (pdf->o_buf[0]),
7823 byte_swap_32 (pdf->o_buf[1]),
7824 byte_swap_32 (pdf->o_buf[2]),
7825 byte_swap_32 (pdf->o_buf[3]),
7826 byte_swap_32 (pdf->o_buf[4]),
7827 byte_swap_32 (pdf->o_buf[5]),
7828 byte_swap_32 (pdf->o_buf[6]),
7829 byte_swap_32 (pdf->o_buf[7]),
7830 rc4key[0],
7831 rc4key[1],
7832 rc4key[2],
7833 rc4key[3],
7834 rc4key[4]
7835 );
7836 }
7837 else if (hash_mode == 10500)
7838 {
7839 pdf_t *pdfs = (pdf_t *) data.esalts_buf;
7840
7841 pdf_t *pdf = &pdfs[salt_pos];
7842
7843 if (pdf->id_len == 32)
7844 {
7845 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",
7846
7847 pdf->V,
7848 pdf->R,
7849 128,
7850 pdf->P,
7851 pdf->enc_md,
7852 pdf->id_len,
7853 byte_swap_32 (pdf->id_buf[0]),
7854 byte_swap_32 (pdf->id_buf[1]),
7855 byte_swap_32 (pdf->id_buf[2]),
7856 byte_swap_32 (pdf->id_buf[3]),
7857 byte_swap_32 (pdf->id_buf[4]),
7858 byte_swap_32 (pdf->id_buf[5]),
7859 byte_swap_32 (pdf->id_buf[6]),
7860 byte_swap_32 (pdf->id_buf[7]),
7861 pdf->u_len,
7862 byte_swap_32 (pdf->u_buf[0]),
7863 byte_swap_32 (pdf->u_buf[1]),
7864 byte_swap_32 (pdf->u_buf[2]),
7865 byte_swap_32 (pdf->u_buf[3]),
7866 byte_swap_32 (pdf->u_buf[4]),
7867 byte_swap_32 (pdf->u_buf[5]),
7868 byte_swap_32 (pdf->u_buf[6]),
7869 byte_swap_32 (pdf->u_buf[7]),
7870 pdf->o_len,
7871 byte_swap_32 (pdf->o_buf[0]),
7872 byte_swap_32 (pdf->o_buf[1]),
7873 byte_swap_32 (pdf->o_buf[2]),
7874 byte_swap_32 (pdf->o_buf[3]),
7875 byte_swap_32 (pdf->o_buf[4]),
7876 byte_swap_32 (pdf->o_buf[5]),
7877 byte_swap_32 (pdf->o_buf[6]),
7878 byte_swap_32 (pdf->o_buf[7])
7879 );
7880 }
7881 else
7882 {
7883 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",
7884
7885 pdf->V,
7886 pdf->R,
7887 128,
7888 pdf->P,
7889 pdf->enc_md,
7890 pdf->id_len,
7891 byte_swap_32 (pdf->id_buf[0]),
7892 byte_swap_32 (pdf->id_buf[1]),
7893 byte_swap_32 (pdf->id_buf[2]),
7894 byte_swap_32 (pdf->id_buf[3]),
7895 pdf->u_len,
7896 byte_swap_32 (pdf->u_buf[0]),
7897 byte_swap_32 (pdf->u_buf[1]),
7898 byte_swap_32 (pdf->u_buf[2]),
7899 byte_swap_32 (pdf->u_buf[3]),
7900 byte_swap_32 (pdf->u_buf[4]),
7901 byte_swap_32 (pdf->u_buf[5]),
7902 byte_swap_32 (pdf->u_buf[6]),
7903 byte_swap_32 (pdf->u_buf[7]),
7904 pdf->o_len,
7905 byte_swap_32 (pdf->o_buf[0]),
7906 byte_swap_32 (pdf->o_buf[1]),
7907 byte_swap_32 (pdf->o_buf[2]),
7908 byte_swap_32 (pdf->o_buf[3]),
7909 byte_swap_32 (pdf->o_buf[4]),
7910 byte_swap_32 (pdf->o_buf[5]),
7911 byte_swap_32 (pdf->o_buf[6]),
7912 byte_swap_32 (pdf->o_buf[7])
7913 );
7914 }
7915 }
7916 else if (hash_mode == 10600)
7917 {
7918 uint digest_idx = salt.digests_offset + digest_pos;
7919
7920 hashinfo_t **hashinfo_ptr = data.hash_info;
7921 char *hash_buf = hashinfo_ptr[digest_idx]->orighash;
7922
7923 snprintf (out_buf, len-1, "%s", hash_buf);
7924 }
7925 else if (hash_mode == 10700)
7926 {
7927 uint digest_idx = salt.digests_offset + digest_pos;
7928
7929 hashinfo_t **hashinfo_ptr = data.hash_info;
7930 char *hash_buf = hashinfo_ptr[digest_idx]->orighash;
7931
7932 snprintf (out_buf, len-1, "%s", hash_buf);
7933 }
7934 else if (hash_mode == 10900)
7935 {
7936 uint digest_idx = salt.digests_offset + digest_pos;
7937
7938 hashinfo_t **hashinfo_ptr = data.hash_info;
7939 char *hash_buf = hashinfo_ptr[digest_idx]->orighash;
7940
7941 snprintf (out_buf, len-1, "%s", hash_buf);
7942 }
7943 else if (hash_mode == 11100)
7944 {
7945 u32 salt_challenge = salt.salt_buf[0];
7946
7947 salt_challenge = byte_swap_32 (salt_challenge);
7948
7949 unsigned char *user_name = (unsigned char *) (salt.salt_buf + 1);
7950
7951 snprintf (out_buf, len-1, "%s%s*%08x*%08x%08x%08x%08x",
7952 SIGNATURE_POSTGRESQL_AUTH,
7953 user_name,
7954 salt_challenge,
7955 digest_buf[0],
7956 digest_buf[1],
7957 digest_buf[2],
7958 digest_buf[3]);
7959 }
7960 else if (hash_mode == 11200)
7961 {
7962 snprintf (out_buf, len-1, "%s%s*%08x%08x%08x%08x%08x",
7963 SIGNATURE_MYSQL_AUTH,
7964 (unsigned char *) salt.salt_buf,
7965 digest_buf[0],
7966 digest_buf[1],
7967 digest_buf[2],
7968 digest_buf[3],
7969 digest_buf[4]);
7970 }
7971 else if (hash_mode == 11300)
7972 {
7973 bitcoin_wallet_t *bitcoin_wallets = (bitcoin_wallet_t *) data.esalts_buf;
7974
7975 bitcoin_wallet_t *bitcoin_wallet = &bitcoin_wallets[salt_pos];
7976
7977 const uint cry_master_len = bitcoin_wallet->cry_master_len;
7978 const uint ckey_len = bitcoin_wallet->ckey_len;
7979 const uint public_key_len = bitcoin_wallet->public_key_len;
7980
7981 char *cry_master_buf = (char *) mymalloc ((cry_master_len * 2) + 1);
7982 char *ckey_buf = (char *) mymalloc ((ckey_len * 2) + 1);
7983 char *public_key_buf = (char *) mymalloc ((public_key_len * 2) + 1);
7984
7985 for (uint i = 0, j = 0; i < cry_master_len; i += 1, j += 2)
7986 {
7987 const u8 *ptr = (const u8 *) bitcoin_wallet->cry_master_buf;
7988
7989 sprintf (cry_master_buf + j, "%02x", ptr[i]);
7990 }
7991
7992 for (uint i = 0, j = 0; i < ckey_len; i += 1, j += 2)
7993 {
7994 const u8 *ptr = (const u8 *) bitcoin_wallet->ckey_buf;
7995
7996 sprintf (ckey_buf + j, "%02x", ptr[i]);
7997 }
7998
7999 for (uint i = 0, j = 0; i < public_key_len; i += 1, j += 2)
8000 {
8001 const u8 *ptr = (const u8 *) bitcoin_wallet->public_key_buf;
8002
8003 sprintf (public_key_buf + j, "%02x", ptr[i]);
8004 }
8005
8006 snprintf (out_buf, len-1, "%s%d$%s$%d$%s$%d$%d$%s$%d$%s",
8007 SIGNATURE_BITCOIN_WALLET,
8008 cry_master_len * 2,
8009 cry_master_buf,
8010 salt.salt_len,
8011 (unsigned char *) salt.salt_buf,
8012 salt.salt_iter + 1,
8013 ckey_len * 2,
8014 ckey_buf,
8015 public_key_len * 2,
8016 public_key_buf
8017 );
8018
8019 free (cry_master_buf);
8020 free (ckey_buf);
8021 free (public_key_buf);
8022 }
8023 else if (hash_mode == 11400)
8024 {
8025 uint digest_idx = salt.digests_offset + digest_pos;
8026
8027 hashinfo_t **hashinfo_ptr = data.hash_info;
8028 char *hash_buf = hashinfo_ptr[digest_idx]->orighash;
8029
8030 snprintf (out_buf, len-1, "%s", hash_buf);
8031 }
8032 else if (hash_mode == 11600)
8033 {
8034 seven_zip_t *seven_zips = (seven_zip_t *) data.esalts_buf;
8035
8036 seven_zip_t *seven_zip = &seven_zips[salt_pos];
8037
8038 const uint data_len = seven_zip->data_len;
8039
8040 char *data_buf = (char *) mymalloc ((data_len * 2) + 1);
8041
8042 for (uint i = 0, j = 0; i < data_len; i += 1, j += 2)
8043 {
8044 const u8 *ptr = (const u8 *) seven_zip->data_buf;
8045
8046 sprintf (data_buf + j, "%02x", ptr[i]);
8047 }
8048
8049 snprintf (out_buf, len-1, "%s%u$%u$%u$%s$%u$%08x%08x%08x%08x$%u$%u$%u$%s",
8050 SIGNATURE_SEVEN_ZIP,
8051 0,
8052 salt.salt_sign[0],
8053 0,
8054 (char *) seven_zip->salt_buf,
8055 seven_zip->iv_len,
8056 seven_zip->iv_buf[0],
8057 seven_zip->iv_buf[1],
8058 seven_zip->iv_buf[2],
8059 seven_zip->iv_buf[3],
8060 seven_zip->crc,
8061 seven_zip->data_len,
8062 seven_zip->unpack_size,
8063 data_buf);
8064
8065 free (data_buf);
8066 }
8067 else if (hash_mode == 11700)
8068 {
8069 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x%08x%08x%08x",
8070 digest_buf[0],
8071 digest_buf[1],
8072 digest_buf[2],
8073 digest_buf[3],
8074 digest_buf[4],
8075 digest_buf[5],
8076 digest_buf[6],
8077 digest_buf[7]);
8078 }
8079 else if (hash_mode == 11800)
8080 {
8081 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
8082 digest_buf[ 0],
8083 digest_buf[ 1],
8084 digest_buf[ 2],
8085 digest_buf[ 3],
8086 digest_buf[ 4],
8087 digest_buf[ 5],
8088 digest_buf[ 6],
8089 digest_buf[ 7],
8090 digest_buf[ 8],
8091 digest_buf[ 9],
8092 digest_buf[10],
8093 digest_buf[11],
8094 digest_buf[12],
8095 digest_buf[13],
8096 digest_buf[14],
8097 digest_buf[15]);
8098 }
8099 else if (hash_mode == 11900)
8100 {
8101 uint digest_idx = salt.digests_offset + digest_pos;
8102
8103 hashinfo_t **hashinfo_ptr = data.hash_info;
8104 char *hash_buf = hashinfo_ptr[digest_idx]->orighash;
8105
8106 snprintf (out_buf, len-1, "%s", hash_buf);
8107 }
8108 else if (hash_mode == 12000)
8109 {
8110 uint digest_idx = salt.digests_offset + digest_pos;
8111
8112 hashinfo_t **hashinfo_ptr = data.hash_info;
8113 char *hash_buf = hashinfo_ptr[digest_idx]->orighash;
8114
8115 snprintf (out_buf, len-1, "%s", hash_buf);
8116 }
8117 else if (hash_mode == 12100)
8118 {
8119 uint digest_idx = salt.digests_offset + digest_pos;
8120
8121 hashinfo_t **hashinfo_ptr = data.hash_info;
8122 char *hash_buf = hashinfo_ptr[digest_idx]->orighash;
8123
8124 snprintf (out_buf, len-1, "%s", hash_buf);
8125 }
8126 else if (hash_mode == 12200)
8127 {
8128 uint *ptr_digest = digest_buf;
8129 uint *ptr_salt = salt.salt_buf;
8130
8131 snprintf (out_buf, len-1, "%s0$1$%08x%08x$%08x%08x",
8132 SIGNATURE_ECRYPTFS,
8133 ptr_salt[0],
8134 ptr_salt[1],
8135 ptr_digest[0],
8136 ptr_digest[1]);
8137 }
8138 else if (hash_mode == 12300)
8139 {
8140 uint *ptr_digest = digest_buf;
8141 uint *ptr_salt = salt.salt_buf;
8142
8143 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",
8144 ptr_digest[ 0], ptr_digest[ 1],
8145 ptr_digest[ 2], ptr_digest[ 3],
8146 ptr_digest[ 4], ptr_digest[ 5],
8147 ptr_digest[ 6], ptr_digest[ 7],
8148 ptr_digest[ 8], ptr_digest[ 9],
8149 ptr_digest[10], ptr_digest[11],
8150 ptr_digest[12], ptr_digest[13],
8151 ptr_digest[14], ptr_digest[15],
8152 ptr_salt[0],
8153 ptr_salt[1],
8154 ptr_salt[2],
8155 ptr_salt[3]);
8156 }
8157 else if (hash_mode == 12400)
8158 {
8159 // encode iteration count
8160
8161 char salt_iter[5] = { 0 };
8162
8163 salt_iter[0] = int_to_itoa64 ((salt.salt_iter ) & 0x3f);
8164 salt_iter[1] = int_to_itoa64 ((salt.salt_iter >> 6) & 0x3f);
8165 salt_iter[2] = int_to_itoa64 ((salt.salt_iter >> 12) & 0x3f);
8166 salt_iter[3] = int_to_itoa64 ((salt.salt_iter >> 18) & 0x3f);
8167 salt_iter[4] = 0;
8168
8169 // encode salt
8170
8171 ptr_salt[0] = int_to_itoa64 ((salt.salt_buf[0] ) & 0x3f);
8172 ptr_salt[1] = int_to_itoa64 ((salt.salt_buf[0] >> 6) & 0x3f);
8173 ptr_salt[2] = int_to_itoa64 ((salt.salt_buf[0] >> 12) & 0x3f);
8174 ptr_salt[3] = int_to_itoa64 ((salt.salt_buf[0] >> 18) & 0x3f);
8175 ptr_salt[4] = 0;
8176
8177 // encode digest
8178
8179 memset (tmp_buf, 0, sizeof (tmp_buf));
8180
8181 digest_buf[0] = byte_swap_32 (digest_buf[0]);
8182 digest_buf[1] = byte_swap_32 (digest_buf[1]);
8183
8184 memcpy (tmp_buf, digest_buf, 8);
8185
8186 base64_encode (int_to_itoa64, (const u8 *) tmp_buf, 8, (u8 *) ptr_plain);
8187
8188 ptr_plain[11] = 0;
8189
8190 // fill the resulting buffer
8191
8192 snprintf (out_buf, len - 1, "_%s%s%s", salt_iter, ptr_salt, ptr_plain);
8193 }
8194 else if (hash_mode == 12500)
8195 {
8196 snprintf (out_buf, len - 1, "%s*0*%08x%08x*%08x%08x%08x%08x",
8197 SIGNATURE_RAR3,
8198 byte_swap_32 (salt.salt_buf[0]),
8199 byte_swap_32 (salt.salt_buf[1]),
8200 salt.salt_buf[2],
8201 salt.salt_buf[3],
8202 salt.salt_buf[4],
8203 salt.salt_buf[5]);
8204 }
8205 else if (hash_mode == 12600)
8206 {
8207 snprintf (out_buf, len - 1, "%08x%08x%08x%08x%08x%08x%08x%08x",
8208 digest_buf[0] + salt.salt_buf_pc[0],
8209 digest_buf[1] + salt.salt_buf_pc[1],
8210 digest_buf[2] + salt.salt_buf_pc[2],
8211 digest_buf[3] + salt.salt_buf_pc[3],
8212 digest_buf[4] + salt.salt_buf_pc[4],
8213 digest_buf[5] + salt.salt_buf_pc[5],
8214 digest_buf[6] + salt.salt_buf_pc[6],
8215 digest_buf[7] + salt.salt_buf_pc[7]);
8216 }
8217 else if (hash_mode == 12700)
8218 {
8219 uint digest_idx = salt.digests_offset + digest_pos;
8220
8221 hashinfo_t **hashinfo_ptr = data.hash_info;
8222 char *hash_buf = hashinfo_ptr[digest_idx]->orighash;
8223
8224 snprintf (out_buf, len-1, "%s", hash_buf);
8225 }
8226 else if (hash_mode == 12800)
8227 {
8228 const u8 *ptr = (const u8 *) salt.salt_buf;
8229
8230 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",
8231 SIGNATURE_MS_DRSR,
8232 ptr[0],
8233 ptr[1],
8234 ptr[2],
8235 ptr[3],
8236 ptr[4],
8237 ptr[5],
8238 ptr[6],
8239 ptr[7],
8240 ptr[8],
8241 ptr[9],
8242 salt.salt_iter + 1,
8243 byte_swap_32 (digest_buf[0]),
8244 byte_swap_32 (digest_buf[1]),
8245 byte_swap_32 (digest_buf[2]),
8246 byte_swap_32 (digest_buf[3]),
8247 byte_swap_32 (digest_buf[4]),
8248 byte_swap_32 (digest_buf[5]),
8249 byte_swap_32 (digest_buf[6]),
8250 byte_swap_32 (digest_buf[7])
8251 );
8252 }
8253 else if (hash_mode == 12900)
8254 {
8255 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",
8256 salt.salt_buf[ 4],
8257 salt.salt_buf[ 5],
8258 salt.salt_buf[ 6],
8259 salt.salt_buf[ 7],
8260 salt.salt_buf[ 8],
8261 salt.salt_buf[ 9],
8262 salt.salt_buf[10],
8263 salt.salt_buf[11],
8264 byte_swap_32 (digest_buf[0]),
8265 byte_swap_32 (digest_buf[1]),
8266 byte_swap_32 (digest_buf[2]),
8267 byte_swap_32 (digest_buf[3]),
8268 byte_swap_32 (digest_buf[4]),
8269 byte_swap_32 (digest_buf[5]),
8270 byte_swap_32 (digest_buf[6]),
8271 byte_swap_32 (digest_buf[7]),
8272 salt.salt_buf[ 0],
8273 salt.salt_buf[ 1],
8274 salt.salt_buf[ 2],
8275 salt.salt_buf[ 3]
8276 );
8277 }
8278 else if (hash_mode == 13000)
8279 {
8280 rar5_t *rar5s = (rar5_t *) data.esalts_buf;
8281
8282 rar5_t *rar5 = &rar5s[salt_pos];
8283
8284 snprintf (out_buf, len-1, "$rar5$16$%08x%08x%08x%08x$%u$%08x%08x%08x%08x$8$%08x%08x",
8285 salt.salt_buf[0],
8286 salt.salt_buf[1],
8287 salt.salt_buf[2],
8288 salt.salt_buf[3],
8289 salt.salt_sign[0],
8290 rar5->iv[0],
8291 rar5->iv[1],
8292 rar5->iv[2],
8293 rar5->iv[3],
8294 byte_swap_32 (digest_buf[0]),
8295 byte_swap_32 (digest_buf[1])
8296 );
8297 }
8298 else if (hash_mode == 13100)
8299 {
8300 krb5tgs_t *krb5tgss = (krb5tgs_t *) data.esalts_buf;
8301
8302 krb5tgs_t *krb5tgs = &krb5tgss[salt_pos];
8303
8304 u8 *ptr_checksum = (u8 *) krb5tgs->checksum;
8305 u8 *ptr_edata2 = (u8 *) krb5tgs->edata2;
8306
8307 char data[2560 * 4 * 2] = { 0 };
8308
8309 char *ptr_data = data;
8310
8311 for (uint i = 0; i < 16; i++, ptr_data += 2)
8312 sprintf (ptr_data, "%02x", ptr_checksum[i]);
8313
8314 /* skip '$' */
8315 ptr_data++;
8316
8317 for (uint i = 0; i < krb5tgs->edata2_len; i++, ptr_data += 2)
8318 sprintf (ptr_data, "%02x", ptr_edata2[i]);
8319
8320 snprintf (out_buf, len-1, "%s$%s$%s$%s",
8321 SIGNATURE_KRB5TGS,
8322 (char *) krb5tgs->account_info,
8323 data,
8324 data + 33);
8325 }
8326 else if (hash_mode == 13200)
8327 {
8328 snprintf (out_buf, len-1, "%s*%d*%08x%08x%08x%08x*%08x%08x%08x%08x%08x%08x",
8329 SIGNATURE_AXCRYPT,
8330 salt.salt_iter,
8331 salt.salt_buf[0],
8332 salt.salt_buf[1],
8333 salt.salt_buf[2],
8334 salt.salt_buf[3],
8335 salt.salt_buf[4],
8336 salt.salt_buf[5],
8337 salt.salt_buf[6],
8338 salt.salt_buf[7],
8339 salt.salt_buf[8],
8340 salt.salt_buf[9]);
8341 }
8342 else if (hash_mode == 13300)
8343 {
8344 snprintf (out_buf, len-1, "%s$%08x%08x%08x%08x",
8345 SIGNATURE_AXCRYPT_SHA1,
8346 digest_buf[0],
8347 digest_buf[1],
8348 digest_buf[2],
8349 digest_buf[3]);
8350 }
8351 else
8352 {
8353 if (hash_type == HASH_TYPE_MD4)
8354 {
8355 snprintf (out_buf, 255, "%08x%08x%08x%08x",
8356 digest_buf[0],
8357 digest_buf[1],
8358 digest_buf[2],
8359 digest_buf[3]);
8360 }
8361 else if (hash_type == HASH_TYPE_MD5)
8362 {
8363 snprintf (out_buf, len-1, "%08x%08x%08x%08x",
8364 digest_buf[0],
8365 digest_buf[1],
8366 digest_buf[2],
8367 digest_buf[3]);
8368 }
8369 else if (hash_type == HASH_TYPE_SHA1)
8370 {
8371 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x",
8372 digest_buf[0],
8373 digest_buf[1],
8374 digest_buf[2],
8375 digest_buf[3],
8376 digest_buf[4]);
8377 }
8378 else if (hash_type == HASH_TYPE_SHA256)
8379 {
8380 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x%08x%08x%08x",
8381 digest_buf[0],
8382 digest_buf[1],
8383 digest_buf[2],
8384 digest_buf[3],
8385 digest_buf[4],
8386 digest_buf[5],
8387 digest_buf[6],
8388 digest_buf[7]);
8389 }
8390 else if (hash_type == HASH_TYPE_SHA384)
8391 {
8392 uint *ptr = digest_buf;
8393
8394 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
8395 ptr[ 1], ptr[ 0],
8396 ptr[ 3], ptr[ 2],
8397 ptr[ 5], ptr[ 4],
8398 ptr[ 7], ptr[ 6],
8399 ptr[ 9], ptr[ 8],
8400 ptr[11], ptr[10]);
8401 }
8402 else if (hash_type == HASH_TYPE_SHA512)
8403 {
8404 uint *ptr = digest_buf;
8405
8406 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
8407 ptr[ 1], ptr[ 0],
8408 ptr[ 3], ptr[ 2],
8409 ptr[ 5], ptr[ 4],
8410 ptr[ 7], ptr[ 6],
8411 ptr[ 9], ptr[ 8],
8412 ptr[11], ptr[10],
8413 ptr[13], ptr[12],
8414 ptr[15], ptr[14]);
8415 }
8416 else if (hash_type == HASH_TYPE_LM)
8417 {
8418 snprintf (out_buf, len-1, "%08x%08x",
8419 digest_buf[0],
8420 digest_buf[1]);
8421 }
8422 else if (hash_type == HASH_TYPE_ORACLEH)
8423 {
8424 snprintf (out_buf, len-1, "%08X%08X",
8425 digest_buf[0],
8426 digest_buf[1]);
8427 }
8428 else if (hash_type == HASH_TYPE_BCRYPT)
8429 {
8430 base64_encode (int_to_bf64, (const u8 *) salt.salt_buf, 16, (u8 *) tmp_buf + 0);
8431 base64_encode (int_to_bf64, (const u8 *) digest_buf, 23, (u8 *) tmp_buf + 22);
8432
8433 tmp_buf[22 + 31] = 0; // base64_encode wants to pad
8434
8435 snprintf (out_buf, len-1, "%s$%s", (char *) salt.salt_sign, tmp_buf);
8436 }
8437 else if (hash_type == HASH_TYPE_KECCAK)
8438 {
8439 uint *ptr = digest_buf;
8440
8441 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",
8442 ptr[ 1], ptr[ 0],
8443 ptr[ 3], ptr[ 2],
8444 ptr[ 5], ptr[ 4],
8445 ptr[ 7], ptr[ 6],
8446 ptr[ 9], ptr[ 8],
8447 ptr[11], ptr[10],
8448 ptr[13], ptr[12],
8449 ptr[15], ptr[14],
8450 ptr[17], ptr[16],
8451 ptr[19], ptr[18],
8452 ptr[21], ptr[20],
8453 ptr[23], ptr[22],
8454 ptr[25], ptr[24],
8455 ptr[27], ptr[26],
8456 ptr[29], ptr[28],
8457 ptr[31], ptr[30],
8458 ptr[33], ptr[32],
8459 ptr[35], ptr[34],
8460 ptr[37], ptr[36],
8461 ptr[39], ptr[38],
8462 ptr[41], ptr[30],
8463 ptr[43], ptr[42],
8464 ptr[45], ptr[44],
8465 ptr[47], ptr[46],
8466 ptr[49], ptr[48]
8467 );
8468
8469 out_buf[salt.keccak_mdlen * 2] = 0;
8470 }
8471 else if (hash_type == HASH_TYPE_RIPEMD160)
8472 {
8473 snprintf (out_buf, 255, "%08x%08x%08x%08x%08x",
8474 digest_buf[0],
8475 digest_buf[1],
8476 digest_buf[2],
8477 digest_buf[3],
8478 digest_buf[4]);
8479 }
8480 else if (hash_type == HASH_TYPE_WHIRLPOOL)
8481 {
8482 digest_buf[ 0] = digest_buf[ 0];
8483 digest_buf[ 1] = digest_buf[ 1];
8484 digest_buf[ 2] = digest_buf[ 2];
8485 digest_buf[ 3] = digest_buf[ 3];
8486 digest_buf[ 4] = digest_buf[ 4];
8487 digest_buf[ 5] = digest_buf[ 5];
8488 digest_buf[ 6] = digest_buf[ 6];
8489 digest_buf[ 7] = digest_buf[ 7];
8490 digest_buf[ 8] = digest_buf[ 8];
8491 digest_buf[ 9] = digest_buf[ 9];
8492 digest_buf[10] = digest_buf[10];
8493 digest_buf[11] = digest_buf[11];
8494 digest_buf[12] = digest_buf[12];
8495 digest_buf[13] = digest_buf[13];
8496 digest_buf[14] = digest_buf[14];
8497 digest_buf[15] = digest_buf[15];
8498
8499 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x",
8500 digest_buf[ 0],
8501 digest_buf[ 1],
8502 digest_buf[ 2],
8503 digest_buf[ 3],
8504 digest_buf[ 4],
8505 digest_buf[ 5],
8506 digest_buf[ 6],
8507 digest_buf[ 7],
8508 digest_buf[ 8],
8509 digest_buf[ 9],
8510 digest_buf[10],
8511 digest_buf[11],
8512 digest_buf[12],
8513 digest_buf[13],
8514 digest_buf[14],
8515 digest_buf[15]);
8516 }
8517 else if (hash_type == HASH_TYPE_GOST)
8518 {
8519 snprintf (out_buf, len-1, "%08x%08x%08x%08x%08x%08x%08x%08x",
8520 digest_buf[0],
8521 digest_buf[1],
8522 digest_buf[2],
8523 digest_buf[3],
8524 digest_buf[4],
8525 digest_buf[5],
8526 digest_buf[6],
8527 digest_buf[7]);
8528 }
8529 else if (hash_type == HASH_TYPE_MYSQL)
8530 {
8531 snprintf (out_buf, len-1, "%08x%08x",
8532 digest_buf[0],
8533 digest_buf[1]);
8534 }
8535 else if (hash_type == HASH_TYPE_LOTUS5)
8536 {
8537 snprintf (out_buf, len-1, "%08x%08x%08x%08x",
8538 digest_buf[0],
8539 digest_buf[1],
8540 digest_buf[2],
8541 digest_buf[3]);
8542 }
8543 else if (hash_type == HASH_TYPE_LOTUS6)
8544 {
8545 digest_buf[ 0] = byte_swap_32 (digest_buf[ 0]);
8546 digest_buf[ 1] = byte_swap_32 (digest_buf[ 1]);
8547 digest_buf[ 2] = byte_swap_32 (digest_buf[ 2]);
8548 digest_buf[ 3] = byte_swap_32 (digest_buf[ 3]);
8549
8550 char buf[16] = { 0 };
8551
8552 memcpy (buf + 0, salt.salt_buf, 5);
8553 memcpy (buf + 5, digest_buf, 9);
8554
8555 buf[3] -= -4;
8556
8557 base64_encode (int_to_lotus64, (const u8 *) buf, 14, (u8 *) tmp_buf);
8558
8559 tmp_buf[18] = salt.salt_buf_pc[7];
8560 tmp_buf[19] = 0;
8561
8562 snprintf (out_buf, len-1, "(G%s)", tmp_buf);
8563 }
8564 else if (hash_type == HASH_TYPE_LOTUS8)
8565 {
8566 char buf[52] = { 0 };
8567
8568 // salt
8569
8570 memcpy (buf + 0, salt.salt_buf, 16);
8571
8572 buf[3] -= -4;
8573
8574 // iteration
8575
8576 snprintf (buf + 16, 11, "%010i", salt.salt_iter + 1);
8577
8578 // chars
8579
8580 buf[26] = salt.salt_buf_pc[0];
8581 buf[27] = salt.salt_buf_pc[1];
8582
8583 // digest
8584
8585 memcpy (buf + 28, digest_buf, 8);
8586
8587 base64_encode (int_to_lotus64, (const u8 *) buf, 36, (u8 *) tmp_buf);
8588
8589 tmp_buf[49] = 0;
8590
8591 snprintf (out_buf, len-1, "(H%s)", tmp_buf);
8592 }
8593 else if (hash_type == HASH_TYPE_CRC32)
8594 {
8595 snprintf (out_buf, len-1, "%08x", byte_swap_32 (digest_buf[0]));
8596 }
8597 }
8598
8599 if (salt_type == SALT_TYPE_INTERN)
8600 {
8601 size_t pos = strlen (out_buf);
8602
8603 out_buf[pos] = data.separator;
8604
8605 char *ptr = (char *) salt.salt_buf;
8606
8607 memcpy (out_buf + pos + 1, ptr, salt.salt_len);
8608
8609 out_buf[pos + 1 + salt.salt_len] = 0;
8610 }
8611 }
8612
8613 void to_hccap_t (hccap_t *hccap, uint salt_pos, uint digest_pos)
8614 {
8615 memset (hccap, 0, sizeof (hccap_t));
8616
8617 salt_t *salt = &data.salts_buf[salt_pos];
8618
8619 memcpy (hccap->essid, salt->salt_buf, salt->salt_len);
8620
8621 wpa_t *wpas = (wpa_t *) data.esalts_buf;
8622 wpa_t *wpa = &wpas[salt_pos];
8623
8624 hccap->keyver = wpa->keyver;
8625
8626 hccap->eapol_size = wpa->eapol_size;
8627
8628 if (wpa->keyver != 1)
8629 {
8630 uint eapol_tmp[64] = { 0 };
8631
8632 for (uint i = 0; i < 64; i++)
8633 {
8634 eapol_tmp[i] = byte_swap_32 (wpa->eapol[i]);
8635 }
8636
8637 memcpy (hccap->eapol, eapol_tmp, wpa->eapol_size);
8638 }
8639 else
8640 {
8641 memcpy (hccap->eapol, wpa->eapol, wpa->eapol_size);
8642 }
8643
8644 uint pke_tmp[25] = { 0 };
8645
8646 for (int i = 5; i < 25; i++)
8647 {
8648 pke_tmp[i] = byte_swap_32 (wpa->pke[i]);
8649 }
8650
8651 char *pke_ptr = (char *) pke_tmp;
8652
8653 memcpy (hccap->mac1, pke_ptr + 23, 6);
8654 memcpy (hccap->mac2, pke_ptr + 29, 6);
8655 memcpy (hccap->nonce1, pke_ptr + 67, 32);
8656 memcpy (hccap->nonce2, pke_ptr + 35, 32);
8657
8658 char *digests_buf_ptr = (char *) data.digests_buf;
8659
8660 uint dgst_size = data.dgst_size;
8661
8662 uint *digest_ptr = (uint *) (digests_buf_ptr + (data.salts_buf[salt_pos].digests_offset * dgst_size) + (digest_pos * dgst_size));
8663
8664 if (wpa->keyver != 1)
8665 {
8666 uint digest_tmp[4] = { 0 };
8667
8668 digest_tmp[0] = byte_swap_32 (digest_ptr[0]);
8669 digest_tmp[1] = byte_swap_32 (digest_ptr[1]);
8670 digest_tmp[2] = byte_swap_32 (digest_ptr[2]);
8671 digest_tmp[3] = byte_swap_32 (digest_ptr[3]);
8672
8673 memcpy (hccap->keymic, digest_tmp, 16);
8674 }
8675 else
8676 {
8677 memcpy (hccap->keymic, digest_ptr, 16);
8678 }
8679 }
8680
8681 void SuspendThreads ()
8682 {
8683 if (data.devices_status == STATUS_RUNNING)
8684 {
8685 hc_timer_set (&data.timer_paused);
8686
8687 data.devices_status = STATUS_PAUSED;
8688
8689 log_info ("Paused");
8690 }
8691 }
8692
8693 void ResumeThreads ()
8694 {
8695 if (data.devices_status == STATUS_PAUSED)
8696 {
8697 float ms_paused;
8698
8699 hc_timer_get (data.timer_paused, ms_paused);
8700
8701 data.ms_paused += ms_paused;
8702
8703 data.devices_status = STATUS_RUNNING;
8704
8705 log_info ("Resumed");
8706 }
8707 }
8708
8709 void bypass ()
8710 {
8711 if (data.devices_status != STATUS_RUNNING) return;
8712
8713 data.devices_status = STATUS_BYPASS;
8714
8715 log_info ("Next dictionary / mask in queue selected, bypassing current one");
8716 }
8717
8718 void stop_at_checkpoint ()
8719 {
8720 if (data.devices_status != STATUS_STOP_AT_CHECKPOINT)
8721 {
8722 if (data.devices_status != STATUS_RUNNING) return;
8723 }
8724
8725 // this feature only makes sense if --restore-disable was not specified
8726
8727 if (data.restore_disable == 1)
8728 {
8729 log_info ("WARNING: this feature is disabled when --restore-disable was specified");
8730
8731 return;
8732 }
8733
8734 // check if monitoring of Restore Point updates should be enabled or disabled
8735
8736 if (data.devices_status != STATUS_STOP_AT_CHECKPOINT)
8737 {
8738 data.devices_status = STATUS_STOP_AT_CHECKPOINT;
8739
8740 // save the current restore point value
8741
8742 data.checkpoint_cur_words = get_lowest_words_done ();
8743
8744 log_info ("Checkpoint enabled: will quit at next Restore Point update");
8745 }
8746 else
8747 {
8748 data.devices_status = STATUS_RUNNING;
8749
8750 // reset the global value for checkpoint checks
8751
8752 data.checkpoint_cur_words = 0;
8753
8754 log_info ("Checkpoint disabled: Restore Point updates will no longer be monitored");
8755 }
8756 }
8757
8758 void myabort ()
8759 {
8760 if (data.devices_status == STATUS_INIT) return;
8761 if (data.devices_status == STATUS_STARTING) return;
8762
8763 data.devices_status = STATUS_ABORTED;
8764 }
8765
8766 void myquit ()
8767 {
8768 if (data.devices_status == STATUS_INIT) return;
8769 if (data.devices_status == STATUS_STARTING) return;
8770
8771 data.devices_status = STATUS_QUIT;
8772 }
8773
8774 void load_kernel (const char *kernel_file, int num_devices, size_t *kernel_lengths, const u8 **kernel_sources)
8775 {
8776 FILE *fp = fopen (kernel_file, "rb");
8777
8778 if (fp != NULL)
8779 {
8780 struct stat st;
8781
8782 memset (&st, 0, sizeof (st));
8783
8784 stat (kernel_file, &st);
8785
8786 u8 *buf = (u8 *) mymalloc (st.st_size + 1);
8787
8788 size_t num_read = fread (buf, sizeof (u8), st.st_size, fp);
8789
8790 if (num_read != (size_t) st.st_size)
8791 {
8792 log_error ("ERROR: %s: %s", kernel_file, strerror (errno));
8793
8794 exit (-1);
8795 }
8796
8797 fclose (fp);
8798
8799 buf[st.st_size] = 0;
8800
8801 for (int i = 0; i < num_devices; i++)
8802 {
8803 kernel_lengths[i] = (size_t) st.st_size;
8804
8805 kernel_sources[i] = buf;
8806 }
8807 }
8808 else
8809 {
8810 log_error ("ERROR: %s: %s", kernel_file, strerror (errno));
8811
8812 exit (-1);
8813 }
8814
8815 return;
8816 }
8817
8818 void writeProgramBin (char *dst, u8 *binary, size_t binary_size)
8819 {
8820 if (binary_size > 0)
8821 {
8822 FILE *fp = fopen (dst, "wb");
8823
8824 lock_file (fp);
8825 fwrite (binary, sizeof (u8), binary_size, fp);
8826
8827 fflush (fp);
8828 fclose (fp);
8829 }
8830 }
8831
8832 /**
8833 * restore
8834 */
8835
8836 restore_data_t *init_restore (int argc, char **argv)
8837 {
8838 restore_data_t *rd = (restore_data_t *) mymalloc (sizeof (restore_data_t));
8839
8840 if (data.restore_disable == 0)
8841 {
8842 FILE *fp = fopen (data.eff_restore_file, "rb");
8843
8844 if (fp)
8845 {
8846 size_t nread = fread (rd, sizeof (restore_data_t), 1, fp);
8847
8848 if (nread != 1)
8849 {
8850 log_error ("ERROR: cannot read %s", data.eff_restore_file);
8851
8852 exit (-1);
8853 }
8854
8855 fclose (fp);
8856
8857 if (rd->pid)
8858 {
8859 char pidbin[BUFSIZ] = { 0 };
8860
8861 int pidbin_len = -1;
8862
8863 #ifdef _POSIX
8864 snprintf (pidbin, sizeof (pidbin) - 1, "/proc/%d/cmdline", rd->pid);
8865
8866 FILE *fd = fopen (pidbin, "rb");
8867
8868 if (fd)
8869 {
8870 pidbin_len = fread (pidbin, 1, BUFSIZ, fd);
8871
8872 pidbin[pidbin_len] = 0;
8873
8874 fclose (fd);
8875
8876 char *argv0_r = strrchr (argv[0], '/');
8877
8878 char *pidbin_r = strrchr (pidbin, '/');
8879
8880 if (argv0_r == NULL) argv0_r = argv[0];
8881
8882 if (pidbin_r == NULL) pidbin_r = pidbin;
8883
8884 if (strcmp (argv0_r, pidbin_r) == 0)
8885 {
8886 log_error ("ERROR: already an instance %s running on pid %d", pidbin, rd->pid);
8887
8888 exit (-1);
8889 }
8890 }
8891
8892 #elif _WIN
8893 HANDLE hProcess = OpenProcess (PROCESS_ALL_ACCESS, FALSE, rd->pid);
8894
8895 char pidbin2[BUFSIZ] = { 0 };
8896
8897 int pidbin2_len = -1;
8898
8899 pidbin_len = GetModuleFileName (NULL, pidbin, BUFSIZ);
8900 pidbin2_len = GetModuleFileNameEx (hProcess, NULL, pidbin2, BUFSIZ);
8901
8902 pidbin[pidbin_len] = 0;
8903 pidbin2[pidbin2_len] = 0;
8904
8905 if (pidbin2_len)
8906 {
8907 if (strcmp (pidbin, pidbin2) == 0)
8908 {
8909 log_error ("ERROR: already an instance %s running on pid %d", pidbin2, rd->pid);
8910
8911 exit (-1);
8912 }
8913 }
8914 #endif
8915 }
8916
8917 if (rd->version_bin < RESTORE_MIN)
8918 {
8919 log_error ("ERROR: cannot use outdated %s. Please remove it.", data.eff_restore_file);
8920
8921 exit (-1);
8922 }
8923 }
8924 }
8925
8926 memset (rd, 0, sizeof (restore_data_t));
8927
8928 rd->version_bin = VERSION_BIN;
8929
8930 #ifdef _POSIX
8931 rd->pid = getpid ();
8932 #elif _WIN
8933 rd->pid = GetCurrentProcessId ();
8934 #endif
8935
8936 if (getcwd (rd->cwd, 255) == NULL)
8937 {
8938 myfree (rd);
8939
8940 return (NULL);
8941 }
8942
8943 rd->argc = argc;
8944 rd->argv = argv;
8945
8946 return (rd);
8947 }
8948
8949 void read_restore (const char *eff_restore_file, restore_data_t *rd)
8950 {
8951 FILE *fp = fopen (eff_restore_file, "rb");
8952
8953 if (fp == NULL)
8954 {
8955 log_error ("ERROR: restore file '%s': %s", eff_restore_file, strerror (errno));
8956
8957 exit (-1);
8958 }
8959
8960 if (fread (rd, sizeof (restore_data_t), 1, fp) != 1)
8961 {
8962 log_error ("ERROR: cannot read %s", eff_restore_file);
8963
8964 exit (-1);
8965 }
8966
8967 rd->argv = (char **) mycalloc (rd->argc, sizeof (char *));
8968
8969 for (uint i = 0; i < rd->argc; i++)
8970 {
8971 char buf[BUFSIZ] = { 0 };
8972
8973 if (fgets (buf, BUFSIZ - 1, fp) == NULL)
8974 {
8975 log_error ("ERROR: cannot read %s", eff_restore_file);
8976
8977 exit (-1);
8978 }
8979
8980 size_t len = strlen (buf);
8981
8982 if (len) buf[len - 1] = 0;
8983
8984 rd->argv[i] = mystrdup (buf);
8985 }
8986
8987 fclose (fp);
8988
8989 char new_cwd[1024] = { 0 };
8990
8991 char *nwd = getcwd (new_cwd, sizeof (new_cwd));
8992
8993 if (nwd == NULL)
8994 {
8995 log_error ("Restore file is corrupted");
8996 }
8997
8998 if (strncmp (new_cwd, rd->cwd, sizeof (new_cwd)) != 0)
8999 {
9000 if (getcwd (rd->cwd, sizeof (rd->cwd)) == NULL)
9001 {
9002 log_error ("ERROR: could not determine current user path: %s", strerror (errno));
9003
9004 exit (-1);
9005 }
9006
9007 log_info ("WARNING: Found old restore file, updating path to %s...", new_cwd);
9008 }
9009
9010 if (chdir (rd->cwd))
9011 {
9012 log_error ("ERROR: cannot chdir to %s: %s", rd->cwd, strerror (errno));
9013
9014 exit (-1);
9015 }
9016 }
9017
9018 u64 get_lowest_words_done ()
9019 {
9020 u64 words_cur = -1;
9021
9022 for (uint device_id = 0; device_id < data.devices_cnt; device_id++)
9023 {
9024 hc_device_param_t *device_param = &data.devices_param[device_id];
9025
9026 if (device_param->skipped) continue;
9027
9028 const u64 words_done = device_param->words_done;
9029
9030 if (words_done < words_cur) words_cur = words_done;
9031 }
9032
9033 // It's possible that a device's workload isn't finished right after a restore-case.
9034 // In that case, this function would return 0 and overwrite the real restore point
9035 // There's also data.words_cur which is set to rd->words_cur but it changes while
9036 // the attack is running therefore we should stick to rd->words_cur.
9037 // Note that -s influences rd->words_cur we should keep a close look on that.
9038
9039 if (words_cur < data.rd->words_cur) words_cur = data.rd->words_cur;
9040
9041 return words_cur;
9042 }
9043
9044 void write_restore (const char *new_restore_file, restore_data_t *rd)
9045 {
9046 u64 words_cur = get_lowest_words_done ();
9047
9048 rd->words_cur = words_cur;
9049
9050 FILE *fp = fopen (new_restore_file, "wb");
9051
9052 if (fp == NULL)
9053 {
9054 log_error ("ERROR: %s: %s", new_restore_file, strerror (errno));
9055
9056 exit (-1);
9057 }
9058
9059 if (setvbuf (fp, NULL, _IONBF, 0))
9060 {
9061 log_error ("ERROR: setvbuf file '%s': %s", new_restore_file, strerror (errno));
9062
9063 exit (-1);
9064 }
9065
9066 fwrite (rd, sizeof (restore_data_t), 1, fp);
9067
9068 for (uint i = 0; i < rd->argc; i++)
9069 {
9070 fprintf (fp, "%s", rd->argv[i]);
9071 fputc ('\n', fp);
9072 }
9073
9074 fflush (fp);
9075
9076 fsync (fileno (fp));
9077
9078 fclose (fp);
9079 }
9080
9081 void cycle_restore ()
9082 {
9083 const char *eff_restore_file = data.eff_restore_file;
9084 const char *new_restore_file = data.new_restore_file;
9085
9086 restore_data_t *rd = data.rd;
9087
9088 write_restore (new_restore_file, rd);
9089
9090 struct stat st;
9091
9092 memset (&st, 0, sizeof(st));
9093
9094 if (stat (eff_restore_file, &st) == 0)
9095 {
9096 if (unlink (eff_restore_file))
9097 {
9098 log_info ("WARN: unlink file '%s': %s", eff_restore_file, strerror (errno));
9099 }
9100 }
9101
9102 if (rename (new_restore_file, eff_restore_file))
9103 {
9104 log_info ("WARN: rename file '%s' to '%s': %s", new_restore_file, eff_restore_file, strerror (errno));
9105 }
9106 }
9107
9108 void check_checkpoint ()
9109 {
9110 // if (data.restore_disable == 1) break; (this is already implied by previous checks)
9111
9112 u64 words_cur = get_lowest_words_done ();
9113
9114 if (words_cur != data.checkpoint_cur_words)
9115 {
9116 myabort ();
9117 }
9118 }
9119
9120 /**
9121 * tuning db
9122 */
9123
9124 void tuning_db_destroy (tuning_db_t *tuning_db)
9125 {
9126 int i;
9127
9128 for (i = 0; i < tuning_db->alias_cnt; i++)
9129 {
9130 tuning_db_alias_t *alias = &tuning_db->alias_buf[i];
9131
9132 myfree (alias->device_name);
9133 myfree (alias->alias_name);
9134 }
9135
9136 for (i = 0; i < tuning_db->entry_cnt; i++)
9137 {
9138 tuning_db_entry_t *entry = &tuning_db->entry_buf[i];
9139
9140 myfree (entry->device_name);
9141 }
9142
9143 myfree (tuning_db->alias_buf);
9144 myfree (tuning_db->entry_buf);
9145
9146 myfree (tuning_db);
9147 }
9148
9149 tuning_db_t *tuning_db_alloc (FILE *fp)
9150 {
9151 tuning_db_t *tuning_db = (tuning_db_t *) mymalloc (sizeof (tuning_db_t));
9152
9153 int num_lines = count_lines (fp);
9154
9155 // a bit over-allocated
9156
9157 tuning_db->alias_buf = (tuning_db_alias_t *) mycalloc (num_lines + 1, sizeof (tuning_db_alias_t));
9158 tuning_db->alias_cnt = 0;
9159
9160 tuning_db->entry_buf = (tuning_db_entry_t *) mycalloc (num_lines + 1, sizeof (tuning_db_entry_t));
9161 tuning_db->entry_cnt = 0;
9162
9163 return tuning_db;
9164 }
9165
9166 tuning_db_t *tuning_db_init (const char *tuning_db_file)
9167 {
9168 FILE *fp = fopen (tuning_db_file, "rb");
9169
9170 if (fp == NULL)
9171 {
9172 log_error ("%s: %s", tuning_db_file, strerror (errno));
9173
9174 exit (-1);
9175 }
9176
9177 tuning_db_t *tuning_db = tuning_db_alloc (fp);
9178
9179 rewind (fp);
9180
9181 int line_num = 0;
9182
9183 while (!feof (fp))
9184 {
9185 char buf[BUFSIZ];
9186
9187 char *line_buf = fgets (buf, sizeof (buf) - 1, fp);
9188
9189 if (line_buf == NULL) break;
9190
9191 line_num++;
9192
9193 const int line_len = in_superchop (line_buf);
9194
9195 if (line_len == 0) continue;
9196
9197 if (line_buf[0] == '#') continue;
9198
9199 // start processing
9200
9201 char *token_ptr[7] = { NULL };
9202
9203 int token_cnt = 0;
9204
9205 char *next = strtok (line_buf, "\t ");
9206
9207 token_ptr[token_cnt] = next;
9208
9209 token_cnt++;
9210
9211 while ((next = strtok (NULL, "\t ")) != NULL)
9212 {
9213 token_ptr[token_cnt] = next;
9214
9215 token_cnt++;
9216 }
9217
9218 if (token_cnt == 2)
9219 {
9220 char *device_name = token_ptr[0];
9221 char *alias_name = token_ptr[1];
9222
9223 tuning_db_alias_t *alias = &tuning_db->alias_buf[tuning_db->alias_cnt];
9224
9225 alias->device_name = mystrdup (device_name);
9226 alias->alias_name = mystrdup (alias_name);
9227
9228 tuning_db->alias_cnt++;
9229 }
9230 else if (token_cnt == 6)
9231 {
9232 if ((token_ptr[1][0] != '0') &&
9233 (token_ptr[1][0] != '1') &&
9234 (token_ptr[1][0] != '3') &&
9235 (token_ptr[1][0] != '*'))
9236 {
9237 log_info ("WARNING: Tuning-db: Invalid attack_mode '%c' in Line '%u'", token_ptr[1][0], line_num);
9238
9239 continue;
9240 }
9241
9242 if ((token_ptr[3][0] != '1') &&
9243 (token_ptr[3][0] != '2') &&
9244 (token_ptr[3][0] != '4') &&
9245 (token_ptr[3][0] != '8') &&
9246 (token_ptr[3][0] != 'N'))
9247 {
9248 log_info ("WARNING: Tuning-db: Invalid vector_width '%c' in Line '%u'", token_ptr[3][0], line_num);
9249
9250 continue;
9251 }
9252
9253 char *device_name = token_ptr[0];
9254
9255 int attack_mode = -1;
9256 int hash_type = -1;
9257 int vector_width = -1;
9258 int kernel_accel = -1;
9259 int kernel_loops = -1;
9260
9261 if (token_ptr[1][0] != '*') attack_mode = atoi (token_ptr[1]);
9262 if (token_ptr[2][0] != '*') hash_type = atoi (token_ptr[2]);
9263 if (token_ptr[3][0] != 'N') vector_width = atoi (token_ptr[3]);
9264
9265 if (token_ptr[4][0] != 'A')
9266 {
9267 kernel_accel = atoi (token_ptr[4]);
9268
9269 if ((kernel_accel < 1) || (kernel_accel > 1024))
9270 {
9271 log_info ("WARNING: Tuning-db: Invalid kernel_accel '%d' in Line '%u'", kernel_accel, line_num);
9272
9273 continue;
9274 }
9275 }
9276 else
9277 {
9278 kernel_accel = 0;
9279 }
9280
9281 if (token_ptr[5][0] != 'A')
9282 {
9283 kernel_loops = atoi (token_ptr[5]);
9284
9285 if ((kernel_loops < 1) || (kernel_loops > 1024))
9286 {
9287 log_info ("WARNING: Tuning-db: Invalid kernel_loops '%d' in Line '%u'", kernel_loops, line_num);
9288
9289 continue;
9290 }
9291 }
9292 else
9293 {
9294 kernel_loops = 0;
9295 }
9296
9297 tuning_db_entry_t *entry = &tuning_db->entry_buf[tuning_db->entry_cnt];
9298
9299 entry->device_name = mystrdup (device_name);
9300 entry->attack_mode = attack_mode;
9301 entry->hash_type = hash_type;
9302 entry->vector_width = vector_width;
9303 entry->kernel_accel = kernel_accel;
9304 entry->kernel_loops = kernel_loops;
9305
9306 tuning_db->entry_cnt++;
9307 }
9308 else
9309 {
9310 log_info ("WARNING: Tuning-db: Invalid number of token in Line '%u'", line_num);
9311
9312 continue;
9313 }
9314 }
9315
9316 fclose (fp);
9317
9318 // todo: print loaded 'cnt' message
9319
9320 // sort the database
9321
9322 qsort (tuning_db->alias_buf, tuning_db->alias_cnt, sizeof (tuning_db_alias_t), sort_by_tuning_db_alias);
9323 qsort (tuning_db->entry_buf, tuning_db->entry_cnt, sizeof (tuning_db_entry_t), sort_by_tuning_db_entry);
9324
9325 return tuning_db;
9326 }
9327
9328 tuning_db_entry_t *tuning_db_search (tuning_db_t *tuning_db, hc_device_param_t *device_param, int attack_mode, int hash_type)
9329 {
9330 static tuning_db_entry_t s;
9331
9332 // first we need to convert all spaces in the device_name to underscore
9333
9334 char *device_name_nospace = strdup (device_param->device_name);
9335
9336 int device_name_length = strlen (device_name_nospace);
9337
9338 int i;
9339
9340 for (i = 0; i < device_name_length; i++)
9341 {
9342 if (device_name_nospace[i] == ' ') device_name_nospace[i] = '_';
9343 }
9344
9345 // find out if there's an alias configured
9346
9347 tuning_db_alias_t a;
9348
9349 a.device_name = device_name_nospace;
9350
9351 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);
9352
9353 char *alias_name = (alias == NULL) ? NULL : alias->alias_name;
9354
9355 // attack-mode 6 and 7 are attack-mode 1 basically
9356
9357 if (attack_mode == 6) attack_mode = 1;
9358 if (attack_mode == 7) attack_mode = 1;
9359
9360 // bsearch is not ideal but fast enough
9361
9362 s.device_name = device_name_nospace;
9363 s.attack_mode = attack_mode;
9364 s.hash_type = hash_type;
9365
9366 tuning_db_entry_t *entry = NULL;
9367
9368 // this will produce all 2^3 combinations required
9369
9370 for (i = 0; i < 8; i++)
9371 {
9372 s.device_name = (i & 1) ? "*" : device_name_nospace;
9373 s.attack_mode = (i & 2) ? -1 : attack_mode;
9374 s.hash_type = (i & 4) ? -1 : hash_type;
9375
9376 entry = bsearch (&s, tuning_db->entry_buf, tuning_db->entry_cnt, sizeof (tuning_db_entry_t), sort_by_tuning_db_entry);
9377
9378 if (entry != NULL) break;
9379
9380 // in non-wildcard mode do some additional checks:
9381
9382 if ((i & 1) == 0)
9383 {
9384 // in case we have an alias-name
9385
9386 if (alias_name != NULL)
9387 {
9388 s.device_name = alias_name;
9389
9390 entry = bsearch (&s, tuning_db->entry_buf, tuning_db->entry_cnt, sizeof (tuning_db_entry_t), sort_by_tuning_db_entry);
9391
9392 if (entry != NULL) break;
9393 }
9394
9395 // or by device type
9396
9397 if (device_param->device_type & CL_DEVICE_TYPE_CPU)
9398 {
9399 s.device_name = "DEVICE_TYPE_CPU";
9400 }
9401 else if (device_param->device_type & CL_DEVICE_TYPE_GPU)
9402 {
9403 s.device_name = "DEVICE_TYPE_GPU";
9404 }
9405 else if (device_param->device_type & CL_DEVICE_TYPE_ACCELERATOR)
9406 {
9407 s.device_name = "DEVICE_TYPE_ACCELERATOR";
9408 }
9409
9410 entry = bsearch (&s, tuning_db->entry_buf, tuning_db->entry_cnt, sizeof (tuning_db_entry_t), sort_by_tuning_db_entry);
9411
9412 if (entry != NULL) break;
9413 }
9414 }
9415
9416 // free converted device_name
9417
9418 myfree (device_name_nospace);
9419
9420 return entry;
9421 }
9422
9423 /**
9424 * parser
9425 */
9426
9427 uint parse_and_store_salt (char *out, char *in, uint salt_len)
9428 {
9429 u8 tmp[256] = { 0 };
9430
9431 if (salt_len > sizeof (tmp))
9432 {
9433 return UINT_MAX;
9434 }
9435
9436 memcpy (tmp, in, salt_len);
9437
9438 if (data.opts_type & OPTS_TYPE_ST_HEX)
9439 {
9440 if ((salt_len % 2) == 0)
9441 {
9442 u32 new_salt_len = salt_len / 2;
9443
9444 for (uint i = 0, j = 0; i < new_salt_len; i += 1, j += 2)
9445 {
9446 u8 p0 = tmp[j + 0];
9447 u8 p1 = tmp[j + 1];
9448
9449 tmp[i] = hex_convert (p1) << 0;
9450 tmp[i] |= hex_convert (p0) << 4;
9451 }
9452
9453 salt_len = new_salt_len;
9454 }
9455 else
9456 {
9457 return UINT_MAX;
9458 }
9459 }
9460 else if (data.opts_type & OPTS_TYPE_ST_BASE64)
9461 {
9462 salt_len = base64_decode (base64_to_int, (const u8 *) in, salt_len, (u8 *) tmp);
9463 }
9464
9465 memset (tmp + salt_len, 0, sizeof (tmp) - salt_len);
9466
9467 if (data.opts_type & OPTS_TYPE_ST_UNICODE)
9468 {
9469 if (salt_len < 20)
9470 {
9471 u32 *tmp_uint = (u32 *) tmp;
9472
9473 tmp_uint[9] = ((tmp_uint[4] >> 8) & 0x00FF0000) | ((tmp_uint[4] >> 16) & 0x000000FF);
9474 tmp_uint[8] = ((tmp_uint[4] << 8) & 0x00FF0000) | ((tmp_uint[4] >> 0) & 0x000000FF);
9475 tmp_uint[7] = ((tmp_uint[3] >> 8) & 0x00FF0000) | ((tmp_uint[3] >> 16) & 0x000000FF);
9476 tmp_uint[6] = ((tmp_uint[3] << 8) & 0x00FF0000) | ((tmp_uint[3] >> 0) & 0x000000FF);
9477 tmp_uint[5] = ((tmp_uint[2] >> 8) & 0x00FF0000) | ((tmp_uint[2] >> 16) & 0x000000FF);
9478 tmp_uint[4] = ((tmp_uint[2] << 8) & 0x00FF0000) | ((tmp_uint[2] >> 0) & 0x000000FF);
9479 tmp_uint[3] = ((tmp_uint[1] >> 8) & 0x00FF0000) | ((tmp_uint[1] >> 16) & 0x000000FF);
9480 tmp_uint[2] = ((tmp_uint[1] << 8) & 0x00FF0000) | ((tmp_uint[1] >> 0) & 0x000000FF);
9481 tmp_uint[1] = ((tmp_uint[0] >> 8) & 0x00FF0000) | ((tmp_uint[0] >> 16) & 0x000000FF);
9482 tmp_uint[0] = ((tmp_uint[0] << 8) & 0x00FF0000) | ((tmp_uint[0] >> 0) & 0x000000FF);
9483
9484 salt_len = salt_len * 2;
9485 }
9486 else
9487 {
9488 return UINT_MAX;
9489 }
9490 }
9491
9492 if (data.opts_type & OPTS_TYPE_ST_LOWER)
9493 {
9494 lowercase (tmp, salt_len);
9495 }
9496
9497 if (data.opts_type & OPTS_TYPE_ST_UPPER)
9498 {
9499 uppercase (tmp, salt_len);
9500 }
9501
9502 u32 len = salt_len;
9503
9504 if (data.opts_type & OPTS_TYPE_ST_ADD80)
9505 {
9506 tmp[len++] = 0x80;
9507 }
9508
9509 if (data.opts_type & OPTS_TYPE_ST_ADD01)
9510 {
9511 tmp[len++] = 0x01;
9512 }
9513
9514 if (data.opts_type & OPTS_TYPE_ST_GENERATE_LE)
9515 {
9516 u32 *tmp_uint = (uint *) tmp;
9517
9518 u32 max = len / 4;
9519
9520 if (len % 4) max++;
9521
9522 for (u32 i = 0; i < max; i++)
9523 {
9524 tmp_uint[i] = byte_swap_32 (tmp_uint[i]);
9525 }
9526
9527 // Important: we may need to increase the length of memcpy since
9528 // we don't want to "loose" some swapped bytes (could happen if
9529 // they do not perfectly fit in the 4-byte blocks)
9530 // Memcpy does always copy the bytes in the BE order, but since
9531 // we swapped them, some important bytes could be in positions
9532 // we normally skip with the original len
9533
9534 if (len % 4) len += 4 - (len % 4);
9535 }
9536
9537 memcpy (out, tmp, len);
9538
9539 return (salt_len);
9540 }
9541
9542 int bcrypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
9543 {
9544 if ((input_len < DISPLAY_LEN_MIN_3200) || (input_len > DISPLAY_LEN_MAX_3200)) return (PARSER_GLOBAL_LENGTH);
9545
9546 if ((memcmp (SIGNATURE_BCRYPT1, input_buf, 4)) && (memcmp (SIGNATURE_BCRYPT2, input_buf, 4)) && (memcmp (SIGNATURE_BCRYPT3, input_buf, 4))) return (PARSER_SIGNATURE_UNMATCHED);
9547
9548 u32 *digest = (u32 *) hash_buf->digest;
9549
9550 salt_t *salt = hash_buf->salt;
9551
9552 memcpy ((char *) salt->salt_sign, input_buf, 6);
9553
9554 char *iter_pos = input_buf + 4;
9555
9556 salt->salt_iter = 1 << atoi (iter_pos);
9557
9558 char *salt_pos = strchr (iter_pos, '$');
9559
9560 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
9561
9562 salt_pos++;
9563
9564 uint salt_len = 16;
9565
9566 salt->salt_len = salt_len;
9567
9568 u8 tmp_buf[100] = { 0 };
9569
9570 base64_decode (bf64_to_int, (const u8 *) salt_pos, 22, tmp_buf);
9571
9572 char *salt_buf_ptr = (char *) salt->salt_buf;
9573
9574 memcpy (salt_buf_ptr, tmp_buf, 16);
9575
9576 salt->salt_buf[0] = byte_swap_32 (salt->salt_buf[0]);
9577 salt->salt_buf[1] = byte_swap_32 (salt->salt_buf[1]);
9578 salt->salt_buf[2] = byte_swap_32 (salt->salt_buf[2]);
9579 salt->salt_buf[3] = byte_swap_32 (salt->salt_buf[3]);
9580
9581 char *hash_pos = salt_pos + 22;
9582
9583 memset (tmp_buf, 0, sizeof (tmp_buf));
9584
9585 base64_decode (bf64_to_int, (const u8 *) hash_pos, 31, tmp_buf);
9586
9587 memcpy (digest, tmp_buf, 24);
9588
9589 digest[0] = byte_swap_32 (digest[0]);
9590 digest[1] = byte_swap_32 (digest[1]);
9591 digest[2] = byte_swap_32 (digest[2]);
9592 digest[3] = byte_swap_32 (digest[3]);
9593 digest[4] = byte_swap_32 (digest[4]);
9594 digest[5] = byte_swap_32 (digest[5]);
9595
9596 digest[5] &= ~0xff; // its just 23 not 24 !
9597
9598 return (PARSER_OK);
9599 }
9600
9601 int cisco4_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
9602 {
9603 if ((input_len < DISPLAY_LEN_MIN_5700) || (input_len > DISPLAY_LEN_MAX_5700)) return (PARSER_GLOBAL_LENGTH);
9604
9605 u32 *digest = (u32 *) hash_buf->digest;
9606
9607 u8 tmp_buf[100] = { 0 };
9608
9609 base64_decode (itoa64_to_int, (const u8 *) input_buf, 43, tmp_buf);
9610
9611 memcpy (digest, tmp_buf, 32);
9612
9613 digest[0] = byte_swap_32 (digest[0]);
9614 digest[1] = byte_swap_32 (digest[1]);
9615 digest[2] = byte_swap_32 (digest[2]);
9616 digest[3] = byte_swap_32 (digest[3]);
9617 digest[4] = byte_swap_32 (digest[4]);
9618 digest[5] = byte_swap_32 (digest[5]);
9619 digest[6] = byte_swap_32 (digest[6]);
9620 digest[7] = byte_swap_32 (digest[7]);
9621
9622 digest[0] -= SHA256M_A;
9623 digest[1] -= SHA256M_B;
9624 digest[2] -= SHA256M_C;
9625 digest[3] -= SHA256M_D;
9626 digest[4] -= SHA256M_E;
9627 digest[5] -= SHA256M_F;
9628 digest[6] -= SHA256M_G;
9629 digest[7] -= SHA256M_H;
9630
9631 return (PARSER_OK);
9632 }
9633
9634 int lm_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
9635 {
9636 if ((input_len < DISPLAY_LEN_MIN_3000) || (input_len > DISPLAY_LEN_MAX_3000)) return (PARSER_GLOBAL_LENGTH);
9637
9638 u32 *digest = (u32 *) hash_buf->digest;
9639
9640 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
9641 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
9642
9643 digest[0] = byte_swap_32 (digest[0]);
9644 digest[1] = byte_swap_32 (digest[1]);
9645
9646 uint tt;
9647
9648 IP (digest[0], digest[1], tt);
9649
9650 digest[0] = digest[0];
9651 digest[1] = digest[1];
9652 digest[2] = 0;
9653 digest[3] = 0;
9654
9655 return (PARSER_OK);
9656 }
9657
9658 int osx1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
9659 {
9660 if ((input_len < DISPLAY_LEN_MIN_122) || (input_len > DISPLAY_LEN_MAX_122)) return (PARSER_GLOBAL_LENGTH);
9661
9662 u32 *digest = (u32 *) hash_buf->digest;
9663
9664 salt_t *salt = hash_buf->salt;
9665
9666 char *hash_pos = input_buf + 8;
9667
9668 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
9669 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
9670 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
9671 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
9672 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
9673
9674 digest[0] -= SHA1M_A;
9675 digest[1] -= SHA1M_B;
9676 digest[2] -= SHA1M_C;
9677 digest[3] -= SHA1M_D;
9678 digest[4] -= SHA1M_E;
9679
9680 uint salt_len = 8;
9681
9682 char *salt_buf_ptr = (char *) salt->salt_buf;
9683
9684 salt_len = parse_and_store_salt (salt_buf_ptr, input_buf, salt_len);
9685
9686 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
9687
9688 salt->salt_len = salt_len;
9689
9690 return (PARSER_OK);
9691 }
9692
9693 int osx512_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
9694 {
9695 if ((input_len < DISPLAY_LEN_MIN_1722) || (input_len > DISPLAY_LEN_MAX_1722)) return (PARSER_GLOBAL_LENGTH);
9696
9697 u64 *digest = (u64 *) hash_buf->digest;
9698
9699 salt_t *salt = hash_buf->salt;
9700
9701 char *hash_pos = input_buf + 8;
9702
9703 digest[0] = hex_to_u64 ((const u8 *) &hash_pos[ 0]);
9704 digest[1] = hex_to_u64 ((const u8 *) &hash_pos[ 16]);
9705 digest[2] = hex_to_u64 ((const u8 *) &hash_pos[ 32]);
9706 digest[3] = hex_to_u64 ((const u8 *) &hash_pos[ 48]);
9707 digest[4] = hex_to_u64 ((const u8 *) &hash_pos[ 64]);
9708 digest[5] = hex_to_u64 ((const u8 *) &hash_pos[ 80]);
9709 digest[6] = hex_to_u64 ((const u8 *) &hash_pos[ 96]);
9710 digest[7] = hex_to_u64 ((const u8 *) &hash_pos[112]);
9711
9712 digest[0] -= SHA512M_A;
9713 digest[1] -= SHA512M_B;
9714 digest[2] -= SHA512M_C;
9715 digest[3] -= SHA512M_D;
9716 digest[4] -= SHA512M_E;
9717 digest[5] -= SHA512M_F;
9718 digest[6] -= SHA512M_G;
9719 digest[7] -= SHA512M_H;
9720
9721 uint salt_len = 8;
9722
9723 char *salt_buf_ptr = (char *) salt->salt_buf;
9724
9725 salt_len = parse_and_store_salt (salt_buf_ptr, input_buf, salt_len);
9726
9727 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
9728
9729 salt->salt_len = salt_len;
9730
9731 return (PARSER_OK);
9732 }
9733
9734 int osc_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
9735 {
9736 if (data.opts_type & OPTS_TYPE_ST_HEX)
9737 {
9738 if ((input_len < DISPLAY_LEN_MIN_21H) || (input_len > DISPLAY_LEN_MAX_21H)) return (PARSER_GLOBAL_LENGTH);
9739 }
9740 else
9741 {
9742 if ((input_len < DISPLAY_LEN_MIN_21) || (input_len > DISPLAY_LEN_MAX_21)) return (PARSER_GLOBAL_LENGTH);
9743 }
9744
9745 u32 *digest = (u32 *) hash_buf->digest;
9746
9747 salt_t *salt = hash_buf->salt;
9748
9749 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
9750 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
9751 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
9752 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
9753
9754 digest[0] = byte_swap_32 (digest[0]);
9755 digest[1] = byte_swap_32 (digest[1]);
9756 digest[2] = byte_swap_32 (digest[2]);
9757 digest[3] = byte_swap_32 (digest[3]);
9758
9759 digest[0] -= MD5M_A;
9760 digest[1] -= MD5M_B;
9761 digest[2] -= MD5M_C;
9762 digest[3] -= MD5M_D;
9763
9764 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
9765
9766 uint salt_len = input_len - 32 - 1;
9767
9768 char *salt_buf = input_buf + 32 + 1;
9769
9770 char *salt_buf_ptr = (char *) salt->salt_buf;
9771
9772 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
9773
9774 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
9775
9776 salt->salt_len = salt_len;
9777
9778 return (PARSER_OK);
9779 }
9780
9781 int netscreen_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
9782 {
9783 if (data.opts_type & OPTS_TYPE_ST_HEX)
9784 {
9785 if ((input_len < DISPLAY_LEN_MIN_22H) || (input_len > DISPLAY_LEN_MAX_22H)) return (PARSER_GLOBAL_LENGTH);
9786 }
9787 else
9788 {
9789 if ((input_len < DISPLAY_LEN_MIN_22) || (input_len > DISPLAY_LEN_MAX_22)) return (PARSER_GLOBAL_LENGTH);
9790 }
9791
9792 // unscramble
9793
9794 char clean_input_buf[32] = { 0 };
9795
9796 char sig[6] = { 'n', 'r', 'c', 's', 't', 'n' };
9797 int pos[6] = { 0, 6, 12, 17, 23, 29 };
9798
9799 for (int i = 0, j = 0, k = 0; i < 30; i++)
9800 {
9801 if (i == pos[j])
9802 {
9803 if (sig[j] != input_buf[i]) return (PARSER_SIGNATURE_UNMATCHED);
9804
9805 j++;
9806 }
9807 else
9808 {
9809 clean_input_buf[k] = input_buf[i];
9810
9811 k++;
9812 }
9813 }
9814
9815 // base64 decode
9816
9817 u32 *digest = (u32 *) hash_buf->digest;
9818
9819 salt_t *salt = hash_buf->salt;
9820
9821 u32 a, b, c, d, e, f;
9822
9823 a = base64_to_int (clean_input_buf[ 0] & 0x7f);
9824 b = base64_to_int (clean_input_buf[ 1] & 0x7f);
9825 c = base64_to_int (clean_input_buf[ 2] & 0x7f);
9826 d = base64_to_int (clean_input_buf[ 3] & 0x7f);
9827 e = base64_to_int (clean_input_buf[ 4] & 0x7f);
9828 f = base64_to_int (clean_input_buf[ 5] & 0x7f);
9829
9830 digest[0] = (((a << 12) | (b << 6) | (c)) << 16)
9831 | (((d << 12) | (e << 6) | (f)) << 0);
9832
9833 a = base64_to_int (clean_input_buf[ 6] & 0x7f);
9834 b = base64_to_int (clean_input_buf[ 7] & 0x7f);
9835 c = base64_to_int (clean_input_buf[ 8] & 0x7f);
9836 d = base64_to_int (clean_input_buf[ 9] & 0x7f);
9837 e = base64_to_int (clean_input_buf[10] & 0x7f);
9838 f = base64_to_int (clean_input_buf[11] & 0x7f);
9839
9840 digest[1] = (((a << 12) | (b << 6) | (c)) << 16)
9841 | (((d << 12) | (e << 6) | (f)) << 0);
9842
9843 a = base64_to_int (clean_input_buf[12] & 0x7f);
9844 b = base64_to_int (clean_input_buf[13] & 0x7f);
9845 c = base64_to_int (clean_input_buf[14] & 0x7f);
9846 d = base64_to_int (clean_input_buf[15] & 0x7f);
9847 e = base64_to_int (clean_input_buf[16] & 0x7f);
9848 f = base64_to_int (clean_input_buf[17] & 0x7f);
9849
9850 digest[2] = (((a << 12) | (b << 6) | (c)) << 16)
9851 | (((d << 12) | (e << 6) | (f)) << 0);
9852
9853 a = base64_to_int (clean_input_buf[18] & 0x7f);
9854 b = base64_to_int (clean_input_buf[19] & 0x7f);
9855 c = base64_to_int (clean_input_buf[20] & 0x7f);
9856 d = base64_to_int (clean_input_buf[21] & 0x7f);
9857 e = base64_to_int (clean_input_buf[22] & 0x7f);
9858 f = base64_to_int (clean_input_buf[23] & 0x7f);
9859
9860 digest[3] = (((a << 12) | (b << 6) | (c)) << 16)
9861 | (((d << 12) | (e << 6) | (f)) << 0);
9862
9863 digest[0] = byte_swap_32 (digest[0]);
9864 digest[1] = byte_swap_32 (digest[1]);
9865 digest[2] = byte_swap_32 (digest[2]);
9866 digest[3] = byte_swap_32 (digest[3]);
9867
9868 digest[0] -= MD5M_A;
9869 digest[1] -= MD5M_B;
9870 digest[2] -= MD5M_C;
9871 digest[3] -= MD5M_D;
9872
9873 if (input_buf[30] != ':') return (PARSER_SEPARATOR_UNMATCHED);
9874
9875 uint salt_len = input_len - 30 - 1;
9876
9877 char *salt_buf = input_buf + 30 + 1;
9878
9879 char *salt_buf_ptr = (char *) salt->salt_buf;
9880
9881 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
9882
9883 // max. salt length: 55 (max for MD5) - 22 (":Administration Tools:") - 1 (0x80) = 32
9884 // 32 - 4 bytes (to fit w0lr for all attack modes) = 28
9885
9886 if (salt_len > 28) return (PARSER_SALT_LENGTH);
9887
9888 salt->salt_len = salt_len;
9889
9890 memcpy (salt_buf_ptr + salt_len, ":Administration Tools:", 22);
9891
9892 salt->salt_len += 22;
9893
9894 return (PARSER_OK);
9895 }
9896
9897 int smf_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
9898 {
9899 if (data.opts_type & OPTS_TYPE_ST_HEX)
9900 {
9901 if ((input_len < DISPLAY_LEN_MIN_121H) || (input_len > DISPLAY_LEN_MAX_121H)) return (PARSER_GLOBAL_LENGTH);
9902 }
9903 else
9904 {
9905 if ((input_len < DISPLAY_LEN_MIN_121) || (input_len > DISPLAY_LEN_MAX_121)) return (PARSER_GLOBAL_LENGTH);
9906 }
9907
9908 u32 *digest = (u32 *) hash_buf->digest;
9909
9910 salt_t *salt = hash_buf->salt;
9911
9912 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
9913 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
9914 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
9915 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
9916 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
9917
9918 digest[0] -= SHA1M_A;
9919 digest[1] -= SHA1M_B;
9920 digest[2] -= SHA1M_C;
9921 digest[3] -= SHA1M_D;
9922 digest[4] -= SHA1M_E;
9923
9924 if (input_buf[40] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
9925
9926 uint salt_len = input_len - 40 - 1;
9927
9928 char *salt_buf = input_buf + 40 + 1;
9929
9930 char *salt_buf_ptr = (char *) salt->salt_buf;
9931
9932 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
9933
9934 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
9935
9936 salt->salt_len = salt_len;
9937
9938 return (PARSER_OK);
9939 }
9940
9941 int dcc2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
9942 {
9943 if (data.opts_type & OPTS_TYPE_ST_HEX)
9944 {
9945 if ((input_len < DISPLAY_LEN_MIN_2100H) || (input_len > DISPLAY_LEN_MAX_2100H)) return (PARSER_GLOBAL_LENGTH);
9946 }
9947 else
9948 {
9949 if ((input_len < DISPLAY_LEN_MIN_2100) || (input_len > DISPLAY_LEN_MAX_2100)) return (PARSER_GLOBAL_LENGTH);
9950 }
9951
9952 if (memcmp (SIGNATURE_DCC2, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
9953
9954 char *iter_pos = input_buf + 6;
9955
9956 salt_t *salt = hash_buf->salt;
9957
9958 uint iter = atoi (iter_pos);
9959
9960 if (iter < 1)
9961 {
9962 iter = ROUNDS_DCC2;
9963 }
9964
9965 salt->salt_iter = iter - 1;
9966
9967 char *salt_pos = strchr (iter_pos, '#');
9968
9969 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
9970
9971 salt_pos++;
9972
9973 char *digest_pos = strchr (salt_pos, '#');
9974
9975 if (digest_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
9976
9977 digest_pos++;
9978
9979 uint salt_len = digest_pos - salt_pos - 1;
9980
9981 u32 *digest = (u32 *) hash_buf->digest;
9982
9983 digest[0] = hex_to_u32 ((const u8 *) &digest_pos[ 0]);
9984 digest[1] = hex_to_u32 ((const u8 *) &digest_pos[ 8]);
9985 digest[2] = hex_to_u32 ((const u8 *) &digest_pos[16]);
9986 digest[3] = hex_to_u32 ((const u8 *) &digest_pos[24]);
9987
9988 char *salt_buf_ptr = (char *) salt->salt_buf;
9989
9990 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
9991
9992 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
9993
9994 salt->salt_len = salt_len;
9995
9996 return (PARSER_OK);
9997 }
9998
9999 int wpa_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10000 {
10001 u32 *digest = (u32 *) hash_buf->digest;
10002
10003 salt_t *salt = hash_buf->salt;
10004
10005 wpa_t *wpa = (wpa_t *) hash_buf->esalt;
10006
10007 hccap_t in;
10008
10009 memcpy (&in, input_buf, input_len);
10010
10011 if (in.eapol_size < 1 || in.eapol_size > 255) return (PARSER_HCCAP_EAPOL_SIZE);
10012
10013 memcpy (digest, in.keymic, 16);
10014
10015 /*
10016 http://www.one-net.eu/jsw/j_sec/m_ptype.html
10017 The phrase "Pairwise key expansion"
10018 Access Point Address (referred to as Authenticator Address AA)
10019 Supplicant Address (referred to as Supplicant Address SA)
10020 Access Point Nonce (referred to as Authenticator Anonce)
10021 Wireless Device Nonce (referred to as Supplicant Nonce Snonce)
10022 */
10023
10024 uint salt_len = strlen (in.essid);
10025
10026 if (salt_len > 36)
10027 {
10028 log_info ("WARNING: the length of the ESSID is too long. The hccap file may be invalid or corrupted");
10029
10030 return (PARSER_SALT_LENGTH);
10031 }
10032
10033 memcpy (salt->salt_buf, in.essid, salt_len);
10034
10035 salt->salt_len = salt_len;
10036
10037 salt->salt_iter = ROUNDS_WPA2 - 1;
10038
10039 unsigned char *pke_ptr = (unsigned char *) wpa->pke;
10040
10041 memcpy (pke_ptr, "Pairwise key expansion", 23);
10042
10043 if (memcmp (in.mac1, in.mac2, 6) < 0)
10044 {
10045 memcpy (pke_ptr + 23, in.mac1, 6);
10046 memcpy (pke_ptr + 29, in.mac2, 6);
10047 }
10048 else
10049 {
10050 memcpy (pke_ptr + 23, in.mac2, 6);
10051 memcpy (pke_ptr + 29, in.mac1, 6);
10052 }
10053
10054 if (memcmp (in.nonce1, in.nonce2, 32) < 0)
10055 {
10056 memcpy (pke_ptr + 35, in.nonce1, 32);
10057 memcpy (pke_ptr + 67, in.nonce2, 32);
10058 }
10059 else
10060 {
10061 memcpy (pke_ptr + 35, in.nonce2, 32);
10062 memcpy (pke_ptr + 67, in.nonce1, 32);
10063 }
10064
10065 for (int i = 0; i < 25; i++)
10066 {
10067 wpa->pke[i] = byte_swap_32 (wpa->pke[i]);
10068 }
10069
10070 wpa->keyver = in.keyver;
10071
10072 if (wpa->keyver > 255)
10073 {
10074 log_info ("ATTENTION!");
10075 log_info (" The WPA/WPA2 key version in your .hccap file is invalid!");
10076 log_info (" This could be due to a recent aircrack-ng bug.");
10077 log_info (" The key version was automatically reset to a reasonable value.");
10078 log_info ("");
10079
10080 wpa->keyver &= 0xff;
10081 }
10082
10083 wpa->eapol_size = in.eapol_size;
10084
10085 unsigned char *eapol_ptr = (unsigned char *) wpa->eapol;
10086
10087 memcpy (eapol_ptr, in.eapol, wpa->eapol_size);
10088
10089 memset (eapol_ptr + wpa->eapol_size, 0, 256 - wpa->eapol_size);
10090
10091 eapol_ptr[wpa->eapol_size] = (unsigned char) 0x80;
10092
10093 if (wpa->keyver == 1)
10094 {
10095 // nothing to do
10096 }
10097 else
10098 {
10099 digest[0] = byte_swap_32 (digest[0]);
10100 digest[1] = byte_swap_32 (digest[1]);
10101 digest[2] = byte_swap_32 (digest[2]);
10102 digest[3] = byte_swap_32 (digest[3]);
10103
10104 for (int i = 0; i < 64; i++)
10105 {
10106 wpa->eapol[i] = byte_swap_32 (wpa->eapol[i]);
10107 }
10108 }
10109
10110 uint32_t *p0 = (uint32_t *) in.essid;
10111 uint32_t c0 = 0;
10112 uint32_t c1 = 0;
10113
10114 for (uint i = 0; i < sizeof (in.essid) / sizeof (uint32_t); i++) c0 ^= *p0++;
10115 for (uint i = 0; i < sizeof (wpa->pke) / sizeof (wpa->pke[0]); i++) c1 ^= wpa->pke[i];
10116
10117 salt->salt_buf[10] = c0;
10118 salt->salt_buf[11] = c1;
10119
10120 return (PARSER_OK);
10121 }
10122
10123 int psafe2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10124 {
10125 u32 *digest = (u32 *) hash_buf->digest;
10126
10127 salt_t *salt = hash_buf->salt;
10128
10129 if (input_len == 0)
10130 {
10131 log_error ("Password Safe v2 container not specified");
10132
10133 exit (-1);
10134 }
10135
10136 FILE *fp = fopen (input_buf, "rb");
10137
10138 if (fp == NULL)
10139 {
10140 log_error ("%s: %s", input_buf, strerror (errno));
10141
10142 exit (-1);
10143 }
10144
10145 psafe2_hdr buf;
10146
10147 memset (&buf, 0, sizeof (psafe2_hdr));
10148
10149 int n = fread (&buf, sizeof (psafe2_hdr), 1, fp);
10150
10151 fclose (fp);
10152
10153 if (n != 1) return (PARSER_PSAFE2_FILE_SIZE);
10154
10155 salt->salt_buf[0] = buf.random[0];
10156 salt->salt_buf[1] = buf.random[1];
10157
10158 salt->salt_len = 8;
10159 salt->salt_iter = 1000;
10160
10161 digest[0] = byte_swap_32 (buf.hash[0]);
10162 digest[1] = byte_swap_32 (buf.hash[1]);
10163 digest[2] = byte_swap_32 (buf.hash[2]);
10164 digest[3] = byte_swap_32 (buf.hash[3]);
10165 digest[4] = byte_swap_32 (buf.hash[4]);
10166
10167 return (PARSER_OK);
10168 }
10169
10170 int psafe3_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10171 {
10172 u32 *digest = (u32 *) hash_buf->digest;
10173
10174 salt_t *salt = hash_buf->salt;
10175
10176 if (input_len == 0)
10177 {
10178 log_error (".psafe3 not specified");
10179
10180 exit (-1);
10181 }
10182
10183 FILE *fp = fopen (input_buf, "rb");
10184
10185 if (fp == NULL)
10186 {
10187 log_error ("%s: %s", input_buf, strerror (errno));
10188
10189 exit (-1);
10190 }
10191
10192 psafe3_t in;
10193
10194 int n = fread (&in, sizeof (psafe3_t), 1, fp);
10195
10196 fclose (fp);
10197
10198 data.hashfile = input_buf; // we will need this in case it gets cracked
10199
10200 if (memcmp (SIGNATURE_PSAFE3, in.signature, 4)) return (PARSER_SIGNATURE_UNMATCHED);
10201
10202 if (n != 1) return (PARSER_PSAFE3_FILE_SIZE);
10203
10204 salt->salt_iter = in.iterations + 1;
10205
10206 salt->salt_buf[0] = in.salt_buf[0];
10207 salt->salt_buf[1] = in.salt_buf[1];
10208 salt->salt_buf[2] = in.salt_buf[2];
10209 salt->salt_buf[3] = in.salt_buf[3];
10210 salt->salt_buf[4] = in.salt_buf[4];
10211 salt->salt_buf[5] = in.salt_buf[5];
10212 salt->salt_buf[6] = in.salt_buf[6];
10213 salt->salt_buf[7] = in.salt_buf[7];
10214
10215 salt->salt_len = 32;
10216
10217 digest[0] = in.hash_buf[0];
10218 digest[1] = in.hash_buf[1];
10219 digest[2] = in.hash_buf[2];
10220 digest[3] = in.hash_buf[3];
10221 digest[4] = in.hash_buf[4];
10222 digest[5] = in.hash_buf[5];
10223 digest[6] = in.hash_buf[6];
10224 digest[7] = in.hash_buf[7];
10225
10226 digest[0] = byte_swap_32 (digest[0]);
10227 digest[1] = byte_swap_32 (digest[1]);
10228 digest[2] = byte_swap_32 (digest[2]);
10229 digest[3] = byte_swap_32 (digest[3]);
10230 digest[4] = byte_swap_32 (digest[4]);
10231 digest[5] = byte_swap_32 (digest[5]);
10232 digest[6] = byte_swap_32 (digest[6]);
10233 digest[7] = byte_swap_32 (digest[7]);
10234
10235 return (PARSER_OK);
10236 }
10237
10238 int phpass_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10239 {
10240 if ((input_len < DISPLAY_LEN_MIN_400) || (input_len > DISPLAY_LEN_MAX_400)) return (PARSER_GLOBAL_LENGTH);
10241
10242 if ((memcmp (SIGNATURE_PHPASS1, input_buf, 3)) && (memcmp (SIGNATURE_PHPASS2, input_buf, 3))) return (PARSER_SIGNATURE_UNMATCHED);
10243
10244 u32 *digest = (u32 *) hash_buf->digest;
10245
10246 salt_t *salt = hash_buf->salt;
10247
10248 char *iter_pos = input_buf + 3;
10249
10250 uint salt_iter = 1 << itoa64_to_int (iter_pos[0]);
10251
10252 if (salt_iter > 0x80000000) return (PARSER_SALT_ITERATION);
10253
10254 memcpy ((char *) salt->salt_sign, input_buf, 4);
10255
10256 salt->salt_iter = salt_iter;
10257
10258 char *salt_pos = iter_pos + 1;
10259
10260 uint salt_len = 8;
10261
10262 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
10263
10264 salt->salt_len = salt_len;
10265
10266 char *hash_pos = salt_pos + salt_len;
10267
10268 phpass_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
10269
10270 return (PARSER_OK);
10271 }
10272
10273 int md5crypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10274 {
10275 if (input_len < DISPLAY_LEN_MIN_500) return (PARSER_GLOBAL_LENGTH);
10276
10277 if (memcmp (SIGNATURE_MD5CRYPT, input_buf, 3)) return (PARSER_SIGNATURE_UNMATCHED);
10278
10279 u32 *digest = (u32 *) hash_buf->digest;
10280
10281 salt_t *salt = hash_buf->salt;
10282
10283 char *salt_pos = input_buf + 3;
10284
10285 uint iterations_len = 0;
10286
10287 if (memcmp (salt_pos, "rounds=", 7) == 0)
10288 {
10289 salt_pos += 7;
10290
10291 for (iterations_len = 0; salt_pos[0] >= '0' && salt_pos[0] <= '9' && iterations_len < 7; iterations_len++, salt_pos += 1) continue;
10292
10293 if (iterations_len == 0 ) return (PARSER_SALT_ITERATION);
10294 if (salt_pos[0] != '$') return (PARSER_SIGNATURE_UNMATCHED);
10295
10296 salt_pos[0] = 0x0;
10297
10298 salt->salt_iter = atoi (salt_pos - iterations_len);
10299
10300 salt_pos += 1;
10301
10302 iterations_len += 8;
10303 }
10304 else
10305 {
10306 salt->salt_iter = ROUNDS_MD5CRYPT;
10307 }
10308
10309 if (input_len > (DISPLAY_LEN_MAX_500 + iterations_len)) return (PARSER_GLOBAL_LENGTH);
10310
10311 char *hash_pos = strchr (salt_pos, '$');
10312
10313 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
10314
10315 uint salt_len = hash_pos - salt_pos;
10316
10317 if (salt_len > 8) return (PARSER_SALT_LENGTH);
10318
10319 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
10320
10321 salt->salt_len = salt_len;
10322
10323 hash_pos++;
10324
10325 uint hash_len = input_len - 3 - iterations_len - salt_len - 1;
10326
10327 if (hash_len != 22) return (PARSER_HASH_LENGTH);
10328
10329 md5crypt_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
10330
10331 return (PARSER_OK);
10332 }
10333
10334 int md5apr1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10335 {
10336 if (memcmp (SIGNATURE_MD5APR1, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
10337
10338 u32 *digest = (u32 *) hash_buf->digest;
10339
10340 salt_t *salt = hash_buf->salt;
10341
10342 char *salt_pos = input_buf + 6;
10343
10344 uint iterations_len = 0;
10345
10346 if (memcmp (salt_pos, "rounds=", 7) == 0)
10347 {
10348 salt_pos += 7;
10349
10350 for (iterations_len = 0; salt_pos[0] >= '0' && salt_pos[0] <= '9' && iterations_len < 7; iterations_len++, salt_pos += 1) continue;
10351
10352 if (iterations_len == 0 ) return (PARSER_SALT_ITERATION);
10353 if (salt_pos[0] != '$') return (PARSER_SIGNATURE_UNMATCHED);
10354
10355 salt_pos[0] = 0x0;
10356
10357 salt->salt_iter = atoi (salt_pos - iterations_len);
10358
10359 salt_pos += 1;
10360
10361 iterations_len += 8;
10362 }
10363 else
10364 {
10365 salt->salt_iter = ROUNDS_MD5CRYPT;
10366 }
10367
10368 if ((input_len < DISPLAY_LEN_MIN_1600) || (input_len > DISPLAY_LEN_MAX_1600 + iterations_len)) return (PARSER_GLOBAL_LENGTH);
10369
10370 char *hash_pos = strchr (salt_pos, '$');
10371
10372 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
10373
10374 uint salt_len = hash_pos - salt_pos;
10375
10376 if (salt_len > 8) return (PARSER_SALT_LENGTH);
10377
10378 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
10379
10380 salt->salt_len = salt_len;
10381
10382 hash_pos++;
10383
10384 md5crypt_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
10385
10386 return (PARSER_OK);
10387 }
10388
10389 int episerver_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10390 {
10391 if ((input_len < DISPLAY_LEN_MIN_141) || (input_len > DISPLAY_LEN_MAX_141)) return (PARSER_GLOBAL_LENGTH);
10392
10393 if (memcmp (SIGNATURE_EPISERVER, input_buf, 14)) return (PARSER_SIGNATURE_UNMATCHED);
10394
10395 u32 *digest = (u32 *) hash_buf->digest;
10396
10397 salt_t *salt = hash_buf->salt;
10398
10399 char *salt_pos = input_buf + 14;
10400
10401 char *hash_pos = strchr (salt_pos, '*');
10402
10403 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
10404
10405 hash_pos++;
10406
10407 uint salt_len = hash_pos - salt_pos - 1;
10408
10409 char *salt_buf_ptr = (char *) salt->salt_buf;
10410
10411 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
10412
10413 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
10414
10415 salt->salt_len = salt_len;
10416
10417 u8 tmp_buf[100] = { 0 };
10418
10419 base64_decode (base64_to_int, (const u8 *) hash_pos, 27, tmp_buf);
10420
10421 memcpy (digest, tmp_buf, 20);
10422
10423 digest[0] = byte_swap_32 (digest[0]);
10424 digest[1] = byte_swap_32 (digest[1]);
10425 digest[2] = byte_swap_32 (digest[2]);
10426 digest[3] = byte_swap_32 (digest[3]);
10427 digest[4] = byte_swap_32 (digest[4]);
10428
10429 digest[0] -= SHA1M_A;
10430 digest[1] -= SHA1M_B;
10431 digest[2] -= SHA1M_C;
10432 digest[3] -= SHA1M_D;
10433 digest[4] -= SHA1M_E;
10434
10435 return (PARSER_OK);
10436 }
10437
10438 int descrypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10439 {
10440 if ((input_len < DISPLAY_LEN_MIN_1500) || (input_len > DISPLAY_LEN_MAX_1500)) return (PARSER_GLOBAL_LENGTH);
10441
10442 unsigned char c12 = itoa64_to_int (input_buf[12]);
10443
10444 if (c12 & 3) return (PARSER_HASH_VALUE);
10445
10446 u32 *digest = (u32 *) hash_buf->digest;
10447
10448 salt_t *salt = hash_buf->salt;
10449
10450 // for ascii_digest
10451 salt->salt_sign[0] = input_buf[0];
10452 salt->salt_sign[1] = input_buf[1];
10453
10454 salt->salt_buf[0] = itoa64_to_int (input_buf[0])
10455 | itoa64_to_int (input_buf[1]) << 6;
10456
10457 salt->salt_len = 2;
10458
10459 u8 tmp_buf[100] = { 0 };
10460
10461 base64_decode (itoa64_to_int, (const u8 *) input_buf + 2, 11, tmp_buf);
10462
10463 memcpy (digest, tmp_buf, 8);
10464
10465 uint tt;
10466
10467 IP (digest[0], digest[1], tt);
10468
10469 digest[2] = 0;
10470 digest[3] = 0;
10471
10472 return (PARSER_OK);
10473 }
10474
10475 int md4_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10476 {
10477 if ((input_len < DISPLAY_LEN_MIN_900) || (input_len > DISPLAY_LEN_MAX_900)) return (PARSER_GLOBAL_LENGTH);
10478
10479 u32 *digest = (u32 *) hash_buf->digest;
10480
10481 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
10482 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
10483 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
10484 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
10485
10486 digest[0] = byte_swap_32 (digest[0]);
10487 digest[1] = byte_swap_32 (digest[1]);
10488 digest[2] = byte_swap_32 (digest[2]);
10489 digest[3] = byte_swap_32 (digest[3]);
10490
10491 digest[0] -= MD4M_A;
10492 digest[1] -= MD4M_B;
10493 digest[2] -= MD4M_C;
10494 digest[3] -= MD4M_D;
10495
10496 return (PARSER_OK);
10497 }
10498
10499 int md4s_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10500 {
10501 if (data.opts_type & OPTS_TYPE_ST_HEX)
10502 {
10503 if ((input_len < DISPLAY_LEN_MIN_910H) || (input_len > DISPLAY_LEN_MAX_910H)) return (PARSER_GLOBAL_LENGTH);
10504 }
10505 else
10506 {
10507 if ((input_len < DISPLAY_LEN_MIN_910) || (input_len > DISPLAY_LEN_MAX_910)) return (PARSER_GLOBAL_LENGTH);
10508 }
10509
10510 u32 *digest = (u32 *) hash_buf->digest;
10511
10512 salt_t *salt = hash_buf->salt;
10513
10514 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
10515 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
10516 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
10517 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
10518
10519 digest[0] = byte_swap_32 (digest[0]);
10520 digest[1] = byte_swap_32 (digest[1]);
10521 digest[2] = byte_swap_32 (digest[2]);
10522 digest[3] = byte_swap_32 (digest[3]);
10523
10524 digest[0] -= MD4M_A;
10525 digest[1] -= MD4M_B;
10526 digest[2] -= MD4M_C;
10527 digest[3] -= MD4M_D;
10528
10529 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
10530
10531 uint salt_len = input_len - 32 - 1;
10532
10533 char *salt_buf = input_buf + 32 + 1;
10534
10535 char *salt_buf_ptr = (char *) salt->salt_buf;
10536
10537 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
10538
10539 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
10540
10541 salt->salt_len = salt_len;
10542
10543 return (PARSER_OK);
10544 }
10545
10546 int md5_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10547 {
10548 if ((input_len < DISPLAY_LEN_MIN_0) || (input_len > DISPLAY_LEN_MAX_0)) return (PARSER_GLOBAL_LENGTH);
10549
10550 u32 *digest = (u32 *) hash_buf->digest;
10551
10552 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
10553 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
10554 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
10555 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
10556
10557 digest[0] = byte_swap_32 (digest[0]);
10558 digest[1] = byte_swap_32 (digest[1]);
10559 digest[2] = byte_swap_32 (digest[2]);
10560 digest[3] = byte_swap_32 (digest[3]);
10561
10562 digest[0] -= MD5M_A;
10563 digest[1] -= MD5M_B;
10564 digest[2] -= MD5M_C;
10565 digest[3] -= MD5M_D;
10566
10567 return (PARSER_OK);
10568 }
10569
10570 int md5half_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10571 {
10572 if ((input_len < DISPLAY_LEN_MIN_5100) || (input_len > DISPLAY_LEN_MAX_5100)) return (PARSER_GLOBAL_LENGTH);
10573
10574 u32 *digest = (u32 *) hash_buf->digest;
10575
10576 digest[0] = hex_to_u32 ((const u8 *) &input_buf[0]);
10577 digest[1] = hex_to_u32 ((const u8 *) &input_buf[8]);
10578 digest[2] = 0;
10579 digest[3] = 0;
10580
10581 digest[0] = byte_swap_32 (digest[0]);
10582 digest[1] = byte_swap_32 (digest[1]);
10583
10584 return (PARSER_OK);
10585 }
10586
10587 int md5s_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10588 {
10589 if (data.opts_type & OPTS_TYPE_ST_HEX)
10590 {
10591 if ((input_len < DISPLAY_LEN_MIN_10H) || (input_len > DISPLAY_LEN_MAX_10H)) return (PARSER_GLOBAL_LENGTH);
10592 }
10593 else
10594 {
10595 if ((input_len < DISPLAY_LEN_MIN_10) || (input_len > DISPLAY_LEN_MAX_10)) return (PARSER_GLOBAL_LENGTH);
10596 }
10597
10598 u32 *digest = (u32 *) hash_buf->digest;
10599
10600 salt_t *salt = hash_buf->salt;
10601
10602 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
10603 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
10604 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
10605 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
10606
10607 digest[0] = byte_swap_32 (digest[0]);
10608 digest[1] = byte_swap_32 (digest[1]);
10609 digest[2] = byte_swap_32 (digest[2]);
10610 digest[3] = byte_swap_32 (digest[3]);
10611
10612 digest[0] -= MD5M_A;
10613 digest[1] -= MD5M_B;
10614 digest[2] -= MD5M_C;
10615 digest[3] -= MD5M_D;
10616
10617 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
10618
10619 uint salt_len = input_len - 32 - 1;
10620
10621 char *salt_buf = input_buf + 32 + 1;
10622
10623 char *salt_buf_ptr = (char *) salt->salt_buf;
10624
10625 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
10626
10627 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
10628
10629 salt->salt_len = salt_len;
10630
10631 return (PARSER_OK);
10632 }
10633
10634 int md5pix_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10635 {
10636 if ((input_len < DISPLAY_LEN_MIN_2400) || (input_len > DISPLAY_LEN_MAX_2400)) return (PARSER_GLOBAL_LENGTH);
10637
10638 u32 *digest = (u32 *) hash_buf->digest;
10639
10640 digest[0] = itoa64_to_int (input_buf[ 0]) << 0
10641 | itoa64_to_int (input_buf[ 1]) << 6
10642 | itoa64_to_int (input_buf[ 2]) << 12
10643 | itoa64_to_int (input_buf[ 3]) << 18;
10644 digest[1] = itoa64_to_int (input_buf[ 4]) << 0
10645 | itoa64_to_int (input_buf[ 5]) << 6
10646 | itoa64_to_int (input_buf[ 6]) << 12
10647 | itoa64_to_int (input_buf[ 7]) << 18;
10648 digest[2] = itoa64_to_int (input_buf[ 8]) << 0
10649 | itoa64_to_int (input_buf[ 9]) << 6
10650 | itoa64_to_int (input_buf[10]) << 12
10651 | itoa64_to_int (input_buf[11]) << 18;
10652 digest[3] = itoa64_to_int (input_buf[12]) << 0
10653 | itoa64_to_int (input_buf[13]) << 6
10654 | itoa64_to_int (input_buf[14]) << 12
10655 | itoa64_to_int (input_buf[15]) << 18;
10656
10657 digest[0] -= MD5M_A;
10658 digest[1] -= MD5M_B;
10659 digest[2] -= MD5M_C;
10660 digest[3] -= MD5M_D;
10661
10662 digest[0] &= 0x00ffffff;
10663 digest[1] &= 0x00ffffff;
10664 digest[2] &= 0x00ffffff;
10665 digest[3] &= 0x00ffffff;
10666
10667 return (PARSER_OK);
10668 }
10669
10670 int md5asa_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10671 {
10672 if (data.opts_type & OPTS_TYPE_ST_HEX)
10673 {
10674 if ((input_len < DISPLAY_LEN_MIN_2410H) || (input_len > DISPLAY_LEN_MAX_2410H)) return (PARSER_GLOBAL_LENGTH);
10675 }
10676 else
10677 {
10678 if ((input_len < DISPLAY_LEN_MIN_2410) || (input_len > DISPLAY_LEN_MAX_2410)) return (PARSER_GLOBAL_LENGTH);
10679 }
10680
10681 u32 *digest = (u32 *) hash_buf->digest;
10682
10683 salt_t *salt = hash_buf->salt;
10684
10685 digest[0] = itoa64_to_int (input_buf[ 0]) << 0
10686 | itoa64_to_int (input_buf[ 1]) << 6
10687 | itoa64_to_int (input_buf[ 2]) << 12
10688 | itoa64_to_int (input_buf[ 3]) << 18;
10689 digest[1] = itoa64_to_int (input_buf[ 4]) << 0
10690 | itoa64_to_int (input_buf[ 5]) << 6
10691 | itoa64_to_int (input_buf[ 6]) << 12
10692 | itoa64_to_int (input_buf[ 7]) << 18;
10693 digest[2] = itoa64_to_int (input_buf[ 8]) << 0
10694 | itoa64_to_int (input_buf[ 9]) << 6
10695 | itoa64_to_int (input_buf[10]) << 12
10696 | itoa64_to_int (input_buf[11]) << 18;
10697 digest[3] = itoa64_to_int (input_buf[12]) << 0
10698 | itoa64_to_int (input_buf[13]) << 6
10699 | itoa64_to_int (input_buf[14]) << 12
10700 | itoa64_to_int (input_buf[15]) << 18;
10701
10702 digest[0] -= MD5M_A;
10703 digest[1] -= MD5M_B;
10704 digest[2] -= MD5M_C;
10705 digest[3] -= MD5M_D;
10706
10707 digest[0] &= 0x00ffffff;
10708 digest[1] &= 0x00ffffff;
10709 digest[2] &= 0x00ffffff;
10710 digest[3] &= 0x00ffffff;
10711
10712 if (input_buf[16] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
10713
10714 uint salt_len = input_len - 16 - 1;
10715
10716 char *salt_buf = input_buf + 16 + 1;
10717
10718 char *salt_buf_ptr = (char *) salt->salt_buf;
10719
10720 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
10721
10722 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
10723
10724 salt->salt_len = salt_len;
10725
10726 return (PARSER_OK);
10727 }
10728
10729 void transform_netntlmv1_key (const u8 *nthash, u8 *key)
10730 {
10731 key[0] = (nthash[0] >> 0);
10732 key[1] = (nthash[0] << 7) | (nthash[1] >> 1);
10733 key[2] = (nthash[1] << 6) | (nthash[2] >> 2);
10734 key[3] = (nthash[2] << 5) | (nthash[3] >> 3);
10735 key[4] = (nthash[3] << 4) | (nthash[4] >> 4);
10736 key[5] = (nthash[4] << 3) | (nthash[5] >> 5);
10737 key[6] = (nthash[5] << 2) | (nthash[6] >> 6);
10738 key[7] = (nthash[6] << 1);
10739
10740 key[0] |= 0x01;
10741 key[1] |= 0x01;
10742 key[2] |= 0x01;
10743 key[3] |= 0x01;
10744 key[4] |= 0x01;
10745 key[5] |= 0x01;
10746 key[6] |= 0x01;
10747 key[7] |= 0x01;
10748 }
10749
10750 int netntlmv1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10751 {
10752 if ((input_len < DISPLAY_LEN_MIN_5500) || (input_len > DISPLAY_LEN_MAX_5500)) return (PARSER_GLOBAL_LENGTH);
10753
10754 u32 *digest = (u32 *) hash_buf->digest;
10755
10756 salt_t *salt = hash_buf->salt;
10757
10758 netntlm_t *netntlm = (netntlm_t *) hash_buf->esalt;
10759
10760 /**
10761 * parse line
10762 */
10763
10764 char *user_pos = input_buf;
10765
10766 char *unused_pos = strchr (user_pos, ':');
10767
10768 if (unused_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
10769
10770 uint user_len = unused_pos - user_pos;
10771
10772 if (user_len > 60) return (PARSER_SALT_LENGTH);
10773
10774 unused_pos++;
10775
10776 char *domain_pos = strchr (unused_pos, ':');
10777
10778 if (domain_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
10779
10780 uint unused_len = domain_pos - unused_pos;
10781
10782 if (unused_len != 0) return (PARSER_SALT_LENGTH);
10783
10784 domain_pos++;
10785
10786 char *srvchall_pos = strchr (domain_pos, ':');
10787
10788 if (srvchall_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
10789
10790 uint domain_len = srvchall_pos - domain_pos;
10791
10792 if (domain_len > 45) return (PARSER_SALT_LENGTH);
10793
10794 srvchall_pos++;
10795
10796 char *hash_pos = strchr (srvchall_pos, ':');
10797
10798 if (srvchall_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
10799
10800 uint srvchall_len = hash_pos - srvchall_pos;
10801
10802 // if (srvchall_len != 0) return (PARSER_SALT_LENGTH);
10803
10804 hash_pos++;
10805
10806 char *clichall_pos = strchr (hash_pos, ':');
10807
10808 if (clichall_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
10809
10810 uint hash_len = clichall_pos - hash_pos;
10811
10812 if (hash_len != 48) return (PARSER_HASH_LENGTH);
10813
10814 clichall_pos++;
10815
10816 uint clichall_len = input_len - user_len - 1 - unused_len - 1 - domain_len - 1 - srvchall_len - 1 - hash_len - 1;
10817
10818 if (clichall_len != 16) return (PARSER_SALT_LENGTH);
10819
10820 /**
10821 * store some data for later use
10822 */
10823
10824 netntlm->user_len = user_len * 2;
10825 netntlm->domain_len = domain_len * 2;
10826 netntlm->srvchall_len = srvchall_len / 2;
10827 netntlm->clichall_len = clichall_len / 2;
10828
10829 char *userdomain_ptr = (char *) netntlm->userdomain_buf;
10830 char *chall_ptr = (char *) netntlm->chall_buf;
10831
10832 /**
10833 * handle username and domainname
10834 */
10835
10836 for (uint i = 0; i < user_len; i++)
10837 {
10838 *userdomain_ptr++ = user_pos[i];
10839 *userdomain_ptr++ = 0;
10840 }
10841
10842 for (uint i = 0; i < domain_len; i++)
10843 {
10844 *userdomain_ptr++ = domain_pos[i];
10845 *userdomain_ptr++ = 0;
10846 }
10847
10848 /**
10849 * handle server challenge encoding
10850 */
10851
10852 for (uint i = 0; i < srvchall_len; i += 2)
10853 {
10854 const char p0 = srvchall_pos[i + 0];
10855 const char p1 = srvchall_pos[i + 1];
10856
10857 *chall_ptr++ = hex_convert (p1) << 0
10858 | hex_convert (p0) << 4;
10859 }
10860
10861 /**
10862 * handle client challenge encoding
10863 */
10864
10865 for (uint i = 0; i < clichall_len; i += 2)
10866 {
10867 const char p0 = clichall_pos[i + 0];
10868 const char p1 = clichall_pos[i + 1];
10869
10870 *chall_ptr++ = hex_convert (p1) << 0
10871 | hex_convert (p0) << 4;
10872 }
10873
10874 /**
10875 * store data
10876 */
10877
10878 char *salt_buf_ptr = (char *) salt->salt_buf;
10879
10880 uint salt_len = parse_and_store_salt (salt_buf_ptr, clichall_pos, clichall_len);
10881
10882 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
10883
10884 salt->salt_len = salt_len;
10885
10886 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
10887 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
10888 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
10889 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
10890
10891 digest[0] = byte_swap_32 (digest[0]);
10892 digest[1] = byte_swap_32 (digest[1]);
10893 digest[2] = byte_swap_32 (digest[2]);
10894 digest[3] = byte_swap_32 (digest[3]);
10895
10896 /* special case, last 8 byte do not need to be checked since they are brute-forced next */
10897
10898 uint digest_tmp[2] = { 0 };
10899
10900 digest_tmp[0] = hex_to_u32 ((const u8 *) &hash_pos[32]);
10901 digest_tmp[1] = hex_to_u32 ((const u8 *) &hash_pos[40]);
10902
10903 digest_tmp[0] = byte_swap_32 (digest_tmp[0]);
10904 digest_tmp[1] = byte_swap_32 (digest_tmp[1]);
10905
10906 /* special case 2: ESS */
10907
10908 if (srvchall_len == 48)
10909 {
10910 if ((netntlm->chall_buf[2] == 0) && (netntlm->chall_buf[3] == 0) && (netntlm->chall_buf[4] == 0) && (netntlm->chall_buf[5] == 0))
10911 {
10912 uint w[16] = { 0 };
10913
10914 w[ 0] = netntlm->chall_buf[6];
10915 w[ 1] = netntlm->chall_buf[7];
10916 w[ 2] = netntlm->chall_buf[0];
10917 w[ 3] = netntlm->chall_buf[1];
10918 w[ 4] = 0x80;
10919 w[14] = 16 * 8;
10920
10921 uint dgst[4] = { 0 };
10922
10923 dgst[0] = MAGIC_A;
10924 dgst[1] = MAGIC_B;
10925 dgst[2] = MAGIC_C;
10926 dgst[3] = MAGIC_D;
10927
10928 md5_64 (w, dgst);
10929
10930 salt->salt_buf[0] = dgst[0];
10931 salt->salt_buf[1] = dgst[1];
10932 }
10933 }
10934
10935 /* precompute netntlmv1 exploit start */
10936
10937 for (uint i = 0; i < 0x10000; i++)
10938 {
10939 uint key_md4[2] = { i, 0 };
10940 uint key_des[2] = { 0, 0 };
10941
10942 transform_netntlmv1_key ((u8 *) key_md4, (u8 *) key_des);
10943
10944 uint Kc[16] = { 0 };
10945 uint Kd[16] = { 0 };
10946
10947 _des_keysetup (key_des, Kc, Kd, c_skb);
10948
10949 uint data3[2] = { salt->salt_buf[0], salt->salt_buf[1] };
10950
10951 _des_encrypt (data3, Kc, Kd, c_SPtrans);
10952
10953 if (data3[0] != digest_tmp[0]) continue;
10954 if (data3[1] != digest_tmp[1]) continue;
10955
10956 salt->salt_buf[2] = i;
10957
10958 salt->salt_len = 24;
10959
10960 break;
10961 }
10962
10963 salt->salt_buf_pc[0] = digest_tmp[0];
10964 salt->salt_buf_pc[1] = digest_tmp[1];
10965
10966 /* precompute netntlmv1 exploit stop */
10967
10968 u32 tt;
10969
10970 IP (digest[0], digest[1], tt);
10971 IP (digest[2], digest[3], tt);
10972
10973 digest[0] = rotr32 (digest[0], 29);
10974 digest[1] = rotr32 (digest[1], 29);
10975 digest[2] = rotr32 (digest[2], 29);
10976 digest[3] = rotr32 (digest[3], 29);
10977
10978 IP (salt->salt_buf[0], salt->salt_buf[1], tt);
10979
10980 salt->salt_buf[0] = rotl32 (salt->salt_buf[0], 3);
10981 salt->salt_buf[1] = rotl32 (salt->salt_buf[1], 3);
10982
10983 return (PARSER_OK);
10984 }
10985
10986 int netntlmv2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
10987 {
10988 if ((input_len < DISPLAY_LEN_MIN_5600) || (input_len > DISPLAY_LEN_MAX_5600)) return (PARSER_GLOBAL_LENGTH);
10989
10990 u32 *digest = (u32 *) hash_buf->digest;
10991
10992 salt_t *salt = hash_buf->salt;
10993
10994 netntlm_t *netntlm = (netntlm_t *) hash_buf->esalt;
10995
10996 /**
10997 * parse line
10998 */
10999
11000 char *user_pos = input_buf;
11001
11002 char *unused_pos = strchr (user_pos, ':');
11003
11004 if (unused_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
11005
11006 uint user_len = unused_pos - user_pos;
11007
11008 if (user_len > 60) return (PARSER_SALT_LENGTH);
11009
11010 unused_pos++;
11011
11012 char *domain_pos = strchr (unused_pos, ':');
11013
11014 if (domain_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
11015
11016 uint unused_len = domain_pos - unused_pos;
11017
11018 if (unused_len != 0) return (PARSER_SALT_LENGTH);
11019
11020 domain_pos++;
11021
11022 char *srvchall_pos = strchr (domain_pos, ':');
11023
11024 if (srvchall_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
11025
11026 uint domain_len = srvchall_pos - domain_pos;
11027
11028 if (domain_len > 45) return (PARSER_SALT_LENGTH);
11029
11030 srvchall_pos++;
11031
11032 char *hash_pos = strchr (srvchall_pos, ':');
11033
11034 if (srvchall_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
11035
11036 uint srvchall_len = hash_pos - srvchall_pos;
11037
11038 if (srvchall_len != 16) return (PARSER_SALT_LENGTH);
11039
11040 hash_pos++;
11041
11042 char *clichall_pos = strchr (hash_pos, ':');
11043
11044 if (clichall_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
11045
11046 uint hash_len = clichall_pos - hash_pos;
11047
11048 if (hash_len != 32) return (PARSER_HASH_LENGTH);
11049
11050 clichall_pos++;
11051
11052 uint clichall_len = input_len - user_len - 1 - unused_len - 1 - domain_len - 1 - srvchall_len - 1 - hash_len - 1;
11053
11054 if (clichall_len > 1024) return (PARSER_SALT_LENGTH);
11055
11056 if (clichall_len % 2) return (PARSER_SALT_VALUE);
11057
11058 /**
11059 * store some data for later use
11060 */
11061
11062 netntlm->user_len = user_len * 2;
11063 netntlm->domain_len = domain_len * 2;
11064 netntlm->srvchall_len = srvchall_len / 2;
11065 netntlm->clichall_len = clichall_len / 2;
11066
11067 char *userdomain_ptr = (char *) netntlm->userdomain_buf;
11068 char *chall_ptr = (char *) netntlm->chall_buf;
11069
11070 /**
11071 * handle username and domainname
11072 */
11073
11074 for (uint i = 0; i < user_len; i++)
11075 {
11076 *userdomain_ptr++ = toupper (user_pos[i]);
11077 *userdomain_ptr++ = 0;
11078 }
11079
11080 for (uint i = 0; i < domain_len; i++)
11081 {
11082 *userdomain_ptr++ = domain_pos[i];
11083 *userdomain_ptr++ = 0;
11084 }
11085
11086 *userdomain_ptr++ = 0x80;
11087
11088 /**
11089 * handle server challenge encoding
11090 */
11091
11092 for (uint i = 0; i < srvchall_len; i += 2)
11093 {
11094 const char p0 = srvchall_pos[i + 0];
11095 const char p1 = srvchall_pos[i + 1];
11096
11097 *chall_ptr++ = hex_convert (p1) << 0
11098 | hex_convert (p0) << 4;
11099 }
11100
11101 /**
11102 * handle client challenge encoding
11103 */
11104
11105 for (uint i = 0; i < clichall_len; i += 2)
11106 {
11107 const char p0 = clichall_pos[i + 0];
11108 const char p1 = clichall_pos[i + 1];
11109
11110 *chall_ptr++ = hex_convert (p1) << 0
11111 | hex_convert (p0) << 4;
11112 }
11113
11114 *chall_ptr++ = 0x80;
11115
11116 /**
11117 * handle hash itself
11118 */
11119
11120 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
11121 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
11122 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
11123 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
11124
11125 digest[0] = byte_swap_32 (digest[0]);
11126 digest[1] = byte_swap_32 (digest[1]);
11127 digest[2] = byte_swap_32 (digest[2]);
11128 digest[3] = byte_swap_32 (digest[3]);
11129
11130 /**
11131 * reuse challange data as salt_buf, its the buffer that is most likely unique
11132 */
11133
11134 salt->salt_buf[0] = 0;
11135 salt->salt_buf[1] = 0;
11136 salt->salt_buf[2] = 0;
11137 salt->salt_buf[3] = 0;
11138 salt->salt_buf[4] = 0;
11139 salt->salt_buf[5] = 0;
11140 salt->salt_buf[6] = 0;
11141 salt->salt_buf[7] = 0;
11142
11143 uint *uptr;
11144
11145 uptr = (uint *) netntlm->userdomain_buf;
11146
11147 for (uint i = 0; i < 16; i += 16)
11148 {
11149 md5_64 (uptr, salt->salt_buf);
11150 }
11151
11152 uptr = (uint *) netntlm->chall_buf;
11153
11154 for (uint i = 0; i < 256; i += 16)
11155 {
11156 md5_64 (uptr, salt->salt_buf);
11157 }
11158
11159 salt->salt_len = 16;
11160
11161 return (PARSER_OK);
11162 }
11163
11164 int joomla_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11165 {
11166 if (data.opts_type & OPTS_TYPE_ST_HEX)
11167 {
11168 if ((input_len < DISPLAY_LEN_MIN_11H) || (input_len > DISPLAY_LEN_MAX_11H)) return (PARSER_GLOBAL_LENGTH);
11169 }
11170 else
11171 {
11172 if ((input_len < DISPLAY_LEN_MIN_11) || (input_len > DISPLAY_LEN_MAX_11)) return (PARSER_GLOBAL_LENGTH);
11173 }
11174
11175 u32 *digest = (u32 *) hash_buf->digest;
11176
11177 salt_t *salt = hash_buf->salt;
11178
11179 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11180 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11181 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11182 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11183
11184 digest[0] = byte_swap_32 (digest[0]);
11185 digest[1] = byte_swap_32 (digest[1]);
11186 digest[2] = byte_swap_32 (digest[2]);
11187 digest[3] = byte_swap_32 (digest[3]);
11188
11189 digest[0] -= MD5M_A;
11190 digest[1] -= MD5M_B;
11191 digest[2] -= MD5M_C;
11192 digest[3] -= MD5M_D;
11193
11194 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
11195
11196 uint salt_len = input_len - 32 - 1;
11197
11198 char *salt_buf = input_buf + 32 + 1;
11199
11200 char *salt_buf_ptr = (char *) salt->salt_buf;
11201
11202 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
11203
11204 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11205
11206 salt->salt_len = salt_len;
11207
11208 return (PARSER_OK);
11209 }
11210
11211 int postgresql_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11212 {
11213 if (data.opts_type & OPTS_TYPE_ST_HEX)
11214 {
11215 if ((input_len < DISPLAY_LEN_MIN_12H) || (input_len > DISPLAY_LEN_MAX_12H)) return (PARSER_GLOBAL_LENGTH);
11216 }
11217 else
11218 {
11219 if ((input_len < DISPLAY_LEN_MIN_12) || (input_len > DISPLAY_LEN_MAX_12)) return (PARSER_GLOBAL_LENGTH);
11220 }
11221
11222 u32 *digest = (u32 *) hash_buf->digest;
11223
11224 salt_t *salt = hash_buf->salt;
11225
11226 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11227 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11228 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11229 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11230
11231 digest[0] = byte_swap_32 (digest[0]);
11232 digest[1] = byte_swap_32 (digest[1]);
11233 digest[2] = byte_swap_32 (digest[2]);
11234 digest[3] = byte_swap_32 (digest[3]);
11235
11236 digest[0] -= MD5M_A;
11237 digest[1] -= MD5M_B;
11238 digest[2] -= MD5M_C;
11239 digest[3] -= MD5M_D;
11240
11241 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
11242
11243 uint salt_len = input_len - 32 - 1;
11244
11245 char *salt_buf = input_buf + 32 + 1;
11246
11247 char *salt_buf_ptr = (char *) salt->salt_buf;
11248
11249 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
11250
11251 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11252
11253 salt->salt_len = salt_len;
11254
11255 return (PARSER_OK);
11256 }
11257
11258 int md5md5_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11259 {
11260 if ((input_len < DISPLAY_LEN_MIN_2600) || (input_len > DISPLAY_LEN_MAX_2600)) return (PARSER_GLOBAL_LENGTH);
11261
11262 u32 *digest = (u32 *) hash_buf->digest;
11263
11264 salt_t *salt = hash_buf->salt;
11265
11266 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11267 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11268 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11269 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11270
11271 digest[0] = byte_swap_32 (digest[0]);
11272 digest[1] = byte_swap_32 (digest[1]);
11273 digest[2] = byte_swap_32 (digest[2]);
11274 digest[3] = byte_swap_32 (digest[3]);
11275
11276 digest[0] -= MD5M_A;
11277 digest[1] -= MD5M_B;
11278 digest[2] -= MD5M_C;
11279 digest[3] -= MD5M_D;
11280
11281 /**
11282 * This is a virtual salt. While the algorithm is basically not salted
11283 * we can exploit the salt buffer to set the 0x80 and the w[14] value.
11284 * This way we can save a special md5md5 kernel and reuse the one from vbull.
11285 */
11286
11287 char *salt_buf_ptr = (char *) salt->salt_buf;
11288
11289 uint salt_len = parse_and_store_salt (salt_buf_ptr, (char *) "", 0);
11290
11291 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11292
11293 salt->salt_len = salt_len;
11294
11295 return (PARSER_OK);
11296 }
11297
11298 int vb3_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11299 {
11300 if (data.opts_type & OPTS_TYPE_ST_HEX)
11301 {
11302 if ((input_len < DISPLAY_LEN_MIN_2611H) || (input_len > DISPLAY_LEN_MAX_2611H)) return (PARSER_GLOBAL_LENGTH);
11303 }
11304 else
11305 {
11306 if ((input_len < DISPLAY_LEN_MIN_2611) || (input_len > DISPLAY_LEN_MAX_2611)) return (PARSER_GLOBAL_LENGTH);
11307 }
11308
11309 u32 *digest = (u32 *) hash_buf->digest;
11310
11311 salt_t *salt = hash_buf->salt;
11312
11313 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11314 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11315 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11316 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11317
11318 digest[0] = byte_swap_32 (digest[0]);
11319 digest[1] = byte_swap_32 (digest[1]);
11320 digest[2] = byte_swap_32 (digest[2]);
11321 digest[3] = byte_swap_32 (digest[3]);
11322
11323 digest[0] -= MD5M_A;
11324 digest[1] -= MD5M_B;
11325 digest[2] -= MD5M_C;
11326 digest[3] -= MD5M_D;
11327
11328 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
11329
11330 uint salt_len = input_len - 32 - 1;
11331
11332 char *salt_buf = input_buf + 32 + 1;
11333
11334 char *salt_buf_ptr = (char *) salt->salt_buf;
11335
11336 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
11337
11338 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11339
11340 salt->salt_len = salt_len;
11341
11342 return (PARSER_OK);
11343 }
11344
11345 int vb30_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11346 {
11347 if (data.opts_type & OPTS_TYPE_ST_HEX)
11348 {
11349 if ((input_len < DISPLAY_LEN_MIN_2711H) || (input_len > DISPLAY_LEN_MAX_2711H)) return (PARSER_GLOBAL_LENGTH);
11350 }
11351 else
11352 {
11353 if ((input_len < DISPLAY_LEN_MIN_2711) || (input_len > DISPLAY_LEN_MAX_2711)) return (PARSER_GLOBAL_LENGTH);
11354 }
11355
11356 u32 *digest = (u32 *) hash_buf->digest;
11357
11358 salt_t *salt = hash_buf->salt;
11359
11360 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11361 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11362 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11363 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11364
11365 digest[0] = byte_swap_32 (digest[0]);
11366 digest[1] = byte_swap_32 (digest[1]);
11367 digest[2] = byte_swap_32 (digest[2]);
11368 digest[3] = byte_swap_32 (digest[3]);
11369
11370 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
11371
11372 uint salt_len = input_len - 32 - 1;
11373
11374 char *salt_buf = input_buf + 32 + 1;
11375
11376 char *salt_buf_ptr = (char *) salt->salt_buf;
11377
11378 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
11379
11380 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11381
11382 salt->salt_len = salt_len;
11383
11384 return (PARSER_OK);
11385 }
11386
11387 int dcc_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11388 {
11389 if (data.opts_type & OPTS_TYPE_ST_HEX)
11390 {
11391 if ((input_len < DISPLAY_LEN_MIN_1100H) || (input_len > DISPLAY_LEN_MAX_1100H)) return (PARSER_GLOBAL_LENGTH);
11392 }
11393 else
11394 {
11395 if ((input_len < DISPLAY_LEN_MIN_1100) || (input_len > DISPLAY_LEN_MAX_1100)) return (PARSER_GLOBAL_LENGTH);
11396 }
11397
11398 u32 *digest = (u32 *) hash_buf->digest;
11399
11400 salt_t *salt = hash_buf->salt;
11401
11402 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11403 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11404 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11405 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11406
11407 digest[0] = byte_swap_32 (digest[0]);
11408 digest[1] = byte_swap_32 (digest[1]);
11409 digest[2] = byte_swap_32 (digest[2]);
11410 digest[3] = byte_swap_32 (digest[3]);
11411
11412 digest[0] -= MD4M_A;
11413 digest[1] -= MD4M_B;
11414 digest[2] -= MD4M_C;
11415 digest[3] -= MD4M_D;
11416
11417 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
11418
11419 uint salt_len = input_len - 32 - 1;
11420
11421 char *salt_buf = input_buf + 32 + 1;
11422
11423 char *salt_buf_ptr = (char *) salt->salt_buf;
11424
11425 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
11426
11427 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11428
11429 salt->salt_len = salt_len;
11430
11431 return (PARSER_OK);
11432 }
11433
11434 int ipb2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11435 {
11436 if (data.opts_type & OPTS_TYPE_ST_HEX)
11437 {
11438 if ((input_len < DISPLAY_LEN_MIN_2811H) || (input_len > DISPLAY_LEN_MAX_2811H)) return (PARSER_GLOBAL_LENGTH);
11439 }
11440 else
11441 {
11442 if ((input_len < DISPLAY_LEN_MIN_2811) || (input_len > DISPLAY_LEN_MAX_2811)) return (PARSER_GLOBAL_LENGTH);
11443 }
11444
11445 u32 *digest = (u32 *) hash_buf->digest;
11446
11447 salt_t *salt = hash_buf->salt;
11448
11449 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11450 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11451 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11452 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11453
11454 digest[0] = byte_swap_32 (digest[0]);
11455 digest[1] = byte_swap_32 (digest[1]);
11456 digest[2] = byte_swap_32 (digest[2]);
11457 digest[3] = byte_swap_32 (digest[3]);
11458
11459 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
11460
11461 uint salt_len = input_len - 32 - 1;
11462
11463 char *salt_buf = input_buf + 32 + 1;
11464
11465 uint salt_pc_block[16] = { 0 };
11466
11467 char *salt_pc_block_ptr = (char *) salt_pc_block;
11468
11469 salt_len = parse_and_store_salt (salt_pc_block_ptr, salt_buf, salt_len);
11470
11471 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11472
11473 salt_pc_block_ptr[salt_len] = (unsigned char) 0x80;
11474
11475 salt_pc_block[14] = salt_len * 8;
11476
11477 uint salt_pc_digest[4] = { MAGIC_A, MAGIC_B, MAGIC_C, MAGIC_D };
11478
11479 md5_64 (salt_pc_block, salt_pc_digest);
11480
11481 salt_pc_digest[0] = byte_swap_32 (salt_pc_digest[0]);
11482 salt_pc_digest[1] = byte_swap_32 (salt_pc_digest[1]);
11483 salt_pc_digest[2] = byte_swap_32 (salt_pc_digest[2]);
11484 salt_pc_digest[3] = byte_swap_32 (salt_pc_digest[3]);
11485
11486 u8 *salt_buf_ptr = (u8 *) salt->salt_buf;
11487
11488 memcpy (salt_buf_ptr, salt_buf, salt_len);
11489
11490 u8 *salt_buf_pc_ptr = (u8 *) salt->salt_buf_pc;
11491
11492 bin_to_hex_lower (salt_pc_digest[0], salt_buf_pc_ptr + 0);
11493 bin_to_hex_lower (salt_pc_digest[1], salt_buf_pc_ptr + 8);
11494 bin_to_hex_lower (salt_pc_digest[2], salt_buf_pc_ptr + 16);
11495 bin_to_hex_lower (salt_pc_digest[3], salt_buf_pc_ptr + 24);
11496
11497 salt->salt_len = 32; // changed, was salt_len before -- was a bug? 32 should be correct
11498
11499 return (PARSER_OK);
11500 }
11501
11502 int sha1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11503 {
11504 if ((input_len < DISPLAY_LEN_MIN_100) || (input_len > DISPLAY_LEN_MAX_100)) return (PARSER_GLOBAL_LENGTH);
11505
11506 u32 *digest = (u32 *) hash_buf->digest;
11507
11508 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11509 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11510 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11511 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11512 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
11513
11514 digest[0] -= SHA1M_A;
11515 digest[1] -= SHA1M_B;
11516 digest[2] -= SHA1M_C;
11517 digest[3] -= SHA1M_D;
11518 digest[4] -= SHA1M_E;
11519
11520 return (PARSER_OK);
11521 }
11522
11523 int sha1linkedin_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11524 {
11525 if ((input_len < DISPLAY_LEN_MIN_100) || (input_len > DISPLAY_LEN_MAX_100)) return (PARSER_GLOBAL_LENGTH);
11526
11527 u32 *digest = (u32 *) hash_buf->digest;
11528
11529 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11530 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11531 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11532 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11533 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
11534
11535 return (PARSER_OK);
11536 }
11537
11538 int sha1axcrypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11539 {
11540 if ((input_len < DISPLAY_LEN_MIN_13300) || (input_len > DISPLAY_LEN_MAX_13300)) return (PARSER_GLOBAL_LENGTH);
11541
11542 if (memcmp (SIGNATURE_AXCRYPT_SHA1, input_buf, 13)) return (PARSER_SIGNATURE_UNMATCHED);
11543
11544 u32 *digest = (u32 *) hash_buf->digest;
11545
11546 input_buf +=14;
11547
11548 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11549 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11550 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11551 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11552 digest[4] = 0x00000000;
11553
11554 return (PARSER_OK);
11555 }
11556
11557 int sha1s_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11558 {
11559 if (data.opts_type & OPTS_TYPE_ST_HEX)
11560 {
11561 if ((input_len < DISPLAY_LEN_MIN_110H) || (input_len > DISPLAY_LEN_MAX_110H)) return (PARSER_GLOBAL_LENGTH);
11562 }
11563 else
11564 {
11565 if ((input_len < DISPLAY_LEN_MIN_110) || (input_len > DISPLAY_LEN_MAX_110)) return (PARSER_GLOBAL_LENGTH);
11566 }
11567
11568 u32 *digest = (u32 *) hash_buf->digest;
11569
11570 salt_t *salt = hash_buf->salt;
11571
11572 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11573 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11574 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11575 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11576 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
11577
11578 digest[0] -= SHA1M_A;
11579 digest[1] -= SHA1M_B;
11580 digest[2] -= SHA1M_C;
11581 digest[3] -= SHA1M_D;
11582 digest[4] -= SHA1M_E;
11583
11584 if (input_buf[40] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
11585
11586 uint salt_len = input_len - 40 - 1;
11587
11588 char *salt_buf = input_buf + 40 + 1;
11589
11590 char *salt_buf_ptr = (char *) salt->salt_buf;
11591
11592 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
11593
11594 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11595
11596 salt->salt_len = salt_len;
11597
11598 return (PARSER_OK);
11599 }
11600
11601 int sha1b64_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11602 {
11603 if ((input_len < DISPLAY_LEN_MIN_101) || (input_len > DISPLAY_LEN_MAX_101)) return (PARSER_GLOBAL_LENGTH);
11604
11605 if (memcmp (SIGNATURE_SHA1B64, input_buf, 5)) return (PARSER_SIGNATURE_UNMATCHED);
11606
11607 u32 *digest = (u32 *) hash_buf->digest;
11608
11609 u8 tmp_buf[100] = { 0 };
11610
11611 base64_decode (base64_to_int, (const u8 *) input_buf + 5, input_len - 5, tmp_buf);
11612
11613 memcpy (digest, tmp_buf, 20);
11614
11615 digest[0] = byte_swap_32 (digest[0]);
11616 digest[1] = byte_swap_32 (digest[1]);
11617 digest[2] = byte_swap_32 (digest[2]);
11618 digest[3] = byte_swap_32 (digest[3]);
11619 digest[4] = byte_swap_32 (digest[4]);
11620
11621 digest[0] -= SHA1M_A;
11622 digest[1] -= SHA1M_B;
11623 digest[2] -= SHA1M_C;
11624 digest[3] -= SHA1M_D;
11625 digest[4] -= SHA1M_E;
11626
11627 return (PARSER_OK);
11628 }
11629
11630 int sha1b64s_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11631 {
11632 if ((input_len < DISPLAY_LEN_MIN_111) || (input_len > DISPLAY_LEN_MAX_111)) return (PARSER_GLOBAL_LENGTH);
11633
11634 if (memcmp (SIGNATURE_SSHA1B64_lower, input_buf, 6) && memcmp (SIGNATURE_SSHA1B64_upper, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
11635
11636 u32 *digest = (u32 *) hash_buf->digest;
11637
11638 salt_t *salt = hash_buf->salt;
11639
11640 u8 tmp_buf[100] = { 0 };
11641
11642 int tmp_len = base64_decode (base64_to_int, (const u8 *) input_buf + 6, input_len - 6, tmp_buf);
11643
11644 if (tmp_len < 20) return (PARSER_HASH_LENGTH);
11645
11646 memcpy (digest, tmp_buf, 20);
11647
11648 int salt_len = tmp_len - 20;
11649
11650 if (salt_len < 0) return (PARSER_SALT_LENGTH);
11651
11652 salt->salt_len = salt_len;
11653
11654 memcpy (salt->salt_buf, tmp_buf + 20, salt->salt_len);
11655
11656 if (data.opts_type & OPTS_TYPE_ST_ADD80)
11657 {
11658 char *ptr = (char *) salt->salt_buf;
11659
11660 ptr[salt->salt_len] = 0x80;
11661 }
11662
11663 digest[0] = byte_swap_32 (digest[0]);
11664 digest[1] = byte_swap_32 (digest[1]);
11665 digest[2] = byte_swap_32 (digest[2]);
11666 digest[3] = byte_swap_32 (digest[3]);
11667 digest[4] = byte_swap_32 (digest[4]);
11668
11669 digest[0] -= SHA1M_A;
11670 digest[1] -= SHA1M_B;
11671 digest[2] -= SHA1M_C;
11672 digest[3] -= SHA1M_D;
11673 digest[4] -= SHA1M_E;
11674
11675 return (PARSER_OK);
11676 }
11677
11678 int mssql2000_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11679 {
11680 if ((input_len < DISPLAY_LEN_MIN_131) || (input_len > DISPLAY_LEN_MAX_131)) return (PARSER_GLOBAL_LENGTH);
11681
11682 if (memcmp (SIGNATURE_MSSQL, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
11683
11684 u32 *digest = (u32 *) hash_buf->digest;
11685
11686 salt_t *salt = hash_buf->salt;
11687
11688 char *salt_buf = input_buf + 6;
11689
11690 uint salt_len = 8;
11691
11692 char *salt_buf_ptr = (char *) salt->salt_buf;
11693
11694 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
11695
11696 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11697
11698 salt->salt_len = salt_len;
11699
11700 char *hash_pos = input_buf + 6 + 8 + 40;
11701
11702 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
11703 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
11704 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
11705 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
11706 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
11707
11708 digest[0] -= SHA1M_A;
11709 digest[1] -= SHA1M_B;
11710 digest[2] -= SHA1M_C;
11711 digest[3] -= SHA1M_D;
11712 digest[4] -= SHA1M_E;
11713
11714 return (PARSER_OK);
11715 }
11716
11717 int mssql2005_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11718 {
11719 if ((input_len < DISPLAY_LEN_MIN_132) || (input_len > DISPLAY_LEN_MAX_132)) return (PARSER_GLOBAL_LENGTH);
11720
11721 if (memcmp (SIGNATURE_MSSQL, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
11722
11723 u32 *digest = (u32 *) hash_buf->digest;
11724
11725 salt_t *salt = hash_buf->salt;
11726
11727 char *salt_buf = input_buf + 6;
11728
11729 uint salt_len = 8;
11730
11731 char *salt_buf_ptr = (char *) salt->salt_buf;
11732
11733 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
11734
11735 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11736
11737 salt->salt_len = salt_len;
11738
11739 char *hash_pos = input_buf + 6 + 8;
11740
11741 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
11742 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
11743 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
11744 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
11745 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
11746
11747 digest[0] -= SHA1M_A;
11748 digest[1] -= SHA1M_B;
11749 digest[2] -= SHA1M_C;
11750 digest[3] -= SHA1M_D;
11751 digest[4] -= SHA1M_E;
11752
11753 return (PARSER_OK);
11754 }
11755
11756 int mssql2012_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11757 {
11758 if ((input_len < DISPLAY_LEN_MIN_1731) || (input_len > DISPLAY_LEN_MAX_1731)) return (PARSER_GLOBAL_LENGTH);
11759
11760 if (memcmp (SIGNATURE_MSSQL2012, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
11761
11762 u64 *digest = (u64 *) hash_buf->digest;
11763
11764 salt_t *salt = hash_buf->salt;
11765
11766 char *salt_buf = input_buf + 6;
11767
11768 uint salt_len = 8;
11769
11770 char *salt_buf_ptr = (char *) salt->salt_buf;
11771
11772 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
11773
11774 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11775
11776 salt->salt_len = salt_len;
11777
11778 char *hash_pos = input_buf + 6 + 8;
11779
11780 digest[0] = hex_to_u64 ((const u8 *) &hash_pos[ 0]);
11781 digest[1] = hex_to_u64 ((const u8 *) &hash_pos[ 16]);
11782 digest[2] = hex_to_u64 ((const u8 *) &hash_pos[ 32]);
11783 digest[3] = hex_to_u64 ((const u8 *) &hash_pos[ 48]);
11784 digest[4] = hex_to_u64 ((const u8 *) &hash_pos[ 64]);
11785 digest[5] = hex_to_u64 ((const u8 *) &hash_pos[ 80]);
11786 digest[6] = hex_to_u64 ((const u8 *) &hash_pos[ 96]);
11787 digest[7] = hex_to_u64 ((const u8 *) &hash_pos[112]);
11788
11789 digest[0] -= SHA512M_A;
11790 digest[1] -= SHA512M_B;
11791 digest[2] -= SHA512M_C;
11792 digest[3] -= SHA512M_D;
11793 digest[4] -= SHA512M_E;
11794 digest[5] -= SHA512M_F;
11795 digest[6] -= SHA512M_G;
11796 digest[7] -= SHA512M_H;
11797
11798 return (PARSER_OK);
11799 }
11800
11801 int oracleh_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11802 {
11803 if (data.opts_type & OPTS_TYPE_ST_HEX)
11804 {
11805 if ((input_len < DISPLAY_LEN_MIN_3100H) || (input_len > DISPLAY_LEN_MAX_3100H)) return (PARSER_GLOBAL_LENGTH);
11806 }
11807 else
11808 {
11809 if ((input_len < DISPLAY_LEN_MIN_3100) || (input_len > DISPLAY_LEN_MAX_3100)) return (PARSER_GLOBAL_LENGTH);
11810 }
11811
11812 u32 *digest = (u32 *) hash_buf->digest;
11813
11814 salt_t *salt = hash_buf->salt;
11815
11816 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11817 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11818 digest[2] = 0;
11819 digest[3] = 0;
11820
11821 digest[0] = byte_swap_32 (digest[0]);
11822 digest[1] = byte_swap_32 (digest[1]);
11823
11824 if (input_buf[16] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
11825
11826 uint salt_len = input_len - 16 - 1;
11827
11828 char *salt_buf = input_buf + 16 + 1;
11829
11830 char *salt_buf_ptr = (char *) salt->salt_buf;
11831
11832 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
11833
11834 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11835
11836 salt->salt_len = salt_len;
11837
11838 return (PARSER_OK);
11839 }
11840
11841 int oracles_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11842 {
11843 if ((input_len < DISPLAY_LEN_MIN_112) || (input_len > DISPLAY_LEN_MAX_112)) return (PARSER_GLOBAL_LENGTH);
11844
11845 u32 *digest = (u32 *) hash_buf->digest;
11846
11847 salt_t *salt = hash_buf->salt;
11848
11849 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11850 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11851 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11852 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11853 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
11854
11855 digest[0] -= SHA1M_A;
11856 digest[1] -= SHA1M_B;
11857 digest[2] -= SHA1M_C;
11858 digest[3] -= SHA1M_D;
11859 digest[4] -= SHA1M_E;
11860
11861 if (input_buf[40] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
11862
11863 uint salt_len = input_len - 40 - 1;
11864
11865 char *salt_buf = input_buf + 40 + 1;
11866
11867 char *salt_buf_ptr = (char *) salt->salt_buf;
11868
11869 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
11870
11871 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11872
11873 salt->salt_len = salt_len;
11874
11875 return (PARSER_OK);
11876 }
11877
11878 int oraclet_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11879 {
11880 if ((input_len < DISPLAY_LEN_MIN_12300) || (input_len > DISPLAY_LEN_MAX_12300)) return (PARSER_GLOBAL_LENGTH);
11881
11882 u32 *digest = (u32 *) hash_buf->digest;
11883
11884 salt_t *salt = hash_buf->salt;
11885
11886 char *hash_pos = input_buf;
11887
11888 digest[ 0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
11889 digest[ 1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
11890 digest[ 2] = hex_to_u32 ((const u8 *) &hash_pos[ 16]);
11891 digest[ 3] = hex_to_u32 ((const u8 *) &hash_pos[ 24]);
11892 digest[ 4] = hex_to_u32 ((const u8 *) &hash_pos[ 32]);
11893 digest[ 5] = hex_to_u32 ((const u8 *) &hash_pos[ 40]);
11894 digest[ 6] = hex_to_u32 ((const u8 *) &hash_pos[ 48]);
11895 digest[ 7] = hex_to_u32 ((const u8 *) &hash_pos[ 56]);
11896 digest[ 8] = hex_to_u32 ((const u8 *) &hash_pos[ 64]);
11897 digest[ 9] = hex_to_u32 ((const u8 *) &hash_pos[ 72]);
11898 digest[10] = hex_to_u32 ((const u8 *) &hash_pos[ 80]);
11899 digest[11] = hex_to_u32 ((const u8 *) &hash_pos[ 88]);
11900 digest[12] = hex_to_u32 ((const u8 *) &hash_pos[ 96]);
11901 digest[13] = hex_to_u32 ((const u8 *) &hash_pos[104]);
11902 digest[14] = hex_to_u32 ((const u8 *) &hash_pos[112]);
11903 digest[15] = hex_to_u32 ((const u8 *) &hash_pos[120]);
11904
11905 char *salt_pos = input_buf + 128;
11906
11907 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_pos[ 0]);
11908 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_pos[ 8]);
11909 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &salt_pos[16]);
11910 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &salt_pos[24]);
11911
11912 salt->salt_iter = ROUNDS_ORACLET - 1;
11913 salt->salt_len = 16;
11914
11915 return (PARSER_OK);
11916 }
11917
11918 int sha256_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11919 {
11920 if ((input_len < DISPLAY_LEN_MIN_1400) || (input_len > DISPLAY_LEN_MAX_1400)) return (PARSER_GLOBAL_LENGTH);
11921
11922 u32 *digest = (u32 *) hash_buf->digest;
11923
11924 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11925 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11926 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11927 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11928 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
11929 digest[5] = hex_to_u32 ((const u8 *) &input_buf[40]);
11930 digest[6] = hex_to_u32 ((const u8 *) &input_buf[48]);
11931 digest[7] = hex_to_u32 ((const u8 *) &input_buf[56]);
11932
11933 digest[0] -= SHA256M_A;
11934 digest[1] -= SHA256M_B;
11935 digest[2] -= SHA256M_C;
11936 digest[3] -= SHA256M_D;
11937 digest[4] -= SHA256M_E;
11938 digest[5] -= SHA256M_F;
11939 digest[6] -= SHA256M_G;
11940 digest[7] -= SHA256M_H;
11941
11942 return (PARSER_OK);
11943 }
11944
11945 int sha256s_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11946 {
11947 if (data.opts_type & OPTS_TYPE_ST_HEX)
11948 {
11949 if ((input_len < DISPLAY_LEN_MIN_1410H) || (input_len > DISPLAY_LEN_MAX_1410H)) return (PARSER_GLOBAL_LENGTH);
11950 }
11951 else
11952 {
11953 if ((input_len < DISPLAY_LEN_MIN_1410) || (input_len > DISPLAY_LEN_MAX_1410)) return (PARSER_GLOBAL_LENGTH);
11954 }
11955
11956 u32 *digest = (u32 *) hash_buf->digest;
11957
11958 salt_t *salt = hash_buf->salt;
11959
11960 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
11961 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
11962 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
11963 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
11964 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
11965 digest[5] = hex_to_u32 ((const u8 *) &input_buf[40]);
11966 digest[6] = hex_to_u32 ((const u8 *) &input_buf[48]);
11967 digest[7] = hex_to_u32 ((const u8 *) &input_buf[56]);
11968
11969 digest[0] -= SHA256M_A;
11970 digest[1] -= SHA256M_B;
11971 digest[2] -= SHA256M_C;
11972 digest[3] -= SHA256M_D;
11973 digest[4] -= SHA256M_E;
11974 digest[5] -= SHA256M_F;
11975 digest[6] -= SHA256M_G;
11976 digest[7] -= SHA256M_H;
11977
11978 if (input_buf[64] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
11979
11980 uint salt_len = input_len - 64 - 1;
11981
11982 char *salt_buf = input_buf + 64 + 1;
11983
11984 char *salt_buf_ptr = (char *) salt->salt_buf;
11985
11986 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
11987
11988 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
11989
11990 salt->salt_len = salt_len;
11991
11992 return (PARSER_OK);
11993 }
11994
11995 int sha384_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
11996 {
11997 if ((input_len < DISPLAY_LEN_MIN_10800) || (input_len > DISPLAY_LEN_MAX_10800)) return (PARSER_GLOBAL_LENGTH);
11998
11999 u64 *digest = (u64 *) hash_buf->digest;
12000
12001 digest[0] = hex_to_u64 ((const u8 *) &input_buf[ 0]);
12002 digest[1] = hex_to_u64 ((const u8 *) &input_buf[ 16]);
12003 digest[2] = hex_to_u64 ((const u8 *) &input_buf[ 32]);
12004 digest[3] = hex_to_u64 ((const u8 *) &input_buf[ 48]);
12005 digest[4] = hex_to_u64 ((const u8 *) &input_buf[ 64]);
12006 digest[5] = hex_to_u64 ((const u8 *) &input_buf[ 80]);
12007 digest[6] = 0;
12008 digest[7] = 0;
12009
12010 digest[0] -= SHA384M_A;
12011 digest[1] -= SHA384M_B;
12012 digest[2] -= SHA384M_C;
12013 digest[3] -= SHA384M_D;
12014 digest[4] -= SHA384M_E;
12015 digest[5] -= SHA384M_F;
12016 digest[6] -= 0;
12017 digest[7] -= 0;
12018
12019 return (PARSER_OK);
12020 }
12021
12022 int sha512_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12023 {
12024 if ((input_len < DISPLAY_LEN_MIN_1700) || (input_len > DISPLAY_LEN_MAX_1700)) return (PARSER_GLOBAL_LENGTH);
12025
12026 u64 *digest = (u64 *) hash_buf->digest;
12027
12028 digest[0] = hex_to_u64 ((const u8 *) &input_buf[ 0]);
12029 digest[1] = hex_to_u64 ((const u8 *) &input_buf[ 16]);
12030 digest[2] = hex_to_u64 ((const u8 *) &input_buf[ 32]);
12031 digest[3] = hex_to_u64 ((const u8 *) &input_buf[ 48]);
12032 digest[4] = hex_to_u64 ((const u8 *) &input_buf[ 64]);
12033 digest[5] = hex_to_u64 ((const u8 *) &input_buf[ 80]);
12034 digest[6] = hex_to_u64 ((const u8 *) &input_buf[ 96]);
12035 digest[7] = hex_to_u64 ((const u8 *) &input_buf[112]);
12036
12037 digest[0] -= SHA512M_A;
12038 digest[1] -= SHA512M_B;
12039 digest[2] -= SHA512M_C;
12040 digest[3] -= SHA512M_D;
12041 digest[4] -= SHA512M_E;
12042 digest[5] -= SHA512M_F;
12043 digest[6] -= SHA512M_G;
12044 digest[7] -= SHA512M_H;
12045
12046 return (PARSER_OK);
12047 }
12048
12049 int sha512s_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12050 {
12051 if (data.opts_type & OPTS_TYPE_ST_HEX)
12052 {
12053 if ((input_len < DISPLAY_LEN_MIN_1710H) || (input_len > DISPLAY_LEN_MAX_1710H)) return (PARSER_GLOBAL_LENGTH);
12054 }
12055 else
12056 {
12057 if ((input_len < DISPLAY_LEN_MIN_1710) || (input_len > DISPLAY_LEN_MAX_1710)) return (PARSER_GLOBAL_LENGTH);
12058 }
12059
12060 u64 *digest = (u64 *) hash_buf->digest;
12061
12062 salt_t *salt = hash_buf->salt;
12063
12064 digest[0] = hex_to_u64 ((const u8 *) &input_buf[ 0]);
12065 digest[1] = hex_to_u64 ((const u8 *) &input_buf[ 16]);
12066 digest[2] = hex_to_u64 ((const u8 *) &input_buf[ 32]);
12067 digest[3] = hex_to_u64 ((const u8 *) &input_buf[ 48]);
12068 digest[4] = hex_to_u64 ((const u8 *) &input_buf[ 64]);
12069 digest[5] = hex_to_u64 ((const u8 *) &input_buf[ 80]);
12070 digest[6] = hex_to_u64 ((const u8 *) &input_buf[ 96]);
12071 digest[7] = hex_to_u64 ((const u8 *) &input_buf[112]);
12072
12073 digest[0] -= SHA512M_A;
12074 digest[1] -= SHA512M_B;
12075 digest[2] -= SHA512M_C;
12076 digest[3] -= SHA512M_D;
12077 digest[4] -= SHA512M_E;
12078 digest[5] -= SHA512M_F;
12079 digest[6] -= SHA512M_G;
12080 digest[7] -= SHA512M_H;
12081
12082 if (input_buf[128] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
12083
12084 uint salt_len = input_len - 128 - 1;
12085
12086 char *salt_buf = input_buf + 128 + 1;
12087
12088 char *salt_buf_ptr = (char *) salt->salt_buf;
12089
12090 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
12091
12092 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
12093
12094 salt->salt_len = salt_len;
12095
12096 return (PARSER_OK);
12097 }
12098
12099 int sha512crypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12100 {
12101 if (memcmp (SIGNATURE_SHA512CRYPT, input_buf, 3)) return (PARSER_SIGNATURE_UNMATCHED);
12102
12103 u64 *digest = (u64 *) hash_buf->digest;
12104
12105 salt_t *salt = hash_buf->salt;
12106
12107 char *salt_pos = input_buf + 3;
12108
12109 uint iterations_len = 0;
12110
12111 if (memcmp (salt_pos, "rounds=", 7) == 0)
12112 {
12113 salt_pos += 7;
12114
12115 for (iterations_len = 0; salt_pos[0] >= '0' && salt_pos[0] <= '9' && iterations_len < 7; iterations_len++, salt_pos += 1) continue;
12116
12117 if (iterations_len == 0 ) return (PARSER_SALT_ITERATION);
12118 if (salt_pos[0] != '$') return (PARSER_SIGNATURE_UNMATCHED);
12119
12120 salt_pos[0] = 0x0;
12121
12122 salt->salt_iter = atoi (salt_pos - iterations_len);
12123
12124 salt_pos += 1;
12125
12126 iterations_len += 8;
12127 }
12128 else
12129 {
12130 salt->salt_iter = ROUNDS_SHA512CRYPT;
12131 }
12132
12133 if ((input_len < DISPLAY_LEN_MIN_1800) || (input_len > DISPLAY_LEN_MAX_1800 + iterations_len)) return (PARSER_GLOBAL_LENGTH);
12134
12135 char *hash_pos = strchr (salt_pos, '$');
12136
12137 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12138
12139 uint salt_len = hash_pos - salt_pos;
12140
12141 if (salt_len > 16) return (PARSER_SALT_LENGTH);
12142
12143 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
12144
12145 salt->salt_len = salt_len;
12146
12147 hash_pos++;
12148
12149 sha512crypt_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
12150
12151 return (PARSER_OK);
12152 }
12153
12154 int keccak_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12155 {
12156 if ((input_len < DISPLAY_LEN_MIN_5000) || (input_len > DISPLAY_LEN_MAX_5000)) return (PARSER_GLOBAL_LENGTH);
12157
12158 if (input_len % 16) return (PARSER_GLOBAL_LENGTH);
12159
12160 u64 *digest = (u64 *) hash_buf->digest;
12161
12162 salt_t *salt = hash_buf->salt;
12163
12164 uint keccak_mdlen = input_len / 2;
12165
12166 for (uint i = 0; i < keccak_mdlen / 8; i++)
12167 {
12168 digest[i] = hex_to_u64 ((const u8 *) &input_buf[i * 16]);
12169
12170 digest[i] = byte_swap_64 (digest[i]);
12171 }
12172
12173 salt->keccak_mdlen = keccak_mdlen;
12174
12175 return (PARSER_OK);
12176 }
12177
12178 int ikepsk_md5_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12179 {
12180 if ((input_len < DISPLAY_LEN_MIN_5300) || (input_len > DISPLAY_LEN_MAX_5300)) return (PARSER_GLOBAL_LENGTH);
12181
12182 u32 *digest = (u32 *) hash_buf->digest;
12183
12184 salt_t *salt = hash_buf->salt;
12185
12186 ikepsk_t *ikepsk = (ikepsk_t *) hash_buf->esalt;
12187
12188 /**
12189 * Parse that strange long line
12190 */
12191
12192 char *in_off[9];
12193
12194 size_t in_len[9] = { 0 };
12195
12196 in_off[0] = strtok (input_buf, ":");
12197
12198 in_len[0] = strlen (in_off[0]);
12199
12200 size_t i;
12201
12202 for (i = 1; i < 9; i++)
12203 {
12204 in_off[i] = strtok (NULL, ":");
12205
12206 if (in_off[i] == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12207
12208 in_len[i] = strlen (in_off[i]);
12209 }
12210
12211 char *ptr = (char *) ikepsk->msg_buf;
12212
12213 for (i = 0; i < in_len[0]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[0] + i);
12214 for (i = 0; i < in_len[1]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[1] + i);
12215 for (i = 0; i < in_len[2]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[2] + i);
12216 for (i = 0; i < in_len[3]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[3] + i);
12217 for (i = 0; i < in_len[4]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[4] + i);
12218 for (i = 0; i < in_len[5]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[5] + i);
12219
12220 *ptr = 0x80;
12221
12222 ikepsk->msg_len = (in_len[0] + in_len[1] + in_len[2] + in_len[3] + in_len[4] + in_len[5]) / 2;
12223
12224 ptr = (char *) ikepsk->nr_buf;
12225
12226 for (i = 0; i < in_len[6]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[6] + i);
12227 for (i = 0; i < in_len[7]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[7] + i);
12228
12229 *ptr = 0x80;
12230
12231 ikepsk->nr_len = (in_len[6] + in_len[7]) / 2;
12232
12233 /**
12234 * Store to database
12235 */
12236
12237 ptr = in_off[8];
12238
12239 digest[0] = hex_to_u32 ((const u8 *) &ptr[ 0]);
12240 digest[1] = hex_to_u32 ((const u8 *) &ptr[ 8]);
12241 digest[2] = hex_to_u32 ((const u8 *) &ptr[16]);
12242 digest[3] = hex_to_u32 ((const u8 *) &ptr[24]);
12243
12244 digest[0] = byte_swap_32 (digest[0]);
12245 digest[1] = byte_swap_32 (digest[1]);
12246 digest[2] = byte_swap_32 (digest[2]);
12247 digest[3] = byte_swap_32 (digest[3]);
12248
12249 salt->salt_len = 32;
12250
12251 salt->salt_buf[0] = ikepsk->nr_buf[0];
12252 salt->salt_buf[1] = ikepsk->nr_buf[1];
12253 salt->salt_buf[2] = ikepsk->nr_buf[2];
12254 salt->salt_buf[3] = ikepsk->nr_buf[3];
12255 salt->salt_buf[4] = ikepsk->nr_buf[4];
12256 salt->salt_buf[5] = ikepsk->nr_buf[5];
12257 salt->salt_buf[6] = ikepsk->nr_buf[6];
12258 salt->salt_buf[7] = ikepsk->nr_buf[7];
12259
12260 return (PARSER_OK);
12261 }
12262
12263 int ikepsk_sha1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12264 {
12265 if ((input_len < DISPLAY_LEN_MIN_5400) || (input_len > DISPLAY_LEN_MAX_5400)) return (PARSER_GLOBAL_LENGTH);
12266
12267 u32 *digest = (u32 *) hash_buf->digest;
12268
12269 salt_t *salt = hash_buf->salt;
12270
12271 ikepsk_t *ikepsk = (ikepsk_t *) hash_buf->esalt;
12272
12273 /**
12274 * Parse that strange long line
12275 */
12276
12277 char *in_off[9];
12278
12279 size_t in_len[9] = { 0 };
12280
12281 in_off[0] = strtok (input_buf, ":");
12282
12283 in_len[0] = strlen (in_off[0]);
12284
12285 size_t i;
12286
12287 for (i = 1; i < 9; i++)
12288 {
12289 in_off[i] = strtok (NULL, ":");
12290
12291 if (in_off[i] == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12292
12293 in_len[i] = strlen (in_off[i]);
12294 }
12295
12296 char *ptr = (char *) ikepsk->msg_buf;
12297
12298 for (i = 0; i < in_len[0]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[0] + i);
12299 for (i = 0; i < in_len[1]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[1] + i);
12300 for (i = 0; i < in_len[2]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[2] + i);
12301 for (i = 0; i < in_len[3]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[3] + i);
12302 for (i = 0; i < in_len[4]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[4] + i);
12303 for (i = 0; i < in_len[5]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[5] + i);
12304
12305 *ptr = 0x80;
12306
12307 ikepsk->msg_len = (in_len[0] + in_len[1] + in_len[2] + in_len[3] + in_len[4] + in_len[5]) / 2;
12308
12309 ptr = (char *) ikepsk->nr_buf;
12310
12311 for (i = 0; i < in_len[6]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[6] + i);
12312 for (i = 0; i < in_len[7]; i += 2) *ptr++ = hex_to_u8 ((const u8 *) in_off[7] + i);
12313
12314 *ptr = 0x80;
12315
12316 ikepsk->nr_len = (in_len[6] + in_len[7]) / 2;
12317
12318 /**
12319 * Store to database
12320 */
12321
12322 ptr = in_off[8];
12323
12324 digest[0] = hex_to_u32 ((const u8 *) &ptr[ 0]);
12325 digest[1] = hex_to_u32 ((const u8 *) &ptr[ 8]);
12326 digest[2] = hex_to_u32 ((const u8 *) &ptr[16]);
12327 digest[3] = hex_to_u32 ((const u8 *) &ptr[24]);
12328 digest[4] = hex_to_u32 ((const u8 *) &ptr[32]);
12329
12330 salt->salt_len = 32;
12331
12332 salt->salt_buf[0] = ikepsk->nr_buf[0];
12333 salt->salt_buf[1] = ikepsk->nr_buf[1];
12334 salt->salt_buf[2] = ikepsk->nr_buf[2];
12335 salt->salt_buf[3] = ikepsk->nr_buf[3];
12336 salt->salt_buf[4] = ikepsk->nr_buf[4];
12337 salt->salt_buf[5] = ikepsk->nr_buf[5];
12338 salt->salt_buf[6] = ikepsk->nr_buf[6];
12339 salt->salt_buf[7] = ikepsk->nr_buf[7];
12340
12341 return (PARSER_OK);
12342 }
12343
12344 int ripemd160_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12345 {
12346 if ((input_len < DISPLAY_LEN_MIN_6000) || (input_len > DISPLAY_LEN_MAX_6000)) return (PARSER_GLOBAL_LENGTH);
12347
12348 u32 *digest = (u32 *) hash_buf->digest;
12349
12350 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
12351 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
12352 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
12353 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
12354 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
12355
12356 digest[0] = byte_swap_32 (digest[0]);
12357 digest[1] = byte_swap_32 (digest[1]);
12358 digest[2] = byte_swap_32 (digest[2]);
12359 digest[3] = byte_swap_32 (digest[3]);
12360 digest[4] = byte_swap_32 (digest[4]);
12361
12362 return (PARSER_OK);
12363 }
12364
12365 int whirlpool_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12366 {
12367 if ((input_len < DISPLAY_LEN_MIN_6100) || (input_len > DISPLAY_LEN_MAX_6100)) return (PARSER_GLOBAL_LENGTH);
12368
12369 u32 *digest = (u32 *) hash_buf->digest;
12370
12371 digest[ 0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
12372 digest[ 1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
12373 digest[ 2] = hex_to_u32 ((const u8 *) &input_buf[ 16]);
12374 digest[ 3] = hex_to_u32 ((const u8 *) &input_buf[ 24]);
12375 digest[ 4] = hex_to_u32 ((const u8 *) &input_buf[ 32]);
12376 digest[ 5] = hex_to_u32 ((const u8 *) &input_buf[ 40]);
12377 digest[ 6] = hex_to_u32 ((const u8 *) &input_buf[ 48]);
12378 digest[ 7] = hex_to_u32 ((const u8 *) &input_buf[ 56]);
12379 digest[ 8] = hex_to_u32 ((const u8 *) &input_buf[ 64]);
12380 digest[ 9] = hex_to_u32 ((const u8 *) &input_buf[ 72]);
12381 digest[10] = hex_to_u32 ((const u8 *) &input_buf[ 80]);
12382 digest[11] = hex_to_u32 ((const u8 *) &input_buf[ 88]);
12383 digest[12] = hex_to_u32 ((const u8 *) &input_buf[ 96]);
12384 digest[13] = hex_to_u32 ((const u8 *) &input_buf[104]);
12385 digest[14] = hex_to_u32 ((const u8 *) &input_buf[112]);
12386 digest[15] = hex_to_u32 ((const u8 *) &input_buf[120]);
12387
12388 return (PARSER_OK);
12389 }
12390
12391 int androidpin_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12392 {
12393 if ((input_len < DISPLAY_LEN_MIN_5800) || (input_len > DISPLAY_LEN_MAX_5800)) return (PARSER_GLOBAL_LENGTH);
12394
12395 u32 *digest = (u32 *) hash_buf->digest;
12396
12397 salt_t *salt = hash_buf->salt;
12398
12399 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
12400 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
12401 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
12402 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
12403 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
12404
12405 if (input_buf[40] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
12406
12407 uint salt_len = input_len - 40 - 1;
12408
12409 char *salt_buf = input_buf + 40 + 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 salt->salt_iter = ROUNDS_ANDROIDPIN - 1;
12420
12421 return (PARSER_OK);
12422 }
12423
12424 int truecrypt_parse_hash_1k (char *input_buf, uint input_len, hash_t *hash_buf)
12425 {
12426 u32 *digest = (u32 *) hash_buf->digest;
12427
12428 salt_t *salt = hash_buf->salt;
12429
12430 tc_t *tc = (tc_t *) hash_buf->esalt;
12431
12432 if (input_len == 0)
12433 {
12434 log_error ("TrueCrypt container not specified");
12435
12436 exit (-1);
12437 }
12438
12439 FILE *fp = fopen (input_buf, "rb");
12440
12441 if (fp == NULL)
12442 {
12443 log_error ("%s: %s", input_buf, strerror (errno));
12444
12445 exit (-1);
12446 }
12447
12448 char buf[512] = { 0 };
12449
12450 int n = fread (buf, 1, sizeof (buf), fp);
12451
12452 fclose (fp);
12453
12454 if (n != 512) return (PARSER_TC_FILE_SIZE);
12455
12456 memcpy (tc->salt_buf, buf, 64);
12457
12458 memcpy (tc->data_buf, buf + 64, 512 - 64);
12459
12460 salt->salt_buf[0] = tc->salt_buf[0];
12461
12462 salt->salt_len = 4;
12463
12464 salt->salt_iter = 1000 - 1;
12465
12466 digest[0] = tc->data_buf[0];
12467
12468 return (PARSER_OK);
12469 }
12470
12471 int truecrypt_parse_hash_2k (char *input_buf, uint input_len, hash_t *hash_buf)
12472 {
12473 u32 *digest = (u32 *) hash_buf->digest;
12474
12475 salt_t *salt = hash_buf->salt;
12476
12477 tc_t *tc = (tc_t *) hash_buf->esalt;
12478
12479 if (input_len == 0)
12480 {
12481 log_error ("TrueCrypt container not specified");
12482
12483 exit (-1);
12484 }
12485
12486 FILE *fp = fopen (input_buf, "rb");
12487
12488 if (fp == NULL)
12489 {
12490 log_error ("%s: %s", input_buf, strerror (errno));
12491
12492 exit (-1);
12493 }
12494
12495 char buf[512] = { 0 };
12496
12497 int n = fread (buf, 1, sizeof (buf), fp);
12498
12499 fclose (fp);
12500
12501 if (n != 512) return (PARSER_TC_FILE_SIZE);
12502
12503 memcpy (tc->salt_buf, buf, 64);
12504
12505 memcpy (tc->data_buf, buf + 64, 512 - 64);
12506
12507 salt->salt_buf[0] = tc->salt_buf[0];
12508
12509 salt->salt_len = 4;
12510
12511 salt->salt_iter = 2000 - 1;
12512
12513 digest[0] = tc->data_buf[0];
12514
12515 return (PARSER_OK);
12516 }
12517
12518 int md5aix_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12519 {
12520 if ((input_len < DISPLAY_LEN_MIN_6300) || (input_len > DISPLAY_LEN_MAX_6300)) return (PARSER_GLOBAL_LENGTH);
12521
12522 if (memcmp (SIGNATURE_MD5AIX, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
12523
12524 u32 *digest = (u32 *) hash_buf->digest;
12525
12526 salt_t *salt = hash_buf->salt;
12527
12528 char *salt_pos = input_buf + 6;
12529
12530 char *hash_pos = strchr (salt_pos, '$');
12531
12532 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12533
12534 uint salt_len = hash_pos - salt_pos;
12535
12536 if (salt_len < 8) return (PARSER_SALT_LENGTH);
12537
12538 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
12539
12540 salt->salt_len = salt_len;
12541
12542 salt->salt_iter = 1000;
12543
12544 hash_pos++;
12545
12546 md5crypt_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
12547
12548 return (PARSER_OK);
12549 }
12550
12551 int sha1aix_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12552 {
12553 if ((input_len < DISPLAY_LEN_MIN_6700) || (input_len > DISPLAY_LEN_MAX_6700)) return (PARSER_GLOBAL_LENGTH);
12554
12555 if (memcmp (SIGNATURE_SHA1AIX, input_buf, 7)) return (PARSER_SIGNATURE_UNMATCHED);
12556
12557 u32 *digest = (u32 *) hash_buf->digest;
12558
12559 salt_t *salt = hash_buf->salt;
12560
12561 char *iter_pos = input_buf + 7;
12562
12563 char *salt_pos = strchr (iter_pos, '$');
12564
12565 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12566
12567 salt_pos++;
12568
12569 char *hash_pos = strchr (salt_pos, '$');
12570
12571 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12572
12573 uint salt_len = hash_pos - salt_pos;
12574
12575 if (salt_len < 16) return (PARSER_SALT_LENGTH);
12576
12577 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
12578
12579 salt->salt_len = salt_len;
12580
12581 char salt_iter[3] = { iter_pos[0], iter_pos[1], 0 };
12582
12583 salt->salt_sign[0] = atoi (salt_iter);
12584
12585 salt->salt_iter = (1 << atoi (salt_iter)) - 1;
12586
12587 hash_pos++;
12588
12589 sha1aix_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
12590
12591 digest[0] = byte_swap_32 (digest[0]);
12592 digest[1] = byte_swap_32 (digest[1]);
12593 digest[2] = byte_swap_32 (digest[2]);
12594 digest[3] = byte_swap_32 (digest[3]);
12595 digest[4] = byte_swap_32 (digest[4]);
12596
12597 return (PARSER_OK);
12598 }
12599
12600 int sha256aix_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12601 {
12602 if ((input_len < DISPLAY_LEN_MIN_6400) || (input_len > DISPLAY_LEN_MAX_6400)) return (PARSER_GLOBAL_LENGTH);
12603
12604 if (memcmp (SIGNATURE_SHA256AIX, input_buf, 9)) return (PARSER_SIGNATURE_UNMATCHED);
12605
12606 u32 *digest = (u32 *) hash_buf->digest;
12607
12608 salt_t *salt = hash_buf->salt;
12609
12610 char *iter_pos = input_buf + 9;
12611
12612 char *salt_pos = strchr (iter_pos, '$');
12613
12614 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12615
12616 salt_pos++;
12617
12618 char *hash_pos = strchr (salt_pos, '$');
12619
12620 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12621
12622 uint salt_len = hash_pos - salt_pos;
12623
12624 if (salt_len < 16) return (PARSER_SALT_LENGTH);
12625
12626 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
12627
12628 salt->salt_len = salt_len;
12629
12630 char salt_iter[3] = { iter_pos[0], iter_pos[1], 0 };
12631
12632 salt->salt_sign[0] = atoi (salt_iter);
12633
12634 salt->salt_iter = (1 << atoi (salt_iter)) - 1;
12635
12636 hash_pos++;
12637
12638 sha256aix_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
12639
12640 digest[0] = byte_swap_32 (digest[0]);
12641 digest[1] = byte_swap_32 (digest[1]);
12642 digest[2] = byte_swap_32 (digest[2]);
12643 digest[3] = byte_swap_32 (digest[3]);
12644 digest[4] = byte_swap_32 (digest[4]);
12645 digest[5] = byte_swap_32 (digest[5]);
12646 digest[6] = byte_swap_32 (digest[6]);
12647 digest[7] = byte_swap_32 (digest[7]);
12648
12649 return (PARSER_OK);
12650 }
12651
12652 int sha512aix_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12653 {
12654 if ((input_len < DISPLAY_LEN_MIN_6500) || (input_len > DISPLAY_LEN_MAX_6500)) return (PARSER_GLOBAL_LENGTH);
12655
12656 if (memcmp (SIGNATURE_SHA512AIX, input_buf, 9)) return (PARSER_SIGNATURE_UNMATCHED);
12657
12658 u64 *digest = (u64 *) hash_buf->digest;
12659
12660 salt_t *salt = hash_buf->salt;
12661
12662 char *iter_pos = input_buf + 9;
12663
12664 char *salt_pos = strchr (iter_pos, '$');
12665
12666 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12667
12668 salt_pos++;
12669
12670 char *hash_pos = strchr (salt_pos, '$');
12671
12672 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12673
12674 uint salt_len = hash_pos - salt_pos;
12675
12676 if (salt_len < 16) return (PARSER_SALT_LENGTH);
12677
12678 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
12679
12680 salt->salt_len = salt_len;
12681
12682 char salt_iter[3] = { iter_pos[0], iter_pos[1], 0 };
12683
12684 salt->salt_sign[0] = atoi (salt_iter);
12685
12686 salt->salt_iter = (1 << atoi (salt_iter)) - 1;
12687
12688 hash_pos++;
12689
12690 sha512aix_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
12691
12692 digest[0] = byte_swap_64 (digest[0]);
12693 digest[1] = byte_swap_64 (digest[1]);
12694 digest[2] = byte_swap_64 (digest[2]);
12695 digest[3] = byte_swap_64 (digest[3]);
12696 digest[4] = byte_swap_64 (digest[4]);
12697 digest[5] = byte_swap_64 (digest[5]);
12698 digest[6] = byte_swap_64 (digest[6]);
12699 digest[7] = byte_swap_64 (digest[7]);
12700
12701 return (PARSER_OK);
12702 }
12703
12704 int agilekey_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12705 {
12706 if ((input_len < DISPLAY_LEN_MIN_6600) || (input_len > DISPLAY_LEN_MAX_6600)) return (PARSER_GLOBAL_LENGTH);
12707
12708 u32 *digest = (u32 *) hash_buf->digest;
12709
12710 salt_t *salt = hash_buf->salt;
12711
12712 agilekey_t *agilekey = (agilekey_t *) hash_buf->esalt;
12713
12714 /**
12715 * parse line
12716 */
12717
12718 char *iterations_pos = input_buf;
12719
12720 char *saltbuf_pos = strchr (iterations_pos, ':');
12721
12722 if (saltbuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12723
12724 uint iterations_len = saltbuf_pos - iterations_pos;
12725
12726 if (iterations_len > 6) return (PARSER_SALT_LENGTH);
12727
12728 saltbuf_pos++;
12729
12730 char *cipherbuf_pos = strchr (saltbuf_pos, ':');
12731
12732 if (cipherbuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12733
12734 uint saltbuf_len = cipherbuf_pos - saltbuf_pos;
12735
12736 if (saltbuf_len != 16) return (PARSER_SALT_LENGTH);
12737
12738 uint cipherbuf_len = input_len - iterations_len - 1 - saltbuf_len - 1;
12739
12740 if (cipherbuf_len != 2080) return (PARSER_HASH_LENGTH);
12741
12742 cipherbuf_pos++;
12743
12744 /**
12745 * pbkdf2 iterations
12746 */
12747
12748 salt->salt_iter = atoi (iterations_pos) - 1;
12749
12750 /**
12751 * handle salt encoding
12752 */
12753
12754 char *saltbuf_ptr = (char *) salt->salt_buf;
12755
12756 for (uint i = 0; i < saltbuf_len; i += 2)
12757 {
12758 const char p0 = saltbuf_pos[i + 0];
12759 const char p1 = saltbuf_pos[i + 1];
12760
12761 *saltbuf_ptr++ = hex_convert (p1) << 0
12762 | hex_convert (p0) << 4;
12763 }
12764
12765 salt->salt_len = saltbuf_len / 2;
12766
12767 /**
12768 * handle cipher encoding
12769 */
12770
12771 uint *tmp = (uint *) mymalloc (32);
12772
12773 char *cipherbuf_ptr = (char *) tmp;
12774
12775 for (uint i = 2016; i < cipherbuf_len; i += 2)
12776 {
12777 const char p0 = cipherbuf_pos[i + 0];
12778 const char p1 = cipherbuf_pos[i + 1];
12779
12780 *cipherbuf_ptr++ = hex_convert (p1) << 0
12781 | hex_convert (p0) << 4;
12782 }
12783
12784 // iv is stored at salt_buf 4 (length 16)
12785 // data is stored at salt_buf 8 (length 16)
12786
12787 salt->salt_buf[ 4] = byte_swap_32 (tmp[0]);
12788 salt->salt_buf[ 5] = byte_swap_32 (tmp[1]);
12789 salt->salt_buf[ 6] = byte_swap_32 (tmp[2]);
12790 salt->salt_buf[ 7] = byte_swap_32 (tmp[3]);
12791
12792 salt->salt_buf[ 8] = byte_swap_32 (tmp[4]);
12793 salt->salt_buf[ 9] = byte_swap_32 (tmp[5]);
12794 salt->salt_buf[10] = byte_swap_32 (tmp[6]);
12795 salt->salt_buf[11] = byte_swap_32 (tmp[7]);
12796
12797 free (tmp);
12798
12799 for (uint i = 0, j = 0; i < 1040; i += 1, j += 2)
12800 {
12801 const char p0 = cipherbuf_pos[j + 0];
12802 const char p1 = cipherbuf_pos[j + 1];
12803
12804 agilekey->cipher[i] = hex_convert (p1) << 0
12805 | hex_convert (p0) << 4;
12806 }
12807
12808 /**
12809 * digest buf
12810 */
12811
12812 digest[0] = 0x10101010;
12813 digest[1] = 0x10101010;
12814 digest[2] = 0x10101010;
12815 digest[3] = 0x10101010;
12816
12817 return (PARSER_OK);
12818 }
12819
12820 int lastpass_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12821 {
12822 if ((input_len < DISPLAY_LEN_MIN_6800) || (input_len > DISPLAY_LEN_MAX_6800)) return (PARSER_GLOBAL_LENGTH);
12823
12824 u32 *digest = (u32 *) hash_buf->digest;
12825
12826 salt_t *salt = hash_buf->salt;
12827
12828 char *hashbuf_pos = input_buf;
12829
12830 char *iterations_pos = strchr (hashbuf_pos, ':');
12831
12832 if (iterations_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12833
12834 uint hash_len = iterations_pos - hashbuf_pos;
12835
12836 if ((hash_len != 32) && (hash_len != 64)) return (PARSER_HASH_LENGTH);
12837
12838 iterations_pos++;
12839
12840 char *saltbuf_pos = strchr (iterations_pos, ':');
12841
12842 if (saltbuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12843
12844 uint iterations_len = saltbuf_pos - iterations_pos;
12845
12846 saltbuf_pos++;
12847
12848 uint salt_len = input_len - hash_len - 1 - iterations_len - 1;
12849
12850 if (salt_len > 32) return (PARSER_SALT_LENGTH);
12851
12852 char *salt_buf_ptr = (char *) salt->salt_buf;
12853
12854 salt_len = parse_and_store_salt (salt_buf_ptr, saltbuf_pos, salt_len);
12855
12856 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
12857
12858 salt->salt_len = salt_len;
12859
12860 salt->salt_iter = atoi (iterations_pos) - 1;
12861
12862 digest[0] = hex_to_u32 ((const u8 *) &hashbuf_pos[ 0]);
12863 digest[1] = hex_to_u32 ((const u8 *) &hashbuf_pos[ 8]);
12864 digest[2] = hex_to_u32 ((const u8 *) &hashbuf_pos[16]);
12865 digest[3] = hex_to_u32 ((const u8 *) &hashbuf_pos[24]);
12866
12867 return (PARSER_OK);
12868 }
12869
12870 int gost_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12871 {
12872 if ((input_len < DISPLAY_LEN_MIN_6900) || (input_len > DISPLAY_LEN_MAX_6900)) return (PARSER_GLOBAL_LENGTH);
12873
12874 u32 *digest = (u32 *) hash_buf->digest;
12875
12876 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
12877 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
12878 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
12879 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
12880 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
12881 digest[5] = hex_to_u32 ((const u8 *) &input_buf[40]);
12882 digest[6] = hex_to_u32 ((const u8 *) &input_buf[48]);
12883 digest[7] = hex_to_u32 ((const u8 *) &input_buf[56]);
12884
12885 digest[0] = byte_swap_32 (digest[0]);
12886 digest[1] = byte_swap_32 (digest[1]);
12887 digest[2] = byte_swap_32 (digest[2]);
12888 digest[3] = byte_swap_32 (digest[3]);
12889 digest[4] = byte_swap_32 (digest[4]);
12890 digest[5] = byte_swap_32 (digest[5]);
12891 digest[6] = byte_swap_32 (digest[6]);
12892 digest[7] = byte_swap_32 (digest[7]);
12893
12894 return (PARSER_OK);
12895 }
12896
12897 int sha256crypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12898 {
12899 if (memcmp (SIGNATURE_SHA256CRYPT, input_buf, 3)) return (PARSER_SIGNATURE_UNMATCHED);
12900
12901 u32 *digest = (u32 *) hash_buf->digest;
12902
12903 salt_t *salt = hash_buf->salt;
12904
12905 char *salt_pos = input_buf + 3;
12906
12907 uint iterations_len = 0;
12908
12909 if (memcmp (salt_pos, "rounds=", 7) == 0)
12910 {
12911 salt_pos += 7;
12912
12913 for (iterations_len = 0; salt_pos[0] >= '0' && salt_pos[0] <= '9' && iterations_len < 7; iterations_len++, salt_pos += 1) continue;
12914
12915 if (iterations_len == 0 ) return (PARSER_SALT_ITERATION);
12916 if (salt_pos[0] != '$') return (PARSER_SIGNATURE_UNMATCHED);
12917
12918 salt_pos[0] = 0x0;
12919
12920 salt->salt_iter = atoi (salt_pos - iterations_len);
12921
12922 salt_pos += 1;
12923
12924 iterations_len += 8;
12925 }
12926 else
12927 {
12928 salt->salt_iter = ROUNDS_SHA256CRYPT;
12929 }
12930
12931 if ((input_len < DISPLAY_LEN_MIN_7400) || (input_len > DISPLAY_LEN_MAX_7400 + iterations_len)) return (PARSER_GLOBAL_LENGTH);
12932
12933 char *hash_pos = strchr (salt_pos, '$');
12934
12935 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12936
12937 uint salt_len = hash_pos - salt_pos;
12938
12939 if (salt_len > 16) return (PARSER_SALT_LENGTH);
12940
12941 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
12942
12943 salt->salt_len = salt_len;
12944
12945 hash_pos++;
12946
12947 sha256crypt_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
12948
12949 return (PARSER_OK);
12950 }
12951
12952 int sha512osx_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
12953 {
12954 uint max_len = DISPLAY_LEN_MAX_7100 + (2 * 128);
12955
12956 if ((input_len < DISPLAY_LEN_MIN_7100) || (input_len > max_len)) return (PARSER_GLOBAL_LENGTH);
12957
12958 if (memcmp (SIGNATURE_SHA512OSX, input_buf, 4)) return (PARSER_SIGNATURE_UNMATCHED);
12959
12960 u64 *digest = (u64 *) hash_buf->digest;
12961
12962 salt_t *salt = hash_buf->salt;
12963
12964 pbkdf2_sha512_t *pbkdf2_sha512 = (pbkdf2_sha512_t *) hash_buf->esalt;
12965
12966 char *iter_pos = input_buf + 4;
12967
12968 char *salt_pos = strchr (iter_pos, '$');
12969
12970 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12971
12972 salt_pos++;
12973
12974 char *hash_pos = strchr (salt_pos, '$');
12975
12976 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
12977
12978 if (((input_len - (hash_pos - input_buf) - 1) % 128) != 0) return (PARSER_GLOBAL_LENGTH);
12979
12980 hash_pos++;
12981
12982 digest[0] = hex_to_u64 ((const u8 *) &hash_pos[ 0]);
12983 digest[1] = hex_to_u64 ((const u8 *) &hash_pos[ 16]);
12984 digest[2] = hex_to_u64 ((const u8 *) &hash_pos[ 32]);
12985 digest[3] = hex_to_u64 ((const u8 *) &hash_pos[ 48]);
12986 digest[4] = hex_to_u64 ((const u8 *) &hash_pos[ 64]);
12987 digest[5] = hex_to_u64 ((const u8 *) &hash_pos[ 80]);
12988 digest[6] = hex_to_u64 ((const u8 *) &hash_pos[ 96]);
12989 digest[7] = hex_to_u64 ((const u8 *) &hash_pos[112]);
12990
12991 uint salt_len = hash_pos - salt_pos - 1;
12992
12993 if ((salt_len % 2) != 0) return (PARSER_SALT_LENGTH);
12994
12995 salt->salt_len = salt_len / 2;
12996
12997 pbkdf2_sha512->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_pos[ 0]);
12998 pbkdf2_sha512->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_pos[ 8]);
12999 pbkdf2_sha512->salt_buf[2] = hex_to_u32 ((const u8 *) &salt_pos[16]);
13000 pbkdf2_sha512->salt_buf[3] = hex_to_u32 ((const u8 *) &salt_pos[24]);
13001 pbkdf2_sha512->salt_buf[4] = hex_to_u32 ((const u8 *) &salt_pos[32]);
13002 pbkdf2_sha512->salt_buf[5] = hex_to_u32 ((const u8 *) &salt_pos[40]);
13003 pbkdf2_sha512->salt_buf[6] = hex_to_u32 ((const u8 *) &salt_pos[48]);
13004 pbkdf2_sha512->salt_buf[7] = hex_to_u32 ((const u8 *) &salt_pos[56]);
13005
13006 pbkdf2_sha512->salt_buf[0] = byte_swap_32 (pbkdf2_sha512->salt_buf[0]);
13007 pbkdf2_sha512->salt_buf[1] = byte_swap_32 (pbkdf2_sha512->salt_buf[1]);
13008 pbkdf2_sha512->salt_buf[2] = byte_swap_32 (pbkdf2_sha512->salt_buf[2]);
13009 pbkdf2_sha512->salt_buf[3] = byte_swap_32 (pbkdf2_sha512->salt_buf[3]);
13010 pbkdf2_sha512->salt_buf[4] = byte_swap_32 (pbkdf2_sha512->salt_buf[4]);
13011 pbkdf2_sha512->salt_buf[5] = byte_swap_32 (pbkdf2_sha512->salt_buf[5]);
13012 pbkdf2_sha512->salt_buf[6] = byte_swap_32 (pbkdf2_sha512->salt_buf[6]);
13013 pbkdf2_sha512->salt_buf[7] = byte_swap_32 (pbkdf2_sha512->salt_buf[7]);
13014 pbkdf2_sha512->salt_buf[8] = 0x01000000;
13015 pbkdf2_sha512->salt_buf[9] = 0x80;
13016
13017 salt->salt_buf[0] = pbkdf2_sha512->salt_buf[0];
13018
13019 salt->salt_iter = atoi (iter_pos) - 1;
13020
13021 return (PARSER_OK);
13022 }
13023
13024 int episerver4_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13025 {
13026 if ((input_len < DISPLAY_LEN_MIN_1441) || (input_len > DISPLAY_LEN_MAX_1441)) return (PARSER_GLOBAL_LENGTH);
13027
13028 if (memcmp (SIGNATURE_EPISERVER4, input_buf, 14)) return (PARSER_SIGNATURE_UNMATCHED);
13029
13030 u32 *digest = (u32 *) hash_buf->digest;
13031
13032 salt_t *salt = hash_buf->salt;
13033
13034 char *salt_pos = input_buf + 14;
13035
13036 char *hash_pos = strchr (salt_pos, '*');
13037
13038 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13039
13040 hash_pos++;
13041
13042 uint salt_len = hash_pos - salt_pos - 1;
13043
13044 char *salt_buf_ptr = (char *) salt->salt_buf;
13045
13046 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
13047
13048 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
13049
13050 salt->salt_len = salt_len;
13051
13052 u8 tmp_buf[100] = { 0 };
13053
13054 base64_decode (base64_to_int, (const u8 *) hash_pos, 43, tmp_buf);
13055
13056 memcpy (digest, tmp_buf, 32);
13057
13058 digest[0] = byte_swap_32 (digest[0]);
13059 digest[1] = byte_swap_32 (digest[1]);
13060 digest[2] = byte_swap_32 (digest[2]);
13061 digest[3] = byte_swap_32 (digest[3]);
13062 digest[4] = byte_swap_32 (digest[4]);
13063 digest[5] = byte_swap_32 (digest[5]);
13064 digest[6] = byte_swap_32 (digest[6]);
13065 digest[7] = byte_swap_32 (digest[7]);
13066
13067 digest[0] -= SHA256M_A;
13068 digest[1] -= SHA256M_B;
13069 digest[2] -= SHA256M_C;
13070 digest[3] -= SHA256M_D;
13071 digest[4] -= SHA256M_E;
13072 digest[5] -= SHA256M_F;
13073 digest[6] -= SHA256M_G;
13074 digest[7] -= SHA256M_H;
13075
13076 return (PARSER_OK);
13077 }
13078
13079 int sha512grub_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13080 {
13081 uint max_len = DISPLAY_LEN_MAX_7200 + (8 * 128);
13082
13083 if ((input_len < DISPLAY_LEN_MIN_7200) || (input_len > max_len)) return (PARSER_GLOBAL_LENGTH);
13084
13085 if (memcmp (SIGNATURE_SHA512GRUB, input_buf, 19)) return (PARSER_SIGNATURE_UNMATCHED);
13086
13087 u64 *digest = (u64 *) hash_buf->digest;
13088
13089 salt_t *salt = hash_buf->salt;
13090
13091 pbkdf2_sha512_t *pbkdf2_sha512 = (pbkdf2_sha512_t *) hash_buf->esalt;
13092
13093 char *iter_pos = input_buf + 19;
13094
13095 char *salt_pos = strchr (iter_pos, '.');
13096
13097 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13098
13099 salt_pos++;
13100
13101 char *hash_pos = strchr (salt_pos, '.');
13102
13103 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13104
13105 if (((input_len - (hash_pos - input_buf) - 1) % 128) != 0) return (PARSER_GLOBAL_LENGTH);
13106
13107 hash_pos++;
13108
13109 digest[0] = hex_to_u64 ((const u8 *) &hash_pos[ 0]);
13110 digest[1] = hex_to_u64 ((const u8 *) &hash_pos[ 16]);
13111 digest[2] = hex_to_u64 ((const u8 *) &hash_pos[ 32]);
13112 digest[3] = hex_to_u64 ((const u8 *) &hash_pos[ 48]);
13113 digest[4] = hex_to_u64 ((const u8 *) &hash_pos[ 64]);
13114 digest[5] = hex_to_u64 ((const u8 *) &hash_pos[ 80]);
13115 digest[6] = hex_to_u64 ((const u8 *) &hash_pos[ 96]);
13116 digest[7] = hex_to_u64 ((const u8 *) &hash_pos[112]);
13117
13118 uint salt_len = hash_pos - salt_pos - 1;
13119
13120 salt_len /= 2;
13121
13122 char *salt_buf_ptr = (char *) pbkdf2_sha512->salt_buf;
13123
13124 uint i;
13125
13126 for (i = 0; i < salt_len; i++)
13127 {
13128 salt_buf_ptr[i] = hex_to_u8 ((const u8 *) &salt_pos[i * 2]);
13129 }
13130
13131 salt_buf_ptr[salt_len + 3] = 0x01;
13132 salt_buf_ptr[salt_len + 4] = 0x80;
13133
13134 salt->salt_buf[0] = pbkdf2_sha512->salt_buf[0];
13135
13136 salt->salt_len = salt_len;
13137
13138 salt->salt_iter = atoi (iter_pos) - 1;
13139
13140 return (PARSER_OK);
13141 }
13142
13143 int sha512b64s_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13144 {
13145 if ((input_len < DISPLAY_LEN_MIN_1711) || (input_len > DISPLAY_LEN_MAX_1711)) return (PARSER_GLOBAL_LENGTH);
13146
13147 if (memcmp (SIGNATURE_SHA512B64S, input_buf, 9)) return (PARSER_SIGNATURE_UNMATCHED);
13148
13149 u64 *digest = (u64 *) hash_buf->digest;
13150
13151 salt_t *salt = hash_buf->salt;
13152
13153 u8 tmp_buf[120] = { 0 };
13154
13155 int tmp_len = base64_decode (base64_to_int, (const u8 *) input_buf + 9, input_len - 9, tmp_buf);
13156
13157 if (tmp_len < 64) return (PARSER_HASH_LENGTH);
13158
13159 memcpy (digest, tmp_buf, 64);
13160
13161 digest[0] = byte_swap_64 (digest[0]);
13162 digest[1] = byte_swap_64 (digest[1]);
13163 digest[2] = byte_swap_64 (digest[2]);
13164 digest[3] = byte_swap_64 (digest[3]);
13165 digest[4] = byte_swap_64 (digest[4]);
13166 digest[5] = byte_swap_64 (digest[5]);
13167 digest[6] = byte_swap_64 (digest[6]);
13168 digest[7] = byte_swap_64 (digest[7]);
13169
13170 digest[0] -= SHA512M_A;
13171 digest[1] -= SHA512M_B;
13172 digest[2] -= SHA512M_C;
13173 digest[3] -= SHA512M_D;
13174 digest[4] -= SHA512M_E;
13175 digest[5] -= SHA512M_F;
13176 digest[6] -= SHA512M_G;
13177 digest[7] -= SHA512M_H;
13178
13179 int salt_len = tmp_len - 64;
13180
13181 if (salt_len < 0) return (PARSER_SALT_LENGTH);
13182
13183 salt->salt_len = salt_len;
13184
13185 memcpy (salt->salt_buf, tmp_buf + 64, salt->salt_len);
13186
13187 if (data.opts_type & OPTS_TYPE_ST_ADD80)
13188 {
13189 char *ptr = (char *) salt->salt_buf;
13190
13191 ptr[salt->salt_len] = 0x80;
13192 }
13193
13194 return (PARSER_OK);
13195 }
13196
13197 int hmacmd5_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13198 {
13199 if (data.opts_type & OPTS_TYPE_ST_HEX)
13200 {
13201 if ((input_len < DISPLAY_LEN_MIN_50H) || (input_len > DISPLAY_LEN_MAX_50H)) return (PARSER_GLOBAL_LENGTH);
13202 }
13203 else
13204 {
13205 if ((input_len < DISPLAY_LEN_MIN_50) || (input_len > DISPLAY_LEN_MAX_50)) return (PARSER_GLOBAL_LENGTH);
13206 }
13207
13208 u32 *digest = (u32 *) hash_buf->digest;
13209
13210 salt_t *salt = hash_buf->salt;
13211
13212 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
13213 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
13214 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
13215 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
13216
13217 digest[0] = byte_swap_32 (digest[0]);
13218 digest[1] = byte_swap_32 (digest[1]);
13219 digest[2] = byte_swap_32 (digest[2]);
13220 digest[3] = byte_swap_32 (digest[3]);
13221
13222 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
13223
13224 uint salt_len = input_len - 32 - 1;
13225
13226 char *salt_buf = input_buf + 32 + 1;
13227
13228 char *salt_buf_ptr = (char *) salt->salt_buf;
13229
13230 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
13231
13232 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
13233
13234 salt->salt_len = salt_len;
13235
13236 return (PARSER_OK);
13237 }
13238
13239 int hmacsha1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13240 {
13241 if (data.opts_type & OPTS_TYPE_ST_HEX)
13242 {
13243 if ((input_len < DISPLAY_LEN_MIN_150H) || (input_len > DISPLAY_LEN_MAX_150H)) return (PARSER_GLOBAL_LENGTH);
13244 }
13245 else
13246 {
13247 if ((input_len < DISPLAY_LEN_MIN_150) || (input_len > DISPLAY_LEN_MAX_150)) return (PARSER_GLOBAL_LENGTH);
13248 }
13249
13250 u32 *digest = (u32 *) hash_buf->digest;
13251
13252 salt_t *salt = hash_buf->salt;
13253
13254 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
13255 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
13256 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
13257 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
13258 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
13259
13260 if (input_buf[40] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
13261
13262 uint salt_len = input_len - 40 - 1;
13263
13264 char *salt_buf = input_buf + 40 + 1;
13265
13266 char *salt_buf_ptr = (char *) salt->salt_buf;
13267
13268 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
13269
13270 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
13271
13272 salt->salt_len = salt_len;
13273
13274 return (PARSER_OK);
13275 }
13276
13277 int hmacsha256_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13278 {
13279 if (data.opts_type & OPTS_TYPE_ST_HEX)
13280 {
13281 if ((input_len < DISPLAY_LEN_MIN_1450H) || (input_len > DISPLAY_LEN_MAX_1450H)) return (PARSER_GLOBAL_LENGTH);
13282 }
13283 else
13284 {
13285 if ((input_len < DISPLAY_LEN_MIN_1450) || (input_len > DISPLAY_LEN_MAX_1450)) return (PARSER_GLOBAL_LENGTH);
13286 }
13287
13288 u32 *digest = (u32 *) hash_buf->digest;
13289
13290 salt_t *salt = hash_buf->salt;
13291
13292 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
13293 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
13294 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
13295 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
13296 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
13297 digest[5] = hex_to_u32 ((const u8 *) &input_buf[40]);
13298 digest[6] = hex_to_u32 ((const u8 *) &input_buf[48]);
13299 digest[7] = hex_to_u32 ((const u8 *) &input_buf[56]);
13300
13301 if (input_buf[64] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
13302
13303 uint salt_len = input_len - 64 - 1;
13304
13305 char *salt_buf = input_buf + 64 + 1;
13306
13307 char *salt_buf_ptr = (char *) salt->salt_buf;
13308
13309 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
13310
13311 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
13312
13313 salt->salt_len = salt_len;
13314
13315 return (PARSER_OK);
13316 }
13317
13318 int hmacsha512_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13319 {
13320 if (data.opts_type & OPTS_TYPE_ST_HEX)
13321 {
13322 if ((input_len < DISPLAY_LEN_MIN_1750H) || (input_len > DISPLAY_LEN_MAX_1750H)) return (PARSER_GLOBAL_LENGTH);
13323 }
13324 else
13325 {
13326 if ((input_len < DISPLAY_LEN_MIN_1750) || (input_len > DISPLAY_LEN_MAX_1750)) return (PARSER_GLOBAL_LENGTH);
13327 }
13328
13329 u64 *digest = (u64 *) hash_buf->digest;
13330
13331 salt_t *salt = hash_buf->salt;
13332
13333 digest[0] = hex_to_u64 ((const u8 *) &input_buf[ 0]);
13334 digest[1] = hex_to_u64 ((const u8 *) &input_buf[ 16]);
13335 digest[2] = hex_to_u64 ((const u8 *) &input_buf[ 32]);
13336 digest[3] = hex_to_u64 ((const u8 *) &input_buf[ 48]);
13337 digest[4] = hex_to_u64 ((const u8 *) &input_buf[ 64]);
13338 digest[5] = hex_to_u64 ((const u8 *) &input_buf[ 80]);
13339 digest[6] = hex_to_u64 ((const u8 *) &input_buf[ 96]);
13340 digest[7] = hex_to_u64 ((const u8 *) &input_buf[112]);
13341
13342 if (input_buf[128] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
13343
13344 uint salt_len = input_len - 128 - 1;
13345
13346 char *salt_buf = input_buf + 128 + 1;
13347
13348 char *salt_buf_ptr = (char *) salt->salt_buf;
13349
13350 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
13351
13352 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
13353
13354 salt->salt_len = salt_len;
13355
13356 return (PARSER_OK);
13357 }
13358
13359 int krb5pa_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13360 {
13361 if ((input_len < DISPLAY_LEN_MIN_7500) || (input_len > DISPLAY_LEN_MAX_7500)) return (PARSER_GLOBAL_LENGTH);
13362
13363 if (memcmp (SIGNATURE_KRB5PA, input_buf, 10)) return (PARSER_SIGNATURE_UNMATCHED);
13364
13365 u32 *digest = (u32 *) hash_buf->digest;
13366
13367 salt_t *salt = hash_buf->salt;
13368
13369 krb5pa_t *krb5pa = (krb5pa_t *) hash_buf->esalt;
13370
13371 /**
13372 * parse line
13373 */
13374
13375 char *user_pos = input_buf + 10 + 1;
13376
13377 char *realm_pos = strchr (user_pos, '$');
13378
13379 if (realm_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13380
13381 uint user_len = realm_pos - user_pos;
13382
13383 if (user_len >= 64) return (PARSER_SALT_LENGTH);
13384
13385 realm_pos++;
13386
13387 char *salt_pos = strchr (realm_pos, '$');
13388
13389 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13390
13391 uint realm_len = salt_pos - realm_pos;
13392
13393 if (realm_len >= 64) return (PARSER_SALT_LENGTH);
13394
13395 salt_pos++;
13396
13397 char *data_pos = strchr (salt_pos, '$');
13398
13399 if (data_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13400
13401 uint salt_len = data_pos - salt_pos;
13402
13403 if (salt_len >= 128) return (PARSER_SALT_LENGTH);
13404
13405 data_pos++;
13406
13407 uint data_len = input_len - 10 - 1 - user_len - 1 - realm_len - 1 - salt_len - 1;
13408
13409 if (data_len != ((36 + 16) * 2)) return (PARSER_SALT_LENGTH);
13410
13411 /**
13412 * copy data
13413 */
13414
13415 memcpy (krb5pa->user, user_pos, user_len);
13416 memcpy (krb5pa->realm, realm_pos, realm_len);
13417 memcpy (krb5pa->salt, salt_pos, salt_len);
13418
13419 char *timestamp_ptr = (char *) krb5pa->timestamp;
13420
13421 for (uint i = 0; i < (36 * 2); i += 2)
13422 {
13423 const char p0 = data_pos[i + 0];
13424 const char p1 = data_pos[i + 1];
13425
13426 *timestamp_ptr++ = hex_convert (p1) << 0
13427 | hex_convert (p0) << 4;
13428 }
13429
13430 char *checksum_ptr = (char *) krb5pa->checksum;
13431
13432 for (uint i = (36 * 2); i < ((36 + 16) * 2); i += 2)
13433 {
13434 const char p0 = data_pos[i + 0];
13435 const char p1 = data_pos[i + 1];
13436
13437 *checksum_ptr++ = hex_convert (p1) << 0
13438 | hex_convert (p0) << 4;
13439 }
13440
13441 /**
13442 * copy some data to generic buffers to make sorting happy
13443 */
13444
13445 salt->salt_buf[0] = krb5pa->timestamp[0];
13446 salt->salt_buf[1] = krb5pa->timestamp[1];
13447 salt->salt_buf[2] = krb5pa->timestamp[2];
13448 salt->salt_buf[3] = krb5pa->timestamp[3];
13449 salt->salt_buf[4] = krb5pa->timestamp[4];
13450 salt->salt_buf[5] = krb5pa->timestamp[5];
13451 salt->salt_buf[6] = krb5pa->timestamp[6];
13452 salt->salt_buf[7] = krb5pa->timestamp[7];
13453 salt->salt_buf[8] = krb5pa->timestamp[8];
13454
13455 salt->salt_len = 36;
13456
13457 digest[0] = krb5pa->checksum[0];
13458 digest[1] = krb5pa->checksum[1];
13459 digest[2] = krb5pa->checksum[2];
13460 digest[3] = krb5pa->checksum[3];
13461
13462 return (PARSER_OK);
13463 }
13464
13465 int sapb_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13466 {
13467 if ((input_len < DISPLAY_LEN_MIN_7700) || (input_len > DISPLAY_LEN_MAX_7700)) return (PARSER_GLOBAL_LENGTH);
13468
13469 u32 *digest = (u32 *) hash_buf->digest;
13470
13471 salt_t *salt = hash_buf->salt;
13472
13473 /**
13474 * parse line
13475 */
13476
13477 char *salt_pos = input_buf;
13478
13479 char *hash_pos = strchr (salt_pos, '$');
13480
13481 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13482
13483 uint salt_len = hash_pos - salt_pos;
13484
13485 if (salt_len >= 40) return (PARSER_SALT_LENGTH);
13486
13487 hash_pos++;
13488
13489 uint hash_len = input_len - 1 - salt_len;
13490
13491 if (hash_len != 16) return (PARSER_HASH_LENGTH);
13492
13493 /**
13494 * valid some data
13495 */
13496
13497 uint user_len = 0;
13498
13499 for (uint i = 0; i < salt_len; i++)
13500 {
13501 if (salt_pos[i] == ' ') continue;
13502
13503 user_len++;
13504 }
13505
13506 // SAP user names cannot be longer than 12 characters
13507 if (user_len > 12) return (PARSER_SALT_LENGTH);
13508
13509 // SAP user name cannot start with ! or ?
13510 if (salt_pos[0] == '!' || salt_pos[0] == '?') return (PARSER_SALT_VALUE);
13511
13512 /**
13513 * copy data
13514 */
13515
13516 char *salt_buf_ptr = (char *) salt->salt_buf;
13517
13518 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
13519
13520 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
13521
13522 salt->salt_len = salt_len;
13523
13524 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[0]);
13525 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[8]);
13526 digest[2] = 0;
13527 digest[3] = 0;
13528
13529 digest[0] = byte_swap_32 (digest[0]);
13530 digest[1] = byte_swap_32 (digest[1]);
13531
13532 return (PARSER_OK);
13533 }
13534
13535 int sapg_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13536 {
13537 if ((input_len < DISPLAY_LEN_MIN_7800) || (input_len > DISPLAY_LEN_MAX_7800)) return (PARSER_GLOBAL_LENGTH);
13538
13539 u32 *digest = (u32 *) hash_buf->digest;
13540
13541 salt_t *salt = hash_buf->salt;
13542
13543 /**
13544 * parse line
13545 */
13546
13547 char *salt_pos = input_buf;
13548
13549 char *hash_pos = strchr (salt_pos, '$');
13550
13551 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13552
13553 uint salt_len = hash_pos - salt_pos;
13554
13555 if (salt_len >= 40) return (PARSER_SALT_LENGTH);
13556
13557 hash_pos++;
13558
13559 uint hash_len = input_len - 1 - salt_len;
13560
13561 if (hash_len != 40) return (PARSER_HASH_LENGTH);
13562
13563 /**
13564 * valid some data
13565 */
13566
13567 uint user_len = 0;
13568
13569 for (uint i = 0; i < salt_len; i++)
13570 {
13571 if (salt_pos[i] == ' ') continue;
13572
13573 user_len++;
13574 }
13575
13576 // SAP user names cannot be longer than 12 characters
13577 // this is kinda buggy. if the username is in utf the length can be up to length 12*3
13578 // so far nobody complained so we stay with this because it helps in optimization
13579 // final string can have a max size of 32 (password) + (10 * 5) = lengthMagicArray + 12 (max salt) + 1 (the 0x80)
13580
13581 if (user_len > 12) return (PARSER_SALT_LENGTH);
13582
13583 // SAP user name cannot start with ! or ?
13584 if (salt_pos[0] == '!' || salt_pos[0] == '?') return (PARSER_SALT_VALUE);
13585
13586 /**
13587 * copy data
13588 */
13589
13590 char *salt_buf_ptr = (char *) salt->salt_buf;
13591
13592 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
13593
13594 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
13595
13596 salt->salt_len = salt_len;
13597
13598 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
13599 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
13600 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
13601 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
13602 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
13603
13604 return (PARSER_OK);
13605 }
13606
13607 int drupal7_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13608 {
13609 if ((input_len < DISPLAY_LEN_MIN_7900) || (input_len > DISPLAY_LEN_MAX_7900)) return (PARSER_GLOBAL_LENGTH);
13610
13611 if (memcmp (SIGNATURE_DRUPAL7, input_buf, 3)) return (PARSER_SIGNATURE_UNMATCHED);
13612
13613 u64 *digest = (u64 *) hash_buf->digest;
13614
13615 salt_t *salt = hash_buf->salt;
13616
13617 char *iter_pos = input_buf + 3;
13618
13619 uint salt_iter = 1 << itoa64_to_int (iter_pos[0]);
13620
13621 if (salt_iter > 0x80000000) return (PARSER_SALT_ITERATION);
13622
13623 memcpy ((char *) salt->salt_sign, input_buf, 4);
13624
13625 salt->salt_iter = salt_iter;
13626
13627 char *salt_pos = iter_pos + 1;
13628
13629 uint salt_len = 8;
13630
13631 memcpy ((char *) salt->salt_buf, salt_pos, salt_len);
13632
13633 salt->salt_len = salt_len;
13634
13635 char *hash_pos = salt_pos + salt_len;
13636
13637 drupal7_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
13638
13639 // ugly hack start
13640
13641 char *tmp = (char *) salt->salt_buf_pc;
13642
13643 tmp[0] = hash_pos[42];
13644
13645 // ugly hack end
13646
13647 digest[ 0] = byte_swap_64 (digest[ 0]);
13648 digest[ 1] = byte_swap_64 (digest[ 1]);
13649 digest[ 2] = byte_swap_64 (digest[ 2]);
13650 digest[ 3] = byte_swap_64 (digest[ 3]);
13651 digest[ 4] = 0;
13652 digest[ 5] = 0;
13653 digest[ 6] = 0;
13654 digest[ 7] = 0;
13655
13656 return (PARSER_OK);
13657 }
13658
13659 int sybasease_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13660 {
13661 if ((input_len < DISPLAY_LEN_MIN_8000) || (input_len > DISPLAY_LEN_MAX_8000)) return (PARSER_GLOBAL_LENGTH);
13662
13663 if (memcmp (SIGNATURE_SYBASEASE, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
13664
13665 u32 *digest = (u32 *) hash_buf->digest;
13666
13667 salt_t *salt = hash_buf->salt;
13668
13669 char *salt_buf = input_buf + 6;
13670
13671 uint salt_len = 16;
13672
13673 char *salt_buf_ptr = (char *) salt->salt_buf;
13674
13675 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
13676
13677 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
13678
13679 salt->salt_len = salt_len;
13680
13681 char *hash_pos = input_buf + 6 + 16;
13682
13683 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
13684 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
13685 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
13686 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
13687 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
13688 digest[5] = hex_to_u32 ((const u8 *) &hash_pos[40]);
13689 digest[6] = hex_to_u32 ((const u8 *) &hash_pos[48]);
13690 digest[7] = hex_to_u32 ((const u8 *) &hash_pos[56]);
13691
13692 return (PARSER_OK);
13693 }
13694
13695 int mysql323_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13696 {
13697 if ((input_len < DISPLAY_LEN_MIN_200) || (input_len > DISPLAY_LEN_MAX_200)) return (PARSER_GLOBAL_LENGTH);
13698
13699 u32 *digest = (u32 *) hash_buf->digest;
13700
13701 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
13702 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
13703 digest[2] = 0;
13704 digest[3] = 0;
13705
13706 return (PARSER_OK);
13707 }
13708
13709 int rakp_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13710 {
13711 if ((input_len < DISPLAY_LEN_MIN_7300) || (input_len > DISPLAY_LEN_MAX_7300)) return (PARSER_GLOBAL_LENGTH);
13712
13713 u32 *digest = (u32 *) hash_buf->digest;
13714
13715 salt_t *salt = hash_buf->salt;
13716
13717 rakp_t *rakp = (rakp_t *) hash_buf->esalt;
13718
13719 char *saltbuf_pos = input_buf;
13720
13721 char *hashbuf_pos = strchr (saltbuf_pos, ':');
13722
13723 if (hashbuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13724
13725 uint saltbuf_len = hashbuf_pos - saltbuf_pos;
13726
13727 if (saltbuf_len < 64) return (PARSER_SALT_LENGTH);
13728 if (saltbuf_len > 512) return (PARSER_SALT_LENGTH);
13729
13730 if (saltbuf_len & 1) return (PARSER_SALT_LENGTH); // muss gerade sein wegen hex
13731
13732 hashbuf_pos++;
13733
13734 uint hashbuf_len = input_len - saltbuf_len - 1;
13735
13736 if (hashbuf_len != 40) return (PARSER_HASH_LENGTH);
13737
13738 char *salt_ptr = (char *) saltbuf_pos;
13739 char *rakp_ptr = (char *) rakp->salt_buf;
13740
13741 uint i;
13742 uint j;
13743
13744 for (i = 0, j = 0; i < saltbuf_len; i += 2, j += 1)
13745 {
13746 rakp_ptr[j] = hex_to_u8 ((const u8 *) &salt_ptr[i]);
13747 }
13748
13749 rakp_ptr[j] = 0x80;
13750
13751 rakp->salt_len = j;
13752
13753 for (i = 0; i < 64; i++)
13754 {
13755 rakp->salt_buf[i] = byte_swap_32 (rakp->salt_buf[i]);
13756 }
13757
13758 salt->salt_buf[0] = rakp->salt_buf[0];
13759 salt->salt_buf[1] = rakp->salt_buf[1];
13760 salt->salt_buf[2] = rakp->salt_buf[2];
13761 salt->salt_buf[3] = rakp->salt_buf[3];
13762 salt->salt_buf[4] = rakp->salt_buf[4];
13763 salt->salt_buf[5] = rakp->salt_buf[5];
13764 salt->salt_buf[6] = rakp->salt_buf[6];
13765 salt->salt_buf[7] = rakp->salt_buf[7];
13766
13767 salt->salt_len = 32; // muss min. 32 haben
13768
13769 digest[0] = hex_to_u32 ((const u8 *) &hashbuf_pos[ 0]);
13770 digest[1] = hex_to_u32 ((const u8 *) &hashbuf_pos[ 8]);
13771 digest[2] = hex_to_u32 ((const u8 *) &hashbuf_pos[16]);
13772 digest[3] = hex_to_u32 ((const u8 *) &hashbuf_pos[24]);
13773 digest[4] = hex_to_u32 ((const u8 *) &hashbuf_pos[32]);
13774
13775 return (PARSER_OK);
13776 }
13777
13778 int netscaler_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13779 {
13780 if ((input_len < DISPLAY_LEN_MIN_8100) || (input_len > DISPLAY_LEN_MAX_8100)) return (PARSER_GLOBAL_LENGTH);
13781
13782 u32 *digest = (u32 *) hash_buf->digest;
13783
13784 salt_t *salt = hash_buf->salt;
13785
13786 if (memcmp (SIGNATURE_NETSCALER, input_buf, 1)) return (PARSER_SIGNATURE_UNMATCHED);
13787
13788 char *salt_pos = input_buf + 1;
13789
13790 memcpy (salt->salt_buf, salt_pos, 8);
13791
13792 salt->salt_buf[0] = byte_swap_32 (salt->salt_buf[0]);
13793 salt->salt_buf[1] = byte_swap_32 (salt->salt_buf[1]);
13794
13795 salt->salt_len = 8;
13796
13797 char *hash_pos = salt_pos + 8;
13798
13799 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
13800 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
13801 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
13802 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
13803 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
13804
13805 digest[0] -= SHA1M_A;
13806 digest[1] -= SHA1M_B;
13807 digest[2] -= SHA1M_C;
13808 digest[3] -= SHA1M_D;
13809 digest[4] -= SHA1M_E;
13810
13811 return (PARSER_OK);
13812 }
13813
13814 int chap_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13815 {
13816 if ((input_len < DISPLAY_LEN_MIN_4800) || (input_len > DISPLAY_LEN_MAX_4800)) return (PARSER_GLOBAL_LENGTH);
13817
13818 u32 *digest = (u32 *) hash_buf->digest;
13819
13820 salt_t *salt = hash_buf->salt;
13821
13822 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
13823 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
13824 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
13825 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
13826
13827 digest[0] = byte_swap_32 (digest[0]);
13828 digest[1] = byte_swap_32 (digest[1]);
13829 digest[2] = byte_swap_32 (digest[2]);
13830 digest[3] = byte_swap_32 (digest[3]);
13831
13832 digest[0] -= MD5M_A;
13833 digest[1] -= MD5M_B;
13834 digest[2] -= MD5M_C;
13835 digest[3] -= MD5M_D;
13836
13837 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
13838
13839 char *salt_buf_ptr = input_buf + 32 + 1;
13840
13841 u32 *salt_buf = salt->salt_buf;
13842
13843 salt_buf[0] = hex_to_u32 ((const u8 *) &salt_buf_ptr[ 0]);
13844 salt_buf[1] = hex_to_u32 ((const u8 *) &salt_buf_ptr[ 8]);
13845 salt_buf[2] = hex_to_u32 ((const u8 *) &salt_buf_ptr[16]);
13846 salt_buf[3] = hex_to_u32 ((const u8 *) &salt_buf_ptr[24]);
13847
13848 salt_buf[0] = byte_swap_32 (salt_buf[0]);
13849 salt_buf[1] = byte_swap_32 (salt_buf[1]);
13850 salt_buf[2] = byte_swap_32 (salt_buf[2]);
13851 salt_buf[3] = byte_swap_32 (salt_buf[3]);
13852
13853 salt->salt_len = 16 + 1;
13854
13855 if (input_buf[65] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
13856
13857 char *idbyte_buf_ptr = input_buf + 32 + 1 + 32 + 1;
13858
13859 salt_buf[4] = hex_to_u8 ((const u8 *) &idbyte_buf_ptr[0]) & 0xff;
13860
13861 return (PARSER_OK);
13862 }
13863
13864 int cloudkey_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13865 {
13866 if ((input_len < DISPLAY_LEN_MIN_8200) || (input_len > DISPLAY_LEN_MAX_8200)) return (PARSER_GLOBAL_LENGTH);
13867
13868 u32 *digest = (u32 *) hash_buf->digest;
13869
13870 salt_t *salt = hash_buf->salt;
13871
13872 cloudkey_t *cloudkey = (cloudkey_t *) hash_buf->esalt;
13873
13874 /**
13875 * parse line
13876 */
13877
13878 char *hashbuf_pos = input_buf;
13879
13880 char *saltbuf_pos = strchr (hashbuf_pos, ':');
13881
13882 if (saltbuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13883
13884 const uint hashbuf_len = saltbuf_pos - hashbuf_pos;
13885
13886 if (hashbuf_len != 64) return (PARSER_HASH_LENGTH);
13887
13888 saltbuf_pos++;
13889
13890 char *iteration_pos = strchr (saltbuf_pos, ':');
13891
13892 if (iteration_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13893
13894 const uint saltbuf_len = iteration_pos - saltbuf_pos;
13895
13896 if (saltbuf_len != 32) return (PARSER_SALT_LENGTH);
13897
13898 iteration_pos++;
13899
13900 char *databuf_pos = strchr (iteration_pos, ':');
13901
13902 if (databuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13903
13904 const uint iteration_len = databuf_pos - iteration_pos;
13905
13906 if (iteration_len < 1) return (PARSER_SALT_ITERATION);
13907 if (iteration_len > 8) return (PARSER_SALT_ITERATION);
13908
13909 const uint databuf_len = input_len - hashbuf_len - 1 - saltbuf_len - 1 - iteration_len - 1;
13910
13911 if (databuf_len < 1) return (PARSER_SALT_LENGTH);
13912 if (databuf_len > 2048) return (PARSER_SALT_LENGTH);
13913
13914 databuf_pos++;
13915
13916 // digest
13917
13918 digest[0] = hex_to_u32 ((const u8 *) &hashbuf_pos[ 0]);
13919 digest[1] = hex_to_u32 ((const u8 *) &hashbuf_pos[ 8]);
13920 digest[2] = hex_to_u32 ((const u8 *) &hashbuf_pos[16]);
13921 digest[3] = hex_to_u32 ((const u8 *) &hashbuf_pos[24]);
13922 digest[4] = hex_to_u32 ((const u8 *) &hashbuf_pos[32]);
13923 digest[5] = hex_to_u32 ((const u8 *) &hashbuf_pos[40]);
13924 digest[6] = hex_to_u32 ((const u8 *) &hashbuf_pos[48]);
13925 digest[7] = hex_to_u32 ((const u8 *) &hashbuf_pos[56]);
13926
13927 // salt
13928
13929 char *saltbuf_ptr = (char *) salt->salt_buf;
13930
13931 for (uint i = 0; i < saltbuf_len; i += 2)
13932 {
13933 const char p0 = saltbuf_pos[i + 0];
13934 const char p1 = saltbuf_pos[i + 1];
13935
13936 *saltbuf_ptr++ = hex_convert (p1) << 0
13937 | hex_convert (p0) << 4;
13938 }
13939
13940 salt->salt_buf[4] = 0x01000000;
13941 salt->salt_buf[5] = 0x80;
13942
13943 salt->salt_len = saltbuf_len / 2;
13944
13945 // iteration
13946
13947 salt->salt_iter = atoi (iteration_pos) - 1;
13948
13949 // data
13950
13951 char *databuf_ptr = (char *) cloudkey->data_buf;
13952
13953 for (uint i = 0; i < databuf_len; i += 2)
13954 {
13955 const char p0 = databuf_pos[i + 0];
13956 const char p1 = databuf_pos[i + 1];
13957
13958 *databuf_ptr++ = hex_convert (p1) << 0
13959 | hex_convert (p0) << 4;
13960 }
13961
13962 *databuf_ptr++ = 0x80;
13963
13964 for (uint i = 0; i < 512; i++)
13965 {
13966 cloudkey->data_buf[i] = byte_swap_32 (cloudkey->data_buf[i]);
13967 }
13968
13969 cloudkey->data_len = databuf_len / 2;
13970
13971 return (PARSER_OK);
13972 }
13973
13974 int nsec3_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
13975 {
13976 if ((input_len < DISPLAY_LEN_MIN_8300) || (input_len > DISPLAY_LEN_MAX_8300)) return (PARSER_GLOBAL_LENGTH);
13977
13978 u32 *digest = (u32 *) hash_buf->digest;
13979
13980 salt_t *salt = hash_buf->salt;
13981
13982 /**
13983 * parse line
13984 */
13985
13986 char *hashbuf_pos = input_buf;
13987
13988 char *domainbuf_pos = strchr (hashbuf_pos, ':');
13989
13990 if (domainbuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
13991
13992 const uint hashbuf_len = domainbuf_pos - hashbuf_pos;
13993
13994 if (hashbuf_len != 32) return (PARSER_HASH_LENGTH);
13995
13996 domainbuf_pos++;
13997
13998 if (domainbuf_pos[0] != '.') return (PARSER_SALT_VALUE);
13999
14000 char *saltbuf_pos = strchr (domainbuf_pos, ':');
14001
14002 if (saltbuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14003
14004 const uint domainbuf_len = saltbuf_pos - domainbuf_pos;
14005
14006 if (domainbuf_len >= 32) return (PARSER_SALT_LENGTH);
14007
14008 saltbuf_pos++;
14009
14010 char *iteration_pos = strchr (saltbuf_pos, ':');
14011
14012 if (iteration_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14013
14014 const uint saltbuf_len = iteration_pos - saltbuf_pos;
14015
14016 if (saltbuf_len >= 28) return (PARSER_SALT_LENGTH); // 28 = 32 - 4; 4 = length
14017
14018 if ((domainbuf_len + saltbuf_len) >= 48) return (PARSER_SALT_LENGTH);
14019
14020 iteration_pos++;
14021
14022 const uint iteration_len = input_len - hashbuf_len - 1 - domainbuf_len - 1 - saltbuf_len - 1;
14023
14024 if (iteration_len < 1) return (PARSER_SALT_ITERATION);
14025 if (iteration_len > 5) return (PARSER_SALT_ITERATION);
14026
14027 // ok, the plan for this algorithm is the following:
14028 // we have 2 salts here, the domain-name and a random salt
14029 // while both are used in the initial transformation,
14030 // only the random salt is used in the following iterations
14031 // so we create two buffer, one that includes domain-name (stored into salt_buf_pc[])
14032 // and one that includes only the real salt (stored into salt_buf[]).
14033 // the domain-name length is put into array position 7 of salt_buf_pc[] since there is not salt_pc_len
14034
14035 u8 tmp_buf[100] = { 0 };
14036
14037 base32_decode (itoa32_to_int, (const u8 *) hashbuf_pos, 32, tmp_buf);
14038
14039 memcpy (digest, tmp_buf, 20);
14040
14041 digest[0] = byte_swap_32 (digest[0]);
14042 digest[1] = byte_swap_32 (digest[1]);
14043 digest[2] = byte_swap_32 (digest[2]);
14044 digest[3] = byte_swap_32 (digest[3]);
14045 digest[4] = byte_swap_32 (digest[4]);
14046
14047 // domain
14048
14049 char *salt_buf_pc_ptr = (char *) salt->salt_buf_pc;
14050
14051 memcpy (salt_buf_pc_ptr, domainbuf_pos, domainbuf_len);
14052
14053 char *len_ptr = NULL;
14054
14055 for (uint i = 0; i < domainbuf_len; i++)
14056 {
14057 if (salt_buf_pc_ptr[i] == '.')
14058 {
14059 len_ptr = &salt_buf_pc_ptr[i];
14060
14061 *len_ptr = 0;
14062 }
14063 else
14064 {
14065 *len_ptr += 1;
14066 }
14067 }
14068
14069 salt->salt_buf_pc[7] = domainbuf_len;
14070
14071 // "real" salt
14072
14073 char *salt_buf_ptr = (char *) salt->salt_buf;
14074
14075 const uint salt_len = parse_and_store_salt (salt_buf_ptr, saltbuf_pos, saltbuf_len);
14076
14077 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
14078
14079 salt->salt_len = salt_len;
14080
14081 // iteration
14082
14083 salt->salt_iter = atoi (iteration_pos);
14084
14085 return (PARSER_OK);
14086 }
14087
14088 int wbb3_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14089 {
14090 if ((input_len < DISPLAY_LEN_MIN_8400) || (input_len > DISPLAY_LEN_MAX_8400)) return (PARSER_GLOBAL_LENGTH);
14091
14092 u32 *digest = (u32 *) hash_buf->digest;
14093
14094 salt_t *salt = hash_buf->salt;
14095
14096 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
14097 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
14098 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
14099 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
14100 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
14101
14102 if (input_buf[40] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
14103
14104 uint salt_len = input_len - 40 - 1;
14105
14106 char *salt_buf = input_buf + 40 + 1;
14107
14108 char *salt_buf_ptr = (char *) salt->salt_buf;
14109
14110 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
14111
14112 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
14113
14114 salt->salt_len = salt_len;
14115
14116 return (PARSER_OK);
14117 }
14118
14119 int racf_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14120 {
14121 const u8 ascii_to_ebcdic[] =
14122 {
14123 0x00, 0x01, 0x02, 0x03, 0x37, 0x2d, 0x2e, 0x2f, 0x16, 0x05, 0x25, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
14124 0x10, 0x11, 0x12, 0x13, 0x3c, 0x3d, 0x32, 0x26, 0x18, 0x19, 0x3f, 0x27, 0x1c, 0x1d, 0x1e, 0x1f,
14125 0x40, 0x4f, 0x7f, 0x7b, 0x5b, 0x6c, 0x50, 0x7d, 0x4d, 0x5d, 0x5c, 0x4e, 0x6b, 0x60, 0x4b, 0x61,
14126 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0x7a, 0x5e, 0x4c, 0x7e, 0x6e, 0x6f,
14127 0x7c, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
14128 0xd7, 0xd8, 0xd9, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0x4a, 0xe0, 0x5a, 0x5f, 0x6d,
14129 0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
14130 0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xc0, 0x6a, 0xd0, 0xa1, 0x07,
14131 0x20, 0x21, 0x22, 0x23, 0x24, 0x15, 0x06, 0x17, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x09, 0x0a, 0x1b,
14132 0x30, 0x31, 0x1a, 0x33, 0x34, 0x35, 0x36, 0x08, 0x38, 0x39, 0x3a, 0x3b, 0x04, 0x14, 0x3e, 0xe1,
14133 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
14134 0x58, 0x59, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75,
14135 0x76, 0x77, 0x78, 0x80, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e,
14136 0x9f, 0xa0, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
14137 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xda, 0xdb,
14138 0xdc, 0xdd, 0xde, 0xdf, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
14139 };
14140
14141 if ((input_len < DISPLAY_LEN_MIN_8500) || (input_len > DISPLAY_LEN_MAX_8500)) return (PARSER_GLOBAL_LENGTH);
14142
14143 if (memcmp (SIGNATURE_RACF, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
14144
14145 u32 *digest = (u32 *) hash_buf->digest;
14146
14147 salt_t *salt = hash_buf->salt;
14148
14149 char *salt_pos = input_buf + 6 + 1;
14150
14151 char *digest_pos = strchr (salt_pos, '*');
14152
14153 if (digest_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14154
14155 uint salt_len = digest_pos - salt_pos;
14156
14157 if (salt_len > 8) return (PARSER_SALT_LENGTH);
14158
14159 uint hash_len = input_len - 1 - salt_len - 1 - 6;
14160
14161 if (hash_len != 16) return (PARSER_HASH_LENGTH);
14162
14163 digest_pos++;
14164
14165 char *salt_buf_ptr = (char *) salt->salt_buf;
14166 char *salt_buf_pc_ptr = (char *) salt->salt_buf_pc;
14167
14168 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
14169
14170 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
14171
14172 salt->salt_len = salt_len;
14173
14174 for (uint i = 0; i < salt_len; i++)
14175 {
14176 salt_buf_pc_ptr[i] = ascii_to_ebcdic[(int) salt_buf_ptr[i]];
14177 }
14178 for (uint i = salt_len; i < 8; i++)
14179 {
14180 salt_buf_pc_ptr[i] = 0x40;
14181 }
14182
14183 uint tt;
14184
14185 IP (salt->salt_buf_pc[0], salt->salt_buf_pc[1], tt);
14186
14187 salt->salt_buf_pc[0] = rotl32 (salt->salt_buf_pc[0], 3u);
14188 salt->salt_buf_pc[1] = rotl32 (salt->salt_buf_pc[1], 3u);
14189
14190 digest[0] = hex_to_u32 ((const u8 *) &digest_pos[ 0]);
14191 digest[1] = hex_to_u32 ((const u8 *) &digest_pos[ 8]);
14192
14193 digest[0] = byte_swap_32 (digest[0]);
14194 digest[1] = byte_swap_32 (digest[1]);
14195
14196 IP (digest[0], digest[1], tt);
14197
14198 digest[0] = rotr32 (digest[0], 29);
14199 digest[1] = rotr32 (digest[1], 29);
14200 digest[2] = 0;
14201 digest[3] = 0;
14202
14203 return (PARSER_OK);
14204 }
14205
14206 int lotus5_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14207 {
14208 if ((input_len < DISPLAY_LEN_MIN_8600) || (input_len > DISPLAY_LEN_MAX_8600)) return (PARSER_GLOBAL_LENGTH);
14209
14210 u32 *digest = (u32 *) hash_buf->digest;
14211
14212 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
14213 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
14214 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
14215 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
14216
14217 digest[0] = byte_swap_32 (digest[0]);
14218 digest[1] = byte_swap_32 (digest[1]);
14219 digest[2] = byte_swap_32 (digest[2]);
14220 digest[3] = byte_swap_32 (digest[3]);
14221
14222 return (PARSER_OK);
14223 }
14224
14225 int lotus6_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14226 {
14227 if ((input_len < DISPLAY_LEN_MIN_8700) || (input_len > DISPLAY_LEN_MAX_8700)) return (PARSER_GLOBAL_LENGTH);
14228
14229 if ((input_buf[0] != '(') || (input_buf[1] != 'G') || (input_buf[21] != ')')) return (PARSER_SIGNATURE_UNMATCHED);
14230
14231 u32 *digest = (u32 *) hash_buf->digest;
14232
14233 salt_t *salt = hash_buf->salt;
14234
14235 u8 tmp_buf[120] = { 0 };
14236
14237 base64_decode (lotus64_to_int, (const u8 *) input_buf + 2, input_len - 3, tmp_buf);
14238
14239 tmp_buf[3] += -4; // dont ask!
14240
14241 memcpy (salt->salt_buf, tmp_buf, 5);
14242
14243 salt->salt_len = 5;
14244
14245 memcpy (digest, tmp_buf + 5, 9);
14246
14247 // yes, only 9 byte are needed to crack, but 10 to display
14248
14249 salt->salt_buf_pc[7] = input_buf[20];
14250
14251 return (PARSER_OK);
14252 }
14253
14254 int lotus8_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14255 {
14256 if ((input_len < DISPLAY_LEN_MIN_9100) || (input_len > DISPLAY_LEN_MAX_9100)) return (PARSER_GLOBAL_LENGTH);
14257
14258 if ((input_buf[0] != '(') || (input_buf[1] != 'H') || (input_buf[DISPLAY_LEN_MAX_9100 - 1] != ')')) return (PARSER_SIGNATURE_UNMATCHED);
14259
14260 u32 *digest = (u32 *) hash_buf->digest;
14261
14262 salt_t *salt = hash_buf->salt;
14263
14264 u8 tmp_buf[120] = { 0 };
14265
14266 base64_decode (lotus64_to_int, (const u8 *) input_buf + 2, input_len - 3, tmp_buf);
14267
14268 tmp_buf[3] += -4; // dont ask!
14269
14270 // salt
14271
14272 memcpy (salt->salt_buf, tmp_buf, 16);
14273
14274 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)
14275
14276 // iteration
14277
14278 char tmp_iter_buf[11] = { 0 };
14279
14280 memcpy (tmp_iter_buf, tmp_buf + 16, 10);
14281
14282 tmp_iter_buf[10] = 0;
14283
14284 salt->salt_iter = atoi (tmp_iter_buf);
14285
14286 if (salt->salt_iter < 1) // well, the limit hopefully is much higher
14287 {
14288 return (PARSER_SALT_ITERATION);
14289 }
14290
14291 salt->salt_iter--; // first round in init
14292
14293 // 2 additional bytes for display only
14294
14295 salt->salt_buf_pc[0] = tmp_buf[26];
14296 salt->salt_buf_pc[1] = tmp_buf[27];
14297
14298 // digest
14299
14300 memcpy (digest, tmp_buf + 28, 8);
14301
14302 digest[0] = byte_swap_32 (digest[0]);
14303 digest[1] = byte_swap_32 (digest[1]);
14304 digest[2] = 0;
14305 digest[3] = 0;
14306
14307 return (PARSER_OK);
14308 }
14309
14310 int hmailserver_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14311 {
14312 if ((input_len < DISPLAY_LEN_MIN_1421) || (input_len > DISPLAY_LEN_MAX_1421)) return (PARSER_GLOBAL_LENGTH);
14313
14314 u32 *digest = (u32 *) hash_buf->digest;
14315
14316 salt_t *salt = hash_buf->salt;
14317
14318 char *salt_buf_pos = input_buf;
14319
14320 char *hash_buf_pos = salt_buf_pos + 6;
14321
14322 digest[0] = hex_to_u32 ((const u8 *) &hash_buf_pos[ 0]);
14323 digest[1] = hex_to_u32 ((const u8 *) &hash_buf_pos[ 8]);
14324 digest[2] = hex_to_u32 ((const u8 *) &hash_buf_pos[16]);
14325 digest[3] = hex_to_u32 ((const u8 *) &hash_buf_pos[24]);
14326 digest[4] = hex_to_u32 ((const u8 *) &hash_buf_pos[32]);
14327 digest[5] = hex_to_u32 ((const u8 *) &hash_buf_pos[40]);
14328 digest[6] = hex_to_u32 ((const u8 *) &hash_buf_pos[48]);
14329 digest[7] = hex_to_u32 ((const u8 *) &hash_buf_pos[56]);
14330
14331 digest[0] -= SHA256M_A;
14332 digest[1] -= SHA256M_B;
14333 digest[2] -= SHA256M_C;
14334 digest[3] -= SHA256M_D;
14335 digest[4] -= SHA256M_E;
14336 digest[5] -= SHA256M_F;
14337 digest[6] -= SHA256M_G;
14338 digest[7] -= SHA256M_H;
14339
14340 char *salt_buf_ptr = (char *) salt->salt_buf;
14341
14342 const uint salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf_pos, 6);
14343
14344 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
14345
14346 salt->salt_len = salt_len;
14347
14348 return (PARSER_OK);
14349 }
14350
14351 int phps_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14352 {
14353 if ((input_len < DISPLAY_LEN_MIN_2612) || (input_len > DISPLAY_LEN_MAX_2612)) return (PARSER_GLOBAL_LENGTH);
14354
14355 u32 *digest = (u32 *) hash_buf->digest;
14356
14357 if (memcmp (SIGNATURE_PHPS, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
14358
14359 salt_t *salt = hash_buf->salt;
14360
14361 char *salt_buf = input_buf + 6;
14362
14363 char *digest_buf = strchr (salt_buf, '$');
14364
14365 if (digest_buf == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14366
14367 uint salt_len = digest_buf - salt_buf;
14368
14369 digest_buf++; // skip the '$' symbol
14370
14371 char *salt_buf_ptr = (char *) salt->salt_buf;
14372
14373 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
14374
14375 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
14376
14377 salt->salt_len = salt_len;
14378
14379 digest[0] = hex_to_u32 ((const u8 *) &digest_buf[ 0]);
14380 digest[1] = hex_to_u32 ((const u8 *) &digest_buf[ 8]);
14381 digest[2] = hex_to_u32 ((const u8 *) &digest_buf[16]);
14382 digest[3] = hex_to_u32 ((const u8 *) &digest_buf[24]);
14383
14384 digest[0] = byte_swap_32 (digest[0]);
14385 digest[1] = byte_swap_32 (digest[1]);
14386 digest[2] = byte_swap_32 (digest[2]);
14387 digest[3] = byte_swap_32 (digest[3]);
14388
14389 digest[0] -= MD5M_A;
14390 digest[1] -= MD5M_B;
14391 digest[2] -= MD5M_C;
14392 digest[3] -= MD5M_D;
14393
14394 return (PARSER_OK);
14395 }
14396
14397 int mediawiki_b_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14398 {
14399 if ((input_len < DISPLAY_LEN_MIN_3711) || (input_len > DISPLAY_LEN_MAX_3711)) return (PARSER_GLOBAL_LENGTH);
14400
14401 if (memcmp (SIGNATURE_MEDIAWIKI_B, input_buf, 3)) return (PARSER_SIGNATURE_UNMATCHED);
14402
14403 u32 *digest = (u32 *) hash_buf->digest;
14404
14405 salt_t *salt = hash_buf->salt;
14406
14407 char *salt_buf = input_buf + 3;
14408
14409 char *digest_buf = strchr (salt_buf, '$');
14410
14411 if (digest_buf == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14412
14413 uint salt_len = digest_buf - salt_buf;
14414
14415 digest_buf++; // skip the '$' symbol
14416
14417 char *salt_buf_ptr = (char *) salt->salt_buf;
14418
14419 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
14420
14421 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
14422
14423 salt_buf_ptr[salt_len] = 0x2d;
14424
14425 salt->salt_len = salt_len + 1;
14426
14427 digest[0] = hex_to_u32 ((const u8 *) &digest_buf[ 0]);
14428 digest[1] = hex_to_u32 ((const u8 *) &digest_buf[ 8]);
14429 digest[2] = hex_to_u32 ((const u8 *) &digest_buf[16]);
14430 digest[3] = hex_to_u32 ((const u8 *) &digest_buf[24]);
14431
14432 digest[0] = byte_swap_32 (digest[0]);
14433 digest[1] = byte_swap_32 (digest[1]);
14434 digest[2] = byte_swap_32 (digest[2]);
14435 digest[3] = byte_swap_32 (digest[3]);
14436
14437 digest[0] -= MD5M_A;
14438 digest[1] -= MD5M_B;
14439 digest[2] -= MD5M_C;
14440 digest[3] -= MD5M_D;
14441
14442 return (PARSER_OK);
14443 }
14444
14445 int peoplesoft_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14446 {
14447 if ((input_len < DISPLAY_LEN_MIN_133) || (input_len > DISPLAY_LEN_MAX_133)) return (PARSER_GLOBAL_LENGTH);
14448
14449 u32 *digest = (u32 *) hash_buf->digest;
14450
14451 salt_t *salt = hash_buf->salt;
14452
14453 u8 tmp_buf[100] = { 0 };
14454
14455 base64_decode (base64_to_int, (const u8 *) input_buf, input_len, tmp_buf);
14456
14457 memcpy (digest, tmp_buf, 20);
14458
14459 digest[0] = byte_swap_32 (digest[0]);
14460 digest[1] = byte_swap_32 (digest[1]);
14461 digest[2] = byte_swap_32 (digest[2]);
14462 digest[3] = byte_swap_32 (digest[3]);
14463 digest[4] = byte_swap_32 (digest[4]);
14464
14465 digest[0] -= SHA1M_A;
14466 digest[1] -= SHA1M_B;
14467 digest[2] -= SHA1M_C;
14468 digest[3] -= SHA1M_D;
14469 digest[4] -= SHA1M_E;
14470
14471 salt->salt_buf[0] = 0x80;
14472
14473 salt->salt_len = 0;
14474
14475 return (PARSER_OK);
14476 }
14477
14478 int skype_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14479 {
14480 if ((input_len < DISPLAY_LEN_MIN_23) || (input_len > DISPLAY_LEN_MAX_23)) return (PARSER_GLOBAL_LENGTH);
14481
14482 u32 *digest = (u32 *) hash_buf->digest;
14483
14484 salt_t *salt = hash_buf->salt;
14485
14486 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
14487 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
14488 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
14489 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
14490
14491 digest[0] = byte_swap_32 (digest[0]);
14492 digest[1] = byte_swap_32 (digest[1]);
14493 digest[2] = byte_swap_32 (digest[2]);
14494 digest[3] = byte_swap_32 (digest[3]);
14495
14496 digest[0] -= MD5M_A;
14497 digest[1] -= MD5M_B;
14498 digest[2] -= MD5M_C;
14499 digest[3] -= MD5M_D;
14500
14501 if (input_buf[32] != ':') return (PARSER_SEPARATOR_UNMATCHED);
14502
14503 uint salt_len = input_len - 32 - 1;
14504
14505 char *salt_buf = input_buf + 32 + 1;
14506
14507 char *salt_buf_ptr = (char *) salt->salt_buf;
14508
14509 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
14510
14511 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
14512
14513 /*
14514 * add static "salt" part
14515 */
14516
14517 memcpy (salt_buf_ptr + salt_len, "\nskyper\n", 8);
14518
14519 salt_len += 8;
14520
14521 salt->salt_len = salt_len;
14522
14523 return (PARSER_OK);
14524 }
14525
14526 int androidfde_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14527 {
14528 if ((input_len < DISPLAY_LEN_MIN_8800) || (input_len > DISPLAY_LEN_MAX_8800)) return (PARSER_GLOBAL_LENGTH);
14529
14530 if (memcmp (SIGNATURE_ANDROIDFDE, input_buf, 5)) return (PARSER_SIGNATURE_UNMATCHED);
14531
14532 u32 *digest = (u32 *) hash_buf->digest;
14533
14534 salt_t *salt = hash_buf->salt;
14535
14536 androidfde_t *androidfde = (androidfde_t *) hash_buf->esalt;
14537
14538 /**
14539 * parse line
14540 */
14541
14542 char *saltlen_pos = input_buf + 1 + 3 + 1;
14543
14544 char *saltbuf_pos = strchr (saltlen_pos, '$');
14545
14546 if (saltbuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14547
14548 uint saltlen_len = saltbuf_pos - saltlen_pos;
14549
14550 if (saltlen_len != 2) return (PARSER_SALT_LENGTH);
14551
14552 saltbuf_pos++;
14553
14554 char *keylen_pos = strchr (saltbuf_pos, '$');
14555
14556 if (keylen_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14557
14558 uint saltbuf_len = keylen_pos - saltbuf_pos;
14559
14560 if (saltbuf_len != 32) return (PARSER_SALT_LENGTH);
14561
14562 keylen_pos++;
14563
14564 char *keybuf_pos = strchr (keylen_pos, '$');
14565
14566 if (keybuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14567
14568 uint keylen_len = keybuf_pos - keylen_pos;
14569
14570 if (keylen_len != 2) return (PARSER_SALT_LENGTH);
14571
14572 keybuf_pos++;
14573
14574 char *databuf_pos = strchr (keybuf_pos, '$');
14575
14576 if (databuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14577
14578 uint keybuf_len = databuf_pos - keybuf_pos;
14579
14580 if (keybuf_len != 32) return (PARSER_SALT_LENGTH);
14581
14582 databuf_pos++;
14583
14584 uint data_len = input_len - 1 - 3 - 1 - saltlen_len - 1 - saltbuf_len - 1 - keylen_len - 1 - keybuf_len - 1;
14585
14586 if (data_len != 3072) return (PARSER_SALT_LENGTH);
14587
14588 /**
14589 * copy data
14590 */
14591
14592 digest[0] = hex_to_u32 ((const u8 *) &keybuf_pos[ 0]);
14593 digest[1] = hex_to_u32 ((const u8 *) &keybuf_pos[ 8]);
14594 digest[2] = hex_to_u32 ((const u8 *) &keybuf_pos[16]);
14595 digest[3] = hex_to_u32 ((const u8 *) &keybuf_pos[24]);
14596
14597 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &saltbuf_pos[ 0]);
14598 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &saltbuf_pos[ 8]);
14599 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &saltbuf_pos[16]);
14600 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &saltbuf_pos[24]);
14601
14602 salt->salt_buf[0] = byte_swap_32 (salt->salt_buf[0]);
14603 salt->salt_buf[1] = byte_swap_32 (salt->salt_buf[1]);
14604 salt->salt_buf[2] = byte_swap_32 (salt->salt_buf[2]);
14605 salt->salt_buf[3] = byte_swap_32 (salt->salt_buf[3]);
14606
14607 salt->salt_len = 16;
14608 salt->salt_iter = ROUNDS_ANDROIDFDE - 1;
14609
14610 for (uint i = 0, j = 0; i < 3072; i += 8, j += 1)
14611 {
14612 androidfde->data[j] = hex_to_u32 ((const u8 *) &databuf_pos[i]);
14613 }
14614
14615 return (PARSER_OK);
14616 }
14617
14618 int scrypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14619 {
14620 if ((input_len < DISPLAY_LEN_MIN_8900) || (input_len > DISPLAY_LEN_MAX_8900)) return (PARSER_GLOBAL_LENGTH);
14621
14622 if (memcmp (SIGNATURE_SCRYPT, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
14623
14624 u32 *digest = (u32 *) hash_buf->digest;
14625
14626 salt_t *salt = hash_buf->salt;
14627
14628 /**
14629 * parse line
14630 */
14631
14632 // first is the N salt parameter
14633
14634 char *N_pos = input_buf + 6;
14635
14636 if (N_pos[0] != ':') return (PARSER_SEPARATOR_UNMATCHED);
14637
14638 N_pos++;
14639
14640 salt->scrypt_N = atoi (N_pos);
14641
14642 // r
14643
14644 char *r_pos = strchr (N_pos, ':');
14645
14646 if (r_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14647
14648 r_pos++;
14649
14650 salt->scrypt_r = atoi (r_pos);
14651
14652 // p
14653
14654 char *p_pos = strchr (r_pos, ':');
14655
14656 if (p_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14657
14658 p_pos++;
14659
14660 salt->scrypt_p = atoi (p_pos);
14661
14662 // salt
14663
14664 char *saltbuf_pos = strchr (p_pos, ':');
14665
14666 if (saltbuf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14667
14668 saltbuf_pos++;
14669
14670 char *hash_pos = strchr (saltbuf_pos, ':');
14671
14672 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14673
14674 hash_pos++;
14675
14676 // base64 decode
14677
14678 u8 tmp_buf[33] = { 0 };
14679
14680 int tmp_len = base64_decode (base64_to_int, (const u8 *) saltbuf_pos, hash_pos - saltbuf_pos, tmp_buf);
14681
14682 char *salt_buf_ptr = (char *) salt->salt_buf;
14683
14684 memcpy (salt_buf_ptr, tmp_buf, tmp_len);
14685
14686 salt->salt_len = tmp_len;
14687 salt->salt_iter = 1;
14688
14689 // digest - base64 decode
14690
14691 memset (tmp_buf, 0, sizeof (tmp_buf));
14692
14693 tmp_len = input_len - (hash_pos - input_buf);
14694
14695 if (tmp_len != 44) return (PARSER_GLOBAL_LENGTH);
14696
14697 base64_decode (base64_to_int, (const u8 *) hash_pos, tmp_len, tmp_buf);
14698
14699 memcpy (digest, tmp_buf, 32);
14700
14701 return (PARSER_OK);
14702 }
14703
14704 int juniper_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14705 {
14706 if ((input_len < DISPLAY_LEN_MIN_501) || (input_len > DISPLAY_LEN_MAX_501)) return (PARSER_GLOBAL_LENGTH);
14707
14708 u32 *digest = (u32 *) hash_buf->digest;
14709
14710 salt_t *salt = hash_buf->salt;
14711
14712 /**
14713 * parse line
14714 */
14715
14716 char decrypted[76] = { 0 }; // iv + hash
14717
14718 juniper_decrypt_hash (input_buf, decrypted);
14719
14720 char *md5crypt_hash = decrypted + 12;
14721
14722 if (memcmp (md5crypt_hash, "$1$danastre$", 12)) return (PARSER_SALT_VALUE);
14723
14724 salt->salt_iter = ROUNDS_MD5CRYPT;
14725
14726 char *salt_pos = md5crypt_hash + 3;
14727
14728 char *hash_pos = strchr (salt_pos, '$'); // or simply salt_pos + 8
14729
14730 salt->salt_len = hash_pos - salt_pos; // should be 8
14731
14732 memcpy ((char *) salt->salt_buf, salt_pos, salt->salt_len);
14733
14734 hash_pos++;
14735
14736 md5crypt_decode ((unsigned char *) digest, (unsigned char *) hash_pos);
14737
14738 return (PARSER_OK);
14739 }
14740
14741 int cisco8_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14742 {
14743 if ((input_len < DISPLAY_LEN_MIN_9200) || (input_len > DISPLAY_LEN_MAX_9200)) return (PARSER_GLOBAL_LENGTH);
14744
14745 if (memcmp (SIGNATURE_CISCO8, input_buf, 3)) return (PARSER_SIGNATURE_UNMATCHED);
14746
14747 u32 *digest = (u32 *) hash_buf->digest;
14748
14749 salt_t *salt = hash_buf->salt;
14750
14751 pbkdf2_sha256_t *pbkdf2_sha256 = (pbkdf2_sha256_t *) hash_buf->esalt;
14752
14753 /**
14754 * parse line
14755 */
14756
14757 // first is *raw* salt
14758
14759 char *salt_pos = input_buf + 3;
14760
14761 char *hash_pos = strchr (salt_pos, '$');
14762
14763 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14764
14765 uint salt_len = hash_pos - salt_pos;
14766
14767 if (salt_len != 14) return (PARSER_SALT_LENGTH);
14768
14769 hash_pos++;
14770
14771 char *salt_buf_ptr = (char *) pbkdf2_sha256->salt_buf;
14772
14773 memcpy (salt_buf_ptr, salt_pos, 14);
14774
14775 salt_buf_ptr[17] = 0x01;
14776 salt_buf_ptr[18] = 0x80;
14777
14778 // add some stuff to normal salt to make sorted happy
14779
14780 salt->salt_buf[0] = pbkdf2_sha256->salt_buf[0];
14781 salt->salt_buf[1] = pbkdf2_sha256->salt_buf[1];
14782 salt->salt_buf[2] = pbkdf2_sha256->salt_buf[2];
14783 salt->salt_buf[3] = pbkdf2_sha256->salt_buf[3];
14784
14785 salt->salt_len = salt_len;
14786 salt->salt_iter = ROUNDS_CISCO8 - 1;
14787
14788 // base64 decode hash
14789
14790 u8 tmp_buf[100] = { 0 };
14791
14792 uint hash_len = input_len - 3 - salt_len - 1;
14793
14794 int tmp_len = base64_decode (itoa64_to_int, (const u8 *) hash_pos, hash_len, tmp_buf);
14795
14796 if (tmp_len != 32) return (PARSER_HASH_LENGTH);
14797
14798 memcpy (digest, tmp_buf, 32);
14799
14800 digest[0] = byte_swap_32 (digest[0]);
14801 digest[1] = byte_swap_32 (digest[1]);
14802 digest[2] = byte_swap_32 (digest[2]);
14803 digest[3] = byte_swap_32 (digest[3]);
14804 digest[4] = byte_swap_32 (digest[4]);
14805 digest[5] = byte_swap_32 (digest[5]);
14806 digest[6] = byte_swap_32 (digest[6]);
14807 digest[7] = byte_swap_32 (digest[7]);
14808
14809 return (PARSER_OK);
14810 }
14811
14812 int cisco9_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14813 {
14814 if ((input_len < DISPLAY_LEN_MIN_9300) || (input_len > DISPLAY_LEN_MAX_9300)) return (PARSER_GLOBAL_LENGTH);
14815
14816 if (memcmp (SIGNATURE_CISCO9, input_buf, 3)) return (PARSER_SIGNATURE_UNMATCHED);
14817
14818 u32 *digest = (u32 *) hash_buf->digest;
14819
14820 salt_t *salt = hash_buf->salt;
14821
14822 /**
14823 * parse line
14824 */
14825
14826 // first is *raw* salt
14827
14828 char *salt_pos = input_buf + 3;
14829
14830 char *hash_pos = strchr (salt_pos, '$');
14831
14832 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14833
14834 uint salt_len = hash_pos - salt_pos;
14835
14836 if (salt_len != 14) return (PARSER_SALT_LENGTH);
14837
14838 salt->salt_len = salt_len;
14839 hash_pos++;
14840
14841 char *salt_buf_ptr = (char *) salt->salt_buf;
14842
14843 memcpy (salt_buf_ptr, salt_pos, salt_len);
14844 salt_buf_ptr[salt_len] = 0;
14845
14846 // base64 decode hash
14847
14848 u8 tmp_buf[100] = { 0 };
14849
14850 uint hash_len = input_len - 3 - salt_len - 1;
14851
14852 int tmp_len = base64_decode (itoa64_to_int, (const u8 *) hash_pos, hash_len, tmp_buf);
14853
14854 if (tmp_len != 32) return (PARSER_HASH_LENGTH);
14855
14856 memcpy (digest, tmp_buf, 32);
14857
14858 // fixed:
14859 salt->scrypt_N = 16384;
14860 salt->scrypt_r = 1;
14861 salt->scrypt_p = 1;
14862 salt->salt_iter = 1;
14863
14864 return (PARSER_OK);
14865 }
14866
14867 int office2007_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
14868 {
14869 if ((input_len < DISPLAY_LEN_MIN_9400) || (input_len > DISPLAY_LEN_MAX_9400)) return (PARSER_GLOBAL_LENGTH);
14870
14871 if (memcmp (SIGNATURE_OFFICE2007, input_buf, 8)) return (PARSER_SIGNATURE_UNMATCHED);
14872
14873 u32 *digest = (u32 *) hash_buf->digest;
14874
14875 salt_t *salt = hash_buf->salt;
14876
14877 office2007_t *office2007 = (office2007_t *) hash_buf->esalt;
14878
14879 /**
14880 * parse line
14881 */
14882
14883 char *version_pos = input_buf + 8 + 1;
14884
14885 char *verifierHashSize_pos = strchr (version_pos, '*');
14886
14887 if (verifierHashSize_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14888
14889 u32 version_len = verifierHashSize_pos - version_pos;
14890
14891 if (version_len != 4) return (PARSER_SALT_LENGTH);
14892
14893 verifierHashSize_pos++;
14894
14895 char *keySize_pos = strchr (verifierHashSize_pos, '*');
14896
14897 if (keySize_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14898
14899 u32 verifierHashSize_len = keySize_pos - verifierHashSize_pos;
14900
14901 if (verifierHashSize_len != 2) return (PARSER_SALT_LENGTH);
14902
14903 keySize_pos++;
14904
14905 char *saltSize_pos = strchr (keySize_pos, '*');
14906
14907 if (saltSize_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14908
14909 u32 keySize_len = saltSize_pos - keySize_pos;
14910
14911 if (keySize_len != 3) return (PARSER_SALT_LENGTH);
14912
14913 saltSize_pos++;
14914
14915 char *osalt_pos = strchr (saltSize_pos, '*');
14916
14917 if (osalt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14918
14919 u32 saltSize_len = osalt_pos - saltSize_pos;
14920
14921 if (saltSize_len != 2) return (PARSER_SALT_LENGTH);
14922
14923 osalt_pos++;
14924
14925 char *encryptedVerifier_pos = strchr (osalt_pos, '*');
14926
14927 if (encryptedVerifier_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14928
14929 u32 osalt_len = encryptedVerifier_pos - osalt_pos;
14930
14931 if (osalt_len != 32) return (PARSER_SALT_LENGTH);
14932
14933 encryptedVerifier_pos++;
14934
14935 char *encryptedVerifierHash_pos = strchr (encryptedVerifier_pos, '*');
14936
14937 if (encryptedVerifierHash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
14938
14939 u32 encryptedVerifier_len = encryptedVerifierHash_pos - encryptedVerifier_pos;
14940
14941 if (encryptedVerifier_len != 32) return (PARSER_SALT_LENGTH);
14942
14943 encryptedVerifierHash_pos++;
14944
14945 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;
14946
14947 if (encryptedVerifierHash_len != 40) return (PARSER_SALT_LENGTH);
14948
14949 const uint version = atoi (version_pos);
14950
14951 if (version != 2007) return (PARSER_SALT_VALUE);
14952
14953 const uint verifierHashSize = atoi (verifierHashSize_pos);
14954
14955 if (verifierHashSize != 20) return (PARSER_SALT_VALUE);
14956
14957 const uint keySize = atoi (keySize_pos);
14958
14959 if ((keySize != 128) && (keySize != 256)) return (PARSER_SALT_VALUE);
14960
14961 office2007->keySize = keySize;
14962
14963 const uint saltSize = atoi (saltSize_pos);
14964
14965 if (saltSize != 16) return (PARSER_SALT_VALUE);
14966
14967 /**
14968 * salt
14969 */
14970
14971 salt->salt_len = 16;
14972 salt->salt_iter = ROUNDS_OFFICE2007;
14973
14974 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &osalt_pos[ 0]);
14975 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &osalt_pos[ 8]);
14976 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &osalt_pos[16]);
14977 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &osalt_pos[24]);
14978
14979 /**
14980 * esalt
14981 */
14982
14983 office2007->encryptedVerifier[0] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 0]);
14984 office2007->encryptedVerifier[1] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 8]);
14985 office2007->encryptedVerifier[2] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[16]);
14986 office2007->encryptedVerifier[3] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[24]);
14987
14988 office2007->encryptedVerifierHash[0] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 0]);
14989 office2007->encryptedVerifierHash[1] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 8]);
14990 office2007->encryptedVerifierHash[2] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[16]);
14991 office2007->encryptedVerifierHash[3] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[24]);
14992 office2007->encryptedVerifierHash[4] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[32]);
14993
14994 /**
14995 * digest
14996 */
14997
14998 digest[0] = office2007->encryptedVerifierHash[0];
14999 digest[1] = office2007->encryptedVerifierHash[1];
15000 digest[2] = office2007->encryptedVerifierHash[2];
15001 digest[3] = office2007->encryptedVerifierHash[3];
15002
15003 return (PARSER_OK);
15004 }
15005
15006 int office2010_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15007 {
15008 if ((input_len < DISPLAY_LEN_MIN_9500) || (input_len > DISPLAY_LEN_MAX_9500)) return (PARSER_GLOBAL_LENGTH);
15009
15010 if (memcmp (SIGNATURE_OFFICE2010, input_buf, 8)) return (PARSER_SIGNATURE_UNMATCHED);
15011
15012 u32 *digest = (u32 *) hash_buf->digest;
15013
15014 salt_t *salt = hash_buf->salt;
15015
15016 office2010_t *office2010 = (office2010_t *) hash_buf->esalt;
15017
15018 /**
15019 * parse line
15020 */
15021
15022 char *version_pos = input_buf + 8 + 1;
15023
15024 char *spinCount_pos = strchr (version_pos, '*');
15025
15026 if (spinCount_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15027
15028 u32 version_len = spinCount_pos - version_pos;
15029
15030 if (version_len != 4) return (PARSER_SALT_LENGTH);
15031
15032 spinCount_pos++;
15033
15034 char *keySize_pos = strchr (spinCount_pos, '*');
15035
15036 if (keySize_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15037
15038 u32 spinCount_len = keySize_pos - spinCount_pos;
15039
15040 if (spinCount_len != 6) return (PARSER_SALT_LENGTH);
15041
15042 keySize_pos++;
15043
15044 char *saltSize_pos = strchr (keySize_pos, '*');
15045
15046 if (saltSize_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15047
15048 u32 keySize_len = saltSize_pos - keySize_pos;
15049
15050 if (keySize_len != 3) return (PARSER_SALT_LENGTH);
15051
15052 saltSize_pos++;
15053
15054 char *osalt_pos = strchr (saltSize_pos, '*');
15055
15056 if (osalt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15057
15058 u32 saltSize_len = osalt_pos - saltSize_pos;
15059
15060 if (saltSize_len != 2) return (PARSER_SALT_LENGTH);
15061
15062 osalt_pos++;
15063
15064 char *encryptedVerifier_pos = strchr (osalt_pos, '*');
15065
15066 if (encryptedVerifier_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15067
15068 u32 osalt_len = encryptedVerifier_pos - osalt_pos;
15069
15070 if (osalt_len != 32) return (PARSER_SALT_LENGTH);
15071
15072 encryptedVerifier_pos++;
15073
15074 char *encryptedVerifierHash_pos = strchr (encryptedVerifier_pos, '*');
15075
15076 if (encryptedVerifierHash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15077
15078 u32 encryptedVerifier_len = encryptedVerifierHash_pos - encryptedVerifier_pos;
15079
15080 if (encryptedVerifier_len != 32) return (PARSER_SALT_LENGTH);
15081
15082 encryptedVerifierHash_pos++;
15083
15084 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;
15085
15086 if (encryptedVerifierHash_len != 64) return (PARSER_SALT_LENGTH);
15087
15088 const uint version = atoi (version_pos);
15089
15090 if (version != 2010) return (PARSER_SALT_VALUE);
15091
15092 const uint spinCount = atoi (spinCount_pos);
15093
15094 if (spinCount != 100000) return (PARSER_SALT_VALUE);
15095
15096 const uint keySize = atoi (keySize_pos);
15097
15098 if (keySize != 128) return (PARSER_SALT_VALUE);
15099
15100 const uint saltSize = atoi (saltSize_pos);
15101
15102 if (saltSize != 16) return (PARSER_SALT_VALUE);
15103
15104 /**
15105 * salt
15106 */
15107
15108 salt->salt_len = 16;
15109 salt->salt_iter = spinCount;
15110
15111 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &osalt_pos[ 0]);
15112 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &osalt_pos[ 8]);
15113 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &osalt_pos[16]);
15114 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &osalt_pos[24]);
15115
15116 /**
15117 * esalt
15118 */
15119
15120 office2010->encryptedVerifier[0] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 0]);
15121 office2010->encryptedVerifier[1] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 8]);
15122 office2010->encryptedVerifier[2] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[16]);
15123 office2010->encryptedVerifier[3] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[24]);
15124
15125 office2010->encryptedVerifierHash[0] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 0]);
15126 office2010->encryptedVerifierHash[1] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 8]);
15127 office2010->encryptedVerifierHash[2] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[16]);
15128 office2010->encryptedVerifierHash[3] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[24]);
15129 office2010->encryptedVerifierHash[4] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[32]);
15130 office2010->encryptedVerifierHash[5] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[40]);
15131 office2010->encryptedVerifierHash[6] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[48]);
15132 office2010->encryptedVerifierHash[7] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[56]);
15133
15134 /**
15135 * digest
15136 */
15137
15138 digest[0] = office2010->encryptedVerifierHash[0];
15139 digest[1] = office2010->encryptedVerifierHash[1];
15140 digest[2] = office2010->encryptedVerifierHash[2];
15141 digest[3] = office2010->encryptedVerifierHash[3];
15142
15143 return (PARSER_OK);
15144 }
15145
15146 int office2013_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15147 {
15148 if ((input_len < DISPLAY_LEN_MIN_9600) || (input_len > DISPLAY_LEN_MAX_9600)) return (PARSER_GLOBAL_LENGTH);
15149
15150 if (memcmp (SIGNATURE_OFFICE2013, input_buf, 8)) return (PARSER_SIGNATURE_UNMATCHED);
15151
15152 u32 *digest = (u32 *) hash_buf->digest;
15153
15154 salt_t *salt = hash_buf->salt;
15155
15156 office2013_t *office2013 = (office2013_t *) hash_buf->esalt;
15157
15158 /**
15159 * parse line
15160 */
15161
15162 char *version_pos = input_buf + 8 + 1;
15163
15164 char *spinCount_pos = strchr (version_pos, '*');
15165
15166 if (spinCount_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15167
15168 u32 version_len = spinCount_pos - version_pos;
15169
15170 if (version_len != 4) return (PARSER_SALT_LENGTH);
15171
15172 spinCount_pos++;
15173
15174 char *keySize_pos = strchr (spinCount_pos, '*');
15175
15176 if (keySize_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15177
15178 u32 spinCount_len = keySize_pos - spinCount_pos;
15179
15180 if (spinCount_len != 6) return (PARSER_SALT_LENGTH);
15181
15182 keySize_pos++;
15183
15184 char *saltSize_pos = strchr (keySize_pos, '*');
15185
15186 if (saltSize_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15187
15188 u32 keySize_len = saltSize_pos - keySize_pos;
15189
15190 if (keySize_len != 3) return (PARSER_SALT_LENGTH);
15191
15192 saltSize_pos++;
15193
15194 char *osalt_pos = strchr (saltSize_pos, '*');
15195
15196 if (osalt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15197
15198 u32 saltSize_len = osalt_pos - saltSize_pos;
15199
15200 if (saltSize_len != 2) return (PARSER_SALT_LENGTH);
15201
15202 osalt_pos++;
15203
15204 char *encryptedVerifier_pos = strchr (osalt_pos, '*');
15205
15206 if (encryptedVerifier_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15207
15208 u32 osalt_len = encryptedVerifier_pos - osalt_pos;
15209
15210 if (osalt_len != 32) return (PARSER_SALT_LENGTH);
15211
15212 encryptedVerifier_pos++;
15213
15214 char *encryptedVerifierHash_pos = strchr (encryptedVerifier_pos, '*');
15215
15216 if (encryptedVerifierHash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15217
15218 u32 encryptedVerifier_len = encryptedVerifierHash_pos - encryptedVerifier_pos;
15219
15220 if (encryptedVerifier_len != 32) return (PARSER_SALT_LENGTH);
15221
15222 encryptedVerifierHash_pos++;
15223
15224 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;
15225
15226 if (encryptedVerifierHash_len != 64) return (PARSER_SALT_LENGTH);
15227
15228 const uint version = atoi (version_pos);
15229
15230 if (version != 2013) return (PARSER_SALT_VALUE);
15231
15232 const uint spinCount = atoi (spinCount_pos);
15233
15234 if (spinCount != 100000) return (PARSER_SALT_VALUE);
15235
15236 const uint keySize = atoi (keySize_pos);
15237
15238 if (keySize != 256) return (PARSER_SALT_VALUE);
15239
15240 const uint saltSize = atoi (saltSize_pos);
15241
15242 if (saltSize != 16) return (PARSER_SALT_VALUE);
15243
15244 /**
15245 * salt
15246 */
15247
15248 salt->salt_len = 16;
15249 salt->salt_iter = spinCount;
15250
15251 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &osalt_pos[ 0]);
15252 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &osalt_pos[ 8]);
15253 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &osalt_pos[16]);
15254 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &osalt_pos[24]);
15255
15256 /**
15257 * esalt
15258 */
15259
15260 office2013->encryptedVerifier[0] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 0]);
15261 office2013->encryptedVerifier[1] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 8]);
15262 office2013->encryptedVerifier[2] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[16]);
15263 office2013->encryptedVerifier[3] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[24]);
15264
15265 office2013->encryptedVerifierHash[0] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 0]);
15266 office2013->encryptedVerifierHash[1] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 8]);
15267 office2013->encryptedVerifierHash[2] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[16]);
15268 office2013->encryptedVerifierHash[3] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[24]);
15269 office2013->encryptedVerifierHash[4] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[32]);
15270 office2013->encryptedVerifierHash[5] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[40]);
15271 office2013->encryptedVerifierHash[6] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[48]);
15272 office2013->encryptedVerifierHash[7] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[56]);
15273
15274 /**
15275 * digest
15276 */
15277
15278 digest[0] = office2013->encryptedVerifierHash[0];
15279 digest[1] = office2013->encryptedVerifierHash[1];
15280 digest[2] = office2013->encryptedVerifierHash[2];
15281 digest[3] = office2013->encryptedVerifierHash[3];
15282
15283 return (PARSER_OK);
15284 }
15285
15286 int oldoffice01_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15287 {
15288 if ((input_len < DISPLAY_LEN_MIN_9700) || (input_len > DISPLAY_LEN_MAX_9700)) return (PARSER_GLOBAL_LENGTH);
15289
15290 if ((memcmp (SIGNATURE_OLDOFFICE0, input_buf, 12)) && (memcmp (SIGNATURE_OLDOFFICE1, input_buf, 12))) return (PARSER_SIGNATURE_UNMATCHED);
15291
15292 u32 *digest = (u32 *) hash_buf->digest;
15293
15294 salt_t *salt = hash_buf->salt;
15295
15296 oldoffice01_t *oldoffice01 = (oldoffice01_t *) hash_buf->esalt;
15297
15298 /**
15299 * parse line
15300 */
15301
15302 char *version_pos = input_buf + 11;
15303
15304 char *osalt_pos = strchr (version_pos, '*');
15305
15306 if (osalt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15307
15308 u32 version_len = osalt_pos - version_pos;
15309
15310 if (version_len != 1) return (PARSER_SALT_LENGTH);
15311
15312 osalt_pos++;
15313
15314 char *encryptedVerifier_pos = strchr (osalt_pos, '*');
15315
15316 if (encryptedVerifier_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15317
15318 u32 osalt_len = encryptedVerifier_pos - osalt_pos;
15319
15320 if (osalt_len != 32) return (PARSER_SALT_LENGTH);
15321
15322 encryptedVerifier_pos++;
15323
15324 char *encryptedVerifierHash_pos = strchr (encryptedVerifier_pos, '*');
15325
15326 if (encryptedVerifierHash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15327
15328 u32 encryptedVerifier_len = encryptedVerifierHash_pos - encryptedVerifier_pos;
15329
15330 if (encryptedVerifier_len != 32) return (PARSER_SALT_LENGTH);
15331
15332 encryptedVerifierHash_pos++;
15333
15334 u32 encryptedVerifierHash_len = input_len - 11 - version_len - 1 - osalt_len - 1 - encryptedVerifier_len - 1;
15335
15336 if (encryptedVerifierHash_len != 32) return (PARSER_SALT_LENGTH);
15337
15338 const uint version = *version_pos - 0x30;
15339
15340 if (version != 0 && version != 1) return (PARSER_SALT_VALUE);
15341
15342 /**
15343 * esalt
15344 */
15345
15346 oldoffice01->version = version;
15347
15348 oldoffice01->encryptedVerifier[0] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 0]);
15349 oldoffice01->encryptedVerifier[1] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 8]);
15350 oldoffice01->encryptedVerifier[2] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[16]);
15351 oldoffice01->encryptedVerifier[3] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[24]);
15352
15353 oldoffice01->encryptedVerifier[0] = byte_swap_32 (oldoffice01->encryptedVerifier[0]);
15354 oldoffice01->encryptedVerifier[1] = byte_swap_32 (oldoffice01->encryptedVerifier[1]);
15355 oldoffice01->encryptedVerifier[2] = byte_swap_32 (oldoffice01->encryptedVerifier[2]);
15356 oldoffice01->encryptedVerifier[3] = byte_swap_32 (oldoffice01->encryptedVerifier[3]);
15357
15358 oldoffice01->encryptedVerifierHash[0] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 0]);
15359 oldoffice01->encryptedVerifierHash[1] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 8]);
15360 oldoffice01->encryptedVerifierHash[2] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[16]);
15361 oldoffice01->encryptedVerifierHash[3] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[24]);
15362
15363 oldoffice01->encryptedVerifierHash[0] = byte_swap_32 (oldoffice01->encryptedVerifierHash[0]);
15364 oldoffice01->encryptedVerifierHash[1] = byte_swap_32 (oldoffice01->encryptedVerifierHash[1]);
15365 oldoffice01->encryptedVerifierHash[2] = byte_swap_32 (oldoffice01->encryptedVerifierHash[2]);
15366 oldoffice01->encryptedVerifierHash[3] = byte_swap_32 (oldoffice01->encryptedVerifierHash[3]);
15367
15368 /**
15369 * salt
15370 */
15371
15372 salt->salt_len = 16;
15373
15374 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &osalt_pos[ 0]);
15375 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &osalt_pos[ 8]);
15376 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &osalt_pos[16]);
15377 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &osalt_pos[24]);
15378
15379 salt->salt_buf[0] = byte_swap_32 (salt->salt_buf[0]);
15380 salt->salt_buf[1] = byte_swap_32 (salt->salt_buf[1]);
15381 salt->salt_buf[2] = byte_swap_32 (salt->salt_buf[2]);
15382 salt->salt_buf[3] = byte_swap_32 (salt->salt_buf[3]);
15383
15384 // this is a workaround as office produces multiple documents with the same salt
15385
15386 salt->salt_len += 32;
15387
15388 salt->salt_buf[ 4] = oldoffice01->encryptedVerifier[0];
15389 salt->salt_buf[ 5] = oldoffice01->encryptedVerifier[1];
15390 salt->salt_buf[ 6] = oldoffice01->encryptedVerifier[2];
15391 salt->salt_buf[ 7] = oldoffice01->encryptedVerifier[3];
15392 salt->salt_buf[ 8] = oldoffice01->encryptedVerifierHash[0];
15393 salt->salt_buf[ 9] = oldoffice01->encryptedVerifierHash[1];
15394 salt->salt_buf[10] = oldoffice01->encryptedVerifierHash[2];
15395 salt->salt_buf[11] = oldoffice01->encryptedVerifierHash[3];
15396
15397 /**
15398 * digest
15399 */
15400
15401 digest[0] = oldoffice01->encryptedVerifierHash[0];
15402 digest[1] = oldoffice01->encryptedVerifierHash[1];
15403 digest[2] = oldoffice01->encryptedVerifierHash[2];
15404 digest[3] = oldoffice01->encryptedVerifierHash[3];
15405
15406 return (PARSER_OK);
15407 }
15408
15409 int oldoffice01cm1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15410 {
15411 return oldoffice01_parse_hash (input_buf, input_len, hash_buf);
15412 }
15413
15414 int oldoffice01cm2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15415 {
15416 if ((input_len < DISPLAY_LEN_MIN_9720) || (input_len > DISPLAY_LEN_MAX_9720)) return (PARSER_GLOBAL_LENGTH);
15417
15418 if ((memcmp (SIGNATURE_OLDOFFICE0, input_buf, 12)) && (memcmp (SIGNATURE_OLDOFFICE1, input_buf, 12))) return (PARSER_SIGNATURE_UNMATCHED);
15419
15420 u32 *digest = (u32 *) hash_buf->digest;
15421
15422 salt_t *salt = hash_buf->salt;
15423
15424 oldoffice01_t *oldoffice01 = (oldoffice01_t *) hash_buf->esalt;
15425
15426 /**
15427 * parse line
15428 */
15429
15430 char *version_pos = input_buf + 11;
15431
15432 char *osalt_pos = strchr (version_pos, '*');
15433
15434 if (osalt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15435
15436 u32 version_len = osalt_pos - version_pos;
15437
15438 if (version_len != 1) return (PARSER_SALT_LENGTH);
15439
15440 osalt_pos++;
15441
15442 char *encryptedVerifier_pos = strchr (osalt_pos, '*');
15443
15444 if (encryptedVerifier_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15445
15446 u32 osalt_len = encryptedVerifier_pos - osalt_pos;
15447
15448 if (osalt_len != 32) return (PARSER_SALT_LENGTH);
15449
15450 encryptedVerifier_pos++;
15451
15452 char *encryptedVerifierHash_pos = strchr (encryptedVerifier_pos, '*');
15453
15454 if (encryptedVerifierHash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15455
15456 u32 encryptedVerifier_len = encryptedVerifierHash_pos - encryptedVerifier_pos;
15457
15458 if (encryptedVerifier_len != 32) return (PARSER_SALT_LENGTH);
15459
15460 encryptedVerifierHash_pos++;
15461
15462 char *rc4key_pos = strchr (encryptedVerifierHash_pos, ':');
15463
15464 if (rc4key_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15465
15466 u32 encryptedVerifierHash_len = rc4key_pos - encryptedVerifierHash_pos;
15467
15468 if (encryptedVerifierHash_len != 32) return (PARSER_SALT_LENGTH);
15469
15470 rc4key_pos++;
15471
15472 u32 rc4key_len = input_len - 11 - version_len - 1 - osalt_len - 1 - encryptedVerifier_len - 1 - encryptedVerifierHash_len - 1;
15473
15474 if (rc4key_len != 10) return (PARSER_SALT_LENGTH);
15475
15476 const uint version = *version_pos - 0x30;
15477
15478 if (version != 0 && version != 1) return (PARSER_SALT_VALUE);
15479
15480 /**
15481 * esalt
15482 */
15483
15484 oldoffice01->version = version;
15485
15486 oldoffice01->encryptedVerifier[0] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 0]);
15487 oldoffice01->encryptedVerifier[1] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 8]);
15488 oldoffice01->encryptedVerifier[2] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[16]);
15489 oldoffice01->encryptedVerifier[3] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[24]);
15490
15491 oldoffice01->encryptedVerifier[0] = byte_swap_32 (oldoffice01->encryptedVerifier[0]);
15492 oldoffice01->encryptedVerifier[1] = byte_swap_32 (oldoffice01->encryptedVerifier[1]);
15493 oldoffice01->encryptedVerifier[2] = byte_swap_32 (oldoffice01->encryptedVerifier[2]);
15494 oldoffice01->encryptedVerifier[3] = byte_swap_32 (oldoffice01->encryptedVerifier[3]);
15495
15496 oldoffice01->encryptedVerifierHash[0] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 0]);
15497 oldoffice01->encryptedVerifierHash[1] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 8]);
15498 oldoffice01->encryptedVerifierHash[2] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[16]);
15499 oldoffice01->encryptedVerifierHash[3] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[24]);
15500
15501 oldoffice01->encryptedVerifierHash[0] = byte_swap_32 (oldoffice01->encryptedVerifierHash[0]);
15502 oldoffice01->encryptedVerifierHash[1] = byte_swap_32 (oldoffice01->encryptedVerifierHash[1]);
15503 oldoffice01->encryptedVerifierHash[2] = byte_swap_32 (oldoffice01->encryptedVerifierHash[2]);
15504 oldoffice01->encryptedVerifierHash[3] = byte_swap_32 (oldoffice01->encryptedVerifierHash[3]);
15505
15506 oldoffice01->rc4key[1] = 0;
15507 oldoffice01->rc4key[0] = 0;
15508
15509 oldoffice01->rc4key[0] |= hex_convert (rc4key_pos[0]) << 28;
15510 oldoffice01->rc4key[0] |= hex_convert (rc4key_pos[1]) << 24;
15511 oldoffice01->rc4key[0] |= hex_convert (rc4key_pos[2]) << 20;
15512 oldoffice01->rc4key[0] |= hex_convert (rc4key_pos[3]) << 16;
15513 oldoffice01->rc4key[0] |= hex_convert (rc4key_pos[4]) << 12;
15514 oldoffice01->rc4key[0] |= hex_convert (rc4key_pos[5]) << 8;
15515 oldoffice01->rc4key[0] |= hex_convert (rc4key_pos[6]) << 4;
15516 oldoffice01->rc4key[0] |= hex_convert (rc4key_pos[7]) << 0;
15517 oldoffice01->rc4key[1] |= hex_convert (rc4key_pos[8]) << 28;
15518 oldoffice01->rc4key[1] |= hex_convert (rc4key_pos[9]) << 24;
15519
15520 oldoffice01->rc4key[0] = byte_swap_32 (oldoffice01->rc4key[0]);
15521 oldoffice01->rc4key[1] = byte_swap_32 (oldoffice01->rc4key[1]);
15522
15523 /**
15524 * salt
15525 */
15526
15527 salt->salt_len = 16;
15528
15529 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &osalt_pos[ 0]);
15530 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &osalt_pos[ 8]);
15531 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &osalt_pos[16]);
15532 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &osalt_pos[24]);
15533
15534 salt->salt_buf[0] = byte_swap_32 (salt->salt_buf[0]);
15535 salt->salt_buf[1] = byte_swap_32 (salt->salt_buf[1]);
15536 salt->salt_buf[2] = byte_swap_32 (salt->salt_buf[2]);
15537 salt->salt_buf[3] = byte_swap_32 (salt->salt_buf[3]);
15538
15539 // this is a workaround as office produces multiple documents with the same salt
15540
15541 salt->salt_len += 32;
15542
15543 salt->salt_buf[ 4] = oldoffice01->encryptedVerifier[0];
15544 salt->salt_buf[ 5] = oldoffice01->encryptedVerifier[1];
15545 salt->salt_buf[ 6] = oldoffice01->encryptedVerifier[2];
15546 salt->salt_buf[ 7] = oldoffice01->encryptedVerifier[3];
15547 salt->salt_buf[ 8] = oldoffice01->encryptedVerifierHash[0];
15548 salt->salt_buf[ 9] = oldoffice01->encryptedVerifierHash[1];
15549 salt->salt_buf[10] = oldoffice01->encryptedVerifierHash[2];
15550 salt->salt_buf[11] = oldoffice01->encryptedVerifierHash[3];
15551
15552 /**
15553 * digest
15554 */
15555
15556 digest[0] = oldoffice01->rc4key[0];
15557 digest[1] = oldoffice01->rc4key[1];
15558 digest[2] = 0;
15559 digest[3] = 0;
15560
15561 return (PARSER_OK);
15562 }
15563
15564 int oldoffice34_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15565 {
15566 if ((input_len < DISPLAY_LEN_MIN_9800) || (input_len > DISPLAY_LEN_MAX_9800)) return (PARSER_GLOBAL_LENGTH);
15567
15568 if ((memcmp (SIGNATURE_OLDOFFICE3, input_buf, 12)) && (memcmp (SIGNATURE_OLDOFFICE4, input_buf, 12))) return (PARSER_SIGNATURE_UNMATCHED);
15569
15570 u32 *digest = (u32 *) hash_buf->digest;
15571
15572 salt_t *salt = hash_buf->salt;
15573
15574 oldoffice34_t *oldoffice34 = (oldoffice34_t *) hash_buf->esalt;
15575
15576 /**
15577 * parse line
15578 */
15579
15580 char *version_pos = input_buf + 11;
15581
15582 char *osalt_pos = strchr (version_pos, '*');
15583
15584 if (osalt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15585
15586 u32 version_len = osalt_pos - version_pos;
15587
15588 if (version_len != 1) return (PARSER_SALT_LENGTH);
15589
15590 osalt_pos++;
15591
15592 char *encryptedVerifier_pos = strchr (osalt_pos, '*');
15593
15594 if (encryptedVerifier_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15595
15596 u32 osalt_len = encryptedVerifier_pos - osalt_pos;
15597
15598 if (osalt_len != 32) return (PARSER_SALT_LENGTH);
15599
15600 encryptedVerifier_pos++;
15601
15602 char *encryptedVerifierHash_pos = strchr (encryptedVerifier_pos, '*');
15603
15604 if (encryptedVerifierHash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15605
15606 u32 encryptedVerifier_len = encryptedVerifierHash_pos - encryptedVerifier_pos;
15607
15608 if (encryptedVerifier_len != 32) return (PARSER_SALT_LENGTH);
15609
15610 encryptedVerifierHash_pos++;
15611
15612 u32 encryptedVerifierHash_len = input_len - 11 - version_len - 1 - osalt_len - 1 - encryptedVerifier_len - 1;
15613
15614 if (encryptedVerifierHash_len != 40) return (PARSER_SALT_LENGTH);
15615
15616 const uint version = *version_pos - 0x30;
15617
15618 if (version != 3 && version != 4) return (PARSER_SALT_VALUE);
15619
15620 /**
15621 * esalt
15622 */
15623
15624 oldoffice34->version = version;
15625
15626 oldoffice34->encryptedVerifier[0] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 0]);
15627 oldoffice34->encryptedVerifier[1] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 8]);
15628 oldoffice34->encryptedVerifier[2] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[16]);
15629 oldoffice34->encryptedVerifier[3] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[24]);
15630
15631 oldoffice34->encryptedVerifier[0] = byte_swap_32 (oldoffice34->encryptedVerifier[0]);
15632 oldoffice34->encryptedVerifier[1] = byte_swap_32 (oldoffice34->encryptedVerifier[1]);
15633 oldoffice34->encryptedVerifier[2] = byte_swap_32 (oldoffice34->encryptedVerifier[2]);
15634 oldoffice34->encryptedVerifier[3] = byte_swap_32 (oldoffice34->encryptedVerifier[3]);
15635
15636 oldoffice34->encryptedVerifierHash[0] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 0]);
15637 oldoffice34->encryptedVerifierHash[1] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 8]);
15638 oldoffice34->encryptedVerifierHash[2] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[16]);
15639 oldoffice34->encryptedVerifierHash[3] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[24]);
15640 oldoffice34->encryptedVerifierHash[4] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[32]);
15641
15642 oldoffice34->encryptedVerifierHash[0] = byte_swap_32 (oldoffice34->encryptedVerifierHash[0]);
15643 oldoffice34->encryptedVerifierHash[1] = byte_swap_32 (oldoffice34->encryptedVerifierHash[1]);
15644 oldoffice34->encryptedVerifierHash[2] = byte_swap_32 (oldoffice34->encryptedVerifierHash[2]);
15645 oldoffice34->encryptedVerifierHash[3] = byte_swap_32 (oldoffice34->encryptedVerifierHash[3]);
15646 oldoffice34->encryptedVerifierHash[4] = byte_swap_32 (oldoffice34->encryptedVerifierHash[4]);
15647
15648 /**
15649 * salt
15650 */
15651
15652 salt->salt_len = 16;
15653
15654 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &osalt_pos[ 0]);
15655 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &osalt_pos[ 8]);
15656 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &osalt_pos[16]);
15657 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &osalt_pos[24]);
15658
15659 // this is a workaround as office produces multiple documents with the same salt
15660
15661 salt->salt_len += 32;
15662
15663 salt->salt_buf[ 4] = oldoffice34->encryptedVerifier[0];
15664 salt->salt_buf[ 5] = oldoffice34->encryptedVerifier[1];
15665 salt->salt_buf[ 6] = oldoffice34->encryptedVerifier[2];
15666 salt->salt_buf[ 7] = oldoffice34->encryptedVerifier[3];
15667 salt->salt_buf[ 8] = oldoffice34->encryptedVerifierHash[0];
15668 salt->salt_buf[ 9] = oldoffice34->encryptedVerifierHash[1];
15669 salt->salt_buf[10] = oldoffice34->encryptedVerifierHash[2];
15670 salt->salt_buf[11] = oldoffice34->encryptedVerifierHash[3];
15671
15672 /**
15673 * digest
15674 */
15675
15676 digest[0] = oldoffice34->encryptedVerifierHash[0];
15677 digest[1] = oldoffice34->encryptedVerifierHash[1];
15678 digest[2] = oldoffice34->encryptedVerifierHash[2];
15679 digest[3] = oldoffice34->encryptedVerifierHash[3];
15680
15681 return (PARSER_OK);
15682 }
15683
15684 int oldoffice34cm1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15685 {
15686 if (memcmp (SIGNATURE_OLDOFFICE3, input_buf, 12)) return (PARSER_SIGNATURE_UNMATCHED);
15687
15688 return oldoffice34_parse_hash (input_buf, input_len, hash_buf);
15689 }
15690
15691 int oldoffice34cm2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15692 {
15693 if ((input_len < DISPLAY_LEN_MIN_9820) || (input_len > DISPLAY_LEN_MAX_9820)) return (PARSER_GLOBAL_LENGTH);
15694
15695 if (memcmp (SIGNATURE_OLDOFFICE3, input_buf, 12)) return (PARSER_SIGNATURE_UNMATCHED);
15696
15697 u32 *digest = (u32 *) hash_buf->digest;
15698
15699 salt_t *salt = hash_buf->salt;
15700
15701 oldoffice34_t *oldoffice34 = (oldoffice34_t *) hash_buf->esalt;
15702
15703 /**
15704 * parse line
15705 */
15706
15707 char *version_pos = input_buf + 11;
15708
15709 char *osalt_pos = strchr (version_pos, '*');
15710
15711 if (osalt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15712
15713 u32 version_len = osalt_pos - version_pos;
15714
15715 if (version_len != 1) return (PARSER_SALT_LENGTH);
15716
15717 osalt_pos++;
15718
15719 char *encryptedVerifier_pos = strchr (osalt_pos, '*');
15720
15721 if (encryptedVerifier_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15722
15723 u32 osalt_len = encryptedVerifier_pos - osalt_pos;
15724
15725 if (osalt_len != 32) return (PARSER_SALT_LENGTH);
15726
15727 encryptedVerifier_pos++;
15728
15729 char *encryptedVerifierHash_pos = strchr (encryptedVerifier_pos, '*');
15730
15731 if (encryptedVerifierHash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15732
15733 u32 encryptedVerifier_len = encryptedVerifierHash_pos - encryptedVerifier_pos;
15734
15735 if (encryptedVerifier_len != 32) return (PARSER_SALT_LENGTH);
15736
15737 encryptedVerifierHash_pos++;
15738
15739 char *rc4key_pos = strchr (encryptedVerifierHash_pos, ':');
15740
15741 if (rc4key_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15742
15743 u32 encryptedVerifierHash_len = rc4key_pos - encryptedVerifierHash_pos;
15744
15745 if (encryptedVerifierHash_len != 40) return (PARSER_SALT_LENGTH);
15746
15747 rc4key_pos++;
15748
15749 u32 rc4key_len = input_len - 11 - version_len - 1 - osalt_len - 1 - encryptedVerifier_len - 1 - encryptedVerifierHash_len - 1;
15750
15751 if (rc4key_len != 10) return (PARSER_SALT_LENGTH);
15752
15753 const uint version = *version_pos - 0x30;
15754
15755 if (version != 3 && version != 4) return (PARSER_SALT_VALUE);
15756
15757 /**
15758 * esalt
15759 */
15760
15761 oldoffice34->version = version;
15762
15763 oldoffice34->encryptedVerifier[0] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 0]);
15764 oldoffice34->encryptedVerifier[1] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[ 8]);
15765 oldoffice34->encryptedVerifier[2] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[16]);
15766 oldoffice34->encryptedVerifier[3] = hex_to_u32 ((const u8 *) &encryptedVerifier_pos[24]);
15767
15768 oldoffice34->encryptedVerifier[0] = byte_swap_32 (oldoffice34->encryptedVerifier[0]);
15769 oldoffice34->encryptedVerifier[1] = byte_swap_32 (oldoffice34->encryptedVerifier[1]);
15770 oldoffice34->encryptedVerifier[2] = byte_swap_32 (oldoffice34->encryptedVerifier[2]);
15771 oldoffice34->encryptedVerifier[3] = byte_swap_32 (oldoffice34->encryptedVerifier[3]);
15772
15773 oldoffice34->encryptedVerifierHash[0] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 0]);
15774 oldoffice34->encryptedVerifierHash[1] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[ 8]);
15775 oldoffice34->encryptedVerifierHash[2] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[16]);
15776 oldoffice34->encryptedVerifierHash[3] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[24]);
15777 oldoffice34->encryptedVerifierHash[4] = hex_to_u32 ((const u8 *) &encryptedVerifierHash_pos[32]);
15778
15779 oldoffice34->encryptedVerifierHash[0] = byte_swap_32 (oldoffice34->encryptedVerifierHash[0]);
15780 oldoffice34->encryptedVerifierHash[1] = byte_swap_32 (oldoffice34->encryptedVerifierHash[1]);
15781 oldoffice34->encryptedVerifierHash[2] = byte_swap_32 (oldoffice34->encryptedVerifierHash[2]);
15782 oldoffice34->encryptedVerifierHash[3] = byte_swap_32 (oldoffice34->encryptedVerifierHash[3]);
15783 oldoffice34->encryptedVerifierHash[4] = byte_swap_32 (oldoffice34->encryptedVerifierHash[4]);
15784
15785 oldoffice34->rc4key[1] = 0;
15786 oldoffice34->rc4key[0] = 0;
15787
15788 oldoffice34->rc4key[0] |= hex_convert (rc4key_pos[0]) << 28;
15789 oldoffice34->rc4key[0] |= hex_convert (rc4key_pos[1]) << 24;
15790 oldoffice34->rc4key[0] |= hex_convert (rc4key_pos[2]) << 20;
15791 oldoffice34->rc4key[0] |= hex_convert (rc4key_pos[3]) << 16;
15792 oldoffice34->rc4key[0] |= hex_convert (rc4key_pos[4]) << 12;
15793 oldoffice34->rc4key[0] |= hex_convert (rc4key_pos[5]) << 8;
15794 oldoffice34->rc4key[0] |= hex_convert (rc4key_pos[6]) << 4;
15795 oldoffice34->rc4key[0] |= hex_convert (rc4key_pos[7]) << 0;
15796 oldoffice34->rc4key[1] |= hex_convert (rc4key_pos[8]) << 28;
15797 oldoffice34->rc4key[1] |= hex_convert (rc4key_pos[9]) << 24;
15798
15799 oldoffice34->rc4key[0] = byte_swap_32 (oldoffice34->rc4key[0]);
15800 oldoffice34->rc4key[1] = byte_swap_32 (oldoffice34->rc4key[1]);
15801
15802 /**
15803 * salt
15804 */
15805
15806 salt->salt_len = 16;
15807
15808 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &osalt_pos[ 0]);
15809 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &osalt_pos[ 8]);
15810 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &osalt_pos[16]);
15811 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &osalt_pos[24]);
15812
15813 // this is a workaround as office produces multiple documents with the same salt
15814
15815 salt->salt_len += 32;
15816
15817 salt->salt_buf[ 4] = oldoffice34->encryptedVerifier[0];
15818 salt->salt_buf[ 5] = oldoffice34->encryptedVerifier[1];
15819 salt->salt_buf[ 6] = oldoffice34->encryptedVerifier[2];
15820 salt->salt_buf[ 7] = oldoffice34->encryptedVerifier[3];
15821 salt->salt_buf[ 8] = oldoffice34->encryptedVerifierHash[0];
15822 salt->salt_buf[ 9] = oldoffice34->encryptedVerifierHash[1];
15823 salt->salt_buf[10] = oldoffice34->encryptedVerifierHash[2];
15824 salt->salt_buf[11] = oldoffice34->encryptedVerifierHash[3];
15825
15826 /**
15827 * digest
15828 */
15829
15830 digest[0] = oldoffice34->rc4key[0];
15831 digest[1] = oldoffice34->rc4key[1];
15832 digest[2] = 0;
15833 digest[3] = 0;
15834
15835 return (PARSER_OK);
15836 }
15837
15838 int radmin2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15839 {
15840 if ((input_len < DISPLAY_LEN_MIN_9900) || (input_len > DISPLAY_LEN_MAX_9900)) return (PARSER_GLOBAL_LENGTH);
15841
15842 u32 *digest = (u32 *) hash_buf->digest;
15843
15844 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
15845 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
15846 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
15847 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
15848
15849 digest[0] = byte_swap_32 (digest[0]);
15850 digest[1] = byte_swap_32 (digest[1]);
15851 digest[2] = byte_swap_32 (digest[2]);
15852 digest[3] = byte_swap_32 (digest[3]);
15853
15854 return (PARSER_OK);
15855 }
15856
15857 int djangosha1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15858 {
15859 if ((input_len < DISPLAY_LEN_MIN_124) || (input_len > DISPLAY_LEN_MAX_124)) return (PARSER_GLOBAL_LENGTH);
15860
15861 if ((memcmp (SIGNATURE_DJANGOSHA1, input_buf, 5)) && (memcmp (SIGNATURE_DJANGOSHA1, input_buf, 5))) return (PARSER_SIGNATURE_UNMATCHED);
15862
15863 u32 *digest = (u32 *) hash_buf->digest;
15864
15865 salt_t *salt = hash_buf->salt;
15866
15867 char *signature_pos = input_buf;
15868
15869 char *salt_pos = strchr (signature_pos, '$');
15870
15871 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15872
15873 u32 signature_len = salt_pos - signature_pos;
15874
15875 if (signature_len != 4) return (PARSER_SIGNATURE_UNMATCHED);
15876
15877 salt_pos++;
15878
15879 char *hash_pos = strchr (salt_pos, '$');
15880
15881 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15882
15883 u32 salt_len = hash_pos - salt_pos;
15884
15885 if (salt_len > 32) return (PARSER_SALT_LENGTH);
15886
15887 hash_pos++;
15888
15889 u32 hash_len = input_len - signature_len - 1 - salt_len - 1;
15890
15891 if (hash_len != 40) return (PARSER_SALT_LENGTH);
15892
15893 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
15894 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
15895 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
15896 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
15897 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
15898
15899 digest[0] -= SHA1M_A;
15900 digest[1] -= SHA1M_B;
15901 digest[2] -= SHA1M_C;
15902 digest[3] -= SHA1M_D;
15903 digest[4] -= SHA1M_E;
15904
15905 char *salt_buf_ptr = (char *) salt->salt_buf;
15906
15907 memcpy (salt_buf_ptr, salt_pos, salt_len);
15908
15909 salt->salt_len = salt_len;
15910
15911 return (PARSER_OK);
15912 }
15913
15914 int djangopbkdf2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15915 {
15916 if ((input_len < DISPLAY_LEN_MIN_10000) || (input_len > DISPLAY_LEN_MAX_10000)) return (PARSER_GLOBAL_LENGTH);
15917
15918 if (memcmp (SIGNATURE_DJANGOPBKDF2, input_buf, 14)) return (PARSER_SIGNATURE_UNMATCHED);
15919
15920 u32 *digest = (u32 *) hash_buf->digest;
15921
15922 salt_t *salt = hash_buf->salt;
15923
15924 pbkdf2_sha256_t *pbkdf2_sha256 = (pbkdf2_sha256_t *) hash_buf->esalt;
15925
15926 /**
15927 * parse line
15928 */
15929
15930 char *iter_pos = input_buf + 14;
15931
15932 const int iter = atoi (iter_pos);
15933
15934 if (iter < 1) return (PARSER_SALT_ITERATION);
15935
15936 salt->salt_iter = iter - 1;
15937
15938 char *salt_pos = strchr (iter_pos, '$');
15939
15940 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15941
15942 salt_pos++;
15943
15944 char *hash_pos = strchr (salt_pos, '$');
15945
15946 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
15947
15948 const uint salt_len = hash_pos - salt_pos;
15949
15950 hash_pos++;
15951
15952 char *salt_buf_ptr = (char *) pbkdf2_sha256->salt_buf;
15953
15954 memcpy (salt_buf_ptr, salt_pos, salt_len);
15955
15956 salt->salt_len = salt_len;
15957
15958 salt_buf_ptr[salt_len + 3] = 0x01;
15959 salt_buf_ptr[salt_len + 4] = 0x80;
15960
15961 // add some stuff to normal salt to make sorted happy
15962
15963 salt->salt_buf[0] = pbkdf2_sha256->salt_buf[0];
15964 salt->salt_buf[1] = pbkdf2_sha256->salt_buf[1];
15965 salt->salt_buf[2] = pbkdf2_sha256->salt_buf[2];
15966 salt->salt_buf[3] = pbkdf2_sha256->salt_buf[3];
15967 salt->salt_buf[4] = salt->salt_iter;
15968
15969 // base64 decode hash
15970
15971 u8 tmp_buf[100] = { 0 };
15972
15973 uint hash_len = input_len - (hash_pos - input_buf);
15974
15975 if (hash_len != 44) return (PARSER_HASH_LENGTH);
15976
15977 base64_decode (base64_to_int, (const u8 *) hash_pos, hash_len, tmp_buf);
15978
15979 memcpy (digest, tmp_buf, 32);
15980
15981 digest[0] = byte_swap_32 (digest[0]);
15982 digest[1] = byte_swap_32 (digest[1]);
15983 digest[2] = byte_swap_32 (digest[2]);
15984 digest[3] = byte_swap_32 (digest[3]);
15985 digest[4] = byte_swap_32 (digest[4]);
15986 digest[5] = byte_swap_32 (digest[5]);
15987 digest[6] = byte_swap_32 (digest[6]);
15988 digest[7] = byte_swap_32 (digest[7]);
15989
15990 return (PARSER_OK);
15991 }
15992
15993 int siphash_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
15994 {
15995 if ((input_len < DISPLAY_LEN_MIN_10100) || (input_len > DISPLAY_LEN_MAX_10100)) return (PARSER_GLOBAL_LENGTH);
15996
15997 u32 *digest = (u32 *) hash_buf->digest;
15998
15999 salt_t *salt = hash_buf->salt;
16000
16001 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
16002 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
16003 digest[2] = 0;
16004 digest[3] = 0;
16005
16006 digest[0] = byte_swap_32 (digest[0]);
16007 digest[1] = byte_swap_32 (digest[1]);
16008
16009 if (input_buf[16] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
16010 if (input_buf[18] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
16011 if (input_buf[20] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
16012
16013 char iter_c = input_buf[17];
16014 char iter_d = input_buf[19];
16015
16016 // atm only defaults, let's see if there's more request
16017 if (iter_c != '2') return (PARSER_SALT_ITERATION);
16018 if (iter_d != '4') return (PARSER_SALT_ITERATION);
16019
16020 char *salt_buf = input_buf + 16 + 1 + 1 + 1 + 1 + 1;
16021
16022 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_buf[ 0]);
16023 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_buf[ 8]);
16024 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &salt_buf[16]);
16025 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &salt_buf[24]);
16026
16027 salt->salt_buf[0] = byte_swap_32 (salt->salt_buf[0]);
16028 salt->salt_buf[1] = byte_swap_32 (salt->salt_buf[1]);
16029 salt->salt_buf[2] = byte_swap_32 (salt->salt_buf[2]);
16030 salt->salt_buf[3] = byte_swap_32 (salt->salt_buf[3]);
16031
16032 salt->salt_len = 16;
16033
16034 return (PARSER_OK);
16035 }
16036
16037 int crammd5_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16038 {
16039 if ((input_len < DISPLAY_LEN_MIN_10200) || (input_len > DISPLAY_LEN_MAX_10200)) return (PARSER_GLOBAL_LENGTH);
16040
16041 if (memcmp (SIGNATURE_CRAM_MD5, input_buf, 10)) return (PARSER_SIGNATURE_UNMATCHED);
16042
16043 u32 *digest = (u32 *) hash_buf->digest;
16044
16045 cram_md5_t *cram_md5 = (cram_md5_t *) hash_buf->esalt;
16046
16047 salt_t *salt = hash_buf->salt;
16048
16049 char *salt_pos = input_buf + 10;
16050
16051 char *hash_pos = strchr (salt_pos, '$');
16052
16053 uint salt_len = hash_pos - salt_pos;
16054
16055 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16056
16057 hash_pos++;
16058
16059 uint hash_len = input_len - 10 - salt_len - 1;
16060
16061 // base64 decode salt
16062
16063 u8 tmp_buf[100] = { 0 };
16064
16065 salt_len = base64_decode (base64_to_int, (const u8 *) salt_pos, salt_len, tmp_buf);
16066
16067 if (salt_len > 55) return (PARSER_SALT_LENGTH);
16068
16069 tmp_buf[salt_len] = 0x80;
16070
16071 memcpy (&salt->salt_buf, tmp_buf, salt_len + 1);
16072
16073 salt->salt_len = salt_len;
16074
16075 // base64 decode salt
16076
16077 memset (tmp_buf, 0, sizeof (tmp_buf));
16078
16079 hash_len = base64_decode (base64_to_int, (const u8 *) hash_pos, hash_len, tmp_buf);
16080
16081 uint user_len = hash_len - 32;
16082
16083 const u8 *tmp_hash = tmp_buf + user_len;
16084
16085 user_len--; // skip the trailing space
16086
16087 digest[0] = hex_to_u32 (&tmp_hash[ 0]);
16088 digest[1] = hex_to_u32 (&tmp_hash[ 8]);
16089 digest[2] = hex_to_u32 (&tmp_hash[16]);
16090 digest[3] = hex_to_u32 (&tmp_hash[24]);
16091
16092 digest[0] = byte_swap_32 (digest[0]);
16093 digest[1] = byte_swap_32 (digest[1]);
16094 digest[2] = byte_swap_32 (digest[2]);
16095 digest[3] = byte_swap_32 (digest[3]);
16096
16097 // store username for host only (output hash if cracked)
16098
16099 memset (cram_md5->user, 0, sizeof (cram_md5->user));
16100 memcpy (cram_md5->user, tmp_buf, user_len);
16101
16102 return (PARSER_OK);
16103 }
16104
16105 int saph_sha1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16106 {
16107 if ((input_len < DISPLAY_LEN_MIN_10300) || (input_len > DISPLAY_LEN_MAX_10300)) return (PARSER_GLOBAL_LENGTH);
16108
16109 if (memcmp (SIGNATURE_SAPH_SHA1, input_buf, 10)) return (PARSER_SIGNATURE_UNMATCHED);
16110
16111 u32 *digest = (u32 *) hash_buf->digest;
16112
16113 salt_t *salt = hash_buf->salt;
16114
16115 char *iter_pos = input_buf + 10;
16116
16117 u32 iter = atoi (iter_pos);
16118
16119 if (iter < 1)
16120 {
16121 return (PARSER_SALT_ITERATION);
16122 }
16123
16124 iter--; // first iteration is special
16125
16126 salt->salt_iter = iter;
16127
16128 char *base64_pos = strchr (iter_pos, '}');
16129
16130 if (base64_pos == NULL)
16131 {
16132 return (PARSER_SIGNATURE_UNMATCHED);
16133 }
16134
16135 base64_pos++;
16136
16137 // base64 decode salt
16138
16139 u32 base64_len = input_len - (base64_pos - input_buf);
16140
16141 u8 tmp_buf[100] = { 0 };
16142
16143 u32 decoded_len = base64_decode (base64_to_int, (const u8 *) base64_pos, base64_len, tmp_buf);
16144
16145 if (decoded_len < 24)
16146 {
16147 return (PARSER_SALT_LENGTH);
16148 }
16149
16150 // copy the salt
16151
16152 uint salt_len = decoded_len - 20;
16153
16154 if (salt_len < 4) return (PARSER_SALT_LENGTH);
16155 if (salt_len > 16) return (PARSER_SALT_LENGTH);
16156
16157 memcpy (&salt->salt_buf, tmp_buf + 20, salt_len);
16158
16159 salt->salt_len = salt_len;
16160
16161 // set digest
16162
16163 u32 *digest_ptr = (u32*) tmp_buf;
16164
16165 digest[0] = byte_swap_32 (digest_ptr[0]);
16166 digest[1] = byte_swap_32 (digest_ptr[1]);
16167 digest[2] = byte_swap_32 (digest_ptr[2]);
16168 digest[3] = byte_swap_32 (digest_ptr[3]);
16169 digest[4] = byte_swap_32 (digest_ptr[4]);
16170
16171 return (PARSER_OK);
16172 }
16173
16174 int redmine_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16175 {
16176 if ((input_len < DISPLAY_LEN_MIN_7600) || (input_len > DISPLAY_LEN_MAX_7600)) return (PARSER_GLOBAL_LENGTH);
16177
16178 u32 *digest = (u32 *) hash_buf->digest;
16179
16180 salt_t *salt = hash_buf->salt;
16181
16182 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
16183 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
16184 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
16185 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
16186 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
16187
16188 if (input_buf[40] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
16189
16190 uint salt_len = input_len - 40 - 1;
16191
16192 char *salt_buf = input_buf + 40 + 1;
16193
16194 char *salt_buf_ptr = (char *) salt->salt_buf;
16195
16196 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
16197
16198 if (salt_len != 32) return (PARSER_SALT_LENGTH);
16199
16200 salt->salt_len = salt_len;
16201
16202 return (PARSER_OK);
16203 }
16204
16205 int pdf11_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16206 {
16207 if ((input_len < DISPLAY_LEN_MIN_10400) || (input_len > DISPLAY_LEN_MAX_10400)) return (PARSER_GLOBAL_LENGTH);
16208
16209 if ((memcmp (SIGNATURE_PDF, input_buf, 5)) && (memcmp (SIGNATURE_PDF, input_buf, 5))) return (PARSER_SIGNATURE_UNMATCHED);
16210
16211 u32 *digest = (u32 *) hash_buf->digest;
16212
16213 salt_t *salt = hash_buf->salt;
16214
16215 pdf_t *pdf = (pdf_t *) hash_buf->esalt;
16216
16217 /**
16218 * parse line
16219 */
16220
16221 char *V_pos = input_buf + 5;
16222
16223 char *R_pos = strchr (V_pos, '*');
16224
16225 if (R_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16226
16227 u32 V_len = R_pos - V_pos;
16228
16229 R_pos++;
16230
16231 char *bits_pos = strchr (R_pos, '*');
16232
16233 if (bits_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16234
16235 u32 R_len = bits_pos - R_pos;
16236
16237 bits_pos++;
16238
16239 char *P_pos = strchr (bits_pos, '*');
16240
16241 if (P_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16242
16243 u32 bits_len = P_pos - bits_pos;
16244
16245 P_pos++;
16246
16247 char *enc_md_pos = strchr (P_pos, '*');
16248
16249 if (enc_md_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16250
16251 u32 P_len = enc_md_pos - P_pos;
16252
16253 enc_md_pos++;
16254
16255 char *id_len_pos = strchr (enc_md_pos, '*');
16256
16257 if (id_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16258
16259 u32 enc_md_len = id_len_pos - enc_md_pos;
16260
16261 id_len_pos++;
16262
16263 char *id_buf_pos = strchr (id_len_pos, '*');
16264
16265 if (id_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16266
16267 u32 id_len_len = id_buf_pos - id_len_pos;
16268
16269 id_buf_pos++;
16270
16271 char *u_len_pos = strchr (id_buf_pos, '*');
16272
16273 if (u_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16274
16275 u32 id_buf_len = u_len_pos - id_buf_pos;
16276
16277 if (id_buf_len != 32) return (PARSER_SALT_LENGTH);
16278
16279 u_len_pos++;
16280
16281 char *u_buf_pos = strchr (u_len_pos, '*');
16282
16283 if (u_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16284
16285 u32 u_len_len = u_buf_pos - u_len_pos;
16286
16287 u_buf_pos++;
16288
16289 char *o_len_pos = strchr (u_buf_pos, '*');
16290
16291 if (o_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16292
16293 u32 u_buf_len = o_len_pos - u_buf_pos;
16294
16295 if (u_buf_len != 64) return (PARSER_SALT_LENGTH);
16296
16297 o_len_pos++;
16298
16299 char *o_buf_pos = strchr (o_len_pos, '*');
16300
16301 if (o_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16302
16303 u32 o_len_len = o_buf_pos - o_len_pos;
16304
16305 o_buf_pos++;
16306
16307 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;
16308
16309 if (o_buf_len != 64) return (PARSER_SALT_LENGTH);
16310
16311 // validate data
16312
16313 const int V = atoi (V_pos);
16314 const int R = atoi (R_pos);
16315 const int P = atoi (P_pos);
16316
16317 if (V != 1) return (PARSER_SALT_VALUE);
16318 if (R != 2) return (PARSER_SALT_VALUE);
16319
16320 const int enc_md = atoi (enc_md_pos);
16321
16322 if ((enc_md != 0) && (enc_md != 1)) return (PARSER_SALT_VALUE);
16323
16324 const int id_len = atoi (id_len_pos);
16325 const int u_len = atoi (u_len_pos);
16326 const int o_len = atoi (o_len_pos);
16327
16328 if (id_len != 16) return (PARSER_SALT_VALUE);
16329 if (u_len != 32) return (PARSER_SALT_VALUE);
16330 if (o_len != 32) return (PARSER_SALT_VALUE);
16331
16332 const int bits = atoi (bits_pos);
16333
16334 if (bits != 40) return (PARSER_SALT_VALUE);
16335
16336 // copy data to esalt
16337
16338 pdf->V = V;
16339 pdf->R = R;
16340 pdf->P = P;
16341
16342 pdf->enc_md = enc_md;
16343
16344 pdf->id_buf[0] = hex_to_u32 ((const u8 *) &id_buf_pos[ 0]);
16345 pdf->id_buf[1] = hex_to_u32 ((const u8 *) &id_buf_pos[ 8]);
16346 pdf->id_buf[2] = hex_to_u32 ((const u8 *) &id_buf_pos[16]);
16347 pdf->id_buf[3] = hex_to_u32 ((const u8 *) &id_buf_pos[24]);
16348 pdf->id_len = id_len;
16349
16350 pdf->u_buf[0] = hex_to_u32 ((const u8 *) &u_buf_pos[ 0]);
16351 pdf->u_buf[1] = hex_to_u32 ((const u8 *) &u_buf_pos[ 8]);
16352 pdf->u_buf[2] = hex_to_u32 ((const u8 *) &u_buf_pos[16]);
16353 pdf->u_buf[3] = hex_to_u32 ((const u8 *) &u_buf_pos[24]);
16354 pdf->u_buf[4] = hex_to_u32 ((const u8 *) &u_buf_pos[32]);
16355 pdf->u_buf[5] = hex_to_u32 ((const u8 *) &u_buf_pos[40]);
16356 pdf->u_buf[6] = hex_to_u32 ((const u8 *) &u_buf_pos[48]);
16357 pdf->u_buf[7] = hex_to_u32 ((const u8 *) &u_buf_pos[56]);
16358 pdf->u_len = u_len;
16359
16360 pdf->o_buf[0] = hex_to_u32 ((const u8 *) &o_buf_pos[ 0]);
16361 pdf->o_buf[1] = hex_to_u32 ((const u8 *) &o_buf_pos[ 8]);
16362 pdf->o_buf[2] = hex_to_u32 ((const u8 *) &o_buf_pos[16]);
16363 pdf->o_buf[3] = hex_to_u32 ((const u8 *) &o_buf_pos[24]);
16364 pdf->o_buf[4] = hex_to_u32 ((const u8 *) &o_buf_pos[32]);
16365 pdf->o_buf[5] = hex_to_u32 ((const u8 *) &o_buf_pos[40]);
16366 pdf->o_buf[6] = hex_to_u32 ((const u8 *) &o_buf_pos[48]);
16367 pdf->o_buf[7] = hex_to_u32 ((const u8 *) &o_buf_pos[56]);
16368 pdf->o_len = o_len;
16369
16370 pdf->id_buf[0] = byte_swap_32 (pdf->id_buf[0]);
16371 pdf->id_buf[1] = byte_swap_32 (pdf->id_buf[1]);
16372 pdf->id_buf[2] = byte_swap_32 (pdf->id_buf[2]);
16373 pdf->id_buf[3] = byte_swap_32 (pdf->id_buf[3]);
16374
16375 pdf->u_buf[0] = byte_swap_32 (pdf->u_buf[0]);
16376 pdf->u_buf[1] = byte_swap_32 (pdf->u_buf[1]);
16377 pdf->u_buf[2] = byte_swap_32 (pdf->u_buf[2]);
16378 pdf->u_buf[3] = byte_swap_32 (pdf->u_buf[3]);
16379 pdf->u_buf[4] = byte_swap_32 (pdf->u_buf[4]);
16380 pdf->u_buf[5] = byte_swap_32 (pdf->u_buf[5]);
16381 pdf->u_buf[6] = byte_swap_32 (pdf->u_buf[6]);
16382 pdf->u_buf[7] = byte_swap_32 (pdf->u_buf[7]);
16383
16384 pdf->o_buf[0] = byte_swap_32 (pdf->o_buf[0]);
16385 pdf->o_buf[1] = byte_swap_32 (pdf->o_buf[1]);
16386 pdf->o_buf[2] = byte_swap_32 (pdf->o_buf[2]);
16387 pdf->o_buf[3] = byte_swap_32 (pdf->o_buf[3]);
16388 pdf->o_buf[4] = byte_swap_32 (pdf->o_buf[4]);
16389 pdf->o_buf[5] = byte_swap_32 (pdf->o_buf[5]);
16390 pdf->o_buf[6] = byte_swap_32 (pdf->o_buf[6]);
16391 pdf->o_buf[7] = byte_swap_32 (pdf->o_buf[7]);
16392
16393 // we use ID for salt, maybe needs to change, we will see...
16394
16395 salt->salt_buf[0] = pdf->id_buf[0];
16396 salt->salt_buf[1] = pdf->id_buf[1];
16397 salt->salt_buf[2] = pdf->id_buf[2];
16398 salt->salt_buf[3] = pdf->id_buf[3];
16399 salt->salt_len = pdf->id_len;
16400
16401 digest[0] = pdf->u_buf[0];
16402 digest[1] = pdf->u_buf[1];
16403 digest[2] = pdf->u_buf[2];
16404 digest[3] = pdf->u_buf[3];
16405
16406 return (PARSER_OK);
16407 }
16408
16409 int pdf11cm1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16410 {
16411 return pdf11_parse_hash (input_buf, input_len, hash_buf);
16412 }
16413
16414 int pdf11cm2_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16415 {
16416 if ((input_len < DISPLAY_LEN_MIN_10420) || (input_len > DISPLAY_LEN_MAX_10420)) return (PARSER_GLOBAL_LENGTH);
16417
16418 if ((memcmp (SIGNATURE_PDF, input_buf, 5)) && (memcmp (SIGNATURE_PDF, input_buf, 5))) return (PARSER_SIGNATURE_UNMATCHED);
16419
16420 u32 *digest = (u32 *) hash_buf->digest;
16421
16422 salt_t *salt = hash_buf->salt;
16423
16424 pdf_t *pdf = (pdf_t *) hash_buf->esalt;
16425
16426 /**
16427 * parse line
16428 */
16429
16430 char *V_pos = input_buf + 5;
16431
16432 char *R_pos = strchr (V_pos, '*');
16433
16434 if (R_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16435
16436 u32 V_len = R_pos - V_pos;
16437
16438 R_pos++;
16439
16440 char *bits_pos = strchr (R_pos, '*');
16441
16442 if (bits_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16443
16444 u32 R_len = bits_pos - R_pos;
16445
16446 bits_pos++;
16447
16448 char *P_pos = strchr (bits_pos, '*');
16449
16450 if (P_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16451
16452 u32 bits_len = P_pos - bits_pos;
16453
16454 P_pos++;
16455
16456 char *enc_md_pos = strchr (P_pos, '*');
16457
16458 if (enc_md_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16459
16460 u32 P_len = enc_md_pos - P_pos;
16461
16462 enc_md_pos++;
16463
16464 char *id_len_pos = strchr (enc_md_pos, '*');
16465
16466 if (id_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16467
16468 u32 enc_md_len = id_len_pos - enc_md_pos;
16469
16470 id_len_pos++;
16471
16472 char *id_buf_pos = strchr (id_len_pos, '*');
16473
16474 if (id_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16475
16476 u32 id_len_len = id_buf_pos - id_len_pos;
16477
16478 id_buf_pos++;
16479
16480 char *u_len_pos = strchr (id_buf_pos, '*');
16481
16482 if (u_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16483
16484 u32 id_buf_len = u_len_pos - id_buf_pos;
16485
16486 if (id_buf_len != 32) return (PARSER_SALT_LENGTH);
16487
16488 u_len_pos++;
16489
16490 char *u_buf_pos = strchr (u_len_pos, '*');
16491
16492 if (u_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16493
16494 u32 u_len_len = u_buf_pos - u_len_pos;
16495
16496 u_buf_pos++;
16497
16498 char *o_len_pos = strchr (u_buf_pos, '*');
16499
16500 if (o_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16501
16502 u32 u_buf_len = o_len_pos - u_buf_pos;
16503
16504 if (u_buf_len != 64) return (PARSER_SALT_LENGTH);
16505
16506 o_len_pos++;
16507
16508 char *o_buf_pos = strchr (o_len_pos, '*');
16509
16510 if (o_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16511
16512 u32 o_len_len = o_buf_pos - o_len_pos;
16513
16514 o_buf_pos++;
16515
16516 char *rc4key_pos = strchr (o_buf_pos, ':');
16517
16518 if (rc4key_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16519
16520 u32 o_buf_len = rc4key_pos - o_buf_pos;
16521
16522 if (o_buf_len != 64) return (PARSER_SALT_LENGTH);
16523
16524 rc4key_pos++;
16525
16526 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;
16527
16528 if (rc4key_len != 10) return (PARSER_SALT_LENGTH);
16529
16530 // validate data
16531
16532 const int V = atoi (V_pos);
16533 const int R = atoi (R_pos);
16534 const int P = atoi (P_pos);
16535
16536 if (V != 1) return (PARSER_SALT_VALUE);
16537 if (R != 2) return (PARSER_SALT_VALUE);
16538
16539 const int enc_md = atoi (enc_md_pos);
16540
16541 if ((enc_md != 0) && (enc_md != 1)) return (PARSER_SALT_VALUE);
16542
16543 const int id_len = atoi (id_len_pos);
16544 const int u_len = atoi (u_len_pos);
16545 const int o_len = atoi (o_len_pos);
16546
16547 if (id_len != 16) return (PARSER_SALT_VALUE);
16548 if (u_len != 32) return (PARSER_SALT_VALUE);
16549 if (o_len != 32) return (PARSER_SALT_VALUE);
16550
16551 const int bits = atoi (bits_pos);
16552
16553 if (bits != 40) return (PARSER_SALT_VALUE);
16554
16555 // copy data to esalt
16556
16557 pdf->V = V;
16558 pdf->R = R;
16559 pdf->P = P;
16560
16561 pdf->enc_md = enc_md;
16562
16563 pdf->id_buf[0] = hex_to_u32 ((const u8 *) &id_buf_pos[ 0]);
16564 pdf->id_buf[1] = hex_to_u32 ((const u8 *) &id_buf_pos[ 8]);
16565 pdf->id_buf[2] = hex_to_u32 ((const u8 *) &id_buf_pos[16]);
16566 pdf->id_buf[3] = hex_to_u32 ((const u8 *) &id_buf_pos[24]);
16567 pdf->id_len = id_len;
16568
16569 pdf->u_buf[0] = hex_to_u32 ((const u8 *) &u_buf_pos[ 0]);
16570 pdf->u_buf[1] = hex_to_u32 ((const u8 *) &u_buf_pos[ 8]);
16571 pdf->u_buf[2] = hex_to_u32 ((const u8 *) &u_buf_pos[16]);
16572 pdf->u_buf[3] = hex_to_u32 ((const u8 *) &u_buf_pos[24]);
16573 pdf->u_buf[4] = hex_to_u32 ((const u8 *) &u_buf_pos[32]);
16574 pdf->u_buf[5] = hex_to_u32 ((const u8 *) &u_buf_pos[40]);
16575 pdf->u_buf[6] = hex_to_u32 ((const u8 *) &u_buf_pos[48]);
16576 pdf->u_buf[7] = hex_to_u32 ((const u8 *) &u_buf_pos[56]);
16577 pdf->u_len = u_len;
16578
16579 pdf->o_buf[0] = hex_to_u32 ((const u8 *) &o_buf_pos[ 0]);
16580 pdf->o_buf[1] = hex_to_u32 ((const u8 *) &o_buf_pos[ 8]);
16581 pdf->o_buf[2] = hex_to_u32 ((const u8 *) &o_buf_pos[16]);
16582 pdf->o_buf[3] = hex_to_u32 ((const u8 *) &o_buf_pos[24]);
16583 pdf->o_buf[4] = hex_to_u32 ((const u8 *) &o_buf_pos[32]);
16584 pdf->o_buf[5] = hex_to_u32 ((const u8 *) &o_buf_pos[40]);
16585 pdf->o_buf[6] = hex_to_u32 ((const u8 *) &o_buf_pos[48]);
16586 pdf->o_buf[7] = hex_to_u32 ((const u8 *) &o_buf_pos[56]);
16587 pdf->o_len = o_len;
16588
16589 pdf->id_buf[0] = byte_swap_32 (pdf->id_buf[0]);
16590 pdf->id_buf[1] = byte_swap_32 (pdf->id_buf[1]);
16591 pdf->id_buf[2] = byte_swap_32 (pdf->id_buf[2]);
16592 pdf->id_buf[3] = byte_swap_32 (pdf->id_buf[3]);
16593
16594 pdf->u_buf[0] = byte_swap_32 (pdf->u_buf[0]);
16595 pdf->u_buf[1] = byte_swap_32 (pdf->u_buf[1]);
16596 pdf->u_buf[2] = byte_swap_32 (pdf->u_buf[2]);
16597 pdf->u_buf[3] = byte_swap_32 (pdf->u_buf[3]);
16598 pdf->u_buf[4] = byte_swap_32 (pdf->u_buf[4]);
16599 pdf->u_buf[5] = byte_swap_32 (pdf->u_buf[5]);
16600 pdf->u_buf[6] = byte_swap_32 (pdf->u_buf[6]);
16601 pdf->u_buf[7] = byte_swap_32 (pdf->u_buf[7]);
16602
16603 pdf->o_buf[0] = byte_swap_32 (pdf->o_buf[0]);
16604 pdf->o_buf[1] = byte_swap_32 (pdf->o_buf[1]);
16605 pdf->o_buf[2] = byte_swap_32 (pdf->o_buf[2]);
16606 pdf->o_buf[3] = byte_swap_32 (pdf->o_buf[3]);
16607 pdf->o_buf[4] = byte_swap_32 (pdf->o_buf[4]);
16608 pdf->o_buf[5] = byte_swap_32 (pdf->o_buf[5]);
16609 pdf->o_buf[6] = byte_swap_32 (pdf->o_buf[6]);
16610 pdf->o_buf[7] = byte_swap_32 (pdf->o_buf[7]);
16611
16612 pdf->rc4key[1] = 0;
16613 pdf->rc4key[0] = 0;
16614
16615 pdf->rc4key[0] |= hex_convert (rc4key_pos[0]) << 28;
16616 pdf->rc4key[0] |= hex_convert (rc4key_pos[1]) << 24;
16617 pdf->rc4key[0] |= hex_convert (rc4key_pos[2]) << 20;
16618 pdf->rc4key[0] |= hex_convert (rc4key_pos[3]) << 16;
16619 pdf->rc4key[0] |= hex_convert (rc4key_pos[4]) << 12;
16620 pdf->rc4key[0] |= hex_convert (rc4key_pos[5]) << 8;
16621 pdf->rc4key[0] |= hex_convert (rc4key_pos[6]) << 4;
16622 pdf->rc4key[0] |= hex_convert (rc4key_pos[7]) << 0;
16623 pdf->rc4key[1] |= hex_convert (rc4key_pos[8]) << 28;
16624 pdf->rc4key[1] |= hex_convert (rc4key_pos[9]) << 24;
16625
16626 pdf->rc4key[0] = byte_swap_32 (pdf->rc4key[0]);
16627 pdf->rc4key[1] = byte_swap_32 (pdf->rc4key[1]);
16628
16629 // we use ID for salt, maybe needs to change, we will see...
16630
16631 salt->salt_buf[0] = pdf->id_buf[0];
16632 salt->salt_buf[1] = pdf->id_buf[1];
16633 salt->salt_buf[2] = pdf->id_buf[2];
16634 salt->salt_buf[3] = pdf->id_buf[3];
16635 salt->salt_buf[4] = pdf->u_buf[0];
16636 salt->salt_buf[5] = pdf->u_buf[1];
16637 salt->salt_buf[6] = pdf->o_buf[0];
16638 salt->salt_buf[7] = pdf->o_buf[1];
16639 salt->salt_len = pdf->id_len + 16;
16640
16641 digest[0] = pdf->rc4key[0];
16642 digest[1] = pdf->rc4key[1];
16643 digest[2] = 0;
16644 digest[3] = 0;
16645
16646 return (PARSER_OK);
16647 }
16648
16649 int pdf14_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16650 {
16651 if ((input_len < DISPLAY_LEN_MIN_10500) || (input_len > DISPLAY_LEN_MAX_10500)) return (PARSER_GLOBAL_LENGTH);
16652
16653 if ((memcmp (SIGNATURE_PDF, input_buf, 5)) && (memcmp (SIGNATURE_PDF, input_buf, 5))) return (PARSER_SIGNATURE_UNMATCHED);
16654
16655 u32 *digest = (u32 *) hash_buf->digest;
16656
16657 salt_t *salt = hash_buf->salt;
16658
16659 pdf_t *pdf = (pdf_t *) hash_buf->esalt;
16660
16661 /**
16662 * parse line
16663 */
16664
16665 char *V_pos = input_buf + 5;
16666
16667 char *R_pos = strchr (V_pos, '*');
16668
16669 if (R_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16670
16671 u32 V_len = R_pos - V_pos;
16672
16673 R_pos++;
16674
16675 char *bits_pos = strchr (R_pos, '*');
16676
16677 if (bits_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16678
16679 u32 R_len = bits_pos - R_pos;
16680
16681 bits_pos++;
16682
16683 char *P_pos = strchr (bits_pos, '*');
16684
16685 if (P_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16686
16687 u32 bits_len = P_pos - bits_pos;
16688
16689 P_pos++;
16690
16691 char *enc_md_pos = strchr (P_pos, '*');
16692
16693 if (enc_md_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16694
16695 u32 P_len = enc_md_pos - P_pos;
16696
16697 enc_md_pos++;
16698
16699 char *id_len_pos = strchr (enc_md_pos, '*');
16700
16701 if (id_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16702
16703 u32 enc_md_len = id_len_pos - enc_md_pos;
16704
16705 id_len_pos++;
16706
16707 char *id_buf_pos = strchr (id_len_pos, '*');
16708
16709 if (id_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16710
16711 u32 id_len_len = id_buf_pos - id_len_pos;
16712
16713 id_buf_pos++;
16714
16715 char *u_len_pos = strchr (id_buf_pos, '*');
16716
16717 if (u_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16718
16719 u32 id_buf_len = u_len_pos - id_buf_pos;
16720
16721 if ((id_buf_len != 32) && (id_buf_len != 64)) return (PARSER_SALT_LENGTH);
16722
16723 u_len_pos++;
16724
16725 char *u_buf_pos = strchr (u_len_pos, '*');
16726
16727 if (u_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16728
16729 u32 u_len_len = u_buf_pos - u_len_pos;
16730
16731 u_buf_pos++;
16732
16733 char *o_len_pos = strchr (u_buf_pos, '*');
16734
16735 if (o_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16736
16737 u32 u_buf_len = o_len_pos - u_buf_pos;
16738
16739 if (u_buf_len != 64) return (PARSER_SALT_LENGTH);
16740
16741 o_len_pos++;
16742
16743 char *o_buf_pos = strchr (o_len_pos, '*');
16744
16745 if (o_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16746
16747 u32 o_len_len = o_buf_pos - o_len_pos;
16748
16749 o_buf_pos++;
16750
16751 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;
16752
16753 if (o_buf_len != 64) return (PARSER_SALT_LENGTH);
16754
16755 // validate data
16756
16757 const int V = atoi (V_pos);
16758 const int R = atoi (R_pos);
16759 const int P = atoi (P_pos);
16760
16761 int vr_ok = 0;
16762
16763 if ((V == 2) && (R == 3)) vr_ok = 1;
16764 if ((V == 4) && (R == 4)) vr_ok = 1;
16765
16766 if (vr_ok == 0) return (PARSER_SALT_VALUE);
16767
16768 const int id_len = atoi (id_len_pos);
16769 const int u_len = atoi (u_len_pos);
16770 const int o_len = atoi (o_len_pos);
16771
16772 if ((id_len != 16) && (id_len != 32)) return (PARSER_SALT_VALUE);
16773
16774 if (u_len != 32) return (PARSER_SALT_VALUE);
16775 if (o_len != 32) return (PARSER_SALT_VALUE);
16776
16777 const int bits = atoi (bits_pos);
16778
16779 if (bits != 128) return (PARSER_SALT_VALUE);
16780
16781 int enc_md = 1;
16782
16783 if (R >= 4)
16784 {
16785 enc_md = atoi (enc_md_pos);
16786 }
16787
16788 // copy data to esalt
16789
16790 pdf->V = V;
16791 pdf->R = R;
16792 pdf->P = P;
16793
16794 pdf->enc_md = enc_md;
16795
16796 pdf->id_buf[0] = hex_to_u32 ((const u8 *) &id_buf_pos[ 0]);
16797 pdf->id_buf[1] = hex_to_u32 ((const u8 *) &id_buf_pos[ 8]);
16798 pdf->id_buf[2] = hex_to_u32 ((const u8 *) &id_buf_pos[16]);
16799 pdf->id_buf[3] = hex_to_u32 ((const u8 *) &id_buf_pos[24]);
16800
16801 if (id_len == 32)
16802 {
16803 pdf->id_buf[4] = hex_to_u32 ((const u8 *) &id_buf_pos[32]);
16804 pdf->id_buf[5] = hex_to_u32 ((const u8 *) &id_buf_pos[40]);
16805 pdf->id_buf[6] = hex_to_u32 ((const u8 *) &id_buf_pos[48]);
16806 pdf->id_buf[7] = hex_to_u32 ((const u8 *) &id_buf_pos[56]);
16807 }
16808
16809 pdf->id_len = id_len;
16810
16811 pdf->u_buf[0] = hex_to_u32 ((const u8 *) &u_buf_pos[ 0]);
16812 pdf->u_buf[1] = hex_to_u32 ((const u8 *) &u_buf_pos[ 8]);
16813 pdf->u_buf[2] = hex_to_u32 ((const u8 *) &u_buf_pos[16]);
16814 pdf->u_buf[3] = hex_to_u32 ((const u8 *) &u_buf_pos[24]);
16815 pdf->u_buf[4] = hex_to_u32 ((const u8 *) &u_buf_pos[32]);
16816 pdf->u_buf[5] = hex_to_u32 ((const u8 *) &u_buf_pos[40]);
16817 pdf->u_buf[6] = hex_to_u32 ((const u8 *) &u_buf_pos[48]);
16818 pdf->u_buf[7] = hex_to_u32 ((const u8 *) &u_buf_pos[56]);
16819 pdf->u_len = u_len;
16820
16821 pdf->o_buf[0] = hex_to_u32 ((const u8 *) &o_buf_pos[ 0]);
16822 pdf->o_buf[1] = hex_to_u32 ((const u8 *) &o_buf_pos[ 8]);
16823 pdf->o_buf[2] = hex_to_u32 ((const u8 *) &o_buf_pos[16]);
16824 pdf->o_buf[3] = hex_to_u32 ((const u8 *) &o_buf_pos[24]);
16825 pdf->o_buf[4] = hex_to_u32 ((const u8 *) &o_buf_pos[32]);
16826 pdf->o_buf[5] = hex_to_u32 ((const u8 *) &o_buf_pos[40]);
16827 pdf->o_buf[6] = hex_to_u32 ((const u8 *) &o_buf_pos[48]);
16828 pdf->o_buf[7] = hex_to_u32 ((const u8 *) &o_buf_pos[56]);
16829 pdf->o_len = o_len;
16830
16831 pdf->id_buf[0] = byte_swap_32 (pdf->id_buf[0]);
16832 pdf->id_buf[1] = byte_swap_32 (pdf->id_buf[1]);
16833 pdf->id_buf[2] = byte_swap_32 (pdf->id_buf[2]);
16834 pdf->id_buf[3] = byte_swap_32 (pdf->id_buf[3]);
16835
16836 if (id_len == 32)
16837 {
16838 pdf->id_buf[4] = byte_swap_32 (pdf->id_buf[4]);
16839 pdf->id_buf[5] = byte_swap_32 (pdf->id_buf[5]);
16840 pdf->id_buf[6] = byte_swap_32 (pdf->id_buf[6]);
16841 pdf->id_buf[7] = byte_swap_32 (pdf->id_buf[7]);
16842 }
16843
16844 pdf->u_buf[0] = byte_swap_32 (pdf->u_buf[0]);
16845 pdf->u_buf[1] = byte_swap_32 (pdf->u_buf[1]);
16846 pdf->u_buf[2] = byte_swap_32 (pdf->u_buf[2]);
16847 pdf->u_buf[3] = byte_swap_32 (pdf->u_buf[3]);
16848 pdf->u_buf[4] = byte_swap_32 (pdf->u_buf[4]);
16849 pdf->u_buf[5] = byte_swap_32 (pdf->u_buf[5]);
16850 pdf->u_buf[6] = byte_swap_32 (pdf->u_buf[6]);
16851 pdf->u_buf[7] = byte_swap_32 (pdf->u_buf[7]);
16852
16853 pdf->o_buf[0] = byte_swap_32 (pdf->o_buf[0]);
16854 pdf->o_buf[1] = byte_swap_32 (pdf->o_buf[1]);
16855 pdf->o_buf[2] = byte_swap_32 (pdf->o_buf[2]);
16856 pdf->o_buf[3] = byte_swap_32 (pdf->o_buf[3]);
16857 pdf->o_buf[4] = byte_swap_32 (pdf->o_buf[4]);
16858 pdf->o_buf[5] = byte_swap_32 (pdf->o_buf[5]);
16859 pdf->o_buf[6] = byte_swap_32 (pdf->o_buf[6]);
16860 pdf->o_buf[7] = byte_swap_32 (pdf->o_buf[7]);
16861
16862 // precompute rc4 data for later use
16863
16864 uint padding[8] =
16865 {
16866 0x5e4ebf28,
16867 0x418a754e,
16868 0x564e0064,
16869 0x0801faff,
16870 0xb6002e2e,
16871 0x803e68d0,
16872 0xfea90c2f,
16873 0x7a695364
16874 };
16875
16876 // md5
16877
16878 uint salt_pc_block[32] = { 0 };
16879
16880 char *salt_pc_ptr = (char *) salt_pc_block;
16881
16882 memcpy (salt_pc_ptr, padding, 32);
16883 memcpy (salt_pc_ptr + 32, pdf->id_buf, pdf->id_len);
16884
16885 uint salt_pc_digest[4] = { 0 };
16886
16887 md5_complete_no_limit (salt_pc_digest, salt_pc_block, 32 + pdf->id_len);
16888
16889 pdf->rc4data[0] = salt_pc_digest[0];
16890 pdf->rc4data[1] = salt_pc_digest[1];
16891
16892 // we use ID for salt, maybe needs to change, we will see...
16893
16894 salt->salt_buf[0] = pdf->id_buf[0];
16895 salt->salt_buf[1] = pdf->id_buf[1];
16896 salt->salt_buf[2] = pdf->id_buf[2];
16897 salt->salt_buf[3] = pdf->id_buf[3];
16898 salt->salt_buf[4] = pdf->u_buf[0];
16899 salt->salt_buf[5] = pdf->u_buf[1];
16900 salt->salt_buf[6] = pdf->o_buf[0];
16901 salt->salt_buf[7] = pdf->o_buf[1];
16902 salt->salt_len = pdf->id_len + 16;
16903
16904 salt->salt_iter = ROUNDS_PDF14;
16905
16906 digest[0] = pdf->u_buf[0];
16907 digest[1] = pdf->u_buf[1];
16908 digest[2] = 0;
16909 digest[3] = 0;
16910
16911 return (PARSER_OK);
16912 }
16913
16914 int pdf17l3_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16915 {
16916 int ret = pdf17l8_parse_hash (input_buf, input_len, hash_buf);
16917
16918 if (ret != PARSER_OK)
16919 {
16920 return ret;
16921 }
16922
16923 u32 *digest = (u32 *) hash_buf->digest;
16924
16925 salt_t *salt = hash_buf->salt;
16926
16927 digest[0] -= SHA256M_A;
16928 digest[1] -= SHA256M_B;
16929 digest[2] -= SHA256M_C;
16930 digest[3] -= SHA256M_D;
16931 digest[4] -= SHA256M_E;
16932 digest[5] -= SHA256M_F;
16933 digest[6] -= SHA256M_G;
16934 digest[7] -= SHA256M_H;
16935
16936 salt->salt_buf[2] = 0x80;
16937
16938 return (PARSER_OK);
16939 }
16940
16941 int pdf17l8_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
16942 {
16943 if ((input_len < DISPLAY_LEN_MIN_10600) || (input_len > DISPLAY_LEN_MAX_10600)) return (PARSER_GLOBAL_LENGTH);
16944
16945 if ((memcmp (SIGNATURE_PDF, input_buf, 5)) && (memcmp (SIGNATURE_PDF, input_buf, 5))) return (PARSER_SIGNATURE_UNMATCHED);
16946
16947 u32 *digest = (u32 *) hash_buf->digest;
16948
16949 salt_t *salt = hash_buf->salt;
16950
16951 pdf_t *pdf = (pdf_t *) hash_buf->esalt;
16952
16953 /**
16954 * parse line
16955 */
16956
16957 char *V_pos = input_buf + 5;
16958
16959 char *R_pos = strchr (V_pos, '*');
16960
16961 if (R_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16962
16963 u32 V_len = R_pos - V_pos;
16964
16965 R_pos++;
16966
16967 char *bits_pos = strchr (R_pos, '*');
16968
16969 if (bits_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16970
16971 u32 R_len = bits_pos - R_pos;
16972
16973 bits_pos++;
16974
16975 char *P_pos = strchr (bits_pos, '*');
16976
16977 if (P_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16978
16979 u32 bits_len = P_pos - bits_pos;
16980
16981 P_pos++;
16982
16983 char *enc_md_pos = strchr (P_pos, '*');
16984
16985 if (enc_md_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16986
16987 u32 P_len = enc_md_pos - P_pos;
16988
16989 enc_md_pos++;
16990
16991 char *id_len_pos = strchr (enc_md_pos, '*');
16992
16993 if (id_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
16994
16995 u32 enc_md_len = id_len_pos - enc_md_pos;
16996
16997 id_len_pos++;
16998
16999 char *id_buf_pos = strchr (id_len_pos, '*');
17000
17001 if (id_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17002
17003 u32 id_len_len = id_buf_pos - id_len_pos;
17004
17005 id_buf_pos++;
17006
17007 char *u_len_pos = strchr (id_buf_pos, '*');
17008
17009 if (u_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17010
17011 u32 id_buf_len = u_len_pos - id_buf_pos;
17012
17013 u_len_pos++;
17014
17015 char *u_buf_pos = strchr (u_len_pos, '*');
17016
17017 if (u_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17018
17019 u32 u_len_len = u_buf_pos - u_len_pos;
17020
17021 u_buf_pos++;
17022
17023 char *o_len_pos = strchr (u_buf_pos, '*');
17024
17025 if (o_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17026
17027 u32 u_buf_len = o_len_pos - u_buf_pos;
17028
17029 o_len_pos++;
17030
17031 char *o_buf_pos = strchr (o_len_pos, '*');
17032
17033 if (o_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17034
17035 u32 o_len_len = o_buf_pos - o_len_pos;
17036
17037 o_buf_pos++;
17038
17039 char *last = strchr (o_buf_pos, '*');
17040
17041 if (last == NULL) last = input_buf + input_len;
17042
17043 u32 o_buf_len = last - o_buf_pos;
17044
17045 // validate data
17046
17047 const int V = atoi (V_pos);
17048 const int R = atoi (R_pos);
17049
17050 int vr_ok = 0;
17051
17052 if ((V == 5) && (R == 5)) vr_ok = 1;
17053 if ((V == 5) && (R == 6)) vr_ok = 1;
17054
17055 if (vr_ok == 0) return (PARSER_SALT_VALUE);
17056
17057 const int bits = atoi (bits_pos);
17058
17059 if (bits != 256) return (PARSER_SALT_VALUE);
17060
17061 int enc_md = atoi (enc_md_pos);
17062
17063 if (enc_md != 1) return (PARSER_SALT_VALUE);
17064
17065 const uint id_len = atoi (id_len_pos);
17066 const uint u_len = atoi (u_len_pos);
17067 const uint o_len = atoi (o_len_pos);
17068
17069 if (V_len > 6) return (PARSER_SALT_LENGTH);
17070 if (R_len > 6) return (PARSER_SALT_LENGTH);
17071 if (P_len > 6) return (PARSER_SALT_LENGTH);
17072 if (id_len_len > 6) return (PARSER_SALT_LENGTH);
17073 if (u_len_len > 6) return (PARSER_SALT_LENGTH);
17074 if (o_len_len > 6) return (PARSER_SALT_LENGTH);
17075 if (bits_len > 6) return (PARSER_SALT_LENGTH);
17076 if (enc_md_len > 6) return (PARSER_SALT_LENGTH);
17077
17078 if ((id_len * 2) != id_buf_len) return (PARSER_SALT_VALUE);
17079 if ((u_len * 2) != u_buf_len) return (PARSER_SALT_VALUE);
17080 if ((o_len * 2) != o_buf_len) return (PARSER_SALT_VALUE);
17081
17082 // copy data to esalt
17083
17084 if (u_len < 40) return (PARSER_SALT_VALUE);
17085
17086 for (int i = 0, j = 0; i < 8 + 2; i += 1, j += 8)
17087 {
17088 pdf->u_buf[i] = hex_to_u32 ((const u8 *) &u_buf_pos[j]);
17089 }
17090
17091 salt->salt_buf[0] = pdf->u_buf[8];
17092 salt->salt_buf[1] = pdf->u_buf[9];
17093
17094 salt->salt_buf[0] = byte_swap_32 (salt->salt_buf[0]);
17095 salt->salt_buf[1] = byte_swap_32 (salt->salt_buf[1]);
17096
17097 salt->salt_len = 8;
17098 salt->salt_iter = ROUNDS_PDF17L8;
17099
17100 digest[0] = pdf->u_buf[0];
17101 digest[1] = pdf->u_buf[1];
17102 digest[2] = pdf->u_buf[2];
17103 digest[3] = pdf->u_buf[3];
17104 digest[4] = pdf->u_buf[4];
17105 digest[5] = pdf->u_buf[5];
17106 digest[6] = pdf->u_buf[6];
17107 digest[7] = pdf->u_buf[7];
17108
17109 return (PARSER_OK);
17110 }
17111
17112 int pbkdf2_sha256_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
17113 {
17114 if ((input_len < DISPLAY_LEN_MIN_10900) || (input_len > DISPLAY_LEN_MAX_10900)) return (PARSER_GLOBAL_LENGTH);
17115
17116 if (memcmp (SIGNATURE_PBKDF2_SHA256, input_buf, 7)) return (PARSER_SIGNATURE_UNMATCHED);
17117
17118 u32 *digest = (u32 *) hash_buf->digest;
17119
17120 salt_t *salt = hash_buf->salt;
17121
17122 pbkdf2_sha256_t *pbkdf2_sha256 = (pbkdf2_sha256_t *) hash_buf->esalt;
17123
17124 /**
17125 * parse line
17126 */
17127
17128 // iterations
17129
17130 char *iter_pos = input_buf + 7;
17131
17132 u32 iter = atoi (iter_pos);
17133
17134 if (iter < 1) return (PARSER_SALT_ITERATION);
17135 if (iter > 999999) return (PARSER_SALT_ITERATION);
17136
17137 // first is *raw* salt
17138
17139 char *salt_pos = strchr (iter_pos, ':');
17140
17141 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17142
17143 salt_pos++;
17144
17145 char *hash_pos = strchr (salt_pos, ':');
17146
17147 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17148
17149 u32 salt_len = hash_pos - salt_pos;
17150
17151 if (salt_len > 64) return (PARSER_SALT_LENGTH);
17152
17153 hash_pos++;
17154
17155 u32 hash_b64_len = input_len - (hash_pos - input_buf);
17156
17157 if (hash_b64_len > 88) return (PARSER_HASH_LENGTH);
17158
17159 // decode salt
17160
17161 char *salt_buf_ptr = (char *) pbkdf2_sha256->salt_buf;
17162
17163 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
17164
17165 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
17166
17167 salt_buf_ptr[salt_len + 3] = 0x01;
17168 salt_buf_ptr[salt_len + 4] = 0x80;
17169
17170 salt->salt_len = salt_len;
17171 salt->salt_iter = iter - 1;
17172
17173 // decode hash
17174
17175 u8 tmp_buf[100] = { 0 };
17176
17177 int hash_len = base64_decode (base64_to_int, (const u8 *) hash_pos, hash_b64_len, tmp_buf);
17178
17179 if (hash_len < 16) return (PARSER_HASH_LENGTH);
17180
17181 memcpy (digest, tmp_buf, 16);
17182
17183 digest[0] = byte_swap_32 (digest[0]);
17184 digest[1] = byte_swap_32 (digest[1]);
17185 digest[2] = byte_swap_32 (digest[2]);
17186 digest[3] = byte_swap_32 (digest[3]);
17187
17188 // add some stuff to normal salt to make sorted happy
17189
17190 salt->salt_buf[0] = pbkdf2_sha256->salt_buf[0];
17191 salt->salt_buf[1] = pbkdf2_sha256->salt_buf[1];
17192 salt->salt_buf[2] = pbkdf2_sha256->salt_buf[2];
17193 salt->salt_buf[3] = pbkdf2_sha256->salt_buf[3];
17194 salt->salt_buf[4] = salt->salt_iter;
17195
17196 return (PARSER_OK);
17197 }
17198
17199 int prestashop_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
17200 {
17201 if ((input_len < DISPLAY_LEN_MIN_11000) || (input_len > DISPLAY_LEN_MAX_11000)) return (PARSER_GLOBAL_LENGTH);
17202
17203 u32 *digest = (u32 *) hash_buf->digest;
17204
17205 salt_t *salt = hash_buf->salt;
17206
17207 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
17208 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
17209 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
17210 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
17211
17212 digest[0] = byte_swap_32 (digest[0]);
17213 digest[1] = byte_swap_32 (digest[1]);
17214 digest[2] = byte_swap_32 (digest[2]);
17215 digest[3] = byte_swap_32 (digest[3]);
17216
17217 if (input_buf[32] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
17218
17219 uint salt_len = input_len - 32 - 1;
17220
17221 char *salt_buf = input_buf + 32 + 1;
17222
17223 char *salt_buf_ptr = (char *) salt->salt_buf;
17224
17225 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
17226
17227 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
17228
17229 salt->salt_len = salt_len;
17230
17231 return (PARSER_OK);
17232 }
17233
17234 int postgresql_auth_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
17235 {
17236 if ((input_len < DISPLAY_LEN_MIN_11100) || (input_len > DISPLAY_LEN_MAX_11100)) return (PARSER_GLOBAL_LENGTH);
17237
17238 if (memcmp (SIGNATURE_POSTGRESQL_AUTH, input_buf, 10)) return (PARSER_SIGNATURE_UNMATCHED);
17239
17240 u32 *digest = (u32 *) hash_buf->digest;
17241
17242 salt_t *salt = hash_buf->salt;
17243
17244 char *user_pos = input_buf + 10;
17245
17246 char *salt_pos = strchr (user_pos, '*');
17247
17248 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17249
17250 salt_pos++;
17251
17252 char *hash_pos = strchr (salt_pos, '*');
17253
17254 hash_pos++;
17255
17256 uint hash_len = input_len - (hash_pos - input_buf);
17257
17258 if (hash_len != 32) return (PARSER_HASH_LENGTH);
17259
17260 uint user_len = salt_pos - user_pos - 1;
17261
17262 uint salt_len = hash_pos - salt_pos - 1;
17263
17264 if (salt_len != 8) return (PARSER_SALT_LENGTH);
17265
17266 /*
17267 * store digest
17268 */
17269
17270 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
17271 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
17272 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
17273 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
17274
17275 digest[0] = byte_swap_32 (digest[0]);
17276 digest[1] = byte_swap_32 (digest[1]);
17277 digest[2] = byte_swap_32 (digest[2]);
17278 digest[3] = byte_swap_32 (digest[3]);
17279
17280 digest[0] -= MD5M_A;
17281 digest[1] -= MD5M_B;
17282 digest[2] -= MD5M_C;
17283 digest[3] -= MD5M_D;
17284
17285 /*
17286 * store salt
17287 */
17288
17289 char *salt_buf_ptr = (char *) salt->salt_buf;
17290
17291 // first 4 bytes are the "challenge"
17292
17293 salt_buf_ptr[0] = hex_to_u8 ((const u8 *) &salt_pos[0]);
17294 salt_buf_ptr[1] = hex_to_u8 ((const u8 *) &salt_pos[2]);
17295 salt_buf_ptr[2] = hex_to_u8 ((const u8 *) &salt_pos[4]);
17296 salt_buf_ptr[3] = hex_to_u8 ((const u8 *) &salt_pos[6]);
17297
17298 // append the user name
17299
17300 user_len = parse_and_store_salt (salt_buf_ptr + 4, user_pos, user_len);
17301
17302 salt->salt_len = 4 + user_len;
17303
17304 return (PARSER_OK);
17305 }
17306
17307 int mysql_auth_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
17308 {
17309 if ((input_len < DISPLAY_LEN_MIN_11200) || (input_len > DISPLAY_LEN_MAX_11200)) return (PARSER_GLOBAL_LENGTH);
17310
17311 if (memcmp (SIGNATURE_MYSQL_AUTH, input_buf, 9)) return (PARSER_SIGNATURE_UNMATCHED);
17312
17313 u32 *digest = (u32 *) hash_buf->digest;
17314
17315 salt_t *salt = hash_buf->salt;
17316
17317 char *salt_pos = input_buf + 9;
17318
17319 char *hash_pos = strchr (salt_pos, '*');
17320
17321 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17322
17323 hash_pos++;
17324
17325 uint hash_len = input_len - (hash_pos - input_buf);
17326
17327 if (hash_len != 40) return (PARSER_HASH_LENGTH);
17328
17329 uint salt_len = hash_pos - salt_pos - 1;
17330
17331 if (salt_len != 40) return (PARSER_SALT_LENGTH);
17332
17333 /*
17334 * store digest
17335 */
17336
17337 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
17338 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
17339 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
17340 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
17341 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
17342
17343 /*
17344 * store salt
17345 */
17346
17347 char *salt_buf_ptr = (char *) salt->salt_buf;
17348
17349 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
17350
17351 salt->salt_len = salt_len;
17352
17353 return (PARSER_OK);
17354 }
17355
17356 int bitcoin_wallet_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
17357 {
17358 if ((input_len < DISPLAY_LEN_MIN_11300) || (input_len > DISPLAY_LEN_MAX_11300)) return (PARSER_GLOBAL_LENGTH);
17359
17360 if (memcmp (SIGNATURE_BITCOIN_WALLET, input_buf, 9)) return (PARSER_SIGNATURE_UNMATCHED);
17361
17362 u32 *digest = (u32 *) hash_buf->digest;
17363
17364 salt_t *salt = hash_buf->salt;
17365
17366 bitcoin_wallet_t *bitcoin_wallet = (bitcoin_wallet_t *) hash_buf->esalt;
17367
17368 /**
17369 * parse line
17370 */
17371
17372 char *cry_master_len_pos = input_buf + 9;
17373
17374 char *cry_master_buf_pos = strchr (cry_master_len_pos, '$');
17375
17376 if (cry_master_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17377
17378 u32 cry_master_len_len = cry_master_buf_pos - cry_master_len_pos;
17379
17380 cry_master_buf_pos++;
17381
17382 char *cry_salt_len_pos = strchr (cry_master_buf_pos, '$');
17383
17384 if (cry_salt_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17385
17386 u32 cry_master_buf_len = cry_salt_len_pos - cry_master_buf_pos;
17387
17388 cry_salt_len_pos++;
17389
17390 char *cry_salt_buf_pos = strchr (cry_salt_len_pos, '$');
17391
17392 if (cry_salt_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17393
17394 u32 cry_salt_len_len = cry_salt_buf_pos - cry_salt_len_pos;
17395
17396 cry_salt_buf_pos++;
17397
17398 char *cry_rounds_pos = strchr (cry_salt_buf_pos, '$');
17399
17400 if (cry_rounds_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17401
17402 u32 cry_salt_buf_len = cry_rounds_pos - cry_salt_buf_pos;
17403
17404 cry_rounds_pos++;
17405
17406 char *ckey_len_pos = strchr (cry_rounds_pos, '$');
17407
17408 if (ckey_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17409
17410 u32 cry_rounds_len = ckey_len_pos - cry_rounds_pos;
17411
17412 ckey_len_pos++;
17413
17414 char *ckey_buf_pos = strchr (ckey_len_pos, '$');
17415
17416 if (ckey_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17417
17418 u32 ckey_len_len = ckey_buf_pos - ckey_len_pos;
17419
17420 ckey_buf_pos++;
17421
17422 char *public_key_len_pos = strchr (ckey_buf_pos, '$');
17423
17424 if (public_key_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17425
17426 u32 ckey_buf_len = public_key_len_pos - ckey_buf_pos;
17427
17428 public_key_len_pos++;
17429
17430 char *public_key_buf_pos = strchr (public_key_len_pos, '$');
17431
17432 if (public_key_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
17433
17434 u32 public_key_len_len = public_key_buf_pos - public_key_len_pos;
17435
17436 public_key_buf_pos++;
17437
17438 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;
17439
17440 const uint cry_master_len = atoi (cry_master_len_pos);
17441 const uint cry_salt_len = atoi (cry_salt_len_pos);
17442 const uint ckey_len = atoi (ckey_len_pos);
17443 const uint public_key_len = atoi (public_key_len_pos);
17444
17445 if (cry_master_buf_len != cry_master_len) return (PARSER_SALT_VALUE);
17446 if (cry_salt_buf_len != cry_salt_len) return (PARSER_SALT_VALUE);
17447 if (ckey_buf_len != ckey_len) return (PARSER_SALT_VALUE);
17448 if (public_key_buf_len != public_key_len) return (PARSER_SALT_VALUE);
17449
17450 for (uint i = 0, j = 0; j < cry_master_len; i += 1, j += 8)
17451 {
17452 bitcoin_wallet->cry_master_buf[i] = hex_to_u32 ((const u8 *) &cry_master_buf_pos[j]);
17453
17454 bitcoin_wallet->cry_master_buf[i] = byte_swap_32 (bitcoin_wallet->cry_master_buf[i]);
17455 }
17456
17457 for (uint i = 0, j = 0; j < ckey_len; i += 1, j += 8)
17458 {
17459 bitcoin_wallet->ckey_buf[i] = hex_to_u32 ((const u8 *) &ckey_buf_pos[j]);
17460
17461 bitcoin_wallet->ckey_buf[i] = byte_swap_32 (bitcoin_wallet->ckey_buf[i]);
17462 }
17463
17464 for (uint i = 0, j = 0; j < public_key_len; i += 1, j += 8)
17465 {
17466 bitcoin_wallet->public_key_buf[i] = hex_to_u32 ((const u8 *) &public_key_buf_pos[j]);
17467
17468 bitcoin_wallet->public_key_buf[i] = byte_swap_32 (bitcoin_wallet->public_key_buf[i]);
17469 }
17470
17471 bitcoin_wallet->cry_master_len = cry_master_len / 2;
17472 bitcoin_wallet->ckey_len = ckey_len / 2;
17473 bitcoin_wallet->public_key_len = public_key_len / 2;
17474
17475 /*
17476 * store digest (should be unique enought, hopefully)
17477 */
17478
17479 digest[0] = bitcoin_wallet->cry_master_buf[0];
17480 digest[1] = bitcoin_wallet->cry_master_buf[1];
17481 digest[2] = bitcoin_wallet->cry_master_buf[2];
17482 digest[3] = bitcoin_wallet->cry_master_buf[3];
17483
17484 /*
17485 * store salt
17486 */
17487
17488 if (cry_rounds_len >= 7) return (PARSER_SALT_VALUE);
17489
17490 const uint cry_rounds = atoi (cry_rounds_pos);
17491
17492 salt->salt_iter = cry_rounds - 1;
17493
17494 char *salt_buf_ptr = (char *) salt->salt_buf;
17495
17496 const uint salt_len = parse_and_store_salt (salt_buf_ptr, cry_salt_buf_pos, cry_salt_buf_len);
17497
17498 salt->salt_len = salt_len;
17499
17500 return (PARSER_OK);
17501 }
17502
17503 int sip_auth_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
17504 {
17505 if ((input_len < DISPLAY_LEN_MIN_11400) || (input_len > DISPLAY_LEN_MAX_11400)) return (PARSER_GLOBAL_LENGTH);
17506
17507 if (memcmp (SIGNATURE_SIP_AUTH, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
17508
17509 u32 *digest = (u32 *) hash_buf->digest;
17510
17511 salt_t *salt = hash_buf->salt;
17512
17513 sip_t *sip = (sip_t *) hash_buf->esalt;
17514
17515 // work with a temporary copy of input_buf (s.t. we can manipulate it directly)
17516
17517 char *temp_input_buf = (char *) mymalloc (input_len + 1);
17518
17519 memcpy (temp_input_buf, input_buf, input_len);
17520
17521 // URI_server:
17522
17523 char *URI_server_pos = temp_input_buf + 6;
17524
17525 char *URI_client_pos = strchr (URI_server_pos, '*');
17526
17527 if (URI_client_pos == NULL)
17528 {
17529 myfree (temp_input_buf);
17530
17531 return (PARSER_SEPARATOR_UNMATCHED);
17532 }
17533
17534 URI_client_pos[0] = 0;
17535 URI_client_pos++;
17536
17537 uint URI_server_len = strlen (URI_server_pos);
17538
17539 if (URI_server_len > 512)
17540 {
17541 myfree (temp_input_buf);
17542
17543 return (PARSER_SALT_LENGTH);
17544 }
17545
17546 // URI_client:
17547
17548 char *user_pos = strchr (URI_client_pos, '*');
17549
17550 if (user_pos == NULL)
17551 {
17552 myfree (temp_input_buf);
17553
17554 return (PARSER_SEPARATOR_UNMATCHED);
17555 }
17556
17557 user_pos[0] = 0;
17558 user_pos++;
17559
17560 uint URI_client_len = strlen (URI_client_pos);
17561
17562 if (URI_client_len > 512)
17563 {
17564 myfree (temp_input_buf);
17565
17566 return (PARSER_SALT_LENGTH);
17567 }
17568
17569 // user:
17570
17571 char *realm_pos = strchr (user_pos, '*');
17572
17573 if (realm_pos == NULL)
17574 {
17575 myfree (temp_input_buf);
17576
17577 return (PARSER_SEPARATOR_UNMATCHED);
17578 }
17579
17580 realm_pos[0] = 0;
17581 realm_pos++;
17582
17583 uint user_len = strlen (user_pos);
17584
17585 if (user_len > 116)
17586 {
17587 myfree (temp_input_buf);
17588
17589 return (PARSER_SALT_LENGTH);
17590 }
17591
17592 // realm:
17593
17594 char *method_pos = strchr (realm_pos, '*');
17595
17596 if (method_pos == NULL)
17597 {
17598 myfree (temp_input_buf);
17599
17600 return (PARSER_SEPARATOR_UNMATCHED);
17601 }
17602
17603 method_pos[0] = 0;
17604 method_pos++;
17605
17606 uint realm_len = strlen (realm_pos);
17607
17608 if (realm_len > 116)
17609 {
17610 myfree (temp_input_buf);
17611
17612 return (PARSER_SALT_LENGTH);
17613 }
17614
17615 // method:
17616
17617 char *URI_prefix_pos = strchr (method_pos, '*');
17618
17619 if (URI_prefix_pos == NULL)
17620 {
17621 myfree (temp_input_buf);
17622
17623 return (PARSER_SEPARATOR_UNMATCHED);
17624 }
17625
17626 URI_prefix_pos[0] = 0;
17627 URI_prefix_pos++;
17628
17629 uint method_len = strlen (method_pos);
17630
17631 if (method_len > 246)
17632 {
17633 myfree (temp_input_buf);
17634
17635 return (PARSER_SALT_LENGTH);
17636 }
17637
17638 // URI_prefix:
17639
17640 char *URI_resource_pos = strchr (URI_prefix_pos, '*');
17641
17642 if (URI_resource_pos == NULL)
17643 {
17644 myfree (temp_input_buf);
17645
17646 return (PARSER_SEPARATOR_UNMATCHED);
17647 }
17648
17649 URI_resource_pos[0] = 0;
17650 URI_resource_pos++;
17651
17652 uint URI_prefix_len = strlen (URI_prefix_pos);
17653
17654 if (URI_prefix_len > 245)
17655 {
17656 myfree (temp_input_buf);
17657
17658 return (PARSER_SALT_LENGTH);
17659 }
17660
17661 // URI_resource:
17662
17663 char *URI_suffix_pos = strchr (URI_resource_pos, '*');
17664
17665 if (URI_suffix_pos == NULL)
17666 {
17667 myfree (temp_input_buf);
17668
17669 return (PARSER_SEPARATOR_UNMATCHED);
17670 }
17671
17672 URI_suffix_pos[0] = 0;
17673 URI_suffix_pos++;
17674
17675 uint URI_resource_len = strlen (URI_resource_pos);
17676
17677 if (URI_resource_len < 1 || URI_resource_len > 246)
17678 {
17679 myfree (temp_input_buf);
17680
17681 return (PARSER_SALT_LENGTH);
17682 }
17683
17684 // URI_suffix:
17685
17686 char *nonce_pos = strchr (URI_suffix_pos, '*');
17687
17688 if (nonce_pos == NULL)
17689 {
17690 myfree (temp_input_buf);
17691
17692 return (PARSER_SEPARATOR_UNMATCHED);
17693 }
17694
17695 nonce_pos[0] = 0;
17696 nonce_pos++;
17697
17698 uint URI_suffix_len = strlen (URI_suffix_pos);
17699
17700 if (URI_suffix_len > 245)
17701 {
17702 myfree (temp_input_buf);
17703
17704 return (PARSER_SALT_LENGTH);
17705 }
17706
17707 // nonce:
17708
17709 char *nonce_client_pos = strchr (nonce_pos, '*');
17710
17711 if (nonce_client_pos == NULL)
17712 {
17713 myfree (temp_input_buf);
17714
17715 return (PARSER_SEPARATOR_UNMATCHED);
17716 }
17717
17718 nonce_client_pos[0] = 0;
17719 nonce_client_pos++;
17720
17721 uint nonce_len = strlen (nonce_pos);
17722
17723 if (nonce_len < 1 || nonce_len > 50)
17724 {
17725 myfree (temp_input_buf);
17726
17727 return (PARSER_SALT_LENGTH);
17728 }
17729
17730 // nonce_client:
17731
17732 char *nonce_count_pos = strchr (nonce_client_pos, '*');
17733
17734 if (nonce_count_pos == NULL)
17735 {
17736 myfree (temp_input_buf);
17737
17738 return (PARSER_SEPARATOR_UNMATCHED);
17739 }
17740
17741 nonce_count_pos[0] = 0;
17742 nonce_count_pos++;
17743
17744 uint nonce_client_len = strlen (nonce_client_pos);
17745
17746 if (nonce_client_len > 50)
17747 {
17748 myfree (temp_input_buf);
17749
17750 return (PARSER_SALT_LENGTH);
17751 }
17752
17753 // nonce_count:
17754
17755 char *qop_pos = strchr (nonce_count_pos, '*');
17756
17757 if (qop_pos == NULL)
17758 {
17759 myfree (temp_input_buf);
17760
17761 return (PARSER_SEPARATOR_UNMATCHED);
17762 }
17763
17764 qop_pos[0] = 0;
17765 qop_pos++;
17766
17767 uint nonce_count_len = strlen (nonce_count_pos);
17768
17769 if (nonce_count_len > 50)
17770 {
17771 myfree (temp_input_buf);
17772
17773 return (PARSER_SALT_LENGTH);
17774 }
17775
17776 // qop:
17777
17778 char *directive_pos = strchr (qop_pos, '*');
17779
17780 if (directive_pos == NULL)
17781 {
17782 myfree (temp_input_buf);
17783
17784 return (PARSER_SEPARATOR_UNMATCHED);
17785 }
17786
17787 directive_pos[0] = 0;
17788 directive_pos++;
17789
17790 uint qop_len = strlen (qop_pos);
17791
17792 if (qop_len > 50)
17793 {
17794 myfree (temp_input_buf);
17795
17796 return (PARSER_SALT_LENGTH);
17797 }
17798
17799 // directive
17800
17801 char *digest_pos = strchr (directive_pos, '*');
17802
17803 if (digest_pos == NULL)
17804 {
17805 myfree (temp_input_buf);
17806
17807 return (PARSER_SEPARATOR_UNMATCHED);
17808 }
17809
17810 digest_pos[0] = 0;
17811 digest_pos++;
17812
17813 uint directive_len = strlen (directive_pos);
17814
17815 if (directive_len != 3)
17816 {
17817 myfree (temp_input_buf);
17818
17819 return (PARSER_SALT_LENGTH);
17820 }
17821
17822 if (memcmp (directive_pos, "MD5", 3))
17823 {
17824 log_info ("ERROR: only the MD5 directive is currently supported\n");
17825
17826 myfree (temp_input_buf);
17827
17828 return (PARSER_SIP_AUTH_DIRECTIVE);
17829 }
17830
17831 /*
17832 * first (pre-)compute: HA2 = md5 ($method . ":" . $uri)
17833 */
17834
17835 uint md5_len = 0;
17836
17837 uint md5_max_len = 4 * 64;
17838
17839 uint md5_remaining_len = md5_max_len;
17840
17841 uint tmp_md5_buf[64] = { 0 };
17842
17843 char *tmp_md5_ptr = (char *) tmp_md5_buf;
17844
17845 snprintf (tmp_md5_ptr, md5_remaining_len, "%s:", method_pos);
17846
17847 md5_len += method_len + 1;
17848 tmp_md5_ptr += method_len + 1;
17849
17850 if (URI_prefix_len > 0)
17851 {
17852 md5_remaining_len = md5_max_len - md5_len;
17853
17854 snprintf (tmp_md5_ptr, md5_remaining_len + 1, "%s:", URI_prefix_pos);
17855
17856 md5_len += URI_prefix_len + 1;
17857 tmp_md5_ptr += URI_prefix_len + 1;
17858 }
17859
17860 md5_remaining_len = md5_max_len - md5_len;
17861
17862 snprintf (tmp_md5_ptr, md5_remaining_len + 1, "%s", URI_resource_pos);
17863
17864 md5_len += URI_resource_len;
17865 tmp_md5_ptr += URI_resource_len;
17866
17867 if (URI_suffix_len > 0)
17868 {
17869 md5_remaining_len = md5_max_len - md5_len;
17870
17871 snprintf (tmp_md5_ptr, md5_remaining_len + 1, ":%s", URI_suffix_pos);
17872
17873 md5_len += 1 + URI_suffix_len;
17874 }
17875
17876 uint tmp_digest[4] = { 0 };
17877
17878 md5_complete_no_limit (tmp_digest, tmp_md5_buf, md5_len);
17879
17880 tmp_digest[0] = byte_swap_32 (tmp_digest[0]);
17881 tmp_digest[1] = byte_swap_32 (tmp_digest[1]);
17882 tmp_digest[2] = byte_swap_32 (tmp_digest[2]);
17883 tmp_digest[3] = byte_swap_32 (tmp_digest[3]);
17884
17885 /*
17886 * esalt
17887 */
17888
17889 char *esalt_buf_ptr = (char *) sip->esalt_buf;
17890
17891 uint esalt_len = 0;
17892
17893 uint max_esalt_len = sizeof (sip->esalt_buf); // 151 = (64 + 64 + 55) - 32, where 32 is the hexadecimal MD5 HA1 hash
17894
17895 // there are 2 possibilities for the esalt:
17896
17897 if ((strcmp (qop_pos, "auth") == 0) || (strcmp (qop_pos, "auth-int") == 0))
17898 {
17899 esalt_len = 1 + nonce_len + 1 + nonce_count_len + 1 + nonce_client_len + 1 + qop_len + 1 + 32;
17900
17901 if (esalt_len > max_esalt_len)
17902 {
17903 myfree (temp_input_buf);
17904
17905 return (PARSER_SALT_LENGTH);
17906 }
17907
17908 snprintf (esalt_buf_ptr, max_esalt_len, ":%s:%s:%s:%s:%08x%08x%08x%08x",
17909 nonce_pos,
17910 nonce_count_pos,
17911 nonce_client_pos,
17912 qop_pos,
17913 tmp_digest[0],
17914 tmp_digest[1],
17915 tmp_digest[2],
17916 tmp_digest[3]);
17917 }
17918 else
17919 {
17920 esalt_len = 1 + nonce_len + 1 + 32;
17921
17922 if (esalt_len > max_esalt_len)
17923 {
17924 myfree (temp_input_buf);
17925
17926 return (PARSER_SALT_LENGTH);
17927 }
17928
17929 snprintf (esalt_buf_ptr, max_esalt_len, ":%s:%08x%08x%08x%08x",
17930 nonce_pos,
17931 tmp_digest[0],
17932 tmp_digest[1],
17933 tmp_digest[2],
17934 tmp_digest[3]);
17935 }
17936
17937 // add 0x80 to esalt
17938
17939 esalt_buf_ptr[esalt_len] = 0x80;
17940
17941 sip->esalt_len = esalt_len;
17942
17943 /*
17944 * actual salt
17945 */
17946
17947 char *sip_salt_ptr = (char *) sip->salt_buf;
17948
17949 uint salt_len = user_len + 1 + realm_len + 1;
17950
17951 uint max_salt_len = 119;
17952
17953 if (salt_len > max_salt_len)
17954 {
17955 myfree (temp_input_buf);
17956
17957 return (PARSER_SALT_LENGTH);
17958 }
17959
17960 snprintf (sip_salt_ptr, max_salt_len + 1, "%s:%s:", user_pos, realm_pos);
17961
17962 sip->salt_len = salt_len;
17963
17964 /*
17965 * fake salt (for sorting)
17966 */
17967
17968 char *salt_buf_ptr = (char *) salt->salt_buf;
17969
17970 max_salt_len = 55;
17971
17972 uint fake_salt_len = salt_len;
17973
17974 if (fake_salt_len > max_salt_len)
17975 {
17976 fake_salt_len = max_salt_len;
17977 }
17978
17979 snprintf (salt_buf_ptr, max_salt_len + 1, "%s:%s:", user_pos, realm_pos);
17980
17981 salt->salt_len = fake_salt_len;
17982
17983 /*
17984 * digest
17985 */
17986
17987 digest[0] = hex_to_u32 ((const u8 *) &digest_pos[ 0]);
17988 digest[1] = hex_to_u32 ((const u8 *) &digest_pos[ 8]);
17989 digest[2] = hex_to_u32 ((const u8 *) &digest_pos[16]);
17990 digest[3] = hex_to_u32 ((const u8 *) &digest_pos[24]);
17991
17992 digest[0] = byte_swap_32 (digest[0]);
17993 digest[1] = byte_swap_32 (digest[1]);
17994 digest[2] = byte_swap_32 (digest[2]);
17995 digest[3] = byte_swap_32 (digest[3]);
17996
17997 myfree (temp_input_buf);
17998
17999 return (PARSER_OK);
18000 }
18001
18002 int crc32_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18003 {
18004 if ((input_len < DISPLAY_LEN_MIN_11500) || (input_len > DISPLAY_LEN_MAX_11500)) return (PARSER_GLOBAL_LENGTH);
18005
18006 if (input_buf[8] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
18007
18008 u32 *digest = (u32 *) hash_buf->digest;
18009
18010 salt_t *salt = hash_buf->salt;
18011
18012 // digest
18013
18014 char *digest_pos = input_buf;
18015
18016 digest[0] = hex_to_u32 ((const u8 *) &digest_pos[0]);
18017 digest[1] = 0;
18018 digest[2] = 0;
18019 digest[3] = 0;
18020
18021 // salt
18022
18023 char *salt_buf = input_buf + 8 + 1;
18024
18025 uint salt_len = 8;
18026
18027 char *salt_buf_ptr = (char *) salt->salt_buf;
18028
18029 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
18030
18031 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
18032
18033 salt->salt_len = salt_len;
18034
18035 return (PARSER_OK);
18036 }
18037
18038 int seven_zip_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18039 {
18040 if ((input_len < DISPLAY_LEN_MIN_11600) || (input_len > DISPLAY_LEN_MAX_11600)) return (PARSER_GLOBAL_LENGTH);
18041
18042 if (memcmp (SIGNATURE_SEVEN_ZIP, input_buf, 4)) return (PARSER_SIGNATURE_UNMATCHED);
18043
18044 u32 *digest = (u32 *) hash_buf->digest;
18045
18046 salt_t *salt = hash_buf->salt;
18047
18048 seven_zip_t *seven_zip = (seven_zip_t *) hash_buf->esalt;
18049
18050 /**
18051 * parse line
18052 */
18053
18054 char *p_buf_pos = input_buf + 4;
18055
18056 char *NumCyclesPower_pos = strchr (p_buf_pos, '$');
18057
18058 if (NumCyclesPower_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18059
18060 u32 p_buf_len = NumCyclesPower_pos - p_buf_pos;
18061
18062 NumCyclesPower_pos++;
18063
18064 char *salt_len_pos = strchr (NumCyclesPower_pos, '$');
18065
18066 if (salt_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18067
18068 u32 NumCyclesPower_len = salt_len_pos - NumCyclesPower_pos;
18069
18070 salt_len_pos++;
18071
18072 char *salt_buf_pos = strchr (salt_len_pos, '$');
18073
18074 if (salt_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18075
18076 u32 salt_len_len = salt_buf_pos - salt_len_pos;
18077
18078 salt_buf_pos++;
18079
18080 char *iv_len_pos = strchr (salt_buf_pos, '$');
18081
18082 if (iv_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18083
18084 u32 salt_buf_len = iv_len_pos - salt_buf_pos;
18085
18086 iv_len_pos++;
18087
18088 char *iv_buf_pos = strchr (iv_len_pos, '$');
18089
18090 if (iv_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18091
18092 u32 iv_len_len = iv_buf_pos - iv_len_pos;
18093
18094 iv_buf_pos++;
18095
18096 char *crc_buf_pos = strchr (iv_buf_pos, '$');
18097
18098 if (crc_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18099
18100 u32 iv_buf_len = crc_buf_pos - iv_buf_pos;
18101
18102 crc_buf_pos++;
18103
18104 char *data_len_pos = strchr (crc_buf_pos, '$');
18105
18106 if (data_len_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18107
18108 u32 crc_buf_len = data_len_pos - crc_buf_pos;
18109
18110 data_len_pos++;
18111
18112 char *unpack_size_pos = strchr (data_len_pos, '$');
18113
18114 if (unpack_size_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18115
18116 u32 data_len_len = unpack_size_pos - data_len_pos;
18117
18118 unpack_size_pos++;
18119
18120 char *data_buf_pos = strchr (unpack_size_pos, '$');
18121
18122 if (data_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18123
18124 u32 unpack_size_len = data_buf_pos - unpack_size_pos;
18125
18126 data_buf_pos++;
18127
18128 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;
18129
18130 const uint iter = atoi (NumCyclesPower_pos);
18131 const uint crc = atoi (crc_buf_pos);
18132 const uint p_buf = atoi (p_buf_pos);
18133 const uint salt_len = atoi (salt_len_pos);
18134 const uint iv_len = atoi (iv_len_pos);
18135 const uint unpack_size = atoi (unpack_size_pos);
18136 const uint data_len = atoi (data_len_pos);
18137
18138 /**
18139 * verify some data
18140 */
18141
18142 if (p_buf != 0) return (PARSER_SALT_VALUE);
18143 if (salt_len != 0) return (PARSER_SALT_VALUE);
18144
18145 if ((data_len * 2) != data_buf_len) return (PARSER_SALT_VALUE);
18146
18147 if (data_len > 384) return (PARSER_SALT_VALUE);
18148
18149 if (unpack_size > data_len) return (PARSER_SALT_VALUE);
18150
18151 /**
18152 * store data
18153 */
18154
18155 seven_zip->iv_buf[0] = hex_to_u32 ((const u8 *) &iv_buf_pos[ 0]);
18156 seven_zip->iv_buf[1] = hex_to_u32 ((const u8 *) &iv_buf_pos[ 8]);
18157 seven_zip->iv_buf[2] = hex_to_u32 ((const u8 *) &iv_buf_pos[16]);
18158 seven_zip->iv_buf[3] = hex_to_u32 ((const u8 *) &iv_buf_pos[24]);
18159
18160 seven_zip->iv_len = iv_len;
18161
18162 memcpy (seven_zip->salt_buf, salt_buf_pos, salt_buf_len); // we just need that for later ascii_digest()
18163
18164 seven_zip->salt_len = 0;
18165
18166 seven_zip->crc = crc;
18167
18168 for (uint i = 0, j = 0; j < data_buf_len; i += 1, j += 8)
18169 {
18170 seven_zip->data_buf[i] = hex_to_u32 ((const u8 *) &data_buf_pos[j]);
18171
18172 seven_zip->data_buf[i] = byte_swap_32 (seven_zip->data_buf[i]);
18173 }
18174
18175 seven_zip->data_len = data_len;
18176
18177 seven_zip->unpack_size = unpack_size;
18178
18179 // real salt
18180
18181 salt->salt_buf[0] = seven_zip->data_buf[0];
18182 salt->salt_buf[1] = seven_zip->data_buf[1];
18183 salt->salt_buf[2] = seven_zip->data_buf[2];
18184 salt->salt_buf[3] = seven_zip->data_buf[3];
18185
18186 salt->salt_len = 16;
18187
18188 salt->salt_sign[0] = iter;
18189
18190 salt->salt_iter = 1 << iter;
18191
18192 /**
18193 * digest
18194 */
18195
18196 digest[0] = crc;
18197 digest[1] = 0;
18198 digest[2] = 0;
18199 digest[3] = 0;
18200
18201 return (PARSER_OK);
18202 }
18203
18204 int gost2012sbog_256_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18205 {
18206 if ((input_len < DISPLAY_LEN_MIN_11700) || (input_len > DISPLAY_LEN_MAX_11700)) return (PARSER_GLOBAL_LENGTH);
18207
18208 u32 *digest = (u32 *) hash_buf->digest;
18209
18210 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
18211 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
18212 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
18213 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
18214 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
18215 digest[5] = hex_to_u32 ((const u8 *) &input_buf[40]);
18216 digest[6] = hex_to_u32 ((const u8 *) &input_buf[48]);
18217 digest[7] = hex_to_u32 ((const u8 *) &input_buf[56]);
18218
18219 digest[0] = byte_swap_32 (digest[0]);
18220 digest[1] = byte_swap_32 (digest[1]);
18221 digest[2] = byte_swap_32 (digest[2]);
18222 digest[3] = byte_swap_32 (digest[3]);
18223 digest[4] = byte_swap_32 (digest[4]);
18224 digest[5] = byte_swap_32 (digest[5]);
18225 digest[6] = byte_swap_32 (digest[6]);
18226 digest[7] = byte_swap_32 (digest[7]);
18227
18228 return (PARSER_OK);
18229 }
18230
18231 int gost2012sbog_512_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18232 {
18233 if ((input_len < DISPLAY_LEN_MIN_11800) || (input_len > DISPLAY_LEN_MAX_11800)) return (PARSER_GLOBAL_LENGTH);
18234
18235 u32 *digest = (u32 *) hash_buf->digest;
18236
18237 digest[ 0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
18238 digest[ 1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
18239 digest[ 2] = hex_to_u32 ((const u8 *) &input_buf[ 16]);
18240 digest[ 3] = hex_to_u32 ((const u8 *) &input_buf[ 24]);
18241 digest[ 4] = hex_to_u32 ((const u8 *) &input_buf[ 32]);
18242 digest[ 5] = hex_to_u32 ((const u8 *) &input_buf[ 40]);
18243 digest[ 6] = hex_to_u32 ((const u8 *) &input_buf[ 48]);
18244 digest[ 7] = hex_to_u32 ((const u8 *) &input_buf[ 56]);
18245 digest[ 8] = hex_to_u32 ((const u8 *) &input_buf[ 64]);
18246 digest[ 9] = hex_to_u32 ((const u8 *) &input_buf[ 72]);
18247 digest[10] = hex_to_u32 ((const u8 *) &input_buf[ 80]);
18248 digest[11] = hex_to_u32 ((const u8 *) &input_buf[ 88]);
18249 digest[12] = hex_to_u32 ((const u8 *) &input_buf[ 96]);
18250 digest[13] = hex_to_u32 ((const u8 *) &input_buf[104]);
18251 digest[14] = hex_to_u32 ((const u8 *) &input_buf[112]);
18252 digest[15] = hex_to_u32 ((const u8 *) &input_buf[120]);
18253
18254 digest[ 0] = byte_swap_32 (digest[ 0]);
18255 digest[ 1] = byte_swap_32 (digest[ 1]);
18256 digest[ 2] = byte_swap_32 (digest[ 2]);
18257 digest[ 3] = byte_swap_32 (digest[ 3]);
18258 digest[ 4] = byte_swap_32 (digest[ 4]);
18259 digest[ 5] = byte_swap_32 (digest[ 5]);
18260 digest[ 6] = byte_swap_32 (digest[ 6]);
18261 digest[ 7] = byte_swap_32 (digest[ 7]);
18262 digest[ 8] = byte_swap_32 (digest[ 8]);
18263 digest[ 9] = byte_swap_32 (digest[ 9]);
18264 digest[10] = byte_swap_32 (digest[10]);
18265 digest[11] = byte_swap_32 (digest[11]);
18266 digest[12] = byte_swap_32 (digest[12]);
18267 digest[13] = byte_swap_32 (digest[13]);
18268 digest[14] = byte_swap_32 (digest[14]);
18269 digest[15] = byte_swap_32 (digest[15]);
18270
18271 return (PARSER_OK);
18272 }
18273
18274 int pbkdf2_md5_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18275 {
18276 if ((input_len < DISPLAY_LEN_MIN_11900) || (input_len > DISPLAY_LEN_MAX_11900)) return (PARSER_GLOBAL_LENGTH);
18277
18278 if (memcmp (SIGNATURE_PBKDF2_MD5, input_buf, 4)) return (PARSER_SIGNATURE_UNMATCHED);
18279
18280 u32 *digest = (u32 *) hash_buf->digest;
18281
18282 salt_t *salt = hash_buf->salt;
18283
18284 pbkdf2_md5_t *pbkdf2_md5 = (pbkdf2_md5_t *) hash_buf->esalt;
18285
18286 /**
18287 * parse line
18288 */
18289
18290 // iterations
18291
18292 char *iter_pos = input_buf + 4;
18293
18294 u32 iter = atoi (iter_pos);
18295
18296 if (iter < 1) return (PARSER_SALT_ITERATION);
18297 if (iter > 999999) return (PARSER_SALT_ITERATION);
18298
18299 // first is *raw* salt
18300
18301 char *salt_pos = strchr (iter_pos, ':');
18302
18303 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18304
18305 salt_pos++;
18306
18307 char *hash_pos = strchr (salt_pos, ':');
18308
18309 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18310
18311 u32 salt_len = hash_pos - salt_pos;
18312
18313 if (salt_len > 64) return (PARSER_SALT_LENGTH);
18314
18315 hash_pos++;
18316
18317 u32 hash_b64_len = input_len - (hash_pos - input_buf);
18318
18319 if (hash_b64_len > 88) return (PARSER_HASH_LENGTH);
18320
18321 // decode salt
18322
18323 char *salt_buf_ptr = (char *) pbkdf2_md5->salt_buf;
18324
18325 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
18326
18327 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
18328
18329 salt_buf_ptr[salt_len + 3] = 0x01;
18330 salt_buf_ptr[salt_len + 4] = 0x80;
18331
18332 salt->salt_len = salt_len;
18333 salt->salt_iter = iter - 1;
18334
18335 // decode hash
18336
18337 u8 tmp_buf[100] = { 0 };
18338
18339 int hash_len = base64_decode (base64_to_int, (const u8 *) hash_pos, hash_b64_len, tmp_buf);
18340
18341 if (hash_len < 16) return (PARSER_HASH_LENGTH);
18342
18343 memcpy (digest, tmp_buf, 16);
18344
18345 // add some stuff to normal salt to make sorted happy
18346
18347 salt->salt_buf[0] = pbkdf2_md5->salt_buf[0];
18348 salt->salt_buf[1] = pbkdf2_md5->salt_buf[1];
18349 salt->salt_buf[2] = pbkdf2_md5->salt_buf[2];
18350 salt->salt_buf[3] = pbkdf2_md5->salt_buf[3];
18351 salt->salt_buf[4] = salt->salt_iter;
18352
18353 return (PARSER_OK);
18354 }
18355
18356 int pbkdf2_sha1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18357 {
18358 if ((input_len < DISPLAY_LEN_MIN_12000) || (input_len > DISPLAY_LEN_MAX_12000)) return (PARSER_GLOBAL_LENGTH);
18359
18360 if (memcmp (SIGNATURE_PBKDF2_SHA1, input_buf, 5)) return (PARSER_SIGNATURE_UNMATCHED);
18361
18362 u32 *digest = (u32 *) hash_buf->digest;
18363
18364 salt_t *salt = hash_buf->salt;
18365
18366 pbkdf2_sha1_t *pbkdf2_sha1 = (pbkdf2_sha1_t *) hash_buf->esalt;
18367
18368 /**
18369 * parse line
18370 */
18371
18372 // iterations
18373
18374 char *iter_pos = input_buf + 5;
18375
18376 u32 iter = atoi (iter_pos);
18377
18378 if (iter < 1) return (PARSER_SALT_ITERATION);
18379 if (iter > 999999) return (PARSER_SALT_ITERATION);
18380
18381 // first is *raw* salt
18382
18383 char *salt_pos = strchr (iter_pos, ':');
18384
18385 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18386
18387 salt_pos++;
18388
18389 char *hash_pos = strchr (salt_pos, ':');
18390
18391 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18392
18393 u32 salt_len = hash_pos - salt_pos;
18394
18395 if (salt_len > 64) return (PARSER_SALT_LENGTH);
18396
18397 hash_pos++;
18398
18399 u32 hash_b64_len = input_len - (hash_pos - input_buf);
18400
18401 if (hash_b64_len > 88) return (PARSER_HASH_LENGTH);
18402
18403 // decode salt
18404
18405 char *salt_buf_ptr = (char *) pbkdf2_sha1->salt_buf;
18406
18407 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
18408
18409 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
18410
18411 salt_buf_ptr[salt_len + 3] = 0x01;
18412 salt_buf_ptr[salt_len + 4] = 0x80;
18413
18414 salt->salt_len = salt_len;
18415 salt->salt_iter = iter - 1;
18416
18417 // decode hash
18418
18419 u8 tmp_buf[100] = { 0 };
18420
18421 int hash_len = base64_decode (base64_to_int, (const u8 *) hash_pos, hash_b64_len, tmp_buf);
18422
18423 if (hash_len < 16) return (PARSER_HASH_LENGTH);
18424
18425 memcpy (digest, tmp_buf, 16);
18426
18427 digest[0] = byte_swap_32 (digest[0]);
18428 digest[1] = byte_swap_32 (digest[1]);
18429 digest[2] = byte_swap_32 (digest[2]);
18430 digest[3] = byte_swap_32 (digest[3]);
18431
18432 // add some stuff to normal salt to make sorted happy
18433
18434 salt->salt_buf[0] = pbkdf2_sha1->salt_buf[0];
18435 salt->salt_buf[1] = pbkdf2_sha1->salt_buf[1];
18436 salt->salt_buf[2] = pbkdf2_sha1->salt_buf[2];
18437 salt->salt_buf[3] = pbkdf2_sha1->salt_buf[3];
18438 salt->salt_buf[4] = salt->salt_iter;
18439
18440 return (PARSER_OK);
18441 }
18442
18443 int pbkdf2_sha512_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18444 {
18445 if ((input_len < DISPLAY_LEN_MIN_12100) || (input_len > DISPLAY_LEN_MAX_12100)) return (PARSER_GLOBAL_LENGTH);
18446
18447 if (memcmp (SIGNATURE_PBKDF2_SHA512, input_buf, 7)) return (PARSER_SIGNATURE_UNMATCHED);
18448
18449 u64 *digest = (u64 *) hash_buf->digest;
18450
18451 salt_t *salt = hash_buf->salt;
18452
18453 pbkdf2_sha512_t *pbkdf2_sha512 = (pbkdf2_sha512_t *) hash_buf->esalt;
18454
18455 /**
18456 * parse line
18457 */
18458
18459 // iterations
18460
18461 char *iter_pos = input_buf + 7;
18462
18463 u32 iter = atoi (iter_pos);
18464
18465 if (iter < 1) return (PARSER_SALT_ITERATION);
18466 if (iter > 999999) return (PARSER_SALT_ITERATION);
18467
18468 // first is *raw* salt
18469
18470 char *salt_pos = strchr (iter_pos, ':');
18471
18472 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18473
18474 salt_pos++;
18475
18476 char *hash_pos = strchr (salt_pos, ':');
18477
18478 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18479
18480 u32 salt_len = hash_pos - salt_pos;
18481
18482 if (salt_len > 64) return (PARSER_SALT_LENGTH);
18483
18484 hash_pos++;
18485
18486 u32 hash_b64_len = input_len - (hash_pos - input_buf);
18487
18488 if (hash_b64_len > 88) return (PARSER_HASH_LENGTH);
18489
18490 // decode salt
18491
18492 char *salt_buf_ptr = (char *) pbkdf2_sha512->salt_buf;
18493
18494 salt_len = parse_and_store_salt (salt_buf_ptr, salt_pos, salt_len);
18495
18496 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
18497
18498 salt_buf_ptr[salt_len + 3] = 0x01;
18499 salt_buf_ptr[salt_len + 4] = 0x80;
18500
18501 salt->salt_len = salt_len;
18502 salt->salt_iter = iter - 1;
18503
18504 // decode hash
18505
18506 u8 tmp_buf[100] = { 0 };
18507
18508 int hash_len = base64_decode (base64_to_int, (const u8 *) hash_pos, hash_b64_len, tmp_buf);
18509
18510 if (hash_len < 16) return (PARSER_HASH_LENGTH);
18511
18512 memcpy (digest, tmp_buf, 64);
18513
18514 digest[0] = byte_swap_64 (digest[0]);
18515 digest[1] = byte_swap_64 (digest[1]);
18516 digest[2] = byte_swap_64 (digest[2]);
18517 digest[3] = byte_swap_64 (digest[3]);
18518 digest[4] = byte_swap_64 (digest[4]);
18519 digest[5] = byte_swap_64 (digest[5]);
18520 digest[6] = byte_swap_64 (digest[6]);
18521 digest[7] = byte_swap_64 (digest[7]);
18522
18523 // add some stuff to normal salt to make sorted happy
18524
18525 salt->salt_buf[0] = pbkdf2_sha512->salt_buf[0];
18526 salt->salt_buf[1] = pbkdf2_sha512->salt_buf[1];
18527 salt->salt_buf[2] = pbkdf2_sha512->salt_buf[2];
18528 salt->salt_buf[3] = pbkdf2_sha512->salt_buf[3];
18529 salt->salt_buf[4] = salt->salt_iter;
18530
18531 return (PARSER_OK);
18532 }
18533
18534 int ecryptfs_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18535 {
18536 if ((input_len < DISPLAY_LEN_MIN_12200) || (input_len > DISPLAY_LEN_MAX_12200)) return (PARSER_GLOBAL_LENGTH);
18537
18538 if (memcmp (SIGNATURE_ECRYPTFS, input_buf, 10)) return (PARSER_SIGNATURE_UNMATCHED);
18539
18540 uint *digest = (uint *) hash_buf->digest;
18541
18542 salt_t *salt = hash_buf->salt;
18543
18544 /**
18545 * parse line
18546 */
18547
18548 char *salt_pos = input_buf + 10 + 2 + 2; // skip over "0$" and "1$"
18549
18550 char *hash_pos = strchr (salt_pos, '$');
18551
18552 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18553
18554 u32 salt_len = hash_pos - salt_pos;
18555
18556 if (salt_len != 16) return (PARSER_SALT_LENGTH);
18557
18558 hash_pos++;
18559
18560 u32 hash_len = input_len - 10 - 2 - 2 - salt_len - 1;
18561
18562 if (hash_len != 16) return (PARSER_HASH_LENGTH);
18563
18564 // decode hash
18565
18566 digest[ 0] = hex_to_u32 ((const u8 *) &hash_pos[0]);
18567 digest[ 1] = hex_to_u32 ((const u8 *) &hash_pos[8]);
18568 digest[ 2] = 0;
18569 digest[ 3] = 0;
18570 digest[ 4] = 0;
18571 digest[ 5] = 0;
18572 digest[ 6] = 0;
18573 digest[ 7] = 0;
18574 digest[ 8] = 0;
18575 digest[ 9] = 0;
18576 digest[10] = 0;
18577 digest[11] = 0;
18578 digest[12] = 0;
18579 digest[13] = 0;
18580 digest[14] = 0;
18581 digest[15] = 0;
18582
18583 // decode salt
18584
18585 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_pos[0]);
18586 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_pos[8]);
18587
18588 salt->salt_iter = ROUNDS_ECRYPTFS;
18589 salt->salt_len = 8;
18590
18591 return (PARSER_OK);
18592 }
18593
18594 int bsdicrypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18595 {
18596 if ((input_len < DISPLAY_LEN_MIN_12400) || (input_len > DISPLAY_LEN_MAX_12400)) return (PARSER_GLOBAL_LENGTH);
18597
18598 if (memcmp (SIGNATURE_BSDICRYPT, input_buf, 1)) return (PARSER_SIGNATURE_UNMATCHED);
18599
18600 unsigned char c19 = itoa64_to_int (input_buf[19]);
18601
18602 if (c19 & 3) return (PARSER_HASH_VALUE);
18603
18604 salt_t *salt = hash_buf->salt;
18605
18606 u32 *digest = (u32 *) hash_buf->digest;
18607
18608 // iteration count
18609
18610 salt->salt_iter = itoa64_to_int (input_buf[1])
18611 | itoa64_to_int (input_buf[2]) << 6
18612 | itoa64_to_int (input_buf[3]) << 12
18613 | itoa64_to_int (input_buf[4]) << 18;
18614
18615 // set salt
18616
18617 salt->salt_buf[0] = itoa64_to_int (input_buf[5])
18618 | itoa64_to_int (input_buf[6]) << 6
18619 | itoa64_to_int (input_buf[7]) << 12
18620 | itoa64_to_int (input_buf[8]) << 18;
18621
18622 salt->salt_len = 4;
18623
18624 u8 tmp_buf[100] = { 0 };
18625
18626 base64_decode (itoa64_to_int, (const u8 *) input_buf + 9, 11, tmp_buf);
18627
18628 memcpy (digest, tmp_buf, 8);
18629
18630 uint tt;
18631
18632 IP (digest[0], digest[1], tt);
18633
18634 digest[0] = rotr32 (digest[0], 31);
18635 digest[1] = rotr32 (digest[1], 31);
18636 digest[2] = 0;
18637 digest[3] = 0;
18638
18639 return (PARSER_OK);
18640 }
18641
18642 int rar3hp_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18643 {
18644 if ((input_len < DISPLAY_LEN_MIN_12500) || (input_len > DISPLAY_LEN_MAX_12500)) return (PARSER_GLOBAL_LENGTH);
18645
18646 if (memcmp (SIGNATURE_RAR3, input_buf, 6)) return (PARSER_SIGNATURE_UNMATCHED);
18647
18648 u32 *digest = (u32 *) hash_buf->digest;
18649
18650 salt_t *salt = hash_buf->salt;
18651
18652 /**
18653 * parse line
18654 */
18655
18656 char *type_pos = input_buf + 6 + 1;
18657
18658 char *salt_pos = strchr (type_pos, '*');
18659
18660 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18661
18662 u32 type_len = salt_pos - type_pos;
18663
18664 if (type_len != 1) return (PARSER_SALT_LENGTH);
18665
18666 salt_pos++;
18667
18668 char *crypted_pos = strchr (salt_pos, '*');
18669
18670 if (crypted_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18671
18672 u32 salt_len = crypted_pos - salt_pos;
18673
18674 if (salt_len != 16) return (PARSER_SALT_LENGTH);
18675
18676 crypted_pos++;
18677
18678 u32 crypted_len = input_len - 6 - 1 - type_len - 1 - salt_len - 1;
18679
18680 if (crypted_len != 32) return (PARSER_SALT_LENGTH);
18681
18682 /**
18683 * copy data
18684 */
18685
18686 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_pos[0]);
18687 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_pos[8]);
18688
18689 salt->salt_buf[0] = byte_swap_32 (salt->salt_buf[0]);
18690 salt->salt_buf[1] = byte_swap_32 (salt->salt_buf[1]);
18691
18692 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &crypted_pos[ 0]);
18693 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &crypted_pos[ 8]);
18694 salt->salt_buf[4] = hex_to_u32 ((const u8 *) &crypted_pos[16]);
18695 salt->salt_buf[5] = hex_to_u32 ((const u8 *) &crypted_pos[24]);
18696
18697 salt->salt_len = 24;
18698 salt->salt_iter = ROUNDS_RAR3;
18699
18700 // there's no hash for rar3. the data which is in crypted_pos is some encrypted data and
18701 // if it matches the value \xc4\x3d\x7b\x00\x40\x07\x00 after decrypt we know that we successfully cracked it.
18702
18703 digest[0] = 0xc43d7b00;
18704 digest[1] = 0x40070000;
18705 digest[2] = 0;
18706 digest[3] = 0;
18707
18708 return (PARSER_OK);
18709 }
18710
18711 int rar5_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18712 {
18713 if ((input_len < DISPLAY_LEN_MIN_13000) || (input_len > DISPLAY_LEN_MAX_13000)) return (PARSER_GLOBAL_LENGTH);
18714
18715 if (memcmp (SIGNATURE_RAR5, input_buf, 1 + 4 + 1)) return (PARSER_SIGNATURE_UNMATCHED);
18716
18717 u32 *digest = (u32 *) hash_buf->digest;
18718
18719 salt_t *salt = hash_buf->salt;
18720
18721 rar5_t *rar5 = (rar5_t *) hash_buf->esalt;
18722
18723 /**
18724 * parse line
18725 */
18726
18727 char *param0_pos = input_buf + 1 + 4 + 1;
18728
18729 char *param1_pos = strchr (param0_pos, '$');
18730
18731 if (param1_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18732
18733 u32 param0_len = param1_pos - param0_pos;
18734
18735 param1_pos++;
18736
18737 char *param2_pos = strchr (param1_pos, '$');
18738
18739 if (param2_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18740
18741 u32 param1_len = param2_pos - param1_pos;
18742
18743 param2_pos++;
18744
18745 char *param3_pos = strchr (param2_pos, '$');
18746
18747 if (param3_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18748
18749 u32 param2_len = param3_pos - param2_pos;
18750
18751 param3_pos++;
18752
18753 char *param4_pos = strchr (param3_pos, '$');
18754
18755 if (param4_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18756
18757 u32 param3_len = param4_pos - param3_pos;
18758
18759 param4_pos++;
18760
18761 char *param5_pos = strchr (param4_pos, '$');
18762
18763 if (param5_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18764
18765 u32 param4_len = param5_pos - param4_pos;
18766
18767 param5_pos++;
18768
18769 u32 param5_len = input_len - 1 - 4 - 1 - param0_len - 1 - param1_len - 1 - param2_len - 1 - param3_len - 1 - param4_len - 1;
18770
18771 char *salt_buf = param1_pos;
18772 char *iv = param3_pos;
18773 char *pswcheck = param5_pos;
18774
18775 const uint salt_len = atoi (param0_pos);
18776 const uint iterations = atoi (param2_pos);
18777 const uint pswcheck_len = atoi (param4_pos);
18778
18779 /**
18780 * verify some data
18781 */
18782
18783 if (param1_len != 32) return (PARSER_SALT_VALUE);
18784 if (param3_len != 32) return (PARSER_SALT_VALUE);
18785 if (param5_len != 16) return (PARSER_SALT_VALUE);
18786
18787 if (salt_len != 16) return (PARSER_SALT_VALUE);
18788 if (iterations == 0) return (PARSER_SALT_VALUE);
18789 if (pswcheck_len != 8) return (PARSER_SALT_VALUE);
18790
18791 /**
18792 * store data
18793 */
18794
18795 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_buf[ 0]);
18796 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_buf[ 8]);
18797 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &salt_buf[16]);
18798 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &salt_buf[24]);
18799
18800 rar5->iv[0] = hex_to_u32 ((const u8 *) &iv[ 0]);
18801 rar5->iv[1] = hex_to_u32 ((const u8 *) &iv[ 8]);
18802 rar5->iv[2] = hex_to_u32 ((const u8 *) &iv[16]);
18803 rar5->iv[3] = hex_to_u32 ((const u8 *) &iv[24]);
18804
18805 salt->salt_len = 16;
18806
18807 salt->salt_sign[0] = iterations;
18808
18809 salt->salt_iter = ((1 << iterations) + 32) - 1;
18810
18811 /**
18812 * digest buf
18813 */
18814
18815 digest[0] = hex_to_u32 ((const u8 *) &pswcheck[ 0]);
18816 digest[1] = hex_to_u32 ((const u8 *) &pswcheck[ 8]);
18817 digest[2] = 0;
18818 digest[3] = 0;
18819
18820 return (PARSER_OK);
18821 }
18822
18823 int krb5tgs_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18824 {
18825 if ((input_len < DISPLAY_LEN_MIN_13100) || (input_len > DISPLAY_LEN_MAX_13100)) return (PARSER_GLOBAL_LENGTH);
18826
18827 if (memcmp (SIGNATURE_KRB5TGS, input_buf, 11)) return (PARSER_SIGNATURE_UNMATCHED);
18828
18829 u32 *digest = (u32 *) hash_buf->digest;
18830
18831 salt_t *salt = hash_buf->salt;
18832
18833 krb5tgs_t *krb5tgs = (krb5tgs_t *) hash_buf->esalt;
18834
18835 /**
18836 * parse line
18837 */
18838
18839 /* Skip '$' */
18840 char *account_pos = input_buf + 11 + 1;
18841
18842 char *data_pos;
18843
18844 uint data_len;
18845
18846 if (account_pos[0] == '*')
18847 {
18848 account_pos++;
18849
18850 data_pos = strchr (account_pos, '*');
18851
18852 /* Skip '*' */
18853 data_pos++;
18854
18855 if (data_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18856
18857 uint account_len = data_pos - account_pos + 1;
18858
18859 if (account_len >= 512) return (PARSER_SALT_LENGTH);
18860
18861 /* Skip '$' */
18862 data_pos++;
18863
18864 data_len = input_len - 11 - 1 - account_len - 2;
18865
18866 memcpy (krb5tgs->account_info, account_pos - 1, account_len);
18867 }
18868 else
18869 {
18870 /* assume $krb5tgs$23$checksum$edata2 */
18871 data_pos = account_pos;
18872
18873 memcpy (krb5tgs->account_info, "**", 3);
18874
18875 data_len = input_len - 11 - 1 - 1;
18876 }
18877
18878 if (data_len < ((16 + 32) * 2)) return (PARSER_SALT_LENGTH);
18879
18880 char *checksum_ptr = (char *) krb5tgs->checksum;
18881
18882 for (uint i = 0; i < 16 * 2; i += 2)
18883 {
18884 const char p0 = data_pos[i + 0];
18885 const char p1 = data_pos[i + 1];
18886
18887 *checksum_ptr++ = hex_convert (p1) << 0
18888 | hex_convert (p0) << 4;
18889 }
18890
18891 char *edata_ptr = (char *) krb5tgs->edata2;
18892
18893 krb5tgs->edata2_len = (data_len - 32) / 2 ;
18894
18895 /* skip '$' */
18896 for (uint i = 16 * 2 + 1; i < (krb5tgs->edata2_len * 2) + (16 * 2 + 1); i += 2)
18897 {
18898 const char p0 = data_pos[i + 0];
18899 const char p1 = data_pos[i + 1];
18900 *edata_ptr++ = hex_convert (p1) << 0
18901 | hex_convert (p0) << 4;
18902 }
18903
18904 /* this is needed for hmac_md5 */
18905 *edata_ptr++ = 0x80;
18906
18907 salt->salt_buf[0] = krb5tgs->checksum[0];
18908 salt->salt_buf[1] = krb5tgs->checksum[1];
18909 salt->salt_buf[2] = krb5tgs->checksum[2];
18910 salt->salt_buf[3] = krb5tgs->checksum[3];
18911
18912 salt->salt_len = 32;
18913
18914 digest[0] = krb5tgs->checksum[0];
18915 digest[1] = krb5tgs->checksum[1];
18916 digest[2] = krb5tgs->checksum[2];
18917 digest[3] = krb5tgs->checksum[3];
18918
18919 return (PARSER_OK);
18920 }
18921
18922 int axcrypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18923 {
18924 if ((input_len < DISPLAY_LEN_MIN_13200) || (input_len > DISPLAY_LEN_MAX_13200)) return (PARSER_GLOBAL_LENGTH);
18925
18926 if (memcmp (SIGNATURE_AXCRYPT, input_buf, 11)) return (PARSER_SIGNATURE_UNMATCHED);
18927
18928 u32 *digest = (u32 *) hash_buf->digest;
18929
18930 salt_t *salt = hash_buf->salt;
18931
18932 /**
18933 * parse line
18934 */
18935
18936 /* Skip '*' */
18937 char *wrapping_rounds_pos = input_buf + 11 + 1;
18938
18939 char *salt_pos;
18940
18941 char *wrapped_key_pos;
18942
18943 char *data_pos;
18944
18945 salt->salt_iter = atoi (wrapping_rounds_pos);
18946
18947 salt_pos = strchr (wrapping_rounds_pos, '*');
18948
18949 if (salt_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18950
18951 uint wrapping_rounds_len = salt_pos - wrapping_rounds_pos;
18952
18953 /* Skip '*' */
18954 salt_pos++;
18955
18956 data_pos = salt_pos;
18957
18958 wrapped_key_pos = strchr (salt_pos, '*');
18959
18960 if (wrapped_key_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
18961
18962 uint salt_len = wrapped_key_pos - salt_pos;
18963
18964 if (salt_len != 32) return (PARSER_SALT_LENGTH);
18965
18966 /* Skip '*' */
18967 wrapped_key_pos++;
18968
18969 uint wrapped_key_len = input_len - 11 - 1 - wrapping_rounds_len - 1 - salt_len - 1;
18970
18971 if (wrapped_key_len != 48) return (PARSER_SALT_LENGTH);
18972
18973 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &data_pos[ 0]);
18974 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &data_pos[ 8]);
18975 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &data_pos[16]);
18976 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &data_pos[24]);
18977
18978 data_pos += 33;
18979
18980 salt->salt_buf[4] = hex_to_u32 ((const u8 *) &data_pos[ 0]);
18981 salt->salt_buf[5] = hex_to_u32 ((const u8 *) &data_pos[ 8]);
18982 salt->salt_buf[6] = hex_to_u32 ((const u8 *) &data_pos[16]);
18983 salt->salt_buf[7] = hex_to_u32 ((const u8 *) &data_pos[24]);
18984 salt->salt_buf[8] = hex_to_u32 ((const u8 *) &data_pos[32]);
18985 salt->salt_buf[9] = hex_to_u32 ((const u8 *) &data_pos[40]);
18986
18987 salt->salt_len = 40;
18988
18989 digest[0] = salt->salt_buf[0];
18990 digest[1] = salt->salt_buf[1];
18991 digest[2] = salt->salt_buf[2];
18992 digest[3] = salt->salt_buf[3];
18993
18994 return (PARSER_OK);
18995 }
18996
18997 int cf10_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
18998 {
18999 if ((input_len < DISPLAY_LEN_MIN_12600) || (input_len > DISPLAY_LEN_MAX_12600)) return (PARSER_GLOBAL_LENGTH);
19000
19001 u32 *digest = (u32 *) hash_buf->digest;
19002
19003 salt_t *salt = hash_buf->salt;
19004
19005 digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
19006 digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
19007 digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
19008 digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
19009 digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
19010 digest[5] = hex_to_u32 ((const u8 *) &input_buf[40]);
19011 digest[6] = hex_to_u32 ((const u8 *) &input_buf[48]);
19012 digest[7] = hex_to_u32 ((const u8 *) &input_buf[56]);
19013
19014 if (input_buf[64] != data.separator) return (PARSER_SEPARATOR_UNMATCHED);
19015
19016 uint salt_len = input_len - 64 - 1;
19017
19018 char *salt_buf = input_buf + 64 + 1;
19019
19020 char *salt_buf_ptr = (char *) salt->salt_buf;
19021
19022 salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len);
19023
19024 if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH);
19025
19026 salt->salt_len = salt_len;
19027
19028 /**
19029 * we can precompute the first sha256 transform
19030 */
19031
19032 uint w[16] = { 0 };
19033
19034 w[ 0] = byte_swap_32 (salt->salt_buf[ 0]);
19035 w[ 1] = byte_swap_32 (salt->salt_buf[ 1]);
19036 w[ 2] = byte_swap_32 (salt->salt_buf[ 2]);
19037 w[ 3] = byte_swap_32 (salt->salt_buf[ 3]);
19038 w[ 4] = byte_swap_32 (salt->salt_buf[ 4]);
19039 w[ 5] = byte_swap_32 (salt->salt_buf[ 5]);
19040 w[ 6] = byte_swap_32 (salt->salt_buf[ 6]);
19041 w[ 7] = byte_swap_32 (salt->salt_buf[ 7]);
19042 w[ 8] = byte_swap_32 (salt->salt_buf[ 8]);
19043 w[ 9] = byte_swap_32 (salt->salt_buf[ 9]);
19044 w[10] = byte_swap_32 (salt->salt_buf[10]);
19045 w[11] = byte_swap_32 (salt->salt_buf[11]);
19046 w[12] = byte_swap_32 (salt->salt_buf[12]);
19047 w[13] = byte_swap_32 (salt->salt_buf[13]);
19048 w[14] = byte_swap_32 (salt->salt_buf[14]);
19049 w[15] = byte_swap_32 (salt->salt_buf[15]);
19050
19051 uint pc256[8] = { SHA256M_A, SHA256M_B, SHA256M_C, SHA256M_D, SHA256M_E, SHA256M_F, SHA256M_G, SHA256M_H };
19052
19053 sha256_64 (w, pc256);
19054
19055 salt->salt_buf_pc[0] = pc256[0];
19056 salt->salt_buf_pc[1] = pc256[1];
19057 salt->salt_buf_pc[2] = pc256[2];
19058 salt->salt_buf_pc[3] = pc256[3];
19059 salt->salt_buf_pc[4] = pc256[4];
19060 salt->salt_buf_pc[5] = pc256[5];
19061 salt->salt_buf_pc[6] = pc256[6];
19062 salt->salt_buf_pc[7] = pc256[7];
19063
19064 digest[0] -= pc256[0];
19065 digest[1] -= pc256[1];
19066 digest[2] -= pc256[2];
19067 digest[3] -= pc256[3];
19068 digest[4] -= pc256[4];
19069 digest[5] -= pc256[5];
19070 digest[6] -= pc256[6];
19071 digest[7] -= pc256[7];
19072
19073 return (PARSER_OK);
19074 }
19075
19076 int mywallet_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
19077 {
19078 if ((input_len < DISPLAY_LEN_MIN_12700) || (input_len > DISPLAY_LEN_MAX_12700)) return (PARSER_GLOBAL_LENGTH);
19079
19080 if (memcmp (SIGNATURE_MYWALLET, input_buf, 12)) return (PARSER_SIGNATURE_UNMATCHED);
19081
19082 u32 *digest = (u32 *) hash_buf->digest;
19083
19084 salt_t *salt = hash_buf->salt;
19085
19086 /**
19087 * parse line
19088 */
19089
19090 char *data_len_pos = input_buf + 1 + 10 + 1;
19091
19092 char *data_buf_pos = strchr (data_len_pos, '$');
19093
19094 if (data_buf_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19095
19096 u32 data_len_len = data_buf_pos - data_len_pos;
19097
19098 if (data_len_len < 1) return (PARSER_SALT_LENGTH);
19099 if (data_len_len > 5) return (PARSER_SALT_LENGTH);
19100
19101 data_buf_pos++;
19102
19103 u32 data_buf_len = input_len - 1 - 10 - 1 - data_len_len - 1;
19104
19105 if (data_buf_len < 64) return (PARSER_HASH_LENGTH);
19106
19107 if (data_buf_len % 16) return (PARSER_HASH_LENGTH);
19108
19109 u32 data_len = atoi (data_len_pos);
19110
19111 if ((data_len * 2) != data_buf_len) return (PARSER_HASH_LENGTH);
19112
19113 /**
19114 * salt
19115 */
19116
19117 char *salt_pos = data_buf_pos;
19118
19119 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_pos[ 0]);
19120 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_pos[ 8]);
19121 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &salt_pos[16]);
19122 salt->salt_buf[3] = hex_to_u32 ((const u8 *) &salt_pos[24]);
19123
19124 // this is actually the CT, which is also the hash later (if matched)
19125
19126 salt->salt_buf[4] = hex_to_u32 ((const u8 *) &salt_pos[32]);
19127 salt->salt_buf[5] = hex_to_u32 ((const u8 *) &salt_pos[40]);
19128 salt->salt_buf[6] = hex_to_u32 ((const u8 *) &salt_pos[48]);
19129 salt->salt_buf[7] = hex_to_u32 ((const u8 *) &salt_pos[56]);
19130
19131 salt->salt_len = 32; // note we need to fix this to 16 in kernel
19132
19133 salt->salt_iter = 10 - 1;
19134
19135 /**
19136 * digest buf
19137 */
19138
19139 digest[0] = salt->salt_buf[4];
19140 digest[1] = salt->salt_buf[5];
19141 digest[2] = salt->salt_buf[6];
19142 digest[3] = salt->salt_buf[7];
19143
19144 return (PARSER_OK);
19145 }
19146
19147 int ms_drsr_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
19148 {
19149 if ((input_len < DISPLAY_LEN_MIN_12800) || (input_len > DISPLAY_LEN_MAX_12800)) return (PARSER_GLOBAL_LENGTH);
19150
19151 if (memcmp (SIGNATURE_MS_DRSR, input_buf, 11)) return (PARSER_SIGNATURE_UNMATCHED);
19152
19153 u32 *digest = (u32 *) hash_buf->digest;
19154
19155 salt_t *salt = hash_buf->salt;
19156
19157 /**
19158 * parse line
19159 */
19160
19161 char *salt_pos = input_buf + 11 + 1;
19162
19163 char *iter_pos = strchr (salt_pos, ',');
19164
19165 if (iter_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19166
19167 u32 salt_len = iter_pos - salt_pos;
19168
19169 if (salt_len != 20) return (PARSER_SALT_LENGTH);
19170
19171 iter_pos++;
19172
19173 char *hash_pos = strchr (iter_pos, ',');
19174
19175 if (hash_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
19176
19177 u32 iter_len = hash_pos - iter_pos;
19178
19179 if (iter_len > 5) return (PARSER_SALT_LENGTH);
19180
19181 hash_pos++;
19182
19183 u32 hash_len = input_len - 11 - 1 - salt_len - 1 - iter_len - 1;
19184
19185 if (hash_len != 64) return (PARSER_HASH_LENGTH);
19186
19187 /**
19188 * salt
19189 */
19190
19191 salt->salt_buf[0] = hex_to_u32 ((const u8 *) &salt_pos[ 0]);
19192 salt->salt_buf[1] = hex_to_u32 ((const u8 *) &salt_pos[ 8]);
19193 salt->salt_buf[2] = hex_to_u32 ((const u8 *) &salt_pos[16]) & 0xffff0000;
19194 salt->salt_buf[3] = 0x00018000;
19195
19196 salt->salt_buf[0] = byte_swap_32 (salt->salt_buf[0]);
19197 salt->salt_buf[1] = byte_swap_32 (salt->salt_buf[1]);
19198 salt->salt_buf[2] = byte_swap_32 (salt->salt_buf[2]);
19199 salt->salt_buf[3] = byte_swap_32 (salt->salt_buf[3]);
19200
19201 salt->salt_len = salt_len / 2;
19202
19203 salt->salt_iter = atoi (iter_pos) - 1;
19204
19205 /**
19206 * digest buf
19207 */
19208
19209 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
19210 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
19211 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
19212 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
19213 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
19214 digest[5] = hex_to_u32 ((const u8 *) &hash_pos[40]);
19215 digest[6] = hex_to_u32 ((const u8 *) &hash_pos[48]);
19216 digest[7] = hex_to_u32 ((const u8 *) &hash_pos[56]);
19217
19218 return (PARSER_OK);
19219 }
19220
19221 int androidfde_samsung_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
19222 {
19223 if ((input_len < DISPLAY_LEN_MIN_12900) || (input_len > DISPLAY_LEN_MAX_12900)) return (PARSER_GLOBAL_LENGTH);
19224
19225 u32 *digest = (u32 *) hash_buf->digest;
19226
19227 salt_t *salt = hash_buf->salt;
19228
19229 /**
19230 * parse line
19231 */
19232
19233 char *hash_pos = input_buf + 64;
19234 char *salt1_pos = input_buf + 128;
19235 char *salt2_pos = input_buf;
19236
19237 /**
19238 * salt
19239 */
19240
19241 salt->salt_buf[ 0] = hex_to_u32 ((const u8 *) &salt1_pos[ 0]);
19242 salt->salt_buf[ 1] = hex_to_u32 ((const u8 *) &salt1_pos[ 8]);
19243 salt->salt_buf[ 2] = hex_to_u32 ((const u8 *) &salt1_pos[16]);
19244 salt->salt_buf[ 3] = hex_to_u32 ((const u8 *) &salt1_pos[24]);
19245
19246 salt->salt_buf[ 4] = hex_to_u32 ((const u8 *) &salt2_pos[ 0]);
19247 salt->salt_buf[ 5] = hex_to_u32 ((const u8 *) &salt2_pos[ 8]);
19248 salt->salt_buf[ 6] = hex_to_u32 ((const u8 *) &salt2_pos[16]);
19249 salt->salt_buf[ 7] = hex_to_u32 ((const u8 *) &salt2_pos[24]);
19250
19251 salt->salt_buf[ 8] = hex_to_u32 ((const u8 *) &salt2_pos[32]);
19252 salt->salt_buf[ 9] = hex_to_u32 ((const u8 *) &salt2_pos[40]);
19253 salt->salt_buf[10] = hex_to_u32 ((const u8 *) &salt2_pos[48]);
19254 salt->salt_buf[11] = hex_to_u32 ((const u8 *) &salt2_pos[56]);
19255
19256 salt->salt_len = 48;
19257
19258 salt->salt_iter = ROUNDS_ANDROIDFDE_SAMSUNG - 1;
19259
19260 /**
19261 * digest buf
19262 */
19263
19264 digest[0] = hex_to_u32 ((const u8 *) &hash_pos[ 0]);
19265 digest[1] = hex_to_u32 ((const u8 *) &hash_pos[ 8]);
19266 digest[2] = hex_to_u32 ((const u8 *) &hash_pos[16]);
19267 digest[3] = hex_to_u32 ((const u8 *) &hash_pos[24]);
19268 digest[4] = hex_to_u32 ((const u8 *) &hash_pos[32]);
19269 digest[5] = hex_to_u32 ((const u8 *) &hash_pos[40]);
19270 digest[6] = hex_to_u32 ((const u8 *) &hash_pos[48]);
19271 digest[7] = hex_to_u32 ((const u8 *) &hash_pos[56]);
19272
19273 return (PARSER_OK);
19274 }
19275
19276 /**
19277 * parallel running threads
19278 */
19279
19280 #ifdef WIN
19281
19282 BOOL WINAPI sigHandler_default (DWORD sig)
19283 {
19284 switch (sig)
19285 {
19286 case CTRL_CLOSE_EVENT:
19287
19288 /*
19289 * special case see: https://stackoverflow.com/questions/3640633/c-setconsolectrlhandler-routine-issue/5610042#5610042
19290 * if the user interacts w/ the user-interface (GUI/cmd), we need to do the finalization job within this signal handler
19291 * function otherwise it is too late (e.g. after returning from this function)
19292 */
19293
19294 myabort ();
19295
19296 SetConsoleCtrlHandler (NULL, TRUE);
19297
19298 hc_sleep (10);
19299
19300 return TRUE;
19301
19302 case CTRL_C_EVENT:
19303 case CTRL_LOGOFF_EVENT:
19304 case CTRL_SHUTDOWN_EVENT:
19305
19306 myabort ();
19307
19308 SetConsoleCtrlHandler (NULL, TRUE);
19309
19310 return TRUE;
19311 }
19312
19313 return FALSE;
19314 }
19315
19316 BOOL WINAPI sigHandler_benchmark (DWORD sig)
19317 {
19318 switch (sig)
19319 {
19320 case CTRL_CLOSE_EVENT:
19321
19322 myabort ();
19323
19324 SetConsoleCtrlHandler (NULL, TRUE);
19325
19326 hc_sleep (10);
19327
19328 return TRUE;
19329
19330 case CTRL_C_EVENT:
19331 case CTRL_LOGOFF_EVENT:
19332 case CTRL_SHUTDOWN_EVENT:
19333
19334 myquit ();
19335
19336 SetConsoleCtrlHandler (NULL, TRUE);
19337
19338 return TRUE;
19339 }
19340
19341 return FALSE;
19342 }
19343
19344 void hc_signal (BOOL WINAPI (callback) (DWORD))
19345 {
19346 if (callback == NULL)
19347 {
19348 SetConsoleCtrlHandler ((PHANDLER_ROUTINE) callback, FALSE);
19349 }
19350 else
19351 {
19352 SetConsoleCtrlHandler ((PHANDLER_ROUTINE) callback, TRUE);
19353 }
19354 }
19355
19356 #else
19357
19358 void sigHandler_default (int sig)
19359 {
19360 myabort ();
19361
19362 signal (sig, NULL);
19363 }
19364
19365 void sigHandler_benchmark (int sig)
19366 {
19367 myquit ();
19368
19369 signal (sig, NULL);
19370 }
19371
19372 void hc_signal (void (callback) (int))
19373 {
19374 if (callback == NULL) callback = SIG_DFL;
19375
19376 signal (SIGINT, callback);
19377 signal (SIGTERM, callback);
19378 signal (SIGABRT, callback);
19379 }
19380
19381 #endif
19382
19383 void status_display ();
19384
19385 void *thread_keypress (void *p)
19386 {
19387 int benchmark = *((int *) p);
19388
19389 uint quiet = data.quiet;
19390
19391 tty_break();
19392
19393 while ((data.devices_status != STATUS_EXHAUSTED) && (data.devices_status != STATUS_CRACKED) && (data.devices_status != STATUS_ABORTED) && (data.devices_status != STATUS_QUIT))
19394 {
19395 int ch = tty_getchar();
19396
19397 if (ch == -1) break;
19398
19399 if (ch == 0) continue;
19400
19401 #ifdef _POSIX
19402 if (ch != '\n')
19403 #endif
19404
19405 hc_thread_mutex_lock (mux_display);
19406
19407 log_info ("");
19408
19409 switch (ch)
19410 {
19411 case 's':
19412 case '\n':
19413
19414 log_info ("");
19415
19416 status_display ();
19417
19418 log_info ("");
19419
19420 if (quiet == 0) fprintf (stdout, "%s", PROMPT);
19421 if (quiet == 0) fflush (stdout);
19422
19423 break;
19424
19425 case 'b':
19426
19427 log_info ("");
19428
19429 bypass ();
19430
19431 log_info ("");
19432
19433 if (quiet == 0) fprintf (stdout, "%s", PROMPT);
19434 if (quiet == 0) fflush (stdout);
19435
19436 break;
19437
19438 case 'p':
19439
19440 log_info ("");
19441
19442 SuspendThreads ();
19443
19444 log_info ("");
19445
19446 if (quiet == 0) fprintf (stdout, "%s", PROMPT);
19447 if (quiet == 0) fflush (stdout);
19448
19449 break;
19450
19451 case 'r':
19452
19453 log_info ("");
19454
19455 ResumeThreads ();
19456
19457 log_info ("");
19458
19459 if (quiet == 0) fprintf (stdout, "%s", PROMPT);
19460 if (quiet == 0) fflush (stdout);
19461
19462 break;
19463
19464 case 'c':
19465
19466 log_info ("");
19467
19468 if (benchmark == 1) break;
19469
19470 stop_at_checkpoint ();
19471
19472 log_info ("");
19473
19474 if (quiet == 0) fprintf (stdout, "%s", PROMPT);
19475 if (quiet == 0) fflush (stdout);
19476
19477 break;
19478
19479 case 'q':
19480
19481 log_info ("");
19482
19483 if (benchmark == 1)
19484 {
19485 myquit ();
19486 }
19487 else
19488 {
19489 myabort ();
19490 }
19491
19492 break;
19493 }
19494
19495 hc_thread_mutex_unlock (mux_display);
19496 }
19497
19498 tty_fix();
19499
19500 return (p);
19501 }
19502
19503 /**
19504 * rules common
19505 */
19506
19507 bool class_num (const u8 c)
19508 {
19509 return ((c >= '0') && (c <= '9'));
19510 }
19511
19512 bool class_lower (const u8 c)
19513 {
19514 return ((c >= 'a') && (c <= 'z'));
19515 }
19516
19517 bool class_upper (const u8 c)
19518 {
19519 return ((c >= 'A') && (c <= 'Z'));
19520 }
19521
19522 bool class_alpha (const u8 c)
19523 {
19524 return (class_lower (c) || class_upper (c));
19525 }
19526
19527 int conv_ctoi (const u8 c)
19528 {
19529 if (class_num (c))
19530 {
19531 return c - '0';
19532 }
19533 else if (class_upper (c))
19534 {
19535 return c - 'A' + 10;
19536 }
19537
19538 return -1;
19539 }
19540
19541 int conv_itoc (const u8 c)
19542 {
19543 if (c < 10)
19544 {
19545 return c + '0';
19546 }
19547 else if (c < 37)
19548 {
19549 return c + 'A' - 10;
19550 }
19551
19552 return -1;
19553 }
19554
19555 /**
19556 * device rules
19557 */
19558
19559 #define INCR_POS if (++rule_pos == rule_len) return (-1)
19560 #define SET_NAME(rule,val) (rule)->cmds[rule_cnt] = ((val) & 0xff) << 0
19561 #define SET_P0(rule,val) INCR_POS; (rule)->cmds[rule_cnt] |= ((val) & 0xff) << 8
19562 #define SET_P1(rule,val) INCR_POS; (rule)->cmds[rule_cnt] |= ((val) & 0xff) << 16
19563 #define MAX_KERNEL_RULES 255
19564 #define GET_NAME(rule) rule_cmd = (((rule)->cmds[rule_cnt] >> 0) & 0xff)
19565 #define GET_P0(rule) INCR_POS; rule_buf[rule_pos] = (((rule)->cmds[rule_cnt] >> 8) & 0xff)
19566 #define GET_P1(rule) INCR_POS; rule_buf[rule_pos] = (((rule)->cmds[rule_cnt] >> 16) & 0xff)
19567
19568 #define SET_P0_CONV(rule,val) INCR_POS; (rule)->cmds[rule_cnt] |= ((conv_ctoi (val)) & 0xff) << 8
19569 #define SET_P1_CONV(rule,val) INCR_POS; (rule)->cmds[rule_cnt] |= ((conv_ctoi (val)) & 0xff) << 16
19570 #define GET_P0_CONV(rule) INCR_POS; rule_buf[rule_pos] = conv_itoc (((rule)->cmds[rule_cnt] >> 8) & 0xff)
19571 #define GET_P1_CONV(rule) INCR_POS; rule_buf[rule_pos] = conv_itoc (((rule)->cmds[rule_cnt] >> 16) & 0xff)
19572
19573 int cpu_rule_to_kernel_rule (char rule_buf[BUFSIZ], uint rule_len, kernel_rule_t *rule)
19574 {
19575 uint rule_pos;
19576 uint rule_cnt;
19577
19578 for (rule_pos = 0, rule_cnt = 0; rule_pos < rule_len && rule_cnt < MAX_KERNEL_RULES; rule_pos++, rule_cnt++)
19579 {
19580 switch (rule_buf[rule_pos])
19581 {
19582 case ' ':
19583 rule_cnt--;
19584 break;
19585
19586 case RULE_OP_MANGLE_NOOP:
19587 SET_NAME (rule, rule_buf[rule_pos]);
19588 break;
19589
19590 case RULE_OP_MANGLE_LREST:
19591 SET_NAME (rule, rule_buf[rule_pos]);
19592 break;
19593
19594 case RULE_OP_MANGLE_UREST:
19595 SET_NAME (rule, rule_buf[rule_pos]);
19596 break;
19597
19598 case RULE_OP_MANGLE_LREST_UFIRST:
19599 SET_NAME (rule, rule_buf[rule_pos]);
19600 break;
19601
19602 case RULE_OP_MANGLE_UREST_LFIRST:
19603 SET_NAME (rule, rule_buf[rule_pos]);
19604 break;
19605
19606 case RULE_OP_MANGLE_TREST:
19607 SET_NAME (rule, rule_buf[rule_pos]);
19608 break;
19609
19610 case RULE_OP_MANGLE_TOGGLE_AT:
19611 SET_NAME (rule, rule_buf[rule_pos]);
19612 SET_P0_CONV (rule, rule_buf[rule_pos]);
19613 break;
19614
19615 case RULE_OP_MANGLE_REVERSE:
19616 SET_NAME (rule, rule_buf[rule_pos]);
19617 break;
19618
19619 case RULE_OP_MANGLE_DUPEWORD:
19620 SET_NAME (rule, rule_buf[rule_pos]);
19621 break;
19622
19623 case RULE_OP_MANGLE_DUPEWORD_TIMES:
19624 SET_NAME (rule, rule_buf[rule_pos]);
19625 SET_P0_CONV (rule, rule_buf[rule_pos]);
19626 break;
19627
19628 case RULE_OP_MANGLE_REFLECT:
19629 SET_NAME (rule, rule_buf[rule_pos]);
19630 break;
19631
19632 case RULE_OP_MANGLE_ROTATE_LEFT:
19633 SET_NAME (rule, rule_buf[rule_pos]);
19634 break;
19635
19636 case RULE_OP_MANGLE_ROTATE_RIGHT:
19637 SET_NAME (rule, rule_buf[rule_pos]);
19638 break;
19639
19640 case RULE_OP_MANGLE_APPEND:
19641 SET_NAME (rule, rule_buf[rule_pos]);
19642 SET_P0 (rule, rule_buf[rule_pos]);
19643 break;
19644
19645 case RULE_OP_MANGLE_PREPEND:
19646 SET_NAME (rule, rule_buf[rule_pos]);
19647 SET_P0 (rule, rule_buf[rule_pos]);
19648 break;
19649
19650 case RULE_OP_MANGLE_DELETE_FIRST:
19651 SET_NAME (rule, rule_buf[rule_pos]);
19652 break;
19653
19654 case RULE_OP_MANGLE_DELETE_LAST:
19655 SET_NAME (rule, rule_buf[rule_pos]);
19656 break;
19657
19658 case RULE_OP_MANGLE_DELETE_AT:
19659 SET_NAME (rule, rule_buf[rule_pos]);
19660 SET_P0_CONV (rule, rule_buf[rule_pos]);
19661 break;
19662
19663 case RULE_OP_MANGLE_EXTRACT:
19664 SET_NAME (rule, rule_buf[rule_pos]);
19665 SET_P0_CONV (rule, rule_buf[rule_pos]);
19666 SET_P1_CONV (rule, rule_buf[rule_pos]);
19667 break;
19668
19669 case RULE_OP_MANGLE_OMIT:
19670 SET_NAME (rule, rule_buf[rule_pos]);
19671 SET_P0_CONV (rule, rule_buf[rule_pos]);
19672 SET_P1_CONV (rule, rule_buf[rule_pos]);
19673 break;
19674
19675 case RULE_OP_MANGLE_INSERT:
19676 SET_NAME (rule, rule_buf[rule_pos]);
19677 SET_P0_CONV (rule, rule_buf[rule_pos]);
19678 SET_P1 (rule, rule_buf[rule_pos]);
19679 break;
19680
19681 case RULE_OP_MANGLE_OVERSTRIKE:
19682 SET_NAME (rule, rule_buf[rule_pos]);
19683 SET_P0_CONV (rule, rule_buf[rule_pos]);
19684 SET_P1 (rule, rule_buf[rule_pos]);
19685 break;
19686
19687 case RULE_OP_MANGLE_TRUNCATE_AT:
19688 SET_NAME (rule, rule_buf[rule_pos]);
19689 SET_P0_CONV (rule, rule_buf[rule_pos]);
19690 break;
19691
19692 case RULE_OP_MANGLE_REPLACE:
19693 SET_NAME (rule, rule_buf[rule_pos]);
19694 SET_P0 (rule, rule_buf[rule_pos]);
19695 SET_P1 (rule, rule_buf[rule_pos]);
19696 break;
19697
19698 case RULE_OP_MANGLE_PURGECHAR:
19699 return (-1);
19700 break;
19701
19702 case RULE_OP_MANGLE_TOGGLECASE_REC:
19703 return (-1);
19704 break;
19705
19706 case RULE_OP_MANGLE_DUPECHAR_FIRST:
19707 SET_NAME (rule, rule_buf[rule_pos]);
19708 SET_P0_CONV (rule, rule_buf[rule_pos]);
19709 break;
19710
19711 case RULE_OP_MANGLE_DUPECHAR_LAST:
19712 SET_NAME (rule, rule_buf[rule_pos]);
19713 SET_P0_CONV (rule, rule_buf[rule_pos]);
19714 break;
19715
19716 case RULE_OP_MANGLE_DUPECHAR_ALL:
19717 SET_NAME (rule, rule_buf[rule_pos]);
19718 break;
19719
19720 case RULE_OP_MANGLE_SWITCH_FIRST:
19721 SET_NAME (rule, rule_buf[rule_pos]);
19722 break;
19723
19724 case RULE_OP_MANGLE_SWITCH_LAST:
19725 SET_NAME (rule, rule_buf[rule_pos]);
19726 break;
19727
19728 case RULE_OP_MANGLE_SWITCH_AT:
19729 SET_NAME (rule, rule_buf[rule_pos]);
19730 SET_P0_CONV (rule, rule_buf[rule_pos]);
19731 SET_P1_CONV (rule, rule_buf[rule_pos]);
19732 break;
19733
19734 case RULE_OP_MANGLE_CHR_SHIFTL:
19735 SET_NAME (rule, rule_buf[rule_pos]);
19736 SET_P0_CONV (rule, rule_buf[rule_pos]);
19737 break;
19738
19739 case RULE_OP_MANGLE_CHR_SHIFTR:
19740 SET_NAME (rule, rule_buf[rule_pos]);
19741 SET_P0_CONV (rule, rule_buf[rule_pos]);
19742 break;
19743
19744 case RULE_OP_MANGLE_CHR_INCR:
19745 SET_NAME (rule, rule_buf[rule_pos]);
19746 SET_P0_CONV (rule, rule_buf[rule_pos]);
19747 break;
19748
19749 case RULE_OP_MANGLE_CHR_DECR:
19750 SET_NAME (rule, rule_buf[rule_pos]);
19751 SET_P0_CONV (rule, rule_buf[rule_pos]);
19752 break;
19753
19754 case RULE_OP_MANGLE_REPLACE_NP1:
19755 SET_NAME (rule, rule_buf[rule_pos]);
19756 SET_P0_CONV (rule, rule_buf[rule_pos]);
19757 break;
19758
19759 case RULE_OP_MANGLE_REPLACE_NM1:
19760 SET_NAME (rule, rule_buf[rule_pos]);
19761 SET_P0_CONV (rule, rule_buf[rule_pos]);
19762 break;
19763
19764 case RULE_OP_MANGLE_DUPEBLOCK_FIRST:
19765 SET_NAME (rule, rule_buf[rule_pos]);
19766 SET_P0_CONV (rule, rule_buf[rule_pos]);
19767 break;
19768
19769 case RULE_OP_MANGLE_DUPEBLOCK_LAST:
19770 SET_NAME (rule, rule_buf[rule_pos]);
19771 SET_P0_CONV (rule, rule_buf[rule_pos]);
19772 break;
19773
19774 case RULE_OP_MANGLE_TITLE:
19775 SET_NAME (rule, rule_buf[rule_pos]);
19776 break;
19777
19778 default:
19779 return (-1);
19780 break;
19781 }
19782 }
19783
19784 if (rule_pos < rule_len) return (-1);
19785
19786 return (0);
19787 }
19788
19789 int kernel_rule_to_cpu_rule (char rule_buf[BUFSIZ], kernel_rule_t *rule)
19790 {
19791 uint rule_cnt;
19792 uint rule_pos;
19793 uint rule_len = BUFSIZ - 1; // maximum possible len
19794
19795 char rule_cmd;
19796
19797 for (rule_cnt = 0, rule_pos = 0; rule_pos < rule_len && rule_cnt < MAX_KERNEL_RULES; rule_pos++, rule_cnt++)
19798 {
19799 GET_NAME (rule);
19800
19801 if (rule_cnt > 0) rule_buf[rule_pos++] = ' ';
19802
19803 switch (rule_cmd)
19804 {
19805 case RULE_OP_MANGLE_NOOP:
19806 rule_buf[rule_pos] = rule_cmd;
19807 break;
19808
19809 case RULE_OP_MANGLE_LREST:
19810 rule_buf[rule_pos] = rule_cmd;
19811 break;
19812
19813 case RULE_OP_MANGLE_UREST:
19814 rule_buf[rule_pos] = rule_cmd;
19815 break;
19816
19817 case RULE_OP_MANGLE_LREST_UFIRST:
19818 rule_buf[rule_pos] = rule_cmd;
19819 break;
19820
19821 case RULE_OP_MANGLE_UREST_LFIRST:
19822 rule_buf[rule_pos] = rule_cmd;
19823 break;
19824
19825 case RULE_OP_MANGLE_TREST:
19826 rule_buf[rule_pos] = rule_cmd;
19827 break;
19828
19829 case RULE_OP_MANGLE_TOGGLE_AT:
19830 rule_buf[rule_pos] = rule_cmd;
19831 GET_P0_CONV (rule);
19832 break;
19833
19834 case RULE_OP_MANGLE_REVERSE:
19835 rule_buf[rule_pos] = rule_cmd;
19836 break;
19837
19838 case RULE_OP_MANGLE_DUPEWORD:
19839 rule_buf[rule_pos] = rule_cmd;
19840 break;
19841
19842 case RULE_OP_MANGLE_DUPEWORD_TIMES:
19843 rule_buf[rule_pos] = rule_cmd;
19844 GET_P0_CONV (rule);
19845 break;
19846
19847 case RULE_OP_MANGLE_REFLECT:
19848 rule_buf[rule_pos] = rule_cmd;
19849 break;
19850
19851 case RULE_OP_MANGLE_ROTATE_LEFT:
19852 rule_buf[rule_pos] = rule_cmd;
19853 break;
19854
19855 case RULE_OP_MANGLE_ROTATE_RIGHT:
19856 rule_buf[rule_pos] = rule_cmd;
19857 break;
19858
19859 case RULE_OP_MANGLE_APPEND:
19860 rule_buf[rule_pos] = rule_cmd;
19861 GET_P0 (rule);
19862 break;
19863
19864 case RULE_OP_MANGLE_PREPEND:
19865 rule_buf[rule_pos] = rule_cmd;
19866 GET_P0 (rule);
19867 break;
19868
19869 case RULE_OP_MANGLE_DELETE_FIRST:
19870 rule_buf[rule_pos] = rule_cmd;
19871 break;
19872
19873 case RULE_OP_MANGLE_DELETE_LAST:
19874 rule_buf[rule_pos] = rule_cmd;
19875 break;
19876
19877 case RULE_OP_MANGLE_DELETE_AT:
19878 rule_buf[rule_pos] = rule_cmd;
19879 GET_P0_CONV (rule);
19880 break;
19881
19882 case RULE_OP_MANGLE_EXTRACT:
19883 rule_buf[rule_pos] = rule_cmd;
19884 GET_P0_CONV (rule);
19885 GET_P1_CONV (rule);
19886 break;
19887
19888 case RULE_OP_MANGLE_OMIT:
19889 rule_buf[rule_pos] = rule_cmd;
19890 GET_P0_CONV (rule);
19891 GET_P1_CONV (rule);
19892 break;
19893
19894 case RULE_OP_MANGLE_INSERT:
19895 rule_buf[rule_pos] = rule_cmd;
19896 GET_P0_CONV (rule);
19897 GET_P1 (rule);
19898 break;
19899
19900 case RULE_OP_MANGLE_OVERSTRIKE:
19901 rule_buf[rule_pos] = rule_cmd;
19902 GET_P0_CONV (rule);
19903 GET_P1 (rule);
19904 break;
19905
19906 case RULE_OP_MANGLE_TRUNCATE_AT:
19907 rule_buf[rule_pos] = rule_cmd;
19908 GET_P0_CONV (rule);
19909 break;
19910
19911 case RULE_OP_MANGLE_REPLACE:
19912 rule_buf[rule_pos] = rule_cmd;
19913 GET_P0 (rule);
19914 GET_P1 (rule);
19915 break;
19916
19917 case RULE_OP_MANGLE_PURGECHAR:
19918 return (-1);
19919 break;
19920
19921 case RULE_OP_MANGLE_TOGGLECASE_REC:
19922 return (-1);
19923 break;
19924
19925 case RULE_OP_MANGLE_DUPECHAR_FIRST:
19926 rule_buf[rule_pos] = rule_cmd;
19927 GET_P0_CONV (rule);
19928 break;
19929
19930 case RULE_OP_MANGLE_DUPECHAR_LAST:
19931 rule_buf[rule_pos] = rule_cmd;
19932 GET_P0_CONV (rule);
19933 break;
19934
19935 case RULE_OP_MANGLE_DUPECHAR_ALL:
19936 rule_buf[rule_pos] = rule_cmd;
19937 break;
19938
19939 case RULE_OP_MANGLE_SWITCH_FIRST:
19940 rule_buf[rule_pos] = rule_cmd;
19941 break;
19942
19943 case RULE_OP_MANGLE_SWITCH_LAST:
19944 rule_buf[rule_pos] = rule_cmd;
19945 break;
19946
19947 case RULE_OP_MANGLE_SWITCH_AT:
19948 rule_buf[rule_pos] = rule_cmd;
19949 GET_P0_CONV (rule);
19950 GET_P1_CONV (rule);
19951 break;
19952
19953 case RULE_OP_MANGLE_CHR_SHIFTL:
19954 rule_buf[rule_pos] = rule_cmd;
19955 GET_P0_CONV (rule);
19956 break;
19957
19958 case RULE_OP_MANGLE_CHR_SHIFTR:
19959 rule_buf[rule_pos] = rule_cmd;
19960 GET_P0_CONV (rule);
19961 break;
19962
19963 case RULE_OP_MANGLE_CHR_INCR:
19964 rule_buf[rule_pos] = rule_cmd;
19965 GET_P0_CONV (rule);
19966 break;
19967
19968 case RULE_OP_MANGLE_CHR_DECR:
19969 rule_buf[rule_pos] = rule_cmd;
19970 GET_P0_CONV (rule);
19971 break;
19972
19973 case RULE_OP_MANGLE_REPLACE_NP1:
19974 rule_buf[rule_pos] = rule_cmd;
19975 GET_P0_CONV (rule);
19976 break;
19977
19978 case RULE_OP_MANGLE_REPLACE_NM1:
19979 rule_buf[rule_pos] = rule_cmd;
19980 GET_P0_CONV (rule);
19981 break;
19982
19983 case RULE_OP_MANGLE_DUPEBLOCK_FIRST:
19984 rule_buf[rule_pos] = rule_cmd;
19985 GET_P0_CONV (rule);
19986 break;
19987
19988 case RULE_OP_MANGLE_DUPEBLOCK_LAST:
19989 rule_buf[rule_pos] = rule_cmd;
19990 GET_P0_CONV (rule);
19991 break;
19992
19993 case RULE_OP_MANGLE_TITLE:
19994 rule_buf[rule_pos] = rule_cmd;
19995 break;
19996
19997 case 0:
19998 return rule_pos - 1;
19999 break;
20000
20001 default:
20002 return (-1);
20003 break;
20004 }
20005 }
20006
20007 if (rule_cnt > 0)
20008 {
20009 return rule_pos;
20010 }
20011
20012 return (-1);
20013 }
20014
20015 /**
20016 * CPU rules : this is from hashcat sources, cpu based rules
20017 */
20018
20019 #define NEXT_RULEPOS(rp) if (++(rp) == rule_len) return (RULE_RC_SYNTAX_ERROR)
20020 #define NEXT_RPTOI(r,rp,up) if (((up) = conv_ctoi ((r)[(rp)])) == -1) return (RULE_RC_SYNTAX_ERROR)
20021
20022 #define MANGLE_TOGGLE_AT(a,p) if (class_alpha ((a)[(p)])) (a)[(p)] ^= 0x20
20023 #define MANGLE_LOWER_AT(a,p) if (class_upper ((a)[(p)])) (a)[(p)] ^= 0x20
20024 #define MANGLE_UPPER_AT(a,p) if (class_lower ((a)[(p)])) (a)[(p)] ^= 0x20
20025
20026 /* #define MANGLE_SWITCH(a,l,r) { char c = (l); arr[(r)] = arr[(l)]; arr[(l)] = c; } */
20027 /* #define MANGLE_SWITCH(a,l,r) { char c = (l); (a)[(r)] = (a)[(l)]; (a)[(l)] = c; } */
20028 #define MANGLE_SWITCH(a,l,r) { char c = (a)[(r)]; (a)[(r)] = (a)[(l)]; (a)[(l)] = c; }
20029
20030 int mangle_lrest (char arr[BLOCK_SIZE], int arr_len)
20031 {
20032 int pos;
20033
20034 for (pos = 0; pos < arr_len; pos++) MANGLE_LOWER_AT (arr, pos);
20035
20036 return (arr_len);
20037 }
20038
20039 int mangle_urest (char arr[BLOCK_SIZE], int arr_len)
20040 {
20041 int pos;
20042
20043 for (pos = 0; pos < arr_len; pos++) MANGLE_UPPER_AT (arr, pos);
20044
20045 return (arr_len);
20046 }
20047
20048 int mangle_trest (char arr[BLOCK_SIZE], int arr_len)
20049 {
20050 int pos;
20051
20052 for (pos = 0; pos < arr_len; pos++) MANGLE_TOGGLE_AT (arr, pos);
20053
20054 return (arr_len);
20055 }
20056
20057 int mangle_reverse (char arr[BLOCK_SIZE], int arr_len)
20058 {
20059 int l;
20060 int r;
20061
20062 for (l = 0; l < arr_len; l++)
20063 {
20064 r = arr_len - 1 - l;
20065
20066 if (l >= r) break;
20067
20068 MANGLE_SWITCH (arr, l, r);
20069 }
20070
20071 return (arr_len);
20072 }
20073
20074 int mangle_double (char arr[BLOCK_SIZE], int arr_len)
20075 {
20076 if ((arr_len * 2) >= BLOCK_SIZE) return (arr_len);
20077
20078 memcpy (&arr[arr_len], arr, (size_t) arr_len);
20079
20080 return (arr_len * 2);
20081 }
20082
20083 int mangle_double_times (char arr[BLOCK_SIZE], int arr_len, int times)
20084 {
20085 if (((arr_len * times) + arr_len) >= BLOCK_SIZE) return (arr_len);
20086
20087 int orig_len = arr_len;
20088
20089 int i;
20090
20091 for (i = 0; i < times; i++)
20092 {
20093 memcpy (&arr[arr_len], arr, orig_len);
20094
20095 arr_len += orig_len;
20096 }
20097
20098 return (arr_len);
20099 }
20100
20101 int mangle_reflect (char arr[BLOCK_SIZE], int arr_len)
20102 {
20103 if ((arr_len * 2) >= BLOCK_SIZE) return (arr_len);
20104
20105 mangle_double (arr, arr_len);
20106
20107 mangle_reverse (arr + arr_len, arr_len);
20108
20109 return (arr_len * 2);
20110 }
20111
20112 int mangle_rotate_left (char arr[BLOCK_SIZE], int arr_len)
20113 {
20114 int l;
20115 int r;
20116
20117 for (l = 0, r = arr_len - 1; r > 0; r--)
20118 {
20119 MANGLE_SWITCH (arr, l, r);
20120 }
20121
20122 return (arr_len);
20123 }
20124
20125 int mangle_rotate_right (char arr[BLOCK_SIZE], int arr_len)
20126 {
20127 int l;
20128 int r;
20129
20130 for (l = 0, r = arr_len - 1; l < r; l++)
20131 {
20132 MANGLE_SWITCH (arr, l, r);
20133 }
20134
20135 return (arr_len);
20136 }
20137
20138 int mangle_append (char arr[BLOCK_SIZE], int arr_len, char c)
20139 {
20140 if ((arr_len + 1) >= BLOCK_SIZE) return (arr_len);
20141
20142 arr[arr_len] = c;
20143
20144 return (arr_len + 1);
20145 }
20146
20147 int mangle_prepend (char arr[BLOCK_SIZE], int arr_len, char c)
20148 {
20149 if ((arr_len + 1) >= BLOCK_SIZE) return (arr_len);
20150
20151 int arr_pos;
20152
20153 for (arr_pos = arr_len - 1; arr_pos > -1; arr_pos--)
20154 {
20155 arr[arr_pos + 1] = arr[arr_pos];
20156 }
20157
20158 arr[0] = c;
20159
20160 return (arr_len + 1);
20161 }
20162
20163 int mangle_delete_at (char arr[BLOCK_SIZE], int arr_len, int upos)
20164 {
20165 if (upos >= arr_len) return (arr_len);
20166
20167 int arr_pos;
20168
20169 for (arr_pos = upos; arr_pos < arr_len - 1; arr_pos++)
20170 {
20171 arr[arr_pos] = arr[arr_pos + 1];
20172 }
20173
20174 return (arr_len - 1);
20175 }
20176
20177 int mangle_extract (char arr[BLOCK_SIZE], int arr_len, int upos, int ulen)
20178 {
20179 if (upos >= arr_len) return (arr_len);
20180
20181 if ((upos + ulen) > arr_len) return (arr_len);
20182
20183 int arr_pos;
20184
20185 for (arr_pos = 0; arr_pos < ulen; arr_pos++)
20186 {
20187 arr[arr_pos] = arr[upos + arr_pos];
20188 }
20189
20190 return (ulen);
20191 }
20192
20193 int mangle_omit (char arr[BLOCK_SIZE], int arr_len, int upos, int ulen)
20194 {
20195 if (upos >= arr_len) return (arr_len);
20196
20197 if ((upos + ulen) >= arr_len) return (arr_len);
20198
20199 int arr_pos;
20200
20201 for (arr_pos = upos; arr_pos < arr_len - ulen; arr_pos++)
20202 {
20203 arr[arr_pos] = arr[arr_pos + ulen];
20204 }
20205
20206 return (arr_len - ulen);
20207 }
20208
20209 int mangle_insert (char arr[BLOCK_SIZE], int arr_len, int upos, char c)
20210 {
20211 if (upos >= arr_len) return (arr_len);
20212
20213 if ((arr_len + 1) >= BLOCK_SIZE) return (arr_len);
20214
20215 int arr_pos;
20216
20217 for (arr_pos = arr_len - 1; arr_pos > upos - 1; arr_pos--)
20218 {
20219 arr[arr_pos + 1] = arr[arr_pos];
20220 }
20221
20222 arr[upos] = c;
20223
20224 return (arr_len + 1);
20225 }
20226
20227 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)
20228 {
20229 if ((arr_len + arr2_cpy) > BLOCK_SIZE) return (RULE_RC_REJECT_ERROR);
20230
20231 if (arr_pos > arr_len) return (RULE_RC_REJECT_ERROR);
20232
20233 if (arr2_pos > arr2_len) return (RULE_RC_REJECT_ERROR);
20234
20235 if ((arr2_pos + arr2_cpy) > arr2_len) return (RULE_RC_REJECT_ERROR);
20236
20237 if (arr2_cpy < 1) return (RULE_RC_SYNTAX_ERROR);
20238
20239 memcpy (arr2, arr2 + arr2_pos, arr2_len - arr2_pos);
20240
20241 memcpy (arr2 + arr2_cpy, arr + arr_pos, arr_len - arr_pos);
20242
20243 memcpy (arr + arr_pos, arr2, arr_len - arr_pos + arr2_cpy);
20244
20245 return (arr_len + arr2_cpy);
20246 }
20247
20248 int mangle_overstrike (char arr[BLOCK_SIZE], int arr_len, int upos, char c)
20249 {
20250 if (upos >= arr_len) return (arr_len);
20251
20252 arr[upos] = c;
20253
20254 return (arr_len);
20255 }
20256
20257 int mangle_truncate_at (char arr[BLOCK_SIZE], int arr_len, int upos)
20258 {
20259 if (upos >= arr_len) return (arr_len);
20260
20261 memset (arr + upos, 0, arr_len - upos);
20262
20263 return (upos);
20264 }
20265
20266 int mangle_replace (char arr[BLOCK_SIZE], int arr_len, char oldc, char newc)
20267 {
20268 int arr_pos;
20269
20270 for (arr_pos = 0; arr_pos < arr_len; arr_pos++)
20271 {
20272 if (arr[arr_pos] != oldc) continue;
20273
20274 arr[arr_pos] = newc;
20275 }
20276
20277 return (arr_len);
20278 }
20279
20280 int mangle_purgechar (char arr[BLOCK_SIZE], int arr_len, char c)
20281 {
20282 int arr_pos;
20283
20284 int ret_len;
20285
20286 for (ret_len = 0, arr_pos = 0; arr_pos < arr_len; arr_pos++)
20287 {
20288 if (arr[arr_pos] == c) continue;
20289
20290 arr[ret_len] = arr[arr_pos];
20291
20292 ret_len++;
20293 }
20294
20295 return (ret_len);
20296 }
20297
20298 int mangle_dupeblock_prepend (char arr[BLOCK_SIZE], int arr_len, int ulen)
20299 {
20300 if (ulen > arr_len) return (arr_len);
20301
20302 if ((arr_len + ulen) >= BLOCK_SIZE) return (arr_len);
20303
20304 char cs[100] = { 0 };
20305
20306 memcpy (cs, arr, ulen);
20307
20308 int i;
20309
20310 for (i = 0; i < ulen; i++)
20311 {
20312 char c = cs[i];
20313
20314 arr_len = mangle_insert (arr, arr_len, i, c);
20315 }
20316
20317 return (arr_len);
20318 }
20319
20320 int mangle_dupeblock_append (char arr[BLOCK_SIZE], int arr_len, int ulen)
20321 {
20322 if (ulen > arr_len) return (arr_len);
20323
20324 if ((arr_len + ulen) >= BLOCK_SIZE) return (arr_len);
20325
20326 int upos = arr_len - ulen;
20327
20328 int i;
20329
20330 for (i = 0; i < ulen; i++)
20331 {
20332 char c = arr[upos + i];
20333
20334 arr_len = mangle_append (arr, arr_len, c);
20335 }
20336
20337 return (arr_len);
20338 }
20339
20340 int mangle_dupechar_at (char arr[BLOCK_SIZE], int arr_len, int upos, int ulen)
20341 {
20342 if ( arr_len == 0) return (arr_len);
20343 if ((arr_len + ulen) >= BLOCK_SIZE) return (arr_len);
20344
20345 char c = arr[upos];
20346
20347 int i;
20348
20349 for (i = 0; i < ulen; i++)
20350 {
20351 arr_len = mangle_insert (arr, arr_len, upos, c);
20352 }
20353
20354 return (arr_len);
20355 }
20356
20357 int mangle_dupechar (char arr[BLOCK_SIZE], int arr_len)
20358 {
20359 if ( arr_len == 0) return (arr_len);
20360 if ((arr_len + arr_len) >= BLOCK_SIZE) return (arr_len);
20361
20362 int arr_pos;
20363
20364 for (arr_pos = arr_len - 1; arr_pos > -1; arr_pos--)
20365 {
20366 int new_pos = arr_pos * 2;
20367
20368 arr[new_pos] = arr[arr_pos];
20369
20370 arr[new_pos + 1] = arr[arr_pos];
20371 }
20372
20373 return (arr_len * 2);
20374 }
20375
20376 int mangle_switch_at_check (char arr[BLOCK_SIZE], int arr_len, int upos, int upos2)
20377 {
20378 if (upos >= arr_len) return (arr_len);
20379 if (upos2 >= arr_len) return (arr_len);
20380
20381 MANGLE_SWITCH (arr, upos, upos2);
20382
20383 return (arr_len);
20384 }
20385
20386 int mangle_switch_at (char arr[BLOCK_SIZE], int arr_len, int upos, int upos2)
20387 {
20388 MANGLE_SWITCH (arr, upos, upos2);
20389
20390 return (arr_len);
20391 }
20392
20393 int mangle_chr_shiftl (char arr[BLOCK_SIZE], int arr_len, int upos)
20394 {
20395 if (upos >= arr_len) return (arr_len);
20396
20397 arr[upos] <<= 1;
20398
20399 return (arr_len);
20400 }
20401
20402 int mangle_chr_shiftr (char arr[BLOCK_SIZE], int arr_len, int upos)
20403 {
20404 if (upos >= arr_len) return (arr_len);
20405
20406 arr[upos] >>= 1;
20407
20408 return (arr_len);
20409 }
20410
20411 int mangle_chr_incr (char arr[BLOCK_SIZE], int arr_len, int upos)
20412 {
20413 if (upos >= arr_len) return (arr_len);
20414
20415 arr[upos] += 1;
20416
20417 return (arr_len);
20418 }
20419
20420 int mangle_chr_decr (char arr[BLOCK_SIZE], int arr_len, int upos)
20421 {
20422 if (upos >= arr_len) return (arr_len);
20423
20424 arr[upos] -= 1;
20425
20426 return (arr_len);
20427 }
20428
20429 int mangle_title (char arr[BLOCK_SIZE], int arr_len)
20430 {
20431 int upper_next = 1;
20432
20433 int pos;
20434
20435 for (pos = 0; pos < arr_len; pos++)
20436 {
20437 if (arr[pos] == ' ')
20438 {
20439 upper_next = 1;
20440
20441 continue;
20442 }
20443
20444 if (upper_next)
20445 {
20446 upper_next = 0;
20447
20448 MANGLE_UPPER_AT (arr, pos);
20449 }
20450 else
20451 {
20452 MANGLE_LOWER_AT (arr, pos);
20453 }
20454 }
20455
20456 return (arr_len);
20457 }
20458
20459 int generate_random_rule (char rule_buf[RP_RULE_BUFSIZ], u32 rp_gen_func_min, u32 rp_gen_func_max)
20460 {
20461 u32 rp_gen_num = get_random_num (rp_gen_func_min, rp_gen_func_max);
20462
20463 u32 j;
20464
20465 u32 rule_pos = 0;
20466
20467 for (j = 0; j < rp_gen_num; j++)
20468 {
20469 u32 r = 0;
20470 u32 p1 = 0;
20471 u32 p2 = 0;
20472 u32 p3 = 0;
20473
20474 switch ((char) get_random_num (0, 9))
20475 {
20476 case 0:
20477 r = get_random_num (0, sizeof (grp_op_nop));
20478 rule_buf[rule_pos++] = grp_op_nop[r];
20479 break;
20480
20481 case 1:
20482 r = get_random_num (0, sizeof (grp_op_pos_p0));
20483 rule_buf[rule_pos++] = grp_op_pos_p0[r];
20484 p1 = get_random_num (0, sizeof (grp_pos));
20485 rule_buf[rule_pos++] = grp_pos[p1];
20486 break;
20487
20488 case 2:
20489 r = get_random_num (0, sizeof (grp_op_pos_p1));
20490 rule_buf[rule_pos++] = grp_op_pos_p1[r];
20491 p1 = get_random_num (1, 6);
20492 rule_buf[rule_pos++] = grp_pos[p1];
20493 break;
20494
20495 case 3:
20496 r = get_random_num (0, sizeof (grp_op_chr));
20497 rule_buf[rule_pos++] = grp_op_chr[r];
20498 p1 = get_random_num (0x20, 0x7e);
20499 rule_buf[rule_pos++] = (char) p1;
20500 break;
20501
20502 case 4:
20503 r = get_random_num (0, sizeof (grp_op_chr_chr));
20504 rule_buf[rule_pos++] = grp_op_chr_chr[r];
20505 p1 = get_random_num (0x20, 0x7e);
20506 rule_buf[rule_pos++] = (char) p1;
20507 p2 = get_random_num (0x20, 0x7e);
20508 while (p1 == p2)
20509 p2 = get_random_num (0x20, 0x7e);
20510 rule_buf[rule_pos++] = (char) p2;
20511 break;
20512
20513 case 5:
20514 r = get_random_num (0, sizeof (grp_op_pos_chr));
20515 rule_buf[rule_pos++] = grp_op_pos_chr[r];
20516 p1 = get_random_num (0, sizeof (grp_pos));
20517 rule_buf[rule_pos++] = grp_pos[p1];
20518 p2 = get_random_num (0x20, 0x7e);
20519 rule_buf[rule_pos++] = (char) p2;
20520 break;
20521
20522 case 6:
20523 r = get_random_num (0, sizeof (grp_op_pos_pos0));
20524 rule_buf[rule_pos++] = grp_op_pos_pos0[r];
20525 p1 = get_random_num (0, sizeof (grp_pos));
20526 rule_buf[rule_pos++] = grp_pos[p1];
20527 p2 = get_random_num (0, sizeof (grp_pos));
20528 while (p1 == p2)
20529 p2 = get_random_num (0, sizeof (grp_pos));
20530 rule_buf[rule_pos++] = grp_pos[p2];
20531 break;
20532
20533 case 7:
20534 r = get_random_num (0, sizeof (grp_op_pos_pos1));
20535 rule_buf[rule_pos++] = grp_op_pos_pos1[r];
20536 p1 = get_random_num (0, sizeof (grp_pos));
20537 rule_buf[rule_pos++] = grp_pos[p1];
20538 p2 = get_random_num (1, sizeof (grp_pos));
20539 while (p1 == p2)
20540 p2 = get_random_num (1, sizeof (grp_pos));
20541 rule_buf[rule_pos++] = grp_pos[p2];
20542 break;
20543
20544 case 8:
20545 r = get_random_num (0, sizeof (grp_op_pos1_pos2_pos3));
20546 rule_buf[rule_pos++] = grp_op_pos1_pos2_pos3[r];
20547 p1 = get_random_num (0, sizeof (grp_pos));
20548 rule_buf[rule_pos++] = grp_pos[p1];
20549 p2 = get_random_num (1, sizeof (grp_pos));
20550 rule_buf[rule_pos++] = grp_pos[p1];
20551 p3 = get_random_num (0, sizeof (grp_pos));
20552 rule_buf[rule_pos++] = grp_pos[p3];
20553 break;
20554 }
20555 }
20556
20557 return (rule_pos);
20558 }
20559
20560 int _old_apply_rule (char *rule, int rule_len, char in[BLOCK_SIZE], int in_len, char out[BLOCK_SIZE])
20561 {
20562 char mem[BLOCK_SIZE] = { 0 };
20563
20564 if (in == NULL) return (RULE_RC_REJECT_ERROR);
20565
20566 if (out == NULL) return (RULE_RC_REJECT_ERROR);
20567
20568 if (in_len < 1 || in_len > BLOCK_SIZE) return (RULE_RC_REJECT_ERROR);
20569
20570 if (rule_len < 1) return (RULE_RC_REJECT_ERROR);
20571
20572 int out_len = in_len;
20573 int mem_len = in_len;
20574
20575 memcpy (out, in, out_len);
20576
20577 int rule_pos;
20578
20579 for (rule_pos = 0; rule_pos < rule_len; rule_pos++)
20580 {
20581 int upos, upos2;
20582 int ulen;
20583
20584 switch (rule[rule_pos])
20585 {
20586 case ' ':
20587 break;
20588
20589 case RULE_OP_MANGLE_NOOP:
20590 break;
20591
20592 case RULE_OP_MANGLE_LREST:
20593 out_len = mangle_lrest (out, out_len);
20594 break;
20595
20596 case RULE_OP_MANGLE_UREST:
20597 out_len = mangle_urest (out, out_len);
20598 break;
20599
20600 case RULE_OP_MANGLE_LREST_UFIRST:
20601 out_len = mangle_lrest (out, out_len);
20602 if (out_len) MANGLE_UPPER_AT (out, 0);
20603 break;
20604
20605 case RULE_OP_MANGLE_UREST_LFIRST:
20606 out_len = mangle_urest (out, out_len);
20607 if (out_len) MANGLE_LOWER_AT (out, 0);
20608 break;
20609
20610 case RULE_OP_MANGLE_TREST:
20611 out_len = mangle_trest (out, out_len);
20612 break;
20613
20614 case RULE_OP_MANGLE_TOGGLE_AT:
20615 NEXT_RULEPOS (rule_pos);
20616 NEXT_RPTOI (rule, rule_pos, upos);
20617 if (upos < out_len) MANGLE_TOGGLE_AT (out, upos);
20618 break;
20619
20620 case RULE_OP_MANGLE_REVERSE:
20621 out_len = mangle_reverse (out, out_len);
20622 break;
20623
20624 case RULE_OP_MANGLE_DUPEWORD:
20625 out_len = mangle_double (out, out_len);
20626 break;
20627
20628 case RULE_OP_MANGLE_DUPEWORD_TIMES:
20629 NEXT_RULEPOS (rule_pos);
20630 NEXT_RPTOI (rule, rule_pos, ulen);
20631 out_len = mangle_double_times (out, out_len, ulen);
20632 break;
20633
20634 case RULE_OP_MANGLE_REFLECT:
20635 out_len = mangle_reflect (out, out_len);
20636 break;
20637
20638 case RULE_OP_MANGLE_ROTATE_LEFT:
20639 mangle_rotate_left (out, out_len);
20640 break;
20641
20642 case RULE_OP_MANGLE_ROTATE_RIGHT:
20643 mangle_rotate_right (out, out_len);
20644 break;
20645
20646 case RULE_OP_MANGLE_APPEND:
20647 NEXT_RULEPOS (rule_pos);
20648 out_len = mangle_append (out, out_len, rule[rule_pos]);
20649 break;
20650
20651 case RULE_OP_MANGLE_PREPEND:
20652 NEXT_RULEPOS (rule_pos);
20653 out_len = mangle_prepend (out, out_len, rule[rule_pos]);
20654 break;
20655
20656 case RULE_OP_MANGLE_DELETE_FIRST:
20657 out_len = mangle_delete_at (out, out_len, 0);
20658 break;
20659
20660 case RULE_OP_MANGLE_DELETE_LAST:
20661 out_len = mangle_delete_at (out, out_len, (out_len) ? out_len - 1 : 0);
20662 break;
20663
20664 case RULE_OP_MANGLE_DELETE_AT:
20665 NEXT_RULEPOS (rule_pos);
20666 NEXT_RPTOI (rule, rule_pos, upos);
20667 out_len = mangle_delete_at (out, out_len, upos);
20668 break;
20669
20670 case RULE_OP_MANGLE_EXTRACT:
20671 NEXT_RULEPOS (rule_pos);
20672 NEXT_RPTOI (rule, rule_pos, upos);
20673 NEXT_RULEPOS (rule_pos);
20674 NEXT_RPTOI (rule, rule_pos, ulen);
20675 out_len = mangle_extract (out, out_len, upos, ulen);
20676 break;
20677
20678 case RULE_OP_MANGLE_OMIT:
20679 NEXT_RULEPOS (rule_pos);
20680 NEXT_RPTOI (rule, rule_pos, upos);
20681 NEXT_RULEPOS (rule_pos);
20682 NEXT_RPTOI (rule, rule_pos, ulen);
20683 out_len = mangle_omit (out, out_len, upos, ulen);
20684 break;
20685
20686 case RULE_OP_MANGLE_INSERT:
20687 NEXT_RULEPOS (rule_pos);
20688 NEXT_RPTOI (rule, rule_pos, upos);
20689 NEXT_RULEPOS (rule_pos);
20690 out_len = mangle_insert (out, out_len, upos, rule[rule_pos]);
20691 break;
20692
20693 case RULE_OP_MANGLE_OVERSTRIKE:
20694 NEXT_RULEPOS (rule_pos);
20695 NEXT_RPTOI (rule, rule_pos, upos);
20696 NEXT_RULEPOS (rule_pos);
20697 out_len = mangle_overstrike (out, out_len, upos, rule[rule_pos]);
20698 break;
20699
20700 case RULE_OP_MANGLE_TRUNCATE_AT:
20701 NEXT_RULEPOS (rule_pos);
20702 NEXT_RPTOI (rule, rule_pos, upos);
20703 out_len = mangle_truncate_at (out, out_len, upos);
20704 break;
20705
20706 case RULE_OP_MANGLE_REPLACE:
20707 NEXT_RULEPOS (rule_pos);
20708 NEXT_RULEPOS (rule_pos);
20709 out_len = mangle_replace (out, out_len, rule[rule_pos - 1], rule[rule_pos]);
20710 break;
20711
20712 case RULE_OP_MANGLE_PURGECHAR:
20713 NEXT_RULEPOS (rule_pos);
20714 out_len = mangle_purgechar (out, out_len, rule[rule_pos]);
20715 break;
20716
20717 case RULE_OP_MANGLE_TOGGLECASE_REC:
20718 /* todo */
20719 break;
20720
20721 case RULE_OP_MANGLE_DUPECHAR_FIRST:
20722 NEXT_RULEPOS (rule_pos);
20723 NEXT_RPTOI (rule, rule_pos, ulen);
20724 out_len = mangle_dupechar_at (out, out_len, 0, ulen);
20725 break;
20726
20727 case RULE_OP_MANGLE_DUPECHAR_LAST:
20728 NEXT_RULEPOS (rule_pos);
20729 NEXT_RPTOI (rule, rule_pos, ulen);
20730 out_len = mangle_dupechar_at (out, out_len, out_len - 1, ulen);
20731 break;
20732
20733 case RULE_OP_MANGLE_DUPECHAR_ALL:
20734 out_len = mangle_dupechar (out, out_len);
20735 break;
20736
20737 case RULE_OP_MANGLE_DUPEBLOCK_FIRST:
20738 NEXT_RULEPOS (rule_pos);
20739 NEXT_RPTOI (rule, rule_pos, ulen);
20740 out_len = mangle_dupeblock_prepend (out, out_len, ulen);
20741 break;
20742
20743 case RULE_OP_MANGLE_DUPEBLOCK_LAST:
20744 NEXT_RULEPOS (rule_pos);
20745 NEXT_RPTOI (rule, rule_pos, ulen);
20746 out_len = mangle_dupeblock_append (out, out_len, ulen);
20747 break;
20748
20749 case RULE_OP_MANGLE_SWITCH_FIRST:
20750 if (out_len >= 2) mangle_switch_at (out, out_len, 0, 1);
20751 break;
20752
20753 case RULE_OP_MANGLE_SWITCH_LAST:
20754 if (out_len >= 2) mangle_switch_at (out, out_len, out_len - 1, out_len - 2);
20755 break;
20756
20757 case RULE_OP_MANGLE_SWITCH_AT:
20758 NEXT_RULEPOS (rule_pos);
20759 NEXT_RPTOI (rule, rule_pos, upos);
20760 NEXT_RULEPOS (rule_pos);
20761 NEXT_RPTOI (rule, rule_pos, upos2);
20762 out_len = mangle_switch_at_check (out, out_len, upos, upos2);
20763 break;
20764
20765 case RULE_OP_MANGLE_CHR_SHIFTL:
20766 NEXT_RULEPOS (rule_pos);
20767 NEXT_RPTOI (rule, rule_pos, upos);
20768 mangle_chr_shiftl (out, out_len, upos);
20769 break;
20770
20771 case RULE_OP_MANGLE_CHR_SHIFTR:
20772 NEXT_RULEPOS (rule_pos);
20773 NEXT_RPTOI (rule, rule_pos, upos);
20774 mangle_chr_shiftr (out, out_len, upos);
20775 break;
20776
20777 case RULE_OP_MANGLE_CHR_INCR:
20778 NEXT_RULEPOS (rule_pos);
20779 NEXT_RPTOI (rule, rule_pos, upos);
20780 mangle_chr_incr (out, out_len, upos);
20781 break;
20782
20783 case RULE_OP_MANGLE_CHR_DECR:
20784 NEXT_RULEPOS (rule_pos);
20785 NEXT_RPTOI (rule, rule_pos, upos);
20786 mangle_chr_decr (out, out_len, upos);
20787 break;
20788
20789 case RULE_OP_MANGLE_REPLACE_NP1:
20790 NEXT_RULEPOS (rule_pos);
20791 NEXT_RPTOI (rule, rule_pos, upos);
20792 if ((upos >= 0) && ((upos + 1) < out_len)) mangle_overstrike (out, out_len, upos, out[upos + 1]);
20793 break;
20794
20795 case RULE_OP_MANGLE_REPLACE_NM1:
20796 NEXT_RULEPOS (rule_pos);
20797 NEXT_RPTOI (rule, rule_pos, upos);
20798 if ((upos >= 1) && ((upos + 0) < out_len)) mangle_overstrike (out, out_len, upos, out[upos - 1]);
20799 break;
20800
20801 case RULE_OP_MANGLE_TITLE:
20802 out_len = mangle_title (out, out_len);
20803 break;
20804
20805 case RULE_OP_MANGLE_EXTRACT_MEMORY:
20806 if (mem_len < 1) return (RULE_RC_REJECT_ERROR);
20807 NEXT_RULEPOS (rule_pos);
20808 NEXT_RPTOI (rule, rule_pos, upos);
20809 NEXT_RULEPOS (rule_pos);
20810 NEXT_RPTOI (rule, rule_pos, ulen);
20811 NEXT_RULEPOS (rule_pos);
20812 NEXT_RPTOI (rule, rule_pos, upos2);
20813 if ((out_len = mangle_insert_multi (out, out_len, upos2, mem, mem_len, upos, ulen)) < 1) return (out_len);
20814 break;
20815
20816 case RULE_OP_MANGLE_APPEND_MEMORY:
20817 if (mem_len < 1) return (RULE_RC_REJECT_ERROR);
20818 if ((out_len + mem_len) > BLOCK_SIZE) return (RULE_RC_REJECT_ERROR);
20819 memcpy (out + out_len, mem, mem_len);
20820 out_len += mem_len;
20821 break;
20822
20823 case RULE_OP_MANGLE_PREPEND_MEMORY:
20824 if (mem_len < 1) return (RULE_RC_REJECT_ERROR);
20825 if ((mem_len + out_len) > BLOCK_SIZE) return (RULE_RC_REJECT_ERROR);
20826 memcpy (mem + mem_len, out, out_len);
20827 out_len += mem_len;
20828 memcpy (out, mem, out_len);
20829 break;
20830
20831 case RULE_OP_MEMORIZE_WORD:
20832 memcpy (mem, out, out_len);
20833 mem_len = out_len;
20834 break;
20835
20836 case RULE_OP_REJECT_LESS:
20837 NEXT_RULEPOS (rule_pos);
20838 NEXT_RPTOI (rule, rule_pos, upos);
20839 if (out_len > upos) return (RULE_RC_REJECT_ERROR);
20840 break;
20841
20842 case RULE_OP_REJECT_GREATER:
20843 NEXT_RULEPOS (rule_pos);
20844 NEXT_RPTOI (rule, rule_pos, upos);
20845 if (out_len < upos) return (RULE_RC_REJECT_ERROR);
20846 break;
20847
20848 case RULE_OP_REJECT_CONTAIN:
20849 NEXT_RULEPOS (rule_pos);
20850 if (strchr (out, rule[rule_pos]) != NULL) return (RULE_RC_REJECT_ERROR);
20851 break;
20852
20853 case RULE_OP_REJECT_NOT_CONTAIN:
20854 NEXT_RULEPOS (rule_pos);
20855 if (strchr (out, rule[rule_pos]) == NULL) return (RULE_RC_REJECT_ERROR);
20856 break;
20857
20858 case RULE_OP_REJECT_EQUAL_FIRST:
20859 NEXT_RULEPOS (rule_pos);
20860 if (out[0] != rule[rule_pos]) return (RULE_RC_REJECT_ERROR);
20861 break;
20862
20863 case RULE_OP_REJECT_EQUAL_LAST:
20864 NEXT_RULEPOS (rule_pos);
20865 if (out[out_len - 1] != rule[rule_pos]) return (RULE_RC_REJECT_ERROR);
20866 break;
20867
20868 case RULE_OP_REJECT_EQUAL_AT:
20869 NEXT_RULEPOS (rule_pos);
20870 NEXT_RPTOI (rule, rule_pos, upos);
20871 if ((upos + 1) > out_len) return (RULE_RC_REJECT_ERROR);
20872 NEXT_RULEPOS (rule_pos);
20873 if (out[upos] != rule[rule_pos]) return (RULE_RC_REJECT_ERROR);
20874 break;
20875
20876 case RULE_OP_REJECT_CONTAINS:
20877 NEXT_RULEPOS (rule_pos);
20878 NEXT_RPTOI (rule, rule_pos, upos);
20879 if ((upos + 1) > out_len) return (RULE_RC_REJECT_ERROR);
20880 NEXT_RULEPOS (rule_pos);
20881 int c; int cnt; for (c = 0, cnt = 0; c < out_len; c++) if (out[c] == rule[rule_pos]) cnt++;
20882 if (cnt < upos) return (RULE_RC_REJECT_ERROR);
20883 break;
20884
20885 case RULE_OP_REJECT_MEMORY:
20886 if ((out_len == mem_len) && (memcmp (out, mem, out_len) == 0)) return (RULE_RC_REJECT_ERROR);
20887 break;
20888
20889 default:
20890 return (RULE_RC_SYNTAX_ERROR);
20891 break;
20892 }
20893 }
20894
20895 memset (out + out_len, 0, BLOCK_SIZE - out_len);
20896
20897 return (out_len);
20898 }