interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / om / tls / test / rsa / run.sh

160 lines · 5848 bytes Raw Blame History
1#!/bin/sh
2# SPDX-License-Identifier: Apache-2.0
3# Test suite for om/tls/rsa.om. Usage: sh om/tls/test/rsa/run.sh PATH_TO_PINNED_OMC
4# Builds om2 + om/tls/sha2.om + om/tls/rsa.om + test/rsa/driver.om into build/tls/rsa/, runs every
5# vector file (Wycheproof, edge, pyca differential), three library mutants and one expectation
6# mutant (each must be detected as MISMATCH with exit 1), and a timing series for 2048 and
7# 4096-bit keys. The driver's flipped-byte negative control runs on every case expected valid.
8# Exit 0 only when every check passes. Environment: RSA_BENCH_N (default 100 verifies per key size).
9set -eu
10omc=${1:?usage: run.sh OMC}
11here=$(cd "$(dirname "$0")" && pwd)
12root=$(cd "$here/../../../.." && pwd)
13pin=$(sed -n 's/^OMC_PIN=//p' "$root/om/units.sh")
14lib="$root/om/tls"
15vec="$here/vectors"
16out="$root/build/tls/rsa"
17rm -rf "$out"
18mkdir -p "$out"
19fail=0
20nice_run() { nice -n 19 ionice -c3 "$@"; }
21
22got=$(sha256sum "$omc" | cut -d' ' -f1)
23echo "omc $got"
24if [ "$got" != "$pin" ]; then
25 echo "FAIL omc sha256 is not the pinned $pin"
26 echo "RSA FAIL"
27 exit 1
28fi
29echo "rsa.om $(sha256sum "$lib/rsa.om" | cut -d' ' -f1)"
30echo "sha2.om $(sha256sum "$lib/sha2.om" | cut -d' ' -f1)"
31echo "functions rsa.om $(grep -c '^fn ' "$lib/rsa.om")"
32
33build() { # build RSA_SOURCE OUT_BINARY
34 { echo om2; cat "$lib/sha2.om" "$1" "$here/driver.om"; } > "$2.unit.om"
35 rc=0
36 nice_run "$omc" < "$2.unit.om" > "$2" 2> "$2.err" || rc=$?
37 if [ "$rc" -ne 0 ] || [ -s "$2.err" ]; then
38 echo "FAIL build $2 (rc $rc): $(cat "$2.err")"
39 rm -f "$2"
40 return 1
41 fi
42 chmod +x "$2"
43}
44
45cases=0
46wcases=0
47ctrls=0
48# run BINARY FILE LABEL: the driver must exit 0 with no mismatch
49run() {
50 rc=0
51 nice_run "$1" < "$2" > "$out/$3.log" 2>&1 || rc=$?
52 last=$(tail -n 1 "$out/$3.log")
53 if [ "$rc" -eq 0 ] && ! grep -q '^MISMATCH ' "$out/$3.log"; then
54 echo "PASS $3: $last"
55 n=$(echo "$last" | awk '{ print $2 }')
56 c=$(echo "$last" | awk '{ print $8 }')
57 cases=$((cases + n))
58 ctrls=$((ctrls + c))
59 case "$3" in wycheproof/*) wcases=$((wcases + n)) ;; esac
60 else
61 echo "FAIL $3 (exit $rc):"
62 cat "$out/$3.log"
63 fail=1
64 fi
65}
66
67mutants=0
68detected=0
69# mutant BINARY FILE LABEL: exit status exactly 1, at least one MISMATCH line and the summary line
70# with a nonzero mismatch count. Anything else (0, 65 bad input, 100+N trap, a signal) is a FAIL.
71mutant() {
72 mutants=$((mutants + 1))
73 rc=0
74 nice_run "$1" < "$2" > "$out/$3.log" 2>&1 || rc=$?
75 nm=$(grep -c '^MISMATCH ' "$out/$3.log" || true)
76 if [ "$rc" -eq 1 ] && [ "$nm" -gt 0 ] && grep -q '^cases .* mismatches [1-9]' "$out/$3.log"; then
77 echo "PASS $3 detected: $nm mismatches, $(tail -n 1 "$out/$3.log")"
78 detected=$((detected + 1))
79 else
80 echo "FAIL $3: exit $rc, $nm MISMATCH lines (need exit 1 and at least one): $(tail -n 1 "$out/$3.log")"
81 fail=1
82 fi
83}
84
85# libmutant LABEL SED_EXPR VECTOR_FILE: build a sed-mutated copy of rsa.om and require detection
86libmutant() {
87 sed "$2" "$lib/rsa.om" > "$out/$1.om"
88 if cmp -s "$lib/rsa.om" "$out/$1.om"; then
89 mutants=$((mutants + 1))
90 echo "FAIL $1: sed did not change the source"
91 fail=1
92 elif build "$out/$1.om" "$out/$1"; then
93 mutant "$out/$1" "$3" "$1"
94 else
95 mutants=$((mutants + 1))
96 fail=1
97 fi
98}
99
100if ! build "$lib/rsa.om" "$out/driver"; then
101 echo "RSA FAIL"
102 exit 1
103fi
104echo "driver $(sha256sum "$out/driver" | cut -d' ' -f1)"
105
106mkdir -p "$out/wycheproof"
107for f in "$vec"/wycheproof/*.txt; do
108 run "$out/driver" "$f" "wycheproof/$(basename "$f" .txt)"
109done
110run "$out/driver" "$vec/edge.txt" edge
111run "$out/driver" "$vec/pyca_gen.txt" pyca_gen
112run "$out/driver" "$vec/fuzz.txt" fuzz
113
114while IFS= read -r l; do
115 case "$l" in '#'*) ;; *) echo "NOT_RUN $l" ;; esac
116done < "$vec/NOT_RUN.txt"
117
118# Wycheproof expectation split (every case above passed, so these are the outcomes)
119split=$(cat "$vec"/wycheproof/*.txt | awk '
120 /^#/ { next }
121 { id = $8 }
122 id ~ /\/valid$/ { v++ }
123 id ~ /\/invalid$/ { i++ }
124 id ~ /\/acceptable/ { if ($1 == "0") aa++; else ar++ }
125 END { printf "valid=%d invalid=%d acceptable=%d (accepted %d: SmallModulus/SmallPublicKey; refused %d: MissingNull)", v, i, aa + ar, aa, ar }')
126echo "WYCHEPROOF $split"
127
128s256="$vec/wycheproof/rsa_signature_2048_sha256_test.txt"
129p256="$vec/wycheproof/rsa_pss_2048_sha256_mgf1_32_test.txt"
130# expectation mutant: the first expected-valid line flipped to expected-refused
131awk 'BEGIN { done = 0 } !done && /^0 / { sub(/^0 /, "n "); done = 1 } { print }' "$s256" > "$out/mut-expect.txt"
132mutant "$out/driver" "$out/mut-expect.txt" mutant-expectation
133# library mutant 1: Montgomery multiply row drops its carry
134libmutant mutant-mont-carry 's/^cy = shr x 32$/cy = 0/' "$s256"
135# library mutant 2: SHA-256 DigestInfo prefix byte 15 changed (05 -> 06)
136libmutant mutant-digestinfo 's/^const RSD2B ulong 0x4801650304020105$/const RSD2B ulong 0x4801650304020106/' "$s256"
137# library mutant 3: MGF1 counter steps by 2
138libmutant mutant-mgf1-counter 's/^ct = add ct 1$/ct = add ct 2/' "$p256"
139
140# timing: the first valid case of a 2048 and a 4096-bit PKCS#1 and PSS file, RSA_BENCH_N verifies
141bn=${RSA_BENCH_N:-100}
142for t in rsa_signature_2048_sha256_test rsa_signature_4096_sha256_test \
143 rsa_pss_2048_sha256_mgf1_32_test rsa_pss_4096_sha256_mgf1_32_test; do
144 grep -m 1 '^0 ' "$vec/wycheproof/$t.txt" > "$out/bench-$t.txt"
145 if nice_run "$out/driver" "$bn" < "$out/bench-$t.txt" > "$out/bench-$t.log" 2>&1; then
146 ns=$(awk '{ print $10 }' "$out/bench-$t.log")
147 echo "TIME $t: $bn verifies, $ns ns total, $((ns / bn)) ns per verify (nice 19)"
148 else
149 echo "FAIL bench $t"
150 cat "$out/bench-$t.log"
151 fail=1
152 fi
153done
154
155if [ "$fail" -ne 0 ] || [ "$detected" -ne "$mutants" ]; then
156 echo "RSA FAIL vectors=$cases (wycheproof=$wcases) mutants=$detected/$mutants"
157 exit 1
158fi
159echo "CONTROLS flipped-signature-byte controls=$ctrls (all refused)"
160echo "RSA PASS vectors=$cases (wycheproof=$wcases) mutants=$detected/$mutants"