code wiki / (root) / nx_mse_wire.nx

nx_mse_wire.nx

buildroot/runtime/nx_mse_wire.nx

15496 B218 linesdepth 6pulls 10 transitivereach 15 importersview sourcekind librarytopic mse
docsdependenciesstructsconstsfunctions

about

nx_mse_wire.nx -- REUSABLE MSE/PE wire library: a drop-in encrypted-socket layer for the torrent transport. nx_mse_handshake PROVED the crypto over loopback; this packages it as an adoptable API so the seeder (inbound) and downloader (outbound) can wrap their peer traffic with ONE small change: swap raw read/write for mse_read/mse_write on an MseCtx. A plaintext ctx (mode=0) is a passthrough, so the SAME serve loop handles both encrypted and legacy plaintext peers -> backward compatible by construction. MseCtx { mode(0=plain,1=rc4), sendS/sendij, recvS/recvij } mse_ctx_plain -> passthrough ctx (legacy peers) mse_connect -> INITIATOR handshake (outbound): fills ctx (send=keyA, recv=keyB) mse_accept -> RECEIVER handshake (inbound, known SKEY): fills ctx (send=keyB, recv=keyA) mse_accept_pfx -> receiver variant when the first Ya byte was already read (demux path) mse_read/write -> transparent decrypt/encrypt (in place); plaintext ctx = raw mse_classify_first_byte -> 0x13 => plaintext BT handshake, else => MSE (Ya) license_tier: ORIGINAL module: nishi-core.torrent.mse_wire depends: mse_keys (dh+rc4+sha1), entropy

dependencies 2 imports · 4 importers

nx_mse_keys.nx nx_entropy.nx nx_mse_wire.nx nx_mse_scan_gate.nx nx_mse_wire_gate.nx nx_torrent_get.nx nx_torrent_seed.nx

imports: nx_mse_keys.nxnx_entropy.nx

imported by: nx_mse_scan_gate.nxnx_mse_wire_gate.nxnx_torrent_get.nxnx_torrent_seed.nx

structs

37struct MseCtx

consts

19const MSE_MAGIC_1024: i64 = 1024
20const MSE_MAGIC_1200: i64 = 1200
44const MSE_CTX_BYTES: i64 = 48 // 5 i64 fields + slack

functions

24func mse_gen_priv(x: *i64) -> i64
46func mw_read_n(fd: i64, buf: *u8, n: i64) -> i64 { var g: i64=0; while g<n { let r: i64=sys_read(fd, buf+g, n-g); if r<=0 { return 0-1 } g=g+r } return g }
47func mw_write_n(fd: i64, buf: *u8, n: i64) -> i64 { var o: i64=0; while o<n { let w: i64=sys_write(fd, buf+o, n-o); if w<=0 { return 0-1 } o=o+w } return n }
48func mw_put4(b: *u8, o: i64, v: i64) -> i64 { b[o]=((v>>24)&0xff) as u8; b[o+1]=((v>>16)&0xff) as u8; b[o+2]=((v>>8)&0xff) as u8; b[o+3]=(v&0xff) as u8; return o+4 }
49func mw_memeq(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 }
50func mw_xor20(a: *u8, b: *u8, out: *u8) -> i64 { var i: i64=0; while i<20 { out[i]=((a[i] as i64) ^ (b[i] as i64)) as u8; i=i+1 } return 0 }
55func mw_recv_scan(fd: i64, pat: *u8, patlen: i64, maxskip: i64) -> i64
68func mw_skip_dec(ctx: *MseCtx, fd: i64, n: i64) -> i64
77func mw_pad_gen(out: *u8) -> i64
85func mw_rc4_stream(key20: *u8, out_s: *i64, out_ij: *i64) -> i64
92func mse_ctx_plain(ctx: *MseCtx) -> i64 { ctx.mode=0; ctx.sendS=0; ctx.sendij=0; ctx.recvS=0; ctx.recvij=0; return 0 }
95func mw_ctx_from_keys(ctx: *MseCtx, send_key: *u8, recv_key: *u8) -> i64
calls 1: mw_rc4_stream
105func mse_write(ctx: *MseCtx, fd: i64, buf: *u8, n: i64) -> i64
110func mse_read(ctx: *MseCtx, fd: i64, buf: *u8, n: i64) -> i64
117func mse_classify_first_byte(b: i64) -> i64 { if b == 0x13 { return 0 } return 1 }
121func mse_connect(fd: i64, skey: *u8, xpriv: *i64, ctx: *MseCtx) -> i64
164func mse_accept_dh(fd: i64, xpriv: *i64, prefix: *u8, prefix_len: i64, out_sbytes: *u8, out_xor: *u8) -> i64
180func mse_skey_target(sbytes: *u8, xr: *u8, out_target: *u8) -> i64
185func mse_skey_matches(skey: *u8, target: *u8) -> i64 { let h2: *u8 = sys_mmap(24); mk_hash_pref("req2" as *u8, skey, 20, h2); return mw_memeq(h2, target, 20) }
188func mse_accept_finish(fd: i64, skey: *u8, sbytes: *u8, ctx: *MseCtx) -> i64
211func mse_accept_pfx(fd: i64, skey: *u8, xpriv: *i64, prefix: *u8, prefix_len: i64, ctx: *MseCtx) -> i64
218func mse_accept(fd: i64, skey: *u8, xpriv: *i64, ctx: *MseCtx) -> i64 { return mse_accept_pfx(fd, skey, xpriv, 0 as *u8, 0, ctx) }
calls 1: mse_accept_pfx