Coverage Report

Created: 2026-07-16 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/evp/kdf_lib.c
Line
Count
Source
1
/*
2
 * Copyright 2018-2025 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright (c) 2018-2019, Oracle and/or its affiliates.  All rights reserved.
4
 *
5
 * Licensed under the Apache License 2.0 (the "License").  You may not use
6
 * this file except in compliance with the License.  You can obtain a copy
7
 * in the file LICENSE in the source distribution or at
8
 * https://www.openssl.org/source/license.html
9
 */
10
11
#include <stdio.h>
12
#include <stdlib.h>
13
#include "internal/cryptlib.h"
14
#include <openssl/evp.h>
15
#include <openssl/kdf.h>
16
#include <openssl/core.h>
17
#include <openssl/core_names.h>
18
#include "crypto/evp.h"
19
#include "internal/numbers.h"
20
#include "internal/provider.h"
21
#include "evp_local.h"
22
#include "internal/param_build_set.h"
23
24
EVP_KDF_CTX *EVP_KDF_CTX_new(EVP_KDF *kdf)
25
0
{
26
0
    EVP_KDF_CTX *ctx = NULL;
27
28
0
    if (kdf == NULL)
29
0
        return NULL;
30
31
0
    ctx = OPENSSL_zalloc(sizeof(EVP_KDF_CTX));
32
0
    if (ctx == NULL
33
0
        || (ctx->algctx = kdf->newctx(ossl_provider_ctx(kdf->prov))) == NULL
34
0
        || !EVP_KDF_up_ref(kdf)) {
35
0
        ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
36
0
        if (ctx != NULL)
37
0
            kdf->freectx(ctx->algctx);
38
0
        OPENSSL_free(ctx);
39
0
        ctx = NULL;
40
0
    } else {
41
0
        ctx->meth = kdf;
42
0
    }
43
0
    return ctx;
44
0
}
45
46
void EVP_KDF_CTX_free(EVP_KDF_CTX *ctx)
47
0
{
48
0
    if (ctx == NULL)
49
0
        return;
50
0
    ctx->meth->freectx(ctx->algctx);
51
0
    ctx->algctx = NULL;
52
0
    EVP_KDF_free(ctx->meth);
53
0
    OPENSSL_free(ctx);
54
0
}
55
56
EVP_KDF_CTX *EVP_KDF_CTX_dup(const EVP_KDF_CTX *src)
57
0
{
58
0
    EVP_KDF_CTX *dst;
59
60
0
    if (src == NULL || src->algctx == NULL || src->meth->dupctx == NULL)
61
0
        return NULL;
62
63
0
    dst = OPENSSL_malloc(sizeof(*dst));
64
0
    if (dst == NULL)
65
0
        return NULL;
66
67
0
    memcpy(dst, src, sizeof(*dst));
68
0
    if (!EVP_KDF_up_ref(dst->meth)) {
69
0
        ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
70
0
        OPENSSL_free(dst);
71
0
        return NULL;
72
0
    }
73
74
0
    dst->algctx = src->meth->dupctx(src->algctx);
75
0
    if (dst->algctx == NULL) {
76
0
        EVP_KDF_CTX_free(dst);
77
0
        return NULL;
78
0
    }
79
0
    return dst;
80
0
}
81
82
int evp_kdf_get_number(const EVP_KDF *kdf)
83
0
{
84
0
    return kdf->name_id;
85
0
}
86
87
const char *EVP_KDF_get0_name(const EVP_KDF *kdf)
88
0
{
89
0
    return kdf->type_name;
90
0
}
91
92
const char *EVP_KDF_get0_description(const EVP_KDF *kdf)
93
0
{
94
0
    return kdf->description;
95
0
}
96
97
int EVP_KDF_is_a(const EVP_KDF *kdf, const char *name)
98
0
{
99
0
    return kdf != NULL && evp_is_a(kdf->prov, kdf->name_id, NULL, name);
100
0
}
101
102
const OSSL_PROVIDER *EVP_KDF_get0_provider(const EVP_KDF *kdf)
103
0
{
104
0
    return kdf->prov;
105
0
}
106
107
const EVP_KDF *EVP_KDF_CTX_get0_kdf(const EVP_KDF_CTX *ctx)
108
0
{
109
0
    return ctx->meth;
110
0
}
111
112
#if !defined(OPENSSL_NO_DEPRECATED_4_1)
113
const EVP_KDF *EVP_KDF_CTX_kdf(const EVP_KDF_CTX *ctx)
114
0
{
115
0
    return EVP_KDF_CTX_get0_kdf(ctx);
116
0
}
117
#endif /* !OPENSSL_NO_DEPRECATED_4_1 */
118
119
EVP_KDF *EVP_KDF_CTX_get1_kdf(const EVP_KDF_CTX *ctx)
120
0
{
121
0
    if (!EVP_KDF_up_ref(ctx->meth))
122
0
        return NULL;
123
0
    return ctx->meth;
124
0
}
125
126
void EVP_KDF_CTX_reset(EVP_KDF_CTX *ctx)
127
0
{
128
0
    if (ctx == NULL)
129
0
        return;
130
131
0
    if (ctx->meth->reset != NULL)
132
0
        ctx->meth->reset(ctx->algctx);
133
0
}
134
135
size_t EVP_KDF_CTX_get_kdf_size(EVP_KDF_CTX *ctx)
136
0
{
137
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
138
0
    size_t s = 0;
139
140
0
    if (ctx == NULL)
141
0
        return 0;
142
143
0
    *params = OSSL_PARAM_construct_size_t(OSSL_KDF_PARAM_SIZE, &s);
144
0
    if (ctx->meth->get_ctx_params != NULL
145
0
        && ctx->meth->get_ctx_params(ctx->algctx, params))
146
0
        return s;
147
0
    if (ctx->meth->get_params != NULL
148
0
        && ctx->meth->get_params(params))
149
0
        return s;
150
0
    return 0;
151
0
}
152
153
int EVP_KDF_derive(EVP_KDF_CTX *ctx, unsigned char *key, size_t keylen,
154
    const OSSL_PARAM params[])
