omnet
aien-dev/omnet
Brought by drakestapleton
omnet / om / blake2s / test / run.sh
| 1 | #!/bin/sh |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # BLAKE2s test suite. Usage: sh om/blake2s/test/run.sh OMC |
| 4 | # Builds lib + driver with the pinned omc into build/test/blake2s/, runs the RFC 7693 Appendix B and |
| 5 | # Appendix E vectors, the BLAKE2 KAT file, the boundary vectors and (when python3 is present) a |
| 6 | # hashlib differential corpus; then proves the harness discriminates with three mutants. |
| 7 | # Exit 0 only when every vector matches and every mutant is detected. |
| 8 | # B2S_SEED sets the differential seed (default 7693). |
| 9 | set -eu |
| 10 | omc=${1:?usage: run.sh OMC} |
| 11 | here=$(cd "$(dirname "$0")" && pwd) |
| 12 | unit=$(dirname "$here") |
| 13 | root=$(cd "$unit/../.." && pwd) |
| 14 | out="$root/build/test/blake2s" |
| 15 | vec="$here/vectors" |
| 16 | seed=${B2S_SEED:-7693} |
| 17 | mkdir -p "$out" |
| 18 | fail=0 |
| 19 | |
| 20 | build() { # build LIBRARY OUTPUT |
| 21 | cat "$1" "$here/driver.om" | "$omc" > "$2" 2> "$2.err" || { |
| 22 | echo "blake2s: omc failed on $1: $(cat "$2.err")" |
| 23 | return 1 |
| 24 | } |
| 25 | chmod +x "$2" |
| 26 | } |
| 27 | |
| 28 | # check NAME INPUT EXPECTED: run the driver on INPUT, compare with EXPECTED byte for byte |
| 29 | check() { |
| 30 | n=$(wc -l < "$3" | tr -d ' ') |
| 31 | if "$drv" < "$2" > "$out/$1.got" && cmp -s "$out/$1.got" "$3"; then |
| 32 | echo "blake2s: $1 $n/$n PASS" |
| 33 | else |
| 34 | echo "blake2s: $1 FAIL ($(cmp "$out/$1.got" "$3" 2>&1 || true))" |
| 35 | fail=1 |
| 36 | fi |
| 37 | } |
| 38 | |
| 39 | drv="$out/driver" |
| 40 | build "$unit/blake2s.om" "$drv" |
| 41 | echo "blake2s: omc sha256 $(sha256sum "$omc" | cut -d' ' -f1)" |
| 42 | echo "blake2s: driver sha256 $(sha256sum "$drv" | cut -d' ' -f1)" |
| 43 | |
| 44 | # RFC 7693 Appendix B case lines and Appendix E grand hash |
| 45 | awk '$1 == "case" { print $2, $3, $4 > in_; print $5 > out_ }' \ |
| 46 | in_="$out/rfc.in" out_="$out/rfc.out" "$vec/rfc7693.txt" |
| 47 | check rfc7693-appB "$out/rfc.in" "$out/rfc.out" |
| 48 | want=$(awk '$1 == "selftest" { print $2 }' "$vec/rfc7693.txt") |
| 49 | if got=$("$drv" selftest) && [ "$got" = "$want" ]; then |
| 50 | echo "blake2s: rfc7693-appE 1/1 PASS ($got)" |
| 51 | else |
| 52 | echo "blake2s: rfc7693-appE FAIL (got ${got:-none})" |
| 53 | fail=1 |
| 54 | fi |
| 55 | |
| 56 | # BLAKE2 KAT: blocks of in:/key:/hash: lines, tab separated, empty in: means the empty message |
| 57 | awk -F'\t' ' |
| 58 | $1 == "in:" { m = ($2 == "") ? "-" : $2 } |
| 59 | $1 == "key:" { k = ($2 == "") ? "-" : $2 } |
| 60 | $1 == "hash:" { print length($2) / 2, k, m > in_; print $2 > out_ } |
| 61 | ' in_="$out/kat.in" out_="$out/kat.out" "$vec/blake2s-kat.txt" |
| 62 | [ "$(wc -l < "$out/kat.out")" -eq 256 ] || { echo "blake2s: kat parse FAIL"; fail=1; } |
| 63 | check kat "$out/kat.in" "$out/kat.out" |
| 64 | |
| 65 | # boundary vectors: OUTN KN N DIGEST, key byte i = i, message byte i = i mod 251 |
| 66 | awk ' |
| 67 | function hex(n, mod, s, i) { |
| 68 | if (n == 0) return "-" |
| 69 | s = "" |
| 70 | for (i = 0; i < n; i++) s = s sprintf("%02x", i % mod) |
| 71 | return s |
| 72 | } |
| 73 | { print $1, hex($2, 256), hex($3, 251) > in_; print $4 > out_ } |
| 74 | ' in_="$out/boundary.in" out_="$out/boundary.out" "$vec/boundary.txt" |
| 75 | check boundary "$out/boundary.in" "$out/boundary.out" |
| 76 | |
| 77 | # differential corpus against python3 hashlib.blake2s |
| 78 | if command -v python3 > /dev/null 2>&1; then |
| 79 | python3 "$here/oracle.py" diff "$seed" "$out" > /dev/null |
| 80 | echo "blake2s: differential seed $seed" |
| 81 | check differential "$out/diff.in" "$out/diff.out" |
| 82 | else |
| 83 | echo "blake2s: NOTE python3 not found, differential corpus NOT_RUN" |
| 84 | fi |
| 85 | |
| 86 | # Mutant rules: a mutant counts as detected only when every run of the binary under test completed |
| 87 | # normally (exit 0; for "selftest" also exit 1, its documented mismatch status; never a trap exit |
| 88 | # 100+N, a signal or a launch failure) with complete output (one line per case, each of the |
| 89 | # expected length), and at least one result differs from the expected one. Output goes to files, |
| 90 | # never through a pipe, so no exit status is lost. |
| 91 | |
| 92 | # complete EXPECTED GOT: GOT has as many newline-ended lines as EXPECTED, each of the same length |
| 93 | complete() { |
| 94 | [ "$(wc -l < "$1")" -eq "$(wc -l < "$2")" ] || return 1 |
| 95 | [ -z "$(tail -c 1 "$2")" ] || return 1 |
| 96 | awk 'NR == FNR { l[FNR] = length($0); next } length($0) != l[FNR] { bad = 1 } END { exit bad }' \ |
| 97 | "$1" "$2" |
| 98 | } |
| 99 | |
| 100 | # mutant 1: one flipped expected digit must be reported as a mismatch |
| 101 | awk 'NR == 1 { c = substr($0, 1, 1); $0 = ((c == "0") ? "1" : "0") substr($0, 2) } { print }' \ |
| 102 | "$out/kat.out" > "$out/mut-expect.out" |
| 103 | rc=0 |
| 104 | "$drv" < "$out/kat.in" > "$out/mut-expect.got" || rc=$? |
| 105 | if [ "$rc" -ne 0 ] || ! complete "$out/mut-expect.out" "$out/mut-expect.got"; then |
| 106 | echo "blake2s: mutant expected-digit run did not complete (exit $rc) FAIL" |
| 107 | fail=1 |
| 108 | elif cmp -s "$out/mut-expect.got" "$out/mut-expect.out"; then |
| 109 | echo "blake2s: mutant expected-digit NOT detected FAIL" |
| 110 | fail=1 |
| 111 | else |
| 112 | echo "blake2s: mutant expected-digit detected PASS" |
| 113 | fi |
| 114 | |
| 115 | # source mutants: each must build, run to completion on the RFC case lines and the Appendix E |
| 116 | # self-test, and disagree with at least one of them |
| 117 | echo "$want" > "$out/self.out" |
| 118 | mutant() { # mutant NAME FROM TO |
| 119 | m="$out/mut-$1" |
| 120 | awk -v from="$2" -v to="$3" '$0 == from { $0 = to; hit++ } { print } END { exit hit != 1 }' \ |
| 121 | "$unit/blake2s.om" > "$m.om" || { echo "blake2s: mutant $1 did not apply FAIL"; fail=1; return 0; } |
| 122 | build "$m.om" "$m" || { fail=1; return 0; } |
| 123 | rc1=0 |
| 124 | "$m" < "$out/rfc.in" > "$m.rfc" || rc1=$? |
| 125 | rc2=0 |
| 126 | "$m" selftest > "$m.self" || rc2=$? |
| 127 | if [ "$rc1" -ne 0 ] || ! complete "$out/rfc.out" "$m.rfc" || |
| 128 | { [ "$rc2" -ne 0 ] && [ "$rc2" -ne 1 ]; } || ! complete "$out/self.out" "$m.self"; then |
| 129 | echo "blake2s: mutant $1 did not complete (exit $rc1, selftest exit $rc2) FAIL" |
| 130 | fail=1 |
| 131 | elif cmp -s "$m.rfc" "$out/rfc.out" && [ "$rc2" -eq 0 ] && cmp -s "$m.self" "$out/self.out"; then |
| 132 | echo "blake2s: mutant $1 NOT detected FAIL" |
| 133 | fail=1 |
| 134 | else |
| 135 | echo "blake2s: mutant $1 detected PASS" |
| 136 | fi |
| 137 | } |
| 138 | mutant iv0 "put w 40 1779033703" "put w 40 1779033702" |
| 139 | mutant final-flag "b2scomp w 1" "b2scomp w 0" |
| 140 | |
| 141 | exit "$fail" |