Bignum Mod: pass endianness as a parameter
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.
Passing endianness as a parameter is a step towards removing it from the
modulus structure.
Signed-off-by: Janos Follath <janos.follath@arm.com>
diff --git a/library/bignum_mod.c b/library/bignum_mod.c
index 4303efe..fa4831c 100644
--- a/library/bignum_mod.c
+++ b/library/bignum_mod.c
@@ -212,7 +212,8 @@
int mbedtls_mpi_mod_read( mbedtls_mpi_mod_residue *r,
const mbedtls_mpi_mod_modulus *m,
const unsigned char *buf,
- size_t buflen )
+ size_t buflen,
+ mbedtls_mpi_mod_ext_rep ext_rep )
{
int ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
@@ -223,7 +224,7 @@
r->limbs == 0 || m->limbs == 0 )
goto cleanup;
- ret = mbedtls_mpi_mod_raw_read( r->p, m, buf, buflen );
+ ret = mbedtls_mpi_mod_raw_read( r->p, m, buf, buflen, ext_rep );
if( ret != 0 )
goto cleanup;
@@ -240,7 +241,8 @@
int mbedtls_mpi_mod_write( const mbedtls_mpi_mod_residue *r,
const mbedtls_mpi_mod_modulus *m,
unsigned char *buf,
- size_t buflen )
+ size_t buflen,
+ mbedtls_mpi_mod_ext_rep ext_rep )
{
int ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
@@ -254,7 +256,7 @@
if ( m->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY)
ret = mbedtls_mpi_mod_raw_from_mont_rep( r->p, m );
- ret = mbedtls_mpi_mod_raw_write( r->p, m, buf, buflen );
+ ret = mbedtls_mpi_mod_raw_write( r->p, m, buf, buflen, ext_rep );
cleanup:
return ( ret );
diff --git a/library/bignum_mod.h b/library/bignum_mod.h
index f0ce3c4..e6da15f 100644
--- a/library/bignum_mod.h
+++ b/library/bignum_mod.h
@@ -181,11 +181,12 @@
* 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.
- * \param m The address of the modulus.
- * \param buf The input buffer to import from.
- * \param buflen The length in bytes of \p buf.
+ * \param r The address of the residue related to \p m. It must have as
+ * many limbs as the modulus \p m.
+ * \param m The address of the modulus.
+ * \param buf The input buffer to import from.
+ * \param buflen The length in bytes of \p buf.
+ * \param ext_rep The endianness of the number in the input buffer.
*
* \return \c 0 if successful.
* \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
@@ -196,7 +197,8 @@
int mbedtls_mpi_mod_read( mbedtls_mpi_mod_residue *r,
const mbedtls_mpi_mod_modulus *m,
const unsigned char *buf,
- size_t buflen );
+ size_t buflen,
+ mbedtls_mpi_mod_ext_rep ext_rep );
/** Write residue data onto a buffer using public representation data.
*
@@ -206,11 +208,12 @@
* 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.
- * \param m The address of the modulus.
- * \param buf The output buffer to export to.
- * \param buflen The length in bytes of \p buf.
+ * \param r The address of the residue related to \p m. It must have as
+ * many limbs as the modulus \p m.
+ * \param m The address of the modulus.
+ * \param buf The output buffer to export to.
+ * \param buflen The length in bytes of \p buf.
+ * \param ext_rep The endianness in which the number should be written into the output buffer.
*
* \return \c 0 if successful.
* \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
@@ -221,7 +224,8 @@
int mbedtls_mpi_mod_write( const mbedtls_mpi_mod_residue *r,
const mbedtls_mpi_mod_modulus *m,
unsigned char *buf,
- size_t buflen );
+ size_t buflen,
+ mbedtls_mpi_mod_ext_rep ext_rep );
/* END MERGE SLOT 7 */
/* BEGIN MERGE SLOT 8 */
diff --git a/tests/suites/test_suite_bignum_mod.function b/tests/suites/test_suite_bignum_mod.function
index 715a839..5a75ebc 100644
--- a/tests/suites/test_suite_bignum_mod.function
+++ b/tests/suites/test_suite_bignum_mod.function
@@ -143,48 +143,59 @@
ASSERT_ALLOC( r_buff, buff_bytes );
memset( r_buff, 0x1, 1 );
+ mbedtls_mpi_mod_ext_rep endian = MBEDTLS_MPI_MOD_EXT_REP_LE;
TEST_EQUAL( 0, mbedtls_mpi_mod_modulus_setup( &m, N, n_limbs,
- MBEDTLS_MPI_MOD_EXT_REP_LE, MBEDTLS_MPI_MOD_REP_MONTGOMERY ) );
+ endian, MBEDTLS_MPI_MOD_REP_MONTGOMERY ) );
TEST_EQUAL( 0, mbedtls_mpi_mod_residue_setup( &r, &m, R , n_limbs ) );
/* Pass for input_r < modulo */
- TEST_EQUAL( 0, mbedtls_mpi_mod_read( &r, &m, r_buff, 1 ) );
+ TEST_EQUAL( 0, mbedtls_mpi_mod_read( &r, &m, r_buff, 1, endian ) );
/* Pass for input_r == modulo -1 */
memset( r_buff, 0xfd, buff_bytes );
- TEST_EQUAL( 0, mbedtls_mpi_mod_read( &r, &m, r_buff, 1 ) );
+ TEST_EQUAL( 0, mbedtls_mpi_mod_read( &r, &m, r_buff, 1, endian ) );
/* modulo->p == NULL || residue->p == NULL ( m2 has not been set-up ) */
- TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA, mbedtls_mpi_mod_read( &r, &m2, r_buff, 1 ) );
- TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA, mbedtls_mpi_mod_read( &rn, &m, r_buff, 1 ) );
- TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA, mbedtls_mpi_mod_write( &r, &m2, r_buff, 1 ) );
- TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA, mbedtls_mpi_mod_write( &rn, &m, r_buff, 1 ) );
+ TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
+ mbedtls_mpi_mod_read( &r, &m2, r_buff, 1, endian ) );
+ TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
+ mbedtls_mpi_mod_read( &rn, &m, r_buff, 1, endian ) );
+ TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
+ mbedtls_mpi_mod_write( &r, &m2, r_buff, 1, endian ) );
+ TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
+ mbedtls_mpi_mod_write( &rn, &m, r_buff, 1, endian ) );
/* Fail for r_limbs < m->limbs */
r.limbs = m.limbs - 1;
- TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA, mbedtls_mpi_mod_read( &r, &m, r_buff, 1 ) );
- TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA, mbedtls_mpi_mod_write( &rn, &m, r_buff, 1 ) );
+ TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
+ mbedtls_mpi_mod_read( &r, &m, r_buff, 1, endian ) );
+ TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
+ mbedtls_mpi_mod_write( &rn, &m, r_buff, 1, endian ) );
r.limbs = r_limbs;
/* Fail if input_r >= modulo m */
/* input_r = modulo */
memset( r_buff, 0xfe, buff_bytes );
- TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA, mbedtls_mpi_mod_read( &r, &m, r_buff, 1 ) );
+ TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
+ mbedtls_mpi_mod_read( &r, &m, r_buff, 1, endian ) );
/* input_r > modulo */
memset( r_buff, 0xff, buff_bytes );
- TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA, mbedtls_mpi_mod_read( &r, &m, r_buff, 1 ) );
+ TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
+ mbedtls_mpi_mod_read( &r, &m, r_buff, 1, endian ) );
/* Data too large to fit */
- TEST_EQUAL(MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL, mbedtls_mpi_mod_read( &r, &m, r_buff, buff_bytes ) );
+ TEST_EQUAL( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL,
+ mbedtls_mpi_mod_read( &r, &m, r_buff, buff_bytes, endian ) );
/* Read the two limbs input data into a larger modulus and residue */
TEST_EQUAL( 0, mbedtls_mpi_mod_modulus_setup( &m2, N2, n2_limbs,
- MBEDTLS_MPI_MOD_EXT_REP_LE, MBEDTLS_MPI_MOD_REP_MONTGOMERY ) );
+ endian, MBEDTLS_MPI_MOD_REP_MONTGOMERY ) );
rn.p = R2;
rn.limbs = r2_limbs;
- TEST_EQUAL(MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL, mbedtls_mpi_mod_write( &rn, &m2, r_buff, 1 ) );
+ TEST_EQUAL( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL,
+ mbedtls_mpi_mod_write( &rn, &m2, r_buff, 1, endian ) );
exit:
mbedtls_mpi_mod_modulus_free( &m );
@@ -198,7 +209,7 @@
/* END_CASE */
/* BEGIN_CASE */
-void mpi_mod_io( char * input_N, data_t * input_A, int iendian )
+void mpi_mod_io( char * input_N, data_t * input_A, int endian )
{
mbedtls_mpi_uint *N = NULL;
mbedtls_mpi_uint *R = NULL;
@@ -221,15 +232,17 @@
/* Init Structures */
mbedtls_mpi_mod_modulus_init( &m );
- TEST_EQUAL( 0, mbedtls_mpi_mod_modulus_setup( &m, N, n_limbs, iendian,
+ TEST_EQUAL( 0, mbedtls_mpi_mod_modulus_setup( &m, N, n_limbs, endian,
MBEDTLS_MPI_MOD_REP_MONTGOMERY ) );
/* Enforcing p_limbs >= m->limbs */
- TEST_EQUAL( 0, mbedtls_mpi_mod_residue_setup( &r, &m, R , n_limbs ) );
+ TEST_EQUAL( 0, mbedtls_mpi_mod_residue_setup( &r, &m, R, n_limbs ) );
- TEST_EQUAL( 0, mbedtls_mpi_mod_read( &r, &m, input_A->x, input_A->len ) );
+ TEST_EQUAL( 0, mbedtls_mpi_mod_read( &r, &m, input_A->x, input_A->len,
+ endian ) );
- TEST_EQUAL( 0,mbedtls_mpi_mod_write( &r, &m, r_buff, a_bytes ) );
+ TEST_EQUAL( 0, mbedtls_mpi_mod_write( &r, &m, r_buff, a_bytes,
+ endian ) );
ASSERT_COMPARE( r_buff, a_bytes, input_A->x, a_bytes );
exit: