Coverage Report

Created: 2026-07-12 07:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/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
117
{
22
117
    EVP_KEM_free(data);
23
117
}
24
25
static int evp_kem_up_ref(void *data)
26
326
{
27
326
    return EVP_KEM_up_ref(data);
28
326
}
29
30
static int evp_kem_init(EVP_PKEY_CTX *ctx, int operation,
31
    const OSSL_PARAM params[], EVP_PKEY *authkey)
32
318
{
33
318
    int ret = 0;
34
318
    EVP_KEM *kem = NULL;
35
318
    EVP_KEYMGMT *tmp_keymgmt = NULL;
36
318
    const OSSL_PROVIDER *tmp_prov = NULL;
37
318
    void *provkey = NULL, *provauthkey = NULL;
38
318
    const char *supported_kem = NULL;
39
318
    int iter;
40
41
318
    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
318
    evp_pkey_ctx_free_old_ops(ctx);
47
318
    ctx->operation = operation;
48
49
318
    if (ctx->pkey == NULL) {
50
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
51
0
        goto err;
52
0
    }
53
318
    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
318
    if (!ossl_assert(ctx->pkey->keymgmt == NULL
61
318
            || ctx->pkey->keymgmt == ctx->keymgmt)) {
62
0
        ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
63
0
        goto err;
64
0
    }
65
318
    supported_kem = evp_keymgmt_util_query_operation_name(ctx->keymgmt,
66
318
        OSSL_OP_KEM);
67
318
    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
636
    for (iter = 1, provkey = NULL; iter < 3 && provkey == NULL; iter++) {
92
318
        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
318
        EVP_KEM_free(kem);
100
318
        kem = NULL;
101
318
        EVP_KEYMGMT_free(tmp_keymgmt);
102
318
        tmp_keymgmt = NULL;
103
104
318
        switch (iter) {
105
318
        case 1:
106
318
            kem = EVP_KEM_fetch(ctx->libctx, supported_kem, ctx->propquery);
107
318
            if (kem != NULL)
108
318
                tmp_prov = EVP_KEM_get0_provider(kem);
109
318
            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
318
        }
122
318
        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
318
        tmp_keymgmt_tofree = tmp_keymgmt = evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
135
318
            EVP_KEYMGMT_get0_name(ctx->keymgmt),
136
318
            ctx->propquery);
137
318
        if (tmp_keymgmt != NULL) {
138
318
            provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
139
318
                &tmp_keymgmt, ctx->propquery);
140
318
            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
318
        }
151
318
        if (tmp_keymgmt == NULL)
152
0
            EVP_KEYMGMT_free(tmp_keymgmt_tofree);
153
318
    }
154
155
318
    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
318
    ctx->op.encap.kem = kem;
162
318
    ctx->op.encap.algctx = kem->newctx(ossl_provider_ctx(kem->prov));
163
318
    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
318
    switch (operation) {
170
182
    case EVP_PKEY_OP_ENCAPSULATE:
171
182
        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
182
        } else if (provauthkey == NULL && kem->encapsulate_init != NULL) {
175
182
            ret = kem->encapsulate_init(ctx->op.encap.algctx, provkey, params);
176
182
        } 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
182
        break;
182
182
    case EVP_PKEY_OP_DECAPSULATE:
183
136
        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
136
        } else if (provauthkey == NULL && kem->decapsulate_init != NULL) {
187
136
            ret = kem->decapsulate_init(ctx->op.encap.algctx, provkey, params);
188
136
        } 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
136
        break;
194
136
    default:
195
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
196
0
        goto err;
197
318
    }
198
199
318
    EVP_KEYMGMT_free(tmp_keymgmt);
200
318
    tmp_keymgmt = NULL;
201
202
318
    if (ret > 0)
203
318
        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
