diff --git a/Tools/ardupilotwaf/boards.py b/Tools/ardupilotwaf/boards.py index c330a55b7d..16c6aff45f 100644 --- a/Tools/ardupilotwaf/boards.py +++ b/Tools/ardupilotwaf/boards.py @@ -282,6 +282,9 @@ class Board: if cfg.options.enable_math_check_indexes: env.CXXFLAGS += ['-DMATH_CHECK_INDEXES'] + if cfg.options.private_key: + env.PRIVATE_KEY = cfg.options.private_key + env.CXXFLAGS += [ '-std=gnu++11', diff --git a/Tools/ardupilotwaf/chibios.py b/Tools/ardupilotwaf/chibios.py index c49068e078..23f79b8fbd 100644 --- a/Tools/ardupilotwaf/chibios.py +++ b/Tools/ardupilotwaf/chibios.py @@ -13,6 +13,7 @@ import sys import re import pickle import struct +import base64 _dynamic_env_data = {} def _load_dynamic_env_data(bld): @@ -229,6 +230,29 @@ def to_unsigned(i): i += 2**32 return i +def sign_firmware(image, private_keyfile): + '''sign firmware with private key''' + try: + import monocypher + except ImportError: + Logs.error("Please install monocypher with: python3 -m pip install pymonocypher") + return None + try: + key = open(private_keyfile, 'r').read() + except Exception as ex: + Logs.error("Failed to open %s" % private_keyfile) + return None + keytype = "PRIVATE_KEYV1:" + if not key.startswith(keytype): + Logs.error("Bad private key file %s" % private_keyfile) + return None + key = base64.b64decode(key[len(keytype):]) + sig = monocypher.signature_sign(key, image) + sig_len = len(sig) + sig_version = 30437 + return struct.pack("