Skip to content

Commit 0ea3377

Browse files
committed
Merge remote-tracking branch 'restricted/pr/552' into development
Ensure this merge passes tests by auto-generating query_config.c, adding MBEDTLS_ECDH_LEGACY_CONTEXT to it. * restricted/pr/552: Fix mbedtls_ecdh_get_params with new ECDH context Test undefining MBEDTLS_ECDH_LEGACY_CONTEXT in all.sh Define MBEDTLS_ECDH_LEGACY_CONTEXT in config.h Add changelog entry for mbedtls_ecdh_get_params robustness Fix ecdh_get_params with mismatching group Add test case for ecdh_get_params with mismatching group Add test case for ecdh_calc_secret Fix typo in documentation
2 parents c73fde7 + 3081629 commit 0ea3377

File tree

11 files changed

+253
-16
lines changed

11 files changed

+253
-16
lines changed

ChangeLog

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ API Changes
5050
mbedtls_ssl_session structure which otherwise stores the peer's
5151
certificate.
5252

53+
Security
54+
* Make mbedtls_ecdh_get_params return an error if the second key
55+
belongs to a different group from the first. Before, if an application
56+
passed keys that belonged to different group, the first key's data was
57+
interpreted according to the second group, which could lead to either
58+
an error or a meaningless output from mbedtls_ecdh_get_params. In the
59+
latter case, this could expose at most 5 bits of the private key.
60+
5361
Bugfix
5462
* Fix a compilation issue with mbedtls_ecp_restart_ctx not being defined
5563
when MBEDTLS_ECP_ALT is defined. Reported by jwhui. Fixes #2242.

include/mbedtls/check_config.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@
125125
#error "MBEDTLS_ECP_RESTARTABLE defined, but it cannot coexist with an alternative or PSA-based ECP implementation"
126126
#endif
127127

128+
#if defined(MBEDTLS_ECP_RESTARTABLE) && \
129+
! defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
130+
#error "MBEDTLS_ECP_RESTARTABLE defined, but not MBEDTLS_ECDH_LEGACY_CONTEXT"
131+
#endif
132+
128133
#if defined(MBEDTLS_ECDSA_DETERMINISTIC) && !defined(MBEDTLS_HMAC_DRBG_C)
129134
#error "MBEDTLS_ECDSA_DETERMINISTIC defined, but not all prerequisites"
130135
#endif

include/mbedtls/config.h

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,10 +760,39 @@
760760
*
761761
* \note This option only works with the default software implementation of
762762
* elliptic curve functionality. It is incompatible with
763-
* MBEDTLS_ECP_ALT, MBEDTLS_ECDH_XXX_ALT and MBEDTLS_ECDSA_XXX_ALT.
763+
* MBEDTLS_ECP_ALT, MBEDTLS_ECDH_XXX_ALT, MBEDTLS_ECDSA_XXX_ALT
764+
* and MBEDTLS_ECDH_LEGACY_CONTEXT.
764765
*/
765766
//#define MBEDTLS_ECP_RESTARTABLE
766767

768+
/**
769+
* \def MBEDTLS_ECDH_LEGACY_CONTEXT
770+
*
771+
* Use a backward compatible ECDH context.
772+
*
773+
* Mbed TLS supports two formats for ECDH contexts (#mbedtls_ecdh_context
774+
* defined in `ecdh.h`). For most applications, the choice of format makes
775+
* no difference, since all library functions can work with either format,
776+
* except that the new format is incompatible with MBEDTLS_ECP_RESTARTABLE.
777+
778+
* The new format used when this option is disabled is smaller
779+
* (56 bytes on a 32-bit platform). In future versions of the library, it
780+
* will support alternative implementations of ECDH operations.
781+
* The new format is incompatible with applications that access
782+
* context fields directly and with restartable ECP operations.
783+
*
784+
* Define this macro if you enable MBEDTLS_ECP_RESTARTABLE or if you
785+
* want to access ECDH context fields directly. Otherwise you should
786+
* comment out this macro definition.
787+
*
788+
* This option has no effect if #MBEDTLS_ECDH_C is not enabled.
789+
*
790+
* \note This configuration option is experimental. Future versions of the
791+
* library may modify the way the ECDH context layout is configured
792+
* and may modify the layout of the new context type.
793+
*/
794+
#define MBEDTLS_ECDH_LEGACY_CONTEXT
795+
767796
/**
768797
* \def MBEDTLS_ECDSA_DETERMINISTIC
769798
*

include/mbedtls/ecdh.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,6 @@
4242

4343
#include "ecp.h"
4444

45-
/*
46-
* Use a backward compatible ECDH context.
47-
*
48-
* This flag is always enabled for now and future versions might add a
49-
* configuration option that conditionally undefines this flag.
50-
* The configuration option in question may have a different name.
51-
*
52-
* Features undefining this flag, must have a warning in their description in
53-
* config.h stating that the feature breaks backward compatibility.
54-
*/
55-
#define MBEDTLS_ECDH_LEGACY_CONTEXT
56-
5745
#ifdef __cplusplus
5846
extern "C" {
5947
#endif

include/mbedtls/ecp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ void mbedtls_ecp_point_init( mbedtls_ecp_point *pt );
482482
*
483483
* \note After this function is called, domain parameters
484484
* for various ECP groups can be loaded through the
485-
* mbedtls_ecp_load() or mbedtls_ecp_tls_read_group()
485+
* mbedtls_ecp_group_load() or mbedtls_ecp_tls_read_group()
486486
* functions.
487487
*/
488488
void mbedtls_ecp_group_init( mbedtls_ecp_group *grp );

library/ecdh.c

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@
4949
typedef mbedtls_ecdh_context mbedtls_ecdh_context_mbed;
5050
#endif
5151

52+
static 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+
5262
#if !defined(MBEDTLS_ECDH_GEN_PUBLIC_ALT)
5363
/*
5464
* Generate public key (restartable version)
@@ -442,8 +452,21 @@ int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx,
442452
ECDH_VALIDATE_RET( side == MBEDTLS_ECDH_OURS ||
443453
side == MBEDTLS_ECDH_THEIRS );
444454

445-
if( ( ret = mbedtls_ecdh_setup( ctx, key->grp.id ) ) != 0 )
446-
return( ret );
455+
if( mbedtls_ecdh_grp_id( ctx ) == MBEDTLS_ECP_DP_NONE )
456+
{
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. */
467+
if( mbedtls_ecdh_grp_id( ctx ) != key->grp.id )
468+
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
469+
}
447470

