interplane.Get Omega
Read in your language

omnet

aien-dev/omnet

Brought by drakestapleton

branch main Branches Tags History

omnet / om / sha512 / test / run.sh

146 lines · 5105 bytes Raw Blame History
1#!/bin/sh
2# SPDX-License-Identifier: Apache-2.0
3# Test suite for om/sha512. Usage: sh om/sha512/test/run.sh OMC
4# OMC is the pinned seed-born compiler (tools/omc.sh prints its path).
5# Exits 0 only when every vector passes and every mutant / negative check fails as expected.
6set -eu
7omc=${1:?usage: run.sh OMC}
8here=$(cd "$(dirname "$0")" && pwd)
9unit=$(dirname "$here")
10root=$(cd "$unit/../.." && pwd)
11out="$root/build/test/sha512"
12vec="$here/vectors"
13mkdir -p "$out"
14total=0
15bad=0
16
17say() { printf '%s\n' "$*"; }
18fail() { say "FAIL $*"; bad=1; }
19
20# build LIB DRIVER BIN: concatenate the library and a program, compile with omc
21build() {
22 cat "$1" "$2" > "$3.om"
23 if ! "$omc" < "$3.om" > "$3" 2> "$3.err"; then
24 say "omc refused $3.om: $(cat "$3.err")"
25 return 1
26 fi
27 chmod +x "$3"
28}
29
30# run_ok NAME FILE: the driver must exit 0 with a complete "sha512 driver: N checks, 0 failed" line
31run_ok() {
32 set +e
33 "$out/driver" < "$2" > "$out/$1.out" 2>&1
34 rc=$?
35 set -e
36 line=$(tail -n 1 "$out/$1.out")
37 n=$(printf '%s\n' "$line" | awk '$1 == "sha512" && $2 == "driver:" && $4 == "checks," && $5 == "0" && $6 == "failed" { print $3 }')
38 if [ "$rc" -eq 0 ] && [ -n "$n" ] && [ "$n" -gt 0 ]; then
39 total=$((total + n))
40 say "PASS $1: $line"
41 else
42 fail "$1: exit $rc: $(tail -n 3 "$out/$1.out" | tr '\n' ' ')"
43 fi
44}
45
46# run_fails NAME FILE BIN WANT CHECKS: a mutant or negative input counts as detected only when BIN
47# completed normally and reported a difference: exit status exactly 1 (the driver's own "some check
48# failed" return; a trap exits 100+N, a signal 128+N, a launch failure 126/127), and a complete
49# summary line "sha512 driver: CHECKS checks, WANT failed" with WANT > 0.
50run_fails() {
51 set +e
52 "$3" < "$2" > "$out/$1.out" 2>&1
53 rc=$?
54 set -e
55 last=$(tail -n 1 "$out/$1.out")
56 want="sha512 driver: $5 checks, $4 failed"
57 if [ "$rc" -eq 1 ] && [ "$4" -gt 0 ] && [ "$last" = "$want" ]; then
58 say "PASS $1: detected ($last)"
59 else
60 fail "$1: expected exit 1 and \"$want\", got exit $rc: $last"
61 fi
62}
63
64build "$unit/sha512.om" "$here/driver.om" "$out/driver" || exit 1
65say "omc sha256 $(sha256sum "$omc" | cut -d' ' -f1)"
66say "library sha256 $(sha256sum "$unit/sha512.om" | cut -d' ' -f1)"
67say "driver sha256 $(sha256sum "$out/driver" | cut -d' ' -f1)"
68
69# NIST CAVP byte-oriented .rsp files -> driver records
70awk '{ sub(/\r$/, "") }
71 /^Len = / { n = $3 / 8 }
72 /^Msg = / { m = $3 }
73 /^MD = / { if (n == 0) m = "-"; print "H", n, m, $3 }' \
74 "$vec/SHA512ShortMsg.rsp" > "$out/short.txt"
75awk '{ sub(/\r$/, "") }
76 /^Len = / { n = $3 / 8 }
77 /^Msg = / { m = $3 }
78 /^MD = / { print "H", n, m, $3 }' \
79 "$vec/SHA512LongMsg.rsp" > "$out/long.txt"
80awk '{ sub(/\r$/, "") }
81 /^Seed = / { printf "M %s", $3 }
82 /^MD = / { printf " %s", $3; k++ }
83 END { print ""; if (k != 100) exit 1 }' \
84 "$vec/SHA512Monte.rsp" > "$out/monte.txt"
85
86run_ok nist-examples "$vec/nist-examples.txt"
87run_ok cavp-short "$out/short.txt"
88run_ok cavp-long "$out/long.txt"
89run_ok cavp-monte "$out/monte.txt"
90run_ok oracle-static "$vec/oracle.txt"
91
92# differential corpus against python3 hashlib.sha512, generated now
93if command -v python3 > /dev/null 2>&1; then
94 seed=${S512_SEED:-1}
95 python3 "$here/gen_vectors.py" diff "$seed" > "$out/diff.txt"
96 python3 "$here/gen_vectors.py" pre "$seed" > "$out/pre.txt"
97 run_ok "hashlib-diff(seed $seed)" "$out/diff.txt"
98 run_ok "hashlib-pre(seed $seed)" "$out/pre.txt"
99else
100 say "NOTE python3 not found: differential corpus against hashlib skipped"
101fi
102
103# mutant 1: one expected digest hex digit changed -> exactly 1 failure
104awk 'NR == 3 { c = substr($4, 128, 1); $4 = substr($4, 1, 127) (c == "0" ? "1" : "0") } { print }' \
105 "$vec/nist-examples.txt" > "$out/mut-expect.txt"
106if cmp -s "$vec/nist-examples.txt" "$out/mut-expect.txt"; then
107 fail "mutant-expected: mutation did not change the file"
108else
109 run_fails mutant-expected "$out/mut-expect.txt" "$out/driver" 1 "$(grep -c '^H ' "$vec/nist-examples.txt")"
110fi
111
112# mutant 2: K[0] changed by one in the library -> every NIST example fails
113sed 's/^put k 0 4794697086780616226$/put k 0 4794697086780616227/' "$unit/sha512.om" > "$out/mut-k-lib.om"
114if cmp -s "$unit/sha512.om" "$out/mut-k-lib.om"; then
115 fail "mutant-constant: mutation did not change the library"
116elif build "$out/mut-k-lib.om" "$here/driver.om" "$out/mut-k"; then
117 n=$(grep -c '^H ' "$vec/nist-examples.txt")
118 run_fails mutant-constant "$vec/nist-examples.txt" "$out/mut-k" "$n" "$n"
119else
120 fail "mutant-constant: build failed"
121fi
122
123# negative: a malformed record (odd hex digit count) is refused, not hashed
124printf 'H 3 61626 %0128d\n' 0 > "$out/malformed.txt"
125run_fails malformed-record "$out/malformed.txt" "$out/driver" 1 1
126
127# negative: n one past the capacity traps 3 (exit 103)
128if build "$unit/sha512.om" "$here/trap.om" "$out/trap"; then
129 set +e
130 "$out/trap"
131 rc=$?
132 set -e
133 if [ "$rc" -eq 103 ]; then
134 say "PASS over-capacity: n = 65537 traps 3 (exit 103)"
135 else
136 fail "over-capacity: expected exit 103, got $rc"
137 fi
138else
139 fail "over-capacity: build failed"
140fi
141
142if [ "$bad" -ne 0 ]; then
143 say "sha512: FAIL"
144 exit 1
145fi
146say "sha512: PASS ($total vector checks)"