omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / om / tls / test / integrity / run.sh
| 1 | #!/bin/sh |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # Software integrity test and show-version service of om/tls (fips.om fips_integ / fips_itool / |
| 4 | # fips_version, build.sh integrity step). |
| 5 | # usage: sh om/tls/test/integrity/run.sh OMC [OUTDIR] |
| 6 | # Checks: a build.sh program passes its self-tests and reports state 0 then 1 with the HMAC in its |
| 7 | # slot; that HMAC equals an independent openssl HMAC-SHA-256 over the file with the slot zeroed |
| 8 | # (NOT_RUN without openssl); an unpatched program, a byte changed in an unused text and a byte |
| 9 | # changed at the end of the file all refuse with check 16 and exit 3; the tool refuses usage errors |
| 10 | # (2), a missing file (3), a file without the marker and one with two markers (4). Code mutants |
| 11 | # (integrity check skipped; slot not masked; marker test always false) must each be caught. |
| 12 | # Last line: "INTEGRITY PASS checks=N mutants=M" or "INTEGRITY FAIL ...". |
| 13 | set -u |
| 14 | omc=${1:?usage: run.sh OMC [OUTDIR]} |
| 15 | here=$(cd "$(dirname "$0")" && pwd); lib=$(cd "$here/../.." && pwd) |
| 16 | out=${2:-$(mktemp -d)}; mkdir -p "$out" |
| 17 | ok=0; bad=0 |
| 18 | say() { echo "$*"; } |
| 19 | pass() { ok=$((ok + 1)); say "PASS $*"; } |
| 20 | fail() { bad=$((bad + 1)); say "FAIL $*"; } |
| 21 | marker=omega-tls-integrity-v1-hmac-sha256: |
| 22 | say "omc $(sha256sum "$omc" | cut -c1-64)" |
| 23 | for u in hkdf fips tls build; do f=$lib/$u.om; [ "$u" = build ] && f=$lib/build.sh; say "$(basename "$f") $(sha256sum "$f" | cut -c1-64)"; done |
| 24 | # slot OFFSET of the 64 hex digits in FILE |
| 25 | slot() { echo $(( $(grep -abo "$marker" "$1" | cut -d: -f1) + 35 )); } |
| 26 | # put BYTE (one char) at OFFSET in FILE |
| 27 | poke() { printf '%s' "$3" | dd of="$1" bs=1 seek="$2" conv=notrunc status=none; } |
| 28 | # expect NAME BIN CHECK RC: the version program printed "check CHECK" and exited RC |
| 29 | expect() { |
| 30 | "$2" > "$out/$1.out" 2>&1; rc=$? |
| 31 | if [ "$rc" -eq "$4" ] && grep -qx "check $3" "$out/$1.out"; then pass "$1: check $3 exit $rc" |
| 32 | else fail "$1: wanted check $3 exit $4, got exit $rc: $(tr '\n' '|' < "$out/$1.out")"; fi |
| 33 | } |
| 34 | |
| 35 | sh "$lib/build.sh" "$omc" "$here/version.om" "$out/ver" 2> "$out/ver.err" || { say "INTEGRITY FAIL build: $(cat "$out/ver.err")"; exit 1; } |
| 36 | expect good "$out/ver" 0 0 |
| 37 | hex=$(grep -ao "${marker}[0-9a-f]*" "$out/ver" | cut -c36-) |
| 38 | l1=$(sed -n 1p "$out/good.out"); l3=$(sed -n 3p "$out/good.out") |
| 39 | if [ "$l1" = "Omega TLS module (om/tls) version 0.1.0 integrity hmac-sha256:$hex state 0" ] && |
| 40 | [ "$l3" = "Omega TLS module (om/tls) version 0.1.0 integrity hmac-sha256:$hex state 1" ] && |
| 41 | [ "${#hex}" -eq 64 ] && [ "$hex" != "$(printf '%064d' 0)" ]; then pass "version: state 0 then 1, slot $hex" |
| 42 | else fail "version lines: $l1 / $l3"; fi |
| 43 | if grep -qx 'short 0' "$out/good.out"; then pass "version: short buffer returns 0"; else fail "version short buffer"; fi |
| 44 | if command -v openssl > /dev/null; then |
| 45 | cp "$out/ver" "$out/ver.z"; poke "$out/ver.z" "$(slot "$out/ver")" "$(printf '%064d' 0)" |
| 46 | ref=$(openssl dgst -sha256 -hmac 'omega-tls module integrity key v1' "$out/ver.z" | sed 's/.*= //') |
| 47 | if [ "$ref" = "$hex" ]; then pass "oracle: openssl HMAC-SHA-256 equals the slot"; else fail "oracle: openssl $ref slot $hex"; fi |
| 48 | else say "NOT_RUN oracle: openssl absent"; fi |
| 49 | |
| 50 | "$omc" < "$out/ver.unit.om" > "$out/ver.unpatched" && chmod +x "$out/ver.unpatched" |
| 51 | expect unpatched "$out/ver.unpatched" 16 3 |
| 52 | cp "$out/ver" "$out/ver.text"; poke "$out/ver.text" "$(grep -abo 'Omega TLS module (om/tls)' "$out/ver" | head -1 | cut -d: -f1)" o |
| 53 | expect text-byte "$out/ver.text" 16 3 |
| 54 | cp "$out/ver" "$out/ver.app"; printf 'x' >> "$out/ver.app" |
| 55 | expect appended-byte "$out/ver.app" 16 3 |
| 56 | cp "$out/ver" "$out/ver.slot"; last=$(printf '%s' "$hex" | cut -c64); [ "$last" = 0 ] && nd=1 || nd=0 |
| 57 | poke "$out/ver.slot" $(( $(slot "$out/ver") + 63 )) "$nd" |
| 58 | expect slot-last-digit "$out/ver.slot" 16 3 |
| 59 | |
| 60 | # the tool alone (build.sh's integrity step): compose it exactly as build.sh does |
| 61 | { printf 'om2\n'; for u in sha2 hkdf drbg x25519 sha3 mlkem chacha aesgcm ec ecdh rsa x509 rev fips tls; do cat "$lib/$u.om"; done |
| 62 | printf 'fn main to int\nret fips_itool\nend\n'; } > "$out/itool.om" |
| 63 | "$omc" < "$out/itool.om" > "$out/itool" && chmod +x "$out/itool" |
| 64 | tool() { "$out/itool" "$@" > /dev/null 2>&1; echo $?; } |
| 65 | [ "$(tool)" -eq 2 ] && pass "tool: no argument exit 2" || fail "tool: no argument" |
| 66 | [ "$(tool "$out/absent")" -eq 3 ] && pass "tool: missing file exit 3" || fail "tool: missing file" |
| 67 | printf 'no marker here\n' > "$out/plain" |
| 68 | [ "$(tool "$out/plain")" -eq 4 ] && pass "tool: no marker exit 4" || fail "tool: no marker" |
| 69 | cp "$out/ver.unpatched" "$out/twice"; printf '%s%064d' "$marker" 0 >> "$out/twice" |
| 70 | [ "$(tool "$out/twice")" -eq 4 ] && pass "tool: two markers exit 4" || fail "tool: two markers" |
| 71 | cp "$out/ver.unpatched" "$out/cut"; printf '%s0000' "$marker" >> "$out/cut" |
| 72 | [ "$(tool "$out/cut")" -eq 4 ] && pass "tool: second marker with a cut slot exit 4" || fail "tool: cut slot" |
| 73 | cp "$out/ver.unpatched" "$out/repatch" |
| 74 | if [ "$(tool "$out/repatch")" -eq 0 ] && cmp -s "$out/repatch" "$out/ver"; then pass "tool: patching the unpatched build gives build.sh's bytes" |
| 75 | else fail "tool: repatch differs from build.sh output"; fi |
| 76 | |
| 77 | # code mutants: each must make a check above fail |
| 78 | mk=0; mbad=0 |
| 79 | mutant() { |
| 80 | d=$out/m-$1; mkdir -p "$d" |
| 81 | for u in sha2 hkdf drbg x25519 sha3 mlkem chacha aesgcm ec ecdh rsa x509 rev fips tls; do cp "$lib/$u.om" "$d/"; done |
| 82 | cp "$lib/build.sh" "$d/" |
| 83 | sed -i "$2" "$d/fips.om" |
| 84 | if cmp -s "$d/fips.om" "$lib/fips.om"; then mbad=$((mbad + 1)); say "FAIL mutant $1: sed changed nothing"; return; fi |
| 85 | sh "$d/build.sh" "$omc" "$here/version.om" "$d/ver" 2> "$d/err"; b=$? |
| 86 | "$omc" < "$d/ver.unit.om" > "$d/unp" 2> /dev/null && chmod +x "$d/unp" |
| 87 | gd=x; sd=x |
| 88 | if [ "$b" -eq 0 ]; then |
| 89 | "$d/ver" > "$d/good.out" 2>&1; gd=$? |
| 90 | cp "$d/ver" "$d/slot"; mh=$(grep -ao "${marker}[0-9a-f]*" "$d/ver" | cut -c36-) |
| 91 | [ "$(printf '%s' "$mh" | cut -c64)" = 0 ] && nd=1 || nd=0 |
| 92 | poke "$d/slot" $(( $(slot "$d/ver") + 63 )) "$nd" |
| 93 | "$d/slot" > "$d/slot.out" 2>&1; sd=$? |
| 94 | fi |
| 95 | "$d/unp" > "$d/unp.out" 2>&1; ud=$? |
| 96 | if [ "$gd" = 0 ] && [ "$ud" = 3 ] && grep -qx 'check 16' "$d/unp.out" && [ "$sd" = 3 ] && grep -qx 'check 16' "$d/slot.out" |
| 97 | then mbad=$((mbad + 1)); say "FAIL mutant $1 survived" |
| 98 | else mk=$((mk + 1)); say "PASS mutant $1 killed (build $b, patched exit $gd, unpatched exit $ud, slot digit exit $sd)"; fi |
| 99 | } |
| 100 | mutant fail-ignored 's/^ret fips_fail 16$/put FPST 1 0/' |
| 101 | mutant no-mask '/^fn fips_imask /,/^end$/ s/^put b i 48$/put b i get b i/' |
| 102 | mutant no-marker '/^fn fips_imark /,/^ret 0$/ s/^if eq d 0$/if eq d 1/' |
| 103 | mutant cmp-short 's/^ret fips_cmp m part FKIH FIMK 64$/ret fips_cmp part m 0 1 part FKIH FIMK 2/' |
| 104 | |
| 105 | if [ "$bad" -eq 0 ] && [ "$mbad" -eq 0 ]; then say "INTEGRITY PASS checks=$ok mutants=$mk"; exit 0; fi |
| 106 | say "INTEGRITY FAIL checks=$ok failed=$bad mutants=$mk survived=$mbad"; exit 1 |