diff --git a/library/bignum.c b/library/bignum.c index cfaaf5f20..d5e799e05 100644 --- a/library/bignum.c +++ b/library/bignum.c @@ -1550,7 +1550,7 @@ int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_ */ static void mpi_montg_init( mbedtls_mpi_uint *mm, const mbedtls_mpi *N ) { - *mm = mbedtls_mpi_montg_init( N->p[0] ); + *mm = mbedtls_mpi_montg_init( N->p ); } /** Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36) diff --git a/library/bignum_core.c b/library/bignum_core.c index bc1bca18f..ac9285361 100644 --- a/library/bignum_core.c +++ b/library/bignum_core.c @@ -348,14 +348,14 @@ void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X, /* * Fast Montgomery initialization (thanks to Tom St Denis). */ -mbedtls_mpi_uint mbedtls_mpi_montg_init( mbedtls_mpi_uint m0 ) +mbedtls_mpi_uint mbedtls_mpi_montg_init( const mbedtls_mpi_uint *N ) { - mbedtls_mpi_uint x = m0; + mbedtls_mpi_uint x = N[0]; - x += ( ( m0 + 2 ) & 4 ) << 1; + x += ( ( N[0] + 2 ) & 4 ) << 1; for( unsigned int i = biL; i >= 8; i /= 2 ) - x *= ( 2 - ( m0 * x ) ); + x *= ( 2 - ( N[0] * x ) ); return( ~x + 1 ); } diff --git a/library/bignum_core.h b/library/bignum_core.h index 02ac55d1b..85e25a82c 100644 --- a/library/bignum_core.h +++ b/library/bignum_core.h @@ -186,12 +186,12 @@ void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X, * \brief Calculate initialisation value for fast Montgomery modular * multiplication * - * \param m0 The least-significant mbedtls_mpi_uint from the modulus, which - * must be odd + * \param[in] N Little-endian presentation of the modulus. This must have + * at least one limb. * - * \return The initialisation value for fast Montgomery modular multiplication + * \return The initialisation value for fast Montgomery modular multiplication */ -mbedtls_mpi_uint mbedtls_mpi_montg_init( mbedtls_mpi_uint m0 ); +mbedtls_mpi_uint mbedtls_mpi_montg_init( const mbedtls_mpi_uint *N ); /** * \brief Perform a known-size multiply accumulate operation: d += b * s diff --git a/tests/suites/test_suite_mpi.function b/tests/suites/test_suite_mpi.function index bf1212a1a..dcc869f80 100644 --- a/tests/suites/test_suite_mpi.function +++ b/tests/suites/test_suite_mpi.function @@ -2015,7 +2015,7 @@ void mbedtls_mpi_montg_init( char * input_N, char * input_mm ) TEST_EQUAL( mm.s, 1 ); /* mbedtls_mpi_montg_init() only returns a result, no error possible */ - mbedtls_mpi_uint result = mbedtls_mpi_montg_init( N.p[0] ); + mbedtls_mpi_uint result = mbedtls_mpi_montg_init( N.p ); /* Check we got the correct result */ TEST_EQUAL( result, mm.p[0] ); @@ -2074,7 +2074,7 @@ void mbedtls_mpi_core_montmul( int limbs_AN4, int limbs_B4, TEST_EQUAL( mbedtls_mpi_grow( &T, limbs_AN * 2 + 1 ), 0 ); /* Calculate the Montgomery constant (this is unit tested separately) */ - mbedtls_mpi_uint mm = mbedtls_mpi_montg_init( N.p[0] ); + mbedtls_mpi_uint mm = mbedtls_mpi_montg_init( N.p ); TEST_EQUAL( mbedtls_mpi_grow( &R, limbs_AN ), 0 ); /* ensure it's got the right number of limbs */