diff --git a/library/bignum_mod.c b/library/bignum_mod.c index 31e18e7410..0f511335d7 100644 --- a/library/bignum_mod.c +++ b/library/bignum_mod.c @@ -176,6 +176,28 @@ exit: /* BEGIN MERGE SLOT 2 */ +int mbedtls_mpi_mod_mul( mbedtls_mpi_mod_residue *X, + const mbedtls_mpi_mod_residue *A, + const mbedtls_mpi_mod_residue *B, + const mbedtls_mpi_mod_modulus *N ) +{ + if( N->limbs == 0 ) + return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; + + if( X->limbs != N->limbs || A->limbs != N->limbs || B->limbs != N->limbs ) + return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; + + mbedtls_mpi_uint *T = mbedtls_calloc( N->limbs * 2 + 1, ciL ); + if( !T ) + return MBEDTLS_ERR_MPI_ALLOC_FAILED; + + mbedtls_mpi_mod_raw_mul( X->p, A->p, B->p, N, T ); + + mbedtls_free( T ); + + return( 0 ); +} + /* END MERGE SLOT 2 */ /* BEGIN MERGE SLOT 3 */ diff --git a/library/bignum_mod.h b/library/bignum_mod.h index 95aaacc4df..f8c71d01ba 100644 --- a/library/bignum_mod.h +++ b/library/bignum_mod.h @@ -217,6 +217,40 @@ void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m ); /* BEGIN MERGE SLOT 2 */ +/** \brief Multiply two residues, returning the residue modulo the specified + * modulus. + * + * \note Currenty handles the case when `m->int_rep` is + * MBEDTLS_MPI_MOD_REP_MONTGOMERY. + * + * The size of the operation is determined by \p N. \p A and \p B must have + * the same number of limbs as \p N. + * + * \p X may be aliased to \p A or \p B, or even both, but may not overlap + * either otherwise. They may not alias \p N (since they must be in canonical + * form, they cannot == \p N). + * + * \param[out] X The address of the result MPI. + * This must be initialized. Must have enough limbs to + * store the full value of the result. + * On successful completion, \p X contains the result of + * the multiplication `A * B * R^-1` mod N where + * `R = 2^(biL *N->limbs)`. + * \param[in] A The address of the first MPI. This must be initialized. + * \param[in] B The address of the second MPI. This must be initialized. + * \param[in] N The address of the modulus. Used to perform a modulo + * operation on the result of the multiplication. + * + * \return \c 0 if successful. + * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if all the parameters does not + * have the same number of limbs or \p N is invalid. + * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. + */ +int mbedtls_mpi_mod_mul( mbedtls_mpi_mod_residue *X, + const mbedtls_mpi_mod_residue *A, + const mbedtls_mpi_mod_residue *B, + const mbedtls_mpi_mod_modulus *N ); + /* END MERGE SLOT 2 */ /* BEGIN MERGE SLOT 3 */