Patrik Dominik Pordi cc492bbf6e
[Sponsored by ARK] Encryption (#24489)
* Added the board configs for encryption, I had to disable smbus and px4 io in the arkv6x

* Added the key generator script

* Added the decryptor, logs are needed for it though

* Added the log download and modified the decryptor

* Quick fixes & README

* Additional modifications & cleanup

* Tested upd connection
Adjusted the log downloader to handle multiple entry responses from the FC
Edited README

* Reverted IP address change

* Added pycryptodome to the requirements.txt

* fixes for log download and decryption

* Removed old log decryptors and updated README

* Pointed the ark borads to the dummy key updated the README accordingly

* Adjusted the folders in README, removed new lines

* Extended command line arguments for all possibilities for description

* Added MAV_CMD_REQUEST_AUTOPILOT_CAPABILITIES after heartbeat received to make sure log request is answered in all cases

* Update Tools/log_encryption/README.md

Co-authored-by: Jacob Dahl <37091262+dakejahl@users.noreply.github.com>

* Update Tools/log_encryption/README.md

Co-authored-by: Jacob Dahl <37091262+dakejahl@users.noreply.github.com>

* Update Tools/log_encryption/README.md

Co-authored-by: Jacob Dahl <37091262+dakejahl@users.noreply.github.com>

* Update Tools/log_encryption/README.md

Co-authored-by: Jacob Dahl <37091262+dakejahl@users.noreply.github.com>

* Edited README, changed the serial connection logic and updated logdownload, made decryption a bit easier to understand

* Update Tools/log_encryption/README.md

Co-authored-by: Hamish Willee <hamishwillee@gmail.com>

* Removed new lines

* arkv6x: add individual mags to default.px4board

---------

Co-authored-by: Jacob Dahl <dahl.jakejacob@gmail.com>
Co-authored-by: Alex Klimaj <alex@arkelectron.com>
Co-authored-by: Jacob Dahl <37091262+dakejahl@users.noreply.github.com>
Co-authored-by: Hamish Willee <hamishwillee@gmail.com>
2025-04-02 23:40:45 -08:00

60 lines
2.6 KiB
Python

import os
import subprocess
# Define the main PX4 directory (one level up from Tools)
PX4_MAIN_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "../.."))
# Define the key folder paths
KEY_FOLDER = os.path.join(PX4_MAIN_DIR, "keys")
PUBLIC_KEY_FOLDER = os.path.join(KEY_FOLDER, "public")
PRIVATE_KEY_FOLDER = os.path.join(KEY_FOLDER, "private")
# Define the key file paths
PRIVATE_KEY_PATH = os.path.join(PRIVATE_KEY_FOLDER, "private_key.pem")
PUBLIC_KEY_DER_PATH = os.path.join(PUBLIC_KEY_FOLDER, "public_key.der")
PUBLIC_KEY_PUB_PATH = os.path.join(PUBLIC_KEY_FOLDER, "public_key.pub")
def create_key_folders():
"""Creates key, public, and private folders if they do not exist."""
for folder in [KEY_FOLDER, PUBLIC_KEY_FOLDER, PRIVATE_KEY_FOLDER]:
if not os.path.exists(folder):
os.makedirs(folder)
print(f"Created '{folder}' directory.")
else:
print(f"'{folder}' directory already exists.")
def generate_private_key():
"""Generates a private key if it does not exist."""
if not os.path.exists(PRIVATE_KEY_PATH):
print("Generating private key...")
subprocess.run(["openssl", "genpkey", "-algorithm", "RSA", "-out", PRIVATE_KEY_PATH, "-pkeyopt", "rsa_keygen_bits:2048"])
print(f"Private key generated at: {PRIVATE_KEY_PATH}")
else:
print("Private key already exists.")
def generate_public_key():
"""Generates a public key in DER and PUB formats if they do not exist."""
if not os.path.exists(PUBLIC_KEY_DER_PATH):
print("Generating public key in DER format...")
subprocess.run(["openssl", "rsa", "-pubout", "-in", PRIVATE_KEY_PATH, "-outform", "DER", "-out", PUBLIC_KEY_DER_PATH])
print(f"Public key (DER) generated at: {PUBLIC_KEY_DER_PATH}")
else:
print("Public key (DER) already exists.")
if not os.path.exists(PUBLIC_KEY_PUB_PATH):
print("Generating public key in hex format...")
with open(PUBLIC_KEY_PUB_PATH, "w") as pub_file:
process = subprocess.Popen(["xxd", "-p", PUBLIC_KEY_DER_PATH], stdout=subprocess.PIPE)
output, _ = process.communicate()
hex_string = output.decode().strip().replace("\n", "")
formatted_hex = ", ".join(f"0x{hex_string[i:i+2]}" for i in range(0, len(hex_string), 2))
pub_file.write(formatted_hex)
print(f"Public key (hex) generated at: {PUBLIC_KEY_PUB_PATH}")
else:
print("Public key (hex) already exists.")
if __name__ == "__main__":
create_key_folders()
generate_private_key()
generate_public_key()