mirror of
https://git.suyu.dev/suyu/mbedtls.git
synced 2026-01-06 06:28:56 +01:00
Change mbedtls_mpi_cmp_mpi_ct to check less than
The signature of mbedtls_mpi_cmp_mpi_ct() meant to support using it in place of mbedtls_mpi_cmp_mpi(). This meant full comparison functionality and a signed result. To make the function more universal and friendly to constant time coding, we change the result type to unsigned. Theoretically, we could encode the comparison result in an unsigned value, but it would be less intuitive. Therefore we won't be able to represent the result as unsigned anymore and the functionality will be constrained to checking if the first operand is less than the second. This is sufficient to support the current use case and to check any relationship between MPIs. The only drawback is that we need to call the function twice when checking for equality, but this can be optimised later if an when it is needed.
This commit is contained in:
parent
4f6cf38016
commit
867a3abff5
5 changed files with 71 additions and 70 deletions
|
|
@ -163,38 +163,38 @@ mbedtls_mpi_cmp_mpi:10:"2":10:"-3":1
|
|||
Base test mbedtls_mpi_cmp_mpi (Mixed values) #6
|
||||
mbedtls_mpi_cmp_mpi:10:"-2":10:"31231231289798":-1
|
||||
|
||||
Base test mbedtls_mpi_cmp_mpi_ct #1
|
||||
mbedtls_mpi_cmp_mpi_ct:1:10:"693":1:10:"693":0:0
|
||||
Base test mbedtls_mpi_lt_mpi_ct #1
|
||||
mbedtls_mpi_lt_mpi_ct:1:10:"693":1:10:"693":0:0
|
||||
|
||||
Base test mbedtls_mpi_cmp_mpi_ct #2
|
||||
mbedtls_mpi_cmp_mpi_ct:1:10:"693":1:10:"692":1:0
|
||||
Base test mbedtls_mpi_lt_mpi_ct #2
|
||||
mbedtls_mpi_lt_mpi_ct:1:10:"693":1:10:"692":0:0
|
||||
|
||||
Base test mbedtls_mpi_cmp_mpi_ct #3
|
||||
mbedtls_mpi_cmp_mpi_ct:1:10:"693":1:10:"694":-1:0
|
||||
Base test mbedtls_mpi_lt_mpi_ct #3
|
||||
mbedtls_mpi_lt_mpi_ct:1:10:"693":1:10:"694":1:0
|
||||
|
||||
Base test mbedtls_mpi_cmp_mpi_ct (Negative values) #1
|
||||
mbedtls_mpi_cmp_mpi_ct:1:10:"-2":1:10:"-2":0:0
|
||||
Base test mbedtls_mpi_lt_mpi_ct (Negative values) #1
|
||||
mbedtls_mpi_lt_mpi_ct:1:10:"-2":1:10:"-2":0:0
|
||||
|
||||
Base test mbedtls_mpi_cmp_mpi_ct (Negative values) #2
|
||||
mbedtls_mpi_cmp_mpi_ct:1:10:"-2":1:10:"-3":1:0
|
||||
Base test mbedtls_mpi_lt_mpi_ct (Negative values) #2
|
||||
mbedtls_mpi_lt_mpi_ct:1:10:"-2":1:10:"-3":0:0
|
||||
|
||||
Base test mbedtls_mpi_cmp_mpi_ct (Negative values) #3
|
||||
mbedtls_mpi_cmp_mpi_ct:1:10:"-2":1:10:"-1":-1:0
|
||||
Base test mbedtls_mpi_lt_mpi_ct (Negative values) #3
|
||||
mbedtls_mpi_lt_mpi_ct:1:10:"-2":1:10:"-1":1:0
|
||||
|
||||
Base test mbedtls_mpi_cmp_mpi_ct (Mixed values) #4
|
||||
mbedtls_mpi_cmp_mpi_ct:1:10:"-3":1:10:"2":-1:0
|
||||
Base test mbedtls_mpi_lt_mpi_ct (Mixed values) #4
|
||||
mbedtls_mpi_lt_mpi_ct:1:10:"-3":1:10:"2":1:0
|
||||
|
||||
Base test mbedtls_mpi_cmp_mpi_ct (Mixed values) #5
|
||||
mbedtls_mpi_cmp_mpi_ct:1:10:"2":1:10:"-3":1:0
|
||||
Base test mbedtls_mpi_lt_mpi_ct (Mixed values) #5
|
||||
mbedtls_mpi_lt_mpi_ct:1:10:"2":1:10:"-3":0:0
|
||||
|
||||
Base test mbedtls_mpi_cmp_mpi_ct (Mixed values) #6
|
||||
mbedtls_mpi_cmp_mpi_ct:2:10:"-2":2:10:"31231231289798":-1:0
|
||||
Base test mbedtls_mpi_lt_mpi_ct (Mixed values) #6
|
||||
mbedtls_mpi_lt_mpi_ct:2:10:"-2":2:10:"31231231289798":1:0
|
||||
|
||||
Base test mbedtls_mpi_cmp_mpi_ct (X is longer in storage) #7
|
||||
mbedtls_mpi_cmp_mpi_ct:3:10:"693":2:10:"693":0:MBEDTLS_ERR_MPI_BAD_INPUT_DATA
|
||||
Base test mbedtls_mpi_lt_mpi_ct (X is longer in storage) #7
|
||||
mbedtls_mpi_lt_mpi_ct:3:10:"693":2:10:"693":0:MBEDTLS_ERR_MPI_BAD_INPUT_DATA
|
||||
|
||||
Base test mbedtls_mpi_cmp_mpi_ct (Y is longer in storage) #8
|
||||
mbedtls_mpi_cmp_mpi_ct:3:10:"693":4:10:"693":0:MBEDTLS_ERR_MPI_BAD_INPUT_DATA
|
||||
Base test mbedtls_mpi_lt_mpi_ct (Y is longer in storage) #8
|
||||
mbedtls_mpi_lt_mpi_ct:3:10:"693":4:10:"693":0:MBEDTLS_ERR_MPI_BAD_INPUT_DATA
|
||||
|
||||
Base test mbedtls_mpi_cmp_abs #1
|
||||
mbedtls_mpi_cmp_abs:10:"693":10:"693":0
|
||||
|
|
|
|||
|
|
@ -538,10 +538,12 @@ exit:
|
|||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void mbedtls_mpi_cmp_mpi_ct( int size_X, int radix_X, char * input_X, int size_Y,
|
||||
int radix_Y, char * input_Y, int input_ret, int input_err )
|
||||
void mbedtls_mpi_lt_mpi_ct( int size_X, int radix_X, char * input_X,
|
||||
int size_Y, int radix_Y, char * input_Y,
|
||||
int input_ret, int input_err )
|
||||
{
|
||||
int ret;
|
||||
unsigned ret;
|
||||
unsigned input_uret = input_ret;
|
||||
mbedtls_mpi X, Y;
|
||||
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
|
||||
|
||||
|
|
@ -551,9 +553,9 @@ void mbedtls_mpi_cmp_mpi_ct( int size_X, int radix_X, char * input_X, int size_Y
|
|||
mbedtls_mpi_grow( &X, size_X );
|
||||
mbedtls_mpi_grow( &Y, size_Y );
|
||||
|
||||
TEST_ASSERT( mbedtls_mpi_cmp_mpi_ct( &X, &Y, &ret ) == input_err );
|
||||
TEST_ASSERT( mbedtls_mpi_lt_mpi_ct( &X, &Y, &ret ) == input_err );
|
||||
if( input_err == 0 )
|
||||
TEST_ASSERT( ret == input_ret );
|
||||
TEST_ASSERT( ret == input_uret );
|
||||
|
||||
exit:
|
||||
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue