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.3.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.3-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.3-cp310-abi3-win_amd64.whl (120.2 kB view details)

Uploaded CPython 3.10+Windows x86-64

cypher_cell-0.1.3-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.3-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.3-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.3-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.3-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (392.0 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.3.tar.gz.

File metadata

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

File hashes

Hashes for cypher_cell-0.1.3.tar.gz
Algorithm Hash digest
SHA256 5531dab25ad676db2b5a8d456354247291f564479b52abb6be3da9f47199ab93
MD5 9f07d4d72e50fe817b0bc3cc05d88d03
BLAKE2b-256 a7b57e294c816041ed1e5f3a97877de6d945f0576ef0c97201977f6a7c32dd55

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cypher_cell-0.1.3-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2e199f1f207c088c4593329238c4512d05972ad5236dfd7607666e7edea717f9
MD5 b528ab47be51bd9343f10a342e778778
BLAKE2b-256 35a5c10aa197b47c72fc41a328824fc13cb1a8145b0d70c58f47496dbe2b3249

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for cypher_cell-0.1.3-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 965aa426a917022d940623db4c4407ad7fa84b9ff7b105629ecbf8211c63a0e2
MD5 acd5798a5f2d096d7402bdb2d41b2947
BLAKE2b-256 2ce7df24bcbce6348a2e8fd0468ef7c1f55e550cccfa89c547575b5eaeae32da

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cypher_cell-0.1.3-cp310-abi3-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 098ecd7ea0c5541b2bc5ba913ee838e190e594d12eaf4e76e3c9163168b02ae3
MD5 98b66d0210f08b428e05373393d7c97e
BLAKE2b-256 14664fef9b0317deb947b0c4f7d25654155dcdcda22a104654804a988a33d610

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cypher_cell-0.1.3-cp310-abi3-musllinux_1_1_aarch64.whl
Algorithm Hash digest
SHA256 e2dc2585daa52676b77eef6f738e9ee3a84d704b90bcaba8021d2c8473ec7246
MD5 e83ccf9434ffa421f6273d012bc19bcb
BLAKE2b-256 245bf40b71fbd0edd29c91fca4f75402bae10c3091aecdd2082a99746e62980b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cypher_cell-0.1.3-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6514999c625d95714c3b326b3a5197f01a3f4326a212a3fb41533e849d6e44a7
MD5 04dfcecce2202523c7ba424b63eee496
BLAKE2b-256 4e85ec5b3fb257ac41a91c5d7e9ab7843cd558a99589940a5d7c875bdcbba6f5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cypher_cell-0.1.3-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 1760c70fa9af087e2d3bcee5256e6fe305b5e682dcf96b5d8e45102f3fba11d2
MD5 51313f255fa3e1313c89ae896a93ce6b
BLAKE2b-256 7924c2b73f06f13cf2ee8722dcf217d4bd2b09ac830ab1114b14a029beb0cb78

See more details on using hashes here.

File details

Details for the file cypher_cell-0.1.3-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.3-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 e2060f004ecf0b3ddc01ee885016aad9ddef256d5abebe8b92c4be0e0e814af3
MD5 a3a27b964e5cf209ab1ae7805cec1745
BLAKE2b-256 c30dd8d3f6587189ad135b83e2ab37331c33fd1c2a7e6c1d98b9c135f658912e

See more details on using hashes here.

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