2 * Copyright 2009 Colin Percival
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * This file was originally written by Colin Percival as part of the Tarsnap
27 * online backup system.
29 #include "scrypt_platform.h"
31 #include <sys/types.h>
40 #include "sysendian.h"
42 #include "crypto_scrypt.h"
44 static void blkcpy(void *, void *, size_t);
45 static void blkxor(void *, void *, size_t);
46 static void salsa20_8(uint32_t[16]);
47 static void blockmix_salsa8(uint32_t *, uint32_t *, uint32_t *, size_t);
48 static uint64_t integerify(void *, size_t);
49 static void smix(uint8_t *, size_t, uint64_t, uint32_t *, uint32_t *);
52 blkcpy(void * dest, void * src, size_t len)
56 size_t L = len / sizeof(size_t);
59 for (i = 0; i < L; i++)
64 blkxor(void * dest, void * src, size_t len)
68 size_t L = len / sizeof(size_t);
71 for (i = 0; i < L; i++)
77 * Apply the salsa20/8 core to the provided block.
80 salsa20_8(uint32_t B[16])
86 for (i = 0; i < 8; i += 2) {
87 #define R(a,b) (((a) << (b)) | ((a) >> (32 - (b))))
88 /* Operate on columns. */
89 x[ 4] ^= R(x[ 0]+x[12], 7); x[ 8] ^= R(x[ 4]+x[ 0], 9);
90 x[12] ^= R(x[ 8]+x[ 4],13); x[ 0] ^= R(x[12]+x[ 8],18);
92 x[ 9] ^= R(x[ 5]+x[ 1], 7); x[13] ^= R(x[ 9]+x[ 5], 9);
93 x[ 1] ^= R(x[13]+x[ 9],13); x[ 5] ^= R(x[ 1]+x[13],18);
95 x[14] ^= R(x[10]+x[ 6], 7); x[ 2] ^= R(x[14]+x[10], 9);
96 x[ 6] ^= R(x[ 2]+x[14],13); x[10] ^= R(x[ 6]+x[ 2],18);
98 x[ 3] ^= R(x[15]+x[11], 7); x[ 7] ^= R(x[ 3]+x[15], 9);
99 x[11] ^= R(x[ 7]+x[ 3],13); x[15] ^= R(x[11]+x[ 7],18);
101 /* Operate on rows. */
102 x[ 1] ^= R(x[ 0]+x[ 3], 7); x[ 2] ^= R(x[ 1]+x[ 0], 9);
103 x[ 3] ^= R(x[ 2]+x[ 1],13); x[ 0] ^= R(x[ 3]+x[ 2],18);
105 x[ 6] ^= R(x[ 5]+x[ 4], 7); x[ 7] ^= R(x[ 6]+x[ 5], 9);
106 x[ 4] ^= R(x[ 7]+x[ 6],13); x[ 5] ^= R(x[ 4]+x[ 7],18);
108 x[11] ^= R(x[10]+x[ 9], 7); x[ 8] ^= R(x[11]+x[10], 9);
109 x[ 9] ^= R(x[ 8]+x[11],13); x[10] ^= R(x[ 9]+x[ 8],18);
111 x[12] ^= R(x[15]+x[14], 7); x[13] ^= R(x[12]+x[15], 9);
112 x[14] ^= R(x[13]+x[12],13); x[15] ^= R(x[14]+x[13],18);
115 for (i = 0; i < 16; i++)
120 * blockmix_salsa8(Bin, Bout, X, r):
121 * Compute Bout = BlockMix_{salsa20/8, r}(Bin). The input Bin must be 128r
122 * bytes in length; the output Bout must also be the same size. The
123 * temporary space X must be 64 bytes.
126 blockmix_salsa8(uint32_t * Bin, uint32_t * Bout, uint32_t * X, size_t r)
130 /* 1: X <-- B_{2r - 1} */
131 blkcpy(X, &Bin[(2 * r - 1) * 16], 64);
133 /* 2: for i = 0 to 2r - 1 do */
134 for (i = 0; i < 2 * r; i += 2) {
135 /* 3: X <-- H(X \xor B_i) */
136 blkxor(X, &Bin[i * 16], 64);
140 /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
141 blkcpy(&Bout[i * 8], X, 64);
143 /* 3: X <-- H(X \xor B_i) */
144 blkxor(X, &Bin[i * 16 + 16], 64);
148 /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
149 blkcpy(&Bout[i * 8 + r * 16], X, 64);
155 * Return the result of parsing B_{2r-1} as a little-endian integer.
158 integerify(void * B, size_t r)
160 uint32_t * X = (void *)((uintptr_t)(B) + (2 * r - 1) * 64);
162 return (((uint64_t)(X[1]) << 32) + X[0]);
166 * smix(B, r, N, V, XY):
167 * Compute B = SMix_r(B, N). The input B must be 128r bytes in length;
168 * the temporary storage V must be 128rN bytes in length; the temporary
169 * storage XY must be 256r + 64 bytes in length. The value N must be a
170 * power of 2 greater than 1. The arrays B, V, and XY must be aligned to a
171 * multiple of 64 bytes.
174 smix(uint8_t * B, size_t r, uint64_t N, uint32_t * V, uint32_t * XY)
177 uint32_t * Y = &XY[32 * r];
178 uint32_t * Z = &XY[64 * r];
184 for (k = 0; k < 32 * r; k++)
185 X[k] = le32dec(&B[4 * k]);
187 /* 2: for i = 0 to N - 1 do */
188 for (i = 0; i < N; i += 2) {
190 blkcpy(&V[i * (32 * r)], X, 128 * r);
193 blockmix_salsa8(X, Y, Z, r);
196 blkcpy(&V[(i + 1) * (32 * r)], Y, 128 * r);
199 blockmix_salsa8(Y, X, Z, r);
202 /* 6: for i = 0 to N - 1 do */
203 for (i = 0; i < N; i += 2) {
204 /* 7: j <-- Integerify(X) mod N */
205 j = integerify(X, r) & (N - 1);
207 /* 8: X <-- H(X \xor V_j) */
208 blkxor(X, &V[j * (32 * r)], 128 * r);
209 blockmix_salsa8(X, Y, Z, r);
211 /* 7: j <-- Integerify(X) mod N */
212 j = integerify(Y, r) & (N - 1);
214 /* 8: X <-- H(X \xor V_j) */
215 blkxor(Y, &V[j * (32 * r)], 128 * r);
216 blockmix_salsa8(Y, X, Z, r);
220 for (k = 0; k < 32 * r; k++)
221 le32enc(&B[4 * k], X[k]);
225 * crypto_scrypt(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen):
226 * Compute scrypt(passwd[0 .. passwdlen - 1], salt[0 .. saltlen - 1], N, r,
227 * p, buflen) and write the result into buf. The parameters r, p, and buflen
228 * must satisfy r * p < 2^30 and buflen <= (2^32 - 1) * 32. The parameter N
229 * must be a power of 2 greater than 1.
231 * Return 0 on success; or -1 on error.
234 crypto_scrypt(const uint8_t * passwd, size_t passwdlen,
235 const uint8_t * salt, size_t saltlen, uint64_t N, uint32_t r, uint32_t p,
236 uint8_t * buf, size_t buflen)
238 void * B0, * V0, * XY0;
244 /* Sanity-check parameters. */
245 #if SIZE_MAX > UINT32_MAX
246 if (buflen > (((uint64_t)(1) << 32) - 1) * 32) {
251 if ((uint64_t)(r) * (uint64_t)(p) >= (1 << 30)) {
255 if (((N & (N - 1)) != 0) || (N == 0)) {
259 if ((r > SIZE_MAX / 128 / p) ||
260 #if SIZE_MAX / 256 <= UINT32_MAX
261 (r > SIZE_MAX / 256) ||
263 (N > SIZE_MAX / 128 / r)) {
268 /* Allocate memory. */
269 #ifdef HAVE_POSIX_MEMALIGN
270 if ((errno = posix_memalign(&B0, 64, 128 * r * p)) != 0)
273 if ((errno = posix_memalign(&XY0, 64, 256 * r + 64)) != 0)
275 XY = (uint32_t *)(XY0);
277 if ((errno = posix_memalign(&V0, 64, 128 * r * N)) != 0)
279 V = (uint32_t *)(V0);
282 if ((B0 = malloc(128 * r * p + 63)) == NULL)
284 B = (uint8_t *)(((uintptr_t)(B0) + 63) & ~ (uintptr_t)(63));
285 if ((XY0 = malloc(256 * r + 64 + 63)) == NULL)
287 XY = (uint32_t *)(((uintptr_t)(XY0) + 63) & ~ (uintptr_t)(63));
289 if ((V0 = malloc(128 * r * N + 63)) == NULL)
291 V = (uint32_t *)(((uintptr_t)(V0) + 63) & ~ (uintptr_t)(63));
295 if ((V0 = mmap(NULL, 128 * r * N, PROT_READ | PROT_WRITE,
297 MAP_ANON | MAP_PRIVATE | MAP_NOCORE,
299 MAP_ANON | MAP_PRIVATE,
301 -1, 0)) == MAP_FAILED)
303 V = (uint32_t *)(V0);
306 /* 1: (B_0 ... B_{p-1}) <-- PBKDF2(P, S, 1, p * MFLen) */
307 PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, 1, B, p * 128 * r);
309 /* 2: for i = 0 to p - 1 do */
310 for (i = 0; i < p; i++) {
311 /* 3: B_i <-- MF(B_i, N) */
312 smix(&B[i * 128 * r], r, N, V, XY);
315 /* 5: DK <-- PBKDF2(P, B, 1, dkLen) */
316 PBKDF2_SHA256(passwd, passwdlen, B, p * 128 * r, 1, buf, buflen);
320 if (munmap(V0, 128 * r * N))