Test mbedtls_mpi_safe_cond_{assign,swap} with the basic functions

Test mbedtls_mpi_safe_cond_assign() and mbedtls_mpi_safe_cond_swap()
with their "unsafe" counterparts mbedtls_mpi_copy() and
mbedtls_mpi_swap(). This way we don't need to repeat the coverage of
test cases.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
Gilles Peskine 2021-06-10 23:00:33 +02:00
parent fc1eeefc99
commit d0722f8e71
2 changed files with 45 additions and 102 deletions

View file

@ -586,42 +586,6 @@ mbedtls_mpi_shrink:4:1:0:1
Shrink 0 limbs in a buffer of size 4 to 0 yielding 1
mbedtls_mpi_shrink:4:0:0:1
Test mbedtls_mpi_safe_cond_assign #1
mbedtls_mpi_safe_cond_assign:+1:"01":+1:"02"
Test mbedtls_mpi_safe_cond_assign #2
mbedtls_mpi_safe_cond_assign:+1:"FF000000000000000001":+1:"02"
Test mbedtls_mpi_safe_cond_assign #3
mbedtls_mpi_safe_cond_assign:+1:"01":+1:"FF000000000000000002"
Test mbedtls_mpi_safe_cond_assign #4
mbedtls_mpi_safe_cond_assign:+1:"01":-1:"02"
Test mbedtls_mpi_safe_cond_assign #5
mbedtls_mpi_safe_cond_assign:-1:"01":+1:"02"
Test mbedtls_mpi_safe_cond_assign #6
mbedtls_mpi_safe_cond_assign:-1:"01":-1:"02"
Test mbedtls_mpi_safe_cond_swap #1
mbedtls_mpi_safe_cond_swap:+1:"01":+1:"02"
Test mbedtls_mpi_safe_cond_swap #2
mbedtls_mpi_safe_cond_swap:+1:"FF000000000000000001":+1:"02"
Test mbedtls_mpi_safe_cond_swap #3
mbedtls_mpi_safe_cond_swap:+1:"01":+1:"FF000000000000000002"
Test mbedtls_mpi_safe_cond_swap #4
mbedtls_mpi_safe_cond_swap:+1:"01":-1:"02"
Test mbedtls_mpi_safe_cond_swap #5
mbedtls_mpi_safe_cond_swap:-1:"01":+1:"02"
Test mbedtls_mpi_safe_cond_swap #6
mbedtls_mpi_safe_cond_swap:-1:"01":-1:"02"
Base test mbedtls_mpi_add_abs #1
mbedtls_mpi_add_abs:10:"12345678":10:"642531":10:"12988209"

View file

