Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/evp/kem.c
Line
Count
Source
1
/*
2
 * Copyright 2020-2022 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 <stdio.h>
11
#include <stdlib.h>
12
#include <openssl/objects.h>
13
#include <openssl/evp.h>
14
#include "internal/cryptlib.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_kem_init(EVP_PKEY_CTX *ctx, int operation,
21
    const OSSL_PARAM params[])
22
{
23
    int ret = 0;
24
    EVP_KEM *kem = NULL;
25
    EVP_KEYMGMT *tmp_keymgmt = NULL;
26
    const OSSL_PROVIDER *tmp_prov = NULL;
27
    void *provkey = NULL;
28
    const char *supported_kem = NULL;
29
    int iter;
30
31
    if (ctx == NULL || ctx->keytype == NULL) {
32
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
33
        return 0;
34
    }
35
36
    evp_pkey_ctx_free_old_ops(ctx);
37
    ctx->operation = operation;
38
39
    if (ctx->pkey == NULL) {
40
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
41
        goto err;
42
    }
43
44
    /*
45
     * Try to derive the supported kem from |ctx->keymgmt|.
46
     */
47
    if (!ossl_assert(ctx->pkey->keymgmt == NULL
48
            || ctx->pkey->keymgmt == ctx->keymgmt)) {
49
        ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
50
        goto err;
51
    }
52
    supported_kem = evp_keymgmt_util_query_operation_name(ctx->keymgmt,
53
        OSSL_OP_KEM);
54
    if (supported_kem == NULL) {
55
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
56
        goto err;
57
    }
58
59
    /*
60
     * Because we cleared out old ops, we shouldn't need to worry about
61
     * checking if kem is already there.
62
     * We perform two iterations:
63
     *
64
     * 1.  Do the normal kem fetch, using the fetching data given by
65
     *     the EVP_PKEY_CTX.
66
     * 2.  Do the provider specific kem fetch, from the same provider
67
     *     as |ctx->keymgmt|
68
     *
69
     * We then try to fetch the keymgmt from the same provider as the
70
     * kem, and try to export |ctx->pkey| to that keymgmt (when this
71
     * keymgmt happens to be the same as |ctx->keymgmt|, the export is
72
     * a no-op, but we call it anyway to not complicate the code even
73
     * more).
74
     * If the export call succeeds (returns a non-NULL provider key pointer),
75
     * we're done and can perform the operation itself.  If not, we perform
76
     * the second iteration, or jump to legacy.
77
     */
78
    for (iter = 1, provkey = NULL; iter < 3 && provkey == NULL; iter++) {
79
        EVP_KEYMGMT *tmp_keymgmt_tofree = NULL;
80
81
        /*
82
         * If we're on the second iteration, free the results from the first.
83
         * They are NULL on the first iteration, so no need to check what
84
         * iteration we're on.
85
         */
86
        EVP_KEM_free(kem);
87
        EVP_KEYMGMT_free(tmp_keymgmt);
88
89
        switch (iter) {
90
        case 1:
91
            kem = EVP_KEM_fetch(ctx->libctx, supported_kem, ctx->propquery);
92
            if (kem != NULL)
93
                tmp_prov = EVP_KEM_get0_provider(kem);
94
            break;
95
        case 2:
96
            tmp_prov = EVP_KEYMGMT_get0_provider(ctx->keymgmt);
97
            kem = evp_kem_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
98
                supported_kem, ctx->propquery);
99
100
            if (kem == NULL) {
101
                ERR_raise(ERR_LIB_EVP,
102
                    EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
103
                ret = -2;
104
                goto err;
105
            }
106
        }
107
        if (kem == NULL)
108
            continue;
109
110
        /*
111
         * Ensure that the key is provided, either natively, or as a cached
112
         * export.  We start by fetching the keymgmt with the same name as
113
         * |ctx->pkey|, but from the provider of the kem method, using the
114
         * same property query as when fetching the kem method.
115
         * With the keymgmt we found (if we did), we try to export |ctx->pkey|
116
         * to it (evp_pkey_export_to_provider() is smart enough to only actually
117
118
         * export it if |tmp_keymgmt| is different from |ctx->pkey|'s keymgmt)
119
         */
120
        tmp_keymgmt_tofree = tmp_keymgmt = evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
121
            EVP_KEYMGMT_get0_name(ctx->keymgmt),
122
            ctx->propquery);
123
        if (tmp_keymgmt != NULL)
124
            provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
125
                &tmp_keymgmt, ctx->propquery);
126
        if (tmp_keymgmt == NULL)
