Coverage Report

Created: 2025-06-13 06:58

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