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