Archana | 1f1a34a | 2021-11-17 08:44:07 +0530 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 2 | """Generate library/psa_crypto_driver_wrappers.c |
| 3 | |
| 4 | This module is invoked by the build sripts to auto generate the |
| 5 | psa_crypto_driver_wrappers.c based on template files in |
| 6 | script/data_files/driver_templates/. |
| 7 | """ |
| 8 | # Copyright The Mbed TLS Contributors |
| 9 | # SPDX-License-Identifier: Apache-2.0 |
| 10 | # |
| 11 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 12 | # not use this file except in compliance with the License. |
| 13 | # You may obtain a copy of the License at |
| 14 | # |
| 15 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 16 | # |
| 17 | # Unless required by applicable law or agreed to in writing, software |
| 18 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 19 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 | # See the License for the specific language governing permissions and |
| 21 | # limitations under the License. |
Archana | 1f1a34a | 2021-11-17 08:44:07 +0530 | [diff] [blame] | 22 | |
| 23 | import sys |
Archana | 1f1a34a | 2021-11-17 08:44:07 +0530 | [diff] [blame] | 24 | import os |
Archana | e829cd6 | 2021-12-24 12:50:36 +0530 | [diff] [blame] | 25 | import json |
Archana | fdbbcba | 2022-02-27 05:38:55 +0530 | [diff] [blame] | 26 | from typing import Tuple, NewType |
Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 27 | import argparse |
Archana | 3143805 | 2022-01-09 15:01:20 +0530 | [diff] [blame] | 28 | import jsonschema |
Archana | 1f1a34a | 2021-11-17 08:44:07 +0530 | [diff] [blame] | 29 | import jinja2 |
Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 30 | from mbedtls_dev import build_tree |
Archana | 1f1a34a | 2021-11-17 08:44:07 +0530 | [diff] [blame] | 31 | |
Archana | fdbbcba | 2022-02-27 05:38:55 +0530 | [diff] [blame] | 32 | JSONSchema = NewType('JSONSchema', object) |
Archana | a78dc70 | 2022-03-13 17:57:45 +0530 | [diff] [blame] | 33 | # The Driver is an Object, but practically it's indexable and can called a dictionary to |
| 34 | # keep MyPy happy till MyPy comes with a more composite type for JsonObjects. |
| 35 | Driver = NewType('Driver', dict) |
Archana | fdbbcba | 2022-02-27 05:38:55 +0530 | [diff] [blame] | 36 | |
Archana | e829cd6 | 2021-12-24 12:50:36 +0530 | [diff] [blame] | 37 | def render(template_path: str, driver_jsoncontext: list) -> str: |
Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 38 | """ |
Archana | e829cd6 | 2021-12-24 12:50:36 +0530 | [diff] [blame] | 39 | Render template from the input file and driver JSON. |
Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 40 | """ |
Archana | 6f21e45 | 2021-11-23 14:46:51 +0530 | [diff] [blame] | 41 | environment = jinja2.Environment( |
| 42 | loader=jinja2.FileSystemLoader(os.path.dirname(template_path)), |
| 43 | keep_trailing_newline=True) |
| 44 | template = environment.get_template(os.path.basename(template_path)) |
Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 45 | |
Archana | 3143805 | 2022-01-09 15:01:20 +0530 | [diff] [blame] | 46 | return template.render(drivers=driver_jsoncontext) |
Archana | 1f1a34a | 2021-11-17 08:44:07 +0530 | [diff] [blame] | 47 | |
Archana | e829cd6 | 2021-12-24 12:50:36 +0530 | [diff] [blame] | 48 | |
Archana | 3143805 | 2022-01-09 15:01:20 +0530 | [diff] [blame] | 49 | def generate_driver_wrapper_file(template_dir: str, \ |
| 50 | output_dir: str, driver_jsoncontext: list) -> None: |
Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 51 | """ |
| 52 | Generate the file psa_crypto_driver_wrapper.c. |
| 53 | """ |
| 54 | driver_wrapper_template_filename = \ |
Archana | e829cd6 | 2021-12-24 12:50:36 +0530 | [diff] [blame] | 55 | os.path.join(template_dir, "psa_crypto_driver_wrappers.c.jinja") |
Archana | 1f1a34a | 2021-11-17 08:44:07 +0530 | [diff] [blame] | 56 | |
Archana | e829cd6 | 2021-12-24 12:50:36 +0530 | [diff] [blame] | 57 | result = render(driver_wrapper_template_filename, driver_jsoncontext) |
Archana | 1f1a34a | 2021-11-17 08:44:07 +0530 | [diff] [blame] | 58 | |
Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 59 | with open(os.path.join(output_dir, "psa_crypto_driver_wrappers.c"), 'w') as out_file: |
| 60 | out_file.write(result) |
Archana | 6f21e45 | 2021-11-23 14:46:51 +0530 | [diff] [blame] | 61 | |
Archana | e829cd6 | 2021-12-24 12:50:36 +0530 | [diff] [blame] | 62 | |
Archana | fdbbcba | 2022-02-27 05:38:55 +0530 | [diff] [blame] | 63 | def validate_json(driverjson_data: Driver, driverschema_list: dict) -> bool: |
Archana | e829cd6 | 2021-12-24 12:50:36 +0530 | [diff] [blame] | 64 | """ |
Archana | fdbbcba | 2022-02-27 05:38:55 +0530 | [diff] [blame] | 65 | Validate the Driver JSON against an appropriate schema |
| 66 | the schema passed could be that matching an opaque/ transparent driver. |
Archana | 04cfe34 | 2022-01-09 13:28:28 +0530 | [diff] [blame] | 67 | """ |
Archana | fdbbcba | 2022-02-27 05:38:55 +0530 | [diff] [blame] | 68 | driver_type = driverjson_data["type"] |
| 69 | driver_prefix = driverjson_data["prefix"] |
Archana | 04cfe34 | 2022-01-09 13:28:28 +0530 | [diff] [blame] | 70 | try: |
Archana | fdbbcba | 2022-02-27 05:38:55 +0530 | [diff] [blame] | 71 | _schema = driverschema_list[driver_type] |
| 72 | jsonschema.validate(instance=driverjson_data, schema=_schema) |
| 73 | |
| 74 | except KeyError as err: |
| 75 | # This could happen if the driverjson_data.type does not exist in the passed in schema list |
| 76 | # schemas = {'transparent': transparent_driver_schema, 'opaque': opaque_driver_schema} |
| 77 | # Print onto stdout and stderr. |
| 78 | print("Unknown Driver type " + driver_type + |
| 79 | " for driver " + driver_prefix, str(err)) |
| 80 | print("Unknown Driver type " + driver_type + |
| 81 | " for driver " + driver_prefix, str(err), file=sys.stderr) |
| 82 | return False |
| 83 | |
Archana | 04cfe34 | 2022-01-09 13:28:28 +0530 | [diff] [blame] | 84 | except jsonschema.exceptions.ValidationError as err: |
Archana | fdbbcba | 2022-02-27 05:38:55 +0530 | [diff] [blame] | 85 | # Print onto stdout and stderr. |
| 86 | print("Error: Failed to validate data file: {} using schema: {}." |
| 87 | "\n Exception Message: \"{}\"" |
| 88 | " ".format(driverjson_data, _schema, str(err))) |
| 89 | print("Error: Failed to validate data file: {} using schema: {}." |
| 90 | "\n Exception Message: \"{}\"" |
| 91 | " ".format(driverjson_data, _schema, str(err)), file=sys.stderr) |
Archana | 04cfe34 | 2022-01-09 13:28:28 +0530 | [diff] [blame] | 92 | return False |
| 93 | |
Archana | 04cfe34 | 2022-01-09 13:28:28 +0530 | [diff] [blame] | 94 | return True |
| 95 | |
Archana | fdbbcba | 2022-02-27 05:38:55 +0530 | [diff] [blame] | 96 | def read_driver_descriptions(mbedtls_root: str, json_directory: str, \ |
Archana | 3143805 | 2022-01-09 15:01:20 +0530 | [diff] [blame] | 97 | jsondriver_list: str) -> Tuple[bool, list]: |
Archana | 04cfe34 | 2022-01-09 13:28:28 +0530 | [diff] [blame] | 98 | """ |
| 99 | Merge driver JSON files into a single ordered JSON after validation. |
Archana | e829cd6 | 2021-12-24 12:50:36 +0530 | [diff] [blame] | 100 | """ |
Archana | fdbbcba | 2022-02-27 05:38:55 +0530 | [diff] [blame] | 101 | result = [] |
| 102 | with open(os.path.join(mbedtls_root, |
| 103 | 'scripts', |
| 104 | 'data_files', |
| 105 | 'driver_jsons', |
| 106 | 'driver_transparent_schema.json'), 'r') as file: |
Archana | 04cfe34 | 2022-01-09 13:28:28 +0530 | [diff] [blame] | 107 | transparent_driver_schema = json.load(file) |
Archana | fdbbcba | 2022-02-27 05:38:55 +0530 | [diff] [blame] | 108 | with open(os.path.join(mbedtls_root, |
| 109 | 'scripts', |
| 110 | 'data_files', |
| 111 | 'driver_jsons', |
| 112 | 'driver_opaque_schema.json'), 'r') as file: |
Archana | 04cfe34 | 2022-01-09 13:28:28 +0530 | [diff] [blame] | 113 | opaque_driver_schema = json.load(file) |
| 114 | |
Archana | fdbbcba | 2022-02-27 05:38:55 +0530 | [diff] [blame] | 115 | driver_schema_list = {'transparent':transparent_driver_schema, |
| 116 | 'opaque':opaque_driver_schema} |
| 117 | |
Archana | 3143805 | 2022-01-09 15:01:20 +0530 | [diff] [blame] | 118 | with open(os.path.join(json_directory, jsondriver_list), 'r') as driverlistfile: |
Archana | e829cd6 | 2021-12-24 12:50:36 +0530 | [diff] [blame] | 119 | driverlist = json.load(driverlistfile) |
| 120 | for file_name in driverlist: |
| 121 | with open(os.path.join(json_directory, file_name), 'r') as infile: |
Archana | 04cfe34 | 2022-01-09 13:28:28 +0530 | [diff] [blame] | 122 | json_data = json.load(infile) |
Archana | fdbbcba | 2022-02-27 05:38:55 +0530 | [diff] [blame] | 123 | ret = validate_json(json_data, driver_schema_list) |
Archana | 3143805 | 2022-01-09 15:01:20 +0530 | [diff] [blame] | 124 | if ret is False: |
Archana | 04cfe34 | 2022-01-09 13:28:28 +0530 | [diff] [blame] | 125 | return ret, [] |
| 126 | result.append(json_data) |
| 127 | return True, result |
Archana | e829cd6 | 2021-12-24 12:50:36 +0530 | [diff] [blame] | 128 | |
| 129 | |
Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 130 | def main() -> int: |
| 131 | """ |
| 132 | Main with command line arguments. |
Archana | fdbbcba | 2022-02-27 05:38:55 +0530 | [diff] [blame] | 133 | returns 1 when read_driver_descriptions returns False |
Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 134 | """ |
Archana | 4a9e026 | 2021-12-19 13:34:30 +0530 | [diff] [blame] | 135 | def_arg_mbedtls_root = build_tree.guess_mbedtls_root() |
Archana | 4a9e026 | 2021-12-19 13:34:30 +0530 | [diff] [blame] | 136 | |
Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 137 | parser = argparse.ArgumentParser() |
Archana | 22c7827 | 2022-04-11 10:12:08 +0530 | [diff] [blame] | 138 | parser.add_argument('--mbedtls-root', default=def_arg_mbedtls_root, |
Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 139 | help='root directory of mbedtls source code') |
Archana | 22c7827 | 2022-04-11 10:12:08 +0530 | [diff] [blame] | 140 | parser.add_argument('--template-dir', |
| 141 | help='directory holding the driver templates') |
| 142 | parser.add_argument('--json-dir', |
| 143 | help='directory holding the driver JSONs') |
Archana | 01aa39e | 2022-03-14 15:29:00 +0530 | [diff] [blame] | 144 | parser.add_argument('output_directory', nargs='?', |
| 145 | help='output file\'s location') |
Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 146 | args = parser.parse_args() |
Archana | 4a9e026 | 2021-12-19 13:34:30 +0530 | [diff] [blame] | 147 | |
Archana | 3143805 | 2022-01-09 15:01:20 +0530 | [diff] [blame] | 148 | mbedtls_root = os.path.abspath(args.mbedtls_root) |
Archana | fdbbcba | 2022-02-27 05:38:55 +0530 | [diff] [blame] | 149 | if args.template_dir is None: |
Archana | 01aa39e | 2022-03-14 15:29:00 +0530 | [diff] [blame] | 150 | args.template_dir = os.path.join(mbedtls_root, |
| 151 | 'scripts', |
| 152 | 'data_files', |
| 153 | 'driver_templates') |
Archana | fdbbcba | 2022-02-27 05:38:55 +0530 | [diff] [blame] | 154 | if args.json_dir is None: |
Archana | 01aa39e | 2022-03-14 15:29:00 +0530 | [diff] [blame] | 155 | args.json_dir = os.path.join(mbedtls_root, |
| 156 | 'scripts', |
| 157 | 'data_files', |
| 158 | 'driver_jsons') |
| 159 | if args.output_directory is None: |
| 160 | args.output_directory = os.path.join(mbedtls_root, 'library') |
| 161 | |
| 162 | output_directory = args.output_directory |
| 163 | template_directory = args.template_dir |
Archana | 3143805 | 2022-01-09 15:01:20 +0530 | [diff] [blame] | 164 | json_directory = args.json_dir |
Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 165 | |
Archana | fdbbcba | 2022-02-27 05:38:55 +0530 | [diff] [blame] | 166 | # Read and validate list of driver jsons from driverlist.json |
| 167 | ret, merged_driver_json = read_driver_descriptions(mbedtls_root, json_directory, |
| 168 | 'driverlist.json') |
Archana | 3143805 | 2022-01-09 15:01:20 +0530 | [diff] [blame] | 169 | if ret is False: |
Archana | e829cd6 | 2021-12-24 12:50:36 +0530 | [diff] [blame] | 170 | return 1 |
Archana | fdbbcba | 2022-02-27 05:38:55 +0530 | [diff] [blame] | 171 | generate_driver_wrapper_file(template_directory, output_directory, merged_driver_json) |
Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 172 | |
| 173 | return 0 |
| 174 | |
| 175 | if __name__ == '__main__': |
| 176 | sys.exit(main()) |