Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/providers/implementations/kem/ec_kem.c
Line
Count
Source
1
/*
2
 * Copyright 2022-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
/* clang-format off */
10
11
/* clang-format on */
12
13
/*
14
 * The following implementation is part of RFC 9180 related to DHKEM using
15
 * EC keys (i.e. P-256, P-384 and P-521)
16
 * References to Sections in the comments below refer to RFC 9180.
17
 */
18
19
#include "internal/deprecated.h"
20
21
#include <openssl/crypto.h>
22
#include <openssl/evp.h>
23
#include <openssl/core_dispatch.h>
24
#include <openssl/core_names.h>
25
#include <openssl/ec.h>
26
#include <openssl/params.h>
27
#include <openssl/err.h>
28
#include <openssl/proverr.h>
29
#include <openssl/kdf.h>
30
#include <openssl/rand.h>
31
#include "internal/cryptlib.h"
32
#include "prov/provider_ctx.h"
33
#include "prov/implementations.h"
34
#include "prov/securitycheck.h"
35
#include "prov/providercommon.h"
36
37
#include <openssl/hpke.h>
38
#include "internal/hpke_util.h"
39
#include "crypto/ec.h"
40
#include "prov/ecx.h"
41
#include "prov/eckem.h"
42
43
typedef struct {
44
    EC_KEY *recipient_key;
45
    EC_KEY *sender_authkey;
46
    OSSL_LIB_CTX *libctx;
47
    char *propq;
48
    unsigned int mode;
49
    unsigned int op;
50
    unsigned char *ikm;
51
    size_t ikmlen;
52
    const char *kdfname;
53
    const OSSL_HPKE_KEM_INFO *info;
54
} PROV_EC_CTX;
55
56
static OSSL_FUNC_kem_newctx_fn eckem_newctx;
57
static OSSL_FUNC_kem_encapsulate_init_fn eckem_encapsulate_init;
58
static OSSL_FUNC_kem_auth_encapsulate_init_fn eckem_auth_encapsulate_init;
59
static OSSL_FUNC_kem_encapsulate_fn eckem_encapsulate;
60
static OSSL_FUNC_kem_decapsulate_init_fn eckem_decapsulate_init;
61
static OSSL_FUNC_kem_auth_decapsulate_init_fn eckem_auth_decapsulate_init;
62
static OSSL_FUNC_kem_decapsulate_fn eckem_decapsulate;
63
static OSSL_FUNC_kem_freectx_fn eckem_freectx;
64
static OSSL_FUNC_kem_set_ctx_params_fn eckem_set_ctx_params;
65
static OSSL_FUNC_kem_settable_ctx_params_fn eckem_settable_ctx_params;
66
67
/* ASCII: "KEM", in hex for EBCDIC compatibility */
68
static const char LABEL_KEM[] = "\x4b\x45\x4d";
69
70
static int eckey_check(const EC_KEY *ec, int requires_privatekey)
71
0
{
72
0
    int rv = 0;
73
0
    BN_CTX *bnctx = NULL;
74
0
    BIGNUM *rem = NULL;
75
0
    const BIGNUM *priv = EC_KEY_get0_private_key(ec);
76
0
    const EC_POINT *pub = EC_KEY_get0_public_key(ec);
77
78
    /* Keys always require a public component */
79
0
    if (pub == NULL) {
80
0
        ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
81
0
        return 0;
82
0
    }
83
0
    if (priv == NULL) {
84
0
        return (requires_privatekey == 0);
85
0
    } else {
86
        /* If there is a private key, check that is non zero (mod order) */
87
0
        const EC_GROUP *group = EC_KEY_get0_group(ec);
88
0
        const BIGNUM *order = EC_GROUP_get0_order(group);
89
90
0
        bnctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(ec));
91
0
        rem = BN_new();
92
93
0
        if (order != NULL && rem != NULL && bnctx != NULL) {
94
0
            rv = BN_mod(rem, priv, order, bnctx)
95
0
                && !BN_is_zero(rem);
96
0
        }
97
0
    }
98
0
    BN_free(rem);
99
0
    BN_CTX_free(bnctx);
100
0
    return rv;
101
0
}
102
103
/* Returns NULL if the curve is not supported */
104
static const char *ec_curvename_get0(const EC_KEY *ec)
105
0
{
106
0
    const EC_GROUP *group = EC_KEY_get0_group(ec);
107
108
0
    return EC_curve_nid2nist(EC_GROUP_get_curve_name(group));
109
0
}
110
111
/*
112
 * Set the recipient key, and free any existing key.
113
 * ec can be NULL.
114
 * The ec key may have only a private or public component
115
 * (but it must have a group).
116
 */
