Coverage Report

Created: 2025-06-13 06:58

/src/openssl31/providers/implementations/kdfs/pbkdf2.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2018-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
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/numbers.h"
26
#include "crypto/evp.h"
27
#include "prov/provider_ctx.h"
28
#include "prov/providercommon.h"
29
#include "prov/implementations.h"
30
#include "prov/provider_util.h"
31
#include "pbkdf2.h"
32
33
/* Constants specified in SP800-132 */
34
0
#define KDF_PBKDF2_MIN_KEY_LEN_BITS  112
35
0
#define KDF_PBKDF2_MAX_KEY_LEN_DIGEST_RATIO 0xFFFFFFFF
36
0
#define KDF_PBKDF2_MIN_ITERATIONS 1000
37
0
#define KDF_PBKDF2_MIN_SALT_LEN   (128 / 8)
38
39
static OSSL_FUNC_kdf_newctx_fn kdf_pbkdf2_new;
40
static OSSL_FUNC_kdf_dupctx_fn kdf_pbkdf2_dup;
41
static OSSL_FUNC_kdf_freectx_fn kdf_pbkdf2_free;
42
static OSSL_FUNC_kdf_reset_fn kdf_pbkdf2_reset;
43
static OSSL_FUNC_kdf_derive_fn kdf_pbkdf2_derive;
44
static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_pbkdf2_settable_ctx_params;
45
static OSSL_FUNC_kdf_set_ctx_params_fn kdf_pbkdf2_set_ctx_params;
46
static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_pbkdf2_gettable_ctx_params;
47
static OSSL_FUNC_kdf_get_ctx_params_fn kdf_pbkdf2_get_ctx_params;
48
49
static int pbkdf2_derive(const char *pass, size_t passlen,
50
                         const unsigned char *salt, int saltlen, uint64_t iter,
51
                         const EVP_MD *digest, unsigned char *key,
52
                         size_t keylen, int extra_checks);
53
54
typedef struct {
55
    void *provctx;
56
    unsigned char *pass;
57
    size_t pass_len;
58
    unsigned char *salt;
59
    size_t salt_len;
60
    uint64_t iter;
61
    PROV_DIGEST digest;
62
    int lower_bound_checks;
63
} KDF_PBKDF2;
64
65
static void kdf_pbkdf2_init(KDF_PBKDF2 *ctx);
66
67
static void *kdf_pbkdf2_new_no_init(void *provctx)
68
67
{
69
67
    KDF_PBKDF2 *ctx;
70
71
67
    if (!ossl_prov_is_running())
72
0
        return NULL;
73
74
67
    ctx = OPENSSL_zalloc(sizeof(*ctx));
75
67
    if (ctx == NULL) {
76
0
        ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
77
0
        return NULL;
78
0
    }
79
67
    ctx->provctx = provctx;
80
67
    return ctx;
81
67
}
82
83
static void *kdf_pbkdf2_new(void *provctx)
84
67
{
85
67
    KDF_PBKDF2 *ctx = kdf_pbkdf2_new_no_init(provctx);
86
87
67
    if (ctx != NULL)
88
67
        kdf_pbkdf2_init(ctx);
89
67
    return ctx;
90
67
}
91
92
static void kdf_pbkdf2_cleanup(KDF_PBKDF2 *ctx)
93
67
{
94
67
    ossl_prov_digest_reset(&ctx->digest);
95
67
    OPENSSL_free(ctx->salt);
96
67
    OPENSSL_clear_free(ctx->pass, ctx->pass_len);
97
67
    memset(ctx, 0, sizeof(*ctx));
98
67
}
99
100
static void kdf_pbkdf2_free(void *vctx)
101
67
{
102
67
    KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx;
103
104
67
    if (ctx != NULL) {
105
67
        kdf_pbkdf2_cleanup(ctx);
106
67
        OPENSSL_free(ctx);
107
67
    }
108
67
}
109
110
static void kdf_pbkdf2_reset(void *vctx)
111
0
{
112
0
    KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx;
113
0
    void *provctx = ctx->provctx;
114
115
0
    kdf_pbkdf2_cleanup(ctx);
116
0
    ctx->provctx = provctx;
117
0
    kdf_pbkdf2_init(ctx);
118
0
}
119
120
static void *kdf_pbkdf2_dup(void *vctx)
121
0
{
122
0
    const KDF_PBKDF2 *src = (const KDF_PBKDF2 *)vctx;
123
0
    KDF_PBKDF2 *dest;
124
125
    /* We need a new PBKDF2 object but uninitialised since we're filling it */
126
0
    dest = kdf_pbkdf2_new_no_init(src->provctx);
127
0
    if (dest != NULL) {
128
0
        if (!ossl_prov_memdup(src->salt, src->salt_len,
129
0
                              &dest->salt, &dest->salt_len)
130
0
                || !ossl_prov_memdup(src->pass, src->pass_len,
131
0
                                     &dest->pass, &dest->pass_len)
132
0
                || !ossl_prov_digest_copy(&dest->digest, &src->digest))
133
0
            goto err;
134
0
        dest->iter = src->iter;
135
0
        dest->lower_bound_checks = src->lower_bound_checks;
136
0
    }
137
0
    return dest;
138
139
0
 err:
140
0
    kdf_pbkdf2_free(dest);
141
0
    return NULL;
142
0
}
143
144
static void kdf_pbkdf2_init(KDF_PBKDF2 *ctx)
145
67
{
146
67
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
147
67
    OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx);
