blob: 35e3f4efc3f38eeb2cca0dbbfb8541e56c5a547d [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
189 memset( ctx, 0, sizeof( mbedtls_ecdh_context ) );
190
191 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 );
286 ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
287 ctx->grp_id = MBEDTLS_ECP_DP_NONE;
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100288 break;
289#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100290 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
291 ecdh_free_internal( &ctx->ctx.mbed_ecdh );
292 break;
293 default:
294 break;
295 }
296
297 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
298 ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
299 ctx->grp_id = MBEDTLS_ECP_DP_NONE;
300#endif
301}
302
303static int ecdh_make_params_internal( mbedtls_ecdh_context_mbed *ctx,
304 size_t *olen, int point_format,
305 unsigned char *buf, size_t blen,
306 int (*f_rng)(void *,
307 unsigned char *,
308 size_t),
309 void *p_rng,
310 int restart_enabled )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100311{
312 int ret;
313 size_t grp_len, pt_len;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200314#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200315 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200316#endif
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100317
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100318 if( ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100320
Ron Eldor19779c42018-11-05 16:58:13 +0200321#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100322 if( restart_enabled )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200323 rs_ctx = &ctx->rs;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100324#else
325 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200326#endif
327
Ron Eldor8493f802018-11-01 11:32:15 +0200328
Ron Eldor2981d8f2018-11-05 18:07:10 +0200329#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200330 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200331 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100332 return( ret );
Ron Eldor2981d8f2018-11-05 18:07:10 +0200333#else
334 if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q,
335 f_rng, p_rng ) ) != 0 )
336 return( ret );
337#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100338
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100339 if( ( ret = mbedtls_ecp_tls_write_group( &ctx->grp, &grp_len, buf,
340 blen ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100341 return( ret );
342
343 buf += grp_len;
344 blen -= grp_len;
345
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100346 if( ( ret = mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, point_format,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200347 &pt_len, buf, blen ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100348 return( ret );
349
350 *olen = grp_len + pt_len;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200351 return( 0 );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100352}
353
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100354/*
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100355 * Setup and write the ServerKeyExchange parameters (RFC 4492)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100356 * struct {
357 * ECParameters curve_params;
358 * ECPoint public;
359 * } ServerECDHParams;
360 */
361int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
362 unsigned char *buf, size_t blen,
363 int (*f_rng)(void *, unsigned char *, size_t),
364 void *p_rng )
365{
366 int restart_enabled = 0;
Hanno Becker91796d72018-12-17 18:10:51 +0000367 ECDH_VALIDATE_RET( ctx != NULL );
368 ECDH_VALIDATE_RET( olen != NULL );
369 ECDH_VALIDATE_RET( buf != NULL );
370 ECDH_VALIDATE_RET( f_rng != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100371
372#if defined(MBEDTLS_ECP_RESTARTABLE)
373 restart_enabled = ctx->restart_enabled;
374#else
375 (void) restart_enabled;
376#endif
377
378#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
379 return( ecdh_make_params_internal( ctx, olen, ctx->point_format, buf, blen,
380 f_rng, p_rng, restart_enabled ) );
381#else
382 switch( ctx->var )
383 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100384#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
385 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000386 return( mbedtls_everest_make_params( &ctx->ctx.everest_ecdh, olen,
387 buf, blen, f_rng, p_rng ) );
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100388#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100389 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
390 return( ecdh_make_params_internal( &ctx->ctx.mbed_ecdh, olen,
391 ctx->point_format, buf, blen,
392 f_rng, p_rng,
393 restart_enabled ) );
394 default:
395 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
396 }
397#endif
398}
399
400static int ecdh_read_params_internal( mbedtls_ecdh_context_mbed *ctx,
401 const unsigned char **buf,
402 const unsigned char *end )
403{
404 return( mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf,
405 end - *buf ) );
406}
407
408/*
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100409 * Read the ServerKeyExhange parameters (RFC 4492)
410 * struct {
411 * ECParameters curve_params;
412 * ECPoint public;
413 * } ServerECDHParams;
414 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100416 const unsigned char **buf,
417 const unsigned char *end )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100418{
419 int ret;
Janos Follathf61e4862018-10-30 11:53:25 +0000420 mbedtls_ecp_group_id grp_id;
Hanno Becker91796d72018-12-17 18:10:51 +0000421 ECDH_VALIDATE_RET( ctx != NULL );
422 ECDH_VALIDATE_RET( buf != NULL );
423 ECDH_VALIDATE_RET( *buf != NULL );
424 ECDH_VALIDATE_RET( end != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100425
Janos Follathf61e4862018-10-30 11:53:25 +0000426 if( ( ret = mbedtls_ecp_tls_read_group_id( &grp_id, buf, end - *buf ) )
427 != 0 )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100428 return( ret );
429
Janos Follathf61e4862018-10-30 11:53:25 +0000430 if( ( ret = mbedtls_ecdh_setup( ctx, grp_id ) ) != 0 )
431 return( ret );
432
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100433#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
434 return( ecdh_read_params_internal( ctx, buf, end ) );
435#else
436 switch( ctx->var )
437 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100438#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
439 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000440 return( mbedtls_everest_read_params( &ctx->ctx.everest_ecdh,
441 buf, end) );
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100442#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100443 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
444 return( ecdh_read_params_internal( &ctx->ctx.mbed_ecdh,
445 buf, end ) );
446 default:
447 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
448 }
449#endif
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100450}
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100451
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100452static int ecdh_get_params_internal( mbedtls_ecdh_context_mbed *ctx,
453 const mbedtls_ecp_keypair *key,
454 mbedtls_ecdh_side side )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100455{
456 int ret;
457
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100458 /* If it's not our key, just import the public part as Qp */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 if( side == MBEDTLS_ECDH_THEIRS )
460 return( mbedtls_ecp_copy( &ctx->Qp, &key->Q ) );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100461
462 /* Our key: import public (as Q) and private parts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 if( side != MBEDTLS_ECDH_OURS )
464 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 if( ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 ||
467 ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100468 return( ret );
469
470 return( 0 );
471}
472
473/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100474 * Get parameters from a keypair
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100475 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100476int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx,
477 const mbedtls_ecp_keypair *key,
478 mbedtls_ecdh_side side )
479{
480 int ret;
Hanno Becker91796d72018-12-17 18:10:51 +0000481 ECDH_VALIDATE_RET( ctx != NULL );
482 ECDH_VALIDATE_RET( key != NULL );
483 ECDH_VALIDATE_RET( side == MBEDTLS_ECDH_OURS ||
484 side == MBEDTLS_ECDH_THEIRS );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100485
Gilles Peskine30816292019-02-22 12:31:25 +0100486 if( mbedtls_ecdh_grp_id( ctx ) == MBEDTLS_ECP_DP_NONE )
Gilles Peskine0b1b71d2018-11-07 22:10:59 +0100487 {
488 /* This is the first call to get_params(). Set up the context
489 * for use with the group. */
490 if( ( ret = mbedtls_ecdh_setup( ctx, key->grp.id ) ) != 0 )
491 return( ret );
492 }
493 else
494 {
495 /* This is not the first call to get_params(). Check that the
496 * current key's group is the same as the context's, which was set
497 * from the first key's group. */
Gilles Peskine30816292019-02-22 12:31:25 +0100498 if( mbedtls_ecdh_grp_id( ctx ) != key->grp.id )
Gilles Peskine0b1b71d2018-11-07 22:10:59 +0100499 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
500 }
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100501
502#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
503 return( ecdh_get_params_internal( ctx, key, side ) );
504#else
505 switch( ctx->var )
506 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100507#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
508 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000509 {
Christoph M. Wintersteiger2e724a12019-01-07 14:19:41 +0000510 mbedtls_everest_ecdh_side s = side == MBEDTLS_ECDH_OURS ?
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000511 MBEDTLS_EVEREST_ECDH_OURS :
512 MBEDTLS_EVEREST_ECDH_THEIRS;
513 return( mbedtls_everest_get_params( &ctx->ctx.everest_ecdh,
514 key, s) );
515 }
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100516#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100517 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
518 return( ecdh_get_params_internal( &ctx->ctx.mbed_ecdh,
519 key, side ) );
520 default:
521 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
522 }
523#endif
524}
525
526static int ecdh_make_public_internal( mbedtls_ecdh_context_mbed *ctx,
527 size_t *olen, int point_format,
528 unsigned char *buf, size_t blen,
529 int (*f_rng)(void *,
530 unsigned char *,
531 size_t),
532 void *p_rng,
533 int restart_enabled )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100534{
535 int ret;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200536#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200537 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200538#endif
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100539
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100540 if( ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100542
Ron Eldorb430d9f2018-11-05 17:18:29 +0200543#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100544 if( restart_enabled )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200545 rs_ctx = &ctx->rs;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100546#else
547 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200548#endif
549
Ron Eldor2981d8f2018-11-05 18:07:10 +0200550#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200551 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100552 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100553 return( ret );
Ron Eldor2981d8f2018-11-05 18:07:10 +0200554#else
555 if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q,
556 f_rng, p_rng ) ) != 0 )
557 return( ret );
558#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100559
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100560 return mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, point_format, olen,
561 buf, blen );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100562}
563
564/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100565 * Setup and export the client public value
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100566 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100567int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
568 unsigned char *buf, size_t blen,
569 int (*f_rng)(void *, unsigned char *, size_t),
570 void *p_rng )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100571{
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100572 int restart_enabled = 0;
Hanno Becker91796d72018-12-17 18:10:51 +0000573 ECDH_VALIDATE_RET( ctx != NULL );
574 ECDH_VALIDATE_RET( olen != NULL );
575 ECDH_VALIDATE_RET( buf != NULL );
Hanno Beckerc81cfec2018-12-18 23:32:42 +0000576 ECDH_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100577
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100578#if defined(MBEDTLS_ECP_RESTARTABLE)
579 restart_enabled = ctx->restart_enabled;
580#endif
581
582#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
583 return( ecdh_make_public_internal( ctx, olen, ctx->point_format, buf, blen,
584 f_rng, p_rng, restart_enabled ) );
585#else
586 switch( ctx->var )
587 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100588#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
589 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000590 return( mbedtls_everest_make_public( &ctx->ctx.everest_ecdh, olen,
591 buf, blen, f_rng, p_rng ) );
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100592#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100593 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
594 return( ecdh_make_public_internal( &ctx->ctx.mbed_ecdh, olen,
595 ctx->point_format, buf, blen,
596 f_rng, p_rng,
597 restart_enabled ) );
598 default:
599 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
600 }
601#endif
602}
603
604static int ecdh_read_public_internal( mbedtls_ecdh_context_mbed *ctx,
605 const unsigned char *buf, size_t blen )
606{
607 int ret;
608 const unsigned char *p = buf;
609
610 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p,
611 blen ) ) != 0 )
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100612 return( ret );
613
614 if( (size_t)( p - buf ) != blen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100616
617 return( 0 );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100618}
619
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100620/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100621 * Parse and import the client's public value
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100622 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100623int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
624 const unsigned char *buf, size_t blen )
625{
Hanno Becker91796d72018-12-17 18:10:51 +0000626 ECDH_VALIDATE_RET( ctx != NULL );
627 ECDH_VALIDATE_RET( buf != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100628
629#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
630 return( ecdh_read_public_internal( ctx, buf, blen ) );
631#else
632 switch( ctx->var )
633 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100634#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
635 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000636 return( mbedtls_everest_read_public( &ctx->ctx.everest_ecdh,
637 buf, blen ) );
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100638#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100639 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
640 return( ecdh_read_public_internal( &ctx->ctx.mbed_ecdh,
641 buf, blen ) );
642 default:
643 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
644 }
645#endif
646}
647
648static int ecdh_calc_secret_internal( mbedtls_ecdh_context_mbed *ctx,
649 size_t *olen, unsigned char *buf,
650 size_t blen,
651 int (*f_rng)(void *,
652 unsigned char *,
653 size_t),
654 void *p_rng,
655 int restart_enabled )
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100656{
657 int ret;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200658#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200659 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200660#endif
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100661
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200662 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100664
Ron Eldorb430d9f2018-11-05 17:18:29 +0200665#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100666 if( restart_enabled )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200667 rs_ctx = &ctx->rs;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100668#else
669 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200670#endif
671
Ron Eldor2981d8f2018-11-05 18:07:10 +0200672#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100673 if( ( ret = ecdh_compute_shared_restartable( &ctx->grp, &ctx->z, &ctx->Qp,
674 &ctx->d, f_rng, p_rng,
675 rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200676 {
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100677 return( ret );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200678 }
Ron Eldor2981d8f2018-11-05 18:07:10 +0200679#else
680 if( ( ret = mbedtls_ecdh_compute_shared( &ctx->grp, &ctx->z, &ctx->Qp,
681 &ctx->d, f_rng, p_rng ) ) != 0 )
682 {
683 return( ret );
684 }
685#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 if( mbedtls_mpi_size( &ctx->z ) > blen )
688 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Paul Bakker41c83d32013-03-20 14:39:14 +0100689
Manuel Pégourié-Gonnard0a56c2c2014-01-17 21:24:04 +0100690 *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 );
Janos Follathab0f71a2019-02-20 10:48:49 +0000691
Janos Follath52ff8e92019-02-26 13:56:04 +0000692 if( mbedtls_ecp_get_type( &ctx->grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
Janos Follathab0f71a2019-02-20 10:48:49 +0000693 return mbedtls_mpi_write_binary_le( &ctx->z, buf, *olen );
694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 return mbedtls_mpi_write_binary( &ctx->z, buf, *olen );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100696}
697
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100698/*
699 * Derive and export the shared secret
700 */
701int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
702 unsigned char *buf, size_t blen,
703 int (*f_rng)(void *, unsigned char *, size_t),
704 void *p_rng )
705{
706 int restart_enabled = 0;
Hanno Becker91796d72018-12-17 18:10:51 +0000707 ECDH_VALIDATE_RET( ctx != NULL );
708 ECDH_VALIDATE_RET( olen != NULL );
709 ECDH_VALIDATE_RET( buf != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100710
711#if defined(MBEDTLS_ECP_RESTARTABLE)
712 restart_enabled = ctx->restart_enabled;
713#endif
714
715#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
716 return( ecdh_calc_secret_internal( ctx, olen, buf, blen, f_rng, p_rng,
717 restart_enabled ) );
718#else
719 switch( ctx->var )
720 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100721#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
722 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000723 return( mbedtls_everest_calc_secret( &ctx->ctx.everest_ecdh, olen,
724 buf, blen, f_rng, p_rng ) );
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100725#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100726 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
727 return( ecdh_calc_secret_internal( &ctx->ctx.mbed_ecdh, olen, buf,
728 blen, f_rng, p_rng,
729 restart_enabled ) );
730 default:
731 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
732 }
733#endif
734}
735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736#endif /* MBEDTLS_ECDH_C */