117
static int recipient_key_set(PROV_EC_CTX *ctx, EC_KEY *ec)
118
0
{
119
0
    EC_KEY_free(ctx->recipient_key);
120
0
    ctx->recipient_key = NULL;
121
122
0
    if (ec != NULL) {
123
0
        const char *curve = ec_curvename_get0(ec);
124
125
0
        if (curve == NULL)
126
0
            return -2;
127
0
        ctx->info = ossl_HPKE_KEM_INFO_find_curve(curve);
128
0
        if (ctx->info == NULL)
129
0
            return -2;
130
0
        if (!EC_KEY_up_ref(ec))
131
0
            return 0;
132
0
        ctx->recipient_key = ec;
133
0
        ctx->kdfname = "HKDF";
134
0
    }
135
0
    return 1;
136
0
}
137
138
/*
139
 * Set the senders auth key, and free any existing auth key.
140
 * ec can be NULL.
141
 */
142
static int sender_authkey_set(PROV_EC_CTX *ctx, EC_KEY *ec)
143
0
{
144
0
    EC_KEY_free(ctx->sender_authkey);
145
0
    ctx->sender_authkey = NULL;
146
147
0
    if (ec != NULL) {
148
0
        if (!EC_KEY_up_ref(ec))
149
0
            return 0;
150
0
        ctx->sender_authkey = ec;
151
0
    }
152
0
    return 1;
153
0
}
154
155
/*
156
 * Serializes a encoded public key buffer into a EC public key.
157
 * Params:
158
 *     in Contains the group.
159
 *     pubbuf The encoded public key buffer
160
 * Returns: The created public EC key, or NULL if there is an error.
161
 */
162
static EC_KEY *eckey_frompub(EC_KEY *in,
163
    const unsigned char *pubbuf, size_t pubbuflen)
164
0
{
165
0
    EC_KEY *key;
166
167
0
    key = EC_KEY_new_ex(ossl_ec_key_get_libctx(in), ossl_ec_key_get0_propq(in));
168
0
    if (key == NULL)
169
0
        goto err;
170
0
    if (!EC_KEY_set_group(key, EC_KEY_get0_group(in)))
171
0
        goto err;
172
0
    if (!EC_KEY_oct2key(key, pubbuf, pubbuflen, NULL))
173
0
        goto err;
174
0
    return key;
175
0
err:
176
0
    EC_KEY_free(key);
177
0
    return NULL;
178
0
}
179
180
/*
181
 * Deserialises a EC public key into a encoded byte array.
182
 * Returns: 1 if successful or 0 otherwise.
183
 */
184
static int ecpubkey_todata(const EC_KEY *ec, unsigned char *out, size_t *outlen,
185
    size_t maxoutlen)
186
0
{
187
0
    const EC_POINT *pub;
188
0
    const EC_GROUP *group;
189
190
0
    group = EC_KEY_get0_group(ec);
191
0
    pub = EC_KEY_get0_public_key(ec);
192
0
    *outlen = EC_POINT_point2oct(group, pub, POINT_CONVERSION_UNCOMPRESSED,
193
0
        out, maxoutlen, NULL);
194
0
    return *outlen != 0;
195
0
}
196
197
static void *eckem_newctx(void *provctx)
198
0
{
199
0
    PROV_EC_CTX *ctx = OPENSSL_zalloc(sizeof(PROV_EC_CTX));
200
201
0
    if (ctx == NULL)
202
0
        return NULL;
203
0
    ctx->libctx = PROV_LIBCTX_OF(provctx);
204
0
    ctx->mode = KEM_MODE_DHKEM;
205
206
0
    return ctx;
207
0
}
208
209
static void eckem_freectx(void *vectx)
210
0
{
211
0
    PROV_EC_CTX *ctx = (PROV_EC_CTX *)vectx;
212
213
0
    OPENSSL_clear_free(ctx->ikm, ctx->ikmlen);
214
0
    recipient_key_set(ctx, NULL);
215
0
    sender_authkey_set(ctx, NULL);
216
0
    OPENSSL_free(ctx);
217
0
}
218
219
static int ossl_ec_match_params(const EC_KEY *key1, const EC_KEY *key2)
220
0
{
221
0
    int ret;
222
0
    BN_CTX *ctx = NULL;
223
0
    const EC_GROUP *group1 = EC_KEY_get0_group(key1);
224
0
    const EC_GROUP *group2 = EC_KEY_get0_group(key2);
225
226
0
    ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(key1));
227
0
    if (ctx == NULL)
228
0
        return 0;
229
230
0
    ret = group1 != NULL
231
0
        && group2 != NULL
232
0
        && EC_GROUP_cmp(group1, group2, ctx) == 0;
233
0
    if (!ret)
234
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISMATCHING_DOMAIN_PARAMETERS);
235
0
    BN_CTX_free(ctx);
236
0
    return ret;
237
0
}
238
239
static int eckem_init(void *vctx, int operation, void *vec, void *vauth,
240
    const OSSL_PARAM params[])
