crypto_scrypt.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 "cpusupport.h"
  37. #include "sha256.h"
  38. #include "warnp.h"
  39. #include "crypto_scrypt_smix.h"
  40. #include "crypto_scrypt_smix_sse2.h"
  41. #include "crypto_scrypt.h"
  42. static void (*smix_func)(uint8_t *, size_t, uint64_t, void *, void *) = NULL;
  43. /**
  44. * _crypto_scrypt(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen, smix):
  45. * Perform the requested scrypt computation, using ${smix} as the smix routine.
  46. */
  47. static int
  48. _crypto_scrypt(const uint8_t * passwd, size_t passwdlen,
  49. const uint8_t * salt, size_t saltlen, uint64_t N, uint32_t _r, uint32_t _p,
  50. uint8_t * buf, size_t buflen,
  51. void (*smix)(uint8_t *, size_t, uint64_t, void *, void *))
  52. {
  53. void * B0, * V0, * XY0;
  54. uint8_t * B;
  55. uint32_t * V;
  56. uint32_t * XY;
  57. size_t r = _r, p = _p;
  58. uint32_t i;
  59. /* Sanity-check parameters. */
  60. #if SIZE_MAX > UINT32_MAX
  61. if (buflen > (((uint64_t)(1) << 32) - 1) * 32) {
  62. errno = EFBIG;
  63. goto err0;
  64. }
  65. #endif
  66. if ((uint64_t)(r) * (uint64_t)(p) >= (1 << 30)) {
  67. errno = EFBIG;
  68. goto err0;
  69. }
  70. if (((N & (N - 1)) != 0) || (N < 2)) {
  71. errno = EINVAL;
  72. goto err0;
  73. }
  74. if ((r > SIZE_MAX / 128 / p) ||
  75. #if SIZE_MAX / 256 <= UINT32_MAX
  76. (r > (SIZE_MAX - 64) / 256) ||
  77. #endif
  78. (N > SIZE_MAX / 128 / r)) {
  79. errno = ENOMEM;
  80. goto err0;
  81. }
  82. /* Allocate memory. */
  83. #ifdef HAVE_POSIX_MEMALIGN
  84. if ((errno = posix_memalign(&B0, 64, 128 * r * p)) != 0)
  85. goto err0;
  86. B = (uint8_t *)(B0);
  87. if ((errno = posix_memalign(&XY0, 64, 256 * r + 64)) != 0)
  88. goto err1;
  89. XY = (uint32_t *)(XY0);
  90. #if !defined(MAP_ANON) || !defined(HAVE_MMAP)
  91. if ((errno = posix_memalign(&V0, 64, (size_t)(128 * r * N))) != 0)
  92. goto err2;
  93. V = (uint32_t *)(V0);
  94. #endif
  95. #else
  96. if ((B0 = malloc(128 * r * p + 63)) == NULL)
  97. goto err0;
  98. B = (uint8_t *)(((uintptr_t)(B0) + 63) & ~ (uintptr_t)(63));
  99. if ((XY0 = malloc(256 * r + 64 + 63)) == NULL)
  100. goto err1;
  101. XY = (uint32_t *)(((uintptr_t)(XY0) + 63) & ~ (uintptr_t)(63));
  102. #if !defined(MAP_ANON) || !defined(HAVE_MMAP)
  103. if ((V0 = malloc(128 * r * N + 63)) == NULL)
  104. goto err2;
  105. V = (uint32_t *)(((uintptr_t)(V0) + 63) & ~ (uintptr_t)(63));
  106. #endif
  107. #endif
  108. #if defined(MAP_ANON) && defined(HAVE_MMAP)
  109. if ((V0 = mmap(NULL, 128 * r * N, PROT_READ | PROT_WRITE,
  110. #ifdef MAP_NOCORE
  111. MAP_ANON | MAP_PRIVATE | MAP_NOCORE,
  112. #else
  113. MAP_ANON | MAP_PRIVATE,
  114. #endif
  115. -1, 0)) == MAP_FAILED)
  116. goto err2;
  117. V = (uint32_t *)(V0);
  118. #endif
  119. /* 1: (B_0 ... B_{p-1}) <-- PBKDF2(P, S, 1, p * MFLen) */
  120. PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, 1, B, p * 128 * r);
  121. /* 2: for i = 0 to p - 1 do */
  122. for (i = 0; i < p; i++) {
  123. /* 3: B_i <-- MF(B_i, N) */
  124. (smix)(&B[i * 128 * r], r, N, V, XY);
  125. }
  126. /* 5: DK <-- PBKDF2(P, B, 1, dkLen) */
  127. PBKDF2_SHA256(passwd, passwdlen, B, p * 128 * r, 1, buf, buflen);
  128. /* Free memory. */
  129. #if defined(MAP_ANON) && defined(HAVE_MMAP)
  130. if (munmap(V0, 128 * r * N))
  131. goto err2;
  132. #else
  133. free(V0);
  134. #endif
  135. free(XY0);
  136. free(B0);
  137. /* Success! */
  138. return (0);
  139. err2:
  140. free(XY0);
  141. err1:
  142. free(B0);
  143. err0:
  144. /* Failure! */
  145. return (-1);
  146. }
  147. #define TESTLEN 64
  148. static struct scrypt_test {
  149. const char * passwd;
  150. const char * salt;
  151. uint64_t N;
  152. uint32_t r;
  153. uint32_t p;
  154. uint8_t result[TESTLEN];
  155. } testcase = {
  156. .passwd = "pleaseletmein",
  157. .salt = "SodiumChloride",
  158. .N = 16,
  159. .r = 8,
  160. .p = 1,
  161. .result = {
  162. 0x25, 0xa9, 0xfa, 0x20, 0x7f, 0x87, 0xca, 0x09,
  163. 0xa4, 0xef, 0x8b, 0x9f, 0x77, 0x7a, 0xca, 0x16,
  164. 0xbe, 0xb7, 0x84, 0xae, 0x18, 0x30, 0xbf, 0xbf,
  165. 0xd3, 0x83, 0x25, 0xaa, 0xbb, 0x93, 0x77, 0xdf,
  166. 0x1b, 0xa7, 0x84, 0xd7, 0x46, 0xea, 0x27, 0x3b,
  167. 0xf5, 0x16, 0xa4, 0x6f, 0xbf, 0xac, 0xf5, 0x11,
  168. 0xc5, 0xbe, 0xba, 0x4c, 0x4a, 0xb3, 0xac, 0xc7,
  169. 0xfa, 0x6f, 0x46, 0x0b, 0x6c, 0x0f, 0x47, 0x7b,
  170. }
  171. };
  172. static int
  173. testsmix(void (*smix)(uint8_t *, size_t, uint64_t, void *, void *))
  174. {
  175. uint8_t hbuf[TESTLEN];
  176. /* Perform the computation. */
  177. if (_crypto_scrypt(
  178. (const uint8_t *)testcase.passwd, strlen(testcase.passwd),
  179. (const uint8_t *)testcase.salt, strlen(testcase.salt),
  180. testcase.N, testcase.r, testcase.p, hbuf, TESTLEN, smix))
  181. return (-1);
  182. /* Does it match? */
  183. return (memcmp(testcase.result, hbuf, TESTLEN));
  184. }
  185. static void
  186. selectsmix(void)
  187. {
  188. #ifdef CPUSUPPORT_X86_SSE2
  189. /* If we're running on an SSE2-capable CPU, try that code. */
  190. if (cpusupport_x86_sse2()) {
  191. /* If SSE2ized smix works, use it. */
  192. if (!testsmix(crypto_scrypt_smix_sse2)) {
  193. smix_func = crypto_scrypt_smix_sse2;
  194. return;
  195. }
  196. warn0("Disabling broken SSE2 scrypt support - please report bug!");
  197. }
  198. #endif
  199. /* If generic smix works, use it. */
  200. if (!testsmix(crypto_scrypt_smix)) {
  201. smix_func = crypto_scrypt_smix;
  202. return;
  203. }
  204. warn0("Generic scrypt code is broken - please report bug!");
  205. /* If we get here, something really bad happened. */
  206. abort();
  207. }
  208. /**
  209. * crypto_scrypt(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen):
  210. * Compute scrypt(passwd[0 .. passwdlen - 1], salt[0 .. saltlen - 1], N, r,
  211. * p, buflen) and write the result into buf. The parameters r, p, and buflen
  212. * must satisfy r * p < 2^30 and buflen <= (2^32 - 1) * 32. The parameter N
  213. * must be a power of 2 greater than 1.
  214. *
  215. * Return 0 on success; or -1 on error.
  216. */
  217. int
  218. crypto_scrypt(const uint8_t * passwd, size_t passwdlen,
  219. const uint8_t * salt, size_t saltlen, uint64_t N, uint32_t _r, uint32_t _p,
  220. uint8_t * buf, size_t buflen)
  221. {
  222. if (smix_func == NULL)
  223. selectsmix();
  224. return (_crypto_scrypt(passwd, passwdlen, salt, saltlen, N, _r, _p,
  225. buf, buflen, smix_func));
  226. }