Coverage Report

Created: 2025-12-10 06:24

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