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 |
| 12 | |
| 13 | |
| 14 | def psa_want_symbol(name: str) -> str: |
| 15 | """Return the PSA_WANT_xxx symbol associated with a PSA crypto feature.""" |
| 16 | if name.startswith('PSA_'): |
| 17 | return name[:4] + 'WANT_' + name[4:] |
| 18 | else: |
| 19 | raise ValueError('Unable to determine the PSA_WANT_ symbol for ' + name) |
| 20 | |
| 21 | def finish_family_dependency(dep: str, bits: int) -> str: |
| 22 | """Finish dep if it's a family dependency symbol prefix. |
| 23 | A family dependency symbol prefix is a PSA_WANT_ symbol that needs to be |
| 24 | qualified by the key size. If dep is such a symbol, finish it by adjusting |
| 25 | the prefix and appending the key size. Other symbols are left unchanged. |
| 26 | """ |
| 27 | return re.sub(r'_FAMILY_(.*)', r'_\1_' + str(bits), dep) |
| 28 | |
| 29 | def finish_family_dependencies(dependencies: List[str], bits: int) -> List[str]: |
| 30 | """Finish any family dependency symbol prefixes. |
| 31 | Apply `finish_family_dependency` to each element of `dependencies`. |
| 32 | """ |
| 33 | return [finish_family_dependency(dep, bits) for dep in dependencies] |
| 34 | |
| 35 | SYMBOLS_WITHOUT_DEPENDENCY = frozenset([ |
| 36 | 'PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG', # modifier, only in policies |
| 37 | 'PSA_ALG_AEAD_WITH_SHORTENED_TAG', # modifier |
| 38 | 'PSA_ALG_ANY_HASH', # only in policies |
| 39 | 'PSA_ALG_AT_LEAST_THIS_LENGTH_MAC', # modifier, only in policies |
| 40 | 'PSA_ALG_KEY_AGREEMENT', # chaining |
| 41 | 'PSA_ALG_TRUNCATED_MAC', # modifier |
| 42 | ]) |
| 43 | |
| 44 | def automatic_dependencies(*expressions: str) -> List[str]: |
| 45 | """Infer dependencies of a test case by looking for PSA_xxx symbols. |
| 46 | The arguments are strings which should be C expressions. Do not use |
| 47 | string literals or comments as this function is not smart enough to |
| 48 | skip them. |
| 49 | """ |
| 50 | used = set() |
| 51 | for expr in expressions: |
| 52 | used.update(re.findall(r'PSA_(?:ALG|ECC_FAMILY|KEY_TYPE)_\w+', expr)) |
| 53 | used.difference_update(SYMBOLS_WITHOUT_DEPENDENCY) |
| 54 | return sorted(psa_want_symbol(name) for name in used) |
| 55 | |
| 56 | # A temporary hack: at the time of writing, not all dependency symbols |
| 57 | # are implemented yet. Skip test cases for which the dependency symbols are |
| 58 | # not available. Once all dependency symbols are available, this hack must |
| 59 | # be removed so that a bug in the dependency symbols properly leads to a test |
| 60 | # failure. |
| 61 | def read_implemented_dependencies(filename: str) -> FrozenSet[str]: |
| 62 | return frozenset(symbol |
| 63 | for line in open(filename) |
| 64 | for symbol in re.findall(r'\bPSA_WANT_\w+\b', line)) |
| 65 | _implemented_dependencies = None #type: Optional[FrozenSet[str]] #pylint: disable=invalid-name |
| 66 | |
| 67 | def hack_dependencies_not_implemented(dependencies: List[str]) -> None: |
| 68 | global _implemented_dependencies #pylint: disable=global-statement,invalid-name |
| 69 | if _implemented_dependencies is None: |
| 70 | _implemented_dependencies = \ |
| 71 | read_implemented_dependencies('include/psa/crypto_config.h') |
| 72 | if not all((dep.lstrip('!') in _implemented_dependencies or |
| 73 | not dep.lstrip('!').startswith('PSA_WANT')) |
| 74 | for dep in dependencies): |
| 75 | dependencies.append('DEPENDENCY_NOT_IMPLEMENTED_YET') |
| 76 | |
| 77 | class Information: |
| 78 | """Gather information about PSA constructors.""" |
| 79 | |
| 80 | def __init__(self) -> None: |
| 81 | self.constructors = self.read_psa_interface() |
| 82 | |
| 83 | @staticmethod |
| 84 | def remove_unwanted_macros( |
| 85 | constructors: macro_collector.PSAMacroEnumerator |
| 86 | ) -> None: |
| 87 | # Mbed TLS doesn't support finite-field DH yet and will not support |
| 88 | # finite-field DSA. Don't attempt to generate any related test case. |
| 89 | constructors.key_types.discard('PSA_KEY_TYPE_DH_KEY_PAIR') |
| 90 | constructors.key_types.discard('PSA_KEY_TYPE_DH_PUBLIC_KEY') |
| 91 | constructors.key_types.discard('PSA_KEY_TYPE_DSA_KEY_PAIR') |
| 92 | constructors.key_types.discard('PSA_KEY_TYPE_DSA_PUBLIC_KEY') |
| 93 | |
| 94 | def read_psa_interface(self) -> macro_collector.PSAMacroEnumerator: |
| 95 | """Return the list of known key types, algorithms, etc.""" |
| 96 | constructors = macro_collector.InputsForTest() |
| 97 | header_file_names = ['include/psa/crypto_values.h', |
| 98 | 'include/psa/crypto_extra.h'] |
| 99 | test_suites = ['tests/suites/test_suite_psa_crypto_metadata.data'] |
| 100 | for header_file_name in header_file_names: |
| 101 | constructors.parse_header(header_file_name) |
| 102 | for test_cases in test_suites: |
| 103 | constructors.parse_test_cases(test_cases) |
| 104 | self.remove_unwanted_macros(constructors) |
| 105 | constructors.gather_arguments() |
| 106 | return constructors |