blob: b8a7dbf0a1657113b80ec86eec9fa71704eb6237 [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
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010041/*
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020042 * Generate public key (restartable version)
43 */
44static int ecdh_gen_public_restartable( mbedtls_ecp_group *grp,
45 mbedtls_mpi *d, mbedtls_ecp_point *Q,
46 int (*f_rng)(void *, unsigned char *, size_t),
47 void *p_rng,
48 mbedtls_ecp_restart_ctx *rs_ctx )
49{
50 int ret;
51
52 /* If multiplication is in progress, we already generated a privkey */
53#if defined(MBEDTLS_ECP_RESTARTABLE)
54 if( rs_ctx == NULL || rs_ctx->rsm == NULL )
55#endif
56 MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, d, f_rng, p_rng ) );
57
58 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, Q, d, &grp->G,
59 f_rng, p_rng, rs_ctx ) );
60
61cleanup:
62 return( ret );
63}
64
65/*
66 * Generate public key
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010067 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010069 int (*f_rng)(void *, unsigned char *, size_t),
70 void *p_rng )
71{
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020072 return( ecdh_gen_public_restartable( grp, d, Q, f_rng, p_rng, NULL ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010073}
74
75/*
76 * Compute shared secret (SEC1 3.3.1)
77 */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020078static int ecdh_compute_shared_restartable( mbedtls_ecp_group *grp,
79 mbedtls_mpi *z,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +020081 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020082 void *p_rng,
83 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010084{
85 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 mbedtls_ecp_point P;
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 mbedtls_ecp_point_init( &P );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010089
90 /*
91 * Make sure Q is a valid pubkey before using it
92 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 MBEDTLS_MPI_CHK( mbedtls_ecp_check_pubkey( grp, Q ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010094
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020095 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, &P, d, Q,
96 f_rng, p_rng, rs_ctx ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 if( mbedtls_ecp_is_zero( &P ) )
Paul Bakkerb548d772013-07-26 14:21:34 +020099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Paul Bakkerb548d772013-07-26 14:21:34 +0200101 goto cleanup;
102 }
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( z, &P.X ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100105
106cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 mbedtls_ecp_point_free( &P );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100108
109 return( ret );
110}
111
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100112/*
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200113 * Compute shared secret (SEC1 3.3.1)
114 */
115int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
116 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
117 int (*f_rng)(void *, unsigned char *, size_t),
118 void *p_rng )
119{
120 return( ecdh_compute_shared_restartable( grp, z, Q, d,
121 f_rng, p_rng, NULL ) );
122}
123
124/*
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100125 * Initialize context
126 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx )
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100128{
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200129 mbedtls_ecp_group_init( &ctx->grp );
130 mbedtls_mpi_init( &ctx->d );
131 mbedtls_ecp_point_init( &ctx->Q );
132 mbedtls_ecp_point_init( &ctx->Qp );
133 mbedtls_mpi_init( &ctx->z );
134 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
135 mbedtls_ecp_point_init( &ctx->Vi );
136 mbedtls_ecp_point_init( &ctx->Vf );
137 mbedtls_mpi_init( &ctx->_d );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200138
139#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200140 ctx->restart_enabled = 0;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200141 mbedtls_ecp_restart_init( &ctx->rs );
142#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100143}
144
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100145/*
146 * Free context
147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx )
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100149{
150 if( ctx == NULL )
151 return;
152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 mbedtls_ecp_group_free( &ctx->grp );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200154 mbedtls_mpi_free( &ctx->d );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 mbedtls_ecp_point_free( &ctx->Q );
156 mbedtls_ecp_point_free( &ctx->Qp );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200157 mbedtls_mpi_free( &ctx->z );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 mbedtls_ecp_point_free( &ctx->Vi );
159 mbedtls_ecp_point_free( &ctx->Vf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 mbedtls_mpi_free( &ctx->_d );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200161
162#if defined(MBEDTLS_ECP_RESTARTABLE)
163 mbedtls_ecp_restart_free( &ctx->rs );
164#endif
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200165
166 mbedtls_ecdh_init( ctx );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100167}
168
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200169#if defined(MBEDTLS_ECP_RESTARTABLE)
170/*
171 * Enable restartable operations for context
172 */
173void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx )
174{
175 ctx->restart_enabled = 1;
176}
177#endif
178
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100179/*
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100180 * Setup and write the ServerKeyExhange parameters (RFC 4492)
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100181 * struct {
182 * ECParameters curve_params;
183 * ECPoint public;
184 * } ServerECDHParams;
185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100187 unsigned char *buf, size_t blen,
188 int (*f_rng)(void *, unsigned char *, size_t),
189 void *p_rng )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100190{
191 int ret;
192 size_t grp_len, pt_len;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200193 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100194
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100195 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100197
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200198#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200199 if( ctx->restart_enabled )
200 rs_ctx = &ctx->rs;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200201#endif
202
203 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
204 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100205 return( ret );
206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 if( ( ret = mbedtls_ecp_tls_write_group( &ctx->grp, &grp_len, buf, blen ) )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100208 != 0 )
209 return( ret );
210
211 buf += grp_len;
212 blen -= grp_len;
213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 if( ( ret = mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100215 &pt_len, buf, blen ) ) != 0 )
216 return( ret );
217
218 *olen = grp_len + pt_len;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200219 return( 0 );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100220}
221
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100222/*
223 * Read the ServerKeyExhange parameters (RFC 4492)
224 * struct {
225 * ECParameters curve_params;
226 * ECPoint public;
227 * } ServerECDHParams;
228 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100230 const unsigned char **buf, const unsigned char *end )
231{
232 int ret;
233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 if( ( ret = mbedtls_ecp_tls_read_group( &ctx->grp, buf, end - *buf ) ) != 0 )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100235 return( ret );
236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf, end - *buf ) )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100238 != 0 )
239 return( ret );
240
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200241 return( 0 );
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100242}
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100243
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100244/*
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100245 * Get parameters from a keypair
246 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key,
248 mbedtls_ecdh_side side )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100249{
250 int ret;
251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100253 return( ret );
254
255 /* If it's not our key, just import the public part as Qp */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 if( side == MBEDTLS_ECDH_THEIRS )
257 return( mbedtls_ecp_copy( &ctx->Qp, &key->Q ) );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100258
259 /* Our key: import public (as Q) and private parts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 if( side != MBEDTLS_ECDH_OURS )
261 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 if( ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 ||
264 ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100265 return( ret );
266
267 return( 0 );
268}
269
270/*
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100271 * Setup and export the client public value
272 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100274 unsigned char *buf, size_t blen,
275 int (*f_rng)(void *, unsigned char *, size_t),
276 void *p_rng )
277{
278 int ret;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200279 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100280
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100281 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100283
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200284#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200285 if( ctx->restart_enabled )
286 rs_ctx = &ctx->rs;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200287#endif
288
289 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
290 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100291 return( ret );
292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 return mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100294 olen, buf, blen );
295}
296
297/*
298 * Parse and import the client's public value
299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100301 const unsigned char *buf, size_t blen )
302{
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100303 int ret;
304 const unsigned char *p = buf;
305
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100306 if( ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p, blen ) ) != 0 )
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100310 return( ret );
311
312 if( (size_t)( p - buf ) != blen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100314
315 return( 0 );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100316}
317
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100318/*
319 * Derive and export the shared secret
320 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200322 unsigned char *buf, size_t blen,
323 int (*f_rng)(void *, unsigned char *, size_t),
324 void *p_rng )
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100325{
326 int ret;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200327 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100328
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200329 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100331
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200332#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200333 if( ctx->restart_enabled )
334 rs_ctx = &ctx->rs;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200335#endif
336
337 if( ( ret = ecdh_compute_shared_restartable( &ctx->grp,
338 &ctx->z, &ctx->Qp, &ctx->d, f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200339 {
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100340 return( ret );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200341 }
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 if( mbedtls_mpi_size( &ctx->z ) > blen )
344 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Paul Bakker41c83d32013-03-20 14:39:14 +0100345
Manuel Pégourié-Gonnard0a56c2c2014-01-17 21:24:04 +0100346 *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 return mbedtls_mpi_write_binary( &ctx->z, buf, *olen );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100348}
349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350#endif /* MBEDTLS_ECDH_C */