Coverage Report

Created: 2025-11-07 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/providers/implementations/exchange/ecdh_exch.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
/*
11
 * ECDH low level APIs are deprecated for public use, but still ok for
12
 * internal use.
13
 */
14
#include "internal/deprecated.h"
15
16
#include <string.h>
17
#include <openssl/crypto.h>
18
#include <openssl/evp.h>
19
#include <openssl/core_dispatch.h>
20
#include <openssl/core_names.h>
21
#include <openssl/ec.h>
22
#include <openssl/params.h>
23
#include <openssl/err.h>
24
#include <openssl/proverr.h>
25
#include "internal/cryptlib.h"
26
#include "prov/provider_ctx.h"
27
#include "prov/providercommon.h"
28
#include "prov/implementations.h"
29
#include "prov/securitycheck.h"
30
#include "crypto/ec.h" /* ossl_ecdh_kdf_X9_63() */
31
#include "providers/implementations/exchange/ecdh_exch.inc"
32
33
static OSSL_FUNC_keyexch_newctx_fn ecdh_newctx;
34
static OSSL_FUNC_keyexch_init_fn ecdh_init;
35
static OSSL_FUNC_keyexch_set_peer_fn ecdh_set_peer;
36
static OSSL_FUNC_keyexch_derive_fn ecdh_derive;
37
static OSSL_FUNC_keyexch_derive_skey_fn ecdh_derive_skey;
38
static OSSL_FUNC_keyexch_freectx_fn ecdh_freectx;
39
static OSSL_FUNC_keyexch_dupctx_fn ecdh_dupctx;
40
static OSSL_FUNC_keyexch_set_ctx_params_fn ecdh_set_ctx_params;
41
static OSSL_FUNC_keyexch_settable_ctx_params_fn ecdh_settable_ctx_params;
42
static OSSL_FUNC_keyexch_get_ctx_params_fn ecdh_get_ctx_params;
43
static OSSL_FUNC_keyexch_gettable_ctx_params_fn ecdh_gettable_ctx_params;
44
45
enum kdf_type {
46
    PROV_ECDH_KDF_NONE = 0,
47
    PROV_ECDH_KDF_X9_63
48
};
49
50
/*
51
 * What's passed as an actual key is defined by the KEYMGMT interface.
52
 * We happen to know that our KEYMGMT simply passes EC_KEY structures, so
53
 * we use that here too.
54
 */
