1 /*-
2  * Copyright 2009 Colin Percival
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * This file was originally written by Colin Percival as part of the Tarsnap
27  * online backup system.
28  */
29 #include "scrypt_platform.h"
30 
31 #include <arm_neon.h>
32 
33 #include <errno.h>
34 #include <stdint.h>
35 #include <limits.h>
36 #include <stdlib.h>
37 #include <string.h>
38 
39 #ifdef USE_OPENSSL_PBKDF2
40 #include <openssl/evp.h>
41 #else
42 #include "sha256.h"
43 #endif
44 #include "sysendian.h"
45 
46 #include "crypto_scrypt.h"
47 
48 #include "crypto_scrypt-neon-salsa208.h"
49 
50 static void blkcpy(void *, void *, size_t);
51 static void blkxor(void *, void *, size_t);
52 void crypto_core_salsa208_armneon2(void *);
53 static void blockmix_salsa8(uint8x16_t *, uint8x16_t *, uint8x16_t *, size_t);
54 static uint64_t integerify(void *, size_t);
55 static void smix(uint8_t *, size_t, uint64_t, void *, void *);
56 
57 static void
blkcpy(void * dest,void * src,size_t len)58 blkcpy(void * dest, void * src, size_t len)
59 {
60 	uint8x16_t * D = dest;
61 	uint8x16_t * S = src;
62 	size_t L = len / 16;
63 	size_t i;
64 
65 	for (i = 0; i < L; i++)
66 		D[i] = S[i];
67 }
68 
69 static void
blkxor(void * dest,void * src,size_t len)70 blkxor(void * dest, void * src, size_t len)
71 {
72 	uint8x16_t * D = dest;
73 	uint8x16_t * S = src;
74 	size_t L = len / 16;
75 	size_t i;
76 
77 	for (i = 0; i < L; i++)
78 		D[i] = veorq_u8(D[i], S[i]);
79 }
80 
81 /**
82  * blockmix_salsa8(B, Y, r):
83  * Compute B = BlockMix_{salsa20/8, r}(B).  The input B must be 128r bytes in
84  * length; the temporary space Y must also be the same size.
85  */
86 static void
blockmix_salsa8(uint8x16_t * Bin,uint8x16_t * Bout,uint8x16_t * X,size_t r)87 blockmix_salsa8(uint8x16_t * Bin, uint8x16_t * Bout, uint8x16_t * X, size_t r)
88 {
89 	size_t i;
90 
91 	/* 1: X <-- B_{2r - 1} */
92 	blkcpy(X, &Bin[8 * r - 4], 64);
93 
94 	/* 2: for i = 0 to 2r - 1 do */
95 	for (i = 0; i < r; i++) {
96 		/* 3: X <-- H(X \xor B_i) */
97 		blkxor(X, &Bin[i * 8], 64);
98                 salsa20_8_intrinsic((void *) X);
99 
100 		/* 4: Y_i <-- X */
101 		/* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
102 		blkcpy(&Bout[i * 4], X, 64);
103 
104 		/* 3: X <-- H(X \xor B_i) */
105 		blkxor(X, &Bin[i * 8 + 4], 64);
106                 salsa20_8_intrinsic((void *) X);
107 
108 		/* 4: Y_i <-- X */
109 		/* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
110 		blkcpy(&Bout[(r + i) * 4], X, 64);
111 	}
112 }
113 
114 /**
115  * integerify(B, r):
116  * Return the result of parsing B_{2r-1} as a little-endian integer.
117  */
118 static uint64_t
integerify(void * B,size_t r)119 integerify(void * B, size_t r)
120 {
121 	uint8_t * X = (void*)((uintptr_t)(B) + (2 * r - 1) * 64);
122 
123 	return (le64dec(X));
124 }
125 
126 /**
127  * smix(B, r, N, V, XY):
128  * Compute B = SMix_r(B, N).  The input B must be 128r bytes in length; the
129  * temporary storage V must be 128rN bytes in length; the temporary storage
130  * XY must be 256r bytes in length.  The value N must be a power of 2.
131  */
132 static void
smix(uint8_t * B,size_t r,uint64_t N,void * V,void * XY)133 smix(uint8_t * B, size_t r, uint64_t N, void * V, void * XY)
134 {
135 	uint8x16_t * X = XY;
136 	uint8x16_t * Y = (void *)((uintptr_t)(XY) + 128 * r);
137         uint8x16_t * Z = (void *)((uintptr_t)(XY) + 256 * r);
138         uint32_t * X32 = (void *)X;
139 	uint64_t i, j;
140         size_t k;
141 
142 	/* 1: X <-- B */
143 	blkcpy(X, B, 128 * r);
144 
145 	/* 2: for i = 0 to N - 1 do */
146 	for (i = 0; i < N; i += 2) {
147 		/* 3: V_i <-- X */
148 		blkcpy((void *)((uintptr_t)(V) + i * 128 * r), X, 128 * r);
149 
150 		/* 4: X <-- H(X) */
151 		blockmix_salsa8(X, Y, Z, r);
152 
153 		/* 3: V_i <-- X */
154 		blkcpy((void *)((uintptr_t)(V) + (i + 1) * 128 * r),
155 		    Y, 128 * r);
156 
157 		/* 4: X <-- H(X) */
158 		blockmix_salsa8(Y, X, Z, r);
159 	}
160 
161 	/* 6: for i = 0 to N - 1 do */
162 	for (i = 0; i < N; i += 2) {
163 		/* 7: j <-- Integerify(X) mod N */
164 		j = integerify(X, r) & (N - 1);
165 
166 		/* 8: X <-- H(X \xor V_j) */
167 		blkxor(X, (void *)((uintptr_t)(V) + j * 128 * r), 128 * r);
168 		blockmix_salsa8(X, Y, Z, r);
169 
170 		/* 7: j <-- Integerify(X) mod N */
171 		j = integerify(Y, r) & (N - 1);
172 
173 		/* 8: X <-- H(X \xor V_j) */
174 		blkxor(Y, (void *)((uintptr_t)(V) + j * 128 * r), 128 * r);
175 		blockmix_salsa8(Y, X, Z, r);
176 	}
177 
178 	/* 10: B' <-- X */
179 	blkcpy(B, X, 128 * r);
180 }
181 
182 /**
183  * crypto_scrypt(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen):
184  * Compute scrypt(passwd[0 .. passwdlen - 1], salt[0 .. saltlen - 1], N, r,
185  * p, buflen) and write the result into buf.  The parameters r, p, and buflen
186  * must satisfy r * p < 2^30 and buflen <= (2^32 - 1) * 32.  The parameter N
187  * must be a power of 2.
188  *
189  * Return 0 on success; or -1 on error.
190  */
191 int
crypto_scrypt(const uint8_t * passwd,size_t passwdlen,const uint8_t * salt,size_t saltlen,uint64_t N,uint32_t r,uint32_t p,uint8_t * buf,size_t buflen)192 crypto_scrypt(const uint8_t * passwd, size_t passwdlen,
193     const uint8_t * salt, size_t saltlen, uint64_t N, uint32_t r, uint32_t p,
194     uint8_t * buf, size_t buflen)
195 {
196 	void * B0, * V0, * XY0;
197 	uint8_t * B;
198 	uint32_t * V;
199 	uint32_t * XY;
200 	uint32_t i;
201 
202 	/* Sanity-check parameters. */
203 #if SIZE_MAX > UINT32_MAX
204 	if (buflen > (((uint64_t)(1) << 32) - 1) * 32) {
205 		errno = EFBIG;
206 		goto err0;
207 	}
208 #endif
209 	if ((uint64_t)(r) * (uint64_t)(p) >= (1 << 30)) {
210 		errno = EFBIG;
211 		goto err0;
212 	}
213 	if (((N & (N - 1)) != 0) || (N == 0)) {
214 		errno = EINVAL;
215 		goto err0;
216 	}
217 	if ((r > SIZE_MAX / 128 / p) ||
218 #if SIZE_MAX / 256 <= UINT32_MAX
219 	    (r > SIZE_MAX / 256) ||
220 #endif
221 	    (N > SIZE_MAX / 128 / r)) {
222 		errno = ENOMEM;
223 		goto err0;
224 	}
225 
226 	/* Allocate memory. */
227 #ifdef HAVE_POSIX_MEMALIGN
228 	if ((errno = posix_memalign(&B0, 64, 128 * r * p)) != 0)
229 		goto err0;
230 	B = (uint8_t *)(B0);
231 	if ((errno = posix_memalign(&XY0, 64, 256 * r + 64)) != 0)
232 		goto err1;
233 	XY = (uint32_t *)(XY0);
234 #ifndef MAP_ANON
235 	if ((errno = posix_memalign(&V0, 64, 128 * r * N)) != 0)
236 		goto err2;
237 	V = (uint32_t *)(V0);
238 #endif
239 #else
240 	if ((B0 = malloc(128 * r * p + 63)) == NULL)
241 		goto err0;
242 	B = (uint8_t *)(((uintptr_t)(B0) + 63) & ~ (uintptr_t)(63));
243 	if ((XY0 = malloc(256 * r + 64 + 63)) == NULL)
244 		goto err1;
245 	XY = (uint32_t *)(((uintptr_t)(XY0) + 63) & ~ (uintptr_t)(63));
246 #ifndef MAP_ANON
247 	if ((V0 = malloc(128 * r * N + 63)) == NULL)
248 		goto err2;
249 	V = (uint32_t *)(((uintptr_t)(V0) + 63) & ~ (uintptr_t)(63));
250 #endif
251 #endif
252 #ifdef MAP_ANON
253 	if ((V0 = mmap(NULL, 128 * r * N, PROT_READ | PROT_WRITE,
254 #ifdef MAP_NOCORE
255 	    MAP_ANON | MAP_PRIVATE | MAP_NOCORE,
256 #else
257 	    MAP_ANON | MAP_PRIVATE,
258 #endif
259 	    -1, 0)) == MAP_FAILED)
260 		goto err2;
261 	V = (uint32_t *)(V0);
262 #endif
263 
264 	/* 1: (B_0 ... B_{p-1}) <-- PBKDF2(P, S, 1, p * MFLen) */
265 #ifdef USE_OPENSSL_PBKDF2
266 	PKCS5_PBKDF2_HMAC((const char *)passwd, passwdlen, salt, saltlen, 1, EVP_sha256(), p * 128 * r, B);
267 #else
268 	PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, 1, B, p * 128 * r);
269 #endif
270 
271 	/* 2: for i = 0 to p - 1 do */
272 	for (i = 0; i < p; i++) {
273 		/* 3: B_i <-- MF(B_i, N) */
274 		smix(&B[i * 128 * r], r, N, V, XY);
275 	}
276 
277 	/* 5: DK <-- PBKDF2(P, B, 1, dkLen) */
278 #ifdef USE_OPENSSL_PBKDF2
279 	PKCS5_PBKDF2_HMAC((const char *)passwd, passwdlen, B, p * 128 * r, 1, EVP_sha256(), buflen, buf);
280 #else
281 	PBKDF2_SHA256(passwd, passwdlen, B, p * 128 * r, 1, buf, buflen);
282 #endif
283 
284 	/* Free memory. */
285 #ifdef MAP_ANON
286 	if (munmap(V0, 128 * r * N))
287 		goto err2;
288 #else
289 	free(V0);
290 #endif
291 	free(XY0);
292 	free(B0);
293 
294 	/* Success! */
295 	return (0);
296 
297 err2:
298 	free(XY0);
299 err1:
300 	free(B0);
301 err0:
302 	/* Failure! */
303 	return (-1);
304 }
305