Strengthen fall-back for mbedtls_ct_compiler_opaque

Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
This commit is contained in:
Dave Rodgman 2023-06-08 17:52:21 +01:00
parent ec85b85468
commit 2894d007d3

View File

@ -65,20 +65,29 @@
* Core const-time primitives * Core const-time primitives
*/ */
/** Ensure that the compiler cannot know the value of x (i.e., cannot optimise /* Ensure that the compiler cannot know the value of x (i.e., cannot optimise
* based on its value) after this function is called. * based on its value) after this function is called.
* *
* If we are not using assembly, this will be fairly inefficient, so its use * If we are not using assembly, this will be fairly inefficient, so its use
* should be minimised. * should be minimised.
*/ */
#if !defined(MBEDTLS_CT_ASM)
/*
* Define an object with the value zero, such that the compiler cannot prove that it
* has the value zero (because it is volatile, it "may be modified in ways unknown to
* the implementation").
*/
static volatile mbedtls_ct_uint_t mbedtls_ct_zero = 0;
#endif
static inline mbedtls_ct_uint_t mbedtls_ct_compiler_opaque(mbedtls_ct_uint_t x) static inline mbedtls_ct_uint_t mbedtls_ct_compiler_opaque(mbedtls_ct_uint_t x)
{ {
#if defined(MBEDTLS_CT_ASM) #if defined(MBEDTLS_CT_ASM)
asm volatile ("" : [x] "+r" (x) :); asm volatile ("" : [x] "+r" (x) :);
return x; return x;
#else #else
volatile mbedtls_ct_uint_t result = x; return x ^ mbedtls_ct_zero;
return result;
#endif #endif
} }