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 / ecdh / run.sh
| 1 | #!/bin/sh |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # Test suite for om/tls/ecdh.om (ECDH P-256/P-384, SP 800-56A Rev. 3 ECC CDH primitive). |
| 4 | # Usage: sh om/tls/test/ecdh/run.sh PATH_TO_PINNED_OMC |
| 5 | # Builds om2 + ec.om + ecdh.om + test/ecdh/driver.om into build/tls/ecdh/, runs every vector file |
| 6 | # (NIST CAVP ECC CDH primitive P-256/P-384, Wycheproof ecdh secp256r1/secp384r1, edge cases), then |
| 7 | # library mutants and an expectation mutant (each must be detected: exit 1 with MISMATCH lines) and |
| 8 | # one timing series per curve. Exit 0 only when every check passes. |
| 9 | # Environment: ECDH_BENCH_N (default 100 shared-secret computations per curve). |
| 10 | set -eu |
| 11 | omc=${1:?usage: run.sh OMC} |
| 12 | here=$(cd "$(dirname "$0")" && pwd) |
| 13 | root=$(cd "$here/../../../.." && pwd) |
| 14 | out="$root/build/tls/ecdh" |
| 15 | pin=$(sed -n 's/^OMC_PIN=//p' "$root/om/units.sh") |
| 16 | rm -rf "$out" |
| 17 | mkdir -p "$out" |
| 18 | fail=0 |
| 19 | nice="nice -n 19 ionice -c3" |
| 20 | |
| 21 | sum=$(sha256sum "$omc" | cut -d' ' -f1) |
| 22 | echo "omc $sum" |
| 23 | if [ "$sum" != "$pin" ]; then |
| 24 | echo "FAIL omc is not the pinned compiler $pin" |
| 25 | echo "ECDH FAIL" |
| 26 | exit 1 |
| 27 | fi |
| 28 | |
| 29 | build() { # build SRC_ECDH OUT_BINARY [SRC_EC] |
| 30 | { echo om2; cat "${3:-$root/om/tls/ec.om}" "$1" "$here/driver.om"; } > "$2.unit.om" |
| 31 | if ! $nice "$omc" < "$2.unit.om" > "$2" 2> "$2.err" || [ -s "$2.err" ]; then |
| 32 | echo "FAIL build $2: $(cat "$2.err")" |
| 33 | return 1 |
| 34 | fi |
| 35 | chmod +x "$2" |
| 36 | } |
| 37 | |
| 38 | # run BINARY FILE LABEL: the driver must exit 0 with no mismatch |
| 39 | run() { |
| 40 | if $nice "$1" < "$2" > "$out/$3.log" 2>&1; then |
| 41 | echo "PASS $3: $(tail -n 1 "$out/$3.log")" |
| 42 | else |
| 43 | echo "FAIL $3 (exit $?):" |
| 44 | cat "$out/$3.log" |
| 45 | fail=1 |
| 46 | fi |
| 47 | } |
| 48 | |
| 49 | # mutant BINARY FILE LABEL: exit status exactly 1, at least one MISMATCH line and the summary line; |
| 50 | # any other outcome (0, 65 bad input, 100+N trap, a signal) fails, so a crash is not a detection. |
| 51 | mutants=0 |
| 52 | detected=0 |
| 53 | mutant() { |
| 54 | mutants=$((mutants + 1)) |
| 55 | rc=0 |
| 56 | $nice "$1" < "$2" > "$out/$3.log" 2>&1 || rc=$? |
| 57 | nm=$(grep -c '^MISMATCH ' "$out/$3.log" || true) |
| 58 | if [ "$rc" -eq 1 ] && [ "$nm" -gt 0 ] && grep -q '^cases .* mismatches [1-9]' "$out/$3.log"; then |
| 59 | detected=$((detected + 1)) |
| 60 | echo "PASS $3 detected: $nm mismatches, $(tail -n 1 "$out/$3.log")" |
| 61 | else |
| 62 | echo "FAIL $3: exit $rc, $nm MISMATCH lines (need exit 1 and at least one): $(tail -n 1 "$out/$3.log")" |
| 63 | fail=1 |
| 64 | fi |
| 65 | } |
| 66 | |
| 67 | # libmut LABEL SED_SCRIPT VECTORFILE: a source mutant of ecdh.om that the vectors must detect |
| 68 | libmut() { |
| 69 | sed "$2" "$root/om/tls/ecdh.om" > "$out/$1.om" |
| 70 | if cmp -s "$root/om/tls/ecdh.om" "$out/$1.om"; then |
| 71 | mutants=$((mutants + 1)) |
| 72 | echo "FAIL $1: sed did not change the source" |
| 73 | fail=1 |
| 74 | elif build "$out/$1.om" "$out/$1"; then |
| 75 | mutant "$out/$1" "$here/vectors/$3" "$1" |
| 76 | else |
| 77 | mutants=$((mutants + 1)) |
| 78 | fail=1 |
| 79 | fi |
| 80 | } |
| 81 | |
| 82 | # ecmut LABEL SED_SCRIPT VECTORFILE: a source mutant of ec.om (the P-256 p2_* path that ecd_mult |
| 83 | # uses) built with the unchanged ecdh.om; the vectors must detect it |
| 84 | ecmut() { |
| 85 | sed "$2" "$root/om/tls/ec.om" > "$out/$1.ec.om" |
| 86 | if cmp -s "$root/om/tls/ec.om" "$out/$1.ec.om"; then |
| 87 | mutants=$((mutants + 1)) |
| 88 | echo "FAIL $1: sed did not change the source" |
| 89 | fail=1 |
| 90 | elif build "$root/om/tls/ecdh.om" "$out/$1" "$out/$1.ec.om"; then |
| 91 | mutant "$out/$1" "$here/vectors/$3" "$1" |
| 92 | else |
| 93 | mutants=$((mutants + 1)) |
| 94 | fail=1 |
| 95 | fi |
| 96 | } |
| 97 | |
| 98 | build "$root/om/tls/ecdh.om" "$out/driver" |
| 99 | echo "ecdh.om $(sha256sum "$root/om/tls/ecdh.om" | cut -d' ' -f1) functions $(grep -c '^fn ' "$root/om/tls/ecdh.om")" |
| 100 | echo "ec.om $(sha256sum "$root/om/tls/ec.om" | cut -d' ' -f1)" |
| 101 | echo "driver $(sha256sum "$out/driver" | cut -d' ' -f1)" |
| 102 | |
| 103 | total=0 |
| 104 | wy=0 |
| 105 | for f in "$here"/vectors/*.txt; do |
| 106 | b=$(basename "$f" .txt) |
| 107 | [ "$b" = NOT_RUN ] && continue |
| 108 | n=$(grep -vc '^#' "$f") |
| 109 | total=$((total + n)) |
| 110 | case "$b" in wycheproof_*) wy=$((wy + n)) ;; esac |
| 111 | run "$out/driver" "$f" "$b" |
| 112 | done |
| 113 | cavp=$(grep -vc '^#' "$here/vectors/cavp_cdh.txt") |
| 114 | edge=$(grep -vc '^#' "$here/vectors/edge.txt") |
| 115 | grep '^NOT_RUN' "$here/vectors/NOT_RUN.txt" |
| 116 | |
| 117 | # expectation mutant: the first CAVP shared-secret line expected to be refused (3) instead of 0 |
| 118 | awk 'BEGIN { done = 0 } !done && $2 == "S" && $3 == "0" { $3 = "3"; done = 1 } { print }' \ |
| 119 | "$here/vectors/cavp_cdh.txt" > "$out/mut-expect.txt" |
| 120 | mutant "$out/driver" "$out/mut-expect.txt" mutant-expectation |
| 121 | |
| 122 | # library mutants: |
| 123 | # peer: partial public-key validation skipped (invalid-curve and off-curve points accepted) |
| 124 | # step: ladder step order swapped (R0 doubled before R1 = R0 + R1, wrong ladder) |
| 125 | # swap: conditional swap from the bit alone, not bit xor previous bit |
| 126 | # bits: ladder over 32L - 1 bits (top scalar bit ignored) |
| 127 | # formula: one subtraction of the complete addition formula turned into an addition |
| 128 | # csub: constant-time final subtraction mask forced to 0 (result left unreduced) |
| 129 | # range: scalar range check [1, n-1] always passing |
| 130 | # pct: pair-wise consistency comparison ignored |
| 131 | libmut mutant-peer 's/^rc = ec_pub w p$/rc = ulong 0/' wycheproof_secp256r1_ecpoint.txt |
| 132 | libmut mutant-step '/^ecd_padd w EDR1 EDR0 EDR1$/{h;d};/^ecd_padd w EDR0 EDR0 EDR0$/G' cavp_cdh.txt |
| 133 | libmut mutant-swap 's/^sw = xor sw b$/sw = b/' cavp_cdh.txt |
| 134 | libmut mutant-bits 's/^nb = mul get w 0 32$/nb = sub mul get w 0 32 1/' cavp_cdh.txt |
| 135 | libmut mutant-formula '0,/^ecd_sub w ECCP EDT3 EDT3 EDT4$/s//ecd_add w ECCP EDT3 EDT3 EDT4/' cavp_cdh.txt |
| 136 | libmut mutant-csub 's/^mk = sub shr x 63 1$/mk = ulong 0/' cavp_cdh.txt |
| 137 | libmut mutant-range 's/^ret and br shr or z sub 0 z 63$/ret 1/' edge.txt |
| 138 | libmut mutant-pct 's/^if or ne rc 0 ne acc 0$/if ne rc 0/' edge.txt |
| 139 | |
| 140 | # P-256 mutants (ecd_mult routes P-256 through p2_smul of ec.om): |
| 141 | # p2-out: ecd_mult copies the result's Y limbs into X |
| 142 | # p2-sel: (ec.om) table-select mask from (j xor v) without the - 1 (always 0, wrong entry) |
| 143 | # p2-dig: (ec.om) window digits of 3 bits instead of 4 |
| 144 | # p2-red: (ec.om) one Solinas term dropped from word 0 of the reduction |
| 145 | libmut mutant-p2-out 's/^put w add EDR0 j get v add P2O j$/put w add EDR0 j get v add add P2O 8 j/' cavp_cdh.txt |
| 146 | ecmut mutant-p2-sel 's/^m = sub 0 shr sub xor j v 1 63$/m = sub 0 shr xor j v 63/' cavp_cdh.txt |
| 147 | ecmut 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/' cavp_cdh.txt |
| 148 | ecmut 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/' cavp_cdh.txt |
| 149 | |
| 150 | # timing: the first CAVP P-256 and P-384 shared-secret lines, ECDH_BENCH_N calls each |
| 151 | bn=${ECDH_BENCH_N:-100} |
| 152 | for c in 256 384; do |
| 153 | awk -v c="cavp-$c-" '$2 == "S" && index($1, c) == 1 { print; exit }' "$here/vectors/cavp_cdh.txt" > "$out/bench-$c.txt" |
| 154 | if $nice "$out/driver" "$bn" < "$out/bench-$c.txt" > "$out/bench-$c.log" 2>&1; then |
| 155 | ns=$(awk '{ print $10 }' "$out/bench-$c.log") |
| 156 | echo "TIME P-$c: $bn shared-secret computations, $ns ns total, $((ns / bn)) ns per call" |
| 157 | else |
| 158 | echo "FAIL bench-$c" |
| 159 | cat "$out/bench-$c.log" |
| 160 | fail=1 |
| 161 | fi |
| 162 | done |
| 163 | |
| 164 | if [ "$fail" -ne 0 ]; then |
| 165 | echo "ECDH FAIL vectors=$total (cavp=$cavp wycheproof=$wy edge=$edge) mutants=$detected/$mutants" |
| 166 | exit 1 |
| 167 | fi |
| 168 | echo "ECDH PASS vectors=$total (cavp=$cavp wycheproof=$wy edge=$edge) mutants=$detected/$mutants" |