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 now uses a Scoped View Pattern: secrets are only accessible via a temporary CypherView handle, obtained by entering a context manager (with block). Sensitive data is not directly accessible from the CypherCell instance. The view is only valid within the context and is automatically invalidated when the block exits.

Key security features:

  • 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.
  • Scoped Access: Data is only accessible via a CypherView inside a with block. Access outside the block raises ValueError: View expired.
  • Volatile & TTL: 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

  • String and Bytes Support: CypherCell now accepts both bytes and str as input. Passing a string is supported for convenience, but is less secure than passing bytes (see below).
  • Scoped View Pattern: Secrets are only accessible via a CypherView object, valid only inside a with block.
  • Automatic Invalidation: Exiting the context manager invalidates the view; further access raises ValueError: View expired.
  • Volatile Mode: If volatile=True, the cell is wiped (zeroized) immediately after the context exits.
  • TTL Enforcement: Time-To-Live is checked both when entering the context and when accessing the view.
  • Memory Locking: Prevents secrets from being swapped to disk (OS-level protection).
  • 🛡️ Anti-Leak repr: Prevents accidental logging; print(cell) always shows [REDACTED].

🛡️ Advanced Hardening Features

cypher_cell includes advanced memory and security hardening:

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 bytes(view) 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: Use bytes(view) for raw binary secrets. Use str(view) for UTF-8 strings (raises if invalid).

🚀 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 (Scoped View)

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

from cypher_cell import CypherCell

# You can now pass either bytes or str to CypherCell:
with CypherCell("super-secret-key") as view:  # str input (less secure)
    secret_str = str(view)
    db_connect(secret_str)

with CypherCell(b"super-secret-key") as view:  # bytes input (recommended)
    secret_bytes = bytes(view)
    db_connect(secret_bytes)
# After the block, view is invalidated and memory is zeroed

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)
with vault as view:
    print(bytes(view))  # Works
# After context exit, vault is wiped and cannot be accessed again
try:
    with vault as view:
        print(bytes(view))
except ValueError:
    print("Cell is wiped")

3. 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")
with cell as view:
    print(str(view))  # env-value

4. 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!")

5. Safe Binary Secret Handling

Safely work with raw cryptographic keys or binary data.

key = b"\x01\x02\x03\x04\x05\x06"
cell = CypherCell(key)
with cell as view:
    raw = bytes(view)
    assert raw == key

6. Compare two CypherCell Objects

# Compare two secure cells without revealing secrets to the Python heap
cell_a = CypherCell.from_env("MASTER_KEY")
cell_b = CypherCell(b"MASTER_KEY_VALUE")

if cell_a.compare(cell_b):
    print("Keys match!")

🏗 Architecture

cypher_cell bridges Python with low-level Rust primitives:

  • Creation: Data is copied into a Vec<u8> in Rust and locked in RAM.
  • Scoped View: Access to secrets is only possible via a temporary CypherView object, valid inside a context manager.
  • Locking: Calls libc::mlock (Unix) or VirtualLock (Windows) to pin memory to RAM.
  • Destruction: When the context exits or TTL expires, Rust executes the Drop trait, which calls zeroize and then unlocks the memory.

authenticate(key)

Known Weaknesses & Usage Tips

Security Tip: While CypherCell safely locks and zeroizes the data it holds, passing a standard Python str or bytes literal (e.g., CypherCell("secret")) leaves a temporary copy in Python's unmanaged heap. For maximum protection against memory forensics, use CypherCell.from_env() or load secrets into a bytearray that you zero out manually after the cell is created.

...existing code...
# GOOD: Data is short-lived and only accessible inside the context
with cell as view:
    authenticate(bytes(view))

