Coverage Report

Created: 2026-09-12 06:55

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
368
{
23
368
    int ret = 0;
24
368
    EVP_KEM *kem = NULL;
25
368
    EVP_KEYMGMT *tmp_keymgmt = NULL;
26
368
    const OSSL_PROVIDER *tmp_prov = NULL;
27
368
    void *provkey = NULL, *provauthkey = NULL;
28
368
    const char *supported_kem = NULL;
29
368
    int iter;
30
31
368
    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
368
    evp_pkey_ctx_free_old_ops(ctx);
37
368
    ctx->operation = operation;
38
39
368
    if (ctx->pkey == NULL) {
40
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
41
0
        goto err;
42
0
    }
43
368
    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
368
    if (!ossl_assert(ctx->pkey->keymgmt == NULL
51
368
            || ctx->pkey->keymgmt == ctx->keymgmt)) {
52
0
        ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
53
0
        goto err;
54
0
    }
55
368
    supported_kem = evp_keymgmt_util_query_operation_name(ctx->keymgmt,
56
368
        OSSL_OP_KEM);
57
368
    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
736
    for (iter = 1, provkey = NULL; iter < 3 && provkey == NULL; iter++) {
82
368
        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
368
        EVP_KEM_free(kem);
90
368
        kem = NULL;
91
368
        EVP_KEYMGMT_free(tmp_keymgmt);
92
368
        tmp_keymgmt = NULL;
93
94
368
        switch (iter) {
95
368
        case 1:
96
368
            kem = EVP_KEM_fetch(ctx->libctx, supported_kem, ctx->propquery);
97
368
            if (kem != NULL)
98
368
                tmp_prov = EVP_KEM_get0_provider(kem);
99
368
            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
368
        }
112
368
        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
368
        tmp_keymgmt_tofree = tmp_keymgmt = evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
125
368
            EVP_KEYMGMT_get0_name(ctx->keymgmt),
126
368
            ctx->propquery);
127
368
        if (tmp_keymgmt != NULL) {
128
368
            provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
129
368
                &tmp_keymgmt, ctx->propquery);
130
368
            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
368
        }
141
368
        if (tmp_keymgmt == NULL)
142
0
            EVP_KEYMGMT_free(tmp_keymgmt_tofree);
143
368
    }
144
145
368
    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
368
    ctx->op.encap.kem = kem;
152
368
    ctx->op.encap.algctx = kem->newctx(ossl_provider_ctx(kem->prov));
153
368
    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
368
    switch (operation) {
160
207
    case EVP_PKEY_OP_ENCAPSULATE:
161
207
        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
207
        } else if (provauthkey == NULL && kem->encapsulate_init != NULL) {
165
207
            ret = kem->encapsulate_init(ctx->op.encap.algctx, provkey, params);
166
207
        } 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
207
        break;
172
207
    case EVP_PKEY_OP_DECAPSULATE:
173
161
        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
161
        } else if (provauthkey == NULL && kem->decapsulate_init != NULL) {
177
161
            ret = kem->decapsulate_init(ctx->op.encap.algctx, provkey, params);
178
161
        } 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
161
        break;
184
161
    default:
185
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
186
0
        goto err;
187
368
    }
188
189
368
    EVP_KEYMGMT_free(tmp_keymgmt);
190
368
    tmp_keymgmt = NULL;
191
192
368
    if (ret > 0)
193
368
        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
