Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:1ay1 Agentty Credential At Rest Encryption

From Leeroopedia
Knowledge Sources
Domains Security, Cryptography
Last Updated 2026-10-28 12:00 GMT

Overview

AES-256-GCM authenticated-encryption envelope for the credentials file, with machine-bound key derivation and optional passphrase hardening.

Description

The agentty::auth::crypt module wraps the credentials JSON payload in an authenticated-encryption envelope so the on-disk bytes are opaque. It is deliberately NOT a substitute for an OS keystore — the key is derived from machine-stable material, not stored in a hardware/OS vault, so a determined attacker running code as the same user on the same machine can re-derive it. What it defends against (at zero new dependency and zero UX cost) is the far more common exposure: the file leaving the machine (backup, cloud-sync, screenshot, `cat`) still readable.

Crypto: AES-256-GCM via OpenSSL EVP. The 256-bit key is `HKDF-SHA256(machine_seed, salt)` where `machine_seed` mixes a per-machine id, numeric user id, and a fixed app-specific context string. A fresh random 128-bit salt + 96-bit nonce are generated on every save, so re-encrypting identical plaintext never produces identical ciphertext.

Optional passphrase mode: the key also folds in a memory-hard `scrypt(passphrase, salt)` factor, closing the local-attacker gap. Enabled via `AGENTTY_PASSPHRASE=…` (non-interactive) or `AGENTTY_ENCRYPT_PASSPHRASE=1` (interactive prompt on `/dev/tty`).

Usage

Import this when you need to seal/unseal credential files at rest. Used by `auth::save/load_credentials`, `auth::keys::save/load`, and `auth::accounts`.

Code Reference

Source Location

Signature

namespace agentty::auth::crypt {

// True iff a passphrase has been configured for this process.
bool passphrase_active();

// Encrypt plaintext into a JSON envelope. Returns nullopt only on hard
// OpenSSL failure — callers should refuse to persist rather than write
// plaintext.
std::optional<std::string> seal(const std::string& plaintext);

// Decrypt a seal() envelope. Returns nullopt if input isn't a v1/v2
// envelope (legacy plaintext — caller tries direct parse + re-encrypts),
// or if authentication fails (tampered / wrong machine / corrupt).
std::optional<std::string> unseal(const std::string& envelope);

// Cheap structural probe: does this look like a sealed envelope?
bool looks_sealed(const std::string& s) noexcept;
}

Import

#include "agentty/auth/cred_crypt.hpp"

I/O Contract

Inputs

Name Type Required Description
plaintext std::string Yes (seal) The JSON credentials payload to encrypt
envelope std::string Yes (unseal) The JSON envelope from a prior seal()
AGENTTY_PASSPHRASE env var No Passphrase for v2 scrypt hardening
AGENTTY_ENCRYPT_PASSPHRASE env var No Set to 1 to prompt for passphrase on /dev/tty

Outputs

Name Type Description
seal() std::optional<std::string> JSON envelope: {v, enc, salt, nonce, ct, tag}; nullopt on OpenSSL failure
unseal() std::optional<std::string> Decrypted plaintext; nullopt if not an envelope or auth failed

Usage Examples

Seal and Unseal Credentials

#include "agentty/auth/cred_crypt.hpp"

// Encrypt credentials before writing to disk
auto envelope = agentty::auth::crypt::seal(credentials_json);
if (envelope) {
    write_file("credentials.json", *envelope);  // opaque bytes on disk
} else {
    // OpenSSL failure — refuse to persist, never write plaintext
}

// Read back and decrypt
std::string raw = read_file("credentials.json");
if (agentty::auth::crypt::looks_sealed(raw)) {
    auto plain = agentty::auth::crypt::unseal(raw);
    if (plain) { /* parse *plain as JSON */ }
    else { /* tampered / wrong machine / corrupt — fail closed */ }
} else {
    // Legacy plaintext — parse directly, re-encrypt on next save
}

Enable Passphrase Hardening

# Non-interactive (CI / scripts):
export AGENTTY_PASSPHRASE="my-secret-passphrase"
agentty login

# Interactive (prompted once on /dev/tty):
export AGENTTY_ENCRYPT_PASSPHRASE=1
agentty login
# Now the key folds in scrypt(passphrase, salt) — can't be decrypted
# without BOTH the machine AND the secret the user knows.

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment