diff --git a/core/UPSTREAM.yaml b/core/UPSTREAM.yaml index 04f13493..08e397ab 100644 --- a/core/UPSTREAM.yaml +++ b/core/UPSTREAM.yaml @@ -15,8 +15,8 @@ vendored: - path: core/eos repository: https://github.com/embeddedos-org/eos.git - revision: 5544c98df94ff460c3a074fb4280c614b784b464 - baseline_drift: 44 + revision: dc13c59d89f228c501c2399bcab76b5244426af5 + baseline_drift: 41 - path: core/eboot repository: https://github.com/embeddedos-org/eBoot.git diff --git a/core/eos/CMakeLists.txt b/core/eos/CMakeLists.txt index ceb3bb5d..10c26346 100644 --- a/core/eos/CMakeLists.txt +++ b/core/eos/CMakeLists.txt @@ -44,10 +44,17 @@ add_library(eos_hal STATIC hal/src/hal_extended_stubs.c ) -if(EOS_PLATFORM STREQUAL "linux" OR NOT CMAKE_CROSSCOMPILING) +# Which backend source is compiled, and which one eos_hal_init() falls back to, +# have to be the same decision: hal_common.c reads EOS_HAL_BACKEND_* (eos dc13c59). +if((EOS_PLATFORM STREQUAL "linux" OR NOT CMAKE_CROSSCOMPILING) AND UNIX) target_sources(eos_hal PRIVATE hal/src/hal_linux.c) + target_compile_definitions(eos_hal PRIVATE EOS_HAL_BACKEND_HOST) +elseif(WIN32) + target_sources(eos_hal PRIVATE hal/src/hal_win32.c) + target_compile_definitions(eos_hal PRIVATE EOS_HAL_BACKEND_WIN32) else() target_sources(eos_hal PRIVATE hal/src/hal_rtos.c) + target_compile_definitions(eos_hal PRIVATE EOS_HAL_BACKEND_RTOS) endif() target_include_directories(eos_hal PUBLIC diff --git a/core/eos/GETTING_STARTED.md b/core/eos/GETTING_STARTED.md index 26cc8bd7..b9bcec2a 100644 --- a/core/eos/GETTING_STARTED.md +++ b/core/eos/GETTING_STARTED.md @@ -46,15 +46,15 @@ Best for: nRF52840-DK, nRF52832-DK, or custom nRF52 boards. ### 1. Clone the repository ```bash -git clone https://github.com/anthropic/EoS.git -cd EoS +git clone https://github.com/embeddedos-org/eos.git +cd eos ``` ### 2. Build the blink example ```bash cd eos/examples/blink-gpio cmake -B build \ - -DCMAKE_TOOLCHAIN_FILE=../../toolchains/arm-none-eabi.cmake \ + -DCMAKE_TOOLCHAIN_FILE=../../toolchains/arm-cortex-m4.cmake \ -DEOS_PRODUCT=iot cmake --build build ``` @@ -96,11 +96,11 @@ Best for: STM32H743-Nucleo, STM32F4-Discovery, or custom STM32 boards. ### 1. Clone and build ```bash -git clone https://github.com/anthropic/EoS.git -cd EoS/eos/examples/blink-gpio +git clone https://github.com/embeddedos-org/eos.git +cd eos/examples/blink-gpio cmake -B build \ - -DCMAKE_TOOLCHAIN_FILE=../../toolchains/arm-none-eabi.cmake \ + -DCMAKE_TOOLCHAIN_FILE=../../toolchains/arm-cortex-m4.cmake \ -DEOS_PRODUCT=industrial cmake --build build ``` @@ -128,11 +128,11 @@ Build and run on your development machine — no MCU needed. ### 1. Clone and build ```bash -git clone https://github.com/anthropic/EoS.git -cd EoS/eos/examples/blink-gpio +git clone https://github.com/embeddedos-org/eos.git +cd eos/examples/blink-gpio # Host build uses Linux HAL backend (simulated GPIO via sysfs/printf) -cmake -B build -DEOS_PRODUCT=iot -DEOS_BUILD_TESTS=ON +cmake -B build -DEOS_BUILD_TESTS=ON cmake --build build ``` @@ -246,7 +246,7 @@ Once your application works, add secure boot with eboot: ### 1. Build eboot for your board ```bash cd EoS/eboot -cmake -B build -DEBLDR_BOARD=nrf52 +cmake -B build -DEBLDR_BOARD=nrf52 -DCMAKE_BUILD_TYPE=Debug cmake --build build ``` @@ -264,7 +264,7 @@ This produces: ### 3. Sign and flash ```bash # Create signing key (first time only) -cd EoS/eboot/tools +cd eBoot/tools python3 sign_image.py --generate-key my-key.pem # Sign firmware diff --git a/core/eos/NOTICE b/core/eos/NOTICE index 2a16b335..60484a6e 100644 --- a/core/eos/NOTICE +++ b/core/eos/NOTICE @@ -3,4 +3,22 @@ Copyright (c) 2026 EoS Project SPDX-License-Identifier: MIT Monorepo: https://github.com/embeddedos-org/ebuild -Website: https://embeddedos-org.github.io \ No newline at end of file +Website: https://embeddedos-org.github.io + +-------------------------------------------------------------------------------- +Third-party components +-------------------------------------------------------------------------------- + +This section is the dependency and licence inventory required by §26 of the +EmbeddedOS Platform Architecture & Ecosystem Design Document, and by ADR-017. + +Every directory under third_party/ must appear here, with the licence it ships +under. This includes machine-learning model weights, whose licences are bespoke +agreements rather than SPDX identifiers and may restrict redistribution. scripts/check_third_party.py fails the build when one does not, and +scripts/generate_sbom.py emits the machine-readable form to sbom.json. + + (none yet) + +External source may only be added under third_party/, at a pinned revision, with +its licence copied verbatim. See third_party/README.md for the procedure and +ADR-017 for the reasoning. diff --git a/core/eos/backends/CMakeLists.txt b/core/eos/backends/CMakeLists.txt index 2f4ee44a..8b58c8ca 100644 --- a/core/eos/backends/CMakeLists.txt +++ b/core/eos/backends/CMakeLists.txt @@ -13,10 +13,6 @@ add_library(eos_backends STATIC src/cargo_backend.c ) -target_include_directories(eos_backends PUBLIC - ${CMAKE_CURRENT_SOURCE_DIR}/include -) - target_link_libraries(eos_backends PRIVATE eos_core) target_compile_options(eos_backends PRIVATE diff --git a/core/eos/backends/include/eos/backend.h b/core/eos/backends/include/eos/backend.h deleted file mode 100644 index 9dee5445..00000000 --- a/core/eos/backends/include/eos/backend.h +++ /dev/null @@ -1,81 +0,0 @@ -// SPDX-License-Identifier: MIT -// Copyright (c) 2026 EoS Project -// ISO/IEC 25000 | ISO/IEC/IEEE 15288:2023 - -/** - * @file backend.h - * @brief Build backend interface and registry. - * - * Each build system (CMake, Ninja, Make, Kbuild, Zephyr, FreeRTOS, NuttX) - * is wrapped as an EosBackend with four operations: configure, build, - * install, and clean. Backends are registered at startup and looked up - * by name or EosBuildType when the scheduler needs to build a node. - */ - -#ifndef EOS_BACKEND_H -#define EOS_BACKEND_H - -#include "eos/types.h" -#include "eos/error.h" - -#define EOS_MAX_BACKENDS 16 /**< Maximum registered backends */ - -/** - * @brief Build backend — thin adapter around a native build system. - * - * Each backend provides four function pointers following the same - * lifecycle: configure → build → install → clean. - */ -typedef struct EosBackend { - char name[EOS_MAX_NAME]; /**< Backend identifier (e.g., "cmake", "zephyr") */ - EosBuildType type; /**< Corresponding build type enum */ - - /** @brief Configure the build (e.g., cmake -S ... -B ...) */ - EosResult (*configure)(struct EosBackend *self, const char *src_dir, - const char *build_dir, const char *toolchain_file, - const EosKeyValue *options, int option_count); - - /** @brief Execute the build (e.g., cmake --build ...) */ - EosResult (*build)(struct EosBackend *self, const char *build_dir, int jobs); - - /** @brief Install built artifacts to a destination directory */ - EosResult (*install)(struct EosBackend *self, const char *build_dir, - const char *install_dir); - - /** @brief Clean build artifacts */ - EosResult (*clean)(struct EosBackend *self, const char *build_dir); -} EosBackend; - -/** @brief Initialize the global backend registry. Must be called before register. */ -void eos_backend_registry_init(void); - -/** @brief Register a backend in the global registry. */ -EosResult eos_backend_register(EosBackend *backend); - -/** @brief Find a registered backend by name. Returns NULL if not found. */ -EosBackend *eos_backend_find(const char *name); - -/** @brief Find a registered backend by build type. Returns NULL if not found. */ -EosBackend *eos_backend_find_by_type(EosBuildType type); - -/* ---- Built-in backend constructors ---- */ - -void eos_backend_cmake_init(EosBackend *b); /**< CMake + Ninja */ -void eos_backend_ninja_init(EosBackend *b); /**< Direct Ninja */ -void eos_backend_make_init(EosBackend *b); /**< GNU Make / Autotools */ -void eos_backend_kbuild_init(EosBackend *b); /**< Linux Kbuild */ - -/* ---- RTOS backend constructors ---- */ - -void eos_backend_zephyr_init(EosBackend *b); /**< Zephyr RTOS (west/CMake) */ -void eos_backend_freertos_init(EosBackend *b); /**< FreeRTOS (CMake/Make/vendor) */ -void eos_backend_nuttx_init(EosBackend *b); /**< Apache NuttX (native) */ - -/* ---- Additional backend constructors ---- */ - -void eos_backend_meson_init(EosBackend *b); /**< Meson + Ninja */ -void eos_backend_autotools_init(EosBackend *b); /**< Autoconf / Automake */ -void eos_backend_buildroot_init(EosBackend *b); /**< Buildroot (kernel+rootfs+image) */ -void eos_backend_cargo_init(EosBackend *b); /**< Rust Cargo */ - -#endif /* EOS_BACKEND_H */ diff --git a/core/eos/backends/src/autotools_backend.c b/core/eos/backends/src/autotools_backend.c index 17886c7c..a240184b 100644 --- a/core/eos/backends/src/autotools_backend.c +++ b/core/eos/backends/src/autotools_backend.c @@ -3,61 +3,64 @@ // ISO/IEC 25000 | ISO/IEC/IEEE 15288:2023 #include "eos/backend.h" -#include "eos/log.h" +#include "eos/shell_cmd.h" #include -#include #include static EosResult autotools_configure(EosBackend *self, const char *src_dir, const char *build_dir, const char *toolchain_file, const EosKeyValue *options, int option_count) { - (void)self; - (void)build_dir; - char cmd[2048]; - int offset = snprintf(cmd, sizeof(cmd), "cd \"%s\" && ./configure", src_dir); - + (void)self; (void)build_dir; + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "cd "); + eos_shell_cmd_arg(&cmd, src_dir); + eos_shell_cmd_text(&cmd, " && ./configure"); if (toolchain_file && toolchain_file[0]) { - offset += snprintf(cmd + offset, sizeof(cmd) - (size_t)offset, - " --host=%s", toolchain_file); + eos_shell_cmd_text(&cmd, " --host="); + eos_shell_cmd_arg(&cmd, toolchain_file); } - - for (int i = 0; i < option_count && offset < (int)sizeof(cmd) - 64; i++) { - offset += snprintf(cmd + offset, sizeof(cmd) - (size_t)offset, - " --%s=%s", options[i].key, options[i].value); + /* --=: the key is an option name, one word; the value quoted. */ + for (int i = 0; i < option_count; i++) { + eos_shell_cmd_text(&cmd, " --"); + eos_shell_cmd_word(&cmd, options[i].key); + eos_shell_cmd_text(&cmd, "="); + eos_shell_cmd_arg(&cmd, options[i].value); } - - EOS_INFO("Autotools configure: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + return eos_shell_cmd_run(&cmd, "Autotools configure"); } static EosResult autotools_build(EosBackend *self, const char *build_dir, int jobs) { (void)self; - char cmd[1024]; - snprintf(cmd, sizeof(cmd), "make -C \"%s\" -j%d", build_dir, jobs > 0 ? jobs : 4); - EOS_INFO("Autotools build: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "make -C "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, " -j"); + eos_shell_cmd_int(&cmd, jobs > 0 ? jobs : 4); + return eos_shell_cmd_run(&cmd, "Autotools build"); } -static EosResult autotools_install(EosBackend *self, const char *build_dir, - const char *install_dir) { +static EosResult autotools_clean(EosBackend *self, const char *build_dir) { (void)self; - char cmd[1024]; - snprintf(cmd, sizeof(cmd), "make -C \"%s\" install DESTDIR=\"%s\"", - build_dir, install_dir); - EOS_INFO("Autotools install: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "make -C "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, " distclean"); + return eos_shell_cmd_run(&cmd, "Autotools clean"); } -static EosResult autotools_clean(EosBackend *self, const char *build_dir) { +static EosResult autotools_install(EosBackend *self, const char *build_dir, + const char *install_dir) { (void)self; - char cmd[1024]; - snprintf(cmd, sizeof(cmd), "make -C \"%s\" distclean", build_dir); - EOS_INFO("Autotools clean: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "make -C "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, " install DESTDIR="); + eos_shell_cmd_arg(&cmd, install_dir); + return eos_shell_cmd_run(&cmd, "Autotools install"); } void eos_backend_autotools_init(EosBackend *b) { diff --git a/core/eos/backends/src/buildroot_backend.c b/core/eos/backends/src/buildroot_backend.c index d60fad92..4d7eea6c 100644 --- a/core/eos/backends/src/buildroot_backend.c +++ b/core/eos/backends/src/buildroot_backend.c @@ -3,17 +3,14 @@ // ISO/IEC 25000 | ISO/IEC/IEEE 15288:2023 #include "eos/backend.h" -#include "eos/log.h" +#include "eos/shell_cmd.h" #include -#include #include static EosResult buildroot_configure(EosBackend *self, const char *src_dir, const char *build_dir, const char *toolchain_file, const EosKeyValue *options, int option_count) { (void)self; - char cmd[2048]; - const char *defconfig = "qemu_aarch64_virt_defconfig"; for (int i = 0; i < option_count; i++) { if (strcmp(options[i].key, "defconfig") == 0) { @@ -22,54 +19,67 @@ static EosResult buildroot_configure(EosBackend *self, const char *src_dir, } } - int offset = snprintf(cmd, sizeof(cmd), - "make -C \"%s\" O=\"%s\" %s", - src_dir, build_dir, defconfig); - + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "make -C "); + eos_shell_cmd_arg(&cmd, src_dir); + eos_shell_cmd_text(&cmd, " O="); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, " "); + eos_shell_cmd_word(&cmd, defconfig); if (toolchain_file && toolchain_file[0]) { - offset += snprintf(cmd + offset, sizeof(cmd) - (size_t)offset, - " BR2_TOOLCHAIN_EXTERNAL_PATH=\"%s\"", toolchain_file); + eos_shell_cmd_text(&cmd, " BR2_TOOLCHAIN_EXTERNAL_PATH="); + eos_shell_cmd_arg(&cmd, toolchain_file); } - - EOS_INFO("Buildroot configure: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + return eos_shell_cmd_run(&cmd, "Buildroot configure"); } static EosResult buildroot_build(EosBackend *self, const char *build_dir, int jobs) { (void)self; - char cmd[1024]; - snprintf(cmd, sizeof(cmd), "make -C \"%s\" -j%d", build_dir, jobs > 0 ? jobs : 4); - EOS_INFO("Buildroot build: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "make -C "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, " -j"); + eos_shell_cmd_int(&cmd, jobs > 0 ? jobs : 4); + return eos_shell_cmd_run(&cmd, "Buildroot build"); +} + +static EosResult buildroot_clean(EosBackend *self, const char *build_dir) { + (void)self; + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "make -C "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, " clean"); + return eos_shell_cmd_run(&cmd, "Buildroot clean"); } static EosResult buildroot_install(EosBackend *self, const char *build_dir, const char *install_dir) { (void)self; - char cmd[1024]; + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); #ifdef _WIN32 - snprintf(cmd, sizeof(cmd), - "if not exist \"%s\" mkdir \"%s\" && copy /Y \"%s\\images\\*\" \"%s\\\"", - install_dir, install_dir, build_dir, install_dir); + eos_shell_cmd_text(&cmd, "if not exist "); + eos_shell_cmd_arg(&cmd, install_dir); + eos_shell_cmd_text(&cmd, " mkdir "); + eos_shell_cmd_arg(&cmd, install_dir); + eos_shell_cmd_text(&cmd, " && copy /Y "); + eos_shell_cmd_arg(&cmd, build_dir); /* the quoted path, then the glob */ + eos_shell_cmd_text(&cmd, "\\images\\* "); + eos_shell_cmd_arg(&cmd, install_dir); + eos_shell_cmd_text(&cmd, "\\"); #else - snprintf(cmd, sizeof(cmd), - "mkdir -p \"%s\" && cp -r \"%s/images/\"* \"%s/\" 2>/dev/null || true", - install_dir, build_dir, install_dir); + eos_shell_cmd_text(&cmd, "mkdir -p "); + eos_shell_cmd_arg(&cmd, install_dir); + eos_shell_cmd_text(&cmd, " && cp -r "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, "/images/* "); + eos_shell_cmd_arg(&cmd, install_dir); + eos_shell_cmd_text(&cmd, "/"); #endif - EOS_INFO("Buildroot install: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; -} - -static EosResult buildroot_clean(EosBackend *self, const char *build_dir) { - (void)self; - char cmd[1024]; - snprintf(cmd, sizeof(cmd), "make -C \"%s\" clean", build_dir); - EOS_INFO("Buildroot clean: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + return eos_shell_cmd_run(&cmd, "Buildroot install"); } void eos_backend_buildroot_init(EosBackend *b) { diff --git a/core/eos/backends/src/cargo_backend.c b/core/eos/backends/src/cargo_backend.c index ff709cfa..de15bc30 100644 --- a/core/eos/backends/src/cargo_backend.c +++ b/core/eos/backends/src/cargo_backend.c @@ -3,21 +3,19 @@ // ISO/IEC 25000 | ISO/IEC/IEEE 15288:2023 #include "eos/backend.h" +#include "eos/shell_cmd.h" #include "eos/log.h" #include -#include #include static EosResult cargo_configure(EosBackend *self, const char *src_dir, const char *build_dir, const char *toolchain_file, const EosKeyValue *options, int option_count) { - (void)self; - (void)build_dir; - (void)options; - (void)option_count; + (void)self; (void)build_dir; (void)options; (void)option_count; if (toolchain_file && toolchain_file[0]) { - EOS_INFO("Cargo: target=%s (set via --target)", toolchain_file); + EOS_DEBUG("Cargo: toolchain file %s is not used; set a target in Cargo config", + toolchain_file); } /* Cargo doesn't have a separate configure step; verify Cargo.toml exists */ @@ -29,49 +27,55 @@ static EosResult cargo_configure(EosBackend *self, const char *src_dir, return EOS_ERR_NOT_FOUND; } fclose(fp); - EOS_DEBUG("Cargo: found Cargo.toml at %s", toml_path); return EOS_OK; } static EosResult cargo_build(EosBackend *self, const char *build_dir, int jobs) { (void)self; - char cmd[1024]; - snprintf(cmd, sizeof(cmd), "cargo build --manifest-path \"%s/Cargo.toml\" " - "--release -j %d", build_dir, jobs > 0 ? jobs : 4); - EOS_INFO("Cargo build: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "cargo build --manifest-path "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, "/Cargo.toml --release -j "); + eos_shell_cmd_int(&cmd, jobs > 0 ? jobs : 4); + return eos_shell_cmd_run(&cmd, "Cargo build"); } static EosResult cargo_install(EosBackend *self, const char *build_dir, const char *install_dir) { (void)self; - char cmd[1024]; + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); #ifdef _WIN32 - snprintf(cmd, sizeof(cmd), - "if not exist \"%s\\bin\" mkdir \"%s\\bin\" && " - "copy /Y \"%s\\target\\release\\*.exe\" \"%s\\bin\\\" 2>nul", - install_dir, install_dir, build_dir, install_dir); + eos_shell_cmd_text(&cmd, "if not exist "); + eos_shell_cmd_arg(&cmd, install_dir); + eos_shell_cmd_text(&cmd, "\\bin mkdir "); + eos_shell_cmd_arg(&cmd, install_dir); + eos_shell_cmd_text(&cmd, "\\bin && copy /Y "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, "\\target\\release\\*.exe "); + eos_shell_cmd_arg(&cmd, install_dir); + eos_shell_cmd_text(&cmd, "\\bin\\ 2>nul"); #else - snprintf(cmd, sizeof(cmd), - "mkdir -p \"%s/bin\" && " - "find \"%s/target/release\" -maxdepth 1 -type f -executable " - "-exec cp {} \"%s/bin/\" \\; 2>/dev/null || true", - install_dir, build_dir, install_dir); + eos_shell_cmd_text(&cmd, "mkdir -p "); + eos_shell_cmd_arg(&cmd, install_dir); + eos_shell_cmd_text(&cmd, "/bin && find "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, "/target/release -maxdepth 1 -type f \\( -perm -0100 -o -perm -0010 -o -perm -0001 \\) -exec cp {} "); + eos_shell_cmd_arg(&cmd, install_dir); + eos_shell_cmd_text(&cmd, "/bin/ \\;"); #endif - EOS_INFO("Cargo install: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + return eos_shell_cmd_run(&cmd, "Cargo install"); } static EosResult cargo_clean(EosBackend *self, const char *build_dir) { (void)self; - char cmd[1024]; - snprintf(cmd, sizeof(cmd), "cargo clean --manifest-path \"%s/Cargo.toml\"", - build_dir); - EOS_INFO("Cargo clean: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "cargo clean --manifest-path "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, "/Cargo.toml"); + return eos_shell_cmd_run(&cmd, "Cargo clean"); } void eos_backend_cargo_init(EosBackend *b) { diff --git a/core/eos/backends/src/cmake_backend.c b/core/eos/backends/src/cmake_backend.c index 7376435c..d955e494 100644 --- a/core/eos/backends/src/cmake_backend.c +++ b/core/eos/backends/src/cmake_backend.c @@ -3,61 +3,73 @@ // ISO/IEC 25000 | ISO/IEC/IEEE 15288:2023 #include "eos/backend.h" -#include "eos/log.h" +#include "eos/shell_cmd.h" #include -#include #include +/* -D= for every option; the key unquoted as one word, the value + * quoted. A value with spaces used to split into two arguments. */ +static void append_defines(EosShellCmd *cmd, const EosKeyValue *options, + int option_count, const char *skip_key) { + for (int i = 0; i < option_count; i++) { + if (skip_key && strcmp(options[i].key, skip_key) == 0) continue; + eos_shell_cmd_text(cmd, " -D"); + eos_shell_cmd_word(cmd, options[i].key); + eos_shell_cmd_text(cmd, "="); + eos_shell_cmd_arg(cmd, options[i].value); + } +} + static EosResult cmake_configure(EosBackend *self, const char *src_dir, - const char *build_dir, const char *toolchain_file, - const EosKeyValue *options, int option_count) { + const char *build_dir, const char *toolchain_file, + const EosKeyValue *options, int option_count) { (void)self; - char cmd[2048]; - int offset = snprintf(cmd, sizeof(cmd), "cmake -S \"%s\" -B \"%s\" -G Ninja", - src_dir, build_dir); - + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "cmake -S "); + eos_shell_cmd_arg(&cmd, src_dir); + eos_shell_cmd_text(&cmd, " -B "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, " -G Ninja"); if (toolchain_file && toolchain_file[0]) { - offset += snprintf(cmd + offset, sizeof(cmd) - (size_t)offset, - " -DCMAKE_TOOLCHAIN_FILE=\"%s\"", toolchain_file); + eos_shell_cmd_text(&cmd, " -DCMAKE_TOOLCHAIN_FILE="); + eos_shell_cmd_arg(&cmd, toolchain_file); } - - for (int i = 0; i < option_count && offset < (int)sizeof(cmd) - 64; i++) { - offset += snprintf(cmd + offset, sizeof(cmd) - (size_t)offset, - " -D%s=%s", options[i].key, options[i].value); - } - - EOS_INFO("CMake configure: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + append_defines(&cmd, options, option_count, NULL); + return eos_shell_cmd_run(&cmd, "CMake configure"); } static EosResult cmake_build(EosBackend *self, const char *build_dir, int jobs) { (void)self; - char cmd[1024]; - snprintf(cmd, sizeof(cmd), "cmake --build \"%s\" -j %d", build_dir, jobs > 0 ? jobs : 4); - EOS_INFO("CMake build: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "cmake --build "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, " -j "); + eos_shell_cmd_int(&cmd, jobs > 0 ? jobs : 4); + return eos_shell_cmd_run(&cmd, "CMake build"); } static EosResult cmake_install(EosBackend *self, const char *build_dir, - const char *install_dir) { + const char *install_dir) { (void)self; - char cmd[1024]; - snprintf(cmd, sizeof(cmd), "cmake --install \"%s\" --prefix \"%s\"", - build_dir, install_dir); - EOS_INFO("CMake install: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "cmake --install "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, " --prefix "); + eos_shell_cmd_arg(&cmd, install_dir); + return eos_shell_cmd_run(&cmd, "CMake install"); } static EosResult cmake_clean(EosBackend *self, const char *build_dir) { (void)self; - char cmd[1024]; - snprintf(cmd, sizeof(cmd), "cmake --build \"%s\" --target clean", build_dir); - EOS_INFO("CMake clean: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "cmake --build "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, " --target clean"); + return eos_shell_cmd_run(&cmd, "CMake clean"); } void eos_backend_cmake_init(EosBackend *b) { diff --git a/core/eos/backends/src/freertos_backend.c b/core/eos/backends/src/freertos_backend.c index 4a840171..d3d338e9 100644 --- a/core/eos/backends/src/freertos_backend.c +++ b/core/eos/backends/src/freertos_backend.c @@ -3,77 +3,137 @@ // ISO/IEC 25000 | ISO/IEC/IEEE 15288:2023 #include "eos/backend.h" +#include "eos/shell_cmd.h" #include "eos/log.h" #include -#include #include +#ifdef _WIN32 +#include +#endif + +/* -D= for every option; the key unquoted as one word, the value + * quoted. A value with spaces used to split into two arguments. */ +static void append_defines(EosShellCmd *cmd, const EosKeyValue *options, + int option_count, const char *skip_key) { + for (int i = 0; i < option_count; i++) { + if (skip_key && strcmp(options[i].key, skip_key) == 0) continue; + eos_shell_cmd_text(cmd, " -D"); + eos_shell_cmd_word(cmd, options[i].key); + eos_shell_cmd_text(cmd, "="); + eos_shell_cmd_arg(cmd, options[i].value); + } +} static EosResult freertos_configure(EosBackend *self, const char *src_dir, const char *build_dir, const char *toolchain_file, const EosKeyValue *options, int option_count) { (void)self; - char cmd[2048]; - int offset = snprintf(cmd, sizeof(cmd), - "cmake -S \"%s\" -B \"%s\" -G Ninja", - src_dir, build_dir); - + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "cmake -S "); + eos_shell_cmd_arg(&cmd, src_dir); + eos_shell_cmd_text(&cmd, " -B "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, " -G Ninja"); if (toolchain_file && toolchain_file[0]) { - offset += snprintf(cmd + offset, sizeof(cmd) - (size_t)offset, - " -DCMAKE_TOOLCHAIN_FILE=\"%s\"", toolchain_file); - } - - /* Pass FREERTOS_KERNEL_PATH if available */ - for (int i = 0; i < option_count && offset < (int)sizeof(cmd) - 64; i++) { - offset += snprintf(cmd + offset, sizeof(cmd) - (size_t)offset, - " -D%s=%s", options[i].key, options[i].value); + eos_shell_cmd_text(&cmd, " -DCMAKE_TOOLCHAIN_FILE="); + eos_shell_cmd_arg(&cmd, toolchain_file); } - - EOS_INFO("FreeRTOS configure: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + /* Pass FREERTOS_KERNEL_PATH and the rest as -D options. */ + append_defines(&cmd, options, option_count, NULL); + return eos_shell_cmd_run(&cmd, "FreeRTOS configure"); } static EosResult freertos_build(EosBackend *self, const char *build_dir, int jobs) { (void)self; - char cmd[1024]; - snprintf(cmd, sizeof(cmd), "cmake --build \"%s\" -j %d", build_dir, jobs > 0 ? jobs : 4); - EOS_INFO("FreeRTOS build: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "cmake --build "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, " -j "); + eos_shell_cmd_int(&cmd, jobs > 0 ? jobs : 4); + return eos_shell_cmd_run(&cmd, "FreeRTOS build"); } +#ifdef _WIN32 +/* Copy every build_dir\*. into install_dir. Returns 0, or -1 if a match + * could not be copied or its path did not fit. No match is not an error. */ +static int copy_kind_win32(const char *build_dir, const char *install_dir, const char *ext) { + char pattern[MAX_PATH], src[MAX_PATH], dst[MAX_PATH]; + int n = snprintf(pattern, sizeof(pattern), "%s\\*.%s", build_dir, ext); + if (n < 0 || (size_t)n >= sizeof(pattern)) return -1; + WIN32_FIND_DATAA fd; + HANDLE h = FindFirstFileA(pattern, &fd); + if (h == INVALID_HANDLE_VALUE) + return GetLastError() == ERROR_FILE_NOT_FOUND ? 0 : -1; + int rc = 0; + do { + if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) continue; + n = snprintf(src, sizeof(src), "%s\\%s", build_dir, fd.cFileName); + if (n < 0 || (size_t)n >= sizeof(src)) { rc = -1; continue; } + n = snprintf(dst, sizeof(dst), "%s\\%s", install_dir, fd.cFileName); + if (n < 0 || (size_t)n >= sizeof(dst)) { rc = -1; continue; } + if (!CopyFileA(src, dst, FALSE)) rc = -1; + } while (FindNextFileA(h, &fd)); + FindClose(h); + return rc; +} +#endif + static EosResult freertos_install(EosBackend *self, const char *build_dir, const char *install_dir) { (void)self; - char cmd[1024]; - snprintf(cmd, sizeof(cmd), #ifdef _WIN32 - "if not exist \"%s\" mkdir \"%s\" && copy /Y \"%s\\*.bin\" \"%s\\\" 2>nul & " - "copy /Y \"%s\\*.elf\" \"%s\\\" 2>nul & " - "copy /Y \"%s\\*.hex\" \"%s\\\" 2>nul", - install_dir, install_dir, - build_dir, install_dir, - build_dir, install_dir, - build_dir, install_dir + /* In-process, like the POSIX find: a cmd.exe chain of + * "copy *.bin & copy *.elf & copy *.hex" took its exit status from the + * last copy, so a build with a .bin but no .hex failed to install. */ + /* The same refusal every backend gives (test_shell_cmd.c checks it): + * nothing here goes through a shell, but a value the shell builder would + * refuse is not a directory name this code should act on. */ + if (!eos_shell_arg_is_safe(build_dir) || !eos_shell_arg_is_safe(install_dir)) { + EOS_ERROR("FreeRTOS install: refusing an unsafe directory name"); + return EOS_ERR_INVALID; + } + DWORD attrs = GetFileAttributesA(build_dir); + if (attrs == INVALID_FILE_ATTRIBUTES || !(attrs & FILE_ATTRIBUTE_DIRECTORY)) { + EOS_ERROR("FreeRTOS install: build directory not found: %s", build_dir); + return EOS_ERR_NOT_FOUND; + } + if (!CreateDirectoryA(install_dir, NULL) && GetLastError() != ERROR_ALREADY_EXISTS) { + EOS_ERROR("FreeRTOS install: cannot create %s", install_dir); + return EOS_ERR_IO; + } + static const char *const kinds[] = { "bin", "elf", "hex" }; + for (size_t i = 0; i < sizeof(kinds) / sizeof(kinds[0]); i++) { + if (copy_kind_win32(build_dir, install_dir, kinds[i]) != 0) { + EOS_ERROR("FreeRTOS install: copying *.%s from %s failed", kinds[i], build_dir); + return EOS_ERR_IO; + } + } + EOS_INFO("FreeRTOS install: images from %s copied to %s", build_dir, install_dir); + return EOS_OK; #else - "mkdir -p \"%s\" && " - "find \"%s\" -maxdepth 2 \\( -name '*.bin' -o -name '*.elf' -o -name '*.hex' \\) " - "-exec cp {} \"%s/\" \\; 2>/dev/null || true", - install_dir, build_dir, install_dir + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "mkdir -p "); + eos_shell_cmd_arg(&cmd, install_dir); + eos_shell_cmd_text(&cmd, " && find "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, " -maxdepth 2 \\( -name '*.bin' -o -name '*.elf' -o -name '*.hex' \\) -exec cp {} "); + eos_shell_cmd_arg(&cmd, install_dir); + eos_shell_cmd_text(&cmd, "/ \\;"); + return eos_shell_cmd_run(&cmd, "FreeRTOS install"); #endif - ); - EOS_INFO("FreeRTOS install: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; } static EosResult freertos_clean(EosBackend *self, const char *build_dir) { (void)self; - char cmd[1024]; - snprintf(cmd, sizeof(cmd), "cmake --build \"%s\" --target clean", build_dir); - EOS_INFO("FreeRTOS clean: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "cmake --build "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, " --target clean"); + return eos_shell_cmd_run(&cmd, "FreeRTOS clean"); } void eos_backend_freertos_init(EosBackend *b) { diff --git a/core/eos/backends/src/kbuild_backend.c b/core/eos/backends/src/kbuild_backend.c index c91ad3a4..f3be8115 100644 --- a/core/eos/backends/src/kbuild_backend.c +++ b/core/eos/backends/src/kbuild_backend.c @@ -3,19 +3,14 @@ // ISO/IEC 25000 | ISO/IEC/IEEE 15288:2023 #include "eos/backend.h" -#include "eos/log.h" +#include "eos/shell_cmd.h" #include -#include #include static EosResult kbuild_configure(EosBackend *self, const char *src_dir, - const char *build_dir, const char *toolchain_file, - const EosKeyValue *options, int option_count) { + const char *build_dir, const char *toolchain_file, + const EosKeyValue *options, int option_count) { (void)self; - (void)build_dir; - (void)options; - (void)option_count; - /* Find defconfig from options */ const char *defconfig = "defconfig"; for (int i = 0; i < option_count; i++) { @@ -25,48 +20,54 @@ static EosResult kbuild_configure(EosBackend *self, const char *src_dir, } } - char cmd[2048]; + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "make -C "); + eos_shell_cmd_arg(&cmd, src_dir); + eos_shell_cmd_text(&cmd, " O="); + eos_shell_cmd_arg(&cmd, build_dir); if (toolchain_file && toolchain_file[0]) { - snprintf(cmd, sizeof(cmd), - "make -C \"%s\" O=\"%s\" ARCH=%s CROSS_COMPILE=%s- %s", - src_dir, build_dir, "arm64", toolchain_file, defconfig); - } else { - snprintf(cmd, sizeof(cmd), "make -C \"%s\" O=\"%s\" %s", - src_dir, build_dir, defconfig); + /* make reads ARCH=, CROSS_COMPILE= and the target as single words. */ + eos_shell_cmd_text(&cmd, " ARCH=arm64 CROSS_COMPILE="); + eos_shell_cmd_word(&cmd, toolchain_file); + eos_shell_cmd_text(&cmd, "-"); } - - EOS_INFO("Kbuild configure: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + eos_shell_cmd_text(&cmd, " "); + eos_shell_cmd_word(&cmd, defconfig); + return eos_shell_cmd_run(&cmd, "Kbuild configure"); } static EosResult kbuild_build(EosBackend *self, const char *build_dir, int jobs) { (void)self; - char cmd[1024]; - snprintf(cmd, sizeof(cmd), "make -C \"%s\" -j%d", build_dir, jobs > 0 ? jobs : 4); - EOS_INFO("Kbuild build: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "make -C "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, " -j"); + eos_shell_cmd_int(&cmd, jobs > 0 ? jobs : 4); + return eos_shell_cmd_run(&cmd, "Kbuild build"); } -static EosResult kbuild_install(EosBackend *self, const char *build_dir, - const char *install_dir) { +static EosResult kbuild_clean(EosBackend *self, const char *build_dir) { (void)self; - char cmd[1024]; - snprintf(cmd, sizeof(cmd), "make -C \"%s\" install INSTALL_PATH=\"%s\"", - build_dir, install_dir); - EOS_INFO("Kbuild install: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "make -C "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, " mrproper"); + return eos_shell_cmd_run(&cmd, "Kbuild clean"); } -static EosResult kbuild_clean(EosBackend *self, const char *build_dir) { +static EosResult kbuild_install(EosBackend *self, const char *build_dir, + const char *install_dir) { (void)self; - char cmd[1024]; - snprintf(cmd, sizeof(cmd), "make -C \"%s\" mrproper", build_dir); - EOS_INFO("Kbuild clean: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "make -C "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, " install INSTALL_PATH="); + eos_shell_cmd_arg(&cmd, install_dir); + return eos_shell_cmd_run(&cmd, "Kbuild install"); } void eos_backend_kbuild_init(EosBackend *b) { diff --git a/core/eos/backends/src/make_backend.c b/core/eos/backends/src/make_backend.c index 24e26e60..b3d9ccc7 100644 --- a/core/eos/backends/src/make_backend.c +++ b/core/eos/backends/src/make_backend.c @@ -3,71 +3,70 @@ // ISO/IEC 25000 | ISO/IEC/IEEE 15288:2023 #include "eos/backend.h" +#include "eos/shell_cmd.h" #include "eos/log.h" #include -#include #include static EosResult make_configure(EosBackend *self, const char *src_dir, - const char *build_dir, const char *toolchain_file, - const EosKeyValue *options, int option_count) { - (void)self; - (void)build_dir; - (void)toolchain_file; - (void)options; - (void)option_count; + const char *build_dir, const char *toolchain_file, + const EosKeyValue *options, int option_count) { + (void)self; (void)build_dir; (void)options; (void)option_count; /* Check for configure script (autotools-style) */ char configure_path[EOS_MAX_PATH]; snprintf(configure_path, sizeof(configure_path), "%s/configure", src_dir); FILE *fp = fopen(configure_path, "r"); - if (fp) { - fclose(fp); - char cmd[2048]; - int offset = snprintf(cmd, sizeof(cmd), "cd \"%s\" && ./configure", src_dir); - - if (toolchain_file && toolchain_file[0]) { - offset += snprintf(cmd + offset, sizeof(cmd) - (size_t)offset, - " --host=%s", toolchain_file); - } - - EOS_INFO("Make configure: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + if (!fp) { + EOS_DEBUG("Make: no configure script found, skipping configure"); + return EOS_OK; } + fclose(fp); - EOS_DEBUG("Make: no configure script found, skipping configure"); - return EOS_OK; + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "cd "); + eos_shell_cmd_arg(&cmd, src_dir); + eos_shell_cmd_text(&cmd, " && ./configure"); + if (toolchain_file && toolchain_file[0]) { + eos_shell_cmd_text(&cmd, " --host="); + eos_shell_cmd_arg(&cmd, toolchain_file); + } + return eos_shell_cmd_run(&cmd, "Make configure"); } static EosResult make_build(EosBackend *self, const char *build_dir, int jobs) { (void)self; - char cmd[1024]; - snprintf(cmd, sizeof(cmd), "make -C \"%s\" -j%d", build_dir, jobs > 0 ? jobs : 4); - EOS_INFO("Make build: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "make -C "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, " -j"); + eos_shell_cmd_int(&cmd, jobs > 0 ? jobs : 4); + return eos_shell_cmd_run(&cmd, "Make build"); } -static EosResult make_install(EosBackend *self, const char *build_dir, - const char *install_dir) { +static EosResult make_clean(EosBackend *self, const char *build_dir) { (void)self; - char cmd[1024]; - snprintf(cmd, sizeof(cmd), "make -C \"%s\" install DESTDIR=\"%s\"", - build_dir, install_dir); - EOS_INFO("Make install: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "make -C "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, " clean"); + return eos_shell_cmd_run(&cmd, "Make clean"); } -static EosResult make_clean(EosBackend *self, const char *build_dir) { +static EosResult make_install(EosBackend *self, const char *build_dir, + const char *install_dir) { (void)self; - char cmd[1024]; - snprintf(cmd, sizeof(cmd), "make -C \"%s\" clean", build_dir); - EOS_INFO("Make clean: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "make -C "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, " install DESTDIR="); + eos_shell_cmd_arg(&cmd, install_dir); + return eos_shell_cmd_run(&cmd, "Make install"); } void eos_backend_make_init(EosBackend *b) { diff --git a/core/eos/backends/src/meson_backend.c b/core/eos/backends/src/meson_backend.c index 62d463ca..5dcf458c 100644 --- a/core/eos/backends/src/meson_backend.c +++ b/core/eos/backends/src/meson_backend.c @@ -3,61 +3,73 @@ // ISO/IEC 25000 | ISO/IEC/IEEE 15288:2023 #include "eos/backend.h" -#include "eos/log.h" +#include "eos/shell_cmd.h" #include -#include #include +/* -D= for every option; the key unquoted as one word, the value + * quoted. A value with spaces used to split into two arguments. */ +static void append_defines(EosShellCmd *cmd, const EosKeyValue *options, + int option_count, const char *skip_key) { + for (int i = 0; i < option_count; i++) { + if (skip_key && strcmp(options[i].key, skip_key) == 0) continue; + eos_shell_cmd_text(cmd, " -D"); + eos_shell_cmd_word(cmd, options[i].key); + eos_shell_cmd_text(cmd, "="); + eos_shell_cmd_arg(cmd, options[i].value); + } +} + static EosResult meson_configure(EosBackend *self, const char *src_dir, const char *build_dir, const char *toolchain_file, const EosKeyValue *options, int option_count) { (void)self; - char cmd[2048]; - int offset = snprintf(cmd, sizeof(cmd), "meson setup \"%s\" \"%s\"", - build_dir, src_dir); - + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "meson setup "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, " "); + eos_shell_cmd_arg(&cmd, src_dir); if (toolchain_file && toolchain_file[0]) { - offset += snprintf(cmd + offset, sizeof(cmd) - (size_t)offset, - " --cross-file \"%s\"", toolchain_file); + eos_shell_cmd_text(&cmd, " --cross-file "); + eos_shell_cmd_arg(&cmd, toolchain_file); } - - for (int i = 0; i < option_count && offset < (int)sizeof(cmd) - 64; i++) { - offset += snprintf(cmd + offset, sizeof(cmd) - (size_t)offset, - " -D%s=%s", options[i].key, options[i].value); - } - - EOS_INFO("Meson configure: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + append_defines(&cmd, options, option_count, NULL); + return eos_shell_cmd_run(&cmd, "Meson configure"); } static EosResult meson_build(EosBackend *self, const char *build_dir, int jobs) { (void)self; - char cmd[1024]; - snprintf(cmd, sizeof(cmd), "ninja -C \"%s\" -j %d", build_dir, jobs > 0 ? jobs : 4); - EOS_INFO("Meson build: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "ninja -C "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, " -j "); + eos_shell_cmd_int(&cmd, jobs > 0 ? jobs : 4); + return eos_shell_cmd_run(&cmd, "Meson build"); } static EosResult meson_install(EosBackend *self, const char *build_dir, const char *install_dir) { (void)self; - char cmd[1024]; - snprintf(cmd, sizeof(cmd), "DESTDIR=\"%s\" ninja -C \"%s\" install", - install_dir, build_dir); - EOS_INFO("Meson install: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "DESTDIR="); + eos_shell_cmd_arg(&cmd, install_dir); + eos_shell_cmd_text(&cmd, " ninja -C "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, " install"); + return eos_shell_cmd_run(&cmd, "Meson install"); } static EosResult meson_clean(EosBackend *self, const char *build_dir) { (void)self; - char cmd[1024]; - snprintf(cmd, sizeof(cmd), "ninja -C \"%s\" -t clean", build_dir); - EOS_INFO("Meson clean: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "ninja -C "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, " -t clean"); + return eos_shell_cmd_run(&cmd, "Meson clean"); } void eos_backend_meson_init(EosBackend *b) { diff --git a/core/eos/backends/src/ninja_backend.c b/core/eos/backends/src/ninja_backend.c index 40bde9f3..e3920102 100644 --- a/core/eos/backends/src/ninja_backend.c +++ b/core/eos/backends/src/ninja_backend.c @@ -3,51 +3,49 @@ // ISO/IEC 25000 | ISO/IEC/IEEE 15288:2023 #include "eos/backend.h" -#include "eos/log.h" +#include "eos/shell_cmd.h" #include -#include #include static EosResult ninja_configure(EosBackend *self, const char *src_dir, - const char *build_dir, const char *toolchain_file, - const EosKeyValue *options, int option_count) { - (void)self; - (void)src_dir; - (void)build_dir; - (void)toolchain_file; - (void)options; - (void)option_count; - EOS_DEBUG("Ninja: no separate configure step (expects build.ninja in place)"); + const char *build_dir, const char *toolchain_file, + const EosKeyValue *options, int option_count) { + (void)self; (void)src_dir; (void)build_dir; (void)toolchain_file; + (void)options; (void)option_count; + /* Ninja consumes a build.ninja that something else generated. */ return EOS_OK; } static EosResult ninja_build(EosBackend *self, const char *build_dir, int jobs) { (void)self; - char cmd[1024]; - snprintf(cmd, sizeof(cmd), "ninja -C \"%s\" -j %d", build_dir, jobs > 0 ? jobs : 4); - EOS_INFO("Ninja build: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "ninja -C "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, " -j "); + eos_shell_cmd_int(&cmd, jobs > 0 ? jobs : 4); + return eos_shell_cmd_run(&cmd, "Ninja build"); } static EosResult ninja_install(EosBackend *self, const char *build_dir, - const char *install_dir) { - (void)self; - (void)install_dir; - char cmd[1024]; - snprintf(cmd, sizeof(cmd), "ninja -C \"%s\" install", build_dir); - EOS_INFO("Ninja install: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + const char *install_dir) { + (void)self; (void)install_dir; + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "ninja -C "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, " install"); + return eos_shell_cmd_run(&cmd, "Ninja install"); } static EosResult ninja_clean(EosBackend *self, const char *build_dir) { (void)self; - char cmd[1024]; - snprintf(cmd, sizeof(cmd), "ninja -C \"%s\" -t clean", build_dir); - EOS_INFO("Ninja clean: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "ninja -C "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, " -t clean"); + return eos_shell_cmd_run(&cmd, "Ninja clean"); } void eos_backend_ninja_init(EosBackend *b) { diff --git a/core/eos/backends/src/nuttx_backend.c b/core/eos/backends/src/nuttx_backend.c index 1fccdb74..1953bd0f 100644 --- a/core/eos/backends/src/nuttx_backend.c +++ b/core/eos/backends/src/nuttx_backend.c @@ -3,18 +3,14 @@ // ISO/IEC 25000 | ISO/IEC/IEEE 15288:2023 #include "eos/backend.h" -#include "eos/log.h" +#include "eos/shell_cmd.h" #include -#include #include static EosResult nuttx_configure(EosBackend *self, const char *src_dir, const char *build_dir, const char *toolchain_file, const EosKeyValue *options, int option_count) { - (void)self; - (void)build_dir; - char cmd[2048]; - + (void)self; (void)build_dir; /* NuttX uses its own tools/configure.sh : flow */ const char *board_config = "sim:nsh"; for (int i = 0; i < option_count; i++) { @@ -24,56 +20,77 @@ static EosResult nuttx_configure(EosBackend *self, const char *src_dir, } } + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "cd "); + eos_shell_cmd_arg(&cmd, src_dir); + eos_shell_cmd_text(&cmd, " && "); if (toolchain_file && toolchain_file[0]) { - snprintf(cmd, sizeof(cmd), - "cd \"%s\" && CROSS_COMPILE=%s- ./tools/configure.sh %s", - src_dir, toolchain_file, board_config); - } else { - snprintf(cmd, sizeof(cmd), - "cd \"%s\" && ./tools/configure.sh %s", - src_dir, board_config); + eos_shell_cmd_text(&cmd, "CROSS_COMPILE="); + eos_shell_cmd_word(&cmd, toolchain_file); + eos_shell_cmd_text(&cmd, "- "); } - - EOS_INFO("NuttX configure: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + eos_shell_cmd_text(&cmd, "./tools/configure.sh "); + eos_shell_cmd_word(&cmd, board_config); + return eos_shell_cmd_run(&cmd, "NuttX configure"); } static EosResult nuttx_build(EosBackend *self, const char *build_dir, int jobs) { (void)self; - char cmd[1024]; - snprintf(cmd, sizeof(cmd), "make -C \"%s\" -j%d", build_dir, jobs > 0 ? jobs : 4); - EOS_INFO("NuttX build: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "make -C "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, " -j"); + eos_shell_cmd_int(&cmd, jobs > 0 ? jobs : 4); + return eos_shell_cmd_run(&cmd, "NuttX build"); +} + +static EosResult nuttx_clean(EosBackend *self, const char *build_dir) { + (void)self; + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "make -C "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, " distclean"); + return eos_shell_cmd_run(&cmd, "NuttX clean"); } static EosResult nuttx_install(EosBackend *self, const char *build_dir, const char *install_dir) { (void)self; - char cmd[1024]; - snprintf(cmd, sizeof(cmd), + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); #ifdef _WIN32 - "if not exist \"%s\" mkdir \"%s\" && copy /Y \"%s\\nuttx.bin\" \"%s\\firmware.bin\" 2>nul", - install_dir, install_dir, build_dir, install_dir + eos_shell_cmd_text(&cmd, "if not exist "); + eos_shell_cmd_arg(&cmd, install_dir); + eos_shell_cmd_text(&cmd, " mkdir "); + eos_shell_cmd_arg(&cmd, install_dir); + eos_shell_cmd_text(&cmd, " && copy /Y "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, "\\nuttx.bin "); + eos_shell_cmd_arg(&cmd, install_dir); + eos_shell_cmd_text(&cmd, "\\firmware.bin 2>nul"); #else - "mkdir -p \"%s\" && cp -f \"%s/nuttx.bin\" \"%s/firmware.bin\" 2>/dev/null || " - "cp -f \"%s/nuttx\" \"%s/firmware.elf\" 2>/dev/null || true", - install_dir, build_dir, install_dir, build_dir, install_dir + eos_shell_cmd_text(&cmd, "mkdir -p "); + eos_shell_cmd_arg(&cmd, install_dir); + eos_shell_cmd_text(&cmd, " && if [ -f "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, "/nuttx.bin ]; then cp -f "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, "/nuttx.bin "); + eos_shell_cmd_arg(&cmd, install_dir); + eos_shell_cmd_text(&cmd, "/firmware.bin; elif [ -f "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, "/nuttx ]; then cp -f "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, "/nuttx "); + eos_shell_cmd_arg(&cmd, install_dir); + eos_shell_cmd_text(&cmd, "/firmware.elf; else echo \"nuttx_install: no nuttx.bin or nuttx image in\" "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, " >&2; exit 1; fi"); #endif - ); - EOS_INFO("NuttX install: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; -} - -static EosResult nuttx_clean(EosBackend *self, const char *build_dir) { - (void)self; - char cmd[1024]; - snprintf(cmd, sizeof(cmd), "make -C \"%s\" distclean", build_dir); - EOS_INFO("NuttX clean: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + return eos_shell_cmd_run(&cmd, "NuttX install"); } void eos_backend_nuttx_init(EosBackend *b) { diff --git a/core/eos/backends/src/zephyr_backend.c b/core/eos/backends/src/zephyr_backend.c index ed6aee18..456d1aa9 100644 --- a/core/eos/backends/src/zephyr_backend.c +++ b/core/eos/backends/src/zephyr_backend.c @@ -3,18 +3,27 @@ // ISO/IEC 25000 | ISO/IEC/IEEE 15288:2023 #include "eos/backend.h" -#include "eos/log.h" +#include "eos/shell_cmd.h" #include -#include #include +/* -D= for every option; the key unquoted as one word, the value + * quoted. A value with spaces used to split into two arguments. */ +static void append_defines(EosShellCmd *cmd, const EosKeyValue *options, + int option_count, const char *skip_key) { + for (int i = 0; i < option_count; i++) { + if (skip_key && strcmp(options[i].key, skip_key) == 0) continue; + eos_shell_cmd_text(cmd, " -D"); + eos_shell_cmd_word(cmd, options[i].key); + eos_shell_cmd_text(cmd, "="); + eos_shell_cmd_arg(cmd, options[i].value); + } +} + static EosResult zephyr_configure(EosBackend *self, const char *src_dir, const char *build_dir, const char *toolchain_file, const EosKeyValue *options, int option_count) { (void)self; - char cmd[2048]; - int offset = 0; - /* Zephyr uses west + CMake under the hood */ const char *board = NULL; for (int i = 0; i < option_count; i++) { @@ -24,81 +33,95 @@ static EosResult zephyr_configure(EosBackend *self, const char *src_dir, } } + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); if (board) { - offset = snprintf(cmd, sizeof(cmd), - "west build -b %s -d \"%s\" \"%s\"", - board, build_dir, src_dir); + eos_shell_cmd_text(&cmd, "west build -b "); + eos_shell_cmd_word(&cmd, board); + eos_shell_cmd_text(&cmd, " -d "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, " "); + eos_shell_cmd_arg(&cmd, src_dir); } else { - offset = snprintf(cmd, sizeof(cmd), - "cmake -S \"%s\" -B \"%s\" -G Ninja -DBOARD=native_posix", - src_dir, build_dir); + eos_shell_cmd_text(&cmd, "cmake -S "); + eos_shell_cmd_arg(&cmd, src_dir); + eos_shell_cmd_text(&cmd, " -B "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, " -G Ninja -DBOARD=native_posix"); } - if (toolchain_file && toolchain_file[0]) { - offset += snprintf(cmd + offset, sizeof(cmd) - (size_t)offset, - " -DCMAKE_TOOLCHAIN_FILE=\"%s\"", toolchain_file); - } - - for (int i = 0; i < option_count && offset < (int)sizeof(cmd) - 64; i++) { - if (strcmp(options[i].key, "board") == 0) continue; - offset += snprintf(cmd + offset, sizeof(cmd) - (size_t)offset, - " -D%s=%s", options[i].key, options[i].value); + eos_shell_cmd_text(&cmd, " -DCMAKE_TOOLCHAIN_FILE="); + eos_shell_cmd_arg(&cmd, toolchain_file); } - - EOS_INFO("Zephyr configure: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + append_defines(&cmd, options, option_count, "board"); + return eos_shell_cmd_run(&cmd, "Zephyr configure"); } static EosResult zephyr_build(EosBackend *self, const char *build_dir, int jobs) { (void)self; - char cmd[1024]; - snprintf(cmd, sizeof(cmd), "west build -d \"%s\" -- -j%d", - build_dir, jobs > 0 ? jobs : 4); - EOS_INFO("Zephyr build: %s", cmd); - int rc = system(cmd); - - if (rc != 0) { - snprintf(cmd, sizeof(cmd), "cmake --build \"%s\" -j %d", - build_dir, jobs > 0 ? jobs : 4); - EOS_INFO("Zephyr fallback build: %s", cmd); - rc = system(cmd); - } + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "west build -d "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, " -- -j"); + eos_shell_cmd_int(&cmd, jobs > 0 ? jobs : 4); + EosResult res = eos_shell_cmd_run(&cmd, "Zephyr build"); + if (res != EOS_ERR_BUILD) return res; /* ran and succeeded, or was refused */ - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + /* west is not there or failed: the build directory is a CMake tree too. */ + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "cmake --build "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, " -j "); + eos_shell_cmd_int(&cmd, jobs > 0 ? jobs : 4); + return eos_shell_cmd_run(&cmd, "Zephyr build (cmake)"); } static EosResult zephyr_install(EosBackend *self, const char *build_dir, const char *install_dir) { (void)self; - char cmd[1024]; - /* Copy firmware outputs to install dir */ - snprintf(cmd, sizeof(cmd), + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); #ifdef _WIN32 - "if not exist \"%s\" mkdir \"%s\" && copy /Y \"%s\\zephyr\\zephyr.bin\" \"%s\\firmware.bin\"", - install_dir, install_dir, build_dir, install_dir + eos_shell_cmd_text(&cmd, "if not exist "); + eos_shell_cmd_arg(&cmd, install_dir); + eos_shell_cmd_text(&cmd, " mkdir "); + eos_shell_cmd_arg(&cmd, install_dir); + eos_shell_cmd_text(&cmd, " && copy /Y "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, "\\zephyr\\zephyr.bin "); + eos_shell_cmd_arg(&cmd, install_dir); + eos_shell_cmd_text(&cmd, "\\firmware.bin"); #else - "mkdir -p \"%s\" && cp -f \"%s/zephyr/zephyr.bin\" \"%s/firmware.bin\" 2>/dev/null || " - "cp -f \"%s/zephyr/zephyr.elf\" \"%s/firmware.elf\" 2>/dev/null || true", - install_dir, build_dir, install_dir, build_dir, install_dir + eos_shell_cmd_text(&cmd, "mkdir -p "); + eos_shell_cmd_arg(&cmd, install_dir); + eos_shell_cmd_text(&cmd, " && if [ -f "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, "/zephyr/zephyr.bin ]; then cp -f "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, "/zephyr/zephyr.bin "); + eos_shell_cmd_arg(&cmd, install_dir); + eos_shell_cmd_text(&cmd, "/firmware.bin; elif [ -f "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, "/zephyr/zephyr.elf ]; then cp -f "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, "/zephyr/zephyr.elf "); + eos_shell_cmd_arg(&cmd, install_dir); + eos_shell_cmd_text(&cmd, "/firmware.elf; else echo \"zephyr_install: no zephyr.bin or zephyr.elf in\" "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, "/zephyr >&2; exit 1; fi"); #endif - ); - EOS_INFO("Zephyr install: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + return eos_shell_cmd_run(&cmd, "Zephyr install"); } static EosResult zephyr_clean(EosBackend *self, const char *build_dir) { (void)self; - char cmd[1024]; -#ifdef _WIN32 - snprintf(cmd, sizeof(cmd), "if exist \"%s\" rmdir /s /q \"%s\"", build_dir, build_dir); -#else - snprintf(cmd, sizeof(cmd), "rm -rf \"%s\"", build_dir); -#endif - EOS_INFO("Zephyr clean: %s", cmd); - int rc = system(cmd); - return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; + EosShellCmd cmd; + eos_shell_cmd_init(&cmd); + eos_shell_cmd_text(&cmd, "cmake --build "); + eos_shell_cmd_arg(&cmd, build_dir); + eos_shell_cmd_text(&cmd, " --target clean"); + return eos_shell_cmd_run(&cmd, "Zephyr clean"); } void eos_backend_zephyr_init(EosBackend *b) { diff --git a/core/eos/cmd/eos/CMakeLists.txt b/core/eos/cmd/eos/CMakeLists.txt index 19401c90..8ce757c2 100644 --- a/core/eos/cmd/eos/CMakeLists.txt +++ b/core/eos/cmd/eos/CMakeLists.txt @@ -1,4 +1,4 @@ -add_executable(eos main.c) +add_executable(eos main.c exec_argv.c) target_link_libraries(eos PRIVATE eos_core diff --git a/core/eos/core/CMakeLists.txt b/core/eos/core/CMakeLists.txt index 440759a3..82070f0f 100644 --- a/core/eos/core/CMakeLists.txt +++ b/core/eos/core/CMakeLists.txt @@ -6,6 +6,7 @@ add_library(eos_core STATIC src/lockfile.c src/layer.c src/scheduler.c + src/shell_cmd.c ) target_include_directories(eos_core PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include diff --git a/core/eos/core/include/eos/shell_cmd.h b/core/eos/core/include/eos/shell_cmd.h new file mode 100644 index 00000000..d577198a --- /dev/null +++ b/core/eos/core/include/eos/shell_cmd.h @@ -0,0 +1,125 @@ +// SPDX-License-Identifier: MIT +// Copyright (c) 2026 EoS Project +// ISO/IEC 25000 | ISO/IEC/IEEE 15288:2023 + +/** + * @file shell_cmd.h + * @brief A shell command built from configuration, checked before it runs. + * + * The build backends and the Linux security service hand commands to + * system(). The text of those commands is written in this tree, but the + * paths, targets and option values in them come from a project's + * configuration. Two things went wrong with that before this file existed: + * a value carrying shell metacharacters ran as shell, and a command that did + * not fit its buffer ran truncated. Both are refused here, in one place. + * + * Usage: + * + * EosShellCmd cmd; + * eos_shell_cmd_init(&cmd); + * eos_shell_cmd_text(&cmd, "cmake -S "); + * eos_shell_cmd_arg(&cmd, src_dir); // double-quoted, checked + * eos_shell_cmd_text(&cmd, " -B "); + * eos_shell_cmd_arg(&cmd, build_dir); + * return eos_shell_cmd_run(&cmd, "CMake configure"); + * + * eos_shell_cmd_text() is for program text written in this tree only. + * Everything that originates in configuration goes through + * eos_shell_cmd_arg() (quoted) or eos_shell_cmd_word() (unquoted, where the + * program treats the token as one word). A refused value or an overflow + * marks the command faulty; every later append is a no-op and + * eos_shell_cmd_run() returns EOS_ERR_INVALID without running anything. + */ + +#ifndef EOS_SHELL_CMD_H +#define EOS_SHELL_CMD_H + +#include +#include "eos/error.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** Longest command the builder holds, terminator included. */ +#define EOS_SHELL_CMD_MAX 4096 + +typedef struct { + char buf[EOS_SHELL_CMD_MAX]; + size_t len; + int fault; /**< non-zero once something was refused or did not fit */ +} EosShellCmd; + +/** @brief Start an empty command. */ +void eos_shell_cmd_init(EosShellCmd *cmd); + +/** + * @brief Append program text written in this tree. + * + * Not for configuration values: nothing is checked or quoted. + */ +void eos_shell_cmd_text(EosShellCmd *cmd, const char *text); + +/** @brief Append a decimal integer (a job count, a port). */ +void eos_shell_cmd_int(EosShellCmd *cmd, int value); + +/** + * @brief Append a configuration value as one double-quoted argument. + * + * Refused unless eos_shell_arg_is_safe() accepts it. + */ +void eos_shell_cmd_arg(EosShellCmd *cmd, const char *arg); + +/** + * @brief Append a configuration value unquoted, as one shell word. + * + * For tokens the program reads as a single word (a make target, a board + * name, a cross-compile prefix). Refused unless eos_shell_word_is_safe() + * accepts it. + */ +void eos_shell_cmd_word(EosShellCmd *cmd, const char *word); + +/** + * @brief Whether a configuration value may appear inside a double-quoted + * shell argument. + * + * Refuses NULL, control characters and DEL, and every character that a + * POSIX shell reads inside or around double quotes: ; | & > < $ ( ) " ' ` \. + * This is the rule services/linux/src/linux_security.c reviewed and shipped + * as is_path_safe(), unchanged. The empty string is accepted; an empty + * argument is "" and the program it reaches decides what that means. + * + * cmd.exe, which the Windows install commands reach, also expands %NAME% + * inside quotes. That substitutes text; it does not run any. A percent + * sign is therefore not refused, so a Windows path with one still works. + */ +int eos_shell_arg_is_safe(const char *arg); + +/** + * @brief Whether a configuration value is one shell word. + * + * Positive definition: isalnum() and . _ / + = : - and nothing else, so an + * incomplete denylist cannot be the next gap. A leading '-' is refused + * separately: it is built from allowed characters but turns a target into + * an option. The empty string is a word. + */ +int eos_shell_word_is_safe(const char *word); + +/** + * @brief Run the command through system(). + * + * @param cmd The command. A faulty one is refused with EOS_ERR_INVALID + * and logged, and nothing runs. + * @param what Short description for the log ("CMake configure"). + * @return EOS_OK when the command exited 0, EOS_ERR_BUILD when it did not, + * EOS_ERR_SYSTEM when no shell could be started or the shell could + * not run the command at all (exit 127), EOS_ERR_INVALID when the + * command was refused before anything ran. + */ +EosResult eos_shell_cmd_run(EosShellCmd *cmd, const char *what); + +#ifdef __cplusplus +} +#endif + +#endif /* EOS_SHELL_CMD_H */ diff --git a/core/eos/core/src/config.c b/core/eos/core/src/config.c index bce14518..d57d46f0 100644 --- a/core/eos/core/src/config.c +++ b/core/eos/core/src/config.c @@ -39,6 +39,11 @@ static int indent_level(const char *line) { return n / 2; } +/* A package index is only safe to dereference when a `- name:` entry claimed + * a slot for it. It is -1 before the first entry and after one is refused for + * exceeding EOS_MAX_PACKAGES, so both ends have to be checked. */ +#define PKG_IDX_VALID(i) ((i) >= 0 && (i) < EOS_MAX_PACKAGES) + static int parse_kv(const char *line, char *key, size_t ksz, char *val, size_t vsz) { const char *colon = strchr(line, ':'); if (!colon) return -1; @@ -119,7 +124,8 @@ EosResult eos_config_load(EosConfig *cfg, const char *path) { } else if (section == SEC_PACKAGES || section == SEC_PKG_ENTRY || section == SEC_PKG_BUILD || - section == SEC_PKG_OPTIONS) { + section == SEC_PKG_OPTIONS || + section == SEC_PKG_DEPS) { if (parse_kv(item, key, sizeof(key), val, sizeof(val)) == 0 && strcmp(key, "name") == 0) { pkg_idx = cfg->package_count; @@ -127,13 +133,27 @@ EosResult eos_config_load(EosConfig *cfg, const char *path) { strncpy(cfg->packages[pkg_idx].name, val, EOS_MAX_NAME - 1); cfg->package_count++; section = SEC_PKG_ENTRY; + } else { + /* Bug fix: EOS_MAX_PACKAGES exceeded. pkg_idx is now + * out of range for cfg->packages[]. Fall back to the + * neutral SEC_PACKAGES state so the "key: val" lines + * that follow (belonging to this rejected package) + * are NOT dispatched into SEC_PKG_ENTRY/SEC_PKG_BUILD/ + * SEC_PKG_OPTIONS below, which would otherwise index + * cfg->packages[pkg_idx] out of bounds -- previously + * this silently corrupted memory adjacent to the + * packages[] array (package_count and beyond). */ + EOS_ERROR("Too many packages in config (max %d); ignoring '%s'", + EOS_MAX_PACKAGES, val); + section = SEC_PACKAGES; + } + } else if (section == SEC_PKG_DEPS && + pkg_idx >= 0 && pkg_idx < EOS_MAX_PACKAGES) { + if (cfg->packages[pkg_idx].dep_count < EOS_MAX_DEPS) { + strncpy(cfg->packages[pkg_idx].deps[cfg->packages[pkg_idx].dep_count], + item, EOS_MAX_NAME - 1); + cfg->packages[pkg_idx].dep_count++; } - } - } else if (section == SEC_PKG_DEPS && pkg_idx >= 0) { - if (cfg->packages[pkg_idx].dep_count < EOS_MAX_DEPS) { - strncpy(cfg->packages[pkg_idx].deps[cfg->packages[pkg_idx].dep_count], - item, EOS_MAX_NAME - 1); - cfg->packages[pkg_idx].dep_count++; } } else if (section == SEC_SYSTEM_RTOS || section == SEC_SYSTEM_RTOS_ENTRY) { /* "- provider: freertos" starts a new RTOS entry */ @@ -163,6 +183,43 @@ EosResult eos_config_load(EosConfig *cfg, const char *path) { if (strcmp(key, "docs") == 0) { section = SEC_DOCS; continue; } } + /* A key at system's own child indent belongs to system, not to + * whichever sub-section was descended into last. Without this, + * `system.linux` swallowed the sibling `rtos:` in every hybrid + * config: SEC_SYSTEM_LINUX handles only `provider`, `kernel` and + * `rootfs`, so the RTOS list was never entered, rtos_count stayed 0, + * and a hybrid build produced no RTOS firmware without saying so. + * SEC_SYSTEM_KERNEL already popped back for one specific sibling; + * this does it for all of them, which is what the indentation means. + * + * The guard is structural rather than a list of the seven keys that + * `case SEC_SYSTEM:` happens to handle today. A key list here is a + * second authority on what a system child key is, kept in step with + * the switch below by hand and by nothing else -- add an eighth key + * there and the swallow returns for it, silently, with no test that + * fails. Every descendant of a system sub-section sits at indent >= 2 + * (system.linux.provider at 2, rtos entry keys at 3) and list items + * `continue` before reaching here, so indent <= 1 is exactly the + * "back at system's own child level" condition. */ + if (indent <= 1 && + (section == SEC_SYSTEM_LINUX || section == SEC_SYSTEM_KERNEL || + section == SEC_SYSTEM_ROOTFS || section == SEC_SYSTEM_RTOS || + section == SEC_SYSTEM_RTOS_ENTRY)) { + section = SEC_SYSTEM; + } + + /* Same shape, one section up. SEC_TOOLCHAIN_LINUX popped back for + * exactly one sibling and SEC_TOOLCHAIN_RTOS for none, so a `target:` + * written at toolchain's own indent after the linux:/rtos: blocks + * landed in toolchain.rtos_target instead of toolchain.target -- + * silently, exactly as rtos_count stayed 0. No config under examples/ + * is ordered that way today, which is the only reason it has not + * bitten. */ + if (indent <= 1 && + (section == SEC_TOOLCHAIN_LINUX || section == SEC_TOOLCHAIN_RTOS)) { + section = SEC_TOOLCHAIN; + } + /* sub-sections */ switch (section) { case SEC_PROJECT: @@ -181,7 +238,6 @@ EosResult eos_config_load(EosConfig *cfg, const char *path) { break; case SEC_TOOLCHAIN_LINUX: if (strcmp(key, "target") == 0) strncpy(cfg->toolchain.target, val, EOS_MAX_NAME - 1); - if (indent <= 1 && strcmp(key, "rtos") == 0) { section = SEC_TOOLCHAIN_RTOS; continue; } break; case SEC_TOOLCHAIN_RTOS: if (strcmp(key, "target") == 0) strncpy(cfg->toolchain.rtos_target, val, EOS_MAX_NAME - 1); @@ -244,6 +300,10 @@ EosResult eos_config_load(EosConfig *cfg, const char *path) { } break; case SEC_PKG_ENTRY: + /* Defense in depth: pkg_idx should always be in range here given + * the SEC_PACKAGES fallback above, but bound it explicitly since + * this indexes cfg->packages[] directly. */ + if (pkg_idx < 0 || pkg_idx >= EOS_MAX_PACKAGES) break; if (strcmp(key, "version") == 0) strncpy(cfg->packages[pkg_idx].version, val, EOS_MAX_NAME - 1); if (strcmp(key, "source") == 0) strncpy(cfg->packages[pkg_idx].source, val, EOS_MAX_URL - 1); if (strcmp(key, "hash") == 0) strncpy(cfg->packages[pkg_idx].hash, val, EOS_HASH_LEN - 1); @@ -251,17 +311,20 @@ EosResult eos_config_load(EosConfig *cfg, const char *path) { if (strcmp(key, "deps") == 0) { section = SEC_PKG_DEPS; continue; } break; case SEC_PKG_BUILD: + if (pkg_idx < 0 || pkg_idx >= EOS_MAX_PACKAGES) break; if (strcmp(key, "type") == 0) { cfg->packages[pkg_idx].build_type = eos_build_type_from_str(val); } if (indent <= 2 && strcmp(key, "options") == 0) { section = SEC_PKG_OPTIONS; continue; } + if (indent <= 2 && strcmp(key, "deps") == 0) { section = SEC_PKG_DEPS; continue; } if (indent <= 2 && strcmp(key, "version") == 0) { section = SEC_PKG_ENTRY; strncpy(cfg->packages[pkg_idx].version, val, EOS_MAX_NAME - 1); } break; case SEC_PKG_OPTIONS: - if (pkg_idx >= 0 && cfg->packages[pkg_idx].option_count < EOS_MAX_OPTIONS) { + if (pkg_idx >= 0 && pkg_idx < EOS_MAX_PACKAGES && + cfg->packages[pkg_idx].option_count < EOS_MAX_OPTIONS) { int oi = cfg->packages[pkg_idx].option_count; strncpy(cfg->packages[pkg_idx].options[oi].key, key, EOS_MAX_NAME - 1); strncpy(cfg->packages[pkg_idx].options[oi].value, val, EOS_MAX_PATH - 1); diff --git a/core/eos/core/src/lockfile.c b/core/eos/core/src/lockfile.c index 50a9f98c..9b663af9 100644 --- a/core/eos/core/src/lockfile.c +++ b/core/eos/core/src/lockfile.c @@ -9,6 +9,20 @@ #include #include +static void package_hash(const EosPackageConfig *pc, char *hash) { + if (pc->hash[0]) { + strncpy(hash, pc->hash, EOS_HASH_LEN - 1); + hash[EOS_HASH_LEN - 1] = '\0'; + return; + } + + char input[2048]; + snprintf(input, sizeof(input), "%s:%s:%s:%s", + pc->name, pc->version, pc->source, + eos_build_type_str(pc->build_type)); + eos_cache_compute_hash(input, strlen(input), hash, EOS_HASH_LEN); +} + EosResult eos_lockfile_generate(EosLockfile *lock, const EosConfig *cfg) { memset(lock, 0, sizeof(*lock)); strncpy(lock->project_name, cfg->project.name, EOS_MAX_NAME - 1); @@ -22,17 +36,8 @@ EosResult eos_lockfile_generate(EosLockfile *lock, const EosConfig *cfg) { strncpy(entry->version, pc->version, EOS_MAX_NAME - 1); strncpy(entry->resolved_version, pc->version, EOS_MAX_NAME - 1); strncpy(entry->source, pc->source, EOS_MAX_URL - 1); - strncpy(entry->hash, pc->hash, EOS_HASH_LEN - 1); entry->build_type = pc->build_type; - - /* If no hash provided, compute one from metadata */ - if (!entry->hash[0]) { - char input[2048]; - snprintf(input, sizeof(input), "%s:%s:%s:%s", - entry->name, entry->version, - entry->source, eos_build_type_str(entry->build_type)); - eos_cache_compute_hash(input, strlen(input), entry->hash, EOS_HASH_LEN); - } + package_hash(pc, entry->hash); lock->count++; } @@ -138,13 +143,26 @@ EosResult eos_lockfile_load(EosLockfile *lock, const char *path) { int eos_lockfile_is_current(const EosLockfile *lock, const EosConfig *cfg) { if (strcmp(lock->project_name, cfg->project.name) != 0) return 0; + if (strcmp(lock->project_version, cfg->project.version) != 0) return 0; if (lock->count != cfg->package_count) return 0; + int matched[EOS_MAX_PACKAGES] = {0}; for (int i = 0; i < cfg->package_count; i++) { + const EosPackageConfig *pc = &cfg->packages[i]; + char expected_hash[EOS_HASH_LEN] = {0}; + package_hash(pc, expected_hash); + int found = 0; for (int j = 0; j < lock->count; j++) { - if (strcmp(cfg->packages[i].name, lock->entries[j].name) == 0 && - strcmp(cfg->packages[i].version, lock->entries[j].version) == 0) { + const EosLockEntry *entry = &lock->entries[j]; + if (!matched[j] && + strcmp(pc->name, entry->name) == 0 && + strcmp(pc->version, entry->version) == 0 && + strcmp(pc->version, entry->resolved_version) == 0 && + strcmp(pc->source, entry->source) == 0 && + strcmp(expected_hash, entry->hash) == 0 && + pc->build_type == entry->build_type) { + matched[j] = 1; found = 1; break; } diff --git a/core/eos/core/src/log.c b/core/eos/core/src/log.c index 2c573899..31194c8e 100644 --- a/core/eos/core/src/log.c +++ b/core/eos/core/src/log.c @@ -5,11 +5,12 @@ #include "eos/log.h" #include #include +#include #include #include #ifdef _WIN32 -#include +#include #endif static EosLogLevel g_log_level = EOS_LOG_INFO; @@ -106,13 +107,23 @@ static const char *color_reset(void) { } static uint32_t get_timestamp_ms(void) { -#ifdef _WIN32 +#if defined(_WIN32) return (uint32_t)GetTickCount(); -#else +#elif defined(CLOCK_MONOTONIC) struct timespec ts; if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) return (uint32_t)(ts.tv_sec * 1000 + ts.tv_nsec / 1000000); return 0; +#else + /* Bare-metal newlib (arm-none-eabi and friends) declares neither + * clock_gettime nor CLOCK_MONOTONIC, so the POSIX branch does not + * compile for the Cortex-M targets this project exists to build for. + * + * There is a timebase on a target -- eos_get_tick_ms() in the HAL -- but + * core/ sits below hal/ and must not depend upward, so log entries carry + * a zero timestamp until a port supplies one. Ordering within the ring + * buffer is preserved regardless; only the absolute time is missing. */ + return 0; #endif } @@ -140,7 +151,7 @@ void eos_log_ring_dump(void) { for (int i = 0; i < g_ring_count; i++) { const EosLogEntry *e = eos_log_ring_get(i); if (e) { - fprintf(stderr, "[%u %5s] %s%s%s\n", + fprintf(stderr, "[%" PRIu32 " %5s] %s%s%s\n", e->timestamp_ms, level_str(e->level), e->module ? e->module : "", e->module ? ": " : "", e->message); @@ -240,4 +251,4 @@ emit_log:; va_end(args); emit(level, module, buf); -} \ No newline at end of file +} diff --git a/core/eos/core/src/scheduler.c b/core/eos/core/src/scheduler.c index 33146718..2911acbc 100644 --- a/core/eos/core/src/scheduler.c +++ b/core/eos/core/src/scheduler.c @@ -35,20 +35,54 @@ static const EosPackage *find_package_by_name(const EosPackageSet *pkgs, return NULL; } +/** + * Append to a fixed buffer and return the new length, never past the end. + * + * snprintf() returns the length it *would* have written, not what it wrote. + * Accumulating that return value walks the offset past the end of the buffer + * as soon as any one append truncates; the next call then gets a pointer past + * the end and a size of `sizeof(buf) - len`, which underflows size_t to a + * huge value. This clamps to what is actually in the buffer instead. + */ +static size_t append_clamped(char *buf, size_t cap, size_t len, + const char *fmt, const char *a, const char *b) { + if (len >= cap - 1u) return cap - 1u; + int n = b ? snprintf(buf + len, cap - len, fmt, a, b) + : snprintf(buf + len, cap - len, fmt, a); + if (n < 0) return len; + return ((size_t)n >= cap - len) ? cap - 1u : len + (size_t)n; +} + static void compute_cache_key(const EosPackage *pkg, const char *toolchain, char *key, size_t key_sz) { char input[2048]; - int len = snprintf(input, sizeof(input), "%s:%s:%s:%s", - pkg->name, pkg->version, - eos_build_type_str(pkg->build_type), - toolchain ? toolchain : "host"); - for (int i = 0; i < pkg->option_count && len < (int)sizeof(input) - 128; i++) { - len += snprintf(input + len, sizeof(input) - (size_t)len, - ":%s=%s", pkg->options[i].key, pkg->options[i].value); + int n = snprintf(input, sizeof(input), "%s:%s:%s:%s", + pkg->name, pkg->version, + eos_build_type_str(pkg->build_type), + toolchain ? toolchain : "host"); + size_t len = (n < 0) ? 0u + : ((size_t)n >= sizeof(input) ? sizeof(input) - 1u : (size_t)n); + + /* Reserve room for the content hash so options can never crowd it out. + * The hash is what identifies the source; dropping it while keeping the + * options would let two different sources share a cache key. The old + * `len < sizeof(input) - 128` guard was reaching for this, but it was + * checked only before an iteration, so one long option still ran past it. */ + size_t options_cap = sizeof(input); + if (pkg->hash[0]) { + size_t hash_room = strlen(pkg->hash) + 2u; /* ':' + hash + NUL */ + options_cap = (hash_room < sizeof(input)) ? sizeof(input) - hash_room + 1u + : 1u; + } + + for (int i = 0; i < pkg->option_count && len < options_cap - 1u; i++) { + len = append_clamped(input, options_cap, len, ":%s=%s", + pkg->options[i].key, pkg->options[i].value); } if (pkg->hash[0]) { - snprintf(input + len, sizeof(input) - (size_t)len, ":%s", pkg->hash); + len = append_clamped(input, sizeof(input), len, ":%s", pkg->hash, NULL); } + (void)len; char hash[EOS_HASH_LEN]; eos_cache_compute_hash(input, (size_t)strlen(input), hash, sizeof(hash)); diff --git a/core/eos/core/src/shell_cmd.c b/core/eos/core/src/shell_cmd.c new file mode 100644 index 00000000..756060b5 --- /dev/null +++ b/core/eos/core/src/shell_cmd.c @@ -0,0 +1,176 @@ +// SPDX-License-Identifier: MIT +// Copyright (c) 2026 EoS Project +// ISO/IEC 25000 | ISO/IEC/IEEE 15288:2023 + +#include "eos/shell_cmd.h" +#include "eos/log.h" +#include +#include +#include +#include +#ifndef _WIN32 +#include +#endif + +/* Reject anything a shell reads as syntax rather than as text. + * + * This rule was first written in services/linux/src/linux_security.c, where + * the list it replaced -- ;|&><$()"' -- missed two that matter. A backtick + * is command substitution in every POSIX shell, so /tmp/`id` passed as safe + * and ran id. A newline ends one command and starts another, so a path + * could append a whole second command. Both were reported SAFE. Control + * characters and backslash are rejected for the same reason. + * + * A NULL value returns 0 -- "refuse" -- because that is the only right + * default for a predicate guarding command construction. */ +/* Windows (cmd.exe, then the program's own CRT argv parsing) reads a quoted + * value differently from a POSIX shell: + * - A backslash is the path separator and means nothing inside double + * quotes, so refusing it refused every Windows path. eos clean, the + * cargo/pip/npm backends and the install tests all failed on the MSVC + * leg with "refusing a shell argument". + * - A backslash directly before the closing quote is different: the CRT + * reads \" as a literal quote, so "C:\dir\" swallows the arguments + * after it. A trailing backslash is refused. + * - %VAR% expands even inside double quotes, and so does !VAR! when + * delayed expansion is on. Both are refused. */ +int eos_shell_arg_is_safe(const char *arg) { + const unsigned char *p; + if (!arg) return 0; +#ifdef _WIN32 + static const char refused[] = ";|&><$()\"'`%!"; +#else + static const char refused[] = ";|&><$()\"'`\\"; +#endif + for (p = (const unsigned char *)arg; *p; p++) { + if (*p < 0x20 || *p == 0x7f) return 0; + if (strchr(refused, (char)*p)) return 0; + } +#ifdef _WIN32 + if (p != (const unsigned char *)arg && p[-1] == '\\') return 0; +#endif + return 1; +} + +/* Some programs read a token as one word whether or not it is quoted -- + * a make target, a CROSS_COMPILE prefix, a board name -- and there a single + * space is already enough to turn one argument into two. + * + * The first version of this rule, also in linux_security.c, was a second + * denylist -- space, tab, *, ?, ~ -- and it had the same shape of hole as + * the first one: it missed [ and ] (a glob character class) and { } (brace + * expansion, which /bin/sh performs when it is bash). Answering an + * incomplete denylist with another denylist just moves the next gap further + * out, so "one shell word" is written as the small positive definition it + * has: isalnum() plus ._/+=:- covers every defconfig target, cross-compile + * prefix, board name, hash algorithm name and hex digest this tree uses, + * and an allowlist cannot be incomplete. + * + * A leading '-' is refused separately: it is built from allowed characters + * but turns `make -C dir ` into an option rather than a target. + * + * The empty string is a word; callers that must not pass one check for + * content before appending. */ +int eos_shell_word_is_safe(const char *word) { + const unsigned char *p; + if (!word) return 0; + if (word[0] == '-') return 0; + for (p = (const unsigned char *)word; *p; p++) { + if (isalnum(*p)) continue; + if (strchr("._/+=:-", (char)*p)) continue; + return 0; + } + return 1; +} + +void eos_shell_cmd_init(EosShellCmd *cmd) { + if (!cmd) return; + cmd->buf[0] = '\0'; + cmd->len = 0; + cmd->fault = 0; +} + +/* Append raw bytes; on a miss mark the command faulty and leave it as it was, + * so a truncated line can never be the one that runs. */ +static void append_raw(EosShellCmd *cmd, const char *s) { + size_t n; + if (!cmd || cmd->fault) return; + n = strlen(s); + if (n >= sizeof(cmd->buf) - cmd->len) { + cmd->fault = 1; + return; + } + memcpy(cmd->buf + cmd->len, s, n + 1); + cmd->len += n; +} + +void eos_shell_cmd_text(EosShellCmd *cmd, const char *text) { + if (!cmd || !text) { if (cmd) cmd->fault = 1; return; } + append_raw(cmd, text); +} + +void eos_shell_cmd_int(EosShellCmd *cmd, int value) { + char digits[16]; + if (!cmd) return; + snprintf(digits, sizeof(digits), "%d", value); + append_raw(cmd, digits); +} + +void eos_shell_cmd_arg(EosShellCmd *cmd, const char *arg) { + if (!cmd) return; + if (!eos_shell_arg_is_safe(arg)) { + EOS_ERROR("refusing a shell argument that is not one quoted value: %s", + arg ? arg : "(null)"); + cmd->fault = 1; + return; + } + /* The quotes and the value land together or not at all, so a value that + * does not fit leaves no dangling quote behind. */ + if (strlen(arg) + 2 >= sizeof(cmd->buf) - cmd->len) { + cmd->fault = 1; + return; + } + append_raw(cmd, "\""); + append_raw(cmd, arg); + append_raw(cmd, "\""); +} + +void eos_shell_cmd_word(EosShellCmd *cmd, const char *word) { + if (!cmd) return; + if (!eos_shell_word_is_safe(word)) { + EOS_ERROR("refusing a shell argument that is not one word: %s", + word ? word : "(null)"); + cmd->fault = 1; + return; + } + append_raw(cmd, word); +} + +EosResult eos_shell_cmd_run(EosShellCmd *cmd, const char *what) { + int rc; + if (!cmd) return EOS_ERR_INVALID; + if (cmd->fault) { + EOS_ERROR("%s: command refused; an argument was unsafe or the line did not fit", + what ? what : "shell command"); + return EOS_ERR_INVALID; + } + EOS_INFO("%s: %s", what ? what : "shell command", cmd->buf); + rc = system(cmd->buf); + /* "The build failed" and "nothing ran at all" are different problems + * for whoever reads the log: -1 is a shell that could not be started + * (fork failed, or the status could not be collected), and a POSIX + * shell exits 127 when it could not run the command it was given. + * Both are the environment, not the build. */ + if (rc == -1) { + EOS_ERROR("%s: could not run a shell at all", what ? what : "shell command"); + return EOS_ERR_SYSTEM; + } +#ifndef _WIN32 + if (WIFEXITED(rc) && WEXITSTATUS(rc) == 127) { + EOS_ERROR("%s: the shell could not run the command (exit 127)", + what ? what : "shell command"); + return EOS_ERR_SYSTEM; + } +#endif + return (rc == 0) ? EOS_OK : EOS_ERR_BUILD; +} diff --git a/core/eos/debug/src/gdb_stub.c b/core/eos/debug/src/gdb_stub.c index 7bcaa32e..864a85b1 100644 --- a/core/eos/debug/src/gdb_stub.c +++ b/core/eos/debug/src/gdb_stub.c @@ -13,6 +13,23 @@ static uint8_t hex_val(char c) { return 0; } +/** + * @brief Hex digit to value, reporting invalid input. + * + * hex_val() maps any non-hex byte to 0, which silently turns a malformed + * payload into zero bytes. Callers that write the decoded result to memory + * use this variant instead and reject the packet. + * + * @param c Candidate hex digit. + * @return 0-15 for a valid digit, -1 otherwise. + */ +static int hex_val_checked(char c) { + if (c >= '0' && c <= '9') return c - '0'; + if (c >= 'a' && c <= 'f') return c - 'a' + 10; + if (c >= 'A' && c <= 'F') return c - 'A' + 10; + return -1; +} + static char hex_char(uint8_t v) { return "0123456789abcdef"[v & 0x0F]; } @@ -32,30 +49,69 @@ static int send_packet(EosGdbStub *stub, const char *data) { return stub->io.write(stub->io.ctx, (const uint8_t *)pkt, n); } +/* The largest packet body the stub accepts. It is what qSupported reports + * as PacketSize, and it is derived from the receive buffer so the two cannot + * drift apart: the stub used to advertise 0x400 over a 512-byte buffer, so a + * packet GDB was told it could send was refused every time. */ +#define GDB_PKT_BUF 512 +#define GDB_PKT_MAX (GDB_PKT_BUF - 1) + +/* The most memory one m/M packet may move, derived from GDB_PKT_MAX so the + * two cannot drift apart either: an M packet is "M,:" -- up to + * 15 characters with a 32-bit address -- followed by two hex digits per + * byte, and it has to fit the body the stub accepts. GDB sizes its bulk + * transfers from PacketSize, so a cap below this is a request the stub + * invited and then refused. The reply to an m packet is 2*len characters + * and fits send_packet()'s buffer for the same reason. */ +#define GDB_MEM_HDR 15 +#define GDB_MEM_MAX ((GDB_PKT_MAX - GDB_MEM_HDR) / 2) + +/* recv_packet() results below zero: */ +#define GDB_RECV_TRANSPORT_GONE (-1) /* read failed: the session is over */ +#define GDB_RECV_BAD_PACKET (-2) /* checksum or size: '-' was sent, GDB will resend */ + static int recv_packet(EosGdbStub *stub, char *buf, int maxlen) { - if (!stub->io.read) return -1; + if (!stub->io.read) return GDB_RECV_TRANSPORT_GONE; uint8_t c; do { - if (stub->io.read(stub->io.ctx, &c, 1) != 1) return -1; + if (stub->io.read(stub->io.ctx, &c, 1) != 1) return GDB_RECV_TRANSPORT_GONE; } while (c != '$'); - int i = 0; - while (i < maxlen - 1) { - if (stub->io.read(stub->io.ctx, &c, 1) != 1) return -1; + /* Read the body up to '#'. A body the buffer cannot hold is drained to + * its '#' and refused with '-', so the checksum and the next packet are + * read from where they really are. Stopping early and treating the next + * two body bytes as the checksum left the stream out of step. */ + int i = 0, overflow = 0, drained = 0; + for (;;) { + if (stub->io.read(stub->io.ctx, &c, 1) != 1) return GDB_RECV_TRANSPORT_GONE; if (c == '#') break; - buf[i++] = (char)c; + if (i < maxlen - 1) { + buf[i++] = (char)c; + } else if (++drained > GDB_PKT_MAX) { + /* Draining is for a body that is merely too long. One that never + * ends -- a peer that keeps sending and never a '#' -- would + * otherwise hold the exception handler forever. Give up on it + * as a bad packet: '-' goes out, the session stays up, and the + * '$' search above resynchronises on whatever comes next. */ + uint8_t nack = '-'; + if (stub->io.write) stub->io.write(stub->io.ctx, &nack, 1); + return GDB_RECV_BAD_PACKET; + } else { + overflow = 1; + } } buf[i] = '\0'; uint8_t cs_chars[2]; - if (stub->io.read(stub->io.ctx, cs_chars, 2) != 2) return -1; + if (stub->io.read(stub->io.ctx, cs_chars, 2) != 2) return GDB_RECV_TRANSPORT_GONE; uint8_t expected = (hex_val((char)cs_chars[0]) << 4) | hex_val((char)cs_chars[1]); uint8_t actual = checksum(buf, i); + int ok = !overflow && actual == expected; - uint8_t ack = (actual == expected) ? '+' : '-'; + uint8_t ack = ok ? '+' : '-'; if (stub->io.write) stub->io.write(stub->io.ctx, &ack, 1); - return (actual == expected) ? i : -1; + return ok ? i : GDB_RECV_BAD_PACKET; } static void hex_encode(char *dst, const uint8_t *src, int len) { @@ -74,7 +130,10 @@ static void handle_read_regs(EosGdbStub *stub) { static void handle_query(EosGdbStub *stub, const char *pkt) { if (strncmp(pkt, "qSupported", 10) == 0) { - send_packet(stub, "PacketSize=400;swbreak+;hwbreak+"); + char supported[64]; + snprintf(supported, sizeof(supported), "PacketSize=%x;swbreak+;hwbreak+", + (unsigned)GDB_PKT_MAX); + send_packet(stub, supported); } else if (strcmp(pkt, "qAttached") == 0) { send_packet(stub, "1"); } else if (strcmp(pkt, "qTStatus") == 0) { @@ -92,32 +151,43 @@ static void handle_query(EosGdbStub *stub, const char *pkt) { static void handle_read_mem(EosGdbStub *stub, const char *pkt) { uint32_t addr = 0, len = 0; - if (sscanf(pkt, "m%x,%x", &addr, &len) != 2 || len > 128) { + if (sscanf(pkt, "m%x,%x", &addr, &len) != 2 || len > GDB_MEM_MAX) { send_packet(stub, "E01"); return; } - uint8_t buf[128]; + uint8_t buf[GDB_MEM_MAX]; if (eos_gdb_read_mem(addr, buf, (int)len) != 0) { send_packet(stub, "E02"); return; } - char resp[257]; + char resp[GDB_MEM_MAX * 2 + 1]; hex_encode(resp, buf, (int)len); send_packet(stub, resp); } static void handle_write_mem(EosGdbStub *stub, const char *pkt) { uint32_t addr = 0, len = 0; - if (sscanf(pkt, "M%x,%x:", &addr, &len) != 2 || len > 128) { + if (sscanf(pkt, "M%x,%x:", &addr, &len) != 2 || len > GDB_MEM_MAX) { send_packet(stub, "E01"); return; } const char *data = strchr(pkt, ':'); if (!data) { send_packet(stub, "E01"); return; } data++; - uint8_t buf[128]; - for (uint32_t i = 0; i < len; i++) - buf[i] = (hex_val(data[i * 2]) << 4) | hex_val(data[i * 2 + 1]); + + /* The packet must actually carry two hex digits per byte. Without this + * check the decode loop read past the end of the received packet and + * handed whatever followed it to eos_gdb_write_mem() at an address the + * remote peer chose. */ + if (strlen(data) < (size_t)len * 2u) { send_packet(stub, "E01"); return; } + + uint8_t buf[GDB_MEM_MAX]; + for (uint32_t i = 0; i < len; i++) { + int hi = hex_val_checked(data[i * 2]); + int lo = hex_val_checked(data[i * 2 + 1]); + if (hi < 0 || lo < 0) { send_packet(stub, "E01"); return; } + buf[i] = (uint8_t)((hi << 4) | lo); + } if (eos_gdb_write_mem(addr, buf, (int)len) != 0) { send_packet(stub, "E02"); return; @@ -185,10 +255,14 @@ void eos_gdb_handle_exception(EosGdbStub *stub, int signal) { snprintf(reply, sizeof(reply), "S%02x", signal & 0xFF); send_packet(stub, reply); - char pkt[512]; + char pkt[GDB_PKT_BUF]; while (stub->connected) { int n = recv_packet(stub, pkt, sizeof(pkt)); - if (n < 0) break; + if (n == GDB_RECV_TRANSPORT_GONE) break; + /* A refused packet has been answered with '-'; GDB resends it. The + * session used to end here instead, on the first corrupt or + * oversized packet. */ + if (n < 0) continue; if (n == 0) continue; switch (pkt[0]) { diff --git a/core/eos/docs/adding-hardware.md b/core/eos/docs/adding-hardware.md index 3c24b7e3..caca7952 100644 --- a/core/eos/docs/adding-hardware.md +++ b/core/eos/docs/adding-hardware.md @@ -131,7 +131,7 @@ eboot_add_board(my_board) ### Step 4: Build and test ```bash -cmake -B build -DEBLDR_BOARD=my_board +cmake -B build -DEBLDR_BOARD=my_board -DCMAKE_BUILD_TYPE=Debug cmake --build build ``` diff --git a/core/eos/docs/hardware-alignment.md b/core/eos/docs/hardware-alignment.md index 9f37ebe3..aa43d191 100644 --- a/core/eos/docs/hardware-alignment.md +++ b/core/eos/docs/hardware-alignment.md @@ -50,6 +50,8 @@ Legacy/Niche: **Result: 25/25 boards aligned.** Every eboot board has a matching eos board definition, and every eos board has a matching eboot port. +> Superseded: measured 2026-09-14, the full inventories are 84 eos board YAMLs against 83 eBoot board directories, and only 7 names match across them; the 25 rows above are the subset the original audit chose, not the whole. Both halves of the sentence above are false for the full inventories. See `docs/three-way-alignment.md` for the measured status table. + --- ## Detailed Board Inventory @@ -96,7 +98,7 @@ Legacy/Niche: | `imx8m/` | ARM64 | `EOS_PLATFORM_ARM_CA53` | Cortex-A53 | eMMC | 2 GB | | `am64x/` | ARM | `EOS_PLATFORM_ARM_CA53` | A53+R5F | 64 MB | 2 GB | | `stm32mp1/` | ARM | `EOS_PLATFORM_ARM_CA72` | A7+M4 | eMMC | 512 MB | -| `qemu-arm64/` | ARM64 | `EOS_PLATFORM_ARM_CA53` | Cortex-A57 | virtio | 512 MB | +| `qemu_arm64/` | ARM64 | `EOS_PLATFORM_ARM_CA53` | Cortex-A57 | virtio | 512 MB | | `riscv64_virt/` | RISC-V | `EOS_PLATFORM_RISCV64` | RV64GC | virtio | 256 MB | | `sifive_u/` | RISC-V | `EOS_PLATFORM_RISCV64` | U74 | SPI | 8 GB | | `esp32/` | Xtensa | `EOS_PLATFORM_XTENSA` | LX6 | 4 MB | 520 KB | @@ -123,7 +125,7 @@ Legacy/Niche: | Architecture | eos Toolchain | eos Boards | eboot Boards | Platform Enum | |-------------|--------------|-----------|-------------|---------------| | **ARM Cortex-M** | `arm-none-eabi` | nRF52840, STM32F4, STM32H7, SAMD51 | nrf52, stm32f4, stm32h7, samd51 | `ARM_CM0`..`ARM_CM33` | -| **ARM Cortex-A** | `aarch64-linux-gnu` | RPi4, i.MX8M, AM64x, STM32MP1, QEMU | rpi4, imx8m, am64x, stm32mp1, qemu-arm64 | `ARM_CA53`, `ARM_CA72` | +| **ARM Cortex-A** | `aarch64-linux-gnu` | RPi4, i.MX8M, AM64x, STM32MP1, QEMU | rpi4, imx8m, am64x, stm32mp1, qemu_arm64 (eos: `qemu-arm64.yaml` — names differ) | `ARM_CA53`, `ARM_CA72` | | **RISC-V 64** | `riscv64-linux-gnu` | generic-riscv64, SiFive U | riscv64_virt, sifive_u | `RISCV64` | | **Xtensa** | (vendor SDK) | ESP32 | esp32 | `XTENSA` | | **x86 / x86_64** | `x86_64-linux-gnu` | generic-x86, generic-x86_64 | x86, x86_64_efi | `X86`, `X86_64` | diff --git a/core/eos/docs/integration-guide.md b/core/eos/docs/integration-guide.md index edf51e7d..0fea5b4b 100644 --- a/core/eos/docs/integration-guide.md +++ b/core/eos/docs/integration-guide.md @@ -82,7 +82,7 @@ EXTRA_OECMAKE = "-DEOS_PRODUCT=gateway" ```bash cd EoS/eboot -cmake -B build -DEBLDR_BOARD=nrf52 +cmake -B build -DEBLDR_BOARD=nrf52 -DCMAKE_BUILD_TYPE=Debug cmake --build build ``` @@ -126,7 +126,7 @@ Build your eos app with the linker script: ```bash cd EoS/eos cmake -B build -DEOS_PRODUCT=iot \ - -DCMAKE_TOOLCHAIN_FILE=toolchains/arm-none-eabi.cmake \ + -DCMAKE_TOOLCHAIN_FILE=toolchains/arm-cortex-m4.cmake \ -DEOS_LINKER_SCRIPT=../ebuild/_generated/eboot_linker.ld cmake --build build ``` @@ -135,7 +135,7 @@ cmake --build build ```bash # Generate signing key (first time only) -cd EoS/eboot/tools +cd eBoot/tools python3 sign_image.py --generate-key firmware-key.pem # Sign the firmware @@ -191,7 +191,7 @@ cmake --build build # Build eboot cd ../eboot -cmake -B build -DEBLDR_BOARD=nrf52 +cmake -B build -DEBLDR_BOARD=nrf52 -DCMAKE_BUILD_TYPE=Debug cmake --build build ``` diff --git a/core/eos/docs/quickstart-host.md b/core/eos/docs/quickstart-host.md index 34d33117..0b4f12a1 100644 --- a/core/eos/docs/quickstart-host.md +++ b/core/eos/docs/quickstart-host.md @@ -31,8 +31,8 @@ winget install CMake.CMake ## Step 1: Build an example ```bash -git clone https://github.com/anthropic/EoS.git -cd EoS/eos/examples/blink-gpio +git clone https://github.com/embeddedos-org/eos.git +cd eos/examples/blink-gpio cmake -B build -DEOS_PRODUCT=iot cmake --build build @@ -58,7 +58,7 @@ Press `Ctrl+C` to stop. ## Step 3: Run the full test suite ```bash -cd EoS/eos +cd eos cmake -B build -DEOS_BUILD_TESTS=ON cmake --build build ctest --test-dir build --output-on-failure @@ -69,7 +69,7 @@ ctest --test-dir build --output-on-failure ## Try all examples ```bash -cd EoS/eos/examples +cd eos/examples # Each example builds the same way: for example in blink-gpio uart-echo multitask-rtos posix-app multicore-amp; do @@ -97,7 +97,7 @@ done When you're ready to target an MCU: 1. Install the cross-compiler (`arm-none-eabi-gcc`) -2. Add `-DCMAKE_TOOLCHAIN_FILE=../../toolchains/arm-none-eabi.cmake` to your cmake command +2. Add `-DCMAKE_TOOLCHAIN_FILE=../../toolchains/arm-cortex-m4.cmake` to your cmake command 3. Flash the resulting binary to your board Your application code stays the same — only the build command changes. diff --git a/core/eos/docs/quickstart-nrf52.md b/core/eos/docs/quickstart-nrf52.md index 2fad60f0..fb1cae4c 100644 --- a/core/eos/docs/quickstart-nrf52.md +++ b/core/eos/docs/quickstart-nrf52.md @@ -35,11 +35,11 @@ nrfjprog --version # Should print 10.x or later ## Step 1: Clone and build ```bash -git clone https://github.com/anthropic/EoS.git -cd EoS/eos/examples/blink-gpio +git clone https://github.com/embeddedos-org/eos.git +cd eos/examples/blink-gpio cmake -B build \ - -DCMAKE_TOOLCHAIN_FILE=../../toolchains/arm-none-eabi.cmake \ + -DCMAKE_TOOLCHAIN_FILE=../../toolchains/arm-cortex-m4.cmake \ -DEOS_PRODUCT=iot cmake --build build ``` diff --git a/core/eos/docs/quickstart-rpi4.md b/core/eos/docs/quickstart-rpi4.md index 160e00d9..9a6cdcee 100644 --- a/core/eos/docs/quickstart-rpi4.md +++ b/core/eos/docs/quickstart-rpi4.md @@ -24,8 +24,8 @@ sudo apt install cmake gcc g++ git python3 python3-pip ```bash # On the Raspberry Pi: -git clone https://github.com/anthropic/EoS.git -cd EoS/eos/examples/blink-gpio +git clone https://github.com/embeddedos-org/eos.git +cd eos/examples/blink-gpio # Build using the Linux HAL backend (native compile on Pi) cmake -B build -DEOS_PRODUCT=gateway @@ -69,7 +69,7 @@ Connect an LED + resistor (330Ω) between GPIO 18 and GND, then run. sudo apt install gcc-aarch64-linux-gnu # Build -cd EoS/eos/examples/blink-gpio +cd eos/examples/blink-gpio cmake -B build \ -DCMAKE_C_COMPILER=aarch64-linux-gnu-gcc \ -DEOS_PRODUCT=gateway diff --git a/core/eos/docs/quickstart-stm32.md b/core/eos/docs/quickstart-stm32.md index ba884291..8ffed29a 100644 --- a/core/eos/docs/quickstart-stm32.md +++ b/core/eos/docs/quickstart-stm32.md @@ -29,11 +29,11 @@ brew install openocd # macOS ## Step 1: Clone and build ```bash -git clone https://github.com/anthropic/EoS.git -cd EoS/eos/examples/blink-gpio +git clone https://github.com/embeddedos-org/eos.git +cd eos/examples/blink-gpio cmake -B build \ - -DCMAKE_TOOLCHAIN_FILE=../../toolchains/arm-none-eabi.cmake \ + -DCMAKE_TOOLCHAIN_FILE=../../toolchains/arm-cortex-m4.cmake \ -DEOS_PRODUCT=industrial cmake --build build ``` diff --git a/core/eos/docs/three-way-alignment.md b/core/eos/docs/three-way-alignment.md index 7f9c8c15..a9e2ab6c 100644 --- a/core/eos/docs/three-way-alignment.md +++ b/core/eos/docs/three-way-alignment.md @@ -4,23 +4,145 @@ This document tracks alignment between all three EoS components to ensure they r --- -## Alignment Status: ✅ ALL ALIGNED +## Alignment Status: ⚠️ NOT ALIGNED — the inventories disagree and nothing cross-checks them + +Measured on 2026-09-14 against origin/master of eos, eBoot and ebuild (eos `9ed6831`, eBoot `221325c`, ebuild `76970c9`). Every number below comes from the commands under "How this was measured"; the previous banner and its counts (25 / 25 / 41 / 24 / 33 / 39 / 5) did not reproduce. | Dimension | eos | eboot | ebuild | Status | |-----------|-----|-------|--------|--------| -| Board definitions | 25 YAML files in `eos/boards/` | 25 board ports in `eboot/boards/` | `MCU_TO_EBOOT_BOARD` + `EOS_BOARD_MAP` in project generator | ✅ Aligned | -| Product profiles | 41 profiles in `eos/products/*.h` | — | `PRODUCT_MAP` (41 entries) in project generator | ✅ Aligned | -| Platform enum | — | 24 `eos_platform_t` entries in `eos_hal.h` | MCU_DATABASE (100+ MCUs) in hw analyzer | ✅ Aligned | -| Peripheral keywords | 33 HAL APIs in `hal.h` + `hal_extended.h` | — | `PERIPHERAL_KEYWORDS` (24 types) + `ComponentDB` (200+ parts) | ✅ Aligned | -| Multicore support | `multicore.h` API | `eos_multicore.h` boot API | `MULTICORE_MCUS` set in project generator | ✅ Aligned | -| Config generation | `eos_config.h` (39 `EOS_ENABLE_*` flags) | `eboot_flash_layout.h` | `EosConfigGenerator` produces both | ✅ Aligned | -| Templates | — | — | 5 templates in `ebuild/templates/` | ✅ Aligned | -| CLI commands | cmake build | cmake build | `ebuild new`, `build`, `analyze`, `generate-project` | ✅ Aligned | +| Board definitions | 84 `boards/*.yaml` (89 entries in `boards/`; the other 5 are `lm3s6965evb.ld`, `startup_stm32f407.S`, `stm32f407vg.ld`, `stm32f407_discovery/`, `validation/`) | 83 `boards/*/` directories, 83 `eboot_add_board()` calls in `CMakeLists.txt`, 83 `boards/*/board_*.h` | `ebuild/sdk_generator.py`: `TARGET_ARCH` 14, `EBOOT_BOARD` 14, `MCU_TO_EBOOT_BOARD` 136 keys (56 distinct eboot dirs); `ebuild/eos_ai/eos_project_generator.py`: `EOS_BOARD_MAP` 116 keys (81 distinct eos YAMLs) | ⚠️ 84 / 83 / 14+136 are differently-keyed inventories of different sizes and no tool compares them. Every `MCU_TO_EBOOT_BOARD` value is an eboot dir (56/56) but 27 eboot dirs are never targeted; every `EOS_BOARD_MAP` value is an eos YAML (81/81) but `hiletgo-esp-wroom-32.yaml`, `qemu-arm64.yaml` and `tms570.yaml` are never mapped; only 7 eos YAML stems share a name with an eboot dir. | +| Product profiles | 48 `products/*.h`; 42 `#if`/`#elif defined(EOS_PRODUCT_*)` branches in `include/eos/eos_config.h` | — | `PRODUCT_MAP` 41 keys in `eos_project_generator.py` | ⚠️ 48 / 42 / 41. All 41 `PRODUCT_MAP` keys have a header and a config branch. `cast_device`, `desktop`, `home_camera`, `iptv_stb`, `smart_speaker`, `tv_os` have a header but neither a config branch nor a `PRODUCT_MAP` entry; `vbox_test` has a header and a config branch but no `PRODUCT_MAP` entry. | +| Platform enum | — | 24 `eos_platform_t` enumerators in `include/eos_hal.h` | `MCU_DATABASE` 171 keys in `eos_hw_analyzer.py`, using 52 distinct `arch` strings | ⚠️ The 24 reproduces. `MCU_DATABASE` describes MCUs by `arch` strings (`arm`, `mips64`, `sh`, …) that share no vocabulary with the enumerators, and nothing maps one onto the other. | +| Peripheral keywords | 33 peripheral families with an `eos__init()`: 5 in `hal/include/eos/hal.h`, 28 in `hal/include/eos/hal_extended.h` | — | `PERIPHERAL_KEYWORDS` 46 keywords mapping to 24 distinct types in `eos_hw_analyzer.py`; `ComponentDB` 114 parts in `component_db.py` (its docstring says 200+) | ⚠️ 33 and 24 reproduce; 200+ does not (114). Only 21 of the 24 types are HAL family names: `ethernet`, `gps`, `watchdog` are spelled `eth`, `gnss`, `wdt` in the HAL, and `cellular`, `flash`, `gpu`, `haptic`, `hdmi`, `ir`, `pcie`, `radar`, `touch` have no keyword at all. | +| Multicore support | `kernel/include/eos/multicore.h` (38 declarations: `eos_core_*`, `eos_spin_*`, `eos_ipi_*`, `eos_rproc_*`, `eos_shmem_*`, …) | `include/eos_multicore.h` (12 `eos_multicore_*` declarations) | `MULTICORE_MCUS` 13 names in `eos_project_generator.py` | ⚠️ All three exist, and that is all that was checked: the two headers are different APIs, and nothing compares either of them to `MULTICORE_MCUS`. | +| Config generation | 34 `#define EOS_ENABLE_*` in `include/eos/eos_config.h` | `eboot_flash_layout.h` is not a file in eBoot (0 tracked files); it is generated output that eBoot only documents (`configs/README.md`, `docs/quickstart.md`, `docs/book/book.md`) | `EosConfigGenerator.generate_eos_config_h()` writes `eos_product_config.h`; `EosBootIntegrator.generate_from_boot_yaml()` writes `eboot_flash_layout.h` | ⚠️ 39 did not reproduce (34), and "`EosConfigGenerator` produces both" was wrong: two classes, two files. Across `ebuild/**/*.py` (`eos_hw_analyzer.py` 24, `eos_project_generator.py` 24, `component_db.py` 23; 29 distinct) ebuild names 29 `EOS_ENABLE_*` flags; 24 are defined in `eos_config.h`, `EOS_ENABLE_GPIO`/`I2C`/`SPI`/`TIMER`/`UART` are defined nowhere in eos, and 10 `eos_config.h` flags (`EOS_ENABLE_DISPLAY_DRV`, `EOS_ENABLE_ETHERNET_DRV`, `EOS_ENABLE_GPU`, `EOS_ENABLE_HDMI`, `EOS_ENABLE_MULTICORE`, `EOS_ENABLE_NET`, `EOS_ENABLE_PCIE`, `EOS_ENABLE_PCIE_DRV`, `EOS_ENABLE_SDIO`, `EOS_ENABLE_USB_HOST`) are never named by ebuild. | +| Templates | — | — | 6 directories in `templates/` at the ebuild repo root (`bare-metal`, `ble-sensor`, `linux-app`, `rtos-app`, `safety-critical`, `secure-boot`); `ebuild/templates/` does not exist | ⚠️ "5 in `ebuild/templates/`" did not reproduce: 6, at a different path. eos names four of them by their `ebuild new --template` spelling (`bare-metal` in `GETTING_STARTED.md` and `docs/quickstart-host.md`, `ble-sensor` in `docs/quickstart-nrf52.md`, `linux-app` in `docs/quickstart-rpi4.md`); all four exist in ebuild's `templates/`. eBoot names none. Nothing checks that a documented template name exists. | +| CLI commands | cmake build (`CMakeLists.txt`) | cmake build (`CMakeLists.txt`) | `new`, `build`, `analyze`, `generate-project` are `@cli.command` functions in `ebuild/cli/commands.py` (23 `@cli.command` entries plus the `repos` group with 6 subcommands) | ✅ The four named subcommands exist by name. That is a presence check on ebuild alone, not an inventory comparison. | + +--- + +## How this was measured + +Run from a directory holding sibling checkouts `eos/`, `eBoot/` and `ebuild/` with `origin/master` fetched (`git -C fetch origin`). Every command reads `origin/master`, not the working tree, so it gives the same answer whatever is checked out. + +```bash +git -C eos ls-tree --name-only origin/master boards/ | wc -l # 89 +git -C eos ls-tree --name-only origin/master boards/ | grep -c '\.yaml$' # 84 +git -C eos ls-tree --name-only origin/master boards/ | grep -v '\.yaml$' # the other 5 +git -C eBoot ls-tree origin/master boards/ | grep -c '^040000' # 83 +git -C eBoot show origin/master:CMakeLists.txt | grep -c 'eboot_add_board(' # 83 +git -C eBoot ls-tree -r --name-only origin/master boards/ | grep -cE '^boards/[^/]+/board_[^/]+\.h$' # 83 +git -C eos ls-tree --name-only origin/master products/ | grep -c '\.h$' # 48 +git -C eBoot show origin/master:include/eos_hal.h | awk '/typedef enum/{f=1} f && /^[[:space:]]*EOS_PLATFORM_/{n++} /} eos_platform_t;/{f=0} END{print n}' # 24 +git -C eos show origin/master:include/eos/eos_config.h | grep -cE '^\s*#\s*define\s+EOS_ENABLE_' # 34 +git -C ebuild ls-tree origin/master templates/ | grep -c '^040000' # 6 +git -C ebuild ls-tree --name-only origin/master ebuild/templates/ | wc -l # 0: the path does not exist +git -C ebuild show origin/master:ebuild/eos_ai/component_db.py > /tmp/component_db.py +python3 -c "import importlib.util as u, sys; s=u.spec_from_file_location('component_db', sys.argv[1]); m=u.module_from_spec(s); sys.modules['component_db']=m; s.loader.exec_module(m); print(len(m.ComponentDB()._db))" /tmp/component_db.py # 114 +git -C ebuild show origin/master:ebuild/cli/commands.py | grep -cE '^@cli\.command' # 23 +git -C ebuild show origin/master:ebuild/cli/commands.py | grep -cE '^@repos\.command' # 6 +git -C ebuild show origin/master:ebuild/cli/commands.py | grep -oE '^def (new|build|analyze|generate_project)\(' # all four +git -C eos show origin/master:kernel/include/eos/multicore.h | grep -oE '\beos_[a-z0-9_]+\s*\(' | sort -u | wc -l # 38 +git -C eBoot show origin/master:include/eos_multicore.h | tr -d '\r' | grep -oE '\beos_multicore_[a-z0-9_]+\s*\(' | sort -u | wc -l # 12 +git -C ebuild show origin/master:ebuild/eos_ai/eos_config_generator.py | grep -n 'eos_product_config.h' # written by generate_eos_config_h() +git -C ebuild show origin/master:ebuild/eos_ai/eos_boot_integrator.py | grep -n 'eboot_flash_layout.h' # written by generate_from_boot_yaml() +git -C eBoot ls-tree -r --name-only origin/master | grep -c 'eboot_flash_layout\.h' # 0 +git -C eBoot grep -l 'eboot_flash_layout' origin/master -- ':!build_sim' # configs/README.md, docs/book/book.md, docs/quickstart.md +``` + +The dictionary sizes and every set comparison in the table come from this script. It parses the ebuild sources with `ast` instead of grepping them: a grep for `":` counts every quoted key in the file, not the entries of one dict. + +```python +import ast, re, subprocess + +def show(repo, path): + return subprocess.run(["git", "-C", repo, "show", f"origin/master:{path}"], + check=True, capture_output=True, text=True).stdout + +def consts(repo, path, *names): + out = {} + for node in ast.walk(ast.parse(show(repo, path))): + if isinstance(node, (ast.Assign, ast.AnnAssign)): + for t in (node.targets if isinstance(node, ast.Assign) else [node.target]): + if isinstance(t, ast.Name) and t.id in names: + out[t.id] = ast.literal_eval(node.value) + return out + +sdk = consts("ebuild", "ebuild/sdk_generator.py", "TARGET_ARCH", "EBOOT_BOARD", "MCU_TO_EBOOT_BOARD") +gen = consts("ebuild", "ebuild/eos_ai/eos_project_generator.py", "EOS_BOARD_MAP", "PRODUCT_MAP", "MULTICORE_MCUS") +hw = consts("ebuild", "ebuild/eos_ai/eos_hw_analyzer.py", "PERIPHERAL_KEYWORDS", "MCU_DATABASE") +for name, d in {**sdk, **gen, **hw}.items(): + print(name, len(d)) +print("PERIPHERAL_KEYWORDS distinct values", len(set(hw["PERIPHERAL_KEYWORDS"].values()))) +print("MCU_DATABASE distinct arch", len({v["arch"] for v in hw["MCU_DATABASE"].values()})) + +yaml = {l.split("/")[-1] for l in show("eos", "boards/").split() if l.endswith(".yaml")} +dirs = {l.split()[-1].split("/")[-1] for l in subprocess.run( + ["git", "-C", "eBoot", "ls-tree", "origin/master", "boards/"], + check=True, capture_output=True, text=True).stdout.splitlines() if l.startswith("040000")} +m2e, ebm = sdk["MCU_TO_EBOOT_BOARD"], gen["EOS_BOARD_MAP"] +print("eos yaml", len(yaml), "| eboot dirs", len(dirs), + "| yaml stems that are eboot dirs", len({y[:-5] for y in yaml} & dirs)) +print("MCU_TO_EBOOT_BOARD distinct values", len(set(m2e.values())), + "| values that are eboot dirs", len(set(m2e.values()) & dirs), + "| eboot dirs never targeted", len(dirs - set(m2e.values()))) +print("EOS_BOARD_MAP distinct values", len(set(ebm.values())), + "| values that are eos yaml", len(set(ebm.values()) & yaml), + "| eos yaml never mapped", sorted(yaml - set(ebm.values()))) + +prods = {l.split("/")[-1][:-2] for l in show("eos", "products/").split() if l.endswith(".h")} +cfg = show("eos", "include/eos/eos_config.h") +chain = {m.lower() for m in re.findall(r"#\s*(?:el)?if\s+defined\s*\(\s*EOS_PRODUCT_([A-Z0-9_]+)\s*\)", cfg)} +pm = set(gen["PRODUCT_MAP"]) +print("products/*.h", len(prods), "| eos_config.h product branches", len(chain), "| PRODUCT_MAP", len(pm)) +print("headers without PRODUCT_MAP entry", sorted(prods - pm)) +print("headers without config branch", sorted(prods - chain), "| PRODUCT_MAP keys without header", sorted(pm - prods)) + +enables = set(re.findall(r"^\s*#\s*define\s+(EOS_ENABLE_[A-Z0-9_]+)", cfg, re.M)) +py_files = [l for l in subprocess.run(["git", "-C", "ebuild", "ls-tree", "-r", "--name-only", + "origin/master", "ebuild/"], capture_output=True, + text=True, check=True).stdout.split() if l.endswith(".py")] +named = set(re.findall(r"EOS_ENABLE_[A-Z0-9_]+", "".join(show("ebuild", f) for f in py_files))) +print("EOS_ENABLE_* defines", len(enables), "| named in ebuild/**/*.py", len(named), "| common", len(enables & named), + "| ebuild-only", sorted(named - enables), "| eos-only", sorted(enables - named)) + +fam = set() +for h in ("hal/include/eos/hal.h", "hal/include/eos/hal_extended.h"): + fam |= set(re.findall(r"\beos_([a-z0-9]+)_init\s*\(", show("eos", h))) - {"hal"} +kw = set(hw["PERIPHERAL_KEYWORDS"].values()) +print("HAL init families", len(fam), "| keyword types that are HAL families", len(kw & fam), + "| keyword types that are not", sorted(kw - fam), "| HAL families with no keyword", sorted(fam - kw)) +``` + +Its output on 2026-09-14: + +``` +MCU_TO_EBOOT_BOARD 136 +TARGET_ARCH 14 +EBOOT_BOARD 14 +MULTICORE_MCUS 13 +PRODUCT_MAP 41 +EOS_BOARD_MAP 116 +MCU_DATABASE 171 +PERIPHERAL_KEYWORDS 46 +PERIPHERAL_KEYWORDS distinct values 24 +MCU_DATABASE distinct arch 52 +eos yaml 84 | eboot dirs 83 | yaml stems that are eboot dirs 7 +MCU_TO_EBOOT_BOARD distinct values 56 | values that are eboot dirs 56 | eboot dirs never targeted 27 +EOS_BOARD_MAP distinct values 81 | values that are eos yaml 81 | eos yaml never mapped ['hiletgo-esp-wroom-32.yaml', 'qemu-arm64.yaml', 'tms570.yaml'] +products/*.h 48 | eos_config.h product branches 42 | PRODUCT_MAP 41 +headers without PRODUCT_MAP entry ['cast_device', 'desktop', 'home_camera', 'iptv_stb', 'smart_speaker', 'tv_os', 'vbox_test'] +headers without config branch ['cast_device', 'desktop', 'home_camera', 'iptv_stb', 'smart_speaker', 'tv_os'] | PRODUCT_MAP keys without header [] +EOS_ENABLE_* defines 34 | named in ebuild/**/*.py 29 | common 24 | ebuild-only ['EOS_ENABLE_GPIO', 'EOS_ENABLE_I2C', 'EOS_ENABLE_SPI', 'EOS_ENABLE_TIMER', 'EOS_ENABLE_UART'] | eos-only ['EOS_ENABLE_DISPLAY_DRV', 'EOS_ENABLE_ETHERNET_DRV', 'EOS_ENABLE_GPU', 'EOS_ENABLE_HDMI', 'EOS_ENABLE_MULTICORE', 'EOS_ENABLE_NET', 'EOS_ENABLE_PCIE', 'EOS_ENABLE_PCIE_DRV', 'EOS_ENABLE_SDIO', 'EOS_ENABLE_USB_HOST'] +HAL init families 33 | keyword types that are HAL families 21 | keyword types that are not ['ethernet', 'gps', 'watchdog'] | HAL families with no keyword ['cellular', 'eth', 'flash', 'gnss', 'gpu', 'haptic', 'hdmi', 'ir', 'pcie', 'radar', 'touch', 'wdt'] +``` --- ## Board-Level Alignment (25 boards) +> Kept from the original audit as a 25-board subset; the full inventories are 84 / 83 / 136 / 116 (status table above). On 2026-09-14 all 25 YAMLs and all 25 eboot directories still exist. The two ebuild columns are shorthand: in 9 rows (`mips`, `powerpc`, `m68k`, `sh4`, `frv`, `h8300`, `mn103`, `strongarm`, `xscale`) the name left of the arrow is not a literal key in at least one of the two maps, which are keyed by MCU part name or name prefix (`mips32`, `pic32`, `jz4740` → `mips`; `mpc`, `p10`, `ppc` → `generic-powerpc.yaml`); the target right of the arrow does exist in every row. + | MCU | eos board YAML | eboot board port | ebuild MCU_TO_EBOOT_BOARD | ebuild EOS_BOARD_MAP | |-----|---------------|-----------------|--------------------------|---------------------| | nRF52840 | `nrf52840.yaml` ✅ | `nrf52/` ✅ | `nrf52→nrf52` ✅ | `nrf52→nrf52840.yaml` ✅ | @@ -53,6 +175,8 @@ This document tracks alignment between all three EoS components to ensure they r ## Product Profile Alignment (41 profiles) +> Kept from the original audit. On 2026-09-14 all 41 rows still hold, but `products/` has 48 headers; the 7 with no `PRODUCT_MAP` entry are listed in the status table. + | eos Profile (`products/*.h`) | In `eos_config.h` #elif chain | In ebuild `PRODUCT_MAP` | |-----|------|------| | `adapter` | ✅ | ✅ | @@ -152,31 +276,33 @@ To verify alignment is maintained after any change: ```bash # 1. Count eos boards -ls eos/boards/*.yaml | wc -l # Should be 25 +ls eos/boards/*.yaml | wc -l # 84 on 2026-09-14 # 2. Count eboot boards -ls eboot/boards/*/board_*.h | wc -l # Should be 25 +ls eboot/boards/*/board_*.h | wc -l # 83 on 2026-09-14 # 3. Count eos product profiles -ls eos/products/*.h | wc -l # Should be 41 +ls eos/products/*.h | wc -l # 48 on 2026-09-14 # 4. Count eos_config.h #elif entries -grep -c "EOS_PRODUCT_" eos/include/eos/eos_config.h # Should be 41 +grep -c "EOS_PRODUCT_" eos/include/eos/eos_config.h # 42 on 2026-09-14 # 5. Count ebuild PRODUCT_MAP entries -grep -c '":' ebuild/ebuild/eos_ai/eos_project_generator.py | head -1 +grep -c '":' ebuild/ebuild/eos_ai/eos_project_generator.py | head -1 # counts every quoted key in the file; use the ast script above (41) # 6. Count platform enum entries -grep -c "EOS_PLATFORM_" eboot/include/eos_hal.h # Should be 24 +grep -c "EOS_PLATFORM_" eboot/include/eos_hal.h # 24 on 2026-09-14 # 7. Run ebuild test -cd ebuild && python test_full_pipeline.py +cd ebuild && python test_full_pipeline.py # test_full_pipeline.py does not exist on ebuild origin/master (2026-09-14) ``` --- ## Build System Alignment +> Kept from the original audit; the counts in this table are the original audit's, and the status table at the top supersedes them. On 2026-09-14 eboot's `CMakeLists.txt` has 86 `EBLDR_BOARD STREQUAL ""` branches naming 83 distinct boards (the sentinel `"none"` appears three times; `grep -c 'EBLDR_BOARD STREQUAL' CMakeLists.txt` → 86, distinct quoted literals → 84 including `"none"`), not 25, and eos's `EOS_PRODUCT` is a free-form cache string whose accepted values are listed in its help text, not enumerated in CMake. + ### CMake Variables Cross-Reference | Variable | eos `CMakeLists.txt` | eboot `CMakeLists.txt` | ebuild CLI | @@ -206,17 +332,19 @@ cmake --build build # Step 4: Build eboot (uses eboot CMakeLists.txt) cd EoS/eboot -cmake -B build -DEBLDR_BOARD=nrf52 +cmake -B build -DEBLDR_BOARD=nrf52 -DCMAKE_BUILD_TYPE=Debug cmake --build build # Outputs: libeboot_hal.a, libeboot_core.a, ebldr_stage0.bin, eboot_firmware.bin # Step 5: Sign and flash (uses eboot tools) -cd EoS/eboot/tools +cd eBoot/tools python3 sign_image.py --key key.pem --input ../../eos/build/app.bin --output signed.bin ``` ### eboot CMakeLists.txt Board Coverage (25/25) +> Kept from the original audit as a 25-board subset; the full eBoot inventory is 83 board directories (status table above), and 27 of them are never targeted by `MCU_TO_EBOOT_BOARD`. The 25/25 below is true of these 25 rows only. + | Board | In `if/elseif` chain | In FATAL_ERROR help string | |-------|---------------------|---------------------------| | stm32f4 | ✅ | ✅ | @@ -259,8 +387,8 @@ python3 sign_image.py --key key.pem --input ../../eos/build/app.bin --output sig | `eos/README.md` | ✅ full API, profiles | ✅ related project link | ✅ related project link | | `eboot/README.md` | ✅ related project link | ✅ full boot API | ✅ related project link | | `ebuild/README.md` | ✅ builds eos | ✅ builds eboot | ✅ full CLI reference | -| `docs/hardware-alignment.md` | ✅ 25 board YAMLs | ✅ 25 board ports | ✅ MCU maps | -| `docs/three-way-alignment.md` | ✅ profiles, boards | ✅ boards, platform enum | ✅ all maps audited | +| `docs/hardware-alignment.md` | ⚠️ 25 of 84 board YAMLs | ⚠️ 25 of 83 board ports | ✅ MCU maps | +| `docs/three-way-alignment.md` | ⚠️ measured 2026-09-14; see the status table | ⚠️ measured 2026-09-14; see the status table | ⚠️ measured 2026-09-14; see the status table | | `docs/adding-hardware.md` | ✅ HAL backends, profiles | ✅ EBOOT_REGISTER_BOARD | ✅ — | | `docs/api-release-process.md` | ✅ Doxyfile, headers | ✅ Doxyfile, headers | ✅ CLI versioning | | `eos/docs/api-reference.md` | ✅ all modules | — | — | diff --git a/core/eos/drivers/devicetree/devicetree.c b/core/eos/drivers/devicetree/devicetree.c index 7ae844fb..ab5100ef 100644 --- a/core/eos/drivers/devicetree/devicetree.c +++ b/core/eos/drivers/devicetree/devicetree.c @@ -11,99 +11,212 @@ static uint32_t be32(const uint8_t *p) { ((uint32_t)p[2] << 8) | (uint32_t)p[3]; } -static uint32_t align4(uint32_t v) { return (v + 3) & ~3u; } +/* ------------------------------------------------------------------ + * Bounds-checked primitives + * + * Everything below treats the blob as untrusted. A flattened device tree + * usually arrives from a prior boot stage or a flash region, so every offset + * and length it contains has to be validated against the buffer we were + * actually handed before it is used to index into it. + * ------------------------------------------------------------------ */ -int eos_dt_parse(EosDeviceTree *dt, const uint8_t *dtb, uint32_t size) { - if (!dt || !dtb || size < 40) return -1; +/* Round `v` up to a 4-byte boundary, refusing the cases where that wraps. */ +static int dt_align4(uint32_t v, uint32_t *out) +{ + if (v > UINT32_MAX - 3u) return -1; + *out = (v + 3u) & ~3u; + return 0; +} + +/* Read a big-endian u32 at `off`, provided all four bytes are inside `len`. */ +static int dt_read_be32(const uint8_t *buf, uint32_t len, uint32_t off, + uint32_t *out) +{ + if (off > len || len - off < 4u) return -1; + *out = be32(buf + off); + return 0; +} + +/* Length of the NUL-terminated string at `off`, without reading past `len`. + * Returns -1 when the buffer ends before a terminator is found. */ +static int dt_strlen_at(const uint8_t *buf, uint32_t len, uint32_t off, + uint32_t *out) +{ + if (off >= len) return -1; + for (uint32_t i = off; i < len; i++) { + if (buf[i] == '\0') { *out = i - off; return 0; } + } + return -1; +} + +/* Copy a bounded, possibly unterminated byte range into a NUL-terminated + * destination buffer. */ +static void dt_copy_str(char *dst, uint32_t dst_sz, const uint8_t *src, + uint32_t src_len) +{ + uint32_t n = (src_len < dst_sz - 1u) ? src_len : dst_sz - 1u; + memcpy(dst, src, n); + dst[n] = '\0'; +} + +/* Cache a string-valued root property. The stored value is only `prop->len` + * bytes and is not guaranteed to be terminated, so stop at whichever comes + * first: an embedded NUL, the property length, or the destination size. */ +static void dt_cache_string_prop(char *dst, uint32_t dst_sz, + const EosDtProp *prop) +{ + uint32_t n = prop->len; + uint32_t i = 0; + while (i < n && prop->data[i] != '\0') i++; + dt_copy_str(dst, dst_sz, prop->data, i); +} + +int eos_dt_parse(EosDeviceTree *dt, const uint8_t *dtb, uint32_t size) +{ + if (!dt || !dtb || size < EOS_DT_HEADER_SIZE) return -1; memset(dt, 0, sizeof(*dt)); - uint32_t magic = be32(dtb); - if (magic != EOS_DT_MAGIC) return -1; + if (be32(dtb) != EOS_DT_MAGIC) return -1; + + uint32_t totalsize = be32(dtb + 4); + uint32_t off_struct = be32(dtb + 8); + uint32_t off_string = be32(dtb + 12); + uint32_t size_string = be32(dtb + 32); + uint32_t size_struct = be32(dtb + 36); - uint32_t totalsize = be32(dtb + 4); - uint32_t off_struct = be32(dtb + 8); - uint32_t off_string = be32(dtb + 12); - dt->version = be32(dtb + 20); + dt->version = be32(dtb + 20); dt->boot_cpuid = be32(dtb + 28); - (void)totalsize; + + /* The blob may sit inside a larger buffer, but it must never claim to be + * larger than the buffer we were given. */ + uint32_t blob_len = size; + if (totalsize > blob_len) return -1; + if (totalsize >= EOS_DT_HEADER_SIZE) blob_len = totalsize; + + /* Both blocks have to begin inside the blob. Without this the subtraction + * below wraps and the walk runs off the end of the buffer. */ + if (off_struct >= blob_len || off_string >= blob_len) return -1; const uint8_t *structs = dtb + off_struct; const uint8_t *strings = dtb + off_string; - uint32_t pos = 0; - EosDtNode *stack[32]; - int depth = 0; + /* Honour the declared block sizes when they fit, otherwise clamp to the + * rest of the blob. Either way the walk stays inside the buffer. */ + uint32_t struct_len = blob_len - off_struct; + uint32_t string_len = blob_len - off_string; + if (size_struct != 0 && size_struct < struct_len) struct_len = size_struct; + if (size_string != 0 && size_string < string_len) string_len = size_string; - while (pos < (size - off_struct)) { - uint32_t token = be32(structs + pos); + EosDtNode *stack[EOS_DT_MAX_DEPTH]; + uint32_t depth = 0; + uint32_t pos = 0; + + /* `pos` advances by at least 4 on every iteration and every read is bounds + * checked against `struct_len`, so this terminates on any input. */ + for (;;) { + uint32_t token; + if (dt_read_be32(structs, struct_len, pos, &token) != 0) return -1; pos += 4; switch (token) { case EOS_DT_BEGIN_NODE: { + uint32_t name_len; + if (dt_strlen_at(structs, struct_len, pos, &name_len) != 0) + return -1; + + uint32_t adv; + if (dt_align4(name_len + 1u, &adv) != 0) return -1; + if (adv > struct_len - pos) return -1; + if (dt->node_count >= EOS_DT_MAX_NODES) return -1; + if (depth >= EOS_DT_MAX_DEPTH) return -1; + EosDtNode *node = &dt->nodes[dt->node_count++]; memset(node, 0, sizeof(*node)); - - const char *name = (const char *)(structs + pos); - strncpy(node->name, name, EOS_DT_NAME_MAX - 1); - pos += align4((uint32_t)strlen(name) + 1); + dt_copy_str(node->name, sizeof(node->name), structs + pos, name_len); if (depth == 0) { dt->root = node; node->parent = NULL; } else { - node->parent = stack[depth - 1]; EosDtNode *parent = stack[depth - 1]; - if (parent->child_count < 16) + node->parent = parent; + if (parent->child_count < EOS_DT_MAX_CHILDREN) parent->children[parent->child_count++] = node; } - if (depth < 32) stack[depth] = node; - depth++; + stack[depth++] = node; + + pos += adv; break; } + case EOS_DT_END_NODE: - if (depth > 0) depth--; + /* A close with nothing open means the blob is malformed. */ + if (depth == 0) return -1; + depth--; break; case EOS_DT_PROP: { - uint32_t len = be32(structs + pos); pos += 4; - uint32_t nameoff = be32(structs + pos); pos += 4; - if (depth == 0) { pos += align4(len); break; } + uint32_t len, nameoff; + if (dt_read_be32(structs, struct_len, pos, &len) != 0) return -1; + pos += 4; + if (dt_read_be32(structs, struct_len, pos, &nameoff) != 0) return -1; + pos += 4; - EosDtNode *node = stack[depth - 1]; - if (node->prop_count < EOS_DT_MAX_PROPS) { - EosDtProp *prop = &node->props[node->prop_count++]; + /* The value, and its 4-byte-aligned padding, must lie inside the + * struct block. */ + uint32_t adv; + if (len > struct_len - pos) return -1; + if (dt_align4(len, &adv) != 0) return -1; + if (adv > struct_len - pos) return -1; + + if (depth > 0) { + /* The name lives in the strings block at an offset the blob + * chose, so it needs validating before it is dereferenced. */ + uint32_t pname_len; + if (dt_strlen_at(strings, string_len, nameoff, &pname_len) != 0) + return -1; const char *pname = (const char *)(strings + nameoff); - strncpy(prop->name, pname, EOS_DT_NAME_MAX - 1); - uint32_t clen = (len > EOS_DT_PROP_MAX) ? EOS_DT_PROP_MAX : len; - memcpy(prop->data, structs + pos, clen); - prop->len = clen; - - /* Cache model and compatible at root level */ - if (depth == 1) { - if (strcmp(pname, "model") == 0) { - strncpy(dt->model, (const char *)prop->data, 63); - dt->model[63] = '\0'; - } else if (strcmp(pname, "compatible") == 0) { - strncpy(dt->compatible, (const char *)prop->data, 127); - dt->compatible[127] = '\0'; + + EosDtNode *node = stack[depth - 1]; + if (node->prop_count < EOS_DT_MAX_PROPS) { + EosDtProp *prop = &node->props[node->prop_count++]; + dt_copy_str(prop->name, sizeof(prop->name), + (const uint8_t *)pname, pname_len); + + uint32_t clen = (len > EOS_DT_PROP_MAX) ? EOS_DT_PROP_MAX : len; + memcpy(prop->data, structs + pos, clen); + prop->len = clen; + + /* Cache model and compatible at root level */ + if (depth == 1) { + if (strcmp(pname, "model") == 0) { + dt_cache_string_prop(dt->model, sizeof(dt->model), prop); + } else if (strcmp(pname, "compatible") == 0) { + dt_cache_string_prop(dt->compatible, + sizeof(dt->compatible), prop); + } } + if (strcmp(pname, "phandle") == 0 && prop->len >= 4) + node->phandle = be32(prop->data); } - if (strcmp(pname, "phandle") == 0 && len >= 4) - node->phandle = be32(prop->data); } - pos += align4(len); + + pos += adv; break; } + case EOS_DT_NOP: break; + case EOS_DT_END: - return 0; + /* Every node that was opened must have been closed. */ + return (depth == 0) ? 0 : -1; + default: return -1; } } - return 0; } EosDtNode *eos_dt_find(EosDeviceTree *dt, const char *path) { @@ -136,12 +249,27 @@ EosDtNode *eos_dt_find(EosDeviceTree *dt, const char *path) { return current; } +/* Substring search restricted to `prop->len`. + * + * A property value is raw bytes, and a value that filled the property buffer + * has no terminator, so strstr() would run past the end of prop->data. */ +static int dt_prop_contains(const EosDtProp *prop, const char *needle) +{ + size_t nlen = strlen(needle); + if (nlen == 0) return 1; + if (prop->len < nlen) return 0; + for (uint32_t i = 0; i + nlen <= prop->len; i++) { + if (memcmp(prop->data + i, needle, nlen) == 0) return 1; + } + return 0; +} + EosDtNode *eos_dt_find_compatible(EosDeviceTree *dt, const char *compat) { if (!dt || !compat) return NULL; for (int i = 0; i < dt->node_count; i++) { for (int j = 0; j < dt->nodes[i].prop_count; j++) { if (strcmp(dt->nodes[i].props[j].name, "compatible") == 0) { - if (strstr((const char *)dt->nodes[i].props[j].data, compat)) + if (dt_prop_contains(&dt->nodes[i].props[j], compat)) return &dt->nodes[i]; } } @@ -194,9 +322,11 @@ int eos_dt_get_reg(EosDtNode *node, uint32_t *addr, uint32_t *size) { int eos_dt_get_irq(EosDtNode *node, int index) { const EosDtProp *p = eos_dt_get_prop(node, "interrupts"); - if (!p) return -1; - uint32_t offset = (uint32_t)(index * 4); - if (offset + 4 > p->len) return -1; + /* A negative index, or one large enough that index * 4 overflows, must be + * rejected before it is turned into an offset. */ + if (!p || index < 0 || index > (int)(UINT32_MAX / 4u)) return -1; + uint32_t offset = (uint32_t)index * 4u; + if (offset > p->len || p->len - offset < 4u) return -1; return (int)be32(p->data + offset); } diff --git a/core/eos/drivers/devicetree/devicetree.h b/core/eos/drivers/devicetree/devicetree.h index 9992185f..b4dc7dcd 100644 --- a/core/eos/drivers/devicetree/devicetree.h +++ b/core/eos/drivers/devicetree/devicetree.h @@ -13,9 +13,17 @@ extern "C" { #define EOS_DT_MAX_NODES 128 #define EOS_DT_MAX_PROPS 16 +#define EOS_DT_MAX_CHILDREN 16 #define EOS_DT_NAME_MAX 64 #define EOS_DT_PROP_MAX 256 +/* Deepest node nesting the parser will follow. A blob nested deeper than this + * is rejected rather than silently walking off the end of the node stack. */ +#define EOS_DT_MAX_DEPTH 32 + +/* Size of the flattened device tree header, in bytes. */ +#define EOS_DT_HEADER_SIZE 40 + #define EOS_DT_MAGIC 0xD00DFEED #define EOS_DT_BEGIN_NODE 0x00000001 #define EOS_DT_END_NODE 0x00000002 @@ -34,7 +42,7 @@ typedef struct eos_dt_node { EosDtProp props[EOS_DT_MAX_PROPS]; int prop_count; struct eos_dt_node *parent; - struct eos_dt_node *children[16]; + struct eos_dt_node *children[EOS_DT_MAX_CHILDREN]; int child_count; uint32_t phandle; } EosDtNode; diff --git a/core/eos/examples/ble-sensor/README.md b/core/eos/examples/ble-sensor/README.md index 9a90ab53..8cd68021 100644 --- a/core/eos/examples/ble-sensor/README.md +++ b/core/eos/examples/ble-sensor/README.md @@ -31,7 +31,7 @@ A complete IoT sensor node that reads temperature from an I2C sensor, computes a ```bash # Cross-compile for nRF52 cmake -B build \ - -DCMAKE_TOOLCHAIN_FILE=../../toolchains/arm-none-eabi.cmake \ + -DCMAKE_TOOLCHAIN_FILE=../../toolchains/arm-cortex-m4.cmake \ -DEOS_PRODUCT=iot cmake --build build diff --git a/core/eos/examples/blink-gpio/README.md b/core/eos/examples/blink-gpio/README.md index 4615fda9..36d52700 100644 --- a/core/eos/examples/blink-gpio/README.md +++ b/core/eos/examples/blink-gpio/README.md @@ -25,7 +25,7 @@ cmake --build build # ARM cross-compile cmake -B build \ - -DCMAKE_TOOLCHAIN_FILE=../../toolchains/arm-none-eabi.cmake \ + -DCMAKE_TOOLCHAIN_FILE=../../toolchains/arm-cortex-m4.cmake \ -DEOS_PRODUCT=iot cmake --build build ``` diff --git a/core/eos/examples/secure-ota/README.md b/core/eos/examples/secure-ota/README.md index 5ca1c931..f6d6710a 100644 --- a/core/eos/examples/secure-ota/README.md +++ b/core/eos/examples/secure-ota/README.md @@ -1,13 +1,16 @@ # Secure OTA — Firmware Update with Verification -Demonstrates a complete over-the-air firmware update flow: check for updates, download, verify SHA-256 integrity, apply to the inactive A/B slot, and reboot. Includes a watchdog task for safety. +Demonstrates a complete over-the-air firmware update flow: check for updates, download, check SHA-256 integrity, authenticate the image, apply to the inactive A/B slot, and reboot. Includes a watchdog task for safety. + +Integrity and authenticity are separate steps. `eos_ota_verify()` compares the download against the SHA-256 the update source declared, which catches a corrupted transfer and nothing more — that digest travels with the update, so anyone able to replace the image can replace the digest. `eos_ota_apply()` then calls the authenticator registered with `eos_ota_set_authenticator()`, which is where a signature check against a provisioned public key belongs. With no authenticator installed, `apply()` refuses. ## What it demonstrates -- OTA update lifecycle: check → download → verify → apply → reboot +- OTA update lifecycle: check → download → integrity → authenticate → apply → reboot - A/B slot management for safe firmware updates -- SHA-256 integrity verification before applying -- Rollback on verification failure +- SHA-256 integrity check before applying +- A vendor-signature authenticator gating `apply()` +- Abort on integrity or authenticity failure - Progress callback for download monitoring - Watchdog task to prevent hang during update @@ -27,8 +30,8 @@ Demonstrates a complete over-the-air firmware update flow: check for updates, do | Module | Header | Functions | |--------|--------|-----------| -| OTA | `eos/ota.h` | `eos_ota_init`, `eos_ota_check_update`, `eos_ota_begin`, `eos_ota_finish`, `eos_ota_verify`, `eos_ota_apply`, `eos_ota_rollback` | -| Crypto | `eos/crypto.h` | Used internally by OTA verify | +| OTA | `eos/ota.h` | `eos_ota_init`, `eos_ota_check_update`, `eos_ota_begin`, `eos_ota_finish`, `eos_ota_verify`, `eos_ota_set_authenticator`, `eos_ota_apply`, `eos_ota_rollback` | +| Crypto | `eos/crypto.h` | SHA-256 for the integrity check; the authenticator is where signature verification goes | | Kernel | `eos/kernel.h` | `eos_kernel_init`, `eos_task_create`, `eos_task_delay_ms` | | OS Services | `eos/os_services.h` | Watchdog management | diff --git a/core/eos/examples/secure-ota/main.c b/core/eos/examples/secure-ota/main.c index 5218c93d..b7c345ee 100644 --- a/core/eos/examples/secure-ota/main.c +++ b/core/eos/examples/secure-ota/main.c @@ -6,9 +6,15 @@ * @file main.c * @brief EoS Example: Secure OTA — Firmware update with verification * - * Demonstrates the OTA update flow: check for update, download, - * verify SHA-256 signature, apply to inactive slot, and reboot. - * Uses crypto services for integrity and watchdog for safety. + * Demonstrates the OTA update flow: check for update, download, check + * integrity, authenticate the image, apply to the inactive slot, and reboot. + * + * Integrity and authenticity are separate steps here, deliberately. + * eos_ota_verify() compares the download against the SHA-256 the update + * source declared, which catches a corrupted transfer but nothing else -- + * that digest arrives with the update, so whoever can replace the image can + * replace the digest. Deciding whether the image is one this device should + * run is the authenticator's job, below. */ #include @@ -23,6 +29,34 @@ #define FIRMWARE_URL "https://fw.example.com/firmware/v2.0.0.bin" #define FIRMWARE_VERSION "2.0.0" +/* The vendor public key this device trusts. A real deployment provisions this + * at manufacture and stores it somewhere immutable. */ +static const uint8_t g_vendor_pubkey[32] = { 0 }; + +/* Called by eos_ota_apply() with the digest the OTA service computed over the + * bytes it actually received. This is where a signature check belongs: verify + * `signature` over `digest` under g_vendor_pubkey and return 0 only then. + * + * services/crypto's verify routines are still stubs and refuse to run unless a + * build defines EOS_ALLOW_STUB_CRYPTO, so this returns -1 rather than pretend. + * Refusing to install is the correct behaviour for a device with no working + * verifier -- returning 0 here would install anything. */ +static int verify_vendor_signature(const uint8_t digest[32], + const uint8_t *signature, + size_t signature_len, + void *ctx) +{ + (void)digest; (void)ctx; + + if (!signature || signature_len == 0) { + printf("[ota] Update carries no signature — refusing\n"); + return -1; + } + + printf("[ota] No signature verifier is linked into this build — refusing\n"); + return -1; +} + static void progress_callback(uint8_t pct, void *ctx) { (void)ctx; @@ -70,22 +104,23 @@ static void ota_task(void *arg) continue; } - /* Step 4: Verify firmware integrity (SHA-256 + signature) */ - printf("[ota] Verifying firmware integrity...\n"); + /* Step 4: Integrity — did the download arrive intact? */ + printf("[ota] Checking firmware integrity...\n"); ret = eos_ota_verify(); if (ret != 0) { - printf("[ota] Verification FAILED: %d — aborting\n", ret); + printf("[ota] Integrity check FAILED: %d — aborting\n", ret); eos_ota_abort(); continue; } - printf("[ota] Verification passed\n"); + printf("[ota] Integrity check passed\n"); - /* Step 5: Apply update (mark inactive slot as next boot) */ - printf("[ota] Applying update...\n"); + /* Step 5: Authenticity — should this device run this image at all? + * apply() refuses outright if no authenticator is installed. */ + printf("[ota] Authenticating and applying update...\n"); ret = eos_ota_apply(); if (ret != 0) { - printf("[ota] Apply failed: %d\n", ret); - eos_ota_rollback(); + printf("[ota] Update rejected (unauthenticated or apply failed): %d\n", ret); + eos_ota_abort(); continue; } @@ -131,6 +166,10 @@ int main(void) /* Register progress callback */ eos_ota_set_progress_callback(progress_callback, NULL); + /* Without this, eos_ota_apply() refuses: an image whose only credential is + * a digest it shipped with is not installable. */ + eos_ota_set_authenticator(verify_vendor_signature, (void *)g_vendor_pubkey); + /* Print current firmware info */ eos_ota_status_t status; eos_ota_get_status(&status); diff --git a/core/eos/examples/ui-touchscreen/README.md b/core/eos/examples/ui-touchscreen/README.md index 0882678d..04c49a96 100644 --- a/core/eos/examples/ui-touchscreen/README.md +++ b/core/eos/examples/ui-touchscreen/README.md @@ -52,7 +52,7 @@ A complete touchscreen application that builds an interactive interface with LVG ```bash # Cross-compile for ARM target cmake -B build \ - -DCMAKE_TOOLCHAIN_FILE=../../toolchains/arm-none-eabi.cmake \ + -DCMAKE_TOOLCHAIN_FILE=../../toolchains/arm-cortex-m4.cmake \ -DEOS_PRODUCT=hmi cmake --build build diff --git a/core/eos/hal/include/eos/hal.h b/core/eos/hal/include/eos/hal.h index 660c01c6..afe58ac7 100644 --- a/core/eos/hal/include/eos/hal.h +++ b/core/eos/hal/include/eos/hal.h @@ -29,10 +29,43 @@ extern "C" { * Initialize the HAL subsystem for the current platform. * Must be called before any other HAL function. * + * Selects the backend compiled for this platform when the application has not + * registered one of its own. A build that wants different hardware behaviour + * calls eos_hal_register_backend() first, and that choice is kept. + * * @return 0 on success, negative error code on failure. */ int eos_hal_init(void); +/** + * Register the backend for this platform. + * + * Called for you by eos_hal_init(). Declared because a board port that + * layers on top of the platform backend needs to name it, and because an + * undeclared registrar is one nobody can find. + * + * Exactly one of these is *defined* in a given build -- CMake compiles either + * hal_linux.c or hal_rtos.c -- but both are declared unconditionally. Guarding + * the declarations on __linux__ meant hal_common.c could not even name the + * registrar CMake had compiled for it on a non-Linux host, and a declaration + * that is never referenced costs nothing. + */ +/* EOS_HAL_HOSTED: this translation unit is being compiled against a hosted + * POSIX environment (Linux, macOS, BSDs), as opposed to Windows or bare + * metal. The ARM + * Cortex-M cross build also compiles hal_linux.c -- EOS_PLATFORM defaults to + * "linux" there -- and relies on this evaluating to 0 so that file stays an + * empty translation unit; arm-none-eabi defines none of these macros. */ +#if defined(__linux__) || (defined(EOS_HAL_BACKEND_HOST) && (defined(__unix__) || defined(__APPLE__))) +#define EOS_HAL_HOSTED 1 +#else +#define EOS_HAL_HOSTED 0 +#endif + +void eos_hal_linux_register(void); +void eos_hal_win32_register(void); +void eos_hal_rtos_register(void); + /** * Deinitialize the HAL subsystem, releasing all resources. */ diff --git a/core/eos/hal/src/hal_common.c b/core/eos/hal/src/hal_common.c index a4d69397..848c7f6c 100644 --- a/core/eos/hal/src/hal_common.c +++ b/core/eos/hal/src/hal_common.c @@ -14,13 +14,45 @@ static const eos_hal_backend_t *active_backend = NULL; +/* Distinguishes "no backend has ever been chosen" from "a backend was chosen + * and it was NULL". eos_hal_init() fills the first case with the platform + * default; the second is a deliberate act by the application and is left + * alone, because a HAL that re-arms itself after being disarmed is worse than + * one that never armed. */ +static int backend_chosen = 0; + void eos_hal_register_backend(const eos_hal_backend_t *backend) { active_backend = backend; + backend_chosen = 1; } int eos_hal_init(void) { + /* hal.h promises initialisation "for the current platform". Until now this + * returned -1 unless the application had already called + * eos_hal_register_backend() itself, which nothing in the tree, none of + * the examples, and none of the scaffolded templates did — so every HAL + * call on a host build silently did nothing and the generated hello-world + * produced no output. + * + * An application that registers its own backend before calling init keeps + * it, and so does one that deliberately registers NULL: the default only + * fills a slot nobody has set. */ + if (!active_backend && !backend_chosen) { + /* EOS_HAL_BACKEND_* is set by CMake next to the target_sources() call + * that picks the backend file, so this cannot select a register + * function whose definition was not compiled. Testing __linux__ here + * did exactly that on every non-Linux host build. */ +#if defined(EOS_HAL_BACKEND_HOST) + eos_hal_linux_register(); +#elif defined(EOS_HAL_BACKEND_WIN32) + eos_hal_win32_register(); +#else + eos_hal_rtos_register(); +#endif + } + if (!active_backend || !active_backend->init) return -1; return active_backend->init(); } diff --git a/core/eos/hal/src/hal_linux.c b/core/eos/hal/src/hal_linux.c index 73ba53d9..89612cf8 100644 --- a/core/eos/hal/src/hal_linux.c +++ b/core/eos/hal/src/hal_linux.c @@ -16,7 +16,15 @@ #include #include -#ifdef __linux__ +/* The host backend. Named for Linux, but it uses only clock_gettime() and + * nanosleep() -- POSIX, and available on macOS and the BSDs too. CMake decides + * which backend file to compile and sets EOS_HAL_BACKEND_HOST when it picks + * this one; __linux__ remains as a fallback for builds that compile this file + * directly without CMake. Keying on __linux__ alone meant a macOS host build + * compiled this file to an empty translation unit and then had no backend at + * all. */ +#include "eos/hal.h" +#if EOS_HAL_HOSTED #include #include @@ -280,4 +288,4 @@ void eos_hal_linux_register(void) eos_hal_register_backend(&linux_backend); } -#endif /* __linux__ */ +#endif /* EOS_HAL_HOSTED */ diff --git a/core/eos/hal/src/hal_rtos.c b/core/eos/hal/src/hal_rtos.c index 15d10ca3..cede44a0 100644 --- a/core/eos/hal/src/hal_rtos.c +++ b/core/eos/hal/src/hal_rtos.c @@ -4,211 +4,453 @@ /** * @file hal_rtos.c - * @brief EoS HAL — RTOS platform backend + * @brief EoS HAL — RTOS platform backend with real register-level drivers * - * Implements the HAL interface using direct register access for bare-metal - * and RTOS targets. Designed for Cortex-M and Cortex-R class MCUs - * with memory-mapped I/O. + * STM32F4 implementations behind EOS_MCU_STM32F4 guards. + * Generic stubs as fallback for other MCUs. */ #include #include -#if !defined(__linux__) - -/* ---- Platform-specific register access ---- */ +#include "eos/hal.h" +#if !EOS_HAL_HOSTED #define REG32(addr) (*(volatile uint32_t *)(addr)) -/* ---- Internal state ---- */ - static volatile uint32_t systick_ms = 0; static bool hal_initialized = false; -/* ---- GPIO (memory-mapped) ---- */ +/* ================================================================ + * STM32F4 Register-Level GPIO + * ================================================================ */ +#if defined(EOS_MCU_STM32F4) + +#define GPIOA_BASE 0x40020000U +#define GPIOB_BASE 0x40020400U +#define GPIOC_BASE 0x40020800U +#define GPIOD_BASE 0x40020C00U +#define GPIO_MODER(base) REG32((base) + 0x00) +#define GPIO_OTYPER(base) REG32((base) + 0x04) +#define GPIO_OSPEEDR(base) REG32((base) + 0x08) +#define GPIO_PUPDR(base) REG32((base) + 0x0C) +#define GPIO_IDR(base) REG32((base) + 0x10) +#define GPIO_ODR(base) REG32((base) + 0x14) +#define GPIO_BSRR(base) REG32((base) + 0x18) +#define GPIO_AFRL(base) REG32((base) + 0x20) +#define GPIO_AFRH(base) REG32((base) + 0x24) + +/* RCC AHB1ENR for GPIO clocks */ +#define RCC_AHB1ENR REG32(0x40023830U) + +static uint32_t gpio_get_base(uint16_t pin) { + uint8_t port = (pin >> 8) & 0xF; + switch (port) { + case 0: return GPIOA_BASE; + case 1: return GPIOB_BASE; + case 2: return GPIOC_BASE; + case 3: return GPIOD_BASE; + default: return GPIOA_BASE; + } +} static int rtos_gpio_init(const eos_gpio_config_t *cfg) { - (void)cfg; - /* Platform-specific GPIO bank and pin configuration. - * Actual implementation depends on the MCU (STM32, nRF52, etc.). - * Board-specific code should override via the HAL backend. */ + if (!cfg) return -1; + uint32_t base = gpio_get_base(cfg->pin); + uint8_t port = (cfg->pin >> 8) & 0xF; + uint8_t pin_num = cfg->pin & 0xFF; + if (pin_num > 15) return -1; + + /* Enable GPIO clock */ + RCC_AHB1ENR |= (1U << port); + + /* Configure MODER */ + uint32_t moder = GPIO_MODER(base); + moder &= ~(3U << (pin_num * 2)); + moder |= ((uint32_t)cfg->mode << (pin_num * 2)); + GPIO_MODER(base) = moder; + + /* Configure speed */ + uint32_t ospeedr = GPIO_OSPEEDR(base); + ospeedr &= ~(3U << (pin_num * 2)); + ospeedr |= ((uint32_t)cfg->speed << (pin_num * 2)); + GPIO_OSPEEDR(base) = ospeedr; + + /* Configure pull-up/pull-down */ + uint32_t pupdr = GPIO_PUPDR(base); + pupdr &= ~(3U << (pin_num * 2)); + pupdr |= ((uint32_t)cfg->pull << (pin_num * 2)); + GPIO_PUPDR(base) = pupdr; + + /* Configure alternate function if AF mode */ + if (cfg->mode == EOS_GPIO_AF) { + if (pin_num < 8) { + uint32_t afrl = GPIO_AFRL(base); + afrl &= ~(0xFU << (pin_num * 4)); + afrl |= ((uint32_t)cfg->af_num << (pin_num * 4)); + GPIO_AFRL(base) = afrl; + } else { + uint32_t afrh = GPIO_AFRH(base); + afrh &= ~(0xFU << ((pin_num - 8) * 4)); + afrh |= ((uint32_t)cfg->af_num << ((pin_num - 8) * 4)); + GPIO_AFRH(base) = afrh; + } + } return 0; } -static void rtos_gpio_deinit(uint16_t pin) -{ - (void)pin; +static void rtos_gpio_deinit(uint16_t pin) { + uint32_t base = gpio_get_base(pin); + uint8_t pin_num = pin & 0xFF; + GPIO_MODER(base) &= ~(3U << (pin_num * 2)); } -static void rtos_gpio_write(uint16_t pin, bool value) -{ - (void)pin; - (void)value; +static void rtos_gpio_write(uint16_t pin, bool value) { + uint32_t base = gpio_get_base(pin); + uint8_t pin_num = pin & 0xFF; + GPIO_BSRR(base) = value ? (1U << pin_num) : (1U << (pin_num + 16)); } -static bool rtos_gpio_read(uint16_t pin) -{ - (void)pin; - return false; +static bool rtos_gpio_read(uint16_t pin) { + uint32_t base = gpio_get_base(pin); + uint8_t pin_num = pin & 0xFF; + return (GPIO_IDR(base) & (1U << pin_num)) != 0; } -static void rtos_gpio_toggle(uint16_t pin) -{ - (void)pin; +static void rtos_gpio_toggle(uint16_t pin) { + uint32_t base = gpio_get_base(pin); + uint8_t pin_num = pin & 0xFF; + GPIO_ODR(base) ^= (1U << pin_num); } static int rtos_gpio_set_irq(uint16_t pin, eos_gpio_edge_t edge, - eos_gpio_callback_t cb, void *ctx) -{ + eos_gpio_callback_t cb, void *ctx) { (void)pin; (void)edge; (void)cb; (void)ctx; + /* EXTI configuration would go here */ return 0; } -/* ---- UART (register-based) ---- */ +/* ================================================================ + * STM32F4 UART (USART1/USART2) + * ================================================================ */ + +#define USART1_BASE 0x40011000U +#define USART2_BASE 0x40004400U +#define USART_SR(base) REG32((base) + 0x00) +#define USART_DR(base) REG32((base) + 0x04) +#define USART_BRR(base) REG32((base) + 0x08) +#define USART_CR1(base) REG32((base) + 0x0C) +#define RCC_APB2ENR REG32(0x40023844U) +#define RCC_APB1ENR REG32(0x40023840U) + +#define USART_CR1_UE (1U << 13) +#define USART_CR1_TE (1U << 3) +#define USART_CR1_RE (1U << 2) +#define USART_SR_TXE (1U << 7) +#define USART_SR_RXNE (1U << 5) +#define USART_SR_TC (1U << 6) + +static uint32_t uart_get_base(uint8_t port) { + return (port == 0) ? USART1_BASE : USART2_BASE; +} -static int rtos_uart_init(const eos_uart_config_t *cfg) -{ - (void)cfg; +static int rtos_uart_init(const eos_uart_config_t *cfg) { + if (!cfg) return -1; + uint32_t base = uart_get_base(cfg->port); + + /* Enable clock */ + if (cfg->port == 0) RCC_APB2ENR |= (1U << 4); /* USART1 */ + else RCC_APB1ENR |= (1U << 17); /* USART2 */ + + /* Configure baud rate (assuming 16MHz APB clock) */ + uint32_t apb_clk = (cfg->port == 0) ? 84000000U : 42000000U; + USART_BRR(base) = apb_clk / cfg->baudrate; + + /* Enable USART, TX, RX */ + USART_CR1(base) = USART_CR1_UE | USART_CR1_TE | USART_CR1_RE; return 0; } -static void rtos_uart_deinit(uint8_t port) -{ - (void)port; +static void rtos_uart_deinit(uint8_t port) { + uint32_t base = uart_get_base(port); + USART_CR1(base) = 0; } -static int rtos_uart_write(uint8_t port, const uint8_t *data, size_t len) -{ - (void)port; (void)data; (void)len; +static int rtos_uart_write(uint8_t port, const uint8_t *data, size_t len) { + uint32_t base = uart_get_base(port); + for (size_t i = 0; i < len; i++) { + while (!(USART_SR(base) & USART_SR_TXE)) {} + USART_DR(base) = data[i]; + } + while (!(USART_SR(base) & USART_SR_TC)) {} + return (int)len; +} + +static int rtos_uart_read(uint8_t port, uint8_t *data, size_t len, uint32_t timeout_ms) { + uint32_t base = uart_get_base(port); + uint32_t start = systick_ms; + for (size_t i = 0; i < len; i++) { + while (!(USART_SR(base) & USART_SR_RXNE)) { + if (timeout_ms != 0xFFFFFFFF && (systick_ms - start) >= timeout_ms) + return (int)i; + } + data[i] = (uint8_t)(USART_DR(base) & 0xFF); + } + return (int)len; +} + +/* ================================================================ + * STM32F4 SPI (SPI1) + * ================================================================ */ + +#define SPI1_BASE 0x40013000U +#define SPI_CR1(base) REG32((base) + 0x00) +#define SPI_SR(base) REG32((base) + 0x08) +#define SPI_DR(base) REG32((base) + 0x0C) +#define SPI_SR_TXE (1U << 1) +#define SPI_SR_RXNE (1U << 0) +#define SPI_SR_BSY (1U << 7) +#define SPI_CR1_SPE (1U << 6) +#define SPI_CR1_MSTR (1U << 2) + +static int rtos_spi_init(const eos_spi_config_t *cfg) { + if (!cfg) return -1; + RCC_APB2ENR |= (1U << 12); /* SPI1 clock */ + uint32_t cr1 = SPI_CR1_MSTR | SPI_CR1_SPE; + cr1 |= ((uint32_t)cfg->mode << 0); /* CPOL/CPHA */ + /* Baud rate prescaler: approximate from clock_hz */ + uint32_t div = 84000000U / cfg->clock_hz; + uint32_t br = 0; + while (div > 2 && br < 7) { div >>= 1; br++; } + cr1 |= (br << 3); + SPI_CR1(SPI1_BASE) = cr1; return 0; } -static int rtos_uart_read(uint8_t port, uint8_t *data, size_t len, uint32_t timeout_ms) -{ - (void)port; (void)data; (void)len; (void)timeout_ms; +static void rtos_spi_deinit(uint8_t port) { + (void)port; + SPI_CR1(SPI1_BASE) = 0; +} + +static int rtos_spi_transfer(uint8_t port, const uint8_t *tx, uint8_t *rx, size_t len) { + (void)port; + uint32_t base = SPI1_BASE; + for (size_t i = 0; i < len; i++) { + while (!(SPI_SR(base) & SPI_SR_TXE)) {} + SPI_DR(base) = tx ? tx[i] : 0xFF; + while (!(SPI_SR(base) & SPI_SR_RXNE)) {} + uint8_t rd = (uint8_t)(SPI_DR(base) & 0xFF); + if (rx) rx[i] = rd; + } + while (SPI_SR(base) & SPI_SR_BSY) {} + return (int)len; +} + +/* ================================================================ + * STM32F4 I2C (I2C1) + * ================================================================ */ + +#define I2C1_BASE 0x40005400U +#define I2C_CR1(base) REG32((base) + 0x00) +#define I2C_CR2(base) REG32((base) + 0x04) +#define I2C_OAR1(base) REG32((base) + 0x08) +#define I2C_DR_REG(base) REG32((base) + 0x10) +#define I2C_SR1(base) REG32((base) + 0x14) +#define I2C_SR2(base) REG32((base) + 0x18) +#define I2C_CCR(base) REG32((base) + 0x1C) +#define I2C_CR1_PE (1U << 0) +#define I2C_CR1_START (1U << 8) +#define I2C_CR1_STOP (1U << 9) +#define I2C_CR1_ACK (1U << 10) +#define I2C_SR1_SB (1U << 0) +#define I2C_SR1_ADDR (1U << 1) +#define I2C_SR1_TXE (1U << 7) +#define I2C_SR1_RXNE (1U << 6) + +static int rtos_i2c_init(const eos_i2c_config_t *cfg) { + if (!cfg) return -1; + RCC_APB1ENR |= (1U << 21); /* I2C1 clock */ + I2C_CR1(I2C1_BASE) = 0; /* Reset */ + I2C_CR2(I2C1_BASE) = 42; /* 42 MHz APB1 */ + /* Standard mode 100kHz: CCR = APB1_CLK / (2 * 100000) */ + I2C_CCR(I2C1_BASE) = 42000000U / (2 * (cfg->clock_hz ? cfg->clock_hz : 100000)); + I2C_CR1(I2C1_BASE) = I2C_CR1_PE | I2C_CR1_ACK; return 0; } -/* ---- SPI (register-based) ---- */ +static void rtos_i2c_deinit(uint8_t port) { + (void)port; + I2C_CR1(I2C1_BASE) = 0; +} -static int rtos_spi_init(const eos_spi_config_t *cfg) { (void)cfg; return 0; } -static void rtos_spi_deinit(uint8_t port) { (void)port; } -static int rtos_spi_transfer(uint8_t port, const uint8_t *tx, uint8_t *rx, size_t len) -{ - (void)port; (void)tx; (void)rx; (void)len; +static int rtos_i2c_write(uint8_t port, uint16_t addr, const uint8_t *data, size_t len) { + (void)port; + uint32_t base = I2C1_BASE; + I2C_CR1(base) |= I2C_CR1_START; + while (!(I2C_SR1(base) & I2C_SR1_SB)) {} + I2C_DR_REG(base) = (uint32_t)(addr << 1); /* Write */ + while (!(I2C_SR1(base) & I2C_SR1_ADDR)) {} + (void)I2C_SR2(base); /* Clear ADDR */ + for (size_t i = 0; i < len; i++) { + while (!(I2C_SR1(base) & I2C_SR1_TXE)) {} + I2C_DR_REG(base) = data[i]; + } + while (!(I2C_SR1(base) & I2C_SR1_TXE)) {} + I2C_CR1(base) |= I2C_CR1_STOP; + return (int)len; +} + +static int rtos_i2c_read(uint8_t port, uint16_t addr, uint8_t *data, size_t len) { + (void)port; + uint32_t base = I2C1_BASE; + I2C_CR1(base) |= I2C_CR1_START | I2C_CR1_ACK; + while (!(I2C_SR1(base) & I2C_SR1_SB)) {} + I2C_DR_REG(base) = (uint32_t)(addr << 1) | 1; /* Read */ + while (!(I2C_SR1(base) & I2C_SR1_ADDR)) {} + (void)I2C_SR2(base); + for (size_t i = 0; i < len; i++) { + if (i == len - 1) I2C_CR1(base) &= ~I2C_CR1_ACK; /* NACK last byte */ + while (!(I2C_SR1(base) & I2C_SR1_RXNE)) {} + data[i] = (uint8_t)(I2C_DR_REG(base) & 0xFF); + } + I2C_CR1(base) |= I2C_CR1_STOP; + return (int)len; +} + +/* ================================================================ + * STM32F4 Timer (TIM2) + * ================================================================ */ + +#define TIM2_BASE 0x40000000U +#define TIM_CR1(base) REG32((base) + 0x00) +#define TIM_DIER(base) REG32((base) + 0x0C) +#define TIM_SR_REG(base) REG32((base) + 0x10) +#define TIM_CNT(base) REG32((base) + 0x24) +#define TIM_PSC(base) REG32((base) + 0x28) +#define TIM_ARR(base) REG32((base) + 0x2C) +#define TIM_CR1_CEN (1U << 0) +#define TIM_CR1_ARPE (1U << 7) + +static int rtos_timer_init(const eos_timer_config_t *cfg) { + if (!cfg) return -1; + RCC_APB1ENR |= (1U << 0); /* TIM2 clock */ + TIM_CR1(TIM2_BASE) = 0; + /* 1 MHz timer clock: PSC = APB1_CLK / 1MHz - 1 */ + TIM_PSC(TIM2_BASE) = 42 - 1; + TIM_ARR(TIM2_BASE) = cfg->period_us; + if (cfg->auto_reload) TIM_CR1(TIM2_BASE) |= TIM_CR1_ARPE; + TIM_DIER(TIM2_BASE) = 1; /* Update interrupt enable */ return 0; } -/* ---- I2C (register-based) ---- */ +static void rtos_timer_deinit(uint8_t timer_id) { + (void)timer_id; + TIM_CR1(TIM2_BASE) = 0; +} -static int rtos_i2c_init(const eos_i2c_config_t *cfg) { (void)cfg; return 0; } -static void rtos_i2c_deinit(uint8_t port) { (void)port; } -static int rtos_i2c_write(uint8_t port, uint16_t addr, const uint8_t *data, size_t len) -{ - (void)port; (void)addr; (void)data; (void)len; +static int rtos_timer_start(uint8_t timer_id) { + (void)timer_id; + TIM_CR1(TIM2_BASE) |= TIM_CR1_CEN; return 0; } -static int rtos_i2c_read(uint8_t port, uint16_t addr, uint8_t *data, size_t len) -{ - (void)port; (void)addr; (void)data; (void)len; + +static int rtos_timer_stop(uint8_t timer_id) { + (void)timer_id; + TIM_CR1(TIM2_BASE) &= ~TIM_CR1_CEN; return 0; } -/* ---- Timer (hardware timer) ---- */ +#else /* Generic stubs for non-STM32F4 targets */ +static int rtos_gpio_init(const eos_gpio_config_t *cfg) { (void)cfg; return 0; } +static void rtos_gpio_deinit(uint16_t pin) { (void)pin; } +static void rtos_gpio_write(uint16_t pin, bool value) { (void)pin; (void)value; } +static bool rtos_gpio_read(uint16_t pin) { (void)pin; return false; } +static void rtos_gpio_toggle(uint16_t pin) { (void)pin; } +static int rtos_gpio_set_irq(uint16_t pin, eos_gpio_edge_t edge, + eos_gpio_callback_t cb, void *ctx) { + (void)pin; (void)edge; (void)cb; (void)ctx; return 0; +} +static int rtos_uart_init(const eos_uart_config_t *cfg) { (void)cfg; return 0; } +static void rtos_uart_deinit(uint8_t port) { (void)port; } +static int rtos_uart_write(uint8_t port, const uint8_t *data, size_t len) { + (void)port; (void)data; (void)len; return 0; +} +static int rtos_uart_read(uint8_t port, uint8_t *data, size_t len, uint32_t timeout_ms) { + (void)port; (void)data; (void)len; (void)timeout_ms; return 0; +} +static int rtos_spi_init(const eos_spi_config_t *cfg) { (void)cfg; return 0; } +static void rtos_spi_deinit(uint8_t port) { (void)port; } +static int rtos_spi_transfer(uint8_t port, const uint8_t *tx, uint8_t *rx, size_t len) { + (void)port; (void)tx; (void)rx; (void)len; return 0; +} +static int rtos_i2c_init(const eos_i2c_config_t *cfg) { (void)cfg; return 0; } +static void rtos_i2c_deinit(uint8_t port) { (void)port; } +static int rtos_i2c_write(uint8_t port, uint16_t addr, const uint8_t *data, size_t len) { + (void)port; (void)addr; (void)data; (void)len; return 0; +} +static int rtos_i2c_read(uint8_t port, uint16_t addr, uint8_t *data, size_t len) { + (void)port; (void)addr; (void)data; (void)len; return 0; +} static int rtos_timer_init(const eos_timer_config_t *cfg) { (void)cfg; return 0; } static void rtos_timer_deinit(uint8_t timer_id) { (void)timer_id; } static int rtos_timer_start(uint8_t timer_id) { (void)timer_id; return 0; } static int rtos_timer_stop(uint8_t timer_id) { (void)timer_id; return 0; } -/* ---- Interrupt control ---- */ +#endif /* EOS_MCU_STM32F4 */ -static void rtos_irq_disable(void) -{ +/* ================================================================ + * Common (all RTOS targets) + * ================================================================ */ + +static void rtos_irq_disable(void) { #if defined(__ARM_ARCH) __asm volatile ("cpsid i" ::: "memory"); #endif } -static void rtos_irq_enable(void) -{ +static void rtos_irq_enable(void) { #if defined(__ARM_ARCH) __asm volatile ("cpsie i" ::: "memory"); #endif } -/* ---- Timing ---- */ - -static void rtos_delay_ms(uint32_t ms) -{ +static void rtos_delay_ms(uint32_t ms) { uint32_t start = systick_ms; - while ((systick_ms - start) < ms) { - /* busy-wait — in a real RTOS, yield to scheduler */ - } -} - -static uint32_t rtos_get_tick_ms(void) -{ - return systick_ms; + while ((systick_ms - start) < ms) {} } -/* Called from SysTick_Handler (Cortex-M) or RTI timer ISR (Cortex-R) */ -void eos_hal_systick_handler(void) -{ - systick_ms++; -} +static uint32_t rtos_get_tick_ms(void) { return systick_ms; } -/* ---- Init ---- */ +void eos_hal_systick_handler(void) { systick_ms++; } -static int rtos_hal_init(void) -{ +static int rtos_hal_init(void) { systick_ms = 0; hal_initialized = true; return 0; } -static void rtos_hal_deinit(void) -{ - hal_initialized = false; -} - -/* ---- Backend registration ---- */ +static void rtos_hal_deinit(void) { hal_initialized = false; } static const eos_hal_backend_t rtos_backend = { - .name = "rtos", - .init = rtos_hal_init, - .deinit = rtos_hal_deinit, - .delay_ms = rtos_delay_ms, - .get_tick_ms = rtos_get_tick_ms, - .gpio_init = rtos_gpio_init, - .gpio_deinit = rtos_gpio_deinit, - .gpio_write = rtos_gpio_write, - .gpio_read = rtos_gpio_read, - .gpio_toggle = rtos_gpio_toggle, - .gpio_set_irq = rtos_gpio_set_irq, - .uart_init = rtos_uart_init, - .uart_deinit = rtos_uart_deinit, - .uart_write = rtos_uart_write, - .uart_read = rtos_uart_read, - .spi_init = rtos_spi_init, - .spi_deinit = rtos_spi_deinit, - .spi_transfer = rtos_spi_transfer, - .i2c_init = rtos_i2c_init, - .i2c_deinit = rtos_i2c_deinit, - .i2c_write = rtos_i2c_write, - .i2c_read = rtos_i2c_read, - .timer_init = rtos_timer_init, - .timer_deinit = rtos_timer_deinit, - .timer_start = rtos_timer_start, - .timer_stop = rtos_timer_stop, - .irq_disable = rtos_irq_disable, - .irq_enable = rtos_irq_enable, + .name = "rtos", + .init = rtos_hal_init, .deinit = rtos_hal_deinit, + .delay_ms = rtos_delay_ms, .get_tick_ms = rtos_get_tick_ms, + .gpio_init = rtos_gpio_init, .gpio_deinit = rtos_gpio_deinit, + .gpio_write = rtos_gpio_write, .gpio_read = rtos_gpio_read, + .gpio_toggle = rtos_gpio_toggle, .gpio_set_irq = rtos_gpio_set_irq, + .uart_init = rtos_uart_init, .uart_deinit = rtos_uart_deinit, + .uart_write = rtos_uart_write, .uart_read = rtos_uart_read, + .spi_init = rtos_spi_init, .spi_deinit = rtos_spi_deinit, + .spi_transfer = rtos_spi_transfer, + .i2c_init = rtos_i2c_init, .i2c_deinit = rtos_i2c_deinit, + .i2c_write = rtos_i2c_write, .i2c_read = rtos_i2c_read, + .timer_init = rtos_timer_init, .timer_deinit = rtos_timer_deinit, + .timer_start = rtos_timer_start, .timer_stop = rtos_timer_stop, + .irq_disable = rtos_irq_disable, .irq_enable = rtos_irq_enable, }; -void eos_hal_rtos_register(void) -{ - eos_hal_register_backend(&rtos_backend); -} +void eos_hal_rtos_register(void) { eos_hal_register_backend(&rtos_backend); } #endif /* !__linux__ */ diff --git a/core/eos/hal/src/hal_win32.c b/core/eos/hal/src/hal_win32.c new file mode 100644 index 00000000..562fc0c7 --- /dev/null +++ b/core/eos/hal/src/hal_win32.c @@ -0,0 +1,95 @@ +// SPDX-License-Identifier: MIT +// Copyright (c) 2026 EoS Project + +/** + * @file hal_win32.c + * @brief EoS HAL backend for hosted Windows builds. + * + * Windows has a host clock but no POSIX GPIO/sysfs implementation. Peripheral + * operations remain explicit no-op stubs, matching the generic host contract. + */ + +#include +#include + +#if defined(_WIN32) + +static bool hal_initialized; +static ULONGLONG host_start_tick; + +static int win32_gpio_init(const eos_gpio_config_t *cfg) { (void)cfg; return 0; } +static void win32_gpio_deinit(uint16_t pin) { (void)pin; } +static void win32_gpio_write(uint16_t pin, bool value) { (void)pin; (void)value; } +static bool win32_gpio_read(uint16_t pin) { (void)pin; return false; } +static void win32_gpio_toggle(uint16_t pin) { (void)pin; } +static int win32_gpio_set_irq(uint16_t pin, eos_gpio_edge_t edge, + eos_gpio_callback_t cb, void *ctx) { + (void)pin; (void)edge; (void)cb; (void)ctx; return 0; +} + +static int win32_uart_init(const eos_uart_config_t *cfg) { (void)cfg; return 0; } +static void win32_uart_deinit(uint8_t port) { (void)port; } +static int win32_uart_write(uint8_t port, const uint8_t *data, size_t len) { + (void)port; (void)data; (void)len; return 0; +} +static int win32_uart_read(uint8_t port, uint8_t *data, size_t len, uint32_t timeout_ms) { + (void)port; (void)data; (void)len; (void)timeout_ms; return 0; +} + +static int win32_spi_init(const eos_spi_config_t *cfg) { (void)cfg; return 0; } +static void win32_spi_deinit(uint8_t port) { (void)port; } +static int win32_spi_transfer(uint8_t port, const uint8_t *tx, uint8_t *rx, size_t len) { + (void)port; (void)tx; (void)rx; (void)len; return 0; +} + +static int win32_i2c_init(const eos_i2c_config_t *cfg) { (void)cfg; return 0; } +static void win32_i2c_deinit(uint8_t port) { (void)port; } +static int win32_i2c_write(uint8_t port, uint16_t addr, const uint8_t *data, size_t len) { + (void)port; (void)addr; (void)data; (void)len; return 0; +} +static int win32_i2c_read(uint8_t port, uint16_t addr, uint8_t *data, size_t len) { + (void)port; (void)addr; (void)data; (void)len; return 0; +} + +static int win32_timer_init(const eos_timer_config_t *cfg) { (void)cfg; return 0; } +static void win32_timer_deinit(uint8_t timer_id) { (void)timer_id; } +static int win32_timer_start(uint8_t timer_id) { (void)timer_id; return 0; } +static int win32_timer_stop(uint8_t timer_id) { (void)timer_id; return 0; } +static void win32_irq_disable(void) {} +static void win32_irq_enable(void) {} + +static void win32_delay_ms(uint32_t ms) { Sleep(ms); } + +static uint32_t win32_get_tick_ms(void) { + return hal_initialized ? (uint32_t)(GetTickCount64() - host_start_tick) : 0; +} + +static int win32_hal_init(void) { + host_start_tick = GetTickCount64(); + hal_initialized = true; + return 0; +} + +static void win32_hal_deinit(void) { hal_initialized = false; } + +static const eos_hal_backend_t win32_backend = { + .name = "win32", + .init = win32_hal_init, .deinit = win32_hal_deinit, + .delay_ms = win32_delay_ms, .get_tick_ms = win32_get_tick_ms, + .gpio_init = win32_gpio_init, .gpio_deinit = win32_gpio_deinit, + .gpio_write = win32_gpio_write, .gpio_read = win32_gpio_read, + .gpio_toggle = win32_gpio_toggle, .gpio_set_irq = win32_gpio_set_irq, + .uart_init = win32_uart_init, .uart_deinit = win32_uart_deinit, + .uart_write = win32_uart_write, .uart_read = win32_uart_read, + .spi_init = win32_spi_init, .spi_deinit = win32_spi_deinit, + .spi_transfer = win32_spi_transfer, + .i2c_init = win32_i2c_init, .i2c_deinit = win32_i2c_deinit, + .i2c_write = win32_i2c_write, .i2c_read = win32_i2c_read, + .timer_init = win32_timer_init, .timer_deinit = win32_timer_deinit, + .timer_start = win32_timer_start, .timer_stop = win32_timer_stop, + .irq_disable = win32_irq_disable, .irq_enable = win32_irq_enable, +}; + +void eos_hal_win32_register(void) { eos_hal_register_backend(&win32_backend); } + +#endif /* _WIN32 */ diff --git a/core/eos/include/eos/backend.h b/core/eos/include/eos/backend.h index ddea672a..9dee5445 100644 --- a/core/eos/include/eos/backend.h +++ b/core/eos/include/eos/backend.h @@ -2,42 +2,80 @@ // Copyright (c) 2026 EoS Project // ISO/IEC 25000 | ISO/IEC/IEEE 15288:2023 +/** + * @file backend.h + * @brief Build backend interface and registry. + * + * Each build system (CMake, Ninja, Make, Kbuild, Zephyr, FreeRTOS, NuttX) + * is wrapped as an EosBackend with four operations: configure, build, + * install, and clean. Backends are registered at startup and looked up + * by name or EosBuildType when the scheduler needs to build a node. + */ + #ifndef EOS_BACKEND_H #define EOS_BACKEND_H #include "eos/types.h" +#include "eos/error.h" -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct EosBackend EosBackend; - -typedef int (*eos_backend_configure_fn)(EosBackend *be, const char *src_dir, - const char *build_dir, - const char *toolchain, - const EosKeyValue *options, - int option_count); -typedef int (*eos_backend_build_fn)(EosBackend *be, const char *build_dir, int jobs); -typedef int (*eos_backend_install_fn)(EosBackend *be, const char *build_dir, - const char *install_dir); - -struct EosBackend { - const char *name; - EosBuildType type; - eos_backend_configure_fn configure; - eos_backend_build_fn build; - eos_backend_install_fn install; - void *user_data; -}; - -int eos_backend_register(EosBackend *be); -EosBackend *eos_backend_find_by_type(EosBuildType type); +#define EOS_MAX_BACKENDS 16 /**< Maximum registered backends */ + +/** + * @brief Build backend — thin adapter around a native build system. + * + * Each backend provides four function pointers following the same + * lifecycle: configure → build → install → clean. + */ +typedef struct EosBackend { + char name[EOS_MAX_NAME]; /**< Backend identifier (e.g., "cmake", "zephyr") */ + EosBuildType type; /**< Corresponding build type enum */ + + /** @brief Configure the build (e.g., cmake -S ... -B ...) */ + EosResult (*configure)(struct EosBackend *self, const char *src_dir, + const char *build_dir, const char *toolchain_file, + const EosKeyValue *options, int option_count); + + /** @brief Execute the build (e.g., cmake --build ...) */ + EosResult (*build)(struct EosBackend *self, const char *build_dir, int jobs); + + /** @brief Install built artifacts to a destination directory */ + EosResult (*install)(struct EosBackend *self, const char *build_dir, + const char *install_dir); + + /** @brief Clean build artifacts */ + EosResult (*clean)(struct EosBackend *self, const char *build_dir); +} EosBackend; + +/** @brief Initialize the global backend registry. Must be called before register. */ +void eos_backend_registry_init(void); + +/** @brief Register a backend in the global registry. */ +EosResult eos_backend_register(EosBackend *backend); + +/** @brief Find a registered backend by name. Returns NULL if not found. */ EosBackend *eos_backend_find(const char *name); -int eos_backend_init_all(void); -#ifdef __cplusplus -} -#endif +/** @brief Find a registered backend by build type. Returns NULL if not found. */ +EosBackend *eos_backend_find_by_type(EosBuildType type); + +/* ---- Built-in backend constructors ---- */ + +void eos_backend_cmake_init(EosBackend *b); /**< CMake + Ninja */ +void eos_backend_ninja_init(EosBackend *b); /**< Direct Ninja */ +void eos_backend_make_init(EosBackend *b); /**< GNU Make / Autotools */ +void eos_backend_kbuild_init(EosBackend *b); /**< Linux Kbuild */ + +/* ---- RTOS backend constructors ---- */ + +void eos_backend_zephyr_init(EosBackend *b); /**< Zephyr RTOS (west/CMake) */ +void eos_backend_freertos_init(EosBackend *b); /**< FreeRTOS (CMake/Make/vendor) */ +void eos_backend_nuttx_init(EosBackend *b); /**< Apache NuttX (native) */ + +/* ---- Additional backend constructors ---- */ + +void eos_backend_meson_init(EosBackend *b); /**< Meson + Ninja */ +void eos_backend_autotools_init(EosBackend *b); /**< Autoconf / Automake */ +void eos_backend_buildroot_init(EosBackend *b); /**< Buildroot (kernel+rootfs+image) */ +void eos_backend_cargo_init(EosBackend *b); /**< Rust Cargo */ #endif /* EOS_BACKEND_H */ diff --git a/core/eos/kernel/include/eos/kernel.h b/core/eos/kernel/include/eos/kernel.h index 892ed04b..3a09f15f 100644 --- a/core/eos/kernel/include/eos/kernel.h +++ b/core/eos/kernel/include/eos/kernel.h @@ -80,12 +80,14 @@ typedef struct { const char *name; eos_task_state_t state; uint8_t priority; + uint8_t base_priority; uint32_t stack_size; eos_task_func_t entry; void *arg; uint32_t *stack_base; uint32_t *stack_ptr; uint32_t wake_tick; + uint8_t wake_armed; /* 1 when wake_tick holds a real deadline */ uint32_t run_count; } eos_task_t; @@ -98,7 +100,27 @@ bool eos_kernel_is_running(void); int eos_task_create(const char *name, eos_task_func_t entry, void *arg, uint8_t priority, uint32_t stack_size); +/** + * @brief Delete a task; its slot becomes available to eos_task_create(). + * @param handle Task handle (slot index) returned by eos_task_create(). + * @return EOS_KERN_OK on success; EOS_KERN_INVALID for handle 0, an + * out-of-range handle, or a slot that holds no task. + * + * Handle 0 is the idle task, and it is permanent: it is the task the + * scheduler falls back to when nothing else is runnable, so it can be + * neither deleted nor suspended. + */ int eos_task_delete(eos_task_handle_t handle); + +/** + * @brief Suspend a task until eos_task_resume() is called on it. + * @param handle Task handle (slot index) returned by eos_task_create(). + * @return EOS_KERN_OK on success; EOS_KERN_INVALID for handle 0, an + * out-of-range handle, or a slot whose entry is NULL (never + * created, or deleted through eos_task_delete()). + * + * Handle 0 is the idle task and is permanent; see eos_task_delete(). + */ int eos_task_suspend(eos_task_handle_t handle); int eos_task_resume(eos_task_handle_t handle); void eos_task_yield(void); @@ -107,6 +129,44 @@ eos_task_handle_t eos_task_get_current(void); eos_task_state_t eos_task_get_state(eos_task_handle_t handle); const char *eos_task_get_name(eos_task_handle_t handle); +/* ============================================================ + * Task Runtime Statistics + * ============================================================ */ + +/** + * @brief Per-task runtime statistics snapshot. + * + * Populated by eos_task_get_stats() / eos_task_get_all_stats(). + * Useful for debugging stack usage and CPU utilization on + * resource-constrained targets. + */ +typedef struct { + uint8_t id; /**< Task slot index */ + const char *name; /**< Task name (may be NULL) */ + uint8_t priority; /**< Current priority (0 = highest) */ + eos_task_state_t state; /**< Current task state */ + uint32_t run_count; /**< Number of times the task was scheduled */ + uint32_t stack_used; /**< Estimated stack bytes consumed */ + uint32_t stack_size; /**< Total stack allocation in bytes */ +} eos_task_stats_t; + +/** + * @brief Get runtime statistics for a single task. + * @param handle Task handle (slot index). + * @param out Pointer to stats structure to fill. + * @return EOS_KERN_OK on success. + */ +int eos_task_get_stats(eos_task_handle_t handle, eos_task_stats_t *out); + +/** + * @brief Get runtime statistics for all active tasks. + * @param out Array to fill with stats entries. + * @param max_entries Maximum number of entries in @p out. + * @param count Output: actual number of entries written. + * @return EOS_KERN_OK on success. + */ +int eos_task_get_all_stats(eos_task_stats_t *out, int max_entries, int *count); + /* ============================================================ * Mutex * ============================================================ */ @@ -165,6 +225,19 @@ int eos_swtimer_delete(eos_swtimer_handle_t handle); * ============================================================ */ void eos_kernel_tick(void); +uint32_t eos_tick_get(void); + +/** + * @brief Current value of the free-running kernel tick counter. + * + * Defined in kernel/src/task.c. It had no declaration in any header, so every + * caller outside that translation unit relied on an implicit declaration — + * which -Werror rejects, and which C23 removes entirely. + * + * The counter wraps at UINT32_MAX. Compare deadlines with a signed difference, + * never with `now >= deadline`. + */ +uint32_t eos_tick_get(void); #ifdef __cplusplus } diff --git a/core/eos/net/src/net.c b/core/eos/net/src/net.c index 23b30a86..3fbd4852 100644 --- a/core/eos/net/src/net.c +++ b/core/eos/net/src/net.c @@ -14,6 +14,16 @@ static bool net_initialized = false; +/* The socket layer below is the freestanding fallback. On a host, ADR-014 + * point 3 maps this API onto POSIX sockets instead; CMake compiles + * net_posix.c and defines EOS_NET_POSIX, and these stubs step aside so there + * is exactly one definition of each symbol. + * + * Left returning -1 rather than deleted: on a target with no IP stack yet, + * failing is the honest answer, and it is what ADR-014 records as the + * Planned state for the Nano and Edge profiles until lwIP lands. */ +#ifndef EOS_NET_POSIX + int eos_net_init(void) { net_initialized = true; @@ -27,8 +37,14 @@ void eos_net_deinit(void) eos_socket_t eos_net_socket(eos_net_proto_t proto) { - (void)proto; - if (!net_initialized) return EOS_SOCKET_INVALID; + if (!net_initialized) { + return EOS_SOCKET_INVALID; + } + + if (proto != EOS_NET_TCP && proto != EOS_NET_UDP) { + return EOS_SOCKET_INVALID; + } + static int next_fd = 1; return (eos_socket_t)next_fd++; } @@ -100,6 +116,9 @@ int eos_net_resolve(const char *hostname, uint32_t *ip) /* ---- HTTP ---- */ + +#endif /* !EOS_NET_POSIX */ + int eos_http_get(const char *url, eos_http_response_t *resp) { (void)url; diff --git a/core/eos/services/CMakeLists.txt b/core/eos/services/CMakeLists.txt index 74e18865..7567a4d4 100644 --- a/core/eos/services/CMakeLists.txt +++ b/core/eos/services/CMakeLists.txt @@ -1,4 +1,5 @@ add_subdirectory(crypto) +add_subdirectory(gps) add_subdirectory(security) add_subdirectory(os) add_subdirectory(linux) diff --git a/core/eos/services/compat/CMakeLists.txt b/core/eos/services/compat/CMakeLists.txt index 097e30aa..3c5b08e5 100644 --- a/core/eos/services/compat/CMakeLists.txt +++ b/core/eos/services/compat/CMakeLists.txt @@ -6,6 +6,15 @@ add_library(eos_compat STATIC src/os_adapter_eos.c ) +# The POSIX adapter lets EoS services run on a host that already has an OS. +# Only built where pthreads exist; it is not a target-side component. +if(UNIX AND NOT CMAKE_CROSSCOMPILING) + target_sources(eos_compat PRIVATE src/os_adapter_posix.c) + target_compile_definitions(eos_compat PUBLIC EOS_HAVE_POSIX_ADAPTER=1) + find_package(Threads REQUIRED) + target_link_libraries(eos_compat PUBLIC Threads::Threads) +endif() + target_include_directories(eos_compat PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/kernel/include diff --git a/core/eos/services/compat/include/eos/os_adapter.h b/core/eos/services/compat/include/eos/os_adapter.h index ab74c5b8..d7d6fc8e 100644 --- a/core/eos/services/compat/include/eos/os_adapter.h +++ b/core/eos/services/compat/include/eos/os_adapter.h @@ -30,6 +30,19 @@ extern "C" { #ifndef EOS_MAX_OS_ADAPTERS #define EOS_MAX_OS_ADAPTERS 4 + +/** + * @brief Block until the operation succeeds, with no timeout. + * + * Every `timeout_ms` parameter below accepts this. The value matches + * EOS_WAIT_FOREVER in kernel.h, but is defined separately so an adapter for a + * foreign OS can be built without including the EoS kernel headers at all — + * which is the entire point of this interface. + */ +#define EOS_OSA_WAIT_FOREVER 0xFFFFFFFFU + +/** @brief Return immediately if the operation would block. */ +#define EOS_OSA_NO_WAIT 0U #endif /* ============================================================ diff --git a/core/eos/services/compat/src/os_adapter_posix.c b/core/eos/services/compat/src/os_adapter_posix.c new file mode 100644 index 00000000..23fd6ffc --- /dev/null +++ b/core/eos/services/compat/src/os_adapter_posix.c @@ -0,0 +1,976 @@ +// SPDX-License-Identifier: MIT +// Copyright (c) 2026 EoS Project +// ISO/IEC 25000 | ISO/IEC/IEEE 15288:2023 + +/** + * @file os_adapter_posix.c + * @brief POSIX host adapter — runs EoS services on Linux, macOS or WSL. + * + * `os_adapter.h` defines a complete OS abstraction and a registry that holds up + * to EOS_MAX_OS_ADAPTERS entries. Until now exactly one was implemented, the + * EoS native kernel, so the abstraction had never been exercised against + * anything but itself. An interface with one implementation is a guess about + * what varies. + * + * This is the second. It backs the same vtable with pthreads, POSIX semaphores + * and CLOCK_MONOTONIC, so EoS services run as an ordinary process on a host + * that already has an operating system — which is the arrangement most + * evaluators start from, and the one the Compute profile in §21 describes. + * + * It also makes the EoS-native adapter falsifiable: the compliance test can now + * run the identical sequence against both and require the same answers. + * + * Deliberate limits, none of them accidental: + * + * - `irq_disable` / `irq_enable` are no-ops. A user-space process cannot mask + * interrupts, and pretending otherwise would hide the fact that code + * relying on them for mutual exclusion is unsafe here. Use a mutex. + * - `priority` is accepted and recorded but not applied. Real-time scheduling + * needs privileges the process usually lacks, and silently running + * SCHED_OTHER while reporting success would be worse than saying so. + * - `stack_size` is honoured through pthread_attr_setstacksize, clamped up to + * PTHREAD_STACK_MIN. + */ + +#include "eos/os_adapter.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define POSIX_MAX_TASKS 16 +#define POSIX_MAX_MUTEXES 16 +#define POSIX_MAX_SEMS 16 +#define POSIX_MAX_QUEUES 8 +#define POSIX_MAX_TIMERS 8 +#define POSIX_NAME_MAX 32 + +/* ============================================================ + * Time helpers + * ============================================================ */ + +static void ms_to_abstime(uint32_t ms, struct timespec *out) +{ + clock_gettime(CLOCK_REALTIME, out); + out->tv_sec += (time_t)(ms / 1000u); + out->tv_nsec += (long)((ms % 1000u) * 1000000L); + if (out->tv_nsec >= 1000000000L) { + out->tv_sec += 1; + out->tv_nsec -= 1000000000L; + } +} + +static void sleep_ms(uint32_t ms) +{ + struct timespec ts = { + .tv_sec = (time_t)(ms / 1000u), + .tv_nsec = (long)((ms % 1000u) * 1000000L), + }; + while (nanosleep(&ts, &ts) == -1 && errno == EINTR) { + /* Interrupted by a signal — finish the remaining interval. */ + } +} + +/* ============================================================ + * Darwin portability + * + * This adapter is documented above as running on macOS, but Darwin does not + * implement three of the POSIX primitives it reaches for: + * + * - pthread_mutex_timedlock() is absent entirely. + * - sem_init() fails with ENOSYS; Darwin has no unnamed semaphores. + * - sem_getvalue() fails with ENOSYS, so the count the semaphore ceiling + * is enforced against cannot be read back. + * + * Named semaphores do not help: sem_open() works, but sem_getvalue() still + * does not. So on Apple platforms the semaphore is a mutex and a condition + * variable holding its own count, which also makes the timed wait exact + * instead of polled. Every other POSIX host keeps the system primitives. + * ============================================================ */ + +#ifdef __APPLE__ + +static bool abstime_reached(const struct timespec *deadline) +{ + struct timespec now; + clock_gettime(CLOCK_REALTIME, &now); + if (now.tv_sec != deadline->tv_sec) return now.tv_sec > deadline->tv_sec; + return now.tv_nsec >= deadline->tv_nsec; +} + +/* No pthread_mutex_timedlock() on Darwin. Poll trylock until the deadline; + * the interval bounds how long a timed lock can overshoot. + * + * Nothing in the tree reaches this today: posix_mutex_lock_fn() sends a zero + * timeout to pthread_mutex_trylock() and EOS_OSA_WAIT_FOREVER to plain + * pthread_mutex_lock(), so only a finite non-zero timeout arrives here, and + * every eos_mutex_lock() call currently passes 0 or EOS_NO_WAIT. A caller that + * does want a finite timed lock inherits this granularity: a timeout shorter + * than the interval still waits a full interval before giving up. Lower the + * value, or take the Linux path, if that ever matters. */ +#define COMPAT_LOCK_POLL_NS 1000000L /* 1 ms */ + +static int compat_mutex_timedlock(pthread_mutex_t *m, const struct timespec *deadline) +{ + for (;;) { + if (pthread_mutex_trylock(m) == 0) return 0; + if (abstime_reached(deadline)) return -1; + struct timespec gap = { .tv_sec = 0, .tv_nsec = COMPAT_LOCK_POLL_NS }; + nanosleep(&gap, NULL); + } +} + +typedef struct { + pthread_mutex_t lock; + pthread_cond_t cv; + unsigned count; +} compat_sem_t; + +static int compat_sem_init(compat_sem_t *s, unsigned value) +{ + if (pthread_mutex_init(&s->lock, NULL) != 0) return -1; + if (pthread_cond_init(&s->cv, NULL) != 0) { + pthread_mutex_destroy(&s->lock); + return -1; + } + s->count = value; + return 0; +} + +static int compat_sem_destroy(compat_sem_t *s) +{ + pthread_cond_destroy(&s->cv); + pthread_mutex_destroy(&s->lock); + return 0; +} + +static int compat_sem_trywait(compat_sem_t *s) +{ + int rc = -1; + pthread_mutex_lock(&s->lock); + if (s->count > 0u) { + s->count--; + rc = 0; + } + pthread_mutex_unlock(&s->lock); + return rc; +} + +static int compat_sem_wait(compat_sem_t *s) +{ + pthread_mutex_lock(&s->lock); + while (s->count == 0u) { + pthread_cond_wait(&s->cv, &s->lock); + } + s->count--; + pthread_mutex_unlock(&s->lock); + return 0; +} + +static int compat_sem_timedwait(compat_sem_t *s, const struct timespec *deadline) +{ + int rc = 0; + pthread_mutex_lock(&s->lock); + while (s->count == 0u && rc == 0) { + rc = pthread_cond_timedwait(&s->cv, &s->lock, deadline); + } + if (s->count > 0u) { + s->count--; + rc = 0; + } else { + rc = -1; + } + pthread_mutex_unlock(&s->lock); + return rc; +} + +static int compat_sem_post(compat_sem_t *s) +{ + pthread_mutex_lock(&s->lock); + s->count++; + pthread_cond_signal(&s->cv); + pthread_mutex_unlock(&s->lock); + return 0; +} + +static int compat_sem_getvalue(compat_sem_t *s, int *value) +{ + pthread_mutex_lock(&s->lock); + *value = (int)s->count; + pthread_mutex_unlock(&s->lock); + return 0; +} + +#else /* !__APPLE__ — use the system primitives */ + +typedef sem_t compat_sem_t; + +static int compat_mutex_timedlock(pthread_mutex_t *m, const struct timespec *deadline) +{ + return pthread_mutex_timedlock(m, deadline); +} + +static int compat_sem_init(compat_sem_t *s, unsigned value) { return sem_init(s, 0, value); } +static int compat_sem_destroy(compat_sem_t *s) { return sem_destroy(s); } +static int compat_sem_trywait(compat_sem_t *s) { return sem_trywait(s); } +static int compat_sem_post(compat_sem_t *s) { return sem_post(s); } +static int compat_sem_getvalue(compat_sem_t *s, int *value) { return sem_getvalue(s, value); } + +static int compat_sem_wait(compat_sem_t *s) +{ + int rc; + while ((rc = sem_wait(s)) == -1 && errno == EINTR) { } + return rc; +} + +static int compat_sem_timedwait(compat_sem_t *s, const struct timespec *deadline) +{ + int rc; + while ((rc = sem_timedwait(s, deadline)) == -1 && errno == EINTR) { } + return rc; +} + +#endif /* __APPLE__ */ + +/* ============================================================ + * Tasks + * ============================================================ */ + +typedef struct { + bool in_use; + char name[POSIX_NAME_MAX]; + pthread_t thread; + eos_osa_task_func_t entry; + void *arg; + uint8_t priority; /* recorded, not applied — see file header */ + + /* Suspend gate. POSIX has no thread suspend, so a suspended task blocks on + * its own condvar the next time it reaches a yield or delay. + * + * `release` only frees a task parked at the gate; it never cancels one. A + * task that has not reached the gate yet still runs its entry function. + * Conflating the two would make task_delete race task creation and skip the + * body entirely. */ + pthread_mutex_t gate_lock; + pthread_cond_t gate_cond; + bool suspended; + bool release; +} posix_task_t; + +static posix_task_t g_tasks[POSIX_MAX_TASKS]; +static pthread_mutex_t g_tasks_lock = PTHREAD_MUTEX_INITIALIZER; +static pthread_key_t g_self_key; +static pthread_once_t g_self_key_once = PTHREAD_ONCE_INIT; + +static void make_self_key(void) +{ + pthread_key_create(&g_self_key, NULL); +} + +/* Block here while suspended. Called from yield and delay, which is where a + * cooperative task is expected to be interruptible. */ +static void gate_check(posix_task_t *t) +{ + pthread_mutex_lock(&t->gate_lock); + while (t->suspended && !t->release) { + pthread_cond_wait(&t->gate_cond, &t->gate_lock); + } + pthread_mutex_unlock(&t->gate_lock); +} + +static void *task_trampoline(void *raw) +{ + posix_task_t *t = (posix_task_t *)raw; + + pthread_once(&g_self_key_once, make_self_key); + pthread_setspecific(g_self_key, t); + + gate_check(t); + if (t->entry) { + t->entry(t->arg); + } + return NULL; +} + +static posix_task_t *current_task(void) +{ + pthread_once(&g_self_key_once, make_self_key); + return (posix_task_t *)pthread_getspecific(g_self_key); +} + +static int posix_task_create(const char *name, eos_osa_task_func_t entry, void *arg, + uint8_t priority, uint32_t stack_size) +{ + if (!entry) return -1; + + pthread_mutex_lock(&g_tasks_lock); + + int slot = -1; + for (int i = 0; i < POSIX_MAX_TASKS; i++) { + if (!g_tasks[i].in_use) { slot = i; break; } + } + if (slot < 0) { + pthread_mutex_unlock(&g_tasks_lock); + return -1; + } + + posix_task_t *t = &g_tasks[slot]; + memset(t, 0, sizeof(*t)); + t->in_use = true; + t->entry = entry; + t->arg = arg; + t->priority = priority; + if (name) { + strncpy(t->name, name, POSIX_NAME_MAX - 1); + } + pthread_mutex_init(&t->gate_lock, NULL); + pthread_cond_init(&t->gate_cond, NULL); + + pthread_attr_t attr; + pthread_attr_init(&attr); + if (stack_size > 0) { + size_t want = stack_size; + if (want < (size_t)PTHREAD_STACK_MIN) { + want = (size_t)PTHREAD_STACK_MIN; + } + pthread_attr_setstacksize(&attr, want); + } + + const int rc = pthread_create(&t->thread, &attr, task_trampoline, t); + pthread_attr_destroy(&attr); + + if (rc != 0) { + pthread_mutex_destroy(&t->gate_lock); + pthread_cond_destroy(&t->gate_cond); + t->in_use = false; + pthread_mutex_unlock(&g_tasks_lock); + return -1; + } + + pthread_mutex_unlock(&g_tasks_lock); + return slot; +} + +static bool task_valid(uint8_t handle) +{ + return handle < POSIX_MAX_TASKS && g_tasks[handle].in_use; +} + +static int posix_task_delete(uint8_t handle) +{ + if (!task_valid(handle)) return -1; + posix_task_t *t = &g_tasks[handle]; + + /* Release the task if it is parked at the suspend gate, then wait for it. + * + * There is no safe way to kill a POSIX thread: pthread_cancel leaves any + * mutex it holds locked forever. So delete means "let it finish and reclaim + * the slot", and the entry function is required to return. A task that + * loops forever will hang this call — which is the truth about threads on a + * hosted OS, and better said plainly than papered over with a cancel that + * corrupts the process. */ + pthread_mutex_lock(&t->gate_lock); + t->release = true; + t->suspended = false; + pthread_cond_broadcast(&t->gate_cond); + pthread_mutex_unlock(&t->gate_lock); + + pthread_join(t->thread, NULL); + + pthread_mutex_destroy(&t->gate_lock); + pthread_cond_destroy(&t->gate_cond); + + pthread_mutex_lock(&g_tasks_lock); + t->in_use = false; + pthread_mutex_unlock(&g_tasks_lock); + return 0; +} + +static int posix_task_suspend(uint8_t handle) +{ + if (!task_valid(handle)) return -1; + posix_task_t *t = &g_tasks[handle]; + pthread_mutex_lock(&t->gate_lock); + t->suspended = true; + pthread_mutex_unlock(&t->gate_lock); + return 0; +} + +static int posix_task_resume(uint8_t handle) +{ + if (!task_valid(handle)) return -1; + posix_task_t *t = &g_tasks[handle]; + pthread_mutex_lock(&t->gate_lock); + t->suspended = false; + pthread_cond_broadcast(&t->gate_cond); + pthread_mutex_unlock(&t->gate_lock); + return 0; +} + +static void posix_task_yield(void) +{ + posix_task_t *t = current_task(); + if (t) gate_check(t); + sched_yield(); +} + +static void posix_task_delay_ms(uint32_t ms) +{ + posix_task_t *t = current_task(); + if (t) gate_check(t); + sleep_ms(ms); +} + +static uint8_t posix_task_get_current(void) +{ + const posix_task_t *t = current_task(); + if (!t) return 0xFFu; + return (uint8_t)(t - g_tasks); +} + +static const char *posix_task_get_name(uint8_t handle) +{ + if (!task_valid(handle)) return "invalid"; + return g_tasks[handle].name; +} + +/* ============================================================ + * Mutexes + * ============================================================ */ + +typedef struct { + bool in_use; + pthread_mutex_t lock; +} posix_mutex_t; + +static posix_mutex_t g_mutexes[POSIX_MAX_MUTEXES]; +static pthread_mutex_t g_mutexes_lock = PTHREAD_MUTEX_INITIALIZER; + +static int posix_mutex_create(uint8_t *out) +{ + if (!out) return -1; + pthread_mutex_lock(&g_mutexes_lock); + for (int i = 0; i < POSIX_MAX_MUTEXES; i++) { + if (!g_mutexes[i].in_use) { + pthread_mutexattr_t attr; + pthread_mutexattr_init(&attr); + /* Recursive, to match the EoS mutex, which counts nesting. */ + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); + pthread_mutex_init(&g_mutexes[i].lock, &attr); + pthread_mutexattr_destroy(&attr); + g_mutexes[i].in_use = true; + *out = (uint8_t)i; + pthread_mutex_unlock(&g_mutexes_lock); + return 0; + } + } + pthread_mutex_unlock(&g_mutexes_lock); + return -1; +} + +static int posix_mutex_lock_fn(uint8_t handle, uint32_t timeout_ms) +{ + if (handle >= POSIX_MAX_MUTEXES || !g_mutexes[handle].in_use) return -1; + pthread_mutex_t *m = &g_mutexes[handle].lock; + + if (timeout_ms == 0u) { + return pthread_mutex_trylock(m) == 0 ? 0 : -1; + } + if (timeout_ms == EOS_OSA_WAIT_FOREVER) { + return pthread_mutex_lock(m) == 0 ? 0 : -1; + } + + struct timespec deadline; + ms_to_abstime(timeout_ms, &deadline); + return compat_mutex_timedlock(m, &deadline) == 0 ? 0 : -1; +} + +static int posix_mutex_unlock(uint8_t handle) +{ + if (handle >= POSIX_MAX_MUTEXES || !g_mutexes[handle].in_use) return -1; + return pthread_mutex_unlock(&g_mutexes[handle].lock) == 0 ? 0 : -1; +} + +static int posix_mutex_delete(uint8_t handle) +{ + if (handle >= POSIX_MAX_MUTEXES || !g_mutexes[handle].in_use) return -1; + pthread_mutex_lock(&g_mutexes_lock); + pthread_mutex_destroy(&g_mutexes[handle].lock); + g_mutexes[handle].in_use = false; + pthread_mutex_unlock(&g_mutexes_lock); + return 0; +} + +/* ============================================================ + * Semaphores + * ============================================================ */ + +typedef struct { + bool in_use; + compat_sem_t sem; + uint32_t max; +} posix_sem_t; + +static posix_sem_t g_sems[POSIX_MAX_SEMS]; +static pthread_mutex_t g_sems_lock = PTHREAD_MUTEX_INITIALIZER; + +static int posix_sem_create(uint8_t *out, uint32_t initial, uint32_t max) +{ + if (!out || max == 0u || initial > max) return -1; + pthread_mutex_lock(&g_sems_lock); + for (int i = 0; i < POSIX_MAX_SEMS; i++) { + if (!g_sems[i].in_use) { + if (compat_sem_init(&g_sems[i].sem, (unsigned)initial) != 0) { + pthread_mutex_unlock(&g_sems_lock); + return -1; + } + g_sems[i].in_use = true; + g_sems[i].max = max; + *out = (uint8_t)i; + pthread_mutex_unlock(&g_sems_lock); + return 0; + } + } + pthread_mutex_unlock(&g_sems_lock); + return -1; +} + +static int posix_sem_wait(uint8_t handle, uint32_t timeout_ms) +{ + if (handle >= POSIX_MAX_SEMS || !g_sems[handle].in_use) return -1; + compat_sem_t *s = &g_sems[handle].sem; + + if (timeout_ms == 0u) { + return compat_sem_trywait(s) == 0 ? 0 : -1; + } + if (timeout_ms == EOS_OSA_WAIT_FOREVER) { + return compat_sem_wait(s) == 0 ? 0 : -1; + } + + struct timespec deadline; + ms_to_abstime(timeout_ms, &deadline); + return compat_sem_timedwait(s, &deadline) == 0 ? 0 : -1; +} + +static int posix_sem_post(uint8_t handle) +{ + if (handle >= POSIX_MAX_SEMS || !g_sems[handle].in_use) return -1; + + /* Enforce the ceiling the caller asked for. POSIX semaphores have no + * maximum, so without this the counting invariant the EoS semaphore + * guarantees would silently not hold on this backend. */ + int value = 0; + if (compat_sem_getvalue(&g_sems[handle].sem, &value) == 0 && + (uint32_t)value >= g_sems[handle].max) { + return -1; + } + return compat_sem_post(&g_sems[handle].sem) == 0 ? 0 : -1; +} + +static int posix_sem_delete(uint8_t handle) +{ + if (handle >= POSIX_MAX_SEMS || !g_sems[handle].in_use) return -1; + pthread_mutex_lock(&g_sems_lock); + compat_sem_destroy(&g_sems[handle].sem); + g_sems[handle].in_use = false; + pthread_mutex_unlock(&g_sems_lock); + return 0; +} + +static uint32_t posix_sem_get_count(uint8_t handle) +{ + if (handle >= POSIX_MAX_SEMS || !g_sems[handle].in_use) return 0; + int value = 0; + if (compat_sem_getvalue(&g_sems[handle].sem, &value) != 0 || value < 0) return 0; + return (uint32_t)value; +} + +/* ============================================================ + * Message queues + * ============================================================ */ + +typedef struct { + bool in_use; + uint8_t *storage; + size_t item_size; + uint32_t capacity; + uint32_t count; + uint32_t head; + uint32_t tail; + pthread_mutex_t lock; + pthread_cond_t not_empty; + pthread_cond_t not_full; +} posix_queue_t; + +static posix_queue_t g_queues[POSIX_MAX_QUEUES]; +static pthread_mutex_t g_queues_lock = PTHREAD_MUTEX_INITIALIZER; + +static int posix_queue_create(uint8_t *out, size_t item_size, uint32_t capacity) +{ + if (!out || item_size == 0u || capacity == 0u) return -1; + /* Reject a size that would wrap the allocation. */ + if (item_size > SIZE_MAX / capacity) return -1; + + pthread_mutex_lock(&g_queues_lock); + for (int i = 0; i < POSIX_MAX_QUEUES; i++) { + if (!g_queues[i].in_use) { + posix_queue_t *q = &g_queues[i]; + q->storage = (uint8_t *)calloc(capacity, item_size); + if (!q->storage) { + pthread_mutex_unlock(&g_queues_lock); + return -1; + } + q->item_size = item_size; + q->capacity = capacity; + q->count = 0; + q->head = 0; + q->tail = 0; + pthread_mutex_init(&q->lock, NULL); + pthread_cond_init(&q->not_empty, NULL); + pthread_cond_init(&q->not_full, NULL); + q->in_use = true; + *out = (uint8_t)i; + pthread_mutex_unlock(&g_queues_lock); + return 0; + } + } + pthread_mutex_unlock(&g_queues_lock); + return -1; +} + +static bool queue_valid(uint8_t handle) +{ + return handle < POSIX_MAX_QUEUES && g_queues[handle].in_use; +} + +static int posix_queue_send(uint8_t handle, const void *item, uint32_t timeout_ms) +{ + if (!queue_valid(handle) || !item) return -1; + posix_queue_t *q = &g_queues[handle]; + + pthread_mutex_lock(&q->lock); + + while (q->count == q->capacity) { + if (timeout_ms == 0u) { + pthread_mutex_unlock(&q->lock); + return -1; + } + if (timeout_ms == EOS_OSA_WAIT_FOREVER) { + pthread_cond_wait(&q->not_full, &q->lock); + continue; + } + struct timespec deadline; + ms_to_abstime(timeout_ms, &deadline); + if (pthread_cond_timedwait(&q->not_full, &q->lock, &deadline) == ETIMEDOUT) { + pthread_mutex_unlock(&q->lock); + return -1; + } + } + + memcpy(q->storage + ((size_t)q->tail * q->item_size), item, q->item_size); + q->tail = (q->tail + 1u) % q->capacity; + q->count++; + + pthread_cond_signal(&q->not_empty); + pthread_mutex_unlock(&q->lock); + return 0; +} + +static int posix_queue_receive(uint8_t handle, void *item, uint32_t timeout_ms) +{ + if (!queue_valid(handle) || !item) return -1; + posix_queue_t *q = &g_queues[handle]; + + pthread_mutex_lock(&q->lock); + + while (q->count == 0u) { + if (timeout_ms == 0u) { + pthread_mutex_unlock(&q->lock); + return -1; + } + if (timeout_ms == EOS_OSA_WAIT_FOREVER) { + pthread_cond_wait(&q->not_empty, &q->lock); + continue; + } + struct timespec deadline; + ms_to_abstime(timeout_ms, &deadline); + if (pthread_cond_timedwait(&q->not_empty, &q->lock, &deadline) == ETIMEDOUT) { + pthread_mutex_unlock(&q->lock); + return -1; + } + } + + memcpy(item, q->storage + ((size_t)q->head * q->item_size), q->item_size); + q->head = (q->head + 1u) % q->capacity; + q->count--; + + pthread_cond_signal(&q->not_full); + pthread_mutex_unlock(&q->lock); + return 0; +} + +static int posix_queue_delete(uint8_t handle) +{ + if (!queue_valid(handle)) return -1; + posix_queue_t *q = &g_queues[handle]; + + pthread_mutex_lock(&g_queues_lock); + free(q->storage); + q->storage = NULL; + pthread_mutex_destroy(&q->lock); + pthread_cond_destroy(&q->not_empty); + pthread_cond_destroy(&q->not_full); + q->in_use = false; + pthread_mutex_unlock(&g_queues_lock); + return 0; +} + +static uint32_t posix_queue_count(uint8_t handle) +{ + if (!queue_valid(handle)) return 0; + posix_queue_t *q = &g_queues[handle]; + pthread_mutex_lock(&q->lock); + const uint32_t n = q->count; + pthread_mutex_unlock(&q->lock); + return n; +} + +static bool posix_queue_is_full(uint8_t handle) +{ + if (!queue_valid(handle)) return false; + return posix_queue_count(handle) == g_queues[handle].capacity; +} + +static bool posix_queue_is_empty(uint8_t handle) +{ + if (!queue_valid(handle)) return true; + return posix_queue_count(handle) == 0u; +} + +/* ============================================================ + * Software timers + * ============================================================ */ + +typedef struct { + bool in_use; + bool running; + bool stop; + char name[POSIX_NAME_MAX]; + uint32_t period_ms; + bool auto_reload; + eos_osa_timer_cb_t callback; + void *ctx; + uint8_t handle; + pthread_t thread; + pthread_mutex_t lock; + pthread_cond_t cond; +} posix_timer_t; + +static posix_timer_t g_timers[POSIX_MAX_TIMERS]; +static pthread_mutex_t g_timers_lock = PTHREAD_MUTEX_INITIALIZER; + +/* One thread per timer. A single timer wheel would be less thread-hungry, but + * POSIX_MAX_TIMERS is 8 and a wheel adds a scheduling policy this adapter has + * no business inventing — the host kernel already has one. */ +static void *timer_thread(void *raw) +{ + posix_timer_t *t = (posix_timer_t *)raw; + + for (;;) { + pthread_mutex_lock(&t->lock); + while (!t->running && !t->stop) { + pthread_cond_wait(&t->cond, &t->lock); + } + if (t->stop) { + pthread_mutex_unlock(&t->lock); + return NULL; + } + + struct timespec deadline; + ms_to_abstime(t->period_ms, &deadline); + const int rc = pthread_cond_timedwait(&t->cond, &t->lock, &deadline); + + if (t->stop) { + pthread_mutex_unlock(&t->lock); + return NULL; + } + const bool expired = (rc == ETIMEDOUT) && t->running; + const bool reload = t->auto_reload; + eos_osa_timer_cb_t cb = t->callback; + void *ctx = t->ctx; + const uint8_t handle = t->handle; + if (expired && !reload) { + t->running = false; + } + pthread_mutex_unlock(&t->lock); + + /* Called outside the lock: a callback that touches this timer must not + * deadlock against it. */ + if (expired && cb) { + cb(handle, ctx); + } + } +} + +static int posix_timer_create(uint8_t *out, const char *name, uint32_t period_ms, + bool auto_reload, eos_osa_timer_cb_t callback, void *ctx) +{ + if (!out || !callback || period_ms == 0u) return -1; + + pthread_mutex_lock(&g_timers_lock); + for (int i = 0; i < POSIX_MAX_TIMERS; i++) { + if (!g_timers[i].in_use) { + posix_timer_t *t = &g_timers[i]; + memset(t, 0, sizeof(*t)); + t->in_use = true; + t->period_ms = period_ms; + t->auto_reload = auto_reload; + t->callback = callback; + t->ctx = ctx; + t->handle = (uint8_t)i; + if (name) strncpy(t->name, name, POSIX_NAME_MAX - 1); + pthread_mutex_init(&t->lock, NULL); + pthread_cond_init(&t->cond, NULL); + + if (pthread_create(&t->thread, NULL, timer_thread, t) != 0) { + pthread_mutex_destroy(&t->lock); + pthread_cond_destroy(&t->cond); + t->in_use = false; + pthread_mutex_unlock(&g_timers_lock); + return -1; + } + *out = (uint8_t)i; + pthread_mutex_unlock(&g_timers_lock); + return 0; + } + } + pthread_mutex_unlock(&g_timers_lock); + return -1; +} + +static bool timer_valid(uint8_t handle) +{ + return handle < POSIX_MAX_TIMERS && g_timers[handle].in_use; +} + +static int posix_timer_start(uint8_t handle) +{ + if (!timer_valid(handle)) return -1; + posix_timer_t *t = &g_timers[handle]; + pthread_mutex_lock(&t->lock); + t->running = true; + pthread_cond_signal(&t->cond); + pthread_mutex_unlock(&t->lock); + return 0; +} + +static int posix_timer_stop(uint8_t handle) +{ + if (!timer_valid(handle)) return -1; + posix_timer_t *t = &g_timers[handle]; + pthread_mutex_lock(&t->lock); + t->running = false; + pthread_cond_signal(&t->cond); + pthread_mutex_unlock(&t->lock); + return 0; +} + +static int posix_timer_delete(uint8_t handle) +{ + if (!timer_valid(handle)) return -1; + posix_timer_t *t = &g_timers[handle]; + + pthread_mutex_lock(&t->lock); + t->stop = true; + t->running = false; + pthread_cond_broadcast(&t->cond); + pthread_mutex_unlock(&t->lock); + + pthread_join(t->thread, NULL); + + pthread_mutex_lock(&g_timers_lock); + pthread_mutex_destroy(&t->lock); + pthread_cond_destroy(&t->cond); + t->in_use = false; + pthread_mutex_unlock(&g_timers_lock); + return 0; +} + +/* ============================================================ + * System + * ============================================================ */ + +static uint32_t posix_get_tick_ms(void) +{ + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return (uint32_t)((uint64_t)ts.tv_sec * 1000u + (uint64_t)ts.tv_nsec / 1000000u); +} + +static void posix_delay_ms(uint32_t ms) +{ + sleep_ms(ms); +} + +/* A user-space process cannot mask interrupts. These are no-ops rather than + * an approximation, because code that relies on them for mutual exclusion is + * unsafe on this backend and should use a mutex. Silently substituting a global + * lock would hide that. */ +static void posix_irq_disable(void) { } +static void posix_irq_enable(void) { } + +/* ============================================================ + * Adapter vtable + * ============================================================ */ + +const eos_os_adapter_t eos_posix_adapter = { + .name = "posix", + + .task_create = posix_task_create, + .task_delete = posix_task_delete, + .task_suspend = posix_task_suspend, + .task_resume = posix_task_resume, + .task_yield = posix_task_yield, + .task_delay_ms = posix_task_delay_ms, + .task_get_current = posix_task_get_current, + .task_get_name = posix_task_get_name, + + .mutex_create = posix_mutex_create, + .mutex_lock = posix_mutex_lock_fn, + .mutex_unlock = posix_mutex_unlock, + .mutex_delete = posix_mutex_delete, + + .sem_create = posix_sem_create, + .sem_wait = posix_sem_wait, + .sem_post = posix_sem_post, + .sem_delete = posix_sem_delete, + .sem_get_count = posix_sem_get_count, + + .queue_create = posix_queue_create, + .queue_send = posix_queue_send, + .queue_receive = posix_queue_receive, + .queue_delete = posix_queue_delete, + .queue_count = posix_queue_count, + .queue_is_full = posix_queue_is_full, + .queue_is_empty = posix_queue_is_empty, + + .timer_create = posix_timer_create, + .timer_start = posix_timer_start, + .timer_stop = posix_timer_stop, + .timer_delete = posix_timer_delete, + + .get_tick_ms = posix_get_tick_ms, + .delay_ms = posix_delay_ms, + .irq_disable = posix_irq_disable, + .irq_enable = posix_irq_enable, + + /* NULL means malloc/free, which is what a hosted process should use. */ + .mem_alloc = NULL, + .mem_free = NULL, +}; diff --git a/core/eos/services/crypto/CMakeLists.txt b/core/eos/services/crypto/CMakeLists.txt index 2bd05e13..dea6cb5f 100644 --- a/core/eos/services/crypto/CMakeLists.txt +++ b/core/eos/services/crypto/CMakeLists.txt @@ -3,6 +3,12 @@ add_library(eos_crypto STATIC src/crc.c src/aes.c src/rsa_ecc_sha512.c + src/ed25519_verify.c + src/ed25519_sign.c + src/ed25519_ge.c + src/ed25519_sc.c + src/ed25519_fe.c + src/ed25519_sha512.c ) target_include_directories(eos_crypto PUBLIC diff --git a/core/eos/services/crypto/include/ed25519.h b/core/eos/services/crypto/include/ed25519.h new file mode 100644 index 00000000..1fe2ff17 --- /dev/null +++ b/core/eos/services/crypto/include/ed25519.h @@ -0,0 +1,43 @@ +#ifndef ED25519_H +#define ED25519_H + +#include + +#if defined(_WIN32) + #if defined(ED25519_BUILD_DLL) + #define ED25519_DECLSPEC __declspec(dllexport) + #elif defined(ED25519_DLL) + #define ED25519_DECLSPEC __declspec(dllimport) + #else + #define ED25519_DECLSPEC + #endif +#else + #define ED25519_DECLSPEC +#endif + + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef ED25519_NO_SEED +int ED25519_DECLSPEC ed25519_create_seed(unsigned char *seed); +#endif + +void ED25519_DECLSPEC ed25519_create_keypair(unsigned char *public_key, unsigned char *private_key, const unsigned char *seed); +void ED25519_DECLSPEC ed25519_sign(unsigned char *signature, const unsigned char *message, size_t message_len, const unsigned char *public_key, const unsigned char *private_key); +int ED25519_DECLSPEC ed25519_verify(const unsigned char *signature, const unsigned char *message, size_t message_len, const unsigned char *public_key); + +/* Non-zero if public_key decodes to a point of prime order, i.e. if it can + * authenticate anything. Use it to validate a stored trust anchor when it is + * configured; ed25519_verify() applies the same rule per signature. */ +int ED25519_DECLSPEC ed25519_public_key_is_usable(const unsigned char *public_key); +void ED25519_DECLSPEC ed25519_add_scalar(unsigned char *public_key, unsigned char *private_key, const unsigned char *scalar); +void ED25519_DECLSPEC ed25519_key_exchange(unsigned char *shared_secret, const unsigned char *public_key, const unsigned char *private_key); + + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/core/eos/services/crypto/include/eos/crypto.h b/core/eos/services/crypto/include/eos/crypto.h index eb47062c..13ccc5d1 100644 --- a/core/eos/services/crypto/include/eos/crypto.h +++ b/core/eos/services/crypto/include/eos/crypto.h @@ -59,6 +59,22 @@ void eos_sha512_hex(const uint8_t digest[EOS_SHA512_DIGEST_SIZE], uint32_t eos_crc32(uint32_t crc, const void *data, size_t len); uint32_t eos_crc32_file(const char *path); + +/** + * CRC-32 of a file, with the read result reported separately. + * + * eos_crc32_file() returns 0 both when the file cannot be read and when its + * CRC really is 0 -- which is the CRC of an empty file -- so a caller cannot + * tell the two apart. Every uint32_t is a valid CRC, so the status needs + * somewhere else to live. + * + * @param path File to read. + * @param out Receives the CRC on success; left untouched on failure. + * @return 0 on success, -1 if path or out is NULL, the file cannot be + * opened, or an error occurred while reading it. + */ +int eos_crc32_file_ex(const char *path, uint32_t *out); + uint64_t eos_crc64(uint64_t crc, const void *data, size_t len); /* ---- AES-128 / AES-256 ---- */ diff --git a/core/eos/services/crypto/src/ed25519_fe.c b/core/eos/services/crypto/src/ed25519_fe.c new file mode 100644 index 00000000..2105eb7b --- /dev/null +++ b/core/eos/services/crypto/src/ed25519_fe.c @@ -0,0 +1,1491 @@ +#include "fixedint.h" +#include "fe.h" + + +/* + helper functions +*/ +static uint64_t load_3(const unsigned char *in) { + uint64_t result; + + result = (uint64_t) in[0]; + result |= ((uint64_t) in[1]) << 8; + result |= ((uint64_t) in[2]) << 16; + + return result; +} + +static uint64_t load_4(const unsigned char *in) { + uint64_t result; + + result = (uint64_t) in[0]; + result |= ((uint64_t) in[1]) << 8; + result |= ((uint64_t) in[2]) << 16; + result |= ((uint64_t) in[3]) << 24; + + return result; +} + + + +/* + h = 0 +*/ + +void fe_0(fe h) { + h[0] = 0; + h[1] = 0; + h[2] = 0; + h[3] = 0; + h[4] = 0; + h[5] = 0; + h[6] = 0; + h[7] = 0; + h[8] = 0; + h[9] = 0; +} + + + +/* + h = 1 +*/ + +void fe_1(fe h) { + h[0] = 1; + h[1] = 0; + h[2] = 0; + h[3] = 0; + h[4] = 0; + h[5] = 0; + h[6] = 0; + h[7] = 0; + h[8] = 0; + h[9] = 0; +} + + + +/* + h = f + g + Can overlap h with f or g. + + Preconditions: + |f| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc. + |g| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc. + + Postconditions: + |h| bounded by 1.1*2^26,1.1*2^25,1.1*2^26,1.1*2^25,etc. +*/ + +void fe_add(fe h, const fe f, const fe g) { + int32_t f0 = f[0]; + int32_t f1 = f[1]; + int32_t f2 = f[2]; + int32_t f3 = f[3]; + int32_t f4 = f[4]; + int32_t f5 = f[5]; + int32_t f6 = f[6]; + int32_t f7 = f[7]; + int32_t f8 = f[8]; + int32_t f9 = f[9]; + int32_t g0 = g[0]; + int32_t g1 = g[1]; + int32_t g2 = g[2]; + int32_t g3 = g[3]; + int32_t g4 = g[4]; + int32_t g5 = g[5]; + int32_t g6 = g[6]; + int32_t g7 = g[7]; + int32_t g8 = g[8]; + int32_t g9 = g[9]; + int32_t h0 = f0 + g0; + int32_t h1 = f1 + g1; + int32_t h2 = f2 + g2; + int32_t h3 = f3 + g3; + int32_t h4 = f4 + g4; + int32_t h5 = f5 + g5; + int32_t h6 = f6 + g6; + int32_t h7 = f7 + g7; + int32_t h8 = f8 + g8; + int32_t h9 = f9 + g9; + + h[0] = h0; + h[1] = h1; + h[2] = h2; + h[3] = h3; + h[4] = h4; + h[5] = h5; + h[6] = h6; + h[7] = h7; + h[8] = h8; + h[9] = h9; +} + + + +/* + Replace (f,g) with (g,g) if b == 1; + replace (f,g) with (f,g) if b == 0. + + Preconditions: b in {0,1}. +*/ + +void fe_cmov(fe f, const fe g, unsigned int b) { + int32_t f0 = f[0]; + int32_t f1 = f[1]; + int32_t f2 = f[2]; + int32_t f3 = f[3]; + int32_t f4 = f[4]; + int32_t f5 = f[5]; + int32_t f6 = f[6]; + int32_t f7 = f[7]; + int32_t f8 = f[8]; + int32_t f9 = f[9]; + int32_t g0 = g[0]; + int32_t g1 = g[1]; + int32_t g2 = g[2]; + int32_t g3 = g[3]; + int32_t g4 = g[4]; + int32_t g5 = g[5]; + int32_t g6 = g[6]; + int32_t g7 = g[7]; + int32_t g8 = g[8]; + int32_t g9 = g[9]; + int32_t x0 = f0 ^ g0; + int32_t x1 = f1 ^ g1; + int32_t x2 = f2 ^ g2; + int32_t x3 = f3 ^ g3; + int32_t x4 = f4 ^ g4; + int32_t x5 = f5 ^ g5; + int32_t x6 = f6 ^ g6; + int32_t x7 = f7 ^ g7; + int32_t x8 = f8 ^ g8; + int32_t x9 = f9 ^ g9; + + b = (unsigned int) (- (int) b); /* silence warning */ + x0 &= b; + x1 &= b; + x2 &= b; + x3 &= b; + x4 &= b; + x5 &= b; + x6 &= b; + x7 &= b; + x8 &= b; + x9 &= b; + + f[0] = f0 ^ x0; + f[1] = f1 ^ x1; + f[2] = f2 ^ x2; + f[3] = f3 ^ x3; + f[4] = f4 ^ x4; + f[5] = f5 ^ x5; + f[6] = f6 ^ x6; + f[7] = f7 ^ x7; + f[8] = f8 ^ x8; + f[9] = f9 ^ x9; +} + +/* + Replace (f,g) with (g,f) if b == 1; + replace (f,g) with (f,g) if b == 0. + + Preconditions: b in {0,1}. +*/ + +void fe_cswap(fe f,fe g,unsigned int b) { + int32_t f0 = f[0]; + int32_t f1 = f[1]; + int32_t f2 = f[2]; + int32_t f3 = f[3]; + int32_t f4 = f[4]; + int32_t f5 = f[5]; + int32_t f6 = f[6]; + int32_t f7 = f[7]; + int32_t f8 = f[8]; + int32_t f9 = f[9]; + int32_t g0 = g[0]; + int32_t g1 = g[1]; + int32_t g2 = g[2]; + int32_t g3 = g[3]; + int32_t g4 = g[4]; + int32_t g5 = g[5]; + int32_t g6 = g[6]; + int32_t g7 = g[7]; + int32_t g8 = g[8]; + int32_t g9 = g[9]; + int32_t x0 = f0 ^ g0; + int32_t x1 = f1 ^ g1; + int32_t x2 = f2 ^ g2; + int32_t x3 = f3 ^ g3; + int32_t x4 = f4 ^ g4; + int32_t x5 = f5 ^ g5; + int32_t x6 = f6 ^ g6; + int32_t x7 = f7 ^ g7; + int32_t x8 = f8 ^ g8; + int32_t x9 = f9 ^ g9; + b = (unsigned int) (- (int) b); /* silence warning */ + x0 &= b; + x1 &= b; + x2 &= b; + x3 &= b; + x4 &= b; + x5 &= b; + x6 &= b; + x7 &= b; + x8 &= b; + x9 &= b; + f[0] = f0 ^ x0; + f[1] = f1 ^ x1; + f[2] = f2 ^ x2; + f[3] = f3 ^ x3; + f[4] = f4 ^ x4; + f[5] = f5 ^ x5; + f[6] = f6 ^ x6; + f[7] = f7 ^ x7; + f[8] = f8 ^ x8; + f[9] = f9 ^ x9; + g[0] = g0 ^ x0; + g[1] = g1 ^ x1; + g[2] = g2 ^ x2; + g[3] = g3 ^ x3; + g[4] = g4 ^ x4; + g[5] = g5 ^ x5; + g[6] = g6 ^ x6; + g[7] = g7 ^ x7; + g[8] = g8 ^ x8; + g[9] = g9 ^ x9; +} + + + +/* + h = f +*/ + +void fe_copy(fe h, const fe f) { + int32_t f0 = f[0]; + int32_t f1 = f[1]; + int32_t f2 = f[2]; + int32_t f3 = f[3]; + int32_t f4 = f[4]; + int32_t f5 = f[5]; + int32_t f6 = f[6]; + int32_t f7 = f[7]; + int32_t f8 = f[8]; + int32_t f9 = f[9]; + + h[0] = f0; + h[1] = f1; + h[2] = f2; + h[3] = f3; + h[4] = f4; + h[5] = f5; + h[6] = f6; + h[7] = f7; + h[8] = f8; + h[9] = f9; +} + + + +/* + Ignores top bit of h. +*/ + +void fe_frombytes(fe h, const unsigned char *s) { + int64_t h0 = load_4(s); + int64_t h1 = load_3(s + 4) << 6; + int64_t h2 = load_3(s + 7) << 5; + int64_t h3 = load_3(s + 10) << 3; + int64_t h4 = load_3(s + 13) << 2; + int64_t h5 = load_4(s + 16); + int64_t h6 = load_3(s + 20) << 7; + int64_t h7 = load_3(s + 23) << 5; + int64_t h8 = load_3(s + 26) << 4; + int64_t h9 = (load_3(s + 29) & 8388607) << 2; + int64_t carry0; + int64_t carry1; + int64_t carry2; + int64_t carry3; + int64_t carry4; + int64_t carry5; + int64_t carry6; + int64_t carry7; + int64_t carry8; + int64_t carry9; + + carry9 = (h9 + (int64_t) (1 << 24)) >> 25; + h0 += carry9 * 19; + h9 -= carry9 << 25; + carry1 = (h1 + (int64_t) (1 << 24)) >> 25; + h2 += carry1; + h1 -= carry1 << 25; + carry3 = (h3 + (int64_t) (1 << 24)) >> 25; + h4 += carry3; + h3 -= carry3 << 25; + carry5 = (h5 + (int64_t) (1 << 24)) >> 25; + h6 += carry5; + h5 -= carry5 << 25; + carry7 = (h7 + (int64_t) (1 << 24)) >> 25; + h8 += carry7; + h7 -= carry7 << 25; + carry0 = (h0 + (int64_t) (1 << 25)) >> 26; + h1 += carry0; + h0 -= carry0 << 26; + carry2 = (h2 + (int64_t) (1 << 25)) >> 26; + h3 += carry2; + h2 -= carry2 << 26; + carry4 = (h4 + (int64_t) (1 << 25)) >> 26; + h5 += carry4; + h4 -= carry4 << 26; + carry6 = (h6 + (int64_t) (1 << 25)) >> 26; + h7 += carry6; + h6 -= carry6 << 26; + carry8 = (h8 + (int64_t) (1 << 25)) >> 26; + h9 += carry8; + h8 -= carry8 << 26; + + h[0] = (int32_t) h0; + h[1] = (int32_t) h1; + h[2] = (int32_t) h2; + h[3] = (int32_t) h3; + h[4] = (int32_t) h4; + h[5] = (int32_t) h5; + h[6] = (int32_t) h6; + h[7] = (int32_t) h7; + h[8] = (int32_t) h8; + h[9] = (int32_t) h9; +} + + + +void fe_invert(fe out, const fe z) { + fe t0; + fe t1; + fe t2; + fe t3; + int i; + + fe_sq(t0, z); + + for (i = 1; i < 1; ++i) { + fe_sq(t0, t0); + } + + fe_sq(t1, t0); + + for (i = 1; i < 2; ++i) { + fe_sq(t1, t1); + } + + fe_mul(t1, z, t1); + fe_mul(t0, t0, t1); + fe_sq(t2, t0); + + for (i = 1; i < 1; ++i) { + fe_sq(t2, t2); + } + + fe_mul(t1, t1, t2); + fe_sq(t2, t1); + + for (i = 1; i < 5; ++i) { + fe_sq(t2, t2); + } + + fe_mul(t1, t2, t1); + fe_sq(t2, t1); + + for (i = 1; i < 10; ++i) { + fe_sq(t2, t2); + } + + fe_mul(t2, t2, t1); + fe_sq(t3, t2); + + for (i = 1; i < 20; ++i) { + fe_sq(t3, t3); + } + + fe_mul(t2, t3, t2); + fe_sq(t2, t2); + + for (i = 1; i < 10; ++i) { + fe_sq(t2, t2); + } + + fe_mul(t1, t2, t1); + fe_sq(t2, t1); + + for (i = 1; i < 50; ++i) { + fe_sq(t2, t2); + } + + fe_mul(t2, t2, t1); + fe_sq(t3, t2); + + for (i = 1; i < 100; ++i) { + fe_sq(t3, t3); + } + + fe_mul(t2, t3, t2); + fe_sq(t2, t2); + + for (i = 1; i < 50; ++i) { + fe_sq(t2, t2); + } + + fe_mul(t1, t2, t1); + fe_sq(t1, t1); + + for (i = 1; i < 5; ++i) { + fe_sq(t1, t1); + } + + fe_mul(out, t1, t0); +} + + + +/* + return 1 if f is in {1,3,5,...,q-2} + return 0 if f is in {0,2,4,...,q-1} + + Preconditions: + |f| bounded by 1.1*2^26,1.1*2^25,1.1*2^26,1.1*2^25,etc. +*/ + +int fe_isnegative(const fe f) { + unsigned char s[32]; + + fe_tobytes(s, f); + + return s[0] & 1; +} + + + +/* + return 1 if f == 0 + return 0 if f != 0 + + Preconditions: + |f| bounded by 1.1*2^26,1.1*2^25,1.1*2^26,1.1*2^25,etc. +*/ + +int fe_isnonzero(const fe f) { + unsigned char s[32]; + unsigned char r; + + fe_tobytes(s, f); + + r = s[0]; + #define F(i) r |= s[i] + F(1); + F(2); + F(3); + F(4); + F(5); + F(6); + F(7); + F(8); + F(9); + F(10); + F(11); + F(12); + F(13); + F(14); + F(15); + F(16); + F(17); + F(18); + F(19); + F(20); + F(21); + F(22); + F(23); + F(24); + F(25); + F(26); + F(27); + F(28); + F(29); + F(30); + F(31); + #undef F + + return r != 0; +} + + + +/* + h = f * g + Can overlap h with f or g. + + Preconditions: + |f| bounded by 1.65*2^26,1.65*2^25,1.65*2^26,1.65*2^25,etc. + |g| bounded by 1.65*2^26,1.65*2^25,1.65*2^26,1.65*2^25,etc. + + Postconditions: + |h| bounded by 1.01*2^25,1.01*2^24,1.01*2^25,1.01*2^24,etc. + */ + + /* + Notes on implementation strategy: + + Using schoolbook multiplication. + Karatsuba would save a little in some cost models. + + Most multiplications by 2 and 19 are 32-bit precomputations; + cheaper than 64-bit postcomputations. + + There is one remaining multiplication by 19 in the carry chain; + one *19 precomputation can be merged into this, + but the resulting data flow is considerably less clean. + + There are 12 carries below. + 10 of them are 2-way parallelizable and vectorizable. + Can get away with 11 carries, but then data flow is much deeper. + + With tighter constraints on inputs can squeeze carries into int32. +*/ + +void fe_mul(fe h, const fe f, const fe g) { + int32_t f0 = f[0]; + int32_t f1 = f[1]; + int32_t f2 = f[2]; + int32_t f3 = f[3]; + int32_t f4 = f[4]; + int32_t f5 = f[5]; + int32_t f6 = f[6]; + int32_t f7 = f[7]; + int32_t f8 = f[8]; + int32_t f9 = f[9]; + int32_t g0 = g[0]; + int32_t g1 = g[1]; + int32_t g2 = g[2]; + int32_t g3 = g[3]; + int32_t g4 = g[4]; + int32_t g5 = g[5]; + int32_t g6 = g[6]; + int32_t g7 = g[7]; + int32_t g8 = g[8]; + int32_t g9 = g[9]; + int32_t g1_19 = 19 * g1; /* 1.959375*2^29 */ + int32_t g2_19 = 19 * g2; /* 1.959375*2^30; still ok */ + int32_t g3_19 = 19 * g3; + int32_t g4_19 = 19 * g4; + int32_t g5_19 = 19 * g5; + int32_t g6_19 = 19 * g6; + int32_t g7_19 = 19 * g7; + int32_t g8_19 = 19 * g8; + int32_t g9_19 = 19 * g9; + int32_t f1_2 = 2 * f1; + int32_t f3_2 = 2 * f3; + int32_t f5_2 = 2 * f5; + int32_t f7_2 = 2 * f7; + int32_t f9_2 = 2 * f9; + int64_t f0g0 = f0 * (int64_t) g0; + int64_t f0g1 = f0 * (int64_t) g1; + int64_t f0g2 = f0 * (int64_t) g2; + int64_t f0g3 = f0 * (int64_t) g3; + int64_t f0g4 = f0 * (int64_t) g4; + int64_t f0g5 = f0 * (int64_t) g5; + int64_t f0g6 = f0 * (int64_t) g6; + int64_t f0g7 = f0 * (int64_t) g7; + int64_t f0g8 = f0 * (int64_t) g8; + int64_t f0g9 = f0 * (int64_t) g9; + int64_t f1g0 = f1 * (int64_t) g0; + int64_t f1g1_2 = f1_2 * (int64_t) g1; + int64_t f1g2 = f1 * (int64_t) g2; + int64_t f1g3_2 = f1_2 * (int64_t) g3; + int64_t f1g4 = f1 * (int64_t) g4; + int64_t f1g5_2 = f1_2 * (int64_t) g5; + int64_t f1g6 = f1 * (int64_t) g6; + int64_t f1g7_2 = f1_2 * (int64_t) g7; + int64_t f1g8 = f1 * (int64_t) g8; + int64_t f1g9_38 = f1_2 * (int64_t) g9_19; + int64_t f2g0 = f2 * (int64_t) g0; + int64_t f2g1 = f2 * (int64_t) g1; + int64_t f2g2 = f2 * (int64_t) g2; + int64_t f2g3 = f2 * (int64_t) g3; + int64_t f2g4 = f2 * (int64_t) g4; + int64_t f2g5 = f2 * (int64_t) g5; + int64_t f2g6 = f2 * (int64_t) g6; + int64_t f2g7 = f2 * (int64_t) g7; + int64_t f2g8_19 = f2 * (int64_t) g8_19; + int64_t f2g9_19 = f2 * (int64_t) g9_19; + int64_t f3g0 = f3 * (int64_t) g0; + int64_t f3g1_2 = f3_2 * (int64_t) g1; + int64_t f3g2 = f3 * (int64_t) g2; + int64_t f3g3_2 = f3_2 * (int64_t) g3; + int64_t f3g4 = f3 * (int64_t) g4; + int64_t f3g5_2 = f3_2 * (int64_t) g5; + int64_t f3g6 = f3 * (int64_t) g6; + int64_t f3g7_38 = f3_2 * (int64_t) g7_19; + int64_t f3g8_19 = f3 * (int64_t) g8_19; + int64_t f3g9_38 = f3_2 * (int64_t) g9_19; + int64_t f4g0 = f4 * (int64_t) g0; + int64_t f4g1 = f4 * (int64_t) g1; + int64_t f4g2 = f4 * (int64_t) g2; + int64_t f4g3 = f4 * (int64_t) g3; + int64_t f4g4 = f4 * (int64_t) g4; + int64_t f4g5 = f4 * (int64_t) g5; + int64_t f4g6_19 = f4 * (int64_t) g6_19; + int64_t f4g7_19 = f4 * (int64_t) g7_19; + int64_t f4g8_19 = f4 * (int64_t) g8_19; + int64_t f4g9_19 = f4 * (int64_t) g9_19; + int64_t f5g0 = f5 * (int64_t) g0; + int64_t f5g1_2 = f5_2 * (int64_t) g1; + int64_t f5g2 = f5 * (int64_t) g2; + int64_t f5g3_2 = f5_2 * (int64_t) g3; + int64_t f5g4 = f5 * (int64_t) g4; + int64_t f5g5_38 = f5_2 * (int64_t) g5_19; + int64_t f5g6_19 = f5 * (int64_t) g6_19; + int64_t f5g7_38 = f5_2 * (int64_t) g7_19; + int64_t f5g8_19 = f5 * (int64_t) g8_19; + int64_t f5g9_38 = f5_2 * (int64_t) g9_19; + int64_t f6g0 = f6 * (int64_t) g0; + int64_t f6g1 = f6 * (int64_t) g1; + int64_t f6g2 = f6 * (int64_t) g2; + int64_t f6g3 = f6 * (int64_t) g3; + int64_t f6g4_19 = f6 * (int64_t) g4_19; + int64_t f6g5_19 = f6 * (int64_t) g5_19; + int64_t f6g6_19 = f6 * (int64_t) g6_19; + int64_t f6g7_19 = f6 * (int64_t) g7_19; + int64_t f6g8_19 = f6 * (int64_t) g8_19; + int64_t f6g9_19 = f6 * (int64_t) g9_19; + int64_t f7g0 = f7 * (int64_t) g0; + int64_t f7g1_2 = f7_2 * (int64_t) g1; + int64_t f7g2 = f7 * (int64_t) g2; + int64_t f7g3_38 = f7_2 * (int64_t) g3_19; + int64_t f7g4_19 = f7 * (int64_t) g4_19; + int64_t f7g5_38 = f7_2 * (int64_t) g5_19; + int64_t f7g6_19 = f7 * (int64_t) g6_19; + int64_t f7g7_38 = f7_2 * (int64_t) g7_19; + int64_t f7g8_19 = f7 * (int64_t) g8_19; + int64_t f7g9_38 = f7_2 * (int64_t) g9_19; + int64_t f8g0 = f8 * (int64_t) g0; + int64_t f8g1 = f8 * (int64_t) g1; + int64_t f8g2_19 = f8 * (int64_t) g2_19; + int64_t f8g3_19 = f8 * (int64_t) g3_19; + int64_t f8g4_19 = f8 * (int64_t) g4_19; + int64_t f8g5_19 = f8 * (int64_t) g5_19; + int64_t f8g6_19 = f8 * (int64_t) g6_19; + int64_t f8g7_19 = f8 * (int64_t) g7_19; + int64_t f8g8_19 = f8 * (int64_t) g8_19; + int64_t f8g9_19 = f8 * (int64_t) g9_19; + int64_t f9g0 = f9 * (int64_t) g0; + int64_t f9g1_38 = f9_2 * (int64_t) g1_19; + int64_t f9g2_19 = f9 * (int64_t) g2_19; + int64_t f9g3_38 = f9_2 * (int64_t) g3_19; + int64_t f9g4_19 = f9 * (int64_t) g4_19; + int64_t f9g5_38 = f9_2 * (int64_t) g5_19; + int64_t f9g6_19 = f9 * (int64_t) g6_19; + int64_t f9g7_38 = f9_2 * (int64_t) g7_19; + int64_t f9g8_19 = f9 * (int64_t) g8_19; + int64_t f9g9_38 = f9_2 * (int64_t) g9_19; + int64_t h0 = f0g0 + f1g9_38 + f2g8_19 + f3g7_38 + f4g6_19 + f5g5_38 + f6g4_19 + f7g3_38 + f8g2_19 + f9g1_38; + int64_t h1 = f0g1 + f1g0 + f2g9_19 + f3g8_19 + f4g7_19 + f5g6_19 + f6g5_19 + f7g4_19 + f8g3_19 + f9g2_19; + int64_t h2 = f0g2 + f1g1_2 + f2g0 + f3g9_38 + f4g8_19 + f5g7_38 + f6g6_19 + f7g5_38 + f8g4_19 + f9g3_38; + int64_t h3 = f0g3 + f1g2 + f2g1 + f3g0 + f4g9_19 + f5g8_19 + f6g7_19 + f7g6_19 + f8g5_19 + f9g4_19; + int64_t h4 = f0g4 + f1g3_2 + f2g2 + f3g1_2 + f4g0 + f5g9_38 + f6g8_19 + f7g7_38 + f8g6_19 + f9g5_38; + int64_t h5 = f0g5 + f1g4 + f2g3 + f3g2 + f4g1 + f5g0 + f6g9_19 + f7g8_19 + f8g7_19 + f9g6_19; + int64_t h6 = f0g6 + f1g5_2 + f2g4 + f3g3_2 + f4g2 + f5g1_2 + f6g0 + f7g9_38 + f8g8_19 + f9g7_38; + int64_t h7 = f0g7 + f1g6 + f2g5 + f3g4 + f4g3 + f5g2 + f6g1 + f7g0 + f8g9_19 + f9g8_19; + int64_t h8 = f0g8 + f1g7_2 + f2g6 + f3g5_2 + f4g4 + f5g3_2 + f6g2 + f7g1_2 + f8g0 + f9g9_38; + int64_t h9 = f0g9 + f1g8 + f2g7 + f3g6 + f4g5 + f5g4 + f6g3 + f7g2 + f8g1 + f9g0 ; + int64_t carry0; + int64_t carry1; + int64_t carry2; + int64_t carry3; + int64_t carry4; + int64_t carry5; + int64_t carry6; + int64_t carry7; + int64_t carry8; + int64_t carry9; + + carry0 = (h0 + (int64_t) (1 << 25)) >> 26; + h1 += carry0; + h0 -= carry0 << 26; + carry4 = (h4 + (int64_t) (1 << 25)) >> 26; + h5 += carry4; + h4 -= carry4 << 26; + + carry1 = (h1 + (int64_t) (1 << 24)) >> 25; + h2 += carry1; + h1 -= carry1 << 25; + carry5 = (h5 + (int64_t) (1 << 24)) >> 25; + h6 += carry5; + h5 -= carry5 << 25; + + carry2 = (h2 + (int64_t) (1 << 25)) >> 26; + h3 += carry2; + h2 -= carry2 << 26; + carry6 = (h6 + (int64_t) (1 << 25)) >> 26; + h7 += carry6; + h6 -= carry6 << 26; + + carry3 = (h3 + (int64_t) (1 << 24)) >> 25; + h4 += carry3; + h3 -= carry3 << 25; + carry7 = (h7 + (int64_t) (1 << 24)) >> 25; + h8 += carry7; + h7 -= carry7 << 25; + + carry4 = (h4 + (int64_t) (1 << 25)) >> 26; + h5 += carry4; + h4 -= carry4 << 26; + carry8 = (h8 + (int64_t) (1 << 25)) >> 26; + h9 += carry8; + h8 -= carry8 << 26; + + carry9 = (h9 + (int64_t) (1 << 24)) >> 25; + h0 += carry9 * 19; + h9 -= carry9 << 25; + + carry0 = (h0 + (int64_t) (1 << 25)) >> 26; + h1 += carry0; + h0 -= carry0 << 26; + + h[0] = (int32_t) h0; + h[1] = (int32_t) h1; + h[2] = (int32_t) h2; + h[3] = (int32_t) h3; + h[4] = (int32_t) h4; + h[5] = (int32_t) h5; + h[6] = (int32_t) h6; + h[7] = (int32_t) h7; + h[8] = (int32_t) h8; + h[9] = (int32_t) h9; +} + + +/* +h = f * 121666 +Can overlap h with f. + +Preconditions: + |f| bounded by 1.1*2^26,1.1*2^25,1.1*2^26,1.1*2^25,etc. + +Postconditions: + |h| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc. +*/ + +void fe_mul121666(fe h, fe f) { + int32_t f0 = f[0]; + int32_t f1 = f[1]; + int32_t f2 = f[2]; + int32_t f3 = f[3]; + int32_t f4 = f[4]; + int32_t f5 = f[5]; + int32_t f6 = f[6]; + int32_t f7 = f[7]; + int32_t f8 = f[8]; + int32_t f9 = f[9]; + int64_t h0 = f0 * (int64_t) 121666; + int64_t h1 = f1 * (int64_t) 121666; + int64_t h2 = f2 * (int64_t) 121666; + int64_t h3 = f3 * (int64_t) 121666; + int64_t h4 = f4 * (int64_t) 121666; + int64_t h5 = f5 * (int64_t) 121666; + int64_t h6 = f6 * (int64_t) 121666; + int64_t h7 = f7 * (int64_t) 121666; + int64_t h8 = f8 * (int64_t) 121666; + int64_t h9 = f9 * (int64_t) 121666; + int64_t carry0; + int64_t carry1; + int64_t carry2; + int64_t carry3; + int64_t carry4; + int64_t carry5; + int64_t carry6; + int64_t carry7; + int64_t carry8; + int64_t carry9; + + carry9 = (h9 + (int64_t) (1<<24)) >> 25; h0 += carry9 * 19; h9 -= carry9 << 25; + carry1 = (h1 + (int64_t) (1<<24)) >> 25; h2 += carry1; h1 -= carry1 << 25; + carry3 = (h3 + (int64_t) (1<<24)) >> 25; h4 += carry3; h3 -= carry3 << 25; + carry5 = (h5 + (int64_t) (1<<24)) >> 25; h6 += carry5; h5 -= carry5 << 25; + carry7 = (h7 + (int64_t) (1<<24)) >> 25; h8 += carry7; h7 -= carry7 << 25; + + carry0 = (h0 + (int64_t) (1<<25)) >> 26; h1 += carry0; h0 -= carry0 << 26; + carry2 = (h2 + (int64_t) (1<<25)) >> 26; h3 += carry2; h2 -= carry2 << 26; + carry4 = (h4 + (int64_t) (1<<25)) >> 26; h5 += carry4; h4 -= carry4 << 26; + carry6 = (h6 + (int64_t) (1<<25)) >> 26; h7 += carry6; h6 -= carry6 << 26; + carry8 = (h8 + (int64_t) (1<<25)) >> 26; h9 += carry8; h8 -= carry8 << 26; + + h[0] = (int32_t) h0; + h[1] = (int32_t) h1; + h[2] = (int32_t) h2; + h[3] = (int32_t) h3; + h[4] = (int32_t) h4; + h[5] = (int32_t) h5; + h[6] = (int32_t) h6; + h[7] = (int32_t) h7; + h[8] = (int32_t) h8; + h[9] = (int32_t) h9; +} + + +/* +h = -f + +Preconditions: + |f| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc. + +Postconditions: + |h| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc. +*/ + +void fe_neg(fe h, const fe f) { + int32_t f0 = f[0]; + int32_t f1 = f[1]; + int32_t f2 = f[2]; + int32_t f3 = f[3]; + int32_t f4 = f[4]; + int32_t f5 = f[5]; + int32_t f6 = f[6]; + int32_t f7 = f[7]; + int32_t f8 = f[8]; + int32_t f9 = f[9]; + int32_t h0 = -f0; + int32_t h1 = -f1; + int32_t h2 = -f2; + int32_t h3 = -f3; + int32_t h4 = -f4; + int32_t h5 = -f5; + int32_t h6 = -f6; + int32_t h7 = -f7; + int32_t h8 = -f8; + int32_t h9 = -f9; + + h[0] = h0; + h[1] = h1; + h[2] = h2; + h[3] = h3; + h[4] = h4; + h[5] = h5; + h[6] = h6; + h[7] = h7; + h[8] = h8; + h[9] = h9; +} + + +void fe_pow22523(fe out, const fe z) { + fe t0; + fe t1; + fe t2; + int i; + fe_sq(t0, z); + + for (i = 1; i < 1; ++i) { + fe_sq(t0, t0); + } + + fe_sq(t1, t0); + + for (i = 1; i < 2; ++i) { + fe_sq(t1, t1); + } + + fe_mul(t1, z, t1); + fe_mul(t0, t0, t1); + fe_sq(t0, t0); + + for (i = 1; i < 1; ++i) { + fe_sq(t0, t0); + } + + fe_mul(t0, t1, t0); + fe_sq(t1, t0); + + for (i = 1; i < 5; ++i) { + fe_sq(t1, t1); + } + + fe_mul(t0, t1, t0); + fe_sq(t1, t0); + + for (i = 1; i < 10; ++i) { + fe_sq(t1, t1); + } + + fe_mul(t1, t1, t0); + fe_sq(t2, t1); + + for (i = 1; i < 20; ++i) { + fe_sq(t2, t2); + } + + fe_mul(t1, t2, t1); + fe_sq(t1, t1); + + for (i = 1; i < 10; ++i) { + fe_sq(t1, t1); + } + + fe_mul(t0, t1, t0); + fe_sq(t1, t0); + + for (i = 1; i < 50; ++i) { + fe_sq(t1, t1); + } + + fe_mul(t1, t1, t0); + fe_sq(t2, t1); + + for (i = 1; i < 100; ++i) { + fe_sq(t2, t2); + } + + fe_mul(t1, t2, t1); + fe_sq(t1, t1); + + for (i = 1; i < 50; ++i) { + fe_sq(t1, t1); + } + + fe_mul(t0, t1, t0); + fe_sq(t0, t0); + + for (i = 1; i < 2; ++i) { + fe_sq(t0, t0); + } + + fe_mul(out, t0, z); + return; +} + + +/* +h = f * f +Can overlap h with f. + +Preconditions: + |f| bounded by 1.65*2^26,1.65*2^25,1.65*2^26,1.65*2^25,etc. + +Postconditions: + |h| bounded by 1.01*2^25,1.01*2^24,1.01*2^25,1.01*2^24,etc. +*/ + +/* +See fe_mul.c for discussion of implementation strategy. +*/ + +void fe_sq(fe h, const fe f) { + int32_t f0 = f[0]; + int32_t f1 = f[1]; + int32_t f2 = f[2]; + int32_t f3 = f[3]; + int32_t f4 = f[4]; + int32_t f5 = f[5]; + int32_t f6 = f[6]; + int32_t f7 = f[7]; + int32_t f8 = f[8]; + int32_t f9 = f[9]; + int32_t f0_2 = 2 * f0; + int32_t f1_2 = 2 * f1; + int32_t f2_2 = 2 * f2; + int32_t f3_2 = 2 * f3; + int32_t f4_2 = 2 * f4; + int32_t f5_2 = 2 * f5; + int32_t f6_2 = 2 * f6; + int32_t f7_2 = 2 * f7; + int32_t f5_38 = 38 * f5; /* 1.959375*2^30 */ + int32_t f6_19 = 19 * f6; /* 1.959375*2^30 */ + int32_t f7_38 = 38 * f7; /* 1.959375*2^30 */ + int32_t f8_19 = 19 * f8; /* 1.959375*2^30 */ + int32_t f9_38 = 38 * f9; /* 1.959375*2^30 */ + int64_t f0f0 = f0 * (int64_t) f0; + int64_t f0f1_2 = f0_2 * (int64_t) f1; + int64_t f0f2_2 = f0_2 * (int64_t) f2; + int64_t f0f3_2 = f0_2 * (int64_t) f3; + int64_t f0f4_2 = f0_2 * (int64_t) f4; + int64_t f0f5_2 = f0_2 * (int64_t) f5; + int64_t f0f6_2 = f0_2 * (int64_t) f6; + int64_t f0f7_2 = f0_2 * (int64_t) f7; + int64_t f0f8_2 = f0_2 * (int64_t) f8; + int64_t f0f9_2 = f0_2 * (int64_t) f9; + int64_t f1f1_2 = f1_2 * (int64_t) f1; + int64_t f1f2_2 = f1_2 * (int64_t) f2; + int64_t f1f3_4 = f1_2 * (int64_t) f3_2; + int64_t f1f4_2 = f1_2 * (int64_t) f4; + int64_t f1f5_4 = f1_2 * (int64_t) f5_2; + int64_t f1f6_2 = f1_2 * (int64_t) f6; + int64_t f1f7_4 = f1_2 * (int64_t) f7_2; + int64_t f1f8_2 = f1_2 * (int64_t) f8; + int64_t f1f9_76 = f1_2 * (int64_t) f9_38; + int64_t f2f2 = f2 * (int64_t) f2; + int64_t f2f3_2 = f2_2 * (int64_t) f3; + int64_t f2f4_2 = f2_2 * (int64_t) f4; + int64_t f2f5_2 = f2_2 * (int64_t) f5; + int64_t f2f6_2 = f2_2 * (int64_t) f6; + int64_t f2f7_2 = f2_2 * (int64_t) f7; + int64_t f2f8_38 = f2_2 * (int64_t) f8_19; + int64_t f2f9_38 = f2 * (int64_t) f9_38; + int64_t f3f3_2 = f3_2 * (int64_t) f3; + int64_t f3f4_2 = f3_2 * (int64_t) f4; + int64_t f3f5_4 = f3_2 * (int64_t) f5_2; + int64_t f3f6_2 = f3_2 * (int64_t) f6; + int64_t f3f7_76 = f3_2 * (int64_t) f7_38; + int64_t f3f8_38 = f3_2 * (int64_t) f8_19; + int64_t f3f9_76 = f3_2 * (int64_t) f9_38; + int64_t f4f4 = f4 * (int64_t) f4; + int64_t f4f5_2 = f4_2 * (int64_t) f5; + int64_t f4f6_38 = f4_2 * (int64_t) f6_19; + int64_t f4f7_38 = f4 * (int64_t) f7_38; + int64_t f4f8_38 = f4_2 * (int64_t) f8_19; + int64_t f4f9_38 = f4 * (int64_t) f9_38; + int64_t f5f5_38 = f5 * (int64_t) f5_38; + int64_t f5f6_38 = f5_2 * (int64_t) f6_19; + int64_t f5f7_76 = f5_2 * (int64_t) f7_38; + int64_t f5f8_38 = f5_2 * (int64_t) f8_19; + int64_t f5f9_76 = f5_2 * (int64_t) f9_38; + int64_t f6f6_19 = f6 * (int64_t) f6_19; + int64_t f6f7_38 = f6 * (int64_t) f7_38; + int64_t f6f8_38 = f6_2 * (int64_t) f8_19; + int64_t f6f9_38 = f6 * (int64_t) f9_38; + int64_t f7f7_38 = f7 * (int64_t) f7_38; + int64_t f7f8_38 = f7_2 * (int64_t) f8_19; + int64_t f7f9_76 = f7_2 * (int64_t) f9_38; + int64_t f8f8_19 = f8 * (int64_t) f8_19; + int64_t f8f9_38 = f8 * (int64_t) f9_38; + int64_t f9f9_38 = f9 * (int64_t) f9_38; + int64_t h0 = f0f0 + f1f9_76 + f2f8_38 + f3f7_76 + f4f6_38 + f5f5_38; + int64_t h1 = f0f1_2 + f2f9_38 + f3f8_38 + f4f7_38 + f5f6_38; + int64_t h2 = f0f2_2 + f1f1_2 + f3f9_76 + f4f8_38 + f5f7_76 + f6f6_19; + int64_t h3 = f0f3_2 + f1f2_2 + f4f9_38 + f5f8_38 + f6f7_38; + int64_t h4 = f0f4_2 + f1f3_4 + f2f2 + f5f9_76 + f6f8_38 + f7f7_38; + int64_t h5 = f0f5_2 + f1f4_2 + f2f3_2 + f6f9_38 + f7f8_38; + int64_t h6 = f0f6_2 + f1f5_4 + f2f4_2 + f3f3_2 + f7f9_76 + f8f8_19; + int64_t h7 = f0f7_2 + f1f6_2 + f2f5_2 + f3f4_2 + f8f9_38; + int64_t h8 = f0f8_2 + f1f7_4 + f2f6_2 + f3f5_4 + f4f4 + f9f9_38; + int64_t h9 = f0f9_2 + f1f8_2 + f2f7_2 + f3f6_2 + f4f5_2; + int64_t carry0; + int64_t carry1; + int64_t carry2; + int64_t carry3; + int64_t carry4; + int64_t carry5; + int64_t carry6; + int64_t carry7; + int64_t carry8; + int64_t carry9; + carry0 = (h0 + (int64_t) (1 << 25)) >> 26; + h1 += carry0; + h0 -= carry0 << 26; + carry4 = (h4 + (int64_t) (1 << 25)) >> 26; + h5 += carry4; + h4 -= carry4 << 26; + carry1 = (h1 + (int64_t) (1 << 24)) >> 25; + h2 += carry1; + h1 -= carry1 << 25; + carry5 = (h5 + (int64_t) (1 << 24)) >> 25; + h6 += carry5; + h5 -= carry5 << 25; + carry2 = (h2 + (int64_t) (1 << 25)) >> 26; + h3 += carry2; + h2 -= carry2 << 26; + carry6 = (h6 + (int64_t) (1 << 25)) >> 26; + h7 += carry6; + h6 -= carry6 << 26; + carry3 = (h3 + (int64_t) (1 << 24)) >> 25; + h4 += carry3; + h3 -= carry3 << 25; + carry7 = (h7 + (int64_t) (1 << 24)) >> 25; + h8 += carry7; + h7 -= carry7 << 25; + carry4 = (h4 + (int64_t) (1 << 25)) >> 26; + h5 += carry4; + h4 -= carry4 << 26; + carry8 = (h8 + (int64_t) (1 << 25)) >> 26; + h9 += carry8; + h8 -= carry8 << 26; + carry9 = (h9 + (int64_t) (1 << 24)) >> 25; + h0 += carry9 * 19; + h9 -= carry9 << 25; + carry0 = (h0 + (int64_t) (1 << 25)) >> 26; + h1 += carry0; + h0 -= carry0 << 26; + h[0] = (int32_t) h0; + h[1] = (int32_t) h1; + h[2] = (int32_t) h2; + h[3] = (int32_t) h3; + h[4] = (int32_t) h4; + h[5] = (int32_t) h5; + h[6] = (int32_t) h6; + h[7] = (int32_t) h7; + h[8] = (int32_t) h8; + h[9] = (int32_t) h9; +} + + +/* +h = 2 * f * f +Can overlap h with f. + +Preconditions: + |f| bounded by 1.65*2^26,1.65*2^25,1.65*2^26,1.65*2^25,etc. + +Postconditions: + |h| bounded by 1.01*2^25,1.01*2^24,1.01*2^25,1.01*2^24,etc. +*/ + +/* +See fe_mul.c for discussion of implementation strategy. +*/ + +void fe_sq2(fe h, const fe f) { + int32_t f0 = f[0]; + int32_t f1 = f[1]; + int32_t f2 = f[2]; + int32_t f3 = f[3]; + int32_t f4 = f[4]; + int32_t f5 = f[5]; + int32_t f6 = f[6]; + int32_t f7 = f[7]; + int32_t f8 = f[8]; + int32_t f9 = f[9]; + int32_t f0_2 = 2 * f0; + int32_t f1_2 = 2 * f1; + int32_t f2_2 = 2 * f2; + int32_t f3_2 = 2 * f3; + int32_t f4_2 = 2 * f4; + int32_t f5_2 = 2 * f5; + int32_t f6_2 = 2 * f6; + int32_t f7_2 = 2 * f7; + int32_t f5_38 = 38 * f5; /* 1.959375*2^30 */ + int32_t f6_19 = 19 * f6; /* 1.959375*2^30 */ + int32_t f7_38 = 38 * f7; /* 1.959375*2^30 */ + int32_t f8_19 = 19 * f8; /* 1.959375*2^30 */ + int32_t f9_38 = 38 * f9; /* 1.959375*2^30 */ + int64_t f0f0 = f0 * (int64_t) f0; + int64_t f0f1_2 = f0_2 * (int64_t) f1; + int64_t f0f2_2 = f0_2 * (int64_t) f2; + int64_t f0f3_2 = f0_2 * (int64_t) f3; + int64_t f0f4_2 = f0_2 * (int64_t) f4; + int64_t f0f5_2 = f0_2 * (int64_t) f5; + int64_t f0f6_2 = f0_2 * (int64_t) f6; + int64_t f0f7_2 = f0_2 * (int64_t) f7; + int64_t f0f8_2 = f0_2 * (int64_t) f8; + int64_t f0f9_2 = f0_2 * (int64_t) f9; + int64_t f1f1_2 = f1_2 * (int64_t) f1; + int64_t f1f2_2 = f1_2 * (int64_t) f2; + int64_t f1f3_4 = f1_2 * (int64_t) f3_2; + int64_t f1f4_2 = f1_2 * (int64_t) f4; + int64_t f1f5_4 = f1_2 * (int64_t) f5_2; + int64_t f1f6_2 = f1_2 * (int64_t) f6; + int64_t f1f7_4 = f1_2 * (int64_t) f7_2; + int64_t f1f8_2 = f1_2 * (int64_t) f8; + int64_t f1f9_76 = f1_2 * (int64_t) f9_38; + int64_t f2f2 = f2 * (int64_t) f2; + int64_t f2f3_2 = f2_2 * (int64_t) f3; + int64_t f2f4_2 = f2_2 * (int64_t) f4; + int64_t f2f5_2 = f2_2 * (int64_t) f5; + int64_t f2f6_2 = f2_2 * (int64_t) f6; + int64_t f2f7_2 = f2_2 * (int64_t) f7; + int64_t f2f8_38 = f2_2 * (int64_t) f8_19; + int64_t f2f9_38 = f2 * (int64_t) f9_38; + int64_t f3f3_2 = f3_2 * (int64_t) f3; + int64_t f3f4_2 = f3_2 * (int64_t) f4; + int64_t f3f5_4 = f3_2 * (int64_t) f5_2; + int64_t f3f6_2 = f3_2 * (int64_t) f6; + int64_t f3f7_76 = f3_2 * (int64_t) f7_38; + int64_t f3f8_38 = f3_2 * (int64_t) f8_19; + int64_t f3f9_76 = f3_2 * (int64_t) f9_38; + int64_t f4f4 = f4 * (int64_t) f4; + int64_t f4f5_2 = f4_2 * (int64_t) f5; + int64_t f4f6_38 = f4_2 * (int64_t) f6_19; + int64_t f4f7_38 = f4 * (int64_t) f7_38; + int64_t f4f8_38 = f4_2 * (int64_t) f8_19; + int64_t f4f9_38 = f4 * (int64_t) f9_38; + int64_t f5f5_38 = f5 * (int64_t) f5_38; + int64_t f5f6_38 = f5_2 * (int64_t) f6_19; + int64_t f5f7_76 = f5_2 * (int64_t) f7_38; + int64_t f5f8_38 = f5_2 * (int64_t) f8_19; + int64_t f5f9_76 = f5_2 * (int64_t) f9_38; + int64_t f6f6_19 = f6 * (int64_t) f6_19; + int64_t f6f7_38 = f6 * (int64_t) f7_38; + int64_t f6f8_38 = f6_2 * (int64_t) f8_19; + int64_t f6f9_38 = f6 * (int64_t) f9_38; + int64_t f7f7_38 = f7 * (int64_t) f7_38; + int64_t f7f8_38 = f7_2 * (int64_t) f8_19; + int64_t f7f9_76 = f7_2 * (int64_t) f9_38; + int64_t f8f8_19 = f8 * (int64_t) f8_19; + int64_t f8f9_38 = f8 * (int64_t) f9_38; + int64_t f9f9_38 = f9 * (int64_t) f9_38; + int64_t h0 = f0f0 + f1f9_76 + f2f8_38 + f3f7_76 + f4f6_38 + f5f5_38; + int64_t h1 = f0f1_2 + f2f9_38 + f3f8_38 + f4f7_38 + f5f6_38; + int64_t h2 = f0f2_2 + f1f1_2 + f3f9_76 + f4f8_38 + f5f7_76 + f6f6_19; + int64_t h3 = f0f3_2 + f1f2_2 + f4f9_38 + f5f8_38 + f6f7_38; + int64_t h4 = f0f4_2 + f1f3_4 + f2f2 + f5f9_76 + f6f8_38 + f7f7_38; + int64_t h5 = f0f5_2 + f1f4_2 + f2f3_2 + f6f9_38 + f7f8_38; + int64_t h6 = f0f6_2 + f1f5_4 + f2f4_2 + f3f3_2 + f7f9_76 + f8f8_19; + int64_t h7 = f0f7_2 + f1f6_2 + f2f5_2 + f3f4_2 + f8f9_38; + int64_t h8 = f0f8_2 + f1f7_4 + f2f6_2 + f3f5_4 + f4f4 + f9f9_38; + int64_t h9 = f0f9_2 + f1f8_2 + f2f7_2 + f3f6_2 + f4f5_2; + int64_t carry0; + int64_t carry1; + int64_t carry2; + int64_t carry3; + int64_t carry4; + int64_t carry5; + int64_t carry6; + int64_t carry7; + int64_t carry8; + int64_t carry9; + h0 += h0; + h1 += h1; + h2 += h2; + h3 += h3; + h4 += h4; + h5 += h5; + h6 += h6; + h7 += h7; + h8 += h8; + h9 += h9; + carry0 = (h0 + (int64_t) (1 << 25)) >> 26; + h1 += carry0; + h0 -= carry0 << 26; + carry4 = (h4 + (int64_t) (1 << 25)) >> 26; + h5 += carry4; + h4 -= carry4 << 26; + carry1 = (h1 + (int64_t) (1 << 24)) >> 25; + h2 += carry1; + h1 -= carry1 << 25; + carry5 = (h5 + (int64_t) (1 << 24)) >> 25; + h6 += carry5; + h5 -= carry5 << 25; + carry2 = (h2 + (int64_t) (1 << 25)) >> 26; + h3 += carry2; + h2 -= carry2 << 26; + carry6 = (h6 + (int64_t) (1 << 25)) >> 26; + h7 += carry6; + h6 -= carry6 << 26; + carry3 = (h3 + (int64_t) (1 << 24)) >> 25; + h4 += carry3; + h3 -= carry3 << 25; + carry7 = (h7 + (int64_t) (1 << 24)) >> 25; + h8 += carry7; + h7 -= carry7 << 25; + carry4 = (h4 + (int64_t) (1 << 25)) >> 26; + h5 += carry4; + h4 -= carry4 << 26; + carry8 = (h8 + (int64_t) (1 << 25)) >> 26; + h9 += carry8; + h8 -= carry8 << 26; + carry9 = (h9 + (int64_t) (1 << 24)) >> 25; + h0 += carry9 * 19; + h9 -= carry9 << 25; + carry0 = (h0 + (int64_t) (1 << 25)) >> 26; + h1 += carry0; + h0 -= carry0 << 26; + h[0] = (int32_t) h0; + h[1] = (int32_t) h1; + h[2] = (int32_t) h2; + h[3] = (int32_t) h3; + h[4] = (int32_t) h4; + h[5] = (int32_t) h5; + h[6] = (int32_t) h6; + h[7] = (int32_t) h7; + h[8] = (int32_t) h8; + h[9] = (int32_t) h9; +} + + +/* +h = f - g +Can overlap h with f or g. + +Preconditions: + |f| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc. + |g| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc. + +Postconditions: + |h| bounded by 1.1*2^26,1.1*2^25,1.1*2^26,1.1*2^25,etc. +*/ + +void fe_sub(fe h, const fe f, const fe g) { + int32_t f0 = f[0]; + int32_t f1 = f[1]; + int32_t f2 = f[2]; + int32_t f3 = f[3]; + int32_t f4 = f[4]; + int32_t f5 = f[5]; + int32_t f6 = f[6]; + int32_t f7 = f[7]; + int32_t f8 = f[8]; + int32_t f9 = f[9]; + int32_t g0 = g[0]; + int32_t g1 = g[1]; + int32_t g2 = g[2]; + int32_t g3 = g[3]; + int32_t g4 = g[4]; + int32_t g5 = g[5]; + int32_t g6 = g[6]; + int32_t g7 = g[7]; + int32_t g8 = g[8]; + int32_t g9 = g[9]; + int32_t h0 = f0 - g0; + int32_t h1 = f1 - g1; + int32_t h2 = f2 - g2; + int32_t h3 = f3 - g3; + int32_t h4 = f4 - g4; + int32_t h5 = f5 - g5; + int32_t h6 = f6 - g6; + int32_t h7 = f7 - g7; + int32_t h8 = f8 - g8; + int32_t h9 = f9 - g9; + + h[0] = h0; + h[1] = h1; + h[2] = h2; + h[3] = h3; + h[4] = h4; + h[5] = h5; + h[6] = h6; + h[7] = h7; + h[8] = h8; + h[9] = h9; +} + + + +/* +Preconditions: + |h| bounded by 1.1*2^26,1.1*2^25,1.1*2^26,1.1*2^25,etc. + +Write p=2^255-19; q=floor(h/p). +Basic claim: q = floor(2^(-255)(h + 19 2^(-25)h9 + 2^(-1))). + +Proof: + Have |h|<=p so |q|<=1 so |19^2 2^(-255) q|<1/4. + Also have |h-2^230 h9|<2^231 so |19 2^(-255)(h-2^230 h9)|<1/4. + + Write y=2^(-1)-19^2 2^(-255)q-19 2^(-255)(h-2^230 h9). + Then 0> 25; + q = (h0 + q) >> 26; + q = (h1 + q) >> 25; + q = (h2 + q) >> 26; + q = (h3 + q) >> 25; + q = (h4 + q) >> 26; + q = (h5 + q) >> 25; + q = (h6 + q) >> 26; + q = (h7 + q) >> 25; + q = (h8 + q) >> 26; + q = (h9 + q) >> 25; + /* Goal: Output h-(2^255-19)q, which is between 0 and 2^255-20. */ + h0 += 19 * q; + /* Goal: Output h-2^255 q, which is between 0 and 2^255-20. */ + carry0 = h0 >> 26; + h1 += carry0; + h0 -= carry0 << 26; + carry1 = h1 >> 25; + h2 += carry1; + h1 -= carry1 << 25; + carry2 = h2 >> 26; + h3 += carry2; + h2 -= carry2 << 26; + carry3 = h3 >> 25; + h4 += carry3; + h3 -= carry3 << 25; + carry4 = h4 >> 26; + h5 += carry4; + h4 -= carry4 << 26; + carry5 = h5 >> 25; + h6 += carry5; + h5 -= carry5 << 25; + carry6 = h6 >> 26; + h7 += carry6; + h6 -= carry6 << 26; + carry7 = h7 >> 25; + h8 += carry7; + h7 -= carry7 << 25; + carry8 = h8 >> 26; + h9 += carry8; + h8 -= carry8 << 26; + carry9 = h9 >> 25; + h9 -= carry9 << 25; + + /* h10 = carry9 */ + /* + Goal: Output h0+...+2^255 h10-2^255 q, which is between 0 and 2^255-20. + Have h0+...+2^230 h9 between 0 and 2^255-1; + evidently 2^255 h10-2^255 q = 0. + Goal: Output h0+...+2^230 h9. + */ + s[0] = (unsigned char) (h0 >> 0); + s[1] = (unsigned char) (h0 >> 8); + s[2] = (unsigned char) (h0 >> 16); + s[3] = (unsigned char) ((h0 >> 24) | (h1 << 2)); + s[4] = (unsigned char) (h1 >> 6); + s[5] = (unsigned char) (h1 >> 14); + s[6] = (unsigned char) ((h1 >> 22) | (h2 << 3)); + s[7] = (unsigned char) (h2 >> 5); + s[8] = (unsigned char) (h2 >> 13); + s[9] = (unsigned char) ((h2 >> 21) | (h3 << 5)); + s[10] = (unsigned char) (h3 >> 3); + s[11] = (unsigned char) (h3 >> 11); + s[12] = (unsigned char) ((h3 >> 19) | (h4 << 6)); + s[13] = (unsigned char) (h4 >> 2); + s[14] = (unsigned char) (h4 >> 10); + s[15] = (unsigned char) (h4 >> 18); + s[16] = (unsigned char) (h5 >> 0); + s[17] = (unsigned char) (h5 >> 8); + s[18] = (unsigned char) (h5 >> 16); + s[19] = (unsigned char) ((h5 >> 24) | (h6 << 1)); + s[20] = (unsigned char) (h6 >> 7); + s[21] = (unsigned char) (h6 >> 15); + s[22] = (unsigned char) ((h6 >> 23) | (h7 << 3)); + s[23] = (unsigned char) (h7 >> 5); + s[24] = (unsigned char) (h7 >> 13); + s[25] = (unsigned char) ((h7 >> 21) | (h8 << 4)); + s[26] = (unsigned char) (h8 >> 4); + s[27] = (unsigned char) (h8 >> 12); + s[28] = (unsigned char) ((h8 >> 20) | (h9 << 6)); + s[29] = (unsigned char) (h9 >> 2); + s[30] = (unsigned char) (h9 >> 10); + s[31] = (unsigned char) (h9 >> 18); +} diff --git a/core/eos/services/crypto/src/ed25519_ge.c b/core/eos/services/crypto/src/ed25519_ge.c new file mode 100644 index 00000000..87c691bf --- /dev/null +++ b/core/eos/services/crypto/src/ed25519_ge.c @@ -0,0 +1,467 @@ +#include "ge.h" +#include "precomp_data.h" + + +/* +r = p + q +*/ + +void ge_add(ge_p1p1 *r, const ge_p3 *p, const ge_cached *q) { + fe t0; + fe_add(r->X, p->Y, p->X); + fe_sub(r->Y, p->Y, p->X); + fe_mul(r->Z, r->X, q->YplusX); + fe_mul(r->Y, r->Y, q->YminusX); + fe_mul(r->T, q->T2d, p->T); + fe_mul(r->X, p->Z, q->Z); + fe_add(t0, r->X, r->X); + fe_sub(r->X, r->Z, r->Y); + fe_add(r->Y, r->Z, r->Y); + fe_add(r->Z, t0, r->T); + fe_sub(r->T, t0, r->T); +} + + +static void slide(signed char *r, const unsigned char *a) { + int i; + int b; + int k; + + for (i = 0; i < 256; ++i) { + r[i] = 1 & (a[i >> 3] >> (i & 7)); + } + + for (i = 0; i < 256; ++i) + if (r[i]) { + for (b = 1; b <= 6 && i + b < 256; ++b) { + if (r[i + b]) { + if (r[i] + (r[i + b] << b) <= 15) { + r[i] += r[i + b] << b; + r[i + b] = 0; + } else if (r[i] - (r[i + b] << b) >= -15) { + r[i] -= r[i + b] << b; + + for (k = i + b; k < 256; ++k) { + if (!r[k]) { + r[k] = 1; + break; + } + + r[k] = 0; + } + } else { + break; + } + } + } + } +} + +/* +r = a * A + b * B +where a = a[0]+256*a[1]+...+256^31 a[31]. +and b = b[0]+256*b[1]+...+256^31 b[31]. +B is the Ed25519 base point (x,4/5) with x positive. +*/ + +void ge_double_scalarmult_vartime(ge_p2 *r, const unsigned char *a, const ge_p3 *A, const unsigned char *b) { + signed char aslide[256]; + signed char bslide[256]; + ge_cached Ai[8]; /* A,3A,5A,7A,9A,11A,13A,15A */ + ge_p1p1 t; + ge_p3 u; + ge_p3 A2; + int i; + slide(aslide, a); + slide(bslide, b); + ge_p3_to_cached(&Ai[0], A); + ge_p3_dbl(&t, A); + ge_p1p1_to_p3(&A2, &t); + ge_add(&t, &A2, &Ai[0]); + ge_p1p1_to_p3(&u, &t); + ge_p3_to_cached(&Ai[1], &u); + ge_add(&t, &A2, &Ai[1]); + ge_p1p1_to_p3(&u, &t); + ge_p3_to_cached(&Ai[2], &u); + ge_add(&t, &A2, &Ai[2]); + ge_p1p1_to_p3(&u, &t); + ge_p3_to_cached(&Ai[3], &u); + ge_add(&t, &A2, &Ai[3]); + ge_p1p1_to_p3(&u, &t); + ge_p3_to_cached(&Ai[4], &u); + ge_add(&t, &A2, &Ai[4]); + ge_p1p1_to_p3(&u, &t); + ge_p3_to_cached(&Ai[5], &u); + ge_add(&t, &A2, &Ai[5]); + ge_p1p1_to_p3(&u, &t); + ge_p3_to_cached(&Ai[6], &u); + ge_add(&t, &A2, &Ai[6]); + ge_p1p1_to_p3(&u, &t); + ge_p3_to_cached(&Ai[7], &u); + ge_p2_0(r); + + for (i = 255; i >= 0; --i) { + if (aslide[i] || bslide[i]) { + break; + } + } + + for (; i >= 0; --i) { + ge_p2_dbl(&t, r); + + if (aslide[i] > 0) { + ge_p1p1_to_p3(&u, &t); + ge_add(&t, &u, &Ai[aslide[i] / 2]); + } else if (aslide[i] < 0) { + ge_p1p1_to_p3(&u, &t); + ge_sub(&t, &u, &Ai[(-aslide[i]) / 2]); + } + + if (bslide[i] > 0) { + ge_p1p1_to_p3(&u, &t); + ge_madd(&t, &u, &Bi[bslide[i] / 2]); + } else if (bslide[i] < 0) { + ge_p1p1_to_p3(&u, &t); + ge_msub(&t, &u, &Bi[(-bslide[i]) / 2]); + } + + ge_p1p1_to_p2(r, &t); + } +} + + +static const fe d = { + -10913610, 13857413, -15372611, 6949391, 114729, -8787816, -6275908, -3247719, -18696448, -12055116 +}; + +static const fe sqrtm1 = { + -32595792, -7943725, 9377950, 3500415, 12389472, -272473, -25146209, -2005654, 326686, 11406482 +}; + +int ge_frombytes_negate_vartime(ge_p3 *h, const unsigned char *s) { + fe u; + fe v; + fe v3; + fe vxx; + fe check; + fe_frombytes(h->Y, s); + fe_1(h->Z); + fe_sq(u, h->Y); + fe_mul(v, u, d); + fe_sub(u, u, h->Z); /* u = y^2-1 */ + fe_add(v, v, h->Z); /* v = dy^2+1 */ + fe_sq(v3, v); + fe_mul(v3, v3, v); /* v3 = v^3 */ + fe_sq(h->X, v3); + fe_mul(h->X, h->X, v); + fe_mul(h->X, h->X, u); /* x = uv^7 */ + fe_pow22523(h->X, h->X); /* x = (uv^7)^((q-5)/8) */ + fe_mul(h->X, h->X, v3); + fe_mul(h->X, h->X, u); /* x = uv^3(uv^7)^((q-5)/8) */ + fe_sq(vxx, h->X); + fe_mul(vxx, vxx, v); + fe_sub(check, vxx, u); /* vx^2-u */ + + if (fe_isnonzero(check)) { + fe_add(check, vxx, u); /* vx^2+u */ + + if (fe_isnonzero(check)) { + return -1; + } + + fe_mul(h->X, h->X, sqrtm1); + } + + if (fe_isnegative(h->X) == (s[31] >> 7)) { + fe_neg(h->X, h->X); + } + + fe_mul(h->T, h->X, h->Y); + return 0; +} + + +/* +r = p + q +*/ + +void ge_madd(ge_p1p1 *r, const ge_p3 *p, const ge_precomp *q) { + fe t0; + fe_add(r->X, p->Y, p->X); + fe_sub(r->Y, p->Y, p->X); + fe_mul(r->Z, r->X, q->yplusx); + fe_mul(r->Y, r->Y, q->yminusx); + fe_mul(r->T, q->xy2d, p->T); + fe_add(t0, p->Z, p->Z); + fe_sub(r->X, r->Z, r->Y); + fe_add(r->Y, r->Z, r->Y); + fe_add(r->Z, t0, r->T); + fe_sub(r->T, t0, r->T); +} + + +/* +r = p - q +*/ + +void ge_msub(ge_p1p1 *r, const ge_p3 *p, const ge_precomp *q) { + fe t0; + + fe_add(r->X, p->Y, p->X); + fe_sub(r->Y, p->Y, p->X); + fe_mul(r->Z, r->X, q->yminusx); + fe_mul(r->Y, r->Y, q->yplusx); + fe_mul(r->T, q->xy2d, p->T); + fe_add(t0, p->Z, p->Z); + fe_sub(r->X, r->Z, r->Y); + fe_add(r->Y, r->Z, r->Y); + fe_sub(r->Z, t0, r->T); + fe_add(r->T, t0, r->T); +} + + +/* +r = p +*/ + +void ge_p1p1_to_p2(ge_p2 *r, const ge_p1p1 *p) { + fe_mul(r->X, p->X, p->T); + fe_mul(r->Y, p->Y, p->Z); + fe_mul(r->Z, p->Z, p->T); +} + + + +/* +r = p +*/ + +void ge_p1p1_to_p3(ge_p3 *r, const ge_p1p1 *p) { + fe_mul(r->X, p->X, p->T); + fe_mul(r->Y, p->Y, p->Z); + fe_mul(r->Z, p->Z, p->T); + fe_mul(r->T, p->X, p->Y); +} + + +void ge_p2_0(ge_p2 *h) { + fe_0(h->X); + fe_1(h->Y); + fe_1(h->Z); +} + + + +/* +r = 2 * p +*/ + +void ge_p2_dbl(ge_p1p1 *r, const ge_p2 *p) { + fe t0; + + fe_sq(r->X, p->X); + fe_sq(r->Z, p->Y); + fe_sq2(r->T, p->Z); + fe_add(r->Y, p->X, p->Y); + fe_sq(t0, r->Y); + fe_add(r->Y, r->Z, r->X); + fe_sub(r->Z, r->Z, r->X); + fe_sub(r->X, t0, r->Y); + fe_sub(r->T, r->T, r->Z); +} + + +void ge_p3_0(ge_p3 *h) { + fe_0(h->X); + fe_1(h->Y); + fe_1(h->Z); + fe_0(h->T); +} + + +/* +r = 2 * p +*/ + +void ge_p3_dbl(ge_p1p1 *r, const ge_p3 *p) { + ge_p2 q; + ge_p3_to_p2(&q, p); + ge_p2_dbl(r, &q); +} + + + +/* +r = p +*/ + +static const fe d2 = { + -21827239, -5839606, -30745221, 13898782, 229458, 15978800, -12551817, -6495438, 29715968, 9444199 +}; + +void ge_p3_to_cached(ge_cached *r, const ge_p3 *p) { + fe_add(r->YplusX, p->Y, p->X); + fe_sub(r->YminusX, p->Y, p->X); + fe_copy(r->Z, p->Z); + fe_mul(r->T2d, p->T, d2); +} + + +/* +r = p +*/ + +void ge_p3_to_p2(ge_p2 *r, const ge_p3 *p) { + fe_copy(r->X, p->X); + fe_copy(r->Y, p->Y); + fe_copy(r->Z, p->Z); +} + + +void ge_p3_tobytes(unsigned char *s, const ge_p3 *h) { + fe recip; + fe x; + fe y; + fe_invert(recip, h->Z); + fe_mul(x, h->X, recip); + fe_mul(y, h->Y, recip); + fe_tobytes(s, y); + s[31] ^= fe_isnegative(x) << 7; +} + + +static unsigned char equal(signed char b, signed char c) { + unsigned char ub = b; + unsigned char uc = c; + unsigned char x = ub ^ uc; /* 0: yes; 1..255: no */ + uint64_t y = x; /* 0: yes; 1..255: no */ + y -= 1; /* large: yes; 0..254: no */ + y >>= 63; /* 1: yes; 0: no */ + return (unsigned char) y; +} + +static unsigned char negative(signed char b) { + uint64_t x = b; /* 18446744073709551361..18446744073709551615: yes; 0..255: no */ + x >>= 63; /* 1: yes; 0: no */ + return (unsigned char) x; +} + +static void cmov(ge_precomp *t, const ge_precomp *u, unsigned char b) { + fe_cmov(t->yplusx, u->yplusx, b); + fe_cmov(t->yminusx, u->yminusx, b); + fe_cmov(t->xy2d, u->xy2d, b); +} + + +static void select(ge_precomp *t, int pos, signed char b) { + ge_precomp minust; + unsigned char bnegative = negative(b); + unsigned char babs = b - (((-bnegative) & b) << 1); + fe_1(t->yplusx); + fe_1(t->yminusx); + fe_0(t->xy2d); + cmov(t, &base[pos][0], equal(babs, 1)); + cmov(t, &base[pos][1], equal(babs, 2)); + cmov(t, &base[pos][2], equal(babs, 3)); + cmov(t, &base[pos][3], equal(babs, 4)); + cmov(t, &base[pos][4], equal(babs, 5)); + cmov(t, &base[pos][5], equal(babs, 6)); + cmov(t, &base[pos][6], equal(babs, 7)); + cmov(t, &base[pos][7], equal(babs, 8)); + fe_copy(minust.yplusx, t->yminusx); + fe_copy(minust.yminusx, t->yplusx); + fe_neg(minust.xy2d, t->xy2d); + cmov(t, &minust, bnegative); +} + +/* +h = a * B +where a = a[0]+256*a[1]+...+256^31 a[31] +B is the Ed25519 base point (x,4/5) with x positive. + +Preconditions: + a[31] <= 127 +*/ + +void ge_scalarmult_base(ge_p3 *h, const unsigned char *a) { + signed char e[64]; + signed char carry; + ge_p1p1 r; + ge_p2 s; + ge_precomp t; + int i; + + for (i = 0; i < 32; ++i) { + e[2 * i + 0] = (a[i] >> 0) & 15; + e[2 * i + 1] = (a[i] >> 4) & 15; + } + + /* each e[i] is between 0 and 15 */ + /* e[63] is between 0 and 7 */ + carry = 0; + + for (i = 0; i < 63; ++i) { + e[i] += carry; + carry = e[i] + 8; + carry >>= 4; + e[i] -= carry << 4; + } + + e[63] += carry; + /* each e[i] is between -8 and 8 */ + ge_p3_0(h); + + for (i = 1; i < 64; i += 2) { + select(&t, i / 2, e[i]); + ge_madd(&r, h, &t); + ge_p1p1_to_p3(h, &r); + } + + ge_p3_dbl(&r, h); + ge_p1p1_to_p2(&s, &r); + ge_p2_dbl(&r, &s); + ge_p1p1_to_p2(&s, &r); + ge_p2_dbl(&r, &s); + ge_p1p1_to_p2(&s, &r); + ge_p2_dbl(&r, &s); + ge_p1p1_to_p3(h, &r); + + for (i = 0; i < 64; i += 2) { + select(&t, i / 2, e[i]); + ge_madd(&r, h, &t); + ge_p1p1_to_p3(h, &r); + } +} + + +/* +r = p - q +*/ + +void ge_sub(ge_p1p1 *r, const ge_p3 *p, const ge_cached *q) { + fe t0; + + fe_add(r->X, p->Y, p->X); + fe_sub(r->Y, p->Y, p->X); + fe_mul(r->Z, r->X, q->YminusX); + fe_mul(r->Y, r->Y, q->YplusX); + fe_mul(r->T, q->T2d, p->T); + fe_mul(r->X, p->Z, q->Z); + fe_add(t0, r->X, r->X); + fe_sub(r->X, r->Z, r->Y); + fe_add(r->Y, r->Z, r->Y); + fe_sub(r->Z, t0, r->T); + fe_add(r->T, t0, r->T); +} + + +void ge_tobytes(unsigned char *s, const ge_p2 *h) { + fe recip; + fe x; + fe y; + fe_invert(recip, h->Z); + fe_mul(x, h->X, recip); + fe_mul(y, h->Y, recip); + fe_tobytes(s, y); + s[31] ^= fe_isnegative(x) << 7; +} diff --git a/core/eos/services/crypto/src/ed25519_sc.c b/core/eos/services/crypto/src/ed25519_sc.c new file mode 100644 index 00000000..da2c357e --- /dev/null +++ b/core/eos/services/crypto/src/ed25519_sc.c @@ -0,0 +1,833 @@ +#include "fixedint.h" +#include "sc.h" + +static uint64_t load_3(const unsigned char *in) { + uint64_t result; + + result = (uint64_t) in[0]; + result |= ((uint64_t) in[1]) << 8; + result |= ((uint64_t) in[2]) << 16; + + return result; +} + +static uint64_t load_4(const unsigned char *in) { + uint64_t result; + + result = (uint64_t) in[0]; + result |= ((uint64_t) in[1]) << 8; + result |= ((uint64_t) in[2]) << 16; + result |= ((uint64_t) in[3]) << 24; + + return result; +} + +/* +Input: + s[0]+256*s[1]+...+256^63*s[63] = s + +Output: + s[0]+256*s[1]+...+256^31*s[31] = s mod l + where l = 2^252 + 27742317777372353535851937790883648493. + Overwrites s in place. +*/ + +void sc_reduce(unsigned char *s) { + // The scalar is represented as 21-bit limbs. + // We first extract the limbs from the 256-bit input. + // Then we perform carry propagation to normalize them. + // Next, we reduce the scalar modulo the curve order l. + // The reduction involves multiplying by a precomputed constant. + // After reduction, we do a final carry propagation. + // The normalized limbs are then packed back into a 256-bit array. + + int64_t s0 = 2097151 & load_3(s); + int64_t s1 = 2097151 & (load_4(s + 2) >> 5); + int64_t s2 = 2097151 & (load_3(s + 5) >> 2); + int64_t s3 = 2097151 & (load_4(s + 7) >> 7); + int64_t s4 = 2097151 & (load_4(s + 10) >> 4); + int64_t s5 = 2097151 & (load_3(s + 13) >> 1); + int64_t s6 = 2097151 & (load_4(s + 15) >> 6); + int64_t s7 = 2097151 & (load_3(s + 18) >> 3); + int64_t s8 = 2097151 & load_3(s + 21); + int64_t s9 = 2097151 & (load_4(s + 23) >> 5); + int64_t s10 = 2097151 & (load_3(s + 26) >> 2); + int64_t s11 = 2097151 & (load_4(s + 28) >> 7); + int64_t s12 = 2097151 & (load_4(s + 31) >> 4); + int64_t s13 = 2097151 & (load_3(s + 34) >> 1); + int64_t s14 = 2097151 & (load_4(s + 36) >> 6); + int64_t s15 = 2097151 & (load_3(s + 39) >> 3); + int64_t s16 = 2097151 & load_3(s + 42); + int64_t s17 = 2097151 & (load_4(s + 44) >> 5); + int64_t s18 = 2097151 & (load_3(s + 47) >> 2); + int64_t s19 = 2097151 & (load_4(s + 49) >> 7); + int64_t s20 = 2097151 & (load_4(s + 52) >> 4); + int64_t s21 = 2097151 & (load_3(s + 55) >> 1); + int64_t s22 = 2097151 & (load_4(s + 57) >> 6); + int64_t s23 = (load_4(s + 60) >> 3); + int64_t carry0; + int64_t carry1; + int64_t carry2; + int64_t carry3; + int64_t carry4; + int64_t carry5; + int64_t carry6; + int64_t carry7; + int64_t carry8; + int64_t carry9; + int64_t carry10; + int64_t carry11; + int64_t carry12; + int64_t carry13; + int64_t carry14; + int64_t carry15; + int64_t carry16; + + s11 += s23 * 666643; + s12 += s23 * 470296; + s13 += s23 * 654183; + s14 -= s23 * 997805; + s15 += s23 * 136657; + s16 -= s23 * 683901; + s23 = 0; + s10 += s22 * 666643; + s11 += s22 * 470296; + s12 += s22 * 654183; + s13 -= s22 * 997805; + s14 += s22 * 136657; + s15 -= s22 * 683901; + s22 = 0; + s9 += s21 * 666643; + s10 += s21 * 470296; + s11 += s21 * 654183; + s12 -= s21 * 997805; + s13 += s21 * 136657; + s14 -= s21 * 683901; + s21 = 0; + s8 += s20 * 666643; + s9 += s20 * 470296; + s10 += s20 * 654183; + s11 -= s20 * 997805; + s12 += s20 * 136657; + s13 -= s20 * 683901; + s20 = 0; + s7 += s19 * 666643; + s8 += s19 * 470296; + s9 += s19 * 654183; + s10 -= s19 * 997805; + s11 += s19 * 136657; + s12 -= s19 * 683901; + s19 = 0; + s6 += s18 * 666643; + s7 += s18 * 470296; + s8 += s18 * 654183; + s9 -= s18 * 997805; + s10 += s18 * 136657; + s11 -= s18 * 683901; + s18 = 0; + carry6 = (s6 + (1 << 20)) >> 21; + s7 += carry6; + s6 -= carry6 << 21; + carry8 = (s8 + (1 << 20)) >> 21; + s9 += carry8; + s8 -= carry8 << 21; + carry10 = (s10 + (1 << 20)) >> 21; + s11 += carry10; + s10 -= carry10 << 21; + carry12 = (s12 + (1 << 20)) >> 21; + s13 += carry12; + s12 -= carry12 << 21; + carry14 = (s14 + (1 << 20)) >> 21; + s15 += carry14; + s14 -= carry14 << 21; + carry16 = (s16 + (1 << 20)) >> 21; + s17 += carry16; + s16 -= carry16 << 21; + carry7 = (s7 + (1 << 20)) >> 21; + s8 += carry7; + s7 -= carry7 << 21; + carry9 = (s9 + (1 << 20)) >> 21; + s10 += carry9; + s9 -= carry9 << 21; + carry11 = (s11 + (1 << 20)) >> 21; + s12 += carry11; + s11 -= carry11 << 21; + carry13 = (s13 + (1 << 20)) >> 21; + s14 += carry13; + s13 -= carry13 << 21; + carry15 = (s15 + (1 << 20)) >> 21; + s16 += carry15; + s15 -= carry15 << 21; + s5 += s17 * 666643; + s6 += s17 * 470296; + s7 += s17 * 654183; + s8 -= s17 * 997805; + s9 += s17 * 136657; + s10 -= s17 * 683901; + s17 = 0; + s4 += s16 * 666643; + s5 += s16 * 470296; + s6 += s16 * 654183; + s7 -= s16 * 997805; + s8 += s16 * 136657; + s9 -= s16 * 683901; + s16 = 0; + s3 += s15 * 666643; + s4 += s15 * 470296; + s5 += s15 * 654183; + s6 -= s15 * 997805; + s7 += s15 * 136657; + s8 -= s15 * 683901; + s15 = 0; + s2 += s14 * 666643; + s3 += s14 * 470296; + s4 += s14 * 654183; + s5 -= s14 * 997805; + s6 += s14 * 136657; + s7 -= s14 * 683901; + s14 = 0; + s1 += s13 * 666643; + s2 += s13 * 470296; + s3 += s13 * 654183; + s4 -= s13 * 997805; + s5 += s13 * 136657; + s6 -= s13 * 683901; + s13 = 0; + s0 += s12 * 666643; + s1 += s12 * 470296; + s2 += s12 * 654183; + s3 -= s12 * 997805; + s4 += s12 * 136657; + s5 -= s12 * 683901; + s12 = 0; + carry0 = (s0 + (1 << 20)) >> 21; + s1 += carry0; + s0 -= carry0 << 21; + carry2 = (s2 + (1 << 20)) >> 21; + s3 += carry2; + s2 -= carry2 << 21; + carry4 = (s4 + (1 << 20)) >> 21; + s5 += carry4; + s4 -= carry4 << 21; + carry6 = (s6 + (1 << 20)) >> 21; + s7 += carry6; + s6 -= carry6 << 21; + carry8 = (s8 + (1 << 20)) >> 21; + s9 += carry8; + s8 -= carry8 << 21; + carry10 = (s10 + (1 << 20)) >> 21; + s11 += carry10; + s10 -= carry10 << 21; + carry1 = (s1 + (1 << 20)) >> 21; + s2 += carry1; + s1 -= carry1 << 21; + carry3 = (s3 + (1 << 20)) >> 21; + s4 += carry3; + s3 -= carry3 << 21; + carry5 = (s5 + (1 << 20)) >> 21; + s6 += carry5; + s5 -= carry5 << 21; + carry7 = (s7 + (1 << 20)) >> 21; + s8 += carry7; + s7 -= carry7 << 21; + carry9 = (s9 + (1 << 20)) >> 21; + s10 += carry9; + s9 -= carry9 << 21; + carry11 = (s11 + (1 << 20)) >> 21; + s12 += carry11; + s11 -= carry11 << 21; + s0 += s12 * 666643; + s1 += s12 * 470296; + s2 += s12 * 654183; + s3 -= s12 * 997805; + s4 += s12 * 136657; + s5 -= s12 * 683901; + s12 = 0; + carry0 = s0 >> 21; + s1 += carry0; + s0 -= carry0 << 21; + carry1 = s1 >> 21; + s2 += carry1; + s1 -= carry1 << 21; + carry2 = s2 >> 21; + s3 += carry2; + s2 -= carry2 << 21; + carry3 = s3 >> 21; + s4 += carry3; + s3 -= carry3 << 21; + carry4 = s4 >> 21; + s5 += carry4; + s4 -= carry4 << 21; + carry5 = s5 >> 21; + s6 += carry5; + s5 -= carry5 << 21; + carry6 = s6 >> 21; + s7 += carry6; + s6 -= carry6 << 21; + carry7 = s7 >> 21; + s8 += carry7; + s7 -= carry7 << 21; + carry8 = s8 >> 21; + s9 += carry8; + s8 -= carry8 << 21; + carry9 = s9 >> 21; + s10 += carry9; + s9 -= carry9 << 21; + carry10 = s10 >> 21; + s11 += carry10; + s10 -= carry10 << 21; + carry11 = s11 >> 21; + s12 += carry11; + s11 -= carry11 << 21; + s0 += s12 * 666643; + s1 += s12 * 470296; + s2 += s12 * 654183; + s3 -= s12 * 997805; + s4 += s12 * 136657; + s5 -= s12 * 683901; + s12 = 0; + carry0 = s0 >> 21; + s1 += carry0; + s0 -= carry0 << 21; + carry1 = s1 >> 21; + s2 += carry1; + s1 -= carry1 << 21; + carry2 = s2 >> 21; + s3 += carry2; + s2 -= carry2 << 21; + carry3 = s3 >> 21; + s4 += carry3; + s3 -= carry3 << 21; + carry4 = s4 >> 21; + s5 += carry4; + s4 -= carry4 << 21; + carry5 = s5 >> 21; + s6 += carry5; + s5 -= carry5 << 21; + carry6 = s6 >> 21; + s7 += carry6; + s6 -= carry6 << 21; + carry7 = s7 >> 21; + s8 += carry7; + s7 -= carry7 << 21; + carry8 = s8 >> 21; + s9 += carry8; + s8 -= carry8 << 21; + carry9 = s9 >> 21; + s10 += carry9; + s9 -= carry9 << 21; + carry10 = s10 >> 21; + s11 += carry10; + s10 -= carry10 << 21; + + s[0] = (unsigned char) (s0 >> 0); + s[1] = (unsigned char) (s0 >> 8); + s[2] = (unsigned char) ((s0 >> 16) | (s1 << 5)); + s[3] = (unsigned char) (s1 >> 3); + s[4] = (unsigned char) (s1 >> 11); + s[5] = (unsigned char) ((s1 >> 19) | (s2 << 2)); + s[6] = (unsigned char) (s2 >> 6); + s[7] = (unsigned char) ((s2 >> 14) | (s3 << 7)); + s[8] = (unsigned char) (s3 >> 1); + s[9] = (unsigned char) (s3 >> 9); + s[10] = (unsigned char) ((s3 >> 17) | (s4 << 4)); + s[11] = (unsigned char) (s4 >> 4); + s[12] = (unsigned char) (s4 >> 12); + s[13] = (unsigned char) ((s4 >> 20) | (s5 << 1)); + s[14] = (unsigned char) (s5 >> 7); + s[15] = (unsigned char) ((s5 >> 15) | (s6 << 6)); + s[16] = (unsigned char) (s6 >> 2); + s[17] = (unsigned char) (s6 >> 10); + s[18] = (unsigned char) ((s6 >> 18) | (s7 << 3)); + s[19] = (unsigned char) (s7 >> 5); + s[20] = (unsigned char) (s7 >> 13); + s[21] = (unsigned char) (s8 >> 0); + s[22] = (unsigned char) (s8 >> 8); + s[23] = (unsigned char) ((s8 >> 16) | (s9 << 5)); + s[24] = (unsigned char) (s9 >> 3); + s[25] = (unsigned char) (s9 >> 11); + s[26] = (unsigned char) ((s9 >> 19) | (s10 << 2)); + s[27] = (unsigned char) (s10 >> 6); + s[28] = (unsigned char) ((s10 >> 14) | (s11 << 7)); + s[29] = (unsigned char) (s11 >> 1); + s[30] = (unsigned char) (s11 >> 9); + s[31] = (unsigned char) (s11 >> 17); +} + + + +/* +Input: + a[0]+256*a[1]+...+256^31*a[31] = a + b[0]+256*b[1]+...+256^31*b[31] = b + c[0]+256*c[1]+...+256^31*c[31] = c + +Output: + s[0]+256*s[1]+...+256^31*s[31] = (ab+c) mod l + where l = 2^252 + 27742317777372353535851937790883648493. +*/ + +void sc_muladd(unsigned char *s, const unsigned char *a, const unsigned char *b, const unsigned char *c) { + // a0 through a11 store the 21-bit limbs of the scalar a. + // These limbs are extracted using bitwise operations and masks. + // load_3 and load_4 read 24-bit and 32-bit values respectively. + // We mask with 2097151 (0x1FFFFF) to keep exactly 21 bits per limb. + // This allows for deferred carry propagation during multiplication. + // The same extraction process is applied to scalars b and c. + // b0 through b11 store the 21-bit limbs of the scalar b. + // c0 through c11 store the 21-bit limbs of the scalar c. + // After extraction, we perform the multiplication a * b. + // The products are accumulated into s0 through s22. + // s0 to s22 represent the limbs of the product before reduction. + // The accumulation uses 64-bit integers to prevent overflow. + // We then add the limbs of c to the lower half of s. + // Finally, we perform modular reduction by the curve order l. + // We do this by interleaving carry propagation and multiplication. + + int64_t a0 = 2097151 & load_3(a); + int64_t a1 = 2097151 & (load_4(a + 2) >> 5); + int64_t a2 = 2097151 & (load_3(a + 5) >> 2); + int64_t a3 = 2097151 & (load_4(a + 7) >> 7); + int64_t a4 = 2097151 & (load_4(a + 10) >> 4); + int64_t a5 = 2097151 & (load_3(a + 13) >> 1); + int64_t a6 = 2097151 & (load_4(a + 15) >> 6); + int64_t a7 = 2097151 & (load_3(a + 18) >> 3); + int64_t a8 = 2097151 & load_3(a + 21); + int64_t a9 = 2097151 & (load_4(a + 23) >> 5); + int64_t a10 = 2097151 & (load_3(a + 26) >> 2); + int64_t a11 = (load_4(a + 28) >> 7); + int64_t b0 = 2097151 & load_3(b); + int64_t b1 = 2097151 & (load_4(b + 2) >> 5); + int64_t b2 = 2097151 & (load_3(b + 5) >> 2); + int64_t b3 = 2097151 & (load_4(b + 7) >> 7); + int64_t b4 = 2097151 & (load_4(b + 10) >> 4); + int64_t b5 = 2097151 & (load_3(b + 13) >> 1); + int64_t b6 = 2097151 & (load_4(b + 15) >> 6); + int64_t b7 = 2097151 & (load_3(b + 18) >> 3); + int64_t b8 = 2097151 & load_3(b + 21); + int64_t b9 = 2097151 & (load_4(b + 23) >> 5); + int64_t b10 = 2097151 & (load_3(b + 26) >> 2); + int64_t b11 = (load_4(b + 28) >> 7); + int64_t c0 = 2097151 & load_3(c); + int64_t c1 = 2097151 & (load_4(c + 2) >> 5); + int64_t c2 = 2097151 & (load_3(c + 5) >> 2); + int64_t c3 = 2097151 & (load_4(c + 7) >> 7); + int64_t c4 = 2097151 & (load_4(c + 10) >> 4); + int64_t c5 = 2097151 & (load_3(c + 13) >> 1); + int64_t c6 = 2097151 & (load_4(c + 15) >> 6); + int64_t c7 = 2097151 & (load_3(c + 18) >> 3); + int64_t c8 = 2097151 & load_3(c + 21); + int64_t c9 = 2097151 & (load_4(c + 23) >> 5); + int64_t c10 = 2097151 & (load_3(c + 26) >> 2); + int64_t c11 = (load_4(c + 28) >> 7); + int64_t s0; + int64_t s1; + int64_t s2; + int64_t s3; + int64_t s4; + int64_t s5; + int64_t s6; + int64_t s7; + int64_t s8; + int64_t s9; + int64_t s10; + int64_t s11; + int64_t s12; + int64_t s13; + int64_t s14; + int64_t s15; + int64_t s16; + int64_t s17; + int64_t s18; + int64_t s19; + int64_t s20; + int64_t s21; + int64_t s22; + int64_t s23; + int64_t carry0; + int64_t carry1; + int64_t carry2; + int64_t carry3; + int64_t carry4; + int64_t carry5; + int64_t carry6; + int64_t carry7; + int64_t carry8; + int64_t carry9; + int64_t carry10; + int64_t carry11; + int64_t carry12; + int64_t carry13; + int64_t carry14; + int64_t carry15; + int64_t carry16; + int64_t carry17; + int64_t carry18; + int64_t carry19; + int64_t carry20; + int64_t carry21; + int64_t carry22; + + s0 = c0 + a0 * b0; + s1 = c1 + a0 * b1 + a1 * b0; + s2 = c2 + a0 * b2 + a1 * b1 + a2 * b0; + s3 = c3 + a0 * b3 + a1 * b2 + a2 * b1 + a3 * b0; + s4 = c4 + a0 * b4 + a1 * b3 + a2 * b2 + a3 * b1 + a4 * b0; + s5 = c5 + a0 * b5 + a1 * b4 + a2 * b3 + a3 * b2 + a4 * b1 + a5 * b0; + s6 = c6 + a0 * b6 + a1 * b5 + a2 * b4 + a3 * b3 + a4 * b2 + a5 * b1 + a6 * b0; + s7 = c7 + a0 * b7 + a1 * b6 + a2 * b5 + a3 * b4 + a4 * b3 + a5 * b2 + a6 * b1 + a7 * b0; + s8 = c8 + a0 * b8 + a1 * b7 + a2 * b6 + a3 * b5 + a4 * b4 + a5 * b3 + a6 * b2 + a7 * b1 + a8 * b0; + s9 = c9 + a0 * b9 + a1 * b8 + a2 * b7 + a3 * b6 + a4 * b5 + a5 * b4 + a6 * b3 + a7 * b2 + a8 * b1 + a9 * b0; + s10 = c10 + a0 * b10 + a1 * b9 + a2 * b8 + a3 * b7 + a4 * b6 + a5 * b5 + a6 * b4 + a7 * b3 + a8 * b2 + a9 * b1 + a10 * b0; + s11 = c11 + a0 * b11 + a1 * b10 + a2 * b9 + a3 * b8 + a4 * b7 + a5 * b6 + a6 * b5 + a7 * b4 + a8 * b3 + a9 * b2 + a10 * b1 + a11 * b0; + s12 = a1 * b11 + a2 * b10 + a3 * b9 + a4 * b8 + a5 * b7 + a6 * b6 + a7 * b5 + a8 * b4 + a9 * b3 + a10 * b2 + a11 * b1; + s13 = a2 * b11 + a3 * b10 + a4 * b9 + a5 * b8 + a6 * b7 + a7 * b6 + a8 * b5 + a9 * b4 + a10 * b3 + a11 * b2; + s14 = a3 * b11 + a4 * b10 + a5 * b9 + a6 * b8 + a7 * b7 + a8 * b6 + a9 * b5 + a10 * b4 + a11 * b3; + s15 = a4 * b11 + a5 * b10 + a6 * b9 + a7 * b8 + a8 * b7 + a9 * b6 + a10 * b5 + a11 * b4; + s16 = a5 * b11 + a6 * b10 + a7 * b9 + a8 * b8 + a9 * b7 + a10 * b6 + a11 * b5; + s17 = a6 * b11 + a7 * b10 + a8 * b9 + a9 * b8 + a10 * b7 + a11 * b6; + s18 = a7 * b11 + a8 * b10 + a9 * b9 + a10 * b8 + a11 * b7; + s19 = a8 * b11 + a9 * b10 + a10 * b9 + a11 * b8; + s20 = a9 * b11 + a10 * b10 + a11 * b9; + s21 = a10 * b11 + a11 * b10; + s22 = a11 * b11; + s23 = 0; + carry0 = (s0 + (1 << 20)) >> 21; + s1 += carry0; + s0 -= carry0 << 21; + carry2 = (s2 + (1 << 20)) >> 21; + s3 += carry2; + s2 -= carry2 << 21; + carry4 = (s4 + (1 << 20)) >> 21; + s5 += carry4; + s4 -= carry4 << 21; + carry6 = (s6 + (1 << 20)) >> 21; + s7 += carry6; + s6 -= carry6 << 21; + carry8 = (s8 + (1 << 20)) >> 21; + s9 += carry8; + s8 -= carry8 << 21; + carry10 = (s10 + (1 << 20)) >> 21; + s11 += carry10; + s10 -= carry10 << 21; + carry12 = (s12 + (1 << 20)) >> 21; + s13 += carry12; + s12 -= carry12 << 21; + carry14 = (s14 + (1 << 20)) >> 21; + s15 += carry14; + s14 -= carry14 << 21; + carry16 = (s16 + (1 << 20)) >> 21; + s17 += carry16; + s16 -= carry16 << 21; + carry18 = (s18 + (1 << 20)) >> 21; + s19 += carry18; + s18 -= carry18 << 21; + carry20 = (s20 + (1 << 20)) >> 21; + s21 += carry20; + s20 -= carry20 << 21; + carry22 = (s22 + (1 << 20)) >> 21; + s23 += carry22; + s22 -= carry22 << 21; + carry1 = (s1 + (1 << 20)) >> 21; + s2 += carry1; + s1 -= carry1 << 21; + carry3 = (s3 + (1 << 20)) >> 21; + s4 += carry3; + s3 -= carry3 << 21; + carry5 = (s5 + (1 << 20)) >> 21; + s6 += carry5; + s5 -= carry5 << 21; + carry7 = (s7 + (1 << 20)) >> 21; + s8 += carry7; + s7 -= carry7 << 21; + carry9 = (s9 + (1 << 20)) >> 21; + s10 += carry9; + s9 -= carry9 << 21; + carry11 = (s11 + (1 << 20)) >> 21; + s12 += carry11; + s11 -= carry11 << 21; + carry13 = (s13 + (1 << 20)) >> 21; + s14 += carry13; + s13 -= carry13 << 21; + carry15 = (s15 + (1 << 20)) >> 21; + s16 += carry15; + s15 -= carry15 << 21; + carry17 = (s17 + (1 << 20)) >> 21; + s18 += carry17; + s17 -= carry17 << 21; + carry19 = (s19 + (1 << 20)) >> 21; + s20 += carry19; + s19 -= carry19 << 21; + carry21 = (s21 + (1 << 20)) >> 21; + s22 += carry21; + s21 -= carry21 << 21; + s11 += s23 * 666643; + s12 += s23 * 470296; + s13 += s23 * 654183; + s14 -= s23 * 997805; + s15 += s23 * 136657; + s16 -= s23 * 683901; + s23 = 0; + s10 += s22 * 666643; + s11 += s22 * 470296; + s12 += s22 * 654183; + s13 -= s22 * 997805; + s14 += s22 * 136657; + s15 -= s22 * 683901; + s22 = 0; + s9 += s21 * 666643; + s10 += s21 * 470296; + s11 += s21 * 654183; + s12 -= s21 * 997805; + s13 += s21 * 136657; + s14 -= s21 * 683901; + s21 = 0; + s8 += s20 * 666643; + s9 += s20 * 470296; + s10 += s20 * 654183; + s11 -= s20 * 997805; + s12 += s20 * 136657; + s13 -= s20 * 683901; + s20 = 0; + s7 += s19 * 666643; + s8 += s19 * 470296; + s9 += s19 * 654183; + s10 -= s19 * 997805; + s11 += s19 * 136657; + s12 -= s19 * 683901; + s19 = 0; + s6 += s18 * 666643; + s7 += s18 * 470296; + s8 += s18 * 654183; + s9 -= s18 * 997805; + s10 += s18 * 136657; + s11 -= s18 * 683901; + s18 = 0; + carry6 = (s6 + (1 << 20)) >> 21; + s7 += carry6; + s6 -= carry6 << 21; + carry8 = (s8 + (1 << 20)) >> 21; + s9 += carry8; + s8 -= carry8 << 21; + carry10 = (s10 + (1 << 20)) >> 21; + s11 += carry10; + s10 -= carry10 << 21; + carry12 = (s12 + (1 << 20)) >> 21; + s13 += carry12; + s12 -= carry12 << 21; + carry14 = (s14 + (1 << 20)) >> 21; + s15 += carry14; + s14 -= carry14 << 21; + carry16 = (s16 + (1 << 20)) >> 21; + s17 += carry16; + s16 -= carry16 << 21; + carry7 = (s7 + (1 << 20)) >> 21; + s8 += carry7; + s7 -= carry7 << 21; + carry9 = (s9 + (1 << 20)) >> 21; + s10 += carry9; + s9 -= carry9 << 21; + carry11 = (s11 + (1 << 20)) >> 21; + s12 += carry11; + s11 -= carry11 << 21; + carry13 = (s13 + (1 << 20)) >> 21; + s14 += carry13; + s13 -= carry13 << 21; + carry15 = (s15 + (1 << 20)) >> 21; + s16 += carry15; + s15 -= carry15 << 21; + s5 += s17 * 666643; + s6 += s17 * 470296; + s7 += s17 * 654183; + s8 -= s17 * 997805; + s9 += s17 * 136657; + s10 -= s17 * 683901; + s17 = 0; + s4 += s16 * 666643; + s5 += s16 * 470296; + s6 += s16 * 654183; + s7 -= s16 * 997805; + s8 += s16 * 136657; + s9 -= s16 * 683901; + s16 = 0; + s3 += s15 * 666643; + s4 += s15 * 470296; + s5 += s15 * 654183; + s6 -= s15 * 997805; + s7 += s15 * 136657; + s8 -= s15 * 683901; + s15 = 0; + s2 += s14 * 666643; + s3 += s14 * 470296; + s4 += s14 * 654183; + s5 -= s14 * 997805; + s6 += s14 * 136657; + s7 -= s14 * 683901; + s14 = 0; + s1 += s13 * 666643; + s2 += s13 * 470296; + s3 += s13 * 654183; + s4 -= s13 * 997805; + s5 += s13 * 136657; + s6 -= s13 * 683901; + s13 = 0; + s0 += s12 * 666643; + s1 += s12 * 470296; + s2 += s12 * 654183; + s3 -= s12 * 997805; + s4 += s12 * 136657; + s5 -= s12 * 683901; + s12 = 0; + carry0 = (s0 + (1 << 20)) >> 21; + s1 += carry0; + s0 -= carry0 << 21; + carry2 = (s2 + (1 << 20)) >> 21; + s3 += carry2; + s2 -= carry2 << 21; + carry4 = (s4 + (1 << 20)) >> 21; + s5 += carry4; + s4 -= carry4 << 21; + carry6 = (s6 + (1 << 20)) >> 21; + s7 += carry6; + s6 -= carry6 << 21; + carry8 = (s8 + (1 << 20)) >> 21; + s9 += carry8; + s8 -= carry8 << 21; + carry10 = (s10 + (1 << 20)) >> 21; + s11 += carry10; + s10 -= carry10 << 21; + carry1 = (s1 + (1 << 20)) >> 21; + s2 += carry1; + s1 -= carry1 << 21; + carry3 = (s3 + (1 << 20)) >> 21; + s4 += carry3; + s3 -= carry3 << 21; + carry5 = (s5 + (1 << 20)) >> 21; + s6 += carry5; + s5 -= carry5 << 21; + carry7 = (s7 + (1 << 20)) >> 21; + s8 += carry7; + s7 -= carry7 << 21; + carry9 = (s9 + (1 << 20)) >> 21; + s10 += carry9; + s9 -= carry9 << 21; + carry11 = (s11 + (1 << 20)) >> 21; + s12 += carry11; + s11 -= carry11 << 21; + s0 += s12 * 666643; + s1 += s12 * 470296; + s2 += s12 * 654183; + s3 -= s12 * 997805; + s4 += s12 * 136657; + s5 -= s12 * 683901; + s12 = 0; + carry0 = s0 >> 21; + s1 += carry0; + s0 -= carry0 << 21; + carry1 = s1 >> 21; + s2 += carry1; + s1 -= carry1 << 21; + carry2 = s2 >> 21; + s3 += carry2; + s2 -= carry2 << 21; + carry3 = s3 >> 21; + s4 += carry3; + s3 -= carry3 << 21; + carry4 = s4 >> 21; + s5 += carry4; + s4 -= carry4 << 21; + carry5 = s5 >> 21; + s6 += carry5; + s5 -= carry5 << 21; + carry6 = s6 >> 21; + s7 += carry6; + s6 -= carry6 << 21; + carry7 = s7 >> 21; + s8 += carry7; + s7 -= carry7 << 21; + carry8 = s8 >> 21; + s9 += carry8; + s8 -= carry8 << 21; + carry9 = s9 >> 21; + s10 += carry9; + s9 -= carry9 << 21; + carry10 = s10 >> 21; + s11 += carry10; + s10 -= carry10 << 21; + carry11 = s11 >> 21; + s12 += carry11; + s11 -= carry11 << 21; + s0 += s12 * 666643; + s1 += s12 * 470296; + s2 += s12 * 654183; + s3 -= s12 * 997805; + s4 += s12 * 136657; + s5 -= s12 * 683901; + s12 = 0; + carry0 = s0 >> 21; + s1 += carry0; + s0 -= carry0 << 21; + carry1 = s1 >> 21; + s2 += carry1; + s1 -= carry1 << 21; + carry2 = s2 >> 21; + s3 += carry2; + s2 -= carry2 << 21; + carry3 = s3 >> 21; + s4 += carry3; + s3 -= carry3 << 21; + carry4 = s4 >> 21; + s5 += carry4; + s4 -= carry4 << 21; + carry5 = s5 >> 21; + s6 += carry5; + s5 -= carry5 << 21; + carry6 = s6 >> 21; + s7 += carry6; + s6 -= carry6 << 21; + carry7 = s7 >> 21; + s8 += carry7; + s7 -= carry7 << 21; + carry8 = s8 >> 21; + s9 += carry8; + s8 -= carry8 << 21; + carry9 = s9 >> 21; + s10 += carry9; + s9 -= carry9 << 21; + carry10 = s10 >> 21; + s11 += carry10; + s10 -= carry10 << 21; + + s[0] = (unsigned char) (s0 >> 0); + s[1] = (unsigned char) (s0 >> 8); + s[2] = (unsigned char) ((s0 >> 16) | (s1 << 5)); + s[3] = (unsigned char) (s1 >> 3); + s[4] = (unsigned char) (s1 >> 11); + s[5] = (unsigned char) ((s1 >> 19) | (s2 << 2)); + s[6] = (unsigned char) (s2 >> 6); + s[7] = (unsigned char) ((s2 >> 14) | (s3 << 7)); + s[8] = (unsigned char) (s3 >> 1); + s[9] = (unsigned char) (s3 >> 9); + s[10] = (unsigned char) ((s3 >> 17) | (s4 << 4)); + s[11] = (unsigned char) (s4 >> 4); + s[12] = (unsigned char) (s4 >> 12); + s[13] = (unsigned char) ((s4 >> 20) | (s5 << 1)); + s[14] = (unsigned char) (s5 >> 7); + s[15] = (unsigned char) ((s5 >> 15) | (s6 << 6)); + s[16] = (unsigned char) (s6 >> 2); + s[17] = (unsigned char) (s6 >> 10); + s[18] = (unsigned char) ((s6 >> 18) | (s7 << 3)); + s[19] = (unsigned char) (s7 >> 5); + s[20] = (unsigned char) (s7 >> 13); + s[21] = (unsigned char) (s8 >> 0); + s[22] = (unsigned char) (s8 >> 8); + s[23] = (unsigned char) ((s8 >> 16) | (s9 << 5)); + s[24] = (unsigned char) (s9 >> 3); + s[25] = (unsigned char) (s9 >> 11); + s[26] = (unsigned char) ((s9 >> 19) | (s10 << 2)); + s[27] = (unsigned char) (s10 >> 6); + s[28] = (unsigned char) ((s10 >> 14) | (s11 << 7)); + s[29] = (unsigned char) (s11 >> 1); + s[30] = (unsigned char) (s11 >> 9); + s[31] = (unsigned char) (s11 >> 17); +} diff --git a/core/eos/services/crypto/src/ed25519_sha512.c b/core/eos/services/crypto/src/ed25519_sha512.c new file mode 100644 index 00000000..ebb89e28 --- /dev/null +++ b/core/eos/services/crypto/src/ed25519_sha512.c @@ -0,0 +1,278 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + * + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org + */ + +#include "fixedint.h" +#include "sha512.h" + +/* the K array */ +static const uint64_t K[80] = { + UINT64_C(0x428a2f98d728ae22), UINT64_C(0x7137449123ef65cd), + UINT64_C(0xb5c0fbcfec4d3b2f), UINT64_C(0xe9b5dba58189dbbc), + UINT64_C(0x3956c25bf348b538), UINT64_C(0x59f111f1b605d019), + UINT64_C(0x923f82a4af194f9b), UINT64_C(0xab1c5ed5da6d8118), + UINT64_C(0xd807aa98a3030242), UINT64_C(0x12835b0145706fbe), + UINT64_C(0x243185be4ee4b28c), UINT64_C(0x550c7dc3d5ffb4e2), + UINT64_C(0x72be5d74f27b896f), UINT64_C(0x80deb1fe3b1696b1), + UINT64_C(0x9bdc06a725c71235), UINT64_C(0xc19bf174cf692694), + UINT64_C(0xe49b69c19ef14ad2), UINT64_C(0xefbe4786384f25e3), + UINT64_C(0x0fc19dc68b8cd5b5), UINT64_C(0x240ca1cc77ac9c65), + UINT64_C(0x2de92c6f592b0275), UINT64_C(0x4a7484aa6ea6e483), + UINT64_C(0x5cb0a9dcbd41fbd4), UINT64_C(0x76f988da831153b5), + UINT64_C(0x983e5152ee66dfab), UINT64_C(0xa831c66d2db43210), + UINT64_C(0xb00327c898fb213f), UINT64_C(0xbf597fc7beef0ee4), + UINT64_C(0xc6e00bf33da88fc2), UINT64_C(0xd5a79147930aa725), + UINT64_C(0x06ca6351e003826f), UINT64_C(0x142929670a0e6e70), + UINT64_C(0x27b70a8546d22ffc), UINT64_C(0x2e1b21385c26c926), + UINT64_C(0x4d2c6dfc5ac42aed), UINT64_C(0x53380d139d95b3df), + UINT64_C(0x650a73548baf63de), UINT64_C(0x766a0abb3c77b2a8), + UINT64_C(0x81c2c92e47edaee6), UINT64_C(0x92722c851482353b), + UINT64_C(0xa2bfe8a14cf10364), UINT64_C(0xa81a664bbc423001), + UINT64_C(0xc24b8b70d0f89791), UINT64_C(0xc76c51a30654be30), + UINT64_C(0xd192e819d6ef5218), UINT64_C(0xd69906245565a910), + UINT64_C(0xf40e35855771202a), UINT64_C(0x106aa07032bbd1b8), + UINT64_C(0x19a4c116b8d2d0c8), UINT64_C(0x1e376c085141ab53), + UINT64_C(0x2748774cdf8eeb99), UINT64_C(0x34b0bcb5e19b48a8), + UINT64_C(0x391c0cb3c5c95a63), UINT64_C(0x4ed8aa4ae3418acb), + UINT64_C(0x5b9cca4f7763e373), UINT64_C(0x682e6ff3d6b2b8a3), + UINT64_C(0x748f82ee5defb2fc), UINT64_C(0x78a5636f43172f60), + UINT64_C(0x84c87814a1f0ab72), UINT64_C(0x8cc702081a6439ec), + UINT64_C(0x90befffa23631e28), UINT64_C(0xa4506cebde82bde9), + UINT64_C(0xbef9a3f7b2c67915), UINT64_C(0xc67178f2e372532b), + UINT64_C(0xca273eceea26619c), UINT64_C(0xd186b8c721c0c207), + UINT64_C(0xeada7dd6cde0eb1e), UINT64_C(0xf57d4f7fee6ed178), + UINT64_C(0x06f067aa72176fba), UINT64_C(0x0a637dc5a2c898a6), + UINT64_C(0x113f9804bef90dae), UINT64_C(0x1b710b35131c471b), + UINT64_C(0x28db77f523047d84), UINT64_C(0x32caab7b40c72493), + UINT64_C(0x3c9ebe0a15c9bebc), UINT64_C(0x431d67c49c100d4c), + UINT64_C(0x4cc5d4becb3e42b6), UINT64_C(0x597f299cfc657e2a), + UINT64_C(0x5fcb6fab3ad6faec), UINT64_C(0x6c44198c4a475817) +}; + +/* Various logical functions */ + +#define ROR64c(x, y) \ + ( ((((x)&UINT64_C(0xFFFFFFFFFFFFFFFF))>>((uint64_t)(y)&UINT64_C(63))) | \ + ((x)<<((uint64_t)(64-((y)&UINT64_C(63)))))) & UINT64_C(0xFFFFFFFFFFFFFFFF)) + +#define STORE64H(x, y) \ + { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \ + (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \ + (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \ + (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); } + +#define LOAD64H(x, y) \ + { x = (((uint64_t)((y)[0] & 255))<<56)|(((uint64_t)((y)[1] & 255))<<48) | \ + (((uint64_t)((y)[2] & 255))<<40)|(((uint64_t)((y)[3] & 255))<<32) | \ + (((uint64_t)((y)[4] & 255))<<24)|(((uint64_t)((y)[5] & 255))<<16) | \ + (((uint64_t)((y)[6] & 255))<<8)|(((uint64_t)((y)[7] & 255))); } + + +#define Ch(x,y,z) (z ^ (x & (y ^ z))) +#define Maj(x,y,z) (((x | y) & z) | (x & y)) +#define S(x, n) ROR64c(x, n) +#define R(x, n) (((x) &UINT64_C(0xFFFFFFFFFFFFFFFF))>>((uint64_t)n)) +#define Sigma0(x) (S(x, 28) ^ S(x, 34) ^ S(x, 39)) +#define Sigma1(x) (S(x, 14) ^ S(x, 18) ^ S(x, 41)) +#define Gamma0(x) (S(x, 1) ^ S(x, 8) ^ R(x, 7)) +#define Gamma1(x) (S(x, 19) ^ S(x, 61) ^ R(x, 6)) +#ifndef MIN + #define MIN(x, y) ( ((x)<(y))?(x):(y) ) +#endif + +/* compress 1024-bits */ +static int sha512_compress(sha512_context *md, unsigned char *buf) +{ + uint64_t S[8], W[80], t0, t1; + int i; + + /* copy state into S */ + for (i = 0; i < 8; i++) { + S[i] = md->state[i]; + } + + /* copy the state into 1024-bits into W[0..15] */ + for (i = 0; i < 16; i++) { + LOAD64H(W[i], buf + (8*i)); + } + + /* fill W[16..79] */ + for (i = 16; i < 80; i++) { + W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16]; + } + +/* Compress */ + #define RND(a,b,c,d,e,f,g,h,i) \ + t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i]; \ + t1 = Sigma0(a) + Maj(a, b, c);\ + d += t0; \ + h = t0 + t1; + + for (i = 0; i < 80; i += 8) { + RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],i+0); + RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],i+1); + RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],i+2); + RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],i+3); + RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],i+4); + RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],i+5); + RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],i+6); + RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],i+7); + } + + #undef RND + + + + /* feedback */ + for (i = 0; i < 8; i++) { + md->state[i] = md->state[i] + S[i]; + } + + return 0; +} + + +/** + Initialize the hash state + @param md The hash state you wish to initialize + @return 0 if successful +*/ +int sha512_init(sha512_context * md) { + if (md == NULL) return 1; + + md->curlen = 0; + md->length = 0; + md->state[0] = UINT64_C(0x6a09e667f3bcc908); + md->state[1] = UINT64_C(0xbb67ae8584caa73b); + md->state[2] = UINT64_C(0x3c6ef372fe94f82b); + md->state[3] = UINT64_C(0xa54ff53a5f1d36f1); + md->state[4] = UINT64_C(0x510e527fade682d1); + md->state[5] = UINT64_C(0x9b05688c2b3e6c1f); + md->state[6] = UINT64_C(0x1f83d9abfb41bd6b); + md->state[7] = UINT64_C(0x5be0cd19137e2179); + + return 0; +} + +/** + Process a block of memory though the hash + @param md The hash state + @param in The data to hash + @param inlen The length of the data (octets) + @return 0 if successful +*/ +int sha512_update (sha512_context * md, const unsigned char *in, size_t inlen) +{ + size_t n; + size_t i; + int err; + if (md == NULL) return 1; + if (in == NULL) return 1; + if (md->curlen > sizeof(md->buf)) { + return 1; + } + while (inlen > 0) { + if (md->curlen == 0 && inlen >= 128) { + if ((err = sha512_compress (md, (unsigned char *)in)) != 0) { + return err; + } + md->length += 128 * 8; + in += 128; + inlen -= 128; + } else { + n = MIN(inlen, (128 - md->curlen)); + + for (i = 0; i < n; i++) { + md->buf[i + md->curlen] = in[i]; + } + + + md->curlen += n; + in += n; + inlen -= n; + if (md->curlen == 128) { + if ((err = sha512_compress (md, md->buf)) != 0) { + return err; + } + md->length += 8*128; + md->curlen = 0; + } + } + } + return 0; +} + +/** + Terminate the hash to get the digest + @param md The hash state + @param out [out] The destination of the hash (64 bytes) + @return 0 if successful +*/ + int sha512_final(sha512_context * md, unsigned char *out) + { + int i; + + if (md == NULL) return 1; + if (out == NULL) return 1; + + if (md->curlen >= sizeof(md->buf)) { + return 1; + } + + /* increase the length of the message */ + md->length += md->curlen * UINT64_C(8); + + /* append the '1' bit */ + md->buf[md->curlen++] = (unsigned char)0x80; + + /* if the length is currently above 112 bytes we append zeros + * then compress. Then we can fall back to padding zeros and length + * encoding like normal. + */ + if (md->curlen > 112) { + while (md->curlen < 128) { + md->buf[md->curlen++] = (unsigned char)0; + } + sha512_compress(md, md->buf); + md->curlen = 0; + } + + /* pad upto 120 bytes of zeroes + * note: that from 112 to 120 is the 64 MSB of the length. We assume that you won't hash + * > 2^64 bits of data... :-) + */ +while (md->curlen < 120) { + md->buf[md->curlen++] = (unsigned char)0; +} + + /* store length */ +STORE64H(md->length, md->buf+120); +sha512_compress(md, md->buf); + + /* copy output */ +for (i = 0; i < 8; i++) { + STORE64H(md->state[i], out+(8*i)); +} + +return 0; +} + +int sha512(const unsigned char *message, size_t message_len, unsigned char *out) +{ + sha512_context ctx; + int ret; + ret = sha512_init(&ctx); + if (ret) return ret; + ret = sha512_update(&ctx, message, message_len); + if (ret) return ret; + ret = sha512_final(&ctx, out); + if (ret) return ret; + return 0; +} diff --git a/core/eos/services/crypto/src/ed25519_sign.c b/core/eos/services/crypto/src/ed25519_sign.c new file mode 100644 index 00000000..30a405bf --- /dev/null +++ b/core/eos/services/crypto/src/ed25519_sign.c @@ -0,0 +1,94 @@ +/* SPDX-License-Identifier: MIT + * Copyright (c) 2026 EoS Project + * + * @file ed25519_sign.c + * @brief Ed25519 key generation and signing (ref10 formulation). + * + * ed25519.h has always declared ed25519_create_keypair() and ed25519_sign(), + * but no translation unit implemented them: the tree could verify signatures + * and never produce one. That made the declarations a trap -- any .eapp + * producer tooling (see #162, which needs producers to sign the v2 envelope) + * would fail at link time. This file implements both on top of the existing + * field/scalar/curve primitives. + * + * The private key layout is seed || public_key (64 bytes), the ref10 + * convention: ed25519_sign() re-derives the expanded secret (az) from the + * seed on every call, so the stored private key never holds the expanded + * scalar at rest. + */ + +#include "ed25519.h" +#include "sha512.h" +#include "ge.h" +#include "sc.h" + +#include + +void ed25519_create_keypair(unsigned char *public_key, + unsigned char *private_key, + const unsigned char *seed) +{ + sha512_context hash; + unsigned char expanded[64]; + ge_p3 A; + + sha512_init(&hash); + sha512_update(&hash, seed, 32); + sha512_final(&hash, expanded); + + expanded[0] &= 248; + expanded[31] &= 63; + expanded[31] |= 64; + + ge_scalarmult_base(&A, expanded); + ge_p3_tobytes(public_key, &A); + + memcpy(private_key, seed, 32); + memcpy(private_key + 32, public_key, 32); + + memset(expanded, 0, sizeof(expanded)); +} + +void ed25519_sign(unsigned char *signature, + const unsigned char *message, size_t message_len, + const unsigned char *public_key, + const unsigned char *private_key) +{ + sha512_context hash; + unsigned char az[64]; + unsigned char nonce[64]; + unsigned char hram[64]; + ge_p3 R; + + /* az = SHA-512(seed), clamped; seed is the first half of private_key. */ + sha512_init(&hash); + sha512_update(&hash, private_key, 32); + sha512_final(&hash, az); + az[0] &= 248; + az[31] &= 63; + az[31] |= 64; + + /* nonce = SHA-512(az[32..64] || message), reduced mod L. */ + sha512_init(&hash); + sha512_update(&hash, az + 32, 32); + sha512_update(&hash, message, message_len); + sha512_final(&hash, nonce); + sc_reduce(nonce); + + ge_scalarmult_base(&R, nonce); + ge_p3_tobytes(signature, &R); + + /* hram = SHA-512(R || public_key || message), reduced mod L. */ + sha512_init(&hash); + sha512_update(&hash, signature, 32); + sha512_update(&hash, public_key, 32); + sha512_update(&hash, message, message_len); + sha512_final(&hash, hram); + sc_reduce(hram); + + /* S = hram * az + nonce (mod L). */ + sc_muladd(signature + 32, hram, az, nonce); + + memset(az, 0, sizeof(az)); + memset(nonce, 0, sizeof(nonce)); +} diff --git a/core/eos/services/crypto/src/ed25519_verify.c b/core/eos/services/crypto/src/ed25519_verify.c new file mode 100644 index 00000000..5afe44cd --- /dev/null +++ b/core/eos/services/crypto/src/ed25519_verify.c @@ -0,0 +1,226 @@ +#include "ed25519.h" +#include "sha512.h" +#include "ge.h" +#include "sc.h" + +static int consttime_equal(const unsigned char *x, const unsigned char *y) { + unsigned char r = 0; + + r = x[0] ^ y[0]; + #define F(i) r |= x[i] ^ y[i] + F(1); + F(2); + F(3); + F(4); + F(5); + F(6); + F(7); + F(8); + F(9); + F(10); + F(11); + F(12); + F(13); + F(14); + F(15); + F(16); + F(17); + F(18); + F(19); + F(20); + F(21); + F(22); + F(23); + F(24); + F(25); + F(26); + F(27); + F(28); + F(29); + F(30); + F(31); + #undef F + + return !r; +} + + +/* L = 2^252 + 27742317777372353535851937790883648493, the prime order of the + * Ed25519 base-point subgroup, little-endian. */ +static const unsigned char ED25519_ORDER_L[32] = { + 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, + 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10 +}; + +/* Reject public keys outside the prime-order subgroup. + * + * Decoding a point is not enough. Ed25519 has eight low-order points, and for + * any of them every term of the verification equation collapses to the identity + * regardless of the message -- so a signature of all zeros verifies against any + * content at all. That is not a weakened signature; it is no signature. + * + * The all-zero encoding is one of the eight, which mattered here: eos_pkg + * shipped eos_pkg_public_key[32] = {0} and consequently accepted every package + * handed to it. + * + * Tested by multiplying the key by L and requiring the identity, rather than by + * comparing against a table of the eight encodings. Both are correct; this one + * cannot be wrong in a way that silently rejects valid keys because a constant + * was transcribed incorrectly. + * + * Multiplying by L alone is not sufficient: the identity has order 1, which + * divides L, so [L]identity = identity and it passes the subgroup test. It is + * rejected separately. Verified rather than assumed -- the first version of + * this check had only the subgroup test and still accepted an identity key. + * + * A is negated by ge_frombytes_negate_vartime(). [L](-A) = -[L]A, and the + * identity is its own negation, so neither test is affected. + */ +static int ed25519_point_is_identity(const unsigned char packed[32]) { + int i; + if (packed[0] != 1) { + return 0; + } + for (i = 1; i < 32; i++) { + if (packed[i] != 0) { + return 0; + } + } + return 1; +} + +static int ed25519_key_has_prime_order(const ge_p3 *A) { + static const unsigned char zero_scalar[32] = {0}; + unsigned char packed[32]; + ge_p2 A2; + ge_p2 LA; + + /* The identity is order 1, so [L]identity = identity and the subgroup + * test below would admit it. Reject it first. */ + ge_p3_to_p2(&A2, A); + ge_tobytes(packed, &A2); + if (ed25519_point_is_identity(packed)) { + return 0; + } + + /* [L]A + [0]B */ + ge_double_scalarmult_vartime(&LA, ED25519_ORDER_L, A, zero_scalar); + ge_tobytes(packed, &LA); + + /* A key of prime order L multiplies to the identity. */ + return ed25519_point_is_identity(packed); +} + +/* Is this encoding usable as a trust anchor? + * + * ed25519_verify() runs the same two tests, but only once a signature is in + * hand, and a failure there is indistinguishable from a bad signature. A + * caller that stores a key -- eos_pkg's trust anchor, see #98 -- needs to ask + * about the key alone, at the moment it is configured, so an unusable anchor + * is reported as an unusable anchor rather than as a stream of packages that + * all appear to be forged. + */ +int ed25519_public_key_is_usable(const unsigned char *public_key) { + ge_p3 A; + + if (public_key == 0) { + return 0; + } + if (ge_frombytes_negate_vartime(&A, public_key) != 0) { + return 0; + } + return ed25519_key_has_prime_order(&A); +} + +/* Reject a signature whose S is not the canonical representative mod L. + * + * The check above this one -- signature[63] & 224 -- is ref10's partial test. + * It rejects any S with a bit set above 2^252, but L is 2^252 + + * 27742317777372353535851937790883648493, so every S in [L, 2^253) passes it + * while being non-canonical. Adding L to a valid S lands squarely in that band: + * (R, S) and (R, S + L) both verified, which is signature malleability. Anything + * that treats a signature as a unique identifier -- deduplication, replay + * caches, logging by signature hash -- sees two distinct signatures over one + * message and one key. + * + * RFC 8032 5.1.7 requires "reject the signature if S is not in the range + * [0, L)". eBoot's core/ed25519_verify.c already does this; this side did not, + * which is precisely the kind of divergence the shared contract corpus in + * tests/vectors/ed25519_contract_vectors.h exists to surface -- and it is how + * this was found. + * + * Compared most-significant byte first, which is a comparison against a public + * constant and reveals nothing about any secret: S is public. + */ +/* Through the public API this boundary is unreachable: a signature with + * S == L and one that simply fails the verification equation both return 0, + * so the corpus cannot tell an off-by-one here from an ordinary rejection + * -- and an off-by-one that refused a valid S = L - 1 would satisfy every + * vector in the corpus while rejecting real signatures in the field. It is + * a pure function of 32 bytes, so it can be tested directly and without + * generating any signatures. + * + * It stays static. tests/test_ed25519_canonical_s.c includes this translation + * unit to reach it, rather than the file being compiled twice with a macro + * that changes this symbol's linkage -- that arrangement made the function + * look unreachable to static analysis, and a linkage that varies by build is + * a poor thing to have in a crypto source file. */ +static int sc_is_canonical(const unsigned char s[32]) { + /* L, little-endian. */ + static const unsigned char L[32] = { + 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, + 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10 + }; + int i; + for (i = 31; i >= 0; i--) { + if (s[i] < L[i]) return 1; + if (s[i] > L[i]) return 0; + } + return 0; /* S == L is not in [0, L) either */ +} + +int ed25519_verify(const unsigned char *signature, const unsigned char *message, size_t message_len, const unsigned char *public_key) { + unsigned char h[64]; + unsigned char checker[32]; + sha512_context hash; + ge_p3 A; + ge_p2 R; + + if (signature[63] & 224) { + return 0; + } + + /* The full range check RFC 8032 5.1.7 asks for; the test above is only + * its top three bits. */ + if (!sc_is_canonical(signature + 32)) { + return 0; + } + + if (ge_frombytes_negate_vartime(&A, public_key) != 0) { + return 0; + } + + if (!ed25519_key_has_prime_order(&A)) { + return 0; + } + + sha512_init(&hash); + sha512_update(&hash, signature, 32); + sha512_update(&hash, public_key, 32); + sha512_update(&hash, message, message_len); + sha512_final(&hash, h); + + sc_reduce(h); + ge_double_scalarmult_vartime(&R, h, &A, signature + 32); + ge_tobytes(checker, &R); + + if (!consttime_equal(checker, signature)) { + return 0; + } + + return 1; +} diff --git a/core/eos/services/crypto/src/fe.h b/core/eos/services/crypto/src/fe.h new file mode 100644 index 00000000..b4b62d28 --- /dev/null +++ b/core/eos/services/crypto/src/fe.h @@ -0,0 +1,41 @@ +#ifndef FE_H +#define FE_H + +#include "fixedint.h" + + +/* + fe means field element. + Here the field is \Z/(2^255-19). + An element t, entries t[0]...t[9], represents the integer + t[0]+2^26 t[1]+2^51 t[2]+2^77 t[3]+2^102 t[4]+...+2^230 t[9]. + Bounds on each t[i] vary depending on context. +*/ + + +typedef int32_t fe[10]; + + +void fe_0(fe h); +void fe_1(fe h); + +void fe_frombytes(fe h, const unsigned char *s); +void fe_tobytes(unsigned char *s, const fe h); + +void fe_copy(fe h, const fe f); +int fe_isnegative(const fe f); +int fe_isnonzero(const fe f); +void fe_cmov(fe f, const fe g, unsigned int b); +void fe_cswap(fe f, fe g, unsigned int b); + +void fe_neg(fe h, const fe f); +void fe_add(fe h, const fe f, const fe g); +void fe_invert(fe out, const fe z); +void fe_sq(fe h, const fe f); +void fe_sq2(fe h, const fe f); +void fe_mul(fe h, const fe f, const fe g); +void fe_mul121666(fe h, fe f); +void fe_pow22523(fe out, const fe z); +void fe_sub(fe h, const fe f, const fe g); + +#endif diff --git a/core/eos/services/crypto/src/fixedint.h b/core/eos/services/crypto/src/fixedint.h new file mode 100644 index 00000000..1a8745b1 --- /dev/null +++ b/core/eos/services/crypto/src/fixedint.h @@ -0,0 +1,72 @@ +/* + Portable header to provide the 32 and 64 bits type. + + Not a compatible replacement for , do not blindly use it as such. +*/ + +#if ((defined(__STDC__) && __STDC__ && __STDC_VERSION__ >= 199901L) || (defined(__WATCOMC__) && (defined(_STDINT_H_INCLUDED) || __WATCOMC__ >= 1250)) || (defined(__GNUC__) && (defined(_STDINT_H) || defined(_STDINT_H_) || defined(__UINT_FAST64_TYPE__)) )) && !defined(FIXEDINT_H_INCLUDED) + #include + #define FIXEDINT_H_INCLUDED + + #if defined(__WATCOMC__) && __WATCOMC__ >= 1250 && !defined(UINT64_C) + #include + #define UINT64_C(x) (x + (UINT64_MAX - UINT64_MAX)) + #endif +#endif + + +#ifndef FIXEDINT_H_INCLUDED + #define FIXEDINT_H_INCLUDED + + #include + + /* (u)int32_t */ + #ifndef uint32_t + #if (ULONG_MAX == 0xffffffffUL) + typedef unsigned long uint32_t; + #elif (UINT_MAX == 0xffffffffUL) + typedef unsigned int uint32_t; + #elif (USHRT_MAX == 0xffffffffUL) + typedef unsigned short uint32_t; + #endif + #endif + + + #ifndef int32_t + #if (LONG_MAX == 0x7fffffffL) + typedef signed long int32_t; + #elif (INT_MAX == 0x7fffffffL) + typedef signed int int32_t; + #elif (SHRT_MAX == 0x7fffffffL) + typedef signed short int32_t; + #endif + #endif + + + /* (u)int64_t */ + #if (defined(__STDC__) && defined(__STDC_VERSION__) && __STDC__ && __STDC_VERSION__ >= 199901L) + typedef long long int64_t; + typedef unsigned long long uint64_t; + + #define UINT64_C(v) v ##ULL + #define INT64_C(v) v ##LL + #elif defined(__GNUC__) + __extension__ typedef long long int64_t; + __extension__ typedef unsigned long long uint64_t; + + #define UINT64_C(v) v ##ULL + #define INT64_C(v) v ##LL + #elif defined(__MWERKS__) || defined(__SUNPRO_C) || defined(__SUNPRO_CC) || defined(__APPLE_CC__) || defined(_LONG_LONG) || defined(_CRAYC) + typedef long long int64_t; + typedef unsigned long long uint64_t; + + #define UINT64_C(v) v ##ULL + #define INT64_C(v) v ##LL + #elif (defined(__WATCOMC__) && defined(__WATCOM_INT64__)) || (defined(_MSC_VER) && _INTEGRAL_MAX_BITS >= 64) || (defined(__BORLANDC__) && __BORLANDC__ > 0x460) || defined(__alpha) || defined(__DECC) + typedef __int64 int64_t; + typedef unsigned __int64 uint64_t; + + #define UINT64_C(v) v ##UI64 + #define INT64_C(v) v ##I64 + #endif +#endif diff --git a/core/eos/services/crypto/src/ge.h b/core/eos/services/crypto/src/ge.h new file mode 100644 index 00000000..17fde2df --- /dev/null +++ b/core/eos/services/crypto/src/ge.h @@ -0,0 +1,74 @@ +#ifndef GE_H +#define GE_H + +#include "fe.h" + + +/* +ge means group element. + +Here the group is the set of pairs (x,y) of field elements (see fe.h) +satisfying -x^2 + y^2 = 1 + d x^2y^2 +where d = -121665/121666. + +Representations: + ge_p2 (projective): (X:Y:Z) satisfying x=X/Z, y=Y/Z + ge_p3 (extended): (X:Y:Z:T) satisfying x=X/Z, y=Y/Z, XY=ZT + ge_p1p1 (completed): ((X:Z),(Y:T)) satisfying x=X/Z, y=Y/T + ge_precomp (Duif): (y+x,y-x,2dxy) +*/ + +typedef struct { + fe X; + fe Y; + fe Z; +} ge_p2; + +typedef struct { + fe X; + fe Y; + fe Z; + fe T; +} ge_p3; + +typedef struct { + fe X; + fe Y; + fe Z; + fe T; +} ge_p1p1; + +typedef struct { + fe yplusx; + fe yminusx; + fe xy2d; +} ge_precomp; + +typedef struct { + fe YplusX; + fe YminusX; + fe Z; + fe T2d; +} ge_cached; + +void ge_p3_tobytes(unsigned char *s, const ge_p3 *h); +void ge_tobytes(unsigned char *s, const ge_p2 *h); +int ge_frombytes_negate_vartime(ge_p3 *h, const unsigned char *s); + +void ge_add(ge_p1p1 *r, const ge_p3 *p, const ge_cached *q); +void ge_sub(ge_p1p1 *r, const ge_p3 *p, const ge_cached *q); +void ge_double_scalarmult_vartime(ge_p2 *r, const unsigned char *a, const ge_p3 *A, const unsigned char *b); +void ge_madd(ge_p1p1 *r, const ge_p3 *p, const ge_precomp *q); +void ge_msub(ge_p1p1 *r, const ge_p3 *p, const ge_precomp *q); +void ge_scalarmult_base(ge_p3 *h, const unsigned char *a); + +void ge_p1p1_to_p2(ge_p2 *r, const ge_p1p1 *p); +void ge_p1p1_to_p3(ge_p3 *r, const ge_p1p1 *p); +void ge_p2_0(ge_p2 *h); +void ge_p2_dbl(ge_p1p1 *r, const ge_p2 *p); +void ge_p3_0(ge_p3 *h); +void ge_p3_dbl(ge_p1p1 *r, const ge_p3 *p); +void ge_p3_to_cached(ge_cached *r, const ge_p3 *p); +void ge_p3_to_p2(ge_p2 *r, const ge_p3 *p); + +#endif diff --git a/core/eos/services/crypto/src/precomp_data.h b/core/eos/services/crypto/src/precomp_data.h new file mode 100644 index 00000000..ff23986c --- /dev/null +++ b/core/eos/services/crypto/src/precomp_data.h @@ -0,0 +1,1391 @@ +static const ge_precomp Bi[8] = { + { + { 25967493, -14356035, 29566456, 3660896, -12694345, 4014787, 27544626, -11754271, -6079156, 2047605 }, + { -12545711, 934262, -2722910, 3049990, -727428, 9406986, 12720692, 5043384, 19500929, -15469378 }, + { -8738181, 4489570, 9688441, -14785194, 10184609, -12363380, 29287919, 11864899, -24514362, -4438546 }, + }, + { + { 15636291, -9688557, 24204773, -7912398, 616977, -16685262, 27787600, -14772189, 28944400, -1550024 }, + { 16568933, 4717097, -11556148, -1102322, 15682896, -11807043, 16354577, -11775962, 7689662, 11199574 }, + { 30464156, -5976125, -11779434, -15670865, 23220365, 15915852, 7512774, 10017326, -17749093, -9920357 }, + }, + { + { 10861363, 11473154, 27284546, 1981175, -30064349, 12577861, 32867885, 14515107, -15438304, 10819380 }, + { 4708026, 6336745, 20377586, 9066809, -11272109, 6594696, -25653668, 12483688, -12668491, 5581306 }, + { 19563160, 16186464, -29386857, 4097519, 10237984, -4348115, 28542350, 13850243, -23678021, -15815942 }, + }, + { + { 5153746, 9909285, 1723747, -2777874, 30523605, 5516873, 19480852, 5230134, -23952439, -15175766 }, + { -30269007, -3463509, 7665486, 10083793, 28475525, 1649722, 20654025, 16520125, 30598449, 7715701 }, + { 28881845, 14381568, 9657904, 3680757, -20181635, 7843316, -31400660, 1370708, 29794553, -1409300 }, + }, + { + { -22518993, -6692182, 14201702, -8745502, -23510406, 8844726, 18474211, -1361450, -13062696, 13821877 }, + { -6455177, -7839871, 3374702, -4740862, -27098617, -10571707, 31655028, -7212327, 18853322, -14220951 }, + { 4566830, -12963868, -28974889, -12240689, -7602672, -2830569, -8514358, -10431137, 2207753, -3209784 }, + }, + { + { -25154831, -4185821, 29681144, 7868801, -6854661, -9423865, -12437364, -663000, -31111463, -16132436 }, + { 25576264, -2703214, 7349804, -11814844, 16472782, 9300885, 3844789, 15725684, 171356, 6466918 }, + { 23103977, 13316479, 9739013, -16149481, 817875, -15038942, 8965339, -14088058, -30714912, 16193877 }, + }, + { + { -33521811, 3180713, -2394130, 14003687, -16903474, -16270840, 17238398, 4729455, -18074513, 9256800 }, + { -25182317, -4174131, 32336398, 5036987, -21236817, 11360617, 22616405, 9761698, -19827198, 630305 }, + { -13720693, 2639453, -24237460, -7406481, 9494427, -5774029, -6554551, -15960994, -2449256, -14291300 }, + }, + { + { -3151181, -5046075, 9282714, 6866145, -31907062, -863023, -18940575, 15033784, 25105118, -7894876 }, + { -24326370, 15950226, -31801215, -14592823, -11662737, -5090925, 1573892, -2625887, 2198790, -15804619 }, + { -3099351, 10324967, -2241613, 7453183, -5446979, -2735503, -13812022, -16236442, -32461234, -12290683 }, + }, +}; + + +/* base[i][j] = (j+1)*256^i*B */ +static const ge_precomp base[32][8] = { + { + { + { 25967493, -14356035, 29566456, 3660896, -12694345, 4014787, 27544626, -11754271, -6079156, 2047605 }, + { -12545711, 934262, -2722910, 3049990, -727428, 9406986, 12720692, 5043384, 19500929, -15469378 }, + { -8738181, 4489570, 9688441, -14785194, 10184609, -12363380, 29287919, 11864899, -24514362, -4438546 }, + }, + { + { -12815894, -12976347, -21581243, 11784320, -25355658, -2750717, -11717903, -3814571, -358445, -10211303 }, + { -21703237, 6903825, 27185491, 6451973, -29577724, -9554005, -15616551, 11189268, -26829678, -5319081 }, + { 26966642, 11152617, 32442495, 15396054, 14353839, -12752335, -3128826, -9541118, -15472047, -4166697 }, + }, + { + { 15636291, -9688557, 24204773, -7912398, 616977, -16685262, 27787600, -14772189, 28944400, -1550024 }, + { 16568933, 4717097, -11556148, -1102322, 15682896, -11807043, 16354577, -11775962, 7689662, 11199574 }, + { 30464156, -5976125, -11779434, -15670865, 23220365, 15915852, 7512774, 10017326, -17749093, -9920357 }, + }, + { + { -17036878, 13921892, 10945806, -6033431, 27105052, -16084379, -28926210, 15006023, 3284568, -6276540 }, + { 23599295, -8306047, -11193664, -7687416, 13236774, 10506355, 7464579, 9656445, 13059162, 10374397 }, + { 7798556, 16710257, 3033922, 2874086, 28997861, 2835604, 32406664, -3839045, -641708, -101325 }, + }, + { + { 10861363, 11473154, 27284546, 1981175, -30064349, 12577861, 32867885, 14515107, -15438304, 10819380 }, + { 4708026, 6336745, 20377586, 9066809, -11272109, 6594696, -25653668, 12483688, -12668491, 5581306 }, + { 19563160, 16186464, -29386857, 4097519, 10237984, -4348115, 28542350, 13850243, -23678021, -15815942 }, + }, + { + { -15371964, -12862754, 32573250, 4720197, -26436522, 5875511, -19188627, -15224819, -9818940, -12085777 }, + { -8549212, 109983, 15149363, 2178705, 22900618, 4543417, 3044240, -15689887, 1762328, 14866737 }, + { -18199695, -15951423, -10473290, 1707278, -17185920, 3916101, -28236412, 3959421, 27914454, 4383652 }, + }, + { + { 5153746, 9909285, 1723747, -2777874, 30523605, 5516873, 19480852, 5230134, -23952439, -15175766 }, + { -30269007, -3463509, 7665486, 10083793, 28475525, 1649722, 20654025, 16520125, 30598449, 7715701 }, + { 28881845, 14381568, 9657904, 3680757, -20181635, 7843316, -31400660, 1370708, 29794553, -1409300 }, + }, + { + { 14499471, -2729599, -33191113, -4254652, 28494862, 14271267, 30290735, 10876454, -33154098, 2381726 }, + { -7195431, -2655363, -14730155, 462251, -27724326, 3941372, -6236617, 3696005, -32300832, 15351955 }, + { 27431194, 8222322, 16448760, -3907995, -18707002, 11938355, -32961401, -2970515, 29551813, 10109425 }, + }, + }, + { + { + { -13657040, -13155431, -31283750, 11777098, 21447386, 6519384, -2378284, -1627556, 10092783, -4764171 }, + { 27939166, 14210322, 4677035, 16277044, -22964462, -12398139, -32508754, 12005538, -17810127, 12803510 }, + { 17228999, -15661624, -1233527, 300140, -1224870, -11714777, 30364213, -9038194, 18016357, 4397660 }, + }, + { + { -10958843, -7690207, 4776341, -14954238, 27850028, -15602212, -26619106, 14544525, -17477504, 982639 }, + { 29253598, 15796703, -2863982, -9908884, 10057023, 3163536, 7332899, -4120128, -21047696, 9934963 }, + { 5793303, 16271923, -24131614, -10116404, 29188560, 1206517, -14747930, 4559895, -30123922, -10897950 }, + }, + { + { -27643952, -11493006, 16282657, -11036493, 28414021, -15012264, 24191034, 4541697, -13338309, 5500568 }, + { 12650548, -1497113, 9052871, 11355358, -17680037, -8400164, -17430592, 12264343, 10874051, 13524335 }, + { 25556948, -3045990, 714651, 2510400, 23394682, -10415330, 33119038, 5080568, -22528059, 5376628 }, + }, + { + { -26088264, -4011052, -17013699, -3537628, -6726793, 1920897, -22321305, -9447443, 4535768, 1569007 }, + { -2255422, 14606630, -21692440, -8039818, 28430649, 8775819, -30494562, 3044290, 31848280, 12543772 }, + { -22028579, 2943893, -31857513, 6777306, 13784462, -4292203, -27377195, -2062731, 7718482, 14474653 }, + }, + { + { 2385315, 2454213, -22631320, 46603, -4437935, -15680415, 656965, -7236665, 24316168, -5253567 }, + { 13741529, 10911568, -33233417, -8603737, -20177830, -1033297, 33040651, -13424532, -20729456, 8321686 }, + { 21060490, -2212744, 15712757, -4336099, 1639040, 10656336, 23845965, -11874838, -9984458, 608372 }, + }, + { + { -13672732, -15087586, -10889693, -7557059, -6036909, 11305547, 1123968, -6780577, 27229399, 23887 }, + { -23244140, -294205, -11744728, 14712571, -29465699, -2029617, 12797024, -6440308, -1633405, 16678954 }, + { -29500620, 4770662, -16054387, 14001338, 7830047, 9564805, -1508144, -4795045, -17169265, 4904953 }, + }, + { + { 24059557, 14617003, 19037157, -15039908, 19766093, -14906429, 5169211, 16191880, 2128236, -4326833 }, + { -16981152, 4124966, -8540610, -10653797, 30336522, -14105247, -29806336, 916033, -6882542, -2986532 }, + { -22630907, 12419372, -7134229, -7473371, -16478904, 16739175, 285431, 2763829, 15736322, 4143876 }, + }, + { + { 2379352, 11839345, -4110402, -5988665, 11274298, 794957, 212801, -14594663, 23527084, -16458268 }, + { 33431127, -11130478, -17838966, -15626900, 8909499, 8376530, -32625340, 4087881, -15188911, -14416214 }, + { 1767683, 7197987, -13205226, -2022635, -13091350, 448826, 5799055, 4357868, -4774191, -16323038 }, + }, + }, + { + { + { 6721966, 13833823, -23523388, -1551314, 26354293, -11863321, 23365147, -3949732, 7390890, 2759800 }, + { 4409041, 2052381, 23373853, 10530217, 7676779, -12885954, 21302353, -4264057, 1244380, -12919645 }, + { -4421239, 7169619, 4982368, -2957590, 30256825, -2777540, 14086413, 9208236, 15886429, 16489664 }, + }, + { + { 1996075, 10375649, 14346367, 13311202, -6874135, -16438411, -13693198, 398369, -30606455, -712933 }, + { -25307465, 9795880, -2777414, 14878809, -33531835, 14780363, 13348553, 12076947, -30836462, 5113182 }, + { -17770784, 11797796, 31950843, 13929123, -25888302, 12288344, -30341101, -7336386, 13847711, 5387222 }, + }, + { + { -18582163, -3416217, 17824843, -2340966, 22744343, -10442611, 8763061, 3617786, -19600662, 10370991 }, + { 20246567, -14369378, 22358229, -543712, 18507283, -10413996, 14554437, -8746092, 32232924, 16763880 }, + { 9648505, 10094563, 26416693, 14745928, -30374318, -6472621, 11094161, 15689506, 3140038, -16510092 }, + }, + { + { -16160072, 5472695, 31895588, 4744994, 8823515, 10365685, -27224800, 9448613, -28774454, 366295 }, + { 19153450, 11523972, -11096490, -6503142, -24647631, 5420647, 28344573, 8041113, 719605, 11671788 }, + { 8678025, 2694440, -6808014, 2517372, 4964326, 11152271, -15432916, -15266516, 27000813, -10195553 }, + }, + { + { -15157904, 7134312, 8639287, -2814877, -7235688, 10421742, 564065, 5336097, 6750977, -14521026 }, + { 11836410, -3979488, 26297894, 16080799, 23455045, 15735944, 1695823, -8819122, 8169720, 16220347 }, + { -18115838, 8653647, 17578566, -6092619, -8025777, -16012763, -11144307, -2627664, -5990708, -14166033 }, + }, + { + { -23308498, -10968312, 15213228, -10081214, -30853605, -11050004, 27884329, 2847284, 2655861, 1738395 }, + { -27537433, -14253021, -25336301, -8002780, -9370762, 8129821, 21651608, -3239336, -19087449, -11005278 }, + { 1533110, 3437855, 23735889, 459276, 29970501, 11335377, 26030092, 5821408, 10478196, 8544890 }, + }, + { + { 32173121, -16129311, 24896207, 3921497, 22579056, -3410854, 19270449, 12217473, 17789017, -3395995 }, + { -30552961, -2228401, -15578829, -10147201, 13243889, 517024, 15479401, -3853233, 30460520, 1052596 }, + { -11614875, 13323618, 32618793, 8175907, -15230173, 12596687, 27491595, -4612359, 3179268, -9478891 }, + }, + { + { 31947069, -14366651, -4640583, -15339921, -15125977, -6039709, -14756777, -16411740, 19072640, -9511060 }, + { 11685058, 11822410, 3158003, -13952594, 33402194, -4165066, 5977896, -5215017, 473099, 5040608 }, + { -20290863, 8198642, -27410132, 11602123, 1290375, -2799760, 28326862, 1721092, -19558642, -3131606 }, + }, + }, + { + { + { 7881532, 10687937, 7578723, 7738378, -18951012, -2553952, 21820786, 8076149, -27868496, 11538389 }, + { -19935666, 3899861, 18283497, -6801568, -15728660, -11249211, 8754525, 7446702, -5676054, 5797016 }, + { -11295600, -3793569, -15782110, -7964573, 12708869, -8456199, 2014099, -9050574, -2369172, -5877341 }, + }, + { + { -22472376, -11568741, -27682020, 1146375, 18956691, 16640559, 1192730, -3714199, 15123619, 10811505 }, + { 14352098, -3419715, -18942044, 10822655, 32750596, 4699007, -70363, 15776356, -28886779, -11974553 }, + { -28241164, -8072475, -4978962, -5315317, 29416931, 1847569, -20654173, -16484855, 4714547, -9600655 }, + }, + { + { 15200332, 8368572, 19679101, 15970074, -31872674, 1959451, 24611599, -4543832, -11745876, 12340220 }, + { 12876937, -10480056, 33134381, 6590940, -6307776, 14872440, 9613953, 8241152, 15370987, 9608631 }, + { -4143277, -12014408, 8446281, -391603, 4407738, 13629032, -7724868, 15866074, -28210621, -8814099 }, + }, + { + { 26660628, -15677655, 8393734, 358047, -7401291, 992988, -23904233, 858697, 20571223, 8420556 }, + { 14620715, 13067227, -15447274, 8264467, 14106269, 15080814, 33531827, 12516406, -21574435, -12476749 }, + { 236881, 10476226, 57258, -14677024, 6472998, 2466984, 17258519, 7256740, 8791136, 15069930 }, + }, + { + { 1276410, -9371918, 22949635, -16322807, -23493039, -5702186, 14711875, 4874229, -30663140, -2331391 }, + { 5855666, 4990204, -13711848, 7294284, -7804282, 1924647, -1423175, -7912378, -33069337, 9234253 }, + { 20590503, -9018988, 31529744, -7352666, -2706834, 10650548, 31559055, -11609587, 18979186, 13396066 }, + }, + { + { 24474287, 4968103, 22267082, 4407354, 24063882, -8325180, -18816887, 13594782, 33514650, 7021958 }, + { -11566906, -6565505, -21365085, 15928892, -26158305, 4315421, -25948728, -3916677, -21480480, 12868082 }, + { -28635013, 13504661, 19988037, -2132761, 21078225, 6443208, -21446107, 2244500, -12455797, -8089383 }, + }, + { + { -30595528, 13793479, -5852820, 319136, -25723172, -6263899, 33086546, 8957937, -15233648, 5540521 }, + { -11630176, -11503902, -8119500, -7643073, 2620056, 1022908, -23710744, -1568984, -16128528, -14962807 }, + { 23152971, 775386, 27395463, 14006635, -9701118, 4649512, 1689819, 892185, -11513277, -15205948 }, + }, + { + { 9770129, 9586738, 26496094, 4324120, 1556511, -3550024, 27453819, 4763127, -19179614, 5867134 }, + { -32765025, 1927590, 31726409, -4753295, 23962434, -16019500, 27846559, 5931263, -29749703, -16108455 }, + { 27461885, -2977536, 22380810, 1815854, -23033753, -3031938, 7283490, -15148073, -19526700, 7734629 }, + }, + }, + { + { + { -8010264, -9590817, -11120403, 6196038, 29344158, -13430885, 7585295, -3176626, 18549497, 15302069 }, + { -32658337, -6171222, -7672793, -11051681, 6258878, 13504381, 10458790, -6418461, -8872242, 8424746 }, + { 24687205, 8613276, -30667046, -3233545, 1863892, -1830544, 19206234, 7134917, -11284482, -828919 }, + }, + { + { 11334899, -9218022, 8025293, 12707519, 17523892, -10476071, 10243738, -14685461, -5066034, 16498837 }, + { 8911542, 6887158, -9584260, -6958590, 11145641, -9543680, 17303925, -14124238, 6536641, 10543906 }, + { -28946384, 15479763, -17466835, 568876, -1497683, 11223454, -2669190, -16625574, -27235709, 8876771 }, + }, + { + { -25742899, -12566864, -15649966, -846607, -33026686, -796288, -33481822, 15824474, -604426, -9039817 }, + { 10330056, 70051, 7957388, -9002667, 9764902, 15609756, 27698697, -4890037, 1657394, 3084098 }, + { 10477963, -7470260, 12119566, -13250805, 29016247, -5365589, 31280319, 14396151, -30233575, 15272409 }, + }, + { + { -12288309, 3169463, 28813183, 16658753, 25116432, -5630466, -25173957, -12636138, -25014757, 1950504 }, + { -26180358, 9489187, 11053416, -14746161, -31053720, 5825630, -8384306, -8767532, 15341279, 8373727 }, + { 28685821, 7759505, -14378516, -12002860, -31971820, 4079242, 298136, -10232602, -2878207, 15190420 }, + }, + { + { -32932876, 13806336, -14337485, -15794431, -24004620, 10940928, 8669718, 2742393, -26033313, -6875003 }, + { -1580388, -11729417, -25979658, -11445023, -17411874, -10912854, 9291594, -16247779, -12154742, 6048605 }, + { -30305315, 14843444, 1539301, 11864366, 20201677, 1900163, 13934231, 5128323, 11213262, 9168384 }, + }, + { + { -26280513, 11007847, 19408960, -940758, -18592965, -4328580, -5088060, -11105150, 20470157, -16398701 }, + { -23136053, 9282192, 14855179, -15390078, -7362815, -14408560, -22783952, 14461608, 14042978, 5230683 }, + { 29969567, -2741594, -16711867, -8552442, 9175486, -2468974, 21556951, 3506042, -5933891, -12449708 }, + }, + { + { -3144746, 8744661, 19704003, 4581278, -20430686, 6830683, -21284170, 8971513, -28539189, 15326563 }, + { -19464629, 10110288, -17262528, -3503892, -23500387, 1355669, -15523050, 15300988, -20514118, 9168260 }, + { -5353335, 4488613, -23803248, 16314347, 7780487, -15638939, -28948358, 9601605, 33087103, -9011387 }, + }, + { + { -19443170, -15512900, -20797467, -12445323, -29824447, 10229461, -27444329, -15000531, -5996870, 15664672 }, + { 23294591, -16632613, -22650781, -8470978, 27844204, 11461195, 13099750, -2460356, 18151676, 13417686 }, + { -24722913, -4176517, -31150679, 5988919, -26858785, 6685065, 1661597, -12551441, 15271676, -15452665 }, + }, + }, + { + { + { 11433042, -13228665, 8239631, -5279517, -1985436, -725718, -18698764, 2167544, -6921301, -13440182 }, + { -31436171, 15575146, 30436815, 12192228, -22463353, 9395379, -9917708, -8638997, 12215110, 12028277 }, + { 14098400, 6555944, 23007258, 5757252, -15427832, -12950502, 30123440, 4617780, -16900089, -655628 }, + }, + { + { -4026201, -15240835, 11893168, 13718664, -14809462, 1847385, -15819999, 10154009, 23973261, -12684474 }, + { -26531820, -3695990, -1908898, 2534301, -31870557, -16550355, 18341390, -11419951, 32013174, -10103539 }, + { -25479301, 10876443, -11771086, -14625140, -12369567, 1838104, 21911214, 6354752, 4425632, -837822 }, + }, + { + { -10433389, -14612966, 22229858, -3091047, -13191166, 776729, -17415375, -12020462, 4725005, 14044970 }, + { 19268650, -7304421, 1555349, 8692754, -21474059, -9910664, 6347390, -1411784, -19522291, -16109756 }, + { -24864089, 12986008, -10898878, -5558584, -11312371, -148526, 19541418, 8180106, 9282262, 10282508 }, + }, + { + { -26205082, 4428547, -8661196, -13194263, 4098402, -14165257, 15522535, 8372215, 5542595, -10702683 }, + { -10562541, 14895633, 26814552, -16673850, -17480754, -2489360, -2781891, 6993761, -18093885, 10114655 }, + { -20107055, -929418, 31422704, 10427861, -7110749, 6150669, -29091755, -11529146, 25953725, -106158 }, + }, + { + { -4234397, -8039292, -9119125, 3046000, 2101609, -12607294, 19390020, 6094296, -3315279, 12831125 }, + { -15998678, 7578152, 5310217, 14408357, -33548620, -224739, 31575954, 6326196, 7381791, -2421839 }, + { -20902779, 3296811, 24736065, -16328389, 18374254, 7318640, 6295303, 8082724, -15362489, 12339664 }, + }, + { + { 27724736, 2291157, 6088201, -14184798, 1792727, 5857634, 13848414, 15768922, 25091167, 14856294 }, + { -18866652, 8331043, 24373479, 8541013, -701998, -9269457, 12927300, -12695493, -22182473, -9012899 }, + { -11423429, -5421590, 11632845, 3405020, 30536730, -11674039, -27260765, 13866390, 30146206, 9142070 }, + }, + { + { 3924129, -15307516, -13817122, -10054960, 12291820, -668366, -27702774, 9326384, -8237858, 4171294 }, + { -15921940, 16037937, 6713787, 16606682, -21612135, 2790944, 26396185, 3731949, 345228, -5462949 }, + { -21327538, 13448259, 25284571, 1143661, 20614966, -8849387, 2031539, -12391231, -16253183, -13582083 }, + }, + { + { 31016211, -16722429, 26371392, -14451233, -5027349, 14854137, 17477601, 3842657, 28012650, -16405420 }, + { -5075835, 9368966, -8562079, -4600902, -15249953, 6970560, -9189873, 16292057, -8867157, 3507940 }, + { 29439664, 3537914, 23333589, 6997794, -17555561, -11018068, -15209202, -15051267, -9164929, 6580396 }, + }, + }, + { + { + { -12185861, -7679788, 16438269, 10826160, -8696817, -6235611, 17860444, -9273846, -2095802, 9304567 }, + { 20714564, -4336911, 29088195, 7406487, 11426967, -5095705, 14792667, -14608617, 5289421, -477127 }, + { -16665533, -10650790, -6160345, -13305760, 9192020, -1802462, 17271490, 12349094, 26939669, -3752294 }, + }, + { + { -12889898, 9373458, 31595848, 16374215, 21471720, 13221525, -27283495, -12348559, -3698806, 117887 }, + { 22263325, -6560050, 3984570, -11174646, -15114008, -566785, 28311253, 5358056, -23319780, 541964 }, + { 16259219, 3261970, 2309254, -15534474, -16885711, -4581916, 24134070, -16705829, -13337066, -13552195 }, + }, + { + { 9378160, -13140186, -22845982, -12745264, 28198281, -7244098, -2399684, -717351, 690426, 14876244 }, + { 24977353, -314384, -8223969, -13465086, 28432343, -1176353, -13068804, -12297348, -22380984, 6618999 }, + { -1538174, 11685646, 12944378, 13682314, -24389511, -14413193, 8044829, -13817328, 32239829, -5652762 }, + }, + { + { -18603066, 4762990, -926250, 8885304, -28412480, -3187315, 9781647, -10350059, 32779359, 5095274 }, + { -33008130, -5214506, -32264887, -3685216, 9460461, -9327423, -24601656, 14506724, 21639561, -2630236 }, + { -16400943, -13112215, 25239338, 15531969, 3987758, -4499318, -1289502, -6863535, 17874574, 558605 }, + }, + { + { -13600129, 10240081, 9171883, 16131053, -20869254, 9599700, 33499487, 5080151, 2085892, 5119761 }, + { -22205145, -2519528, -16381601, 414691, -25019550, 2170430, 30634760, -8363614, -31999993, -5759884 }, + { -6845704, 15791202, 8550074, -1312654, 29928809, -12092256, 27534430, -7192145, -22351378, 12961482 }, + }, + { + { -24492060, -9570771, 10368194, 11582341, -23397293, -2245287, 16533930, 8206996, -30194652, -5159638 }, + { -11121496, -3382234, 2307366, 6362031, -135455, 8868177, -16835630, 7031275, 7589640, 8945490 }, + { -32152748, 8917967, 6661220, -11677616, -1192060, -15793393, 7251489, -11182180, 24099109, -14456170 }, + }, + { + { 5019558, -7907470, 4244127, -14714356, -26933272, 6453165, -19118182, -13289025, -6231896, -10280736 }, + { 10853594, 10721687, 26480089, 5861829, -22995819, 1972175, -1866647, -10557898, -3363451, -6441124 }, + { -17002408, 5906790, 221599, -6563147, 7828208, -13248918, 24362661, -2008168, -13866408, 7421392 }, + }, + { + { 8139927, -6546497, 32257646, -5890546, 30375719, 1886181, -21175108, 15441252, 28826358, -4123029 }, + { 6267086, 9695052, 7709135, -16603597, -32869068, -1886135, 14795160, -7840124, 13746021, -1742048 }, + { 28584902, 7787108, -6732942, -15050729, 22846041, -7571236, -3181936, -363524, 4771362, -8419958 }, + }, + }, + { + { + { 24949256, 6376279, -27466481, -8174608, -18646154, -9930606, 33543569, -12141695, 3569627, 11342593 }, + { 26514989, 4740088, 27912651, 3697550, 19331575, -11472339, 6809886, 4608608, 7325975, -14801071 }, + { -11618399, -14554430, -24321212, 7655128, -1369274, 5214312, -27400540, 10258390, -17646694, -8186692 }, + }, + { + { 11431204, 15823007, 26570245, 14329124, 18029990, 4796082, -31446179, 15580664, 9280358, -3973687 }, + { -160783, -10326257, -22855316, -4304997, -20861367, -13621002, -32810901, -11181622, -15545091, 4387441 }, + { -20799378, 12194512, 3937617, -5805892, -27154820, 9340370, -24513992, 8548137, 20617071, -7482001 }, + }, + { + { -938825, -3930586, -8714311, 16124718, 24603125, -6225393, -13775352, -11875822, 24345683, 10325460 }, + { -19855277, -1568885, -22202708, 8714034, 14007766, 6928528, 16318175, -1010689, 4766743, 3552007 }, + { -21751364, -16730916, 1351763, -803421, -4009670, 3950935, 3217514, 14481909, 10988822, -3994762 }, + }, + { + { 15564307, -14311570, 3101243, 5684148, 30446780, -8051356, 12677127, -6505343, -8295852, 13296005 }, + { -9442290, 6624296, -30298964, -11913677, -4670981, -2057379, 31521204, 9614054, -30000824, 12074674 }, + { 4771191, -135239, 14290749, -13089852, 27992298, 14998318, -1413936, -1556716, 29832613, -16391035 }, + }, + { + { 7064884, -7541174, -19161962, -5067537, -18891269, -2912736, 25825242, 5293297, -27122660, 13101590 }, + { -2298563, 2439670, -7466610, 1719965, -27267541, -16328445, 32512469, -5317593, -30356070, -4190957 }, + { -30006540, 10162316, -33180176, 3981723, -16482138, -13070044, 14413974, 9515896, 19568978, 9628812 }, + }, + { + { 33053803, 199357, 15894591, 1583059, 27380243, -4580435, -17838894, -6106839, -6291786, 3437740 }, + { -18978877, 3884493, 19469877, 12726490, 15913552, 13614290, -22961733, 70104, 7463304, 4176122 }, + { -27124001, 10659917, 11482427, -16070381, 12771467, -6635117, -32719404, -5322751, 24216882, 5944158 }, + }, + { + { 8894125, 7450974, -2664149, -9765752, -28080517, -12389115, 19345746, 14680796, 11632993, 5847885 }, + { 26942781, -2315317, 9129564, -4906607, 26024105, 11769399, -11518837, 6367194, -9727230, 4782140 }, + { 19916461, -4828410, -22910704, -11414391, 25606324, -5972441, 33253853, 8220911, 6358847, -1873857 }, + }, + { + { 801428, -2081702, 16569428, 11065167, 29875704, 96627, 7908388, -4480480, -13538503, 1387155 }, + { 19646058, 5720633, -11416706, 12814209, 11607948, 12749789, 14147075, 15156355, -21866831, 11835260 }, + { 19299512, 1155910, 28703737, 14890794, 2925026, 7269399, 26121523, 15467869, -26560550, 5052483 }, + }, + }, + { + { + { -3017432, 10058206, 1980837, 3964243, 22160966, 12322533, -6431123, -12618185, 12228557, -7003677 }, + { 32944382, 14922211, -22844894, 5188528, 21913450, -8719943, 4001465, 13238564, -6114803, 8653815 }, + { 22865569, -4652735, 27603668, -12545395, 14348958, 8234005, 24808405, 5719875, 28483275, 2841751 }, + }, + { + { -16420968, -1113305, -327719, -12107856, 21886282, -15552774, -1887966, -315658, 19932058, -12739203 }, + { -11656086, 10087521, -8864888, -5536143, -19278573, -3055912, 3999228, 13239134, -4777469, -13910208 }, + { 1382174, -11694719, 17266790, 9194690, -13324356, 9720081, 20403944, 11284705, -14013818, 3093230 }, + }, + { + { 16650921, -11037932, -1064178, 1570629, -8329746, 7352753, -302424, 16271225, -24049421, -6691850 }, + { -21911077, -5927941, -4611316, -5560156, -31744103, -10785293, 24123614, 15193618, -21652117, -16739389 }, + { -9935934, -4289447, -25279823, 4372842, 2087473, 10399484, 31870908, 14690798, 17361620, 11864968 }, + }, + { + { -11307610, 6210372, 13206574, 5806320, -29017692, -13967200, -12331205, -7486601, -25578460, -16240689 }, + { 14668462, -12270235, 26039039, 15305210, 25515617, 4542480, 10453892, 6577524, 9145645, -6443880 }, + { 5974874, 3053895, -9433049, -10385191, -31865124, 3225009, -7972642, 3936128, -5652273, -3050304 }, + }, + { + { 30625386, -4729400, -25555961, -12792866, -20484575, 7695099, 17097188, -16303496, -27999779, 1803632 }, + { -3553091, 9865099, -5228566, 4272701, -5673832, -16689700, 14911344, 12196514, -21405489, 7047412 }, + { 20093277, 9920966, -11138194, -5343857, 13161587, 12044805, -32856851, 4124601, -32343828, -10257566 }, + }, + { + { -20788824, 14084654, -13531713, 7842147, 19119038, -13822605, 4752377, -8714640, -21679658, 2288038 }, + { -26819236, -3283715, 29965059, 3039786, -14473765, 2540457, 29457502, 14625692, -24819617, 12570232 }, + { -1063558, -11551823, 16920318, 12494842, 1278292, -5869109, -21159943, -3498680, -11974704, 4724943 }, + }, + { + { 17960970, -11775534, -4140968, -9702530, -8876562, -1410617, -12907383, -8659932, -29576300, 1903856 }, + { 23134274, -14279132, -10681997, -1611936, 20684485, 15770816, -12989750, 3190296, 26955097, 14109738 }, + { 15308788, 5320727, -30113809, -14318877, 22902008, 7767164, 29425325, -11277562, 31960942, 11934971 }, + }, + { + { -27395711, 8435796, 4109644, 12222639, -24627868, 14818669, 20638173, 4875028, 10491392, 1379718 }, + { -13159415, 9197841, 3875503, -8936108, -1383712, -5879801, 33518459, 16176658, 21432314, 12180697 }, + { -11787308, 11500838, 13787581, -13832590, -22430679, 10140205, 1465425, 12689540, -10301319, -13872883 }, + }, + }, + { + { + { 5414091, -15386041, -21007664, 9643570, 12834970, 1186149, -2622916, -1342231, 26128231, 6032912 }, + { -26337395, -13766162, 32496025, -13653919, 17847801, -12669156, 3604025, 8316894, -25875034, -10437358 }, + { 3296484, 6223048, 24680646, -12246460, -23052020, 5903205, -8862297, -4639164, 12376617, 3188849 }, + }, + { + { 29190488, -14659046, 27549113, -1183516, 3520066, -10697301, 32049515, -7309113, -16109234, -9852307 }, + { -14744486, -9309156, 735818, -598978, -20407687, -5057904, 25246078, -15795669, 18640741, -960977 }, + { -6928835, -16430795, 10361374, 5642961, 4910474, 12345252, -31638386, -494430, 10530747, 1053335 }, + }, + { + { -29265967, -14186805, -13538216, -12117373, -19457059, -10655384, -31462369, -2948985, 24018831, 15026644 }, + { -22592535, -3145277, -2289276, 5953843, -13440189, 9425631, 25310643, 13003497, -2314791, -15145616 }, + { -27419985, -603321, -8043984, -1669117, -26092265, 13987819, -27297622, 187899, -23166419, -2531735 }, + }, + { + { -21744398, -13810475, 1844840, 5021428, -10434399, -15911473, 9716667, 16266922, -5070217, 726099 }, + { 29370922, -6053998, 7334071, -15342259, 9385287, 2247707, -13661962, -4839461, 30007388, -15823341 }, + { -936379, 16086691, 23751945, -543318, -1167538, -5189036, 9137109, 730663, 9835848, 4555336 }, + }, + { + { -23376435, 1410446, -22253753, -12899614, 30867635, 15826977, 17693930, 544696, -11985298, 12422646 }, + { 31117226, -12215734, -13502838, 6561947, -9876867, -12757670, -5118685, -4096706, 29120153, 13924425 }, + { -17400879, -14233209, 19675799, -2734756, -11006962, -5858820, -9383939, -11317700, 7240931, -237388 }, + }, + { + { -31361739, -11346780, -15007447, -5856218, -22453340, -12152771, 1222336, 4389483, 3293637, -15551743 }, + { -16684801, -14444245, 11038544, 11054958, -13801175, -3338533, -24319580, 7733547, 12796905, -6335822 }, + { -8759414, -10817836, -25418864, 10783769, -30615557, -9746811, -28253339, 3647836, 3222231, -11160462 }, + }, + { + { 18606113, 1693100, -25448386, -15170272, 4112353, 10045021, 23603893, -2048234, -7550776, 2484985 }, + { 9255317, -3131197, -12156162, -1004256, 13098013, -9214866, 16377220, -2102812, -19802075, -3034702 }, + { -22729289, 7496160, -5742199, 11329249, 19991973, -3347502, -31718148, 9936966, -30097688, -10618797 }, + }, + { + { 21878590, -5001297, 4338336, 13643897, -3036865, 13160960, 19708896, 5415497, -7360503, -4109293 }, + { 27736861, 10103576, 12500508, 8502413, -3413016, -9633558, 10436918, -1550276, -23659143, -8132100 }, + { 19492550, -12104365, -29681976, -852630, -3208171, 12403437, 30066266, 8367329, 13243957, 8709688 }, + }, + }, + { + { + { 12015105, 2801261, 28198131, 10151021, 24818120, -4743133, -11194191, -5645734, 5150968, 7274186 }, + { 2831366, -12492146, 1478975, 6122054, 23825128, -12733586, 31097299, 6083058, 31021603, -9793610 }, + { -2529932, -2229646, 445613, 10720828, -13849527, -11505937, -23507731, 16354465, 15067285, -14147707 }, + }, + { + { 7840942, 14037873, -33364863, 15934016, -728213, -3642706, 21403988, 1057586, -19379462, -12403220 }, + { 915865, -16469274, 15608285, -8789130, -24357026, 6060030, -17371319, 8410997, -7220461, 16527025 }, + { 32922597, -556987, 20336074, -16184568, 10903705, -5384487, 16957574, 52992, 23834301, 6588044 }, + }, + { + { 32752030, 11232950, 3381995, -8714866, 22652988, -10744103, 17159699, 16689107, -20314580, -1305992 }, + { -4689649, 9166776, -25710296, -10847306, 11576752, 12733943, 7924251, -2752281, 1976123, -7249027 }, + { 21251222, 16309901, -2983015, -6783122, 30810597, 12967303, 156041, -3371252, 12331345, -8237197 }, + }, + { + { 8651614, -4477032, -16085636, -4996994, 13002507, 2950805, 29054427, -5106970, 10008136, -4667901 }, + { 31486080, 15114593, -14261250, 12951354, 14369431, -7387845, 16347321, -13662089, 8684155, -10532952 }, + { 19443825, 11385320, 24468943, -9659068, -23919258, 2187569, -26263207, -6086921, 31316348, 14219878 }, + }, + { + { -28594490, 1193785, 32245219, 11392485, 31092169, 15722801, 27146014, 6992409, 29126555, 9207390 }, + { 32382935, 1110093, 18477781, 11028262, -27411763, -7548111, -4980517, 10843782, -7957600, -14435730 }, + { 2814918, 7836403, 27519878, -7868156, -20894015, -11553689, -21494559, 8550130, 28346258, 1994730 }, + }, + { + { -19578299, 8085545, -14000519, -3948622, 2785838, -16231307, -19516951, 7174894, 22628102, 8115180 }, + { -30405132, 955511, -11133838, -15078069, -32447087, -13278079, -25651578, 3317160, -9943017, 930272 }, + { -15303681, -6833769, 28856490, 1357446, 23421993, 1057177, 24091212, -1388970, -22765376, -10650715 }, + }, + { + { -22751231, -5303997, -12907607, -12768866, -15811511, -7797053, -14839018, -16554220, -1867018, 8398970 }, + { -31969310, 2106403, -4736360, 1362501, 12813763, 16200670, 22981545, -6291273, 18009408, -15772772 }, + { -17220923, -9545221, -27784654, 14166835, 29815394, 7444469, 29551787, -3727419, 19288549, 1325865 }, + }, + { + { 15100157, -15835752, -23923978, -1005098, -26450192, 15509408, 12376730, -3479146, 33166107, -8042750 }, + { 20909231, 13023121, -9209752, 16251778, -5778415, -8094914, 12412151, 10018715, 2213263, -13878373 }, + { 32529814, -11074689, 30361439, -16689753, -9135940, 1513226, 22922121, 6382134, -5766928, 8371348 }, + }, + }, + { + { + { 9923462, 11271500, 12616794, 3544722, -29998368, -1721626, 12891687, -8193132, -26442943, 10486144 }, + { -22597207, -7012665, 8587003, -8257861, 4084309, -12970062, 361726, 2610596, -23921530, -11455195 }, + { 5408411, -1136691, -4969122, 10561668, 24145918, 14240566, 31319731, -4235541, 19985175, -3436086 }, + }, + { + { -13994457, 16616821, 14549246, 3341099, 32155958, 13648976, -17577068, 8849297, 65030, 8370684 }, + { -8320926, -12049626, 31204563, 5839400, -20627288, -1057277, -19442942, 6922164, 12743482, -9800518 }, + { -2361371, 12678785, 28815050, 4759974, -23893047, 4884717, 23783145, 11038569, 18800704, 255233 }, + }, + { + { -5269658, -1773886, 13957886, 7990715, 23132995, 728773, 13393847, 9066957, 19258688, -14753793 }, + { -2936654, -10827535, -10432089, 14516793, -3640786, 4372541, -31934921, 2209390, -1524053, 2055794 }, + { 580882, 16705327, 5468415, -2683018, -30926419, -14696000, -7203346, -8994389, -30021019, 7394435 }, + }, + { + { 23838809, 1822728, -15738443, 15242727, 8318092, -3733104, -21672180, -3492205, -4821741, 14799921 }, + { 13345610, 9759151, 3371034, -16137791, 16353039, 8577942, 31129804, 13496856, -9056018, 7402518 }, + { 2286874, -4435931, -20042458, -2008336, -13696227, 5038122, 11006906, -15760352, 8205061, 1607563 }, + }, + { + { 14414086, -8002132, 3331830, -3208217, 22249151, -5594188, 18364661, -2906958, 30019587, -9029278 }, + { -27688051, 1585953, -10775053, 931069, -29120221, -11002319, -14410829, 12029093, 9944378, 8024 }, + { 4368715, -3709630, 29874200, -15022983, -20230386, -11410704, -16114594, -999085, -8142388, 5640030 }, + }, + { + { 10299610, 13746483, 11661824, 16234854, 7630238, 5998374, 9809887, -16694564, 15219798, -14327783 }, + { 27425505, -5719081, 3055006, 10660664, 23458024, 595578, -15398605, -1173195, -18342183, 9742717 }, + { 6744077, 2427284, 26042789, 2720740, -847906, 1118974, 32324614, 7406442, 12420155, 1994844 }, + }, + { + { 14012521, -5024720, -18384453, -9578469, -26485342, -3936439, -13033478, -10909803, 24319929, -6446333 }, + { 16412690, -4507367, 10772641, 15929391, -17068788, -4658621, 10555945, -10484049, -30102368, -4739048 }, + { 22397382, -7767684, -9293161, -12792868, 17166287, -9755136, -27333065, 6199366, 21880021, -12250760 }, + }, + { + { -4283307, 5368523, -31117018, 8163389, -30323063, 3209128, 16557151, 8890729, 8840445, 4957760 }, + { -15447727, 709327, -6919446, -10870178, -29777922, 6522332, -21720181, 12130072, -14796503, 5005757 }, + { -2114751, -14308128, 23019042, 15765735, -25269683, 6002752, 10183197, -13239326, -16395286, -2176112 }, + }, + }, + { + { + { -19025756, 1632005, 13466291, -7995100, -23640451, 16573537, -32013908, -3057104, 22208662, 2000468 }, + { 3065073, -1412761, -25598674, -361432, -17683065, -5703415, -8164212, 11248527, -3691214, -7414184 }, + { 10379208, -6045554, 8877319, 1473647, -29291284, -12507580, 16690915, 2553332, -3132688, 16400289 }, + }, + { + { 15716668, 1254266, -18472690, 7446274, -8448918, 6344164, -22097271, -7285580, 26894937, 9132066 }, + { 24158887, 12938817, 11085297, -8177598, -28063478, -4457083, -30576463, 64452, -6817084, -2692882 }, + { 13488534, 7794716, 22236231, 5989356, 25426474, -12578208, 2350710, -3418511, -4688006, 2364226 }, + }, + { + { 16335052, 9132434, 25640582, 6678888, 1725628, 8517937, -11807024, -11697457, 15445875, -7798101 }, + { 29004207, -7867081, 28661402, -640412, -12794003, -7943086, 31863255, -4135540, -278050, -15759279 }, + { -6122061, -14866665, -28614905, 14569919, -10857999, -3591829, 10343412, -6976290, -29828287, -10815811 }, + }, + { + { 27081650, 3463984, 14099042, -4517604, 1616303, -6205604, 29542636, 15372179, 17293797, 960709 }, + { 20263915, 11434237, -5765435, 11236810, 13505955, -10857102, -16111345, 6493122, -19384511, 7639714 }, + { -2830798, -14839232, 25403038, -8215196, -8317012, -16173699, 18006287, -16043750, 29994677, -15808121 }, + }, + { + { 9769828, 5202651, -24157398, -13631392, -28051003, -11561624, -24613141, -13860782, -31184575, 709464 }, + { 12286395, 13076066, -21775189, -1176622, -25003198, 4057652, -32018128, -8890874, 16102007, 13205847 }, + { 13733362, 5599946, 10557076, 3195751, -5557991, 8536970, -25540170, 8525972, 10151379, 10394400 }, + }, + { + { 4024660, -16137551, 22436262, 12276534, -9099015, -2686099, 19698229, 11743039, -33302334, 8934414 }, + { -15879800, -4525240, -8580747, -2934061, 14634845, -698278, -9449077, 3137094, -11536886, 11721158 }, + { 17555939, -5013938, 8268606, 2331751, -22738815, 9761013, 9319229, 8835153, -9205489, -1280045 }, + }, + { + { -461409, -7830014, 20614118, 16688288, -7514766, -4807119, 22300304, 505429, 6108462, -6183415 }, + { -5070281, 12367917, -30663534, 3234473, 32617080, -8422642, 29880583, -13483331, -26898490, -7867459 }, + { -31975283, 5726539, 26934134, 10237677, -3173717, -605053, 24199304, 3795095, 7592688, -14992079 }, + }, + { + { 21594432, -14964228, 17466408, -4077222, 32537084, 2739898, 6407723, 12018833, -28256052, 4298412 }, + { -20650503, -11961496, -27236275, 570498, 3767144, -1717540, 13891942, -1569194, 13717174, 10805743 }, + { -14676630, -15644296, 15287174, 11927123, 24177847, -8175568, -796431, 14860609, -26938930, -5863836 }, + }, + }, + { + { + { 12962541, 5311799, -10060768, 11658280, 18855286, -7954201, 13286263, -12808704, -4381056, 9882022 }, + { 18512079, 11319350, -20123124, 15090309, 18818594, 5271736, -22727904, 3666879, -23967430, -3299429 }, + { -6789020, -3146043, 16192429, 13241070, 15898607, -14206114, -10084880, -6661110, -2403099, 5276065 }, + }, + { + { 30169808, -5317648, 26306206, -11750859, 27814964, 7069267, 7152851, 3684982, 1449224, 13082861 }, + { 10342826, 3098505, 2119311, 193222, 25702612, 12233820, 23697382, 15056736, -21016438, -8202000 }, + { -33150110, 3261608, 22745853, 7948688, 19370557, -15177665, -26171976, 6482814, -10300080, -11060101 }, + }, + { + { 32869458, -5408545, 25609743, 15678670, -10687769, -15471071, 26112421, 2521008, -22664288, 6904815 }, + { 29506923, 4457497, 3377935, -9796444, -30510046, 12935080, 1561737, 3841096, -29003639, -6657642 }, + { 10340844, -6630377, -18656632, -2278430, 12621151, -13339055, 30878497, -11824370, -25584551, 5181966 }, + }, + { + { 25940115, -12658025, 17324188, -10307374, -8671468, 15029094, 24396252, -16450922, -2322852, -12388574 }, + { -21765684, 9916823, -1300409, 4079498, -1028346, 11909559, 1782390, 12641087, 20603771, -6561742 }, + { -18882287, -11673380, 24849422, 11501709, 13161720, -4768874, 1925523, 11914390, 4662781, 7820689 }, + }, + { + { 12241050, -425982, 8132691, 9393934, 32846760, -1599620, 29749456, 12172924, 16136752, 15264020 }, + { -10349955, -14680563, -8211979, 2330220, -17662549, -14545780, 10658213, 6671822, 19012087, 3772772 }, + { 3753511, -3421066, 10617074, 2028709, 14841030, -6721664, 28718732, -15762884, 20527771, 12988982 }, + }, + { + { -14822485, -5797269, -3707987, 12689773, -898983, -10914866, -24183046, -10564943, 3299665, -12424953 }, + { -16777703, -15253301, -9642417, 4978983, 3308785, 8755439, 6943197, 6461331, -25583147, 8991218 }, + { -17226263, 1816362, -1673288, -6086439, 31783888, -8175991, -32948145, 7417950, -30242287, 1507265 }, + }, + { + { 29692663, 6829891, -10498800, 4334896, 20945975, -11906496, -28887608, 8209391, 14606362, -10647073 }, + { -3481570, 8707081, 32188102, 5672294, 22096700, 1711240, -33020695, 9761487, 4170404, -2085325 }, + { -11587470, 14855945, -4127778, -1531857, -26649089, 15084046, 22186522, 16002000, -14276837, -8400798 }, + }, + { + { -4811456, 13761029, -31703877, -2483919, -3312471, 7869047, -7113572, -9620092, 13240845, 10965870 }, + { -7742563, -8256762, -14768334, -13656260, -23232383, 12387166, 4498947, 14147411, 29514390, 4302863 }, + { -13413405, -12407859, 20757302, -13801832, 14785143, 8976368, -5061276, -2144373, 17846988, -13971927 }, + }, + }, + { + { + { -2244452, -754728, -4597030, -1066309, -6247172, 1455299, -21647728, -9214789, -5222701, 12650267 }, + { -9906797, -16070310, 21134160, 12198166, -27064575, 708126, 387813, 13770293, -19134326, 10958663 }, + { 22470984, 12369526, 23446014, -5441109, -21520802, -9698723, -11772496, -11574455, -25083830, 4271862 }, + }, + { + { -25169565, -10053642, -19909332, 15361595, -5984358, 2159192, 75375, -4278529, -32526221, 8469673 }, + { 15854970, 4148314, -8893890, 7259002, 11666551, 13824734, -30531198, 2697372, 24154791, -9460943 }, + { 15446137, -15806644, 29759747, 14019369, 30811221, -9610191, -31582008, 12840104, 24913809, 9815020 }, + }, + { + { -4709286, -5614269, -31841498, -12288893, -14443537, 10799414, -9103676, 13438769, 18735128, 9466238 }, + { 11933045, 9281483, 5081055, -5183824, -2628162, -4905629, -7727821, -10896103, -22728655, 16199064 }, + { 14576810, 379472, -26786533, -8317236, -29426508, -10812974, -102766, 1876699, 30801119, 2164795 }, + }, + { + { 15995086, 3199873, 13672555, 13712240, -19378835, -4647646, -13081610, -15496269, -13492807, 1268052 }, + { -10290614, -3659039, -3286592, 10948818, 23037027, 3794475, -3470338, -12600221, -17055369, 3565904 }, + { 29210088, -9419337, -5919792, -4952785, 10834811, -13327726, -16512102, -10820713, -27162222, -14030531 }, + }, + { + { -13161890, 15508588, 16663704, -8156150, -28349942, 9019123, -29183421, -3769423, 2244111, -14001979 }, + { -5152875, -3800936, -9306475, -6071583, 16243069, 14684434, -25673088, -16180800, 13491506, 4641841 }, + { 10813417, 643330, -19188515, -728916, 30292062, -16600078, 27548447, -7721242, 14476989, -12767431 }, + }, + { + { 10292079, 9984945, 6481436, 8279905, -7251514, 7032743, 27282937, -1644259, -27912810, 12651324 }, + { -31185513, -813383, 22271204, 11835308, 10201545, 15351028, 17099662, 3988035, 21721536, -3148940 }, + { 10202177, -6545839, -31373232, -9574638, -32150642, -8119683, -12906320, 3852694, 13216206, 14842320 }, + }, + { + { -15815640, -10601066, -6538952, -7258995, -6984659, -6581778, -31500847, 13765824, -27434397, 9900184 }, + { 14465505, -13833331, -32133984, -14738873, -27443187, 12990492, 33046193, 15796406, -7051866, -8040114 }, + { 30924417, -8279620, 6359016, -12816335, 16508377, 9071735, -25488601, 15413635, 9524356, -7018878 }, + }, + { + { 12274201, -13175547, 32627641, -1785326, 6736625, 13267305, 5237659, -5109483, 15663516, 4035784 }, + { -2951309, 8903985, 17349946, 601635, -16432815, -4612556, -13732739, -15889334, -22258478, 4659091 }, + { -16916263, -4952973, -30393711, -15158821, 20774812, 15897498, 5736189, 15026997, -2178256, -13455585 }, + }, + }, + { + { + { -8858980, -2219056, 28571666, -10155518, -474467, -10105698, -3801496, 278095, 23440562, -290208 }, + { 10226241, -5928702, 15139956, 120818, -14867693, 5218603, 32937275, 11551483, -16571960, -7442864 }, + { 17932739, -12437276, -24039557, 10749060, 11316803, 7535897, 22503767, 5561594, -3646624, 3898661 }, + }, + { + { 7749907, -969567, -16339731, -16464, -25018111, 15122143, -1573531, 7152530, 21831162, 1245233 }, + { 26958459, -14658026, 4314586, 8346991, -5677764, 11960072, -32589295, -620035, -30402091, -16716212 }, + { -12165896, 9166947, 33491384, 13673479, 29787085, 13096535, 6280834, 14587357, -22338025, 13987525 }, + }, + { + { -24349909, 7778775, 21116000, 15572597, -4833266, -5357778, -4300898, -5124639, -7469781, -2858068 }, + { 9681908, -6737123, -31951644, 13591838, -6883821, 386950, 31622781, 6439245, -14581012, 4091397 }, + { -8426427, 1470727, -28109679, -1596990, 3978627, -5123623, -19622683, 12092163, 29077877, -14741988 }, + }, + { + { 5269168, -6859726, -13230211, -8020715, 25932563, 1763552, -5606110, -5505881, -20017847, 2357889 }, + { 32264008, -15407652, -5387735, -1160093, -2091322, -3946900, 23104804, -12869908, 5727338, 189038 }, + { 14609123, -8954470, -6000566, -16622781, -14577387, -7743898, -26745169, 10942115, -25888931, -14884697 }, + }, + { + { 20513500, 5557931, -15604613, 7829531, 26413943, -2019404, -21378968, 7471781, 13913677, -5137875 }, + { -25574376, 11967826, 29233242, 12948236, -6754465, 4713227, -8940970, 14059180, 12878652, 8511905 }, + { -25656801, 3393631, -2955415, -7075526, -2250709, 9366908, -30223418, 6812974, 5568676, -3127656 }, + }, + { + { 11630004, 12144454, 2116339, 13606037, 27378885, 15676917, -17408753, -13504373, -14395196, 8070818 }, + { 27117696, -10007378, -31282771, -5570088, 1127282, 12772488, -29845906, 10483306, -11552749, -1028714 }, + { 10637467, -5688064, 5674781, 1072708, -26343588, -6982302, -1683975, 9177853, -27493162, 15431203 }, + }, + { + { 20525145, 10892566, -12742472, 12779443, -29493034, 16150075, -28240519, 14943142, -15056790, -7935931 }, + { -30024462, 5626926, -551567, -9981087, 753598, 11981191, 25244767, -3239766, -3356550, 9594024 }, + { -23752644, 2636870, -5163910, -10103818, 585134, 7877383, 11345683, -6492290, 13352335, -10977084 }, + }, + { + { -1931799, -5407458, 3304649, -12884869, 17015806, -4877091, -29783850, -7752482, -13215537, -319204 }, + { 20239939, 6607058, 6203985, 3483793, -18386976, -779229, -20723742, 15077870, -22750759, 14523817 }, + { 27406042, -6041657, 27423596, -4497394, 4996214, 10002360, -28842031, -4545494, -30172742, -4805667 }, + }, + }, + { + { + { 11374242, 12660715, 17861383, -12540833, 10935568, 1099227, -13886076, -9091740, -27727044, 11358504 }, + { -12730809, 10311867, 1510375, 10778093, -2119455, -9145702, 32676003, 11149336, -26123651, 4985768 }, + { -19096303, 341147, -6197485, -239033, 15756973, -8796662, -983043, 13794114, -19414307, -15621255 }, + }, + { + { 6490081, 11940286, 25495923, -7726360, 8668373, -8751316, 3367603, 6970005, -1691065, -9004790 }, + { 1656497, 13457317, 15370807, 6364910, 13605745, 8362338, -19174622, -5475723, -16796596, -5031438 }, + { -22273315, -13524424, -64685, -4334223, -18605636, -10921968, -20571065, -7007978, -99853, -10237333 }, + }, + { + { 17747465, 10039260, 19368299, -4050591, -20630635, -16041286, 31992683, -15857976, -29260363, -5511971 }, + { 31932027, -4986141, -19612382, 16366580, 22023614, 88450, 11371999, -3744247, 4882242, -10626905 }, + { 29796507, 37186, 19818052, 10115756, -11829032, 3352736, 18551198, 3272828, -5190932, -4162409 }, + }, + { + { 12501286, 4044383, -8612957, -13392385, -32430052, 5136599, -19230378, -3529697, 330070, -3659409 }, + { 6384877, 2899513, 17807477, 7663917, -2358888, 12363165, 25366522, -8573892, -271295, 12071499 }, + { -8365515, -4042521, 25133448, -4517355, -6211027, 2265927, -32769618, 1936675, -5159697, 3829363 }, + }, + { + { 28425966, -5835433, -577090, -4697198, -14217555, 6870930, 7921550, -6567787, 26333140, 14267664 }, + { -11067219, 11871231, 27385719, -10559544, -4585914, -11189312, 10004786, -8709488, -21761224, 8930324 }, + { -21197785, -16396035, 25654216, -1725397, 12282012, 11008919, 1541940, 4757911, -26491501, -16408940 }, + }, + { + { 13537262, -7759490, -20604840, 10961927, -5922820, -13218065, -13156584, 6217254, -15943699, 13814990 }, + { -17422573, 15157790, 18705543, 29619, 24409717, -260476, 27361681, 9257833, -1956526, -1776914 }, + { -25045300, -10191966, 15366585, 15166509, -13105086, 8423556, -29171540, 12361135, -18685978, 4578290 }, + }, + { + { 24579768, 3711570, 1342322, -11180126, -27005135, 14124956, -22544529, 14074919, 21964432, 8235257 }, + { -6528613, -2411497, 9442966, -5925588, 12025640, -1487420, -2981514, -1669206, 13006806, 2355433 }, + { -16304899, -13605259, -6632427, -5142349, 16974359, -10911083, 27202044, 1719366, 1141648, -12796236 }, + }, + { + { -12863944, -13219986, -8318266, -11018091, -6810145, -4843894, 13475066, -3133972, 32674895, 13715045 }, + { 11423335, -5468059, 32344216, 8962751, 24989809, 9241752, -13265253, 16086212, -28740881, -15642093 }, + { -1409668, 12530728, -6368726, 10847387, 19531186, -14132160, -11709148, 7791794, -27245943, 4383347 }, + }, + }, + { + { + { -28970898, 5271447, -1266009, -9736989, -12455236, 16732599, -4862407, -4906449, 27193557, 6245191 }, + { -15193956, 5362278, -1783893, 2695834, 4960227, 12840725, 23061898, 3260492, 22510453, 8577507 }, + { -12632451, 11257346, -32692994, 13548177, -721004, 10879011, 31168030, 13952092, -29571492, -3635906 }, + }, + { + { 3877321, -9572739, 32416692, 5405324, -11004407, -13656635, 3759769, 11935320, 5611860, 8164018 }, + { -16275802, 14667797, 15906460, 12155291, -22111149, -9039718, 32003002, -8832289, 5773085, -8422109 }, + { -23788118, -8254300, 1950875, 8937633, 18686727, 16459170, -905725, 12376320, 31632953, 190926 }, + }, + { + { -24593607, -16138885, -8423991, 13378746, 14162407, 6901328, -8288749, 4508564, -25341555, -3627528 }, + { 8884438, -5884009, 6023974, 10104341, -6881569, -4941533, 18722941, -14786005, -1672488, 827625 }, + { -32720583, -16289296, -32503547, 7101210, 13354605, 2659080, -1800575, -14108036, -24878478, 1541286 }, + }, + { + { 2901347, -1117687, 3880376, -10059388, -17620940, -3612781, -21802117, -3567481, 20456845, -1885033 }, + { 27019610, 12299467, -13658288, -1603234, -12861660, -4861471, -19540150, -5016058, 29439641, 15138866 }, + { 21536104, -6626420, -32447818, -10690208, -22408077, 5175814, -5420040, -16361163, 7779328, 109896 }, + }, + { + { 30279744, 14648750, -8044871, 6425558, 13639621, -743509, 28698390, 12180118, 23177719, -554075 }, + { 26572847, 3405927, -31701700, 12890905, -19265668, 5335866, -6493768, 2378492, 4439158, -13279347 }, + { -22716706, 3489070, -9225266, -332753, 18875722, -1140095, 14819434, -12731527, -17717757, -5461437 }, + }, + { + { -5056483, 16566551, 15953661, 3767752, -10436499, 15627060, -820954, 2177225, 8550082, -15114165 }, + { -18473302, 16596775, -381660, 15663611, 22860960, 15585581, -27844109, -3582739, -23260460, -8428588 }, + { -32480551, 15707275, -8205912, -5652081, 29464558, 2713815, -22725137, 15860482, -21902570, 1494193 }, + }, + { + { -19562091, -14087393, -25583872, -9299552, 13127842, 759709, 21923482, 16529112, 8742704, 12967017 }, + { -28464899, 1553205, 32536856, -10473729, -24691605, -406174, -8914625, -2933896, -29903758, 15553883 }, + { 21877909, 3230008, 9881174, 10539357, -4797115, 2841332, 11543572, 14513274, 19375923, -12647961 }, + }, + { + { 8832269, -14495485, 13253511, 5137575, 5037871, 4078777, 24880818, -6222716, 2862653, 9455043 }, + { 29306751, 5123106, 20245049, -14149889, 9592566, 8447059, -2077124, -2990080, 15511449, 4789663 }, + { -20679756, 7004547, 8824831, -9434977, -4045704, -3750736, -5754762, 108893, 23513200, 16652362 }, + }, + }, + { + { + { -33256173, 4144782, -4476029, -6579123, 10770039, -7155542, -6650416, -12936300, -18319198, 10212860 }, + { 2756081, 8598110, 7383731, -6859892, 22312759, -1105012, 21179801, 2600940, -9988298, -12506466 }, + { -24645692, 13317462, -30449259, -15653928, 21365574, -10869657, 11344424, 864440, -2499677, -16710063 }, + }, + { + { -26432803, 6148329, -17184412, -14474154, 18782929, -275997, -22561534, 211300, 2719757, 4940997 }, + { -1323882, 3911313, -6948744, 14759765, -30027150, 7851207, 21690126, 8518463, 26699843, 5276295 }, + { -13149873, -6429067, 9396249, 365013, 24703301, -10488939, 1321586, 149635, -15452774, 7159369 }, + }, + { + { 9987780, -3404759, 17507962, 9505530, 9731535, -2165514, 22356009, 8312176, 22477218, -8403385 }, + { 18155857, -16504990, 19744716, 9006923, 15154154, -10538976, 24256460, -4864995, -22548173, 9334109 }, + { 2986088, -4911893, 10776628, -3473844, 10620590, -7083203, -21413845, 14253545, -22587149, 536906 }, + }, + { + { 4377756, 8115836, 24567078, 15495314, 11625074, 13064599, 7390551, 10589625, 10838060, -15420424 }, + { -19342404, 867880, 9277171, -3218459, -14431572, -1986443, 19295826, -15796950, 6378260, 699185 }, + { 7895026, 4057113, -7081772, -13077756, -17886831, -323126, -716039, 15693155, -5045064, -13373962 }, + }, + { + { -7737563, -5869402, -14566319, -7406919, 11385654, 13201616, 31730678, -10962840, -3918636, -9669325 }, + { 10188286, -15770834, -7336361, 13427543, 22223443, 14896287, 30743455, 7116568, -21786507, 5427593 }, + { 696102, 13206899, 27047647, -10632082, 15285305, -9853179, 10798490, -4578720, 19236243, 12477404 }, + }, + { + { -11229439, 11243796, -17054270, -8040865, -788228, -8167967, -3897669, 11180504, -23169516, 7733644 }, + { 17800790, -14036179, -27000429, -11766671, 23887827, 3149671, 23466177, -10538171, 10322027, 15313801 }, + { 26246234, 11968874, 32263343, -5468728, 6830755, -13323031, -15794704, -101982, -24449242, 10890804 }, + }, + { + { -31365647, 10271363, -12660625, -6267268, 16690207, -13062544, -14982212, 16484931, 25180797, -5334884 }, + { -586574, 10376444, -32586414, -11286356, 19801893, 10997610, 2276632, 9482883, 316878, 13820577 }, + { -9882808, -4510367, -2115506, 16457136, -11100081, 11674996, 30756178, -7515054, 30696930, -3712849 }, + }, + { + { 32988917, -9603412, 12499366, 7910787, -10617257, -11931514, -7342816, -9985397, -32349517, 7392473 }, + { -8855661, 15927861, 9866406, -3649411, -2396914, -16655781, -30409476, -9134995, 25112947, -2926644 }, + { -2504044, -436966, 25621774, -5678772, 15085042, -5479877, -24884878, -13526194, 5537438, -13914319 }, + }, + }, + { + { + { -11225584, 2320285, -9584280, 10149187, -33444663, 5808648, -14876251, -1729667, 31234590, 6090599 }, + { -9633316, 116426, 26083934, 2897444, -6364437, -2688086, 609721, 15878753, -6970405, -9034768 }, + { -27757857, 247744, -15194774, -9002551, 23288161, -10011936, -23869595, 6503646, 20650474, 1804084 }, + }, + { + { -27589786, 15456424, 8972517, 8469608, 15640622, 4439847, 3121995, -10329713, 27842616, -202328 }, + { -15306973, 2839644, 22530074, 10026331, 4602058, 5048462, 28248656, 5031932, -11375082, 12714369 }, + { 20807691, -7270825, 29286141, 11421711, -27876523, -13868230, -21227475, 1035546, -19733229, 12796920 }, + }, + { + { 12076899, -14301286, -8785001, -11848922, -25012791, 16400684, -17591495, -12899438, 3480665, -15182815 }, + { -32361549, 5457597, 28548107, 7833186, 7303070, -11953545, -24363064, -15921875, -33374054, 2771025 }, + { -21389266, 421932, 26597266, 6860826, 22486084, -6737172, -17137485, -4210226, -24552282, 15673397 }, + }, + { + { -20184622, 2338216, 19788685, -9620956, -4001265, -8740893, -20271184, 4733254, 3727144, -12934448 }, + { 6120119, 814863, -11794402, -622716, 6812205, -15747771, 2019594, 7975683, 31123697, -10958981 }, + { 30069250, -11435332, 30434654, 2958439, 18399564, -976289, 12296869, 9204260, -16432438, 9648165 }, + }, + { + { 32705432, -1550977, 30705658, 7451065, -11805606, 9631813, 3305266, 5248604, -26008332, -11377501 }, + { 17219865, 2375039, -31570947, -5575615, -19459679, 9219903, 294711, 15298639, 2662509, -16297073 }, + { -1172927, -7558695, -4366770, -4287744, -21346413, -8434326, 32087529, -1222777, 32247248, -14389861 }, + }, + { + { 14312628, 1221556, 17395390, -8700143, -4945741, -8684635, -28197744, -9637817, -16027623, -13378845 }, + { -1428825, -9678990, -9235681, 6549687, -7383069, -468664, 23046502, 9803137, 17597934, 2346211 }, + { 18510800, 15337574, 26171504, 981392, -22241552, 7827556, -23491134, -11323352, 3059833, -11782870 }, + }, + { + { 10141598, 6082907, 17829293, -1947643, 9830092, 13613136, -25556636, -5544586, -33502212, 3592096 }, + { 33114168, -15889352, -26525686, -13343397, 33076705, 8716171, 1151462, 1521897, -982665, -6837803 }, + { -32939165, -4255815, 23947181, -324178, -33072974, -12305637, -16637686, 3891704, 26353178, 693168 }, + }, + { + { 30374239, 1595580, -16884039, 13186931, 4600344, 406904, 9585294, -400668, 31375464, 14369965 }, + { -14370654, -7772529, 1510301, 6434173, -18784789, -6262728, 32732230, -13108839, 17901441, 16011505 }, + { 18171223, -11934626, -12500402, 15197122, -11038147, -15230035, -19172240, -16046376, 8764035, 12309598 }, + }, + }, + { + { + { 5975908, -5243188, -19459362, -9681747, -11541277, 14015782, -23665757, 1228319, 17544096, -10593782 }, + { 5811932, -1715293, 3442887, -2269310, -18367348, -8359541, -18044043, -15410127, -5565381, 12348900 }, + { -31399660, 11407555, 25755363, 6891399, -3256938, 14872274, -24849353, 8141295, -10632534, -585479 }, + }, + { + { -12675304, 694026, -5076145, 13300344, 14015258, -14451394, -9698672, -11329050, 30944593, 1130208 }, + { 8247766, -6710942, -26562381, -7709309, -14401939, -14648910, 4652152, 2488540, 23550156, -271232 }, + { 17294316, -3788438, 7026748, 15626851, 22990044, 113481, 2267737, -5908146, -408818, -137719 }, + }, + { + { 16091085, -16253926, 18599252, 7340678, 2137637, -1221657, -3364161, 14550936, 3260525, -7166271 }, + { -4910104, -13332887, 18550887, 10864893, -16459325, -7291596, -23028869, -13204905, -12748722, 2701326 }, + { -8574695, 16099415, 4629974, -16340524, -20786213, -6005432, -10018363, 9276971, 11329923, 1862132 }, + }, + { + { 14763076, -15903608, -30918270, 3689867, 3511892, 10313526, -21951088, 12219231, -9037963, -940300 }, + { 8894987, -3446094, 6150753, 3013931, 301220, 15693451, -31981216, -2909717, -15438168, 11595570 }, + { 15214962, 3537601, -26238722, -14058872, 4418657, -15230761, 13947276, 10730794, -13489462, -4363670 }, + }, + { + { -2538306, 7682793, 32759013, 263109, -29984731, -7955452, -22332124, -10188635, 977108, 699994 }, + { -12466472, 4195084, -9211532, 550904, -15565337, 12917920, 19118110, -439841, -30534533, -14337913 }, + { 31788461, -14507657, 4799989, 7372237, 8808585, -14747943, 9408237, -10051775, 12493932, -5409317 }, + }, + { + { -25680606, 5260744, -19235809, -6284470, -3695942, 16566087, 27218280, 2607121, 29375955, 6024730 }, + { 842132, -2794693, -4763381, -8722815, 26332018, -12405641, 11831880, 6985184, -9940361, 2854096 }, + { -4847262, -7969331, 2516242, -5847713, 9695691, -7221186, 16512645, 960770, 12121869, 16648078 }, + }, + { + { -15218652, 14667096, -13336229, 2013717, 30598287, -464137, -31504922, -7882064, 20237806, 2838411 }, + { -19288047, 4453152, 15298546, -16178388, 22115043, -15972604, 12544294, -13470457, 1068881, -12499905 }, + { -9558883, -16518835, 33238498, 13506958, 30505848, -1114596, -8486907, -2630053, 12521378, 4845654 }, + }, + { + { -28198521, 10744108, -2958380, 10199664, 7759311, -13088600, 3409348, -873400, -6482306, -12885870 }, + { -23561822, 6230156, -20382013, 10655314, -24040585, -11621172, 10477734, -1240216, -3113227, 13974498 }, + { 12966261, 15550616, -32038948, -1615346, 21025980, -629444, 5642325, 7188737, 18895762, 12629579 }, + }, + }, + { + { + { 14741879, -14946887, 22177208, -11721237, 1279741, 8058600, 11758140, 789443, 32195181, 3895677 }, + { 10758205, 15755439, -4509950, 9243698, -4879422, 6879879, -2204575, -3566119, -8982069, 4429647 }, + { -2453894, 15725973, -20436342, -10410672, -5803908, -11040220, -7135870, -11642895, 18047436, -15281743 }, + }, + { + { -25173001, -11307165, 29759956, 11776784, -22262383, -15820455, 10993114, -12850837, -17620701, -9408468 }, + { 21987233, 700364, -24505048, 14972008, -7774265, -5718395, 32155026, 2581431, -29958985, 8773375 }, + { -25568350, 454463, -13211935, 16126715, 25240068, 8594567, 20656846, 12017935, -7874389, -13920155 }, + }, + { + { 6028182, 6263078, -31011806, -11301710, -818919, 2461772, -31841174, -5468042, -1721788, -2776725 }, + { -12278994, 16624277, 987579, -5922598, 32908203, 1248608, 7719845, -4166698, 28408820, 6816612 }, + { -10358094, -8237829, 19549651, -12169222, 22082623, 16147817, 20613181, 13982702, -10339570, 5067943 }, + }, + { + { -30505967, -3821767, 12074681, 13582412, -19877972, 2443951, -19719286, 12746132, 5331210, -10105944 }, + { 30528811, 3601899, -1957090, 4619785, -27361822, -15436388, 24180793, -12570394, 27679908, -1648928 }, + { 9402404, -13957065, 32834043, 10838634, -26580150, -13237195, 26653274, -8685565, 22611444, -12715406 }, + }, + { + { 22190590, 1118029, 22736441, 15130463, -30460692, -5991321, 19189625, -4648942, 4854859, 6622139 }, + { -8310738, -2953450, -8262579, -3388049, -10401731, -271929, 13424426, -3567227, 26404409, 13001963 }, + { -31241838, -15415700, -2994250, 8939346, 11562230, -12840670, -26064365, -11621720, -15405155, 11020693 }, + }, + { + { 1866042, -7949489, -7898649, -10301010, 12483315, 13477547, 3175636, -12424163, 28761762, 1406734 }, + { -448555, -1777666, 13018551, 3194501, -9580420, -11161737, 24760585, -4347088, 25577411, -13378680 }, + { -24290378, 4759345, -690653, -1852816, 2066747, 10693769, -29595790, 9884936, -9368926, 4745410 }, + }, + { + { -9141284, 6049714, -19531061, -4341411, -31260798, 9944276, -15462008, -11311852, 10931924, -11931931 }, + { -16561513, 14112680, -8012645, 4817318, -8040464, -11414606, -22853429, 10856641, -20470770, 13434654 }, + { 22759489, -10073434, -16766264, -1871422, 13637442, -10168091, 1765144, -12654326, 28445307, -5364710 }, + }, + { + { 29875063, 12493613, 2795536, -3786330, 1710620, 15181182, -10195717, -8788675, 9074234, 1167180 }, + { -26205683, 11014233, -9842651, -2635485, -26908120, 7532294, -18716888, -9535498, 3843903, 9367684 }, + { -10969595, -6403711, 9591134, 9582310, 11349256, 108879, 16235123, 8601684, -139197, 4242895 }, + }, + }, + { + { + { 22092954, -13191123, -2042793, -11968512, 32186753, -11517388, -6574341, 2470660, -27417366, 16625501 }, + { -11057722, 3042016, 13770083, -9257922, 584236, -544855, -7770857, 2602725, -27351616, 14247413 }, + { 6314175, -10264892, -32772502, 15957557, -10157730, 168750, -8618807, 14290061, 27108877, -1180880 }, + }, + { + { -8586597, -7170966, 13241782, 10960156, -32991015, -13794596, 33547976, -11058889, -27148451, 981874 }, + { 22833440, 9293594, -32649448, -13618667, -9136966, 14756819, -22928859, -13970780, -10479804, -16197962 }, + { -7768587, 3326786, -28111797, 10783824, 19178761, 14905060, 22680049, 13906969, -15933690, 3797899 }, + }, + { + { 21721356, -4212746, -12206123, 9310182, -3882239, -13653110, 23740224, -2709232, 20491983, -8042152 }, + { 9209270, -15135055, -13256557, -6167798, -731016, 15289673, 25947805, 15286587, 30997318, -6703063 }, + { 7392032, 16618386, 23946583, -8039892, -13265164, -1533858, -14197445, -2321576, 17649998, -250080 }, + }, + { + { -9301088, -14193827, 30609526, -3049543, -25175069, -1283752, -15241566, -9525724, -2233253, 7662146 }, + { -17558673, 1763594, -33114336, 15908610, -30040870, -12174295, 7335080, -8472199, -3174674, 3440183 }, + { -19889700, -5977008, -24111293, -9688870, 10799743, -16571957, 40450, -4431835, 4862400, 1133 }, + }, + { + { -32856209, -7873957, -5422389, 14860950, -16319031, 7956142, 7258061, 311861, -30594991, -7379421 }, + { -3773428, -1565936, 28985340, 7499440, 24445838, 9325937, 29727763, 16527196, 18278453, 15405622 }, + { -4381906, 8508652, -19898366, -3674424, -5984453, 15149970, -13313598, 843523, -21875062, 13626197 }, + }, + { + { 2281448, -13487055, -10915418, -2609910, 1879358, 16164207, -10783882, 3953792, 13340839, 15928663 }, + { 31727126, -7179855, -18437503, -8283652, 2875793, -16390330, -25269894, -7014826, -23452306, 5964753 }, + { 4100420, -5959452, -17179337, 6017714, -18705837, 12227141, -26684835, 11344144, 2538215, -7570755 }, + }, + { + { -9433605, 6123113, 11159803, -2156608, 30016280, 14966241, -20474983, 1485421, -629256, -15958862 }, + { -26804558, 4260919, 11851389, 9658551, -32017107, 16367492, -20205425, -13191288, 11659922, -11115118 }, + { 26180396, 10015009, -30844224, -8581293, 5418197, 9480663, 2231568, -10170080, 33100372, -1306171 }, + }, + { + { 15121113, -5201871, -10389905, 15427821, -27509937, -15992507, 21670947, 4486675, -5931810, -14466380 }, + { 16166486, -9483733, -11104130, 6023908, -31926798, -1364923, 2340060, -16254968, -10735770, -10039824 }, + { 28042865, -3557089, -12126526, 12259706, -3717498, -6945899, 6766453, -8689599, 18036436, 5803270 }, + }, + }, + { + { + { -817581, 6763912, 11803561, 1585585, 10958447, -2671165, 23855391, 4598332, -6159431, -14117438 }, + { -31031306, -14256194, 17332029, -2383520, 31312682, -5967183, 696309, 50292, -20095739, 11763584 }, + { -594563, -2514283, -32234153, 12643980, 12650761, 14811489, 665117, -12613632, -19773211, -10713562 }, + }, + { + { 30464590, -11262872, -4127476, -12734478, 19835327, -7105613, -24396175, 2075773, -17020157, 992471 }, + { 18357185, -6994433, 7766382, 16342475, -29324918, 411174, 14578841, 8080033, -11574335, -10601610 }, + { 19598397, 10334610, 12555054, 2555664, 18821899, -10339780, 21873263, 16014234, 26224780, 16452269 }, + }, + { + { -30223925, 5145196, 5944548, 16385966, 3976735, 2009897, -11377804, -7618186, -20533829, 3698650 }, + { 14187449, 3448569, -10636236, -10810935, -22663880, -3433596, 7268410, -10890444, 27394301, 12015369 }, + { 19695761, 16087646, 28032085, 12999827, 6817792, 11427614, 20244189, -1312777, -13259127, -3402461 }, + }, + { + { 30860103, 12735208, -1888245, -4699734, -16974906, 2256940, -8166013, 12298312, -8550524, -10393462 }, + { -5719826, -11245325, -1910649, 15569035, 26642876, -7587760, -5789354, -15118654, -4976164, 12651793 }, + { -2848395, 9953421, 11531313, -5282879, 26895123, -12697089, -13118820, -16517902, 9768698, -2533218 }, + }, + { + { -24719459, 1894651, -287698, -4704085, 15348719, -8156530, 32767513, 12765450, 4940095, 10678226 }, + { 18860224, 15980149, -18987240, -1562570, -26233012, -11071856, -7843882, 13944024, -24372348, 16582019 }, + { -15504260, 4970268, -29893044, 4175593, -20993212, -2199756, -11704054, 15444560, -11003761, 7989037 }, + }, + { + { 31490452, 5568061, -2412803, 2182383, -32336847, 4531686, -32078269, 6200206, -19686113, -14800171 }, + { -17308668, -15879940, -31522777, -2831, -32887382, 16375549, 8680158, -16371713, 28550068, -6857132 }, + { -28126887, -5688091, 16837845, -1820458, -6850681, 12700016, -30039981, 4364038, 1155602, 5988841 }, + }, + { + { 21890435, -13272907, -12624011, 12154349, -7831873, 15300496, 23148983, -4470481, 24618407, 8283181 }, + { -33136107, -10512751, 9975416, 6841041, -31559793, 16356536, 3070187, -7025928, 1466169, 10740210 }, + { -1509399, -15488185, -13503385, -10655916, 32799044, 909394, -13938903, -5779719, -32164649, -15327040 }, + }, + { + { 3960823, -14267803, -28026090, -15918051, -19404858, 13146868, 15567327, 951507, -3260321, -573935 }, + { 24740841, 5052253, -30094131, 8961361, 25877428, 6165135, -24368180, 14397372, -7380369, -6144105 }, + { -28888365, 3510803, -28103278, -1158478, -11238128, -10631454, -15441463, -14453128, -1625486, -6494814 }, + }, + }, + { + { + { 793299, -9230478, 8836302, -6235707, -27360908, -2369593, 33152843, -4885251, -9906200, -621852 }, + { 5666233, 525582, 20782575, -8038419, -24538499, 14657740, 16099374, 1468826, -6171428, -15186581 }, + { -4859255, -3779343, -2917758, -6748019, 7778750, 11688288, -30404353, -9871238, -1558923, -9863646 }, + }, + { + { 10896332, -7719704, 824275, 472601, -19460308, 3009587, 25248958, 14783338, -30581476, -15757844 }, + { 10566929, 12612572, -31944212, 11118703, -12633376, 12362879, 21752402, 8822496, 24003793, 14264025 }, + { 27713862, -7355973, -11008240, 9227530, 27050101, 2504721, 23886875, -13117525, 13958495, -5732453 }, + }, + { + { -23481610, 4867226, -27247128, 3900521, 29838369, -8212291, -31889399, -10041781, 7340521, -15410068 }, + { 4646514, -8011124, -22766023, -11532654, 23184553, 8566613, 31366726, -1381061, -15066784, -10375192 }, + { -17270517, 12723032, -16993061, 14878794, 21619651, -6197576, 27584817, 3093888, -8843694, 3849921 }, + }, + { + { -9064912, 2103172, 25561640, -15125738, -5239824, 9582958, 32477045, -9017955, 5002294, -15550259 }, + { -12057553, -11177906, 21115585, -13365155, 8808712, -12030708, 16489530, 13378448, -25845716, 12741426 }, + { -5946367, 10645103, -30911586, 15390284, -3286982, -7118677, 24306472, 15852464, 28834118, -7646072 }, + }, + { + { -17335748, -9107057, -24531279, 9434953, -8472084, -583362, -13090771, 455841, 20461858, 5491305 }, + { 13669248, -16095482, -12481974, -10203039, -14569770, -11893198, -24995986, 11293807, -28588204, -9421832 }, + { 28497928, 6272777, -33022994, 14470570, 8906179, -1225630, 18504674, -14165166, 29867745, -8795943 }, + }, + { + { -16207023, 13517196, -27799630, -13697798, 24009064, -6373891, -6367600, -13175392, 22853429, -4012011 }, + { 24191378, 16712145, -13931797, 15217831, 14542237, 1646131, 18603514, -11037887, 12876623, -2112447 }, + { 17902668, 4518229, -411702, -2829247, 26878217, 5258055, -12860753, 608397, 16031844, 3723494 }, + }, + { + { -28632773, 12763728, -20446446, 7577504, 33001348, -13017745, 17558842, -7872890, 23896954, -4314245 }, + { -20005381, -12011952, 31520464, 605201, 2543521, 5991821, -2945064, 7229064, -9919646, -8826859 }, + { 28816045, 298879, -28165016, -15920938, 19000928, -1665890, -12680833, -2949325, -18051778, -2082915 }, + }, + { + { 16000882, -344896, 3493092, -11447198, -29504595, -13159789, 12577740, 16041268, -19715240, 7847707 }, + { 10151868, 10572098, 27312476, 7922682, 14825339, 4723128, -32855931, -6519018, -10020567, 3852848 }, + { -11430470, 15697596, -21121557, -4420647, 5386314, 15063598, 16514493, -15932110, 29330899, -15076224 }, + }, + }, + { + { + { -25499735, -4378794, -15222908, -6901211, 16615731, 2051784, 3303702, 15490, -27548796, 12314391 }, + { 15683520, -6003043, 18109120, -9980648, 15337968, -5997823, -16717435, 15921866, 16103996, -3731215 }, + { -23169824, -10781249, 13588192, -1628807, -3798557, -1074929, -19273607, 5402699, -29815713, -9841101 }, + }, + { + { 23190676, 2384583, -32714340, 3462154, -29903655, -1529132, -11266856, 8911517, -25205859, 2739713 }, + { 21374101, -3554250, -33524649, 9874411, 15377179, 11831242, -33529904, 6134907, 4931255, 11987849 }, + { -7732, -2978858, -16223486, 7277597, 105524, -322051, -31480539, 13861388, -30076310, 10117930 }, + }, + { + { -29501170, -10744872, -26163768, 13051539, -25625564, 5089643, -6325503, 6704079, 12890019, 15728940 }, + { -21972360, -11771379, -951059, -4418840, 14704840, 2695116, 903376, -10428139, 12885167, 8311031 }, + { -17516482, 5352194, 10384213, -13811658, 7506451, 13453191, 26423267, 4384730, 1888765, -5435404 }, + }, + { + { -25817338, -3107312, -13494599, -3182506, 30896459, -13921729, -32251644, -12707869, -19464434, -3340243 }, + { -23607977, -2665774, -526091, 4651136, 5765089, 4618330, 6092245, 14845197, 17151279, -9854116 }, + { -24830458, -12733720, -15165978, 10367250, -29530908, -265356, 22825805, -7087279, -16866484, 16176525 }, + }, + { + { -23583256, 6564961, 20063689, 3798228, -4740178, 7359225, 2006182, -10363426, -28746253, -10197509 }, + { -10626600, -4486402, -13320562, -5125317, 3432136, -6393229, 23632037, -1940610, 32808310, 1099883 }, + { 15030977, 5768825, -27451236, -2887299, -6427378, -15361371, -15277896, -6809350, 2051441, -15225865 }, + }, + { + { -3362323, -7239372, 7517890, 9824992, 23555850, 295369, 5148398, -14154188, -22686354, 16633660 }, + { 4577086, -16752288, 13249841, -15304328, 19958763, -14537274, 18559670, -10759549, 8402478, -9864273 }, + { -28406330, -1051581, -26790155, -907698, -17212414, -11030789, 9453451, -14980072, 17983010, 9967138 }, + }, + { + { -25762494, 6524722, 26585488, 9969270, 24709298, 1220360, -1677990, 7806337, 17507396, 3651560 }, + { -10420457, -4118111, 14584639, 15971087, -15768321, 8861010, 26556809, -5574557, -18553322, -11357135 }, + { 2839101, 14284142, 4029895, 3472686, 14402957, 12689363, -26642121, 8459447, -5605463, -7621941 }, + }, + { + { -4839289, -3535444, 9744961, 2871048, 25113978, 3187018, -25110813, -849066, 17258084, -7977739 }, + { 18164541, -10595176, -17154882, -1542417, 19237078, -9745295, 23357533, -15217008, 26908270, 12150756 }, + { -30264870, -7647865, 5112249, -7036672, -1499807, -6974257, 43168, -5537701, -32302074, 16215819 }, + }, + }, + { + { + { -6898905, 9824394, -12304779, -4401089, -31397141, -6276835, 32574489, 12532905, -7503072, -8675347 }, + { -27343522, -16515468, -27151524, -10722951, 946346, 16291093, 254968, 7168080, 21676107, -1943028 }, + { 21260961, -8424752, -16831886, -11920822, -23677961, 3968121, -3651949, -6215466, -3556191, -7913075 }, + }, + { + { 16544754, 13250366, -16804428, 15546242, -4583003, 12757258, -2462308, -8680336, -18907032, -9662799 }, + { -2415239, -15577728, 18312303, 4964443, -15272530, -12653564, 26820651, 16690659, 25459437, -4564609 }, + { -25144690, 11425020, 28423002, -11020557, -6144921, -15826224, 9142795, -2391602, -6432418, -1644817 }, + }, + { + { -23104652, 6253476, 16964147, -3768872, -25113972, -12296437, -27457225, -16344658, 6335692, 7249989 }, + { -30333227, 13979675, 7503222, -12368314, -11956721, -4621693, -30272269, 2682242, 25993170, -12478523 }, + { 4364628, 5930691, 32304656, -10044554, -8054781, 15091131, 22857016, -10598955, 31820368, 15075278 }, + }, + { + { 31879134, -8918693, 17258761, 90626, -8041836, -4917709, 24162788, -9650886, -17970238, 12833045 }, + { 19073683, 14851414, -24403169, -11860168, 7625278, 11091125, -19619190, 2074449, -9413939, 14905377 }, + { 24483667, -11935567, -2518866, -11547418, -1553130, 15355506, -25282080, 9253129, 27628530, -7555480 }, + }, + { + { 17597607, 8340603, 19355617, 552187, 26198470, -3176583, 4593324, -9157582, -14110875, 15297016 }, + { 510886, 14337390, -31785257, 16638632, 6328095, 2713355, -20217417, -11864220, 8683221, 2921426 }, + { 18606791, 11874196, 27155355, -5281482, -24031742, 6265446, -25178240, -1278924, 4674690, 13890525 }, + }, + { + { 13609624, 13069022, -27372361, -13055908, 24360586, 9592974, 14977157, 9835105, 4389687, 288396 }, + { 9922506, -519394, 13613107, 5883594, -18758345, -434263, -12304062, 8317628, 23388070, 16052080 }, + { 12720016, 11937594, -31970060, -5028689, 26900120, 8561328, -20155687, -11632979, -14754271, -10812892 }, + }, + { + { 15961858, 14150409, 26716931, -665832, -22794328, 13603569, 11829573, 7467844, -28822128, 929275 }, + { 11038231, -11582396, -27310482, -7316562, -10498527, -16307831, -23479533, -9371869, -21393143, 2465074 }, + { 20017163, -4323226, 27915242, 1529148, 12396362, 15675764, 13817261, -9658066, 2463391, -4622140 }, + }, + { + { -16358878, -12663911, -12065183, 4996454, -1256422, 1073572, 9583558, 12851107, 4003896, 12673717 }, + { -1731589, -15155870, -3262930, 16143082, 19294135, 13385325, 14741514, -9103726, 7903886, 2348101 }, + { 24536016, -16515207, 12715592, -3862155, 1511293, 10047386, -3842346, -7129159, -28377538, 10048127 }, + }, + }, + { + { + { -12622226, -6204820, 30718825, 2591312, -10617028, 12192840, 18873298, -7297090, -32297756, 15221632 }, + { -26478122, -11103864, 11546244, -1852483, 9180880, 7656409, -21343950, 2095755, 29769758, 6593415 }, + { -31994208, -2907461, 4176912, 3264766, 12538965, -868111, 26312345, -6118678, 30958054, 8292160 }, + }, + { + { 31429822, -13959116, 29173532, 15632448, 12174511, -2760094, 32808831, 3977186, 26143136, -3148876 }, + { 22648901, 1402143, -22799984, 13746059, 7936347, 365344, -8668633, -1674433, -3758243, -2304625 }, + { -15491917, 8012313, -2514730, -12702462, -23965846, -10254029, -1612713, -1535569, -16664475, 8194478 }, + }, + { + { 27338066, -7507420, -7414224, 10140405, -19026427, -6589889, 27277191, 8855376, 28572286, 3005164 }, + { 26287124, 4821776, 25476601, -4145903, -3764513, -15788984, -18008582, 1182479, -26094821, -13079595 }, + { -7171154, 3178080, 23970071, 6201893, -17195577, -4489192, -21876275, -13982627, 32208683, -1198248 }, + }, + { + { -16657702, 2817643, -10286362, 14811298, 6024667, 13349505, -27315504, -10497842, -27672585, -11539858 }, + { 15941029, -9405932, -21367050, 8062055, 31876073, -238629, -15278393, -1444429, 15397331, -4130193 }, + { 8934485, -13485467, -23286397, -13423241, -32446090, 14047986, 31170398, -1441021, -27505566, 15087184 }, + }, + { + { -18357243, -2156491, 24524913, -16677868, 15520427, -6360776, -15502406, 11461896, 16788528, -5868942 }, + { -1947386, 16013773, 21750665, 3714552, -17401782, -16055433, -3770287, -10323320, 31322514, -11615635 }, + { 21426655, -5650218, -13648287, -5347537, -28812189, -4920970, -18275391, -14621414, 13040862, -12112948 }, + }, + { + { 11293895, 12478086, -27136401, 15083750, -29307421, 14748872, 14555558, -13417103, 1613711, 4896935 }, + { -25894883, 15323294, -8489791, -8057900, 25967126, -13425460, 2825960, -4897045, -23971776, -11267415 }, + { -15924766, -5229880, -17443532, 6410664, 3622847, 10243618, 20615400, 12405433, -23753030, -8436416 }, + }, + { + { -7091295, 12556208, -20191352, 9025187, -17072479, 4333801, 4378436, 2432030, 23097949, -566018 }, + { 4565804, -16025654, 20084412, -7842817, 1724999, 189254, 24767264, 10103221, -18512313, 2424778 }, + { 366633, -11976806, 8173090, -6890119, 30788634, 5745705, -7168678, 1344109, -3642553, 12412659 }, + }, + { + { -24001791, 7690286, 14929416, -168257, -32210835, -13412986, 24162697, -15326504, -3141501, 11179385 }, + { 18289522, -14724954, 8056945, 16430056, -21729724, 7842514, -6001441, -1486897, -18684645, -11443503 }, + { 476239, 6601091, -6152790, -9723375, 17503545, -4863900, 27672959, 13403813, 11052904, 5219329 }, + }, + }, + { + { + { 20678546, -8375738, -32671898, 8849123, -5009758, 14574752, 31186971, -3973730, 9014762, -8579056 }, + { -13644050, -10350239, -15962508, 5075808, -1514661, -11534600, -33102500, 9160280, 8473550, -3256838 }, + { 24900749, 14435722, 17209120, -15292541, -22592275, 9878983, -7689309, -16335821, -24568481, 11788948 }, + }, + { + { -3118155, -11395194, -13802089, 14797441, 9652448, -6845904, -20037437, 10410733, -24568470, -1458691 }, + { -15659161, 16736706, -22467150, 10215878, -9097177, 7563911, 11871841, -12505194, -18513325, 8464118 }, + { -23400612, 8348507, -14585951, -861714, -3950205, -6373419, 14325289, 8628612, 33313881, -8370517 }, + }, + { + { -20186973, -4967935, 22367356, 5271547, -1097117, -4788838, -24805667, -10236854, -8940735, -5818269 }, + { -6948785, -1795212, -32625683, -16021179, 32635414, -7374245, 15989197, -12838188, 28358192, -4253904 }, + { -23561781, -2799059, -32351682, -1661963, -9147719, 10429267, -16637684, 4072016, -5351664, 5596589 }, + }, + { + { -28236598, -3390048, 12312896, 6213178, 3117142, 16078565, 29266239, 2557221, 1768301, 15373193 }, + { -7243358, -3246960, -4593467, -7553353, -127927, -912245, -1090902, -4504991, -24660491, 3442910 }, + { -30210571, 5124043, 14181784, 8197961, 18964734, -11939093, 22597931, 7176455, -18585478, 13365930 }, + }, + { + { -7877390, -1499958, 8324673, 4690079, 6261860, 890446, 24538107, -8570186, -9689599, -3031667 }, + { 25008904, -10771599, -4305031, -9638010, 16265036, 15721635, 683793, -11823784, 15723479, -15163481 }, + { -9660625, 12374379, -27006999, -7026148, -7724114, -12314514, 11879682, 5400171, 519526, -1235876 }, + }, + { + { 22258397, -16332233, -7869817, 14613016, -22520255, -2950923, -20353881, 7315967, 16648397, 7605640 }, + { -8081308, -8464597, -8223311, 9719710, 19259459, -15348212, 23994942, -5281555, -9468848, 4763278 }, + { -21699244, 9220969, -15730624, 1084137, -25476107, -2852390, 31088447, -7764523, -11356529, 728112 }, + }, + { + { 26047220, -11751471, -6900323, -16521798, 24092068, 9158119, -4273545, -12555558, -29365436, -5498272 }, + { 17510331, -322857, 5854289, 8403524, 17133918, -3112612, -28111007, 12327945, 10750447, 10014012 }, + { -10312768, 3936952, 9156313, -8897683, 16498692, -994647, -27481051, -666732, 3424691, 7540221 }, + }, + { + { 30322361, -6964110, 11361005, -4143317, 7433304, 4989748, -7071422, -16317219, -9244265, 15258046 }, + { 13054562, -2779497, 19155474, 469045, -12482797, 4566042, 5631406, 2711395, 1062915, -5136345 }, + { -19240248, -11254599, -29509029, -7499965, -5835763, 13005411, -6066489, 12194497, 32960380, 1459310 }, + }, + }, + { + { + { 19852034, 7027924, 23669353, 10020366, 8586503, -6657907, 394197, -6101885, 18638003, -11174937 }, + { 31395534, 15098109, 26581030, 8030562, -16527914, -5007134, 9012486, -7584354, -6643087, -5442636 }, + { -9192165, -2347377, -1997099, 4529534, 25766844, 607986, -13222, 9677543, -32294889, -6456008 }, + }, + { + { -2444496, -149937, 29348902, 8186665, 1873760, 12489863, -30934579, -7839692, -7852844, -8138429 }, + { -15236356, -15433509, 7766470, 746860, 26346930, -10221762, -27333451, 10754588, -9431476, 5203576 }, + { 31834314, 14135496, -770007, 5159118, 20917671, -16768096, -7467973, -7337524, 31809243, 7347066 }, + }, + { + { -9606723, -11874240, 20414459, 13033986, 13716524, -11691881, 19797970, -12211255, 15192876, -2087490 }, + { -12663563, -2181719, 1168162, -3804809, 26747877, -14138091, 10609330, 12694420, 33473243, -13382104 }, + { 33184999, 11180355, 15832085, -11385430, -1633671, 225884, 15089336, -11023903, -6135662, 14480053 }, + }, + { + { 31308717, -5619998, 31030840, -1897099, 15674547, -6582883, 5496208, 13685227, 27595050, 8737275 }, + { -20318852, -15150239, 10933843, -16178022, 8335352, -7546022, -31008351, -12610604, 26498114, 66511 }, + { 22644454, -8761729, -16671776, 4884562, -3105614, -13559366, 30540766, -4286747, -13327787, -7515095 }, + }, + { + { -28017847, 9834845, 18617207, -2681312, -3401956, -13307506, 8205540, 13585437, -17127465, 15115439 }, + { 23711543, -672915, 31206561, -8362711, 6164647, -9709987, -33535882, -1426096, 8236921, 16492939 }, + { -23910559, -13515526, -26299483, -4503841, 25005590, -7687270, 19574902, 10071562, 6708380, -6222424 }, + }, + { + { 2101391, -4930054, 19702731, 2367575, -15427167, 1047675, 5301017, 9328700, 29955601, -11678310 }, + { 3096359, 9271816, -21620864, -15521844, -14847996, -7592937, -25892142, -12635595, -9917575, 6216608 }, + { -32615849, 338663, -25195611, 2510422, -29213566, -13820213, 24822830, -6146567, -26767480, 7525079 }, + }, + { + { -23066649, -13985623, 16133487, -7896178, -3389565, 778788, -910336, -2782495, -19386633, 11994101 }, + { 21691500, -13624626, -641331, -14367021, 3285881, -3483596, -25064666, 9718258, -7477437, 13381418 }, + { 18445390, -4202236, 14979846, 11622458, -1727110, -3582980, 23111648, -6375247, 28535282, 15779576 }, + }, + { + { 30098053, 3089662, -9234387, 16662135, -21306940, 11308411, -14068454, 12021730, 9955285, -16303356 }, + { 9734894, -14576830, -7473633, -9138735, 2060392, 11313496, -18426029, 9924399, 20194861, 13380996 }, + { -26378102, -7965207, -22167821, 15789297, -18055342, -6168792, -1984914, 15707771, 26342023, 10146099 }, + }, + }, + { + { + { -26016874, -219943, 21339191, -41388, 19745256, -2878700, -29637280, 2227040, 21612326, -545728 }, + { -13077387, 1184228, 23562814, -5970442, -20351244, -6348714, 25764461, 12243797, -20856566, 11649658 }, + { -10031494, 11262626, 27384172, 2271902, 26947504, -15997771, 39944, 6114064, 33514190, 2333242 }, + }, + { + { -21433588, -12421821, 8119782, 7219913, -21830522, -9016134, -6679750, -12670638, 24350578, -13450001 }, + { -4116307, -11271533, -23886186, 4843615, -30088339, 690623, -31536088, -10406836, 8317860, 12352766 }, + { 18200138, -14475911, -33087759, -2696619, -23702521, -9102511, -23552096, -2287550, 20712163, 6719373 }, + }, + { + { 26656208, 6075253, -7858556, 1886072, -28344043, 4262326, 11117530, -3763210, 26224235, -3297458 }, + { -17168938, -14854097, -3395676, -16369877, -19954045, 14050420, 21728352, 9493610, 18620611, -16428628 }, + { -13323321, 13325349, 11432106, 5964811, 18609221, 6062965, -5269471, -9725556, -30701573, -16479657 }, + }, + { + { -23860538, -11233159, 26961357, 1640861, -32413112, -16737940, 12248509, -5240639, 13735342, 1934062 }, + { 25089769, 6742589, 17081145, -13406266, 21909293, -16067981, -15136294, -3765346, -21277997, 5473616 }, + { 31883677, -7961101, 1083432, -11572403, 22828471, 13290673, -7125085, 12469656, 29111212, -5451014 }, + }, + { + { 24244947, -15050407, -26262976, 2791540, -14997599, 16666678, 24367466, 6388839, -10295587, 452383 }, + { -25640782, -3417841, 5217916, 16224624, 19987036, -4082269, -24236251, -5915248, 15766062, 8407814 }, + { -20406999, 13990231, 15495425, 16395525, 5377168, 15166495, -8917023, -4388953, -8067909, 2276718 }, + }, + { + { 30157918, 12924066, -17712050, 9245753, 19895028, 3368142, -23827587, 5096219, 22740376, -7303417 }, + { 2041139, -14256350, 7783687, 13876377, -25946985, -13352459, 24051124, 13742383, -15637599, 13295222 }, + { 33338237, -8505733, 12532113, 7977527, 9106186, -1715251, -17720195, -4612972, -4451357, -14669444 }, + }, + { + { -20045281, 5454097, -14346548, 6447146, 28862071, 1883651, -2469266, -4141880, 7770569, 9620597 }, + { 23208068, 7979712, 33071466, 8149229, 1758231, -10834995, 30945528, -1694323, -33502340, -14767970 }, + { 1439958, -16270480, -1079989, -793782, 4625402, 10647766, -5043801, 1220118, 30494170, -11440799 }, + }, + { + { -5037580, -13028295, -2970559, -3061767, 15640974, -6701666, -26739026, 926050, -1684339, -13333647 }, + { 13908495, -3549272, 30919928, -6273825, -21521863, 7989039, 9021034, 9078865, 3353509, 4033511 }, + { -29663431, -15113610, 32259991, -344482, 24295849, -12912123, 23161163, 8839127, 27485041, 7356032 }, + }, + }, + { + { + { 9661027, 705443, 11980065, -5370154, -1628543, 14661173, -6346142, 2625015, 28431036, -16771834 }, + { -23839233, -8311415, -25945511, 7480958, -17681669, -8354183, -22545972, 14150565, 15970762, 4099461 }, + { 29262576, 16756590, 26350592, -8793563, 8529671, -11208050, 13617293, -9937143, 11465739, 8317062 }, + }, + { + { -25493081, -6962928, 32500200, -9419051, -23038724, -2302222, 14898637, 3848455, 20969334, -5157516 }, + { -20384450, -14347713, -18336405, 13884722, -33039454, 2842114, -21610826, -3649888, 11177095, 14989547 }, + { -24496721, -11716016, 16959896, 2278463, 12066309, 10137771, 13515641, 2581286, -28487508, 9930240 }, + }, + { + { -17751622, -2097826, 16544300, -13009300, -15914807, -14949081, 18345767, -13403753, 16291481, -5314038 }, + { -33229194, 2553288, 32678213, 9875984, 8534129, 6889387, -9676774, 6957617, 4368891, 9788741 }, + { 16660756, 7281060, -10830758, 12911820, 20108584, -8101676, -21722536, -8613148, 16250552, -11111103 }, + }, + { + { -19765507, 2390526, -16551031, 14161980, 1905286, 6414907, 4689584, 10604807, -30190403, 4782747 }, + { -1354539, 14736941, -7367442, -13292886, 7710542, -14155590, -9981571, 4383045, 22546403, 437323 }, + { 31665577, -12180464, -16186830, 1491339, -18368625, 3294682, 27343084, 2786261, -30633590, -14097016 }, + }, + { + { -14467279, -683715, -33374107, 7448552, 19294360, 14334329, -19690631, 2355319, -19284671, -6114373 }, + { 15121312, -15796162, 6377020, -6031361, -10798111, -12957845, 18952177, 15496498, -29380133, 11754228 }, + { -2637277, -13483075, 8488727, -14303896, 12728761, -1622493, 7141596, 11724556, 22761615, -10134141 }, + }, + { + { 16918416, 11729663, -18083579, 3022987, -31015732, -13339659, -28741185, -12227393, 32851222, 11717399 }, + { 11166634, 7338049, -6722523, 4531520, -29468672, -7302055, 31474879, 3483633, -1193175, -4030831 }, + { -185635, 9921305, 31456609, -13536438, -12013818, 13348923, 33142652, 6546660, -19985279, -3948376 }, + }, + { + { -32460596, 11266712, -11197107, -7899103, 31703694, 3855903, -8537131, -12833048, -30772034, -15486313 }, + { -18006477, 12709068, 3991746, -6479188, -21491523, -10550425, -31135347, -16049879, 10928917, 3011958 }, + { -6957757, -15594337, 31696059, 334240, 29576716, 14796075, -30831056, -12805180, 18008031, 10258577 }, + }, + { + { -22448644, 15655569, 7018479, -4410003, -30314266, -1201591, -1853465, 1367120, 25127874, 6671743 }, + { 29701166, -14373934, -10878120, 9279288, -17568, 13127210, 21382910, 11042292, 25838796, 4642684 }, + { -20430234, 14955537, -24126347, 8124619, -5369288, -5990470, 30468147, -13900640, 18423289, 4177476 }, + }, + }, +}; diff --git a/core/eos/services/crypto/src/sc.h b/core/eos/services/crypto/src/sc.h new file mode 100644 index 00000000..e29e7fa5 --- /dev/null +++ b/core/eos/services/crypto/src/sc.h @@ -0,0 +1,12 @@ +#ifndef SC_H +#define SC_H + +/* +The set of scalars is \Z/l +where l = 2^252 + 27742317777372353535851937790883648493. +*/ + +void sc_reduce(unsigned char *s); +void sc_muladd(unsigned char *s, const unsigned char *a, const unsigned char *b, const unsigned char *c); + +#endif diff --git a/core/eos/services/crypto/src/sha512.h b/core/eos/services/crypto/src/sha512.h new file mode 100644 index 00000000..a34dd5e4 --- /dev/null +++ b/core/eos/services/crypto/src/sha512.h @@ -0,0 +1,21 @@ +#ifndef SHA512_H +#define SHA512_H + +#include + +#include "fixedint.h" + +/* state */ +typedef struct sha512_context_ { + uint64_t length, state[8]; + size_t curlen; + unsigned char buf[128]; +} sha512_context; + + +int sha512_init(sha512_context * md); +int sha512_final(sha512_context * md, unsigned char *out); +int sha512_update(sha512_context * md, const unsigned char *in, size_t inlen); +int sha512(const unsigned char *message, size_t message_len, unsigned char *out); + +#endif diff --git a/core/eos/services/filesystem/include/eos/filesystem.h b/core/eos/services/filesystem/include/eos/filesystem.h index 54ac08eb..909719d9 100644 --- a/core/eos/services/filesystem/include/eos/filesystem.h +++ b/core/eos/services/filesystem/include/eos/filesystem.h @@ -81,9 +81,11 @@ int eos_fs_stat(eos_fs_stat_t *stat); eos_file_t eos_fs_open(const char *path, uint32_t flags); int eos_fs_close(eos_file_t fd); int eos_fs_read(eos_file_t fd, void *buf, size_t len); +/* Writing after seeking beyond EOF zero-fills the intervening gap. */ int eos_fs_write(eos_file_t fd, const void *data, size_t len); int eos_fs_seek(eos_file_t fd, int32_t offset, eos_seek_whence_t whence); int eos_fs_tell(eos_file_t fd, uint32_t *pos); +/* Shrink or grow a file; bytes added by growth read back as zero. */ int eos_fs_truncate(eos_file_t fd, uint32_t size); int eos_fs_sync(eos_file_t fd); @@ -94,8 +96,17 @@ int eos_fs_readdir(eos_dir_t dir, eos_dirent_t *entry); int eos_fs_closedir(eos_dir_t dir); /* Path operations */ + +/* Unlink a path. POSIX semantics: the name is removed immediately, but a file + * with descriptors still open on it keeps its storage until the last one is + * closed. Until then it is invisible to eos_fs_exists() and eos_fs_readdir() + * while still counting towards eos_fs_stat()'s used_bytes. */ int eos_fs_remove(const char *path); + +/* Rename a path, replacing new_path if it already exists (as POSIX rename() + * does). Renaming a path onto itself succeeds and changes nothing. */ int eos_fs_rename(const char *old_path, const char *new_path); + bool eos_fs_exists(const char *path); #ifdef __cplusplus diff --git a/core/eos/services/gps/CMakeLists.txt b/core/eos/services/gps/CMakeLists.txt new file mode 100644 index 00000000..0a8573f9 --- /dev/null +++ b/core/eos/services/gps/CMakeLists.txt @@ -0,0 +1,10 @@ +add_library(eos_gps STATIC + src/gps_nmea.c +) + +target_compile_definitions(eos_gps PUBLIC EOS_ENABLE_GPS=1) + +target_include_directories(eos_gps PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/include + ${CMAKE_SOURCE_DIR}/include +) \ No newline at end of file diff --git a/core/eos/services/gps/include/eos/gps_nmea.h b/core/eos/services/gps/include/eos/gps_nmea.h new file mode 100644 index 00000000..d13fe7d3 --- /dev/null +++ b/core/eos/services/gps/include/eos/gps_nmea.h @@ -0,0 +1,48 @@ +// SPDX-License-Identifier: MIT +// Copyright (c) 2026 EoS Project +// ISO/IEC 25000 | ISO/IEC/IEEE 15288:2023 + +/** + * @file gps_nmea.h + * @brief EoS GPS Service — NMEA 0183 sentence parsing + * + * Provides parsing of standard NMEA GPRMC sentences into a structured + * position/velocity fix usable by higher-level location services. + */ + +#ifndef EOS_GPS_NMEA_H +#define EOS_GPS_NMEA_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct { + double latitude; + double longitude; + float altitude; + float speed_knots; + int satellites; + bool fix_valid; +} eos_gps_data_t; + +/** + * Parse a standard NMEA $GPRMC sentence. + * + * @param nmea NUL-terminated NMEA sentence, e.g. + * "$GPRMC,092750.000,A,3746.4949,N,12225.1644,W,0.00,0.00,270526,,,A*7B" + * @param data Output struct populated on success. + * @return 0 on success (data->fix_valid indicates whether the fix is valid), + * -1 on invalid arguments or sentence type, -2 if the sentence has + * too few fields to parse. + */ +int eos_gps_parse_gprmc(const char *nmea, eos_gps_data_t *data); + +#ifdef __cplusplus +} +#endif + +#endif /* EOS_GPS_NMEA_H */ \ No newline at end of file diff --git a/core/eos/services/gps/src/gps_nmea.c b/core/eos/services/gps/src/gps_nmea.c new file mode 100644 index 00000000..f7c46b31 --- /dev/null +++ b/core/eos/services/gps/src/gps_nmea.c @@ -0,0 +1,63 @@ +// SPDX-License-Identifier: MIT +// Copyright (c) 2026 EoS Project +// ISO/IEC 25000 | ISO/IEC/IEEE 15288:2023 + +#include +#include +#include +#include +#include + +#if EOS_ENABLE_GPS + +/* Parse standard NMEA $GPRMC sentence */ +int eos_gps_parse_gprmc(const char *nmea, eos_gps_data_t *data) { + if (!nmea || !data) return -1; + if (strncmp(nmea, "$GPRMC", 6) != 0) return -1; + + char buf[128]; + strncpy(buf, nmea, sizeof(buf)-1); + buf[sizeof(buf)-1] = '\0'; + + char *tokens[15]; + int tok_cnt = 0; + char *p = buf; + while (p && tok_cnt < 15) { + tokens[tok_cnt++] = p; + char *comma = strchr(p, ','); + if (comma) { + *comma = '\0'; + p = comma + 1; + } else { + break; + } + } + + if (tok_cnt < 8) return -2; + + /* Check status (A=Active, V=Void) */ + if (strcmp(tokens[2], "A") != 0) { + data->fix_valid = false; + return 0; + } + + /* Parse Latitude: DDMM.MMMM */ + double raw_lat = (tokens[3][0] != '\0') ? atof(tokens[3]) : 0.0; + int lat_deg = (int)(raw_lat / 100); + double lat_min = raw_lat - (lat_deg * 100); + data->latitude = lat_deg + (lat_min / 60.0); + if (strcmp(tokens[4], "S") == 0) data->latitude = -data->latitude; + + /* Parse Longitude: DDDMM.MMMM */ + double raw_lon = (tokens[5][0] != '\0') ? atof(tokens[5]) : 0.0; + int lon_deg = (int)(raw_lon / 100); + double lon_min = raw_lon - (lon_deg * 100); + data->longitude = lon_deg + (lon_min / 60.0); + if (strcmp(tokens[6], "W") == 0) data->longitude = -data->longitude; + + data->speed_knots = (tokens[7][0] != '\0') ? (float)atof(tokens[7]) : 0.0f; + data->fix_valid = true; + return 0; +} + +#endif /* EOS_ENABLE_GPS */ \ No newline at end of file diff --git a/core/eos/services/linux/CMakeLists.txt b/core/eos/services/linux/CMakeLists.txt index 1b182c0f..5d506a3f 100644 --- a/core/eos/services/linux/CMakeLists.txt +++ b/core/eos/services/linux/CMakeLists.txt @@ -6,6 +6,16 @@ target_include_directories(eos_linux_security PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include ) +# For EOS_ERROR. Same resolution as #108 took for eos_ota: report through the +# tree's logger rather than fprintf(stderr). No cycle -- eos_core does not +# depend on eos_linux_security. +# +# PRIVATE: eos/log.h is used by linux_security.c and by nothing under +# include/, so there is no reason to push eos_core's include directories onto +# every consumer. CMake still records the link requirement of a static +# library's PRIVATE dependency as $, so consumers still link it. +target_link_libraries(eos_linux_security PRIVATE eos_core) + target_compile_options(eos_linux_security PRIVATE $<$:-Wall -Wextra -Wpedantic> $<$:-Wall -Wextra -Wpedantic> diff --git a/core/eos/services/ota/CMakeLists.txt b/core/eos/services/ota/CMakeLists.txt index 37da743b..4fa874d1 100644 --- a/core/eos/services/ota/CMakeLists.txt +++ b/core/eos/services/ota/CMakeLists.txt @@ -6,4 +6,4 @@ target_include_directories(eos_ota PUBLIC ${CMAKE_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/services/crypto/include ) -target_link_libraries(eos_ota PUBLIC eos_hal eos_crypto) \ No newline at end of file +target_link_libraries(eos_ota PUBLIC eos_hal eos_crypto eos_core) \ No newline at end of file diff --git a/core/eos/services/ota/include/eos/ota.h b/core/eos/services/ota/include/eos/ota.h index 5306c590..b9a781dc 100644 --- a/core/eos/services/ota/include/eos/ota.h +++ b/core/eos/services/ota/include/eos/ota.h @@ -44,6 +44,12 @@ typedef struct { uint32_t expected_size; uint8_t expected_sha256[32]; bool use_tls; + + /* Signature over the image, handed to the authenticator callback. May be + * empty: expected_sha256 alone proves only that the download was not + * corrupted in transit, never who produced it. */ + const uint8_t *signature; + size_t signature_len; } eos_ota_source_t; typedef struct { @@ -59,6 +65,22 @@ typedef struct { typedef void (*eos_ota_progress_cb)(uint8_t progress_pct, void *ctx); +/** + * @brief Authenticity check for a downloaded image. + * + * Called by eos_ota_apply() with the SHA-256 the service computed over the + * bytes it actually received -- not the one the update source declared -- plus + * whatever signature material accompanied the update. The platform installs + * one of these with eos_ota_set_authenticator(); it is where a public key and + * a real signature verifier belong (see services/security). + * + * @return 0 if the image is authentic, non-zero otherwise. + */ +typedef int (*eos_ota_authenticator_cb)(const uint8_t digest[32], + const uint8_t *signature, + size_t signature_len, + void *ctx); + int eos_ota_init(void); void eos_ota_deinit(void); @@ -68,7 +90,36 @@ int eos_ota_write_chunk(const uint8_t *data, size_t len); int eos_ota_finish(void); int eos_ota_abort(void); +/** + * @brief Check the downloaded image against the source's declared SHA-256. + * + * This is an integrity check only. The digest it compares against travels with + * the update, so anyone able to substitute the image can substitute the digest + * too. Passing this does not mean the image is trusted -- eos_ota_apply() + * enforces that separately. + * + * @return 0 if the bytes received hash to the declared digest. + */ int eos_ota_verify(void); + +/** + * @brief Install an authenticity callback, or NULL to remove it. + * + * Must be called before eos_ota_apply(). Without one, apply() refuses: an + * unauthenticated image is not installable in a default build. + */ +int eos_ota_set_authenticator(eos_ota_authenticator_cb cb, void *ctx); + +/** + * @brief Switch the active slot to the downloaded image. + * + * Requires that eos_ota_verify() passed and that the installed authenticator + * accepted the image. A build with no authenticator installed is refused + * unless it defines EOS_ALLOW_UNSIGNED_OTA, which mirrors the + * EOS_ALLOW_STUB_CRYPTO opt-in in services/crypto. + * + * @return 0 on success, -1 if the image is unverified or unauthenticated. + */ int eos_ota_apply(void); int eos_ota_rollback(void); diff --git a/core/eos/services/ota/src/ota.c b/core/eos/services/ota/src/ota.c index 2d9e6b43..24ab7db1 100644 --- a/core/eos/services/ota/src/ota.c +++ b/core/eos/services/ota/src/ota.c @@ -4,6 +4,7 @@ #include "eos/ota.h" #include "eos/crypto.h" +#include "eos/log.h" #include #if EOS_ENABLE_OTA @@ -16,10 +17,16 @@ static struct { uint32_t bytes_written; uint32_t total_size; uint8_t expected_hash[32]; + uint8_t actual_hash[32]; + const uint8_t *signature; + size_t signature_len; int in_progress; - int verified; + int integrity_ok; /* bytes received hash to expected_hash */ + int authenticated; /* the authenticator accepted the image */ eos_ota_progress_cb progress_cb; void *progress_ctx; + eos_ota_authenticator_cb auth_cb; + void *auth_ctx; uint8_t slot_valid[2]; } g_ota; @@ -47,16 +54,23 @@ int eos_ota_begin(const eos_ota_source_t *source) { g_ota.update_slot = (g_ota.active_slot == EOS_OTA_SLOT_A) ? EOS_OTA_SLOT_B : EOS_OTA_SLOT_A; g_ota.total_size = source->expected_size; g_ota.bytes_written = 0; - g_ota.verified = 0; + g_ota.integrity_ok = 0; + g_ota.authenticated = 0; g_ota.in_progress = 1; memcpy(g_ota.expected_hash, source->expected_sha256, 32); + memset(g_ota.actual_hash, 0, sizeof(g_ota.actual_hash)); + g_ota.signature = source->signature; + g_ota.signature_len = source->signature_len; eos_sha256_init(&g_ota.hash_ctx); return 0; } int eos_ota_write_chunk(const uint8_t *data, size_t len) { if (!g_ota.initialized || !g_ota.in_progress || !data) return -1; - if (g_ota.bytes_written + len > g_ota.total_size) return -1; + /* Compare against remaining space. Adding len can wrap and accept a huge chunk. */ + if (g_ota.bytes_written > g_ota.total_size || + len > (size_t)(g_ota.total_size - g_ota.bytes_written)) + return -1; eos_sha256_update(&g_ota.hash_ctx, data, len); g_ota.bytes_written += (uint32_t)len; if (g_ota.progress_cb && g_ota.total_size > 0) { @@ -75,23 +89,59 @@ int eos_ota_finish(void) { int eos_ota_abort(void) { g_ota.in_progress = 0; g_ota.bytes_written = 0; - g_ota.verified = 0; + g_ota.integrity_ok = 0; + g_ota.authenticated = 0; return 0; } int eos_ota_verify(void) { if (!g_ota.initialized || g_ota.bytes_written != g_ota.total_size) return -1; - uint8_t actual[32]; - eos_sha256_final(&g_ota.hash_ctx, actual); - if (memcmp(actual, g_ota.expected_hash, 32) != 0) return -1; - g_ota.verified = 1; - g_ota.slot_valid[g_ota.update_slot] = 1; + if (g_ota.integrity_ok) return 0; /* the hash context is single-use */ + + eos_sha256_final(&g_ota.hash_ctx, g_ota.actual_hash); + if (memcmp(g_ota.actual_hash, g_ota.expected_hash, 32) != 0) return -1; + + /* Integrity only. The slot is not marked valid here: until apply() runs it + * holds a half-installed image, and marking it would make it a legal + * eos_ota_rollback() / eos_ota_set_active_slot() target. */ + g_ota.integrity_ok = 1; + return 0; +} + +int eos_ota_set_authenticator(eos_ota_authenticator_cb cb, void *ctx) { + if (!g_ota.initialized) return -1; + g_ota.auth_cb = cb; + g_ota.auth_ctx = ctx; return 0; } int eos_ota_apply(void) { - if (!g_ota.initialized || !g_ota.verified) return -1; + if (!g_ota.initialized || !g_ota.integrity_ok) return -1; + + if (g_ota.auth_cb) { + /* The authenticator is given the digest this service computed, never + * the one the update declared. */ + if (g_ota.auth_cb(g_ota.actual_hash, g_ota.signature, + g_ota.signature_len, g_ota.auth_ctx) != 0) { + g_ota.authenticated = 0; + return -1; + } + g_ota.authenticated = 1; + } else { +#ifdef EOS_ALLOW_UNSIGNED_OTA + g_ota.authenticated = 1; +#else + EOS_ERROR("ota: refusing to apply an update with no authenticator " + "installed; expected_sha256 travels with the update and " + "proves nothing about its origin. Call " + "eos_ota_set_authenticator(), or build with " + "EOS_ALLOW_UNSIGNED_OTA."); + return -1; +#endif + } + g_ota.active_slot = g_ota.update_slot; + g_ota.slot_valid[g_ota.update_slot] = 1; return 0; } diff --git a/core/eos/services/rtos/include/eos/rtos_security.h b/core/eos/services/rtos/include/eos/rtos_security.h index db8661b8..4ca025b1 100644 --- a/core/eos/services/rtos/include/eos/rtos_security.h +++ b/core/eos/services/rtos/include/eos/rtos_security.h @@ -20,6 +20,9 @@ /* ---- MPU-based Task Isolation ---- */ #define EOS_MPU_MAX_REGIONS 16 +/* ARMv7-M RASR.SIZE encodes 2^(SIZE+1) bytes with SIZE >= 4, so the + * smallest region the hardware can express is 32 bytes. */ +#define EOS_MPU_MIN_REGION_SIZE 32u #define EOS_MPU_MAX_TASKS 32 typedef enum { diff --git a/core/eos/services/rtos/src/rtos_security.c b/core/eos/services/rtos/src/rtos_security.c index 2c4aa2d8..eeb48ad3 100644 --- a/core/eos/services/rtos/src/rtos_security.c +++ b/core/eos/services/rtos/src/rtos_security.c @@ -72,14 +72,47 @@ int eos_mpu_validate(const EosMpuConfig *mpu) { } for (int r = 0; r < task->region_count; r++) { const EosMpuRegion *reg = &task->regions[r]; - /* Check power-of-2 alignment (ARM MPU requirement) */ - if (reg->size > 0 && (reg->size & (reg->size - 1)) != 0) { - printf(" WARN: Task '%s' region '%s' size 0x%x not power-of-2\n", + + /* These are errors, not warnings. + * + * ARMv7-M encodes a region's extent as a power-of-two exponent in + * RASR.SIZE, and RBAR requires the base to be aligned to that + * extent. A region that violates either cannot be expressed: what + * gets programmed covers different memory than was asked for. For + * a task-isolation service, "isolated, but not over the range you + * specified" is the failure this validation exists to catch, so it + * has to count against the return value rather than print a note + * and report the configuration clean. */ + if (reg->size == 0) { + printf(" ERROR: Task '%s' region '%s' has size 0\n", + task->task_name, reg->label); + errors++; + continue; /* every check below divides by or masks with size */ + } + if ((reg->size & (reg->size - 1)) != 0) { + printf(" ERROR: Task '%s' region '%s' size 0x%x not power-of-2\n", task->task_name, reg->label, reg->size); + errors++; + } + if (reg->size < EOS_MPU_MIN_REGION_SIZE) { + printf(" ERROR: Task '%s' region '%s' size 0x%x below the " + "%u-byte minimum\n", + task->task_name, reg->label, reg->size, + (unsigned)EOS_MPU_MIN_REGION_SIZE); + errors++; } - if (reg->base_addr % reg->size != 0 && reg->size > 0) { - printf(" WARN: Task '%s' region '%s' base 0x%x not aligned to size 0x%x\n", + /* Guarded by the size == 0 check above: `base % size` with size 0 + * is undefined. It was previously written as + * `reg->base_addr % reg->size != 0 && reg->size > 0`, and && only + * guards what is to its right -- so the modulo ran first and a + * zero-sized region divided by zero. UBSan aborts on it; on x86 + * that is SIGFPE, while AArch64's udiv quietly yields 0, so it + * reproduced only on some hosts. */ + if (reg->base_addr % reg->size != 0) { + printf(" ERROR: Task '%s' region '%s' base 0x%x not aligned " + "to size 0x%x\n", task->task_name, reg->label, reg->base_addr, reg->size); + errors++; } } } diff --git a/core/eos/services/security/src/security.c b/core/eos/services/security/src/security.c index cc3bb680..67dd7ea9 100644 --- a/core/eos/services/security/src/security.c +++ b/core/eos/services/security/src/security.c @@ -17,6 +17,20 @@ void eos_secureboot_init(EosSecureBoot *sb) { int eos_secureboot_verify_image(EosSecureBoot *sb, const char *image_path, const char *sig_path, const char *pubkey_path) { + /* Guard before the copies, not after. The check further down already + * refuses a missing pubkey_path, which is the author saying NULL is an + * input this function expects to see -- but the three strncpy() calls + * below ran first, and strncpy(dst, NULL, n) is undefined behaviour, so + * the caller got SIGSEGV instead of the EOS_BOOT_FAILED that guard was + * written to return. */ + if (!sb) return -1; + if (!image_path || !sig_path || !pubkey_path) { + fprintf(stderr, "eos-security: secure boot: image, signature and " + "public key paths are all required\n"); + sb->status = EOS_BOOT_FAILED; + return -1; + } + strncpy(sb->image_path, image_path, sizeof(sb->image_path) - 1); strncpy(sb->sig_path, sig_path, sizeof(sb->sig_path) - 1); strncpy(sb->pubkey_path, pubkey_path, sizeof(sb->pubkey_path) - 1); @@ -53,10 +67,34 @@ int eos_secureboot_verify_image(EosSecureBoot *sb, const char *image_path, fclose(img); eos_sha256_final(&ctx, digest); + /* Load the public key the caller named. This previously fabricated an + * all-zero key and never opened pubkey_path at all, so verification ran + * against no key material whatsoever and a nonexistent key path still + * produced EOS_BOOT_VERIFIED. Refusing here means a missing or unreadable + * key fails the boot instead of silently passing it. */ EosRsaKey pub_key; memset(&pub_key, 0, sizeof(pub_key)); pub_key.key_bits = 2048; + if (!pubkey_path || !*pubkey_path) { + fprintf(stderr, "eos-security: secure boot: no public key path supplied\n"); + sb->status = EOS_BOOT_FAILED; + return -1; + } + FILE *kf = fopen(pubkey_path, "rb"); + if (!kf) { + fprintf(stderr, "eos-security: secure boot: cannot open public key '%s'\n", pubkey_path); + sb->status = EOS_BOOT_FAILED; + return -1; + } + size_t key_len = fread(pub_key.n, 1, sizeof(pub_key.n), kf); + fclose(kf); + if (key_len == 0) { + fprintf(stderr, "eos-security: secure boot: public key '%s' is empty\n", pubkey_path); + sb->status = EOS_BOOT_FAILED; + return -1; + } + if (eos_rsa_verify_sha256(&pub_key, digest, sig_buf, sig_len) == 0) { sb->status = EOS_BOOT_VERIFIED; return 0; diff --git a/core/eos/systems/CMakeLists.txt b/core/eos/systems/CMakeLists.txt index 5db3caf8..d932dcbb 100644 --- a/core/eos/systems/CMakeLists.txt +++ b/core/eos/systems/CMakeLists.txt @@ -8,4 +8,8 @@ add_library(eos_systems STATIC target_include_directories(eos_systems PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include ) -target_link_libraries(eos_systems PRIVATE eos_core eos_toolchains) +# eos_backends, not just at the consumers: systems/src/firmware.c includes +# eos/backend.h and calls eos_backend_find(), so this is where the dependency +# belongs. Declaring it at each consumer instead made every future user of +# eos_systems rediscover it -- which is how test_firmware came to name it. +target_link_libraries(eos_systems PRIVATE eos_core eos_toolchains eos_backends) diff --git a/core/eos/tests/CMakeLists.txt b/core/eos/tests/CMakeLists.txt index 5611e3b5..453b8dd7 100644 --- a/core/eos/tests/CMakeLists.txt +++ b/core/eos/tests/CMakeLists.txt @@ -114,3 +114,8 @@ target_include_directories(test_package PRIVATE ) target_link_libraries(test_package PRIVATE eos_pkg eos_core) add_test(NAME test_package COMMAND test_package) + +# --- test_devicetree: Flattened device tree parser (from eos dc13c59) --- +add_executable(test_devicetree test_devicetree.c) +target_link_libraries(test_devicetree PRIVATE eos_devicetree) +add_test(NAME test_devicetree COMMAND test_devicetree) diff --git a/core/eos/tests/test_config.c b/core/eos/tests/test_config.c index 3fb70814..12fff468 100644 --- a/core/eos/tests/test_config.c +++ b/core/eos/tests/test_config.c @@ -4,7 +4,10 @@ #include #include +#include +#include #include "eos/config.h" +#include "eos/lockfile.h" #include "eos/log.h" static int tests_run = 0; @@ -100,6 +103,230 @@ static void test_config_missing_file(void) { ASSERT(res == EOS_ERR_IO, "returns IO error for missing file"); } +/* + * Regression test for the out-of-bounds write in eos_config_load(): a + * config listing more than EOS_MAX_PACKAGES packages used to leave pkg_idx + * pointing past the end of cfg->packages[] while `section` stayed in + * SEC_PKG_ENTRY/SEC_PKG_BUILD/SEC_PKG_OPTIONS, so the *next* package's + * fields (version/build/options) were written through cfg->packages[pkg_idx] + * with pkg_idx == EOS_MAX_PACKAGES -- one element past the array, which in + * EosConfig is immediately followed by `int package_count`. A pre-fix build + * would clobber package_count (and beyond) here; this test pins the correct, + * safe behavior: extra packages are rejected and package_count stays capped. + */ +static void test_config_load_package_overflow(void) { + printf("test_config_load_package_overflow:\n"); + static EosConfig cfg; + + const int total_packages = EOS_MAX_PACKAGES + 3; + + FILE *fp = fopen("test_eos_overflow.yaml", "w"); + if (!fp) { + printf(" SKIP: cannot create test config file\n"); + return; + } + + fprintf(fp, "project:\n"); + fprintf(fp, " name: overflow-project\n"); + fprintf(fp, " version: 1.0.0\n"); + fprintf(fp, "\n"); + fprintf(fp, "packages:\n"); + for (int i = 0; i < total_packages; i++) { + fprintf(fp, " - name: pkg%d\n", i); + fprintf(fp, " version: 1.0.%d\n", i); + fprintf(fp, " build:\n"); + fprintf(fp, " type: cmake\n"); + fprintf(fp, " options:\n"); + fprintf(fp, " opt%d: val%d\n", i, i); + } + fclose(fp); + + EosResult res = eos_config_load(&cfg, "test_eos_overflow.yaml"); + + ASSERT(res == EOS_OK, "config with too many packages still loads"); + ASSERT(cfg.package_count == EOS_MAX_PACKAGES, + "package_count is capped at EOS_MAX_PACKAGES, not corrupted"); + + /* The first package must be entirely unaffected. */ + ASSERT(strcmp(cfg.packages[0].name, "pkg0") == 0, "first package name intact"); + ASSERT(strcmp(cfg.packages[0].version, "1.0.0") == 0, "first package version intact"); + + /* The last IN-BOUNDS package (index EOS_MAX_PACKAGES-1) must hold its + * own data, not data bled in from the rejected packages that follow. */ + int last = EOS_MAX_PACKAGES - 1; + char expected_name[EOS_MAX_NAME]; + char expected_version[EOS_MAX_NAME]; + snprintf(expected_name, sizeof(expected_name), "pkg%d", last); + snprintf(expected_version, sizeof(expected_version), "1.0.%d", last); + ASSERT(strcmp(cfg.packages[last].name, expected_name) == 0, + "last in-bounds package name is its own, not overwritten"); + ASSERT(strcmp(cfg.packages[last].version, expected_version) == 0, + "last in-bounds package version is its own, not overwritten"); + ASSERT(cfg.packages[last].option_count == 1, + "last in-bounds package option_count is its own, not overwritten"); + + remove("test_eos_overflow.yaml"); +} +static void test_lockfile_freshness(void) { + printf("test_lockfile_freshness:\n"); + static EosConfig cfg; + static EosLockfile lock; + + eos_config_init(&cfg); + snprintf(cfg.project.name, sizeof(cfg.project.name), "%s", "demo"); + snprintf(cfg.project.version, sizeof(cfg.project.version), "%s", "1.0.0"); + cfg.package_count = 2; + + snprintf(cfg.packages[0].name, sizeof(cfg.packages[0].name), "%s", "alpha"); + snprintf(cfg.packages[0].version, sizeof(cfg.packages[0].version), "%s", "1.2.3"); + snprintf(cfg.packages[0].source, sizeof(cfg.packages[0].source), "%s", "https://example.com/alpha.tar.gz"); + snprintf(cfg.packages[0].hash, sizeof(cfg.packages[0].hash), "%s", "explicit-checksum"); + cfg.packages[0].build_type = EOS_BUILD_CMAKE; + + snprintf(cfg.packages[1].name, sizeof(cfg.packages[1].name), "%s", "beta"); + snprintf(cfg.packages[1].version, sizeof(cfg.packages[1].version), "%s", "4.5.6"); + snprintf(cfg.packages[1].source, sizeof(cfg.packages[1].source), "%s", "https://example.com/beta.tar.gz"); + cfg.packages[1].build_type = EOS_BUILD_MAKE; + + ASSERT(eos_lockfile_generate(&lock, &cfg) == EOS_OK, "lockfile generates"); + ASSERT(eos_lockfile_is_current(&lock, &cfg), "generated lockfile is current"); + + EosLockEntry tmp = lock.entries[0]; + lock.entries[0] = lock.entries[1]; + lock.entries[1] = tmp; + ASSERT(eos_lockfile_is_current(&lock, &cfg), "package order does not affect freshness"); + tmp = lock.entries[0]; + lock.entries[0] = lock.entries[1]; + lock.entries[1] = tmp; + + snprintf(cfg.project.version, sizeof(cfg.project.version), "%s", "2.0.0"); + ASSERT(!eos_lockfile_is_current(&lock, &cfg), "project version change is stale"); + snprintf(cfg.project.version, sizeof(cfg.project.version), "%s", "1.0.0"); + + snprintf(cfg.packages[0].name, sizeof(cfg.packages[0].name), "%s", "renamed-alpha"); + ASSERT(!eos_lockfile_is_current(&lock, &cfg), "package name change is stale"); + snprintf(cfg.packages[0].name, sizeof(cfg.packages[0].name), "%s", "alpha"); + + snprintf(cfg.packages[0].version, sizeof(cfg.packages[0].version), "%s", "1.2.4"); + ASSERT(!eos_lockfile_is_current(&lock, &cfg), "requested version change is stale"); + snprintf(cfg.packages[0].version, sizeof(cfg.packages[0].version), "%s", "1.2.3"); + + snprintf(cfg.packages[0].source, sizeof(cfg.packages[0].source), "%s", "https://example.com/alpha-v2.tar.gz"); + ASSERT(!eos_lockfile_is_current(&lock, &cfg), "package source change is stale"); + snprintf(cfg.packages[0].source, sizeof(cfg.packages[0].source), "%s", "https://example.com/alpha.tar.gz"); + + snprintf(cfg.packages[0].hash, sizeof(cfg.packages[0].hash), "%s", "new-checksum"); + ASSERT(!eos_lockfile_is_current(&lock, &cfg), "explicit checksum change is stale"); + snprintf(cfg.packages[0].hash, sizeof(cfg.packages[0].hash), "%s", "explicit-checksum"); + + cfg.packages[0].build_type = EOS_BUILD_MAKE; + ASSERT(!eos_lockfile_is_current(&lock, &cfg), "build type change is stale"); + cfg.packages[0].build_type = EOS_BUILD_CMAKE; + + snprintf(lock.entries[0].resolved_version, sizeof(lock.entries[0].resolved_version), "%s", "1.2.4"); + ASSERT(!eos_lockfile_is_current(&lock, &cfg), "resolved version change is stale"); + snprintf(lock.entries[0].resolved_version, sizeof(lock.entries[0].resolved_version), "%s", "1.2.3"); + + lock.entries[1].hash[0] ^= 1; + ASSERT(!eos_lockfile_is_current(&lock, &cfg), "generated checksum change is stale"); +} + +static void test_package_after_dependencies(void) { + printf("test_package_after_dependencies:\n"); + + static EosConfig cfg; + + FILE *fp = fopen("test_package_dependencies.yaml", "w"); + if (!fp) { + printf(" SKIP: cannot create dependency test file\n"); + return; + } + + fprintf(fp, "packages:\n"); + fprintf(fp, " - name: application\n"); + fprintf(fp, " version: 1.0.0\n"); + fprintf(fp, " deps:\n"); + fprintf(fp, " - logging-library\n"); + fprintf(fp, " - name: network-library\n"); + fprintf(fp, " version: 2.0.0\n"); + + fclose(fp); + + EosResult res = eos_config_load( + &cfg, + "test_package_dependencies.yaml" + ); + + ASSERT(res == EOS_OK, + "configuration with package dependencies loads"); + + ASSERT(cfg.package_count == 2, + "two separate packages are parsed"); + + ASSERT(strcmp(cfg.packages[0].name, "application") == 0, + "first package name is correct"); + + ASSERT(cfg.packages[0].dep_count == 1, + "first package has exactly one dependency"); + + ASSERT(strcmp(cfg.packages[0].deps[0], "logging-library") == 0, + "dependency value is correct"); + + ASSERT(strcmp(cfg.packages[1].name, "network-library") == 0, + "package following dependency list is parsed"); + + ASSERT(strcmp(cfg.packages[1].version, "2.0.0") == 0, + "second package version is parsed"); + + remove("test_package_dependencies.yaml"); +} + +static void test_dependencies_after_build(void) { + printf("test_dependencies_after_build:\n"); + + static EosConfig cfg; + + FILE *fp = fopen("test_dependencies_after_build.yaml", "w"); + if (!fp) { + printf(" SKIP: cannot create build dependency test file\n"); + return; + } + + fprintf(fp, "packages:\n"); + fprintf(fp, " - name: busybox\n"); + fprintf(fp, " version: 1.36.1\n"); + fprintf(fp, " build:\n"); + fprintf(fp, " type: kbuild\n"); + fprintf(fp, " deps:\n"); + fprintf(fp, " - zlib\n"); + fclose(fp); + + EosResult res = eos_config_load( + &cfg, + "test_dependencies_after_build.yaml" + ); + + ASSERT(res == EOS_OK, + "configuration with dependencies after build loads"); + + ASSERT(cfg.package_count == 1, + "one package is parsed"); + + ASSERT(strcmp(cfg.packages[0].name, "busybox") == 0, + "package name is correct"); + + ASSERT(cfg.packages[0].build_type == EOS_BUILD_KBUILD, + "build type before dependencies is parsed"); + + ASSERT(cfg.packages[0].dep_count == 1, + "package has one dependency"); + + ASSERT(strcmp(cfg.packages[0].deps[0], "zlib") == 0, + "dependency after build is parsed"); + + remove("test_dependencies_after_build.yaml"); +} + int main(void) { eos_log_set_level(EOS_LOG_ERROR); @@ -107,7 +334,11 @@ int main(void) { test_config_init(); test_config_load(); + test_package_after_dependencies(); + test_dependencies_after_build(); test_config_missing_file(); + test_config_load_package_overflow(); + test_lockfile_freshness(); printf("\n=== Results: %d/%d passed ===\n", tests_passed, tests_run); return (tests_passed == tests_run) ? 0 : 1; diff --git a/core/eos/tests/test_devicetree.c b/core/eos/tests/test_devicetree.c new file mode 100644 index 00000000..d66b69dc --- /dev/null +++ b/core/eos/tests/test_devicetree.c @@ -0,0 +1,440 @@ +// SPDX-License-Identifier: MIT +// Copyright (c) 2026 EoS Project +// ISO/IEC 25000 | ISO/IEC/IEEE 15288:2023 + +/** + * @file test_devicetree.c + * @brief Flattened device tree parser: happy path and malformed-blob rejection + * + * A DTB reaches us from a prior boot stage or a flash region, so every offset + * and length inside it is untrusted input. The "malformed" cases below are + * regressions: each one used to read outside the blob (or outside the parser's + * own node stack) before the bounds checks were added. Running this suite under + * -fsanitize=address,undefined reproduces the original reports. + */ + +#include +#include +#include +#include "devicetree.h" + +static int tests_run = 0; +static int tests_passed = 0; + +#define ASSERT(expr, msg) \ + do { \ + tests_run++; \ + if (!(expr)) { \ + printf(" FAIL: %s (line %d)\n", msg, __LINE__); \ + } else { \ + tests_passed++; \ + printf(" PASS: %s\n", msg); \ + } \ + } while (0) + +/* ------------------------------------------------------------------ + * Minimal DTB builder + * ------------------------------------------------------------------ */ + +typedef struct { + uint8_t buf[1024]; + uint32_t len; +} Blob; + +static void b_reset(Blob *b) { b->len = 0; } + +static void b_u32(Blob *b, uint32_t v) +{ + b->buf[b->len++] = (uint8_t)(v >> 24); + b->buf[b->len++] = (uint8_t)(v >> 16); + b->buf[b->len++] = (uint8_t)(v >> 8); + b->buf[b->len++] = (uint8_t)v; +} + +/* Append a NUL-terminated string, then pad to a 4-byte boundary. */ +static void b_str_pad(Blob *b, const char *s) +{ + size_t n = strlen(s) + 1; + memcpy(b->buf + b->len, s, n); + b->len += (uint32_t)n; + while (b->len & 3u) b->buf[b->len++] = 0; +} + +/* Append a NUL-terminated string with no padding (strings block). */ +static uint32_t b_str(Blob *b, const char *s) +{ + uint32_t off = b->len; + size_t n = strlen(s) + 1; + memcpy(b->buf + b->len, s, n); + b->len += (uint32_t)n; + return off; +} + +static void put32(uint8_t *p, uint32_t v) +{ + p[0] = (uint8_t)(v >> 24); p[1] = (uint8_t)(v >> 16); + p[2] = (uint8_t)(v >> 8); p[3] = (uint8_t)v; +} + +/* + * Compose a well-formed v17 blob from a struct block and a strings block. + * Returns a heap buffer so ASan's redzones sit immediately after the blob and + * any over-read is caught precisely. + */ +static uint8_t *compose(const Blob *st, const Blob *str, uint32_t *out_size) +{ + uint32_t off_rsv = EOS_DT_HEADER_SIZE; + uint32_t off_struct = off_rsv + 8u; /* one empty reserve entry */ + uint32_t off_string = off_struct + st->len; + uint32_t total = off_string + str->len; + + uint8_t *b = calloc(1, total); + put32(b + 0, EOS_DT_MAGIC); + put32(b + 4, total); + put32(b + 8, off_struct); + put32(b + 12, off_string); + put32(b + 16, off_rsv); + put32(b + 20, 17); /* version */ + put32(b + 24, 16); /* last_comp_version */ + put32(b + 28, 0); /* boot_cpuid_phys */ + put32(b + 32, str->len); /* size_dt_strings */ + put32(b + 36, st->len); /* size_dt_struct */ + + memcpy(b + off_struct, st->buf, st->len); + memcpy(b + off_string, str->buf, str->len); + *out_size = total; + return b; +} + +/* + * A small but realistic tree: + * + * / { + * model = "EoS Test Board"; + * compatible = "eos,test-board"; + * uart@40011000 { + * reg = <0x40011000 0x400>; + * interrupts = <37 38>; + * phandle = <1>; + * }; + * }; + */ +static uint8_t *build_valid_dtb(uint32_t *out_size) +{ + static Blob st, str; + b_reset(&st); b_reset(&str); + + uint32_t s_model = b_str(&str, "model"); + uint32_t s_compat = b_str(&str, "compatible"); + uint32_t s_reg = b_str(&str, "reg"); + uint32_t s_irq = b_str(&str, "interrupts"); + uint32_t s_phandle = b_str(&str, "phandle"); + + b_u32(&st, EOS_DT_BEGIN_NODE); + b_str_pad(&st, ""); /* root has an empty name */ + + b_u32(&st, EOS_DT_PROP); + b_u32(&st, 15); b_u32(&st, s_model); + b_str_pad(&st, "EoS Test Board"); + + b_u32(&st, EOS_DT_PROP); + b_u32(&st, 15); b_u32(&st, s_compat); + b_str_pad(&st, "eos,test-board"); + + b_u32(&st, EOS_DT_BEGIN_NODE); + b_str_pad(&st, "uart@40011000"); + + b_u32(&st, EOS_DT_PROP); + b_u32(&st, 8); b_u32(&st, s_reg); + b_u32(&st, 0x40011000); b_u32(&st, 0x400); + + b_u32(&st, EOS_DT_PROP); + b_u32(&st, 8); b_u32(&st, s_irq); + b_u32(&st, 37); b_u32(&st, 38); + + b_u32(&st, EOS_DT_PROP); + b_u32(&st, 4); b_u32(&st, s_phandle); + b_u32(&st, 1); + + b_u32(&st, EOS_DT_END_NODE); /* uart */ + b_u32(&st, EOS_DT_END_NODE); /* root */ + b_u32(&st, EOS_DT_END); + + return compose(&st, &str, out_size); +} + +/* ------------------------------------------------------------------ + * Happy path + * ------------------------------------------------------------------ */ + +static void test_parse_valid_blob(void) +{ + printf("test_parse_valid_blob:\n"); + uint32_t size = 0; + uint8_t *dtb = build_valid_dtb(&size); + EosDeviceTree *dt = malloc(sizeof(*dt)); + + ASSERT(eos_dt_parse(dt, dtb, size) == 0, "well-formed blob parses"); + ASSERT(eos_dt_node_count(dt) == 2, "root plus one child node"); + ASSERT(dt->version == 17, "version read from header"); + ASSERT(strcmp(dt->model, "EoS Test Board") == 0, "model cached at root"); + ASSERT(strcmp(dt->compatible, "eos,test-board") == 0, + "compatible cached at root"); + + free(dt); free(dtb); +} + +static void test_lookup_and_property_access(void) +{ + printf("test_lookup_and_property_access:\n"); + uint32_t size = 0; + uint8_t *dtb = build_valid_dtb(&size); + EosDeviceTree *dt = malloc(sizeof(*dt)); + eos_dt_parse(dt, dtb, size); + + ASSERT(eos_dt_find(dt, "/") == dt->root, "root resolves"); + + EosDtNode *uart = eos_dt_find(dt, "/uart@40011000"); + ASSERT(uart != NULL, "child node resolves by path"); + ASSERT(eos_dt_find(dt, "/nonexistent") == NULL, "unknown path returns NULL"); + + if (uart) { + ASSERT(uart->parent == dt->root, "child links back to its parent"); + ASSERT(eos_dt_get_prop(uart, "reg") != NULL, "reg property present"); + ASSERT(eos_dt_get_prop(uart, "absent") == NULL, "absent property is NULL"); + + uint32_t addr = 0, sz = 0; + ASSERT(eos_dt_get_reg(uart, &addr, &sz) == 0, "reg decodes"); + ASSERT(addr == 0x40011000u && sz == 0x400u, "reg holds address and size"); + + ASSERT(eos_dt_get_irq(uart, 0) == 37, "first interrupt decodes"); + ASSERT(eos_dt_get_irq(uart, 1) == 38, "second interrupt decodes"); + ASSERT(eos_dt_get_irq(uart, 2) == -1, "index past the array is rejected"); + ASSERT(eos_dt_get_irq(uart, -1) == -1, "negative index is rejected"); + ASSERT(eos_dt_get_irq(uart, 0x40000000) == -1, + "index that would overflow the offset is rejected"); + + uint32_t ph = 0; + ASSERT(eos_dt_get_u32(uart, "phandle", &ph) == 0 && ph == 1u, + "u32 property decodes"); + ASSERT(eos_dt_find_by_phandle(dt, 1) == uart, "phandle lookup works"); + } + + ASSERT(eos_dt_find_compatible(dt, "eos,test-board") == dt->root, + "compatible lookup finds the root"); + ASSERT(eos_dt_find_compatible(dt, "vendor,absent") == NULL, + "unknown compatible returns NULL"); + + char buf[32]; + ASSERT(eos_dt_get_string(dt->root, "model", buf, (int)sizeof(buf)) == 0 && + strcmp(buf, "EoS Test Board") == 0, "string property copies out"); + + free(dt); free(dtb); +} + +/* ------------------------------------------------------------------ + * Malformed input — each of these was an out-of-bounds read + * ------------------------------------------------------------------ */ + +/* Build a bare 40-byte header with caller-chosen offsets. */ +static uint8_t *lone_header(uint32_t total, uint32_t off_struct, + uint32_t off_string) +{ + uint8_t *b = calloc(1, EOS_DT_HEADER_SIZE); + put32(b + 0, EOS_DT_MAGIC); + put32(b + 4, total); + put32(b + 8, off_struct); + put32(b + 12, off_string); + put32(b + 20, 17); + return b; +} + +static void test_rejects_malformed_blobs(void) +{ + printf("test_rejects_malformed_blobs:\n"); + EosDeviceTree *dt = malloc(sizeof(*dt)); + + ASSERT(eos_dt_parse(NULL, NULL, 0) == -1, "NULL arguments rejected"); + + { /* Shorter than a header. */ + uint8_t tiny[8] = {0}; + ASSERT(eos_dt_parse(dt, tiny, sizeof(tiny)) == -1, + "blob shorter than the header is rejected"); + } + + { /* Wrong magic. */ + uint8_t *b = lone_header(EOS_DT_HEADER_SIZE, EOS_DT_HEADER_SIZE, EOS_DT_HEADER_SIZE); + put32(b, 0xDEADBEEF); + ASSERT(eos_dt_parse(dt, b, EOS_DT_HEADER_SIZE) == -1, "bad magic is rejected"); + free(b); + } + + { /* off_struct past the end of the buffer. + `size - off_struct` used to wrap, giving the walk a huge bound. */ + uint8_t *b = lone_header(EOS_DT_HEADER_SIZE, 4096, 4096); + ASSERT(eos_dt_parse(dt, b, EOS_DT_HEADER_SIZE) == -1, + "struct offset past the buffer is rejected"); + free(b); + } + + { /* totalsize larger than the buffer we were handed. */ + uint8_t *b = lone_header(0x10000, EOS_DT_HEADER_SIZE, EOS_DT_HEADER_SIZE); + ASSERT(eos_dt_parse(dt, b, EOS_DT_HEADER_SIZE) == -1, + "totalsize larger than the buffer is rejected"); + free(b); + } + + { /* BEGIN_NODE whose name has no terminator before the blob ends. + strlen() used to run past the allocation. */ + uint32_t total = 48; + uint8_t *b = calloc(1, total); + put32(b + 0, EOS_DT_MAGIC); put32(b + 4, total); + put32(b + 8, 40); put32(b + 12, 44); put32(b + 20, 17); + put32(b + 40, EOS_DT_BEGIN_NODE); + memset(b + 44, 'A', 4); /* four non-NUL bytes, then the end */ + ASSERT(eos_dt_parse(dt, b, total) == -1, + "unterminated node name is rejected"); + free(b); + } + + { /* PROP with a name offset far outside the strings block. */ + uint32_t total = 64; + uint8_t *b = calloc(1, total); + put32(b + 0, EOS_DT_MAGIC); put32(b + 4, total); + put32(b + 8, 40); put32(b + 12, 44); put32(b + 20, 17); + put32(b + 40, EOS_DT_BEGIN_NODE); /* root, empty name + padding */ + put32(b + 48, EOS_DT_PROP); + put32(b + 52, 4); + put32(b + 56, 0x40000000); /* nameoff far out of range */ + put32(b + 60, 0xDEADBEEF); + ASSERT(eos_dt_parse(dt, b, total) == -1, + "property name offset outside the strings block is rejected"); + free(b); + } + + { /* Nesting deeper than the parser's node stack. + stack[depth - 1] used to read past the end of the local array. */ + uint32_t depth = EOS_DT_MAX_DEPTH + 8u; + uint32_t total = EOS_DT_HEADER_SIZE + depth * 8u + 8u; + uint8_t *b = calloc(1, total); + put32(b + 0, EOS_DT_MAGIC); put32(b + 4, total); + put32(b + 8, EOS_DT_HEADER_SIZE); put32(b + 12, total - 4); + put32(b + 20, 17); + uint32_t p = EOS_DT_HEADER_SIZE; + for (uint32_t i = 0; i < depth; i++) { + put32(b + p, EOS_DT_BEGIN_NODE); p += 4; + put32(b + p, 0); p += 4; /* empty name + padding */ + } + put32(b + p, EOS_DT_END); + ASSERT(eos_dt_parse(dt, b, total) == -1, + "nesting deeper than the node stack is rejected"); + free(b); + } + + { /* END_NODE with nothing open. */ + uint32_t total = 48; + uint8_t *b = calloc(1, total); + put32(b + 0, EOS_DT_MAGIC); put32(b + 4, total); + put32(b + 8, 40); put32(b + 12, 44); put32(b + 20, 17); + put32(b + 40, EOS_DT_END_NODE); + put32(b + 44, EOS_DT_END); + ASSERT(eos_dt_parse(dt, b, total) == -1, "unbalanced END_NODE is rejected"); + free(b); + } + + { /* Property length running past the end of the struct block. */ + uint32_t total = 64; + uint8_t *b = calloc(1, total); + put32(b + 0, EOS_DT_MAGIC); put32(b + 4, total); + put32(b + 8, 40); put32(b + 12, 60); put32(b + 20, 17); + put32(b + 40, EOS_DT_BEGIN_NODE); + put32(b + 48, EOS_DT_PROP); + put32(b + 52, 0xFFFF0000); /* absurd value length */ + put32(b + 56, 0); + ASSERT(eos_dt_parse(dt, b, total) == -1, + "property length past the struct block is rejected"); + free(b); + } + + { /* Truncated: no FDT_END terminator. */ + uint32_t total = 48; + uint8_t *b = calloc(1, total); + put32(b + 0, EOS_DT_MAGIC); put32(b + 4, total); + put32(b + 8, 40); put32(b + 12, 44); put32(b + 20, 17); + put32(b + 40, EOS_DT_BEGIN_NODE); + ASSERT(eos_dt_parse(dt, b, total) == -1, "missing FDT_END is rejected"); + free(b); + } + + free(dt); +} + +/* + * Every prefix of a valid blob is malformed. The parser has to reject each one + * without reading past the (shortened) buffer — this is the cheap deterministic + * stand-in for the fuzz target, and it runs everywhere. + */ +static void test_truncations_never_overrun(void) +{ + printf("test_truncations_never_overrun:\n"); + uint32_t size = 0; + uint8_t *full = build_valid_dtb(&size); + EosDeviceTree *dt = malloc(sizeof(*dt)); + + int rejected_all = 1; + for (uint32_t n = 0; n < size; n++) { + uint8_t *cut = malloc(n ? n : 1); + memcpy(cut, full, n); + /* Patch totalsize down so the blob is self-consistent about its size. */ + if (n >= EOS_DT_HEADER_SIZE) put32(cut + 4, n); + if (eos_dt_parse(dt, cut, n) == 0) rejected_all = 0; + free(cut); + } + ASSERT(rejected_all, "every truncation of a valid blob is rejected"); + + /* And the untruncated blob still parses, so the loop above proves something. */ + ASSERT(eos_dt_parse(dt, full, size) == 0, "the untruncated blob still parses"); + + free(dt); free(full); +} + +/* + * Flip one byte at a time across the whole blob. The result may parse or may be + * rejected — what matters is that it never reads outside the buffer. Under ASan + * this is where the original parser fell over. + */ +static void test_single_byte_corruption_is_safe(void) +{ + printf("test_single_byte_corruption_is_safe:\n"); + uint32_t size = 0; + uint8_t *full = build_valid_dtb(&size); + EosDeviceTree *dt = malloc(sizeof(*dt)); + + for (uint32_t i = 0; i < size; i++) { + static const uint8_t patterns[] = { 0x00, 0x01, 0x7F, 0x80, 0xFF }; + for (size_t k = 0; k < sizeof(patterns); k++) { + uint8_t *copy = malloc(size); + memcpy(copy, full, size); + copy[i] = patterns[k]; + (void)eos_dt_parse(dt, copy, size); /* must not read out of bounds */ + free(copy); + } + } + ASSERT(1, "single-byte corruption never reads outside the blob"); + + free(dt); free(full); +} + +int main(void) +{ + printf("=== EoS Device Tree Parser Tests ===\n\n"); + + test_parse_valid_blob(); + test_lookup_and_property_access(); + test_rejects_malformed_blobs(); + test_truncations_never_overrun(); + test_single_byte_corruption_is_safe(); + + printf("\n%d/%d assertions passed\n", tests_passed, tests_run); + return (tests_passed == tests_run) ? 0 : 1; +} diff --git a/core/eos/tests/test_firmware.c b/core/eos/tests/test_firmware.c index 7134a51d..b931ba40 100644 --- a/core/eos/tests/test_firmware.c +++ b/core/eos/tests/test_firmware.c @@ -75,7 +75,7 @@ static void test_arm_cortex_arch(void) { } static void test_firmware_init(void) { - EosConfig cfg; + static EosConfig cfg; /* ~4 MB: MSVC's 1 MB default stack cannot hold it */ memset(&cfg, 0, sizeof(cfg)); strncpy(cfg.workspace.build_dir, ".eos/build", EOS_MAX_PATH - 1); strncpy(cfg.toolchain.rtos_target, "arm-none-eabi", EOS_MAX_NAME - 1); @@ -100,7 +100,7 @@ static void test_firmware_init(void) { } static void test_firmware_dry_run(void) { - EosConfig cfg; + static EosConfig cfg; /* ~4 MB: MSVC's 1 MB default stack cannot hold it */ memset(&cfg, 0, sizeof(cfg)); strncpy(cfg.workspace.build_dir, ".eos/build", EOS_MAX_PATH - 1); @@ -119,7 +119,7 @@ static void test_firmware_dry_run(void) { } static void test_hybrid_init(void) { - EosConfig cfg; + static EosConfig cfg; /* ~4 MB: MSVC's 1 MB default stack cannot hold it */ memset(&cfg, 0, sizeof(cfg)); strncpy(cfg.workspace.build_dir, ".eos/build", EOS_MAX_PATH - 1); cfg.system.kind = EOS_SYSTEM_HYBRID; @@ -154,7 +154,7 @@ static void test_firmware_graph_node(void) { } static void test_config_load_rtos(void) { - EosConfig cfg; + static EosConfig cfg; /* ~4 MB: MSVC's 1 MB default stack cannot hold it */ EosResult res = eos_config_load(&cfg, "examples/motor-controller/eos.yaml"); if (res != EOS_OK) { printf("(skip - example file not found) "); @@ -168,7 +168,7 @@ static void test_config_load_rtos(void) { } static void test_config_load_hybrid(void) { - EosConfig cfg; + static EosConfig cfg; /* ~4 MB: MSVC's 1 MB default stack cannot hold it */ EosResult res = eos_config_load(&cfg, "examples/industrial-gateway/eos.yaml"); if (res != EOS_OK) { printf("(skip - example file not found) "); diff --git a/core/eos/tests/test_hal.c b/core/eos/tests/test_hal.c index 0767868f..fe2a2b8b 100644 --- a/core/eos/tests/test_hal.c +++ b/core/eos/tests/test_hal.c @@ -150,14 +150,66 @@ TEST(test_no_backend) { ASSERT(eos_get_tick_ms() == 0); } + +/* ── Platform backend selection ─────────────────────────────────────────── + * + * hal.h promises initialisation "for the current platform". It used to return + * -1 unless the application had already called eos_hal_register_backend() + * itself — which nothing in the tree, none of the examples and none of the + * scaffolded templates did. Every HAL call on a host build silently did + * nothing, and the generated hello-world produced no output. + */ + +static int g_custom_init_calls; + +static int custom_init(void) { g_custom_init_calls++; return 0; } + +static const eos_hal_backend_t custom_backend = { + .name = "custom", + .init = custom_init, +}; + +TEST(test_init_selects_the_platform_backend) { + /* A program that has registered nothing still gets a working HAL. */ + ASSERT(eos_hal_init() == 0); + eos_hal_deinit(); +} + +TEST(test_tick_source_advances) { + /* The one facility every platform must provide. A backend that was never + * selected reports 0 forever, which reads as a stopped clock rather than + * as a missing backend. */ + ASSERT(eos_hal_init() == 0); + uint32_t before = eos_get_tick_ms(); + eos_delay_ms(20); + ASSERT(eos_get_tick_ms() >= before + 10); + eos_hal_deinit(); +} + +TEST(test_an_application_backend_is_not_overridden) { + /* The default fills an empty slot; it does not outrank a choice. */ + g_custom_init_calls = 0; + eos_hal_register_backend(&custom_backend); + ASSERT(eos_hal_init() == 0); + ASSERT(g_custom_init_calls == 1); + eos_hal_register_backend(NULL); +} + int main(void) { printf("=== EoS: HAL Unit Tests ===\n\n"); + /* These two must come first. Every later test calls setup(), which + * registers a mock backend; run after it, they would exercise the mock + * and quietly stop testing platform selection at all. */ + run_test_init_selects_the_platform_backend(); + run_test_tick_source_advances(); + run_test_init_deinit(); run_test_gpio_write_read(); run_test_gpio_toggle(); run_test_tick_counter(); + run_test_an_application_backend_is_not_overridden(); run_test_no_backend(); - tests_run = 5; + tests_run = 8; printf("\n%d/%d tests passed\n", tests_passed, tests_run); return (tests_passed == tests_run) ? 0 : 1; } diff --git a/core/eos/tests/test_net.c b/core/eos/tests/test_net.c index d18e7816..3af6cfb4 100644 --- a/core/eos/tests/test_net.c +++ b/core/eos/tests/test_net.c @@ -1,7 +1,7 @@ // SPDX-License-Identifier: MIT #include +#include #include -#include #define EOS_ENABLE_NET 1 #include "eos/eos_config.h" #include "eos/net.h" @@ -9,15 +9,27 @@ static int passed = 0; #define PASS(name) do { printf("[PASS] %s\n", name); passed++; } while(0) +/* Not assert(): CI builds Release, and NDEBUG deletes assert() together with + * the expression inside it. Half these checks wrap the call under test -- + * assert(eos_net_connect(...) == 0) stopped connecting at all, the echo + * thread blocked in accept() forever, and pthread_join() hung the suite. A + * check macro must always evaluate its expression. */ +#define CHECK(cond) do { \ + if (!(cond)) { \ + fprintf(stderr, "[FAIL] %s:%d: %s\n", __FILE__, __LINE__, #cond); \ + exit(1); \ + } \ +} while (0) + static void test_net_init(void) { - assert(eos_net_init() == 0); + CHECK(eos_net_init() == 0); eos_net_deinit(); PASS("net init/deinit"); } static void test_net_socket_tcp(void) { eos_net_init(); eos_socket_t s = eos_net_socket(EOS_NET_TCP); - assert(s != EOS_SOCKET_INVALID); + CHECK(s != EOS_SOCKET_INVALID); eos_net_close(s); eos_net_deinit(); PASS("net socket TCP"); @@ -25,15 +37,25 @@ static void test_net_socket_tcp(void) { static void test_net_socket_udp(void) { eos_net_init(); eos_socket_t s = eos_net_socket(EOS_NET_UDP); - assert(s != EOS_SOCKET_INVALID); + CHECK(s != EOS_SOCKET_INVALID); eos_net_close(s); eos_net_deinit(); PASS("net socket UDP"); } +static void test_net_socket_invalid_protocol(void) { + eos_net_init(); + + eos_socket_t s = eos_net_socket((eos_net_proto_t)99); + + CHECK(s == EOS_SOCKET_INVALID); + + eos_net_deinit(); + PASS("net socket invalid protocol"); +} static void test_net_bind(void) { eos_net_init(); eos_socket_t s = eos_net_socket(EOS_NET_TCP); - assert(eos_net_bind(s, 8080) == 0); + CHECK(eos_net_bind(s, 8080) == 0); eos_net_close(s); eos_net_deinit(); PASS("net bind"); @@ -42,7 +64,7 @@ static void test_net_listen(void) { eos_net_init(); eos_socket_t s = eos_net_socket(EOS_NET_TCP); eos_net_bind(s, 9090); - assert(eos_net_listen(s, 5) == 0); + CHECK(eos_net_listen(s, 5) == 0); eos_net_close(s); eos_net_deinit(); PASS("net listen"); @@ -50,13 +72,13 @@ static void test_net_listen(void) { static void test_net_close(void) { eos_net_init(); eos_socket_t s = eos_net_socket(EOS_NET_TCP); - assert(eos_net_close(s) == 0); + CHECK(eos_net_close(s) == 0); eos_net_deinit(); PASS("net close"); } static void test_net_close_invalid(void) { eos_net_init(); - assert(eos_net_close(EOS_SOCKET_INVALID) != 0); + CHECK(eos_net_close(EOS_SOCKET_INVALID) != 0); eos_net_deinit(); PASS("net close invalid"); } @@ -71,7 +93,7 @@ static void test_net_resolve(void) { static void test_net_resolve_null(void) { eos_net_init(); int r = eos_net_resolve(NULL, NULL); - assert(r != 0); + CHECK(r != 0); eos_net_deinit(); PASS("net resolve null"); } @@ -138,11 +160,105 @@ static void test_mdns_init(void) { eos_net_deinit(); PASS("mdns init/deinit"); } + +/* ── POSIX socket layer (ADR-014 point 3) ───────────────────────────────── + * + * Everything above tests the facade's argument validation, which passed just + * as well when eos_net_connect() was `return -1`. These exercise a real + * connection, so they compile only where a host OS supplies the sockets. + */ +#ifdef EOS_NET_POSIX +#include +#include +#include + +static uint16_t g_test_port = 45771; + +static void *echo_server(void *arg) { + int *ready = (int *)arg; + eos_socket_t s = eos_net_socket(EOS_NET_TCP); + if (s == EOS_SOCKET_INVALID || eos_net_bind(s, g_test_port) != 0) { + *ready = -1; + return NULL; + } + eos_net_listen(s, 1); + *ready = 1; + eos_net_addr_t peer; + eos_socket_t c = eos_net_accept(s, &peer); + if (c != EOS_SOCKET_INVALID) { + char buf[32]; + memset(buf, 0, sizeof(buf)); + int n = eos_net_recv(c, buf, sizeof(buf) - 1, 2000); + if (n > 0) eos_net_send(c, buf, (size_t)n); + eos_net_close(c); + } + eos_net_close(s); + return NULL; +} + +static void test_net_tcp_round_trip(void) { + eos_net_init(); + int ready = 0; + pthread_t t; + pthread_create(&t, NULL, echo_server, &ready); + /* Wait for the listener rather than sleeping a fixed interval, so this + * does not go flaky on a loaded machine. */ + for (int i = 0; i < 400 && ready == 0; i++) { + struct timespec ts; + ts.tv_sec = 0; + ts.tv_nsec = 5 * 1000 * 1000; + nanosleep(&ts, NULL); + } + CHECK(ready == 1); + eos_socket_t c = eos_net_socket(EOS_NET_TCP); + eos_net_addr_t addr; + addr.ip = inet_addr("127.0.0.1"); + addr.port = g_test_port; + CHECK(eos_net_connect(c, &addr) == 0); + CHECK(eos_net_send(c, "ping", 4) == 4); + char buf[32]; + memset(buf, 0, sizeof(buf)); + CHECK(eos_net_recv(c, buf, sizeof(buf) - 1, 2000) == 4); + CHECK(strcmp(buf, "ping") == 0); + eos_net_close(c); + pthread_join(t, NULL); + eos_net_deinit(); + PASS("net tcp round trip"); +} + +static void test_net_resolve_localhost(void) { + eos_net_init(); + uint32_t ip = 0; + CHECK(eos_net_resolve("localhost", &ip) == 0); + CHECK(ip == inet_addr("127.0.0.1")); + /* A name that cannot resolve must fail rather than reporting success and + * leaving the caller with whatever was in the output already. */ + uint32_t before = ip; + CHECK(eos_net_resolve("no.such.host.invalid", &ip) == -1); + CHECK(ip == before); + eos_net_deinit(); + PASS("net resolve localhost"); +} + +static void test_net_connect_refused(void) { + eos_net_init(); + eos_socket_t c = eos_net_socket(EOS_NET_TCP); + eos_net_addr_t addr; + addr.ip = inet_addr("127.0.0.1"); + addr.port = 1; /* nothing listens on port 1 */ + CHECK(eos_net_connect(c, &addr) != 0); + eos_net_close(c); + eos_net_deinit(); + PASS("net connect to a closed port fails"); +} +#endif /* EOS_NET_POSIX */ + int main(void) { printf("=== EoS Networking Tests ===\n"); test_net_init(); test_net_socket_tcp(); test_net_socket_udp(); + test_net_socket_invalid_protocol(); test_net_bind(); test_net_listen(); test_net_close(); @@ -155,6 +271,11 @@ int main(void) { test_http_post(); test_mqtt_connect(); test_mdns_init(); +#ifdef EOS_NET_POSIX + test_net_tcp_round_trip(); + test_net_resolve_localhost(); + test_net_connect_refused(); +#endif printf("\n=== ALL %d NET TESTS PASSED ===\n", passed); return 0; } diff --git a/core/eos/tests/test_ota.c b/core/eos/tests/test_ota.c index 784ef438..0f7a0636 100644 --- a/core/eos/tests/test_ota.c +++ b/core/eos/tests/test_ota.c @@ -4,6 +4,8 @@ #include #include +#include +#include #include #include "eos/ota.h" #include "eos/crypto.h" @@ -11,6 +13,49 @@ static int g_progress = 0; static void progress_cb(uint8_t pct, void *ctx) { g_progress = pct; (void)ctx; } + +/* ---- Authenticator stubs ------------------------------------------------- + * The platform installs one of these; the OTA service must not install a + * default that says yes. The signature bytes are opaque here -- what matters + * is that apply() consults the callback and honours its answer. */ + +static int g_auth_calls; +static uint8_t g_auth_digest[32]; + +static int auth_accept(const uint8_t digest[32], const uint8_t *sig, + size_t sig_len, void *ctx) { + (void)sig; (void)sig_len; (void)ctx; + g_auth_calls++; + memcpy(g_auth_digest, digest, 32); + return 0; +} + +static int auth_reject(const uint8_t digest[32], const uint8_t *sig, + size_t sig_len, void *ctx) { + (void)digest; (void)sig; (void)sig_len; (void)ctx; + g_auth_calls++; + return -1; +} + +static void sha256_of(const void *data, size_t len, uint8_t out[32]) { + EosSha256 ctx; + eos_sha256_init(&ctx); + eos_sha256_update(&ctx, data, len); + eos_sha256_final(&ctx, out); +} + +/* Download an image end to end, stopping just before apply(). */ +static void stage_update(const uint8_t *fw, size_t len, const uint8_t hash[32]) { + eos_ota_source_t src; + memset(&src, 0, sizeof(src)); + src.expected_size = (uint32_t)len; + memcpy(src.expected_sha256, hash, 32); + assert(eos_ota_begin(&src) == 0); + assert(eos_ota_write_chunk(fw, len) == 0); + assert(eos_ota_finish() == 0); + assert(eos_ota_verify() == 0); +} + static void test_ota_init(void) { assert(eos_ota_init() == 0); assert(eos_ota_get_active_slot() == EOS_OTA_SLOT_A); @@ -25,7 +70,7 @@ static void test_ota_full_update(void) { eos_ota_source_t src; memset(&src, 0, sizeof(src)); src.expected_size = sizeof(fw); memcpy(src.expected_sha256, hash, 32); - strcpy(src.version, "2.0"); + snprintf(src.version, sizeof(src.version), "2.0"); bool avail = false; assert(eos_ota_check_update(&src, &avail) == 0); assert(avail); @@ -35,6 +80,7 @@ static void test_ota_full_update(void) { assert(eos_ota_write_chunk(fw + 32, 32) == 0); assert(eos_ota_finish() == 0); assert(eos_ota_verify() == 0); + assert(eos_ota_set_authenticator(auth_accept, NULL) == 0); assert(eos_ota_apply() == 0); assert(eos_ota_get_active_slot() == EOS_OTA_SLOT_B); eos_ota_deinit(); @@ -66,7 +112,8 @@ static void test_ota_rollback(void) { eos_ota_write_chunk(fw, 8); eos_ota_finish(); eos_ota_verify(); - eos_ota_apply(); + eos_ota_set_authenticator(auth_accept, NULL); + assert(eos_ota_apply() == 0); assert(eos_ota_get_active_slot() == EOS_OTA_SLOT_B); assert(eos_ota_rollback() == 0); assert(eos_ota_get_active_slot() == EOS_OTA_SLOT_A); @@ -77,6 +124,7 @@ static void test_ota_rollback(void) { static void test_ota_status(void) { eos_ota_init(); eos_ota_status_t st; + (void)st; assert(eos_ota_get_status(&st) == 0); assert(st.active_slot == EOS_OTA_SLOT_A); assert(eos_ota_get_status(NULL) == -1); @@ -84,6 +132,112 @@ static void test_ota_status(void) { printf("[PASS] ota status\n"); } +/* expected_sha256 arrives with the update, so an attacker who swaps the image + * swaps the digest too. Passing eos_ota_verify() must not be enough to install. */ +static void test_ota_apply_refuses_without_authenticator(void) { + eos_ota_init(); + uint8_t fw[32]; memset(fw, 0xDD, sizeof(fw)); + uint8_t hash[32]; sha256_of(fw, sizeof(fw), hash); + + stage_update(fw, sizeof(fw), hash); + + assert(eos_ota_apply() == -1); + assert(eos_ota_get_active_slot() == EOS_OTA_SLOT_A); + eos_ota_deinit(); + printf("[PASS] ota apply refuses without authenticator\n"); +} + +static void test_ota_apply_honours_a_rejecting_authenticator(void) { + eos_ota_init(); + uint8_t fw[32]; memset(fw, 0xEE, sizeof(fw)); + uint8_t hash[32]; sha256_of(fw, sizeof(fw), hash); + + g_auth_calls = 0; + stage_update(fw, sizeof(fw), hash); + assert(eos_ota_set_authenticator(auth_reject, NULL) == 0); + + assert(eos_ota_apply() == -1); + assert(g_auth_calls == 1); + assert(eos_ota_get_active_slot() == EOS_OTA_SLOT_A); + eos_ota_deinit(); + printf("[PASS] ota apply honours a rejecting authenticator\n"); +} + +/* The authenticator has to see the digest the service computed over the bytes + * it received, not the one the update declared -- otherwise the signature + * would cover an attacker-chosen value. */ +static void test_ota_authenticator_sees_the_computed_digest(void) { + eos_ota_init(); + uint8_t fw[48]; memset(fw, 0x5A, sizeof(fw)); + uint8_t hash[32]; sha256_of(fw, sizeof(fw), hash); + + g_auth_calls = 0; + memset(g_auth_digest, 0, sizeof(g_auth_digest)); + stage_update(fw, sizeof(fw), hash); + assert(eos_ota_set_authenticator(auth_accept, NULL) == 0); + assert(eos_ota_apply() == 0); + + assert(g_auth_calls == 1); + assert(memcmp(g_auth_digest, hash, 32) == 0); + eos_ota_deinit(); + printf("[PASS] ota authenticator sees the computed digest\n"); +} + +/* A slot that was downloaded and hashed but never applied still holds a + * half-installed image. Marking it valid at verify() time made it a legal + * rollback target. */ +static void test_ota_unapplied_slot_is_not_a_rollback_target(void) { + eos_ota_init(); + uint8_t fw[16]; memset(fw, 0x77, sizeof(fw)); + uint8_t hash[32]; sha256_of(fw, sizeof(fw), hash); + + stage_update(fw, sizeof(fw), hash); + assert(eos_ota_apply() == -1); /* no authenticator */ + + assert(eos_ota_rollback() == -1); + assert(eos_ota_set_active_slot(EOS_OTA_SLOT_B) == -1); + assert(eos_ota_get_active_slot() == EOS_OTA_SLOT_A); + eos_ota_deinit(); + printf("[PASS] ota unapplied slot is not a rollback target\n"); +} + +static void test_ota_write_chunk_overflow(void) { + eos_ota_init(); + uint8_t fw[64]; + memset(fw, 0xAA, sizeof(fw)); + eos_ota_source_t src; + memset(&src, 0, sizeof(src)); + src.expected_size = sizeof(fw); + assert(eos_ota_begin(&src) == 0); + assert(eos_ota_write_chunk(fw, 32) == 0); + assert(eos_ota_write_chunk(fw, 33) == -1); + + eos_ota_status_t st; + assert(eos_ota_get_status(&st) == 0); + assert(st.bytes_received == 32); + eos_ota_deinit(); + printf("[PASS] ota write chunk overflow\n"); +} + +static void test_ota_write_chunk_wrap(void) { + eos_ota_init(); + uint8_t fw[64]; + memset(fw, 0xAA, sizeof(fw)); + eos_ota_source_t src; + memset(&src, 0, sizeof(src)); + src.expected_size = sizeof(fw); + assert(eos_ota_begin(&src) == 0); + /* One accepted byte so bytes_written + SIZE_MAX wraps on the old add. */ + assert(eos_ota_write_chunk(fw, 1) == 0); + assert(eos_ota_write_chunk(fw, SIZE_MAX) == -1); + + eos_ota_status_t st; + assert(eos_ota_get_status(&st) == 0); + assert(st.bytes_received == 1); + eos_ota_deinit(); + printf("[PASS] ota write chunk wrap\n"); +} + int main(void) { printf("=== EoS OTA Tests ===\n"); test_ota_init(); @@ -91,6 +245,12 @@ int main(void) { test_ota_verify_fail(); test_ota_rollback(); test_ota_status(); - printf("=== ALL OTA TESTS PASSED (5/5) ===\n"); + test_ota_apply_refuses_without_authenticator(); + test_ota_apply_honours_a_rejecting_authenticator(); + test_ota_authenticator_sees_the_computed_digest(); + test_ota_unapplied_slot_is_not_a_rollback_target(); + test_ota_write_chunk_overflow(); + test_ota_write_chunk_wrap(); + printf("=== ALL OTA TESTS PASSED (11/11) ===\n"); return 0; } \ No newline at end of file diff --git a/core/eos/tests/test_package.c b/core/eos/tests/test_package.c index aabe690e..fc6c40db 100644 --- a/core/eos/tests/test_package.c +++ b/core/eos/tests/test_package.c @@ -9,8 +9,10 @@ static int passed = 0; static int dummy_init(void) { return 0; } +/* sizeof(EosPackageSet) is ~3.9 MB, which overflows the default stack as a + * plain local. cmd/eos/main.c stores it static for the same reason. */ static void test_package_register(void) { - EosPackageSet set; + static EosPackageSet set; memset(&set, 0, sizeof(set)); EosPackage pkg; memset(&pkg, 0, sizeof(pkg)); @@ -24,7 +26,7 @@ static void test_package_register(void) { } static void test_package_find(void) { - EosPackageSet set; + static EosPackageSet set; memset(&set, 0, sizeof(set)); EosPackage pkg; memset(&pkg, 0, sizeof(pkg)); @@ -39,7 +41,7 @@ static void test_package_find(void) { } static void test_package_find_not_found(void) { - EosPackageSet set; + static EosPackageSet set; memset(&set, 0, sizeof(set)); const EosPackage *found = eos_package_find(&set, "nonexistent"); assert(found == NULL); @@ -53,7 +55,7 @@ static void test_package_find_null(void) { } static void test_package_register_multiple(void) { - EosPackageSet set; + static EosPackageSet set; memset(&set, 0, sizeof(set)); EosPackage p1, p2, p3; memset(&p1, 0, sizeof(p1)); @@ -76,7 +78,7 @@ static void test_package_register_multiple(void) { } static void test_package_init_all(void) { - EosPackageSet set; + static EosPackageSet set; memset(&set, 0, sizeof(set)); EosPackage pkg; memset(&pkg, 0, sizeof(pkg)); @@ -90,7 +92,7 @@ static void test_package_init_all(void) { } static void test_package_build_type(void) { - EosPackageSet set; + static EosPackageSet set; memset(&set, 0, sizeof(set)); EosPackage pkg; memset(&pkg, 0, sizeof(pkg)); diff --git a/core/eos/tests/test_security.c b/core/eos/tests/test_security.c index 49cd76c0..2ef04c9d 100644 --- a/core/eos/tests/test_security.c +++ b/core/eos/tests/test_security.c @@ -39,6 +39,29 @@ TEST(test_secureboot_init) { ASSERT(strcmp(sb.hash_algo, "sha256") == 0); } +/* A NULL path must fail the boot, not crash it. eos_secureboot_verify_image() + * already refused an absent public key near the end of the function, but the + * three strncpy() calls at the top ran first, so the refusal was unreachable + * and the call died with SIGSEGV instead. Without this case nothing in the + * suite ever passes NULL, which is how it survived. */ +TEST(test_secureboot_null_paths) { + EosSecureBoot sb; + + eos_secureboot_init(&sb); + ASSERT(eos_secureboot_verify_image(&sb, NULL, "sig", "key") == -1); + ASSERT(sb.status == EOS_BOOT_FAILED); + + eos_secureboot_init(&sb); + ASSERT(eos_secureboot_verify_image(&sb, "img", NULL, "key") == -1); + ASSERT(sb.status == EOS_BOOT_FAILED); + + eos_secureboot_init(&sb); + ASSERT(eos_secureboot_verify_image(&sb, "img", "sig", NULL) == -1); + ASSERT(sb.status == EOS_BOOT_FAILED); + + ASSERT(eos_secureboot_verify_image(NULL, "img", "sig", "key") == -1); +} + TEST(test_keystore_add_find) { EosKeyStore ks; eos_keystore_init(&ks, NULL); @@ -114,6 +137,7 @@ TEST(test_acl_last_match_wins) { int main(void) { printf("=== EoS: Security Services Unit Tests ===\n\n"); run_test_secureboot_init(); + run_test_secureboot_null_paths(); run_test_keystore_add_find(); run_test_keystore_not_found(); run_test_keystore_multiple_keys(); @@ -121,7 +145,7 @@ int main(void) { run_test_acl_default_deny(); run_test_acl_wildcard(); run_test_acl_last_match_wins(); - tests_run = 8; + tests_run = 9; printf("\n%d/%d tests passed\n", tests_passed, tests_run); return (tests_passed == tests_run) ? 0 : 1; } diff --git a/core/eos/toolchains/arm-none-eabi-r5.cmake b/core/eos/toolchains/arm-none-eabi-r5.cmake index f49d756f..38435d68 100644 --- a/core/eos/toolchains/arm-none-eabi-r5.cmake +++ b/core/eos/toolchains/arm-none-eabi-r5.cmake @@ -18,9 +18,14 @@ set(CMAKE_SIZE arm-none-eabi-size) set(CMAKE_AR arm-none-eabi-ar) set(CMAKE_RANLIB arm-none-eabi-ranlib) -set(CMAKE_C_FLAGS_INIT "-mcpu=cortex-r5 -mfloat-abi=hard -mfpu=vfpv3-d16 --specs=nosys.specs -ffunction-sections -fdata-sections -fno-common") +# -specs= belongs on the link line only. It reached one here as well, via the +# compiler driver, so this file was not broken -- but it was one added linker +# -specs= away from the duplicate-spec failure arm-cortex-m4.cmake had, which +# is the natural thing to write the first time an executable fails to link. +# Moved so all three ARM bare-metal toolchains state it the same way. +set(CMAKE_C_FLAGS_INIT "-mcpu=cortex-r5 -mfloat-abi=hard -mfpu=vfpv3-d16 -ffunction-sections -fdata-sections -fno-common") set(CMAKE_CXX_FLAGS_INIT "${CMAKE_C_FLAGS_INIT}") -set(CMAKE_EXE_LINKER_FLAGS_INIT "-Wl,--gc-sections -nostartfiles") +set(CMAKE_EXE_LINKER_FLAGS_INIT "-Wl,--gc-sections -nostartfiles -specs=nosys.specs") set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)