๐ pq-jwt
Post-Quantum JWT - A quantum-resistant JWT implementation using ML-DSA (Module-Lattice Digital Signature Algorithm) signatures.
๐ก๏ธ Future-proof your authentication - Protect your JWTs against quantum computer attacks with NIST-standardized post-quantum cryptography.
๐ Features
- โ Quantum-Resistant - Uses ML-DSA (FIPS 204) signatures that remain secure even against quantum attacks
- โ Multiple Security Levels - Choose from ML-DSA-44, ML-DSA-65, or ML-DSA-87 based on your needs
- โ Standards Compliant - JWT format following RFC 7519
- โ Zero Dependencies Bloat - Minimal, focused dependencies
- โ Easy to Use - Simple, intuitive API
- โ Well Tested - Comprehensive test coverage with unit and integration tests
- โ Pure Rust - Memory-safe implementation with no unsafe code
๐ฆ Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
๐ Quick Start
use ;
๐ Usage Examples
Basic Authentication Token
use ;
// Generate long-term keypair (store securely!)
let = generate_keypair?;
// Create user session token
let user_claims = r#"{
"sub": "user123",
"name": "Alice",
"role": "admin",
"iat": 1704067200,
"exp": 1704153600
}"#;
// Sign the token
let = sign?;
// Later: verify the token
let payload = verify?;
println!;
API Authentication
use ;
// Server initialization
let =
generate_keypair?;
// Issue API token
let api_claims = r#"{
"api_key": "ak_live_123456",
"scope": ["read", "write"],
"rate_limit": 1000
}"#;
let = sign?;
// Client sends: Authorization: Bearer <api_token>
// Server verifies:
match verify
Custom Payload
use ;
use ;
let claims = CustomClaims ;
let payload_json = to_string?;
let = sign?;
// Later...
let verified = verify?;
let decoded: CustomClaims = from_str?;
๐ Security Levels
Choose the right security level for your use case:
| Variant | NIST Level | Signature Size | Key Gen | Sign | Verify | Use Case |
|---|---|---|---|---|---|---|
| ML-DSA-44 | Category 2 | ~2.4 KB | ~200 ยตs | ~460 ยตs | ~140 ยตs | IoT devices, low-power systems |
| ML-DSA-65 | Category 3 | ~3.3 KB | ~350 ยตs | ~930 ยตs | ~220 ยตs | Recommended for most applications |
| ML-DSA-87 | Category 5 | ~4.6 KB | ~440 ยตs | ~550 ยตs | ~315 ยตs | High-security requirements, long-term secrets |
Security Level Comparison
- NIST Category 2 โ AES-128 security
- NIST Category 3 โ AES-192 security (Recommended)
- NIST Category 5 โ AES-256 security
Choosing an Algorithm
use MlDsaAlgo;
// For most web applications (recommended)
let algo = Dsa65;
// For IoT or bandwidth-constrained environments
let algo = Dsa44;
// For maximum security (government, financial)
let algo = Dsa87;
๐ฏ Performance
Benchmarked on Apple M1 Pro (release build):
ML-DSA-65 Performance:
โโ Key Generation: ~350 ยตs (2,857 ops/sec)
โโ Signing: ~930 ยตs (1,075 ops/sec)
โโ Verification: ~220 ยตs (4,545 ops/sec)
Token Size: ~4.5 KB (vs ~300 bytes for ECDSA)
Performance Tips
- Cache Keys: Generate keypairs once and reuse them
- Pre-verify Format: Check JWT structure before cryptographic verification
- Use ML-DSA-44: If bandwidth is critical and security level 2 is acceptable
- Batch Operations: Verify multiple tokens in parallel for better throughput
๐ Size Comparison
| Algorithm | Private Key | Public Key | Signature | Total JWT |
|---|---|---|---|---|
| ECDSA P-256 | 32 bytes | 64 bytes | 64 bytes | ~300 bytes |
| RSA-2048 | 1.2 KB | 270 bytes | 256 bytes | ~800 bytes |
| ML-DSA-44 | 2.5 KB | 1.3 KB | 2.4 KB | ~3.3 KB |
| ML-DSA-65 | 4 KB | 1.9 KB | 3.3 KB | ~4.5 KB |
| ML-DSA-87 | 4.9 KB | 2.6 KB | 4.6 KB | ~6.2 KB |
โ ๏ธ Trade-off: Post-quantum signatures are larger, but provide quantum resistance. The size increase is the price of security against quantum attacks.
๐ ๏ธ API Reference
Functions
generate_keypair(algo: MlDsaAlgo) -> Result<(String, String), String>
Generates a new keypair for the specified algorithm.
Returns: (private_key_hex, public_key_hex)
let = generate_keypair?;
sign(algo: MlDsaAlgo, payload: &str, private_key_hex: &str) -> Result<(String, String), String>
Signs a payload and returns a JWT.
Returns: (jwt, public_key_hex)
let = sign?;
verify(jwt: &str, public_key_hex: &str) -> Result<String, String>
Verifies a JWT and returns the decoded payload.
Returns: payload if valid, error otherwise
let payload = verify?;
Enums
MlDsaAlgo
Available algorithm variants:
MlDsaAlgo::Dsa44- NIST Category 2MlDsaAlgo::Dsa65- NIST Category 3 (Recommended)MlDsaAlgo::Dsa87- NIST Category 5
๐ Security Considerations
Key Management
- Never commit private keys to version control
- Rotate keys regularly (every 90 days recommended)
- Use environment variables or secret management systems
- Store keys encrypted at rest
// โ Good
let private_key = var?;
// โ Bad
let private_key = "4343e9e24838dbd8..."; // hardcoded
Token Best Practices
- Always include expiration (
expclaim) - Use short lifetimes for sensitive operations (15 min - 1 hour)
- Implement token revocation if needed
- Validate claims after verification
- Use HTTPS for token transmission
Example with Expiration
use ;
let now = now.duration_since?.as_secs;
let exp = now + 3600; // 1 hour from now
let payload = format!;
let = sign?;
๐ค Why Post-Quantum?
The Quantum Threat
Quantum computers, when fully developed, will break current cryptographic systems:
- RSA - Vulnerable to Shor's algorithm
- ECDSA - Vulnerable to Shor's algorithm
- Diffie-Hellman - Vulnerable to quantum attacks
Timeline
- 2023: NIST standardizes post-quantum algorithms (ML-DSA = FIPS 204)
- 2025-2030: Quantum computers may break RSA-2048
- 2030+: All systems must use post-quantum crypto
"Harvest Now, Decrypt Later"
Attackers can:
- Intercept and store encrypted data today
- Wait for quantum computers to become available
- Decrypt the data retroactively
Solution: Start using post-quantum crypto NOW to protect long-term secrets.
๐ Comparison with Classical JWT
| Feature | pq-jwt (ML-DSA) | Classical (ECDSA) |
|---|---|---|
| Quantum Resistant | โ Yes | โ No |
| NIST Standardized | โ FIPS 204 | โ FIPS 186 |
| Token Size | 3-6 KB | ~300 bytes |
| Sign Speed | ~0.5-1 ms | ~0.05-0.1 ms |
| Verify Speed | ~0.2-0.3 ms | ~0.1-0.2 ms |
| Security Level | 128-256 bit | 128-256 bit |
| Future Proof | โ Yes | โ Vulnerable to quantum |
๐ง Integration Examples
With Actix Web
use ;
use ;
async
With Axum
use ;
use verify;
async
๐งช Testing
Run the test suite:
# Run all tests
# Run with output
# Run specific test
# Run benchmarks
๐ Further Reading
- NIST FIPS 204 - ML-DSA Standard
- Post-Quantum Cryptography FAQ
- JWT RFC 7519
- NIST Post-Quantum Standards
๐ค Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Development Setup
๐ License
This project is dual-licensed under:
- MIT License (LICENSE-MIT or http://opensource.org/licenses/MIT)
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
You may choose either license for your use.
๐จโ๐ป Author
MKSingh (@MKSingh_Dev)
โญ Star History
If you find this project useful, please consider giving it a star! โญ
Made with โค๏ธ for a quantum-safe future