scrypt-sse.c 9.4 KB

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