241
0
{
242
0
    int rv;
243
0
    PROV_EC_CTX *ctx = (PROV_EC_CTX *)vctx;
244
0
    EC_KEY *ec = vec;
245
0
    EC_KEY *auth = vauth;
246
247
0
    if (!ossl_prov_is_running())
248
0
        return 0;
249
250
0
    if (!eckey_check(ec, operation == EVP_PKEY_OP_DECAPSULATE))
251
0
        return 0;
252
0
    rv = recipient_key_set(ctx, ec);
253
0
    if (rv <= 0)
254
0
        return rv;
255
256
0
    if (auth != NULL) {
257
0
        if (!ossl_ec_match_params(ec, auth)
258
0
            || !eckey_check(auth, operation == EVP_PKEY_OP_ENCAPSULATE)
259
0
            || !sender_authkey_set(ctx, auth))
260
0
            return 0;
261
0
    }
262
263
0
    ctx->op = operation;
264
0
    return eckem_set_ctx_params(vctx, params);
265
0
}
266
267
static int eckem_encapsulate_init(void *vctx, void *vec,
268
    const OSSL_PARAM params[])
269
0
{
270
0
    return eckem_init(vctx, EVP_PKEY_OP_ENCAPSULATE, vec, NULL, params);
271
0
}
272
273
static int eckem_decapsulate_init(void *vctx, void *vec,
274
    const OSSL_PARAM params[])
275
0
{
276
0
    return eckem_init(vctx, EVP_PKEY_OP_DECAPSULATE, vec, NULL, params);
277
0
}
278
279
static int eckem_auth_encapsulate_init(void *vctx, void *vecx, void *vauthpriv,
280
    const OSSL_PARAM params[])
281
0
{
282
0
    return eckem_init(vctx, EVP_PKEY_OP_ENCAPSULATE, vecx, vauthpriv, params);
283
0
}
284
285
static int eckem_auth_decapsulate_init(void *vctx, void *vecx, void *vauthpub,
286
    const OSSL_PARAM params[])
287
0
{
288
0
    return eckem_init(vctx, EVP_PKEY_OP_DECAPSULATE, vecx, vauthpub, params);
289
0
}
290
291
/* clang-format off */
292
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
293
#ifndef eckem_set_ctx_params_list
294
static const OSSL_PARAM eckem_set_ctx_params_list[] = {
295
    OSSL_PARAM_utf8_string(OSSL_KEM_PARAM_OPERATION, NULL, 0),
296
    OSSL_PARAM_octet_string(OSSL_KEM_PARAM_IKME, NULL, 0),
297
    OSSL_PARAM_END
298
};
299
#endif
300
301
#ifndef eckem_set_ctx_params_st
302
struct eckem_set_ctx_params_st {
303
    OSSL_PARAM *ikme;
304
    OSSL_PARAM *op;
305
};
306
#endif
307
308
#ifndef eckem_set_ctx_params_decoder
309
static int eckem_set_ctx_params_decoder
310
    (const OSSL_PARAM *p, struct eckem_set_ctx_params_st *r)
311
0
{
312
0
    const char *s;
313
314
0
    memset(r, 0, sizeof(*r));
315
0
    if (p != NULL)
316
0
        for (; (s = p->key) != NULL; p++)
317
0
            switch(s[0]) {
318
0
            default:
319
0
                break;
320
0
            case 'i':
321
0
                if (ossl_likely(strcmp("kme", s + 1) == 0)) {
322
                    /* OSSL_KEM_PARAM_IKME */
323
0
                    if (ossl_unlikely(r->ikme != NULL)) {
324
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
325
0
                                       "param %s is repeated", s);
326
0
                        return 0;
327
0
                    }
328
0
                    r->ikme = (OSSL_PARAM *)p;
329
0
                }
330
0
                break;
331
0
            case 'o':
332
0
                if (ossl_likely(strcmp("peration", s + 1) == 0)) {
333
                    /* OSSL_KEM_PARAM_OPERATION */
334
0
                    if (ossl_unlikely(r->op != NULL)) {
335
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
336
0
                                       "param %s is repeated", s);
337
0
                        return 0;
338
0
                    }
339
0
                    r->op = (OSSL_PARAM *)p;
340
0
                }
341
0
            }
342
0
    return 1;
