GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - samjanny/memseal: A small, boring Rust vault for password-protected named secrets.

BranchesTags
Open more actions menu

Latest commit

 

History

53 Commits
53 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
benches
benches
 
 
fuzz
fuzz
 
 
src
src
 
 
tests/fixtures
tests/fixtures
 
 
.gitignore
.gitignore
 
 
CHANGELOG.md
CHANGELOG.md
 
 
Cargo.lock
Cargo.lock
 
 
Cargo.toml
Cargo.toml
 
 
DESIGN.md
DESIGN.md
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
ROADMAP.md
ROADMAP.md
 
 
SECURITY.md
SECURITY.md
 
 

Repository files navigation

memseal

CI crates.io docs.rs License: MIT

A small password-based encrypted vault for named secrets, with authenticated encryption, bounded parsing, and explicit memory-hygiene trade-offs.

Status: memseal is an experimental 0.x crate and has not been independently audited.

Note: This crate is not a wrapper around Linux mseal(2). "memseal" refers to sealing secrets in an encrypted in-memory vault.

Overview

memseal stores named secrets in an encrypted vault protected by a password.

It is designed for applications that need a small, self-contained encrypted vault that can be kept in memory, exported to bytes, or saved to disk.

It is not a replacement for OS keyrings, HSMs, cloud secret managers, or mature password managers.

Quick Start

use memseal::Vault;

let mut vault = Vault::create(b"my-password-here").unwrap();

// Store secrets
vault.store("api_key", b"sk-secret-12345").unwrap();
vault.store("db_url", b"postgres://user:pass@host/db").unwrap();

// Export to bytes
let bytes = vault.export().unwrap();

// Reopen with the same password
let vault = Vault::open(b"my-password-here", &bytes).unwrap();
let matches = vault
    .with_secret("api_key", |secret| secret == b"sk-secret-12345")
    .unwrap();

assert_eq!(matches, Some(true));

File Persistence

use memseal::{Vault, VaultError};
use std::path::Path;

fn main() -> Result<(), VaultError> {
    let mut vault = Vault::create(b"my-password-here")?;

    vault.store("api_key", b"sk-secret-12345")?;

    // Save to disk
    vault.save(Path::new("secrets.seal"))?;

    // Later: load and retrieve
    let vault = Vault::load(Path::new("secrets.seal"), b"my-password-here")?;
    let matches = vault.with_secret("api_key", |secret| {
        secret == b"sk-secret-12345"
    })?;
    assert_eq!(matches, Some(true));

    Ok(())
}

API

use memseal::{Vault, VaultError};
use std::path::Path;

fn main() -> Result<(), VaultError> {
    let password = b"my-password-here";

    // Create
    let mut vault = Vault::create(password)?;

    // Store, access, remove
    vault.store("name", b"secret")?;
    let matches = vault.with_secret("name", |secret| secret == b"secret")?;
    assert_eq!(matches, Some(true));

    // List: count, membership, and the names themselves (sorted)
    assert_eq!(vault.len(), 1);
    assert!(!vault.is_empty());
    assert!(vault.contains("name")?);
    let listed = vault.with_names(|names| names.len())?; // names wiped after the closure
    assert_eq!(listed, 1);
    assert_eq!(vault.names()?, ["name"]); // caller-owned copy

    let existed = vault.remove("name")?; // bool
    assert!(existed);

    // Change password
    vault.change_password(password, b"new-password-here")?;

    // Export to bytes, then reopen with the new password
    let bytes = vault.export()?;
    let reopened = Vault::open(b"new-password-here", &bytes)?;
    assert_eq!(reopened.retrieve("name")?, None);

    // File I/O
    vault.save(Path::new("vault.seal"))?;
    let loaded = Vault::load(Path::new("vault.seal"), b"new-password-here")?;
    let _ = loaded;

    Ok(())
}

Limits

Item Limit
Password length Minimum 8 bytes
Entry name Maximum 255 bytes
Entry data Maximum 64 MiB
Vault file Maximum 256 MiB
Index entries Maximum 1024
Header JSON Maximum 4 KiB
KDF parameters accepted by open() 32 MiB to 256 MiB memory, 3 to 10 iterations

The 256 MiB file bound is enforced on both sides: load() and open() refuse larger input and export()/save() refuse to produce it. The index is a compact binary encoding (128 bytes plus the name length per entry, on top of the secret itself), so the bound on total stored secrets is a little under 256 MiB; export() fails cleanly when it is exceeded and the vault stays usable.

