Files
libultraship/tools/generate_keys_header.py
LywxandKenix Whisperwind 787ec0bde4 Implemented dynamic loading (#1051)
* Fully migrated event system into lus

* Fixed tidy

* Last tidy error

* Added some more tidy stuff

* Last pass of tidy stuff

* Implemented dynamic module loading among o2r metadata

* Cleanup some things and moved to built-in libtcc

* Reverted CI dependencies

* Trying to fix libtcc compilation

* Another attempt at trying to fix this

* Fixed GetPlatform error

* Attempt at fixing cross-compilation for libtcc

* Another attempt for ios this time

* Disabled codesign on ios

* Addressed tidy comments

* Implemented keystore system for signature validation

* Renamed some things to manifest

* Fixed small compilation issue

* More tidy fixes

* Fully tested checksum and signature validations

* Added a sign tool

* Renamed missing function

* Removed leftover

* Fixed scripts not loading

* Implemented LoadAll based on dependencies

* Small fix so mods can load high resolution textures

* Added line fixer so it says the real path

* Fixed an issue related to f3d modding

* Fixed tidy

* Shifted some opcodes by one

* Fixed weird rebase shenanigans

* Moved to throw when loading untrusted scripts

* Fixed tidy

* Splitted compile and load for better info on ports

* Fixed tidy again

* Fixed wrong types on event system

* Fixed returning an invalid id when sorting, implemented an internal id tracker among optimizing the sort when registering a new listener

* Removed an unused queue

* Moved HexToBytes into StringHelper and renamed KeystoreSystem to Keystore

* Moved ScriptLoader data to be private

* Replaced default license

* Exported symbols on windows, added variables to set more things on libtcc and changed cmake to tell cmake to export a .lib

* Fixed wrong macro

* Fixed an error when generating .a files

* Fixed tidy

* Fixed more tidy

* I hate tidy

* Disabled readability-identifier-naming for original c extern

* Fixed wrong define

* Removed segmented guard because of false positives

* Fixed validation to verify if address comes from a DLL

* Fixed comment

* Fixed bad cast

* Fixed wrong cast again

* Implemented script loading test

* Fixed missing virtual function

* Added prints to tests

* Fix tests

* Fixed Script System tests

* Fixed last error and a better way to test ModInit and ModExit

* Updated dependencies among adding a tool to generate a keypair

* Added a key load based on user-prompt, trying to fix linux test and updated sign tool to add public_key

* Applied formatting on script_test

* Fixed StringHelper format

* Added missing header

* Updated test to be a ptr on status

* Removed unnecessary stuff

* Fully validated and fixed keystore saving and loading

* Updated geenerate keys script

* Fixed CompileAll

* Addressed latest comments

* Updated autogenerated keys script

* Updated untrusted handler to expose key too

* Added an small warning when not using ed25519 keys

* Added missing python dependencies

* Fixed tidy and updated actions

* Another attempt at fixing windows builds

* Removed search by listener validation because sometimes the same callback is used

* Added an error handler just to avoid crashing the program

* Added better errors for compiler

* Splitted logs into better fitted spdlog calls

* Kinda non-scope but i fixed a big performance issue on shaders

* Added KeyOrigin to know if it comes from the game, from lus or from the user

* Renamed from ScriptSystem to ScriptLoader

* Implemented cleanup on context destroy

* Finish some cleaning

* Wrapped dll load code into an optional ifder

* Fixed linux tests

* Enabled dll loading in test

* Inverted define

* Populates initial key list

* Moves keys to root of LUS

---------

Co-authored-by: Kenix Whisperwind <kenixwhisperwind@gmail.com>
2026-04-13 22:40:35 -04:00

62 lines
2.3 KiB
Python

import sys
import os
from cryptography.hazmat.primitives import serialization
def generate_multi_key_header(output_path, pem_files):
key_variable_names = []
with open(output_path, 'w') as out_f:
out_f.write("#pragma once\n\n")
out_f.write("#include <string_view>\n")
out_f.write("#include <array>\n")
out_f.write("#include <cstddef>\n\n")
out_f.write("// Auto-generated file containing public keys\n\n")
out_f.write("struct DefaultKey {\n")
out_f.write(" std::string_view name;\n")
out_f.write(" std::string_view data;\n")
out_f.write("};\n\n")
for pem_path in pem_files:
base_name = os.path.splitext(os.path.basename(pem_path))[0]
with open(pem_path, "rb") as key_f:
key_data = key_f.read()
try:
public_key_obj = serialization.load_pem_public_key(key_data)
except ValueError:
private_key = serialization.load_pem_private_key(
key_data,
password=None
)
public_key_obj = private_key.public_key()
raw_public_key = public_key_obj.public_bytes(
encoding=serialization.Encoding.Raw,
format=serialization.PublicFormat.Raw
)
public_key_hex = raw_public_key.hex()
var_name = f"{base_name}_data"
out_f.write(f'inline constexpr std::string_view {var_name} = "{public_key_hex}";\n')
key_variable_names.append((base_name, var_name))
num_keys = len(key_variable_names)
out_f.write("\n// Table of all public keys\n")
out_f.write(f"inline constexpr std::array<DefaultKey, {num_keys}> AllDefaultKeys = {{\n")
for string_name, var_name in key_variable_names:
out_f.write(f' DefaultKey{{ "{string_name}", {var_name} }},\n')
out_f.write("};\n\n")
out_f.write(f"inline constexpr std::size_t AllDefaultKeysSize = {num_keys};\n")
if __name__ == "__main__":
if len(sys.argv) < 3:
print("Usage: python generate_keys_header.py <output_header_path> <key1.pem> [key2.pem ...]")
sys.exit(1)
output_header = sys.argv[1]
input_pems = sys.argv[2:]
generate_multi_key_header(output_header, input_pems)