55
56
typedef struct {
57
    OSSL_LIB_CTX *libctx;
58
59
    EC_KEY *k;
60
    EC_KEY *peerk;
61
62
    /*
63
     * ECDH cofactor mode:
64
     *
65
     *  . 0  disabled
66
     *  . 1  enabled
67
     *  . -1 use cofactor mode set for k
68
     */
69
    int cofactor_mode;
70
71
    /************
72
     * ECDH KDF *
73
     ************/
74
    /* KDF (if any) to use for ECDH */
75
    enum kdf_type kdf_type;
76
    /* Message digest to use for key derivation */
77
    EVP_MD *kdf_md;
78
    /* User key material */
79
    unsigned char *kdf_ukm;
80
    size_t kdf_ukmlen;
81
    /* KDF output length */
82
    size_t kdf_outlen;
83
    OSSL_FIPS_IND_DECLARE
84
} PROV_ECDH_CTX;
85
86
static
87
void *ecdh_newctx(void *provctx)
88
0
{
89
0
    PROV_ECDH_CTX *pectx;
90
91
0
    if (!ossl_prov_is_running())
92
0
        return NULL;
93
94
0
    pectx = OPENSSL_zalloc(sizeof(*pectx));
95
0
    if (pectx == NULL)
96
0
        return NULL;
97
98
0
    pectx->libctx = PROV_LIBCTX_OF(provctx);
99
0
    pectx->cofactor_mode = -1;
100
0
    pectx->kdf_type = PROV_ECDH_KDF_NONE;
101
0
    OSSL_FIPS_IND_INIT(pectx)
102
103
0
    return (void *)pectx;
104
0
}
105
106
static
107
int ecdh_init(void *vpecdhctx, void *vecdh, const OSSL_PARAM params[])
108
0
{
109
0
    PROV_ECDH_CTX *pecdhctx = (PROV_ECDH_CTX *)vpecdhctx;
110
111
0
    if (!ossl_prov_is_running()
112
0
            || pecdhctx == NULL
113
0
            || vecdh == NULL
114
0
            || (EC_KEY_get0_group(vecdh) == NULL)
115
0
            || !EC_KEY_up_ref(vecdh))
116
0
        return 0;
117
0
    EC_KEY_free(pecdhctx->k);
118
0
    pecdhctx->k = vecdh;
119
0
    pecdhctx->cofactor_mode = -1;
120
0
    pecdhctx->kdf_type = PROV_ECDH_KDF_NONE;
121
122
0
    OSSL_FIPS_IND_SET_APPROVED(pecdhctx)
123
0
    if (!ecdh_set_ctx_params(pecdhctx, params))
124
0
        return 0;
125
#ifdef FIPS_MODULE
126
    if (!ossl_fips_ind_ec_key_check(OSSL_FIPS_IND_GET(pecdhctx),
127
                                    OSSL_FIPS_IND_SETTABLE0, pecdhctx->libctx,
128
                                    EC_KEY_get0_group(vecdh), "ECDH Init", 1))
129
        return 0;
130
#endif
131
0
    return 1;
132
0
}
133
134
static
135
int ecdh_match_params(const EC_KEY *priv, const EC_KEY *peer)
136
0
{
137
0
    int ret;
138
0
    BN_CTX *ctx = NULL;
139
0
    const EC_GROUP *group_priv = EC_KEY_get0_group(priv);
140
0
    const EC_GROUP *group_peer = EC_KEY_get0_group(peer);
141
142
0
    ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(priv));
143
0
    if (ctx == NULL) {
144
0
        ERR_raise(ERR_LIB_PROV, ERR_R_BN_LIB);
145
0
        return 0;
146
0
    }
147
0
    ret = group_priv != NULL
148
0
          && group_peer != NULL
149
0
          && EC_GROUP_cmp(group_priv, group_peer, ctx) == 0;
150
0
    if (!ret)
151
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISMATCHING_DOMAIN_PARAMETERS);
152
0
    BN_CTX_free(ctx);
153
0
    return ret;