Vault files written by releases up to 0.2.1 (format version 1: JSON index, Argon2i) are still opened transparently. Opening one costs a second key derivation, because the vault is re-keyed with Argon2id in memory; the next export() or save() then writes the current format. See DESIGN.md section 12.

Errors

Every fallible call returns VaultError. The enum is #[non_exhaustive], so a match on it needs a wildcard arm:

use memseal::{Vault, VaultError};

fn main() {
    let bytes = {
        let mut vault = Vault::create(b"my-password-here").unwrap();
        vault.store("api_key", b"sk-secret-12345").unwrap();
        vault.export().unwrap()
    };

    match Vault::open(b"wrong-password", &bytes) {
        Ok(_) => unreachable!("the password is wrong"),
        // Wrong password, or the bytes were tampered with: the AEAD gives a
        // single verdict, so the library cannot tell the two apart. Do not
        // retry in a loop; every attempt repeats the Argon2 derivation.
        Err(VaultError::InvalidPassword) => {}
        // Length fields, header, or index are malformed or out of bounds.
        Err(VaultError::CorruptedData(msg)) => panic!("corrupted vault: {msg}"),
        Err(other) => panic!("unexpected error: {other}"),
    }
}
Variant Meaning
InvalidPassword The encrypted index failed to authenticate: wrong password, or a tampered, truncated, or corrupted vault (a file cut off before the end of the smallest possible index is CorruptedData instead). Also returned by change_password() when the current password does not match.
InvalidInput(String) An argument violated a documented limit: password shorter than 8 bytes, entry name over 255 bytes, entry data over 64 MiB, or a new name when the vault already holds 1024 entries.
CorruptedData(String) The vault bytes violate the format or a documented bound (header length, KDF parameters, minimum index length, index version, entry count, entry sizes), or the authenticated index is internally inconsistent. Anything found before authentication is reported without running Argon2.
CryptoError(String) A cryptographic or random-number operation failed, or memory could not be allocated: the locked pages for the subkeys, or the Argon2id working memory when the allocator refuses it.
SerializationError(String) Serializing the vault failed, or the export exceeds 256 MiB.
IoError(std::io::Error) A file operation in load() or save() failed.

A missing entry is not an error: retrieve() and with_secret() return Ok(None), and remove() returns Ok(false).

Upgrading from 0.1.x or 0.2.0: the variants InvalidHeader, InvalidIndex, InvalidDataBlock, InvalidMetaBlock, InvalidPath, InvalidVersion, InvalidFormat, and EntryNotFound were removed in 0.2.1. None of them was ever returned, so only exhaustive match expressions need to change. Upgrading from 0.2.1: InvalidKey is removed (it could not be returned either), limit violations now surface as InvalidInput instead of CryptoError, and an inconsistent counter in an otherwise authenticated index is reported by open() as CorruptedData instead of CryptoError. Two things outside the error type also change: a vault file is rewritten in format version 2 the first time it is saved by 0.3.0, and 0.2.1 cannot read that format, so keep a backup of each file before upgrading an application that may need to roll back; and the minimum supported Rust version is now 1.88.

Handling Plaintext

Prefer with_secret() when plaintext only needs to be used temporarily. It decrypts the requested value, lends it to a closure as &[u8], then zeroizes the library-owned plaintext allocation as soon as the closure returns:

use memseal::{Vault, VaultError};

fn main() -> Result<(), VaultError> {
    let mut vault = Vault::create(b"my-password-here")?;
    vault.store("api_key", b"sk-secret-12345")?;

    let authenticated = vault.with_secret("api_key", |secret| {
        // Use the borrowed plaintext only inside this closure.
        secret == b"sk-secret-12345"
    })?;
    assert_eq!(authenticated, Some(true));

    Ok(())
}

The callback is not called for a missing entry and with_secret() returns Ok(None). Its return value may leave the closure, but borrowed plaintext cannot. A caller can still explicitly copy the bytes inside the callback; any such copy is caller-owned and cannot be cleared by the vault.

For convenience and backwards compatibility, retrieve() returns decrypted data as Option<Vec<u8>>.

This is convenient, but it means the caller owns the returned plaintext and is responsible for handling it carefully.

