Add flow monitor protection to mbedtls_platform_memcmp()

Signed-off-by: Piotr Nowicki <piotr.nowicki@arm.com>
This commit is contained in:
Piotr Nowicki 2020-05-20 13:57:38 +02:00
parent 2d21e3e47b
commit 4aaa34c03f

View File

@ -125,22 +125,25 @@ int mbedtls_platform_memcmp( const void *buf1, const void *buf2, size_t num )
volatile unsigned char diff = 0;
size_t i = num;
size_t flow_counter = 0;
size_t start_offset = (size_t) mbedtls_platform_random_in_range( num );
for( i = start_offset; i < num; i++ )
{
unsigned char x = A[i], y = B[i];
flow_counter++;
diff |= x ^ y;
}
for( i = 0; i < start_offset; i++ )
{
unsigned char x = A[i], y = B[i];
flow_counter++;
diff |= x ^ y;
}
return( diff );
/* Return 0 only when diff is 0 and flow_counter is equal to num */
return( (int) diff | (int) ( flow_counter ^ num ) );
}
uint32_t mbedtls_platform_random_in_range( size_t num )