Tools: Add python modules
Add generic python modules to TF-M to support build and image packaging
tools.
* arg_utils provides helpers to deal with argparse arguments
* c_include gets the list of include paths for a file from the
`compile_commands.json` build database
* c_macro includes a python implementation of (most of) the C
preprocessor.
* c_struct is a libclang-based evaluator of C datastructures (Including
enums) which can be used to generate python representations of nested
C datastructures which rely on complex macro configuration.
* crypto_conversion_utils provides helpers to convert various types of
crypto keys to different and convert string representations of
algorithms and hash functions to their python objects
* encrypt_data provides functions to encrypt bytes() objects
* file_loader provides automatic handler functions for various filetypes
based on their extensions, primarily useful for loading crypto keys
* key_derivation provides a python implementation of HKDF and a
SP200-108 CMAC KDF, both matching the TF-M/MbedTLS/CC3XX implementation
* sign_data provides functions to perform symmetric and asymmetric
signatures of bytes() objects
* sign_then_encrypt_data provides combined signing and encryption,
either via symmetric AEAD modes or a combination of the sign_data and
encrypt_data modules
* struct_pack provides helper functions for packing bytes objects
together.
Change-Id: I858dd8ef69c9069ec0a44e4ad3f9a1d70cc5d4da
Signed-off-by: Raef Coles <raef.coles@arm.com>
diff --git a/tools/modules/file_loader.py b/tools/modules/file_loader.py
new file mode 100644
index 0000000..0cffaa8
--- /dev/null
+++ b/tools/modules/file_loader.py
@@ -0,0 +1,43 @@
+#!/usr/bin/env python3
+#-------------------------------------------------------------------------------
+# SPDX-FileCopyrightText: Copyright The TrustedFirmware-M Contributors
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+from cryptography.hazmat.primitives.serialization import load_pem_private_key, Encoding, PublicFormat
+import logging
+logger = logging.getLogger("TF-M")
+from os.path import splitext, isfile
+
+def _load_bin(filepath):
+ with open(filepath, 'rb') as input_file:
+ return input_file.read()
+
+def _load_pem(filepath):
+ with open(filepath, 'rb') as input_file:
+ data = input_file.read()
+
+ key = load_pem_private_key(data, password=None)
+
+ # MCUBoot wants DER keys
+ key_bytes = key.public_key().public_bytes(Encoding.DER, PublicFormat.SubjectPublicKeyInfo)
+ logger.info("Loaded DER public key {} of size {} from {}".format(key_bytes, len(key_bytes), filepath))
+
+ return key_bytes
+
+def load_bytes_from_file(filepath):
+ _, extension = splitext(filepath)
+
+ if not filepath or not isfile(filepath):
+ raise FileNotFoundError
+
+ logging.info("Loading bytes from {} with type {}".format(filepath, extension))
+
+ if extension == ".pem":
+ return _load_pem(filepath)
+ elif extension == ".pub":
+ return _load_bin(filepath)[4:]
+ else:
+ return _load_bin(filepath)