343
0
}
344
#endif
345
/* End of machine generated */
346
/* clang-format on */
347
348
static int eckem_set_ctx_params(void *vctx, const OSSL_PARAM params[])
349
0
{
350
0
    PROV_EC_CTX *ctx = (PROV_EC_CTX *)vctx;
351
0
    struct eckem_set_ctx_params_st p;
352
0
    int mode;
353
354
0
    if (ctx == NULL || !eckem_set_ctx_params_decoder(params, &p))
355
0
        return 0;
356
357
0
    if (p.ikme != NULL) {
358
0
        void *tmp = NULL;
359
0
        size_t tmplen = 0;
360
361
0
        if (p.ikme->data != NULL && p.ikme->data_size != 0) {
362
0
            if (!OSSL_PARAM_get_octet_string(p.ikme, &tmp, 0, &tmplen))
363
0
                return 0;
364
0
        }
365
0
        OPENSSL_clear_free(ctx->ikm, ctx->ikmlen);
366
        /* Set the ephemeral seed */
367
0
        ctx->ikm = tmp;
368
0
        ctx->ikmlen = tmplen;
369
0
    }
370
371
0
    if (p.op != NULL) {
372
0
        if (p.op->data_type != OSSL_PARAM_UTF8_STRING)
373
0
            return 0;
374
0
        mode = ossl_eckem_modename2id(p.op->data);
375
0
        if (mode == KEM_MODE_UNDEFINED)
376
0
            return 0;
377
0
        ctx->mode = mode;
378
0
    }
379
0
    return 1;
380
0
}
381
382
static const OSSL_PARAM *eckem_settable_ctx_params(ossl_unused void *vctx,
383
    ossl_unused void *provctx)
384
5
{
385
5
    return eckem_set_ctx_params_list;
386
5
}
387
388
/*
389
 * See Section 4.1 DH-Based KEM (DHKEM) ExtractAndExpand
390
 */
391
static int dhkem_extract_and_expand(EVP_KDF_CTX *kctx,
392
    unsigned char *okm, size_t okmlen,
393
    uint16_t kemid,
394
    const unsigned char *dhkm, size_t dhkmlen,
395
    const unsigned char *kemctx,
396
    size_t kemctxlen)
397
0
{
398
0
    uint8_t suiteid[2];
399
0
    uint8_t prk[EVP_MAX_MD_SIZE];
400
0
    size_t prklen = okmlen;
401
0
    int ret;
402
403
0
    if (prklen > sizeof(prk))
404
0
        return 0;
405
406
0
    suiteid[0] = (kemid >> 8) & 0xff;
407
0
    suiteid[1] = kemid & 0xff;
408
409
0
    ret = ossl_hpke_labeled_extract(kctx, prk, prklen,
410
0
              NULL, 0, LABEL_KEM, suiteid, sizeof(suiteid),
411
0
              OSSL_DHKEM_LABEL_EAE_PRK, dhkm, dhkmlen)
412
0
        && ossl_hpke_labeled_expand(kctx, okm, okmlen, prk, prklen,
413
0
            LABEL_KEM, suiteid, sizeof(suiteid),
414
0
            OSSL_DHKEM_LABEL_SHARED_SECRET,
415
0
            kemctx, kemctxlen);
416
0
    OPENSSL_cleanse(prk, prklen);
417
0
    return ret;
418
0
}
419
420
/*
421
 * See Section 7.1.3 DeriveKeyPair.
422
 *
423
 * This function is used by ec keygen.
424
 * (For this reason it does not use any of the state stored in PROV_EC_CTX).
425
 *
426
 * Params:
427
 *     ec An initialized ec key.
428
 *     priv The buffer to store the generated private key into (it is assumed
429
 *          this is of length alg->encodedprivlen).
430
 *     ikm buffer containing the input key material (seed). This must be set.
431
 *     ikmlen size of the ikm buffer in bytes
432
 * Returns:
433
 *     1 if successful or 0 otherwise.
434
 */
435
int ossl_ec_dhkem_derive_private(EC_KEY *ec, BIGNUM *priv,
436
    const unsigned char *ikm, size_t ikmlen)
437
0
{
438
0
    int ret = 0;
439
0
    EVP_KDF_CTX *kdfctx = NULL;
440
0
    uint8_t suiteid[2];
441
0
    unsigned char prk[OSSL_HPKE_MAX_SECRET];
442
0
    unsigned char privbuf[OSSL_HPKE_MAX_PRIVATE];
443
0
    const BIGNUM *order;
444
0
    unsigned char counter = 0;
445
0
    const char *curve = ec_curvename_get0(ec);
446
0
    const OSSL_HPKE_KEM_INFO *info;
447
448
0
    if (curve == NULL)
449
0
        return -2;
450
451
0
    info = ossl_HPKE_KEM_INFO_find_curve(curve);
452
0
    if (info == NULL)
453
0
        return -2;
454
455
0
    kdfctx = ossl_kdf_ctx_create("HKDF", info->mdname,
456
0
        ossl_ec_key_get_libctx(ec),
457
0
        ossl_ec_key_get0_propq(ec));
458
0
    if (kdfctx == NULL)
459
0
        return 0;
460
461
    /* ikmlen should have a length of at least Nsk */
462
0
    if (ikmlen < info->Nsk) {
463
0
        ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_INPUT_LENGTH,
464
0
            "ikm length is :%zu, should be at least %zu",
465
0
            ikmlen, info->Nsk);
466
0
        goto err;
467
0
    }
