From 91295d2b8f3a6163b6cf29897548a1779f00b9fb Mon Sep 17 00:00:00 2001 From: Janos Follath Date: Thu, 24 Nov 2022 18:20:26 +0000 Subject: [PATCH] Bignum Mod: remove endianness from modulus The external representation before included more than just endianness (like reading in Mongtomery curve scalars or converting hashes to numbers in a standard compliant way). These are higher level concepts and are out of scope for Bignum and for the modulus structure. Signed-off-by: Janos Follath --- library/bignum_mod.c | 14 --------- library/bignum_mod.h | 4 --- tests/suites/test_suite_bignum_mod.data | 30 ++++--------------- tests/suites/test_suite_bignum_mod.function | 12 ++++---- .../suites/test_suite_bignum_mod_raw.function | 8 ++--- 5 files changed, 15 insertions(+), 53 deletions(-) diff --git a/library/bignum_mod.c b/library/bignum_mod.c index fa4831c7a..3cb3c436d 100644 --- a/library/bignum_mod.c +++ b/library/bignum_mod.c @@ -65,7 +65,6 @@ void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m ) m->p = NULL; m->limbs = 0; m->bits = 0; - m->ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID; m->int_rep = MBEDTLS_MPI_MOD_REP_INVALID; } @@ -96,7 +95,6 @@ void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m ) m->p = NULL; m->limbs = 0; m->bits = 0; - m->ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID; m->int_rep = MBEDTLS_MPI_MOD_REP_INVALID; } @@ -138,7 +136,6 @@ cleanup: int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m, const mbedtls_mpi_uint *p, size_t p_limbs, - mbedtls_mpi_mod_ext_rep ext_rep, mbedtls_mpi_mod_rep_selector int_rep ) { int ret = 0; @@ -147,17 +144,6 @@ int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m, m->limbs = p_limbs; m->bits = mbedtls_mpi_core_bitlen( p, p_limbs ); - switch( ext_rep ) - { - case MBEDTLS_MPI_MOD_EXT_REP_LE: - case MBEDTLS_MPI_MOD_EXT_REP_BE: - m->ext_rep = ext_rep; - break; - default: - ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA; - goto exit; - } - switch( int_rep ) { case MBEDTLS_MPI_MOD_REP_MONTGOMERY: diff --git a/library/bignum_mod.h b/library/bignum_mod.h index e6da15fbc..5f948a499 100644 --- a/library/bignum_mod.h +++ b/library/bignum_mod.h @@ -64,7 +64,6 @@ typedef struct { const mbedtls_mpi_uint *p; size_t limbs; // number of limbs size_t bits; // bitlen of p - mbedtls_mpi_mod_ext_rep ext_rep; // signals external representation (eg. byte order) mbedtls_mpi_mod_rep_selector int_rep; // selector to signal the active member of the union union rep { @@ -122,8 +121,6 @@ void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m ); * not be modified in any way until after * mbedtls_mpi_mod_modulus_free() is called. * \param p_limbs The number of limbs of \p p. - * \param ext_rep The external representation to be used for residues - * associated with \p m (see #mbedtls_mpi_mod_ext_rep). * \param int_rep The internal representation to be used for residues * associated with \p m (see #mbedtls_mpi_mod_rep_selector). * @@ -134,7 +131,6 @@ void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m ); int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m, const mbedtls_mpi_uint *p, size_t p_limbs, - mbedtls_mpi_mod_ext_rep ext_rep, mbedtls_mpi_mod_rep_selector int_rep ); /** Free elements of a modulus structure. diff --git a/tests/suites/test_suite_bignum_mod.data b/tests/suites/test_suite_bignum_mod.data index ba7d5779f..ef9416e16 100644 --- a/tests/suites/test_suite_bignum_mod.data +++ b/tests/suites/test_suite_bignum_mod.data @@ -1,29 +1,11 @@ -Test mbedtls_mpi_mod_setup #1 (Both representations invalid) -mpi_mod_setup:MBEDTLS_MPI_MOD_EXT_REP_INVALID:MBEDTLS_MPI_MOD_REP_INVALID:MBEDTLS_ERR_MPI_BAD_INPUT_DATA +Test mbedtls_mpi_mod_setup #1 (Internal representation invalid) +mpi_mod_setup:MBEDTLS_MPI_MOD_REP_INVALID:MBEDTLS_ERR_MPI_BAD_INPUT_DATA -Test mbedtls_mpi_mod_setup #2 (Internal representation invalid) -mpi_mod_setup:MBEDTLS_MPI_MOD_EXT_REP_LE:MBEDTLS_MPI_MOD_REP_INVALID:MBEDTLS_ERR_MPI_BAD_INPUT_DATA +Test mbedtls_mpi_mod_setup #6 (Optimised reduction) +mpi_mod_setup:MBEDTLS_MPI_MOD_REP_OPT_RED:0 -Test mbedtls_mpi_mod_setup #3 (Internal representation invalid) -mpi_mod_setup:MBEDTLS_MPI_MOD_EXT_REP_BE:MBEDTLS_MPI_MOD_REP_INVALID:MBEDTLS_ERR_MPI_BAD_INPUT_DATA - -Test mbedtls_mpi_mod_setup #4 (External representation invalid) -mpi_mod_setup:MBEDTLS_MPI_MOD_EXT_REP_INVALID:MBEDTLS_MPI_MOD_REP_MONTGOMERY:MBEDTLS_ERR_MPI_BAD_INPUT_DATA - -Test mbedtls_mpi_mod_setup #5 (External representation invalid) -mpi_mod_setup:MBEDTLS_MPI_MOD_EXT_REP_INVALID:MBEDTLS_MPI_MOD_REP_OPT_RED:MBEDTLS_ERR_MPI_BAD_INPUT_DATA - -Test mbedtls_mpi_mod_setup #6 (Both representations valid) -mpi_mod_setup:MBEDTLS_MPI_MOD_EXT_REP_BE:MBEDTLS_MPI_MOD_REP_OPT_RED:0 - -Test mbedtls_mpi_mod_setup #7 (Both representations valid) -mpi_mod_setup:MBEDTLS_MPI_MOD_EXT_REP_BE:MBEDTLS_MPI_MOD_REP_MONTGOMERY:0 - -Test mbedtls_mpi_mod_setup #8 (Both representations valid) -mpi_mod_setup:MBEDTLS_MPI_MOD_EXT_REP_LE:MBEDTLS_MPI_MOD_REP_OPT_RED:0 - -Test mbedtls_mpi_mod_setup #9 (Both representations valid) -mpi_mod_setup:MBEDTLS_MPI_MOD_EXT_REP_LE:MBEDTLS_MPI_MOD_REP_MONTGOMERY:0 +Test mbedtls_mpi_mod_setup #7 (Montgomery representation) +mpi_mod_setup:MBEDTLS_MPI_MOD_REP_MONTGOMERY:0 # BEGIN MERGE SLOT 1 diff --git a/tests/suites/test_suite_bignum_mod.function b/tests/suites/test_suite_bignum_mod.function index 5a75ebc3a..bb87ba9d9 100644 --- a/tests/suites/test_suite_bignum_mod.function +++ b/tests/suites/test_suite_bignum_mod.function @@ -12,7 +12,7 @@ */ /* BEGIN_CASE */ -void mpi_mod_setup( int ext_rep, int int_rep, int iret ) +void mpi_mod_setup( int int_rep, int iret ) { #define MLIMBS 8 mbedtls_mpi_uint mp[MLIMBS]; @@ -22,7 +22,7 @@ void mpi_mod_setup( int ext_rep, int int_rep, int iret ) memset( mp, 0xFF, sizeof(mp) ); mbedtls_mpi_mod_modulus_init( &m ); - ret = mbedtls_mpi_mod_modulus_setup( &m, mp, MLIMBS, ext_rep, int_rep ); + ret = mbedtls_mpi_mod_modulus_setup( &m, mp, MLIMBS, int_rep ); TEST_EQUAL( ret, iret ); /* Only test if the constants have been set-up */ @@ -96,7 +96,7 @@ void mpi_residue_setup( char * input_X, char * input_Y, int ret ) TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &R, &r_limbs, input_Y ) ); TEST_EQUAL( 0, mbedtls_mpi_mod_modulus_setup( &m, N, n_limbs, - MBEDTLS_MPI_MOD_EXT_REP_LE, MBEDTLS_MPI_MOD_REP_MONTGOMERY ) ); + MBEDTLS_MPI_MOD_REP_MONTGOMERY ) ); TEST_EQUAL( ret, mbedtls_mpi_mod_residue_setup( &r, &m, R , r_limbs ) ); @@ -145,7 +145,7 @@ void mpi_mod_io_neg( ) mbedtls_mpi_mod_ext_rep endian = MBEDTLS_MPI_MOD_EXT_REP_LE; TEST_EQUAL( 0, mbedtls_mpi_mod_modulus_setup( &m, N, n_limbs, - endian, MBEDTLS_MPI_MOD_REP_MONTGOMERY ) ); + MBEDTLS_MPI_MOD_REP_MONTGOMERY ) ); TEST_EQUAL( 0, mbedtls_mpi_mod_residue_setup( &r, &m, R , n_limbs ) ); @@ -191,7 +191,7 @@ void mpi_mod_io_neg( ) /* Read the two limbs input data into a larger modulus and residue */ TEST_EQUAL( 0, mbedtls_mpi_mod_modulus_setup( &m2, N2, n2_limbs, - endian, MBEDTLS_MPI_MOD_REP_MONTGOMERY ) ); + MBEDTLS_MPI_MOD_REP_MONTGOMERY ) ); rn.p = R2; rn.limbs = r2_limbs; TEST_EQUAL( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL, @@ -232,7 +232,7 @@ void mpi_mod_io( char * input_N, data_t * input_A, int endian ) /* Init Structures */ mbedtls_mpi_mod_modulus_init( &m ); - TEST_EQUAL( 0, mbedtls_mpi_mod_modulus_setup( &m, N, n_limbs, endian, + TEST_EQUAL( 0, mbedtls_mpi_mod_modulus_setup( &m, N, n_limbs, MBEDTLS_MPI_MOD_REP_MONTGOMERY ) ); /* Enforcing p_limbs >= m->limbs */ diff --git a/tests/suites/test_suite_bignum_mod_raw.function b/tests/suites/test_suite_bignum_mod_raw.function index 031897889..eb1980c29 100644 --- a/tests/suites/test_suite_bignum_mod_raw.function +++ b/tests/suites/test_suite_bignum_mod_raw.function @@ -49,7 +49,7 @@ void mpi_mod_raw_io( data_t *input, int nb_int, int nx_32_int, 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, + int ret = mbedtls_mpi_mod_modulus_setup( &m, init, nx, MBEDTLS_MPI_MOD_REP_MONTGOMERY ); TEST_EQUAL( ret, 0 ); @@ -138,7 +138,6 @@ void mpi_mod_raw_cond_assign( char * input_X, memset( buff_m, 0xFF, copy_limbs ); TEST_EQUAL( mbedtls_mpi_mod_modulus_setup( &m, buff_m, copy_limbs, - MBEDTLS_MPI_MOD_EXT_REP_BE, MBEDTLS_MPI_MOD_REP_MONTGOMERY ), 0 ); /* condition is false */ @@ -211,7 +210,6 @@ void mpi_mod_raw_cond_swap( char * input_X, memset( buff_m, 0xFF, copy_limbs ); TEST_EQUAL( mbedtls_mpi_mod_modulus_setup( &m, buff_m, copy_limbs, - MBEDTLS_MPI_MOD_EXT_REP_BE, MBEDTLS_MPI_MOD_REP_MONTGOMERY ), 0 ); ASSERT_ALLOC( X, limbs ); @@ -480,7 +478,7 @@ void mpi_mod_raw_to_mont_rep( char * input_N, char * input_A, char * input_X ) TEST_LE_U(a_limbs, n_limbs); TEST_EQUAL( 0, mbedtls_mpi_mod_modulus_setup( &m, N, n_limbs, - MBEDTLS_MPI_MOD_EXT_REP_BE, MBEDTLS_MPI_MOD_REP_MONTGOMERY ) ); + MBEDTLS_MPI_MOD_REP_MONTGOMERY ) ); /* Convert from cannonical into Montgomery representation */ TEST_EQUAL(0, mbedtls_mpi_mod_raw_to_mont_rep( A, &m ) ); @@ -516,7 +514,7 @@ void mpi_mod_raw_from_mont_rep( char * input_N, char * input_A, char * input_X ) TEST_LE_U(a_limbs, n_limbs); TEST_EQUAL( 0, mbedtls_mpi_mod_modulus_setup( &m, N, n_limbs, - MBEDTLS_MPI_MOD_EXT_REP_BE, MBEDTLS_MPI_MOD_REP_MONTGOMERY ) ); + MBEDTLS_MPI_MOD_REP_MONTGOMERY ) ); /* Convert from Montgomery into cannonical representation */ TEST_EQUAL(0, mbedtls_mpi_mod_raw_from_mont_rep( A, &m ) );