From adb23c30f933a6210ec41016460ed1cd82a30220 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Thu, 21 Nov 2024 17:35:20 +0100 Subject: [PATCH] Support an alternative prefix in psa_information.automatic_dependencies No change to the generated files (the new code isn't used yet). Signed-off-by: Gilles Peskine --- .../mbedtls_framework/crypto_data_tests.py | 6 ++---- scripts/mbedtls_framework/psa_information.py | 21 ++++++++++++++----- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/scripts/mbedtls_framework/crypto_data_tests.py b/scripts/mbedtls_framework/crypto_data_tests.py index 40bd5672c..5aef5c574 100644 --- a/scripts/mbedtls_framework/crypto_data_tests.py +++ b/scripts/mbedtls_framework/crypto_data_tests.py @@ -21,10 +21,8 @@ def psa_low_level_dependencies(*expressions: str) -> List[str]: This function generates MBEDTLS_PSA_BUILTIN_xxx symbols. """ - high_level = psa_information.automatic_dependencies(*expressions) - for dep in high_level: - assert dep.startswith('PSA_WANT_') - return ['MBEDTLS_PSA_BUILTIN_' + dep[9:] for dep in high_level] + return psa_information.automatic_dependencies(*expressions, + prefix='MBEDTLS_PSA_BUILTIN_') class HashPSALowLevel: diff --git a/scripts/mbedtls_framework/psa_information.py b/scripts/mbedtls_framework/psa_information.py index 4f0633cd2..1ff02da61 100644 --- a/scripts/mbedtls_framework/psa_information.py +++ b/scripts/mbedtls_framework/psa_information.py @@ -57,10 +57,16 @@ class Information: return constructors -def psa_want_symbol(name: str) -> str: - """Return the PSA_WANT_xxx symbol associated with a PSA crypto feature.""" +def psa_want_symbol(name: str, prefix: Optional[str] = None) -> str: + """Return the PSA_WANT_xxx symbol associated with a PSA crypto feature. + + You can use an altenative `prefix`, e.g. 'MBEDTLS_PSA_BUILTIN_' + when specifically testing builtin implementations. + """ + if prefix is None: + prefix = 'PSA_WANT_' if name.startswith('PSA_'): - return name[:4] + 'WANT_' + name[4:] + return prefix + name[4:] else: raise ValueError('Unable to determine the PSA_WANT_ symbol for ' + name) @@ -88,18 +94,23 @@ SYMBOLS_WITHOUT_DEPENDENCY = frozenset([ 'PSA_ALG_KEY_AGREEMENT', # chaining 'PSA_ALG_TRUNCATED_MAC', # modifier ]) -def automatic_dependencies(*expressions: str) -> List[str]: +def automatic_dependencies(*expressions: str, + prefix: Optional[str] = None) -> List[str]: """Infer dependencies of a test case by looking for PSA_xxx symbols. The arguments are strings which should be C expressions. Do not use string literals or comments as this function is not smart enough to skip them. + + `prefix`: prefix to use in dependencies. Defaults to ``'PSA_WANT_'``. + Use ``'MBEDTLS_PSA_BUILTIN_'`` when specifically testing + builtin implementations. """ used = set() for expr in expressions: used.update(re.findall(r'PSA_(?:ALG|ECC_FAMILY|DH_FAMILY|KEY_TYPE)_\w+', expr)) used.difference_update(SYMBOLS_WITHOUT_DEPENDENCY) - return sorted(psa_want_symbol(name) for name in used) + return sorted(psa_want_symbol(name, prefix=prefix) for name in used) # Define set of regular expressions and dependencies to optionally append # extra dependencies for test case based on key description.