127
            EVP_KEYMGMT_free(tmp_keymgmt_tofree);
128
    }
129
130
    if (provkey == NULL) {
131
        EVP_KEM_free(kem);
132
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
133
        goto err;
134
    }
135
136
    ctx->op.encap.kem = kem;
137
    ctx->op.encap.algctx = kem->newctx(ossl_provider_ctx(kem->prov));
138
    if (ctx->op.encap.algctx == NULL) {
139
        /* The provider key can stay in the cache */
140
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
141
        goto err;
142
    }
143
144
    switch (operation) {
145
    case EVP_PKEY_OP_ENCAPSULATE:
146
        if (kem->encapsulate_init == NULL) {
147
            ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
148
            ret = -2;
149
            goto err;
150
        }
151
        ret = kem->encapsulate_init(ctx->op.encap.algctx, provkey, params);
152
        break;
153
    case EVP_PKEY_OP_DECAPSULATE:
154
        if (kem->decapsulate_init == NULL) {
155
            ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
156
            ret = -2;
157
            goto err;
158
        }
159
        ret = kem->decapsulate_init(ctx->op.encap.algctx, provkey, params);
160
        break;
161
    default:
162
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
163
        goto err;
164
    }
165
166
    EVP_KEYMGMT_free(tmp_keymgmt);
167
    tmp_keymgmt = NULL;
168
169
    if (ret > 0)
170
        return 1;
171
err:
172
    if (ret <= 0) {
173
        evp_pkey_ctx_free_old_ops(ctx);
174
        ctx->operation = EVP_PKEY_OP_UNDEFINED;
175
    }
176
    EVP_KEYMGMT_free(tmp_keymgmt);
177
    return ret;
