Tomás González | 734d22c | 2023-10-30 15:15:45 +0000 | [diff] [blame] | 1 | """Collect information about PSA cryptographic mechanisms. |
| 2 | """ |
| 3 | |
| 4 | # Copyright The Mbed TLS Contributors |
Tomás González | 5fae560 | 2023-11-13 11:45:12 +0000 | [diff] [blame] | 5 | # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 6 | |
Tomás González | 734d22c | 2023-10-30 15:15:45 +0000 | [diff] [blame] | 7 | |
| 8 | import re |
| 9 | from typing import Dict, FrozenSet, List, Optional |
| 10 | |
| 11 | from . import macro_collector |
Gilles Peskine | c7b58d5 | 2024-04-10 15:55:39 +0200 | [diff] [blame] | 12 | from . import test_case |
Tomás González | 734d22c | 2023-10-30 15:15:45 +0000 | [diff] [blame] | 13 | |
| 14 | |
| 15 | def psa_want_symbol(name: str) -> str: |
| 16 | """Return the PSA_WANT_xxx symbol associated with a PSA crypto feature.""" |
| 17 | if name.startswith('PSA_'): |
| 18 | return name[:4] + 'WANT_' + name[4:] |
| 19 | else: |
| 20 | raise ValueError('Unable to determine the PSA_WANT_ symbol for ' + name) |
| 21 | |
| 22 | def finish_family_dependency(dep: str, bits: int) -> str: |
| 23 | """Finish dep if it's a family dependency symbol prefix. |
| 24 | A family dependency symbol prefix is a PSA_WANT_ symbol that needs to be |
| 25 | qualified by the key size. If dep is such a symbol, finish it by adjusting |
| 26 | the prefix and appending the key size. Other symbols are left unchanged. |
| 27 | """ |
| 28 | return re.sub(r'_FAMILY_(.*)', r'_\1_' + str(bits), dep) |
| 29 | |
| 30 | def finish_family_dependencies(dependencies: List[str], bits: int) -> List[str]: |
| 31 | """Finish any family dependency symbol prefixes. |
| 32 | Apply `finish_family_dependency` to each element of `dependencies`. |
| 33 | """ |
| 34 | return [finish_family_dependency(dep, bits) for dep in dependencies] |
| 35 | |
| 36 | SYMBOLS_WITHOUT_DEPENDENCY = frozenset([ |
| 37 | 'PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG', # modifier, only in policies |
| 38 | 'PSA_ALG_AEAD_WITH_SHORTENED_TAG', # modifier |
| 39 | 'PSA_ALG_ANY_HASH', # only in policies |
| 40 | 'PSA_ALG_AT_LEAST_THIS_LENGTH_MAC', # modifier, only in policies |
| 41 | 'PSA_ALG_KEY_AGREEMENT', # chaining |
| 42 | 'PSA_ALG_TRUNCATED_MAC', # modifier |
| 43 | ]) |
| 44 | |
| 45 | def automatic_dependencies(*expressions: str) -> List[str]: |
| 46 | """Infer dependencies of a test case by looking for PSA_xxx symbols. |
| 47 | The arguments are strings which should be C expressions. Do not use |
| 48 | string literals or comments as this function is not smart enough to |
| 49 | skip them. |
| 50 | """ |
| 51 | used = set() |
| 52 | for expr in expressions: |
| 53 | used.update(re.findall(r'PSA_(?:ALG|ECC_FAMILY|KEY_TYPE)_\w+', expr)) |
| 54 | used.difference_update(SYMBOLS_WITHOUT_DEPENDENCY) |
| 55 | return sorted(psa_want_symbol(name) for name in used) |
| 56 | |
| 57 | # A temporary hack: at the time of writing, not all dependency symbols |
| 58 | # are implemented yet. Skip test cases for which the dependency symbols are |
| 59 | # not available. Once all dependency symbols are available, this hack must |
| 60 | # be removed so that a bug in the dependency symbols properly leads to a test |
| 61 | # failure. |
| 62 | def read_implemented_dependencies(filename: str) -> FrozenSet[str]: |
| 63 | return frozenset(symbol |
| 64 | for line in open(filename) |
| 65 | for symbol in re.findall(r'\bPSA_WANT_\w+\b', line)) |
| 66 | _implemented_dependencies = None #type: Optional[FrozenSet[str]] #pylint: disable=invalid-name |
| 67 | |
| 68 | def hack_dependencies_not_implemented(dependencies: List[str]) -> None: |
| 69 | global _implemented_dependencies #pylint: disable=global-statement,invalid-name |
| 70 | if _implemented_dependencies is None: |
| 71 | _implemented_dependencies = \ |
| 72 | read_implemented_dependencies('include/psa/crypto_config.h') |
Gilles Peskine | c113b42 | 2024-04-10 16:27:19 +0200 | [diff] [blame] | 73 | _implemented_dependencies = _implemented_dependencies.union( |
| 74 | read_implemented_dependencies('include/mbedtls/config_psa.h')) |
Gilles Peskine | c6fe12a | 2024-04-10 16:36:13 +0200 | [diff] [blame] | 75 | for dep in dependencies: |
| 76 | dep = dep.lstrip('!') |
| 77 | if dep.startswith('PSA_WANT') and dep not in _implemented_dependencies: |
| 78 | dependencies.append('DEPENDENCY_NOT_IMPLEMENTED_YET_' + dep) |
Gilles Peskine | c3b261a | 2024-04-10 17:19:04 +0200 | [diff] [blame] | 79 | dependencies.sort() |
Tomás González | 734d22c | 2023-10-30 15:15:45 +0000 | [diff] [blame] | 80 | |
| 81 | class Information: |
| 82 | """Gather information about PSA constructors.""" |
| 83 | |
| 84 | def __init__(self) -> None: |
| 85 | self.constructors = self.read_psa_interface() |
| 86 | |
| 87 | @staticmethod |
| 88 | def remove_unwanted_macros( |
| 89 | constructors: macro_collector.PSAMacroEnumerator |
| 90 | ) -> None: |
| 91 | # Mbed TLS doesn't support finite-field DH yet and will not support |
| 92 | # finite-field DSA. Don't attempt to generate any related test case. |
| 93 | constructors.key_types.discard('PSA_KEY_TYPE_DH_KEY_PAIR') |
| 94 | constructors.key_types.discard('PSA_KEY_TYPE_DH_PUBLIC_KEY') |
| 95 | constructors.key_types.discard('PSA_KEY_TYPE_DSA_KEY_PAIR') |
| 96 | constructors.key_types.discard('PSA_KEY_TYPE_DSA_PUBLIC_KEY') |
| 97 | |
| 98 | def read_psa_interface(self) -> macro_collector.PSAMacroEnumerator: |
| 99 | """Return the list of known key types, algorithms, etc.""" |
| 100 | constructors = macro_collector.InputsForTest() |
| 101 | header_file_names = ['include/psa/crypto_values.h', |
| 102 | 'include/psa/crypto_extra.h'] |
| 103 | test_suites = ['tests/suites/test_suite_psa_crypto_metadata.data'] |
| 104 | for header_file_name in header_file_names: |
| 105 | constructors.parse_header(header_file_name) |
| 106 | for test_cases in test_suites: |
| 107 | constructors.parse_test_cases(test_cases) |
| 108 | self.remove_unwanted_macros(constructors) |
| 109 | constructors.gather_arguments() |
| 110 | return constructors |
Gilles Peskine | c7b58d5 | 2024-04-10 15:55:39 +0200 | [diff] [blame] | 111 | |
| 112 | |
| 113 | class TestCase(test_case.TestCase): |
| 114 | """A PSA test case with automatically inferred dependencies.""" |
| 115 | |
| 116 | def __init__(self) -> None: |
| 117 | super().__init__() |
| 118 | self.key_bits = None #type: Optional[int] |
| 119 | |
| 120 | def set_key_bits(self, key_bits: Optional[int]) -> None: |
| 121 | """Use the given key size for automatic dependency generation. |
| 122 | |
| 123 | Call this function before set_arguments() if relevant. |
| 124 | |
| 125 | This is only relevant for ECC and DH keys. For other key types, |
| 126 | this information is ignored. |
| 127 | """ |
| 128 | self.key_bits = key_bits |
| 129 | |
| 130 | def set_arguments(self, arguments: List[str]) -> None: |
| 131 | """Set test case arguments and automatically infer dependencies.""" |
| 132 | super().set_arguments(arguments) |
| 133 | dependencies = automatic_dependencies(*arguments) |
| 134 | if self.key_bits is not None: |
| 135 | dependencies = finish_family_dependencies(dependencies, self.key_bits) |
Gilles Peskine | d3286af | 2024-04-10 16:40:10 +0200 | [diff] [blame^] | 136 | hack_dependencies_not_implemented(dependencies) |
Gilles Peskine | c7b58d5 | 2024-04-10 15:55:39 +0200 | [diff] [blame] | 137 | self.dependencies += dependencies |