148
149
67
    params[0] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
150
67
                                                 SN_sha1, 0);
151
67
    if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx))
152
        /* This is an error, but there is no way to indicate such directly */
153
0
        ossl_prov_digest_reset(&ctx->digest);
154
67
    ctx->iter = PKCS5_DEFAULT_ITER;
155
67
    ctx->lower_bound_checks = ossl_kdf_pbkdf2_default_checks;
156
67
}
157
158
static int pbkdf2_set_membuf(unsigned char **buffer, size_t *buflen,
159
                             const OSSL_PARAM *p)
160
115
{
161
115
    OPENSSL_clear_free(*buffer, *buflen);
162
115
    *buffer = NULL;
163
115
    *buflen = 0;
164
165
115
    if (p->data_size == 0) {
166
59
        if ((*buffer = OPENSSL_malloc(1)) == NULL) {
167
0
            ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
168
0
            return 0;
169
0
        }
170
59
    } else if (p->data != NULL) {
171
56
        if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen))
172
0
            return 0;
173
56
    }
174
115
    return 1;
175
115
}
176
177
static int kdf_pbkdf2_derive(void *vctx, unsigned char *key, size_t keylen,
178
                             const OSSL_PARAM params[])
179
49
{
180
49
    KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx;
181
49
    const EVP_MD *md;
182
183
49
    if (!ossl_prov_is_running() || !kdf_pbkdf2_set_ctx_params(ctx, params))
184
0
        return 0;
185
186
49
    if (ctx->pass == NULL) {
187
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS);
188
0
        return 0;
189
0
    }
190
191
49
    if (ctx->salt == NULL) {
192
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT);
193
0
        return 0;
194
0
    }
195
196
49
    md = ossl_prov_digest_md(&ctx->digest);
197
49
    return pbkdf2_derive((char *)ctx->pass, ctx->pass_len,
198
49
                         ctx->salt, ctx->salt_len, ctx->iter,
199
49
                         md, key, keylen, ctx->lower_bound_checks);
200
49
}
201
202
static int kdf_pbkdf2_set_ctx_params(void *vctx, const OSSL_PARAM params[])
203
0
{
204
0
    const OSSL_PARAM *p;
205
0
    KDF_PBKDF2 *ctx = vctx;
206
0
    OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx);
207
0
    int pkcs5;
208
0
    uint64_t iter, min_iter;
209
210
0
    if (params == NULL)
211
0
        return 1;
212
213
0
    if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx))
214
0
        return 0;
