nx_mse_wire.nx
buildroot/runtime/nx_mse_wire.nx
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
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
| 37 | struct MseCtx |
consts
| 19 | const MSE_MAGIC_1024: i64 = 1024 |
| 20 | const MSE_MAGIC_1200: i64 = 1200 |
| 44 | const MSE_CTX_BYTES: i64 = 48 // 5 i64 fields + slack |
functions
| 24 | func mse_gen_priv(x: *i64) -> i64 |
| 46 | func 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 } |
| 47 | func 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 } |
| 48 | func 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 } |
| 49 | func 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 } |
| 50 | func 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 } |
| 55 | func mw_recv_scan(fd: i64, pat: *u8, patlen: i64, maxskip: i64) -> i64 |
| 68 | func mw_skip_dec(ctx: *MseCtx, fd: i64, n: i64) -> i64 |
| 77 | func mw_pad_gen(out: *u8) -> i64 |
| 85 | func mw_rc4_stream(key20: *u8, out_s: *i64, out_ij: *i64) -> i64 |
| 92 | func mse_ctx_plain(ctx: *MseCtx) -> i64 { ctx.mode=0; ctx.sendS=0; ctx.sendij=0; ctx.recvS=0; ctx.recvij=0; return 0 } |
| 95 | func mw_ctx_from_keys(ctx: *MseCtx, send_key: *u8, recv_key: *u8) -> i64 calls 1: mw_rc4_stream |
| 105 | func mse_write(ctx: *MseCtx, fd: i64, buf: *u8, n: i64) -> i64 called by 8: mainmse_accept_finishmaintg_seed_hellotg_from_peerlp_leech+2 calls 2: rc4_cryptmw_write_n |
| 110 | func mse_read(ctx: *MseCtx, fd: i64, buf: *u8, n: i64) -> i64 called by 11: mainmse_connectmse_accept_finishmaintg_read_msgtg_from_peer+5 calls 2: mw_read_nrc4_crypt |
| 117 | func mse_classify_first_byte(b: i64) -> i64 { if b == 0x13 { return 0 } return 1 } |
| 121 | func mse_connect(fd: i64, skey: *u8, xpriv: *i64, ctx: *MseCtx) -> i64 called by 4: mainmaintg_from_peerlp_live calls 17: md_load_pmse_dh_publicbi_to_bytes_bemw_write_nmw_pad_genmw_read_n+11 |
| 164 | func mse_accept_dh(fd: i64, xpriv: *i64, prefix: *u8, prefix_len: i64, out_sbytes: *u8, out_xor: *u8) -> i64 |
| 180 | func mse_skey_target(sbytes: *u8, xr: *u8, out_target: *u8) -> i64 |
| 185 | func 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) } |
| 188 | func mse_accept_finish(fd: i64, skey: *u8, sbytes: *u8, ctx: *MseCtx) -> i64 called by 2: mse_accept_pfxts_serve_peer_registry calls 8: mk_derivemw_rc4_streammse_readmw_memeqmw_skip_decmw_pad_gen+2 |
| 211 | func mse_accept_pfx(fd: i64, skey: *u8, xpriv: *i64, prefix: *u8, prefix_len: i64, ctx: *MseCtx) -> i64 called by 3: mse_acceptmaints_serve_peer calls 4: mse_accept_dhmse_skey_targetmse_skey_matchesmse_accept_finish |
| 218 | func 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 |