1 /*
2  * This file is part of John the Ripper password cracker,
3  * Copyright (c) 1996-2001,2005,2010-2012,2015,2017 by Solar Designer
4  */
5 
6 /*
7  * Bitslice DES implementation.
8  */
9 
10 #ifndef _JOHN_DES_BS_H
11 #define _JOHN_DES_BS_H
12 
13 #include <stdint.h>
14 
15 #include "arch.h"
16 #include "common.h"
17 
18 /* For struct db_salt */
19 #include "loader.h"
20 
21 #ifndef DES_BS_ALGORITHM_NAME
22 #define DES_BS_ALGORITHM_NAME		"DES " ARCH_BITS_STR "/" ARCH_BITS_STR
23 #endif
24 
25 #if DES_BS_VECTOR
26 #define DES_BS_DEPTH			(ARCH_BITS * DES_BS_VECTOR)
27 #else
28 #define DES_BS_DEPTH			ARCH_BITS
29 #endif
30 
31 #if DES_BS_VECTOR
32 #ifndef DES_BS_VECTOR_SIZE
33 #define DES_BS_VECTOR_SIZE		DES_BS_VECTOR
34 #endif
35 typedef ARCH_WORD DES_bs_vector[DES_BS_VECTOR_SIZE];
36 #else
37 #define DES_bs_vector			ARCH_WORD
38 #endif
39 
40 /*
41  * All bitslice DES parameters combined into one struct for more efficient
42  * cache usage. Don't re-order unless you know what you're doing, as there
43  * is an optimization that would produce undefined results if you did.
44  *
45  * This must match the definition in x86-mmx.S.
46  */
47 typedef struct {
48 #if DES_BS_EXPAND
49 	ARCH_WORD *KSp[0x300];	/* Initial key schedule (key bit pointers) */
50 #endif
51 	union {
52 		ARCH_WORD *p[0x300];	/* Key bit pointers */
53 #if DES_BS_EXPAND
54 		DES_bs_vector v[0x300];	/* Key bit values */
55 #endif
56 	} KS;			/* Current key schedule */
57 	union {
58 		ARCH_WORD *E[96];	/* Expansion function (data bit ptrs) */
59 		unsigned char u[0x100];	/* Uppercase (for LM) */
60 	} E;
61 	DES_bs_vector K[56];	/* Keys */
62 	DES_bs_vector B[64];	/* Data blocks */
63 #if DES_BS_ASM
64 	DES_bs_vector tmp[16];	/* Miscellaneous temporary storage */
65 #else
66 	DES_bs_vector zero;	/* All 0 bits */
67 	DES_bs_vector ones;	/* All 1 bits */
68 	DES_bs_vector masks[8];	/* Each byte set to 0x01 ... 0x80 */
69 #endif
70 	union {
71 		unsigned char c[8][8][sizeof(DES_bs_vector)];
72 		DES_bs_vector v[8][8];
73 	} xkeys;		/* Partially transposed key bits matrix */
74 	unsigned char *pxkeys[DES_BS_DEPTH]; /* Pointers into xkeys.c */
75 	int keys_changed;	/* If keys have changed */
76 	unsigned int salt;	/* Salt value corresponding to E[] contents */
77 	DES_bs_vector *Ens[48];	/* Pointers into B[] for non-salted E */
78 } DES_bs_combined;
79 
80 //store plaintext//
81 extern DES_bs_vector DES_bs_P[64];
82 
83 
84 #if defined(_OPENMP) && !DES_BS_ASM
85 #define DES_bs_mt			1
86 #if __AVX2__
87 #define DES_bs_cpt			16
88 #else
89 #define DES_bs_cpt			32
90 #endif
91 #define DES_bs_mt_max			(DES_bs_cpt * 1024)
92 extern int DES_bs_min_kpc, DES_bs_max_kpc;
93 extern int DES_bs_nt;
94 extern DES_bs_combined *DES_bs_all_p;
95 #define DES_bs_all_align		64
96 #define DES_bs_all_size \
97 	((sizeof(DES_bs_combined) + (DES_bs_all_align - 1)) & \
98 	    ~(DES_bs_all_align - 1))
99 #define DES_bs_all_by_tnum(tnum) \
100 	(*(DES_bs_combined *)((char *)DES_bs_all_p + (tnum) * DES_bs_all_size))
101 #ifdef __GNUC__
102 #define DES_bs_all \
103 	(*(DES_bs_combined *)((char *)DES_bs_all_p + t))
104 #define for_each_t(n) \
105 	for (t = 0; t < (n) * DES_bs_all_size; t += DES_bs_all_size)
106 #define init_t() \
107 	int t = (unsigned int)index / DES_BS_DEPTH * DES_bs_all_size; \
108 	index = (unsigned int)index % DES_BS_DEPTH;
109 #else
110 /*
111  * For compilers that complain about the above e.g. with "iteration expression
112  * of omp for loop does not have a canonical shape".
113  */
114 #define DES_bs_all \
115 	DES_bs_all_by_tnum(t)
116 #define for_each_t(n) \
117 	for (t = 0; t < (n); t++)
118 #define init_t() \
119 	int t = (unsigned int)index / DES_BS_DEPTH; \
120 	index = (unsigned int)index % DES_BS_DEPTH;
121 #endif
122 #else
123 #define DES_bs_mt			0
124 #define DES_bs_cpt			1
125 extern DES_bs_combined CC_CACHE_ALIGN DES_bs_all;
126 #define for_each_t(n)
127 #define init_t()
128 #endif
129 
130 /*
131  * Initializes the internal structures.
132  */
133 extern void DES_bs_init(int LM, int cpt);
134 
135 /*
136  * Sets a salt for DES_bs_crypt().
137  */
138 extern void DES_bs_set_salt(ARCH_WORD salt);
139 #if DES_bs_mt
140 extern void DES_bs_set_salt_for_thread(int t, unsigned int salt);
141 #endif
142 
143 /*
144  * Set a key for DES_bs_crypt() or DES_bs_crypt_LM(), respectively.
145  */
146 extern void DES_bs_set_key(char *key, int index);
147 extern void DES_bs_set_key_LM(char *key, int index);
148 
149 /*
150  * Almost generic implementation: 24-bit salts, variable iteration count.
151  */
152 extern void DES_bs_crypt(int count, int keys_count);
153 
154 /*
155  * A simplified special-case implementation: 12-bit salts, 25 iterations.
156  */
157 extern void DES_bs_crypt_25(int keys_count);
158 
159 /*
160  * Another special-case version: a non-zero IV, no salts, no iterations.
161  */
162 extern int DES_bs_crypt_LM(int *keys_count, struct db_salt *salt);
163 
164 /*
165  * Converts an ASCII ciphertext to binary to be used with one of the
166  * comparison functions.
167  */
168 extern uint32_t *DES_bs_get_binary(char *ciphertext);
169 
170 /*
171  * Similarly, for LM hashes.
172  */
173 extern uint32_t *DES_bs_get_binary_LM(char *ciphertext);
174 
175 /*
176  * The reverse of DES_bs_get_binary_LM().
177  */
178 extern char *DES_bs_get_source_LM(uint32_t *raw);
179 
180 /*
181  * Calculate a hash for a DES_bs_crypt*() output.
182  *
183  * "t"-suffixed versions of these functions are for tripcodes (they skip
184  * bits that are part of the base-64 character not included in tripcodes).
185  * There's no DES_bs_get_hash_0t() because it would be exactly the same as
186  * DES_bs_get_hash_0() (all four initial bits are included in tripcodes).
187  */
188 extern int DES_bs_get_hash_0(int index);
189 extern int DES_bs_get_hash_1(int index);
190 extern int DES_bs_get_hash_2(int index);
191 extern int DES_bs_get_hash_3(int index);
192 extern int DES_bs_get_hash_4(int index);
193 extern int DES_bs_get_hash_5(int index);
194 extern int DES_bs_get_hash_6(int index);
195 extern int DES_bs_get_hash_0t(int index);
196 extern int DES_bs_get_hash_1t(int index);
197 extern int DES_bs_get_hash_2t(int index);
198 extern int DES_bs_get_hash_3t(int index);
199 extern int DES_bs_get_hash_4t(int index);
200 extern int DES_bs_get_hash_5t(int index);
201 
202 /*
203  * Compares 32 bits of a given ciphertext against at least the first count of
204  * the DES_bs_crypt*() outputs and returns zero if no matches detected.
205  */
206 extern int DES_bs_cmp_all(uint32_t *binary, int count);
207 
208 /*
209  * Compares count bits of a given ciphertext against one of the outputs.
210  */
211 extern int DES_bs_cmp_one(uint32_t *binary, int count, int index);
212 
213 extern void DES_bs_crypt_plain(int keys_count);
214 extern void DES_bs_generate_plaintext(unsigned char *plaintext);
215 #endif
216