Skip to content

Commit

Permalink
Bind functions' availability for config options
Browse files Browse the repository at this point in the history
Signed-off-by: gabor-mezei-arm <[email protected]>
  • Loading branch information
gabor-mezei-arm committed Aug 25, 2021
1 parent 5abf445 commit a283f18
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
22 changes: 17 additions & 5 deletions library/constant_time.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "mbedtls/bignum.h"
#endif

#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
#if defined(MBEDTLS_SSL_TLS_C)
#include "ssl_misc.h"
#endif

Expand Down Expand Up @@ -106,6 +106,8 @@ size_t mbedtls_cf_size_mask( size_t value )
#endif
}

#if defined(MBEDTLS_BIGNUM_C)

mbedtls_mpi_uint mbedtls_cf_mpi_uint_mask( mbedtls_mpi_uint value )
{
/* MSVC has a warning about unary minus on unsigned, but this is
Expand All @@ -120,6 +122,8 @@ mbedtls_mpi_uint mbedtls_cf_mpi_uint_mask( mbedtls_mpi_uint value )
#endif
}

#endif /* MBEDTLS_BIGNUM_C */

/*
* Constant-flow mask generation for "less than" comparison:
* - if x < y, return all bits 1, that is (size_t) -1
Expand Down Expand Up @@ -239,6 +243,8 @@ unsigned mbedtls_cf_size_gt( size_t x, size_t y )
}


#if defined(MBEDTLS_BIGNUM_C)

/** Decide if an integer is less than the other, without branches.
*
* \param x First integer.
Expand Down Expand Up @@ -274,6 +280,8 @@ unsigned mbedtls_cf_mpi_uint_lt( const mbedtls_mpi_uint x,
return (unsigned) ret;
}

#endif /* MBEDTLS_BIGNUM_C */

/** Choose between two integer values, without branches.
*
* This is equivalent to `condition ? if1 : if0`, but is likely to be compiled
Expand Down Expand Up @@ -373,6 +381,8 @@ void mbedtls_cf_uint32_cond_assign( uint32_t * dest,
*dest = ( src & mask ) | ( ( *dest ) & ~mask );
}

#if defined(MBEDTLS_BIGNUM_C)

/*
* Conditionally assign dest = src, without leaking information
* about whether the assignment was made or not.
Expand Down Expand Up @@ -404,6 +414,8 @@ void mbedtls_cf_mpi_uint_cond_assign( size_t n,
dest[i] = ( src[i] & mask ) | ( dest[i] & ~mask );
}

#endif /* MBEDTLS_BIGNUM_C */

/*
* Constant flow lookup into table.
*/
Expand Down Expand Up @@ -488,7 +500,7 @@ void mbedtls_cf_memcpy_if_eq( unsigned char *dest,
dest[i] = ( src[i] & mask ) | ( dest[i] & ~mask );
}

#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
#if defined(MBEDTLS_SSL_TLS_C)

/*
* Compute HMAC of variable-length data with constant flow.
Expand Down Expand Up @@ -601,7 +613,7 @@ void mbedtls_ssl_cf_memcpy_offset( unsigned char *dst,
}
}

#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
#endif /* MBEDTLS_SSL_TLS_C */

#if defined(MBEDTLS_BIGNUM_C)

Expand Down Expand Up @@ -752,7 +764,7 @@ int mbedtls_mpi_lt_mpi_ct( const mbedtls_mpi *X,

#endif /* MBEDTLS_BIGNUM_C */

#if defined(MBEDTLS_PKCS1_V15)
#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)

#define RSA_VALIDATE_RET( cond ) \
MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_RSA_BAD_INPUT_DATA )
Expand Down Expand Up @@ -912,4 +924,4 @@ int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(
return( ret );
}

#endif /* MBEDTLS_PKCS1_V15 */
#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */
14 changes: 10 additions & 4 deletions library/constant_time.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "mbedtls/bignum.h"
#endif

#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
#if defined(MBEDTLS_SSL_TLS_C)
#include "mbedtls/md.h"
#endif

Expand All @@ -35,7 +35,9 @@ unsigned mbedtls_cf_uint_mask( unsigned value );

size_t mbedtls_cf_size_mask( size_t value );

#if defined(MBEDTLS_BIGNUM_C)
mbedtls_mpi_uint mbedtls_cf_mpi_uint_mask( mbedtls_mpi_uint value );
#endif /* MBEDTLS_BIGNUM_C */

size_t mbedtls_cf_size_mask_lt( size_t x, size_t y );

Expand All @@ -47,8 +49,10 @@ unsigned mbedtls_cf_size_bool_eq( size_t x, size_t y );

unsigned mbedtls_cf_size_gt( size_t x, size_t y );

#if defined(MBEDTLS_BIGNUM_C)
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 condition, unsigned if1, unsigned if0 );

Expand All @@ -62,10 +66,12 @@ void mbedtls_cf_uint32_cond_assign( uint32_t * dest,
const uint32_t src,
uint32_t condition );

#if defined(MBEDTLS_BIGNUM_C)
void mbedtls_cf_mpi_uint_cond_assign( size_t n,
mbedtls_mpi_uint *dest,
const mbedtls_mpi_uint *src,
unsigned char condition );
unsigned char assign );
#endif /* MBEDTLS_BIGNUM_C */

unsigned char mbedtls_cf_uchar_table_lookup( const unsigned char * const table,
const size_t table_size,
Expand All @@ -80,7 +86,7 @@ void mbedtls_cf_memcpy_if_eq( unsigned char *dest,
size_t len,
size_t c1, size_t c2 );

#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
#if defined(MBEDTLS_SSL_TLS_C)

int mbedtls_ssl_cf_hmac(
mbedtls_md_context_t *ctx,
Expand All @@ -95,4 +101,4 @@ void mbedtls_ssl_cf_memcpy_offset( unsigned char *dst,
size_t offset_min, size_t offset_max,
size_t len );

#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
#endif /* MBEDTLS_SSL_TLS_C */

0 comments on commit a283f18

Please sign in to comment.