bignum_mod_raw: Ported mbedtls_mpi_get_montgomery_constant_unsafe from prototype

Signed-off-by: Minos Galanakis <minos.galanakis@arm.com>
This commit is contained in:
Hanno Becker 2022-08-11 17:29:32 +01:00 committed by Minos Galanakis
parent 383b0bbea0
commit ec440f2397
2 changed files with 35 additions and 0 deletions

View File

@ -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 */

View File

@ -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 */