code wiki / _hdl_build / nx_dr_restore_verify_gate.nx

nx_dr_restore_verify_gate.nx source

↩ module page · 48 lines · 3172 B

1// nx_dr_restore_verify_gate.nx -- exceed-gate for CAP-DR-RESTORE-VERIFY. Proves the integrity-scrub logic: a 2// faithful copy re-hashes identically (verify OK), a single-byte change is DETECTED (corruption caught), and the 3// hash is a deterministic KAT (sha256("abc")). This is what a naive cp+size-check CANNOT do -> the exceed. 4// Sovereign: nx_syscalls + nx_sha256. expect_exit: 0 5import "nx_syscalls.nx" 6import "nx_sha256.nx" 7import "nx_gate_verdict.nx" 8 9func gp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 10func gn(v: i64) -> i64 { var t: i64=v; if t<0{sys_write(1,"-" as *u8,1);t=0-t} let tm:*u8=sys_mmap(24); var k:i64=0; if t==0{tm[0]=48 as u8;k=1} while t>0{tm[k]=(48+(t%10)) as u8;t=t/10;k=k+1} let b:*u8=sys_mmap(24); var j:i64=0; while j<k{b[j]=tm[k-1-j];j=j+1} sys_write(1,b,k); return 0 } 11func g_hex(buf: *u8, n: i64, out: *u8) -> i64 { 12 let dig: *u8 = sys_mmap(64); sha256_digest(buf, n, dig) 13 let hx: *u8 = "0123456789abcdef" as *u8 14 var h: i64=0; while h<32 { let by: i64=(dig[h] as i64)&0xff; out[h*2]=hx[(by>>4)&15]; out[h*2+1]=hx[by&15]; h=h+1 } out[64]=0 as u8 15 return 0 16} 17func g_eq(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 } 18 19func main(argc: i64, argv: *i64) -> i64 { 20 gp("=== nx_dr_restore_verify_gate (integrity scrub: faithful=OK, tamper=DETECTED, KAT) ===\n" as *u8) 21 var pass: i64=0; var fail: i64=0 22 let hA: *u8=sys_mmap(80); let hB: *u8=sys_mmap(80); let hC: *u8=sys_mmap(80) 23 24 // T1 a faithful copy re-hashes identically 25 g_hex("hello dr backup payload" as *u8, 23, hA) 26 g_hex("hello dr backup payload" as *u8, 23, hB) 27 if g_eq(hA, hB, 64) == 1 { pass=pass+1; gp(" T1 faithful copy -> identical hash (verify OK) PASS\n" as *u8) } else { fail=fail+1; gp(" T1 FAIL\n" as *u8) } 28 29 // T2 a single-byte change is DETECTED (this is what size-check misses -- same length, different bytes) 30 g_hex("hello dr backup payloaX" as *u8, 23, hC) 31 if g_eq(hA, hC, 64) == 0 { pass=pass+1; gp(" T2 single-byte tamper (same length) -> hash differs (CORRUPTION DETECTED) PASS\n" as *u8) } else { fail=fail+1; gp(" T2 FAIL undetected\n" as *u8) } 32 33 // T3 deterministic KAT: sha256("abc") 34 let kat: *u8 = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" as *u8 35 g_hex("abc" as *u8, 3, hA) 36 if g_eq(hA, kat, 64) == 1 { pass=pass+1; gp(" T3 sha256(\"abc\") KAT -> deterministic PASS\n" as *u8) } else { fail=fail+1; gp(" T3 FAIL kat\n" as *u8) } 37 38 gp("RESULT pass=" as *u8); gn(pass); gp(" fail=" as *u8); gn(fail) 39 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check 40 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled 41 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify. 42 let ctr__dry: *i64 = gv_ctr() 43 ctr__dry[0] = pass 44 ctr__dry[1] = pass + fail 45 let rc__dry: i64 = gv_verdict("DR-RESTORE-VERIFY-GATE" as *u8, ctr__dry, "byte-exact integrity scrub -- exceeds a cp+size-check backup)" as *u8) 46 sys_exit(rc__dry) 47 return rc__dry 48}