Coverage Report

Created: 2024-07-27 06:39

/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
44
#define KDF_PBKDF2_MAX_KEY_LEN_DIGEST_RATIO 0xFFFFFFFF
36
8
#define KDF_PBKDF2_MIN_ITERATIONS 1000
37
17
#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
65
{
69
65
    KDF_PBKDF2 *ctx;
70
71
65
    if (!ossl_prov_is_running())
72
0
        return NULL;
73
74
65
    ctx = OPENSSL_zalloc(sizeof(*ctx));
75
65
    if (ctx == NULL) {
76
0
        ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
77
0
        return NULL;
78
0
    }
79
65
    ctx->provctx = provctx;
80
65
    return ctx;
81
65
}
82
83
static void *kdf_pbkdf2_new(void *provctx)
84
65
{
85
65
    KDF_PBKDF2 *ctx = kdf_pbkdf2_new_no_init(provctx);
86
87
65
    if (ctx != NULL)
88
65
        kdf_pbkdf2_init(ctx);
89
65
    return ctx;
90
65
}
91
92
static void kdf_pbkdf2_cleanup(KDF_PBKDF2 *ctx)
93
65
{
94
65
    ossl_prov_digest_reset(&ctx->digest);
95
65
    OPENSSL_free(ctx->salt);
96
65
    OPENSSL_clear_free(ctx->pass, ctx->pass_len);
97
65
    memset(ctx, 0, sizeof(*ctx));
98
65
}
99
100
static void kdf_pbkdf2_free(void *vctx)
101
65
{
102
65
    KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx;
103
104
65
    if (ctx != NULL) {
105
65
        kdf_pbkdf2_cleanup(ctx);
106
65
        OPENSSL_free(ctx);
107
65
    }
108
65
}
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
65
{
146
65
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
147
65
    OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx);
148
149
65
    params[0] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
150
65
                                                 SN_sha1, 0);
151
65
    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
65
    ctx->iter = PKCS5_DEFAULT_ITER;
155
65
    ctx->lower_bound_checks = ossl_kdf_pbkdf2_default_checks;
156
65
}
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
55
        if ((*buffer = OPENSSL_malloc(1)) == NULL) {
167
0
            ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
168
0
            return 0;
169
0
        }
170
60
    } else if (p->data != NULL) {
171
60
        if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen))
172
0
            return 0;
173
60
    }
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
45
{
180
45
    KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx;
181
45
    const EVP_MD *md;
182
183
45
    if (!ossl_prov_is_running() || !kdf_pbkdf2_set_ctx_params(ctx, params))
184
0
        return 0;
185
186
45
    if (ctx->pass == NULL) {
187
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS);
188
0
        return 0;
189
0
    }
190
191
45
    if (ctx->salt == NULL) {
192
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT);
193
0
        return 0;
194
0
    }
195
196
45
    md = ossl_prov_digest_md(&ctx->digest);
197
45
    return pbkdf2_derive((char *)ctx->pass, ctx->pass_len,
198
45
                         ctx->salt, ctx->salt_len, ctx->iter,
199
45
                         md, key, keylen, ctx->lower_bound_checks);
200
45
}
201
202
static int kdf_pbkdf2_set_ctx_params(void *vctx, const OSSL_PARAM params[])
203
110
{
204
110
    const OSSL_PARAM *p;
205
110
    KDF_PBKDF2 *ctx = vctx;
206
110
    OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx);
207
110
    int pkcs5;
208
110
    uint64_t iter, min_iter;
209
210
110
    if (params == NULL)
211
45
        return 1;
212
213
65
    if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx))
214
3
        return 0;
215
216
62
    if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PKCS5)) != NULL) {
217
62
        if (!OSSL_PARAM_get_int(p, &pkcs5))
218
0
            return 0;
219
62
        ctx->lower_bound_checks = pkcs5 == 0;
220
62
    }
221
222
62
    if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PASSWORD)) != NULL)
223
62
        if (!pbkdf2_set_membuf(&ctx->pass, &ctx->pass_len, p))
