blob: d68db8ac7483f56a0297f692c1d4e891d24291f9 [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
Janos Follath5a3e1bf2018-08-13 15:54:22 +010041#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
42typedef mbedtls_ecdh_context mbedtls_ecdh_context_mbed;
43#endif
44
Ron Eldora84c1cb2017-10-10 19:04:27 +030045#if !defined(MBEDTLS_ECDH_GEN_PUBLIC_ALT)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010046/*
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020047 * Generate public key (restartable version)
Manuel Pégourié-Gonnardc0edc962018-10-16 10:38:19 +020048 *
49 * Note: this internal function relies on its caller preserving the value of
Manuel Pégourié-Gonnardca29fdf2018-10-22 09:56:53 +020050 * the output parameter 'd' across continuation calls. This would not be
Manuel Pégourié-Gonnardc0edc962018-10-16 10:38:19 +020051 * acceptable for a public function but is OK here as we control call sites.
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020052 */
53static int ecdh_gen_public_restartable( mbedtls_ecp_group *grp,
54 mbedtls_mpi *d, mbedtls_ecp_point *Q,
55 int (*f_rng)(void *, unsigned char *, size_t),
56 void *p_rng,
57 mbedtls_ecp_restart_ctx *rs_ctx )
58{
59 int ret;
60
61 /* If multiplication is in progress, we already generated a privkey */
62#if defined(MBEDTLS_ECP_RESTARTABLE)
63 if( rs_ctx == NULL || rs_ctx->rsm == NULL )
64#endif
65 MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, d, f_rng, p_rng ) );
66
67 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, Q, d, &grp->G,
68 f_rng, p_rng, rs_ctx ) );
69
70cleanup:
71 return( ret );
72}
73
74/*
75 * Generate public key
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010076 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010078 int (*f_rng)(void *, unsigned char *, size_t),
79 void *p_rng )
80{
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020081 return( ecdh_gen_public_restartable( grp, d, Q, f_rng, p_rng, NULL ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010082}
Ron Eldor936d2842018-11-01 13:05:52 +020083#endif /* !MBEDTLS_ECDH_GEN_PUBLIC_ALT */
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010084
Ron Eldora84c1cb2017-10-10 19:04:27 +030085#if !defined(MBEDTLS_ECDH_COMPUTE_SHARED_ALT)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010086/*
87 * Compute shared secret (SEC1 3.3.1)
88 */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020089static int ecdh_compute_shared_restartable( mbedtls_ecp_group *grp,
90 mbedtls_mpi *z,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +020092 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020093 void *p_rng,
94 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010095{
96 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 mbedtls_ecp_point P;
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 mbedtls_ecp_point_init( &P );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100100
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200101 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, &P, d, Q,
102 f_rng, p_rng, rs_ctx ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 if( mbedtls_ecp_is_zero( &P ) )
Paul Bakkerb548d772013-07-26 14:21:34 +0200105 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Paul Bakkerb548d772013-07-26 14:21:34 +0200107 goto cleanup;
108 }
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( z, &P.X ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100111
112cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 mbedtls_ecp_point_free( &P );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100114
115 return( ret );
116}
117
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100118/*
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200119 * Compute shared secret (SEC1 3.3.1)
120 */
121int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
122 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
123 int (*f_rng)(void *, unsigned char *, size_t),
124 void *p_rng )
125{
126 return( ecdh_compute_shared_restartable( grp, z, Q, d,
127 f_rng, p_rng, NULL ) );
128}
Ron Eldor936d2842018-11-01 13:05:52 +0200129#endif /* !MBEDTLS_ECDH_COMPUTE_SHARED_ALT */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200130
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100131static void ecdh_init_internal( mbedtls_ecdh_context_mbed *ctx )
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100132{
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200133 mbedtls_ecp_group_init( &ctx->grp );
134 mbedtls_mpi_init( &ctx->d );
135 mbedtls_ecp_point_init( &ctx->Q );
136 mbedtls_ecp_point_init( &ctx->Qp );
137 mbedtls_mpi_init( &ctx->z );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200138
139#if defined(MBEDTLS_ECP_RESTARTABLE)
140 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/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100145 * Initialize context
Janos Follathf61e4862018-10-30 11:53:25 +0000146 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100147void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx )
148{
149#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
150 ecdh_init_internal( ctx );
151 mbedtls_ecp_point_init( &ctx->Vi );
152 mbedtls_ecp_point_init( &ctx->Vf );
153 mbedtls_mpi_init( &ctx->_d );
154#else
155 memset( ctx, 0, sizeof( mbedtls_ecdh_context ) );
156
157 ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
158#endif
159 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
160#if defined(MBEDTLS_ECP_RESTARTABLE)
161 ctx->restart_enabled = 0;
162#endif
163}
164
165static int ecdh_setup_internal( mbedtls_ecdh_context_mbed *ctx,
166 mbedtls_ecp_group_id grp_id )
Janos Follathf61e4862018-10-30 11:53:25 +0000167{
168 int ret;
169
170 ret = mbedtls_ecp_group_load( &ctx->grp, grp_id );
171 if( ret != 0 )
172 {
Janos Follathf61e4862018-10-30 11:53:25 +0000173 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
174 }
175
176 return( 0 );
177}
178
179/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100180 * Setup context
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100181 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100182int mbedtls_ecdh_setup( mbedtls_ecdh_context *ctx, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100183{
184 if( ctx == NULL )
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100185 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100186
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100187#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
188 return( ecdh_setup_internal( ctx, grp_id ) );
189#else
190 switch( grp_id )
191 {
192 default:
193 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
194 ctx->var = MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0;
195 ctx->grp_id = grp_id;
196 ecdh_init_internal( &ctx->ctx.mbed_ecdh );
197 return( ecdh_setup_internal( &ctx->ctx.mbed_ecdh, grp_id ) );
198 }
199#endif
200}
201
202static void ecdh_free_internal( mbedtls_ecdh_context_mbed *ctx )
203{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 mbedtls_ecp_group_free( &ctx->grp );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200205 mbedtls_mpi_free( &ctx->d );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 mbedtls_ecp_point_free( &ctx->Q );
207 mbedtls_ecp_point_free( &ctx->Qp );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200208 mbedtls_mpi_free( &ctx->z );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200209
210#if defined(MBEDTLS_ECP_RESTARTABLE)
211 mbedtls_ecp_restart_free( &ctx->rs );
212#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100213}
214
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200215#if defined(MBEDTLS_ECP_RESTARTABLE)
216/*
217 * Enable restartable operations for context
218 */
219void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx )
220{
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100221 if( ctx == NULL )
222 return;
223
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200224 ctx->restart_enabled = 1;
225}
226#endif
227
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100228/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100229 * Free context
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100230 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100231void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx )
232{
233 if( ctx == NULL )
234 return;
235
236#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
237 mbedtls_ecp_point_free( &ctx->Vi );
238 mbedtls_ecp_point_free( &ctx->Vf );
239 mbedtls_mpi_free( &ctx->_d );
240 ecdh_free_internal( ctx );
241#else
242 switch( ctx->var )
243 {
244 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
245 ecdh_free_internal( &ctx->ctx.mbed_ecdh );
246 break;
247 default:
248 break;
249 }
250
251 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
252 ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
253 ctx->grp_id = MBEDTLS_ECP_DP_NONE;
254#endif
255}
256
257static int ecdh_make_params_internal( mbedtls_ecdh_context_mbed *ctx,
258 size_t *olen, int point_format,
259 unsigned char *buf, size_t blen,
260 int (*f_rng)(void *,
261 unsigned char *,
262 size_t),
263 void *p_rng,
264 int restart_enabled )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100265{
266 int ret;
267 size_t grp_len, pt_len;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200268#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200269 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200270#endif
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100271
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100272 if( ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100274
Ron Eldor19779c42018-11-05 16:58:13 +0200275#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100276 if( restart_enabled )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200277 rs_ctx = &ctx->rs;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100278#else
279 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200280#endif
281
Ron Eldor8493f802018-11-01 11:32:15 +0200282
Ron Eldor2981d8f2018-11-05 18:07:10 +0200283#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200284 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200285 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100286 return( ret );
Ron Eldor2981d8f2018-11-05 18:07:10 +0200287#else
288 if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q,
289 f_rng, p_rng ) ) != 0 )
290 return( ret );
291#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100292
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100293 if( ( ret = mbedtls_ecp_tls_write_group( &ctx->grp, &grp_len, buf,
294 blen ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100295 return( ret );
296
297 buf += grp_len;
298 blen -= grp_len;
299
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100300 if( ( ret = mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, point_format,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200301 &pt_len, buf, blen ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100302 return( ret );
303
304 *olen = grp_len + pt_len;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200305 return( 0 );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100306}
307
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100308/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100309 * Setup and write the ServerKeyExhange parameters (RFC 4492)
310 * struct {
311 * ECParameters curve_params;
312 * ECPoint public;
313 * } ServerECDHParams;
314 */
315int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
316 unsigned char *buf, size_t blen,
317 int (*f_rng)(void *, unsigned char *, size_t),
318 void *p_rng )
319{
320 int restart_enabled = 0;
321
322 if( ctx == NULL )
323 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
324
325#if defined(MBEDTLS_ECP_RESTARTABLE)
326 restart_enabled = ctx->restart_enabled;
327#else
328 (void) restart_enabled;
329#endif
330
331#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
332 return( ecdh_make_params_internal( ctx, olen, ctx->point_format, buf, blen,
333 f_rng, p_rng, restart_enabled ) );
334#else
335 switch( ctx->var )
336 {
337 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
338 return( ecdh_make_params_internal( &ctx->ctx.mbed_ecdh, olen,
339 ctx->point_format, buf, blen,
340 f_rng, p_rng,
341 restart_enabled ) );
342 default:
343 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
344 }
345#endif
346}
347
348static int ecdh_read_params_internal( mbedtls_ecdh_context_mbed *ctx,
349 const unsigned char **buf,
350 const unsigned char *end )
351{
352 return( mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf,
353 end - *buf ) );
354}
355
356/*
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100357 * Read the ServerKeyExhange parameters (RFC 4492)
358 * struct {
359 * ECParameters curve_params;
360 * ECPoint public;
361 * } ServerECDHParams;
362 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100364 const unsigned char **buf,
365 const unsigned char *end )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100366{
367 int ret;
Janos Follathf61e4862018-10-30 11:53:25 +0000368 mbedtls_ecp_group_id grp_id;
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100369
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100370 if( ctx == NULL )
371 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
372
Janos Follathf61e4862018-10-30 11:53:25 +0000373 if( ( ret = mbedtls_ecp_tls_read_group_id( &grp_id, buf, end - *buf ) )
374 != 0 )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100375 return( ret );
376
Janos Follathf61e4862018-10-30 11:53:25 +0000377 if( ( ret = mbedtls_ecdh_setup( ctx, grp_id ) ) != 0 )
378 return( ret );
379
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100380#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
381 return( ecdh_read_params_internal( ctx, buf, end ) );
382#else
383 switch( ctx->var )
384 {
385 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
386 return( ecdh_read_params_internal( &ctx->ctx.mbed_ecdh,
387 buf, end ) );
388 default:
389 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
390 }
391#endif
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100392}
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100393
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100394static int ecdh_get_params_internal( mbedtls_ecdh_context_mbed *ctx,
395 const mbedtls_ecp_keypair *key,
396 mbedtls_ecdh_side side )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100397{
398 int ret;
399
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100400 /* If it's not our key, just import the public part as Qp */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 if( side == MBEDTLS_ECDH_THEIRS )
402 return( mbedtls_ecp_copy( &ctx->Qp, &key->Q ) );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100403
404 /* Our key: import public (as Q) and private parts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 if( side != MBEDTLS_ECDH_OURS )
406 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 if( ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 ||
409 ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100410 return( ret );
411
412 return( 0 );
413}
414
415/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100416 * Get parameters from a keypair
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100417 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100418int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx,
419 const mbedtls_ecp_keypair *key,
420 mbedtls_ecdh_side side )
421{
422 int ret;
423
424 if( ctx == NULL )
425 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
426
427 if( ( ret = mbedtls_ecdh_setup( ctx, key->grp.id ) ) != 0 )
428 return( ret );
429
430#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
431 return( ecdh_get_params_internal( ctx, key, side ) );
432#else
433 switch( ctx->var )
434 {
435 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
436 return( ecdh_get_params_internal( &ctx->ctx.mbed_ecdh,
437 key, side ) );
438 default:
439 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
440 }
441#endif
442}
443
444static int ecdh_make_public_internal( mbedtls_ecdh_context_mbed *ctx,
445 size_t *olen, int point_format,
446 unsigned char *buf, size_t blen,
447 int (*f_rng)(void *,
448 unsigned char *,
449 size_t),
450 void *p_rng,
451 int restart_enabled )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100452{
453 int ret;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200454#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200455 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200456#endif
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100457
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100458 if( ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100460
Ron Eldorb430d9f2018-11-05 17:18:29 +0200461#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100462 if( restart_enabled )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200463 rs_ctx = &ctx->rs;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100464#else
465 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200466#endif
467
Ron Eldor2981d8f2018-11-05 18:07:10 +0200468#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200469 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100470 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100471 return( ret );
Ron Eldor2981d8f2018-11-05 18:07:10 +0200472#else
473 if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q,
474 f_rng, p_rng ) ) != 0 )
475 return( ret );
476#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100477
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100478 return mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, point_format, olen,
479 buf, blen );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100480}
481
482/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100483 * Setup and export the client public value
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100484 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100485int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
486 unsigned char *buf, size_t blen,
487 int (*f_rng)(void *, unsigned char *, size_t),
488 void *p_rng )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100489{
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100490 int restart_enabled = 0;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100491
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100492 if( ctx == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100494
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100495#if defined(MBEDTLS_ECP_RESTARTABLE)
496 restart_enabled = ctx->restart_enabled;
497#endif
498
499#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
500 return( ecdh_make_public_internal( ctx, olen, ctx->point_format, buf, blen,
501 f_rng, p_rng, restart_enabled ) );
502#else
503 switch( ctx->var )
504 {
505 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
506 return( ecdh_make_public_internal( &ctx->ctx.mbed_ecdh, olen,
507 ctx->point_format, buf, blen,
508 f_rng, p_rng,
509 restart_enabled ) );
510 default:
511 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
512 }
513#endif
514}
515
516static int ecdh_read_public_internal( mbedtls_ecdh_context_mbed *ctx,
517 const unsigned char *buf, size_t blen )
518{
519 int ret;
520 const unsigned char *p = buf;
521
522 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p,
523 blen ) ) != 0 )
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100524 return( ret );
525
526 if( (size_t)( p - buf ) != blen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100528
529 return( 0 );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100530}
531
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100532/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100533 * Parse and import the client's public value
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100534 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100535int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
536 const unsigned char *buf, size_t blen )
537{
538 if( ctx == NULL )
539 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
540
541#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
542 return( ecdh_read_public_internal( ctx, buf, blen ) );
543#else
544 switch( ctx->var )
545 {
546 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
547 return( ecdh_read_public_internal( &ctx->ctx.mbed_ecdh,
548 buf, blen ) );
549 default:
550 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
551 }
552#endif
553}
554
555static int ecdh_calc_secret_internal( mbedtls_ecdh_context_mbed *ctx,
556 size_t *olen, unsigned char *buf,
557 size_t blen,
558 int (*f_rng)(void *,
559 unsigned char *,
560 size_t),
561 void *p_rng,
562 int restart_enabled )
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100563{
564 int ret;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200565#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200566 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200567#endif
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100568
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200569 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100571
Ron Eldorb430d9f2018-11-05 17:18:29 +0200572#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100573 if( restart_enabled )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200574 rs_ctx = &ctx->rs;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100575#else
576 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200577#endif
578
Ron Eldor2981d8f2018-11-05 18:07:10 +0200579#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100580 if( ( ret = ecdh_compute_shared_restartable( &ctx->grp, &ctx->z, &ctx->Qp,
581 &ctx->d, f_rng, p_rng,
582 rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200583 {
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100584 return( ret );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200585 }
Ron Eldor2981d8f2018-11-05 18:07:10 +0200586#else
587 if( ( ret = mbedtls_ecdh_compute_shared( &ctx->grp, &ctx->z, &ctx->Qp,
588 &ctx->d, f_rng, p_rng ) ) != 0 )
589 {
590 return( ret );
591 }
592#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 if( mbedtls_mpi_size( &ctx->z ) > blen )
595 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Paul Bakker41c83d32013-03-20 14:39:14 +0100596
Manuel Pégourié-Gonnard0a56c2c2014-01-17 21:24:04 +0100597 *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 return mbedtls_mpi_write_binary( &ctx->z, buf, *olen );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100599}
600
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100601/*
602 * Derive and export the shared secret
603 */
604int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
605 unsigned char *buf, size_t blen,
606 int (*f_rng)(void *, unsigned char *, size_t),
607 void *p_rng )
608{
609 int restart_enabled = 0;
610
611 if( ctx == NULL )
612 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
613
614#if defined(MBEDTLS_ECP_RESTARTABLE)
615 restart_enabled = ctx->restart_enabled;
616#endif
617
618#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
619 return( ecdh_calc_secret_internal( ctx, olen, buf, blen, f_rng, p_rng,
620 restart_enabled ) );
621#else
622 switch( ctx->var )
623 {
624 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
625 return( ecdh_calc_secret_internal( &ctx->ctx.mbed_ecdh, olen, buf,
626 blen, f_rng, p_rng,
627 restart_enabled ) );
628 default:
629 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
630 }
631#endif
632}
633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634#endif /* MBEDTLS_ECDH_C */