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

206 lines · 8828 bytes Raw Blame History
1#!/bin/sh
2# SPDX-License-Identifier: Apache-2.0
3# Test suite for om/tls/aesgcm.om (AES, FIPS 197; AES-GCM, NIST SP 800-38D; TLS AEAD_AES_128_GCM).
4# Usage: sh om/tls/test/aesgcm/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 and
6# the 1 MiB round trip holds. Final line: AESGCM PASS vectors=N (nist=..., wycheproof=W) mutants=M/M.
7set -eu
8omc=${1:?usage: run.sh OMC}
9here=$(cd "$(dirname "$0")" && pwd)
10lib=$(cd "$here/../.." && pwd)/aesgcm.om
11root=$(cd "$here/../../../.." && pwd)
12out="$root/build/tls/aesgcm"
13vec="$here/vectors"
14zip_sha=f9fc479e134cde2980b3bb7cddbcb567b2cd96fd753835243ed067699f26a023
15run() { nice -n 19 ionice -c3 "$@"; }
16mkdir -p "$out"
17total=0
18nist=0
19mut=0
20mutok=0
21bad=0
22
23say() { printf '%s\n' "$*"; }
24fail() { say "FAIL $*"; bad=1; }
25
26# build LIB PROGRAM BIN: `om2`, the library, the program; accepted only with rc 0 and empty stderr
27build() {
28 { printf 'om2\n'; cat "$1" "$2"; } > "$3.om"
29 if run "$omc" < "$3.om" > "$3" 2> "$3.err" && [ ! -s "$3.err" ]; then
30 chmod +x "$3"
31 return 0
32 fi
33 say "omc refused $3.om: $(cat "$3.err")"
34 return 1
35}
36
37# run_ok NAME FILE WANT: exit 0 and a complete "aesgcm driver: WANT checks, 0 failed" line, WANT
38# being the number of records the converter wrote (no record may be lost); adds WANT 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 == "aesgcm" && $2 == "driver:" && $4 == "checks," && $5 == "0" && $6 == "failed" { print $3 }')
46 if [ "$rc" -eq 0 ] && [ -n "$n" ] && [ "$n" -gt 0 ] && [ "$n" -eq "$3" ]; then
47 total=$((total + n))
48 say "PASS $1: $line"
49 else
50 fail "$1: exit $rc, want $3 checks: $(tail -n 3 "$out/$1.out" | tr '\n' ' ')"
51 fi
52}
53
54# run_detect NAME FILE BIN [WANT]: detected only when BIN exits exactly 1 (the driver's own "a check
55# failed"; a trap exits 100+N, a signal 128+N), prints a complete summary line with F > 0 (F = WANT
56# when given), one MISMATCH line per failure and no BAD (malformed record) line.
57run_detect() {
58 mut=$((mut + 1))
59 set +e
60 run "$3" < "$2" > "$out/$1.out" 2>&1
61 rc=$?
62 set -e
63 line=$(tail -n 1 "$out/$1.out")
64 f=$(printf '%s\n' "$line" | awk '$1 == "aesgcm" && $2 == "driver:" && $4 == "checks," && $6 == "failed" { print $5 }')
65 nm=$(grep -c '^MISMATCH line ' "$out/$1.out" || true)
66 nb=$(grep -c '^BAD line ' "$out/$1.out" || true)
67 if [ "$rc" -eq 1 ] && [ -n "$f" ] && [ "$f" -gt 0 ] && [ "$nm" -eq "$f" ] && [ "$nb" -eq 0 ] &&
68 { [ $# -lt 4 ] || [ "$f" -eq "$4" ]; }; then
69 mutok=$((mutok + 1))
70 say "PASS $1: detected, exit 1, $nm MISMATCH ($line)"
71 else
72 fail "$1: not detected: exit $rc, MISMATCH $nm, BAD $nb, last: $line"
73 fi
74}
75
76# mutant NAME SED: the library with one sed substitution (which must change exactly one line),
77# compiled with the driver and run on FIPS 197 + Wycheproof; must be detected
78mutant() {
79 sed "$2" "$lib" > "$out/lib-$1.om"
80 nd=$(diff "$lib" "$out/lib-$1.om" | grep -c '^>' || true)
81 if [ "$nd" -ne 1 ]; then
82 mut=$((mut + 1))
83 fail "mutant $1: sed changed $nd lines, want 1"
84 return
85 fi
86 if build "$out/lib-$1.om" "$here/driver.om" "$out/mut-$1"; then
87 run_detect "mutant-$1" "$out/small.txt" "$out/mut-$1"
88 else
89 mut=$((mut + 1))
90 fail "mutant $1: build refused"
91 fi
92}
93
94say "omc sha256 $(sha256sum "$omc" | cut -d' ' -f1)"
95build "$lib" "$here/driver.om" "$out/driver" || { say "AESGCM FAIL (driver build)"; exit 1; }
96say "library sha256 $(sha256sum "$lib" | cut -d' ' -f1)"
97say "library functions $(grep -c '^fn ' "$lib")"
98
99# FIPS 197 Appendix C.1-C.3 (+ Appendix B)
100nf=$(grep -c '^B ' "$vec/fips197.txt")
101run_ok fips197 "$vec/fips197.txt" "$nf"
102
103# Wycheproof aes_gcm_test.json, every test (converted by gen_wycheproof.py; header records the
104# commit and sha256)
105nw=$(grep -c '^W ' "$vec/wycheproof_aes_gcm.txt")
106run_ok wycheproof "$vec/wycheproof_aes_gcm.txt" "$nw"
107wv=$(grep -c '^W [0-9]* V ' "$vec/wycheproof_aes_gcm.txt")
108wi=$(grep -c '^W [0-9]* I ' "$vec/wycheproof_aes_gcm.txt")
109
110# NIST CAVP gcmtestvectors.zip, committed unchanged; converted at test time into build/
111if [ "$(sha256sum "$vec/gcmtestvectors.zip" | cut -d' ' -f1)" != "$zip_sha" ]; then
112 fail "gcmtestvectors.zip sha256 mismatch"
113else
114 for f in gcmEncryptExtIV128 gcmEncryptExtIV192 gcmEncryptExtIV256; do
115 unzip -p "$vec/gcmtestvectors.zip" "$f.rsp" | awk '{ sub(/\r$/, "") }
116 function h(s) { return s == "" ? "-" : s }
117 /^Count = / { c++ }
118 /^Key = / { k = $3 } /^IV = / { iv = $3 } /^PT = / { pt = $3 }
119 /^AAD = / { a = $3 } /^CT = / { ct = $3 }
120 /^Tag = / { print "E", h(k), h(iv), h(a), h(pt), h(ct), h($3); n++ }
121 END { if (c != n || n == 0) exit 1 }' > "$out/$f.txt" || fail "$f: conversion lost records"
122 nc=$(unzip -p "$vec/gcmtestvectors.zip" "$f.rsp" | grep -c '^Count = ')
123 run_ok "$f" "$out/$f.txt" "$nc"
124 nist=$((nist + nc))
125 done
126 nfail=0
127 for f in gcmDecrypt128 gcmDecrypt192 gcmDecrypt256; do
128 unzip -p "$vec/gcmtestvectors.zip" "$f.rsp" | awk '{ sub(/\r$/, "") }
129 function h(s) { return s == "" ? "-" : s }
130 /^Count = / { c++ }
131 /^Key = / { k = $3 } /^IV = / { iv = $3 } /^CT = / { ct = $3 }
132 /^AAD = / { a = $3 } /^Tag = / { t = $3 }
133 /^PT = / { print "D", h(k), h(iv), h(a), h(ct), h(t), h($3); n++ }
134 /^FAIL$/ { print "D", h(k), h(iv), h(a), h(ct), h(t), "FAIL"; n++ }
135 END { if (c != n || n == 0) exit 1 }' > "$out/$f.txt" || fail "$f: conversion lost records"
136 nc=$(unzip -p "$vec/gcmtestvectors.zip" "$f.rsp" | grep -c '^Count = ')
137 run_ok "$f" "$out/$f.txt" "$nc"
138 nist=$((nist + nc))
139 nfail=$((nfail + $(grep -c ' FAIL$' "$out/$f.txt")))
140 done
141 say "nist: $nist vectors ($nfail decrypt vectors expect FAIL)"
142fi
143
144# parameter refusals: return 2, nothing written. ctl = klen + 256 tlen + 65536 ivlen.
145ctl() { printf '%s' $(($1 + 256 * $2 + 65536 * $3)); }
146{
147 say "# X CTL KIALEN BUFLEN WANT: aes_gcm_seal and aes_gcm_open both return WANT"
148 say "X $(ctl 15 16 12) 27 16 2"
149 say "X $(ctl 0 16 12) 12 16 2"
150 say "X $(ctl 17 16 12) 29 16 2"
151 say "X $(ctl 33 16 12) 45 16 2"
152 say "X $(ctl 255 16 12) 300 16 2"
153 say "X $(ctl 16 0 12) 28 16 2"
154 say "X $(ctl 16 17 12) 28 32 2"
155 say "X $(ctl 16 255 12) 28 300 2"
156 say "X $(ctl 16 16 0) 16 16 2"
157 say "X $(ctl 16 16 12) 27 16 2"
158 say "X $(ctl 32 16 1000) 1031 16 2"
159 say "X $(ctl 16 16 12) 28 15 2"
160 say "X $(ctl 24 1 1) 24 1 2"
161 say "X $(ctl 24 4 1) 25 3 2"
162 say "# Y KNLEN AADLEN BUFLEN WANT: aead_aes128gcm_seal and _open both return WANT"
163 say "Y 27 0 16 2"
164 say "Y 29 0 16 2"
165 say "Y 0 0 16 2"
166 say "Y 28 5 15 2"
167 say "Y 28 0 0 2"
168} > "$out/params.txt"
169run_ok params "$out/params.txt" 19
170
171# 1 MiB in-place seal and open (three runs; the first warms up), throughput printed by the driver
172printf 'T 1048576\nT 1048576\nT 1048576\n' > "$out/speed.txt"
173run_ok speed "$out/speed.txt" 3
174grep '^aesgcm speed ' "$out/speed.out" || true
175
176# mutants run on FIPS 197 + Wycheproof
177cat "$vec/fips197.txt" "$vec/wycheproof_aes_gcm.txt" > "$out/small.txt"
178# S-box circuit: output gate S2 = T123 # T124 (XNOR) flipped to XOR
179mutant sbox 's/^put s 5 not xor get s 130 get s 131$/put s 5 xor get s 130 get s 131/'
180# GHASH reduction polynomial R = 0xe1 || 0^120 changed to 0xe3 || 0^120
181mutant ghash 's/^const AGR ulong 0xe100000000000000$/const AGR ulong 0xe300000000000000/'
182# key schedule Rcon: the xtime reduction 0x11b changed to 0x11d (Rcon[9], Rcon[10] wrong)
183mutant rcon 's/^rc = xor rc 0x11b$/rc = xor rc 0x11d/'
184# tag check bypassed (open accepts any tag): caught by the tamper controls and the invalid vectors
185mutant tagcheck 's/^d = or d xor get buf add n i gcm_tbyte k i$/d = or d 0/'
186# expectation mutant: one expected tag hex digit of Wycheproof tcId 1 changed -> exactly 1 failure
187awk '$1 == "W" && $2 == "1" { c = substr($9, 32, 1); $9 = substr($9, 1, 31) (c == "0" ? "1" : "0") } { print }' \
188 "$vec/wycheproof_aes_gcm.txt" > "$out/mut-expect.txt"
189if cmp -s "$vec/wycheproof_aes_gcm.txt" "$out/mut-expect.txt"; then
190 mut=$((mut + 1))
191 fail "expectation mutant: awk changed nothing"
192else
193 run_detect mutant-expect "$out/mut-expect.txt" "$out/driver" 1
194fi
195
196say "NOT_RUN wycheproof aes_gcm_siv_test.json: not vendored; AES-GCM-SIV is om/tls/gcmsiv.om"
197say "NOT_RUN wycheproof aria_gcm_test.json, seed_gcm_test.json, sm4_gcm_test.json: other block ciphers"
198say "NOT_RUN wycheproof c2sp_chunked_encryption_aes_{128,256}_gcm_test.json: chunked-encryption construction, not raw AES-GCM"
199
200if [ "$bad" -ne 0 ] || [ "$mutok" -ne "$mut" ]; then
201 say "AESGCM FAIL vectors=$total mutants=$mutok/$mut"
202 exit 1
203fi
204say "wycheproof: $nw tests: valid $wv passed, invalid $wi refused, acceptable 0"
205say "checks outside the vectors: params=19 speed=3 (both passed)"
206say "AESGCM PASS vectors=$((nf + nist + nw)) (fips197=$nf, nist=$nist, wycheproof=$nw) mutants=$mutok/$mut"