diff options
Diffstat (limited to 'toxencryptsave/crypto_pwhash_scryptsalsa208sha256/nosse/pwhash_scryptsalsa208sha256_nosse.c')
-rw-r--r-- | toxencryptsave/crypto_pwhash_scryptsalsa208sha256/nosse/pwhash_scryptsalsa208sha256_nosse.c | 309 |
1 files changed, 309 insertions, 0 deletions
diff --git a/toxencryptsave/crypto_pwhash_scryptsalsa208sha256/nosse/pwhash_scryptsalsa208sha256_nosse.c b/toxencryptsave/crypto_pwhash_scryptsalsa208sha256/nosse/pwhash_scryptsalsa208sha256_nosse.c new file mode 100644 index 00000000..97d9ba68 --- /dev/null +++ b/toxencryptsave/crypto_pwhash_scryptsalsa208sha256/nosse/pwhash_scryptsalsa208sha256_nosse.c | |||
@@ -0,0 +1,309 @@ | |||
1 | #ifdef HAVE_CONFIG_H | ||
2 | #include "config.h" | ||
3 | #endif | ||
4 | #ifdef VANILLA_NACL /* toxcore only uses this when libsodium is unavailable */ | ||
5 | |||
6 | /*- | ||
7 | * Copyright 2009 Colin Percival | ||
8 | * Copyright 2013 Alexander Peslyak | ||
9 | * All rights reserved. | ||
10 | * | ||
11 | * Redistribution and use in source and binary forms, with or without | ||
12 | * modification, are permitted provided that the following conditions | ||
13 | * are met: | ||
14 | * 1. Redistributions of source code must retain the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer. | ||
16 | * 2. Redistributions in binary form must reproduce the above copyright | ||
17 | * notice, this list of conditions and the following disclaimer in the | ||
18 | * documentation and/or other materials provided with the distribution. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | ||
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | * SUCH DAMAGE. | ||
31 | * | ||
32 | * This file was originally written by Colin Percival as part of the Tarsnap | ||
33 | * online backup system. | ||
34 | */ | ||
35 | |||
36 | #include <errno.h> | ||
37 | #include <limits.h> | ||
38 | #include <stdint.h> | ||
39 | #include <stdlib.h> | ||
40 | #include <string.h> | ||
41 | |||
42 | #include "../pbkdf2-sha256.h" | ||
43 | #include "../sysendian.h" | ||
44 | #include "../crypto_scrypt.h" | ||
45 | |||
46 | static inline void | ||
47 | blkcpy(void * dest, const void * src, size_t len) | ||
48 | { | ||
49 | size_t * D = (size_t *) dest; | ||
50 | const size_t * S = (const size_t *) src; | ||
51 | size_t L = len / sizeof(size_t); | ||
52 | size_t i; | ||
53 | |||
54 | for (i = 0; i < L; i++) | ||
55 | D[i] = S[i]; | ||
56 | } | ||
57 | |||
58 | static inline void | ||
59 | blkxor(void * dest, const void * src, size_t len) | ||
60 | { | ||
61 | size_t * D = (size_t *) dest; | ||
62 | const size_t * S = (const size_t *) src; | ||
63 | size_t L = len / sizeof(size_t); | ||
64 | size_t i; | ||
65 | |||
66 | for (i = 0; i < L; i++) | ||
67 | D[i] ^= S[i]; | ||
68 | } | ||
69 | |||
70 | /** | ||
71 | * salsa20_8(B): | ||
72 | * Apply the salsa20/8 core to the provided block. | ||
73 | */ | ||
74 | static void | ||
75 | salsa20_8(uint32_t B[16]) | ||
76 | { | ||
77 | uint32_t x[16]; | ||
78 | size_t i; | ||
79 | |||
80 | blkcpy(x, B, 64); | ||
81 | for (i = 0; i < 8; i += 2) { | ||
82 | #define R(a,b) (((a) << (b)) | ((a) >> (32 - (b)))) | ||
83 | /* Operate on columns. */ | ||
84 | x[ 4] ^= R(x[ 0]+x[12], 7); x[ 8] ^= R(x[ 4]+x[ 0], 9); | ||
85 | x[12] ^= R(x[ 8]+x[ 4],13); x[ 0] ^= R(x[12]+x[ 8],18); | ||
86 | |||
87 | x[ 9] ^= R(x[ 5]+x[ 1], 7); x[13] ^= R(x[ 9]+x[ 5], 9); | ||
88 | x[ 1] ^= R(x[13]+x[ 9],13); x[ 5] ^= R(x[ 1]+x[13],18); | ||
89 | |||
90 | x[14] ^= R(x[10]+x[ 6], 7); x[ 2] ^= R(x[14]+x[10], 9); | ||
91 | x[ 6] ^= R(x[ 2]+x[14],13); x[10] ^= R(x[ 6]+x[ 2],18); | ||
92 | |||
93 | x[ 3] ^= R(x[15]+x[11], 7); x[ 7] ^= R(x[ 3]+x[15], 9); | ||
94 | x[11] ^= R(x[ 7]+x[ 3],13); x[15] ^= R(x[11]+x[ 7],18); | ||
95 | |||
96 | /* Operate on rows. */ | ||
97 | x[ 1] ^= R(x[ 0]+x[ 3], 7); x[ 2] ^= R(x[ 1]+x[ 0], 9); | ||
98 | x[ 3] ^= R(x[ 2]+x[ 1],13); x[ 0] ^= R(x[ 3]+x[ 2],18); | ||
99 | |||
100 | x[ 6] ^= R(x[ 5]+x[ 4], 7); x[ 7] ^= R(x[ 6]+x[ 5], 9); | ||
101 | x[ 4] ^= R(x[ 7]+x[ 6],13); x[ 5] ^= R(x[ 4]+x[ 7],18); | ||
102 | |||
103 | x[11] ^= R(x[10]+x[ 9], 7); x[ 8] ^= R(x[11]+x[10], 9); | ||
104 | x[ 9] ^= R(x[ 8]+x[11],13); x[10] ^= R(x[ 9]+x[ 8],18); | ||
105 | |||
106 | x[12] ^= R(x[15]+x[14], 7); x[13] ^= R(x[12]+x[15], 9); | ||
107 | x[14] ^= R(x[13]+x[12],13); x[15] ^= R(x[14]+x[13],18); | ||
108 | #undef R | ||
109 | } | ||
110 | for (i = 0; i < 16; i++) | ||
111 | B[i] += x[i]; | ||
112 | } | ||
113 | |||
114 | /** | ||
115 | * blockmix_salsa8(Bin, Bout, X, r): | ||
116 | * Compute Bout = BlockMix_{salsa20/8, r}(Bin). The input Bin must be 128r | ||
117 | * bytes in length; the output Bout must also be the same size. The | ||
118 | * temporary space X must be 64 bytes. | ||
119 | */ | ||
120 | static void | ||
121 | blockmix_salsa8(const uint32_t * Bin, uint32_t * Bout, uint32_t * X, size_t r) | ||
122 | { | ||
123 | size_t i; | ||
124 | |||
125 | /* 1: X <-- B_{2r - 1} */ | ||
126 | blkcpy(X, &Bin[(2 * r - 1) * 16], 64); | ||
127 | |||
128 | /* 2: for i = 0 to 2r - 1 do */ | ||
129 | for (i = 0; i < 2 * r; i += 2) { | ||
130 | /* 3: X <-- H(X \xor B_i) */ | ||
131 | blkxor(X, &Bin[i * 16], 64); | ||
132 | salsa20_8(X); | ||
133 | |||
134 | /* 4: Y_i <-- X */ | ||
135 | /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */ | ||
136 | blkcpy(&Bout[i * 8], X, 64); | ||
137 | |||
138 | /* 3: X <-- H(X \xor B_i) */ | ||
139 | blkxor(X, &Bin[i * 16 + 16], 64); | ||
140 | salsa20_8(X); | ||
141 | |||
142 | /* 4: Y_i <-- X */ | ||
143 | /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */ | ||
144 | blkcpy(&Bout[i * 8 + r * 16], X, 64); | ||
145 | } | ||
146 | } | ||
147 | |||
148 | /** | ||
149 | * integerify(B, r): | ||
150 | * Return the result of parsing B_{2r-1} as a little-endian integer. | ||
151 | */ | ||
152 | static inline uint64_t | ||
153 | integerify(const void * B, size_t r) | ||
154 | { | ||
155 | const uint32_t * X = (const uint32_t *)((uintptr_t)(B) + (2 * r - 1) * 64); | ||
156 | |||
157 | return (((uint64_t)(X[1]) << 32) + X[0]); | ||
158 | } | ||
159 | |||
160 | /** | ||
161 | * smix(B, r, N, V, XY): | ||
162 | * Compute B = SMix_r(B, N). The input B must be 128r bytes in length; | ||
163 | * the temporary storage V must be 128rN bytes in length; the temporary | ||
164 | * storage XY must be 256r + 64 bytes in length. The value N must be a | ||
165 | * power of 2 greater than 1. The arrays B, V, and XY must be aligned to a | ||
166 | * multiple of 64 bytes. | ||
167 | */ | ||
168 | static void | ||
169 | smix(uint8_t * B, size_t r, uint64_t N, uint32_t * V, uint32_t * XY) | ||
170 | { | ||
171 | uint32_t * X = XY; | ||
172 | uint32_t * Y = &XY[32 * r]; | ||
173 | uint32_t * Z = &XY[64 * r]; | ||
174 | uint64_t i; | ||
175 | uint64_t j; | ||
176 | size_t k; | ||
177 | |||
178 | /* 1: X <-- B */ | ||
179 | for (k = 0; k < 32 * r; k++) | ||
180 | X[k] = le32dec(&B[4 * k]); | ||
181 | |||
182 | /* 2: for i = 0 to N - 1 do */ | ||
183 | for (i = 0; i < N; i += 2) { | ||
184 | /* 3: V_i <-- X */ | ||
185 | blkcpy(&V[i * (32 * r)], X, 128 * r); | ||
186 | |||
187 | /* 4: X <-- H(X) */ | ||
188 | blockmix_salsa8(X, Y, Z, r); | ||
189 | |||
190 | /* 3: V_i <-- X */ | ||
191 | blkcpy(&V[(i + 1) * (32 * r)], Y, 128 * r); | ||
192 | |||
193 | /* 4: X <-- H(X) */ | ||
194 | blockmix_salsa8(Y, X, Z, r); | ||
195 | } | ||
196 | |||
197 | /* 6: for i = 0 to N - 1 do */ | ||
198 | for (i = 0; i < N; i += 2) { | ||
199 | /* 7: j <-- Integerify(X) mod N */ | ||
200 | j = integerify(X, r) & (N - 1); | ||
201 | |||
202 | /* 8: X <-- H(X \xor V_j) */ | ||
203 | blkxor(X, &V[j * (32 * r)], 128 * r); | ||
204 | blockmix_salsa8(X, Y, Z, r); | ||
205 | |||
206 | /* 7: j <-- Integerify(X) mod N */ | ||
207 | j = integerify(Y, r) & (N - 1); | ||
208 | |||
209 | /* 8: X <-- H(X \xor V_j) */ | ||
210 | blkxor(Y, &V[j * (32 * r)], 128 * r); | ||
211 | blockmix_salsa8(Y, X, Z, r); | ||
212 | } | ||
213 | /* 10: B' <-- X */ | ||
214 | for (k = 0; k < 32 * r; k++) | ||
215 | le32enc(&B[4 * k], X[k]); | ||
216 | } | ||
217 | |||
218 | /** | ||
219 | * escrypt_kdf(local, passwd, passwdlen, salt, saltlen, | ||
220 | * N, r, p, buf, buflen): | ||
221 | * Compute scrypt(passwd[0 .. passwdlen - 1], salt[0 .. saltlen - 1], N, r, | ||
222 | * p, buflen) and write the result into buf. The parameters r, p, and buflen | ||
223 | * must satisfy r * p < 2^30 and buflen <= (2^32 - 1) * 32. The parameter N | ||
224 | * must be a power of 2 greater than 1. | ||
225 | * | ||
226 | * Return 0 on success; or -1 on error. | ||
227 | */ | ||
228 | int | ||
229 | escrypt_kdf_nosse(escrypt_local_t * local, | ||
230 | const uint8_t * passwd, size_t passwdlen, | ||
231 | const uint8_t * salt, size_t saltlen, | ||
232 | uint64_t N, uint32_t _r, uint32_t _p, | ||
233 | uint8_t * buf, size_t buflen) | ||
234 | { | ||
235 | size_t B_size, V_size, XY_size, need; | ||
236 | uint8_t * B; | ||
237 | uint32_t * V, * XY; | ||
238 | size_t r = _r, p = _p; | ||
239 | uint32_t i; | ||
240 | |||
241 | /* Sanity-check parameters. */ | ||
242 | #if SIZE_MAX > UINT32_MAX | ||
243 | if (buflen > (((uint64_t)(1) << 32) - 1) * 32) { | ||
244 | errno = EFBIG; | ||
245 | return -1; | ||
246 | } | ||
247 | #endif | ||
248 | if ((uint64_t)(r) * (uint64_t)(p) >= (1 << 30)) { | ||
249 | errno = EFBIG; | ||
250 | return -1; | ||
251 | } | ||
252 | if (((N & (N - 1)) != 0) || (N < 2)) { | ||
253 | errno = EINVAL; | ||
254 | return -1; | ||
255 | } | ||
256 | if (r == 0 || p == 0) { | ||
257 | errno = EINVAL; | ||
258 | return -1; | ||
259 | } | ||
260 | if ((r > SIZE_MAX / 128 / p) || | ||
261 | #if SIZE_MAX / 256 <= UINT32_MAX | ||
262 | (r > SIZE_MAX / 256) || | ||
263 | #endif | ||
264 | (N > SIZE_MAX / 128 / r)) { | ||
265 | errno = ENOMEM; | ||
266 | return -1; | ||
267 | } | ||
268 | |||
269 | /* Allocate memory. */ | ||
270 | B_size = (size_t)128 * r * p; | ||
271 | V_size = (size_t)128 * r * N; | ||
272 | need = B_size + V_size; | ||
273 | if (need < V_size) { | ||
274 | errno = ENOMEM; | ||
275 | return -1; | ||
276 | } | ||
277 | XY_size = (size_t)256 * r + 64; | ||
278 | need += XY_size; | ||
279 | if (need < XY_size) { | ||
280 | errno = ENOMEM; | ||
281 | return -1; | ||
282 | } | ||
283 | if (local->size < need) { | ||
284 | if (free_region(local)) | ||
285 | return -1; | ||
286 | if (!alloc_region(local, need)) | ||
287 | return -1; | ||
288 | } | ||
289 | B = (uint8_t *)local->aligned; | ||
290 | V = (uint32_t *)((uint8_t *)B + B_size); | ||
291 | XY = (uint32_t *)((uint8_t *)V + V_size); | ||
292 | |||
293 | /* 1: (B_0 ... B_{p-1}) <-- PBKDF2(P, S, 1, p * MFLen) */ | ||
294 | PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, 1, B, B_size); | ||
295 | |||
296 | /* 2: for i = 0 to p - 1 do */ | ||
297 | for (i = 0; i < p; i++) { | ||
298 | /* 3: B_i <-- MF(B_i, N) */ | ||
299 | smix(&B[(size_t)128 * i * r], r, N, V, XY); | ||
300 | } | ||
301 | |||
302 | /* 5: DK <-- PBKDF2(P, B, 1, dkLen) */ | ||
303 | PBKDF2_SHA256(passwd, passwdlen, B, B_size, 1, buf, buflen); | ||
304 | |||
305 | /* Success! */ | ||
306 | return 0; | ||
307 | } | ||
308 | |||
309 | #endif | ||