Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -888,8 +888,8 @@ TABLES_BSS.
between each, matrix in `tools/package/_common.sh`:

c64-https-ip65-onchip.prg stock C64 + RR-Net, no REU, no turbo
c64-https-uci-onchip.prg turbo, no REU
c64-https-uci-comb.prg turbo + REU, fastest (needs bank 2 + boot precompute)
c64-https-uci-onchip.prg turbo, no REU (TRUST_STORE=1)
c64-https-uci-comb.prg turbo + REU, fastest (needs bank 2 + boot precompute; TRUST_STORE=1)

REU-profile images were retired (curation: still fastest below ~18 MHz,
one line in `PACKAGE_VARIANTS` to restore). **That line is a
Expand Down
27 changes: 27 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,20 @@ endif
# public key and generation floor come from TRUST_BUNDLE_KEY_INC, a file
# `tools/trust_bundle.py inc` writes. The tree's default is the TEST-ONLY
# key, whose private half is committed: anyone can sign for it.
# TRUST_RELEASE=1 marks a release build (tools/package/build_prgs.sh passes
# it for every product) and refuses that key three times over: here at parse
# time, by the .inc's 64 `$$XX` tokens (fast, but textual); in trust_bundle.s,
# by its TRUST_BUNDLE_KEY_IS_TEST_ONLY flag; and after the link, by the
# ASSEMBLED bytes (tools/check_release_key.py searches the PRG for the test
# key derived from its PEM, and the PRG is deleted on a hit), which no
# spelling of the .inc gets past.
TRUST_BUNDLE ?=
TRUST_BUNDLE_KEY_INC ?= tools/trust_bundle_TEST_ONLY_pubkey.inc
TRUST_BUNDLE_TEST_KEY_INC := tools/trust_bundle_TEST_ONLY_pubkey.inc
TRUST_RELEASE ?=
ifneq ($(filter-out 0 1,$(TRUST_RELEASE)),)
$(error TRUST_RELEASE must be 1 (a release build) or 0/unset)
endif
# The 64 key bytes of an .inc, as one word: what the guard compares.
trust_key_bytes = $(shell sed -n '/^\.macro TRUST_BUNDLE_PUBKEY_BYTES/,/^\.endmacro/p' '$(1)' 2>/dev/null | grep -o '\$$[0-9A-Fa-f][0-9A-Fa-f]' | tr -d '\n$$' | tr a-f A-F)
ifeq ($(TRUST_BUNDLE),1)
Expand All @@ -296,6 +308,12 @@ TRUST_BUNDLE_KEY_HEX := $(call trust_key_bytes,$(TRUST_BUNDLE_KEY_INC))
ifneq ($(words $(TRUST_BUNDLE_KEY_HEX)) $(shell printf '%s' '$(TRUST_BUNDLE_KEY_HEX)' | wc -c | tr -d ' '),1 128)
$(error TRUST_BUNDLE_KEY_INC=$(TRUST_BUNDLE_KEY_INC) has no 64-byte TRUST_BUNDLE_PUBKEY_BYTES macro)
endif
ifeq ($(TRUST_RELEASE),1)
ifeq ($(TRUST_BUNDLE_KEY_HEX),$(call trust_key_bytes,$(TRUST_BUNDLE_TEST_KEY_INC)))
$(error TRUST_RELEASE=1 with the TEST-ONLY trust-bundle key ($(TRUST_BUNDLE_KEY_INC)): its private half is in the repository, so anyone could sign a bundle this release accepts. Build with TRUST_BUNDLE_KEY_INC=<your production key's .inc>, or without TRUST_BUNDLE)
endif
CA65FLAGS += -D TRUST_RELEASE=1
endif
CA65FLAGS += -D TRUST_BUNDLE=1
UCI_SRCS += src/net/uci/trust_bundle.s
else ifneq ($(filter-out 0,$(TRUST_BUNDLE)),)
Expand Down Expand Up @@ -660,6 +678,9 @@ $(PRG): $(PRG_DEPS)
@grep -q '^al C:BC00 \.sqtab_lo' $(LABELS) || \
{ echo 'ERROR: sqtab_lo is not at $$BC00 — TABLES_BSS layout drifted; realign LIB_SHARED_SQTAB_BASE in tools/integration/build_x25519.sh and build_nistcurves_p256.sh'; \
grep ' \.sqtab_lo$$' $(LABELS); exit 1; }
ifeq ($(TRUST_RELEASE)$(TRUST_BUNDLE),11)
@python3 tools/check_release_key.py $@ || { rm -f $@; exit 1; }
endif

# Phase 5 Fix D: $(LABELS) is normally a side-effect of the $(PRG)
# link recipe; we don't add an explicit rule. The overlay-bin rule
Expand Down Expand Up @@ -1062,6 +1083,12 @@ endif
# pair, whichever of them grows a direct dependency next.
build/boot.o build/http.o build/cert_pin.o: build/https_host.inc
build/net/uci/trust_store.o: build/https_host.inc
# Only reached when the parse-time write above did not run (a -n/-q/-t dry
# run on a tree that has never built): the same write, as a recipe, so a
# dry run of a fresh checkout answers instead of dying "No rule to make".
build/https_host.inc:
@mkdir -p build
$(HTTPS_TARGET_INC_BODY) > $@

# The trust-bundle key (#155 phase 2, L3) gets the same content-compared
# copy, build/trust_key.inc: a different key or floor rebuilds exactly
Expand Down
8 changes: 7 additions & 1 deletion src/net/uci/trust_bundle.s
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
; then the record for this attempt's ts_key, if any, goes to tb_spki
; (tb_found = 1): the hook in src/cert_pin.s warns if the server's key
; differs. The public key and the floor come from build/trust_key.inc
; (TRUST_BUNDLE_KEY_INC; the tree's key is TEST-ONLY).
; (TRUST_BUNDLE_KEY_INC; the tree's key is TEST-ONLY, and a release build
; refuses it — see the assert below and the Makefile).
;
; VERIFY ONCE PER BOOT (DECISIONS 11, Q4): the ECDSA verify (16-30 s at
; 48 MHz) runs at the first 'G' that finds the file. Its digest and verdict
Expand Down Expand Up @@ -69,6 +70,11 @@ TB_V_NONE = 0 ; tb_verdict: nothing judged yet
TB_V_GOOD = 1
TB_V_BAD = 2

; A release (make package) must not carry the key whose private half is in
; the repository. The Makefile refuses it too, by its bytes.
.ifdef TRUST_RELEASE
.assert TRUST_BUNDLE_KEY_IS_TEST_ONLY = 0, error, "TEST-ONLY trust-bundle key in a release build (TRUST_BUNDLE_KEY_INC)"
.endif
.assert TB_HEADER_LEN = 8 .and TB_RECORD_LEN = TS_REC_SIZE .and TB_SIG_LEN = 64, error, "bundle format v1"
.assert TB_FILE_MAX + 1 <= TCP_RECV_MASK + 1, error, "bundle > TCP ring"
.assert TB_MAX_RECORDS < 64, error, "tb_body_len: N * 64 must fit in 12 bits"
Expand Down
58 changes: 58 additions & 0 deletions tools/check_release_key.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env python3
"""check_release_key.py — refuse a linked PRG that carries the TEST-ONLY bundle key.

#155 phase 2, L3. The Makefile's parse-time guard compares the key .inc's
`$XX` tokens, which is text: an .inc that spells a byte in decimal (`234`
for `$EA`) or hides a `$00` in a comment slips past it while ca65 still
assembles the test key (adv-269 #1). This looks at what was ASSEMBLED: it
derives the test key's 64 bytes (Qx || Qy, the layout trust_bundle.s
embeds) from tools/trust_bundle_TEST_ONLY_signing_key.pem — no .inc parsed —
and searches the whole PRG for them. Anywhere is enough: the key has no
business in a release image, and a whole-file search needs no label math
for the comb cold image (linked to run in cert_buf, carried at $C000).

The Makefile runs it after the link under TRUST_RELEASE=1 TRUST_BUNDLE=1
and deletes the PRG when it fails.

python3 tools/check_release_key.py build/c64-https.prg

Exit 0 the key is absent; 1 it is present, or the check could not run
(fail closed: this only ever runs for a release).
"""
from __future__ import annotations

import sys
from pathlib import Path

HERE = Path(__file__).resolve().parent


def test_key_bytes() -> bytes:
sys.path.insert(0, str(HERE))
import trust_bundle as tb # noqa: PLC0415 — needs `cryptography`
return tb.pubkey_xy(tb.load_private_key(tb.TEST_KEY_PATH).public_key())


def main(argv) -> int:
if len(argv) != 1:
print("usage: check_release_key.py <prg>", file=sys.stderr)
return 1
try:
key = test_key_bytes()
prg = Path(argv[0]).read_bytes()
except Exception as exc: # noqa: BLE001 — fail closed
print(f"ERROR: release key check could not run ({type(exc).__name__}: {exc})",
file=sys.stderr)
return 1
assert len(key) == 64
at = prg.find(key)
if at >= 0:
print(f"ERROR: {argv[0]} carries the TEST-ONLY trust-bundle key (PRG offset {at}): "
"its private half is in the repository. Build the release with "
"TRUST_BUNDLE_KEY_INC=<your production key's .inc>.", file=sys.stderr)
return 1
return 0


if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
13 changes: 11 additions & 2 deletions tools/package/_common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,22 @@
# and every downstream script picks it up with no further edits.
PACKAGE_VARIANTS=(
"ip65-onchip|c64-https-ip65-onchip.prg|BACKEND=ip65 USE_NISTCURVES_ONCHIP=1|ip65-noreu|ip65|MAXIMUM COMPATIBILITY. Bone-stock C64 + RR-Net / cs8900a cartridge. No REU, no turbo, nothing optional at all — the only image a completely unmodified machine can run end to end. Slowest: ~36 min per handshake at 1 MHz."
"uci-onchip|c64-https-uci-onchip.prg|BACKEND=uci USE_NISTCURVES_ONCHIP=1|uci-noreu|uci|TURBO, NO REU. Ultimate 64 / C64 Ultimate at 32/48/64 MHz with the REU disabled or absent. Boots straight to the menu."
"uci-comb|c64-https-uci-comb.prg|BACKEND=uci USE_NISTCURVES_ONCHIP_COMB=1|uci-comb|uci|TURBO + REU — FASTEST. Ultimate 64 / C64 Ultimate at 32/48/64 MHz with the REU enabled. ~1.7x faster verify than the no-REU turbo image (measured: 16.4 s vs 28.4 s on a U64E at 48 MHz). Costs a one-time table build at every boot: ~34 s at 64 MHz, ~45 s at 48 MHz. Needs REU bank 2."
"uci-onchip|c64-https-uci-onchip.prg|BACKEND=uci USE_NISTCURVES_ONCHIP=1 TRUST_STORE=1|uci-noreu|uci|TURBO, NO REU. Ultimate 64 / C64 Ultimate at 32/48/64 MHz with the REU disabled or absent. Boots straight to the menu. Remembers each server's key on first use (TRUST.A / TRUST.B on /USB1) and refuses a changed one unless you accept it."
"uci-comb|c64-https-uci-comb.prg|BACKEND=uci USE_NISTCURVES_ONCHIP_COMB=1 TRUST_STORE=1|uci-comb|uci|TURBO + REU — FASTEST. Ultimate 64 / C64 Ultimate at 32/48/64 MHz with the REU enabled. ~1.7x faster verify than the no-REU turbo image (measured: 16.4 s vs 28.4 s on a U64E at 48 MHz). Costs a one-time table build at every boot: ~34 s at 64 MHz, ~45 s at 48 MHz. Needs REU bank 2. Remembers each server's key on first use (TRUST.A / TRUST.B on /USB1) and refuses a changed one unless you accept it."
)

# Field accessors — `variant_field <line> <1-based index>`.
variant_field() { printf '%s' "$1" | cut -d'|' -f"$2"; }

# The make arguments a variant is BUILT with: its matrix args plus
# TRUST_RELEASE=1, which marks a release build. Under TRUST_BUNDLE=1 the
# Makefile then refuses the TEST-ONLY bundle key (#155 phase 2, L3);
# without the bundle it changes no byte. build_prgs.sh records exactly
# this in build-info, so verify_release.py rebuilds with it too. The
# bundle stays out of the matrix until a production key exists
# (DECISIONS 11, Q1).
variant_make_args() { printf '%s TRUST_RELEASE=1' "$(variant_field "$1" 3)"; }

# --- Paths --------------------------------------------------------------------
# PROJECT_ROOT must be set by the caller before sourcing (it knows its own $0).
: "${PROJECT_ROOT:?_common.sh: PROJECT_ROOT must be set before sourcing}"
Expand Down
8 changes: 5 additions & 3 deletions tools/package/build_prgs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
# Three variants, one per shipped product (PACKAGE_VARIANTS in _common.sh):
#
# c64-https-ip65-onchip.prg make BACKEND=ip65 USE_NISTCURVES_ONCHIP=1
# c64-https-uci-onchip.prg make BACKEND=uci USE_NISTCURVES_ONCHIP=1
# c64-https-uci-comb.prg make BACKEND=uci USE_NISTCURVES_ONCHIP_COMB=1
# c64-https-uci-onchip.prg make BACKEND=uci USE_NISTCURVES_ONCHIP=1 TRUST_STORE=1
# c64-https-uci-comb.prg make BACKEND=uci USE_NISTCURVES_ONCHIP_COMB=1 TRUST_STORE=1
#
# each with TRUST_RELEASE=1 appended (variant_make_args in _common.sh).
#
# The matrix itself lives in _common.sh; this script has no per-variant
# knowledge and nothing version-specific, so it survives a library bump with
Expand Down Expand Up @@ -126,7 +128,7 @@ failed=0
for line in "${PACKAGE_VARIANTS[@]}"; do
key="$(variant_field "$line" 1)"
prg="$(variant_field "$line" 2)"
args="$(variant_field "$line" 3)"
args="$(variant_make_args "$line")"

echo "[package] === $key ==="
echo "[package] make clean && make $args"
Expand Down
7 changes: 5 additions & 2 deletions tools/package/verify_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
SKIP_REBUILD=1 skip check 1 (it costs four full builds)
SKIP_VICE=1 skip the VICE boots (keeps the c1541 byte-compare)
SKIP_LISTENER=1 skip check 3
VICE_BOOT_TIMEOUT seconds to wait for the menu (default 180)
VICE_BOOT_TIMEOUT seconds to wait for the banner (default 600: a loaded
host took 90 s for ip65-onchip and up to 150 s for uci)
"""
from __future__ import annotations

Expand Down Expand Up @@ -225,7 +226,7 @@ def check_d64_boots(variants: list[dict]) -> None:
except Exception as exc: # noqa: BLE001
record("c64_test_harness importable", False, f"{type(exc).__name__}: {exc}")
return
timeout = float(os.environ.get("VICE_BOOT_TIMEOUT", "240"))
timeout = float(os.environ.get("VICE_BOOT_TIMEOUT", "600"))
import time
expected = expected_d64_images(variants)
present = set(d64_images())
Expand Down Expand Up @@ -288,6 +289,8 @@ def check_d64_boots(variants: list[dict]) -> None:
detail = f"nothing recognisable on screen within {timeout:.0f}s"
if foreign:
detail += f"; UNEXPECTED foreign banner {foreign}"
if not ok:
detail += f" (boot budget {timeout:.0f}s; VICE_BOOT_TIMEOUT to change)"
record(f"{image.name} boots to the banner", ok, detail)


Expand Down
2 changes: 1 addition & 1 deletion tools/package/write_manifest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ grep '^failreason=' "$BUILD_INFO" | cut -d= -f2- | while read -r key reason; do
[ "$(variant_field "$line" 1)" = "$key" ] || continue
prg="$(variant_field "$line" 2)"
echo " $prg"
echo " make $(variant_field "$line" 3)"
echo " make $(variant_make_args "$line")"
done
[ -n "$prg" ] || echo " $key"
echo " $reason"
Expand Down
Loading