mirror of
https://github.com/yuzu-emu/mbedtls.git
synced 2025-02-24 11:56:53 +00:00
Move mbedtls_cf_memcpy_if_eq function to the constant-time module
Signed-off-by: Gabor Mezei <gabor.mezei@arm.com>
This commit is contained in:
parent
7b23c0b46d
commit
ee06febbb4
|
@ -395,3 +395,26 @@ void mbedtls_cf_mem_move_to_left( void *start,
|
||||||
buf[total-1] = mbedtls_cf_uint_if( no_op, buf[total-1], 0 );
|
buf[total-1] = mbedtls_cf_uint_if( no_op, buf[total-1], 0 );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Constant-flow conditional memcpy:
|
||||||
|
* - if c1 == c2, equivalent to memcpy(dst, src, len),
|
||||||
|
* - otherwise, a no-op,
|
||||||
|
* but with execution flow independent of the values of c1 and c2.
|
||||||
|
*
|
||||||
|
* This function is implemented without using comparison operators, as those
|
||||||
|
* might be translated to branches by some compilers on some platforms.
|
||||||
|
*/
|
||||||
|
void mbedtls_cf_memcpy_if_eq( unsigned char *dst,
|
||||||
|
const unsigned char *src,
|
||||||
|
size_t len,
|
||||||
|
size_t c1, size_t c2 )
|
||||||
|
{
|
||||||
|
/* mask = c1 == c2 ? 0xff : 0x00 */
|
||||||
|
const size_t equal = mbedtls_cf_size_bool_eq( c1, c2 );
|
||||||
|
const unsigned char mask = (unsigned char) mbedtls_cf_size_mask( equal );
|
||||||
|
|
||||||
|
/* dst[i] = c1 == c2 ? src[i] : dst[i] */
|
||||||
|
for( size_t i = 0; i < len; i++ )
|
||||||
|
dst[i] = ( src[i] & mask ) | ( dst[i] & ~mask );
|
||||||
|
}
|
||||||
|
|
|
@ -69,3 +69,8 @@ void mbedtls_cf_mpi_uint_cond_assign( size_t n,
|
||||||
void mbedtls_cf_mem_move_to_left( void *start,
|
void mbedtls_cf_mem_move_to_left( void *start,
|
||||||
size_t total,
|
size_t total,
|
||||||
size_t offset );
|
size_t offset );
|
||||||
|
|
||||||
|
void mbedtls_cf_memcpy_if_eq( unsigned char *dst,
|
||||||
|
const unsigned char *src,
|
||||||
|
size_t len,
|
||||||
|
size_t c1, size_t c2 );
|
||||||
|
|
|
@ -1046,29 +1046,6 @@ int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
|
||||||
|
|
||||||
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
|
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
|
||||||
|
|
||||||
/*
|
|
||||||
* Constant-flow conditional memcpy:
|
|
||||||
* - if c1 == c2, equivalent to memcpy(dst, src, len),
|
|
||||||
* - otherwise, a no-op,
|
|
||||||
* but with execution flow independent of the values of c1 and c2.
|
|
||||||
*
|
|
||||||
* This function is implemented without using comparison operators, as those
|
|
||||||
* might be translated to branches by some compilers on some platforms.
|
|
||||||
*/
|
|
||||||
static void mbedtls_cf_memcpy_if_eq( unsigned char *dst,
|
|
||||||
const unsigned char *src,
|
|
||||||
size_t len,
|
|
||||||
size_t c1, size_t c2 )
|
|
||||||
{
|
|
||||||
/* mask = c1 == c2 ? 0xff : 0x00 */
|
|
||||||
const size_t equal = mbedtls_cf_size_bool_eq( c1, c2 );
|
|
||||||
const unsigned char mask = (unsigned char) mbedtls_cf_size_mask( equal );
|
|
||||||
|
|
||||||
/* dst[i] = c1 == c2 ? src[i] : dst[i] */
|
|
||||||
for( size_t i = 0; i < len; i++ )
|
|
||||||
dst[i] = ( src[i] & mask ) | ( dst[i] & ~mask );
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Compute HMAC of variable-length data with constant flow.
|
* Compute HMAC of variable-length data with constant flow.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in a new issue