# BAD: Secret lingers in the 'key' variable outside the context
with cell as view:
    key = bytes(view)
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.5.tar.gz (18.1 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.5-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (210.9 kB view details)

Uploaded PyPymanylinux: glibc 2.17+ x86-64

cypher_cell-0.1.5-cp310-abi3-win_amd64.whl (119.8 kB view details)

Uploaded CPython 3.10+Windows x86-64

cypher_cell-0.1.5-cp310-abi3-musllinux_1_1_x86_64.whl (225.6 kB view details)

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

cypher_cell-0.1.5-cp310-abi3-musllinux_1_1_aarch64.whl (216.0 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.1+ ARM64

cypher_cell-0.1.5-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (214.0 kB view details)

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

cypher_cell-0.1.5-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (217.1 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

cypher_cell-0.1.5-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (388.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.5.tar.gz.

File metadata

  • Download URL: cypher_cell-0.1.5.tar.gz
  • Upload date:
  • Size: 18.1 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.5.tar.gz
Algorithm Hash digest
SHA256 a3e969148b69a00c0c68a7cc6d30a35e61994c911c971499c57c9ca805fb278d
MD5 dd5ca5777f055edea624f1658de6fc1d
BLAKE2b-256 902f4ec54a7c4762a7a80a7627338a2f14c9a52a236605ba71d6e16b5e8abdc6

See more details on using hashes here.

Provenance

The following attestation bundles were made for cypher_cell-0.1.5.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.5-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cypher_cell-0.1.5-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d9c8f0d24407b31b542c07bcd3f140331104fa147a1625eae10bf55b2164458c
MD5 7ac8b6b11e12cbf111c6d44eb265b12c
BLAKE2b-256 f801aea1493631965785b610e654785e7710bf81fa090893b32d919a6b213ae3

See more details on using hashes here.

Provenance

The following attestation bundles were made for cypher_cell-0.1.5-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.5-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: cypher_cell-0.1.5-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 119.8 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.5-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 4495cc727f3a5e33c718d8188cf563249a23847baf91cc4f6d8968801a5e5c28
MD5 71925a826673895ec008ebba58b3a4ca
BLAKE2b-256 0cb2db1ea06c8a9308d4c0b9d4bf100433933f541471946ceef8a9cc0c155d0a

See more details on using hashes here.

Provenance

The following attestation bundles were made for cypher_cell-0.1.5-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.5-cp310-abi3-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for cypher_cell-0.1.5-cp310-abi3-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 d786183215befb41d205a4123d7c59d6d21609a7359cf43ff79061ab8253eebe
MD5 66916cf5319e33157936ec4d7548ea19
BLAKE2b-256 21ed12818f7996d4c787e7993c73e28c58bd3ccff7d84b73b309adfb13028b50

See more details on using hashes here.

Provenance

The following attestation bundles were made for cypher_cell-0.1.5-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.5-cp310-abi3-musllinux_1_1_aarch64.whl.

File metadata

File hashes

Hashes for cypher_cell-0.1.5-cp310-abi3-musllinux_1_1_aarch64.whl
Algorithm Hash digest
SHA256 6e8af78870d3ea318a46da2f97da07c02713cadfb599ef91b6bcda18fe831c30
MD5 98e90fab7f3aa9a45ca5e500bed20f4d
BLAKE2b-256 23ee8f43295d158e04d3eda39eff2a320c4d8d32a6f6d64af0c4751c03ee1575

See more details on using hashes here.

Provenance

The following attestation bundles were made for cypher_cell-0.1.5-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.5-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cypher_cell-0.1.5-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3df571b8a27f693c9702cbac52e70d047034a29b811a7dbf7d661815887efc9e
MD5 324f0d9480cc512656c40ec95306e35a
BLAKE2b-256 06d24f06f2a3717f1f8e007b0713675567257480c4ee40f8362511fc63445431

See more details on using hashes here.

Provenance

The following attestation bundles were made for cypher_cell-0.1.5-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.5-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for cypher_cell-0.1.5-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 3fe82396c2b702e7ba99525e97259ddb9e3dc1440c6978bfc4e098e8677c03b3
MD5 385200e939f4297924b7fdb1cd8a23c7
BLAKE2b-256 62d09d9bdacd5612c99865ae8872859ac60357dcd51f6fbba89883de5e77832a

See more details on using hashes here.

Provenance

The following attestation bundles were made for cypher_cell-0.1.5-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.5-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.5-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 1f1476b41378979c0f39032ef7e6fda1e24a9ee8860d89b3e8e362db609c9c9d
MD5 6ec4c8a6700be7ac17b3e18135f2de2b
BLAKE2b-256 4ca3e0c91064bbb80adc6dfcebcfd0360e84ddacfc417ca4786d1f219feb2de1

See more details on using hashes here.

Provenance

The following attestation bundles were made for cypher_cell-0.1.5-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