In particular, caller code should avoid:

  • logging returned secrets;
  • cloning or converting them unnecessarily;
  • keeping plaintext alive longer than needed;
  • assuming returned plaintext is protected by mlock.

Internal temporary plaintext and key material are zeroized where possible, but plaintext returned by retrieve() belongs to the caller.

Entry names follow the same split. The vault never keeps a name in plaintext: it files each entry under an HMAC of its name and stores the name itself encrypted. with_names() decrypts those copies, lends the sorted names to a closure as &[String], and zeroizes them when the closure returns, as with_secret() does for a secret; names() returns them as a Vec<String> that the caller owns and the vault cannot clear. Prefer with_names() when the names are sensitive in themselves. len(), is_empty(), and contains() decrypt nothing.

Vault implements Debug so that it can be unwrapped out of a Result or held in a struct that derives Debug. The output is the number of entries and nothing else (Vault { entries: 2, .. }).

If the caller wants drop-time zeroization, the returned Vec<u8> can be wrapped by the caller using zeroize::Zeroizing:

use memseal::{Vault, VaultError};
use zeroize::Zeroizing;

fn main() -> Result<(), VaultError> {
    let mut vault = Vault::create(b"my-password-here")?;
    vault.store("api_key", b"sk-secret-12345")?;

    if let Some(secret) = vault.retrieve("api_key")? {
        let secret = Zeroizing::new(secret);

        // Use secret here.
        assert_eq!(secret.as_slice(), b"sk-secret-12345");

        // This allocation will be zeroized when `secret` is dropped.
    }

    Ok(())
}

This only zeroizes that returned allocation on drop. It does not prevent accidental copies made by caller code or by the allocator/runtime.

What memseal is

  • A small embedded vault for named secrets.
  • Password-based: vault keys are derived from a caller-provided password.
  • Self-contained: vaults can be exported to bytes or saved to disk.
  • Authenticated: encrypted data is protected against tampering.
  • Explicit about its limitations and memory-hygiene trade-offs.

What memseal is not

  • Not independently audited.
  • Not an OS keyring wrapper.
  • Not a cloud secret manager.
  • Not an HSM.
  • Not a password manager.
  • Not a general secure-memory allocator.
  • Not related to Linux mseal(2).

Intended Use Cases

  • Embedded encrypted vaults - Store named secrets in an application-managed encrypted vault.
  • Portable secret bundles - Export/load a password-protected vault without relying on an OS credential store.
  • Credential caches - Keep secrets encrypted at rest in memory and on disk, while accepting explicit caller-owned plaintext boundaries.
  • Application-managed secret storage - Store small sets of API keys, tokens, or credentials where a lightweight Rust-native vault is appropriate.

Threat Model

For the exact byte format, key derivation chain, nonce derivation, and AAD bindings that back these claims, see DESIGN.md.

Intended mitigations

Threat Mitigation
Tampered vault data The vault header is authenticated as AAD for index decryption; the index and every per-entry payload are encrypted and authenticated with XChaCha20-Poly1305. Bit flips in authenticated header fields, nonces, ciphertext, or Poly1305 tags are detected during open() or entry retrieval.
Entry swap attacks Each entry's data and name ciphertexts share an AAD made of the entry's HMAC-derived key and its data_counter. Swapping either the encrypted data or the encrypted name across entries causes AEAD verification to fail.
Entry name leakage in serialized vaults Entry names are not stored in plaintext. Index keys are derived with HMAC-SHA256.
KDF parameter downgrade The vault header is authenticated as AAD, so tampering with persisted KDF parameters is detected. Header KDF fields are also bounded before they reach Argon2: open() accepts only 32 MiB to 256 MiB of memory and 3 to 10 iterations, so a forged header can neither make password guessing cheap nor force more than one 256 MiB, 10-pass derivation before the file is rejected. See DESIGN.md section 10.1.
Nonce reuse All XChaCha20 nonces are 24-byte values drawn from the OS CSPRNG and stored next to the ciphertext they protect: a fresh index nonce on every export(), fresh entry data/name nonces on every store(). Random generation also covers state forks: two vault instances opened from the same persisted bytes cannot repeat a nonce when both export, which counter-derived nonces would. Monotonic counters remain as authenticated state, are bound into entry AAD, and overflow at u64::MAX is a hard error.
Key reuse across roles The Argon2id-derived master key is never used directly. HKDF-SHA256 derives two 32-byte subkeys with disjoint info strings: one for XChaCha20-Poly1305, one for HMAC-SHA256 entry-name hashing. The master key is zeroized as soon as both subkeys exist.
Plaintext lifetime inside the library Internal temporary plaintext and key material are zeroized where possible, including error paths.
Resource exhaustion from crafted files Vault file size (256 MiB), header JSON length (4 KiB), KDF parameters, entry name length, entry data size, per-entry ciphertext sizes, and the decoded index entry count are bounded before processing. The binary index decoder checks every length against the remaining input and the documented bounds before copying, and rejects duplicate keys and trailing bytes. The residual, deliberate cost of opening a hostile file is one bounded Argon2id derivation.
Swap and core-dump exposure of in-memory subkeys Each subkey lives in a LockedSecret: a memsec allocation that occupies pages of its own, sits between guard pages, is locked with mlock where supported (on Linux also excluded from core dumps), and is zeroized on drop.

