zupt/tests/test_ct_timing.sh
Cristian Cezar Moisés 31fa4028aa
Some checks failed
CI / packaging-syntax (push) Has been cancelled
CI / build-and-test (clang) (push) Has been cancelled
CI / build-and-test (gcc) (push) Has been cancelled
CI / strict-warnings (clang, -Wall -Wextra -Wpedantic -Wshadow -Wcast-align -Wstrict-prototypes -Wmissing-prototypes -Wnull-dereference -O2 -std=c11 -Werror) (push) Has been cancelled
CI / strict-warnings (gcc, -Wall -Wextra -Wpedantic -Wshadow -Wcast-align -Wstrict-prototypes -Wmissing-prototypes -Wnull-dereference -Wformat-security -Wlogical-op -Wjump-misses-init -Wdouble-promotion -O2 -std=c11 -Werror) (push) Has been cancelled
CI / sanitizers (push) Has been cancelled
CI / pie-hardening (push) Has been cancelled
CI / cross-aarch64 (push) Has been cancelled
CI / dist-reproducibility (push) Has been cancelled
CI / release (push) Has been cancelled
tests: make 'make check' pass on the source-only (WITH_SDK=0) build
The SDK-mode regression tests (audit, F-08/F-10/F-11/F-12, pq-box, KDF
transparency) now skip cleanly when built without libzuptsdk, and the
constant-time test links libzuptsdk only when present (its primitives are
native). Also drop a dead 'seqs_decoded' variable in vv_ans.c that failed
the exact-size test's -Werror=unused-but-set-variable. 'make check' is
green source-only, so the openSUSE %check no longer blocks the build.
2026-07-08 16:15:04 -03:00

70 lines
2.7 KiB
Shell
Executable file

#!/bin/bash
# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright (c) 2025-2026 Cristian Cezar Moisés
#
# dudect-style constant-time verification of zupt_ct_memeq (v3.5.0).
# Builds at -O2 (the shipped optimisation level — so this tests the code
# as users run it, including that the volatile accumulator survives the
# optimiser) and runs the Welch t-test harness.
#
# Verdict is environment-relative: zupt_ct_memeq's data-dependent timing
# signal must be a small fraction (<=20%) of leaky memcmp measured in the
# same environment. On a dedicated box the ratio is ~0; on this shared
# vCPU it lands near 1%. If the host is too coarse for even memcmp to
# show a leak, the test reports INCONCLUSIVE (exit 0) rather than
# passing vacuously.
set -u
SDK_DIR="${ZUPTSDK_DIR:-vendor/zuptsdk}"
ARCH=$(uname -m)
if [ "$ARCH" = "x86_64" ] || [ "$ARCH" = "i686" ]; then
SHANI="-msha -mssse3 -msse4.1"
else
SHANI=""
fi
TMP=$(mktemp -d)
# This test uses only native CT primitives (zupt_ct_memeq, ML-KEM CT compare);
# the libzuptsdk linkage is vestigial. Link it only when the vendored library
# is present (WITH_SDK builds); source-only builds compile+run without it.
SDK_LINK=""
if ls "$SDK_DIR"/libzuptsdk.so* >/dev/null 2>&1; then
SDK_LINK="-L$SDK_DIR -lzuptsdk -Wl,-rpath,$(cd "$SDK_DIR" && pwd)"
fi
if gcc -Iinclude -Isrc -I"$SDK_DIR/include" -Wall -Wextra -Werror $SHANI -O2 -std=c11 \
tests/test_ct_timing.c \
src/zupt_crypto.c src/zupt_sha256.c src/zupt_sha256_shani.c src/zupt_aes256.c \
src/zupt_xxh.c src/zupt_keccak.c src/zupt_x25519.c src/zupt_mlkem.c \
src/zupt_cpuid.c src/zupt_mlock.c \
$SDK_LINK -lm \
-o "$TMP/t" 2>"$TMP/cc.log"; then
"$TMP/t"; rc=$?
else
echo " ✗ constant-time test failed to compile"
head -15 "$TMP/cc.log" | sed 's/^/ /'
rc=1
fi
rm -rf "$TMP"
# Source-routing guard: the security-critical compares must use the single
# audited zupt_ct_memeq primitive, not a reintroduced inline byte-OR loop.
# This is what makes the 32-byte timing proof transfer to the ML-KEM
# 1088-byte decaps compare (same function, length-independent).
echo ""
echo " -- source routing (audited primitive) --"
ROUTE_OK=0
if grep -q "zupt_ct_memeq(ct, ct_prime, 1088)" src/zupt_mlkem.c; then
echo " ✓ ML-KEM decaps compare routes through zupt_ct_memeq"
else
echo " ✗ ML-KEM decaps compare does NOT use zupt_ct_memeq (inline loop regressed?)"
ROUTE_OK=1
fi
# The decaps path must not contain a raw 1088-byte inline OR-compare anymore.
if grep -qE "for *\(int i = 0; i < 1088;" src/zupt_mlkem.c; then
echo " ✗ raw 1088-byte inline compare loop present in zupt_mlkem.c"
ROUTE_OK=1
else
echo " ✓ no raw 1088-byte inline compare loop in zupt_mlkem.c"
fi
[ "$rc" = 0 ] && rc=$ROUTE_OK
exit $rc