Skip to main content

Hardened, self-destructing memory cells for Python secrets, powered by Rust.

Project description

cypher_cell

Python Versions License Unit Tests Latest Release Platform Rust Backend

Hardened, self-destructing memory cells for Python secrets, powered by Rust.

cypher_cell is a Python extension module (written in Rust) that provides a secure, zero-leakage memory container for sensitive data such as API keys, passwords, cryptographic material, and tokens. Unlike standard Python strings and bytes, which are immutable, interned, and can linger in RAM or swap, cypher_cell ensures your secrets are:

  • Locked in RAM: Prevented from being swapped to disk using OS-level memory locking.
  • Zeroized: Overwritten with zeros immediately when no longer needed, leaving no trace in memory.
  • Ephemeral: Optionally destroyed after a single access or a configurable time-to-live (TTL).
  • Leak-resistant: Never exposed in logs, tracebacks, or accidental prints.

Why use cypher_cell?

Python's default memory model is not designed for handling secrets. Sensitive data can be copied, cached, or swapped to disk without your control. Attackers with access to memory dumps, swap files, or process introspection tools can easily recover secrets. cypher_cell is designed for developers and security engineers who need:

  • In-memory protection for credentials in long-running apps, CLI tools, or servers
  • Defense-in-depth for cryptographic operations
  • Secure handling of ephemeral secrets (e.g., one-time tokens, session keys)
  • Compliance with security standards that require memory zeroization

Features

  • 🔒 Memory Locking: Prevents secrets from being swapped to disk (OS-level protection).
  • 🧹 Guaranteed Zeroization: Memory is physically overwritten with zeros the moment the object is dropped or expires.
  • 👻 Volatile Mode: "Burn-after-reading" logic—the cell wipes itself immediately after one access.
  • ⏳ Time-To-Live (TTL): Secrets automatically vanish after a configurable duration.
  • 🛡️ Anti-Leak repr: Prevents accidental logging; print(cell) always shows [REDACTED].

🛡️ Advanced Hardening Features

cypher_cell includes several advanced memory and security hardening techniques beyond standard secret management:

Feature Implementation Benefit
Direct Env Loading from_env Secrets loaded directly from environment variables, never touching Python's heap.
Timing Protection verify (constant-time) Protects against timing attacks by using constant-time comparison for secret verification.
Anti-Core Dump MADV_DONTDUMP On Linux, secrets are excluded from core dumps if the process crashes.
Anti-Fork MADV_DONTFORK Prevents child processes from inheriting secret memory regions.
Binary Safety reveal_bytes Safely handles raw cryptographic keys and binary secrets, even if not valid UTF-8.

Implementation Details

  • Direct Env Loading: CypherCell.from_env("VAR") loads secrets directly from environment variables, minimizing exposure to Python's garbage-collected memory.
  • Timing Protection: The verify() method uses constant-time comparison to prevent attackers from inferring secrets via timing analysis.
  • Anti-Core Dump: On Linux, memory is marked with MADV_DONTDUMP so secrets are never written to disk in crash dumps.
  • Anti-Fork: Memory is marked with MADV_DONTFORK so child processes cannot inherit secret memory.
  • Binary Safety: reveal_bytes() allows safe handling of raw binary secrets (e.g., cryptographic keys) that may not be valid UTF-8, avoiding crashes and leaks.

🚀 Installation

Clone and build locally:

git clone https://github.com/Rivendael/cypher_cell.git
cd cypher_cell
pip install maturin
maturin develop

🛠 Usage

⚠️ Pro Tip: To prevent the secret from ever hitting the Python heap, avoid CypherCell(b"my-secret"). Instead, use CypherCell.from_env("MY_SECRET") or (in future) CypherCell.from_file("/path/to/key") to load secrets directly from secure sources.

1. Basic Secure Vault

Keep a secret locked in RAM and ensure it is wiped as soon as you are done.

from cypher_cell import CypherCell

# Use as a Context Manager for maximum safety
with CypherCell(b"super-secret-key") as cell:
    # Use the secret
    db_connect(cell.reveal())
# Memory is now zeroed and unlocked

2. "Mission Impossible" Cell (Volatile + TTL)

Create a secret that disappears after one read or 30 seconds, whichever comes first.

