blob: b9319470e56497322a2932450570c19dc06439ed [file] [log] [blame]
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +01001/*
2 * Elliptic curve Diffie-Hellman
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
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 */
19
20/*
21 * References:
22 *
23 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010024 * RFC 4492
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010025 */
26
Gilles Peskinedb09ef62020-06-03 01:43:33 +020027#include "common.h"
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010030
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/ecdh.h"
Hanno Becker91796d72018-12-17 18:10:51 +000032#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000033#include "mbedtls/error.h"
Jerry Yubdc71882021-09-14 19:30:36 +080034
35#include "ecdh_misc.h"
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010036
Rich Evans00ab4702015-02-06 13:43:58 +000037#include <string.h>
38
Hanno Becker91796d72018-12-17 18:10:51 +000039/* Parameter validation macros based on platform_util.h */
40#define ECDH_VALIDATE_RET( cond ) \
41 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_ECP_BAD_INPUT_DATA )
42#define ECDH_VALIDATE( cond ) \
43 MBEDTLS_INTERNAL_VALIDATE( cond )
44
Janos Follath5a3e1bf2018-08-13 15:54:22 +010045#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
46typedef mbedtls_ecdh_context mbedtls_ecdh_context_mbed;
47#endif
48
Gilles Peskine30816292019-02-22 12:31:25 +010049static mbedtls_ecp_group_id mbedtls_ecdh_grp_id(
50 const mbedtls_ecdh_context *ctx )
51{
52#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
53 return( ctx->grp.id );
54#else
55 return( ctx->grp_id );
56#endif
57}
58
Gilles Peskine20b3ef32019-02-11 18:41:27 +010059int mbedtls_ecdh_can_do( mbedtls_ecp_group_id gid )
60{
61 /* At this time, all groups support ECDH. */
62 (void) gid;
Christoph M. Wintersteigerc25df682019-04-16 12:54:56 +010063 return( 1 );
Gilles Peskine20b3ef32019-02-11 18:41:27 +010064}
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{
Janos Follath24eed8d2019-11-22 13:21:35 +000080 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020081
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{
Janos Follath24eed8d2019-11-22 13:21:35 +0000121 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
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
Christoph M. Wintersteigerd8c45d52019-02-20 17:16:53 +0000186 memset( ctx, 0, sizeof( mbedtls_ecdh_context ) );
187
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100188 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{
Janos Follath24eed8d2019-11-22 13:21:35 +0000199 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follathf61e4862018-10-30 11:53:25 +0000200
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. Wintersteiger3ff60bc2019-02-15 12:59:59 +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. Wintersteiger3ff60bc2019-02-15 12:59:59 +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 );
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100283 break;
284#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100285 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
286 ecdh_free_internal( &ctx->ctx.mbed_ecdh );
287 break;
288 default:
289 break;
290 }
291
292 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
293 ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
294 ctx->grp_id = MBEDTLS_ECP_DP_NONE;
295#endif
296}
297
298static int ecdh_make_params_internal( mbedtls_ecdh_context_mbed *ctx,
299 size_t *olen, int point_format,
300 unsigned char *buf, size_t blen,
301 int (*f_rng)(void *,
302 unsigned char *,
303 size_t),
304 void *p_rng,
305 int restart_enabled )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100306{
Janos Follath24eed8d2019-11-22 13:21:35 +0000307 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100308 size_t grp_len, pt_len;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200309#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200310 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200311#endif
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100312
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100313 if( ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100315
Ron Eldor19779c42018-11-05 16:58:13 +0200316#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100317 if( restart_enabled )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200318 rs_ctx = &ctx->rs;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100319#else
320 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200321#endif
322
Ron Eldor8493f802018-11-01 11:32:15 +0200323
Ron Eldor2981d8f2018-11-05 18:07:10 +0200324#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200325 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200326 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100327 return( ret );
Ron Eldor2981d8f2018-11-05 18:07:10 +0200328#else
329 if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q,
330 f_rng, p_rng ) ) != 0 )
331 return( ret );
332#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100333
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100334 if( ( ret = mbedtls_ecp_tls_write_group( &ctx->grp, &grp_len, buf,
335 blen ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100336 return( ret );
337
338 buf += grp_len;
339 blen -= grp_len;
340
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100341 if( ( ret = mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, point_format,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200342 &pt_len, buf, blen ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100343 return( ret );
344
345 *olen = grp_len + pt_len;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200346 return( 0 );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100347}
348
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100349/*
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100350 * Setup and write the ServerKeyExchange parameters (RFC 4492)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100351 * struct {
352 * ECParameters curve_params;
353 * ECPoint public;
354 * } ServerECDHParams;
355 */
356int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
357 unsigned char *buf, size_t blen,
358 int (*f_rng)(void *, unsigned char *, size_t),
359 void *p_rng )
360{
361 int restart_enabled = 0;
Hanno Becker91796d72018-12-17 18:10:51 +0000362 ECDH_VALIDATE_RET( ctx != NULL );
363 ECDH_VALIDATE_RET( olen != NULL );
364 ECDH_VALIDATE_RET( buf != NULL );
365 ECDH_VALIDATE_RET( f_rng != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100366
367#if defined(MBEDTLS_ECP_RESTARTABLE)
368 restart_enabled = ctx->restart_enabled;
369#else
370 (void) restart_enabled;
371#endif
372
373#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
374 return( ecdh_make_params_internal( ctx, olen, ctx->point_format, buf, blen,
375 f_rng, p_rng, restart_enabled ) );
376#else
377 switch( ctx->var )
378 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100379#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
380 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000381 return( mbedtls_everest_make_params( &ctx->ctx.everest_ecdh, olen,
382 buf, blen, f_rng, p_rng ) );
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100383#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100384 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
385 return( ecdh_make_params_internal( &ctx->ctx.mbed_ecdh, olen,
386 ctx->point_format, buf, blen,
387 f_rng, p_rng,
388 restart_enabled ) );
389 default:
390 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
391 }
392#endif
393}
394
395static int ecdh_read_params_internal( mbedtls_ecdh_context_mbed *ctx,
396 const unsigned char **buf,
397 const unsigned char *end )
398{
399 return( mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf,
400 end - *buf ) );
401}
402
403/*
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100404 * Read the ServerKeyExhange parameters (RFC 4492)
405 * struct {
406 * ECParameters curve_params;
407 * ECPoint public;
408 * } ServerECDHParams;
409 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100411 const unsigned char **buf,
412 const unsigned char *end )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100413{
Janos Follath24eed8d2019-11-22 13:21:35 +0000414 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follathf61e4862018-10-30 11:53:25 +0000415 mbedtls_ecp_group_id grp_id;
Hanno Becker91796d72018-12-17 18:10:51 +0000416 ECDH_VALIDATE_RET( ctx != NULL );
417 ECDH_VALIDATE_RET( buf != NULL );
418 ECDH_VALIDATE_RET( *buf != NULL );
419 ECDH_VALIDATE_RET( end != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100420
Janos Follathf61e4862018-10-30 11:53:25 +0000421 if( ( ret = mbedtls_ecp_tls_read_group_id( &grp_id, buf, end - *buf ) )
422 != 0 )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100423 return( ret );
424
Janos Follathf61e4862018-10-30 11:53:25 +0000425 if( ( ret = mbedtls_ecdh_setup( ctx, grp_id ) ) != 0 )
426 return( ret );
427
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100428#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
429 return( ecdh_read_params_internal( ctx, buf, end ) );
430#else
431 switch( ctx->var )
432 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100433#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
434 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000435 return( mbedtls_everest_read_params( &ctx->ctx.everest_ecdh,
436 buf, end) );
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100437#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100438 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
439 return( ecdh_read_params_internal( &ctx->ctx.mbed_ecdh,
440 buf, end ) );
441 default:
442 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
443 }
444#endif
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100445}
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100446
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100447static int ecdh_get_params_internal( mbedtls_ecdh_context_mbed *ctx,
448 const mbedtls_ecp_keypair *key,
449 mbedtls_ecdh_side side )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100450{
Janos Follath24eed8d2019-11-22 13:21:35 +0000451 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100452
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100453 /* If it's not our key, just import the public part as Qp */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 if( side == MBEDTLS_ECDH_THEIRS )
455 return( mbedtls_ecp_copy( &ctx->Qp, &key->Q ) );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100456
457 /* Our key: import public (as Q) and private parts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 if( side != MBEDTLS_ECDH_OURS )
459 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 if( ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 ||
462 ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100463 return( ret );
464
465 return( 0 );
466}
467
468/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100469 * Get parameters from a keypair
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100470 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100471int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx,
472 const mbedtls_ecp_keypair *key,
473 mbedtls_ecdh_side side )
474{
Janos Follath24eed8d2019-11-22 13:21:35 +0000475 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker91796d72018-12-17 18:10:51 +0000476 ECDH_VALIDATE_RET( ctx != NULL );
477 ECDH_VALIDATE_RET( key != NULL );
478 ECDH_VALIDATE_RET( side == MBEDTLS_ECDH_OURS ||
479 side == MBEDTLS_ECDH_THEIRS );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100480
Gilles Peskine30816292019-02-22 12:31:25 +0100481 if( mbedtls_ecdh_grp_id( ctx ) == MBEDTLS_ECP_DP_NONE )
Gilles Peskine0b1b71d2018-11-07 22:10:59 +0100482 {
483 /* This is the first call to get_params(). Set up the context
484 * for use with the group. */
485 if( ( ret = mbedtls_ecdh_setup( ctx, key->grp.id ) ) != 0 )
486 return( ret );
487 }
488 else
489 {
490 /* This is not the first call to get_params(). Check that the
491 * current key's group is the same as the context's, which was set
492 * from the first key's group. */
Gilles Peskine30816292019-02-22 12:31:25 +0100493 if( mbedtls_ecdh_grp_id( ctx ) != key->grp.id )
Gilles Peskine0b1b71d2018-11-07 22:10:59 +0100494 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
495 }
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100496
497#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
498 return( ecdh_get_params_internal( ctx, key, side ) );
499#else
500 switch( ctx->var )
501 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100502#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
503 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000504 {
Christoph M. Wintersteiger2e724a12019-01-07 14:19:41 +0000505 mbedtls_everest_ecdh_side s = side == MBEDTLS_ECDH_OURS ?
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000506 MBEDTLS_EVEREST_ECDH_OURS :
507 MBEDTLS_EVEREST_ECDH_THEIRS;
508 return( mbedtls_everest_get_params( &ctx->ctx.everest_ecdh,
509 key, s) );
510 }
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100511#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100512 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
513 return( ecdh_get_params_internal( &ctx->ctx.mbed_ecdh,
514 key, side ) );
515 default:
516 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
517 }
518#endif
519}
520
521static int ecdh_make_public_internal( mbedtls_ecdh_context_mbed *ctx,
522 size_t *olen, int point_format,
523 unsigned char *buf, size_t blen,
524 int (*f_rng)(void *,
525 unsigned char *,
526 size_t),
527 void *p_rng,
528 int restart_enabled )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100529{
Janos Follath24eed8d2019-11-22 13:21:35 +0000530 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200531#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200532 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200533#endif
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100534
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100535 if( ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100537
Ron Eldorb430d9f2018-11-05 17:18:29 +0200538#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100539 if( restart_enabled )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200540 rs_ctx = &ctx->rs;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100541#else
542 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200543#endif
544
Ron Eldor2981d8f2018-11-05 18:07:10 +0200545#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200546 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100547 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100548 return( ret );
Ron Eldor2981d8f2018-11-05 18:07:10 +0200549#else
550 if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q,
551 f_rng, p_rng ) ) != 0 )
552 return( ret );
553#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100554
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100555 return mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, point_format, olen,
556 buf, blen );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100557}
558
559/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100560 * Setup and export the client public value
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100561 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100562int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
563 unsigned char *buf, size_t blen,
564 int (*f_rng)(void *, unsigned char *, size_t),
565 void *p_rng )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100566{
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100567 int restart_enabled = 0;
Hanno Becker91796d72018-12-17 18:10:51 +0000568 ECDH_VALIDATE_RET( ctx != NULL );
569 ECDH_VALIDATE_RET( olen != NULL );
570 ECDH_VALIDATE_RET( buf != NULL );
Hanno Beckerc81cfec2018-12-18 23:32:42 +0000571 ECDH_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100572
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100573#if defined(MBEDTLS_ECP_RESTARTABLE)
574 restart_enabled = ctx->restart_enabled;
575#endif
576
577#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
578 return( ecdh_make_public_internal( ctx, olen, ctx->point_format, buf, blen,
579 f_rng, p_rng, restart_enabled ) );
580#else
581 switch( ctx->var )
582 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100583#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
584 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000585 return( mbedtls_everest_make_public( &ctx->ctx.everest_ecdh, olen,
586 buf, blen, f_rng, p_rng ) );
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100587#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100588 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
589 return( ecdh_make_public_internal( &ctx->ctx.mbed_ecdh, olen,
590 ctx->point_format, buf, blen,
591 f_rng, p_rng,
592 restart_enabled ) );
593 default:
594 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
595 }
596#endif
597}
598
599static int ecdh_read_public_internal( mbedtls_ecdh_context_mbed *ctx,
600 const unsigned char *buf, size_t blen )
601{
Janos Follath24eed8d2019-11-22 13:21:35 +0000602 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100603 const unsigned char *p = buf;
604
605 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p,
606 blen ) ) != 0 )
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100607 return( ret );
608
609 if( (size_t)( p - buf ) != blen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100611
612 return( 0 );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100613}
614
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100615/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100616 * Parse and import the client's public value
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100617 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100618int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
619 const unsigned char *buf, size_t blen )
620{
Hanno Becker91796d72018-12-17 18:10:51 +0000621 ECDH_VALIDATE_RET( ctx != NULL );
622 ECDH_VALIDATE_RET( buf != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100623
624#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
625 return( ecdh_read_public_internal( ctx, buf, blen ) );
626#else
627 switch( ctx->var )
628 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100629#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
630 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000631 return( mbedtls_everest_read_public( &ctx->ctx.everest_ecdh,
632 buf, blen ) );
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100633#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100634 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
635 return( ecdh_read_public_internal( &ctx->ctx.mbed_ecdh,
636 buf, blen ) );
637 default:
638 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
639 }
640#endif
641}
642
643static int ecdh_calc_secret_internal( mbedtls_ecdh_context_mbed *ctx,
644 size_t *olen, unsigned char *buf,
645 size_t blen,
646 int (*f_rng)(void *,
647 unsigned char *,
648 size_t),
649 void *p_rng,
650 int restart_enabled )
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100651{
Janos Follath24eed8d2019-11-22 13:21:35 +0000652 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200653#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200654 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200655#endif
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100656
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200657 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100659
Ron Eldorb430d9f2018-11-05 17:18:29 +0200660#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100661 if( restart_enabled )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200662 rs_ctx = &ctx->rs;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100663#else
664 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200665#endif
666
Ron Eldor2981d8f2018-11-05 18:07:10 +0200667#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100668 if( ( ret = ecdh_compute_shared_restartable( &ctx->grp, &ctx->z, &ctx->Qp,
669 &ctx->d, f_rng, p_rng,
670 rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200671 {
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100672 return( ret );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200673 }
Ron Eldor2981d8f2018-11-05 18:07:10 +0200674#else
675 if( ( ret = mbedtls_ecdh_compute_shared( &ctx->grp, &ctx->z, &ctx->Qp,
676 &ctx->d, f_rng, p_rng ) ) != 0 )
677 {
678 return( ret );
679 }
680#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 if( mbedtls_mpi_size( &ctx->z ) > blen )
683 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Paul Bakker41c83d32013-03-20 14:39:14 +0100684
Manuel Pégourié-Gonnard0a56c2c2014-01-17 21:24:04 +0100685 *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 );
Janos Follathab0f71a2019-02-20 10:48:49 +0000686
Janos Follath52ff8e92019-02-26 13:56:04 +0000687 if( mbedtls_ecp_get_type( &ctx->grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
Janos Follathab0f71a2019-02-20 10:48:49 +0000688 return mbedtls_mpi_write_binary_le( &ctx->z, buf, *olen );
689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 return mbedtls_mpi_write_binary( &ctx->z, buf, *olen );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100691}
692
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100693/*
694 * Derive and export the shared secret
695 */
696int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
697 unsigned char *buf, size_t blen,
698 int (*f_rng)(void *, unsigned char *, size_t),
699 void *p_rng )
700{
701 int restart_enabled = 0;
Hanno Becker91796d72018-12-17 18:10:51 +0000702 ECDH_VALIDATE_RET( ctx != NULL );
703 ECDH_VALIDATE_RET( olen != NULL );
704 ECDH_VALIDATE_RET( buf != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100705
706#if defined(MBEDTLS_ECP_RESTARTABLE)
707 restart_enabled = ctx->restart_enabled;
708#endif
709
710#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
711 return( ecdh_calc_secret_internal( ctx, olen, buf, blen, f_rng, p_rng,
712 restart_enabled ) );
713#else
714 switch( ctx->var )
715 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100716#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
717 case MBEDTLS_ECDH_VARIANT_EVEREST:
Christoph M. Wintersteiger4936beb2018-12-12 17:26:41 +0000718 return( mbedtls_everest_calc_secret( &ctx->ctx.everest_ecdh, olen,
719 buf, blen, f_rng, p_rng ) );
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100720#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100721 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
722 return( ecdh_calc_secret_internal( &ctx->ctx.mbed_ecdh, olen, buf,
723 blen, f_rng, p_rng,
724 restart_enabled ) );
725 default:
726 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
727 }
728#endif
729}
730
Jerry Yu56fc07f2021-09-01 17:48:49 +0800731#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
732
733static int ecdh_tls13_make_params_internal( mbedtls_ecdh_context_mbed *ctx,
Jerry Yubdc71882021-09-14 19:30:36 +0800734 size_t *olen, int point_format, unsigned char *buf, size_t blen,
735 int ( *f_rng )( void *, unsigned char *, size_t), void *p_rng )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800736{
737 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu56fc07f2021-09-01 17:48:49 +0800738
739 if( ctx->grp.pbits == 0 )
740 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
741
Jerry Yu56fc07f2021-09-01 17:48:49 +0800742 if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q,
743 f_rng, p_rng ) ) != 0 )
744 return( ret );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800745
746 ret = mbedtls_ecp_point_write_binary( &ctx->grp, &ctx->Q, point_format,
747 olen, buf, blen );
748 if( ret != 0 )
749 return( ret );
750
751 return( 0 );
752}
753
754int mbedtls_ecdh_tls13_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
Jerry Yubdc71882021-09-14 19:30:36 +0800755 unsigned char *buf, size_t blen,
756 int ( *f_rng )( void *, unsigned char *, size_t ),
757 void *p_rng )
Jerry Yu56fc07f2021-09-01 17:48:49 +0800758{
Jerry Yu56fc07f2021-09-01 17:48:49 +0800759 ECDH_VALIDATE_RET( ctx != NULL );
760 ECDH_VALIDATE_RET( olen != NULL );
761 ECDH_VALIDATE_RET( buf != NULL );
762 ECDH_VALIDATE_RET( f_rng != NULL );
763
Jerry Yubdc71882021-09-14 19:30:36 +0800764
Jerry Yu56fc07f2021-09-01 17:48:49 +0800765#if defined(MBEDTLS_ECP_RESTARTABLE)
Jerry Yubdc71882021-09-14 19:30:36 +0800766 if( ctx-> restart_enabled )
767 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800768#endif
769
770#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Jerry Yubdc71882021-09-14 19:30:36 +0800771 return( ecdh_tls13_make_params_internal( ctx, olen, ctx->point_format,
772 buf, blen, f_rng, p_rng ) );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800773#else
774 switch( ctx->var )
775 {
776#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
777 case MBEDTLS_ECDH_VARIANT_EVEREST:
Jerry Yubdc71882021-09-14 19:30:36 +0800778 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800779#endif
780 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
781 return( ecdh_tls13_make_params_internal( &ctx->ctx.mbed_ecdh, olen,
782 ctx->point_format, buf, blen,
Jerry Yubdc71882021-09-14 19:30:36 +0800783 f_rng, p_rng ) );
Jerry Yu56fc07f2021-09-01 17:48:49 +0800784 default:
785 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
786 }
787#endif
788}
789
790#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792#endif /* MBEDTLS_ECDH_C */