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 / ec / run.sh
| 1 | #!/bin/sh |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # Test suite for om/tls/ec.om (ECDSA P-256/P-384 verification). |
| 4 | # Usage: sh om/tls/test/ec/run.sh PATH_TO_PINNED_OMC |
| 5 | # Builds om2 + sha2.om + ec.om + test/ec/driver.om into build/tls/ec/, runs every vector file |
| 6 | # (Wycheproof, NIST CAVP SigVer, edge cases), library mutants plus an expectation mutant and a |
| 7 | # digest-column mutant (each must be detected: exit 1 with MISMATCH lines), a malformed-input |
| 8 | # corpus (gen_fuzz.py, when python3 is present), the P-256 field and scalar-multiplication vectors of |
| 9 | # gen_p256.py with eight mutants of the p2_* section (p256_driver.om; python3 required) and one timing |
| 10 | # series per curve. |
| 11 | # Exit 0 only when every check passes. Environment: EC_BENCH_N (default 200 verifies per curve), |
| 12 | # EC_FUZZ_N (default 3000 malformed cases), EC_FUZZ_SEED (default 1). |
| 13 | set -eu |
| 14 | omc=${1:?usage: run.sh OMC} |
| 15 | here=$(cd "$(dirname "$0")" && pwd) |
| 16 | root=$(cd "$here/../../../.." && pwd) |
| 17 | out="$root/build/tls/ec" |
| 18 | pin=$(sed -n 's/^OMC_PIN=//p' "$root/om/units.sh") |
| 19 | rm -rf "$out" |
| 20 | mkdir -p "$out" |
| 21 | fail=0 |
| 22 | nice="nice -n 19 ionice -c3" |
| 23 | |
| 24 | sum=$(sha256sum "$omc" | cut -d' ' -f1) |
| 25 | echo "omc $sum" |
| 26 | if [ "$sum" != "$pin" ]; then |
| 27 | echo "FAIL omc is not the pinned compiler $pin" |
| 28 | echo "EC FAIL" |
| 29 | exit 1 |
| 30 | fi |
| 31 | |
| 32 | build() { # build SRC_EC OUT_BINARY |
| 33 | { echo om2; cat "$root/om/tls/sha2.om" "$1" "$here/driver.om"; } > "$2.unit.om" |
| 34 | if ! $nice "$omc" < "$2.unit.om" > "$2" 2> "$2.err" || [ -s "$2.err" ]; then |
| 35 | echo "FAIL build $2: $(cat "$2.err")" |
| 36 | return 1 |
| 37 | fi |
| 38 | chmod +x "$2" |
| 39 | } |
| 40 | |
| 41 | # run BINARY FILE LABEL: the driver must exit 0 with no mismatch |
| 42 | run() { |
| 43 | if $nice "$1" < "$2" > "$out/$3.log" 2>&1; then |
| 44 | echo "PASS $3: $(tail -n 1 "$out/$3.log")" |
| 45 | else |
| 46 | echo "FAIL $3 (exit $?):" |
| 47 | cat "$out/$3.log" |
| 48 | fail=1 |
| 49 | fi |
| 50 | } |
| 51 | |
| 52 | # mutant BINARY FILE LABEL: exit status exactly 1, at least one MISMATCH line and the summary line; |
| 53 | # any other outcome (0, 65 bad input, 100+N trap, a signal) fails, so a crash is not a detection. |
| 54 | mutants=0 |
| 55 | detected=0 |
| 56 | mutant() { |
| 57 | mutants=$((mutants + 1)) |
| 58 | rc=0 |
| 59 | $nice "$1" < "$2" > "$out/$3.log" 2>&1 || rc=$? |
| 60 | nm=$(grep -c '^MISMATCH ' "$out/$3.log" || true) |
| 61 | if [ "$rc" -eq 1 ] && [ "$nm" -gt 0 ] && grep -q '^cases .* mismatches [1-9]' "$out/$3.log"; then |
| 62 | detected=$((detected + 1)) |
| 63 | echo "PASS $3 detected: $nm mismatches, $(tail -n 1 "$out/$3.log")" |
| 64 | else |
| 65 | echo "FAIL $3: exit $rc, $nm MISMATCH lines (need exit 1 and at least one): $(tail -n 1 "$out/$3.log")" |
| 66 | fail=1 |
| 67 | fi |
| 68 | } |
| 69 | |
| 70 | # libmut LABEL SED_EXPR VECTORFILE: a source mutant of ec.om that the vectors must detect |
| 71 | libmut() { |
| 72 | sed "$2" "$root/om/tls/ec.om" > "$out/$1.om" |
| 73 | if cmp -s "$root/om/tls/ec.om" "$out/$1.om"; then |
| 74 | mutants=$((mutants + 1)) |
| 75 | echo "FAIL $1: sed did not change the source" |
| 76 | fail=1 |
| 77 | elif build "$out/$1.om" "$out/$1"; then |
| 78 | mutant "$out/$1" "$here/vectors/$3" "$1" |
| 79 | else |
| 80 | mutants=$((mutants + 1)) |
| 81 | fail=1 |
| 82 | fi |
| 83 | } |
| 84 | |
| 85 | build "$root/om/tls/ec.om" "$out/driver" |
| 86 | echo "ec.om $(sha256sum "$root/om/tls/ec.om" | cut -d' ' -f1) functions $(grep -c '^fn ' "$root/om/tls/ec.om")" |
| 87 | echo "sha2.om $(sha256sum "$root/om/tls/sha2.om" | cut -d' ' -f1)" |
| 88 | echo "driver $(sha256sum "$out/driver" | cut -d' ' -f1)" |
| 89 | |
| 90 | total=0 |
| 91 | wy=0 |
| 92 | for f in "$here"/vectors/*.txt; do |
| 93 | b=$(basename "$f" .txt) |
| 94 | [ "$b" = NOT_RUN ] && continue |
| 95 | n=$(grep -vc '^#' "$f") |
| 96 | total=$((total + n)) |
| 97 | case "$b" in wycheproof_*) wy=$((wy + n)) ;; esac |
| 98 | run "$out/driver" "$f" "$b" |
| 99 | done |
| 100 | cavp=$(grep -vc '^#' "$here/vectors/cavp_sigver.txt") |
| 101 | edge=$(grep -vc '^#' "$here/vectors/edge.txt") |
| 102 | grep '^NOT_RUN' "$here/vectors/NOT_RUN.txt" |
| 103 | |
| 104 | # expectation mutant: the first expected-valid Wycheproof line flipped to expected-invalid |
| 105 | awk 'BEGIN { done = 0 } !done && $4 == "0" { $4 = "1"; done = 1 } { print }' \ |
| 106 | "$here/vectors/wycheproof_secp256r1_sha256.txt" > "$out/mut-expect.txt" |
| 107 | mutant "$out/driver" "$out/mut-expect.txt" mutant-expectation |
| 108 | |
| 109 | # library mutants: curve constant b of P-256 (one limb), the point-addition formula (X3 = r^2 + HHH |
| 110 | # - 2V instead of - HHH), field reduction (Montgomery n0' with 2 instead of 5 Newton steps, 12 bits |
| 111 | # right), and DER strictness (non-minimal INTEGER padding accepted) |
| 112 | libmut mutant-b256 's/^put w 120 0x27d2604b$/put w 120 0x27d2604c/' wycheproof_secp256r1_sha256.txt |
| 113 | libmut mutant-padd 's/^ec_sub w ECCP p p ECA7$/ec_add w ECCP p p ECA7/' wycheproof_secp384r1_sha384.txt |
| 114 | libmut mutant-n0 's/^for k ulong 5$/for k ulong 2/' cavp_sigver.txt |
| 115 | libmut mutant-der 's/^if lt get hs add v 1 128$/if lt get hs add v 1 0/' edge.txt |
| 116 | |
| 117 | # P-256 specialised arithmetic (the p2_* section of ec.om): field operations and both scalar |
| 118 | # multiplications against gen_p256.py (Python integers, textbook affine arithmetic), then source |
| 119 | # mutants of that section that the same vectors must detect. Without python3 this part is NOT_RUN |
| 120 | # and the suite fails (no PASS without the P-256 vectors). |
| 121 | # p2-red: one Solinas term dropped (word 0 without -c14), the deliberately broken reduction |
| 122 | # p2-fold: the fold of the carry at word 3 added instead of subtracted |
| 123 | # p2-carry: the signed carry turned into a logical shift (negative words carried wrongly) |
| 124 | # p2-csub: the final masked subtraction of p never taken |
| 125 | # p2-inv: the inversion chain one squaring short |
| 126 | # p2-formula: one subtraction of the complete addition formula turned into an addition |
| 127 | # p2-sel: the table-select mask from (j xor v) without the - 1 (always 0, wrong entry) |
| 128 | # p2-dig: window digits of 3 bits instead of 4 |
| 129 | p2build() { # p2build SRC_EC OUT_BINARY |
| 130 | { echo om2; cat "$1" "$here/p256_driver.om"; } > "$2.unit.om" |
| 131 | if ! $nice "$omc" < "$2.unit.om" > "$2" 2> "$2.err" || [ -s "$2.err" ]; then |
| 132 | echo "FAIL build $2: $(cat "$2.err")" |
| 133 | return 1 |
| 134 | fi |
| 135 | chmod +x "$2" |
| 136 | } |
| 137 | p2mut() { # p2mut LABEL SED_EXPR |
| 138 | sed "$2" "$root/om/tls/ec.om" > "$out/$1.om" |
| 139 | mutants=$((mutants + 1)) |
| 140 | if cmp -s "$root/om/tls/ec.om" "$out/$1.om"; then |
| 141 | echo "FAIL $1: sed did not change the source" |
| 142 | fail=1 |
| 143 | return |
| 144 | fi |
| 145 | if ! p2build "$out/$1.om" "$out/$1"; then |
| 146 | fail=1 |
| 147 | return |
| 148 | fi |
| 149 | rc=0 |
| 150 | $nice "$out/$1" < "$out/p256.txt" > "$out/$1.log" 2>&1 || rc=$? |
| 151 | nm=$(grep -c '^MISMATCH ' "$out/$1.log" || true) |
| 152 | if [ "$rc" -eq 1 ] && [ "$nm" -gt 0 ]; then |
| 153 | detected=$((detected + 1)) |
| 154 | echo "PASS $1 detected: $nm mismatches, $(tail -n 1 "$out/$1.log")" |
| 155 | else |
| 156 | echo "FAIL $1: exit $rc, $nm MISMATCH lines (need exit 1 and at least one): $(tail -n 1 "$out/$1.log")" |
| 157 | fail=1 |
| 158 | fi |
| 159 | } |
| 160 | if command -v python3 > /dev/null 2>&1; then |
| 161 | python3 "$here/gen_p256.py" "${EC_P256_N:-200}" "${EC_P256_SEED:-1}" > "$out/p256.txt" |
| 162 | p256=$(wc -l < "$out/p256.txt") |
| 163 | total=$((total + p256)) |
| 164 | if p2build "$root/om/tls/ec.om" "$out/p256"; then |
| 165 | run "$out/p256" "$out/p256.txt" p256 |
| 166 | else |
| 167 | fail=1 |
| 168 | fi |
| 169 | p2mut mutant-p2-red 's/^put r 0 sub sub sub sub s get t 11 get t 12 get t 13 get t 14$/put r 0 sub sub sub s get t 11 get t 12 get t 13/' |
| 170 | p2mut mutant-p2-fold 's/^put r 3 sub get r 3 f$/put r 3 add get r 3 f/' |
| 171 | p2mut mutant-p2-carry 's/^cr = bitcast ulong shr bitcast long z 32$/cr = shr z 32/' |
| 172 | p2mut mutant-p2-csub '0,/^mk = sub br 1$/s//mk = ulong 0/' |
| 173 | p2mut mutant-p2-inv 's/^p2_sqn w P2I3 P2I3 30$/p2_sqn w P2I3 P2I3 29/' |
| 174 | p2mut mutant-p2-formula '0,/^p2_sub w P2T3 P2T3 P2T4$/s//p2_add w P2T3 P2T3 P2T4/' |
| 175 | p2mut mutant-p2-sel 's/^m = sub 0 shr sub xor j v 1 63$/m = sub 0 shr xor j v 63/' |
| 176 | p2mut mutant-p2-dig 's/^ret and shr get w add k shr i 3 mul and i 7 4 15$/ret and shr get w add k shr i 3 mul and i 7 4 7/' |
| 177 | else |
| 178 | p256=0 |
| 179 | echo "NOT_RUN p256 vectors: python3 not found" |
| 180 | fail=1 |
| 181 | fi |
| 182 | |
| 183 | # digest cross-check control: the hashlib digest column of the first case damaged; the driver's |
| 184 | # sha2_digest comparison must report it |
| 185 | awk 'BEGIN { done = 0 } !done && !/^#/ { $9 = (substr($9, 1, 1) == "0" ? "1" : "0") substr($9, 2); done = 1 } { print }' \ |
| 186 | "$here/vectors/wycheproof_secp384r1_sha384.txt" > "$out/mut-digest.txt" |
| 187 | mutant "$out/driver" "$out/mut-digest.txt" mutant-digest-column |
| 188 | |
| 189 | # malformed-input corpus (python test tooling; skipped when python3 is absent): every case must be |
| 190 | # refused without a trap |
| 191 | if command -v python3 > /dev/null 2>&1; then |
| 192 | python3 "$here/gen_fuzz.py" "$here/vectors" "$out/fuzz.txt" "${EC_FUZZ_N:-3000}" "${EC_FUZZ_SEED:-1}" |
| 193 | run "$out/driver" "$out/fuzz.txt" fuzz |
| 194 | else |
| 195 | echo "NOTE fuzz corpus skipped: python3 not found" |
| 196 | fi |
| 197 | |
| 198 | # timing: the first valid DER line of the P-256/SHA-256 and P-384/SHA-384 Wycheproof files |
| 199 | bn=${EC_BENCH_N:-200} |
| 200 | for c in secp256r1_sha256 secp384r1_sha384; do |
| 201 | awk '$4 == "0" && $3 == "d" { print; exit }' "$here/vectors/wycheproof_$c.txt" > "$out/bench-$c.txt" |
| 202 | if $nice "$out/driver" "$bn" < "$out/bench-$c.txt" > "$out/bench-$c.log" 2>&1; then |
| 203 | ns=$(awk '{ print $10 }' "$out/bench-$c.log") |
| 204 | echo "TIME $c: $bn verifies, $ns ns total, $((ns / bn)) ns per verify" |
| 205 | else |
| 206 | echo "FAIL bench-$c" |
| 207 | cat "$out/bench-$c.log" |
| 208 | fail=1 |
| 209 | fi |
| 210 | done |
| 211 | |
| 212 | if [ "$fail" -ne 0 ]; then |
| 213 | echo "EC FAIL vectors=$total (wycheproof=$wy cavp=$cavp edge=$edge p256=$p256) mutants=$detected/$mutants" |
| 214 | exit 1 |
| 215 | fi |
| 216 | echo "EC PASS vectors=$total (wycheproof=$wy cavp=$cavp edge=$edge p256=$p256) mutants=$detected/$mutants" |