Coverage Report

Created: 2026-02-22 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/providers/implementations/kdfs/pbkdf2.c
Line
Count
Source
1
/*
2
 * Copyright 2018-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
/*
11
 * HMAC low level APIs are deprecated for public use, but still ok for internal
12
 * use.
13
 */
14
#include "internal/deprecated.h"
15
16
#include <stdlib.h>
17
#include <stdarg.h>
18
#include <string.h>
19
#include <openssl/hmac.h>
20
#include <openssl/evp.h>
21
#include <openssl/kdf.h>
22
#include <openssl/core_names.h>
23
#include <openssl/proverr.h>
24
#include "internal/cryptlib.h"
25
#include "internal/fips.h"
26
#include "internal/numbers.h"
27
#include "crypto/evp.h"
28
#include "prov/provider_ctx.h"
29
#include "prov/providercommon.h"
30
#include "prov/implementations.h"
31
#include "prov/provider_util.h"
32
#include "prov/securitycheck.h"
33
#include "providers/implementations/kdfs/pbkdf2.inc"
34
35
/* Constants specified in SP800-132 */
36
0
#define KDF_PBKDF2_MIN_KEY_LEN_BITS 112
37
0
#define KDF_PBKDF2_MAX_KEY_LEN_DIGEST_RATIO 0xFFFFFFFF
38
0
#define KDF_PBKDF2_MIN_ITERATIONS 1000
39
0
#define KDF_PBKDF2_MIN_SALT_LEN (128 / 8)
40
/*
41
 * The Implementation Guidance for FIPS 140-3 says in section D.N
42
 * "Password-Based Key Derivation for Storage Applications" that "the vendor
43
 * shall document in the module's Security Policy the length of
44
 * a password/passphrase used in key derivation and establish an upper bound
45
 * for the probability of having this parameter guessed at random. This
46
 * probability shall take into account not only the length of the
47
 * password/passphrase, but also the difficulty of guessing it. The decision on
48
 * the minimum length of a password used for key derivation is the vendor's,
49
 * but the vendor shall at a minimum informally justify the decision."
50
 *
51
 * ACVP may assume 8, most FIPS modules choose 8, BC-FJA chose 14.
52
 *
53
 * Allow setting this for default provider too, in case consistency is
54
 * desired for FIPS and Default providers. Because password being
55
 * accepted on one system, but not the other, is very confusing.
56
 */
57
#ifndef KDF_PBKDF2_MIN_PASSWORD_LEN
58
#ifdef FIPS_MODULE
59
#define KDF_PBKDF2_MIN_PASSWORD_LEN (8)
60
#define KDF_PBKDF2_FIPS_SELF_TEST_ITERATIONS 2
61
#else
62
0
#define KDF_PBKDF2_MIN_PASSWORD_LEN (1)
63
#endif
64
#endif
65
66
static OSSL_FUNC_kdf_newctx_fn kdf_pbkdf2_new;
67
static OSSL_FUNC_kdf_dupctx_fn kdf_pbkdf2_dup;
68
static OSSL_FUNC_kdf_freectx_fn kdf_pbkdf2_free;
69
static OSSL_FUNC_kdf_reset_fn kdf_pbkdf2_reset;
70
static OSSL_FUNC_kdf_derive_fn kdf_pbkdf2_derive;
71
static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_pbkdf2_settable_ctx_params;
72
static OSSL_FUNC_kdf_set_ctx_params_fn kdf_pbkdf2_set_ctx_params;
73
static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_pbkdf2_gettable_ctx_params;
74
static OSSL_FUNC_kdf_get_ctx_params_fn kdf_pbkdf2_get_ctx_params;
75
76
typedef struct {
77
    void *provctx;
78
    unsigned char *pass;
79
    size_t pass_len;
80
    unsigned char *salt;
81
    size_t salt_len;
82
    uint64_t iter;
83
    PROV_DIGEST digest;
84
    int lower_bound_checks;
85
    OSSL_FIPS_IND_DECLARE
86
} KDF_PBKDF2;
87
88
static int pbkdf2_derive(KDF_PBKDF2 *ctx, const char *pass, size_t passlen,
89
    const unsigned char *salt, int saltlen, uint64_t iter,
90
    const EVP_MD *digest, unsigned char *key,
91
    size_t keylen, int lower_bound_checks);
