scrypt.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /*
  2. * Copyright 2017-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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 <stdlib.h>
  10. #include <stdarg.h>
  11. #include <string.h>
  12. #include <openssl/evp.h>
  13. #include <openssl/kdf.h>
  14. #include <openssl/err.h>
  15. #include <openssl/core_names.h>
  16. #include <openssl/proverr.h>
  17. #include "crypto/evp.h"
  18. #include "internal/numbers.h"
  19. #include "prov/implementations.h"
  20. #include "prov/provider_ctx.h"
  21. #include "prov/providercommon.h"
  22. #include "prov/provider_util.h"
  23. #ifndef OPENSSL_NO_SCRYPT
  24. static OSSL_FUNC_kdf_newctx_fn kdf_scrypt_new;
  25. static OSSL_FUNC_kdf_dupctx_fn kdf_scrypt_dup;
  26. static OSSL_FUNC_kdf_freectx_fn kdf_scrypt_free;
  27. static OSSL_FUNC_kdf_reset_fn kdf_scrypt_reset;
  28. static OSSL_FUNC_kdf_derive_fn kdf_scrypt_derive;
  29. static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_scrypt_settable_ctx_params;
  30. static OSSL_FUNC_kdf_set_ctx_params_fn kdf_scrypt_set_ctx_params;
  31. static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_scrypt_gettable_ctx_params;
  32. static OSSL_FUNC_kdf_get_ctx_params_fn kdf_scrypt_get_ctx_params;
  33. static int scrypt_alg(const char *pass, size_t passlen,
  34. const unsigned char *salt, size_t saltlen,
  35. uint64_t N, uint64_t r, uint64_t p, uint64_t maxmem,
  36. unsigned char *key, size_t keylen, EVP_MD *sha256,
  37. OSSL_LIB_CTX *libctx, const char *propq);
  38. typedef struct {
  39. OSSL_LIB_CTX *libctx;
  40. char *propq;
  41. unsigned char *pass;
  42. size_t pass_len;
  43. unsigned char *salt;
  44. size_t salt_len;
  45. uint64_t N;
  46. uint64_t r, p;
  47. uint64_t maxmem_bytes;
  48. EVP_MD *sha256;
  49. } KDF_SCRYPT;
  50. static void kdf_scrypt_init(KDF_SCRYPT *ctx);
  51. static void *kdf_scrypt_new_inner(OSSL_LIB_CTX *libctx)
  52. {
  53. KDF_SCRYPT *ctx;
  54. if (!ossl_prov_is_running())
  55. return NULL;
  56. ctx = OPENSSL_zalloc(sizeof(*ctx));
  57. if (ctx == NULL) {
  58. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  59. return NULL;
  60. }
  61. ctx->libctx = libctx;
  62. kdf_scrypt_init(ctx);
  63. return ctx;
  64. }
  65. static void *kdf_scrypt_new(void *provctx)
  66. {
  67. return kdf_scrypt_new_inner(PROV_LIBCTX_OF(provctx));
  68. }
  69. static void kdf_scrypt_free(void *vctx)
  70. {
  71. KDF_SCRYPT *ctx = (KDF_SCRYPT *)vctx;
  72. if (ctx != NULL) {
  73. OPENSSL_free(ctx->propq);
  74. EVP_MD_free(ctx->sha256);
  75. kdf_scrypt_reset(ctx);
  76. OPENSSL_free(ctx);
  77. }
  78. }
  79. static void kdf_scrypt_reset(void *vctx)
  80. {
  81. KDF_SCRYPT *ctx = (KDF_SCRYPT *)vctx;
  82. OPENSSL_free(ctx->salt);
  83. OPENSSL_clear_free(ctx->pass, ctx->pass_len);
  84. kdf_scrypt_init(ctx);
  85. }
  86. static void *kdf_scrypt_dup(void *vctx)
  87. {
  88. const KDF_SCRYPT *src = (const KDF_SCRYPT *)vctx;
  89. KDF_SCRYPT *dest;
  90. dest = kdf_scrypt_new_inner(src->libctx);
  91. if (dest != NULL) {
  92. if (src->sha256 != NULL && !EVP_MD_up_ref(src->sha256))
  93. goto err;
  94. if (src->propq != NULL) {
  95. dest->propq = OPENSSL_strdup(src->propq);
  96. if (dest->propq == NULL)
  97. goto err;
  98. }
  99. if (!ossl_prov_memdup(src->salt, src->salt_len,
  100. &dest->salt, &dest->salt_len)
  101. || !ossl_prov_memdup(src->pass, src->pass_len,
  102. &dest->pass , &dest->pass_len))
  103. goto err;
  104. dest->N = src->N;
  105. dest->r = src->r;
  106. dest->p = src->p;
  107. dest->maxmem_bytes = src->maxmem_bytes;
  108. dest->sha256 = src->sha256;
  109. }
  110. return dest;
  111. err:
  112. kdf_scrypt_free(dest);
  113. return NULL;
  114. }
  115. static void kdf_scrypt_init(KDF_SCRYPT *ctx)
  116. {
  117. /* Default values are the most conservative recommendation given in the
  118. * original paper of C. Percival. Derivation uses roughly 1 GiB of memory
  119. * for this parameter choice (approx. 128 * r * N * p bytes).
  120. */
  121. ctx->N = 1 << 20;
  122. ctx->r = 8;
  123. ctx->p = 1;
  124. ctx->maxmem_bytes = 1025 * 1024 * 1024;
  125. }
  126. static int scrypt_set_membuf(unsigned char **buffer, size_t *buflen,
  127. const OSSL_PARAM *p)
  128. {
  129. OPENSSL_clear_free(*buffer, *buflen);
  130. *buffer = NULL;
  131. *buflen = 0;
  132. if (p->data_size == 0) {
  133. if ((*buffer = OPENSSL_malloc(1)) == NULL) {
  134. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  135. return 0;
  136. }
  137. } else if (p->data != NULL) {
  138. if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen))
  139. return 0;
  140. }
  141. return 1;
  142. }
  143. static int set_digest(KDF_SCRYPT *ctx)
  144. {
  145. EVP_MD_free(ctx->sha256);
  146. ctx->sha256 = EVP_MD_fetch(ctx->libctx, "sha256", ctx->propq);
  147. if (ctx->sha256 == NULL) {
  148. OPENSSL_free(ctx);
  149. ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_LOAD_SHA256);
  150. return 0;
  151. }
  152. return 1;
  153. }
  154. static int set_property_query(KDF_SCRYPT *ctx, const char *propq)
  155. {
  156. OPENSSL_free(ctx->propq);
  157. ctx->propq = NULL;
  158. if (propq != NULL) {
  159. ctx->propq = OPENSSL_strdup(propq);
  160. if (ctx->propq == NULL) {
  161. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  162. return 0;
  163. }
  164. }
  165. return 1;
  166. }
  167. static int kdf_scrypt_derive(void *vctx, unsigned char *key, size_t keylen,
  168. const OSSL_PARAM params[])
  169. {
  170. KDF_SCRYPT *ctx = (KDF_SCRYPT *)vctx;
  171. if (!ossl_prov_is_running() || !kdf_scrypt_set_ctx_params(ctx, params))
  172. return 0;
  173. if (ctx->pass == NULL) {
  174. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS);
  175. return 0;
  176. }
  177. if (ctx->salt == NULL) {
  178. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT);
  179. return 0;
  180. }
  181. if (ctx->sha256 == NULL && !set_digest(ctx))
  182. return 0;
  183. return scrypt_alg((char *)ctx->pass, ctx->pass_len, ctx->salt,
  184. ctx->salt_len, ctx->N, ctx->r, ctx->p,
  185. ctx->maxmem_bytes, key, keylen, ctx->sha256,
  186. ctx->libctx, ctx->propq);
  187. }
  188. static int is_power_of_two(uint64_t value)
  189. {
  190. return (value != 0) && ((value & (value - 1)) == 0);
  191. }
  192. static int kdf_scrypt_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  193. {
  194. const OSSL_PARAM *p;
  195. KDF_SCRYPT *ctx = vctx;
  196. uint64_t u64_value;
  197. if (params == NULL)
  198. return 1;
  199. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PASSWORD)) != NULL)
  200. if (!scrypt_set_membuf(&ctx->pass, &ctx->pass_len, p))
  201. return 0;
  202. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT)) != NULL)
  203. if (!scrypt_set_membuf(&ctx->salt, &ctx->salt_len, p))
  204. return 0;
  205. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SCRYPT_N))
  206. != NULL) {
  207. if (!OSSL_PARAM_get_uint64(p, &u64_value)
  208. || u64_value <= 1
  209. || !is_power_of_two(u64_value))
  210. return 0;
  211. ctx->N = u64_value;
  212. }
  213. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SCRYPT_R))
  214. != NULL) {
  215. if (!OSSL_PARAM_get_uint64(p, &u64_value) || u64_value < 1)
  216. return 0;
  217. ctx->r = u64_value;
  218. }
  219. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SCRYPT_P))
  220. != NULL) {
  221. if (!OSSL_PARAM_get_uint64(p, &u64_value) || u64_value < 1)
  222. return 0;
  223. ctx->p = u64_value;
  224. }
  225. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SCRYPT_MAXMEM))
  226. != NULL) {
  227. if (!OSSL_PARAM_get_uint64(p, &u64_value) || u64_value < 1)
  228. return 0;
  229. ctx->maxmem_bytes = u64_value;
  230. }
  231. p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PROPERTIES);
  232. if (p != NULL) {
  233. if (p->data_type != OSSL_PARAM_UTF8_STRING
  234. || !set_property_query(ctx, p->data)
  235. || !set_digest(ctx))
  236. return 0;
  237. }
  238. return 1;
  239. }
  240. static const OSSL_PARAM *kdf_scrypt_settable_ctx_params(ossl_unused void *ctx,
  241. ossl_unused void *p_ctx)
  242. {
  243. static const OSSL_PARAM known_settable_ctx_params[] = {
  244. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PASSWORD, NULL, 0),
  245. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0),
  246. OSSL_PARAM_uint64(OSSL_KDF_PARAM_SCRYPT_N, NULL),
  247. OSSL_PARAM_uint32(OSSL_KDF_PARAM_SCRYPT_R, NULL),
  248. OSSL_PARAM_uint32(OSSL_KDF_PARAM_SCRYPT_P, NULL),
  249. OSSL_PARAM_uint64(OSSL_KDF_PARAM_SCRYPT_MAXMEM, NULL),
  250. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
  251. OSSL_PARAM_END
  252. };
  253. return known_settable_ctx_params;
  254. }
  255. static int kdf_scrypt_get_ctx_params(void *vctx, OSSL_PARAM params[])
  256. {
  257. OSSL_PARAM *p;
  258. if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
  259. return OSSL_PARAM_set_size_t(p, SIZE_MAX);
  260. return -2;
  261. }
  262. static const OSSL_PARAM *kdf_scrypt_gettable_ctx_params(ossl_unused void *ctx,
  263. ossl_unused void *p_ctx)
  264. {
  265. static const OSSL_PARAM known_gettable_ctx_params[] = {
  266. OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
  267. OSSL_PARAM_END
  268. };
  269. return known_gettable_ctx_params;
  270. }
  271. const OSSL_DISPATCH ossl_kdf_scrypt_functions[] = {
  272. { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_scrypt_new },
  273. { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_scrypt_dup },
  274. { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_scrypt_free },
  275. { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_scrypt_reset },
  276. { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_scrypt_derive },
  277. { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
  278. (void(*)(void))kdf_scrypt_settable_ctx_params },
  279. { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_scrypt_set_ctx_params },
  280. { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
  281. (void(*)(void))kdf_scrypt_gettable_ctx_params },
  282. { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_scrypt_get_ctx_params },
  283. { 0, NULL }
  284. };
  285. #define R(a,b) (((a) << (b)) | ((a) >> (32 - (b))))
  286. static void salsa208_word_specification(uint32_t inout[16])
  287. {
  288. int i;
  289. uint32_t x[16];
  290. memcpy(x, inout, sizeof(x));
  291. for (i = 8; i > 0; i -= 2) {
  292. x[4] ^= R(x[0] + x[12], 7);
  293. x[8] ^= R(x[4] + x[0], 9);
  294. x[12] ^= R(x[8] + x[4], 13);
  295. x[0] ^= R(x[12] + x[8], 18);
  296. x[9] ^= R(x[5] + x[1], 7);
  297. x[13] ^= R(x[9] + x[5], 9);
  298. x[1] ^= R(x[13] + x[9], 13);
  299. x[5] ^= R(x[1] + x[13], 18);
  300. x[14] ^= R(x[10] + x[6], 7);
  301. x[2] ^= R(x[14] + x[10], 9);
  302. x[6] ^= R(x[2] + x[14], 13);
  303. x[10] ^= R(x[6] + x[2], 18);
  304. x[3] ^= R(x[15] + x[11], 7);
  305. x[7] ^= R(x[3] + x[15], 9);
  306. x[11] ^= R(x[7] + x[3], 13);
  307. x[15] ^= R(x[11] + x[7], 18);
  308. x[1] ^= R(x[0] + x[3], 7);
  309. x[2] ^= R(x[1] + x[0], 9);
  310. x[3] ^= R(x[2] + x[1], 13);
  311. x[0] ^= R(x[3] + x[2], 18);
  312. x[6] ^= R(x[5] + x[4], 7);
  313. x[7] ^= R(x[6] + x[5], 9);
  314. x[4] ^= R(x[7] + x[6], 13);
  315. x[5] ^= R(x[4] + x[7], 18);
  316. x[11] ^= R(x[10] + x[9], 7);
  317. x[8] ^= R(x[11] + x[10], 9);
  318. x[9] ^= R(x[8] + x[11], 13);
  319. x[10] ^= R(x[9] + x[8], 18);
  320. x[12] ^= R(x[15] + x[14], 7);
  321. x[13] ^= R(x[12] + x[15], 9);
  322. x[14] ^= R(x[13] + x[12], 13);
  323. x[15] ^= R(x[14] + x[13], 18);
  324. }
  325. for (i = 0; i < 16; ++i)
  326. inout[i] += x[i];
  327. OPENSSL_cleanse(x, sizeof(x));
  328. }
  329. static void scryptBlockMix(uint32_t *B_, uint32_t *B, uint64_t r)
  330. {
  331. uint64_t i, j;
  332. uint32_t X[16], *pB;
  333. memcpy(X, B + (r * 2 - 1) * 16, sizeof(X));
  334. pB = B;
  335. for (i = 0; i < r * 2; i++) {
  336. for (j = 0; j < 16; j++)
  337. X[j] ^= *pB++;
  338. salsa208_word_specification(X);
  339. memcpy(B_ + (i / 2 + (i & 1) * r) * 16, X, sizeof(X));
  340. }
  341. OPENSSL_cleanse(X, sizeof(X));
  342. }
  343. static void scryptROMix(unsigned char *B, uint64_t r, uint64_t N,
  344. uint32_t *X, uint32_t *T, uint32_t *V)
  345. {
  346. unsigned char *pB;
  347. uint32_t *pV;
  348. uint64_t i, k;
  349. /* Convert from little endian input */
  350. for (pV = V, i = 0, pB = B; i < 32 * r; i++, pV++) {
  351. *pV = *pB++;
  352. *pV |= *pB++ << 8;
  353. *pV |= *pB++ << 16;
  354. *pV |= (uint32_t)*pB++ << 24;
  355. }
  356. for (i = 1; i < N; i++, pV += 32 * r)
  357. scryptBlockMix(pV, pV - 32 * r, r);
  358. scryptBlockMix(X, V + (N - 1) * 32 * r, r);
  359. for (i = 0; i < N; i++) {
  360. uint32_t j;
  361. j = X[16 * (2 * r - 1)] % N;
  362. pV = V + 32 * r * j;
  363. for (k = 0; k < 32 * r; k++)
  364. T[k] = X[k] ^ *pV++;
  365. scryptBlockMix(X, T, r);
  366. }
  367. /* Convert output to little endian */
  368. for (i = 0, pB = B; i < 32 * r; i++) {
  369. uint32_t xtmp = X[i];
  370. *pB++ = xtmp & 0xff;
  371. *pB++ = (xtmp >> 8) & 0xff;
  372. *pB++ = (xtmp >> 16) & 0xff;
  373. *pB++ = (xtmp >> 24) & 0xff;
  374. }
  375. }
  376. #ifndef SIZE_MAX
  377. # define SIZE_MAX ((size_t)-1)
  378. #endif
  379. /*
  380. * Maximum power of two that will fit in uint64_t: this should work on
  381. * most (all?) platforms.
  382. */
  383. #define LOG2_UINT64_MAX (sizeof(uint64_t) * 8 - 1)
  384. /*
  385. * Maximum value of p * r:
  386. * p <= ((2^32-1) * hLen) / MFLen =>
  387. * p <= ((2^32-1) * 32) / (128 * r) =>
  388. * p * r <= (2^30-1)
  389. */
  390. #define SCRYPT_PR_MAX ((1 << 30) - 1)
  391. static int scrypt_alg(const char *pass, size_t passlen,
  392. const unsigned char *salt, size_t saltlen,
  393. uint64_t N, uint64_t r, uint64_t p, uint64_t maxmem,
  394. unsigned char *key, size_t keylen, EVP_MD *sha256,
  395. OSSL_LIB_CTX *libctx, const char *propq)
  396. {
  397. int rv = 0;
  398. unsigned char *B;
  399. uint32_t *X, *V, *T;
  400. uint64_t i, Blen, Vlen;
  401. /* Sanity check parameters */
  402. /* initial check, r,p must be non zero, N >= 2 and a power of 2 */
  403. if (r == 0 || p == 0 || N < 2 || (N & (N - 1)))
  404. return 0;
  405. /* Check p * r < SCRYPT_PR_MAX avoiding overflow */
  406. if (p > SCRYPT_PR_MAX / r) {
  407. ERR_raise(ERR_LIB_EVP, EVP_R_MEMORY_LIMIT_EXCEEDED);
  408. return 0;
  409. }
  410. /*
  411. * Need to check N: if 2^(128 * r / 8) overflows limit this is
  412. * automatically satisfied since N <= UINT64_MAX.
  413. */
  414. if (16 * r <= LOG2_UINT64_MAX) {
  415. if (N >= (((uint64_t)1) << (16 * r))) {
  416. ERR_raise(ERR_LIB_EVP, EVP_R_MEMORY_LIMIT_EXCEEDED);
  417. return 0;
  418. }
  419. }
  420. /* Memory checks: check total allocated buffer size fits in uint64_t */
  421. /*
  422. * B size in section 5 step 1.S
  423. * Note: we know p * 128 * r < UINT64_MAX because we already checked
  424. * p * r < SCRYPT_PR_MAX
  425. */
  426. Blen = p * 128 * r;
  427. /*
  428. * Yet we pass it as integer to PKCS5_PBKDF2_HMAC... [This would
  429. * have to be revised when/if PKCS5_PBKDF2_HMAC accepts size_t.]
  430. */
  431. if (Blen > INT_MAX) {
  432. ERR_raise(ERR_LIB_EVP, EVP_R_MEMORY_LIMIT_EXCEEDED);
  433. return 0;
  434. }
  435. /*
  436. * Check 32 * r * (N + 2) * sizeof(uint32_t) fits in uint64_t
  437. * This is combined size V, X and T (section 4)
  438. */
  439. i = UINT64_MAX / (32 * sizeof(uint32_t));
  440. if (N + 2 > i / r) {
  441. ERR_raise(ERR_LIB_EVP, EVP_R_MEMORY_LIMIT_EXCEEDED);
  442. return 0;
  443. }
  444. Vlen = 32 * r * (N + 2) * sizeof(uint32_t);
  445. /* check total allocated size fits in uint64_t */
  446. if (Blen > UINT64_MAX - Vlen) {
  447. ERR_raise(ERR_LIB_EVP, EVP_R_MEMORY_LIMIT_EXCEEDED);
  448. return 0;
  449. }
  450. /* Check that the maximum memory doesn't exceed a size_t limits */
  451. if (maxmem > SIZE_MAX)
  452. maxmem = SIZE_MAX;
  453. if (Blen + Vlen > maxmem) {
  454. ERR_raise(ERR_LIB_EVP, EVP_R_MEMORY_LIMIT_EXCEEDED);
  455. return 0;
  456. }
  457. /* If no key return to indicate parameters are OK */
  458. if (key == NULL)
  459. return 1;
  460. B = OPENSSL_malloc((size_t)(Blen + Vlen));
  461. if (B == NULL) {
  462. ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
  463. return 0;
  464. }
  465. X = (uint32_t *)(B + Blen);
  466. T = X + 32 * r;
  467. V = T + 32 * r;
  468. if (ossl_pkcs5_pbkdf2_hmac_ex(pass, passlen, salt, saltlen, 1, sha256,
  469. (int)Blen, B, libctx, propq) == 0)
  470. goto err;
  471. for (i = 0; i < p; i++)
  472. scryptROMix(B + 128 * r * i, r, N, X, T, V);
  473. if (ossl_pkcs5_pbkdf2_hmac_ex(pass, passlen, B, (int)Blen, 1, sha256,
  474. keylen, key, libctx, propq) == 0)
  475. goto err;
  476. rv = 1;
  477. err:
  478. if (rv == 0)
  479. ERR_raise(ERR_LIB_EVP, EVP_R_PBKDF2_ERROR);
  480. OPENSSL_clear_free(B, (size_t)(Blen + Vlen));
  481. return rv;
  482. }
  483. #endif