code wiki / _hdl_build / nx_nishifs_crypt.nx

nx_nishifs_crypt.nx

buildroot/runtime/_hdl_build/nx_nishifs_crypt.nx

9778 B162 linesdepth 5pulls 5 transitivereach 0 importersview sourcekind tooltopic nishifs
docsdependenciesstructsconstsfunctions

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

nx_syscalls.nx nx_sha256.nx nx_nishifs_crypt.nx

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

main ui_puts sys_write ui_slen sys_mmap enc_put sha256_digest sys_mmap ↻ sha256_init sys_mmap ↻ sha256_k sha256_update sha256_compress_ni_blocks blk_set_byte sha256_compress sha256_compress_ni blk_word blk_byte sha256_final blk_set_byte ↻ sha256_compress ↻ sys_mmap ↻ arena_find rd_u32_le keystream_xor sys_mmap ↻ sha256_digest ↻ xorb wr_u32_le enc_get sys_mmap ↻ arena_find ↻ keystream_xor ↻ sha256_digest ↻ cid_eq arena_find ↻ sha256_digest ↻ ui_num sys_mmap ↻ sys_write ↻

structs

none

consts

19const ENC_MAGIC_65536: i64 = 65536
21const ENC_OK: i64 = 0
22const ENC_NOTFOUND: i64 = 3
23const ENC_AUTHFAIL: i64 = 8

functions

25func 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 }
called by 1: main calls 1: sys_write
26func 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 }
called by 1: main calls 2: sys_mmapsys_write
27func ui_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
called by 1: main
28func 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 }
called by 2: enc_getmain
29func 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
30func 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
32func 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
35func keystream_xor(key: *u8, klen: i64, nonce: *u8, buf: *u8, n: i64) -> i64
53func arena_find(arena: *u8, endsz: i64, cid: *u8, outlen: *i64) -> i64
called by 3: enc_putenc_getmain calls 1: rd_u32_le
65func enc_put(arena: *u8, endp: *i64, key: *u8, klen: i64, pt: *u8, n: i64, cidout: *u8) -> i64
80func enc_get(arena: *u8, endsz: i64, key: *u8, klen: i64, cid: *u8, out: *u8, cap: i64, lenout: *i64) -> i64
97func main() -> i64