From 9a111f0da04b4b411764ba0cbd950036adea3862 Mon Sep 17 00:00:00 2001 From: Jens Wiklander Date: Wed, 7 Nov 2018 08:11:29 +0100 Subject: [PATCH] libmbedtls: make mbedtls_mpi_mont*() available Makes mbedtls_mpi_montg_init(), mbedtls_mpi_montmul() and mbedtls_mpi_montred() available for external use. Acked-by: Jerome Forissier Signed-off-by: Jens Wiklander [jf: rebased onto mbedtls-2.22.0] [jf: rebased onto mbedtls-2.27.0, keep static functions] Signed-off-by: Jerome Forissier [jf: rebased onto mbedtls-2.28.1] Signed-off-by: Jerome Forissier [jw: rebased onto mbedtls-3.4.0] Signed-off-by: Jens Wiklander --- .../mbedtls/include/mbedtls/bignum.h | 30 +++++++++++++++++++ lib/libmbedtls/mbedtls/library/bignum.c | 18 +++++++++++ 2 files changed, 48 insertions(+) diff --git a/lib/libmbedtls/mbedtls/include/mbedtls/bignum.h b/lib/libmbedtls/mbedtls/include/mbedtls/bignum.h index b1d4b88ba5b..613e3bc72ee 100644 --- a/lib/libmbedtls/mbedtls/include/mbedtls/bignum.h +++ b/lib/libmbedtls/mbedtls/include/mbedtls/bignum.h @@ -1053,6 +1053,36 @@ int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int flags, int (*f_rng)(void *, unsigned char *, size_t), void *p_rng); +/** + * \brief Montgomery initialization + * + * \param mm The -1/m mod N result + * \param N The modulus + */ +void mbedtls_mpi_montg_init( mbedtls_mpi_uint *mm, const mbedtls_mpi *N ); + +/** + * \brief Montgomery multiplication: A = A * B * R^-1 mod N + * \A Parameter and result + * \B Parameter + * \N Modulus + * \mm Parameter from mbedtls_mpi_montg_init() + * \T Temporary variable, should be as twice as big as N + 2 + */ +void mbedtls_mpi_montmul(mbedtls_mpi *A, const mbedtls_mpi *B, + const mbedtls_mpi *N, mbedtls_mpi_uint mm, + mbedtls_mpi *T ); + +/** + * \brief Montgomery reduction: A = A * R^-1 mod N + * \A Parameter and result + * \N Modulus + * \mm Parameter from mbedtls_mpi_montg_init() + * \T Temporary variable, should be as twice as big as N + 2 + */ +void mbedtls_mpi_montred(mbedtls_mpi *A, const mbedtls_mpi *N, + mbedtls_mpi_uint mm, mbedtls_mpi *T); + #if defined(MBEDTLS_SELF_TEST) /** diff --git a/lib/libmbedtls/mbedtls/library/bignum.c b/lib/libmbedtls/mbedtls/library/bignum.c index d3a1b00d520..7ea191e7385 100644 --- a/lib/libmbedtls/mbedtls/library/bignum.c +++ b/lib/libmbedtls/mbedtls/library/bignum.c @@ -1571,6 +1571,11 @@ static void mpi_montg_init(mbedtls_mpi_uint *mm, const mbedtls_mpi *N) *mm = mbedtls_mpi_core_montmul_init(N->p); } +void mbedtls_mpi_montg_init( mbedtls_mpi_uint *mm, const mbedtls_mpi *N ) +{ + mpi_montg_init( mm, N ); +} + /** Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36) * * \param[in,out] A One of the numbers to multiply. @@ -1599,6 +1604,13 @@ static void mpi_montmul(mbedtls_mpi *A, const mbedtls_mpi *B, mbedtls_mpi_core_montmul(A->p, A->p, B->p, B->n, N->p, N->n, mm, T->p); } +void mbedtls_mpi_montmul(mbedtls_mpi *A, const mbedtls_mpi *B, + const mbedtls_mpi *N, mbedtls_mpi_uint mm, + mbedtls_mpi *T ) +{ + mpi_montmul( A, B, N, mm, T); +} + /* * Montgomery reduction: A = A * R^-1 mod N * @@ -1616,6 +1628,12 @@ static void mpi_montred(mbedtls_mpi *A, const mbedtls_mpi *N, mpi_montmul(A, &U, N, mm, T); } +void mbedtls_mpi_montred(mbedtls_mpi *A, const mbedtls_mpi *N, + mbedtls_mpi_uint mm, mbedtls_mpi *T) +{ + mpi_montred(A, N, mm, T); +} + /** * Select an MPI from a table without leaking the index. *