Gilles Peskine | 0994049 | 2021-01-26 22:16:30 +0100 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
| 2 | """Generate test data for PSA cryptographic mechanisms. |
| 3 | """ |
| 4 | |
| 5 | # Copyright The Mbed TLS Contributors |
| 6 | # SPDX-License-Identifier: Apache-2.0 |
| 7 | # |
| 8 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | # not use this file except in compliance with the License. |
| 10 | # You may obtain a copy of the License at |
| 11 | # |
| 12 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | # |
| 14 | # Unless required by applicable law or agreed to in writing, software |
| 15 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | # See the License for the specific language governing permissions and |
| 18 | # limitations under the License. |
| 19 | |
| 20 | import argparse |
| 21 | import sys |
| 22 | from typing import TypeVar |
| 23 | |
| 24 | import scripts_path # pylint: disable=unused-import |
| 25 | from mbedtls_dev import macro_collector |
| 26 | |
| 27 | T = TypeVar('T') #pylint: disable=invalid-name |
| 28 | |
| 29 | class TestGenerator: |
| 30 | """Gather information and generate test data.""" |
| 31 | |
| 32 | def __init__(self, options): |
| 33 | self.test_suite_directory = self.get_option(options, 'directory', |
| 34 | 'tests/suites') |
| 35 | self.constructors = self.read_psa_interface() |
| 36 | |
| 37 | @staticmethod |
| 38 | def get_option(options, name: str, default: T) -> T: |
| 39 | value = getattr(options, name, None) |
| 40 | return default if value is None else value |
| 41 | |
| 42 | @staticmethod |
| 43 | def remove_unwanted_macros( |
| 44 | constructors: macro_collector.PSAMacroCollector |
| 45 | ) -> None: |
| 46 | # Mbed TLS doesn't support DSA. Don't attempt to generate any related |
| 47 | # test case. |
| 48 | constructors.key_types.discard('PSA_KEY_TYPE_DSA_KEY_PAIR') |
| 49 | constructors.key_types.discard('PSA_KEY_TYPE_DSA_PUBLIC_KEY') |
| 50 | constructors.algorithms_from_hash.pop('PSA_ALG_DSA', None) |
| 51 | constructors.algorithms_from_hash.pop('PSA_ALG_DETERMINISTIC_DSA', None) |
| 52 | |
| 53 | def read_psa_interface(self) -> macro_collector.PSAMacroCollector: |
| 54 | """Return the list of known key types, algorithms, etc.""" |
| 55 | constructors = macro_collector.PSAMacroCollector() |
| 56 | header_file_names = ['include/psa/crypto_values.h', |
| 57 | 'include/psa/crypto_extra.h'] |
| 58 | for header_file_name in header_file_names: |
| 59 | with open(header_file_name, 'rb') as header_file: |
| 60 | constructors.read_file(header_file) |
| 61 | self.remove_unwanted_macros(constructors) |
| 62 | return constructors |
| 63 | |
| 64 | def main(args): |
| 65 | """Command line entry point.""" |
| 66 | parser = argparse.ArgumentParser(description=__doc__) |
| 67 | options = parser.parse_args(args) |
| 68 | generator = TestGenerator(options) |
| 69 | |
| 70 | if __name__ == '__main__': |
| 71 | main(sys.argv[1:]) |