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