155
0
{
156
0
    if (ctx == NULL)
157
0
        return 0;
158
159
0
    return ctx->meth->derive(ctx->algctx, key, keylen, params);
160
0
}
161
162
struct convert_key {
163
    const char *name;
164
    OSSL_PARAM *param;
165
};
166
167
static int convert_key_cb(const OSSL_PARAM params[], void *arg)
168
0
{
169
0
    struct convert_key *ckey = arg;
170
0
    const OSSL_PARAM *raw_bytes;
171
0
    unsigned char *data;
172
0
    size_t len;
173
174
0
    raw_bytes = OSSL_PARAM_locate_const(params, OSSL_SKEY_PARAM_RAW_BYTES);
175
0
    if (raw_bytes == NULL)
176
0
        return 0;
177
178
0
    if (!OSSL_PARAM_get_octet_string_ptr(raw_bytes, (const void **)&data, &len))
179
0
        return 0;
180
181
0
    *ckey->param = OSSL_PARAM_construct_octet_string(ckey->name, data, len);
182
0
    return 1;
183
0
}
184
185
int EVP_KDF_CTX_set_SKEY(EVP_KDF_CTX *ctx, EVP_SKEY *key, const char *paramname)
186
0
{
187
0
    struct convert_key ckey;
188
0
    OSSL_PARAM params[2] = {
189
0
        OSSL_PARAM_END,
190
0
        OSSL_PARAM_END,
191
0
    };
192
193
0
    if (ctx == NULL)
194
0
        return 0;
195
196
0
    ckey.name = (paramname != NULL) ? paramname : OSSL_KDF_PARAM_KEY;
197
198
0
    if (ctx->meth->set_skey != NULL && ctx->meth->prov == key->skeymgmt->prov)
199
0
        return ctx->meth->set_skey(ctx->algctx, key->keydata, ckey.name);
200
201
    /*
202
     * We can't use the opaque key directly.
203
     * Let's try to export it and set the ctx params in a traditional manner.
204
     */
205
0
    ckey.param = &params[0];
206
207
0
    if (!ctx->meth->set_ctx_params)
208
0
        return 0;
209
210
0
    if (EVP_SKEY_export(key, OSSL_SKEYMGMT_SELECT_SECRET_KEY,
211
0
            convert_key_cb, &ckey))
212
0
        return ctx->meth->set_ctx_params(ctx->algctx, params);
213
214
0
    return 0;
215
0
}
216
217
EVP_SKEY *EVP_KDF_derive_SKEY(EVP_KDF_CTX *ctx, EVP_SKEYMGMT *mgmt,
218
    const char *key_type, const char *propquery,
219
    size_t keylen, const OSSL_PARAM params[])
