interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / scripts / test_trust1_measurement_tools.sh

137 lines · 7232 bytes Raw Blame History
1#!/usr/bin/env bash
2# SPDX-License-Identifier: Apache-2.0
3# Copyright 2026 Drake Stapleton (AIEN / aien-dev)
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# test_trust1_measurement_tools.sh: host self-test for the TRUST-1 Gate 0/2
18# read-only tools, run against a software TPM (swtpm). No hardware access:
19# PCRs come from swtpm, Secure Boot variables from a fake efivars folder,
20# and the firmware event log is absent.
21#
22# Checks: capture works and is self-consistent; compare finds no change
23# between identical states and names the PCR that changed; the Gate 2
24# receipt records a restored rollback and stops on an unrestored one; a
25# tampered capture is refused; the credential header decoder reads a
26# synthetic credential and its --live check follows PCR 7.
27
28set -euo pipefail
29
30repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
31campaign="${repo_root}/scripts/tpm_measurement_campaign.sh"
32credpol="${repo_root}/scripts/trust1_credential_policy.sh"
33
34# FLAG(sovereignty): tpm2-tools (outside dep) required on Linux host; replace with in-house C TPM tool.
35for t in swtpm tpm2_pcrread tpm2_pcrextend tpm2_eventlog xxd; do
36 command -v "${t}" >/dev/null || { echo "Error: ${t} not installed" >&2; exit 2; }
37done
38
39work="$(mktemp -d)"
40cleanup() {
41 [[ -z "${SWTPM_PID:-}" ]] || kill "${SWTPM_PID}" 2>/dev/null || true
42 rm -rf "${work}"
43}
44trap cleanup EXIT
45
46port=$((20000 + RANDOM % 20000))
47mkdir -p "${work}/tpm"
48swtpm socket --tpm2 --tpmstate dir="${work}/tpm" \
49 --server type=tcp,port="${port}",bindaddr=127.0.0.1 \
50 --ctrl type=tcp,port=$((port + 1)),bindaddr=127.0.0.1 \
51 --flags not-need-init,startup-clear &
52SWTPM_PID=$!
53export TPM2TOOLS_TCTI="swtpm:host=127.0.0.1,port=${port}"
54for _ in $(seq 1 50); do tpm2_pcrread sha256:0 >/dev/null 2>&1 && break; sleep 0.1; done
55tpm2_pcrread sha256:0 >/dev/null
56
57# Fake efivarfs: 4 attribute bytes + payload.
58efi="${work}/efivars"
59mkdir -p "${efi}"
60g=8be4df61-93ca-11d2-aa0d-00e098032b8c
61s=d719b2cb-3d3a-4596-a3bc-dad00e67656f
62printf '\x06\x00\x00\x00\x01' >"${efi}/SecureBoot-${g}"
63printf '\x06\x00\x00\x00\x00' >"${efi}/SetupMode-${g}"
64for v in PK KEK; do printf '\x27\x00\x00\x00test-%s' "${v}" >"${efi}/${v}-${g}"; done
65for v in db dbx; do printf '\x27\x00\x00\x00test-%s' "${v}" >"${efi}/${v}-${s}"; done
66export AIENOS_EFIVARS_DIR="${efi}" AIENOS_EVENTLOG="${work}/no-eventlog"
67
68fail() { echo "FAIL $*"; exit 1; }
69pass() { echo "PASS $*"; }
70
71bash "${campaign}" capture "${work}/a" state-a >/dev/null
72bash "${campaign}" capture "${work}/b" state-b >/dev/null
73grep -q '^secure_boot=1$' "${work}/a/capture.env" || fail "secure_boot byte not read"
74grep -q '^eventlog_replay=no_eventlog$' "${work}/a/capture.env" || fail "absent event log not recorded"
75bash "${campaign}" compare "${work}/a" "${work}/b" >/dev/null || fail "identical states reported as changed"
76pass "capture + compare: identical states compare equal"
77
78bash "${campaign}" capture "${work}/a" again >/dev/null 2>&1 && fail "capture overwrote an existing directory"
79pass "capture refuses to overwrite"
80
81tpm2_pcrextend 7:sha256=$(printf '%064d' 1) >/dev/null
82bash "${campaign}" capture "${work}/c" state-c >/dev/null
83set +e
84out="$(bash "${campaign}" compare "${work}/a" "${work}/c")"; rc=$?
85set -e
86[[ ${rc} -eq 3 ]] || fail "compare exit ${rc}, expected 3 after PCR 7 extend"
87grep -q '^CHANGED pcr_sha256_7 ' <<<"${out}" || fail "compare did not name PCR 7"
88[[ "$(grep -c '^CHANGED' <<<"${out}")" -eq 1 ]] || fail "compare reported more than PCR 7"
89pass "compare names exactly PCR 7 after one extend"
90
91bash "${campaign}" receipt --variable test-extend --before "${work}/a" --after "${work}/c" \
92 --post-rollback "${work}/b" --explain "self-test" --out "${work}/r1.json" >/dev/null \
93 || fail "restored rollback not recorded"
94grep -q '"rollback_restored": true' "${work}/r1.json" || fail "r1 rollback_restored not true"
95grep -q 'pcr_sha256_7' "${work}/r1.json" || fail "r1 does not list PCR 7 as changed"
96set +e
97bash "${campaign}" receipt --variable test-unrestored --before "${work}/a" --after "${work}/c" \
98 --post-rollback "${work}/c" --prev "${work}/r1.json" --out "${work}/r2.json" >/dev/null; rc=$?
99set -e
100[[ ${rc} -eq 4 ]] || fail "unrestored rollback exit ${rc}, expected 4"
101grep -q '"result": "STOP_UNEXPECTED_STATE"' "${work}/r2.json" || fail "r2 not STOP"
102grep -q "\"previous_receipt_sha256\": \"$(sha256sum "${work}/r1.json" | cut -d' ' -f1)\"" "${work}/r2.json" \
103 || fail "r2 not chained to r1"
104pass "receipt: restored rollback RECORDED, unrestored STOP, chained"
105
106echo extra >"${work}/b/extra.txt"
107bash "${campaign}" compare "${work}/a" "${work}/b" >/dev/null 2>&1 && fail "added file not detected"
108echo x >>"${work}/c/capture.env"
109bash "${campaign}" compare "${work}/a" "${work}/c" >/dev/null 2>&1 && fail "edited capture not detected"
110pass "tampered captures refused"
111
112# Synthetic host+tpm2 credential header bound to PolicyPCR(sha256, PCR 7).
113pcr7="$(tpm2_pcrread sha256:7 | awk '$1 == 7 { sub(/^0x/, "", $3); print tolower($3) }')"
114pd="$(printf '%s' "${pcr7}" | xxd -r -p | sha256sum | cut -d' ' -f1)"
115policy="$(printf '%064d%s%s%s' 0 0000017f 00000001000b03800000 "${pd}" | xxd -r -p | sha256sum | cut -d' ' -f1)"
116{
117 printf '93a8940948744490' ; printf '90caf2fc93cab553' # type id host+tpm2
118 printf '20000000010000000c00000010000000' # key, block, iv, tag sizes
119 printf '%032d' 0 # iv (12) padded to 16
120 printf '8000000000000000' ; printf '0b00' ; printf '2300' # mask, bank, primary
121 printf '08000000' ; printf '20000000' # blob 8 bytes, policy 32
122 printf '%016d' 0 # blob
123 printf '%s' "${policy}"
124 printf '%064d' 0 # trailing ciphertext stand-in
125} | xxd -r -p | base64 >"${work}/test.cred"
126dec="$(bash "${credpol}" --live "${work}/test.cred")" || fail "decoder rejected matching credential"
127grep -q 'type=host+tpm2' <<<"${dec}" || fail "type not decoded"
128grep -q 'pcrs=7$' <<<"${dec}" || fail "PCR mask not decoded"
129grep -q 'pcr_bank=sha256' <<<"${dec}" || fail "bank not decoded"
130grep -q "policy_hash=${policy}" <<<"${dec}" || fail "policy hash not decoded"
131grep -q 'policy_matches_live_pcrs=yes' <<<"${dec}" || fail "live policy should match"
132tpm2_pcrextend 7:sha256=$(printf '%064d' 2) >/dev/null
133bash "${credpol}" --live "${work}/test.cred" >"${work}/dec2.txt" && fail "decoder accepted after PCR 7 changed"
134grep -q 'policy_matches_live_pcrs=no' "${work}/dec2.txt" || fail "live mismatch not reported"
135pass "credential decoder: header decoded, --live follows PCR 7"
136
137echo "TRUST-1 measurement tools self-test: ALL PASS"