code wiki / _hdl_build / nx_media_crypt_wasm_gate.nx
nx_media_crypt_wasm_gate.nx source
↩ module page · 81 lines · 4600 B
1// nx_media_crypt_wasm_gate.nx -- proves the sovereign ChaCha20 cipher runs CORRECTLY IN WASM: compile
2// nx_media_crypt_wasm.nx -> wat -> wasm (sovereign pipeline), load in nx_wasm_vm, run RFC 8439 sec2.4.2
3// encryption in the VM's linear memory, compare to the standard's ciphertext. This is the browser lane,
4// bit-identical to the native KAT. NISHI all the way (fork sovereign compilers -> nx_wasm_vm). ORIGINAL
5import "nx_syscalls.nx"
6import "nx_gate_emit_lib.nx"
7import "nx_wasm_vm.nx"
8import "nx_media_crypt.nx" // mc_hexval for the expected-ciphertext hexdec
9
10const CWAT: *u8 = "/tmp/nx_compile_wat.sov.elf"
11const WATC: *u8 = "/tmp/nx_wat_compiler.sov.elf"
12const SRCNX: *u8 = "/mnt/c/Users/elder/nishi-core/nxc2/runtime/nx_media_crypt_wasm.nx"
13const OWAT: *u8 = "/tmp/mcw.wat"
14const OWASM: *u8 = "/tmp/mcw.wasm"
15
16func run_elf(elf: *u8, a1: *u8, a2: *u8) -> i64 {
17 let pid: i64 = sys_fork()
18 if pid == 0 {
19 let argv: *i64 = sys_mmap(64) as *i64
20 argv[0]=elf as i64; argv[1]=a1 as i64; argv[2]=a2 as i64; argv[3]=0
21 let envp: *i64 = sys_mmap(16) as *i64
22 envp[0]="PATH=/usr/bin:/bin" as *u8 as i64; envp[1]=0
23 sys_execve(elf, argv, envp); sys_exit(127)
24 }
25 let st: *i64 = sys_mmap(16) as *i64; sys_wait4(pid, st, 0)
26 return (st[0] >> 8) & 0xff }
27func mw(mod: *WasmMod, addr: i64, byte: i64) -> i64 { let m: *u8 = ((mod.mem as i64)+addr) as *u8; m[0]=byte as u8; return 0 }
28func mr(mod: *WasmMod, addr: i64) -> i64 { let m: *u8 = ((mod.mem as i64)+addr) as *u8; return m[0] & 0xff }
29func hexdec(hex: *u8, nbytes: i64, out: *u8) -> i64 {
30 var i: i64=0; while i<nbytes { out[i]=((mc_hexval(hex[i*2]&0xff)<<4)|mc_hexval(hex[i*2+1]&0xff)) as u8; i=i+1 } return 0 }
31
32func main() -> i64 {
33 g_puts("=== nx_media_crypt WASM gate (sovereign compile -> nx_wasm_vm: ChaCha20 vs RFC 8439) ===\n" as *u8)
34 var pass: i64 = 0; var total: i64 = 0
35
36 let rc1: i64 = run_elf(CWAT, SRCNX, OWAT)
37 g_puts(" [build] nx_compile_wat rc=" as *u8); g_pn(rc1); g_puts("\n" as *u8)
38 let rc2: i64 = run_elf(WATC, OWAT, OWASM)
39 g_puts(" [build] nx_wat_compiler rc=" as *u8); g_pn(rc2); g_puts("\n" as *u8)
40
41 let box: *i64 = sys_mmap(16) as *i64
42 let wasm: *u8 = sys_read_file(OWASM, box)
43 if (wasm as i64)==0 { g_puts(" FAIL cannot read mcw.wasm\nMCW-WASM RED\n" as *u8); return 1 }
44 g_puts(" [measure] wasm bytes=" as *u8); g_pn(box[0]); g_puts("\n" as *u8)
45 let mod: *WasmMod = wm_new(wasm, box[0])
46 if wm_parse(mod) != 0 { g_puts(" FAIL wasm parse\nMCW-WASM RED\n" as *u8); return 1 }
47 mod.mem = sys_mmap(131072) as *u8
48 pass = pass + g_check("cipher compiled to wasm + mcw_xform exported" as *u8, wm_find_export(mod, "mcw_xform" as *u8) >= 0); total = total + 1
49
50 // RFC 8439 sec2.4.2: key 00..1f @0x100, nonce 00..004a..00 @0x140, plaintext(114) @0x200, counter 1
51 var i: i64 = 0
52 while i < 32 { mw(mod, 0x100 + i, i); i = i + 1 }
53 i = 0
54 while i < 12 { mw(mod, 0x140 + i, 0); i = i + 1 }
55 mw(mod, 0x140 + 7, 0x4a)
56 let pt: *u8 = "Ladies and Gentlemen of the class of '99: If I could offer you only one tip for the future, sunscreen would be it." as *u8
57 var pl: i64 = 0; while pt[pl]!=(0 as u8){pl=pl+1}
58 i = 0
59 while i < pl { mw(mod, 0x200 + i, pt[i] & 0xff); i = i + 1 }
60
61 // encrypt IN THE VM: mcw_xform(key=0x100, nonce=0x140, ctr0=1, data=0x200, len=pl)
62 wm_run(mod, "mcw_xform" as *u8, 0x100, 0x140, 1, 0x200, pl, 5)
63
64 let cexp: *u8 = sys_mmap(256)
65 hexdec("6e2e359a2568f98041ba0728dd0d6981e97e7aec1d4360c20a27afccfd9fae0bf91b65c5524733ab8f593dabcd62b3571639d624e65152ab8f530c359f0861d807ca0dbf500d6a6156a38e088a22b65e52bc514d16ccf806818ce91ab77937365af90bbf74a35be6b40b8eedf2785e42874d" as *u8, 114, cexp)
66 var okc: i64 = 1
67 i = 0
68 while i < pl { if mr(mod, 0x200 + i) != (cexp[i] & 0xff) { okc = 0; i = pl } else { i = i + 1 } }
69 pass = pass + g_check("WASM ChaCha20 == RFC 8439 2.4.2 ciphertext (in-VM)" as *u8, okc); total = total + 1
70
71 // decrypt round-trip IN THE VM: xform again -> back to plaintext
72 wm_run(mod, "mcw_xform" as *u8, 0x100, 0x140, 1, 0x200, pl, 5)
73 var okr: i64 = 1
74 i = 0
75 while i < pl { if mr(mod, 0x200 + i) != (pt[i] & 0xff) { okr = 0; i = pl } else { i = i + 1 } }
76 pass = pass + g_check("WASM decrypt round-trip recovers plaintext (in-VM)" as *u8, okr); total = total + 1
77
78 g_puts("MCW-WASM: " as *u8); g_pn(pass); g_puts("/" as *u8); g_pn(total)
79 if pass == total { g_puts(" ALL GREEN -- sovereign E2EE cipher runs correct IN WASM\n" as *u8); return 0 }
80 g_puts(" RED\n" as *u8)
81 return 1 }