Coverage Report

Created: 2025-08-28 07:07

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