From 2603fec329a8466b036621f53ae072d4278b2e5e Mon Sep 17 00:00:00 2001 From: Andrzej Kurek Date: Fri, 8 Apr 2022 08:28:50 -0400 Subject: [PATCH 1/3] Remove dummy timing implementation Having such implementation might cause issues for those that expect to have a working implementation. Having a compile-time error is better in such case. Signed-off-by: Andrzej Kurek --- library/timing.c | 41 ----------------------------------------- 1 file changed, 41 deletions(-) diff --git a/library/timing.c b/library/timing.c index a5a55d66c..d46a6d85e 100644 --- a/library/timing.c +++ b/library/timing.c @@ -67,7 +67,6 @@ struct _hr_time #endif #endif /* _WIN32 && !EFIX64 && !EFI32 */ -#if defined(MBEDTLS_HAVE_TIME) #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \ ( defined(_MSC_VER) && defined(_M_IX86) ) || defined(__WATCOMC__) @@ -526,45 +525,5 @@ hard_test_done: } #endif /* MBEDTLS_SELF_TEST */ - -#else -volatile int mbedtls_timing_alarmed = 0; -int mbedtls_timing_get_delay( void *data ) -{ - (void) data; - return( 0 ); -} - -void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms ) -{ - (void) data; - (void) int_ms; - (void) fin_ms; -} - -unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset ) -{ - (void) val; - (void) reset; - return( 0 ); -} - -unsigned long mbedtls_timing_hardclock( void ) -{ - return( 0 ); -} - -void mbedtls_set_alarm( int seconds ) -{ - (void) seconds; -} -#if defined(MBEDTLS_SELF_TEST) -int mbedtls_timing_self_test( int verbose ) -{ - (void) verbose; - return( 0 ); -} -#endif /* MBEDTLS_SELF_TEST */ -#endif /* MBEDTLS_HAVE_TIME */ #endif /* !MBEDTLS_TIMING_ALT */ #endif /* MBEDTLS_TIMING_C */ From 263d8f7e6125075bf3c99fbab191ba0744123363 Mon Sep 17 00:00:00 2001 From: Andrzej Kurek Date: Fri, 8 Apr 2022 08:34:41 -0400 Subject: [PATCH 2/3] Remove the dependency on MBEDTLS_HAVE_TIME from MBEDTLS_TIMING_C The timing module might include time.h on its own when on a suitable platform, even if MBEDTLS_HAVE_TIME is disabled. Co-authored-by: Tom Cosgrove Signed-off-by: Andrzej Kurek --- include/mbedtls/config.h | 11 ++++++++++- library/timing.c | 4 ++-- tests/suites/test_suite_timing.data | 6 ------ 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/include/mbedtls/config.h b/include/mbedtls/config.h index b0296eb8d..109f217bf 100644 --- a/include/mbedtls/config.h +++ b/include/mbedtls/config.h @@ -128,7 +128,12 @@ * MBEDTLS_PLATFORM_TIME_MACRO, MBEDTLS_PLATFORM_TIME_TYPE_MACRO and * MBEDTLS_PLATFORM_STD_TIME. * - * Comment if your system does not support time functions + * Comment if your system does not support time functions. + * + * \note If MBEDTLS_TIMING_C is set - to enable the semi-portable timing + * interface - timing.c will include time.h on suitable platforms + * regardless of the setting of MBEDTLS_HAVE_TIME, unless + * MBEDTLS_TIMING_ALT is used. See timing.c for more information. */ #define MBEDTLS_HAVE_TIME @@ -3466,6 +3471,10 @@ * your own implementation of the whole module by setting * \c MBEDTLS_TIMING_ALT in the current file. * + * \note The timing module will include time.h on suitable platforms + * regardless of the setting of MBEDTLS_HAVE_TIME, unless + * MBEDTLS_TIMING_ALT is used. See timing.c for more information. + * * \note See also our Knowledge Base article about porting to a new * environment: * https://tls.mbed.org/kb/how-to/how-do-i-port-mbed-tls-to-a-new-environment-OS diff --git a/library/timing.c b/library/timing.c index d46a6d85e..57bc9bcc1 100644 --- a/library/timing.c +++ b/library/timing.c @@ -57,14 +57,14 @@ struct _hr_time #include #include #include -#if defined(MBEDTLS_HAVE_TIME) +/* time.h should be included independently of MBEDTLS_HAVE_TIME. If the + * platform matches the ifdefs above, it will be used. */ #include #include struct _hr_time { struct timeval start; }; -#endif #endif /* _WIN32 && !EFIX64 && !EFI32 */ #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \ diff --git a/tests/suites/test_suite_timing.data b/tests/suites/test_suite_timing.data index b7b089c44..2522da1ea 100644 --- a/tests/suites/test_suite_timing.data +++ b/tests/suites/test_suite_timing.data @@ -1,23 +1,17 @@ Timing: hardclock -depends_on:MBEDTLS_HAVE_TIME timing_hardclock: Timing: get timer -depends_on:MBEDTLS_HAVE_TIME timing_get_timer: Timing: set alarm with no delay -depends_on:MBEDTLS_HAVE_TIME timing_set_alarm:0: Timing: set alarm with 1s delay -depends_on:MBEDTLS_HAVE_TIME timing_set_alarm:1: Timing: delay 0ms -depends_on:MBEDTLS_HAVE_TIME timing_delay:0: Timing: delay 100ms -depends_on:MBEDTLS_HAVE_TIME timing_delay:100: From 7ece76857805a3e6a8d70290a084632d3e3fa7e0 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Tue, 5 Apr 2022 21:39:43 +0200 Subject: [PATCH 3/3] Seed the PRNG even if time() isn't available time() is only needed to seed the PRNG non-deterministically. If it isn't available, do seed it, but pick a static seed. Signed-off-by: Andrzej Kurek --- programs/test/udp_proxy.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/programs/test/udp_proxy.c b/programs/test/udp_proxy.c index 9a6087c72..a50da1a3f 100644 --- a/programs/test/udp_proxy.c +++ b/programs/test/udp_proxy.c @@ -827,7 +827,6 @@ int main( int argc, char *argv[] ) get_options( argc, argv ); -#if defined(MBEDTLS_HAVE_TIME) /* * Decisions to drop/delay/duplicate packets are pseudo-random: dropping * exactly 1 in N packets would lead to problems when a flight has exactly @@ -838,12 +837,15 @@ int main( int argc, char *argv[] ) */ if( opt.seed == 0 ) { +#if defined(MBEDTLS_HAVE_TIME) opt.seed = (unsigned int) mbedtls_time( NULL ); +#else + opt.seed = 1; +#endif /* MBEDTLS_HAVE_TIME */ mbedtls_printf( " . Pseudo-random seed: %u\n", opt.seed ); } srand( opt.seed ); -#endif /* MBEDTLS_HAVE_TIME */ /* * 0. "Connect" to the server