code wiki / _hdl_build / nx_tor_cell_gate.nx
nx_tor_cell_gate.nx source
↩ module page · 111 lines · 8814 B
1// nx_tor_cell_gate.nx -- GATE for the Tor link-protocol cell codec (tor-spec sec.3/sec.4), byte-exact vs the
2// spec cell layout. STRUCTURAL only (no live Tor -- zero external calls); the live path rides the existing
3// nx_tls13_client_session_run. anon_is_anonymizing() stays 0. license_tier: ORIGINAL expect_exit: 0
4import "nx_syscalls.nx"
5import "nx_tor_cell.nx"
6
7func g_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
8func g_n(v: i64) -> i64 { var m: i64=v; if m<0{g_w("-" as *u8);m=0-m} let t:*u8=sys_mmap(24); var k:i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} let o:*u8=sys_mmap(24); var i:i64=0; while i<k{o[i]=t[k-1-i];i=i+1} sys_write(1,o,k); return 0 }
9func g_fw(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 }
10func g_fn(fd: i64, v: i64) -> i64 { var m: i64=v; let t:*u8=sys_mmap(24); var k:i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} let o:*u8=sys_mmap(24); var i:i64=0; while i<k{o[i]=t[k-1-i];i=i+1} sys_write(fd,o,k); return 0 }
11func g_row(id: *u8, ok: i64, pass: *i64, tot: *i64) -> i64 { tot[0]=tot[0]+1; g_w(" " as *u8); g_w(id); if ok==1 { g_w(": OK\n" as *u8); pass[0]=pass[0]+1 } else { g_w(": FAIL\n" as *u8) } return 0 }
12func g_eq(a: *u8, b: *u8, n: i64) -> i64 { var i: i64=0; while i<n { if a[i]!=b[i] { return 0 } i=i+1 } return 1 }
13func g_allzero(a: *u8, off: i64, n: i64) -> i64 { var i: i64=0; while i<n { if a[off+i]!=(0 as u8) { return 0 } i=i+1 } return 1 }
14
15func main() -> i64 {
16 let pass: *i64 = sys_mmap(8) as *i64; pass[0]=0
17 let tot: *i64 = sys_mmap(8) as *i64; tot[0]=0
18 g_w("=== NX-TOR-CELL GATE (tor-spec sec.3/sec.4 link cells: byte-exact framing + handshake cells) ===\n" as *u8)
19
20 // CT1: VERSIONS [3,4,5] byte-exact == 00 00 07 00 06 00 03 00 04 00 05
21 let vers: *i64 = sys_mmap(8*8) as *i64; vers[0]=3; vers[1]=4; vers[2]=5
22 let vout: *u8 = sys_mmap(64); let vlen: i64 = tc_build_versions(vers, 3, vout)
23 let vexp: *u8 = sys_mmap(16); vexp[0]=0x00;vexp[1]=0x00;vexp[2]=0x07;vexp[3]=0x00;vexp[4]=0x06;vexp[5]=0x00;vexp[6]=0x03;vexp[7]=0x00;vexp[8]=0x04;vexp[9]=0x00;vexp[10]=0x05
24 var ct1: i64=0; if vlen==11 { if g_eq(vout, vexp, 11)==1 { ct1=1 } }
25 g_row("CT1 VERSIONS[3,4,5] byte-exact (0000 07 0006 0003 0004 0005)" as *u8, ct1, pass, tot)
26
27 // CT2: VERSIONS round-trip build->parse
28 let pv: *i64 = sys_mmap(8*8) as *i64; let nrt: i64 = tc_parse_versions(vout, vlen, pv, 8)
29 var ct2: i64=0; if nrt==3 { if pv[0]==3 { if pv[1]==4 { if pv[2]==5 { ct2=1 } } } }
30 g_row("CT2 VERSIONS round-trip parse -> [3,4,5]" as *u8, ct2, pass, tot)
31
32 // CT3: version negotiation highest-common([3,4,5],[2,4]) == 4
33 let vb: *i64 = sys_mmap(8*8) as *i64; vb[0]=2; vb[1]=4
34 var ct3: i64=0; if tc_negotiate_version(vers, 3, vb, 2)==4 { ct3=1 }
35 g_row("CT3 link-version negotiation highest-common -> 4" as *u8, ct3, pass, tot)
36
37 // CT4: FIXED cell circid_len=4, circid=1, cmd=NETINFO(8), payload 'AB' -> 514B, header + padding exact
38 let payAB: *u8 = sys_mmap(8); payAB[0]=0x41; payAB[1]=0x42
39 let f4: *u8 = sys_mmap(600); let f4len: i64 = tc_build_fixed(1, TCELL_NETINFO, payAB, 2, 4, f4)
40 var ct4: i64=1
41 if f4len!=514 { ct4=0 }
42 if (f4[0])!=(0 as u8) { ct4=0 } if (f4[3])!=(1 as u8) { ct4=0 } if (f4[4])!=(8 as u8) { ct4=0 }
43 if (f4[5])!=(0x41 as u8) { ct4=0 } if (f4[6])!=(0x42 as u8) { ct4=0 }
44 if g_allzero(f4, 7, 507)!=1 { ct4=0 } // payload[2..509) zero-padded
45 g_row("CT4 FIXED cell v4+ (CircID4) == 514B, header+payload+zero-pad exact" as *u8, ct4, pass, tot)
46
47 // CT5: FIXED cell circid_len=2 -> 512B (the classic 512-byte cell, link v<=3)
48 let f2: *u8 = sys_mmap(600); let f2len: i64 = tc_build_fixed(0, TCELL_NETINFO, payAB, 2, 2, f2)
49 var ct5: i64=0; if f2len==512 { ct5=1 }
50 g_row("CT5 FIXED cell v<=3 (CircID2) == 512B" as *u8, ct5, pass, tot)
51
52 // CT6: VARIABLE cell circid_len=2, cmd=CERTS(129), payload 'XYZ' -> 00 00 81 00 03 58 59 5A
53 let payXYZ: *u8 = sys_mmap(8); payXYZ[0]=0x58; payXYZ[1]=0x59; payXYZ[2]=0x5A
54 let vc: *u8 = sys_mmap(64); let vclen: i64 = tc_build_var(0, TCELL_CERTS, payXYZ, 3, 2, vc)
55 let vcexp: *u8 = sys_mmap(16); vcexp[0]=0x00;vcexp[1]=0x00;vcexp[2]=0x81;vcexp[3]=0x00;vcexp[4]=0x03;vcexp[5]=0x58;vcexp[6]=0x59;vcexp[7]=0x5A
56 var ct6: i64=0; if vclen==8 { if g_eq(vc, vcexp, 8)==1 { ct6=1 } }
57 g_row("CT6 VARIABLE cell byte-exact (0000 81 0003 585 95A)" as *u8, ct6, pass, tot)
58
59 // CT7: parse_var round-trip on CT6
60 let ocmd: *i64 = sys_mmap(8) as *i64; let ooff: *i64 = sys_mmap(8) as *i64; let oplen: *i64 = sys_mmap(8) as *i64
61 let tlen: i64 = tc_parse_var(vc, vclen, 2, ocmd, ooff, oplen)
62 var ct7: i64=0; if tlen==8 { if ocmd[0]==129 { if oplen[0]==3 { if ooff[0]==5 { ct7=1 } } } }
63 g_row("CT7 parse_var round-trip (cmd=129 len=3 ploff=5 total=8)" as *u8, ct7, pass, tot)
64
65 // CT8: NETINFO build (circid_len=4, ts=0x11223344, other=127.0.0.1) header+payload exact, 514B
66 let ip: *u8 = sys_mmap(4); ip[0]=0x7f; ip[1]=0x00; ip[2]=0x00; ip[3]=0x01
67 let ni: *u8 = sys_mmap(600); let nilen: i64 = tc_build_netinfo(4, 0x11223344, ip, ni)
68 let niexp: *u8 = sys_mmap(24)
69 niexp[0]=0x00;niexp[1]=0x00;niexp[2]=0x00;niexp[3]=0x00;niexp[4]=0x08;niexp[5]=0x11;niexp[6]=0x22;niexp[7]=0x33;niexp[8]=0x44;niexp[9]=0x04;niexp[10]=0x04;niexp[11]=0x7f;niexp[12]=0x00;niexp[13]=0x00;niexp[14]=0x01;niexp[15]=0x00
70 var ct8: i64=0; if nilen==514 { if g_eq(ni, niexp, 16)==1 { if g_allzero(ni, 16, 498)==1 { ct8=1 } } }
71 g_row("CT8 NETINFO build byte-exact (ts|IPv4 other-addr|0 my-addrs), 514B" as *u8, ct8, pass, tot)
72
73 // CT9: NETINFO parse round-trip (payload = ni + 5-byte header)
74 let ots: *i64 = sys_mmap(8) as *i64; let oat: *i64 = sys_mmap(8) as *i64; let oal: *i64 = sys_mmap(8) as *i64; let oad: *u8 = sys_mmap(16)
75 let npr: i64 = tc_parse_netinfo((ni as i64 + 5) as *u8, 509, ots, oat, oad, oal)
76 var ct9: i64=0; if npr==0 { if ots[0]==0x11223344 { if oat[0]==4 { if oal[0]==4 { if oad[0]==(0x7f as u8) { if oad[3]==(0x01 as u8) { ct9=1 } } } } } }
77 g_row("CT9 NETINFO parse round-trip (ts + other IPv4 7f000001)" as *u8, ct9, pass, tot)
78
79 // CT10: CERTS parse: [02][01 0002 4142][02 0003 434445] -> 2 certs
80 let cp: *u8 = sys_mmap(32); cp[0]=0x02; cp[1]=0x01;cp[2]=0x00;cp[3]=0x02;cp[4]=0x41;cp[5]=0x42; cp[6]=0x02;cp[7]=0x00;cp[8]=0x03;cp[9]=0x43;cp[10]=0x44;cp[11]=0x45
81 let cty: *i64 = sys_mmap(8*8) as *i64; let cln: *i64 = sys_mmap(8*8) as *i64
82 let ncerts: i64 = tc_parse_certs(cp, 12, cty, cln, 8)
83 var ct10: i64=0; if ncerts==2 { if cty[0]==1 { if cln[0]==2 { if cty[1]==2 { if cln[1]==3 { ct10=1 } } } } }
84 g_row("CT10 CERTS parse -> 2 certs (types/lens exact)" as *u8, ct10, pass, tot)
85
86 // CT11: AUTH_CHALLENGE parse: [32xAA][0002][0001][0003] -> challenge + 2 methods
87 let acp: *u8 = sys_mmap(64); var ai: i64=0; while ai<32 { acp[ai]=0xAA as u8; ai=ai+1 } acp[32]=0x00;acp[33]=0x02;acp[34]=0x00;acp[35]=0x01;acp[36]=0x00;acp[37]=0x03
88 let chal: *u8 = sys_mmap(32); let meth: *i64 = sys_mmap(8*8) as *i64
89 let nmeth: i64 = tc_parse_auth_challenge(acp, 38, chal, meth, 8)
90 var ct11: i64=0; if nmeth==2 { if (chal[0]&0xff)==0xAA { if (chal[31]&0xff)==0xAA { if meth[0]==1 { if meth[1]==3 { ct11=1 } } } } }
91 g_row("CT11 AUTH_CHALLENGE parse -> 32B challenge + methods [1,3]" as *u8, ct11, pass, tot)
92
93 // CT12: negative -- truncated VERSIONS (declares plen=6, only 4 payload bytes present) -> reject
94 let tv: *u8 = sys_mmap(16); tv[0]=0x00;tv[1]=0x00;tv[2]=0x07;tv[3]=0x00;tv[4]=0x06;tv[5]=0x00;tv[6]=0x03;tv[7]=0x00;tv[8]=0x04
95 var ct12: i64=0; if tc_parse_versions(tv, 9, pv, 8) < 0 { ct12=1 }
96 g_row("CT12 truncated VERSIONS -> REJECTED" as *u8, ct12, pass, tot)
97
98 // CT13: negative -- CERTS cert length overruns the payload -> reject
99 let co: *u8 = sys_mmap(16); co[0]=0x01; co[1]=0x01;co[2]=0x00;co[3]=0x05;co[4]=0x41;co[5]=0x42
100 var ct13: i64=0; if tc_parse_certs(co, 6, cty, cln, 8) < 0 { ct13=1 }
101 g_row("CT13 CERTS overrun -> REJECTED" as *u8, ct13, pass, tot)
102
103 g_w("TOR-CELL-GATE rows=" as *u8); g_n(tot[0]); g_w(" pass=" as *u8); g_n(pass[0])
104 if pass[0]==tot[0] {
105 g_w(" verdict=GREEN\n" as *u8)
106 let lg: i64 = sys_openat_append("knowledge/status/tor_cell_gate.log" as *u8, 0x1a4)
107 if lg>=0 { g_fw(lg, "TOR-CELL-GATE pass=" as *u8); g_fn(lg, pass[0]); g_fw(lg, "/" as *u8); g_fn(lg, tot[0]); g_fw(lg, " verdict=GREEN scope=cell-framing-byte-exact-structural(VERSIONS/NETINFO/CERTS/AUTH_CHALLENGE; NOT-live-tor) epoch=" as *u8); g_fn(lg, sys_now_realtime_sec()); g_fw(lg, "\n" as *u8); sys_close(lg) }
108 sys_exit(0); return 0
109 }
110 g_w(" verdict=RED\n" as *u8); sys_exit(1); return 1
111}