Fabcoin Core  0.16.2
P2P Digital Currency
crypto_scrypt-nosse.c
Go to the documentation of this file.
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 
30 #include <sys/types.h>
31 #ifndef _WIN32
32 #include <sys/mman.h>
33 #endif
34 #include <errno.h>
35 #include <stdint.h>
36 #include <stdlib.h>
37 #include <string.h>
38 
39 #include "sha256.h"
40 #include "sysendian.h"
41 
42 #include "libscrypt.h"
43 
44 static void blkcpy(void *, void *, size_t);
45 static void blkxor(void *, void *, size_t);
46 static void salsa20_8(uint32_t[16]);
47 static void blockmix_salsa8(uint32_t *, uint32_t *, uint32_t *, size_t);
48 static uint64_t integerify(void *, size_t);
49 static void smix(uint8_t *, size_t, uint64_t, uint32_t *, uint32_t *);
50 
51 static void
52 blkcpy(void * dest, void * src, size_t len)
53 {
54  size_t * D = dest;
55  size_t * S = src;
56  size_t L = len / sizeof(size_t);
57  size_t i;
58 
59  for (i = 0; i < L; i++)
60  D[i] = S[i];
61 }
62 
63 static void
64 blkxor(void * dest, void * src, size_t len)
65 {
66  size_t * D = dest;
67  size_t * S = src;
68  size_t L = len / sizeof(size_t);
69  size_t i;
70 
71  for (i = 0; i < L; i++)
72  D[i] ^= S[i];
73 }
74 
79 static void
80 salsa20_8(uint32_t B[16])
81 {
82  uint32_t x[16];
83  size_t i;
84 
85  blkcpy(x, B, 64);
86  for (i = 0; i < 8; i += 2) {
87 #define R(a,b) (((a) << (b)) | ((a) >> (32 - (b))))
88  /* Operate on columns. */
89  x[ 4] ^= R(x[ 0]+x[12], 7); x[ 8] ^= R(x[ 4]+x[ 0], 9);
90  x[12] ^= R(x[ 8]+x[ 4],13); x[ 0] ^= R(x[12]+x[ 8],18);
91 
92  x[ 9] ^= R(x[ 5]+x[ 1], 7); x[13] ^= R(x[ 9]+x[ 5], 9);
93  x[ 1] ^= R(x[13]+x[ 9],13); x[ 5] ^= R(x[ 1]+x[13],18);
94 
95  x[14] ^= R(x[10]+x[ 6], 7); x[ 2] ^= R(x[14]+x[10], 9);
96  x[ 6] ^= R(x[ 2]+x[14],13); x[10] ^= R(x[ 6]+x[ 2],18);
97 
98  x[ 3] ^= R(x[15]+x[11], 7); x[ 7] ^= R(x[ 3]+x[15], 9);
99  x[11] ^= R(x[ 7]+x[ 3],13); x[15] ^= R(x[11]+x[ 7],18);
100 
101  /* Operate on rows. */
102  x[ 1] ^= R(x[ 0]+x[ 3], 7); x[ 2] ^= R(x[ 1]+x[ 0], 9);
103  x[ 3] ^= R(x[ 2]+x[ 1],13); x[ 0] ^= R(x[ 3]+x[ 2],18);
104 
105  x[ 6] ^= R(x[ 5]+x[ 4], 7); x[ 7] ^= R(x[ 6]+x[ 5], 9);
106  x[ 4] ^= R(x[ 7]+x[ 6],13); x[ 5] ^= R(x[ 4]+x[ 7],18);
107 
108  x[11] ^= R(x[10]+x[ 9], 7); x[ 8] ^= R(x[11]+x[10], 9);
109  x[ 9] ^= R(x[ 8]+x[11],13); x[10] ^= R(x[ 9]+x[ 8],18);
110 
111  x[12] ^= R(x[15]+x[14], 7); x[13] ^= R(x[12]+x[15], 9);
112  x[14] ^= R(x[13]+x[12],13); x[15] ^= R(x[14]+x[13],18);
113 #undef R
114  }
115  for (i = 0; i < 16; i++)
116  B[i] += x[i];
117 }
118 
125 static void
126 blockmix_salsa8(uint32_t * Bin, uint32_t * Bout, uint32_t * X, size_t r)
127 {
128  size_t i;
129 
130  /* 1: X <-- B_{2r - 1} */
131  blkcpy(X, &Bin[(2 * r - 1) * 16], 64);
132 
133  /* 2: for i = 0 to 2r - 1 do */
134  for (i = 0; i < 2 * r; i += 2) {
135  /* 3: X <-- H(X \xor B_i) */
136  blkxor(X, &Bin[i * 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], X, 64);
142 
143  /* 3: X <-- H(X \xor B_i) */
144  blkxor(X, &Bin[i * 16 + 16], 64);
145  salsa20_8(X);
146 
147  /* 4: Y_i <-- X */
148  /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
149  blkcpy(&Bout[i * 8 + r * 16], X, 64);
150  }
151 }
152 
157 static uint64_t
158 integerify(void * B, size_t r)
159 {
160  uint32_t * X = (void *)((uintptr_t)(B) + (2 * r - 1) * 64);
161 
162  return (((uint64_t)(X[1]) << 32) + X[0]);
163 }
164 
173 static void
174 smix(uint8_t * B, size_t r, uint64_t N, uint32_t * V, uint32_t * XY)
175 {
176  uint32_t * X = XY;
177  uint32_t * Y = &XY[32 * r];
178  uint32_t * Z = &XY[64 * r];
179  uint64_t i;
180  uint64_t j;
181  size_t k;
182 
183  /* 1: X <-- B */
184  for (k = 0; k < 32 * r; k++)
185  X[k] = le32dec(&B[4 * k]);
186 
187  /* 2: for i = 0 to N - 1 do */
188  for (i = 0; i < N; i += 2) {
189  /* 3: V_i <-- X */
190  blkcpy(&V[i * (32 * r)], X, 128 * r);
191 
192  /* 4: X <-- H(X) */
193  blockmix_salsa8(X, Y, Z, r);
194 
195  /* 3: V_i <-- X */
196  blkcpy(&V[(i + 1) * (32 * r)], Y, 128 * r);
197 
198  /* 4: X <-- H(X) */
199  blockmix_salsa8(Y, X, Z, r);
200  }
201 
202  /* 6: for i = 0 to N - 1 do */
203  for (i = 0; i < N; i += 2) {
204  /* 7: j <-- Integerify(X) mod N */
205  j = integerify(X, r) & (N - 1);
206 
207  /* 8: X <-- H(X \xor V_j) */
208  blkxor(X, &V[j * (32 * r)], 128 * r);
209  blockmix_salsa8(X, Y, Z, r);
210 
211  /* 7: j <-- Integerify(X) mod N */
212  j = integerify(Y, r) & (N - 1);
213 
214  /* 8: X <-- H(X \xor V_j) */
215  blkxor(Y, &V[j * (32 * r)], 128 * r);
216  blockmix_salsa8(Y, X, Z, r);
217  }
218 
219  /* 10: B' <-- X */
220  for (k = 0; k < 32 * r; k++)
221  le32enc(&B[4 * k], X[k]);
222 }
223 
233 int
234 libscrypt_scrypt(const uint8_t * passwd, size_t passwdlen,
235  const uint8_t * salt, size_t saltlen, uint64_t N, uint32_t r, uint32_t p,
236  uint8_t * buf, size_t buflen)
237 {
238  void * B0, * V0, * XY0;
239  uint8_t * B;
240  uint32_t * V;
241  uint32_t * XY;
242  uint32_t i;
243 
244  /* Sanity-check parameters. */
245 #if SIZE_MAX > UINT32_MAX
246  if (buflen > (((uint64_t)(1) << 32) - 1) * 32) {
247  errno = EFBIG;
248  goto err0;
249  }
250 #endif
251  if ((uint64_t)(r) * (uint64_t)(p) >= (1 << 30)) {
252  errno = EFBIG;
253  goto err0;
254  }
255  if (r == 0 || p == 0) {
256  errno = EINVAL;
257  goto err0;
258  }
259  if (((N & (N - 1)) != 0) || (N < 2)) {
260  errno = EINVAL;
261  goto err0;
262  }
263  if ((r > SIZE_MAX / 128 / p) ||
264 #if SIZE_MAX / 256 <= UINT32_MAX
265  (r > SIZE_MAX / 256) ||
266 #endif
267  (N > SIZE_MAX / 128 / r)) {
268  errno = ENOMEM;
269  goto err0;
270  }
271 
272  /* Allocate memory. */
273 #ifdef HAVE_POSIX_MEMALIGN
274  if ((errno = posix_memalign(&B0, 64, 128 * r * p)) != 0)
275  goto err0;
276  B = (uint8_t *)(B0);
277  if ((errno = posix_memalign(&XY0, 64, 256 * r + 64)) != 0)
278  goto err1;
279  XY = (uint32_t *)(XY0);
280 #ifndef MAP_ANON
281  if ((errno = posix_memalign(&V0, 64, 128 * r * N)) != 0)
282  goto err2;
283  V = (uint32_t *)(V0);
284 #endif
285 #else
286  if ((B0 = malloc(128 * r * p + 63)) == NULL)
287  goto err0;
288  B = (uint8_t *)(((uintptr_t)(B0) + 63) & ~ (uintptr_t)(63));
289  if ((XY0 = malloc(256 * r + 64 + 63)) == NULL)
290  goto err1;
291  XY = (uint32_t *)(((uintptr_t)(XY0) + 63) & ~ (uintptr_t)(63));
292 #ifndef MAP_ANON
293  if ((V0 = malloc(128 * r * N + 63)) == NULL)
294  goto err2;
295  V = (uint32_t *)(((uintptr_t)(V0) + 63) & ~ (uintptr_t)(63));
296 #endif
297 #endif
298 #ifdef MAP_ANON
299  if ((V0 = mmap(NULL, 128 * r * N, PROT_READ | PROT_WRITE,
300 #ifdef MAP_NOCORE
301  MAP_ANON | MAP_PRIVATE | MAP_NOCORE,
302 #else
303  MAP_ANON | MAP_PRIVATE,
304 #endif
305  -1, 0)) == MAP_FAILED)
306  goto err2;
307  V = (uint32_t *)(V0);
308 #endif
309 
310  /* 1: (B_0 ... B_{p-1}) <-- PBKDF2(P, S, 1, p * MFLen) */
311  libscrypt_PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, 1, B, p * 128 * r);
312 
313  /* 2: for i = 0 to p - 1 do */
314  for (i = 0; i < p; i++) {
315  /* 3: B_i <-- MF(B_i, N) */
316  smix(&B[i * 128 * r], r, N, V, XY);
317  }
318 
319  /* 5: DK <-- PBKDF2(P, B, 1, dkLen) */
320  libscrypt_PBKDF2_SHA256(passwd, passwdlen, B, p * 128 * r, 1, buf, buflen);
321 
322  /* Free memory. */
323 #ifdef MAP_ANON
324  if (munmap(V0, 128 * r * N))
325  goto err2;
326 #else
327  free(V0);
328 #endif
329  free(XY0);
330  free(B0);
331 
332  /* Success! */
333  return (0);
334 
335 err2:
336  free(XY0);
337 err1:
338  free(B0);
339 err0:
340  /* Failure! */
341  return (-1);
342 }
#define R(a, b)
int libscrypt_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)
crypto_scrypt(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen): Compute scrypt(passwd[0 ...
#define x(i)
#define B0
Definition: integer.cpp:2166
#define V0
void libscrypt_PBKDF2_SHA256(const uint8_t *passwd, size_t passwdlen, const uint8_t *salt, size_t saltlen, uint64_t c, uint8_t *buf, size_t dkLen)
PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen): Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and write the output to buf.
Definition: sha256.c:362
#define X(name)
Definition: net.cpp:642
#define S(a)
Definition: mars.cpp:50
#define SIZE_MAX
Definition: misc.h:113