468
469
0
    suiteid[0] = info->kem_id / 256;
470
0
    suiteid[1] = info->kem_id % 256;
471
472
0
    if (!ossl_hpke_labeled_extract(kdfctx, prk, info->Nsecret,
473
0
            NULL, 0, LABEL_KEM, suiteid, sizeof(suiteid),
474
0
            OSSL_DHKEM_LABEL_DKP_PRK, ikm, ikmlen))
475
0
        goto err;
476
477
0
    order = EC_GROUP_get0_order(EC_KEY_get0_group(ec));
478
0
    do {
479
0
        if (!ossl_hpke_labeled_expand(kdfctx, privbuf, info->Nsk,
480
0
                prk, info->Nsecret,
481
0
                LABEL_KEM, suiteid, sizeof(suiteid),
482
0
                OSSL_DHKEM_LABEL_CANDIDATE,
483
0
                &counter, 1))
484
0
            goto err;
485
0
        privbuf[0] &= info->bitmask;
486
0
        if (BN_bin2bn(privbuf, (int)info->Nsk, priv) == NULL)
487
0
            goto err;
488
0
        if (counter == 0xFF) {
489
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY);
490
0
            goto err;
491
0
        }
492
0
        counter++;
493
0
    } while (BN_is_zero(priv) || BN_cmp(priv, order) >= 0);
494
0
    ret = 1;
495
0
err:
496
0
    OPENSSL_cleanse(prk, sizeof(prk));
497
0
    OPENSSL_cleanse(privbuf, sizeof(privbuf));
498
0
    EVP_KDF_CTX_free(kdfctx);
499
0
    return ret;
500
0
}
501
502
/*
503
 * Do a keygen operation without having to use EVP_PKEY.
504
 * Params:
505
 *     ctx Context object
506
 *     ikm The seed material - if this is NULL, then a random seed is used.
507
 * Returns:
508
 *     The generated EC key, or NULL on failure.
509
 */
510
static EC_KEY *derivekey(PROV_EC_CTX *ctx,
511
    const unsigned char *ikm, size_t ikmlen)
512
0
{
513
0
    int ret = 0;
514
0
    EC_KEY *key;
515
0
    unsigned char *seed = (unsigned char *)ikm;
516
0
    size_t seedlen = ikmlen;
517
0
    unsigned char tmpbuf[OSSL_HPKE_MAX_PRIVATE];
518
519
0
    key = EC_KEY_new_ex(ctx->libctx, ctx->propq);
520
0
    if (key == NULL)
521
0
        goto err;
522
0
    if (!EC_KEY_set_group(key, EC_KEY_get0_group(ctx->recipient_key)))
523
0
        goto err;
524
525
    /* Generate a random seed if there is no input ikm */
526
0
    if (seed == NULL || seedlen == 0) {
527
0
        seedlen = ctx->info->Nsk;
528
0
        if (seedlen > sizeof(tmpbuf))
529
0
            goto err;
530
0
        if (RAND_priv_bytes_ex(ctx->libctx, tmpbuf, seedlen, 0) <= 0)
531
0
            goto err;
532
0
        seed = tmpbuf;
533
0
    }
534
0
    ret = ossl_ec_generate_key_dhkem(key, seed, seedlen);
535
0
err:
536
0
    if (seed != ikm)
537
0
        OPENSSL_cleanse(seed, seedlen);
538
0
    if (ret <= 0) {
539
0
        EC_KEY_free(key);
540
0
        key = NULL;
541
0
    }
542
0
    return key;
543
0
}
544
545
/*
546
 * Before doing a key exchange the public key of the peer needs to be checked
547
 * Note that the group check is not done here as we have already checked
548
 * that it only uses one of the approved curve names when the key was set.
549
 *
550
 * Returns 1 if the public key is valid, or 0 if it fails.
551
 */
552
static int check_publickey(const EC_KEY *pub)
553
0
{
554
0
    int ret = 0;
555
0
    BN_CTX *bnctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(pub));
556
557
0
    if (bnctx == NULL)
558
0
        return 0;
559
0
    ret = ossl_ec_key_public_check(pub, bnctx);
560
0
    BN_CTX_free(bnctx);
561
562
0
    return ret;
563
0
}
564
565
/*
566
 * Do an ecdh key exchange.
567
 * dhkm = DH(sender, peer)
568
 *
569
 * NOTE: Instead of using EVP_PKEY_derive() API's, we use EC_KEY operations
570
 *       to avoid messy conversions back to EVP_PKEY.
571
 *
572
 * Returns the size of the secret if successful, or 0 otherwise,
573
 */
