Skip to content

EVFS: encrypted vault container#69

Merged
Adel-Ayoub merged 25 commits into
devfrom
evfs
Mar 6, 2026
Merged

EVFS: encrypted vault container#69
Adel-Ayoub merged 25 commits into
devfrom
evfs

Conversation

@Adel-Ayoub

Copy link
Copy Markdown
Collaborator

Summary

  • Implement .vault encrypted container with per-segment AEAD encryption (AES-256-GCM / ChaCha20-Poly1305), BLAKE3
    integrity checksums, and WAL-based crash recovery
  • Master key expanded via HKDF-SHA256 into 3 domain-separated sub-keys (cipher, nonce derivation, index encryption)
    — never stored directly
  • Pre-allocated vault with CSPRNG-filled free space (indistinguishable from encrypted data)
  • Dual-redundant encrypted index (primary + shadow) survives single-region corruption
  • Secure deletion overwrites segment regions with CSPRNG bytes
  • Advisory file locking prevents concurrent access
  • Add Dart VaultService wrapper and 17 integration tests covering the full Dart → FRB → Rust → FRB → Dart
    pipeline

Architecture

.vault file layout:
Header (32B) → Primary Index (64KB encrypted) → Data Region (segments + CSPRNG fill) → Shadow Index (64KB
encrypted)
{vault_path}.wal — undo journal for crash recovery
{vault_path}.lock — advisory flock

  • Write: compress → AEAD encrypt (deterministic nonce from generation counter) → BLAKE3 checksum → WAL journal
    → write segment → flush dual index → WAL commit
  • Read: seek to offset → AEAD decrypt → verify BLAKE3 → decompress
  • Delete: WAL journal → CSPRNG overwrite region → deallocate → flush dual index → WAL commit

Key Design Decisions

  • Nonce reuse prevention: nonces derived via HKDF(nonce_key, segment_index || generation) — generation
    increments on every overwrite
  • Fixed-size vault: file never grows/shrinks; free space filled with OsRng bytes
  • Index padding: always 64KB regardless of entry count (hides segment count from observer)
  • Best-fit allocator: free list with adjacent-region merging, fallback to append
  • Constant-time comparisons: subtle::ConstantTimeEq for nonce verification and checksum validation

Files Changed

File Action
rust/src/core/evfs/format.rs Created — vault header, segment index, free-region allocator
rust/src/core/evfs/segment.rs Created — HKDF key derivation, AEAD encrypt/decrypt, BLAKE3 checksums, secure
erase
rust/src/core/evfs/wal.rs Created — WAL entries with CRC32, crash recovery, advisory file locking
rust/src/api/evfs/mod.rs Created — public vault API (create/open/write/read/delete/list/capacity/close)
lib/src/evfs/vault_service.dart Created — Dart wrapper with optional compression on write, automatic
decompression on read
lib/m_security.dart Edited — added VaultService export
integration_test/evfs_test.dart Created — 17 integration tests
rust/src/frb_generated.rs Regenerated

Rust Tests (248 total, all pass)

EVFS unit tests cover: header roundtrip, index serialization, allocation/deallocation/merge, generation counters,
key derivation domain separation, nonce uniqueness, segment encrypt/decrypt with all compression modes, MIME-aware
skip, index encryption, checksum verification, secure erase, pre-allocation, WAL entry roundtrip/CRC/recovery, file
locking

Integration Tests (17)

Core lifecycle (10): create/write/close/open/read roundtrip, multiple segments, overwrite, read nonexistent
throws, delete, vault full, wrong key, concurrent open locked, list names, capacity info

Compression (4): zstd auto-decompression, brotli auto-decompression, MIME-aware skip (.jpg), mixed algorithms
in same vault

Integrity & recovery (3): tampered segment detection, WAL crash recovery, corrupted primary index falls back to
shadow

Verification

  • cargo check --features compression — clean
  • cargo clippy --features compression --all-targets — no warnings
  • flutter analyze lib/ integration_test/ — no issues
  • 248/248 Rust unit tests pass

Adel-Ayoub and others added 25 commits March 5, 2026 09:42
  VaultFull, VaultLocked, SegmentNotFound, VaultCorrupted.
  Module root with format, segment, and wal submodules.
  VaultHeader (32B), SegmentEntry with generation counters and
  per-segment compression, SegmentIndex with best-fit allocator,
  free-region merging, padded serialization (64KB), layout constants,
  and 27 unit tests.
feat(evfs): vault format structures and segment index
  HKDF domain-separated sub-keys (cipher/nonce/index), generation-aware
  nonce derivation, compress-then-encrypt pipeline with MIME-aware skip,
  BLAKE3 checksums with constant-time verification (subtle), CSPRNG
  pre-allocation in 64KB chunks, secure erase with fsync. 23 tests.
  HKDF domain-separated sub-keys (cipher/nonce/index), generation-aware
  nonce derivation for both segments and index, compress-then-encrypt
  pipeline with MIME-aware skip, BLAKE3 checksums with constant-time
  verification (subtle), CSPRNG pre-allocation in 64KB chunks, secure
  erase with fsync, intermediate buffer zeroization. 26 tests.
…cryption

feat(evfs): add per-segment encryption, checksums, and secure deletion
…recovery

feat(evfs): WAL crash recovery and advisory file locking
  - Add ENCRYPTED_INDEX_SIZE (INDEX_PAD_SIZE + 28) for on-disk layout
  - Update DATA_REGION_OFFSET and shadow/WAL offsets accordingly
  - Add total_vault_size() helper for vault file preallocation
  - Add pub(super) aead_encrypt_random_nonce and aead_decrypt_with_stored_nonce
    for index encryption without generation-based nonce derivation
  - Add VaultHandle (opaque) holding file, keys, index, WAL, and lock
  - Implement vault_create, vault_open, vault_write, vault_read,
    vault_delete, vault_list, vault_capacity, vault_close
  - Compress-then-encrypt pipeline with MIME-aware skip
  - BLAKE3 checksum on original plaintext (pre-compression)
  - Free-list allocation with best-fit search and adjacent merge
  - WAL crash recovery with snapshot size validation
  - Shadow index fallback when primary is corrupted
  - Zeroize master key and plaintext data after use
  - Safe usize cast for 32-bit platform compatibility
feat(evfs): implement Vault Rust API with VaultHandle
feat(evfs): add Dart VaultService wrapper and integration tests
@Adel-Ayoub Adel-Ayoub merged commit e66d968 into dev Mar 6, 2026
5 checks passed
@Adel-Ayoub Adel-Ayoub deleted the evfs branch March 6, 2026 11:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants