From 5a85442604b3b749aca50e0459bcbafd8e6830e3 Mon Sep 17 00:00:00 2001 From: gabor-mezei-arm Date: Mon, 27 Sep 2021 12:25:07 +0200 Subject: [PATCH] Move mbedtls_cf_size_gt function to the constant-time module Signed-off-by: gabor-mezei-arm --- library/constant_time.c | 16 ++++++++++++++++ library/constant_time.h | 2 ++ library/rsa.c | 16 ---------------- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/library/constant_time.c b/library/constant_time.c index d73f4fe448..7da4046621 100644 --- a/library/constant_time.c +++ b/library/constant_time.c @@ -213,3 +213,19 @@ size_t mbedtls_cf_size_bool_eq( size_t x, size_t y ) return( 1 ^ diff1 ); } + +/** Check whether a size is out of bounds, without branches. + * + * This is equivalent to `size > max`, but is likely to be compiled to + * to code using bitwise operation rather than a branch. + * + * \param size Size to check. + * \param max Maximum desired value for \p size. + * \return \c 0 if `size <= max`. + * \return \c 1 if `size > max`. + */ +unsigned mbedtls_cf_size_gt( size_t size, size_t max ) +{ + /* Return the sign bit (1 for negative) of (max - size). */ + return( ( max - size ) >> ( sizeof( size_t ) * 8 - 1 ) ); +} diff --git a/library/constant_time.h b/library/constant_time.h index 50108d7d91..eff7f446f1 100644 --- a/library/constant_time.h +++ b/library/constant_time.h @@ -39,3 +39,5 @@ size_t mbedtls_cf_size_mask_lt( size_t x, size_t y ); size_t mbedtls_cf_size_mask_ge( size_t x, size_t y ); size_t mbedtls_cf_size_bool_eq( size_t x, size_t y ); + +unsigned mbedtls_cf_size_gt( size_t size, size_t max ); diff --git a/library/rsa.c b/library/rsa.c index 3e19ad950c..21d6d12dff 100644 --- a/library/rsa.c +++ b/library/rsa.c @@ -1458,22 +1458,6 @@ cleanup: #endif /* MBEDTLS_PKCS1_V21 */ #if defined(MBEDTLS_PKCS1_V15) -/** Check whether a size is out of bounds, without branches. - * - * This is equivalent to `size > max`, but is likely to be compiled to - * to code using bitwise operation rather than a branch. - * - * \param size Size to check. - * \param max Maximum desired value for \p size. - * \return \c 0 if `size <= max`. - * \return \c 1 if `size > max`. - */ -static unsigned mbedtls_cf_size_gt( size_t size, size_t max ) -{ - /* Return the sign bit (1 for negative) of (max - size). */ - return( ( max - size ) >> ( sizeof( size_t ) * 8 - 1 ) ); -} - /** Choose between two integer values, without branches. * * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled