2022-09-30 13:56:54 +02:00
|
|
|
/* BEGIN_HEADER */
|
|
|
|
#include "mbedtls/bignum.h"
|
|
|
|
#include "mbedtls/entropy.h"
|
2022-10-19 17:43:22 +02:00
|
|
|
#include "bignum_core.h"
|
2022-09-30 13:56:54 +02:00
|
|
|
#include "bignum_mod_raw.h"
|
|
|
|
#include "constant_time_internal.h"
|
|
|
|
#include "test/constant_flow.h"
|
|
|
|
|
|
|
|
/* END_HEADER */
|
|
|
|
|
|
|
|
/* BEGIN_DEPENDENCIES
|
|
|
|
* depends_on:MBEDTLS_BIGNUM_C
|
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_mod_raw_io( data_t *input, int nb_int, int nx_32_int,
|
|
|
|
int iendian, int iret, int oret )
|
|
|
|
{
|
2022-10-20 17:57:10 +02:00
|
|
|
mbedtls_mpi_mod_modulus m;
|
|
|
|
mbedtls_mpi_mod_modulus_init( &m );
|
|
|
|
|
2022-09-30 13:56:54 +02:00
|
|
|
if( iret != 0 )
|
|
|
|
TEST_ASSERT( oret == 0 );
|
|
|
|
|
|
|
|
TEST_LE_S( 0, nb_int );
|
|
|
|
size_t nb = nb_int;
|
|
|
|
|
|
|
|
unsigned char buf[1024];
|
|
|
|
TEST_LE_U( nb, sizeof( buf ) );
|
|
|
|
|
|
|
|
/* nx_32_int is the number of 32 bit limbs, if we have 64 bit limbs we need
|
|
|
|
* to halve the number of limbs to have the same size. */
|
|
|
|
size_t nx;
|
|
|
|
TEST_LE_S( 0, nx_32_int );
|
|
|
|
if( sizeof( mbedtls_mpi_uint ) == 8 )
|
|
|
|
nx = nx_32_int / 2 + nx_32_int % 2;
|
|
|
|
else
|
|
|
|
nx = nx_32_int;
|
|
|
|
|
|
|
|
mbedtls_mpi_uint X[sizeof( buf ) / sizeof( mbedtls_mpi_uint )];
|
|
|
|
TEST_LE_U( nx, sizeof( X ) / sizeof( X[0] ) );
|
|
|
|
|
|
|
|
int endian;
|
|
|
|
if( iendian == MBEDTLS_MPI_MOD_EXT_REP_INVALID )
|
|
|
|
endian = MBEDTLS_MPI_MOD_EXT_REP_LE;
|
|
|
|
else
|
|
|
|
endian = iendian;
|
|
|
|
|
|
|
|
mbedtls_mpi_uint init[sizeof( X ) / sizeof( X[0] )];
|
|
|
|
memset( init, 0xFF, sizeof( init ) );
|
|
|
|
int ret = mbedtls_mpi_mod_modulus_setup( &m, init, nx, endian,
|
|
|
|
MBEDTLS_MPI_MOD_REP_MONTGOMERY );
|
|
|
|
TEST_EQUAL( ret, 0 );
|
|
|
|
|
|
|
|
if( iendian == MBEDTLS_MPI_MOD_EXT_REP_INVALID && iret != 0 )
|
|
|
|
m.ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID;
|
|
|
|
|
|
|
|
ret = mbedtls_mpi_mod_raw_read( X, &m, input->x, input->len );
|
|
|
|
TEST_EQUAL( ret, iret );
|
|
|
|
|
|
|
|
if( iret == 0 )
|
|
|
|
{
|
|
|
|
if( iendian == MBEDTLS_MPI_MOD_EXT_REP_INVALID && oret != 0 )
|
|
|
|
m.ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID;
|
|
|
|
|
|
|
|
ret = mbedtls_mpi_mod_raw_write( X, &m, buf, nb );
|
|
|
|
TEST_EQUAL( ret, oret );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( iret == 0 ) && ( oret == 0 ) )
|
|
|
|
{
|
|
|
|
if( nb > input->len )
|
|
|
|
{
|
|
|
|
if( endian == MBEDTLS_MPI_MOD_EXT_REP_BE )
|
|
|
|
{
|
|
|
|
size_t leading_zeroes = nb - input->len;
|
|
|
|
TEST_ASSERT( memcmp( buf + nb - input->len, input->x, input->len ) == 0 );
|
|
|
|
for( size_t i = 0; i < leading_zeroes; i++ )
|
|
|
|
TEST_EQUAL( buf[i], 0 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_ASSERT( memcmp( buf, input->x, input->len ) == 0 );
|
|
|
|
for( size_t i = input->len; i < nb; i++ )
|
|
|
|
TEST_EQUAL( buf[i], 0 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( endian == MBEDTLS_MPI_MOD_EXT_REP_BE )
|
|
|
|
{
|
|
|
|
size_t leading_zeroes = input->len - nb;
|
|
|
|
TEST_ASSERT( memcmp( input->x + input->len - nb, buf, nb ) == 0 );
|
|
|
|
for( size_t i = 0; i < leading_zeroes; i++ )
|
|
|
|
TEST_EQUAL( input->x[i], 0 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_ASSERT( memcmp( input->x, buf, nb ) == 0 );
|
|
|
|
for( size_t i = nb; i < input->len; i++ )
|
|
|
|
TEST_EQUAL( input->x[i], 0 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_mpi_mod_modulus_free( &m );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
2022-10-19 16:15:29 +02:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2022-10-19 17:22:15 +02:00
|
|
|
void mpi_mod_raw_cond_assign( char * input_X,
|
|
|
|
char * input_Y,
|
2022-10-19 16:15:29 +02:00
|
|
|
int input_bytes )
|
|
|
|
{
|
|
|
|
mbedtls_mpi_uint *X = NULL;
|
|
|
|
mbedtls_mpi_uint *Y = NULL;
|
|
|
|
mbedtls_mpi_uint *buff_m = NULL;
|
|
|
|
mbedtls_mpi_mod_modulus m;
|
2022-10-19 17:22:15 +02:00
|
|
|
size_t limbs_X;
|
|
|
|
size_t limbs_Y;
|
|
|
|
|
|
|
|
TEST_EQUAL( mbedtls_test_read_mpi_core( &X, &limbs_X, input_X ), 0 );
|
|
|
|
TEST_EQUAL( mbedtls_test_read_mpi_core( &Y, &limbs_Y, input_Y ), 0 );
|
|
|
|
|
2022-10-19 16:15:29 +02:00
|
|
|
size_t limbs = limbs_X;
|
|
|
|
size_t copy_limbs = CHARS_TO_LIMBS( input_bytes );
|
|
|
|
size_t bytes = limbs * sizeof( mbedtls_mpi_uint );
|
|
|
|
size_t copy_bytes = copy_limbs * sizeof( mbedtls_mpi_uint );
|
|
|
|
|
2022-10-20 17:57:10 +02:00
|
|
|
mbedtls_mpi_mod_modulus_init( &m );
|
|
|
|
|
2022-10-19 16:15:29 +02:00
|
|
|
TEST_EQUAL( limbs_X, limbs_Y );
|
|
|
|
TEST_ASSERT( copy_limbs <= limbs );
|
|
|
|
|
2022-10-19 17:22:15 +02:00
|
|
|
ASSERT_ALLOC( buff_m, copy_limbs );
|
|
|
|
memset( buff_m, 0xFF, copy_limbs );
|
|
|
|
TEST_EQUAL( mbedtls_mpi_mod_modulus_setup(
|
2022-10-19 16:15:29 +02:00
|
|
|
&m, buff_m, copy_limbs,
|
|
|
|
MBEDTLS_MPI_MOD_EXT_REP_BE,
|
2022-10-19 17:22:15 +02:00
|
|
|
MBEDTLS_MPI_MOD_REP_MONTGOMERY ), 0 );
|
2022-10-19 16:15:29 +02:00
|
|
|
|
|
|
|
/* condition is false */
|
|
|
|
TEST_CF_SECRET( X, bytes );
|
|
|
|
TEST_CF_SECRET( Y, bytes );
|
|
|
|
|
|
|
|
mbedtls_mpi_mod_raw_cond_assign( X, Y, &m, 0 );
|
|
|
|
|
2022-10-20 12:27:36 +02:00
|
|
|
TEST_CF_PUBLIC( X, bytes );
|
|
|
|
TEST_CF_PUBLIC( Y, bytes );
|
|
|
|
|
2022-10-19 16:15:29 +02:00
|
|
|
TEST_ASSERT( memcmp( X, Y, bytes ) != 0 );
|
|
|
|
|
|
|
|
/* condition is true */
|
|
|
|
TEST_CF_SECRET( X, bytes );
|
|
|
|
TEST_CF_SECRET( Y, bytes );
|
|
|
|
|
|
|
|
mbedtls_mpi_mod_raw_cond_assign( X, Y, &m, 1 );
|
|
|
|
|
2022-10-20 12:27:36 +02:00
|
|
|
TEST_CF_PUBLIC( X, bytes );
|
|
|
|
TEST_CF_PUBLIC( Y, bytes );
|
|
|
|
|
2022-10-19 16:15:29 +02:00
|
|
|
/* Check if the given length is copied even it is smaller
|
|
|
|
than the length of the given MPIs. */
|
|
|
|
if( copy_limbs <limbs )
|
|
|
|
{
|
|
|
|
ASSERT_COMPARE( X, copy_bytes, Y, copy_bytes );
|
|
|
|
TEST_ASSERT( memcmp( X, Y, bytes ) != 0 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ASSERT_COMPARE( X, bytes, Y, bytes );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_free( X );
|
|
|
|
mbedtls_free( Y );
|
|
|
|
|
|
|
|
mbedtls_mpi_mod_modulus_free( &m );
|
|
|
|
mbedtls_free( buff_m );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2022-10-19 17:22:15 +02:00
|
|
|
void mpi_mod_raw_cond_swap( char * input_X,
|
|
|
|
char * input_Y,
|
2022-10-19 16:15:29 +02:00
|
|
|
int input_bytes )
|
|
|
|
{
|
|
|
|
mbedtls_mpi_uint *tmp_X = NULL;
|
|
|
|
mbedtls_mpi_uint *tmp_Y = NULL;
|
|
|
|
mbedtls_mpi_uint *X = NULL;
|
|
|
|
mbedtls_mpi_uint *Y = NULL;
|
|
|
|
mbedtls_mpi_uint *buff_m = NULL;
|
|
|
|
mbedtls_mpi_mod_modulus m;
|
2022-10-19 17:22:15 +02:00
|
|
|
size_t limbs_X;
|
|
|
|
size_t limbs_Y;
|
|
|
|
|
|
|
|
TEST_EQUAL( mbedtls_test_read_mpi_core( &tmp_X, &limbs_X, input_X ), 0 );
|
|
|
|
TEST_EQUAL( mbedtls_test_read_mpi_core( &tmp_Y, &limbs_Y, input_Y ), 0 );
|
|
|
|
|
2022-10-19 16:15:29 +02:00
|
|
|
size_t limbs = limbs_X;
|
|
|
|
size_t copy_limbs = CHARS_TO_LIMBS( input_bytes );
|
|
|
|
size_t bytes = limbs * sizeof( mbedtls_mpi_uint );
|
|
|
|
size_t copy_bytes = copy_limbs * sizeof( mbedtls_mpi_uint );
|
|
|
|
|
2022-10-20 17:57:10 +02:00
|
|
|
mbedtls_mpi_mod_modulus_init( &m );
|
|
|
|
|
2022-10-19 16:15:29 +02:00
|
|
|
TEST_EQUAL( limbs_X, limbs_Y );
|
|
|
|
TEST_ASSERT( copy_limbs <= limbs );
|
|
|
|
|
|
|
|
ASSERT_ALLOC( buff_m, copy_limbs );
|
2022-10-19 17:22:15 +02:00
|
|
|
memset( buff_m, 0xFF, copy_limbs );
|
|
|
|
TEST_EQUAL( mbedtls_mpi_mod_modulus_setup(
|
2022-10-19 16:15:29 +02:00
|
|
|
&m, buff_m, copy_limbs,
|
|
|
|
MBEDTLS_MPI_MOD_EXT_REP_BE,
|
2022-10-19 17:22:15 +02:00
|
|
|
MBEDTLS_MPI_MOD_REP_MONTGOMERY ), 0 );
|
2022-10-19 16:15:29 +02:00
|
|
|
|
|
|
|
ASSERT_ALLOC( X, limbs );
|
|
|
|
memcpy( X, tmp_X, bytes );
|
|
|
|
|
|
|
|
ASSERT_ALLOC( Y, bytes );
|
|
|
|
memcpy( Y, tmp_Y, bytes );
|
|
|
|
|
|
|
|
/* condition is false */
|
|
|
|
TEST_CF_SECRET( X, bytes );
|
|
|
|
TEST_CF_SECRET( Y, bytes );
|
|
|
|
|
|
|
|
mbedtls_mpi_mod_raw_cond_swap( X, Y, &m, 0 );
|
|
|
|
|
2022-10-20 12:27:36 +02:00
|
|
|
TEST_CF_PUBLIC( X, bytes );
|
|
|
|
TEST_CF_PUBLIC( Y, bytes );
|
|
|
|
|
2022-10-19 16:15:29 +02:00
|
|
|
ASSERT_COMPARE( X, bytes, tmp_X, bytes );
|
|
|
|
ASSERT_COMPARE( Y, bytes, tmp_Y, bytes );
|
|
|
|
|
|
|
|
/* condition is true */
|
|
|
|
TEST_CF_SECRET( X, bytes );
|
|
|
|
TEST_CF_SECRET( Y, bytes );
|
|
|
|
|
|
|
|
mbedtls_mpi_mod_raw_cond_swap( X, Y, &m, 1 );
|
|
|
|
|
2022-10-20 12:27:36 +02:00
|
|
|
TEST_CF_PUBLIC( X, bytes );
|
|
|
|
TEST_CF_PUBLIC( Y, bytes );
|
|
|
|
|
2022-10-19 16:15:29 +02:00
|
|
|
/* Check if the given length is copied even it is smaller
|
|
|
|
than the length of the given MPIs. */
|
|
|
|
if( copy_limbs < limbs )
|
|
|
|
{
|
|
|
|
ASSERT_COMPARE( X, copy_bytes, tmp_Y, copy_bytes );
|
|
|
|
ASSERT_COMPARE( Y, copy_bytes, tmp_X, copy_bytes );
|
|
|
|
TEST_ASSERT( memcmp( X, tmp_X, bytes ) != 0 );
|
|
|
|
TEST_ASSERT( memcmp( X, tmp_Y, bytes ) != 0 );
|
|
|
|
TEST_ASSERT( memcmp( Y, tmp_X, bytes ) != 0 );
|
|
|
|
TEST_ASSERT( memcmp( Y, tmp_Y, bytes ) != 0 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ASSERT_COMPARE( X, bytes, tmp_Y, bytes );
|
|
|
|
ASSERT_COMPARE( Y, bytes, tmp_X, bytes );
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_free( tmp_X );
|
|
|
|
mbedtls_free( tmp_Y );
|
|
|
|
mbedtls_free( X );
|
|
|
|
mbedtls_free( Y );
|
|
|
|
|
|
|
|
mbedtls_mpi_mod_modulus_free( &m );
|
|
|
|
mbedtls_free( buff_m );
|
|
|
|
}
|
2022-11-02 17:14:08 +01:00
|
|
|
/* END_CASE */
|
2022-11-02 15:35:17 +01:00
|
|
|
|
|
|
|
/* BEGIN MERGE SLOT 1 */
|
|
|
|
|
|
|
|
/* END MERGE SLOT 1 */
|
|
|
|
|
|
|
|
/* BEGIN MERGE SLOT 2 */
|
|
|
|
|
|
|
|
/* END MERGE SLOT 2 */
|
|
|
|
|
|
|
|
/* BEGIN MERGE SLOT 3 */
|
|
|
|
|
|
|
|
/* END MERGE SLOT 3 */
|
|
|
|
|
|
|
|
/* BEGIN MERGE SLOT 4 */
|
|
|
|
|
|
|
|
/* END MERGE SLOT 4 */
|
|
|
|
|
|
|
|
/* BEGIN MERGE SLOT 5 */
|
|
|
|
|
|
|
|
/* END MERGE SLOT 5 */
|
|
|
|
|
|
|
|
/* BEGIN MERGE SLOT 6 */
|
|
|
|
|
|
|
|
/* END MERGE SLOT 6 */
|
|
|
|
|
|
|
|
/* BEGIN MERGE SLOT 7 */
|
2022-11-07 16:53:23 +01:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_mod_raw_to_mont_rep( char * input_N, char * input_A, char * input_X )
|
|
|
|
{
|
|
|
|
mbedtls_mpi N, A, X;
|
|
|
|
mbedtls_mpi_mod_modulus m;
|
|
|
|
|
|
|
|
mbedtls_mpi_init( &N );
|
|
|
|
mbedtls_mpi_init( &A );
|
|
|
|
mbedtls_mpi_init( &X );
|
|
|
|
|
|
|
|
/* Read inputs */
|
|
|
|
TEST_EQUAL( 0, mbedtls_test_read_mpi( &N, input_N ) );
|
|
|
|
TEST_EQUAL( 0, mbedtls_test_read_mpi( &A, input_A ) );
|
|
|
|
TEST_EQUAL( 0, mbedtls_test_read_mpi( &X, input_X ) );
|
|
|
|
|
|
|
|
/* All of the inputs are +ve (or zero) */
|
|
|
|
TEST_EQUAL( 1, X.s );
|
|
|
|
TEST_EQUAL( 1, A.s );
|
|
|
|
|
|
|
|
mbedtls_mpi_mod_modulus_init( &m );
|
|
|
|
TEST_EQUAL( 0, mbedtls_mpi_mod_modulus_setup( &m, N.p, N.n, MBEDTLS_MPI_MOD_EXT_REP_BE, MBEDTLS_MPI_MOD_REP_MONTGOMERY ) );
|
2022-11-02 15:35:17 +01:00
|
|
|
|
2022-11-07 16:53:23 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_mpi_mod_raw_to_mont_rep( A.p ,&m ) );
|
|
|
|
|
|
|
|
/* Calculated matches expected value */
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &A, &X ) == 0 );
|
|
|
|
|
|
|
|
/* Output is +ve (or zero) */
|
|
|
|
TEST_EQUAL( 1, A.s );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_mpi_mod_modulus_free( &m );
|
|
|
|
mbedtls_mpi_free( &N );
|
|
|
|
mbedtls_mpi_free( &A );
|
|
|
|
mbedtls_mpi_free( &X );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
2022-11-02 15:35:17 +01:00
|
|
|
/* END MERGE SLOT 7 */
|
|
|
|
|
|
|
|
/* BEGIN MERGE SLOT 8 */
|
|
|
|
|
|
|
|
/* END MERGE SLOT 8 */
|
|
|
|
|
|
|
|
/* BEGIN MERGE SLOT 9 */
|
|
|
|
|
|
|
|
/* END MERGE SLOT 9 */
|
|
|
|
|
|
|
|
/* BEGIN MERGE SLOT 10 */
|
|
|
|
|
|
|
|
/* END MERGE SLOT 10 */
|