1 /*
2  * This file is part of John the Ripper password cracker,
3  * Copyright (c) 2013-2018 by magnum
4  * Copyright (c) 2014 by Sayantan Datta
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted.
8  *
9  * There's ABSOLUTELY NO WARRANTY, express or implied.
10  */
11 
12 #include "mask_ext.h"
13 #include "misc.h"	// error()
14 #include "options.h"
15 #include "memory.h"
16 
17 //#define MASK_DEBUG
18 
19 int *mask_skip_ranges = NULL;
20 int mask_max_skip_loc = -1;
21 int mask_int_cand_target = 0;
22 int mask_gpu_is_static = 0;
23 mask_int_cand_ctx mask_int_cand = { NULL, NULL, 1 };
24 
combination_util(int * data,int start,int end,int index,int r,mask_cpu_context * ptr,int * delta)25 static void combination_util(int *data, int start, int end, int index,
26                              int r, mask_cpu_context *ptr, int *delta)
27 {
28 	int i;
29 
30 	if (index == r) {
31 		int tmp = 1;
32 		for (i = 0; i < r; i++)
33 			tmp *= ptr->ranges[data[i]].count;
34 
35 		tmp -= mask_int_cand_target;
36 		tmp = tmp < 0 ? -tmp : tmp;
37 
38 		if (tmp < *delta) {
39 			for (i = 0; i < r; i++)
40 				mask_skip_ranges[i] = data[i];
41 
42 			mask_max_skip_loc = mask_skip_ranges[i-1];
43 			*delta = tmp;
44 		}
45 
46 		return;
47 	}
48 
49 	for (i = start; i <= end && end - i + 1 >= r - index; i++) {
50 		data[index] = i;
51 		combination_util(data, i + 1, end, index + 1,
52 				 r, ptr, delta);
53 	}
54 }
55 
generate_int_keys(mask_cpu_context * ptr)56 static void generate_int_keys(mask_cpu_context *ptr)
57 {
58 	int i, repeat = 1, modulo;
59 
60 #ifdef MASK_DEBUG
61 	fprintf(stderr, "%s()\n", __FUNCTION__);
62 #endif
63 
64 #define fill_cand(t) \
65 	for (i = 0; i < mask_int_cand.num_int_cand; i++) \
66 		mask_int_cand.int_cand[i].x[t] =	 \
67 			ptr->ranges[mask_skip_ranges[t]].chars \
68 			[(i/repeat) % modulo]
69 
70 #define cond(t) t < MASK_FMT_INT_PLHDR && mask_skip_ranges[t] != -1
71 
72 	for (i = 1; i < MASK_FMT_INT_PLHDR && mask_skip_ranges[i] != -1; i++)
73 		repeat *= ptr->ranges[mask_skip_ranges[i]].count;
74 	modulo = ptr->ranges[mask_skip_ranges[0]].count;
75 
76 	for (i = 0; i < mask_int_cand.num_int_cand; i++)
77 		mask_int_cand.int_cand[i].i = 0;
78 
79 	fill_cand(0);
80 
81 	if (cond(1)) {
82 		modulo = ptr->ranges[mask_skip_ranges[1]].count;
83 		repeat /= modulo;
84 		fill_cand(1);
85 	}
86 	if (cond(2)) {
87 		modulo = ptr->ranges[mask_skip_ranges[2]].count;
88 		repeat /= modulo;
89 		fill_cand(2);
90 	}
91 	if (cond(3)) {
92 		repeat = 1;
93 		modulo = ptr->ranges[mask_skip_ranges[3]].count;
94 		fill_cand(3);
95 	}
96 #undef fill_cand
97 #undef cond
98 }
99 
check_static_gpu_mask(int max_static_range)100 static void check_static_gpu_mask(int max_static_range)
101 {
102 	unsigned int i;
103 	mask_gpu_is_static = 1;
104 
105 	for (i = 0; i < MASK_FMT_INT_PLHDR; i++)
106 		if (max_static_range <= mask_skip_ranges[i]) {
107 			mask_gpu_is_static = 0;
108 			break;
109 		}
110 
111 	mask_gpu_is_static |= !(options.flags & FLG_MASK_STACKED);
112 
113 #ifdef MASK_DEBUG
114 	fprintf(stderr, "%s() return: mask is%s static\n", __FUNCTION__, mask_gpu_is_static ? "" : "n't");
115 #endif
116 }
117 
mask_ext_calc_combination(mask_cpu_context * ptr,int max_static_range)118 void mask_ext_calc_combination(mask_cpu_context *ptr, int max_static_range)
119 {
120 	int *data, i, n;
121 	int delta_to_target = 0x7fffffff;
122 
123 #ifdef MASK_DEBUG
124 	fprintf(stderr, "%s()\n", __FUNCTION__);
125 #endif
126 
127 	mask_int_cand.num_int_cand = 1;
128 	mask_int_cand.int_cpu_mask_ctx = NULL;
129 	mask_int_cand.int_cand = NULL;
130 
131 	if (!mask_int_cand_target)
132 		return;
133 
134 	if (MASK_FMT_INT_PLHDR > 4) {
135 		fprintf(stderr, "MASK_FMT_INT_PLHDR value must not exceed 4.\n");
136 		error();
137 	}
138 
139 	n = ptr->count;
140 	data = (int*) mem_alloc(n * sizeof(int));
141 	mask_skip_ranges = (int*) mem_alloc(MASK_FMT_INT_PLHDR * sizeof(int));
142 
143 	for (i = 0; i < MASK_FMT_INT_PLHDR; i++)
144 		mask_skip_ranges[i] = -1;
145 
146 	/* Fix the maximum number of ranges that can be calculated on GPU to 3 */
147 	for (i = 1; i <= MASK_FMT_INT_PLHDR; i++)
148 		combination_util(data, 0, n - 1, 0, i, ptr,
149 				 &delta_to_target);
150 
151 	if (mask_skip_ranges[0] != -1) {
152 		mask_int_cand.num_int_cand = 1;
153 		for (i = 0; i < MASK_FMT_INT_PLHDR &&
154 		     mask_skip_ranges[i] != -1; i++)
155 			mask_int_cand.num_int_cand *= ptr->
156 				ranges[mask_skip_ranges[i]].count;
157 	}
158 
159 	if (mask_int_cand.num_int_cand > 1) {
160 		mask_int_cand.int_cpu_mask_ctx = ptr;
161 		mask_int_cand.int_cand = (mask_char4 *)
162 			mem_alloc(mask_int_cand.num_int_cand * sizeof(mask_char4));
163 		generate_int_keys(ptr);
164 	}
165 
166 	check_static_gpu_mask(max_static_range);
167 
168 #if 0
169 	for (i = 0; i < mask_int_cand.num_int_cand && mask_int_cand.int_cand; i++)
170 		fprintf(stderr, "%c%c%c%c\n", mask_int_cand.int_cand[i].x[0], mask_int_cand.int_cand[i].x[1], mask_int_cand.int_cand[i].x[2], mask_int_cand.int_cand[i].x[3]);
171 #endif
172 	MEM_FREE(data);
173 }
174