92
93
static void kdf_pbkdf2_init(KDF_PBKDF2 *ctx);
94
95
static void *kdf_pbkdf2_new_no_init(void *provctx)
96
0
{
97
0
    KDF_PBKDF2 *ctx;
98
99
0
    if (!ossl_prov_is_running())
100
0
        return NULL;
101
102
#ifdef FIPS_MODULE
103
    if (!ossl_deferred_self_test(PROV_LIBCTX_OF(provctx),
104
            ST_ID_KDF_PBKDF2))
105
        return NULL;
106
#endif
107
108
0
    ctx = OPENSSL_zalloc(sizeof(*ctx));
109
0
    if (ctx == NULL)
110
0
        return NULL;
111
0
    ctx->provctx = provctx;
112
0
    OSSL_FIPS_IND_INIT(ctx);
113
0
    return ctx;
114
0
}
115
116
static void *kdf_pbkdf2_new(void *provctx)
117
0
{
118
0
    KDF_PBKDF2 *ctx = kdf_pbkdf2_new_no_init(provctx);
119
120
0
    if (ctx != NULL)
121
0
        kdf_pbkdf2_init(ctx);
122
0
    return ctx;
123
0
}
124
125
static void kdf_pbkdf2_cleanup(KDF_PBKDF2 *ctx)
126
0
{
127
0
    ossl_prov_digest_reset(&ctx->digest);
128
#ifdef OPENSSL_PEDANTIC_ZEROIZATION
129
    OPENSSL_clear_free(ctx->salt, ctx->salt_len);
130
#else
131
0
    OPENSSL_free(ctx->salt);
132
0
#endif
133
0
    OPENSSL_clear_free(ctx->pass, ctx->pass_len);
134
0
    memset(ctx, 0, sizeof(*ctx));
135
0
}
136
137
static void kdf_pbkdf2_free(void *vctx)
138
0
{
139
0
    KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx;
140
141
0
    if (ctx != NULL) {
142
0
        kdf_pbkdf2_cleanup(ctx);
143
0
        OPENSSL_free(ctx);
144
0
    }
145
0
}
146
147
static void kdf_pbkdf2_reset(void *vctx)
148
0
{
149
0
    KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx;
150
0
    void *provctx = ctx->provctx;
151
152
0
    kdf_pbkdf2_cleanup(ctx);
153
0
    ctx->provctx = provctx;
154
0
    kdf_pbkdf2_init(ctx);
155
0
}
156
157
static void *kdf_pbkdf2_dup(void *vctx)
158
0
{
159
0
    const KDF_PBKDF2 *src = (const KDF_PBKDF2 *)vctx;
160
0
    KDF_PBKDF2 *dest;
161
162
    /* We need a new PBKDF2 object but uninitialised since we're filling it */
163
0
    dest = kdf_pbkdf2_new_no_init(src->provctx);
164
0
    if (dest != NULL) {
165
0
        if (!ossl_prov_memdup(src->salt, src->salt_len,
166
0
                &dest->salt, &dest->salt_len)
167
0
            || !ossl_prov_memdup(src->pass, src->pass_len,
168
0
                &dest->pass, &dest->pass_len)
169
0
            || !ossl_prov_digest_copy(&dest->digest, &src->digest))
170
0
            goto err;
171
0
        dest->iter = src->iter;
172
0
        dest->lower_bound_checks = src->lower_bound_checks;
173
0
        OSSL_FIPS_IND_COPY(dest, src)
174
0
    }
175
0
    return dest;
176
177
0
err:
178
0
    kdf_pbkdf2_free(dest);
179
0
    return NULL;
180
0
}
181
182
static void kdf_pbkdf2_init(KDF_PBKDF2 *ctx)
183
0
{
184
0
    OSSL_PARAM param;
185
0
    OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx);
186
187
0
    param = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
188
0
        SN_sha1, 0);
189
0
    if (!ossl_prov_digest_load(&ctx->digest, &param, NULL, provctx))
190
        /* This is an error, but there is no way to indicate such directly */
191
0
        ossl_prov_digest_reset(&ctx->digest);
192
0
    ctx->iter = PKCS5_DEFAULT_ITER;
193
#ifdef FIPS_MODULE
194
    ctx->lower_bound_checks = 1;
195
#else
196
0
    ctx->lower_bound_checks = 0;
197
0
#endif
198
0
}
199
200
static int pbkdf2_set_membuf(unsigned char **buffer, size_t *buflen,
201
    const OSSL_PARAM *p)
202
0
{
203
0
    OPENSSL_clear_free(*buffer, *buflen);
204
0
    *buffer = NULL;
205
0
    *buflen = 0;
206
207
0
    if (p->data_size == 0) {
208
0
        if ((*buffer = OPENSSL_malloc(1)) == NULL)
209
0
            return 0;
210
0
    } else if (p->data != NULL) {
211
0
        if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen))
212
0
            return 0;
213
0
    }
214
0
    return 1;
