Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/crypto/evp/kem.c
Line
Count
Source
1
/*
2
 * Copyright 2020-2023 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 int evp_kem_init(EVP_PKEY_CTX *ctx, int operation,
21
    const OSSL_PARAM params[], EVP_PKEY *authkey)
22
256
{
23
256
    int ret = 0;
24
256
    EVP_KEM *kem = NULL;
25
256
    EVP_KEYMGMT *tmp_keymgmt = NULL;
26
256
    const OSSL_PROVIDER *tmp_prov = NULL;
27
256
    void *provkey = NULL, *provauthkey = NULL;
28
256
    const char *supported_kem = NULL;
29
256
    int iter;
30
31
256
    if (ctx == NULL || ctx->keytype == NULL) {
32
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
33
0
        return 0;
34
0
    }
35
36
256
    evp_pkey_ctx_free_old_ops(ctx);
37
256
    ctx->operation = operation;
38
39
256
    if (ctx->pkey == NULL) {
40
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
41
0
        goto err;
42
0
    }
43
256
    if (authkey != NULL && authkey->type != ctx->pkey->type) {
44
0
        ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES);
45
0
        return 0;
46
0
    }
47
    /*
48
     * Try to derive the supported kem from |ctx->keymgmt|.
49
     */
50
256
    if (!ossl_assert(ctx->pkey->keymgmt == NULL
51
256
            || ctx->pkey->keymgmt == ctx->keymgmt)) {
52
0
        ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
53
0
        goto err;
54
0
    }
55
256
    supported_kem = evp_keymgmt_util_query_operation_name(ctx->keymgmt,
56
256
        OSSL_OP_KEM);
57
256
    if (supported_kem == NULL) {
58
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
59
0
        goto err;
60
0
    }
61
62
    /*
63
     * Because we cleared out old ops, we shouldn't need to worry about
64
     * checking if kem is already there.
65
     * We perform two iterations:
66
     *
67
     * 1.  Do the normal kem fetch, using the fetching data given by
68
     *     the EVP_PKEY_CTX.
69
     * 2.  Do the provider specific kem fetch, from the same provider
70
     *     as |ctx->keymgmt|
71
     *
72
     * We then try to fetch the keymgmt from the same provider as the
73
     * kem, and try to export |ctx->pkey| to that keymgmt (when this
74
     * keymgmt happens to be the same as |ctx->keymgmt|, the export is
75
     * a no-op, but we call it anyway to not complicate the code even
76
     * more).
77
     * If the export call succeeds (returns a non-NULL provider key pointer),
78
     * we're done and can perform the operation itself.  If not, we perform
79
     * the second iteration, or jump to legacy.
80
     */
81
512
    for (iter = 1, provkey = NULL; iter < 3 && provkey == NULL; iter++) {
82
256
        EVP_KEYMGMT *tmp_keymgmt_tofree = NULL;
83
84
        /*
85
         * If we're on the second iteration, free the results from the first.
86
         * They are NULL on the first iteration, so no need to check what
87
         * iteration we're on.
88
         */
89
256
        EVP_KEM_free(kem);
90
256
        EVP_KEYMGMT_free(tmp_keymgmt);
91
92
256
        switch (iter) {
93
256
        case 1:
94
256
            kem = EVP_KEM_fetch(ctx->libctx, supported_kem, ctx->propquery);
95
256
            if (kem != NULL)
96
256
                tmp_prov = EVP_KEM_get0_provider(kem);
97
256
            break;
98
0
        case 2:
99
0
            tmp_prov = EVP_KEYMGMT_get0_provider(ctx->keymgmt);
100
0
            kem = evp_kem_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
101
0
                supported_kem, ctx->propquery);
102
103
0
            if (kem == NULL) {
104
0
                ERR_raise(ERR_LIB_EVP,
105
0
                    EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
106
0
                ret = -2;
107
0
                goto err;
108
0
            }
109
256
        }
110
256
        if (kem == NULL)
111
0
            continue;
112
113
        /*
114
         * Ensure that the key is provided, either natively, or as a cached
115
         * export.  We start by fetching the keymgmt with the same name as
116
         * |ctx->pkey|, but from the provider of the kem method, using the
117
         * same property query as when fetching the kem method.
118
         * With the keymgmt we found (if we did), we try to export |ctx->pkey|
119
         * to it (evp_pkey_export_to_provider() is smart enough to only actually
120
         * export it if |tmp_keymgmt| is different from |ctx->pkey|'s keymgmt)
121
         */
122
256
        tmp_keymgmt_tofree = tmp_keymgmt = evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
123
256
            EVP_KEYMGMT_get0_name(ctx->keymgmt),
124
256
            ctx->propquery);
