Coverage Report

Created: 2026-07-24 06:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/evp/kdf_meth.c
Line
Count
Source
1
/*
2
 * Copyright 2019-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 <openssl/evp.h>
11
#include <openssl/err.h>
12
#include <openssl/core.h>
13
#include <openssl/core_dispatch.h>
14
#include <openssl/kdf.h>
15
#include "internal/provider.h"
16
#include "internal/core.h"
17
#include "crypto/evp.h"
18
#include "evp_local.h"
19
20
static int evp_kdf_up_ref(void *vkdf)
21
0
{
22
0
    EVP_KDF *kdf = (EVP_KDF *)vkdf;
23
0
    int ref = 0;
24
25
0
    return CRYPTO_UP_REF(&kdf->refcnt, &ref);
26
0
}
27
28
static void evp_kdf_free(void *vkdf)
29
0
{
30
0
    EVP_KDF *kdf = (EVP_KDF *)vkdf;
31
0
    int ref = 0;
32
33
0
    if (kdf == NULL)
34
0
        return;
35
36
0
    CRYPTO_DOWN_REF(&kdf->refcnt, &ref);
37
0
    if (ref > 0)
38
0
        return;
39
0
    OPENSSL_free(kdf->type_name);
40
0
    ossl_provider_free(kdf->prov);
41
0
    CRYPTO_FREE_REF(&kdf->refcnt);
42
0
    OPENSSL_free(kdf);
43
0
}
44
45
static void *evp_kdf_new(void)
46
0
{
47
0
    EVP_KDF *kdf = NULL;
48
49
0
    if ((kdf = OPENSSL_zalloc(sizeof(*kdf))) == NULL
50
0
        || !CRYPTO_NEW_REF(&kdf->refcnt, 1)) {
51
0
        OPENSSL_free(kdf);
52
0
        return NULL;
53
0
    }
54
0
    return kdf;
55
0
}
56
57
static void *evp_kdf_from_algorithm(int name_id,
58
    const OSSL_ALGORITHM *algodef,
59
    OSSL_PROVIDER *prov, int no_store)
60
0
{
61
0
    const OSSL_DISPATCH *fns = algodef->implementation;
62
0
    EVP_KDF *kdf = NULL;
63
0
    int fnkdfcnt = 0, fnctxcnt = 0;
64
65
0
    if ((kdf = evp_kdf_new()) == NULL) {
66
0
        ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
67
0
        return NULL;
68
0
    }
69
0
    kdf->name_id = name_id;
70
0
    kdf->no_store = no_store;
71
72
0
    if ((kdf->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL)
73
0
        goto err;
74
75
0
    kdf->description = algodef->algorithm_description;
76
77
0
    for (; fns->function_id != 0; fns++) {
78
0
        switch (fns->function_id) {
79
0
        case OSSL_FUNC_KDF_NEWCTX:
80
0
            if (kdf->newctx != NULL)
81
0
                break;
82
0
            kdf->newctx = OSSL_FUNC_kdf_newctx(fns);
83
0
            fnctxcnt++;
84
0
            break;
85
0
        case OSSL_FUNC_KDF_DUPCTX:
86
0
            if (kdf->dupctx != NULL)
87
0
                break;
88
0
            kdf->dupctx = OSSL_FUNC_kdf_dupctx(fns);
89
0
            break;
90
0
        case OSSL_FUNC_KDF_FREECTX:
91
0
            if (kdf->freectx != NULL)
92
0
                break;
93
0
            kdf->freectx = OSSL_FUNC_kdf_freectx(fns);
94
0
            fnctxcnt++;
95
0
            break;
96
0
        case OSSL_FUNC_KDF_RESET:
97
0
            if (kdf->reset != NULL)
98
0
                break;
99
0
            kdf->reset = OSSL_FUNC_kdf_reset(fns);
100
0
            break;
101
0
        case OSSL_FUNC_KDF_DERIVE:
102
0
            if (kdf->derive != NULL)
103
0
                break;
104
0
            kdf->derive = OSSL_FUNC_kdf_derive(fns);
105
0
            fnkdfcnt++;
106
0
            break;
107
0
        case OSSL_FUNC_KDF_GETTABLE_PARAMS:
108
0
            if (kdf->gettable_params != NULL)
109
0
                break;
110
0
            kdf->gettable_params = OSSL_FUNC_kdf_gettable_params(fns);
111
0
            break;
112
0
        case OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS:
113
0
            if (kdf->gettable_ctx_params != NULL)
114
0
                break;
115
0
            kdf->gettable_ctx_params = OSSL_FUNC_kdf_gettable_ctx_params(fns);
116
0
            break;
117
0
        case OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS:
118
0
            if (kdf->settable_ctx_params != NULL)
119
0
                break;
120
0
            kdf->settable_ctx_params = OSSL_FUNC_kdf_settable_ctx_params(fns);
121
0
            break;
122
0
        case OSSL_FUNC_KDF_GET_PARAMS:
123
0
            if (kdf->get_params != NULL)
124
0
                break;
125
0
            kdf->get_params = OSSL_FUNC_kdf_get_params(fns);
126
0
            break;
127
0
        case OSSL_FUNC_KDF_GET_CTX_PARAMS:
128
0
            if (kdf->get_ctx_params != NULL)
129
0
                break;
130
0
            kdf->get_ctx_params = OSSL_FUNC_kdf_get_ctx_params(fns);
131
0
            break;
132
0
        case OSSL_FUNC_KDF_SET_CTX_PARAMS:
133
0
            if (kdf->set_ctx_params != NULL)
134
0
                break;
135
0
            kdf->set_ctx_params = OSSL_FUNC_kdf_set_ctx_params(fns);
136
0
            break;
137
0
        case OSSL_FUNC_KDF_SET_SKEY:
138
0
            if (kdf->set_skey != NULL)
139
0
                break;
140
0
            kdf->set_skey = OSSL_FUNC_kdf_set_skey(fns);
141
0
            break;
142
0
        case OSSL_FUNC_KDF_DERIVE_SKEY:
143
0
            if (kdf->derive_skey != NULL)
144
0
                break;
145
0
            kdf->derive_skey = OSSL_FUNC_kdf_derive_skey(fns);
146
0
            break;
147
0
        }
148
0
    }
149
0
    if (fnkdfcnt != 1 || fnctxcnt != 2) {
150
        /*
151
         * In order to be a consistent set of functions we must have at least
152
         * a derive function, and a complete set of context management
153
         * functions.
154
         */
