Coverage Report

Created: 2026-07-23 06:28

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