Make RNG exclude the given maximum value

The RNG will give numbers in range of [0, num), so that
the given maximum is excluded.
This commit is contained in:
Jarno Lamsa 2019-10-02 08:23:11 +03:00
parent 0ff7109b7c
commit f5ebe2a7ce
2 changed files with 5 additions and 4 deletions

View file

@ -225,8 +225,9 @@ int mbedtls_platform_memcmp( const void *buf1, const void *buf2, size_t num );
* cryptographically secure RNG, but provide an RNG for utility
* functions.
*
* \param num Max-value for the generated random number.
*
* \param num Max-value for the generated random number, exclusive.
* The generated number will be on range [0, num).
* \return The generated random number.
*/
uint32_t mbedtls_platform_random_in_range( size_t num );

View file

@ -84,7 +84,7 @@ void mbedtls_platform_memset( void *ptr, int value, size_t num )
/* Randomize start offset. */
size_t start_offset = (size_t) mbedtls_platform_random_in_range( num );
/* Randomize data */
size_t data = (size_t) mbedtls_platform_random_in_range( 0xff );
size_t data = (size_t) mbedtls_platform_random_in_range( 256 );
/* Perform a pair of memset operations from random locations with
* random data */
@ -101,7 +101,7 @@ void mbedtls_platform_memcpy( void *dst, const void *src, size_t num )
/* Randomize start offset. */
size_t start_offset = (size_t) mbedtls_platform_random_in_range( num );
/* Randomize initial data to prevent leakage while copying */
size_t data = (size_t) mbedtls_platform_random_in_range( 0xff );
size_t data = (size_t) mbedtls_platform_random_in_range( 256 );
memset( (void *) dst, data, num );
memcpy( (void *) ( (unsigned char *) dst + start_offset ),