Skip to main content

quantaegis_sdk/
algorithm.rs

1//! Algorithm identifiers and metadata for crypto-agile key management and envelopes.
2
3use quantaegis_core::{Error, Result};
4
5/// Supported Key Encapsulation Mechanism (KEM) algorithms.
6#[repr(u16)]
7#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
8pub enum KemAlgorithm {
9    /// NIST FIPS 203 ML-KEM-512 (NIST Security Category 1).
10    MlKem512 = 0x0101,
11    /// NIST FIPS 203 ML-KEM-768 (NIST Security Category 3).
12    MlKem768 = 0x0102,
13    /// NIST FIPS 203 ML-KEM-1024 (NIST Security Category 5).
14    MlKem1024 = 0x0103,
15    /// IETF/CFRG X-Wing Hybrid KEM (`ML-KEM-768` + `X25519`, NIST Category 3 + Classical 128-bit).
16    XWing = 0x0201,
17}
18
19impl KemAlgorithm {
20    /// Stable 16-bit big-endian wire identifier used in `AgilePublicKey` and `Envelope` headers.
21    #[inline]
22    pub const fn wire_id(self) -> u16 {
23        self as u16
24    }
25
26    /// Parses a 16-bit wire identifier.
27    pub const fn from_wire_id(id: u16) -> Result<Self> {
28        match id {
29            0x0101 => Ok(Self::MlKem512),
30            0x0102 => Ok(Self::MlKem768),
31            0x0103 => Ok(Self::MlKem1024),
32            0x0201 => Ok(Self::XWing),
33            _ => Err(Error::InvalidCiphertext),
34        }
35    }
36
37    /// Canonical human-readable algorithm name (suitable for CBOM / inventory reporting).
38    #[inline]
39    pub const fn name(self) -> &'static str {
40        match self {
41            Self::MlKem512 => "ML-KEM-512",
42            Self::MlKem768 => "ML-KEM-768",
43            Self::MlKem1024 => "ML-KEM-1024",
44            Self::XWing => "X-Wing",
45        }
46    }
47
48    /// NIST Post-Quantum Security Category (`1`, `3`, or `5`).
49    #[inline]
50    pub const fn nist_category(self) -> u8 {
51        match self {
52            Self::MlKem512 => 1,
53            Self::MlKem768 | Self::XWing => 3,
54            Self::MlKem1024 => 5,
55        }
56    }
57
58    /// Returns `true` if this algorithm combines both classical and post-quantum security.
59    #[inline]
60    pub const fn is_hybrid(self) -> bool {
61        matches!(self, Self::XWing)
62    }
63
64    /// Returns the official RFC 9935 / NIST CSOR OID string if standardized for X.509/PKCS#8.
65    #[inline]
66    pub const fn oid(self) -> Option<&'static str> {
67        match self {
68            Self::MlKem512 => Some("2.16.840.1.101.3.4.4.1"),
69            Self::MlKem768 => Some("2.16.840.1.101.3.4.4.2"),
70            Self::MlKem1024 => Some("2.16.840.1.101.3.4.4.3"),
71            Self::XWing => None,
72        }
73    }
74}
75
76/// Supported 256-bit quantum-resistant Authenticated Encryption with Associated Data (AEAD) schemes.
77#[repr(u8)]
78#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Default)]
79pub enum AeadAlgorithm {
80    /// AES-256-GCM (NIST SP 800-38D, 256-bit key, 96-bit nonce, 128-bit tag).
81    #[default]
82    Aes256Gcm = 0x01,
83    /// ChaCha20-Poly1305 (RFC 8439, 256-bit key, 96-bit nonce, 128-bit tag).
84    ChaCha20Poly1305 = 0x02,
85}
86
87impl AeadAlgorithm {
88    /// Stable 8-bit wire identifier used in `Envelope` headers.
89    #[inline]
90    pub const fn wire_id(self) -> u8 {
91        self as u8
92    }
93
94    /// Parses an 8-bit wire identifier.
95    pub const fn from_wire_id(id: u8) -> Result<Self> {
96        match id {
97            0x01 => Ok(Self::Aes256Gcm),
98            0x02 => Ok(Self::ChaCha20Poly1305),
99            _ => Err(Error::InvalidCiphertext),
100        }
101    }
102
103    /// Canonical algorithm name.
104    #[inline]
105    pub const fn name(self) -> &'static str {
106        match self {
107            Self::Aes256Gcm => "AES-256-GCM",
108            Self::ChaCha20Poly1305 => "ChaCha20-Poly1305",
109        }
110    }
111}
112
113/// Supported Post-Quantum & Hybrid Digital Signature algorithms.
114#[repr(u16)]
115#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
116pub enum SignatureAlgorithm {
117    /// NIST FIPS 204 ML-DSA-44 (NIST Security Category 2).
118    MlDsa44 = 0x0301,
119    /// NIST FIPS 204 ML-DSA-65 (NIST Security Category 3).
120    MlDsa65 = 0x0302,
121    /// NIST FIPS 204 ML-DSA-87 (NIST Security Category 5).
122    MlDsa87 = 0x0303,
123    /// NIST FIPS 205 SLH-DSA-SHAKE-128s (NIST Category 1, hash-only, small signature).
124    SlhDsaShake128s = 0x0401,
125    /// NIST FIPS 205 SLH-DSA-SHAKE-128f (NIST Category 1, hash-only, fast signing).
126    SlhDsaShake128f = 0x0402,
127    /// NIST FIPS 205 SLH-DSA-SHAKE-192s (NIST Category 3, hash-only, small signature).
128    SlhDsaShake192s = 0x0403,
129    /// NIST FIPS 205 SLH-DSA-SHAKE-192f (NIST Category 3, hash-only, fast signing).
130    SlhDsaShake192f = 0x0404,
131    /// NIST FIPS 205 SLH-DSA-SHAKE-256s (NIST Category 5, hash-only, small signature).
132    SlhDsaShake256s = 0x0405,
133    /// NIST FIPS 205 SLH-DSA-SHAKE-256f (NIST Category 5, hash-only, fast signing).
134    SlhDsaShake256f = 0x0406,
135    /// IETF LAMPS Composite ML-DSA (`MLDSA65-Ed25519-SHA512`, NIST Category 3 + Classical 128-bit).
136    MlDsa65Ed25519 = 0x0501,
137}
138
139impl SignatureAlgorithm {
140    /// Stable 16-bit big-endian wire identifier used in agile signature keys and envelopes.
141    #[inline]
142    pub const fn wire_id(self) -> u16 {
143        self as u16
144    }
145
146    /// Parses a 16-bit wire identifier.
147    pub const fn from_wire_id(id: u16) -> Result<Self> {
148        match id {
149            0x0301 => Ok(Self::MlDsa44),
150            0x0302 => Ok(Self::MlDsa65),
151            0x0303 => Ok(Self::MlDsa87),
152            0x0401 => Ok(Self::SlhDsaShake128s),
153            0x0402 => Ok(Self::SlhDsaShake128f),
154            0x0403 => Ok(Self::SlhDsaShake192s),
155            0x0404 => Ok(Self::SlhDsaShake192f),
156            0x0405 => Ok(Self::SlhDsaShake256s),
157            0x0406 => Ok(Self::SlhDsaShake256f),
158            0x0501 => Ok(Self::MlDsa65Ed25519),
159            _ => Err(Error::InvalidSignature),
160        }
161    }
162
163    /// Canonical human-readable algorithm name (suitable for CBOM / inventory reporting).
164    #[inline]
165    pub const fn name(self) -> &'static str {
166        match self {
167            Self::MlDsa44 => "ML-DSA-44",
168            Self::MlDsa65 => "ML-DSA-65",
169            Self::MlDsa87 => "ML-DSA-87",
170            Self::SlhDsaShake128s => "SLH-DSA-SHAKE-128s",
171            Self::SlhDsaShake128f => "SLH-DSA-SHAKE-128f",
172            Self::SlhDsaShake192s => "SLH-DSA-SHAKE-192s",
173            Self::SlhDsaShake192f => "SLH-DSA-SHAKE-192f",
174            Self::SlhDsaShake256s => "SLH-DSA-SHAKE-256s",
175            Self::SlhDsaShake256f => "SLH-DSA-SHAKE-256f",
176            Self::MlDsa65Ed25519 => "MLDSA65-Ed25519-SHA512",
177        }
178    }
179
180    /// NIST Post-Quantum Security Category (`1`, `2`, `3`, or `5`).
181    #[inline]
182    pub const fn nist_category(self) -> u8 {
183        match self {
184            Self::SlhDsaShake128s | Self::SlhDsaShake128f => 1,
185            Self::MlDsa44 => 2,
186            Self::MlDsa65
187            | Self::SlhDsaShake192s
188            | Self::SlhDsaShake192f
189            | Self::MlDsa65Ed25519 => 3,
190            Self::MlDsa87 | Self::SlhDsaShake256s | Self::SlhDsaShake256f => 5,
191        }
192    }
193
194    /// Returns `true` if this algorithm combines classical and post-quantum security.
195    #[inline]
196    pub const fn is_hybrid(self) -> bool {
197        matches!(self, Self::MlDsa65Ed25519)
198    }
199
200    /// Returns the official RFC 9881 / RFC 9909 NIST CSOR OID string if standardized.
201    #[inline]
202    pub const fn oid(self) -> Option<&'static str> {
203        match self {
204            Self::MlDsa44 => Some("2.16.840.1.101.3.4.3.17"),
205            Self::MlDsa65 => Some("2.16.840.1.101.3.4.3.18"),
206            Self::MlDsa87 => Some("2.16.840.1.101.3.4.3.19"),
207            Self::SlhDsaShake128s => Some("2.16.840.1.101.3.4.3.26"),
208            Self::SlhDsaShake128f => Some("2.16.840.1.101.3.4.3.27"),
209            Self::SlhDsaShake192s => Some("2.16.840.1.101.3.4.3.28"),
210            Self::SlhDsaShake192f => Some("2.16.840.1.101.3.4.3.29"),
211            Self::SlhDsaShake256s => Some("2.16.840.1.101.3.4.3.30"),
212            Self::SlhDsaShake256f => Some("2.16.840.1.101.3.4.3.31"),
213            Self::MlDsa65Ed25519 => None,
214        }
215    }
216}