scrypt-ref.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 <errno.h>
  31. #include <stdint.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include "sha256.h"
  35. #include "sysendian.h"
  36. #include "scrypt.h"
  37. static void blkcpy(uint8_t *, uint8_t *, size_t);
  38. static void blkxor(uint8_t *, uint8_t *, size_t);
  39. static void salsa20_8(uint8_t[64]);
  40. static void blockmix_salsa8(uint8_t *, uint8_t *, size_t);
  41. static uint64_t integerify(uint8_t *, size_t);
  42. static void smix(uint8_t *, size_t, uint64_t, uint8_t *, uint8_t *);
  43. static void
  44. blkcpy(uint8_t * dest, uint8_t * src, size_t len)
  45. {
  46. size_t i;
  47. for (i = 0; i < len; i++)
  48. dest[i] = src[i];
  49. }
  50. static void
  51. blkxor(uint8_t * dest, uint8_t * src, size_t len)
  52. {
  53. size_t i;
  54. for (i = 0; i < len; i++)
  55. dest[i] ^= src[i];
  56. }
  57. /**
  58. * salsa20_8(B):
  59. * Apply the salsa20/8 core to the provided block.
  60. */
  61. static void
  62. salsa20_8(uint8_t B[64])
  63. {
  64. uint32_t B32[16];
  65. uint32_t x[16];
  66. size_t i;
  67. /* Convert little-endian values in. */
  68. for (i = 0; i < 16; i++)
  69. B32[i] = le32dec(&B[i * 4]);
  70. /* Compute x = doubleround^4(B32). */
  71. for (i = 0; i < 16; i++)
  72. x[i] = B32[i];
  73. for (i = 0; i < 8; i += 2) {
  74. #define R(a,b) (((a) << (b)) | ((a) >> (32 - (b))))
  75. /* Operate on columns. */
  76. x[ 4] ^= R(x[ 0]+x[12], 7); x[ 8] ^= R(x[ 4]+x[ 0], 9);
  77. x[12] ^= R(x[ 8]+x[ 4],13); x[ 0] ^= R(x[12]+x[ 8],18);
  78. x[ 9] ^= R(x[ 5]+x[ 1], 7); x[13] ^= R(x[ 9]+x[ 5], 9);
  79. x[ 1] ^= R(x[13]+x[ 9],13); x[ 5] ^= R(x[ 1]+x[13],18);
  80. x[14] ^= R(x[10]+x[ 6], 7); x[ 2] ^= R(x[14]+x[10], 9);
  81. x[ 6] ^= R(x[ 2]+x[14],13); x[10] ^= R(x[ 6]+x[ 2],18);
  82. x[ 3] ^= R(x[15]+x[11], 7); x[ 7] ^= R(x[ 3]+x[15], 9);
  83. x[11] ^= R(x[ 7]+x[ 3],13); x[15] ^= R(x[11]+x[ 7],18);
  84. /* Operate on rows. */
  85. x[ 1] ^= R(x[ 0]+x[ 3], 7); x[ 2] ^= R(x[ 1]+x[ 0], 9);
  86. x[ 3] ^= R(x[ 2]+x[ 1],13); x[ 0] ^= R(x[ 3]+x[ 2],18);
  87. x[ 6] ^= R(x[ 5]+x[ 4], 7); x[ 7] ^= R(x[ 6]+x[ 5], 9);
  88. x[ 4] ^= R(x[ 7]+x[ 6],13); x[ 5] ^= R(x[ 4]+x[ 7],18);
  89. x[11] ^= R(x[10]+x[ 9], 7); x[ 8] ^= R(x[11]+x[10], 9);
  90. x[ 9] ^= R(x[ 8]+x[11],13); x[10] ^= R(x[ 9]+x[ 8],18);
  91. x[12] ^= R(x[15]+x[14], 7); x[13] ^= R(x[12]+x[15], 9);
  92. x[14] ^= R(x[13]+x[12],13); x[15] ^= R(x[14]+x[13],18);
  93. #undef R
  94. }
  95. /* Compute B32 = B32 + x. */
  96. for (i = 0; i < 16; i++)
  97. B32[i] += x[i];
  98. /* Convert little-endian values out. */
  99. for (i = 0; i < 16; i++)
  100. le32enc(&B[4 * i], B32[i]);
  101. }
  102. /**
  103. * blockmix_salsa8(B, Y, r):
  104. * Compute B = BlockMix_{salsa20/8, r}(B). The input B must be 128r bytes in
  105. * length; the temporary space Y must also be the same size.
  106. */
  107. static void
  108. blockmix_salsa8(uint8_t * B, uint8_t * Y, size_t r)
  109. {
  110. uint8_t X[64];
  111. size_t i;
  112. /* 1: X <-- B_{2r - 1} */
  113. blkcpy(X, &B[(2 * r - 1) * 64], 64);
  114. /* 2: for i = 0 to 2r - 1 do */
  115. for (i = 0; i < 2 * r; i++) {
  116. /* 3: X <-- H(X \xor B_i) */
  117. blkxor(X, &B[i * 64], 64);
  118. salsa20_8(X);
  119. /* 4: Y_i <-- X */
  120. blkcpy(&Y[i * 64], X, 64);
  121. }
  122. /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
  123. for (i = 0; i < r; i++)
  124. blkcpy(&B[i * 64], &Y[(i * 2) * 64], 64);
  125. for (i = 0; i < r; i++)
  126. blkcpy(&B[(i + r) * 64], &Y[(i * 2 + 1) * 64], 64);
  127. }
  128. /**
  129. * integerify(B, r):
  130. * Return the result of parsing B_{2r-1} as a little-endian integer.
  131. */
  132. static uint64_t
  133. integerify(uint8_t * B, size_t r)
  134. {
  135. uint8_t * X = &B[(2 * r - 1) * 64];
  136. return (le64dec(X));
  137. }
  138. /**
  139. * smix(B, r, N, V, XY):
  140. * Compute B = SMix_r(B, N). The input B must be 128r bytes in length; the
  141. * temporary storage V must be 128rN bytes in length; the temporary storage
  142. * XY must be 256r bytes in length. The value N must be a power of 2.
  143. */
  144. static void
  145. smix(uint8_t * B, size_t r, uint64_t N, uint8_t * V, uint8_t * XY)
  146. {
  147. uint8_t * X = XY;
  148. uint8_t * Y = &XY[128 * r];
  149. uint64_t i;
  150. uint64_t j;
  151. /* 1: X <-- B */
  152. blkcpy(X, B, 128 * r);
  153. /* 2: for i = 0 to N - 1 do */
  154. for (i = 0; i < N; i++) {
  155. /* 3: V_i <-- X */
  156. blkcpy(&V[i * (128 * r)], X, 128 * r);
  157. /* 4: X <-- H(X) */
  158. blockmix_salsa8(X, Y, r);
  159. }
  160. /* 6: for i = 0 to N - 1 do */
  161. for (i = 0; i < N; i++) {
  162. /* 7: j <-- Integerify(X) mod N */
  163. j = integerify(X, r) & (N - 1);
  164. /* 8: X <-- H(X \xor V_j) */
  165. blkxor(X, &V[j * (128 * r)], 128 * r);
  166. blockmix_salsa8(X, Y, r);
  167. }
  168. /* 10: B' <-- X */
  169. blkcpy(B, X, 128 * r);
  170. }
  171. /**
  172. * scrypt(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen):
  173. * Compute scrypt(passwd[0 .. passwdlen - 1], salt[0 .. saltlen - 1], N, r,
  174. * p, buflen) and write the result into buf. The parameters r, p, and buflen
  175. * must satisfy r * p < 2^30 and buflen <= (2^32 - 1) * 32. The parameter N
  176. * must be a power of 2.
  177. *
  178. * Return 0 on success; or -1 on error.
  179. */
  180. int
  181. scrypt(const uint8_t * passwd, size_t passwdlen,
  182. const uint8_t * salt, size_t saltlen, uint64_t N, uint32_t r, uint32_t p,
  183. uint8_t * buf, size_t buflen)
  184. {
  185. uint8_t * B;
  186. uint8_t * V;
  187. uint8_t * XY;
  188. uint32_t i;
  189. /* Sanity-check parameters. */
  190. #if SIZE_MAX > UINT32_MAX
  191. if (buflen > (((uint64_t)(1) << 32) - 1) * 32) {
  192. errno = EFBIG;
  193. goto err0;
  194. }
  195. #endif
  196. if ((uint64_t)(r) * (uint64_t)(p) >= (1 << 30)) {
  197. errno = EFBIG;
  198. goto err0;
  199. }
  200. if (((N & (N - 1)) != 0) || (N == 0)) {
  201. errno = EINVAL;
  202. goto err0;
  203. }
  204. if ((r > SIZE_MAX / 128 / p) ||
  205. #if SIZE_MAX / 256 <= UINT32_MAX
  206. (r > SIZE_MAX / 256) ||
  207. #endif
  208. (N > SIZE_MAX / 128 / r)) {
  209. errno = ENOMEM;
  210. goto err0;
  211. }
  212. /* Allocate memory. */
  213. if ((B = malloc(128 * r * p)) == NULL)
  214. goto err0;
  215. if ((XY = malloc(256 * r)) == NULL)
  216. goto err1;
  217. if ((V = malloc(128 * r * N)) == NULL)
  218. goto err2;
  219. /* 1: (B_0 ... B_{p-1}) <-- PBKDF2(P, S, 1, p * MFLen) */
  220. PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, 1, B, p * 128 * r);
  221. /* 2: for i = 0 to p - 1 do */
  222. for (i = 0; i < p; i++) {
  223. /* 3: B_i <-- MF(B_i, N) */
  224. smix(&B[i * 128 * r], r, N, V, XY);
  225. }
  226. /* 5: DK <-- PBKDF2(P, B, 1, dkLen) */
  227. PBKDF2_SHA256(passwd, passwdlen, B, p * 128 * r, 1, buf, buflen);
  228. /* Free memory. */
  229. free(V);
  230. free(XY);
  231. free(B);
  232. /* Success! */
  233. return (0);
  234. err2:
  235. free(XY);
  236. err1:
  237. free(B);
  238. err0:
  239. /* Failure! */
  240. return (-1);
  241. }