aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Cosgrove <tom.cosgrove@arm.com>2022-12-12 17:06:27 +0000
committerTom Cosgrove <tom.cosgrove@arm.com>2022-12-15 16:56:36 +0000
commit28ff92cc3a72d98885e4fa6dc19e9725db5f9ac7 (patch)
tree1b04297e920113c36eca096308b4c51b2b1f79b2
parent30f3b4d60112ffe68cc162cc01938b84d18c9960 (diff)
downloadmbedtls-28ff92cc3a72d98885e4fa6dc19e9725db5f9ac7.zip
mbedtls-28ff92cc3a72d98885e4fa6dc19e9725db5f9ac7.tar.gz
mbedtls-28ff92cc3a72d98885e4fa6dc19e9725db5f9ac7.tar.bz2
Add an explicit mbedtls_mpi_core_montmul_working_limbs() function
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
-rw-r--r--library/bignum_core.h21
-rw-r--r--library/bignum_mod_raw.c4
-rw-r--r--library/bignum_mod_raw.h4
-rw-r--r--tests/suites/test_suite_bignum_core.function8
-rw-r--r--tests/suites/test_suite_bignum_mod_raw.function4
5 files changed, 38 insertions, 3 deletions
diff --git a/library/bignum_core.h b/library/bignum_core.h
index 7f5375e..7b5787c 100644
--- a/library/bignum_core.h
+++ b/library/bignum_core.h
@@ -502,6 +502,10 @@ int mbedtls_mpi_core_fill_random( mbedtls_mpi_uint *X, size_t X_limbs,
* \brief Returns the number of limbs of working memory required for
* a call to `mbedtls_mpi_core_exp_mod()`.
*
+ * \note This will always be at least
+ * `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`,
+ * i.e. sufficient for a call to `mbedtls_mpi_core_montmul()`.
+ *
* \param AN_limbs The number of limbs in the input `A` and the modulus `N`
* (they must be the same size) that will be given to
* `mbedtls_mpi_core_exp_mod()`.
@@ -585,6 +589,23 @@ mbedtls_mpi_uint mbedtls_mpi_core_sub_int( mbedtls_mpi_uint *X,
mbedtls_mpi_uint mbedtls_mpi_core_check_zero_ct( const mbedtls_mpi_uint *A,
size_t limbs );
+/**
+ * \brief Returns the number of limbs of working memory required for
+ * a call to `mbedtls_mpi_core_montmul()`.
+ *
+ * \param AN_limbs The number of limbs in the input `A` and the modulus `N`
+ * (they must be the same size) that will be given to
+ * `mbedtls_mpi_core_montmul()` or one of the other functions
+ * that specifies this as the amount of working memory needed.
+ *
+ * \return The number of limbs of working memory required by
+ * `mbedtls_mpi_core_montmul()` (or other similar function).
+ */
+static inline size_t mbedtls_mpi_core_montmul_working_limbs( size_t AN_limbs )
+{
+ return( 2 * AN_limbs + 1 );
+}
+
/* END MERGE SLOT 3 */
/* BEGIN MERGE SLOT 4 */
diff --git a/library/bignum_mod_raw.c b/library/bignum_mod_raw.c
index c98a1c1..be8fc86 100644
--- a/library/bignum_mod_raw.c
+++ b/library/bignum_mod_raw.c
@@ -183,7 +183,7 @@ int mbedtls_mpi_mod_raw_to_mont_rep( mbedtls_mpi_uint *X,
const mbedtls_mpi_mod_modulus *m )
{
mbedtls_mpi_uint *T;
- const size_t t_limbs = m->limbs * 2 + 1;
+ const size_t t_limbs = mbedtls_mpi_core_montmul_working_limbs( m->limbs );
if( ( T = (mbedtls_mpi_uint *) mbedtls_calloc( t_limbs, ciL ) ) == NULL )
return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
@@ -200,7 +200,7 @@ int mbedtls_mpi_mod_raw_from_mont_rep( mbedtls_mpi_uint *X,
const mbedtls_mpi_mod_modulus *m )
{
const mbedtls_mpi_uint one = 1;
- const size_t t_limbs = m->limbs * 2 + 1;
+ const size_t t_limbs = mbedtls_mpi_core_montmul_working_limbs( m->limbs );
mbedtls_mpi_uint *T;
if( ( T = (mbedtls_mpi_uint *) mbedtls_calloc( t_limbs, ciL ) ) == NULL )
diff --git a/library/bignum_mod_raw.h b/library/bignum_mod_raw.h
index f9968ba..73eaf18 100644
--- a/library/bignum_mod_raw.h
+++ b/library/bignum_mod_raw.h
@@ -178,6 +178,10 @@ void mbedtls_mpi_mod_raw_sub( mbedtls_mpi_uint *X,
* \brief Returns the number of limbs of working memory required for
* a call to `mbedtls_mpi_mod_raw_inv_prime()`.
*
+ * \note This will always be at least
+ * `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`,
+ * i.e. sufficient for a call to `mbedtls_mpi_core_montmul()`.
+ *
* \param AN_limbs The number of limbs in the input `A` and the modulus `N`
* (they must be the same size) that will be given to
* `mbedtls_mpi_mod_raw_inv_prime()`.
diff --git a/tests/suites/test_suite_bignum_core.function b/tests/suites/test_suite_bignum_core.function
index 9392f51..038ee6b 100644
--- a/tests/suites/test_suite_bignum_core.function
+++ b/tests/suites/test_suite_bignum_core.function
@@ -798,7 +798,9 @@ void mpi_core_montmul( int limbs_AN4, int limbs_B4,
TEST_EQUAL( 0, mbedtls_mpi_grow( X, limbs_AN ) );
TEST_EQUAL( 0, mbedtls_mpi_grow( &B, limbs_B ) );
- TEST_EQUAL( 0, mbedtls_mpi_grow( &T, limbs_AN * 2 + 1 ) );
+ size_t working_limbs = mbedtls_mpi_core_montmul_working_limbs( limbs_AN );
+ TEST_EQUAL( working_limbs, limbs_AN * 2 + 1 );
+ TEST_EQUAL( 0, mbedtls_mpi_grow( &T, working_limbs ) );
/* Calculate the Montgomery constant (this is unit tested separately) */
mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init( N.p );
@@ -1083,6 +1085,10 @@ void mpi_core_exp_mod( char * input_N, char * input_A,
TEST_LE_U( min_expected_working_limbs, working_limbs );
TEST_LE_U( working_limbs, max_expected_working_limbs );
+ /* Should also be at least mbedtls_mpi_core_montmul_working_limbs() */
+ TEST_LE_U( mbedtls_mpi_core_montmul_working_limbs( N_limbs ),
+ working_limbs );
+
ASSERT_ALLOC( T, working_limbs );
mbedtls_mpi_core_exp_mod( Y, A, N, N_limbs, E, E_limbs, R2, T );
diff --git a/tests/suites/test_suite_bignum_mod_raw.function b/tests/suites/test_suite_bignum_mod_raw.function
index 83e1f54..ef0f712 100644
--- a/tests/suites/test_suite_bignum_mod_raw.function
+++ b/tests/suites/test_suite_bignum_mod_raw.function
@@ -394,6 +394,10 @@ void mpi_mod_raw_inv_prime( char * input_N, char * input_A, char * input_X )
TEST_LE_U( min_expected_working_limbs, working_limbs );
TEST_LE_U( working_limbs, max_expected_working_limbs );
+ /* Should also be at least mbedtls_mpi_core_montmul_working_limbs() */
+ TEST_LE_U( mbedtls_mpi_core_montmul_working_limbs( N_limbs ),
+ working_limbs );
+
ASSERT_ALLOC( T, working_limbs );
mbedtls_mpi_mod_raw_inv_prime( Y, A, N, N_limbs, R2, T );