diff --git a/library/bignum.c b/library/bignum.c index 50da6b33b5..74f10af8d5 100644 --- a/library/bignum.c +++ b/library/bignum.c @@ -1907,19 +1907,17 @@ int mbedtls_mpi_mod_int(mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_s /* * Fast Montgomery initialization (thanks to Tom St Denis) */ -void mbedtls_mpi_montg_init(mbedtls_mpi_uint *mm, const mbedtls_mpi *N) +mbedtls_mpi_uint mbedtls_mpi_montmul_init(const mbedtls_mpi_uint *N) { - mbedtls_mpi_uint x, m0 = N->p[0]; - unsigned int i; + mbedtls_mpi_uint x = N[0]; - x = m0; - x += ((m0 + 2) & 4) << 1; + x += ((N[0] + 2) & 4) << 1; - for (i = biL; i >= 8; i /= 2) { - x *= (2 - (m0 * x)); + for (unsigned int i = biL; i >= 8; i /= 2) { + x *= (2 - (N[0] * x)); } - *mm = ~x + 1; + return ~x + 1; } void mbedtls_mpi_montmul(mbedtls_mpi *A, @@ -2069,7 +2067,7 @@ int mbedtls_mpi_exp_mod(mbedtls_mpi *X, const mbedtls_mpi *A, /* * Init temps and window size */ - mbedtls_mpi_montg_init(&mm, N); + mm = mbedtls_mpi_montmul_init(N->p); mbedtls_mpi_init(&RR); mbedtls_mpi_init(&T); mbedtls_mpi_init(&Apos); mbedtls_mpi_init(&WW); diff --git a/library/bignum_internal.h b/library/bignum_internal.h index f14c294a5a..5435ebb464 100644 --- a/library/bignum_internal.h +++ b/library/bignum_internal.h @@ -30,14 +30,14 @@ int mbedtls_mpi_get_mont_r2_unsafe(mbedtls_mpi *X, /** * \brief Calculate initialisation value for fast Montgomery modular - * multiplication. + * multiplication * - * \param[out] mm The initialisation value for fast Montgomery modular - * multiplication. - * \param[in] N Little-endian presentation of the modulus. This must have - * at least one limb. + * \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 */ -void mbedtls_mpi_montg_init(mbedtls_mpi_uint *mm, const mbedtls_mpi *N); +mbedtls_mpi_uint mbedtls_mpi_montmul_init(const mbedtls_mpi_uint *N); /** Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36) * diff --git a/library/rsa.c b/library/rsa.c index 23fe84310b..0a0c2e3880 100644 --- a/library/rsa.c +++ b/library/rsa.c @@ -815,8 +815,7 @@ static int rsa_unblind(mbedtls_mpi *T, mbedtls_mpi *Vf, const mbedtls_mpi *N) const size_t nlimbs = N->n; const size_t tlimbs = 2 * (nlimbs + 1); - mbedtls_mpi_uint mm; - mbedtls_mpi_montg_init(&mm, N); + mbedtls_mpi_uint mm = mbedtls_mpi_montmul_init(N->p); mbedtls_mpi RR, M_T;