blob: d795ec4da9706c792b112cde83cdad4ad2fa72a1 [file] [log] [blame]
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +01001/*
2 * Elliptic curve Diffie-Hellman
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010020 */
21
22/*
23 * References:
24 *
25 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010026 * RFC 4492
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010027 */
28
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#endif
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010036
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/ecdh.h"
Hanno Becker91796d72018-12-17 18:10:51 +000038#include "mbedtls/platform_util.h"
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010039
Rich Evans00ab4702015-02-06 13:43:58 +000040#include <string.h>
41
Hanno Becker91796d72018-12-17 18:10:51 +000042/* Parameter validation macros based on platform_util.h */
43#define ECDH_VALIDATE_RET( cond ) \
44 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_ECP_BAD_INPUT_DATA )
45#define ECDH_VALIDATE( cond ) \
46 MBEDTLS_INTERNAL_VALIDATE( cond )
47
Janos Follath5a3e1bf2018-08-13 15:54:22 +010048#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
49typedef mbedtls_ecdh_context mbedtls_ecdh_context_mbed;
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +010050#else
51#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
52#include "everest/everest.h"
53#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +010054#endif
55
Gilles Peskine30816292019-02-22 12:31:25 +010056static mbedtls_ecp_group_id mbedtls_ecdh_grp_id(
57 const mbedtls_ecdh_context *ctx )
58{
59#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
60 return( ctx->grp.id );
61#else
62 return( ctx->grp_id );
63#endif
64}
65
Gilles Peskine20b3ef32019-02-11 18:41:27 +010066int mbedtls_ecdh_can_do( mbedtls_ecp_group_id gid )
67{
68 /* At this time, all groups support ECDH. */
69 (void) gid;
70 return 1;
71}
72
Ron Eldora84c1cb2017-10-10 19:04:27 +030073#if !defined(MBEDTLS_ECDH_GEN_PUBLIC_ALT)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010074/*
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020075 * Generate public key (restartable version)
Manuel Pégourié-Gonnardc0edc962018-10-16 10:38:19 +020076 *
77 * Note: this internal function relies on its caller preserving the value of
Manuel Pégourié-Gonnardca29fdf2018-10-22 09:56:53 +020078 * the output parameter 'd' across continuation calls. This would not be
Manuel Pégourié-Gonnardc0edc962018-10-16 10:38:19 +020079 * acceptable for a public function but is OK here as we control call sites.
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020080 */
81static int ecdh_gen_public_restartable( mbedtls_ecp_group *grp,
82 mbedtls_mpi *d, mbedtls_ecp_point *Q,
83 int (*f_rng)(void *, unsigned char *, size_t),
84 void *p_rng,
85 mbedtls_ecp_restart_ctx *rs_ctx )
86{
87 int ret;
88
89 /* If multiplication is in progress, we already generated a privkey */
90#if defined(MBEDTLS_ECP_RESTARTABLE)
91 if( rs_ctx == NULL || rs_ctx->rsm == NULL )
92#endif
93 MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, d, f_rng, p_rng ) );
94
95 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, Q, d, &grp->G,
96 f_rng, p_rng, rs_ctx ) );
97
98cleanup:
99 return( ret );
100}
101
102/*
103 * Generate public key
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100104 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100106 int (*f_rng)(void *, unsigned char *, size_t),
107 void *p_rng )
108{
Hanno Becker91796d72018-12-17 18:10:51 +0000109 ECDH_VALIDATE_RET( grp != NULL );
110 ECDH_VALIDATE_RET( d != NULL );
111 ECDH_VALIDATE_RET( Q != NULL );
112 ECDH_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200113 return( ecdh_gen_public_restartable( grp, d, Q, f_rng, p_rng, NULL ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100114}
Ron Eldor936d2842018-11-01 13:05:52 +0200115#endif /* !MBEDTLS_ECDH_GEN_PUBLIC_ALT */
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100116
Ron Eldora84c1cb2017-10-10 19:04:27 +0300117#if !defined(MBEDTLS_ECDH_COMPUTE_SHARED_ALT)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100118/*
119 * Compute shared secret (SEC1 3.3.1)
120 */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200121static int ecdh_compute_shared_restartable( mbedtls_ecp_group *grp,
122 mbedtls_mpi *z,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200124 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200125 void *p_rng,
126 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100127{
128 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 mbedtls_ecp_point P;
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 mbedtls_ecp_point_init( &P );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100132
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200133 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, &P, d, Q,
134 f_rng, p_rng, rs_ctx ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 if( mbedtls_ecp_is_zero( &P ) )
Paul Bakkerb548d772013-07-26 14:21:34 +0200137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Paul Bakkerb548d772013-07-26 14:21:34 +0200139 goto cleanup;
140 }
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( z, &P.X ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100143
144cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_ecp_point_free( &P );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100146
147 return( ret );
148}
149
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100150/*
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200151 * Compute shared secret (SEC1 3.3.1)
152 */
153int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
154 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
155 int (*f_rng)(void *, unsigned char *, size_t),
156 void *p_rng )
157{
Hanno Becker91796d72018-12-17 18:10:51 +0000158 ECDH_VALIDATE_RET( grp != NULL );
159 ECDH_VALIDATE_RET( Q != NULL );
160 ECDH_VALIDATE_RET( d != NULL );
161 ECDH_VALIDATE_RET( z != NULL );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200162 return( ecdh_compute_shared_restartable( grp, z, Q, d,
163 f_rng, p_rng, NULL ) );
164}
Ron Eldor936d2842018-11-01 13:05:52 +0200165#endif /* !MBEDTLS_ECDH_COMPUTE_SHARED_ALT */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200166
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100167static void ecdh_init_internal( mbedtls_ecdh_context_mbed *ctx )
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100168{
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200169 mbedtls_ecp_group_init( &ctx->grp );
170 mbedtls_mpi_init( &ctx->d );
171 mbedtls_ecp_point_init( &ctx->Q );
172 mbedtls_ecp_point_init( &ctx->Qp );
173 mbedtls_mpi_init( &ctx->z );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200174
175#if defined(MBEDTLS_ECP_RESTARTABLE)
176 mbedtls_ecp_restart_init( &ctx->rs );
177#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100178}
179
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100180/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100181 * Initialize context
Janos Follathf61e4862018-10-30 11:53:25 +0000182 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100183void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx )
184{
Hanno Becker91796d72018-12-17 18:10:51 +0000185 ECDH_VALIDATE( ctx != NULL );
186
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100187#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
188 ecdh_init_internal( ctx );
189 mbedtls_ecp_point_init( &ctx->Vi );
190 mbedtls_ecp_point_init( &ctx->Vf );
191 mbedtls_mpi_init( &ctx->_d );
192#else
193 memset( ctx, 0, sizeof( mbedtls_ecdh_context ) );
194
195 ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
196#endif
197 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
198#if defined(MBEDTLS_ECP_RESTARTABLE)
199 ctx->restart_enabled = 0;
200#endif
201}
202
203static int ecdh_setup_internal( mbedtls_ecdh_context_mbed *ctx,
204 mbedtls_ecp_group_id grp_id )
Janos Follathf61e4862018-10-30 11:53:25 +0000205{
206 int ret;
207
208 ret = mbedtls_ecp_group_load( &ctx->grp, grp_id );
209 if( ret != 0 )
210 {
Janos Follathf61e4862018-10-30 11:53:25 +0000211 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
212 }
213
214 return( 0 );
215}
216
217/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100218 * Setup context
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100219 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100220int mbedtls_ecdh_setup( mbedtls_ecdh_context *ctx, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100221{
Hanno Becker91796d72018-12-17 18:10:51 +0000222 ECDH_VALIDATE_RET( ctx != NULL );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100223
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100224#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
225 return( ecdh_setup_internal( ctx, grp_id ) );
226#else
227 switch( grp_id )
228 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100229#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
Christoph M. Wintersteiger3ff60bc2019-02-15 12:59:59 +0000230 case MBEDTLS_ECP_DP_CURVE25519:
231 ctx->point_format = MBEDTLS_ECP_PF_COMPRESSED;
232 ctx->var = MBEDTLS_ECDH_VARIANT_EVEREST;
233 ctx->grp_id = grp_id;
234 return( mbedtls_everest_setup( &ctx->ctx.everest_ecdh, grp_id ) );
Christoph M. Wintersteiger78c9c462018-12-06 17:16:32 +0000235#endif
Christoph M. Wintersteiger3ff60bc2019-02-15 12:59:59 +0000236 default:
237 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
238 ctx->var = MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0;
239 ctx->grp_id = grp_id;
240 ecdh_init_internal( &ctx->ctx.mbed_ecdh );
241 return( ecdh_setup_internal( &ctx->ctx.mbed_ecdh, grp_id ) );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100242 }
243#endif
244}
245
246static void ecdh_free_internal( mbedtls_ecdh_context_mbed *ctx )
247{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 mbedtls_ecp_group_free( &ctx->grp );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200249 mbedtls_mpi_free( &ctx->d );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 mbedtls_ecp_point_free( &ctx->Q );
251 mbedtls_ecp_point_free( &ctx->Qp );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200252 mbedtls_mpi_free( &ctx->z );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200253
254#if defined(MBEDTLS_ECP_RESTARTABLE)
255 mbedtls_ecp_restart_free( &ctx->rs );
256#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100257}
258
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200259#if defined(MBEDTLS_ECP_RESTARTABLE)
260/*
261 * Enable restartable operations for context
262 */
263void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx )
264{
Hanno Beckera7634e82018-12-18 18:45:00 +0000265 ECDH_VALIDATE( ctx != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100266
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200267 ctx->restart_enabled = 1;
268}
269#endif
270
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100271/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100272 * Free context
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100273 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100274void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx )
275{
276 if( ctx == NULL )
277 return;
278
279#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
280 mbedtls_ecp_point_free( &ctx->Vi );
281 mbedtls_ecp_point_free( &ctx->Vf );
282 mbedtls_mpi_free( &ctx->_d );
283 ecdh_free_internal( ctx );
284#else
285 switch( ctx->var )
286 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100287#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
288 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000289 mbedtls_everest_free( &ctx->ctx.everest_ecdh );
290 ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
291 ctx->grp_id = MBEDTLS_ECP_DP_NONE;
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100292 break;
293#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100294 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
295 ecdh_free_internal( &ctx->ctx.mbed_ecdh );
296 break;
297 default:
298 break;
299 }
300
301 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
302 ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
303 ctx->grp_id = MBEDTLS_ECP_DP_NONE;
304#endif
305}
306
307static int ecdh_make_params_internal( mbedtls_ecdh_context_mbed *ctx,
308 size_t *olen, int point_format,
309 unsigned char *buf, size_t blen,
310 int (*f_rng)(void *,
311 unsigned char *,
312 size_t),
313 void *p_rng,
314 int restart_enabled )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100315{
316 int ret;
317 size_t grp_len, pt_len;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200318#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200319 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200320#endif
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100321
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100322 if( ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100324
Ron Eldor19779c42018-11-05 16:58:13 +0200325#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100326 if( restart_enabled )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200327 rs_ctx = &ctx->rs;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100328#else
329 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200330#endif
331
Ron Eldor8493f802018-11-01 11:32:15 +0200332
Ron Eldor2981d8f2018-11-05 18:07:10 +0200333#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200334 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200335 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100336 return( ret );
Ron Eldor2981d8f2018-11-05 18:07:10 +0200337#else
338 if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q,
339 f_rng, p_rng ) ) != 0 )
340 return( ret );
341#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100342
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100343 if( ( ret = mbedtls_ecp_tls_write_group( &ctx->grp, &grp_len, buf,
344 blen ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100345 return( ret );
346
347 buf += grp_len;
348 blen -= grp_len;
349
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100350 if( ( ret = mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, point_format,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200351 &pt_len, buf, blen ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100352 return( ret );
353
354 *olen = grp_len + pt_len;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200355 return( 0 );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100356}
357
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100358/*
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100359 * Setup and write the ServerKeyExchange parameters (RFC 4492)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100360 * struct {
361 * ECParameters curve_params;
362 * ECPoint public;
363 * } ServerECDHParams;
364 */
365int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
366 unsigned char *buf, size_t blen,
367 int (*f_rng)(void *, unsigned char *, size_t),
368 void *p_rng )
369{
370 int restart_enabled = 0;
Hanno Becker91796d72018-12-17 18:10:51 +0000371 ECDH_VALIDATE_RET( ctx != NULL );
372 ECDH_VALIDATE_RET( olen != NULL );
373 ECDH_VALIDATE_RET( buf != NULL );
374 ECDH_VALIDATE_RET( f_rng != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100375
376#if defined(MBEDTLS_ECP_RESTARTABLE)
377 restart_enabled = ctx->restart_enabled;
378#else
379 (void) restart_enabled;
380#endif
381
382#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
383 return( ecdh_make_params_internal( ctx, olen, ctx->point_format, buf, blen,
384 f_rng, p_rng, restart_enabled ) );
385#else
386 switch( ctx->var )
387 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100388#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
389 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000390 return( mbedtls_everest_make_params( &ctx->ctx.everest_ecdh, olen,
391 buf, blen, f_rng, p_rng ) );
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100392#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100393 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
394 return( ecdh_make_params_internal( &ctx->ctx.mbed_ecdh, olen,
395 ctx->point_format, buf, blen,
396 f_rng, p_rng,
397 restart_enabled ) );
398 default:
399 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
400 }
401#endif
402}
403
404static int ecdh_read_params_internal( mbedtls_ecdh_context_mbed *ctx,
405 const unsigned char **buf,
406 const unsigned char *end )
407{
408 return( mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf,
409 end - *buf ) );
410}
411
412/*
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100413 * Read the ServerKeyExhange parameters (RFC 4492)
414 * struct {
415 * ECParameters curve_params;
416 * ECPoint public;
417 * } ServerECDHParams;
418 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100420 const unsigned char **buf,
421 const unsigned char *end )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100422{
423 int ret;
Janos Follathf61e4862018-10-30 11:53:25 +0000424 mbedtls_ecp_group_id grp_id;
Hanno Becker91796d72018-12-17 18:10:51 +0000425 ECDH_VALIDATE_RET( ctx != NULL );
426 ECDH_VALIDATE_RET( buf != NULL );
427 ECDH_VALIDATE_RET( *buf != NULL );
428 ECDH_VALIDATE_RET( end != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100429
Janos Follathf61e4862018-10-30 11:53:25 +0000430 if( ( ret = mbedtls_ecp_tls_read_group_id( &grp_id, buf, end - *buf ) )
431 != 0 )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100432 return( ret );
433
Janos Follathf61e4862018-10-30 11:53:25 +0000434 if( ( ret = mbedtls_ecdh_setup( ctx, grp_id ) ) != 0 )
435 return( ret );
436
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100437#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
438 return( ecdh_read_params_internal( ctx, buf, end ) );
439#else
440 switch( ctx->var )
441 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100442#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
443 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000444 return( mbedtls_everest_read_params( &ctx->ctx.everest_ecdh,
445 buf, end) );
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100446#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100447 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
448 return( ecdh_read_params_internal( &ctx->ctx.mbed_ecdh,
449 buf, end ) );
450 default:
451 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
452 }
453#endif
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100454}
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100455
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100456static int ecdh_get_params_internal( mbedtls_ecdh_context_mbed *ctx,
457 const mbedtls_ecp_keypair *key,
458 mbedtls_ecdh_side side )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100459{
460 int ret;
461
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100462 /* If it's not our key, just import the public part as Qp */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 if( side == MBEDTLS_ECDH_THEIRS )
464 return( mbedtls_ecp_copy( &ctx->Qp, &key->Q ) );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100465
466 /* Our key: import public (as Q) and private parts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 if( side != MBEDTLS_ECDH_OURS )
468 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 if( ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 ||
471 ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100472 return( ret );
473
474 return( 0 );
475}
476
477/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100478 * Get parameters from a keypair
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100479 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100480int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx,
481 const mbedtls_ecp_keypair *key,
482 mbedtls_ecdh_side side )
483{
484 int ret;
Hanno Becker91796d72018-12-17 18:10:51 +0000485 ECDH_VALIDATE_RET( ctx != NULL );
486 ECDH_VALIDATE_RET( key != NULL );
487 ECDH_VALIDATE_RET( side == MBEDTLS_ECDH_OURS ||
488 side == MBEDTLS_ECDH_THEIRS );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100489
Gilles Peskine30816292019-02-22 12:31:25 +0100490 if( mbedtls_ecdh_grp_id( ctx ) == MBEDTLS_ECP_DP_NONE )
Gilles Peskine0b1b71d2018-11-07 22:10:59 +0100491 {
492 /* This is the first call to get_params(). Set up the context
493 * for use with the group. */
494 if( ( ret = mbedtls_ecdh_setup( ctx, key->grp.id ) ) != 0 )
495 return( ret );
496 }
497 else
498 {
499 /* This is not the first call to get_params(). Check that the
500 * current key's group is the same as the context's, which was set
501 * from the first key's group. */
Gilles Peskine30816292019-02-22 12:31:25 +0100502 if( mbedtls_ecdh_grp_id( ctx ) != key->grp.id )
Gilles Peskine0b1b71d2018-11-07 22:10:59 +0100503 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
504 }
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100505
506#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
507 return( ecdh_get_params_internal( ctx, key, side ) );
508#else
509 switch( ctx->var )
510 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100511#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
512 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000513 {
Christoph M. Wintersteiger2e724a12019-01-07 14:19:41 +0000514 mbedtls_everest_ecdh_side s = side == MBEDTLS_ECDH_OURS ?
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000515 MBEDTLS_EVEREST_ECDH_OURS :
516 MBEDTLS_EVEREST_ECDH_THEIRS;
517 return( mbedtls_everest_get_params( &ctx->ctx.everest_ecdh,
518 key, s) );
519 }
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100520#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100521 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
522 return( ecdh_get_params_internal( &ctx->ctx.mbed_ecdh,
523 key, side ) );
524 default:
525 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
526 }
527#endif
528}
529
530static int ecdh_make_public_internal( mbedtls_ecdh_context_mbed *ctx,
531 size_t *olen, int point_format,
532 unsigned char *buf, size_t blen,
533 int (*f_rng)(void *,
534 unsigned char *,
535 size_t),
536 void *p_rng,
537 int restart_enabled )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100538{
539 int ret;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200540#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200541 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200542#endif
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100543
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100544 if( ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100546
Ron Eldorb430d9f2018-11-05 17:18:29 +0200547#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100548 if( restart_enabled )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200549 rs_ctx = &ctx->rs;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100550#else
551 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200552#endif
553
Ron Eldor2981d8f2018-11-05 18:07:10 +0200554#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200555 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100556 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100557 return( ret );
Ron Eldor2981d8f2018-11-05 18:07:10 +0200558#else
559 if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q,
560 f_rng, p_rng ) ) != 0 )
561 return( ret );
562#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100563
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100564 return mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, point_format, olen,
565 buf, blen );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100566}
567
568/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100569 * Setup and export the client public value
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100570 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100571int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
572 unsigned char *buf, size_t blen,
573 int (*f_rng)(void *, unsigned char *, size_t),
574 void *p_rng )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100575{
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100576 int restart_enabled = 0;
Hanno Becker91796d72018-12-17 18:10:51 +0000577 ECDH_VALIDATE_RET( ctx != NULL );
578 ECDH_VALIDATE_RET( olen != NULL );
579 ECDH_VALIDATE_RET( buf != NULL );
Hanno Beckerc81cfec2018-12-18 23:32:42 +0000580 ECDH_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100581
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100582#if defined(MBEDTLS_ECP_RESTARTABLE)
583 restart_enabled = ctx->restart_enabled;
584#endif
585
586#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
587 return( ecdh_make_public_internal( ctx, olen, ctx->point_format, buf, blen,
588 f_rng, p_rng, restart_enabled ) );
589#else
590 switch( ctx->var )
591 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100592#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
593 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000594 return( mbedtls_everest_make_public( &ctx->ctx.everest_ecdh, olen,
595 buf, blen, f_rng, p_rng ) );
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100596#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100597 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
598 return( ecdh_make_public_internal( &ctx->ctx.mbed_ecdh, olen,
599 ctx->point_format, buf, blen,
600 f_rng, p_rng,
601 restart_enabled ) );
602 default:
603 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
604 }
605#endif
606}
607
608static int ecdh_read_public_internal( mbedtls_ecdh_context_mbed *ctx,
609 const unsigned char *buf, size_t blen )
610{
611 int ret;
612 const unsigned char *p = buf;
613
614 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p,
615 blen ) ) != 0 )
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100616 return( ret );
617
618 if( (size_t)( p - buf ) != blen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100620
621 return( 0 );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100622}
623
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100624/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100625 * Parse and import the client's public value
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100626 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100627int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
628 const unsigned char *buf, size_t blen )
629{
Hanno Becker91796d72018-12-17 18:10:51 +0000630 ECDH_VALIDATE_RET( ctx != NULL );
631 ECDH_VALIDATE_RET( buf != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100632
633#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
634 return( ecdh_read_public_internal( ctx, buf, blen ) );
635#else
636 switch( ctx->var )
637 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100638#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
639 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000640 return( mbedtls_everest_read_public( &ctx->ctx.everest_ecdh,
641 buf, blen ) );
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100642#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100643 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
644 return( ecdh_read_public_internal( &ctx->ctx.mbed_ecdh,
645 buf, blen ) );
646 default:
647 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
648 }
649#endif
650}
651
652static int ecdh_calc_secret_internal( mbedtls_ecdh_context_mbed *ctx,
653 size_t *olen, unsigned char *buf,
654 size_t blen,
655 int (*f_rng)(void *,
656 unsigned char *,
657 size_t),
658 void *p_rng,
659 int restart_enabled )
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100660{
661 int ret;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200662#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200663 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200664#endif
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100665
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200666 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100668
Ron Eldorb430d9f2018-11-05 17:18:29 +0200669#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100670 if( restart_enabled )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200671 rs_ctx = &ctx->rs;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100672#else
673 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200674#endif
675
Ron Eldor2981d8f2018-11-05 18:07:10 +0200676#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100677 if( ( ret = ecdh_compute_shared_restartable( &ctx->grp, &ctx->z, &ctx->Qp,
678 &ctx->d, f_rng, p_rng,
679 rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200680 {
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100681 return( ret );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200682 }
Ron Eldor2981d8f2018-11-05 18:07:10 +0200683#else
684 if( ( ret = mbedtls_ecdh_compute_shared( &ctx->grp, &ctx->z, &ctx->Qp,
685 &ctx->d, f_rng, p_rng ) ) != 0 )
686 {
687 return( ret );
688 }
689#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 if( mbedtls_mpi_size( &ctx->z ) > blen )
692 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Paul Bakker41c83d32013-03-20 14:39:14 +0100693
Manuel Pégourié-Gonnard0a56c2c2014-01-17 21:24:04 +0100694 *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 );
Janos Follathab0f71a2019-02-20 10:48:49 +0000695
Janos Follath52ff8e92019-02-26 13:56:04 +0000696 if( mbedtls_ecp_get_type( &ctx->grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
Janos Follathab0f71a2019-02-20 10:48:49 +0000697 return mbedtls_mpi_write_binary_le( &ctx->z, buf, *olen );
698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 return mbedtls_mpi_write_binary( &ctx->z, buf, *olen );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100700}
701
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100702/*
703 * Derive and export the shared secret
704 */
705int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
706 unsigned char *buf, size_t blen,
707 int (*f_rng)(void *, unsigned char *, size_t),
708 void *p_rng )
709{
710 int restart_enabled = 0;
Hanno Becker91796d72018-12-17 18:10:51 +0000711 ECDH_VALIDATE_RET( ctx != NULL );
712 ECDH_VALIDATE_RET( olen != NULL );
713 ECDH_VALIDATE_RET( buf != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100714
715#if defined(MBEDTLS_ECP_RESTARTABLE)
716 restart_enabled = ctx->restart_enabled;
717#endif
718
719#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
720 return( ecdh_calc_secret_internal( ctx, olen, buf, blen, f_rng, p_rng,
721 restart_enabled ) );
722#else
723 switch( ctx->var )
724 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100725#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
726 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000727 return( mbedtls_everest_calc_secret( &ctx->ctx.everest_ecdh, olen,
728 buf, blen, f_rng, p_rng ) );
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100729#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100730 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
731 return( ecdh_calc_secret_internal( &ctx->ctx.mbed_ecdh, olen, buf,
732 blen, f_rng, p_rng,
733 restart_enabled ) );
734 default:
735 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
736 }
737#endif
738}
739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740#endif /* MBEDTLS_ECDH_C */