blob: 58e67bfab253df8b9df6009aa0397eaf1c42f625 [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)
223 case MBEDTLS_ECP_DP_CURVE25519:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000224 {
225 ctx->point_format = MBEDTLS_ECP_PF_COMPRESSED;
226 ctx->var = MBEDTLS_ECDH_VARIANT_EVEREST;
227 ctx->grp_id = grp_id;
228 return( mbedtls_everest_setup( &ctx->ctx.everest_ecdh, grp_id ) );
229 }
Christoph M. Wintersteiger78c9c462018-12-06 17:16:32 +0000230#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100231 default:
232 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
233 ctx->var = MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0;
234 ctx->grp_id = grp_id;
235 ecdh_init_internal( &ctx->ctx.mbed_ecdh );
236 return( ecdh_setup_internal( &ctx->ctx.mbed_ecdh, grp_id ) );
237 }
238#endif
239}
240
241static void ecdh_free_internal( mbedtls_ecdh_context_mbed *ctx )
242{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_ecp_group_free( &ctx->grp );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200244 mbedtls_mpi_free( &ctx->d );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 mbedtls_ecp_point_free( &ctx->Q );
246 mbedtls_ecp_point_free( &ctx->Qp );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200247 mbedtls_mpi_free( &ctx->z );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200248
249#if defined(MBEDTLS_ECP_RESTARTABLE)
250 mbedtls_ecp_restart_free( &ctx->rs );
251#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100252}
253
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200254#if defined(MBEDTLS_ECP_RESTARTABLE)
255/*
256 * Enable restartable operations for context
257 */
258void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx )
259{
Hanno Beckera7634e82018-12-18 18:45:00 +0000260 ECDH_VALIDATE( ctx != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100261
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200262 ctx->restart_enabled = 1;
263}
264#endif
265
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100266/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100267 * Free context
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100268 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100269void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx )
270{
271 if( ctx == NULL )
272 return;
273
274#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
275 mbedtls_ecp_point_free( &ctx->Vi );
276 mbedtls_ecp_point_free( &ctx->Vf );
277 mbedtls_mpi_free( &ctx->_d );
278 ecdh_free_internal( ctx );
279#else
280 switch( ctx->var )
281 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100282#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
283 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000284 mbedtls_everest_free( &ctx->ctx.everest_ecdh );
285 ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
286 ctx->grp_id = MBEDTLS_ECP_DP_NONE;
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100287 break;
288#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100289 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
290 ecdh_free_internal( &ctx->ctx.mbed_ecdh );
291 break;
292 default:
293 break;
294 }
295
296 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
297 ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
298 ctx->grp_id = MBEDTLS_ECP_DP_NONE;
299#endif
300}
301
302static int ecdh_make_params_internal( mbedtls_ecdh_context_mbed *ctx,
303 size_t *olen, int point_format,
304 unsigned char *buf, size_t blen,
305 int (*f_rng)(void *,
306 unsigned char *,
307 size_t),
308 void *p_rng,
309 int restart_enabled )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100310{
311 int ret;
312 size_t grp_len, pt_len;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200313#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200314 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200315#endif
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100316
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100317 if( ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100319
Ron Eldor19779c42018-11-05 16:58:13 +0200320#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100321 if( restart_enabled )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200322 rs_ctx = &ctx->rs;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100323#else
324 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200325#endif
326
Ron Eldor8493f802018-11-01 11:32:15 +0200327
Ron Eldor2981d8f2018-11-05 18:07:10 +0200328#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200329 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200330 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100331 return( ret );
Ron Eldor2981d8f2018-11-05 18:07:10 +0200332#else
333 if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q,
334 f_rng, p_rng ) ) != 0 )
335 return( ret );
336#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100337
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100338 if( ( ret = mbedtls_ecp_tls_write_group( &ctx->grp, &grp_len, buf,
339 blen ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100340 return( ret );
341
342 buf += grp_len;
343 blen -= grp_len;
344
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100345 if( ( ret = mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, point_format,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200346 &pt_len, buf, blen ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100347 return( ret );
348
349 *olen = grp_len + pt_len;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200350 return( 0 );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100351}
352
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100353/*
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100354 * Setup and write the ServerKeyExchange parameters (RFC 4492)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100355 * struct {
356 * ECParameters curve_params;
357 * ECPoint public;
358 * } ServerECDHParams;
359 */
360int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
361 unsigned char *buf, size_t blen,
362 int (*f_rng)(void *, unsigned char *, size_t),
363 void *p_rng )
364{
365 int restart_enabled = 0;
Hanno Becker91796d72018-12-17 18:10:51 +0000366 ECDH_VALIDATE_RET( ctx != NULL );
367 ECDH_VALIDATE_RET( olen != NULL );
368 ECDH_VALIDATE_RET( buf != NULL );
369 ECDH_VALIDATE_RET( f_rng != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100370
371#if defined(MBEDTLS_ECP_RESTARTABLE)
372 restart_enabled = ctx->restart_enabled;
373#else
374 (void) restart_enabled;
375#endif
376
377#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
378 return( ecdh_make_params_internal( ctx, olen, ctx->point_format, buf, blen,
379 f_rng, p_rng, restart_enabled ) );
380#else
381 switch( ctx->var )
382 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100383#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
384 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000385 return( mbedtls_everest_make_params( &ctx->ctx.everest_ecdh, olen,
386 buf, blen, f_rng, p_rng ) );
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100387#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100388 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
389 return( ecdh_make_params_internal( &ctx->ctx.mbed_ecdh, olen,
390 ctx->point_format, buf, blen,
391 f_rng, p_rng,
392 restart_enabled ) );
393 default:
394 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
395 }
396#endif
397}
398
399static int ecdh_read_params_internal( mbedtls_ecdh_context_mbed *ctx,
400 const unsigned char **buf,
401 const unsigned char *end )
402{
403 return( mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf,
404 end - *buf ) );
405}
406
407/*
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100408 * Read the ServerKeyExhange parameters (RFC 4492)
409 * struct {
410 * ECParameters curve_params;
411 * ECPoint public;
412 * } ServerECDHParams;
413 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100415 const unsigned char **buf,
416 const unsigned char *end )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100417{
418 int ret;
Janos Follathf61e4862018-10-30 11:53:25 +0000419 mbedtls_ecp_group_id grp_id;
Hanno Becker91796d72018-12-17 18:10:51 +0000420 ECDH_VALIDATE_RET( ctx != NULL );
421 ECDH_VALIDATE_RET( buf != NULL );
422 ECDH_VALIDATE_RET( *buf != NULL );
423 ECDH_VALIDATE_RET( end != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100424
Janos Follathf61e4862018-10-30 11:53:25 +0000425 if( ( ret = mbedtls_ecp_tls_read_group_id( &grp_id, buf, end - *buf ) )
426 != 0 )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100427 return( ret );
428
Janos Follathf61e4862018-10-30 11:53:25 +0000429 if( ( ret = mbedtls_ecdh_setup( ctx, grp_id ) ) != 0 )
430 return( ret );
431
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100432#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
433 return( ecdh_read_params_internal( ctx, buf, end ) );
434#else
435 switch( ctx->var )
436 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100437#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
438 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000439 return( mbedtls_everest_read_params( &ctx->ctx.everest_ecdh,
440 buf, end) );
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100441#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100442 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
443 return( ecdh_read_params_internal( &ctx->ctx.mbed_ecdh,
444 buf, end ) );
445 default:
446 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
447 }
448#endif
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100449}
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100450
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100451static int ecdh_get_params_internal( mbedtls_ecdh_context_mbed *ctx,
452 const mbedtls_ecp_keypair *key,
453 mbedtls_ecdh_side side )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100454{
455 int ret;
456
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100457 /* If it's not our key, just import the public part as Qp */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 if( side == MBEDTLS_ECDH_THEIRS )
459 return( mbedtls_ecp_copy( &ctx->Qp, &key->Q ) );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100460
461 /* Our key: import public (as Q) and private parts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 if( side != MBEDTLS_ECDH_OURS )
463 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 if( ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 ||
466 ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100467 return( ret );
468
469 return( 0 );
470}
471
472/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100473 * Get parameters from a keypair
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100474 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100475int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx,
476 const mbedtls_ecp_keypair *key,
477 mbedtls_ecdh_side side )
478{
479 int ret;
Hanno Becker91796d72018-12-17 18:10:51 +0000480 ECDH_VALIDATE_RET( ctx != NULL );
481 ECDH_VALIDATE_RET( key != NULL );
482 ECDH_VALIDATE_RET( side == MBEDTLS_ECDH_OURS ||
483 side == MBEDTLS_ECDH_THEIRS );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100484
Gilles Peskine30816292019-02-22 12:31:25 +0100485 if( mbedtls_ecdh_grp_id( ctx ) == MBEDTLS_ECP_DP_NONE )
Gilles Peskine0b1b71d2018-11-07 22:10:59 +0100486 {
487 /* This is the first call to get_params(). Set up the context
488 * for use with the group. */
489 if( ( ret = mbedtls_ecdh_setup( ctx, key->grp.id ) ) != 0 )
490 return( ret );
491 }
492 else
493 {
494 /* This is not the first call to get_params(). Check that the
495 * current key's group is the same as the context's, which was set
496 * from the first key's group. */
Gilles Peskine30816292019-02-22 12:31:25 +0100497 if( mbedtls_ecdh_grp_id( ctx ) != key->grp.id )
Gilles Peskine0b1b71d2018-11-07 22:10:59 +0100498 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
499 }
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100500
501#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
502 return( ecdh_get_params_internal( ctx, key, side ) );
503#else
504 switch( ctx->var )
505 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100506#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
507 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000508 {
Christoph M. Wintersteiger2e724a12019-01-07 14:19:41 +0000509 mbedtls_everest_ecdh_side s = side == MBEDTLS_ECDH_OURS ?
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000510 MBEDTLS_EVEREST_ECDH_OURS :
511 MBEDTLS_EVEREST_ECDH_THEIRS;
512 return( mbedtls_everest_get_params( &ctx->ctx.everest_ecdh,
513 key, s) );
514 }
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100515#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100516 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
517 return( ecdh_get_params_internal( &ctx->ctx.mbed_ecdh,
518 key, side ) );
519 default:
520 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
521 }
522#endif
523}
524
525static int ecdh_make_public_internal( mbedtls_ecdh_context_mbed *ctx,
526 size_t *olen, int point_format,
527 unsigned char *buf, size_t blen,
528 int (*f_rng)(void *,
529 unsigned char *,
530 size_t),
531 void *p_rng,
532 int restart_enabled )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100533{
534 int ret;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200535#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200536 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200537#endif
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100538
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100539 if( ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100541
Ron Eldorb430d9f2018-11-05 17:18:29 +0200542#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100543 if( restart_enabled )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200544 rs_ctx = &ctx->rs;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100545#else
546 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200547#endif
548
Ron Eldor2981d8f2018-11-05 18:07:10 +0200549#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200550 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100551 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100552 return( ret );
Ron Eldor2981d8f2018-11-05 18:07:10 +0200553#else
554 if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q,
555 f_rng, p_rng ) ) != 0 )
556 return( ret );
557#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100558
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100559 return mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, point_format, olen,
560 buf, blen );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100561}
562
563/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100564 * Setup and export the client public value
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100565 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100566int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
567 unsigned char *buf, size_t blen,
568 int (*f_rng)(void *, unsigned char *, size_t),
569 void *p_rng )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100570{
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100571 int restart_enabled = 0;
Hanno Becker91796d72018-12-17 18:10:51 +0000572 ECDH_VALIDATE_RET( ctx != NULL );
573 ECDH_VALIDATE_RET( olen != NULL );
574 ECDH_VALIDATE_RET( buf != NULL );
Hanno Beckerc81cfec2018-12-18 23:32:42 +0000575 ECDH_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100576
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100577#if defined(MBEDTLS_ECP_RESTARTABLE)
578 restart_enabled = ctx->restart_enabled;
579#endif
580
581#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
582 return( ecdh_make_public_internal( ctx, olen, ctx->point_format, buf, blen,
583 f_rng, p_rng, restart_enabled ) );
584#else
585 switch( ctx->var )
586 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100587#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
588 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000589 return( mbedtls_everest_make_public( &ctx->ctx.everest_ecdh, olen,
590 buf, blen, f_rng, p_rng ) );
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100591#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100592 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
593 return( ecdh_make_public_internal( &ctx->ctx.mbed_ecdh, olen,
594 ctx->point_format, buf, blen,
595 f_rng, p_rng,
596 restart_enabled ) );
597 default:
598 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
599 }
600#endif
601}
602
603static int ecdh_read_public_internal( mbedtls_ecdh_context_mbed *ctx,
604 const unsigned char *buf, size_t blen )
605{
606 int ret;
607 const unsigned char *p = buf;
608
609 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p,
610 blen ) ) != 0 )
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100611 return( ret );
612
613 if( (size_t)( p - buf ) != blen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100615
616 return( 0 );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100617}
618
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100619/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100620 * Parse and import the client's public value
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100621 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100622int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
623 const unsigned char *buf, size_t blen )
624{
Hanno Becker91796d72018-12-17 18:10:51 +0000625 ECDH_VALIDATE_RET( ctx != NULL );
626 ECDH_VALIDATE_RET( buf != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100627
628#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
629 return( ecdh_read_public_internal( ctx, buf, blen ) );
630#else
631 switch( ctx->var )
632 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100633#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
634 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000635 return( mbedtls_everest_read_public( &ctx->ctx.everest_ecdh,
636 buf, blen ) );
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100637#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100638 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
639 return( ecdh_read_public_internal( &ctx->ctx.mbed_ecdh,
640 buf, blen ) );
641 default:
642 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
643 }
644#endif
645}
646
647static int ecdh_calc_secret_internal( mbedtls_ecdh_context_mbed *ctx,
648 size_t *olen, unsigned char *buf,
649 size_t blen,
650 int (*f_rng)(void *,
651 unsigned char *,
652 size_t),
653 void *p_rng,
654 int restart_enabled )
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100655{
656 int ret;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200657#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200658 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200659#endif
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100660
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200661 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100663
Ron Eldorb430d9f2018-11-05 17:18:29 +0200664#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100665 if( restart_enabled )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200666 rs_ctx = &ctx->rs;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100667#else
668 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200669#endif
670
Ron Eldor2981d8f2018-11-05 18:07:10 +0200671#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100672 if( ( ret = ecdh_compute_shared_restartable( &ctx->grp, &ctx->z, &ctx->Qp,
673 &ctx->d, f_rng, p_rng,
674 rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200675 {
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100676 return( ret );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200677 }
Ron Eldor2981d8f2018-11-05 18:07:10 +0200678#else
679 if( ( ret = mbedtls_ecdh_compute_shared( &ctx->grp, &ctx->z, &ctx->Qp,
680 &ctx->d, f_rng, p_rng ) ) != 0 )
681 {
682 return( ret );
683 }
684#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 if( mbedtls_mpi_size( &ctx->z ) > blen )
687 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Paul Bakker41c83d32013-03-20 14:39:14 +0100688
Manuel Pégourié-Gonnard0a56c2c2014-01-17 21:24:04 +0100689 *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 );
Janos Follathab0f71a2019-02-20 10:48:49 +0000690
Janos Follath52ff8e92019-02-26 13:56:04 +0000691 if( mbedtls_ecp_get_type( &ctx->grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
Janos Follathab0f71a2019-02-20 10:48:49 +0000692 return mbedtls_mpi_write_binary_le( &ctx->z, buf, *olen );
693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 return mbedtls_mpi_write_binary( &ctx->z, buf, *olen );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100695}
696
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100697/*
698 * Derive and export the shared secret
699 */
700int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
701 unsigned char *buf, size_t blen,
702 int (*f_rng)(void *, unsigned char *, size_t),
703 void *p_rng )
704{
705 int restart_enabled = 0;
Hanno Becker91796d72018-12-17 18:10:51 +0000706 ECDH_VALIDATE_RET( ctx != NULL );
707 ECDH_VALIDATE_RET( olen != NULL );
708 ECDH_VALIDATE_RET( buf != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100709
710#if defined(MBEDTLS_ECP_RESTARTABLE)
711 restart_enabled = ctx->restart_enabled;
712#endif
713
714#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
715 return( ecdh_calc_secret_internal( ctx, olen, buf, blen, f_rng, p_rng,
716 restart_enabled ) );
717#else
718 switch( ctx->var )
719 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100720#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
721 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000722 return( mbedtls_everest_calc_secret( &ctx->ctx.everest_ecdh, olen,
723 buf, blen, f_rng, p_rng ) );
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100724#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100725 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
726 return( ecdh_calc_secret_internal( &ctx->ctx.mbed_ecdh, olen, buf,
727 blen, f_rng, p_rng,
728 restart_enabled ) );
729 default:
730 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
731 }
732#endif
733}
734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735#endif /* MBEDTLS_ECDH_C */