318
}
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
182
{
223
182
    return evp_kem_init(ctx, EVP_PKEY_OP_ENCAPSULATE, params, NULL);
224
182
}
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
206
{
230
206
    if (ctx == NULL)
231
0
        return 0;
232
233
206
    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
206
    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
206
    if (out != NULL && secret == NULL)
244
0
        return 0;
245
246
206
    return ctx->op.encap.kem->encapsulate(ctx->op.encap.algctx,
247
206
        out, outlen, secret, secretlen);
248
206
}
249
250
int EVP_PKEY_decapsulate_init(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
251
136
{
252
136
    return evp_kem_init(ctx, EVP_PKEY_OP_DECAPSULATE, params, NULL);
253
136
}
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
138
{
267
138
    if (ctx == NULL
268
138
        || (in == NULL || inlen == 0)
269
138
        || (secret == NULL && secretlen == NULL))
270
0
        return 0;
271
272
138
    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
138
    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
138
    return ctx->op.encap.kem->decapsulate(ctx->op.encap.algctx,
282
138
        secret, secretlen, in, inlen);
283
138
}
284
285
static EVP_KEM *evp_kem_new(OSSL_PROVIDER *prov)
286
125
{
287
125
    EVP_KEM *kem = OPENSSL_zalloc(sizeof(EVP_KEM));
288
289
125
    if (kem == NULL)
290
0
        return NULL;
291
292
125
    if (!CRYPTO_NEW_REF(&kem->refcnt, 1)
293
125
        || !ossl_provider_up_ref(prov)) {
294
0
        CRYPTO_FREE_REF(&kem->refcnt);
295
0
        OPENSSL_free(kem);
296
0
        return NULL;
297
0
    }
298
125
    kem->prov = prov;
299
300
125
    return kem;
301
125
}
302
303
static void *evp_kem_from_algorithm(int name_id, const OSSL_ALGORITHM *algodef,
304
    OSSL_PROVIDER *prov)
305
133
{
306
133
    const OSSL_DISPATCH *fns = algodef->implementation;
307
133
    EVP_KEM *kem = NULL;
308
133
    int ctxfncnt = 0, encfncnt = 0, decfncnt = 0;
309
133
    int gparamfncnt = 0, sparamfncnt = 0;
310
311
133
    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
133
    kem->name_id = name_id;
317
133
    if ((kem->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL)
318
0
        goto err;
319
133
    kem->description = algodef->algorithm_description;
320
321
1.31k
    for (; fns->function_id != 0; fns++) {
322
1.18k
        switch (fns->function_id) {
323
133
        case OSSL_FUNC_KEM_NEWCTX:
324
133
            if (kem->newctx != NULL)
325
0
                break;
326
133
            kem->newctx = OSSL_FUNC_kem_newctx(fns);
327
133
            ctxfncnt++;
328
133
            break;
329
133
        case OSSL_FUNC_KEM_ENCAPSULATE_INIT:
330
133
            if (kem->encapsulate_init != NULL)
331
0
                break;
332
133
            kem->encapsulate_init = OSSL_FUNC_kem_encapsulate_init(fns);
333
133
            encfncnt++;
334
133
            break;
335
39
        case OSSL_FUNC_KEM_AUTH_ENCAPSULATE_INIT:
336
39
            if (kem->auth_encapsulate_init != NULL)
337
0
                break;
338
39
            kem->auth_encapsulate_init = OSSL_FUNC_kem_auth_encapsulate_init(fns);
339
39
            encfncnt++;
340
39
            break;
341
133
        case OSSL_FUNC_KEM_ENCAPSULATE:
342
133
            if (kem->encapsulate != NULL)
343
0
                break;
344
133
            kem->encapsulate = OSSL_FUNC_kem_encapsulate(fns);
345
133
            encfncnt++;
346
133
            break;
347
133
        case OSSL_FUNC_KEM_DECAPSULATE_INIT:
348
133
            if (kem->decapsulate_init != NULL)
349
0
                break;
350
133
            kem->decapsulate_init = OSSL_FUNC_kem_decapsulate_init(fns);
351
133
            decfncnt++;
352
133
            break;
353
39
        case OSSL_FUNC_KEM_AUTH_DECAPSULATE_INIT:
354
39
            if (kem->auth_decapsulate_init != NULL)
355
0
                break;
356
39
            kem->auth_decapsulate_init = OSSL_FUNC_kem_auth_decapsulate_init(fns);
357
39
            decfncnt++;
358
39
            break;
359
133
        case OSSL_FUNC_KEM_DECAPSULATE:
360
133
            if (kem->decapsulate != NULL)
361
0
                break;
362
133
            kem->decapsulate = OSSL_FUNC_kem_decapsulate(fns);
363
133
            decfncnt++;
364
133
            break;
365
133
        case OSSL_FUNC_KEM_FREECTX:
366
133
            if (kem->freectx != NULL)
367
0
                break;
368
133
            kem->freectx = OSSL_FUNC_kem_freectx(fns);
369
133
            ctxfncnt++;
370
133
            break;
371
13
        case OSSL_FUNC_KEM_DUPCTX:
372
13
            if (kem->dupctx != NULL)
373
0
                break;
374
13
            kem->dupctx = OSSL_FUNC_kem_dupctx(fns);
375
13
            break;
376
13
        case OSSL_FUNC_KEM_GET_CTX_PARAMS:
377
13
            if (kem->get_ctx_params != NULL)
378
0
                break;
379
13
            kem->get_ctx_params
380
13
                = OSSL_FUNC_kem_get_ctx_params(fns);
381
13
            gparamfncnt++;
382
13
            break;
383
13
        case OSSL_FUNC_KEM_GETTABLE_CTX_PARAMS:
384
13
            if (kem->gettable_ctx_params != NULL)
385
0
                break;
386
13
            kem->gettable_ctx_params
387
13
                = OSSL_FUNC_kem_gettable_ctx_params(fns);
388
13
            gparamfncnt++;
389
13
            break;
390
133
        case OSSL_FUNC_KEM_SET_CTX_PARAMS:
391
133
            if (kem->set_ctx_params != NULL)
392
0
                break;
393
133
            kem->set_ctx_params
394
133
                = OSSL_FUNC_kem_set_ctx_params(fns);
395
133
            sparamfncnt++;
396
133
            break;
397
133
        case OSSL_FUNC_KEM_SETTABLE_CTX_PARAMS:
398
133
            if (kem->settable_ctx_params != NULL)
399
0
                break;
400
133
            kem->settable_ctx_params
401
133
                = OSSL_FUNC_kem_settable_ctx_params(fns);
402
133
            sparamfncnt++;
403
133
            break;
404
1.18k
        }
405
1.18k
    }
406
133
    if (ctxfncnt != 2
407
133
        || (encfncnt != 0 && encfncnt != 2 && encfncnt != 3)
408
133
        || (decfncnt != 0 && decfncnt != 2 && decfncnt != 3)
409
133
        || (encfncnt != decfncnt)
410
133
        || (gparamfncnt != 0 && gparamfncnt != 2)
411
133
        || (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
133
    return kem;
428
0
err:
429
0
    EVP_KEM_free(kem);
430
0
    return NULL;
431
133
}
432
433
void EVP_KEM_free(EVP_KEM *kem)
434
493
{
435
493
    int i;
436
437
493
    if (kem == NULL)
438
184
        return;
439
440
309
    CRYPTO_DOWN_REF(&kem->refcnt, &i);
441
309
    if (i > 0)
442
276
        return;
443
33
    OPENSSL_free(kem->type_name);
444
33
    ossl_provider_free(kem->prov);
445
33
    CRYPTO_FREE_REF(&kem->refcnt);
446
33
    OPENSSL_free(kem);
447
33
}
448
449
int EVP_KEM_up_ref(EVP_KEM *kem)
450
328
{
451
328
    int ref = 0;
452
453
328
    CRYPTO_UP_REF(&kem->refcnt, &ref);
454
328
    return 1;
455
328
}
456
457
OSSL_PROVIDER *EVP_KEM_get0_provider(const EVP_KEM *kem)
458
422
{
459
422
    return kem->prov;
460
422
}
461
462
EVP_KEM *EVP_KEM_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
463
    const char *properties)
464
318
{
465
318
    return evp_generic_fetch(ctx, OSSL_OP_KEM, algorithm, properties,
466
318
        evp_kem_from_algorithm,
467
318
        evp_kem_up_ref,
468
318
        evp_kem_free);
469
318
}
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
8
{
504
8
    evp_generic_do_all(libctx, OSSL_OP_KEM, (void (*)(void *, void *))fn, arg,
505
8
        evp_kem_from_algorithm,
506
8
        evp_kem_up_ref,
507
8
        evp_kem_free);
508
8
}
509
510
int EVP_KEM_names_do_all(const EVP_KEM *kem,
511
    void (*fn)(const char *name, void *data),
512
    void *data)
513
0
{
514
0
    if (kem->prov != NULL)
515
0
        return evp_names_do_all(kem->prov, kem->name_id, fn, data);
516
517
0
    return 1;
518
0
}
519
520
const OSSL_PARAM *EVP_KEM_gettable_ctx_params(const EVP_KEM *kem)
521
0
{
522
0
    void *provctx;
523
524
0
    if (kem == NULL || kem->gettable_ctx_params == NULL)
525
0
        return NULL;
526
527
0
    provctx = ossl_provider_ctx(EVP_KEM_get0_provider(kem));
528
0
    return kem->gettable_ctx_params(NULL, provctx);
529
0
}
530
531
const OSSL_PARAM *EVP_KEM_settable_ctx_params(const EVP_KEM *kem)
532
60
{
533
60
    void *provctx;
534
535
60
    if (kem == NULL || kem->settable_ctx_params == NULL)
536
0
        return NULL;
537
538
60
    provctx = ossl_provider_ctx(EVP_KEM_get0_provider(kem));
539
    return kem->settable_ctx_params(NULL, provctx);
540
60
}