code wiki / _hdl_build / nx_device_cert_gate.nx

nx_device_cert_gate.nx source

↩ module page · 91 lines · 6123 B

1// nx_device_cert_gate.nx -- gate for device provisioning (nx_device_cert). Proves the challenge-response device 2// auth over the KAT-verified ed25519: a provisioned device signs a fresh challenge and is admitted; a TAMPERED 3// signature, an UNKNOWN device, a REPLAY (wrong challenge), and an IMPERSONATION (signing with a different key) 4// are all rejected. Anchors on the RFC 8032 test vector so the crypto is proven GENUINE, not a stub. Positive 5// AND negative controls. Appends "ACCESSGATE row=nx_device_cert device-provision ... verdict=PASS" to 6// knowledge/status/access_gate.log on all-pass. license_tier: ORIGINAL 7import "nx_device_cert.nx" 8import "nx_ed25519_signature.nx" 9import "nx_syscalls.nx" 10 11func dg_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 12func dg_num(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(1,bb,k); return 0 } 13func dg_cat(dst: *u8, off: i64, s: *u8) -> i64 { var o: i64=off; var k: i64=0; while s[k]!=(0 as u8){dst[o]=s[k];o=o+1;k=k+1} return o } 14func dg_catnum(dst: *u8, off: i64, v: i64) -> i64 { var o: i64=off; let t: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m}; var k: i64=0; if m==0{t[0]=48 as u8;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i: i64=0; while i<k{dst[o]=t[k-1-i];o=o+1;i=i+1} return o } 15func dg_row(id: i64, ok: i64, what: *u8) -> i64 { dg_w("DCROW " as *u8); dg_num(id); dg_w(" " as *u8); if ok==1 { dg_w("PASS " as *u8) } else { dg_w("FAIL " as *u8) } dg_w(what); dg_w("\n" as *u8); return ok } 16func dg_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 17 18func dg_hb(c: i64) -> i64 { if c>=0x30 { if c<=0x39 { return c-0x30 } } if c>=0x61 { if c<=0x66 { return c-0x61+10 } } if c>=0x41 { if c<=0x46 { return c-0x41+10 } } return 0 } 19func dg_hex2bytes(hex: *u8, nbytes: i64, out: *u8) -> i64 { var i: i64=0; while i<nbytes { let hi: i64=dg_hb(hex[i*2] as i64); let lo: i64=dg_hb(hex[i*2+1] as i64); out[i]=((hi<<4)|lo) as u8; i=i+1 } return 0 } 20func dg_byteeq(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 } 21 22func main() -> i64 { 23 // RFC 8032 test-1 seed + expected pubkey (proves the ed25519 is the real spec, not a stub) 24 let priv: *u8 = sys_mmap(32) 25 dg_hex2bytes("9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60" as *u8, 32, priv) 26 let exp_pub: *u8 = sys_mmap(32) 27 dg_hex2bytes("d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a" as *u8, 32, exp_pub) 28 let pub: *u8 = sys_mmap(32) 29 ed25519_pub_from_priv(priv, pub) 30 31 // provision device "laptop-1" with its public key 32 let ids: *i64 = sys_mmap(8*8) as *i64 33 let pubs: *i64 = sys_mmap(8*8) as *i64 34 ids[0]="laptop-1" as *u8 as i64; pubs[0]=pub as i64 35 let n: i64 = 1 36 37 // the device signs a FRESH server challenge with its private key 38 let chal: *u8 = "nishi-access-wall-nonce-00000001" as *u8 39 let clen: i64 = dg_slen(chal) 40 let sig: *u8 = sys_mmap(64) 41 ed25519_sign_full(priv, chal, clen, sig) 42 43 var pass: i64 = 0 44 var rows: i64 = 0 45 var ok: i64 = 0 46 47 // R0: genuine crypto -- derived pubkey matches the RFC 8032 vector 48 ok = dg_byteeq(pub, exp_pub, 32) 49 rows=rows+1; pass=pass+dg_row(0, ok, "ed25519 genuine: pub_from_priv matches RFC 8032 test vector" as *u8) 50 51 // R1: provisioned device signs the challenge -> device_ok 52 ok = dc_verify(ids, pubs, n, "laptop-1" as *u8, chal, clen, sig) 53 rows=rows+1; pass=pass+dg_row(1, ok, "provisioned device proves possession -> device_ok" as *u8) 54 55 // R2 (NEG): tampered signature -> rejected 56 let sigT: *u8 = sys_mmap(64); var c0: i64=0; while c0<64 { sigT[c0]=sig[c0]; c0=c0+1 } 57 if (sig[0] as i64)==0 { sigT[0]=1 as u8 } else { sigT[0]=0 as u8 } 58 ok = 0; if dc_verify(ids, pubs, n, "laptop-1" as *u8, chal, clen, sigT)==0 { ok=1 } 59 rows=rows+1; pass=pass+dg_row(2, ok, "tampered signature rejected (neg control)" as *u8) 60 61 // R3 (NEG): unknown / unprovisioned device -> denied (deny-by-default) 62 ok = 0; if dc_verify(ids, pubs, n, "phone-9" as *u8, chal, clen, sig)==0 { ok=1 } 63 rows=rows+1; pass=pass+dg_row(3, ok, "unknown device -> denied (deny-by-default, neg)" as *u8) 64 65 // R4 (NEG): replay -- a signature over a DIFFERENT challenge does not verify against this one 66 let chal2: *u8 = "nishi-access-wall-nonce-00000002" as *u8 67 ok = 0; if dc_verify(ids, pubs, n, "laptop-1" as *u8, chal2, dg_slen(chal2), sig)==0 { ok=1 } 68 rows=rows+1; pass=pass+dg_row(4, ok, "replay defeated: sig bound to its challenge (neg)" as *u8) 69 70 // R5 (NEG): impersonation -- an attacker with a DIFFERENT key cannot pass as laptop-1 71 let priv2: *u8 = sys_mmap(32) 72 dg_hex2bytes("0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20" as *u8, 32, priv2) 73 let sig2: *u8 = sys_mmap(64) 74 ed25519_sign_full(priv2, chal, clen, sig2) 75 ok = 0; if dc_verify(ids, pubs, n, "laptop-1" as *u8, chal, clen, sig2)==0 { ok=1 } 76 rows=rows+1; pass=pass+dg_row(5, ok, "impersonation with a different key rejected (neg)" as *u8) 77 78 dg_w("NX-DEVICE-CERT-GATE rows=" as *u8); dg_num(rows); dg_w(" pass=" as *u8); dg_num(pass); dg_w("\n" as *u8) 79 if pass == rows { 80 let line: *u8 = sys_mmap(256) 81 var off: i64 = dg_cat(line, 0, "ACCESSGATE row=nx_device_cert device-provision rows=" as *u8) 82 off = dg_catnum(line, off, rows); off = dg_cat(line, off, " pass=" as *u8); off = dg_catnum(line, off, pass) 83 off = dg_cat(line, off, " verdict=PASS\n" as *u8) 84 let gf: i64 = sys_openat_append("knowledge/status/access_gate.log" as *u8, 0x1a4) 85 if gf >= 0 { sys_write(gf, line, off); sys_close(gf) } 86 dg_w("NX-DEVICE-CERT-GATE verdict=PASS -- device provisioning recorded in access_gate.log\n" as *u8) 87 sys_exit(0); return 0 88 } 89 dg_w("NX-DEVICE-CERT-GATE verdict=FAIL -- NOT recorded (no fake-green)\n" as *u8) 90 sys_exit(1); return 1 91}