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