scrypt-nosse.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 "scrypt_platform.h"
  30. #include <sys/types.h>
  31. #include <sys/mman.h>
  32. #include <errno.h>
  33. #include <stdint.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include "sha256.h"
  37. #include "sysendian.h"
  38. #include "scrypt.h"
  39. static void blkcpy(void *, void *, size_t);
  40. static void blkxor(void *, void *, size_t);
  41. static void salsa20_8(uint32_t[16]);
  42. static void blockmix_salsa8(uint32_t *, uint32_t *, uint32_t *, size_t);
  43. static uint64_t integerify(void *, size_t);
  44. static void smix(uint8_t *, size_t, uint64_t, uint32_t *, uint32_t *);
  45. static void
  46. blkcpy(void * dest, void * src, size_t len)
  47. {
  48. size_t * D = dest;
  49. size_t * S = src;
  50. size_t L = len / sizeof(size_t);
  51. size_t i;
  52. for (i = 0; i < L; i++)
  53. D[i] = S[i];
  54. }
  55. static void
  56. blkxor(void * dest, void * src, size_t len)
  57. {
  58. size_t * D = dest;
  59. size_t * S = src;
  60. size_t L = len / sizeof(size_t);
  61. size_t i;
  62. for (i = 0; i < L; i++)
  63. D[i] ^= S[i];
  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. blkcpy(x, B, 64);
  75. for (i = 0; i < 8; i += 2) {
  76. #define R(a,b) (((a) << (b)) | ((a) >> (32 - (b))))
  77. /* Operate on columns. */
  78. x[ 4] ^= R(x[ 0]+x[12], 7); x[ 8] ^= R(x[ 4]+x[ 0], 9);
  79. x[12] ^= R(x[ 8]+x[ 4],13); x[ 0] ^= R(x[12]+x[ 8],18);
  80. x[ 9] ^= R(x[ 5]+x[ 1], 7); x[13] ^= R(x[ 9]+x[ 5], 9);
  81. x[ 1] ^= R(x[13]+x[ 9],13); x[ 5] ^= R(x[ 1]+x[13],18);
  82. x[14] ^= R(x[10]+x[ 6], 7); x[ 2] ^= R(x[14]+x[10], 9);
  83. x[ 6] ^= R(x[ 2]+x[14],13); x[10] ^= R(x[ 6]+x[ 2],18);
  84. x[ 3] ^= R(x[15]+x[11], 7); x[ 7] ^= R(x[ 3]+x[15], 9);
  85. x[11] ^= R(x[ 7]+x[ 3],13); x[15] ^= R(x[11]+x[ 7],18);
  86. /* Operate on rows. */
  87. x[ 1] ^= R(x[ 0]+x[ 3], 7); x[ 2] ^= R(x[ 1]+x[ 0], 9);
  88. x[ 3] ^= R(x[ 2]+x[ 1],13); x[ 0] ^= R(x[ 3]+x[ 2],18);
  89. x[ 6] ^= R(x[ 5]+x[ 4], 7); x[ 7] ^= R(x[ 6]+x[ 5], 9);
  90. x[ 4] ^= R(x[ 7]+x[ 6],13); x[ 5] ^= R(x[ 4]+x[ 7],18);
  91. x[11] ^= R(x[10]+x[ 9], 7); x[ 8] ^= R(x[11]+x[10], 9);
  92. x[ 9] ^= R(x[ 8]+x[11],13); x[10] ^= R(x[ 9]+x[ 8],18);
  93. x[12] ^= R(x[15]+x[14], 7); x[13] ^= R(x[12]+x[15], 9);
  94. x[14] ^= R(x[13]+x[12],13); x[15] ^= R(x[14]+x[13],18);
  95. #undef R
  96. }
  97. for (i = 0; i < 16; i++)
  98. B[i] += x[i];
  99. }
  100. /**
  101. * blockmix_salsa8(Bin, Bout, X, r):
  102. * Compute Bout = BlockMix_{salsa20/8, r}(Bin). The input Bin must be 128r
  103. * bytes in length; the output Bout must also be the same size. The
  104. * temporary space X must be 64 bytes.
  105. */
  106. static void
  107. blockmix_salsa8(uint32_t * Bin, uint32_t * Bout, uint32_t * X, size_t r)
  108. {
  109. size_t i;
  110. /* 1: X <-- B_{2r - 1} */
  111. blkcpy(X, &Bin[(2 * r - 1) * 16], 64);
  112. /* 2: for i = 0 to 2r - 1 do */
  113. for (i = 0; i < 2 * r; i += 2) {
  114. /* 3: X <-- H(X \xor B_i) */
  115. blkxor(X, &Bin[i * 16], 64);
  116. salsa20_8(X);
  117. /* 4: Y_i <-- X */
  118. /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
  119. blkcpy(&Bout[i * 8], X, 64);
  120. /* 3: X <-- H(X \xor B_i) */
  121. blkxor(X, &Bin[i * 16 + 16], 64);
  122. salsa20_8(X);
  123. /* 4: Y_i <-- X */
  124. /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
  125. blkcpy(&Bout[i * 8 + r * 16], X, 64);
  126. }
  127. }
  128. /**
  129. * integerify(B, r):
  130. * Return the result of parsing B_{2r-1} as a little-endian integer.
  131. */
  132. static uint64_t
  133. integerify(void * B, size_t r)
  134. {
  135. uint32_t * X = (void *)((uintptr_t)(B) + (2 * r - 1) * 64);
  136. return (((uint64_t)(X[1]) << 32) + X[0]);
  137. }
  138. /**
  139. * smix(B, r, N, V, XY):
  140. * Compute B = SMix_r(B, N). The input B must be 128r bytes in length;
  141. * the temporary storage V must be 128rN bytes in length; the temporary
  142. * storage XY must be 256r + 64 bytes in length. The value N must be a
  143. * power of 2 greater than 1. The arrays B, V, and XY must be aligned to a
  144. * multiple of 64 bytes.
  145. */
  146. static void
  147. smix(uint8_t * B, size_t r, uint64_t N, uint32_t * V, uint32_t * XY)
  148. {
  149. uint32_t * X = XY;
  150. uint32_t * Y = &XY[32 * r];
  151. uint32_t * Z = &XY[64 * r];
  152. uint64_t i;
  153. uint64_t j;
  154. size_t k;
  155. /* 1: X <-- B */
  156. for (k = 0; k < 32 * r; k++)
  157. X[k] = le32dec(&B[4 * k]);
  158. /* 2: for i = 0 to N - 1 do */
  159. for (i = 0; i < N; i += 2) {
  160. /* 3: V_i <-- X */
  161. blkcpy(&V[i * (32 * r)], X, 128 * r);
  162. /* 4: X <-- H(X) */
  163. blockmix_salsa8(X, Y, Z, r);
  164. /* 3: V_i <-- X */
  165. blkcpy(&V[(i + 1) * (32 * r)], Y, 128 * r);
  166. /* 4: X <-- H(X) */
  167. blockmix_salsa8(Y, X, Z, r);
  168. }
  169. /* 6: for i = 0 to N - 1 do */
  170. for (i = 0; i < N; i += 2) {
  171. /* 7: j <-- Integerify(X) mod N */
  172. j = integerify(X, r) & (N - 1);
  173. /* 8: X <-- H(X \xor V_j) */
  174. blkxor(X, &V[j * (32 * r)], 128 * r);
  175. blockmix_salsa8(X, Y, Z, r);
  176. /* 7: j <-- Integerify(X) mod N */
  177. j = integerify(Y, r) & (N - 1);
  178. /* 8: X <-- H(X \xor V_j) */
  179. blkxor(Y, &V[j * (32 * r)], 128 * r);
  180. blockmix_salsa8(Y, X, Z, r);
  181. }
  182. /* 10: B' <-- X */
  183. for (k = 0; k < 32 * r; k++)
  184. le32enc(&B[4 * k], X[k]);
  185. }
  186. /**
  187. * scrypt(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen):
  188. * Compute scrypt(passwd[0 .. passwdlen - 1], salt[0 .. saltlen - 1], N, r,
  189. * p, buflen) and write the result into buf. The parameters r, p, and buflen
  190. * must satisfy r * p < 2^30 and buflen <= (2^32 - 1) * 32. The parameter N
  191. * must be a power of 2 greater than 1.
  192. *
  193. * Return 0 on success; or -1 on error.
  194. */
  195. int
  196. scrypt(const uint8_t * passwd, size_t passwdlen,
  197. const uint8_t * salt, size_t saltlen, uint64_t N, uint32_t r, uint32_t p,
  198. uint8_t * buf, size_t buflen)
  199. {
  200. void * B0, * V0, * XY0;
  201. uint8_t * B;
  202. uint32_t * V;
  203. uint32_t * XY;
  204. uint32_t i;
  205. /* Sanity-check parameters. */
  206. #if SIZE_MAX > UINT32_MAX
  207. if (buflen > (((uint64_t)(1) << 32) - 1) * 32) {
  208. errno = EFBIG;
  209. goto err0;
  210. }
  211. #endif
  212. if ((uint64_t)(r) * (uint64_t)(p) >= (1 << 30)) {
  213. errno = EFBIG;
  214. goto err0;
  215. }
  216. if (((N & (N - 1)) != 0) || (N == 0)) {
  217. errno = EINVAL;
  218. goto err0;
  219. }
  220. if ((r > SIZE_MAX / 128 / p) ||
  221. #if SIZE_MAX / 256 <= UINT32_MAX
  222. (r > SIZE_MAX / 256) ||
  223. #endif
  224. (N > SIZE_MAX / 128 / r)) {
  225. errno = ENOMEM;
  226. goto err0;
  227. }
  228. /* Allocate memory. */
  229. #ifdef HAVE_POSIX_MEMALIGN
  230. if ((errno = posix_memalign(&B0, 64, 128 * r * p)) != 0)
  231. goto err0;
  232. B = (uint8_t *)(B0);
  233. if ((errno = posix_memalign(&XY0, 64, 256 * r + 64)) != 0)
  234. goto err1;
  235. XY = (uint32_t *)(XY0);
  236. #ifndef MAP_ANON
  237. if ((errno = posix_memalign(&V0, 64, 128 * r * N)) != 0)
  238. goto err2;
  239. V = (uint32_t *)(V0);
  240. #endif
  241. #else
  242. if ((B0 = malloc(128 * r * p + 63)) == NULL)
  243. goto err0;
  244. B = (uint8_t *)(((uintptr_t)(B0) + 63) & ~ (uintptr_t)(63));
  245. if ((XY0 = malloc(256 * r + 64 + 63)) == NULL)
  246. goto err1;
  247. XY = (uint32_t *)(((uintptr_t)(XY0) + 63) & ~ (uintptr_t)(63));
  248. #ifndef MAP_ANON
  249. if ((V0 = malloc(128 * r * N + 63)) == NULL)
  250. goto err2;
  251. V = (uint32_t *)(((uintptr_t)(V0) + 63) & ~ (uintptr_t)(63));
  252. #endif
  253. #endif
  254. #ifdef MAP_ANON
  255. if ((V0 = mmap(NULL, 128 * r * N, PROT_READ | PROT_WRITE,
  256. #ifdef MAP_NOCORE
  257. MAP_ANON | MAP_PRIVATE | MAP_NOCORE,
  258. #else
  259. MAP_ANON | MAP_PRIVATE,
  260. #endif
  261. -1, 0)) == MAP_FAILED)
  262. goto err2;
  263. V = (uint32_t *)(V0);
  264. #endif
  265. /* 1: (B_0 ... B_{p-1}) <-- PBKDF2(P, S, 1, p * MFLen) */
  266. PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, 1, B, p * 128 * r);
  267. /* 2: for i = 0 to p - 1 do */
  268. for (i = 0; i < p; i++) {
  269. /* 3: B_i <-- MF(B_i, N) */
  270. smix(&B[i * 128 * r], r, N, V, XY);
  271. }
  272. /* 5: DK <-- PBKDF2(P, B, 1, dkLen) */
  273. PBKDF2_SHA256(passwd, passwdlen, B, p * 128 * r, 1, buf, buflen);
  274. /* Free memory. */
  275. #ifdef MAP_ANON
  276. if (munmap(V0, 128 * r * N))
  277. goto err2;
  278. #else
  279. free(V0);
  280. #endif
  281. free(XY0);
  282. free(B0);
  283. /* Success! */
  284. return (0);
  285. err2:
  286. free(XY0);
  287. err1:
  288. free(B0);
  289. err0:
  290. /* Failure! */
  291. return (-1);
  292. }