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 / sha2 / run.sh
| 1 | #!/bin/sh |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # Test suite for om/tls/sha2.om and om/tls/hkdf.om. Usage (from the omega-mv root): |
| 4 | # sh om/tls/test/sha2/run.sh /tmp/omtls/seed/out/omc |
| 5 | # Builds into build/tls/sha2/. Exits 0 only when every vector passes and every mutant and negative |
| 6 | # control is detected. Final line: SHA2 PASS vectors=N (wycheproof=W) mutants=M/M, or SHA2 FAIL. |
| 7 | set -eu |
| 8 | omc=${1:?usage: run.sh OMC} |
| 9 | here=$(cd "$(dirname "$0")" && pwd) |
| 10 | lib=$(cd "$here/../.." && pwd) |
| 11 | root=$(cd "$lib/../.." && pwd) |
| 12 | out="$root/build/tls/sha2" |
| 13 | vec="$here/vectors" |
| 14 | mkdir -p "$out" |
| 15 | nice="nice -n 19 ionice -c3" |
| 16 | checks=0 |
| 17 | vectors=0 |
| 18 | wyche=0 |
| 19 | mut=0 |
| 20 | mutok=0 |
| 21 | bad=0 |
| 22 | |
| 23 | say() { printf '%s\n' "$*"; } |
| 24 | fail() { say "FAIL $*"; bad=1; } |
| 25 | |
| 26 | # build SHA2 HKDF BIN: om2 line, the two libraries, the driver; compile with omc |
| 27 | build() { |
| 28 | { printf 'om2\n'; cat "$1" "$2" "$here/driver.om"; } > "$3.om" |
| 29 | if ! $nice "$omc" < "$3.om" > "$3" 2> "$3.err" || [ -s "$3.err" ]; then |
| 30 | say "omc refused $3.om: $(cat "$3.err")" |
| 31 | return 1 |
| 32 | fi |
| 33 | chmod +x "$3" |
| 34 | } |
| 35 | |
| 36 | # vectors in FILE: one per record line, 100 per Monte record |
| 37 | count() { |
| 38 | awk '/^[HGTPX] / { n++ } /^M / { n += 100 } END { print n + 0 }' "$1" |
| 39 | } |
| 40 | |
| 41 | # run_ok NAME FILE: exit 0 and "sha2 driver: N checks, 0 failed", N > 0 |
| 42 | run_ok() { |
| 43 | set +e |
| 44 | $nice "$out/driver" < "$2" > "$out/$1.out" 2>&1 |
| 45 | rc=$? |
| 46 | set -e |
| 47 | line=$(tail -n 1 "$out/$1.out") |
| 48 | n=$(printf '%s\n' "$line" | awk '$1 == "sha2" && $2 == "driver:" && $4 == "checks," && $5 == "0" && $6 == "failed" { print $3 }') |
| 49 | if [ "$rc" -eq 0 ] && [ -n "$n" ] && [ "$n" -gt 0 ]; then |
| 50 | checks=$((checks + n)) |
| 51 | v=$(count "$2") |
| 52 | vectors=$((vectors + v)) |
| 53 | say "PASS $1: $v vectors, $line" |
| 54 | else |
| 55 | fail "$1: exit $rc: $(tail -n 3 "$out/$1.out" | tr '\n' ' ')" |
| 56 | fi |
| 57 | } |
| 58 | |
| 59 | # detected NAME FILE BIN [WANT]: BIN completed normally and reported a difference: exit status |
| 60 | # exactly 1 (a trap exits 100+N, a signal 128+N) and a complete summary line with F > 0 failures |
| 61 | # (exactly WANT when given) |
| 62 | detected() { |
| 63 | set +e |
| 64 | $nice "$3" < "$2" > "$out/$1.out" 2>&1 |
| 65 | rc=$? |
| 66 | set -e |
| 67 | last=$(tail -n 1 "$out/$1.out") |
| 68 | f=$(printf '%s\n' "$last" | awk '$1 == "sha2" && $2 == "driver:" && $4 == "checks," && $6 == "failed" { print $5 }') |
| 69 | ok=0 |
| 70 | if [ "$rc" -eq 1 ] && [ -n "$f" ] && [ "$f" -gt 0 ]; then |
| 71 | ok=1 |
| 72 | if [ $# -ge 4 ] && [ "$f" -ne "$4" ]; then |
| 73 | ok=0 |
| 74 | fi |
| 75 | fi |
| 76 | if [ "$ok" -eq 1 ]; then |
| 77 | say "PASS $1: detected ($(grep -c '^MISMATCH\|^BAD' "$out/$1.out") failure lines; $last)" |
| 78 | return 0 |
| 79 | fi |
| 80 | fail "$1: not detected: exit $rc: $last" |
| 81 | return 1 |
| 82 | } |
| 83 | |
| 84 | # mutant NAME FILE SED_EXPR WHICH: apply SED_EXPR to sha2.om (WHICH sha2) or hkdf.om (WHICH hkdf), |
| 85 | # rebuild the driver, and require detection on FILE |
| 86 | mutant() { |
| 87 | mut=$((mut + 1)) |
| 88 | s="$lib/sha2.om" |
| 89 | h="$lib/hkdf.om" |
| 90 | if [ "$4" = sha2 ]; then |
| 91 | sed "$3" "$lib/sha2.om" > "$out/mut-$1.lib" |
| 92 | cmp -s "$lib/sha2.om" "$out/mut-$1.lib" && { fail "$1: sed changed nothing"; return; } |
| 93 | s="$out/mut-$1.lib" |
| 94 | else |
| 95 | sed "$3" "$lib/hkdf.om" > "$out/mut-$1.lib" |
| 96 | cmp -s "$lib/hkdf.om" "$out/mut-$1.lib" && { fail "$1: sed changed nothing"; return; } |
| 97 | h="$out/mut-$1.lib" |
| 98 | fi |
| 99 | build "$s" "$h" "$out/mut-$1" || { fail "$1: mutant did not compile"; return; } |
| 100 | if detected "mutant-$1" "$2" "$out/mut-$1"; then |
| 101 | mutok=$((mutok + 1)) |
| 102 | fi |
| 103 | } |
| 104 | |
| 105 | build "$lib/sha2.om" "$lib/hkdf.om" "$out/driver" || { say "SHA2 FAIL build"; exit 1; } |
| 106 | say "omc sha256 $(sha256sum "$omc" | cut -d' ' -f1)" |
| 107 | say "sha2.om sha256 $(sha256sum "$lib/sha2.om" | cut -d' ' -f1)" |
| 108 | say "hkdf.om sha256 $(sha256sum "$lib/hkdf.om" | cut -d' ' -f1)" |
| 109 | |
| 110 | # NIST CAVP SHAVS byte-oriented .rsp -> driver records (Len is in bits; Msg is "00" when Len = 0) |
| 111 | for a in 256 384 512; do |
| 112 | for k in ShortMsg LongMsg; do |
| 113 | awk -v a=$a '{ sub(/\r$/, "") } |
| 114 | /^Len = / { n = $3 } |
| 115 | /^Msg = / { m = $3 } |
| 116 | /^MD = / { if (n == 0) m = "-"; print "H", a, m, $3 }' \ |
| 117 | "$vec/SHA$a$k.rsp" > "$out/nist-$a-$k.txt" |
| 118 | done |
| 119 | awk -v a=$a '{ sub(/\r$/, "") } |
| 120 | /^Seed = / { printf "M %s %s", a, $3 } |
| 121 | /^MD = / { printf " %s", $3; k++ } |
| 122 | END { print ""; if (k != 100) exit 1 }' \ |
| 123 | "$vec/SHA${a}Monte.rsp" > "$out/nist-$a-Monte.txt" |
| 124 | done |
| 125 | for a in 256 384 512; do |
| 126 | for k in ShortMsg LongMsg Monte; do |
| 127 | run_ok "nist-sha$a-$k" "$out/nist-$a-$k.txt" |
| 128 | done |
| 129 | done |
| 130 | run_ok rfc4231-hmac "$vec/rfc4231-hmac.txt" |
| 131 | run_ok rfc5869-hkdf "$vec/rfc5869-hkdf.txt" |
| 132 | run_ok generated-long "$vec/generated-long.txt" |
| 133 | w0=$vectors |
| 134 | run_ok wycheproof-hmac "$vec/wycheproof-hmac.txt" |
| 135 | run_ok wycheproof-hkdf "$vec/wycheproof-hkdf.txt" |
| 136 | wyche=$((vectors - w0)) |
| 137 | |
| 138 | # source mutants (each must give MISMATCH lines and exit 1, never a trap) |
| 139 | mutant sha256-k0 "$out/nist-256-ShortMsg.txt" 's/^put k 0 0x428a2f98$/put k 0 0x428a2f99/' sha2 |
| 140 | mutant sha512-k0 "$out/nist-512-ShortMsg.txt" 's/^put k 0 0x428a2f98d728ae22$/put k 0 0x428a2f98d728ae23/' sha2 |
| 141 | mutant hmac-ipad "$vec/rfc4231-hmac.txt" 's/^put pad i xor get kb i 0x36$/put pad i xor get kb i 0x37/' hkdf |
| 142 | mutant hkdf-ctr "$vec/wycheproof-hkdf.txt" 's/^put t 64 uchar n$/put t 64 uchar xor n 1/' hkdf |
| 143 | |
| 144 | # expectation mutant: one MD hex digit changed -> exactly 2 failures (one-shot and split) |
| 145 | mut=$((mut + 1)) |
| 146 | awk 'NR == 10 { c = substr($4, 1, 1); $4 = (c == "0" ? "1" : "0") substr($4, 2) } { print }' \ |
| 147 | "$out/nist-256-ShortMsg.txt" > "$out/mut-expect.txt" |
| 148 | if cmp -s "$out/nist-256-ShortMsg.txt" "$out/mut-expect.txt"; then |
| 149 | fail "expectation mutant changed nothing" |
| 150 | elif detected mutant-expect "$out/mut-expect.txt" "$out/driver" 2; then |
| 151 | mutok=$((mutok + 1)) |
| 152 | fi |
| 153 | |
| 154 | # negative controls: a malformed record (odd hex digit count) is refused; a valid HMAC vector with |
| 155 | # one tag byte flipped is refused (the driver also flips a message byte of every valid T record and |
| 156 | # an OKM byte of every valid X record, see driver.om) |
| 157 | printf 'H 256 61626 ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad\n' > "$out/neg-malformed.txt" |
| 158 | detected neg-malformed "$out/neg-malformed.txt" "$out/driver" 1 || true |
| 159 | awk 'NR == 4 { c = substr($5, 1, 2); $5 = (c == "00" ? "01" : "00") substr($5, 3) } { print }' \ |
| 160 | "$vec/rfc4231-hmac.txt" > "$out/neg-tag.txt" |
| 161 | detected neg-tag-flip "$out/neg-tag.txt" "$out/driver" || true |
| 162 | |
| 163 | if [ "$bad" -ne 0 ]; then |
| 164 | say "SHA2 FAIL vectors=$vectors (wycheproof=$wyche) mutants=$mutok/$mut" |
| 165 | exit 1 |
| 166 | fi |
| 167 | say "SHA2 PASS vectors=$vectors (wycheproof=$wyche) mutants=$mutok/$mut" |