Out of scope / limitations

Threat Reason
Kernel-level or root attacker A privileged attacker can read process memory regardless of user-space protections.
Debugger-based extraction A debugger attached to the process can read decrypted data while it is being processed, including inside a with_secret() callback, or after it has been returned by retrieve().
Caller-owned plaintext leaks with_secret() keeps the library-owned buffer scoped to a callback and zeroizes it afterward, but cannot prevent the callback from making copies. retrieve() returns Vec<u8> directly, and names() returns the entry names as Vec<String> (with_names() is its scoped counterpart). The caller remains responsible for avoiding logs, copies, long-lived plaintext, and unsafe conversions.
Side-channel attacks memseal does not attempt to mitigate Spectre, cache timing, power analysis, or other side channels. This includes the timing difference between a hit and a miss in retrieve()/with_secret(), which reveals whether a name exists to anyone who can time calls; contains() answers the same question outright.
Compromised dependencies The crate trusts its dependency chain, including orion, argon2, memsec, and zeroize.
Denial of service memseal detects corruption and refuses to open tampered files, but it cannot recover from them. An attacker with write or delete access to the vault file can deny access until a clean copy is restored. Backups and replication are the integrator's responsibility.
Full swap protection Only the two LockedSecret allocations that hold the subkeys are locked. Transient copies made during each operation, allocator metadata, returned plaintext, and caller-owned copies are outside that guarantee.
Rollback protection memseal does not provide rollback protection. An attacker who can replace a vault file with an older valid copy can cause the application to load older data unless the application stores freshness/version information externally.
File permissions on Windows save() creates the file with mode 0600 on Unix only. On Windows the file inherits the DACL of its directory; restricting it is the caller's job.
Untrusted paths load() and save() follow symbolic links like any std::fs call and do not validate the path. Callers that accept paths from untrusted sources must validate them first; open()/load() bound the work a hostile file can cause but do not make it free.
Formal cryptographic assurance The crate has not been independently audited. The integration layer should be reviewed before high-risk use.

Architecture

            Password (>= 8 bytes)
               |
          Argon2id
      128 MiB, 4 iterations
      random 16-byte salt
               |
          Master Key (32B)
               |
         HKDF-SHA256
         salt = KDF salt
          /         \
    enc_subkey    hmac_subkey
      (32B)         (32B)
        |              |
        |              +--> HMAC-SHA256 entry-name hashing
        |
        +--> XChaCha20-Poly1305 encryption

Per-entry encryption:
  nonce = random 24 bytes (OS CSPRNG), stored as the ciphertext prefix
  aad   = hex(HMAC-SHA256(hmac_subkey, plaintext_name)) || data_counter (u64 LE)
  ct    = XChaCha20-Poly1305(enc_subkey, nonce, plaintext, aad)

Index encryption:
  fresh random index nonce on every export, stored in the file
  vault header is authenticated as AAD

Note: mlock is applied only to the two LockedSecret allocations that hold the subkeys. The subkeys are stored in the clear inside those locked pages: encrypting them in memory would need a key with the same protection, so it would add nothing an attacker who can read the pages does not already get. The lock does not cover every secret-related allocation. Each container occupies at least one page of its own, so every open Vault holds two locked pages (8 KiB with 4 KiB pages) against RLIMIT_MEMLOCK, and change_password() briefly needs four while the old and the new index coexist (so does open() on a file written by 0.2.1, which is re-keyed the same way). Vault::create, open, load, and change_password fail with CryptoError when the limit is exhausted rather than fall back to unlocked memory.

