blob: c5726877d5e73ce65fa2852d000d3883a8d7b273 [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"
Hanno Becker91796d72018-12-17 18:10:51 +000038#include "mbedtls/platform_util.h"
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010039
Rich Evans00ab4702015-02-06 13:43:58 +000040#include <string.h>
41
Hanno Becker91796d72018-12-17 18:10:51 +000042/* Parameter validation macros based on platform_util.h */
43#define ECDH_VALIDATE_RET( cond ) \
44 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_ECP_BAD_INPUT_DATA )
45#define ECDH_VALIDATE( cond ) \
46 MBEDTLS_INTERNAL_VALIDATE( cond )
47
Janos Follath5a3e1bf2018-08-13 15:54:22 +010048#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
49typedef mbedtls_ecdh_context mbedtls_ecdh_context_mbed;
50#endif
51
Gilles Peskine30816292019-02-22 12:31:25 +010052static mbedtls_ecp_group_id mbedtls_ecdh_grp_id(
53 const mbedtls_ecdh_context *ctx )
54{
55#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
56 return( ctx->grp.id );
57#else
58 return( ctx->grp_id );
59#endif
60}
61
Ron Eldora84c1cb2017-10-10 19:04:27 +030062#if !defined(MBEDTLS_ECDH_GEN_PUBLIC_ALT)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010063/*
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020064 * Generate public key (restartable version)
Manuel Pégourié-Gonnardc0edc962018-10-16 10:38:19 +020065 *
66 * Note: this internal function relies on its caller preserving the value of
Manuel Pégourié-Gonnardca29fdf2018-10-22 09:56:53 +020067 * the output parameter 'd' across continuation calls. This would not be
Manuel Pégourié-Gonnardc0edc962018-10-16 10:38:19 +020068 * acceptable for a public function but is OK here as we control call sites.
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020069 */
70static int ecdh_gen_public_restartable( mbedtls_ecp_group *grp,
71 mbedtls_mpi *d, mbedtls_ecp_point *Q,
72 int (*f_rng)(void *, unsigned char *, size_t),
73 void *p_rng,
74 mbedtls_ecp_restart_ctx *rs_ctx )
75{
76 int ret;
77
78 /* If multiplication is in progress, we already generated a privkey */
79#if defined(MBEDTLS_ECP_RESTARTABLE)
80 if( rs_ctx == NULL || rs_ctx->rsm == NULL )
81#endif
82 MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, d, f_rng, p_rng ) );
83
84 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, Q, d, &grp->G,
85 f_rng, p_rng, rs_ctx ) );
86
87cleanup:
88 return( ret );
89}
90
91/*
92 * Generate public key
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010093 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010095 int (*f_rng)(void *, unsigned char *, size_t),
96 void *p_rng )
97{
Hanno Becker91796d72018-12-17 18:10:51 +000098 ECDH_VALIDATE_RET( grp != NULL );
99 ECDH_VALIDATE_RET( d != NULL );
100 ECDH_VALIDATE_RET( Q != NULL );
101 ECDH_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200102 return( ecdh_gen_public_restartable( grp, d, Q, f_rng, p_rng, NULL ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100103}
Ron Eldor936d2842018-11-01 13:05:52 +0200104#endif /* !MBEDTLS_ECDH_GEN_PUBLIC_ALT */
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100105
Ron Eldora84c1cb2017-10-10 19:04:27 +0300106#if !defined(MBEDTLS_ECDH_COMPUTE_SHARED_ALT)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100107/*
108 * Compute shared secret (SEC1 3.3.1)
109 */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200110static int ecdh_compute_shared_restartable( mbedtls_ecp_group *grp,
111 mbedtls_mpi *z,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200113 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200114 void *p_rng,
115 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100116{
117 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 mbedtls_ecp_point P;
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 mbedtls_ecp_point_init( &P );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100121
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200122 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, &P, d, Q,
123 f_rng, p_rng, rs_ctx ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 if( mbedtls_ecp_is_zero( &P ) )
Paul Bakkerb548d772013-07-26 14:21:34 +0200126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Paul Bakkerb548d772013-07-26 14:21:34 +0200128 goto cleanup;
129 }
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( z, &P.X ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100132
133cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_ecp_point_free( &P );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100135
136 return( ret );
137}
138
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100139/*
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200140 * Compute shared secret (SEC1 3.3.1)
141 */
142int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
143 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
144 int (*f_rng)(void *, unsigned char *, size_t),
145 void *p_rng )
146{
Hanno Becker91796d72018-12-17 18:10:51 +0000147 ECDH_VALIDATE_RET( grp != NULL );
148 ECDH_VALIDATE_RET( Q != NULL );
149 ECDH_VALIDATE_RET( d != NULL );
150 ECDH_VALIDATE_RET( z != NULL );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200151 return( ecdh_compute_shared_restartable( grp, z, Q, d,
152 f_rng, p_rng, NULL ) );
153}
Ron Eldor936d2842018-11-01 13:05:52 +0200154#endif /* !MBEDTLS_ECDH_COMPUTE_SHARED_ALT */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200155
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100156static void ecdh_init_internal( mbedtls_ecdh_context_mbed *ctx )
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100157{
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200158 mbedtls_ecp_group_init( &ctx->grp );
159 mbedtls_mpi_init( &ctx->d );
160 mbedtls_ecp_point_init( &ctx->Q );
161 mbedtls_ecp_point_init( &ctx->Qp );
162 mbedtls_mpi_init( &ctx->z );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200163
164#if defined(MBEDTLS_ECP_RESTARTABLE)
165 mbedtls_ecp_restart_init( &ctx->rs );
166#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100167}
168
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100169/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100170 * Initialize context
Janos Follathf61e4862018-10-30 11:53:25 +0000171 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100172void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx )
173{
Hanno Becker91796d72018-12-17 18:10:51 +0000174 ECDH_VALIDATE( ctx != NULL );
175
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100176#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
177 ecdh_init_internal( ctx );
178 mbedtls_ecp_point_init( &ctx->Vi );
179 mbedtls_ecp_point_init( &ctx->Vf );
180 mbedtls_mpi_init( &ctx->_d );
181#else
182 memset( ctx, 0, sizeof( mbedtls_ecdh_context ) );
183
184 ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
185#endif
186 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
187#if defined(MBEDTLS_ECP_RESTARTABLE)
188 ctx->restart_enabled = 0;
189#endif
190}
191
192static int ecdh_setup_internal( mbedtls_ecdh_context_mbed *ctx,
193 mbedtls_ecp_group_id grp_id )
Janos Follathf61e4862018-10-30 11:53:25 +0000194{
195 int ret;
196
197 ret = mbedtls_ecp_group_load( &ctx->grp, grp_id );
198 if( ret != 0 )
199 {
Janos Follathf61e4862018-10-30 11:53:25 +0000200 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
201 }
202
203 return( 0 );
204}
205
206/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100207 * Setup context
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100208 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100209int mbedtls_ecdh_setup( mbedtls_ecdh_context *ctx, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100210{
Hanno Becker91796d72018-12-17 18:10:51 +0000211 ECDH_VALIDATE_RET( ctx != NULL );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100212
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100213#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
214 return( ecdh_setup_internal( ctx, grp_id ) );
215#else
216 switch( grp_id )
217 {
218 default:
219 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
220 ctx->var = MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0;
221 ctx->grp_id = grp_id;
222 ecdh_init_internal( &ctx->ctx.mbed_ecdh );
223 return( ecdh_setup_internal( &ctx->ctx.mbed_ecdh, grp_id ) );
224 }
225#endif
226}
227
228static void ecdh_free_internal( mbedtls_ecdh_context_mbed *ctx )
229{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 mbedtls_ecp_group_free( &ctx->grp );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200231 mbedtls_mpi_free( &ctx->d );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 mbedtls_ecp_point_free( &ctx->Q );
233 mbedtls_ecp_point_free( &ctx->Qp );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200234 mbedtls_mpi_free( &ctx->z );
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200235
236#if defined(MBEDTLS_ECP_RESTARTABLE)
237 mbedtls_ecp_restart_free( &ctx->rs );
238#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100239}
240
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200241#if defined(MBEDTLS_ECP_RESTARTABLE)
242/*
243 * Enable restartable operations for context
244 */
245void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx )
246{
Hanno Beckera7634e82018-12-18 18:45:00 +0000247 ECDH_VALIDATE( ctx != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100248
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200249 ctx->restart_enabled = 1;
250}
251#endif
252
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100253/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100254 * Free context
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100255 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100256void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx )
257{
258 if( ctx == NULL )
259 return;
260
261#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
262 mbedtls_ecp_point_free( &ctx->Vi );
263 mbedtls_ecp_point_free( &ctx->Vf );
264 mbedtls_mpi_free( &ctx->_d );
265 ecdh_free_internal( ctx );
266#else
267 switch( ctx->var )
268 {
269 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
270 ecdh_free_internal( &ctx->ctx.mbed_ecdh );
271 break;
272 default:
273 break;
274 }
275
276 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
277 ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
278 ctx->grp_id = MBEDTLS_ECP_DP_NONE;
279#endif
280}
281
282static int ecdh_make_params_internal( mbedtls_ecdh_context_mbed *ctx,
283 size_t *olen, int point_format,
284 unsigned char *buf, size_t blen,
285 int (*f_rng)(void *,
286 unsigned char *,
287 size_t),
288 void *p_rng,
289 int restart_enabled )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100290{
291 int ret;
292 size_t grp_len, pt_len;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200293#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200294 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200295#endif
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100296
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100297 if( ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100299
Ron Eldor19779c42018-11-05 16:58:13 +0200300#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100301 if( restart_enabled )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200302 rs_ctx = &ctx->rs;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100303#else
304 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200305#endif
306
Ron Eldor8493f802018-11-01 11:32:15 +0200307
Ron Eldor2981d8f2018-11-05 18:07:10 +0200308#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200309 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200310 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100311 return( ret );
Ron Eldor2981d8f2018-11-05 18:07:10 +0200312#else
313 if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q,
314 f_rng, p_rng ) ) != 0 )
315 return( ret );
316#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100317
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100318 if( ( ret = mbedtls_ecp_tls_write_group( &ctx->grp, &grp_len, buf,
319 blen ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100320 return( ret );
321
322 buf += grp_len;
323 blen -= grp_len;
324
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100325 if( ( ret = mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, point_format,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200326 &pt_len, buf, blen ) ) != 0 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100327 return( ret );
328
329 *olen = grp_len + pt_len;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200330 return( 0 );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100331}
332
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100333/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100334 * Setup and write the ServerKeyExhange parameters (RFC 4492)
335 * struct {
336 * ECParameters curve_params;
337 * ECPoint public;
338 * } ServerECDHParams;
339 */
340int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
341 unsigned char *buf, size_t blen,
342 int (*f_rng)(void *, unsigned char *, size_t),
343 void *p_rng )
344{
345 int restart_enabled = 0;
Hanno Becker91796d72018-12-17 18:10:51 +0000346 ECDH_VALIDATE_RET( ctx != NULL );
347 ECDH_VALIDATE_RET( olen != NULL );
348 ECDH_VALIDATE_RET( buf != NULL );
349 ECDH_VALIDATE_RET( f_rng != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100350
351#if defined(MBEDTLS_ECP_RESTARTABLE)
352 restart_enabled = ctx->restart_enabled;
353#else
354 (void) restart_enabled;
355#endif
356
357#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
358 return( ecdh_make_params_internal( ctx, olen, ctx->point_format, buf, blen,
359 f_rng, p_rng, restart_enabled ) );
360#else
361 switch( ctx->var )
362 {
363 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
364 return( ecdh_make_params_internal( &ctx->ctx.mbed_ecdh, olen,
365 ctx->point_format, buf, blen,
366 f_rng, p_rng,
367 restart_enabled ) );
368 default:
369 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
370 }
371#endif
372}
373
374static int ecdh_read_params_internal( mbedtls_ecdh_context_mbed *ctx,
375 const unsigned char **buf,
376 const unsigned char *end )
377{
378 return( mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf,
379 end - *buf ) );
380}
381
382/*
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100383 * Read the ServerKeyExhange parameters (RFC 4492)
384 * struct {
385 * ECParameters curve_params;
386 * ECPoint public;
387 * } ServerECDHParams;
388 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100390 const unsigned char **buf,
391 const unsigned char *end )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100392{
393 int ret;
Janos Follathf61e4862018-10-30 11:53:25 +0000394 mbedtls_ecp_group_id grp_id;
Hanno Becker91796d72018-12-17 18:10:51 +0000395 ECDH_VALIDATE_RET( ctx != NULL );
396 ECDH_VALIDATE_RET( buf != NULL );
397 ECDH_VALIDATE_RET( *buf != NULL );
398 ECDH_VALIDATE_RET( end != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100399
Janos Follathf61e4862018-10-30 11:53:25 +0000400 if( ( ret = mbedtls_ecp_tls_read_group_id( &grp_id, buf, end - *buf ) )
401 != 0 )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100402 return( ret );
403
Janos Follathf61e4862018-10-30 11:53:25 +0000404 if( ( ret = mbedtls_ecdh_setup( ctx, grp_id ) ) != 0 )
405 return( ret );
406
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100407#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
408 return( ecdh_read_params_internal( ctx, buf, end ) );
409#else
410 switch( ctx->var )
411 {
412 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
413 return( ecdh_read_params_internal( &ctx->ctx.mbed_ecdh,
414 buf, end ) );
415 default:
416 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
417 }
418#endif
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100419}
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100420
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100421static int ecdh_get_params_internal( mbedtls_ecdh_context_mbed *ctx,
422 const mbedtls_ecp_keypair *key,
423 mbedtls_ecdh_side side )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100424{
425 int ret;
426
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100427 /* If it's not our key, just import the public part as Qp */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 if( side == MBEDTLS_ECDH_THEIRS )
429 return( mbedtls_ecp_copy( &ctx->Qp, &key->Q ) );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100430
431 /* Our key: import public (as Q) and private parts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 if( side != MBEDTLS_ECDH_OURS )
433 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 if( ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 ||
436 ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 )
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100437 return( ret );
438
439 return( 0 );
440}
441
442/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100443 * Get parameters from a keypair
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100444 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100445int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx,
446 const mbedtls_ecp_keypair *key,
447 mbedtls_ecdh_side side )
448{
449 int ret;
Hanno Becker91796d72018-12-17 18:10:51 +0000450 ECDH_VALIDATE_RET( ctx != NULL );
451 ECDH_VALIDATE_RET( key != NULL );
452 ECDH_VALIDATE_RET( side == MBEDTLS_ECDH_OURS ||
453 side == MBEDTLS_ECDH_THEIRS );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100454
Gilles Peskine30816292019-02-22 12:31:25 +0100455 if( mbedtls_ecdh_grp_id( ctx ) == MBEDTLS_ECP_DP_NONE )
Gilles Peskine0b1b71d2018-11-07 22:10:59 +0100456 {
457 /* This is the first call to get_params(). Set up the context
458 * for use with the group. */
459 if( ( ret = mbedtls_ecdh_setup( ctx, key->grp.id ) ) != 0 )
460 return( ret );
461 }
462 else
463 {
464 /* This is not the first call to get_params(). Check that the
465 * current key's group is the same as the context's, which was set
466 * from the first key's group. */
Gilles Peskine30816292019-02-22 12:31:25 +0100467 if( mbedtls_ecdh_grp_id( ctx ) != key->grp.id )
Gilles Peskine0b1b71d2018-11-07 22:10:59 +0100468 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
469 }
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100470
471#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
472 return( ecdh_get_params_internal( ctx, key, side ) );
473#else
474 switch( ctx->var )
475 {
476 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
477 return( ecdh_get_params_internal( &ctx->ctx.mbed_ecdh,
478 key, side ) );
479 default:
480 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
481 }
482#endif
483}
484
485static int ecdh_make_public_internal( mbedtls_ecdh_context_mbed *ctx,
486 size_t *olen, int point_format,
487 unsigned char *buf, size_t blen,
488 int (*f_rng)(void *,
489 unsigned char *,
490 size_t),
491 void *p_rng,
492 int restart_enabled )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100493{
494 int ret;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200495#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200496 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200497#endif
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100498
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100499 if( ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100501
Ron Eldorb430d9f2018-11-05 17:18:29 +0200502#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100503 if( restart_enabled )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200504 rs_ctx = &ctx->rs;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100505#else
506 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200507#endif
508
Ron Eldor2981d8f2018-11-05 18:07:10 +0200509#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200510 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100511 f_rng, p_rng, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100512 return( ret );
Ron Eldor2981d8f2018-11-05 18:07:10 +0200513#else
514 if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q,
515 f_rng, p_rng ) ) != 0 )
516 return( ret );
517#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100518
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100519 return mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, point_format, olen,
520 buf, blen );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100521}
522
523/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100524 * Setup and export the client public value
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100525 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100526int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
527 unsigned char *buf, size_t blen,
528 int (*f_rng)(void *, unsigned char *, size_t),
529 void *p_rng )
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100530{
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100531 int restart_enabled = 0;
Hanno Becker91796d72018-12-17 18:10:51 +0000532 ECDH_VALIDATE_RET( ctx != NULL );
533 ECDH_VALIDATE_RET( olen != NULL );
534 ECDH_VALIDATE_RET( buf != NULL );
Hanno Beckerc81cfec2018-12-18 23:32:42 +0000535 ECDH_VALIDATE_RET( f_rng != NULL );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100536
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100537#if defined(MBEDTLS_ECP_RESTARTABLE)
538 restart_enabled = ctx->restart_enabled;
539#endif
540
541#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
542 return( ecdh_make_public_internal( ctx, olen, ctx->point_format, buf, blen,
543 f_rng, p_rng, restart_enabled ) );
544#else
545 switch( ctx->var )
546 {
547 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
548 return( ecdh_make_public_internal( &ctx->ctx.mbed_ecdh, olen,
549 ctx->point_format, buf, blen,
550 f_rng, p_rng,
551 restart_enabled ) );
552 default:
553 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
554 }
555#endif
556}
557
558static int ecdh_read_public_internal( mbedtls_ecdh_context_mbed *ctx,
559 const unsigned char *buf, size_t blen )
560{
561 int ret;
562 const unsigned char *p = buf;
563
564 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p,
565 blen ) ) != 0 )
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100566 return( ret );
567
568 if( (size_t)( p - buf ) != blen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100570
571 return( 0 );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100572}
573
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100574/*
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100575 * Parse and import the client's public value
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100576 */
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100577int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
578 const unsigned char *buf, size_t blen )
579{
Hanno Becker91796d72018-12-17 18:10:51 +0000580 ECDH_VALIDATE_RET( ctx != NULL );
581 ECDH_VALIDATE_RET( buf != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100582
583#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
584 return( ecdh_read_public_internal( ctx, buf, blen ) );
585#else
586 switch( ctx->var )
587 {
588 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
589 return( ecdh_read_public_internal( &ctx->ctx.mbed_ecdh,
590 buf, blen ) );
591 default:
592 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
593 }
594#endif
595}
596
597static int ecdh_calc_secret_internal( mbedtls_ecdh_context_mbed *ctx,
598 size_t *olen, unsigned char *buf,
599 size_t blen,
600 int (*f_rng)(void *,
601 unsigned char *,
602 size_t),
603 void *p_rng,
604 int restart_enabled )
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100605{
606 int ret;
Ron Eldor2981d8f2018-11-05 18:07:10 +0200607#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200608 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
Ron Eldor936d2842018-11-01 13:05:52 +0200609#endif
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100610
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200611 if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100613
Ron Eldorb430d9f2018-11-05 17:18:29 +0200614#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100615 if( restart_enabled )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200616 rs_ctx = &ctx->rs;
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100617#else
618 (void) restart_enabled;
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200619#endif
620
Ron Eldor2981d8f2018-11-05 18:07:10 +0200621#if defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100622 if( ( ret = ecdh_compute_shared_restartable( &ctx->grp, &ctx->z, &ctx->Qp,
623 &ctx->d, f_rng, p_rng,
624 rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200625 {
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100626 return( ret );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200627 }
Ron Eldor2981d8f2018-11-05 18:07:10 +0200628#else
629 if( ( ret = mbedtls_ecdh_compute_shared( &ctx->grp, &ctx->z, &ctx->Qp,
630 &ctx->d, f_rng, p_rng ) ) != 0 )
631 {
632 return( ret );
633 }
634#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 if( mbedtls_mpi_size( &ctx->z ) > blen )
637 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Paul Bakker41c83d32013-03-20 14:39:14 +0100638
Manuel Pégourié-Gonnard0a56c2c2014-01-17 21:24:04 +0100639 *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 return mbedtls_mpi_write_binary( &ctx->z, buf, *olen );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100641}
642
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100643/*
644 * Derive and export the shared secret
645 */
646int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
647 unsigned char *buf, size_t blen,
648 int (*f_rng)(void *, unsigned char *, size_t),
649 void *p_rng )
650{
651 int restart_enabled = 0;
Hanno Becker91796d72018-12-17 18:10:51 +0000652 ECDH_VALIDATE_RET( ctx != NULL );
653 ECDH_VALIDATE_RET( olen != NULL );
654 ECDH_VALIDATE_RET( buf != NULL );
Janos Follath5a3e1bf2018-08-13 15:54:22 +0100655
656#if defined(MBEDTLS_ECP_RESTARTABLE)
657 restart_enabled = ctx->restart_enabled;
658#endif
659
660#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
661 return( ecdh_calc_secret_internal( ctx, olen, buf, blen, f_rng, p_rng,
662 restart_enabled ) );
663#else
664 switch( ctx->var )
665 {
666 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
667 return( ecdh_calc_secret_internal( &ctx->ctx.mbed_ecdh, olen, buf,
668 blen, f_rng, p_rng,
669 restart_enabled ) );
670 default:
671 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
672 }
673#endif
674}
675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676#endif /* MBEDTLS_ECDH_C */