Skip to main content

quantaegis_sdk/
policy.rs

1//! Compliance security policies for commercial PQC migration.
2
3use quantaegis_core::{Error, Result};
4
5use crate::algorithm::{KemAlgorithm, SignatureAlgorithm};
6
7/// Security policy enforced across key generation, key import, and envelope encryption/decryption.
8#[derive(Copy, Clone, Debug, Eq, PartialEq, Default)]
9pub enum SecurityPolicy {
10    /// Allows all supported NIST PQC and hybrid KEMs (including Category 1 `ML-KEM-512`).
11    Permissive,
12    /// Aligned with **NIST IR 8547 (Post-2030)**: requires NIST-standardized PQC
13    /// (FIPS 203 / 204 / 205) or a PQ/T hybrid built on it, and excludes quantum-vulnerable
14    /// classical-only algorithms.
15    ///
16    /// **Currently accepts exactly the same algorithms as [`Self::Permissive`].** This is
17    /// intentional. IR 8547 deprecates and then disallows only quantum-vulnerable algorithms
18    /// (RSA, ECDSA, `EdDSA`, (EC)DH, ...). It keeps every parameter set of ML-KEM, ML-DSA and
19    /// SLH-DSA, including Category 1 / 2 sets such as `ML-KEM-512`, and it allows hybrids. Every
20    /// algorithm this SDK implements is in that set: ML-KEM, ML-DSA, SLH-DSA, `X-Wing` and
21    /// `MLDSA65-Ed25519-SHA512`. Unlike [`Self::Permissive`], this policy states the compliance
22    /// intent explicitly, and it must reject any classical-only algorithm, and any post-quantum
23    /// algorithm NIST has not standardized, that is added in the future. For a minimum security category, use [`Self::CryptrecCategory3Plus`].
24    NistPost2030,
25    /// Aligned with **Japan CRYPTREC PQC List (2026)** baseline: enforces **NIST Category 3+**
26    /// (`ML-KEM-768`, `ML-KEM-1024`, or `X-Wing`); rejects `ML-KEM-512`.
27    #[default]
28    CryptrecCategory3Plus,
29    /// Enforces **PQ + Classical Hybrid** (`X-Wing`) for migration windows requiring defense-in-depth
30    /// against both classical and quantum cryptanalysis.
31    HybridRequired,
32}
33
34impl SecurityPolicy {
35    /// Checks whether `alg` satisfies this security policy.
36    ///
37    /// Returns [`Error::PolicyViolation`] if `alg` is disallowed.
38    pub const fn validate_kem(self, alg: KemAlgorithm) -> Result<()> {
39        let allowed = match self {
40            // `NistPost2030`: every supported KEM is FIPS 203 ML-KEM or the X-Wing hybrid over
41            // ML-KEM-768, all permitted by NIST IR 8547 (see the variant docs). A classical-only
42            // KEM added later must be rejected here.
43            Self::Permissive | Self::NistPost2030 => true,
44            Self::CryptrecCategory3Plus => alg.nist_category() >= 3,
45            Self::HybridRequired => alg.is_hybrid(),
46        };
47
48        if allowed {
49            Ok(())
50        } else {
51            Err(Error::PolicyViolation)
52        }
53    }
54
55    /// Recommended default KEM algorithm under this policy (`X-Wing` satisfies all policies).
56    #[inline]
57    pub const fn default_kem(self) -> KemAlgorithm {
58        KemAlgorithm::XWing
59    }
60
61    /// Checks whether digital signature algorithm `alg` satisfies this security policy.
62    ///
63    /// Returns [`Error::PolicyViolation`] if `alg` is disallowed.
64    pub const fn validate_signature(self, alg: SignatureAlgorithm) -> Result<()> {
65        let allowed = match self {
66            // `NistPost2030`: every supported signature is FIPS 204 ML-DSA, FIPS 205 SLH-DSA or
67            // the ML-DSA-65 + Ed25519 composite, all permitted by NIST IR 8547 (see the variant
68            // docs). A classical-only signature added later must be rejected here.
69            Self::Permissive | Self::NistPost2030 => true,
70            Self::CryptrecCategory3Plus => alg.nist_category() >= 3,
71            Self::HybridRequired => alg.is_hybrid(),
72        };
73
74        if allowed {
75            Ok(())
76        } else {
77            Err(Error::PolicyViolation)
78        }
79    }
80
81    /// Recommended default digital signature algorithm under this policy.
82    ///
83    /// Returns [`SignatureAlgorithm::MlDsa65Ed25519`] for [`Self::HybridRequired`] and
84    /// [`SignatureAlgorithm::MlDsa65`] otherwise.
85    #[inline]
86    pub const fn default_signature(self) -> SignatureAlgorithm {
87        match self {
88            Self::HybridRequired => SignatureAlgorithm::MlDsa65Ed25519,
89            Self::Permissive | Self::NistPost2030 | Self::CryptrecCategory3Plus => {
90                SignatureAlgorithm::MlDsa65
91            }
92        }
93    }
94}
95
96#[cfg(test)]
97mod tests {
98    use super::*;
99
100    #[test]
101    fn policy_matrix_enforcement() {
102        assert_eq!(
103            SecurityPolicy::Permissive.validate_kem(KemAlgorithm::MlKem512),
104            Ok(())
105        );
106        assert_eq!(
107            SecurityPolicy::NistPost2030.validate_kem(KemAlgorithm::MlKem512),
108            Ok(())
109        );
110
111        // Japan CRYPTREC Category 3+ rejects ML-KEM-512, allows 768 / 1024 / X-Wing
112        assert_eq!(
113            SecurityPolicy::CryptrecCategory3Plus.validate_kem(KemAlgorithm::MlKem512),
114            Err(Error::PolicyViolation)
115        );
116        assert_eq!(
117            SecurityPolicy::CryptrecCategory3Plus.validate_kem(KemAlgorithm::MlKem768),
118            Ok(())
119        );
120        assert_eq!(
121            SecurityPolicy::CryptrecCategory3Plus.validate_kem(KemAlgorithm::MlKem1024),
122            Ok(())
123        );
124        assert_eq!(
125            SecurityPolicy::CryptrecCategory3Plus.validate_kem(KemAlgorithm::XWing),
126            Ok(())
127        );
128
129        // HybridRequired rejects pure PQC, allows X-Wing
130        assert_eq!(
131            SecurityPolicy::HybridRequired.validate_kem(KemAlgorithm::MlKem768),
132            Err(Error::PolicyViolation)
133        );
134        assert_eq!(
135            SecurityPolicy::HybridRequired.validate_kem(KemAlgorithm::XWing),
136            Ok(())
137        );
138    }
139
140    /// Pins the documented intent: `NistPost2030` admits every currently supported algorithm
141    /// (all are NIST PQC or PQ/T hybrids permitted by NIST IR 8547), i.e. the same set as
142    /// `Permissive`. Adding a classical-only or non-NIST algorithm must update this test.
143    #[test]
144    fn nist_post_2030_admits_all_supported_pqc_and_hybrid_algorithms() {
145        for alg in [
146            KemAlgorithm::MlKem512,
147            KemAlgorithm::MlKem768,
148            KemAlgorithm::MlKem1024,
149            KemAlgorithm::XWing,
150        ] {
151            assert_eq!(SecurityPolicy::NistPost2030.validate_kem(alg), Ok(()));
152            assert_eq!(SecurityPolicy::Permissive.validate_kem(alg), Ok(()));
153        }
154        for alg in [
155            SignatureAlgorithm::MlDsa44,
156            SignatureAlgorithm::MlDsa65,
157            SignatureAlgorithm::MlDsa87,
158            SignatureAlgorithm::SlhDsaShake128s,
159            SignatureAlgorithm::SlhDsaShake128f,
160            SignatureAlgorithm::SlhDsaShake192s,
161            SignatureAlgorithm::SlhDsaShake192f,
162            SignatureAlgorithm::SlhDsaShake256s,
163            SignatureAlgorithm::SlhDsaShake256f,
164            SignatureAlgorithm::MlDsa65Ed25519,
165        ] {
166            assert_eq!(SecurityPolicy::NistPost2030.validate_signature(alg), Ok(()));
167            assert_eq!(SecurityPolicy::Permissive.validate_signature(alg), Ok(()));
168        }
169    }
170}