1 /*
2 * This software is Copyright (c) 2016-2019 Denis Burykin
3 * [denis_burykin yahoo com], [denis-burykin2014 yandex ru]
4 * and it is hereby released to the general public under the following terms:
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted.
7 *
8 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <sys/time.h>
12 #include <ctype.h>
13
14 #include "../loader.h"
15 #include "../formats.h"
16 #include "../memory.h"
17 #include "../misc.h"
18 #include "../options.h"
19 #include "../params.h"
20
21 #include "jtr_device.h"
22 #include "task.h"
23 #include "jtr_mask.h"
24 #include "device_bitstream.h"
25 #include "device_format.h"
26 #include "ztex_sn.h"
27
28
29 // If some task is not completed in this many seconds,
30 // then it counts the device as not functioning one.
31 #define DEVICE_TASK_TIMEOUT 5
32
33 // Control the behavior in case where no devices are ready
34 // because of errors. Startup behavior is not affected.
35 // Set to 1 if you want to wait until any device is up.
36 // Consider ztex_scan.h:ZTEX_SCAN_INTERVAL_DEFAULT
37 const int WAIT_UNTIL_DEVICE_UP = 1;
38
39 /*
40 * keys_buffer. In mask mode, range_info_buffer also used.
41 */
42 char *keys_buffer;
43 unsigned char *range_info_buffer;
44
45 /*
46 * Used by get_key() to transfer the output
47 */
48 char *output_key;
49
50 /*
51 * Task.
52 * - includes all the data required to perform computation
53 * - is assigned to some device (part of the device) independent from
54 * other devices from the point of view from JtR's core (jtr_device)
55 * - processed task (status == TASK_COMPLETE) can include the result.
56 *
57 * Currently there's a global task_list until some further improvement
58 * such as tasks are split into batches or alike.
59 */
60 struct task_list *task_list;
61
62 /*
63 * State of device initialization and information output.
64 */
65 int jtr_device_list_printed = 0;
66
67 /*
68 * Saved by device_format_init()
69 */
70 struct fmt_params *jtr_fmt_params;
71 struct device_bitstream *jtr_bitstream;
72 struct list_main *jtr_devices_allow;
73 int jtr_verbosity;
74
device_format_init(struct fmt_main * fmt_main,struct device_bitstream * bitstream,struct list_main * devices_allow,int verbosity)75 void device_format_init(struct fmt_main *fmt_main,
76 struct device_bitstream *bitstream, struct list_main *devices_allow,
77 int verbosity)
78 {
79 jtr_fmt_params = &fmt_main->params;
80 jtr_bitstream = bitstream;
81 jtr_devices_allow = devices_allow;
82 jtr_verbosity = verbosity;
83
84
85 static int init_conf_devices = 0;
86 if (!init_conf_devices) {
87 // Initialize [List.ZTEX:Devices] configuration section.
88 ztex_sn_init_conf_devices();
89
90 // Check -dev command-line option.
91 struct list_entry *entry;
92 int found_error = 0;
93 for (entry = devices_allow->head; entry; entry = entry->next) {
94 if (ztex_sn_alias_is_valid(entry->data)) {
95 if (!ztex_sn_check_alias(entry->data))
96 found_error = 1;
97 }
98 else if (!ztex_sn_is_valid(entry->data)) {
99 fprintf(stderr, "Error: bad Serial Number '%s'\n", entry->data);
100 found_error = 1;
101 }
102 }
103 if (found_error)
104 error();
105
106 init_conf_devices = 1;
107 }
108
109
110 // Mask issues. 1 mask per JtR run can be specified. Global variables.
111
112 // Mask initialization (mask_int_cand_target).
113 // - Without this, mask is completely unrolled on CPU.
114 // - With this value set, some of ranges get unrolled, so remaining ranges
115 // result in approximate this number (can be greater or less than)
116 // of candidates per key.
117 // - If the number is large enough, ranges don't get unrolled on CPU.
118 // - If mask has more than MASK_FMT_INT_PLHDR ranges, extra ranges
119 // are unrolled regardless of mask_int_cand_target.
120
121 // Mask can create too many candidates. That would result in problems
122 // with slow response or timeout.
123 // crypt_all() must finish in some reasonable 'response time'
124 // such as 0.5-1.0s.
125
126 // Reduce mask (request to unroll some ranges on CPU if necessary)
127 // by setting mask_int_cand_target.
128 // Unroll all ranges on CPU if format is slow enough.
129 //
130 int template_keys = jtr_bitstream->min_template_keys;
131 if (template_keys < 1)
132 template_keys = 1;
133
134 if (!jtr_bitstream->min_keys || jtr_bitstream->candidates_per_crypt
135 > (50 * jtr_bitstream->min_keys / template_keys) ) {
136
137 mask_int_cand_target = jtr_bitstream->candidates_per_crypt
138 / template_keys;
139 }
140 // It requires actual mask size (number of candidates in mask)
141 // to calculate max_keys_per_crypt.
142 // On init(), mask is not ready yet.
143 }
144
145
device_format_done()146 void device_format_done()
147 {
148 MEM_FREE(keys_buffer);
149 MEM_FREE(range_info_buffer);
150 }
151
152
device_format_reset()153 void device_format_reset()
154 {
155 // Initialize hardware and libusb
156 // Uses globals: jtr_device_list, device_list, jtr_bitstream.
157 if (!jtr_device_list_init())
158 error();
159
160 if (!jtr_device_list_printed) {
161 if (jtr_verbosity >= VERB_DEFAULT)
162 jtr_device_list_print();
163 jtr_device_list_print_count();
164 jtr_device_list_printed = 1;
165 }
166
167 // Mask data is ready, calculate and set keys_per_crypt
168 uint64_t keys_per_crypt;
169
170 int min_template_keys = jtr_bitstream->min_template_keys;
171 if (min_template_keys < 1)
172 min_template_keys = 1;
173
174 if (self_test_running) {
175 // Self-test runs too long, using different keys_per_crypt
176 keys_per_crypt = jtr_bitstream->test_keys_per_crypt;
177 } else {
178 keys_per_crypt = jtr_bitstream->candidates_per_crypt
179 / mask_num_cand() / min_template_keys;
180 if (!keys_per_crypt)
181 keys_per_crypt = 1;
182 keys_per_crypt *= min_template_keys;
183 }
184
185 keys_per_crypt *= jtr_device_list_count();
186 // Ensure updated *pcount < (1ULL << 31)
187 while (keys_per_crypt * mask_num_cand() >= 1ULL << 31)
188 keys_per_crypt /= 2;
189
190 if (keys_per_crypt > jtr_bitstream->abs_max_keys_per_crypt) {
191 keys_per_crypt = jtr_bitstream->abs_max_keys_per_crypt;
192
193 // Don't print on self-test, print on benchmark.
194 if (!self_test_running)
195 fprintf(stderr, "Warning: Slow communication channel "\
196 "to the device. "\
197 "Increase mask or expect performance degradation.\n");
198 }
199
200 jtr_fmt_params->max_keys_per_crypt = keys_per_crypt;
201 jtr_fmt_params->min_keys_per_crypt = keys_per_crypt;
202
203 if (jtr_verbosity >= VERB_LEGACY) {
204 fprintf(stderr, "RESET: mask:%d keys:%d", mask_num_cand(),
205 jtr_fmt_params->max_keys_per_crypt);
206 if (mask_num_cand() > 1)
207 fprintf(stderr, " (%d)", mask_num_cand()
208 * jtr_fmt_params->max_keys_per_crypt);
209 fprintf(stderr, " devs:%d\n", jtr_device_list_count());
210 }
211
212 // (re-)allocate keys_buffer, output_key
213 int plaintext_len = jtr_fmt_params->plaintext_length;
214
215 MEM_FREE(keys_buffer);
216 keys_buffer = mem_alloc(plaintext_len
217 * jtr_fmt_params->max_keys_per_crypt);
218
219 MEM_FREE(output_key);
220 output_key = mem_alloc(plaintext_len + 1);
221 output_key[plaintext_len] = 0;
222
223 MEM_FREE(range_info_buffer);
224 if (!mask_is_inactive())
225 range_info_buffer = mem_alloc(MASK_FMT_INT_PLHDR
226 * jtr_fmt_params->max_keys_per_crypt);
227
228
229 task_list_delete(task_list);
230 task_list = NULL;
231 }
232
233
device_format_set_salt(void * salt)234 void device_format_set_salt(void *salt)
235 {
236 }
237
238
device_format_set_key(char * key,int index)239 void device_format_set_key(char *key, int index)
240 {
241 // Copy key into buffer.
242 memcpy(keys_buffer + index * jtr_fmt_params->plaintext_length,
243 key, jtr_fmt_params->plaintext_length);
244
245 //mask_print();
246 //fprintf(stderr, "set_key:%s\n", key);
247
248 // Copy mask data for template key into range_info_buffer.
249 if (!mask_is_inactive())
250 mask_set_range_info(range_info_buffer + index * MASK_FMT_INT_PLHDR);
251 }
252
253
device_format_crypt_all(int * pcount,struct db_salt * salt)254 int device_format_crypt_all(int *pcount, struct db_salt *salt)
255 {
256 if (jtr_verbosity > VERB_LEGACY)
257 fprintf(stderr,"crypt_all:%d ",*pcount);
258
259 // * create tasks from keys_buffer, 1 task for each jtr_device
260 // * equally distribute load among tasks assuming all devices are equal
261 // * assign tasks to jtr_devices
262 // * global jtr_device_list used
263 //
264 task_list_delete(task_list);
265 for (;;) {
266 task_list = task_list_create(*pcount, keys_buffer,
267 mask_is_inactive() ? NULL : range_info_buffer);
268 if (task_list)
269 break;
270
271 // No devices.
272 if (!jtr_device_list_check())
273 usleep(50000);
274 }
275
276 // Send data to devices, continue communication until result is received
277 int rw_result;
278 for (;;) {
279
280 // Check if a new device is up(connected)
281 jtr_device_list_check();
282
283 // If some devices were stopped then some tasks are unassigned.
284 tasks_assign(task_list, jtr_device_list);
285
286 // Perform r/w operations. Stop erroneous devices.
287 rw_result = jtr_device_list_rw(task_list);
288
289 // No operational devices remain.
290 if (!WAIT_UNTIL_DEVICE_UP && rw_result < 0)
291 break;
292
293 // Some tasks could be unable to complete for too long.
294 struct timeval tv;
295 gettimeofday(&tv, NULL);
296 for (;;) {
297 struct task *task = task_find_by_mtime(task_list,
298 tv.tv_sec - DEVICE_TASK_TIMEOUT);
299 if (!task)
300 break;
301 // Underlying physical device "silently" stopped operation.
302 device_stop(task->jtr_device->device, task_list, "Timeout.");
303 }
304
305 // Process input packets, store results in task_result
306 for (;;) {
307 struct jtr_device *dev
308 = jtr_device_list_process_inpkt(task_list);
309 if (!dev)
310 break;
311 device_stop(dev->device, task_list, "bad input packet.");
312 }
313
314 // Computation done.
315 if (task_list_all_completed(task_list))
316 break;
317
318 // There was no data transfer on devices.
319 // Don't use 100% CPU in a loop.
320 if (rw_result <= 0)
321 usleep(1000);
322
323 }
324
325 if (!WAIT_UNTIL_DEVICE_UP && rw_result < 0) {
326 fprintf(stderr, "No ZTEX devices available, exiting\n");
327 error();
328 }
329
330 // Number of devices can change at runtime.
331 // Dynamic adjustment of max_keys_per_crypt could be a good idea.
332
333 *pcount *= mask_num_cand();
334 int result_count = task_list_result_count(task_list);
335
336 if (jtr_verbosity > VERB_LEGACY)
337 fprintf(stderr,"result_count:%d\n", result_count);
338
339 task_list_create_index(task_list);
340
341 return result_count;
342 }
343
344
get_hash(int index)345 inline static int get_hash(int index)
346 {
347 uint32_t out;
348 struct task_result *result = task_result_by_index(task_list, index);
349 if (!result || !result->binary) {
350 fprintf(stderr,"get_hash(%d): no task_result or binary\n", index);
351 error();
352 }
353 out = *(uint32_t *)result->binary;
354 //fprintf(stderr,"get_hash(%d): %04x, key %s\n",index,out,result->key);
355 return out;
356 }
357
358
device_format_get_hash_0(int index)359 int device_format_get_hash_0(int index) {
360 return get_hash(index) & PH_MASK_0;
361 }
device_format_get_hash_1(int index)362 int device_format_get_hash_1(int index) {
363 return get_hash(index) & PH_MASK_1;
364 }
device_format_get_hash_2(int index)365 int device_format_get_hash_2(int index) {
366 return get_hash(index) & PH_MASK_2;
367 }
device_format_get_hash_3(int index)368 int device_format_get_hash_3(int index) {
369 return get_hash(index) & PH_MASK_3;
370 }
device_format_get_hash_4(int index)371 int device_format_get_hash_4(int index) {
372 return get_hash(index) & PH_MASK_4;
373 }
device_format_get_hash_5(int index)374 int device_format_get_hash_5(int index) {
375 return get_hash(index) & PH_MASK_5;
376 }
device_format_get_hash_6(int index)377 int device_format_get_hash_6(int index) {
378 return get_hash(index) & PH_MASK_6;
379 }
380
381
device_format_cmp_all(void * binary,int count)382 int device_format_cmp_all(void *binary, int count)
383 {
384 return !!count;
385 }
386
387
device_format_cmp_one(void * binary,int index)388 int device_format_cmp_one(void *binary, int index)
389 {
390 struct task_result *result = task_result_by_index(task_list, index);
391 if (!result) {
392 fprintf(stderr,"device_format_cmp_one(%d): no result\n", index);
393 error();
394 }
395 if (!result->binary) {
396 fprintf(stderr,"device_format_cmp_one(%d): no binary\n", index);
397 error();
398 }
399
400 return !memcmp(result->binary, binary, jtr_fmt_params->binary_size);
401 }
402
403
device_format_cmp_exact(char * source,int index)404 int device_format_cmp_exact(char *source, int index)
405 {
406 // TODO
407 return 1;
408 }
409
410
device_format_get_password(int index)411 struct db_password *device_format_get_password(int index)
412 {
413 struct task_result *result = task_result_by_index(task_list, index);
414 if (!result) {
415 fprintf(stderr, "get_password(%d): no task_result\n", index);
416 return NULL;
417 }
418 if (!result->pw) {
419 fprintf(stderr, "get_password(%d): no result->pw\n", index);
420 return NULL;
421 }
422 return result->pw;
423 }
424
425
device_format_get_key(int index)426 char *device_format_get_key(int index)
427 {
428 if (index < 0 || index >= (mask_num_cand() ? mask_num_cand() : 1)
429 * jtr_fmt_params->max_keys_per_crypt)
430 return "-----";
431
432 // It happens status reporting is requested and there's
433 // a task_result at same index.
434 if (task_list) {
435 struct task_result *result = task_result_by_index(task_list, index);
436 if (result)
437 return result->key;
438 }
439
440 // There must be status reporting or self-test
441 int plaintext_len = jtr_fmt_params->plaintext_length;
442
443 if (mask_num_cand() > 1) {
444 int key_num = index / mask_num_cand();
445 int gen_id = index % mask_num_cand();
446 memcpy(output_key, keys_buffer
447 + key_num * plaintext_len, plaintext_len);
448 mask_reconstruct_plaintext(output_key, range_info_buffer
449 + key_num * MASK_FMT_INT_PLHDR, gen_id);
450
451 } else {
452 memcpy(output_key, keys_buffer + index * plaintext_len,
453 plaintext_len);
454 }
455
456 return output_key;
457 }
458