Add random delay function to platform_utils
Add delay function to platform_utils. The function will delay program execution by incrementing local variable randomised number of times.
This commit is contained in:
parent
7d2434fac2
commit
4c63b98e94
2 changed files with 38 additions and 0 deletions
|
@ -238,6 +238,21 @@ int mbedtls_platform_memcmp( const void *buf1, const void *buf2, size_t num );
|
||||||
*/
|
*/
|
||||||
uint32_t mbedtls_platform_random_in_range( size_t num );
|
uint32_t mbedtls_platform_random_in_range( size_t num );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Random delay function.
|
||||||
|
*
|
||||||
|
* Function implements random delay by incrementing local variable
|
||||||
|
* randomized number of times.
|
||||||
|
*
|
||||||
|
* \note Currently the function is dependent of hardware providing an
|
||||||
|
* rng with MBEDTLS_ENTROPY_HARDWARE_ALT.
|
||||||
|
*
|
||||||
|
* \param num Max-value for the local variable increments.
|
||||||
|
*
|
||||||
|
* \return In success number of increments, -1 in case of errors.
|
||||||
|
*/
|
||||||
|
int mbedtls_platform_random_delay( size_t num );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief This function does nothing, but can be inserted between
|
* \brief This function does nothing, but can be inserted between
|
||||||
* successive reads to a volatile local variable to prevent
|
* successive reads to a volatile local variable to prevent
|
||||||
|
|
|
@ -165,6 +165,29 @@ uint32_t mbedtls_platform_random_in_range( size_t num )
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int mbedtls_platform_random_delay( size_t max_rand )
|
||||||
|
{
|
||||||
|
#if !defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
|
||||||
|
(void) max_rand;
|
||||||
|
return -1;
|
||||||
|
#else
|
||||||
|
size_t random_number;
|
||||||
|
volatile size_t i = 0;
|
||||||
|
if( max_rand == 0 || max_rand > INT_MAX )
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
random_number = mbedtls_platform_random_in_range( max_rand );
|
||||||
|
|
||||||
|
do {
|
||||||
|
i++;
|
||||||
|
} while ( i < random_number );
|
||||||
|
|
||||||
|
return (int) i;
|
||||||
|
#endif /* !MBEDTLS_ENTROPY_HARDWARE_ALT */
|
||||||
|
}
|
||||||
|
|
||||||
/* Some compilers (armcc 5 for example) optimize away successive reads from a
|
/* Some compilers (armcc 5 for example) optimize away successive reads from a
|
||||||
* volatile local variable (which we use as a counter-measure to fault
|
* volatile local variable (which we use as a counter-measure to fault
|
||||||
* injection attacks), unless there is a call to an external function between
|
* injection attacks), unless there is a call to an external function between
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue