2022-09-30 14:02:16 +02:00
|
|
|
/* BEGIN_HEADER */
|
|
|
|
#include "mbedtls/bignum.h"
|
|
|
|
#include "mbedtls/entropy.h"
|
|
|
|
#include "bignum_mod.h"
|
|
|
|
#include "constant_time_internal.h"
|
|
|
|
#include "test/constant_flow.h"
|
|
|
|
/* END_HEADER */
|
|
|
|
|
|
|
|
/* BEGIN_DEPENDENCIES
|
|
|
|
* depends_on:MBEDTLS_BIGNUM_C
|
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2022-11-24 19:20:26 +01:00
|
|
|
void mpi_mod_setup( int int_rep, int iret )
|
2022-09-30 14:02:16 +02:00
|
|
|
{
|
|
|
|
#define MLIMBS 8
|
|
|
|
mbedtls_mpi_uint mp[MLIMBS];
|
|
|
|
mbedtls_mpi_mod_modulus m;
|
|
|
|
int ret;
|
|
|
|
|
2022-10-27 16:58:02 +02:00
|
|
|
memset( mp, 0xFF, sizeof(mp) );
|
2022-09-30 14:02:16 +02:00
|
|
|
|
|
|
|
mbedtls_mpi_mod_modulus_init( &m );
|
2022-11-24 19:20:26 +01:00
|
|
|
ret = mbedtls_mpi_mod_modulus_setup( &m, mp, MLIMBS, int_rep );
|
2022-09-30 14:02:16 +02:00
|
|
|
TEST_EQUAL( ret, iret );
|
|
|
|
|
2022-10-19 02:48:32 +02:00
|
|
|
/* Only test if the constants have been set-up */
|
|
|
|
if ( ret == 0 && int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY )
|
|
|
|
{
|
|
|
|
/* Test that the consts have been calculated */
|
|
|
|
TEST_ASSERT( m.rep.mont.rr != NULL );
|
|
|
|
TEST_ASSERT( m.rep.mont.mm != 0 );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-09-30 14:02:16 +02:00
|
|
|
/* Address sanitiser should catch if we try to free mp */
|
|
|
|
mbedtls_mpi_mod_modulus_free( &m );
|
|
|
|
|
|
|
|
/* Make sure that the modulus doesn't have reference to mp anymore */
|
|
|
|
TEST_ASSERT( m.p != mp );
|
|
|
|
|
2022-10-19 02:48:32 +02:00
|
|
|
/* Only test if the constants have been set-up */
|
|
|
|
if ( ret == 0 && int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY )
|
|
|
|
{
|
|
|
|
/* Verify the data and pointers allocated have been properly wiped */
|
|
|
|
TEST_ASSERT( m.rep.mont.rr == NULL );
|
|
|
|
TEST_ASSERT( m.rep.mont.mm == 0 );
|
|
|
|
}
|
2022-09-30 14:02:16 +02:00
|
|
|
exit:
|
|
|
|
/* It should be safe to call an mbedtls free several times */
|
|
|
|
mbedtls_mpi_mod_modulus_free( &m );
|
|
|
|
|
|
|
|
#undef MLIMBS
|
|
|
|
}
|
|
|
|
/* 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-10 17:56:02 +01:00
|
|
|
/* BEGIN_CASE */
|
2022-11-26 12:47:14 +01:00
|
|
|
void mpi_residue_setup( char * input_N, char * input_R, int ret )
|
2022-11-16 17:29:15 +01:00
|
|
|
{
|
|
|
|
mbedtls_mpi_uint *N = NULL;
|
|
|
|
mbedtls_mpi_uint *R = NULL;
|
2022-11-24 10:09:47 +01:00
|
|
|
size_t n_limbs, r_limbs;
|
2022-11-16 17:29:15 +01:00
|
|
|
mbedtls_mpi_mod_modulus m;
|
|
|
|
mbedtls_mpi_mod_residue r;
|
|
|
|
|
|
|
|
mbedtls_mpi_mod_modulus_init( &m );
|
|
|
|
|
2022-11-24 10:09:47 +01:00
|
|
|
/* Allocate the memory for intermediate data structures */
|
2022-11-26 12:47:14 +01:00
|
|
|
TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &N, &n_limbs, input_N ) );
|
|
|
|
TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &R, &r_limbs, input_R ) );
|
2022-11-24 10:09:47 +01:00
|
|
|
|
2022-11-16 17:29:15 +01:00
|
|
|
TEST_EQUAL( 0, mbedtls_mpi_mod_modulus_setup( &m, N, n_limbs,
|
2022-11-24 19:20:26 +01:00
|
|
|
MBEDTLS_MPI_MOD_REP_MONTGOMERY ) );
|
2022-11-16 17:29:15 +01:00
|
|
|
|
2022-11-24 10:09:47 +01:00
|
|
|
TEST_EQUAL( ret, mbedtls_mpi_mod_residue_setup( &r, &m, R , r_limbs ) );
|
2022-11-16 17:29:15 +01:00
|
|
|
|
2022-11-26 12:47:14 +01:00
|
|
|
if ( ret == 0 )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( r.limbs, r_limbs );
|
|
|
|
TEST_ASSERT( r.p == R );
|
|
|
|
}
|
|
|
|
|
2022-11-16 17:29:15 +01:00
|
|
|
exit:
|
|
|
|
mbedtls_mpi_mod_modulus_free( &m );
|
|
|
|
mbedtls_free( N );
|
|
|
|
mbedtls_free( R );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
2022-11-24 10:09:47 +01:00
|
|
|
|
2022-11-16 17:29:15 +01:00
|
|
|
/* BEGIN_CASE */
|
2022-11-26 13:20:41 +01:00
|
|
|
void mpi_mod_io_neg( char * input_N, data_t * buf, int ret )
|
2022-11-10 17:56:02 +01:00
|
|
|
{
|
|
|
|
mbedtls_mpi_uint *N = NULL;
|
|
|
|
mbedtls_mpi_uint *R = NULL;
|
|
|
|
|
|
|
|
mbedtls_mpi_mod_modulus m;
|
|
|
|
mbedtls_mpi_mod_residue r;
|
|
|
|
mbedtls_mpi_mod_residue rn = { NULL, 0 };
|
2022-11-25 20:32:10 +01:00
|
|
|
mbedtls_mpi_mod_ext_rep endian = MBEDTLS_MPI_MOD_EXT_REP_LE;
|
2022-11-10 17:56:02 +01:00
|
|
|
|
2022-11-25 16:57:04 +01:00
|
|
|
mbedtls_mpi_mod_modulus_init( &m );
|
|
|
|
|
2022-11-26 13:20:41 +01:00
|
|
|
size_t n_limbs;
|
2022-11-25 20:32:10 +01:00
|
|
|
TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &N, &n_limbs, input_N ) );
|
2022-11-26 13:20:41 +01:00
|
|
|
size_t r_limbs = n_limbs;
|
|
|
|
ASSERT_ALLOC( R, r_limbs );
|
2022-11-10 17:56:02 +01:00
|
|
|
|
2022-11-25 20:32:10 +01:00
|
|
|
/* modulo->p == NULL || residue->p == NULL ( m has not been set-up ) */
|
|
|
|
TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
|
2022-11-26 13:05:50 +01:00
|
|
|
mbedtls_mpi_mod_read( &r, &m, buf->x, buf->len, endian ) );
|
2022-11-10 17:56:02 +01:00
|
|
|
|
2022-11-25 20:32:10 +01:00
|
|
|
TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
|
2022-11-26 13:05:50 +01:00
|
|
|
mbedtls_mpi_mod_write( &r, &m, buf->x, buf->len, endian ) );
|
2022-11-10 17:56:02 +01:00
|
|
|
|
2022-11-25 20:32:10 +01:00
|
|
|
TEST_EQUAL( 0, mbedtls_mpi_mod_modulus_setup( &m, N, n_limbs,
|
|
|
|
MBEDTLS_MPI_MOD_REP_MONTGOMERY ) );
|
2022-11-26 13:20:41 +01:00
|
|
|
TEST_EQUAL( 0, mbedtls_mpi_mod_residue_setup( &r, &m, R , r_limbs ) );
|
2022-11-10 17:56:02 +01:00
|
|
|
|
2022-11-25 20:32:10 +01:00
|
|
|
/* modulo->p == NULL || residue->p == NULL ( m has been set-up ) */
|
2022-11-24 19:02:46 +01:00
|
|
|
TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
|
2022-11-26 13:05:50 +01:00
|
|
|
mbedtls_mpi_mod_read( &rn, &m, buf->x, buf->len, endian ) );
|
2022-11-24 19:02:46 +01:00
|
|
|
TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
|
2022-11-26 13:05:50 +01:00
|
|
|
mbedtls_mpi_mod_write( &rn, &m, buf->x, buf->len, endian ) );
|
2022-11-10 17:56:02 +01:00
|
|
|
|
2022-11-26 15:59:27 +01:00
|
|
|
/* Fail for r_limbs < m->limbs */
|
|
|
|
r.limbs--;
|
|
|
|
TEST_ASSERT( r.limbs < m.limbs );
|
|
|
|
TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
|
|
|
|
mbedtls_mpi_mod_read( &r, &m, buf->x, buf->len, endian ) );
|
|
|
|
TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
|
|
|
|
mbedtls_mpi_mod_write( &r, &m, buf->x, buf->len, endian ) );
|
|
|
|
r.limbs++;
|
|
|
|
|
2022-11-25 20:32:10 +01:00
|
|
|
/* Fail for r_limbs > m->limbs */
|
2022-11-26 15:59:27 +01:00
|
|
|
m.limbs--;
|
|
|
|
TEST_ASSERT( r.limbs > m.limbs );
|
2022-11-24 19:02:46 +01:00
|
|
|
TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
|
2022-11-26 13:05:50 +01:00
|
|
|
mbedtls_mpi_mod_read( &r, &m, buf->x, buf->len, endian ) );
|
2022-11-24 19:02:46 +01:00
|
|
|
TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
|
2022-11-26 13:05:50 +01:00
|
|
|
mbedtls_mpi_mod_write( &r, &m, buf->x, buf->len, endian ) );
|
2022-11-26 15:59:27 +01:00
|
|
|
m.limbs++;
|
2022-11-10 17:56:02 +01:00
|
|
|
|
2022-11-25 20:32:10 +01:00
|
|
|
/* Test the read */
|
2022-11-26 13:05:50 +01:00
|
|
|
TEST_EQUAL( ret, mbedtls_mpi_mod_read( &r, &m, buf->x, buf->len, endian ) );
|
2022-11-02 15:35:17 +01:00
|
|
|
|
2022-11-25 20:32:10 +01:00
|
|
|
/* Test write overflow only when the representation is large and read is successful */
|
2022-11-26 15:19:02 +01:00
|
|
|
if ( r.limbs > 1 && ret == 0 )
|
2022-11-25 20:32:10 +01:00
|
|
|
TEST_EQUAL( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL,
|
2022-11-26 13:05:50 +01:00
|
|
|
mbedtls_mpi_mod_write( &r, &m, buf->x, 1, endian ) );
|
2022-11-26 15:59:27 +01:00
|
|
|
|
2022-11-10 17:56:02 +01:00
|
|
|
exit:
|
2022-11-26 15:59:27 +01:00
|
|
|
mbedtls_mpi_mod_residue_release( &r );
|
2022-11-10 17:56:02 +01:00
|
|
|
mbedtls_mpi_mod_modulus_free( &m );
|
|
|
|
mbedtls_free( N );
|
|
|
|
mbedtls_free( R );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2022-11-24 19:02:46 +01:00
|
|
|
void mpi_mod_io( char * input_N, data_t * input_A, int endian )
|
2022-11-10 17:56:02 +01:00
|
|
|
{
|
|
|
|
mbedtls_mpi_uint *N = NULL;
|
|
|
|
mbedtls_mpi_uint *R = NULL;
|
2022-11-26 16:39:02 +01:00
|
|
|
mbedtls_mpi_uint *R_COPY = NULL;
|
2022-11-26 18:23:16 +01:00
|
|
|
unsigned char *obuf = NULL;
|
|
|
|
unsigned char *ref_buf = NULL;
|
2022-11-10 17:56:02 +01:00
|
|
|
mbedtls_mpi_mod_modulus m;
|
|
|
|
mbedtls_mpi_mod_residue r;
|
2022-11-26 16:39:02 +01:00
|
|
|
mbedtls_mpi_mod_residue r_copy;
|
2022-11-10 17:56:02 +01:00
|
|
|
size_t n_limbs, n_bytes, a_bytes;
|
|
|
|
|
2022-11-25 16:57:04 +01:00
|
|
|
mbedtls_mpi_mod_modulus_init( &m );
|
|
|
|
|
2022-11-10 17:56:02 +01:00
|
|
|
/* Read inputs */
|
|
|
|
TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &N, &n_limbs, input_N ) );
|
|
|
|
n_bytes = n_limbs * sizeof( mbedtls_mpi_uint );
|
2022-11-26 15:19:02 +01:00
|
|
|
a_bytes = input_A->len;
|
2022-11-10 17:56:02 +01:00
|
|
|
|
|
|
|
/* Allocate the memory for intermediate data structures */
|
|
|
|
ASSERT_ALLOC( R, n_bytes );
|
2022-11-26 16:39:02 +01:00
|
|
|
ASSERT_ALLOC( R_COPY, n_bytes );
|
2022-11-10 17:56:02 +01:00
|
|
|
|
|
|
|
/* Test that input's size is not greater to modulo's */
|
2022-11-26 15:19:02 +01:00
|
|
|
TEST_LE_U( a_bytes, n_bytes );
|
2022-11-10 17:56:02 +01:00
|
|
|
|
|
|
|
/* Init Structures */
|
2022-11-24 19:20:26 +01:00
|
|
|
TEST_EQUAL( 0, mbedtls_mpi_mod_modulus_setup( &m, N, n_limbs,
|
2022-11-10 17:56:02 +01:00
|
|
|
MBEDTLS_MPI_MOD_REP_MONTGOMERY ) );
|
|
|
|
|
|
|
|
/* Enforcing p_limbs >= m->limbs */
|
2022-11-24 19:02:46 +01:00
|
|
|
TEST_EQUAL( 0, mbedtls_mpi_mod_residue_setup( &r, &m, R, n_limbs ) );
|
2022-11-10 17:56:02 +01:00
|
|
|
|
2022-11-24 19:02:46 +01:00
|
|
|
TEST_EQUAL( 0, mbedtls_mpi_mod_read( &r, &m, input_A->x, input_A->len,
|
|
|
|
endian ) );
|
2022-11-10 17:56:02 +01:00
|
|
|
|
2022-11-26 18:23:16 +01:00
|
|
|
/* Read a copy for checking that writing didn't change the value of r */
|
|
|
|
TEST_EQUAL( 0, mbedtls_mpi_mod_residue_setup( &r_copy, &m,
|
|
|
|
R_COPY, n_limbs ) );
|
2022-11-26 16:39:02 +01:00
|
|
|
TEST_EQUAL( 0, mbedtls_mpi_mod_read( &r_copy, &m, input_A->x, input_A->len,
|
|
|
|
endian ) );
|
|
|
|
|
2022-11-26 18:23:16 +01:00
|
|
|
/* Get number of bytes without leading zeroes */
|
|
|
|
size_t a_bytes_trimmed = a_bytes;
|
|
|
|
while( a_bytes_trimmed > 0 )
|
|
|
|
{
|
|
|
|
unsigned char* r_byte_array = (unsigned char*) r.p;
|
|
|
|
if( r_byte_array[--a_bytes_trimmed] != 0 )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
a_bytes_trimmed++;
|
|
|
|
|
|
|
|
/* Test write with three output buffer sizes: tight, same as input and
|
|
|
|
* longer than the input */
|
|
|
|
size_t obuf_sizes[3];
|
|
|
|
const size_t obuf_sizes_len = sizeof( obuf_sizes ) / sizeof( obuf_sizes[0] );
|
|
|
|
obuf_sizes[0] = a_bytes_trimmed;
|
|
|
|
obuf_sizes[1] = a_bytes;
|
|
|
|
obuf_sizes[2] = a_bytes + 8;
|
|
|
|
|
|
|
|
for( size_t i = 0; i < obuf_sizes_len; i++ )
|
|
|
|
{
|
|
|
|
ASSERT_ALLOC( obuf, obuf_sizes[i] );
|
|
|
|
TEST_EQUAL( 0, mbedtls_mpi_mod_write( &r, &m, obuf, obuf_sizes[i], endian ) );
|
|
|
|
|
|
|
|
/* Make sure that writing didn't corrupt the value of r */
|
|
|
|
ASSERT_COMPARE( r.p, r.limbs, r_copy.p, r_copy.limbs );
|
|
|
|
|
|
|
|
/* Set up reference output for checking the result */
|
|
|
|
ASSERT_ALLOC( ref_buf, obuf_sizes[i] );
|
|
|
|
switch( endian )
|
|
|
|
{
|
|
|
|
case MBEDTLS_MPI_MOD_EXT_REP_LE:
|
|
|
|
memcpy( ref_buf, input_A->x, a_bytes_trimmed );
|
|
|
|
break;
|
|
|
|
case MBEDTLS_MPI_MOD_EXT_REP_BE:
|
|
|
|
{
|
|
|
|
size_t a_offset = input_A->len - a_bytes_trimmed;
|
|
|
|
size_t ref_offset = obuf_sizes[i] - a_bytes_trimmed;
|
|
|
|
memcpy( ref_buf + ref_offset, input_A->x + a_offset,
|
|
|
|
a_bytes_trimmed );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
TEST_ASSERT( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check the result */
|
|
|
|
ASSERT_COMPARE( obuf, obuf_sizes[i], ref_buf, obuf_sizes[i] );
|
|
|
|
|
|
|
|
mbedtls_free( ref_buf );
|
|
|
|
ref_buf = NULL;
|
|
|
|
mbedtls_free( obuf );
|
|
|
|
obuf = NULL;
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:56:02 +01:00
|
|
|
exit:
|
|
|
|
mbedtls_mpi_mod_modulus_free( &m );
|
|
|
|
mbedtls_free( N );
|
|
|
|
mbedtls_free( R );
|
2022-11-26 16:39:02 +01:00
|
|
|
mbedtls_free( R_COPY );
|
2022-11-26 18:23:16 +01:00
|
|
|
mbedtls_free( obuf );
|
2022-11-10 17:56:02 +01:00
|
|
|
}
|
|
|
|
/* 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 */
|