574
static int generate_ecdhkm(const EC_KEY *sender, const EC_KEY *peer,
575
    unsigned char *out, size_t maxout,
576
    unsigned int secretsz)
577
0
{
578
0
    const EC_GROUP *group = EC_KEY_get0_group(sender);
579
0
    size_t secretlen = (EC_GROUP_get_degree(group) + 7) / 8;
580
581
0
    if (secretlen != secretsz || secretlen > maxout) {
582
0
        ERR_raise_data(ERR_LIB_PROV, PROV_R_BAD_LENGTH, "secretsz invalid");
583
0
        return 0;
584
0
    }
585
586
0
    if (!check_publickey(peer))
587
0
        return 0;
588
0
    return ECDH_compute_key(out, secretlen, EC_KEY_get0_public_key(peer),
589
0
               sender, NULL)
590
0
        > 0;
591
0
}
592
593
/*
594
 * Derive a secret using ECDH (code is shared by the encap and decap)
595
 *
596
 * dhkm = Concat(ecdh(privkey1, peerkey1), ecdh(privkey2, peerkey2)
597
 * kemctx = Concat(sender_pub, recipient_pub, ctx->sender_authkey)
598
 * secret = dhkem_extract_and_expand(kemid, dhkm, kemctx);
599
 *
600
 * Params:
601
 *     ctx Object that contains algorithm state and constants.
602
 *     secret The returned secret (with a length ctx->alg->secretlen bytes).
603
 *     privkey1 A private key used for ECDH key derivation.
604
 *     peerkey1 A public key used for ECDH key derivation with privkey1
605
 *     privkey2 A optional private key used for a second ECDH key derivation.
606
 *              It can be NULL.
607
 *     peerkey2 A optional public key used for a second ECDH key derivation
608
 *              with privkey2,. It can be NULL.
609
 *     sender_pub The senders public key in encoded form.
610
 *     recipient_pub The recipients public key in encoded form.
611
 * Notes:
612
 *     The second ecdh() is only used for the HPKE auth modes when both privkey2
613
 *     and peerkey2 are non NULL (i.e. ctx->sender_authkey is not NULL).
614
 */
615
static int derive_secret(PROV_EC_CTX *ctx, unsigned char *secret,
616
    const EC_KEY *privkey1, const EC_KEY *peerkey1,
617
    const EC_KEY *privkey2, const EC_KEY *peerkey2,
618
    const unsigned char *sender_pub,
619
    const unsigned char *recipient_pub)
620
0
{
621
0
    int ret = 0;
622
0
    EVP_KDF_CTX *kdfctx = NULL;
623
0
    unsigned char sender_authpub[OSSL_HPKE_MAX_PUBLIC];
624
0
    unsigned char dhkm[OSSL_HPKE_MAX_PRIVATE * 2];
625
0
    unsigned char kemctx[OSSL_HPKE_MAX_PUBLIC * 3];
626
0
    size_t sender_authpublen;
627
0
    size_t kemctxlen = 0, dhkmlen = 0;
628
0
    const OSSL_HPKE_KEM_INFO *info = ctx->info;
629
0
    size_t encodedpublen = info->Npk;
630
0
    size_t encodedprivlen = info->Nsk;
631
0
    int auth = ctx->sender_authkey != NULL;
632
633
0
    if (!generate_ecdhkm(privkey1, peerkey1, dhkm, sizeof(dhkm),
634
0
            (unsigned int)encodedprivlen))
635
0
        goto err;
636
0
    dhkmlen = encodedprivlen;
637
0
    kemctxlen = 2 * encodedpublen;
638
639
    /* Concat the optional second ECDH (used for Auth) */
640
0
    if (auth) {
641
        /* Get the public key of the auth sender in encoded form */
642
0
        if (!ecpubkey_todata(ctx->sender_authkey, sender_authpub,
643
0
                &sender_authpublen, sizeof(sender_authpub)))
644
0
            goto err;
645
0
        if (sender_authpublen != encodedpublen) {
646
0
            ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY,
647
0
                "Invalid sender auth public key");
648
0
            goto err;
649
0
        }
650
0
        if (!generate_ecdhkm(privkey2, peerkey2,
651
0
                dhkm + dhkmlen, sizeof(dhkm) - dhkmlen,
652
0
                (unsigned int)encodedprivlen))
653
0
            goto err;
654
0
        dhkmlen += encodedprivlen;
655
0
        kemctxlen += encodedpublen;
656
0
    }
657
0
    if (kemctxlen > sizeof(kemctx))
658
0
        goto err;
659
660
    /* kemctx is the concat of both sides encoded public key */
661
0
    memcpy(kemctx, sender_pub, info->Npk);
662
0
    memcpy(kemctx + info->Npk, recipient_pub, info->Npk);
663
0
    if (auth)
664
0
        memcpy(kemctx + 2 * encodedpublen, sender_authpub, encodedpublen);