224
0
            return 0;
225
226
62
    if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT)) != NULL) {
227
62
        if (ctx->lower_bound_checks != 0
228
62
            && p->data_size < KDF_PBKDF2_MIN_SALT_LEN) {
229
9
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
230
9
            return 0;
231
9
        }
232
53
        if (!pbkdf2_set_membuf(&ctx->salt, &ctx->salt_len,p))
233
0
            return 0;
234
53
    }
235
236
53
    if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_ITER)) != NULL) {
237
53
        if (!OSSL_PARAM_get_uint64(p, &iter))
238
0
            return 0;
239
53
        min_iter = ctx->lower_bound_checks != 0 ? KDF_PBKDF2_MIN_ITERATIONS : 1;
240
53
        if (iter < min_iter) {
241
8
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_ITERATION_COUNT);
242
8
            return 0;
243
8
        }
244
45
        ctx->iter = iter;
245
45
    }
246
45
    return 1;
247
53
}
248
249
static const OSSL_PARAM *kdf_pbkdf2_settable_ctx_params(ossl_unused void *ctx,
250
                                                        ossl_unused void *p_ctx)
251
65
{
252
65
    static const OSSL_PARAM known_settable_ctx_params[] = {
253
65
        OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
254
65
        OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
255
65
        OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PASSWORD, NULL, 0),
256
65
        OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0),
257
65
        OSSL_PARAM_uint64(OSSL_KDF_PARAM_ITER, NULL),
258
65
        OSSL_PARAM_int(OSSL_KDF_PARAM_PKCS5, NULL),
259
65
        OSSL_PARAM_END
260
65
    };
261
65
    return known_settable_ctx_params;
262
65
}
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
45
{
313
45
    int ret = 0;
314
45
    unsigned char digtmp[EVP_MAX_MD_SIZE], *p, itmp[4];
315
45
    int cplen, k, tkeylen, mdlen;
316
45
    uint64_t j;
317
45
    unsigned long i = 1;
318
45
    HMAC_CTX *hctx_tpl = NULL, *hctx = NULL;
319
320
45
    mdlen = EVP_MD_get_size(digest);
321
45
    if (mdlen <= 0)
322
1
        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
44
    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
44
    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
44
    hctx_tpl = HMAC_CTX_new();
349
44
    if (hctx_tpl == NULL)
350
0
        return 0;
351
44
    p = key;
352
44
    tkeylen = keylen;
353
44
    if (!HMAC_Init_ex(hctx_tpl, pass, passlen, digest, NULL))
354
0
        goto err;
355
44
    hctx = HMAC_CTX_new();
356
44
    if (hctx == NULL)
357
0
        goto err;
358
105
    while (tkeylen) {
359
61
        if (tkeylen > mdlen)
360
17
            cplen = mdlen;
361
44
        else
362
44
            cplen = tkeylen;
363
        /*
364
         * We are unlikely to ever use more than 256 blocks (5120 bits!) but
365
         * just in case...
366
         */
367
61
        itmp[0] = (unsigned char)((i >> 24) & 0xff);
368
61
        itmp[1] = (unsigned char)((i >> 16) & 0xff);
369
61
        itmp[2] = (unsigned char)((i >> 8) & 0xff);
370
61
        itmp[3] = (unsigned char)(i & 0xff);
371
61
        if (!HMAC_CTX_copy(hctx, hctx_tpl))
372
0
            goto err;
373
61
        if (!HMAC_Update(hctx, salt, saltlen)
374
61
                || !HMAC_Update(hctx, itmp, 4)
375
61
                || !HMAC_Final(hctx, digtmp, NULL))
376
0
            goto err;
377
61
        memcpy(p, digtmp, cplen);
378
61
        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
61
        tkeylen -= cplen;
388
61
        i++;
389
61
        p += cplen;
390
61
    }
391
44
    ret = 1;
392
393
44
err:
394
44
    HMAC_CTX_free(hctx);
395
44
    HMAC_CTX_free(hctx_tpl);
396
44
    return ret;
397
44
}