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