665
0
    kdfctx = ossl_kdf_ctx_create(ctx->kdfname, info->mdname,
666
0
        ctx->libctx, ctx->propq);
667
0
    if (kdfctx == NULL)
668
0
        goto err;
669
0
    if (!dhkem_extract_and_expand(kdfctx, secret, info->Nsecret,
670
0
            info->kem_id, dhkm, dhkmlen,
671
0
            kemctx, kemctxlen))
672
0
        goto err;
673
0
    ret = 1;
674
0
err:
675
0
    OPENSSL_cleanse(dhkm, dhkmlen);
676
0
    EVP_KDF_CTX_free(kdfctx);
677
0
    return ret;
678
0
}
679
680
/*
681
 * Do a DHKEM encapsulate operation.
682
 *
683
 * See Section 4.1 Encap() and AuthEncap()
684
 *
685
 * Params:
686
 *     ctx A context object holding the recipients public key and the
687
 *         optional senders auth private key.
688
 *     enc A buffer to return the senders ephemeral public key.
689
 *         Setting this to NULL allows the enclen and secretlen to return
690
 *         values, without calculating the secret.
691
 *     enclen Passes in the max size of the enc buffer and returns the
692
 *            encoded public key length.
693
 *     secret A buffer to return the calculated shared secret.
694
 *     secretlen Passes in the max size of the secret buffer and returns the
695
 *               secret length.
696
 * Returns: 1 on success or 0 otherwise.
697
 */
698
static int dhkem_encap(PROV_EC_CTX *ctx,
699
    unsigned char *enc, size_t *enclen,
700
    unsigned char *secret, size_t *secretlen)
701
0
{
702
0
    int ret = 0;
703
0
    EC_KEY *sender_ephemkey = NULL;
704
0
    unsigned char sender_pub[OSSL_HPKE_MAX_PUBLIC];
705
0
    unsigned char recipient_pub[OSSL_HPKE_MAX_PUBLIC];
706
0
    size_t sender_publen, recipient_publen;
707
0
    const OSSL_HPKE_KEM_INFO *info = ctx->info;
708
709
0
    if (enc == NULL) {
710
0
        if (enclen == NULL && secretlen == NULL)
711
0
            return 0;
712
0
        if (enclen != NULL)
713
0
            *enclen = info->Nenc;
714
0
        if (secretlen != NULL)
715
0
            *secretlen = info->Nsecret;
716
0
        return 1;
717
0
    }
718
719
0
    if (*secretlen < info->Nsecret) {
720
0
        ERR_raise_data(ERR_LIB_PROV, PROV_R_BAD_LENGTH, "*secretlen too small");
721
0
        return 0;
722
0
    }
723
0
    if (*enclen < info->Nenc) {
724
0
        ERR_raise_data(ERR_LIB_PROV, PROV_R_BAD_LENGTH, "*enclen too small");
725
0
        return 0;
726
0
    }
727
728
    /* Create an ephemeral key */
729
0
    sender_ephemkey = derivekey(ctx, ctx->ikm, ctx->ikmlen);
730
0
    if (sender_ephemkey == NULL)
731
0
        goto err;
732
0
    if (!ecpubkey_todata(sender_ephemkey, sender_pub, &sender_publen,
733
0
            sizeof(sender_pub))
734
0
        || !ecpubkey_todata(ctx->recipient_key, recipient_pub,
735
0
            &recipient_publen, sizeof(recipient_pub)))
736
0
        goto err;
737
738
0
    if (sender_publen != info->Npk
739
0
        || recipient_publen != sender_publen) {
740
0
        ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY, "Invalid public key");
741
0
        goto err;
742
0
    }
743
744
0
    if (!derive_secret(ctx, secret,
745
0
            sender_ephemkey, ctx->recipient_key,
746
0
            ctx->sender_authkey, ctx->recipient_key,
747
0
            sender_pub, recipient_pub))
748
0
        goto err;
749
750
    /* Return the senders ephemeral public key in encoded form */
751
0
    memcpy(enc, sender_pub, sender_publen);
752
0
    *enclen = sender_publen;
753
0
    *secretlen = info->Nsecret;
754
0
    ret = 1;
755
0
err:
756
0
    EC_KEY_free(sender_ephemkey);
757
0
    return ret;
758
0
}
759
760
/*
761
 * Do a DHKEM decapsulate operation.
762
 * See Section 4.1 Decap() and Auth Decap()
763
 *
764
 * Params:
765
 *     ctx A context object holding the recipients private key and the
766
 *         optional senders auth public key.
767
 *     secret A buffer to return the calculated shared secret. Setting this to
768
 *            NULL can be used to return the secretlen.
769
 *     secretlen Passes in the max size of the secret buffer and returns the
770
 *               secret length.
771
 *     enc A buffer containing the senders ephemeral public key that was returned
772
 *         from dhkem_encap().
773
 *     enclen The length in bytes of enc.
774
 * Returns: 1 If the shared secret is returned or 0 on error.
775
 */
