interplane.Get Omega
Read in your language

omnet

aien-dev/omnet

Brought by drakestapleton

branch main Branches Tags History

omnet / om / argon2 / test / run.sh

148 lines · 5660 bytes Raw Blame History
1#!/bin/sh
2# SPDX-License-Identifier: Apache-2.0
3# Argon2i test suite. Usage: sh om/argon2/test/run.sh OMC
4# Builds om/blake2b/blake2b.om + argon2.om + argon2pool.om + the driver with the pinned omc into
5# build/test/argon2/, runs the RFC 9106 5.2 vector, the committed Go differential corpus (parameter
6# sets and Tailnet Lock disablement values), the out-of-range parameter sets and the contract traps;
7# then proves the harness discriminates with an expected-digit mutant and eight source mutants.
8# Exit 0 only when every vector matches and every mutant is detected.
9# ARGON2_ORACLE=PATH (a built test/gen/oracle) adds a fresh random Go differential corpus of
10# ARGON2_N cases (default 200) with seed ARGON2_SEED (default 9106); without it that step is NOT_RUN.
11set -eu
12omc=${1:?usage: run.sh OMC}
13here=$(cd "$(dirname "$0")" && pwd)
14unit=$(dirname "$here")
15root=$(cd "$unit/../.." && pwd)
16b2b="$root/om/blake2b/blake2b.om"
17out="$root/build/test/argon2"
18vec="$here/vectors"
19mkdir -p "$out"
20fail=0
21
22build() { # build ARGON2_LIB POOL MAIN OUTPUT
23 cat "$b2b" "$1" "$2" "$3" | "$omc" > "$4" 2> "$4.err" || {
24 echo "argon2: omc failed on $1 $2: $(cat "$4.err")"
25 return 1
26 }
27 chmod +x "$4"
28}
29
30# split VECTORFILE NAME: driver input lines (without "case ", without the last field) and the
31# expected last field, skipping comments
32split() {
33 awk '/^#/ { next } { e = $NF; $NF = ""; sub(/ $/, ""); sub(/^case /, ""); print > in_; print e > out_ }' \
34 in_="$out/$2.in" out_="$out/$2.out" "$1"
35}
36
37# check NAME: run the driver on NAME.in, compare with NAME.out line for line
38check() {
39 n=$(wc -l < "$out/$1.out" | tr -d ' ')
40 if "$drv" < "$out/$1.in" > "$out/$1.got" && cmp -s "$out/$1.got" "$out/$1.out"; then
41 echo "argon2: $1 $n/$n PASS"
42 else
43 echo "argon2: $1 FAIL ($(cmp "$out/$1.got" "$out/$1.out" 2>&1 || true))"
44 fail=1
45 fi
46}
47
48drv="$out/driver"
49build "$unit/argon2.om" "$unit/argon2pool.om" "$here/driver.om" "$drv"
50echo "argon2: omc sha256 $(sha256sum "$omc" | cut -d' ' -f1)"
51echo "argon2: driver sha256 $(sha256sum "$drv" | cut -d' ' -f1)"
52
53split "$vec/rfc9106.txt" rfc9106
54check rfc9106
55split "$vec/go-diff.txt" go-diff
56check go-diff
57echo "argon2: go-diff has $(grep -c '^case ' "$vec/go-diff.txt") parameter sets and $(grep -c '^tka ' "$vec/go-diff.txt") tka secrets"
58split "$vec/reject.txt" reject
59check reject
60
61# fresh random corpus against the Go referee, when one is supplied
62if [ -n "${ARGON2_ORACLE:-}" ]; then
63 seed=${ARGON2_SEED:-9106}
64 python3 "$here/gen/vectors.py" "$ARGON2_ORACLE" random "$seed" "${ARGON2_N:-200}" > "$out/random.txt"
65 split "$out/random.txt" random
66 echo "argon2: random seed $seed"
67 check random
68else
69 echo "argon2: NOTE ARGON2_ORACLE not set, random Go corpus NOT_RUN"
70fi
71
72# contract traps: each must exit 103 (trap 3)
73build "$unit/argon2.om" "$unit/argon2pool.om" "$here/trap.om" "$out/trap"
74for c in 1 2 3; do
75 if "$out/trap" "$c"; then rc=0; else rc=$?; fi
76 if [ "$rc" -eq 103 ]; then
77 echo "argon2: trap $c exit 103 PASS"
78 else
79 echo "argon2: trap $c exit $rc FAIL"
80 fail=1
81 fi
82done
83
84# mutant 1: one flipped expected digit must be reported as a mismatch
85awk 'NR == 1 { c = substr($0, 1, 1); $0 = ((c == "0") ? "1" : "0") substr($0, 2) } { print }' \
86 "$out/go-diff.out" > "$out/mut-expect.out"
87
88# runcmp BIN IN EXPECT TAG: run BIN < IN into TAG.got, without a pipeline so the exit status is kept.
89# Sets res to "same" or "differs" when BIN exited 0 with exactly as many lines as EXPECT, else to
90# "abnormal (...)": a trap (exit 100+N), a signal, a launch failure or a short output never counts as
91# a detected mismatch.
92runcmp() {
93 if "$1" < "$2" > "$4.got" 2> "$4.err"; then rc=0; else rc=$?; fi
94 if [ "$rc" -ne 0 ]; then
95 res="abnormal (exit $rc)"
96 elif [ "$(wc -l < "$4.got")" -ne "$(wc -l < "$3")" ]; then
97 res="abnormal (output has $(wc -l < "$4.got" | tr -d ' ') of $(wc -l < "$3" | tr -d ' ') lines)"
98 elif cmp -s "$4.got" "$3"; then
99 res=same
100 else
101 res=differs
102 fi
103}
104
105runcmp "$drv" "$out/go-diff.in" "$out/mut-expect.out" "$out/mut-expect"
106if [ "$res" = differs ]; then
107 echo "argon2: mutant expected-digit detected PASS"
108else
109 echo "argon2: mutant expected-digit $res FAIL"
110 fail=1
111fi
112
113# source mutants: each must build, run normally on the RFC vector and the Go corpus, and disagree
114# with at least one expected tag
115cat "$out/rfc9106.in" "$out/go-diff.in" > "$out/all.in"
116cat "$out/rfc9106.out" "$out/go-diff.out" > "$out/all.out"
117mutant() { # mutant NAME FILE FROM TO
118 m="$out/mut-$1"
119 awk -v from="$3" -v to="$4" '$0 == from { $0 = to; hit++ } { print } END { exit hit != 1 }' \
120 "$unit/$2" > "$m.om" || { echo "argon2: mutant $1 did not apply FAIL"; fail=1; return 0; }
121 if [ "$2" = argon2.om ]; then
122 build "$m.om" "$unit/argon2pool.om" "$here/driver.om" "$m" || { fail=1; return 0; }
123 else
124 build "$unit/argon2.om" "$m.om" "$here/driver.om" "$m" || { fail=1; return 0; }
125 fi
126 runcmp "$m" "$out/all.in" "$out/all.out" "$m"
127 case "$res" in
128 differs) echo "argon2: mutant $1 detected PASS" ;;
129 same)
130 echo "argon2: mutant $1 NOT detected FAIL"
131 fail=1
132 ;;
133 *)
134 echo "argon2: mutant $1 ran $res FAIL"
135 fail=1
136 ;;
137 esac
138}
139mutant version argon2.om "a2le32 b 16 19" "a2le32 b 16 16"
140mutant type argon2.om "a2le32 b 20 1" "a2le32 b 20 2"
141mutant blamka argon2.om "ret add add x y shl mul and x 4294967295 and y 4294967295 1" "ret add x y"
142mutant column argon2.om "ret add add mul i 2 mul shr k 1 16 and k 1" "ret add mul i 16 k"
143mutant area argon2.om "area = sub area 1" "area = sub area 0"
144mutant counter argon2.om "put z 6 add get z 6 1" "put z 6 get z 6"
145mutant xor13 argon2pool.om "xr = 1" "xr = 0"
146mutant addr128 argon2pool.om "if or eq i i0 eq rem i 128 0" "if eq i i0"
147
148exit "$fail"