diff --git a/scripts/mbedtls_dev/bignum_core.py b/scripts/mbedtls_dev/bignum_core.py index 0d238e714b..e88f469a14 100644 --- a/scripts/mbedtls_dev/bignum_core.py +++ b/scripts/mbedtls_dev/bignum_core.py @@ -69,6 +69,44 @@ class BignumCoreShiftR(BignumCoreTarget, metaclass=ABCMeta): for count in counts: yield cls(input_hex, descr, count).create_test_case() +class BignumCoreCTLookup(BignumCoreTarget, metaclass=ABCMeta): + """Test cases for mbedtls_mpi_core_ct_uint_table_lookup().""" + count = 0 + test_function = "mpi_core_ct_uint_table_lookup" + test_name = "Constant time MPI table lookup" + + bitsizes = [ + (32, "One limb"), + (192, "Smallest curve sized"), + (512, "Largest curve sized"), + (2048, "Small FF/RSA sized"), + (4096, "Large FF/RSA sized"), + ] + + window_sizes = [ 0, 1, 2, 3, 4, 5, 6 ] + + def __init__(self, + bitsize: int, descr: str, window_size: int) -> None: + self.bitsize = bitsize + self.bitsize_description = descr + self.window_size = window_size + + def arguments(self) -> List[str]: + return [str(self.bitsize), str(self.window_size)] + + def description(self) -> str: + return '{} - {} MPI with {} bit window'.format( + BignumCoreCTLookup.test_name, + self.bitsize_description, + self.window_size + ) + + @classmethod + def generate_function_tests(cls) -> Iterator[test_case.TestCase]: + for bitsize, bitsize_description in cls.bitsizes: + for window_size in cls.window_sizes: + yield (cls(bitsize, bitsize_description, window_size) + .create_test_case()) class BignumCoreOperation(bignum_common.OperationCommon, BignumCoreTarget, metaclass=ABCMeta): #pylint: disable=abstract-method diff --git a/tests/suites/test_suite_bignum_core.function b/tests/suites/test_suite_bignum_core.function index 57c2f37387..696d873eaf 100644 --- a/tests/suites/test_suite_bignum_core.function +++ b/tests/suites/test_suite_bignum_core.function @@ -941,3 +941,40 @@ exit: mbedtls_mpi_free( &RR_REF ); } /* END_CASE */ + +/* BEGIN_CASE */ +void mpi_core_ct_uint_table_lookup( int bitlen, int window_size ) +{ + size_t limbs = BITS_TO_LIMBS( bitlen ); + size_t count = ( (size_t) 1 ) << window_size; + + mbedtls_mpi_uint *table = NULL; + ASSERT_ALLOC( table, limbs * count ); + + mbedtls_mpi_uint *dest = NULL; + ASSERT_ALLOC( dest, limbs ); + + /* + * Fill the table with a unique counter so that differences are easily + * detected. (And have their relationship to the index relatively non-trivial just + * to be sure.) + */ + for( size_t i = 0; i < count * limbs; i++ ) + { + table[i] = ~i - 1; + } + + for( size_t i = 0; i < count; i++ ) + { + memset( dest, 0x00, limbs * sizeof( *dest ) ); + mbedtls_mpi_core_ct_uint_table_lookup( dest, table, limbs, count, i ); + + mbedtls_mpi_uint *current = table + i * limbs; + ASSERT_COMPARE( dest, limbs * sizeof( *dest ), + current, limbs * sizeof( *current ) ); + } + +exit: + ; +} +/* END_CASE */