2 * Elliptic curve Diffie-Hellman
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * This file is part of mbed TLS (https://tls.mbed.org)
25 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
29 #if !defined(MBEDTLS_CONFIG_FILE)
30 #include "mbedtls/config.h"
32 #include MBEDTLS_CONFIG_FILE
35 #if defined(MBEDTLS_ECDH_C)
37 #include "mbedtls/ecdh.h"
38 #include "mbedtls/platform_util.h"
42 /* 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 )
48 #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
49 typedef mbedtls_ecdh_context mbedtls_ecdh_context_mbed;
52 static mbedtls_ecp_group_id mbedtls_ecdh_grp_id(
53 const mbedtls_ecdh_context *ctx )
55 #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
56 return( ctx->grp.id );
58 return( ctx->grp_id );
62 #if !defined(MBEDTLS_ECDH_GEN_PUBLIC_ALT)
64 * Generate public key (restartable version)
66 * Note: this internal function relies on its caller preserving the value of
67 * the output parameter 'd' across continuation calls. This would not be
68 * acceptable for a public function but is OK here as we control call sites.
70 static 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),
74 mbedtls_ecp_restart_ctx *rs_ctx )
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 )
82 MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, d, f_rng, p_rng ) );
84 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, Q, d, &grp->G,
85 f_rng, p_rng, rs_ctx ) );
94 int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
95 int (*f_rng)(void *, unsigned char *, size_t),
98 ECDH_VALIDATE_RET( grp != NULL );
99 ECDH_VALIDATE_RET( d != NULL );
100 ECDH_VALIDATE_RET( Q != NULL );
101 ECDH_VALIDATE_RET( f_rng != NULL );
102 return( ecdh_gen_public_restartable( grp, d, Q, f_rng, p_rng, NULL ) );
104 #endif /* !MBEDTLS_ECDH_GEN_PUBLIC_ALT */
106 #if !defined(MBEDTLS_ECDH_COMPUTE_SHARED_ALT)
108 * Compute shared secret (SEC1 3.3.1)
110 static int ecdh_compute_shared_restartable( mbedtls_ecp_group *grp,
112 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
113 int (*f_rng)(void *, unsigned char *, size_t),
115 mbedtls_ecp_restart_ctx *rs_ctx )
120 mbedtls_ecp_point_init( &P );
122 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, &P, d, Q,
123 f_rng, p_rng, rs_ctx ) );
125 if( mbedtls_ecp_is_zero( &P ) )
127 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
131 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( z, &P.X ) );
134 mbedtls_ecp_point_free( &P );
140 * Compute shared secret (SEC1 3.3.1)
142 int 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),
147 ECDH_VALIDATE_RET( grp != NULL );
148 ECDH_VALIDATE_RET( Q != NULL );
149 ECDH_VALIDATE_RET( d != NULL );
150 ECDH_VALIDATE_RET( z != NULL );
151 return( ecdh_compute_shared_restartable( grp, z, Q, d,
152 f_rng, p_rng, NULL ) );
154 #endif /* !MBEDTLS_ECDH_COMPUTE_SHARED_ALT */
156 static void ecdh_init_internal( mbedtls_ecdh_context_mbed *ctx )
158 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 );
164 #if defined(MBEDTLS_ECP_RESTARTABLE)
165 mbedtls_ecp_restart_init( &ctx->rs );
172 void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx )
174 ECDH_VALIDATE( ctx != NULL );
176 #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 );
182 memset( ctx, 0, sizeof( mbedtls_ecdh_context ) );
184 ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
186 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
187 #if defined(MBEDTLS_ECP_RESTARTABLE)
188 ctx->restart_enabled = 0;
192 static int ecdh_setup_internal( mbedtls_ecdh_context_mbed *ctx,
193 mbedtls_ecp_group_id grp_id )
197 ret = mbedtls_ecp_group_load( &ctx->grp, grp_id );
200 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
209 int mbedtls_ecdh_setup( mbedtls_ecdh_context *ctx, mbedtls_ecp_group_id grp_id )
211 ECDH_VALIDATE_RET( ctx != NULL );
213 #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
214 return( ecdh_setup_internal( ctx, grp_id ) );
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 ) );
228 static void ecdh_free_internal( mbedtls_ecdh_context_mbed *ctx )
230 mbedtls_ecp_group_free( &ctx->grp );
231 mbedtls_mpi_free( &ctx->d );
232 mbedtls_ecp_point_free( &ctx->Q );
233 mbedtls_ecp_point_free( &ctx->Qp );
234 mbedtls_mpi_free( &ctx->z );
236 #if defined(MBEDTLS_ECP_RESTARTABLE)
237 mbedtls_ecp_restart_free( &ctx->rs );
241 #if defined(MBEDTLS_ECP_RESTARTABLE)
243 * Enable restartable operations for context
245 void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx )
247 ECDH_VALIDATE( ctx != NULL );
249 ctx->restart_enabled = 1;
256 void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx )
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 );
269 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
270 ecdh_free_internal( &ctx->ctx.mbed_ecdh );
276 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
277 ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
278 ctx->grp_id = MBEDTLS_ECP_DP_NONE;
282 static int ecdh_make_params_internal( mbedtls_ecdh_context_mbed *ctx,
283 size_t *olen, int point_format,
284 unsigned char *buf, size_t blen,
289 int restart_enabled )
292 size_t grp_len, pt_len;
293 #if defined(MBEDTLS_ECP_RESTARTABLE)
294 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
297 if( ctx->grp.pbits == 0 )
298 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
300 #if defined(MBEDTLS_ECP_RESTARTABLE)
301 if( restart_enabled )
304 (void) restart_enabled;
308 #if defined(MBEDTLS_ECP_RESTARTABLE)
309 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
310 f_rng, p_rng, rs_ctx ) ) != 0 )
313 if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q,
314 f_rng, p_rng ) ) != 0 )
316 #endif /* MBEDTLS_ECP_RESTARTABLE */
318 if( ( ret = mbedtls_ecp_tls_write_group( &ctx->grp, &grp_len, buf,
325 if( ( ret = mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, point_format,
326 &pt_len, buf, blen ) ) != 0 )
329 *olen = grp_len + pt_len;
334 * Setup and write the ServerKeyExhange parameters (RFC 4492)
336 * ECParameters curve_params;
338 * } ServerECDHParams;
340 int 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),
345 int restart_enabled = 0;
346 ECDH_VALIDATE_RET( ctx != NULL );
347 ECDH_VALIDATE_RET( olen != NULL );
348 ECDH_VALIDATE_RET( buf != NULL );
349 ECDH_VALIDATE_RET( f_rng != NULL );
351 #if defined(MBEDTLS_ECP_RESTARTABLE)
352 restart_enabled = ctx->restart_enabled;
354 (void) restart_enabled;
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 ) );
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,
369 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
374 static int ecdh_read_params_internal( mbedtls_ecdh_context_mbed *ctx,
375 const unsigned char **buf,
376 const unsigned char *end )
378 return( mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf,
383 * Read the ServerKeyExhange parameters (RFC 4492)
385 * ECParameters curve_params;
387 * } ServerECDHParams;
389 int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
390 const unsigned char **buf,
391 const unsigned char *end )
394 mbedtls_ecp_group_id grp_id;
395 ECDH_VALIDATE_RET( ctx != NULL );
396 ECDH_VALIDATE_RET( buf != NULL );
397 ECDH_VALIDATE_RET( *buf != NULL );
398 ECDH_VALIDATE_RET( end != NULL );
400 if( ( ret = mbedtls_ecp_tls_read_group_id( &grp_id, buf, end - *buf ) )
404 if( ( ret = mbedtls_ecdh_setup( ctx, grp_id ) ) != 0 )
407 #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
408 return( ecdh_read_params_internal( ctx, buf, end ) );
412 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
413 return( ecdh_read_params_internal( &ctx->ctx.mbed_ecdh,
416 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
421 static int ecdh_get_params_internal( mbedtls_ecdh_context_mbed *ctx,
422 const mbedtls_ecp_keypair *key,
423 mbedtls_ecdh_side side )
427 /* If it's not our key, just import the public part as Qp */
428 if( side == MBEDTLS_ECDH_THEIRS )
429 return( mbedtls_ecp_copy( &ctx->Qp, &key->Q ) );
431 /* Our key: import public (as Q) and private parts */
432 if( side != MBEDTLS_ECDH_OURS )
433 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
435 if( ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 ||
436 ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 )
443 * Get parameters from a keypair
445 int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx,
446 const mbedtls_ecp_keypair *key,
447 mbedtls_ecdh_side side )
450 ECDH_VALIDATE_RET( ctx != NULL );
451 ECDH_VALIDATE_RET( key != NULL );
452 ECDH_VALIDATE_RET( side == MBEDTLS_ECDH_OURS ||
453 side == MBEDTLS_ECDH_THEIRS );
455 if( mbedtls_ecdh_grp_id( ctx ) == MBEDTLS_ECP_DP_NONE )
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 )
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. */
467 if( mbedtls_ecdh_grp_id( ctx ) != key->grp.id )
468 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
471 #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
472 return( ecdh_get_params_internal( ctx, key, side ) );
476 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
477 return( ecdh_get_params_internal( &ctx->ctx.mbed_ecdh,
480 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
485 static int ecdh_make_public_internal( mbedtls_ecdh_context_mbed *ctx,
486 size_t *olen, int point_format,
487 unsigned char *buf, size_t blen,
492 int restart_enabled )
495 #if defined(MBEDTLS_ECP_RESTARTABLE)
496 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
499 if( ctx->grp.pbits == 0 )
500 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
502 #if defined(MBEDTLS_ECP_RESTARTABLE)
503 if( restart_enabled )
506 (void) restart_enabled;
509 #if defined(MBEDTLS_ECP_RESTARTABLE)
510 if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q,
511 f_rng, p_rng, rs_ctx ) ) != 0 )
514 if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q,
515 f_rng, p_rng ) ) != 0 )
517 #endif /* MBEDTLS_ECP_RESTARTABLE */
519 return mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, point_format, olen,
524 * Setup and export the client public value
526 int 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),
531 int restart_enabled = 0;
532 ECDH_VALIDATE_RET( ctx != NULL );
533 ECDH_VALIDATE_RET( olen != NULL );
534 ECDH_VALIDATE_RET( buf != NULL );
535 ECDH_VALIDATE_RET( f_rng != NULL );
537 #if defined(MBEDTLS_ECP_RESTARTABLE)
538 restart_enabled = ctx->restart_enabled;
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 ) );
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,
553 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
558 static int ecdh_read_public_internal( mbedtls_ecdh_context_mbed *ctx,
559 const unsigned char *buf, size_t blen )
562 const unsigned char *p = buf;
564 if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p,
568 if( (size_t)( p - buf ) != blen )
569 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
575 * Parse and import the client's public value
577 int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
578 const unsigned char *buf, size_t blen )
580 ECDH_VALIDATE_RET( ctx != NULL );
581 ECDH_VALIDATE_RET( buf != NULL );
583 #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
584 return( ecdh_read_public_internal( ctx, buf, blen ) );
588 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
589 return( ecdh_read_public_internal( &ctx->ctx.mbed_ecdh,
592 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
597 static int ecdh_calc_secret_internal( mbedtls_ecdh_context_mbed *ctx,
598 size_t *olen, unsigned char *buf,
604 int restart_enabled )
607 #if defined(MBEDTLS_ECP_RESTARTABLE)
608 mbedtls_ecp_restart_ctx *rs_ctx = NULL;
611 if( ctx == NULL || ctx->grp.pbits == 0 )
612 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
614 #if defined(MBEDTLS_ECP_RESTARTABLE)
615 if( restart_enabled )
618 (void) restart_enabled;
621 #if defined(MBEDTLS_ECP_RESTARTABLE)
622 if( ( ret = ecdh_compute_shared_restartable( &ctx->grp, &ctx->z, &ctx->Qp,
623 &ctx->d, f_rng, p_rng,
629 if( ( ret = mbedtls_ecdh_compute_shared( &ctx->grp, &ctx->z, &ctx->Qp,
630 &ctx->d, f_rng, p_rng ) ) != 0 )
634 #endif /* MBEDTLS_ECP_RESTARTABLE */
636 if( mbedtls_mpi_size( &ctx->z ) > blen )
637 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
639 *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 );
641 if( mbedtls_ecp_get_type( &ctx->grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
642 return mbedtls_mpi_write_binary_le( &ctx->z, buf, *olen );
644 return mbedtls_mpi_write_binary( &ctx->z, buf, *olen );
648 * Derive and export the shared secret
650 int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
651 unsigned char *buf, size_t blen,
652 int (*f_rng)(void *, unsigned char *, size_t),
655 int restart_enabled = 0;
656 ECDH_VALIDATE_RET( ctx != NULL );
657 ECDH_VALIDATE_RET( olen != NULL );
658 ECDH_VALIDATE_RET( buf != NULL );
660 #if defined(MBEDTLS_ECP_RESTARTABLE)
661 restart_enabled = ctx->restart_enabled;
664 #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
665 return( ecdh_calc_secret_internal( ctx, olen, buf, blen, f_rng, p_rng,
670 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
671 return( ecdh_calc_secret_internal( &ctx->ctx.mbed_ecdh, olen, buf,
675 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
680 #endif /* MBEDTLS_ECDH_C */