776
static int dhkem_decap(PROV_EC_CTX *ctx,
777
    unsigned char *secret, size_t *secretlen,
778
    const unsigned char *enc, size_t enclen)
779
0
{
780
0
    int ret = 0;
781
0
    EC_KEY *sender_ephempubkey = NULL;
782
0
    const OSSL_HPKE_KEM_INFO *info = ctx->info;
783
0
    unsigned char recipient_pub[OSSL_HPKE_MAX_PUBLIC];
784
0
    size_t recipient_publen;
785
0
    size_t encodedpublen = info->Npk;
786
787
0
    if (secret == NULL) {
788
0
        *secretlen = info->Nsecret;
789
0
        return 1;
790
0
    }
791
792
0
    if (*secretlen < info->Nsecret) {
793
0
        ERR_raise_data(ERR_LIB_PROV, PROV_R_BAD_LENGTH, "*secretlen too small");
794
0
        return 0;
795
0
    }
796
0
    if (enclen != encodedpublen) {
797
0
        ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY, "Invalid enc public key");
798
0
        return 0;
799
0
    }
800
801
0
    sender_ephempubkey = eckey_frompub(ctx->recipient_key, enc, enclen);
802
0
    if (sender_ephempubkey == NULL)
803
0
        goto err;
804
0
    if (!ecpubkey_todata(ctx->recipient_key, recipient_pub, &recipient_publen,
805
0
            sizeof(recipient_pub)))
806
0
        goto err;
807
0
    if (recipient_publen != encodedpublen) {
808
0
        ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY, "Invalid recipient public key");
809
0
        goto err;
810
0
    }
811
812
0
    if (!derive_secret(ctx, secret,
813
0
            ctx->recipient_key, sender_ephempubkey,
814
0
            ctx->recipient_key, ctx->sender_authkey,
815
0
            enc, recipient_pub))
816
0
        goto err;
817
0
    *secretlen = info->Nsecret;
818
0
    ret = 1;
819
0
err:
820
0
    EC_KEY_free(sender_ephempubkey);
821
0
    return ret;
822
0
}
823
824
static int eckem_encapsulate(void *vctx, unsigned char *out, size_t *outlen,
825
    unsigned char *secret, size_t *secretlen)
826
0
{
827
0
    PROV_EC_CTX *ctx = (PROV_EC_CTX *)vctx;
828
829
0
    switch (ctx->mode) {
830
0
    case KEM_MODE_DHKEM:
831
0
        return dhkem_encap(ctx, out, outlen, secret, secretlen);
832
0
    default:
833
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);
834
0
        return -2;
835
0
    }
836
0
}
837
838
static int eckem_decapsulate(void *vctx, unsigned char *out, size_t *outlen,
839
    const unsigned char *in, size_t inlen)
840
0
{
841
0
    PROV_EC_CTX *ctx = (PROV_EC_CTX *)vctx;
842
843
0
    switch (ctx->mode) {
844
0
    case KEM_MODE_DHKEM:
845
0
        return dhkem_decap(ctx, out, outlen, in, inlen);
846
0
    default:
847
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);
848
0
        return -2;
849
0
    }
850
0
}
851
852
const OSSL_DISPATCH ossl_ec_asym_kem_functions[] = {
853
    { OSSL_FUNC_KEM_NEWCTX, (void (*)(void))eckem_newctx },
854
    { OSSL_FUNC_KEM_ENCAPSULATE_INIT,
855
        (void (*)(void))eckem_encapsulate_init },
856
    { OSSL_FUNC_KEM_ENCAPSULATE, (void (*)(void))eckem_encapsulate },
857
    { OSSL_FUNC_KEM_DECAPSULATE_INIT,
858
        (void (*)(void))eckem_decapsulate_init },
859
    { OSSL_FUNC_KEM_DECAPSULATE, (void (*)(void))eckem_decapsulate },
860
    { OSSL_FUNC_KEM_FREECTX, (void (*)(void))eckem_freectx },
861
    { OSSL_FUNC_KEM_SET_CTX_PARAMS,
862
        (void (*)(void))eckem_set_ctx_params },
863
    { OSSL_FUNC_KEM_SETTABLE_CTX_PARAMS,
864
        (void (*)(void))eckem_settable_ctx_params },
865
    { OSSL_FUNC_KEM_AUTH_ENCAPSULATE_INIT,
866
        (void (*)(void))eckem_auth_encapsulate_init },
867
    { OSSL_FUNC_KEM_AUTH_DECAPSULATE_INIT,
868
        (void (*)(void))eckem_auth_decapsulate_init },
869
    OSSL_DISPATCH_END
870
};