blob: cae3b290f441b3b49cdfc6329c3d07d040165f40 [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é-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 memset( ctx, 0, sizeof( mbedtls_ecdh_context ) );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200130
131#if defined(MBEDTLS_ECP_RESTARTABLE)
132 mbedtls_ecp_restart_init( &ctx->rs );
133#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100134}
135
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100136/*
137 * Free context
138 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx )
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100140{
141 if( ctx == NULL )
142 return;
143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 mbedtls_ecp_group_free( &ctx->grp );
145 mbedtls_ecp_point_free( &ctx->Q );
146 mbedtls_ecp_point_free( &ctx->Qp );
147 mbedtls_ecp_point_free( &ctx->Vi );
148 mbedtls_ecp_point_free( &ctx->Vf );
149 mbedtls_mpi_free( &ctx->d );
150 mbedtls_mpi_free( &ctx->z );
151 mbedtls_mpi_free( &ctx->_d );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200152
153#if defined(MBEDTLS_ECP_RESTARTABLE)
154 mbedtls_ecp_restart_free( &ctx->rs );
155#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100156}
157
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200158#if defined(MBEDTLS_ECP_RESTARTABLE)
159/*
160 * Enable restartable operations for context
161 */
162void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx )
163{
164 ctx->restart_enabled = 1;
165}
166#endif
167
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100168/*
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100169 * Setup and write the ServerKeyExhange parameters (RFC 4492)
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100170 * struct {
171 * ECParameters curve_params;
172 * ECPoint public;
173 * } ServerECDHParams;
174 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100176 unsigned char *buf, size_t blen,
177 int (*f_rng)(void *, unsigned char *, size_t),
178 void *p_rng )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100179{
180 int ret;
181 size_t grp_len, pt_len;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200182 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100183
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100184 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100186
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200187#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200188 if( ctx->restart_enabled )
189 rs_ctx = &ctx->rs;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200190#endif
191
192 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
193 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100194 return( ret );
195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 if( ( ret = mbedtls_ecp_tls_write_group( &ctx->grp, &grp_len, buf, blen ) )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100197 != 0 )
198 return( ret );
199
200 buf += grp_len;
201 blen -= grp_len;
202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 if( ( ret = mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100204 &pt_len, buf, blen ) ) != 0 )
205 return( ret );
206
207 *olen = grp_len + pt_len;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200208 return( 0 );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100209}
210
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100211/*
212 * Read the ServerKeyExhange parameters (RFC 4492)
213 * struct {
214 * ECParameters curve_params;
215 * ECPoint public;
216 * } ServerECDHParams;
217 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100219 const unsigned char **buf, const unsigned char *end )
220{
221 int ret;
222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 if( ( ret = mbedtls_ecp_tls_read_group( &ctx->grp, buf, end - *buf ) ) != 0 )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100224 return( ret );
225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf, end - *buf ) )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100227 != 0 )
228 return( ret );
229
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200230 return( 0 );
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100231}
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100232
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100233/*
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100234 * Get parameters from a keypair
235 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key,
237 mbedtls_ecdh_side side )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100238{
239 int ret;
240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100242 return( ret );
243
244 /* If it's not our key, just import the public part as Qp */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 if( side == MBEDTLS_ECDH_THEIRS )
246 return( mbedtls_ecp_copy( &ctx->Qp, &key->Q ) );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100247
248 /* Our key: import public (as Q) and private parts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 if( side != MBEDTLS_ECDH_OURS )
250 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 if( ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 ||
253 ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100254 return( ret );
255
256 return( 0 );
257}
258
259/*
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100260 * Setup and export the client public value
261 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100263 unsigned char *buf, size_t blen,
264 int (*f_rng)(void *, unsigned char *, size_t),
265 void *p_rng )
266{
267 int ret;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200268 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100269
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100270 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100272
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200273#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200274 if( ctx->restart_enabled )
275 rs_ctx = &ctx->rs;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200276#endif
277
278 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
279 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100280 return( ret );
281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 return mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100283 olen, buf, blen );
284}
285
286/*
287 * Parse and import the client's public value
288 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100290 const unsigned char *buf, size_t blen )
291{
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100292 int ret;
293 const unsigned char *p = buf;
294
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100295 if( ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p, blen ) ) != 0 )
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100299 return( ret );
300
301 if( (size_t)( p - buf ) != blen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100303
304 return( 0 );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100305}
306
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100307/*
308 * Derive and export the shared secret
309 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200311 unsigned char *buf, size_t blen,
312 int (*f_rng)(void *, unsigned char *, size_t),
313 void *p_rng )
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100314{
315 int ret;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200316 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100317
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200318 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100320
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200321#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200322 if( ctx->restart_enabled )
323 rs_ctx = &ctx->rs;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200324#endif
325
326 if( ( ret = ecdh_compute_shared_restartable( &ctx->grp,
327 &ctx->z, &ctx->Qp, &ctx->d, f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200328 {
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100329 return( ret );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200330 }
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 if( mbedtls_mpi_size( &ctx->z ) > blen )
333 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Paul Bakker41c83d32013-03-20 14:39:14 +0100334
Manuel Pégourié-Gonnard0a56c2c2014-01-17 21:24:04 +0100335 *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 return mbedtls_mpi_write_binary( &ctx->z, buf, *olen );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100337}
338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339#endif /* MBEDTLS_ECDH_C */