blob: da95c60dad276aa88d336951d8203d784bfd5e13 [file] [log] [blame] [raw]
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>
Hanno Becker91796d72018-12-17 18:10:51 +000041
42/* 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 )
Rich Evans00ab4702015-02-06 13:43:58 +000047
Janos Follath5a3e1bf2018-08-13 15:54:22 +010048#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
49typedef mbedtls_ecdh_context mbedtls_ecdh_context_mbed;
50#endif
51
Ron Eldora84c1cb2017-10-10 19:04:27 +030052#if !defined(MBEDTLS_ECDH_GEN_PUBLIC_ALT)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010053/*
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020054 * Generate public key (restartable version)
Manuel Pégourié-Gonnardc0edc962018-10-16 10:38:19 +020055 *
56 * Note: this internal function relies on its caller preserving the value of
Manuel Pégourié-Gonnardca29fdf2018-10-22 09:56:53 +020057 * the output parameter 'd' across continuation calls. This would not be
Manuel Pégourié-Gonnardc0edc962018-10-16 10:38:19 +020058 * acceptable for a public function but is OK here as we control call sites.
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020059 */
60static int ecdh_gen_public_restartable( mbedtls_ecp_group *grp,
61 mbedtls_mpi *d, mbedtls_ecp_point *Q,
62 int (*f_rng)(void *, unsigned char *, size_t),
63 void *p_rng,
64 mbedtls_ecp_restart_ctx *rs_ctx )
65{
66 int ret;
67
68 /* If multiplication is in progress, we already generated a privkey */
69#if defined(MBEDTLS_ECP_RESTARTABLE)
70 if( rs_ctx == NULL || rs_ctx->rsm == NULL )
71#endif
72 MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, d, f_rng, p_rng ) );
73
74 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, Q, d, &grp->G,
75 f_rng, p_rng, rs_ctx ) );
76
77cleanup:
78 return( ret );
79}
80
81/*
82 * Generate public key
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010083 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010085 int (*f_rng)(void *, unsigned char *, size_t),
86 void *p_rng )
87{
Hanno Becker91796d72018-12-17 18:10:51 +000088 ECDH_VALIDATE_RET( grp != NULL );
89 ECDH_VALIDATE_RET( d != NULL );
90 ECDH_VALIDATE_RET( Q != NULL );
91 ECDH_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020092 return( ecdh_gen_public_restartable( grp, d, Q, f_rng, p_rng, NULL ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010093}
Ron Eldor936d2842018-11-01 13:05:52 +020094#endif /* !MBEDTLS_ECDH_GEN_PUBLIC_ALT */
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010095
Ron Eldora84c1cb2017-10-10 19:04:27 +030096#if !defined(MBEDTLS_ECDH_COMPUTE_SHARED_ALT)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010097/*
98 * Compute shared secret (SEC1 3.3.1)
99 */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200100static int ecdh_compute_shared_restartable( mbedtls_ecp_group *grp,
101 mbedtls_mpi *z,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200103 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200104 void *p_rng,
105 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100106{
107 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 mbedtls_ecp_point P;
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 mbedtls_ecp_point_init( &P );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100111
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200112 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, &P, d, Q,
113 f_rng, p_rng, rs_ctx ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 if( mbedtls_ecp_is_zero( &P ) )
Paul Bakkerb548d772013-07-26 14:21:34 +0200116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Paul Bakkerb548d772013-07-26 14:21:34 +0200118 goto cleanup;
119 }
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( z, &P.X ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100122
123cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 mbedtls_ecp_point_free( &P );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100125
126 return( ret );
127}
128
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100129/*
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200130 * Compute shared secret (SEC1 3.3.1)
131 */
132int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
133 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
134 int (*f_rng)(void *, unsigned char *, size_t),
135 void *p_rng )
136{
Hanno Becker91796d72018-12-17 18:10:51 +0000137 ECDH_VALIDATE_RET( grp != NULL );
138 ECDH_VALIDATE_RET( Q != NULL );
139 ECDH_VALIDATE_RET( d != NULL );
140 ECDH_VALIDATE_RET( z != NULL );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200141 return( ecdh_compute_shared_restartable( grp, z, Q, d,
142 f_rng, p_rng, NULL ) );
143}
Ron Eldor936d2842018-11-01 13:05:52 +0200144#endif /* !MBEDTLS_ECDH_COMPUTE_SHARED_ALT */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200145
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100146static void ecdh_init_internal( mbedtls_ecdh_context_mbed *ctx )
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100147{
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200148 mbedtls_ecp_group_init( &ctx->grp );
149 mbedtls_mpi_init( &ctx->d );
150 mbedtls_ecp_point_init( &ctx->Q );
151 mbedtls_ecp_point_init( &ctx->Qp );
152 mbedtls_mpi_init( &ctx->z );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200153
154#if defined(MBEDTLS_ECP_RESTARTABLE)
155 mbedtls_ecp_restart_init( &ctx->rs );
156#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100157}
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100158
159/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100160 * Initialize context
Janos Follathf61e4862018-10-30 11:53:25 +0000161 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100162void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx )
163{
Hanno Becker91796d72018-12-17 18:10:51 +0000164 ECDH_VALIDATE( ctx != NULL );
165
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100166#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
167 ecdh_init_internal( ctx );
168 mbedtls_ecp_point_init( &ctx->Vi );
169 mbedtls_ecp_point_init( &ctx->Vf );
170 mbedtls_mpi_init( &ctx->_d );
171#else
172 memset( ctx, 0, sizeof( mbedtls_ecdh_context ) );
173
174 ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
175#endif
176 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
177#if defined(MBEDTLS_ECP_RESTARTABLE)
178 ctx->restart_enabled = 0;
179#endif
180}
181
182static int ecdh_setup_internal( mbedtls_ecdh_context_mbed *ctx,
183 mbedtls_ecp_group_id grp_id )
Janos Follathf61e4862018-10-30 11:53:25 +0000184{
185 int ret;
186
187 ret = mbedtls_ecp_group_load( &ctx->grp, grp_id );
188 if( ret != 0 )
189 {
Janos Follathf61e4862018-10-30 11:53:25 +0000190 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
191 }
192
193 return( 0 );
194}
195
196/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100197 * Setup context
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100198 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100199int mbedtls_ecdh_setup( mbedtls_ecdh_context *ctx, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100200{
Hanno Becker91796d72018-12-17 18:10:51 +0000201 ECDH_VALIDATE_RET( ctx != NULL );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100202
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100203#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
204 return( ecdh_setup_internal( ctx, grp_id ) );
205#else
206 switch( grp_id )
207 {
208 default:
209 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
210 ctx->var = MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0;
211 ctx->grp_id = grp_id;
212 ecdh_init_internal( &ctx->ctx.mbed_ecdh );
213 return( ecdh_setup_internal( &ctx->ctx.mbed_ecdh, grp_id ) );
214 }
215#endif
216}
217
218static void ecdh_free_internal( mbedtls_ecdh_context_mbed *ctx )
219{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 mbedtls_ecp_group_free( &ctx->grp );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200221 mbedtls_mpi_free( &ctx->d );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 mbedtls_ecp_point_free( &ctx->Q );
223 mbedtls_ecp_point_free( &ctx->Qp );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200224 mbedtls_mpi_free( &ctx->z );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200225
226#if defined(MBEDTLS_ECP_RESTARTABLE)
227 mbedtls_ecp_restart_free( &ctx->rs );
228#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100229}
230
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200231#if defined(MBEDTLS_ECP_RESTARTABLE)
232/*
233 * Enable restartable operations for context
234 */
235void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx )
236{
Hanno Beckera7634e82018-12-18 18:45:00 +0000237 ECDH_VALIDATE( ctx != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100238
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200239 ctx->restart_enabled = 1;
240}
241#endif
242
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100243/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100244 * Free context
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100245 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100246void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx )
247{
248 if( ctx == NULL )
249 return;
250
251#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
252 mbedtls_ecp_point_free( &ctx->Vi );
253 mbedtls_ecp_point_free( &ctx->Vf );
254 mbedtls_mpi_free( &ctx->_d );
255 ecdh_free_internal( ctx );
256#else
257 switch( ctx->var )
258 {
259 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
260 ecdh_free_internal( &ctx->ctx.mbed_ecdh );
261 break;
262 default:
263 break;
264 }
265
266 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
267 ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
268 ctx->grp_id = MBEDTLS_ECP_DP_NONE;
269#endif
270}
271
272static int ecdh_make_params_internal( mbedtls_ecdh_context_mbed *ctx,
273 size_t *olen, int point_format,
274 unsigned char *buf, size_t blen,
275 int (*f_rng)(void *,
276 unsigned char *,
277 size_t),
278 void *p_rng,
279 int restart_enabled )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100280{
281 int ret;
282 size_t grp_len, pt_len;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200283#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200284 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200285#endif
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100286
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100287 if( ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100289
Ron Eldor19779c42018-11-05 16:58:13 +0200290#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100291 if( restart_enabled )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200292 rs_ctx = &ctx->rs;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100293#else
294 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200295#endif
296
Ron Eldor8493f802018-11-01 11:32:15 +0200297
Ron Eldor2981d8f2018-11-05 18:07:10 +0200298#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200299 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200300 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100301 return( ret );
Ron Eldor2981d8f2018-11-05 18:07:10 +0200302#else
303 if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q,
304 f_rng, p_rng ) ) != 0 )
305 return( ret );
306#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100307
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100308 if( ( ret = mbedtls_ecp_tls_write_group( &ctx->grp, &grp_len, buf,
309 blen ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100310 return( ret );
311
312 buf += grp_len;
313 blen -= grp_len;
314
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100315 if( ( ret = mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, point_format,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200316 &pt_len, buf, blen ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100317 return( ret );
318
319 *olen = grp_len + pt_len;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200320 return( 0 );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100321}
322
323/*
324 * Setup and write the ServerKeyExhange parameters (RFC 4492)
325 * struct {
326 * ECParameters curve_params;
327 * ECPoint public;
328 * } ServerECDHParams;
329 */
330int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
331 unsigned char *buf, size_t blen,
332 int (*f_rng)(void *, unsigned char *, size_t),
333 void *p_rng )
334{
335 int restart_enabled = 0;
Hanno Becker91796d72018-12-17 18:10:51 +0000336 ECDH_VALIDATE_RET( ctx != NULL );
337 ECDH_VALIDATE_RET( olen != NULL );
338 ECDH_VALIDATE_RET( buf != NULL );
339 ECDH_VALIDATE_RET( f_rng != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100340
341#if defined(MBEDTLS_ECP_RESTARTABLE)
342 restart_enabled = ctx->restart_enabled;
343#else
344 (void) restart_enabled;
345#endif
346
347#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
348 return( ecdh_make_params_internal( ctx, olen, ctx->point_format, buf, blen,
349 f_rng, p_rng, restart_enabled ) );
350#else
351 switch( ctx->var )
352 {
353 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
354 return( ecdh_make_params_internal( &ctx->ctx.mbed_ecdh, olen,
355 ctx->point_format, buf, blen,
356 f_rng, p_rng,
357 restart_enabled ) );
358 default:
359 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
360 }
361#endif
362}
363
364static int ecdh_read_params_internal( mbedtls_ecdh_context_mbed *ctx,
365 const unsigned char **buf,
366 const unsigned char *end )
367{
368 return( mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf,
369 end - *buf ) );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100370}
371
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100372/*
373 * Read the ServerKeyExhange parameters (RFC 4492)
374 * struct {
375 * ECParameters curve_params;
376 * ECPoint public;
377 * } ServerECDHParams;
378 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100380 const unsigned char **buf,
381 const unsigned char *end )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100382{
383 int ret;
Janos Follathf61e4862018-10-30 11:53:25 +0000384 mbedtls_ecp_group_id grp_id;
Hanno Becker91796d72018-12-17 18:10:51 +0000385 ECDH_VALIDATE_RET( ctx != NULL );
386 ECDH_VALIDATE_RET( buf != NULL );
387 ECDH_VALIDATE_RET( *buf != NULL );
388 ECDH_VALIDATE_RET( end != NULL );
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100389
Janos Follathf61e4862018-10-30 11:53:25 +0000390 if( ( ret = mbedtls_ecp_tls_read_group_id( &grp_id, buf, end - *buf ) )
391 != 0 )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100392 return( ret );
393
Janos Follathf61e4862018-10-30 11:53:25 +0000394 if( ( ret = mbedtls_ecdh_setup( ctx, grp_id ) ) != 0 )
395 return( ret );
396
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100397#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
398 return( ecdh_read_params_internal( ctx, buf, end ) );
399#else
400 switch( ctx->var )
401 {
402 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
403 return( ecdh_read_params_internal( &ctx->ctx.mbed_ecdh,
404 buf, end ) );
405 default:
406 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
407 }
408#endif
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100409}
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100410
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100411static int ecdh_get_params_internal( mbedtls_ecdh_context_mbed *ctx,
412 const mbedtls_ecp_keypair *key,
413 mbedtls_ecdh_side side )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100414{
415 int ret;
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100416
417 /* If it's not our key, just import the public part as Qp */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 if( side == MBEDTLS_ECDH_THEIRS )
419 return( mbedtls_ecp_copy( &ctx->Qp, &key->Q ) );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100420
421 /* Our key: import public (as Q) and private parts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 if( side != MBEDTLS_ECDH_OURS )
423 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 if( ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 ||
426 ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100427 return( ret );
428
429 return( 0 );
430}
431
432/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100433 * Get parameters from a keypair
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100434 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100435int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx,
436 const mbedtls_ecp_keypair *key,
437 mbedtls_ecdh_side side )
438{
439 int ret;
Hanno Becker91796d72018-12-17 18:10:51 +0000440 ECDH_VALIDATE_RET( ctx != NULL );
441 ECDH_VALIDATE_RET( key != NULL );
442 ECDH_VALIDATE_RET( side == MBEDTLS_ECDH_OURS ||
443 side == MBEDTLS_ECDH_THEIRS );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100444
445 if( ( ret = mbedtls_ecdh_setup( ctx, key->grp.id ) ) != 0 )
446 return( ret );
447
448#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
449 return( ecdh_get_params_internal( ctx, key, side ) );
450#else
451 switch( ctx->var )
452 {
453 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
454 return( ecdh_get_params_internal( &ctx->ctx.mbed_ecdh,
455 key, side ) );
456 default:
457 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
458 }
459#endif
460}
461
462static int ecdh_make_public_internal( mbedtls_ecdh_context_mbed *ctx,
463 size_t *olen, int point_format,
464 unsigned char *buf, size_t blen,
465 int (*f_rng)(void *,
466 unsigned char *,
467 size_t),
468 void *p_rng,
469 int restart_enabled )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100470{
471 int ret;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200472#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200473 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200474#endif
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100475
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100476 if( ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100478
Ron Eldorb430d9f2018-11-05 17:18:29 +0200479#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100480 if( restart_enabled )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200481 rs_ctx = &ctx->rs;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100482#else
483 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200484#endif
485
Ron Eldor2981d8f2018-11-05 18:07:10 +0200486#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200487 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100488 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100489 return( ret );
Ron Eldor2981d8f2018-11-05 18:07:10 +0200490#else
491 if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q,
492 f_rng, p_rng ) ) != 0 )
493 return( ret );
494#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100495
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100496 return mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, point_format, olen,
497 buf, blen );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100498}
499
500/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100501 * Setup and export the client public value
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100502 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100503int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
504 unsigned char *buf, size_t blen,
505 int (*f_rng)(void *, unsigned char *, size_t),
506 void *p_rng )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100507{
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100508 int restart_enabled = 0;
Hanno Becker91796d72018-12-17 18:10:51 +0000509 ECDH_VALIDATE_RET( ctx != NULL );
510 ECDH_VALIDATE_RET( olen != NULL );
511 ECDH_VALIDATE_RET( buf != NULL );
Hanno Beckerc81cfec2018-12-18 23:32:42 +0000512 ECDH_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100513
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100514#if defined(MBEDTLS_ECP_RESTARTABLE)
515 restart_enabled = ctx->restart_enabled;
516#endif
517
518#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
519 return( ecdh_make_public_internal( ctx, olen, ctx->point_format, buf, blen,
520 f_rng, p_rng, restart_enabled ) );
521#else
522 switch( ctx->var )
523 {
524 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
525 return( ecdh_make_public_internal( &ctx->ctx.mbed_ecdh, olen,
526 ctx->point_format, buf, blen,
527 f_rng, p_rng,
528 restart_enabled ) );
529 default:
530 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
531 }
532#endif
533}
534
535static int ecdh_read_public_internal( mbedtls_ecdh_context_mbed *ctx,
536 const unsigned char *buf, size_t blen )
537{
538 int ret;
539 const unsigned char *p = buf;
540
541 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p,
542 blen ) ) != 0 )
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100543 return( ret );
544
545 if( (size_t)( p - buf ) != blen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100547
548 return( 0 );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100549}
550
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100551/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100552 * Parse and import the client's public value
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100553 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100554int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
555 const unsigned char *buf, size_t blen )
556{
Hanno Becker91796d72018-12-17 18:10:51 +0000557 ECDH_VALIDATE_RET( ctx != NULL );
558 ECDH_VALIDATE_RET( buf != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100559
560#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
561 return( ecdh_read_public_internal( ctx, buf, blen ) );
562#else
563 switch( ctx->var )
564 {
565 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
566 return( ecdh_read_public_internal( &ctx->ctx.mbed_ecdh,
567 buf, blen ) );
568 default:
569 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
570 }
571#endif
572}
573
574static int ecdh_calc_secret_internal( mbedtls_ecdh_context_mbed *ctx,
575 size_t *olen, unsigned char *buf,
576 size_t blen,
577 int (*f_rng)(void *,
578 unsigned char *,
579 size_t),
580 void *p_rng,
581 int restart_enabled )
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100582{
583 int ret;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200584#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200585 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200586#endif
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100587
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200588 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100590
Ron Eldorb430d9f2018-11-05 17:18:29 +0200591#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100592 if( restart_enabled )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200593 rs_ctx = &ctx->rs;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100594#else
595 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200596#endif
597
Ron Eldor2981d8f2018-11-05 18:07:10 +0200598#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100599 if( ( ret = ecdh_compute_shared_restartable( &ctx->grp, &ctx->z, &ctx->Qp,
600 &ctx->d, f_rng, p_rng,
601 rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200602 {
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100603 return( ret );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200604 }
Ron Eldor2981d8f2018-11-05 18:07:10 +0200605#else
606 if( ( ret = mbedtls_ecdh_compute_shared( &ctx->grp, &ctx->z, &ctx->Qp,
607 &ctx->d, f_rng, p_rng ) ) != 0 )
608 {
609 return( ret );
610 }
611#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 if( mbedtls_mpi_size( &ctx->z ) > blen )
614 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Paul Bakker41c83d32013-03-20 14:39:14 +0100615
Manuel Pégourié-Gonnard0a56c2c2014-01-17 21:24:04 +0100616 *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 return mbedtls_mpi_write_binary( &ctx->z, buf, *olen );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100618}
619
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100620/*
621 * Derive and export the shared secret
622 */
623int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
624 unsigned char *buf, size_t blen,
625 int (*f_rng)(void *, unsigned char *, size_t),
626 void *p_rng )
627{
628 int restart_enabled = 0;
Hanno Becker91796d72018-12-17 18:10:51 +0000629 ECDH_VALIDATE_RET( ctx != NULL );
630 ECDH_VALIDATE_RET( olen != NULL );
631 ECDH_VALIDATE_RET( buf != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100632
633#if defined(MBEDTLS_ECP_RESTARTABLE)
634 restart_enabled = ctx->restart_enabled;
635#endif
636
637#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
638 return( ecdh_calc_secret_internal( ctx, olen, buf, blen, f_rng, p_rng,
639 restart_enabled ) );
640#else
641 switch( ctx->var )
642 {
643 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
644 return( ecdh_calc_secret_internal( &ctx->ctx.mbed_ecdh, olen, buf,
645 blen, f_rng, p_rng,
646 restart_enabled ) );
647 default:
648 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
649 }
650#endif
651}
652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653#endif /* MBEDTLS_ECDH_C */