From d9299c388e9bb60166c72e943c704a3d079256bd Mon Sep 17 00:00:00 2001 From: Minos Galanakis Date: Tue, 1 Nov 2022 16:19:07 +0000 Subject: [PATCH] bignum_mod_raw: Refactored Montgomery conversion functions This patch updates the `mbedtls_mpi_mod_raw_conv_xx()` methods as follows: * Renamed for simplicity: conv_fwd -> from_mont_rep, conv_inv -> to_mont_rep. * Uncoupled the dependency on the legaly bignum interface. * `mbedtls_mpi` is no longer used for temporary buffer allocation. Signed-off-by: Minos Galanakis --- library/bignum_mod_raw.c | 42 ++++++++++++++++++++++++++-------------- library/bignum_mod_raw.h | 20 +++++++++++-------- 2 files changed, 39 insertions(+), 23 deletions(-) diff --git a/library/bignum_mod_raw.c b/library/bignum_mod_raw.c index 97f7731c4c..b43add77d3 100644 --- a/library/bignum_mod_raw.c +++ b/library/bignum_mod_raw.c @@ -127,26 +127,38 @@ int mbedtls_mpi_mod_raw_write( const mbedtls_mpi_uint *A, /* END MERGE SLOT 6 */ /* BEGIN MERGE SLOT 7 */ -int mbedtls_mpi_mod_raw_conv_inv( mbedtls_mpi_uint *X, - const mbedtls_mpi_mod_modulus *modulus ) +int mbedtls_mpi_mod_raw_to_mont_rep( mbedtls_mpi_uint *X, + const mbedtls_mpi_mod_modulus *m ) { - mbedtls_mpi_uint one = 1; - mbedtls_mpi T; - mbedtls_mpi_init( &T ); - mbedtls_mpi_core_montmul( X, X, &one, 1, m->p, m->limbs, - m->rep.mont.mm, T.p ); - mbedtls_mpi_free( &T ); + mbedtls_mpi_uint *T; + const size_t t_limbs = m->limbs * 2 + 1; + + if( ( T = (mbedtls_mpi_uint *) mbedtls_calloc( t_limbs, ciL ) ) == NULL ) + return( MBEDTLS_ERR_MPI_ALLOC_FAILED ); + + mbedtls_mpi_core_montmul( X, X, m->rep.mont.rr, m->limbs, m->p, m->limbs, + m->rep.mont.mm, T ); + + mbedtls_platform_zeroize( T, t_limbs * ciL ); + mbedtls_free( T ); return( 0 ); } -int mbedtls_mpi_mod_raw_conv_fwd( mbedtls_mpi_uint *X, - const mbedtls_mpi_mod_modulus *modulus ) +int mbedtls_mpi_mod_raw_from_mont_rep( mbedtls_mpi_uint *X, + const mbedtls_mpi_mod_modulus *m ) { - mbedtls_mpi T; - mbedtls_mpi_init( &T ); - mbedtls_mpi_core_montmul( X, X, m->rep.mont.rr, 1, m->p, m->limbs, - m->rep.mont.mm, T.p ); - mbedtls_mpi_free( &T ); + const mbedtls_mpi_uint one = 1; + const size_t t_limbs = m->limbs * 2 + 1; + mbedtls_mpi_uint *T; + + if( ( T = (mbedtls_mpi_uint *) mbedtls_calloc( t_limbs, ciL ) ) == NULL ) + return( MBEDTLS_ERR_MPI_ALLOC_FAILED ); + + mbedtls_mpi_core_montmul( X, X, &one, 1, m->p, m->limbs, + m->rep.mont.mm, T ); + + mbedtls_platform_zeroize( T, t_limbs * ciL ); + mbedtls_free( T ); return( 0 ); } /* END MERGE SLOT 7 */ diff --git a/library/bignum_mod_raw.h b/library/bignum_mod_raw.h index 38415f415f..f738e917e1 100644 --- a/library/bignum_mod_raw.h +++ b/library/bignum_mod_raw.h @@ -163,25 +163,29 @@ int mbedtls_mpi_mod_raw_write( const mbedtls_mpi_uint *A, /* END MERGE SLOT 6 */ /* BEGIN MERGE SLOT 7 */ -/** Convert from internal to public (little endian) data presentation +/** Convert an MPI into Montgomery form. * * \param X The address of the MPI. - * \param m The address of a modulus. + * Must have the same number of limbs as \p m. + * \param m The address of the modulus, which gives the size of + * the base `R` = 2^(biL*m->limbs). * * \return \c 0 if successful. */ -int mbedtls_mpi_mod_raw_conv_inv( mbedtls_mpi_uint *X, - const mbedtls_mpi_mod_modulus *modulus ); +int mbedtls_mpi_mod_raw_to_mont_rep( mbedtls_mpi_uint *X, + const mbedtls_mpi_mod_modulus *m ); -/** Convert from public (little endian) to internal data presentation. +/** Convert an MPI back from Montgomery representation. * * \param X The address of the MPI. - * \param m The address of a modulus. + * Must have the same number of limbs as \p m. + * \param m The address of the modulus, which gives the size of + * the base `R`= 2^(biL*m->limbs). * * \return \c 0 if successful. */ -int mbedtls_mpi_mod_raw_conv_fwd( mbedtls_mpi_uint *X, - const mbedtls_mpi_mod_modulus *modulus ); +int mbedtls_mpi_mod_raw_from_mont_rep( mbedtls_mpi_uint *X, + const mbedtls_mpi_mod_modulus *m ); /* END MERGE SLOT 7 */ /* BEGIN MERGE SLOT 8 */