Debian Codegraph @

modules/crypto/argon2d.c

raw
1 /*
2 * Argon2d reference source code - reference C implementation
3 * Retrieved from <https://github.com/P-H-C/phc-winner-argon2>
4 *
5 * Copyright (C) 2015:
6 * - Daniel Dinu
7 * - Dmitry Khovratovich
8 * - Jean-Philippe Aumasson
9 * - Samuel Neves
10 *
11 * You may use this work under the terms of a Creative Commons CC0 1.0
12 * License/Waiver or the Apache Public License 2.0, at your option.
13 *
14 * The terms of these licenses can be found at:
15 * - CC0 1.0 Universal <http://creativecommons.org/publicdomain/zero/1.0>
16 * - Apache 2.0 <http://www.apache.org/licenses/LICENSE-2.0>
17 *
18 * Heavily modified for use as a password hashing module in
19 * Atheme IRC Services <https://github.com/atheme/atheme>
20 * by Aaron M. D. Jones <[email protected]> (2017)
21 */
22
23 #include "atheme.h"
24
25 #include <inttypes.h>
26 #include <limits.h>
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #define ARGON2D_MEMCOST_MIN 8
33 #define ARGON2D_MEMCOST_DEF 14
34 #define ARGON2D_MEMCOST_MAX 20
35
36 #define ARGON2D_TIMECOST_MIN 4
37 #define ARGON2D_TIMECOST_DEF 32
38 #define ARGON2D_TIMECOST_MAX 16384
39
40 #define ATHEME_ARGON2D_LOADB64 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
41
42 // Format strings for (s)scanf(3)
43 #define ATHEME_ARGON2D_LOADSALT "$argon2d$v=19$m=%" SCNu32 ",t=%" SCNu32 ",p=1$%[" ATHEME_ARGON2D_LOADB64 "]$"
44
45 // Format strings for (sn)printf(3)
46 #define ATHEME_ARGON2D_SAVESALT "$argon2d$v=19$m=%" PRIu32 ",t=%" PRIu32 ",p=1$%s$"
47 #define ATHEME_ARGON2D_SAVEHASH ATHEME_ARGON2D_SAVESALT "%s"
48
49 #define ATHEME_ARGON2D_AUTHLEN 0x00
50 #define ATHEME_ARGON2D_HASHLEN 0x40
51 #define ATHEME_ARGON2D_LANECNT 0x01
52 #define ATHEME_ARGON2D_PRIVLEN 0x00
53 #define ATHEME_ARGON2D_SALTLEN 0x20
54 #define ATHEME_ARGON2D_TYPEVAL 0x00
55 #define ATHEME_ARGON2D_VERSION 0x13
56
57 #define ARGON2_BLKSZ 0x400
58 #define ARGON2_PREHASH_LEN 0x40
59 #define ARGON2_PRESEED_LEN 0x48
60 #define ARGON2_BLK_QWORDS (ARGON2_BLKSZ / 0x08)
61 #define ARGON2_SYNC_POINTS 0x04
62
63 #define BLAKE2B_BLOCKLEN 0x80
64 #define BLAKE2B_HASHLEN 0x40
65 #define BLAKE2B_HASHLEN_HALF 0x20
66 #define BLAKE2B_PERSLEN 0x10
67 #define BLAKE2B_SALTLEN 0x10
68
69 #pragma pack(push, 1)
70
71 struct blake2b_param
72 {
73 uint8_t hash_len;
74 uint8_t key_len;
75 uint8_t fanout;
76 uint8_t depth;
77 uint32_t leaf_len;
78 uint64_t node_off;
79 uint8_t node_depth;
80 uint8_t inner_len;
81 uint8_t reserved[0x0E];
82 uint8_t salt[BLAKE2B_SALTLEN];
83 uint8_t personal[BLAKE2B_PERSLEN];
84 } __attribute__((packed));
85
86 #pragma pack(pop)
87
88 struct blake2b_state
89 {
90 uint64_t h[0x08];
91 uint64_t t[0x02];
92 uint64_t f[0x02];
93 size_t buflen;
94 size_t outlen;
95 uint8_t last_node;
96 uint8_t buf[BLAKE2B_BLOCKLEN];
97 };
98
99 struct argon2d_block
100 {
101 uint64_t v[ARGON2_BLK_QWORDS];
102 };
103
104 struct argon2d_context
105 {
106 const uint8_t *pass;
107 uint8_t salt[ATHEME_ARGON2D_SALTLEN];
108 uint8_t hash[ATHEME_ARGON2D_HASHLEN];
109 uint32_t passlen;
110 uint32_t m_cost;
111 uint32_t t_cost;
112 uint32_t lane_len;
113 uint32_t seg_len;
114 uint32_t index;
115 };
116
117 enum BLAKE2B_SZCHK_STATIC_ASSERT
118 {
119 // Ensure `struct blake2b_param' has been properly packed & padded (a poor man's `static_assert')
120 // This will generate a compile-time division-by-zero error if this is not the case
121 // This is critical to the correct functioning of blake2b_init() below
122 // INTEROPERABILITY WITH OTHER ARGON2D IMPLMEMENTATIONS MAY BE JEOPARDISED IF THIS IS REMOVED
123 BLAKE2B_SZCHK_0 = (0x01 / !!(CHAR_BIT == 0x08)),
124 BLAKE2B_SZCHK_1 = (0x01 / !!(sizeof(struct blake2b_param) == (sizeof(uint64_t) * CHAR_BIT)))
125 };
126
127 static const uint64_t blake2b_iv[0x08] = {
128
129 UINT64_C(0x6A09E667F3BCC908), UINT64_C(0xBB67AE8584CAA73B), UINT64_C(0x3C6EF372FE94F82B),
130 UINT64_C(0xA54FF53A5F1D36F1), UINT64_C(0x510E527FADE682D1), UINT64_C(0x9B05688C2B3E6C1F),
131 UINT64_C(0x1F83D9ABFB41BD6B), UINT64_C(0x5BE0CD19137E2179)
132 };
133
134 static const uint64_t blake2b_sigma[0x0C][0x10] = {
135
136 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F },
137 { 0x0E, 0x0A, 0x04, 0x08, 0x09, 0x0F, 0x0D, 0x06, 0x01, 0x0C, 0x00, 0x02, 0x0B, 0x07, 0x05, 0x03 },
138 { 0x0B, 0x08, 0x0C, 0x00, 0x05, 0x02, 0x0F, 0x0D, 0x0A, 0x0E, 0x03, 0x06, 0x07, 0x01, 0x09, 0x04 },
139 { 0x07, 0x09, 0x03, 0x01, 0x0D, 0x0C, 0x0B, 0x0E, 0x02, 0x06, 0x05, 0x0A, 0x04, 0x00, 0x0F, 0x08 },
140 { 0x09, 0x00, 0x05, 0x07, 0x02, 0x04, 0x0A, 0x0F, 0x0E, 0x01, 0x0B, 0x0C, 0x06, 0x08, 0x03, 0x0D },
141 { 0x02, 0x0C, 0x06, 0x0A, 0x00, 0x0B, 0x08, 0x03, 0x04, 0x0D, 0x07, 0x05, 0x0F, 0x0E, 0x01, 0x09 },
142 { 0x0C, 0x05, 0x01, 0x0F, 0x0E, 0x0D, 0x04, 0x0A, 0x00, 0x07, 0x06, 0x03, 0x09, 0x02, 0x08, 0x0B },
143 { 0x0D, 0x0B, 0x07, 0x0E, 0x0C, 0x01, 0x03, 0x09, 0x05, 0x00, 0x0F, 0x04, 0x08, 0x06, 0x02, 0x0A },
144 { 0x06, 0x0F, 0x0E, 0x09, 0x0B, 0x03, 0x00, 0x08, 0x0C, 0x02, 0x0D, 0x07, 0x01, 0x04, 0x0A, 0x05 },
145 { 0x0A, 0x02, 0x08, 0x04, 0x07, 0x06, 0x01, 0x05, 0x0F, 0x0B, 0x09, 0x0E, 0x03, 0x0C, 0x0D, 0x00 },
146 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F },
147 { 0x0E, 0x0A, 0x04, 0x08, 0x09, 0x0F, 0x0D, 0x06, 0x01, 0x0C, 0x00, 0x02, 0x0B, 0x07, 0x05, 0x03 }
148 };
149
150 /*
151 * This is reallocated on-demand to save allocating and freeing every time we
152 * digest a password. The mempoolsz variable tracks how large (in blocks) the
153 * currently-allocated memory pool is.
154 */
155 static struct argon2d_block *argon2d_mempool = NULL;
156 static uint32_t argon2d_mempoolsz = 0;
157
158 static inline bool __attribute__((warn_unused_result))
159 atheme_argon2d_mempool_realloc(const uint32_t mem_blocks)
160 {
161 if (argon2d_mempool != NULL && argon2d_mempoolsz >= mem_blocks)
162 return true;
163
164 struct argon2d_block *mempool_tmp;
165 const size_t required_sz = mem_blocks * sizeof(struct argon2d_block);
166
167 if (!(mempool_tmp = realloc(argon2d_mempool, required_sz)))
168 {
169 (void) slog(LG_ERROR, "%s: memory allocation failure", __func__);
170 return false;
171 }
172
173 argon2d_mempool = mempool_tmp;
174 argon2d_mempoolsz = mem_blocks;
175 return true;
176 }
177
178 #if (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)) || \
179 defined(__LITTLE_ENDIAN__) || defined(__ARMEL__) || defined(__MIPSEL__) || defined(__AARCH64EL__) || \
180 defined(__amd64__) || defined(__i386__) || defined(_M_IX86) || defined(_M_X64) || defined(_M_AMD64)
181
182 #define NATIVE_LITTLE_ENDIAN
183 #endif
184
185 static inline void
186 blake2b_store32(uint8_t *const restrict dst, const uint32_t w)
187 {
188 #if defined(NATIVE_LITTLE_ENDIAN)
189 (void) memcpy(dst, &w, sizeof w);
190 #else
191 for (size_t x = 0x00; x < 0x04; x++)
192 dst[x] = (uint8_t)(w >> (x * 0x08));
193 #endif
194 }
195
196 static inline void
197 blake2b_store64(uint8_t *const restrict dst, const uint64_t w)
198 {
199 #if defined(NATIVE_LITTLE_ENDIAN)
200 (void) memcpy(dst, &w, sizeof w);
201 #else
202 for (size_t x = 0x00; x < 0x08; x++)
203 dst[x] = (uint8_t)(w >> (x * 0x08));
204 #endif
205 }
206
207 static inline uint64_t
208 blake2b_load64(const uint8_t *const restrict src)
209 {
210 uint64_t w = 0;
211 #if defined(NATIVE_LITTLE_ENDIAN)
212 (void) memcpy(&w, src, sizeof w);
213 #else
214 for (size_t x = 0x00; x < 0x08; x++)
215 w |= (((uint64_t) src[x]) << (x * 0x08));
216 #endif
217 return w;
218 }
219
220 static inline uint64_t
221 blake2b_rotr64(const uint64_t w, const unsigned c)
222 {
223 return ((w >> c) | (w << (0x40 - c)));
224 }
225
226 static inline uint64_t
227 blake2b_fBlaMka(const uint64_t x, const uint64_t y)
228 {
229 const uint64_t m = UINT64_C(0xFFFFFFFF);
230 const uint64_t v = ((x & m) * (y & m));
231 return (x + y + (v * 0x02));
232 }
233
234 static inline void
235 blake2b_set_lastblock(struct blake2b_state *const restrict state)
236 {
237 state->f[0x00] = (uint64_t) -1;
238
239 if (state->last_node)
240 state->f[0x01] = (uint64_t) -1;
241 }
242
243 static inline void
244 blake2b_counter_inc(struct blake2b_state *const restrict state, const uint64_t inc)
245 {
246 state->t[0x00] += inc;
247 state->t[0x01] += (state->t[0x00] < inc);
248 }
249
250 static inline void
251 blake2b_init(struct blake2b_state *const restrict state, const size_t outlen)
252 {
253 struct blake2b_param params;
254 const uint8_t *const params_byte = (const uint8_t *) &params;
255
256 (void) memset(&params, 0x00, sizeof params);
257 (void) memset(state, 0x00, sizeof *state);
258 (void) memcpy(state->h, blake2b_iv, sizeof blake2b_iv);
259
260 params.depth = 0x01;
261 params.fanout = 0x01;
262 params.hash_len = (uint8_t) outlen;
263 state->outlen = outlen;
264
265 for (size_t x = 0x00; x < 0x08; x++)
266 state->h[x] ^= blake2b_load64(&params_byte[(x * sizeof state->h[x])]);
267 }
268
269 static void
270 blake2b_compress(struct blake2b_state *const restrict state, const uint8_t *const restrict block)
271 {
272 uint64_t m[0x10];
273 uint64_t v[0x10];
274
275 for (size_t x = 0x00; x < 0x10; x++)
276 m[x] = blake2b_load64(block + (x * sizeof m[x]));
277
278 for (size_t x = 0x00; x < 0x08; x++)
279 {
280 v[x] = state->h[x];
281 v[x + 0x08] = blake2b_iv[x];
282 }
283 for (size_t x = 0x00; x < 0x02; x++)
284 {
285 v[x + 0x0C] ^= state->t[x];
286 v[x + 0x0E] ^= state->f[x];
287 }
288
289 #define G(r, i, a, b, c, d) do { \
290 (a) = (a) + (b) + m[blake2b_sigma[r][(i * 0x02) + 0x00]]; \
291 (d) = blake2b_rotr64(((d) ^ (a)), 0x20); (c) += (d); \
292 (b) = blake2b_rotr64(((b) ^ (c)), 0x18); \
293 (a) = (a) + (b) + m[blake2b_sigma[r][(i * 0x02) + 0x01]]; \
294 (d) = blake2b_rotr64(((d) ^ (a)), 0x10); (c) += (d); \
295 (b) = blake2b_rotr64(((b) ^ (c)), 0x3F); \
296 } while (0)
297
298 for (size_t x = 0x00; x < 0x0C; x++)
299 {
300 G(x, 0x00, v[0x00], v[0x04], v[0x08], v[0x0C]);
301 G(x, 0x01, v[0x01], v[0x05], v[0x09], v[0x0D]);
302 G(x, 0x02, v[0x02], v[0x06], v[0x0A], v[0x0E]);
303 G(x, 0x03, v[0x03], v[0x07], v[0x0B], v[0x0F]);
304 G(x, 0x04, v[0x00], v[0x05], v[0x0A], v[0x0F]);
305 G(x, 0x05, v[0x01], v[0x06], v[0x0B], v[0x0C]);
306 G(x, 0x06, v[0x02], v[0x07], v[0x08], v[0x0D]);
307 G(x, 0x07, v[0x03], v[0x04], v[0x09], v[0x0E]);
308 }
309
310 #undef G
311
312 for (size_t x = 0x00; x < 0x08; x++)
313 state->h[x] ^= (v[x] ^ v[x + 0x08]);
314 }
315
316 static bool __attribute__((warn_unused_result))
317 blake2b_update(struct blake2b_state *const restrict state, const uint8_t *restrict in, size_t inlen)
318 {
319 if (!inlen)
320 return true;
321
322 if (state->f[0x00] != 0x00)
323 return false;
324
325 if ((state->buflen + inlen) > BLAKE2B_BLOCKLEN)
326 {
327 const size_t left = state->buflen;
328 const size_t fill = BLAKE2B_BLOCKLEN - left;
329
330 (void) memcpy(&state->buf[left], in, fill);
331 (void) blake2b_counter_inc(state, BLAKE2B_BLOCKLEN);
332 (void) blake2b_compress(state, state->buf);
333
334 in += fill;
335 inlen -= fill;
336 state->buflen = 0x00;
337
338 while (inlen > BLAKE2B_BLOCKLEN)
339 {
340 (void) blake2b_counter_inc(state, BLAKE2B_BLOCKLEN);
341 (void) blake2b_compress(state, in);
342
343 in += BLAKE2B_BLOCKLEN;
344 inlen -= BLAKE2B_BLOCKLEN;
345 }
346 }
347
348 (void) memcpy(&state->buf[state->buflen], in, inlen);
349 state->buflen += inlen;
350 return true;
351 }
352
353 static inline bool __attribute__((warn_unused_result))
354 blake2b_final(struct blake2b_state *const restrict state, uint8_t *const restrict out)
355 {
356 if (state->f[0x00] != 0x00)
357 return false;
358
359 (void) blake2b_counter_inc(state, state->buflen);
360 (void) blake2b_set_lastblock(state);
361 (void) memset(&state->buf[state->buflen], 0x00, (BLAKE2B_BLOCKLEN - state->buflen));
362 (void) blake2b_compress(state, state->buf);
363
364 uint8_t buffer[BLAKE2B_HASHLEN];
365 (void) memset(buffer, 0x00, sizeof buffer);
366
367 for (size_t x = 0x00; x < 0x08; x++)
368 (void) blake2b_store64(&buffer[(x * sizeof state->h[x])], state->h[x]);
369
370 (void) memcpy(out, buffer, state->outlen);
371 return true;
372 }
373
374 static inline bool __attribute__((warn_unused_result))
375 blake2b_full(const uint8_t *const restrict in, const size_t inlen, uint8_t *const restrict out, const size_t outlen)
376 {
377 struct blake2b_state state;
378 (void) blake2b_init(&state, outlen);
379
380 if (!blake2b_update(&state, in, inlen))
381 return false;
382
383 return blake2b_final(&state, out);
384 }
385
386 static bool __attribute__((warn_unused_result))
387 blake2b_long(const uint8_t *const restrict in, const size_t inlen, uint8_t *restrict out, const size_t outlen)
388 {
389 uint8_t outlen_buf[4] = { 0x00, 0x00, 0x00, 0x00 };
390 struct blake2b_state blake_state;
391
392 if ((sizeof(outlen) > sizeof(uint32_t)) && (outlen > UINT32_MAX))
393 return false;
394
395 (void) blake2b_store32(outlen_buf, (uint32_t) outlen);
396
397 if (outlen <= BLAKE2B_HASHLEN)
398 {
399 (void) blake2b_init(&blake_state, outlen);
400
401 if (!blake2b_update(&blake_state, outlen_buf, sizeof outlen_buf))
402 return false;
403 if (!blake2b_update(&blake_state, in, inlen))
404 return false;
405 if (!blake2b_final(&blake_state, out))
406 return false;
407
408 return true;
409 }
410
411 uint8_t ibuf[BLAKE2B_HASHLEN];
412 uint8_t obuf[BLAKE2B_HASHLEN];
413
414 (void) blake2b_init(&blake_state, BLAKE2B_HASHLEN);
415
416 if (!blake2b_update(&blake_state, outlen_buf, sizeof outlen_buf))
417 return false;
418 if (!blake2b_update(&blake_state, in, inlen))
419 return false;
420 if (!blake2b_final(&blake_state, obuf))
421 return false;
422
423 (void) memcpy(out, obuf, BLAKE2B_HASHLEN_HALF);
424 out += BLAKE2B_HASHLEN_HALF;
425
426 uint32_t remain = (((uint32_t) outlen) - BLAKE2B_HASHLEN_HALF);
427
428 while (remain > BLAKE2B_HASHLEN)
429 {
430 (void) memcpy(ibuf, obuf, BLAKE2B_HASHLEN);
431
432 if (!blake2b_full(ibuf, BLAKE2B_HASHLEN, obuf, BLAKE2B_HASHLEN))
433 return false;
434
435 (void) memcpy(out, obuf, BLAKE2B_HASHLEN_HALF);
436
437 out += BLAKE2B_HASHLEN_HALF;
438 remain -= BLAKE2B_HASHLEN_HALF;
439 }
440
441 (void) memcpy(ibuf, obuf, BLAKE2B_HASHLEN);
442
443 if (!blake2b_full(ibuf, BLAKE2B_HASHLEN, obuf, remain))
444 return false;
445
446 (void) memcpy(out, obuf, remain);
447 return true;
448 }
449
450 static inline void
451 argon2d_copy_block(struct argon2d_block *const restrict dst, const struct argon2d_block *const restrict src)
452 {
453 (void) memcpy(dst->v, src->v, (0x08 * ARGON2_BLK_QWORDS));
454 }
455
456 static inline void
457 argon2d_xor_block(struct argon2d_block *const restrict dst, const struct argon2d_block *const restrict src)
458 {
459 for (size_t x = 0x00; x < ARGON2_BLK_QWORDS; x++)
460 dst->v[x] ^= src->v[x];
461 }
462
463 static inline void
464 argon2d_load_block(struct argon2d_block *const restrict dst, const uint8_t *const restrict input)
465 {
466 for (size_t x = 0x00; x < ARGON2_BLK_QWORDS; x++)
467 dst->v[x] = blake2b_load64(&input[(x * sizeof dst->v[x])]);
468 }
469
470 static inline void
471 argon2d_store_block(uint8_t *const restrict output, const struct argon2d_block *const restrict src)
472 {
473 for (size_t x = 0x00; x < ARGON2_BLK_QWORDS; x++)
474 (void) blake2b_store64(&output[(x * sizeof src->v[x])], src->v[x]);
475 }
476
477 static inline bool __attribute__((warn_unused_result))
478 argon2d_hash_init(struct argon2d_context *const restrict ctx, uint8_t *const restrict bhash)
479 {
480 struct blake2b_state state;
481 uint8_t value[4];
482
483 (void) blake2b_init(&state, ARGON2_PREHASH_LEN);
484 (void) blake2b_store32(value, ATHEME_ARGON2D_LANECNT);
485
486 if (!blake2b_update(&state, value, sizeof value))
487 return false;
488
489 (void) blake2b_store32(value, ATHEME_ARGON2D_HASHLEN);
490
491 if (!blake2b_update(&state, value, sizeof value))
492 return false;
493
494 (void) blake2b_store32(value, ctx->m_cost);
495
496 if (!blake2b_update(&state, value, sizeof value))
497 return false;
498
499 (void) blake2b_store32(value, ctx->t_cost);
500
501 if (!blake2b_update(&state, value, sizeof value))
502 return false;
503
504 (void) blake2b_store32(value, ATHEME_ARGON2D_VERSION);
505
506 if (!blake2b_update(&state, value, sizeof value))
507 return false;
508
509 (void) blake2b_store32(value, ATHEME_ARGON2D_TYPEVAL);
510
511 if (!blake2b_update(&state, value, sizeof value))
512 return false;
513
514 (void) blake2b_store32(value, ctx->passlen);
515
516 if (!blake2b_update(&state, value, sizeof value))
517 return false;
518
519 if (!blake2b_update(&state, ctx->pass, ctx->passlen))
520 return false;
521
522 (void) blake2b_store32(value, ATHEME_ARGON2D_SALTLEN);
523
524 if (!blake2b_update(&state, value, sizeof value))
525 return false;
526
527 if (!blake2b_update(&state, ctx->salt, ATHEME_ARGON2D_SALTLEN))
528 return false;
529
530 (void) blake2b_store32(value, ATHEME_ARGON2D_PRIVLEN);
531
532 if (!blake2b_update(&state, value, sizeof value))
533 return false;
534
535 (void) blake2b_store32(value, ATHEME_ARGON2D_AUTHLEN);
536
537 if (!blake2b_update(&state, value, sizeof value))
538 return false;
539
540 return blake2b_final(&state, bhash);
541 }
542
543 static uint32_t
544 argon2d_idx(const struct argon2d_context *const restrict ctx, const uint32_t pass, const uint8_t slice,
545 const uint64_t rand_p)
546 {
547 uint32_t ra_size = (ctx->index - 0x01);
548
549 if (pass)
550 ra_size += (ctx->lane_len - ctx->seg_len);
551 else if (slice)
552 ra_size += (slice * ctx->seg_len);
553
554 uint64_t relative_pos = (uint64_t)(rand_p & 0xFFFFFFFF);
555 relative_pos = ((relative_pos * relative_pos) >> 0x20U);
556 relative_pos = (ra_size - 0x01 - ((ra_size * relative_pos) >> 0x20U));
557
558 uint32_t start_pos = 0x00;
559 if (pass && slice != (ARGON2_SYNC_POINTS - 0x01))
560 start_pos = ((slice + 0x01) * ctx->seg_len);
561
562 return ((start_pos + ((uint32_t) relative_pos)) % ctx->lane_len);
563 }
564
565 static void
566 argon2d_fill_block(const struct argon2d_block *const prev, const struct argon2d_block *const ref,
567 struct argon2d_block *const next, const uint32_t pass)
568 {
569 struct argon2d_block block_r;
570 struct argon2d_block block_x;
571
572 (void) argon2d_copy_block(&block_r, ref);
573 (void) argon2d_xor_block(&block_r, prev);
574 (void) argon2d_copy_block(&block_x, &block_r);
575
576 if (pass != 0x00)
577 (void) argon2d_xor_block(&block_x, next);
578
579 uint64_t *const v = block_r.v;
580
581 #define F(a, b, c, d) do { \
582 (a) = blake2b_fBlaMka((a), (b)); \
583 (d) = blake2b_rotr64(((d) ^ (a)), 0x20); \
584 (c) = blake2b_fBlaMka((c), (d)); \
585 (b) = blake2b_rotr64(((b) ^ (c)), 0x18); \
586 (a) = blake2b_fBlaMka((a), (b)); \
587 (d) = blake2b_rotr64(((d) ^ (a)), 0x10); \
588 (c) = blake2b_fBlaMka((c), (d)); \
589 (b) = blake2b_rotr64(((b) ^ (c)), 0x3F); \
590 } while (0)
591
592 for (size_t x = 0x00; x < 0x08; x++)
593 {
594 F(v[(0x10 * x) + 0x00], v[(0x10 * x) + 0x04], v[(0x10 * x) + 0x08], v[(0x10 * x) + 0x0C]);
595 F(v[(0x10 * x) + 0x01], v[(0x10 * x) + 0x05], v[(0x10 * x) + 0x09], v[(0x10 * x) + 0x0D]);
596 F(v[(0x10 * x) + 0x02], v[(0x10 * x) + 0x06], v[(0x10 * x) + 0x0A], v[(0x10 * x) + 0x0E]);
597 F(v[(0x10 * x) + 0x03], v[(0x10 * x) + 0x07], v[(0x10 * x) + 0x0B], v[(0x10 * x) + 0x0F]);
598 F(v[(0x10 * x) + 0x00], v[(0x10 * x) + 0x05], v[(0x10 * x) + 0x0A], v[(0x10 * x) + 0x0F]);
599 F(v[(0x10 * x) + 0x01], v[(0x10 * x) + 0x06], v[(0x10 * x) + 0x0B], v[(0x10 * x) + 0x0C]);
600 F(v[(0x10 * x) + 0x02], v[(0x10 * x) + 0x07], v[(0x10 * x) + 0x08], v[(0x10 * x) + 0x0D]);
601 F(v[(0x10 * x) + 0x03], v[(0x10 * x) + 0x04], v[(0x10 * x) + 0x09], v[(0x10 * x) + 0x0E]);
602 }
603 for (size_t x = 0x00; x < 0x08; x++)
604 {
605 F(v[(0x02 * x) + 0x00], v[(0x02 * x) + 0x20], v[(0x02 * x) + 0x40], v[(0x02 * x) + 0x60]);
606 F(v[(0x02 * x) + 0x01], v[(0x02 * x) + 0x21], v[(0x02 * x) + 0x41], v[(0x02 * x) + 0x61]);
607 F(v[(0x02 * x) + 0x10], v[(0x02 * x) + 0x30], v[(0x02 * x) + 0x50], v[(0x02 * x) + 0x70]);
608 F(v[(0x02 * x) + 0x11], v[(0x02 * x) + 0x31], v[(0x02 * x) + 0x51], v[(0x02 * x) + 0x71]);
609 F(v[(0x02 * x) + 0x00], v[(0x02 * x) + 0x21], v[(0x02 * x) + 0x50], v[(0x02 * x) + 0x71]);
610 F(v[(0x02 * x) + 0x01], v[(0x02 * x) + 0x30], v[(0x02 * x) + 0x51], v[(0x02 * x) + 0x60]);
611 F(v[(0x02 * x) + 0x10], v[(0x02 * x) + 0x31], v[(0x02 * x) + 0x40], v[(0x02 * x) + 0x61]);
612 F(v[(0x02 * x) + 0x11], v[(0x02 * x) + 0x20], v[(0x02 * x) + 0x41], v[(0x02 * x) + 0x70]);
613 }
614
615 #undef F
616
617 (void) argon2d_copy_block(next, &block_x);
618 (void) argon2d_xor_block(next, &block_r);
619 }
620
621 static inline void
622 argon2d_segment_fill(struct argon2d_context *const restrict ctx, const uint32_t pass, const uint8_t slice)
623 {
624 const uint32_t start_idx = ((!pass && !slice) ? 0x02 : 0x00);
625 uint32_t cur_off = (slice * ctx->seg_len) + start_idx;
626 uint32_t prv_off = (cur_off - 0x01);
627
628 if ((cur_off % ctx->lane_len) == 0x00)
629 prv_off += ctx->lane_len;
630
631 for (uint32_t i = start_idx; i < ctx->seg_len; i++, cur_off++, prv_off++)
632 {
633 if ((cur_off % ctx->lane_len) == 0x01)
634 prv_off = (cur_off - 0x01);
635
636 ctx->index = i;
637
638 const uint64_t rand_p = argon2d_mempool[prv_off].v[0x00];
639 const uint32_t ref_idx = argon2d_idx(ctx, pass, slice, rand_p);
640
641 const struct argon2d_block *const prv = &argon2d_mempool[prv_off];
642 const struct argon2d_block *const ref = &argon2d_mempool[ref_idx];
643 struct argon2d_block *const cur = &argon2d_mempool[cur_off];
644
645 (void) argon2d_fill_block(prv, ref, cur, pass);
646 }
647 }
648
649 static bool __attribute__((warn_unused_result))
650 argon2d_hash_raw(struct argon2d_context *const restrict ctx)
651 {
652 ctx->seg_len = (ctx->m_cost / ARGON2_SYNC_POINTS);
653
654 const uint32_t mem_blocks = (ctx->seg_len * ARGON2_SYNC_POINTS);
655
656 ctx->lane_len = mem_blocks;
657
658 if (!atheme_argon2d_mempool_realloc(mem_blocks))
659 return false;
660
661 uint8_t bhash_init[ARGON2_PRESEED_LEN];
662
663 if (!argon2d_hash_init(ctx, bhash_init))
664 return false;
665
666 (void) blake2b_store32(bhash_init + ARGON2_PREHASH_LEN, 0x00);
667 (void) blake2b_store32(bhash_init + ARGON2_PREHASH_LEN + 0x04, 0x00);
668
669 uint8_t bhash_bytes[ARGON2_BLKSZ];
670
671 if (!blake2b_long(bhash_init, ARGON2_PRESEED_LEN, bhash_bytes, ARGON2_BLKSZ))
672 return false;
673
674 (void) argon2d_load_block(&argon2d_mempool[0x00], bhash_bytes);
675 (void) blake2b_store32(bhash_init + ARGON2_PREHASH_LEN, 0x01);
676
677 if (!blake2b_long(bhash_init, ARGON2_PRESEED_LEN, bhash_bytes, ARGON2_BLKSZ))
678 return false;
679
680 (void) argon2d_load_block(&argon2d_mempool[0x01], bhash_bytes);
681
682 for (uint32_t pass = 0x00; pass < ctx->t_cost; pass++)
683 {
684 for (uint8_t slice = 0x00; slice < ARGON2_SYNC_POINTS; slice++)
685 {
686 ctx->index = 0x00;
687 (void) argon2d_segment_fill(ctx, pass, slice);
688 }
689 }
690
691 struct argon2d_block bhash_final;
692 (void) argon2d_copy_block(&bhash_final, &argon2d_mempool[ctx->lane_len - 0x01]);
693 (void) argon2d_store_block(bhash_bytes, &bhash_final);
694
695 return blake2b_long(bhash_bytes, ARGON2_BLKSZ, ctx->hash, ATHEME_ARGON2D_HASHLEN);
696 }
697
698 #define EQ(x, y) ((((0x00 - (((unsigned) (x)) ^ ((unsigned) (y)))) >> 0x08U) & 0xFF) ^ 0xFF)
699 #define GT(x, y) (((((unsigned) (y)) - ((unsigned) (x))) >> 0x08U) & 0xFF)
700 #define GE(x, y) (GT(y, x) ^ 0xFF)
701 #define LE(x, y) (GE(y, x))
702
703 static inline uint8_t
704 argon2d_dec_b64_char(const char v)
705 {
706 const uint8_t c = (const uint8_t) v;
707 const uint8_t x = (GE(c, 0x41) & LE(c, 0x5A) & (c - 0x41)) | (GE(c, 0x61) & LE(c, 0x7A) & \
708 (c - 0x47)) | (GE(c, 0x30) & LE(c, 0x39) & (c + 0x04)) | (EQ(c, 0x2B) & \
709 0x3E) | (EQ(c, 0x2F) & 0x3F);
710
711 return (x | (EQ(x, 0x00) & (EQ(c, 0x41) ^ 0xFF)));
712 }
713
714 #undef EQ
715 #undef GT
716 #undef GE
717 #undef LE
718
719 static inline size_t
720 argon2d_dec_b64(const char *restrict src, uint8_t *restrict dst, const size_t dst_len)
721 {
722 size_t written = 0x00;
723 size_t acc_len = 0x00;
724 uint64_t acc = 0x00;
725 bool exiting = false;
726
727 for (;;)
728 {
729 while (acc_len >= 0x08)
730 {
731 if (written++ >= dst_len)
732 return 0;
733
734 acc_len -= 0x08;
735 *dst++ = ((uint8_t)((acc >> acc_len) & 0xFF));
736 }
737 if (exiting)
738 {
739 if (acc_len > 0x04 || (acc & ((0x01U << acc_len) - 0x01)) != 0x00)
740 return 0;
741
742 return written;
743 }
744 while (acc_len < 0x3C)
745 {
746 uint8_t d = argon2d_dec_b64_char(*src++);
747
748 if (d == 0xFF)
749 {
750 exiting = true;
751 break;
752 }
753
754 acc = ((acc << 0x06U) | ((uint64_t) d));
755 acc_len += 0x06;
756 }
757 }
758 }
759
760 static inline void
761 argon2d_enc_b64(const uint8_t *restrict src, size_t src_len, char *restrict dst)
762 {
763 static const char base64_etab[] = ATHEME_ARGON2D_LOADB64;
764
765 while (src_len > 0x02)
766 {
767 *dst++ = base64_etab[(size_t)(src[0x00] >> 0x02U)];
768 *dst++ = base64_etab[(size_t)(((src[0x00] & 0x03) << 0x04U) + (src[0x01] >> 0x04U))];
769 *dst++ = base64_etab[(size_t)(((src[0x01] & 0x0F) << 0x02U) + (src[0x02] >> 0x06U))];
770 *dst++ = base64_etab[(size_t)(src[0x02] & 0x3FU)];
771
772 src += 0x03;
773 src_len -= 0x03;
774 }
775 if (src_len > 0x00)
776 {
777 *dst++ = base64_etab[(size_t)(src[0x00] >> 0x02U)];
778
779 if (src_len > 0x01)
780 {
781 *dst++ = base64_etab[(size_t)(((src[0x00] & 0x03) << 0x04U) + (src[0x01] >> 0x04U))];
782 *dst++ = base64_etab[(size_t)((src[0x01] & 0x0F) << 0x02U)];
783 }
784 else
785 *dst++ = base64_etab[(size_t)((src[0x00] & 0x03) << 0x04U)];
786 }
787
788 *dst = 0x00;
789 }
790
791 /*
792 * The default memory and time cost variables
793 * These can be adjusted in the configuration file
794 */
795 static unsigned int atheme_argon2d_mcost = ARGON2D_MEMCOST_DEF;
796 static unsigned int atheme_argon2d_tcost = ARGON2D_TIMECOST_DEF;
797
798 static const char *
799 atheme_argon2d_salt(void)
800 {
801 const uint32_t m_cost = 0x01U << (uint32_t)atheme_argon2d_mcost;
802 const uint32_t t_cost = (uint32_t)atheme_argon2d_tcost;
803
804 uint8_t salt[ATHEME_ARGON2D_SALTLEN];
805 (void) arc4random_buf(salt, sizeof salt);
806
807 char salt_b64[0x2000];
808 (void) argon2d_enc_b64(salt, sizeof salt, salt_b64);
809
810 static char res[PASSLEN];
811 if (snprintf(res, PASSLEN, ATHEME_ARGON2D_SAVESALT, m_cost, t_cost, salt_b64) >= PASSLEN)
812 return NULL;
813
814 return res;
815 }
816
817 static const char *
818 atheme_argon2d_crypt(const char *const restrict pass, const char *const restrict encoded)
819 {
820 struct argon2d_context ctx;
821 (void) memset(&ctx, 0x00, sizeof ctx);
822
823 char salt_b64[0x2000];
824 if (sscanf(encoded, ATHEME_ARGON2D_LOADSALT, &ctx.m_cost, &ctx.t_cost, salt_b64) != 3)
825 return NULL;
826
827 if ((ctx.m_cost > (0x01U << ARGON2D_MEMCOST_MAX)) || (ctx.t_cost > ARGON2D_TIMECOST_MAX))
828 return NULL;
829
830 if (argon2d_dec_b64(salt_b64, ctx.salt, sizeof ctx.salt) != sizeof ctx.salt)
831 return NULL;
832
833 ctx.pass = (const uint8_t *) pass;
834 ctx.passlen = (uint32_t) strlen(pass);
835
836 if (!argon2d_hash_raw(&ctx))
837 return NULL;
838
839 char hash_b64[0x2000];
840 (void) argon2d_enc_b64(ctx.hash, sizeof ctx.hash, hash_b64);
841
842 static char res[PASSLEN];
843 if (snprintf(res, PASSLEN, ATHEME_ARGON2D_SAVEHASH, ctx.m_cost, ctx.t_cost, salt_b64, hash_b64) >= PASSLEN)
844 return NULL;
845
846 return res;
847 }
848
849 static bool
850 atheme_argon2d_upgrade(const char *const restrict encoded)
851 {
852 uint32_t m_cost;
853 uint32_t t_cost;
854 char salt_b64[0x2000];
855 if (sscanf(encoded, ATHEME_ARGON2D_LOADSALT, &m_cost, &t_cost, salt_b64) != 3)
856 return false;
857
858 uint8_t salt[ATHEME_ARGON2D_SALTLEN];
859 if (argon2d_dec_b64(salt_b64, salt, sizeof salt) != sizeof salt)
860 return false;
861
862 if (m_cost != (0x01U << atheme_argon2d_mcost))
863 return true;
864
865 if (t_cost != atheme_argon2d_tcost)
866 return true;
867
868 return false;
869 }
870
871 static crypt_impl_t atheme_argon2d_crypt_impl = {
872
873 .id = "argon2d",
874 .crypt = &atheme_argon2d_crypt,
875 .salt = &atheme_argon2d_salt,
876 .needs_param_upgrade = &atheme_argon2d_upgrade,
877 };
878
879 static mowgli_list_t atheme_argon2d_conf_table;
880
881 static void
882 atheme_argon2d_modinit(module_t __attribute__((unused)) *const restrict m)
883 {
884 (void) crypt_register(&atheme_argon2d_crypt_impl);
885
886 (void) add_subblock_top_conf("ARGON2D", &atheme_argon2d_conf_table);
887
888 (void) add_uint_conf_item("MEMORY", &atheme_argon2d_conf_table, 0, &atheme_argon2d_mcost,
889 ARGON2D_MEMCOST_MIN, ARGON2D_MEMCOST_MAX, ARGON2D_MEMCOST_DEF);
890
891 (void) add_uint_conf_item("TIME", &atheme_argon2d_conf_table, 0, &atheme_argon2d_tcost,
892 ARGON2D_TIMECOST_MIN, ARGON2D_TIMECOST_MAX, ARGON2D_TIMECOST_DEF);
893 }
894
895 static void
896 atheme_argon2d_moddeinit(const module_unload_intent_t __attribute__((unused)) intent)
897 {
898 (void) del_conf_item("TIME", &atheme_argon2d_conf_table);
899 (void) del_conf_item("MEMORY", &atheme_argon2d_conf_table);
900 (void) del_top_conf("ARGON2D");
901
902 (void) crypt_unregister(&atheme_argon2d_crypt_impl);
903
904 (void) free(argon2d_mempool);
905 }
906
907 DECLARE_MODULE_V1("crypto/argon2d", false, atheme_argon2d_modinit, atheme_argon2d_moddeinit,
908 PACKAGE_VERSION, "Aaron M. D. Jones <[email protected]>");