blob: 58ef881f04760c5df0a6bf78305f6b533eab55cb [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010018 */
19
20/*
21 * References:
22 *
Xiaokang Qian47041472023-04-12 06:07:23 +000023 * SEC1 https://www.secg.org/sec1-v2.pdf
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010024 * RFC 4492
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010025 */
26
Gilles Peskinedb09ef62020-06-03 01:43:33 +020027#include "common.h"
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010030
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/ecdh.h"
Hanno Becker91796d72018-12-17 18:10:51 +000032#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000033#include "mbedtls/error.h"
Jerry Yubdc71882021-09-14 19:30:36 +080034
Rich Evans00ab4702015-02-06 13:43:58 +000035#include <string.h>
36
Janos Follath5a3e1bf2018-08-13 15:54:22 +010037#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
38typedef mbedtls_ecdh_context mbedtls_ecdh_context_mbed;
39#endif
40
Gilles Peskine30816292019-02-22 12:31:25 +010041static mbedtls_ecp_group_id mbedtls_ecdh_grp_id(
Gilles Peskine449bd832023-01-11 14:50:10 +010042 const mbedtls_ecdh_context *ctx)
Gilles Peskine30816292019-02-22 12:31:25 +010043{
44#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine449bd832023-01-11 14:50:10 +010045 return ctx->grp.id;
Gilles Peskine30816292019-02-22 12:31:25 +010046#else
Gilles Peskine449bd832023-01-11 14:50:10 +010047 return ctx->grp_id;
Gilles Peskine30816292019-02-22 12:31:25 +010048#endif
49}
50
Gilles Peskine449bd832023-01-11 14:50:10 +010051int mbedtls_ecdh_can_do(mbedtls_ecp_group_id gid)
Gilles Peskine20b3ef32019-02-11 18:41:27 +010052{
53 /* At this time, all groups support ECDH. */
54 (void) gid;
Gilles Peskine449bd832023-01-11 14:50:10 +010055 return 1;
Gilles Peskine20b3ef32019-02-11 18:41:27 +010056}
57
Ron Eldora84c1cb2017-10-10 19:04:27 +030058#if !defined(MBEDTLS_ECDH_GEN_PUBLIC_ALT)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010059/*
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020060 * Generate public key (restartable version)
Manuel Pégourié-Gonnardc0edc962018-10-16 10:38:19 +020061 *
62 * Note: this internal function relies on its caller preserving the value of
Manuel Pégourié-Gonnardca29fdf2018-10-22 09:56:53 +020063 * the output parameter 'd' across continuation calls. This would not be
Manuel Pégourié-Gonnardc0edc962018-10-16 10:38:19 +020064 * acceptable for a public function but is OK here as we control call sites.
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020065 */
Gilles Peskine449bd832023-01-11 14:50:10 +010066static int ecdh_gen_public_restartable(mbedtls_ecp_group *grp,
67 mbedtls_mpi *d, mbedtls_ecp_point *Q,
68 int (*f_rng)(void *, unsigned char *, size_t),
69 void *p_rng,
70 mbedtls_ecp_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020071{
Janos Follath24eed8d2019-11-22 13:21:35 +000072 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020073
David Horstmann91e20a02022-10-06 19:11:28 +010074 int restarting = 0;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020075#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +010076 restarting = (rs_ctx != NULL && rs_ctx->rsm != NULL);
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020077#endif
David Horstmann91e20a02022-10-06 19:11:28 +010078 /* If multiplication is in progress, we already generated a privkey */
Gilles Peskine449bd832023-01-11 14:50:10 +010079 if (!restarting) {
80 MBEDTLS_MPI_CHK(mbedtls_ecp_gen_privkey(grp, d, f_rng, p_rng));
81 }
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020082
Gilles Peskine449bd832023-01-11 14:50:10 +010083 MBEDTLS_MPI_CHK(mbedtls_ecp_mul_restartable(grp, Q, d, &grp->G,
84 f_rng, p_rng, rs_ctx));
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020085
86cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +010087 return ret;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020088}
89
90/*
91 * Generate public key
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010092 */
Gilles Peskine449bd832023-01-11 14:50:10 +010093int mbedtls_ecdh_gen_public(mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
94 int (*f_rng)(void *, unsigned char *, size_t),
95 void *p_rng)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010096{
Gilles Peskine449bd832023-01-11 14:50:10 +010097 return ecdh_gen_public_restartable(grp, d, Q, f_rng, p_rng, NULL);
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010098}
Ron Eldor936d2842018-11-01 13:05:52 +020099#endif /* !MBEDTLS_ECDH_GEN_PUBLIC_ALT */
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100100
Ron Eldora84c1cb2017-10-10 19:04:27 +0300101#if !defined(MBEDTLS_ECDH_COMPUTE_SHARED_ALT)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100102/*
103 * Compute shared secret (SEC1 3.3.1)
104 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100105static int ecdh_compute_shared_restartable(mbedtls_ecp_group *grp,
106 mbedtls_mpi *z,
107 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
108 int (*f_rng)(void *, unsigned char *, size_t),
109 void *p_rng,
110 mbedtls_ecp_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100111{
Janos Follath24eed8d2019-11-22 13:21:35 +0000112 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 mbedtls_ecp_point P;
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 mbedtls_ecp_point_init(&P);
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100116
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 MBEDTLS_MPI_CHK(mbedtls_ecp_mul_restartable(grp, &P, d, Q,
118 f_rng, p_rng, rs_ctx));
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 if (mbedtls_ecp_is_zero(&P)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Paul Bakkerb548d772013-07-26 14:21:34 +0200122 goto cleanup;
123 }
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(z, &P.X));
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100126
127cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 mbedtls_ecp_point_free(&P);
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100129
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 return ret;
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100131}
132
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100133/*
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200134 * Compute shared secret (SEC1 3.3.1)
135 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100136int mbedtls_ecdh_compute_shared(mbedtls_ecp_group *grp, mbedtls_mpi *z,
137 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
138 int (*f_rng)(void *, unsigned char *, size_t),
139 void *p_rng)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200140{
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 return ecdh_compute_shared_restartable(grp, z, Q, d,
142 f_rng, p_rng, NULL);
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200143}
Ron Eldor936d2842018-11-01 13:05:52 +0200144#endif /* !MBEDTLS_ECDH_COMPUTE_SHARED_ALT */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146static void ecdh_init_internal(mbedtls_ecdh_context_mbed *ctx)
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100147{
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 mbedtls_ecp_group_init(&ctx->grp);
149 mbedtls_mpi_init(&ctx->d);
150 mbedtls_ecp_point_init(&ctx->Q);
151 mbedtls_ecp_point_init(&ctx->Qp);
152 mbedtls_mpi_init(&ctx->z);
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200153
154#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 mbedtls_ecp_restart_init(&ctx->rs);
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200156#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100157}
158
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100159/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100160 * Initialize context
Janos Follathf61e4862018-10-30 11:53:25 +0000161 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100162void mbedtls_ecdh_init(mbedtls_ecdh_context *ctx)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100163{
164#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 ecdh_init_internal(ctx);
166 mbedtls_ecp_point_init(&ctx->Vi);
167 mbedtls_ecp_point_init(&ctx->Vf);
168 mbedtls_mpi_init(&ctx->_d);
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100169#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 memset(ctx, 0, sizeof(mbedtls_ecdh_context));
Christoph M. Wintersteigerd8c45d52019-02-20 17:16:53 +0000171
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100172 ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
173#endif
174 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
175#if defined(MBEDTLS_ECP_RESTARTABLE)
176 ctx->restart_enabled = 0;
177#endif
178}
179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180static int ecdh_setup_internal(mbedtls_ecdh_context_mbed *ctx,
181 mbedtls_ecp_group_id grp_id)
Janos Follathf61e4862018-10-30 11:53:25 +0000182{
Janos Follath24eed8d2019-11-22 13:21:35 +0000183 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follathf61e4862018-10-30 11:53:25 +0000184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 ret = mbedtls_ecp_group_load(&ctx->grp, grp_id);
186 if (ret != 0) {
187 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Janos Follathf61e4862018-10-30 11:53:25 +0000188 }
189
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 return 0;
Janos Follathf61e4862018-10-30 11:53:25 +0000191}
192
193/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100194 * Setup context
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100195 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100196int mbedtls_ecdh_setup(mbedtls_ecdh_context *ctx, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100197{
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100198#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 return ecdh_setup_internal(ctx, grp_id);
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100200#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 switch (grp_id) {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100202#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
Christoph M. Wintersteiger3ff60bc2019-02-15 12:59:59 +0000203 case MBEDTLS_ECP_DP_CURVE25519:
204 ctx->point_format = MBEDTLS_ECP_PF_COMPRESSED;
205 ctx->var = MBEDTLS_ECDH_VARIANT_EVEREST;
206 ctx->grp_id = grp_id;
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 return mbedtls_everest_setup(&ctx->ctx.everest_ecdh, grp_id);
Christoph M. Wintersteiger78c9c462018-12-06 17:16:32 +0000208#endif
Christoph M. Wintersteiger3ff60bc2019-02-15 12:59:59 +0000209 default:
210 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
211 ctx->var = MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0;
212 ctx->grp_id = grp_id;
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 ecdh_init_internal(&ctx->ctx.mbed_ecdh);
214 return ecdh_setup_internal(&ctx->ctx.mbed_ecdh, grp_id);
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100215 }
216#endif
217}
218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219static void ecdh_free_internal(mbedtls_ecdh_context_mbed *ctx)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100220{
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 mbedtls_ecp_group_free(&ctx->grp);
222 mbedtls_mpi_free(&ctx->d);
223 mbedtls_ecp_point_free(&ctx->Q);
224 mbedtls_ecp_point_free(&ctx->Qp);
225 mbedtls_mpi_free(&ctx->z);
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200226
227#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 mbedtls_ecp_restart_free(&ctx->rs);
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200229#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100230}
231
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200232#if defined(MBEDTLS_ECP_RESTARTABLE)
233/*
234 * Enable restartable operations for context
235 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100236void mbedtls_ecdh_enable_restart(mbedtls_ecdh_context *ctx)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200237{
238 ctx->restart_enabled = 1;
239}
240#endif
241
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100242/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100243 * Free context
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100244 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100245void mbedtls_ecdh_free(mbedtls_ecdh_context *ctx)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100246{
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 if (ctx == NULL) {
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100248 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 }
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100250
251#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 mbedtls_ecp_point_free(&ctx->Vi);
253 mbedtls_ecp_point_free(&ctx->Vf);
254 mbedtls_mpi_free(&ctx->_d);
255 ecdh_free_internal(ctx);
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100256#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 switch (ctx->var) {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100258#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
259 case MBEDTLS_ECDH_VARIANT_EVEREST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 mbedtls_everest_free(&ctx->ctx.everest_ecdh);
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100261 break;
262#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100263 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 ecdh_free_internal(&ctx->ctx.mbed_ecdh);
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100265 break;
266 default:
267 break;
268 }
269
270 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
271 ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
272 ctx->grp_id = MBEDTLS_ECP_DP_NONE;
273#endif
274}
275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276static int ecdh_make_params_internal(mbedtls_ecdh_context_mbed *ctx,
277 size_t *olen, int point_format,
278 unsigned char *buf, size_t blen,
279 int (*f_rng)(void *,
280 unsigned char *,
281 size_t),
282 void *p_rng,
283 int restart_enabled)
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100284{
Janos Follath24eed8d2019-11-22 13:21:35 +0000285 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100286 size_t grp_len, pt_len;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200287#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200288 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200289#endif
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 if (ctx->grp.pbits == 0) {
292 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
293 }
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100294
Ron Eldor19779c42018-11-05 16:58:13 +0200295#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 if (restart_enabled) {
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200297 rs_ctx = &ctx->rs;
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 }
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100299#else
300 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200301#endif
302
Ron Eldor8493f802018-11-01 11:32:15 +0200303
Ron Eldor2981d8f2018-11-05 18:07:10 +0200304#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 if ((ret = ecdh_gen_public_restartable(&ctx->grp, &ctx->d, &ctx->Q,
306 f_rng, p_rng, rs_ctx)) != 0) {
307 return ret;
308 }
Ron Eldor2981d8f2018-11-05 18:07:10 +0200309#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 if ((ret = mbedtls_ecdh_gen_public(&ctx->grp, &ctx->d, &ctx->Q,
311 f_rng, p_rng)) != 0) {
312 return ret;
313 }
Ron Eldor2981d8f2018-11-05 18:07:10 +0200314#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100315
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 if ((ret = mbedtls_ecp_tls_write_group(&ctx->grp, &grp_len, buf,
317 blen)) != 0) {
318 return ret;
319 }
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100320
321 buf += grp_len;
322 blen -= grp_len;
323
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 if ((ret = mbedtls_ecp_tls_write_point(&ctx->grp, &ctx->Q, point_format,
325 &pt_len, buf, blen)) != 0) {
326 return ret;
327 }
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100328
329 *olen = grp_len + pt_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 return 0;
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100331}
332
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100333/*
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100334 * Setup and write the ServerKeyExchange parameters (RFC 4492)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100335 * struct {
336 * ECParameters curve_params;
337 * ECPoint public;
338 * } ServerECDHParams;
339 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100340int mbedtls_ecdh_make_params(mbedtls_ecdh_context *ctx, size_t *olen,
341 unsigned char *buf, size_t blen,
342 int (*f_rng)(void *, unsigned char *, size_t),
343 void *p_rng)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100344{
345 int restart_enabled = 0;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100346#if defined(MBEDTLS_ECP_RESTARTABLE)
347 restart_enabled = ctx->restart_enabled;
348#else
349 (void) restart_enabled;
350#endif
351
352#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 return ecdh_make_params_internal(ctx, olen, ctx->point_format, buf, blen,
354 f_rng, p_rng, restart_enabled);
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100355#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 switch (ctx->var) {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100357#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
358 case MBEDTLS_ECDH_VARIANT_EVEREST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 return mbedtls_everest_make_params(&ctx->ctx.everest_ecdh, olen,
360 buf, blen, f_rng, p_rng);
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100361#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100362 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 return ecdh_make_params_internal(&ctx->ctx.mbed_ecdh, olen,
364 ctx->point_format, buf, blen,
365 f_rng, p_rng,
366 restart_enabled);
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100367 default:
368 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
369 }
370#endif
371}
372
Gilles Peskine449bd832023-01-11 14:50:10 +0100373static int ecdh_read_params_internal(mbedtls_ecdh_context_mbed *ctx,
374 const unsigned char **buf,
375 const unsigned char *end)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100376{
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 return mbedtls_ecp_tls_read_point(&ctx->grp, &ctx->Qp, buf,
378 end - *buf);
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100379}
380
381/*
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400382 * Read the ServerKeyExchange parameters (RFC 4492)
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100383 * struct {
384 * ECParameters curve_params;
385 * ECPoint public;
386 * } ServerECDHParams;
387 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100388int mbedtls_ecdh_read_params(mbedtls_ecdh_context *ctx,
389 const unsigned char **buf,
390 const unsigned char *end)
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100391{
Janos Follath24eed8d2019-11-22 13:21:35 +0000392 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follathf61e4862018-10-30 11:53:25 +0000393 mbedtls_ecp_group_id grp_id;
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 if ((ret = mbedtls_ecp_tls_read_group_id(&grp_id, buf, end - *buf))
395 != 0) {
396 return ret;
397 }
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100398
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 if ((ret = mbedtls_ecdh_setup(ctx, grp_id)) != 0) {
400 return ret;
401 }
Janos Follathf61e4862018-10-30 11:53:25 +0000402
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100403#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 return ecdh_read_params_internal(ctx, buf, end);
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100405#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 switch (ctx->var) {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100407#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
408 case MBEDTLS_ECDH_VARIANT_EVEREST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 return mbedtls_everest_read_params(&ctx->ctx.everest_ecdh,
410 buf, end);
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100411#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100412 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 return ecdh_read_params_internal(&ctx->ctx.mbed_ecdh,
414 buf, end);
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100415 default:
416 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
417 }
418#endif
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100419}
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100420
Gilles Peskine449bd832023-01-11 14:50:10 +0100421static int ecdh_get_params_internal(mbedtls_ecdh_context_mbed *ctx,
422 const mbedtls_ecp_keypair *key,
423 mbedtls_ecdh_side side)
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100424{
Janos Follath24eed8d2019-11-22 13:21:35 +0000425 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100426
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100427 /* If it's not our key, just import the public part as Qp */
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 if (side == MBEDTLS_ECDH_THEIRS) {
429 return mbedtls_ecp_copy(&ctx->Qp, &key->Q);
430 }
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100431
432 /* Our key: import public (as Q) and private parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 if (side != MBEDTLS_ECDH_OURS) {
434 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
435 }
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100436
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 if ((ret = mbedtls_ecp_copy(&ctx->Q, &key->Q)) != 0 ||
438 (ret = mbedtls_mpi_copy(&ctx->d, &key->d)) != 0) {
439 return ret;
440 }
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 return 0;
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100443}
444
445/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100446 * Get parameters from a keypair
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100447 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100448int mbedtls_ecdh_get_params(mbedtls_ecdh_context *ctx,
449 const mbedtls_ecp_keypair *key,
450 mbedtls_ecdh_side side)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100451{
Janos Follath24eed8d2019-11-22 13:21:35 +0000452 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 if (side != MBEDTLS_ECDH_OURS && side != MBEDTLS_ECDH_THEIRS) {
454 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
455 }
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 if (mbedtls_ecdh_grp_id(ctx) == MBEDTLS_ECP_DP_NONE) {
Gilles Peskine0b1b71d2018-11-07 22:10:59 +0100458 /* This is the first call to get_params(). Set up the context
459 * for use with the group. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 if ((ret = mbedtls_ecdh_setup(ctx, key->grp.id)) != 0) {
461 return ret;
462 }
463 } else {
Gilles Peskine0b1b71d2018-11-07 22:10:59 +0100464 /* This is not the first call to get_params(). Check that the
465 * current key's group is the same as the context's, which was set
466 * from the first key's group. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 if (mbedtls_ecdh_grp_id(ctx) != key->grp.id) {
468 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
469 }
Gilles Peskine0b1b71d2018-11-07 22:10:59 +0100470 }
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100471
472#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 return ecdh_get_params_internal(ctx, key, side);
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100474#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 switch (ctx->var) {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100476#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
477 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000478 {
Christoph M. Wintersteiger2e724a12019-01-07 14:19:41 +0000479 mbedtls_everest_ecdh_side s = side == MBEDTLS_ECDH_OURS ?
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 MBEDTLS_EVEREST_ECDH_OURS :
481 MBEDTLS_EVEREST_ECDH_THEIRS;
482 return mbedtls_everest_get_params(&ctx->ctx.everest_ecdh,
483 key, s);
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000484 }
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100485#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100486 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 return ecdh_get_params_internal(&ctx->ctx.mbed_ecdh,
488 key, side);
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100489 default:
490 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
491 }
492#endif
493}
494
Gilles Peskine449bd832023-01-11 14:50:10 +0100495static int ecdh_make_public_internal(mbedtls_ecdh_context_mbed *ctx,
496 size_t *olen, int point_format,
497 unsigned char *buf, size_t blen,
498 int (*f_rng)(void *,
499 unsigned char *,
500 size_t),
501 void *p_rng,
502 int restart_enabled)
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100503{
Janos Follath24eed8d2019-11-22 13:21:35 +0000504 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200505#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200506 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200507#endif
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100508
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 if (ctx->grp.pbits == 0) {
510 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
511 }
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100512
Ron Eldorb430d9f2018-11-05 17:18:29 +0200513#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 if (restart_enabled) {
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200515 rs_ctx = &ctx->rs;
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 }
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100517#else
518 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200519#endif
520
Ron Eldor2981d8f2018-11-05 18:07:10 +0200521#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 if ((ret = ecdh_gen_public_restartable(&ctx->grp, &ctx->d, &ctx->Q,
523 f_rng, p_rng, rs_ctx)) != 0) {
524 return ret;
525 }
Ron Eldor2981d8f2018-11-05 18:07:10 +0200526#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 if ((ret = mbedtls_ecdh_gen_public(&ctx->grp, &ctx->d, &ctx->Q,
528 f_rng, p_rng)) != 0) {
529 return ret;
530 }
Ron Eldor2981d8f2018-11-05 18:07:10 +0200531#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100532
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 return mbedtls_ecp_tls_write_point(&ctx->grp, &ctx->Q, point_format, olen,
534 buf, blen);
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100535}
536
537/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100538 * Setup and export the client public value
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100539 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100540int mbedtls_ecdh_make_public(mbedtls_ecdh_context *ctx, size_t *olen,
541 unsigned char *buf, size_t blen,
542 int (*f_rng)(void *, unsigned char *, size_t),
543 void *p_rng)
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100544{
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100545 int restart_enabled = 0;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100546#if defined(MBEDTLS_ECP_RESTARTABLE)
547 restart_enabled = ctx->restart_enabled;
548#endif
549
550#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 return ecdh_make_public_internal(ctx, olen, ctx->point_format, buf, blen,
552 f_rng, p_rng, restart_enabled);
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100553#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 switch (ctx->var) {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100555#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
556 case MBEDTLS_ECDH_VARIANT_EVEREST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 return mbedtls_everest_make_public(&ctx->ctx.everest_ecdh, olen,
558 buf, blen, f_rng, p_rng);
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100559#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100560 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 return ecdh_make_public_internal(&ctx->ctx.mbed_ecdh, olen,
562 ctx->point_format, buf, blen,
563 f_rng, p_rng,
564 restart_enabled);
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100565 default:
566 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
567 }
568#endif
569}
570
Gilles Peskine449bd832023-01-11 14:50:10 +0100571static int ecdh_read_public_internal(mbedtls_ecdh_context_mbed *ctx,
572 const unsigned char *buf, size_t blen)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100573{
Janos Follath24eed8d2019-11-22 13:21:35 +0000574 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100575 const unsigned char *p = buf;
576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 if ((ret = mbedtls_ecp_tls_read_point(&ctx->grp, &ctx->Qp, &p,
578 blen)) != 0) {
579 return ret;
580 }
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100581
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 if ((size_t) (p - buf) != blen) {
583 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
584 }
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 return 0;
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100587}
588
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100589/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100590 * Parse and import the client's public value
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100591 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100592int mbedtls_ecdh_read_public(mbedtls_ecdh_context *ctx,
593 const unsigned char *buf, size_t blen)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100594{
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100595#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 return ecdh_read_public_internal(ctx, buf, blen);
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100597#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 switch (ctx->var) {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100599#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
600 case MBEDTLS_ECDH_VARIANT_EVEREST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 return mbedtls_everest_read_public(&ctx->ctx.everest_ecdh,
602 buf, blen);
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100603#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100604 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 return ecdh_read_public_internal(&ctx->ctx.mbed_ecdh,
606 buf, blen);
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100607 default:
608 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
609 }
610#endif
611}
612
Gilles Peskine449bd832023-01-11 14:50:10 +0100613static int ecdh_calc_secret_internal(mbedtls_ecdh_context_mbed *ctx,
614 size_t *olen, unsigned char *buf,
615 size_t blen,
616 int (*f_rng)(void *,
617 unsigned char *,
618 size_t),
619 void *p_rng,
620 int restart_enabled)
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100621{
Janos Follath24eed8d2019-11-22 13:21:35 +0000622 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200623#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200624 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200625#endif
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100626
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 if (ctx == NULL || ctx->grp.pbits == 0) {
628 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
629 }
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100630
Ron Eldorb430d9f2018-11-05 17:18:29 +0200631#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 if (restart_enabled) {
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200633 rs_ctx = &ctx->rs;
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 }
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100635#else
636 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200637#endif
638
Ron Eldor2981d8f2018-11-05 18:07:10 +0200639#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 if ((ret = ecdh_compute_shared_restartable(&ctx->grp, &ctx->z, &ctx->Qp,
641 &ctx->d, f_rng, p_rng,
642 rs_ctx)) != 0) {
643 return ret;
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200644 }
Ron Eldor2981d8f2018-11-05 18:07:10 +0200645#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 if ((ret = mbedtls_ecdh_compute_shared(&ctx->grp, &ctx->z, &ctx->Qp,
647 &ctx->d, f_rng, p_rng)) != 0) {
648 return ret;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200649 }
650#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100651
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 if (mbedtls_mpi_size(&ctx->z) > blen) {
653 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
654 }
Paul Bakker41c83d32013-03-20 14:39:14 +0100655
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 *olen = ctx->grp.pbits / 8 + ((ctx->grp.pbits % 8) != 0);
Janos Follathab0f71a2019-02-20 10:48:49 +0000657
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 if (mbedtls_ecp_get_type(&ctx->grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
659 return mbedtls_mpi_write_binary_le(&ctx->z, buf, *olen);
660 }
Janos Follathab0f71a2019-02-20 10:48:49 +0000661
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 return mbedtls_mpi_write_binary(&ctx->z, buf, *olen);
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100663}
664
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100665/*
666 * Derive and export the shared secret
667 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100668int mbedtls_ecdh_calc_secret(mbedtls_ecdh_context *ctx, size_t *olen,
669 unsigned char *buf, size_t blen,
670 int (*f_rng)(void *, unsigned char *, size_t),
671 void *p_rng)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100672{
673 int restart_enabled = 0;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100674#if defined(MBEDTLS_ECP_RESTARTABLE)
675 restart_enabled = ctx->restart_enabled;
676#endif
677
678#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 return ecdh_calc_secret_internal(ctx, olen, buf, blen, f_rng, p_rng,
680 restart_enabled);
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100681#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 switch (ctx->var) {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100683#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
684 case MBEDTLS_ECDH_VARIANT_EVEREST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 return mbedtls_everest_calc_secret(&ctx->ctx.everest_ecdh, olen,
686 buf, blen, f_rng, p_rng);
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100687#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100688 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 return ecdh_calc_secret_internal(&ctx->ctx.mbed_ecdh, olen, buf,
690 blen, f_rng, p_rng,
691 restart_enabled);
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100692 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100694 }
695#endif
696}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697#endif /* MBEDTLS_ECDH_C */