From 13d60eb4fc6d976cc692775afa179d7bc75a5b04 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Mon, 25 Jan 2021 22:42:14 +0100 Subject: [PATCH] MacroCollector: default to not including intermediate macros By default, exclude macros whose numerical value is not a valid member of the semantic type (e.g. PSA_ALG_xxx_BASE is not itself an algorithm, only an intermediate value used to construct others). But do include them with include_intermediate=True, which generate_psa_constants.py does. Signed-off-by: Gilles Peskine --- scripts/generate_psa_constants.py | 3 +++ scripts/mbedtls_dev/macro_collector.py | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/scripts/generate_psa_constants.py b/scripts/generate_psa_constants.py index 4f8b87bb2..01c5a32a1 100755 --- a/scripts/generate_psa_constants.py +++ b/scripts/generate_psa_constants.py @@ -213,6 +213,9 @@ class CaseBuilder(macro_collector.PSAMacroCollector): 2. Call `write_file` to write ``psa_constant_names_generated.c``. """ + def __init__(self): + super().__init__(include_intermediate=True) + @staticmethod def _make_return_case(name): return 'case %(name)s: return "%(name)s";' % {'name': name} diff --git a/scripts/mbedtls_dev/macro_collector.py b/scripts/mbedtls_dev/macro_collector.py index 0f7be604d..ca277795c 100644 --- a/scripts/mbedtls_dev/macro_collector.py +++ b/scripts/mbedtls_dev/macro_collector.py @@ -22,7 +22,15 @@ class PSAMacroCollector: """Collect PSA crypto macro definitions from C header files. """ - def __init__(self): + def __init__(self, include_intermediate=False): + """Set up an object to collect PSA macro definitions. + + Call the read_file method of the constructed object on each header file. + + * include_intermediate: if true, include intermediate macros such as + PSA_XXX_BASE that do not designate semantic values. + """ + self.include_intermediate = include_intermediate self.statuses = set() self.key_types = set() self.key_types_from_curve = {} @@ -37,6 +45,11 @@ class PSAMacroCollector: def is_internal_name(self, name): """Whether this is an internal macro. Internal macros will be skipped.""" + if not self.include_intermediate: + if name.endswith('_BASE') or name.endswith('_NONE'): + return True + if '_CATEGORY_' in name: + return True return name.endswith('_FLAG') or name.endswith('MASK') # "#define" followed by a macro name with either no parameters