125
256
        if (tmp_keymgmt != NULL) {
126
256
            provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
127
256
                &tmp_keymgmt, ctx->propquery);
128
256
            if (provkey != NULL && authkey != NULL) {
129
0
                provauthkey = evp_pkey_export_to_provider(authkey, ctx->libctx,
130
0
                    &tmp_keymgmt,
131
0
                    ctx->propquery);
132
0
                if (provauthkey == NULL) {
133
0
                    EVP_KEM_free(kem);
134
0
                    ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
135
0
                    goto err;
136
0
                }
137
0
            }
138
256
        }
139
256
        if (tmp_keymgmt == NULL)
140
0
            EVP_KEYMGMT_free(tmp_keymgmt_tofree);
141
256
    }
142
143
256
    if (provkey == 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
149
256
    ctx->op.encap.kem = kem;
150
256
    ctx->op.encap.algctx = kem->newctx(ossl_provider_ctx(kem->prov));
151
256
    if (ctx->op.encap.algctx == NULL) {
152
        /* The provider key can stay in the cache */
153
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
154
0
        goto err;
155
0
    }
156
157
256
    switch (operation) {
158
156
    case EVP_PKEY_OP_ENCAPSULATE:
159
156
        if (provauthkey != NULL && kem->auth_encapsulate_init != NULL) {
160
0
            ret = kem->auth_encapsulate_init(ctx->op.encap.algctx, provkey,
161
0
                provauthkey, params);
162
156
        } else if (provauthkey == NULL && kem->encapsulate_init != NULL) {
163
156
            ret = kem->encapsulate_init(ctx->op.encap.algctx, provkey, params);
164
156
        } else {
165
0
            ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
166
0
            ret = -2;
167
0
            goto err;
168
0
        }
169
156
        break;
170
156
    case EVP_PKEY_OP_DECAPSULATE:
171
100
        if (provauthkey != NULL && kem->auth_decapsulate_init != NULL) {
172
0
            ret = kem->auth_decapsulate_init(ctx->op.encap.algctx, provkey,
173
0
                provauthkey, params);
174
100
        } else if (provauthkey == NULL && kem->encapsulate_init != NULL) {
175
100
            ret = kem->decapsulate_init(ctx->op.encap.algctx, provkey, params);
176
100
        } 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
100
        break;
182
100
    default:
183
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
184
0
        goto err;
185
256
    }
186
187
256
    EVP_KEYMGMT_free(tmp_keymgmt);
188
256
    tmp_keymgmt = NULL;
189
190
256
    if (ret > 0)
191
256
        return 1;
192
0
err:
193
0
    if (ret <= 0) {
194
0
        evp_pkey_ctx_free_old_ops(ctx);
195
0
        ctx->operation = EVP_PKEY_OP_UNDEFINED;
196
0
    }
197
0
    EVP_KEYMGMT_free(tmp_keymgmt);
198
0
    return ret;
199
256
}
200
201
int EVP_PKEY_auth_encapsulate_init(EVP_PKEY_CTX *ctx, EVP_PKEY *authpriv,
202
    const OSSL_PARAM params[])
