blob: e6ae99994e1a5967907886fc4adf777ccac4755f [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/*
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;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200194#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200195 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200196#endif
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100197
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100198 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100200
Ron Eldor19779c42018-11-05 16:58:13 +0200201#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200202 if( ctx->restart_enabled )
203 rs_ctx = &ctx->rs;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200204#endif
205
Ron Eldor8493f802018-11-01 11:32:15 +0200206
Ron Eldor2981d8f2018-11-05 18:07:10 +0200207#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200208 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200209 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100210 return( ret );
Ron Eldor2981d8f2018-11-05 18:07:10 +0200211#else
212 if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q,
213 f_rng, p_rng ) ) != 0 )
214 return( ret );
215#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 if( ( ret = mbedtls_ecp_tls_write_group( &ctx->grp, &grp_len, buf, blen ) )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100218 != 0 )
219 return( ret );
220
221 buf += grp_len;
222 blen -= grp_len;
223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 if( ( ret = mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200225 &pt_len, buf, blen ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100226 return( ret );
227
228 *olen = grp_len + pt_len;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200229 return( 0 );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100230}
231
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100232/*
233 * Read the ServerKeyExhange parameters (RFC 4492)
234 * struct {
235 * ECParameters curve_params;
236 * ECPoint public;
237 * } ServerECDHParams;
238 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100240 const unsigned char **buf, const unsigned char *end )
241{
242 int ret;
243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 if( ( ret = mbedtls_ecp_tls_read_group( &ctx->grp, buf, end - *buf ) ) != 0 )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100245 return( ret );
246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf, end - *buf ) )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100248 != 0 )
249 return( ret );
250
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200251 return( 0 );
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100252}
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100253
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100254/*
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100255 * Get parameters from a keypair
256 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key,
258 mbedtls_ecdh_side side )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100259{
260 int ret;
261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100263 return( ret );
264
265 /* If it's not our key, just import the public part as Qp */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 if( side == MBEDTLS_ECDH_THEIRS )
267 return( mbedtls_ecp_copy( &ctx->Qp, &key->Q ) );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100268
269 /* Our key: import public (as Q) and private parts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 if( side != MBEDTLS_ECDH_OURS )
271 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 if( ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 ||
274 ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100275 return( ret );
276
277 return( 0 );
278}
279
280/*
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100281 * Setup and export the client public value
282 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100284 unsigned char *buf, size_t blen,
285 int (*f_rng)(void *, unsigned char *, size_t),
286 void *p_rng )
287{
288 int ret;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200289#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200290 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200291#endif
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100292
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100293 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100295
Ron Eldorb430d9f2018-11-05 17:18:29 +0200296#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200297 if( ctx->restart_enabled )
298 rs_ctx = &ctx->rs;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200299#endif
300
Ron Eldor2981d8f2018-11-05 18:07:10 +0200301#if defined(MBEDTLS_ECP_RESTARTABLE)
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 Eldor2981d8f2018-11-05 18:07:10 +0200305#else
306 if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q,
307 f_rng, p_rng ) ) != 0 )
308 return( ret );
309#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 return mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100312 olen, buf, blen );
313}
314
315/*
316 * Parse and import the client's public value
317 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100319 const unsigned char *buf, size_t blen )
320{
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100321 int ret;
322 const unsigned char *p = buf;
323
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100324 if( ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p, blen ) ) != 0 )
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100328 return( ret );
329
330 if( (size_t)( p - buf ) != blen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100332
333 return( 0 );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100334}
335
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100336/*
337 * Derive and export the shared secret
338 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200340 unsigned char *buf, size_t blen,
341 int (*f_rng)(void *, unsigned char *, size_t),
342 void *p_rng )
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100343{
344 int ret;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200345#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200346 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200347#endif
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100348
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200349 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100351
Ron Eldorb430d9f2018-11-05 17:18:29 +0200352#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200353 if( ctx->restart_enabled )
354 rs_ctx = &ctx->rs;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200355#endif
356
Ron Eldor2981d8f2018-11-05 18:07:10 +0200357#if defined(MBEDTLS_ECP_RESTARTABLE)
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 Eldor2981d8f2018-11-05 18:07:10 +0200363#else
364 if( ( ret = mbedtls_ecdh_compute_shared( &ctx->grp, &ctx->z, &ctx->Qp,
365 &ctx->d, f_rng, p_rng ) ) != 0 )
366 {
367 return( ret );
368 }
369#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 if( mbedtls_mpi_size( &ctx->z ) > blen )
372 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Paul Bakker41c83d32013-03-20 14:39:14 +0100373
Manuel Pégourié-Gonnard0a56c2c2014-01-17 21:24:04 +0100374 *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 return mbedtls_mpi_write_binary( &ctx->z, buf, *olen );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100376}
377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378#endif /* MBEDTLS_ECDH_C */