lib-q-core
Core types and traits for the lib-Q post-quantum cryptography library.
Features
- Algorithm Identifiers: Unified enum for all supported post-quantum algorithms
- Error Handling: Comprehensive error types with detailed information
- Key Types: Secure key structures with automatic memory zeroization
- WASM Support: Full WebAssembly compatibility with JavaScript bindings
- No-std Compatible: Works in embedded and constrained environments
No-std Support
This crate supports no_std environments through feature flags:
Feature Flags
std(default): Full standard library support with detailed error messagesalloc: Allocation support for dynamic memory (Vec, String)no_std: Minimal no_std support without allocationgetrandom: Cryptographically secure random number generationserde: Serialization supportwasm: WebAssembly bindings
Usage Examples
Full std support (default)
use ;
let mut kem_ctx = new;
let keypair = kem_ctx.generate_keypair?;
// Full random number generation
let random_bytes = random_bytes?;
// String formatting available
let hex = bytes_to_hex;
No-std with allocation
use ;
// In Cargo.toml: default-features = false, features = ["alloc"]
let mut kem_ctx = new;
let keypair = kem_ctx.generate_keypair?;
// Vec and String available, but no random generation
Minimal no-std
use ;
// In Cargo.toml: default-features = false, features = ["no_std"]
let mut kem_ctx = new;
let keypair = kem_ctx.generate_keypair?;
// No allocation, no random generation, static error messages
Error Handling in no_std
In no_std mode, error messages use static strings instead of dynamic allocation:
// std mode
InvalidAlgorithm
// no_std mode
InvalidAlgorithm
Random Number Generation
Random number generation requires the getrandom feature:
// With getrandom feature
let random_bytes = random_bytes?;
// Without getrandom feature
let random_bytes = random_bytes; // Returns error
Supported Algorithms
Key Encapsulation Mechanisms (KEMs)
- ML-KEM (FIPS 203): ML-KEM-512, ML-KEM-768, ML-KEM-1024
- CB-KEM: Multiple parameter sets
- HQC: HQC-128, HQC-192, HQC-256
Digital Signatures
- ML-DSA (FIPS 204): ML-DSA-44, ML-DSA-65, ML-DSA-87 (see
lib-q-ml-dsa,lib-q-ring) - FN-DSA (FIPS 206): FN-DSA-1, FN-DSA-5
- SLH-DSA (FIPS 205): SHA-2 and SHAKE parameter families (
lib-q-slh-dsa,lib-q-sig)
Hash Functions
- SHA-3 family: SHA3-224, SHA3-256, SHA3-384, SHA3-512
- SHAKE: SHAKE128, SHAKE256
- Customizable SHAKE: cSHAKE128, cSHAKE256
- KMAC: KMAC128, KMAC256
- TupleHash: TupleHash128, TupleHash256
- ParallelHash: ParallelHash128, ParallelHash256
Usage
Rust
use ;
let mut kem_ctx = new;
let keypair = kem_ctx.generate_keypair?;
WebAssembly
import init from 'lib-q-kem';
await ;
const algorithms = ;
const kem = ;
Security
All cryptographic operations are implemented following NIST standards and best practices:
- Constant-time operations where applicable
- Secure memory handling with automatic zeroization
- Side-channel resistance considerations
- Comprehensive input validation
Workspace
This crate is part of the lib-Q workspace. Related pieces include lib-q-kem, lib-q-sig, lib-q-hpke, lib-q-zkp, lib-q-ring, and tooling crates such as lib-q-sca-test.
AEAD decrypt layers (0.0.2+)
- Layer A (default):
lib_q_core::Aead::decryptandlib_q_core::api::AeadOperations::decryptreturnResultonly. Existing callers keep using this; behavior ofdecryptis unchanged for algorithms that only extended Layer B alongside it. - Layer B (opt-in):
lib_q_core::AeadDecryptSemantic::decrypt_semanticreturnslib_q_core::Result<DecryptSemanticOutcome>(i.e.Result<DecryptSemanticOutcome, lib_q_core::Error>); authentication failure isOk(DecryptSemanticOutcome::AuthenticationFailed)with no plaintext bytes in that arm. Operational errors (sizes, keys, nonces, unsupported AD) stayErr. Ciphertext shorter than the AEAD tag isErr(Error::InvalidCiphertextSize)(useError::aead_ciphertext_shorter_than_tagat AEAD boundaries for consistent construction). - Migration: import
AeadDecryptSemanticand match onDecryptSemanticOutcome; do not collapse the outcome to a secret-derivedboolwithout a reviewed threat model. Concrete types implement the trait where shipped:lib_q_saturnin::SaturninAead,lib_q_saturnin::SaturninShortAead,lib_q_aead::Shake256Aead,lib_q_duplex_aead::DuplexSpongeAead,lib_q_tweak_aead::TweakAead,lib_q_romulus::{RomulusNAead, RomulusMAead}, and the correspondinglib-q-aeadregistry wrappers (SaturninAead,DuplexSpongeAead, …). Fordyn AeadWithMetadataor metadata-driven dispatch, readAeadMetadata::supports_semantic_decrypt/AeadWithMetadata::supports_semantic_decryptbefore assuming Layer B. HPKE exposes semantic decrypt onSaturninAeadImplandShake256AeadImpl. - Layer A
Errvariant names after a failed integrity check are not unified across algorithms (AuthenticationFailedvsVerificationFailed); seedocs/adr/003-aead-decrypt-layers.mdfor the mapping table. Layer B usesAuthenticationFailedin theOkarm consistently. - Full specification:
docs/adr/003-aead-decrypt-layers.md. WASMAeadContextbindings remain Layer A unless extended deliberately.
License
Licensed under the Apache License, Version 2.0.