215
0
}
216
217
static int pbkdf2_lower_bound_check_passed(int saltlen, uint64_t iter,
218
    size_t keylen, size_t passlen,
219
    int *error, const char **desc)
220
0
{
221
0
    uint64_t min_iter = KDF_PBKDF2_MIN_ITERATIONS;
222
223
0
    if (passlen < KDF_PBKDF2_MIN_PASSWORD_LEN) {
224
0
        *error = PROV_R_PASSWORD_STRENGTH_TOO_WEAK;
225
0
        if (desc != NULL)
226
0
            *desc = "Weak password";
227
0
        return 0;
228
0
    }
229
0
    if ((keylen * 8) < KDF_PBKDF2_MIN_KEY_LEN_BITS) {
230
0
        *error = PROV_R_KEY_SIZE_TOO_SMALL;
231
0
        if (desc != NULL)
232
0
            *desc = "Key size";
233
0
        return 0;
234
0
    }
235
0
    if (saltlen < KDF_PBKDF2_MIN_SALT_LEN) {
236
0
        *error = PROV_R_INVALID_SALT_LENGTH;
237
0
        if (desc != NULL)
238
0
            *desc = "Salt size";
239
0
        return 0;
240
0
    }
241
#ifdef FIPS_MODULE
242
    /* Modify this check during self-test. See FIPS 140-3 IG 10.3.A.8 */
243
    if (ossl_self_test_in_progress(ST_ID_KDF_PBKDF2)) {
244
        min_iter = KDF_PBKDF2_FIPS_SELF_TEST_ITERATIONS;
245
    }
246
#endif
247
0
    if (iter < min_iter) {
248
0
        *error = PROV_R_INVALID_ITERATION_COUNT;
249
0
        if (desc != NULL)
250
0
            *desc = "Iteration count";
251
0
        return 0;
252
0
    }
253
254
0
    return 1;
255
0
}
256
257
#ifdef FIPS_MODULE
258
static int fips_lower_bound_check_passed(KDF_PBKDF2 *ctx, int saltlen,
259
    uint64_t iter, size_t keylen,
260
    size_t passlen)
261
{
262
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
263
    int error = 0;
264
    const char *desc = NULL;
265
    int approved = pbkdf2_lower_bound_check_passed(saltlen, iter, keylen,
266
        passlen, &error, &desc);
267
268
    if (!approved) {
269
        if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0, libctx,
270
                "PBKDF2", desc,
271
                ossl_fips_config_pbkdf2_lower_bound_check)) {
272
            ERR_raise(ERR_LIB_PROV, error);
273
            return 0;
274
        }
275
    }
276
    return 1;
277
}
278
#endif
279
280
static int lower_bound_check_passed(KDF_PBKDF2 *ctx, int saltlen, uint64_t iter,
281
    size_t keylen, size_t passlen,
282
    int lower_bound_checks)
283
0
{
284
#ifdef FIPS_MODULE
285
    if (!fips_lower_bound_check_passed(ctx, saltlen, iter, keylen, passlen))
286
        return 0;
287
#else
288
0
    if (lower_bound_checks) {
289
0
        int error = 0;
290
0
        int passed = pbkdf2_lower_bound_check_passed(saltlen, iter, keylen,
291
0
            passlen, &error, NULL);
292
293
0
        if (!passed) {
294
0
            ERR_raise(ERR_LIB_PROV, error);
295
0
            return 0;
296
0
        }
297
0
    } else if (iter < 1) {
298
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_ITERATION_COUNT);
299
0
        return 0;
300
0
    }
301
0
#endif
302
303
0
    return 1;
304
0
}
305
306
static int kdf_pbkdf2_derive(void *vctx, unsigned char *key, size_t keylen,
307
    const OSSL_PARAM params[])
308
0
{
309
0
    KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx;
310
0
    const EVP_MD *md;
311
312
0
    if (!ossl_prov_is_running() || !kdf_pbkdf2_set_ctx_params(ctx, params))
313
0
        return 0;
314
315
0
    if (ctx->pass == NULL) {
316
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS);
317
0
        return 0;
318
0
    }
319
320
0
    if (ctx->salt == NULL) {
321
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT);
322
0
        return 0;
323
0
    }
324
325
0
    md = ossl_prov_digest_md(&ctx->digest);
326
0
    return pbkdf2_derive(ctx, (char *)ctx->pass, ctx->pass_len,
327
0
        ctx->salt, (int)ctx->salt_len, ctx->iter,
328
0
        md, key, keylen, ctx->lower_bound_checks);
