code wiki / (root) / nx_e2e.nx

nx_e2e.nx source

↩ module page · 31 lines · 2157 B

1// nx_e2e.nx -- sovereign END-TO-END ENCRYPTION session for private calls/messages (the "pure E2E, never on a 2// server" requirement). X25519 ECDH key agreement -> a session key both endpoints derive INDEPENDENTLY -> all 3// relayed media (video frames, audio, chat) sealed with ChaCha20-Poly1305 AEAD. The relay daemon carries ONLY 4// ciphertext + tag; it has no private key, so it can never read the call. Contacts (a peer's name + public key) 5// live in the browser's local store ONLY, never POSTed. Composes nx_x25519 + nx_chacha20_poly1305. license_tier: ORIGINAL 6import "nx_x25519.nx" 7import "nx_chacha20_poly1305.nx" 8 9// X25519 base point u=9 (32 bytes: 0x09 then zeros) 10func e2e_base(out: *u8) -> i64 { out[0] = 9 as u8; var i: i64 = 1; while i < 32 { out[i] = 0 as u8; i = i + 1 } return 0 } 11// public key = X25519(private, base). priv/pub are 32 bytes; private key NEVER leaves the device. 12func e2e_pub(priv: *u8, pub_out: *u8) -> i64 { 13 let base: *u8 = sys_mmap(32) 14 e2e_base(base) 15 return x25519(priv, base, pub_out) 16} 17// 32-byte session key = X25519(my_private, peer_public). Both sides compute the SAME key (ECDH symmetry) without 18// it ever crossing the wire. (Production hardening: HKDF-Extract this secret before use; the agreement property 19// proven here is identical -- HKDF only adds domain separation/hygiene.) 20func e2e_session_key(my_priv: *u8, peer_pub: *u8, key_out: *u8) -> i64 { return x25519(my_priv, peer_pub, key_out) } 21// SEAL: AEAD-encrypt a payload with the session key -> ciphertext + 16-byte tag. returns 0. 22func e2e_seal(key: *u8, nonce: *u8, pt: *u8, n: i64, ct_out: *u8, tag_out: *u8) -> i64 { 23 return nx_chacha20_poly1305_encrypt(key, nonce, 0 as *u8, 0, pt, n, ct_out, tag_out) 24} 25// OPEN: AEAD-decrypt + verify the tag -> plaintext. returns 1 if authentic, 0 if tampered or wrong key 26// (so the relay, or anyone without the shared key, gets nothing). 27func e2e_open(key: *u8, nonce: *u8, ct: *u8, n: i64, tag: *u8, pt_out: *u8) -> i64 { 28 let v: i64 = nx_chacha20_poly1305_decrypt(key, nonce, 0 as *u8, 0, ct, n, tag, pt_out) 29 if v == 1 { return 1 } // NX_AEAD_VERDICT_OK 30 return 0 31}