add tests for P256 ARM assembly

This commit is contained in:
Sergio R. Caprile committed 2026-12-02 10:13:27 -03:00
1 parent 443c34c66e
commit 95d421aa16
8 files changed
+287 -37

No files matched your search

+9
View File
@@ -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:
+5 -37
View File
@@ -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:
+36
View File
@@ -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)
+27
View File
@@ -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 = .;
}
+27
View File
@@ -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 = .;
}
+27
View File
@@ -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 = .;
}
+50
View File
@@ -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"
+106
View File
@@ -0,0 +1,106 @@
#include "tls_p256.h"
#include "log.h"
#include "util.h"
#include <stdio.h>
#include <stdlib.h>
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;
}