2
0

pbe_scrypt.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright 2015-2018 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. /* Sanity check parameters */
  150. /* initial check, r,p must be non zero, N >= 2 and a power of 2 */
  151. if (r == 0 || p == 0 || N < 2 || (N & (N - 1)))
  152. return 0;
  153. /* Check p * r < SCRYPT_PR_MAX avoiding overflow */
  154. if (p > SCRYPT_PR_MAX / r) {
  155. EVPerr(EVP_F_EVP_PBE_SCRYPT, EVP_R_MEMORY_LIMIT_EXCEEDED);
  156. return 0;
  157. }
  158. /*
  159. * Need to check N: if 2^(128 * r / 8) overflows limit this is
  160. * automatically satisfied since N <= UINT64_MAX.
  161. */
  162. if (16 * r <= LOG2_UINT64_MAX) {
  163. if (N >= (((uint64_t)1) << (16 * r))) {
  164. EVPerr(EVP_F_EVP_PBE_SCRYPT, EVP_R_MEMORY_LIMIT_EXCEEDED);
  165. return 0;
  166. }
  167. }
  168. /* Memory checks: check total allocated buffer size fits in uint64_t */
  169. /*
  170. * B size in section 5 step 1.S
  171. * Note: we know p * 128 * r < UINT64_MAX because we already checked
  172. * p * r < SCRYPT_PR_MAX
  173. */
  174. Blen = p * 128 * r;
  175. /*
  176. * Yet we pass it as integer to PKCS5_PBKDF2_HMAC... [This would
  177. * have to be revised when/if PKCS5_PBKDF2_HMAC accepts size_t.]
  178. */
  179. if (Blen > INT_MAX) {
  180. EVPerr(EVP_F_EVP_PBE_SCRYPT, EVP_R_MEMORY_LIMIT_EXCEEDED);
  181. return 0;
  182. }
  183. /*
  184. * Check 32 * r * (N + 2) * sizeof(uint32_t) fits in uint64_t
  185. * This is combined size V, X and T (section 4)
  186. */
  187. i = UINT64_MAX / (32 * sizeof(uint32_t));
  188. if (N + 2 > i / r) {
  189. EVPerr(EVP_F_EVP_PBE_SCRYPT, EVP_R_MEMORY_LIMIT_EXCEEDED);
  190. return 0;
  191. }
  192. Vlen = 32 * r * (N + 2) * sizeof(uint32_t);
  193. /* check total allocated size fits in uint64_t */
  194. if (Blen > UINT64_MAX - Vlen) {
  195. EVPerr(EVP_F_EVP_PBE_SCRYPT, EVP_R_MEMORY_LIMIT_EXCEEDED);
  196. return 0;
  197. }
  198. if (maxmem == 0)
  199. maxmem = SCRYPT_MAX_MEM;
  200. /* Check that the maximum memory doesn't exceed a size_t limits */
  201. if (maxmem > SIZE_MAX)
  202. maxmem = SIZE_MAX;
  203. if (Blen + Vlen > maxmem) {
  204. EVPerr(EVP_F_EVP_PBE_SCRYPT, EVP_R_MEMORY_LIMIT_EXCEEDED);
  205. return 0;
  206. }
  207. /* If no key return to indicate parameters are OK */
  208. if (key == NULL)
  209. return 1;
  210. B = OPENSSL_malloc((size_t)(Blen + Vlen));
  211. if (B == NULL) {
  212. EVPerr(EVP_F_EVP_PBE_SCRYPT, ERR_R_MALLOC_FAILURE);
  213. return 0;
  214. }
  215. X = (uint32_t *)(B + Blen);
  216. T = X + 32 * r;
  217. V = T + 32 * r;
  218. if (PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, 1, EVP_sha256(),
  219. (int)Blen, B) == 0)
  220. goto err;
  221. for (i = 0; i < p; i++)
  222. scryptROMix(B + 128 * r * i, r, N, X, T, V);
  223. if (PKCS5_PBKDF2_HMAC(pass, passlen, B, (int)Blen, 1, EVP_sha256(),
  224. keylen, key) == 0)
  225. goto err;
  226. rv = 1;
  227. err:
  228. if (rv == 0)
  229. EVPerr(EVP_F_EVP_PBE_SCRYPT, EVP_R_PBKDF2_ERROR);
  230. OPENSSL_clear_free(B, (size_t)(Blen + Vlen));
  231. return rv;
  232. }
  233. #endif