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 / native / m5 / tests / m5_migsig_tool_test.sh

135 lines · 8278 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# m5_migsig tool test: build, sign and verify an owner-signed migration
18# record with FRESHLY GENERATED TEST KEYS (random, labelled TEST-ONLY, deleted
19# afterwards; never an owner key), then check every refusal end to end.
20# If openssl is installed, also cross-checks the public key and the signature
21# against it (an independent Ed25519); otherwise that part is reported skipped.
22# Usage: m5_migsig_tool_test.sh TOOL WORK_DIR
23# Last line: M5_MIGSIG_TOOL: PASS (exit 0) or M5_MIGSIG_TOOL: FAIL (exit 1).
24set -u
25tool="$(realpath "$1")"; work="$2"
26umask 077
27rm -rf "${work}"; mkdir -p "${work}"; cd "${work}" || exit 1
28fail=0
29ok() { echo "ok $1"; }
30bad() { echo "FAIL $1"; fail=1; }
31# expect RC DESCRIPTION -- command...
32expect() {
33 local want="$1" what="$2"; shift 3
34 "$@" >out.log 2>&1; local rc=$?
35 if [[ ${rc} == "${want}" ]]; then ok "${what}"; else bad "${what} (exit ${rc}, want ${want}): $(head -c 300 out.log)"; fi
36}
37flip_byte() { # FILE OFFSET
38 local v; v=$(od -An -tu1 -j "$2" -N1 "$1" | tr -d ' ')
39 printf "\\x$(printf %02x $((v ^ 1)))" | dd of="$1" bs=1 seek="$2" conv=notrunc status=none
40}
41
42head -c 32 /dev/urandom > TEST-ONLY-owner-key-1.sk
43head -c 32 /dev/urandom > TEST-ONLY-owner-key-2.sk
44expect 0 "pubkey (TEST key 1)" -- "${tool}" pubkey TEST-ONLY-owner-key-1.sk TEST-ONLY-owner-key-1.pub
45expect 0 "pubkey (TEST key 2)" -- "${tool}" pubkey TEST-ONLY-owner-key-2.sk TEST-ONLY-owner-key-2.pub
46[[ $(wc -c < TEST-ONLY-owner-key-1.pub) == 65 ]] && ok "public key file is 64 hex + newline" || bad "public key file size"
47
48man=$(printf 'AIENOS TEST-ONLY manifest' | sha256sum | cut -d' ' -f1)
49cat > spec.txt <<SPEC
50# TEST-ONLY migration spec
51class=test
52agent_root=$(printf '32%.0s' $(seq 32))
53source_store=$(printf 'a0%.0s' $(seq 16))
54dest_store=$(printf 'd0%.0s' $(seq 16))
55source_store_generation=9
56store_format_version=1
57envelope=$(printf '30%.0s' $(seq 32))
58envelope=$(printf '10%.0s' $(seq 32))
59envelope=$(printf '20%.0s' $(seq 32))
60migration_manifest_digest=${man}
61migration_counter=4
62owner_hierarchy_generation=1
63SPEC
64expect 0 "body from spec" -- "${tool}" body spec.txt TEST-ONLY-owner-key-1.pub body.bin
65[[ $(wc -c < body.bin) == 272 ]] && ok "body is 272 bytes" || bad "body size"
66expect 0 "sign with TEST key 1" -- "${tool}" sign TEST-ONLY-owner-key-1.sk body.bin rec.bin
67expect 0 "verify (last counter 3)" -- "${tool}" verify TEST-ONLY-owner-key-1.pub rec.bin spec.txt 3
68grep -q '^M5_OWNER_MIGRATION_VERIFY: OK$' out.log && grep -q '^envelope_count=3$' out.log && ok "verify output" || bad "verify output"
69
70# Refusals
71expect 1 "wrong public key refused" -- "${tool}" verify TEST-ONLY-owner-key-2.pub rec.bin spec.txt 3
72grep -q 'REFUSED AUTH' out.log && ok "wrong key reported as AUTH" || bad "wrong key reason"
73expect 1 "replayed counter (last 4) refused" -- "${tool}" verify TEST-ONLY-owner-key-1.pub rec.bin spec.txt 4
74grep -q 'REFUSED REPLAY' out.log && ok "replay reported as REPLAY" || bad "replay reason"
75expect 1 "older counter (last 9) refused" -- "${tool}" verify TEST-ONLY-owner-key-1.pub rec.bin spec.txt 9
76expect 1 "unsigned body refused" -- "${tool}" verify TEST-ONLY-owner-key-1.pub body.bin spec.txt 3
77for off in 0 12 20 90 100 112 124 140 170 195 205 210 271; do
78 cp rec.bin t.bin; flip_byte t.bin "${off}"
79 expect 1 "tampered byte ${off} refused" -- "${tool}" verify TEST-ONLY-owner-key-1.pub t.bin spec.txt 3
80done
81for n in 0 1 207 208 271; do
82 head -c "${n}" rec.bin > t.bin
83 expect 1 "truncated to ${n} bytes refused" -- "${tool}" verify TEST-ONLY-owner-key-1.pub t.bin spec.txt 3
84done
85{ cat rec.bin; printf 'x'; } > t.bin
86expect 1 "extended record refused" -- "${tool}" verify TEST-ONLY-owner-key-1.pub t.bin spec.txt 3
87sed 's/^source_store_generation=9$/source_store_generation=8/' spec.txt > spec_gen.txt
88expect 1 "verifier expecting another store generation refuses" -- "${tool}" verify TEST-ONLY-owner-key-1.pub rec.bin spec_gen.txt 3
89grep -v "^envelope=$(printf '20%.0s' $(seq 32))\$" spec.txt > spec_env.txt
90expect 1 "verifier expecting another envelope set refuses" -- "${tool}" verify TEST-ONLY-owner-key-1.pub rec.bin spec_env.txt 3
91sed 's/^class=test$/class=production/' spec.txt > spec_cls.txt
92expect 1 "TEST-class record refused in production mode" -- "${tool}" verify TEST-ONLY-owner-key-1.pub rec.bin spec_cls.txt 3
93expect 1 "signing a body that names another key refused" -- "${tool}" sign TEST-ONLY-owner-key-2.sk body.bin rec2.bin
94[[ ! -e rec2.bin ]] && ok "no output after refused signing" || bad "output left after refused signing"
95expect 1 "re-signing a signed record refused" -- "${tool}" sign TEST-ONLY-owner-key-1.sk rec.bin rec3.bin
96expect 2 "existing output not overwritten" -- "${tool}" sign TEST-ONLY-owner-key-1.sk body.bin rec.bin
97cp TEST-ONLY-owner-key-1.sk loose.sk; chmod 644 loose.sk
98expect 2 "group/world-readable secret key refused" -- "${tool}" sign loose.sk body.bin rec4.bin
99head -c 31 TEST-ONLY-owner-key-1.sk > short.sk
100expect 2 "31-byte secret key refused" -- "${tool}" sign short.sk body.bin rec5.bin
101{ cat spec.txt; echo "envelope=$(printf '10%.0s' $(seq 32))"; } > spec_dup.txt
102expect 2 "duplicate envelope in spec refused" -- "${tool}" body spec_dup.txt TEST-ONLY-owner-key-1.pub b2.bin
103grep -v '^migration_counter=' spec.txt > spec_miss.txt
104expect 2 "spec missing a key refused" -- "${tool}" body spec_miss.txt TEST-ONLY-owner-key-1.pub b3.bin
105{ cat spec.txt; echo "migration_counter=5"; } > spec_twice.txt
106expect 2 "spec key given twice refused" -- "${tool}" body spec_twice.txt TEST-ONLY-owner-key-1.pub b4.bin
107
108# Key file formats: hex secret key and PKCS#8 DER give the same signature.
109od -An -tx1 -v TEST-ONLY-owner-key-1.sk | tr -d ' \n' > TEST-ONLY-owner-key-1.hex; echo >> TEST-ONLY-owner-key-1.hex
110expect 0 "hex secret key accepted" -- "${tool}" sign TEST-ONLY-owner-key-1.hex body.bin rec_hex.bin
111cmp -s rec.bin rec_hex.bin && ok "hex key gives the identical record" || bad "hex key record differs"
112{ printf '\x30\x2e\x02\x01\x00\x30\x05\x06\x03\x2b\x65\x70\x04\x22\x04\x20'; cat TEST-ONLY-owner-key-1.sk; } > TEST-ONLY-owner-key-1.p8.der
113expect 0 "PKCS#8 DER secret key accepted" -- "${tool}" sign TEST-ONLY-owner-key-1.p8.der body.bin rec_der.bin
114cmp -s rec.bin rec_der.bin && ok "DER key gives the identical record" || bad "DER key record differs"
115
116# Independent cross-check with openssl (outside tool used only as a test oracle).
117if command -v openssl >/dev/null && openssl pkey -inform DER -in TEST-ONLY-owner-key-1.p8.der -pubout -outform DER -out spki.der 2>/dev/null; then
118 [[ "$(tail -c 32 spki.der | od -An -tx1 -v | tr -d ' \n')" == "$(head -c 64 TEST-ONLY-owner-key-1.pub)" ]] \
119 && ok "public key matches openssl" || bad "public key differs from openssl"
120 expect 0 "SPKI DER public key accepted" -- "${tool}" verify spki.der rec.bin spec.txt 3
121 { printf 'AIENOS-M5-OWNER-MIGRATION-V1\0'; head -c 208 rec.bin; } > msg.bin
122 tail -c 64 rec.bin > sig.bin
123 if openssl pkeyutl -verify -pubin -inkey spki.der -keyform DER -rawin -in msg.bin -sigfile sig.bin >/dev/null 2>&1; then
124 ok "openssl verifies the record signature"
125 else bad "openssl does not verify the record signature"; fi
126 flip_byte msg.bin 150
127 if openssl pkeyutl -verify -pubin -inkey spki.der -keyform DER -rawin -in msg.bin -sigfile sig.bin >/dev/null 2>&1; then
128 bad "openssl accepted a tampered message"
129 else ok "openssl refuses the tampered message"; fi
130else
131 echo "skip openssl cross-check (openssl with Ed25519 not available)"
132fi
133
134rm -f ./*.sk ./*.hex ./*.p8.der
135if [[ ${fail} == 0 ]]; then echo "M5_MIGSIG_TOOL: PASS"; else echo "M5_MIGSIG_TOOL: FAIL"; exit 1; fi