Cryptographic Primitives

Primitive Implementation Purpose
Argon2id argon2 (RustCrypto) Password-based key derivation (Argon2i from orion for format version 1 files)
HKDF-SHA256 orion Subkey derivation
XChaCha20-Poly1305 orion Authenticated encryption
HMAC-SHA256 orion Entry-name hashing
OS CSPRNG getrandom Random salt and nonce generation
mlock / munlock memsec Page-exclusive locked allocations for the two subkeys
Zeroization zeroize Clearing internal temporary secrets where possible

Security Properties

  • Small public API. The crate exposes Vault and VaultError; internal modules are private.
  • Unsafe code is isolated. The crate uses #![deny(unsafe_code)] at the crate root. The memory-locking module explicitly allows unsafe code for the memsec allocation and locking calls and for the Send/Sync impls of the type that uniquely owns each locked region.
  • Domain separation. Subkey derivation uses distinct HKDF info labels for the encryption and HMAC keys.
  • Authenticated encryption. Vault index data and entries are encrypted with AEAD.
  • Bounded parsing. Untrusted vault data is checked against size and parameter bounds before processing; a hostile file can cost at most one bounded Argon2id derivation (256 MiB, 10 passes) before it is rejected.
  • Atomic file writes. save() writes to a temporary file, fsyncs it, renames it, and uses 0600 permissions on Unix (Windows inherits the directory ACL).
  • Best-effort memory hygiene. Internal temporary key material and plaintext are zeroized where possible.
  • Partial swap protection. The two subkey containers are locked with mlock where supported, but this does not cover every allocation.

Comparison

memseal is not a replacement for lower-level memory-hygiene crates such as zeroize, secrecy, or memsec.

It is also not an OS credential-store wrapper like keyring.

memseal is useful when an application wants a small, portable, self-contained encrypted vault for named secrets that it can export, save, and load directly.

Development

cargo build
cargo test
cargo fmt --all -- --check
cargo clippy --all-targets -- -D warnings

Nearly every test derives a key with Argon2 over 128 MiB. Cargo.toml therefore optimizes four dependencies in the dev profile (argon2, blake2, orion, zeroize), which brings cargo test from over two minutes down to about half a minute while memseal itself still builds unoptimized, with debug assertions and overflow checks on. The first build compiles those four crates at opt-level = 3; nothing changes for crates that depend on memseal, since profiles apply only to the package being built.

Run benchmarks with:

cargo bench --bench full_bench

Fuzz the untrusted-input parsers (nightly toolchain and cargo-fuzz):

cargo install cargo-fuzz
mkdir -p fuzz/corpus/parse_envelope
cargo +nightly fuzz run parse_envelope fuzz/corpus/parse_envelope fuzz/seeds/parse_envelope -- -max_total_time=300

The targets (parse_envelope, decode_index, decode_index_v1) call the parsers through the crate's fuzzing feature, which is not part of the public API and must never be enabled by a dependency. See fuzz/README.md.

CI

GitHub Actions runs on every push and PR to main:

  • cargo check --all-targets, with and without --features fuzzing
  • cargo fmt --all -- --check
  • cargo clippy --all-targets -- -D warnings, with and without --features fuzzing
  • cargo test on Linux, macOS, and Windows
  • cargo check --all-targets --locked on the MSRV toolchain (1.88)
  • cargo check --manifest-path fuzz/Cargo.toml --locked, which compiles the fuzz targets (nothing is fuzzed in CI) and fails if fuzz/Cargo.lock has fallen behind the dependencies of the main crate
  • rustsec/audit-check action

Everything except the test job runs on Linux only.

MSRV

memseal uses the Rust 2024 edition and declares rust-version = "1.88" in Cargo.toml, the first release with let chains. CI checks the crate builds on that toolchain.

Security

This crate has not been independently audited.

Please report security issues privately. See SECURITY.md.

License

MIT - see LICENSE.

About

A small, boring Rust vault for password-protected named secrets.

Topics

argon2cryptographyencryptionmemory-allocationmemory-managementrustsecretssecurityvaultxchacha20poly1305zeroization

Resources

Readme
MIT license

Security policy

Security policy
Activity

Stars

0 stars

Watchers

0 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.