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