Coverage Report

Created: 2025-04-22 06:18

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