// Example 15: Authenticated Encrypted File Vault & Ed25519-Signed PEM Manifest CLI // Zero OS crypto DLLs: pure-Veko HKDF-SHA256, ChaCha20-Poly1305 AEAD, HMAC-SHA256, // Ed25519 manifest signatures, and multi-block PEM storage across ORO VM, Wasm 3, and Native. // // Self-demo (no args): // Track A (ORO VM) : veko run examples/15_encrypted_vault_cli.vk // Track A (.vko) : veko compile --vko examples/15_encrypted_vault_cli.vk -o dist/15.vko ; veko run dist/15.vko // Track B (Wasm 3) : veko run --wasm --fuel=0 examples/15_encrypted_vault_cli.vk // Track C (Native) : veko compile --native examples/15_encrypted_vault_cli.vk -o dist/15.exe ; ./dist/15.exe // // CLI subcommands: // veko run examples/15_encrypted_vault_cli.vk -- init [--pass ] // veko run examples/15_encrypted_vault_cli.vk -- set [--pass ] // veko run examples/15_encrypted_vault_cli.vk -- get [--pass ] // veko run examples/15_encrypted_vault_cli.vk -- list [--pass ] // veko run examples/15_encrypted_vault_cli.vk -- verify // veko run --wasm --allow-fs --fuel=0 examples/15_encrypted_vault_cli.vk -- init vault.pem --pass mypw use [pem = codec/pem, sha = crypto/sha256, aead = crypto/chacha20, kx = crypto/x25519, json, bytes, str] VaultHeader = struct { version i64 = 1 cipher str = 'chacha20-poly1305' kdf str = 'hkdf-sha256' pubkey_hex str salt_hex str nonce_hex str mac_hex str entries i64 } VaultSecret = struct { name str value secret str } hex-nibble = fn [b i64] i64 [ if b >= 48 and b <= 57 [ return (b - 48) ] if b >= 97 and b <= 102 [ return (b - 87) ] if b >= 65 and b <= 70 [ return (b - 55) ] -1 ] is-map? = fn [v any] bool [ (type-of v) == .map_val ] is-str? = fn [v any] bool [ t = type-of v t == .str or t == .str_inline or t == .str_heap ] is-num? = fn [v any] bool [ t = type-of v t == .i64_val or t == .f64_val ] hex-to-bytes = fn [hx str] any [ bs = bytes/from-str hx n = len bs if (n & 1) != 0 [ return (err 'hex: odd length') ] mut out = bytes/with-cap (n >> 1) mut i = 0 while i < n [ hi = hex-nibble bs[i] lo = hex-nibble bs[i + 1] if hi < 0 or lo < 0 [ return (err 'hex: invalid hex char') ] bytes/push (mut out) ((hi << 4) | lo) i = i + 2 ] out ] derive-keys = fn [pass str, salt bytes] map [ prk = sha/hkdf-extract salt (bytes/from-str pass) { enc_key = sha/hkdf-expand prk (bytes/from-str 'veko-vault-aead-v1') 32 mac_key = sha/hkdf-expand prk (bytes/from-str 'veko-vault-hmac-v1') 32 sign_seed = sha/hkdf-expand prk (bytes/from-str 'veko-vault-ed25519-v1') 32 } ] build-aad = fn [ver i64, cipher str, kdf str, pk_hex str, salt_hex str, nonce_hex str, count i64] bytes [ bytes/from-str (str/join [(mold ver) cipher kdf pk_hex salt_hex nonce_hex (mold count)] '|') ] compute-manifest-digest = fn [aad bytes, ct_and_tag bytes] bytes [ mut buf = bytes/with-cap ((len aad) + (len ct_and_tag)) bytes/push-all (mut buf) aad bytes/push-all (mut buf) ct_and_tag sha/sha256 buf ] flip-first-byte = fn [src bytes] bytes [ n = len src mut copy = bytes/with-cap n if n > 0 [ bytes/push (mut copy) ((src[0] ^ 0x01) & 0xff) if n > 1 [ bytes/push-all (mut copy) src[1..n] ] ] copy ] seal-vault = fn [pass str, salt bytes, nonce bytes, entries map, cached_pk any] any [ keys = derive-keys pass salt pk = if cached_pk == .none [ kx/ed25519-pubkey keys.sign_seed ] else [ cached_pk ] if err? pk [ return pk ] pk_hex = sha/to-hex pk salt_hex = sha/to-hex salt nonce_hex = sha/to-hex nonce count = len (map/keys entries) aad = build-aad 1 'chacha20-poly1305' 'hkdf-sha256' pk_hex salt_hex nonce_hex count payload_json = json/encode { entries = entries } ct_and_tag = aead/aead-encrypt keys.enc_key nonce aad (bytes/from-str payload_json) manifest_msg = compute-manifest-digest aad ct_and_tag mac_hex = sha/to-hex (sha/hmac-sha256 keys.mac_key manifest_msg) sig = kx/ed25519-sign keys.sign_seed manifest_msg if err? sig [ return sig ] hdr = VaultHeader { version = 1 cipher = 'chacha20-poly1305' kdf = 'hkdf-sha256' pubkey_hex = pk_hex salt_hex = salt_hex nonce_hex = nonce_hex mac_hex = mac_hex entries = count } hdr_bytes = bytes/from-str (json/encode hdr) pem_text = str/join [ (pem/pem-encode 'VEKO VAULT HEADER' hdr_bytes) (pem/pem-encode 'VEKO VAULT CIPHERTEXT' ct_and_tag) (pem/pem-encode 'VEKO VAULT SIGNATURE' sig) ] '\n' { pem_text = pem_text header = hdr aad = aad ct_and_tag = ct_and_tag sig = sig pk = pk manifest_msg = manifest_msg } ] parse-vault-pem = fn [pem_text str] any [ pblk = pem/pem-to-block pem_text if err? pblk [ return pblk ] mut hdr_bytes = bytes/with-cap 0 mut ct_bytes = bytes/with-cap 0 mut sig_bytes = bytes/with-cap 0 mut got_hdr = false mut got_ct = false mut got_sig = false _ = parse-block pblk [ any [ [.pem lbl = str! der = any!] [ if lbl == 'VEKO VAULT HEADER' [ hdr_bytes = der got_hdr = true ] if lbl == 'VEKO VAULT CIPHERTEXT' [ ct_bytes = der got_ct = true ] if lbl == 'VEKO VAULT SIGNATURE' [ sig_bytes = der got_sig = true ] ] ] ] if not (got_hdr and got_ct and got_sig) [ return (err 'vault: missing required PEM sections') ] hdr_dec = json/decode/or (bytes/to-str hdr_bytes) [.none] if err? hdr_dec or hdr_dec == .none or not (is-map? hdr_dec) [ return (err 'vault: malformed header JSON') ] raw_ver = json/get hdr_dec 'version' cipher = json/get hdr_dec 'cipher' kdf = json/get hdr_dec 'kdf' pk_hex = json/get hdr_dec 'pubkey_hex' salt_hex = json/get hdr_dec 'salt_hex' nonce_hex = json/get hdr_dec 'nonce_hex' mac_hex = json/get hdr_dec 'mac_hex' raw_entries = json/get hdr_dec 'entries' if not (is-num? raw_ver) or not (is-num? raw_entries) [ return (err 'vault: malformed header JSON') ] if not (is-str? cipher) or not (is-str? kdf) [ return (err 'vault: malformed header JSON') ] if not (is-str? pk_hex) or (len pk_hex) != 64 [ return (err 'vault: malformed header JSON') ] if not (is-str? salt_hex) or (len salt_hex) != 32 [ return (err 'vault: malformed header JSON') ] if not (is-str? nonce_hex) or (len nonce_hex) != 24 [ return (err 'vault: malformed header JSON') ] if not (is-str? mac_hex) or (len mac_hex) != 64 [ return (err 'vault: malformed header JSON') ] ver = as-i64 raw_ver entries_n = as-i64 raw_entries pk = hex-to-bytes pk_hex if err? pk [ return pk ] salt = hex-to-bytes salt_hex if err? salt [ return salt ] nonce = hex-to-bytes nonce_hex if err? nonce [ return nonce ] aad = build-aad ver cipher kdf pk_hex salt_hex nonce_hex entries_n manifest_msg = compute-manifest-digest aad ct_bytes { version = ver cipher = cipher kdf = kdf pk = pk salt = salt nonce = nonce mac_hex = mac_hex entries_n = entries_n aad = aad ct_and_tag = ct_bytes sig = sig_bytes manifest_msg = manifest_msg } ] verify-vault-manifest = fn [parsed map] bool [ kx/ed25519-verify parsed.pk parsed.manifest_msg parsed.sig ] decrypt-vault-entries = fn [parsed map, pass str] any [ keys = derive-keys pass parsed.salt expected_mac = sha/hmac-sha256 keys.mac_key parsed.manifest_msg stored_mac = hex-to-bytes parsed.mac_hex if err? stored_mac [ return stored_mac ] if not (sha/ct-eq expected_mac stored_mac) [ return (err 'vault: HMAC-SHA256 verification failed (wrong passphrase or tampered manifest)') ] pt = aead/aead-decrypt keys.enc_key parsed.nonce parsed.aad parsed.ct_and_tag if err? pt [ return pt ] dec = json/decode/or (bytes/to-str pt) [.none] if err? dec or dec == .none or not (is-map? dec) [ return (err 'vault: corrupted plaintext payload') ] entries_val = json/get dec 'entries' if err? entries_val or entries_val == .null or not (is-map? entries_val) [ return (err 'vault: corrupted plaintext payload') ] entries_val ] open-vault = fn [parsed map, pass str] any [ if not (verify-vault-manifest parsed) [ return (err 'vault: Ed25519 manifest signature verification failed') ] decrypt-vault-entries parsed pass ] resolve-demo-path = fn [] str [ tmp1 = os/getenv/or 'TEMP' [''] tmp2 = if tmp1 == '' [ os/getenv/or 'TMP' [''] ] else [ tmp1 ] if tmp2 == '' [ 'veko_vault_demo.pem' ] else [ str/join [tmp2 '/veko_vault_demo.pem'] '' ] ] default-passphrase = fn [] str [ env_pw = os/getenv/or 'VEKO_VAULT_PASSPHRASE' [''] if env_pw == '' [ 'veko-vault-demo-passphrase' ] else [ env_pw ] ] run-self-demo = fn [] i64 [ demo_path = resolve-demo-path pass = default-passphrase // 1. `.secret` governance: VaultSecret + bytes/with-secret blocked from JSON egress env_secret = os/getenv/secret/or 'VEKO_VAULT_KEY' ['veko-vault-secret-material'] vs = VaultSecret { name = 'master', value = env_secret } mut sec_buf = bytes/with-secret 16b bytes/push-all (mut sec_buf) (bytes/from-str 'master-key-bytes') sec_box = [sec_buf] fw_verdict = json/encode/or { key = sec_box[0] } ['secret-json-blocked'] // 2. Seal vault with 3 entries using HKDF-SHA256 + ChaCha20-Poly1305 + Ed25519 salt = random/bytes 16 nonce = random/bytes 12 entries = { 'api.token' = 'tok_prod_veko_991' 'db.dsn' = 'postgres://vault.local/app' 'tls.pin' = 'sha256/veko-spki-pin' } sealed = seal-vault pass salt nonce entries .none if err? sealed [ println (mold sealed) return 1 ] _ = os/write-file demo_path sealed.pem_text exists_after_write = os/exists? demo_path // 3. Reload PEM from disk/VFS, parse via `pem-to-block` + `parse-block`, verify & decrypt raw_bytes = os/read-bytes demo_path parsed = parse-vault-pem (bytes/to-str raw_bytes) if err? parsed [ println (mold parsed) return 1 ] sig_ok = verify-vault-manifest parsed if not sig_ok [ println 'err(vault: Ed25519 manifest signature verification failed)' return 1 ] opened = decrypt-vault-entries parsed pass if err? opened [ println (mold opened) return 1 ] // 4. Tamper-detection suite: // (a) Ed25519 manifest tamper (2nd verify call) bad_msg = flip-first-byte parsed.manifest_msg tamper_sig_ok = kx/ed25519-verify parsed.pk bad_msg parsed.sig // (b) ChaCha20-Poly1305 ciphertext tamper keys = derive-keys pass parsed.salt bad_ct = flip-first-byte parsed.ct_and_tag tamper_aead = aead/aead-decrypt/or keys.enc_key parsed.nonce parsed.aad bad_ct [.rejected] // (c) Wrong passphrase rejection wrong_keys = derive-keys 'wrong-passphrase' parsed.salt wrong_aead = aead/aead-decrypt/or wrong_keys.enc_key parsed.nonce parsed.aad parsed.ct_and_tag [.rejected] // 5. Scrub temporary demo file (no `os/remove` builtin; overwrite full byte length for Wasm CoW VFS) scrub_hdr = '# veko-vault-demo-scrubbed\n' orig_n = len raw_bytes mut scrub_parts = [scrub_hdr] mut scrub_cur = len scrub_hdr while scrub_cur < orig_n [ push mut scrub_parts ' ' scrub_cur = scrub_cur + 1 ] _ = os/write-file demo_path (str/join scrub_parts '') scrubbed = bytes/to-str (os/read-bytes demo_path) scrub_ok = not (str/contains? scrubbed 'BEGIN VEKO VAULT') println (str/join ['vault-secret-mold=' (mold sec_buf) '|name=' vs.name] '') println (str/join ['vault-secret-firewall=' fw_verdict] '') println (str/join ['vault-sealed=exists:' (mold exists_after_write) ',entries:' (mold parsed.entries_n) ',cipher:' parsed.cipher] '') println (str/join ['vault-ed25519-verify=' (mold sig_ok)] '') println (str/join ['vault-unseal=api:' (json/get opened 'api.token') ',db:' (json/get opened 'db.dsn') ',tls:' (json/get opened 'tls.pin')] '') println (str/join ['tamper-ed25519-rejected=' (mold (not tamper_sig_ok))] '') println (str/join ['tamper-aead-rejected=' (mold (tamper_aead == .rejected))] '') println (str/join ['wrong-pass-rejected=' (mold (wrong_aead == .rejected))] '') println (str/join ['demo-scrubbed=' (mold scrub_ok)] '') 0 ] print-usage = fn [] i64 [ println 'Veko Encrypted Vault CLI (Ed25519 + ChaCha20-Poly1305 + PEM)' println 'Usage:' println ' init [--pass ]' println ' set [--pass ]' println ' get [--pass ]' println ' list [--pass ]' println ' verify ' println ' --help' 0 ] parse-cli-args = fn [args block] any [ mut pos = [] mut pass = default-passphrase mut help = false mut i = 0 while i < (len args) [ a = args[i] if a == '--help' or a == '-h' [ help = true i = i + 1 ] else [ if a == '--pass' [ if (i + 1) >= (len args) [ return (err 'vault: --pass requires a value') ] pass = args[i + 1] i = i + 2 ] else [ push mut pos a i = i + 1 ] ] ] { pos = pos, pass = pass, help = help } ] run-cli = fn [args block] i64 [ cli = parse-cli-args args if err? cli [ println (mold cli) return 1 ] if cli.help or (len cli.pos) == 0 [ return (print-usage) ] cmd = cli.pos[0] pass = cli.pass if cmd == 'init' [ if (len cli.pos) < 2 [ println 'error: init requires ' return 1 ] path = cli.pos[1] salt = random/bytes 16 nonce = random/bytes 12 sealed = seal-vault pass salt nonce {} .none if err? sealed [ println (mold sealed) return 1 ] _ = os/write-file path sealed.pem_text println (str/join ['vault-init=ok|file=' path '|entries=0'] '') return 0 ] if cmd == 'verify' [ if (len cli.pos) < 2 [ println 'error: verify requires ' return 1 ] path = cli.pos[1] if not (os/exists? path) [ println (str/join ['error: file not found: ' path] '') return 1 ] parsed = parse-vault-pem (bytes/to-str (os/read-bytes path)) if err? parsed [ println (mold parsed) return 1 ] ok = verify-vault-manifest parsed println (str/join ['vault-verify=' (mold ok) '|entries=' (mold parsed.entries_n)] '') if ok [ return 0 ] else [ return 1 ] ] if cmd == 'set' [ if (len cli.pos) < 4 [ println 'error: set requires ' return 1 ] path = cli.pos[1] k = cli.pos[2] v = cli.pos[3] if not (os/exists? path) [ println (str/join ['error: file not found: ' path] '') return 1 ] parsed = parse-vault-pem (bytes/to-str (os/read-bytes path)) if err? parsed [ println (mold parsed) return 1 ] entries = open-vault parsed pass if err? entries [ println (mold entries) return 1 ] updated = map/set entries k v new_nonce = random/bytes 12 sealed = seal-vault pass parsed.salt new_nonce updated parsed.pk if err? sealed [ println (mold sealed) return 1 ] _ = os/write-file path sealed.pem_text println (str/join ['vault-set=' k '|entries=' (mold (len (map/keys updated)))] '') return 0 ] if cmd == 'get' [ if (len cli.pos) < 3 [ println 'error: get requires ' return 1 ] path = cli.pos[1] k = cli.pos[2] if not (os/exists? path) [ println (str/join ['error: file not found: ' path] '') return 1 ] parsed = parse-vault-pem (bytes/to-str (os/read-bytes path)) if err? parsed [ println (mold parsed) return 1 ] entries = open-vault parsed pass if err? entries [ println (mold entries) return 1 ] val = map/get/or entries k [.none] if val == .none [ println (str/join ['vault-get=' k '|found=false'] '') return 1 ] println (str/join ['vault-get=' k '=' val] '') return 0 ] if cmd == 'list' [ if (len cli.pos) < 2 [ println 'error: list requires ' return 1 ] path = cli.pos[1] if not (os/exists? path) [ println (str/join ['error: file not found: ' path] '') return 1 ] parsed = parse-vault-pem (bytes/to-str (os/read-bytes path)) if err? parsed [ println (mold parsed) return 1 ] entries = open-vault parsed pass if err? entries [ println (mold entries) return 1 ] ks = map/keys entries println (str/join ['vault-list=count:' (mold (len ks)) '|keys=' (str/join ks ',')] '') return 0 ] println (str/join ['error: unknown command ' cmd] '') print-usage ] if (len os/args) == 0 [ _ = run-self-demo ] else [ _ = run-cli os/args ]