blob: a118de53455522ee89a8e863fe1b7a8b7c3d3c4a [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)
44 */
45static int ecdh_gen_public_restartable( mbedtls_ecp_group *grp,
46 mbedtls_mpi *d, mbedtls_ecp_point *Q,
47 int (*f_rng)(void *, unsigned char *, size_t),
48 void *p_rng,
49 mbedtls_ecp_restart_ctx *rs_ctx )
50{
51 int ret;
52
53 /* If multiplication is in progress, we already generated a privkey */
54#if defined(MBEDTLS_ECP_RESTARTABLE)
55 if( rs_ctx == NULL || rs_ctx->rsm == NULL )
56#endif
57 MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, d, f_rng, p_rng ) );
58
59 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, Q, d, &grp->G,
60 f_rng, p_rng, rs_ctx ) );
61
62cleanup:
63 return( ret );
64}
65
66/*
67 * Generate public key
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010068 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010070 int (*f_rng)(void *, unsigned char *, size_t),
71 void *p_rng )
72{
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020073 return( ecdh_gen_public_restartable( grp, d, Q, f_rng, p_rng, NULL ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010074}
Ron Eldora84c1cb2017-10-10 19:04:27 +030075#endif /* MBEDTLS_ECDH_GEN_PUBLIC_ALT */
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010076
Ron Eldora84c1cb2017-10-10 19:04:27 +030077#if !defined(MBEDTLS_ECDH_COMPUTE_SHARED_ALT)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010078/*
79 * Compute shared secret (SEC1 3.3.1)
80 */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020081static int ecdh_compute_shared_restartable( mbedtls_ecp_group *grp,
82 mbedtls_mpi *z,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +020084 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020085 void *p_rng,
86 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010087{
88 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 mbedtls_ecp_point P;
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 mbedtls_ecp_point_init( &P );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010092
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020093 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, &P, d, Q,
94 f_rng, p_rng, rs_ctx ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 if( mbedtls_ecp_is_zero( &P ) )
Paul Bakkerb548d772013-07-26 14:21:34 +020097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Paul Bakkerb548d772013-07-26 14:21:34 +020099 goto cleanup;
100 }
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( z, &P.X ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100103
104cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 mbedtls_ecp_point_free( &P );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100106
107 return( ret );
108}
Ron Eldora84c1cb2017-10-10 19:04:27 +0300109#endif /* MBEDTLS_ECDH_COMPUTE_SHARED_ALT */
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100110
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100111/*
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200112 * Compute shared secret (SEC1 3.3.1)
113 */
114int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
115 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
116 int (*f_rng)(void *, unsigned char *, size_t),
117 void *p_rng )
118{
119 return( ecdh_compute_shared_restartable( grp, z, Q, d,
120 f_rng, p_rng, NULL ) );
121}
122
123/*
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100124 * Initialize context
125 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx )
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100127{
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200128 mbedtls_ecp_group_init( &ctx->grp );
129 mbedtls_mpi_init( &ctx->d );
130 mbedtls_ecp_point_init( &ctx->Q );
131 mbedtls_ecp_point_init( &ctx->Qp );
132 mbedtls_mpi_init( &ctx->z );
133 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
134 mbedtls_ecp_point_init( &ctx->Vi );
135 mbedtls_ecp_point_init( &ctx->Vf );
136 mbedtls_mpi_init( &ctx->_d );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200137
138#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200139 ctx->restart_enabled = 0;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200140 mbedtls_ecp_restart_init( &ctx->rs );
141#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100142}
143
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100144/*
145 * Free context
146 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx )
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100148{
149 if( ctx == NULL )
150 return;
151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_ecp_group_free( &ctx->grp );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200153 mbedtls_mpi_free( &ctx->d );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 mbedtls_ecp_point_free( &ctx->Q );
155 mbedtls_ecp_point_free( &ctx->Qp );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200156 mbedtls_mpi_free( &ctx->z );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_ecp_point_free( &ctx->Vi );
158 mbedtls_ecp_point_free( &ctx->Vf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 mbedtls_mpi_free( &ctx->_d );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200160
161#if defined(MBEDTLS_ECP_RESTARTABLE)
162 mbedtls_ecp_restart_free( &ctx->rs );
163#endif
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200164
165 mbedtls_ecdh_init( ctx );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100166}
167
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200168#if defined(MBEDTLS_ECP_RESTARTABLE)
169/*
170 * Enable restartable operations for context
171 */
172void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx )
173{
174 ctx->restart_enabled = 1;
175}
176#endif
177
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100178/*
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100179 * Setup and write the ServerKeyExhange parameters (RFC 4492)
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100180 * struct {
181 * ECParameters curve_params;
182 * ECPoint public;
183 * } ServerECDHParams;
184 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100186 unsigned char *buf, size_t blen,
187 int (*f_rng)(void *, unsigned char *, size_t),
188 void *p_rng )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100189{
190 int ret;
191 size_t grp_len, pt_len;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200192 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100193
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100194 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100196
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200197#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200198 if( ctx->restart_enabled )
199 rs_ctx = &ctx->rs;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200200#endif
201
202 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
203 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100204 return( ret );
205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 if( ( ret = mbedtls_ecp_tls_write_group( &ctx->grp, &grp_len, buf, blen ) )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100207 != 0 )
208 return( ret );
209
210 buf += grp_len;
211 blen -= grp_len;
212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 if( ( ret = mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100214 &pt_len, buf, blen ) ) != 0 )
215 return( ret );
216
217 *olen = grp_len + pt_len;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200218 return( 0 );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100219}
220
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100221/*
222 * Read the ServerKeyExhange parameters (RFC 4492)
223 * struct {
224 * ECParameters curve_params;
225 * ECPoint public;
226 * } ServerECDHParams;
227 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100229 const unsigned char **buf, const unsigned char *end )
230{
231 int ret;
232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 if( ( ret = mbedtls_ecp_tls_read_group( &ctx->grp, buf, end - *buf ) ) != 0 )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100234 return( ret );
235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf, end - *buf ) )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100237 != 0 )
238 return( ret );
239
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200240 return( 0 );
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100241}
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100242
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100243/*
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100244 * Get parameters from a keypair
245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key,
247 mbedtls_ecdh_side side )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100248{
249 int ret;
250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100252 return( ret );
253
254 /* If it's not our key, just import the public part as Qp */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 if( side == MBEDTLS_ECDH_THEIRS )
256 return( mbedtls_ecp_copy( &ctx->Qp, &key->Q ) );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100257
258 /* Our key: import public (as Q) and private parts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 if( side != MBEDTLS_ECDH_OURS )
260 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 if( ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 ||
263 ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100264 return( ret );
265
266 return( 0 );
267}
268
269/*
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100270 * Setup and export the client public value
271 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100273 unsigned char *buf, size_t blen,
274 int (*f_rng)(void *, unsigned char *, size_t),
275 void *p_rng )
276{
277 int ret;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200278 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100279
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100280 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100282
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200283#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200284 if( ctx->restart_enabled )
285 rs_ctx = &ctx->rs;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200286#endif
287
288 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
289 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100290 return( ret );
291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 return mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100293 olen, buf, blen );
294}
295
296/*
297 * Parse and import the client's public value
298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100300 const unsigned char *buf, size_t blen )
301{
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100302 int ret;
303 const unsigned char *p = buf;
304
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100305 if( ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p, blen ) ) != 0 )
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100309 return( ret );
310
311 if( (size_t)( p - buf ) != blen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100313
314 return( 0 );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100315}
316
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100317/*
318 * Derive and export the shared secret
319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200321 unsigned char *buf, size_t blen,
322 int (*f_rng)(void *, unsigned char *, size_t),
323 void *p_rng )
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100324{
325 int ret;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200326 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100327
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200328 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100330
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200331#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200332 if( ctx->restart_enabled )
333 rs_ctx = &ctx->rs;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200334#endif
335
336 if( ( ret = ecdh_compute_shared_restartable( &ctx->grp,
337 &ctx->z, &ctx->Qp, &ctx->d, f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200338 {
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100339 return( ret );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200340 }
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 if( mbedtls_mpi_size( &ctx->z ) > blen )
343 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Paul Bakker41c83d32013-03-20 14:39:14 +0100344
Manuel Pégourié-Gonnard0a56c2c2014-01-17 21:24:04 +0100345 *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 return mbedtls_mpi_write_binary( &ctx->z, buf, *olen );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100347}
348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349#endif /* MBEDTLS_ECDH_C */