vault = CypherCell(b"transient-key", volatile=True, ttl_sec=30)
print(vault.reveal())  # Works
print(vault.reveal())  # Raises ValueError (already wiped)

3. Masked Debugging

Reveal only what you need for logs.

cell = CypherCell(b"SK-7721-9904-1234")
print(cell.reveal_masked(suffix_len=4))  # Output: *************1234

4. Load Secret Directly from Environment

Avoids Python heap exposure by loading secrets straight from environment variables.

import os
from cypher_cell import CypherCell

os.environ["MY_SECRET"] = "env-value"
cell = CypherCell.from_env("MY_SECRET")
print(cell.reveal())  # env-value

5. Constant-Time Secret Verification

Protects against timing attacks when checking secrets.

cell = CypherCell(b"top-secret")
if cell.verify(b"top-secret"):
    print("Access granted!")
else:
    print("Access denied!")

6. Safe Binary Secret Handling

Safely work with raw cryptographic keys or binary data.

key = b"\x01\x02\x03\x04\x05\x06"
cell = CypherCell(key)
raw = cell.reveal_bytes()
assert raw == key

🏗 Architecture

cypher_cell bridges Python with low-level Rust primitives:

  • Creation: Data is copied into a Vec<u8> in Rust.
  • Locking: Calls libc::mlock (Unix) or VirtualLock (Windows) to pin memory to RAM.
  • Destruction: When the Python reference count hits zero or __exit__ is called, Rust executes the Drop trait, which calls zeroize and then unlocks the memory.

Known Weaknesses & Usage Tips

While cypher_cell protects the data within its vault, the act of passing a string to CypherCell or calling .reveal() creates temporary copies in Python's unmanaged memory. For maximum security, use the context manager and minimize the lifetime of the revealed string.

Note on .reveal(): When you call .reveal(), Python creates a standard, immutable string. While cypher_cell wipes its own internal memory, it cannot wipe the string Python just created. Always use secrets in the narrowest scope possible:

Warning on Literals: Avoid passing string literals directly like CypherCell("my_secret"). Python may intern these strings, keeping them in memory for the duration of the process regardless of what cypher_cell does. Always load from environment variables, files, or buffers.

# GOOD: String is short-lived
authenticate(cell.reveal())

# BAD: Secret lingers in the 'key' variable
key = cell.reveal()
authenticate(key)

🧪 Testing

Run the test suite with:

pytest tests/

⚖️ License