@ -503,21 +503,38 @@ exit:
/* BEGIN_CASE */
void mbedtls_mpi_copy( char *src_hex, char *dst_hex )
{
mbedtls_mpi src, dst;
mbedtls_mpi src, dst, ref;
mbedtls_mpi_init( &src );
mbedtls_mpi_init( &dst );
mbedtls_mpi_init( &ref );
TEST_ASSERT( mbedtls_test_read_mpi( &src, 16, src_hex ) == 0 );
TEST_ASSERT( mbedtls_test_read_mpi( &ref, 16, dst_hex ) == 0 );
/* mbedtls_mpi_copy() */
TEST_ASSERT( mbedtls_test_read_mpi( &dst, 16, dst_hex ) == 0 );
TEST_ASSERT( mbedtls_mpi_copy( &dst, &src ) == 0 );
TEST_ASSERT( sign_is_valid( &dst ) );
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &src ) == 0 );
/* mbedtls_mpi_safe_cond_assign(), assignment done */
mbedtls_mpi_free( &dst );
TEST_ASSERT( mbedtls_test_read_mpi( &dst, 16, dst_hex ) == 0 );
TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &dst, &src, 1 ) == 0 );
TEST_ASSERT( sign_is_valid( &dst ) );
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &src ) == 0 );
/* mbedtls_mpi_safe_cond_assign(), assignment not done */
mbedtls_mpi_free( &dst );
TEST_ASSERT( mbedtls_test_read_mpi( &dst, 16, dst_hex ) == 0 );
TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &dst, &src, 0 ) == 0 );
TEST_ASSERT( sign_is_valid( &dst ) );
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &ref ) == 0 );
exit:
mbedtls_mpi_free( &src );
mbedtls_mpi_free( &dst );
mbedtls_mpi_free( &ref );
}
/* END_CASE */
@ -548,17 +565,40 @@ void mbedtls_mpi_swap( char *X_hex, char *Y_hex )
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &Y0 );
TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, X_hex ) == 0 );
TEST_ASSERT( mbedtls_test_read_mpi( &Y, 16, Y_hex ) == 0 );
TEST_ASSERT( mbedtls_test_read_mpi( &X0, 16, X_hex ) == 0 );
TEST_ASSERT( mbedtls_test_read_mpi( &Y0, 16, Y_hex ) == 0 );
/* mbedtls_mpi_swap() */
TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, X_hex ) == 0 );
TEST_ASSERT( mbedtls_test_read_mpi( &Y, 16, Y_hex ) == 0 );
mbedtls_mpi_swap( &X, &Y );
TEST_ASSERT( sign_is_valid( &X ) );
TEST_ASSERT( sign_is_valid( &Y ) );
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
/* mbedtls_mpi_safe_cond_swap(), swap done */
mbedtls_mpi_free( &X );
mbedtls_mpi_free( &Y );
TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, X_hex ) == 0 );
TEST_ASSERT( mbedtls_test_read_mpi( &Y, 16, Y_hex ) == 0 );
TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
TEST_ASSERT( sign_is_valid( &X ) );
TEST_ASSERT( sign_is_valid( &Y ) );
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
/* mbedtls_mpi_safe_cond_swap(), swap not done */
mbedtls_mpi_free( &X );
mbedtls_mpi_free( &Y );
TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, X_hex ) == 0 );
TEST_ASSERT( mbedtls_test_read_mpi( &Y, 16, Y_hex ) == 0 );
TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
TEST_ASSERT( sign_is_valid( &X ) );
TEST_ASSERT( sign_is_valid( &Y ) );
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &Y0 ) == 0 );
exit:
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
mbedtls_mpi_free( &X0 ); mbedtls_mpi_free( &Y0 );
@ -600,67 +640,6 @@ exit:
}
/* END_CASE */
/* BEGIN_CASE */
void mbedtls_mpi_safe_cond_assign( int x_sign, char * x_str, int y_sign,
char * y_str )
{
mbedtls_mpi X, Y, XX;
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &XX );
TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, x_str ) == 0 );
X.s = x_sign;
TEST_ASSERT( mbedtls_test_read_mpi( &Y, 16, y_str ) == 0 );
Y.s = y_sign;
TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 0 ) == 0 );
TEST_ASSERT( sign_is_valid( &X ) );
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 1 ) == 0 );
TEST_ASSERT( sign_is_valid( &X ) );
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
exit:
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &XX );
}
/* END_CASE */
/* BEGIN_CASE */
void mbedtls_mpi_safe_cond_swap( int x_sign, char * x_str, int y_sign,
char * y_str )
{
mbedtls_mpi X, Y, XX, YY;
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
mbedtls_mpi_init( &XX ); mbedtls_mpi_init( &YY );
TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, x_str ) == 0 );
X.s = x_sign;
TEST_ASSERT( mbedtls_test_read_mpi( &Y, 16, y_str ) == 0 );
Y.s = y_sign;
TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
TEST_ASSERT( mbedtls_mpi_copy( &YY, &Y ) == 0 );
TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
TEST_ASSERT( sign_is_valid( &X ) );
TEST_ASSERT( sign_is_valid( &Y ) );
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &YY ) == 0 );
TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
TEST_ASSERT( sign_is_valid( &X ) );
TEST_ASSERT( sign_is_valid( &Y ) );
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &XX ) == 0 );
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &YY ) == 0 );
exit:
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
mbedtls_mpi_free( &XX ); mbedtls_mpi_free( &YY );
}
/* END_CASE */
/* BEGIN_CASE */
void mbedtls_mpi_add_mpi( int radix_X, char * input_X, int radix_Y,
char * input_Y, int radix_A, char * input_A )