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.6.tar.gz (18.2 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.6-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.6-cp310-abi3-win_amd64.whl (119.5 kB view details)

Uploaded CPython 3.10+Windows x86-64

cypher_cell-0.1.6-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.6-cp310-abi3-musllinux_1_1_aarch64.whl (216.1 kB view details)

Uploaded CPython 3.10+musllinux: musl 1.1+ ARM64

cypher_cell-0.1.6-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.6-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (217.2 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

cypher_cell-0.1.6-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.6.tar.gz.

File metadata

  • Download URL: cypher_cell-0.1.6.tar.gz
  • Upload date:
  • Size: 18.2 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.6.tar.gz
Algorithm Hash digest
SHA256 94e85992100322f4a676fbb7a36733b7797f2b2caa598a017d27032fe17e83c1
MD5 7de63a066fe9ccd595021656dade0484
BLAKE2b-256 6772d9a55b9e5b6ebaf7082cc248ba7fb931d596052167ea7d6b15a398147954

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for cypher_cell-0.1.6-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a04b68038dccac9c04ebf1574d9838d5a0f63b7308f5ff5bf733b388aa81475f
MD5 f6d166fc7f8af040e243aeea12ed71fc
BLAKE2b-256 915cb93e86f45ddf6ef9136d2faccd470646cf0ad4c5e18fe6195dc27b70c840

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: cypher_cell-0.1.6-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 119.5 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.6-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 629b0dcd3cc7000575f6e1de2b361bb2446566e2b35ebdceeda95cafba2e9976
MD5 106ba788669c794aa6161261546b474a
BLAKE2b-256 cf899125650a95a2698846d7e561ef8435737dc5141981b7d7e6e91d47a146db

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for cypher_cell-0.1.6-cp310-abi3-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 88b8e9bc34c9c33c7bcf348340915ed828d7b0d73c7e5e3e59395f24dc2c69d6
MD5 858e773fd4b325eb8349f56120e0fa97
BLAKE2b-256 b5ca6acdab03626883747960f73e04b6ec266b81ea2ecc0bbab264bafb9f4889

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for cypher_cell-0.1.6-cp310-abi3-musllinux_1_1_aarch64.whl
Algorithm Hash digest
SHA256 4a9d814fc8b063d45cb4237b36ee6a69b92295ad3f495caacd33a3cdd97d342e
MD5 e0895e046d6e1efa76bbcf8cbf321e83
BLAKE2b-256 0aaf1bd4ed4f159599be424521d0b3f968ccd3bc71cbc79dec1d215656192ddb

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for cypher_cell-0.1.6-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 aae83092ba5e342d21a1e2d374a1e35a7be2a968d124598fd9593f334202cb2b
MD5 5ea1ae5eac9ec5709190ceaf0eb461ce
BLAKE2b-256 9078e4f4e3ed507d59d650cb7655f2d77675939a4b58b1b1b5d3fe47fb9c3a4f

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for cypher_cell-0.1.6-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 6db760ccdcab4fa02563dd1cbb3ae0f8130b5690b8bb99e6860749206d1ffa28
MD5 797a3574cbf1d6d89bb4314b50c901d7
BLAKE2b-256 d0d2a857dca74153ed861d34a7df6b1aa23e30ca42b1c570caaeeccbe7752505

See more details on using hashes here.

Provenance

The following attestation bundles were made for cypher_cell-0.1.6-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.6-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.6-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 674f4ec66bee35ab731adf472b63ac3feb1aa37fb5b0a83f1028d5e35b0813c8
MD5 78406e6630b86b9ebe46af0fca218711
BLAKE2b-256 a4361f71efe6bc3919a974916b2c68a0e84eee85202352a145d4b44b76c385d7

See more details on using hashes here.

Provenance

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