From 4c7cf7d742dce297c772fdf94f2de9d8a7237dab Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Wed, 9 Nov 2022 14:07:43 +0100 Subject: [PATCH 01/10] Add low level subtraction with modulus Signed-off-by: Gabor Mezei --- library/bignum_mod_raw.c | 10 ++++++++++ library/bignum_mod_raw.h | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/library/bignum_mod_raw.c b/library/bignum_mod_raw.c index b43add77d3..9a8e3ee9a2 100644 --- a/library/bignum_mod_raw.c +++ b/library/bignum_mod_raw.c @@ -108,6 +108,16 @@ int mbedtls_mpi_mod_raw_write( const mbedtls_mpi_uint *A, /* BEGIN MERGE SLOT 2 */ +void mbedtls_mpi_mod_raw_sub( mbedtls_mpi_uint *X, + const mbedtls_mpi_uint *A, + const mbedtls_mpi_uint *B, + const mbedtls_mpi_mod_modulus *N ) +{ + mbedtls_mpi_uint c = mbedtls_mpi_core_sub( X, A, B, N->limbs ); + + (void) mbedtls_mpi_core_add_if( X, N->p, N->limbs, c ); +} + /* END MERGE SLOT 2 */ /* BEGIN MERGE SLOT 3 */ diff --git a/library/bignum_mod_raw.h b/library/bignum_mod_raw.h index f738e917e1..0f1b0c0de1 100644 --- a/library/bignum_mod_raw.h +++ b/library/bignum_mod_raw.h @@ -144,6 +144,26 @@ int mbedtls_mpi_mod_raw_write( const mbedtls_mpi_uint *A, /* BEGIN MERGE SLOT 2 */ +/** \brief Perform a subtraction of two MPIs and return the modulus + * of the result. + * + * The size of the operation is determined by \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[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. Use to perform a modulu + * operation on the result of the subtraction. + * + * \note Both \p A and \p B must be smaller than the modulus \p N. + */ +void mbedtls_mpi_mod_raw_sub( mbedtls_mpi_uint *X, + const mbedtls_mpi_uint *A, + const mbedtls_mpi_uint *B, + const mbedtls_mpi_mod_modulus *N ); + /* END MERGE SLOT 2 */ /* BEGIN MERGE SLOT 3 */ From cefe03a10cb0e6010670cc9c265c6c1891130f6b Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Tue, 15 Nov 2022 18:50:17 +0100 Subject: [PATCH 02/10] Add tests for low level subtraction with modulus Signed-off-by: Gabor Mezei --- tests/suites/test_suite_bignum_mod_raw.data | 39 ++++++++++++++ .../suites/test_suite_bignum_mod_raw.function | 53 +++++++++++++++++++ 2 files changed, 92 insertions(+) diff --git a/tests/suites/test_suite_bignum_mod_raw.data b/tests/suites/test_suite_bignum_mod_raw.data index 8cbd918f88..9290ef4f99 100644 --- a/tests/suites/test_suite_bignum_mod_raw.data +++ b/tests/suites/test_suite_bignum_mod_raw.data @@ -185,6 +185,45 @@ mpi_mod_raw_cond_swap:"00000000FFFFFFFF55555555AAAAAAAA":"FEDCBA9876543210FEDCBA # BEGIN MERGE SLOT 2 +mbedtls_mpi_mod_raw_sub: 0 - 0, mod 2 +mpi_mod_raw_sub:"0":"0":"2":"0" + +mbedtls_mpi_mod_raw_sub: 0 - 1, mod 2 +mpi_mod_raw_sub:"0":"1":"2":"1" + +mbedtls_mpi_mod_raw_sub: 1 - 0, mod 2 +mpi_mod_raw_sub:"1":"0":"2":"1" + +mbedtls_mpi_mod_raw_sub: 9 - 2, mod 10 +mpi_mod_raw_sub:"9":"2":"A":"7" + +mbedtls_mpi_mod_raw_sub: 6 - 7, mod 10 +mpi_mod_raw_sub:"6":"7":"A":"9" + +mbedtls_mpi_mod_raw_sub: 4 byte values, first is bigger +mpi_mod_raw_sub:"7DE15401":"5553FE19":"971F63D9":"288D55E8" + +mbedtls_mpi_mod_raw_sub: 4 byte values, second is bigger +mpi_mod_raw_sub:"58AB5FB1":"B7AFCADC":"DBD9D998":"7CD56E6D" + +mbedtls_mpi_mod_raw_sub: 8 byte values, first is bigger +mpi_mod_raw_sub:"8FE975EFDF264BDF":"6A4E956C95C1649E":"BB63D38C3AF7922E":"259AE0834964E741" + +mbedtls_mpi_mod_raw_sub: 8 byte values, second is bigger +mpi_mod_raw_sub:"0D6A093316013F68":"39DA803DB5C41434":"509876EAB3D1C663":"2427FFE0140EF197" + +mbedtls_mpi_mod_raw_sub: 16 byte values, first is bigger +mpi_mod_raw_sub:"94789C046DEC921C4EF12561271EEB12":"3AD8A050F6CC00354FDE5557E1839859":"A52A910D219A87F4F174FD9481873CEE":"599FFBB3772091E6FF12D009459B52B9" + +mbedtls_mpi_mod_raw_sub: 16 byte values, second is bigger +mpi_mod_raw_sub:"14D92E888E1274EDC37CA73B20B1F8BF":"8B7281E720762FD849948DCFA3CCC7F6":"94D3A7DE6456850BF05EB88B486DD4CD":"1E3A547FD1F2CA216A46D1F6C5530596" + +mbedtls_mpi_mod_raw_sub: 256 byte values, first is bigger +mpi_mod_raw_sub:"A39EE6C3FACCF07EEDDF235E8B31DBF843D2040AE2076B357EDD65FA16E639B9AA22451B9BA9E740343DC4BB0B1E6991B58084E1294A8849A326CAAC736514448D3D45464D5AC9D965046CD258923D892A027C89F4801DC754CAFC0418CF673D04D05E56074F46F59BD89CDE1D11168623293B564141DF7098EBCA253B10B4756C8897E4D23190AC943CE03AAFA7A76BCB8A44D8C76A3ABEC8F854BAD7333E80AAF2FA9A8D8BF148FD257396CD3B8E1418F0EE7C148146BF3DABF22C42E22A5397AC1CFFD77222D16700CDAAC8D9AB6CE1FD7A9DC7C826617748A7489AB77535F51BD9671FBCD1ADA63B4B780F46D325856F91DE68316CA34FEC645055614096":"66FF62A95E9CB6DE7242182484F223EDC6AE1E480773854A40A9EA2AD2349EA134722BCB9D8F55DBFA535CB919CE9A4933660F38DC08848D8A1442044AE3519D697DC85D999F525428A93D8347D975A54FDC46233402DD0E19E67F2146D49DB2990C88C0548DC14EE82E4A56F9C23B0119EC624FDF889D29BA4AB2B85040537649CF210C8F5712F0FD5B05CDA71949A1070FE9D28BF2D57D1D0D5DFD290E97060CA67A83F7C6F463E359E50102C6452E0EEAC3C0638264748466BA836C6CDAEB38B86F3A002324EE8C8690EC4E832692EA0216F71196E883209B62379B73E4474994554ADD57E4095A1004F475923EA44F553F325483328F7776A8DEB6A330F7":"CBB1EA630FEB95DCD8E54CE32CDC2CB7C42FDF8174B534F95247575399CAACBCAC7B6B35494E64308866ADD71C3AAB3875A0444635275F08EB77B52039559B1C9764EA1DA7F36676C4FAEB6F210DC6934156DB306ABDD75460CE3728C307A4829B7015CF1F4C7F086D30AE3B1045353FD590E36383F354AC8CCD38B65887D27F00B2862E0DD1DA0C5003BE346E4DB3980728334EE8DBABD649CE893F17CE64468E552F02EFBF4AF29F75CE032507412531B7B64F4B08BA971E83B3564602BB07129782879E04960319CBD885F6E83AD71D26C14FD5382087D24A6C7D201D0A25B772D194ED64EDC4B153BBB64CC51947752F6AF4E0DBE50EBBF215531144C139":"3C9F841A9C3039A07B9D0B3A063FB80A7D23E5C2DA93E5EB3E337BCF44B19B1875B0194FFE1A916439EA6801F14FCF48821A75A84D4203BC191288A82881C2A723BF7CE8B3BB77853C5B2F4F10B8C7E3DA263666C07D40B93AE47CE2D1FAC98A6BC3D595B2C185A6B3AA5287234EDB85093CD90661B94246DEA1176CEAD060FF22B976D842DA7DBB96E1DA6D088E5DCAC47A5B063B776541ABEAF6BDAE24A77A9E4C801695C4FCE519CB8E95CA7548E60A062ABBB0FEE24AB94537A8D6754F685EF3ADC5D74EFDE2DA7A3CBE7A5684D9F7FB63A6B6313DDE56AD4510FF4390EEAB87841C4264EDA44C2B468399B49481361A52AC13AE3A13D875BB719EBE0F9F" + +mbedtls_mpi_mod_raw_sub: 256 byte values, second is bigger +mpi_mod_raw_sub:"974663D69B8161A296AB77AD971DDA0672BE34E1EE5DB71D68AF76926CFB8C698AB19B45CD6F829260403839088DDF7E5339966D2D51DAC9E6EABB5C797739953D7537FC4B59EF089E5D0845C5501ECBEDECE2575AE8FC67D46E0B59DDED9528D66EF664D8415887CEC9641C0CBCB45B5EA921263B025EC4A7389695C95F7F17737FB4D42B8DA97BB8B4173276156D87E82F6EC157E1980EFBF0D9144CAB42797B5B0B0FA3D4008034456488202F213A7C28C19D514C111530F6895FAAA243BEFCDAB5CAEF4908F31AA2736BE287B81677F36A22C698FE964F88CDF8EE3DC7780735986627297BF63CA9EE8E9754FD443CE45E50165CF444A5CF538657D7E028":"A3B1E589E726DF39C69C7FFAF4A98D20D38F2DFB4DBCDF25FB95B93405C3DF0A2BE1940A3B5F9076389434D943750B48F5C44D13EB5C4502484D21BE3E7CC17BFC4C8AA12EE2EDF0014A64435678A489D0D28EFE2C54F5FBE262A32D27EF8B0172D3E8A40CF00856EDA21BFCD516DE3899838C535031DE227440B6A97B9A58398B6D8DB2CCE10230D9E738655B30022E737CF716F7C6E1C27D30E30FBA57C1A0224B929C6496B9D3DE40B8258211D8889C747069274F30887A717A95FEA4A4AE292F7ECAAFA69995B2ACA5A98F2AB850B291C3B935B260D598189732D2F9BAECC3A49603AEE5AA14D02393834C66B7F3D41D266264806F46533FC1044E52EF67":"B28F0A9BC32356B8DA0A7C0B96E0A7BF1A724C723486B13E050CE1A87A1FCA8868A3DA57C2F04EC578759A32EA96B3113EFA757586BBF93EE422CA2EE5A649D4B38D07729314B4E6E652886027DA61FD5AF4A2B1C38F5465D6B0310DF95D389CCC748961EDA8F24F57B18E3C485467CC13898325A77B609996EC23A9C312A37C135BBB49C50956B7FB2FD4FFE7A1F85538C19E8D84255184B6D98ED49F506831A1CA5198706F887D6F243E49CBC803B205F1FF87CA6E18859BFDB1FADF084F7F696B0C2AC7742ECC37A8270FAF00454D4C152C18AAE80D17B84B3E03CC38BD68E2D28A903F3CB2DC5C4FD2341E5F323B164AF996ADCF45619101BA65AC6B12EE":"A62388E8777DD921AA1973BE3954F4A4B9A15358D527893572269F06E15777E7C773E193550040E1A0219D92AFAF87469C6FBECEC8B18F0682C063CD20A0C1EDF4B5B4CDAF8BB5FF83652C6296B1DC3F780EF60AF2235AD1C8BB993AAF5B42C4300F9722B8FA428038D8D65B7FFA3DEED8AF17F8924BE13BC9E4039610D7CA59FB6DE26B23B5FE02D9FCB3CD028763AEAD741637E44007D1359984D931A3E90AFAD9CA0BAFACCF29C528EAAC69E54C63E5A650BBF46AF9125282C0C48B05EE903D16432B07169E299F9DF4D2025D45131176D2823BCEAAD86FBB74C9E77CC9F426638CF2B78084BDC8D62D3F694D778B7F1231845FABCA5FE3914CE7B5F003AF" + # END MERGE SLOT 2 # BEGIN MERGE SLOT 3 diff --git a/tests/suites/test_suite_bignum_mod_raw.function b/tests/suites/test_suite_bignum_mod_raw.function index 4adccce25b..22a73e0323 100644 --- a/tests/suites/test_suite_bignum_mod_raw.function +++ b/tests/suites/test_suite_bignum_mod_raw.function @@ -275,6 +275,59 @@ exit: /* BEGIN MERGE SLOT 2 */ +/* BEGIN_CASE */ +void mpi_mod_raw_sub( 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 *res = NULL; + size_t limbs_A; + size_t limbs_B; + size_t limbs_N; + size_t limbs_res; + + 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( &res, &limbs_res, result ), 0 ); + + size_t limbs = limbs_N; + size_t bytes = limbs * sizeof( mbedtls_mpi_uint ); + + TEST_ASSERT( limbs_A <= limbs ); + TEST_ASSERT( limbs_B <= limbs ); + TEST_ASSERT( limbs_res <= limbs ); + + ASSERT_ALLOC( X, limbs ); + + mbedtls_mpi_mod_modulus m; + mbedtls_mpi_mod_modulus_init( &m ); + + TEST_EQUAL( mbedtls_mpi_mod_modulus_setup( + &m, N, limbs, + MBEDTLS_MPI_MOD_EXT_REP_BE, + MBEDTLS_MPI_MOD_REP_MONTGOMERY ), 0 ); + + mbedtls_mpi_mod_raw_sub( X, A, B, &m ); + + ASSERT_COMPARE( X, bytes, res, bytes ); + +exit: + mbedtls_free( A ); + mbedtls_free( B ); + mbedtls_free( X ); + mbedtls_free( res ); + + mbedtls_mpi_mod_modulus_free( &m ); + mbedtls_free( N ); +} +/* END_CASE */ + /* END MERGE SLOT 2 */ /* BEGIN MERGE SLOT 3 */ From c426d9b6cc48d2e957c1f0ddeceb2810e878bc24 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Tue, 15 Nov 2022 18:51:20 +0100 Subject: [PATCH 03/10] Add generated test for low level subtraction with modulus Signed-off-by: Gabor Mezei --- scripts/mbedtls_dev/bignum_mod_raw.py | 90 +++++++++++++++++++++++++++ tests/CMakeLists.txt | 1 + tests/Makefile | 1 + 3 files changed, 92 insertions(+) diff --git a/scripts/mbedtls_dev/bignum_mod_raw.py b/scripts/mbedtls_dev/bignum_mod_raw.py index 60f2feded6..5d4bda2a7d 100644 --- a/scripts/mbedtls_dev/bignum_mod_raw.py +++ b/scripts/mbedtls_dev/bignum_mod_raw.py @@ -30,6 +30,96 @@ class BignumModRawTarget(test_data_generation.BaseTarget): # BEGIN MERGE SLOT 2 +class BignumModRawSub(BignumModRawOperation): + """Test cases for bignum mod raw sub.""" + count = 0 + symbol = "-" + test_function = "mpi_mod_raw_sub" + test_name = "mbedtls_mpi_mod_raw_sub" + unique_combinations_only = False + + input_values = [ + "0", "1", "fe", "ff", "fffe", "ffff", + "fffffffffffffffe", "ffffffffffffffff", + "fffffffffffffffffffffffffffffffe", + "ffffffffffffffffffffffffffffffff", + "1234567890abcdef01234567890abcdef0", + "3653f8dd9b1f282e4067c3584ee207f8da94e3e8ab73738f", + "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe", + "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0", + ( + "14c15c910b11ad28cc21ce88d0060cc54278c2614e1bcb383bb4a570294c4ea3" + "738d243a6e58d5ca49c7b59b995253fd6c79a3de69f85e3131f3b9238224b122" + "c3e4a892d9196ada4fcfa583e1df8af9b474c7e89286a1754abcb06ae8abb93f" + "01d89a024cdce7a6d7288ff68c320f89f1347e0cdd905ecfd160c5d0ef412ed6" + ) + ] + + modulus_values = [ + "7", "ff", + "d1c127a667786703830500038ebaef20e5a3e2dc378fb75b" + "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff43", + "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff67", + ( + "c93ba7ec74d96f411ba008bdb78e63ff11bb5df46a51e16b2c9d156f8e4e18ab" + "f5e052cb01f47d0d1925a77f60991577e128fb6f52f34a27950a594baadd3d80" + "57abeb222cf3cca962db16abf79f2ada5bd29ab2f51244bf295eff9f6aaba130" + "2efc449b128be75eeaca04bc3c1a155d11d14e8be32a2c8287b3996cf6ad5223" + ), + ( + "5c083126e978d4fdf3b645a1cac083126e978d4fdf3b645a1cac083126e978d4" + "fdf3b645a1cac083126e978d4fdf3b645a1cac083126e978d4fdf3b645a1cac0" + "83126e978d4fdf3b645a1cac083126e978d4fdf3b645a1cac083126e978d4fdf" + "3b645a1cac083126e978d4fdf3b645a1cac083126e978d4fdf3b645a1cac05d2" + ) + ] + + descr_tpl = '{} #{} \"{}\" - \"{}\" % \"{}\".' + + BITS_IN_LIMB = 32 + + @property + def boundary(self) -> int: + return self.int_n + + @property + def x(self): # pylint: disable=invalid-name + return (self.int_a - self.int_b) % self.int_n if self.int_n > 0 else 0 + + @property + def hex_x(self) -> str: + return format(self.x, 'x').zfill(self.hex_digits) + + def description(self) -> str: + return self.descr_tpl.format(self.test_name, + self.count, + self.int_a, + self.int_b, + self.int_n) + + def arguments(self) -> List[str]: + return [bignum_common.quote_str(n) for n in [self.hex_a, + self.hex_b, + self.hex_n, + self.hex_x]] + + def result(self) -> List[str]: + return [self.hex_x] + + @classmethod + def generate_function_tests(cls) -> Iterator[test_case.TestCase]: + for a_value, b_value in cls.get_value_pairs(): + int_a = bignum_common.hex_to_int(a_value) + int_b = bignum_common.hex_to_int(b_value) + highest = max(int_a, int_b) + + # Choose a modulus bigger then the arguments + for n_value in cls.modulus_values: + int_n = bignum_common.hex_to_int(n_value) + if highest < int_n: + yield cls(n_value, a_value, b_value, cls.BITS_IN_LIMB).create_test_case() + # END MERGE SLOT 2 # BEGIN MERGE SLOT 3 diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index c23cb6b3d9..0ef6fdbc4c 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -70,6 +70,7 @@ if(GEN_FILES) ${CMAKE_CURRENT_SOURCE_DIR}/../tests/scripts/generate_bignum_tests.py ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/bignum_common.py ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/bignum_core.py + ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/bignum_mod_raw.py ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/test_case.py ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/test_data_generation.py ) diff --git a/tests/Makefile b/tests/Makefile index 7c08f54e14..0b31cdd076 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -94,6 +94,7 @@ $(GENERATED_BIGNUM_DATA_FILES): generated_bignum_test_data generated_bignum_test_data: scripts/generate_bignum_tests.py generated_bignum_test_data: ../scripts/mbedtls_dev/bignum_common.py generated_bignum_test_data: ../scripts/mbedtls_dev/bignum_core.py +generated_bignum_test_data: ../scripts/mbedtls_dev/bignum_mod_raw.py generated_bignum_test_data: ../scripts/mbedtls_dev/test_case.py generated_bignum_test_data: ../scripts/mbedtls_dev/test_data_generation.py generated_bignum_test_data: From 3411e949cdc202824634af489ad5492ef46210d9 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Wed, 16 Nov 2022 11:31:00 +0100 Subject: [PATCH 04/10] Cas variable to proper type Signed-off-by: Gabor Mezei --- library/bignum_mod_raw.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/bignum_mod_raw.c b/library/bignum_mod_raw.c index 9a8e3ee9a2..6c694b0c58 100644 --- a/library/bignum_mod_raw.c +++ b/library/bignum_mod_raw.c @@ -115,7 +115,7 @@ void mbedtls_mpi_mod_raw_sub( mbedtls_mpi_uint *X, { mbedtls_mpi_uint c = mbedtls_mpi_core_sub( X, A, B, N->limbs ); - (void) mbedtls_mpi_core_add_if( X, N->p, N->limbs, c ); + (void) mbedtls_mpi_core_add_if( X, N->p, N->limbs, (unsigned) c ); } /* END MERGE SLOT 2 */ From 02d2313829b1dad945b95873a502fd8dcd248559 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Wed, 23 Nov 2022 13:09:43 +0100 Subject: [PATCH 05/10] Fix documentation Signed-off-by: Gabor Mezei --- library/bignum_mod_raw.h | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/library/bignum_mod_raw.h b/library/bignum_mod_raw.h index 0f1b0c0de1..05fa9d6ac8 100644 --- a/library/bignum_mod_raw.h +++ b/library/bignum_mod_raw.h @@ -144,20 +144,22 @@ int mbedtls_mpi_mod_raw_write( const mbedtls_mpi_uint *A, /* BEGIN MERGE SLOT 2 */ -/** \brief Perform a subtraction of two MPIs and return the modulus - * of the result. +/** \brief Subtract two MPIs, returning the residue modulo the specified + * modulus. * - * The size of the operation is determined by \p N. + * 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. * * \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[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. Use to perform a modulu + * \param[in] N The address of the modulus. Used to perform a modulo * operation on the result of the subtraction. - * - * \note Both \p A and \p B must be smaller than the modulus \p N. */ void mbedtls_mpi_mod_raw_sub( mbedtls_mpi_uint *X, const mbedtls_mpi_uint *A, From 68a45e0aaff5409e75d2eee01d179dcbd29e59fd Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Wed, 23 Nov 2022 13:12:51 +0100 Subject: [PATCH 06/10] Fix potential not initialized warning Signed-off-by: Gabor Mezei --- tests/suites/test_suite_bignum_mod_raw.function | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/suites/test_suite_bignum_mod_raw.function b/tests/suites/test_suite_bignum_mod_raw.function index 22a73e0323..aaf26ca794 100644 --- a/tests/suites/test_suite_bignum_mod_raw.function +++ b/tests/suites/test_suite_bignum_mod_raw.function @@ -291,6 +291,9 @@ void mpi_mod_raw_sub( char * input_A, size_t limbs_N; size_t limbs_res; + 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 ); @@ -305,9 +308,6 @@ void mpi_mod_raw_sub( char * input_A, ASSERT_ALLOC( X, limbs ); - mbedtls_mpi_mod_modulus m; - mbedtls_mpi_mod_modulus_init( &m ); - TEST_EQUAL( mbedtls_mpi_mod_modulus_setup( &m, N, limbs, MBEDTLS_MPI_MOD_EXT_REP_BE, From 4d3f3c54305b06c084ea8e9b6f64339d6e5174bc Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Wed, 23 Nov 2022 13:14:15 +0100 Subject: [PATCH 07/10] Fix the checking of the used limbs Signed-off-by: Gabor Mezei --- tests/suites/test_suite_bignum_mod_raw.function | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/suites/test_suite_bignum_mod_raw.function b/tests/suites/test_suite_bignum_mod_raw.function index aaf26ca794..256ec9dcb7 100644 --- a/tests/suites/test_suite_bignum_mod_raw.function +++ b/tests/suites/test_suite_bignum_mod_raw.function @@ -302,9 +302,9 @@ void mpi_mod_raw_sub( char * input_A, size_t limbs = limbs_N; size_t bytes = limbs * sizeof( mbedtls_mpi_uint ); - TEST_ASSERT( limbs_A <= limbs ); - TEST_ASSERT( limbs_B <= limbs ); - TEST_ASSERT( limbs_res <= limbs ); + TEST_EQUAL( limbs_A, limbs ); + TEST_EQUAL( limbs_B, limbs ); + TEST_EQUAL( limbs_res, limbs ); ASSERT_ALLOC( X, limbs ); From b3b34666575f0a1ac8e828fa5efb12d42ecb4da5 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Wed, 23 Nov 2022 13:17:13 +0100 Subject: [PATCH 08/10] Test subtraction if the parameters are aliased to each other Signed-off-by: Gabor Mezei --- .../suites/test_suite_bignum_mod_raw.function | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/tests/suites/test_suite_bignum_mod_raw.function b/tests/suites/test_suite_bignum_mod_raw.function index 256ec9dcb7..2d9e412280 100644 --- a/tests/suites/test_suite_bignum_mod_raw.function +++ b/tests/suites/test_suite_bignum_mod_raw.function @@ -314,9 +314,29 @@ void mpi_mod_raw_sub( char * input_A, MBEDTLS_MPI_MOD_REP_MONTGOMERY ), 0 ); mbedtls_mpi_mod_raw_sub( X, A, B, &m ); - ASSERT_COMPARE( X, bytes, res, bytes ); + /* alias X to A */ + memcpy( X, A, bytes ); + mbedtls_mpi_mod_raw_sub( X, X, B, &m ); + ASSERT_COMPARE( X, bytes, res, bytes ); + + /* alias X to B */ + memcpy( X, B, bytes ); + mbedtls_mpi_mod_raw_sub( X, A, X, &m ); + ASSERT_COMPARE( X, bytes, res, bytes ); + + /* A == B: alias A and B */ + if( memcmp( A, B, bytes ) == 0 ) + { + mbedtls_mpi_mod_raw_sub( X, A, A, &m ); + ASSERT_COMPARE( X, bytes, res, bytes ); + + /* X, A, B all aliased together */ + memcpy( X, A, bytes ); + mbedtls_mpi_mod_raw_sub( X, X, X, &m ); + ASSERT_COMPARE( X, bytes, res, bytes ); + } exit: mbedtls_free( A ); mbedtls_free( B ); From 6b3c0c59433d5354e255fbdc3e6c5a37299a34b4 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Wed, 23 Nov 2022 16:45:05 +0100 Subject: [PATCH 09/10] Update the test case generator Signed-off-by: Gabor Mezei --- scripts/mbedtls_dev/bignum_mod_raw.py | 93 ++++----------------------- 1 file changed, 11 insertions(+), 82 deletions(-) diff --git a/scripts/mbedtls_dev/bignum_mod_raw.py b/scripts/mbedtls_dev/bignum_mod_raw.py index 5d4bda2a7d..c271048548 100644 --- a/scripts/mbedtls_dev/bignum_mod_raw.py +++ b/scripts/mbedtls_dev/bignum_mod_raw.py @@ -30,95 +30,24 @@ class BignumModRawTarget(test_data_generation.BaseTarget): # BEGIN MERGE SLOT 2 -class BignumModRawSub(BignumModRawOperation): - """Test cases for bignum mod raw sub.""" - count = 0 +class BignumModRawSub(bignum_common.ModOperationCommon, + BignumModRawTarget): + """Test cases for bignum mpi_mod_raw_sub().""" symbol = "-" test_function = "mpi_mod_raw_sub" test_name = "mbedtls_mpi_mod_raw_sub" - unique_combinations_only = False - - input_values = [ - "0", "1", "fe", "ff", "fffe", "ffff", - "fffffffffffffffe", "ffffffffffffffff", - "fffffffffffffffffffffffffffffffe", - "ffffffffffffffffffffffffffffffff", - "1234567890abcdef01234567890abcdef0", - "3653f8dd9b1f282e4067c3584ee207f8da94e3e8ab73738f", - "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe", - "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", - "1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0", - ( - "14c15c910b11ad28cc21ce88d0060cc54278c2614e1bcb383bb4a570294c4ea3" - "738d243a6e58d5ca49c7b59b995253fd6c79a3de69f85e3131f3b9238224b122" - "c3e4a892d9196ada4fcfa583e1df8af9b474c7e89286a1754abcb06ae8abb93f" - "01d89a024cdce7a6d7288ff68c320f89f1347e0cdd905ecfd160c5d0ef412ed6" - ) - ] - - modulus_values = [ - "7", "ff", - "d1c127a667786703830500038ebaef20e5a3e2dc378fb75b" - "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff43", - "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff67", - ( - "c93ba7ec74d96f411ba008bdb78e63ff11bb5df46a51e16b2c9d156f8e4e18ab" - "f5e052cb01f47d0d1925a77f60991577e128fb6f52f34a27950a594baadd3d80" - "57abeb222cf3cca962db16abf79f2ada5bd29ab2f51244bf295eff9f6aaba130" - "2efc449b128be75eeaca04bc3c1a155d11d14e8be32a2c8287b3996cf6ad5223" - ), - ( - "5c083126e978d4fdf3b645a1cac083126e978d4fdf3b645a1cac083126e978d4" - "fdf3b645a1cac083126e978d4fdf3b645a1cac083126e978d4fdf3b645a1cac0" - "83126e978d4fdf3b645a1cac083126e978d4fdf3b645a1cac083126e978d4fdf" - "3b645a1cac083126e978d4fdf3b645a1cac083126e978d4fdf3b645a1cac05d2" - ) - ] - - descr_tpl = '{} #{} \"{}\" - \"{}\" % \"{}\".' - - BITS_IN_LIMB = 32 - - @property - def boundary(self) -> int: - return self.int_n - - @property - def x(self): # pylint: disable=invalid-name - return (self.int_a - self.int_b) % self.int_n if self.int_n > 0 else 0 - - @property - def hex_x(self) -> str: - return format(self.x, 'x').zfill(self.hex_digits) - - def description(self) -> str: - return self.descr_tpl.format(self.test_name, - self.count, - self.int_a, - self.int_b, - self.int_n) + input_style = "fixed" + arity = 2 def arguments(self) -> List[str]: - return [bignum_common.quote_str(n) for n in [self.hex_a, - self.hex_b, - self.hex_n, - self.hex_x]] + 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]: - return [self.hex_x] - - @classmethod - def generate_function_tests(cls) -> Iterator[test_case.TestCase]: - for a_value, b_value in cls.get_value_pairs(): - int_a = bignum_common.hex_to_int(a_value) - int_b = bignum_common.hex_to_int(b_value) - highest = max(int_a, int_b) - - # Choose a modulus bigger then the arguments - for n_value in cls.modulus_values: - int_n = bignum_common.hex_to_int(n_value) - if highest < int_n: - yield cls(n_value, a_value, b_value, cls.BITS_IN_LIMB).create_test_case() + result = (self.int_a - self.int_b) % self.int_n + return [self.format_result(result)] # END MERGE SLOT 2 From cbcbf4e4340b92380960ac7fe1267b9ab869a8f6 Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Thu, 24 Nov 2022 11:48:59 +0100 Subject: [PATCH 10/10] Remove hand-written tests got raw_mod_sub The generated tests cover all off the hand-written tests. Signed-off-by: Gabor Mezei --- tests/suites/test_suite_bignum_mod_raw.data | 39 --------------------- 1 file changed, 39 deletions(-) diff --git a/tests/suites/test_suite_bignum_mod_raw.data b/tests/suites/test_suite_bignum_mod_raw.data index 9290ef4f99..8cbd918f88 100644 --- a/tests/suites/test_suite_bignum_mod_raw.data +++ b/tests/suites/test_suite_bignum_mod_raw.data @@ -185,45 +185,6 @@ mpi_mod_raw_cond_swap:"00000000FFFFFFFF55555555AAAAAAAA":"FEDCBA9876543210FEDCBA # BEGIN MERGE SLOT 2 -mbedtls_mpi_mod_raw_sub: 0 - 0, mod 2 -mpi_mod_raw_sub:"0":"0":"2":"0" - -mbedtls_mpi_mod_raw_sub: 0 - 1, mod 2 -mpi_mod_raw_sub:"0":"1":"2":"1" - -mbedtls_mpi_mod_raw_sub: 1 - 0, mod 2 -mpi_mod_raw_sub:"1":"0":"2":"1" - -mbedtls_mpi_mod_raw_sub: 9 - 2, mod 10 -mpi_mod_raw_sub:"9":"2":"A":"7" - -mbedtls_mpi_mod_raw_sub: 6 - 7, mod 10 -mpi_mod_raw_sub:"6":"7":"A":"9" - -mbedtls_mpi_mod_raw_sub: 4 byte values, first is bigger -mpi_mod_raw_sub:"7DE15401":"5553FE19":"971F63D9":"288D55E8" - -mbedtls_mpi_mod_raw_sub: 4 byte values, second is bigger -mpi_mod_raw_sub:"58AB5FB1":"B7AFCADC":"DBD9D998":"7CD56E6D" - -mbedtls_mpi_mod_raw_sub: 8 byte values, first is bigger -mpi_mod_raw_sub:"8FE975EFDF264BDF":"6A4E956C95C1649E":"BB63D38C3AF7922E":"259AE0834964E741" - -mbedtls_mpi_mod_raw_sub: 8 byte values, second is bigger -mpi_mod_raw_sub:"0D6A093316013F68":"39DA803DB5C41434":"509876EAB3D1C663":"2427FFE0140EF197" - -mbedtls_mpi_mod_raw_sub: 16 byte values, first is bigger -mpi_mod_raw_sub:"94789C046DEC921C4EF12561271EEB12":"3AD8A050F6CC00354FDE5557E1839859":"A52A910D219A87F4F174FD9481873CEE":"599FFBB3772091E6FF12D009459B52B9" - -mbedtls_mpi_mod_raw_sub: 16 byte values, second is bigger -mpi_mod_raw_sub:"14D92E888E1274EDC37CA73B20B1F8BF":"8B7281E720762FD849948DCFA3CCC7F6":"94D3A7DE6456850BF05EB88B486DD4CD":"1E3A547FD1F2CA216A46D1F6C5530596" - -mbedtls_mpi_mod_raw_sub: 256 byte values, first is bigger -mpi_mod_raw_sub:"A39EE6C3FACCF07EEDDF235E8B31DBF843D2040AE2076B357EDD65FA16E639B9AA22451B9BA9E740343DC4BB0B1E6991B58084E1294A8849A326CAAC736514448D3D45464D5AC9D965046CD258923D892A027C89F4801DC754CAFC0418CF673D04D05E56074F46F59BD89CDE1D11168623293B564141DF7098EBCA253B10B4756C8897E4D23190AC943CE03AAFA7A76BCB8A44D8C76A3ABEC8F854BAD7333E80AAF2FA9A8D8BF148FD257396CD3B8E1418F0EE7C148146BF3DABF22C42E22A5397AC1CFFD77222D16700CDAAC8D9AB6CE1FD7A9DC7C826617748A7489AB77535F51BD9671FBCD1ADA63B4B780F46D325856F91DE68316CA34FEC645055614096":"66FF62A95E9CB6DE7242182484F223EDC6AE1E480773854A40A9EA2AD2349EA134722BCB9D8F55DBFA535CB919CE9A4933660F38DC08848D8A1442044AE3519D697DC85D999F525428A93D8347D975A54FDC46233402DD0E19E67F2146D49DB2990C88C0548DC14EE82E4A56F9C23B0119EC624FDF889D29BA4AB2B85040537649CF210C8F5712F0FD5B05CDA71949A1070FE9D28BF2D57D1D0D5DFD290E97060CA67A83F7C6F463E359E50102C6452E0EEAC3C0638264748466BA836C6CDAEB38B86F3A002324EE8C8690EC4E832692EA0216F71196E883209B62379B73E4474994554ADD57E4095A1004F475923EA44F553F325483328F7776A8DEB6A330F7":"CBB1EA630FEB95DCD8E54CE32CDC2CB7C42FDF8174B534F95247575399CAACBCAC7B6B35494E64308866ADD71C3AAB3875A0444635275F08EB77B52039559B1C9764EA1DA7F36676C4FAEB6F210DC6934156DB306ABDD75460CE3728C307A4829B7015CF1F4C7F086D30AE3B1045353FD590E36383F354AC8CCD38B65887D27F00B2862E0DD1DA0C5003BE346E4DB3980728334EE8DBABD649CE893F17CE64468E552F02EFBF4AF29F75CE032507412531B7B64F4B08BA971E83B3564602BB07129782879E04960319CBD885F6E83AD71D26C14FD5382087D24A6C7D201D0A25B772D194ED64EDC4B153BBB64CC51947752F6AF4E0DBE50EBBF215531144C139":"3C9F841A9C3039A07B9D0B3A063FB80A7D23E5C2DA93E5EB3E337BCF44B19B1875B0194FFE1A916439EA6801F14FCF48821A75A84D4203BC191288A82881C2A723BF7CE8B3BB77853C5B2F4F10B8C7E3DA263666C07D40B93AE47CE2D1FAC98A6BC3D595B2C185A6B3AA5287234EDB85093CD90661B94246DEA1176CEAD060FF22B976D842DA7DBB96E1DA6D088E5DCAC47A5B063B776541ABEAF6BDAE24A77A9E4C801695C4FCE519CB8E95CA7548E60A062ABBB0FEE24AB94537A8D6754F685EF3ADC5D74EFDE2DA7A3CBE7A5684D9F7FB63A6B6313DDE56AD4510FF4390EEAB87841C4264EDA44C2B468399B49481361A52AC13AE3A13D875BB719EBE0F9F" - -mbedtls_mpi_mod_raw_sub: 256 byte values, second is bigger -mpi_mod_raw_sub:"974663D69B8161A296AB77AD971DDA0672BE34E1EE5DB71D68AF76926CFB8C698AB19B45CD6F829260403839088DDF7E5339966D2D51DAC9E6EABB5C797739953D7537FC4B59EF089E5D0845C5501ECBEDECE2575AE8FC67D46E0B59DDED9528D66EF664D8415887CEC9641C0CBCB45B5EA921263B025EC4A7389695C95F7F17737FB4D42B8DA97BB8B4173276156D87E82F6EC157E1980EFBF0D9144CAB42797B5B0B0FA3D4008034456488202F213A7C28C19D514C111530F6895FAAA243BEFCDAB5CAEF4908F31AA2736BE287B81677F36A22C698FE964F88CDF8EE3DC7780735986627297BF63CA9EE8E9754FD443CE45E50165CF444A5CF538657D7E028":"A3B1E589E726DF39C69C7FFAF4A98D20D38F2DFB4DBCDF25FB95B93405C3DF0A2BE1940A3B5F9076389434D943750B48F5C44D13EB5C4502484D21BE3E7CC17BFC4C8AA12EE2EDF0014A64435678A489D0D28EFE2C54F5FBE262A32D27EF8B0172D3E8A40CF00856EDA21BFCD516DE3899838C535031DE227440B6A97B9A58398B6D8DB2CCE10230D9E738655B30022E737CF716F7C6E1C27D30E30FBA57C1A0224B929C6496B9D3DE40B8258211D8889C747069274F30887A717A95FEA4A4AE292F7ECAAFA69995B2ACA5A98F2AB850B291C3B935B260D598189732D2F9BAECC3A49603AEE5AA14D02393834C66B7F3D41D266264806F46533FC1044E52EF67":"B28F0A9BC32356B8DA0A7C0B96E0A7BF1A724C723486B13E050CE1A87A1FCA8868A3DA57C2F04EC578759A32EA96B3113EFA757586BBF93EE422CA2EE5A649D4B38D07729314B4E6E652886027DA61FD5AF4A2B1C38F5465D6B0310DF95D389CCC748961EDA8F24F57B18E3C485467CC13898325A77B609996EC23A9C312A37C135BBB49C50956B7FB2FD4FFE7A1F85538C19E8D84255184B6D98ED49F506831A1CA5198706F887D6F243E49CBC803B205F1FF87CA6E18859BFDB1FADF084F7F696B0C2AC7742ECC37A8270FAF00454D4C152C18AAE80D17B84B3E03CC38BD68E2D28A903F3CB2DC5C4FD2341E5F323B164AF996ADCF45619101BA65AC6B12EE":"A62388E8777DD921AA1973BE3954F4A4B9A15358D527893572269F06E15777E7C773E193550040E1A0219D92AFAF87469C6FBECEC8B18F0682C063CD20A0C1EDF4B5B4CDAF8BB5FF83652C6296B1DC3F780EF60AF2235AD1C8BB993AAF5B42C4300F9722B8FA428038D8D65B7FFA3DEED8AF17F8924BE13BC9E4039610D7CA59FB6DE26B23B5FE02D9FCB3CD028763AEAD741637E44007D1359984D931A3E90AFAD9CA0BAFACCF29C528EAAC69E54C63E5A650BBF46AF9125282C0C48B05EE903D16432B07169E299F9DF4D2025D45131176D2823BCEAAD86FBB74C9E77CC9F426638CF2B78084BDC8D62D3F694D778B7F1231845FABCA5FE3914CE7B5F003AF" - # END MERGE SLOT 2 # BEGIN MERGE SLOT 3