PSA test case generation: dependency inference class: key generation

Use psa_information.TestCase for positive test cases for key generation.

The caller remains responsible for tweaking dependencies for some key
types (public keys for which the test is a negative case, RSA which requires
an additional dependency).

No change to the generated output.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
Gilles Peskine 2024-04-10 16:07:29 +02:00
parent d3286af1b7
commit 6281cf432f

View File

@ -124,21 +124,18 @@ class KeyTypeNotSupported:
def test_case_for_key_generation( def test_case_for_key_generation(
key_type: str, bits: int, key_type: str, bits: int,
dependencies: List[str],
*args: str, *args: str,
result: str = '' result: str = ''
) -> test_case.TestCase: ) -> test_case.TestCase:
"""Return one test case exercising a key generation. """Return one test case exercising a key generation.
""" """
psa_information.hack_dependencies_not_implemented(dependencies) tc = psa_information.TestCase()
tc = test_case.TestCase()
short_key_type = crypto_knowledge.short_expression(key_type) short_key_type = crypto_knowledge.short_expression(key_type)
tc.set_description('PSA {} {}-bit' tc.set_description('PSA {} {}-bit'
.format(short_key_type, bits)) .format(short_key_type, bits))
tc.set_dependencies(sorted(dependencies))
tc.set_function('generate_key') tc.set_function('generate_key')
tc.set_key_bits(bits)
tc.set_arguments([key_type] + list(args) + [result]) tc.set_arguments([key_type] + list(args) + [result])
return tc return tc
class KeyGenerate: class KeyGenerate:
@ -160,28 +157,25 @@ class KeyGenerate:
PSA_ERROR_INVALID_ARGUMENT status is expected. PSA_ERROR_INVALID_ARGUMENT status is expected.
""" """
result = 'PSA_SUCCESS' result = 'PSA_SUCCESS'
import_dependencies = [psa_information.psa_want_symbol(kt.name)]
if kt.params is not None:
import_dependencies += [psa_information.psa_want_symbol(sym)
for i, sym in enumerate(kt.params)]
if kt.name.endswith('_PUBLIC_KEY'): if kt.name.endswith('_PUBLIC_KEY'):
# The library checks whether the key type is a public key generically,
# before it reaches a point where it needs support for the specific key
# type, so it returns INVALID_ARGUMENT for unsupported public key types.
generate_dependencies = []
result = 'PSA_ERROR_INVALID_ARGUMENT' result = 'PSA_ERROR_INVALID_ARGUMENT'
else:
generate_dependencies = import_dependencies
if kt.name == 'PSA_KEY_TYPE_RSA_KEY_PAIR':
generate_dependencies.append("MBEDTLS_GENPRIME")
for bits in kt.sizes_to_test(): for bits in kt.sizes_to_test():
yield test_case_for_key_generation( tc = test_case_for_key_generation(
kt.expression, bits, kt.expression, bits,
psa_information.finish_family_dependencies(generate_dependencies, bits),
str(bits), str(bits),
result result
) )
if result == 'PSA_ERROR_INVALID_ARGUMENT':
# The library checks whether the key type is a public key generically,
# before it reaches a point where it needs support for the specific key
# type, so it returns INVALID_ARGUMENT for unsupported public key types.
tc.set_dependencies([])
elif kt.name == 'PSA_KEY_TYPE_RSA_KEY_PAIR':
# A necessary deviation because PSA_WANT symbols don't
# distinguish between key generation and usage, but for
# RSA key generation has an extra requirement.
tc.dependencies.insert(0, 'MBEDTLS_GENPRIME')
yield tc
def test_cases_for_key_generation(self) -> Iterator[test_case.TestCase]: def test_cases_for_key_generation(self) -> Iterator[test_case.TestCase]:
"""Generate test cases that exercise the generation of keys.""" """Generate test cases that exercise the generation of keys."""