blob: 25a788b9353e2299e0224a3dd3d216923213313b [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
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020090 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, &P, d, Q,
91 f_rng, p_rng, rs_ctx ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 if( mbedtls_ecp_is_zero( &P ) )
Paul Bakkerb548d772013-07-26 14:21:34 +020094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Paul Bakkerb548d772013-07-26 14:21:34 +020096 goto cleanup;
97 }
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( z, &P.X ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100100
101cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 mbedtls_ecp_point_free( &P );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100103
104 return( ret );
105}
106
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100107/*
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200108 * Compute shared secret (SEC1 3.3.1)
109 */
110int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
111 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
112 int (*f_rng)(void *, unsigned char *, size_t),
113 void *p_rng )
114{
115 return( ecdh_compute_shared_restartable( grp, z, Q, d,
116 f_rng, p_rng, NULL ) );
117}
118
119/*
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100120 * Initialize context
121 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx )
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100123{
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200124 mbedtls_ecp_group_init( &ctx->grp );
125 mbedtls_mpi_init( &ctx->d );
126 mbedtls_ecp_point_init( &ctx->Q );
127 mbedtls_ecp_point_init( &ctx->Qp );
128 mbedtls_mpi_init( &ctx->z );
129 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
130 mbedtls_ecp_point_init( &ctx->Vi );
131 mbedtls_ecp_point_init( &ctx->Vf );
132 mbedtls_mpi_init( &ctx->_d );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200133
134#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200135 ctx->restart_enabled = 0;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200136 mbedtls_ecp_restart_init( &ctx->rs );
137#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100138}
139
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100140/*
141 * Free context
142 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx )
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100144{
145 if( ctx == NULL )
146 return;
147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_ecp_group_free( &ctx->grp );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200149 mbedtls_mpi_free( &ctx->d );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 mbedtls_ecp_point_free( &ctx->Q );
151 mbedtls_ecp_point_free( &ctx->Qp );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200152 mbedtls_mpi_free( &ctx->z );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 mbedtls_ecp_point_free( &ctx->Vi );
154 mbedtls_ecp_point_free( &ctx->Vf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 mbedtls_mpi_free( &ctx->_d );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200156
157#if defined(MBEDTLS_ECP_RESTARTABLE)
158 mbedtls_ecp_restart_free( &ctx->rs );
159#endif
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200160
161 mbedtls_ecdh_init( ctx );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100162}
163
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200164#if defined(MBEDTLS_ECP_RESTARTABLE)
165/*
166 * Enable restartable operations for context
167 */
168void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx )
169{
170 ctx->restart_enabled = 1;
171}
172#endif
173
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100174/*
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100175 * Setup and write the ServerKeyExhange parameters (RFC 4492)
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100176 * struct {
177 * ECParameters curve_params;
178 * ECPoint public;
179 * } ServerECDHParams;
180 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100182 unsigned char *buf, size_t blen,
183 int (*f_rng)(void *, unsigned char *, size_t),
184 void *p_rng )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100185{
186 int ret;
187 size_t grp_len, pt_len;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200188 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100189
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100190 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100192
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200193#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200194 if( ctx->restart_enabled )
195 rs_ctx = &ctx->rs;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200196#endif
197
198 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
199 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100200 return( ret );
201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 if( ( ret = mbedtls_ecp_tls_write_group( &ctx->grp, &grp_len, buf, blen ) )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100203 != 0 )
204 return( ret );
205
206 buf += grp_len;
207 blen -= grp_len;
208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 if( ( ret = mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100210 &pt_len, buf, blen ) ) != 0 )
211 return( ret );
212
213 *olen = grp_len + pt_len;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200214 return( 0 );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100215}
216
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100217/*
218 * Read the ServerKeyExhange parameters (RFC 4492)
219 * struct {
220 * ECParameters curve_params;
221 * ECPoint public;
222 * } ServerECDHParams;
223 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100225 const unsigned char **buf, const unsigned char *end )
226{
227 int ret;
228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 if( ( ret = mbedtls_ecp_tls_read_group( &ctx->grp, buf, end - *buf ) ) != 0 )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100230 return( ret );
231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf, end - *buf ) )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100233 != 0 )
234 return( ret );
235
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200236 return( 0 );
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100237}
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100238
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100239/*
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100240 * Get parameters from a keypair
241 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key,
243 mbedtls_ecdh_side side )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100244{
245 int ret;
246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100248 return( ret );
249
250 /* If it's not our key, just import the public part as Qp */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 if( side == MBEDTLS_ECDH_THEIRS )
252 return( mbedtls_ecp_copy( &ctx->Qp, &key->Q ) );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100253
254 /* Our key: import public (as Q) and private parts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 if( side != MBEDTLS_ECDH_OURS )
256 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 if( ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 ||
259 ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100260 return( ret );
261
262 return( 0 );
263}
264
265/*
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100266 * Setup and export the client public value
267 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100269 unsigned char *buf, size_t blen,
270 int (*f_rng)(void *, unsigned char *, size_t),
271 void *p_rng )
272{
273 int ret;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200274 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100275
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100276 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100278
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200279#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200280 if( ctx->restart_enabled )
281 rs_ctx = &ctx->rs;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200282#endif
283
284 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
285 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100286 return( ret );
287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 return mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100289 olen, buf, blen );
290}
291
292/*
293 * Parse and import the client's public value
294 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100296 const unsigned char *buf, size_t blen )
297{
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100298 int ret;
299 const unsigned char *p = buf;
300
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100301 if( ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p, blen ) ) != 0 )
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100305 return( ret );
306
307 if( (size_t)( p - buf ) != blen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100309
310 return( 0 );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100311}
312
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100313/*
314 * Derive and export the shared secret
315 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200317 unsigned char *buf, size_t blen,
318 int (*f_rng)(void *, unsigned char *, size_t),
319 void *p_rng )
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100320{
321 int ret;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200322 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100323
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200324 if( ctx == NULL || ctx->grp.pbits == 0 )
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é-Gonnard66ba48a2017-04-27 11:38:26 +0200327#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200328 if( ctx->restart_enabled )
329 rs_ctx = &ctx->rs;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200330#endif
331
332 if( ( ret = ecdh_compute_shared_restartable( &ctx->grp,
333 &ctx->z, &ctx->Qp, &ctx->d, f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200334 {
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100335 return( ret );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200336 }
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 if( mbedtls_mpi_size( &ctx->z ) > blen )
339 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Paul Bakker41c83d32013-03-20 14:39:14 +0100340
Manuel Pégourié-Gonnard0a56c2c2014-01-17 21:24:04 +0100341 *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 return mbedtls_mpi_write_binary( &ctx->z, buf, *olen );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100343}
344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345#endif /* MBEDTLS_ECDH_C */