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