yescrypt-opt.c

filter: Statementstmt 67.5% 344 / 509 Branchbran 52.9% 143 / 270 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 62.5% 487 / 779 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« yescrypt-common.c ↑ Summary sha256.c »
1 /*-
2 * Copyright 2009 Colin Percival
3 * Copyright 2012-2018 Alexander Peslyak
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * This file was originally written by Colin Percival as part of the Tarsnap
28 * online backup system.
29 */
30  
31 /*
32 * AVX and especially XOP speed up Salsa20 a lot, but this mostly matters for
33 * classic scrypt and for YESCRYPT_WORM (which use 8 rounds of Salsa20 per
34 * sub-block), and much less so for YESCRYPT_RW (which uses 2 rounds of Salsa20
35 * per block except during pwxform S-box initialization).
36 */
37 /* cpx: the build notes below are noise for CPAN installs, and #warning is an
38 * error on compilers that lack it, so they are compiled only on request. */
39 #ifdef CPX_YESCRYPT_BUILD_NOTES
40 #ifdef __XOP__
41 #warning "Note: XOP is enabled. That's great."
42 #elif defined(__AVX__)
43 #warning "Note: AVX is enabled, which is great for classic scrypt and YESCRYPT_WORM, but is sometimes slightly slower than plain SSE2 for YESCRYPT_RW"
44 #elif defined(__SSE2__)
45 #warning "Note: AVX and XOP are not enabled, which is great for YESCRYPT_RW, but they would substantially improve performance at classic scrypt and YESCRYPT_WORM"
46 #elif defined(__x86_64__) || defined(__i386__)
47 #warning "SSE2 not enabled. Expect poor performance."
48 #else
49 #warning "Note: building generic code for non-x86. That's OK."
50 #endif
51 #endif /* cpx: CPX_YESCRYPT_BUILD_NOTES */
52  
53 /*
54 * The SSE4 code version has fewer instructions than the generic SSE2 version,
55 * but all of the instructions are SIMD, thereby wasting the scalar execution
56 * units. Thus, the generic SSE2 version below actually runs faster on some
57 * CPUs due to its balanced mix of SIMD and scalar instructions.
58 */
59 #undef USE_SSE4_FOR_32BIT
60  
61 /* cpx: rename external symbols (see cpx_yescrypt_names.h) */
62 #include "cpx_yescrypt_names.h"
63  
64 #ifdef __SSE2__
65 /*
66 * GCC before 4.9 would by default unnecessarily use store/load (without
67 * SSE4.1) or (V)PEXTR (with SSE4.1 or AVX) instead of simply (V)MOV.
68 * This was tracked as GCC bug 54349.
69 * "-mtune=corei7" works around this, but is only supported for GCC 4.6+.
70 * We use inline asm for pre-4.6 GCC, further down this file.
71 */
72 #if __GNUC__ == 4 && __GNUC_MINOR__ >= 6 && __GNUC_MINOR__ < 9 && \
73 !defined(__clang__) && !defined(__ICC)
74 #pragma GCC target ("tune=corei7")
75 #endif
76 #include <emmintrin.h>
77 #ifdef __XOP__
78 #include <x86intrin.h>
79 #endif
80 #elif defined(__SSE__)
81 #include <xmmintrin.h>
82 #endif
83  
84 #include <errno.h>
85 #include <stdint.h>
86 #include <stdlib.h>
87 #include <string.h>
88  
89 #include "insecure_memzero.h"
90 #include "sha256.h"
91 #include "sysendian.h"
92  
93 #define YESCRYPT_INTERNAL
94 #include "yescrypt.h"
95  
96 #include "yescrypt-platform.c"
97  
98 #if __STDC_VERSION__ >= 199901L
99 /* Have restrict */
100 #elif defined(__GNUC__)
101 #define restrict __restrict
102 #else
103 #define restrict
104 #endif
105  
106 #ifdef __GNUC__
107 #define unlikely(exp) __builtin_expect(exp, 0)
108 #else
109 #define unlikely(exp) (exp)
110 #endif
111  
112 #ifdef __SSE__
113 #define PREFETCH(x, hint) _mm_prefetch((const char *)(x), (hint));
114 #else
115 #undef PREFETCH
116 #endif
117  
118 typedef union {
119 uint32_t w[16];
120 uint64_t d[8];
121 #ifdef __SSE2__
122 __m128i q[4];
123 #endif
124 } salsa20_blk_t;
125  
126 2884 static inline void salsa20_simd_shuffle(const salsa20_blk_t *Bin,
127 salsa20_blk_t *Bout)
128 {
129 #define COMBINE(out, in1, in2) \
130 Bout->d[out] = Bin->w[in1 * 2] | ((uint64_t)Bin->w[in2 * 2 + 1] << 32);
131 2884 COMBINE(0, 0, 2)
132 2884 COMBINE(1, 5, 7)
133 2884 COMBINE(2, 2, 4)
134 2884 COMBINE(3, 7, 1)
135 2884 COMBINE(4, 4, 6)
136 2884 COMBINE(5, 1, 3)
137 2884 COMBINE(6, 6, 0)
138 2884 COMBINE(7, 3, 5)
139 #undef COMBINE
140 2884 }
141  
142 2884 static inline void salsa20_simd_unshuffle(const salsa20_blk_t *Bin,
143 salsa20_blk_t *Bout)
144 {
145 #define UNCOMBINE(out, in1, in2) \
146 Bout->w[out * 2] = Bin->d[in1]; \
147 Bout->w[out * 2 + 1] = Bin->d[in2] >> 32;
148 2884 UNCOMBINE(0, 0, 6)
149 2884 UNCOMBINE(1, 5, 3)
150 2884 UNCOMBINE(2, 2, 0)
151 2884 UNCOMBINE(3, 7, 5)
152 2884 UNCOMBINE(4, 4, 2)
153 2884 UNCOMBINE(5, 1, 7)
154 2884 UNCOMBINE(6, 6, 4)
155 2884 UNCOMBINE(7, 3, 1)
156 #undef UNCOMBINE
157 2884 }
158  
159 #ifdef __SSE2__
160 #define DECL_X \
161 __m128i X0, X1, X2, X3;
162 #define DECL_Y \
163 __m128i Y0, Y1, Y2, Y3;
164 #define READ_X(in) \
165 X0 = (in).q[0]; X1 = (in).q[1]; X2 = (in).q[2]; X3 = (in).q[3];
166 #define WRITE_X(out) \
167 (out).q[0] = X0; (out).q[1] = X1; (out).q[2] = X2; (out).q[3] = X3;
168  
169 #ifdef __XOP__
170 #define ARX(out, in1, in2, s) \
171 out = _mm_xor_si128(out, _mm_roti_epi32(_mm_add_epi32(in1, in2), s));
172 #else
173 #define ARX(out, in1, in2, s) { \
174 __m128i tmp = _mm_add_epi32(in1, in2); \
175 out = _mm_xor_si128(out, _mm_slli_epi32(tmp, s)); \
176 out = _mm_xor_si128(out, _mm_srli_epi32(tmp, 32 - s)); \
177 }
178 #endif
179  
180 #define SALSA20_2ROUNDS \
181 /* Operate on "columns" */ \
182 ARX(X1, X0, X3, 7) \
183 ARX(X2, X1, X0, 9) \
184 ARX(X3, X2, X1, 13) \
185 ARX(X0, X3, X2, 18) \
186 /* Rearrange data */ \
187 X1 = _mm_shuffle_epi32(X1, 0x93); \
188 X2 = _mm_shuffle_epi32(X2, 0x4E); \
189 X3 = _mm_shuffle_epi32(X3, 0x39); \
190 /* Operate on "rows" */ \
191 ARX(X3, X0, X1, 7) \
192 ARX(X2, X3, X0, 9) \
193 ARX(X1, X2, X3, 13) \
194 ARX(X0, X1, X2, 18) \
195 /* Rearrange data */ \
196 X1 = _mm_shuffle_epi32(X1, 0x39); \
197 X2 = _mm_shuffle_epi32(X2, 0x4E); \
198 X3 = _mm_shuffle_epi32(X3, 0x93);
199  
200 /**
201 * Apply the Salsa20 core to the block provided in (X0 ... X3).
202 */
203 #define SALSA20_wrapper(out, rounds) { \
204 __m128i Z0 = X0, Z1 = X1, Z2 = X2, Z3 = X3; \
205 rounds \
206 (out).q[0] = X0 = _mm_add_epi32(X0, Z0); \
207 (out).q[1] = X1 = _mm_add_epi32(X1, Z1); \
208 (out).q[2] = X2 = _mm_add_epi32(X2, Z2); \
209 (out).q[3] = X3 = _mm_add_epi32(X3, Z3); \
210 }
211  
212 /**
213 * Apply the Salsa20/2 core to the block provided in X.
214 */
215 #define SALSA20_2(out) \
216 SALSA20_wrapper(out, SALSA20_2ROUNDS)
217  
218 #define SALSA20_8ROUNDS \
219 SALSA20_2ROUNDS SALSA20_2ROUNDS SALSA20_2ROUNDS SALSA20_2ROUNDS
220  
221 #define XOR_X(in) \
222 X0 = _mm_xor_si128(X0, (in).q[0]); \
223 X1 = _mm_xor_si128(X1, (in).q[1]); \
224 X2 = _mm_xor_si128(X2, (in).q[2]); \
225 X3 = _mm_xor_si128(X3, (in).q[3]);
226  
227 #define XOR_X_2(in1, in2) \
228 X0 = _mm_xor_si128((in1).q[0], (in2).q[0]); \
229 X1 = _mm_xor_si128((in1).q[1], (in2).q[1]); \
230 X2 = _mm_xor_si128((in1).q[2], (in2).q[2]); \
231 X3 = _mm_xor_si128((in1).q[3], (in2).q[3]);
232  
233 #define XOR_X_WRITE_XOR_Y_2(out, in) \
234 (out).q[0] = Y0 = _mm_xor_si128((out).q[0], (in).q[0]); \
235 (out).q[1] = Y1 = _mm_xor_si128((out).q[1], (in).q[1]); \
236 (out).q[2] = Y2 = _mm_xor_si128((out).q[2], (in).q[2]); \
237 (out).q[3] = Y3 = _mm_xor_si128((out).q[3], (in).q[3]); \
238 X0 = _mm_xor_si128(X0, Y0); \
239 X1 = _mm_xor_si128(X1, Y1); \
240 X2 = _mm_xor_si128(X2, Y2); \
241 X3 = _mm_xor_si128(X3, Y3);
242  
243 /**
244 * Apply the Salsa20/8 core to the block provided in X ^ in.
245 */
246 #define SALSA20_8_XOR_MEM(in, out) \
247 XOR_X(in) \
248 SALSA20_wrapper(out, SALSA20_8ROUNDS)
249  
250 #define INTEGERIFY _mm_cvtsi128_si32(X0)
251  
252 #else /* !defined(__SSE2__) */
253  
254 #define DECL_X \
255 salsa20_blk_t X;
256 #define DECL_Y \
257 salsa20_blk_t Y;
258  
259 #define COPY(out, in) \
260 (out).d[0] = (in).d[0]; \
261 (out).d[1] = (in).d[1]; \
262 (out).d[2] = (in).d[2]; \
263 (out).d[3] = (in).d[3]; \
264 (out).d[4] = (in).d[4]; \
265 (out).d[5] = (in).d[5]; \
266 (out).d[6] = (in).d[6]; \
267 (out).d[7] = (in).d[7];
268  
269 #define READ_X(in) COPY(X, in)
270 #define WRITE_X(out) COPY(out, X)
271  
272 /**
273 * salsa20(B):
274 * Apply the Salsa20 core to the provided block.
275 */
276 static inline void salsa20(salsa20_blk_t *restrict B,
277 salsa20_blk_t *restrict Bout, uint32_t doublerounds)
278 {
279 salsa20_blk_t X;
280 #define x X.w
281  
282 salsa20_simd_unshuffle(B, &X);
283  
284 do {
285 #define R(a,b) (((a) << (b)) | ((a) >> (32 - (b))))
286 /* Operate on columns */
287 x[ 4] ^= R(x[ 0]+x[12], 7); x[ 8] ^= R(x[ 4]+x[ 0], 9);
288 x[12] ^= R(x[ 8]+x[ 4],13); x[ 0] ^= R(x[12]+x[ 8],18);
289  
290 x[ 9] ^= R(x[ 5]+x[ 1], 7); x[13] ^= R(x[ 9]+x[ 5], 9);
291 x[ 1] ^= R(x[13]+x[ 9],13); x[ 5] ^= R(x[ 1]+x[13],18);
292  
293 x[14] ^= R(x[10]+x[ 6], 7); x[ 2] ^= R(x[14]+x[10], 9);
294 x[ 6] ^= R(x[ 2]+x[14],13); x[10] ^= R(x[ 6]+x[ 2],18);
295  
296 x[ 3] ^= R(x[15]+x[11], 7); x[ 7] ^= R(x[ 3]+x[15], 9);
297 x[11] ^= R(x[ 7]+x[ 3],13); x[15] ^= R(x[11]+x[ 7],18);
298  
299 /* Operate on rows */
300 x[ 1] ^= R(x[ 0]+x[ 3], 7); x[ 2] ^= R(x[ 1]+x[ 0], 9);
301 x[ 3] ^= R(x[ 2]+x[ 1],13); x[ 0] ^= R(x[ 3]+x[ 2],18);
302  
303 x[ 6] ^= R(x[ 5]+x[ 4], 7); x[ 7] ^= R(x[ 6]+x[ 5], 9);
304 x[ 4] ^= R(x[ 7]+x[ 6],13); x[ 5] ^= R(x[ 4]+x[ 7],18);
305  
306 x[11] ^= R(x[10]+x[ 9], 7); x[ 8] ^= R(x[11]+x[10], 9);
307 x[ 9] ^= R(x[ 8]+x[11],13); x[10] ^= R(x[ 9]+x[ 8],18);
308  
309 x[12] ^= R(x[15]+x[14], 7); x[13] ^= R(x[12]+x[15], 9);
310 x[14] ^= R(x[13]+x[12],13); x[15] ^= R(x[14]+x[13],18);
311 #undef R
312 } while (--doublerounds);
313 #undef x
314  
315 {
316 uint32_t i;
317 salsa20_simd_shuffle(&X, Bout);
318 for (i = 0; i < 16; i += 4) {
319 B->w[i] = Bout->w[i] += B->w[i];
320 B->w[i + 1] = Bout->w[i + 1] += B->w[i + 1];
321 B->w[i + 2] = Bout->w[i + 2] += B->w[i + 2];
322 B->w[i + 3] = Bout->w[i + 3] += B->w[i + 3];
323 }
324 }
325  
326 #if 0
327 /* Too expensive */
328 insecure_memzero(&X, sizeof(X));
329 #endif
330 }
331  
332 /**
333 * Apply the Salsa20/2 core to the block provided in X.
334 */
335 #define SALSA20_2(out) \
336 salsa20(&X, &out, 1);
337  
338 #define XOR(out, in1, in2) \
339 (out).d[0] = (in1).d[0] ^ (in2).d[0]; \
340 (out).d[1] = (in1).d[1] ^ (in2).d[1]; \
341 (out).d[2] = (in1).d[2] ^ (in2).d[2]; \
342 (out).d[3] = (in1).d[3] ^ (in2).d[3]; \
343 (out).d[4] = (in1).d[4] ^ (in2).d[4]; \
344 (out).d[5] = (in1).d[5] ^ (in2).d[5]; \
345 (out).d[6] = (in1).d[6] ^ (in2).d[6]; \
346 (out).d[7] = (in1).d[7] ^ (in2).d[7];
347  
348 #define XOR_X(in) XOR(X, X, in)
349 #define XOR_X_2(in1, in2) XOR(X, in1, in2)
350 #define XOR_X_WRITE_XOR_Y_2(out, in) \
351 XOR(Y, out, in) \
352 COPY(out, Y) \
353 XOR(X, X, Y)
354  
355 /**
356 * Apply the Salsa20/8 core to the block provided in X ^ in.
357 */
358 #define SALSA20_8_XOR_MEM(in, out) \
359 XOR_X(in); \
360 salsa20(&X, &out, 4);
361  
362 #define INTEGERIFY (uint32_t)X.d[0]
363 #endif
364  
365 /**
366 * blockmix_salsa8(Bin, Bout, r):
367 * Compute Bout = BlockMix_{salsa20/8, r}(Bin). The input Bin must be 128r
368 * bytes in length; the output Bout must also be the same size.
369 */
370 2624 static void blockmix_salsa8(const salsa20_blk_t *restrict Bin,
371 salsa20_blk_t *restrict Bout, size_t r)
372 {
373 size_t i;
374 DECL_X
375  
376 2624 READ_X(Bin[r * 2 - 1])
377 21120 ▶ for (i = 0; i < r; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < r; i++) { 18496 2624 21120
378 3107328 SALSA20_8_XOR_MEM(Bin[i * 2], Bout[i])
379 3125824 SALSA20_8_XOR_MEM(Bin[i * 2 + 1], Bout[r + i])
380 }
381 2624 }
382  
383 512 static uint32_t blockmix_salsa8_xor(const salsa20_blk_t *restrict Bin1,
384 const salsa20_blk_t *restrict Bin2, salsa20_blk_t *restrict Bout,
385 size_t r)
386 {
387 size_t i;
388 DECL_X
389  
390 #ifdef PREFETCH
391 512 PREFETCH(&Bin2[r * 2 - 1], _MM_HINT_T0)
392 16384 ▶ for (i = 0; i < r - 1; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < r - 1; i++) { 15872 512 16384
393 15872 PREFETCH(&Bin2[i * 2], _MM_HINT_T0)
394 15872 PREFETCH(&Bin2[i * 2 + 1], _MM_HINT_T0)
395 }
396 512 PREFETCH(&Bin2[i * 2], _MM_HINT_T0)
397 #endif
398  
399 2048 XOR_X_2(Bin1[r * 2 - 1], Bin2[r * 2 - 1])
400 16896 ▶ for (i = 0; i < r; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < r; i++) { 16384 512 16896
401 65536 XOR_X(Bin1[i * 2])
402 2752512 SALSA20_8_XOR_MEM(Bin2[i * 2], Bout[i])
403 65536 XOR_X(Bin1[i * 2 + 1])
404 2768896 SALSA20_8_XOR_MEM(Bin2[i * 2 + 1], Bout[r + i])
405 }
406  
407 512 return INTEGERIFY;
408 }
409  
410 /* This is tunable */
411 #define Swidth 8
412  
413 /* Not tunable in this implementation, hard-coded in a few places */
414 #define PWXsimple 2
415 #define PWXgather 4
416  
417 /* Derived values. Not tunable except via Swidth above. */
418 #define PWXbytes (PWXgather * PWXsimple * 8)
419 #define Sbytes (3 * (1 << Swidth) * PWXsimple * 8)
420 #define Smask (((1 << Swidth) - 1) * PWXsimple * 8)
421 #define Smask2 (((uint64_t)Smask << 32) | Smask)
422  
423 #define DECL_SMASK2REG /* empty */
424 #define FORCE_REGALLOC_3 /* empty */
425 #define MAYBE_MEMORY_BARRIER /* empty */
426  
427 #ifdef __SSE2__
428 /*
429 * (V)PSRLDQ and (V)PSHUFD have higher throughput than (V)PSRLQ on some CPUs
430 * starting with Sandy Bridge. Additionally, PSHUFD uses separate source and
431 * destination registers, whereas the shifts would require an extra move
432 * instruction for our code when building without AVX. Unfortunately, PSHUFD
433 * is much slower on Conroe (4 cycles latency vs. 1 cycle latency for PSRLQ)
434 * and somewhat slower on some non-Intel CPUs (luckily not including AMD
435 * Bulldozer and Piledriver).
436 */
437 #ifdef __AVX__
438 #define HI32(X) \
439 _mm_srli_si128((X), 4)
440 #elif 1 /* As an option, check for __SSE4_1__ here not to hurt Conroe */
441 #define HI32(X) \
442 _mm_shuffle_epi32((X), _MM_SHUFFLE(2,3,0,1))
443 #else
444 #define HI32(X) \
445 _mm_srli_epi64((X), 32)
446 #endif
447  
448 #if defined(__x86_64__) && \
449 __GNUC__ == 4 && __GNUC_MINOR__ < 6 && !defined(__ICC)
450 #ifdef __AVX__
451 #define MOVQ "vmovq"
452 #else
453 /* "movq" would be more correct, but "movd" is supported by older binutils
454 * due to an error in AMD's spec for x86-64. */
455 #define MOVQ "movd"
456 #endif
457 #define EXTRACT64(X) ({ \
458 uint64_t result; \
459 __asm__(MOVQ " %1, %0" : "=r" (result) : "x" (X)); \
460 result; \
461 })
462 #elif defined(__x86_64__) && !defined(_MSC_VER) && !defined(__OPEN64__)
463 /* MSVC and Open64 had bugs */
464 #define EXTRACT64(X) _mm_cvtsi128_si64(X)
465 #elif defined(__x86_64__) && defined(__SSE4_1__)
466 /* No known bugs for this intrinsic */
467 #include <smmintrin.h>
468 #define EXTRACT64(X) _mm_extract_epi64((X), 0)
469 #elif defined(USE_SSE4_FOR_32BIT) && defined(__SSE4_1__)
470 /* 32-bit */
471 #include <smmintrin.h>
472 #if 0
473 /* This is currently unused by the code below, which instead uses these two
474 * intrinsics explicitly when (!defined(__x86_64__) && defined(__SSE4_1__)) */
475 #define EXTRACT64(X) \
476 ((uint64_t)(uint32_t)_mm_cvtsi128_si32(X) | \
477 ((uint64_t)(uint32_t)_mm_extract_epi32((X), 1) << 32))
478 #endif
479 #else
480 /* 32-bit or compilers with known past bugs in _mm_cvtsi128_si64() */
481 #define EXTRACT64(X) \
482 ((uint64_t)(uint32_t)_mm_cvtsi128_si32(X) | \
483 ((uint64_t)(uint32_t)_mm_cvtsi128_si32(HI32(X)) << 32))
484 #endif
485  
486 #if defined(__x86_64__) && (defined(__AVX__) || !defined(__GNUC__))
487 /* 64-bit with AVX */
488 /* Force use of 64-bit AND instead of two 32-bit ANDs */
489 #undef DECL_SMASK2REG
490 #if defined(__GNUC__) && !defined(__ICC)
491 #define DECL_SMASK2REG uint64_t Smask2reg = Smask2;
492 /* Force use of lower-numbered registers to reduce number of prefixes, relying
493 * on out-of-order execution and register renaming. */
494 #define FORCE_REGALLOC_1 \
495 __asm__("" : "=a" (x), "+d" (Smask2reg), "+S" (S0), "+D" (S1));
496 #define FORCE_REGALLOC_2 \
497 __asm__("" : : "c" (lo));
498 #else
499 static volatile uint64_t Smask2var = Smask2;
500 #define DECL_SMASK2REG uint64_t Smask2reg = Smask2var;
501 #define FORCE_REGALLOC_1 /* empty */
502 #define FORCE_REGALLOC_2 /* empty */
503 #endif
504 #define PWXFORM_SIMD(X) { \
505 uint64_t x; \
506 FORCE_REGALLOC_1 \
507 uint32_t lo = x = EXTRACT64(X) & Smask2reg; \
508 FORCE_REGALLOC_2 \
509 uint32_t hi = x >> 32; \
510 X = _mm_mul_epu32(HI32(X), X); \
511 X = _mm_add_epi64(X, *(__m128i *)(S0 + lo)); \
512 X = _mm_xor_si128(X, *(__m128i *)(S1 + hi)); \
513 }
514 #elif defined(__x86_64__)
515 /* 64-bit without AVX. This relies on out-of-order execution and register
516 * renaming. It may actually be fastest on CPUs with AVX(2) as well - e.g.,
517 * it runs great on Haswell. */
518 #ifdef CPX_YESCRYPT_BUILD_NOTES /* cpx: see the build notes above */
519 #warning "Note: using x86-64 inline assembly for YESCRYPT_RW. That's great."
520 #endif
521 /* We need a compiler memory barrier between sub-blocks to ensure that none of
522 * the writes into what was S2 during processing of the previous sub-block are
523 * postponed until after a read from S0 or S1 in the inline asm code below. */
524 #undef MAYBE_MEMORY_BARRIER
525 #define MAYBE_MEMORY_BARRIER \
526 __asm__("" : : : "memory");
527 #ifdef __ILP32__ /* x32 */
528 #define REGISTER_PREFIX "e"
529 #else
530 #define REGISTER_PREFIX "r"
531 #endif
532 #define PWXFORM_SIMD(X) { \
533 __m128i H; \
534 __asm__( \
535 "movd %0, %%rax\n\t" \
536 "pshufd $0xb1, %0, %1\n\t" \
537 "andq %2, %%rax\n\t" \
538 "pmuludq %1, %0\n\t" \
539 "movl %%eax, %%ecx\n\t" \
540 "shrq $0x20, %%rax\n\t" \
541 "paddq (%3,%%" REGISTER_PREFIX "cx), %0\n\t" \
542 "pxor (%4,%%" REGISTER_PREFIX "ax), %0\n\t" \
543 : "+x" (X), "=x" (H) \
544 : "d" (Smask2), "S" (S0), "D" (S1) \
545 : "cc", "ax", "cx"); \
546 }
547 #elif defined(USE_SSE4_FOR_32BIT) && defined(__SSE4_1__)
548 /* 32-bit with SSE4.1 */
549 #define PWXFORM_SIMD(X) { \
550 __m128i x = _mm_and_si128(X, _mm_set1_epi64x(Smask2)); \
551 __m128i s0 = *(__m128i *)(S0 + (uint32_t)_mm_cvtsi128_si32(x)); \
552 __m128i s1 = *(__m128i *)(S1 + (uint32_t)_mm_extract_epi32(x, 1)); \
553 X = _mm_mul_epu32(HI32(X), X); \
554 X = _mm_add_epi64(X, s0); \
555 X = _mm_xor_si128(X, s1); \
556 }
557 #else
558 /* 32-bit without SSE4.1 */
559 #define PWXFORM_SIMD(X) { \
560 uint64_t x = EXTRACT64(X) & Smask2; \
561 __m128i s0 = *(__m128i *)(S0 + (uint32_t)x); \
562 __m128i s1 = *(__m128i *)(S1 + (x >> 32)); \
563 X = _mm_mul_epu32(HI32(X), X); \
564 X = _mm_add_epi64(X, s0); \
565 X = _mm_xor_si128(X, s1); \
566 }
567 #endif
568  
569 #define PWXFORM_ROUND \
570 PWXFORM_SIMD(X0) \
571 PWXFORM_SIMD(X1) \
572 PWXFORM_SIMD(X2) \
573 PWXFORM_SIMD(X3)
574  
575 #if defined(__x86_64__) && defined(__GNUC__) && !defined(__ICC)
576 #undef FORCE_REGALLOC_3
577 #define FORCE_REGALLOC_3 __asm__("" : : "b" (Sw));
578 #endif
579  
580 #else /* !defined(__SSE2__) */
581  
582 #define PWXFORM_SIMD(x0, x1) { \
583 uint64_t x = x0 & Smask2; \
584 uint64_t *p0 = (uint64_t *)(S0 + (uint32_t)x); \
585 uint64_t *p1 = (uint64_t *)(S1 + (x >> 32)); \
586 x0 = ((x0 >> 32) * (uint32_t)x0 + p0[0]) ^ p1[0]; \
587 x1 = ((x1 >> 32) * (uint32_t)x1 + p0[1]) ^ p1[1]; \
588 }
589  
590 #define PWXFORM_ROUND \
591 PWXFORM_SIMD(X.d[0], X.d[1]) \
592 PWXFORM_SIMD(X.d[2], X.d[3]) \
593 PWXFORM_SIMD(X.d[4], X.d[5]) \
594 PWXFORM_SIMD(X.d[6], X.d[7])
595 #endif
596  
597 /*
598 * This offset helps address the 256-byte write block via the single-byte
599 * displacements encodable in x86(-64) instructions. It is needed because the
600 * displacements are signed. Without it, we'd get 4-byte displacements for
601 * half of the writes. Setting it to 0x80 instead of 0x7c would avoid needing
602 * a displacement for one of the writes, but then the LEA instruction would
603 * need a 4-byte displacement.
604 */
605 #define PWXFORM_WRITE_OFFSET 0x7c
606  
607 #define PWXFORM_WRITE \
608 WRITE_X(*(salsa20_blk_t *)(Sw - PWXFORM_WRITE_OFFSET)) \
609 Sw += 64;
610  
611 #define PWXFORM { \
612 uint8_t *Sw = S2 + w + PWXFORM_WRITE_OFFSET; \
613 FORCE_REGALLOC_3 \
614 MAYBE_MEMORY_BARRIER \
615 PWXFORM_ROUND \
616 PWXFORM_ROUND PWXFORM_WRITE \
617 PWXFORM_ROUND PWXFORM_WRITE \
618 PWXFORM_ROUND PWXFORM_WRITE \
619 PWXFORM_ROUND PWXFORM_WRITE \
620 PWXFORM_ROUND \
621 w = (w + 64 * 4) & Smask2; \
622 { \
623 uint8_t *Stmp = S2; \
624 S2 = S1; \
625 S1 = S0; \
626 S0 = Stmp; \
627 } \
628 }
629  
630 typedef struct {
631 uint8_t *S0, *S1, *S2;
632 size_t w;
633 } pwxform_ctx_t;
634  
635 #define Salloc (Sbytes + ((sizeof(pwxform_ctx_t) + 63) & ~63U))
636  
637 /**
638 * blockmix_pwxform(Bin, Bout, r, S):
639 * Compute Bout = BlockMix_pwxform{salsa20/2, r, S}(Bin). The input Bin must
640 * be 128r bytes in length; the output Bout must also be the same size.
641 */
642 44 static void blockmix(const salsa20_blk_t *restrict Bin,
643 salsa20_blk_t *restrict Bout, size_t r, pwxform_ctx_t *restrict ctx)
644 {
645 44 uint8_t *S0 = ctx->S0, *S1 = ctx->S1, *S2 = ctx->S2;
646 44 size_t w = ctx->w;
647 size_t i;
648 DECL_X
649  
650 /* Convert count of 128-byte blocks to max index of 64-byte block */
651 44 r = r * 2 - 1;
652  
653 44 READ_X(Bin[r])
654  
655 DECL_SMASK2REG
656  
657 44 i = 0;
658 do {
659 10336 XOR_X(Bin[i])
660 2584 PWXFORM
661 2584 ▶ if (unlikely(i >= r))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(i >= r)) 44 2540 2584
662 44 break;
663 2540 WRITE_X(Bout[i])
664 2540 i++;
665 } while (1);
666  
667 44 ctx->S0 = S0; ctx->S1 = S1; ctx->S2 = S2;
668 44 ctx->w = w;
669  
670 1936 SALSA20_2(Bout[i])
671 44 }
672  
673 19956 static uint32_t blockmix_xor(const salsa20_blk_t *Bin1,
674 const salsa20_blk_t *restrict Bin2, salsa20_blk_t *Bout,
675 size_t r, int Bin2_in_ROM, pwxform_ctx_t *restrict ctx)
676 {
677 19956 uint8_t *S0 = ctx->S0, *S1 = ctx->S1, *S2 = ctx->S2;
678 19956 size_t w = ctx->w;
679 size_t i;
680 DECL_X
681  
682 /* Convert count of 128-byte blocks to max index of 64-byte block */
683 19956 r = r * 2 - 1;
684  
685 #ifdef PREFETCH
686 19956 ▶ if (Bin2_in_ROM) {
Branch: 1 branch
BranchTrueFalseTotal
if (Bin2_in_ROM) { 0 19956 19956
687 0 PREFETCH(&Bin2[r], _MM_HINT_NTA)
688 0 ▶ for (i = 0; i < r; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < r; i++) { 0 0 0
689 0 PREFETCH(&Bin2[i], _MM_HINT_NTA)
690 }
691 } else {
692 19956 PREFETCH(&Bin2[r], _MM_HINT_T0)
693 1261160 ▶ for (i = 0; i < r; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < r; i++) { 1241204 19956 1261160
694 1241204 PREFETCH(&Bin2[i], _MM_HINT_T0)
695 }
696 }
697 #else
698 (void)Bin2_in_ROM; /* unused */
699 #endif
700  
701 79824 XOR_X_2(Bin1[r], Bin2[r])
702  
703 DECL_SMASK2REG
704  
705 19956 i = 0;
706 19956 r--;
707 do {
708 2522320 XOR_X(Bin1[i])
709 2522320 XOR_X(Bin2[i])
710 630580 PWXFORM
711 630580 WRITE_X(Bout[i])
712  
713 2522320 XOR_X(Bin1[i + 1])
714 2522320 XOR_X(Bin2[i + 1])
715 630580 PWXFORM
716  
717 630580 ▶ if (unlikely(i >= r))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(i >= r)) 19956 610624 630580
718 19956 break;
719  
720 610624 WRITE_X(Bout[i + 1])
721  
722 610624 i += 2;
723 } while (1);
724 19956 i++;
725  
726 19956 ctx->S0 = S0; ctx->S1 = S1; ctx->S2 = S2;
727 19956 ctx->w = w;
728  
729 898020 SALSA20_2(Bout[i])
730  
731 19956 return INTEGERIFY;
732 }
733  
734 268996 static uint32_t blockmix_xor_save(salsa20_blk_t *restrict Bin1out,
735 salsa20_blk_t *restrict Bin2,
736 size_t r, pwxform_ctx_t *restrict ctx)
737 {
738 268996 uint8_t *S0 = ctx->S0, *S1 = ctx->S1, *S2 = ctx->S2;
739 268996 size_t w = ctx->w;
740 size_t i;
741 DECL_X
742 DECL_Y
743  
744 /* Convert count of 128-byte blocks to max index of 64-byte block */
745 268996 r = r * 2 - 1;
746  
747 #ifdef PREFETCH
748 268996 PREFETCH(&Bin2[r], _MM_HINT_T0)
749 17210208 ▶ for (i = 0; i < r; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < r; i++) { 16941212 268996 17210208
750 16941212 PREFETCH(&Bin2[i], _MM_HINT_T0)
751 }
752 #endif
753  
754 1075984 XOR_X_2(Bin1out[r], Bin2[r])
755  
756 DECL_SMASK2REG
757  
758 268996 i = 0;
759 268996 r--;
760 do {
761 68840832 XOR_X_WRITE_XOR_Y_2(Bin2[i], Bin1out[i])
762 8605104 PWXFORM
763 8605104 WRITE_X(Bin1out[i])
764  
765 68840832 XOR_X_WRITE_XOR_Y_2(Bin2[i + 1], Bin1out[i + 1])
766 8605104 PWXFORM
767  
768 8605104 ▶ if (unlikely(i >= r))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(i >= r)) 268996 8336108 8605104
769 268996 break;
770  
771 8336108 WRITE_X(Bin1out[i + 1])
772  
773 8336108 i += 2;
774 } while (1);
775 268996 i++;
776  
777 268996 ctx->S0 = S0; ctx->S1 = S1; ctx->S2 = S2;
778 268996 ctx->w = w;
779  
780 12104820 SALSA20_2(Bin1out[i])
781  
782 268996 return INTEGERIFY;
783 }
784  
785 /**
786 * integerify(B, r):
787 * Return the result of parsing B_{2r-1} as a little-endian integer.
788 */
789 46 static inline uint32_t integerify(const salsa20_blk_t *B, size_t r)
790 {
791 /*
792 * Our 64-bit words are in host byte order, which is why we don't just read
793 * w[0] here (would be wrong on big-endian). Also, our 32-bit words are
794 * SIMD-shuffled (so the next 32 bits would be part of d[6]), but currently
795 * this does not matter as we only care about the least significant 32 bits.
796 */
797 46 return (uint32_t)B[2 * r - 1].d[0];
798 }
799  
800 /**
801 * smix1(B, r, N, flags, V, NROM, VROM, XY, ctx):
802 * Compute first loop of B = SMix_r(B, N). The input B must be 128r bytes in
803 * length; the temporary storage V must be 128rN bytes in length; the temporary
804 * storage XY must be 128r+64 bytes in length. N must be even and at least 4.
805 * The array V must be aligned to a multiple of 64 bytes, and arrays B and XY
806 * to a multiple of at least 16 bytes.
807 */
808 46 static void smix1(uint8_t *B, size_t r, uint32_t N, yescrypt_flags_t flags,
809 salsa20_blk_t *V, uint32_t NROM, const salsa20_blk_t *VROM,
810 salsa20_blk_t *XY, pwxform_ctx_t *ctx)
811 {
812 46 size_t s = 2 * r;
813 46 salsa20_blk_t *X = V, *Y = &V[s];
814 uint32_t i, j;
815  
816 1510 ▶ for (i = 0; i < 2 * r; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 2 * r; i++) { 1464 46 1510
817 1464 const salsa20_blk_t *src = (salsa20_blk_t *)&B[i * 64];
818 1464 salsa20_blk_t *tmp = Y;
819 1464 salsa20_blk_t *dst = &X[i];
820 size_t k;
821 24888 ▶ for (k = 0; k < 16; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < 16; k++) 23424 1464 24888
822 23424 tmp->w[k] = le32dec(&src->w[k]);
823 1464 salsa20_simd_shuffle(tmp, dst);
824 }
825  
826 46 ▶ if (VROM) {
Branch: 1 branch
BranchTrueFalseTotal
if (VROM) { 0 46 46
827 uint32_t n;
828 const salsa20_blk_t *V_j;
829  
830 0 V_j = &VROM[(NROM - 1) * s];
831 0 j = blockmix_xor(X, V_j, Y, r, 1, ctx) & (NROM - 1);
832 0 V_j = &VROM[j * s];
833 0 X = Y + s;
834 0 j = blockmix_xor(Y, V_j, X, r, 1, ctx);
835  
836 0 ▶ for (n = 2; n < N; n <<= 1) {
Branch: 1 branch
BranchTrueFalseTotal
for (n = 2; n < N; n <<= 1) { 0 0 0
837 0 ▶ uint32_t m = (n < N / 2) ? n : (N - 1 - n);
Branch: 1 branch
BranchTrueFalseTotal
uint32_t m = (n < N / 2) ? n : (N - 1 - n); 0 0 0
838 0 ▶ for (i = 1; i < m; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < m; i += 2) { 0 0 0
839 0 j &= n - 1;
840 0 j += i - 1;
841 0 V_j = &V[j * s];
842 0 Y = X + s;
843 0 j = blockmix_xor(X, V_j, Y, r, 0, ctx) & (NROM - 1);
844 0 V_j = &VROM[j * s];
845 0 X = Y + s;
846 0 j = blockmix_xor(Y, V_j, X, r, 1, ctx);
847 }
848 }
849 0 n >>= 1;
850  
851 0 j &= n - 1;
852 0 j += N - 2 - n;
853 0 V_j = &V[j * s];
854 0 Y = X + s;
855 0 j = blockmix_xor(X, V_j, Y, r, 0, ctx) & (NROM - 1);
856 0 V_j = &VROM[j * s];
857 0 blockmix_xor(Y, V_j, XY, r, 1, ctx);
858 46 ▶ } else if (flags & YESCRYPT_RW) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (flags & YESCRYPT_RW) { 22 24 46
859 uint32_t n;
860 salsa20_blk_t *V_j;
861  
862 22 blockmix(X, Y, r, ctx);
863 22 X = Y + s;
864 22 blockmix(Y, X, r, ctx);
865 22 j = integerify(X, r);
866  
867 181 ▶ for (n = 2; n < N; n <<= 1) {
Branch: 1 branch
BranchTrueFalseTotal
for (n = 2; n < N; n <<= 1) { 159 22 181
868 159 ▶ uint32_t m = (n < N / 2) ? n : (N - 1 - n);
Branch: 1 branch
BranchTrueFalseTotal
uint32_t m = (n < N / 2) ? n : (N - 1 - n); 22 137 159
869 10115 ▶ for (i = 1; i < m; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < m; i += 2) { 9956 159 10115
870 9956 Y = X + s;
871 9956 j &= n - 1;
872 9956 j += i - 1;
873 9956 V_j = &V[j * s];
874 9956 j = blockmix_xor(X, V_j, Y, r, 0, ctx);
875 9956 j &= n - 1;
876 9956 j += i;
877 9956 V_j = &V[j * s];
878 9956 X = Y + s;
879 9956 j = blockmix_xor(Y, V_j, X, r, 0, ctx);
880 }
881 }
882 22 n >>= 1;
883  
884 22 j &= n - 1;
885 22 j += N - 2 - n;
886 22 V_j = &V[j * s];
887 22 Y = X + s;
888 22 j = blockmix_xor(X, V_j, Y, r, 0, ctx);
889 22 j &= n - 1;
890 22 j += N - 1 - n;
891 22 V_j = &V[j * s];
892 22 blockmix_xor(Y, V_j, XY, r, 0, ctx);
893 } else {
894 24 N -= 2;
895 do {
896 1288 blockmix_salsa8(X, Y, r);
897 1288 X = Y + s;
898 1288 blockmix_salsa8(Y, X, r);
899 1288 Y = X + s;
900 1288 ▶ } while ((N -= 2));
Branch: 1 branch
BranchTrueFalseTotal
} while ((N -= 2)); 1264 24 1288
901  
902 24 blockmix_salsa8(X, Y, r);
903 24 blockmix_salsa8(Y, XY, r);
904 }
905  
906 1510 ▶ for (i = 0; i < 2 * r; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 2 * r; i++) { 1464 46 1510
907 1464 const salsa20_blk_t *src = &XY[i];
908 1464 salsa20_blk_t *tmp = &XY[s];
909 1464 salsa20_blk_t *dst = (salsa20_blk_t *)&B[i * 64];
910 size_t k;
911 24888 ▶ for (k = 0; k < 16; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < 16; k++) 23424 1464 24888
912 23424 le32enc(&tmp->w[k], src->w[k]);
913 1464 salsa20_simd_unshuffle(tmp, dst);
914 }
915 46 }
916  
917 /**
918 * smix2(B, r, N, Nloop, flags, V, NROM, VROM, XY, ctx):
919 * Compute second loop of B = SMix_r(B, N). The input B must be 128r bytes in
920 * length; the temporary storage V must be 128rN bytes in length; the temporary
921 * storage XY must be 256r bytes in length. N must be a power of 2 and at
922 * least 2. Nloop must be even. The array V must be aligned to a multiple of
923 * 64 bytes, and arrays B and XY to a multiple of at least 16 bytes.
924 */
925 26 static void smix2(uint8_t *B, size_t r, uint32_t N, uint64_t Nloop,
926 yescrypt_flags_t flags, salsa20_blk_t *V, uint32_t NROM,
927 const salsa20_blk_t *VROM, salsa20_blk_t *XY, pwxform_ctx_t *ctx)
928 {
929 26 size_t s = 2 * r;
930 26 salsa20_blk_t *X = XY, *Y = &XY[s];
931 uint32_t i, j;
932  
933 26 ▶ if (Nloop == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (Nloop == 0) 2 24 26
934 2 return;
935  
936 1444 ▶ for (i = 0; i < 2 * r; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 2 * r; i++) { 1420 24 1444
937 1420 const salsa20_blk_t *src = (salsa20_blk_t *)&B[i * 64];
938 1420 salsa20_blk_t *tmp = Y;
939 1420 salsa20_blk_t *dst = &X[i];
940 size_t k;
941 24140 ▶ for (k = 0; k < 16; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < 16; k++) 22720 1420 24140
942 22720 tmp->w[k] = le32dec(&src->w[k]);
943 1420 salsa20_simd_shuffle(tmp, dst);
944 }
945  
946 24 j = integerify(X, r) & (N - 1);
947  
948 /*
949 * Normally, VROM implies YESCRYPT_RW, but we check for these separately
950 * because our SMix resets YESCRYPT_RW for the smix2() calls operating on the
951 * entire V when p > 1.
952 */
953 24 ▶ if (VROM && (flags & YESCRYPT_RW)) {
Branch: 2 branches
BranchTrueFalseTotal
if (VROM && (flags & YESCRYPT_RW)) { 0 24 24
if (VROM && (flags & YESCRYPT_RW)) { 0 0 0
954 do {
955 0 salsa20_blk_t *V_j = &V[j * s];
956 const salsa20_blk_t *VROM_j;
957 0 j = blockmix_xor_save(X, V_j, r, ctx) & (NROM - 1);
958 0 VROM_j = &VROM[j * s];
959 0 j = blockmix_xor(X, VROM_j, X, r, 1, ctx) & (N - 1);
960 0 ▶ } while (Nloop -= 2);
Branch: 1 branch
BranchTrueFalseTotal
} while (Nloop -= 2); 0 0 0
961 24 ▶ } else if (VROM) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (VROM) { 0 24 24
962 do {
963 0 const salsa20_blk_t *V_j = &V[j * s];
964 0 j = blockmix_xor(X, V_j, X, r, 0, ctx) & (NROM - 1);
965 0 V_j = &VROM[j * s];
966 0 j = blockmix_xor(X, V_j, X, r, 1, ctx) & (N - 1);
967 0 ▶ } while (Nloop -= 2);
Branch: 1 branch
BranchTrueFalseTotal
} while (Nloop -= 2); 0 0 0
968 24 ▶ } else if (flags & YESCRYPT_RW) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (flags & YESCRYPT_RW) { 22 2 24
969 do {
970 134498 salsa20_blk_t *V_j = &V[j * s];
971 134498 j = blockmix_xor_save(X, V_j, r, ctx) & (N - 1);
972 134498 V_j = &V[j * s];
973 134498 j = blockmix_xor_save(X, V_j, r, ctx) & (N - 1);
974 134498 ▶ } while (Nloop -= 2);
Branch: 1 branch
BranchTrueFalseTotal
} while (Nloop -= 2); 134476 22 134498
975 2 ▶ } else if (ctx) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (ctx) { 0 2 2
976 do {
977 0 const salsa20_blk_t *V_j = &V[j * s];
978 0 j = blockmix_xor(X, V_j, X, r, 0, ctx) & (N - 1);
979 0 V_j = &V[j * s];
980 0 j = blockmix_xor(X, V_j, X, r, 0, ctx) & (N - 1);
981 0 ▶ } while (Nloop -= 2);
Branch: 1 branch
BranchTrueFalseTotal
} while (Nloop -= 2); 0 0 0
982 } else {
983 do {
984 256 const salsa20_blk_t *V_j = &V[j * s];
985 256 j = blockmix_salsa8_xor(X, V_j, Y, r) & (N - 1);
986 256 V_j = &V[j * s];
987 256 j = blockmix_salsa8_xor(Y, V_j, X, r) & (N - 1);
988 256 ▶ } while (Nloop -= 2);
Branch: 1 branch
BranchTrueFalseTotal
} while (Nloop -= 2); 254 2 256
989 }
990  
991 1444 ▶ for (i = 0; i < 2 * r; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 2 * r; i++) { 1420 24 1444
992 1420 const salsa20_blk_t *src = &X[i];
993 1420 salsa20_blk_t *tmp = Y;
994 1420 salsa20_blk_t *dst = (salsa20_blk_t *)&B[i * 64];
995 size_t k;
996 24140 ▶ for (k = 0; k < 16; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < 16; k++) 22720 1420 24140
997 22720 le32enc(&tmp->w[k], src->w[k]);
998 1420 salsa20_simd_unshuffle(tmp, dst);
999 }
1000 }
1001  
1002 /**
1003 * p2floor(x):
1004 * Largest power of 2 not greater than argument.
1005 */
1006 24 static uint64_t p2floor(uint64_t x)
1007 {
1008 uint64_t y;
1009 24 ▶ while ((y = x & (x - 1)))
Branch: 1 branch
BranchTrueFalseTotal
while ((y = x & (x - 1))) 0 24 24
1010 0 x = y;
1011 24 return x;
1012 }
1013  
1014 /**
1015 * smix(B, r, N, p, t, flags, V, NROM, VROM, XY, S, passwd):
1016 * Compute B = SMix_r(B, N). The input B must be 128rp bytes in length; the
1017 * temporary storage V must be 128rN bytes in length; the temporary storage
1018 * XY must be 256r or 256rp bytes in length (the larger size is required with
1019 * OpenMP-enabled builds). N must be a power of 2 and at least 4. The array V
1020 * must be aligned to a multiple of 64 bytes, and arrays B and XY to a multiple
1021 * of at least 16 bytes (aligning them to 64 bytes as well saves cache lines
1022 * and helps avoid false sharing in OpenMP-enabled builds when p > 1, but it
1023 * might also result in cache bank conflicts).
1024 */
1025 24 static void smix(uint8_t *B, size_t r, uint32_t N, uint32_t p, uint32_t t,
1026 yescrypt_flags_t flags,
1027 salsa20_blk_t *V, uint32_t NROM, const salsa20_blk_t *VROM,
1028 salsa20_blk_t *XY, uint8_t *S, uint8_t *passwd)
1029 {
1030 24 size_t s = 2 * r;
1031 uint32_t Nchunk;
1032 uint64_t Nloop_all, Nloop_rw;
1033 uint32_t i;
1034  
1035 24 Nchunk = N / p;
1036 24 Nloop_all = Nchunk;
1037 24 ▶ if (flags & YESCRYPT_RW) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags & YESCRYPT_RW) { 22 2 24
1038 22 ▶ if (t <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (t <= 1) { 20 2 22
1039 20 ▶ if (t)
Branch: 1 branch
BranchTrueFalseTotal
if (t) 1 19 20
1040 1 Nloop_all *= 2; /* 2/3 */
1041 20 Nloop_all = (Nloop_all + 2) / 3; /* 1/3, round up */
1042 } else {
1043 2 Nloop_all *= t - 1;
1044 }
1045 2 ▶ } else if (t) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (t) { 0 2 2
1046 0 ▶ if (t == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (t == 1) 0 0 0
1047 0 Nloop_all += (Nloop_all + 1) / 2; /* 1.5, round up */
1048 0 Nloop_all *= t;
1049 }
1050  
1051 24 Nloop_rw = 0;
1052 24 ▶ if (flags & YESCRYPT_INIT_SHARED)
Branch: 1 branch
BranchTrueFalseTotal
if (flags & YESCRYPT_INIT_SHARED) 0 24 24
1053 0 Nloop_rw = Nloop_all;
1054 24 ▶ else if (flags & YESCRYPT_RW)
Branch: 1 branch
BranchTrueFalseTotal
else if (flags & YESCRYPT_RW) 22 2 24
1055 22 Nloop_rw = Nloop_all / p;
1056  
1057 24 Nchunk &= ~(uint32_t)1; /* round down to even */
1058 24 Nloop_all++; Nloop_all &= ~(uint64_t)1; /* round up to even */
1059 24 Nloop_rw++; Nloop_rw &= ~(uint64_t)1; /* round up to even */
1060  
1061 #ifdef _OPENMP
1062 #pragma omp parallel if (p > 1) default(none) private(i) shared(B, r, N, p, flags, V, NROM, VROM, XY, S, passwd, s, Nchunk, Nloop_all, Nloop_rw)
1063 {
1064 #pragma omp for
1065 #endif
1066 48 ▶ for (i = 0; i < p; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < p; i++) { 24 24 48
1067 24 uint32_t Vchunk = i * Nchunk;
1068 24 ▶ uint32_t Np = (i < p - 1) ? Nchunk : (N - Vchunk);
Branch: 1 branch
BranchTrueFalseTotal
uint32_t Np = (i < p - 1) ? Nchunk : (N - Vchunk); 24 0 24
1069 24 uint8_t *Bp = &B[128 * r * i];
1070 24 salsa20_blk_t *Vp = &V[Vchunk * s];
1071 #ifdef _OPENMP
1072 salsa20_blk_t *XYp = &XY[i * (2 * s)];
1073 #else
1074 24 salsa20_blk_t *XYp = XY;
1075 #endif
1076 24 pwxform_ctx_t *ctx_i = NULL;
1077 24 ▶ if (flags & YESCRYPT_RW) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags & YESCRYPT_RW) { 22 2 24
1078 22 uint8_t *Si = S + i * Salloc;
1079 22 smix1(Bp, 1, Sbytes / 128, 0 /* no flags */,
1080 (salsa20_blk_t *)Si, 0, NULL, XYp, NULL);
1081 22 ctx_i = (pwxform_ctx_t *)(Si + Sbytes);
1082 22 ctx_i->S2 = Si;
1083 22 ctx_i->S1 = Si + Sbytes / 3;
1084 22 ctx_i->S0 = Si + Sbytes / 3 * 2;
1085 22 ctx_i->w = 0;
1086 22 ▶ if (i == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (i == 0) 22 0 22
1087 22 HMAC_SHA256_Buf(Bp + (128 * r - 64), 64,
1088 passwd, 32, passwd);
1089 }
1090 24 smix1(Bp, r, Np, flags, Vp, NROM, VROM, XYp, ctx_i);
1091 24 smix2(Bp, r, p2floor(Np), Nloop_rw, flags, Vp,
1092 NROM, VROM, XYp, ctx_i);
1093 }
1094  
1095 24 ▶ if (Nloop_all > Nloop_rw) {
Branch: 1 branch
BranchTrueFalseTotal
if (Nloop_all > Nloop_rw) { 2 22 24
1096 #ifdef _OPENMP
1097 #pragma omp for
1098 #endif
1099 4 ▶ for (i = 0; i < p; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < p; i++) { 2 2 4
1100 2 uint8_t *Bp = &B[128 * r * i];
1101 #ifdef _OPENMP
1102 salsa20_blk_t *XYp = &XY[i * (2 * s)];
1103 #else
1104 2 salsa20_blk_t *XYp = XY;
1105 #endif
1106 2 pwxform_ctx_t *ctx_i = NULL;
1107 2 ▶ if (flags & YESCRYPT_RW) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags & YESCRYPT_RW) { 0 2 2
1108 0 uint8_t *Si = S + i * Salloc;
1109 0 ctx_i = (pwxform_ctx_t *)(Si + Sbytes);
1110 }
1111 2 smix2(Bp, r, N, Nloop_all - Nloop_rw,
1112 flags & ~YESCRYPT_RW, V, NROM, VROM, XYp, ctx_i);
1113 }
1114 }
1115 #ifdef _OPENMP
1116 }
1117 #endif
1118 24 }
1119  
1120 /**
1121 * yescrypt_kdf_body(shared, local, passwd, passwdlen, salt, saltlen,
1122 * flags, N, r, p, t, NROM, buf, buflen):
1123 * Compute scrypt(passwd[0 .. passwdlen - 1], salt[0 .. saltlen - 1], N, r,
1124 * p, buflen), or a revision of scrypt as requested by flags and shared, and
1125 * write the result into buf.
1126 *
1127 * shared and flags may request special modes as described in yescrypt.h.
1128 *
1129 * local is the thread-local data structure, allowing to preserve and reuse a
1130 * memory allocation across calls, thereby reducing its overhead.
1131 *
1132 * t controls computation time while not affecting peak memory usage.
1133 *
1134 * Return 0 on success; or -1 on error.
1135 *
1136 * This optimized implementation currently limits N to the range from 4 to
1137 * 2^31, but other implementations might not.
1138 */
1139 28 static int yescrypt_kdf_body(const yescrypt_shared_t *shared,
1140 yescrypt_local_t *local,
1141 const uint8_t *passwd, size_t passwdlen,
1142 const uint8_t *salt, size_t saltlen,
1143 yescrypt_flags_t flags, uint64_t N, uint32_t r, uint32_t p, uint32_t t,
1144 uint64_t NROM,
1145 uint8_t *buf, size_t buflen)
1146 {
1147 yescrypt_region_t tmp;
1148 const salsa20_blk_t *VROM;
1149 size_t B_size, V_size, XY_size, need;
1150 uint8_t *B, *S;
1151 salsa20_blk_t *V, *XY;
1152 uint8_t sha256[32];
1153 28 uint8_t dk[sizeof(sha256)], *dkp = buf;
1154  
1155 /* Sanity-check parameters */
1156 28 switch (flags & YESCRYPT_MODE_MASK) {
1157 1 case 0: /* classic scrypt - can't have anything non-standard */
1158 1 ▶ if (flags || t || NROM)
Branch: 3 branches
BranchTrueFalseTotal
if (flags || t || NROM) 1 0 1
if (flags || t || NROM) 1 0 1
if (flags || t || NROM) 0 1 1
1159 0 goto out_EINVAL;
1160 1 break;
1161 1 case YESCRYPT_WORM:
1162 1 ▶ if (flags != YESCRYPT_WORM || NROM)
Branch: 2 branches
BranchTrueFalseTotal
if (flags != YESCRYPT_WORM || NROM) 1 0 1
if (flags != YESCRYPT_WORM || NROM) 0 1 1
1163 0 goto out_EINVAL;
1164 1 break;
1165 26 case YESCRYPT_RW:
1166 26 ▶ if (flags != (flags & YESCRYPT_KNOWN_FLAGS))
Branch: 1 branch
BranchTrueFalseTotal
if (flags != (flags & YESCRYPT_KNOWN_FLAGS)) 0 26 26
1167 0 goto out_EINVAL;
1168 #if PWXsimple == 2 && PWXgather == 4 && Sbytes == 12288
1169 26 ▶ if ((flags & YESCRYPT_RW_FLAVOR_MASK) ==
Branch: 1 branch
BranchTrueFalseTotal
if ((flags & YESCRYPT_RW_FLAVOR_MASK) == 26 0 26
1170 (YESCRYPT_ROUNDS_6 | YESCRYPT_GATHER_4 |
1171 YESCRYPT_SIMPLE_2 | YESCRYPT_SBOX_12K))
1172 26 break;
1173 #else
1174 #error "Unsupported pwxform settings"
1175 #endif
1176 /* FALLTHRU */
1177 default:
1178 0 goto out_EINVAL;
1179 }
1180 #if SIZE_MAX > UINT32_MAX
1181 28 ▶ if (buflen > (((uint64_t)1 << 32) - 1) * 32)
Branch: 1 branch
BranchTrueFalseTotal
if (buflen > (((uint64_t)1 << 32) - 1) * 32) 0 28 28
1182 0 goto out_EINVAL;
1183 #endif
1184 28 ▶ if ((uint64_t)r * (uint64_t)p >= 1 << 30)
Branch: 1 branch
BranchTrueFalseTotal
if ((uint64_t)r * (uint64_t)p >= 1 << 30) 0 28 28
1185 0 goto out_EINVAL;
1186 28 ▶ if (N > UINT32_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (N > UINT32_MAX) 0 28 28
1187 0 goto out_EINVAL;
1188 28 ▶ if ((N & (N - 1)) != 0 || N <= 3 || r < 1 || p < 1)
Branch: 4 branches
BranchTrueFalseTotal
if ((N & (N - 1)) != 0 || N <= 3 || r < 1 || p < 1) 28 0 28
if ((N & (N - 1)) != 0 || N <= 3 || r < 1 || p < 1) 28 0 28
if ((N & (N - 1)) != 0 || N <= 3 || r < 1 || p < 1) 28 0 28
if ((N & (N - 1)) != 0 || N <= 3 || r < 1 || p < 1) 0 28 28
1189 0 goto out_EINVAL;
1190 28 ▶ if (r > SIZE_MAX / 256 / p ||
Branch: 1 branch
BranchTrueFalseTotal
if (r > SIZE_MAX / 256 / p || 28 0 28
1191 28 ▶ N > SIZE_MAX / 128 / r)
Branch: 1 branch
BranchTrueFalseTotal
N > SIZE_MAX / 128 / r) 0 28 28
1192 0 goto out_EINVAL;
1193 28 ▶ if (flags & YESCRYPT_RW) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags & YESCRYPT_RW) { 26 2 28
1194 26 ▶ if (N / p <= 3 || p > SIZE_MAX / Salloc)
Branch: 1 branch
BranchTrueFalseTotal
if (N / p <= 3 || p > SIZE_MAX / Salloc) 0 26 26
1195 0 goto out_EINVAL;
1196 }
1197 #ifdef _OPENMP
1198 else if (N > SIZE_MAX / 128 / (r * p)) {
1199 goto out_EINVAL;
1200 }
1201 #endif
1202  
1203 28 VROM = NULL;
1204 28 ▶ if (shared) {
Branch: 1 branch
BranchTrueFalseTotal
if (shared) { 0 28 28
1205 0 uint64_t expected_size = (size_t)128 * r * NROM;
1206 0 ▶ if ((NROM & (NROM - 1)) != 0 ||
Branch: 2 branches
BranchTrueFalseTotal
if ((NROM & (NROM - 1)) != 0 || 0 0 0
if ((NROM & (NROM - 1)) != 0 || 0 0 0
1207 0 ▶ NROM <= 1 || NROM > UINT32_MAX ||
Branch: 1 branch
BranchTrueFalseTotal
NROM <= 1 || NROM > UINT32_MAX || 0 0 0
1208 0 ▶ shared->aligned_size < expected_size)
Branch: 1 branch
BranchTrueFalseTotal
shared->aligned_size < expected_size) 0 0 0
1209 0 goto out_EINVAL;
1210 0 ▶ if (!(flags & YESCRYPT_INIT_SHARED)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(flags & YESCRYPT_INIT_SHARED)) { 0 0 0
1211 0 uint64_t *tag = (uint64_t *)
1212 0 ((uint8_t *)shared->aligned + expected_size - 48);
1213 0 ▶ if (tag[0] != YESCRYPT_ROM_TAG1 || tag[1] != YESCRYPT_ROM_TAG2)
Branch: 2 branches
BranchTrueFalseTotal
if (tag[0] != YESCRYPT_ROM_TAG1 || tag[1] != YESCRYPT_ROM_TAG2) 0 0 0
if (tag[0] != YESCRYPT_ROM_TAG1 || tag[1] != YESCRYPT_ROM_TAG2) 0 0 0
1214 0 goto out_EINVAL;
1215 }
1216 0 VROM = shared->aligned;
1217 } else {
1218 28 ▶ if (NROM)
Branch: 1 branch
BranchTrueFalseTotal
if (NROM) 0 28 28
1219 0 goto out_EINVAL;
1220 }
1221  
1222 /* Allocate memory */
1223 28 V = NULL;
1224 28 V_size = (size_t)128 * r * N;
1225 #ifdef _OPENMP
1226 if (!(flags & YESCRYPT_RW))
1227 V_size *= p;
1228 #endif
1229 28 need = V_size;
1230 28 ▶ if (flags & YESCRYPT_INIT_SHARED) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags & YESCRYPT_INIT_SHARED) { 0 28 28
1231 0 ▶ if (local->aligned_size < need) {
Branch: 1 branch
BranchTrueFalseTotal
if (local->aligned_size < need) { 0 0 0
1232 0 ▶ if (local->base || local->aligned ||
Branch: 2 branches
BranchTrueFalseTotal
if (local->base || local->aligned || 0 0 0
if (local->base || local->aligned || 0 0 0
1233 0 ▶ local->base_size || local->aligned_size)
Branch: 2 branches
BranchTrueFalseTotal
local->base_size || local->aligned_size) 0 0 0
local->base_size || local->aligned_size) 0 0 0
1234 0 goto out_EINVAL;
1235 0 ▶ if (!alloc_region(local, need))
Branch: 1 branch
BranchTrueFalseTotal
if (!alloc_region(local, need)) 0 0 0
1236 0 return -1;
1237 }
1238 0 ▶ if (flags & YESCRYPT_ALLOC_ONLY)
Branch: 1 branch
BranchTrueFalseTotal
if (flags & YESCRYPT_ALLOC_ONLY) 0 0 0
1239 0 return -2; /* expected "failure" */
1240 0 V = (salsa20_blk_t *)local->aligned;
1241 0 need = 0;
1242 }
1243 28 B_size = (size_t)128 * r * p;
1244 28 need += B_size;
1245 28 ▶ if (need < B_size)
Branch: 1 branch
BranchTrueFalseTotal
if (need < B_size) 0 28 28
1246 0 goto out_EINVAL;
1247 28 XY_size = (size_t)256 * r;
1248 #ifdef _OPENMP
1249 XY_size *= p;
1250 #endif
1251 28 need += XY_size;
1252 28 ▶ if (need < XY_size)
Branch: 1 branch
BranchTrueFalseTotal
if (need < XY_size) 0 28 28
1253 0 goto out_EINVAL;
1254 28 ▶ if (flags & YESCRYPT_RW) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags & YESCRYPT_RW) { 26 2 28
1255 26 size_t S_size = (size_t)Salloc * p;
1256 26 need += S_size;
1257 26 ▶ if (need < S_size)
Branch: 1 branch
BranchTrueFalseTotal
if (need < S_size) 0 26 26
1258 0 goto out_EINVAL;
1259 }
1260 28 ▶ if (flags & YESCRYPT_INIT_SHARED) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags & YESCRYPT_INIT_SHARED) { 0 28 28
1261 0 ▶ if (!alloc_region(&tmp, need))
Branch: 1 branch
BranchTrueFalseTotal
if (!alloc_region(&tmp, need)) 0 0 0
1262 0 return -1;
1263 0 B = (uint8_t *)tmp.aligned;
1264 0 XY = (salsa20_blk_t *)((uint8_t *)B + B_size);
1265 } else {
1266 28 init_region(&tmp);
1267 28 ▶ if (local->aligned_size < need) {
Branch: 1 branch
BranchTrueFalseTotal
if (local->aligned_size < need) { 20 8 28
1268 20 ▶ if (free_region(local))
Branch: 1 branch
BranchTrueFalseTotal
if (free_region(local)) 0 20 20
1269 0 return -1;
1270 20 ▶ if (!alloc_region(local, need))
Branch: 1 branch
BranchTrueFalseTotal
if (!alloc_region(local, need)) 0 20 20
1271 0 return -1;
1272 }
1273 28 ▶ if (flags & YESCRYPT_ALLOC_ONLY)
Branch: 1 branch
BranchTrueFalseTotal
if (flags & YESCRYPT_ALLOC_ONLY) 4 24 28
1274 4 return -3; /* expected "failure" */
1275 24 B = (uint8_t *)local->aligned;
1276 24 V = (salsa20_blk_t *)((uint8_t *)B + B_size);
1277 24 XY = (salsa20_blk_t *)((uint8_t *)V + V_size);
1278 }
1279 24 S = NULL;
1280 24 ▶ if (flags & YESCRYPT_RW)
Branch: 1 branch
BranchTrueFalseTotal
if (flags & YESCRYPT_RW) 22 2 24
1281 22 S = (uint8_t *)XY + XY_size;
1282  
1283 24 ▶ if (flags) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags) { 23 1 24
1284 23 HMAC_SHA256_Buf("yescrypt-prehash",
1285 23 ▶ (flags & YESCRYPT_PREHASH) ? 16 : 8,
Branch: 1 branch
BranchTrueFalseTotal
(flags & YESCRYPT_PREHASH) ? 16 : 8, 4 19 23
1286 passwd, passwdlen, sha256);
1287 23 passwd = sha256;
1288 23 passwdlen = sizeof(sha256);
1289 }
1290  
1291 24 PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, 1, B, B_size);
1292  
1293 24 ▶ if (flags)
Branch: 1 branch
BranchTrueFalseTotal
if (flags) 23 1 24
1294 23 memcpy(sha256, B, sizeof(sha256));
1295  
1296 24 ▶ if (p == 1 || (flags & YESCRYPT_RW)) {
Branch: 2 branches
BranchTrueFalseTotal
if (p == 1 || (flags & YESCRYPT_RW)) { 0 24 24
if (p == 1 || (flags & YESCRYPT_RW)) { 0 0 0
1297 24 smix(B, r, N, p, t, flags, V, NROM, VROM, XY, S, sha256);
1298 } else {
1299 uint32_t i;
1300 #ifdef _OPENMP
1301 #pragma omp parallel for default(none) private(i) shared(B, r, N, p, t, flags, V, NROM, VROM, XY, S)
1302 #endif
1303 0 ▶ for (i = 0; i < p; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < p; i++) { 0 0 0
1304 #ifdef _OPENMP
1305 smix(&B[(size_t)128 * r * i], r, N, 1, t, flags,
1306 &V[(size_t)2 * r * i * N],
1307 NROM, VROM,
1308 &XY[(size_t)4 * r * i], NULL, NULL);
1309 #else
1310 0 smix(&B[(size_t)128 * r * i], r, N, 1, t, flags, V,
1311 NROM, VROM, XY, NULL, NULL);
1312 #endif
1313 }
1314 }
1315  
1316 24 dkp = buf;
1317 24 ▶ if (flags && buflen < sizeof(dk)) {
Branch: 2 branches
BranchTrueFalseTotal
if (flags && buflen < sizeof(dk)) { 23 1 24
if (flags && buflen < sizeof(dk)) { 0 23 23
1318 0 PBKDF2_SHA256(passwd, passwdlen, B, B_size, 1, dk, sizeof(dk));
1319 0 dkp = dk;
1320 }
1321  
1322 24 PBKDF2_SHA256(passwd, passwdlen, B, B_size, 1, buf, buflen);
1323  
1324 /*
1325 * Except when computing classic scrypt, allow all computation so far
1326 * to be performed on the client. The final steps below match those of
1327 * SCRAM (RFC 5802), so that an extension of SCRAM (with the steps so
1328 * far in place of SCRAM's use of PBKDF2 and with SHA-256 in place of
1329 * SCRAM's use of SHA-1) would be usable with yescrypt hashes.
1330 */
1331 24 ▶ if (flags && !(flags & YESCRYPT_PREHASH)) {
Branch: 2 branches
BranchTrueFalseTotal
if (flags && !(flags & YESCRYPT_PREHASH)) { 23 1 24
if (flags && !(flags & YESCRYPT_PREHASH)) { 19 4 23
1332 /* Compute ClientKey */
1333 19 HMAC_SHA256_Buf(dkp, sizeof(dk), "Client Key", 10, sha256);
1334 /* Compute StoredKey */
1335 {
1336 19 size_t clen = buflen;
1337 19 ▶ if (clen > sizeof(dk))
Branch: 1 branch
BranchTrueFalseTotal
if (clen > sizeof(dk)) 0 19 19
1338 0 clen = sizeof(dk);
1339 19 SHA256_Buf(sha256, sizeof(sha256), dk);
1340 19 memcpy(buf, dk, clen);
1341 }
1342 }
1343  
1344 24 ▶ if (flags) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags) { 23 1 24
1345 23 insecure_memzero(sha256, sizeof(sha256));
1346 23 insecure_memzero(dk, sizeof(dk));
1347 }
1348  
1349 24 ▶ if (free_region(&tmp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (free_region(&tmp)) { 0 24 24
1350 0 insecure_memzero(buf, buflen); /* must preserve errno */
1351 0 return -1;
1352 }
1353  
1354 /* Success! */
1355 24 return 0;
1356  
1357 0 out_EINVAL:
1358 0 errno = EINVAL;
1359 0 return -1;
1360 }
1361  
1362 /**
1363 * yescrypt_kdf(shared, local, passwd, passwdlen, salt, saltlen, params,
1364 * buf, buflen):
1365 * Compute scrypt or its revision as requested by the parameters. The inputs
1366 * to this function are the same as those for yescrypt_kdf_body() above, with
1367 * the addition of g, which controls hash upgrades (0 for no upgrades so far).
1368 */
1369 20 int yescrypt_kdf(const yescrypt_shared_t *shared, yescrypt_local_t *local,
1370 const uint8_t *passwd, size_t passwdlen,
1371 const uint8_t *salt, size_t saltlen,
1372 const yescrypt_params_t *params,
1373 uint8_t *buf, size_t buflen)
1374 {
1375 20 yescrypt_flags_t flags = params->flags;
1376 20 uint64_t N = params->N;
1377 20 uint32_t r = params->r;
1378 20 uint32_t p = params->p;
1379 20 uint32_t t = params->t;
1380 20 uint32_t g = params->g;
1381 20 uint64_t NROM = params->NROM;
1382 uint8_t dk[32];
1383 int retval;
1384  
1385 /* Support for hash upgrades has been temporarily removed */
1386 20 ▶ if (g) {
Branch: 1 branch
BranchTrueFalseTotal
if (g) { 0 20 20
1387 0 errno = EINVAL;
1388 0 return -1;
1389 }
1390  
1391 20 ▶ if ((flags & (YESCRYPT_RW | YESCRYPT_INIT_SHARED)) == YESCRYPT_RW &&
Branch: 2 branches
BranchTrueFalseTotal
if ((flags & (YESCRYPT_RW | YESCRYPT_INIT_SHARED)) == YESCRYPT_RW && 18 2 20
if ((flags & (YESCRYPT_RW | YESCRYPT_INIT_SHARED)) == YESCRYPT_RW && 18 0 18
1392 18 ▶ p >= 1 && N / p >= 0x100 && N / p * r >= 0x20000) {
Branch: 2 branches
BranchTrueFalseTotal
p >= 1 && N / p >= 0x100 && N / p * r >= 0x20000) { 17 1 18
p >= 1 && N / p >= 0x100 && N / p * r >= 0x20000) { 4 13 17
1393 4 ▶ if (yescrypt_kdf_body(shared, local,
Branch: 1 branch
BranchTrueFalseTotal
if (yescrypt_kdf_body(shared, local, 0 4 4
1394 passwd, passwdlen, salt, saltlen,
1395 flags | YESCRYPT_ALLOC_ONLY, N, r, p, t, NROM,
1396 buf, buflen) != -3) {
1397 0 errno = EINVAL;
1398 0 return -1;
1399 }
1400 4 ▶ if ((retval = yescrypt_kdf_body(shared, local,
Branch: 1 branch
BranchTrueFalseTotal
if ((retval = yescrypt_kdf_body(shared, local, 0 4 4
1401 passwd, passwdlen, salt, saltlen,
1402 flags | YESCRYPT_PREHASH, N >> 6, r, p, 0, NROM,
1403 dk, sizeof(dk))))
1404 0 return retval;
1405 4 passwd = dk;
1406 4 passwdlen = sizeof(dk);
1407 }
1408  
1409 20 retval = yescrypt_kdf_body(shared, local,
1410 passwd, passwdlen, salt, saltlen,
1411 flags, N, r, p, t, NROM, buf, buflen);
1412 #ifndef SKIP_MEMZERO
1413 20 ▶ if (passwd == dk)
Branch: 1 branch
BranchTrueFalseTotal
if (passwd == dk) 4 16 20
1414 4 insecure_memzero(dk, sizeof(dk));
1415 #endif
1416 20 return retval;
1417 }
1418  
1419 0 int yescrypt_init_shared(yescrypt_shared_t *shared,
1420 const uint8_t *seed, size_t seedlen,
1421 const yescrypt_params_t *params)
1422 {
1423 yescrypt_params_t subparams;
1424 yescrypt_shared_t half1, half2;
1425 uint8_t salt[32];
1426 uint64_t *tag;
1427  
1428 0 subparams = *params;
1429 0 subparams.flags |= YESCRYPT_INIT_SHARED;
1430 0 subparams.N = params->NROM;
1431 0 subparams.NROM = 0;
1432  
1433 0 ▶ if (!(params->flags & YESCRYPT_RW) || params->N || params->g)
Branch: 3 branches
BranchTrueFalseTotal
if (!(params->flags & YESCRYPT_RW) || params->N || params->g) 0 0 0
if (!(params->flags & YESCRYPT_RW) || params->N || params->g) 0 0 0
if (!(params->flags & YESCRYPT_RW) || params->N || params->g) 0 0 0
1434 0 return -1;
1435  
1436 0 ▶ if (params->flags & YESCRYPT_SHARED_PREALLOCATED) {
Branch: 1 branch
BranchTrueFalseTotal
if (params->flags & YESCRYPT_SHARED_PREALLOCATED) { 0 0 0
1437 0 ▶ if (!shared->aligned || !shared->aligned_size)
Branch: 2 branches
BranchTrueFalseTotal
if (!shared->aligned || !shared->aligned_size) 0 0 0
if (!shared->aligned || !shared->aligned_size) 0 0 0
1438 0 return -1;
1439  
1440 /* Overwrite a possible old ROM tag before we overwrite the rest */
1441 0 tag = (uint64_t *)
1442 0 ((uint8_t *)shared->aligned + shared->aligned_size - 48);
1443 0 memset(tag, 0, 48);
1444 } else {
1445 0 init_region(shared);
1446  
1447 0 subparams.flags |= YESCRYPT_ALLOC_ONLY;
1448 0 ▶ if (yescrypt_kdf(NULL, shared, NULL, 0, NULL, 0, &subparams,
Branch: 1 branch
BranchTrueFalseTotal
if (yescrypt_kdf(NULL, shared, NULL, 0, NULL, 0, &subparams, 0 0 0
1449 0 ▶ NULL, 0) != -2 || !shared->aligned)
Branch: 1 branch
BranchTrueFalseTotal
NULL, 0) != -2 || !shared->aligned) 0 0 0
1450 0 return -1;
1451 0 subparams.flags -= YESCRYPT_ALLOC_ONLY;
1452 }
1453  
1454 0 subparams.N /= 2;
1455  
1456 0 half1 = *shared;
1457 0 half1.aligned_size /= 2;
1458 0 half2 = half1;
1459 0 half2.aligned = (uint8_t *)half2.aligned + half1.aligned_size;
1460  
1461 0 ▶ if (yescrypt_kdf(NULL, &half1,
Branch: 1 branch
BranchTrueFalseTotal
if (yescrypt_kdf(NULL, &half1, 0 0 0
1462 seed, seedlen, (const uint8_t *)"yescrypt-ROMhash", 16, &subparams,
1463 salt, sizeof(salt)))
1464 0 goto fail;
1465  
1466 0 subparams.NROM = subparams.N;
1467  
1468 0 ▶ if (yescrypt_kdf(&half1, &half2,
Branch: 1 branch
BranchTrueFalseTotal
if (yescrypt_kdf(&half1, &half2, 0 0 0
1469 seed, seedlen, salt, sizeof(salt), &subparams, salt, sizeof(salt)))
1470 0 goto fail;
1471  
1472 0 ▶ if (yescrypt_kdf(&half2, &half1,
Branch: 1 branch
BranchTrueFalseTotal
if (yescrypt_kdf(&half2, &half1, 0 0 0
1473 seed, seedlen, salt, sizeof(salt), &subparams, salt, sizeof(salt)))
1474 0 goto fail;
1475  
1476 0 tag = (uint64_t *)
1477 0 ((uint8_t *)shared->aligned + shared->aligned_size - 48);
1478 0 tag[0] = YESCRYPT_ROM_TAG1;
1479 0 tag[1] = YESCRYPT_ROM_TAG2;
1480 0 tag[2] = le64dec(salt);
1481 0 tag[3] = le64dec(salt + 8);
1482 0 tag[4] = le64dec(salt + 16);
1483 0 tag[5] = le64dec(salt + 24);
1484  
1485 0 insecure_memzero(salt, sizeof(salt));
1486 0 return 0;
1487  
1488 0 fail:
1489 0 insecure_memzero(salt, sizeof(salt));
1490 0 ▶ if (!(params->flags & YESCRYPT_SHARED_PREALLOCATED))
Branch: 1 branch
BranchTrueFalseTotal
if (!(params->flags & YESCRYPT_SHARED_PREALLOCATED)) 0 0 0
1491 0 free_region(shared);
1492 0 return -1;
1493 }
1494  
1495 0 yescrypt_binary_t *yescrypt_digest_shared(yescrypt_shared_t *shared)
1496 {
1497 static yescrypt_binary_t digest;
1498 uint64_t *tag;
1499  
1500 0 ▶ if (shared->aligned_size < 48)
Branch: 1 branch
BranchTrueFalseTotal
if (shared->aligned_size < 48) 0 0 0
1501 0 return NULL;
1502  
1503 0 tag = (uint64_t *)
1504 0 ((uint8_t *)shared->aligned + shared->aligned_size - 48);
1505  
1506 0 ▶ if (tag[0] != YESCRYPT_ROM_TAG1 || tag[1] != YESCRYPT_ROM_TAG2)
Branch: 2 branches
BranchTrueFalseTotal
if (tag[0] != YESCRYPT_ROM_TAG1 || tag[1] != YESCRYPT_ROM_TAG2) 0 0 0
if (tag[0] != YESCRYPT_ROM_TAG1 || tag[1] != YESCRYPT_ROM_TAG2) 0 0 0
1507 0 return NULL;
1508  
1509 0 le64enc(digest.uc, tag[2]);
1510 0 le64enc(digest.uc + 8, tag[3]);
1511 0 le64enc(digest.uc + 16, tag[4]);
1512 0 le64enc(digest.uc + 24, tag[5]);
1513  
1514 0 return &digest;
1515 }
1516  
1517 0 int yescrypt_free_shared(yescrypt_shared_t *shared)
1518 {
1519 0 return free_region(shared);
1520 }
1521  
1522 36 int yescrypt_init_local(yescrypt_local_t *local)
1523 {
1524 36 init_region(local);
1525 36 return 0;
1526 }
1527  
1528 36 int yescrypt_free_local(yescrypt_local_t *local)
1529 {
1530 36 return free_region(local);
1531 }
« yescrypt-common.c ↑ Summary sha256.c »