code wiki / _hdl_build / nx_nishifs_crypt.nx
nx_nishifs_crypt.nx
buildroot/runtime/_hdl_build/nx_nishifs_crypt.nx
about
nx_nishifs_crypt.nx -- ladder C7 (the FS): NATIVE ENCRYPTION AT REST for NishiFS (the last FS core axis).
Content-addressed encryption, done honestly: the CID is sha256 over the PLAINTEXT (so integrity-on-read +
dedup are preserved), the object is stored as CIPHERTEXT, and reads DECRYPT-then-verify the CID -- so a
WRONG KEY yields wrong plaintext -> CID mismatch -> rejected (the integrity check doubles as authentication).
Cipher = a SOVEREIGN sha256-keystream (hash-CTR): keystream block_i = sha256(key || nonce || i), XORed into
the data; per-object nonce = the plaintext CID (convergent encryption -> same content+key = same ciphertext
= dedup preserved). XOR is implemented bitwise (xorb) since it composes only +,&,|,<<,>>.
HONEST CAVEATS (no overclaim): (1) a standard AEAD (ChaCha20-Poly1305 / AES-GCM) is the hardening refinement
over a hash-CTR stream cipher; (2) convergent encryption has the known confirmation-of-file attack; (3) the
key here is a literal for the KAT -- real key mgmt (KDF from a passphrase / hardware) is separate.
KAT 6/6: T1 encrypt-at-rest round-trip (right key decrypts); T2 stored bytes are CIPHERTEXT (!= plaintext);
T3 wrong key -> AUTH-FAIL (no plaintext leaked); T4 CID over plaintext + dedup (same pt+key -> same store,
arena does not grow); T5 tamper ciphertext -> AUTH-FAIL (integrity caught); T6 distinct plaintext -> distinct
ciphertext (no keystream reuse). composes nx_sha256 + nx_syscalls.
NEVER-BRICK (Rule 26): pure in-memory, writes NOTHING. expect_exit: 0 license_tier: ORIGINAL
dependencies 2 imports · 0 importers
imports: nx_syscalls.nxnx_sha256.nx
imported by: nobody (leaf or entry point)
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| none |
consts
| 19 | const ENC_MAGIC_65536: i64 = 65536 |
| 21 | const ENC_OK: i64 = 0 |
| 22 | const ENC_NOTFOUND: i64 = 3 |
| 23 | const ENC_AUTHFAIL: i64 = 8 |
functions
| 25 | func ui_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } |
| 26 | func ui_num(v: i64) -> i64 { let b: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(1,"-" as *u8,1)} 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{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 } |
| 27 | func ui_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } called by 1: main |
| 28 | func cid_eq(a: *u8, b: *u8) -> i64 { var i: i64=0; while i<32 { if a[i]!=b[i] { return 0 } i=i+1 } return 1 } |
| 29 | func wr_u32_le(d: *u8, off: i64, v: i64) -> i64 { d[off]=(v & 0xFF) as u8; d[off+1]=((v>>8)&0xFF) as u8; d[off+2]=((v>>16)&0xFF) as u8; d[off+3]=((v>>24)&0xFF) as u8; return 0 } called by 1: enc_put |
| 30 | func rd_u32_le(d: *u8, off: i64) -> i64 { return (d[off] as i64) | ((d[off+1] as i64)<<8) | ((d[off+2] as i64)<<16) | ((d[off+3] as i64)<<24) } called by 1: arena_find |
| 32 | func xorb(a: i64, b: i64) -> i64 { var r: i64=0; var bit: i64=0; while bit<8 { let ab: i64=(a>>bit)&1; let bb: i64=(b>>bit)&1; let xb: i64=(ab+bb)&1; r = r | (xb<<bit); bit=bit+1 } return r } called by 1: keystream_xor |
| 35 | func keystream_xor(key: *u8, klen: i64, nonce: *u8, buf: *u8, n: i64) -> i64 |
| 53 | func arena_find(arena: *u8, endsz: i64, cid: *u8, outlen: *i64) -> i64 |
| 65 | func enc_put(arena: *u8, endp: *i64, key: *u8, klen: i64, pt: *u8, n: i64, cidout: *u8) -> i64 |
| 80 | func enc_get(arena: *u8, endsz: i64, key: *u8, klen: i64, cid: *u8, out: *u8, cap: i64, lenout: *i64) -> i64 |
| 97 | func main() -> i64 |