329
0
}
330
331
static int kdf_pbkdf2_set_ctx_params(void *vctx, const OSSL_PARAM params[])
332
0
{
333
0
    struct pbkdf2_set_ctx_params_st p;
334
0
    KDF_PBKDF2 *ctx = vctx;
335
0
    OSSL_LIB_CTX *provctx;
336
0
    int pkcs5;
337
0
    uint64_t iter;
338
0
    const EVP_MD *md;
339
340
0
    if (ctx == NULL || !pbkdf2_set_ctx_params_decoder(params, &p))
341
0
        return 0;
342
343
0
    provctx = PROV_LIBCTX_OF(ctx->provctx);
344
345
0
    if (p.digest != NULL) {
346
0
        if (!ossl_prov_digest_load(&ctx->digest, p.digest, p.propq, provctx))
347
0
            return 0;
348
0
        md = ossl_prov_digest_md(&ctx->digest);
349
0
        if (EVP_MD_xof(md)) {
350
0
            ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED);
351
0
            return 0;
352
0
        }
353
0
    }
354
355
0
    if (p.pkcs5 != NULL) {
356
0
        if (!OSSL_PARAM_get_int(p.pkcs5, &pkcs5))
357
0
            return 0;
358
0
        ctx->lower_bound_checks = pkcs5 == 0;
359
#ifdef FIPS_MODULE
360
        ossl_FIPS_IND_set_settable(OSSL_FIPS_IND_GET(ctx),
361
            OSSL_FIPS_IND_SETTABLE0,
362
            ctx->lower_bound_checks);
363
#endif
364
0
    }
365
366
0
    if (p.pw != NULL) {
367
0
        if (ctx->lower_bound_checks != 0
368
0
            && p.pw->data_size < KDF_PBKDF2_MIN_PASSWORD_LEN) {
369
0
            ERR_raise(ERR_LIB_PROV, PROV_R_PASSWORD_STRENGTH_TOO_WEAK);
370
0
            return 0;
371
0
        }
372
0
        if (!pbkdf2_set_membuf(&ctx->pass, &ctx->pass_len, p.pw))
373
0
            return 0;
374
0
    }
375
376
0
    if (p.salt != NULL) {
377
0
        if (!lower_bound_check_passed(ctx, (int)p.salt->data_size, UINT64_MAX, SIZE_MAX,
378
0
                SIZE_MAX, ctx->lower_bound_checks))
379
0
            return 0;
380
0
        if (!pbkdf2_set_membuf(&ctx->salt, &ctx->salt_len, p.salt))
381
0
            return 0;
382
0
    }
383
384
0
    if (p.iter != NULL) {
385
0
        if (!OSSL_PARAM_get_uint64(p.iter, &iter))
386
0
            return 0;
387
0
        if (!lower_bound_check_passed(ctx, INT_MAX, iter, SIZE_MAX,
388
0
                SIZE_MAX, ctx->lower_bound_checks))
389
0
            return 0;
390
0
        ctx->iter = iter;
391
0
    }
392
0
    return 1;
393
0
}
394
395
static const OSSL_PARAM *kdf_pbkdf2_settable_ctx_params(ossl_unused void *ctx,
396
    ossl_unused void *p_ctx)
397
0
{
398
0
    return pbkdf2_set_ctx_params_list;
399
0
}
400
401
static int kdf_pbkdf2_get_ctx_params(void *vctx, OSSL_PARAM params[])
402
0
{
403
0
    KDF_PBKDF2 *ctx = vctx;
404
0
    struct pbkdf2_get_ctx_params_st p;
405
406
0
    if (ctx == NULL || !pbkdf2_get_ctx_params_decoder(params, &p))
407
0
        return 0;
408
409
0
    if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, SIZE_MAX))
410
0
        return 0;
411
412
0
    if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(ctx, p.ind))
413
0
        return 0;
414
0
    return 1;
415
0
}
416
417
static const OSSL_PARAM *kdf_pbkdf2_gettable_ctx_params(ossl_unused void *ctx,
418
    ossl_unused void *p_ctx)
419
0
{
420
0
    return pbkdf2_get_ctx_params_list;
421
0
}
422
423
const OSSL_DISPATCH ossl_kdf_pbkdf2_functions[] = {
424
    { OSSL_FUNC_KDF_NEWCTX, (void (*)(void))kdf_pbkdf2_new },
425
    { OSSL_FUNC_KDF_DUPCTX, (void (*)(void))kdf_pbkdf2_dup },
426
    { OSSL_FUNC_KDF_FREECTX, (void (*)(void))kdf_pbkdf2_free },
427
    { OSSL_FUNC_KDF_RESET, (void (*)(void))kdf_pbkdf2_reset },
428
    { OSSL_FUNC_KDF_DERIVE, (void (*)(void))kdf_pbkdf2_derive },
429
    { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
430
        (void (*)(void))kdf_pbkdf2_settable_ctx_params },
431
    { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void (*)(void))kdf_pbkdf2_set_ctx_params },
