pwhash_scryptsalsa208sha256_sse.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. # define ARX(out, in1, in2, s) \
  48. { \
  49. __m128i T = _mm_add_epi32(in1, in2); \
  50. out = _mm_xor_si128(out, _mm_slli_epi32(T, s)); \
  51. out = _mm_xor_si128(out, _mm_srli_epi32(T, 32 - s)); \
  52. }
  53. # define SALSA20_2ROUNDS \
  54. /* Operate on "columns". */ \
  55. ARX(X1, X0, X3, 7) \
  56. ARX(X2, X1, X0, 9) \
  57. ARX(X3, X2, X1, 13) \
  58. ARX(X0, X3, X2, 18) \
  59. \
  60. /* Rearrange data. */ \
  61. X1 = _mm_shuffle_epi32(X1, 0x93); \
  62. X2 = _mm_shuffle_epi32(X2, 0x4E); \
  63. X3 = _mm_shuffle_epi32(X3, 0x39); \
  64. \
  65. /* Operate on "rows". */ \
  66. ARX(X3, X0, X1, 7) \
  67. ARX(X2, X3, X0, 9) \
  68. ARX(X1, X2, X3, 13) \
  69. ARX(X0, X1, X2, 18) \
  70. \
  71. /* Rearrange data. */ \
  72. X1 = _mm_shuffle_epi32(X1, 0x39); \
  73. X2 = _mm_shuffle_epi32(X2, 0x4E); \
  74. X3 = _mm_shuffle_epi32(X3, 0x93);
  75. /*
  76. * Apply the salsa20/8 core to the block provided in (X0 ... X3) ^ (Z0 ... Z3).
  77. */
  78. # define SALSA20_8_XOR(in, out) \
  79. { \
  80. __m128i Y0 = X0 = _mm_xor_si128(X0, (in)[0]); \
  81. __m128i Y1 = X1 = _mm_xor_si128(X1, (in)[1]); \
  82. __m128i Y2 = X2 = _mm_xor_si128(X2, (in)[2]); \
  83. __m128i Y3 = X3 = _mm_xor_si128(X3, (in)[3]); \
  84. SALSA20_2ROUNDS \
  85. SALSA20_2ROUNDS \
  86. SALSA20_2ROUNDS \
  87. SALSA20_2ROUNDS(out)[0] = X0 = _mm_add_epi32(X0, Y0); \
  88. (out)[1] = X1 = _mm_add_epi32(X1, Y1); \
  89. (out)[2] = X2 = _mm_add_epi32(X2, Y2); \
  90. (out)[3] = X3 = _mm_add_epi32(X3, Y3); \
  91. }
  92. /*
  93. * blockmix_salsa8(Bin, Bout, r):
  94. * Compute Bout = BlockMix_{salsa20/8, r}(Bin).
  95. * The input Bin must be 128r bytes in length;
  96. * the output Bout must also be the same size.
  97. */
  98. static inline void
  99. blockmix_salsa8(const __m128i *Bin, __m128i *Bout, size_t r)
  100. {
  101. __m128i X0, X1, X2, X3;
  102. size_t i;
  103. /* 1: X <-- B_{2r - 1} */
  104. X0 = Bin[8 * r - 4];
  105. X1 = Bin[8 * r - 3];
  106. X2 = Bin[8 * r - 2];
  107. X3 = Bin[8 * r - 1];
  108. /* 3: X <-- H(X \xor B_i) */
  109. /* 4: Y_i <-- X */
  110. /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
  111. SALSA20_8_XOR(Bin, Bout)
  112. /* 2: for i = 0 to 2r - 1 do */
  113. r--;
  114. for (i = 0; i < r;) {
  115. /* 3: X <-- H(X \xor B_i) */
  116. /* 4: Y_i <-- X */
  117. /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
  118. SALSA20_8_XOR(&Bin[i * 8 + 4], &Bout[(r + i) * 4 + 4])
  119. i++;
  120. /* 3: X <-- H(X \xor B_i) */
  121. /* 4: Y_i <-- X */
  122. /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
  123. SALSA20_8_XOR(&Bin[i * 8], &Bout[i * 4])
  124. }
  125. /* 3: X <-- H(X \xor B_i) */
  126. /* 4: Y_i <-- X */
  127. /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
  128. SALSA20_8_XOR(&Bin[i * 8 + 4], &Bout[(r + i) * 4 + 4])
  129. }
  130. # define XOR4(in) \
  131. X0 = _mm_xor_si128(X0, (in)[0]); \
  132. X1 = _mm_xor_si128(X1, (in)[1]); \
  133. X2 = _mm_xor_si128(X2, (in)[2]); \
  134. X3 = _mm_xor_si128(X3, (in)[3]);
  135. # define XOR4_2(in1, in2) \
  136. X0 = _mm_xor_si128((in1)[0], (in2)[0]); \
  137. X1 = _mm_xor_si128((in1)[1], (in2)[1]); \
  138. X2 = _mm_xor_si128((in1)[2], (in2)[2]); \
  139. X3 = _mm_xor_si128((in1)[3], (in2)[3]);
  140. static inline uint32_t
  141. blockmix_salsa8_xor(const __m128i *Bin1, const __m128i *Bin2, __m128i *Bout,
  142. size_t r)
  143. {
  144. __m128i X0, X1, X2, X3;
  145. size_t i;
  146. /* 1: X <-- B_{2r - 1} */
  147. XOR4_2(&Bin1[8 * r - 4], &Bin2[8 * r - 4])
  148. /* 3: X <-- H(X \xor B_i) */
  149. /* 4: Y_i <-- X */
  150. /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
  151. XOR4(Bin1)
  152. SALSA20_8_XOR(Bin2, Bout)
  153. /* 2: for i = 0 to 2r - 1 do */
  154. r--;
  155. for (i = 0; i < r;) {
  156. /* 3: X <-- H(X \xor B_i) */
  157. /* 4: Y_i <-- X */
  158. /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
  159. XOR4(&Bin1[i * 8 + 4])
  160. SALSA20_8_XOR(&Bin2[i * 8 + 4], &Bout[(r + i) * 4 + 4])
  161. i++;
  162. /* 3: X <-- H(X \xor B_i) */
  163. /* 4: Y_i <-- X */
  164. /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
  165. XOR4(&Bin1[i * 8])
  166. SALSA20_8_XOR(&Bin2[i * 8], &Bout[i * 4])
  167. }
  168. /* 3: X <-- H(X \xor B_i) */
  169. /* 4: Y_i <-- X */
  170. /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
  171. XOR4(&Bin1[i * 8 + 4])
  172. SALSA20_8_XOR(&Bin2[i * 8 + 4], &Bout[(r + i) * 4 + 4])
  173. return _mm_cvtsi128_si32(X0);
  174. }
  175. # undef ARX
  176. # undef SALSA20_2ROUNDS
  177. # undef SALSA20_8_XOR
  178. # undef XOR4
  179. # undef XOR4_2
  180. /*
  181. * integerify(B, r):
  182. * Return the result of parsing B_{2r-1} as a little-endian integer.
  183. * Note that B's layout is permuted compared to the generic implementation.
  184. */
  185. static inline uint64_t
  186. integerify(const void *B, size_t r)
  187. {
  188. const uint64_t *X = ((const uint64_t *) B) + (2 * r - 1) * 8;
  189. return *X;
  190. }
  191. /*
  192. * smix(B, r, N, V, XY):
  193. * Compute B = SMix_r(B, N). The input B must be 128r bytes in length;
  194. * the temporary storage V must be 128rN bytes in length; the temporary
  195. * storage XY must be 256r + 64 bytes in length. The value N must be a
  196. * power of 2 greater than 1. The arrays B, V, and XY must be aligned to a
  197. * multiple of 64 bytes.
  198. */
  199. static void
  200. smix(uint8_t *B, size_t r, uint64_t N, void *V, void *XY)
  201. {
  202. size_t s = 128 * r;
  203. __m128i *X = (__m128i *) V, *Y;
  204. uint32_t *X32 = (uint32_t *) V;
  205. uint64_t i, j;
  206. size_t k;
  207. /* 1: X <-- B */
  208. /* 3: V_i <-- X */
  209. for (k = 0; k < 2 * r; k++) {
  210. for (i = 0; i < 16; i++) {
  211. X32[k * 16 + i] = LOAD32_LE(&B[(k * 16 + (i * 5 % 16)) * 4]);
  212. }
  213. }
  214. /* 2: for i = 0 to N - 1 do */
  215. for (i = 1; i < N - 1; i += 2) {
  216. /* 4: X <-- H(X) */
  217. /* 3: V_i <-- X */
  218. Y = (__m128i *) ((uintptr_t)(V) + i * s);
  219. blockmix_salsa8(X, Y, r);
  220. /* 4: X <-- H(X) */
  221. /* 3: V_i <-- X */
  222. X = (__m128i *) ((uintptr_t)(V) + (i + 1) * s);
  223. blockmix_salsa8(Y, X, r);
  224. }
  225. /* 4: X <-- H(X) */
  226. /* 3: V_i <-- X */
  227. Y = (__m128i *) ((uintptr_t)(V) + i * s);
  228. blockmix_salsa8(X, Y, r);
  229. /* 4: X <-- H(X) */
  230. /* 3: V_i <-- X */
  231. X = (__m128i *) XY;
  232. blockmix_salsa8(Y, X, r);
  233. X32 = (uint32_t *) XY;
  234. Y = (__m128i *) ((uintptr_t)(XY) + s);
  235. /* 7: j <-- Integerify(X) mod N */
  236. j = integerify(X, r) & (N - 1);
  237. /* 6: for i = 0 to N - 1 do */
  238. for (i = 0; i < N; i += 2) {
  239. __m128i *V_j = (__m128i *) ((uintptr_t)(V) + j * s);
  240. /* 8: X <-- H(X \xor V_j) */
  241. /* 7: j <-- Integerify(X) mod N */
  242. j = blockmix_salsa8_xor(X, V_j, Y, r) & (N - 1);
  243. V_j = (__m128i *) ((uintptr_t)(V) + j * s);
  244. /* 8: X <-- H(X \xor V_j) */
  245. /* 7: j <-- Integerify(X) mod N */
  246. j = blockmix_salsa8_xor(Y, V_j, X, r) & (N - 1);
  247. }
  248. /* 10: B' <-- X */
  249. for (k = 0; k < 2 * r; k++) {
  250. for (i = 0; i < 16; i++) {
  251. STORE32_LE(&B[(k * 16 + (i * 5 % 16)) * 4], X32[k * 16 + i]);
  252. }
  253. }
  254. }
  255. /*
  256. * escrypt_kdf(local, passwd, passwdlen, salt, saltlen,
  257. * N, r, p, buf, buflen):
  258. * Compute scrypt(passwd[0 .. passwdlen - 1], salt[0 .. saltlen - 1], N, r,
  259. * p, buflen) and write the result into buf. The parameters r, p, and buflen
  260. * must satisfy r * p < 2^30 and buflen <= (2^32 - 1) * 32. The parameter N
  261. * must be a power of 2 greater than 1.
  262. *
  263. * Return 0 on success; or -1 on error.
  264. */
  265. int
  266. escrypt_kdf_sse(escrypt_local_t *local, const uint8_t *passwd, size_t passwdlen,
  267. const uint8_t *salt, size_t saltlen, uint64_t N, uint32_t _r,
  268. uint32_t _p, uint8_t *buf, size_t buflen)
  269. {
  270. size_t B_size, V_size, XY_size, need;
  271. uint8_t * B;
  272. uint32_t *V, *XY;
  273. size_t r = _r, p = _p;
  274. uint32_t i;
  275. /* Sanity-check parameters. */
  276. # if SIZE_MAX > UINT32_MAX
  277. /* LCOV_EXCL_START */
  278. if (buflen > (((uint64_t)(1) << 32) - 1) * 32) {
  279. errno = EFBIG;
  280. return -1;
  281. }
  282. /* LCOV_EXCL_END */
  283. # endif
  284. if ((uint64_t)(r) * (uint64_t)(p) >= ((uint64_t) 1 << 30)) {
  285. errno = EFBIG;
  286. return -1;
  287. }
  288. if (N > UINT32_MAX) {
  289. errno = EFBIG;
  290. return -1;
  291. }
  292. if (((N & (N - 1)) != 0) || (N < 2)) {
  293. errno = EINVAL;
  294. return -1;
  295. }
  296. if (r == 0 || p == 0) {
  297. errno = EINVAL;
  298. return -1;
  299. }
  300. /* LCOV_EXCL_START */
  301. if ((r > SIZE_MAX / 128 / p) ||
  302. # if SIZE_MAX / 256 <= UINT32_MAX
  303. (r > SIZE_MAX / 256) ||
  304. # endif
  305. (N > SIZE_MAX / 128 / r)) {
  306. errno = ENOMEM;
  307. return -1;
  308. }
  309. /* LCOV_EXCL_END */
  310. /* Allocate memory. */
  311. B_size = (size_t) 128 * r * p;
  312. V_size = (size_t) 128 * r * N;
  313. need = B_size + V_size;
  314. /* LCOV_EXCL_START */
  315. if (need < V_size) {
  316. errno = ENOMEM;
  317. return -1;
  318. }
  319. /* LCOV_EXCL_END */
  320. XY_size = (size_t) 256 * r + 64;
  321. need += XY_size;
  322. /* LCOV_EXCL_START */
  323. if (need < XY_size) {
  324. errno = ENOMEM;
  325. return -1;
  326. }
  327. /* LCOV_EXCL_END */
  328. if (local->size < need) {
  329. if (escrypt_free_region(local)) {
  330. return -1; /* LCOV_EXCL_LINE */
  331. }
  332. if (!escrypt_alloc_region(local, need)) {
  333. return -1; /* LCOV_EXCL_LINE */
  334. }
  335. }
  336. B = (uint8_t *) local->aligned;
  337. V = (uint32_t *) ((uint8_t *) B + B_size);
  338. XY = (uint32_t *) ((uint8_t *) V + V_size);
  339. /* 1: (B_0 ... B_{p-1}) <-- PBKDF2(P, S, 1, p * MFLen) */
  340. escrypt_PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, 1, B, B_size);
  341. /* 2: for i = 0 to p - 1 do */
  342. for (i = 0; i < p; i++) {
  343. /* 3: B_i <-- MF(B_i, N) */
  344. smix(&B[(size_t) 128 * i * r], r, N, V, XY);
  345. }
  346. /* 5: DK <-- PBKDF2(P, B, 1, dkLen) */
  347. escrypt_PBKDF2_SHA256(passwd, passwdlen, B, B_size, 1, buf, buflen);
  348. /* Success! */
  349. return 0;
  350. }
  351. #endif