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 "php.h"
30 #ifdef PHP_WIN32
31 #include "zend_config.w32.h"
32 #endif
33 
34 #include <sys/types.h>
35 #include <sys/mman.h>
36 
37 #include <emmintrin.h>
38 #include <errno.h>
39 #include <stdint.h>
40 #include <stdlib.h>
41 #include <string.h>
42 
43 #include "sha256.h"
44 #include "sysendian.h"
45 
46 #include "crypto_scrypt.h"
47 #include "php_scrypt.h"
48 
49 static void blkcpy(void *, void *, size_t);
50 static void blkxor(void *, void *, size_t);
51 static void salsa20_8(__m128i *);
52 static void blockmix_salsa8(__m128i *, __m128i *, __m128i *, size_t);
53 static uint64_t integerify(void *, size_t);
54 static void smix(uint8_t *, size_t, uint64_t, void *, void *);
55 
56 static void
blkcpy(void * dest,void * src,size_t len)57 blkcpy(void * dest, void * src, size_t len)
58 {
59     __m128i * D = dest;
60     __m128i * S = src;
61     size_t L = len / 16;
62     size_t i;
63 
64     for (i = 0; i < L; i++)
65         D[i] = S[i];
66 }
67 
68 static void
blkxor(void * dest,void * src,size_t len)69 blkxor(void * dest, void * src, size_t len)
70 {
71     __m128i * D = dest;
72     __m128i * S = src;
73     size_t L = len / 16;
74     size_t i;
75 
76     for (i = 0; i < L; i++)
77         D[i] = _mm_xor_si128(D[i], S[i]);
78 }
79 
80 /**
81  * salsa20_8(B):
82  * Apply the salsa20/8 core to the provided block.
83  */
84 static void
salsa20_8(__m128i B[4])85 salsa20_8(__m128i B[4])
86 {
87     __m128i X0, X1, X2, X3;
88     __m128i T;
89     size_t i;
90 
91     X0 = B[0];
92     X1 = B[1];
93     X2 = B[2];
94     X3 = B[3];
95 
96     for (i = 0; i < 8; i += 2) {
97         /* Operate on "columns". */
98         T = _mm_add_epi32(X0, X3);
99         X1 = _mm_xor_si128(X1, _mm_slli_epi32(T, 7));
100         X1 = _mm_xor_si128(X1, _mm_srli_epi32(T, 25));
101         T = _mm_add_epi32(X1, X0);
102         X2 = _mm_xor_si128(X2, _mm_slli_epi32(T, 9));
103         X2 = _mm_xor_si128(X2, _mm_srli_epi32(T, 23));
104         T = _mm_add_epi32(X2, X1);
105         X3 = _mm_xor_si128(X3, _mm_slli_epi32(T, 13));
106         X3 = _mm_xor_si128(X3, _mm_srli_epi32(T, 19));
107         T = _mm_add_epi32(X3, X2);
108         X0 = _mm_xor_si128(X0, _mm_slli_epi32(T, 18));
109         X0 = _mm_xor_si128(X0, _mm_srli_epi32(T, 14));
110 
111         /* Rearrange data. */
112         X1 = _mm_shuffle_epi32(X1, 0x93);
113         X2 = _mm_shuffle_epi32(X2, 0x4E);
114         X3 = _mm_shuffle_epi32(X3, 0x39);
115 
116         /* Operate on "rows". */
117         T = _mm_add_epi32(X0, X1);
118         X3 = _mm_xor_si128(X3, _mm_slli_epi32(T, 7));
119         X3 = _mm_xor_si128(X3, _mm_srli_epi32(T, 25));
120         T = _mm_add_epi32(X3, X0);
121         X2 = _mm_xor_si128(X2, _mm_slli_epi32(T, 9));
122         X2 = _mm_xor_si128(X2, _mm_srli_epi32(T, 23));
123         T = _mm_add_epi32(X2, X3);
124         X1 = _mm_xor_si128(X1, _mm_slli_epi32(T, 13));
125         X1 = _mm_xor_si128(X1, _mm_srli_epi32(T, 19));
126         T = _mm_add_epi32(X1, X2);
127         X0 = _mm_xor_si128(X0, _mm_slli_epi32(T, 18));
128         X0 = _mm_xor_si128(X0, _mm_srli_epi32(T, 14));
129 
130         /* Rearrange data. */
131         X1 = _mm_shuffle_epi32(X1, 0x39);
132         X2 = _mm_shuffle_epi32(X2, 0x4E);
133         X3 = _mm_shuffle_epi32(X3, 0x93);
134     }
135 
136     B[0] = _mm_add_epi32(B[0], X0);
137     B[1] = _mm_add_epi32(B[1], X1);
138     B[2] = _mm_add_epi32(B[2], X2);
139     B[3] = _mm_add_epi32(B[3], X3);
140 }
141 
142 /**
143  * blockmix_salsa8(Bin, Bout, X, r):
144  * Compute Bout = BlockMix_{salsa20/8, r}(Bin).  The input Bin must be 128r
145  * bytes in length; the output Bout must also be the same size.  The
146  * temporary space X must be 64 bytes.
147  */
148 static void
blockmix_salsa8(__m128i * Bin,__m128i * Bout,__m128i * X,size_t r)149 blockmix_salsa8(__m128i * Bin, __m128i * Bout, __m128i * X, size_t r)
150 {
151     size_t i;
152 
153     /* 1: X <-- B_{2r - 1} */
154     blkcpy(X, &Bin[8 * r - 4], 64);
155 
156     /* 2: for i = 0 to 2r - 1 do */
157     for (i = 0; i < r; i++) {
158         /* 3: X <-- H(X \xor B_i) */
159         blkxor(X, &Bin[i * 8], 64);
160         salsa20_8(X);
161 
162         /* 4: Y_i <-- X */
163         /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
164         blkcpy(&Bout[i * 4], X, 64);
165 
166         /* 3: X <-- H(X \xor B_i) */
167         blkxor(X, &Bin[i * 8 + 4], 64);
168         salsa20_8(X);
169 
170         /* 4: Y_i <-- X */
171         /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
172         blkcpy(&Bout[(r + i) * 4], X, 64);
173     }
174 }
175 
176 /**
177  * integerify(B, r):
178  * Return the result of parsing B_{2r-1} as a little-endian integer.
179  */
180 static uint64_t
integerify(void * B,size_t r)181 integerify(void * B, size_t r)
182 {
183     uint32_t * X = (void *)((uintptr_t)(B) + (2 * r - 1) * 64);
184 
185     return (((uint64_t)(X[13]) << 32) + X[0]);
186 }
187 
188 /**
189  * smix(B, r, N, V, XY):
190  * Compute B = SMix_r(B, N).  The input B must be 128r bytes in length;
191  * the temporary storage V must be 128rN bytes in length; the temporary
192  * storage XY must be 256r + 64 bytes in length.  The value N must be a
193  * power of 2 greater than 1.  The arrays B, V, and XY must be aligned to a
194  * multiple of 64 bytes.
195  */
196 static void
smix(uint8_t * B,size_t r,uint64_t N,void * V,void * XY)197 smix(uint8_t * B, size_t r, uint64_t N, void * V, void * XY)
198 {
199     __m128i * X = XY;
200     __m128i * Y = (void *)((uintptr_t)(XY) + 128 * r);
201     __m128i * Z = (void *)((uintptr_t)(XY) + 256 * r);
202     uint32_t * X32 = (void *)X;
203     uint64_t i, j;
204     size_t k;
205 
206     /* 1: X <-- B */
207     for (k = 0; k < 2 * r; k++) {
208         for (i = 0; i < 16; i++) {
209             X32[k * 16 + i] =
210                 le32dec(&B[(k * 16 + (i * 5 % 16)) * 4]);
211         }
212     }
213 
214     /* 2: for i = 0 to N - 1 do */
215     for (i = 0; i < N; i += 2) {
216         /* 3: V_i <-- X */
217         blkcpy((void *)((uintptr_t)(V) + i * 128 * r), X, 128 * r);
218 
219         /* 4: X <-- H(X) */
220         blockmix_salsa8(X, Y, Z, r);
221 
222         /* 3: V_i <-- X */
223         blkcpy((void *)((uintptr_t)(V) + (i + 1) * 128 * r),
224             Y, 128 * r);
225 
226         /* 4: X <-- H(X) */
227         blockmix_salsa8(Y, X, Z, r);
228     }
229 
230     /* 6: for i = 0 to N - 1 do */
231     for (i = 0; i < N; i += 2) {
232         /* 7: j <-- Integerify(X) mod N */
233         j = integerify(X, r) & (N - 1);
234 
235         /* 8: X <-- H(X \xor V_j) */
236         blkxor(X, (void *)((uintptr_t)(V) + j * 128 * r), 128 * r);
237         blockmix_salsa8(X, Y, Z, r);
238 
239         /* 7: j <-- Integerify(X) mod N */
240         j = integerify(Y, r) & (N - 1);
241 
242         /* 8: X <-- H(X \xor V_j) */
243         blkxor(Y, (void *)((uintptr_t)(V) + j * 128 * r), 128 * r);
244         blockmix_salsa8(Y, X, Z, r);
245     }
246 
247     /* 10: B' <-- X */
248     for (k = 0; k < 2 * r; k++) {
249         for (i = 0; i < 16; i++) {
250             le32enc(&B[(k * 16 + (i * 5 % 16)) * 4],
251                 X32[k * 16 + i]);
252         }
253     }
254 }
255 
256 /**
257  * crypto_scrypt(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen):
258  * Compute scrypt(passwd[0 .. passwdlen - 1], salt[0 .. saltlen - 1], N, r,
259  * p, buflen) and write the result into buf.  The parameters r, p, and buflen
260  * must satisfy r * p < 2^30 and buflen <= (2^32 - 1) * 32.  The parameter N
261  * must be a power of 2 greater than 1.
262  *
263  * Return 0 on success; or -1 on error.
264  */
265 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)266 crypto_scrypt(const uint8_t * passwd, size_t passwdlen,
267     const uint8_t * salt, size_t saltlen, uint64_t N, uint32_t r, uint32_t p,
268     uint8_t * buf, size_t buflen)
269 {
270     void * B0, * V0, * XY0;
271     uint8_t * B;
272     uint32_t * V;
273     uint32_t * XY;
274     uint32_t i;
275 
276     TSRMLS_FETCH();
277 
278     /* Sanity-check parameters. */
279 #if SIZE_MAX > UINT32_MAX
280     if (buflen > (((uint64_t)(1) << 32) - 1) * 32) {
281         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid Parameters: $keyLength too big");
282         errno = EFBIG;
283         goto err0;
284     }
285 #endif
286     if ((uint64_t)(r) * (uint64_t)(p) >= (1 << 30)) {
287         errno = EFBIG;
288         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid Parameters; $r * $p is >= 2^30");
289         goto err0;
290     }
291     if (((N & (N - 1)) != 0) || (N == 0)) {
292         errno = EINVAL;
293         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid Parameters; $N is not a power of two greater than 1");
294         goto err0;
295     }
296     if ((r > SIZE_MAX / 128 / p) ||
297 #if SIZE_MAX / 256 <= UINT32_MAX
298         (r > (SIZE_MAX - 64) / 256) ||
299 #endif
300         (N > SIZE_MAX / 128 / r)) {
301         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid Parameters");
302         errno = ENOMEM;
303         goto err0;
304     }
305 
306     /* Allocate memory. */
307 #ifdef HAVE_POSIX_MEMALIGN
308     if ((errno = posix_memalign(&B0, 64, 128 * r * p)) != 0)
309         goto err0;
310     B = (uint8_t *)(B0);
311     if ((errno = posix_memalign(&XY0, 64, 256 * r + 64)) != 0)
312         goto err1;
313     XY = (uint32_t *)(XY0);
314 #ifndef MAP_ANON
315     if ((errno = posix_memalign(&V0, 64, 128 * r * N)) != 0)
316         goto err2;
317     V = (uint32_t *)(V0);
318 #endif
319 #else
320     if ((B0 = malloc(128 * r * p + 63)) == NULL)
321         goto err0;
322     B = (uint8_t *)(((uintptr_t)(B0) + 63) & ~ (uintptr_t)(63));
323     if ((XY0 = malloc(256 * r + 64 + 63)) == NULL)
324         goto err1;
325     XY = (uint32_t *)(((uintptr_t)(XY0) + 63) & ~ (uintptr_t)(63));
326 #ifndef MAP_ANON
327     if ((V0 = malloc(128 * r * N + 63)) == NULL)
328         goto err2;
329     V = (uint32_t *)(((uintptr_t)(V0) + 63) & ~ (uintptr_t)(63));
330 #endif
331 #endif
332 #ifdef MAP_ANON
333     if ((V0 = mmap(NULL, 128 * r * N, PROT_READ | PROT_WRITE,
334 #ifdef MAP_NOCORE
335         MAP_ANON | MAP_PRIVATE | MAP_NOCORE,
336 #else
337         MAP_ANON | MAP_PRIVATE,
338 #endif
339         -1, 0)) == MAP_FAILED)
340         goto err2;
341     V = (uint32_t *)(V0);
342 #endif
343 
344     /* 1: (B_0 ... B_{p-1}) <-- PBKDF2(P, S, 1, p * MFLen) */
345     PBKDF2_SHA256_SCRYPT(passwd, passwdlen, salt, saltlen, 1, B, p * 128 * r);
346 
347     /* 2: for i = 0 to p - 1 do */
348     for (i = 0; i < p; i++) {
349         /* 3: B_i <-- MF(B_i, N) */
350         smix(&B[i * 128 * r], r, N, V, XY);
351     }
352 
353     /* 5: DK <-- PBKDF2(P, B, 1, dkLen) */
354     PBKDF2_SHA256_SCRYPT(passwd, passwdlen, B, p * 128 * r, 1, buf, buflen);
355 
356     /* Free memory. */
357 #ifdef MAP_ANON
358     if (munmap(V0, 128 * r * N))
359         goto err2;
360 #else
361     free(V0);
362 #endif
363     free(XY0);
364     free(B0);
365 
366     /* Success! */
367     return (0);
368 
369 err2:
370     free(XY0);
371 err1:
372     free(B0);
373 err0:
374     /* Failure! */
375     return (-1);
376 }
377