blob: be3637466fff61bfec03336b77984e0f294ef378 [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:
224 return( mbedtls_everest_setup( ctx, grp_id ) );
225#endif
226 break;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100227 default:
228 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
229 ctx->var = MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0;
230 ctx->grp_id = grp_id;
231 ecdh_init_internal( &ctx->ctx.mbed_ecdh );
232 return( ecdh_setup_internal( &ctx->ctx.mbed_ecdh, grp_id ) );
233 }
234#endif
235}
236
237static void ecdh_free_internal( mbedtls_ecdh_context_mbed *ctx )
238{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 mbedtls_ecp_group_free( &ctx->grp );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200240 mbedtls_mpi_free( &ctx->d );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 mbedtls_ecp_point_free( &ctx->Q );
242 mbedtls_ecp_point_free( &ctx->Qp );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200243 mbedtls_mpi_free( &ctx->z );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200244
245#if defined(MBEDTLS_ECP_RESTARTABLE)
246 mbedtls_ecp_restart_free( &ctx->rs );
247#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100248}
249
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200250#if defined(MBEDTLS_ECP_RESTARTABLE)
251/*
252 * Enable restartable operations for context
253 */
254void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx )
255{
Hanno Beckera7634e82018-12-18 18:45:00 +0000256 ECDH_VALIDATE( ctx != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100257
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200258 ctx->restart_enabled = 1;
259}
260#endif
261
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100262/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100263 * Free context
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100264 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100265void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx )
266{
267 if( ctx == NULL )
268 return;
269
270#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
271 mbedtls_ecp_point_free( &ctx->Vi );
272 mbedtls_ecp_point_free( &ctx->Vf );
273 mbedtls_mpi_free( &ctx->_d );
274 ecdh_free_internal( ctx );
275#else
276 switch( ctx->var )
277 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100278#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
279 case MBEDTLS_ECDH_VARIANT_EVEREST:
280 mbedtls_everest_free( ctx );
281 break;
282#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100283 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
284 ecdh_free_internal( &ctx->ctx.mbed_ecdh );
285 break;
286 default:
287 break;
288 }
289
290 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
291 ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
292 ctx->grp_id = MBEDTLS_ECP_DP_NONE;
293#endif
294}
295
296static int ecdh_make_params_internal( mbedtls_ecdh_context_mbed *ctx,
297 size_t *olen, int point_format,
298 unsigned char *buf, size_t blen,
299 int (*f_rng)(void *,
300 unsigned char *,
301 size_t),
302 void *p_rng,
303 int restart_enabled )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100304{
305 int ret;
306 size_t grp_len, pt_len;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200307#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200308 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200309#endif
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100310
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100311 if( ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100313
Ron Eldor19779c42018-11-05 16:58:13 +0200314#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100315 if( restart_enabled )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200316 rs_ctx = &ctx->rs;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100317#else
318 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200319#endif
320
Ron Eldor8493f802018-11-01 11:32:15 +0200321
Ron Eldor2981d8f2018-11-05 18:07:10 +0200322#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200323 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200324 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100325 return( ret );
Ron Eldor2981d8f2018-11-05 18:07:10 +0200326#else
327 if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q,
328 f_rng, p_rng ) ) != 0 )
329 return( ret );
330#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100331
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100332 if( ( ret = mbedtls_ecp_tls_write_group( &ctx->grp, &grp_len, buf,
333 blen ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100334 return( ret );
335
336 buf += grp_len;
337 blen -= grp_len;
338
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100339 if( ( ret = mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, point_format,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200340 &pt_len, buf, blen ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100341 return( ret );
342
343 *olen = grp_len + pt_len;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200344 return( 0 );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100345}
346
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100347/*
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100348 * Setup and write the ServerKeyExchange parameters (RFC 4492)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100349 * struct {
350 * ECParameters curve_params;
351 * ECPoint public;
352 * } ServerECDHParams;
353 */
354int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
355 unsigned char *buf, size_t blen,
356 int (*f_rng)(void *, unsigned char *, size_t),
357 void *p_rng )
358{
359 int restart_enabled = 0;
Hanno Becker91796d72018-12-17 18:10:51 +0000360 ECDH_VALIDATE_RET( ctx != NULL );
361 ECDH_VALIDATE_RET( olen != NULL );
362 ECDH_VALIDATE_RET( buf != NULL );
363 ECDH_VALIDATE_RET( f_rng != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100364
365#if defined(MBEDTLS_ECP_RESTARTABLE)
366 restart_enabled = ctx->restart_enabled;
367#else
368 (void) restart_enabled;
369#endif
370
371#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
372 return( ecdh_make_params_internal( ctx, olen, ctx->point_format, buf, blen,
373 f_rng, p_rng, restart_enabled ) );
374#else
375 switch( ctx->var )
376 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100377#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
378 case MBEDTLS_ECDH_VARIANT_EVEREST:
379 return( mbedtls_everest_make_params( ctx, olen, buf, blen, f_rng, p_rng ) );
380#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100381 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
382 return( ecdh_make_params_internal( &ctx->ctx.mbed_ecdh, olen,
383 ctx->point_format, buf, blen,
384 f_rng, p_rng,
385 restart_enabled ) );
386 default:
387 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
388 }
389#endif
390}
391
392static int ecdh_read_params_internal( mbedtls_ecdh_context_mbed *ctx,
393 const unsigned char **buf,
394 const unsigned char *end )
395{
396 return( mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf,
397 end - *buf ) );
398}
399
400/*
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100401 * Read the ServerKeyExhange parameters (RFC 4492)
402 * struct {
403 * ECParameters curve_params;
404 * ECPoint public;
405 * } ServerECDHParams;
406 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100408 const unsigned char **buf,
409 const unsigned char *end )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100410{
411 int ret;
Janos Follathf61e4862018-10-30 11:53:25 +0000412 mbedtls_ecp_group_id grp_id;
Hanno Becker91796d72018-12-17 18:10:51 +0000413 ECDH_VALIDATE_RET( ctx != NULL );
414 ECDH_VALIDATE_RET( buf != NULL );
415 ECDH_VALIDATE_RET( *buf != NULL );
416 ECDH_VALIDATE_RET( end != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100417
Janos Follathf61e4862018-10-30 11:53:25 +0000418 if( ( ret = mbedtls_ecp_tls_read_group_id( &grp_id, buf, end - *buf ) )
419 != 0 )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100420 return( ret );
421
Janos Follathf61e4862018-10-30 11:53:25 +0000422 if( ( ret = mbedtls_ecdh_setup( ctx, grp_id ) ) != 0 )
423 return( ret );
424
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100425#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
426 return( ecdh_read_params_internal( ctx, buf, end ) );
427#else
428 switch( ctx->var )
429 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100430#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
431 case MBEDTLS_ECDH_VARIANT_EVEREST:
432 return( mbedtls_everest_read_params( ctx, buf, end) );
433#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100434 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
435 return( ecdh_read_params_internal( &ctx->ctx.mbed_ecdh,
436 buf, end ) );
437 default:
438 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
439 }
440#endif
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100441}
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100442
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100443static int ecdh_get_params_internal( mbedtls_ecdh_context_mbed *ctx,
444 const mbedtls_ecp_keypair *key,
445 mbedtls_ecdh_side side )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100446{
447 int ret;
448
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100449 /* If it's not our key, just import the public part as Qp */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 if( side == MBEDTLS_ECDH_THEIRS )
451 return( mbedtls_ecp_copy( &ctx->Qp, &key->Q ) );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100452
453 /* Our key: import public (as Q) and private parts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 if( side != MBEDTLS_ECDH_OURS )
455 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 if( ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 ||
458 ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100459 return( ret );
460
461 return( 0 );
462}
463
464/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100465 * Get parameters from a keypair
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100466 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100467int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx,
468 const mbedtls_ecp_keypair *key,
469 mbedtls_ecdh_side side )
470{
471 int ret;
Hanno Becker91796d72018-12-17 18:10:51 +0000472 ECDH_VALIDATE_RET( ctx != NULL );
473 ECDH_VALIDATE_RET( key != NULL );
474 ECDH_VALIDATE_RET( side == MBEDTLS_ECDH_OURS ||
475 side == MBEDTLS_ECDH_THEIRS );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100476
Gilles Peskine30816292019-02-22 12:31:25 +0100477 if( mbedtls_ecdh_grp_id( ctx ) == MBEDTLS_ECP_DP_NONE )
Gilles Peskine0b1b71d2018-11-07 22:10:59 +0100478 {
479 /* This is the first call to get_params(). Set up the context
480 * for use with the group. */
481 if( ( ret = mbedtls_ecdh_setup( ctx, key->grp.id ) ) != 0 )
482 return( ret );
483 }
484 else
485 {
486 /* This is not the first call to get_params(). Check that the
487 * current key's group is the same as the context's, which was set
488 * from the first key's group. */
Gilles Peskine30816292019-02-22 12:31:25 +0100489 if( mbedtls_ecdh_grp_id( ctx ) != key->grp.id )
Gilles Peskine0b1b71d2018-11-07 22:10:59 +0100490 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
491 }
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100492
493#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
494 return( ecdh_get_params_internal( ctx, key, side ) );
495#else
496 switch( ctx->var )
497 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100498#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
499 case MBEDTLS_ECDH_VARIANT_EVEREST:
500 return( mbedtls_everest_get_params( ctx, key, side ) );
501#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100502 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
503 return( ecdh_get_params_internal( &ctx->ctx.mbed_ecdh,
504 key, side ) );
505 default:
506 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
507 }
508#endif
509}
510
511static int ecdh_make_public_internal( mbedtls_ecdh_context_mbed *ctx,
512 size_t *olen, int point_format,
513 unsigned char *buf, size_t blen,
514 int (*f_rng)(void *,
515 unsigned char *,
516 size_t),
517 void *p_rng,
518 int restart_enabled )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100519{
520 int ret;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200521#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200522 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200523#endif
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100524
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100525 if( ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100527
Ron Eldorb430d9f2018-11-05 17:18:29 +0200528#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100529 if( restart_enabled )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200530 rs_ctx = &ctx->rs;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100531#else
532 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200533#endif
534
Ron Eldor2981d8f2018-11-05 18:07:10 +0200535#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200536 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100537 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100538 return( ret );
Ron Eldor2981d8f2018-11-05 18:07:10 +0200539#else
540 if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q,
541 f_rng, p_rng ) ) != 0 )
542 return( ret );
543#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100544
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100545 return mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, point_format, olen,
546 buf, blen );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100547}
548
549/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100550 * Setup and export the client public value
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100551 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100552int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
553 unsigned char *buf, size_t blen,
554 int (*f_rng)(void *, unsigned char *, size_t),
555 void *p_rng )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100556{
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100557 int restart_enabled = 0;
Hanno Becker91796d72018-12-17 18:10:51 +0000558 ECDH_VALIDATE_RET( ctx != NULL );
559 ECDH_VALIDATE_RET( olen != NULL );
560 ECDH_VALIDATE_RET( buf != NULL );
Hanno Beckerc81cfec2018-12-18 23:32:42 +0000561 ECDH_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100562
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100563#if defined(MBEDTLS_ECP_RESTARTABLE)
564 restart_enabled = ctx->restart_enabled;
565#endif
566
567#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
568 return( ecdh_make_public_internal( ctx, olen, ctx->point_format, buf, blen,
569 f_rng, p_rng, restart_enabled ) );
570#else
571 switch( ctx->var )
572 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100573#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
574 case MBEDTLS_ECDH_VARIANT_EVEREST:
575 return( mbedtls_everest_make_public( ctx, olen, buf, blen, f_rng, p_rng ) );
576#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100577 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
578 return( ecdh_make_public_internal( &ctx->ctx.mbed_ecdh, olen,
579 ctx->point_format, buf, blen,
580 f_rng, p_rng,
581 restart_enabled ) );
582 default:
583 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
584 }
585#endif
586}
587
588static int ecdh_read_public_internal( mbedtls_ecdh_context_mbed *ctx,
589 const unsigned char *buf, size_t blen )
590{
591 int ret;
592 const unsigned char *p = buf;
593
594 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p,
595 blen ) ) != 0 )
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100596 return( ret );
597
598 if( (size_t)( p - buf ) != blen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100600
601 return( 0 );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100602}
603
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100604/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100605 * Parse and import the client's public value
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100606 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100607int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
608 const unsigned char *buf, size_t blen )
609{
Hanno Becker91796d72018-12-17 18:10:51 +0000610 ECDH_VALIDATE_RET( ctx != NULL );
611 ECDH_VALIDATE_RET( buf != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100612
613#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
614 return( ecdh_read_public_internal( ctx, buf, blen ) );
615#else
616 switch( ctx->var )
617 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100618#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
619 case MBEDTLS_ECDH_VARIANT_EVEREST:
620 return( mbedtls_everest_read_public( ctx, buf, blen ) );
621#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100622 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
623 return( ecdh_read_public_internal( &ctx->ctx.mbed_ecdh,
624 buf, blen ) );
625 default:
626 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
627 }
628#endif
629}
630
631static int ecdh_calc_secret_internal( mbedtls_ecdh_context_mbed *ctx,
632 size_t *olen, unsigned char *buf,
633 size_t blen,
634 int (*f_rng)(void *,
635 unsigned char *,
636 size_t),
637 void *p_rng,
638 int restart_enabled )
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100639{
640 int ret;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200641#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200642 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200643#endif
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100644
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200645 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100647
Ron Eldorb430d9f2018-11-05 17:18:29 +0200648#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100649 if( restart_enabled )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200650 rs_ctx = &ctx->rs;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100651#else
652 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200653#endif
654
Ron Eldor2981d8f2018-11-05 18:07:10 +0200655#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100656 if( ( ret = ecdh_compute_shared_restartable( &ctx->grp, &ctx->z, &ctx->Qp,
657 &ctx->d, f_rng, p_rng,
658 rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200659 {
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100660 return( ret );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200661 }
Ron Eldor2981d8f2018-11-05 18:07:10 +0200662#else
663 if( ( ret = mbedtls_ecdh_compute_shared( &ctx->grp, &ctx->z, &ctx->Qp,
664 &ctx->d, f_rng, p_rng ) ) != 0 )
665 {
666 return( ret );
667 }
668#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 if( mbedtls_mpi_size( &ctx->z ) > blen )
671 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Paul Bakker41c83d32013-03-20 14:39:14 +0100672
Manuel Pégourié-Gonnard0a56c2c2014-01-17 21:24:04 +0100673 *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 );
Janos Follathab0f71a2019-02-20 10:48:49 +0000674
Janos Follath52ff8e92019-02-26 13:56:04 +0000675 if( mbedtls_ecp_get_type( &ctx->grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
Janos Follathab0f71a2019-02-20 10:48:49 +0000676 return mbedtls_mpi_write_binary_le( &ctx->z, buf, *olen );
677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 return mbedtls_mpi_write_binary( &ctx->z, buf, *olen );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100679}
680
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100681/*
682 * Derive and export the shared secret
683 */
684int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
685 unsigned char *buf, size_t blen,
686 int (*f_rng)(void *, unsigned char *, size_t),
687 void *p_rng )
688{
689 int restart_enabled = 0;
Hanno Becker91796d72018-12-17 18:10:51 +0000690 ECDH_VALIDATE_RET( ctx != NULL );
691 ECDH_VALIDATE_RET( olen != NULL );
692 ECDH_VALIDATE_RET( buf != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100693
694#if defined(MBEDTLS_ECP_RESTARTABLE)
695 restart_enabled = ctx->restart_enabled;
696#endif
697
698#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
699 return( ecdh_calc_secret_internal( ctx, olen, buf, blen, f_rng, p_rng,
700 restart_enabled ) );
701#else
702 switch( ctx->var )
703 {
Christoph M. Wintersteigerc9f737b2018-10-25 13:03:05 +0100704#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
705 case MBEDTLS_ECDH_VARIANT_EVEREST:
706 return( mbedtls_everest_calc_secret( ctx, olen, buf, blen, f_rng, p_rng ) );
707#endif
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100708 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
709 return( ecdh_calc_secret_internal( &ctx->ctx.mbed_ecdh, olen, buf,
710 blen, f_rng, p_rng,
711 restart_enabled ) );
712 default:
713 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
714 }
715#endif
716}
717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718#endif /* MBEDTLS_ECDH_C */