blob: 702ba1a40974b18d223951b07502370e91ff95b8 [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"
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010038
Rich Evans00ab4702015-02-06 13:43:58 +000039#include <string.h>
40
Ron Eldora84c1cb2017-10-10 19:04:27 +030041#if !defined(MBEDTLS_ECDH_GEN_PUBLIC_ALT)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010042/*
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020043 * Generate public key (restartable version)
Manuel Pégourié-Gonnardc0edc962018-10-16 10:38:19 +020044 *
45 * Note: this internal function relies on its caller preserving the value of
Manuel Pégourié-Gonnardca29fdf2018-10-22 09:56:53 +020046 * the output parameter 'd' across continuation calls. This would not be
Manuel Pégourié-Gonnardc0edc962018-10-16 10:38:19 +020047 * acceptable for a public function but is OK here as we control call sites.
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020048 */
49static int ecdh_gen_public_restartable( mbedtls_ecp_group *grp,
50 mbedtls_mpi *d, mbedtls_ecp_point *Q,
51 int (*f_rng)(void *, unsigned char *, size_t),
52 void *p_rng,
53 mbedtls_ecp_restart_ctx *rs_ctx )
54{
55 int ret;
56
57 /* If multiplication is in progress, we already generated a privkey */
58#if defined(MBEDTLS_ECP_RESTARTABLE)
59 if( rs_ctx == NULL || rs_ctx->rsm == NULL )
60#endif
61 MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, d, f_rng, p_rng ) );
62
63 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, Q, d, &grp->G,
64 f_rng, p_rng, rs_ctx ) );
65
66cleanup:
67 return( ret );
68}
69
70/*
71 * Generate public key
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010072 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010074 int (*f_rng)(void *, unsigned char *, size_t),
75 void *p_rng )
76{
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020077 return( ecdh_gen_public_restartable( grp, d, Q, f_rng, p_rng, NULL ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010078}
Ron Eldor936d2842018-11-01 13:05:52 +020079#endif /* !MBEDTLS_ECDH_GEN_PUBLIC_ALT */
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010080
Ron Eldora84c1cb2017-10-10 19:04:27 +030081#if !defined(MBEDTLS_ECDH_COMPUTE_SHARED_ALT)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010082/*
83 * Compute shared secret (SEC1 3.3.1)
84 */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020085static int ecdh_compute_shared_restartable( mbedtls_ecp_group *grp,
86 mbedtls_mpi *z,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +020088 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020089 void *p_rng,
90 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010091{
92 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 mbedtls_ecp_point P;
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 mbedtls_ecp_point_init( &P );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010096
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020097 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, &P, d, Q,
98 f_rng, p_rng, rs_ctx ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 if( mbedtls_ecp_is_zero( &P ) )
Paul Bakkerb548d772013-07-26 14:21:34 +0200101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Paul Bakkerb548d772013-07-26 14:21:34 +0200103 goto cleanup;
104 }
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( z, &P.X ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100107
108cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 mbedtls_ecp_point_free( &P );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100110
111 return( ret );
112}
113
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100114/*
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200115 * Compute shared secret (SEC1 3.3.1)
116 */
117int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
118 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
119 int (*f_rng)(void *, unsigned char *, size_t),
120 void *p_rng )
121{
122 return( ecdh_compute_shared_restartable( grp, z, Q, d,
123 f_rng, p_rng, NULL ) );
124}
Ron Eldor936d2842018-11-01 13:05:52 +0200125#endif /* !MBEDTLS_ECDH_COMPUTE_SHARED_ALT */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200126
127/*
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100128 * Initialize context
129 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx )
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100131{
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200132 mbedtls_ecp_group_init( &ctx->grp );
133 mbedtls_mpi_init( &ctx->d );
134 mbedtls_ecp_point_init( &ctx->Q );
135 mbedtls_ecp_point_init( &ctx->Qp );
136 mbedtls_mpi_init( &ctx->z );
137 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
138 mbedtls_ecp_point_init( &ctx->Vi );
139 mbedtls_ecp_point_init( &ctx->Vf );
140 mbedtls_mpi_init( &ctx->_d );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200141
142#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200143 ctx->restart_enabled = 0;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200144 mbedtls_ecp_restart_init( &ctx->rs );
145#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100146}
147
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100148/*
Janos Follathf61e4862018-10-30 11:53:25 +0000149 * Setup context
150 */
151int mbedtls_ecdh_setup( mbedtls_ecdh_context *ctx, mbedtls_ecp_group_id grp_id )
152{
153 int ret;
154
155 ret = mbedtls_ecp_group_load( &ctx->grp, grp_id );
156 if( ret != 0 )
157 {
158 mbedtls_ecdh_free( ctx );
159 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
160 }
161
162 return( 0 );
163}
164
165/*
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100166 * Free context
167 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx )
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100169{
170 if( ctx == NULL )
171 return;
172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 mbedtls_ecp_group_free( &ctx->grp );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200174 mbedtls_mpi_free( &ctx->d );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_ecp_point_free( &ctx->Q );
176 mbedtls_ecp_point_free( &ctx->Qp );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200177 mbedtls_mpi_free( &ctx->z );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 mbedtls_ecp_point_free( &ctx->Vi );
179 mbedtls_ecp_point_free( &ctx->Vf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 mbedtls_mpi_free( &ctx->_d );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200181
182#if defined(MBEDTLS_ECP_RESTARTABLE)
183 mbedtls_ecp_restart_free( &ctx->rs );
184#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100185}
186
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200187#if defined(MBEDTLS_ECP_RESTARTABLE)
188/*
189 * Enable restartable operations for context
190 */
191void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx )
192{
193 ctx->restart_enabled = 1;
194}
195#endif
196
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100197/*
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100198 * Setup and write the ServerKeyExhange parameters (RFC 4492)
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100199 * struct {
200 * ECParameters curve_params;
201 * ECPoint public;
202 * } ServerECDHParams;
203 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100205 unsigned char *buf, size_t blen,
206 int (*f_rng)(void *, unsigned char *, size_t),
207 void *p_rng )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100208{
209 int ret;
210 size_t grp_len, pt_len;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200211#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200212 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200213#endif
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100214
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100215 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100217
Ron Eldor19779c42018-11-05 16:58:13 +0200218#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200219 if( ctx->restart_enabled )
220 rs_ctx = &ctx->rs;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200221#endif
222
Ron Eldor8493f802018-11-01 11:32:15 +0200223
Ron Eldor2981d8f2018-11-05 18:07:10 +0200224#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200225 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200226 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100227 return( ret );
Ron Eldor2981d8f2018-11-05 18:07:10 +0200228#else
229 if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q,
230 f_rng, p_rng ) ) != 0 )
231 return( ret );
232#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 if( ( ret = mbedtls_ecp_tls_write_group( &ctx->grp, &grp_len, buf, blen ) )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100235 != 0 )
236 return( ret );
237
238 buf += grp_len;
239 blen -= grp_len;
240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 if( ( ret = mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200242 &pt_len, buf, blen ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100243 return( ret );
244
245 *olen = grp_len + pt_len;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200246 return( 0 );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100247}
248
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100249/*
250 * Read the ServerKeyExhange parameters (RFC 4492)
251 * struct {
252 * ECParameters curve_params;
253 * ECPoint public;
254 * } ServerECDHParams;
255 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100257 const unsigned char **buf, const unsigned char *end )
258{
259 int ret;
Janos Follathf61e4862018-10-30 11:53:25 +0000260 mbedtls_ecp_group_id grp_id;
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100261
Janos Follathf61e4862018-10-30 11:53:25 +0000262 if( ( ret = mbedtls_ecp_tls_read_group_id( &grp_id, buf, end - *buf ) )
263 != 0 )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100264 return( ret );
265
Janos Follathf61e4862018-10-30 11:53:25 +0000266 if( ( ret = mbedtls_ecdh_setup( ctx, grp_id ) ) != 0 )
267 return( ret );
268
269 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf,
270 end - *buf ) ) != 0 )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100271 return( ret );
272
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200273 return( 0 );
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100274}
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100275
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100276/*
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100277 * Get parameters from a keypair
278 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key,
280 mbedtls_ecdh_side side )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100281{
282 int ret;
283
Janos Follathf61e4862018-10-30 11:53:25 +0000284 if( ( ret = mbedtls_ecdh_setup( ctx, key->grp.id ) ) != 0 )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100285 return( ret );
286
287 /* If it's not our key, just import the public part as Qp */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 if( side == MBEDTLS_ECDH_THEIRS )
289 return( mbedtls_ecp_copy( &ctx->Qp, &key->Q ) );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100290
291 /* Our key: import public (as Q) and private parts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 if( side != MBEDTLS_ECDH_OURS )
293 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 if( ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 ||
296 ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100297 return( ret );
298
299 return( 0 );
300}
301
302/*
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100303 * Setup and export the client public value
304 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100306 unsigned char *buf, size_t blen,
307 int (*f_rng)(void *, unsigned char *, size_t),
308 void *p_rng )
309{
310 int ret;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200311#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200312 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200313#endif
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100314
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100315 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100317
Ron Eldorb430d9f2018-11-05 17:18:29 +0200318#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200319 if( ctx->restart_enabled )
320 rs_ctx = &ctx->rs;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200321#endif
322
Ron Eldor2981d8f2018-11-05 18:07:10 +0200323#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200324 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
325 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100326 return( ret );
Ron Eldor2981d8f2018-11-05 18:07:10 +0200327#else
328 if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q,
329 f_rng, p_rng ) ) != 0 )
330 return( ret );
331#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 return mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100334 olen, buf, blen );
335}
336
337/*
338 * Parse and import the client's public value
339 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100341 const unsigned char *buf, size_t blen )
342{
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100343 int ret;
344 const unsigned char *p = buf;
345
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100346 if( ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p, blen ) ) != 0 )
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100350 return( ret );
351
352 if( (size_t)( p - buf ) != blen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100354
355 return( 0 );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100356}
357
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100358/*
359 * Derive and export the shared secret
360 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200362 unsigned char *buf, size_t blen,
363 int (*f_rng)(void *, unsigned char *, size_t),
364 void *p_rng )
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100365{
366 int ret;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200367#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200368 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200369#endif
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100370
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200371 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100373
Ron Eldorb430d9f2018-11-05 17:18:29 +0200374#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200375 if( ctx->restart_enabled )
376 rs_ctx = &ctx->rs;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200377#endif
378
Ron Eldor2981d8f2018-11-05 18:07:10 +0200379#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200380 if( ( ret = ecdh_compute_shared_restartable( &ctx->grp,
381 &ctx->z, &ctx->Qp, &ctx->d, f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200382 {
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100383 return( ret );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200384 }
Ron Eldor2981d8f2018-11-05 18:07:10 +0200385#else
386 if( ( ret = mbedtls_ecdh_compute_shared( &ctx->grp, &ctx->z, &ctx->Qp,
387 &ctx->d, f_rng, p_rng ) ) != 0 )
388 {
389 return( ret );
390 }
391#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 if( mbedtls_mpi_size( &ctx->z ) > blen )
394 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Paul Bakker41c83d32013-03-20 14:39:14 +0100395
Manuel Pégourié-Gonnard0a56c2c2014-01-17 21:24:04 +0100396 *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 return mbedtls_mpi_write_binary( &ctx->z, buf, *olen );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100398}
399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400#endif /* MBEDTLS_ECDH_C */