Add mbedtls_mpi_core_ct_uint_table_lookup tests

Signed-off-by: Janos Follath <janos.follath@arm.com>
This commit is contained in:
Janos Follath 2022-10-26 19:10:29 +01:00
parent e50f2f1a8e
commit 032924f297
2 changed files with 75 additions and 0 deletions

View File

@ -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

View File

@ -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 */