From 95d421aa1670b1a0bbc9f7c5b3d4b05293199805 Mon Sep 17 00:00:00 2001 From: "Sergio R. Caprile" Date: Tue, 1 Dec 2026 17:31:27 -0300 Subject: [PATCH] add tests for P256 ARM assembly --- .github/workflows/nightly.yml | 9 +++ .github/workflows/on_demand.yml | 42 ++---------- test/p256_assembly/Makefile | 36 ++++++++++ test/p256_assembly/p256_m33.ld | 27 ++++++++ test/p256_assembly/p256_m4.ld | 27 ++++++++ test/p256_assembly/p256_m55.ld | 27 ++++++++ test/p256_assembly/p256_startup.S | 50 ++++++++++++++ test/p256_assembly/p256_test.c | 106 ++++++++++++++++++++++++++++++ 8 files changed, 287 insertions(+), 37 deletions(-) create mode 100644 test/p256_assembly/Makefile create mode 100644 test/p256_assembly/p256_m33.ld create mode 100644 test/p256_assembly/p256_m4.ld create mode 100644 test/p256_assembly/p256_m55.ld create mode 100644 test/p256_assembly/p256_startup.S create mode 100644 test/p256_assembly/p256_test.c diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index d1b5a895..72dc8aef 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -70,6 +70,15 @@ jobs: if [[ "${{ matrix.target }}" = ppc_* ]]; then export TFLAGS="$TFLAGS -D_FILE_OFFSET_BITS=64"; fi sudo apt -y update ; sudo apt -y install binfmt-support qemu-user-static && ./test/setup_mqtt_server.sh && docker run --rm --privileged multiarch/qemu-user-static --reset -p yes && make -C test ${{ matrix.target }} + p256_assembly: + runs-on: ubuntu-24.04 + name: P256 Cortex-M + steps: + - uses: actions/checkout@v5 + with: { fetch-depth: 2 } + - run: sudo apt -y update && sudo apt -y install gcc-arm-none-eabi qemu-system-arm + - run: make -C test/p256_assembly CFLAGS_EXTRA='-Wno-sign-conversion -Wno-conversion' + mip: runs-on: ubuntu-latest strategy: diff --git a/.github/workflows/on_demand.yml b/.github/workflows/on_demand.yml index 646865d5..a8c1e124 100644 --- a/.github/workflows/on_demand.yml +++ b/.github/workflows/on_demand.yml @@ -4,47 +4,15 @@ on: env: IPV6: 0 jobs: - atcmd: - runs-on: ubuntu-latest - strategy: - fail-fast: false - matrix: - cc: [gcc, clang, g++, clang++] - name: ATcmd CC=${{ matrix.cc }} - steps: - - uses: actions/checkout@v5 - with: { fetch-depth: 2 } - - uses: webfactory/ssh-agent@v0.10.0 - with: - ssh-private-key: ${{ secrets.HEALTH_TESTS_SSH_KEY }} - - run: make -C test atcmd_test CC=${{ matrix.cc }} > log - - if: success() || failure() - run: | - cat log - test/health.awk < log > json - scp -o "StrictHostKeyChecking=no" json "root@176.9.217.245:/data/downloads/health/atcmd_${{ matrix.cc }}_$(date +"%Y%m%d").json" - atcmd89: - runs-on: ubuntu-24.04 # Ubuntu 26.04 Docker 29.4.2 breaks 32-bit Wine; switch at >= 29.4.3 - strategy: - fail-fast: false - matrix: - target: [atcmd_vc98] - name: ATcmd C89 ${{ matrix.target }} - steps: - - uses: actions/checkout@v5 - with: { fetch-depth: 2 } - - run: make -C test ${{ matrix.target }} - atcmdbe: + + p256_assembly: runs-on: ubuntu-24.04 - strategy: - fail-fast: false - matrix: - target: [atcmd_s390] - name: ATcmd be ${{ matrix.target }} + name: P256 Cortex-M steps: - uses: actions/checkout@v5 with: { fetch-depth: 2 } - - run: sudo apt -y update ; sudo apt -y install binfmt-support qemu-user-static && docker run --rm --privileged multiarch/qemu-user-static --reset -p yes && make -C test ${{ matrix.target }} + - run: sudo apt -y update && sudo apt -y install gcc-arm-none-eabi qemu-system-arm + - run: make -C test/p256_assembly CFLAGS_EXTRA='-Wno-sign-conversion -Wno-conversion' # macos: diff --git a/test/p256_assembly/Makefile b/test/p256_assembly/Makefile new file mode 100644 index 00000000..85a8b6c1 --- /dev/null +++ b/test/p256_assembly/Makefile @@ -0,0 +1,36 @@ +CFLAGS = -pedantic -W -Wall -Werror -Wshadow -Wdouble-promotion +CFLAGS += -fno-common -Wconversion -Wundef -Wmissing-prototypes -Wstrict-prototypes +CFLAGS += -O3 -g3 -ffunction-sections -fdata-sections +CFLAGS += -I../../src -DMG_ARCH=MG_ARCH_ARMGCC -DMG_TLS=MG_TLS_BUILTIN +CFLAGS += -DMG_P256=MG_P256_LENNGREN -DMG_ENABLE_CUSTOM_RANDOM=1 +# M4, M33, and M55 cover Armv7E-M, v8-M, and v8.1-M. M7 is ISA-equivalent +# to M4 for this scalar DSP code, so its cache and FPU add no coverage here. +TARGETS = p256_m4 p256_m33 p256_m55 +SOURCES = p256_test.c p256_startup.S ../../src/tls_p256.c +SOURCES += ../../src/tls_p256_cortex.c ../../src/sha256.c ../../src/util.c +SOURCES += ../../src/iobuf.c ../../src/fmt.c ../../src/printf.c ../../src/log.c + +.PHONY: all clean $(TARGETS) + +all: $(TARGETS) + +p256_m4: P256_CPU = cortex-m4 +p256_m4: P256_MACHINE = mps2-an386 +p256_m4: P256_LD = p256_m4.ld +p256_m4: p256_m4.ld +p256_m33: P256_CPU = cortex-m33 +p256_m33: P256_MACHINE = mps2-an505 +p256_m33: P256_LD = p256_m33.ld +p256_m33: p256_m33.ld +p256_m55: P256_CPU = cortex-m55 +p256_m55: P256_MACHINE = mps3-an547 +p256_m55: P256_LD = p256_m55.ld +p256_m55: p256_m55.ld +CFLAGS += -mcpu=$(P256_CPU) -mthumb $(CFLAGS_EXTRA) +LDFLAGS ?= -nostartfiles --specs=rdimon.specs -Wl,-T,$(P256_LD) -Wl,--gc-sections +$(TARGETS): $(SOURCES) + arm-none-eabi-gcc $(SOURCES) $(CFLAGS) $(LDFLAGS) -o $@ + qemu-system-arm -M $(P256_MACHINE) -nographic -semihosting-config enable=on,target=native -kernel $@ + +clean: + rm -rf $(TARGETS) diff --git a/test/p256_assembly/p256_m33.ld b/test/p256_assembly/p256_m33.ld new file mode 100644 index 00000000..70492d32 --- /dev/null +++ b/test/p256_assembly/p256_m33.ld @@ -0,0 +1,27 @@ +ENTRY(Reset_Handler); + +MEMORY { + flash(rx) : ORIGIN = 0x10000000, LENGTH = 4M + sram(rwx) : ORIGIN = 0x28000000, LENGTH = 4M +} + +_estack = ORIGIN(sram) + LENGTH(sram); + +SECTIONS { + .vectors : { KEEP(*(.isr_vector)) } > flash + .text : { *(.text* .text.*) } > flash + .rodata : { *(.rodata*) } > flash + + .data : { + _sdata = .; + *(.data SORT(.data.*)) + _edata = .; + } > sram AT > flash + _sidata = LOADADDR(.data); + + .bss : { _sbss = .; *(.bss SORT(.bss.*) COMMON) _ebss = .; } > sram + + . = ALIGN(8); + _end = .; + end = .; +} diff --git a/test/p256_assembly/p256_m4.ld b/test/p256_assembly/p256_m4.ld new file mode 100644 index 00000000..a7a4919f --- /dev/null +++ b/test/p256_assembly/p256_m4.ld @@ -0,0 +1,27 @@ +ENTRY(Reset_Handler); + +MEMORY { + flash(rx) : ORIGIN = 0x00000000, LENGTH = 4M + sram(rwx) : ORIGIN = 0x20000000, LENGTH = 4M +} + +_estack = ORIGIN(sram) + LENGTH(sram); + +SECTIONS { + .vectors : { KEEP(*(.isr_vector)) } > flash + .text : { *(.text* .text.*) } > flash + .rodata : { *(.rodata*) } > flash + + .data : { + _sdata = .; + *(.data SORT(.data.*)) + _edata = .; + } > sram AT > flash + _sidata = LOADADDR(.data); + + .bss : { _sbss = .; *(.bss SORT(.bss.*) COMMON) _ebss = .; } > sram + + . = ALIGN(8); + _end = .; + end = .; +} diff --git a/test/p256_assembly/p256_m55.ld b/test/p256_assembly/p256_m55.ld new file mode 100644 index 00000000..45045fff --- /dev/null +++ b/test/p256_assembly/p256_m55.ld @@ -0,0 +1,27 @@ +ENTRY(Reset_Handler); + +MEMORY { + flash(rx) : ORIGIN = 0x00000000, LENGTH = 512K + sram(rwx) : ORIGIN = 0x21000000, LENGTH = 4M +} + +_estack = ORIGIN(sram) + LENGTH(sram); + +SECTIONS { + .vectors : { KEEP(*(.isr_vector)) } > flash + .text : { *(.text* .text.*) } > flash + .rodata : { *(.rodata*) } > flash + + .data : { + _sdata = .; + *(.data SORT(.data.*)) + _edata = .; + } > sram AT > flash + _sidata = LOADADDR(.data); + + .bss : { _sbss = .; *(.bss SORT(.bss.*) COMMON) _ebss = .; } > sram + + . = ALIGN(8); + _end = .; + end = .; +} diff --git a/test/p256_assembly/p256_startup.S b/test/p256_assembly/p256_startup.S new file mode 100644 index 00000000..1703f992 --- /dev/null +++ b/test/p256_assembly/p256_startup.S @@ -0,0 +1,50 @@ +.syntax unified +.thumb + +.section .isr_vector, "a", %progbits +.word _estack +.word Reset_Handler +.rept 14 +.word Default_Handler +.endr + +.section .text.Reset_Handler, "ax", %progbits +.thumb_func +.global Reset_Handler +Reset_Handler: + ldr sp, =_estack + ldr r0, =_sidata + ldr r1, =_sdata + ldr r2, =_edata +1: + cmp r1, r2 + bcs 2f + ldr r3, [r0], #4 + str r3, [r1], #4 + b 1b +2: + ldr r1, =_sbss + ldr r2, =_ebss + movs r3, #0 +3: + cmp r1, r2 + bcs 4f + str r3, [r1], #4 + b 3b +4: + bl main + b exit + +.thumb_func +Default_Handler: + ldr r0, =4 + ldr r1, =exception + bkpt 0xab + ldr r0, =0x18 + ldr r1, =0x20023 + bkpt 0xab + b . + +.section .rodata +exception: + .asciz "FAILURE: unhandled exception\n" diff --git a/test/p256_assembly/p256_test.c b/test/p256_assembly/p256_test.c new file mode 100644 index 00000000..0da6e2de --- /dev/null +++ b/test/p256_assembly/p256_test.c @@ -0,0 +1,106 @@ +#include "tls_p256.h" +#include "log.h" +#include "util.h" + +#include +#include + +extern void initialise_monitor_handles(void); + +static int s_num_tests; +static bool s_error; + +#ifdef NO_ABORT +static int s_abort; +#define ABORT() ++s_abort, s_error = true +#else +#ifdef NO_SLEEP_ABORT +#define ABORT() abort() +#else +#define ABORT() \ + sleep(2); /* 2s, GH print reason */ \ + abort(); +#endif +#endif + +static const uint8_t s_prv[32] = { + 0xc9, 0xaf, 0xa9, 0xd8, 0x45, 0xba, 0x75, 0x16, + 0x6b, 0x5c, 0x21, 0x57, 0x67, 0xb1, 0xd6, 0x93, + 0x4e, 0x50, 0xc3, 0xdb, 0x36, 0xe8, 0x9b, 0x12, + 0x7b, 0x8a, 0x62, 0x2b, 0x12, 0x0f, 0x67, 0x21}; +static const uint8_t s_pub[64] = { + 0x60, 0xfe, 0xd4, 0xba, 0x25, 0x5a, 0x9d, 0x31, + 0xc9, 0x61, 0xeb, 0x74, 0xc6, 0x35, 0x6d, 0x68, + 0xc0, 0x49, 0xb8, 0x92, 0x3b, 0x61, 0xfa, 0x6c, + 0xe6, 0x69, 0x62, 0x2e, 0x60, 0xf2, 0x9f, 0xb6, + 0x79, 0x03, 0xfe, 0x10, 0x08, 0xb8, 0xbc, 0x99, + 0xa4, 0x1a, 0xe9, 0xe9, 0x56, 0x28, 0xbc, 0x64, + 0xf2, 0xf1, 0xb2, 0x0c, 0x2d, 0x7e, 0x9f, 0x51, + 0x77, 0xa3, 0xc2, 0x94, 0xd4, 0x46, 0x22, 0x99}; +static const uint8_t s_hash[32] = { + 0xaf, 0x2b, 0xdb, 0xe1, 0xaa, 0x9b, 0x6e, 0xc1, + 0xe2, 0xad, 0xe1, 0xd6, 0x94, 0xf4, 0x1f, 0xc7, + 0x1a, 0x83, 0x1d, 0x02, 0x68, 0xe9, 0x89, 0x15, + 0x62, 0x11, 0x3d, 0x8a, 0x62, 0xad, 0xd1, 0xbf}; +static const uint8_t s_sig[64] = { + 0xef, 0xd4, 0x8b, 0x2a, 0xac, 0xb6, 0xa8, 0xfd, + 0x11, 0x40, 0xdd, 0x9c, 0xd4, 0x5e, 0x81, 0xd6, + 0x9d, 0x2c, 0x87, 0x7b, 0x56, 0xaa, 0xf9, 0x91, + 0xc3, 0x4d, 0x0e, 0xa8, 0x4e, 0xaf, 0x37, 0x16, + 0xf7, 0xcb, 0x1c, 0x94, 0x2d, 0x65, 0x7c, 0x41, + 0xd4, 0x36, 0xc7, 0xa1, 0xb6, 0xe2, 0x9f, 0x65, + 0xf3, 0xe9, 0x00, 0xdb, 0xb9, 0xaf, 0xf4, 0x06, + 0x4d, 0xc4, 0xab, 0x2f, 0x84, 0x3a, 0xcd, 0xa8}; + +bool mg_random(void *buf, size_t len) { + if (len != sizeof(s_prv)) return false; + memcpy(buf, s_prv, len); + return true; +} + +#define ASSERT(expr) \ + do { \ + s_num_tests++; \ + if (!(expr)) { \ + printf("FAILURE %s:%d: %s\n", __FILE__, __LINE__, #expr); \ + fflush(stdout); \ + ABORT(); \ + } \ + } while (0) + +static void print_char(char c, void *param) { + (void) param; + putchar(c); +} + +static void test_p256(void) { + uint8_t pub[64], sig[64], prv2[32], pub2[64], sec1[32], sec2[32]; + + ASSERT(mg_p256_compute_public_key(s_prv, pub) == 1); + ASSERT(memcmp(pub, s_pub, sizeof(pub)) == 0); + ASSERT(mg_p256_sign(s_prv, s_hash, sizeof(s_hash), sig) == 1); + ASSERT(memcmp(sig, s_sig, sizeof(sig)) == 0); + ASSERT(mg_p256_verify(s_pub, s_hash, sizeof(s_hash), s_sig) == 1); + sig[10] ^= 1; + ASSERT(mg_p256_verify(s_pub, s_hash, sizeof(s_hash), sig) == 0); + ASSERT(mg_p256_make_key(pub2, prv2) == 1); + ASSERT(mg_p256_compute_public_key(prv2, pub) == 1); + ASSERT(memcmp(pub, pub2, sizeof(pub)) == 0); + ASSERT(mg_p256_shared_secret(pub2, s_prv, sec1) == 1); + ASSERT(mg_p256_shared_secret(s_pub, prv2, sec2) == 1); + ASSERT(memcmp(sec1, sec2, sizeof(sec1)) == 0); + pub2[63] ^= 1; + ASSERT(mg_p256_shared_secret(pub2, s_prv, sec1) == 0); +} + +int main(void) { + initialise_monitor_handles(); + mg_log_set_fn(print_char, NULL); + test_p256(); +#ifdef NO_ABORT + if (s_abort != 0) return EXIT_FAILURE; +#endif + if (s_error) return EXIT_FAILURE; + printf("SUCCESS. Total tests: %d\n", s_num_tests); + return EXIT_SUCCESS; +}