blob: f1609bde030cacdb491ebb540bb247745f1bd779 [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;
50#endif
51
Gilles Peskine30816292019-02-22 12:31:25 +010052static mbedtls_ecp_group_id mbedtls_ecdh_grp_id(
53 const mbedtls_ecdh_context *ctx )
54{
55#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
56 return( ctx->grp.id );
57#else
58 return( ctx->grp_id );
59#endif
60}
61
Gilles Peskine20b3ef32019-02-11 18:41:27 +010062int mbedtls_ecdh_can_do( mbedtls_ecp_group_id gid )
63{
64 /* At this time, all groups support ECDH. */
65 (void) gid;
66 return 1;
67}
68
Ron Eldora84c1cb2017-10-10 19:04:27 +030069#if !defined(MBEDTLS_ECDH_GEN_PUBLIC_ALT)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010070/*
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020071 * Generate public key (restartable version)
Manuel Pégourié-Gonnardc0edc962018-10-16 10:38:19 +020072 *
73 * Note: this internal function relies on its caller preserving the value of
Manuel Pégourié-Gonnardca29fdf2018-10-22 09:56:53 +020074 * the output parameter 'd' across continuation calls. This would not be
Manuel Pégourié-Gonnardc0edc962018-10-16 10:38:19 +020075 * acceptable for a public function but is OK here as we control call sites.
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020076 */
77static int ecdh_gen_public_restartable( mbedtls_ecp_group *grp,
78 mbedtls_mpi *d, mbedtls_ecp_point *Q,
79 int (*f_rng)(void *, unsigned char *, size_t),
80 void *p_rng,
81 mbedtls_ecp_restart_ctx *rs_ctx )
82{
83 int ret;
84
85 /* If multiplication is in progress, we already generated a privkey */
86#if defined(MBEDTLS_ECP_RESTARTABLE)
87 if( rs_ctx == NULL || rs_ctx->rsm == NULL )
88#endif
89 MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, d, f_rng, p_rng ) );
90
91 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, Q, d, &grp->G,
92 f_rng, p_rng, rs_ctx ) );
93
94cleanup:
95 return( ret );
96}
97
98/*
99 * Generate public key
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100100 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100102 int (*f_rng)(void *, unsigned char *, size_t),
103 void *p_rng )
104{
Hanno Becker91796d72018-12-17 18:10:51 +0000105 ECDH_VALIDATE_RET( grp != NULL );
106 ECDH_VALIDATE_RET( d != NULL );
107 ECDH_VALIDATE_RET( Q != NULL );
108 ECDH_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200109 return( ecdh_gen_public_restartable( grp, d, Q, f_rng, p_rng, NULL ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100110}
Ron Eldor936d2842018-11-01 13:05:52 +0200111#endif /* !MBEDTLS_ECDH_GEN_PUBLIC_ALT */
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100112
Ron Eldora84c1cb2017-10-10 19:04:27 +0300113#if !defined(MBEDTLS_ECDH_COMPUTE_SHARED_ALT)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100114/*
115 * Compute shared secret (SEC1 3.3.1)
116 */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200117static int ecdh_compute_shared_restartable( mbedtls_ecp_group *grp,
118 mbedtls_mpi *z,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200120 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200121 void *p_rng,
122 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100123{
124 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 mbedtls_ecp_point P;
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_ecp_point_init( &P );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100128
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200129 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, &P, d, Q,
130 f_rng, p_rng, rs_ctx ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 if( mbedtls_ecp_is_zero( &P ) )
Paul Bakkerb548d772013-07-26 14:21:34 +0200133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Paul Bakkerb548d772013-07-26 14:21:34 +0200135 goto cleanup;
136 }
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( z, &P.X ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100139
140cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 mbedtls_ecp_point_free( &P );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100142
143 return( ret );
144}
145
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100146/*
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200147 * Compute shared secret (SEC1 3.3.1)
148 */
149int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
150 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
151 int (*f_rng)(void *, unsigned char *, size_t),
152 void *p_rng )
153{
Hanno Becker91796d72018-12-17 18:10:51 +0000154 ECDH_VALIDATE_RET( grp != NULL );
155 ECDH_VALIDATE_RET( Q != NULL );
156 ECDH_VALIDATE_RET( d != NULL );
157 ECDH_VALIDATE_RET( z != NULL );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200158 return( ecdh_compute_shared_restartable( grp, z, Q, d,
159 f_rng, p_rng, NULL ) );
160}
Ron Eldor936d2842018-11-01 13:05:52 +0200161#endif /* !MBEDTLS_ECDH_COMPUTE_SHARED_ALT */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200162
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100163static void ecdh_init_internal( mbedtls_ecdh_context_mbed *ctx )
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100164{
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200165 mbedtls_ecp_group_init( &ctx->grp );
166 mbedtls_mpi_init( &ctx->d );
167 mbedtls_ecp_point_init( &ctx->Q );
168 mbedtls_ecp_point_init( &ctx->Qp );
169 mbedtls_mpi_init( &ctx->z );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200170
171#if defined(MBEDTLS_ECP_RESTARTABLE)
172 mbedtls_ecp_restart_init( &ctx->rs );
173#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100174}
175
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100176/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100177 * Initialize context
Janos Follathf61e4862018-10-30 11:53:25 +0000178 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100179void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx )
180{
Hanno Becker91796d72018-12-17 18:10:51 +0000181 ECDH_VALIDATE( ctx != NULL );
182
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100183#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
184 ecdh_init_internal( ctx );
185 mbedtls_ecp_point_init( &ctx->Vi );
186 mbedtls_ecp_point_init( &ctx->Vf );
187 mbedtls_mpi_init( &ctx->_d );
188#else
Christoph M. Wintersteigerd8c45d52019-02-20 17:16:53 +0000189 memset( ctx, 0, sizeof( mbedtls_ecdh_context ) );
190
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100191 ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
192#endif
193 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
194#if defined(MBEDTLS_ECP_RESTARTABLE)
195 ctx->restart_enabled = 0;
196#endif
197}
198
199static int ecdh_setup_internal( mbedtls_ecdh_context_mbed *ctx,
200 mbedtls_ecp_group_id grp_id )
Janos Follathf61e4862018-10-30 11:53:25 +0000201{
202 int ret;
203
204 ret = mbedtls_ecp_group_load( &ctx->grp, grp_id );
205 if( ret != 0 )
206 {
Janos Follathf61e4862018-10-30 11:53:25 +0000207 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
208 }
209
210 return( 0 );
211}
212
213/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100214 * Setup context
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100215 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100216int mbedtls_ecdh_setup( mbedtls_ecdh_context *ctx, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100217{
Hanno Becker91796d72018-12-17 18:10:51 +0000218 ECDH_VALIDATE_RET( ctx != NULL );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100219
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100220#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
221 return( ecdh_setup_internal( ctx, grp_id ) );
222#else
223 switch( grp_id )
224 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100225#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
Christoph M. Wintersteiger3ff60bc2019-02-15 12:59:59 +0000226 case MBEDTLS_ECP_DP_CURVE25519:
227 ctx->point_format = MBEDTLS_ECP_PF_COMPRESSED;
228 ctx->var = MBEDTLS_ECDH_VARIANT_EVEREST;
229 ctx->grp_id = grp_id;
230 return( mbedtls_everest_setup( &ctx->ctx.everest_ecdh, grp_id ) );
Christoph M. Wintersteiger78c9c462018-12-06 17:16:32 +0000231#endif
Christoph M. Wintersteiger3ff60bc2019-02-15 12:59:59 +0000232 default:
233 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
234 ctx->var = MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0;
235 ctx->grp_id = grp_id;
236 ecdh_init_internal( &ctx->ctx.mbed_ecdh );
237 return( ecdh_setup_internal( &ctx->ctx.mbed_ecdh, grp_id ) );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100238 }
239#endif
240}
241
242static void ecdh_free_internal( mbedtls_ecdh_context_mbed *ctx )
243{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_ecp_group_free( &ctx->grp );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200245 mbedtls_mpi_free( &ctx->d );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 mbedtls_ecp_point_free( &ctx->Q );
247 mbedtls_ecp_point_free( &ctx->Qp );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200248 mbedtls_mpi_free( &ctx->z );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200249
250#if defined(MBEDTLS_ECP_RESTARTABLE)
251 mbedtls_ecp_restart_free( &ctx->rs );
252#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100253}
254
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200255#if defined(MBEDTLS_ECP_RESTARTABLE)
256/*
257 * Enable restartable operations for context
258 */
259void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx )
260{
Hanno Beckera7634e82018-12-18 18:45:00 +0000261 ECDH_VALIDATE( ctx != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100262
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200263 ctx->restart_enabled = 1;
264}
265#endif
266
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100267/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100268 * Free context
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100269 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100270void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx )
271{
272 if( ctx == NULL )
273 return;
274
275#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
276 mbedtls_ecp_point_free( &ctx->Vi );
277 mbedtls_ecp_point_free( &ctx->Vf );
278 mbedtls_mpi_free( &ctx->_d );
279 ecdh_free_internal( ctx );
280#else
281 switch( ctx->var )
282 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100283#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
284 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000285 mbedtls_everest_free( &ctx->ctx.everest_ecdh );
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100286 break;
287#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100288 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
289 ecdh_free_internal( &ctx->ctx.mbed_ecdh );
290 break;
291 default:
292 break;
293 }
294
295 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
296 ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
297 ctx->grp_id = MBEDTLS_ECP_DP_NONE;
298#endif
299}
300
301static int ecdh_make_params_internal( mbedtls_ecdh_context_mbed *ctx,
302 size_t *olen, int point_format,
303 unsigned char *buf, size_t blen,
304 int (*f_rng)(void *,
305 unsigned char *,
306 size_t),
307 void *p_rng,
308 int restart_enabled )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100309{
310 int ret;
311 size_t grp_len, pt_len;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200312#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200313 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200314#endif
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100315
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100316 if( ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100318
Ron Eldor19779c42018-11-05 16:58:13 +0200319#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100320 if( restart_enabled )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200321 rs_ctx = &ctx->rs;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100322#else
323 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200324#endif
325
Ron Eldor8493f802018-11-01 11:32:15 +0200326
Ron Eldor2981d8f2018-11-05 18:07:10 +0200327#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200328 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200329 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100330 return( ret );
Ron Eldor2981d8f2018-11-05 18:07:10 +0200331#else
332 if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q,
333 f_rng, p_rng ) ) != 0 )
334 return( ret );
335#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100336
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100337 if( ( ret = mbedtls_ecp_tls_write_group( &ctx->grp, &grp_len, buf,
338 blen ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100339 return( ret );
340
341 buf += grp_len;
342 blen -= grp_len;
343
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100344 if( ( ret = mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, point_format,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200345 &pt_len, buf, blen ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100346 return( ret );
347
348 *olen = grp_len + pt_len;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200349 return( 0 );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100350}
351
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100352/*
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100353 * Setup and write the ServerKeyExchange parameters (RFC 4492)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100354 * struct {
355 * ECParameters curve_params;
356 * ECPoint public;
357 * } ServerECDHParams;
358 */
359int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
360 unsigned char *buf, size_t blen,
361 int (*f_rng)(void *, unsigned char *, size_t),
362 void *p_rng )
363{
364 int restart_enabled = 0;
Hanno Becker91796d72018-12-17 18:10:51 +0000365 ECDH_VALIDATE_RET( ctx != NULL );
366 ECDH_VALIDATE_RET( olen != NULL );
367 ECDH_VALIDATE_RET( buf != NULL );
368 ECDH_VALIDATE_RET( f_rng != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100369
370#if defined(MBEDTLS_ECP_RESTARTABLE)
371 restart_enabled = ctx->restart_enabled;
372#else
373 (void) restart_enabled;
374#endif
375
376#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
377 return( ecdh_make_params_internal( ctx, olen, ctx->point_format, buf, blen,
378 f_rng, p_rng, restart_enabled ) );
379#else
380 switch( ctx->var )
381 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100382#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
383 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000384 return( mbedtls_everest_make_params( &ctx->ctx.everest_ecdh, olen,
385 buf, blen, f_rng, p_rng ) );
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100386#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100387 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
388 return( ecdh_make_params_internal( &ctx->ctx.mbed_ecdh, olen,
389 ctx->point_format, buf, blen,
390 f_rng, p_rng,
391 restart_enabled ) );
392 default:
393 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
394 }
395#endif
396}
397
398static int ecdh_read_params_internal( mbedtls_ecdh_context_mbed *ctx,
399 const unsigned char **buf,
400 const unsigned char *end )
401{
402 return( mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf,
403 end - *buf ) );
404}
405
406/*
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100407 * Read the ServerKeyExhange parameters (RFC 4492)
408 * struct {
409 * ECParameters curve_params;
410 * ECPoint public;
411 * } ServerECDHParams;
412 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100414 const unsigned char **buf,
415 const unsigned char *end )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100416{
417 int ret;
Janos Follathf61e4862018-10-30 11:53:25 +0000418 mbedtls_ecp_group_id grp_id;
Hanno Becker91796d72018-12-17 18:10:51 +0000419 ECDH_VALIDATE_RET( ctx != NULL );
420 ECDH_VALIDATE_RET( buf != NULL );
421 ECDH_VALIDATE_RET( *buf != NULL );
422 ECDH_VALIDATE_RET( end != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100423
Janos Follathf61e4862018-10-30 11:53:25 +0000424 if( ( ret = mbedtls_ecp_tls_read_group_id( &grp_id, buf, end - *buf ) )
425 != 0 )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100426 return( ret );
427
Janos Follathf61e4862018-10-30 11:53:25 +0000428 if( ( ret = mbedtls_ecdh_setup( ctx, grp_id ) ) != 0 )
429 return( ret );
430
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100431#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
432 return( ecdh_read_params_internal( ctx, buf, end ) );
433#else
434 switch( ctx->var )
435 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100436#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
437 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000438 return( mbedtls_everest_read_params( &ctx->ctx.everest_ecdh,
439 buf, end) );
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100440#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100441 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
442 return( ecdh_read_params_internal( &ctx->ctx.mbed_ecdh,
443 buf, end ) );
444 default:
445 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
446 }
447#endif
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100448}
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100449
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100450static int ecdh_get_params_internal( mbedtls_ecdh_context_mbed *ctx,
451 const mbedtls_ecp_keypair *key,
452 mbedtls_ecdh_side side )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100453{
454 int ret;
455
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100456 /* If it's not our key, just import the public part as Qp */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 if( side == MBEDTLS_ECDH_THEIRS )
458 return( mbedtls_ecp_copy( &ctx->Qp, &key->Q ) );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100459
460 /* Our key: import public (as Q) and private parts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 if( side != MBEDTLS_ECDH_OURS )
462 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 if( ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 ||
465 ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100466 return( ret );
467
468 return( 0 );
469}
470
471/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100472 * Get parameters from a keypair
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100473 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100474int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx,
475 const mbedtls_ecp_keypair *key,
476 mbedtls_ecdh_side side )
477{
478 int ret;
Hanno Becker91796d72018-12-17 18:10:51 +0000479 ECDH_VALIDATE_RET( ctx != NULL );
480 ECDH_VALIDATE_RET( key != NULL );
481 ECDH_VALIDATE_RET( side == MBEDTLS_ECDH_OURS ||
482 side == MBEDTLS_ECDH_THEIRS );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100483
Gilles Peskine30816292019-02-22 12:31:25 +0100484 if( mbedtls_ecdh_grp_id( ctx ) == MBEDTLS_ECP_DP_NONE )
Gilles Peskine0b1b71d2018-11-07 22:10:59 +0100485 {
486 /* This is the first call to get_params(). Set up the context
487 * for use with the group. */
488 if( ( ret = mbedtls_ecdh_setup( ctx, key->grp.id ) ) != 0 )
489 return( ret );
490 }
491 else
492 {
493 /* This is not the first call to get_params(). Check that the
494 * current key's group is the same as the context's, which was set
495 * from the first key's group. */
Gilles Peskine30816292019-02-22 12:31:25 +0100496 if( mbedtls_ecdh_grp_id( ctx ) != key->grp.id )
Gilles Peskine0b1b71d2018-11-07 22:10:59 +0100497 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
498 }
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100499
500#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
501 return( ecdh_get_params_internal( ctx, key, side ) );
502#else
503 switch( ctx->var )
504 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100505#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
506 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000507 {
Christoph M. Wintersteiger2e724a12019-01-07 14:19:41 +0000508 mbedtls_everest_ecdh_side s = side == MBEDTLS_ECDH_OURS ?
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000509 MBEDTLS_EVEREST_ECDH_OURS :
510 MBEDTLS_EVEREST_ECDH_THEIRS;
511 return( mbedtls_everest_get_params( &ctx->ctx.everest_ecdh,
512 key, s) );
513 }
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100514#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100515 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
516 return( ecdh_get_params_internal( &ctx->ctx.mbed_ecdh,
517 key, side ) );
518 default:
519 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
520 }
521#endif
522}
523
524static int ecdh_make_public_internal( mbedtls_ecdh_context_mbed *ctx,
525 size_t *olen, int point_format,
526 unsigned char *buf, size_t blen,
527 int (*f_rng)(void *,
528 unsigned char *,
529 size_t),
530 void *p_rng,
531 int restart_enabled )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100532{
533 int ret;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200534#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200535 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200536#endif
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100537
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100538 if( ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100540
Ron Eldorb430d9f2018-11-05 17:18:29 +0200541#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100542 if( restart_enabled )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200543 rs_ctx = &ctx->rs;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100544#else
545 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200546#endif
547
Ron Eldor2981d8f2018-11-05 18:07:10 +0200548#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200549 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100550 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100551 return( ret );
Ron Eldor2981d8f2018-11-05 18:07:10 +0200552#else
553 if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q,
554 f_rng, p_rng ) ) != 0 )
555 return( ret );
556#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100557
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100558 return mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, point_format, olen,
559 buf, blen );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100560}
561
562/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100563 * Setup and export the client public value
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100564 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100565int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
566 unsigned char *buf, size_t blen,
567 int (*f_rng)(void *, unsigned char *, size_t),
568 void *p_rng )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100569{
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100570 int restart_enabled = 0;
Hanno Becker91796d72018-12-17 18:10:51 +0000571 ECDH_VALIDATE_RET( ctx != NULL );
572 ECDH_VALIDATE_RET( olen != NULL );
573 ECDH_VALIDATE_RET( buf != NULL );
Hanno Beckerc81cfec2018-12-18 23:32:42 +0000574 ECDH_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100575
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100576#if defined(MBEDTLS_ECP_RESTARTABLE)
577 restart_enabled = ctx->restart_enabled;
578#endif
579
580#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
581 return( ecdh_make_public_internal( ctx, olen, ctx->point_format, buf, blen,
582 f_rng, p_rng, restart_enabled ) );
583#else
584 switch( ctx->var )
585 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100586#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
587 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000588 return( mbedtls_everest_make_public( &ctx->ctx.everest_ecdh, olen,
589 buf, blen, f_rng, p_rng ) );
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100590#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100591 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
592 return( ecdh_make_public_internal( &ctx->ctx.mbed_ecdh, olen,
593 ctx->point_format, buf, blen,
594 f_rng, p_rng,
595 restart_enabled ) );
596 default:
597 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
598 }
599#endif
600}
601
602static int ecdh_read_public_internal( mbedtls_ecdh_context_mbed *ctx,
603 const unsigned char *buf, size_t blen )
604{
605 int ret;
606 const unsigned char *p = buf;
607
608 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p,
609 blen ) ) != 0 )
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100610 return( ret );
611
612 if( (size_t)( p - buf ) != blen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100614
615 return( 0 );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100616}
617
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100618/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100619 * Parse and import the client's public value
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100620 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100621int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
622 const unsigned char *buf, size_t blen )
623{
Hanno Becker91796d72018-12-17 18:10:51 +0000624 ECDH_VALIDATE_RET( ctx != NULL );
625 ECDH_VALIDATE_RET( buf != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100626
627#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
628 return( ecdh_read_public_internal( ctx, buf, blen ) );
629#else
630 switch( ctx->var )
631 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100632#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
633 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000634 return( mbedtls_everest_read_public( &ctx->ctx.everest_ecdh,
635 buf, blen ) );
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100636#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100637 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
638 return( ecdh_read_public_internal( &ctx->ctx.mbed_ecdh,
639 buf, blen ) );
640 default:
641 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
642 }
643#endif
644}
645
646static int ecdh_calc_secret_internal( mbedtls_ecdh_context_mbed *ctx,
647 size_t *olen, unsigned char *buf,
648 size_t blen,
649 int (*f_rng)(void *,
650 unsigned char *,
651 size_t),
652 void *p_rng,
653 int restart_enabled )
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100654{
655 int ret;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200656#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200657 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200658#endif
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100659
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200660 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100662
Ron Eldorb430d9f2018-11-05 17:18:29 +0200663#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100664 if( restart_enabled )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200665 rs_ctx = &ctx->rs;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100666#else
667 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200668#endif
669
Ron Eldor2981d8f2018-11-05 18:07:10 +0200670#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100671 if( ( ret = ecdh_compute_shared_restartable( &ctx->grp, &ctx->z, &ctx->Qp,
672 &ctx->d, f_rng, p_rng,
673 rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200674 {
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100675 return( ret );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200676 }
Ron Eldor2981d8f2018-11-05 18:07:10 +0200677#else
678 if( ( ret = mbedtls_ecdh_compute_shared( &ctx->grp, &ctx->z, &ctx->Qp,
679 &ctx->d, f_rng, p_rng ) ) != 0 )
680 {
681 return( ret );
682 }
683#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 if( mbedtls_mpi_size( &ctx->z ) > blen )
686 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Paul Bakker41c83d32013-03-20 14:39:14 +0100687
Manuel Pégourié-Gonnard0a56c2c2014-01-17 21:24:04 +0100688 *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 );
Janos Follathab0f71a2019-02-20 10:48:49 +0000689
Janos Follath52ff8e92019-02-26 13:56:04 +0000690 if( mbedtls_ecp_get_type( &ctx->grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
Janos Follathab0f71a2019-02-20 10:48:49 +0000691 return mbedtls_mpi_write_binary_le( &ctx->z, buf, *olen );
692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 return mbedtls_mpi_write_binary( &ctx->z, buf, *olen );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100694}
695
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100696/*
697 * Derive and export the shared secret
698 */
699int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
700 unsigned char *buf, size_t blen,
701 int (*f_rng)(void *, unsigned char *, size_t),
702 void *p_rng )
703{
704 int restart_enabled = 0;
Hanno Becker91796d72018-12-17 18:10:51 +0000705 ECDH_VALIDATE_RET( ctx != NULL );
706 ECDH_VALIDATE_RET( olen != NULL );
707 ECDH_VALIDATE_RET( buf != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100708
709#if defined(MBEDTLS_ECP_RESTARTABLE)
710 restart_enabled = ctx->restart_enabled;
711#endif
712
713#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
714 return( ecdh_calc_secret_internal( ctx, olen, buf, blen, f_rng, p_rng,
715 restart_enabled ) );
716#else
717 switch( ctx->var )
718 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100719#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
720 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000721 return( mbedtls_everest_calc_secret( &ctx->ctx.everest_ecdh, olen,
722 buf, blen, f_rng, p_rng ) );
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100723#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100724 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
725 return( ecdh_calc_secret_internal( &ctx->ctx.mbed_ecdh, olen, buf,
726 blen, f_rng, p_rng,
727 restart_enabled ) );
728 default:
729 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
730 }
731#endif
732}
733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734#endif /* MBEDTLS_ECDH_C */