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

161 lines · 7510 bytes Raw Blame History
1#!/bin/sh
2# SPDX-License-Identifier: Apache-2.0
3# ACVP sample-vector suite for om/tls: runs NIST's published ACVP-Server sample vectors
4# (usnistgov/ACVP-Server, gen-val/json-files, pinned commit below) through the library and compares
5# every answer with NIST's expectedResults.json. This is testing against the CAVP/ACVP test methods
6# with NIST's published vectors, not NIST validation: these are self-run sample vectors, NOT a CAVP
7# certificate. Only an accredited lab submitting through the ACVP production server earns one.
8#
9# Usage: sh om/tls/test/acvp/run.sh OMC (OMC = the pinned seed compiler)
10# Environment (all optional):
11# ACVP_DIR an ACVP-Server gen-val/json-files directory; otherwise the pinned commit is fetched
12# into build/tls/acvp/acvp-server. Every file used is checked against acvp-inputs.sha256.
13# MLKEM_SRC directory holding sha3.om and mlkem.om when they are not in om/tls yet
14# ACVP_DRBG_ROOT an omega-mv tree holding om/tls/test/drbg/run.sh when this one does not
15# Per algorithm: ACVP <FOLDER> PASS tests=N not_applicable=M, then the in-memory negative control
16# (one expected value corrupted must FAIL). Final line:
17# ACVP PASS algos=K tests=N not_applicable=M not_run=NONE exit 0
18# ACVP PARTIAL algos=K tests=N not_applicable=M not_run=LIST exit 3 (nothing failed, something not run)
19# ACVP FAIL ... exit 1
20set -eu
21omc=$(cd "$(dirname "${1:?usage: run.sh OMC}")" && pwd)/$(basename "$1")
22here=$(cd "$(dirname "$0")" && pwd)
23lib=$(cd "$here/../.." && pwd)
24root=$(cd "$lib/../.." && pwd)
25out="$root/build/tls/acvp"
26w="$out/work"
27pin=975de31eb83d87039ec88934fdc47d8c312b892d
28nice="nice -n 19 ionice -c3"
29mkdir -p "$w"
30rm -f "$w"/*
31
32if [ -n "${ACVP_DIR:-}" ]; then
33 J=$ACVP_DIR
34else
35 src="$out/acvp-server"
36 if [ ! -d "$src/.git" ]; then
37 git clone -q --filter=blob:none --no-checkout https://github.com/usnistgov/ACVP-Server.git "$src"
38 fi
39 git -C "$src" sparse-checkout set --no-cone $(sed 's,^[0-9a-f]* ,/gen-val/json-files/,' "$here/acvp-inputs.sha256")
40 git -C "$src" checkout -q "$pin"
41 J="$src/gen-val/json-files"
42fi
43(cd "$J" && sha256sum -c --quiet "$here/acvp-inputs.sha256") || { echo "ACVP FAIL inputs differ from acvp-inputs.sha256"; exit 1; }
44echo "ACVP inputs: ACVP-Server $pin, $(wc -l < "$here/acvp-inputs.sha256") files match acvp-inputs.sha256"
45
46fail=0 algos=0 tests=0 na=0 notrun=""
47cat "$here/acvpio.om" "$here/acvp.om" "$here/acvpmain.om" > "$out/acvp.prog.om"
48$nice sh "$lib/build.sh" "$omc" "$out/acvp.prog.om" "$out/acvp"
49
50# driver negative controls: a malformed record must be refused (BAD, exit 1)
51if printf 'H 1 256 abc\n' | "$out/acvp" > "$w/bad.out"; then
52 echo "ACVP FAIL driver accepted a malformed record"; fail=1
53elif [ "$(cat "$w/bad.out")" != "1 BAD" ]; then
54 echo "ACVP FAIL driver malformed-record reply: $(cat "$w/bad.out")"; fail=1
55fi
56
57field() { sed -n "s/.* $1=\([0-9]*\).*/\1/p"; }
58
59run_set() {
60 conv=$(python3 "$here/acvp2om.py" "$J" "$2" "$w") || { echo "ACVP $2 FAIL conversion"; fail=1; return; }
61 echo "$conv" > "$w/$2.conv.log"
62 if [ "$(echo "$conv" | field records)" = 0 ]; then
63 n=$(echo "$conv" | field not_applicable)
64 echo "ACVP $2 NOT_APPLICABLE tests=0 not_applicable=$n (reasons in $2.map.json)"
65 na=$((na + n))
66 return
67 fi
68 for f in "$w/$2".in.*; do
69 if ! $nice "$1" < "$f" > "$w/$2.out.${f##*.}"; then
70 echo "ACVP $2 FAIL driver exit on $(basename "$f")"; fail=1; return
71 fi
72 done
73 if ! python3 "$here/om2acvp.py" "$J" "$2" "$w"; then
74 echo "ACVP $2 FAIL response not built"; fail=1; return
75 fi
76 if line=$(python3 "$here/acvpcmp.py" "$J/$2/expectedResults.json" "$w/$2.response.json" "$w/$2.map.json" "$2" 2>> "$w/$2.cmp.err"); then
77 echo "$line"
78 algos=$((algos + 1)) tests=$((tests + $(echo "$line" | field tests))) na=$((na + $(echo "$line" | field not_applicable)))
79 else
80 echo "$line"; fail=1
81 fi
82 if python3 "$here/acvpcmp.py" "$J/$2/expectedResults.json" "$w/$2.response.json" "$w/$2.map.json" "$2" --flip > "$w/$2.neg.out" 2>&1; then
83 echo "ACVP $2 FAIL negative control (corrupted expected value) not detected"; fail=1
84 else
85 echo "ACVP $2 negative control FAIL as required: $(tail -1 "$w/$2.neg.out")"
86 fi
87}
88
89for f in SHA2-256-1.0 SHA2-384-1.0 SHA2-512-1.0 \
90 HMAC-SHA2-256-1.0 HMAC-SHA2-384-1.0 HMAC-SHA2-512-1.0 HMAC-SHA2-256-2.0 HMAC-SHA2-384-2.0 HMAC-SHA2-512-2.0 \
91 ACVP-AES-GCM-1.0 ACVP-AES-ECB-1.0 ECDSA-SigVer-1.0 ECDSA-SigVer-FIPS186-5 \
92 RSA-SigVer-FIPS186-4 RSA-SigVer-FIPS186-5 KDA-HKDF-Sp800-56Cr1 KDA-HKDF-Sp800-56Cr2 \
93 TLS-v1.3-KDF-RFC8446 XECDH-SSC-RFC7748 KAS-ECC-SSC-Sp800-56Ar3; do
94 run_set "$out/acvp" "$f"
95done
96
97# TLS 1.3 key schedule through tls.om's own ts_* path (the 25 tests the native schedule covers)
98t=TLS-v1.3-KDF-RFC8446
99if [ -f "$w/$t.native.response.json" ]; then
100 python3 "$here/acvpcmp.py" "$J/$t/expectedResults.json" "$w/$t.native.response.json" "$w/$t.map.json" "$t-native(tls.om)" --subset || fail=1
101 if python3 "$here/acvpcmp.py" "$J/$t/expectedResults.json" "$w/$t.native.response.json" "$w/$t.map.json" "$t-native" --subset --flip > "$w/$t.native.neg.out" 2>&1; then
102 echo "ACVP $t-native FAIL negative control not detected"; fail=1
103 fi
104fi
105
106# ML-KEM-768 (FIPS 203) through mlkem.om
107if [ -f "$lib/mlkem.om" ] && [ -f "$lib/sha3.om" ]; then
108 mk=$lib
109else
110 mk=${MLKEM_SRC:-}
111fi
112if [ -n "$mk" ] && [ -f "$mk/mlkem.om" ] && [ -f "$mk/sha3.om" ]; then
113 echo "ACVP ML-KEM sources: $(cd "$mk" && sha256sum sha3.om mlkem.om | tr '\n' ' ')"
114 { echo om2; cat "$mk/sha3.om" "$mk/mlkem.om" "$here/acvpio.om" "$here/mlkemacvp.om" "$here/acvpmain.om"; } > "$out/mlkem.unit.om"
115 $nice "$omc" < "$out/mlkem.unit.om" > "$out/mlkem"
116 chmod +x "$out/mlkem"
117 run_set "$out/mlkem" ML-KEM-keyGen-FIPS203
118 run_set "$out/mlkem" ML-KEM-encapDecap-FIPS203
119else
120 echo "ACVP ML-KEM NOT_RUN (no sha3.om/mlkem.om in om/tls or MLKEM_SRC)"
121 notrun="${notrun}ML-KEM,"
122fi
123
124# HMAC_DRBG SHA-256 (SP 800-90A) through om/tls/test/drbg, converted from the same pinned ACVP-Server
125dr=${ACVP_DRBG_ROOT:-$root}
126if [ -f "$dr/om/tls/test/drbg/run.sh" ]; then
127 hv="$dr/om/tls/test/drbg/vectors/acvp.txt"
128 grep -q "^# ACVP-Server $pin " "$hv" || { echo "ACVP hmacDRBG FAIL vectors not from ACVP-Server $pin"; fail=1; }
129 for p in hmacDRBG-1.0/internalProjection.json hmacDRBG-SP800-90Ar1/internalProjection.json; do
130 grep -q "^# $p sha256 $(cd "$J" && sha256sum "$p" | cut -d' ' -f1)\$" "$hv" || { echo "ACVP hmacDRBG FAIL $p sha256 differs"; fail=1; }
131 done
132 if line=$(cd "$dr" && $nice sh om/tls/test/drbg/run.sh "$omc" 2>&1 | tail -1) && echo "$line" | grep -q '^DRBG PASS '; then
133 echo "ACVP hmacDRBG (om/tls/test/drbg/run.sh in $dr) $line"
134 algos=$((algos + 1))
135 else
136 echo "ACVP hmacDRBG FAIL: $line"; fail=1
137 fi
138else
139 echo "ACVP hmacDRBG NOT_RUN (no om/tls/test/drbg/run.sh; set ACVP_DRBG_ROOT)"
140 notrun="${notrun}hmacDRBG,"
141fi
142
143# Outside the FIPS track (no ACVP vectors for these): RFC 8439 / RFC 7748 suites with Wycheproof
144for s in chacha x25519; do
145 if line=$($nice sh "$lib/test/$s/run.sh" "$omc" 2>&1 | tail -1) && echo "$line" | grep -q ' PASS '; then
146 echo "NONFIPS $line"
147 else
148 echo "NONFIPS $s FAIL: $line"; fail=1
149 fi
150done
151
152notrun=${notrun%,}
153if [ "$fail" -ne 0 ]; then
154 echo "ACVP FAIL algos=$algos tests=$tests not_applicable=$na not_run=${notrun:-NONE}"
155 exit 1
156fi
157if [ -n "$notrun" ]; then
158 echo "ACVP PARTIAL algos=$algos tests=$tests not_applicable=$na not_run=$notrun"
159 exit 3
160fi
161echo "ACVP PASS algos=$algos tests=$tests not_applicable=$na not_run=NONE"