215
216
0
    if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PKCS5)) != NULL) {
217
0
        if (!OSSL_PARAM_get_int(p, &pkcs5))
218
0
            return 0;
219
0
        ctx->lower_bound_checks = pkcs5 == 0;
220
0
    }
221
222
0
    if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PASSWORD)) != NULL)
223
0
        if (!pbkdf2_set_membuf(&ctx->pass, &ctx->pass_len, p))
224
0
            return 0;
225
226
0
    if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT)) != NULL) {
227
0
        if (ctx->lower_bound_checks != 0
228
0
            && p->data_size < KDF_PBKDF2_MIN_SALT_LEN) {
229
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
230
0
            return 0;
231
0
        }
232
0
        if (!pbkdf2_set_membuf(&ctx->salt, &ctx->salt_len,p))
233
0
            return 0;
234
0
    }
235
236
0
    if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_ITER)) != NULL) {
237
0
        if (!OSSL_PARAM_get_uint64(p, &iter))
238
0
            return 0;
239
0
        min_iter = ctx->lower_bound_checks != 0 ? KDF_PBKDF2_MIN_ITERATIONS : 1;
240
0
        if (iter < min_iter) {
241
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_ITERATION_COUNT);
242
0
            return 0;
243
0
        }
244
0
        ctx->iter = iter;
245
0
    }
246
0
    return 1;
247
0
}
248
249
static const OSSL_PARAM *kdf_pbkdf2_settable_ctx_params(ossl_unused void *ctx,
250
                                                        ossl_unused void *p_ctx)
251
67
{
252
67
    static const OSSL_PARAM known_settable_ctx_params[] = {
253
67
        OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
254
67
        OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
255
67
        OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PASSWORD, NULL, 0),
256
67
        OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0),
257
67
        OSSL_PARAM_uint64(OSSL_KDF_PARAM_ITER, NULL),
258
67
        OSSL_PARAM_int(OSSL_KDF_PARAM_PKCS5, NULL),
259
67
        OSSL_PARAM_END
260
67
    };
261
67
    return known_settable_ctx_params;
262
67
}
263
264
static int kdf_pbkdf2_get_ctx_params(void *vctx, OSSL_PARAM params[])
265
0
{
266
0
    OSSL_PARAM *p;
267
268
0
    if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
269
0
        return OSSL_PARAM_set_size_t(p, SIZE_MAX);
270
0
    return -2;
271
0
}
272
273
static const OSSL_PARAM *kdf_pbkdf2_gettable_ctx_params(ossl_unused void *ctx,
274
                                                        ossl_unused void *p_ctx)
275
0
{
276
0
    static const OSSL_PARAM known_gettable_ctx_params[] = {
277
0
        OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
278
0
        OSSL_PARAM_END
279
0
    };
280
0
    return known_gettable_ctx_params;
281
0
}
282
283
const OSSL_DISPATCH ossl_kdf_pbkdf2_functions[] = {
284
    { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_pbkdf2_new },
285
    { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_pbkdf2_dup },
286
    { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_pbkdf2_free },
287
    { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_pbkdf2_reset },
288
    { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_pbkdf2_derive },
289
    { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
290
      (void(*)(void))kdf_pbkdf2_settable_ctx_params },
291
    { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_pbkdf2_set_ctx_params },
292
    { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
293
      (void(*)(void))kdf_pbkdf2_gettable_ctx_params },
294
    { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_pbkdf2_get_ctx_params },
295
    { 0, NULL }
296
};
297
298
/*
299
 * This is an implementation of PKCS#5 v2.0 password based encryption key
300
 * derivation function PBKDF2. SHA1 version verified against test vectors
301
 * posted by Peter Gutmann to the PKCS-TNG mailing list.
302
 *
303
 * The constraints specified by SP800-132 have been added i.e.
304
 *  - Check the range of the key length.
305
 *  - Minimum iteration count of 1000.
306
 *  - Randomly-generated portion of the salt shall be at least 128 bits.
307
 */
