Coverage Report

Created: 2025-06-13 06:57

/src/openssl/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
22
{
22
22
    EVP_KEM_free(data);
23
22
}
24
25
static int evp_kem_up_ref(void *data)
26
22
{
27
22
    return EVP_KEM_up_ref(data);
28
22
}
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 =
133
0
            evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
134
0
                                        EVP_KEYMGMT_get0_name(ctx->keymgmt),
135
0
                                        ctx->propquery);
136
0
        if (tmp_keymgmt != NULL) {
137
0
            provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
138
0
                                                  &tmp_keymgmt, ctx->propquery);
139
0
            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
0
        }
150
0
        if (tmp_keymgmt == NULL)
151
0
            EVP_KEYMGMT_free(tmp_keymgmt_tofree);
152
0
    }
153
154
0
    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
0
    ctx->op.encap.kem = kem;
161
0
    ctx->op.encap.algctx = kem->newctx(ossl_provider_ctx(kem->prov));
162
0
    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
0
    switch (operation) {
169
0
    case EVP_PKEY_OP_ENCAPSULATE:
170
0
        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
0
        } else if (provauthkey == NULL && kem->encapsulate_init != NULL) {
174
0
            ret = kem->encapsulate_init(ctx->op.encap.algctx, provkey, params);
175
0
        } 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
0
        break;
181
0
    case EVP_PKEY_OP_DECAPSULATE:
182
0
        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
0
        } else if (provauthkey == NULL && kem->encapsulate_init != NULL) {
186
0
            ret = kem->decapsulate_init(ctx->op.encap.algctx, provkey, params);
187
0
        } 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
0
        break;
193
0
    default:
194
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
195
0
        goto err;
196
0
    }
197
198
0
    EVP_KEYMGMT_free(tmp_keymgmt);
199
0
    tmp_keymgmt = NULL;
200
201
0
    if (ret > 0)
202
0
        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
