Coverage Report

Created: 2025-08-28 07:07

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