aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / scripts / test_trust1_measurement_tools.sh
| 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 | |
| 28 | set -euo pipefail |
| 29 | |
| 30 | repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 31 | campaign="${repo_root}/scripts/tpm_measurement_campaign.sh" |
| 32 | credpol="${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. |
| 35 | for 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; } |
| 37 | done |
| 38 | |
| 39 | work="$(mktemp -d)" |
| 40 | cleanup() { |
| 41 | [[ -z "${SWTPM_PID:-}" ]] || kill "${SWTPM_PID}" 2>/dev/null || true |
| 42 | rm -rf "${work}" |
| 43 | } |
| 44 | trap cleanup EXIT |
| 45 | |
| 46 | port=$((20000 + RANDOM % 20000)) |
| 47 | mkdir -p "${work}/tpm" |
| 48 | swtpm 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 & |
| 52 | SWTPM_PID=$! |
| 53 | export TPM2TOOLS_TCTI="swtpm:host=127.0.0.1,port=${port}" |
| 54 | for _ in $(seq 1 50); do tpm2_pcrread sha256:0 >/dev/null 2>&1 && break; sleep 0.1; done |
| 55 | tpm2_pcrread sha256:0 >/dev/null |
| 56 | |
| 57 | # Fake efivarfs: 4 attribute bytes + payload. |
| 58 | efi="${work}/efivars" |
| 59 | mkdir -p "${efi}" |
| 60 | g=8be4df61-93ca-11d2-aa0d-00e098032b8c |
| 61 | s=d719b2cb-3d3a-4596-a3bc-dad00e67656f |
| 62 | printf '\x06\x00\x00\x00\x01' >"${efi}/SecureBoot-${g}" |
| 63 | printf '\x06\x00\x00\x00\x00' >"${efi}/SetupMode-${g}" |
| 64 | for v in PK KEK; do printf '\x27\x00\x00\x00test-%s' "${v}" >"${efi}/${v}-${g}"; done |
| 65 | for v in db dbx; do printf '\x27\x00\x00\x00test-%s' "${v}" >"${efi}/${v}-${s}"; done |
| 66 | export AIENOS_EFIVARS_DIR="${efi}" AIENOS_EVENTLOG="${work}/no-eventlog" |
| 67 | |
| 68 | fail() { echo "FAIL $*"; exit 1; } |
| 69 | pass() { echo "PASS $*"; } |
| 70 | |
| 71 | bash "${campaign}" capture "${work}/a" state-a >/dev/null |
| 72 | bash "${campaign}" capture "${work}/b" state-b >/dev/null |
| 73 | grep -q '^secure_boot=1$' "${work}/a/capture.env" || fail "secure_boot byte not read" |
| 74 | grep -q '^eventlog_replay=no_eventlog$' "${work}/a/capture.env" || fail "absent event log not recorded" |
| 75 | bash "${campaign}" compare "${work}/a" "${work}/b" >/dev/null || fail "identical states reported as changed" |
| 76 | pass "capture + compare: identical states compare equal" |
| 77 | |
| 78 | bash "${campaign}" capture "${work}/a" again >/dev/null 2>&1 && fail "capture overwrote an existing directory" |
| 79 | pass "capture refuses to overwrite" |
| 80 | |
| 81 | tpm2_pcrextend 7:sha256=$(printf '%064d' 1) >/dev/null |
| 82 | bash "${campaign}" capture "${work}/c" state-c >/dev/null |
| 83 | set +e |
| 84 | out="$(bash "${campaign}" compare "${work}/a" "${work}/c")"; rc=$? |
| 85 | set -e |
| 86 | [[ ${rc} -eq 3 ]] || fail "compare exit ${rc}, expected 3 after PCR 7 extend" |
| 87 | grep -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" |
| 89 | pass "compare names exactly PCR 7 after one extend" |
| 90 | |
| 91 | bash "${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" |
| 94 | grep -q '"rollback_restored": true' "${work}/r1.json" || fail "r1 rollback_restored not true" |
| 95 | grep -q 'pcr_sha256_7' "${work}/r1.json" || fail "r1 does not list PCR 7 as changed" |
| 96 | set +e |
| 97 | bash "${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=$? |
| 99 | set -e |
| 100 | [[ ${rc} -eq 4 ]] || fail "unrestored rollback exit ${rc}, expected 4" |
| 101 | grep -q '"result": "STOP_UNEXPECTED_STATE"' "${work}/r2.json" || fail "r2 not STOP" |
| 102 | grep -q "\"previous_receipt_sha256\": \"$(sha256sum "${work}/r1.json" | cut -d' ' -f1)\"" "${work}/r2.json" \ |
| 103 | || fail "r2 not chained to r1" |
| 104 | pass "receipt: restored rollback RECORDED, unrestored STOP, chained" |
| 105 | |
| 106 | echo extra >"${work}/b/extra.txt" |
| 107 | bash "${campaign}" compare "${work}/a" "${work}/b" >/dev/null 2>&1 && fail "added file not detected" |
| 108 | echo x >>"${work}/c/capture.env" |
| 109 | bash "${campaign}" compare "${work}/a" "${work}/c" >/dev/null 2>&1 && fail "edited capture not detected" |
| 110 | pass "tampered captures refused" |
| 111 | |
| 112 | # Synthetic host+tpm2 credential header bound to PolicyPCR(sha256, PCR 7). |
| 113 | pcr7="$(tpm2_pcrread sha256:7 | awk '$1 == 7 { sub(/^0x/, "", $3); print tolower($3) }')" |
| 114 | pd="$(printf '%s' "${pcr7}" | xxd -r -p | sha256sum | cut -d' ' -f1)" |
| 115 | policy="$(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" |
| 126 | dec="$(bash "${credpol}" --live "${work}/test.cred")" || fail "decoder rejected matching credential" |
| 127 | grep -q 'type=host+tpm2' <<<"${dec}" || fail "type not decoded" |
| 128 | grep -q 'pcrs=7$' <<<"${dec}" || fail "PCR mask not decoded" |
| 129 | grep -q 'pcr_bank=sha256' <<<"${dec}" || fail "bank not decoded" |
| 130 | grep -q "policy_hash=${policy}" <<<"${dec}" || fail "policy hash not decoded" |
| 131 | grep -q 'policy_matches_live_pcrs=yes' <<<"${dec}" || fail "live policy should match" |
| 132 | tpm2_pcrextend 7:sha256=$(printf '%064d' 2) >/dev/null |
| 133 | bash "${credpol}" --live "${work}/test.cred" >"${work}/dec2.txt" && fail "decoder accepted after PCR 7 changed" |
| 134 | grep -q 'policy_matches_live_pcrs=no' "${work}/dec2.txt" || fail "live mismatch not reported" |
| 135 | pass "credential decoder: header decoded, --live follows PCR 7" |
| 136 | |
| 137 | echo "TRUST-1 measurement tools self-test: ALL PASS" |