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 |
Gilles Peskine | 519762b | 2024-04-10 20:41:51 +0200 | [diff] [blame] | 9 | from typing import Dict, FrozenSet, Iterator, List, Optional, Set |
Tomás González | 734d22c | 2023-10-30 15:15:45 +0000 | [diff] [blame] | 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 | |
Tomás González | 734d22c | 2023-10-30 15:15:45 +0000 | [diff] [blame] | 57 | |
| 58 | class Information: |
| 59 | """Gather information about PSA constructors.""" |
| 60 | |
| 61 | def __init__(self) -> None: |
| 62 | self.constructors = self.read_psa_interface() |
| 63 | |
| 64 | @staticmethod |
| 65 | def remove_unwanted_macros( |
| 66 | constructors: macro_collector.PSAMacroEnumerator |
| 67 | ) -> None: |
Gilles Peskine | 0311b21 | 2024-04-11 11:38:29 +0200 | [diff] [blame] | 68 | """Remove macros from consideration during value enumeration.""" |
| 69 | # Remove some mechanisms that are declared but not implemented. |
| 70 | # The corresponding test cases would be commented out anyway |
| 71 | # thanks to the detect_not_implemented_dependencies mechanism, |
| 72 | # but for those particular key types, we don't even have enough |
| 73 | # support in the test scripts to construct test keys. So |
| 74 | # we arrange to not even attempt to generate test cases. |
Tomás González | 734d22c | 2023-10-30 15:15:45 +0000 | [diff] [blame] | 75 | constructors.key_types.discard('PSA_KEY_TYPE_DH_KEY_PAIR') |
| 76 | constructors.key_types.discard('PSA_KEY_TYPE_DH_PUBLIC_KEY') |
| 77 | constructors.key_types.discard('PSA_KEY_TYPE_DSA_KEY_PAIR') |
| 78 | constructors.key_types.discard('PSA_KEY_TYPE_DSA_PUBLIC_KEY') |
| 79 | |
| 80 | def read_psa_interface(self) -> macro_collector.PSAMacroEnumerator: |
| 81 | """Return the list of known key types, algorithms, etc.""" |
| 82 | constructors = macro_collector.InputsForTest() |
| 83 | header_file_names = ['include/psa/crypto_values.h', |
| 84 | 'include/psa/crypto_extra.h'] |
| 85 | test_suites = ['tests/suites/test_suite_psa_crypto_metadata.data'] |
| 86 | for header_file_name in header_file_names: |
| 87 | constructors.parse_header(header_file_name) |
| 88 | for test_cases in test_suites: |
| 89 | constructors.parse_test_cases(test_cases) |
| 90 | self.remove_unwanted_macros(constructors) |
| 91 | constructors.gather_arguments() |
| 92 | return constructors |
Gilles Peskine | c7b58d5 | 2024-04-10 15:55:39 +0200 | [diff] [blame] | 93 | |
| 94 | |
| 95 | class TestCase(test_case.TestCase): |
Gilles Peskine | 764c2d3 | 2024-04-10 18:12:02 +0200 | [diff] [blame] | 96 | """A PSA test case with automatically inferred dependencies. |
| 97 | |
| 98 | For mechanisms like ECC curves where the support status includes |
| 99 | the key bit-size, this class assumes that only one bit-size is |
| 100 | involved in a given test case. |
| 101 | """ |
Gilles Peskine | c7b58d5 | 2024-04-10 15:55:39 +0200 | [diff] [blame] | 102 | |
Gilles Peskine | 519762b | 2024-04-10 20:41:51 +0200 | [diff] [blame] | 103 | # Use a class variable to cache the set of implemented dependencies. |
| 104 | # Call read_implemented_dependencies() to fill the cache. |
| 105 | _implemented_dependencies = None #type: Optional[FrozenSet[str]] |
| 106 | |
| 107 | DEPENDENCY_SYMBOL_RE = re.compile(r'\bPSA_WANT_\w+\b') |
| 108 | @classmethod |
| 109 | def _yield_implemented_dependencies(cls) -> Iterator[str]: |
| 110 | for filename in ['include/psa/crypto_config.h', |
| 111 | 'include/mbedtls/config_psa.h']: |
| 112 | with open(filename) as inp: |
| 113 | content = inp.read() |
| 114 | yield from cls.DEPENDENCY_SYMBOL_RE.findall(content) |
| 115 | |
| 116 | @classmethod |
| 117 | def read_implemented_dependencies(cls) -> FrozenSet[str]: |
| 118 | if cls._implemented_dependencies is None: |
| 119 | cls._implemented_dependencies = \ |
| 120 | frozenset(cls._yield_implemented_dependencies()) |
| 121 | # Redundant return to reassure pylint (mypy is fine without it). |
| 122 | # Known issue: https://github.com/pylint-dev/pylint/issues/3045 |
| 123 | return cls._implemented_dependencies |
| 124 | return cls._implemented_dependencies |
| 125 | |
| 126 | # We skip test cases for which the dependency symbols are not defined. |
| 127 | # We assume that this means that a required mechanism is not implemented. |
| 128 | # Note that if we erroneously skip generating test cases for |
| 129 | # mechanisms that are not implemented, this should be caught |
| 130 | # by the NOT_SUPPORTED test cases generated by generate_psa_tests.py |
| 131 | # in test_suite_psa_crypto_not_supported and test_suite_psa_crypto_op_fail: |
| 132 | # those emit negative tests, which will not be skipped here. |
| 133 | def detect_not_implemented_dependencies(self) -> None: |
| 134 | """Detect dependencies that are not implemented.""" |
| 135 | all_implemented_dependencies = self.read_implemented_dependencies() |
Gilles Peskine | b8ddf6a | 2024-04-11 11:19:24 +0200 | [diff] [blame] | 136 | not_implemented = [dep |
| 137 | for dep in self.dependencies |
| 138 | if (dep.startswith('PSA_WANT') and |
| 139 | dep not in all_implemented_dependencies)] |
| 140 | if not_implemented: |
| 141 | self.skip_because('not implemented: ' + |
| 142 | ' '.join(not_implemented)) |
Gilles Peskine | 519762b | 2024-04-10 20:41:51 +0200 | [diff] [blame] | 143 | |
Gilles Peskine | c7b58d5 | 2024-04-10 15:55:39 +0200 | [diff] [blame] | 144 | def __init__(self) -> None: |
| 145 | super().__init__() |
| 146 | self.key_bits = None #type: Optional[int] |
Gilles Peskine | 1ae57ec | 2024-04-10 17:16:16 +0200 | [diff] [blame] | 147 | self.negated_dependencies = set() #type: Set[str] |
| 148 | |
| 149 | def assumes_not_supported(self, name: str) -> None: |
| 150 | """Negate the given mechanism for automatic dependency generation. |
| 151 | |
| 152 | Call this function before set_arguments() for a test case that should |
| 153 | run if the given mechanism is not supported. |
| 154 | |
Gilles Peskine | 764c2d3 | 2024-04-10 18:12:02 +0200 | [diff] [blame] | 155 | A mechanism is either a PSA_XXX symbol (e.g. PSA_KEY_TYPE_AES, |
| 156 | PSA_ALG_HMAC, etc.) or a PSA_WANT_XXX symbol. |
Gilles Peskine | 1ae57ec | 2024-04-10 17:16:16 +0200 | [diff] [blame] | 157 | """ |
Gilles Peskine | 764c2d3 | 2024-04-10 18:12:02 +0200 | [diff] [blame] | 158 | symbol = name |
| 159 | if not symbol.startswith('PSA_WANT_'): |
| 160 | symbol = psa_want_symbol(name) |
| 161 | self.negated_dependencies.add(symbol) |
Gilles Peskine | c7b58d5 | 2024-04-10 15:55:39 +0200 | [diff] [blame] | 162 | |
| 163 | def set_key_bits(self, key_bits: Optional[int]) -> None: |
| 164 | """Use the given key size for automatic dependency generation. |
| 165 | |
| 166 | Call this function before set_arguments() if relevant. |
| 167 | |
| 168 | This is only relevant for ECC and DH keys. For other key types, |
| 169 | this information is ignored. |
| 170 | """ |
| 171 | self.key_bits = key_bits |
| 172 | |
| 173 | def set_arguments(self, arguments: List[str]) -> None: |
| 174 | """Set test case arguments and automatically infer dependencies.""" |
| 175 | super().set_arguments(arguments) |
| 176 | dependencies = automatic_dependencies(*arguments) |
Gilles Peskine | 9ffffab | 2024-04-19 19:08:34 +0200 | [diff] [blame^] | 177 | # In test cases for not-supported features, the dependencies for |
| 178 | # the not-supported feature(s) must be negated. We make sure that |
| 179 | # all negated dependencies are present in the result, even in edge |
| 180 | # cases where they would not be detected automatically (for example, |
| 181 | # to restrict ECDSA-not-supported test cases to configurations |
| 182 | # where neither deterministic ECDSA nor randomized ECDSA are supported, |
| 183 | # to avoid the edge case that both ECDSA verifications are the same). |
| 184 | dependencies = ([dep for dep in dependencies |
| 185 | if dep not in self.negated_dependencies] + |
| 186 | ['!' + dep for dep in self.negated_dependencies]) |
Gilles Peskine | c7b58d5 | 2024-04-10 15:55:39 +0200 | [diff] [blame] | 187 | if self.key_bits is not None: |
| 188 | dependencies = finish_family_dependencies(dependencies, self.key_bits) |
Gilles Peskine | 519762b | 2024-04-10 20:41:51 +0200 | [diff] [blame] | 189 | self.dependencies += sorted(dependencies) |
| 190 | self.detect_not_implemented_dependencies() |