448471
#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
449472
return( ecdh_get_params_internal( ctx, key, side ) );

library/version_features.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,9 @@ static const char *features[] = {
351351
#if defined(MBEDTLS_ECP_RESTARTABLE)
352352
"MBEDTLS_ECP_RESTARTABLE",
353353
#endif /* MBEDTLS_ECP_RESTARTABLE */
354+
#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
355+
"MBEDTLS_ECDH_LEGACY_CONTEXT",
356+
#endif /* MBEDTLS_ECDH_LEGACY_CONTEXT */
354357
#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
355358
"MBEDTLS_ECDSA_DETERMINISTIC",
356359
#endif /* MBEDTLS_ECDSA_DETERMINISTIC */

programs/ssl/query_config.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,14 @@ int query_config( const char *config )
978978
}
979979
#endif /* MBEDTLS_ECP_RESTARTABLE */
980980

981+
#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
982+
if( strcmp( "MBEDTLS_ECDH_LEGACY_CONTEXT", config ) == 0 )
983+
{
984+
MACRO_EXPANSION_TO_STR( MBEDTLS_ECDH_LEGACY_CONTEXT );
985+
return( 0 );
986+
}
987+
#endif /* MBEDTLS_ECDH_LEGACY_CONTEXT */
988+
981989
#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
982990
if( strcmp( "MBEDTLS_ECDSA_DETERMINISTIC", config ) == 0 )
983991
{

tests/scripts/all.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,23 @@ component_test_rsa_no_crt () {
678678
if_build_succeeded tests/compat.sh -t RSA
679679
}
680680

681+
component_test_new_ecdh_context () {
682+
msg "build: new ECDH context (ASan build)" # ~ 6 min
683+
scripts/config.pl unset MBEDTLS_ECDH_LEGACY_CONTEXT
684+
CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan .
685+
make
686+
687+
msg "test: new ECDH context - main suites (inc. selftests) (ASan build)" # ~ 50s
688+
make test
689+
690+
msg "test: new ECDH context - ECDH-related part of ssl-opt.sh (ASan build)" # ~ 5s
691+
if_build_succeeded tests/ssl-opt.sh -f ECDH
692+
693+
msg "test: new ECDH context - compat.sh with some ECDH ciphersuites (ASan build)" # ~ 3 min
694+
# Exclude some symmetric ciphers that are redundant here to gain time.
695+
if_build_succeeded tests/compat.sh -f ECDH -V NO -e 'ARCFOUR\|ARIA\|CAMELLIA\|CHACHA\|DES\|RC4'
696+
}
697+
681698
component_test_small_ssl_out_content_len () {
682699
msg "build: small SSL_OUT_CONTENT_LEN (ASan build)"
683700
scripts/config.pl set MBEDTLS_SSL_IN_CONTENT_LEN 16384

tests/suites/test_suite_ecdh.data

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,19 @@ ecdh_restart:MBEDTLS_ECP_DP_SECP256R1:"C88F01F510D9AC3F70A292DAA2316DE544E9AAB8A
7979
ECDH exchange legacy context
8080
depends_on:MBEDTLS_ECP_DP_SECP192R1_ENABLED
8181
ecdh_exchange_legacy:MBEDTLS_ECP_DP_SECP192R1
82+
83+
ECDH calc_secret: ours first, SECP256R1 (RFC 5903)
84+
depends_on:MBEDTLS_ECP_DP_SECP256R1_ENABLED
85+
ecdh_exchange_calc_secret:MBEDTLS_ECP_DP_SECP256R1:"c6ef9c5d78ae012a011164acb397ce2088685d8f06bf9be0b283ab46476bee53":"04dad0b65394221cf9b051e1feca5787d098dfe637fc90b9ef945d0c37725811805271a0461cdb8252d61f1c456fa3e59ab1f45b33accf5f58389e0577b8990bb3":0:"d6840f6b42f6edafd13116e0e12565202fef8e9ece7dce03812464d04b9442de"
86+
87+
ECDH calc_secret: theirs first, SECP256R1 (RFC 5903)
88+
depends_on:MBEDTLS_ECP_DP_SECP256R1_ENABLED
89+
ecdh_exchange_calc_secret:MBEDTLS_ECP_DP_SECP256R1:"c6ef9c5d78ae012a011164acb397ce2088685d8f06bf9be0b283ab46476bee53":"04dad0b65394221cf9b051e1feca5787d098dfe637fc90b9ef945d0c37725811805271a0461cdb8252d61f1c456fa3e59ab1f45b33accf5f58389e0577b8990bb3":1:"d6840f6b42f6edafd13116e0e12565202fef8e9ece7dce03812464d04b9442de"
90+
91+
ECDH get_params with mismatched groups: our BP256R1, their SECP256R1
92+
depends_on:MBEDTLS_ECP_DP_SECP256R1_ENABLED:MBEDTLS_ECP_DP_BP256R1_ENABLED
93+
ecdh_exchange_get_params_fail:MBEDTLS_ECP_DP_BP256R1:"1234567812345678123456781234567812345678123456781234567812345678":MBEDTLS_ECP_DP_SECP256R1:"04dad0b65394221cf9b051e1feca5787d098dfe637fc90b9ef945d0c37725811805271a0461cdb8252d61f1c456fa3e59ab1f45b33accf5f58389e0577b8990bb3":0:MBEDTLS_ERR_ECP_BAD_INPUT_DATA
94+
95+
ECDH get_params with mismatched groups: their SECP256R1, our BP256R1
96+
depends_on:MBEDTLS_ECP_DP_SECP256R1_ENABLED:MBEDTLS_ECP_DP_BP256R1_ENABLED
97+
ecdh_exchange_get_params_fail:MBEDTLS_ECP_DP_BP256R1:"1234567812345678123456781234567812345678123456781234567812345678":MBEDTLS_ECP_DP_SECP256R1:"04dad0b65394221cf9b051e1feca5787d098dfe637fc90b9ef945d0c37725811805271a0461cdb8252d61f1c456fa3e59ab1f45b33accf5f58389e0577b8990bb3":1:MBEDTLS_ERR_ECP_BAD_INPUT_DATA

tests/suites/test_suite_ecdh.function

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,41 @@
11
/* BEGIN_HEADER */
22
#include "mbedtls/ecdh.h"
3+
4+
static int load_public_key( int grp_id, data_t *point,
5+
mbedtls_ecp_keypair *ecp )
6+
{
7+
int ok = 0;
8+
TEST_ASSERT( mbedtls_ecp_group_load( &ecp->grp, grp_id ) == 0 );
9+
TEST_ASSERT( mbedtls_ecp_point_read_binary( &ecp->grp,
10+
&ecp->Q,
11+
point->x,
12+
point->len ) == 0 );
13+
TEST_ASSERT( mbedtls_ecp_check_pubkey( &ecp->grp,
14+
&ecp->Q ) == 0 );
15+
ok = 1;
16+
exit:
17+
return( ok );
18+
}
19+
20+
static int load_private_key( int grp_id, data_t *private_key,
21+
mbedtls_ecp_keypair *ecp,
22+
rnd_pseudo_info *rnd_info )
23+
{
24+
int ok = 0;
25+
TEST_ASSERT( mbedtls_ecp_group_load( &ecp->grp, grp_id ) == 0 );
26+
TEST_ASSERT( mbedtls_mpi_read_binary( &ecp->d,
27+
private_key->x,
28+
private_key->len ) == 0 );
29+
TEST_ASSERT( mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) == 0 );
30+
/* Calculate the public key from the private key. */
31+
TEST_ASSERT( mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d,
32+
&ecp->grp.G,
33+
&rnd_pseudo_rand, rnd_info ) == 0 );
34+
ok = 1;
35+
exit:
36+
return( ok );
37+
}
38+
339
/* END_HEADER */
440