368
}
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
207
{
213
207
    return evp_kem_init(ctx, EVP_PKEY_OP_ENCAPSULATE, params, NULL);
214
207
}
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
231
{
220
231
    if (ctx == NULL)
221
0
        return 0;
222
223
231
    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
231
    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
231
    if (out != NULL && secret == NULL)
234
0
        return 0;
235
236
231
    return ctx->op.encap.kem->encapsulate(ctx->op.encap.algctx,
237
231
        out, outlen, secret, secretlen);
238
231
}
239
240
int EVP_PKEY_decapsulate_init(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
241
161
{
242
161
    return evp_kem_init(ctx, EVP_PKEY_OP_DECAPSULATE, params, NULL);
243
161
}
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
163
{
257
163
    if (ctx == NULL
258
163
        || (in == NULL || inlen == 0)
259
163
        || (secret == NULL && secretlen == NULL))
260
0
        return 0;
261
262
163
    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
163
    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
163
    return ctx->op.encap.kem->decapsulate(ctx->op.encap.algctx,
272
163
        secret, secretlen, in, inlen);
273
163
}
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
181
{
295
181
    const OSSL_DISPATCH *fns = algodef->implementation;
296
181
    EVP_KEM *kem = NULL;
297
181
    int ctxfncnt = 0, encfncnt = 0, decfncnt = 0;
298
181
    int gparamfncnt = 0, sparamfncnt = 0;
299
300
181
    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
181
    kem->name_id = name_id;
306
181
    if ((kem->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL)
307
0
        goto err;
308
181
    kem->description = algodef->algorithm_description;
309
310
1.78k
    for (; fns->function_id != 0; fns++) {
311
1.60k
        switch (fns->function_id) {
312
181
        case OSSL_FUNC_KEM_NEWCTX:
313
181
            if (kem->newctx != NULL)
314
0
                break;
315
181
            kem->newctx = OSSL_FUNC_kem_newctx(fns);
316
181
            ctxfncnt++;
317
181
            break;
318
181
        case OSSL_FUNC_KEM_ENCAPSULATE_INIT:
319
181
            if (kem->encapsulate_init != NULL)
320
0
                break;
321
181
            kem->encapsulate_init = OSSL_FUNC_kem_encapsulate_init(fns);
322
181
            encfncnt++;
323
181
            break;
324
51
        case OSSL_FUNC_KEM_AUTH_ENCAPSULATE_INIT:
325
51
            if (kem->auth_encapsulate_init != NULL)
326
0
                break;
327
51
            kem->auth_encapsulate_init = OSSL_FUNC_kem_auth_encapsulate_init(fns);
328
51
            encfncnt++;
329
51
            break;
330
181
        case OSSL_FUNC_KEM_ENCAPSULATE:
331
181
            if (kem->encapsulate != NULL)
332
0
                break;
333
181
            kem->encapsulate = OSSL_FUNC_kem_encapsulate(fns);
334
181
            encfncnt++;
335
181
            break;
336
181
        case OSSL_FUNC_KEM_DECAPSULATE_INIT:
337
181
            if (kem->decapsulate_init != NULL)
338
0
                break;
339
181
            kem->decapsulate_init = OSSL_FUNC_kem_decapsulate_init(fns);
340
181
            decfncnt++;
341
181
            break;
342
51
        case OSSL_FUNC_KEM_AUTH_DECAPSULATE_INIT:
343
51
            if (kem->auth_decapsulate_init != NULL)
344
0
                break;
345
51
            kem->auth_decapsulate_init = OSSL_FUNC_kem_auth_decapsulate_init(fns);
346
51
            decfncnt++;
347
51
            break;
348
181
        case OSSL_FUNC_KEM_DECAPSULATE:
349
181
            if (kem->decapsulate != NULL)
350
0
                break;
351
181
            kem->decapsulate = OSSL_FUNC_kem_decapsulate(fns);
352
181
            decfncnt++;
353
181
            break;
354
181
        case OSSL_FUNC_KEM_FREECTX:
355
181
            if (kem->freectx != NULL)
356
0
                break;
357
181
            kem->freectx = OSSL_FUNC_kem_freectx(fns);
358
181
            ctxfncnt++;
359
181
            break;
360
17
        case OSSL_FUNC_KEM_DUPCTX:
361
17
            if (kem->dupctx != NULL)
362
0
                break;
363
17
            kem->dupctx = OSSL_FUNC_kem_dupctx(fns);
364
17
            break;
365
17
        case OSSL_FUNC_KEM_GET_CTX_PARAMS:
366
17
            if (kem->get_ctx_params != NULL)
367
0
                break;
368
17
            kem->get_ctx_params
369
17
                = OSSL_FUNC_kem_get_ctx_params(fns);
370
17
            gparamfncnt++;
371
17
            break;
372
17
        case OSSL_FUNC_KEM_GETTABLE_CTX_PARAMS:
373
17
            if (kem->gettable_ctx_params != NULL)
374
0
                break;
375
17
            kem->gettable_ctx_params
376
17
                = OSSL_FUNC_kem_gettable_ctx_params(fns);
377
17
            gparamfncnt++;
378
17
            break;
379
181
        case OSSL_FUNC_KEM_SET_CTX_PARAMS:
380
181
            if (kem->set_ctx_params != NULL)
381
0
                break;
382
181
            kem->set_ctx_params
383
181
                = OSSL_FUNC_kem_set_ctx_params(fns);
384
181
            sparamfncnt++;
385
181
            break;
386
181
        case OSSL_FUNC_KEM_SETTABLE_CTX_PARAMS:
387
181
            if (kem->settable_ctx_params != NULL)
388
0
                break;
389
181
            kem->settable_ctx_params
390
181
                = OSSL_FUNC_kem_settable_ctx_params(fns);
391
181
            sparamfncnt++;
392
181
            break;
393
1.60k
        }
394
1.60k
    }
395
181
    if (ctxfncnt != 2
396
181
        || (encfncnt != 0 && encfncnt != 2 && encfncnt != 3)
397
181
        || (decfncnt != 0 && decfncnt != 2 && decfncnt != 3)
398
181
        || (encfncnt != decfncnt)
399
181
        || (gparamfncnt != 0 && gparamfncnt != 2)
400
181
        || (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
181
    return kem;
417
0
err:
418
0
    EVP_KEM_free(kem);
419
0
    return NULL;
420
181
}
421
422
void EVP_KEM_free(EVP_KEM *kem)
423
621
{
424
621
    int i;
425
426
621
    if (kem == NULL)
427
230
        return;
428
429
391
    CRYPTO_DOWN_REF(&kem->refcnt, &i);
430
391
    if (i > 0)
431
358
        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
437
{
440
437
    int ref = 0;
441
442
437
    CRYPTO_UP_REF(&kem->refcnt, &ref);
443
437
    return 1;
444
437
}
445
446
OSSL_PROVIDER *EVP_KEM_get0_provider(const EVP_KEM *kem)
447
545
{
448
545
    return kem->prov;
449
545
}
450
451
EVP_KEM *EVP_KEM_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
452
    const char *properties)
453
368
{
454
368
    return evp_generic_fetch(ctx, OSSL_OP_KEM, algorithm, properties,
455
368
        evp_kem_from_algorithm,
456
368
        (int (*)(void *))EVP_KEM_up_ref,
457
368
        (void (*)(void *))EVP_KEM_free);
458
368
}
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
11
{
493
11
    evp_generic_do_all(libctx, OSSL_OP_KEM, (void (*)(void *, void *))fn, arg,
494
11
        evp_kem_from_algorithm,
495
11
        (int (*)(void *))EVP_KEM_up_ref,
496
11
        (void (*)(void *))EVP_KEM_free);
497
11
}
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
67
{
522
67
    void *provctx;
523
524
67
    if (kem == NULL || kem->settable_ctx_params == NULL)
525
0
        return NULL;
526
527
67
    provctx = ossl_provider_ctx(EVP_KEM_get0_provider(kem));
528
    return kem->settable_ctx_params(NULL, provctx);
529
67
}