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 <jerome.forissier@linaro.org>
Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>
[jf: rebased onto mbedtls-2.22.0]
Signed-off-by: Jerome Forissier <jerome@forissier.org>
[jf: rebased onto mbedtls-2.27.0, keep static functions]
Signed-off-by: Jerome Forissier <jerome@forissier.org>
diff --git a/lib/libmbedtls/mbedtls/include/mbedtls/bignum.h b/lib/libmbedtls/mbedtls/include/mbedtls/bignum.h
index f087035..b0cb21c 100644
--- a/lib/libmbedtls/mbedtls/include/mbedtls/bignum.h
+++ b/lib/libmbedtls/mbedtls/include/mbedtls/bignum.h
@@ -1037,6 +1037,36 @@
                    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,
+                          const 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, const 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 20afa22..9e84da5 100644
--- a/lib/libmbedtls/mbedtls/library/bignum.c
+++ b/lib/libmbedtls/mbedtls/library/bignum.c
@@ -2141,6 +2141,11 @@
     *mm = ~x + 1;
 }
 
+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.
@@ -2210,6 +2215,12 @@
     mpi_safe_cond_assign( n, A->p, d, (unsigned char) d[n] );
 }
 
+void mbedtls_mpi_montmul( mbedtls_mpi *A, const mbedtls_mpi *B, const mbedtls_mpi *N, mbedtls_mpi_uint mm,
+                          const mbedtls_mpi *T )
+{
+    mpi_montmul( A, B, N, mm, T);
+}
+
 /*
  * Montgomery reduction: A = A * R^-1 mod N
  *
@@ -2227,6 +2238,12 @@
     mpi_montmul( A, &U, N, mm, T );
 }
 
+void mbedtls_mpi_montred( mbedtls_mpi *A, const mbedtls_mpi *N,
+                          mbedtls_mpi_uint mm, const mbedtls_mpi *T )
+{
+    mpi_montred( A, N, mm, T );
+}
+
 /*
  * Constant-flow boolean "equal" comparison:
  * return x == y