blob: 37927f7e582d473c52f6d361159c266d3b8199e6 [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;
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;
Hanno Becker91796d72018-12-17 18:10:51 +000067 ECDH_VALIDATE_RET( grp != NULL );
68 ECDH_VALIDATE_RET( d != NULL );
69 ECDH_VALIDATE_RET( Q != NULL );
70 ECDH_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020071
72 /* If multiplication is in progress, we already generated a privkey */
73#if defined(MBEDTLS_ECP_RESTARTABLE)
74 if( rs_ctx == NULL || rs_ctx->rsm == NULL )
75#endif
76 MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, d, f_rng, p_rng ) );
77
78 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, Q, d, &grp->G,
79 f_rng, p_rng, rs_ctx ) );
80
81cleanup:
82 return( ret );
83}
84
85/*
86 * Generate public key
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010087 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010089 int (*f_rng)(void *, unsigned char *, size_t),
90 void *p_rng )
91{
Hanno Becker91796d72018-12-17 18:10:51 +000092 ECDH_VALIDATE_RET( grp != NULL );
93 ECDH_VALIDATE_RET( d != NULL );
94 ECDH_VALIDATE_RET( Q != NULL );
95 ECDH_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020096 return( ecdh_gen_public_restartable( grp, d, Q, f_rng, p_rng, NULL ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010097}
Ron Eldor936d2842018-11-01 13:05:52 +020098#endif /* !MBEDTLS_ECDH_GEN_PUBLIC_ALT */
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010099
Ron Eldora84c1cb2017-10-10 19:04:27 +0300100#if !defined(MBEDTLS_ECDH_COMPUTE_SHARED_ALT)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100101/*
102 * Compute shared secret (SEC1 3.3.1)
103 */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200104static int ecdh_compute_shared_restartable( mbedtls_ecp_group *grp,
105 mbedtls_mpi *z,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200107 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200108 void *p_rng,
109 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100110{
111 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 mbedtls_ecp_point P;
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 mbedtls_ecp_point_init( &P );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100115
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200116 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, &P, d, Q,
117 f_rng, p_rng, rs_ctx ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 if( mbedtls_ecp_is_zero( &P ) )
Paul Bakkerb548d772013-07-26 14:21:34 +0200120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Paul Bakkerb548d772013-07-26 14:21:34 +0200122 goto cleanup;
123 }
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( z, &P.X ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100126
127cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_ecp_point_free( &P );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100129
130 return( ret );
131}
132
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100133/*
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200134 * Compute shared secret (SEC1 3.3.1)
135 */
136int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
137 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
138 int (*f_rng)(void *, unsigned char *, size_t),
139 void *p_rng )
140{
Hanno Becker91796d72018-12-17 18:10:51 +0000141 ECDH_VALIDATE_RET( grp != NULL );
142 ECDH_VALIDATE_RET( Q != NULL );
143 ECDH_VALIDATE_RET( d != NULL );
144 ECDH_VALIDATE_RET( z != NULL );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200145 return( ecdh_compute_shared_restartable( grp, z, Q, d,
146 f_rng, p_rng, NULL ) );
147}
Ron Eldor936d2842018-11-01 13:05:52 +0200148#endif /* !MBEDTLS_ECDH_COMPUTE_SHARED_ALT */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200149
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100150static void ecdh_init_internal( mbedtls_ecdh_context_mbed *ctx )
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100151{
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200152 mbedtls_ecp_group_init( &ctx->grp );
153 mbedtls_mpi_init( &ctx->d );
154 mbedtls_ecp_point_init( &ctx->Q );
155 mbedtls_ecp_point_init( &ctx->Qp );
156 mbedtls_mpi_init( &ctx->z );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200157
158#if defined(MBEDTLS_ECP_RESTARTABLE)
159 mbedtls_ecp_restart_init( &ctx->rs );
160#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100161}
162
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100163/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100164 * Initialize context
Janos Follathf61e4862018-10-30 11:53:25 +0000165 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100166void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx )
167{
Hanno Becker91796d72018-12-17 18:10:51 +0000168 ECDH_VALIDATE( ctx != NULL );
169
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100170#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
171 ecdh_init_internal( ctx );
172 mbedtls_ecp_point_init( &ctx->Vi );
173 mbedtls_ecp_point_init( &ctx->Vf );
174 mbedtls_mpi_init( &ctx->_d );
175#else
176 memset( ctx, 0, sizeof( mbedtls_ecdh_context ) );
177
178 ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
179#endif
180 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
181#if defined(MBEDTLS_ECP_RESTARTABLE)
182 ctx->restart_enabled = 0;
183#endif
184}
185
186static int ecdh_setup_internal( mbedtls_ecdh_context_mbed *ctx,
187 mbedtls_ecp_group_id grp_id )
Janos Follathf61e4862018-10-30 11:53:25 +0000188{
189 int ret;
190
191 ret = mbedtls_ecp_group_load( &ctx->grp, grp_id );
192 if( ret != 0 )
193 {
Janos Follathf61e4862018-10-30 11:53:25 +0000194 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
195 }
196
197 return( 0 );
198}
199
200/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100201 * Setup context
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100202 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100203int mbedtls_ecdh_setup( mbedtls_ecdh_context *ctx, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100204{
Hanno Becker91796d72018-12-17 18:10:51 +0000205 ECDH_VALIDATE_RET( ctx != NULL );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100206
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100207#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
208 return( ecdh_setup_internal( ctx, grp_id ) );
209#else
210 switch( grp_id )
211 {
212 default:
213 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
214 ctx->var = MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0;
215 ctx->grp_id = grp_id;
216 ecdh_init_internal( &ctx->ctx.mbed_ecdh );
217 return( ecdh_setup_internal( &ctx->ctx.mbed_ecdh, grp_id ) );
218 }
219#endif
220}
221
222static void ecdh_free_internal( mbedtls_ecdh_context_mbed *ctx )
223{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 mbedtls_ecp_group_free( &ctx->grp );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200225 mbedtls_mpi_free( &ctx->d );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 mbedtls_ecp_point_free( &ctx->Q );
227 mbedtls_ecp_point_free( &ctx->Qp );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200228 mbedtls_mpi_free( &ctx->z );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200229
230#if defined(MBEDTLS_ECP_RESTARTABLE)
231 mbedtls_ecp_restart_free( &ctx->rs );
232#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100233}
234
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200235#if defined(MBEDTLS_ECP_RESTARTABLE)
236/*
237 * Enable restartable operations for context
238 */
239void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx )
240{
Hanno Beckera7634e82018-12-18 18:45:00 +0000241 ECDH_VALIDATE( ctx != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100242
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200243 ctx->restart_enabled = 1;
244}
245#endif
246
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100247/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100248 * Free context
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100249 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100250void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx )
251{
252 if( ctx == NULL )
253 return;
254
255#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
256 mbedtls_ecp_point_free( &ctx->Vi );
257 mbedtls_ecp_point_free( &ctx->Vf );
258 mbedtls_mpi_free( &ctx->_d );
259 ecdh_free_internal( ctx );
260#else
261 switch( ctx->var )
262 {
263 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
264 ecdh_free_internal( &ctx->ctx.mbed_ecdh );
265 break;
266 default:
267 break;
268 }
269
270 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
271 ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
272 ctx->grp_id = MBEDTLS_ECP_DP_NONE;
273#endif
274}
275
276static int ecdh_make_params_internal( mbedtls_ecdh_context_mbed *ctx,
277 size_t *olen, int point_format,
278 unsigned char *buf, size_t blen,
279 int (*f_rng)(void *,
280 unsigned char *,
281 size_t),
282 void *p_rng,
283 int restart_enabled )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100284{
285 int ret;
286 size_t grp_len, pt_len;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200287#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200288 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200289#endif
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100290
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100291 if( ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100293
Ron Eldor19779c42018-11-05 16:58:13 +0200294#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100295 if( restart_enabled )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200296 rs_ctx = &ctx->rs;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100297#else
298 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200299#endif
300
Ron Eldor8493f802018-11-01 11:32:15 +0200301
Ron Eldor2981d8f2018-11-05 18:07:10 +0200302#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200303 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200304 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100305 return( ret );
Ron Eldor2981d8f2018-11-05 18:07:10 +0200306#else
307 if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q,
308 f_rng, p_rng ) ) != 0 )
309 return( ret );
310#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100311
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100312 if( ( ret = mbedtls_ecp_tls_write_group( &ctx->grp, &grp_len, buf,
313 blen ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100314 return( ret );
315
316 buf += grp_len;
317 blen -= grp_len;
318
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100319 if( ( ret = mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, point_format,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200320 &pt_len, buf, blen ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100321 return( ret );
322
323 *olen = grp_len + pt_len;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200324 return( 0 );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100325}
326
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100327/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100328 * Setup and write the ServerKeyExhange parameters (RFC 4492)
329 * struct {
330 * ECParameters curve_params;
331 * ECPoint public;
332 * } ServerECDHParams;
333 */
334int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
335 unsigned char *buf, size_t blen,
336 int (*f_rng)(void *, unsigned char *, size_t),
337 void *p_rng )
338{
339 int restart_enabled = 0;
Hanno Becker91796d72018-12-17 18:10:51 +0000340 ECDH_VALIDATE_RET( ctx != NULL );
341 ECDH_VALIDATE_RET( olen != NULL );
342 ECDH_VALIDATE_RET( buf != NULL );
343 ECDH_VALIDATE_RET( f_rng != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100344
345#if defined(MBEDTLS_ECP_RESTARTABLE)
346 restart_enabled = ctx->restart_enabled;
347#else
348 (void) restart_enabled;
349#endif
350
351#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
352 return( ecdh_make_params_internal( ctx, olen, ctx->point_format, buf, blen,
353 f_rng, p_rng, restart_enabled ) );
354#else
355 switch( ctx->var )
356 {
357 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
358 return( ecdh_make_params_internal( &ctx->ctx.mbed_ecdh, olen,
359 ctx->point_format, buf, blen,
360 f_rng, p_rng,
361 restart_enabled ) );
362 default:
363 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
364 }
365#endif
366}
367
368static int ecdh_read_params_internal( mbedtls_ecdh_context_mbed *ctx,
369 const unsigned char **buf,
370 const unsigned char *end )
371{
372 return( mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf,
373 end - *buf ) );
374}
375
376/*
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100377 * Read the ServerKeyExhange parameters (RFC 4492)
378 * struct {
379 * ECParameters curve_params;
380 * ECPoint public;
381 * } ServerECDHParams;
382 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100384 const unsigned char **buf,
385 const unsigned char *end )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100386{
387 int ret;
Janos Follathf61e4862018-10-30 11:53:25 +0000388 mbedtls_ecp_group_id grp_id;
Hanno Becker91796d72018-12-17 18:10:51 +0000389 ECDH_VALIDATE_RET( ctx != NULL );
390 ECDH_VALIDATE_RET( buf != NULL );
391 ECDH_VALIDATE_RET( *buf != NULL );
392 ECDH_VALIDATE_RET( end != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100393
Janos Follathf61e4862018-10-30 11:53:25 +0000394 if( ( ret = mbedtls_ecp_tls_read_group_id( &grp_id, buf, end - *buf ) )
395 != 0 )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100396 return( ret );
397
Janos Follathf61e4862018-10-30 11:53:25 +0000398 if( ( ret = mbedtls_ecdh_setup( ctx, grp_id ) ) != 0 )
399 return( ret );
400
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100401#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
402 return( ecdh_read_params_internal( ctx, buf, end ) );
403#else
404 switch( ctx->var )
405 {
406 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
407 return( ecdh_read_params_internal( &ctx->ctx.mbed_ecdh,
408 buf, end ) );
409 default:
410 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
411 }
412#endif
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100413}
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100414
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100415static int ecdh_get_params_internal( mbedtls_ecdh_context_mbed *ctx,
416 const mbedtls_ecp_keypair *key,
417 mbedtls_ecdh_side side )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100418{
419 int ret;
420
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100421 /* If it's not our key, just import the public part as Qp */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 if( side == MBEDTLS_ECDH_THEIRS )
423 return( mbedtls_ecp_copy( &ctx->Qp, &key->Q ) );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100424
425 /* Our key: import public (as Q) and private parts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 if( side != MBEDTLS_ECDH_OURS )
427 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 if( ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 ||
430 ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100431 return( ret );
432
433 return( 0 );
434}
435
436/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100437 * Get parameters from a keypair
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100438 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100439int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx,
440 const mbedtls_ecp_keypair *key,
441 mbedtls_ecdh_side side )
442{
443 int ret;
Hanno Becker91796d72018-12-17 18:10:51 +0000444 ECDH_VALIDATE_RET( ctx != NULL );
445 ECDH_VALIDATE_RET( key != NULL );
446 ECDH_VALIDATE_RET( side == MBEDTLS_ECDH_OURS ||
447 side == MBEDTLS_ECDH_THEIRS );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100448
449 if( ( ret = mbedtls_ecdh_setup( ctx, key->grp.id ) ) != 0 )
450 return( ret );
451
452#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
453 return( ecdh_get_params_internal( ctx, key, side ) );
454#else
455 switch( ctx->var )
456 {
457 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
458 return( ecdh_get_params_internal( &ctx->ctx.mbed_ecdh,
459 key, side ) );
460 default:
461 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
462 }
463#endif
464}
465
466static int ecdh_make_public_internal( mbedtls_ecdh_context_mbed *ctx,
467 size_t *olen, int point_format,
468 unsigned char *buf, size_t blen,
469 int (*f_rng)(void *,
470 unsigned char *,
471 size_t),
472 void *p_rng,
473 int restart_enabled )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100474{
475 int ret;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200476#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200477 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200478#endif
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100479
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100480 if( ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100482
Ron Eldorb430d9f2018-11-05 17:18:29 +0200483#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100484 if( restart_enabled )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200485 rs_ctx = &ctx->rs;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100486#else
487 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200488#endif
489
Ron Eldor2981d8f2018-11-05 18:07:10 +0200490#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200491 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100492 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100493 return( ret );
Ron Eldor2981d8f2018-11-05 18:07:10 +0200494#else
495 if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q,
496 f_rng, p_rng ) ) != 0 )
497 return( ret );
498#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100499
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100500 return mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, point_format, olen,
501 buf, blen );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100502}
503
504/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100505 * Setup and export the client public value
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100506 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100507int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
508 unsigned char *buf, size_t blen,
509 int (*f_rng)(void *, unsigned char *, size_t),
510 void *p_rng )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100511{
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100512 int restart_enabled = 0;
Hanno Becker91796d72018-12-17 18:10:51 +0000513 ECDH_VALIDATE_RET( ctx != NULL );
514 ECDH_VALIDATE_RET( olen != NULL );
515 ECDH_VALIDATE_RET( buf != NULL );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100516
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100517#if defined(MBEDTLS_ECP_RESTARTABLE)
518 restart_enabled = ctx->restart_enabled;
519#endif
520
521#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
522 return( ecdh_make_public_internal( ctx, olen, ctx->point_format, buf, blen,
523 f_rng, p_rng, restart_enabled ) );
524#else
525 switch( ctx->var )
526 {
527 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
528 return( ecdh_make_public_internal( &ctx->ctx.mbed_ecdh, olen,
529 ctx->point_format, buf, blen,
530 f_rng, p_rng,
531 restart_enabled ) );
532 default:
533 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
534 }
535#endif
536}
537
538static int ecdh_read_public_internal( mbedtls_ecdh_context_mbed *ctx,
539 const unsigned char *buf, size_t blen )
540{
541 int ret;
542 const unsigned char *p = buf;
543
544 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p,
545 blen ) ) != 0 )
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100546 return( ret );
547
548 if( (size_t)( p - buf ) != blen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100550
551 return( 0 );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100552}
553
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100554/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100555 * Parse and import the client's public value
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100556 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100557int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
558 const unsigned char *buf, size_t blen )
559{
Hanno Becker91796d72018-12-17 18:10:51 +0000560 ECDH_VALIDATE_RET( ctx != NULL );
561 ECDH_VALIDATE_RET( buf != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100562
563#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
564 return( ecdh_read_public_internal( ctx, buf, blen ) );
565#else
566 switch( ctx->var )
567 {
568 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
569 return( ecdh_read_public_internal( &ctx->ctx.mbed_ecdh,
570 buf, blen ) );
571 default:
572 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
573 }
574#endif
575}
576
577static int ecdh_calc_secret_internal( mbedtls_ecdh_context_mbed *ctx,
578 size_t *olen, unsigned char *buf,
579 size_t blen,
580 int (*f_rng)(void *,
581 unsigned char *,
582 size_t),
583 void *p_rng,
584 int restart_enabled )
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100585{
586 int ret;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200587#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200588 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200589#endif
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100590
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200591 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100593
Ron Eldorb430d9f2018-11-05 17:18:29 +0200594#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100595 if( restart_enabled )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200596 rs_ctx = &ctx->rs;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100597#else
598 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200599#endif
600
Ron Eldor2981d8f2018-11-05 18:07:10 +0200601#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100602 if( ( ret = ecdh_compute_shared_restartable( &ctx->grp, &ctx->z, &ctx->Qp,
603 &ctx->d, f_rng, p_rng,
604 rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200605 {
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100606 return( ret );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200607 }
Ron Eldor2981d8f2018-11-05 18:07:10 +0200608#else
609 if( ( ret = mbedtls_ecdh_compute_shared( &ctx->grp, &ctx->z, &ctx->Qp,
610 &ctx->d, f_rng, p_rng ) ) != 0 )
611 {
612 return( ret );
613 }
614#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 if( mbedtls_mpi_size( &ctx->z ) > blen )
617 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Paul Bakker41c83d32013-03-20 14:39:14 +0100618
Manuel Pégourié-Gonnard0a56c2c2014-01-17 21:24:04 +0100619 *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 return mbedtls_mpi_write_binary( &ctx->z, buf, *olen );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100621}
622
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100623/*
624 * Derive and export the shared secret
625 */
626int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
627 unsigned char *buf, size_t blen,
628 int (*f_rng)(void *, unsigned char *, size_t),
629 void *p_rng )
630{
631 int restart_enabled = 0;
Hanno Becker91796d72018-12-17 18:10:51 +0000632 ECDH_VALIDATE_RET( ctx != NULL );
633 ECDH_VALIDATE_RET( olen != NULL );
634 ECDH_VALIDATE_RET( buf != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100635
636#if defined(MBEDTLS_ECP_RESTARTABLE)
637 restart_enabled = ctx->restart_enabled;
638#endif
639
640#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
641 return( ecdh_calc_secret_internal( ctx, olen, buf, blen, f_rng, p_rng,
642 restart_enabled ) );
643#else
644 switch( ctx->var )
645 {
646 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
647 return( ecdh_calc_secret_internal( &ctx->ctx.mbed_ecdh, olen, buf,
648 blen, f_rng, p_rng,
649 restart_enabled ) );
650 default:
651 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
652 }
653#endif
654}
655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656#endif /* MBEDTLS_ECDH_C */