541
/* BEGIN_DEPENDENCIES
@@ -464,3 +500,107 @@ exit:
464500
mbedtls_ecdh_free( &cli );
465501
}
466502
/* END_CASE */
503+
504+
/* BEGIN_CASE */
505+
void ecdh_exchange_calc_secret( int grp_id,
506+
data_t *our_private_key,
507+
data_t *their_point,
508+
int ours_first,
509+
data_t *expected )
510+
{
511+
rnd_pseudo_info rnd_info;
512+
mbedtls_ecp_keypair our_key;
513+
mbedtls_ecp_keypair their_key;
514+
mbedtls_ecdh_context ecdh;
515+
unsigned char shared_secret[MBEDTLS_ECP_MAX_BYTES];
516+
size_t shared_secret_length = 0;
517+
518+
memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
519+
mbedtls_ecdh_init( &ecdh );
520+
mbedtls_ecp_keypair_init( &our_key );
521+
mbedtls_ecp_keypair_init( &their_key );
522+
523+
if( ! load_private_key( grp_id, our_private_key, &our_key, &rnd_info ) )
524+
goto exit;
525+
if( ! load_public_key( grp_id, their_point, &their_key ) )
526+
goto exit;
527+
528+
/* Import the keys to the ECDH calculation. */
529+
if( ours_first )
530+
{
531+
TEST_ASSERT( mbedtls_ecdh_get_params(
532+
&ecdh, &our_key, MBEDTLS_ECDH_OURS ) == 0 );
533+
TEST_ASSERT( mbedtls_ecdh_get_params(
534+
&ecdh, &their_key, MBEDTLS_ECDH_THEIRS ) == 0 );
535+
}
536+
else
537+
{
538+
TEST_ASSERT( mbedtls_ecdh_get_params(
539+
&ecdh, &their_key, MBEDTLS_ECDH_THEIRS ) == 0 );
540+
TEST_ASSERT( mbedtls_ecdh_get_params(
541+
&ecdh, &our_key, MBEDTLS_ECDH_OURS ) == 0 );
542+
}
543+
544+
/* Perform the ECDH calculation. */
545+
TEST_ASSERT( mbedtls_ecdh_calc_secret(
546+
&ecdh,
547+
&shared_secret_length,
548+
shared_secret, sizeof( shared_secret ),
549+
&rnd_pseudo_rand, &rnd_info ) == 0 );
550+
TEST_ASSERT( shared_secret_length == expected->len );
551+
TEST_ASSERT( memcmp( expected->x, shared_secret,
552+
shared_secret_length ) == 0 );
553+
554+
exit:
555+
mbedtls_ecdh_free( &ecdh );
556+
mbedtls_ecp_keypair_free( &our_key );
557+
mbedtls_ecp_keypair_free( &their_key );
558+
}
559+
/* END_CASE */
560+
561+
/* BEGIN_CASE */
562+
void ecdh_exchange_get_params_fail( int our_grp_id,
563+
data_t *our_private_key,
564+
int their_grp_id,
565+
data_t *their_point,
566+
int ours_first,
567+
int expected_ret )
568+
{
569+
rnd_pseudo_info rnd_info;
570+
mbedtls_ecp_keypair our_key;
571+
mbedtls_ecp_keypair their_key;
572+
mbedtls_ecdh_context ecdh;
573+
574+
memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
575+
mbedtls_ecdh_init( &ecdh );
576+
mbedtls_ecp_keypair_init( &our_key );
577+
mbedtls_ecp_keypair_init( &their_key );
578+
579+
if( ! load_private_key( our_grp_id, our_private_key, &our_key, &rnd_info ) )
580+
goto exit;
581+
if( ! load_public_key( their_grp_id, their_point, &their_key ) )
582+
goto exit;
583+
584+
if( ours_first )
585+
{
586+
TEST_ASSERT( mbedtls_ecdh_get_params(
587+
&ecdh, &our_key, MBEDTLS_ECDH_OURS ) == 0 );
588+
TEST_ASSERT( mbedtls_ecdh_get_params(
589+
&ecdh, &their_key, MBEDTLS_ECDH_THEIRS ) ==
590+
expected_ret );
591+
}
592+
else
593+
{
594+
TEST_ASSERT( mbedtls_ecdh_get_params(
595+
&ecdh, &their_key, MBEDTLS_ECDH_THEIRS ) == 0 );
596+
TEST_ASSERT( mbedtls_ecdh_get_params(
597+
&ecdh, &our_key, MBEDTLS_ECDH_OURS ) ==
598+
expected_ret );
599+
}
600+
601+
exit:
602+
mbedtls_ecdh_free( &ecdh );
603+
mbedtls_ecp_keypair_free( &our_key );
604+
mbedtls_ecp_keypair_free( &their_key );
605+
}
606+
/* END_CASE */

0 commit comments

Comments
 (0)