154
0
}
155
156
static
157
int ecdh_set_peer(void *vpecdhctx, void *vecdh)
158
0
{
159
0
    PROV_ECDH_CTX *pecdhctx = (PROV_ECDH_CTX *)vpecdhctx;
160
161
0
    if (!ossl_prov_is_running()
162
0
            || pecdhctx == NULL
163
0
            || vecdh == NULL
164
0
            || !ecdh_match_params(pecdhctx->k, vecdh))
165
0
        return 0;
166
#ifdef FIPS_MODULE
167
    if (!ossl_fips_ind_ec_key_check(OSSL_FIPS_IND_GET(pecdhctx),
168
                                    OSSL_FIPS_IND_SETTABLE0, pecdhctx->libctx,
169
                                    EC_KEY_get0_group(vecdh), "ECDH Set Peer",
170
                                    1))
171
        return 0;
172
#endif
173
0
    if (!EC_KEY_up_ref(vecdh))
174
0
        return 0;
175
176
0
    EC_KEY_free(pecdhctx->peerk);
177
0
    pecdhctx->peerk = vecdh;
178
0
    return 1;
179
0
}
180
181
static
182
void ecdh_freectx(void *vpecdhctx)
183
0
{
184
0
    PROV_ECDH_CTX *pecdhctx = (PROV_ECDH_CTX *)vpecdhctx;
185
186
0
    EC_KEY_free(pecdhctx->k);
187
0
    EC_KEY_free(pecdhctx->peerk);
188
189
0
    EVP_MD_free(pecdhctx->kdf_md);
190
0
    OPENSSL_clear_free(pecdhctx->kdf_ukm, pecdhctx->kdf_ukmlen);
191
192
0
    OPENSSL_free(pecdhctx);
193
0
}
194
195
static
196
void *ecdh_dupctx(void *vpecdhctx)
197
0
{
198
0
    PROV_ECDH_CTX *srcctx = (PROV_ECDH_CTX *)vpecdhctx;
199
0
    PROV_ECDH_CTX *dstctx;
200
201
0
    if (!ossl_prov_is_running())
202
0
        return NULL;
203
204
0
    dstctx = OPENSSL_zalloc(sizeof(*srcctx));
205
0
    if (dstctx == NULL)
206
0
        return NULL;
207
208
0
    *dstctx = *srcctx;
209
210
    /* clear all pointers */
211
212
0
    dstctx->k= NULL;
213
0
    dstctx->peerk = NULL;
214
0
    dstctx->kdf_md = NULL;
215
0
    dstctx->kdf_ukm = NULL;
216
217
    /* up-ref all ref-counted objects referenced in dstctx */
218
219
0
    if (srcctx->k != NULL && !EC_KEY_up_ref(srcctx->k))
220
0
        goto err;
221
0
    else
222
0
        dstctx->k = srcctx->k;
223
224
0
    if (srcctx->peerk != NULL && !EC_KEY_up_ref(srcctx->peerk))
225
0
        goto err;
226
0
    else
227
0
        dstctx->peerk = srcctx->peerk;
228
229
0
    if (srcctx->kdf_md != NULL && !EVP_MD_up_ref(srcctx->kdf_md))
230
0
        goto err;
231
0
    else
232
0
        dstctx->kdf_md = srcctx->kdf_md;
233
234
    /* Duplicate UKM data if present */
235
0
    if (srcctx->kdf_ukm != NULL && srcctx->kdf_ukmlen > 0) {
236
0
        dstctx->kdf_ukm = OPENSSL_memdup(srcctx->kdf_ukm,
237
0
                                         srcctx->kdf_ukmlen);
238
0
        if (dstctx->kdf_ukm == NULL)
239
0
            goto err;
240
0
    }
241
242
0
    return dstctx;
243
244
0
 err:
245
0
    ecdh_freectx(dstctx);
246
0
    return NULL;
247
0
}
248
249
static
250
int ecdh_set_ctx_params(void *vpecdhctx, const OSSL_PARAM params[])
251
0
{
252
0
    char name[80] = { '\0' }; /* should be big enough */
253
0
    char *str = NULL;
254
0
    PROV_ECDH_CTX *pectx = (PROV_ECDH_CTX *)vpecdhctx;
255
0
    struct ecdh_set_ctx_params_st p;
256
257
0
    if (pectx == NULL || !ecdh_set_ctx_params_decoder(params, &p))
258
0
        return 0;
259
260
0
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(pectx, OSSL_FIPS_IND_SETTABLE0, p.ind_k))
261
0
        return 0;
262
0
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(pectx, OSSL_FIPS_IND_SETTABLE1, p.ind_d))
263
0
        return 0;
264
0
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(pectx, OSSL_FIPS_IND_SETTABLE2, p.ind_cofac))
265
0
        return 0;
266
267
0
    if (p.mode != NULL) {
268
0
        int mode;
269
270
0
        if (!OSSL_PARAM_get_int(p.mode, &mode))
271
0
            return 0;
272
273
0
        if (mode < -1 || mode > 1)
274
0
            return 0;
275
276
0
        pectx->cofactor_mode = mode;
277
0
    }
278
279
0
    if (p.kdf != NULL) {
280
0
        str = name;
281
0
        if (!OSSL_PARAM_get_utf8_string(p.kdf, &str, sizeof(name)))
282
0
            return 0;
283
284
0
        if (name[0] == '\0')
285
0
            pectx->kdf_type = PROV_ECDH_KDF_NONE;
286
0
        else if (strcmp(name, OSSL_KDF_NAME_X963KDF) == 0)
287
0
            pectx->kdf_type = PROV_ECDH_KDF_X9_63;
288
0
        else
289
0
            return 0;
290
0
    }
