2 * Copyright 2009 Colin Percival
3 * Copyright 2012,2013 Alexander Peslyak
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
27 * This file was originally written by Colin Percival as part of the Tarsnap
28 * online backup system.
31 #if defined(HAVE_EMMINTRIN_H) || defined(_MSC_VER)
33 # pragma GCC target("sse2")
35 #include <emmintrin.h>
36 #if defined(__XOP__) && defined(DISABLED)
37 # include <x86intrin.h>
46 #include "../pbkdf2-sha256.h"
47 #include "../sysendian.h"
48 #include "../crypto_scrypt.h"
50 #if defined(__XOP__) && defined(DISABLED)
51 #define ARX(out, in1, in2, s) \
52 out = _mm_xor_si128(out, _mm_roti_epi32(_mm_add_epi32(in1, in2), s));
54 #define ARX(out, in1, in2, s) \
56 __m128i T = _mm_add_epi32(in1, in2); \
57 out = _mm_xor_si128(out, _mm_slli_epi32(T, s)); \
58 out = _mm_xor_si128(out, _mm_srli_epi32(T, 32-s)); \
62 #define SALSA20_2ROUNDS \
63 /* Operate on "columns". */ \
69 /* Rearrange data. */ \
70 X1 = _mm_shuffle_epi32(X1, 0x93); \
71 X2 = _mm_shuffle_epi32(X2, 0x4E); \
72 X3 = _mm_shuffle_epi32(X3, 0x39); \
74 /* Operate on "rows". */ \
80 /* Rearrange data. */ \
81 X1 = _mm_shuffle_epi32(X1, 0x39); \
82 X2 = _mm_shuffle_epi32(X2, 0x4E); \
83 X3 = _mm_shuffle_epi32(X3, 0x93);
86 * Apply the salsa20/8 core to the block provided in (X0 ... X3) ^ (Z0 ... Z3).
88 #define SALSA20_8_XOR(in, out) \
90 __m128i Y0 = X0 = _mm_xor_si128(X0, (in)[0]); \
91 __m128i Y1 = X1 = _mm_xor_si128(X1, (in)[1]); \
92 __m128i Y2 = X2 = _mm_xor_si128(X2, (in)[2]); \
93 __m128i Y3 = X3 = _mm_xor_si128(X3, (in)[3]); \
98 (out)[0] = X0 = _mm_add_epi32(X0, Y0); \
99 (out)[1] = X1 = _mm_add_epi32(X1, Y1); \
100 (out)[2] = X2 = _mm_add_epi32(X2, Y2); \
101 (out)[3] = X3 = _mm_add_epi32(X3, Y3); \
105 * blockmix_salsa8(Bin, Bout, r):
106 * Compute Bout = BlockMix_{salsa20/8, r}(Bin). The input Bin must be 128r
107 * bytes in length; the output Bout must also be the same size.
110 blockmix_salsa8(const __m128i * Bin, __m128i * Bout, size_t r)
112 __m128i X0, X1, X2, X3;
115 /* 1: X <-- B_{2r - 1} */
121 /* 3: X <-- H(X \xor B_i) */
123 /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
124 SALSA20_8_XOR(Bin, Bout)
126 /* 2: for i = 0 to 2r - 1 do */
128 for (i = 0; i < r;) {
129 /* 3: X <-- H(X \xor B_i) */
131 /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
132 SALSA20_8_XOR(&Bin[i * 8 + 4], &Bout[(r + i) * 4 + 4])
136 /* 3: X <-- H(X \xor B_i) */
138 /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
139 SALSA20_8_XOR(&Bin[i * 8], &Bout[i * 4])
142 /* 3: X <-- H(X \xor B_i) */
144 /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
145 SALSA20_8_XOR(&Bin[i * 8 + 4], &Bout[(r + i) * 4 + 4])
149 X0 = _mm_xor_si128(X0, (in)[0]); \
150 X1 = _mm_xor_si128(X1, (in)[1]); \
151 X2 = _mm_xor_si128(X2, (in)[2]); \
152 X3 = _mm_xor_si128(X3, (in)[3]);
154 #define XOR4_2(in1, in2) \
155 X0 = _mm_xor_si128((in1)[0], (in2)[0]); \
156 X1 = _mm_xor_si128((in1)[1], (in2)[1]); \
157 X2 = _mm_xor_si128((in1)[2], (in2)[2]); \
158 X3 = _mm_xor_si128((in1)[3], (in2)[3]);
160 static inline uint32_t
161 blockmix_salsa8_xor(const __m128i * Bin1, const __m128i * Bin2, __m128i * Bout,
164 __m128i X0, X1, X2, X3;
167 /* 1: X <-- B_{2r - 1} */
168 XOR4_2(&Bin1[8 * r - 4], &Bin2[8 * r - 4])
170 /* 3: X <-- H(X \xor B_i) */
172 /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
174 SALSA20_8_XOR(Bin2, Bout)
176 /* 2: for i = 0 to 2r - 1 do */
178 for (i = 0; i < r;) {
179 /* 3: X <-- H(X \xor B_i) */
181 /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
182 XOR4(&Bin1[i * 8 + 4])
183 SALSA20_8_XOR(&Bin2[i * 8 + 4], &Bout[(r + i) * 4 + 4])
187 /* 3: X <-- H(X \xor B_i) */
189 /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
191 SALSA20_8_XOR(&Bin2[i * 8], &Bout[i * 4])
194 /* 3: X <-- H(X \xor B_i) */
196 /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
197 XOR4(&Bin1[i * 8 + 4])
198 SALSA20_8_XOR(&Bin2[i * 8 + 4], &Bout[(r + i) * 4 + 4])
200 return _mm_cvtsi128_si32(X0);
204 #undef SALSA20_2ROUNDS
211 * Return the result of parsing B_{2r-1} as a little-endian integer.
213 static inline uint32_t
214 integerify(const void * B, size_t r)
216 return *(const uint32_t *)((uintptr_t)(B) + (2 * r - 1) * 64);
220 * smix(B, r, N, V, XY):
221 * Compute B = SMix_r(B, N). The input B must be 128r bytes in length;
222 * the temporary storage V must be 128rN bytes in length; the temporary
223 * storage XY must be 256r + 64 bytes in length. The value N must be a
224 * power of 2 greater than 1. The arrays B, V, and XY must be aligned to a
225 * multiple of 64 bytes.
228 smix(uint8_t * B, size_t r, uint32_t N, void * V, void * XY)
231 __m128i * X = (__m128i *) V, * Y;
232 uint32_t * X32 = (uint32_t *) V;
238 for (k = 0; k < 2 * r; k++) {
239 for (i = 0; i < 16; i++) {
241 le32dec(&B[(k * 16 + (i * 5 % 16)) * 4]);
245 /* 2: for i = 0 to N - 1 do */
246 for (i = 1; i < N - 1; i += 2) {
249 Y = (__m128i *)((uintptr_t)(V) + i * s);
250 blockmix_salsa8(X, Y, r);
254 X = (__m128i *)((uintptr_t)(V) + (i + 1) * s);
255 blockmix_salsa8(Y, X, r);
260 Y = (__m128i *)((uintptr_t)(V) + i * s);
261 blockmix_salsa8(X, Y, r);
266 blockmix_salsa8(Y, X, r);
268 X32 = (uint32_t *) XY;
269 Y = (__m128i *)((uintptr_t)(XY) + s);
271 /* 7: j <-- Integerify(X) mod N */
272 j = integerify(X, r) & (N - 1);
274 /* 6: for i = 0 to N - 1 do */
275 for (i = 0; i < N; i += 2) {
276 __m128i * V_j = (__m128i *)((uintptr_t)(V) + j * s);
278 /* 8: X <-- H(X \xor V_j) */
279 /* 7: j <-- Integerify(X) mod N */
280 j = blockmix_salsa8_xor(X, V_j, Y, r) & (N - 1);
281 V_j = (__m128i *)((uintptr_t)(V) + j * s);
283 /* 8: X <-- H(X \xor V_j) */
284 /* 7: j <-- Integerify(X) mod N */
285 j = blockmix_salsa8_xor(Y, V_j, X, r) & (N - 1);
289 for (k = 0; k < 2 * r; k++) {
290 for (i = 0; i < 16; i++) {
291 le32enc(&B[(k * 16 + (i * 5 % 16)) * 4],
298 * escrypt_kdf(local, passwd, passwdlen, salt, saltlen,
299 * N, r, p, buf, buflen):
300 * Compute scrypt(passwd[0 .. passwdlen - 1], salt[0 .. saltlen - 1], N, r,
301 * p, buflen) and write the result into buf. The parameters r, p, and buflen
302 * must satisfy r * p < 2^30 and buflen <= (2^32 - 1) * 32. The parameter N
303 * must be a power of 2 greater than 1.
305 * Return 0 on success; or -1 on error.
308 escrypt_kdf_sse(escrypt_local_t * local,
309 const uint8_t * passwd, size_t passwdlen,
310 const uint8_t * salt, size_t saltlen,
311 uint64_t N, uint32_t _r, uint32_t _p,
312 uint8_t * buf, size_t buflen)
314 size_t B_size, V_size, XY_size, need;
317 size_t r = _r, p = _p;
320 /* Sanity-check parameters. */
321 #if SIZE_MAX > UINT32_MAX
322 if (buflen > (((uint64_t)(1) << 32) - 1) * 32) {
327 if ((uint64_t)(r) * (uint64_t)(p) >= (1 << 30)) {
331 if (N > UINT32_MAX) {
335 if (((N & (N - 1)) != 0) || (N < 2)) {
339 if (r == 0 || p == 0) {
343 if ((r > SIZE_MAX / 128 / p) ||
344 #if SIZE_MAX / 256 <= UINT32_MAX
345 (r > SIZE_MAX / 256) ||
347 (N > SIZE_MAX / 128 / r)) {
352 /* Allocate memory. */
353 B_size = (size_t)128 * r * p;
354 V_size = (size_t)128 * r * N;
355 need = B_size + V_size;
360 XY_size = (size_t)256 * r + 64;
362 if (need < XY_size) {
366 if (local->size < need) {
367 if (free_region(local))
368 return -1; /* LCOV_EXCL_LINE */
369 if (!alloc_region(local, need))
370 return -1; /* LCOV_EXCL_LINE */
372 B = (uint8_t *)local->aligned;
373 V = (uint32_t *)((uint8_t *)B + B_size);
374 XY = (uint32_t *)((uint8_t *)V + V_size);
376 /* 1: (B_0 ... B_{p-1}) <-- PBKDF2(P, S, 1, p * MFLen) */
377 PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, 1, B, B_size);
379 /* 2: for i = 0 to p - 1 do */
380 for (i = 0; i < p; i++) {
381 /* 3: B_i <-- MF(B_i, N) */
382 smix(&B[(size_t)128 * i * r], r, (uint32_t) N, V, XY);
385 /* 5: DK <-- PBKDF2(P, B, 1, dkLen) */
386 PBKDF2_SHA256(passwd, passwdlen, B, B_size, 1, buf, buflen);