mirror of
https://github.com/yuzu-emu/mbedtls.git
synced 2025-01-22 03:21:07 +00:00
Merge pull request #3841 from AndrzejKurek/baremetal-rnd-in-range-fix
Move size checks outside of mbedtls_platform_random_in_range
This commit is contained in:
commit
5eba1d82a2
|
@ -276,9 +276,9 @@ uint32_t mbedtls_platform_random_uint32( void );
|
||||||
* cryptographically secure RNG, but provide an RNG for utility
|
* cryptographically secure RNG, but provide an RNG for utility
|
||||||
* functions.
|
* functions.
|
||||||
*
|
*
|
||||||
* \note If the given range is [0, 0), 0 is returned.
|
|
||||||
*
|
|
||||||
* \param num Max-value for the generated random number, exclusive.
|
* \param num Max-value for the generated random number, exclusive.
|
||||||
|
* Must be greater than zero, otherwise an undefined behavior
|
||||||
|
* will occur on "num % 0".
|
||||||
* The generated number will be on range [0, num).
|
* The generated number will be on range [0, num).
|
||||||
*
|
*
|
||||||
* \return The generated random number.
|
* \return The generated random number.
|
||||||
|
|
|
@ -119,43 +119,45 @@ void *mbedtls_platform_zeroize( void *buf, size_t len )
|
||||||
|
|
||||||
void *mbedtls_platform_memset( void *ptr, int value, size_t num )
|
void *mbedtls_platform_memset( void *ptr, int value, size_t num )
|
||||||
{
|
{
|
||||||
size_t i, start_offset;
|
size_t i, start_offset = 0;
|
||||||
volatile size_t flow_counter = 0;
|
volatile size_t flow_counter = 0;
|
||||||
volatile char *b = ptr;
|
volatile char *b = ptr;
|
||||||
char rnd_data;
|
char rnd_data;
|
||||||
|
if( num > 0 )
|
||||||
start_offset = (size_t) mbedtls_platform_random_in_range( (uint32_t) num );
|
|
||||||
rnd_data = (char) mbedtls_platform_random_in_range( 256 );
|
|
||||||
|
|
||||||
/* Perform a memset operations with random data and start from a random
|
|
||||||
* location */
|
|
||||||
for( i = start_offset; i < num; ++i )
|
|
||||||
{
|
{
|
||||||
b[i] = rnd_data;
|
start_offset = (size_t) mbedtls_platform_random_in_range( (uint32_t) num );
|
||||||
flow_counter++;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Start from a random location with target data */
|
rnd_data = (char) mbedtls_platform_random_in_range( 256 );
|
||||||
for( i = start_offset; i < num; ++i )
|
|
||||||
{
|
|
||||||
b[i] = value;
|
|
||||||
flow_counter++;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Second memset operation with random data */
|
/* Perform a memset operations with random data and start from a random
|
||||||
for( i = 0; i < start_offset; ++i )
|
* location */
|
||||||
{
|
for( i = start_offset; i < num; ++i )
|
||||||
b[i] = rnd_data;
|
{
|
||||||
flow_counter++;
|
b[i] = rnd_data;
|
||||||
}
|
flow_counter++;
|
||||||
|
}
|
||||||
|
|
||||||
/* Finish memset operation with correct data */
|
/* Start from a random location with target data */
|
||||||
for( i = 0; i < start_offset; ++i )
|
for( i = start_offset; i < num; ++i )
|
||||||
{
|
{
|
||||||
b[i] = value;
|
b[i] = value;
|
||||||
flow_counter++;
|
flow_counter++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Second memset operation with random data */
|
||||||
|
for( i = 0; i < start_offset; ++i )
|
||||||
|
{
|
||||||
|
b[i] = rnd_data;
|
||||||
|
flow_counter++;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Finish memset operation with correct data */
|
||||||
|
for( i = 0; i < start_offset; ++i )
|
||||||
|
{
|
||||||
|
b[i] = value;
|
||||||
|
flow_counter++;
|
||||||
|
}
|
||||||
|
}
|
||||||
/* check the correct number of iterations */
|
/* check the correct number of iterations */
|
||||||
if( flow_counter == 2 * num )
|
if( flow_counter == 2 * num )
|
||||||
{
|
{
|
||||||
|
@ -165,6 +167,7 @@ void *mbedtls_platform_memset( void *ptr, int value, size_t num )
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -245,22 +248,25 @@ int mbedtls_platform_memequal( const void *buf1, const void *buf2, size_t num )
|
||||||
|
|
||||||
/* Start from a random location and check the correct number of iterations */
|
/* Start from a random location and check the correct number of iterations */
|
||||||
size_t i, flow_counter = 0;
|
size_t i, flow_counter = 0;
|
||||||
size_t start_offset = (size_t) mbedtls_platform_random_in_range( (uint32_t) num );
|
size_t start_offset = 0;
|
||||||
|
if( num > 0 )
|
||||||
for( i = start_offset; i < num; i++ )
|
|
||||||
{
|
{
|
||||||
unsigned char x = A[i], y = B[i];
|
start_offset = (size_t) mbedtls_platform_random_in_range( (uint32_t) num );
|
||||||
flow_counter++;
|
|
||||||
diff |= x ^ y;
|
|
||||||
}
|
|
||||||
|
|
||||||
for( i = 0; i < start_offset; i++ )
|
for( i = start_offset; i < num; i++ )
|
||||||
{
|
{
|
||||||
unsigned char x = A[i], y = B[i];
|
unsigned char x = A[i], y = B[i];
|
||||||
flow_counter++;
|
flow_counter++;
|
||||||
diff |= x ^ y;
|
diff |= x ^ y;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for( i = 0; i < start_offset; i++ )
|
||||||
|
{
|
||||||
|
unsigned char x = A[i], y = B[i];
|
||||||
|
flow_counter++;
|
||||||
|
diff |= x ^ y;
|
||||||
|
}
|
||||||
|
}
|
||||||
/* Return 0 only when diff is 0 and flow_counter is equal to num */
|
/* Return 0 only when diff is 0 and flow_counter is equal to num */
|
||||||
return( (int) diff | (int) ( flow_counter ^ num ) );
|
return( (int) diff | (int) ( flow_counter ^ num ) );
|
||||||
}
|
}
|
||||||
|
@ -340,18 +346,7 @@ void mbedtls_platform_random_buf( uint8_t *buf, size_t len )
|
||||||
|
|
||||||
uint32_t mbedtls_platform_random_in_range( uint32_t num )
|
uint32_t mbedtls_platform_random_in_range( uint32_t num )
|
||||||
{
|
{
|
||||||
uint32_t result;
|
return mbedtls_platform_random_uint32() % num;
|
||||||
|
|
||||||
if( num <= 1 )
|
|
||||||
{
|
|
||||||
result = 0;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
result = mbedtls_platform_random_uint32() % num;
|
|
||||||
}
|
|
||||||
|
|
||||||
return( result );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void mbedtls_platform_random_delay( void )
|
void mbedtls_platform_random_delay( void )
|
||||||
|
|
Loading…
Reference in a new issue