Unify mask generation functions

Generate all-bits 0 or all bits 1 mask from a value instead of from a bit.

Signed-off-by: gabor-mezei-arm <gabor.mezei@arm.com>
This commit is contained in:
gabor-mezei-arm 2021-08-10 20:56:21 +02:00
parent 87ac5bef97
commit 396438c57b
No known key found for this signature in database
GPG key ID: 106F5A41ECC305BD
2 changed files with 6 additions and 6 deletions

View file

@ -72,9 +72,9 @@ unsigned mbedtls_cf_uint_mask( unsigned value )
}
/*
* Turn a bit into a mask:
* - if bit == 1, return the all-bits 1 mask, aka (size_t) -1
* - if bit == 0, return the all-bits 0 mask, aka 0
* Turn a value into a mask:
* - if value != 0, return the all-bits 1 mask, aka (size_t) -1
* - if value == 0, return the all-bits 0 mask, aka 0
*
* This function can be used to write constant-time code by replacing branches
* with bit operations using masks.
@ -82,7 +82,7 @@ unsigned mbedtls_cf_uint_mask( unsigned value )
* This function is implemented without using comparison operators, as those
* might be translated to branches by some compilers on some platforms.
*/
size_t mbedtls_cf_size_mask( size_t bit )
size_t mbedtls_cf_size_mask( size_t value )
{
/* MSVC has a warning about unary minus on unsigned integer types,
* but this is well-defined and precisely what we want to do here. */
@ -90,7 +90,7 @@ size_t mbedtls_cf_size_mask( size_t bit )
#pragma warning( push )
#pragma warning( disable : 4146 )
#endif
return -bit;
return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) );
#if defined(_MSC_VER)
#pragma warning( pop )
#endif

View file

@ -36,7 +36,7 @@ int mbedtls_cf_memcmp( const void *a,
unsigned mbedtls_cf_uint_mask( unsigned value );
size_t mbedtls_cf_size_mask( size_t bit );
size_t mbedtls_cf_size_mask( size_t value );
size_t mbedtls_cf_size_mask_lt( size_t x,
size_t y );