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