From 65cefdbfcbfc2aa9d886c77d6a0d925126ba8322 Mon Sep 17 00:00:00 2001 From: gabor-mezei-arm Date: Mon, 27 Sep 2021 15:47:00 +0200 Subject: [PATCH] Create mbedtls_cf_size_if function Add a constant-time function with size_t parameter for choosing between two integer values, like the ?: ternary operator. Signed-off-by: gabor-mezei-arm --- library/constant_time.c | 6 ++++++ library/constant_time.h | 2 ++ 2 files changed, 8 insertions(+) diff --git a/library/constant_time.c b/library/constant_time.c index 8a9c45399..d84132749 100644 --- a/library/constant_time.c +++ b/library/constant_time.c @@ -295,6 +295,12 @@ unsigned mbedtls_cf_uint_if( unsigned cond, unsigned if1, unsigned if0 ) return( ( mask & if1 ) | (~mask & if0 ) ); } +size_t mbedtls_cf_size_if( unsigned cond, size_t if1, size_t if0 ) +{ + size_t mask = mbedtls_cf_size_mask( cond ); + return( ( mask & if1 ) | (~mask & if0 ) ); +} + /** * Select between two sign values in constant-time. * diff --git a/library/constant_time.h b/library/constant_time.h index 782e89a6a..c7ca490d3 100644 --- a/library/constant_time.h +++ b/library/constant_time.h @@ -59,6 +59,8 @@ unsigned mbedtls_cf_mpi_uint_lt( const mbedtls_mpi_uint x, unsigned mbedtls_cf_uint_if( unsigned cond, unsigned if1, unsigned if0 ); +size_t mbedtls_cf_size_if( unsigned cond, size_t if1, size_t if0 ); + int mbedtls_cf_cond_select_sign( int a, int b, unsigned char second ); #if defined(MBEDTLS_BIGNUM_C)