bignum_mod: Refactored `mbedtls_mpi_mod_read/write()`
This patch adjusts the I/O methods and the tests.
Documentation has also been updated to be more clear.
Signed-off-by: Minos Galanakis <minos.galanakis@arm.com>
diff --git a/library/bignum_mod.c b/library/bignum_mod.c
index 770e633..c10fb2e 100644
--- a/library/bignum_mod.c
+++ b/library/bignum_mod.c
@@ -210,8 +210,8 @@
/* BEGIN MERGE SLOT 7 */
int mbedtls_mpi_mod_read( mbedtls_mpi_mod_residue *r,
- mbedtls_mpi_mod_modulus *m,
- unsigned char *buf,
+ const mbedtls_mpi_mod_modulus *m,
+ const unsigned char *buf,
size_t buflen )
{
int ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
@@ -219,7 +219,7 @@
if ( r == NULL || m == NULL )
goto cleanup;
- if ( r->p == NULL || m->p == NULL || r->limbs > m->limbs ||\
+ if ( r->p == NULL || m->p == NULL || r->limbs > m->limbs ||
r->limbs == 0 || m->limbs == 0 )
goto cleanup;
@@ -228,6 +228,8 @@
if( ret != 0 )
goto cleanup;
+ r->limbs = m->limbs;
+
if (m->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY)
ret = mbedtls_mpi_mod_raw_to_mont_rep(r->p, m);
@@ -235,8 +237,8 @@
return ( ret );
}
-int mbedtls_mpi_mod_write( mbedtls_mpi_mod_residue *r,
- mbedtls_mpi_mod_modulus *m,
+int mbedtls_mpi_mod_write( const mbedtls_mpi_mod_residue *r,
+ const mbedtls_mpi_mod_modulus *m,
unsigned char *buf,
size_t buflen )
{
@@ -245,7 +247,7 @@
if ( r == NULL || m == NULL )
goto cleanup;
- if ( r->p == NULL || m->p == NULL || r->limbs > m->limbs ||\
+ if ( r->p == NULL || m->p == NULL || r->limbs > m->limbs ||
r->limbs == 0 || m->limbs == 0 )
goto cleanup;
diff --git a/library/bignum_mod.h b/library/bignum_mod.h
index 4a01dfc..f0ce3c4 100644
--- a/library/bignum_mod.h
+++ b/library/bignum_mod.h
@@ -177,8 +177,9 @@
/** Read public representation data stored in a buffer into a residue structure.
*
* The `mbedtls_mpi_mod_residue` and `mbedtls_mpi_mod_modulus` structures must
- * be compatible. The data will be automatically converted into the appropriate
- * representation based on the value of `m->int_rep field`.
+ * be compatible (Data in public representation is assumed to be in the m->ext_rep
+ * and will be padded to m->limbs). The data will be automatically converted
+ * into the appropriate internal representation based on the value of `m->int_rep`.
*
* \param r The address of the residue related to \p m. It must have as
* many limbs as the modulus \p m.
@@ -193,15 +194,17 @@
* of \p m is invalid or \p X is not less than \p m.
*/
int mbedtls_mpi_mod_read( mbedtls_mpi_mod_residue *r,
- mbedtls_mpi_mod_modulus *m,
- unsigned char *buf,
+ const mbedtls_mpi_mod_modulus *m,
+ const unsigned char *buf,
size_t buflen );
/** Write residue data onto a buffer using public representation data.
*
* The `mbedtls_mpi_mod_residue` and `mbedtls_mpi_mod_modulus` structures must
- * be compatible. The data will be automatically converted into the appropriate
- * representation based on the value of `m->int_rep field`.
+ * be compatible (Data will be exported onto the bufer using the m->ext_rep
+ * and will be read as of m->limbs length).The data will be automatically
+ * converted from the appropriate internal representation based on the
+ * value of `m->int_rep field`.
*
* \param r The address of the residue related to \p m. It must have as
* many limbs as the modulus \p m.
@@ -215,8 +218,8 @@
* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation
* of \p m is invalid.
*/
-int mbedtls_mpi_mod_write( mbedtls_mpi_mod_residue *r,
- mbedtls_mpi_mod_modulus *m,
+int mbedtls_mpi_mod_write( const mbedtls_mpi_mod_residue *r,
+ const mbedtls_mpi_mod_modulus *m,
unsigned char *buf,
size_t buflen );
/* END MERGE SLOT 7 */
diff --git a/tests/suites/test_suite_bignum_mod.function b/tests/suites/test_suite_bignum_mod.function
index e4d7b41..715a839 100644
--- a/tests/suites/test_suite_bignum_mod.function
+++ b/tests/suites/test_suite_bignum_mod.function
@@ -110,13 +110,6 @@
/* BEGIN_CASE */
void mpi_mod_io_neg( )
{
- #define IO_ZERO 0
- #define IO_ONE 1
- #define IO_MIN1 2
- #define IO_MAX 3
- #define IO_2LIMBS_MIN1 4
- #define IO_2LIMBS 5
-
mbedtls_mpi_uint *N = NULL;
mbedtls_mpi_uint *R = NULL;
mbedtls_mpi_uint *N2 = NULL;
@@ -130,15 +123,18 @@
mbedtls_mpi_mod_modulus m2;
mbedtls_mpi_mod_residue rn = { NULL, 0 };
- const char * s_data[ 6 ] = { "00", "01", "fe", "ff",
- "7ffffffffffffffff0" ,"7ffffffffffffffff1" };
+ const char *hex_residue_single = "01";
+ const char *hex_modulus_single = "fe";
+ const char *hex_residue_multi = "7ffffffffffffffffffffffffffffff0";
+ const char *hex_modulus_multi = "7ffffffffffffffffffffffffffffff1";
+
const size_t buff_bytes = 1024;
/* Allocate the memory for intermediate data structures */
- TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &N, &n_limbs, s_data[ IO_MIN1 ] ) );
- TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &R, &r_limbs, s_data[ IO_ONE ] ) );
- TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &N2, &n2_limbs, s_data[ IO_2LIMBS ] ) );
- TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &R2, &r2_limbs, s_data[ IO_2LIMBS_MIN1 ] ) );
+ TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &N, &n_limbs, hex_modulus_single ) );
+ TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &R, &r_limbs, hex_residue_single ) );
+ TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &N2, &n2_limbs, hex_modulus_multi ) );
+ TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &R2, &r2_limbs, hex_residue_multi ) );
mbedtls_mpi_mod_modulus_init( &m );
mbedtls_mpi_mod_modulus_init( &m2 );
@@ -155,7 +151,7 @@
/* Pass for input_r < modulo */
TEST_EQUAL( 0, mbedtls_mpi_mod_read( &r, &m, r_buff, 1 ) );
- /* input_r == modulo -1 */
+ /* Pass for input_r == modulo -1 */
memset( r_buff, 0xfd, buff_bytes );
TEST_EQUAL( 0, mbedtls_mpi_mod_read( &r, &m, r_buff, 1 ) );
@@ -198,13 +194,6 @@
mbedtls_free( N2 );
mbedtls_free( R2 );
mbedtls_free( r_buff );
-
- #undef IO_ZERO
- #undef IO_ONE
- #undef IO_MIN1
- #undef IO_MAX
- #undef IO_2LIMBS_MIN1
- #undef IO_2LIMBS
}
/* END_CASE */