Gilles Peskine | fdb7223 | 2023-06-19 20:46:47 +0200 | [diff] [blame] | 1 | """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 | |
| 19 | import re |
Yanray Wang | 6b190d4 | 2023-11-01 13:44:14 +0800 | [diff] [blame] | 20 | from collections import OrderedDict |
| 21 | from typing import FrozenSet, List, Optional |
Gilles Peskine | fdb7223 | 2023-06-19 20:46:47 +0200 | [diff] [blame] | 22 | |
| 23 | from . import macro_collector |
| 24 | |
| 25 | |
| 26 | class 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 | |
| 56 | def 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 | |
| 63 | def 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 | |
| 72 | def 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 | |
| 79 | SYMBOLS_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 | ]) |
| 87 | def 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 Wang | 6b190d4 | 2023-11-01 13:44:14 +0800 | [diff] [blame] | 101 | # 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 Wang | 70743b0 | 2023-11-09 16:13:53 +0800 | [diff] [blame^] | 105 | AES_128BIT_ONLY_DEP_REGEX = re.compile(r'AES\s(192|256)') |
Yanray Wang | 6b190d4 | 2023-11-01 13:44:14 +0800 | [diff] [blame] | 106 | AES_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 Wang | 70743b0 | 2023-11-09 16:13:53 +0800 | [diff] [blame^] | 109 | ECB_NO_PADDING_DEP_REGEX = re.compile(r'(AES|ARIA|CAMELLIA).*ECB_NO_PADDING') |
Yanray Wang | 6b190d4 | 2023-11-01 13:44:14 +0800 | [diff] [blame] | 110 | ECB_NO_PADDING_DEP = ['!MBEDTLS_BLOCK_CIPHER_NO_DECRYPT'] |
Gilles Peskine | fdb7223 | 2023-06-19 20:46:47 +0200 | [diff] [blame] | 111 | |
Yanray Wang | 6b190d4 | 2023-11-01 13:44:14 +0800 | [diff] [blame] | 112 | DEPENDENCY_FROM_DESCRIPTION = OrderedDict() |
| 113 | DEPENDENCY_FROM_DESCRIPTION[AES_128BIT_ONLY_DEP_REGEX] = AES_128BIT_ONLY_DEP |
| 114 | DEPENDENCY_FROM_DESCRIPTION[ECB_NO_PADDING_DEP_REGEX] = ECB_NO_PADDING_DEP |
| 115 | def 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 Peskine | fdb7223 | 2023-06-19 20:46:47 +0200 | [diff] [blame] | 120 | """ |
Yanray Wang | 6b190d4 | 2023-11-01 13:44:14 +0800 | [diff] [blame] | 121 | for regex, deps in DEPENDENCY_FROM_DESCRIPTION.items(): |
Gilles Peskine | fdb7223 | 2023-06-19 20:46:47 +0200 | [diff] [blame] | 122 | if re.search(regex, description): |
Yanray Wang | 6b190d4 | 2023-11-01 13:44:14 +0800 | [diff] [blame] | 123 | dep_list += deps |
Gilles Peskine | fdb7223 | 2023-06-19 20:46:47 +0200 | [diff] [blame] | 124 | |
Yanray Wang | 6b190d4 | 2023-11-01 13:44:14 +0800 | [diff] [blame] | 125 | return dep_list |
Gilles Peskine | fdb7223 | 2023-06-19 20:46:47 +0200 | [diff] [blame] | 126 | |
| 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. |
| 132 | def 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 |
| 137 | def 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 | |
| 147 | def 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 | |
| 167 | def 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 |