blob: 66a2d1687aacdebce364eb1e82fc85b076008021 [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
Ron Eldora84c1cb2017-10-10 19:04:27 +030066#if !defined(MBEDTLS_ECDH_GEN_PUBLIC_ALT)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010067/*
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020068 * Generate public key (restartable version)
Manuel Pégourié-Gonnardc0edc962018-10-16 10:38:19 +020069 *
70 * Note: this internal function relies on its caller preserving the value of
Manuel Pégourié-Gonnardca29fdf2018-10-22 09:56:53 +020071 * the output parameter 'd' across continuation calls. This would not be
Manuel Pégourié-Gonnardc0edc962018-10-16 10:38:19 +020072 * acceptable for a public function but is OK here as we control call sites.
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020073 */
74static int ecdh_gen_public_restartable( mbedtls_ecp_group *grp,
75 mbedtls_mpi *d, mbedtls_ecp_point *Q,
76 int (*f_rng)(void *, unsigned char *, size_t),
77 void *p_rng,
78 mbedtls_ecp_restart_ctx *rs_ctx )
79{
80 int ret;
81
82 /* If multiplication is in progress, we already generated a privkey */
83#if defined(MBEDTLS_ECP_RESTARTABLE)
84 if( rs_ctx == NULL || rs_ctx->rsm == NULL )
85#endif
86 MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, d, f_rng, p_rng ) );
87
88 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, Q, d, &grp->G,
89 f_rng, p_rng, rs_ctx ) );
90
91cleanup:
92 return( ret );
93}
94
95/*
96 * Generate public key
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010097 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010099 int (*f_rng)(void *, unsigned char *, size_t),
100 void *p_rng )
101{
Hanno Becker91796d72018-12-17 18:10:51 +0000102 ECDH_VALIDATE_RET( grp != NULL );
103 ECDH_VALIDATE_RET( d != NULL );
104 ECDH_VALIDATE_RET( Q != NULL );
105 ECDH_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200106 return( ecdh_gen_public_restartable( grp, d, Q, f_rng, p_rng, NULL ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100107}
Ron Eldor936d2842018-11-01 13:05:52 +0200108#endif /* !MBEDTLS_ECDH_GEN_PUBLIC_ALT */
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100109
Ron Eldora84c1cb2017-10-10 19:04:27 +0300110#if !defined(MBEDTLS_ECDH_COMPUTE_SHARED_ALT)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100111/*
112 * Compute shared secret (SEC1 3.3.1)
113 */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200114static int ecdh_compute_shared_restartable( mbedtls_ecp_group *grp,
115 mbedtls_mpi *z,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200117 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200118 void *p_rng,
119 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100120{
121 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 mbedtls_ecp_point P;
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 mbedtls_ecp_point_init( &P );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100125
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200126 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, &P, d, Q,
127 f_rng, p_rng, rs_ctx ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 if( mbedtls_ecp_is_zero( &P ) )
Paul Bakkerb548d772013-07-26 14:21:34 +0200130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Paul Bakkerb548d772013-07-26 14:21:34 +0200132 goto cleanup;
133 }
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( z, &P.X ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100136
137cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 mbedtls_ecp_point_free( &P );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100139
140 return( ret );
141}
142
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100143/*
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200144 * Compute shared secret (SEC1 3.3.1)
145 */
146int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
147 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
148 int (*f_rng)(void *, unsigned char *, size_t),
149 void *p_rng )
150{
Hanno Becker91796d72018-12-17 18:10:51 +0000151 ECDH_VALIDATE_RET( grp != NULL );
152 ECDH_VALIDATE_RET( Q != NULL );
153 ECDH_VALIDATE_RET( d != NULL );
154 ECDH_VALIDATE_RET( z != NULL );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200155 return( ecdh_compute_shared_restartable( grp, z, Q, d,
156 f_rng, p_rng, NULL ) );
157}
Ron Eldor936d2842018-11-01 13:05:52 +0200158#endif /* !MBEDTLS_ECDH_COMPUTE_SHARED_ALT */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200159
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100160static void ecdh_init_internal( mbedtls_ecdh_context_mbed *ctx )
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100161{
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200162 mbedtls_ecp_group_init( &ctx->grp );
163 mbedtls_mpi_init( &ctx->d );
164 mbedtls_ecp_point_init( &ctx->Q );
165 mbedtls_ecp_point_init( &ctx->Qp );
166 mbedtls_mpi_init( &ctx->z );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200167
168#if defined(MBEDTLS_ECP_RESTARTABLE)
169 mbedtls_ecp_restart_init( &ctx->rs );
170#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100171}
172
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100173/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100174 * Initialize context
Janos Follathf61e4862018-10-30 11:53:25 +0000175 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100176void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx )
177{
Hanno Becker91796d72018-12-17 18:10:51 +0000178 ECDH_VALIDATE( ctx != NULL );
179
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100180#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
181 ecdh_init_internal( ctx );
182 mbedtls_ecp_point_init( &ctx->Vi );
183 mbedtls_ecp_point_init( &ctx->Vf );
184 mbedtls_mpi_init( &ctx->_d );
185#else
186 memset( ctx, 0, sizeof( mbedtls_ecdh_context ) );
187
188 ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
189#endif
190 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
191#if defined(MBEDTLS_ECP_RESTARTABLE)
192 ctx->restart_enabled = 0;
193#endif
194}
195
196static int ecdh_setup_internal( mbedtls_ecdh_context_mbed *ctx,
197 mbedtls_ecp_group_id grp_id )
Janos Follathf61e4862018-10-30 11:53:25 +0000198{
199 int ret;
200
201 ret = mbedtls_ecp_group_load( &ctx->grp, grp_id );
202 if( ret != 0 )
203 {
Janos Follathf61e4862018-10-30 11:53:25 +0000204 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
205 }
206
207 return( 0 );
208}
209
210/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100211 * Setup context
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100212 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100213int mbedtls_ecdh_setup( mbedtls_ecdh_context *ctx, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100214{
Hanno Becker91796d72018-12-17 18:10:51 +0000215 ECDH_VALIDATE_RET( ctx != NULL );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100216
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100217#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
218 return( ecdh_setup_internal( ctx, grp_id ) );
219#else
220 switch( grp_id )
221 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100222#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
Christoph M. Wintersteigerea243942019-01-07 14:12:25 +0000223 case MBEDTLS_ECP_DP_CURVE25519:
224 ctx->point_format = MBEDTLS_ECP_PF_COMPRESSED;
225 ctx->var = MBEDTLS_ECDH_VARIANT_EVEREST;
226 ctx->grp_id = grp_id;
227 return( mbedtls_everest_setup( &ctx->ctx.everest_ecdh, grp_id ) );
Christoph M. Wintersteiger78c9c462018-12-06 17:16:32 +0000228#endif
Christoph M. Wintersteigerea243942019-01-07 14:12:25 +0000229 default:
230 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
231 ctx->var = MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0;
232 ctx->grp_id = grp_id;
233 ecdh_init_internal( &ctx->ctx.mbed_ecdh );
234 return( ecdh_setup_internal( &ctx->ctx.mbed_ecdh, grp_id ) );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100235 }
236#endif
237}
238
239static void ecdh_free_internal( mbedtls_ecdh_context_mbed *ctx )
240{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 mbedtls_ecp_group_free( &ctx->grp );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200242 mbedtls_mpi_free( &ctx->d );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_ecp_point_free( &ctx->Q );
244 mbedtls_ecp_point_free( &ctx->Qp );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200245 mbedtls_mpi_free( &ctx->z );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200246
247#if defined(MBEDTLS_ECP_RESTARTABLE)
248 mbedtls_ecp_restart_free( &ctx->rs );
249#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100250}
251
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200252#if defined(MBEDTLS_ECP_RESTARTABLE)
253/*
254 * Enable restartable operations for context
255 */
256void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx )
257{
Hanno Beckera7634e82018-12-18 18:45:00 +0000258 ECDH_VALIDATE( ctx != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100259
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200260 ctx->restart_enabled = 1;
261}
262#endif
263
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100264/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100265 * Free context
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100266 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100267void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx )
268{
269 if( ctx == NULL )
270 return;
271
272#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
273 mbedtls_ecp_point_free( &ctx->Vi );
274 mbedtls_ecp_point_free( &ctx->Vf );
275 mbedtls_mpi_free( &ctx->_d );
276 ecdh_free_internal( ctx );
277#else
278 switch( ctx->var )
279 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100280#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
281 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000282 mbedtls_everest_free( &ctx->ctx.everest_ecdh );
283 ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
284 ctx->grp_id = MBEDTLS_ECP_DP_NONE;
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100285 break;
286#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100287 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
288 ecdh_free_internal( &ctx->ctx.mbed_ecdh );
289 break;
290 default:
291 break;
292 }
293
294 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
295 ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
296 ctx->grp_id = MBEDTLS_ECP_DP_NONE;
297#endif
298}
299
300static int ecdh_make_params_internal( mbedtls_ecdh_context_mbed *ctx,
301 size_t *olen, int point_format,
302 unsigned char *buf, size_t blen,
303 int (*f_rng)(void *,
304 unsigned char *,
305 size_t),
306 void *p_rng,
307 int restart_enabled )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100308{
309 int ret;
310 size_t grp_len, pt_len;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200311#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200312 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200313#endif
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100314
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100315 if( ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100317
Ron Eldor19779c42018-11-05 16:58:13 +0200318#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100319 if( restart_enabled )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200320 rs_ctx = &ctx->rs;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100321#else
322 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200323#endif
324
Ron Eldor8493f802018-11-01 11:32:15 +0200325
Ron Eldor2981d8f2018-11-05 18:07:10 +0200326#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200327 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200328 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100329 return( ret );
Ron Eldor2981d8f2018-11-05 18:07:10 +0200330#else
331 if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q,
332 f_rng, p_rng ) ) != 0 )
333 return( ret );
334#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100335
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100336 if( ( ret = mbedtls_ecp_tls_write_group( &ctx->grp, &grp_len, buf,
337 blen ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100338 return( ret );
339
340 buf += grp_len;
341 blen -= grp_len;
342
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100343 if( ( ret = mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, point_format,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200344 &pt_len, buf, blen ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100345 return( ret );
346
347 *olen = grp_len + pt_len;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200348 return( 0 );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100349}
350
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100351/*
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100352 * Setup and write the ServerKeyExchange parameters (RFC 4492)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100353 * struct {
354 * ECParameters curve_params;
355 * ECPoint public;
356 * } ServerECDHParams;
357 */
358int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
359 unsigned char *buf, size_t blen,
360 int (*f_rng)(void *, unsigned char *, size_t),
361 void *p_rng )
362{
363 int restart_enabled = 0;
Hanno Becker91796d72018-12-17 18:10:51 +0000364 ECDH_VALIDATE_RET( ctx != NULL );
365 ECDH_VALIDATE_RET( olen != NULL );
366 ECDH_VALIDATE_RET( buf != NULL );
367 ECDH_VALIDATE_RET( f_rng != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100368
369#if defined(MBEDTLS_ECP_RESTARTABLE)
370 restart_enabled = ctx->restart_enabled;
371#else
372 (void) restart_enabled;
373#endif
374
375#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
376 return( ecdh_make_params_internal( ctx, olen, ctx->point_format, buf, blen,
377 f_rng, p_rng, restart_enabled ) );
378#else
379 switch( ctx->var )
380 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100381#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
382 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000383 return( mbedtls_everest_make_params( &ctx->ctx.everest_ecdh, olen,
384 buf, blen, f_rng, p_rng ) );
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100385#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100386 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
387 return( ecdh_make_params_internal( &ctx->ctx.mbed_ecdh, olen,
388 ctx->point_format, buf, blen,
389 f_rng, p_rng,
390 restart_enabled ) );
391 default:
392 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
393 }
394#endif
395}
396
397static int ecdh_read_params_internal( mbedtls_ecdh_context_mbed *ctx,
398 const unsigned char **buf,
399 const unsigned char *end )
400{
401 return( mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf,
402 end - *buf ) );
403}
404
405/*
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100406 * Read the ServerKeyExhange parameters (RFC 4492)
407 * struct {
408 * ECParameters curve_params;
409 * ECPoint public;
410 * } ServerECDHParams;
411 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100413 const unsigned char **buf,
414 const unsigned char *end )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100415{
416 int ret;
Janos Follathf61e4862018-10-30 11:53:25 +0000417 mbedtls_ecp_group_id grp_id;
Hanno Becker91796d72018-12-17 18:10:51 +0000418 ECDH_VALIDATE_RET( ctx != NULL );
419 ECDH_VALIDATE_RET( buf != NULL );
420 ECDH_VALIDATE_RET( *buf != NULL );
421 ECDH_VALIDATE_RET( end != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100422
Janos Follathf61e4862018-10-30 11:53:25 +0000423 if( ( ret = mbedtls_ecp_tls_read_group_id( &grp_id, buf, end - *buf ) )
424 != 0 )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100425 return( ret );
426
Janos Follathf61e4862018-10-30 11:53:25 +0000427 if( ( ret = mbedtls_ecdh_setup( ctx, grp_id ) ) != 0 )
428 return( ret );
429
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100430#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
431 return( ecdh_read_params_internal( ctx, buf, end ) );
432#else
433 switch( ctx->var )
434 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100435#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
436 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000437 return( mbedtls_everest_read_params( &ctx->ctx.everest_ecdh,
438 buf, end) );
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100439#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100440 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
441 return( ecdh_read_params_internal( &ctx->ctx.mbed_ecdh,
442 buf, end ) );
443 default:
444 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
445 }
446#endif
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100447}
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100448
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100449static int ecdh_get_params_internal( mbedtls_ecdh_context_mbed *ctx,
450 const mbedtls_ecp_keypair *key,
451 mbedtls_ecdh_side side )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100452{
453 int ret;
454
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100455 /* If it's not our key, just import the public part as Qp */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 if( side == MBEDTLS_ECDH_THEIRS )
457 return( mbedtls_ecp_copy( &ctx->Qp, &key->Q ) );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100458
459 /* Our key: import public (as Q) and private parts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 if( side != MBEDTLS_ECDH_OURS )
461 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 if( ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 ||
464 ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100465 return( ret );
466
467 return( 0 );
468}
469
470/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100471 * Get parameters from a keypair
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100472 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100473int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx,
474 const mbedtls_ecp_keypair *key,
475 mbedtls_ecdh_side side )
476{
477 int ret;
Hanno Becker91796d72018-12-17 18:10:51 +0000478 ECDH_VALIDATE_RET( ctx != NULL );
479 ECDH_VALIDATE_RET( key != NULL );
480 ECDH_VALIDATE_RET( side == MBEDTLS_ECDH_OURS ||
481 side == MBEDTLS_ECDH_THEIRS );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100482
Gilles Peskine30816292019-02-22 12:31:25 +0100483 if( mbedtls_ecdh_grp_id( ctx ) == MBEDTLS_ECP_DP_NONE )
Gilles Peskine0b1b71d2018-11-07 22:10:59 +0100484 {
485 /* This is the first call to get_params(). Set up the context
486 * for use with the group. */
487 if( ( ret = mbedtls_ecdh_setup( ctx, key->grp.id ) ) != 0 )
488 return( ret );
489 }
490 else
491 {
492 /* This is not the first call to get_params(). Check that the
493 * current key's group is the same as the context's, which was set
494 * from the first key's group. */
Gilles Peskine30816292019-02-22 12:31:25 +0100495 if( mbedtls_ecdh_grp_id( ctx ) != key->grp.id )
Gilles Peskine0b1b71d2018-11-07 22:10:59 +0100496 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
497 }
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100498
499#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
500 return( ecdh_get_params_internal( ctx, key, side ) );
501#else
502 switch( ctx->var )
503 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100504#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
505 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000506 {
Christoph M. Wintersteiger2e724a12019-01-07 14:19:41 +0000507 mbedtls_everest_ecdh_side s = side == MBEDTLS_ECDH_OURS ?
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000508 MBEDTLS_EVEREST_ECDH_OURS :
509 MBEDTLS_EVEREST_ECDH_THEIRS;
510 return( mbedtls_everest_get_params( &ctx->ctx.everest_ecdh,
511 key, s) );
512 }
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100513#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100514 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
515 return( ecdh_get_params_internal( &ctx->ctx.mbed_ecdh,
516 key, side ) );
517 default:
518 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
519 }
520#endif
521}
522
523static int ecdh_make_public_internal( mbedtls_ecdh_context_mbed *ctx,
524 size_t *olen, int point_format,
525 unsigned char *buf, size_t blen,
526 int (*f_rng)(void *,
527 unsigned char *,
528 size_t),
529 void *p_rng,
530 int restart_enabled )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100531{
532 int ret;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200533#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200534 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200535#endif
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100536
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100537 if( ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100539
Ron Eldorb430d9f2018-11-05 17:18:29 +0200540#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100541 if( restart_enabled )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200542 rs_ctx = &ctx->rs;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100543#else
544 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200545#endif
546
Ron Eldor2981d8f2018-11-05 18:07:10 +0200547#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200548 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100549 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100550 return( ret );
Ron Eldor2981d8f2018-11-05 18:07:10 +0200551#else
552 if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q,
553 f_rng, p_rng ) ) != 0 )
554 return( ret );
555#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100556
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100557 return mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, point_format, olen,
558 buf, blen );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100559}
560
561/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100562 * Setup and export the client public value
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100563 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100564int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
565 unsigned char *buf, size_t blen,
566 int (*f_rng)(void *, unsigned char *, size_t),
567 void *p_rng )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100568{
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100569 int restart_enabled = 0;
Hanno Becker91796d72018-12-17 18:10:51 +0000570 ECDH_VALIDATE_RET( ctx != NULL );
571 ECDH_VALIDATE_RET( olen != NULL );
572 ECDH_VALIDATE_RET( buf != NULL );
Hanno Beckerc81cfec2018-12-18 23:32:42 +0000573 ECDH_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100574
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100575#if defined(MBEDTLS_ECP_RESTARTABLE)
576 restart_enabled = ctx->restart_enabled;
577#endif
578
579#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
580 return( ecdh_make_public_internal( ctx, olen, ctx->point_format, buf, blen,
581 f_rng, p_rng, restart_enabled ) );
582#else
583 switch( ctx->var )
584 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100585#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
586 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000587 return( mbedtls_everest_make_public( &ctx->ctx.everest_ecdh, olen,
588 buf, blen, f_rng, p_rng ) );
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100589#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100590 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
591 return( ecdh_make_public_internal( &ctx->ctx.mbed_ecdh, olen,
592 ctx->point_format, buf, blen,
593 f_rng, p_rng,
594 restart_enabled ) );
595 default:
596 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
597 }
598#endif
599}
600
601static int ecdh_read_public_internal( mbedtls_ecdh_context_mbed *ctx,
602 const unsigned char *buf, size_t blen )
603{
604 int ret;
605 const unsigned char *p = buf;
606
607 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p,
608 blen ) ) != 0 )
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100609 return( ret );
610
611 if( (size_t)( p - buf ) != blen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100613
614 return( 0 );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100615}
616
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100617/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100618 * Parse and import the client's public value
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100619 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100620int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
621 const unsigned char *buf, size_t blen )
622{
Hanno Becker91796d72018-12-17 18:10:51 +0000623 ECDH_VALIDATE_RET( ctx != NULL );
624 ECDH_VALIDATE_RET( buf != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100625
626#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
627 return( ecdh_read_public_internal( ctx, buf, blen ) );
628#else
629 switch( ctx->var )
630 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100631#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
632 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000633 return( mbedtls_everest_read_public( &ctx->ctx.everest_ecdh,
634 buf, blen ) );
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100635#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100636 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
637 return( ecdh_read_public_internal( &ctx->ctx.mbed_ecdh,
638 buf, blen ) );
639 default:
640 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
641 }
642#endif
643}
644
645static int ecdh_calc_secret_internal( mbedtls_ecdh_context_mbed *ctx,
646 size_t *olen, unsigned char *buf,
647 size_t blen,
648 int (*f_rng)(void *,
649 unsigned char *,
650 size_t),
651 void *p_rng,
652 int restart_enabled )
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100653{
654 int ret;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200655#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200656 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200657#endif
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100658
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200659 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100661
Ron Eldorb430d9f2018-11-05 17:18:29 +0200662#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100663 if( restart_enabled )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200664 rs_ctx = &ctx->rs;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100665#else
666 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200667#endif
668
Ron Eldor2981d8f2018-11-05 18:07:10 +0200669#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100670 if( ( ret = ecdh_compute_shared_restartable( &ctx->grp, &ctx->z, &ctx->Qp,
671 &ctx->d, f_rng, p_rng,
672 rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200673 {
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100674 return( ret );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200675 }
Ron Eldor2981d8f2018-11-05 18:07:10 +0200676#else
677 if( ( ret = mbedtls_ecdh_compute_shared( &ctx->grp, &ctx->z, &ctx->Qp,
678 &ctx->d, f_rng, p_rng ) ) != 0 )
679 {
680 return( ret );
681 }
682#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 if( mbedtls_mpi_size( &ctx->z ) > blen )
685 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Paul Bakker41c83d32013-03-20 14:39:14 +0100686
Manuel Pégourié-Gonnard0a56c2c2014-01-17 21:24:04 +0100687 *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 );
Janos Follathab0f71a2019-02-20 10:48:49 +0000688
Janos Follath52ff8e92019-02-26 13:56:04 +0000689 if( mbedtls_ecp_get_type( &ctx->grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
Janos Follathab0f71a2019-02-20 10:48:49 +0000690 return mbedtls_mpi_write_binary_le( &ctx->z, buf, *olen );
691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 return mbedtls_mpi_write_binary( &ctx->z, buf, *olen );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100693}
694
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100695/*
696 * Derive and export the shared secret
697 */
698int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
699 unsigned char *buf, size_t blen,
700 int (*f_rng)(void *, unsigned char *, size_t),
701 void *p_rng )
702{
703 int restart_enabled = 0;
Hanno Becker91796d72018-12-17 18:10:51 +0000704 ECDH_VALIDATE_RET( ctx != NULL );
705 ECDH_VALIDATE_RET( olen != NULL );
706 ECDH_VALIDATE_RET( buf != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100707
708#if defined(MBEDTLS_ECP_RESTARTABLE)
709 restart_enabled = ctx->restart_enabled;
710#endif
711
712#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
713 return( ecdh_calc_secret_internal( ctx, olen, buf, blen, f_rng, p_rng,
714 restart_enabled ) );
715#else
716 switch( ctx->var )
717 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100718#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
719 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000720 return( mbedtls_everest_calc_secret( &ctx->ctx.everest_ecdh, olen,
721 buf, blen, f_rng, p_rng ) );
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100722#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100723 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
724 return( ecdh_calc_secret_internal( &ctx->ctx.mbed_ecdh, olen, buf,
725 blen, f_rng, p_rng,
726 restart_enabled ) );
727 default:
728 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
729 }
730#endif
731}
732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733#endif /* MBEDTLS_ECDH_C */