Coverage Report

Created: 2023-06-08 06:40

/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
0
{
22
0
    EVP_KDF *kdf = (EVP_KDF *)vkdf;
23
0
    int ref = 0;
24
25
0
    CRYPTO_UP_REF(&kdf->refcnt, &ref, kdf->lock);
26
0
    return 1;
27
0
}
28
29
static void evp_kdf_free(void *vkdf)
30
0
{
31
0
    EVP_KDF *kdf = (EVP_KDF *)vkdf;
32
0
    int ref = 0;
33
34
0
    if (kdf == NULL)
35
0
        return;
36
37
0
    CRYPTO_DOWN_REF(&kdf->refcnt, &ref, kdf->lock);
38
0
    if (ref > 0)
39
0
        return;
40
0
    OPENSSL_free(kdf->type_name);
41
0
    ossl_provider_free(kdf->prov);
42
0
    CRYPTO_THREAD_lock_free(kdf->lock);
43
0
    OPENSSL_free(kdf);
44
0
}
45
46
static void *evp_kdf_new(void)
47
0
{
48
0
    EVP_KDF *kdf = NULL;
49
50
0
    if ((kdf = OPENSSL_zalloc(sizeof(*kdf))) == NULL
51
0
        || (kdf->lock = CRYPTO_THREAD_lock_new()) == NULL) {
52
0
        OPENSSL_free(kdf);
53
0
        return NULL;
54
0
    }
55
0
    kdf->refcnt = 1;
56
0
    return kdf;
57
0
}
58
59
static void *evp_kdf_from_algorithm(int name_id,
60
                                    const OSSL_ALGORITHM *algodef,
61
                                    OSSL_PROVIDER *prov)
62
0
{
63
0
    const OSSL_DISPATCH *fns = algodef->implementation;
64
0
    EVP_KDF *kdf = NULL;
65
0
    int fnkdfcnt = 0, fnctxcnt = 0;
66
67
0
    if ((kdf = evp_kdf_new()) == NULL) {
68
0
        ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
69
0
        return NULL;
70
0
    }
71
0
    kdf->name_id = name_id;
72
0
    if ((kdf->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
73
0
        evp_kdf_free(kdf);
74
0
        return NULL;
75
0
    }
76
0
    kdf->description = algodef->algorithm_description;
77
78
0
    for (; fns->function_id != 0; fns++) {
79
0
        switch (fns->function_id) {
80
0
        case OSSL_FUNC_KDF_NEWCTX:
81
0
            if (kdf->newctx != NULL)
82
0
                break;
83
0
            kdf->newctx = OSSL_FUNC_kdf_newctx(fns);
84
0
            fnctxcnt++;
85
0
            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
0
        case OSSL_FUNC_KDF_FREECTX:
92
0
            if (kdf->freectx != NULL)
93
0
                break;
94
0
            kdf->freectx = OSSL_FUNC_kdf_freectx(fns);
95
0
            fnctxcnt++;
96
0
            break;
97
0
        case OSSL_FUNC_KDF_RESET:
98
0
            if (kdf->reset != NULL)
99
0
                break;
100
0
            kdf->reset = OSSL_FUNC_kdf_reset(fns);
101
0
            break;
102
0
        case OSSL_FUNC_KDF_DERIVE:
103
0
            if (kdf->derive != NULL)
104
0
                break;
105
0
            kdf->derive = OSSL_FUNC_kdf_derive(fns);
106
0
            fnkdfcnt++;
107
0
            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
0
        case OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS:
115
0
            if (kdf->gettable_ctx_params != NULL)
116
0
                break;
117
0
            kdf->gettable_ctx_params =
118
0
                OSSL_FUNC_kdf_gettable_ctx_params(fns);
119
0
            break;
120
0
        case OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS:
121
0
            if (kdf->settable_ctx_params != NULL)
122
0
                break;
123
0
            kdf->settable_ctx_params =
124
0
                OSSL_FUNC_kdf_settable_ctx_params(fns);
125
0
            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
0
        case OSSL_FUNC_KDF_GET_CTX_PARAMS:
132
0
            if (kdf->get_ctx_params != NULL)
133
0
                break;
134
0
            kdf->get_ctx_params = OSSL_FUNC_kdf_get_ctx_params(fns);
135
0
            break;
136
0
        case OSSL_FUNC_KDF_SET_CTX_PARAMS:
137
0
            if (kdf->set_ctx_params != NULL)
138
0
                break;
139
0
            kdf->set_ctx_params = OSSL_FUNC_kdf_set_ctx_params(fns);
140
0
            break;
141
0
        }
142
0
    }
143
0
    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
0
    kdf->prov = prov;
154
0
    if (prov != NULL)
155
0
        ossl_provider_up_ref(prov);
156
157
0
    return kdf;
158
0
}
159
160
EVP_KDF *EVP_KDF_fetch(OSSL_LIB_CTX *libctx, const char *algorithm,
161
                       const char *properties)
162
0
{
163
0
    return evp_generic_fetch(libctx, OSSL_OP_KDF, algorithm, properties,
164
0
                             evp_kdf_from_algorithm, evp_kdf_up_ref,
165
0
                             evp_kdf_free);
166
0
}
167
168
int EVP_KDF_up_ref(EVP_KDF *kdf)
169
0
{
170
0
    return evp_kdf_up_ref(kdf);
171
0
}
172
173
void EVP_KDF_free(EVP_KDF *kdf)
174
0
{
175
0
    evp_kdf_free(kdf);
176
0
}
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
0
{
187
0
    void *alg;
188
189
0
    if (kdf->gettable_ctx_params == NULL)
190
0
        return NULL;
191
0
    alg = ossl_provider_ctx(EVP_KDF_get0_provider(kdf));
192
0
    return kdf->gettable_ctx_params(NULL, alg);
193
0
}
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
}