Coverage Report

Created: 2023-06-08 06:41

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