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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,10 @@ already refused a step later, as `DF_ERR_TYPE = $04`). Test:
- `net_tcp_set_recv_cb` is an RTS stub. Boot banner: `rr-net` under ip65,
`UCI NETWORKING` under UCI — `boot_check.py` asserts both.

### ClientHello shape

UCI CH is sectioned (`CH_SECTIONED`, follows `TLS_STREAM_DEFRAME`): `tls_ch_ks_ptr/len` (16-bit, BSS, one-shot) name a bulk key-share that `tls_ch_send_sections` streams from its own buffer between the head and tail staged in `tls_rec_buf`, as ONE record; 0 = classic X25519-only hello (all builds today). Rule: arm `tls_ch_ks_ptr/len` after TCP connect, immediately before `jsr tls_connect`; the sender clears it on every exit. HRR random -> `tls_sh_err=$01` (both backends). Test: `test_tls_handshake.py` [2d-2k], [3d].

### VIC-II blanking — worth 6.3%, not the fleet's "20-25%"

`src/vic.s` `vic_blank`/`vic_unblank` wrap the two X25519 scalar mults, the
Expand Down
12 changes: 12 additions & 0 deletions src/constants.inc
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,15 @@ HTTP_BUF_SIZE = 256 ; HTTP request/response line buffer
; BASIC ROM shadow and I/O). Declared here as an equate rather than in
; data.asm so the PRG/BSS stays small — the RAM exists regardless.
tcp_recv_buf = $c000

; CH_SECTIONED: the sectioned/hybrid ClientHello (16-bit bulk key-share
; section, supported_groups 11EC,001D). Build-time switch, NOT a Makefile
; knob: it follows TLS_STREAM_DEFRAME (UCI only). ip65 has no bytes to
; spare and is excluded from PQ. Note a ~1.2 KB hybrid ServerHello is NOT
; deframed today (tls_recv_server_hello is single-record; a server fragments
; it under MFL=512): that is the next phase (ServerHello on the streaming
; deframer). Without CH_SECTIONED the builder still uses the same templates
; and emits the X25519-only hello, byte-identical.
.ifdef TLS_STREAM_DEFRAME
CH_SECTIONED = 1
.endif
130 changes: 130 additions & 0 deletions src/tls13.s
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@
; --- ClientHello / ServerHello builders & parsers (tls_handshake) ---
.import tls_build_client_hello
.import tls_parse_server_hello
.ifdef CH_SECTIONED
.import tls_ch_ks_ptr
.import tls_ch_ks_len
.import tls_ch_split
.import tls_rec_header
.endif

; --- Transcript hash (tls_transcript.s) ---
.import tls_transcript_init
Expand Down Expand Up @@ -423,6 +429,21 @@ tls_close:
; =============================================================================
; tls_send_client_hello - build and send ClientHello, init transcript
; Output: C=0 success, C=1 send failure
;
; Two paths. tls_ch_ks_len = 0 (every build today): the whole message sits
; in tls_rec_buf and goes out exactly as it always has. tls_ch_ks_len != 0:
; the message is SECTIONED (see tls_build_client_hello) and tls_ch_send_
; sections streams it. Either way ONE TLS record carries the whole
; ClientHello: RFC 8446 5.1 would allow fragmenting it, but the CH is sent
; before any max_fragment_length agreement (MFL, RFC 6066 4), so the 2^14
; record bound applies to it and a ~1.4 KB hello is a legal single record
; with no reassembly needed in the peer.
; Arming contract: set tls_ch_ks_ptr/len immediately before jsr tls_connect,
; i.e. after DNS and TCP connect succeeded. tls_connect reaches this routine
; with no branch in between, and the sectioned sender clears the length on
; every exit (success or send failure), so nothing stays armed past one
; ClientHello. A clear at tls_connect ENTRY cannot help: the caller arms
; before the call, so an entry clear would also discard a fresh arm.
; =============================================================================
tls_send_client_hello:
; initialize transcript hash
Expand All @@ -431,6 +452,13 @@ tls_send_client_hello:
; build ClientHello directly into tls_rec_buf, sets tls_rec_len
jsr tls_build_client_hello

.ifdef CH_SECTIONED
lda tls_ch_ks_len
ora tls_ch_ks_len+1
beq @classic
jmp tls_ch_send_sections
@classic:
.endif
; send as plaintext handshake record
lda #TLS_CT_HANDSHAKE
jsr tls_record_send_plaintext
Expand All @@ -455,6 +483,108 @@ tls_send_client_hello:
sec
rts

; =============================================================================
; tls_ch_send_sections - stream a sectioned ClientHello as ONE record.
; Wire order: record header (5, not hashed), head, bulk, tail; each section is
; folded into the transcript right after it is sent, in wire order (the
; SHA-256 block buffer makes the fold split-point independent).
; Clears tls_ch_ks_len (one-shot). Output: C=0 success, C=1 send failure.
; Lives in TLS_CODE: jsr/jmp-only, off the ip65 LOADER.
; =============================================================================
.ifdef CH_SECTIONED
.segment "TLS_CODE"
tls_ch_send_sections:
lda #TLS_CT_HANDSHAKE
sta tls_rec_header
lda #$03
sta tls_rec_header+1
sta tls_rec_header+2
; record length = staged bytes + bulk (16-bit)
lda tls_rec_len
clc
adc tls_ch_ks_len
sta tls_rec_header+4
lda tls_rec_len+1
adc tls_ch_ks_len+1
sta tls_rec_header+3
lda #5
sta net_send_len
lda #0
sta net_send_len+1
lda #<tls_rec_header
ldx #>tls_rec_header
jsr net_tcp_send
bcs @cs_done

; head = tls_rec_buf[0 .. split)
lda tls_ch_split
sta net_send_len
lda #0
sta net_send_len+1
lda #<tls_rec_buf
ldx #>tls_rec_buf
jsr tls_ch_section
bcs @cs_done

; bulk = (tls_ch_ks_ptr), tls_ch_ks_len bytes
lda tls_ch_ks_len
sta net_send_len
lda tls_ch_ks_len+1
sta net_send_len+1
lda tls_ch_ks_ptr
ldx tls_ch_ks_ptr+1
jsr tls_ch_section
bcs @cs_done

; tail = tls_rec_buf[split .. tls_rec_len)
lda tls_rec_len
sec
sbc tls_ch_split
sta net_send_len
lda tls_rec_len+1
sbc #0
sta net_send_len+1
lda #<tls_rec_buf
clc
adc tls_ch_split
pha
lda #>tls_rec_buf
adc #0
tax
pla
jsr tls_ch_section
@cs_done:
php
lda #0
sta tls_ch_ks_len ; one-shot
sta tls_ch_ks_len+1
plp
rts

; tls_ch_section - send net_send_len bytes at A/X, then fold them into the
; transcript. C=1 on send failure (nothing folded).
tls_ch_section:
sta ch_sec_ptr ; net_tcp_send may use zp_ptr
stx ch_sec_ptr+1
jsr net_tcp_send
bcs @sec_ret
lda ch_sec_ptr
sta zp_ptr
lda ch_sec_ptr+1
sta zp_ptr+1
lda net_send_len
sta zp_count
lda net_send_len+1
sta zp_count+1
jsr tls_transcript_update
clc
@sec_ret:
rts
.segment "BSS"
ch_sec_ptr: .res 2
.segment "CODE"
.endif

; =============================================================================
; tls_recv_server_hello - receive and parse ServerHello, update transcript
; Output: C=0 success, C=1 timeout or parse error
Expand Down
Loading