Move mbedtls_cf_uint_if function to the constant-time module

Signed-off-by: Gabor Mezei <gabor.mezei@arm.com>
This commit is contained in:
gabor-mezei-arm 2021-09-27 12:59:30 +02:00 committed by Gabor Mezei
parent 097d4f555e
commit 7533253125
No known key found for this signature in database
GPG key ID: 106F5A41ECC305BD
3 changed files with 19 additions and 16 deletions

View file

@ -273,3 +273,19 @@ unsigned mbedtls_cf_mpi_uint_lt( const mbedtls_mpi_uint x,
}
#endif /* MBEDTLS_BIGNUM_C */
/** Choose between two integer values, without branches.
*
* This is equivalent to `cond ? if1 : if0`, but is likely to be compiled
* to code using bitwise operation rather than a branch.
*
* \param cond Condition to test.
* \param if1 Value to use if \p cond is nonzero.
* \param if0 Value to use if \p cond is zero.
* \return \c if1 if \p cond is nonzero, otherwise \c if0.
*/
unsigned mbedtls_cf_uint_if( unsigned cond, unsigned if1, unsigned if0 )
{
unsigned mask = mbedtls_cf_uint_mask( cond );
return( ( mask & if1 ) | (~mask & if0 ) );
}

View file

@ -52,3 +52,6 @@ unsigned mbedtls_cf_mpi_uint_lt( const mbedtls_mpi_uint x,
const mbedtls_mpi_uint y );
#endif /* MBEDTLS_BIGNUM_C */
unsigned mbedtls_cf_uint_if( unsigned cond, unsigned if1, unsigned if0 );

View file

@ -1479,22 +1479,6 @@ cleanup:
#endif /* MBEDTLS_PKCS1_V21 */
#if defined(MBEDTLS_PKCS1_V15)
/** Choose between two integer values, without branches.
*
* This is equivalent to `cond ? if1 : if0`, but is likely to be compiled
* to code using bitwise operation rather than a branch.
*
* \param cond Condition to test.
* \param if1 Value to use if \p cond is nonzero.
* \param if0 Value to use if \p cond is zero.
* \return \c if1 if \p cond is nonzero, otherwise \c if0.
*/
static unsigned mbedtls_cf_uint_if( unsigned cond, unsigned if1, unsigned if0 )
{
unsigned mask = mbedtls_cf_uint_mask( cond );
return( ( mask & if1 ) | (~mask & if0 ) );
}
/** Shift some data towards the left inside a buffer without leaking
* the length of the data through side channels.
*