physics-mv
FORGE: request-bound machine realization, providers and verification for AienOS. Host, simulated and GB10 hardware paths have separate evidence.
Brought by drakestapleton
physics-mv / m3 / tests / run_sha256_kat.sh
| 1 | #!/bin/sh |
| 2 | # m3/tests/run_sha256_kat.sh -- end-to-end SHA-256 KAT for m3/sha256_be.s. |
| 3 | # |
| 4 | # 1. Builds everything (m3/build.sh). |
| 5 | # 2. Cross-checks the host tool (m3tool sha256) against coreutils sha256sum. |
| 6 | # 3. Boots the bare-metal KAT in QEMU (SCTLR_EL1.A on), dumps RES and PAT by |
| 7 | # QMP pmemsave, and checks by STATE INSPECTION: every digest equals host |
| 8 | # sha256sum of the same bytes, every refusal returns its code and leaves |
| 9 | # its 0xA5-prefilled out buffer untouched. |
| 10 | # 4. Boots the fault-probe variant: a deliberate misaligned load must take a |
| 11 | # synchronous alignment fault (ESR EC 0x25, DFSC 0x21) and never finish. |
| 12 | # Exits 0 only if every check passes. Evidence lands in m3/build/kat/. |
| 13 | set -eu |
| 14 | |
| 15 | M3=$(cd "$(dirname "$0")/.." && pwd -P) |
| 16 | B=$M3/build |
| 17 | W=$B/kat |
| 18 | FAILS=0 |
| 19 | |
| 20 | "$M3/build.sh" |
| 21 | T=$B/m3tool |
| 22 | rm -rf "$W"; mkdir -p "$W/vec" |
| 23 | LOG=$W/results.txt; : > "$LOG" |
| 24 | |
| 25 | ok() { echo "PASS $*" | tee -a "$LOG"; } |
| 26 | bad() { echo "FAIL $*" | tee -a "$LOG"; FAILS=$((FAILS + 1)); } |
| 27 | check() { if [ "$2" = "$3" ]; then ok "$1 ($2)"; else bad "$1: got '$2' want '$3'"; fi; } |
| 28 | hsha() { sha256sum < "$1" | cut -d' ' -f1; } |
| 29 | pattern() { LC_ALL=C awk -v n="$1" 'BEGIN { for (i = 0; i < n; i++) printf "%c", i % 256 }'; } |
| 30 | |
| 31 | # ---- host vectors --------------------------------------------------------- |
| 32 | : > "$W/vec/0_empty" |
| 33 | printf 'abc' > "$W/vec/1_abc" |
| 34 | printf 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq' > "$W/vec/2_nist56" |
| 35 | pattern 1000 > "$W/vec/3_pat1000" |
| 36 | pattern 65536 > "$W/vec/4_pat65536" |
| 37 | check "host vector nist56 length" "$(wc -c < "$W/vec/2_nist56" | tr -d ' ')" 56 |
| 38 | check "host vector pat65536 length" "$(wc -c < "$W/vec/4_pat65536" | tr -d ' ')" 65536 |
| 39 | |
| 40 | # ---- m3tool sha256 vs sha256sum ------------------------------------------- |
| 41 | for n in 55 56 63 64 65 119 120 200000; do pattern "$n" > "$W/vec/x_pat$n"; done |
| 42 | for f in "$W"/vec/* "$B/kat.bin" "$B/m3tool" "$M3/../sha256_clean.c"; do |
| 43 | check "m3tool sha256 $(basename "$f") ($(wc -c < "$f" | tr -d ' ') B)" "$("$T" sha256 "$f")" "$(hsha "$f")" |
| 44 | done |
| 45 | check "known abc digest" "$(hsha "$W/vec/1_abc")" ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad |
| 46 | |
| 47 | # ---- QEMU: main KAT ------------------------------------------------------- |
| 48 | RES=0x40280000 |
| 49 | "$M3/tools/qemu_inspect.sh" --out "$W/run" --marker 'M3_KAT_DONE' --fail-marker 'M3_KAT_FAULT' \ |
| 50 | --timeout 20 --bios "$B/kat_stub.bin" --loader "$B/kat.bin@0x40200000" \ |
| 51 | --region "res:$RES:0x240" --region "pat:0x40290000:0x10000" \ |
| 52 | || { bad "qemu_inspect main KAT (rc=$?)"; echo "RESULT: FAIL ($FAILS)"; exit 1; } |
| 53 | R=$W/run/res.bin |
| 54 | check "RES magic" "$("$T" hexfield "$R" 0 8)" 0x534552544b41334d |
| 55 | check "guest PAT == host pattern" "$(hsha "$W/run/pat.bin")" "$(hsha "$W/vec/4_pat65536")" |
| 56 | sctlr=$("$T" hexfield "$R" 8 8) |
| 57 | check "SCTLR_EL1.A set" "$(( sctlr & 2 ))" 2 |
| 58 | check "CurrentEL == EL1" "$("$T" hexfield "$R" 16 8)" 0x0000000000000004 |
| 59 | i=0 |
| 60 | for v in 0_empty 1_abc 2_nist56 3_pat1000 4_pat65536; do |
| 61 | check "vector $i rc" "$("$T" hexfield "$R" $((0x20 + 4 * i)) 4)" 0x00000000 |
| 62 | check "vector $i $v digest == sha256sum" "$("$T" bytes "$R" $((0x80 + 32 * i)) 32)" "$(hsha "$W/vec/$v")" |
| 63 | i=$((i + 1)) |
| 64 | done |
| 65 | check "refusal len=SHA256_MAX_LEN+1 rc" "$("$T" hexfield "$R" $((0x20 + 4 * 5)) 4)" 0x00000001 |
| 66 | check "refusal misaligned data rc" "$("$T" hexfield "$R" $((0x20 + 4 * 6)) 4)" 0x00000002 |
| 67 | check "refusal misaligned out rc" "$("$T" hexfield "$R" $((0x20 + 4 * 7)) 4)" 0x00000003 |
| 68 | check "refusal data+len wrap rc" "$("$T" hexfield "$R" $((0x20 + 4 * 8)) 4)" 0x00000004 |
| 69 | a5=$(printf 'a5%.0s' $(seq 1 $((0x200 - 0x140)))) |
| 70 | if [ "$("$T" bytes "$R" $((0x140)) $((0x200 - 0x140)))" = "$a5" ]; then |
| 71 | ok "refusal out buffers untouched (0x140..0x200: 192 B, all 0xA5)" |
| 72 | else bad "refusal out buffers modified (0x140..0x200 not all 0xA5)"; fi |
| 73 | check "no fault recorded" "$("$T" hexfield "$R" $((0x200)) 8)" 0x0000000000000000 |
| 74 | |
| 75 | # ---- QEMU: alignment fault probe ----------------------------------------- |
| 76 | if "$M3/tools/qemu_inspect.sh" --out "$W/fault" --marker 'M3_KAT_FAULT' --fail-marker 'M3_KAT_DONE' \ |
| 77 | --timeout 20 --bios "$B/kat_stub.bin" --loader "$B/kat_fault.bin@0x40200000" \ |
| 78 | --region "res:$RES:0x240"; then |
| 79 | F=$W/fault/res.bin |
| 80 | check "fault probe: fault magic" "$("$T" hexfield "$F" $((0x200)) 8)" 0x21544c554146334d |
| 81 | esr=$("$T" hexfield "$F" $((0x208)) 8) |
| 82 | check "fault probe: ESR EC = 0x25 (data abort, same EL)" "$(( (esr >> 26) & 0x3f ))" 37 |
| 83 | check "fault probe: DFSC = 0x21 (alignment fault)" "$(( esr & 0x3f ))" 33 |
| 84 | check "fault probe: FAR = PAT+1" "$("$T" hexfield "$F" $((0x218)) 8)" 0x0000000040290001 |
| 85 | check "fault probe: vector = current EL SPx sync (0x200)" "$("$T" hexfield "$F" $((0x220)) 8)" 0x0000000000000200 |
| 86 | check "fault probe: run never completed (no RES magic)" "$("$T" hexfield "$F" 0 8)" 0x0000000000000000 |
| 87 | else |
| 88 | bad "qemu_inspect fault probe (rc=$?)" |
| 89 | fi |
| 90 | |
| 91 | if [ "$FAILS" -eq 0 ]; then echo "RESULT: PASS" | tee -a "$LOG"; exit 0; fi |
| 92 | echo "RESULT: FAIL ($FAILS)" | tee -a "$LOG"; exit 1 |