1 /*
2  * Tiger cracker patch for JtR. Hacked together during April of 2013 by Dhiru
3  * Kholia <dhiru at openwall.com>.
4  *
5  * This software is Copyright (c) 2013 Dhiru Kholia <dhiru at openwall.com> and
6  * it is hereby released to the general public under the following terms:
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted.
10  */
11 
12 #if FMT_EXTERNS_H
13 extern struct fmt_main fmt_tiger;
14 #elif FMT_REGISTERS_H
15 john_register_one(&fmt_tiger);
16 #else
17 
18 #include <string.h>
19 
20 #ifdef _OPENMP
21 #include <omp.h>
22 #endif
23 
24 #include "arch.h"
25 #include "sph_tiger.h"
26 #include "misc.h"
27 #include "common.h"
28 #include "formats.h"
29 #include "params.h"
30 #include "options.h"
31 
32 #define FORMAT_LABEL		"Tiger"
33 #define FORMAT_NAME		""
34 #define FORMAT_TAG		"$tiger$"
35 #define TAG_LENGTH		(sizeof(FORMAT_TAG)-1)
36 #define ALGORITHM_NAME		"Tiger 32/" ARCH_BITS_STR
37 #define BENCHMARK_COMMENT	""
38 #define BENCHMARK_LENGTH	0x107
39 #define PLAINTEXT_LENGTH	125
40 #define BINARY_SIZE		24
41 #define SALT_SIZE		0
42 #define BINARY_ALIGN		4
43 #define SALT_ALIGN		1
44 
45 #ifndef OMP_SCALE
46 #ifdef __MIC__
47 #define OMP_SCALE  2
48 #else
49 #define OMP_SCALE  32 // Tuned w/ MKPC for core i7
50 #endif
51 #endif
52 
53 #define MIN_KEYS_PER_CRYPT	1
54 #define MAX_KEYS_PER_CRYPT	64
55 
56 static struct fmt_tests tiger_tests[] = {
57 	{"3293AC630C13F0245F92BBB1766E16167A4E58492DDE73F3", ""},
58 	{"$tiger$D981F8CB78201A950DCF3048751E441C517FCA1AA55A29F6", "message digest"},
59 	{"$tiger$a90197a19d2872ed8a5d508ba5b42deecf08344cc9f42195", "12346789"},
60 	{"$tiger$4a82b9bb5911e1eccfd27d90584903d568e4f96b4ecf0d97", "UPPERCASE"},
61 	{NULL}
62 };
63 
64 static char (*saved_key)[PLAINTEXT_LENGTH + 1];
65 static uint32_t (*crypt_out)[BINARY_SIZE / sizeof(uint32_t)];
66 
init(struct fmt_main * self)67 static void init(struct fmt_main *self)
68 {
69 	omp_autotune(self, OMP_SCALE);
70 
71 	saved_key = mem_calloc(self->params.max_keys_per_crypt,
72 	                       sizeof(*saved_key));
73 	crypt_out = mem_calloc(self->params.max_keys_per_crypt,
74 	                       sizeof(*crypt_out));
75 }
76 
done(void)77 static void done(void)
78 {
79 	MEM_FREE(crypt_out);
80 	MEM_FREE(saved_key);
81 }
82 
valid(char * ciphertext,struct fmt_main * self)83 static int valid(char *ciphertext, struct fmt_main *self)
84 {
85 	char *p;
86 
87 	p = ciphertext;
88 
89 	if (!strncmp(p, FORMAT_TAG, TAG_LENGTH))
90 		p += TAG_LENGTH;
91 	if (strlen(p) != BINARY_SIZE * 2)
92 		return 0;
93 
94 	while(*p)
95 		if (atoi16[ARCH_INDEX(*p++)] == 0x7f)
96 			return 0;
97 	return 1;
98 }
99 
split(char * ciphertext,int index,struct fmt_main * self)100 static char *split(char *ciphertext, int index, struct fmt_main *self)
101 {
102 	static char out[TAG_LENGTH + BINARY_SIZE*2 + 1];
103 
104 	if (!strncmp(ciphertext, FORMAT_TAG, TAG_LENGTH))
105 		ciphertext += TAG_LENGTH;
106 
107 	memcpy(out, FORMAT_TAG, TAG_LENGTH);
108 	strnzcpy(out + TAG_LENGTH, ciphertext, BINARY_SIZE*2 + 1);
109 	strupr(out + TAG_LENGTH);
110 	return out;
111 }
112 
get_binary(char * ciphertext)113 static void *get_binary(char *ciphertext)
114 {
115 	static union {
116 		unsigned char c[BINARY_SIZE];
117 		ARCH_WORD dummy;
118 	} buf;
119 	unsigned char *out = buf.c;
120 	char *p;
121 	int i;
122 
123 	p = ciphertext + TAG_LENGTH;
124 
125 	for (i = 0; i < BINARY_SIZE; i++) {
126 		out[i] =
127 		    (atoi16[ARCH_INDEX(*p)] << 4) |
128 		    atoi16[ARCH_INDEX(p[1])];
129 		p += 2;
130 	}
131 
132 	return out;
133 }
134 
135 #define COMMON_GET_HASH_VAR crypt_out
136 #include "common-get-hash.h"
137 
crypt_all(int * pcount,struct db_salt * salt)138 static int crypt_all(int *pcount, struct db_salt *salt)
139 {
140 	const int count = *pcount;
141 	int index;
142 
143 #ifdef _OPENMP
144 #pragma omp parallel for
145 #endif
146 	for (index = 0; index < count; index++) {
147 		sph_tiger_context ctx;
148 
149 		sph_tiger_init(&ctx);
150 		sph_tiger(&ctx, saved_key[index], strlen(saved_key[index]));
151 		sph_tiger_close(&ctx, (unsigned char*)crypt_out[index]);
152 	}
153 
154 	return count;
155 }
156 
cmp_all(void * binary,int count)157 static int cmp_all(void *binary, int count)
158 {
159 	int index;
160 
161 	for (index = 0; index < count; index++)
162 		if (!memcmp(binary, crypt_out[index], ARCH_SIZE))
163 			return 1;
164 	return 0;
165 }
166 
cmp_one(void * binary,int index)167 static int cmp_one(void *binary, int index)
168 {
169 	return !memcmp(binary, crypt_out[index], BINARY_SIZE);
170 }
171 
cmp_exact(char * source,int index)172 static int cmp_exact(char *source, int index)
173 {
174 	return 1;
175 }
176 
tiger_set_key(char * key,int index)177 static void tiger_set_key(char *key, int index)
178 {
179 	strnzcpy(saved_key[index], key, sizeof(*saved_key));
180 }
181 
get_key(int index)182 static char *get_key(int index)
183 {
184 	return saved_key[index];
185 }
186 
187 struct fmt_main fmt_tiger = {
188 	{
189 		FORMAT_LABEL,
190 		FORMAT_NAME,
191 		ALGORITHM_NAME,
192 		BENCHMARK_COMMENT,
193 		BENCHMARK_LENGTH,
194 		0,
195 		PLAINTEXT_LENGTH,
196 		BINARY_SIZE,
197 		BINARY_ALIGN,
198 		SALT_SIZE,
199 		SALT_ALIGN,
200 		MIN_KEYS_PER_CRYPT,
201 		MAX_KEYS_PER_CRYPT,
202 		FMT_OMP | FMT_CASE | FMT_8_BIT | FMT_SPLIT_UNIFIES_CASE,
203 		{ NULL },
204 		{ FORMAT_TAG },
205 		tiger_tests
206 	}, {
207 		init,
208 		done,
209 		fmt_default_reset,
210 		fmt_default_prepare,
211 		valid,
212 		split,
213 		get_binary,
214 		fmt_default_salt,
215 		{ NULL },
216 		fmt_default_source,
217 		{
218 			fmt_default_binary_hash_0,
219 			fmt_default_binary_hash_1,
220 			fmt_default_binary_hash_2,
221 			fmt_default_binary_hash_3,
222 			fmt_default_binary_hash_4,
223 			fmt_default_binary_hash_5,
224 			fmt_default_binary_hash_6
225 		},
226 		fmt_default_salt_hash,
227 		NULL,
228 		fmt_default_set_salt,
229 		tiger_set_key,
230 		get_key,
231 		fmt_default_clear_keys,
232 		crypt_all,
233 		{
234 #define COMMON_GET_HASH_LINK
235 #include "common-get-hash.h"
236 		},
237 		cmp_all,
238 		cmp_one,
239 		cmp_exact
240 	}
241 };
242 
243 #endif /* plugin stanza */
244