pwhash_scryptsalsa208sha256_sse.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*-
  2. * Copyright 2009 Colin Percival
  3. * Copyright 2012,2013 Alexander Peslyak
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. * SUCH DAMAGE.
  26. *
  27. * This file was originally written by Colin Percival as part of the Tarsnap
  28. * online backup system.
  29. */
  30. #include <errno.h>
  31. #include <limits.h>
  32. #include <stdint.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include "private/common.h"
  36. #include "private/sse2_64_32.h"
  37. #ifdef HAVE_EMMINTRIN_H
  38. # ifdef __GNUC__
  39. # pragma GCC target("sse2")
  40. # endif
  41. # include <emmintrin.h>
  42. # if defined(__XOP__) && defined(DISABLED)
  43. # include <x86intrin.h>
  44. # endif
  45. # include "../crypto_scrypt.h"
  46. # include "../pbkdf2-sha256.h"
  47. # if defined(__XOP__) && defined(DISABLED)
  48. # define ARX(out, in1, in2, s) \
  49. out = _mm_xor_si128(out, _mm_roti_epi32(_mm_add_epi32(in1, in2), s));
  50. # else
  51. # define ARX(out, in1, in2, s) \
  52. { \
  53. __m128i T = _mm_add_epi32(in1, in2); \
  54. out = _mm_xor_si128(out, _mm_slli_epi32(T, s)); \
  55. out = _mm_xor_si128(out, _mm_srli_epi32(T, 32 - s)); \
  56. }
  57. # endif
  58. # define SALSA20_2ROUNDS \
  59. /* Operate on "columns". */ \
  60. ARX(X1, X0, X3, 7) \
  61. ARX(X2, X1, X0, 9) \
  62. ARX(X3, X2, X1, 13) \
  63. ARX(X0, X3, X2, 18) \
  64. \
  65. /* Rearrange data. */ \
  66. X1 = _mm_shuffle_epi32(X1, 0x93); \
  67. X2 = _mm_shuffle_epi32(X2, 0x4E); \
  68. X3 = _mm_shuffle_epi32(X3, 0x39); \
  69. \
  70. /* Operate on "rows". */ \
  71. ARX(X3, X0, X1, 7) \
  72. ARX(X2, X3, X0, 9) \
  73. ARX(X1, X2, X3, 13) \
  74. ARX(X0, X1, X2, 18) \
  75. \
  76. /* Rearrange data. */ \
  77. X1 = _mm_shuffle_epi32(X1, 0x39); \
  78. X2 = _mm_shuffle_epi32(X2, 0x4E); \
  79. X3 = _mm_shuffle_epi32(X3, 0x93);
  80. /**
  81. * Apply the salsa20/8 core to the block provided in (X0 ... X3) ^ (Z0 ... Z3).
  82. */
  83. # define SALSA20_8_XOR(in, out) \
  84. { \
  85. __m128i Y0 = X0 = _mm_xor_si128(X0, (in)[0]); \
  86. __m128i Y1 = X1 = _mm_xor_si128(X1, (in)[1]); \
  87. __m128i Y2 = X2 = _mm_xor_si128(X2, (in)[2]); \
  88. __m128i Y3 = X3 = _mm_xor_si128(X3, (in)[3]); \
  89. SALSA20_2ROUNDS \
  90. SALSA20_2ROUNDS \
  91. SALSA20_2ROUNDS \
  92. SALSA20_2ROUNDS(out)[0] = X0 = _mm_add_epi32(X0, Y0); \
  93. (out)[1] = X1 = _mm_add_epi32(X1, Y1); \
  94. (out)[2] = X2 = _mm_add_epi32(X2, Y2); \
  95. (out)[3] = X3 = _mm_add_epi32(X3, Y3); \
  96. }
  97. /**
  98. * blockmix_salsa8(Bin, Bout, r):
  99. * Compute Bout = BlockMix_{salsa20/8, r}(Bin). The input Bin must be 128r
  100. * bytes in length; the output Bout must also be the same size.
  101. */
  102. static inline void
  103. blockmix_salsa8(const __m128i *Bin, __m128i *Bout, size_t r)
  104. {
  105. __m128i X0, X1, X2, X3;
  106. size_t i;
  107. /* 1: X <-- B_{2r - 1} */
  108. X0 = Bin[8 * r - 4];
  109. X1 = Bin[8 * r - 3];
  110. X2 = Bin[8 * r - 2];
  111. X3 = Bin[8 * r - 1];
  112. /* 3: X <-- H(X \xor B_i) */
  113. /* 4: Y_i <-- X */
  114. /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
  115. SALSA20_8_XOR(Bin, Bout)
  116. /* 2: for i = 0 to 2r - 1 do */
  117. r--;
  118. for (i = 0; i < r;) {
  119. /* 3: X <-- H(X \xor B_i) */
  120. /* 4: Y_i <-- X */
  121. /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
  122. SALSA20_8_XOR(&Bin[i * 8 + 4], &Bout[(r + i) * 4 + 4])
  123. i++;
  124. /* 3: X <-- H(X \xor B_i) */
  125. /* 4: Y_i <-- X */
  126. /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
  127. SALSA20_8_XOR(&Bin[i * 8], &Bout[i * 4])
  128. }
  129. /* 3: X <-- H(X \xor B_i) */
  130. /* 4: Y_i <-- X */
  131. /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
  132. SALSA20_8_XOR(&Bin[i * 8 + 4], &Bout[(r + i) * 4 + 4])
  133. }
  134. # define XOR4(in) \
  135. X0 = _mm_xor_si128(X0, (in)[0]); \
  136. X1 = _mm_xor_si128(X1, (in)[1]); \
  137. X2 = _mm_xor_si128(X2, (in)[2]); \
  138. X3 = _mm_xor_si128(X3, (in)[3]);
  139. # define XOR4_2(in1, in2) \
  140. X0 = _mm_xor_si128((in1)[0], (in2)[0]); \
  141. X1 = _mm_xor_si128((in1)[1], (in2)[1]); \
  142. X2 = _mm_xor_si128((in1)[2], (in2)[2]); \
  143. X3 = _mm_xor_si128((in1)[3], (in2)[3]);
  144. static inline uint32_t
  145. blockmix_salsa8_xor(const __m128i *Bin1, const __m128i *Bin2, __m128i *Bout,
  146. size_t r)
  147. {
  148. __m128i X0, X1, X2, X3;
  149. size_t i;
  150. /* 1: X <-- B_{2r - 1} */
  151. XOR4_2(&Bin1[8 * r - 4], &Bin2[8 * r - 4])
  152. /* 3: X <-- H(X \xor B_i) */
  153. /* 4: Y_i <-- X */
  154. /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
  155. XOR4(Bin1)
  156. SALSA20_8_XOR(Bin2, Bout)
  157. /* 2: for i = 0 to 2r - 1 do */
  158. r--;
  159. for (i = 0; i < r;) {
  160. /* 3: X <-- H(X \xor B_i) */
  161. /* 4: Y_i <-- X */
  162. /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
  163. XOR4(&Bin1[i * 8 + 4])
  164. SALSA20_8_XOR(&Bin2[i * 8 + 4], &Bout[(r + i) * 4 + 4])
  165. i++;
  166. /* 3: X <-- H(X \xor B_i) */
  167. /* 4: Y_i <-- X */
  168. /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
  169. XOR4(&Bin1[i * 8])
  170. SALSA20_8_XOR(&Bin2[i * 8], &Bout[i * 4])
  171. }
  172. /* 3: X <-- H(X \xor B_i) */
  173. /* 4: Y_i <-- X */
  174. /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
  175. XOR4(&Bin1[i * 8 + 4])
  176. SALSA20_8_XOR(&Bin2[i * 8 + 4], &Bout[(r + i) * 4 + 4])
  177. return _mm_cvtsi128_si32(X0);
  178. }
  179. # undef ARX
  180. # undef SALSA20_2ROUNDS
  181. # undef SALSA20_8_XOR
  182. # undef XOR4
  183. # undef XOR4_2
  184. /**
  185. * integerify(B, r):
  186. * Return the result of parsing B_{2r-1} as a little-endian integer.
  187. * Note that B's layout is permuted compared to the generic implementation.
  188. */
  189. static inline uint32_t
  190. integerify(const void *B, size_t r)
  191. {
  192. return *(const uint32_t *) ((uintptr_t)(B) + (2 * r - 1) * 64);
  193. }
  194. /**
  195. * smix(B, r, N, V, XY):
  196. * Compute B = SMix_r(B, N). The input B must be 128r bytes in length;
  197. * the temporary storage V must be 128rN bytes in length; the temporary
  198. * storage XY must be 256r + 64 bytes in length. The value N must be a
  199. * power of 2 greater than 1. The arrays B, V, and XY must be aligned to a
  200. * multiple of 64 bytes.
  201. */
  202. static void
  203. smix(uint8_t *B, size_t r, uint32_t N, void *V, void *XY)
  204. {
  205. size_t s = 128 * r;
  206. __m128i * X = (__m128i *) V, *Y;
  207. uint32_t *X32 = (uint32_t *) V;
  208. uint32_t i, j;
  209. size_t k;
  210. /* 1: X <-- B */
  211. /* 3: V_i <-- X */
  212. for (k = 0; k < 2 * r; k++) {
  213. for (i = 0; i < 16; i++) {
  214. X32[k * 16 + i] = LOAD32_LE(&B[(k * 16 + (i * 5 % 16)) * 4]);
  215. }
  216. }
  217. /* 2: for i = 0 to N - 1 do */
  218. for (i = 1; i < N - 1; i += 2) {
  219. /* 4: X <-- H(X) */
  220. /* 3: V_i <-- X */
  221. Y = (__m128i *) ((uintptr_t)(V) + i * s);
  222. blockmix_salsa8(X, Y, r);
  223. /* 4: X <-- H(X) */
  224. /* 3: V_i <-- X */
  225. X = (__m128i *) ((uintptr_t)(V) + (i + 1) * s);
  226. blockmix_salsa8(Y, X, r);
  227. }
  228. /* 4: X <-- H(X) */
  229. /* 3: V_i <-- X */
  230. Y = (__m128i *) ((uintptr_t)(V) + i * s);
  231. blockmix_salsa8(X, Y, r);
  232. /* 4: X <-- H(X) */
  233. /* 3: V_i <-- X */
  234. X = (__m128i *) XY;
  235. blockmix_salsa8(Y, X, r);
  236. X32 = (uint32_t *) XY;
  237. Y = (__m128i *) ((uintptr_t)(XY) + s);
  238. /* 7: j <-- Integerify(X) mod N */
  239. j = integerify(X, r) & (N - 1);
  240. /* 6: for i = 0 to N - 1 do */
  241. for (i = 0; i < N; i += 2) {
  242. __m128i *V_j = (__m128i *) ((uintptr_t)(V) + j * s);
  243. /* 8: X <-- H(X \xor V_j) */
  244. /* 7: j <-- Integerify(X) mod N */
  245. j = blockmix_salsa8_xor(X, V_j, Y, r) & (N - 1);
  246. V_j = (__m128i *) ((uintptr_t)(V) + j * s);
  247. /* 8: X <-- H(X \xor V_j) */
  248. /* 7: j <-- Integerify(X) mod N */
  249. j = blockmix_salsa8_xor(Y, V_j, X, r) & (N - 1);
  250. }
  251. /* 10: B' <-- X */
  252. for (k = 0; k < 2 * r; k++) {
  253. for (i = 0; i < 16; i++) {
  254. STORE32_LE(&B[(k * 16 + (i * 5 % 16)) * 4], X32[k * 16 + i]);
  255. }
  256. }
  257. }
  258. /**
  259. * escrypt_kdf(local, passwd, passwdlen, salt, saltlen,
  260. * N, r, p, buf, buflen):
  261. * Compute scrypt(passwd[0 .. passwdlen - 1], salt[0 .. saltlen - 1], N, r,
  262. * p, buflen) and write the result into buf. The parameters r, p, and buflen
  263. * must satisfy r * p < 2^30 and buflen <= (2^32 - 1) * 32. The parameter N
  264. * must be a power of 2 greater than 1.
  265. *
  266. * Return 0 on success; or -1 on error.
  267. */
  268. int
  269. escrypt_kdf_sse(escrypt_local_t *local, const uint8_t *passwd, size_t passwdlen,
  270. const uint8_t *salt, size_t saltlen, uint64_t N, uint32_t _r,
  271. uint32_t _p, uint8_t *buf, size_t buflen)
  272. {
  273. size_t B_size, V_size, XY_size, need;
  274. uint8_t * B;
  275. uint32_t *V, *XY;
  276. size_t r = _r, p = _p;
  277. uint32_t i;
  278. /* Sanity-check parameters. */
  279. if (r == 0 || p == 0) {
  280. errno = EINVAL;
  281. return -1;
  282. }
  283. # if SIZE_MAX > UINT32_MAX
  284. /* LCOV_EXCL_START */
  285. if (buflen > (((uint64_t)(1) << 32) - 1) * 32) {
  286. errno = EFBIG;
  287. return -1;
  288. }
  289. /* LCOV_EXCL_END */
  290. # endif
  291. if ((uint64_t)(r) * (uint64_t)(p) >= ((uint64_t) 1 << 30)) {
  292. errno = EFBIG;
  293. return -1;
  294. }
  295. if (N > UINT32_MAX) {
  296. errno = EFBIG;
  297. return -1;
  298. }
  299. if (((N & (N - 1)) != 0) || (N < 2)) {
  300. errno = EINVAL;
  301. return -1;
  302. }
  303. if (r == 0 || p == 0) {
  304. errno = EINVAL;
  305. return -1;
  306. }
  307. /* LCOV_EXCL_START */
  308. if ((r > SIZE_MAX / 128 / p) ||
  309. # if SIZE_MAX / 256 <= UINT32_MAX
  310. (r > SIZE_MAX / 256) ||
  311. # endif
  312. (N > SIZE_MAX / 128 / r)) {
  313. errno = ENOMEM;
  314. return -1;
  315. }
  316. /* LCOV_EXCL_END */
  317. /* Allocate memory. */
  318. B_size = (size_t) 128 * r * p;
  319. V_size = (size_t) 128 * r * N;
  320. need = B_size + V_size;
  321. /* LCOV_EXCL_START */
  322. if (need < V_size) {
  323. errno = ENOMEM;
  324. return -1;
  325. }
  326. /* LCOV_EXCL_END */
  327. XY_size = (size_t) 256 * r + 64;
  328. need += XY_size;
  329. /* LCOV_EXCL_START */
  330. if (need < XY_size) {
  331. errno = ENOMEM;
  332. return -1;
  333. }
  334. /* LCOV_EXCL_END */
  335. if (local->size < need) {
  336. if (free_region(local)) {
  337. return -1; /* LCOV_EXCL_LINE */
  338. }
  339. if (!alloc_region(local, need)) {
  340. return -1; /* LCOV_EXCL_LINE */
  341. }
  342. }
  343. B = (uint8_t *) local->aligned;
  344. V = (uint32_t *) ((uint8_t *) B + B_size);
  345. XY = (uint32_t *) ((uint8_t *) V + V_size);
  346. /* 1: (B_0 ... B_{p-1}) <-- PBKDF2(P, S, 1, p * MFLen) */
  347. PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, 1, B, B_size);
  348. /* 2: for i = 0 to p - 1 do */
  349. for (i = 0; i < p; i++) {
  350. /* 3: B_i <-- MF(B_i, N) */
  351. smix(&B[(size_t) 128 * i * r], r, (uint32_t) N, V, XY);
  352. }
  353. /* 5: DK <-- PBKDF2(P, B, 1, dkLen) */
  354. PBKDF2_SHA256(passwd, passwdlen, B, B_size, 1, buf, buflen);
  355. /* Success! */
  356. return 0;
  357. }
  358. #endif