interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / om / tls / test / drbg / run.sh

132 lines · 5674 bytes Raw Blame History
1#!/bin/sh
2# SPDX-License-Identifier: Apache-2.0
3# Test suite for om/tls/drbg.om (HMAC_DRBG, SP 800-90A Rev.1, HMAC-SHA-256). Usage (from the
4# omega-mv root):
5# sh om/tls/test/drbg/run.sh /tmp/omtls/seed/out/omc
6# Tested against the ACVP/CAVP HMAC_DRBG test methods; not NIST-validated. Builds into
7# build/tls/drbg/. Exits 0 only when every vector and API check passes and every mutant and negative
8# control is detected. Final line: DRBG PASS vectors=N (acvp=A, cavp=C) api=K mutants=M/M, or DRBG FAIL.
9# vectors/*.txt are written by convert.py from the pinned ACVP-Server and CAVP files (see their
10# headers); the driver also flips one expected byte of every D record and requires a mismatch.
11set -eu
12omc=${1:?usage: run.sh OMC}
13here=$(cd "$(dirname "$0")" && pwd)
14lib=$(cd "$here/../.." && pwd)
15root=$(cd "$lib/../.." && pwd)
16out="$root/build/tls/drbg"
17vec="$here/vectors"
18mkdir -p "$out"
19nice="nice -n 19 ionice -c3"
20vectors=0
21acvp=0
22cavp=0
23api=0
24mut=0
25mutok=0
26bad=0
27
28say() { printf '%s\n' "$*"; }
29fail() { say "FAIL $*"; bad=1; }
30
31# build DRBG BIN: om2 line, sha2, hkdf, the DRBG unit, the driver; compile with omc
32build() {
33 { printf 'om2\n'; cat "$lib/sha2.om" "$lib/hkdf.om" "$1" "$here/driver.om"; } > "$2.om"
34 if ! $nice "$omc" < "$2.om" > "$2" 2> "$2.err" || [ -s "$2.err" ]; then
35 say "omc refused $2.om: $(cat "$2.err")"
36 return 1
37 fi
38 chmod +x "$2"
39}
40
41# run_ok NAME FILE: exit 0 and "drbg driver: N checks, 0 failed", N > 0; sets n
42run_ok() {
43 set +e
44 $nice "$out/driver" < "$2" > "$out/$1.out" 2>&1
45 rc=$?
46 set -e
47 line=$(tail -n 1 "$out/$1.out")
48 n=$(printf '%s\n' "$line" | awk '$1 == "drbg" && $2 == "driver:" && $4 == "checks," && $5 == "0" && $6 == "failed" { print $3 }')
49 if [ "$rc" -eq 0 ] && [ -n "$n" ] && [ "$n" -gt 0 ]; then
50 say "PASS $1: $line"
51 return 0
52 fi
53 n=0
54 fail "$1: exit $rc: $(tail -n 3 "$out/$1.out" | tr '\n' ' ')"
55 return 1
56}
57
58# detected NAME FILE BIN [PATTERN]: exit exactly 1 with a complete summary line with F > 0
59# failures (a trap exits 100+N, a signal 128+N); with PATTERN, the output must also contain it
60detected() {
61 set +e
62 $nice "$3" < "$2" > "$out/$1.out" 2>&1
63 rc=$?
64 set -e
65 last=$(tail -n 1 "$out/$1.out")
66 f=$(printf '%s\n' "$last" | awk '$1 == "drbg" && $2 == "driver:" && $4 == "checks," && $6 == "failed" { print $5 }')
67 if [ "$rc" -eq 1 ] && [ -n "$f" ] && [ "$f" -gt 0 ]; then
68 if [ $# -lt 4 ] || grep -q "$4" "$out/$1.out"; then
69 say "PASS $1: detected ($(grep -c '^MISMATCH\|^BAD\|^STUCK' "$out/$1.out") failure lines; $last)"
70 return 0
71 fi
72 fi
73 fail "$1: not detected: exit $rc: $last"
74 return 1
75}
76
77# mutant NAME FILE SED_EXPR [PATTERN]: apply SED_EXPR to drbg.om, rebuild, require detection
78mutant() {
79 mut=$((mut + 1))
80 sed "$3" "$lib/drbg.om" > "$out/mut-$1.lib"
81 cmp -s "$lib/drbg.om" "$out/mut-$1.lib" && { fail "$1: sed changed nothing"; return; }
82 build "$out/mut-$1.lib" "$out/mut-$1" || { fail "$1: mutant did not compile"; return; }
83 if detected "mutant-$1" "$2" "$out/mut-$1" ${4:+"$4"}; then
84 mutok=$((mutok + 1))
85 fi
86}
87
88build "$lib/drbg.om" "$out/driver" || { say "DRBG FAIL build"; exit 1; }
89say "omc sha256 $(sha256sum "$omc" | cut -d' ' -f1)"
90for u in sha2 hkdf drbg; do say "$u.om sha256 $(sha256sum "$lib/$u.om" | cut -d' ' -f1)"; done
91for f in acvp cavp; do say "$f.txt sha256 $(sha256sum "$vec/$f.txt" | cut -d' ' -f1)"; done
92
93if run_ok acvp "$vec/acvp.txt"; then acvp=$(grep -c '^D ' "$vec/acvp.txt"); fi
94if run_ok cavp "$vec/cavp.txt"; then cavp=$(grep -c '^D ' "$vec/cavp.txt"); fi
95vectors=$((acvp + cavp))
96printf 'S\n' > "$out/api.txt"
97if run_ok api "$out/api.txt"; then api=$n; fi
98
99# source mutants: each must be detected (exit 1 with failures, never a trap)
100mutant upd-one-round "$vec/acvp.txt" 's/^if eq len pd 0$/if eq len pd len pd/'
101mutant upd-sep-byte "$vec/cavp.txt" 's/^put b 0 uchar r$/put b 0 uchar add r 2/'
102mutant inst-v-init "$vec/cavp.txt" 's/^put st add 32 i 1$/put st add 32 i 0/'
103mutant gen-no-final-update "$vec/cavp.txt" '/^e = drbg_fill st dst$/,/^ret 0$/ s/^drbg_upd st ad$/n = ctr/'
104mutant gen-no-ad-update "$vec/acvp.txt" 's/^if gt len ad 0$/if gt len ad 65536/'
105mutant ctr-frozen "$out/api.txt" 's/^drbg_wr st DRCTR add ctr 1$/drbg_wr st DRCTR ctr/'
106mutant limit-off "$out/api.txt" 's/^if gt ctr lim$/if gt ctr DRRSMX/'
107mutant short-seed "$out/api.txt" 's/^if lt len sm DRMINSM$/if lt len sm 1/'
108mutant big-request "$out/api.txt" 's/^if gt len dst DRMAXRQ$/if gt len dst 65537/'
109mutant wipe-partial "$out/api.txt" 's/^drbg_zb st$/drbg_zb part st 0 64/'
110# stuck output: V is not advanced between blocks; the continuous test must latch (STUCK lines)
111mutant stuck-v "$vec/acvp.txt" '/^fn drbg_fill /,/^end$/ s/^drbg_next st x$/e = or e 0/' '^STUCK line'
112
113# expectation mutant: one ReturnedBits hex digit changed -> exactly that record fails
114mut=$((mut + 1))
115awk '/^D / && !done { c = substr($NF, 1, 1); $NF = (c == "0" ? "1" : "0") substr($NF, 2); done = 1 } { print }' \
116 "$vec/cavp.txt" > "$out/mut-expect.txt"
117if cmp -s "$vec/cavp.txt" "$out/mut-expect.txt"; then
118 fail "expectation mutant changed nothing"
119elif detected mutant-expect "$out/mut-expect.txt" "$out/driver" '^MISMATCH line'; then
120 mutok=$((mutok + 1))
121fi
122
123# negative control: a malformed record (odd hex digit count in the nonce) is refused
124grep -m 1 '^D ' "$vec/cavp.txt" | awk '{ $4 = substr($4, 2); print }' > "$out/neg-malformed.txt"
125mut=$((mut + 1))
126if detected neg-malformed "$out/neg-malformed.txt" "$out/driver" '^BAD line'; then mutok=$((mutok + 1)); fi
127
128if [ "$bad" -ne 0 ]; then
129 say "DRBG FAIL vectors=$vectors (acvp=$acvp, cavp=$cavp) api=$api mutants=$mutok/$mut"
130 exit 1
131fi
132say "DRBG PASS vectors=$vectors (acvp=$acvp, cavp=$cavp) api=$api mutants=$mutok/$mut"