Coverage Report

Created: 2025-12-31 06:58

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