Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omnet/blob/main/om/blake2b/test/run.sh # omnet aien-dev/omnet Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omnet / om / blake2b / test / run.sh` 160 lines ยท 5840 bytes Raw Blame History 1 #!/bin/sh 2 # SPDX-License-Identifier: Apache-2.0 3 # BLAKE2b test suite. Usage: sh om/blake2b/test/run.sh OMC 4 # Builds lib + driver with the pinned omc into build/test/blake2b/, runs the RFC 7693 Appendix A 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 five mutants. 7 # Exit 0 only when every vector matches and every mutant is detected. 8 # B2B_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/blake2b" 15 vec="$here/vectors" 16 seed=${B2B_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 "blake2b: 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 "blake2b: $1 $n/$n PASS" 33 else 34 echo "blake2b: $1 FAIL ($(cmp "$out/$1.got" "$3" 2>&1 || true))" 35 fail=1 36 fi 37 } 38 39 drv="$out/driver" 40 build "$unit/blake2b.om" "$drv" 41 echo "blake2b: omc sha256 $(sha256sum "$omc" | cut -d' ' -f1)" 42 echo "blake2b: driver sha256 $(sha256sum "$drv" | cut -d' ' -f1)" 43 44 # RFC 7693 Appendix A 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-appA "$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 "blake2b: rfc7693-appE 1/1 PASS ($got)" 51 else 52 echo "blake2b: 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/blake2b-kat.txt" 62 [ "$(wc -l < "$out/kat.out")" -eq 256 ] || { echo "blake2b: 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.blake2b 78 if command -v python3 > /dev/null 2>&1; then 79 python3 "$here/oracle.py" diff "$seed" "$out" > /dev/null 80 echo "blake2b: differential seed $seed" 81 check differential "$out/diff.in" "$out/diff.out" 82 else 83 echo "blake2b: NOTE python3 not found, differential corpus NOT_RUN" 84 fi 85 86 # runcmp BIN IN EXPECT TAG: run BIN < IN into TAG.got, without a pipeline so the exit status is kept. 87 # Sets res to "same" or "differs" when BIN exited 0 with exactly as many lines as EXPECT, else to 88 # "abnormal (...)": a trap (exit 100+N), a signal, a launch failure or a short output never counts as 89 # a detected mismatch. 90 runcmp() { 91 if "$1" < "$2" > "$4.got" 2> "$4.err"; then rc=0; else rc=$?; fi 92 if [ "$rc" -ne 0 ]; then 93 res="abnormal (exit $rc)" 94 elif [ "$(wc -l < "$4.got")" -ne "$(wc -l < "$3")" ]; then 95 res="abnormal (output has $(wc -l < "$4.got" | tr -d ' ') of $(wc -l < "$3" | tr -d ' ') lines)" 96 elif cmp -s "$4.got" "$3"; then 97 res=same 98 else 99 res=differs 100 fi 101 } 102 103 # selfcmp BIN TAG: run BIN selftest. Sets res to "same" (exit 0, grand hash = want), "differs" (the 104 # documented mismatch outcome: exit 1 with a 64-digit grand hash other than want) or "abnormal (...)". 105 selfcmp() { 106 if "$1" selftest > "$2.got" 2> "$2.err"; then rc=0; else rc=$?; fi 107 g=$(cat "$2.got") 108 if [ "$rc" -eq 0 ] && [ "$g" = "$want" ]; then 109 res=same 110 elif [ "$rc" -eq 1 ] && [ "${#g}" -eq 64 ] && [ "$g" != "$want" ]; then 111 res=differs 112 else 113 res="abnormal (selftest exit $rc, output '$g')" 114 fi 115 } 116 117 # mutant 1: one flipped expected digit must be reported as a mismatch 118 awk 'NR == 1 { c = substr($0, 1, 1); $0 = ((c == "0") ? "1" : "0") substr($0, 2) } { print }' \ 119 "$out/kat.out" > "$out/mut-expect.out" 120 runcmp "$drv" "$out/kat.in" "$out/mut-expect.out" "$out/mut-expect" 121 if [ "$res" = differs ]; then 122 echo "blake2b: mutant expected-digit detected PASS" 123 else 124 echo "blake2b: mutant expected-digit $res FAIL" 125 fail=1 126 fi 127 128 # source mutants: each must build, run normally on the RFC case lines, the KAT and the self-test, and 129 # disagree with at least one expected result 130 cat "$out/rfc.in" "$out/kat.in" > "$out/mut.in" 131 cat "$out/rfc.out" "$out/kat.out" > "$out/mut.out" 132 mutant() { # mutant NAME FROM TO 133 m="$out/mut-$1" 134 awk -v from="$2" -v to="$3" '$0 == from { $0 = to; hit++ } { print } END { exit hit != 1 }' \ 135 "$unit/blake2b.om" > "$m.om" || { echo "blake2b: mutant $1 did not apply FAIL"; fail=1; return 0; } 136 build "$m.om" "$m" || { fail=1; return 0; } 137 runcmp "$m" "$out/mut.in" "$out/mut.out" "$m.cases" 138 r1=$res 139 selfcmp "$m" "$m.self" 140 r2=$res 141 case "$r1 $r2" in 142 *abnormal*) 143 echo "blake2b: mutant $1 ran abnormally: cases $r1, selftest $r2 FAIL" 144 fail=1 145 ;; 146 *differs*) 147 echo "blake2b: mutant $1 detected PASS" 148 ;; 149 *) 150 echo "blake2b: mutant $1 NOT detected FAIL" 151 fail=1 152 ;; 153 esac 154 } 155 mutant iv0 "put w 40 `7640891576956012808`" "put w 40 `7640891576956012809`" 156 mutant final-flag "b2bcomp w 1" "b2bcomp w 0" 157 mutant rot63 "put w b or shr t 63 shl t 1" "put w b or shr t 62 shl t 2" 158 mutant rounds "for r ulong 12" "for r ulong 10" 159 160 exit "$fail"