2021-11-17 04:14:07 +01:00
|
|
|
#!/usr/bin/env python3
|
2023-09-11 04:39:27 +02:00
|
|
|
"""Generate library/psa_crypto_driver_wrappers.h
|
|
|
|
library/psa_crypto_driver_wrappers_no_static.c
|
2021-12-19 04:47:04 +01:00
|
|
|
|
2022-04-13 20:28:52 +02:00
|
|
|
This module is invoked by the build scripts to auto generate the
|
2023-09-11 04:39:27 +02:00
|
|
|
psa_crypto_driver_wrappers.h and psa_crypto_driver_wrappers_no_static
|
|
|
|
based on template files in script/data_files/driver_templates/.
|
2021-12-19 04:47:04 +01:00
|
|
|
"""
|
|
|
|
# Copyright The Mbed TLS Contributors
|
2023-11-14 16:16:38 +01:00
|
|
|
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2021-11-17 04:14:07 +01:00
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
2021-12-24 08:20:36 +01:00
|
|
|
import json
|
2022-09-17 23:13:52 +02:00
|
|
|
from typing import NewType, Dict, Any
|
|
|
|
from traceback import format_tb
|
2021-12-19 04:47:04 +01:00
|
|
|
import argparse
|
2022-01-09 10:31:20 +01:00
|
|
|
import jsonschema
|
2021-11-17 04:14:07 +01:00
|
|
|
import jinja2
|
2021-12-19 04:47:04 +01:00
|
|
|
from mbedtls_dev import build_tree
|
2021-11-17 04:14:07 +01:00
|
|
|
|
2022-02-27 01:08:55 +01:00
|
|
|
JSONSchema = NewType('JSONSchema', object)
|
2022-03-13 13:27:45 +01:00
|
|
|
# The Driver is an Object, but practically it's indexable and can called a dictionary to
|
|
|
|
# keep MyPy happy till MyPy comes with a more composite type for JsonObjects.
|
|
|
|
Driver = NewType('Driver', dict)
|
2022-02-27 01:08:55 +01:00
|
|
|
|
2022-09-15 14:25:37 +02:00
|
|
|
|
|
|
|
class JsonValidationException(Exception):
|
|
|
|
def __init__(self, message="Json Validation Failed"):
|
|
|
|
self.message = message
|
|
|
|
super().__init__(self.message)
|
|
|
|
|
|
|
|
|
2022-09-17 22:07:58 +02:00
|
|
|
class DriverReaderException(Exception):
|
|
|
|
def __init__(self, message="Driver Reader Failed"):
|
|
|
|
self.message = message
|
|
|
|
super().__init__(self.message)
|
|
|
|
|
|
|
|
|
2021-12-24 08:20:36 +01:00
|
|
|
def render(template_path: str, driver_jsoncontext: list) -> str:
|
2021-12-19 04:47:04 +01:00
|
|
|
"""
|
2021-12-24 08:20:36 +01:00
|
|
|
Render template from the input file and driver JSON.
|
2021-12-19 04:47:04 +01:00
|
|
|
"""
|
2021-11-23 10:16:51 +01:00
|
|
|
environment = jinja2.Environment(
|
|
|
|
loader=jinja2.FileSystemLoader(os.path.dirname(template_path)),
|
|
|
|
keep_trailing_newline=True)
|
|
|
|
template = environment.get_template(os.path.basename(template_path))
|
2021-12-19 04:47:04 +01:00
|
|
|
|
2022-01-09 10:31:20 +01:00
|
|
|
return template.render(drivers=driver_jsoncontext)
|
2021-11-17 04:14:07 +01:00
|
|
|
|
2022-09-17 23:37:16 +02:00
|
|
|
def generate_driver_wrapper_file(template_dir: str,
|
|
|
|
output_dir: str,
|
2023-09-11 04:39:27 +02:00
|
|
|
template_file_name: str,
|
2022-09-17 23:37:16 +02:00
|
|
|
driver_jsoncontext: list) -> None:
|
2021-12-19 04:47:04 +01:00
|
|
|
"""
|
|
|
|
Generate the file psa_crypto_driver_wrapper.c.
|
|
|
|
"""
|
|
|
|
driver_wrapper_template_filename = \
|
2023-09-11 04:39:27 +02:00
|
|
|
os.path.join(template_dir, template_file_name)
|
2021-12-19 04:47:04 +01:00
|
|
|
|
2021-12-24 08:20:36 +01:00
|
|
|
result = render(driver_wrapper_template_filename, driver_jsoncontext)
|
2021-12-19 04:47:04 +01:00
|
|
|
|
2023-09-12 12:45:55 +02:00
|
|
|
with open(file=os.path.join(output_dir, os.path.splitext(template_file_name)[0]),
|
2022-09-17 23:13:52 +02:00
|
|
|
mode='w',
|
|
|
|
encoding='UTF-8') as out_file:
|
2021-12-19 04:47:04 +01:00
|
|
|
out_file.write(result)
|
|
|
|
|
2022-01-09 08:58:28 +01:00
|
|
|
|
2022-09-17 22:07:58 +02:00
|
|
|
def validate_json(driverjson_data: Driver, driverschema_list: dict) -> None:
|
2021-12-24 08:20:36 +01:00
|
|
|
"""
|
2022-02-27 01:08:55 +01:00
|
|
|
Validate the Driver JSON against an appropriate schema
|
|
|
|
the schema passed could be that matching an opaque/ transparent driver.
|
2021-12-24 08:20:36 +01:00
|
|
|
"""
|
2022-02-27 01:08:55 +01:00
|
|
|
driver_type = driverjson_data["type"]
|
|
|
|
driver_prefix = driverjson_data["prefix"]
|
2022-01-09 08:58:28 +01:00
|
|
|
try:
|
2022-02-27 01:08:55 +01:00
|
|
|
_schema = driverschema_list[driver_type]
|
|
|
|
jsonschema.validate(instance=driverjson_data, schema=_schema)
|
|
|
|
except KeyError as err:
|
2022-09-17 22:07:58 +02:00
|
|
|
# This could happen if the driverjson_data.type does not exist in the provided schema list
|
2022-02-27 01:08:55 +01:00
|
|
|
# schemas = {'transparent': transparent_driver_schema, 'opaque': opaque_driver_schema}
|
|
|
|
# Print onto stdout and stderr.
|
|
|
|
print("Unknown Driver type " + driver_type +
|
|
|
|
" for driver " + driver_prefix, str(err))
|
|
|
|
print("Unknown Driver type " + driver_type +
|
|
|
|
" for driver " + driver_prefix, str(err), file=sys.stderr)
|
2022-09-17 22:07:58 +02:00
|
|
|
raise JsonValidationException() from err
|
2022-02-27 01:08:55 +01:00
|
|
|
|
2022-01-09 08:58:28 +01:00
|
|
|
except jsonschema.exceptions.ValidationError as err:
|
2022-02-27 01:08:55 +01:00
|
|
|
# Print onto stdout and stderr.
|
|
|
|
print("Error: Failed to validate data file: {} using schema: {}."
|
|
|
|
"\n Exception Message: \"{}\""
|
|
|
|
" ".format(driverjson_data, _schema, str(err)))
|
|
|
|
print("Error: Failed to validate data file: {} using schema: {}."
|
|
|
|
"\n Exception Message: \"{}\""
|
|
|
|
" ".format(driverjson_data, _schema, str(err)), file=sys.stderr)
|
2022-09-17 22:07:58 +02:00
|
|
|
raise JsonValidationException() from err
|
2021-12-24 08:20:36 +01:00
|
|
|
|
2022-09-15 14:25:37 +02:00
|
|
|
|
|
|
|
def load_driver(schemas: Dict[str, Any], driver_file: str) -> Any:
|
2022-09-17 23:37:16 +02:00
|
|
|
"""loads validated json driver"""
|
2022-09-17 23:13:52 +02:00
|
|
|
with open(file=driver_file, mode='r', encoding='UTF-8') as f:
|
2022-09-15 14:25:37 +02:00
|
|
|
json_data = json.load(f)
|
2022-09-17 22:07:58 +02:00
|
|
|
try:
|
|
|
|
validate_json(json_data, schemas)
|
|
|
|
except JsonValidationException as e:
|
|
|
|
raise DriverReaderException from e
|
2022-09-15 14:25:37 +02:00
|
|
|
return json_data
|
|
|
|
|
|
|
|
|
2023-11-22 17:23:16 +01:00
|
|
|
def load_schemas(project_root: str) -> Dict[str, Any]:
|
2022-09-19 10:03:05 +02:00
|
|
|
"""
|
|
|
|
Load schemas map
|
|
|
|
"""
|
2022-09-17 22:07:58 +02:00
|
|
|
schema_file_paths = {
|
2023-11-22 17:23:16 +01:00
|
|
|
'transparent': os.path.join(project_root,
|
2022-09-17 22:07:58 +02:00
|
|
|
'scripts',
|
|
|
|
'data_files',
|
|
|
|
'driver_jsons',
|
|
|
|
'driver_transparent_schema.json'),
|
2023-11-22 17:23:16 +01:00
|
|
|
'opaque': os.path.join(project_root,
|
2022-09-17 22:07:58 +02:00
|
|
|
'scripts',
|
|
|
|
'data_files',
|
|
|
|
'driver_jsons',
|
2022-09-18 12:37:53 +02:00
|
|
|
'driver_opaque_schema.json')
|
2022-09-17 22:07:58 +02:00
|
|
|
}
|
|
|
|
driver_schema = {}
|
|
|
|
for key, file_path in schema_file_paths.items():
|
2022-09-17 23:13:52 +02:00
|
|
|
with open(file=file_path, mode='r', encoding='UTF-8') as file:
|
2022-09-17 22:07:58 +02:00
|
|
|
driver_schema[key] = json.load(file)
|
|
|
|
return driver_schema
|
|
|
|
|
|
|
|
|
2023-11-22 17:23:16 +01:00
|
|
|
def read_driver_descriptions(project_root: str,
|
2022-09-17 22:07:58 +02:00
|
|
|
json_directory: str,
|
|
|
|
jsondriver_list: str) -> list:
|
2021-12-24 08:20:36 +01:00
|
|
|
"""
|
2022-01-09 08:58:28 +01:00
|
|
|
Merge driver JSON files into a single ordered JSON after validation.
|
2021-12-24 08:20:36 +01:00
|
|
|
"""
|
2023-11-22 17:23:16 +01:00
|
|
|
driver_schema = load_schemas(project_root)
|
2022-09-15 14:25:37 +02:00
|
|
|
|
2022-09-17 23:13:52 +02:00
|
|
|
with open(file=os.path.join(json_directory, jsondriver_list),
|
|
|
|
mode='r',
|
|
|
|
encoding='UTF-8') as driver_list_file:
|
2022-09-17 22:07:58 +02:00
|
|
|
driver_list = json.load(driver_list_file)
|
2022-09-15 14:25:37 +02:00
|
|
|
|
2022-09-17 22:07:58 +02:00
|
|
|
return [load_driver(schemas=driver_schema,
|
|
|
|
driver_file=os.path.join(json_directory, driver_file_name))
|
|
|
|
for driver_file_name in driver_list]
|
|
|
|
|
|
|
|
|
2022-09-17 23:37:16 +02:00
|
|
|
def trace_exception(e: Exception, file=sys.stderr) -> None:
|
|
|
|
"""Prints exception trace to the given TextIO handle"""
|
2022-09-17 22:07:58 +02:00
|
|
|
print("Exception: type: %s, message: %s, trace: %s" % (
|
|
|
|
e.__class__, str(e), format_tb(e.__traceback__)
|
|
|
|
), file)
|
2021-12-24 08:20:36 +01:00
|
|
|
|
|
|
|
|
2023-09-11 12:36:20 +02:00
|
|
|
TEMPLATE_FILENAMES = ["psa_crypto_driver_wrappers.h.jinja",
|
2023-09-11 04:39:27 +02:00
|
|
|
"psa_crypto_driver_wrappers_no_static.c.jinja"]
|
|
|
|
|
2021-12-19 04:47:04 +01:00
|
|
|
def main() -> int:
|
|
|
|
"""
|
|
|
|
Main with command line arguments.
|
|
|
|
"""
|
2023-11-22 18:18:22 +01:00
|
|
|
def_arg_project_root = build_tree.guess_project_root()
|
2021-12-19 09:04:30 +01:00
|
|
|
|
2021-12-19 04:47:04 +01:00
|
|
|
parser = argparse.ArgumentParser()
|
2023-11-22 17:23:16 +01:00
|
|
|
parser.add_argument('--project-root', default=def_arg_project_root,
|
2023-11-15 17:43:02 +01:00
|
|
|
help='root directory of repo source code')
|
2022-04-11 06:42:08 +02:00
|
|
|
parser.add_argument('--template-dir',
|
|
|
|
help='directory holding the driver templates')
|
|
|
|
parser.add_argument('--json-dir',
|
|
|
|
help='directory holding the driver JSONs')
|
2022-03-14 10:59:00 +01:00
|
|
|
parser.add_argument('output_directory', nargs='?',
|
|
|
|
help='output file\'s location')
|
2021-12-19 04:47:04 +01:00
|
|
|
args = parser.parse_args()
|
2021-12-19 09:04:30 +01:00
|
|
|
|
2023-11-22 17:23:16 +01:00
|
|
|
project_root = os.path.abspath(args.project_root)
|
2022-09-17 22:07:58 +02:00
|
|
|
|
2023-11-30 15:33:35 +01:00
|
|
|
crypto_core_directory = build_tree.crypto_core_directory(project_root)
|
2023-11-14 19:05:19 +01:00
|
|
|
|
2022-09-17 22:07:58 +02:00
|
|
|
output_directory = args.output_directory if args.output_directory is not None else \
|
2023-11-30 15:33:35 +01:00
|
|
|
os.path.join(project_root, crypto_core_directory)
|
2023-11-14 19:05:19 +01:00
|
|
|
|
2022-09-17 22:07:58 +02:00
|
|
|
template_directory = args.template_dir if args.template_dir is not None else \
|
2023-11-22 17:23:16 +01:00
|
|
|
os.path.join(project_root,
|
2022-09-17 22:07:58 +02:00
|
|
|
'scripts',
|
|
|
|
'data_files',
|
|
|
|
'driver_templates')
|
|
|
|
json_directory = args.json_dir if args.json_dir is not None else \
|
2023-11-22 17:23:16 +01:00
|
|
|
os.path.join(project_root,
|
2022-09-17 22:07:58 +02:00
|
|
|
'scripts',
|
|
|
|
'data_files',
|
|
|
|
'driver_jsons')
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Read and validate list of driver jsons from driverlist.json
|
2023-11-22 17:23:16 +01:00
|
|
|
merged_driver_json = read_driver_descriptions(project_root,
|
2022-09-17 22:07:58 +02:00
|
|
|
json_directory,
|
|
|
|
'driverlist.json')
|
|
|
|
except DriverReaderException as e:
|
|
|
|
trace_exception(e)
|
2021-12-24 08:20:36 +01:00
|
|
|
return 1
|
2023-09-11 04:39:27 +02:00
|
|
|
for template_filename in TEMPLATE_FILENAMES:
|
|
|
|
generate_driver_wrapper_file(template_directory, output_directory,
|
|
|
|
template_filename, merged_driver_json)
|
2021-12-19 04:47:04 +01:00
|
|
|
return 0
|
|
|
|
|
2022-09-15 14:25:37 +02:00
|
|
|
|
2021-12-19 04:47:04 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main())
|