diff --git a/library/bignum_core.c b/library/bignum_core.c index 00837298b0..457405d277 100644 --- a/library/bignum_core.c +++ b/library/bignum_core.c @@ -511,4 +511,20 @@ void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X, mbedtls_ct_mpi_uint_cond_assign( AN_limbs, X, T, (unsigned char) ( carry ^ borrow ) ); } +int mbedtls_mpi_get_montgomery_constant_unsafe( mbedtls_mpi *X, + mbedtls_mpi const *N ) +{ + int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; + + if ( X == NULL || N == NULL ) goto cleanup; + + MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 1 ) ); + MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( X, N->n * 2 * biL ) ); + MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( X, X, N ) ); + MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( X, N->n ) ); + +cleanup: + return( ret ); +} + #endif /* MBEDTLS_BIGNUM_C */ diff --git a/library/bignum_core.h b/library/bignum_core.h index 56a3bf874f..3100bd5ff9 100644 --- a/library/bignum_core.h +++ b/library/bignum_core.h @@ -412,4 +412,23 @@ void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X, const mbedtls_mpi_uint *N, size_t AN_limbs, mbedtls_mpi_uint mm, mbedtls_mpi_uint *T ); +/** + * \brief Calculate initialisation value for fast Montgomery modular + * multiplication + * + * \param[out] X A pointer to the result of the calculation of + * Montgomery const 2^{2*n*biL} mod N. + * \param[in] N Little-endian presentation of the modulus, which must be odd. + * + * \return 0 if successful. + * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if there is not enough space + * to store the value of Montgomery constant squared. + * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p N modulus is zero. + * \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p N modulus is negative. + * \return #MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED \p N, \p X are NULL + * or other operations fail. + */ +int mbedtls_mpi_get_montgomery_constant_unsafe( mbedtls_mpi *X, + mbedtls_mpi const *N ); + #endif /* MBEDTLS_BIGNUM_CORE_H */