Coverage Report

Created: 2025-08-28 07:07

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