scrypt.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /*
  2. * Copyright 2017-2025 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. return NULL;
  59. ctx->libctx = libctx;
  60. kdf_scrypt_init(ctx);
  61. return ctx;
  62. }
  63. static void *kdf_scrypt_new(void *provctx)
  64. {
  65. return kdf_scrypt_new_inner(PROV_LIBCTX_OF(provctx));
  66. }
  67. static void kdf_scrypt_free(void *vctx)
  68. {
  69. KDF_SCRYPT *ctx = (KDF_SCRYPT *)vctx;
  70. if (ctx != NULL) {
  71. OPENSSL_free(ctx->propq);
  72. EVP_MD_free(ctx->sha256);
  73. kdf_scrypt_reset(ctx);
  74. OPENSSL_free(ctx);
  75. }
  76. }
  77. static void kdf_scrypt_reset(void *vctx)
  78. {
  79. KDF_SCRYPT *ctx = (KDF_SCRYPT *)vctx;
  80. OPENSSL_free(ctx->salt);
  81. ctx->salt = NULL;
  82. OPENSSL_clear_free(ctx->pass, ctx->pass_len);
  83. ctx->pass = NULL;
  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. return 0;
  135. } else if (p->data != NULL) {
  136. if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen))
  137. return 0;
  138. }
  139. return 1;
  140. }
  141. static int set_digest(KDF_SCRYPT *ctx)
  142. {
  143. EVP_MD_free(ctx->sha256);
  144. ctx->sha256 = EVP_MD_fetch(ctx->libctx, "sha256", ctx->propq);
  145. if (ctx->sha256 == NULL) {
  146. ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_LOAD_SHA256);
  147. return 0;
  148. }
  149. return 1;
  150. }
  151. static int set_property_query(KDF_SCRYPT *ctx, const char *propq)
  152. {
  153. OPENSSL_free(ctx->propq);
  154. ctx->propq = NULL;
  155. if (propq != NULL) {
  156. ctx->propq = OPENSSL_strdup(propq);
  157. if (ctx->propq == NULL)
  158. return 0;
  159. }
  160. return 1;
  161. }
  162. static int kdf_scrypt_derive(void *vctx, unsigned char *key, size_t keylen,
  163. const OSSL_PARAM params[])
  164. {
  165. KDF_SCRYPT *ctx = (KDF_SCRYPT *)vctx;
  166. if (!ossl_prov_is_running() || !kdf_scrypt_set_ctx_params(ctx, params))
  167. return 0;
  168. if (ctx->pass == NULL) {
  169. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS);
  170. return 0;
  171. }
  172. if (ctx->salt == NULL) {
  173. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT);
  174. return 0;
  175. }
  176. if (ctx->sha256 == NULL && !set_digest(ctx))
  177. return 0;
  178. return scrypt_alg((char *)ctx->pass, ctx->pass_len, ctx->salt,
  179. ctx->salt_len, ctx->N, ctx->r, ctx->p,
  180. ctx->maxmem_bytes, key, keylen, ctx->sha256,
  181. ctx->libctx, ctx->propq);
  182. }
  183. static int is_power_of_two(uint64_t value)
  184. {
  185. return (value != 0) && ((value & (value - 1)) == 0);
  186. }
  187. static int kdf_scrypt_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  188. {
  189. const OSSL_PARAM *p;
  190. KDF_SCRYPT *ctx = vctx;
  191. uint64_t u64_value;
  192. if (params == NULL)
  193. return 1;
  194. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PASSWORD)) != NULL)
  195. if (!scrypt_set_membuf(&ctx->pass, &ctx->pass_len, p))
  196. return 0;
  197. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT)) != NULL)
  198. if (!scrypt_set_membuf(&ctx->salt, &ctx->salt_len, p))
  199. return 0;
  200. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SCRYPT_N))
  201. != NULL) {
  202. if (!OSSL_PARAM_get_uint64(p, &u64_value)
  203. || u64_value <= 1
  204. || !is_power_of_two(u64_value))
  205. return 0;
  206. ctx->N = u64_value;
  207. }
  208. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SCRYPT_R))
  209. != NULL) {
  210. if (!OSSL_PARAM_get_uint64(p, &u64_value) || u64_value < 1)
  211. return 0;
  212. ctx->r = u64_value;
  213. }
  214. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SCRYPT_P))
  215. != NULL) {
  216. if (!OSSL_PARAM_get_uint64(p, &u64_value) || u64_value < 1)
  217. return 0;
  218. ctx->p = u64_value;
  219. }
  220. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SCRYPT_MAXMEM))
  221. != NULL) {
  222. if (!OSSL_PARAM_get_uint64(p, &u64_value) || u64_value < 1)
  223. return 0;
  224. ctx->maxmem_bytes = u64_value;
  225. }
  226. p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PROPERTIES);
  227. if (p != NULL) {
  228. if (p->data_type != OSSL_PARAM_UTF8_STRING
  229. || !set_property_query(ctx, p->data)
  230. || !set_digest(ctx))
  231. return 0;
  232. }
  233. return 1;
  234. }
  235. static const OSSL_PARAM *kdf_scrypt_settable_ctx_params(ossl_unused void *ctx,
  236. ossl_unused void *p_ctx)
  237. {
  238. static const OSSL_PARAM known_settable_ctx_params[] = {
  239. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PASSWORD, NULL, 0),
  240. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0),
  241. OSSL_PARAM_uint64(OSSL_KDF_PARAM_SCRYPT_N, NULL),
  242. OSSL_PARAM_uint32(OSSL_KDF_PARAM_SCRYPT_R, NULL),
  243. OSSL_PARAM_uint32(OSSL_KDF_PARAM_SCRYPT_P, NULL),
  244. OSSL_PARAM_uint64(OSSL_KDF_PARAM_SCRYPT_MAXMEM, NULL),
  245. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
  246. OSSL_PARAM_END
  247. };
  248. return known_settable_ctx_params;
  249. }
  250. static int kdf_scrypt_get_ctx_params(void *vctx, OSSL_PARAM params[])
  251. {
  252. OSSL_PARAM *p;
  253. if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
  254. return OSSL_PARAM_set_size_t(p, SIZE_MAX);
  255. return -2;
  256. }
  257. static const OSSL_PARAM *kdf_scrypt_gettable_ctx_params(ossl_unused void *ctx,
  258. ossl_unused void *p_ctx)
  259. {
  260. static const OSSL_PARAM known_gettable_ctx_params[] = {
  261. OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
  262. OSSL_PARAM_END
  263. };
  264. return known_gettable_ctx_params;
  265. }
  266. const OSSL_DISPATCH ossl_kdf_scrypt_functions[] = {
  267. { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_scrypt_new },
  268. { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_scrypt_dup },
  269. { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_scrypt_free },
  270. { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_scrypt_reset },
  271. { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_scrypt_derive },
  272. { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
  273. (void(*)(void))kdf_scrypt_settable_ctx_params },
  274. { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_scrypt_set_ctx_params },
  275. { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
  276. (void(*)(void))kdf_scrypt_gettable_ctx_params },
  277. { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_scrypt_get_ctx_params },
  278. OSSL_DISPATCH_END
  279. };
  280. #define R(a,b) (((a) << (b)) | ((a) >> (32 - (b))))
  281. static void salsa208_word_specification(uint32_t inout[16])
  282. {
  283. int i;
  284. uint32_t x[16];
  285. memcpy(x, inout, sizeof(x));
  286. for (i = 8; i > 0; i -= 2) {
  287. x[4] ^= R(x[0] + x[12], 7);
  288. x[8] ^= R(x[4] + x[0], 9);
  289. x[12] ^= R(x[8] + x[4], 13);
  290. x[0] ^= R(x[12] + x[8], 18);
  291. x[9] ^= R(x[5] + x[1], 7);
  292. x[13] ^= R(x[9] + x[5], 9);
  293. x[1] ^= R(x[13] + x[9], 13);
  294. x[5] ^= R(x[1] + x[13], 18);
  295. x[14] ^= R(x[10] + x[6], 7);
  296. x[2] ^= R(x[14] + x[10], 9);
  297. x[6] ^= R(x[2] + x[14], 13);
  298. x[10] ^= R(x[6] + x[2], 18);
  299. x[3] ^= R(x[15] + x[11], 7);
  300. x[7] ^= R(x[3] + x[15], 9);
  301. x[11] ^= R(x[7] + x[3], 13);
  302. x[15] ^= R(x[11] + x[7], 18);
  303. x[1] ^= R(x[0] + x[3], 7);
  304. x[2] ^= R(x[1] + x[0], 9);
  305. x[3] ^= R(x[2] + x[1], 13);
  306. x[0] ^= R(x[3] + x[2], 18);
  307. x[6] ^= R(x[5] + x[4], 7);
  308. x[7] ^= R(x[6] + x[5], 9);
  309. x[4] ^= R(x[7] + x[6], 13);
  310. x[5] ^= R(x[4] + x[7], 18);
  311. x[11] ^= R(x[10] + x[9], 7);
  312. x[8] ^= R(x[11] + x[10], 9);
  313. x[9] ^= R(x[8] + x[11], 13);
  314. x[10] ^= R(x[9] + x[8], 18);
  315. x[12] ^= R(x[15] + x[14], 7);
  316. x[13] ^= R(x[12] + x[15], 9);
  317. x[14] ^= R(x[13] + x[12], 13);
  318. x[15] ^= R(x[14] + x[13], 18);
  319. }
  320. for (i = 0; i < 16; ++i)
  321. inout[i] += x[i];
  322. OPENSSL_cleanse(x, sizeof(x));
  323. }
  324. static void scryptBlockMix(uint32_t *B_, uint32_t *B, uint64_t r)
  325. {
  326. uint64_t i, j;
  327. uint32_t X[16], *pB;
  328. memcpy(X, B + (r * 2 - 1) * 16, sizeof(X));
  329. pB = B;
  330. for (i = 0; i < r * 2; i++) {
  331. for (j = 0; j < 16; j++)
  332. X[j] ^= *pB++;
  333. salsa208_word_specification(X);
  334. memcpy(B_ + (i / 2 + (i & 1) * r) * 16, X, sizeof(X));
  335. }
  336. OPENSSL_cleanse(X, sizeof(X));
  337. }
  338. static void scryptROMix(unsigned char *B, uint64_t r, uint64_t N,
  339. uint32_t *X, uint32_t *T, uint32_t *V)
  340. {
  341. unsigned char *pB;
  342. uint32_t *pV;
  343. uint64_t i, k;
  344. /* Convert from little endian input */
  345. for (pV = V, i = 0, pB = B; i < 32 * r; i++, pV++) {
  346. *pV = *pB++;
  347. *pV |= *pB++ << 8;
  348. *pV |= *pB++ << 16;
  349. *pV |= (uint32_t)*pB++ << 24;
  350. }
  351. for (i = 1; i < N; i++, pV += 32 * r)
  352. scryptBlockMix(pV, pV - 32 * r, r);
  353. scryptBlockMix(X, V + (N - 1) * 32 * r, r);
  354. for (i = 0; i < N; i++) {
  355. uint32_t j;
  356. j = X[16 * (2 * r - 1)] % N;
  357. pV = V + 32 * r * j;
  358. for (k = 0; k < 32 * r; k++)
  359. T[k] = X[k] ^ *pV++;
  360. scryptBlockMix(X, T, r);
  361. }
  362. /* Convert output to little endian */
  363. for (i = 0, pB = B; i < 32 * r; i++) {
  364. uint32_t xtmp = X[i];
  365. *pB++ = xtmp & 0xff;
  366. *pB++ = (xtmp >> 8) & 0xff;
  367. *pB++ = (xtmp >> 16) & 0xff;
  368. *pB++ = (xtmp >> 24) & 0xff;
  369. }
  370. }
  371. #ifndef SIZE_MAX
  372. # define SIZE_MAX ((size_t)-1)
  373. #endif
  374. /*
  375. * Maximum power of two that will fit in uint64_t: this should work on
  376. * most (all?) platforms.
  377. */
  378. #define LOG2_UINT64_MAX (sizeof(uint64_t) * 8 - 1)
  379. /*
  380. * Maximum value of p * r:
  381. * p <= ((2^32-1) * hLen) / MFLen =>
  382. * p <= ((2^32-1) * 32) / (128 * r) =>
  383. * p * r <= (2^30-1)
  384. */
  385. #define SCRYPT_PR_MAX ((1 << 30) - 1)
  386. static int scrypt_alg(const char *pass, size_t passlen,
  387. const unsigned char *salt, size_t saltlen,
  388. uint64_t N, uint64_t r, uint64_t p, uint64_t maxmem,
  389. unsigned char *key, size_t keylen, EVP_MD *sha256,
  390. OSSL_LIB_CTX *libctx, const char *propq)
  391. {
  392. int rv = 0;
  393. unsigned char *B;
  394. uint32_t *X, *V, *T;
  395. uint64_t i, Blen, Vlen;
  396. /* Sanity check parameters */
  397. /* initial check, r,p must be non zero, N >= 2 and a power of 2 */
  398. if (r == 0 || p == 0 || N < 2 || (N & (N - 1)))
  399. return 0;
  400. /* Check p * r < SCRYPT_PR_MAX avoiding overflow */
  401. if (p > SCRYPT_PR_MAX / r) {
  402. ERR_raise(ERR_LIB_EVP, EVP_R_MEMORY_LIMIT_EXCEEDED);
  403. return 0;
  404. }
  405. /*
  406. * Need to check N: if 2^(128 * r / 8) overflows limit this is
  407. * automatically satisfied since N <= UINT64_MAX.
  408. */
  409. if (16 * r <= LOG2_UINT64_MAX) {
  410. if (N >= (((uint64_t)1) << (16 * r))) {
  411. ERR_raise(ERR_LIB_EVP, EVP_R_MEMORY_LIMIT_EXCEEDED);
  412. return 0;
  413. }
  414. }
  415. /* Memory checks: check total allocated buffer size fits in uint64_t */
  416. /*
  417. * B size in section 5 step 1.S
  418. * Note: we know p * 128 * r < UINT64_MAX because we already checked
  419. * p * r < SCRYPT_PR_MAX
  420. */
  421. Blen = p * 128 * r;
  422. /*
  423. * Yet we pass it as integer to PKCS5_PBKDF2_HMAC... [This would
  424. * have to be revised when/if PKCS5_PBKDF2_HMAC accepts size_t.]
  425. */
  426. if (Blen > INT_MAX) {
  427. ERR_raise(ERR_LIB_EVP, EVP_R_MEMORY_LIMIT_EXCEEDED);
  428. return 0;
  429. }
  430. /*
  431. * Check 32 * r * (N + 2) * sizeof(uint32_t) fits in uint64_t
  432. * This is combined size V, X and T (section 4)
  433. */
  434. i = UINT64_MAX / (32 * sizeof(uint32_t));
  435. if (N + 2 > i / r) {
  436. ERR_raise(ERR_LIB_EVP, EVP_R_MEMORY_LIMIT_EXCEEDED);
  437. return 0;
  438. }
  439. Vlen = 32 * r * (N + 2) * sizeof(uint32_t);
  440. /* check total allocated size fits in uint64_t */
  441. if (Blen > UINT64_MAX - Vlen) {
  442. ERR_raise(ERR_LIB_EVP, EVP_R_MEMORY_LIMIT_EXCEEDED);
  443. return 0;
  444. }
  445. /* Check that the maximum memory doesn't exceed a size_t limits */
  446. if (maxmem > SIZE_MAX)
  447. maxmem = SIZE_MAX;
  448. if (Blen + Vlen > maxmem) {
  449. ERR_raise(ERR_LIB_EVP, EVP_R_MEMORY_LIMIT_EXCEEDED);
  450. return 0;
  451. }
  452. /* If no key return to indicate parameters are OK */
  453. if (key == NULL)
  454. return 1;
  455. B = OPENSSL_malloc((size_t)(Blen + Vlen));
  456. if (B == NULL)
  457. return 0;
  458. X = (uint32_t *)(B + Blen);
  459. T = X + 32 * r;
  460. V = T + 32 * r;
  461. if (ossl_pkcs5_pbkdf2_hmac_ex(pass, passlen, salt, saltlen, 1, sha256,
  462. (int)Blen, B, libctx, propq) == 0)
  463. goto err;
  464. for (i = 0; i < p; i++)
  465. scryptROMix(B + 128 * r * i, r, N, X, T, V);
  466. if (ossl_pkcs5_pbkdf2_hmac_ex(pass, passlen, B, (int)Blen, 1, sha256,
  467. keylen, key, libctx, propq) == 0)
  468. goto err;
  469. rv = 1;
  470. err:
  471. if (rv == 0)
  472. ERR_raise(ERR_LIB_EVP, EVP_R_PBKDF2_ERROR);
  473. OPENSSL_clear_free(B, (size_t)(Blen + Vlen));
  474. return rv;
  475. }
  476. #endif