⚠️ VeridianOS Kernel Documentation - This is low-level kernel code. All functions are unsafe unless explicitly marked otherwise. no_std

veridian_kernel/crypto/
pq_params.rs

1//! NIST Post-Quantum Cryptography Parameter Sets
2//!
3//! Official parameter sets from NIST FIPS 203 (ML-KEM) and FIPS 204 (ML-DSA).
4//!
5//! ## References
6#![allow(dead_code)]
7//! - FIPS 203: Module-Lattice-Based Key-Encapsulation Mechanism Standard
8//! - FIPS 204: Module-Lattice-Based Digital Signature Standard
9//! - NIST PQC Standardization Process: <https://csrc.nist.gov/projects/post-quantum-cryptography>
10
11/// ML-DSA (Dilithium) Parameter Sets
12pub mod dilithium {
13    /// ML-DSA-44 (Security Level 2 - equivalent to AES-128)
14    pub mod level2 {
15        pub const PUBLIC_KEY_SIZE: usize = 1312;
16        pub const SECRET_KEY_SIZE: usize = 2528;
17        pub const SIGNATURE_SIZE: usize = 2420;
18
19        // Lattice parameters
20        pub const Q: u32 = 8380417; // Prime modulus
21        pub const D: u32 = 13; // Dropped bits from t
22        pub const TAU: usize = 39; // Number of ±1's in c
23        pub const CHALLENGE_ENTROPY: usize = 192; // Bits of entropy in c
24        pub const GAMMA1: u32 = 1 << 17; // y coefficient range
25        pub const GAMMA2: u32 = (Q - 1) / 88; // Low-order rounding range
26        pub const K: usize = 4; // Rows in A
27        pub const L: usize = 4; // Columns in A
28        pub const ETA: u32 = 2; // Secret key range
29        pub const BETA: u32 = TAU as u32 * ETA; // Max coefficient of ct0
30        pub const OMEGA: usize = 80; // Max number of 1s in hint
31    }
32
33    /// ML-DSA-65 (Security Level 3 - equivalent to AES-192)
34    pub mod level3 {
35        pub const PUBLIC_KEY_SIZE: usize = 1952;
36        pub const SECRET_KEY_SIZE: usize = 4000;
37        pub const SIGNATURE_SIZE: usize = 3293;
38
39        pub const Q: u32 = 8380417;
40        pub const D: u32 = 13;
41        pub const TAU: usize = 49;
42        pub const CHALLENGE_ENTROPY: usize = 225;
43        pub const GAMMA1: u32 = 1 << 19;
44        pub const GAMMA2: u32 = (Q - 1) / 32;
45        pub const K: usize = 6;
46        pub const L: usize = 5;
47        pub const ETA: u32 = 4;
48        pub const BETA: u32 = TAU as u32 * ETA;
49        pub const OMEGA: usize = 55;
50    }
51
52    /// ML-DSA-87 (Security Level 5 - equivalent to AES-256)
53    pub mod level5 {
54        pub const PUBLIC_KEY_SIZE: usize = 2592;
55        pub const SECRET_KEY_SIZE: usize = 4864;
56        pub const SIGNATURE_SIZE: usize = 4595;
57
58        pub const Q: u32 = 8380417;
59        pub const D: u32 = 13;
60        pub const TAU: usize = 60;
61        pub const CHALLENGE_ENTROPY: usize = 257;
62        pub const GAMMA1: u32 = 1 << 19;
63        pub const GAMMA2: u32 = (Q - 1) / 32;
64        pub const K: usize = 8;
65        pub const L: usize = 7;
66        pub const ETA: u32 = 2;
67        pub const BETA: u32 = TAU as u32 * ETA;
68        pub const OMEGA: usize = 75;
69    }
70}
71
72/// ML-KEM (Kyber) Parameter Sets
73pub mod kyber {
74    /// ML-KEM-512 (Security Level 1 - equivalent to AES-128)
75    pub mod kyber512 {
76        pub const PUBLIC_KEY_SIZE: usize = 800;
77        pub const SECRET_KEY_SIZE: usize = 1632;
78        pub const CIPHERTEXT_SIZE: usize = 768;
79        pub const SHARED_SECRET_SIZE: usize = 32;
80
81        // Lattice parameters
82        pub const Q: u32 = 3329; // Prime modulus
83        pub const N: usize = 256; // Polynomial degree
84        pub const K: usize = 2; // Module rank
85        pub const ETA1: u32 = 3; // Secret key noise
86        pub const ETA2: u32 = 2; // Encryption noise
87        pub const DU: usize = 10; // Ciphertext compression
88        pub const DV: usize = 4; // Ciphertext compression
89    }
90
91    /// ML-KEM-768 (Security Level 3 - equivalent to AES-192)
92    pub mod kyber768 {
93        pub const PUBLIC_KEY_SIZE: usize = 1184;
94        pub const SECRET_KEY_SIZE: usize = 2400;
95        pub const CIPHERTEXT_SIZE: usize = 1088;
96        pub const SHARED_SECRET_SIZE: usize = 32;
97
98        pub const Q: u32 = 3329;
99        pub const N: usize = 256;
100        pub const K: usize = 3;
101        pub const ETA1: u32 = 2;
102        pub const ETA2: u32 = 2;
103        pub const DU: usize = 10;
104        pub const DV: usize = 4;
105    }
106
107    /// ML-KEM-1024 (Security Level 5 - equivalent to AES-256)
108    pub mod kyber1024 {
109        pub const PUBLIC_KEY_SIZE: usize = 1568;
110        pub const SECRET_KEY_SIZE: usize = 3168;
111        pub const CIPHERTEXT_SIZE: usize = 1568;
112        pub const SHARED_SECRET_SIZE: usize = 32;
113
114        pub const Q: u32 = 3329;
115        pub const N: usize = 256;
116        pub const K: usize = 4;
117        pub const ETA1: u32 = 2;
118        pub const ETA2: u32 = 2;
119        pub const DU: usize = 11;
120        pub const DV: usize = 5;
121    }
122}
123
124/// Security level mapping
125#[derive(Debug, Clone, Copy, PartialEq, Eq)]
126pub(crate) enum SecurityLevel {
127    /// NIST Level 1 (equivalent to AES-128, ~128 bits quantum security)
128    Level1,
129    /// NIST Level 2 (equivalent to SHA-256 collision resistance, ~128 bits)
130    Level2,
131    /// NIST Level 3 (equivalent to AES-192, ~192 bits quantum security)
132    Level3,
133    /// NIST Level 4 (equivalent to SHA-384 collision resistance, ~192 bits)
134    Level4,
135    /// NIST Level 5 (equivalent to AES-256, ~256 bits quantum security)
136    Level5,
137}
138
139impl SecurityLevel {
140    /// Get classical security bits
141    pub(crate) fn classical_bits(&self) -> u32 {
142        match self {
143            SecurityLevel::Level1 | SecurityLevel::Level2 => 128,
144            SecurityLevel::Level3 | SecurityLevel::Level4 => 192,
145            SecurityLevel::Level5 => 256,
146        }
147    }
148
149    /// Get quantum security bits
150    pub(crate) fn quantum_bits(&self) -> u32 {
151        match self {
152            SecurityLevel::Level1 | SecurityLevel::Level2 => 128,
153            SecurityLevel::Level3 | SecurityLevel::Level4 => 192,
154            SecurityLevel::Level5 => 256,
155        }
156    }
157
158    /// Get recommended use cases
159    pub(crate) fn use_cases(&self) -> &'static str {
160        match self {
161            SecurityLevel::Level1 | SecurityLevel::Level2 => {
162                "Standard security applications, IoT devices, embedded systems"
163            }
164            SecurityLevel::Level3 | SecurityLevel::Level4 => {
165                "High-value data, long-term confidentiality, government applications"
166            }
167            SecurityLevel::Level5 => {
168                "Top Secret information, critical infrastructure, military use"
169            }
170        }
171    }
172}
173
174/// Recommended parameter selections
175pub mod recommendations {
176    use super::SecurityLevel;
177
178    /// Get recommended Dilithium level for security requirement
179    pub(crate) fn dilithium_level(required_security: SecurityLevel) -> &'static str {
180        match required_security {
181            SecurityLevel::Level1 | SecurityLevel::Level2 => "ML-DSA-44 (Level 2)",
182            SecurityLevel::Level3 | SecurityLevel::Level4 => "ML-DSA-65 (Level 3)",
183            SecurityLevel::Level5 => "ML-DSA-87 (Level 5)",
184        }
185    }
186
187    /// Get recommended Kyber level for security requirement
188    pub(crate) fn kyber_level(required_security: SecurityLevel) -> &'static str {
189        match required_security {
190            SecurityLevel::Level1 | SecurityLevel::Level2 => "ML-KEM-512",
191            SecurityLevel::Level3 | SecurityLevel::Level4 => "ML-KEM-768",
192            SecurityLevel::Level5 => "ML-KEM-1024",
193        }
194    }
195
196    /// Default security level for general use
197    pub(crate) const DEFAULT_SECURITY: SecurityLevel = SecurityLevel::Level3;
198
199    /// Performance vs Security trade-offs
200    pub(crate) const PERFORMANCE_NOTES: &str = "
201    Level 2 (ML-DSA-44, ML-KEM-512):
202      - Fastest performance
203      - Smallest key/signature sizes
204      - Suitable for most applications
205      - ~2-3x faster than Level 3
206
207    Level 3 (ML-DSA-65, ML-KEM-768): [RECOMMENDED]
208      - Balanced performance/security
209      - Recommended default
210      - Long-term security margin
211      - Moderate size increase
212
213    Level 5 (ML-DSA-87, ML-KEM-1024):
214      - Maximum security
215      - Largest keys/signatures
216      - ~2x slower than Level 3
217      - Use only when required
218    ";
219}
220
221#[cfg(test)]
222mod tests {
223    use super::*;
224
225    #[test]
226    fn test_parameter_consistency() {
227        // Verify Dilithium parameters match NIST spec
228        assert_eq!(dilithium::level2::PUBLIC_KEY_SIZE, 1312);
229        assert_eq!(dilithium::level3::PUBLIC_KEY_SIZE, 1952);
230        assert_eq!(dilithium::level5::PUBLIC_KEY_SIZE, 2592);
231
232        // Verify Kyber parameters match NIST spec
233        assert_eq!(kyber::kyber512::PUBLIC_KEY_SIZE, 800);
234        assert_eq!(kyber::kyber768::PUBLIC_KEY_SIZE, 1184);
235        assert_eq!(kyber::kyber1024::PUBLIC_KEY_SIZE, 1568);
236    }
237
238    #[test]
239    fn test_security_levels() {
240        assert_eq!(SecurityLevel::Level2.classical_bits(), 128);
241        assert_eq!(SecurityLevel::Level3.classical_bits(), 192);
242        assert_eq!(SecurityLevel::Level5.classical_bits(), 256);
243    }
244}