Coverage Report

Created: 2025-12-31 06:58

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