code wiki / _hdl_build / nx_e2e_gate.nx
nx_e2e_gate.nx source
↩ module page · 59 lines · 3494 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"
11
12func 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 }
13
14func main() -> i64 {
15 g_puts("nx_e2e gate (sovereign end-to-end encryption -- the relay can't read the call)\n" as *u8)
16 var pass: i64 = 0; var total: i64 = 0
17
18 let ap: *u8 = sys_mmap(32); let bp: *u8 = sys_mmap(32); let ep: *u8 = sys_mmap(32)
19 var i: i64 = 0
20 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 }
21 let apub: *u8 = sys_mmap(32); let bpub: *u8 = sys_mmap(32)
22 e2e_pub(ap, apub) // Alice's public key (shareable / stored in a contact)
23 e2e_pub(bp, bpub) // Bob's public key
24
25 // 1) ECDH agreement: both derive the same session key
26 let akey: *u8 = sys_mmap(32); let bkey: *u8 = sys_mmap(32)
27 e2e_session_key(ap, bpub, akey) // Alice: my_priv x Bob_pub
28 e2e_session_key(bp, apub, bkey) // Bob: my_priv x Alice_pub
29 pass = pass + g_check("ECDH: Alice and Bob derive the SAME session key (E2E core)" as *u8, beq(akey, bkey, 32)); total=total+1
30
31 // 2) seal/open round-trip
32 let nonce: *u8 = sys_mmap(12); i=0; while i<12 { nonce[i] = (1 + i) as u8; i=i+1 }
33 let msg: *u8 = "hello nishi family -- private call" as *u8
34 var mlen: i64 = 0; while msg[mlen] != (0 as u8) { mlen = mlen + 1 }
35 let ct: *u8 = sys_mmap(256); let tag: *u8 = sys_mmap(16); let dec: *u8 = sys_mmap(256)
36 e2e_seal(akey, nonce, msg, mlen, ct, tag)
37 let ok: i64 = e2e_open(bkey, nonce, ct, mlen, tag, dec)
38 pass = pass + g_check("seal/open: Bob decrypts Alice's message exactly" as *u8, (ok == 1) & (beq(dec, msg, mlen) == 1)); total=total+1
39
40 // 3) the RELAY / an eavesdropper (no shared key) cannot read it
41 let ekey: *u8 = sys_mmap(32)
42 e2e_session_key(ep, apub, ekey) // Eve's key (or the server's: it has no participant private key)
43 let dec2: *u8 = sys_mmap(256)
44 let eve: i64 = e2e_open(ekey, nonce, ct, mlen, tag, dec2)
45 pass = pass + g_check("relay/eavesdropper without the key CANNOT open (neg)" as *u8, eve == 0); total=total+1
46
47 // 4) on the wire it's ciphertext, not plaintext
48 pass = pass + g_check("on-the-wire payload is ciphertext, not plaintext" as *u8, beq(ct, msg, mlen) == 0); total=total+1
49
50 // 5) tamper detection
51 ct[0] = (ct[0] ^ 0xff) as u8
52 let dec3: *u8 = sys_mmap(256)
53 let tampered: i64 = e2e_open(bkey, nonce, ct, mlen, tag, dec3)
54 pass = pass + g_check("tamper: a flipped ciphertext byte is rejected (Poly1305, neg)" as *u8, tampered == 0); total=total+1
55
56 g_puts("---- e2e gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8)
57 if pass == total { g_puts("verdict=GREEN\n" as *u8); sys_exit(0); return 0 }
58 g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1
59}