203
0
{
204
0
    if (authpriv == NULL)
205
0
        return 0;
206
0
    return evp_kem_init(ctx, EVP_PKEY_OP_ENCAPSULATE, params, authpriv);
207
0
}
208
209
int EVP_PKEY_encapsulate_init(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
210
156
{
211
156
    return evp_kem_init(ctx, EVP_PKEY_OP_ENCAPSULATE, params, NULL);
212
156
}
213
214
int EVP_PKEY_encapsulate(EVP_PKEY_CTX *ctx,
215
    unsigned char *out, size_t *outlen,
216
    unsigned char *secret, size_t *secretlen)
217
184
{
218
184
    if (ctx == NULL)
219
0
        return 0;
220
221
184
    if (ctx->operation != EVP_PKEY_OP_ENCAPSULATE) {
222
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
223
0
        return -1;
224
0
    }
225
226
184
    if (ctx->op.encap.algctx == NULL) {
227
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
228
0
        return -2;
229
0
    }
230
231
184
    if (out != NULL && secret == NULL)
232
0
        return 0;
233
234
184
    return ctx->op.encap.kem->encapsulate(ctx->op.encap.algctx,
235
184
        out, outlen, secret, secretlen);
236
184
}
237
238
int EVP_PKEY_decapsulate_init(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
239
100
{
240
100
    return evp_kem_init(ctx, EVP_PKEY_OP_DECAPSULATE, params, NULL);
241
100
}
242
243
int EVP_PKEY_auth_decapsulate_init(EVP_PKEY_CTX *ctx, EVP_PKEY *authpub,
244
    const OSSL_PARAM params[])
245
0
{
246
0
    if (authpub == NULL)
247
0
        return 0;
248
0
    return evp_kem_init(ctx, EVP_PKEY_OP_DECAPSULATE, params, authpub);
249
0
}
250
251
int EVP_PKEY_decapsulate(EVP_PKEY_CTX *ctx,
252
    unsigned char *secret, size_t *secretlen,
253
    const unsigned char *in, size_t inlen)
254
100
{
255
100
    if (ctx == NULL
256
100
        || (in == NULL || inlen == 0)
257
100
        || (secret == NULL && secretlen == NULL))
258
0
        return 0;
259
260
100
    if (ctx->operation != EVP_PKEY_OP_DECAPSULATE) {
261
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
262
0
        return -1;
263
0
    }
264
265
100
    if (ctx->op.encap.algctx == NULL) {
266
0
        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
267
0
        return -2;
268
0
    }
269
100
    return ctx->op.encap.kem->decapsulate(ctx->op.encap.algctx,
270
100
        secret, secretlen, in, inlen);
271
100
}
272
273
static EVP_KEM *evp_kem_new(OSSL_PROVIDER *prov)
274
8
{
275
8
    EVP_KEM *kem = OPENSSL_zalloc(sizeof(EVP_KEM));
276
277
8
    if (kem == NULL)
278
0
        return NULL;
279
280
8
    if (!CRYPTO_NEW_REF(&kem->refcnt, 1)) {
281
0
        OPENSSL_free(kem);
282
0
        return NULL;
283
0
    }
284
8
    kem->prov = prov;
285
8
    ossl_provider_up_ref(prov);
286
287
8
    return kem;
288
8
}
289
290
static void *evp_kem_from_algorithm(int name_id, const OSSL_ALGORITHM *algodef,
291
    OSSL_PROVIDER *prov)
292
118
{
293
118
    const OSSL_DISPATCH *fns = algodef->implementation;
294
118
    EVP_KEM *kem = NULL;
295
118
    int ctxfncnt = 0, encfncnt = 0, decfncnt = 0;
296
118
    int gparamfncnt = 0, sparamfncnt = 0;
297
298
118
    if ((kem = evp_kem_new(prov)) == NULL) {
299
0
        ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
300
0
        goto err;
301
0
    }
302
303
118
    kem->name_id = name_id;
304
118
    if ((kem->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL)
305
0
        goto err;
306
118
    kem->description = algodef->algorithm_description;
307
308
1.17k
    for (; fns->function_id != 0; fns++) {
309
1.05k
        switch (fns->function_id) {
310
118
        case OSSL_FUNC_KEM_NEWCTX:
311
118
            if (kem->newctx != NULL)
312
0
                break;
313
118
            kem->newctx = OSSL_FUNC_kem_newctx(fns);
314
118
            ctxfncnt++;
315
118
            break;
316
118
        case OSSL_FUNC_KEM_ENCAPSULATE_INIT:
317
118
            if (kem->encapsulate_init != NULL)
318
0
                break;
319
118
            kem->encapsulate_init = OSSL_FUNC_kem_encapsulate_init(fns);
320
118
            encfncnt++;
321
118
            break;
322
36
        case OSSL_FUNC_KEM_AUTH_ENCAPSULATE_INIT:
323
36
            if (kem->auth_encapsulate_init != NULL)
324
0
                break;
325
36
            kem->auth_encapsulate_init = OSSL_FUNC_kem_auth_encapsulate_init(fns);
326
36
            encfncnt++;
327
36
            break;
328
118
        case OSSL_FUNC_KEM_ENCAPSULATE:
329
118
            if (kem->encapsulate != NULL)
330
0
                break;
331
118
            kem->encapsulate = OSSL_FUNC_kem_encapsulate(fns);
332
118
            encfncnt++;
333
118
            break;
334
118
        case OSSL_FUNC_KEM_DECAPSULATE_INIT:
335
118
            if (kem->decapsulate_init != NULL)
336
0
                break;
337
118
            kem->decapsulate_init = OSSL_FUNC_kem_decapsulate_init(fns);
338
118
            decfncnt++;
339
118
            break;
340
36
        case OSSL_FUNC_KEM_AUTH_DECAPSULATE_INIT:
341
36
            if (kem->auth_decapsulate_init != NULL)
342
0
                break;
343
36
            kem->auth_decapsulate_init = OSSL_FUNC_kem_auth_decapsulate_init(fns);
344
36
            decfncnt++;
345
36
            break;
346
118
        case OSSL_FUNC_KEM_DECAPSULATE:
347
118
            if (kem->decapsulate != NULL)
348
0
                break;
349
118
            kem->decapsulate = OSSL_FUNC_kem_decapsulate(fns);
350
118
            decfncnt++;
351
118
            break;
352
118
        case OSSL_FUNC_KEM_FREECTX:
353
118
            if (kem->freectx != NULL)
354
0
                break;
355
118
            kem->freectx = OSSL_FUNC_kem_freectx(fns);
356
118
            ctxfncnt++;
357
118
            break;
358
12
        case OSSL_FUNC_KEM_DUPCTX:
359
12
            if (kem->dupctx != NULL)
360
0
                break;
361
12
            kem->dupctx = OSSL_FUNC_kem_dupctx(fns);
362
12
            break;
363
12
        case OSSL_FUNC_KEM_GET_CTX_PARAMS:
364
12
            if (kem->get_ctx_params != NULL)
365
0
                break;
366
12
            kem->get_ctx_params
367
12
                = OSSL_FUNC_kem_get_ctx_params(fns);
368
12
            gparamfncnt++;
369
12
            break;
370
12
        case OSSL_FUNC_KEM_GETTABLE_CTX_PARAMS:
371
12
            if (kem->gettable_ctx_params != NULL)
372
0
                break;
373
12
            kem->gettable_ctx_params
374
12
                = OSSL_FUNC_kem_gettable_ctx_params(fns);
375
12
            gparamfncnt++;
376
12
            break;
377
118
        case OSSL_FUNC_KEM_SET_CTX_PARAMS:
378
118
            if (kem->set_ctx_params != NULL)
379
0
                break;
380
118
            kem->set_ctx_params
381
118
                = OSSL_FUNC_kem_set_ctx_params(fns);
382
118
            sparamfncnt++;
383
118
            break;
384
118
        case OSSL_FUNC_KEM_SETTABLE_CTX_PARAMS:
385
118
            if (kem->settable_ctx_params != NULL)
386
0
                break;
387
118
            kem->settable_ctx_params
388
118
                = OSSL_FUNC_kem_settable_ctx_params(fns);
389
118
            sparamfncnt++;
390
118
            break;
391
1.05k
        }
392
1.05k
    }
393
118
    if (ctxfncnt != 2
394
118
        || (encfncnt != 0 && encfncnt != 2 && encfncnt != 3)
395
118
        || (decfncnt != 0 && decfncnt != 2 && decfncnt != 3)
396
118
        || (encfncnt != decfncnt)
397
118
        || (gparamfncnt != 0 && gparamfncnt != 2)
398
118
        || (sparamfncnt != 0 && sparamfncnt != 2)) {
399
        /*
400
         * In order to be a consistent set of functions we must have at least
401
         * a set of context functions (newctx and freectx) as well as a pair
402
         * (or triplet) of "kem" functions:
403
         * (encapsulate_init, (and/or auth_encapsulate_init), encapsulate) or
404
         * (decapsulate_init, (and/or auth_decapsulate_init), decapsulate).
405
         * set_ctx_params and settable_ctx_params are optional, but if one of
406
         * them is present then the other one must also be present. The same
407
         * applies to get_ctx_params and gettable_ctx_params.
408
         * The dupctx function is optional.
409
         */
410
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
411
0
        goto err;
412
0
    }
413
414
118
    return kem;
415
0
err:
416
0
    EVP_KEM_free(kem);
417
0
    return NULL;
418
118
}
419
420
void EVP_KEM_free(EVP_KEM *kem)
421
658
{
422
658
    int i;
423
424
658
    if (kem == NULL)
425
256
        return;
426
427
402
    CRYPTO_DOWN_REF(&kem->refcnt, &i);
428
402
    if (i > 0)
429
380
        return;
430
22
    OPENSSL_free(kem->type_name);
431
22
    ossl_provider_free(kem->prov);
432
22
    CRYPTO_FREE_REF(&kem->refcnt);
433
22
    OPENSSL_free(kem);
434
22
}
435
436
int EVP_KEM_up_ref(EVP_KEM *kem)
437
459
{
438
459
    int ref = 0;
439
440
459
    CRYPTO_UP_REF(&kem->refcnt, &ref);
441
459
    return 1;
442
459
}
443
444
OSSL_PROVIDER *EVP_KEM_get0_provider(const EVP_KEM *kem)
445
348
{
446
348
    return kem->prov;
447
348
}
448
449
EVP_KEM *EVP_KEM_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
450
    const char *properties)
451
256
{
452
256
    return evp_generic_fetch(ctx, OSSL_OP_KEM, algorithm, properties,
453
256
        evp_kem_from_algorithm,
454
256
        (int (*)(void *))EVP_KEM_up_ref,
455
256
        (void (*)(void *))EVP_KEM_free);
456
256
}
457
458
EVP_KEM *evp_kem_fetch_from_prov(OSSL_PROVIDER *prov, const char *algorithm,
459
    const char *properties)
460
0
{
461
0
    return evp_generic_fetch_from_prov(prov, OSSL_OP_KEM, algorithm, properties,
462
0
        evp_kem_from_algorithm,
463
0
        (int (*)(void *))EVP_KEM_up_ref,
464
0
        (void (*)(void *))EVP_KEM_free);
465
0
}
466
467
int EVP_KEM_is_a(const EVP_KEM *kem, const char *name)
468
0
{
469
0
    return kem != NULL && evp_is_a(kem->prov, kem->name_id, NULL, name);
470
0
}
471
472
int evp_kem_get_number(const EVP_KEM *kem)
473
0
{
474
0
    return kem->name_id;
475
0
}
476
477
const char *EVP_KEM_get0_name(const EVP_KEM *kem)
478
0
{
479
0
    return kem->type_name;
480
0
}
481
482
const char *EVP_KEM_get0_description(const EVP_KEM *kem)
483
0
{
484
0
    return kem->description;
485
0
}
486
487
void EVP_KEM_do_all_provided(OSSL_LIB_CTX *libctx,
488
    void (*fn)(EVP_KEM *kem, void *arg),
489
    void *arg)
490
8
{
491
8
    evp_generic_do_all(libctx, OSSL_OP_KEM, (void (*)(void *, void *))fn, arg,
492
8
        evp_kem_from_algorithm,
493
8
        (int (*)(void *))EVP_KEM_up_ref,
494
8
        (void (*)(void *))EVP_KEM_free);
495
8
}
496
497
int EVP_KEM_names_do_all(const EVP_KEM *kem,
498
    void (*fn)(const char *name, void *data),
499
    void *data)
500
0
{
501
0
    if (kem->prov != NULL)
502
0
        return evp_names_do_all(kem->prov, kem->name_id, fn, data);
503
504
0
    return 1;
505
0
}
506
507
const OSSL_PARAM *EVP_KEM_gettable_ctx_params(const EVP_KEM *kem)
508
0
{
509
0
    void *provctx;
510
511
0
    if (kem == NULL || kem->gettable_ctx_params == NULL)
512
0
        return NULL;
513
514
0
    provctx = ossl_provider_ctx(EVP_KEM_get0_provider(kem));
515
0
    return kem->gettable_ctx_params(NULL, provctx);
516
0
}
517
518
const OSSL_PARAM *EVP_KEM_settable_ctx_params(const EVP_KEM *kem)
519
52
{
520
52
    void *provctx;
521
522
52
    if (kem == NULL || kem->settable_ctx_params == NULL)
523
0
        return NULL;
524
525
52
    provctx = ossl_provider_ctx(EVP_KEM_get0_provider(kem));
526
    return kem->settable_ctx_params(NULL, provctx);
527
52
}