scrypt.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stddef.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <openssl/evp.h>
  13. #include <openssl/err.h>
  14. #include <internal/numbers.h>
  15. #ifndef OPENSSL_NO_SCRYPT
  16. #define R(a,b) (((a) << (b)) | ((a) >> (32 - (b))))
  17. static void salsa208_word_specification(uint32_t inout[16])
  18. {
  19. int i;
  20. uint32_t x[16];
  21. memcpy(x, inout, sizeof(x));
  22. for (i = 8; i > 0; i -= 2) {
  23. x[4] ^= R(x[0] + x[12], 7);
  24. x[8] ^= R(x[4] + x[0], 9);
  25. x[12] ^= R(x[8] + x[4], 13);
  26. x[0] ^= R(x[12] + x[8], 18);
  27. x[9] ^= R(x[5] + x[1], 7);
  28. x[13] ^= R(x[9] + x[5], 9);
  29. x[1] ^= R(x[13] + x[9], 13);
  30. x[5] ^= R(x[1] + x[13], 18);
  31. x[14] ^= R(x[10] + x[6], 7);
  32. x[2] ^= R(x[14] + x[10], 9);
  33. x[6] ^= R(x[2] + x[14], 13);
  34. x[10] ^= R(x[6] + x[2], 18);
  35. x[3] ^= R(x[15] + x[11], 7);
  36. x[7] ^= R(x[3] + x[15], 9);
  37. x[11] ^= R(x[7] + x[3], 13);
  38. x[15] ^= R(x[11] + x[7], 18);
  39. x[1] ^= R(x[0] + x[3], 7);
  40. x[2] ^= R(x[1] + x[0], 9);
  41. x[3] ^= R(x[2] + x[1], 13);
  42. x[0] ^= R(x[3] + x[2], 18);
  43. x[6] ^= R(x[5] + x[4], 7);
  44. x[7] ^= R(x[6] + x[5], 9);
  45. x[4] ^= R(x[7] + x[6], 13);
  46. x[5] ^= R(x[4] + x[7], 18);
  47. x[11] ^= R(x[10] + x[9], 7);
  48. x[8] ^= R(x[11] + x[10], 9);
  49. x[9] ^= R(x[8] + x[11], 13);
  50. x[10] ^= R(x[9] + x[8], 18);
  51. x[12] ^= R(x[15] + x[14], 7);
  52. x[13] ^= R(x[12] + x[15], 9);
  53. x[14] ^= R(x[13] + x[12], 13);
  54. x[15] ^= R(x[14] + x[13], 18);
  55. }
  56. for (i = 0; i < 16; ++i)
  57. inout[i] += x[i];
  58. OPENSSL_cleanse(x, sizeof(x));
  59. }
  60. static void scryptBlockMix(uint32_t *B_, uint32_t *B, uint64_t r)
  61. {
  62. uint64_t i, j;
  63. uint32_t X[16], *pB;
  64. memcpy(X, B + (r * 2 - 1) * 16, sizeof(X));
  65. pB = B;
  66. for (i = 0; i < r * 2; i++) {
  67. for (j = 0; j < 16; j++)
  68. X[j] ^= *pB++;
  69. salsa208_word_specification(X);
  70. memcpy(B_ + (i / 2 + (i & 1) * r) * 16, X, sizeof(X));
  71. }
  72. OPENSSL_cleanse(X, sizeof(X));
  73. }
  74. static void scryptROMix(unsigned char *B, uint64_t r, uint64_t N,
  75. uint32_t *X, uint32_t *T, uint32_t *V)
  76. {
  77. unsigned char *pB;
  78. uint32_t *pV;
  79. uint64_t i, k;
  80. /* Convert from little endian input */
  81. for (pV = V, i = 0, pB = B; i < 32 * r; i++, pV++) {
  82. *pV = *pB++;
  83. *pV |= *pB++ << 8;
  84. *pV |= *pB++ << 16;
  85. *pV |= (uint32_t)*pB++ << 24;
  86. }
  87. for (i = 1; i < N; i++, pV += 32 * r)
  88. scryptBlockMix(pV, pV - 32 * r, r);
  89. scryptBlockMix(X, V + (N - 1) * 32 * r, r);
  90. for (i = 0; i < N; i++) {
  91. uint32_t j;
  92. j = X[16 * (2 * r - 1)] % N;
  93. pV = V + 32 * r * j;
  94. for (k = 0; k < 32 * r; k++)
  95. T[k] = X[k] ^ *pV++;
  96. scryptBlockMix(X, T, r);
  97. }
  98. /* Convert output to little endian */
  99. for (i = 0, pB = B; i < 32 * r; i++) {
  100. uint32_t xtmp = X[i];
  101. *pB++ = xtmp & 0xff;
  102. *pB++ = (xtmp >> 8) & 0xff;
  103. *pB++ = (xtmp >> 16) & 0xff;
  104. *pB++ = (xtmp >> 24) & 0xff;
  105. }
  106. }
  107. #ifndef SIZE_MAX
  108. # define SIZE_MAX ((size_t)-1)
  109. #endif
  110. /*
  111. * Maximum power of two that will fit in uint64_t: this should work on
  112. * most (all?) platforms.
  113. */
  114. #define LOG2_UINT64_MAX (sizeof(uint64_t) * 8 - 1)
  115. /*
  116. * Maximum value of p * r:
  117. * p <= ((2^32-1) * hLen) / MFLen =>
  118. * p <= ((2^32-1) * 32) / (128 * r) =>
  119. * p * r <= (2^30-1)
  120. *
  121. */
  122. #define SCRYPT_PR_MAX ((1 << 30) - 1)
  123. /*
  124. * Maximum permitted memory allow this to be overridden with Configuration
  125. * option: e.g. -DSCRYPT_MAX_MEM=0 for maximum possible.
  126. */
  127. #ifdef SCRYPT_MAX_MEM
  128. # if SCRYPT_MAX_MEM == 0
  129. # undef SCRYPT_MAX_MEM
  130. /*
  131. * Although we could theoretically allocate SIZE_MAX memory that would leave
  132. * no memory available for anything else so set limit as half that.
  133. */
  134. # define SCRYPT_MAX_MEM (SIZE_MAX/2)
  135. # endif
  136. #else
  137. /* Default memory limit: 32 MB */
  138. # define SCRYPT_MAX_MEM (1024 * 1024 * 32)
  139. #endif
  140. int EVP_PBE_scrypt(const char *pass, size_t passlen,
  141. const unsigned char *salt, size_t saltlen,
  142. uint64_t N, uint64_t r, uint64_t p, uint64_t maxmem,
  143. unsigned char *key, size_t keylen)
  144. {
  145. int rv = 0;
  146. unsigned char *B;
  147. uint32_t *X, *V, *T;
  148. uint64_t i, Blen, Vlen;
  149. size_t allocsize;
  150. /* Sanity check parameters */
  151. /* initial check, r,p must be non zero, N >= 2 and a power of 2 */
  152. if (r == 0 || p == 0 || N < 2 || (N & (N - 1)))
  153. return 0;
  154. /* Check p * r < SCRYPT_PR_MAX avoiding overflow */
  155. if (p > SCRYPT_PR_MAX / r)
  156. return 0;
  157. /*
  158. * Need to check N: if 2^(128 * r / 8) overflows limit this is
  159. * automatically satisfied since N <= UINT64_MAX.
  160. */
  161. if (16 * r <= LOG2_UINT64_MAX) {
  162. if (N >= (((uint64_t)1) << (16 * r)))
  163. return 0;
  164. }
  165. /* Memory checks: check total allocated buffer size fits in uint64_t */
  166. /*
  167. * B size in section 5 step 1.S
  168. * Note: we know p * 128 * r < UINT64_MAX because we already checked
  169. * p * r < SCRYPT_PR_MAX
  170. */
  171. Blen = p * 128 * r;
  172. /*
  173. * Check 32 * r * (N + 2) * sizeof(uint32_t) fits in
  174. * uint64_t and also size_t (their sizes are unrelated).
  175. * This is combined size V, X and T (section 4)
  176. */
  177. i = UINT64_MAX / (32 * sizeof(uint32_t));
  178. if (N + 2 > i / r)
  179. return 0;
  180. Vlen = 32 * r * (N + 2) * sizeof(uint32_t);
  181. /* check total allocated size fits in uint64_t */
  182. if (Blen > UINT64_MAX - Vlen)
  183. return 0;
  184. /* check total allocated size fits in size_t */
  185. if (Blen > SIZE_MAX - Vlen)
  186. return 0;
  187. allocsize = (size_t)(Blen + Vlen);
  188. if (maxmem == 0)
  189. maxmem = SCRYPT_MAX_MEM;
  190. if (allocsize > maxmem) {
  191. EVPerr(EVP_F_EVP_PBE_SCRYPT, EVP_R_MEMORY_LIMIT_EXCEEDED);
  192. return 0;
  193. }
  194. /* If no key return to indicate parameters are OK */
  195. if (key == NULL)
  196. return 1;
  197. B = OPENSSL_malloc(allocsize);
  198. if (B == NULL)
  199. return 0;
  200. X = (uint32_t *)(B + Blen);
  201. T = X + 32 * r;
  202. V = T + 32 * r;
  203. if (PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, 1, EVP_sha256(),
  204. Blen, B) == 0)
  205. goto err;
  206. for (i = 0; i < p; i++)
  207. scryptROMix(B + 128 * r * i, r, N, X, T, V);
  208. if (PKCS5_PBKDF2_HMAC(pass, passlen, B, Blen, 1, EVP_sha256(),
  209. keylen, key) == 0)
  210. goto err;
  211. rv = 1;
  212. err:
  213. OPENSSL_clear_free(B, allocsize);
  214. return rv;
  215. }
  216. #endif