code wiki / _hdl_build / nx_e2e_gate.nx
nx_e2e_gate.nx source
↩ module page · 67 lines · 3931 B
1// nx_e2e_gate.nx -- proves the sovereign E2E session (nx_e2e): private calls the relay can't read. Native, no node.
2// 1) ECDH agreement: Alice and Bob independently derive the SAME session key (the E2E core)
3// 2) seal/open round-trip: Bob decrypts Alice's sealed message exactly
4// 3) RELAY/EAVESDROPPER CAN'T READ (neg): a party without the shared key (the daemon, or Eve) fails to open
5// 4) on-the-wire is ciphertext, not plaintext
6// 5) tamper detection (neg): flipping one ciphertext byte -> open rejects (Poly1305)
7// license_tier: ORIGINAL
8import "nx_syscalls.nx"
9import "nx_gate_emit_lib.nx"
10import "nx_e2e.nx"
11import "nx_gate_verdict.nx"
12
13func beq(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 }
14
15func main() -> i64 {
16 g_puts("nx_e2e gate (sovereign end-to-end encryption -- the relay can't read the call)\n" as *u8)
17 var pass: i64 = 0; var total: i64 = 0
18
19 let ap: *u8 = sys_mmap(32); let bp: *u8 = sys_mmap(32); let ep: *u8 = sys_mmap(32)
20 var i: i64 = 0
21 while i < 32 { ap[i] = ((0x11 + i) & 0xff) as u8; bp[i] = ((0x55 + i*3) & 0xff) as u8; ep[i] = ((0x99 + i*7) & 0xff) as u8; i = i + 1 }
22 let apub: *u8 = sys_mmap(32); let bpub: *u8 = sys_mmap(32)
23 e2e_pub(ap, apub) // Alice's public key (shareable / stored in a contact)
24 e2e_pub(bp, bpub) // Bob's public key
25
26 // 1) ECDH agreement: both derive the same session key
27 let akey: *u8 = sys_mmap(32); let bkey: *u8 = sys_mmap(32)
28 e2e_session_key(ap, bpub, akey) // Alice: my_priv x Bob_pub
29 e2e_session_key(bp, apub, bkey) // Bob: my_priv x Alice_pub
30 pass = pass + g_check("ECDH: Alice and Bob derive the SAME session key (E2E core)" as *u8, beq(akey, bkey, 32)); total=total+1
31
32 // 2) seal/open round-trip
33 let nonce: *u8 = sys_mmap(12); i=0; while i<12 { nonce[i] = (1 + i) as u8; i=i+1 }
34 let msg: *u8 = "hello nishi family -- private call" as *u8
35 var mlen: i64 = 0; while msg[mlen] != (0 as u8) { mlen = mlen + 1 }
36 let ct: *u8 = sys_mmap(256); let tag: *u8 = sys_mmap(16); let dec: *u8 = sys_mmap(256)
37 e2e_seal(akey, nonce, msg, mlen, ct, tag)
38 let ok: i64 = e2e_open(bkey, nonce, ct, mlen, tag, dec)
39 pass = pass + g_check("seal/open: Bob decrypts Alice's message exactly" as *u8, (ok == 1) & (beq(dec, msg, mlen) == 1)); total=total+1
40
41 // 3) the RELAY / an eavesdropper (no shared key) cannot read it
42 let ekey: *u8 = sys_mmap(32)
43 e2e_session_key(ep, apub, ekey) // Eve's key (or the server's: it has no participant private key)
44 let dec2: *u8 = sys_mmap(256)
45 let eve: i64 = e2e_open(ekey, nonce, ct, mlen, tag, dec2)
46 pass = pass + g_check("relay/eavesdropper without the key CANNOT open (neg)" as *u8, eve == 0); total=total+1
47
48 // 4) on the wire it's ciphertext, not plaintext
49 pass = pass + g_check("on-the-wire payload is ciphertext, not plaintext" as *u8, beq(ct, msg, mlen) == 0); total=total+1
50
51 // 5) tamper detection
52 ct[0] = (ct[0] ^ 0xff) as u8
53 let dec3: *u8 = sys_mmap(256)
54 let tampered: i64 = e2e_open(bkey, nonce, ct, mlen, tag, dec3)
55 pass = pass + g_check("tamper: a flipped ciphertext byte is rejected (Poly1305, neg)" as *u8, tampered == 0); total=total+1
56
57 g_puts("---- e2e gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8)
58 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
59 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
60 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
61 let ctr__dry: *i64 = gv_ctr()
62 ctr__dry[0] = pass
63 ctr__dry[1] = total
64 let rc__dry: i64 = gv_verdict("E2E-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8)
65 sys_exit(rc__dry)
66 return rc__dry
67}