220
0
{
221
0
    EVP_SKEYMGMT *skeymgmt = NULL;
222
0
    EVP_SKEY *ret = NULL;
223
224
0
    if (ctx == NULL || key_type == NULL) {
225
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
226
0
        return NULL;
227
0
    }
228
229
0
    if (mgmt != NULL) {
230
0
        skeymgmt = mgmt;
231
0
    } else {
232
0
        skeymgmt = evp_skeymgmt_fetch_from_prov(ctx->meth->prov,
233
0
            key_type, propquery);
234
0
        if (skeymgmt == NULL) {
235
            /*
236
             * The provider does not support skeymgmt, let's try to fallback
237
             * to a provider that supports it
238
             */
239
0
            skeymgmt = EVP_SKEYMGMT_fetch(ossl_provider_libctx(ctx->meth->prov),
240
0
                key_type, propquery);
241
0
        }
242
243
0
        if (skeymgmt == NULL) {
244
0
            ERR_raise(ERR_LIB_EVP, ERR_R_FETCH_FAILED);
245
0
            return NULL;
246
0
        }
247
0
    }
248
249
    /* Fallback to raw derive + import if necessary */
250
0
    if (skeymgmt->prov != ctx->meth->prov || ctx->meth->derive_skey == NULL) {
251
0
        unsigned char *key = NULL;
252
0
        OSSL_PARAM import_params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
253
254
0
        if (ctx->meth->derive == NULL) {
255
0
            ERR_raise(ERR_R_EVP_LIB, ERR_R_UNSUPPORTED);
256
0
            return NULL;
257
0
        }
258
259
0
        key = OPENSSL_zalloc(keylen);
260
0
        if (!key)
261
0
            return NULL;
262
263
0
        if (!ctx->meth->derive(ctx->algctx, key, keylen, params)) {
264
0
            OPENSSL_free(key);
265
0
            return NULL;
266
0
        }
267
0
        import_params[0] = OSSL_PARAM_construct_octet_string(OSSL_SKEY_PARAM_RAW_BYTES,
268
0
            key, keylen);
269
270
0
        ret = EVP_SKEY_import_SKEYMGMT(ossl_provider_libctx(ctx->meth->prov), skeymgmt,
271
0
            OSSL_SKEYMGMT_SELECT_SECRET_KEY, import_params);
272
273
0
        if (mgmt != skeymgmt)
274
0
            EVP_SKEYMGMT_free(skeymgmt);
275
276
0
        OPENSSL_clear_free(key, keylen);
277
0
        return ret;
278
0
    }
279
280
0
    ret = evp_skey_alloc(skeymgmt);
281
0
    if (ret == NULL) {
282
0
        if (mgmt != skeymgmt)
283
0
            EVP_SKEYMGMT_free(skeymgmt);
284
0
        return NULL;
285
0
    }
286
287
0
    ret->keydata = ctx->meth->derive_skey(ctx->algctx, key_type, ossl_provider_ctx(skeymgmt->prov),
288
0
        skeymgmt->import, keylen, params);
289
0
    if (ret->keydata == NULL) {
290
0
        EVP_SKEY_free(ret);
291
0
        ret = NULL;
292
0
    }
293
294
0
    if (mgmt != skeymgmt)
295
0
        EVP_SKEYMGMT_free(skeymgmt);
296
0
    return ret;
297
0
}
298
299
/*
300
 * The {get,set}_params functions return 1 if there is no corresponding
301
 * function in the implementation.  This is the same as if there was one,
302
 * but it didn't recognise any of the given params, i.e. nothing in the
303
 * bag of parameters was useful.
304
 */
305
int EVP_KDF_get_params(EVP_KDF *kdf, OSSL_PARAM params[])
306
0
{
307
0
    if (kdf->get_params != NULL)
308
0
        return kdf->get_params(params);
309
0
    return 1;
310
0
}
311
312
int EVP_KDF_CTX_get_params(EVP_KDF_CTX *ctx, OSSL_PARAM params[])
313
0
{
314
0
    if (ctx->meth->get_ctx_params != NULL)
315
0
        return ctx->meth->get_ctx_params(ctx->algctx, params);
316
0
    return 1;
317
0
}
318
319
int EVP_KDF_CTX_set_params(EVP_KDF_CTX *ctx, const OSSL_PARAM params[])
320
0
{
321
0
    if (ctx->meth->set_ctx_params != NULL)
322
0
        return ctx->meth->set_ctx_params(ctx->algctx, params);
323
0
    return 1;
324
0
}
325
326
int EVP_KDF_names_do_all(const EVP_KDF *kdf,
327
    void (*fn)(const char *name, void *data),
328
    void *data)
329
0
{
330
0
    if (kdf->prov != NULL)
331
0
        return evp_names_do_all(kdf->prov, kdf->name_id, fn, data);
332
333
0
    return 1;
334
0
}