1 /*
2  * GPG cracker patch for JtR. Hacked together during Monsoon of 2012 by
3  * Dhiru Kholia <dhiru.kholia at gmail.com> .
4  *
5  * This software is Copyright (c) 2012, Dhiru Kholia <dhiru.kholia at gmail.com>
6  * and is based on,
7  *
8  * pgpry - PGP private key recovery
9  * Copyright (C) 2010 Jonas Gehring
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>
23  *
24  * Converted to use 'common' code, Feb29-Mar1 2016, JimF.
25  */
26 
27 #if FMT_EXTERNS_H
28 extern struct fmt_main fmt_gpg;
29 #elif FMT_REGISTERS_H
30 john_register_one(&fmt_gpg);
31 #else
32 
33 #include <string.h>
34 #include <stdint.h>
35 
36 #ifdef _OPENMP
37 #include <omp.h>
38 #endif
39 
40 #include "arch.h"
41 #include "params.h"
42 #include "common.h"
43 #include "formats.h"
44 #include "misc.h"
45 #include "twofish.h"
46 #include "md5.h"
47 #include "rc4.h"
48 #include "pdfcrack_md5.h"
49 #include "sha.h"
50 #include "sha2.h"
51 #include "gpg_common.h"
52 
53 #define FORMAT_LABEL        "gpg"
54 #define FORMAT_NAME         "OpenPGP / GnuPG Secret Key"
55 #define ALGORITHM_NAME      "32/" ARCH_BITS_STR
56 #define BENCHMARK_LENGTH    0x107
57 #define SALT_SIZE           sizeof(struct gpg_common_custom_salt*)
58 
59 #ifndef OMP_SCALE
60 #define OMP_SCALE           1 // MKPC and scale tuned for i7
61 #endif
62 
63 #define MIN_KEYS_PER_CRYPT  1
64 #define MAX_KEYS_PER_CRYPT  1
65 
66 static char (*saved_key)[PLAINTEXT_LENGTH + 1];
67 static int *cracked;
68 static int any_cracked;
69 static size_t cracked_size;
70 
init(struct fmt_main * self)71 static void init(struct fmt_main *self)
72 {
73 	omp_autotune(self, OMP_SCALE);
74 
75 	saved_key = mem_calloc_align(sizeof(*saved_key),
76 			self->params.max_keys_per_crypt, MEM_ALIGN_WORD);
77 	any_cracked = 0;
78 	cracked_size = sizeof(*cracked) * self->params.max_keys_per_crypt;
79 	cracked = mem_calloc_align(sizeof(*cracked), self->params.max_keys_per_crypt, MEM_ALIGN_WORD);
80 	Twofish_initialise();
81 }
82 
done(void)83 static void done(void)
84 {
85 	MEM_FREE(cracked);
86 	MEM_FREE(saved_key);
87 }
88 
valid(char * ciphertext,struct fmt_main * self)89 static int valid(char *ciphertext, struct fmt_main *self)
90 {
91 	return gpg_common_valid(ciphertext, self, 1);
92 }
93 
set_salt(void * salt)94 static void set_salt(void *salt)
95 {
96 	gpg_common_cur_salt = *(struct gpg_common_custom_salt **)salt;
97 }
98 
gpg_set_key(char * key,int index)99 static void gpg_set_key(char *key, int index)
100 {
101 	strnzcpy(saved_key[index], key, sizeof(*saved_key));
102 }
103 
get_key(int index)104 static char *get_key(int index)
105 {
106 	return saved_key[index];
107 }
108 
crypt_all(int * pcount,struct db_salt * salt)109 static int crypt_all(int *pcount, struct db_salt *salt)
110 {
111 	const int count = *pcount;
112 	int index = 0;
113 	int ks = gpg_common_keySize(gpg_common_cur_salt->cipher_algorithm);
114 
115 	if (any_cracked) {
116 		memset(cracked, 0, cracked_size);
117 		any_cracked = 0;
118 	}
119 
120 #ifdef _OPENMP
121 #pragma omp parallel for
122 #endif
123 	for (index = 0; index < count; index++) {
124 		int res;
125 		unsigned char keydata[64];
126 
127 		gpg_common_cur_salt->s2kfun(saved_key[index], keydata, ks);
128 		res = gpg_common_check(keydata, ks);
129 		if (res) {
130 			cracked[index] = 1;
131 #ifdef _OPENMP
132 #pragma omp atomic
133 #endif
134 			any_cracked |= 1;
135 		}
136 	}
137 
138 	return count;
139 }
140 
cmp_all(void * binary,int count)141 static int cmp_all(void *binary, int count)
142 {
143 	return any_cracked;
144 }
145 
cmp_one(void * binary,int index)146 static int cmp_one(void *binary, int index)
147 {
148 	return cracked[index];
149 }
150 
cmp_exact(char * source,int index)151 static int cmp_exact(char *source, int index)
152 {
153 	return 1;
154 }
155 
156 struct fmt_main fmt_gpg = {
157 	{
158 		FORMAT_LABEL,
159 		FORMAT_NAME,
160 		ALGORITHM_NAME,
161 		BENCHMARK_COMMENT,
162 		BENCHMARK_LENGTH,
163 		0,
164 		PLAINTEXT_LENGTH,
165 		BINARY_SIZE,
166 		BINARY_ALIGN,
167 		SALT_SIZE,
168 		SALT_ALIGN,
169 		MIN_KEYS_PER_CRYPT,
170 		MAX_KEYS_PER_CRYPT,
171 		FMT_CASE | FMT_8_BIT | FMT_OMP | FMT_DYNA_SALT | FMT_HUGE_INPUT,
172 		{
173 			"s2k-count", /* only for gpg --s2k-mode 3, see man gpg, option --s2k-count n */
174 			"hash algorithm [1:MD5 2:SHA1 3:RIPEMD160 8:SHA256 9:SHA384 10:SHA512 11:SHA224]",
175 			"cipher algorithm [1:IDEA 2:3DES 3:CAST5 4:Blowfish 7:AES128 8:AES192 9:AES256 10:Twofish 11:Camellia128 12:Camellia192 13:Camellia256]",
176 		},
177 		{ FORMAT_TAG },
178 		gpg_common_gpg_tests
179 	},
180 	{
181 		init,
182 		done,
183 		fmt_default_reset,
184 		fmt_default_prepare,
185 		valid,
186 		fmt_default_split,
187 		fmt_default_binary,
188 		gpg_common_get_salt,
189 		{
190 			gpg_common_gpg_s2k_count,
191 			gpg_common_gpg_hash_algorithm,
192 			gpg_common_gpg_cipher_algorithm,
193 		},
194 		fmt_default_source,
195 		{
196 			fmt_default_binary_hash
197 		},
198 		fmt_default_salt_hash,
199 		NULL,
200 		set_salt,
201 		gpg_set_key,
202 		get_key,
203 		fmt_default_clear_keys,
204 		crypt_all,
205 		{
206 			fmt_default_get_hash
207 		},
208 		cmp_all,
209 		cmp_one,
210 		cmp_exact
211 	}
212 };
213 
214 #endif /* plugin stanza */
215