Repository navigation
Conversation
|
I thought that this approach was already under dispute for its architecture. Why is this brought up again without settling on the architecture? |
|
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. |
|
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. |
|
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 |
|
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 ( 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 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. |
|
@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 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:
I'll post the numbers here. Written with Claude Opus 5.5 in Claude Code. |
|
@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 The peer directory includes 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. |
|
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
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. |
|
@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:
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 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:
This PR's own TLS suite ( 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. |
|
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. |
|
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
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. |
…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>
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>
|
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:
Written with Claude Opus 5.5 in Claude Code. |
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>
|
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.
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 The rest:
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. |
Closes #995. Part of #952.
Draft. This PR is the top of a stack and does not build on current
masterwithout the PRs below it: #1028 (u64ctrlon ESP-IDF 6.1.1), #996 (raw_u64on 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 answers90,TLS NOT AVAILABLE(or 81, for a malformed$21/$24with the no-reply bit;doc/uci_tls.md). The engine is built into theraw_u64module only. The firmware gate is#if (U64 == 1) && defined(NIOS)(network_target.h:59), so the Elite II and C64 Ultimate, whose module isu64ctrl, answer 90.Change
The protocol is specified in
doc/uci_tls.mdin this PR.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'ssdkconfigpoints 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'scertdataand theRELEASE=1failure on a stale file; master's test keeps the age limit and the hash pin.software/u64ctrl/ca/README.mdnow says that a refresh also bumpsraw_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 andtls_engine.cnow say where the Mozilla data and the Mbed TLS source come from.software/wifi/tls/tls_engine.con 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 calls0x30to0x35(software/wifi/tls/rpc_tls.h). The ESP-side TLS code sits insoftware/wifi/tls/, not inraw_u64/main, so the Elite II / C64 Ultimate module (u64ctrl) can build the same files later. At most two sessions (NET_MAX_TLS).$21to$25in network target 3, in a new file,software/io/network/network_target_tls.cc(1,661 lines).network_target.ccgains 52 lines.READ_SOCKET,WRITE_SOCKETandCLOSE_SOCKETwork unchanged on a TLS handle. A status is clamped to 255 bytes, because the status length register is 8 bits.$21OPEN_TLS$22TLS_TRUST_CA$23TLS_INFO$FF), or a session's version, suite, group and key hash$24START_TLS$25TLS_RELEASEOPEN_TLSthe client abortedWhy 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 andUNSENTadditions towifi_dedicated.cc/.h(the dedicated call channel that #953 introduces).GET_RANDOMitself is #953. The TLS reply path uses the thread byte0xFCfor 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:
legacy_versionas 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 withprotocol_version, which is equivalent to upstream's fixff52b208fefor this engine. The host testshrr_legacy_version_13andlegacy_version_13fail without the check and pass with it.doc/uci_tls.mddocuments the case underVERSION REFUSED.sdkconfigfiles. 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
memcpyfor an empty step input inwifi_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_CHUNKdoes not exist onmaster. The hook that makes it work on a TLS handle (inwrite_socket_chunk/send_to_socket) and its$16test are in neither PR. They land with whichever of this PR and #835 merges second.doc/uci_tls.mdmarks$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.binis 958,912 bytes with the hardening above (972,464 without it; 957,424 on 6.0.3), in a 1,048,576-bytefactorypartition. 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_c3is 909,296 bytes (922,912 without the hardening) andu64ctrl1,072,992 (1,086,560). The flash is 2 MB, so a larger app partition (SINGLE_APP_LARGE) is possible later; it changespartition-table.bin, and this PR does not do it. The CA file holds 112 roots againstMAX_CERTS200.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.ccto 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 hash2a5eb4590) 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:
net_tls_test --route all --expect green --require-lock, Ethernetbulk-aes-gcm(S40), TLS bulk transferrun-tests --e2e --profile quickrun-tests --e2e --profile standarduci-net-target, deep profileESP-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 forSEND ERROR: 12, and #835 with the$16TLS 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, noGET_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:fdb59b059)net_tls_test, both routes, 46 scenario runsRED as expected over 46 scenario runs, 342s: every TLS scenario answers21,UNKNOWN COMMAND; S19e and S19e2b are N/A on the native routeGREEN 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 logged13,/12,statuses are the refusal scenarios, verdict OKuci-net-target, deep profilerun-tests --e2e --profile quickExtra 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 with94,CERTIFICATE NAME MISMATCH; a server without TLS 1.3 refused with14,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:fdb59b059)tls_c64u.py)21,UNKNOWN COMMAND$23 $FF00,OKwith the 16-byte capability record,$23 0181,$21valid 90 (TLS NOT AVAILABLE),$21malformed 81,$24unowned 81,$22with 5 bytes 81,$22with 6 bytes 90,$2500,OKThe 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_WAIToption moved from$C00Ato$C00B(tests/e2e/io/command_interface/uci_agent.asmandtests/e2e/lib/uci_native.py), because the draft #936 uses$C00AforOPT_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 writeOPT_WAITat its new address on its own (it had been written together withOPT_ABRTin 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_idf6RC=0 (102 checks, 0 failed),make host_testsfor the TLS rig exit 0 including the two hardening tests, andtests/lib/u64ctrl_ca_test.pyOK):make -C target/pc/linux/wifitls test:wifi_tls_testandnet_tls_test, 0 failures. An earlier head had 59 and 337 checks, and 343 with the$16test 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.pyandruff check --config tests/ruff.toml tests/pass.net_tls_selftest.py --quickpasses with and withoutc64_test_harnessinstalled. The harness (https://github.com/JC-000/c64-test-harness, public) is not a dependency. Without it,net_tls_test.pywarns and skips the device lock unless--require-lockis given.net_tls_test.pyis not registered inrun-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 offlineuci-net-tls-selftestis registered in the DEEP profile.tools/ca_storetests (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/testin my fork. That directory is not in this PR.Overlap with #953
This PR is stacked on #953 and uses its
wifi_dedicatedchannel.DEDICATED_BUSYmeans "no answer, or not sent" in #953 and "sent, no answer" in the TLS code, so this PR addsDEDICATED_UNSENT = 6and theGET_RANDOMwrapper maps it to the busy result (return (retval == DEDICATED_UNSENT) ? WIFI_RANDOM_BUSY : retval;), soGET_RANDOManswers 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 isNONE, 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_IDENTIFYon thread bytes0xFDto0xFF, clear of this PR's0xFC.Not tested, and known gaps
GET_RANDOManswering 88 while TLS holds the module channel was not provoked on hardware (above).net_tls_testwere not run in this round.dispatch_commandandtls_rpc_note_blockare.hacked_recvchange from the ESP-IDF PR has not run on hardware.Area contacts: barryw (UCI), chrisgleissner (
tests/,run-tests). GideonZ for the go-ahead.Written with Claude Opus 5.5 in Claude Code.