Improved on the fix of #309 and extended the test to cover subroutines.

This commit is contained in:
Janos Follath 2015-10-25 12:29:13 +01:00 committed by Manuel Pégourié-Gonnard
parent d0e0c03520
commit 5429c0a7d0
2 changed files with 21 additions and 5 deletions

View File

@ -887,12 +887,19 @@ int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi
if( X == B ) if( X == B )
{ {
const mbedtls_mpi *T;
if( B == A ) if( B == A )
return mbedtls_mpi_shift_l( X, 1 ); {
// Making a temporary copy instead of shifting by one to deny
// the possibility of corresponding side-channel attacks.
mbedtls_mpi TB;
T = A; A = X; B = T; mbedtls_mpi_init( &TB );
MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) );
return mbedtls_mpi_add_abs( X, A, &TB );
}
B = A; A = X;
} }
if( X != A ) if( X != A )

View File

@ -448,8 +448,17 @@ void mbedtls_mpi_add_mpi_inplace( int radix_X, char *input_X, int radix_A, char
mbedtls_mpi X, A; mbedtls_mpi X, A;
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 ); TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );