blob: 80e9676419c59c49442bfad08977987be4099a87 [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}
Ron Eldora84c1cb2017-10-10 19:04:27 +0300113#endif /* MBEDTLS_ECDH_COMPUTE_SHARED_ALT */
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100114
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100115/*
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200116 * Compute shared secret (SEC1 3.3.1)
117 */
118int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
119 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
120 int (*f_rng)(void *, unsigned char *, size_t),
121 void *p_rng )
122{
123 return( ecdh_compute_shared_restartable( grp, z, Q, d,
124 f_rng, p_rng, NULL ) );
125}
126
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
204 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200205 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100206 return( ret );
207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 if( ( ret = mbedtls_ecp_tls_write_group( &ctx->grp, &grp_len, buf, blen ) )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100209 != 0 )
210 return( ret );
211
212 buf += grp_len;
213 blen -= grp_len;
214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 if( ( ret = mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200216 &pt_len, buf, blen ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100217 return( ret );
218
219 *olen = grp_len + pt_len;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200220 return( 0 );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100221}
222
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100223/*
224 * Read the ServerKeyExhange parameters (RFC 4492)
225 * struct {
226 * ECParameters curve_params;
227 * ECPoint public;
228 * } ServerECDHParams;
229 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100231 const unsigned char **buf, const unsigned char *end )
232{
233 int ret;
234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 if( ( ret = mbedtls_ecp_tls_read_group( &ctx->grp, buf, end - *buf ) ) != 0 )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100236 return( ret );
237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf, end - *buf ) )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100239 != 0 )
240 return( ret );
241
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200242 return( 0 );
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100243}
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100244
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100245/*
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100246 * Get parameters from a keypair
247 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key,
249 mbedtls_ecdh_side side )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100250{
251 int ret;
252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100254 return( ret );
255
256 /* If it's not our key, just import the public part as Qp */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 if( side == MBEDTLS_ECDH_THEIRS )
258 return( mbedtls_ecp_copy( &ctx->Qp, &key->Q ) );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100259
260 /* Our key: import public (as Q) and private parts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 if( side != MBEDTLS_ECDH_OURS )
262 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 if( ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 ||
265 ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100266 return( ret );
267
268 return( 0 );
269}
270
271/*
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100272 * Setup and export the client public value
273 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100275 unsigned char *buf, size_t blen,
276 int (*f_rng)(void *, unsigned char *, size_t),
277 void *p_rng )
278{
279 int ret;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200280 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100281
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100282 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100284
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200285#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200286 if( ctx->restart_enabled )
287 rs_ctx = &ctx->rs;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200288#endif
289
290 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
291 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100292 return( ret );
293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 return mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100295 olen, buf, blen );
296}
297
298/*
299 * Parse and import the client's public value
300 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100302 const unsigned char *buf, size_t blen )
303{
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100304 int ret;
305 const unsigned char *p = buf;
306
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100307 if( ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p, blen ) ) != 0 )
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100311 return( ret );
312
313 if( (size_t)( p - buf ) != blen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100315
316 return( 0 );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100317}
318
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100319/*
320 * Derive and export the shared secret
321 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200323 unsigned char *buf, size_t blen,
324 int (*f_rng)(void *, unsigned char *, size_t),
325 void *p_rng )
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100326{
327 int ret;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200328 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100329
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200330 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100332
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200333#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200334 if( ctx->restart_enabled )
335 rs_ctx = &ctx->rs;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200336#endif
337
338 if( ( ret = ecdh_compute_shared_restartable( &ctx->grp,
339 &ctx->z, &ctx->Qp, &ctx->d, f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200340 {
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100341 return( ret );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200342 }
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 if( mbedtls_mpi_size( &ctx->z ) > blen )
345 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Paul Bakker41c83d32013-03-20 14:39:14 +0100346
Manuel Pégourié-Gonnard0a56c2c2014-01-17 21:24:04 +0100347 *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 return mbedtls_mpi_write_binary( &ctx->z, buf, *olen );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100349}
350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351#endif /* MBEDTLS_ECDH_C */