blob: f05e2c06afd2128a1b8d161c1343ffc00ecf6933 [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 Eldora84c1cb2017-10-10 19:04:27 +030079#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 Eldor8493f802018-11-01 11:32:15 +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/*
149 * Free context
150 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx )
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100152{
153 if( ctx == NULL )
154 return;
155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 mbedtls_ecp_group_free( &ctx->grp );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200157 mbedtls_mpi_free( &ctx->d );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 mbedtls_ecp_point_free( &ctx->Q );
159 mbedtls_ecp_point_free( &ctx->Qp );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200160 mbedtls_mpi_free( &ctx->z );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 mbedtls_ecp_point_free( &ctx->Vi );
162 mbedtls_ecp_point_free( &ctx->Vf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 mbedtls_mpi_free( &ctx->_d );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200164
165#if defined(MBEDTLS_ECP_RESTARTABLE)
166 mbedtls_ecp_restart_free( &ctx->rs );
167#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100168}
169
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200170#if defined(MBEDTLS_ECP_RESTARTABLE)
171/*
172 * Enable restartable operations for context
173 */
174void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx )
175{
176 ctx->restart_enabled = 1;
177}
178#endif
179
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100180/*
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100181 * Setup and write the ServerKeyExhange parameters (RFC 4492)
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100182 * struct {
183 * ECParameters curve_params;
184 * ECPoint public;
185 * } ServerECDHParams;
186 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100188 unsigned char *buf, size_t blen,
189 int (*f_rng)(void *, unsigned char *, size_t),
190 void *p_rng )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100191{
192 int ret;
193 size_t grp_len, pt_len;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200194 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100195
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100196 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100198
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200199#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200200 if( ctx->restart_enabled )
201 rs_ctx = &ctx->rs;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200202#endif
203
Ron Eldor8493f802018-11-01 11:32:15 +0200204
205#if defined(MBEDTLS_ECDH_GEN_PUBLIC_ALT)
206 if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q,
207 f_rng, p_rng ) ) != 0 )
208 return( ret );
209#else
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200210 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200211 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100212 return( ret );
Ron Eldor8493f802018-11-01 11:32:15 +0200213#endif
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 if( ( ret = mbedtls_ecp_tls_write_group( &ctx->grp, &grp_len, buf, blen ) )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100216 != 0 )
217 return( ret );
218
219 buf += grp_len;
220 blen -= grp_len;
221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 if( ( ret = mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200223 &pt_len, buf, blen ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100224 return( ret );
225
226 *olen = grp_len + pt_len;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200227 return( 0 );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100228}
229
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100230/*
231 * Read the ServerKeyExhange parameters (RFC 4492)
232 * struct {
233 * ECParameters curve_params;
234 * ECPoint public;
235 * } ServerECDHParams;
236 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100238 const unsigned char **buf, const unsigned char *end )
239{
240 int ret;
241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 if( ( ret = mbedtls_ecp_tls_read_group( &ctx->grp, buf, end - *buf ) ) != 0 )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100243 return( ret );
244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf, end - *buf ) )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100246 != 0 )
247 return( ret );
248
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200249 return( 0 );
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100250}
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100251
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100252/*
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100253 * Get parameters from a keypair
254 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key,
256 mbedtls_ecdh_side side )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100257{
258 int ret;
259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100261 return( ret );
262
263 /* If it's not our key, just import the public part as Qp */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 if( side == MBEDTLS_ECDH_THEIRS )
265 return( mbedtls_ecp_copy( &ctx->Qp, &key->Q ) );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100266
267 /* Our key: import public (as Q) and private parts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 if( side != MBEDTLS_ECDH_OURS )
269 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 if( ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 ||
272 ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100273 return( ret );
274
275 return( 0 );
276}
277
278/*
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100279 * Setup and export the client public value
280 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100282 unsigned char *buf, size_t blen,
283 int (*f_rng)(void *, unsigned char *, size_t),
284 void *p_rng )
285{
286 int ret;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200287 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100288
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100289 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100291
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200292#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200293 if( ctx->restart_enabled )
294 rs_ctx = &ctx->rs;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200295#endif
296
Ron Eldor8493f802018-11-01 11:32:15 +0200297#if defined(MBEDTLS_ECDH_GEN_PUBLIC_ALT)
298 if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q,
299 f_rng, p_rng ) ) != 0 )
300 return( ret );
301#else
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200302 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
303 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100304 return( ret );
Ron Eldor8493f802018-11-01 11:32:15 +0200305#endif
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 return mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100308 olen, buf, blen );
309}
310
311/*
312 * Parse and import the client's public value
313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100315 const unsigned char *buf, size_t blen )
316{
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100317 int ret;
318 const unsigned char *p = buf;
319
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100320 if( ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p, blen ) ) != 0 )
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100324 return( ret );
325
326 if( (size_t)( p - buf ) != blen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100328
329 return( 0 );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100330}
331
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100332/*
333 * Derive and export the shared secret
334 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200336 unsigned char *buf, size_t blen,
337 int (*f_rng)(void *, unsigned char *, size_t),
338 void *p_rng )
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100339{
340 int ret;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200341 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100342
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200343 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100345
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200346#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200347 if( ctx->restart_enabled )
348 rs_ctx = &ctx->rs;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200349#endif
350
Ron Eldor8493f802018-11-01 11:32:15 +0200351#if defined(MBEDTLS_ECDH_COMPUTE_SHARED_ALT)
352 if( ( ret = mbedtls_ecdh_compute_shared( &ctx->grp, &ctx->z, &ctx->Qp,
353 &ctx->d, f_rng, p_rng ) ) != 0 )
354 {
355 return( ret );
356 }
357#else
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200358 if( ( ret = ecdh_compute_shared_restartable( &ctx->grp,
359 &ctx->z, &ctx->Qp, &ctx->d, f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200360 {
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100361 return( ret );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200362 }
Ron Eldor8493f802018-11-01 11:32:15 +0200363#endif
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 if( mbedtls_mpi_size( &ctx->z ) > blen )
366 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Paul Bakker41c83d32013-03-20 14:39:14 +0100367
Manuel Pégourié-Gonnard0a56c2c2014-01-17 21:24:04 +0100368 *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 return mbedtls_mpi_write_binary( &ctx->z, buf, *olen );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100370}
371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372#endif /* MBEDTLS_ECDH_C */