Debian Codegraph @

deps/phc-winner-argon2-20190702/_hashcat/core.c

raw symbols SCIP
1 /*
2 * Argon2 reference source code package - reference C implementations
3 *
4 * Copyright 2015
5 * Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves
6 *
7 * You may use this work under the terms of a Creative Commons CC0 1.0
8 * License/Waiver or the Apache Public License 2.0, at your option. The terms of
9 * these licenses can be found at:
10 *
11 * - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
12 * - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * You should have received a copy of both of these licenses along with this
15 * software. If not, they may be obtained at the above URLs.
16 */
17
18 /*For memory wiping*/
19 #ifdef _MSC_VER
20 #include <windows.h>
21 #include <winbase.h> /* For SecureZeroMemory */
22 #endif
23 #if defined __STDC_LIB_EXT1__
24 #define __STDC_WANT_LIB_EXT1__ 1
25 #endif
26 #define VC_GE_2005(version) (version >= 1400)
27
28 /* for explicit_bzero() on glibc */
29 #define _DEFAULT_SOURCE
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "core.h"
36
37 #include "blake2/blake2.h"
38 #include "blake2/blake2-impl.h"
39
40
41
42 #if defined(__clang__)
43 #if __has_attribute(optnone)
44 #define NOT_OPTIMIZED __attribute__((optnone))
45 #endif
46 #elif defined(__GNUC__)
47 #define GCC_VERSION \
48 (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
49 #if GCC_VERSION >= 40400
50 #define NOT_OPTIMIZED __attribute__((optimize("O0")))
51 #endif
52 #endif
53 #ifndef NOT_OPTIMIZED
54 #define NOT_OPTIMIZED
55 #endif
56
57 /***************Instance and Position constructors**********/
58 void init_block_value(block *b, uint8_t in) { memset(b->v, in, sizeof(b->v)); }
59
60 void copy_block(block *dst, const block *src) {
61 memcpy(dst->v, src->v, sizeof(uint64_t) * ARGON2_QWORDS_IN_BLOCK);
62 }
63
64 void xor_block(block *dst, const block *src) {
65 int i;
66 for (i = 0; i < ARGON2_QWORDS_IN_BLOCK; ++i) {
67 dst->v[i] ^= src->v[i];
68 }
69 }
70
71 static void load_block(block *dst, const void *input) {
72 unsigned i;
73 for (i = 0; i < ARGON2_QWORDS_IN_BLOCK; ++i) {
74 dst->v[i] = load64((const uint8_t *)input + i * sizeof(dst->v[i]));
75 }
76 }
77
78 static void store_block(void *output, const block *src) {
79 unsigned i;
80 for (i = 0; i < ARGON2_QWORDS_IN_BLOCK; ++i) {
81 store64((uint8_t *)output + i * sizeof(src->v[i]), src->v[i]);
82 }
83 }
84
85 /***************Memory functions*****************/
86
87
88
89 void finalize(const argon2_context *context, argon2_instance_t *instance) {
90 if (context != NULL && instance != NULL) {
91 block blockhash;
92 uint32_t l;
93
94 copy_block(&blockhash, instance->memory + instance->lane_length - 1);
95
96 /* XOR the last blocks */
97 for (l = 1; l < instance->lanes; ++l) {
98 uint32_t last_block_in_lane =
99 l * instance->lane_length + (instance->lane_length - 1);
100 xor_block(&blockhash, instance->memory + last_block_in_lane);
101 }
102
103 /* Hash the result */
104 {
105 uint8_t blockhash_bytes[ARGON2_BLOCK_SIZE];
106 store_block(blockhash_bytes, &blockhash);
107 blake2b_long(context->out, context->outlen, blockhash_bytes,
108 ARGON2_BLOCK_SIZE);
109
110 }
111
112
113 }
114 }
115
116 uint32_t index_alpha(const argon2_instance_t *instance,
117 const argon2_position_t *position, uint32_t pseudo_rand,
118 int same_lane) {
119 /*
120 * Pass 0:
121 * This lane : all already finished segments plus already constructed
122 * blocks in this segment
123 * Other lanes : all already finished segments
124 * Pass 1+:
125 * This lane : (SYNC_POINTS - 1) last segments plus already constructed
126 * blocks in this segment
127 * Other lanes : (SYNC_POINTS - 1) last segments
128 */
129 uint32_t reference_area_size;
130 uint64_t relative_position;
131 uint32_t start_position, absolute_position;
132
133 if (0 == position->pass) {
134 /* First pass */
135 if (0 == position->slice) {
136 /* First slice */
137 reference_area_size =
138 position->index - 1; /* all but the previous */
139 } else {
140 if (same_lane) {
141 /* The same lane => add current segment */
142 reference_area_size =
143 position->slice * instance->segment_length +
144 position->index - 1;
145 } else {
146 reference_area_size =
147 position->slice * instance->segment_length +
148 ((position->index == 0) ? (-1) : 0);
149 }
150 }
151 } else {
152 /* Second pass */
153 if (same_lane) {
154 reference_area_size = instance->lane_length -
155 instance->segment_length + position->index -
156 1;
157 } else {
158 reference_area_size = instance->lane_length -
159 instance->segment_length +
160 ((position->index == 0) ? (-1) : 0);
161 }
162 }
163
164 /* 1.2.4. Mapping pseudo_rand to 0..<reference_area_size-1> and produce
165 * relative position */
166 relative_position = pseudo_rand;
167 relative_position = relative_position * relative_position >> 32;
168 relative_position = reference_area_size - 1 -
169 (reference_area_size * relative_position >> 32);
170
171 /* 1.2.5 Computing starting position */
172 start_position = 0;
173
174 if (0 != position->pass) {
175 start_position = (position->slice == ARGON2_SYNC_POINTS - 1)
176 ? 0
177 : (position->slice + 1) * instance->segment_length;
178 }
179
180 /* 1.2.6. Computing absolute position */
181 absolute_position = (start_position + relative_position) %
182 instance->lane_length; /* absolute position */
183 return absolute_position;
184 }
185
186 /* Single-threaded version for p=1 case */
187 int fill_memory_blocks(argon2_instance_t *instance) {
188 uint32_t r, s, l;
189
190 for (r = 0; r < instance->passes; ++r) {
191 for (s = 0; s < ARGON2_SYNC_POINTS; ++s) {
192 for (l = 0; l < instance->lanes; ++l) {
193 argon2_position_t position = {r, l, (uint8_t)s, 0};
194 fill_segment(instance, position);
195 }
196 }
197
198 }
199 return ARGON2_OK;
200 }
201
202
203
204 void fill_first_blocks(uint8_t *blockhash, const argon2_instance_t *instance) {
205 uint32_t l;
206 /* Make the first and second block in each lane as G(H0||0||i) or
207 G(H0||1||i) */
208 uint8_t blockhash_bytes[ARGON2_BLOCK_SIZE];
209 for (l = 0; l < instance->lanes; ++l) {
210
211 store32(blockhash + ARGON2_PREHASH_DIGEST_LENGTH, 0);
212 store32(blockhash + ARGON2_PREHASH_DIGEST_LENGTH + 4, l);
213 blake2b_long(blockhash_bytes, ARGON2_BLOCK_SIZE, blockhash,
214 ARGON2_PREHASH_SEED_LENGTH);
215 load_block(&instance->memory[l * instance->lane_length + 0],
216 blockhash_bytes);
217
218 store32(blockhash + ARGON2_PREHASH_DIGEST_LENGTH, 1);
219 blake2b_long(blockhash_bytes, ARGON2_BLOCK_SIZE, blockhash,
220 ARGON2_PREHASH_SEED_LENGTH);
221 load_block(&instance->memory[l * instance->lane_length + 1],
222 blockhash_bytes);
223 }
224
225 }
226
227 void initial_hash(uint8_t *blockhash, argon2_context *context,
228 argon2_type type) {
229 blake2b_state BlakeHash;
230 uint8_t value[sizeof(uint32_t)];
231
232 if (NULL == context || NULL == blockhash) {
233 return;
234 }
235
236 blake2b_init(&BlakeHash, ARGON2_PREHASH_DIGEST_LENGTH);
237
238 store32(&value, context->lanes);
239 blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
240
241 store32(&value, context->outlen);
242 blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
243
244 store32(&value, context->m_cost);
245 blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
246
247 store32(&value, context->t_cost);
248 blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
249
250 store32(&value, context->version);
251 blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
252
253 store32(&value, (uint32_t)type);
254 blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
255
256 store32(&value, context->pwdlen);
257 blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
258
259 if (context->pwd != NULL) {
260 blake2b_update(&BlakeHash, (const uint8_t *)context->pwd,
261 context->pwdlen);
262
263
264 }
265
266 store32(&value, context->saltlen);
267 blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
268
269 if (context->salt != NULL) {
270 blake2b_update(&BlakeHash, (const uint8_t *)context->salt,
271 context->saltlen);
272 }
273
274 store32(&value, context->secretlen);
275 blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
276
277 if (context->secret != NULL) {
278 blake2b_update(&BlakeHash, (const uint8_t *)context->secret,
279 context->secretlen);
280
281
282 }
283
284 store32(&value, context->adlen);
285 blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
286
287 if (context->ad != NULL) {
288 blake2b_update(&BlakeHash, (const uint8_t *)context->ad,
289 context->adlen);
290 }
291
292 blake2b_final(&BlakeHash, blockhash, ARGON2_PREHASH_DIGEST_LENGTH);
293 }
294
295 int initialize(argon2_instance_t *instance, argon2_context *context) {
296 uint8_t blockhash[ARGON2_PREHASH_SEED_LENGTH];
297
298
299
300 instance->context_ptr = context;
301
302 /* 1. Memory allocation */
303
304 instance->memory = context->memory;
305
306
307 /* 2. Initial hashing */
308 /* H_0 + 8 extra bytes to produce the first blocks */
309 /* uint8_t blockhash[ARGON2_PREHASH_SEED_LENGTH]; */
310 /* Hashing all inputs */
311 initial_hash(blockhash, context, instance->type);
312 /* Zeroing 8 extra bytes */
313
314
315 /* 3. Creating first blocks, we always have at least two blocks in a slice
316 */
317 fill_first_blocks(blockhash, instance);
318
319 return ARGON2_OK;
320 }