291
292
0
    if (p.digest != NULL) {
293
0
        char mdprops[80] = { '\0' }; /* should be big enough */
294
295
0
        str = name;
296
0
        if (!OSSL_PARAM_get_utf8_string(p.digest, &str, sizeof(name)))
297
0
            return 0;
298
299
0
        str = mdprops;
300
0
        if (p.propq != NULL) {
301
0
            if (!OSSL_PARAM_get_utf8_string(p.propq, &str, sizeof(mdprops)))
302
0
                return 0;
303
0
        }
304
305
0
        EVP_MD_free(pectx->kdf_md);
306
0
        pectx->kdf_md = EVP_MD_fetch(pectx->libctx, name, mdprops);
307
0
        if (pectx->kdf_md == NULL)
308
0
            return 0;
309
        /* XOF digests are not allowed */
310
0
        if (EVP_MD_xof(pectx->kdf_md)) {
311
0
            ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED);
312
0
            return 0;
313
0
        }
314
#ifdef FIPS_MODULE
315
        if (!ossl_fips_ind_digest_exch_check(OSSL_FIPS_IND_GET(pectx),
316
                                             OSSL_FIPS_IND_SETTABLE1, pectx->libctx,
317
                                             pectx->kdf_md, "ECDH Set Ctx")) {
318
            EVP_MD_free(pectx->kdf_md);
319
            pectx->kdf_md = NULL;
320
            return 0;
321
        }
322
#endif
323
0
    }
324
325
0
    if (p.len != NULL) {
326
0
        size_t outlen;
327
328
0
        if (!OSSL_PARAM_get_size_t(p.len, &outlen))
329
0
            return 0;
330
0
        pectx->kdf_outlen = outlen;
331
0
    }
332
333
0
    if (p.ukm != NULL) {
334
0
        void *tmp_ukm = NULL;
335
0
        size_t tmp_ukmlen;
336
337
0
        if (!OSSL_PARAM_get_octet_string(p.ukm, &tmp_ukm, 0, &tmp_ukmlen))
338
0
            return 0;
339
0
        OPENSSL_free(pectx->kdf_ukm);
340
0
        pectx->kdf_ukm = tmp_ukm;
341
0
        pectx->kdf_ukmlen = tmp_ukmlen;
342
0
    }
343
344
0
    return 1;
345
0
}
346
347
static
348
const OSSL_PARAM *ecdh_settable_ctx_params(ossl_unused void *vpecdhctx,
349
                                           ossl_unused void *provctx)
350
0
{
351
0
    return ecdh_set_ctx_params_list;
352
0
}
353
354
static
355
int ecdh_get_ctx_params(void *vpecdhctx, OSSL_PARAM params[])
356
0
{
357
0
    PROV_ECDH_CTX *pectx = (PROV_ECDH_CTX *)vpecdhctx;
358
0
    struct ecdh_get_ctx_params_st p;
359
360
0
    if (pectx == NULL || !ecdh_get_ctx_params_decoder(params, &p))
361
0
        return 0;
362
363
0
    if (p.mode != NULL) {
364
0
        int mode = pectx->cofactor_mode;
365
366
0
        if (mode == -1) {
367
            /* check what is the default for pecdhctx->k */
368
0
            mode = EC_KEY_get_flags(pectx->k) & EC_FLAG_COFACTOR_ECDH ? 1 : 0;
369
0
        }
370
371
0
        if (!OSSL_PARAM_set_int(p.mode, mode))
372
0
            return 0;
373
0
    }
374
375
0
    if (p.kdf != NULL) {
376
0
        const char *kdf_type = NULL;
377
378
0
        switch (pectx->kdf_type) {
379
0
            case PROV_ECDH_KDF_NONE:
380
0
                kdf_type = "";
381
0
                break;
382
0
            case PROV_ECDH_KDF_X9_63:
383
0
                kdf_type = OSSL_KDF_NAME_X963KDF;
384
0
                break;
385
0
            default:
386
0
                return 0;
387
0
        }
388
389
0
        if (!OSSL_PARAM_set_utf8_string(p.kdf, kdf_type))
390
0
            return 0;
391
0
    }
392
393
0
    if (p.digest != NULL
394
0
            && !OSSL_PARAM_set_utf8_string(p.digest, pectx->kdf_md == NULL
395
0
                                           ? ""
396
0
                                           : EVP_MD_get0_name(pectx->kdf_md))) {
397
0
        return 0;
398
0
    }
399
400
0
    if (p.len != NULL && !OSSL_PARAM_set_size_t(p.len, pectx->kdf_outlen))
401
0
        return 0;
402
403
0
    if (p.ukm != NULL &&
404
0
        !OSSL_PARAM_set_octet_ptr(p.ukm, pectx->kdf_ukm, pectx->kdf_ukmlen))
405
0
        return 0;
406
407
0
    if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(pectx, p.ind))
