Coverage Report

Created: 2026-06-08 06:07

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