From b2dbf2c113aa4daa0c375b5102872b7ac97a4131 Mon Sep 17 00:00:00 2001 From: gabor-mezei-arm Date: Mon, 27 Sep 2021 12:59:30 +0200 Subject: [PATCH] Move mbedtls_cf_uint_if function to the constant-time module Signed-off-by: gabor-mezei-arm --- library/constant_time.c | 16 ++++++++++++++++ library/constant_time.h | 3 +++ library/rsa.c | 16 ---------------- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/library/constant_time.c b/library/constant_time.c index b513c6a9d4..6d531345cc 100644 --- a/library/constant_time.c +++ b/library/constant_time.c @@ -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 ) ); +} diff --git a/library/constant_time.h b/library/constant_time.h index 3c18b4ef96..973e856d6b 100644 --- a/library/constant_time.h +++ b/library/constant_time.h @@ -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 ); + diff --git a/library/rsa.c b/library/rsa.c index 21d6d12dff..a75906a3c3 100644 --- a/library/rsa.c +++ b/library/rsa.c @@ -1458,22 +1458,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. *