blob: fb46845c906823bb04258ec3fe2cb67af3ef4b0c [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é-Gonnard63533e42013-02-10 14:21:04 +0100164}
165
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200166#if defined(MBEDTLS_ECP_RESTARTABLE)
167/*
168 * Enable restartable operations for context
169 */
170void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx )
171{
172 ctx->restart_enabled = 1;
173}
174#endif
175
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100176/*
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100177 * Setup and write the ServerKeyExhange parameters (RFC 4492)
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100178 * struct {
179 * ECParameters curve_params;
180 * ECPoint public;
181 * } ServerECDHParams;
182 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100184 unsigned char *buf, size_t blen,
185 int (*f_rng)(void *, unsigned char *, size_t),
186 void *p_rng )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100187{
188 int ret;
189 size_t grp_len, pt_len;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200190 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100191
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100192 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100194
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200195#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200196 if( ctx->restart_enabled )
197 rs_ctx = &ctx->rs;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200198#endif
199
200 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200201 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100202 return( ret );
203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 if( ( ret = mbedtls_ecp_tls_write_group( &ctx->grp, &grp_len, buf, blen ) )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100205 != 0 )
206 return( ret );
207
208 buf += grp_len;
209 blen -= grp_len;
210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 if( ( ret = mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200212 &pt_len, buf, blen ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100213 return( ret );
214
215 *olen = grp_len + pt_len;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200216 return( 0 );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100217}
218
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100219/*
220 * Read the ServerKeyExhange parameters (RFC 4492)
221 * struct {
222 * ECParameters curve_params;
223 * ECPoint public;
224 * } ServerECDHParams;
225 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100227 const unsigned char **buf, const unsigned char *end )
228{
229 int ret;
230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 if( ( ret = mbedtls_ecp_tls_read_group( &ctx->grp, buf, end - *buf ) ) != 0 )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100232 return( ret );
233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf, end - *buf ) )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100235 != 0 )
236 return( ret );
237
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200238 return( 0 );
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100239}
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100240
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100241/*
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100242 * Get parameters from a keypair
243 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key,
245 mbedtls_ecdh_side side )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100246{
247 int ret;
248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100250 return( ret );
251
252 /* If it's not our key, just import the public part as Qp */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 if( side == MBEDTLS_ECDH_THEIRS )
254 return( mbedtls_ecp_copy( &ctx->Qp, &key->Q ) );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100255
256 /* Our key: import public (as Q) and private parts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 if( side != MBEDTLS_ECDH_OURS )
258 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 if( ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 ||
261 ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100262 return( ret );
263
264 return( 0 );
265}
266
267/*
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100268 * Setup and export the client public value
269 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100271 unsigned char *buf, size_t blen,
272 int (*f_rng)(void *, unsigned char *, size_t),
273 void *p_rng )
274{
275 int ret;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200276 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100277
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100278 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100280
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200281#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200282 if( ctx->restart_enabled )
283 rs_ctx = &ctx->rs;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200284#endif
285
286 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
287 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100288 return( ret );
289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 return mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100291 olen, buf, blen );
292}
293
294/*
295 * Parse and import the client's public value
296 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100298 const unsigned char *buf, size_t blen )
299{
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100300 int ret;
301 const unsigned char *p = buf;
302
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100303 if( ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p, blen ) ) != 0 )
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100307 return( ret );
308
309 if( (size_t)( p - buf ) != blen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100311
312 return( 0 );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100313}
314
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100315/*
316 * Derive and export the shared secret
317 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200319 unsigned char *buf, size_t blen,
320 int (*f_rng)(void *, unsigned char *, size_t),
321 void *p_rng )
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100322{
323 int ret;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200324 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100325
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200326 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100328
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200329#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200330 if( ctx->restart_enabled )
331 rs_ctx = &ctx->rs;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200332#endif
333
334 if( ( ret = ecdh_compute_shared_restartable( &ctx->grp,
335 &ctx->z, &ctx->Qp, &ctx->d, f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200336 {
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100337 return( ret );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200338 }
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 if( mbedtls_mpi_size( &ctx->z ) > blen )
341 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Paul Bakker41c83d32013-03-20 14:39:14 +0100342
Manuel Pégourié-Gonnard0a56c2c2014-01-17 21:24:04 +0100343 *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 return mbedtls_mpi_write_binary( &ctx->z, buf, *olen );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100345}
346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347#endif /* MBEDTLS_ECDH_C */