lib-q-random: Secure Random Number Generation for libQ
A comprehensive, secure random number generation system designed specifically for post-quantum cryptography applications in the libQ ecosystem.
Features
- Cryptographically Secure: Uses OS entropy sources and hardware RNGs when available
- Multiple Providers: Support for OS, deterministic, and hardware entropy sources
- Entropy Validation: Comprehensive entropy quality assessment and validation
- no_std Support: Works in constrained environments without standard library
- WASM Compatible: Full support for WebAssembly and browser environments
- Zero-Copy: Efficient memory usage with minimal allocations
- Thread-Safe: Safe for use in multi-threaded environments
- Extensible: Plugin architecture for custom entropy sources
- Custom Entropy Sources: Secure callback-based system for plugging in custom entropy sources in
no_stdand WASM environments
Quick Start
Add this to your Cargo.toml:
[]
= "0.0.4"
# For custom entropy sources in no_std/WASM environments
= { = "0.0.4", = ["custom-entropy"] }
Basic Usage
use ;
// Create a secure RNG for production use
let mut rng = new_secure_rng?;
// Generate random bytes
let mut bytes = ;
rng.fill_bytes;
// Create a deterministic RNG for testing
let mut test_rng = new_deterministic_rng;
test_rng.fill_bytes;
Advanced Usage
use ;
// Create a custom RNG with user-provided entropy
let entropy_data = vec!;
let entropy_source = new;
Custom Entropy Sources (no_std/WASM)
For no_std and WASM environments, you can plug in custom entropy sources:
Feature Flags
std: Enable standard library features (default)no_std: Enable no_std compatibilitycustom-entropy: Enable custom entropy source supportsecure: Enable cryptographically secure RNGsdeterministic: Enable deterministic RNGs for testingentropy-validation: Enable entropy quality validationzeroize: Enable automatic memory zeroizationwasm: Enable WebAssembly support
Security Considerations
- Entropy Quality: Always use high-quality entropy sources for cryptographic operations
- Custom Entropy: When using custom entropy sources, ensure they provide cryptographically secure randomness
- Memory Safety: Sensitive data is automatically zeroized when using the
zeroizefeature - Thread Safety: All RNG implementations are thread-safe and can be used in multi-threaded environments
Architecture
The crate is organized into several key components:
Core Traits
SecureRng: Enhanced random number generator trait for cryptographic applicationsEntropySource: Trait for entropy sources that provide random dataRngProvider: Trait for RNG providers that create and manage RNG instances
Providers
LibQRng: Main RNG implementation with unified interfaceOsEntropySource: Operating system entropy sourceHardwareEntropySource: RDRAND on x86 / x86_64 when thestdfeature is enabled and the CPU supports it; otherwise unavailable (useOsEntropySourceon other targets)DeterministicEntropySource: Deterministic source for testingUserEntropySource: User-provided entropy source
Validation
EntropyValidator: Comprehensive entropy validation and quality assessmentEntropyQuality: Entropy quality assessment resultquick_entropy_check: Quick entropy validation for small samples
Security Considerations
- All RNGs are cryptographically secure by default
- Entropy validation ensures sufficient randomness
- Secure memory clearing prevents key material leakage
- Constant-time operations prevent timing attacks
- Comprehensive error handling prevents fallback to weak randomness
Platform Support
Operating Systems
- Linux (using
/dev/urandom) - macOS (using
SecRandomCopyBytes) - Windows (using
CryptGenRandom) - FreeBSD, OpenBSD, NetBSD
- WebAssembly (using
crypto.getRandomValues())
Hardware RNGs
- Intel RDRAND (when available)
- ARM TRNG (when available)
- Platform-specific hardware RNGs
Feature Flags
std: Standard library support (default)alloc: Dynamic memory allocation supportsecure: Cryptographically secure RNGs (default)deterministic: Deterministic RNGs for testing (KT128 expansion; always available withlib-q-k12)hash: HPKEKt128Rngexporthpke: HPKE RNG integration (hash)deterministic-saturnin: Optional Saturnin CTR deterministic RNG (LibQRng::new_deterministic_saturnin)hardware: Hardware RNG supportzeroize: Secure memory clearing (default)entropy-validation: Entropy validation featureswasm: WebAssembly supportjs: JavaScript bindings
Examples
Basic Random Generation
use new_secure_rng;
let mut rng = new_secure_rng?;
let mut key = ;
rng.fill_bytes;
Deterministic Testing
use new_deterministic_rng;
let mut seed = ;
seed.copy_from_slice;
let mut rng = new_deterministic_rng;
let mut bytes = ;
rng.fill_bytes;
// bytes will be the same every time with the same seed (KT128 / libQ-DET-RNG-v1)
Migration (0.0.3 → 0.0.4)
Deterministic APIs are unchanged by name, but output bytes differ: ChaCha20 was replaced with KT128 (KangarooTwelve) and domain-separated expansion. new_deterministic_from_u64 now uses SplitMix64 to form a 32-byte seed. Re-record any test vectors that pinned ChaCha20 output. See CHANGELOG.md.
Custom Entropy Source
use ;
let entropy_data = vec!;
let entropy_source = new;
let mut rng = new_custom;
Entropy Validation
use ;
let validator = new;
let data = ;
// Quick check
if quick_entropy_check
// Full validation
match validator.validate_entropy
Performance
The crate is designed for high performance with minimal overhead:
- Zero-copy operations where possible
- Efficient entropy source selection
- Optimized validation algorithms
- Minimal memory allocations
Benchmarks are available in the benches/ directory.
Testing
The crate includes comprehensive tests:
- Unit tests for all components
- Integration tests for end-to-end functionality
- Property-based tests for statistical properties
- Performance benchmarks
Run tests with:
License
This project is licensed under the Apache License, Version 2.0. See the LICENSE file for details.
Security
For security-related issues, please see our Security Policy.
Changelog
See CHANGELOG.md for a list of changes.
Related Projects
- libQ - Post-quantum cryptography library
- lib-q-core - Core types and traits
- lib-q-kem - Key encapsulation mechanisms