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 / chacha / run.sh

207 lines · 9803 bytes Raw Blame History
1#!/bin/sh
2# SPDX-License-Identifier: Apache-2.0
3# Test suite for om/tls/chacha.om (ChaCha20, Poly1305, AEAD_CHACHA20_POLY1305, RFC 8439).
4# Usage: sh om/tls/test/chacha/run.sh OMC (OMC = the pinned seed compiler, /tmp/omtls/seed/out/omc)
5# Exits 0 only when every vector passes, the tamper controls are refused, every mutant is detected
6# and the 1 MiB round trip holds. Final line: CHACHA PASS vectors=N (wycheproof=W) mutants=M/M.
7set -eu
8omc=${1:?usage: run.sh OMC}
9here=$(cd "$(dirname "$0")" && pwd)
10lib=$(cd "$here/../.." && pwd)/chacha.om
11root=$(cd "$here/../../../.." && pwd)
12out="$root/build/tls/chacha"
13vec="$here/vectors"
14wyc=${WYCHEPROOF:-/tmp/wycheproof}
15wyc_commit=d17cc89fa5c8f4f4d378b5385ebf75da5d0926c9
16run() { nice -n 19 ionice -c3 "$@"; }
17mkdir -p "$out"
18total=0
19wtotal=0
20mut=0
21mutok=0
22bad=0
23
24say() { printf '%s\n' "$*"; }
25fail() { say "FAIL $*"; bad=1; }
26
27# build LIB PROGRAM BIN: `om2`, the library, the program; accepted only with rc 0 and empty stderr
28build() {
29 { printf 'om2\n'; cat "$1" "$2"; } > "$3.om"
30 if run "$omc" < "$3.om" > "$3" 2> "$3.err" && [ ! -s "$3.err" ]; then
31 chmod +x "$3"
32 return 0
33 fi
34 say "omc refused $3.om: $(cat "$3.err")"
35 return 1
36}
37
38# run_ok NAME FILE: exit 0 and a complete "chacha driver: N checks, 0 failed" line; adds N to total
39run_ok() {
40 set +e
41 run "$out/driver" < "$2" > "$out/$1.out" 2>&1
42 rc=$?
43 set -e
44 line=$(tail -n 1 "$out/$1.out")
45 n=$(printf '%s\n' "$line" | awk '$1 == "chacha" && $2 == "driver:" && $4 == "checks," && $5 == "0" && $6 == "failed" { print $3 }')
46 if [ "$rc" -eq 0 ] && [ -n "$n" ] && [ "$n" -gt 0 ]; then
47 total=$((total + n))
48 last=$n
49 say "PASS $1: $line"
50 else
51 last=0
52 fail "$1: exit $rc: $(tail -n 3 "$out/$1.out" | tr '\n' ' ')"
53 fi
54}
55
56# run_detect NAME FILE BIN [WANT]: detected only when BIN exits exactly 1 (the driver's own "a check
57# failed"; a trap exits 100+N, a signal 128+N), prints a complete summary line with F > 0 (F = WANT
58# when given), at least one MISMATCH line and no BAD (malformed record) line.
59run_detect() {
60 mut=$((mut + 1))
61 set +e
62 run "$3" < "$2" > "$out/$1.out" 2>&1
63 rc=$?
64 set -e
65 line=$(tail -n 1 "$out/$1.out")
66 f=$(printf '%s\n' "$line" | awk '$1 == "chacha" && $2 == "driver:" && $4 == "checks," && $6 == "failed" { print $5 }')
67 nm=$(grep -c '^MISMATCH line ' "$out/$1.out" || true)
68 nb=$(grep -c '^BAD line ' "$out/$1.out" || true)
69 if [ "$rc" -eq 1 ] && [ -n "$f" ] && [ "$f" -gt 0 ] && [ "$nm" -eq "$f" ] && [ "$nb" -eq 0 ] &&
70 { [ $# -lt 4 ] || [ "$f" -eq "$4" ]; }; then
71 mutok=$((mutok + 1))
72 say "PASS $1: detected, exit 1, $nm MISMATCH ($line)"
73 else
74 fail "$1: not detected: exit $rc, MISMATCH $nm, BAD $nb, last: $line"
75 fi
76}
77
78say "omc sha256 $(sha256sum "$omc" | cut -d' ' -f1)"
79build "$lib" "$here/driver.om" "$out/driver" || { say "CHACHA FAIL (driver build)"; exit 1; }
80say "library sha256 $(sha256sum "$lib" | cut -d' ' -f1)"
81say "driver sha256 $(sha256sum "$out/driver" | cut -d' ' -f1)"
82
83# 1. RFC 8439 sections 2.1.1-2.8.2 and Appendix A.1-A.5 (gen_rfc.py over the committed RFC text)
84run_ok rfc8439 "$vec/rfc8439-vectors.txt"
85
86# 2. Wycheproof (committed conversions; regenerated and compared when the pinned clone is present)
87if [ -d "$wyc/.git" ] && [ "$(git -C "$wyc" rev-parse HEAD)" = "$wyc_commit" ] && command -v python3 > /dev/null 2>&1; then
88 python3 "$here/gen_wycheproof.py" aead "$wyc/testvectors_v1/chacha20_poly1305_test.json" "$wyc_commit" > "$out/wp-aead.txt" 2> /dev/null
89 python3 "$here/gen_wycheproof.py" counter "$wyc/testvectors_v1/chacha20_counter_test.json" "$wyc_commit" > "$out/wp-counter.txt" 2> /dev/null
90 if cmp -s "$out/wp-aead.txt" "$vec/wycheproof-chacha20-poly1305.txt" && cmp -s "$out/wp-counter.txt" "$vec/wycheproof-chacha20-counter.txt"; then
91 say "PASS wycheproof-regenerate: committed conversions equal a fresh conversion at $wyc_commit"
92 else
93 fail "wycheproof-regenerate: committed conversions differ from $wyc"
94 fi
95else
96 say "NOTE wycheproof clone at $wyc_commit not found: committed conversions used without regeneration"
97fi
98run_ok wycheproof-chacha20-poly1305 "$vec/wycheproof-chacha20-poly1305.txt"
99wtotal=$((wtotal + last))
100run_ok wycheproof-chacha20-counter "$vec/wycheproof-chacha20-counter.txt"
101wtotal=$((wtotal + last))
102say "NOTE wycheproof chacha20_poly1305_test.json: $(sed -n 3p "$vec/wycheproof-chacha20-poly1305.txt" | cut -c3-) (0 valid sealed+opened, 1 invalid tag refused, 2 invalid nonce size refused; no acceptable)"
103say "NOTE wycheproof chacha20_counter_test.json: $(sed -n 3p "$vec/wycheproof-chacha20-counter.txt" | cut -c3-) (NOT_RUN = XChaCha20 groups, not implemented)"
104
105# 3. differential corpus against python cryptography (OpenSSL), seeded
106if python3 -c 'import cryptography' > /dev/null 2>&1; then
107 seed=${CHACHA_SEED:-1}
108 python3 "$here/gen_diff.py" "$seed" 903 > "$out/diff.txt"
109 run_ok "cryptography-diff(seed $seed)" "$out/diff.txt"
110else
111 say "NOT_RUN cryptography-diff: python3 cryptography not importable"
112fi
113
114# 4. tamper controls: the RFC 8439 2.8.2 record with one ciphertext byte, then one tag byte, flipped
115# and the expected code set to 1; the driver passes only if open returns 1 and zeroes the buffer
116grep '^A 808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f070000004041424344454647 ' \
117 "$vec/rfc8439-vectors.txt" > "$out/aead282.txt"
118[ "$(wc -l < "$out/aead282.txt")" -eq 1 ] || fail "tamper: RFC 2.8.2 record not found"
119awk '{ c = substr($5, 1, 1); $5 = (c == "0" ? "1" : "0") substr($5, 2); $6 = 1; print }' "$out/aead282.txt" > "$out/tamper-ct.txt"
120awk '{ n = length($5); c = substr($5, n, 1); $5 = substr($5, 1, n - 1) (c == "0" ? "1" : "0"); $6 = 1; print }' "$out/aead282.txt" > "$out/tamper-tag.txt"
121awk '{ c = substr($3, 1, 1); $3 = (c == "0" ? "1" : "0") substr($3, 2); $6 = 1; print }' "$out/aead282.txt" > "$out/tamper-aad.txt"
122cat "$out/tamper-ct.txt" "$out/tamper-tag.txt" "$out/tamper-aad.txt" > "$out/tamper.txt"
123run_ok tamper-refused "$out/tamper.txt"
124# and the same tampered records claimed valid (code 0) must be reported as mismatches
125awk '{ $6 = 0; print }' "$out/tamper.txt" > "$out/tamper-claimed.txt"
126set +e
127run "$out/driver" < "$out/tamper-claimed.txt" > "$out/tamper-claimed.out" 2>&1
128rc=$?
129set -e
130if [ "$rc" -eq 1 ] && [ "$(tail -n 1 "$out/tamper-claimed.out")" = "chacha driver: 3 checks, 3 failed" ]; then
131 say "PASS tamper-claimed-valid: 3 of 3 tampered records rejected"
132else
133 fail "tamper-claimed-valid: exit $rc: $(tail -n 1 "$out/tamper-claimed.out")"
134fi
135
136cat "$vec/rfc8439-vectors.txt" "$vec/wycheproof-chacha20-poly1305.txt" > "$out/mutant-input.txt"
137
138# 5. expectation mutant: one hex digit of the RFC 2.5.2 Poly1305 tag changed -> exactly 1 failure
139awk '/^P 85d6be78/ && !d { n = length($4); c = substr($4, n, 1); $4 = substr($4, 1, n - 1) (c == "0" ? "1" : "0"); d = 1 } { print }' \
140 "$vec/rfc8439-vectors.txt" > "$out/mut-expect.txt"
141if cmp -s "$vec/rfc8439-vectors.txt" "$out/mut-expect.txt"; then
142 mut=$((mut + 1))
143 fail "mutant-expectation: mutation did not change the vectors"
144else
145 run_detect mutant-expectation "$out/mut-expect.txt" "$out/driver" 1
146fi
147
148# 6. library mutants (each a real constant/operator, one line, checked to change the source)
149# lib_mutant NAME AWK-PROGRAM
150lib_mutant() {
151 awk "$2" "$lib" > "$out/mut-$1.lib"
152 if cmp -s "$lib" "$out/mut-$1.lib"; then
153 mut=$((mut + 1))
154 fail "mutant-$1: mutation did not change the library"
155 elif build "$out/mut-$1.lib" "$here/driver.om" "$out/mut-$1"; then
156 run_detect "mutant-$1" "$out/mutant-input.txt" "$out/mut-$1"
157 else
158 mut=$((mut + 1))
159 fail "mutant-$1: build failed"
160 fi
161}
162# ChaCha20 quarter round: first rotation 16 -> 15
163lib_mutant qr-rotation '$0 == "d = rotl xor d a 16" { $0 = "d = rotl xor d a 15" } { print }'
164# Poly1305 clamp: r[15] mask 15 -> 31 (one more bit of r kept)
165lib_mutant poly-clamp '$0 == "put c 15 and get c 15 15" { $0 = "put c 15 and get c 15 31" } { print }'
166# Poly1305 2^130 - 5 fold in the block multiply: carry * 5 -> carry * 4 (first occurrence only)
167lib_mutant poly-fold '$0 == "d = add get p 9 mul c 5" && !d { $0 = "d = add get p 9 mul c 4"; d = 1 } { print }'
168# Poly1305 final reduction: the h / h - p select mask inverted
169lib_mutant poly-final '$0 == "m = sub shr d 63 1" { $0 = "m = sub 0 shr d 63" } { print }'
170
171# 7. 1 MiB seal/open round trip (bench.om), output compared with python cryptography, timed
172if build "$lib" "$here/bench.om" "$out/bench"; then
173 t0=$(date +%s%N)
174 set +e
175 run "$out/bench" > "$out/bench.bin"
176 rc=$?
177 set -e
178 t1=$(date +%s%N)
179 got=$(sha256sum "$out/bench.bin" | cut -d' ' -f1)
180 if [ "$rc" -ne 0 ] || [ "$(wc -c < "$out/bench.bin")" -ne 1048592 ]; then
181 fail "bench-1MiB: exit $rc, $(wc -c < "$out/bench.bin") bytes"
182 elif python3 -c 'import cryptography' > /dev/null 2>&1; then
183 want=$(python3 -c '
184import hashlib
185from cryptography.hazmat.primitives.ciphers.aead import ChaCha20Poly1305
186kn = bytes(range(44)); ad = bytes(range(160, 172))
187pt = bytes((31 * i + 7) & 255 for i in range(1 << 20))
188print(hashlib.sha256(ChaCha20Poly1305(kn[:32]).encrypt(kn[32:], pt, ad)).hexdigest())')
189 if [ "$got" = "$want" ]; then
190 say "PASS bench-1MiB: 16 open+reseal rounds restored the plaintext; sealed sha256 $got equals cryptography"
191 else
192 fail "bench-1MiB: sealed sha256 $got, cryptography $want"
193 fi
194 else
195 say "PASS bench-1MiB: 16 open+reseal rounds restored the plaintext (NOT_RUN oracle: no python cryptography)"
196 fi
197 ns=$((t1 - t0))
198 say "THROUGHPUT bench-1MiB: 33 MiB through seal/open in $ns ns = $(awk -v n="$ns" 'BEGIN { printf "%.1f", 33 * 1048576 / (n / 1e9) / 1e6 }') MB/s (whole process, incl. start-up and pattern checks)"
199else
200 fail "bench-1MiB: build failed"
201fi
202
203if [ "$bad" -ne 0 ]; then
204 say "CHACHA FAIL vectors=$total (wycheproof=$wtotal) mutants=$mutok/$mut"
205 exit 1
206fi
207say "CHACHA PASS vectors=$total (wycheproof=$wtotal) mutants=$mutok/$mut"