155
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
156
0
        goto err;
157
0
    }
158
0
    if (prov != NULL && !ossl_provider_up_ref(prov))
159
0
        goto err;
160
161
0
    kdf->prov = prov;
162
163
0
    return kdf;
164
165
0
err:
166
0
    evp_kdf_free(kdf);
167
0
    return NULL;
168
0
}
169
170
EVP_KDF *EVP_KDF_fetch(OSSL_LIB_CTX *libctx, const char *algorithm,
171
    const char *properties)
172
0
{
173
0
    return evp_generic_fetch(libctx, OSSL_OP_KDF, algorithm, properties,
174
0
        evp_kdf_from_algorithm, evp_kdf_up_ref,
175
0
        evp_kdf_free);
176
0
}
177
178
int EVP_KDF_up_ref(EVP_KDF *kdf)
179
0
{
180
#ifdef OPENSSL_NO_CACHED_FETCH
181
    return evp_kdf_up_ref(kdf);
182
#else
183
0
    if (kdf->no_store != 0)
184
0
        return evp_kdf_up_ref(kdf);
185
0
    return 1;
186
0
#endif
187
0
}
188
189
void EVP_KDF_free(EVP_KDF *kdf)
190
0
{
191
#ifdef OPENSSL_NO_CACHED_FETCH
192
    evp_kdf_free(kdf);
193
#else
194
0
    if (kdf != NULL && (kdf->no_store != 0))
195
0
        evp_kdf_free(kdf);
196
0
#endif
197
0
}
198
199
const OSSL_PARAM *EVP_KDF_gettable_params(const EVP_KDF *kdf)
200
0
{
201
0
    if (kdf->gettable_params == NULL)
202
0
        return NULL;
203
0
    return kdf->gettable_params(ossl_provider_ctx(EVP_KDF_get0_provider(kdf)));
204
0
}
205
206
const OSSL_PARAM *EVP_KDF_gettable_ctx_params(const EVP_KDF *kdf)
207
0
{
208
0
    void *alg;
209
210
0
    if (kdf->gettable_ctx_params == NULL)
211
0
        return NULL;
212
0
    alg = ossl_provider_ctx(EVP_KDF_get0_provider(kdf));
213
0
    return kdf->gettable_ctx_params(NULL, alg);
214
0
}
215
216
const OSSL_PARAM *EVP_KDF_settable_ctx_params(const EVP_KDF *kdf)
217
0
{
218
0
    void *alg;
219
220
0
    if (kdf->settable_ctx_params == NULL)
221
0
        return NULL;
222
0
    alg = ossl_provider_ctx(EVP_KDF_get0_provider(kdf));
223
0
    return kdf->settable_ctx_params(NULL, alg);
224
0
}
225
226
const OSSL_PARAM *EVP_KDF_CTX_gettable_params(EVP_KDF_CTX *ctx)
227
0
{
228
0
    void *alg;
229
230
0
    if (ctx->meth->gettable_ctx_params == NULL)
231
0
        return NULL;
232
0
    alg = ossl_provider_ctx(EVP_KDF_get0_provider(ctx->meth));
233
0
    return ctx->meth->gettable_ctx_params(ctx->algctx, alg);
234
0
}
235
236
const OSSL_PARAM *EVP_KDF_CTX_settable_params(EVP_KDF_CTX *ctx)
237
0
{
238
0
    void *alg;
239
240
0
    if (ctx->meth->settable_ctx_params == NULL)
241
0
        return NULL;
242
0
    alg = ossl_provider_ctx(EVP_KDF_get0_provider(ctx->meth));
243
0
    return ctx->meth->settable_ctx_params(ctx->algctx, alg);
244
0
}
245
246
void EVP_KDF_do_all_provided(OSSL_LIB_CTX *libctx,
247
    void (*fn)(EVP_KDF *kdf, void *arg),
248
    void *arg)
249
0
{
250
0
    struct EVP_KDF_do_all_provided_thunk t;
251
252
0
    t.fn = fn;
253
0
    t.arg = arg;
254
0
    evp_generic_do_all(libctx, OSSL_OP_KDF,
255
0
        EVP_KDF_do_all_provided_thunk, &t,
256
0
        evp_kdf_from_algorithm, evp_kdf_up_ref, evp_kdf_free);
257
0
}