178
}
179
180
int EVP_PKEY_encapsulate_init(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
181
156
{
182
156
    return evp_kem_init(ctx, EVP_PKEY_OP_ENCAPSULATE, params);
183
156
}
184
185
int EVP_PKEY_encapsulate(EVP_PKEY_CTX *ctx,
186
    unsigned char *out, size_t *outlen,
187
    unsigned char *secret, size_t *secretlen)
188
184
{
189
184
    if (ctx == NULL)
190
0
        return 0;
191
192
184
    if (ctx->operation != EVP_PKEY_OP_ENCAPSULATE) {
193
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
194
0
        return -1;
195
0
    }
196
197
184
    if (ctx->op.encap.algctx == NULL) {
198
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
199
0
        return -2;
200
0
    }
201
202
184
    if (out != NULL && secret == NULL)
203
0
        return 0;
204
205
184
    return ctx->op.encap.kem->encapsulate(ctx->op.encap.algctx,
206
184
        out, outlen, secret, secretlen);
207
184
}
208
209
int EVP_PKEY_decapsulate_init(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
210
100
{
211
100
    return evp_kem_init(ctx, EVP_PKEY_OP_DECAPSULATE, params);
212
100
}
213
214
int EVP_PKEY_decapsulate(EVP_PKEY_CTX *ctx,
215
    unsigned char *secret, size_t *secretlen,
216
    const unsigned char *in, size_t inlen)
217
100
{
218
100
    if (ctx == NULL
219
100
        || (in == NULL || inlen == 0)
220
100
        || (secret == NULL && secretlen == NULL))
221
0
        return 0;
222
223
100
    if (ctx->operation != EVP_PKEY_OP_DECAPSULATE) {
224
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
225
0
        return -1;
226
0
    }
227
228
100
    if (ctx->op.encap.algctx == NULL) {
229
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
230
0
        return -2;
231
0
    }
232
100
    return ctx->op.encap.kem->decapsulate(ctx->op.encap.algctx,
233
100
        secret, secretlen, in, inlen);
234
100
}
235
236
static EVP_KEM *evp_kem_new(OSSL_PROVIDER *prov)
237
{
238
    EVP_KEM *kem = OPENSSL_zalloc(sizeof(EVP_KEM));
239
240
    if (kem == NULL) {
241
        ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
242
        return NULL;
243
    }
244
245
    kem->lock = CRYPTO_THREAD_lock_new();
246
    if (kem->lock == NULL) {
247
        ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
248
        OPENSSL_free(kem);
249
        return NULL;
250
    }
251
    kem->prov = prov;
252
    ossl_provider_up_ref(prov);
253
    kem->refcnt = 1;
254
255
    return kem;
256
}
257
258
static void *evp_kem_from_algorithm(int name_id, const OSSL_ALGORITHM *algodef,
259
    OSSL_PROVIDER *prov)
260
{
261
    const OSSL_DISPATCH *fns = algodef->implementation;
262
    EVP_KEM *kem = NULL;
263
    int ctxfncnt = 0, encfncnt = 0, decfncnt = 0;
264
    int gparamfncnt = 0, sparamfncnt = 0;
265
266
    if ((kem = evp_kem_new(prov)) == NULL) {
267
        ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
268
        goto err;
269
    }
270
271
    kem->name_id = name_id;
272
    if ((kem->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL)
273
        goto err;
274
    kem->description = algodef->algorithm_description;
275
276
    for (; fns->function_id != 0; fns++) {
277
        switch (fns->function_id) {
278
        case OSSL_FUNC_KEM_NEWCTX:
279
            if (kem->newctx != NULL)
280
                break;
281
            kem->newctx = OSSL_FUNC_kem_newctx(fns);
282
            ctxfncnt++;
283
            break;
284
        case OSSL_FUNC_KEM_ENCAPSULATE_INIT:
285
            if (kem->encapsulate_init != NULL)
286
                break;
287
            kem->encapsulate_init = OSSL_FUNC_kem_encapsulate_init(fns);
288
            encfncnt++;
289
            break;
290
        case OSSL_FUNC_KEM_ENCAPSULATE:
291
            if (kem->encapsulate != NULL)
292
                break;
293
            kem->encapsulate = OSSL_FUNC_kem_encapsulate(fns);
294
            encfncnt++;
295
            break;
296
        case OSSL_FUNC_KEM_DECAPSULATE_INIT:
297
            if (kem->decapsulate_init != NULL)
298
                break;
299
            kem->decapsulate_init = OSSL_FUNC_kem_decapsulate_init(fns);
300
            decfncnt++;
301
            break;
302
        case OSSL_FUNC_KEM_DECAPSULATE:
303
            if (kem->decapsulate != NULL)
304
                break;
305
            kem->decapsulate = OSSL_FUNC_kem_decapsulate(fns);
306
            decfncnt++;
307
            break;
308
        case OSSL_FUNC_KEM_FREECTX:
309
            if (kem->freectx != NULL)
310
                break;
311
            kem->freectx = OSSL_FUNC_kem_freectx(fns);
312
            ctxfncnt++;
313
            break;
314
        case OSSL_FUNC_KEM_DUPCTX:
315
            if (kem->dupctx != NULL)
316
                break;
317
            kem->dupctx = OSSL_FUNC_kem_dupctx(fns);
318
            break;
319
        case OSSL_FUNC_KEM_GET_CTX_PARAMS:
320
            if (kem->get_ctx_params != NULL)
321
                break;
322
            kem->get_ctx_params
323
                = OSSL_FUNC_kem_get_ctx_params(fns);
324
            gparamfncnt++;
325
            break;
326
        case OSSL_FUNC_KEM_GETTABLE_CTX_PARAMS:
327
            if (kem->gettable_ctx_params != NULL)
328
                break;
329
            kem->gettable_ctx_params
330
                = OSSL_FUNC_kem_gettable_ctx_params(fns);
331
            gparamfncnt++;
332
            break;
333
        case OSSL_FUNC_KEM_SET_CTX_PARAMS:
334
            if (kem->set_ctx_params != NULL)
335
                break;
336
            kem->set_ctx_params
337
                = OSSL_FUNC_kem_set_ctx_params(fns);
338
            sparamfncnt++;
339
            break;
340
        case OSSL_FUNC_KEM_SETTABLE_CTX_PARAMS:
341
            if (kem->settable_ctx_params != NULL)
342
                break;
343
            kem->settable_ctx_params
344
                = OSSL_FUNC_kem_settable_ctx_params(fns);
345
            sparamfncnt++;
346
            break;
347
        }
348
    }
349
    if (ctxfncnt != 2
350
        || (encfncnt != 0 && encfncnt != 2)
351
        || (decfncnt != 0 && decfncnt != 2)
352
        || (encfncnt != 2 && decfncnt != 2)
353
        || (gparamfncnt != 0 && gparamfncnt != 2)
354
        || (sparamfncnt != 0 && sparamfncnt != 2)) {
355
        /*
356
         * In order to be a consistent set of functions we must have at least
357
         * a set of context functions (newctx and freectx) as well as a pair of
358
         * "kem" functions: (encapsulate_init, encapsulate) or
359
         * (decapsulate_init, decapsulate). set_ctx_params and settable_ctx_params are
360
         * optional, but if one of them is present then the other one must also
361
         * be present. The same applies to get_ctx_params and
362
         * gettable_ctx_params. The dupctx function is optional.
363
         */
364
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
365
        goto err;
366
    }
367
368
    return kem;
369
err:
370
    EVP_KEM_free(kem);
371
    return NULL;
372
}
373
374
void EVP_KEM_free(EVP_KEM *kem)
375
658
{
376
658
    int i;
377
378
658
    if (kem == NULL)
379
256
        return;
380
381
402
    CRYPTO_DOWN_REF(&kem->refcnt, &i, kem->lock);
382
402
    if (i > 0)
383
380
        return;
384
22
    OPENSSL_free(kem->type_name);
385
22
    ossl_provider_free(kem->prov);
386
22
    CRYPTO_THREAD_lock_free(kem->lock);
387
22
    OPENSSL_free(kem);
388
22
}
389
390
int EVP_KEM_up_ref(EVP_KEM *kem)
391
459
{
392
459
    int ref = 0;
393
394
459
    CRYPTO_UP_REF(&kem->refcnt, &ref, kem->lock);
395
459
    return 1;
396
459
}
397
398
OSSL_PROVIDER *EVP_KEM_get0_provider(const EVP_KEM *kem)
399
348
{
400
348
    return kem->prov;
401
348
}
402
403
EVP_KEM *EVP_KEM_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
404
    const char *properties)
405
256
{
406
256
    return evp_generic_fetch(ctx, OSSL_OP_KEM, algorithm, properties,
407
256
        evp_kem_from_algorithm,
408
256
        (int (*)(void *))EVP_KEM_up_ref,
409
256
        (void (*)(void *))EVP_KEM_free);
410
256
}
411
412
EVP_KEM *evp_kem_fetch_from_prov(OSSL_PROVIDER *prov, const char *algorithm,
413
    const char *properties)
414
0
{
415
0
    return evp_generic_fetch_from_prov(prov, OSSL_OP_KEM, algorithm, properties,
416
0
        evp_kem_from_algorithm,
417
0
        (int (*)(void *))EVP_KEM_up_ref,
418
0
        (void (*)(void *))EVP_KEM_free);
419
0
}
420
421
int EVP_KEM_is_a(const EVP_KEM *kem, const char *name)
422
0
{
423
0
    return kem != NULL && evp_is_a(kem->prov, kem->name_id, NULL, name);
424
0
}
425
426
int evp_kem_get_number(const EVP_KEM *kem)
427
0
{
428
0
    return kem->name_id;
429
0
}
430
431
const char *EVP_KEM_get0_name(const EVP_KEM *kem)
432
0
{
433
0
    return kem->type_name;
434
0
}
435
436
const char *EVP_KEM_get0_description(const EVP_KEM *kem)
437
0
{
438
0
    return kem->description;
439
0
}
440
441
void EVP_KEM_do_all_provided(OSSL_LIB_CTX *libctx,
442
    void (*fn)(EVP_KEM *kem, void *arg),
443
    void *arg)
444
8
{
445
8
    evp_generic_do_all(libctx, OSSL_OP_KEM, (void (*)(void *, void *))fn, arg,
446
8
        evp_kem_from_algorithm,
447
8
        (int (*)(void *))EVP_KEM_up_ref,
448
8
        (void (*)(void *))EVP_KEM_free);
449
8
}
450
451
int EVP_KEM_names_do_all(const EVP_KEM *kem,
452
    void (*fn)(const char *name, void *data),
453
    void *data)
454
0
{
455
0
    if (kem->prov != NULL)
456
0
        return evp_names_do_all(kem->prov, kem->name_id, fn, data);
457
458
0
    return 1;
459
0
}
460
461
const OSSL_PARAM *EVP_KEM_gettable_ctx_params(const EVP_KEM *kem)
462
0
{
463
0
    void *provctx;
464
465
0
    if (kem == NULL || kem->gettable_ctx_params == NULL)
466
0
        return NULL;
467
468
0
    provctx = ossl_provider_ctx(EVP_KEM_get0_provider(kem));
469
0
    return kem->gettable_ctx_params(NULL, provctx);
470
0
}
471
472
const OSSL_PARAM *EVP_KEM_settable_ctx_params(const EVP_KEM *kem)
473
52
{
474
52
    void *provctx;
475
476
52
    if (kem == NULL || kem->settable_ctx_params == NULL)
477
0
        return NULL;
478
479
52
    provctx = ossl_provider_ctx(EVP_KEM_get0_provider(kem));
480
    return kem->settable_ctx_params(NULL, provctx);
481
52
}