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