408
0
        return 0;
409
0
    return 1;
410
0
}
411
412
static
413
const OSSL_PARAM *ecdh_gettable_ctx_params(ossl_unused void *vpecdhctx,
414
                                           ossl_unused void *provctx)
415
0
{
416
0
    return ecdh_get_ctx_params_list;
417
0
}
418
419
static ossl_inline
420
size_t ecdh_size(const EC_KEY *k)
421
0
{
422
0
    size_t degree = 0;
423
0
    const EC_GROUP *group;
424
425
0
    if (k == NULL
426
0
            || (group = EC_KEY_get0_group(k)) == NULL)
427
0
        return 0;
428
429
0
    degree = EC_GROUP_get_degree(group);
430
431
0
    return (degree + 7) / 8;
432
0
}
433
434
static ossl_inline
435
int ecdh_plain_derive(void *vpecdhctx, unsigned char *secret,
436
                      size_t *psecretlen, size_t outlen)
437
0
{
438
0
    PROV_ECDH_CTX *pecdhctx = (PROV_ECDH_CTX *)vpecdhctx;
439
0
    int retlen, ret = 0;
440
0
    size_t ecdhsize, size;
441
0
    const EC_POINT *ppubkey = NULL;
442
0
    EC_KEY *privk = NULL;
443
0
    const EC_GROUP *group;
444
0
    const BIGNUM *cofactor;
445
0
    int key_cofactor_mode;
446
0
    int has_cofactor;
447
#ifdef FIPS_MODULE
448
    int cofactor_approved = 0;
449
#endif
450
451
0
    if (pecdhctx->k == NULL || pecdhctx->peerk == NULL) {
452
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
453
0
        return 0;
454
0
    }
455
456
0
    ecdhsize = ecdh_size(pecdhctx->k);
457
0
    if (secret == NULL) {
458
0
        *psecretlen = ecdhsize;
459
0
        return 1;
460
0
    }
461
462
0
    if ((group = EC_KEY_get0_group(pecdhctx->k)) == NULL
463
0
            || (cofactor = EC_GROUP_get0_cofactor(group)) == NULL)
464
0
        return 0;
465
466
0
    has_cofactor = !BN_is_one(cofactor);
467
468
    /*
469
     * NB: unlike PKCS#3 DH, if outlen is less than maximum size this is not
470
     * an error, the result is truncated.
471
     */
472
0
    size = outlen < ecdhsize ? outlen : ecdhsize;
473
474
    /*
475
     * The ctx->cofactor_mode flag has precedence over the
476
     * cofactor_mode flag set on ctx->k.
477
     *
478
     * - if ctx->cofactor_mode == -1, use ctx->k directly
479
     * - if ctx->cofactor_mode == key_cofactor_mode, use ctx->k directly
480
     * - if ctx->cofactor_mode != key_cofactor_mode:
481
     *     - if ctx->k->cofactor == 1, the cofactor_mode flag is irrelevant, use
482
     *          ctx->k directly
483
     *     - if ctx->k->cofactor != 1, use a duplicate of ctx->k with the flag
484
     *          set to ctx->cofactor_mode
485
     */
486
0
    key_cofactor_mode =
487
0
        (EC_KEY_get_flags(pecdhctx->k) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
488
0
    if (pecdhctx->cofactor_mode != -1
489
0
            && pecdhctx->cofactor_mode != key_cofactor_mode
490
0
            && has_cofactor) {
491
0
        if ((privk = EC_KEY_dup(pecdhctx->k)) == NULL)
492
0
            return 0;
493
494
0
        if (pecdhctx->cofactor_mode == 1) {
495
0
            EC_KEY_set_flags(privk, EC_FLAG_COFACTOR_ECDH);
496
#ifdef FIPS_MODULE
497
            cofactor_approved = 1;
498
#endif
499
0
        } else {
500
0
            EC_KEY_clear_flags(privk, EC_FLAG_COFACTOR_ECDH);
501
0
        }
502
0
    } else {
503
0
        privk = pecdhctx->k;
504
#ifdef FIPS_MODULE
505
        cofactor_approved = key_cofactor_mode;
506
#endif
507
0
    }
508
509
#ifdef FIPS_MODULE
510
    /*
511
     * SP800-56A r3 Section 5.7.1.2 requires ECC Cofactor DH to be used.
512
     * This applies to the 'B' and 'K' curves that have cofactors that are not 1.
513
     */
514
    if (has_cofactor && !cofactor_approved) {
515
        if (!OSSL_FIPS_IND_ON_UNAPPROVED(pecdhctx, OSSL_FIPS_IND_SETTABLE2,
516
                                         pecdhctx->libctx, "ECDH", "Cofactor",
517
                                         ossl_fips_config_ecdh_cofactor_check)) {
518
            ERR_raise(ERR_LIB_PROV, PROV_R_COFACTOR_REQUIRED);
519
            goto end;
520
        }
521
    }
522
#endif
523
524
0
    ppubkey = EC_KEY_get0_public_key(pecdhctx->peerk);
525
526
0
    retlen = ECDH_compute_key(secret, size, ppubkey, privk, NULL);
527
528
0
    if (retlen <= 0)
529
0
        goto end;
530
531
0
    *psecretlen = retlen;
532
0
    ret = 1;
533
534
0
 end:
535
0
    if (privk != pecdhctx->k)
536
0
        EC_KEY_free(privk);
537
0
    return ret;
538
0
}
539
540
static ossl_inline
541
int ecdh_X9_63_kdf_derive(void *vpecdhctx, unsigned char *secret,
542
                          size_t *psecretlen, size_t outlen)
543
0
{
544
0
    PROV_ECDH_CTX *pecdhctx = (PROV_ECDH_CTX *)vpecdhctx;
545
0
    unsigned char *stmp = NULL;
546
0
    size_t stmplen;
547
0
    int ret = 0;
548
549
0
    if (secret == NULL) {
550
0
        *psecretlen = pecdhctx->kdf_outlen;
551
0
        return 1;
552
0
    }
553
554
0
    if (pecdhctx->kdf_outlen > outlen) {
555
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
556
0
        return 0;
557
0
    }
558
0
    if (!ecdh_plain_derive(vpecdhctx, NULL, &stmplen, 0))
559
0
        return 0;
560
0
    if ((stmp = OPENSSL_secure_malloc(stmplen)) == NULL)
561
0
        return 0;
562
0
    if (!ecdh_plain_derive(vpecdhctx, stmp, &stmplen, stmplen))
563
0
        goto err;
564
565
    /* Do KDF stuff */
566
0
    if (!ossl_ecdh_kdf_X9_63(secret, pecdhctx->kdf_outlen,
567
0
                             stmp, stmplen,
568
0
                             pecdhctx->kdf_ukm,
569
0
                             pecdhctx->kdf_ukmlen,
570
0
                             pecdhctx->kdf_md,
571
0
                             pecdhctx->libctx, NULL))
572
0
        goto err;
573
0
    *psecretlen = pecdhctx->kdf_outlen;
574
0
    ret = 1;
575
576
0
 err:
577
0
    OPENSSL_secure_clear_free(stmp, stmplen);
578
0
    return ret;
579
0
}
580
581
static
582
int ecdh_derive(void *vpecdhctx, unsigned char *secret,
583
                size_t *psecretlen, size_t outlen)
584
0
{
585
0
    PROV_ECDH_CTX *pecdhctx = (PROV_ECDH_CTX *)vpecdhctx;
586
587
0
    switch (pecdhctx->kdf_type) {
588
0
        case PROV_ECDH_KDF_NONE:
589
0
            return ecdh_plain_derive(vpecdhctx, secret, psecretlen, outlen);
590
0
        case PROV_ECDH_KDF_X9_63:
591
0
            return ecdh_X9_63_kdf_derive(vpecdhctx, secret, psecretlen, outlen);
592
0
        default:
593
0
            break;
594
0
    }
595
0
    return 0;
596
0
}
597
598
static
599
void *ecdh_derive_skey(void *vpecdhctx, const char *key_type ossl_unused,
600
                       void *provctx, OSSL_FUNC_skeymgmt_import_fn *import,
601
                       size_t outlen, const OSSL_PARAM params_in[] ossl_unused)
602
0
{
603
0
    unsigned char *secret = NULL;
604
0
    size_t secretlen = 0;
605
0
    void *ret = NULL;
606
0
    OSSL_PARAM params[2] = {OSSL_PARAM_END, OSSL_PARAM_END};
607
608
0
    if (import == NULL || outlen == 0)
609
0
        return NULL;
610
611
0
    if (ecdh_derive(vpecdhctx, secret, &secretlen, outlen) == 0)
612
0
        return NULL;
613
614
0
    if ((secret = OPENSSL_malloc(secretlen)) == NULL)
615
0
        return NULL;
616
617
0
    if (ecdh_derive(vpecdhctx, secret, &secretlen, outlen) == 0
618
0
        || secretlen != outlen) {
619
0
        OPENSSL_clear_free(secret, secretlen);
620
0
        return NULL;
621
0
    }
622
623
0
    params[0] = OSSL_PARAM_construct_octet_string(OSSL_SKEY_PARAM_RAW_BYTES,
624
0
                                                  (void *)secret, outlen);
625
626
    /* This is mandatory, no need to check for its presence */
627
0
    ret = import(provctx, OSSL_SKEYMGMT_SELECT_SECRET_KEY, params);
628
0
    OPENSSL_clear_free(secret, secretlen);
629
630
0
    return ret;
631
0
}
632
633
const OSSL_DISPATCH ossl_ecdh_keyexch_functions[] = {
634
    { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))ecdh_newctx },
635
    { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))ecdh_init },
636
    { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))ecdh_derive },
637
    { OSSL_FUNC_KEYEXCH_DERIVE_SKEY, (void (*)(void))ecdh_derive_skey },
638
    { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))ecdh_set_peer },
639
    { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))ecdh_freectx },
640
    { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))ecdh_dupctx },
641
    { OSSL_FUNC_KEYEXCH_SET_CTX_PARAMS, (void (*)(void))ecdh_set_ctx_params },
642
    { OSSL_FUNC_KEYEXCH_SETTABLE_CTX_PARAMS,
643
      (void (*)(void))ecdh_settable_ctx_params },
644
    { OSSL_FUNC_KEYEXCH_GET_CTX_PARAMS, (void (*)(void))ecdh_get_ctx_params },
645
    { OSSL_FUNC_KEYEXCH_GETTABLE_CTX_PARAMS,
646
      (void (*)(void))ecdh_gettable_ctx_params },
647
    OSSL_DISPATCH_END
648
};