Coverage Report

Created: 2024-11-21 07:03

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