Skip to content

TLS client sockets for the U64, through its ESP32 - #997

Draft
JC-000 wants to merge 125 commits into
GideonZ:masterfrom
JC-000:pr/3-tls-r1
Draft

JC-000 wants to merge 125 commits into
GideonZ:masterfrom
JC-000:pr/3-tls-r1

Conversation

@JC-000

@JC-000 JC-000 commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Closes #995. Part of #952.

Draft. This PR is the top of a stack and does not build on current master without the PRs below it: #1028 (u64ctrl on ESP-IDF 6.1.1), #996 (raw_u64 on ESP-IDF 6.1.1, closes #994), #1029 (raw_c3, draft) and #953 (GET_RANDOM). #1027 and the WiFi PR (#1041) are already on master. Base: master 3db34d2; tests are included in the diff. It stays a draft until the PRs below it merge, and I keep it up to date with #953. It also needs the go-ahead on #995 first. Module version numbers are placeholders for the maintainer to assign at merge.

Try it. A prebuilt image of an earlier head of this branch is on my fork as an experimental prerelease, tls-preview-f3df8261. The demo PRG is in c64-https v0.5.1.

Problem

A C64 program that wants HTTPS does the crypto on the 6510. On a U64 Elite at 48 MHz, the 752 KB Wikipedia "Commodore 64" article takes 163.5 s with TLS on the 6510 and 19.0 s with TLS on the ESP32 (8.6x). The 6510 build is a test build and skips the certificate-chain checks the ESP32 does, so the actual acceleration is greater than 8.6x. For robots.txt the ratio is 7.9x. At a stock 1 MHz the offload client fetches a 28 KB page in 16 s. I measured these on an earlier build of the same engine, with my c64-https test clients. They are not part of this PR.

Products: the U64 and U64 Elite (raw_u64). Every other product has the commands and answers 90,TLS NOT AVAILABLE (or 81, for a malformed $21/$24 with the no-reply bit; doc/uci_tls.md). The engine is built into the raw_u64 module only. The firmware gate is #if (U64 == 1) && defined(NIOS) (network_target.h:59), so the Elite II and C64 Ultimate, whose module is u64ctrl, answer 90.

Change

The protocol is specified in doc/uci_tls.md in this PR.

  • CA store. One root store for both modules: software/u64ctrl/ca/cacert.pem (112 certificates at this head, CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_MAX_CERTS=200), the file master has since Trust Mozilla's current root list in u64ctrl's network recovery (fixes the emSign roots) #1034, and its checks (tests/lib/u64ctrl_ca_test.py: age limit, pinned hash). raw_u64's sdkconfig points its custom bundle at it (../../u64ctrl/ca/cacert.pem). This PR's own store (software/wifi/ca_store/, CA_PINS, tools/ca_store/, the 180-day build gate and the CI step "Test CA Store") is removed. What is lost with it: the cross-check of the file against Mozilla's certdata and the RELEASE=1 failure on a stale file; master's test keeps the age limit and the hash pin. software/u64ctrl/ca/README.md now says that a refresh also bumps raw_u64's module version. The store is this file, not ESP-IDF's: 6.1.1 ships Mozilla's data of 2026-02-11, with roots Mozilla has since dropped, and the READMEs and tls_engine.c now say where the Mozilla data and the Mbed TLS source come from.
  • ESP32 TLS engine. software/wifi/tls/tls_engine.c on stock mbedTLS 4.1.1 (the same Mbed TLS commit in ESP-IDF 6.0.3 and 6.1.1), using the chip's AES, SHA and big-number hardware. TLS 1.3 with a TLS 1.2 fallback. Module RPC calls 0x30 to 0x35 (software/wifi/tls/rpc_tls.h). The ESP-side TLS code sits in software/wifi/tls/, not in raw_u64/main, so the Elite II / C64 Ultimate module (u64ctrl) can build the same files later. At most two sessions (NET_MAX_TLS).
  • UCI commands $21 to $25 in network target 3, in a new file, software/io/network/network_target_tls.cc (1,661 lines). network_target.cc gains 52 lines. READ_SOCKET, WRITE_SOCKET and CLOSE_SOCKET work unchanged on a TLS handle. A status is clamped to 255 bytes, because the status length register is 8 bits.
Code Command What it does
$21 OPEN_TLS connect, handshake and verify; returns an ordinary socket handle
$22 TLS_TRUST_CA upload one DER CA certificate, in chunks
$23 TLS_INFO capability record (handle $FF), or a session's version, suite, group and key hash
$24 START_TLS upgrade an open TCP handle in place
$25 TLS_RELEASE free the session of an OPEN_TLS the client aborted

Why this shape: the ESP32 runs the session and the Ultimate carries the ciphertext, so the C64 only sees plaintext and needs nothing beyond $21. The server is always authenticated: by the CA store, one uploaded CA, or a pinned key hash.

