quantcrypt/kem/api/
algorithm.rs

1use crate::kem::common::{config::oids::Oid, kem_type::KemType};
2use strum::IntoEnumIterator;
3use strum_macros::{Display, EnumIter};
4
5#[derive(Clone, Debug, PartialEq, EnumIter, Display, Copy)]
6/// The permissible algorithms for the `AlgorithmIdentifier` type.
7pub enum KemAlgorithm {
8    /// Pure KEMs
9    MlKem512,
10    MlKem768,
11    MlKem1024,
12
13    // The composite algorithm list from the old version
14    MlKem512P256,
15    MlKem512BrainpoolP256r1,
16    MlKem512X25519,
17    MlKem512Rsa2048,
18    MlKem512Rsa3072,
19    MlKem768P256,
20
21    // The compsite algorithm list is from the latest editor's draft:
22    //https://lamps-wg.github.io/draft-composite-kem/draft-ietf-lamps-pq-composite-kem.html
23    MlKem768Rsa2048,
24    MlKem768Rsa3072,
25    MlKem768Rsa4096,
26    MlKem768X25519,
27    MlKem768P384,
28    MlKem768BrainpoolP256r1,
29    MlKem1024P384,
30    MlKem1024BrainpoolP384r1,
31    MlKem1024X448,
32}
33
34impl KemAlgorithm {
35    /// Get all KEM algorithms
36    pub(crate) fn all() -> Vec<KemAlgorithm> {
37        KemAlgorithm::iter().collect()
38    }
39
40    /// Get the corresponding `DsaType` for the algorithm
41    pub(crate) fn get_kem_type(&self) -> KemType {
42        match self {
43            // Pure KEMs
44            KemAlgorithm::MlKem512 => KemType::MlKem512,
45            KemAlgorithm::MlKem768 => KemType::MlKem768,
46            KemAlgorithm::MlKem1024 => KemType::MlKem1024,
47
48            // The composite algorithm list from the old version
49            KemAlgorithm::MlKem512P256 => KemType::MlKem512P256,
50            KemAlgorithm::MlKem512BrainpoolP256r1 => KemType::MlKem512BrainpoolP256r1,
51            KemAlgorithm::MlKem512X25519 => KemType::MlKem512X25519,
52            KemAlgorithm::MlKem512Rsa2048 => KemType::MlKem512Rsa2048,
53            KemAlgorithm::MlKem512Rsa3072 => KemType::MlKem512Rsa3072,
54            KemAlgorithm::MlKem768P256 => KemType::MlKem768P256,
55
56            // The compsite algorithm list is from the latest editor's draft:
57            //https://lamps-wg.github.io/draft-composite-kem/draft-ietf-lamps-pq-composite-kem.html
58            KemAlgorithm::MlKem768Rsa2048 => KemType::MlKem768Rsa2048,
59            KemAlgorithm::MlKem768Rsa3072 => KemType::MlKem768Rsa3072,
60            KemAlgorithm::MlKem768Rsa4096 => KemType::MlKem768Rsa4096,
61            KemAlgorithm::MlKem768X25519 => KemType::MlKem768X25519,
62            KemAlgorithm::MlKem768P384 => KemType::MlKem768P384,
63            KemAlgorithm::MlKem768BrainpoolP256r1 => KemType::MlKem768BrainpoolP256r1,
64            KemAlgorithm::MlKem1024P384 => KemType::MlKem1024P384,
65            KemAlgorithm::MlKem1024BrainpoolP384r1 => KemType::MlKem1024BrainpoolP384r1,
66            KemAlgorithm::MlKem1024X448 => KemType::MlKem1024X448,
67        }
68    }
69
70    /// Check if the algorithm is a composite or pure algorithm
71    ///
72    /// # Returns
73    ///
74    /// True if the algorithm is a composite algorithm, false otherwise
75    pub fn is_composite(&self) -> bool {
76        !matches!(
77            self,
78            KemAlgorithm::MlKem512 | KemAlgorithm::MlKem768 | KemAlgorithm::MlKem1024
79        )
80    }
81
82    /// Get the OID for the algorithm
83    ///
84    /// # Returns
85    ///
86    /// The OID for the algorithm
87    pub fn get_oid(&self) -> String {
88        self.get_kem_type().get_oid()
89    }
90
91    /// Get the KEM algorithm from an OID
92    ///
93    /// # Arguments
94    ///
95    /// * `oid` - The OID of the KEM algorithm
96    ///
97    /// # Returns
98    ///
99    /// The KEM algorithm corresponding to the OID, or None if the OID is not recognized
100    pub fn from_oid(oid: &str) -> Option<KemAlgorithm> {
101        KemAlgorithm::all()
102            .iter()
103            .find(|x| x.get_oid() == oid)
104            .cloned()
105    }
106}