blob: 8c27e4e19645956402395a9561d4d55dcef9694f [file] [log] [blame]
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +01001/*
2 * Elliptic curve Diffie-Hellman
3 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010045 */
46
47/*
48 * References:
49 *
50 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010051 * RFC 4492
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010052 */
53
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020056#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020058#endif
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010061
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000062#include "mbedtls/ecdh.h"
Hanno Becker91796d72018-12-17 18:10:51 +000063#include "mbedtls/platform_util.h"
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010064
Rich Evans00ab4702015-02-06 13:43:58 +000065#include <string.h>
66
Hanno Becker91796d72018-12-17 18:10:51 +000067/* Parameter validation macros based on platform_util.h */
68#define ECDH_VALIDATE_RET( cond ) \
69 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_ECP_BAD_INPUT_DATA )
70#define ECDH_VALIDATE( cond ) \
71 MBEDTLS_INTERNAL_VALIDATE( cond )
72
Janos Follath5a3e1bf2018-08-13 15:54:22 +010073#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
74typedef mbedtls_ecdh_context mbedtls_ecdh_context_mbed;
75#endif
76
Gilles Peskine05fcf4f2019-02-22 12:31:25 +010077static mbedtls_ecp_group_id mbedtls_ecdh_grp_id(
78 const mbedtls_ecdh_context *ctx )
79{
80#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
81 return( ctx->grp.id );
82#else
83 return( ctx->grp_id );
84#endif
85}
86
Ron Eldora84c1cb2017-10-10 19:04:27 +030087#if !defined(MBEDTLS_ECDH_GEN_PUBLIC_ALT)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010088/*
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020089 * Generate public key (restartable version)
Manuel Pégourié-Gonnardc0edc962018-10-16 10:38:19 +020090 *
91 * Note: this internal function relies on its caller preserving the value of
Manuel Pégourié-Gonnardca29fdf2018-10-22 09:56:53 +020092 * the output parameter 'd' across continuation calls. This would not be
Manuel Pégourié-Gonnardc0edc962018-10-16 10:38:19 +020093 * acceptable for a public function but is OK here as we control call sites.
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020094 */
95static int ecdh_gen_public_restartable( mbedtls_ecp_group *grp,
96 mbedtls_mpi *d, mbedtls_ecp_point *Q,
97 int (*f_rng)(void *, unsigned char *, size_t),
98 void *p_rng,
99 mbedtls_ecp_restart_ctx *rs_ctx )
100{
101 int ret;
102
103 /* If multiplication is in progress, we already generated a privkey */
104#if defined(MBEDTLS_ECP_RESTARTABLE)
105 if( rs_ctx == NULL || rs_ctx->rsm == NULL )
106#endif
107 MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, d, f_rng, p_rng ) );
108
109 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, Q, d, &grp->G,
110 f_rng, p_rng, rs_ctx ) );
111
112cleanup:
113 return( ret );
114}
115
116/*
117 * Generate public key
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100120 int (*f_rng)(void *, unsigned char *, size_t),
121 void *p_rng )
122{
Hanno Becker91796d72018-12-17 18:10:51 +0000123 ECDH_VALIDATE_RET( grp != NULL );
124 ECDH_VALIDATE_RET( d != NULL );
125 ECDH_VALIDATE_RET( Q != NULL );
126 ECDH_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200127 return( ecdh_gen_public_restartable( grp, d, Q, f_rng, p_rng, NULL ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100128}
Ron Eldor936d2842018-11-01 13:05:52 +0200129#endif /* !MBEDTLS_ECDH_GEN_PUBLIC_ALT */
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100130
Ron Eldora84c1cb2017-10-10 19:04:27 +0300131#if !defined(MBEDTLS_ECDH_COMPUTE_SHARED_ALT)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100132/*
133 * Compute shared secret (SEC1 3.3.1)
134 */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200135static int ecdh_compute_shared_restartable( mbedtls_ecp_group *grp,
136 mbedtls_mpi *z,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200138 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200139 void *p_rng,
140 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100141{
142 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 mbedtls_ecp_point P;
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_ecp_point_init( &P );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100146
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200147 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, &P, d, Q,
148 f_rng, p_rng, rs_ctx ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 if( mbedtls_ecp_is_zero( &P ) )
Paul Bakkerb548d772013-07-26 14:21:34 +0200151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Paul Bakkerb548d772013-07-26 14:21:34 +0200153 goto cleanup;
154 }
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( z, &P.X ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100157
158cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 mbedtls_ecp_point_free( &P );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100160
161 return( ret );
162}
163
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100164/*
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200165 * Compute shared secret (SEC1 3.3.1)
166 */
167int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
168 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
169 int (*f_rng)(void *, unsigned char *, size_t),
170 void *p_rng )
171{
Hanno Becker91796d72018-12-17 18:10:51 +0000172 ECDH_VALIDATE_RET( grp != NULL );
173 ECDH_VALIDATE_RET( Q != NULL );
174 ECDH_VALIDATE_RET( d != NULL );
175 ECDH_VALIDATE_RET( z != NULL );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200176 return( ecdh_compute_shared_restartable( grp, z, Q, d,
177 f_rng, p_rng, NULL ) );
178}
Ron Eldor936d2842018-11-01 13:05:52 +0200179#endif /* !MBEDTLS_ECDH_COMPUTE_SHARED_ALT */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200180
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100181static void ecdh_init_internal( mbedtls_ecdh_context_mbed *ctx )
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100182{
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200183 mbedtls_ecp_group_init( &ctx->grp );
184 mbedtls_mpi_init( &ctx->d );
185 mbedtls_ecp_point_init( &ctx->Q );
186 mbedtls_ecp_point_init( &ctx->Qp );
187 mbedtls_mpi_init( &ctx->z );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200188
189#if defined(MBEDTLS_ECP_RESTARTABLE)
190 mbedtls_ecp_restart_init( &ctx->rs );
191#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100192}
193
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100194/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100195 * Initialize context
Janos Follathf61e4862018-10-30 11:53:25 +0000196 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100197void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx )
198{
Hanno Becker91796d72018-12-17 18:10:51 +0000199 ECDH_VALIDATE( ctx != NULL );
200
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100201#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
202 ecdh_init_internal( ctx );
203 mbedtls_ecp_point_init( &ctx->Vi );
204 mbedtls_ecp_point_init( &ctx->Vf );
205 mbedtls_mpi_init( &ctx->_d );
206#else
207 memset( ctx, 0, sizeof( mbedtls_ecdh_context ) );
208
209 ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
210#endif
211 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
212#if defined(MBEDTLS_ECP_RESTARTABLE)
213 ctx->restart_enabled = 0;
214#endif
215}
216
217static int ecdh_setup_internal( mbedtls_ecdh_context_mbed *ctx,
218 mbedtls_ecp_group_id grp_id )
Janos Follathf61e4862018-10-30 11:53:25 +0000219{
220 int ret;
221
222 ret = mbedtls_ecp_group_load( &ctx->grp, grp_id );
223 if( ret != 0 )
224 {
Janos Follathf61e4862018-10-30 11:53:25 +0000225 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
226 }
227
228 return( 0 );
229}
230
231/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100232 * Setup context
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100233 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100234int mbedtls_ecdh_setup( mbedtls_ecdh_context *ctx, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100235{
Hanno Becker91796d72018-12-17 18:10:51 +0000236 ECDH_VALIDATE_RET( ctx != NULL );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100237
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100238#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
239 return( ecdh_setup_internal( ctx, grp_id ) );
240#else
241 switch( grp_id )
242 {
243 default:
244 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
245 ctx->var = MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0;
246 ctx->grp_id = grp_id;
247 ecdh_init_internal( &ctx->ctx.mbed_ecdh );
248 return( ecdh_setup_internal( &ctx->ctx.mbed_ecdh, grp_id ) );
249 }
250#endif
251}
252
253static void ecdh_free_internal( mbedtls_ecdh_context_mbed *ctx )
254{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_ecp_group_free( &ctx->grp );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200256 mbedtls_mpi_free( &ctx->d );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 mbedtls_ecp_point_free( &ctx->Q );
258 mbedtls_ecp_point_free( &ctx->Qp );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200259 mbedtls_mpi_free( &ctx->z );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200260
261#if defined(MBEDTLS_ECP_RESTARTABLE)
262 mbedtls_ecp_restart_free( &ctx->rs );
263#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100264}
265
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200266#if defined(MBEDTLS_ECP_RESTARTABLE)
267/*
268 * Enable restartable operations for context
269 */
270void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx )
271{
Hanno Beckera7634e82018-12-18 18:45:00 +0000272 ECDH_VALIDATE( ctx != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100273
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200274 ctx->restart_enabled = 1;
275}
276#endif
277
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100278/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100279 * Free context
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100280 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100281void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx )
282{
283 if( ctx == NULL )
284 return;
285
286#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
287 mbedtls_ecp_point_free( &ctx->Vi );
288 mbedtls_ecp_point_free( &ctx->Vf );
289 mbedtls_mpi_free( &ctx->_d );
290 ecdh_free_internal( ctx );
291#else
292 switch( ctx->var )
293 {
294 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
295 ecdh_free_internal( &ctx->ctx.mbed_ecdh );
296 break;
297 default:
298 break;
299 }
300
301 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
302 ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
303 ctx->grp_id = MBEDTLS_ECP_DP_NONE;
304#endif
305}
306
307static int ecdh_make_params_internal( mbedtls_ecdh_context_mbed *ctx,
308 size_t *olen, int point_format,
309 unsigned char *buf, size_t blen,
310 int (*f_rng)(void *,
311 unsigned char *,
312 size_t),
313 void *p_rng,
314 int restart_enabled )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100315{
316 int ret;
317 size_t grp_len, pt_len;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200318#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200319 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200320#endif
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100321
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100322 if( ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100324
Ron Eldor19779c42018-11-05 16:58:13 +0200325#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100326 if( restart_enabled )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200327 rs_ctx = &ctx->rs;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100328#else
329 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200330#endif
331
Ron Eldor8493f802018-11-01 11:32:15 +0200332
Ron Eldor2981d8f2018-11-05 18:07:10 +0200333#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200334 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200335 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100336 return( ret );
Ron Eldor2981d8f2018-11-05 18:07:10 +0200337#else
338 if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q,
339 f_rng, p_rng ) ) != 0 )
340 return( ret );
341#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100342
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100343 if( ( ret = mbedtls_ecp_tls_write_group( &ctx->grp, &grp_len, buf,
344 blen ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100345 return( ret );
346
347 buf += grp_len;
348 blen -= grp_len;
349
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100350 if( ( ret = mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, point_format,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200351 &pt_len, buf, blen ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100352 return( ret );
353
354 *olen = grp_len + pt_len;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200355 return( 0 );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100356}
357
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100358/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100359 * Setup and write the ServerKeyExhange parameters (RFC 4492)
360 * struct {
361 * ECParameters curve_params;
362 * ECPoint public;
363 * } ServerECDHParams;
364 */
365int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
366 unsigned char *buf, size_t blen,
367 int (*f_rng)(void *, unsigned char *, size_t),
368 void *p_rng )
369{
370 int restart_enabled = 0;
Hanno Becker91796d72018-12-17 18:10:51 +0000371 ECDH_VALIDATE_RET( ctx != NULL );
372 ECDH_VALIDATE_RET( olen != NULL );
373 ECDH_VALIDATE_RET( buf != NULL );
374 ECDH_VALIDATE_RET( f_rng != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100375
376#if defined(MBEDTLS_ECP_RESTARTABLE)
377 restart_enabled = ctx->restart_enabled;
378#else
379 (void) restart_enabled;
380#endif
381
382#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
383 return( ecdh_make_params_internal( ctx, olen, ctx->point_format, buf, blen,
384 f_rng, p_rng, restart_enabled ) );
385#else
386 switch( ctx->var )
387 {
388 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
389 return( ecdh_make_params_internal( &ctx->ctx.mbed_ecdh, olen,
390 ctx->point_format, buf, blen,
391 f_rng, p_rng,
392 restart_enabled ) );
393 default:
394 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
395 }
396#endif
397}
398
399static int ecdh_read_params_internal( mbedtls_ecdh_context_mbed *ctx,
400 const unsigned char **buf,
401 const unsigned char *end )
402{
403 return( mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf,
404 end - *buf ) );
405}
406
407/*
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100408 * Read the ServerKeyExhange parameters (RFC 4492)
409 * struct {
410 * ECParameters curve_params;
411 * ECPoint public;
412 * } ServerECDHParams;
413 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100415 const unsigned char **buf,
416 const unsigned char *end )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100417{
418 int ret;
Janos Follathf61e4862018-10-30 11:53:25 +0000419 mbedtls_ecp_group_id grp_id;
Hanno Becker91796d72018-12-17 18:10:51 +0000420 ECDH_VALIDATE_RET( ctx != NULL );
421 ECDH_VALIDATE_RET( buf != NULL );
422 ECDH_VALIDATE_RET( *buf != NULL );
423 ECDH_VALIDATE_RET( end != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100424
Janos Follathf61e4862018-10-30 11:53:25 +0000425 if( ( ret = mbedtls_ecp_tls_read_group_id( &grp_id, buf, end - *buf ) )
426 != 0 )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100427 return( ret );
428
Janos Follathf61e4862018-10-30 11:53:25 +0000429 if( ( ret = mbedtls_ecdh_setup( ctx, grp_id ) ) != 0 )
430 return( ret );
431
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100432#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
433 return( ecdh_read_params_internal( ctx, buf, end ) );
434#else
435 switch( ctx->var )
436 {
437 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
438 return( ecdh_read_params_internal( &ctx->ctx.mbed_ecdh,
439 buf, end ) );
440 default:
441 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
442 }
443#endif
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100444}
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100445
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100446static int ecdh_get_params_internal( mbedtls_ecdh_context_mbed *ctx,
447 const mbedtls_ecp_keypair *key,
448 mbedtls_ecdh_side side )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100449{
450 int ret;
451
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100452 /* If it's not our key, just import the public part as Qp */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 if( side == MBEDTLS_ECDH_THEIRS )
454 return( mbedtls_ecp_copy( &ctx->Qp, &key->Q ) );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100455
456 /* Our key: import public (as Q) and private parts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 if( side != MBEDTLS_ECDH_OURS )
458 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 if( ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 ||
461 ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100462 return( ret );
463
464 return( 0 );
465}
466
467/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100468 * Get parameters from a keypair
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100469 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100470int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx,
471 const mbedtls_ecp_keypair *key,
472 mbedtls_ecdh_side side )
473{
474 int ret;
Hanno Becker91796d72018-12-17 18:10:51 +0000475 ECDH_VALIDATE_RET( ctx != NULL );
476 ECDH_VALIDATE_RET( key != NULL );
477 ECDH_VALIDATE_RET( side == MBEDTLS_ECDH_OURS ||
478 side == MBEDTLS_ECDH_THEIRS );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100479
Gilles Peskine05fcf4f2019-02-22 12:31:25 +0100480 if( mbedtls_ecdh_grp_id( ctx ) == MBEDTLS_ECP_DP_NONE )
Gilles Peskineb47045a2018-11-07 22:10:59 +0100481 {
482 /* This is the first call to get_params(). Set up the context
483 * for use with the group. */
484 if( ( ret = mbedtls_ecdh_setup( ctx, key->grp.id ) ) != 0 )
485 return( ret );
486 }
487 else
488 {
489 /* This is not the first call to get_params(). Check that the
490 * current key's group is the same as the context's, which was set
491 * from the first key's group. */
Gilles Peskine05fcf4f2019-02-22 12:31:25 +0100492 if( mbedtls_ecdh_grp_id( ctx ) != key->grp.id )
Gilles Peskineb47045a2018-11-07 22:10:59 +0100493 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
494 }
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100495
496#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
497 return( ecdh_get_params_internal( ctx, key, side ) );
498#else
499 switch( ctx->var )
500 {
501 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
502 return( ecdh_get_params_internal( &ctx->ctx.mbed_ecdh,
503 key, side ) );
504 default:
505 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
506 }
507#endif
508}
509
510static int ecdh_make_public_internal( mbedtls_ecdh_context_mbed *ctx,
511 size_t *olen, int point_format,
512 unsigned char *buf, size_t blen,
513 int (*f_rng)(void *,
514 unsigned char *,
515 size_t),
516 void *p_rng,
517 int restart_enabled )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100518{
519 int ret;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200520#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200521 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200522#endif
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100523
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100524 if( ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100526
Ron Eldorb430d9f2018-11-05 17:18:29 +0200527#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100528 if( restart_enabled )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200529 rs_ctx = &ctx->rs;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100530#else
531 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200532#endif
533
Ron Eldor2981d8f2018-11-05 18:07:10 +0200534#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200535 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100536 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100537 return( ret );
Ron Eldor2981d8f2018-11-05 18:07:10 +0200538#else
539 if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q,
540 f_rng, p_rng ) ) != 0 )
541 return( ret );
542#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100543
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100544 return mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, point_format, olen,
545 buf, blen );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100546}
547
548/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100549 * Setup and export the client public value
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100550 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100551int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
552 unsigned char *buf, size_t blen,
553 int (*f_rng)(void *, unsigned char *, size_t),
554 void *p_rng )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100555{
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100556 int restart_enabled = 0;
Hanno Becker91796d72018-12-17 18:10:51 +0000557 ECDH_VALIDATE_RET( ctx != NULL );
558 ECDH_VALIDATE_RET( olen != NULL );
559 ECDH_VALIDATE_RET( buf != NULL );
Hanno Beckerc81cfec2018-12-18 23:32:42 +0000560 ECDH_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100561
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100562#if defined(MBEDTLS_ECP_RESTARTABLE)
563 restart_enabled = ctx->restart_enabled;
564#endif
565
566#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
567 return( ecdh_make_public_internal( ctx, olen, ctx->point_format, buf, blen,
568 f_rng, p_rng, restart_enabled ) );
569#else
570 switch( ctx->var )
571 {
572 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
573 return( ecdh_make_public_internal( &ctx->ctx.mbed_ecdh, olen,
574 ctx->point_format, buf, blen,
575 f_rng, p_rng,
576 restart_enabled ) );
577 default:
578 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
579 }
580#endif
581}
582
583static int ecdh_read_public_internal( mbedtls_ecdh_context_mbed *ctx,
584 const unsigned char *buf, size_t blen )
585{
586 int ret;
587 const unsigned char *p = buf;
588
589 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p,
590 blen ) ) != 0 )
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100591 return( ret );
592
593 if( (size_t)( p - buf ) != blen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100595
596 return( 0 );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100597}
598
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100599/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100600 * Parse and import the client's public value
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100601 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100602int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
603 const unsigned char *buf, size_t blen )
604{
Hanno Becker91796d72018-12-17 18:10:51 +0000605 ECDH_VALIDATE_RET( ctx != NULL );
606 ECDH_VALIDATE_RET( buf != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100607
608#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
609 return( ecdh_read_public_internal( ctx, buf, blen ) );
610#else
611 switch( ctx->var )
612 {
613 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
614 return( ecdh_read_public_internal( &ctx->ctx.mbed_ecdh,
615 buf, blen ) );
616 default:
617 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
618 }
619#endif
620}
621
622static int ecdh_calc_secret_internal( mbedtls_ecdh_context_mbed *ctx,
623 size_t *olen, unsigned char *buf,
624 size_t blen,
625 int (*f_rng)(void *,
626 unsigned char *,
627 size_t),
628 void *p_rng,
629 int restart_enabled )
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100630{
631 int ret;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200632#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200633 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200634#endif
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100635
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200636 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100638
Ron Eldorb430d9f2018-11-05 17:18:29 +0200639#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100640 if( restart_enabled )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200641 rs_ctx = &ctx->rs;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100642#else
643 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200644#endif
645
Ron Eldor2981d8f2018-11-05 18:07:10 +0200646#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100647 if( ( ret = ecdh_compute_shared_restartable( &ctx->grp, &ctx->z, &ctx->Qp,
648 &ctx->d, f_rng, p_rng,
649 rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200650 {
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100651 return( ret );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200652 }
Ron Eldor2981d8f2018-11-05 18:07:10 +0200653#else
654 if( ( ret = mbedtls_ecdh_compute_shared( &ctx->grp, &ctx->z, &ctx->Qp,
655 &ctx->d, f_rng, p_rng ) ) != 0 )
656 {
657 return( ret );
658 }
659#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 if( mbedtls_mpi_size( &ctx->z ) > blen )
662 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Paul Bakker41c83d32013-03-20 14:39:14 +0100663
Manuel Pégourié-Gonnard0a56c2c2014-01-17 21:24:04 +0100664 *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 return mbedtls_mpi_write_binary( &ctx->z, buf, *olen );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100666}
667
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100668/*
669 * Derive and export the shared secret
670 */
671int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
672 unsigned char *buf, size_t blen,
673 int (*f_rng)(void *, unsigned char *, size_t),
674 void *p_rng )
675{
676 int restart_enabled = 0;
Hanno Becker91796d72018-12-17 18:10:51 +0000677 ECDH_VALIDATE_RET( ctx != NULL );
678 ECDH_VALIDATE_RET( olen != NULL );
679 ECDH_VALIDATE_RET( buf != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100680
681#if defined(MBEDTLS_ECP_RESTARTABLE)
682 restart_enabled = ctx->restart_enabled;
683#endif
684
685#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
686 return( ecdh_calc_secret_internal( ctx, olen, buf, blen, f_rng, p_rng,
687 restart_enabled ) );
688#else
689 switch( ctx->var )
690 {
691 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
692 return( ecdh_calc_secret_internal( &ctx->ctx.mbed_ecdh, olen, buf,
693 blen, f_rng, p_rng,
694 restart_enabled ) );
695 default:
696 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
697 }
698#endif
699}
700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701#endif /* MBEDTLS_ECDH_C */