432
    { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
433
        (void (*)(void))kdf_pbkdf2_gettable_ctx_params },
434
    { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void (*)(void))kdf_pbkdf2_get_ctx_params },
435
    OSSL_DISPATCH_END
436
};
437
438
/*
439
 * This is an implementation of PKCS#5 v2.0 password based encryption key
440
 * derivation function PBKDF2. SHA1 version verified against test vectors
441
 * posted by Peter Gutmann to the PKCS-TNG mailing list.
442
 *
443
 * The constraints specified by SP800-132 have been added i.e.
444
 *  - Check the range of the key length.
445
 *  - Minimum iteration count of 1000.
446
 *  - Randomly-generated portion of the salt shall be at least 128 bits.
447
 */
448
static int pbkdf2_derive(KDF_PBKDF2 *ctx, const char *pass, size_t passlen,
449
    const unsigned char *salt, int saltlen, uint64_t iter,
450
    const EVP_MD *digest, unsigned char *key,
451
    size_t keylen, int lower_bound_checks)
452
0
{
453
0
    int ret = 0;
454
0
    unsigned char digtmp[EVP_MAX_MD_SIZE], *p, itmp[4];
455
0
    int cplen, k, tkeylen, mdlen;
456
0
    uint64_t j;
457
0
    unsigned long i = 1;
458
0
    HMAC_CTX *hctx_tpl = NULL, *hctx = NULL;
459
460
0
    mdlen = EVP_MD_get_size(digest);
461
0
    if (mdlen <= 0)
462
0
        return 0;
463
464
    /*
465
     * This check should always be done because keylen / mdlen >= (2^32 - 1)
466
     * results in an overflow of the loop counter 'i'.
467
     */
468
0
    if ((keylen / mdlen) >= KDF_PBKDF2_MAX_KEY_LEN_DIGEST_RATIO) {
469
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
470
0
        return 0;
471
0
    }
472
473
0
    if (!lower_bound_check_passed(ctx, saltlen, iter, keylen, passlen, lower_bound_checks))
474
0
        return 0;
475
476
0
    hctx_tpl = HMAC_CTX_new();
477
0
    if (hctx_tpl == NULL)
478
0
        return 0;
479
0
    p = key;
480
0
    tkeylen = (int)keylen;
481
0
    if (!HMAC_Init_ex(hctx_tpl, pass, (int)passlen, digest, NULL))
482
0
        goto err;
483
0
    hctx = HMAC_CTX_new();
484
0
    if (hctx == NULL)
485
0
        goto err;
486
0
    while (tkeylen) {
487
0
        if (tkeylen > mdlen)
488
0
            cplen = mdlen;
489
0
        else
490
0
            cplen = tkeylen;
491
        /*
492
         * We are unlikely to ever use more than 256 blocks (5120 bits!) but
493
         * just in case...
494
         */
495
0
        itmp[0] = (unsigned char)((i >> 24) & 0xff);
496
0
        itmp[1] = (unsigned char)((i >> 16) & 0xff);
497
0
        itmp[2] = (unsigned char)((i >> 8) & 0xff);
498
0
        itmp[3] = (unsigned char)(i & 0xff);
499
0
        if (!HMAC_CTX_copy(hctx, hctx_tpl))
500
0
            goto err;
501
0
        if (!HMAC_Update(hctx, salt, saltlen)
502
0
            || !HMAC_Update(hctx, itmp, 4)
503
0
            || !HMAC_Final(hctx, digtmp, NULL))
504
0
            goto err;
505
0
        memcpy(p, digtmp, cplen);
506
0
        for (j = 1; j < iter; j++) {
507
0
            if (!HMAC_CTX_copy(hctx, hctx_tpl))
508
0
                goto err;
509
0
            if (!HMAC_Update(hctx, digtmp, mdlen)
510
0
                || !HMAC_Final(hctx, digtmp, NULL))
511
0
                goto err;
512
0
            for (k = 0; k < cplen; k++)
513
0
                p[k] ^= digtmp[k];
514
0
        }
515
0
        tkeylen -= cplen;
516
0
        i++;
517
0
        p += cplen;
518
0
    }
519
0
    ret = 1;
520
521
0
err:
522
0
    HMAC_CTX_free(hctx);
523
0
    HMAC_CTX_free(hctx_tpl);
524
0
    return ret;
525
0
}