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/struct_pack.py b/tools/modules/struct_pack.py
new file mode 100644
index 0000000..21f5808
--- /dev/null
+++ b/tools/modules/struct_pack.py
@@ -0,0 +1,24 @@
+#!/usr/bin/env python3
+#-------------------------------------------------------------------------------
+# SPDX-FileCopyrightText: Copyright The TrustedFirmware-M Contributors
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+import struct
+
+import logging
+logger = logging.getLogger("TF-M")
+
+def struct_pack(objects, pad_to=0):
+ defstring = "<"
+ for obj in objects:
+ defstring += str(len(obj)) + "s"
+
+ size = struct.calcsize(defstring)
+ if size < pad_to:
+ defstring += str(pad_to - size) + "x"
+ assert size < pad_to or pad_to == 0, "Error padding struct of size {} to {}".format(size, pad_to)
+
+ return (bytes(struct.pack(defstring, *objects)))