Skip to main content

High-level post-quantum hybrid encryption (ML-KEM/Kyber768 + AES-256-GCM) with a Fernet-like API

Project description

QuantumGuard

CI Coverage

High-level post-quantum hybrid encryption library for Python. Uses ML-KEM (Kyber768, NIST FIPS 203) for key encapsulation and AES-256-GCM for symmetric encryption, with a simple API similar to cryptography.fernet.

Requirements

  • Compatibility: Python 3.9+
  • pypqc (PQClean bindings, Kyber768)
  • cryptography (AES-GCM)

Installation

pip install quantumguard

Quick Start

from quantumguard import QuantumGuard

# Generate key pair
public_key, private_key = QuantumGuard.generate_keypair()

# Encrypt with public key
qg = QuantumGuard(public_key)
ciphertext = qg.encrypt("Post-quantum secret message")

# Decrypt with private key
qg_private = QuantumGuard(private_key)
decrypted_text = qg_private.decrypt(ciphertext)  # bytes
print(decrypted_text.decode("utf-8"))

Key Persistence (PEM / Base64)

Keys can be exported and imported in PEM or Base64 for storage in .key files:

# Export to PEM
pem_private = QuantumGuard.key_to_pem(private_key, "private")
pem_public = QuantumGuard.key_to_pem(public_key, "public")

# Save to file
with open("private.key", "w") as f:
    f.write(pem_private)

# Load from PEM
with open("private.key") as f:
    key_bytes, kind = QuantumGuard.key_from_pem(f.read())
qg = QuantumGuard(key_bytes)  # or pass the PEM string directly: QuantumGuard(f.read())

Base64 with optional type prefix (qg.pub. / qg.priv.) is also supported via key_to_base64 and key_from_base64.

Serialization Format

  • Binary (default): Magic + version, KEM ciphertext length, KEM ciphertext, 12-byte nonce, 16-byte tag, AES ciphertext. Compact and deterministic.
  • JSON: Optional output_format="json" in encrypt() for debugging or interoperability. Payload is UTF-8 JSON with base64-encoded fields (v, k, n, t, c).

Decryption accepts both formats automatically.

API Summary

  • QuantumGuard(key) — Initialize with public or private key (bytes, PEM, or Base64).
  • generate_keypair() — Returns (public_key, private_key) as bytes.
  • encrypt(plaintext, output_format="binary"|"json") — Encrypt; returns serialized bytes.
  • decrypt(ciphertext) — Decrypt; returns plaintext bytes.
  • cleanup() — Wipe key material; do not use the instance afterward.
  • key_to_pem(key, kind), key_from_pem(pem) — PEM export/import.
  • key_to_base64(key, kind), key_from_base64(b64) — Base64 export/import.
  • secure_wipe(buffer) — Zero a bytearray (best-effort).
  • SecureBytes(length) — Context manager or .burn() for short-lived secrets.
  • __version__ (package version string), NONCE_BYTES, TAG_BYTES, MAX_KEM_CT_BYTES (max KEM ciphertext length), VERSION (serialization format version, integer), RECOMMENDED_MAX_PLAINTEXT_BYTES (recommended max plaintext size for apps to enforce; library does not enforce it) — constants for integrations.

See docstrings for full details. For a generated API Reference, build the Sphinx docs (see Contributing); output is in docs/build/html/.

Security Notes

  • Message size: The library does not impose a maximum plaintext length. Applications should enforce a maximum plaintext size per message (e.g. 1 MiB; see RECOMMENDED_MAX_PLAINTEXT_BYTES) to mitigate DoS from excessively large payloads. Practical limits are also system memory and the AES-GCM specification; use streaming or chunking for very large data if needed.
  • AAD (Additional Authenticated Data): AAD is not used in this API. For binding ciphertext to context (e.g. session id, nonce), applications must enforce that at a higher layer.
  • Memory: Sensitive key material is held in a bytearray and can be wiped with qg.cleanup(). Do not use the instance for encrypt() or decrypt() after calling cleanup(); those methods will raise InvalidKeyError. Python does not guarantee that copies do not exist (e.g. interpreter or C extensions). For custom buffers use secure_wipe(); for short-lived secrets consider SecureBytes (context manager or .burn()).
  • Dependencies: Requires pypqc >= 0.0.6.1 (KyberSlash fix). Do not use older versions.
  • Algorithms: ML-KEM-768 (FIPS 203) for KEM; AES-256-GCM for encryption.

References

License

MIT

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

quantumguard-0.1.1.tar.gz (20.4 kB view details)

Uploaded Source

Built Distribution

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

quantumguard-0.1.1-py3-none-any.whl (15.3 kB view details)

Uploaded Python 3

File details

Details for the file quantumguard-0.1.1.tar.gz.

File metadata

  • Download URL: quantumguard-0.1.1.tar.gz
  • Upload date:
  • Size: 20.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.2

File hashes

Hashes for quantumguard-0.1.1.tar.gz
Algorithm Hash digest
SHA256 830e28393d78c510a7413354769f79ee26a7861d971017bdc529c02ed40ba44b
MD5 c0573f8af193a79288ada88f8bd54f68
BLAKE2b-256 10eba78b1e93755214a21c903fdd8297b8aff1119eceef2128191d6144ca7bd2

See more details on using hashes here.

File details

Details for the file quantumguard-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: quantumguard-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 15.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.2

File hashes

Hashes for quantumguard-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 50d2b779bea1940b59ace076961f20d693cfa52084940fe3bb53eb4c51a2eb57
MD5 8b6e3797252cdff327a9e77b75678157
BLAKE2b-256 581284c816e7efe598b4f52dcc3051afefe27f7bffead1515d2382130dfc7671

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