Backends (quantum_safe.backends)¶
Cryptographic backend adapters. You rarely need to interact with these
directly — use get_kem_backend() and
get_signature_backend() only when you need
to force a specific backend.
- quantum_safe.backends.get_kem_backend(name='auto')[source]¶
Return the KEM backend for the given name.
- Parameters:
name (
str) – “auto”, “liboqs”, or “rustcrypto”. Falls back through the priority list if “auto”.- Raises:
BackendNotAvailable – if the requested backend is not installed.
ValueError – if the backend name is unknown.
- Return type:
- quantum_safe.backends.get_signature_backend(name='auto')[source]¶
Return the signature backend for the given name.
- Parameters:
name (
str)- Return type:
- quantum_safe.backends.list_available_backends()[source]¶
Return availability status for all known backends.
Useful for diagnostics: python -c “from quantum_safe.backends import list_available_backends; print(list_available_backends())”.
Abstract base classes¶
- class quantum_safe.backends.base.AbstractKEMBackend[source]¶
Bases:
ABCInterface for KEM (Key Encapsulation Mechanism) backends.
Implementations wrap a specific cryptographic library (liboqs, RustCrypto, etc.) and expose a uniform interface for keygen, encap, and decap.
All methods are synchronous. If you need async, call from a thread pool.
- abstractmethod supported_algorithms()[source]¶
Return metadata for all KEM algorithms this backend supports.
- Return type:
list[AlgorithmInfo]
- abstractmethod keygen(algorithm)[source]¶
Generate a key pair.
- Parameters:
algorithm (
str) – Algorithm name, e.g. “ML-KEM-768”- Return type:
- Returns:
(public_key_bytes, secret_key_bytes)
- Raises:
UnsupportedAlgorithm – if the algorithm is not supported
KeyGenerationError – if keygen fails (RNG failure, etc.)
- abstractmethod encapsulate(algorithm, public_key)[source]¶
Encapsulate: generate a ciphertext and shared secret.
- Parameters:
- Return type:
- Returns:
(ciphertext_bytes, shared_secret_bytes)
- Raises:
UnsupportedAlgorithm – if the algorithm is not supported
CryptoError – if encapsulation fails
- abstractmethod decapsulate(algorithm, secret_key, ciphertext)[source]¶
Decapsulate: recover the shared secret from a ciphertext.
- Parameters:
- Return type:
- Returns:
shared_secret_bytes (32 bytes for all standardized algorithms)
- Raises:
DecapsulationError – if decapsulation fails — NEVER reveal why
UnsupportedAlgorithm – if the algorithm is not supported
- class quantum_safe.backends.base.AbstractSignatureBackend[source]¶
Bases:
ABCInterface for digital signature backends.
- abstractmethod supported_algorithms()[source]¶
Return metadata for all signature algorithms this backend supports.
- Return type:
list[AlgorithmInfo]
- abstractmethod sign(algorithm, secret_key, message, context=b'')[source]¶
Sign a message.
- Parameters:
- Return type:
- Returns:
signature_bytes
- Raises:
UnsupportedAlgorithm – if the algorithm is not supported
CryptoError – if signing fails
- abstractmethod verify(algorithm, public_key, message, signature, context=b'')[source]¶
Verify a signature.
- Parameters:
- Return type:
- Returns:
True if valid, False if invalid.
Note: This returns bool rather than raising on invalid signatures. The caller (Sign.verify()) is responsible for raising VerificationError. Backends should NOT raise on invalid signatures — return False.
liboqs backend¶
- class quantum_safe.backends.liboqs.LiboqsKEMBackend[source]¶
Bases:
AbstractKEMBackendKEM backend using liboqs-python.
- supported_algorithms()[source]¶
Return algorithms supported by this backend.
We only return algorithms that are actually enabled in the installed liboqs build — some builds omit BIKE or HQC for size/license reasons.
- Return type:
list[AlgorithmInfo]
- keygen(algorithm)[source]¶
Generate an ML-KEM (or other KEM) key pair.
Returns (public_key_bytes, secret_key_bytes).
- encapsulate(algorithm, public_key)[source]¶
Encapsulate a shared secret under the given public key.
Returns (ciphertext_bytes, shared_secret_bytes).
- decapsulate(algorithm, secret_key, ciphertext)[source]¶
Decapsulate a shared secret from a ciphertext.
Returns shared_secret_bytes.
IMPORTANT: liboqs ML-KEM implements implicit rejection (per FIPS 203 §6.3) — a malformed ciphertext returns a pseudorandom value rather than raising an exception. We check the output length to detect the most obvious failure modes, but we cannot distinguish a bad ciphertext from a good one by inspecting the output.
- class quantum_safe.backends.liboqs.LiboqsSignatureBackend[source]¶
Bases:
AbstractSignatureBackendSignature backend using liboqs-python.
- supported_algorithms()[source]¶
Return metadata for all signature algorithms this backend supports.
- Return type:
list[AlgorithmInfo]
- sign(algorithm, secret_key, message, context=b'')[source]¶
Sign a message.
FIPS 204 §5.2 specifies that the context is prepended to the message before hashing. liboqs-python’s Signature.sign() doesn’t directly expose the context parameter in older versions, so we prepend it manually using the format:
context_len (1 byte) || context || message
This is consistent with the HashML-DSA construction in FIPS 204 §5.4. When liboqs exposes context natively (v0.11+), we’ll use that instead.