diff --git a/CLAUDE.md b/CLAUDE.md index 9bd6a9cf..e1bf329b 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -43,10 +43,14 @@ Fresh clone (ip65 backend; UCI needs none of the ip65 steps): - To test blob provenance, **flip a byte** — never move the blob aside. `make` regenerates it deterministically, reproducing the baseline hash, which reads exactly like a live trap. That false positive has been hit. - - **`libs/nistcurves` must be >= v0.11.2** (`CONTRACT_ZP_DEFINES`, - knob-staleness guard). A stale checkout is caught in ~0.05 s by a - source probe in `tools/integration/build_nistcurves_p256.sh` (#124); - `tools/check_upstream_pins.py --worktree` reports checkout-vs-gitlink. + - **`libs/nistcurves` must be >= v0.14.0** (`CONTRACT_ZP_DEFINES`, + knob-staleness guard, the §8.2 REU DMA settle, the #148 comb + fail-closed fix, and the §6.1 `zp_aliases*.o` split the wrapper's + bare-export guard is written against — full list in the + `CONTRACT_DEFINES` block of `tools/integration/build_nistcurves_p256.sh`). + A stale checkout is caught in ~0.05 s by a source probe in that same + script (#124); `tools/check_upstream_pins.py --worktree` reports + checkout-vs-gitlink. **A flag change no longer needs `make clean` (#159).** make tracks source mtimes, not the command line, so this used to be a discipline you only had @@ -134,7 +138,20 @@ Variables: the slot by 1,536 B. - `ENABLE_P384_VERIFY=1` — re-arms the P-384 verify arm. **Unsafe on its own** (overwrites live code — see Crypto ABI); exists so - `tools/test_p384_overlay_hazard.py` can be mutation-tested. + `tools/test_p384_overlay_hazard.py` can be mutation-tested. It is also + the **only** thing that links `src/crypto/ecdsa_verify_384.s` and + `src/crypto/p384_force_link.s` at all, so it is the one command that + keeps `ecdsa_verify_384.s` honest: + + make BACKEND=uci USE_NISTCURVES_ONCHIP=1 ENABLE_P384_VERIFY=1 + + **Assembling that file is worthless as coverage**: `.import` of a + symbol nothing defines is a LINK-time error in ca65, not an + assemble-time one, so `ca65 src/crypto/ecdsa_verify_384.s` succeeds + with every one of its imports unresolved. Only the armed link finds a + P-384 symbol that has gone away. The flag applies on all five profiles + since the hoist out of the `USE_NISTCURVES_ONCHIP` block — it used to + be silently dropped by the two REU-profile builds. - `HTTPS_HOST` / `HTTPS_PATH` / `HTTPS_SNI` / `HTTPS_PORT` / `HTTPS_BODY_TO_REU=1` — build-time target. Hosts >63 chars are a build error. The strings live in their own `HTTPS_TARGET_RODATA` segment @@ -172,7 +189,7 @@ fixed buffers in crypto BSS. ChaCha20-Poly1305 in-tree, permanent: `chacha20_encrypt`, `poly1305_init/update/final`, `aead_encrypt/decrypt` SHA-256 in-tree: `sha256_init/update/final` - ECDSA P-256 sibling `libs/nistcurves@v0.11.2`: + ECDSA P-256 sibling `libs/nistcurves@v0.14.0`: `ecdsa_verify_256`, `ec_scalar_mul_var` (plus the `ec_base_x`, `ec_gx256` data — that is the whole surface we import). Dispatcher: @@ -183,6 +200,22 @@ fixed buffers in crypto BSS. range + on-curve check); c64-https does none of its own, and `Q` comes straight from the attacker-supplied certificate, so `ecdsa_verify.s` carries `.assert LIB_NISTCURVES_VERSION_MINOR >= 9`. + - `LIB_NISTCURVES_ABI_VERSION` is **4** from the v0.14.0 pin and + `src/lib_contract_asserts.s` pins it. Two hops from the 2 that stood + at v0.11.2 — v0.13.0 gave `ec_scalar_mul` a carry return, v0.14.0 + fixed `reu_fetch_mul_row` to take the row index in A as §8.2 always + said — and NEITHER reaches our call surface: we never `jsr + ec_scalar_mul` into the library (`ecdsa_verify.s` defines its own + shim) and we never call the fetch. The counter moving is the gate + telling you to check, not a break. + - v0.13.0 (upstream #148, reported from here) made the Lim-Lee comb + fail closed on an unusable anchor-table slot; before it, + `ecdsa_verify` accepted the collapsed u1·G = O case. That is a real + forgery and `c64-https-uci-comb.prg` shipped on that path. Keep the + scope exact: it catches a **collapsed** anchor table, not a + **corrupt** one. Anyone who can write REU bank 2 plants a valid + point, reaches a forged R with Z != 0 throughout, and no + post-condition on the result sees it. - Zero page: fe25519 `$2C-$37`, x25519 `$38-$3A`, ECDSA bignum `$22-$3C`, time-shared; sibling slots `fp_mul_i=$39`, `fp_mul_j=$3A`, `nistcurves_zp_ptr2=$3D` (verify in `build/labels.txt`). All defined @@ -211,6 +244,19 @@ The curve comes from the certificate, so the gate is what makes the advertisement change safe. No P-384 build target has ever completed (see Known issues); fix the build before re-wiring. +The lane is now gated at the **link line** as well as in the source: +`ecdsa_verify_384.o` used to be in every shipped image and reachable from +none of them (ca65 emits no import record for an `.import` nothing +references, so the wildcard pulled it in), costing 299 B of +`CRYPTO_AUX_CODE` + 33 B of `CRYPTO_RODATA` everywhere. Both P-384-only +objects now link only under `ENABLE_P384_VERIFY=1`. Measured consequence: +exactly two symbols lose their only link-time consumer — +`crypto_swap_to_p384_sha384` and `crypto_swap_to_p384_curve`. They are +still exported unconditionally by `src/crypto/shared/crypto_swap.s` and +still occupy bytes; nothing imports them in any of the five unarmed +builds. That is the whole surface change, and it is measured from the +`Imports list` of all five `build/c64-https.map` files, not estimated. + Sibling-library memory requirements: code + rodata in `CRYPTO_HOT` / `CRYPTO_RESIDENT` ($6000-$9FFF), never crossing $A000 (boot zeroes $A000-$BFFF); big BSS in `CRYPTO_COLD_SHADOW`; segments named per contract @@ -262,10 +308,13 @@ drops a symbol fails the link by name on both backends. Surface: `net_dhcp` (alias), and `net_print_ip` — IP printing is consumer UI and is now `print_local_ip` in `boot.s`, one copy for both backends. - Byte accounting on ip65 (the tight one): LOADER went from 16 B free to - 58 B; `print_local_ip` rides LOADER_OVERFLOW, so the NET_CODE tail that - is `HTTPS_HOST`/`HTTPS_PATH`'s ip65 budget shrank from 170 to ~60 B - beyond the default strings (wikipedia's +46 B still builds on both ip65 - profiles; the theoretical 165 B host+path maximum no longer does). + 58 B **at the time of #142**; it is **21 B** at the v0.14.0 pin, which + is the number the Memory layout section carries and the one to use. + `print_local_ip` rides LOADER_OVERFLOW, so the NET_CODE tail that is + `HTTPS_HOST`/`HTTPS_PATH`'s ip65 budget shrank from 170 to ~60 B beyond + the default strings (56 B measured; wikipedia's +46 B still builds on + both ip65 profiles, with 14 B to spare — verified at this pin; the + theoretical 165 B host+path maximum no longer does). - `src/net/ip65/` — ip65/RR-Net (cs8900a). Blob loaded at $2000 via `.incbin`; `net.s` is the adapter; `ip65_symbols.inc` is the single @@ -380,8 +429,17 @@ engineering-notes. **Real servers work** (2026-08-21/22, U64E @ 48 MHz, comb): github.com, browserleaks.com, lwn.net all HTTP 200 (~32-39 s to Finished), and -en.wikipedia.org's C64 article — 125,235 B, chunked, into REU `$10:0000` -via `HTTPS_BODY_TO_REU=1`, byte-verified, shown by `src/viewer.s`. The +en.wikipedia.org's C64 article streamed into REU `$10:0000` via +`HTTPS_BODY_TO_REU=1` and shown by `src/viewer.s` — **handshake and GET +verified, body completeness NOT** (#211). This entry used to claim +125,235 B byte-verified; that does not reproduce on either tree. `http_get` +returns `carry=0`/`http_status=200` on bodies tens of KB short of their +`Content-Length`, intermittently and not as a function of size — measured +offline against a local listener with no chunking, and live at +117,192 / 89,526 / 73,720 B against a same-day 125,703 B anchor. It can also +hang outright. **Do not treat any large-body fetch as complete until #211 +closes**; it went unnoticed because the only rig on that path cannot go red +on a short body (#210). The handshake results above are unaffected. The local-listener handshake works on both backends (UCI at 48 MHz and 1 MHz; ip65 in VICE at honest 1 MHz, ~36 min). @@ -474,11 +532,26 @@ already refused a step later, as `DF_ERR_TYPE = $04`). Test: - **CRYPTO_OVERLAY vs rig scratch**: new resident tenants in `$4200-$5FFF` shrink what the rigs' `MemoryArbiter` can hand out (server-name validation took the comb tail from 714 to 223 B and broke - `rig_https_wiki.py`, which now drives the menu instead). The harness - write guard raises `MemoryPolicyError` before the wire. - - `CRYPTO_HOT` margin under UCI is **81 B** at v0.9.1+ and was one byte at - v0.6.0; 288 B of that was a one-off dead-data recovery upstream. Watch - it on every pin bump. + `rig_https_wiki.py`, which now drives the menu instead). Those are the + numbers from that episode; the comb tail is **153 B** at the v0.14.0 + pin — see the memory map below, and never size rig scratch from this + bullet's historical figures. The harness write guard raises + `MemoryPolicyError` before the wire. + - `CRYPTO_HOT` margin under UCI is **per profile, and the one number this + file used to carry (81 B) was wrong by more than half.** Measured at the + v0.14.0 pin **with the P-384 objects gated out**: **203 B** uci-onchip, + **93 B** uci-comb, 166 B on the unshipped REU default. At v0.11.2 it was + 36 / 193 / 23 — measured on master at `48657f5`, which is where to check + it: the v0.11.2 pin is no longer reachable from this branch (it fails the + branch's own ABI assert). So the 81 B was already stale before this bump + (#193). + The onchip figure grew for two independent reasons: + `LIB_NISTCURVES_P256_RODATA` moved to `CRYPTO_OVERLAY` in + `cfg/c64-https-uci.cfg` to absorb v0.12.0's +58 B of settle call sites, + and gating returned 33 B of `CRYPTO_RODATA`. Comb is the exception at + 93 B because its cfg already routes `CRYPTO_RODATA` to `CRYPTO_OVERLAY`, + so its 33 B came back there instead — see the memory map below. + Watch it on every pin bump, and measure all three. - `http_recv_response`: `Content-Length` (single-SP matcher, 16-bit sentinel `$FFFF`) and chunked (`http_state_body_chunked`, `HTTP_AUX_CODE`; chunks >64 KB desync) supported; body rendered via @@ -515,7 +588,7 @@ Model: `T(f) = D + C/f`. The REU profile has a ~42-56 s floor (row-fetch DMA anchored to the ~1 MHz bus, ~16 KB per `fp_mul`); onchip has none but ~1.9x the CPU work; comb halves the CPU work again but needs REU bank 2 and a boot precompute. **Read the pin, not the commit** — almost every -figure was taken at `libs/nistcurves` v0.6.0; the pin is v0.11.2 and the +figure was taken at `libs/nistcurves` v0.6.0; the pin is v0.14.0 and the only re-measured points are 48 MHz UCI REU (80.8 → 82.1 → 82.4 s, n=1; the +1.6% is v0.7.0's public-key validation, worth paying) and comb. @@ -607,7 +680,13 @@ UCI (`cfg/c64-https-uci.cfg`, W1 hot/cold split — the reference): build: TLS_DEFRAME_CODE (~1.4 KB), CERT_BUF_BSS (2,048 B), HTTPS_TARGET_RODATA, x509_name; comb adds RODATA/LIMLEE_BSS - (~223 B tail free). Also the slot for the + (**153 B** tail free measured at the + v0.14.0 pin with the P-384 objects gated + out; it was 120 B before gating returned + 33 B of CRYPTO_RODATA here, and the + ~223 B this file used to claim was stale + — it was 159 B at v0.11.2). Also the slot + for the (broken) overlay-embed flags. $6000-$9FFF CRYPTO_HOT resident code + rodata + small BSS $A000-$BFFF CRYPTO_COLD_SHADOW large BSS (RAM under BASIC ROM, $01=$36); @@ -625,7 +704,8 @@ ip65 (`cfg/c64-https-ip65.cfg`): $2000-$3FFF NET_CODE ip65 blob + LOADER_OVERFLOW + CRYPTO_AUX_CODE2 + HTTPS_TARGET_RODATA $4000-$4F8B NET_BSS blob's BSS (live at runtime, EMPTY to ld65) - $4F8C-$5FFF CRYPTO_OVERLAY 4,212 B: TLS_CODE + CRYPTO_AUX_CODE + $4F8C-$5FFF CRYPTO_OVERLAY 4,212 B: TLS_CODE + CRYPTO_AUX_CODE + + HTTP_AUX_CODE + HTTP_AUX_CODE2 $6000-$9FFF CRYPTO_RESIDENT code + rodata, never crossing $A000 $A000-$BFFF CRYPTO_COLD_SHADOW BSS; `cert_buf` (1,536 B) pinned at $A000 and unioned with LIB_NISTCURVES_P256_BSS @@ -633,9 +713,34 @@ ip65 (`cfg/c64-https-ip65.cfg`): capped so growth is a link error) $C000-$CFFF TCP_BUF 4 KB ring for the ip65 callback -ip65 is essentially full: largest free block ~170-186 B (NET_CODE tail), -40 B CRYPTO_RESIDENT, 22 B CRYPTO_OVERLAY, 16 B LOADER. PRG size is not a -headroom gauge. A contiguous-region cfg restructure is a known TODO. +**Maintenance hazard for every margin figure below, and in the UCI section +above: they are PER PROFILE, and the recurring failure is that a figure gets +corrected in the profile someone was looking at and left stale in the other +four.** Three instances in the v0.14.0 bump alone, two of which left this +file contradicting itself hundreds of lines apart. When you change a number +here, re-measure it in all five builds off `build/c64-https.map` rather than +deriving the others by arithmetic — the deltas are not uniform, because a +segment recovered in one profile can land in a different region in another +(comb's `CRYPTO_RODATA` is the worked example: see the 33 B note above). + +ip65 is essentially full. Measured at the v0.14.0 pin, with the P-384 +objects gated out of the link: 56 B NET_CODE tail (on top of the 20 B the +default target strings already use), 145 B CRYPTO_RESIDENT, 152 B +CRYPTO_OVERLAY, 21 B LOADER, and a 43 B hole below TABLES_BSS in +CRYPTO_COLD_SHADOW. PRG size is not a headroom gauge. **CRYPTO_OVERLAY and +CRYPTO_RESIDENT are ADJACENT ($4F8C-$5FFF and $6000-$9FFF), so they are one +pool of 20,596 B, and no amount of shuffling segments between them creates +space** — that is why the contiguous-region cfg restructure is only worth +the boundary fragmentation it recovers. That property is what settled the +v0.14.0 bump: pre-gating content was 20,631 B against 20,596 B of capacity, +35 B short *however* the segments were arranged, so ip65 could not be made +to link by moving anything. Gating the unreachable `ecdsa_verify_384.o` out +(332 B) is what created real space; `HTTP_AUX_CODE2` moving to +CRYPTO_OVERLAY only rebalances the two halves of the pool, and does it to +match what both UCI cfgs already do. The other free blocks are not +reachable from the pool: NET_CODE's tail is the `HTTPS_HOST`/`HTTPS_PATH` +budget, and the smallest segment in the pool is `LIB_NISTCURVES_MUL_CODE` +at 162 B (not `HTTP_AUX_CODE2`'s 169 B, which this file used to name). - `LOADER_OVERFLOW` carries ~125 B of `http.s` that outgrew LOADER. - `src/loadaddr.s` (PRG load address) and `src/exports.s` (promotes @@ -659,7 +764,13 @@ between each, matrix in `tools/package/_common.sh`: c64-https-uci-comb.prg turbo + REU, fastest (needs bank 2 + boot precompute) REU-profile images were retired (curation: still fastest below ~18 MHz, -one line in `PACKAGE_VARIANTS` to restore). One product per .d64. The +one line in `PACKAGE_VARIANTS` to restore). **That line is a +security-relevant decision, not just a curation one.** Measured +`LIB_NISTCURVES_REU_BANKS_USED`: `$0000` for both shipped onchip products, +`$0004` for uci-comb, and **`$0003` for the two REU profiles** — they do +library REU DMA, so restoring one restores an image exposed to the +SPEC §8.2 REU DMA settle fix (libs/nistcurves v0.12.0). Check the pin +covers it before adding the line back. One product per .d64. The listener `c64-https-listener.py` is a single self-extracting file with no third-party deps; it needs an `ssl` with TLS 1.3 (macOS `/usr/bin/python3` is LibreSSL and cannot serve this client) and `--selftest` proves the path diff --git a/Makefile b/Makefile index 52d1ff1d..191a7ae3 100644 --- a/Makefile +++ b/Makefile @@ -233,23 +233,6 @@ $(error USE_NISTCURVES_ONCHIP places LIB_NISTCURVES_MUL_CODE in CRYPTO_OVERLAY - endif CA65FLAGS += -D USE_NISTCURVES_ONCHIP=1 -# ENABLE_P384_VERIFY=1 — re-arms the P-384 verify arm in -# src/crypto/ecdsa_verify.s. OFF BY DEFAULT AND UNSAFE ON ITS OWN. -# -# The arm calls crypto_swap_to_p384_sha384, which DMAs $1E00 bytes from REU -# banks that only reu_p384_overlay_init writes — and that routine is itself -# `.ifdef USE_OVERLAY_P384_EMBED`, which no shipped build sets and which -# cannot currently be built at all (the SHA-384 table overflows OVERLAY_REGION -# by 1,536 B; see "Known issues"). Setting this flag WITHOUT a working overlay -# reproduces the c64-https#128-era hazard on purpose: live resident code -# overwritten, machine hung. -# -# It exists so the guard can be mutation-tested — a safety gate nobody can -# switch off is a gate nobody can prove is load-bearing. Turning it on is -# expected to make tools/test_p384_overlay_hazard.py FAIL. -ifeq ($(ENABLE_P384_VERIFY),1) -CA65FLAGS += -D ENABLE_P384_VERIFY=1 -endif ifeq ($(USE_NISTCURVES_ONCHIP_COMB),1) SIBLING_LIB_ARCHIVES := build/lib/nistcurves-p256-onchip-comb.a CA65FLAGS += -D USE_NISTCURVES_COMB=1 @@ -346,6 +329,71 @@ else CRYPTO_SRCS_EFFECTIVE := $(CRYPTO_SRCS_ALL) endif +# ENABLE_P384_VERIFY=1 — re-arms the P-384 verify arm in +# src/crypto/ecdsa_verify.s. OFF BY DEFAULT AND UNSAFE ON ITS OWN. +# +# The arm calls crypto_swap_to_p384_sha384, which DMAs $1E00 bytes from REU +# banks that only reu_p384_overlay_init writes — and that routine is itself +# `.ifdef USE_OVERLAY_P384_EMBED`, which no shipped build sets and which +# cannot currently be built at all (the SHA-384 table overflows OVERLAY_REGION +# by 1,536 B; see "Known issues"). Setting this flag WITHOUT a working overlay +# reproduces the c64-https#128-era hazard on purpose: live resident code +# overwritten, machine hung. +# +# It exists so the guard can be mutation-tested — a safety gate nobody can +# switch off is a gate nobody can prove is load-bearing. Turning it on is +# expected to make tools/test_p384_overlay_hazard.py FAIL. +# +# TOP LEVEL ON PURPOSE. This block used to sit inside +# `ifeq ($(USE_NISTCURVES_ONCHIP),1)`, so the two REU-profile builds — plain +# `make` and `make BACKEND=uci` — dropped the -D silently and the flag did +# nothing at all. Measured at the previous commit, PRG sha256, ip65 REU +# profile: +# +# make BACKEND=ip65 d780f719fac82e07… +# make BACKEND=ip65 ENABLE_P384_VERIFY=1 d780f719fac82e07… <- identical +# +# and the same for BACKEND=uci (7c79a4e00a19685d… both ways). The mutation +# test that proves this gate is load-bearing was a no-op on two of the five +# configurations. The three onchip profiles were unaffected and their armed +# PRGs are byte-identical across this move. +# +# The two P-384-only objects are gated on the SAME flag, so the lane is one +# switch and not two half-switches: +# +# src/crypto/ecdsa_verify_384.s the TLS-side P-384 dispatcher. Its only +# export is ecdsa_verify_384_tls, and the +# only `.import` of that symbol anywhere is +# the one inside ecdsa_verify.s's +# `.ifdef ENABLE_P384_VERIFY`. ca65 emits no +# import record for an .import nothing +# references, so with the flag off the object +# was linked and UNREACHABLE: it appears in +# no Imports-list entry of any of the five +# build/c64-https.map files, and od65 +# --dump-imports on ecdsa_verify.o does not +# list it. It cost 299 B of CRYPTO_AUX_CODE + +# 33 B of CRYPTO_RODATA + 2 B of BSS in every +# shipped image. +# src/crypto/p384_force_link.s already inert: its entire body is +# `.ifdef USE_NISTCURVES_P384`, and nothing +# defines that symbol today (the CA65FLAGS +# line for it is commented out below). Zero +# bytes either way; gated here so the lane's +# file set lives in one place. +# +# What this does NOT do: it does not delete the P-384 lane. Both files stay in +# tree, as does every other piece of P-384 plumbing, on the same policy the +# retired EMBED_P256_OVERLAY follows. `ENABLE_P384_VERIFY=1` restores the +# previous link line; the three onchip profiles' armed PRGs are byte-identical +# to the pre-gating ones (b9f03169…, 3c271262…, cc555c19…). +P384_SRCS := src/crypto/ecdsa_verify_384.s src/crypto/p384_force_link.s +ifeq ($(ENABLE_P384_VERIFY),1) +CA65FLAGS += -D ENABLE_P384_VERIFY=1 +else +CRYPTO_SRCS_EFFECTIVE := $(filter-out $(P384_SRCS),$(CRYPTO_SRCS_EFFECTIVE)) +endif + # Per-backend source + object selection. ifeq ($(BACKEND),ip65) NET_SRCS := $(IP65_SRCS) diff --git a/README.md b/README.md index 475118cb..c053f797 100644 --- a/README.md +++ b/README.md @@ -9,11 +9,14 @@ An HTTPS client for the Commodore 64 in 6502 assembly. Implements TLS 1.3 over T > **Real-server milestone (2026-08-21/22).** The client now completes TLS 1.3 > handshakes and HTTP GETs against real public servers on the open internet — -> **github.com, browserleaks.com and lwn.net** all return HTTP 200 — and, as a -> demonstration, fetches the full 125 KB **Wikipedia article about the -> Commodore 64** over TLS into a 16 MB REU and scrolls it on the C64's own -> screen. Requires the UCI backend at turbo (comb profile). See the Project -> Status section and the "End-to-end HTTPS status" notes in `CLAUDE.md`. +> **github.com, browserleaks.com and lwn.net** all return HTTP 200. It also +> streams the **Wikipedia article about the Commodore 64** over TLS into a +> 16 MB REU and scrolls it on the C64's own screen — but **that body is not +> verified**: `http_get` intermittently reports success on a truncated body +> (issue #211), so treat the large-body demo as a demo. The handshake result +> is unaffected. Requires the UCI backend at turbo (comb profile). See the +> Project Status section and the "End-to-end HTTPS status" notes in +> `CLAUDE.md`. ## I just want to run it @@ -300,7 +303,11 @@ $4000-$4F8B NET_BSS ip65 blob's BSS (live at runtime; ld65 sees it as EMPTY, so do not read it as headroom) $4F8C-$5FFF CRYPTO_OVERLAY 4,212 B; holds TLS_CODE + CRYPTO_AUX_CODE - + HTTP_AUX_CODE + + HTTP_AUX_CODE + HTTP_AUX_CODE2 (that + last one rode the CRYPTO_RESIDENT tail + until the v0.14.0 bump moved it here in + cfg/c64-https-ip65.cfg — check the cfg, + not this line, on any other tree) $6000-$9FFF CRYPTO_RESIDENT 16 KB code + rodata, incl. the libs/nistcurves P-256 verify path $A000-$BFFF CRYPTO_COLD_SHADOW 8 KB BSS under the BASIC ROM shadow @@ -325,7 +332,8 @@ $4200-$5FFF CRYPTO_OVERLAY 7,680 B, and NOT an empty overlay slot: CERT_BUF_BSS (2,048 B), X509_NAME_CODE, HTTP_SINK_CODE, HTTP_AUX_CODE2, HTTPS_TARGET_RODATA, the nistcurves - mul code, plus VIEWER_CODE (onchip) or + mul code and (from the v0.14.0 pin) its + P-256 rodata, plus VIEWER_CODE (onchip) or the comb rodata + LIMLEE_BSS (comb) $6000-$9FFF CRYPTO_HOT 16 KB code + rodata + UCI_BSS $A000-$BFFF CRYPTO_COLD_SHADOW 8 KB BSS (same tenants as ip65, less @@ -341,21 +349,42 @@ $E000-$FDFF OVERLAY_BLOB_CURVE_RAM P-384 curve overlay blob slot (empty An earlier version of this section called `CRYPTO_OVERLAY` "empty in the shipped build". That was wrong by about two orders of magnitude, and it is the kind of error that invites someone to put 2 KB there. The measured -free space in every region of every shipped product, tail of the last -segment to the end of the region: +free space in every region of every shipped product — first free address to +the end of the region — re-measured off `build/c64-https.map` at the +`libs/nistcurves` **v0.14.0** pin, with the unreachable P-384 objects gated +out of the link: | region | `ip65-onchip` | `uci-onchip` | `uci-comb` | |---|---:|---:|---:| | `LOADER` | 21 B | 148 B | 122 B | -| `NET_CODE` | 56 B | 67 B | 67 B | +| `NET_CODE` | 56 B | 366 B | 366 B | | `NET_BSS_TAIL` | — | 43 B | 43 B | -| `CRYPTO_OVERLAY` | 22 B | 2,035 B | **159 B** | -| `CRYPTO_RESIDENT` / `CRYPTO_HOT` | 40 B | 36 B | 193 B | -| `CRYPTO_COLD_SHADOW` (gap below the `$BA00` `TABLES_BSS` pin) | 44 B | 1,578 B | 1,578 B | - -So ip65 is genuinely full — its largest block is the 56 B `NET_CODE` tail, -which is also the budget a longer `HTTPS_HOST`/`HTTPS_PATH` eats into — and -`uci-comb` has 159 B of overlay tail, not the kilobytes the old text implied. +| `CRYPTO_OVERLAY` | 152 B | 1,804 B | **153 B** | +| `CRYPTO_RESIDENT` / `CRYPTO_HOT` | 145 B | 203 B | 93 B | +| `CRYPTO_COLD_SHADOW` (gap below the `$BA00` `TABLES_BSS` pin) | 43 B | 1,577 B | 1,577 B | + +Where a cell moved it moved for its own reason, and the reasons do not +generalise across profiles — **read them per profile, and re-measure all three +when you change one.** The UCI `NET_CODE` gain is the +299 B of `CRYPTO_AUX_CODE` that `ecdsa_verify_384.o` occupied in every image +while being reachable from none; `uci-onchip`'s overlay lost ground because +`LIB_NISTCURVES_P256_RODATA` moved there to absorb the v0.12.0 REU-settle +call sites, while `uci-comb`'s `CRYPTO_HOT` fell to 93 B because its cfg +already routed `CRYPTO_RODATA` to the overlay, so its share of the recovery +landed there instead. Four cells did not move at all (`LOADER` and `NET_CODE` +on ip65, `LOADER` on `uci-onchip`, both `NET_BSS_TAIL` cells), which is its own +warning: an unchanged number here is not evidence that a region was left alone. +The single byte off each `CRYPTO_COLD_SHADOW` cell is real segment growth, not +a re-count of the old one — v0.14.0 adds a 3 B `LIB_NISTCURVES_BSS` while `BSS` +shrinks by 2, which walks `CRYPTO_BSS`'s last address from `$B9D3` to `$B9D4` +on ip65 and from `$B3D5` to `$B3D6` under UCI. + +So ip65 is still genuinely full — 152 B is its largest block now, and the +56 B `NET_CODE` tail is still the budget a longer `HTTPS_HOST`/`HTTPS_PATH` +eats into. Note that ip65's `CRYPTO_OVERLAY` and `CRYPTO_RESIDENT` are +**adjacent** ($4F8C-$5FFF and $6000-$9FFF), so they are one 20,596 B pool and +moving segments between them creates nothing; the 332 B this bump recovered by +gating P-384 out of the link is what made the v0.14.0 pin fit at all. `uci-onchip` is the roomy one, and only because it ships neither the comb tables nor the comb rodata. @@ -398,6 +427,14 @@ profile, which was retired from the release lineup; every shipped image passes under Memory Map, and `PACKAGE_VARIANTS` in `tools/package/_common.sh`, which is the single source of truth for the matrix. +Restoring a REU-profile image is one line in `PACKAGE_VARIANTS`, and it is a +**security-relevant** line rather than a curation one. Measured +`LIB_NISTCURVES_REU_BANKS_USED`: `$0000` for both shipped onchip products — +they share one archive and do no library REU DMA — `$0004` for `uci-comb`, and +`$0003` for the two unshipped REU profiles. Only images with a non-zero value +are exposed to the defects a `libs/nistcurves` bump fixes in that path, so +check the pin covers them before adding the line back. + The knobs, all read straight from the Makefile: | variable | effect | @@ -475,15 +512,15 @@ it is supposed to detect. ## Project Status -Current status of the three shipped products (measured 2026-09-05 with cc65 -from Homebrew; `make clean` before each, then `stat` the PRG and +Current status of the three shipped products (re-measured at the +`libs/nistcurves` v0.14.0 pin with cc65 from Homebrew; `make clean` before each, then `stat` the PRG and `grep -c '^al ' build/labels.txt`): | product | PRG | labels | |---|---:|---:| -| `c64-https-ip65-onchip` | 47,105 B | 2,398 | -| `c64-https-uci-onchip` | 62,977 B | 2,729 | -| `c64-https-uci-comb` | 62,977 B | 2,864 | +| `c64-https-ip65-onchip` | 47,105 B | 2,395 | +| `c64-https-uci-onchip` | 62,977 B | 2,726 | +| `c64-https-uci-comb` | 62,977 B | 2,859 | The unshipped default profiles land on the same two PRG sizes (47,105 B / 62,977 B) with fewer labels, which is the point: **the PRG size is not a @@ -516,15 +553,15 @@ Progress: - ip65 on **real RR-Net silicon — a first, 2026-09-05.** An RR-Net (CS8900a) cartridge in a cartridge port, on its own wired 10.0.66.0/24 segment, ran the whole path at **stock 1 MHz with no turbo and no REU**: DHCP (the C64 took the pinned lease, read back from its own `net_local_ip` rather than from the server's lease file), a DNS lookup over the cartridge, a 141-byte ClientHello with the correct SNI on the cable, application data both ways, and **HTTP 200** with the body byte-checked out of `http_resp_buf` and `net_last_error` `$00`. Every check the rig ran passed. They are not all wire checks: the wire assertions that attribute traffic to the cartridge discriminate it by Ethernet source address, the rest read the C64's own memory back over DMA or assert host-side preconditions, and `tests/rig_ip65_rrnet_hw.py` says which is which. **1,979 s (33.0 min)** from `G` to done, n=1 on both sides — faster than the same profile in VICE at honest 1 MHz, and by **at least** the ~8% the raw figures show, because the VICE number predates the current `libs/nistcurves` pin and the newer pin is slower (the first Known Issues bullet below carries both that figure and the pin caveat). Our first measurement of the real cartridge port. The "body never appears on the wire in clear" check passed *conclusively*, because its positive control — the SNI, which genuinely is in the clear in the ClientHello — was found, making that a claim about the wire rather than about the searcher. **Scope:** this is `ip65-onchip` against the bundled local listener. It does not exercise ip65 against a real internet server, it says nothing about the two UCI images, and it does not touch the chain-validation or name-validation caveats above — `ip65-onchip` still does neither. - The same cartridge at **48 MHz turbo, 2026-09-06.** `TURBO_MHZ=48` on the same rig, the same build configuration and the same `G`-to-`CONNECTION CLOSED` boundary: **43.1 s, a 46x speed-up** over the 1 MHz run above. That comparison is like-for-like — same rig, same cartridge, real silicon on both sides — and is a different kind of claim from the VICE comparison in the bullet above, which crosses a `libs/nistcurves` pin and therefore stays a lower bound. The open risk was CS8900a register timing at turbo, which is the reason the rig defaults to stock speed, and it did not materialise: DHCP completed on the automatic attempt with no retries, because ip65's ~15 s budget rides CIA2 timer B and the CIA timers run at real phi2 at every CPU clock (measured, with the negative control that separates that from a CPU still at 1 MHz, in `docs/engineering-notes.md`; reproducer `tools/probe_cia_timer_rate.py`). **24 of 25 checks passed, and the one that failed has to be read before this run is quoted.** `check_tls_connected` samples `tls_state` over DMA, and at 48 MHz the window in which that value exists — between the traffic-key derivation and `tls_close` — fits inside a single poll. So the handshake's completion at turbo is **inference from converging evidence** (HTTP 200 and the exact body out of the C64's own buffer, `TLS HANDSHAKE OK` on screen, application data both ways on the cable) **rather than the direct observation the 1 MHz run had.** It is the weaker of the two results and the check was left red rather than softened; `tests/rig_ip65_rrnet_hw.py` says why, and issue #204 tracks the fix. **Nothing else widens:** still one device, one cartridge, one local listener, the same port-only deviation from the shipped image, and still no server-name validation on ip65. This adds a second clock and no more. - [x] **Real public-internet HTTPS (UCI/comb, turbo)** — github.com, browserleaks.com and lwn.net all return `http_status=200` on real U64E hardware at 48 MHz. Needed three pieces of work over the local-listener path: a streaming handshake-message deframer (`src/tls_deframe.s`) for flights where handshake messages don't align with TLS records; a 2048 B `cert_buf` under UCI so larger real leaves fit; and — the last blocker — clamping the UCI adapter's `SOCKET_READ` request to the receive ring's free space, without which any flight over ~4 KB lost its tail to a ring-wrap drop. Build-time target is `make HTTPS_HOST=` / `HTTPS_PATH=`; the rig is `tools/uci/rig_https_live.py`. -- [x] **Wikipedia article into REU + on-screen viewer (stretch goal, UCI/comb)** — `make HTTPS_HOST=en.wikipedia.org HTTPS_PATH='/w/index.php?title=Commodore_64&action=raw' HTTPS_BODY_TO_REU=1` streams the 125,235 B article body into REU bank 16 (`$10:0000`) and drops into a scroll viewer (`src/viewer.s`, CRSR/SPACE/F1/HOME/Q). Verified on U64E @ 48 MHz, body byte-checked against a host-side reference fetch. Rig: `tools/uci/rig_https_wiki.py`. +- [x] **Wikipedia article into REU + on-screen viewer (stretch goal, UCI/comb) — demonstrated, body NOT verified.** `make HTTPS_HOST=en.wikipedia.org HTTPS_PATH='/w/index.php?title=Commodore_64&action=raw' HTTPS_BODY_TO_REU=1` streams the article body into REU bank 16 (`$10:0000`) and drops into a scroll viewer (`src/viewer.s`, CRSR/SPACE/F1/HOME/Q); the handshake, the GET and the viewer all work on a U64E @ 48 MHz. **What does not hold is the byte count.** This entry used to claim 125,235 B byte-checked against a host-side reference fetch; that does not reproduce on either tree. `http_get` returns `carry=0` with `http_status=200` on bodies tens of kilobytes short of their `Content-Length`, intermittently and not as a function of size — reproduced offline against a local listener with no chunking involved, and seen live at 117,192 / 89,526 / 73,720 B against a same-day 125,703 B `curl` anchor. Those are device runs of three different images and #211 names the PRG sha256 of each, which is what identifies them — a commit stops identifying an image the moment a docs commit lands on top of it. That is **issue #211**, and until it is closed no large-body fetch on any image should be treated as complete. It survived because the one rig on that path could not go red on a short body (**issue #210**). The defect is in the body path only: the handshake result above is unaffected, and nothing here changes what the client does or does not authenticate. **ip65 has never been tested against a body large enough to show this** — at any size, in fact, beyond the local listener's short one. Rig: `tools/uci/rig_https_wiki.py`. ### Known Issues -- **The handshake is slow, and the ECDSA P-256 verify dominates it.** Every figure here is quoted from the measurement record in `CLAUDE.md`. Except where noted they were taken at the **`libs/nistcurves` v0.6.0 pin**, and the pin is now v0.11.2, so treat them as a baseline rather than as current. The one profile re-measured at the current pin is comb: 46.986 / 24.440 / 16.402 s verify at 16 / 32 / 48 MHz (U64E, n=3, VIC blanking active). End-to-end handshake + GET against the local listener, U64E, master 2ceb5b1: **80.8 s** (REU profile, 48 MHz), **45.5 s** (onchip profile, 48 MHz), **1,157.7 s** (REU, stock 1 MHz). One point of that sweep has been carried forward: 48 MHz REU measures **82.1 s** at v0.9.1 and **82.4 s** at v0.10.1 (n=1 each, so the 0.4% step between them is noise; the 1.6% from v0.6.0 is the FIPS 186-5 public-key validation gate v0.7.0 added). No other clock or profile has been re-measured. On the REU-less stock-C64 path (ip65 + onchip, no REU, honest 1 MHz in VICE) the whole run measured **2,159.7 s = 36.0 min**, of which the verify stretch alone was 1,416.7 s. That is fine for the local listener, which holds the connection open; it exceeds a typical 10-30 s real-world server handshake window. -- **P-384 is parked, and doubly gated — it is not merely "stubbed".** An earlier version of this entry said the dispatcher "advertises `ecdsa_secp384r1_sha384` (0x0503)". It does not, and has not since v0.4.1: `sig_algs_ext_data` in `src/tls_handshake.s` carries exactly one scheme, `ecdsa_secp256r1_sha256` (0x0403), and `src/crypto/ecdsa_verify.s` compiles its P-384 arm to a `sec` reject unless `ENABLE_P384_VERIFY=1`. Both gates matter, because the curve comes from the certificate rather than from what we advertised — that combination is what closed the v0.4.0 hang in which a P-384 certificate made the overlay swap DMA over live resident code. Separately, **no P-384 build target has ever completed**: `make p384-overlay` from a clean tree stops at `No rule to make target 'build/labels.txt'`, and once a main build has produced that file it stops at `Segment 'LIB_NISTCURVES_SHA384_TABLES' overflows memory area 'OVERLAY_REGION' by 1536 bytes`. Certificates requiring P-384 are rejected, not verified. +- **The handshake is slow, and the ECDSA P-256 verify dominates it.** Every figure here is quoted from the measurement record in `CLAUDE.md`. Except where noted they were taken at the **`libs/nistcurves` v0.6.0 pin**, and the pin is now v0.14.0, so treat them as a baseline rather than as current. The one profile re-measured since is comb, at the v0.11.2 pin: 46.986 / 24.440 / 16.402 s verify at 16 / 32 / 48 MHz (U64E, n=3, VIC blanking active). End-to-end handshake + GET against the local listener, U64E, master 2ceb5b1: **80.8 s** (REU profile, 48 MHz), **45.5 s** (onchip profile, 48 MHz), **1,157.7 s** (REU, stock 1 MHz). One point of that sweep has been carried forward: 48 MHz REU measures **82.1 s** at v0.9.1 and **82.4 s** at v0.10.1 (n=1 each, so the 0.4% step between them is noise; the 1.6% from v0.6.0 is the FIPS 186-5 public-key validation gate v0.7.0 added). No other clock or profile has been re-measured. On the REU-less stock-C64 path (ip65 + onchip, no REU, honest 1 MHz in VICE) the whole run measured **2,159.7 s = 36.0 min**, of which the verify stretch alone was 1,416.7 s. That is fine for the local listener, which holds the connection open; it exceeds a typical 10-30 s real-world server handshake window. +- **P-384 is parked, and doubly gated — it is not merely "stubbed".** An earlier version of this entry said the dispatcher "advertises `ecdsa_secp384r1_sha384` (0x0503)". It does not, and has not since v0.4.1: `sig_algs_ext_data` in `src/tls_handshake.s` carries exactly one scheme, `ecdsa_secp256r1_sha256` (0x0403), and `src/crypto/ecdsa_verify.s` compiles its P-384 arm to a `sec` reject unless `ENABLE_P384_VERIFY=1`. Both gates matter, because the curve comes from the certificate rather than from what we advertised — that combination is what closed the v0.4.0 hang in which a P-384 certificate made the overlay swap DMA over live resident code. Separately, **no P-384 build target has ever completed**: `make p384-overlay` from a clean tree stops at `No rule to make target 'build/labels.txt'`, and once a main build has produced that file it stops at `Segment 'LIB_NISTCURVES_SHA384_TABLES' overflows memory area 'OVERLAY_REGION' by 1536 bytes`. Certificates requiring P-384 are rejected, not verified. From the v0.14.0 pin the lane is gated at the **link line** as well: `src/crypto/ecdsa_verify_384.s` and `src/crypto/p384_force_link.s` link only under `ENABLE_P384_VERIFY=1`. They used to be in every shipped image and reachable from none of it — ca65 emits no import record for an `.import` nothing references, so the wildcard pulled them in — at a cost of 332 B (299 B of `CRYPTO_AUX_CODE` + 33 B of `CRYPTO_RODATA`), which is the space that made the v0.14.0 bump fit on ip65 at all. `ENABLE_P384_VERIFY=1` now also applies on all five profiles: it used to be silently dropped by the two REU-profile builds, which is issue #207 — the mutation control that was supposed to fail under it passed vacuously there. - **`USE_X25519_SIBLING=1` links under neither backend, and each backend dies on a different segment.** The duplicate-symbol failure this entry used to record — `ld65: Error: Duplicate external identifier: 'reu_mul_tables_init'`, on **both** backends — was closed by the `libs/nistcurves` v0.10.1 / `libs/x25519` v0.11.0 bump plus a build-time deferral: `tools/integration/build_nistcurves_p256.sh` passes `-D SHARED_REU_MUL_INIT -D SHARED_REU_MUL_FETCH` through `CONTRACT_DEFINES`, so the library gates out its own copy of the SPEC §8.2 `reu_mul` provider that `src/boot.s` supplies itself. (This entry used to describe an archive-surgery workaround — dropping `reu_mul_init.o`. That is gone: the wrapper `cp`s the upstream archive unmodified, which is what §6.1 requires.) What replaced it is a placement problem, not a symbol collision. Measured at the `libs/x25519` **v0.16.0** pin and identical at v0.13.0, `make clean && make ... USE_X25519_SIBLING=1` stops with `Segment 'X25519_BSS' overflows memory area 'CRYPTO_OVERLAY' by 1536 bytes` under UCI and `Segment 'X25519_RODATA' overflows memory area 'CRYPTO_OVERLAY' by 3840 bytes` under ip65 (ip65's overlay slot is 4,212 B against UCI's 7,680 B). **An earlier version of this entry claimed the UCI link succeeded, and quoted 3,584 B for ip65. Both were wrong** — the UCI link has not produced a PRG, and 3,584 was a figure from a tree in which `HTTP_AUX_CODE` was not yet a `CRYPTO_OVERLAY` tenant. **Neither number is a total deficit, either:** ld65 warns once per memory area, at the first segment that tips it over, so everything placed after that segment is uncounted. ip65's real shortfall is **5,376 B**. The derivation, and the wrong explanation that fit the evidence first, are in `docs/engineering-notes.md` under "`USE_X25519_SIBLING=1` overflow — two relink experiments". The flag remains **off by default** and no shipped artifact contains the sibling; the in-tree X25519 in `src/crypto/{x25519,fe25519}.s` is what every release PRG is built from. Flipping the default is not a decision anyone can take on byte grounds today. - **Real-server reach is UCI/comb + turbo only, and has size limits.** The public-internet HTTPS above works on the comb profile at turbo; the stock-C64 ip65 path is far too slow for a real server's connection window (~36 min/handshake). Among real leaves, en.wikipedia.org's 1636 B leaf needs the 2048 B UCI `cert_buf` (fits); anything larger, or a server that ignores `max_fragment_length` and sends >548 B records (e.g. Cloudflare), is out of scope. Cloudflare additionally enforces a ~15 s connect-to-first-request deadline the C64 cannot meet and is deliberately unsupported. -- **The wikipedia stall was a client bug, now fixed.** Historical note for anyone bisecting: TLS flights larger than the ~4 KB UCI receive ring used to stall permanently, because `net_poll` requested a fixed 512 B and its fill loop dropped bytes past the ring's current free space (discarded as "delivered"). Fixed by clamping the `SOCKET_READ` request to ring free space (`src/net/uci/net.s`). It was never a firmware bug; github/browserleaks/lwn flights are under 4 KB and were unaffected. +- **The wikipedia stall was a client bug, now fixed.** Historical note for anyone bisecting: TLS flights larger than the ~4 KB UCI receive ring used to stall permanently, because `net_poll` requested a fixed 512 B and its fill loop dropped bytes past the ring's current free space (discarded as "delivered"). Fixed by clamping the `SOCKET_READ` request to ring free space (`src/net/uci/net.s`). It was never a firmware bug; github/browserleaks/lwn flights are under 4 KB and were unaffected. **That fix is not the end of the large-body story** — the body path still terminates early and reports success, intermittently, on both trees (issue #211). Do not read this bullet as saying large fetches are now sound. - **VICE 3.9** previously appeared to crash on chained HMAC-SHA256 calls (backend-independent — affects the crypto-only test suites), but this was caused by hardcoded port numbers bypassing the test harness port allocator. With proper `ViceInstanceManager` usage (no hardcoded ports), all N=1..10 chained calls succeed reliably. ## Test Automation @@ -756,7 +793,7 @@ python3 tools/uci/rig_http_local.py # HTTP GET against local listener python3 tools/uci/rig_http_live.py # HTTP GET against a real server python3 tools/uci/rig_https_local.py # HTTPS GET (TLS 1.3 + ECDSA-P256) python3 tools/uci/rig_https_live.py # HTTPS against a real public server -python3 tools/uci/rig_https_wiki.py # the 125 KB Wikipedia fetch into the REU +python3 tools/uci/rig_https_wiki.py # the Wikipedia fetch into the REU (body unverified, #211/#210) python3 tools/uci/rig_https_banner.py # the only rig that walks the menu into do_https_get python3 tools/uci/rig_https_print_body.py # wrapper: print the response body python3 tools/uci/rig_https_bad_finished.py # client must ABORT on a forged server Finished diff --git a/cfg/c64-https-ip65.cfg b/cfg/c64-https-ip65.cfg index 7944f90d..3e966dc1 100644 --- a/cfg/c64-https-ip65.cfg +++ b/cfg/c64-https-ip65.cfg @@ -226,9 +226,12 @@ SEGMENTS { # LOADER is packed on ip65 (8 B free); under UCI it rides LOADER. HTTP_AUX_CODE: load = CRYPTO_OVERLAY, type = ro, optional = yes; # jsr-only helper slice split out of HTTP_AUX_CODE (body_begin / - # done_check / hex_digit): CRYPTO_OVERLAY cannot hold the whole W4 - # machinery on ip65, so this slice rides the CRYPTO_RESIDENT tail. - HTTP_AUX_CODE2: load = CRYPTO_RESIDENT, type = ro, optional = yes; + # done_check / hex_digit). It rode the CRYPTO_RESIDENT tail while + # CRYPTO_OVERLAY could not hold the whole W4 machinery on ip65; gating + # the unreachable P-384 object out of the link retired that constraint, + # so it now sits with HTTP_AUX_CODE — the placement cfg/c64-https-uci.cfg + # and cfg/c64-https-uci-onchip.cfg have always used. + HTTP_AUX_CODE2: load = CRYPTO_OVERLAY, type = ro, optional = yes; CRYPTO_CODE: load = CRYPTO_RESIDENT, type = ro; CRYPTO_RODATA: load = CRYPTO_RESIDENT, type = ro; # libs/nistcurves segment names (c64-lib-contract / cfa9085+). diff --git a/cfg/c64-https-uci.cfg b/cfg/c64-https-uci.cfg index 6ad9107f..7c8d962c 100644 --- a/cfg/c64-https-uci.cfg +++ b/cfg/c64-https-uci.cfg @@ -61,7 +61,9 @@ # Under EMBED_P256_OVERLAY=1: routed via # OVERLAY_BLOB_P256 to the live overlay # slot at boot. -# LIB_NISTCURVES_P256_RODATA - curve256, mod256 constants. CRYPTO_HOT. +# LIB_NISTCURVES_P256_RODATA - curve256, mod256 constants. CRYPTO_OVERLAY +# from the v0.14.0 pin (was CRYPTO_HOT) — +# see the routing line near the bottom. # LIB_NISTCURVES_P256_BSS - data_p256 working buffers. NET_BSS_TAIL # (NOT CRYPTO_HOT — see the routing line # near the bottom of this file and the @@ -399,7 +401,28 @@ SEGMENTS { # from this same archive), not by re-routing the segments at # link time. LIB_NISTCURVES_P256_CODE: load = CRYPTO_HOT, type = ro, optional = yes; - LIB_NISTCURVES_P256_RODATA: load = CRYPTO_HOT, type = ro, optional = yes; + # CRYPTO_OVERLAY, not CRYPTO_HOT, from the libs/nistcurves v0.14.0 pin. + # v0.12.0's SPEC 8.2 REU DMA settle added 58 B to LIB_NISTCURVES_P256_CODE + # (thirteen `jsr nistcurves_reu_dma_wait` sites) against 36 B of free + # CRYPTO_HOT, so the link failed here: + # + # ld65: Warning: cfg/c64-https-uci.cfg(NNN): Segment + # 'LIB_NISTCURVES_P256_RODATA' overflows memory area + # 'CRYPTO_HOT' by 22 bytes + # + # (NNN is CRYPTO_HOT's own MEMORY line, not this one — ld65 names the + # region's declaration. Do not paste a line number here; the comment + # you are reading moves it.) + # + # — 22 B under USE_NISTCURVES_ONCHIP=1 and 59 B under the default REU + # profile, which shares this cfg. Moving the 192 B of curve/mod + # constants into CRYPTO_OVERLAY (~2 KB free in both profiles) buys back + # far more than the deficit and costs the hot region nothing at runtime: + # CRYPTO_OVERLAY is plain resident RAM at $4200-$5FFF under UCI, it is + # where LIB_NISTCURVES_MUL_CODE already lives, and cfg/c64-https-uci-onchip.cfg + # has routed this same segment there since the comb profile existed. + # It is rodata, so nothing about the cold-shadow zero-fill applies. + LIB_NISTCURVES_P256_RODATA: load = CRYPTO_OVERLAY, type = ro, optional = yes; # P-256 BSS is 1,312 B (fp256/mod256/points256 scratch + ecdsa256 # input/output staging + data_p256 working buffers). Stays in # NET_BSS_TAIL under the W1 hot/cold split — CRYPTO_COLD_SHADOW diff --git a/libs/nistcurves b/libs/nistcurves index 58f4cf53..c05ead66 160000 --- a/libs/nistcurves +++ b/libs/nistcurves @@ -1 +1 @@ -Subproject commit 58f4cf53b28eb35c4872c93b9b6d799d031ff892 +Subproject commit c05ead664e4611544696a683a16c57396a777913 diff --git a/src/lib_contract_asserts.s b/src/lib_contract_asserts.s index 5874b9a9..d77da2ae 100644 --- a/src/lib_contract_asserts.s +++ b/src/lib_contract_asserts.s @@ -162,12 +162,30 @@ APP_OWNED = LIB_SHARED_PRIMITIVES_SQTAB | LIB_SHARED_PRIMITIVES_REU_MUL | LIB_SH ; on MINOR bumps, so MAJOR carries no signal. ; ; nistcurves shipped 0 from v0.3.0 through v0.8.0, bumped to 1 at v0.9.0 -; (17 exported symbols REMOVED, c64-nist-curves #90/#91), and to 2 at -; v0.10.0 (the lib-contract phase-3 namespace wave, c64-nist-curves #103). +; (17 exported symbols REMOVED, c64-nist-curves #90/#91), to 2 at v0.10.0 +; (the lib-contract phase-3 namespace wave, c64-nist-curves #103), to 3 at +; v0.13.0 (`ec_scalar_mul` gained a carry return, part of the #148 +; fail-closed fix) and to 4 at v0.14.0 (#153: `reu_fetch_mul_row` takes the +; row index in A, which §8.2 had documented all along — what changed is +; upstream's conformance to it, which is why upstream ruled it MINOR). ; If a submodule bump makes this assert fire, that is the gate working: ; re-check the integration against the new export surface, then update the ; expected value on the next line — do not delete the assert. ; +; THE 2 -> 4 RE-CHECK (v0.11.2 -> v0.14.0). NEITHER hop reaches c64-https's +; call surface, and both were checked rather than assumed: +; +; ec_scalar_mul's carry return — we never `jsr ec_scalar_mul` into the +; library. src/crypto/ecdsa_verify.s defines its own shim (compiled out +; under comb; dead code under the two non-comb builds, whose ECDSA_NO_COMB +; archives do not import it), and v0.13.0's new carry check sits inside +; the library's own `.ifndef ECDSA_NO_COMB`. +; +; reu_fetch_mul_row's A-on-entry convention — c64-https never calls it. +; `git grep 'jsr reu_fetch_mul_row'` over the v0.14.0 sources finds one +; site, src/fp384.asm:697, which the archive build never assembles. +; src/boot.s owns the c64-https copy under SPEC §8.2 deferral. +; ; THE v0.9.1 -> v0.10.1 RE-CHECK, so the next person can audit the audit ; rather than re-run it blind. Method: `git diff v0.9.1 v0.10.1 -- src/` ; filtered to `.export`/`.exportzp` lines, cross-checked against every @@ -187,20 +205,33 @@ APP_OWNED = LIB_SHARED_PRIMITIVES_SQTAB | LIB_SHARED_PRIMITIVES_REU_MUL | LIB_SH ; Everything else in the wave is ADDITIVE at the default gate: the §6.5 ; rename window (#107) adds canonical `nistcurves_zp_*` / ; `nistcurves_mul_*` names while KEEPING the bare forms as same-address -; aliases, export-gated behind -D LIB_NO_BARE_EXPORTS=1. c64-https links -; ungated, so `mul_dma_lo` / `mul_dma_hi` / `mul_cached_a` / -; `mul_src2_buf` — which src/data.s provides and boot.s imports — are -; untouched. The functional surface c64-https actually consumes is the -; same four symbols as at v0.9.1: ec_base_x, ec_gx256, ec_scalar_mul_var, -; ecdsa_verify_256, all still exported. +; aliases, export-gated behind -D LIB_NO_BARE_EXPORTS=1. That sentence +; used to end "c64-https links ungated, so mul_dma_lo / mul_dma_hi / +; mul_cached_a / mul_src2_buf are untouched" — WE DO NOT LINK UNGATED, +; and have not since the wrapper started passing the flag (the very next +; paragraph depends on it). src/data.s owns those four buffers precisely +; BECAUSE the archive's copies are suppressed. The functional surface +; c64-https actually consumes is the same four symbols as at v0.9.1: +; ec_base_x, ec_gx256, ec_scalar_mul_var, ecdsa_verify_256, all still +; exported. ; ; One INPUT-side break is real and was migrated: `-D zp_ptr2=$3d` in ; tools/integration/build_nistcurves_p256.sh now hard-errors, because ; the bare alias assignment is no longer `.ifndef`-guarded. The override ; is spelled `-D nistcurves_zp_ptr2=$3d` from this pin; the wrapper's ; od65 post-check was retargeted to the canonical name so it cannot go -; vacuous. See that script's ZP_OVERRIDES block. -; PREFIXED FORM, from the v0.11.2 pin. The wrapper builds the archive with +; vacuous. (The ZP_OVERRIDES array this used to cite is gone — the +; overrides ride CONTRACT_ZP_DEFINES now; read the check_zp_slot calls.) +; +; The SECOND leg of that post-check — "no member re-exports the bare +; zp_ptr2" — DID go vacuous at v0.14.0, which is the cautionary tale. +; #154 moved the bare aliases out of zp_config*.o into their own archived +; TU (zp_aliases*.o) for §6.1 member isolation, so the member the guard +; read could no longer carry the alias whether or not the suppression +; flag took effect, and it passed by reading the wrong object. It now +; scans every member of the archive, which cannot be re-homed out from +; under it. +; PREFIXED FORM, from the v0.11.2 pin onward. The wrapper builds the archive with ; `-D LIB_NO_BARE_EXPORTS=1` (SPEC §6.5) — which is what lets src/data.s ; keep its own adopter-private mul_dma_* buffers without colliding — so the ; bare `LIB_ABI_VERSION` is no longer exported and importing it fails as an @@ -208,7 +239,7 @@ APP_OWNED = LIB_SHARED_PRIMITIVES_SQTAB | LIB_SHARED_PRIMITIVES_REU_MUL | LIB_SH ; anticipated; it arrived via the bare-export gate rather than via a second ; contract library entering the link. .import LIB_NISTCURVES_ABI_VERSION -.assert LIB_NISTCURVES_ABI_VERSION = 2, lderror, "libs/nistcurves: exported-surface generation changed (LIB_NISTCURVES_ABI_VERSION != 2) — re-check the integration, then bump the expected value in src/lib_contract_asserts.s" +.assert LIB_NISTCURVES_ABI_VERSION = 4, lderror, "libs/nistcurves: exported-surface generation changed (LIB_NISTCURVES_ABI_VERSION != 4) — re-check the integration, then bump the expected value in src/lib_contract_asserts.s" ; ===================================================================== diff --git a/tools/integration/build_nistcurves_p256.sh b/tools/integration/build_nistcurves_p256.sh index 3ac1fc5e..3f8b32bc 100755 --- a/tools/integration/build_nistcurves_p256.sh +++ b/tools/integration/build_nistcurves_p256.sh @@ -141,17 +141,25 @@ AR65="${AR65:-ar65}" # # Probing for those two says exactly what the next 200 lines depend on, and # keeps saying it correctly if upstream renames a tag under us. It is -# deliberately NOT the full requirement — CLAUDE.md records >= v0.11.2 for -# reasons this cannot see (v0.11.1's on-chip SHARED_CT_MUL_8X8 fix, v0.11.2's -# knob-staleness guard, both of which fail later and elsewhere). This gate -# exists to convert one specific illegible failure into a remedy, not to -# certify the pin. +# deliberately NOT the full requirement — CLAUDE.md records >= v0.14.0 for +# reasons this cannot see (the list is at the CONTRACT_DEFINES block below; +# every one of them fails later and elsewhere, and one of them does not fail +# at all — it changes what a signature check accepts). This gate exists to +# convert one specific illegible failure into a remedy, not to certify the +# pin. lib_preflight_fail() { local what="$1" local head; head="$(git -C "$LIB_DIR" rev-parse HEAD 2>/dev/null || true)" local found; found="$(git -C "$LIB_DIR" describe --tags --always 2>/dev/null || echo '')" local pin; pin="$(git -C "$PROJECT_ROOT" ls-tree HEAD libs/nistcurves 2>/dev/null | awk '{print $3}')" local diagnosis + # The tag for the PINNED sha, resolved rather than hardcoded: this line + # used to carry a literal "(v0.11.2)" that had to be edited by hand at + # every pin bump, and a wrong version in a diagnostic aimed at a + # version-staleness failure is worse than no version at all. Empty when + # the checkout cannot resolve it (which is itself one of the states this + # message is printed in), and the sha still identifies the pin. + local pintag; pintag="$(git -C "$LIB_DIR" describe --tags --exact-match "$pin" 2>/dev/null || true)" if [ -z "$head" ]; then diagnosis="The submodule is not checked out at all." elif [ "$head" != "$pin" ]; then @@ -170,7 +178,7 @@ ERROR: libs/nistcurves checkout is unusable for this build (c64-https#124). $what submodule checkout : $found - this repo pins : ${pin:0:12}${pin:+ (v0.11.2)} + this repo pins : ${pin:0:12}${pintag:+ ($pintag)} $diagnosis Fix it with: @@ -282,13 +290,41 @@ grep -q 'CONTRACT_ZP_DEFINES' "$LIB_DIR/Makefile" \ # and silently yields a WRONG # address with no diagnostic. # -# Requires libs/nistcurves >= v0.11.2: +# Requires libs/nistcurves >= v0.14.0: # v0.11.1 made SHARED_CT_MUL_8X8 assemble against the on-chip TU # (c64-nist-curves#123) — before that this needed a glue TU. # v0.11.2 added the knob-staleness guard: a changed CONTRACT_DEFINES used # to reuse stale objects and exit 0 with a DIFFERENT archive than # requested. That silent no-op is why an earlier attempt at this # change produced a comb image that would not boot. +# v0.12.0 added the SPEC §8.2 REU DMA settle (`nistcurves_reu_dma_wait`) +# across thirteen sites — a completion confirm plus a settle loop +# between a DMA and the next REU register write. +# v0.13.0 (c64-nist-curves#148) made the Lim-Lee comb FAIL CLOSED on an +# unusable anchor-table slot. Before it, `ecdsa_verify` accepted +# the collapsed u1·G = O case — a signature forgery, and +# c64-https-uci-comb.prg is a shipped product on that path +# (LIB_NISTCURVES_REU_BANKS_USED = $0004). Read the scope +# exactly: the guard catches a COLLAPSED anchor table, not a +# CORRUPT one. An attacker who can write REU bank 2 plants a +# valid point instead, reaches a forged R with Z != 0 throughout, +# and no post-condition on the result would see it. Upstream is +# emphatic about that distinction; do not widen the claim. +# v0.13.0 (c64-nist-curves#153) also fixed `reu_fetch_mul_row` to take the +# row index in A, as §8.2 had always documented; through v0.13.0 +# it read a library-private cached byte and ignored A entirely. +# That plus v0.13.0's `ec_scalar_mul` carry return is why +# LIB_NISTCURVES_ABI_VERSION moved 2 -> 4 (see the assert in +# src/lib_contract_asserts.s). Neither hop reaches our call +# surface: we never `jsr ec_scalar_mul` into the library, and we +# do not call the fetch. +# v0.14.0 (c64-nist-curves#154) moved the deprecated bare `zp_*` aliases +# out of `zp_config*.o` into their own archived TU +# (`zp_aliases*.o`), for SPEC §6.1 member isolation. The +# bare-export scan in section 2 below depends on this pin's +# member layout — and, more to the point, was VACUOUS against it +# until retargeted, because the member it used to read can no +# longer carry a bare alias whether or not the flag took. CONTRACT_DEFINES="-D SHARED_SQTAB_INIT -D SHARED_REU_MUL_INIT -D SHARED_REU_MUL_FETCH -D SHARED_CT_MUL_8X8 -D LIB_NO_BARE_EXPORTS=1 -D LIB_SHARED_SQTAB_BASE=0xBC00" # ZP-slot overrides go in the SEPARATE variable, not the one above: SPEC §6.2 # splits them because a globally-delivered slot define collides with every @@ -375,14 +411,55 @@ check_zp_slot nistcurves_zp_ptr2 0x0000003D check_zp_slot fp_mul_i 0x00000039 check_zp_slot fp_mul_j 0x0000003A -# The suppression gate must have reached this TU too — that is the whole -# point of routing the overrides through CONTRACT_ZP_DEFINES. -if "${OD65:-od65}" --dump-exports "$LIB_BUILD/$ZP_MEMBER" | grep -q '"zp_ptr2"'; then - echo "ERROR: $ZP_MEMBER re-exports the bare 'zp_ptr2' — LIB_NO_BARE_EXPORTS did not reach it." >&2 +# The suppression gate must have reached the archive too — that is the whole +# point of routing the overrides through CONTRACT_ZP_DEFINES rather than +# rebuilding one member with a different knob string. +# +# THIS SCAN COVERS EVERY MEMBER, and that is a correction, not thoroughness +# for its own sake. It used to od65 "$ZP_MEMBER" alone, which was right while +# the bare `zp_*` aliases were exported from zp_config.s. libs/nistcurves +# v0.14.0 moved them into their own translation unit (src/zp_aliases.s -> +# member zp_aliases_p256verify.o / _p256comb.o, upstream issue #154, SPEC +# §6.1 member isolation), so from that pin `zp_config*.o` exports no bare +# alias WHETHER OR NOT the suppression flag took effect and the old guard was +# vacuous — it passed by reading an object that could no longer fail. +# +# Naming the new member instead would put the guard back in the same trap the +# next time upstream re-homes an alias. Scanning the archive's whole member +# list cannot go vacuous that way: wherever a bare export lands, it is in some +# member of the archive we are about to link, and the loop names the member in +# the error so the report is no less specific than the old one. +# +# Members are read from upstream's build directory, exactly as the ZP check +# above does — listing an archive and reading the objects it names is not +# extraction, so SPEC §6.1 stays clean. A member the archive names but that is +# absent is a hard error, never a skipped iteration: a guard that silently +# scans nothing is the failure mode this whole block exists to close, and a +# wrong ZP slot has NO link error (it corrupts cert parsing at runtime). +BARE_OFFENDERS="" +SCANNED=0 +for m in $( "$AR65" t "$UPSTREAM_ARCHIVE" ); do + if [ ! -f "$LIB_BUILD/$m" ]; then + echo "ERROR: member '$m' named by $(basename "$UPSTREAM_ARCHIVE") but absent from $LIB_BUILD;" >&2 + echo " the bare-export scan cannot be completed and must not pass vacuously." >&2 + exit 1 + fi + SCANNED=$((SCANNED + 1)) + if "${OD65:-od65}" --dump-exports "$LIB_BUILD/$m" | grep -q '"zp_ptr2"'; then + BARE_OFFENDERS="$BARE_OFFENDERS $m" + fi +done +if [ "$SCANNED" -eq 0 ]; then + echo "ERROR: $(basename "$UPSTREAM_ARCHIVE") lists no members — nothing was scanned for bare zp_* exports." >&2 + exit 1 +fi +if [ -n "$BARE_OFFENDERS" ]; then + echo "ERROR: these archive members re-export the bare 'zp_ptr2' —" >&2 + echo " LIB_NO_BARE_EXPORTS did not reach them:$BARE_OFFENDERS" >&2 echo " One archive must carry one configuration (SPEC 6.2)." >&2 exit 1 fi -echo "[p256/$PROFILE] ZP overrides verified in $ZP_MEMBER; bare zp_* suppressed" +echo "[p256/$PROFILE] ZP overrides verified in $ZP_MEMBER; bare zp_* suppressed across $SCANNED members" cp "$UPSTREAM_ARCHIVE" "$ARCHIVE"