Coverage Report

Created: 2025-06-13 06:57

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