Coverage Report

Created: 2025-12-10 06:24

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