blob: c3a3d487e7f5895ddf105d48e221fd8e7a917695 [file] [log] [blame]
Tomás González734d22c2023-10-30 15:15:45 +00001"""Collect information about PSA cryptographic mechanisms.
2"""
3
4# Copyright The Mbed TLS Contributors
Tomás González5fae5602023-11-13 11:45:12 +00005# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6
Tomás González734d22c2023-10-30 15:15:45 +00007
8import re
9from typing import Dict, FrozenSet, List, Optional
10
11from . import macro_collector
12
13
14def 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
21def 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
29def 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
35SYMBOLS_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
44def 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.
61def 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
67def 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')
Gilles Peskinec6fe12a2024-04-10 16:36:13 +020072 for dep in dependencies:
73 dep = dep.lstrip('!')
74 if dep.startswith('PSA_WANT') and dep not in _implemented_dependencies:
75 dependencies.append('DEPENDENCY_NOT_IMPLEMENTED_YET_' + dep)
Gilles Peskinec3b261a2024-04-10 17:19:04 +020076 dependencies.sort()
Tomás González734d22c2023-10-30 15:15:45 +000077
78class Information:
79 """Gather information about PSA constructors."""
80
81 def __init__(self) -> None:
82 self.constructors = self.read_psa_interface()
83
84 @staticmethod
85 def remove_unwanted_macros(
86 constructors: macro_collector.PSAMacroEnumerator
87 ) -> None:
88 # Mbed TLS doesn't support finite-field DH yet and will not support
89 # finite-field DSA. Don't attempt to generate any related test case.
90 constructors.key_types.discard('PSA_KEY_TYPE_DH_KEY_PAIR')
91 constructors.key_types.discard('PSA_KEY_TYPE_DH_PUBLIC_KEY')
92 constructors.key_types.discard('PSA_KEY_TYPE_DSA_KEY_PAIR')
93 constructors.key_types.discard('PSA_KEY_TYPE_DSA_PUBLIC_KEY')
94
95 def read_psa_interface(self) -> macro_collector.PSAMacroEnumerator:
96 """Return the list of known key types, algorithms, etc."""
97 constructors = macro_collector.InputsForTest()
98 header_file_names = ['include/psa/crypto_values.h',
99 'include/psa/crypto_extra.h']
100 test_suites = ['tests/suites/test_suite_psa_crypto_metadata.data']
101 for header_file_name in header_file_names:
102 constructors.parse_header(header_file_name)
103 for test_cases in test_suites:
104 constructors.parse_test_cases(test_cases)
105 self.remove_unwanted_macros(constructors)
106 constructors.gather_arguments()
107 return constructors