blob: b276c6adadf4c4845950b540c831b2c8e0620f03 [file] [log] [blame]
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +01001/*
2 * Elliptic curve Diffie-Hellman
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +01006 */
7
8/*
9 * References:
10 *
Xiaokang Qian47041472023-04-12 06:07:23 +000011 * SEC1 https://www.secg.org/sec1-v2.pdf
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010012 * RFC 4492
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010013 */
14
Gilles Peskinedb09ef62020-06-03 01:43:33 +020015#include "common.h"
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020017#if defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010018
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000019#include "mbedtls/ecdh.h"
Hanno Becker91796d72018-12-17 18:10:51 +000020#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000021#include "mbedtls/error.h"
Jerry Yubdc71882021-09-14 19:30:36 +080022
Rich Evans00ab4702015-02-06 13:43:58 +000023#include <string.h>
24
Janos Follath5a3e1bf2018-08-13 15:54:22 +010025#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
26typedef mbedtls_ecdh_context mbedtls_ecdh_context_mbed;
27#endif
28
Gilles Peskine30816292019-02-22 12:31:25 +010029static mbedtls_ecp_group_id mbedtls_ecdh_grp_id(
Gilles Peskine449bd832023-01-11 14:50:10 +010030 const mbedtls_ecdh_context *ctx)
Gilles Peskine30816292019-02-22 12:31:25 +010031{
32#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine449bd832023-01-11 14:50:10 +010033 return ctx->grp.id;
Gilles Peskine30816292019-02-22 12:31:25 +010034#else
Gilles Peskine449bd832023-01-11 14:50:10 +010035 return ctx->grp_id;
Gilles Peskine30816292019-02-22 12:31:25 +010036#endif
37}
38
Gilles Peskine449bd832023-01-11 14:50:10 +010039int mbedtls_ecdh_can_do(mbedtls_ecp_group_id gid)
Gilles Peskine20b3ef32019-02-11 18:41:27 +010040{
41 /* At this time, all groups support ECDH. */
42 (void) gid;
Gilles Peskine449bd832023-01-11 14:50:10 +010043 return 1;
Gilles Peskine20b3ef32019-02-11 18:41:27 +010044}
45
Ron Eldora84c1cb2017-10-10 19:04:27 +030046#if !defined(MBEDTLS_ECDH_GEN_PUBLIC_ALT)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010047/*
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020048 * Generate public key (restartable version)
Manuel Pégourié-Gonnardc0edc962018-10-16 10:38:19 +020049 *
50 * Note: this internal function relies on its caller preserving the value of
Manuel Pégourié-Gonnardca29fdf2018-10-22 09:56:53 +020051 * the output parameter 'd' across continuation calls. This would not be
Manuel Pégourié-Gonnardc0edc962018-10-16 10:38:19 +020052 * acceptable for a public function but is OK here as we control call sites.
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020053 */
Gilles Peskine449bd832023-01-11 14:50:10 +010054static int ecdh_gen_public_restartable(mbedtls_ecp_group *grp,
55 mbedtls_mpi *d, mbedtls_ecp_point *Q,
56 int (*f_rng)(void *, unsigned char *, size_t),
57 void *p_rng,
58 mbedtls_ecp_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020059{
Janos Follath24eed8d2019-11-22 13:21:35 +000060 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020061
David Horstmann91e20a02022-10-06 19:11:28 +010062 int restarting = 0;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020063#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +010064 restarting = (rs_ctx != NULL && rs_ctx->rsm != NULL);
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020065#endif
David Horstmann91e20a02022-10-06 19:11:28 +010066 /* If multiplication is in progress, we already generated a privkey */
Gilles Peskine449bd832023-01-11 14:50:10 +010067 if (!restarting) {
68 MBEDTLS_MPI_CHK(mbedtls_ecp_gen_privkey(grp, d, f_rng, p_rng));
69 }
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020070
Gilles Peskine449bd832023-01-11 14:50:10 +010071 MBEDTLS_MPI_CHK(mbedtls_ecp_mul_restartable(grp, Q, d, &grp->G,
72 f_rng, p_rng, rs_ctx));
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020073
74cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +010075 return ret;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020076}
77
78/*
79 * Generate public key
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010080 */
Gilles Peskine449bd832023-01-11 14:50:10 +010081int mbedtls_ecdh_gen_public(mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
82 int (*f_rng)(void *, unsigned char *, size_t),
83 void *p_rng)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010084{
Gilles Peskine449bd832023-01-11 14:50:10 +010085 return ecdh_gen_public_restartable(grp, d, Q, f_rng, p_rng, NULL);
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010086}
Ron Eldor936d2842018-11-01 13:05:52 +020087#endif /* !MBEDTLS_ECDH_GEN_PUBLIC_ALT */
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010088
Ron Eldora84c1cb2017-10-10 19:04:27 +030089#if !defined(MBEDTLS_ECDH_COMPUTE_SHARED_ALT)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010090/*
91 * Compute shared secret (SEC1 3.3.1)
92 */
Gilles Peskine449bd832023-01-11 14:50:10 +010093static int ecdh_compute_shared_restartable(mbedtls_ecp_group *grp,
94 mbedtls_mpi *z,
95 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
96 int (*f_rng)(void *, unsigned char *, size_t),
97 void *p_rng,
98 mbedtls_ecp_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010099{
Janos Follath24eed8d2019-11-22 13:21:35 +0000100 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 mbedtls_ecp_point P;
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100102
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 mbedtls_ecp_point_init(&P);
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100104
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 MBEDTLS_MPI_CHK(mbedtls_ecp_mul_restartable(grp, &P, d, Q,
106 f_rng, p_rng, rs_ctx));
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 if (mbedtls_ecp_is_zero(&P)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Paul Bakkerb548d772013-07-26 14:21:34 +0200110 goto cleanup;
111 }
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(z, &P.X));
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100114
115cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 mbedtls_ecp_point_free(&P);
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 return ret;
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100119}
120
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100121/*
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200122 * Compute shared secret (SEC1 3.3.1)
123 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100124int mbedtls_ecdh_compute_shared(mbedtls_ecp_group *grp, mbedtls_mpi *z,
125 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
126 int (*f_rng)(void *, unsigned char *, size_t),
127 void *p_rng)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200128{
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 return ecdh_compute_shared_restartable(grp, z, Q, d,
130 f_rng, p_rng, NULL);
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200131}
Ron Eldor936d2842018-11-01 13:05:52 +0200132#endif /* !MBEDTLS_ECDH_COMPUTE_SHARED_ALT */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134static void ecdh_init_internal(mbedtls_ecdh_context_mbed *ctx)
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100135{
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 mbedtls_ecp_group_init(&ctx->grp);
137 mbedtls_mpi_init(&ctx->d);
138 mbedtls_ecp_point_init(&ctx->Q);
139 mbedtls_ecp_point_init(&ctx->Qp);
140 mbedtls_mpi_init(&ctx->z);
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200141
142#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 mbedtls_ecp_restart_init(&ctx->rs);
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200144#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100145}
146
Minos Galanakisd7537382024-02-23 12:17:59 +0000147mbedtls_ecp_group_id mbedtls_ecdh_get_grp_id(mbedtls_ecdh_context *ctx)
148{
149#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
150 return ctx->MBEDTLS_PRIVATE(grp).id;
151#else
152 return ctx->MBEDTLS_PRIVATE(grp_id);
153#endif
154}
155
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100156/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100157 * Initialize context
Janos Follathf61e4862018-10-30 11:53:25 +0000158 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100159void mbedtls_ecdh_init(mbedtls_ecdh_context *ctx)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100160{
161#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 ecdh_init_internal(ctx);
163 mbedtls_ecp_point_init(&ctx->Vi);
164 mbedtls_ecp_point_init(&ctx->Vf);
165 mbedtls_mpi_init(&ctx->_d);
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100166#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 memset(ctx, 0, sizeof(mbedtls_ecdh_context));
Christoph M. Wintersteigerd8c45d52019-02-20 17:16:53 +0000168
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100169 ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
170#endif
171 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
172#if defined(MBEDTLS_ECP_RESTARTABLE)
173 ctx->restart_enabled = 0;
174#endif
175}
176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177static int ecdh_setup_internal(mbedtls_ecdh_context_mbed *ctx,
178 mbedtls_ecp_group_id grp_id)
Janos Follathf61e4862018-10-30 11:53:25 +0000179{
Janos Follath24eed8d2019-11-22 13:21:35 +0000180 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follathf61e4862018-10-30 11:53:25 +0000181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 ret = mbedtls_ecp_group_load(&ctx->grp, grp_id);
183 if (ret != 0) {
184 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Janos Follathf61e4862018-10-30 11:53:25 +0000185 }
186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 return 0;
Janos Follathf61e4862018-10-30 11:53:25 +0000188}
189
190/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100191 * Setup context
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100192 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100193int mbedtls_ecdh_setup(mbedtls_ecdh_context *ctx, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100194{
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100195#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 return ecdh_setup_internal(ctx, grp_id);
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100197#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 switch (grp_id) {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100199#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
Christoph M. Wintersteiger3ff60bc2019-02-15 12:59:59 +0000200 case MBEDTLS_ECP_DP_CURVE25519:
201 ctx->point_format = MBEDTLS_ECP_PF_COMPRESSED;
202 ctx->var = MBEDTLS_ECDH_VARIANT_EVEREST;
203 ctx->grp_id = grp_id;
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 return mbedtls_everest_setup(&ctx->ctx.everest_ecdh, grp_id);
Christoph M. Wintersteiger78c9c462018-12-06 17:16:32 +0000205#endif
Christoph M. Wintersteiger3ff60bc2019-02-15 12:59:59 +0000206 default:
207 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
208 ctx->var = MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0;
209 ctx->grp_id = grp_id;
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 ecdh_init_internal(&ctx->ctx.mbed_ecdh);
211 return ecdh_setup_internal(&ctx->ctx.mbed_ecdh, grp_id);
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100212 }
213#endif
214}
215
Gilles Peskine449bd832023-01-11 14:50:10 +0100216static void ecdh_free_internal(mbedtls_ecdh_context_mbed *ctx)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100217{
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 mbedtls_ecp_group_free(&ctx->grp);
219 mbedtls_mpi_free(&ctx->d);
220 mbedtls_ecp_point_free(&ctx->Q);
221 mbedtls_ecp_point_free(&ctx->Qp);
222 mbedtls_mpi_free(&ctx->z);
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200223
224#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 mbedtls_ecp_restart_free(&ctx->rs);
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200226#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100227}
228
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200229#if defined(MBEDTLS_ECP_RESTARTABLE)
230/*
231 * Enable restartable operations for context
232 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100233void mbedtls_ecdh_enable_restart(mbedtls_ecdh_context *ctx)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200234{
235 ctx->restart_enabled = 1;
236}
237#endif
238
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100239/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100240 * Free context
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100241 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100242void mbedtls_ecdh_free(mbedtls_ecdh_context *ctx)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100243{
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 if (ctx == NULL) {
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100245 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 }
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100247
248#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 mbedtls_ecp_point_free(&ctx->Vi);
250 mbedtls_ecp_point_free(&ctx->Vf);
251 mbedtls_mpi_free(&ctx->_d);
252 ecdh_free_internal(ctx);
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100253#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 switch (ctx->var) {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100255#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
256 case MBEDTLS_ECDH_VARIANT_EVEREST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 mbedtls_everest_free(&ctx->ctx.everest_ecdh);
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100258 break;
259#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100260 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 ecdh_free_internal(&ctx->ctx.mbed_ecdh);
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100262 break;
263 default:
264 break;
265 }
266
267 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
268 ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
269 ctx->grp_id = MBEDTLS_ECP_DP_NONE;
270#endif
271}
272
Gilles Peskine449bd832023-01-11 14:50:10 +0100273static int ecdh_make_params_internal(mbedtls_ecdh_context_mbed *ctx,
274 size_t *olen, int point_format,
275 unsigned char *buf, size_t blen,
276 int (*f_rng)(void *,
277 unsigned char *,
278 size_t),
279 void *p_rng,
280 int restart_enabled)
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100281{
Janos Follath24eed8d2019-11-22 13:21:35 +0000282 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100283 size_t grp_len, pt_len;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200284#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200285 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200286#endif
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 if (ctx->grp.pbits == 0) {
289 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
290 }
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100291
Ron Eldor19779c42018-11-05 16:58:13 +0200292#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 if (restart_enabled) {
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200294 rs_ctx = &ctx->rs;
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 }
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100296#else
297 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200298#endif
299
Ron Eldor8493f802018-11-01 11:32:15 +0200300
Ron Eldor2981d8f2018-11-05 18:07:10 +0200301#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 if ((ret = ecdh_gen_public_restartable(&ctx->grp, &ctx->d, &ctx->Q,
303 f_rng, p_rng, rs_ctx)) != 0) {
304 return ret;
305 }
Ron Eldor2981d8f2018-11-05 18:07:10 +0200306#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 if ((ret = mbedtls_ecdh_gen_public(&ctx->grp, &ctx->d, &ctx->Q,
308 f_rng, p_rng)) != 0) {
309 return ret;
310 }
Ron Eldor2981d8f2018-11-05 18:07:10 +0200311#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100312
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 if ((ret = mbedtls_ecp_tls_write_group(&ctx->grp, &grp_len, buf,
314 blen)) != 0) {
315 return ret;
316 }
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100317
318 buf += grp_len;
319 blen -= grp_len;
320
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 if ((ret = mbedtls_ecp_tls_write_point(&ctx->grp, &ctx->Q, point_format,
322 &pt_len, buf, blen)) != 0) {
323 return ret;
324 }
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100325
326 *olen = grp_len + pt_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 return 0;
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100328}
329
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100330/*
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100331 * Setup and write the ServerKeyExchange parameters (RFC 4492)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100332 * struct {
333 * ECParameters curve_params;
334 * ECPoint public;
335 * } ServerECDHParams;
336 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100337int mbedtls_ecdh_make_params(mbedtls_ecdh_context *ctx, size_t *olen,
338 unsigned char *buf, size_t blen,
339 int (*f_rng)(void *, unsigned char *, size_t),
340 void *p_rng)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100341{
342 int restart_enabled = 0;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100343#if defined(MBEDTLS_ECP_RESTARTABLE)
344 restart_enabled = ctx->restart_enabled;
345#else
346 (void) restart_enabled;
347#endif
348
349#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 return ecdh_make_params_internal(ctx, olen, ctx->point_format, buf, blen,
351 f_rng, p_rng, restart_enabled);
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100352#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 switch (ctx->var) {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100354#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
355 case MBEDTLS_ECDH_VARIANT_EVEREST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 return mbedtls_everest_make_params(&ctx->ctx.everest_ecdh, olen,
357 buf, blen, f_rng, p_rng);
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100358#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100359 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 return ecdh_make_params_internal(&ctx->ctx.mbed_ecdh, olen,
361 ctx->point_format, buf, blen,
362 f_rng, p_rng,
363 restart_enabled);
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100364 default:
365 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
366 }
367#endif
368}
369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370static int ecdh_read_params_internal(mbedtls_ecdh_context_mbed *ctx,
371 const unsigned char **buf,
372 const unsigned char *end)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100373{
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 return mbedtls_ecp_tls_read_point(&ctx->grp, &ctx->Qp, buf,
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000375 (size_t) (end - *buf));
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100376}
377
378/*
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400379 * Read the ServerKeyExchange parameters (RFC 4492)
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100380 * struct {
381 * ECParameters curve_params;
382 * ECPoint public;
383 * } ServerECDHParams;
384 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100385int mbedtls_ecdh_read_params(mbedtls_ecdh_context *ctx,
386 const unsigned char **buf,
387 const unsigned char *end)
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100388{
Janos Follath24eed8d2019-11-22 13:21:35 +0000389 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follathf61e4862018-10-30 11:53:25 +0000390 mbedtls_ecp_group_id grp_id;
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000391 if ((ret = mbedtls_ecp_tls_read_group_id(&grp_id, buf, (size_t) (end - *buf)))
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 != 0) {
393 return ret;
394 }
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100395
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 if ((ret = mbedtls_ecdh_setup(ctx, grp_id)) != 0) {
397 return ret;
398 }
Janos Follathf61e4862018-10-30 11:53:25 +0000399
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100400#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 return ecdh_read_params_internal(ctx, buf, end);
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100402#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 switch (ctx->var) {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100404#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
405 case MBEDTLS_ECDH_VARIANT_EVEREST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 return mbedtls_everest_read_params(&ctx->ctx.everest_ecdh,
407 buf, end);
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100408#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100409 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 return ecdh_read_params_internal(&ctx->ctx.mbed_ecdh,
411 buf, end);
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100412 default:
413 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
414 }
415#endif
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100416}
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100417
Gilles Peskine449bd832023-01-11 14:50:10 +0100418static int ecdh_get_params_internal(mbedtls_ecdh_context_mbed *ctx,
419 const mbedtls_ecp_keypair *key,
420 mbedtls_ecdh_side side)
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100421{
Janos Follath24eed8d2019-11-22 13:21:35 +0000422 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100423
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100424 /* If it's not our key, just import the public part as Qp */
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 if (side == MBEDTLS_ECDH_THEIRS) {
426 return mbedtls_ecp_copy(&ctx->Qp, &key->Q);
427 }
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100428
429 /* Our key: import public (as Q) and private parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 if (side != MBEDTLS_ECDH_OURS) {
431 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
432 }
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100433
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 if ((ret = mbedtls_ecp_copy(&ctx->Q, &key->Q)) != 0 ||
435 (ret = mbedtls_mpi_copy(&ctx->d, &key->d)) != 0) {
436 return ret;
437 }
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100438
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 return 0;
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100440}
441
442/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100443 * Get parameters from a keypair
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100444 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100445int mbedtls_ecdh_get_params(mbedtls_ecdh_context *ctx,
446 const mbedtls_ecp_keypair *key,
447 mbedtls_ecdh_side side)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100448{
Janos Follath24eed8d2019-11-22 13:21:35 +0000449 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 if (side != MBEDTLS_ECDH_OURS && side != MBEDTLS_ECDH_THEIRS) {
451 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
452 }
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 if (mbedtls_ecdh_grp_id(ctx) == MBEDTLS_ECP_DP_NONE) {
Gilles Peskine0b1b71d2018-11-07 22:10:59 +0100455 /* This is the first call to get_params(). Set up the context
456 * for use with the group. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 if ((ret = mbedtls_ecdh_setup(ctx, key->grp.id)) != 0) {
458 return ret;
459 }
460 } else {
Gilles Peskine0b1b71d2018-11-07 22:10:59 +0100461 /* This is not the first call to get_params(). Check that the
462 * current key's group is the same as the context's, which was set
463 * from the first key's group. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 if (mbedtls_ecdh_grp_id(ctx) != key->grp.id) {
465 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
466 }
Gilles Peskine0b1b71d2018-11-07 22:10:59 +0100467 }
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100468
469#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 return ecdh_get_params_internal(ctx, key, side);
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100471#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 switch (ctx->var) {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100473#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
474 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000475 {
Christoph M. Wintersteiger2e724a12019-01-07 14:19:41 +0000476 mbedtls_everest_ecdh_side s = side == MBEDTLS_ECDH_OURS ?
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 MBEDTLS_EVEREST_ECDH_OURS :
478 MBEDTLS_EVEREST_ECDH_THEIRS;
479 return mbedtls_everest_get_params(&ctx->ctx.everest_ecdh,
480 key, s);
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000481 }
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100482#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100483 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 return ecdh_get_params_internal(&ctx->ctx.mbed_ecdh,
485 key, side);
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100486 default:
487 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
488 }
489#endif
490}
491
Gilles Peskine449bd832023-01-11 14:50:10 +0100492static int ecdh_make_public_internal(mbedtls_ecdh_context_mbed *ctx,
493 size_t *olen, int point_format,
494 unsigned char *buf, size_t blen,
495 int (*f_rng)(void *,
496 unsigned char *,
497 size_t),
498 void *p_rng,
499 int restart_enabled)
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100500{
Janos Follath24eed8d2019-11-22 13:21:35 +0000501 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200502#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200503 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200504#endif
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 if (ctx->grp.pbits == 0) {
507 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
508 }
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100509
Ron Eldorb430d9f2018-11-05 17:18:29 +0200510#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 if (restart_enabled) {
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200512 rs_ctx = &ctx->rs;
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 }
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100514#else
515 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200516#endif
517
Ron Eldor2981d8f2018-11-05 18:07:10 +0200518#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 if ((ret = ecdh_gen_public_restartable(&ctx->grp, &ctx->d, &ctx->Q,
520 f_rng, p_rng, rs_ctx)) != 0) {
521 return ret;
522 }
Ron Eldor2981d8f2018-11-05 18:07:10 +0200523#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 if ((ret = mbedtls_ecdh_gen_public(&ctx->grp, &ctx->d, &ctx->Q,
525 f_rng, p_rng)) != 0) {
526 return ret;
527 }
Ron Eldor2981d8f2018-11-05 18:07:10 +0200528#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100529
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 return mbedtls_ecp_tls_write_point(&ctx->grp, &ctx->Q, point_format, olen,
531 buf, blen);
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100532}
533
534/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100535 * Setup and export the client public value
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100536 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100537int mbedtls_ecdh_make_public(mbedtls_ecdh_context *ctx, size_t *olen,
538 unsigned char *buf, size_t blen,
539 int (*f_rng)(void *, unsigned char *, size_t),
540 void *p_rng)
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100541{
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100542 int restart_enabled = 0;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100543#if defined(MBEDTLS_ECP_RESTARTABLE)
544 restart_enabled = ctx->restart_enabled;
545#endif
546
547#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 return ecdh_make_public_internal(ctx, olen, ctx->point_format, buf, blen,
549 f_rng, p_rng, restart_enabled);
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100550#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 switch (ctx->var) {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100552#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
553 case MBEDTLS_ECDH_VARIANT_EVEREST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 return mbedtls_everest_make_public(&ctx->ctx.everest_ecdh, olen,
555 buf, blen, f_rng, p_rng);
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100556#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100557 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 return ecdh_make_public_internal(&ctx->ctx.mbed_ecdh, olen,
559 ctx->point_format, buf, blen,
560 f_rng, p_rng,
561 restart_enabled);
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100562 default:
563 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
564 }
565#endif
566}
567
Gilles Peskine449bd832023-01-11 14:50:10 +0100568static int ecdh_read_public_internal(mbedtls_ecdh_context_mbed *ctx,
569 const unsigned char *buf, size_t blen)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100570{
Janos Follath24eed8d2019-11-22 13:21:35 +0000571 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100572 const unsigned char *p = buf;
573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 if ((ret = mbedtls_ecp_tls_read_point(&ctx->grp, &ctx->Qp, &p,
575 blen)) != 0) {
576 return ret;
577 }
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100578
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 if ((size_t) (p - buf) != blen) {
580 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
581 }
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100582
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 return 0;
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100584}
585
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100586/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100587 * Parse and import the client's public value
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100588 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100589int mbedtls_ecdh_read_public(mbedtls_ecdh_context *ctx,
590 const unsigned char *buf, size_t blen)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100591{
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100592#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 return ecdh_read_public_internal(ctx, buf, blen);
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100594#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 switch (ctx->var) {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100596#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
597 case MBEDTLS_ECDH_VARIANT_EVEREST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 return mbedtls_everest_read_public(&ctx->ctx.everest_ecdh,
599 buf, blen);
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100600#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100601 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 return ecdh_read_public_internal(&ctx->ctx.mbed_ecdh,
603 buf, blen);
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100604 default:
605 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
606 }
607#endif
608}
609
Gilles Peskine449bd832023-01-11 14:50:10 +0100610static int ecdh_calc_secret_internal(mbedtls_ecdh_context_mbed *ctx,
611 size_t *olen, unsigned char *buf,
612 size_t blen,
613 int (*f_rng)(void *,
614 unsigned char *,
615 size_t),
616 void *p_rng,
617 int restart_enabled)
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100618{
Janos Follath24eed8d2019-11-22 13:21:35 +0000619 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200620#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200621 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200622#endif
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100623
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 if (ctx == NULL || ctx->grp.pbits == 0) {
625 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
626 }
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100627
Ron Eldorb430d9f2018-11-05 17:18:29 +0200628#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 if (restart_enabled) {
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200630 rs_ctx = &ctx->rs;
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 }
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100632#else
633 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200634#endif
635
Ron Eldor2981d8f2018-11-05 18:07:10 +0200636#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 if ((ret = ecdh_compute_shared_restartable(&ctx->grp, &ctx->z, &ctx->Qp,
638 &ctx->d, f_rng, p_rng,
639 rs_ctx)) != 0) {
640 return ret;
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200641 }
Ron Eldor2981d8f2018-11-05 18:07:10 +0200642#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 if ((ret = mbedtls_ecdh_compute_shared(&ctx->grp, &ctx->z, &ctx->Qp,
644 &ctx->d, f_rng, p_rng)) != 0) {
645 return ret;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200646 }
647#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100648
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 if (mbedtls_mpi_size(&ctx->z) > blen) {
650 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
651 }
Paul Bakker41c83d32013-03-20 14:39:14 +0100652
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 *olen = ctx->grp.pbits / 8 + ((ctx->grp.pbits % 8) != 0);
Janos Follathab0f71a2019-02-20 10:48:49 +0000654
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 if (mbedtls_ecp_get_type(&ctx->grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
656 return mbedtls_mpi_write_binary_le(&ctx->z, buf, *olen);
657 }
Janos Follathab0f71a2019-02-20 10:48:49 +0000658
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 return mbedtls_mpi_write_binary(&ctx->z, buf, *olen);
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100660}
661
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100662/*
663 * Derive and export the shared secret
664 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100665int mbedtls_ecdh_calc_secret(mbedtls_ecdh_context *ctx, size_t *olen,
666 unsigned char *buf, size_t blen,
667 int (*f_rng)(void *, unsigned char *, size_t),
668 void *p_rng)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100669{
670 int restart_enabled = 0;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100671#if defined(MBEDTLS_ECP_RESTARTABLE)
672 restart_enabled = ctx->restart_enabled;
673#endif
674
675#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 return ecdh_calc_secret_internal(ctx, olen, buf, blen, f_rng, p_rng,
677 restart_enabled);
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100678#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 switch (ctx->var) {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100680#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
681 case MBEDTLS_ECDH_VARIANT_EVEREST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 return mbedtls_everest_calc_secret(&ctx->ctx.everest_ecdh, olen,
683 buf, blen, f_rng, p_rng);
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100684#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100685 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 return ecdh_calc_secret_internal(&ctx->ctx.mbed_ecdh, olen, buf,
687 blen, f_rng, p_rng,
688 restart_enabled);
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100689 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100691 }
692#endif
693}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694#endif /* MBEDTLS_ECDH_C */