code wiki / (root) / nx_connect_device_client_gate.nx

nx_connect_device_client_gate.nx source

↩ module page · 109 lines · 7034 B

1// nx_connect_device_client_gate.nx -- proves the PRIVACY-FIRST / DATA-ON-THE-ENDPOINT property on the 2// emitted device-client bytes: the user's contacts + message history are kept in the browser's own storage, 3// ENCRYPTED at rest, and the ONLY things that ever leave the device are a public key and ciphertext. There 4// is nothing private for our server to hold. Crypto correctness is separately proven on the SAME wasms by 5// nx_media_crypt_wasm_gate 3/3 (ChaCha20) and nx_connect_keyx_vm_gate 5/5 (X25519 agreement). 6// license_tier: ORIGINAL expect_exit: 0 7import "nx_syscalls.nx" 8import "nx_connect_device_client.nx" 9 10func w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 11func slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 12func has(h: *u8, hn: i64, nd: *u8) -> i64 { 13 let nl: i64=slen(nd); if nl==0 { return 0 } 14 var i: i64=0 15 while i+nl<=hn { var k: i64=0; var hit: i64=1; while k<nl { if h[i+k]!=nd[k] { hit=0; k=nl } else { k=k+1 } } if hit==1 { return 1 } i=i+1 } 16 return 0 17} 18func ck(pass: i64, label: *u8, fails: *i64) -> i64 { 19 w(" " as *u8); w(label); w(": " as *u8) 20 if pass==1 { w("PASS\n" as *u8) } else { w("FAIL\n" as *u8); fails[0]=fails[0]+1 } 21 return 0 22} 23 24func main() -> i64 { 25 let fails: *i64 = sys_mmap(16) as *i64 26 fails[0]=0 27 w("=== nx_connect_device_client_gate -- private data lives on the device, only ciphertext/pubkeys leave ===\n" as *u8) 28 let dst: *u8 = sys_mmap(262144) 29 let n: i64 = cd_emit_html("Q0hBQ0hB" as *u8, 8, "WDI1NTE5" as *u8, 8, dst) // placeholder b64s 30 31 // T1: ships BOTH sovereign wasms 32 var t1:i64=1 33 if has(dst, n, "const BC='Q0hBQ0hB'" as *u8)==0 { t1=0 } 34 if has(dst, n, "const BX='WDI1NTE5'" as *u8)==0 { t1=0 } 35 if has(dst, n, "WebAssembly.instantiate(ub(BC))" as *u8)==0 { t1=0 } 36 if has(dst, n, "WebAssembly.instantiate(ub(BX))" as *u8)==0 { t1=0 } 37 ck(t1, "T1 embeds both sovereign wasms (ChaCha20 + X25519), standard instantiate" as *u8, fails) 38 39 // T2: the DEVICE VAULT is in localStorage, and it is ENCRYPTED before it is written (never plaintext JSON) 40 var t2:i64=1 41 if has(dst, n, "localStorage.setItem('nxvault_'+ME" as *u8)==0 { t2=0 } 42 if has(dst, n, "const ct=cc(vkeyHex(),nonce,pt);localStorage.setItem('nxvault_'+ME,hx(nonce)+':'+hx(ct))" as *u8)==0 { t2=0 } 43 // NEG: JSON of the vault must never be written to storage in the clear 44 if has(dst, n, "localStorage.setItem('nxvault_'+ME,JSON.stringify" as *u8)==1 { t2=0 } 45 ck(t2, "T2 device vault: contacts+messages stored in localStorage, ChaCha20-ENCRYPTED at rest (no plaintext store)" as *u8, fails) 46 47 // T3: the vault key is derived ON-DEVICE from the private identity key and never sent 48 var t3:i64=1 49 if has(dst, n, "function vkeyHex(){return hx(PRIV);}" as *u8)==0 { t3=0 } 50 if has(dst, n, "localStorage.getItem('nxid_priv_'+n)" as *u8)==0 { t3=0 } 51 if has(dst, n, "crypto.getRandomValues(r)" as *u8)==0 { t3=0 } 52 // NEG: the private key must never appear in a fetch body 53 if has(dst, n, "body:'n='+n+'&p='+hx(PRIV)" as *u8)==1 { t3=0 } 54 if has(dst, n, "&priv=" as *u8)==1 { t3=0 } 55 ck(t3, "T3 vault key derived on-device from the private key; private key never sent" as *u8, fails) 56 57 // T4: adding a CONTACT stores it ON DEVICE and does NOT POST it to the server 58 var t4:i64=1 59 if has(dst, n, "v.contacts.push({h:to,p:peer.p});vaultSave(v)" as *u8)==0 { t4=0 } 60 // the addc handler fetches the public directory (GET) and saves locally; it must not POST the contact 61 if has(dst, n, "body:'contact=" as *u8)==1 { t4=0 } 62 if has(dst, n, "body:'addcontact" as *u8)==1 { t4=0 } 63 ck(t4, "T4 adding a contact writes it to the device vault; nothing about it is sent to the server" as *u8, fails) 64 65 // T5: SEND posts only ciphertext; the message plaintext is recorded in the device vault, never posted 66 var t5:i64=1 67 if has(dst, n, "body:'from='+ME+'&to='+to+'&seq='+seq+'&ct='+ct" as *u8)==0 { t5=0 } 68 if has(dst, n, "v.messages.push({peer:to,dir:'out',seq:seq,text:$('m').value});vaultSave(v)" as *u8)==0 { t5=0 } 69 // NEG: the plaintext must never be in a POST body 70 if has(dst, n, "&text=" as *u8)==1 { t5=0 } 71 if has(dst, n, "&msg=" as *u8)==1 { t5=0 } 72 if has(dst, n, "&plain=" as *u8)==1 { t5=0 } 73 ck(t5, "T5 send: only ciphertext (from/to/seq/ct) is POSTed; plaintext is recorded on-device only" as *u8, fails) 74 75 // T6: SYNC decrypts fetched ciphertext in-browser and writes PLAINTEXT into the device vault, not back to us 76 var t6:i64=1 77 if has(dst, n, "fetch('/connect/sealed?u='+ME)" as *u8)==0 { t6=0 } 78 if has(dst, n, "ccSeq(key,m.s,uh(m.ct))" as *u8)==0 { t6=0 } 79 if has(dst, n, "v.messages.push({peer:m.f,dir:'in',seq:m.s,text:pt});" as *u8)==0 { t6=0 } 80 // DELETE-ON-DELIVERY: after storing in the vault, the device ACKs so the server can drop its copy 81 if has(dst, n, "fetch('/connect/ack'" as *u8)==0 { t6=0 } 82 if has(dst, n, "body:'from='+m.f+'&to='+ME+'&seq='+m.s" as *u8)==0 { t6=0 } 83 ck(t6, "T6 sync: ciphertext decrypted in-browser -> plaintext to device vault -> ACK so the server deletes its copy" as *u8, fails) 84 85 // T7: the ONLY server-bound fetch bodies are pubkey publish + ciphertext seal; render reads the vault 86 var t7:i64=1 87 if has(dst, n, "body:'n='+n+'&p='+pub" as *u8)==0 { t7=0 } // publish PUBLIC key 88 if has(dst, n, "const v=vaultLoad();const cu=$('contacts')" as *u8)==0 { t7=0 } // render from the vault 89 // no crypto reimplemented in JS 90 if has(dst, n, "quarterround" as *u8)==1 { t7=0 } 91 if has(dst, n, "0x61707865" as *u8)==1 { t7=0 } 92 if has(dst, n, "121665" as *u8)==1 { t7=0 } 93 ck(t7, "T7 server sees only public key + ciphertext; the UI renders from the on-device vault; no JS crypto" as *u8, fails) 94 95 // T8: the privacy claim is STATED on the page (honesty), and the a11y landmark is present 96 var t8:i64=1 97 if has(dst, n, "live in this browser" as *u8)==0 { t8=0 } 98 if has(dst, n, "nothing private on our systems to sniff" as *u8)==0 { t8=0 } 99 if has(dst, n, "<main id=\"main\">" as *u8)==0 { t8=0 } 100 ck(t8, "T8 honest framing on-page (data-on-device, nothing to sniff) + main landmark" as *u8, fails) 101 102 w(" emitted bytes=" as *u8) 103 let nb: *u8=sys_mmap(28); var m: i64=n; var kk: i64=0; if m==0{nb[0]=48 as u8;kk=1} while m>0{nb[kk]=(48+(m%10)) as u8;m=m/10;kk=kk+1} let bb: *u8=sys_mmap(28); var j: i64=0; while j<kk{bb[j]=nb[kk-1-j];j=j+1} sys_write(1,bb,kk) 104 w("\n fails=" as *u8); m=fails[0]; kk=0; let t2b: *u8=sys_mmap(28); if m==0{t2b[0]=48 as u8;kk=1} while m>0{t2b[kk]=(48+(m%10)) as u8;m=m/10;kk=kk+1} let b2: *u8=sys_mmap(28); j=0; while j<kk{b2[j]=t2b[kk-1-j];j=j+1} sys_write(1,b2,kk); w("\n" as *u8) 105 if fails[0]==0 { w("VERDICT: verdict=GREEN (data-on-endpoint: encrypted device vault; only public keys + ciphertext leave the device)\n" as *u8); sys_exit(0) } 106 w("VERDICT: verdict=RED\n" as *u8) 107 sys_exit(1) 108 return 1 109}