From 23983f30dea2b897cea92f95719bc3cabfcf5f83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Tue, 19 May 2020 12:38:31 +0200 Subject: [PATCH 01/23] Add config.h option MBEDTLS_ECP_NO_INTERNAL_RNG MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit No effect so far, except on dependency checking, as the feature it's meant to disable isn't implemented yet (so the descriptions in config.h and the ChangeLog entry are anticipation for now). Signed-off-by: Manuel Pégourié-Gonnard --- ChangeLog.d/ecp-internal-rng.txt | 5 +++++ include/mbedtls/check_config.h | 8 ++++++++ include/mbedtls/config.h | 22 ++++++++++++++++++++++ library/version_features.c | 3 +++ programs/ssl/query_config.c | 8 ++++++++ scripts/config.pl | 1 + tests/scripts/all.sh | 18 ++++++++++++++++++ 7 files changed, 65 insertions(+) create mode 100644 ChangeLog.d/ecp-internal-rng.txt diff --git a/ChangeLog.d/ecp-internal-rng.txt b/ChangeLog.d/ecp-internal-rng.txt new file mode 100644 index 000000000..bf11a7391 --- /dev/null +++ b/ChangeLog.d/ecp-internal-rng.txt @@ -0,0 +1,5 @@ +Changes + * The ECP module, enabled by `MBEDTLS_ECP_C`, now depends on + `MBEDTLS_CTR_DRBG_C` or `MBEDTLS_HMAC_DRBG_C` for some side-channel + coutermeasures. If side channels are not a concern, this dependency can + be avoided by enabling the new option `MBEDTLS_ECP_NO_INTERNAL_RNG`. diff --git a/include/mbedtls/check_config.h b/include/mbedtls/check_config.h index 93de091c4..0aef0d068 100644 --- a/include/mbedtls/check_config.h +++ b/include/mbedtls/check_config.h @@ -140,6 +140,14 @@ #error "MBEDTLS_ECP_C defined, but not all prerequisites" #endif +#if defined(MBEDTLS_ECP_C) && !( \ + defined(MBEDTLS_ECP_ALT) || \ + defined(MBEDTLS_CTR_DRBG_C) || \ + defined(MBEDTLS_HMAC_DRBG_C) || \ + defined(MBEDTLS_ECP_NO_INTERNAL_RNG)) +#error "MBEDTLS_ECP_C requires a DRBG module unless MBEDTLS_ECP_NO_INTERNAL_RNG is defined or an alternative implementation is used" +#endif + #if defined(MBEDTLS_PK_PARSE_C) && !defined(MBEDTLS_ASN1_PARSE_C) #error "MBEDTLS_PK_PARSE_C defined, but not all prerequesites" #endif diff --git a/include/mbedtls/config.h b/include/mbedtls/config.h index 834cced87..9442eb44b 100644 --- a/include/mbedtls/config.h +++ b/include/mbedtls/config.h @@ -780,6 +780,28 @@ */ #define MBEDTLS_ECP_NIST_OPTIM +/** + * \def MBEDTLS_ECP_NO_INTERNAL_RNG + * + * When this option is disabled, mbedtls_ecp_mul() will make use of an + * internal RNG when called with a NULL \c f_rng argument, in order to protect + * against some side-channel attacks. + * + * This protection introduces a dependency of the ECP module on one of the + * DRBG modules. For very constrained implementations that don't require this + * protection (for example, because you're only doing signature verification, + * so not manipulating any secret, or because local/physical side-channel + * attacks are outside your threat model), it might be desirable to get rid of + * that dependency. + * + * \warning Enabling this option makes some uses of ECP vulnerable to some + * side-channel attacks. Only enable it if you know that's not a problem for + * your use case. + * + * Uncomment this macro to disable some counter-measures in ECP. + */ +//#define MBEDTLS_ECP_NO_INTERNAL_RNG + /** * \def MBEDTLS_ECP_RESTARTABLE * diff --git a/library/version_features.c b/library/version_features.c index 3b67b2be8..6c9833ea9 100644 --- a/library/version_features.c +++ b/library/version_features.c @@ -351,6 +351,9 @@ static const char *features[] = { #if defined(MBEDTLS_ECP_NIST_OPTIM) "MBEDTLS_ECP_NIST_OPTIM", #endif /* MBEDTLS_ECP_NIST_OPTIM */ +#if defined(MBEDTLS_ECP_NO_INTERNAL_RNG) + "MBEDTLS_ECP_NO_INTERNAL_RNG", +#endif /* MBEDTLS_ECP_NO_INTERNAL_RNG */ #if defined(MBEDTLS_ECP_RESTARTABLE) "MBEDTLS_ECP_RESTARTABLE", #endif /* MBEDTLS_ECP_RESTARTABLE */ diff --git a/programs/ssl/query_config.c b/programs/ssl/query_config.c index 37e4141e9..976724e37 100644 --- a/programs/ssl/query_config.c +++ b/programs/ssl/query_config.c @@ -978,6 +978,14 @@ int query_config( const char *config ) } #endif /* MBEDTLS_ECP_NIST_OPTIM */ +#if defined(MBEDTLS_ECP_NO_INTERNAL_RNG) + if( strcmp( "MBEDTLS_ECP_NO_INTERNAL_RNG", config ) == 0 ) + { + MACRO_EXPANSION_TO_STR( MBEDTLS_ECP_NO_INTERNAL_RNG ); + return( 0 ); + } +#endif /* MBEDTLS_ECP_NO_INTERNAL_RNG */ + #if defined(MBEDTLS_ECP_RESTARTABLE) if( strcmp( "MBEDTLS_ECP_RESTARTABLE", config ) == 0 ) { diff --git a/scripts/config.pl b/scripts/config.pl index ba563d6eb..72aabf672 100755 --- a/scripts/config.pl +++ b/scripts/config.pl @@ -73,6 +73,7 @@ my @excluded = qw( MBEDTLS_CTR_DRBG_USE_128_BIT_KEY MBEDTLS_DEPRECATED_REMOVED MBEDTLS_DEPRECATED_WARNING +MBEDTLS_ECP_NO_INTERNAL_RNG MBEDTLS_HAVE_SSE2 MBEDTLS_MEMORY_BACKTRACE MBEDTLS_MEMORY_BUFFER_ALLOC_C diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index 634dfa80f..b16fd9a26 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -817,6 +817,24 @@ component_test_no_hmac_drbg () { # so there's little value in running those lengthy tests here. } +component_test_ecp_no_internal_rng () { + msg "build: Default plus ECP_NO_INTERNAL_RNG minus DRBG modules" + scripts/config.pl set MBEDTLS_ECP_NO_INTERNAL_RNG + scripts/config.pl unset MBEDTLS_CTR_DRBG_C + scripts/config.pl unset MBEDTLS_HMAC_DRBG_C + scripts/config.pl unset MBEDTLS_ECDSA_DETERMINISTIC # requires HMAC_DRBG + scripts/config.pl unset MBEDTLS_PSA_CRYPTO_C # requires a DRBG + scripts/config.pl unset MBEDTLS_PSA_CRYPTO_STORAGE_C # requires PSA Crypto + + CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan . + make + + msg "test: ECP_NO_INTERNAL_RNG, no DRBG module" + make test + + # no SSL tests as they all depend on having a DRBG +} + component_test_small_ssl_out_content_len () { msg "build: small SSL_OUT_CONTENT_LEN (ASan build)" scripts/config.pl set MBEDTLS_SSL_IN_CONTENT_LEN 16384 From fb11d252b216d5709288061ac8934da3f89c46d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Fri, 22 May 2020 12:12:36 +0200 Subject: [PATCH 02/23] Implement use of internal DRBG for ecp_mul() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The case of MBEDTLS_ECP_RESTARTABLE isn't handled correctly yet: in that case the DRBG instance should persist when resuming the operation. This will be addressed in the next commit. When both CTR_DRBG and HMAC_DRBG are available, CTR_DRBG is preferred since both are suitable but CTR_DRBG tends to be faster and I needed a tie-breaker. There are currently three possible cases to test: - NO_INTERNAL_RNG is set -> tested in test_ecp_no_internal_rng - it's unset and CTR_DRBG is available -> tested in the default config - it's unset and CTR_DRBG is disabled -> tested in test_ecp_internal_rng_no_ctr_drbg Signed-off-by: Manuel Pégourié-Gonnard --- include/mbedtls/md.h | 2 + library/ecp.c | 137 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+) diff --git a/include/mbedtls/md.h b/include/mbedtls/md.h index 8bcf766a6..adf06a4c5 100644 --- a/include/mbedtls/md.h +++ b/include/mbedtls/md.h @@ -98,6 +98,8 @@ typedef struct mbedtls_md_context_t * \brief This function returns the list of digests supported by the * generic digest module. * + * \note The list starts with the strongest available hashes. + * * \return A statically allocated array of digests. Each element * in the returned list is an integer belonging to the * message-digest enumeration #mbedtls_md_type_t. diff --git a/library/ecp.c b/library/ecp.c index 0357cde20..725ecc65f 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -104,6 +104,16 @@ #include "mbedtls/ecp_internal.h" +#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG) +#if defined(MBEDTLS_CTR_DRBG_C) +#include "mbedtls/ctr_drbg.h" +#elif defined(MBEDTLS_HMAC_DRBG_C) +#include "mbedtls/hmac_drbg.h" +#else +#error "Invalid configuration detected. Include check_config.h to ensure that the configuration is valid." +#endif +#endif /* MBEDTLS_ECP_NO_INTERNAL_RNG */ + #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \ !defined(inline) && !defined(__cplusplus) #define inline __inline @@ -117,6 +127,113 @@ static unsigned long add_count, dbl_count, mul_count; #endif +#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG) +/* + * Currently ecp_mul() takes a RNG function as an argument, used for + * side-channel protection, but it can be NULL. The initial reasonning was + * that people will pass non-NULL RNG when they care about side-channels, but + * unfortunately we have some APIs that call ecp_mul() with a NULL RNG, with + * no opportunity for the user to do anything about it. + * + * The obvious strategies for addressing that include: + * - change those APIs so that they take RNG arguments; + * - require a global RNG to be available to all crypto modules. + * + * Unfortunately those would break compatibility. So what we do instead is + * have our own internal DRBG instance, seeded from the secret scalar. + * + * The following is a light-weight abstraction layer for doing that with + * CTR_DRBG or HMAC_DRBG. + */ + +#if defined(MBEDTLS_CTR_DRBG_C) +/* DRBG context type */ +typedef mbedtls_ctr_drbg_context ecp_drbg_context; + +/* DRBG context init */ +static inline void ecp_drbg_init( ecp_drbg_context *ctx ) +{ + mbedtls_ctr_drbg_init( ctx ); +} + +/* DRBG context free */ +static inline void ecp_drbg_free( ecp_drbg_context *ctx ) +{ + mbedtls_ctr_drbg_free( ctx ); +} + +/* DRBG function */ +static inline int ecp_drbg_random( void *p_rng, + unsigned char *output, size_t output_len ) +{ + return( mbedtls_ctr_drbg_random( p_rng, output, output_len ) ); +} + +/* + * Since CTR_DRBG doesn't have a seed_buf() function the way HMAC_DRBG does, + * we need to pass an entropy function when seeding. So we use a dummy + * function for that, and pass the actual entropy as customisation string. + * (During seeding of CTR_DRBG the entropy input and customisation string are + * concatenated before being used to update the secret state.) + */ +static int ecp_ctr_drbg_null_entropy(void *ctx, unsigned char *out, size_t len) +{ + (void) ctx; + memset( out, 0, len ); + return( 0 ); +} + +/* DRBG context seeding */ +static int ecp_drbg_seed( ecp_drbg_context *ctx, const mbedtls_mpi *secret ) +{ + const unsigned char *secret_p = (const unsigned char *) secret->p; + const size_t secret_size = secret->n * sizeof( mbedtls_mpi_uint ); + + return( mbedtls_ctr_drbg_seed( ctx, ecp_ctr_drbg_null_entropy, NULL, + secret_p, secret_size ) ); +} + +#elif defined(MBEDTLS_HMAC_DRBG_C) +/* DRBG context type */ +typedef mbedtls_hmac_drbg_context ecp_drbg_context; + +/* DRBG context init */ +static inline void ecp_drbg_init( ecp_drbg_context *ctx ) +{ + mbedtls_hmac_drbg_init( ctx ); +} + +/* DRBG context free */ +static inline void ecp_drbg_free( ecp_drbg_context *ctx ) +{ + mbedtls_hmac_drbg_free( ctx ); +} + +/* DRBG function */ +static inline int ecp_drbg_random( void *p_rng, + unsigned char *output, size_t output_len ) +{ + return( mbedtls_hmac_drbg_random( p_rng, output, output_len ) ); +} + +/* DRBG context seeding */ +static int ecp_drbg_seed( ecp_drbg_context *ctx, const mbedtls_mpi *secret ) +{ + const unsigned char *secret_p = (const unsigned char *) secret->p; + const size_t secret_size = secret->n * sizeof( mbedtls_mpi_uint ); + + /* The list starts with strong hashes */ + const mbedtls_md_type_t md_type = mbedtls_md_list()[0]; + const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_type ); + + return( mbedtls_hmac_drbg_seed_buf( ctx, md_info, secret_p, secret_size ) ); +} + +#else +#error "Invalid configuration detected. Include check_config.h to ensure that the configuration is valid." +#endif /* DRBG modules */ +#endif /* MBEDTLS_ECP_NO_INTERNAL_RNG */ + #if defined(MBEDTLS_ECP_RESTARTABLE) /* * Maximum number of "basic operations" to be done in a row. @@ -2363,12 +2480,19 @@ int mbedtls_ecp_mul_restartable( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA; #if defined(MBEDTLS_ECP_INTERNAL_ALT) char is_grp_capable = 0; +#endif +#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG) + ecp_drbg_context drbg_ctx; #endif ECP_VALIDATE_RET( grp != NULL ); ECP_VALIDATE_RET( R != NULL ); ECP_VALIDATE_RET( m != NULL ); ECP_VALIDATE_RET( P != NULL ); +#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG) + ecp_drbg_init( &drbg_ctx ); +#endif /* !MBEDTLS_ECP_NO_INTERNAL_RNG */ + #if defined(MBEDTLS_ECP_RESTARTABLE) /* reset ops count for this call if top-level */ if( rs_ctx != NULL && rs_ctx->depth++ == 0 ) @@ -2380,6 +2504,15 @@ int mbedtls_ecp_mul_restartable( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, MBEDTLS_MPI_CHK( mbedtls_internal_ecp_init( grp ) ); #endif /* MBEDTLS_ECP_INTERNAL_ALT */ +#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG) + if( f_rng == NULL ) + { + MBEDTLS_MPI_CHK( ecp_drbg_seed( &drbg_ctx, m ) ); + f_rng = &ecp_drbg_random; + p_rng = &drbg_ctx; + } +#endif /* !MBEDTLS_ECP_NO_INTERNAL_RNG */ + #if defined(MBEDTLS_ECP_RESTARTABLE) /* skip argument check when restarting */ if( rs_ctx == NULL || rs_ctx->rsm == NULL ) @@ -2410,6 +2543,10 @@ cleanup: mbedtls_internal_ecp_free( grp ); #endif /* MBEDTLS_ECP_INTERNAL_ALT */ +#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG) + ecp_drbg_free( &drbg_ctx ); +#endif + #if defined(MBEDTLS_ECP_RESTARTABLE) if( rs_ctx != NULL ) rs_ctx->depth--; From d18f0519a5ae92b06036f28815502a2d05f6e270 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Wed, 3 Jun 2020 12:11:56 +0200 Subject: [PATCH 03/23] Move internal drbg init to specific mul functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit While it seems cleaner and more convenient to set it in the top-level mbedtls_ecp_mul() function, the existence of the restartable option changes things - when it's enabled the drbg context needs to be saved in the restart context (more precisely in the restart_mul sub-context), which can only be done when it's allocated, which is in the curve-specific mul function. This commit only internal drbg management from mbedtls_ecp_mul() to ecp_mul_mxz() and ecp_mul_comb(), without modifying behaviour (even internal), and a future commit will modify the ecp_mul_comb() version to handle restart properly. Signed-off-by: Manuel Pégourié-Gonnard --- library/ecp.c | 59 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 22 deletions(-) diff --git a/library/ecp.c b/library/ecp.c index 725ecc65f..b120adf74 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -2141,11 +2141,25 @@ static int ecp_mul_comb( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, int ret; unsigned char w, p_eq_g, i; size_t d; - unsigned char T_size, T_ok; - mbedtls_ecp_point *T; + unsigned char T_size = 0, T_ok = 0; + mbedtls_ecp_point *T = NULL; +#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG) + ecp_drbg_context drbg_ctx; + + ecp_drbg_init( &drbg_ctx ); +#endif ECP_RS_ENTER( rsm ); +#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG) + if( f_rng == NULL ) + { + MBEDTLS_MPI_CHK( ecp_drbg_seed( &drbg_ctx, m ) ); + f_rng = &ecp_drbg_random; + p_rng = &drbg_ctx; + } +#endif /* !MBEDTLS_ECP_NO_INTERNAL_RNG */ + /* Is P the base point ? */ #if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1 p_eq_g = ( mbedtls_mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 && @@ -2217,6 +2231,10 @@ static int ecp_mul_comb( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, cleanup: +#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG) + ecp_drbg_free( &drbg_ctx ); +#endif + /* does T belong to the group? */ if( T == grp->T ) T = NULL; @@ -2407,9 +2425,22 @@ static int ecp_mul_mxz( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, unsigned char b; mbedtls_ecp_point RP; mbedtls_mpi PX; +#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG) + ecp_drbg_context drbg_ctx; + ecp_drbg_init( &drbg_ctx ); +#endif mbedtls_ecp_point_init( &RP ); mbedtls_mpi_init( &PX ); +#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG) + if( f_rng == NULL ) + { + MBEDTLS_MPI_CHK( ecp_drbg_seed( &drbg_ctx, m ) ); + f_rng = &ecp_drbg_random; + p_rng = &drbg_ctx; + } +#endif /* !MBEDTLS_ECP_NO_INTERNAL_RNG */ + /* Save PX and read from P before writing to R, in case P == R */ MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &PX, &P->X ) ); MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &RP, P ) ); @@ -2462,6 +2493,10 @@ static int ecp_mul_mxz( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, MBEDTLS_MPI_CHK( ecp_normalize_mxz( grp, R ) ); cleanup: +#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG) + ecp_drbg_free( &drbg_ctx ); +#endif + mbedtls_ecp_point_free( &RP ); mbedtls_mpi_free( &PX ); return( ret ); @@ -2480,19 +2515,12 @@ int mbedtls_ecp_mul_restartable( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA; #if defined(MBEDTLS_ECP_INTERNAL_ALT) char is_grp_capable = 0; -#endif -#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG) - ecp_drbg_context drbg_ctx; #endif ECP_VALIDATE_RET( grp != NULL ); ECP_VALIDATE_RET( R != NULL ); ECP_VALIDATE_RET( m != NULL ); ECP_VALIDATE_RET( P != NULL ); -#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG) - ecp_drbg_init( &drbg_ctx ); -#endif /* !MBEDTLS_ECP_NO_INTERNAL_RNG */ - #if defined(MBEDTLS_ECP_RESTARTABLE) /* reset ops count for this call if top-level */ if( rs_ctx != NULL && rs_ctx->depth++ == 0 ) @@ -2504,15 +2532,6 @@ int mbedtls_ecp_mul_restartable( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, MBEDTLS_MPI_CHK( mbedtls_internal_ecp_init( grp ) ); #endif /* MBEDTLS_ECP_INTERNAL_ALT */ -#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG) - if( f_rng == NULL ) - { - MBEDTLS_MPI_CHK( ecp_drbg_seed( &drbg_ctx, m ) ); - f_rng = &ecp_drbg_random; - p_rng = &drbg_ctx; - } -#endif /* !MBEDTLS_ECP_NO_INTERNAL_RNG */ - #if defined(MBEDTLS_ECP_RESTARTABLE) /* skip argument check when restarting */ if( rs_ctx == NULL || rs_ctx->rsm == NULL ) @@ -2543,10 +2562,6 @@ cleanup: mbedtls_internal_ecp_free( grp ); #endif /* MBEDTLS_ECP_INTERNAL_ALT */ -#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG) - ecp_drbg_free( &drbg_ctx ); -#endif - #if defined(MBEDTLS_ECP_RESTARTABLE) if( rs_ctx != NULL ) rs_ctx->depth--; From 047986c2f8d8227aebb143054250c36d26ac2c3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Thu, 4 Jun 2020 09:43:14 +0200 Subject: [PATCH 04/23] Add support for RESTARTABLE with internal RNG MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently we draw pseudo-random numbers at the beginning and end of the main loop. With ECP_RESTARTABLE, it's possible that between those two occasions we returned from the multiplication function, hence lost our internal DRBG context that lives in this function's stack frame. This would result in the same pseudo-random numbers being used for blinding in multiple places. While it's not immediately clear that this would give rise to an attack, it's also absolutely not clear that it doesn't. So let's avoid that by using a DRBG context that lives inside the restart context and persists across return/resume cycles. That way the RESTARTABLE case uses exactly the same pseudo-random numbers as the non-restartable case. Testing and compile-time options: - The case ECP_RESTARTABLE && !ECP_NO_INTERNAL_RNG is already tested by component_test_no_use_psa_crypto_full_cmake_asan. - The case ECP_RESTARTABLE && ECP_NO_INTERNAL_RNG didn't have a pre-existing test so a component is added. Testing and runtime options: when ECP_RESTARTABLE is enabled, the test suites already contain cases where restart happens and cases where it doesn't (because the operation is short enough or because restart is disabled (NULL restart context)). Signed-off-by: Manuel Pégourié-Gonnard --- library/ecp.c | 34 ++++++++++++++++++++++++++++++++-- tests/scripts/all.sh | 19 +++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/library/ecp.c b/library/ecp.c index b120adf74..8e87e60c3 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -281,6 +281,10 @@ struct mbedtls_ecp_restart_mul ecp_rsm_comb_core, /* ecp_mul_comb_core() */ ecp_rsm_final_norm, /* do the final normalization */ } state; +#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG) + ecp_drbg_context drbg_ctx; + unsigned char drbg_seeded; +#endif }; /* @@ -293,6 +297,10 @@ static void ecp_restart_rsm_init( mbedtls_ecp_restart_mul_ctx *ctx ) ctx->T = NULL; ctx->T_size = 0; ctx->state = ecp_rsm_init; +#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG) + ecp_drbg_init( &ctx->drbg_ctx ); + ctx->drbg_seeded = 0; +#endif } /* @@ -314,6 +322,10 @@ static void ecp_restart_rsm_free( mbedtls_ecp_restart_mul_ctx *ctx ) mbedtls_free( ctx->T ); } +#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG) + ecp_drbg_free( &ctx->drbg_ctx ); +#endif + ecp_restart_rsm_init( ctx ); } @@ -2154,9 +2166,27 @@ static int ecp_mul_comb( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, #if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG) if( f_rng == NULL ) { - MBEDTLS_MPI_CHK( ecp_drbg_seed( &drbg_ctx, m ) ); + /* Adjust pointers */ f_rng = &ecp_drbg_random; - p_rng = &drbg_ctx; +#if defined(MBEDTLS_ECP_RESTARTABLE) + if( rs_ctx != NULL && rs_ctx->rsm != NULL ) + p_rng = &rs_ctx->rsm->drbg_ctx; + else +#endif + p_rng = &drbg_ctx; + + /* Initialize internal DRBG if necessary */ +#if defined(MBEDTLS_ECP_RESTARTABLE) + if( rs_ctx == NULL || rs_ctx->rsm == NULL || + rs_ctx->rsm->drbg_seeded == 0 ) +#endif + { + MBEDTLS_MPI_CHK( ecp_drbg_seed( p_rng, m ) ); + } +#if defined(MBEDTLS_ECP_RESTARTABLE) + if( rs_ctx != NULL && rs_ctx->rsm != NULL ) + rs_ctx->rsm->drbg_seeded = 1; +#endif } #endif /* !MBEDTLS_ECP_NO_INTERNAL_RNG */ diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index b16fd9a26..039ae158c 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -835,6 +835,25 @@ component_test_ecp_no_internal_rng () { # no SSL tests as they all depend on having a DRBG } +component_test_ecp_restartable_no_internal_rng () { + msg "build: Default plus ECP_RESTARTABLE and ECP_NO_INTERNAL_RNG, no DRBG" + scripts/config.pl set MBEDTLS_ECP_NO_INTERNAL_RNG + scripts/config.pl set MBEDTLS_ECP_RESTARTABLE + scripts/config.pl unset MBEDTLS_CTR_DRBG_C + scripts/config.pl unset MBEDTLS_HMAC_DRBG_C + scripts/config.pl unset MBEDTLS_ECDSA_DETERMINISTIC # requires HMAC_DRBG + scripts/config.pl unset MBEDTLS_PSA_CRYPTO_C # requires CTR_DRBG + scripts/config.pl unset MBEDTLS_PSA_CRYPTO_STORAGE_C # requires PSA Crypto + + CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan . + make + + msg "test: ECP_RESTARTABLE and ECP_NO_INTERNAL_RNG, no DRBG module" + make test + + # no SSL tests as they all depend on having a DRBG +} + component_test_small_ssl_out_content_len () { msg "build: small SSL_OUT_CONTENT_LEN (ASan build)" scripts/config.pl set MBEDTLS_SSL_IN_CONTENT_LEN 16384 From b34aeeb8d64ad7f21962954c4616ffa942304b16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Thu, 4 Jun 2020 10:20:12 +0200 Subject: [PATCH 05/23] Update documentation about optional f_rng parameter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Manuel Pégourié-Gonnard --- include/mbedtls/ecp.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/include/mbedtls/ecp.h b/include/mbedtls/ecp.h index 065a4cc0b..f2e591693 100644 --- a/include/mbedtls/ecp.h +++ b/include/mbedtls/ecp.h @@ -813,6 +813,9 @@ int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, * intermediate results to prevent potential timing attacks * targeting these results. We recommend always providing * a non-NULL \p f_rng. The overhead is negligible. + * Note: unless #MBEDTLS_ECP_NO_INTERNAL_RNG is defined, when + * \p f_rng is NULL, an internal RNG (seeded from the value + * of \p m) will be used instead. * * \param grp The ECP group to use. * This must be initialized and have group parameters From 84fd65724027a360207d3c294cc5af35a071ade0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Thu, 4 Jun 2020 10:31:06 +0200 Subject: [PATCH 06/23] Add Security ChangeLog entry for lack of blinding MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Manuel Pégourié-Gonnard --- ChangeLog.d/ecp-internal-rng.txt | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ChangeLog.d/ecp-internal-rng.txt b/ChangeLog.d/ecp-internal-rng.txt index bf11a7391..c0419acad 100644 --- a/ChangeLog.d/ecp-internal-rng.txt +++ b/ChangeLog.d/ecp-internal-rng.txt @@ -3,3 +3,13 @@ Changes `MBEDTLS_CTR_DRBG_C` or `MBEDTLS_HMAC_DRBG_C` for some side-channel coutermeasures. If side channels are not a concern, this dependency can be avoided by enabling the new option `MBEDTLS_ECP_NO_INTERNAL_RNG`. + +Security + * Fix side channel in mbedtls_ecp_check_pub_priv() and + mbedtls_pk_parse_key() / mbedtls_pk_parse_keyfile() (when loading a + private key that didn't include the uncompressed public key), as well as + mbedtls_ecp_mul() / mbedtls_ecp_mul_restartable() when called with a NULL + f_rng argument. An attacker with access to precise enough timing and + memory access information (typically an untrusted operating system + attacking a secure enclave) could fully recover the ECC private key. + Found and reported by Alejandro Cabrera Aldaya and Billy Brumley. From c334f41bf9175c2089aba83157093572c0304a3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Thu, 4 Jun 2020 10:43:29 +0200 Subject: [PATCH 07/23] Skip redundant checks for NULL f_rng MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Unless MBEDTLS_ECP_NO_INTERNAL_RNG is defined, it's no longer possible for f_rng to be NULL at the places that randomize coordinates. Eliminate the NULL check in this case: - it makes it clearer to reviewers that randomization always happens (unless the user opted out at compile time) - a NULL check in a place where it's easy to prove the value is never NULL might upset or confuse static analyzers (including humans) - removing the check saves a bit of code size Signed-off-by: Manuel Pégourié-Gonnard --- library/ecp.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/library/ecp.c b/library/ecp.c index 8e87e60c3..67edf4994 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -1948,7 +1948,9 @@ static int ecp_mul_comb_core( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R i = d; MBEDTLS_MPI_CHK( ecp_select_comb( grp, R, T, T_size, x[i] ) ); MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->Z, 1 ) ); +#if defined(MBEDTLS_ECP_NO_INTERNAL_RNG) if( f_rng != 0 ) +#endif MBEDTLS_MPI_CHK( ecp_randomize_jac( grp, R, f_rng, p_rng ) ); } @@ -2081,7 +2083,9 @@ final_norm: * * Avoid the leak by randomizing coordinates before we normalize them. */ +#if defined(MBEDTLS_ECP_NO_INTERNAL_RNG) if( f_rng != 0 ) +#endif MBEDTLS_MPI_CHK( ecp_randomize_jac( grp, RR, f_rng, p_rng ) ); MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV ); @@ -2484,7 +2488,9 @@ static int ecp_mul_mxz( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, MOD_ADD( RP.X ); /* Randomize coordinates of the starting point */ +#if defined(MBEDTLS_ECP_NO_INTERNAL_RNG) if( f_rng != NULL ) +#endif MBEDTLS_MPI_CHK( ecp_randomize_mxz( grp, &RP, f_rng, p_rng ) ); /* Loop invariant: R = result so far, RP = R + P */ @@ -2517,7 +2523,9 @@ static int ecp_mul_mxz( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, * * Avoid the leak by randomizing coordinates before we normalize them. */ +#if defined(MBEDTLS_ECP_NO_INTERNAL_RNG) if( f_rng != NULL ) +#endif MBEDTLS_MPI_CHK( ecp_randomize_mxz( grp, R, f_rng, p_rng ) ); MBEDTLS_MPI_CHK( ecp_normalize_mxz( grp, R ) ); From c7295f5416d4c5c52b736ea183f7f9c0038d2054 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Thu, 4 Jun 2020 12:32:14 +0200 Subject: [PATCH 08/23] Use HMAC_DRBG by default for ECP internal DRBG MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It results in smaller code than using CTR_DRBG (64 bytes smaller on ARMv6-M with arm-none-eabi-gcc 7.3.1), so let's use this by default when both are available. Signed-off-by: Manuel Pégourié-Gonnard --- library/ecp.c | 84 ++++++++++++++++++++++++++------------------------- 1 file changed, 43 insertions(+), 41 deletions(-) diff --git a/library/ecp.c b/library/ecp.c index 67edf4994..9b6efe5d3 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -105,10 +105,10 @@ #include "mbedtls/ecp_internal.h" #if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG) -#if defined(MBEDTLS_CTR_DRBG_C) -#include "mbedtls/ctr_drbg.h" -#elif defined(MBEDTLS_HMAC_DRBG_C) +#if defined(MBEDTLS_HMAC_DRBG_C) #include "mbedtls/hmac_drbg.h" +#elif defined(MBEDTLS_CTR_DRBG_C) +#include "mbedtls/ctr_drbg.h" #else #error "Invalid configuration detected. Include check_config.h to ensure that the configuration is valid." #endif @@ -143,10 +143,48 @@ static unsigned long add_count, dbl_count, mul_count; * have our own internal DRBG instance, seeded from the secret scalar. * * The following is a light-weight abstraction layer for doing that with - * CTR_DRBG or HMAC_DRBG. + * HMAC_DRBG (first choice) or CTR_DRBG. */ -#if defined(MBEDTLS_CTR_DRBG_C) +#if defined(MBEDTLS_HMAC_DRBG_C) + +/* DRBG context type */ +typedef mbedtls_hmac_drbg_context ecp_drbg_context; + +/* DRBG context init */ +static inline void ecp_drbg_init( ecp_drbg_context *ctx ) +{ + mbedtls_hmac_drbg_init( ctx ); +} + +/* DRBG context free */ +static inline void ecp_drbg_free( ecp_drbg_context *ctx ) +{ + mbedtls_hmac_drbg_free( ctx ); +} + +/* DRBG function */ +static inline int ecp_drbg_random( void *p_rng, + unsigned char *output, size_t output_len ) +{ + return( mbedtls_hmac_drbg_random( p_rng, output, output_len ) ); +} + +/* DRBG context seeding */ +static int ecp_drbg_seed( ecp_drbg_context *ctx, const mbedtls_mpi *secret ) +{ + const unsigned char *secret_p = (const unsigned char *) secret->p; + const size_t secret_size = secret->n * sizeof( mbedtls_mpi_uint ); + + /* The list starts with strong hashes */ + const mbedtls_md_type_t md_type = mbedtls_md_list()[0]; + const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_type ); + + return( mbedtls_hmac_drbg_seed_buf( ctx, md_info, secret_p, secret_size ) ); +} + +#elif defined(MBEDTLS_CTR_DRBG_C) + /* DRBG context type */ typedef mbedtls_ctr_drbg_context ecp_drbg_context; @@ -193,42 +231,6 @@ static int ecp_drbg_seed( ecp_drbg_context *ctx, const mbedtls_mpi *secret ) secret_p, secret_size ) ); } -#elif defined(MBEDTLS_HMAC_DRBG_C) -/* DRBG context type */ -typedef mbedtls_hmac_drbg_context ecp_drbg_context; - -/* DRBG context init */ -static inline void ecp_drbg_init( ecp_drbg_context *ctx ) -{ - mbedtls_hmac_drbg_init( ctx ); -} - -/* DRBG context free */ -static inline void ecp_drbg_free( ecp_drbg_context *ctx ) -{ - mbedtls_hmac_drbg_free( ctx ); -} - -/* DRBG function */ -static inline int ecp_drbg_random( void *p_rng, - unsigned char *output, size_t output_len ) -{ - return( mbedtls_hmac_drbg_random( p_rng, output, output_len ) ); -} - -/* DRBG context seeding */ -static int ecp_drbg_seed( ecp_drbg_context *ctx, const mbedtls_mpi *secret ) -{ - const unsigned char *secret_p = (const unsigned char *) secret->p; - const size_t secret_size = secret->n * sizeof( mbedtls_mpi_uint ); - - /* The list starts with strong hashes */ - const mbedtls_md_type_t md_type = mbedtls_md_list()[0]; - const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_type ); - - return( mbedtls_hmac_drbg_seed_buf( ctx, md_info, secret_p, secret_size ) ); -} - #else #error "Invalid configuration detected. Include check_config.h to ensure that the configuration is valid." #endif /* DRBG modules */ From 18b0b3c4b56ee7b81838b5f2dc4a7f1655de0cdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Mon, 8 Jun 2020 09:53:20 +0200 Subject: [PATCH 09/23] Avoid superflous randomization with restartable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Checking the budget only after the randomization is done means sometimes we were randomizing first, then noticing we ran out of budget, return, come back and randomize again before we finally normalize. While this is fine from a correctness and security perspective, it's a minor inefficiency, and can also be disconcerting while debugging, so we might as well avoid it. Signed-off-by: Manuel Pégourié-Gonnard --- library/ecp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/ecp.c b/library/ecp.c index 9b6efe5d3..67f399f1e 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -2073,6 +2073,7 @@ static int ecp_mul_comb_after_precomp( const mbedtls_ecp_group *grp, rs_ctx->rsm->state = ecp_rsm_final_norm; final_norm: + MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV ); #endif /* * Knowledge of the jacobian coordinates may leak the last few bits of the @@ -2090,7 +2091,6 @@ final_norm: #endif MBEDTLS_MPI_CHK( ecp_randomize_jac( grp, RR, f_rng, p_rng ) ); - MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV ); MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, RR ) ); #if defined(MBEDTLS_ECP_RESTARTABLE) From 0defc579d7788c1e1fa5a650d81d5cf71597a597 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Wed, 10 Jun 2020 09:18:25 +0200 Subject: [PATCH 10/23] Fix typo in a comment Co-authored-by: Janos Follath --- library/ecp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/ecp.c b/library/ecp.c index 67f399f1e..82c931f5e 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -130,7 +130,7 @@ static unsigned long add_count, dbl_count, mul_count; #if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG) /* * Currently ecp_mul() takes a RNG function as an argument, used for - * side-channel protection, but it can be NULL. The initial reasonning was + * side-channel protection, but it can be NULL. The initial reasoning was * that people will pass non-NULL RNG when they care about side-channels, but * unfortunately we have some APIs that call ecp_mul() with a NULL RNG, with * no opportunity for the user to do anything about it. From 72177e362b4298bf9349861c430dd6d44c1a5ebb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Tue, 16 Jun 2020 12:51:42 +0200 Subject: [PATCH 11/23] Add fall-back to hash-based KDF for internal ECP DRBG MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The dependency on a DRBG module was perhaps a bit strict for LTS branches, so let's have an option that works with no DRBG when at least one SHA module is present. This changes the internal API of ecp_drbg_seed() by adding the size of the MPI as a parameter. Re-computing the size from the number of limbs doesn't work too well here as we're writing out to a fixed-size buffer and for some curves (P-521) that would round up too much. Using mbedtls_mpi_get_len() is not entirely satisfactory either as it would mean using a variable-length encoding, with could open side channels. Signed-off-by: Manuel Pégourié-Gonnard --- include/mbedtls/check_config.h | 5 +- library/ecp.c | 224 +++++++++++++++++++++++++++++++-- tests/scripts/all.sh | 63 ++++++++++ 3 files changed, 281 insertions(+), 11 deletions(-) diff --git a/include/mbedtls/check_config.h b/include/mbedtls/check_config.h index 0aef0d068..3ae86a19b 100644 --- a/include/mbedtls/check_config.h +++ b/include/mbedtls/check_config.h @@ -144,8 +144,11 @@ defined(MBEDTLS_ECP_ALT) || \ defined(MBEDTLS_CTR_DRBG_C) || \ defined(MBEDTLS_HMAC_DRBG_C) || \ + defined(MBEDTLS_SHA512_C) || \ + defined(MBEDTLS_SHA256_C) || \ + defined(MBEDTLS_SHA1_C) || \ defined(MBEDTLS_ECP_NO_INTERNAL_RNG)) -#error "MBEDTLS_ECP_C requires a DRBG module unless MBEDTLS_ECP_NO_INTERNAL_RNG is defined or an alternative implementation is used" +#error "MBEDTLS_ECP_C requires a DRBG or SHA module unless MBEDTLS_ECP_NO_INTERNAL_RNG is defined or an alternative implementation is used" #endif #if defined(MBEDTLS_PK_PARSE_C) && !defined(MBEDTLS_ASN1_PARSE_C) diff --git a/library/ecp.c b/library/ecp.c index 82c931f5e..b8ad9357a 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -109,6 +109,12 @@ #include "mbedtls/hmac_drbg.h" #elif defined(MBEDTLS_CTR_DRBG_C) #include "mbedtls/ctr_drbg.h" +#elif defined(MBEDTLS_SHA512_C) +#include "mbedtls/sha512.h" +#elif defined(MBEDTLS_SHA256_C) +#include "mbedtls/sha256.h" +#elif defined(MBEDTLS_SHA1_C) +#include "mbedtls/sha1.h" #else #error "Invalid configuration detected. Include check_config.h to ensure that the configuration is valid." #endif @@ -171,16 +177,16 @@ static inline int ecp_drbg_random( void *p_rng, } /* DRBG context seeding */ -static int ecp_drbg_seed( ecp_drbg_context *ctx, const mbedtls_mpi *secret ) +static int ecp_drbg_seed( ecp_drbg_context *ctx, + const mbedtls_mpi *secret, size_t secret_len ) { const unsigned char *secret_p = (const unsigned char *) secret->p; - const size_t secret_size = secret->n * sizeof( mbedtls_mpi_uint ); /* The list starts with strong hashes */ const mbedtls_md_type_t md_type = mbedtls_md_list()[0]; const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_type ); - return( mbedtls_hmac_drbg_seed_buf( ctx, md_info, secret_p, secret_size ) ); + return( mbedtls_hmac_drbg_seed_buf( ctx, md_info, secret_p, secret_len ) ); } #elif defined(MBEDTLS_CTR_DRBG_C) @@ -222,18 +228,113 @@ static int ecp_ctr_drbg_null_entropy(void *ctx, unsigned char *out, size_t len) } /* DRBG context seeding */ -static int ecp_drbg_seed( ecp_drbg_context *ctx, const mbedtls_mpi *secret ) +static int ecp_drbg_seed( ecp_drbg_context *ctx, + const mbedtls_mpi *secret, size_t secret_len ) { const unsigned char *secret_p = (const unsigned char *) secret->p; - const size_t secret_size = secret->n * sizeof( mbedtls_mpi_uint ); return( mbedtls_ctr_drbg_seed( ctx, ecp_ctr_drbg_null_entropy, NULL, - secret_p, secret_size ) ); + secret_p, secret_len ) ); } -#else +#elif defined(MBEDTLS_SHA512_C) || \ + defined(MBEDTLS_SHA256_C) || \ + defined(MBEDTLS_SHA1_C) + +/* This will be used in the self-test function */ +#define ECP_ONE_STEP_KDF + +/* + * We need to expand secret data (the scalar) into a longer stream of bytes. + * + * We'll use the One-Step KDF from NIST SP 800-56C, with option 1 (H is a hash + * function) and empty FixedInfo. (Though we'll make it fit the DRBG API for + * convenience, this is not a full-fledged DRBG, but we don't need one here.) + * + * We need a basic hash abstraction layer to use whatever SHA is available. + */ +#if defined(MBEDTLS_SHA512_C) + +#define HASH_FUNC( in, ilen, out ) mbedtls_sha512_ret( in, ilen, out, 0 ); +#define HASH_BLOCK_BYTES ( 512 / 8 ) + +#elif defined(MBEDTLS_SHA256_C) + +#define HASH_FUNC( in, ilen, out ) mbedtls_sha256_ret( in, ilen, out, 0 ); +#define HASH_BLOCK_BYTES ( 256 / 8 ) + +#else // from a previous #if we know that SHA-1 is available if SHA-2 isn't + +#define HASH_FUNC mbedtls_sha1_ret +#define HASH_BLOCK_BYTES ( 160 / 8 ) + +#endif /* SHA512/SHA256/SHA1 abstraction */ + +/* + * State consists of a 32-bit counter plus the secret value. + * + * We stored them concatenated in a single buffer as that's what will get + * passed to the hash function. + */ +typedef struct { + size_t total_len; + uint8_t buf[4 + MBEDTLS_ECP_MAX_BYTES]; +} ecp_drbg_context; + +static void ecp_drbg_init( ecp_drbg_context *ctx ) +{ + memset( ctx, 0, sizeof( ecp_drbg_context ) ); +} + +static void ecp_drbg_free( ecp_drbg_context *ctx ) +{ + mbedtls_platform_zeroize( ctx, sizeof( ecp_drbg_context ) ); +} + +static int ecp_drbg_seed( ecp_drbg_context *ctx, + const mbedtls_mpi *secret, size_t secret_len ) +{ + ctx->total_len = 4 + secret_len; + memset( ctx->buf, 0, 4); + return( mbedtls_mpi_write_binary( secret, ctx->buf + 4, secret_len ) ); +} + +static int ecp_drbg_random( void *p_rng, unsigned char *output, size_t output_len ) +{ + ecp_drbg_context *ctx = p_rng; + int ret; + size_t len_done = 0; + + while( len_done < output_len ) + { + uint8_t tmp[HASH_BLOCK_BYTES]; + uint8_t use_len; + + /* We don't need to draw more that 255 blocks, so don't bother with + * carry propagation and just return an error instead. */ + ctx->buf[3] += 1; + if( ctx->buf[3] == 0 ) + return( MBEDTLS_ERR_ECP_RANDOM_FAILED ); + + ret = HASH_FUNC( ctx->buf, ctx->total_len, tmp ); + if( ret != 0 ) + return( ret ); + + if( output_len - len_done > HASH_BLOCK_BYTES ) + use_len = HASH_BLOCK_BYTES; + else + use_len = output_len - len_done; + + memcpy( output + len_done, tmp, use_len ); + len_done += use_len; + } + + return( 0 ); +} + +#else /* DRBG/SHA modules */ #error "Invalid configuration detected. Include check_config.h to ensure that the configuration is valid." -#endif /* DRBG modules */ +#endif /* DRBG/SHA modules */ #endif /* MBEDTLS_ECP_NO_INTERNAL_RNG */ #if defined(MBEDTLS_ECP_RESTARTABLE) @@ -2187,7 +2288,8 @@ static int ecp_mul_comb( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, rs_ctx->rsm->drbg_seeded == 0 ) #endif { - MBEDTLS_MPI_CHK( ecp_drbg_seed( p_rng, m ) ); + const size_t m_len = ( grp->nbits + 7 ) / 8; + MBEDTLS_MPI_CHK( ecp_drbg_seed( p_rng, m, m_len ) ); } #if defined(MBEDTLS_ECP_RESTARTABLE) if( rs_ctx != NULL && rs_ctx->rsm != NULL ) @@ -2471,7 +2573,8 @@ static int ecp_mul_mxz( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, #if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG) if( f_rng == NULL ) { - MBEDTLS_MPI_CHK( ecp_drbg_seed( &drbg_ctx, m ) ); + const size_t m_len = ( grp->nbits + 7 ) / 8; + MBEDTLS_MPI_CHK( ecp_drbg_seed( &drbg_ctx, m, m_len ) ); f_rng = &ecp_drbg_random; p_rng = &drbg_ctx; } @@ -3091,6 +3194,89 @@ cleanup: #if defined(MBEDTLS_SELF_TEST) +#if defined(ECP_ONE_STEP_KDF) +/* + * There are no test vectors from NIST for the One-Step KDF in SP 800-56C, + * but unofficial ones can be found at: + * https://github.com/patrickfav/singlestep-kdf/wiki/NIST-SP-800-56C-Rev1:-Non-Official-Test-Vectors + * + * We only use the ones with empty fixedInfo, and for brevity's sake, only + * 32-bytes output (with SHA-1 that's more than one block, with SHA-256 + * exactly one block, and with SHA-512 less than one block). + */ +#if defined(MBEDTLS_SHA512_C) + +static const uint8_t test_kdf_z[16] = { + 0xeb, 0xf3, 0x19, 0x67, 0x1e, 0xac, 0xcc, 0x6f, + 0xc5, 0xc0, 0x5d, 0x95, 0x8d, 0x17, 0x15, 0x94, +}; +static const uint8_t test_kdf_out[32] = { + 0xa9, 0x48, 0x85, 0x67, 0x54, 0x7c, 0x2a, 0x8e, + 0x9e, 0xd1, 0x67, 0x76, 0xe3, 0x1c, 0x03, 0x92, + 0x41, 0x77, 0x2a, 0x9e, 0xc7, 0xcc, 0xd7, 0x1f, + 0xda, 0x12, 0xe9, 0xba, 0xc9, 0xb2, 0x17, 0x24, +}; + +#elif defined(MBEDTLS_SHA256_C) + +static const uint8_t test_kdf_z[16] = { + 0x0d, 0x5e, 0xc8, 0x9a, 0x68, 0xb1, 0xa7, 0xa0, + 0xdf, 0x95, 0x24, 0x54, 0x3f, 0x4d, 0x70, 0xef, +}; +static const uint8_t test_kdf_out[32] = { + 0x77, 0xbc, 0x94, 0x9e, 0xa0, 0xd3, 0xdd, 0x5c, + 0x8e, 0xb7, 0xeb, 0x84, 0x05, 0x40, 0x60, 0xfa, + 0x96, 0x6e, 0x7e, 0xcd, 0x73, 0x9f, 0xa1, 0xe6, + 0x34, 0x3f, 0x6d, 0x82, 0x16, 0x22, 0xb4, 0x45, +}; + +#elif defined(MBEDTLS_SHA1_C) + +static const uint8_t test_kdf_z[16] = { + 0x4e, 0x1e, 0x70, 0xc9, 0x88, 0x68, 0x19, 0xa3, + 0x1b, 0xc2, 0x9a, 0x53, 0x79, 0x11, 0xad, 0xd9, +}; +static const uint8_t test_kdf_out[32] = { + 0xdd, 0xbf, 0xc4, 0x40, 0x44, 0x9a, 0xab, 0x41, + 0x31, 0xc6, 0xd8, 0xae, 0xc0, 0x8c, 0xe1, 0x49, + 0x6f, 0x27, 0x02, 0x24, 0x1d, 0x0e, 0x27, 0xcc, + 0x15, 0x5c, 0x5c, 0x7c, 0x3c, 0xda, 0x75, 0xb5, +}; + +#else +#error "Need at least one of SHA-512, SHA-256 or SHA-1" +#endif + +static int ecp_kdf_self_test( void ) +{ + int ret; + ecp_drbg_context kdf_ctx; + mbedtls_mpi scalar; + uint8_t out[sizeof( test_kdf_out )]; + + ecp_drbg_init( &kdf_ctx ); + mbedtls_mpi_init( &scalar ); + memset( out, 0, sizeof( out ) ); + + MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &scalar, + test_kdf_z, sizeof( test_kdf_z ) ) ); + + MBEDTLS_MPI_CHK( ecp_drbg_seed( &kdf_ctx, + &scalar, sizeof( test_kdf_z ) ) ); + + MBEDTLS_MPI_CHK( ecp_drbg_random( &kdf_ctx, out, sizeof( out ) ) ); + + if( memcmp( out, test_kdf_out, sizeof( out ) ) != 0 ) + ret = -1; + +cleanup: + ecp_drbg_free( &kdf_ctx ); + mbedtls_mpi_free( &scalar ); + + return( ret ); +} +#endif /* ECP_ONE_STEP_KDF */ + /* * Checkup routine */ @@ -3202,6 +3388,24 @@ int mbedtls_ecp_self_test( int verbose ) if( verbose != 0 ) mbedtls_printf( "passed\n" ); +#if defined(ECP_ONE_STEP_KDF) + if( verbose != 0 ) + mbedtls_printf( " ECP test #3 (internal KDF): " ); + + ret = ecp_kdf_self_test(); + if( ret != 0 ) + { + if( verbose != 0 ) + mbedtls_printf( "failed\n" ); + + ret = 1; + goto cleanup; + } + + if( verbose != 0 ) + mbedtls_printf( "passed\n" ); +#endif /* ECP_ONE_STEP_KDF */ + cleanup: if( ret < 0 && verbose != 0 ) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index 039ae158c..483173602 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -817,6 +817,69 @@ component_test_no_hmac_drbg () { # so there's little value in running those lengthy tests here. } +component_test_no_drbg_all_hashes () { + # this tests the internal ECP DRBG using a KDF based on SHA-512 + msg "build: Default minus DRBGs" + scripts/config.pl unset MBEDTLS_CTR_DRBG_C + scripts/config.pl unset MBEDTLS_HMAC_DRBG_C + scripts/config.pl unset MBEDTLS_ECDSA_DETERMINISTIC # requires HMAC_DRBG + scripts/config.pl unset MBEDTLS_PSA_CRYPTO_C # requires a DRBG + scripts/config.pl unset MBEDTLS_PSA_CRYPTO_STORAGE_C # requires PSA Crypto + + CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan . + make + + msg "test: Default minus DRBGs" + make test + + # no SSL tests as they all depend on having a DRBG +} + +component_test_no_drbg_no_sha512 () { + # this tests the internal ECP DRBG using a KDF based on SHA-256 + msg "build: Default minus DRBGs minus SHA-512" + scripts/config.pl unset MBEDTLS_CTR_DRBG_C + scripts/config.pl unset MBEDTLS_HMAC_DRBG_C + scripts/config.pl unset MBEDTLS_ECDSA_DETERMINISTIC # requires HMAC_DRBG + scripts/config.pl unset MBEDTLS_PSA_CRYPTO_C # requires a DRBG + scripts/config.pl unset MBEDTLS_PSA_CRYPTO_STORAGE_C # requires PSA Crypto + scripts/config.pl unset MBEDTLS_SHA512_C + + CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan . + make + + msg "test: Default minus DRBGs minus SHA-512" + make test + + # no SSL tests as they all depend on having a DRBG +} + +component_test_no_drbg_no_sha2 () { + # this tests the internal ECP DRBG using a KDF based on SHA-1 + msg "build: Default minus DRBGs minus SHA-2" + scripts/config.pl unset MBEDTLS_CTR_DRBG_C + scripts/config.pl unset MBEDTLS_HMAC_DRBG_C + scripts/config.pl unset MBEDTLS_ECDSA_DETERMINISTIC # requires HMAC_DRBG + scripts/config.pl unset MBEDTLS_PSA_CRYPTO_C # requires a DRBG + scripts/config.pl unset MBEDTLS_PSA_CRYPTO_STORAGE_C # requires PSA Crypto + scripts/config.pl unset MBEDTLS_SHA512_C + scripts/config.pl unset MBEDTLS_SHA256_C + scripts/config.pl unset MBEDTLS_ENTROPY_C # requires SHA-2 + scripts/config.pl unset MBEDTLS_PSA_CRYPTO_C # requires Entropy + scripts/config.pl unset MBEDTLS_PSA_CRYPTO_STORAGE_C # requires PSA Crypto + scripts/config.pl unset MBEDTLS_PSA_CRYPTO_SE_C # requires PSA Crypto + scripts/config.pl unset MBEDTLS_USE_PSA_CRYPTO # requires PSA Crypto + scripts/config.pl unset MBEDTLS_SSL_PROTO_TLS1_2 # requires SHA-2 + + CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan . + make + + msg "test: Default minus DRBGs minus SHA-2" + make test + + # no SSL tests as they all depend on having a DRBG +} + component_test_ecp_no_internal_rng () { msg "build: Default plus ECP_NO_INTERNAL_RNG minus DRBG modules" scripts/config.pl set MBEDTLS_ECP_NO_INTERNAL_RNG From 301a9ee583ed59275f2ce0f47bc9f986ca75ae76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Wed, 17 Jun 2020 10:12:43 +0200 Subject: [PATCH 12/23] Fix potential memory overread in seed functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The previous commit introduced a potential memory overread by reading secret_len bytes from secret->p, while the is no guarantee that secret has enough limbs for that. Fix that by using an intermediate buffer and mpi_write_binary(). Signed-off-by: Manuel Pégourié-Gonnard --- library/ecp.c | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/library/ecp.c b/library/ecp.c index b8ad9357a..91fbe3b9f 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -180,13 +180,21 @@ static inline int ecp_drbg_random( void *p_rng, static int ecp_drbg_seed( ecp_drbg_context *ctx, const mbedtls_mpi *secret, size_t secret_len ) { - const unsigned char *secret_p = (const unsigned char *) secret->p; - + int ret; + unsigned char secret_bytes[MBEDTLS_ECP_MAX_BYTES]; /* The list starts with strong hashes */ const mbedtls_md_type_t md_type = mbedtls_md_list()[0]; const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_type ); - return( mbedtls_hmac_drbg_seed_buf( ctx, md_info, secret_p, secret_len ) ); + MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( secret, + secret_bytes, secret_len ) ); + + ret = mbedtls_hmac_drbg_seed_buf( ctx, md_info, secret_bytes, secret_len ); + +cleanup: + mbedtls_platform_zeroize( secret_bytes, secret_len ); + + return( ret ); } #elif defined(MBEDTLS_CTR_DRBG_C) @@ -231,10 +239,19 @@ static int ecp_ctr_drbg_null_entropy(void *ctx, unsigned char *out, size_t len) static int ecp_drbg_seed( ecp_drbg_context *ctx, const mbedtls_mpi *secret, size_t secret_len ) { - const unsigned char *secret_p = (const unsigned char *) secret->p; + int ret; + unsigned char secret_bytes[MBEDTLS_ECP_MAX_BYTES]; - return( mbedtls_ctr_drbg_seed( ctx, ecp_ctr_drbg_null_entropy, NULL, - secret_p, secret_len ) ); + MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( secret, + secret_bytes, secret_len ) ); + + ret = mbedtls_ctr_drbg_seed( ctx, ecp_ctr_drbg_null_entropy, NULL, + secret_bytes, secret_len ); + +cleanup: + mbedtls_platform_zeroize( secret_bytes, secret_len ); + + return( ret ); } #elif defined(MBEDTLS_SHA512_C) || \ From 424210a93c05450a7a8a5b0f6255d788b3f93f74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Wed, 17 Jun 2020 12:13:23 +0200 Subject: [PATCH 13/23] Allow inclusion of entropy.h when it's disabled MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The build was failing in all.sh component test_no_drbg_no_sha2 because entropy.h was referencing mbedtls_sha256_context but not including sha256.h when SHA-256 and SHA-512 were both disabled. This broke query_config.c which includes entropy.h (and actually all headers) unconditionally. Signed-off-by: Manuel Pégourié-Gonnard --- include/mbedtls/entropy.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/include/mbedtls/entropy.h b/include/mbedtls/entropy.h index ca06dc3c5..8bf5d713e 100644 --- a/include/mbedtls/entropy.h +++ b/include/mbedtls/entropy.h @@ -86,6 +86,8 @@ #define MBEDTLS_ENTROPY_SOURCE_STRONG 1 /**< Entropy source is strong */ #define MBEDTLS_ENTROPY_SOURCE_WEAK 0 /**< Entropy source is weak */ +#if defined(MBEDTLS_ENTROPY_C) + #ifdef __cplusplus extern "C" { #endif @@ -286,4 +288,6 @@ int mbedtls_entropy_source_self_test( int verbose ); } #endif +#endif /* MBEDTLS_ENTROPY_C */ + #endif /* entropy.h */ From 2d91c30f4cbf6fa30735a8b6d995db7a3e889768 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Wed, 17 Jun 2020 12:26:54 +0200 Subject: [PATCH 14/23] Update dependencies documentation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Manuel Pégourié-Gonnard --- ChangeLog.d/ecp-internal-rng.txt | 3 ++- include/mbedtls/config.h | 11 ++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/ChangeLog.d/ecp-internal-rng.txt b/ChangeLog.d/ecp-internal-rng.txt index c0419acad..f6b3c0f7f 100644 --- a/ChangeLog.d/ecp-internal-rng.txt +++ b/ChangeLog.d/ecp-internal-rng.txt @@ -1,6 +1,7 @@ Changes * The ECP module, enabled by `MBEDTLS_ECP_C`, now depends on - `MBEDTLS_CTR_DRBG_C` or `MBEDTLS_HMAC_DRBG_C` for some side-channel + `MBEDTLS_CTR_DRBG_C`, `MBEDTLS_HMAC_DRBG_C`, `MBEDTLS_SHA512_C`, + `MBEDTLS_SHA256_C` or `MBEDTLS_SHA1_C` for some side-channel coutermeasures. If side channels are not a concern, this dependency can be avoided by enabling the new option `MBEDTLS_ECP_NO_INTERNAL_RNG`. diff --git a/include/mbedtls/config.h b/include/mbedtls/config.h index 9442eb44b..90c90f931 100644 --- a/include/mbedtls/config.h +++ b/include/mbedtls/config.h @@ -788,11 +788,12 @@ * against some side-channel attacks. * * This protection introduces a dependency of the ECP module on one of the - * DRBG modules. For very constrained implementations that don't require this - * protection (for example, because you're only doing signature verification, - * so not manipulating any secret, or because local/physical side-channel - * attacks are outside your threat model), it might be desirable to get rid of - * that dependency. + * DRBG or SHA modules (HMAC-DRBG, CTR-DRBG, SHA-512, SHA-256 or SHA-1). + * For very constrained applications that don't require this protection + * (for example, because you're only doing signature verification, so not + * manipulating any secret, or because local/physical side-channel attacks are + * outside your threat model), it might be desirable to get rid of that + * dependency. * * \warning Enabling this option makes some uses of ECP vulnerable to some * side-channel attacks. Only enable it if you know that's not a problem for From a90a95bcbd19836417f68f4a864e8a09ec5024d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Wed, 17 Jun 2020 12:40:57 +0200 Subject: [PATCH 15/23] Zeroize temporary stack buffer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Manuel Pégourié-Gonnard --- library/ecp.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/library/ecp.c b/library/ecp.c index 91fbe3b9f..050a497e1 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -321,10 +321,10 @@ static int ecp_drbg_random( void *p_rng, unsigned char *output, size_t output_le ecp_drbg_context *ctx = p_rng; int ret; size_t len_done = 0; + uint8_t tmp[HASH_BLOCK_BYTES]; while( len_done < output_len ) { - uint8_t tmp[HASH_BLOCK_BYTES]; uint8_t use_len; /* We don't need to draw more that 255 blocks, so don't bother with @@ -346,6 +346,8 @@ static int ecp_drbg_random( void *p_rng, unsigned char *output, size_t output_le len_done += use_len; } + mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); + return( 0 ); } From 7d7c00412fcb0caf15ea88f8b7d44243e5f5af8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Wed, 17 Jun 2020 12:57:33 +0200 Subject: [PATCH 16/23] Improve comment justifying a hard-coded limitation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Manuel Pégourié-Gonnard --- library/ecp.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/library/ecp.c b/library/ecp.c index 050a497e1..9e0c8ef7c 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -327,8 +327,19 @@ static int ecp_drbg_random( void *p_rng, unsigned char *output, size_t output_le { uint8_t use_len; - /* We don't need to draw more that 255 blocks, so don't bother with - * carry propagation and just return an error instead. */ + /* This function is only called for coordinate randomisation, which + * happens only twice in a scalar multiplication. Each time needs a + * random value in the range [2, p-1], and gets it by drawing len(p) + * bytes from this function, and retrying up to 10 times if unlucky. + * + * So for the largest curve, each scalar multiplication draws at most + * 2 * 66 bytes. The minimum block size is 20 bytes (with SHA-1), so + * that means at most 66 blocks. + * + * Since we don't need to draw more that 255 blocks, don't bother + * with carry propagation and just return an error instead. We can + * change that it we even need to draw more blinding values. + */ ctx->buf[3] += 1; if( ctx->buf[3] == 0 ) return( MBEDTLS_ERR_ECP_RANDOM_FAILED ); From 20cd85c6e140c632fe80198b7f7e82bbd468341e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Thu, 18 Jun 2020 11:30:40 +0200 Subject: [PATCH 17/23] Use starts/finish around Lucky 13 dummy compressions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #3246 Signed-off-by: Manuel Pégourié-Gonnard --- ChangeLog.d/l13-hw-accel.txt | 7 +++++++ library/ssl_tls.c | 14 ++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 ChangeLog.d/l13-hw-accel.txt diff --git a/ChangeLog.d/l13-hw-accel.txt b/ChangeLog.d/l13-hw-accel.txt new file mode 100644 index 000000000..53c79243b --- /dev/null +++ b/ChangeLog.d/l13-hw-accel.txt @@ -0,0 +1,7 @@ +Security + * Fix issue in Lucky 13 counter-measure that could make it ineffective when + hardware accelerators were used (using one of the MBEDTLS_SHAxxx_ALT + macros). This would cause the original Lucky 13 attack to be possible in + those configurations, allowing an active network attacker to recover + plaintext after repeated timing measurements under some conditions. + Reported and fix suggested by Luc Perneel in #3246. diff --git a/library/ssl_tls.c b/library/ssl_tls.c index 9bc4fa81b..02b8f2654 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -2269,10 +2269,20 @@ static int ssl_decrypt_buf( mbedtls_ssl_context *ssl ) ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen ); mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect ); - /* Call mbedtls_md_process at least once due to cache attacks - * that observe whether md_process() was called of not */ + /* Dummy calls to compression function. + * Call mbedtls_md_process at least once due to cache attacks + * that observe whether md_process() was called of not. + * Respect the usual start-(process|update)-finish sequence for + * the sake of hardware accelerators that might require it. */ + mbedtls_md_starts( &ssl->transform_in->md_ctx_dec ); for( j = 0; j < extra_run + 1; j++ ) mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg ); + { + /* The switch statement above already checks that we're using + * one of MD-5, SHA-1, SHA-256 or SHA-384. */ + unsigned char tmp[384 / 8]; + mbedtls_md_finish( &ssl->transform_in->md_ctx_dec, tmp ); + } mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec ); From 2ebb1e18e997e7ba4e679d7b7b2fe1da4581d1f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Thu, 18 Jun 2020 11:59:57 +0200 Subject: [PATCH 18/23] Revert "Allow inclusion of entropy.h when it's disabled" This reverts commit 424210a93c05450a7a8a5b0f6255d788b3f93f74. This change was not safe enough for an LTS branch, as it might break code that assumes it's safe to declare an object of type mbedtls_entropy_context even when MBEDTLS_ENTROPY_C is undefined. --- include/mbedtls/entropy.h | 4 ---- 1 file changed, 4 deletions(-) diff --git a/include/mbedtls/entropy.h b/include/mbedtls/entropy.h index 8bf5d713e..ca06dc3c5 100644 --- a/include/mbedtls/entropy.h +++ b/include/mbedtls/entropy.h @@ -86,8 +86,6 @@ #define MBEDTLS_ENTROPY_SOURCE_STRONG 1 /**< Entropy source is strong */ #define MBEDTLS_ENTROPY_SOURCE_WEAK 0 /**< Entropy source is weak */ -#if defined(MBEDTLS_ENTROPY_C) - #ifdef __cplusplus extern "C" { #endif @@ -288,6 +286,4 @@ int mbedtls_entropy_source_self_test( int verbose ); } #endif -#endif /* MBEDTLS_ENTROPY_C */ - #endif /* entropy.h */ From 2df5857dbef9beebf0c5c1c76b4557773a04ef44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Thu, 18 Jun 2020 12:14:34 +0200 Subject: [PATCH 19/23] Remove SHA-1 as a fallback option MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - it's 2020, there shouldn't be too many systems out there where SHA-1 is the only available hash option, so its usefulness is limited - OTOH testing configurations without SHA-2 reveal bugs that are not easy to fix in a fully compatible way So overall, the benefit/cost ratio is not good enough to justify keeping SHA-1 as a fallback option here. Signed-off-by: Manuel Pégourié-Gonnard --- ChangeLog.d/ecp-internal-rng.txt | 8 +++---- include/mbedtls/check_config.h | 3 +-- include/mbedtls/config.h | 11 +++++---- library/ecp.c | 38 ++++++-------------------------- tests/scripts/all.sh | 26 ---------------------- 5 files changed, 17 insertions(+), 69 deletions(-) diff --git a/ChangeLog.d/ecp-internal-rng.txt b/ChangeLog.d/ecp-internal-rng.txt index f6b3c0f7f..8b5c5147e 100644 --- a/ChangeLog.d/ecp-internal-rng.txt +++ b/ChangeLog.d/ecp-internal-rng.txt @@ -1,9 +1,9 @@ Changes * The ECP module, enabled by `MBEDTLS_ECP_C`, now depends on - `MBEDTLS_CTR_DRBG_C`, `MBEDTLS_HMAC_DRBG_C`, `MBEDTLS_SHA512_C`, - `MBEDTLS_SHA256_C` or `MBEDTLS_SHA1_C` for some side-channel - coutermeasures. If side channels are not a concern, this dependency can - be avoided by enabling the new option `MBEDTLS_ECP_NO_INTERNAL_RNG`. + `MBEDTLS_CTR_DRBG_C`, `MBEDTLS_HMAC_DRBG_C`, `MBEDTLS_SHA512_C` or + `MBEDTLS_SHA256_C` for some side-channel coutermeasures. If side channels + are not a concern, this dependency can be avoided by enabling the new + option `MBEDTLS_ECP_NO_INTERNAL_RNG`. Security * Fix side channel in mbedtls_ecp_check_pub_priv() and diff --git a/include/mbedtls/check_config.h b/include/mbedtls/check_config.h index 3ae86a19b..21991b8ba 100644 --- a/include/mbedtls/check_config.h +++ b/include/mbedtls/check_config.h @@ -146,9 +146,8 @@ defined(MBEDTLS_HMAC_DRBG_C) || \ defined(MBEDTLS_SHA512_C) || \ defined(MBEDTLS_SHA256_C) || \ - defined(MBEDTLS_SHA1_C) || \ defined(MBEDTLS_ECP_NO_INTERNAL_RNG)) -#error "MBEDTLS_ECP_C requires a DRBG or SHA module unless MBEDTLS_ECP_NO_INTERNAL_RNG is defined or an alternative implementation is used" +#error "MBEDTLS_ECP_C requires a DRBG or SHA-2 module unless MBEDTLS_ECP_NO_INTERNAL_RNG is defined or an alternative implementation is used" #endif #if defined(MBEDTLS_PK_PARSE_C) && !defined(MBEDTLS_ASN1_PARSE_C) diff --git a/include/mbedtls/config.h b/include/mbedtls/config.h index 90c90f931..f0057dbd3 100644 --- a/include/mbedtls/config.h +++ b/include/mbedtls/config.h @@ -788,12 +788,11 @@ * against some side-channel attacks. * * This protection introduces a dependency of the ECP module on one of the - * DRBG or SHA modules (HMAC-DRBG, CTR-DRBG, SHA-512, SHA-256 or SHA-1). - * For very constrained applications that don't require this protection - * (for example, because you're only doing signature verification, so not - * manipulating any secret, or because local/physical side-channel attacks are - * outside your threat model), it might be desirable to get rid of that - * dependency. + * DRBG or SHA modules (HMAC-DRBG, CTR-DRBG, SHA-512 or SHA-256.) For very + * constrained applications that don't require this protection (for example, + * because you're only doing signature verification, so not manipulating any + * secret, or because local/physical side-channel attacks are outside your + * threat model), it might be desirable to get rid of that dependency. * * \warning Enabling this option makes some uses of ECP vulnerable to some * side-channel attacks. Only enable it if you know that's not a problem for diff --git a/library/ecp.c b/library/ecp.c index 9e0c8ef7c..cb21f363d 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -113,8 +113,6 @@ #include "mbedtls/sha512.h" #elif defined(MBEDTLS_SHA256_C) #include "mbedtls/sha256.h" -#elif defined(MBEDTLS_SHA1_C) -#include "mbedtls/sha1.h" #else #error "Invalid configuration detected. Include check_config.h to ensure that the configuration is valid." #endif @@ -254,9 +252,7 @@ cleanup: return( ret ); } -#elif defined(MBEDTLS_SHA512_C) || \ - defined(MBEDTLS_SHA256_C) || \ - defined(MBEDTLS_SHA1_C) +#elif defined(MBEDTLS_SHA512_C) || defined(MBEDTLS_SHA256_C) /* This will be used in the self-test function */ #define ECP_ONE_STEP_KDF @@ -268,7 +264,7 @@ cleanup: * function) and empty FixedInfo. (Though we'll make it fit the DRBG API for * convenience, this is not a full-fledged DRBG, but we don't need one here.) * - * We need a basic hash abstraction layer to use whatever SHA is available. + * We need a basic hash abstraction layer to use whatever SHA-2 is available. */ #if defined(MBEDTLS_SHA512_C) @@ -280,12 +276,7 @@ cleanup: #define HASH_FUNC( in, ilen, out ) mbedtls_sha256_ret( in, ilen, out, 0 ); #define HASH_BLOCK_BYTES ( 256 / 8 ) -#else // from a previous #if we know that SHA-1 is available if SHA-2 isn't - -#define HASH_FUNC mbedtls_sha1_ret -#define HASH_BLOCK_BYTES ( 160 / 8 ) - -#endif /* SHA512/SHA256/SHA1 abstraction */ +#endif /* SHA512/SHA256 abstraction */ /* * State consists of a 32-bit counter plus the secret value. @@ -333,8 +324,8 @@ static int ecp_drbg_random( void *p_rng, unsigned char *output, size_t output_le * bytes from this function, and retrying up to 10 times if unlucky. * * So for the largest curve, each scalar multiplication draws at most - * 2 * 66 bytes. The minimum block size is 20 bytes (with SHA-1), so - * that means at most 66 blocks. + * 20 * 66 bytes. The minimum block size is 32 (SHA-256), so with + * rounding that means a most 20 * 3 blocks. * * Since we don't need to draw more that 255 blocks, don't bother * with carry propagation and just return an error instead. We can @@ -3231,8 +3222,8 @@ cleanup: * https://github.com/patrickfav/singlestep-kdf/wiki/NIST-SP-800-56C-Rev1:-Non-Official-Test-Vectors * * We only use the ones with empty fixedInfo, and for brevity's sake, only - * 32-bytes output (with SHA-1 that's more than one block, with SHA-256 - * exactly one block, and with SHA-512 less than one block). + * 32-bytes output (with SHA-256 that's exactly one block, and with SHA-512 + * less than one block). */ #if defined(MBEDTLS_SHA512_C) @@ -3260,21 +3251,6 @@ static const uint8_t test_kdf_out[32] = { 0x34, 0x3f, 0x6d, 0x82, 0x16, 0x22, 0xb4, 0x45, }; -#elif defined(MBEDTLS_SHA1_C) - -static const uint8_t test_kdf_z[16] = { - 0x4e, 0x1e, 0x70, 0xc9, 0x88, 0x68, 0x19, 0xa3, - 0x1b, 0xc2, 0x9a, 0x53, 0x79, 0x11, 0xad, 0xd9, -}; -static const uint8_t test_kdf_out[32] = { - 0xdd, 0xbf, 0xc4, 0x40, 0x44, 0x9a, 0xab, 0x41, - 0x31, 0xc6, 0xd8, 0xae, 0xc0, 0x8c, 0xe1, 0x49, - 0x6f, 0x27, 0x02, 0x24, 0x1d, 0x0e, 0x27, 0xcc, - 0x15, 0x5c, 0x5c, 0x7c, 0x3c, 0xda, 0x75, 0xb5, -}; - -#else -#error "Need at least one of SHA-512, SHA-256 or SHA-1" #endif static int ecp_kdf_self_test( void ) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index 483173602..87c16531b 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -854,32 +854,6 @@ component_test_no_drbg_no_sha512 () { # no SSL tests as they all depend on having a DRBG } -component_test_no_drbg_no_sha2 () { - # this tests the internal ECP DRBG using a KDF based on SHA-1 - msg "build: Default minus DRBGs minus SHA-2" - scripts/config.pl unset MBEDTLS_CTR_DRBG_C - scripts/config.pl unset MBEDTLS_HMAC_DRBG_C - scripts/config.pl unset MBEDTLS_ECDSA_DETERMINISTIC # requires HMAC_DRBG - scripts/config.pl unset MBEDTLS_PSA_CRYPTO_C # requires a DRBG - scripts/config.pl unset MBEDTLS_PSA_CRYPTO_STORAGE_C # requires PSA Crypto - scripts/config.pl unset MBEDTLS_SHA512_C - scripts/config.pl unset MBEDTLS_SHA256_C - scripts/config.pl unset MBEDTLS_ENTROPY_C # requires SHA-2 - scripts/config.pl unset MBEDTLS_PSA_CRYPTO_C # requires Entropy - scripts/config.pl unset MBEDTLS_PSA_CRYPTO_STORAGE_C # requires PSA Crypto - scripts/config.pl unset MBEDTLS_PSA_CRYPTO_SE_C # requires PSA Crypto - scripts/config.pl unset MBEDTLS_USE_PSA_CRYPTO # requires PSA Crypto - scripts/config.pl unset MBEDTLS_SSL_PROTO_TLS1_2 # requires SHA-2 - - CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan . - make - - msg "test: Default minus DRBGs minus SHA-2" - make test - - # no SSL tests as they all depend on having a DRBG -} - component_test_ecp_no_internal_rng () { msg "build: Default plus ECP_NO_INTERNAL_RNG minus DRBG modules" scripts/config.pl set MBEDTLS_ECP_NO_INTERNAL_RNG From 2df1423effd8f6c8a49aadf5a96e3f4c89da20e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Mon, 22 Jun 2020 10:18:58 +0200 Subject: [PATCH 20/23] Test multi-block output of the hash-based KDF MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Manuel Pégourié-Gonnard --- library/ecp.c | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/library/ecp.c b/library/ecp.c index cb21f363d..44217f7ae 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -3222,33 +3222,35 @@ cleanup: * https://github.com/patrickfav/singlestep-kdf/wiki/NIST-SP-800-56C-Rev1:-Non-Official-Test-Vectors * * We only use the ones with empty fixedInfo, and for brevity's sake, only - * 32-bytes output (with SHA-256 that's exactly one block, and with SHA-512 + * 40-bytes output (with SHA-256 that's more than one block, and with SHA-512 * less than one block). */ #if defined(MBEDTLS_SHA512_C) static const uint8_t test_kdf_z[16] = { - 0xeb, 0xf3, 0x19, 0x67, 0x1e, 0xac, 0xcc, 0x6f, - 0xc5, 0xc0, 0x5d, 0x95, 0x8d, 0x17, 0x15, 0x94, + 0x3b, 0xa9, 0x79, 0xe9, 0xbc, 0x5e, 0x3e, 0xc7, + 0x61, 0x30, 0x36, 0xb6, 0xf5, 0x1c, 0xd5, 0xaa, }; -static const uint8_t test_kdf_out[32] = { - 0xa9, 0x48, 0x85, 0x67, 0x54, 0x7c, 0x2a, 0x8e, - 0x9e, 0xd1, 0x67, 0x76, 0xe3, 0x1c, 0x03, 0x92, - 0x41, 0x77, 0x2a, 0x9e, 0xc7, 0xcc, 0xd7, 0x1f, - 0xda, 0x12, 0xe9, 0xba, 0xc9, 0xb2, 0x17, 0x24, +static const uint8_t test_kdf_out[40] = { + 0x3e, 0xf6, 0xda, 0xf9, 0x51, 0x60, 0x70, 0x5f, + 0xdf, 0x21, 0xcd, 0xab, 0xac, 0x25, 0x7b, 0x05, + 0xfe, 0xc1, 0xab, 0x7c, 0xc9, 0x68, 0x43, 0x25, + 0x8a, 0xfc, 0x40, 0x6e, 0x5b, 0xf7, 0x98, 0x27, + 0x10, 0xfa, 0x7b, 0x93, 0x52, 0xd4, 0x16, 0xaa, }; #elif defined(MBEDTLS_SHA256_C) static const uint8_t test_kdf_z[16] = { - 0x0d, 0x5e, 0xc8, 0x9a, 0x68, 0xb1, 0xa7, 0xa0, - 0xdf, 0x95, 0x24, 0x54, 0x3f, 0x4d, 0x70, 0xef, + 0xc8, 0x3e, 0x35, 0x8e, 0x99, 0xa6, 0x89, 0xc6, + 0x7d, 0xb4, 0xfe, 0x39, 0xcf, 0x8f, 0x26, 0xe1, }; -static const uint8_t test_kdf_out[32] = { - 0x77, 0xbc, 0x94, 0x9e, 0xa0, 0xd3, 0xdd, 0x5c, - 0x8e, 0xb7, 0xeb, 0x84, 0x05, 0x40, 0x60, 0xfa, - 0x96, 0x6e, 0x7e, 0xcd, 0x73, 0x9f, 0xa1, 0xe6, - 0x34, 0x3f, 0x6d, 0x82, 0x16, 0x22, 0xb4, 0x45, +static const uint8_t test_kdf_out[40] = { + 0x7d, 0xf6, 0x41, 0xf8, 0x3c, 0x47, 0xdc, 0x28, + 0x5f, 0x7f, 0xaa, 0xde, 0x05, 0x64, 0xd6, 0x25, + 0x00, 0x6a, 0x47, 0xd9, 0x1e, 0xa4, 0xa0, 0x8c, + 0xd7, 0xf7, 0x0c, 0x99, 0xaa, 0xa0, 0x72, 0x66, + 0x69, 0x0e, 0x25, 0xaa, 0xa1, 0x63, 0x14, 0x79, }; #endif From 49e6caf12ed99e4f8aa2c461c478acce36f235ed Mon Sep 17 00:00:00 2001 From: Janos Follath Date: Fri, 26 Jun 2020 11:33:34 +0100 Subject: [PATCH 21/23] Assemble ChangeLog Executed scripts/assemble_changelog.py. Signed-off-by: Janos Follath --- ChangeLog | 52 ++++++++++++++++++++++++ ChangeLog.d/bugfix.txt | 4 -- ChangeLog.d/ecp-internal-rng.txt | 16 -------- ChangeLog.d/error-asn1.txt | 2 - ChangeLog.d/fix-ecp-mul-memory-leak.txt | 3 -- ChangeLog.d/l13-hw-accel.txt | 7 ---- ChangeLog.d/max_pathlen.txt | 5 --- ChangeLog.d/montmul-cmp-branch.txt | 6 --- ChangeLog.d/unified-exit-in-examples.txt | 4 -- ChangeLog.d/uniformize_bounds_checks.txt | 9 ---- 10 files changed, 52 insertions(+), 56 deletions(-) delete mode 100644 ChangeLog.d/bugfix.txt delete mode 100644 ChangeLog.d/ecp-internal-rng.txt delete mode 100644 ChangeLog.d/error-asn1.txt delete mode 100644 ChangeLog.d/fix-ecp-mul-memory-leak.txt delete mode 100644 ChangeLog.d/l13-hw-accel.txt delete mode 100644 ChangeLog.d/max_pathlen.txt delete mode 100644 ChangeLog.d/montmul-cmp-branch.txt delete mode 100644 ChangeLog.d/unified-exit-in-examples.txt delete mode 100644 ChangeLog.d/uniformize_bounds_checks.txt diff --git a/ChangeLog b/ChangeLog index 96fa9f334..cf210add5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,57 @@ mbed TLS ChangeLog (Sorted per branch, date) += mbed TLS x.x.x branch released xxxx-xx-xx + +Security + * Fix a side channel vulnerability in modular exponentiation that could + reveal an RSA private key used in a secure enclave. Noticed by Sangho Lee, + Ming-Wei Shih, Prasun Gera, Taesoo Kim and Hyesoon Kim (Georgia Institute + of Technology); and Marcus Peinado (Microsoft Research). Reported by Raoul + Strackx (Fortanix) in #3394. + * Fix side channel in mbedtls_ecp_check_pub_priv() and + mbedtls_pk_parse_key() / mbedtls_pk_parse_keyfile() (when loading a + private key that didn't include the uncompressed public key), as well as + mbedtls_ecp_mul() / mbedtls_ecp_mul_restartable() when called with a NULL + f_rng argument. An attacker with access to precise enough timing and + memory access information (typically an untrusted operating system + attacking a secure enclave) could fully recover the ECC private key. + Found and reported by Alejandro Cabrera Aldaya and Billy Brumley. + * Fix issue in Lucky 13 counter-measure that could make it ineffective when + hardware accelerators were used (using one of the MBEDTLS_SHAxxx_ALT + macros). This would cause the original Lucky 13 attack to be possible in + those configurations, allowing an active network attacker to recover + plaintext after repeated timing measurements under some conditions. + Reported and fix suggested by Luc Perneel in #3246. + +Bugfix + * Fix the Visual Studio Release x64 build configuration for mbedtls itself. + Completes a previous fix in Mbed TLS 2.16.3 that only fixed the build for + the example programs. Reported in #1430 and fix contributed by irwir. + * Fix undefined behavior in X.509 certificate parsing if the + pathLenConstraint basic constraint value is equal to INT_MAX. + The actual effect with almost every compiler is the intended + behavior, so this is unlikely to be exploitable anywhere. #3197 + * Include asn1.h in error.c. Fixes #3328 reported by David Hu. + * Fix potential memory leaks in ecp_randomize_jac() and ecp_randomize_mxz() + when PRNG function fails. Contributed by Jonas Lejeune in #3318. + * Add additional bounds checks in ssl_write_client_hello() preventing + output buffer overflow if the configuration declared a buffer that was + too small. + +Changes + * Unify the example programs termination to call mbedtls_exit() instead of + using a return command. This has been done to enable customization of the + behavior in bare metal environments. + * Abort the ClientHello writing function as soon as some extension doesn't + fit into the record buffer. Previously, such extensions were silently + dropped. As a consequence, the TLS handshake now fails when the output + buffer is not large enough to hold the ClientHello. + * The ECP module, enabled by `MBEDTLS_ECP_C`, now depends on + `MBEDTLS_CTR_DRBG_C`, `MBEDTLS_HMAC_DRBG_C`, `MBEDTLS_SHA512_C` or + `MBEDTLS_SHA256_C` for some side-channel coutermeasures. If side channels + are not a concern, this dependency can be avoided by enabling the new + option `MBEDTLS_ECP_NO_INTERNAL_RNG`. + = mbed TLS 2.16.6 branch released 2020-04-14 Security diff --git a/ChangeLog.d/bugfix.txt b/ChangeLog.d/bugfix.txt deleted file mode 100644 index d7ace300e..000000000 --- a/ChangeLog.d/bugfix.txt +++ /dev/null @@ -1,4 +0,0 @@ -Bugfix - * Fix the Visual Studio Release x64 build configuration for mbedtls itself. - Completes a previous fix in Mbed TLS 2.16.3 that only fixed the build for - the example programs. Reported in #1430 and fix contributed by irwir. diff --git a/ChangeLog.d/ecp-internal-rng.txt b/ChangeLog.d/ecp-internal-rng.txt deleted file mode 100644 index 8b5c5147e..000000000 --- a/ChangeLog.d/ecp-internal-rng.txt +++ /dev/null @@ -1,16 +0,0 @@ -Changes - * The ECP module, enabled by `MBEDTLS_ECP_C`, now depends on - `MBEDTLS_CTR_DRBG_C`, `MBEDTLS_HMAC_DRBG_C`, `MBEDTLS_SHA512_C` or - `MBEDTLS_SHA256_C` for some side-channel coutermeasures. If side channels - are not a concern, this dependency can be avoided by enabling the new - option `MBEDTLS_ECP_NO_INTERNAL_RNG`. - -Security - * Fix side channel in mbedtls_ecp_check_pub_priv() and - mbedtls_pk_parse_key() / mbedtls_pk_parse_keyfile() (when loading a - private key that didn't include the uncompressed public key), as well as - mbedtls_ecp_mul() / mbedtls_ecp_mul_restartable() when called with a NULL - f_rng argument. An attacker with access to precise enough timing and - memory access information (typically an untrusted operating system - attacking a secure enclave) could fully recover the ECC private key. - Found and reported by Alejandro Cabrera Aldaya and Billy Brumley. diff --git a/ChangeLog.d/error-asn1.txt b/ChangeLog.d/error-asn1.txt deleted file mode 100644 index c165696fd..000000000 --- a/ChangeLog.d/error-asn1.txt +++ /dev/null @@ -1,2 +0,0 @@ -Bugfix - * Include asn1.h in error.c. Fixes #3328 reported by David Hu. diff --git a/ChangeLog.d/fix-ecp-mul-memory-leak.txt b/ChangeLog.d/fix-ecp-mul-memory-leak.txt deleted file mode 100644 index e82cadc2d..000000000 --- a/ChangeLog.d/fix-ecp-mul-memory-leak.txt +++ /dev/null @@ -1,3 +0,0 @@ -Bugfix - * Fix potential memory leaks in ecp_randomize_jac() and ecp_randomize_mxz() - when PRNG function fails. Contributed by Jonas Lejeune in #3318. diff --git a/ChangeLog.d/l13-hw-accel.txt b/ChangeLog.d/l13-hw-accel.txt deleted file mode 100644 index 53c79243b..000000000 --- a/ChangeLog.d/l13-hw-accel.txt +++ /dev/null @@ -1,7 +0,0 @@ -Security - * Fix issue in Lucky 13 counter-measure that could make it ineffective when - hardware accelerators were used (using one of the MBEDTLS_SHAxxx_ALT - macros). This would cause the original Lucky 13 attack to be possible in - those configurations, allowing an active network attacker to recover - plaintext after repeated timing measurements under some conditions. - Reported and fix suggested by Luc Perneel in #3246. diff --git a/ChangeLog.d/max_pathlen.txt b/ChangeLog.d/max_pathlen.txt deleted file mode 100644 index 4ebf0ad93..000000000 --- a/ChangeLog.d/max_pathlen.txt +++ /dev/null @@ -1,5 +0,0 @@ -Bugfix - * Fix undefined behavior in X.509 certificate parsing if the - pathLenConstraint basic constraint value is equal to INT_MAX. - The actual effect with almost every compiler is the intended - behavior, so this is unlikely to be exploitable anywhere. #3197 diff --git a/ChangeLog.d/montmul-cmp-branch.txt b/ChangeLog.d/montmul-cmp-branch.txt deleted file mode 100644 index 59945188a..000000000 --- a/ChangeLog.d/montmul-cmp-branch.txt +++ /dev/null @@ -1,6 +0,0 @@ -Security - * Fix a side channel vulnerability in modular exponentiation that could - reveal an RSA private key used in a secure enclave. Noticed by Sangho Lee, - Ming-Wei Shih, Prasun Gera, Taesoo Kim and Hyesoon Kim (Georgia Institute - of Technology); and Marcus Peinado (Microsoft Research). Reported by Raoul - Strackx (Fortanix) in #3394. diff --git a/ChangeLog.d/unified-exit-in-examples.txt b/ChangeLog.d/unified-exit-in-examples.txt deleted file mode 100644 index 3ef9798ad..000000000 --- a/ChangeLog.d/unified-exit-in-examples.txt +++ /dev/null @@ -1,4 +0,0 @@ -Changes - * Unify the example programs termination to call mbedtls_exit() instead of - using a return command. This has been done to enable customization of the - behavior in bare metal environments. diff --git a/ChangeLog.d/uniformize_bounds_checks.txt b/ChangeLog.d/uniformize_bounds_checks.txt deleted file mode 100644 index 210ab1051..000000000 --- a/ChangeLog.d/uniformize_bounds_checks.txt +++ /dev/null @@ -1,9 +0,0 @@ -Bugfix - * Add additional bounds checks in ssl_write_client_hello() preventing - output buffer overflow if the configuration declared a buffer that was - too small. -Changes - * Abort the ClientHello writing function as soon as some extension doesn't - fit into the record buffer. Previously, such extensions were silently - dropped. As a consequence, the TLS handshake now fails when the output - buffer is not large enough to hold the ClientHello. From ef5f8fc52a8d2cfa4fee06bccdbbf8c3170d2b32 Mon Sep 17 00:00:00 2001 From: Janos Follath Date: Fri, 26 Jun 2020 12:35:53 +0100 Subject: [PATCH 22/23] Bump version to Mbed TLS 2.16.7 Executed "./scripts/bump_version.sh --version 2.16.7" Signed-off-by: Janos Follath --- doxygen/input/doc_mainpage.h | 2 +- doxygen/mbedtls.doxyfile | 2 +- include/mbedtls/version.h | 8 ++++---- library/CMakeLists.txt | 6 +++--- tests/suites/test_suite_version.data | 4 ++-- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/doxygen/input/doc_mainpage.h b/doxygen/input/doc_mainpage.h index d61f92891..f1a9b0b51 100644 --- a/doxygen/input/doc_mainpage.h +++ b/doxygen/input/doc_mainpage.h @@ -51,7 +51,7 @@ */ /** - * @mainpage mbed TLS v2.16.6 source code documentation + * @mainpage mbed TLS v2.16.7 source code documentation * * This documentation describes the internal structure of mbed TLS. It was * automatically generated from specially formatted comment blocks in diff --git a/doxygen/mbedtls.doxyfile b/doxygen/mbedtls.doxyfile index 1edc9cefa..642c63000 100644 --- a/doxygen/mbedtls.doxyfile +++ b/doxygen/mbedtls.doxyfile @@ -28,7 +28,7 @@ DOXYFILE_ENCODING = UTF-8 # identify the project. Note that if you do not use Doxywizard you need # to put quotes around the project name if it contains spaces. -PROJECT_NAME = "mbed TLS v2.16.6" +PROJECT_NAME = "mbed TLS v2.16.7" # The PROJECT_NUMBER tag can be used to enter a project or revision number. # This could be handy for archiving the generated documentation or diff --git a/include/mbedtls/version.h b/include/mbedtls/version.h index 86e1f65d0..2bff31d51 100644 --- a/include/mbedtls/version.h +++ b/include/mbedtls/version.h @@ -67,16 +67,16 @@ */ #define MBEDTLS_VERSION_MAJOR 2 #define MBEDTLS_VERSION_MINOR 16 -#define MBEDTLS_VERSION_PATCH 6 +#define MBEDTLS_VERSION_PATCH 7 /** * The single version number has the following structure: * MMNNPP00 * Major version | Minor version | Patch version */ -#define MBEDTLS_VERSION_NUMBER 0x02100600 -#define MBEDTLS_VERSION_STRING "2.16.6" -#define MBEDTLS_VERSION_STRING_FULL "mbed TLS 2.16.6" +#define MBEDTLS_VERSION_NUMBER 0x02100700 +#define MBEDTLS_VERSION_STRING "2.16.7" +#define MBEDTLS_VERSION_STRING_FULL "mbed TLS 2.16.7" #if defined(MBEDTLS_VERSION_C) diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index 93ce7b8f4..fb4acd9b9 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -165,15 +165,15 @@ endif(USE_STATIC_MBEDTLS_LIBRARY) if(USE_SHARED_MBEDTLS_LIBRARY) add_library(mbedcrypto SHARED ${src_crypto}) - set_target_properties(mbedcrypto PROPERTIES VERSION 2.16.6 SOVERSION 3) + set_target_properties(mbedcrypto PROPERTIES VERSION 2.16.7 SOVERSION 3) target_link_libraries(mbedcrypto ${libs}) add_library(mbedx509 SHARED ${src_x509}) - set_target_properties(mbedx509 PROPERTIES VERSION 2.16.6 SOVERSION 0) + set_target_properties(mbedx509 PROPERTIES VERSION 2.16.7 SOVERSION 0) target_link_libraries(mbedx509 ${libs} mbedcrypto) add_library(mbedtls SHARED ${src_tls}) - set_target_properties(mbedtls PROPERTIES VERSION 2.16.6 SOVERSION 12) + set_target_properties(mbedtls PROPERTIES VERSION 2.16.7 SOVERSION 12) target_link_libraries(mbedtls ${libs} mbedx509) install(TARGETS mbedtls mbedx509 mbedcrypto diff --git a/tests/suites/test_suite_version.data b/tests/suites/test_suite_version.data index 54c1220ef..6a0ac0b1e 100644 --- a/tests/suites/test_suite_version.data +++ b/tests/suites/test_suite_version.data @@ -1,8 +1,8 @@ Check compiletime library version -check_compiletime_version:"2.16.6" +check_compiletime_version:"2.16.7" Check runtime library version -check_runtime_version:"2.16.6" +check_runtime_version:"2.16.7" Check for MBEDTLS_VERSION_C check_feature:"MBEDTLS_VERSION_C":0 From 6c48d09c2fbf44db16fc1ec77bcbb916445983bc Mon Sep 17 00:00:00 2001 From: Janos Follath Date: Fri, 26 Jun 2020 12:42:11 +0100 Subject: [PATCH 23/23] Update ChangeLog header Signed-off-by: Janos Follath --- ChangeLog | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index cf210add5..b765f6b6c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,6 @@ mbed TLS ChangeLog (Sorted per branch, date) -= mbed TLS x.x.x branch released xxxx-xx-xx += mbed TLS 2.16.7 branch released 2020-07-01 Security * Fix a side channel vulnerability in modular exponentiation that could