blob: 2287ae13ee4685f560df0597dc2fd6d5cf4330e7 [file] [log] [blame]
Gilles Peskinefdb72232023-06-19 20:46:47 +02001"""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
Yanray Wang6b190d42023-11-01 13:44:14 +080020from collections import OrderedDict
21from typing import FrozenSet, List, Optional
Gilles Peskinefdb72232023-06-19 20:46:47 +020022
23from . import macro_collector
24
25
26class Information:
27 """Gather information about PSA constructors."""
28
29 def __init__(self) -> None:
30 self.constructors = self.read_psa_interface()
31
32 @staticmethod
33 def remove_unwanted_macros(
34 constructors: macro_collector.PSAMacroEnumerator
35 ) -> None:
36 # Mbed TLS does not support finite-field DSA.
37 # Don't attempt to generate any related test case.
38 constructors.key_types.discard('PSA_KEY_TYPE_DSA_KEY_PAIR')
39 constructors.key_types.discard('PSA_KEY_TYPE_DSA_PUBLIC_KEY')
40
41 def read_psa_interface(self) -> macro_collector.PSAMacroEnumerator:
42 """Return the list of known key types, algorithms, etc."""
43 constructors = macro_collector.InputsForTest()
44 header_file_names = ['include/psa/crypto_values.h',
45 'include/psa/crypto_extra.h']
46 test_suites = ['tests/suites/test_suite_psa_crypto_metadata.data']
47 for header_file_name in header_file_names:
48 constructors.parse_header(header_file_name)
49 for test_cases in test_suites:
50 constructors.parse_test_cases(test_cases)
51 self.remove_unwanted_macros(constructors)
52 constructors.gather_arguments()
53 return constructors
54
55
56def psa_want_symbol(name: str) -> str:
57 """Return the PSA_WANT_xxx symbol associated with a PSA crypto feature."""
58 if name.startswith('PSA_'):
59 return name[:4] + 'WANT_' + name[4:]
60 else:
61 raise ValueError('Unable to determine the PSA_WANT_ symbol for ' + name)
62
63def finish_family_dependency(dep: str, bits: int) -> str:
64 """Finish dep if it's a family dependency symbol prefix.
65
66 A family dependency symbol prefix is a PSA_WANT_ symbol that needs to be
67 qualified by the key size. If dep is such a symbol, finish it by adjusting
68 the prefix and appending the key size. Other symbols are left unchanged.
69 """
70 return re.sub(r'_FAMILY_(.*)', r'_\1_' + str(bits), dep)
71
72def finish_family_dependencies(dependencies: List[str], bits: int) -> List[str]:
73 """Finish any family dependency symbol prefixes.
74
75 Apply `finish_family_dependency` to each element of `dependencies`.
76 """
77 return [finish_family_dependency(dep, bits) for dep in dependencies]
78
79SYMBOLS_WITHOUT_DEPENDENCY = frozenset([
80 'PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG', # modifier, only in policies
81 'PSA_ALG_AEAD_WITH_SHORTENED_TAG', # modifier
82 'PSA_ALG_ANY_HASH', # only in policies
83 'PSA_ALG_AT_LEAST_THIS_LENGTH_MAC', # modifier, only in policies
84 'PSA_ALG_KEY_AGREEMENT', # chaining
85 'PSA_ALG_TRUNCATED_MAC', # modifier
86])
87def automatic_dependencies(*expressions: str) -> List[str]:
88 """Infer dependencies of a test case by looking for PSA_xxx symbols.
89
90 The arguments are strings which should be C expressions. Do not use
91 string literals or comments as this function is not smart enough to
92 skip them.
93 """
94 used = set()
95 for expr in expressions:
96 used.update(re.findall(r'PSA_(?:ALG|ECC_FAMILY|KEY_TYPE)_\w+', expr))
97 used.difference_update(SYMBOLS_WITHOUT_DEPENDENCY)
98 return sorted(psa_want_symbol(name) for name in used)
99
100# Define set of regular expressions and dependencies to optionally append
Yanray Wang6b190d42023-11-01 13:44:14 +0800101# extra dependencies for test case based on key description.
102
103# Skip AES test cases which require 192- or 256-bit key
104# if MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH defined
Yanray Wang70743b02023-11-09 16:13:53 +0800105AES_128BIT_ONLY_DEP_REGEX = re.compile(r'AES\s(192|256)')
Yanray Wang6b190d42023-11-01 13:44:14 +0800106AES_128BIT_ONLY_DEP = ['!MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH']
107# Skip AES/ARIA/CAMELLIA test cases which require decrypt operation in ECB mode
108# if MBEDTLS_BLOCK_CIPHER_NO_DECRYPT enabled.
Yanray Wang70743b02023-11-09 16:13:53 +0800109ECB_NO_PADDING_DEP_REGEX = re.compile(r'(AES|ARIA|CAMELLIA).*ECB_NO_PADDING')
Yanray Wang6b190d42023-11-01 13:44:14 +0800110ECB_NO_PADDING_DEP = ['!MBEDTLS_BLOCK_CIPHER_NO_DECRYPT']
Gilles Peskinefdb72232023-06-19 20:46:47 +0200111
Yanray Wang6b190d42023-11-01 13:44:14 +0800112DEPENDENCY_FROM_DESCRIPTION = OrderedDict()
113DEPENDENCY_FROM_DESCRIPTION[AES_128BIT_ONLY_DEP_REGEX] = AES_128BIT_ONLY_DEP
114DEPENDENCY_FROM_DESCRIPTION[ECB_NO_PADDING_DEP_REGEX] = ECB_NO_PADDING_DEP
115def generate_description_dependencies(
116 dep_list: List[str],
117 description: str
118 ) -> List[str]:
119 """Return additional dependencies based on test case description and REGEX.
Gilles Peskinefdb72232023-06-19 20:46:47 +0200120 """
Yanray Wang6b190d42023-11-01 13:44:14 +0800121 for regex, deps in DEPENDENCY_FROM_DESCRIPTION.items():
Gilles Peskinefdb72232023-06-19 20:46:47 +0200122 if re.search(regex, description):
Yanray Wang6b190d42023-11-01 13:44:14 +0800123 dep_list += deps
Gilles Peskinefdb72232023-06-19 20:46:47 +0200124
Yanray Wang6b190d42023-11-01 13:44:14 +0800125 return dep_list
Gilles Peskinefdb72232023-06-19 20:46:47 +0200126
127# A temporary hack: at the time of writing, not all dependency symbols
128# are implemented yet. Skip test cases for which the dependency symbols are
129# not available. Once all dependency symbols are available, this hack must
130# be removed so that a bug in the dependency symbols properly leads to a test
131# failure.
132def read_implemented_dependencies(filename: str) -> FrozenSet[str]:
133 return frozenset(symbol
134 for line in open(filename)
135 for symbol in re.findall(r'\bPSA_WANT_\w+\b', line))
136_implemented_dependencies = None #type: Optional[FrozenSet[str]] #pylint: disable=invalid-name
137def hack_dependencies_not_implemented(dependencies: List[str]) -> None:
138 global _implemented_dependencies #pylint: disable=global-statement,invalid-name
139 if _implemented_dependencies is None:
140 _implemented_dependencies = \
141 read_implemented_dependencies('include/psa/crypto_config.h')
142 if not all((dep.lstrip('!') in _implemented_dependencies or
143 not dep.lstrip('!').startswith('PSA_WANT'))
144 for dep in dependencies):
145 dependencies.append('DEPENDENCY_NOT_IMPLEMENTED_YET')
146
147def tweak_key_pair_dependency(dep: str, usage: str):
148 """
149 This helper function add the proper suffix to PSA_WANT_KEY_TYPE_xxx_KEY_PAIR
150 symbols according to the required usage.
151 """
152 ret_list = list()
153 if dep.endswith('KEY_PAIR'):
154 if usage == "BASIC":
155 # BASIC automatically includes IMPORT and EXPORT for test purposes (see
156 # config_psa.h).
157 ret_list.append(re.sub(r'KEY_PAIR', r'KEY_PAIR_BASIC', dep))
158 ret_list.append(re.sub(r'KEY_PAIR', r'KEY_PAIR_IMPORT', dep))
159 ret_list.append(re.sub(r'KEY_PAIR', r'KEY_PAIR_EXPORT', dep))
160 elif usage == "GENERATE":
161 ret_list.append(re.sub(r'KEY_PAIR', r'KEY_PAIR_GENERATE', dep))
162 else:
163 # No replacement to do in this case
164 ret_list.append(dep)
165 return ret_list
166
167def fix_key_pair_dependencies(dep_list: List[str], usage: str):
168 new_list = [new_deps
169 for dep in dep_list
170 for new_deps in tweak_key_pair_dependency(dep, usage)]
171
172 return new_list