308
static int pbkdf2_derive(const char *pass, size_t passlen,
309
                         const unsigned char *salt, int saltlen, uint64_t iter,
310
                         const EVP_MD *digest, unsigned char *key,
311
                         size_t keylen, int lower_bound_checks)
312
0
{
313
0
    int ret = 0;
314
0
    unsigned char digtmp[EVP_MAX_MD_SIZE], *p, itmp[4];
315
0
    int cplen, k, tkeylen, mdlen;
316
0
    uint64_t j;
317
0
    unsigned long i = 1;
318
0
    HMAC_CTX *hctx_tpl = NULL, *hctx = NULL;
319
320
0
    mdlen = EVP_MD_get_size(digest);
321
0
    if (mdlen <= 0)
322
0
        return 0;
323
324
    /*
325
     * This check should always be done because keylen / mdlen >= (2^32 - 1)
326
     * results in an overflow of the loop counter 'i'.
327
     */
328
0
    if ((keylen / mdlen) >= KDF_PBKDF2_MAX_KEY_LEN_DIGEST_RATIO) {
329
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
330
0
        return 0;
331
0
    }
332
333
0
    if (lower_bound_checks) {
334
0
        if ((keylen * 8) < KDF_PBKDF2_MIN_KEY_LEN_BITS) {
335
0
            ERR_raise(ERR_LIB_PROV, PROV_R_KEY_SIZE_TOO_SMALL);
336
0
            return 0;
337
0
        }
338
0
        if (saltlen < KDF_PBKDF2_MIN_SALT_LEN) {
339
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
340
0
            return 0;
341
0
        }
342
0
        if (iter < KDF_PBKDF2_MIN_ITERATIONS) {
343
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_ITERATION_COUNT);
344
0
            return 0;
345
0
        }
346
0
    }
347
348
0
    hctx_tpl = HMAC_CTX_new();
349
0
    if (hctx_tpl == NULL)
350
0
        return 0;
351
0
    p = key;
352
0
    tkeylen = keylen;
353
0
    if (!HMAC_Init_ex(hctx_tpl, pass, passlen, digest, NULL))
354
0
        goto err;
355
0
    hctx = HMAC_CTX_new();
356
0
    if (hctx == NULL)
357
0
        goto err;
358
0
    while (tkeylen) {
359
0
        if (tkeylen > mdlen)
360
0
            cplen = mdlen;
361
0
        else
362
0
            cplen = tkeylen;
363
        /*
364
         * We are unlikely to ever use more than 256 blocks (5120 bits!) but
365
         * just in case...
366
         */
367
0
        itmp[0] = (unsigned char)((i >> 24) & 0xff);
368
0
        itmp[1] = (unsigned char)((i >> 16) & 0xff);
369
0
        itmp[2] = (unsigned char)((i >> 8) & 0xff);
370
0
        itmp[3] = (unsigned char)(i & 0xff);
371
0
        if (!HMAC_CTX_copy(hctx, hctx_tpl))
372
0
            goto err;
373
0
        if (!HMAC_Update(hctx, salt, saltlen)
374
0
                || !HMAC_Update(hctx, itmp, 4)
375
0
                || !HMAC_Final(hctx, digtmp, NULL))
376
0
            goto err;
377
0
        memcpy(p, digtmp, cplen);
378
0
        for (j = 1; j < iter; j++) {
379
0
            if (!HMAC_CTX_copy(hctx, hctx_tpl))
380
0
                goto err;
381
0
            if (!HMAC_Update(hctx, digtmp, mdlen)
382
0
                    || !HMAC_Final(hctx, digtmp, NULL))
383
0
                goto err;
384
0
            for (k = 0; k < cplen; k++)
385
0
                p[k] ^= digtmp[k];
386
0
        }
387
0
        tkeylen -= cplen;
388
0
        i++;
389
0
        p += cplen;
390
0
    }
391
0
    ret = 1;
392
393
0
err:
394
0
    HMAC_CTX_free(hctx);
395
0
    HMAC_CTX_free(hctx_tpl);
396
0
    return ret;
397
0
}