From 057daa3b284d9be70484d8689b2f4896cf450c52 Mon Sep 17 00:00:00 2001 From: Piotr Nowicki Date: Mon, 3 Aug 2020 13:08:33 +0200 Subject: [PATCH] Random delay can be disabled in configuration Use random delay depending on whether MBEDTLS_FI_COUNTERMEASURES is defined Signed-off-by: Piotr Nowicki --- include/mbedtls/platform_util.h | 4 ++++ library/platform_util.c | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/include/mbedtls/platform_util.h b/include/mbedtls/platform_util.h index 6f0920963..8d00eba9f 100644 --- a/include/mbedtls/platform_util.h +++ b/include/mbedtls/platform_util.h @@ -262,6 +262,10 @@ uint32_t mbedtls_platform_random_in_range( uint32_t num ); * * Duration of the delay is random as number of variable increments * is randomized. + * + * \note This function works only if the MBEDTLS_FI_COUNTERMEASURES flag + * is defined in the configuration. Otherwise, the function does + * nothing. */ void mbedtls_platform_random_delay( void ); diff --git a/library/platform_util.c b/library/platform_util.c index 641172ad9..3fa943738 100644 --- a/library/platform_util.c +++ b/library/platform_util.c @@ -255,6 +255,7 @@ uint32_t mbedtls_platform_random_in_range( uint32_t num ) void mbedtls_platform_random_delay( void ) { +#if defined(MBEDTLS_FI_COUNTERMEASURES) uint32_t rn_1, rn_2, rn_3; volatile size_t i = 0; uint8_t shift; @@ -276,6 +277,9 @@ void mbedtls_platform_random_delay( void ) rn_3 = ( rn_3 << shift ) | ( rn_3 >> ( 32 - shift ) ); rn_2 ^= rn_3; } while( i < rn_1 || rn_2 == 0 || rn_3 == 0 ); + +#endif /* MBEDTLS_FI_COUNTERMEASURES */ + return; } #if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT)