scrypt-sse.c 9.3 KB

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