From 9db81e9cca84d01414027a80e78a8de1b33aca28 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Tue, 13 Dec 2022 10:51:37 +0100 Subject: [PATCH 01/12] Add mod_mul function Signed-off-by: Gabor Mezei --- library/bignum_mod.c | 22 ++++++++++++++++++++++ library/bignum_mod.h | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/library/bignum_mod.c b/library/bignum_mod.c index 31e18e7410..0f511335d7 100644 --- a/library/bignum_mod.c +++ b/library/bignum_mod.c @@ -176,6 +176,28 @@ exit: /* BEGIN MERGE SLOT 2 */ +int mbedtls_mpi_mod_mul( mbedtls_mpi_mod_residue *X, + const mbedtls_mpi_mod_residue *A, + const mbedtls_mpi_mod_residue *B, + const mbedtls_mpi_mod_modulus *N ) +{ + if( N->limbs == 0 ) + return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; + + if( X->limbs != N->limbs || A->limbs != N->limbs || B->limbs != N->limbs ) + return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; + + mbedtls_mpi_uint *T = mbedtls_calloc( N->limbs * 2 + 1, ciL ); + if( !T ) + return MBEDTLS_ERR_MPI_ALLOC_FAILED; + + mbedtls_mpi_mod_raw_mul( X->p, A->p, B->p, N, T ); + + mbedtls_free( T ); + + return( 0 ); +} + /* END MERGE SLOT 2 */ /* BEGIN MERGE SLOT 3 */ diff --git a/library/bignum_mod.h b/library/bignum_mod.h index 95aaacc4df..f8c71d01ba 100644 --- a/library/bignum_mod.h +++ b/library/bignum_mod.h @@ -217,6 +217,40 @@ void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m ); /* BEGIN MERGE SLOT 2 */ +/** \brief Multiply two residues, returning the residue modulo the specified + * modulus. + * + * \note Currenty handles the case when `m->int_rep` is + * MBEDTLS_MPI_MOD_REP_MONTGOMERY. + * + * The size of the operation is determined by \p N. \p A and \p B must have + * the same number of limbs as \p N. + * + * \p X may be aliased to \p A or \p B, or even both, but may not overlap + * either otherwise. They may not alias \p N (since they must be in canonical + * form, they cannot == \p N). + * + * \param[out] X The address of the result MPI. + * This must be initialized. Must have enough limbs to + * store the full value of the result. + * On successful completion, \p X contains the result of + * the multiplication `A * B * R^-1` mod N where + * `R = 2^(biL *N->limbs)`. + * \param[in] A The address of the first MPI. This must be initialized. + * \param[in] B The address of the second MPI. This must be initialized. + * \param[in] N The address of the modulus. Used to perform a modulo + * operation on the result of the multiplication. + * + * \return \c 0 if successful. + * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if all the parameters does not + * have the same number of limbs or \p N is invalid. + * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. + */ +int mbedtls_mpi_mod_mul( mbedtls_mpi_mod_residue *X, + const mbedtls_mpi_mod_residue *A, + const mbedtls_mpi_mod_residue *B, + const mbedtls_mpi_mod_modulus *N ); + /* END MERGE SLOT 2 */ /* BEGIN MERGE SLOT 3 */ From eca74668c78e449dac6f05fc544f30a80a0a6313 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Tue, 13 Dec 2022 10:53:50 +0100 Subject: [PATCH 02/12] Add tests for mod_mul Signed-off-by: Gabor Mezei --- scripts/mbedtls_dev/bignum_mod.py | 19 ++ tests/suites/test_suite_bignum_mod.function | 186 +++++++++++++++++++ tests/suites/test_suite_bignum_mod.misc.data | 7 + 3 files changed, 212 insertions(+) diff --git a/scripts/mbedtls_dev/bignum_mod.py b/scripts/mbedtls_dev/bignum_mod.py index 25afe3053c..e0fc159393 100644 --- a/scripts/mbedtls_dev/bignum_mod.py +++ b/scripts/mbedtls_dev/bignum_mod.py @@ -31,6 +31,25 @@ class BignumModTarget(test_data_generation.BaseTarget): # BEGIN MERGE SLOT 2 +class BignumModMul(bignum_common.ModOperationCommon, + BignumModTarget): + """Test cases for bignum mpi_mod_mul().""" + symbol = "*" + test_function = "mpi_mod_mul" + test_name = "mbedtls_mpi_mod_mul" + input_style = "arch_split" + arity = 2 + + def arguments(self) -> List[str]: + return [bignum_common.quote_str(n) for n in [self.arg_a, + self.arg_b, + self.arg_n] + ] + self.result() + + def result(self) -> List[str]: + result = (self.int_a * self.int_b) % self.int_n + return [self.format_result(result)] + # END MERGE SLOT 2 # BEGIN MERGE SLOT 3 diff --git a/tests/suites/test_suite_bignum_mod.function b/tests/suites/test_suite_bignum_mod.function index 79f5134a90..7c407a8a69 100644 --- a/tests/suites/test_suite_bignum_mod.function +++ b/tests/suites/test_suite_bignum_mod.function @@ -2,6 +2,7 @@ #include "mbedtls/bignum.h" #include "mbedtls/entropy.h" #include "bignum_mod.h" +#include "bignum_mod_raw.h" #include "constant_time_internal.h" #include "test/constant_flow.h" @@ -102,6 +103,191 @@ exit: /* BEGIN MERGE SLOT 2 */ +/* BEGIN_CASE */ +void mpi_mod_mul( char * input_A, + char * input_B, + char * input_N, + char * result ) +{ + mbedtls_mpi_uint *A = NULL; + mbedtls_mpi_uint *B = NULL; + mbedtls_mpi_uint *N = NULL; + mbedtls_mpi_uint *X = NULL; + mbedtls_mpi_uint *R = NULL; + size_t limbs_A; + size_t limbs_B; + size_t limbs_N; + size_t limbs_R; + + mbedtls_mpi_mod_modulus m; + mbedtls_mpi_mod_modulus_init( &m ); + + TEST_EQUAL( mbedtls_test_read_mpi_core( &A, &limbs_A, input_A ), 0 ); + TEST_EQUAL( mbedtls_test_read_mpi_core( &B, &limbs_B, input_B ), 0 ); + TEST_EQUAL( mbedtls_test_read_mpi_core( &N, &limbs_N, input_N ), 0 ); + TEST_EQUAL( mbedtls_test_read_mpi_core( &R, &limbs_R, result ), 0 ); + + const size_t limbs = limbs_N; + const size_t bytes = limbs * sizeof( mbedtls_mpi_uint ); + + TEST_EQUAL( limbs_A, limbs ); + TEST_EQUAL( limbs_B, limbs ); + TEST_EQUAL( limbs_R, limbs ); + + ASSERT_ALLOC( X, limbs ); + + TEST_EQUAL( mbedtls_mpi_mod_modulus_setup( + &m, N, limbs, + MBEDTLS_MPI_MOD_REP_MONTGOMERY ), 0 ); + + mbedtls_mpi_mod_residue rA; + TEST_EQUAL( mbedtls_mpi_mod_residue_setup( &rA, &m, A, limbs ), 0 ); + + mbedtls_mpi_mod_residue rB; + TEST_EQUAL( mbedtls_mpi_mod_residue_setup( &rB, &m, B, limbs ), 0 ); + + mbedtls_mpi_mod_residue rX; + TEST_EQUAL( mbedtls_mpi_mod_residue_setup( &rX, &m, X, limbs ), 0 ); + + /* Convert to Montgomery representation */ + TEST_EQUAL( mbedtls_mpi_mod_raw_to_mont_rep( rA.p, &m ), 0 ); + TEST_EQUAL( mbedtls_mpi_mod_raw_to_mont_rep( rB.p, &m ), 0 ); + + TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rA, &rB, &m ), 0 ); + TEST_EQUAL( mbedtls_mpi_mod_raw_from_mont_rep( rX.p, &m ), 0 ); + ASSERT_COMPARE( rX.p, bytes, R, bytes ); + + /* alias X to A */ + memcpy( rX.p, rA.p, bytes ); + TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rX, &rB, &m ), 0 ); + TEST_EQUAL( mbedtls_mpi_mod_raw_from_mont_rep( rX.p, &m ), 0 ); + ASSERT_COMPARE( rX.p, bytes, R, bytes ); + + /* alias X to B */ + memcpy( rX.p, rB.p, bytes ); + TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rA, &rX, &m ), 0); + TEST_EQUAL( mbedtls_mpi_mod_raw_from_mont_rep( rX.p, &m ), 0 ); + ASSERT_COMPARE( rX.p, bytes, R, bytes ); + + /* A == B: alias A and B */ + if( memcmp( rA.p, rB.p, bytes ) == 0 ) + { + TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rA, &rA, &m ), 0 ); + TEST_EQUAL( mbedtls_mpi_mod_raw_from_mont_rep( rX.p, &m ), 0 ); + ASSERT_COMPARE( rX.p, bytes, R, bytes ); + + /* X, A, B all aliased together */ + memcpy( rX.p, rA.p, bytes ); + TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rX, &rX, &m ), 0 ); + TEST_EQUAL( mbedtls_mpi_mod_raw_from_mont_rep( rX.p, &m ), 0 ); + ASSERT_COMPARE( rX.p, bytes, R, bytes ); + } + + /* A != B: test B * A */ + else + { + TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rB, &rA, &m ), 0 ); + TEST_EQUAL( mbedtls_mpi_mod_raw_from_mont_rep( rX.p, &m ), 0 ); + ASSERT_COMPARE( rX.p, bytes, R, bytes ); + + /* B * A: alias X to A */ + memcpy( rX.p, rA.p, bytes ); + TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rB, &rX, &m ), 0 ); + TEST_EQUAL( mbedtls_mpi_mod_raw_from_mont_rep( rX.p, &m ), 0 ); + ASSERT_COMPARE( rX.p, bytes, R, bytes ); + + /* B + A: alias X to B */ + memcpy( rX.p, rB.p, bytes ); + TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rX, &rA, &m ), 0 ); + TEST_EQUAL( mbedtls_mpi_mod_raw_from_mont_rep( rX.p, &m ), 0 ); + ASSERT_COMPARE( rX.p, bytes, R, bytes ); + } + +exit: + mbedtls_mpi_mod_residue_release( &rA ); + mbedtls_mpi_mod_residue_release( &rB ); + mbedtls_mpi_mod_residue_release( &rX ); + mbedtls_mpi_mod_modulus_free( &m ); + + mbedtls_free( A ); + mbedtls_free( B ); + mbedtls_free( N ); + mbedtls_free( X ); + mbedtls_free( R ); +} +/* END_CASE */ + +/* BEGIN_CASE */ +void mpi_mod_mul_neg( char * input_A, + char * input_B, + char * input_N, + char * result, + int exp_ret ) +{ + mbedtls_mpi_uint *A = NULL; + mbedtls_mpi_uint *B = NULL; + mbedtls_mpi_uint *N = NULL; + mbedtls_mpi_uint *X = NULL; + mbedtls_mpi_uint *R = NULL; + size_t limbs_A = 0; + size_t limbs_B = 0; + size_t limbs_N = 0; + size_t limbs_X = 0; + + mbedtls_mpi_mod_modulus m; + mbedtls_mpi_mod_modulus_init( &m ); + + mbedtls_mpi_mod_modulus fake_m; + mbedtls_mpi_mod_modulus_init( &fake_m ); + + TEST_EQUAL( mbedtls_test_read_mpi_core( &A, &limbs_A, input_A ), 0 ); + TEST_EQUAL( mbedtls_test_read_mpi_core( &B, &limbs_B, input_B ), 0 ); + TEST_EQUAL( mbedtls_test_read_mpi_core( &N, &limbs_N, input_N ), 0 ); + TEST_EQUAL( mbedtls_test_read_mpi_core( &R, &limbs_X, result ), 0 ); + + ASSERT_ALLOC( X, limbs_X ); + + TEST_EQUAL( mbedtls_mpi_mod_modulus_setup( + &m, N, limbs_N, + MBEDTLS_MPI_MOD_REP_MONTGOMERY ), 0 ); + + mbedtls_mpi_mod_residue rA; + TEST_EQUAL( mbedtls_mpi_mod_residue_setup( &rA, &m, A, limbs_N ), 0 ); + rA.limbs = limbs_A; + + mbedtls_mpi_mod_residue rB; + TEST_EQUAL( mbedtls_mpi_mod_residue_setup( &rB, &m, B, limbs_N ), 0 ); + rB.limbs = limbs_B; + + mbedtls_mpi_mod_residue rX; + TEST_EQUAL( mbedtls_mpi_mod_residue_setup( &rX, &m, X, limbs_N ), 0 ); + rX.limbs = limbs_X; + + /* Convert to Montgomery representation */ + TEST_EQUAL( mbedtls_mpi_mod_raw_to_mont_rep( rA.p, &m ), 0 ); + TEST_EQUAL( mbedtls_mpi_mod_raw_to_mont_rep( rB.p, &m ), 0 ); + + TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rA, &rB, &m ), exp_ret ); + + /* Check when m is not initialized */ + TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rA, &rB, &fake_m ), + MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); + +exit: + mbedtls_mpi_mod_residue_release( &rA ); + mbedtls_mpi_mod_residue_release( &rB ); + mbedtls_mpi_mod_residue_release( &rX ); + mbedtls_mpi_mod_modulus_free( &m ); + mbedtls_mpi_mod_modulus_free( &fake_m ); + + mbedtls_free( A ); + mbedtls_free( B ); + mbedtls_free( N ); + mbedtls_free( X ); + mbedtls_free( R ); +} +/* END_CASE */ + /* END MERGE SLOT 2 */ /* BEGIN MERGE SLOT 3 */ diff --git a/tests/suites/test_suite_bignum_mod.misc.data b/tests/suites/test_suite_bignum_mod.misc.data index 6240e214ba..e36921141b 100644 --- a/tests/suites/test_suite_bignum_mod.misc.data +++ b/tests/suites/test_suite_bignum_mod.misc.data @@ -12,7 +12,14 @@ mpi_mod_setup:MBEDTLS_MPI_MOD_REP_MONTGOMERY:0 # END MERGE SLOT 1 # BEGIN MERGE SLOT 2 +Test mpi_mod_mul #1 N->limbs != A->limbs +mpi_mod_mul_neg:"1":"00000000000000000000000000000000":"f0000000000000000000000000000000":"0":MBEDTLS_ERR_MPI_BAD_INPUT_DATA +Test mpi_mod_mul #2 N->limbs != B->limbs +mpi_mod_mul_neg:"1234567890abcdef1234567890abcdef":"0":"f0000000000000000000000000000000":"0":MBEDTLS_ERR_MPI_BAD_INPUT_DATA + +Test mpi_mod_mul #3 N->limbs != X->limbs +mpi_mod_mul_neg:"1234567890abcdef1234567890abcdef":"00000000000000000000000000000000":"f0000000000000000000000000000000":"0":MBEDTLS_ERR_MPI_BAD_INPUT_DATA # END MERGE SLOT 2 # BEGIN MERGE SLOT 3 From 6a31b7252d610884d770bab9ffcd1b80936fb415 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Fri, 16 Dec 2022 15:24:03 +0100 Subject: [PATCH 03/12] Fix documentation Signed-off-by: Gabor Mezei --- library/bignum_mod.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/library/bignum_mod.h b/library/bignum_mod.h index f8c71d01ba..d2f6cee5e0 100644 --- a/library/bignum_mod.h +++ b/library/bignum_mod.h @@ -220,29 +220,29 @@ void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m ); /** \brief Multiply two residues, returning the residue modulo the specified * modulus. * - * \note Currenty handles the case when `m->int_rep` is + * \note Currently handles the case when `N->int_rep` is * MBEDTLS_MPI_MOD_REP_MONTGOMERY. * - * The size of the operation is determined by \p N. \p A and \p B must have - * the same number of limbs as \p N. + * The size of the operation is determined by \p N. \p A, \p B and \p X must + * all be associated with the modulus \p N and must all have the same number + * of limbs as \p N. * * \p X may be aliased to \p A or \p B, or even both, but may not overlap * either otherwise. They may not alias \p N (since they must be in canonical * form, they cannot == \p N). * - * \param[out] X The address of the result MPI. - * This must be initialized. Must have enough limbs to - * store the full value of the result. + * \param[out] X The address of the result MPI. Must have the same + * number of limbs as \p N. * On successful completion, \p X contains the result of * the multiplication `A * B * R^-1` mod N where - * `R = 2^(biL *N->limbs)`. - * \param[in] A The address of the first MPI. This must be initialized. - * \param[in] B The address of the second MPI. This must be initialized. + * `R = 2^(biL * N->limbs)`. + * \param[in] A The address of the first MPI. + * \param[in] B The address of the second MPI. * \param[in] N The address of the modulus. Used to perform a modulo * operation on the result of the multiplication. * * \return \c 0 if successful. - * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if all the parameters does not + * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if all the parameters do not * have the same number of limbs or \p N is invalid. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. */ From 2840884c35e966fe7ebc97576feccaecf6509cee Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Fri, 16 Dec 2022 15:24:35 +0100 Subject: [PATCH 04/12] Typo Signed-off-by: Gabor Mezei --- library/bignum_mod.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/bignum_mod.c b/library/bignum_mod.c index 0f511335d7..30289aaf07 100644 --- a/library/bignum_mod.c +++ b/library/bignum_mod.c @@ -187,7 +187,7 @@ int mbedtls_mpi_mod_mul( mbedtls_mpi_mod_residue *X, if( X->limbs != N->limbs || A->limbs != N->limbs || B->limbs != N->limbs ) return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; - mbedtls_mpi_uint *T = mbedtls_calloc( N->limbs * 2 + 1, ciL ); + mbedtls_mpi_uint *T = mbedtls_calloc( N->limbs * 2 + 1, ciL ); if( !T ) return MBEDTLS_ERR_MPI_ALLOC_FAILED; From 77b877d5a79277e445a15aed73b5c735fa074c63 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Fri, 16 Dec 2022 15:25:02 +0100 Subject: [PATCH 05/12] Generate operands in Mongomery representation for the test function Signed-off-by: Gabor Mezei --- scripts/mbedtls_dev/bignum_mod.py | 8 ++++---- tests/suites/test_suite_bignum_mod.function | 12 ------------ 2 files changed, 4 insertions(+), 16 deletions(-) diff --git a/scripts/mbedtls_dev/bignum_mod.py b/scripts/mbedtls_dev/bignum_mod.py index e0fc159393..1960723f1d 100644 --- a/scripts/mbedtls_dev/bignum_mod.py +++ b/scripts/mbedtls_dev/bignum_mod.py @@ -41,14 +41,14 @@ class BignumModMul(bignum_common.ModOperationCommon, arity = 2 def arguments(self) -> List[str]: - return [bignum_common.quote_str(n) for n in [self.arg_a, - self.arg_b, - self.arg_n] + return [self.format_result(self.to_montgomery(self.int_a)), + self.format_result(self.to_montgomery(self.int_b)), + bignum_common.quote_str(self.arg_n) ] + self.result() def result(self) -> List[str]: result = (self.int_a * self.int_b) % self.int_n - return [self.format_result(result)] + return [self.format_result(self.to_montgomery(result))] # END MERGE SLOT 2 diff --git a/tests/suites/test_suite_bignum_mod.function b/tests/suites/test_suite_bignum_mod.function index 7c407a8a69..51a910fd1d 100644 --- a/tests/suites/test_suite_bignum_mod.function +++ b/tests/suites/test_suite_bignum_mod.function @@ -149,37 +149,28 @@ void mpi_mod_mul( char * input_A, mbedtls_mpi_mod_residue rX; TEST_EQUAL( mbedtls_mpi_mod_residue_setup( &rX, &m, X, limbs ), 0 ); - /* Convert to Montgomery representation */ - TEST_EQUAL( mbedtls_mpi_mod_raw_to_mont_rep( rA.p, &m ), 0 ); - TEST_EQUAL( mbedtls_mpi_mod_raw_to_mont_rep( rB.p, &m ), 0 ); - TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rA, &rB, &m ), 0 ); - TEST_EQUAL( mbedtls_mpi_mod_raw_from_mont_rep( rX.p, &m ), 0 ); ASSERT_COMPARE( rX.p, bytes, R, bytes ); /* alias X to A */ memcpy( rX.p, rA.p, bytes ); TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rX, &rB, &m ), 0 ); - TEST_EQUAL( mbedtls_mpi_mod_raw_from_mont_rep( rX.p, &m ), 0 ); ASSERT_COMPARE( rX.p, bytes, R, bytes ); /* alias X to B */ memcpy( rX.p, rB.p, bytes ); TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rA, &rX, &m ), 0); - TEST_EQUAL( mbedtls_mpi_mod_raw_from_mont_rep( rX.p, &m ), 0 ); ASSERT_COMPARE( rX.p, bytes, R, bytes ); /* A == B: alias A and B */ if( memcmp( rA.p, rB.p, bytes ) == 0 ) { TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rA, &rA, &m ), 0 ); - TEST_EQUAL( mbedtls_mpi_mod_raw_from_mont_rep( rX.p, &m ), 0 ); ASSERT_COMPARE( rX.p, bytes, R, bytes ); /* X, A, B all aliased together */ memcpy( rX.p, rA.p, bytes ); TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rX, &rX, &m ), 0 ); - TEST_EQUAL( mbedtls_mpi_mod_raw_from_mont_rep( rX.p, &m ), 0 ); ASSERT_COMPARE( rX.p, bytes, R, bytes ); } @@ -187,19 +178,16 @@ void mpi_mod_mul( char * input_A, else { TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rB, &rA, &m ), 0 ); - TEST_EQUAL( mbedtls_mpi_mod_raw_from_mont_rep( rX.p, &m ), 0 ); ASSERT_COMPARE( rX.p, bytes, R, bytes ); /* B * A: alias X to A */ memcpy( rX.p, rA.p, bytes ); TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rB, &rX, &m ), 0 ); - TEST_EQUAL( mbedtls_mpi_mod_raw_from_mont_rep( rX.p, &m ), 0 ); ASSERT_COMPARE( rX.p, bytes, R, bytes ); /* B + A: alias X to B */ memcpy( rX.p, rB.p, bytes ); TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rX, &rA, &m ), 0 ); - TEST_EQUAL( mbedtls_mpi_mod_raw_from_mont_rep( rX.p, &m ), 0 ); ASSERT_COMPARE( rX.p, bytes, R, bytes ); } From 809baef2dd91dc344a89170485ffea9aeaad33f3 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Fri, 16 Dec 2022 16:31:59 +0100 Subject: [PATCH 06/12] Use helper functions to simplify test code Signed-off-by: Gabor Mezei --- tests/suites/test_suite_bignum_mod.function | 124 ++++++++------------ 1 file changed, 50 insertions(+), 74 deletions(-) diff --git a/tests/suites/test_suite_bignum_mod.function b/tests/suites/test_suite_bignum_mod.function index 51a910fd1d..6e25ff7200 100644 --- a/tests/suites/test_suite_bignum_mod.function +++ b/tests/suites/test_suite_bignum_mod.function @@ -109,99 +109,89 @@ void mpi_mod_mul( char * input_A, char * input_N, char * result ) { - mbedtls_mpi_uint *A = NULL; - mbedtls_mpi_uint *B = NULL; - mbedtls_mpi_uint *N = NULL; mbedtls_mpi_uint *X = NULL; - mbedtls_mpi_uint *R = NULL; - size_t limbs_A; - size_t limbs_B; - size_t limbs_N; - size_t limbs_R; mbedtls_mpi_mod_modulus m; mbedtls_mpi_mod_modulus_init( &m ); - TEST_EQUAL( mbedtls_test_read_mpi_core( &A, &limbs_A, input_A ), 0 ); - TEST_EQUAL( mbedtls_test_read_mpi_core( &B, &limbs_B, input_B ), 0 ); - TEST_EQUAL( mbedtls_test_read_mpi_core( &N, &limbs_N, input_N ), 0 ); - TEST_EQUAL( mbedtls_test_read_mpi_core( &R, &limbs_R, result ), 0 ); - - const size_t limbs = limbs_N; - const size_t bytes = limbs * sizeof( mbedtls_mpi_uint ); - - TEST_EQUAL( limbs_A, limbs ); - TEST_EQUAL( limbs_B, limbs ); - TEST_EQUAL( limbs_R, limbs ); - - ASSERT_ALLOC( X, limbs ); - - TEST_EQUAL( mbedtls_mpi_mod_modulus_setup( - &m, N, limbs, - MBEDTLS_MPI_MOD_REP_MONTGOMERY ), 0 ); + TEST_EQUAL( test_read_modulus( &m, MBEDTLS_MPI_MOD_REP_MONTGOMERY, input_N ), + 0 ); mbedtls_mpi_mod_residue rA; - TEST_EQUAL( mbedtls_mpi_mod_residue_setup( &rA, &m, A, limbs ), 0 ); + TEST_EQUAL( test_read_residue( &rA, &m, input_A, 0 ), 0 ); mbedtls_mpi_mod_residue rB; - TEST_EQUAL( mbedtls_mpi_mod_residue_setup( &rB, &m, B, limbs ), 0 ); + TEST_EQUAL( test_read_residue( &rB, &m, input_B, 0 ), 0 ); + + mbedtls_mpi_mod_residue rR; + TEST_EQUAL( test_read_residue( &rR, &m, result, 0 ), 0 ); + + const size_t limbs = m.limbs; + const size_t bytes = limbs * sizeof( mbedtls_mpi_uint ); + + TEST_EQUAL( rA.limbs, limbs ); + TEST_EQUAL( rB.limbs, limbs ); + TEST_EQUAL( rR.limbs, limbs ); + + ASSERT_ALLOC( X, limbs ); mbedtls_mpi_mod_residue rX; TEST_EQUAL( mbedtls_mpi_mod_residue_setup( &rX, &m, X, limbs ), 0 ); TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rA, &rB, &m ), 0 ); - ASSERT_COMPARE( rX.p, bytes, R, bytes ); + ASSERT_COMPARE( rX.p, bytes, rR.p, bytes ); /* alias X to A */ memcpy( rX.p, rA.p, bytes ); TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rX, &rB, &m ), 0 ); - ASSERT_COMPARE( rX.p, bytes, R, bytes ); + ASSERT_COMPARE( rX.p, bytes, rR.p, bytes ); /* alias X to B */ memcpy( rX.p, rB.p, bytes ); TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rA, &rX, &m ), 0); - ASSERT_COMPARE( rX.p, bytes, R, bytes ); + ASSERT_COMPARE( rX.p, bytes, rR.p, bytes ); /* A == B: alias A and B */ if( memcmp( rA.p, rB.p, bytes ) == 0 ) { TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rA, &rA, &m ), 0 ); - ASSERT_COMPARE( rX.p, bytes, R, bytes ); + ASSERT_COMPARE( rX.p, bytes, rR.p, bytes ); /* X, A, B all aliased together */ memcpy( rX.p, rA.p, bytes ); TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rX, &rX, &m ), 0 ); - ASSERT_COMPARE( rX.p, bytes, R, bytes ); + ASSERT_COMPARE( rX.p, bytes, rR.p, bytes ); } /* A != B: test B * A */ else { TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rB, &rA, &m ), 0 ); - ASSERT_COMPARE( rX.p, bytes, R, bytes ); + ASSERT_COMPARE( rX.p, bytes, rR.p, bytes ); /* B * A: alias X to A */ memcpy( rX.p, rA.p, bytes ); TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rB, &rX, &m ), 0 ); - ASSERT_COMPARE( rX.p, bytes, R, bytes ); + ASSERT_COMPARE( rX.p, bytes, rR.p, bytes ); /* B + A: alias X to B */ memcpy( rX.p, rB.p, bytes ); TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rX, &rA, &m ), 0 ); - ASSERT_COMPARE( rX.p, bytes, R, bytes ); + ASSERT_COMPARE( rX.p, bytes, rR.p, bytes ); } exit: mbedtls_mpi_mod_residue_release( &rA ); mbedtls_mpi_mod_residue_release( &rB ); + mbedtls_mpi_mod_residue_release( &rR ); mbedtls_mpi_mod_residue_release( &rX ); mbedtls_mpi_mod_modulus_free( &m ); - mbedtls_free( A ); - mbedtls_free( B ); - mbedtls_free( N ); + mbedtls_free( rA.p ); + mbedtls_free( rB.p ); + mbedtls_free( rR.p ); mbedtls_free( X ); - mbedtls_free( R ); + mbedtls_free( (mbedtls_mpi_uint *) m.p ); } /* END_CASE */ @@ -212,51 +202,36 @@ void mpi_mod_mul_neg( char * input_A, char * result, int exp_ret ) { - mbedtls_mpi_uint *A = NULL; - mbedtls_mpi_uint *B = NULL; - mbedtls_mpi_uint *N = NULL; mbedtls_mpi_uint *X = NULL; - mbedtls_mpi_uint *R = NULL; - size_t limbs_A = 0; - size_t limbs_B = 0; - size_t limbs_N = 0; - size_t limbs_X = 0; mbedtls_mpi_mod_modulus m; mbedtls_mpi_mod_modulus_init( &m ); - mbedtls_mpi_mod_modulus fake_m; - mbedtls_mpi_mod_modulus_init( &fake_m ); - - TEST_EQUAL( mbedtls_test_read_mpi_core( &A, &limbs_A, input_A ), 0 ); - TEST_EQUAL( mbedtls_test_read_mpi_core( &B, &limbs_B, input_B ), 0 ); - TEST_EQUAL( mbedtls_test_read_mpi_core( &N, &limbs_N, input_N ), 0 ); - TEST_EQUAL( mbedtls_test_read_mpi_core( &R, &limbs_X, result ), 0 ); - - ASSERT_ALLOC( X, limbs_X ); - - TEST_EQUAL( mbedtls_mpi_mod_modulus_setup( - &m, N, limbs_N, - MBEDTLS_MPI_MOD_REP_MONTGOMERY ), 0 ); + TEST_EQUAL( test_read_modulus( &m, MBEDTLS_MPI_MOD_REP_MONTGOMERY, input_N ), + 0 ); mbedtls_mpi_mod_residue rA; - TEST_EQUAL( mbedtls_mpi_mod_residue_setup( &rA, &m, A, limbs_N ), 0 ); - rA.limbs = limbs_A; + TEST_EQUAL( test_read_residue( &rA, &m, input_A, 1 ), 0 ); mbedtls_mpi_mod_residue rB; - TEST_EQUAL( mbedtls_mpi_mod_residue_setup( &rB, &m, B, limbs_N ), 0 ); - rB.limbs = limbs_B; + TEST_EQUAL( test_read_residue( &rB, &m, input_B, 1 ), 0 ); + + mbedtls_mpi_mod_residue rR; + TEST_EQUAL( test_read_residue( &rR, &m, result, 1 ), 0 ); + + const size_t limbs = m.limbs; + + ASSERT_ALLOC( X, limbs ); mbedtls_mpi_mod_residue rX; - TEST_EQUAL( mbedtls_mpi_mod_residue_setup( &rX, &m, X, limbs_N ), 0 ); - rX.limbs = limbs_X; - - /* Convert to Montgomery representation */ - TEST_EQUAL( mbedtls_mpi_mod_raw_to_mont_rep( rA.p, &m ), 0 ); - TEST_EQUAL( mbedtls_mpi_mod_raw_to_mont_rep( rB.p, &m ), 0 ); + TEST_EQUAL( mbedtls_mpi_mod_residue_setup( &rX, &m, X, limbs ), 0 ); + rX.limbs = rR.limbs; TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rA, &rB, &m ), exp_ret ); + mbedtls_mpi_mod_modulus fake_m; + mbedtls_mpi_mod_modulus_init( &fake_m ); + /* Check when m is not initialized */ TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rA, &rB, &fake_m ), MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); @@ -264,15 +239,16 @@ void mpi_mod_mul_neg( char * input_A, exit: mbedtls_mpi_mod_residue_release( &rA ); mbedtls_mpi_mod_residue_release( &rB ); + mbedtls_mpi_mod_residue_release( &rR ); mbedtls_mpi_mod_residue_release( &rX ); mbedtls_mpi_mod_modulus_free( &m ); mbedtls_mpi_mod_modulus_free( &fake_m ); - mbedtls_free( A ); - mbedtls_free( B ); - mbedtls_free( N ); + mbedtls_free( rA.p ); + mbedtls_free( rB.p ); + mbedtls_free( rR.p ); mbedtls_free( X ); - mbedtls_free( R ); + mbedtls_free( (mbedtls_mpi_uint *) m.p ); } /* END_CASE */ From 8a261646847b46bc27a59b64f48c5df02086104a Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Fri, 16 Dec 2022 17:18:28 +0100 Subject: [PATCH 07/12] Supress pylint's duplicated code warning Signed-off-by: Gabor Mezei --- scripts/mbedtls_dev/bignum_mod.py | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/mbedtls_dev/bignum_mod.py b/scripts/mbedtls_dev/bignum_mod.py index 1960723f1d..a83e1360ac 100644 --- a/scripts/mbedtls_dev/bignum_mod.py +++ b/scripts/mbedtls_dev/bignum_mod.py @@ -33,6 +33,7 @@ class BignumModTarget(test_data_generation.BaseTarget): class BignumModMul(bignum_common.ModOperationCommon, BignumModTarget): + # pylint:disable=duplicate-code """Test cases for bignum mpi_mod_mul().""" symbol = "*" test_function = "mpi_mod_mul" From 61fd1fb4b1e8aa5adf634804e250fa272ee23edc Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Tue, 20 Dec 2022 13:54:26 +0100 Subject: [PATCH 08/12] Calling the residue_release() is not needed Signed-off-by: Gabor Mezei --- tests/suites/test_suite_bignum_mod.function | 8 -------- 1 file changed, 8 deletions(-) diff --git a/tests/suites/test_suite_bignum_mod.function b/tests/suites/test_suite_bignum_mod.function index 6e25ff7200..c19c2f40f4 100644 --- a/tests/suites/test_suite_bignum_mod.function +++ b/tests/suites/test_suite_bignum_mod.function @@ -181,10 +181,6 @@ void mpi_mod_mul( char * input_A, } exit: - mbedtls_mpi_mod_residue_release( &rA ); - mbedtls_mpi_mod_residue_release( &rB ); - mbedtls_mpi_mod_residue_release( &rR ); - mbedtls_mpi_mod_residue_release( &rX ); mbedtls_mpi_mod_modulus_free( &m ); mbedtls_free( rA.p ); @@ -237,10 +233,6 @@ void mpi_mod_mul_neg( char * input_A, MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); exit: - mbedtls_mpi_mod_residue_release( &rA ); - mbedtls_mpi_mod_residue_release( &rB ); - mbedtls_mpi_mod_residue_release( &rR ); - mbedtls_mpi_mod_residue_release( &rX ); mbedtls_mpi_mod_modulus_free( &m ); mbedtls_mpi_mod_modulus_free( &fake_m ); From f9728137d853e35a7c3830220bc7eaade03fcbd1 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Tue, 20 Dec 2022 13:55:37 +0100 Subject: [PATCH 09/12] Fix the order of freeing memory Signed-off-by: Gabor Mezei --- tests/suites/test_suite_bignum_mod.function | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/suites/test_suite_bignum_mod.function b/tests/suites/test_suite_bignum_mod.function index c19c2f40f4..4914e1d89a 100644 --- a/tests/suites/test_suite_bignum_mod.function +++ b/tests/suites/test_suite_bignum_mod.function @@ -181,13 +181,13 @@ void mpi_mod_mul( char * input_A, } exit: - mbedtls_mpi_mod_modulus_free( &m ); - mbedtls_free( rA.p ); mbedtls_free( rB.p ); mbedtls_free( rR.p ); mbedtls_free( X ); mbedtls_free( (mbedtls_mpi_uint *) m.p ); + + mbedtls_mpi_mod_modulus_free( &m ); } /* END_CASE */ @@ -233,14 +233,14 @@ void mpi_mod_mul_neg( char * input_A, MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); exit: - mbedtls_mpi_mod_modulus_free( &m ); - mbedtls_mpi_mod_modulus_free( &fake_m ); - mbedtls_free( rA.p ); mbedtls_free( rB.p ); mbedtls_free( rR.p ); mbedtls_free( X ); mbedtls_free( (mbedtls_mpi_uint *) m.p ); + + mbedtls_mpi_mod_modulus_free( &m ); + mbedtls_mpi_mod_modulus_free( &fake_m ); } /* END_CASE */ From 496cd37bacbfa3fbd6239ae2d9a5f8cc83d04811 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Tue, 20 Dec 2022 13:56:16 +0100 Subject: [PATCH 10/12] Use equality checking for NULL value Signed-off-by: Gabor Mezei --- library/bignum_mod.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/bignum_mod.c b/library/bignum_mod.c index 30289aaf07..6ff1c4ecce 100644 --- a/library/bignum_mod.c +++ b/library/bignum_mod.c @@ -188,7 +188,7 @@ int mbedtls_mpi_mod_mul( mbedtls_mpi_mod_residue *X, return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; mbedtls_mpi_uint *T = mbedtls_calloc( N->limbs * 2 + 1, ciL ); - if( !T ) + if( T == NULL ) return MBEDTLS_ERR_MPI_ALLOC_FAILED; mbedtls_mpi_mod_raw_mul( X->p, A->p, B->p, N, T ); From 78c4fb45513847b8e08f5a5f84d26a799c6b3d29 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Tue, 20 Dec 2022 18:09:49 +0100 Subject: [PATCH 11/12] Fix possible uninitialization error Signed-off-by: Gabor Mezei --- tests/suites/test_suite_bignum_mod.function | 26 ++++++++++----------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/tests/suites/test_suite_bignum_mod.function b/tests/suites/test_suite_bignum_mod.function index 4914e1d89a..527146e300 100644 --- a/tests/suites/test_suite_bignum_mod.function +++ b/tests/suites/test_suite_bignum_mod.function @@ -111,20 +111,20 @@ void mpi_mod_mul( char * input_A, { mbedtls_mpi_uint *X = NULL; + mbedtls_mpi_mod_residue rA = { NULL, 0 }; + mbedtls_mpi_mod_residue rB = { NULL, 0 }; + mbedtls_mpi_mod_residue rR = { NULL, 0 }; + mbedtls_mpi_mod_residue rX = { NULL, 0 }; + mbedtls_mpi_mod_modulus m; mbedtls_mpi_mod_modulus_init( &m ); TEST_EQUAL( test_read_modulus( &m, MBEDTLS_MPI_MOD_REP_MONTGOMERY, input_N ), 0 ); - mbedtls_mpi_mod_residue rA; TEST_EQUAL( test_read_residue( &rA, &m, input_A, 0 ), 0 ); - - mbedtls_mpi_mod_residue rB; TEST_EQUAL( test_read_residue( &rB, &m, input_B, 0 ), 0 ); - - mbedtls_mpi_mod_residue rR; - TEST_EQUAL( test_read_residue( &rR, &m, result, 0 ), 0 ); + TEST_EQUAL( test_read_residue( &rR, &m, result, 0 ), 0 ); const size_t limbs = m.limbs; const size_t bytes = limbs * sizeof( mbedtls_mpi_uint ); @@ -135,7 +135,6 @@ void mpi_mod_mul( char * input_A, ASSERT_ALLOC( X, limbs ); - mbedtls_mpi_mod_residue rX; TEST_EQUAL( mbedtls_mpi_mod_residue_setup( &rX, &m, X, limbs ), 0 ); TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rA, &rB, &m ), 0 ); @@ -200,26 +199,25 @@ void mpi_mod_mul_neg( char * input_A, { mbedtls_mpi_uint *X = NULL; + mbedtls_mpi_mod_residue rA = { NULL, 0 }; + mbedtls_mpi_mod_residue rB = { NULL, 0 }; + mbedtls_mpi_mod_residue rR = { NULL, 0 }; + mbedtls_mpi_mod_residue rX = { NULL, 0 }; + mbedtls_mpi_mod_modulus m; mbedtls_mpi_mod_modulus_init( &m ); TEST_EQUAL( test_read_modulus( &m, MBEDTLS_MPI_MOD_REP_MONTGOMERY, input_N ), 0 ); - mbedtls_mpi_mod_residue rA; TEST_EQUAL( test_read_residue( &rA, &m, input_A, 1 ), 0 ); - - mbedtls_mpi_mod_residue rB; TEST_EQUAL( test_read_residue( &rB, &m, input_B, 1 ), 0 ); - - mbedtls_mpi_mod_residue rR; - TEST_EQUAL( test_read_residue( &rR, &m, result, 1 ), 0 ); + TEST_EQUAL( test_read_residue( &rR, &m, result, 1 ), 0 ); const size_t limbs = m.limbs; ASSERT_ALLOC( X, limbs ); - mbedtls_mpi_mod_residue rX; TEST_EQUAL( mbedtls_mpi_mod_residue_setup( &rX, &m, X, limbs ), 0 ); rX.limbs = rR.limbs; From f65c71fbe6f8b472f3398f4e490c97ca16899f24 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Wed, 21 Dec 2022 11:54:22 +0100 Subject: [PATCH 12/12] Fix possible uninitialization error Signed-off-by: Gabor Mezei --- tests/suites/test_suite_bignum_mod.function | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/suites/test_suite_bignum_mod.function b/tests/suites/test_suite_bignum_mod.function index 527146e300..8ab8ccf32a 100644 --- a/tests/suites/test_suite_bignum_mod.function +++ b/tests/suites/test_suite_bignum_mod.function @@ -207,6 +207,9 @@ void mpi_mod_mul_neg( char * input_A, mbedtls_mpi_mod_modulus m; mbedtls_mpi_mod_modulus_init( &m ); + mbedtls_mpi_mod_modulus fake_m; + mbedtls_mpi_mod_modulus_init( &fake_m ); + TEST_EQUAL( test_read_modulus( &m, MBEDTLS_MPI_MOD_REP_MONTGOMERY, input_N ), 0 ); @@ -223,9 +226,6 @@ void mpi_mod_mul_neg( char * input_A, TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rA, &rB, &m ), exp_ret ); - mbedtls_mpi_mod_modulus fake_m; - mbedtls_mpi_mod_modulus_init( &fake_m ); - /* Check when m is not initialized */ TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rA, &rB, &fake_m ), MBEDTLS_ERR_MPI_BAD_INPUT_DATA );