code wiki / _hdl_build / nx_dr_encrypt.nx

nx_dr_encrypt.nx source

↩ module page · 73 lines · 4826 B

1// nx_dr_encrypt.nx -- CAP-DR-ENCRYPT: encryption-at-rest for the DR archive. Composes the VETTED RFC-8439 2// ChaCha20-Poly1305 AEAD (nx_chacha20_poly1305) -- NOT hand-rolled crypto. key = sha256(keyfile) (operator-held 3// passphrase, never on the command line); nonce = sha256(plaintext)[0..12] (CONVERGENT: distinct plaintext -> 4// distinct nonce, so no reuse per key; reproducible). Sealed file = [nonce:12][tag:16][ciphertext:N]. open() checks 5// the Poly1305 tag FIRST (constant-time) -> tampering / wrong-key = rejected, plaintext never emitted. So the 6// accounts never sit in plaintext on /mnt/d, and any corruption is cryptographically detected. 7// nx_dr_encrypt seal <plain-in> <enc-out> <keyfile> 8// nx_dr_encrypt open <enc-in> <plain-out> <keyfile> 9// license_tier: ORIGINAL (composes nx_chacha20_poly1305 + nx_sha256) 10import "nx_syscalls.nx" 11import "nx_sha256.nx" 12import "nx_chacha20_poly1305.nx" 13const E_MAGIC_65536: i64 = 65536 14 15func e_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 16func e_wn(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 } 17func e_read(path: *u8, out: *u8, cap: i64) -> i64 { 18 let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 - 1 } 19 var total: i64 = 0; var go: i64 = 1 20 while go == 1 { let nr: i64 = sys_read(fd, (out as i64 + total) as *u8, cap - total); if nr <= 0 { go = 0 } if nr > 0 { total = total + nr } if total >= cap { go = 0 } } 21 sys_close(fd); return total 22} 23func e_write(path: *u8, buf: *u8, n: i64) -> i64 { let fd: i64 = sys_openat_wr(path, 0x1a4); if fd < 0 { return 0 - 1 } sys_write(fd, buf, n); sys_close(fd); return n } 24func e_streq(a: *u8, b: *u8) -> i64 { var i: i64=0; while a[i]!=(0 as u8) { if a[i]!=b[i] { return 0 } i=i+1 } if b[i]!=(0 as u8) { return 0 } return 1 } 25// key = sha256(keyfile-contents). Returns 0 ok, -1 if keyfile unreadable. 26func e_key(keyfile: *u8, keyout: *u8) -> i64 { 27 let kb: *u8 = sys_mmap(E_MAGIC_65536); let kn: i64 = e_read(keyfile, kb, E_MAGIC_65536) 28 if kn < 0 { return 0 - 1 } 29 sha256_digest(kb, kn, keyout); return 0 30} 31 32const E_CAP: i64 = 67108864 33 34func main(argc: i64, argv: *i64) -> i64 { 35 if argc < 5 { e_w("usage: nx_dr_encrypt seal|open <in> <out> <keyfile>\n" as *u8); sys_exit(2); return 2 } 36 let mode: *u8 = argv[1] as *u8 37 let inp: *u8 = argv[2] as *u8 38 let outp: *u8 = argv[3] as *u8 39 let keyf: *u8 = argv[4] as *u8 40 41 let key: *u8 = sys_mmap(32) 42 if e_key(keyf, key) != 0 { e_w("nx_dr_encrypt: cannot read keyfile\n" as *u8); sys_exit(3); return 3 } 43 44 if e_streq(mode, "seal" as *u8) == 1 { 45 let pt: *u8 = sys_mmap(E_CAP); let n: i64 = e_read(inp, pt, E_CAP) 46 if n < 0 { e_w("nx_dr_encrypt: cannot read plaintext\n" as *u8); sys_exit(4); return 4 } 47 let dig: *u8 = sys_mmap(64); sha256_digest(pt, n, dig) // nonce source (convergent) 48 let ct: *u8 = sys_mmap(n + 16); let tag: *u8 = sys_mmap(32) 49 nx_chacha20_poly1305_encrypt(key, dig, "" as *u8, 0, pt, n, ct, tag) 50 let enc: *u8 = sys_mmap(n + 32) 51 var i: i64=0; while i<12 { enc[i]=dig[i]; i=i+1 } // nonce (12) 52 i=0; while i<16 { enc[12+i]=tag[i]; i=i+1 } // tag (16) 53 i=0; while i<n { enc[28+i]=ct[i]; i=i+1 } // ciphertext 54 e_write(outp, enc, n + 28) 55 e_w("nx_dr_encrypt: SEALED " as *u8); e_wn(n); e_w(" bytes -> " as *u8); e_w(outp); e_w(" ([nonce|tag|ct], ChaCha20-Poly1305)\n" as *u8) 56 sys_exit(0); return 0 57 } 58 if e_streq(mode, "open" as *u8) == 1 { 59 let enc: *u8 = sys_mmap(E_CAP); let m: i64 = e_read(inp, enc, E_CAP) 60 if m < 28 { e_w("nx_dr_encrypt: enc file too short / missing\n" as *u8); sys_exit(4); return 4 } 61 let ct_len: i64 = m - 28 62 let nonce: *u8 = sys_mmap(16); var i: i64=0; while i<12 { nonce[i]=enc[i]; i=i+1 } 63 let tag: *u8 = sys_mmap(32); i=0; while i<16 { tag[i]=enc[12+i]; i=i+1 } 64 let ct: *u8 = (enc as i64 + 28) as *u8 65 let pt: *u8 = sys_mmap(ct_len + 16) 66 let v: i64 = nx_chacha20_poly1305_decrypt(key, nonce, "" as *u8, 0, ct, ct_len, tag, pt) 67 if v != NX_AEAD_VERDICT_OK { e_w("nx_dr_encrypt: TAG MISMATCH -- tampered or wrong key (plaintext NOT emitted)\n" as *u8); sys_exit(1); return 1 } 68 e_write(outp, pt, ct_len) 69 e_w("nx_dr_encrypt: OPENED + VERIFIED " as *u8); e_wn(ct_len); e_w(" bytes -> " as *u8); e_w(outp); e_w(" (tag OK)\n" as *u8) 70 sys_exit(0); return 0 71 } 72 e_w("nx_dr_encrypt: unknown mode (use seal|open)\n" as *u8); sys_exit(2); return 2 73}