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 24, 2021
1 parent 6091695 commit 5c29d58
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
20 changes: 16 additions & 4 deletions library/constant_time.c
Original file line number Diff line number Diff line change
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 `cond ? if1 : if0`, but is likely to be compiled
Expand Down Expand Up @@ -343,6 +351,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 @@ -374,6 +384,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 @@ -458,7 +470,7 @@ void mbedtls_cf_mem_move_to_left( void *start,
dst[i] = ( src[i] & mask ) | ( dst[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 @@ -571,7 +583,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 @@ -754,7 +766,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 @@ -914,4 +926,4 @@ int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(
return( ret );
}

#endif /* MBEDTLS_PKCS1_V15 */
#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */
10 changes: 8 additions & 2 deletions library/constant_time.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 cond, unsigned if1, unsigned if0 );

Expand All @@ -60,10 +64,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 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 *dst,

int mbedtls_cf_cond_select_sign( int a, int b, unsigned char second );

#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 5c29d58

Please sign in to comment.