Also in this PR: SNTP time (sntp_time.cc), which the BUNDLE and CUSTOM trust modes need, and the cancel and UNSENT additions to wifi_dedicated.cc/.h (the dedicated call channel that #953 introduces). GET_RANDOM itself is #953. The TLS reply path uses the thread byte 0xFC for the dedicated channel, clear of the slot indices of #1006 and of the values #936 uses.

Hardening: two Mbed TLS 4.1.2 fixes

Mbed TLS 4.1.2 fixes issues that reach a 4.1.1 build, and ESP-IDF 6.1.1 still ships 4.1.1. Two are closed here, in the engine and its configuration, without carrying a patched copy of Mbed TLS:

  1. A TLS 1.2 ServerHello that leaves the session version above 1.2 (CVE-2026-115490). Mbed TLS's TLS 1.2 ServerHello parser takes legacy_version as the session version, so a hello claiming 0x0304 carried on in the TLS 1.3 machine without a key exchange. The engine's group check stops the plain case, not the one that follows a HelloRetryRequest. The engine now refuses such a ServerHello at once with protocol_version, which is equivalent to upstream's fix ff52b208fe for this engine. The host tests hrr_legacy_version_13 and legacy_version_13 fail without the check and pass with it. doc/uci_tls.md documents the case under VERSION REFUSED.
  2. Curves the engine never offers (CVE-2026-90471). secp256k1 and the three Brainpool curves are off in all three module sdkconfig files. The engine offers x25519, P-256, P-384 and P-521, and those stay. The 112 roots in the store use RSA (73), P-384 (35), P-256 (3) and P-521 (1), so nothing shipped needs the others. Each image is about 13.5 KB smaller (sizes under "Flash").

Accepted limit: a private PKI whose certificates use a Brainpool curve no longer connects.

No other 4.1.2 CVE reaches this client-only build with variable buffer length off. The one remaining 4.1.2 fix without a CVE, a TLS 1.2 CertificateRequest over-read, is reachable only after the server's certificate and signature are verified. It arrives with ESP-IDF's own Mbed TLS update.

This hardening is host-tested. It has not run on hardware; the hardware results below are for the build before it.

The same series also guards a null memcpy for an empty step input in wifi_tls.cc, which UBSan flagged on the host (it failed under UBSan before the guard and passes after it).

Interaction with #835

$16 WRITE_SOCKET_CHUNK does not exist on master. The hook that makes it work on a TLS handle (in write_socket_chunk/send_to_socket) and its $16 test are in neither PR. They land with whichever of this PR and #835 merges second. doc/uci_tls.md marks $16 "Where the firmware has it". The hardware GREEN image below carried #835 and that hook.

Size

Against #953 (what this PR adds): 84 files, +13,040 / -127. Tests are most of it; the code is the TLS engine, the UCI target file and the shared directory software/wifi/tls/. This is far over the 300-line guideline in CONTRIBUTING.md. I did not split it, because the commands cannot be tested from a C64 without the engine on the module.

Flash: bridge.bin is 958,912 bytes with the hardening above (972,464 without it; 957,424 on 6.0.3), in a 1,048,576-byte factory partition. Free: 89,664 bytes, 8.6% (91,152, 8.7% on 6.0.3). IRAM: 108,551 of 131,072 bytes used, 22,521 free. raw_c3 is 909,296 bytes (922,912 without the hardening) and u64ctrl 1,072,992 (1,086,560). The flash is 2 MB, so a larger app partition (SINGLE_APP_LARGE) is possible later; it changes partition-table.bin, and this PR does not do it. The CA file holds 112 roots against MAX_CERTS 200.

Nios II app, U64: 1250.0 KiB of 1472 (222.0 KiB free, 15.1%, size check passes) at an earlier head, without #835; I did not re-measure at this head. This PR also adds network_target_tls.cc to the U2, U2+, U2+L and U64 Elite II Makefiles, where the commands answer "not available". I did not measure those builds.

Testing

Hardware, ESP-IDF 6.1.1. One work-in-progress build (git hash 1a0778724) carried this PR with #1095 and the whole stack (#1028, #996, #1029, #953), #835 and test-only fixes that belong to no PR here. It was installed on the U64E with the updater; the configuration was reset. The 6.0.3 figures in brackets are from the build before it (git hash 2a5eb4590) on the same device. The hardening above is not in this build.

The RED runs below stay valid. The move from 6.0.3 to 6.1.1 is a toolchain change with no RED of its own, and the RED image has no TLS commands.

Ultimate 64 Elite (U64E), firmware 3.15a, core 1.50:

Test GREEN (6.1.1)
net_tls_test --route all --expect green --require-lock, Ethernet GREEN, 54 scenario runs (52 on 6.0.3)
same, WiFi with the Ethernet cable out GREEN, 16 scenario runs (14)
60-minute TLS loop 29 of 29 iterations passed
bulk-aes-gcm (S40), TLS bulk transfer GREEN, sizes up to 1 MiB
run-tests --e2e --profile quick all 37 suite runs passed
run-tests --e2e --profile standard all 73 suite runs passed in 2,646 s (2,812 s)
uci-net-target, deep profile exit 0 over Ethernet and over WiFi

ESP-IDF 6.1.1 changed esp_aes_gcm.c (it buffers the AAD and the GHASH input differently), and the engine links it. The host tests never reach that file, so S40, which sends records of varied sizes up to 1 MiB, is the hardware check for it. The module's WiFi behaviour (boot, joins, soaks, throughput) is in #996.

C64U: its module in the 6.1.1 build carries the engine from #1095, so it answers the TLS commands as a TLS build. The 8-form table below checks a module without the engine and was not repeated on 6.1.1; the C64U TLS results are in #1095.

Hardware, first round (ESP-IDF 6.0.3). One work-in-progress build (git hash fdb59b059) carried this PR together with the whole stack (the PRs listed above and the WiFi PR) and test-only commits that are in no PR here (the lwIP pbuf pool change for SEND ERROR: 12, and #835 with the $16 TLS hook). Every PR head is an ancestor of that commit, so this PR's patches are the bench's. The PR head here differs from the bench's only by the E2E agent mailbox move (below); the bench also has its own version-number commit. The RED runs were on the image before the flash: ESP-IDF 5.3.1 modules, no TLS commands, no GET_RANDOM. After the flash the config was reset and only needed settings set.

Ultimate 64 Elite (U64E), firmware 3.15a, core 1.50, net_tls_test.py --route all --expect green --require-lock:

Test RED (pre-flash image) GREEN (fdb59b059)
net_tls_test, both routes, 46 scenario runs RED as expected over 46 scenario runs, 342s: every TLS scenario answers 21,UNKNOWN COMMAND; S19e and S19e2b are N/A on the native route GREEN as expected over 46 scenario runs, 966s, exit 0, 0 FAIL or ERROR verdicts; only S19e and S19e2b skipped (N/A on native: REST-only abort). The REST route passes at this head, including S10, S13 and S30. The logged 13, / 12, statuses are the refusal scenarios, verdict OK
uci-net-target, deep profile exit 0 over Ethernet and over WiFi
run-tests --e2e --profile quick all 37 suite runs passed

Extra evidence, a real client: c64-https's ESP32-TLS demo program, c64-https-uci-m3-demo from c64-https v0.6.1 (.prg, sha256 f5c1e287…), ran this exact image on the U64E (Ethernet, 48 MHz) on the same GREEN build: the Wikipedia "Commodore 64" article over TLS 1.3, HTTP 200, 760,692 bytes matching Content-Length, 17 s; github.com /robots.txt, HTTP 200, 4 s; a host whose certificate is for another name refused with 94,CERTIFICATE NAME MISMATCH; a server without TLS 1.3 refused with 14,TLS ALERT RECEIVED: 40. Its logs are in that project, not in this PR.

C64 Ultimate (C64U): its module is u64ctrl, which has no TLS engine here, so by design it answers as a non-TLS build. The check is that the commands answer correctly and nothing else changes:

Test RED (pre-flash image) GREEN (fdb59b059)
The 8 TLS command forms (tls_c64u.py) all 8 answer 21,UNKNOWN COMMAND 8/8 as specified: $23 $FF 00,OK with the 16-byte capability record, $23 01 81, $21 valid 90 (TLS NOT AVAILABLE), $21 malformed 81, $24 unowned 81, $22 with 5 bytes 81, $22 with 6 bytes 90, $25 00,OK

The module's WiFi behaviour under this stack (boot, joining, the suites and soaks, the before/after throughput on ESP-IDF 5.3.1 and 6) is in #996 and #1028, since this PR's code is in the same image.

E2E agent mailbox. The TLS E2E agent's OPT_WAIT option moved from $C00A to $C00B (tests/e2e/io/command_interface/uci_agent.asm and tests/e2e/lib/uci_native.py), because the draft #936 uses $C00A for OPT_CANCEL. The change is the last commit of this branch. It assembles cleanly with the tree's 64tass in CI's image (no errors or warnings). A follow-up commit makes the native client write OPT_WAIT at its new address on its own (it had been written together with OPT_ABRT in one two-byte write). With both commits, the TLS E2E suite reran on the U64E over the native route: every scenario that ran passed, including the slow-handshake ones.

Host tests (no device; at this head in the CI image: make host_tests_idf6 RC=0 (102 checks, 0 failed), make host_tests for the TLS rig exit 0 including the two hardening tests, and tests/lib/u64ctrl_ca_test.py OK):

  • make -C target/pc/linux/wifitls test: wifi_tls_test and net_tls_test, 0 failures. An earlier head had 59 and 337 checks, and 343 with the $16 test on the branch that also has Spike for #807: NET_CMD_WRITE_SOCKET_CHUNK ($16), a payload larger than one command as one datagram #835.
  • tests/lib/registry_test.py, esp_depends_test.py and ruff check --config tests/ruff.toml tests/ pass.
  • net_tls_selftest.py --quick passes with and without c64_test_harness installed. The harness (https://github.com/JC-000/c64-test-harness, public) is not a dependency. Without it, net_tls_test.py warns and skips the device lock unless --require-lock is given.
  • net_tls_test.py is not registered in run-tests. It is opt-in by name, because it can switch "WiFi Enabled" on and needs the test host as a TLS peer and the Internet. The offline uci-net-tls-selftest is registered in the DEEP profile.
  • The tools/ca_store tests (91) are gone with the store.

CI steps added: "Test UCI TLS Sockets". The step "Test CA Store" is removed with the store. CI has not run on the rebased head.

Engine outside the Ultimate: the engine also builds unmodified into an ESP-IDF app with no Ultimate code: https://github.com/JC-000/1541u-tls-standalone/tree/0e2a7c4752516acd3971601d476c198bc987d4d7. On a classic ESP32 dev board its handshake takes 0.6-2.9 s. That repository does not contain the engine tests I keep under raw_u64/test in my fork. That directory is not in this PR.

Overlap with #953

This PR is stacked on #953 and uses its wifi_dedicated channel. DEDICATED_BUSY means "no answer, or not sent" in #953 and "sent, no answer" in the TLS code, so this PR adds DEDICATED_UNSENT = 6 and the GET_RANDOM wrapper maps it to the busy result (return (retval == DEDICATED_UNSENT) ? WIFI_RANDOM_BUSY : retval;), so GET_RANDOM answers 88, not 87, while TLS holds the channel. That mapping rests on host tests; it was not provoked on hardware. The TLS engine's own random draws are not under #953's entropy gate; the engine refuses only when the entropy source is NONE, which with the SAR ADC source now means no source at all.

#936 no longer uses RPC codes of its own: it carries its traffic inside CMD_IDENTIFY on thread bytes 0xFD to 0xFF, clear of this PR's 0xFC.

Not tested, and known gaps

  • The hardening above has not run on hardware.
  • No Ultimate-II cartridge test. CONTRIBUTING.md asks for one; I have a U64 Elite and a C64 Ultimate only. No U64 (non-Elite) or Elite II hardware test either. The "not available" answer of the products without the engine is covered by host tests, and on the C64U by the table above.
  • Nios II app size for the U2 family was not measured (above).
  • GET_RANDOM answering 88 while TLS holds the module channel was not provoked on hardware (above).
  • The opt-in scenarios S32 to S34 of net_tls_test were not run in this round.
  • The dispatch task and the UART interrupt run on core 1 (the ESP-IDF PR's performance commits), and the TLS task is on core 1 at idle priority plus one, below the dispatch task at priority 19. The hardware runs used this arrangement. The TLS engine code is not in the IRAM fragment, but dispatch_command and tls_rpc_note_block are.
  • CI has never run on the self-hosted runner.
  • The hacked_recv change from the ESP-IDF PR has not run on hardware.
  • Open question for GideonZ and chrisgleissner (TLS client sockets for the Ultimate 64 through its ESP32: a 5-PR series, and coordinating with the other TLS work #952, question 4): should the commands sit behind an "Experimental" setting, off by default, as barryw suggested on Add authenticated HTTPS to the existing target 6 HTTP interface #936?

Area contacts: barryw (UCI), chrisgleissner (tests/, run-tests). GideonZ for the go-ahead.

Written with Claude Opus 5.5 in Claude Code.

@GideonZ

GideonZ commented Oct 15, 2026

Copy link
Copy Markdown
Owner

I thought that this approach was already under dispute for its architecture. Why is this brought up again without settling on the architecture?

@JC-000

JC-000 commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor Author

No one said anything for 11 days, so it seemed reasonable to present the architecture I was proposing in a usable format. I re-ran the test and build cycle last night and made sure there was a working demo program that uses the offload.

@JC-000

JC-000 commented Oct 15, 2026

Copy link
Copy Markdown
Contributor Author

Since this works end to end on the U64 and U64 Elite, I'm now working on bringing the same TLS offload to the C64 Ultimate and Elite II, so the products with an ESP32 module have the same feature. This PR will likely change as a result so even if everyone aligns, it should stay a draft for the time being.

@chrisgleissner

chrisgleissner commented Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator

Hi @GideonZ and @JC-000 ,

I agree with both of you. POCs are very normal to try out different approaches. And I applaud the initiative. However, it should be made clear that something is a POC.

Additionally, raising it against Gideon's repo has the downside of causing real expense for Gideon due to his build server building code that may never get merged.

So I would suggest to target your own fork's master instead of Gideon's master for POCs. Then, I can see no harm and only benefits in augmenting a design proposition with a POC to back it up. This is how I work professionally, as well. Nothing speaks louder than working, clear code, and the perfect design may not be viable once turned into code.

@chrisgleissner chrisgleissner added enhancement New functionality or an improvement to existing behavior, including formats and developer tooling. uci C64-side Ultimate Command Interface, command targets, and Ultimate DOS commands; REST is network. network Networking and remote services: REST, Socket DMA, FTP, Telnet, and online access. labels Oct 17, 2026
@barryw

barryw commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

Following up on @GideonZ's request in #952 for main-CPU benchmarks, and @JC-000's note that Nios had not been measured: I ran a temporary authenticated TLS client on my U64 Elite. Device reported firmware 3.15a (99784768), FPGA 126, core 1.50. Build used Mbed TLS 3.6.7 with U64 Nios -Os -mno-hw-mul flags. Wired U64 connected to a local-LAN peer. TLS 1.2 used ECDHE-ECDSA-CHACHA20-POLY1305 with a local P-256 CA and server certificate; chain, hostname, and dates were verified. Test-only deterministic entropy was used, so this is a performance diagnostic, not a deployable TLS implementation.

After one warmup, 12/12 connections completed verification and matched the 256 KiB payload; peer reported no resumption. Median handshake: 35.980 s (35.965–35.995). Median TLS payload transfer: 7.355 s (34.8 KiB/s); same payload over plain TCP: 0.298 s (860 KiB/s). A separate profiled firmware completed 3/3 verified handshakes and spent 35.465 s of 35.745 s (99.2%) in P-256 point multiplication/multiply-add. Application grew by 98,080 bytes versus clean master, including test route and CA, leaving 144 KiB U64 application flash. Idle heap was stable across 12 runs. Concurrent /v1/info probe: median 8.85 ms idle versus 24.83 ms during handshake, with no failed requests.

This fills one U64 Nios data point; production entropy, TLS 1.3, other suites, and other products remain unmeasured. It is not a direct performance comparison with this PR's ESP32 implementation (Mbed TLS 4; TLS 1.3 with TLS 1.2 fallback). A direct comparison would match peer, TLS version/cipher and payload while accounting for differing transports. Fixture and raw JSON available if useful.

@JC-000

JC-000 commented Oct 24, 2026

Copy link
Copy Markdown
Contributor Author

@barryw Thanks for the Nios numbers. Yes to the fixture and the raw JSON. Your profile also answers the #952 question of what to accelerate on the Nios: P-256 point multiplication is 99.2% of the handshake.

I'll run the matched comparison on my U64 Elite with this PR: your peer, pinned to TLS 1.2 with ECDHE-ECDSA-CHACHA20-POLY1305, your P-256 chain (CA uploaded with TLS_TRUST_CA), the same 256 KiB payload and your /v1/info probe. I'll report handshake, transfer and app-size cost the way you did, with the ESP32 image size alongside the Nios app delta.

I'll also measure service impact on both builds, if you can share the firmware branch for your client. On the U64 the Nios runs the menu, FTP and REST, the software IEC drive, and disk-image loading and write-back; the drive CPUs run in the FPGA. Your probe shows REST answering during a handshake (median 8.85 ms idle, 24.83 ms during, no failures). Next to it I'll time FTP downloads during back-to-back handshakes against an idle baseline.

The comparison will state what differs:

  • Transport: in this PR TCP stays in the Nios lwIP and the ciphertext crosses the Nios-ESP32 UART, so transfer time includes that link. Your build runs TLS directly on lwIP.
  • Library: Mbed TLS 4.1.1 on the ESP32 with its SHA and big-number hardware (no ECC unit on this ESP32; ChaCha20-Poly1305 runs in software), against 3.6.7 on the Nios.
  • Entropy: the ESP32 hardware RNG, which this PR uses only while Wi-Fi runs, so Wi-Fi stays enabled with the route on Ethernet; against your deterministic test entropy.

I'll post the numbers here.

Written with Claude Opus 5.5 in Claude Code.

@barryw

barryw commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

@JC-000 Thanks for taking on the matched run and FTP contention measurements. The Nios firmware source, peer, certificate chain, raw results, and build instructions are on a diagnostic branch of my fork: benchmark README. The raw JSON has the 12 uninstrumented runs, three profile runs, and the /v1/info latency samples; I removed only U64 hostname/ID/MAC fields.

The peer directory includes ca.der for TLS_TRUST_CA, the P-256 server certificate and test key, TLS and plain-TCP listeners, and a local smoke test. It sends the same byte-checked 256 KiB payload. The branch pins Mbed TLS 3.6.7 and includes the optional curve-profile patch. Fresh checkout on beast built update.u64; U64 application-space gate passed with 144.1 KiB free. I have not flashed that published branch onto the U64; the measured diagnostic firmware was built from the same benchmark code before the fixture was packaged.

One comparison detail: the peer now explicitly pins P-256 ECDHE. It did not pin or log the negotiated ECDHE group during my original 12 runs. For a strict comparison, please rerun the Nios branch and your ESP32 branch against this pinned peer, rather than treating my earlier 35.980 s median as a fully matched group measurement. The client uses deterministic test entropy and fixed LAN peer address, so this branch is for benchmarking only.

@barryw

barryw commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Follow-up to my Nios benchmark above: I tested PSK on a temporary U64 Elite diagnostic build to see whether using PSK changes the picture. This used the same wired LAN test host (separate certificate and PSK listeners) and verified 256 KiB payload, TLS 1.2 with ChaCha20-Poly1305, Mbed TLS 3.6.7, and the U64's -Os -mno-hw-mul build flags. Each series had one warmup; every recorded run completed with the expected cipher and payload.

Key exchange Runs Median handshake Median 256 KiB TLS transfer
Pure PSK 12/12 65 ms (60–65 ms) 7.725 s
ECDHE-PSK 5/5 11.865 s (11.860–11.870 s) 7.725 s
Certificate ECDHE-ECDSA 3/3 35.875 s (35.860–35.875 s) 7.335 s

Plain TCP moved the same 256 KiB in about 0.29 s. The 65 ms result is the TLS handshake on a LAN, not a complete socket open or an Internet latency measurement. Pure PSK avoids the expensive P-256 work, but has no forward secrecy, needs secure shared-key provisioning and rotation, and is not an option for ordinary public HTTPS servers. ECDHE-PSK retains forward secrecy but still takes nearly 12 seconds on this Nios. Neither PSK mode fixes encrypted bulk throughput. These Nios results do not constitute a matched comparison with this PR's ESP32 implementation. They show why pure PSK is a specialized choice for managed peers, not a general replacement for certificate-verified TLS.

This remains a performance diagnostic: fixed test PSK and deterministic test entropy, not production key handling or entropy. I have not measured game-sized packets, DTLS, resumption, other products, or WAN latency.

@JC-000

JC-000 commented Oct 26, 2026

Copy link
Copy Markdown
Contributor Author

@barryw Thanks for publishing the fixture. I ran it and this PR on the same U64 Elite, and set up the PR so it is measured the way your fixture measures itself.

The equivalent tester. Your fixture is a Nios task: PUT /v1/machine:tls_bench runs Mbed TLS on the Nios and receives into a 1024-byte Nios buffer, so the data never goes to the C64. For this PR I wrote a bench-only Nios task (software/api/esp_tls_bench.cc, not part of the PR) with the same endpoint, parameters and JSON, so one collector drives both builds: our port of your tools/tls_bench_collect.py (same PUT, 0.25 s poll, 2 s gap), which only adds reporting fields.

  • Same task priority, same 5 ms FreeRTOS tick and elapsed(), same 1024-byte buffer with the byte check, same test host and payload (256 KiB), and your run_plain copied line for line.
  • It drives this PR's command target directly (net.parse_command/get_more_data, the command bytes a C64 sends, without the UCI): TRUST_CA, OPEN_TLS, TLS_INFO, WRITE 'B', READ until 256 KiB.
  • Handshake: yours times mbedtls_ssl_handshake (tls_bench.cc:231-239). Ours is the target's own handshake window, read back with TLS_INFO; it starts after the module open and the TCP connect, like yours. Transfer, on both: from the return of the 'B' write to the last byte checked (tls_bench.cc:257-274, esp_tls_bench.cc:230-252).
  • Each run fails unless the session is TLS 1.2, ChaCha20-Poly1305 (0xCCA9), P-256 (0x0017), which your server forces.

Results. The tree measured is this PR on top of #996 plus performance commits not yet pushed to either PR, on current master; I'll update both. Same device, wired, same test host, same day (about 5 h apart, with a reflash between); Turbo Manual at 48 MHz on both, read back per series (a pass at 1 MHz gave the same figures); 1 warmup + 12 runs per series, no errors.

Your Nios fixture This PR, from the Nios task
Handshake, median (your method / matched) 35.62 s / 34.36 s 1.565 s / 1.565 s
256 KiB over TLS 7.28 s / 7.02 s 2.33 s / 2.31 s
256 KiB plain TCP 270 ms / 260 ms 275 ms / 265 ms
REST latency while TLS runs (median; idle ≈10 ms) 20.7 ms 10.8 ms
FTP download while TLS runs (idle ≈1,255 KiB/s) 610 KiB/s 1,020 KiB/s

"Your method" polls the device every 0.25 s during the run; "matched" leaves it alone until the run ends. Your fixture's results here are within about 1% of your own (35.6 s and 7.3 s here; 35.98 s and 7.36 s in your post, 35.875 s and 7.335 s in your PSK table). The plain transfer is within one 5 ms tick on both builds, so both carried the same background load. In the capture, the client's compute step between ServerHelloDone and ClientKeyExchange is about 35.57 s of each 35.60 s handshake, which your profile puts in P-256.

What still differs:

  • The TLS engine: Mbed TLS 4.1.1 on the ESP32 (with its SHA and bignum hardware) behind a UART, against Mbed TLS 3.6.7 on the Nios. That is what is being compared.
  • Against this PR: our handshake window includes the UART round trip of each handshake step and one statistics call at the end (under one 5 ms tick when timed alone), and our ClientHello also offers TLS 1.3 with an X25519 key share before the server picks 1.2. On the wire our handshake takes 1.36 s of the 1.565 s.
  • Our READ goes through the command layer in 1024-byte replies (two blocks each); yours calls ssl_read directly. That also counts against this PR.
  • Not ranked: entropy (the ESP32's hardware RNG with Wi-Fi up, against your fixed test entropy), and socket mode (ours non-blocking with TCP_NODELAY, yours blocking).
  • Found while doing this: this PR's socket doesn't answer the server's close_notify. It's outside the timed window; I'll fix it in the PR.

This PR's own TLS suite (net_tls_test.py) also passed on this build: 46 scenario runs, 3 skipped as not applicable to the route.

Everything needed to repeat it (the responder branch, the patch that points your fixture at another test host, the scripts, server setup, device config, run rules, and the per-run results with device identifiers removed) is at https://github.com/JC-000/1541ultimate/tree/8ad132e79f2ffba446a8cdcb32af8c6c4950ed98/repro/997-tls-bench.

I agree with your PSK conclusion: pure PSK suits managed peers only, and your 11.9 s ECDHE-PSK handshake is still about 7.6 times this PR's certificate handshake.

Not measured: the Wi-Fi data path, other products, DTLS, resumption, game-sized packets, WAN latency.

Written with Claude Opus 5.5 in Claude Code.

@JC-000

JC-000 commented Oct 26, 2026

Copy link
Copy Markdown
Contributor Author

One point I should have made explicit above: those figures include the cost of offloading. In the responder run every TLS record arrives on the Nios's own TCP stack, goes to the ESP32 over the UART as ciphertext, and comes back as plaintext, so each byte crosses the UART twice, and every handshake step is a UART round trip. With that included, the handshake is still about 23 times shorter and the 256 KiB transfer about 3 times shorter (about 110 KiB/s through the round trip), and REST and FTP stay close to their idle figures while TLS runs, because the Nios no longer does the crypto.

That makes me think the same offload could later serve the other direction too, for example TLS in front of the REST/HTTP interface (and FTP), without taking Nios time from the machine. This PR is client-only, and a server needs its own pieces (a device key and certificate, server-side handshakes, how the user provisions them), so that would be a separate discussion and separate measurements. I'm only noting that today's numbers don't argue against it.

Written with Claude Opus 5.5 in Claude Code.

@JC-000

JC-000 commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

I rebased this PR onto the updated #996 (on master 4ac0608, #1028 and #1027). The commits below its own seven are #1028's 11, #1027's 4 and #996's 9, so the Commits tab shows those 24 and then this PR's 7. Please look only at the last seven.

The conflicts were in the files #996's performance commits and master touch. In Makefile (the .PHONY line and the host_tests list) and run-tests (the suite list), master and #1028 added entries; I kept master's and #1028's entries and this PR's ca_store_test, wifitls test and uci-net-tls-selftest. In software/wifi/raw_u64/main/CMakeLists.txt I kept this PR's TLS source block and #996's linker fragment. In rpc_dispatch.c, start_dispatch creates the TLS task and then the dispatch task, which #996 pins to core 1 at priority 19. The TLS task is also on core 1, at idle priority plus one, so the two share that core. Two commits are new on this PR since the last head:

  • Answer the peer's close_notify with our own on TLS client sockets. The engine ended the session on the peer's close_notify without replying, so the peer saw a bare FIN. The new test (S6) failed on the U64 Elite on both the REST and the native route before the fix and passes on both after it. The full net_tls_test suite is green on this head: 46 scenario runs.
  • Keep the U64 WiFi bridge command dispatch and TLS block timing in IRAM, which puts dispatch_command and tls_rpc_note_block in the IRAM fragment.

The module is 950,848 bytes (9% of the app partition free), with 106,595 of 131,072 bytes of IRAM used. The images for the hardware runs had no #835 and a module version that exists only on my bench, so that the updater installs the module.

Written with Claude Opus 5.5 and Claude Sonnet 5.5 in Claude Code; reviewed adversarially by separate Claude Opus 5.5 sessions before posting.

JC-000 and others added 6 commits October 30, 2026 14:29
…DF 6.0.3

u64ctrl moves to ESP-IDF 6.0.3 (mbedTLS 4) and bumps the module version
so the updater installs it. This changes the toolchain of the C64
Ultimate's power controller, not only of its WiFi: the module runs
power, button and keepalive handling. It is one commit because none of
its parts builds alone: the CMake check stops CI's ESP-IDF 5.3.1 build,
and the recovery code does not compile under mbedTLS 4.

- my_uart.c: uart_periph.h moved from soc/ to hal/, and
  uart_isr_handle_t (a typedef of intr_handle_t) is gone. The same two
  changes GideonZ#996 makes for raw_u64.
- CMakeLists.txt: configure stops on an ESP-IDF older than 6.
- sdkconfig: regenerated by idf.py build from the ESP-IDF 5.3.1 file.
  Values present in both versions are carried; of the 1498 options in
  the 5.3.1 file only the esptool before/after spelling, DHCP option
  length (68 to 69), X509 trusted cert callback and
  ESP_NETIF_RECEIVE_REPORT_ERRORS changed value (SOC_ constants aside).
  Options that are new take ESP-IDF 6's defaults, among them picolibc
  instead of newlib and the FreeRTOS and ring-buffer functions running
  from flash rather than IRAM. Brownout level 7 and the 100 Hz tick are
  carried.
- rpc_dispatch.h: the module version is the next minor above master's,
  see the history line. The maintainer assigns the number at merge.
- CI builds u64ctrl in espressif/idf:v6.0.3, pinned by digest in
  $IDF6_IMAGE; raw_u64 and raw_c3 stay in my_docker_image. The ESP32
  cache key names both images. README.txt and software/u64ctrl/README.md
  say how to build it.

The recovery code from the Wi-Fi recovery change is ported to mbedTLS 4
and PSA Crypto, because it did not compile: mbedTLS 4 has no public
mbedtls/sha256.h and no mbedtls_pk_get_type(). The mbedTLS 4 port is of
code written for mbedTLS 3.

- recovery_verify.c: the key type comes from mbedtls_pk_get_key_type()
  (an ECC public key); the manifest hash is psa_hash_compute();
  psa_crypto_init() is always called, so the init does not depend on
  MBEDTLS_USE_PSA_CRYPTO, which is still defined as a legacy symbol that
  is slated for removal. A failure to start is reported as
  RECOVERY_ERR_CRYPTO ("crypto library failed"), not as a bad signature.
  The signature itself is still checked by mbedtls_pk_verify() with a DER
  ECDSA signature, as before.
- recovery_verify.h, recovery_verify.c: the streaming hash of a
  downloaded file moves out of recovery.c into recovery_hash_*(), on
  psa_hash_*(), so the host test can run it. It also holds the compare
  with the manifest's hash, reported as RECOVERY_ERR_HASH_MISMATCH.
- recovery.c: stream_file() uses it. The checks, their order and the log
  line for a differing hash are as before; a failure of the crypto
  library is logged by name.
- Two result codes are added after the existing ones; nothing that was
  returned before changes.
- The host test links TF-PSA-Crypto from ESP-IDF 6, so it moves out of
  make host_tests, which CI runs in ESP-IDF 5.3.1, into host_tests_idf6
  and its own CI step in $IDF6_IMAGE.

Tests, written first and red against the unported code (recovery_verify.c
cannot include mbedtls/sha256.h under ESP-IDF 6), check by the error each
case names:

- a valid manifest is accepted;
- the wrong key, a tampered manifest, and a manifest correctly signed by
  an RSA key (rsa512.manifest) are refused as a bad signature;
- a changed or truncated image or bitstream is refused as a hash
  mismatch, and so is a file whose manifest hash differs in its first,
  middle or last byte, which a compare that stopped early would accept;
- a crypto library that will not start, or a hash that fails in its
  update or finish, is reported as such, never as a bad signature or a
  match. The test forces these through --wrap=psa_crypto_init,
  psa_hash_update and psa_hash_finish, so the product code carries no
  test hook.
The signed vectors are unchanged; the two RSA files are new.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
On mbedTLS 4, PSA's key slots are protected by a mutex only with
MBEDTLS_THREADING_C. It is ESP-IDF 6's default; the regenerated
sdkconfig had carried 5.3.1's "not set". The WiFi supplicant
(ESP_WIFI_MBEDTLS_CRYPTO, on in this project) imports and destroys PSA
keys now, and it runs next to anything else that uses PSA. The pthread
implementation comes with it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…T buffer

When no UART buffer is free, the receive hook returned ERR_BUF and
wlanif_input() freed the packet. On ESP-IDF 5.3.1 the WiFi driver was
then told ESP_OK; on ESP-IDF 6 esp_netif_receive() passes the error
on, so the driver would see ESP_FAIL on this path. Stock ESP-IDF 6
already returns ESP_FAIL whenever lwIP's input mailbox is full, so the
driver handles it; free the packet here and return ERR_OK only to
leave this path exactly as before. The packet is dropped either way.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
UART1 is routed to the FPGA pins in two places: at boot, in
setup_modem(), and whenever the machine is switched on, in
regulator_enable(). Before either, the pins hold their reset state or,
after check_fpga() or a power off, a pull-down (check_fpga() sets it on
RXD only; the power-off path sets it on all four). ESP-IDF 5.3.1's
uart_set_pin() put a pull-up on RXD and CTS before connecting them,
replacing it. ESP-IDF 6's uart_set_pin() never touches pulls, so RXD
would stay pulled down and CTS at its reset state while the FPGA
configures, reading as a break and as clear to send where 5.3.1 read
idle and not clear.

One helper, my_uart_connect_pins(), sets the pull-ups and then routes
the pins, in that order as 5.3.1 did, and both places call it. While
the machine is off RX and CTS stay routed, pulled down; a boot that
stays off has the pull-ups on for about 100 ms until
regulator_enable(0), the same as on 5.3.1.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
ESP-IDF 6's brownout interrupt waits for every enabled UART's
transmitter to drain before it resets the chip; 5.3.1 reset at once.
UART1 honours CTS, and CTS idles high through its pull-up when the FPGA
loses power with the machine, so bytes left in the transmit FIFO would
never leave and the wait would last until the 300 ms interrupt
watchdog, ending in a panic reset instead of a brownout reset.

Register a brownout callback, which runs in IRAM ahead of that wait,
that turns CTS flow control off so the FIFO drains. A failed
registration is logged and does not stop the boot.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The host test only used keys of its own. Load every key in
recovery_trusted_keys as an EC public key, require a spare next to the main
key, require that no two shipped keys are the same, and check that a manifest
is verified by a key placed behind all of them.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
JC-000 and others added 19 commits November 16, 2026 14:38
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The 1.31 line of the version history said the module was built with ESP-IDF 6.0.3. It is now built with 6.1.1.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
ESP-IDF 6.1.1 added CONFIG_ESP_WIFI_ENABLE_WPA3_OWE_SOFTAP, which defaults to y. 6.0.3 had no such option, and the module only runs as a station, so it is set to n to keep the 6.0.3 behavior and not link soft-AP code.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The 1.20 line of the version history said the module was built with ESP-IDF 6.0.3. It is now built with 6.1.1.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
ESP-IDF 6.1.1 added CONFIG_ESP_WIFI_ENABLE_WPA3_OWE_SOFTAP, which defaults to y. 6.0.3 had no such option, and the module only runs as a station, so it is set to n to keep the 6.0.3 behavior and not link soft-AP code.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The 1.13 line of the version history said the module was built with ESP-IDF 6.0.3. It is now built with 6.1.1.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
ESP-IDF 6.1.1 added CONFIG_ESP_WIFI_ENABLE_WPA3_OWE_SOFTAP, which defaults to y. 6.0.3 had no such option, and the module only runs as a station, so it is set to n to keep the 6.0.3 behavior and not link soft-AP code.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…branch

# Conflicts:
#	software/u64ctrl/main/rpc_dispatch.h
#	software/wifi/raw_c3/main/rpc_dispatch.h
#	software/wifi/raw_u64/main/rpc_dispatch.h
This branch turns off the supplicant's TLS client. The option is ESP_WIFI_MBEDTLS_TLS_CLIENT in 6.0.3 and in 6.1.1 alike; WPA_MBEDTLS_TLS_CLIENT is its deprecated alias. 6.1.1 writes the deprecated-alias block twice, and the merged sdkconfig held the second copy at y, next to the real option set to off. The first build under 6.1.1 resolved the alias to off and rewrote the file. The file now holds what the build produces, so the setting is the same as before.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The raw_u64 README gave the date of the Mozilla root list that ESP-IDF ships as that of 6.0.3 (2025-12-02). The list in 6.1.1 is of 2026-02-11. The comment on the entropy draws of the TLS engine named the IDF 6.0.3 copy of mbedTLS 4.1.1; it names 6.1.1 now.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
fill_step copied the ciphertext and plaintext input of a step with memcpy whatever their length. A step with nothing to send of one kind has a null pointer for it, and memcpy's pointer arguments must not be null even when it copies 0 bytes. Built with UndefinedBehaviorSanitizer, the wifitls host test stopped there ("null pointer passed as argument 2, which is declared to never be null"). Each copy is now made only when its length is not 0. The wifitls and nettls tests pass under the sanitizer.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
mbedTLS's TLS 1.2 ServerHello parser takes legacy_version as the session
version, so a hello claiming 0x0304 carried on in the TLS 1.3 machine without
a key exchange. After a HelloRetryRequest the engine's group check does not
catch it. Refuse it at the ServerHello with protocol_version.

Host test (host-tls rig): hrr_legacy_version_13 and legacy_version_13.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The engine offers x25519, P-256, P-384 and P-521 only; the build also
carried secp256k1 and the three Brainpool curves. The roots in the CA store
use P-256, P-384 and P-521 (and RSA), so nothing shipped needs the others.
Applied to all three module products.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@JC-000

JC-000 commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

Hardware check of the Mbed TLS hardening commits (b759883 TLS 1.2 ServerHello version check, f2655e3 curve trim, ec31688 docs) on an Ultimate 64 Elite, flashed with a bench build of this branch's head ec31688 together with our other open PRs:

  • TLS E2E suite, all routes: 54 of 54 scenario runs passed
  • uci-net-target, standard profile: passed
  • six passes of a loop over five TLS scenarios: 6 of 6 passed

Written with Claude Opus 5.5 in Claude Code.

JC-000 and others added 9 commits November 23, 2026 13:22
Conflicts resolved:
- software/u64ctrl/main/rpc_dispatch.h: master's GideonZ#1097 took V1.30 for the root list
  change; this branch keeps its own V1.31 (ESP-IDF 6.1.1) and carries master's
  1.30 history note below it.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Brings in master 40188c7 through the merged pr/idf6-u64ctrl branch (the PR
below this one in the stack, merge/1028-master-1123). No conflicts.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Brings in master 40188c7 through the merged pr/2-idf6-r4 branch (the PR
below this one in the stack, merge/996-master-1123). No conflicts.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Brings in master 40188c7 through the merged pr/idf6-raw_c3 branch (merge/1029-master-1123).

Conflicts resolved:
- Makefile (host_tests): this branch added four GET_RANDOM host test lines (radioentropy,
  wifirandom, wifidedicated, getrandompaths) where master added the userinterface
  tests line; both kept, so host_tests runs all of them.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Brings in master 40188c7 through the merged uci-net-get-random branch (merge/953-master-1123).

Conflicts resolved:
- Makefile (host_tests): this branch added the wifitls test line where master added the
  userinterface tests line; both kept, so host_tests runs both.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
network_target.cc dispatches the TLS commands to NetworkTarget::open_tls and
four more, which live in network_target_tls.cc, so the network-target test
could not link (upstream CI fails "Test Ethernet Transmit Framing" on it).
The host build has U64 undefined, so NET_TLS is 0 and the file contributes
the "TLS not available" branch the product also uses without the WiFi
module; no fakes are needed.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Mbed TLS advisory 2026-11 (CVE-2026-115490): a ServerHello without
supported_versions, legacy_version 0x0304 and a TLS 1.3 suite moves a client
that offers TLS 1.2 and 1.3 into the TLS 1.3 machine with no key exchange.
ESP-IDF v6.1.1 ships the affected Mbed TLS 4.1.1. tls_engine.c refuses that
ServerHello (fail_reason VERSION_REFUSED, Ultimate status 18) but no test
sent it.

software/test/tls_engine/tls_guard_test.c includes the unmodified engine,
built against the Mbed TLS source in the espressif/idf:v6.1.1 image with
stand-ins for ESP-IDF and the entropy gate, and drives it with a scripted
attacker (two ServerHello forms) and with honest TLS 1.2 and TLS 1.3 servers
as controls. "make test" is green; "make red" builds a copy of the engine with
the guard branch disabled and fails the attack checks. Not wired into CI: it
needs the IDF image.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
When mbedTLS refuses a ServerHello with BAD_PROTOCOL_VERSION, tls_classify
reported ssl->tls_version as the version the server sent. That is the server's
version on Mbed TLS 4.1.1 but not on a 4.1.x that has the upstream fix for the
legacy_version confusion: the fixed library puts tls_version back to TLS 1.2
before it returns, so a server that sent 0x0304 would have been reported as
0x0303.

Take legacy_version from the ServerHello bytes, and the selected version of
supported_versions when the message has one, as the REQUIRE_TLS13 branch of the
same function already does. ssl->tls_version stays as the fallback for a message
that is no longer there.

Also correct a comment in tls_note_handshake: the group of a HelloRetryRequest
is not recorded. mbedTLS has cleared the message when that step returns, so the
group comes from the ServerHello that follows.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…rier

The guard test now passes on Mbed TLS 4.1.1 and on a 4.1.x with the upstream
fix for the legacy_version confusion. It asserts what holds on both: the
ServerHello is refused, with a fatal protocol_version alert, the session is
freed, and the version reported is the 0x0304 the server sent. A bare Mbed TLS
client probes which library is linked and the test prints the case that ran:
on 4.1.1 the engine's guard refuses; on the fixed library the library refuses
first and the guard is not reached.

The guard is defence in depth, an early and specific refusal, and not the only
barrier. With the guard disabled, "make test" also drives the whole attack
(ServerHello, Certificate, CertificateVerify and Finished, computed from the
client's own key schedule) and shows that the engine's end-of-handshake check
still refuses ("Negotiated group not found") and the session never reaches
ESTABLISHED. That check is a generic refusal with no fail reason, which is
what the guard improves on.

"make red" builds the engine with the guard disabled and now fails (non-zero)
if that build passes on a library that accepts the ServerHello. On the fixed
library it reports that it is not applicable.

New controls: TLS 1.3 with a HelloRetryRequest (server limited to secp384r1)
and a server offering both TLS 1.2 and 1.3 reach ESTABLISHED.

build-mbedtls.sh fails on any compile error, uses a unique container name, and
can build a scratch copy of the library with a patch applied
(MBEDTLS_PATCH). The Makefile header says how to run it (the IDF v6.1.1
image) and how the throwaway test key and certificate were generated.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@JC-000

JC-000 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Mbed TLS published 22 advisories on 16 November. ESP-IDF v6.1.1, which this PR builds on, ships Mbed TLS 4.1.1; the fixes are in 4.1.2, and no ESP-IDF release includes them yet. I went through all 22 against this PR's configuration.

One applies and is blocked by the engine: TLS version confusion. A ServerHello with a TLS 1.2 record that claims 1.3 in its body leaves the unfixed library in a TLS 1.3 session with no key exchange. tls_engine.c already refused such a ServerHello, but nothing tested that. The three commits I just pushed add the test:

  • software/test/tls_engine/tls_guard_test.c feeds the attack ServerHello to the unmodified engine, linked against the Mbed TLS that the IDF v6.1.1 image ships.
  • With the guard disabled, the session goes live at TLS 1.3 and a plaintext Certificate is accepted. With the guard, the handshake ends with a fatal alert and TLS VERSION REFUSED.
  • With the guard disabled, the forged handshake is still refused at the end, by the engine's check that a key-exchange group was negotiated. So the guard is the first of two barriers.
  • Honest TLS 1.2, TLS 1.3, TLS 1.3 with HelloRetryRequest, and dual-version servers reach an established session.
  • The refusal now reports the version from the ServerHello bytes (tls_engine.c, +15/-3). Without that, a fixed library would make the message name 1.2 for a server that sent 1.3.

The test passes on the unfixed library and on one with the 4.1.x fix applied. It is not in CI, because it needs the IDF image to build the library; the Makefile in target/pc/linux/tlsguard/ has the command. It runs on x86 with Mbed TLS defaults, not the module's sdkconfig.

The rest:

Advisories Result
18 do not apply: client only, no renegotiation, no resumption, no CRLs, no client certificate, no EC J-PAKE
1 (TLS 1.3 transform leak) applies, low: memory is leaked when an allocation or crypto call fails mid-handshake
2 (speculative execution) not determined; no untrusted code runs on the module

One thing left open: after a HelloRetryRequest the engine accepts a TLS 1.2 ServerHello, which RFC 8446 forbids. I do not see how an attacker could use it, since the Finished transcripts would differ, but I have not tested that.

When ESP-IDF picks up Mbed TLS 4.1.2 I will rebuild the modules on it and rerun the TLS gates.

Written with Claude Fable 5.2, Claude Opus 5.5 and Claude Sonnet 5.5 in Claude Code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New functionality or an improvement to existing behavior, including formats and developer tooling. network Networking and remote services: REST, Socket DMA, FTP, Telnet, and online access. uci C64-side Ultimate Command Interface, command targets, and Ultimate DOS commands; REST is network.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TLS client sockets for the U64, through its ESP32 Move the raw_u64 WiFi module firmware to ESP-IDF 6.0.3

4 participants