0
}
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
0
{
222
0
    return evp_kem_init(ctx, EVP_PKEY_OP_ENCAPSULATE, params, NULL);
223
0
}
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
0
{
229
0
    if (ctx == NULL)
230
0
        return 0;
231
232
0
    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
0
    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
0
    if (out != NULL && secret == NULL)
243
0
        return 0;
244
245
0
    return ctx->op.encap.kem->encapsulate(ctx->op.encap.algctx,
246
0
                                          out, outlen, secret, secretlen);
247
0
}
248
249
int EVP_PKEY_decapsulate_init(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
250
0
{
251
0
    return evp_kem_init(ctx, EVP_PKEY_OP_DECAPSULATE, params, NULL);
252
0
}
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
0
{
266
0
    if (ctx == NULL
267
0
        || (in == NULL || inlen == 0)
268
0
        || (secret == NULL && secretlen == NULL))
269
0
        return 0;
270
271
0
    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
0
    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
0
    return ctx->op.encap.kem->decapsulate(ctx->op.encap.algctx,
281
0
                                          secret, secretlen, in, inlen);
282
0
}
283
284
static EVP_KEM *evp_kem_new(OSSL_PROVIDER *prov)
285
22
{
286
22
    EVP_KEM *kem = OPENSSL_zalloc(sizeof(EVP_KEM));
287
288
22
    if (kem == NULL)
289
0
        return NULL;
290
291
22
    if (!CRYPTO_NEW_REF(&kem->refcnt, 1)
292
22
        || !ossl_provider_up_ref(prov)) {
293
0
        CRYPTO_FREE_REF(&kem->refcnt);
294
0
        OPENSSL_free(kem);
295
0
        return NULL;
296
0
    }
297
22
    kem->prov = prov;
298
299
22
    return kem;
300
22
}
301
302
static void *evp_kem_from_algorithm(int name_id, const OSSL_ALGORITHM *algodef,
303
                                    OSSL_PROVIDER *prov)
304
22
{
305
22
    const OSSL_DISPATCH *fns = algodef->implementation;
306
22
    EVP_KEM *kem = NULL;
307
22
    int ctxfncnt = 0, encfncnt = 0, decfncnt = 0;
308
22
    int gparamfncnt = 0, sparamfncnt = 0;
309
310
22
    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
22
    kem->name_id = name_id;
316
22
    if ((kem->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL)
317
0
        goto err;
318
22
    kem->description = algodef->algorithm_description;
319
320
216
    for (; fns->function_id != 0; fns++) {
321
194
        switch (fns->function_id) {
322
22
        case OSSL_FUNC_KEM_NEWCTX:
323
22
            if (kem->newctx != NULL)
324
0
                break;
325
22
            kem->newctx = OSSL_FUNC_kem_newctx(fns);
326
22
            ctxfncnt++;
327
22
            break;
328
22
        case OSSL_FUNC_KEM_ENCAPSULATE_INIT:
329
22
            if (kem->encapsulate_init != NULL)
330
0
                break;
331
22
            kem->encapsulate_init = OSSL_FUNC_kem_encapsulate_init(fns);
332
22
            encfncnt++;
333
22
            break;
334
6
        case OSSL_FUNC_KEM_AUTH_ENCAPSULATE_INIT:
335
6
            if (kem->auth_encapsulate_init != NULL)
336
0
                break;
337
6
            kem->auth_encapsulate_init = OSSL_FUNC_kem_auth_encapsulate_init(fns);
338
6
            encfncnt++;
339
6
            break;
340
22
        case OSSL_FUNC_KEM_ENCAPSULATE:
341
22
            if (kem->encapsulate != NULL)
342
0
                break;
343
22
            kem->encapsulate = OSSL_FUNC_kem_encapsulate(fns);
344
22
            encfncnt++;
345
22
            break;
346
22
        case OSSL_FUNC_KEM_DECAPSULATE_INIT:
347
22
            if (kem->decapsulate_init != NULL)
348
0
                break;
349
22
            kem->decapsulate_init = OSSL_FUNC_kem_decapsulate_init(fns);
350
22
            decfncnt++;
351
22
            break;
352
6
        case OSSL_FUNC_KEM_AUTH_DECAPSULATE_INIT:
353
6
            if (kem->auth_decapsulate_init != NULL)
354
0
                break;
355
6
            kem->auth_decapsulate_init = OSSL_FUNC_kem_auth_decapsulate_init(fns);
356
6
            decfncnt++;
357
6
            break;
358
22
        case OSSL_FUNC_KEM_DECAPSULATE:
359
22
            if (kem->decapsulate != NULL)
360
0
                break;
361
22
            kem->decapsulate = OSSL_FUNC_kem_decapsulate(fns);
362
22
            decfncnt++;
363
22
            break;
364
22
        case OSSL_FUNC_KEM_FREECTX:
365
22
            if (kem->freectx != NULL)
366
0
                break;
367
22
            kem->freectx = OSSL_FUNC_kem_freectx(fns);
368
22
            ctxfncnt++;
369
22
            break;
370
2
        case OSSL_FUNC_KEM_DUPCTX:
371
2
            if (kem->dupctx != NULL)
372
0
                break;
373
2
            kem->dupctx = OSSL_FUNC_kem_dupctx(fns);
374
2
            break;
375
2
        case OSSL_FUNC_KEM_GET_CTX_PARAMS:
376
2
            if (kem->get_ctx_params != NULL)
377
0
                break;
378
2
            kem->get_ctx_params
379
2
                = OSSL_FUNC_kem_get_ctx_params(fns);
380
2
            gparamfncnt++;
381
2
            break;
382
2
        case OSSL_FUNC_KEM_GETTABLE_CTX_PARAMS:
383
2
            if (kem->gettable_ctx_params != NULL)
384
0
                break;
385
2
            kem->gettable_ctx_params
386
2
                = OSSL_FUNC_kem_gettable_ctx_params(fns);
387
2
            gparamfncnt++;
388
2
            break;
389
22
        case OSSL_FUNC_KEM_SET_CTX_PARAMS:
390
22
            if (kem->set_ctx_params != NULL)
391
0
                break;
392
22
            kem->set_ctx_params
393
22
                = OSSL_FUNC_kem_set_ctx_params(fns);
394
22
            sparamfncnt++;
395
22
            break;
396
22
        case OSSL_FUNC_KEM_SETTABLE_CTX_PARAMS:
397
22
            if (kem->settable_ctx_params != NULL)
398
0
                break;
399
22
            kem->settable_ctx_params
400
22
                = OSSL_FUNC_kem_settable_ctx_params(fns);
401
22
            sparamfncnt++;
402
22
            break;
403
194
        }
404
194
    }
405
22
    if (ctxfncnt != 2
406
22
        || (encfncnt != 0 && encfncnt != 2 && encfncnt != 3)
407
22
        || (decfncnt != 0 && decfncnt != 2 && decfncnt != 3)
408
22
        || (encfncnt != decfncnt)
409
22
        || (gparamfncnt != 0 && gparamfncnt != 2)
410
22
        || (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
22
    return kem;
427
0
 err:
428
0
    EVP_KEM_free(kem);
429
0
    return NULL;
430
22
}
431
432
void EVP_KEM_free(EVP_KEM *kem)
433
22
{
434
22
    int i;
435
436
22
    if (kem == NULL)
437
0
        return;
438
439
22
    CRYPTO_DOWN_REF(&kem->refcnt, &i);
440
22
    if (i > 0)
441
22
        return;
442
0
    OPENSSL_free(kem->type_name);
443
0
    ossl_provider_free(kem->prov);
444
0
    CRYPTO_FREE_REF(&kem->refcnt);
445
0
    OPENSSL_free(kem);
446
0
}
447
448
int EVP_KEM_up_ref(EVP_KEM *kem)
449
44
{
450
44
    int ref = 0;
451
452
44
    CRYPTO_UP_REF(&kem->refcnt, &ref);
453
44
    return 1;
454
44
}
455
456
OSSL_PROVIDER *EVP_KEM_get0_provider(const EVP_KEM *kem)
457
11
{
458
11
    return kem->prov;
459
11
}
460
461
EVP_KEM *EVP_KEM_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
462
                       const char *properties)
463
0
{
464
0
    return evp_generic_fetch(ctx, OSSL_OP_KEM, algorithm, properties,
465
0
                             evp_kem_from_algorithm,
466
0
                             evp_kem_up_ref,
467
0
                             evp_kem_free);
468
0
}
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
2
{
503
2
    evp_generic_do_all(libctx, OSSL_OP_KEM, (void (*)(void *, void *))fn, arg,
504
2
                       evp_kem_from_algorithm,
505
2
                       evp_kem_up_ref,
506
2
                       evp_kem_free);
507
2
}
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
11
{
532
11
    void *provctx;
533
534
11
    if (kem == NULL || kem->settable_ctx_params == NULL)
535
0
        return NULL;
536
537
11
    provctx = ossl_provider_ctx(EVP_KEM_get0_provider(kem));
538
11
    return kem->settable_ctx_params(NULL, provctx);
539
11
}