MIT © Rivendael

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cypher_cell-0.1.4.tar.gz (18.9 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

cypher_cell-0.1.4-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (212.8 kB view details)

Uploaded PyPymanylinux: glibc 2.17+ x86-64

cypher_cell-0.1.4-cp310-abi3-win_amd64.whl (120.2 kB view details)

Uploaded CPython 3.10+Windows x86-64

cypher_cell-0.1.4-cp310-abi3-musllinux_1_1_x86_64.whl (227.3 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.1+ x86-64

cypher_cell-0.1.4-cp310-abi3-musllinux_1_1_aarch64.whl (217.4 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.1+ ARM64

cypher_cell-0.1.4-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (215.8 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ x86-64

cypher_cell-0.1.4-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (218.4 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

cypher_cell-0.1.4-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (391.9 kB view details)

Uploaded CPython 3.10+macOS 10.12+ universal2 (ARM64, x86-64)macOS 10.12+ x86-64macOS 11.0+ ARM64

File details

Details for the file cypher_cell-0.1.4.tar.gz.

File metadata

  • Download URL: cypher_cell-0.1.4.tar.gz
  • Upload date:
  • Size: 18.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cypher_cell-0.1.4.tar.gz
Algorithm Hash digest
SHA256 020c9b932a05df70982bbf76c81058368b4c8cb619b7f684b5bfd6a26ce846a1
MD5 d52a28fc3d580169470ac4b3a6b79cbd
BLAKE2b-256 c708727627602f2d176f49c6523f1394a64bc7a0636ac2e0b85b00e914977b68

See more details on using hashes here.

Provenance

The following attestation bundles were made for cypher_cell-0.1.4.tar.gz:

Publisher: build-wheels.yml on Rivendael/cypher_cell

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cypher_cell-0.1.4-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cypher_cell-0.1.4-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ca522cfd587cb1887fd639144466e5f1906f718d7182857c5bab4e72d70f2cb8
MD5 4b7fec976541e34f001d89c3297900bf
BLAKE2b-256 bed36629de2d4f774ae4d4474a9f31da23f4bf829a93f6a4f0ea8788d0db25c3

See more details on using hashes here.

Provenance

The following attestation bundles were made for cypher_cell-0.1.4-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build-wheels.yml on Rivendael/cypher_cell

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cypher_cell-0.1.4-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: cypher_cell-0.1.4-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 120.2 kB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cypher_cell-0.1.4-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 cb127c6095cd64f3bc4737665178c10c535afb5ad4634d9de715bdebf3aa9322
MD5 9ce8922782c15bc858df0a2fc0a1765c
BLAKE2b-256 37d3c8eeb0a35d6dea521bf9f158fa9f7231bde25298f5903c1a8630f6502793

See more details on using hashes here.

Provenance

The following attestation bundles were made for cypher_cell-0.1.4-cp310-abi3-win_amd64.whl:

Publisher: build-wheels.yml on Rivendael/cypher_cell

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cypher_cell-0.1.4-cp310-abi3-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for cypher_cell-0.1.4-cp310-abi3-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 e28bc7c514598a224ebc91469051837879395c70350cdc91231c32776c5bce1f
MD5 e75ce7773c1788ebf46f408a9665efd2
BLAKE2b-256 34601621fedd2652be3d963e6fefb5348d188e67a56afe2a1e75802da81e59f0

See more details on using hashes here.

Provenance

The following attestation bundles were made for cypher_cell-0.1.4-cp310-abi3-musllinux_1_1_x86_64.whl:

Publisher: build-wheels.yml on Rivendael/cypher_cell

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cypher_cell-0.1.4-cp310-abi3-musllinux_1_1_aarch64.whl.

File metadata

File hashes

Hashes for cypher_cell-0.1.4-cp310-abi3-musllinux_1_1_aarch64.whl
Algorithm Hash digest
SHA256 fcec2a79d54dd4ade11b89c024da9340d100b6abff39e9b9d8f02d2d3d76a421
MD5 1385b47466f7d0a424bc3f1f95f1c6bc
BLAKE2b-256 d23a2aa8898e4f3ddebf5a63cafb7779bc34835ff2786f39915ac8f5ebbd1eed

See more details on using hashes here.

Provenance

The following attestation bundles were made for cypher_cell-0.1.4-cp310-abi3-musllinux_1_1_aarch64.whl:

Publisher: build-wheels.yml on Rivendael/cypher_cell

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cypher_cell-0.1.4-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cypher_cell-0.1.4-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 697c2e5286e3f5a446faa027b9bca2ba9f371eeb4312e5bac3ec860e72bbbb22
MD5 4635f7dc537915cb3a73c47779f4bbce
BLAKE2b-256 7d29be7bdd8554315ad610116a141a2b6fe2f863c37ce756db1db0ef6731b4cb

See more details on using hashes here.

Provenance

The following attestation bundles were made for cypher_cell-0.1.4-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build-wheels.yml on Rivendael/cypher_cell

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cypher_cell-0.1.4-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for cypher_cell-0.1.4-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 01f3f13d9070629f11d91aa55f615ae1084ed3aac047956fb4d6b7a858d9e777
MD5 6c5540781b0b52c79329a9c6b6b1c97a
BLAKE2b-256 3dbed4cb3d1f03641e7a8b2b0babc3cb540bf3cec5217a43d966cad9b9c18a6c

See more details on using hashes here.

Provenance

The following attestation bundles were made for cypher_cell-0.1.4-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: build-wheels.yml on Rivendael/cypher_cell

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cypher_cell-0.1.4-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl.

File metadata

File hashes

Hashes for cypher_cell-0.1.4-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 3c7304471d9a23b4d71f465d67e3fa7094719f66a31d17c0fa93a730e3f79d6f
MD5 472e5f76d9670372196f2dc0c424f63d
BLAKE2b-256 f75632e4499536f92ee4c30afa5c0c751a68e8557b4bb9d0361b76c5967cbbd0

See more details on using hashes here.

Provenance

The following attestation bundles were made for cypher_cell-0.1.4-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl:

Publisher: build-wheels.yml on Rivendael/cypher_cell

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page