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 / gcmsiv / run.sh
| 1 | #!/bin/sh |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # Test suite for om/tls/gcmsiv.om (AES-GCM-SIV, RFC 8452). |
| 4 | # Usage: sh om/tls/test/gcmsiv/run.sh OMC (OMC = the pinned seed compiler, seed/out/omc) |
| 5 | # Every test vector of RFC 8452 Appendix C (C.1 AEAD_AES_128_GCM_SIV, C.2 AEAD_AES_256_GCM_SIV, |
| 6 | # C.3 counter wrap) is converted from the committed RFC text at test time, with every intermediate |
| 7 | # value the RFC lists (derived keys, POLYVAL result, masked value, tag, initial counter) checked |
| 8 | # stage by stage, plus the section 8 worked example. Exits 0 only when every vector and refusal |
| 9 | # passes and every mutant is detected. Final line: |
| 10 | # GCMSIV PASS vectors=N (rfc8452 c1=24 c2=24 c3=2 s8=1) mutants=M/M |
| 11 | set -eu |
| 12 | omc=${1:?usage: run.sh OMC} |
| 13 | here=$(cd "$(dirname "$0")" && pwd) |
| 14 | tls=$(cd "$here/../.." && pwd) |
| 15 | gcm="$tls/aesgcm.om" |
| 16 | lib="$tls/gcmsiv.om" |
| 17 | root=$(cd "$here/../../../.." && pwd) |
| 18 | out="$root/build/tls/gcmsiv" |
| 19 | vec="$here/vectors" |
| 20 | rfc_sha=ca48ec466b401ce5d68d6d1628127a1800adcc16f2573646aaa3d7df284d3c17 |
| 21 | run() { nice -n 19 ionice -c3 "$@"; } |
| 22 | mkdir -p "$out" |
| 23 | total=0 |
| 24 | mut=0 |
| 25 | mutok=0 |
| 26 | bad=0 |
| 27 | |
| 28 | say() { printf '%s\n' "$*"; } |
| 29 | fail() { say "FAIL $*"; bad=1; } |
| 30 | |
| 31 | # build LIB PROGRAM BIN: `om2`, aesgcm.om, LIB (a gcmsiv.om), the program; accepted only with rc 0 |
| 32 | # and empty stderr |
| 33 | build() { |
| 34 | { printf 'om2\n'; cat "$gcm" "$1" "$2"; } > "$3.om" |
| 35 | if run "$omc" < "$3.om" > "$3" 2> "$3.err" && [ ! -s "$3.err" ]; then |
| 36 | chmod +x "$3" |
| 37 | return 0 |
| 38 | fi |
| 39 | say "omc refused $3.om: $(cat "$3.err")" |
| 40 | return 1 |
| 41 | } |
| 42 | |
| 43 | # run_ok NAME FILE WANT: exit 0 and a complete "gcmsiv driver: WANT checks, 0 failed" line |
| 44 | run_ok() { |
| 45 | set +e |
| 46 | run "$out/driver" < "$2" > "$out/$1.out" 2>&1 |
| 47 | rc=$? |
| 48 | set -e |
| 49 | line=$(tail -n 1 "$out/$1.out") |
| 50 | n=$(printf '%s\n' "$line" | awk '$1 == "gcmsiv" && $2 == "driver:" && $4 == "checks," && $5 == "0" && $6 == "failed" { print $3 }') |
| 51 | if [ "$rc" -eq 0 ] && [ -n "$n" ] && [ "$n" -gt 0 ] && [ "$n" -eq "$3" ]; then |
| 52 | total=$((total + n)) |
| 53 | say "PASS $1: $line" |
| 54 | else |
| 55 | fail "$1: exit $rc, want $3 checks: $(tail -n 3 "$out/$1.out" | tr '\n' ' ')" |
| 56 | fi |
| 57 | } |
| 58 | |
| 59 | # run_detect NAME FILE BIN STAGE [WANT]: detected only when BIN exits exactly 1 (a trap exits |
| 60 | # 100+N, a signal 128+N), prints a complete summary line with F > 0 (F = WANT when given), one |
| 61 | # MISMATCH line per failure, no BAD line, and at least one mismatch at STAGE |
| 62 | run_detect() { |
| 63 | mut=$((mut + 1)) |
| 64 | set +e |
| 65 | run "$3" < "$2" > "$out/$1.out" 2>&1 |
| 66 | rc=$? |
| 67 | set -e |
| 68 | line=$(tail -n 1 "$out/$1.out") |
| 69 | f=$(printf '%s\n' "$line" | awk '$1 == "gcmsiv" && $2 == "driver:" && $4 == "checks," && $6 == "failed" { print $5 }') |
| 70 | nm=$(grep -c '^MISMATCH line ' "$out/$1.out" || true) |
| 71 | nb=$(grep -c '^BAD line ' "$out/$1.out" || true) |
| 72 | ns=$(grep -c "^MISMATCH line [0-9]* stage $4\$" "$out/$1.out" || true) |
| 73 | stages=$(awk '$1 == "MISMATCH" { print $5 }' "$out/$1.out" | sort | uniq -c | awk '{ printf "%s%s=%s", s, $2, $1; s = "," }') |
| 74 | if [ "$rc" -eq 1 ] && [ -n "$f" ] && [ "$f" -gt 0 ] && [ "$nm" -eq "$f" ] && [ "$nb" -eq 0 ] && |
| 75 | [ "$ns" -gt 0 ] && { [ $# -lt 5 ] || [ "$f" -eq "$5" ]; }; then |
| 76 | mutok=$((mutok + 1)) |
| 77 | say "PASS $1: detected at stage $4, exit 1, $nm MISMATCH ($stages) ($line)" |
| 78 | else |
| 79 | fail "$1: not detected at stage $4: exit $rc, MISMATCH $nm ($stages), BAD $nb, last: $line" |
| 80 | fi |
| 81 | } |
| 82 | |
| 83 | # mutant NAME STAGE SED: gcmsiv.om with one sed substitution (which must change exactly one line), |
| 84 | # compiled with the driver and run on every RFC vector; must be detected at STAGE |
| 85 | mutant() { |
| 86 | sed "$3" "$lib" > "$out/lib-$1.om" |
| 87 | nd=$(diff "$lib" "$out/lib-$1.om" | grep -c '^>' || true) |
| 88 | if [ "$nd" -ne 1 ]; then |
| 89 | mut=$((mut + 1)) |
| 90 | fail "mutant $1: sed changed $nd lines, want 1" |
| 91 | return |
| 92 | fi |
| 93 | if build "$out/lib-$1.om" "$here/driver.om" "$out/mut-$1"; then |
| 94 | run_detect "mutant-$1" "$out/rfc.txt" "$out/mut-$1" "$2" |
| 95 | else |
| 96 | mut=$((mut + 1)) |
| 97 | fail "mutant $1: build refused" |
| 98 | fi |
| 99 | } |
| 100 | |
| 101 | say "omc sha256 $(sha256sum "$omc" | cut -d' ' -f1)" |
| 102 | build "$lib" "$here/driver.om" "$out/driver" || { say "GCMSIV FAIL (driver build)"; exit 1; } |
| 103 | say "library sha256 $(sha256sum "$lib" | cut -d' ' -f1)" |
| 104 | say "library functions $(grep -c '^fn ' "$lib")" |
| 105 | |
| 106 | # RFC 8452 text, committed unchanged (https://www.rfc-editor.org/rfc/rfc8452.txt) |
| 107 | if [ "$(sha256sum "$vec/rfc8452.txt" | cut -d' ' -f1)" != "$rfc_sha" ]; then |
| 108 | say "GCMSIV FAIL (rfc8452.txt sha256 mismatch)" |
| 109 | exit 1 |
| 110 | fi |
| 111 | |
| 112 | # Appendix C, every record: one G line per "Plaintext (N bytes) =" field. Field lines are |
| 113 | # " Name = hex"; a value continues on deeper-indented hex-only lines (also across a page break). |
| 114 | # The converter checks every declared byte count, the key/nonce/intermediate sizes, that the result |
| 115 | # is 16 bytes longer than the plaintext, and the record counts per appendix section. |
| 116 | awk '{ sub(/\r$/, ""); sub(/\f/, "") } |
| 117 | function h(s) { return s == "" ? "-" : s } |
| 118 | function die(m) { print "rfc8452 converter: " m " (record at line " ln ")" > "/dev/stderr"; err = 1; exit 1 } |
| 119 | function flush() { |
| 120 | if (ln == 0) return |
| 121 | if (length(v["P"]) != 2 * pb) die("plaintext length") |
| 122 | if (length(v["A"]) != 2 * ab) die("aad length") |
| 123 | if (length(v["R"]) != 2 * rb || rb != pb + 16) die("result length") |
| 124 | if (length(v["K"]) != 32 && length(v["K"]) != 64) die("key length") |
| 125 | if (length(v["E"]) != length(v["K"])) die("encryption key length") |
| 126 | if (length(v["N"]) != 24) die("nonce length") |
| 127 | if (length(v["H"]) != 32 || length(v["S"]) != 32 || length(v["M"]) != 32) die("intermediate length") |
| 128 | if (length(v["T"]) != 32 || length(v["C"]) != 32) die("tag or counter length") |
| 129 | print "G", ln, v["K"], v["N"], h(v["A"]), h(v["P"]), v["H"], v["E"], v["S"], v["M"], v["T"], v["C"], v["R"] |
| 130 | cnt[sec]++ |
| 131 | ln = 0 |
| 132 | } |
| 133 | /^Gueron, et al\./ { next } |
| 134 | /^RFC 8452 / { next } |
| 135 | /^$/ { next } |
| 136 | /^C\.1\. AEAD_AES_128_GCM_SIV$/ { flush(); sec = "c1"; next } |
| 137 | /^C\.2\. AEAD_AES_256_GCM_SIV$/ { flush(); sec = "c2"; next } |
| 138 | /^C\.3\. Counter Wrap Tests$/ { flush(); sec = "c3"; next } |
| 139 | /^Acknowledgements$/ { flush(); sec = ""; next } |
| 140 | sec == "" { next } |
| 141 | /^ [^ ].* =/ { |
| 142 | name = $0; sub(/ =.*/, "", name); sub(/^ /, "", name) |
| 143 | val = $0; sub(/^[^=]*= */, "", val); gsub(/ /, "", val) |
| 144 | if (name ~ /^Plaintext \([0-9]+ bytes\)$/) { |
| 145 | flush(); ln = NR; split("", v); pb = name; gsub(/[^0-9]/, "", pb); pb += 0; f = "P" |
| 146 | } else if (name ~ /^AAD \([0-9]+ bytes\)$/) { |
| 147 | ab = name; gsub(/[^0-9]/, "", ab); ab += 0; f = "A" |
| 148 | } else if (name ~ /^Result \([0-9]+ bytes\)$/) { |
| 149 | rb = name; gsub(/[^0-9]/, "", rb); rb += 0; f = "R" |
| 150 | } else if (name == "Key") f = "K" |
| 151 | else if (name == "Nonce") f = "N" |
| 152 | else if (name == "Record authentication key") f = "H" |
| 153 | else if (name == "Record encryption key") f = "E" |
| 154 | else if (name == "POLYVAL input") f = "I" |
| 155 | else if (name == "POLYVAL result") f = "S" |
| 156 | else if (name == "POLYVAL result XOR nonce") f = "X" |
| 157 | else if (name == "... and masked") f = "M" |
| 158 | else if (name == "Tag") f = "T" |
| 159 | else if (name == "Initial counter") f = "C" |
| 160 | else die("unknown field " name) |
| 161 | if (val !~ /^[0-9a-f]*$/) die("non-hex value in " name) |
| 162 | v[f] = val |
| 163 | next |
| 164 | } |
| 165 | /^ +[0-9a-f]+$/ { if (f == "") die("orphan continuation"); val = $0; gsub(/ /, "", val); v[f] = v[f] val; next } |
| 166 | { f = "" } |
| 167 | END { |
| 168 | if (err) exit 1 |
| 169 | flush() |
| 170 | if (cnt["c1"] != 24 || cnt["c2"] != 24 || cnt["c3"] != 2) { |
| 171 | print "rfc8452 converter: counts c1=" cnt["c1"] " c2=" cnt["c2"] " c3=" cnt["c3"] ", want 24 24 2" > "/dev/stderr" |
| 172 | exit 1 |
| 173 | } |
| 174 | }' "$vec/rfc8452.txt" > "$out/rfc.txt" || { say "GCMSIV FAIL (rfc8452 conversion)"; exit 1; } |
| 175 | np=$(grep -c 'Plaintext (' "$vec/rfc8452.txt" || true) |
| 176 | ng=$(grep -c '^G ' "$out/rfc.txt") |
| 177 | [ "$ng" -eq 50 ] && [ "$ng" -eq "$np" ] || { say "GCMSIV FAIL (converted $ng records, RFC has $np plaintext fields)"; exit 1; } |
| 178 | c1=$(awk '$1 == "G" && $2 < 1634' "$out/rfc.txt" | wc -l | tr -d ' ') |
| 179 | run_ok rfc8452-appendix-c "$out/rfc.txt" $((8 * ng)) |
| 180 | |
| 181 | # Section 8 worked example (hand-copied from the RFC; the converter does not parse section 8) |
| 182 | run_ok rfc8452-section8 "$vec/section8.txt" 8 |
| 183 | |
| 184 | # parameter refusals and boundary lengths |
| 185 | { |
| 186 | say "# P KNLEN AADLEN BUFLEN WANT" |
| 187 | say "P 0 0 16 2" |
| 188 | say "P 16 0 16 2" |
| 189 | say "P 27 0 16 2" |
| 190 | say "P 29 0 16 2" |
| 191 | say "P 32 0 16 2" |
| 192 | say "P 36 0 16 2" |
| 193 | say "P 43 0 16 2" |
| 194 | say "P 45 0 16 2" |
| 195 | say "P 28 0 0 2" |
| 196 | say "P 28 0 15 2" |
| 197 | say "P 44 5 15 2" |
| 198 | say "P 28 0 16 0" |
| 199 | say "P 44 0 16 0" |
| 200 | say "P 28 65536 65536 0" |
| 201 | } > "$out/params.txt" |
| 202 | run_ok params "$out/params.txt" 14 |
| 203 | |
| 204 | # mutants, one per stage, each run on every Appendix C vector |
| 205 | # POLYVAL: mulX_GHASH without the reduction (the POLYVAL key is H * x unreduced) |
| 206 | mutant polyval polyval 's/^h = xor shr h 1 and m AGR$/h = shr h 1/' |
| 207 | # key derivation: the LE32 block counter placed in byte 1 instead of byte 0 |
| 208 | mutant derive derive 's/^put s add 150 mul b 2 or shl add c b 56 shr h 32$/put s add 150 mul b 2 or shl add c b 48 shr h 32/' |
| 209 | # tag input: bit 7 of byte 15 of S ^ nonce not cleared |
| 210 | mutant mask mask 's/^put k 126 and get k 126 GSMSK$/put k 126 get k 126/' |
| 211 | # counter block: bit 7 of byte 15 of the tag not set |
| 212 | mutant ctrbit counter 's/^put k 128 or tl 0x80$/put k 128 tl/' |
| 213 | # tag check bypassed (open accepts any tag) |
| 214 | mutant tagcheck refuse 's/^d = or xor get k 125 th xor get k 126 tl$/d = ulong 0/' |
| 215 | # expectation mutant: the last tag hex digit of the first C.1 record changed -> exactly 1 failure |
| 216 | awk '$1 == "G" && !done { c = substr($11, 32, 1); $11 = substr($11, 1, 31) (c == "0" ? "1" : "0"); done = 1 } { print }' \ |
| 217 | "$out/rfc.txt" > "$out/mut-expect.txt" |
| 218 | if cmp -s "$out/rfc.txt" "$out/mut-expect.txt"; then |
| 219 | mut=$((mut + 1)) |
| 220 | fail "expectation mutant: awk changed nothing" |
| 221 | else |
| 222 | run_detect mutant-expect "$out/mut-expect.txt" "$out/driver" tag 1 |
| 223 | fi |
| 224 | |
| 225 | c2=$(awk '$1 == "G" && $2 >= 1634 && $2 < 2247' "$out/rfc.txt" | wc -l | tr -d ' ') |
| 226 | c3=$(awk '$1 == "G" && $2 >= 2247' "$out/rfc.txt" | wc -l | tr -d ' ') |
| 227 | say "NOT_RUN wycheproof aes_gcm_siv_test.json: not vendored" |
| 228 | |
| 229 | if [ "$bad" -ne 0 ] || [ "$mutok" -ne "$mut" ]; then |
| 230 | say "GCMSIV FAIL vectors=$((ng + 1)) mutants=$mutok/$mut" |
| 231 | exit 1 |
| 232 | fi |
| 233 | say "GCMSIV PASS vectors=$((ng + 1)) (rfc8452 c1=$c1 c2=$c2 c3=$c3 s8=1) checks=$total mutants=$mutok/$mut" |