blob: 3cf53337123b2f9bac9cfc804ea504ea1ba2efd5 [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"
Janos Follath24eed8d2019-11-22 13:21:35 +000039#include "mbedtls/error.h"
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010040
Rich Evans00ab4702015-02-06 13:43:58 +000041#include <string.h>
42
Hanno Becker91796d72018-12-17 18:10:51 +000043/* Parameter validation macros based on platform_util.h */
44#define ECDH_VALIDATE_RET( cond ) \
45 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_ECP_BAD_INPUT_DATA )
46#define ECDH_VALIDATE( cond ) \
47 MBEDTLS_INTERNAL_VALIDATE( cond )
48
Janos Follath5a3e1bf2018-08-13 15:54:22 +010049#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
50typedef mbedtls_ecdh_context mbedtls_ecdh_context_mbed;
51#endif
52
Gilles Peskine30816292019-02-22 12:31:25 +010053static mbedtls_ecp_group_id mbedtls_ecdh_grp_id(
54 const mbedtls_ecdh_context *ctx )
55{
56#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
57 return( ctx->grp.id );
58#else
59 return( ctx->grp_id );
60#endif
61}
62
Gilles Peskine20b3ef32019-02-11 18:41:27 +010063int mbedtls_ecdh_can_do( mbedtls_ecp_group_id gid )
64{
65 /* At this time, all groups support ECDH. */
66 (void) gid;
Christoph M. Wintersteigerc25df682019-04-16 12:54:56 +010067 return( 1 );
Gilles Peskine20b3ef32019-02-11 18:41:27 +010068}
69
Ron Eldora84c1cb2017-10-10 19:04:27 +030070#if !defined(MBEDTLS_ECDH_GEN_PUBLIC_ALT)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010071/*
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020072 * Generate public key (restartable version)
Manuel Pégourié-Gonnardc0edc962018-10-16 10:38:19 +020073 *
74 * Note: this internal function relies on its caller preserving the value of
Manuel Pégourié-Gonnardca29fdf2018-10-22 09:56:53 +020075 * the output parameter 'd' across continuation calls. This would not be
Manuel Pégourié-Gonnardc0edc962018-10-16 10:38:19 +020076 * acceptable for a public function but is OK here as we control call sites.
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020077 */
78static int ecdh_gen_public_restartable( mbedtls_ecp_group *grp,
79 mbedtls_mpi *d, mbedtls_ecp_point *Q,
80 int (*f_rng)(void *, unsigned char *, size_t),
81 void *p_rng,
82 mbedtls_ecp_restart_ctx *rs_ctx )
83{
Janos Follath24eed8d2019-11-22 13:21:35 +000084 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020085
86 /* If multiplication is in progress, we already generated a privkey */
87#if defined(MBEDTLS_ECP_RESTARTABLE)
88 if( rs_ctx == NULL || rs_ctx->rsm == NULL )
89#endif
90 MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, d, f_rng, p_rng ) );
91
92 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, Q, d, &grp->G,
93 f_rng, p_rng, rs_ctx ) );
94
95cleanup:
96 return( ret );
97}
98
99/*
100 * Generate public key
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100101 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100103 int (*f_rng)(void *, unsigned char *, size_t),
104 void *p_rng )
105{
Hanno Becker91796d72018-12-17 18:10:51 +0000106 ECDH_VALIDATE_RET( grp != NULL );
107 ECDH_VALIDATE_RET( d != NULL );
108 ECDH_VALIDATE_RET( Q != NULL );
109 ECDH_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200110 return( ecdh_gen_public_restartable( grp, d, Q, f_rng, p_rng, NULL ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100111}
Ron Eldor936d2842018-11-01 13:05:52 +0200112#endif /* !MBEDTLS_ECDH_GEN_PUBLIC_ALT */
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100113
Ron Eldora84c1cb2017-10-10 19:04:27 +0300114#if !defined(MBEDTLS_ECDH_COMPUTE_SHARED_ALT)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100115/*
116 * Compute shared secret (SEC1 3.3.1)
117 */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200118static int ecdh_compute_shared_restartable( mbedtls_ecp_group *grp,
119 mbedtls_mpi *z,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200121 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200122 void *p_rng,
123 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100124{
Janos Follath24eed8d2019-11-22 13:21:35 +0000125 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 mbedtls_ecp_point P;
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_ecp_point_init( &P );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100129
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200130 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, &P, d, Q,
131 f_rng, p_rng, rs_ctx ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 if( mbedtls_ecp_is_zero( &P ) )
Paul Bakkerb548d772013-07-26 14:21:34 +0200134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Paul Bakkerb548d772013-07-26 14:21:34 +0200136 goto cleanup;
137 }
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( z, &P.X ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100140
141cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 mbedtls_ecp_point_free( &P );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100143
144 return( ret );
145}
146
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100147/*
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200148 * Compute shared secret (SEC1 3.3.1)
149 */
150int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
151 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
152 int (*f_rng)(void *, unsigned char *, size_t),
153 void *p_rng )
154{
Hanno Becker91796d72018-12-17 18:10:51 +0000155 ECDH_VALIDATE_RET( grp != NULL );
156 ECDH_VALIDATE_RET( Q != NULL );
157 ECDH_VALIDATE_RET( d != NULL );
158 ECDH_VALIDATE_RET( z != NULL );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200159 return( ecdh_compute_shared_restartable( grp, z, Q, d,
160 f_rng, p_rng, NULL ) );
161}
Ron Eldor936d2842018-11-01 13:05:52 +0200162#endif /* !MBEDTLS_ECDH_COMPUTE_SHARED_ALT */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200163
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100164static void ecdh_init_internal( mbedtls_ecdh_context_mbed *ctx )
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100165{
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200166 mbedtls_ecp_group_init( &ctx->grp );
167 mbedtls_mpi_init( &ctx->d );
168 mbedtls_ecp_point_init( &ctx->Q );
169 mbedtls_ecp_point_init( &ctx->Qp );
170 mbedtls_mpi_init( &ctx->z );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200171
172#if defined(MBEDTLS_ECP_RESTARTABLE)
173 mbedtls_ecp_restart_init( &ctx->rs );
174#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100175}
176
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100177/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100178 * Initialize context
Janos Follathf61e4862018-10-30 11:53:25 +0000179 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100180void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx )
181{
Hanno Becker91796d72018-12-17 18:10:51 +0000182 ECDH_VALIDATE( ctx != NULL );
183
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100184#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
185 ecdh_init_internal( ctx );
186 mbedtls_ecp_point_init( &ctx->Vi );
187 mbedtls_ecp_point_init( &ctx->Vf );
188 mbedtls_mpi_init( &ctx->_d );
189#else
Christoph M. Wintersteigerd8c45d52019-02-20 17:16:53 +0000190 memset( ctx, 0, sizeof( mbedtls_ecdh_context ) );
191
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100192 ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
193#endif
194 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
195#if defined(MBEDTLS_ECP_RESTARTABLE)
196 ctx->restart_enabled = 0;
197#endif
198}
199
200static int ecdh_setup_internal( mbedtls_ecdh_context_mbed *ctx,
201 mbedtls_ecp_group_id grp_id )
Janos Follathf61e4862018-10-30 11:53:25 +0000202{
Janos Follath24eed8d2019-11-22 13:21:35 +0000203 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follathf61e4862018-10-30 11:53:25 +0000204
205 ret = mbedtls_ecp_group_load( &ctx->grp, grp_id );
206 if( ret != 0 )
207 {
Janos Follathf61e4862018-10-30 11:53:25 +0000208 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
209 }
210
211 return( 0 );
212}
213
214/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100215 * Setup context
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100216 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100217int mbedtls_ecdh_setup( mbedtls_ecdh_context *ctx, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100218{
Hanno Becker91796d72018-12-17 18:10:51 +0000219 ECDH_VALIDATE_RET( ctx != NULL );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100220
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100221#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
222 return( ecdh_setup_internal( ctx, grp_id ) );
223#else
224 switch( grp_id )
225 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100226#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
Christoph M. Wintersteiger3ff60bc2019-02-15 12:59:59 +0000227 case MBEDTLS_ECP_DP_CURVE25519:
228 ctx->point_format = MBEDTLS_ECP_PF_COMPRESSED;
229 ctx->var = MBEDTLS_ECDH_VARIANT_EVEREST;
230 ctx->grp_id = grp_id;
231 return( mbedtls_everest_setup( &ctx->ctx.everest_ecdh, grp_id ) );
Christoph M. Wintersteiger78c9c462018-12-06 17:16:32 +0000232#endif
Christoph M. Wintersteiger3ff60bc2019-02-15 12:59:59 +0000233 default:
234 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
235 ctx->var = MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0;
236 ctx->grp_id = grp_id;
237 ecdh_init_internal( &ctx->ctx.mbed_ecdh );
238 return( ecdh_setup_internal( &ctx->ctx.mbed_ecdh, grp_id ) );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100239 }
240#endif
241}
242
243static void ecdh_free_internal( mbedtls_ecdh_context_mbed *ctx )
244{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 mbedtls_ecp_group_free( &ctx->grp );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200246 mbedtls_mpi_free( &ctx->d );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 mbedtls_ecp_point_free( &ctx->Q );
248 mbedtls_ecp_point_free( &ctx->Qp );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200249 mbedtls_mpi_free( &ctx->z );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200250
251#if defined(MBEDTLS_ECP_RESTARTABLE)
252 mbedtls_ecp_restart_free( &ctx->rs );
253#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100254}
255
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200256#if defined(MBEDTLS_ECP_RESTARTABLE)
257/*
258 * Enable restartable operations for context
259 */
260void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx )
261{
Hanno Beckera7634e82018-12-18 18:45:00 +0000262 ECDH_VALIDATE( ctx != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100263
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200264 ctx->restart_enabled = 1;
265}
266#endif
267
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100268/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100269 * Free context
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100270 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100271void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx )
272{
273 if( ctx == NULL )
274 return;
275
276#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
277 mbedtls_ecp_point_free( &ctx->Vi );
278 mbedtls_ecp_point_free( &ctx->Vf );
279 mbedtls_mpi_free( &ctx->_d );
280 ecdh_free_internal( ctx );
281#else
282 switch( ctx->var )
283 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100284#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
285 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000286 mbedtls_everest_free( &ctx->ctx.everest_ecdh );
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{
Janos Follath24eed8d2019-11-22 13:21:35 +0000311 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100312 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{
Janos Follath24eed8d2019-11-22 13:21:35 +0000418 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
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{
Janos Follath24eed8d2019-11-22 13:21:35 +0000455 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100456
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{
Janos Follath24eed8d2019-11-22 13:21:35 +0000479 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
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{
Janos Follath24eed8d2019-11-22 13:21:35 +0000534 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
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{
Janos Follath24eed8d2019-11-22 13:21:35 +0000606 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100607 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{
Janos Follath24eed8d2019-11-22 13:21:35 +0000656 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
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 */