Coverage Report

Created: 2026-10-28 06:28

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